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/posix_acl_xattr.h>
12 #include <linux/slab.h>
14 #define XATTR_CEPH_PREFIX "ceph."
15 #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
17 static int __remove_xattr(struct ceph_inode_info *ci,
18 struct ceph_inode_xattr *xattr);
20 static const struct xattr_handler ceph_other_xattr_handler;
23 * List of handlers for synthetic system.* attributes. Other
24 * attributes are handled directly.
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,
31 &ceph_other_xattr_handler,
35 static bool ceph_is_valid_xattr(const char *name)
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);
45 * These define virtual xattrs exposing the recursive directory
46 * statistics and layout metadata.
50 size_t name_size; /* strlen(name) + 1 (for '\0') */
51 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
53 bool readonly, hidden;
54 bool (*exists_cb)(struct ceph_inode_info *ci);
59 static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
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);
67 static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
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=";
77 size_t len, total_len = 0;
80 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
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);
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);
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);
100 total_len += strlen(ns_field) + pool_ns->len;
103 if (size >= total_len) {
104 memcpy(val, buf, len);
107 len = strlen(pool_name);
108 memcpy(val + ret, pool_name, len);
112 len = strlen(ns_field);
113 memcpy(val + ret, ns_field, len);
115 memcpy(val + ret, pool_ns->str, pool_ns->len);
119 up_read(&osdc->lock);
120 ceph_put_string(pool_ns);
124 static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
125 char *val, size_t size)
127 return snprintf(val, size, "%u", ci->i_layout.stripe_unit);
130 static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
131 char *val, size_t size)
133 return snprintf(val, size, "%u", ci->i_layout.stripe_count);
136 static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
137 char *val, size_t size)
139 return snprintf(val, size, "%u", ci->i_layout.object_size);
142 static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
143 char *val, size_t size)
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;
151 down_read(&osdc->lock);
152 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
154 ret = snprintf(val, size, "%s", pool_name);
156 ret = snprintf(val, size, "%lld", (unsigned long long)pool);
157 up_read(&osdc->lock);
161 static size_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
162 char *val, size_t size)
165 struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
167 ret = snprintf(val, size, "%.*s", (int)ns->len, ns->str);
175 static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
178 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
181 static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
184 return snprintf(val, size, "%lld", ci->i_files);
187 static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
190 return snprintf(val, size, "%lld", ci->i_subdirs);
193 static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
196 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
199 static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
202 return snprintf(val, size, "%lld", ci->i_rfiles);
205 static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
208 return snprintf(val, size, "%lld", ci->i_rsubdirs);
211 static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
214 return snprintf(val, size, "%lld", ci->i_rbytes);
217 static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
220 return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
221 (long)ci->i_rctime.tv_nsec);
225 #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
226 #define CEPH_XATTR_NAME2(_type, _name, _name2) \
227 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
229 #define XATTR_NAME_CEPH(_type, _name) \
231 .name = CEPH_XATTR_NAME(_type, _name), \
232 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
233 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
238 #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
240 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
241 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
242 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
245 .exists_cb = ceph_vxattrcb_layout_exists, \
248 static struct ceph_vxattr ceph_dir_vxattrs[] = {
250 .name = "ceph.dir.layout",
251 .name_size = sizeof("ceph.dir.layout"),
252 .getxattr_cb = ceph_vxattrcb_layout,
255 .exists_cb = ceph_vxattrcb_layout_exists,
257 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
258 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
259 XATTR_LAYOUT_FIELD(dir, layout, object_size),
260 XATTR_LAYOUT_FIELD(dir, layout, pool),
261 XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
262 XATTR_NAME_CEPH(dir, entries),
263 XATTR_NAME_CEPH(dir, files),
264 XATTR_NAME_CEPH(dir, subdirs),
265 XATTR_NAME_CEPH(dir, rentries),
266 XATTR_NAME_CEPH(dir, rfiles),
267 XATTR_NAME_CEPH(dir, rsubdirs),
268 XATTR_NAME_CEPH(dir, rbytes),
269 XATTR_NAME_CEPH(dir, rctime),
270 { .name = NULL, 0 } /* Required table terminator */
272 static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
276 static struct ceph_vxattr ceph_file_vxattrs[] = {
278 .name = "ceph.file.layout",
279 .name_size = sizeof("ceph.file.layout"),
280 .getxattr_cb = ceph_vxattrcb_layout,
283 .exists_cb = ceph_vxattrcb_layout_exists,
285 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
286 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
287 XATTR_LAYOUT_FIELD(file, layout, object_size),
288 XATTR_LAYOUT_FIELD(file, layout, pool),
289 XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
290 { .name = NULL, 0 } /* Required table terminator */
292 static size_t ceph_file_vxattrs_name_size; /* total size of all names */
294 static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
296 if (S_ISDIR(inode->i_mode))
297 return ceph_dir_vxattrs;
298 else if (S_ISREG(inode->i_mode))
299 return ceph_file_vxattrs;
303 static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
305 if (vxattrs == ceph_dir_vxattrs)
306 return ceph_dir_vxattrs_name_size;
307 if (vxattrs == ceph_file_vxattrs)
308 return ceph_file_vxattrs_name_size;
314 * Compute the aggregate size (including terminating '\0') of all
315 * virtual extended attribute names in the given vxattr table.
317 static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
319 struct ceph_vxattr *vxattr;
322 for (vxattr = vxattrs; vxattr->name; vxattr++)
324 size += vxattr->name_size;
329 /* Routines called at initialization and exit time */
331 void __init ceph_xattr_init(void)
333 ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
334 ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
337 void ceph_xattr_exit(void)
339 ceph_dir_vxattrs_name_size = 0;
340 ceph_file_vxattrs_name_size = 0;
343 static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
346 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
349 while (vxattr->name) {
350 if (!strcmp(vxattr->name, name))
359 static int __set_xattr(struct ceph_inode_info *ci,
360 const char *name, int name_len,
361 const char *val, int val_len,
362 int flags, int update_xattr,
363 struct ceph_inode_xattr **newxattr)
366 struct rb_node *parent = NULL;
367 struct ceph_inode_xattr *xattr = NULL;
371 p = &ci->i_xattrs.index.rb_node;
374 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
375 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
381 if (name_len == xattr->name_len)
383 else if (name_len < xattr->name_len)
394 if (xattr && (flags & XATTR_CREATE))
396 else if (!xattr && (flags & XATTR_REPLACE))
404 if (update_xattr < 0) {
406 __remove_xattr(ci, xattr);
417 xattr->name_len = name_len;
418 xattr->should_free_name = update_xattr;
420 ci->i_xattrs.count++;
421 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
425 if (xattr->should_free_val)
426 kfree((void *)xattr->val);
432 ci->i_xattrs.names_size -= xattr->name_len;
433 ci->i_xattrs.vals_size -= xattr->val_len;
435 ci->i_xattrs.names_size += name_len;
436 ci->i_xattrs.vals_size += val_len;
442 xattr->val_len = val_len;
443 xattr->dirty = update_xattr;
444 xattr->should_free_val = (val && update_xattr);
447 rb_link_node(&xattr->node, parent, p);
448 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
449 dout("__set_xattr_val p=%p\n", p);
452 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
453 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
458 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
462 struct rb_node *parent = NULL;
463 struct ceph_inode_xattr *xattr = NULL;
464 int name_len = strlen(name);
467 p = &ci->i_xattrs.index.rb_node;
470 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
471 c = strncmp(name, xattr->name, xattr->name_len);
472 if (c == 0 && name_len > xattr->name_len)
479 dout("__get_xattr %s: found %.*s\n", name,
480 xattr->val_len, xattr->val);
485 dout("__get_xattr %s: not found\n", name);
490 static void __free_xattr(struct ceph_inode_xattr *xattr)
494 if (xattr->should_free_name)
495 kfree((void *)xattr->name);
496 if (xattr->should_free_val)
497 kfree((void *)xattr->val);
502 static int __remove_xattr(struct ceph_inode_info *ci,
503 struct ceph_inode_xattr *xattr)
508 rb_erase(&xattr->node, &ci->i_xattrs.index);
510 if (xattr->should_free_name)
511 kfree((void *)xattr->name);
512 if (xattr->should_free_val)
513 kfree((void *)xattr->val);
515 ci->i_xattrs.names_size -= xattr->name_len;
516 ci->i_xattrs.vals_size -= xattr->val_len;
517 ci->i_xattrs.count--;
523 static char *__copy_xattr_names(struct ceph_inode_info *ci,
527 struct ceph_inode_xattr *xattr = NULL;
529 p = rb_first(&ci->i_xattrs.index);
530 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
533 xattr = rb_entry(p, struct ceph_inode_xattr, node);
534 memcpy(dest, xattr->name, xattr->name_len);
535 dest[xattr->name_len] = '\0';
537 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
538 xattr->name_len, ci->i_xattrs.names_size);
540 dest += xattr->name_len + 1;
547 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
549 struct rb_node *p, *tmp;
550 struct ceph_inode_xattr *xattr = NULL;
552 p = rb_first(&ci->i_xattrs.index);
554 dout("__ceph_destroy_xattrs p=%p\n", p);
557 xattr = rb_entry(p, struct ceph_inode_xattr, node);
560 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
561 xattr->name_len, xattr->name);
562 rb_erase(tmp, &ci->i_xattrs.index);
567 ci->i_xattrs.names_size = 0;
568 ci->i_xattrs.vals_size = 0;
569 ci->i_xattrs.index_version = 0;
570 ci->i_xattrs.count = 0;
571 ci->i_xattrs.index = RB_ROOT;
574 static int __build_xattrs(struct inode *inode)
575 __releases(ci->i_ceph_lock)
576 __acquires(ci->i_ceph_lock)
582 const char *name, *val;
583 struct ceph_inode_info *ci = ceph_inode(inode);
585 struct ceph_inode_xattr **xattrs = NULL;
589 dout("__build_xattrs() len=%d\n",
590 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
592 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
593 return 0; /* already built */
595 __ceph_destroy_xattrs(ci);
598 /* updated internal xattr rb tree */
599 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
600 p = ci->i_xattrs.blob->vec.iov_base;
601 end = p + ci->i_xattrs.blob->vec.iov_len;
602 ceph_decode_32_safe(&p, end, numattr, bad);
603 xattr_version = ci->i_xattrs.version;
604 spin_unlock(&ci->i_ceph_lock);
606 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
612 for (i = 0; i < numattr; i++) {
613 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
619 spin_lock(&ci->i_ceph_lock);
620 if (ci->i_xattrs.version != xattr_version) {
621 /* lost a race, retry */
622 for (i = 0; i < numattr; i++)
630 ceph_decode_32_safe(&p, end, len, bad);
634 ceph_decode_32_safe(&p, end, len, bad);
638 err = __set_xattr(ci, name, namelen, val, len,
639 0, 0, &xattrs[numattr]);
646 ci->i_xattrs.index_version = ci->i_xattrs.version;
647 ci->i_xattrs.dirty = false;
651 spin_lock(&ci->i_ceph_lock);
654 for (i = 0; i < numattr; i++)
658 ci->i_xattrs.names_size = 0;
662 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
666 * 4 bytes for the length, and additional 4 bytes per each xattr name,
667 * 4 bytes per each value
669 int size = 4 + ci->i_xattrs.count*(4 + 4) +
670 ci->i_xattrs.names_size +
671 ci->i_xattrs.vals_size;
672 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
673 ci->i_xattrs.count, ci->i_xattrs.names_size,
674 ci->i_xattrs.vals_size);
677 size += 4 + 4 + name_size + val_size;
683 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
684 * and swap into place. It returns the old i_xattrs.blob (or NULL) so
685 * that it can be freed by the caller as the i_ceph_lock is likely to be
688 struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
691 struct ceph_inode_xattr *xattr = NULL;
692 struct ceph_buffer *old_blob = NULL;
695 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
696 if (ci->i_xattrs.dirty) {
697 int need = __get_required_blob_size(ci, 0, 0);
699 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
701 p = rb_first(&ci->i_xattrs.index);
702 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
704 ceph_encode_32(&dest, ci->i_xattrs.count);
706 xattr = rb_entry(p, struct ceph_inode_xattr, node);
708 ceph_encode_32(&dest, xattr->name_len);
709 memcpy(dest, xattr->name, xattr->name_len);
710 dest += xattr->name_len;
711 ceph_encode_32(&dest, xattr->val_len);
712 memcpy(dest, xattr->val, xattr->val_len);
713 dest += xattr->val_len;
718 /* adjust buffer len; it may be larger than we need */
719 ci->i_xattrs.prealloc_blob->vec.iov_len =
720 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
722 if (ci->i_xattrs.blob)
723 old_blob = ci->i_xattrs.blob;
724 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
725 ci->i_xattrs.prealloc_blob = NULL;
726 ci->i_xattrs.dirty = false;
727 ci->i_xattrs.version++;
733 static inline int __get_request_mask(struct inode *in) {
734 struct ceph_mds_request *req = current->journal_info;
736 if (req && req->r_target_inode == in) {
737 if (req->r_op == CEPH_MDS_OP_LOOKUP ||
738 req->r_op == CEPH_MDS_OP_LOOKUPINO ||
739 req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
740 req->r_op == CEPH_MDS_OP_GETATTR) {
741 mask = le32_to_cpu(req->r_args.getattr.mask);
742 } else if (req->r_op == CEPH_MDS_OP_OPEN ||
743 req->r_op == CEPH_MDS_OP_CREATE) {
744 mask = le32_to_cpu(req->r_args.open.mask);
750 ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
753 struct ceph_inode_info *ci = ceph_inode(inode);
754 struct ceph_inode_xattr *xattr;
755 struct ceph_vxattr *vxattr = NULL;
759 /* let's see if a virtual xattr was requested */
760 vxattr = ceph_match_vxattr(inode, name);
762 err = ceph_do_getattr(inode, 0, true);
766 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
767 err = vxattr->getxattr_cb(ci, value, size);
768 if (size && size < err)
774 req_mask = __get_request_mask(inode);
776 spin_lock(&ci->i_ceph_lock);
777 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
778 ci->i_xattrs.version, ci->i_xattrs.index_version);
780 if (ci->i_xattrs.version == 0 ||
781 !((req_mask & CEPH_CAP_XATTR_SHARED) ||
782 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
783 spin_unlock(&ci->i_ceph_lock);
785 /* security module gets xattr while filling trace */
786 if (current->journal_info) {
787 pr_warn_ratelimited("sync getxattr %p "
788 "during filling trace\n", inode);
792 /* get xattrs from mds (if we don't already have them) */
793 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
796 spin_lock(&ci->i_ceph_lock);
799 err = __build_xattrs(inode);
803 err = -ENODATA; /* == ENOATTR */
804 xattr = __get_xattr(ci, name);
809 if (size && size < xattr->val_len)
812 err = xattr->val_len;
816 memcpy(value, xattr->val, xattr->val_len);
818 if (current->journal_info &&
819 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
820 ci->i_ceph_flags |= CEPH_I_SEC_INITED;
822 spin_unlock(&ci->i_ceph_lock);
826 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
828 struct inode *inode = d_inode(dentry);
829 struct ceph_inode_info *ci = ceph_inode(inode);
830 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
837 spin_lock(&ci->i_ceph_lock);
838 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
839 ci->i_xattrs.version, ci->i_xattrs.index_version);
841 if (ci->i_xattrs.version == 0 ||
842 !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
843 spin_unlock(&ci->i_ceph_lock);
844 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
847 spin_lock(&ci->i_ceph_lock);
850 err = __build_xattrs(inode);
854 * Start with virtual dir xattr names (if any) (including
855 * terminating '\0' characters for each).
857 vir_namelen = ceph_vxattrs_name_size(vxattrs);
859 /* adding 1 byte per each variable due to the null termination */
860 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
862 if (size && vir_namelen + namelen > size)
865 err = namelen + vir_namelen;
869 names = __copy_xattr_names(ci, names);
871 /* virtual xattr names, too */
874 for (i = 0; vxattrs[i].name; i++) {
875 if (!vxattrs[i].hidden &&
876 !(vxattrs[i].exists_cb &&
877 !vxattrs[i].exists_cb(ci))) {
878 len = sprintf(names, "%s", vxattrs[i].name);
886 spin_unlock(&ci->i_ceph_lock);
890 static int ceph_sync_setxattr(struct inode *inode, const char *name,
891 const char *value, size_t size, int flags)
893 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
894 struct ceph_inode_info *ci = ceph_inode(inode);
895 struct ceph_mds_request *req;
896 struct ceph_mds_client *mdsc = fsc->mdsc;
897 struct ceph_pagelist *pagelist = NULL;
898 int op = CEPH_MDS_OP_SETXATTR;
902 /* copy value into pagelist */
903 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
907 ceph_pagelist_init(pagelist);
908 err = ceph_pagelist_append(pagelist, value, size);
912 if (flags & CEPH_XATTR_REPLACE)
913 op = CEPH_MDS_OP_RMXATTR;
915 flags |= CEPH_XATTR_REMOVE;
918 dout("setxattr value=%.*s\n", (int)size, value);
921 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
927 req->r_path2 = kstrdup(name, GFP_NOFS);
929 ceph_mdsc_put_request(req);
934 if (op == CEPH_MDS_OP_SETXATTR) {
935 req->r_args.setxattr.flags = cpu_to_le32(flags);
936 req->r_pagelist = pagelist;
940 req->r_inode = inode;
943 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
945 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
946 err = ceph_mdsc_do_request(mdsc, NULL, req);
947 ceph_mdsc_put_request(req);
948 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
952 ceph_pagelist_release(pagelist);
956 int __ceph_setxattr(struct inode *inode, const char *name,
957 const void *value, size_t size, int flags)
959 struct ceph_vxattr *vxattr;
960 struct ceph_inode_info *ci = ceph_inode(inode);
961 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
962 struct ceph_cap_flush *prealloc_cf = NULL;
963 struct ceph_buffer *old_blob = NULL;
967 int name_len = strlen(name);
969 char *newname = NULL;
971 struct ceph_inode_xattr *xattr = NULL;
972 int required_blob_size;
973 bool lock_snap_rwsem = false;
975 if (ceph_snap(inode) != CEPH_NOSNAP)
978 vxattr = ceph_match_vxattr(inode, name);
979 if (vxattr && vxattr->readonly)
982 /* pass any unhandled ceph.* xattrs through to the MDS */
983 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
984 goto do_sync_unlocked;
986 /* preallocate memory for xattr name, value, index node */
988 newname = kmemdup(name, name_len + 1, GFP_NOFS);
993 newval = kmemdup(value, val_len, GFP_NOFS);
998 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1002 prealloc_cf = ceph_alloc_cap_flush();
1006 spin_lock(&ci->i_ceph_lock);
1008 issued = __ceph_caps_issued(ci, NULL);
1009 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
1012 if (!lock_snap_rwsem && !ci->i_head_snapc) {
1013 lock_snap_rwsem = true;
1014 if (!down_read_trylock(&mdsc->snap_rwsem)) {
1015 spin_unlock(&ci->i_ceph_lock);
1016 down_read(&mdsc->snap_rwsem);
1017 spin_lock(&ci->i_ceph_lock);
1022 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
1023 __build_xattrs(inode);
1025 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1027 if (!ci->i_xattrs.prealloc_blob ||
1028 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
1029 struct ceph_buffer *blob;
1031 spin_unlock(&ci->i_ceph_lock);
1032 ceph_buffer_put(old_blob); /* Shouldn't be required */
1033 dout(" pre-allocating new blob size=%d\n", required_blob_size);
1034 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
1036 goto do_sync_unlocked;
1037 spin_lock(&ci->i_ceph_lock);
1038 /* prealloc_blob can't be released while holding i_ceph_lock */
1039 if (ci->i_xattrs.prealloc_blob)
1040 old_blob = ci->i_xattrs.prealloc_blob;
1041 ci->i_xattrs.prealloc_blob = blob;
1045 err = __set_xattr(ci, newname, name_len, newval, val_len,
1046 flags, value ? 1 : -1, &xattr);
1049 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1051 ci->i_xattrs.dirty = true;
1052 inode->i_ctime = current_time(inode);
1055 spin_unlock(&ci->i_ceph_lock);
1056 ceph_buffer_put(old_blob);
1057 if (lock_snap_rwsem)
1058 up_read(&mdsc->snap_rwsem);
1060 __mark_inode_dirty(inode, dirty);
1061 ceph_free_cap_flush(prealloc_cf);
1065 spin_unlock(&ci->i_ceph_lock);
1067 if (lock_snap_rwsem)
1068 up_read(&mdsc->snap_rwsem);
1070 /* security module set xattr while filling trace */
1071 if (current->journal_info) {
1072 pr_warn_ratelimited("sync setxattr %p "
1073 "during filling trace\n", inode);
1076 err = ceph_sync_setxattr(inode, name, value, size, flags);
1079 ceph_free_cap_flush(prealloc_cf);
1086 static int ceph_get_xattr_handler(const struct xattr_handler *handler,
1087 struct dentry *dentry, struct inode *inode,
1088 const char *name, void *value, size_t size)
1090 if (!ceph_is_valid_xattr(name))
1092 return __ceph_getxattr(inode, name, value, size);
1095 static int ceph_set_xattr_handler(const struct xattr_handler *handler,
1096 struct dentry *unused, struct inode *inode,
1097 const char *name, const void *value,
1098 size_t size, int flags)
1100 if (!ceph_is_valid_xattr(name))
1102 return __ceph_setxattr(inode, name, value, size, flags);
1105 static const struct xattr_handler ceph_other_xattr_handler = {
1106 .prefix = "", /* match any name => handlers called with full name */
1107 .get = ceph_get_xattr_handler,
1108 .set = ceph_set_xattr_handler,
1111 #ifdef CONFIG_SECURITY
1112 bool ceph_security_xattr_wanted(struct inode *in)
1114 return in->i_security != NULL;
1117 bool ceph_security_xattr_deadlock(struct inode *in)
1119 struct ceph_inode_info *ci;
1121 if (!in->i_security)
1123 ci = ceph_inode(in);
1124 spin_lock(&ci->i_ceph_lock);
1125 ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1126 !(ci->i_xattrs.version > 0 &&
1127 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1128 spin_unlock(&ci->i_ceph_lock);