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