blob: 5bc8edb4c2a60ce82bfd03ac55b48c7f3003fde3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Yan, Zheng25e6bae2014-09-16 19:15:28 +08003#include <linux/ceph/pagelist.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004
Sage Weil355da1e2009-10-06 11:31:08 -07005#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07006#include "mds_client.h"
7
8#include <linux/ceph/decode.h>
Sage Weil355da1e2009-10-06 11:31:08 -07009
10#include <linux/xattr.h>
Linus Torvalds4db658e2014-01-28 18:06:18 -080011#include <linux/posix_acl_xattr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Sage Weil355da1e2009-10-06 11:31:08 -070013
Alex Elder22891902012-01-23 15:49:28 -060014#define XATTR_CEPH_PREFIX "ceph."
15#define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
16
Yan, Zhengbcdfeb22014-02-11 13:04:19 +080017static int __remove_xattr(struct ceph_inode_info *ci,
18 struct ceph_inode_xattr *xattr);
19
Wei Yongjun5130cce2016-10-17 15:11:29 +000020static const struct xattr_handler ceph_other_xattr_handler;
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +020021
Guangliang Zhao7221fe42013-11-11 15:18:03 +080022/*
23 * List of handlers for synthetic system.* attributes. Other
24 * attributes are handled directly.
25 */
26const struct xattr_handler *ceph_xattr_handlers[] = {
27#ifdef CONFIG_CEPH_FS_POSIX_ACL
Linus Torvalds4db658e2014-01-28 18:06:18 -080028 &posix_acl_access_xattr_handler,
29 &posix_acl_default_xattr_handler,
Guangliang Zhao7221fe42013-11-11 15:18:03 +080030#endif
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +020031 &ceph_other_xattr_handler,
Guangliang Zhao7221fe42013-11-11 15:18:03 +080032 NULL,
33};
34
Sage Weil355da1e2009-10-06 11:31:08 -070035static bool ceph_is_valid_xattr(const char *name)
36{
Alex Elder22891902012-01-23 15:49:28 -060037 return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
Sage Weil1a756272010-05-11 11:40:25 -070038 !strncmp(name, XATTR_SECURITY_PREFIX,
Sage Weil355da1e2009-10-06 11:31:08 -070039 XATTR_SECURITY_PREFIX_LEN) ||
40 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
41 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
42}
43
44/*
45 * These define virtual xattrs exposing the recursive directory
46 * statistics and layout metadata.
47 */
Alex Elder881a5fa2012-01-23 15:49:28 -060048struct ceph_vxattr {
Sage Weil355da1e2009-10-06 11:31:08 -070049 char *name;
Alex Elder3ce6cd12012-01-23 15:49:28 -060050 size_t name_size; /* strlen(name) + 1 (for '\0') */
Sage Weil355da1e2009-10-06 11:31:08 -070051 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
52 size_t size);
Sage Weilf36e4472013-01-20 21:59:29 -080053 bool (*exists_cb)(struct ceph_inode_info *ci);
Yan, Zheng4e9906e2018-04-25 17:14:05 +080054 unsigned int flags;
Sage Weil355da1e2009-10-06 11:31:08 -070055};
56
Yan, Zheng4e9906e2018-04-25 17:14:05 +080057#define VXATTR_FLAG_READONLY (1<<0)
58#define VXATTR_FLAG_HIDDEN (1<<1)
Yan, Zheng49a9f4f2018-04-25 17:30:23 +080059#define VXATTR_FLAG_RSTAT (1<<2)
Yan, Zheng4e9906e2018-04-25 17:14:05 +080060
Sage Weil32ab0bd2013-01-19 16:46:32 -080061/* layouts */
62
63static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
64{
Yan, Zheng779fe0f2016-03-07 09:35:06 +080065 struct ceph_file_layout *fl = &ci->i_layout;
66 return (fl->stripe_unit > 0 || fl->stripe_count > 0 ||
67 fl->object_size > 0 || fl->pool_id >= 0 ||
68 rcu_dereference_raw(fl->pool_ns) != NULL);
Sage Weil32ab0bd2013-01-19 16:46:32 -080069}
70
71static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
Yan, Zheng1e5c6642014-03-24 13:00:54 +080072 size_t size)
Sage Weil32ab0bd2013-01-19 16:46:32 -080073{
Sage Weil32ab0bd2013-01-19 16:46:32 -080074 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
75 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zheng779fe0f2016-03-07 09:35:06 +080076 struct ceph_string *pool_ns;
Yan, Zheng76271512016-02-03 21:24:49 +080077 s64 pool = ci->i_layout.pool_id;
Sage Weil32ab0bd2013-01-19 16:46:32 -080078 const char *pool_name;
Yan, Zheng779fe0f2016-03-07 09:35:06 +080079 const char *ns_field = " pool_namespace=";
Yan, Zheng1e5c6642014-03-24 13:00:54 +080080 char buf[128];
Yan, Zheng779fe0f2016-03-07 09:35:06 +080081 size_t len, total_len = 0;
82 int ret;
83
84 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
Sage Weil32ab0bd2013-01-19 16:46:32 -080085
86 dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020087 down_read(&osdc->lock);
Sage Weil32ab0bd2013-01-19 16:46:32 -080088 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
Yan, Zheng1e5c6642014-03-24 13:00:54 +080089 if (pool_name) {
Yan, Zheng779fe0f2016-03-07 09:35:06 +080090 len = snprintf(buf, sizeof(buf),
Yan, Zheng76271512016-02-03 21:24:49 +080091 "stripe_unit=%u stripe_count=%u object_size=%u pool=",
92 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
93 ci->i_layout.object_size);
Yan, Zheng779fe0f2016-03-07 09:35:06 +080094 total_len = len + strlen(pool_name);
Yan, Zheng1e5c6642014-03-24 13:00:54 +080095 } else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +080096 len = snprintf(buf, sizeof(buf),
Yan, Zheng76271512016-02-03 21:24:49 +080097 "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
98 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
99 ci->i_layout.object_size, (unsigned long long)pool);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800100 total_len = len;
101 }
102
103 if (pool_ns)
104 total_len += strlen(ns_field) + pool_ns->len;
105
106 if (!size) {
107 ret = total_len;
108 } else if (total_len > size) {
109 ret = -ERANGE;
110 } else {
111 memcpy(val, buf, len);
112 ret = len;
113 if (pool_name) {
114 len = strlen(pool_name);
115 memcpy(val + ret, pool_name, len);
116 ret += len;
117 }
118 if (pool_ns) {
119 len = strlen(ns_field);
120 memcpy(val + ret, ns_field, len);
121 ret += len;
122 memcpy(val + ret, pool_ns->str, pool_ns->len);
123 ret += pool_ns->len;
Yan, Zheng1e5c6642014-03-24 13:00:54 +0800124 }
125 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200126 up_read(&osdc->lock);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800127 ceph_put_string(pool_ns);
Sage Weil32ab0bd2013-01-19 16:46:32 -0800128 return ret;
129}
130
Sage Weil695b7112013-01-20 22:08:33 -0800131static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
132 char *val, size_t size)
133{
Yan, Zheng76271512016-02-03 21:24:49 +0800134 return snprintf(val, size, "%u", ci->i_layout.stripe_unit);
Sage Weil695b7112013-01-20 22:08:33 -0800135}
136
137static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
138 char *val, size_t size)
139{
Yan, Zheng76271512016-02-03 21:24:49 +0800140 return snprintf(val, size, "%u", ci->i_layout.stripe_count);
Sage Weil695b7112013-01-20 22:08:33 -0800141}
142
143static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
144 char *val, size_t size)
145{
Yan, Zheng76271512016-02-03 21:24:49 +0800146 return snprintf(val, size, "%u", ci->i_layout.object_size);
Sage Weil695b7112013-01-20 22:08:33 -0800147}
148
149static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
150 char *val, size_t size)
151{
152 int ret;
153 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
154 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zheng76271512016-02-03 21:24:49 +0800155 s64 pool = ci->i_layout.pool_id;
Sage Weil695b7112013-01-20 22:08:33 -0800156 const char *pool_name;
157
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200158 down_read(&osdc->lock);
Sage Weil695b7112013-01-20 22:08:33 -0800159 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
160 if (pool_name)
161 ret = snprintf(val, size, "%s", pool_name);
162 else
163 ret = snprintf(val, size, "%lld", (unsigned long long)pool);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200164 up_read(&osdc->lock);
Sage Weil695b7112013-01-20 22:08:33 -0800165 return ret;
166}
167
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800168static size_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
169 char *val, size_t size)
170{
171 int ret = 0;
172 struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
173 if (ns) {
174 ret = snprintf(val, size, "%.*s", (int)ns->len, ns->str);
175 ceph_put_string(ns);
176 }
177 return ret;
178}
179
Sage Weil355da1e2009-10-06 11:31:08 -0700180/* directories */
181
Alex Elderaa4066e2012-01-23 15:49:28 -0600182static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700183 size_t size)
184{
185 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
186}
187
Alex Elderaa4066e2012-01-23 15:49:28 -0600188static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700189 size_t size)
190{
191 return snprintf(val, size, "%lld", ci->i_files);
192}
193
Alex Elderaa4066e2012-01-23 15:49:28 -0600194static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700195 size_t size)
196{
197 return snprintf(val, size, "%lld", ci->i_subdirs);
198}
199
Alex Elderaa4066e2012-01-23 15:49:28 -0600200static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700201 size_t size)
202{
203 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
204}
205
Alex Elderaa4066e2012-01-23 15:49:28 -0600206static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700207 size_t size)
208{
209 return snprintf(val, size, "%lld", ci->i_rfiles);
210}
211
Alex Elderaa4066e2012-01-23 15:49:28 -0600212static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700213 size_t size)
214{
215 return snprintf(val, size, "%lld", ci->i_rsubdirs);
216}
217
Alex Elderaa4066e2012-01-23 15:49:28 -0600218static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700219 size_t size)
220{
221 return snprintf(val, size, "%lld", ci->i_rbytes);
222}
223
Alex Elderaa4066e2012-01-23 15:49:28 -0600224static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700225 size_t size)
226{
Alex Elder3489b422012-03-08 16:50:09 -0600227 return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
Sage Weil355da1e2009-10-06 11:31:08 -0700228 (long)ci->i_rctime.tv_nsec);
229}
230
Luis Henriquesfb18a572018-01-05 10:47:18 +0000231/* quotas */
232
233static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
234{
Yan, Zhengf1919822018-04-08 09:54:39 +0800235 bool ret = false;
236 spin_lock(&ci->i_ceph_lock);
237 if ((ci->i_max_files || ci->i_max_bytes) &&
238 ci->i_vino.snap == CEPH_NOSNAP &&
239 ci->i_snap_realm &&
240 ci->i_snap_realm->ino == ci->i_vino.ino)
241 ret = true;
242 spin_unlock(&ci->i_ceph_lock);
243 return ret;
Luis Henriquesfb18a572018-01-05 10:47:18 +0000244}
245
246static size_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
247 size_t size)
248{
249 return snprintf(val, size, "max_bytes=%llu max_files=%llu",
250 ci->i_max_bytes, ci->i_max_files);
251}
252
253static size_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
254 char *val, size_t size)
255{
256 return snprintf(val, size, "%llu", ci->i_max_bytes);
257}
258
259static size_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
260 char *val, size_t size)
261{
262 return snprintf(val, size, "%llu", ci->i_max_files);
263}
Sage Weil32ab0bd2013-01-19 16:46:32 -0800264
Alex Eldereb788082012-01-23 15:49:28 -0600265#define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
Sage Weil695b7112013-01-20 22:08:33 -0800266#define CEPH_XATTR_NAME2(_type, _name, _name2) \
267 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
Alex Eldereb788082012-01-23 15:49:28 -0600268
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800269#define XATTR_NAME_CEPH(_type, _name, _flags) \
Sage Weil88601472013-01-31 11:53:27 -0800270 { \
271 .name = CEPH_XATTR_NAME(_type, _name), \
272 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
273 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800274 .exists_cb = NULL, \
275 .flags = (VXATTR_FLAG_READONLY | _flags), \
Sage Weil88601472013-01-31 11:53:27 -0800276 }
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800277#define XATTR_RSTAT_FIELD(_type, _name) \
278 XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
Sage Weil695b7112013-01-20 22:08:33 -0800279#define XATTR_LAYOUT_FIELD(_type, _name, _field) \
280 { \
281 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
282 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
283 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
Sage Weil695b7112013-01-20 22:08:33 -0800284 .exists_cb = ceph_vxattrcb_layout_exists, \
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800285 .flags = VXATTR_FLAG_HIDDEN, \
Sage Weil695b7112013-01-20 22:08:33 -0800286 }
Luis Henriquesfb18a572018-01-05 10:47:18 +0000287#define XATTR_QUOTA_FIELD(_type, _name) \
288 { \
289 .name = CEPH_XATTR_NAME(_type, _name), \
290 .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
291 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
Luis Henriquesfb18a572018-01-05 10:47:18 +0000292 .exists_cb = ceph_vxattrcb_quota_exists, \
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800293 .flags = VXATTR_FLAG_HIDDEN, \
Luis Henriquesfb18a572018-01-05 10:47:18 +0000294 }
Alex Eldereb788082012-01-23 15:49:28 -0600295
Alex Elder881a5fa2012-01-23 15:49:28 -0600296static struct ceph_vxattr ceph_dir_vxattrs[] = {
Sage Weil1f08f2b2013-01-20 22:07:12 -0800297 {
298 .name = "ceph.dir.layout",
299 .name_size = sizeof("ceph.dir.layout"),
300 .getxattr_cb = ceph_vxattrcb_layout,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800301 .exists_cb = ceph_vxattrcb_layout_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800302 .flags = VXATTR_FLAG_HIDDEN,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800303 },
Sage Weil695b7112013-01-20 22:08:33 -0800304 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
305 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
306 XATTR_LAYOUT_FIELD(dir, layout, object_size),
307 XATTR_LAYOUT_FIELD(dir, layout, pool),
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800308 XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800309 XATTR_NAME_CEPH(dir, entries, 0),
310 XATTR_NAME_CEPH(dir, files, 0),
311 XATTR_NAME_CEPH(dir, subdirs, 0),
312 XATTR_RSTAT_FIELD(dir, rentries),
313 XATTR_RSTAT_FIELD(dir, rfiles),
314 XATTR_RSTAT_FIELD(dir, rsubdirs),
315 XATTR_RSTAT_FIELD(dir, rbytes),
316 XATTR_RSTAT_FIELD(dir, rctime),
Luis Henriquesfb18a572018-01-05 10:47:18 +0000317 {
318 .name = "ceph.quota",
319 .name_size = sizeof("ceph.quota"),
320 .getxattr_cb = ceph_vxattrcb_quota,
Luis Henriquesfb18a572018-01-05 10:47:18 +0000321 .exists_cb = ceph_vxattrcb_quota_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800322 .flags = VXATTR_FLAG_HIDDEN,
Luis Henriquesfb18a572018-01-05 10:47:18 +0000323 },
324 XATTR_QUOTA_FIELD(quota, max_bytes),
325 XATTR_QUOTA_FIELD(quota, max_files),
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600326 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700327};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600328static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700329
330/* files */
331
Alex Elder881a5fa2012-01-23 15:49:28 -0600332static struct ceph_vxattr ceph_file_vxattrs[] = {
Sage Weil32ab0bd2013-01-19 16:46:32 -0800333 {
334 .name = "ceph.file.layout",
335 .name_size = sizeof("ceph.file.layout"),
336 .getxattr_cb = ceph_vxattrcb_layout,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800337 .exists_cb = ceph_vxattrcb_layout_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800338 .flags = VXATTR_FLAG_HIDDEN,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800339 },
Sage Weil695b7112013-01-20 22:08:33 -0800340 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
341 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
342 XATTR_LAYOUT_FIELD(file, layout, object_size),
343 XATTR_LAYOUT_FIELD(file, layout, pool),
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800344 XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600345 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700346};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600347static size_t ceph_file_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700348
Alex Elder881a5fa2012-01-23 15:49:28 -0600349static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -0700350{
351 if (S_ISDIR(inode->i_mode))
352 return ceph_dir_vxattrs;
353 else if (S_ISREG(inode->i_mode))
354 return ceph_file_vxattrs;
355 return NULL;
356}
357
Alex Elder3ce6cd12012-01-23 15:49:28 -0600358static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
359{
360 if (vxattrs == ceph_dir_vxattrs)
361 return ceph_dir_vxattrs_name_size;
362 if (vxattrs == ceph_file_vxattrs)
363 return ceph_file_vxattrs_name_size;
Yan, Zheng0abb43d2014-09-18 16:11:12 +0800364 BUG_ON(vxattrs);
Alex Elder3ce6cd12012-01-23 15:49:28 -0600365 return 0;
366}
367
368/*
369 * Compute the aggregate size (including terminating '\0') of all
370 * virtual extended attribute names in the given vxattr table.
371 */
372static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
373{
374 struct ceph_vxattr *vxattr;
375 size_t size = 0;
376
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800377 for (vxattr = vxattrs; vxattr->name; vxattr++) {
378 if (!(vxattr->flags & VXATTR_FLAG_HIDDEN))
Sage Weil88601472013-01-31 11:53:27 -0800379 size += vxattr->name_size;
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800380 }
Alex Elder3ce6cd12012-01-23 15:49:28 -0600381
382 return size;
383}
384
385/* Routines called at initialization and exit time */
386
387void __init ceph_xattr_init(void)
388{
389 ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
390 ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
391}
392
393void ceph_xattr_exit(void)
394{
395 ceph_dir_vxattrs_name_size = 0;
396 ceph_file_vxattrs_name_size = 0;
397}
398
Alex Elder881a5fa2012-01-23 15:49:28 -0600399static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
Sage Weil355da1e2009-10-06 11:31:08 -0700400 const char *name)
401{
Alex Elder881a5fa2012-01-23 15:49:28 -0600402 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
Alex Elder06476a62012-01-23 15:49:27 -0600403
404 if (vxattr) {
405 while (vxattr->name) {
406 if (!strcmp(vxattr->name, name))
407 return vxattr;
408 vxattr++;
409 }
410 }
411
Sage Weil355da1e2009-10-06 11:31:08 -0700412 return NULL;
413}
414
415static int __set_xattr(struct ceph_inode_info *ci,
416 const char *name, int name_len,
417 const char *val, int val_len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800418 int flags, int update_xattr,
Sage Weil355da1e2009-10-06 11:31:08 -0700419 struct ceph_inode_xattr **newxattr)
420{
421 struct rb_node **p;
422 struct rb_node *parent = NULL;
423 struct ceph_inode_xattr *xattr = NULL;
424 int c;
425 int new = 0;
426
427 p = &ci->i_xattrs.index.rb_node;
428 while (*p) {
429 parent = *p;
430 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
431 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
432 if (c < 0)
433 p = &(*p)->rb_left;
434 else if (c > 0)
435 p = &(*p)->rb_right;
436 else {
437 if (name_len == xattr->name_len)
438 break;
439 else if (name_len < xattr->name_len)
440 p = &(*p)->rb_left;
441 else
442 p = &(*p)->rb_right;
443 }
444 xattr = NULL;
445 }
446
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800447 if (update_xattr) {
448 int err = 0;
Luis Henriqueseeca9582017-04-28 11:14:04 +0100449
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800450 if (xattr && (flags & XATTR_CREATE))
451 err = -EEXIST;
452 else if (!xattr && (flags & XATTR_REPLACE))
453 err = -ENODATA;
454 if (err) {
455 kfree(name);
456 kfree(val);
Luis Henriqueseeca9582017-04-28 11:14:04 +0100457 kfree(*newxattr);
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800458 return err;
459 }
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800460 if (update_xattr < 0) {
461 if (xattr)
462 __remove_xattr(ci, xattr);
463 kfree(name);
Luis Henriqueseeca9582017-04-28 11:14:04 +0100464 kfree(*newxattr);
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800465 return 0;
466 }
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800467 }
468
Sage Weil355da1e2009-10-06 11:31:08 -0700469 if (!xattr) {
470 new = 1;
471 xattr = *newxattr;
472 xattr->name = name;
473 xattr->name_len = name_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800474 xattr->should_free_name = update_xattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700475
476 ci->i_xattrs.count++;
477 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
478 } else {
479 kfree(*newxattr);
480 *newxattr = NULL;
481 if (xattr->should_free_val)
482 kfree((void *)xattr->val);
483
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800484 if (update_xattr) {
Sage Weil355da1e2009-10-06 11:31:08 -0700485 kfree((void *)name);
486 name = xattr->name;
487 }
488 ci->i_xattrs.names_size -= xattr->name_len;
489 ci->i_xattrs.vals_size -= xattr->val_len;
490 }
Sage Weil355da1e2009-10-06 11:31:08 -0700491 ci->i_xattrs.names_size += name_len;
492 ci->i_xattrs.vals_size += val_len;
493 if (val)
494 xattr->val = val;
495 else
496 xattr->val = "";
497
498 xattr->val_len = val_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800499 xattr->dirty = update_xattr;
500 xattr->should_free_val = (val && update_xattr);
Sage Weil355da1e2009-10-06 11:31:08 -0700501
502 if (new) {
503 rb_link_node(&xattr->node, parent, p);
504 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
505 dout("__set_xattr_val p=%p\n", p);
506 }
507
508 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
509 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
510
511 return 0;
512}
513
514static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
515 const char *name)
516{
517 struct rb_node **p;
518 struct rb_node *parent = NULL;
519 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800520 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700521 int c;
522
523 p = &ci->i_xattrs.index.rb_node;
524 while (*p) {
525 parent = *p;
526 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
527 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800528 if (c == 0 && name_len > xattr->name_len)
529 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700530 if (c < 0)
531 p = &(*p)->rb_left;
532 else if (c > 0)
533 p = &(*p)->rb_right;
534 else {
535 dout("__get_xattr %s: found %.*s\n", name,
536 xattr->val_len, xattr->val);
537 return xattr;
538 }
539 }
540
541 dout("__get_xattr %s: not found\n", name);
542
543 return NULL;
544}
545
546static void __free_xattr(struct ceph_inode_xattr *xattr)
547{
548 BUG_ON(!xattr);
549
550 if (xattr->should_free_name)
551 kfree((void *)xattr->name);
552 if (xattr->should_free_val)
553 kfree((void *)xattr->val);
554
555 kfree(xattr);
556}
557
558static int __remove_xattr(struct ceph_inode_info *ci,
559 struct ceph_inode_xattr *xattr)
560{
561 if (!xattr)
Yan, Zheng524186a2014-02-11 13:23:09 +0800562 return -ENODATA;
Sage Weil355da1e2009-10-06 11:31:08 -0700563
564 rb_erase(&xattr->node, &ci->i_xattrs.index);
565
566 if (xattr->should_free_name)
567 kfree((void *)xattr->name);
568 if (xattr->should_free_val)
569 kfree((void *)xattr->val);
570
571 ci->i_xattrs.names_size -= xattr->name_len;
572 ci->i_xattrs.vals_size -= xattr->val_len;
573 ci->i_xattrs.count--;
574 kfree(xattr);
575
576 return 0;
577}
578
Sage Weil355da1e2009-10-06 11:31:08 -0700579static char *__copy_xattr_names(struct ceph_inode_info *ci,
580 char *dest)
581{
582 struct rb_node *p;
583 struct ceph_inode_xattr *xattr = NULL;
584
585 p = rb_first(&ci->i_xattrs.index);
586 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
587
588 while (p) {
589 xattr = rb_entry(p, struct ceph_inode_xattr, node);
590 memcpy(dest, xattr->name, xattr->name_len);
591 dest[xattr->name_len] = '\0';
592
593 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
594 xattr->name_len, ci->i_xattrs.names_size);
595
596 dest += xattr->name_len + 1;
597 p = rb_next(p);
598 }
599
600 return dest;
601}
602
603void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
604{
605 struct rb_node *p, *tmp;
606 struct ceph_inode_xattr *xattr = NULL;
607
608 p = rb_first(&ci->i_xattrs.index);
609
610 dout("__ceph_destroy_xattrs p=%p\n", p);
611
612 while (p) {
613 xattr = rb_entry(p, struct ceph_inode_xattr, node);
614 tmp = p;
615 p = rb_next(tmp);
616 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
617 xattr->name_len, xattr->name);
618 rb_erase(tmp, &ci->i_xattrs.index);
619
620 __free_xattr(xattr);
621 }
622
623 ci->i_xattrs.names_size = 0;
624 ci->i_xattrs.vals_size = 0;
625 ci->i_xattrs.index_version = 0;
626 ci->i_xattrs.count = 0;
627 ci->i_xattrs.index = RB_ROOT;
628}
629
630static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800631 __releases(ci->i_ceph_lock)
632 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700633{
634 u32 namelen;
635 u32 numattr = 0;
636 void *p, *end;
637 u32 len;
638 const char *name, *val;
639 struct ceph_inode_info *ci = ceph_inode(inode);
640 int xattr_version;
641 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800642 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700643 int i;
644
645 dout("__build_xattrs() len=%d\n",
646 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
647
648 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
649 return 0; /* already built */
650
651 __ceph_destroy_xattrs(ci);
652
653start:
654 /* updated internal xattr rb tree */
655 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
656 p = ci->i_xattrs.blob->vec.iov_base;
657 end = p + ci->i_xattrs.blob->vec.iov_len;
658 ceph_decode_32_safe(&p, end, numattr, bad);
659 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800660 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700661
Ilya Dryomov7e8a2952014-07-25 11:47:21 +0400662 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
Sage Weil355da1e2009-10-06 11:31:08 -0700663 GFP_NOFS);
664 err = -ENOMEM;
665 if (!xattrs)
666 goto bad_lock;
Ilya Dryomov1a295bd2014-07-25 12:44:58 +0400667
Sage Weil355da1e2009-10-06 11:31:08 -0700668 for (i = 0; i < numattr; i++) {
669 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
670 GFP_NOFS);
671 if (!xattrs[i])
672 goto bad_lock;
673 }
674
Sage Weilbe655592011-11-30 09:47:09 -0800675 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700676 if (ci->i_xattrs.version != xattr_version) {
677 /* lost a race, retry */
678 for (i = 0; i < numattr; i++)
679 kfree(xattrs[i]);
680 kfree(xattrs);
Alan Cox21ec6ff2012-07-20 08:18:36 -0500681 xattrs = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700682 goto start;
683 }
684 err = -EIO;
685 while (numattr--) {
686 ceph_decode_32_safe(&p, end, len, bad);
687 namelen = len;
688 name = p;
689 p += len;
690 ceph_decode_32_safe(&p, end, len, bad);
691 val = p;
692 p += len;
693
694 err = __set_xattr(ci, name, namelen, val, len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800695 0, 0, &xattrs[numattr]);
Sage Weil355da1e2009-10-06 11:31:08 -0700696
697 if (err < 0)
698 goto bad;
699 }
700 kfree(xattrs);
701 }
702 ci->i_xattrs.index_version = ci->i_xattrs.version;
703 ci->i_xattrs.dirty = false;
704
705 return err;
706bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800707 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700708bad:
709 if (xattrs) {
710 for (i = 0; i < numattr; i++)
711 kfree(xattrs[i]);
712 kfree(xattrs);
713 }
714 ci->i_xattrs.names_size = 0;
715 return err;
716}
717
718static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
719 int val_size)
720{
721 /*
722 * 4 bytes for the length, and additional 4 bytes per each xattr name,
723 * 4 bytes per each value
724 */
725 int size = 4 + ci->i_xattrs.count*(4 + 4) +
726 ci->i_xattrs.names_size +
727 ci->i_xattrs.vals_size;
728 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
729 ci->i_xattrs.count, ci->i_xattrs.names_size,
730 ci->i_xattrs.vals_size);
731
732 if (name_size)
733 size += 4 + 4 + name_size + val_size;
734
735 return size;
736}
737
738/*
739 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
740 * and swap into place.
741 */
742void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
743{
744 struct rb_node *p;
745 struct ceph_inode_xattr *xattr = NULL;
746 void *dest;
747
748 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
749 if (ci->i_xattrs.dirty) {
750 int need = __get_required_blob_size(ci, 0, 0);
751
752 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
753
754 p = rb_first(&ci->i_xattrs.index);
755 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
756
757 ceph_encode_32(&dest, ci->i_xattrs.count);
758 while (p) {
759 xattr = rb_entry(p, struct ceph_inode_xattr, node);
760
761 ceph_encode_32(&dest, xattr->name_len);
762 memcpy(dest, xattr->name, xattr->name_len);
763 dest += xattr->name_len;
764 ceph_encode_32(&dest, xattr->val_len);
765 memcpy(dest, xattr->val, xattr->val_len);
766 dest += xattr->val_len;
767
768 p = rb_next(p);
769 }
770
771 /* adjust buffer len; it may be larger than we need */
772 ci->i_xattrs.prealloc_blob->vec.iov_len =
773 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
774
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800775 if (ci->i_xattrs.blob)
776 ceph_buffer_put(ci->i_xattrs.blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700777 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
778 ci->i_xattrs.prealloc_blob = NULL;
779 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700780 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700781 }
782}
783
Yan, Zheng315f2402016-03-07 10:34:50 +0800784static inline int __get_request_mask(struct inode *in) {
785 struct ceph_mds_request *req = current->journal_info;
786 int mask = 0;
787 if (req && req->r_target_inode == in) {
788 if (req->r_op == CEPH_MDS_OP_LOOKUP ||
789 req->r_op == CEPH_MDS_OP_LOOKUPINO ||
790 req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
791 req->r_op == CEPH_MDS_OP_GETATTR) {
792 mask = le32_to_cpu(req->r_args.getattr.mask);
793 } else if (req->r_op == CEPH_MDS_OP_OPEN ||
794 req->r_op == CEPH_MDS_OP_CREATE) {
795 mask = le32_to_cpu(req->r_args.open.mask);
796 }
797 }
798 return mask;
799}
800
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800801ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
Sage Weil355da1e2009-10-06 11:31:08 -0700802 size_t size)
803{
Sage Weil355da1e2009-10-06 11:31:08 -0700804 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700805 struct ceph_inode_xattr *xattr;
Alex Elder881a5fa2012-01-23 15:49:28 -0600806 struct ceph_vxattr *vxattr = NULL;
Yan, Zheng315f2402016-03-07 10:34:50 +0800807 int req_mask;
808 int err;
Sage Weil355da1e2009-10-06 11:31:08 -0700809
Sage Weil0bee82f2013-01-20 22:00:58 -0800810 /* let's see if a virtual xattr was requested */
811 vxattr = ceph_match_vxattr(inode, name);
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800812 if (vxattr) {
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800813 int mask = 0;
814 if (vxattr->flags & VXATTR_FLAG_RSTAT)
815 mask |= CEPH_STAT_RSTAT;
816 err = ceph_do_getattr(inode, mask, true);
Yan, Zheng1684dd02017-06-14 15:54:56 +0800817 if (err)
818 return err;
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800819 err = -ENODATA;
820 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci)))
821 err = vxattr->getxattr_cb(ci, value, size);
majianpenga1dc1932013-06-19 14:58:10 +0800822 return err;
Sage Weil0bee82f2013-01-20 22:00:58 -0800823 }
824
Yan, Zheng315f2402016-03-07 10:34:50 +0800825 req_mask = __get_request_mask(inode);
826
majianpenga1dc1932013-06-19 14:58:10 +0800827 spin_lock(&ci->i_ceph_lock);
828 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
829 ci->i_xattrs.version, ci->i_xattrs.index_version);
830
Yan, Zheng508b32d2014-09-16 21:46:17 +0800831 if (ci->i_xattrs.version == 0 ||
Yan, Zheng315f2402016-03-07 10:34:50 +0800832 !((req_mask & CEPH_CAP_XATTR_SHARED) ||
833 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
Sage Weilbe655592011-11-30 09:47:09 -0800834 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng315f2402016-03-07 10:34:50 +0800835
836 /* security module gets xattr while filling trace */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200837 if (current->journal_info) {
Yan, Zheng315f2402016-03-07 10:34:50 +0800838 pr_warn_ratelimited("sync getxattr %p "
839 "during filling trace\n", inode);
840 return -EBUSY;
841 }
842
Sage Weil355da1e2009-10-06 11:31:08 -0700843 /* get xattrs from mds (if we don't already have them) */
Yan, Zheng508b32d2014-09-16 21:46:17 +0800844 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700845 if (err)
846 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800847 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700848 }
849
Sage Weil355da1e2009-10-06 11:31:08 -0700850 err = __build_xattrs(inode);
851 if (err < 0)
852 goto out;
853
Sage Weil355da1e2009-10-06 11:31:08 -0700854 err = -ENODATA; /* == ENOATTR */
855 xattr = __get_xattr(ci, name);
Sage Weil0bee82f2013-01-20 22:00:58 -0800856 if (!xattr)
Sage Weil355da1e2009-10-06 11:31:08 -0700857 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700858
859 err = -ERANGE;
860 if (size && size < xattr->val_len)
861 goto out;
862
863 err = xattr->val_len;
864 if (size == 0)
865 goto out;
866
867 memcpy(value, xattr->val, xattr->val_len);
868
Markus Elfringd37b1d92017-08-20 20:22:02 +0200869 if (current->journal_info &&
Yan, Zheng315f2402016-03-07 10:34:50 +0800870 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
871 ci->i_ceph_flags |= CEPH_I_SEC_INITED;
Sage Weil355da1e2009-10-06 11:31:08 -0700872out:
Sage Weilbe655592011-11-30 09:47:09 -0800873 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700874 return err;
875}
876
877ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
878{
David Howells2b0143b2015-03-17 22:25:59 +0000879 struct inode *inode = d_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700880 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder881a5fa2012-01-23 15:49:28 -0600881 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700882 u32 vir_namelen = 0;
883 u32 namelen;
884 int err;
885 u32 len;
886 int i;
887
Sage Weilbe655592011-11-30 09:47:09 -0800888 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700889 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
890 ci->i_xattrs.version, ci->i_xattrs.index_version);
891
Yan, Zheng508b32d2014-09-16 21:46:17 +0800892 if (ci->i_xattrs.version == 0 ||
893 !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
Sage Weilbe655592011-11-30 09:47:09 -0800894 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng508b32d2014-09-16 21:46:17 +0800895 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700896 if (err)
897 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800898 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700899 }
900
Sage Weil355da1e2009-10-06 11:31:08 -0700901 err = __build_xattrs(inode);
902 if (err < 0)
903 goto out;
Alex Elder3ce6cd12012-01-23 15:49:28 -0600904 /*
905 * Start with virtual dir xattr names (if any) (including
906 * terminating '\0' characters for each).
907 */
908 vir_namelen = ceph_vxattrs_name_size(vxattrs);
909
Sage Weil355da1e2009-10-06 11:31:08 -0700910 /* adding 1 byte per each variable due to the null termination */
Sage Weilb65917d2013-01-20 22:02:39 -0800911 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
Sage Weil355da1e2009-10-06 11:31:08 -0700912 err = -ERANGE;
Sage Weilb65917d2013-01-20 22:02:39 -0800913 if (size && vir_namelen + namelen > size)
Sage Weil355da1e2009-10-06 11:31:08 -0700914 goto out;
915
Sage Weilb65917d2013-01-20 22:02:39 -0800916 err = namelen + vir_namelen;
Sage Weil355da1e2009-10-06 11:31:08 -0700917 if (size == 0)
918 goto out;
919
920 names = __copy_xattr_names(ci, names);
921
922 /* virtual xattr names, too */
Sage Weilb65917d2013-01-20 22:02:39 -0800923 err = namelen;
924 if (vxattrs) {
Sage Weil355da1e2009-10-06 11:31:08 -0700925 for (i = 0; vxattrs[i].name; i++) {
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800926 if (!(vxattrs[i].flags & VXATTR_FLAG_HIDDEN) &&
Sage Weilb65917d2013-01-20 22:02:39 -0800927 !(vxattrs[i].exists_cb &&
928 !vxattrs[i].exists_cb(ci))) {
929 len = sprintf(names, "%s", vxattrs[i].name);
930 names += len + 1;
931 err += len + 1;
932 }
Sage Weil355da1e2009-10-06 11:31:08 -0700933 }
Sage Weilb65917d2013-01-20 22:02:39 -0800934 }
Sage Weil355da1e2009-10-06 11:31:08 -0700935
936out:
Sage Weilbe655592011-11-30 09:47:09 -0800937 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700938 return err;
939}
940
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200941static int ceph_sync_setxattr(struct inode *inode, const char *name,
Sage Weil355da1e2009-10-06 11:31:08 -0700942 const char *value, size_t size, int flags)
943{
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200944 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700945 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700946 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700947 struct ceph_mds_client *mdsc = fsc->mdsc;
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800948 struct ceph_pagelist *pagelist = NULL;
Yan, Zheng04303d82016-04-21 12:11:54 +0800949 int op = CEPH_MDS_OP_SETXATTR;
Sage Weil355da1e2009-10-06 11:31:08 -0700950 int err;
Sage Weil355da1e2009-10-06 11:31:08 -0700951
Yan, Zheng0aeff372014-12-17 21:26:47 +0800952 if (size > 0) {
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800953 /* copy value into pagelist */
954 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
955 if (!pagelist)
Sage Weil355da1e2009-10-06 11:31:08 -0700956 return -ENOMEM;
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800957
958 ceph_pagelist_init(pagelist);
959 err = ceph_pagelist_append(pagelist, value, size);
960 if (err)
961 goto out;
Yan, Zheng0aeff372014-12-17 21:26:47 +0800962 } else if (!value) {
Yan, Zheng04303d82016-04-21 12:11:54 +0800963 if (flags & CEPH_XATTR_REPLACE)
964 op = CEPH_MDS_OP_RMXATTR;
965 else
966 flags |= CEPH_XATTR_REMOVE;
Sage Weil355da1e2009-10-06 11:31:08 -0700967 }
968
969 dout("setxattr value=%.*s\n", (int)size, value);
970
971 /* do request */
Yan, Zheng04303d82016-04-21 12:11:54 +0800972 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +0100973 if (IS_ERR(req)) {
974 err = PTR_ERR(req);
975 goto out;
976 }
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400977
Sage Weil355da1e2009-10-06 11:31:08 -0700978 req->r_path2 = kstrdup(name, GFP_NOFS);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400979 if (!req->r_path2) {
980 ceph_mdsc_put_request(req);
981 err = -ENOMEM;
982 goto out;
983 }
Sage Weil355da1e2009-10-06 11:31:08 -0700984
Yan, Zheng04303d82016-04-21 12:11:54 +0800985 if (op == CEPH_MDS_OP_SETXATTR) {
986 req->r_args.setxattr.flags = cpu_to_le32(flags);
987 req->r_pagelist = pagelist;
988 pagelist = NULL;
989 }
Sage Weil355da1e2009-10-06 11:31:08 -0700990
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400991 req->r_inode = inode;
992 ihold(inode);
993 req->r_num_caps = 1;
994 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
995
Sage Weil355da1e2009-10-06 11:31:08 -0700996 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil752c8bd2013-02-05 13:52:29 -0800997 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil355da1e2009-10-06 11:31:08 -0700998 ceph_mdsc_put_request(req);
999 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
1000
1001out:
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001002 if (pagelist)
1003 ceph_pagelist_release(pagelist);
Sage Weil355da1e2009-10-06 11:31:08 -07001004 return err;
1005}
1006
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001007int __ceph_setxattr(struct inode *inode, const char *name,
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001008 const void *value, size_t size, int flags)
Sage Weil355da1e2009-10-06 11:31:08 -07001009{
Alex Elder881a5fa2012-01-23 15:49:28 -06001010 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -07001011 struct ceph_inode_info *ci = ceph_inode(inode);
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001012 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001013 struct ceph_cap_flush *prealloc_cf = NULL;
Alex Elder18fa8b32012-01-23 15:49:28 -06001014 int issued;
Sage Weil355da1e2009-10-06 11:31:08 -07001015 int err;
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001016 int dirty = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001017 int name_len = strlen(name);
1018 int val_len = size;
1019 char *newname = NULL;
1020 char *newval = NULL;
1021 struct ceph_inode_xattr *xattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -07001022 int required_blob_size;
Yan, Zhengf1919822018-04-08 09:54:39 +08001023 bool check_realm = false;
Yan, Zheng604d1b02015-05-01 17:49:16 +08001024 bool lock_snap_rwsem = false;
Sage Weil355da1e2009-10-06 11:31:08 -07001025
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001026 if (ceph_snap(inode) != CEPH_NOSNAP)
1027 return -EROFS;
Sage Weil355da1e2009-10-06 11:31:08 -07001028
Alex Elder06476a62012-01-23 15:49:27 -06001029 vxattr = ceph_match_vxattr(inode, name);
Yan, Zhengf1919822018-04-08 09:54:39 +08001030 if (vxattr) {
Yan, Zheng4e9906e2018-04-25 17:14:05 +08001031 if (vxattr->flags & VXATTR_FLAG_READONLY)
Yan, Zhengf1919822018-04-08 09:54:39 +08001032 return -EOPNOTSUPP;
1033 if (value && !strncmp(vxattr->name, "ceph.quota", 10))
1034 check_realm = true;
1035 }
Sage Weil355da1e2009-10-06 11:31:08 -07001036
Sage Weil3adf6542013-01-31 11:53:41 -08001037 /* pass any unhandled ceph.* xattrs through to the MDS */
1038 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1039 goto do_sync_unlocked;
1040
Sage Weil355da1e2009-10-06 11:31:08 -07001041 /* preallocate memory for xattr name, value, index node */
1042 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +02001043 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001044 if (!newname)
1045 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001046
1047 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -06001048 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001049 if (!newval)
1050 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001051 }
1052
1053 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1054 if (!xattr)
1055 goto out;
1056
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001057 prealloc_cf = ceph_alloc_cap_flush();
1058 if (!prealloc_cf)
1059 goto out;
1060
Sage Weilbe655592011-11-30 09:47:09 -08001061 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001062retry:
1063 issued = __ceph_caps_issued(ci, NULL);
Yan, Zheng508b32d2014-09-16 21:46:17 +08001064 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
Sage Weil355da1e2009-10-06 11:31:08 -07001065 goto do_sync;
Yan, Zheng604d1b02015-05-01 17:49:16 +08001066
1067 if (!lock_snap_rwsem && !ci->i_head_snapc) {
1068 lock_snap_rwsem = true;
1069 if (!down_read_trylock(&mdsc->snap_rwsem)) {
1070 spin_unlock(&ci->i_ceph_lock);
1071 down_read(&mdsc->snap_rwsem);
1072 spin_lock(&ci->i_ceph_lock);
1073 goto retry;
1074 }
1075 }
1076
1077 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
Sage Weil355da1e2009-10-06 11:31:08 -07001078 __build_xattrs(inode);
1079
1080 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1081
1082 if (!ci->i_xattrs.prealloc_blob ||
1083 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
Alex Elder18fa8b32012-01-23 15:49:28 -06001084 struct ceph_buffer *blob;
Sage Weil355da1e2009-10-06 11:31:08 -07001085
Sage Weilbe655592011-11-30 09:47:09 -08001086 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001087 dout(" preaallocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -08001088 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001089 if (!blob)
Yan, Zheng604d1b02015-05-01 17:49:16 +08001090 goto do_sync_unlocked;
Sage Weilbe655592011-11-30 09:47:09 -08001091 spin_lock(&ci->i_ceph_lock);
Sage Weilb6c1d5b2009-12-07 12:17:17 -08001092 if (ci->i_xattrs.prealloc_blob)
1093 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -07001094 ci->i_xattrs.prealloc_blob = blob;
1095 goto retry;
1096 }
1097
Yan, Zhengbcdfeb22014-02-11 13:04:19 +08001098 err = __set_xattr(ci, newname, name_len, newval, val_len,
1099 flags, value ? 1 : -1, &xattr);
Alex Elder18fa8b32012-01-23 15:49:28 -06001100
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001101 if (!err) {
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001102 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1103 &prealloc_cf);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001104 ci->i_xattrs.dirty = true;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001105 inode->i_ctime = current_time(inode);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001106 }
Alex Elder18fa8b32012-01-23 15:49:28 -06001107
Sage Weilbe655592011-11-30 09:47:09 -08001108 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng604d1b02015-05-01 17:49:16 +08001109 if (lock_snap_rwsem)
1110 up_read(&mdsc->snap_rwsem);
Sage Weilfca65b42011-05-04 11:33:47 -07001111 if (dirty)
1112 __mark_inode_dirty(inode, dirty);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001113 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001114 return err;
1115
1116do_sync:
Sage Weilbe655592011-11-30 09:47:09 -08001117 spin_unlock(&ci->i_ceph_lock);
Sage Weil3adf6542013-01-31 11:53:41 -08001118do_sync_unlocked:
Yan, Zheng604d1b02015-05-01 17:49:16 +08001119 if (lock_snap_rwsem)
1120 up_read(&mdsc->snap_rwsem);
Yan, Zheng315f2402016-03-07 10:34:50 +08001121
1122 /* security module set xattr while filling trace */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001123 if (current->journal_info) {
Yan, Zheng315f2402016-03-07 10:34:50 +08001124 pr_warn_ratelimited("sync setxattr %p "
1125 "during filling trace\n", inode);
1126 err = -EBUSY;
1127 } else {
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001128 err = ceph_sync_setxattr(inode, name, value, size, flags);
Yan, Zhengf1919822018-04-08 09:54:39 +08001129 if (err >= 0 && check_realm) {
1130 /* check if snaprealm was created for quota inode */
1131 spin_lock(&ci->i_ceph_lock);
1132 if ((ci->i_max_files || ci->i_max_bytes) &&
1133 !(ci->i_snap_realm &&
1134 ci->i_snap_realm->ino == ci->i_vino.ino))
1135 err = -EOPNOTSUPP;
1136 spin_unlock(&ci->i_ceph_lock);
1137 }
Yan, Zheng315f2402016-03-07 10:34:50 +08001138 }
Sage Weil355da1e2009-10-06 11:31:08 -07001139out:
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001140 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001141 kfree(newname);
1142 kfree(newval);
1143 kfree(xattr);
1144 return err;
1145}
1146
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001147static int ceph_get_xattr_handler(const struct xattr_handler *handler,
1148 struct dentry *dentry, struct inode *inode,
1149 const char *name, void *value, size_t size)
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001150{
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001151 if (!ceph_is_valid_xattr(name))
1152 return -EOPNOTSUPP;
1153 return __ceph_getxattr(inode, name, value, size);
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001154}
Yan, Zheng315f2402016-03-07 10:34:50 +08001155
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001156static int ceph_set_xattr_handler(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04001157 struct dentry *unused, struct inode *inode,
1158 const char *name, const void *value,
1159 size_t size, int flags)
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001160{
1161 if (!ceph_is_valid_xattr(name))
1162 return -EOPNOTSUPP;
Al Viro59301222016-05-27 10:19:30 -04001163 return __ceph_setxattr(inode, name, value, size, flags);
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001164}
1165
Wei Yongjun5130cce2016-10-17 15:11:29 +00001166static const struct xattr_handler ceph_other_xattr_handler = {
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001167 .prefix = "", /* match any name => handlers called with full name */
1168 .get = ceph_get_xattr_handler,
1169 .set = ceph_set_xattr_handler,
1170};
1171
Yan, Zheng315f2402016-03-07 10:34:50 +08001172#ifdef CONFIG_SECURITY
1173bool ceph_security_xattr_wanted(struct inode *in)
1174{
1175 return in->i_security != NULL;
1176}
1177
1178bool ceph_security_xattr_deadlock(struct inode *in)
1179{
1180 struct ceph_inode_info *ci;
1181 bool ret;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001182 if (!in->i_security)
Yan, Zheng315f2402016-03-07 10:34:50 +08001183 return false;
1184 ci = ceph_inode(in);
1185 spin_lock(&ci->i_ceph_lock);
1186 ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1187 !(ci->i_xattrs.version > 0 &&
1188 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1189 spin_unlock(&ci->i_ceph_lock);
1190 return ret;
1191}
1192#endif