1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3 #include <linux/ceph/pagelist.h>
6 #include "mds_client.h"
8 #include <linux/ceph/decode.h>
10 #include <linux/xattr.h>
11 #include <linux/security.h>
12 #include <linux/posix_acl_xattr.h>
13 #include <linux/slab.h>
15 #define XATTR_CEPH_PREFIX "ceph."
16 #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
18 static int __remove_xattr(struct ceph_inode_info *ci,
19 struct ceph_inode_xattr *xattr);
21 static bool ceph_is_valid_xattr(const char *name)
23 return !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
24 !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
25 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
26 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
30 * These define virtual xattrs exposing the recursive directory
31 * statistics and layout metadata.
35 size_t name_size; /* strlen(name) + 1 (for '\0') */
36 ssize_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
38 bool (*exists_cb)(struct ceph_inode_info *ci);
42 #define VXATTR_FLAG_READONLY (1<<0)
43 #define VXATTR_FLAG_HIDDEN (1<<1)
44 #define VXATTR_FLAG_RSTAT (1<<2)
45 #define VXATTR_FLAG_DIRSTAT (1<<3)
49 static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
51 struct ceph_file_layout *fl = &ci->i_layout;
52 return (fl->stripe_unit > 0 || fl->stripe_count > 0 ||
53 fl->object_size > 0 || fl->pool_id >= 0 ||
54 rcu_dereference_raw(fl->pool_ns) != NULL);
57 static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
60 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.inode.i_sb);
61 struct ceph_osd_client *osdc = &fsc->client->osdc;
62 struct ceph_string *pool_ns;
63 s64 pool = ci->i_layout.pool_id;
64 const char *pool_name;
65 const char *ns_field = " pool_namespace=";
67 size_t len, total_len = 0;
70 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
72 dout("ceph_vxattrcb_layout %p\n", &ci->netfs.inode);
73 down_read(&osdc->lock);
74 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
76 len = snprintf(buf, sizeof(buf),
77 "stripe_unit=%u stripe_count=%u object_size=%u pool=",
78 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
79 ci->i_layout.object_size);
80 total_len = len + strlen(pool_name);
82 len = snprintf(buf, sizeof(buf),
83 "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
84 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
85 ci->i_layout.object_size, pool);
90 total_len += strlen(ns_field) + pool_ns->len;
93 if (size >= total_len) {
94 memcpy(val, buf, len);
97 len = strlen(pool_name);
98 memcpy(val + ret, pool_name, len);
102 len = strlen(ns_field);
103 memcpy(val + ret, ns_field, len);
105 memcpy(val + ret, pool_ns->str, pool_ns->len);
109 up_read(&osdc->lock);
110 ceph_put_string(pool_ns);
115 * The convention with strings in xattrs is that they should not be NULL
116 * terminated, since we're returning the length with them. snprintf always
117 * NULL terminates however, so call it on a temporary buffer and then memcpy
118 * the result into place.
120 static __printf(3, 4)
121 int ceph_fmt_xattr(char *val, size_t size, const char *fmt, ...)
125 char buf[96]; /* NB: reevaluate size if new vxattrs are added */
128 ret = vsnprintf(buf, size ? sizeof(buf) : 0, fmt, args);
132 if (size && ret + 1 > sizeof(buf)) {
133 WARN_ONCE(true, "Returned length too big (%d)", ret);
138 memcpy(val, buf, ret);
142 static ssize_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
143 char *val, size_t size)
145 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_unit);
148 static ssize_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
149 char *val, size_t size)
151 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_count);
154 static ssize_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
155 char *val, size_t size)
157 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.object_size);
160 static ssize_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
161 char *val, size_t size)
164 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.inode.i_sb);
165 struct ceph_osd_client *osdc = &fsc->client->osdc;
166 s64 pool = ci->i_layout.pool_id;
167 const char *pool_name;
169 down_read(&osdc->lock);
170 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
172 ret = strlen(pool_name);
174 memcpy(val, pool_name, ret);
176 ret = ceph_fmt_xattr(val, size, "%lld", pool);
178 up_read(&osdc->lock);
182 static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
183 char *val, size_t size)
186 struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
191 memcpy(val, ns->str, ret);
199 static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
202 return ceph_fmt_xattr(val, size, "%lld", ci->i_files + ci->i_subdirs);
205 static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
208 return ceph_fmt_xattr(val, size, "%lld", ci->i_files);
211 static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
214 return ceph_fmt_xattr(val, size, "%lld", ci->i_subdirs);
217 static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
220 return ceph_fmt_xattr(val, size, "%lld",
221 ci->i_rfiles + ci->i_rsubdirs);
224 static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
227 return ceph_fmt_xattr(val, size, "%lld", ci->i_rfiles);
230 static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
233 return ceph_fmt_xattr(val, size, "%lld", ci->i_rsubdirs);
236 static ssize_t ceph_vxattrcb_dir_rsnaps(struct ceph_inode_info *ci, char *val,
239 return ceph_fmt_xattr(val, size, "%lld", ci->i_rsnaps);
242 static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
245 return ceph_fmt_xattr(val, size, "%lld", ci->i_rbytes);
248 static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
251 return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
252 ci->i_rctime.tv_nsec);
256 static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci)
258 return ci->i_dir_pin != -ENODATA;
261 static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val,
264 return ceph_fmt_xattr(val, size, "%d", (int)ci->i_dir_pin);
268 static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
271 spin_lock(&ci->i_ceph_lock);
272 if ((ci->i_max_files || ci->i_max_bytes) &&
273 ci->i_vino.snap == CEPH_NOSNAP &&
275 ci->i_snap_realm->ino == ci->i_vino.ino)
277 spin_unlock(&ci->i_ceph_lock);
281 static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
284 return ceph_fmt_xattr(val, size, "max_bytes=%llu max_files=%llu",
285 ci->i_max_bytes, ci->i_max_files);
288 static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
289 char *val, size_t size)
291 return ceph_fmt_xattr(val, size, "%llu", ci->i_max_bytes);
294 static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
295 char *val, size_t size)
297 return ceph_fmt_xattr(val, size, "%llu", ci->i_max_files);
301 static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci)
303 return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0);
306 static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val,
309 return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec,
310 ci->i_snap_btime.tv_nsec);
313 static ssize_t ceph_vxattrcb_cluster_fsid(struct ceph_inode_info *ci,
314 char *val, size_t size)
316 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.inode.i_sb);
318 return ceph_fmt_xattr(val, size, "%pU", &fsc->client->fsid);
321 static ssize_t ceph_vxattrcb_client_id(struct ceph_inode_info *ci,
322 char *val, size_t size)
324 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.inode.i_sb);
326 return ceph_fmt_xattr(val, size, "client%lld",
327 ceph_client_gid(fsc->client));
330 static ssize_t ceph_vxattrcb_caps(struct ceph_inode_info *ci, char *val,
335 spin_lock(&ci->i_ceph_lock);
336 issued = __ceph_caps_issued(ci, NULL);
337 spin_unlock(&ci->i_ceph_lock);
339 return ceph_fmt_xattr(val, size, "%s/0x%x",
340 ceph_cap_string(issued), issued);
343 static ssize_t ceph_vxattrcb_auth_mds(struct ceph_inode_info *ci,
344 char *val, size_t size)
348 spin_lock(&ci->i_ceph_lock);
349 ret = ceph_fmt_xattr(val, size, "%d",
350 ci->i_auth_cap ? ci->i_auth_cap->session->s_mds : -1);
351 spin_unlock(&ci->i_ceph_lock);
355 #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
356 #define CEPH_XATTR_NAME2(_type, _name, _name2) \
357 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
359 #define XATTR_NAME_CEPH(_type, _name, _flags) \
361 .name = CEPH_XATTR_NAME(_type, _name), \
362 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
363 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
365 .flags = (VXATTR_FLAG_READONLY | _flags), \
367 #define XATTR_RSTAT_FIELD(_type, _name) \
368 XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
369 #define XATTR_RSTAT_FIELD_UPDATABLE(_type, _name) \
371 .name = CEPH_XATTR_NAME(_type, _name), \
372 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
373 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
375 .flags = VXATTR_FLAG_RSTAT, \
377 #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
379 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
380 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
381 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
382 .exists_cb = ceph_vxattrcb_layout_exists, \
383 .flags = VXATTR_FLAG_HIDDEN, \
385 #define XATTR_QUOTA_FIELD(_type, _name) \
387 .name = CEPH_XATTR_NAME(_type, _name), \
388 .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
389 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
390 .exists_cb = ceph_vxattrcb_quota_exists, \
391 .flags = VXATTR_FLAG_HIDDEN, \
394 static struct ceph_vxattr ceph_dir_vxattrs[] = {
396 .name = "ceph.dir.layout",
397 .name_size = sizeof("ceph.dir.layout"),
398 .getxattr_cb = ceph_vxattrcb_layout,
399 .exists_cb = ceph_vxattrcb_layout_exists,
400 .flags = VXATTR_FLAG_HIDDEN,
402 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
403 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
404 XATTR_LAYOUT_FIELD(dir, layout, object_size),
405 XATTR_LAYOUT_FIELD(dir, layout, pool),
406 XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
407 XATTR_NAME_CEPH(dir, entries, VXATTR_FLAG_DIRSTAT),
408 XATTR_NAME_CEPH(dir, files, VXATTR_FLAG_DIRSTAT),
409 XATTR_NAME_CEPH(dir, subdirs, VXATTR_FLAG_DIRSTAT),
410 XATTR_RSTAT_FIELD(dir, rentries),
411 XATTR_RSTAT_FIELD(dir, rfiles),
412 XATTR_RSTAT_FIELD(dir, rsubdirs),
413 XATTR_RSTAT_FIELD(dir, rsnaps),
414 XATTR_RSTAT_FIELD(dir, rbytes),
415 XATTR_RSTAT_FIELD_UPDATABLE(dir, rctime),
417 .name = "ceph.dir.pin",
418 .name_size = sizeof("ceph.dir.pin"),
419 .getxattr_cb = ceph_vxattrcb_dir_pin,
420 .exists_cb = ceph_vxattrcb_dir_pin_exists,
421 .flags = VXATTR_FLAG_HIDDEN,
424 .name = "ceph.quota",
425 .name_size = sizeof("ceph.quota"),
426 .getxattr_cb = ceph_vxattrcb_quota,
427 .exists_cb = ceph_vxattrcb_quota_exists,
428 .flags = VXATTR_FLAG_HIDDEN,
430 XATTR_QUOTA_FIELD(quota, max_bytes),
431 XATTR_QUOTA_FIELD(quota, max_files),
433 .name = "ceph.snap.btime",
434 .name_size = sizeof("ceph.snap.btime"),
435 .getxattr_cb = ceph_vxattrcb_snap_btime,
436 .exists_cb = ceph_vxattrcb_snap_btime_exists,
437 .flags = VXATTR_FLAG_READONLY,
441 .name_size = sizeof("ceph.caps"),
442 .getxattr_cb = ceph_vxattrcb_caps,
444 .flags = VXATTR_FLAG_HIDDEN,
446 { .name = NULL, 0 } /* Required table terminator */
451 static struct ceph_vxattr ceph_file_vxattrs[] = {
453 .name = "ceph.file.layout",
454 .name_size = sizeof("ceph.file.layout"),
455 .getxattr_cb = ceph_vxattrcb_layout,
456 .exists_cb = ceph_vxattrcb_layout_exists,
457 .flags = VXATTR_FLAG_HIDDEN,
459 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
460 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
461 XATTR_LAYOUT_FIELD(file, layout, object_size),
462 XATTR_LAYOUT_FIELD(file, layout, pool),
463 XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
465 .name = "ceph.snap.btime",
466 .name_size = sizeof("ceph.snap.btime"),
467 .getxattr_cb = ceph_vxattrcb_snap_btime,
468 .exists_cb = ceph_vxattrcb_snap_btime_exists,
469 .flags = VXATTR_FLAG_READONLY,
473 .name_size = sizeof("ceph.caps"),
474 .getxattr_cb = ceph_vxattrcb_caps,
476 .flags = VXATTR_FLAG_HIDDEN,
478 { .name = NULL, 0 } /* Required table terminator */
481 static struct ceph_vxattr ceph_common_vxattrs[] = {
483 .name = "ceph.cluster_fsid",
484 .name_size = sizeof("ceph.cluster_fsid"),
485 .getxattr_cb = ceph_vxattrcb_cluster_fsid,
487 .flags = VXATTR_FLAG_READONLY,
490 .name = "ceph.client_id",
491 .name_size = sizeof("ceph.client_id"),
492 .getxattr_cb = ceph_vxattrcb_client_id,
494 .flags = VXATTR_FLAG_READONLY,
497 .name = "ceph.auth_mds",
498 .name_size = sizeof("ceph.auth_mds"),
499 .getxattr_cb = ceph_vxattrcb_auth_mds,
501 .flags = VXATTR_FLAG_READONLY,
503 { .name = NULL, 0 } /* Required table terminator */
506 static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
508 if (S_ISDIR(inode->i_mode))
509 return ceph_dir_vxattrs;
510 else if (S_ISREG(inode->i_mode))
511 return ceph_file_vxattrs;
515 static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
518 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
521 while (vxattr->name) {
522 if (!strcmp(vxattr->name, name))
528 vxattr = ceph_common_vxattrs;
529 while (vxattr->name) {
530 if (!strcmp(vxattr->name, name))
538 #define MAX_XATTR_VAL_PRINT_LEN 256
540 static int __set_xattr(struct ceph_inode_info *ci,
541 const char *name, int name_len,
542 const char *val, int val_len,
543 int flags, int update_xattr,
544 struct ceph_inode_xattr **newxattr)
547 struct rb_node *parent = NULL;
548 struct ceph_inode_xattr *xattr = NULL;
552 p = &ci->i_xattrs.index.rb_node;
555 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
556 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
562 if (name_len == xattr->name_len)
564 else if (name_len < xattr->name_len)
575 if (xattr && (flags & XATTR_CREATE))
577 else if (!xattr && (flags & XATTR_REPLACE))
585 if (update_xattr < 0) {
587 __remove_xattr(ci, xattr);
598 xattr->name_len = name_len;
599 xattr->should_free_name = update_xattr;
601 ci->i_xattrs.count++;
602 dout("%s count=%d\n", __func__, ci->i_xattrs.count);
606 if (xattr->should_free_val)
613 ci->i_xattrs.names_size -= xattr->name_len;
614 ci->i_xattrs.vals_size -= xattr->val_len;
616 ci->i_xattrs.names_size += name_len;
617 ci->i_xattrs.vals_size += val_len;
623 xattr->val_len = val_len;
624 xattr->dirty = update_xattr;
625 xattr->should_free_val = (val && update_xattr);
628 rb_link_node(&xattr->node, parent, p);
629 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
630 dout("%s p=%p\n", __func__, p);
633 dout("%s added %llx.%llx xattr %p %.*s=%.*s%s\n", __func__,
634 ceph_vinop(&ci->netfs.inode), xattr, name_len, name,
635 min(val_len, MAX_XATTR_VAL_PRINT_LEN), val,
636 val_len > MAX_XATTR_VAL_PRINT_LEN ? "..." : "");
641 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
645 struct rb_node *parent = NULL;
646 struct ceph_inode_xattr *xattr = NULL;
647 int name_len = strlen(name);
650 p = &ci->i_xattrs.index.rb_node;
653 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
654 c = strncmp(name, xattr->name, xattr->name_len);
655 if (c == 0 && name_len > xattr->name_len)
662 int len = min(xattr->val_len, MAX_XATTR_VAL_PRINT_LEN);
664 dout("%s %s: found %.*s%s\n", __func__, name, len,
665 xattr->val, xattr->val_len > len ? "..." : "");
670 dout("%s %s: not found\n", __func__, name);
675 static void __free_xattr(struct ceph_inode_xattr *xattr)
679 if (xattr->should_free_name)
681 if (xattr->should_free_val)
687 static int __remove_xattr(struct ceph_inode_info *ci,
688 struct ceph_inode_xattr *xattr)
693 rb_erase(&xattr->node, &ci->i_xattrs.index);
695 if (xattr->should_free_name)
697 if (xattr->should_free_val)
700 ci->i_xattrs.names_size -= xattr->name_len;
701 ci->i_xattrs.vals_size -= xattr->val_len;
702 ci->i_xattrs.count--;
708 static char *__copy_xattr_names(struct ceph_inode_info *ci,
712 struct ceph_inode_xattr *xattr = NULL;
714 p = rb_first(&ci->i_xattrs.index);
715 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
718 xattr = rb_entry(p, struct ceph_inode_xattr, node);
719 memcpy(dest, xattr->name, xattr->name_len);
720 dest[xattr->name_len] = '\0';
722 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
723 xattr->name_len, ci->i_xattrs.names_size);
725 dest += xattr->name_len + 1;
732 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
734 struct rb_node *p, *tmp;
735 struct ceph_inode_xattr *xattr = NULL;
737 p = rb_first(&ci->i_xattrs.index);
739 dout("__ceph_destroy_xattrs p=%p\n", p);
742 xattr = rb_entry(p, struct ceph_inode_xattr, node);
745 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
746 xattr->name_len, xattr->name);
747 rb_erase(tmp, &ci->i_xattrs.index);
752 ci->i_xattrs.names_size = 0;
753 ci->i_xattrs.vals_size = 0;
754 ci->i_xattrs.index_version = 0;
755 ci->i_xattrs.count = 0;
756 ci->i_xattrs.index = RB_ROOT;
759 static int __build_xattrs(struct inode *inode)
760 __releases(ci->i_ceph_lock)
761 __acquires(ci->i_ceph_lock)
767 const char *name, *val;
768 struct ceph_inode_info *ci = ceph_inode(inode);
770 struct ceph_inode_xattr **xattrs = NULL;
774 dout("__build_xattrs() len=%d\n",
775 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
777 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
778 return 0; /* already built */
780 __ceph_destroy_xattrs(ci);
783 /* updated internal xattr rb tree */
784 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
785 p = ci->i_xattrs.blob->vec.iov_base;
786 end = p + ci->i_xattrs.blob->vec.iov_len;
787 ceph_decode_32_safe(&p, end, numattr, bad);
788 xattr_version = ci->i_xattrs.version;
789 spin_unlock(&ci->i_ceph_lock);
791 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
797 for (i = 0; i < numattr; i++) {
798 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
804 spin_lock(&ci->i_ceph_lock);
805 if (ci->i_xattrs.version != xattr_version) {
806 /* lost a race, retry */
807 for (i = 0; i < numattr; i++)
815 ceph_decode_32_safe(&p, end, len, bad);
819 ceph_decode_32_safe(&p, end, len, bad);
823 err = __set_xattr(ci, name, namelen, val, len,
824 0, 0, &xattrs[numattr]);
831 ci->i_xattrs.index_version = ci->i_xattrs.version;
832 ci->i_xattrs.dirty = false;
836 spin_lock(&ci->i_ceph_lock);
839 for (i = 0; i < numattr; i++)
843 ci->i_xattrs.names_size = 0;
847 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
851 * 4 bytes for the length, and additional 4 bytes per each xattr name,
852 * 4 bytes per each value
854 int size = 4 + ci->i_xattrs.count*(4 + 4) +
855 ci->i_xattrs.names_size +
856 ci->i_xattrs.vals_size;
857 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
858 ci->i_xattrs.count, ci->i_xattrs.names_size,
859 ci->i_xattrs.vals_size);
862 size += 4 + 4 + name_size + val_size;
868 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
869 * and swap into place. It returns the old i_xattrs.blob (or NULL) so
870 * that it can be freed by the caller as the i_ceph_lock is likely to be
873 struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
876 struct ceph_inode_xattr *xattr = NULL;
877 struct ceph_buffer *old_blob = NULL;
880 dout("__build_xattrs_blob %p\n", &ci->netfs.inode);
881 if (ci->i_xattrs.dirty) {
882 int need = __get_required_blob_size(ci, 0, 0);
884 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
886 p = rb_first(&ci->i_xattrs.index);
887 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
889 ceph_encode_32(&dest, ci->i_xattrs.count);
891 xattr = rb_entry(p, struct ceph_inode_xattr, node);
893 ceph_encode_32(&dest, xattr->name_len);
894 memcpy(dest, xattr->name, xattr->name_len);
895 dest += xattr->name_len;
896 ceph_encode_32(&dest, xattr->val_len);
897 memcpy(dest, xattr->val, xattr->val_len);
898 dest += xattr->val_len;
903 /* adjust buffer len; it may be larger than we need */
904 ci->i_xattrs.prealloc_blob->vec.iov_len =
905 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
907 if (ci->i_xattrs.blob)
908 old_blob = ci->i_xattrs.blob;
909 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
910 ci->i_xattrs.prealloc_blob = NULL;
911 ci->i_xattrs.dirty = false;
912 ci->i_xattrs.version++;
918 static inline int __get_request_mask(struct inode *in) {
919 struct ceph_mds_request *req = current->journal_info;
921 if (req && req->r_target_inode == in) {
922 if (req->r_op == CEPH_MDS_OP_LOOKUP ||
923 req->r_op == CEPH_MDS_OP_LOOKUPINO ||
924 req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
925 req->r_op == CEPH_MDS_OP_GETATTR) {
926 mask = le32_to_cpu(req->r_args.getattr.mask);
927 } else if (req->r_op == CEPH_MDS_OP_OPEN ||
928 req->r_op == CEPH_MDS_OP_CREATE) {
929 mask = le32_to_cpu(req->r_args.open.mask);
935 ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
938 struct ceph_inode_info *ci = ceph_inode(inode);
939 struct ceph_inode_xattr *xattr;
940 struct ceph_vxattr *vxattr;
944 if (strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
945 goto handle_non_vxattrs;
947 /* let's see if a virtual xattr was requested */
948 vxattr = ceph_match_vxattr(inode, name);
951 if (vxattr->flags & VXATTR_FLAG_RSTAT)
952 mask |= CEPH_STAT_RSTAT;
953 if (vxattr->flags & VXATTR_FLAG_DIRSTAT)
954 mask |= CEPH_CAP_FILE_SHARED;
955 err = ceph_do_getattr(inode, mask, true);
959 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
960 err = vxattr->getxattr_cb(ci, value, size);
961 if (size && size < err)
966 err = ceph_do_getvxattr(inode, name, value, size);
967 /* this would happen with a new client and old server combo */
968 if (err == -EOPNOTSUPP)
973 req_mask = __get_request_mask(inode);
975 spin_lock(&ci->i_ceph_lock);
976 dout("getxattr %p name '%s' ver=%lld index_ver=%lld\n", inode, name,
977 ci->i_xattrs.version, ci->i_xattrs.index_version);
979 if (ci->i_xattrs.version == 0 ||
980 !((req_mask & CEPH_CAP_XATTR_SHARED) ||
981 __ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1))) {
982 spin_unlock(&ci->i_ceph_lock);
984 /* security module gets xattr while filling trace */
985 if (current->journal_info) {
986 pr_warn_ratelimited("sync getxattr %p "
987 "during filling trace\n", inode);
991 /* get xattrs from mds (if we don't already have them) */
992 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
995 spin_lock(&ci->i_ceph_lock);
998 err = __build_xattrs(inode);
1002 err = -ENODATA; /* == ENOATTR */
1003 xattr = __get_xattr(ci, name);
1008 if (size && size < xattr->val_len)
1011 err = xattr->val_len;
1015 memcpy(value, xattr->val, xattr->val_len);
1017 if (current->journal_info &&
1018 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
1019 security_ismaclabel(name + XATTR_SECURITY_PREFIX_LEN))
1020 ci->i_ceph_flags |= CEPH_I_SEC_INITED;
1022 spin_unlock(&ci->i_ceph_lock);
1026 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
1028 struct inode *inode = d_inode(dentry);
1029 struct ceph_inode_info *ci = ceph_inode(inode);
1030 bool len_only = (size == 0);
1034 spin_lock(&ci->i_ceph_lock);
1035 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
1036 ci->i_xattrs.version, ci->i_xattrs.index_version);
1038 if (ci->i_xattrs.version == 0 ||
1039 !__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1)) {
1040 spin_unlock(&ci->i_ceph_lock);
1041 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
1044 spin_lock(&ci->i_ceph_lock);
1047 err = __build_xattrs(inode);
1051 /* add 1 byte for each xattr due to the null termination */
1052 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
1054 if (namelen > size) {
1058 names = __copy_xattr_names(ci, names);
1063 spin_unlock(&ci->i_ceph_lock);
1067 static int ceph_sync_setxattr(struct inode *inode, const char *name,
1068 const char *value, size_t size, int flags)
1070 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
1071 struct ceph_inode_info *ci = ceph_inode(inode);
1072 struct ceph_mds_request *req;
1073 struct ceph_mds_client *mdsc = fsc->mdsc;
1074 struct ceph_osd_client *osdc = &fsc->client->osdc;
1075 struct ceph_pagelist *pagelist = NULL;
1076 int op = CEPH_MDS_OP_SETXATTR;
1080 /* copy value into pagelist */
1081 pagelist = ceph_pagelist_alloc(GFP_NOFS);
1085 err = ceph_pagelist_append(pagelist, value, size);
1088 } else if (!value) {
1089 if (flags & CEPH_XATTR_REPLACE)
1090 op = CEPH_MDS_OP_RMXATTR;
1092 flags |= CEPH_XATTR_REMOVE;
1095 dout("setxattr value size: %zu\n", size);
1098 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1104 req->r_path2 = kstrdup(name, GFP_NOFS);
1105 if (!req->r_path2) {
1106 ceph_mdsc_put_request(req);
1111 if (op == CEPH_MDS_OP_SETXATTR) {
1112 req->r_args.setxattr.flags = cpu_to_le32(flags);
1113 req->r_args.setxattr.osdmap_epoch =
1114 cpu_to_le32(osdc->osdmap->epoch);
1115 req->r_pagelist = pagelist;
1119 req->r_inode = inode;
1121 req->r_num_caps = 1;
1122 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1124 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
1125 err = ceph_mdsc_do_request(mdsc, NULL, req);
1126 ceph_mdsc_put_request(req);
1127 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
1131 ceph_pagelist_release(pagelist);
1135 int __ceph_setxattr(struct inode *inode, const char *name,
1136 const void *value, size_t size, int flags)
1138 struct ceph_vxattr *vxattr;
1139 struct ceph_inode_info *ci = ceph_inode(inode);
1140 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1141 struct ceph_cap_flush *prealloc_cf = NULL;
1142 struct ceph_buffer *old_blob = NULL;
1146 int name_len = strlen(name);
1148 char *newname = NULL;
1149 char *newval = NULL;
1150 struct ceph_inode_xattr *xattr = NULL;
1151 int required_blob_size;
1152 bool check_realm = false;
1153 bool lock_snap_rwsem = false;
1155 if (ceph_snap(inode) != CEPH_NOSNAP)
1158 vxattr = ceph_match_vxattr(inode, name);
1160 if (vxattr->flags & VXATTR_FLAG_READONLY)
1162 if (value && !strncmp(vxattr->name, "ceph.quota", 10))
1166 /* pass any unhandled ceph.* xattrs through to the MDS */
1167 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1168 goto do_sync_unlocked;
1170 /* preallocate memory for xattr name, value, index node */
1172 newname = kmemdup(name, name_len + 1, GFP_NOFS);
1177 newval = kmemdup(value, val_len, GFP_NOFS);
1182 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1186 prealloc_cf = ceph_alloc_cap_flush();
1190 spin_lock(&ci->i_ceph_lock);
1192 issued = __ceph_caps_issued(ci, NULL);
1193 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1194 if ((ci->i_xattrs.version == 0) || !(issued & CEPH_CAP_XATTR_EXCL) ||
1195 (required_blob_size > mdsc->mdsmap->m_max_xattr_size)) {
1196 dout("%s do sync setxattr: version: %llu size: %d max: %llu\n",
1197 __func__, ci->i_xattrs.version, required_blob_size,
1198 mdsc->mdsmap->m_max_xattr_size);
1202 if (!lock_snap_rwsem && !ci->i_head_snapc) {
1203 lock_snap_rwsem = true;
1204 if (!down_read_trylock(&mdsc->snap_rwsem)) {
1205 spin_unlock(&ci->i_ceph_lock);
1206 down_read(&mdsc->snap_rwsem);
1207 spin_lock(&ci->i_ceph_lock);
1212 dout("setxattr %p name '%s' issued %s\n", inode, name,
1213 ceph_cap_string(issued));
1214 __build_xattrs(inode);
1216 if (!ci->i_xattrs.prealloc_blob ||
1217 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
1218 struct ceph_buffer *blob;
1220 spin_unlock(&ci->i_ceph_lock);
1221 ceph_buffer_put(old_blob); /* Shouldn't be required */
1222 dout(" pre-allocating new blob size=%d\n", required_blob_size);
1223 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
1225 goto do_sync_unlocked;
1226 spin_lock(&ci->i_ceph_lock);
1227 /* prealloc_blob can't be released while holding i_ceph_lock */
1228 if (ci->i_xattrs.prealloc_blob)
1229 old_blob = ci->i_xattrs.prealloc_blob;
1230 ci->i_xattrs.prealloc_blob = blob;
1234 err = __set_xattr(ci, newname, name_len, newval, val_len,
1235 flags, value ? 1 : -1, &xattr);
1238 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1240 ci->i_xattrs.dirty = true;
1241 inode_set_ctime_current(inode);
1244 spin_unlock(&ci->i_ceph_lock);
1245 ceph_buffer_put(old_blob);
1246 if (lock_snap_rwsem)
1247 up_read(&mdsc->snap_rwsem);
1249 __mark_inode_dirty(inode, dirty);
1250 ceph_free_cap_flush(prealloc_cf);
1254 spin_unlock(&ci->i_ceph_lock);
1256 if (lock_snap_rwsem)
1257 up_read(&mdsc->snap_rwsem);
1259 /* security module set xattr while filling trace */
1260 if (current->journal_info) {
1261 pr_warn_ratelimited("sync setxattr %p "
1262 "during filling trace\n", inode);
1265 err = ceph_sync_setxattr(inode, name, value, size, flags);
1266 if (err >= 0 && check_realm) {
1267 /* check if snaprealm was created for quota inode */
1268 spin_lock(&ci->i_ceph_lock);
1269 if ((ci->i_max_files || ci->i_max_bytes) &&
1270 !(ci->i_snap_realm &&
1271 ci->i_snap_realm->ino == ci->i_vino.ino))
1273 spin_unlock(&ci->i_ceph_lock);
1277 ceph_free_cap_flush(prealloc_cf);
1284 static int ceph_get_xattr_handler(const struct xattr_handler *handler,
1285 struct dentry *dentry, struct inode *inode,
1286 const char *name, void *value, size_t size)
1288 if (!ceph_is_valid_xattr(name))
1290 return __ceph_getxattr(inode, name, value, size);
1293 static int ceph_set_xattr_handler(const struct xattr_handler *handler,
1294 struct mnt_idmap *idmap,
1295 struct dentry *unused, struct inode *inode,
1296 const char *name, const void *value,
1297 size_t size, int flags)
1299 if (!ceph_is_valid_xattr(name))
1301 return __ceph_setxattr(inode, name, value, size, flags);
1304 static const struct xattr_handler ceph_other_xattr_handler = {
1305 .prefix = "", /* match any name => handlers called with full name */
1306 .get = ceph_get_xattr_handler,
1307 .set = ceph_set_xattr_handler,
1310 #ifdef CONFIG_SECURITY
1311 bool ceph_security_xattr_wanted(struct inode *in)
1313 return in->i_security != NULL;
1316 bool ceph_security_xattr_deadlock(struct inode *in)
1318 struct ceph_inode_info *ci;
1320 if (!in->i_security)
1322 ci = ceph_inode(in);
1323 spin_lock(&ci->i_ceph_lock);
1324 ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1325 !(ci->i_xattrs.version > 0 &&
1326 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1327 spin_unlock(&ci->i_ceph_lock);
1331 #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1332 int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
1333 struct ceph_acl_sec_ctx *as_ctx)
1335 struct ceph_pagelist *pagelist = as_ctx->pagelist;
1340 err = security_dentry_init_security(dentry, mode, &dentry->d_name,
1341 &name, &as_ctx->sec_ctx,
1342 &as_ctx->sec_ctxlen);
1344 WARN_ON_ONCE(err != -EOPNOTSUPP);
1345 err = 0; /* do nothing */
1351 pagelist = ceph_pagelist_alloc(GFP_KERNEL);
1354 err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
1357 ceph_pagelist_encode_32(pagelist, 1);
1361 * FIXME: Make security_dentry_init_security() generic. Currently
1362 * It only supports single security module and only selinux has
1363 * dentry_init_security hook.
1365 name_len = strlen(name);
1366 err = ceph_pagelist_reserve(pagelist,
1367 4 * 2 + name_len + as_ctx->sec_ctxlen);
1371 if (as_ctx->pagelist) {
1372 /* update count of KV pairs */
1373 BUG_ON(pagelist->length <= sizeof(__le32));
1374 if (list_is_singular(&pagelist->head)) {
1375 le32_add_cpu((__le32*)pagelist->mapped_tail, 1);
1377 struct page *page = list_first_entry(&pagelist->head,
1379 void *addr = kmap_atomic(page);
1380 le32_add_cpu((__le32*)addr, 1);
1381 kunmap_atomic(addr);
1384 as_ctx->pagelist = pagelist;
1387 ceph_pagelist_encode_32(pagelist, name_len);
1388 ceph_pagelist_append(pagelist, name, name_len);
1390 ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen);
1391 ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1395 if (pagelist && !as_ctx->pagelist)
1396 ceph_pagelist_release(pagelist);
1399 #endif /* CONFIG_CEPH_FS_SECURITY_LABEL */
1400 #endif /* CONFIG_SECURITY */
1402 void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
1404 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1405 posix_acl_release(as_ctx->acl);
1406 posix_acl_release(as_ctx->default_acl);
1408 #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1409 security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1411 if (as_ctx->pagelist)
1412 ceph_pagelist_release(as_ctx->pagelist);
1416 * List of handlers for synthetic system.* attributes. Other
1417 * attributes are handled directly.
1419 const struct xattr_handler *ceph_xattr_handlers[] = {
1420 &ceph_other_xattr_handler,