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 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
356 static bool ceph_vxattrcb_fscrypt_auth_exists(struct ceph_inode_info *ci)
358 return ci->fscrypt_auth_len;
361 static ssize_t ceph_vxattrcb_fscrypt_auth(struct ceph_inode_info *ci,
362 char *val, size_t size)
365 if (size < ci->fscrypt_auth_len)
367 memcpy(val, ci->fscrypt_auth, ci->fscrypt_auth_len);
369 return ci->fscrypt_auth_len;
371 #endif /* CONFIG_FS_ENCRYPTION */
373 #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
374 #define CEPH_XATTR_NAME2(_type, _name, _name2) \
375 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
377 #define XATTR_NAME_CEPH(_type, _name, _flags) \
379 .name = CEPH_XATTR_NAME(_type, _name), \
380 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
381 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
383 .flags = (VXATTR_FLAG_READONLY | _flags), \
385 #define XATTR_RSTAT_FIELD(_type, _name) \
386 XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
387 #define XATTR_RSTAT_FIELD_UPDATABLE(_type, _name) \
389 .name = CEPH_XATTR_NAME(_type, _name), \
390 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
391 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
393 .flags = VXATTR_FLAG_RSTAT, \
395 #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
397 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
398 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
399 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
400 .exists_cb = ceph_vxattrcb_layout_exists, \
401 .flags = VXATTR_FLAG_HIDDEN, \
403 #define XATTR_QUOTA_FIELD(_type, _name) \
405 .name = CEPH_XATTR_NAME(_type, _name), \
406 .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
407 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
408 .exists_cb = ceph_vxattrcb_quota_exists, \
409 .flags = VXATTR_FLAG_HIDDEN, \
412 static struct ceph_vxattr ceph_dir_vxattrs[] = {
414 .name = "ceph.dir.layout",
415 .name_size = sizeof("ceph.dir.layout"),
416 .getxattr_cb = ceph_vxattrcb_layout,
417 .exists_cb = ceph_vxattrcb_layout_exists,
418 .flags = VXATTR_FLAG_HIDDEN,
420 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
421 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
422 XATTR_LAYOUT_FIELD(dir, layout, object_size),
423 XATTR_LAYOUT_FIELD(dir, layout, pool),
424 XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
425 XATTR_NAME_CEPH(dir, entries, VXATTR_FLAG_DIRSTAT),
426 XATTR_NAME_CEPH(dir, files, VXATTR_FLAG_DIRSTAT),
427 XATTR_NAME_CEPH(dir, subdirs, VXATTR_FLAG_DIRSTAT),
428 XATTR_RSTAT_FIELD(dir, rentries),
429 XATTR_RSTAT_FIELD(dir, rfiles),
430 XATTR_RSTAT_FIELD(dir, rsubdirs),
431 XATTR_RSTAT_FIELD(dir, rsnaps),
432 XATTR_RSTAT_FIELD(dir, rbytes),
433 XATTR_RSTAT_FIELD_UPDATABLE(dir, rctime),
435 .name = "ceph.dir.pin",
436 .name_size = sizeof("ceph.dir.pin"),
437 .getxattr_cb = ceph_vxattrcb_dir_pin,
438 .exists_cb = ceph_vxattrcb_dir_pin_exists,
439 .flags = VXATTR_FLAG_HIDDEN,
442 .name = "ceph.quota",
443 .name_size = sizeof("ceph.quota"),
444 .getxattr_cb = ceph_vxattrcb_quota,
445 .exists_cb = ceph_vxattrcb_quota_exists,
446 .flags = VXATTR_FLAG_HIDDEN,
448 XATTR_QUOTA_FIELD(quota, max_bytes),
449 XATTR_QUOTA_FIELD(quota, max_files),
451 .name = "ceph.snap.btime",
452 .name_size = sizeof("ceph.snap.btime"),
453 .getxattr_cb = ceph_vxattrcb_snap_btime,
454 .exists_cb = ceph_vxattrcb_snap_btime_exists,
455 .flags = VXATTR_FLAG_READONLY,
459 .name_size = sizeof("ceph.caps"),
460 .getxattr_cb = ceph_vxattrcb_caps,
462 .flags = VXATTR_FLAG_HIDDEN,
464 { .name = NULL, 0 } /* Required table terminator */
469 static struct ceph_vxattr ceph_file_vxattrs[] = {
471 .name = "ceph.file.layout",
472 .name_size = sizeof("ceph.file.layout"),
473 .getxattr_cb = ceph_vxattrcb_layout,
474 .exists_cb = ceph_vxattrcb_layout_exists,
475 .flags = VXATTR_FLAG_HIDDEN,
477 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
478 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
479 XATTR_LAYOUT_FIELD(file, layout, object_size),
480 XATTR_LAYOUT_FIELD(file, layout, pool),
481 XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
483 .name = "ceph.snap.btime",
484 .name_size = sizeof("ceph.snap.btime"),
485 .getxattr_cb = ceph_vxattrcb_snap_btime,
486 .exists_cb = ceph_vxattrcb_snap_btime_exists,
487 .flags = VXATTR_FLAG_READONLY,
491 .name_size = sizeof("ceph.caps"),
492 .getxattr_cb = ceph_vxattrcb_caps,
494 .flags = VXATTR_FLAG_HIDDEN,
496 { .name = NULL, 0 } /* Required table terminator */
499 static struct ceph_vxattr ceph_common_vxattrs[] = {
501 .name = "ceph.cluster_fsid",
502 .name_size = sizeof("ceph.cluster_fsid"),
503 .getxattr_cb = ceph_vxattrcb_cluster_fsid,
505 .flags = VXATTR_FLAG_READONLY,
508 .name = "ceph.client_id",
509 .name_size = sizeof("ceph.client_id"),
510 .getxattr_cb = ceph_vxattrcb_client_id,
512 .flags = VXATTR_FLAG_READONLY,
515 .name = "ceph.auth_mds",
516 .name_size = sizeof("ceph.auth_mds"),
517 .getxattr_cb = ceph_vxattrcb_auth_mds,
519 .flags = VXATTR_FLAG_READONLY,
521 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
523 .name = "ceph.fscrypt.auth",
524 .name_size = sizeof("ceph.fscrypt.auth"),
525 .getxattr_cb = ceph_vxattrcb_fscrypt_auth,
526 .exists_cb = ceph_vxattrcb_fscrypt_auth_exists,
527 .flags = VXATTR_FLAG_READONLY,
529 #endif /* CONFIG_FS_ENCRYPTION */
530 { .name = NULL, 0 } /* Required table terminator */
533 static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
535 if (S_ISDIR(inode->i_mode))
536 return ceph_dir_vxattrs;
537 else if (S_ISREG(inode->i_mode))
538 return ceph_file_vxattrs;
542 static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
545 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
548 while (vxattr->name) {
549 if (!strcmp(vxattr->name, name))
555 vxattr = ceph_common_vxattrs;
556 while (vxattr->name) {
557 if (!strcmp(vxattr->name, name))
565 #define MAX_XATTR_VAL_PRINT_LEN 256
567 static int __set_xattr(struct ceph_inode_info *ci,
568 const char *name, int name_len,
569 const char *val, int val_len,
570 int flags, int update_xattr,
571 struct ceph_inode_xattr **newxattr)
574 struct rb_node *parent = NULL;
575 struct ceph_inode_xattr *xattr = NULL;
579 p = &ci->i_xattrs.index.rb_node;
582 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
583 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
589 if (name_len == xattr->name_len)
591 else if (name_len < xattr->name_len)
602 if (xattr && (flags & XATTR_CREATE))
604 else if (!xattr && (flags & XATTR_REPLACE))
612 if (update_xattr < 0) {
614 __remove_xattr(ci, xattr);
625 xattr->name_len = name_len;
626 xattr->should_free_name = update_xattr;
628 ci->i_xattrs.count++;
629 dout("%s count=%d\n", __func__, ci->i_xattrs.count);
633 if (xattr->should_free_val)
640 ci->i_xattrs.names_size -= xattr->name_len;
641 ci->i_xattrs.vals_size -= xattr->val_len;
643 ci->i_xattrs.names_size += name_len;
644 ci->i_xattrs.vals_size += val_len;
650 xattr->val_len = val_len;
651 xattr->dirty = update_xattr;
652 xattr->should_free_val = (val && update_xattr);
655 rb_link_node(&xattr->node, parent, p);
656 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
657 dout("%s p=%p\n", __func__, p);
660 dout("%s added %llx.%llx xattr %p %.*s=%.*s%s\n", __func__,
661 ceph_vinop(&ci->netfs.inode), xattr, name_len, name,
662 min(val_len, MAX_XATTR_VAL_PRINT_LEN), val,
663 val_len > MAX_XATTR_VAL_PRINT_LEN ? "..." : "");
668 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
672 struct rb_node *parent = NULL;
673 struct ceph_inode_xattr *xattr = NULL;
674 int name_len = strlen(name);
677 p = &ci->i_xattrs.index.rb_node;
680 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
681 c = strncmp(name, xattr->name, xattr->name_len);
682 if (c == 0 && name_len > xattr->name_len)
689 int len = min(xattr->val_len, MAX_XATTR_VAL_PRINT_LEN);
691 dout("%s %s: found %.*s%s\n", __func__, name, len,
692 xattr->val, xattr->val_len > len ? "..." : "");
697 dout("%s %s: not found\n", __func__, name);
702 static void __free_xattr(struct ceph_inode_xattr *xattr)
706 if (xattr->should_free_name)
708 if (xattr->should_free_val)
714 static int __remove_xattr(struct ceph_inode_info *ci,
715 struct ceph_inode_xattr *xattr)
720 rb_erase(&xattr->node, &ci->i_xattrs.index);
722 if (xattr->should_free_name)
724 if (xattr->should_free_val)
727 ci->i_xattrs.names_size -= xattr->name_len;
728 ci->i_xattrs.vals_size -= xattr->val_len;
729 ci->i_xattrs.count--;
735 static char *__copy_xattr_names(struct ceph_inode_info *ci,
739 struct ceph_inode_xattr *xattr = NULL;
741 p = rb_first(&ci->i_xattrs.index);
742 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
745 xattr = rb_entry(p, struct ceph_inode_xattr, node);
746 memcpy(dest, xattr->name, xattr->name_len);
747 dest[xattr->name_len] = '\0';
749 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
750 xattr->name_len, ci->i_xattrs.names_size);
752 dest += xattr->name_len + 1;
759 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
761 struct rb_node *p, *tmp;
762 struct ceph_inode_xattr *xattr = NULL;
764 p = rb_first(&ci->i_xattrs.index);
766 dout("__ceph_destroy_xattrs p=%p\n", p);
769 xattr = rb_entry(p, struct ceph_inode_xattr, node);
772 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
773 xattr->name_len, xattr->name);
774 rb_erase(tmp, &ci->i_xattrs.index);
779 ci->i_xattrs.names_size = 0;
780 ci->i_xattrs.vals_size = 0;
781 ci->i_xattrs.index_version = 0;
782 ci->i_xattrs.count = 0;
783 ci->i_xattrs.index = RB_ROOT;
786 static int __build_xattrs(struct inode *inode)
787 __releases(ci->i_ceph_lock)
788 __acquires(ci->i_ceph_lock)
794 const char *name, *val;
795 struct ceph_inode_info *ci = ceph_inode(inode);
797 struct ceph_inode_xattr **xattrs = NULL;
801 dout("__build_xattrs() len=%d\n",
802 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
804 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
805 return 0; /* already built */
807 __ceph_destroy_xattrs(ci);
810 /* updated internal xattr rb tree */
811 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
812 p = ci->i_xattrs.blob->vec.iov_base;
813 end = p + ci->i_xattrs.blob->vec.iov_len;
814 ceph_decode_32_safe(&p, end, numattr, bad);
815 xattr_version = ci->i_xattrs.version;
816 spin_unlock(&ci->i_ceph_lock);
818 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
824 for (i = 0; i < numattr; i++) {
825 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
831 spin_lock(&ci->i_ceph_lock);
832 if (ci->i_xattrs.version != xattr_version) {
833 /* lost a race, retry */
834 for (i = 0; i < numattr; i++)
842 ceph_decode_32_safe(&p, end, len, bad);
846 ceph_decode_32_safe(&p, end, len, bad);
850 err = __set_xattr(ci, name, namelen, val, len,
851 0, 0, &xattrs[numattr]);
858 ci->i_xattrs.index_version = ci->i_xattrs.version;
859 ci->i_xattrs.dirty = false;
863 spin_lock(&ci->i_ceph_lock);
866 for (i = 0; i < numattr; i++)
870 ci->i_xattrs.names_size = 0;
874 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
878 * 4 bytes for the length, and additional 4 bytes per each xattr name,
879 * 4 bytes per each value
881 int size = 4 + ci->i_xattrs.count*(4 + 4) +
882 ci->i_xattrs.names_size +
883 ci->i_xattrs.vals_size;
884 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
885 ci->i_xattrs.count, ci->i_xattrs.names_size,
886 ci->i_xattrs.vals_size);
889 size += 4 + 4 + name_size + val_size;
895 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
896 * and swap into place. It returns the old i_xattrs.blob (or NULL) so
897 * that it can be freed by the caller as the i_ceph_lock is likely to be
900 struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
903 struct ceph_inode_xattr *xattr = NULL;
904 struct ceph_buffer *old_blob = NULL;
907 dout("__build_xattrs_blob %p\n", &ci->netfs.inode);
908 if (ci->i_xattrs.dirty) {
909 int need = __get_required_blob_size(ci, 0, 0);
911 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
913 p = rb_first(&ci->i_xattrs.index);
914 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
916 ceph_encode_32(&dest, ci->i_xattrs.count);
918 xattr = rb_entry(p, struct ceph_inode_xattr, node);
920 ceph_encode_32(&dest, xattr->name_len);
921 memcpy(dest, xattr->name, xattr->name_len);
922 dest += xattr->name_len;
923 ceph_encode_32(&dest, xattr->val_len);
924 memcpy(dest, xattr->val, xattr->val_len);
925 dest += xattr->val_len;
930 /* adjust buffer len; it may be larger than we need */
931 ci->i_xattrs.prealloc_blob->vec.iov_len =
932 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
934 if (ci->i_xattrs.blob)
935 old_blob = ci->i_xattrs.blob;
936 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
937 ci->i_xattrs.prealloc_blob = NULL;
938 ci->i_xattrs.dirty = false;
939 ci->i_xattrs.version++;
945 static inline int __get_request_mask(struct inode *in) {
946 struct ceph_mds_request *req = current->journal_info;
948 if (req && req->r_target_inode == in) {
949 if (req->r_op == CEPH_MDS_OP_LOOKUP ||
950 req->r_op == CEPH_MDS_OP_LOOKUPINO ||
951 req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
952 req->r_op == CEPH_MDS_OP_GETATTR) {
953 mask = le32_to_cpu(req->r_args.getattr.mask);
954 } else if (req->r_op == CEPH_MDS_OP_OPEN ||
955 req->r_op == CEPH_MDS_OP_CREATE) {
956 mask = le32_to_cpu(req->r_args.open.mask);
962 ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
965 struct ceph_inode_info *ci = ceph_inode(inode);
966 struct ceph_inode_xattr *xattr;
967 struct ceph_vxattr *vxattr;
971 if (strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
972 goto handle_non_vxattrs;
974 /* let's see if a virtual xattr was requested */
975 vxattr = ceph_match_vxattr(inode, name);
978 if (vxattr->flags & VXATTR_FLAG_RSTAT)
979 mask |= CEPH_STAT_RSTAT;
980 if (vxattr->flags & VXATTR_FLAG_DIRSTAT)
981 mask |= CEPH_CAP_FILE_SHARED;
982 err = ceph_do_getattr(inode, mask, true);
986 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
987 err = vxattr->getxattr_cb(ci, value, size);
988 if (size && size < err)
993 err = ceph_do_getvxattr(inode, name, value, size);
994 /* this would happen with a new client and old server combo */
995 if (err == -EOPNOTSUPP)
1000 req_mask = __get_request_mask(inode);
1002 spin_lock(&ci->i_ceph_lock);
1003 dout("getxattr %p name '%s' ver=%lld index_ver=%lld\n", inode, name,
1004 ci->i_xattrs.version, ci->i_xattrs.index_version);
1006 if (ci->i_xattrs.version == 0 ||
1007 !((req_mask & CEPH_CAP_XATTR_SHARED) ||
1008 __ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1))) {
1009 spin_unlock(&ci->i_ceph_lock);
1011 /* security module gets xattr while filling trace */
1012 if (current->journal_info) {
1013 pr_warn_ratelimited("sync getxattr %p "
1014 "during filling trace\n", inode);
1018 /* get xattrs from mds (if we don't already have them) */
1019 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
1022 spin_lock(&ci->i_ceph_lock);
1025 err = __build_xattrs(inode);
1029 err = -ENODATA; /* == ENOATTR */
1030 xattr = __get_xattr(ci, name);
1035 if (size && size < xattr->val_len)
1038 err = xattr->val_len;
1042 memcpy(value, xattr->val, xattr->val_len);
1044 if (current->journal_info &&
1045 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
1046 security_ismaclabel(name + XATTR_SECURITY_PREFIX_LEN))
1047 ci->i_ceph_flags |= CEPH_I_SEC_INITED;
1049 spin_unlock(&ci->i_ceph_lock);
1053 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
1055 struct inode *inode = d_inode(dentry);
1056 struct ceph_inode_info *ci = ceph_inode(inode);
1057 bool len_only = (size == 0);
1061 spin_lock(&ci->i_ceph_lock);
1062 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
1063 ci->i_xattrs.version, ci->i_xattrs.index_version);
1065 if (ci->i_xattrs.version == 0 ||
1066 !__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1)) {
1067 spin_unlock(&ci->i_ceph_lock);
1068 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
1071 spin_lock(&ci->i_ceph_lock);
1074 err = __build_xattrs(inode);
1078 /* add 1 byte for each xattr due to the null termination */
1079 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
1081 if (namelen > size) {
1085 names = __copy_xattr_names(ci, names);
1090 spin_unlock(&ci->i_ceph_lock);
1094 static int ceph_sync_setxattr(struct inode *inode, const char *name,
1095 const char *value, size_t size, int flags)
1097 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
1098 struct ceph_inode_info *ci = ceph_inode(inode);
1099 struct ceph_mds_request *req;
1100 struct ceph_mds_client *mdsc = fsc->mdsc;
1101 struct ceph_osd_client *osdc = &fsc->client->osdc;
1102 struct ceph_pagelist *pagelist = NULL;
1103 int op = CEPH_MDS_OP_SETXATTR;
1107 /* copy value into pagelist */
1108 pagelist = ceph_pagelist_alloc(GFP_NOFS);
1112 err = ceph_pagelist_append(pagelist, value, size);
1115 } else if (!value) {
1116 if (flags & CEPH_XATTR_REPLACE)
1117 op = CEPH_MDS_OP_RMXATTR;
1119 flags |= CEPH_XATTR_REMOVE;
1122 dout("setxattr value size: %zu\n", size);
1125 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1131 req->r_path2 = kstrdup(name, GFP_NOFS);
1132 if (!req->r_path2) {
1133 ceph_mdsc_put_request(req);
1138 if (op == CEPH_MDS_OP_SETXATTR) {
1139 req->r_args.setxattr.flags = cpu_to_le32(flags);
1140 req->r_args.setxattr.osdmap_epoch =
1141 cpu_to_le32(osdc->osdmap->epoch);
1142 req->r_pagelist = pagelist;
1146 req->r_inode = inode;
1148 req->r_num_caps = 1;
1149 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1151 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
1152 err = ceph_mdsc_do_request(mdsc, NULL, req);
1153 ceph_mdsc_put_request(req);
1154 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
1158 ceph_pagelist_release(pagelist);
1162 int __ceph_setxattr(struct inode *inode, const char *name,
1163 const void *value, size_t size, int flags)
1165 struct ceph_vxattr *vxattr;
1166 struct ceph_inode_info *ci = ceph_inode(inode);
1167 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1168 struct ceph_cap_flush *prealloc_cf = NULL;
1169 struct ceph_buffer *old_blob = NULL;
1173 int name_len = strlen(name);
1175 char *newname = NULL;
1176 char *newval = NULL;
1177 struct ceph_inode_xattr *xattr = NULL;
1178 int required_blob_size;
1179 bool check_realm = false;
1180 bool lock_snap_rwsem = false;
1182 if (ceph_snap(inode) != CEPH_NOSNAP)
1185 vxattr = ceph_match_vxattr(inode, name);
1187 if (vxattr->flags & VXATTR_FLAG_READONLY)
1189 if (value && !strncmp(vxattr->name, "ceph.quota", 10))
1193 /* pass any unhandled ceph.* xattrs through to the MDS */
1194 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1195 goto do_sync_unlocked;
1197 /* preallocate memory for xattr name, value, index node */
1199 newname = kmemdup(name, name_len + 1, GFP_NOFS);
1204 newval = kmemdup(value, val_len, GFP_NOFS);
1209 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1213 prealloc_cf = ceph_alloc_cap_flush();
1217 spin_lock(&ci->i_ceph_lock);
1219 issued = __ceph_caps_issued(ci, NULL);
1220 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1221 if ((ci->i_xattrs.version == 0) || !(issued & CEPH_CAP_XATTR_EXCL) ||
1222 (required_blob_size > mdsc->mdsmap->m_max_xattr_size)) {
1223 dout("%s do sync setxattr: version: %llu size: %d max: %llu\n",
1224 __func__, ci->i_xattrs.version, required_blob_size,
1225 mdsc->mdsmap->m_max_xattr_size);
1229 if (!lock_snap_rwsem && !ci->i_head_snapc) {
1230 lock_snap_rwsem = true;
1231 if (!down_read_trylock(&mdsc->snap_rwsem)) {
1232 spin_unlock(&ci->i_ceph_lock);
1233 down_read(&mdsc->snap_rwsem);
1234 spin_lock(&ci->i_ceph_lock);
1239 dout("setxattr %p name '%s' issued %s\n", inode, name,
1240 ceph_cap_string(issued));
1241 __build_xattrs(inode);
1243 if (!ci->i_xattrs.prealloc_blob ||
1244 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
1245 struct ceph_buffer *blob;
1247 spin_unlock(&ci->i_ceph_lock);
1248 ceph_buffer_put(old_blob); /* Shouldn't be required */
1249 dout(" pre-allocating new blob size=%d\n", required_blob_size);
1250 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
1252 goto do_sync_unlocked;
1253 spin_lock(&ci->i_ceph_lock);
1254 /* prealloc_blob can't be released while holding i_ceph_lock */
1255 if (ci->i_xattrs.prealloc_blob)
1256 old_blob = ci->i_xattrs.prealloc_blob;
1257 ci->i_xattrs.prealloc_blob = blob;
1261 err = __set_xattr(ci, newname, name_len, newval, val_len,
1262 flags, value ? 1 : -1, &xattr);
1265 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1267 ci->i_xattrs.dirty = true;
1268 inode_set_ctime_current(inode);
1271 spin_unlock(&ci->i_ceph_lock);
1272 ceph_buffer_put(old_blob);
1273 if (lock_snap_rwsem)
1274 up_read(&mdsc->snap_rwsem);
1276 __mark_inode_dirty(inode, dirty);
1277 ceph_free_cap_flush(prealloc_cf);
1281 spin_unlock(&ci->i_ceph_lock);
1283 if (lock_snap_rwsem)
1284 up_read(&mdsc->snap_rwsem);
1286 /* security module set xattr while filling trace */
1287 if (current->journal_info) {
1288 pr_warn_ratelimited("sync setxattr %p "
1289 "during filling trace\n", inode);
1292 err = ceph_sync_setxattr(inode, name, value, size, flags);
1293 if (err >= 0 && check_realm) {
1294 /* check if snaprealm was created for quota inode */
1295 spin_lock(&ci->i_ceph_lock);
1296 if ((ci->i_max_files || ci->i_max_bytes) &&
1297 !(ci->i_snap_realm &&
1298 ci->i_snap_realm->ino == ci->i_vino.ino))
1300 spin_unlock(&ci->i_ceph_lock);
1304 ceph_free_cap_flush(prealloc_cf);
1311 static int ceph_get_xattr_handler(const struct xattr_handler *handler,
1312 struct dentry *dentry, struct inode *inode,
1313 const char *name, void *value, size_t size)
1315 if (!ceph_is_valid_xattr(name))
1317 return __ceph_getxattr(inode, name, value, size);
1320 static int ceph_set_xattr_handler(const struct xattr_handler *handler,
1321 struct mnt_idmap *idmap,
1322 struct dentry *unused, struct inode *inode,
1323 const char *name, const void *value,
1324 size_t size, int flags)
1326 if (!ceph_is_valid_xattr(name))
1328 return __ceph_setxattr(inode, name, value, size, flags);
1331 static const struct xattr_handler ceph_other_xattr_handler = {
1332 .prefix = "", /* match any name => handlers called with full name */
1333 .get = ceph_get_xattr_handler,
1334 .set = ceph_set_xattr_handler,
1337 #ifdef CONFIG_SECURITY
1338 bool ceph_security_xattr_wanted(struct inode *in)
1340 return in->i_security != NULL;
1343 bool ceph_security_xattr_deadlock(struct inode *in)
1345 struct ceph_inode_info *ci;
1347 if (!in->i_security)
1349 ci = ceph_inode(in);
1350 spin_lock(&ci->i_ceph_lock);
1351 ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1352 !(ci->i_xattrs.version > 0 &&
1353 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1354 spin_unlock(&ci->i_ceph_lock);
1358 #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1359 int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
1360 struct ceph_acl_sec_ctx *as_ctx)
1362 struct ceph_pagelist *pagelist = as_ctx->pagelist;
1367 err = security_dentry_init_security(dentry, mode, &dentry->d_name,
1368 &name, &as_ctx->sec_ctx,
1369 &as_ctx->sec_ctxlen);
1371 WARN_ON_ONCE(err != -EOPNOTSUPP);
1372 err = 0; /* do nothing */
1378 pagelist = ceph_pagelist_alloc(GFP_KERNEL);
1381 err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
1384 ceph_pagelist_encode_32(pagelist, 1);
1388 * FIXME: Make security_dentry_init_security() generic. Currently
1389 * It only supports single security module and only selinux has
1390 * dentry_init_security hook.
1392 name_len = strlen(name);
1393 err = ceph_pagelist_reserve(pagelist,
1394 4 * 2 + name_len + as_ctx->sec_ctxlen);
1398 if (as_ctx->pagelist) {
1399 /* update count of KV pairs */
1400 BUG_ON(pagelist->length <= sizeof(__le32));
1401 if (list_is_singular(&pagelist->head)) {
1402 le32_add_cpu((__le32*)pagelist->mapped_tail, 1);
1404 struct page *page = list_first_entry(&pagelist->head,
1406 void *addr = kmap_atomic(page);
1407 le32_add_cpu((__le32*)addr, 1);
1408 kunmap_atomic(addr);
1411 as_ctx->pagelist = pagelist;
1414 ceph_pagelist_encode_32(pagelist, name_len);
1415 ceph_pagelist_append(pagelist, name, name_len);
1417 ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen);
1418 ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1422 if (pagelist && !as_ctx->pagelist)
1423 ceph_pagelist_release(pagelist);
1426 #endif /* CONFIG_CEPH_FS_SECURITY_LABEL */
1427 #endif /* CONFIG_SECURITY */
1429 void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
1431 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1432 posix_acl_release(as_ctx->acl);
1433 posix_acl_release(as_ctx->default_acl);
1435 #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1436 security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1438 #ifdef CONFIG_FS_ENCRYPTION
1439 kfree(as_ctx->fscrypt_auth);
1441 if (as_ctx->pagelist)
1442 ceph_pagelist_release(as_ctx->pagelist);
1446 * List of handlers for synthetic system.* attributes. Other
1447 * attributes are handled directly.
1449 const struct xattr_handler *ceph_xattr_handlers[] = {
1450 &ceph_other_xattr_handler,