1 #include "ceph_debug.h"
5 #include <linux/xattr.h>
6 #include <linux/slab.h>
8 static bool ceph_is_valid_xattr(const char *name)
10 return !strncmp(name, XATTR_SECURITY_PREFIX,
11 XATTR_SECURITY_PREFIX_LEN) ||
12 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
13 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
17 * These define virtual xattrs exposing the recursive directory
18 * statistics and layout metadata.
20 struct ceph_vxattr_cb {
23 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
29 static size_t ceph_vxattrcb_entries(struct ceph_inode_info *ci, char *val,
32 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
35 static size_t ceph_vxattrcb_files(struct ceph_inode_info *ci, char *val,
38 return snprintf(val, size, "%lld", ci->i_files);
41 static size_t ceph_vxattrcb_subdirs(struct ceph_inode_info *ci, char *val,
44 return snprintf(val, size, "%lld", ci->i_subdirs);
47 static size_t ceph_vxattrcb_rentries(struct ceph_inode_info *ci, char *val,
50 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
53 static size_t ceph_vxattrcb_rfiles(struct ceph_inode_info *ci, char *val,
56 return snprintf(val, size, "%lld", ci->i_rfiles);
59 static size_t ceph_vxattrcb_rsubdirs(struct ceph_inode_info *ci, char *val,
62 return snprintf(val, size, "%lld", ci->i_rsubdirs);
65 static size_t ceph_vxattrcb_rbytes(struct ceph_inode_info *ci, char *val,
68 return snprintf(val, size, "%lld", ci->i_rbytes);
71 static size_t ceph_vxattrcb_rctime(struct ceph_inode_info *ci, char *val,
74 return snprintf(val, size, "%ld.%ld", (long)ci->i_rctime.tv_sec,
75 (long)ci->i_rctime.tv_nsec);
78 static struct ceph_vxattr_cb ceph_dir_vxattrs[] = {
79 { true, "user.ceph.dir.entries", ceph_vxattrcb_entries},
80 { true, "user.ceph.dir.files", ceph_vxattrcb_files},
81 { true, "user.ceph.dir.subdirs", ceph_vxattrcb_subdirs},
82 { true, "user.ceph.dir.rentries", ceph_vxattrcb_rentries},
83 { true, "user.ceph.dir.rfiles", ceph_vxattrcb_rfiles},
84 { true, "user.ceph.dir.rsubdirs", ceph_vxattrcb_rsubdirs},
85 { true, "user.ceph.dir.rbytes", ceph_vxattrcb_rbytes},
86 { true, "user.ceph.dir.rctime", ceph_vxattrcb_rctime},
92 static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
97 ret = snprintf(val, size,
98 "chunk_bytes=%lld\nstripe_count=%lld\nobject_size=%lld\n",
99 (unsigned long long)ceph_file_layout_su(ci->i_layout),
100 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
101 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
102 if (ceph_file_layout_pg_preferred(ci->i_layout))
103 ret += snprintf(val + ret, size, "preferred_osd=%lld\n",
104 (unsigned long long)ceph_file_layout_pg_preferred(
109 static struct ceph_vxattr_cb ceph_file_vxattrs[] = {
110 { true, "user.ceph.layout", ceph_vxattrcb_layout},
114 static struct ceph_vxattr_cb *ceph_inode_vxattrs(struct inode *inode)
116 if (S_ISDIR(inode->i_mode))
117 return ceph_dir_vxattrs;
118 else if (S_ISREG(inode->i_mode))
119 return ceph_file_vxattrs;
123 static struct ceph_vxattr_cb *ceph_match_vxattr(struct ceph_vxattr_cb *vxattr,
127 if (strcmp(vxattr->name, name) == 0)
130 } while (vxattr->name);
134 static int __set_xattr(struct ceph_inode_info *ci,
135 const char *name, int name_len,
136 const char *val, int val_len,
138 int should_free_name, int should_free_val,
139 struct ceph_inode_xattr **newxattr)
142 struct rb_node *parent = NULL;
143 struct ceph_inode_xattr *xattr = NULL;
147 p = &ci->i_xattrs.index.rb_node;
150 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
151 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
157 if (name_len == xattr->name_len)
159 else if (name_len < xattr->name_len)
171 xattr->name_len = name_len;
172 xattr->should_free_name = should_free_name;
174 ci->i_xattrs.count++;
175 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
179 if (xattr->should_free_val)
180 kfree((void *)xattr->val);
182 if (should_free_name) {
186 ci->i_xattrs.names_size -= xattr->name_len;
187 ci->i_xattrs.vals_size -= xattr->val_len;
189 ci->i_xattrs.names_size += name_len;
190 ci->i_xattrs.vals_size += val_len;
196 xattr->val_len = val_len;
197 xattr->dirty = dirty;
198 xattr->should_free_val = (val && should_free_val);
201 rb_link_node(&xattr->node, parent, p);
202 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
203 dout("__set_xattr_val p=%p\n", p);
206 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
207 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
212 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
216 struct rb_node *parent = NULL;
217 struct ceph_inode_xattr *xattr = NULL;
220 p = &ci->i_xattrs.index.rb_node;
223 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
224 c = strncmp(name, xattr->name, xattr->name_len);
230 dout("__get_xattr %s: found %.*s\n", name,
231 xattr->val_len, xattr->val);
236 dout("__get_xattr %s: not found\n", name);
241 static void __free_xattr(struct ceph_inode_xattr *xattr)
245 if (xattr->should_free_name)
246 kfree((void *)xattr->name);
247 if (xattr->should_free_val)
248 kfree((void *)xattr->val);
253 static int __remove_xattr(struct ceph_inode_info *ci,
254 struct ceph_inode_xattr *xattr)
259 rb_erase(&xattr->node, &ci->i_xattrs.index);
261 if (xattr->should_free_name)
262 kfree((void *)xattr->name);
263 if (xattr->should_free_val)
264 kfree((void *)xattr->val);
266 ci->i_xattrs.names_size -= xattr->name_len;
267 ci->i_xattrs.vals_size -= xattr->val_len;
268 ci->i_xattrs.count--;
274 static int __remove_xattr_by_name(struct ceph_inode_info *ci,
278 struct ceph_inode_xattr *xattr;
281 p = &ci->i_xattrs.index.rb_node;
282 xattr = __get_xattr(ci, name);
283 err = __remove_xattr(ci, xattr);
287 static char *__copy_xattr_names(struct ceph_inode_info *ci,
291 struct ceph_inode_xattr *xattr = NULL;
293 p = rb_first(&ci->i_xattrs.index);
294 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
297 xattr = rb_entry(p, struct ceph_inode_xattr, node);
298 memcpy(dest, xattr->name, xattr->name_len);
299 dest[xattr->name_len] = '\0';
301 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
302 xattr->name_len, ci->i_xattrs.names_size);
304 dest += xattr->name_len + 1;
311 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
313 struct rb_node *p, *tmp;
314 struct ceph_inode_xattr *xattr = NULL;
316 p = rb_first(&ci->i_xattrs.index);
318 dout("__ceph_destroy_xattrs p=%p\n", p);
321 xattr = rb_entry(p, struct ceph_inode_xattr, node);
324 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
325 xattr->name_len, xattr->name);
326 rb_erase(tmp, &ci->i_xattrs.index);
331 ci->i_xattrs.names_size = 0;
332 ci->i_xattrs.vals_size = 0;
333 ci->i_xattrs.index_version = 0;
334 ci->i_xattrs.count = 0;
335 ci->i_xattrs.index = RB_ROOT;
338 static int __build_xattrs(struct inode *inode)
344 const char *name, *val;
345 struct ceph_inode_info *ci = ceph_inode(inode);
347 struct ceph_inode_xattr **xattrs = NULL;
351 dout("__build_xattrs() len=%d\n",
352 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
354 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
355 return 0; /* already built */
357 __ceph_destroy_xattrs(ci);
360 /* updated internal xattr rb tree */
361 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
362 p = ci->i_xattrs.blob->vec.iov_base;
363 end = p + ci->i_xattrs.blob->vec.iov_len;
364 ceph_decode_32_safe(&p, end, numattr, bad);
365 xattr_version = ci->i_xattrs.version;
366 spin_unlock(&inode->i_lock);
368 xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
373 memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
374 for (i = 0; i < numattr; i++) {
375 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
381 spin_lock(&inode->i_lock);
382 if (ci->i_xattrs.version != xattr_version) {
383 /* lost a race, retry */
384 for (i = 0; i < numattr; i++)
391 ceph_decode_32_safe(&p, end, len, bad);
395 ceph_decode_32_safe(&p, end, len, bad);
399 err = __set_xattr(ci, name, namelen, val, len,
400 0, 0, 0, &xattrs[numattr]);
407 ci->i_xattrs.index_version = ci->i_xattrs.version;
408 ci->i_xattrs.dirty = false;
412 spin_lock(&inode->i_lock);
415 for (i = 0; i < numattr; i++)
419 ci->i_xattrs.names_size = 0;
423 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
427 * 4 bytes for the length, and additional 4 bytes per each xattr name,
428 * 4 bytes per each value
430 int size = 4 + ci->i_xattrs.count*(4 + 4) +
431 ci->i_xattrs.names_size +
432 ci->i_xattrs.vals_size;
433 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
434 ci->i_xattrs.count, ci->i_xattrs.names_size,
435 ci->i_xattrs.vals_size);
438 size += 4 + 4 + name_size + val_size;
444 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
445 * and swap into place.
447 void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
450 struct ceph_inode_xattr *xattr = NULL;
453 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
454 if (ci->i_xattrs.dirty) {
455 int need = __get_required_blob_size(ci, 0, 0);
457 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
459 p = rb_first(&ci->i_xattrs.index);
460 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
462 ceph_encode_32(&dest, ci->i_xattrs.count);
464 xattr = rb_entry(p, struct ceph_inode_xattr, node);
466 ceph_encode_32(&dest, xattr->name_len);
467 memcpy(dest, xattr->name, xattr->name_len);
468 dest += xattr->name_len;
469 ceph_encode_32(&dest, xattr->val_len);
470 memcpy(dest, xattr->val, xattr->val_len);
471 dest += xattr->val_len;
476 /* adjust buffer len; it may be larger than we need */
477 ci->i_xattrs.prealloc_blob->vec.iov_len =
478 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
480 if (ci->i_xattrs.blob)
481 ceph_buffer_put(ci->i_xattrs.blob);
482 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
483 ci->i_xattrs.prealloc_blob = NULL;
484 ci->i_xattrs.dirty = false;
488 ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
491 struct inode *inode = dentry->d_inode;
492 struct ceph_inode_info *ci = ceph_inode(inode);
493 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
495 struct ceph_inode_xattr *xattr;
496 struct ceph_vxattr_cb *vxattr = NULL;
498 if (!ceph_is_valid_xattr(name))
501 /* let's see if a virtual xattr was requested */
503 vxattr = ceph_match_vxattr(vxattrs, name);
505 spin_lock(&inode->i_lock);
506 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
507 ci->i_xattrs.version, ci->i_xattrs.index_version);
509 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
510 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
513 spin_unlock(&inode->i_lock);
514 /* get xattrs from mds (if we don't already have them) */
515 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
520 spin_lock(&inode->i_lock);
522 if (vxattr && vxattr->readonly) {
523 err = vxattr->getxattr_cb(ci, value, size);
527 err = __build_xattrs(inode);
532 err = -ENODATA; /* == ENOATTR */
533 xattr = __get_xattr(ci, name);
536 err = vxattr->getxattr_cb(ci, value, size);
541 if (size && size < xattr->val_len)
544 err = xattr->val_len;
548 memcpy(value, xattr->val, xattr->val_len);
551 spin_unlock(&inode->i_lock);
555 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
557 struct inode *inode = dentry->d_inode;
558 struct ceph_inode_info *ci = ceph_inode(inode);
559 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
566 spin_lock(&inode->i_lock);
567 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
568 ci->i_xattrs.version, ci->i_xattrs.index_version);
570 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
571 (ci->i_xattrs.index_version > ci->i_xattrs.version)) {
574 spin_unlock(&inode->i_lock);
575 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
580 spin_lock(&inode->i_lock);
582 err = __build_xattrs(inode);
588 /* include virtual dir xattrs */
590 for (i = 0; vxattrs[i].name; i++)
591 vir_namelen += strlen(vxattrs[i].name) + 1;
592 /* adding 1 byte per each variable due to the null termination */
593 namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
595 if (size && namelen > size)
602 names = __copy_xattr_names(ci, names);
604 /* virtual xattr names, too */
606 for (i = 0; vxattrs[i].name; i++) {
607 len = sprintf(names, "%s", vxattrs[i].name);
612 spin_unlock(&inode->i_lock);
616 static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
617 const char *value, size_t size, int flags)
619 struct ceph_client *client = ceph_sb_to_client(dentry->d_sb);
620 struct inode *inode = dentry->d_inode;
621 struct ceph_inode_info *ci = ceph_inode(inode);
622 struct inode *parent_inode = dentry->d_parent->d_inode;
623 struct ceph_mds_request *req;
624 struct ceph_mds_client *mdsc = &client->mdsc;
627 struct page **pages = NULL;
630 /* copy value into some pages */
631 nr_pages = calc_pages_for(0, size);
633 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
637 for (i = 0; i < nr_pages; i++) {
638 pages[i] = __page_cache_alloc(GFP_NOFS);
643 kaddr = kmap(pages[i]);
644 memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
645 min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
649 dout("setxattr value=%.*s\n", (int)size, value);
652 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
658 req->r_inode = igrab(inode);
659 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
661 req->r_args.setxattr.flags = cpu_to_le32(flags);
662 req->r_path2 = kstrdup(name, GFP_NOFS);
664 req->r_pages = pages;
665 req->r_num_pages = nr_pages;
666 req->r_data_len = size;
668 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
669 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
670 ceph_mdsc_put_request(req);
671 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
675 for (i = 0; i < nr_pages; i++)
676 __free_page(pages[i]);
682 int ceph_setxattr(struct dentry *dentry, const char *name,
683 const void *value, size_t size, int flags)
685 struct inode *inode = dentry->d_inode;
686 struct ceph_inode_info *ci = ceph_inode(inode);
687 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
689 int name_len = strlen(name);
691 char *newname = NULL;
693 struct ceph_inode_xattr *xattr = NULL;
695 int required_blob_size;
697 if (ceph_snap(inode) != CEPH_NOSNAP)
700 if (!ceph_is_valid_xattr(name))
704 struct ceph_vxattr_cb *vxattr =
705 ceph_match_vxattr(vxattrs, name);
706 if (vxattr && vxattr->readonly)
710 /* preallocate memory for xattr name, value, index node */
712 newname = kmalloc(name_len + 1, GFP_NOFS);
715 memcpy(newname, name, name_len + 1);
718 newval = kmalloc(val_len + 1, GFP_NOFS);
721 memcpy(newval, value, val_len);
722 newval[val_len] = '\0';
725 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
729 spin_lock(&inode->i_lock);
731 issued = __ceph_caps_issued(ci, NULL);
732 if (!(issued & CEPH_CAP_XATTR_EXCL))
734 __build_xattrs(inode);
736 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
738 if (!ci->i_xattrs.prealloc_blob ||
739 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
740 struct ceph_buffer *blob = NULL;
742 spin_unlock(&inode->i_lock);
743 dout(" preaallocating new blob size=%d\n", required_blob_size);
744 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
747 spin_lock(&inode->i_lock);
748 if (ci->i_xattrs.prealloc_blob)
749 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
750 ci->i_xattrs.prealloc_blob = blob;
754 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
755 err = __set_xattr(ci, newname, name_len, newval,
756 val_len, 1, 1, 1, &xattr);
757 __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
758 ci->i_xattrs.dirty = true;
759 inode->i_ctime = CURRENT_TIME;
760 spin_unlock(&inode->i_lock);
765 spin_unlock(&inode->i_lock);
766 err = ceph_sync_setxattr(dentry, name, value, size, flags);
774 static int ceph_send_removexattr(struct dentry *dentry, const char *name)
776 struct ceph_client *client = ceph_sb_to_client(dentry->d_sb);
777 struct ceph_mds_client *mdsc = &client->mdsc;
778 struct inode *inode = dentry->d_inode;
779 struct inode *parent_inode = dentry->d_parent->d_inode;
780 struct ceph_mds_request *req;
783 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
787 req->r_inode = igrab(inode);
788 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
790 req->r_path2 = kstrdup(name, GFP_NOFS);
792 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
793 ceph_mdsc_put_request(req);
797 int ceph_removexattr(struct dentry *dentry, const char *name)
799 struct inode *inode = dentry->d_inode;
800 struct ceph_inode_info *ci = ceph_inode(inode);
801 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
805 if (ceph_snap(inode) != CEPH_NOSNAP)
808 if (!ceph_is_valid_xattr(name))
812 struct ceph_vxattr_cb *vxattr =
813 ceph_match_vxattr(vxattrs, name);
814 if (vxattr && vxattr->readonly)
818 spin_lock(&inode->i_lock);
819 __build_xattrs(inode);
820 issued = __ceph_caps_issued(ci, NULL);
821 dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
823 if (!(issued & CEPH_CAP_XATTR_EXCL))
826 err = __remove_xattr_by_name(ceph_inode(inode), name);
827 __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
828 ci->i_xattrs.dirty = true;
829 inode->i_ctime = CURRENT_TIME;
831 spin_unlock(&inode->i_lock);
835 spin_unlock(&inode->i_lock);
836 err = ceph_send_removexattr(dentry, name);