1 #include <linux/ceph/ceph_debug.h>
4 #include "mds_client.h"
6 #include <linux/ceph/decode.h>
8 #include <linux/xattr.h>
9 #include <linux/posix_acl_xattr.h>
10 #include <linux/slab.h>
12 #define XATTR_CEPH_PREFIX "ceph."
13 #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
15 static int __remove_xattr(struct ceph_inode_info *ci,
16 struct ceph_inode_xattr *xattr);
19 * List of handlers for synthetic system.* attributes. Other
20 * attributes are handled directly.
22 const struct xattr_handler *ceph_xattr_handlers[] = {
23 #ifdef CONFIG_CEPH_FS_POSIX_ACL
24 &posix_acl_access_xattr_handler,
25 &posix_acl_default_xattr_handler,
30 static bool ceph_is_valid_xattr(const char *name)
32 return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
33 !strncmp(name, XATTR_SECURITY_PREFIX,
34 XATTR_SECURITY_PREFIX_LEN) ||
35 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
36 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
37 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
41 * These define virtual xattrs exposing the recursive directory
42 * statistics and layout metadata.
46 size_t name_size; /* strlen(name) + 1 (for '\0') */
47 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
49 bool readonly, hidden;
50 bool (*exists_cb)(struct ceph_inode_info *ci);
55 static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
58 char *p = (char *)&ci->i_layout;
60 for (s = 0; s < sizeof(ci->i_layout); s++, p++)
66 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 s64 pool = ceph_file_layout_pg_pool(ci->i_layout);
73 const char *pool_name;
76 dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
77 down_read(&osdc->map_sem);
78 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
80 size_t len = strlen(pool_name);
81 ret = snprintf(buf, sizeof(buf),
82 "stripe_unit=%lld stripe_count=%lld object_size=%lld pool=",
83 (unsigned long long)ceph_file_layout_su(ci->i_layout),
84 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
85 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
88 } else if (ret + len > size) {
91 memcpy(val, buf, ret);
92 memcpy(val + ret, pool_name, len);
96 ret = snprintf(buf, sizeof(buf),
97 "stripe_unit=%lld stripe_count=%lld object_size=%lld pool=%lld",
98 (unsigned long long)ceph_file_layout_su(ci->i_layout),
99 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
100 (unsigned long long)ceph_file_layout_object_size(ci->i_layout),
101 (unsigned long long)pool);
104 memcpy(val, buf, ret);
109 up_read(&osdc->map_sem);
113 static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
114 char *val, size_t size)
116 return snprintf(val, size, "%lld",
117 (unsigned long long)ceph_file_layout_su(ci->i_layout));
120 static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
121 char *val, size_t size)
123 return snprintf(val, size, "%lld",
124 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout));
127 static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
128 char *val, size_t size)
130 return snprintf(val, size, "%lld",
131 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
134 static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
135 char *val, size_t size)
138 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
139 struct ceph_osd_client *osdc = &fsc->client->osdc;
140 s64 pool = ceph_file_layout_pg_pool(ci->i_layout);
141 const char *pool_name;
143 down_read(&osdc->map_sem);
144 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
146 ret = snprintf(val, size, "%s", pool_name);
148 ret = snprintf(val, size, "%lld", (unsigned long long)pool);
149 up_read(&osdc->map_sem);
155 static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
158 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
161 static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
164 return snprintf(val, size, "%lld", ci->i_files);
167 static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
170 return snprintf(val, size, "%lld", ci->i_subdirs);
173 static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
176 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
179 static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
182 return snprintf(val, size, "%lld", ci->i_rfiles);
185 static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
188 return snprintf(val, size, "%lld", ci->i_rsubdirs);
191 static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
194 return snprintf(val, size, "%lld", ci->i_rbytes);
197 static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
200 return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
201 (long)ci->i_rctime.tv_nsec);
205 #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
206 #define CEPH_XATTR_NAME2(_type, _name, _name2) \
207 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
209 #define XATTR_NAME_CEPH(_type, _name) \
211 .name = CEPH_XATTR_NAME(_type, _name), \
212 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
213 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
218 #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
220 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
221 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
222 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
225 .exists_cb = ceph_vxattrcb_layout_exists, \
228 static struct ceph_vxattr ceph_dir_vxattrs[] = {
230 .name = "ceph.dir.layout",
231 .name_size = sizeof("ceph.dir.layout"),
232 .getxattr_cb = ceph_vxattrcb_layout,
235 .exists_cb = ceph_vxattrcb_layout_exists,
237 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
238 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
239 XATTR_LAYOUT_FIELD(dir, layout, object_size),
240 XATTR_LAYOUT_FIELD(dir, layout, pool),
241 XATTR_NAME_CEPH(dir, entries),
242 XATTR_NAME_CEPH(dir, files),
243 XATTR_NAME_CEPH(dir, subdirs),
244 XATTR_NAME_CEPH(dir, rentries),
245 XATTR_NAME_CEPH(dir, rfiles),
246 XATTR_NAME_CEPH(dir, rsubdirs),
247 XATTR_NAME_CEPH(dir, rbytes),
248 XATTR_NAME_CEPH(dir, rctime),
249 { .name = NULL, 0 } /* Required table terminator */
251 static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
255 static struct ceph_vxattr ceph_file_vxattrs[] = {
257 .name = "ceph.file.layout",
258 .name_size = sizeof("ceph.file.layout"),
259 .getxattr_cb = ceph_vxattrcb_layout,
262 .exists_cb = ceph_vxattrcb_layout_exists,
264 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
265 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
266 XATTR_LAYOUT_FIELD(file, layout, object_size),
267 XATTR_LAYOUT_FIELD(file, layout, pool),
268 { .name = NULL, 0 } /* Required table terminator */
270 static size_t ceph_file_vxattrs_name_size; /* total size of all names */
272 static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
274 if (S_ISDIR(inode->i_mode))
275 return ceph_dir_vxattrs;
276 else if (S_ISREG(inode->i_mode))
277 return ceph_file_vxattrs;
281 static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
283 if (vxattrs == ceph_dir_vxattrs)
284 return ceph_dir_vxattrs_name_size;
285 if (vxattrs == ceph_file_vxattrs)
286 return ceph_file_vxattrs_name_size;
292 * Compute the aggregate size (including terminating '\0') of all
293 * virtual extended attribute names in the given vxattr table.
295 static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
297 struct ceph_vxattr *vxattr;
300 for (vxattr = vxattrs; vxattr->name; vxattr++)
302 size += vxattr->name_size;
307 /* Routines called at initialization and exit time */
309 void __init ceph_xattr_init(void)
311 ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
312 ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
315 void ceph_xattr_exit(void)
317 ceph_dir_vxattrs_name_size = 0;
318 ceph_file_vxattrs_name_size = 0;
321 static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
324 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
327 while (vxattr->name) {
328 if (!strcmp(vxattr->name, name))
337 static int __set_xattr(struct ceph_inode_info *ci,
338 const char *name, int name_len,
339 const char *val, int val_len,
340 int flags, int update_xattr,
341 struct ceph_inode_xattr **newxattr)
344 struct rb_node *parent = NULL;
345 struct ceph_inode_xattr *xattr = NULL;
349 p = &ci->i_xattrs.index.rb_node;
352 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
353 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
359 if (name_len == xattr->name_len)
361 else if (name_len < xattr->name_len)
371 if (xattr && (flags & XATTR_CREATE))
373 else if (!xattr && (flags & XATTR_REPLACE))
380 if (update_xattr < 0) {
382 __remove_xattr(ci, xattr);
392 xattr->name_len = name_len;
393 xattr->should_free_name = update_xattr;
395 ci->i_xattrs.count++;
396 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
400 if (xattr->should_free_val)
401 kfree((void *)xattr->val);
407 ci->i_xattrs.names_size -= xattr->name_len;
408 ci->i_xattrs.vals_size -= xattr->val_len;
410 ci->i_xattrs.names_size += name_len;
411 ci->i_xattrs.vals_size += val_len;
417 xattr->val_len = val_len;
418 xattr->dirty = update_xattr;
419 xattr->should_free_val = (val && update_xattr);
422 rb_link_node(&xattr->node, parent, p);
423 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
424 dout("__set_xattr_val p=%p\n", p);
427 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
428 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
433 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
437 struct rb_node *parent = NULL;
438 struct ceph_inode_xattr *xattr = NULL;
439 int name_len = strlen(name);
442 p = &ci->i_xattrs.index.rb_node;
445 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
446 c = strncmp(name, xattr->name, xattr->name_len);
447 if (c == 0 && name_len > xattr->name_len)
454 dout("__get_xattr %s: found %.*s\n", name,
455 xattr->val_len, xattr->val);
460 dout("__get_xattr %s: not found\n", name);
465 static void __free_xattr(struct ceph_inode_xattr *xattr)
469 if (xattr->should_free_name)
470 kfree((void *)xattr->name);
471 if (xattr->should_free_val)
472 kfree((void *)xattr->val);
477 static int __remove_xattr(struct ceph_inode_info *ci,
478 struct ceph_inode_xattr *xattr)
483 rb_erase(&xattr->node, &ci->i_xattrs.index);
485 if (xattr->should_free_name)
486 kfree((void *)xattr->name);
487 if (xattr->should_free_val)
488 kfree((void *)xattr->val);
490 ci->i_xattrs.names_size -= xattr->name_len;
491 ci->i_xattrs.vals_size -= xattr->val_len;
492 ci->i_xattrs.count--;
498 static int __remove_xattr_by_name(struct ceph_inode_info *ci,
502 struct ceph_inode_xattr *xattr;
505 p = &ci->i_xattrs.index.rb_node;
506 xattr = __get_xattr(ci, name);
507 err = __remove_xattr(ci, xattr);
511 static char *__copy_xattr_names(struct ceph_inode_info *ci,
515 struct ceph_inode_xattr *xattr = NULL;
517 p = rb_first(&ci->i_xattrs.index);
518 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
521 xattr = rb_entry(p, struct ceph_inode_xattr, node);
522 memcpy(dest, xattr->name, xattr->name_len);
523 dest[xattr->name_len] = '\0';
525 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
526 xattr->name_len, ci->i_xattrs.names_size);
528 dest += xattr->name_len + 1;
535 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
537 struct rb_node *p, *tmp;
538 struct ceph_inode_xattr *xattr = NULL;
540 p = rb_first(&ci->i_xattrs.index);
542 dout("__ceph_destroy_xattrs p=%p\n", p);
545 xattr = rb_entry(p, struct ceph_inode_xattr, node);
548 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
549 xattr->name_len, xattr->name);
550 rb_erase(tmp, &ci->i_xattrs.index);
555 ci->i_xattrs.names_size = 0;
556 ci->i_xattrs.vals_size = 0;
557 ci->i_xattrs.index_version = 0;
558 ci->i_xattrs.count = 0;
559 ci->i_xattrs.index = RB_ROOT;
562 static int __build_xattrs(struct inode *inode)
563 __releases(ci->i_ceph_lock)
564 __acquires(ci->i_ceph_lock)
570 const char *name, *val;
571 struct ceph_inode_info *ci = ceph_inode(inode);
573 struct ceph_inode_xattr **xattrs = NULL;
577 dout("__build_xattrs() len=%d\n",
578 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
580 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
581 return 0; /* already built */
583 __ceph_destroy_xattrs(ci);
586 /* updated internal xattr rb tree */
587 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
588 p = ci->i_xattrs.blob->vec.iov_base;
589 end = p + ci->i_xattrs.blob->vec.iov_len;
590 ceph_decode_32_safe(&p, end, numattr, bad);
591 xattr_version = ci->i_xattrs.version;
592 spin_unlock(&ci->i_ceph_lock);
594 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
600 for (i = 0; i < numattr; i++) {
601 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
607 spin_lock(&ci->i_ceph_lock);
608 if (ci->i_xattrs.version != xattr_version) {
609 /* lost a race, retry */
610 for (i = 0; i < numattr; i++)
618 ceph_decode_32_safe(&p, end, len, bad);
622 ceph_decode_32_safe(&p, end, len, bad);
626 err = __set_xattr(ci, name, namelen, val, len,
627 0, 0, &xattrs[numattr]);
634 ci->i_xattrs.index_version = ci->i_xattrs.version;
635 ci->i_xattrs.dirty = false;
639 spin_lock(&ci->i_ceph_lock);
642 for (i = 0; i < numattr; i++)
646 ci->i_xattrs.names_size = 0;
650 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
654 * 4 bytes for the length, and additional 4 bytes per each xattr name,
655 * 4 bytes per each value
657 int size = 4 + ci->i_xattrs.count*(4 + 4) +
658 ci->i_xattrs.names_size +
659 ci->i_xattrs.vals_size;
660 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
661 ci->i_xattrs.count, ci->i_xattrs.names_size,
662 ci->i_xattrs.vals_size);
665 size += 4 + 4 + name_size + val_size;
671 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
672 * and swap into place.
674 void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
677 struct ceph_inode_xattr *xattr = NULL;
680 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
681 if (ci->i_xattrs.dirty) {
682 int need = __get_required_blob_size(ci, 0, 0);
684 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
686 p = rb_first(&ci->i_xattrs.index);
687 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
689 ceph_encode_32(&dest, ci->i_xattrs.count);
691 xattr = rb_entry(p, struct ceph_inode_xattr, node);
693 ceph_encode_32(&dest, xattr->name_len);
694 memcpy(dest, xattr->name, xattr->name_len);
695 dest += xattr->name_len;
696 ceph_encode_32(&dest, xattr->val_len);
697 memcpy(dest, xattr->val, xattr->val_len);
698 dest += xattr->val_len;
703 /* adjust buffer len; it may be larger than we need */
704 ci->i_xattrs.prealloc_blob->vec.iov_len =
705 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
707 if (ci->i_xattrs.blob)
708 ceph_buffer_put(ci->i_xattrs.blob);
709 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
710 ci->i_xattrs.prealloc_blob = NULL;
711 ci->i_xattrs.dirty = false;
712 ci->i_xattrs.version++;
716 ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
719 struct ceph_inode_info *ci = ceph_inode(inode);
721 struct ceph_inode_xattr *xattr;
722 struct ceph_vxattr *vxattr = NULL;
724 if (!ceph_is_valid_xattr(name))
727 /* let's see if a virtual xattr was requested */
728 vxattr = ceph_match_vxattr(inode, name);
729 if (vxattr && !(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
730 err = vxattr->getxattr_cb(ci, value, size);
734 spin_lock(&ci->i_ceph_lock);
735 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
736 ci->i_xattrs.version, ci->i_xattrs.index_version);
738 if (ci->i_xattrs.version == 0 ||
739 !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
740 spin_unlock(&ci->i_ceph_lock);
741 /* get xattrs from mds (if we don't already have them) */
742 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
745 spin_lock(&ci->i_ceph_lock);
748 err = __build_xattrs(inode);
752 err = -ENODATA; /* == ENOATTR */
753 xattr = __get_xattr(ci, name);
758 if (size && size < xattr->val_len)
761 err = xattr->val_len;
765 memcpy(value, xattr->val, xattr->val_len);
768 spin_unlock(&ci->i_ceph_lock);
772 ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
775 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
776 return generic_getxattr(dentry, name, value, size);
778 return __ceph_getxattr(dentry->d_inode, name, value, size);
781 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
783 struct inode *inode = dentry->d_inode;
784 struct ceph_inode_info *ci = ceph_inode(inode);
785 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
792 spin_lock(&ci->i_ceph_lock);
793 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
794 ci->i_xattrs.version, ci->i_xattrs.index_version);
796 if (ci->i_xattrs.version == 0 ||
797 !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
798 spin_unlock(&ci->i_ceph_lock);
799 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
802 spin_lock(&ci->i_ceph_lock);
805 err = __build_xattrs(inode);
809 * Start with virtual dir xattr names (if any) (including
810 * terminating '\0' characters for each).
812 vir_namelen = ceph_vxattrs_name_size(vxattrs);
814 /* adding 1 byte per each variable due to the null termination */
815 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
817 if (size && vir_namelen + namelen > size)
820 err = namelen + vir_namelen;
824 names = __copy_xattr_names(ci, names);
826 /* virtual xattr names, too */
829 for (i = 0; vxattrs[i].name; i++) {
830 if (!vxattrs[i].hidden &&
831 !(vxattrs[i].exists_cb &&
832 !vxattrs[i].exists_cb(ci))) {
833 len = sprintf(names, "%s", vxattrs[i].name);
841 spin_unlock(&ci->i_ceph_lock);
845 static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
846 const char *value, size_t size, int flags)
848 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
849 struct inode *inode = dentry->d_inode;
850 struct ceph_inode_info *ci = ceph_inode(inode);
851 struct ceph_mds_request *req;
852 struct ceph_mds_client *mdsc = fsc->mdsc;
855 struct page **pages = NULL;
858 /* copy value into some pages */
859 nr_pages = calc_pages_for(0, size);
861 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
865 for (i = 0; i < nr_pages; i++) {
866 pages[i] = __page_cache_alloc(GFP_NOFS);
871 kaddr = kmap(pages[i]);
872 memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
873 min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
877 dout("setxattr value=%.*s\n", (int)size, value);
880 flags |= CEPH_XATTR_REMOVE;
883 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
889 req->r_inode = inode;
891 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
893 req->r_args.setxattr.flags = cpu_to_le32(flags);
894 req->r_path2 = kstrdup(name, GFP_NOFS);
896 req->r_pages = pages;
897 req->r_num_pages = nr_pages;
898 req->r_data_len = size;
900 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
901 err = ceph_mdsc_do_request(mdsc, NULL, req);
902 ceph_mdsc_put_request(req);
903 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
907 for (i = 0; i < nr_pages; i++)
908 __free_page(pages[i]);
914 int __ceph_setxattr(struct dentry *dentry, const char *name,
915 const void *value, size_t size, int flags)
917 struct inode *inode = dentry->d_inode;
918 struct ceph_vxattr *vxattr;
919 struct ceph_inode_info *ci = ceph_inode(inode);
923 int name_len = strlen(name);
925 char *newname = NULL;
927 struct ceph_inode_xattr *xattr = NULL;
928 int required_blob_size;
930 if (!ceph_is_valid_xattr(name))
933 vxattr = ceph_match_vxattr(inode, name);
934 if (vxattr && vxattr->readonly)
937 /* pass any unhandled ceph.* xattrs through to the MDS */
938 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
939 goto do_sync_unlocked;
941 /* preallocate memory for xattr name, value, index node */
943 newname = kmemdup(name, name_len + 1, GFP_NOFS);
948 newval = kmemdup(value, val_len, GFP_NOFS);
953 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
957 spin_lock(&ci->i_ceph_lock);
959 issued = __ceph_caps_issued(ci, NULL);
960 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
961 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
963 __build_xattrs(inode);
965 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
967 if (!ci->i_xattrs.prealloc_blob ||
968 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
969 struct ceph_buffer *blob;
971 spin_unlock(&ci->i_ceph_lock);
972 dout(" preaallocating new blob size=%d\n", required_blob_size);
973 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
976 spin_lock(&ci->i_ceph_lock);
977 if (ci->i_xattrs.prealloc_blob)
978 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
979 ci->i_xattrs.prealloc_blob = blob;
983 err = __set_xattr(ci, newname, name_len, newval, val_len,
984 flags, value ? 1 : -1, &xattr);
987 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
988 ci->i_xattrs.dirty = true;
989 inode->i_ctime = CURRENT_TIME;
992 spin_unlock(&ci->i_ceph_lock);
994 __mark_inode_dirty(inode, dirty);
998 spin_unlock(&ci->i_ceph_lock);
1000 err = ceph_sync_setxattr(dentry, name, value, size, flags);
1008 int ceph_setxattr(struct dentry *dentry, const char *name,
1009 const void *value, size_t size, int flags)
1011 if (ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
1014 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1015 return generic_setxattr(dentry, name, value, size, flags);
1017 return __ceph_setxattr(dentry, name, value, size, flags);
1020 static int ceph_send_removexattr(struct dentry *dentry, const char *name)
1022 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
1023 struct ceph_mds_client *mdsc = fsc->mdsc;
1024 struct inode *inode = dentry->d_inode;
1025 struct ceph_mds_request *req;
1028 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
1031 return PTR_ERR(req);
1032 req->r_inode = inode;
1034 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1035 req->r_num_caps = 1;
1036 req->r_path2 = kstrdup(name, GFP_NOFS);
1038 err = ceph_mdsc_do_request(mdsc, NULL, req);
1039 ceph_mdsc_put_request(req);
1043 int __ceph_removexattr(struct dentry *dentry, const char *name)
1045 struct inode *inode = dentry->d_inode;
1046 struct ceph_vxattr *vxattr;
1047 struct ceph_inode_info *ci = ceph_inode(inode);
1050 int required_blob_size;
1053 if (!ceph_is_valid_xattr(name))
1056 vxattr = ceph_match_vxattr(inode, name);
1057 if (vxattr && vxattr->readonly)
1060 /* pass any unhandled ceph.* xattrs through to the MDS */
1061 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1062 goto do_sync_unlocked;
1065 spin_lock(&ci->i_ceph_lock);
1067 issued = __ceph_caps_issued(ci, NULL);
1068 dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
1070 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
1072 __build_xattrs(inode);
1074 required_blob_size = __get_required_blob_size(ci, 0, 0);
1076 if (!ci->i_xattrs.prealloc_blob ||
1077 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
1078 struct ceph_buffer *blob;
1080 spin_unlock(&ci->i_ceph_lock);
1081 dout(" preaallocating new blob size=%d\n", required_blob_size);
1082 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
1085 spin_lock(&ci->i_ceph_lock);
1086 if (ci->i_xattrs.prealloc_blob)
1087 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
1088 ci->i_xattrs.prealloc_blob = blob;
1092 err = __remove_xattr_by_name(ceph_inode(inode), name);
1094 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
1095 ci->i_xattrs.dirty = true;
1096 inode->i_ctime = CURRENT_TIME;
1097 spin_unlock(&ci->i_ceph_lock);
1099 __mark_inode_dirty(inode, dirty);
1102 spin_unlock(&ci->i_ceph_lock);
1104 err = ceph_send_removexattr(dentry, name);
1109 int ceph_removexattr(struct dentry *dentry, const char *name)
1111 if (ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
1114 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1115 return generic_removexattr(dentry, name);
1117 return __ceph_removexattr(dentry, name);