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)
16 * List of handlers for synthetic system.* attributes. Other
17 * attributes are handled directly.
19 const struct xattr_handler *ceph_xattr_handlers[] = {
20 #ifdef CONFIG_CEPH_FS_POSIX_ACL
21 &posix_acl_access_xattr_handler,
22 &posix_acl_default_xattr_handler,
27 static bool ceph_is_valid_xattr(const char *name)
29 return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
30 !strncmp(name, XATTR_SECURITY_PREFIX,
31 XATTR_SECURITY_PREFIX_LEN) ||
32 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
33 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
34 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
38 * These define virtual xattrs exposing the recursive directory
39 * statistics and layout metadata.
43 size_t name_size; /* strlen(name) + 1 (for '\0') */
44 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
46 bool readonly, hidden;
47 bool (*exists_cb)(struct ceph_inode_info *ci);
52 static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
55 char *p = (char *)&ci->i_layout;
57 for (s = 0; s < sizeof(ci->i_layout); s++, p++)
63 static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
67 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
68 struct ceph_osd_client *osdc = &fsc->client->osdc;
69 s64 pool = ceph_file_layout_pg_pool(ci->i_layout);
70 const char *pool_name;
72 dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
73 down_read(&osdc->map_sem);
74 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
76 ret = snprintf(val, size,
77 "stripe_unit=%lld stripe_count=%lld object_size=%lld pool=%s",
78 (unsigned long long)ceph_file_layout_su(ci->i_layout),
79 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
80 (unsigned long long)ceph_file_layout_object_size(ci->i_layout),
83 ret = snprintf(val, size,
84 "stripe_unit=%lld stripe_count=%lld object_size=%lld pool=%lld",
85 (unsigned long long)ceph_file_layout_su(ci->i_layout),
86 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
87 (unsigned long long)ceph_file_layout_object_size(ci->i_layout),
88 (unsigned long long)pool);
90 up_read(&osdc->map_sem);
94 static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
95 char *val, size_t size)
97 return snprintf(val, size, "%lld",
98 (unsigned long long)ceph_file_layout_su(ci->i_layout));
101 static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
102 char *val, size_t size)
104 return snprintf(val, size, "%lld",
105 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout));
108 static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
109 char *val, size_t size)
111 return snprintf(val, size, "%lld",
112 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
115 static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
116 char *val, size_t size)
119 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
120 struct ceph_osd_client *osdc = &fsc->client->osdc;
121 s64 pool = ceph_file_layout_pg_pool(ci->i_layout);
122 const char *pool_name;
124 down_read(&osdc->map_sem);
125 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
127 ret = snprintf(val, size, "%s", pool_name);
129 ret = snprintf(val, size, "%lld", (unsigned long long)pool);
130 up_read(&osdc->map_sem);
136 static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
139 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
142 static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
145 return snprintf(val, size, "%lld", ci->i_files);
148 static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
151 return snprintf(val, size, "%lld", ci->i_subdirs);
154 static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
157 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
160 static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
163 return snprintf(val, size, "%lld", ci->i_rfiles);
166 static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
169 return snprintf(val, size, "%lld", ci->i_rsubdirs);
172 static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
175 return snprintf(val, size, "%lld", ci->i_rbytes);
178 static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
181 return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
182 (long)ci->i_rctime.tv_nsec);
186 #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
187 #define CEPH_XATTR_NAME2(_type, _name, _name2) \
188 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
190 #define XATTR_NAME_CEPH(_type, _name) \
192 .name = CEPH_XATTR_NAME(_type, _name), \
193 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
194 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
199 #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
201 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
202 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
203 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
206 .exists_cb = ceph_vxattrcb_layout_exists, \
209 static struct ceph_vxattr ceph_dir_vxattrs[] = {
211 .name = "ceph.dir.layout",
212 .name_size = sizeof("ceph.dir.layout"),
213 .getxattr_cb = ceph_vxattrcb_layout,
216 .exists_cb = ceph_vxattrcb_layout_exists,
218 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
219 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
220 XATTR_LAYOUT_FIELD(dir, layout, object_size),
221 XATTR_LAYOUT_FIELD(dir, layout, pool),
222 XATTR_NAME_CEPH(dir, entries),
223 XATTR_NAME_CEPH(dir, files),
224 XATTR_NAME_CEPH(dir, subdirs),
225 XATTR_NAME_CEPH(dir, rentries),
226 XATTR_NAME_CEPH(dir, rfiles),
227 XATTR_NAME_CEPH(dir, rsubdirs),
228 XATTR_NAME_CEPH(dir, rbytes),
229 XATTR_NAME_CEPH(dir, rctime),
230 { .name = NULL, 0 } /* Required table terminator */
232 static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
236 static struct ceph_vxattr ceph_file_vxattrs[] = {
238 .name = "ceph.file.layout",
239 .name_size = sizeof("ceph.file.layout"),
240 .getxattr_cb = ceph_vxattrcb_layout,
243 .exists_cb = ceph_vxattrcb_layout_exists,
245 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
246 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
247 XATTR_LAYOUT_FIELD(file, layout, object_size),
248 XATTR_LAYOUT_FIELD(file, layout, pool),
249 { .name = NULL, 0 } /* Required table terminator */
251 static size_t ceph_file_vxattrs_name_size; /* total size of all names */
253 static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
255 if (S_ISDIR(inode->i_mode))
256 return ceph_dir_vxattrs;
257 else if (S_ISREG(inode->i_mode))
258 return ceph_file_vxattrs;
262 static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
264 if (vxattrs == ceph_dir_vxattrs)
265 return ceph_dir_vxattrs_name_size;
266 if (vxattrs == ceph_file_vxattrs)
267 return ceph_file_vxattrs_name_size;
274 * Compute the aggregate size (including terminating '\0') of all
275 * virtual extended attribute names in the given vxattr table.
277 static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
279 struct ceph_vxattr *vxattr;
282 for (vxattr = vxattrs; vxattr->name; vxattr++)
284 size += vxattr->name_size;
289 /* Routines called at initialization and exit time */
291 void __init ceph_xattr_init(void)
293 ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
294 ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
297 void ceph_xattr_exit(void)
299 ceph_dir_vxattrs_name_size = 0;
300 ceph_file_vxattrs_name_size = 0;
303 static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
306 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
309 while (vxattr->name) {
310 if (!strcmp(vxattr->name, name))
319 static int __set_xattr(struct ceph_inode_info *ci,
320 const char *name, int name_len,
321 const char *val, int val_len,
322 int flags, int update_xattr,
323 struct ceph_inode_xattr **newxattr)
326 struct rb_node *parent = NULL;
327 struct ceph_inode_xattr *xattr = NULL;
331 p = &ci->i_xattrs.index.rb_node;
334 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
335 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
341 if (name_len == xattr->name_len)
343 else if (name_len < xattr->name_len)
353 if (xattr && (flags & XATTR_CREATE))
355 else if (!xattr && (flags & XATTR_REPLACE))
368 xattr->name_len = name_len;
369 xattr->should_free_name = update_xattr;
371 ci->i_xattrs.count++;
372 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
376 if (xattr->should_free_val)
377 kfree((void *)xattr->val);
383 ci->i_xattrs.names_size -= xattr->name_len;
384 ci->i_xattrs.vals_size -= xattr->val_len;
386 ci->i_xattrs.names_size += name_len;
387 ci->i_xattrs.vals_size += val_len;
393 xattr->val_len = val_len;
394 xattr->dirty = update_xattr;
395 xattr->should_free_val = (val && update_xattr);
398 rb_link_node(&xattr->node, parent, p);
399 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
400 dout("__set_xattr_val p=%p\n", p);
403 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
404 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
409 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
413 struct rb_node *parent = NULL;
414 struct ceph_inode_xattr *xattr = NULL;
415 int name_len = strlen(name);
418 p = &ci->i_xattrs.index.rb_node;
421 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
422 c = strncmp(name, xattr->name, xattr->name_len);
423 if (c == 0 && name_len > xattr->name_len)
430 dout("__get_xattr %s: found %.*s\n", name,
431 xattr->val_len, xattr->val);
436 dout("__get_xattr %s: not found\n", name);
441 static void __free_xattr(struct ceph_inode_xattr *xattr)
445 if (xattr->should_free_name)
446 kfree((void *)xattr->name);
447 if (xattr->should_free_val)
448 kfree((void *)xattr->val);
453 static int __remove_xattr(struct ceph_inode_info *ci,
454 struct ceph_inode_xattr *xattr)
459 rb_erase(&xattr->node, &ci->i_xattrs.index);
461 if (xattr->should_free_name)
462 kfree((void *)xattr->name);
463 if (xattr->should_free_val)
464 kfree((void *)xattr->val);
466 ci->i_xattrs.names_size -= xattr->name_len;
467 ci->i_xattrs.vals_size -= xattr->val_len;
468 ci->i_xattrs.count--;
474 static int __remove_xattr_by_name(struct ceph_inode_info *ci,
478 struct ceph_inode_xattr *xattr;
481 p = &ci->i_xattrs.index.rb_node;
482 xattr = __get_xattr(ci, name);
483 err = __remove_xattr(ci, xattr);
487 static char *__copy_xattr_names(struct ceph_inode_info *ci,
491 struct ceph_inode_xattr *xattr = NULL;
493 p = rb_first(&ci->i_xattrs.index);
494 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
497 xattr = rb_entry(p, struct ceph_inode_xattr, node);
498 memcpy(dest, xattr->name, xattr->name_len);
499 dest[xattr->name_len] = '\0';
501 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
502 xattr->name_len, ci->i_xattrs.names_size);
504 dest += xattr->name_len + 1;
511 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
513 struct rb_node *p, *tmp;
514 struct ceph_inode_xattr *xattr = NULL;
516 p = rb_first(&ci->i_xattrs.index);
518 dout("__ceph_destroy_xattrs p=%p\n", p);
521 xattr = rb_entry(p, struct ceph_inode_xattr, node);
524 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
525 xattr->name_len, xattr->name);
526 rb_erase(tmp, &ci->i_xattrs.index);
531 ci->i_xattrs.names_size = 0;
532 ci->i_xattrs.vals_size = 0;
533 ci->i_xattrs.index_version = 0;
534 ci->i_xattrs.count = 0;
535 ci->i_xattrs.index = RB_ROOT;
538 static int __build_xattrs(struct inode *inode)
539 __releases(ci->i_ceph_lock)
540 __acquires(ci->i_ceph_lock)
546 const char *name, *val;
547 struct ceph_inode_info *ci = ceph_inode(inode);
549 struct ceph_inode_xattr **xattrs = NULL;
553 dout("__build_xattrs() len=%d\n",
554 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
556 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
557 return 0; /* already built */
559 __ceph_destroy_xattrs(ci);
562 /* updated internal xattr rb tree */
563 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
564 p = ci->i_xattrs.blob->vec.iov_base;
565 end = p + ci->i_xattrs.blob->vec.iov_len;
566 ceph_decode_32_safe(&p, end, numattr, bad);
567 xattr_version = ci->i_xattrs.version;
568 spin_unlock(&ci->i_ceph_lock);
570 xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
575 memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
576 for (i = 0; i < numattr; i++) {
577 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
583 spin_lock(&ci->i_ceph_lock);
584 if (ci->i_xattrs.version != xattr_version) {
585 /* lost a race, retry */
586 for (i = 0; i < numattr; i++)
594 ceph_decode_32_safe(&p, end, len, bad);
598 ceph_decode_32_safe(&p, end, len, bad);
602 err = __set_xattr(ci, name, namelen, val, len,
603 0, 0, &xattrs[numattr]);
610 ci->i_xattrs.index_version = ci->i_xattrs.version;
611 ci->i_xattrs.dirty = false;
615 spin_lock(&ci->i_ceph_lock);
618 for (i = 0; i < numattr; i++)
622 ci->i_xattrs.names_size = 0;
626 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
630 * 4 bytes for the length, and additional 4 bytes per each xattr name,
631 * 4 bytes per each value
633 int size = 4 + ci->i_xattrs.count*(4 + 4) +
634 ci->i_xattrs.names_size +
635 ci->i_xattrs.vals_size;
636 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
637 ci->i_xattrs.count, ci->i_xattrs.names_size,
638 ci->i_xattrs.vals_size);
641 size += 4 + 4 + name_size + val_size;
647 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
648 * and swap into place.
650 void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
653 struct ceph_inode_xattr *xattr = NULL;
656 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
657 if (ci->i_xattrs.dirty) {
658 int need = __get_required_blob_size(ci, 0, 0);
660 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
662 p = rb_first(&ci->i_xattrs.index);
663 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
665 ceph_encode_32(&dest, ci->i_xattrs.count);
667 xattr = rb_entry(p, struct ceph_inode_xattr, node);
669 ceph_encode_32(&dest, xattr->name_len);
670 memcpy(dest, xattr->name, xattr->name_len);
671 dest += xattr->name_len;
672 ceph_encode_32(&dest, xattr->val_len);
673 memcpy(dest, xattr->val, xattr->val_len);
674 dest += xattr->val_len;
679 /* adjust buffer len; it may be larger than we need */
680 ci->i_xattrs.prealloc_blob->vec.iov_len =
681 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
683 if (ci->i_xattrs.blob)
684 ceph_buffer_put(ci->i_xattrs.blob);
685 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
686 ci->i_xattrs.prealloc_blob = NULL;
687 ci->i_xattrs.dirty = false;
688 ci->i_xattrs.version++;
692 ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
695 struct ceph_inode_info *ci = ceph_inode(inode);
697 struct ceph_inode_xattr *xattr;
698 struct ceph_vxattr *vxattr = NULL;
700 if (!ceph_is_valid_xattr(name))
703 /* let's see if a virtual xattr was requested */
704 vxattr = ceph_match_vxattr(inode, name);
705 if (vxattr && !(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
706 err = vxattr->getxattr_cb(ci, value, size);
710 spin_lock(&ci->i_ceph_lock);
711 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
712 ci->i_xattrs.version, ci->i_xattrs.index_version);
714 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
715 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
718 spin_unlock(&ci->i_ceph_lock);
719 /* get xattrs from mds (if we don't already have them) */
720 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
725 spin_lock(&ci->i_ceph_lock);
727 err = __build_xattrs(inode);
732 err = -ENODATA; /* == ENOATTR */
733 xattr = __get_xattr(ci, name);
738 if (size && size < xattr->val_len)
741 err = xattr->val_len;
745 memcpy(value, xattr->val, xattr->val_len);
748 spin_unlock(&ci->i_ceph_lock);
752 ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
755 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
756 return generic_getxattr(dentry, name, value, size);
758 return __ceph_getxattr(dentry->d_inode, name, value, size);
761 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
763 struct inode *inode = dentry->d_inode;
764 struct ceph_inode_info *ci = ceph_inode(inode);
765 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
772 spin_lock(&ci->i_ceph_lock);
773 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
774 ci->i_xattrs.version, ci->i_xattrs.index_version);
776 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
777 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
780 spin_unlock(&ci->i_ceph_lock);
781 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
786 spin_lock(&ci->i_ceph_lock);
788 err = __build_xattrs(inode);
794 * Start with virtual dir xattr names (if any) (including
795 * terminating '\0' characters for each).
797 vir_namelen = ceph_vxattrs_name_size(vxattrs);
799 /* adding 1 byte per each variable due to the null termination */
800 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
802 if (size && vir_namelen + namelen > size)
805 err = namelen + vir_namelen;
809 names = __copy_xattr_names(ci, names);
811 /* virtual xattr names, too */
814 for (i = 0; vxattrs[i].name; i++) {
815 if (!vxattrs[i].hidden &&
816 !(vxattrs[i].exists_cb &&
817 !vxattrs[i].exists_cb(ci))) {
818 len = sprintf(names, "%s", vxattrs[i].name);
826 spin_unlock(&ci->i_ceph_lock);
830 static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
831 const char *value, size_t size, int flags)
833 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
834 struct inode *inode = dentry->d_inode;
835 struct ceph_inode_info *ci = ceph_inode(inode);
836 struct inode *parent_inode;
837 struct ceph_mds_request *req;
838 struct ceph_mds_client *mdsc = fsc->mdsc;
841 struct page **pages = NULL;
844 /* copy value into some pages */
845 nr_pages = calc_pages_for(0, size);
847 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
851 for (i = 0; i < nr_pages; i++) {
852 pages[i] = __page_cache_alloc(GFP_NOFS);
857 kaddr = kmap(pages[i]);
858 memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
859 min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
863 dout("setxattr value=%.*s\n", (int)size, value);
866 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
872 req->r_inode = inode;
874 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
876 req->r_args.setxattr.flags = cpu_to_le32(flags);
877 req->r_path2 = kstrdup(name, GFP_NOFS);
879 req->r_pages = pages;
880 req->r_num_pages = nr_pages;
881 req->r_data_len = size;
883 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
884 parent_inode = ceph_get_dentry_parent_inode(dentry);
885 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
887 ceph_mdsc_put_request(req);
888 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
892 for (i = 0; i < nr_pages; i++)
893 __free_page(pages[i]);
899 int __ceph_setxattr(struct dentry *dentry, const char *name,
900 const void *value, size_t size, int flags)
902 struct inode *inode = dentry->d_inode;
903 struct ceph_vxattr *vxattr;
904 struct ceph_inode_info *ci = ceph_inode(inode);
908 int name_len = strlen(name);
910 char *newname = NULL;
912 struct ceph_inode_xattr *xattr = NULL;
913 int required_blob_size;
915 if (!ceph_is_valid_xattr(name))
918 vxattr = ceph_match_vxattr(inode, name);
919 if (vxattr && vxattr->readonly)
922 /* pass any unhandled ceph.* xattrs through to the MDS */
923 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
924 goto do_sync_unlocked;
926 /* preallocate memory for xattr name, value, index node */
928 newname = kmemdup(name, name_len + 1, GFP_NOFS);
933 newval = kmemdup(value, val_len, GFP_NOFS);
938 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
942 spin_lock(&ci->i_ceph_lock);
944 issued = __ceph_caps_issued(ci, NULL);
945 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
946 if (!(issued & CEPH_CAP_XATTR_EXCL))
948 __build_xattrs(inode);
950 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
952 if (!ci->i_xattrs.prealloc_blob ||
953 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
954 struct ceph_buffer *blob;
956 spin_unlock(&ci->i_ceph_lock);
957 dout(" preaallocating new blob size=%d\n", required_blob_size);
958 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
961 spin_lock(&ci->i_ceph_lock);
962 if (ci->i_xattrs.prealloc_blob)
963 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
964 ci->i_xattrs.prealloc_blob = blob;
968 err = __set_xattr(ci, newname, name_len, newval,
969 val_len, flags, 1, &xattr);
972 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
973 ci->i_xattrs.dirty = true;
974 inode->i_ctime = CURRENT_TIME;
977 spin_unlock(&ci->i_ceph_lock);
979 __mark_inode_dirty(inode, dirty);
983 spin_unlock(&ci->i_ceph_lock);
985 err = ceph_sync_setxattr(dentry, name, value, size, flags);
993 int ceph_setxattr(struct dentry *dentry, const char *name,
994 const void *value, size_t size, int flags)
996 if (ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
999 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1000 return generic_setxattr(dentry, name, value, size, flags);
1002 return __ceph_setxattr(dentry, name, value, size, flags);
1005 static int ceph_send_removexattr(struct dentry *dentry, const char *name)
1007 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
1008 struct ceph_mds_client *mdsc = fsc->mdsc;
1009 struct inode *inode = dentry->d_inode;
1010 struct inode *parent_inode;
1011 struct ceph_mds_request *req;
1014 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
1017 return PTR_ERR(req);
1018 req->r_inode = inode;
1020 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1021 req->r_num_caps = 1;
1022 req->r_path2 = kstrdup(name, GFP_NOFS);
1024 parent_inode = ceph_get_dentry_parent_inode(dentry);
1025 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
1027 ceph_mdsc_put_request(req);
1031 int __ceph_removexattr(struct dentry *dentry, const char *name)
1033 struct inode *inode = dentry->d_inode;
1034 struct ceph_vxattr *vxattr;
1035 struct ceph_inode_info *ci = ceph_inode(inode);
1038 int required_blob_size;
1041 if (!ceph_is_valid_xattr(name))
1044 vxattr = ceph_match_vxattr(inode, name);
1045 if (vxattr && vxattr->readonly)
1048 /* pass any unhandled ceph.* xattrs through to the MDS */
1049 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1050 goto do_sync_unlocked;
1053 spin_lock(&ci->i_ceph_lock);
1055 issued = __ceph_caps_issued(ci, NULL);
1056 dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
1058 if (!(issued & CEPH_CAP_XATTR_EXCL))
1060 __build_xattrs(inode);
1062 required_blob_size = __get_required_blob_size(ci, 0, 0);
1064 if (!ci->i_xattrs.prealloc_blob ||
1065 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
1066 struct ceph_buffer *blob;
1068 spin_unlock(&ci->i_ceph_lock);
1069 dout(" preaallocating new blob size=%d\n", required_blob_size);
1070 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
1073 spin_lock(&ci->i_ceph_lock);
1074 if (ci->i_xattrs.prealloc_blob)
1075 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
1076 ci->i_xattrs.prealloc_blob = blob;
1080 err = __remove_xattr_by_name(ceph_inode(inode), name);
1082 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
1083 ci->i_xattrs.dirty = true;
1084 inode->i_ctime = CURRENT_TIME;
1085 spin_unlock(&ci->i_ceph_lock);
1087 __mark_inode_dirty(inode, dirty);
1090 spin_unlock(&ci->i_ceph_lock);
1092 err = ceph_send_removexattr(dentry, name);
1097 int ceph_removexattr(struct dentry *dentry, const char *name)
1099 if (ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
1102 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1103 return generic_removexattr(dentry, name);
1105 return __ceph_removexattr(dentry, name);