1 #include "ceph_debug.h"
5 #include <linux/xattr.h>
7 static bool ceph_is_valid_xattr(const char *name)
9 return !strncmp(name, XATTR_SECURITY_PREFIX,
10 XATTR_SECURITY_PREFIX_LEN) ||
11 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
12 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
16 * These define virtual xattrs exposing the recursive directory
17 * statistics and layout metadata.
19 struct ceph_vxattr_cb {
22 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
28 static size_t ceph_vxattrcb_entries(struct ceph_inode_info *ci, char *val,
31 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
34 static size_t ceph_vxattrcb_files(struct ceph_inode_info *ci, char *val,
37 return snprintf(val, size, "%lld", ci->i_files);
40 static size_t ceph_vxattrcb_subdirs(struct ceph_inode_info *ci, char *val,
43 return snprintf(val, size, "%lld", ci->i_subdirs);
46 static size_t ceph_vxattrcb_rentries(struct ceph_inode_info *ci, char *val,
49 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
52 static size_t ceph_vxattrcb_rfiles(struct ceph_inode_info *ci, char *val,
55 return snprintf(val, size, "%lld", ci->i_rfiles);
58 static size_t ceph_vxattrcb_rsubdirs(struct ceph_inode_info *ci, char *val,
61 return snprintf(val, size, "%lld", ci->i_rsubdirs);
64 static size_t ceph_vxattrcb_rbytes(struct ceph_inode_info *ci, char *val,
67 return snprintf(val, size, "%lld", ci->i_rbytes);
70 static size_t ceph_vxattrcb_rctime(struct ceph_inode_info *ci, char *val,
73 return snprintf(val, size, "%ld.%ld", (long)ci->i_rctime.tv_sec,
74 (long)ci->i_rctime.tv_nsec);
77 static struct ceph_vxattr_cb ceph_dir_vxattrs[] = {
78 { true, "user.ceph.dir.entries", ceph_vxattrcb_entries},
79 { true, "user.ceph.dir.files", ceph_vxattrcb_files},
80 { true, "user.ceph.dir.subdirs", ceph_vxattrcb_subdirs},
81 { true, "user.ceph.dir.rentries", ceph_vxattrcb_rentries},
82 { true, "user.ceph.dir.rfiles", ceph_vxattrcb_rfiles},
83 { true, "user.ceph.dir.rsubdirs", ceph_vxattrcb_rsubdirs},
84 { true, "user.ceph.dir.rbytes", ceph_vxattrcb_rbytes},
85 { true, "user.ceph.dir.rctime", ceph_vxattrcb_rctime},
91 static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
96 ret = snprintf(val, size,
97 "chunk_bytes=%lld\nstripe_count=%lld\nobject_size=%lld\n",
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 if (ceph_file_layout_pg_preferred(ci->i_layout))
102 ret += snprintf(val + ret, size, "preferred_osd=%lld\n",
103 (unsigned long long)ceph_file_layout_pg_preferred(
108 static struct ceph_vxattr_cb ceph_file_vxattrs[] = {
109 { true, "user.ceph.layout", ceph_vxattrcb_layout},
113 static struct ceph_vxattr_cb *ceph_inode_vxattrs(struct inode *inode)
115 if (S_ISDIR(inode->i_mode))
116 return ceph_dir_vxattrs;
117 else if (S_ISREG(inode->i_mode))
118 return ceph_file_vxattrs;
122 static struct ceph_vxattr_cb *ceph_match_vxattr(struct ceph_vxattr_cb *vxattr,
126 if (strcmp(vxattr->name, name) == 0)
129 } while (vxattr->name);
133 static int __set_xattr(struct ceph_inode_info *ci,
134 const char *name, int name_len,
135 const char *val, int val_len,
137 int should_free_name, int should_free_val,
138 struct ceph_inode_xattr **newxattr)
141 struct rb_node *parent = NULL;
142 struct ceph_inode_xattr *xattr = NULL;
146 p = &ci->i_xattrs.index.rb_node;
149 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
150 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
156 if (name_len == xattr->name_len)
158 else if (name_len < xattr->name_len)
170 xattr->name_len = name_len;
171 xattr->should_free_name = should_free_name;
173 ci->i_xattrs.count++;
174 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
178 if (xattr->should_free_val)
179 kfree((void *)xattr->val);
181 if (should_free_name) {
185 ci->i_xattrs.names_size -= xattr->name_len;
186 ci->i_xattrs.vals_size -= xattr->val_len;
189 pr_err("__set_xattr ENOMEM on %p %llx.%llx xattr %s=%s\n",
190 &ci->vfs_inode, ceph_vinop(&ci->vfs_inode), name,
194 ci->i_xattrs.names_size += name_len;
195 ci->i_xattrs.vals_size += val_len;
201 xattr->val_len = val_len;
202 xattr->dirty = dirty;
203 xattr->should_free_val = (val && should_free_val);
206 rb_link_node(&xattr->node, parent, p);
207 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
208 dout("__set_xattr_val p=%p\n", p);
211 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
212 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
217 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
221 struct rb_node *parent = NULL;
222 struct ceph_inode_xattr *xattr = NULL;
225 p = &ci->i_xattrs.index.rb_node;
228 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
229 c = strncmp(name, xattr->name, xattr->name_len);
235 dout("__get_xattr %s: found %.*s\n", name,
236 xattr->val_len, xattr->val);
241 dout("__get_xattr %s: not found\n", name);
246 static void __free_xattr(struct ceph_inode_xattr *xattr)
250 if (xattr->should_free_name)
251 kfree((void *)xattr->name);
252 if (xattr->should_free_val)
253 kfree((void *)xattr->val);
258 static int __remove_xattr(struct ceph_inode_info *ci,
259 struct ceph_inode_xattr *xattr)
264 rb_erase(&xattr->node, &ci->i_xattrs.index);
266 if (xattr->should_free_name)
267 kfree((void *)xattr->name);
268 if (xattr->should_free_val)
269 kfree((void *)xattr->val);
271 ci->i_xattrs.names_size -= xattr->name_len;
272 ci->i_xattrs.vals_size -= xattr->val_len;
273 ci->i_xattrs.count--;
279 static int __remove_xattr_by_name(struct ceph_inode_info *ci,
283 struct ceph_inode_xattr *xattr;
286 p = &ci->i_xattrs.index.rb_node;
287 xattr = __get_xattr(ci, name);
288 err = __remove_xattr(ci, xattr);
292 static char *__copy_xattr_names(struct ceph_inode_info *ci,
296 struct ceph_inode_xattr *xattr = NULL;
298 p = rb_first(&ci->i_xattrs.index);
299 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
302 xattr = rb_entry(p, struct ceph_inode_xattr, node);
303 memcpy(dest, xattr->name, xattr->name_len);
304 dest[xattr->name_len] = '\0';
306 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
307 xattr->name_len, ci->i_xattrs.names_size);
309 dest += xattr->name_len + 1;
316 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
318 struct rb_node *p, *tmp;
319 struct ceph_inode_xattr *xattr = NULL;
321 p = rb_first(&ci->i_xattrs.index);
323 dout("__ceph_destroy_xattrs p=%p\n", p);
326 xattr = rb_entry(p, struct ceph_inode_xattr, node);
329 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
330 xattr->name_len, xattr->name);
331 rb_erase(tmp, &ci->i_xattrs.index);
336 ci->i_xattrs.names_size = 0;
337 ci->i_xattrs.vals_size = 0;
338 ci->i_xattrs.index_version = 0;
339 ci->i_xattrs.count = 0;
340 ci->i_xattrs.index = RB_ROOT;
343 static int __build_xattrs(struct inode *inode)
349 const char *name, *val;
350 struct ceph_inode_info *ci = ceph_inode(inode);
352 struct ceph_inode_xattr **xattrs = NULL;
356 dout("__build_xattrs() len=%d\n",
357 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
359 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
360 return 0; /* already built */
362 __ceph_destroy_xattrs(ci);
365 /* updated internal xattr rb tree */
366 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
367 p = ci->i_xattrs.blob->vec.iov_base;
368 end = p + ci->i_xattrs.blob->vec.iov_len;
369 ceph_decode_32_safe(&p, end, numattr, bad);
370 xattr_version = ci->i_xattrs.version;
371 spin_unlock(&inode->i_lock);
373 xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
378 memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
379 for (i = 0; i < numattr; i++) {
380 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
386 spin_lock(&inode->i_lock);
387 if (ci->i_xattrs.version != xattr_version) {
388 /* lost a race, retry */
389 for (i = 0; i < numattr; i++)
396 ceph_decode_32_safe(&p, end, len, bad);
400 ceph_decode_32_safe(&p, end, len, bad);
404 err = __set_xattr(ci, name, namelen, val, len,
405 0, 0, 0, &xattrs[numattr]);
412 ci->i_xattrs.index_version = ci->i_xattrs.version;
413 ci->i_xattrs.dirty = false;
417 spin_lock(&inode->i_lock);
420 for (i = 0; i < numattr; i++)
424 ci->i_xattrs.names_size = 0;
428 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
432 * 4 bytes for the length, and additional 4 bytes per each xattr name,
433 * 4 bytes per each value
435 int size = 4 + ci->i_xattrs.count*(4 + 4) +
436 ci->i_xattrs.names_size +
437 ci->i_xattrs.vals_size;
438 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
439 ci->i_xattrs.count, ci->i_xattrs.names_size,
440 ci->i_xattrs.vals_size);
443 size += 4 + 4 + name_size + val_size;
449 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
450 * and swap into place.
452 void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
455 struct ceph_inode_xattr *xattr = NULL;
458 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
459 if (ci->i_xattrs.dirty) {
460 int need = __get_required_blob_size(ci, 0, 0);
462 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
464 p = rb_first(&ci->i_xattrs.index);
465 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
467 ceph_encode_32(&dest, ci->i_xattrs.count);
469 xattr = rb_entry(p, struct ceph_inode_xattr, node);
471 ceph_encode_32(&dest, xattr->name_len);
472 memcpy(dest, xattr->name, xattr->name_len);
473 dest += xattr->name_len;
474 ceph_encode_32(&dest, xattr->val_len);
475 memcpy(dest, xattr->val, xattr->val_len);
476 dest += xattr->val_len;
481 /* adjust buffer len; it may be larger than we need */
482 ci->i_xattrs.prealloc_blob->vec.iov_len =
483 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
485 ceph_buffer_put(ci->i_xattrs.blob);
486 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
487 ci->i_xattrs.prealloc_blob = NULL;
488 ci->i_xattrs.dirty = false;
492 ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
495 struct inode *inode = dentry->d_inode;
496 struct ceph_inode_info *ci = ceph_inode(inode);
497 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
499 struct ceph_inode_xattr *xattr;
500 struct ceph_vxattr_cb *vxattr = NULL;
502 if (!ceph_is_valid_xattr(name))
505 /* let's see if a virtual xattr was requested */
507 vxattr = ceph_match_vxattr(vxattrs, name);
509 spin_lock(&inode->i_lock);
510 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
511 ci->i_xattrs.version, ci->i_xattrs.index_version);
513 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
514 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
517 spin_unlock(&inode->i_lock);
518 /* get xattrs from mds (if we don't already have them) */
519 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
524 spin_lock(&inode->i_lock);
526 if (vxattr && vxattr->readonly) {
527 err = vxattr->getxattr_cb(ci, value, size);
531 err = __build_xattrs(inode);
536 err = -ENODATA; /* == ENOATTR */
537 xattr = __get_xattr(ci, name);
540 err = vxattr->getxattr_cb(ci, value, size);
545 if (size && size < xattr->val_len)
548 err = xattr->val_len;
552 memcpy(value, xattr->val, xattr->val_len);
555 spin_unlock(&inode->i_lock);
559 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
561 struct inode *inode = dentry->d_inode;
562 struct ceph_inode_info *ci = ceph_inode(inode);
563 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
570 spin_lock(&inode->i_lock);
571 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
572 ci->i_xattrs.version, ci->i_xattrs.index_version);
574 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
575 (ci->i_xattrs.index_version > ci->i_xattrs.version)) {
578 spin_unlock(&inode->i_lock);
579 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
584 spin_lock(&inode->i_lock);
586 err = __build_xattrs(inode);
592 /* include virtual dir xattrs */
594 for (i = 0; vxattrs[i].name; i++)
595 vir_namelen += strlen(vxattrs[i].name) + 1;
596 /* adding 1 byte per each variable due to the null termination */
597 namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
599 if (size && namelen > size)
606 names = __copy_xattr_names(ci, names);
608 /* virtual xattr names, too */
610 for (i = 0; vxattrs[i].name; i++) {
611 len = sprintf(names, "%s", vxattrs[i].name);
616 spin_unlock(&inode->i_lock);
620 static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
621 const char *value, size_t size, int flags)
623 struct ceph_client *client = ceph_client(dentry->d_sb);
624 struct inode *inode = dentry->d_inode;
625 struct ceph_inode_info *ci = ceph_inode(inode);
626 struct inode *parent_inode = dentry->d_parent->d_inode;
627 struct ceph_mds_request *req;
628 struct ceph_mds_client *mdsc = &client->mdsc;
631 struct page **pages = NULL;
634 /* copy value into some pages */
635 nr_pages = calc_pages_for(0, size);
637 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
641 for (i = 0; i < nr_pages; i++) {
642 pages[i] = alloc_page(GFP_NOFS);
647 kaddr = kmap(pages[i]);
648 memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
649 min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
653 dout("setxattr value=%.*s\n", (int)size, value);
656 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
660 req->r_inode = igrab(inode);
661 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
663 req->r_args.setxattr.flags = cpu_to_le32(flags);
664 req->r_path2 = kstrdup(name, GFP_NOFS);
666 req->r_pages = pages;
667 req->r_num_pages = nr_pages;
668 req->r_data_len = size;
670 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
671 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
672 ceph_mdsc_put_request(req);
673 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
677 for (i = 0; i < nr_pages; i++)
678 __free_page(pages[i]);
684 int ceph_setxattr(struct dentry *dentry, const char *name,
685 const void *value, size_t size, int flags)
687 struct inode *inode = dentry->d_inode;
688 struct ceph_inode_info *ci = ceph_inode(inode);
689 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
691 int name_len = strlen(name);
693 char *newname = NULL;
695 struct ceph_inode_xattr *xattr = NULL;
697 int required_blob_size;
699 if (ceph_snap(inode) != CEPH_NOSNAP)
702 if (!ceph_is_valid_xattr(name))
706 struct ceph_vxattr_cb *vxattr =
707 ceph_match_vxattr(vxattrs, name);
708 if (vxattr && vxattr->readonly)
712 /* preallocate memory for xattr name, value, index node */
714 newname = kmalloc(name_len + 1, GFP_NOFS);
717 memcpy(newname, name, name_len + 1);
720 newval = kmalloc(val_len + 1, GFP_NOFS);
723 memcpy(newval, value, val_len);
724 newval[val_len] = '\0';
727 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
731 spin_lock(&inode->i_lock);
733 issued = __ceph_caps_issued(ci, NULL);
734 if (!(issued & CEPH_CAP_XATTR_EXCL))
736 __build_xattrs(inode);
738 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
740 if (!ci->i_xattrs.prealloc_blob ||
741 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
742 struct ceph_buffer *blob = NULL;
744 spin_unlock(&inode->i_lock);
745 dout(" preaallocating new blob size=%d\n", required_blob_size);
746 blob = ceph_buffer_new_alloc(required_blob_size, GFP_NOFS);
749 spin_lock(&inode->i_lock);
750 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
751 ci->i_xattrs.prealloc_blob = blob;
755 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
756 err = __set_xattr(ci, newname, name_len, newval,
757 val_len, 1, 1, 1, &xattr);
758 __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
759 ci->i_xattrs.dirty = true;
760 inode->i_ctime = CURRENT_TIME;
761 spin_unlock(&inode->i_lock);
766 spin_unlock(&inode->i_lock);
767 err = ceph_sync_setxattr(dentry, name, value, size, flags);
775 static int ceph_send_removexattr(struct dentry *dentry, const char *name)
777 struct ceph_client *client = ceph_client(dentry->d_sb);
778 struct ceph_mds_client *mdsc = &client->mdsc;
779 struct inode *inode = dentry->d_inode;
780 struct inode *parent_inode = dentry->d_parent->d_inode;
781 struct ceph_mds_request *req;
784 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
788 req->r_inode = igrab(inode);
789 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
791 req->r_path2 = kstrdup(name, GFP_NOFS);
793 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
794 ceph_mdsc_put_request(req);
798 int ceph_removexattr(struct dentry *dentry, const char *name)
800 struct inode *inode = dentry->d_inode;
801 struct ceph_inode_info *ci = ceph_inode(inode);
802 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
806 if (ceph_snap(inode) != CEPH_NOSNAP)
809 if (!ceph_is_valid_xattr(name))
813 struct ceph_vxattr_cb *vxattr =
814 ceph_match_vxattr(vxattrs, name);
815 if (vxattr && vxattr->readonly)
819 spin_lock(&inode->i_lock);
820 __build_xattrs(inode);
821 issued = __ceph_caps_issued(ci, NULL);
822 dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
824 if (!(issued & CEPH_CAP_XATTR_EXCL))
827 err = __remove_xattr_by_name(ceph_inode(inode), name);
828 __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
829 ci->i_xattrs.dirty = true;
830 inode->i_ctime = CURRENT_TIME;
832 spin_unlock(&inode->i_lock);
836 spin_unlock(&inode->i_lock);
837 err = ceph_send_removexattr(dentry, name);