1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3 #include <linux/ceph/pagelist.h>
4
5 #include "super.h"
6 #include "mds_client.h"
7
8 #include <linux/ceph/decode.h>
9
10 #include <linux/xattr.h>
11 #include <linux/posix_acl_xattr.h>
12 #include <linux/slab.h>
13
14 #define XATTR_CEPH_PREFIX "ceph."
15 #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
16
17 static int __remove_xattr(struct ceph_inode_info *ci,
18 struct ceph_inode_xattr *xattr);
19
20 static const struct xattr_handler ceph_other_xattr_handler;
21
22 /*
23 * List of handlers for synthetic system.* attributes. Other
24 * attributes are handled directly.
25 */
26 const struct xattr_handler *ceph_xattr_handlers[] = {
27 #ifdef CONFIG_CEPH_FS_POSIX_ACL
28 &posix_acl_access_xattr_handler,
29 &posix_acl_default_xattr_handler,
30 #endif
31 &ceph_other_xattr_handler,
32 NULL,
33 };
34
ceph_is_valid_xattr(const char * name)35 static bool ceph_is_valid_xattr(const char *name)
36 {
37 return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
38 !strncmp(name, XATTR_SECURITY_PREFIX,
39 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 */
48 struct ceph_vxattr {
49 char *name;
50 size_t name_size; /* strlen(name) + 1 (for '\0') */
51 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
52 size_t size);
53 bool (*exists_cb)(struct ceph_inode_info *ci);
54 unsigned int flags;
55 };
56
57 #define VXATTR_FLAG_READONLY (1<<0)
58 #define VXATTR_FLAG_HIDDEN (1<<1)
59 #define VXATTR_FLAG_RSTAT (1<<2)
60
61 /* layouts */
62
ceph_vxattrcb_layout_exists(struct ceph_inode_info * ci)63 static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
64 {
65 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);
69 }
70
ceph_vxattrcb_layout(struct ceph_inode_info * ci,char * val,size_t size)71 static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
72 size_t size)
73 {
74 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
75 struct ceph_osd_client *osdc = &fsc->client->osdc;
76 struct ceph_string *pool_ns;
77 s64 pool = ci->i_layout.pool_id;
78 const char *pool_name;
79 const char *ns_field = " pool_namespace=";
80 char buf[128];
81 size_t len, total_len = 0;
82 ssize_t ret;
83
84 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
85
86 dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
87 down_read(&osdc->lock);
88 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
89 if (pool_name) {
90 len = snprintf(buf, sizeof(buf),
91 "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);
94 total_len = len + strlen(pool_name);
95 } else {
96 len = snprintf(buf, sizeof(buf),
97 "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);
100 total_len = len;
101 }
102
103 if (pool_ns)
104 total_len += strlen(ns_field) + pool_ns->len;
105
106 ret = total_len;
107 if (size >= total_len) {
108 memcpy(val, buf, len);
109 ret = len;
110 if (pool_name) {
111 len = strlen(pool_name);
112 memcpy(val + ret, pool_name, len);
113 ret += len;
114 }
115 if (pool_ns) {
116 len = strlen(ns_field);
117 memcpy(val + ret, ns_field, len);
118 ret += len;
119 memcpy(val + ret, pool_ns->str, pool_ns->len);
120 ret += pool_ns->len;
121 }
122 }
123 up_read(&osdc->lock);
124 ceph_put_string(pool_ns);
125 return ret;
126 }
127
ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info * ci,char * val,size_t size)128 static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
129 char *val, size_t size)
130 {
131 return snprintf(val, size, "%u", ci->i_layout.stripe_unit);
132 }
133
ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info * ci,char * val,size_t size)134 static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
135 char *val, size_t size)
136 {
137 return snprintf(val, size, "%u", ci->i_layout.stripe_count);
138 }
139
ceph_vxattrcb_layout_object_size(struct ceph_inode_info * ci,char * val,size_t size)140 static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
141 char *val, size_t size)
142 {
143 return snprintf(val, size, "%u", ci->i_layout.object_size);
144 }
145
ceph_vxattrcb_layout_pool(struct ceph_inode_info * ci,char * val,size_t size)146 static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
147 char *val, size_t size)
148 {
149 int ret;
150 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
151 struct ceph_osd_client *osdc = &fsc->client->osdc;
152 s64 pool = ci->i_layout.pool_id;
153 const char *pool_name;
154
155 down_read(&osdc->lock);
156 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
157 if (pool_name)
158 ret = snprintf(val, size, "%s", pool_name);
159 else
160 ret = snprintf(val, size, "%lld", (unsigned long long)pool);
161 up_read(&osdc->lock);
162 return ret;
163 }
164
ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info * ci,char * val,size_t size)165 static size_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
166 char *val, size_t size)
167 {
168 int ret = 0;
169 struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
170 if (ns) {
171 ret = snprintf(val, size, "%.*s", (int)ns->len, ns->str);
172 ceph_put_string(ns);
173 }
174 return ret;
175 }
176
177 /* directories */
178
ceph_vxattrcb_dir_entries(struct ceph_inode_info * ci,char * val,size_t size)179 static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
180 size_t size)
181 {
182 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
183 }
184
ceph_vxattrcb_dir_files(struct ceph_inode_info * ci,char * val,size_t size)185 static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
186 size_t size)
187 {
188 return snprintf(val, size, "%lld", ci->i_files);
189 }
190
ceph_vxattrcb_dir_subdirs(struct ceph_inode_info * ci,char * val,size_t size)191 static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
192 size_t size)
193 {
194 return snprintf(val, size, "%lld", ci->i_subdirs);
195 }
196
ceph_vxattrcb_dir_rentries(struct ceph_inode_info * ci,char * val,size_t size)197 static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
198 size_t size)
199 {
200 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
201 }
202
ceph_vxattrcb_dir_rfiles(struct ceph_inode_info * ci,char * val,size_t size)203 static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
204 size_t size)
205 {
206 return snprintf(val, size, "%lld", ci->i_rfiles);
207 }
208
ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info * ci,char * val,size_t size)209 static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
210 size_t size)
211 {
212 return snprintf(val, size, "%lld", ci->i_rsubdirs);
213 }
214
ceph_vxattrcb_dir_rbytes(struct ceph_inode_info * ci,char * val,size_t size)215 static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
216 size_t size)
217 {
218 return snprintf(val, size, "%lld", ci->i_rbytes);
219 }
220
ceph_vxattrcb_dir_rctime(struct ceph_inode_info * ci,char * val,size_t size)221 static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
222 size_t size)
223 {
224 return snprintf(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
225 ci->i_rctime.tv_nsec);
226 }
227
228 /* quotas */
229
ceph_vxattrcb_quota_exists(struct ceph_inode_info * ci)230 static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
231 {
232 bool ret = false;
233 spin_lock(&ci->i_ceph_lock);
234 if ((ci->i_max_files || ci->i_max_bytes) &&
235 ci->i_vino.snap == CEPH_NOSNAP &&
236 ci->i_snap_realm &&
237 ci->i_snap_realm->ino == ci->i_vino.ino)
238 ret = true;
239 spin_unlock(&ci->i_ceph_lock);
240 return ret;
241 }
242
ceph_vxattrcb_quota(struct ceph_inode_info * ci,char * val,size_t size)243 static size_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
244 size_t size)
245 {
246 return snprintf(val, size, "max_bytes=%llu max_files=%llu",
247 ci->i_max_bytes, ci->i_max_files);
248 }
249
ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info * ci,char * val,size_t size)250 static size_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
251 char *val, size_t size)
252 {
253 return snprintf(val, size, "%llu", ci->i_max_bytes);
254 }
255
ceph_vxattrcb_quota_max_files(struct ceph_inode_info * ci,char * val,size_t size)256 static size_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
257 char *val, size_t size)
258 {
259 return snprintf(val, size, "%llu", ci->i_max_files);
260 }
261
262 #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
263 #define CEPH_XATTR_NAME2(_type, _name, _name2) \
264 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
265
266 #define XATTR_NAME_CEPH(_type, _name, _flags) \
267 { \
268 .name = CEPH_XATTR_NAME(_type, _name), \
269 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
270 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
271 .exists_cb = NULL, \
272 .flags = (VXATTR_FLAG_READONLY | _flags), \
273 }
274 #define XATTR_RSTAT_FIELD(_type, _name) \
275 XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
276 #define XATTR_RSTAT_FIELD_UPDATABLE(_type, _name) \
277 { \
278 .name = CEPH_XATTR_NAME(_type, _name), \
279 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
280 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
281 .exists_cb = NULL, \
282 .flags = VXATTR_FLAG_RSTAT, \
283 }
284 #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
285 { \
286 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
287 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
288 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
289 .exists_cb = ceph_vxattrcb_layout_exists, \
290 .flags = VXATTR_FLAG_HIDDEN, \
291 }
292 #define XATTR_QUOTA_FIELD(_type, _name) \
293 { \
294 .name = CEPH_XATTR_NAME(_type, _name), \
295 .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
296 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
297 .exists_cb = ceph_vxattrcb_quota_exists, \
298 .flags = VXATTR_FLAG_HIDDEN, \
299 }
300
301 static struct ceph_vxattr ceph_dir_vxattrs[] = {
302 {
303 .name = "ceph.dir.layout",
304 .name_size = sizeof("ceph.dir.layout"),
305 .getxattr_cb = ceph_vxattrcb_layout,
306 .exists_cb = ceph_vxattrcb_layout_exists,
307 .flags = VXATTR_FLAG_HIDDEN,
308 },
309 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
310 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
311 XATTR_LAYOUT_FIELD(dir, layout, object_size),
312 XATTR_LAYOUT_FIELD(dir, layout, pool),
313 XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
314 XATTR_NAME_CEPH(dir, entries, 0),
315 XATTR_NAME_CEPH(dir, files, 0),
316 XATTR_NAME_CEPH(dir, subdirs, 0),
317 XATTR_RSTAT_FIELD(dir, rentries),
318 XATTR_RSTAT_FIELD(dir, rfiles),
319 XATTR_RSTAT_FIELD(dir, rsubdirs),
320 XATTR_RSTAT_FIELD(dir, rbytes),
321 XATTR_RSTAT_FIELD_UPDATABLE(dir, rctime),
322 {
323 .name = "ceph.quota",
324 .name_size = sizeof("ceph.quota"),
325 .getxattr_cb = ceph_vxattrcb_quota,
326 .exists_cb = ceph_vxattrcb_quota_exists,
327 .flags = VXATTR_FLAG_HIDDEN,
328 },
329 XATTR_QUOTA_FIELD(quota, max_bytes),
330 XATTR_QUOTA_FIELD(quota, max_files),
331 { .name = NULL, 0 } /* Required table terminator */
332 };
333 static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
334
335 /* files */
336
337 static struct ceph_vxattr ceph_file_vxattrs[] = {
338 {
339 .name = "ceph.file.layout",
340 .name_size = sizeof("ceph.file.layout"),
341 .getxattr_cb = ceph_vxattrcb_layout,
342 .exists_cb = ceph_vxattrcb_layout_exists,
343 .flags = VXATTR_FLAG_HIDDEN,
344 },
345 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
346 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
347 XATTR_LAYOUT_FIELD(file, layout, object_size),
348 XATTR_LAYOUT_FIELD(file, layout, pool),
349 XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
350 { .name = NULL, 0 } /* Required table terminator */
351 };
352 static size_t ceph_file_vxattrs_name_size; /* total size of all names */
353
ceph_inode_vxattrs(struct inode * inode)354 static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
355 {
356 if (S_ISDIR(inode->i_mode))
357 return ceph_dir_vxattrs;
358 else if (S_ISREG(inode->i_mode))
359 return ceph_file_vxattrs;
360 return NULL;
361 }
362
ceph_vxattrs_name_size(struct ceph_vxattr * vxattrs)363 static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
364 {
365 if (vxattrs == ceph_dir_vxattrs)
366 return ceph_dir_vxattrs_name_size;
367 if (vxattrs == ceph_file_vxattrs)
368 return ceph_file_vxattrs_name_size;
369 BUG_ON(vxattrs);
370 return 0;
371 }
372
373 /*
374 * Compute the aggregate size (including terminating '\0') of all
375 * virtual extended attribute names in the given vxattr table.
376 */
vxattrs_name_size(struct ceph_vxattr * vxattrs)377 static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
378 {
379 struct ceph_vxattr *vxattr;
380 size_t size = 0;
381
382 for (vxattr = vxattrs; vxattr->name; vxattr++) {
383 if (!(vxattr->flags & VXATTR_FLAG_HIDDEN))
384 size += vxattr->name_size;
385 }
386
387 return size;
388 }
389
390 /* Routines called at initialization and exit time */
391
ceph_xattr_init(void)392 void __init ceph_xattr_init(void)
393 {
394 ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
395 ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
396 }
397
ceph_xattr_exit(void)398 void ceph_xattr_exit(void)
399 {
400 ceph_dir_vxattrs_name_size = 0;
401 ceph_file_vxattrs_name_size = 0;
402 }
403
ceph_match_vxattr(struct inode * inode,const char * name)404 static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
405 const char *name)
406 {
407 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
408
409 if (vxattr) {
410 while (vxattr->name) {
411 if (!strcmp(vxattr->name, name))
412 return vxattr;
413 vxattr++;
414 }
415 }
416
417 return NULL;
418 }
419
__set_xattr(struct ceph_inode_info * ci,const char * name,int name_len,const char * val,int val_len,int flags,int update_xattr,struct ceph_inode_xattr ** newxattr)420 static int __set_xattr(struct ceph_inode_info *ci,
421 const char *name, int name_len,
422 const char *val, int val_len,
423 int flags, int update_xattr,
424 struct ceph_inode_xattr **newxattr)
425 {
426 struct rb_node **p;
427 struct rb_node *parent = NULL;
428 struct ceph_inode_xattr *xattr = NULL;
429 int c;
430 int new = 0;
431
432 p = &ci->i_xattrs.index.rb_node;
433 while (*p) {
434 parent = *p;
435 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
436 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
437 if (c < 0)
438 p = &(*p)->rb_left;
439 else if (c > 0)
440 p = &(*p)->rb_right;
441 else {
442 if (name_len == xattr->name_len)
443 break;
444 else if (name_len < xattr->name_len)
445 p = &(*p)->rb_left;
446 else
447 p = &(*p)->rb_right;
448 }
449 xattr = NULL;
450 }
451
452 if (update_xattr) {
453 int err = 0;
454
455 if (xattr && (flags & XATTR_CREATE))
456 err = -EEXIST;
457 else if (!xattr && (flags & XATTR_REPLACE))
458 err = -ENODATA;
459 if (err) {
460 kfree(name);
461 kfree(val);
462 kfree(*newxattr);
463 return err;
464 }
465 if (update_xattr < 0) {
466 if (xattr)
467 __remove_xattr(ci, xattr);
468 kfree(name);
469 kfree(*newxattr);
470 return 0;
471 }
472 }
473
474 if (!xattr) {
475 new = 1;
476 xattr = *newxattr;
477 xattr->name = name;
478 xattr->name_len = name_len;
479 xattr->should_free_name = update_xattr;
480
481 ci->i_xattrs.count++;
482 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
483 } else {
484 kfree(*newxattr);
485 *newxattr = NULL;
486 if (xattr->should_free_val)
487 kfree((void *)xattr->val);
488
489 if (update_xattr) {
490 kfree((void *)name);
491 name = xattr->name;
492 }
493 ci->i_xattrs.names_size -= xattr->name_len;
494 ci->i_xattrs.vals_size -= xattr->val_len;
495 }
496 ci->i_xattrs.names_size += name_len;
497 ci->i_xattrs.vals_size += val_len;
498 if (val)
499 xattr->val = val;
500 else
501 xattr->val = "";
502
503 xattr->val_len = val_len;
504 xattr->dirty = update_xattr;
505 xattr->should_free_val = (val && update_xattr);
506
507 if (new) {
508 rb_link_node(&xattr->node, parent, p);
509 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
510 dout("__set_xattr_val p=%p\n", p);
511 }
512
513 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
514 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
515
516 return 0;
517 }
518
__get_xattr(struct ceph_inode_info * ci,const char * name)519 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
520 const char *name)
521 {
522 struct rb_node **p;
523 struct rb_node *parent = NULL;
524 struct ceph_inode_xattr *xattr = NULL;
525 int name_len = strlen(name);
526 int c;
527
528 p = &ci->i_xattrs.index.rb_node;
529 while (*p) {
530 parent = *p;
531 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
532 c = strncmp(name, xattr->name, xattr->name_len);
533 if (c == 0 && name_len > xattr->name_len)
534 c = 1;
535 if (c < 0)
536 p = &(*p)->rb_left;
537 else if (c > 0)
538 p = &(*p)->rb_right;
539 else {
540 dout("__get_xattr %s: found %.*s\n", name,
541 xattr->val_len, xattr->val);
542 return xattr;
543 }
544 }
545
546 dout("__get_xattr %s: not found\n", name);
547
548 return NULL;
549 }
550
__free_xattr(struct ceph_inode_xattr * xattr)551 static void __free_xattr(struct ceph_inode_xattr *xattr)
552 {
553 BUG_ON(!xattr);
554
555 if (xattr->should_free_name)
556 kfree((void *)xattr->name);
557 if (xattr->should_free_val)
558 kfree((void *)xattr->val);
559
560 kfree(xattr);
561 }
562
__remove_xattr(struct ceph_inode_info * ci,struct ceph_inode_xattr * xattr)563 static int __remove_xattr(struct ceph_inode_info *ci,
564 struct ceph_inode_xattr *xattr)
565 {
566 if (!xattr)
567 return -ENODATA;
568
569 rb_erase(&xattr->node, &ci->i_xattrs.index);
570
571 if (xattr->should_free_name)
572 kfree((void *)xattr->name);
573 if (xattr->should_free_val)
574 kfree((void *)xattr->val);
575
576 ci->i_xattrs.names_size -= xattr->name_len;
577 ci->i_xattrs.vals_size -= xattr->val_len;
578 ci->i_xattrs.count--;
579 kfree(xattr);
580
581 return 0;
582 }
583
__copy_xattr_names(struct ceph_inode_info * ci,char * dest)584 static char *__copy_xattr_names(struct ceph_inode_info *ci,
585 char *dest)
586 {
587 struct rb_node *p;
588 struct ceph_inode_xattr *xattr = NULL;
589
590 p = rb_first(&ci->i_xattrs.index);
591 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
592
593 while (p) {
594 xattr = rb_entry(p, struct ceph_inode_xattr, node);
595 memcpy(dest, xattr->name, xattr->name_len);
596 dest[xattr->name_len] = '\0';
597
598 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
599 xattr->name_len, ci->i_xattrs.names_size);
600
601 dest += xattr->name_len + 1;
602 p = rb_next(p);
603 }
604
605 return dest;
606 }
607
__ceph_destroy_xattrs(struct ceph_inode_info * ci)608 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
609 {
610 struct rb_node *p, *tmp;
611 struct ceph_inode_xattr *xattr = NULL;
612
613 p = rb_first(&ci->i_xattrs.index);
614
615 dout("__ceph_destroy_xattrs p=%p\n", p);
616
617 while (p) {
618 xattr = rb_entry(p, struct ceph_inode_xattr, node);
619 tmp = p;
620 p = rb_next(tmp);
621 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
622 xattr->name_len, xattr->name);
623 rb_erase(tmp, &ci->i_xattrs.index);
624
625 __free_xattr(xattr);
626 }
627
628 ci->i_xattrs.names_size = 0;
629 ci->i_xattrs.vals_size = 0;
630 ci->i_xattrs.index_version = 0;
631 ci->i_xattrs.count = 0;
632 ci->i_xattrs.index = RB_ROOT;
633 }
634
__build_xattrs(struct inode * inode)635 static int __build_xattrs(struct inode *inode)
636 __releases(ci->i_ceph_lock)
637 __acquires(ci->i_ceph_lock)
638 {
639 u32 namelen;
640 u32 numattr = 0;
641 void *p, *end;
642 u32 len;
643 const char *name, *val;
644 struct ceph_inode_info *ci = ceph_inode(inode);
645 int xattr_version;
646 struct ceph_inode_xattr **xattrs = NULL;
647 int err = 0;
648 int i;
649
650 dout("__build_xattrs() len=%d\n",
651 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
652
653 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
654 return 0; /* already built */
655
656 __ceph_destroy_xattrs(ci);
657
658 start:
659 /* updated internal xattr rb tree */
660 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
661 p = ci->i_xattrs.blob->vec.iov_base;
662 end = p + ci->i_xattrs.blob->vec.iov_len;
663 ceph_decode_32_safe(&p, end, numattr, bad);
664 xattr_version = ci->i_xattrs.version;
665 spin_unlock(&ci->i_ceph_lock);
666
667 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
668 GFP_NOFS);
669 err = -ENOMEM;
670 if (!xattrs)
671 goto bad_lock;
672
673 for (i = 0; i < numattr; i++) {
674 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
675 GFP_NOFS);
676 if (!xattrs[i])
677 goto bad_lock;
678 }
679
680 spin_lock(&ci->i_ceph_lock);
681 if (ci->i_xattrs.version != xattr_version) {
682 /* lost a race, retry */
683 for (i = 0; i < numattr; i++)
684 kfree(xattrs[i]);
685 kfree(xattrs);
686 xattrs = NULL;
687 goto start;
688 }
689 err = -EIO;
690 while (numattr--) {
691 ceph_decode_32_safe(&p, end, len, bad);
692 namelen = len;
693 name = p;
694 p += len;
695 ceph_decode_32_safe(&p, end, len, bad);
696 val = p;
697 p += len;
698
699 err = __set_xattr(ci, name, namelen, val, len,
700 0, 0, &xattrs[numattr]);
701
702 if (err < 0)
703 goto bad;
704 }
705 kfree(xattrs);
706 }
707 ci->i_xattrs.index_version = ci->i_xattrs.version;
708 ci->i_xattrs.dirty = false;
709
710 return err;
711 bad_lock:
712 spin_lock(&ci->i_ceph_lock);
713 bad:
714 if (xattrs) {
715 for (i = 0; i < numattr; i++)
716 kfree(xattrs[i]);
717 kfree(xattrs);
718 }
719 ci->i_xattrs.names_size = 0;
720 return err;
721 }
722
__get_required_blob_size(struct ceph_inode_info * ci,int name_size,int val_size)723 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
724 int val_size)
725 {
726 /*
727 * 4 bytes for the length, and additional 4 bytes per each xattr name,
728 * 4 bytes per each value
729 */
730 int size = 4 + ci->i_xattrs.count*(4 + 4) +
731 ci->i_xattrs.names_size +
732 ci->i_xattrs.vals_size;
733 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
734 ci->i_xattrs.count, ci->i_xattrs.names_size,
735 ci->i_xattrs.vals_size);
736
737 if (name_size)
738 size += 4 + 4 + name_size + val_size;
739
740 return size;
741 }
742
743 /*
744 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
745 * and swap into place. It returns the old i_xattrs.blob (or NULL) so
746 * that it can be freed by the caller as the i_ceph_lock is likely to be
747 * held.
748 */
__ceph_build_xattrs_blob(struct ceph_inode_info * ci)749 struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
750 {
751 struct rb_node *p;
752 struct ceph_inode_xattr *xattr = NULL;
753 struct ceph_buffer *old_blob = NULL;
754 void *dest;
755
756 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
757 if (ci->i_xattrs.dirty) {
758 int need = __get_required_blob_size(ci, 0, 0);
759
760 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
761
762 p = rb_first(&ci->i_xattrs.index);
763 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
764
765 ceph_encode_32(&dest, ci->i_xattrs.count);
766 while (p) {
767 xattr = rb_entry(p, struct ceph_inode_xattr, node);
768
769 ceph_encode_32(&dest, xattr->name_len);
770 memcpy(dest, xattr->name, xattr->name_len);
771 dest += xattr->name_len;
772 ceph_encode_32(&dest, xattr->val_len);
773 memcpy(dest, xattr->val, xattr->val_len);
774 dest += xattr->val_len;
775
776 p = rb_next(p);
777 }
778
779 /* adjust buffer len; it may be larger than we need */
780 ci->i_xattrs.prealloc_blob->vec.iov_len =
781 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
782
783 if (ci->i_xattrs.blob)
784 old_blob = ci->i_xattrs.blob;
785 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
786 ci->i_xattrs.prealloc_blob = NULL;
787 ci->i_xattrs.dirty = false;
788 ci->i_xattrs.version++;
789 }
790
791 return old_blob;
792 }
793
__get_request_mask(struct inode * in)794 static inline int __get_request_mask(struct inode *in) {
795 struct ceph_mds_request *req = current->journal_info;
796 int mask = 0;
797 if (req && req->r_target_inode == in) {
798 if (req->r_op == CEPH_MDS_OP_LOOKUP ||
799 req->r_op == CEPH_MDS_OP_LOOKUPINO ||
800 req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
801 req->r_op == CEPH_MDS_OP_GETATTR) {
802 mask = le32_to_cpu(req->r_args.getattr.mask);
803 } else if (req->r_op == CEPH_MDS_OP_OPEN ||
804 req->r_op == CEPH_MDS_OP_CREATE) {
805 mask = le32_to_cpu(req->r_args.open.mask);
806 }
807 }
808 return mask;
809 }
810
__ceph_getxattr(struct inode * inode,const char * name,void * value,size_t size)811 ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
812 size_t size)
813 {
814 struct ceph_inode_info *ci = ceph_inode(inode);
815 struct ceph_inode_xattr *xattr;
816 struct ceph_vxattr *vxattr = NULL;
817 int req_mask;
818 int err;
819
820 /* let's see if a virtual xattr was requested */
821 vxattr = ceph_match_vxattr(inode, name);
822 if (vxattr) {
823 int mask = 0;
824 if (vxattr->flags & VXATTR_FLAG_RSTAT)
825 mask |= CEPH_STAT_RSTAT;
826 err = ceph_do_getattr(inode, mask, true);
827 if (err)
828 return err;
829 err = -ENODATA;
830 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
831 err = vxattr->getxattr_cb(ci, value, size);
832 if (size && size < err)
833 err = -ERANGE;
834 }
835 return err;
836 }
837
838 req_mask = __get_request_mask(inode);
839
840 spin_lock(&ci->i_ceph_lock);
841 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
842 ci->i_xattrs.version, ci->i_xattrs.index_version);
843
844 if (ci->i_xattrs.version == 0 ||
845 !((req_mask & CEPH_CAP_XATTR_SHARED) ||
846 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
847 spin_unlock(&ci->i_ceph_lock);
848
849 /* security module gets xattr while filling trace */
850 if (current->journal_info) {
851 pr_warn_ratelimited("sync getxattr %p "
852 "during filling trace\n", inode);
853 return -EBUSY;
854 }
855
856 /* get xattrs from mds (if we don't already have them) */
857 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
858 if (err)
859 return err;
860 spin_lock(&ci->i_ceph_lock);
861 }
862
863 err = __build_xattrs(inode);
864 if (err < 0)
865 goto out;
866
867 err = -ENODATA; /* == ENOATTR */
868 xattr = __get_xattr(ci, name);
869 if (!xattr)
870 goto out;
871
872 err = -ERANGE;
873 if (size && size < xattr->val_len)
874 goto out;
875
876 err = xattr->val_len;
877 if (size == 0)
878 goto out;
879
880 memcpy(value, xattr->val, xattr->val_len);
881
882 if (current->journal_info &&
883 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
884 ci->i_ceph_flags |= CEPH_I_SEC_INITED;
885 out:
886 spin_unlock(&ci->i_ceph_lock);
887 return err;
888 }
889
ceph_listxattr(struct dentry * dentry,char * names,size_t size)890 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
891 {
892 struct inode *inode = d_inode(dentry);
893 struct ceph_inode_info *ci = ceph_inode(inode);
894 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
895 u32 vir_namelen = 0;
896 u32 namelen;
897 int err;
898 u32 len;
899 int i;
900
901 spin_lock(&ci->i_ceph_lock);
902 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
903 ci->i_xattrs.version, ci->i_xattrs.index_version);
904
905 if (ci->i_xattrs.version == 0 ||
906 !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
907 spin_unlock(&ci->i_ceph_lock);
908 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
909 if (err)
910 return err;
911 spin_lock(&ci->i_ceph_lock);
912 }
913
914 err = __build_xattrs(inode);
915 if (err < 0)
916 goto out;
917 /*
918 * Start with virtual dir xattr names (if any) (including
919 * terminating '\0' characters for each).
920 */
921 vir_namelen = ceph_vxattrs_name_size(vxattrs);
922
923 /* adding 1 byte per each variable due to the null termination */
924 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
925 err = -ERANGE;
926 if (size && vir_namelen + namelen > size)
927 goto out;
928
929 err = namelen + vir_namelen;
930 if (size == 0)
931 goto out;
932
933 names = __copy_xattr_names(ci, names);
934
935 /* virtual xattr names, too */
936 err = namelen;
937 if (vxattrs) {
938 for (i = 0; vxattrs[i].name; i++) {
939 if (!(vxattrs[i].flags & VXATTR_FLAG_HIDDEN) &&
940 !(vxattrs[i].exists_cb &&
941 !vxattrs[i].exists_cb(ci))) {
942 len = sprintf(names, "%s", vxattrs[i].name);
943 names += len + 1;
944 err += len + 1;
945 }
946 }
947 }
948
949 out:
950 spin_unlock(&ci->i_ceph_lock);
951 return err;
952 }
953
ceph_sync_setxattr(struct inode * inode,const char * name,const char * value,size_t size,int flags)954 static int ceph_sync_setxattr(struct inode *inode, const char *name,
955 const char *value, size_t size, int flags)
956 {
957 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
958 struct ceph_inode_info *ci = ceph_inode(inode);
959 struct ceph_mds_request *req;
960 struct ceph_mds_client *mdsc = fsc->mdsc;
961 struct ceph_pagelist *pagelist = NULL;
962 int op = CEPH_MDS_OP_SETXATTR;
963 int err;
964
965 if (size > 0) {
966 /* copy value into pagelist */
967 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
968 if (!pagelist)
969 return -ENOMEM;
970
971 ceph_pagelist_init(pagelist);
972 err = ceph_pagelist_append(pagelist, value, size);
973 if (err)
974 goto out;
975 } else if (!value) {
976 if (flags & CEPH_XATTR_REPLACE)
977 op = CEPH_MDS_OP_RMXATTR;
978 else
979 flags |= CEPH_XATTR_REMOVE;
980 }
981
982 dout("setxattr value=%.*s\n", (int)size, value);
983
984 /* do request */
985 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
986 if (IS_ERR(req)) {
987 err = PTR_ERR(req);
988 goto out;
989 }
990
991 req->r_path2 = kstrdup(name, GFP_NOFS);
992 if (!req->r_path2) {
993 ceph_mdsc_put_request(req);
994 err = -ENOMEM;
995 goto out;
996 }
997
998 if (op == CEPH_MDS_OP_SETXATTR) {
999 req->r_args.setxattr.flags = cpu_to_le32(flags);
1000 req->r_pagelist = pagelist;
1001 pagelist = NULL;
1002 }
1003
1004 req->r_inode = inode;
1005 ihold(inode);
1006 req->r_num_caps = 1;
1007 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1008
1009 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
1010 err = ceph_mdsc_do_request(mdsc, NULL, req);
1011 ceph_mdsc_put_request(req);
1012 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
1013
1014 out:
1015 if (pagelist)
1016 ceph_pagelist_release(pagelist);
1017 return err;
1018 }
1019
__ceph_setxattr(struct inode * inode,const char * name,const void * value,size_t size,int flags)1020 int __ceph_setxattr(struct inode *inode, const char *name,
1021 const void *value, size_t size, int flags)
1022 {
1023 struct ceph_vxattr *vxattr;
1024 struct ceph_inode_info *ci = ceph_inode(inode);
1025 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1026 struct ceph_cap_flush *prealloc_cf = NULL;
1027 struct ceph_buffer *old_blob = NULL;
1028 int issued;
1029 int err;
1030 int dirty = 0;
1031 int name_len = strlen(name);
1032 int val_len = size;
1033 char *newname = NULL;
1034 char *newval = NULL;
1035 struct ceph_inode_xattr *xattr = NULL;
1036 int required_blob_size;
1037 bool check_realm = false;
1038 bool lock_snap_rwsem = false;
1039
1040 if (ceph_snap(inode) != CEPH_NOSNAP)
1041 return -EROFS;
1042
1043 vxattr = ceph_match_vxattr(inode, name);
1044 if (vxattr) {
1045 if (vxattr->flags & VXATTR_FLAG_READONLY)
1046 return -EOPNOTSUPP;
1047 if (value && !strncmp(vxattr->name, "ceph.quota", 10))
1048 check_realm = true;
1049 }
1050
1051 /* pass any unhandled ceph.* xattrs through to the MDS */
1052 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1053 goto do_sync_unlocked;
1054
1055 /* preallocate memory for xattr name, value, index node */
1056 err = -ENOMEM;
1057 newname = kmemdup(name, name_len + 1, GFP_NOFS);
1058 if (!newname)
1059 goto out;
1060
1061 if (val_len) {
1062 newval = kmemdup(value, val_len, GFP_NOFS);
1063 if (!newval)
1064 goto out;
1065 }
1066
1067 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1068 if (!xattr)
1069 goto out;
1070
1071 prealloc_cf = ceph_alloc_cap_flush();
1072 if (!prealloc_cf)
1073 goto out;
1074
1075 spin_lock(&ci->i_ceph_lock);
1076 retry:
1077 issued = __ceph_caps_issued(ci, NULL);
1078 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
1079 goto do_sync;
1080
1081 if (!lock_snap_rwsem && !ci->i_head_snapc) {
1082 lock_snap_rwsem = true;
1083 if (!down_read_trylock(&mdsc->snap_rwsem)) {
1084 spin_unlock(&ci->i_ceph_lock);
1085 down_read(&mdsc->snap_rwsem);
1086 spin_lock(&ci->i_ceph_lock);
1087 goto retry;
1088 }
1089 }
1090
1091 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
1092 __build_xattrs(inode);
1093
1094 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1095
1096 if (!ci->i_xattrs.prealloc_blob ||
1097 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
1098 struct ceph_buffer *blob;
1099
1100 spin_unlock(&ci->i_ceph_lock);
1101 ceph_buffer_put(old_blob); /* Shouldn't be required */
1102 dout(" pre-allocating new blob size=%d\n", required_blob_size);
1103 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
1104 if (!blob)
1105 goto do_sync_unlocked;
1106 spin_lock(&ci->i_ceph_lock);
1107 /* prealloc_blob can't be released while holding i_ceph_lock */
1108 if (ci->i_xattrs.prealloc_blob)
1109 old_blob = ci->i_xattrs.prealloc_blob;
1110 ci->i_xattrs.prealloc_blob = blob;
1111 goto retry;
1112 }
1113
1114 err = __set_xattr(ci, newname, name_len, newval, val_len,
1115 flags, value ? 1 : -1, &xattr);
1116
1117 if (!err) {
1118 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1119 &prealloc_cf);
1120 ci->i_xattrs.dirty = true;
1121 inode->i_ctime = current_time(inode);
1122 }
1123
1124 spin_unlock(&ci->i_ceph_lock);
1125 ceph_buffer_put(old_blob);
1126 if (lock_snap_rwsem)
1127 up_read(&mdsc->snap_rwsem);
1128 if (dirty)
1129 __mark_inode_dirty(inode, dirty);
1130 ceph_free_cap_flush(prealloc_cf);
1131 return err;
1132
1133 do_sync:
1134 spin_unlock(&ci->i_ceph_lock);
1135 do_sync_unlocked:
1136 if (lock_snap_rwsem)
1137 up_read(&mdsc->snap_rwsem);
1138
1139 /* security module set xattr while filling trace */
1140 if (current->journal_info) {
1141 pr_warn_ratelimited("sync setxattr %p "
1142 "during filling trace\n", inode);
1143 err = -EBUSY;
1144 } else {
1145 err = ceph_sync_setxattr(inode, name, value, size, flags);
1146 if (err >= 0 && check_realm) {
1147 /* check if snaprealm was created for quota inode */
1148 spin_lock(&ci->i_ceph_lock);
1149 if ((ci->i_max_files || ci->i_max_bytes) &&
1150 !(ci->i_snap_realm &&
1151 ci->i_snap_realm->ino == ci->i_vino.ino))
1152 err = -EOPNOTSUPP;
1153 spin_unlock(&ci->i_ceph_lock);
1154 }
1155 }
1156 out:
1157 ceph_free_cap_flush(prealloc_cf);
1158 kfree(newname);
1159 kfree(newval);
1160 kfree(xattr);
1161 return err;
1162 }
1163
ceph_get_xattr_handler(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * value,size_t size)1164 static int ceph_get_xattr_handler(const struct xattr_handler *handler,
1165 struct dentry *dentry, struct inode *inode,
1166 const char *name, void *value, size_t size)
1167 {
1168 if (!ceph_is_valid_xattr(name))
1169 return -EOPNOTSUPP;
1170 return __ceph_getxattr(inode, name, value, size);
1171 }
1172
ceph_set_xattr_handler(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)1173 static int ceph_set_xattr_handler(const struct xattr_handler *handler,
1174 struct dentry *unused, struct inode *inode,
1175 const char *name, const void *value,
1176 size_t size, int flags)
1177 {
1178 if (!ceph_is_valid_xattr(name))
1179 return -EOPNOTSUPP;
1180 return __ceph_setxattr(inode, name, value, size, flags);
1181 }
1182
1183 static const struct xattr_handler ceph_other_xattr_handler = {
1184 .prefix = "", /* match any name => handlers called with full name */
1185 .get = ceph_get_xattr_handler,
1186 .set = ceph_set_xattr_handler,
1187 };
1188
1189 #ifdef CONFIG_SECURITY
ceph_security_xattr_wanted(struct inode * in)1190 bool ceph_security_xattr_wanted(struct inode *in)
1191 {
1192 return in->i_security != NULL;
1193 }
1194
ceph_security_xattr_deadlock(struct inode * in)1195 bool ceph_security_xattr_deadlock(struct inode *in)
1196 {
1197 struct ceph_inode_info *ci;
1198 bool ret;
1199 if (!in->i_security)
1200 return false;
1201 ci = ceph_inode(in);
1202 spin_lock(&ci->i_ceph_lock);
1203 ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1204 !(ci->i_xattrs.version > 0 &&
1205 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1206 spin_unlock(&ci->i_ceph_lock);
1207 return ret;
1208 }
1209 #endif
1210