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;
190 pr_err("__set_xattr ENOMEM on %p %llx.%llx xattr %s=%s\n",
191 &ci->vfs_inode, ceph_vinop(&ci->vfs_inode), name,
195 ci->i_xattrs.names_size += name_len;
196 ci->i_xattrs.vals_size += val_len;
202 xattr->val_len = val_len;
203 xattr->dirty = dirty;
204 xattr->should_free_val = (val && should_free_val);
207 rb_link_node(&xattr->node, parent, p);
208 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
209 dout("__set_xattr_val p=%p\n", p);
212 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
213 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
218 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
222 struct rb_node *parent = NULL;
223 struct ceph_inode_xattr *xattr = NULL;
226 p = &ci->i_xattrs.index.rb_node;
229 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
230 c = strncmp(name, xattr->name, xattr->name_len);
236 dout("__get_xattr %s: found %.*s\n", name,
237 xattr->val_len, xattr->val);
242 dout("__get_xattr %s: not found\n", name);
247 static void __free_xattr(struct ceph_inode_xattr *xattr)
251 if (xattr->should_free_name)
252 kfree((void *)xattr->name);
253 if (xattr->should_free_val)
254 kfree((void *)xattr->val);
259 static int __remove_xattr(struct ceph_inode_info *ci,
260 struct ceph_inode_xattr *xattr)
265 rb_erase(&xattr->node, &ci->i_xattrs.index);
267 if (xattr->should_free_name)
268 kfree((void *)xattr->name);
269 if (xattr->should_free_val)
270 kfree((void *)xattr->val);
272 ci->i_xattrs.names_size -= xattr->name_len;
273 ci->i_xattrs.vals_size -= xattr->val_len;
274 ci->i_xattrs.count--;
280 static int __remove_xattr_by_name(struct ceph_inode_info *ci,
284 struct ceph_inode_xattr *xattr;
287 p = &ci->i_xattrs.index.rb_node;
288 xattr = __get_xattr(ci, name);
289 err = __remove_xattr(ci, xattr);
293 static char *__copy_xattr_names(struct ceph_inode_info *ci,
297 struct ceph_inode_xattr *xattr = NULL;
299 p = rb_first(&ci->i_xattrs.index);
300 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
303 xattr = rb_entry(p, struct ceph_inode_xattr, node);
304 memcpy(dest, xattr->name, xattr->name_len);
305 dest[xattr->name_len] = '\0';
307 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
308 xattr->name_len, ci->i_xattrs.names_size);
310 dest += xattr->name_len + 1;
317 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
319 struct rb_node *p, *tmp;
320 struct ceph_inode_xattr *xattr = NULL;
322 p = rb_first(&ci->i_xattrs.index);
324 dout("__ceph_destroy_xattrs p=%p\n", p);
327 xattr = rb_entry(p, struct ceph_inode_xattr, node);
330 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
331 xattr->name_len, xattr->name);
332 rb_erase(tmp, &ci->i_xattrs.index);
337 ci->i_xattrs.names_size = 0;
338 ci->i_xattrs.vals_size = 0;
339 ci->i_xattrs.index_version = 0;
340 ci->i_xattrs.count = 0;
341 ci->i_xattrs.index = RB_ROOT;
344 static int __build_xattrs(struct inode *inode)
350 const char *name, *val;
351 struct ceph_inode_info *ci = ceph_inode(inode);
353 struct ceph_inode_xattr **xattrs = NULL;
357 dout("__build_xattrs() len=%d\n",
358 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
360 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
361 return 0; /* already built */
363 __ceph_destroy_xattrs(ci);
366 /* updated internal xattr rb tree */
367 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
368 p = ci->i_xattrs.blob->vec.iov_base;
369 end = p + ci->i_xattrs.blob->vec.iov_len;
370 ceph_decode_32_safe(&p, end, numattr, bad);
371 xattr_version = ci->i_xattrs.version;
372 spin_unlock(&inode->i_lock);
374 xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
379 memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
380 for (i = 0; i < numattr; i++) {
381 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
387 spin_lock(&inode->i_lock);
388 if (ci->i_xattrs.version != xattr_version) {
389 /* lost a race, retry */
390 for (i = 0; i < numattr; i++)
397 ceph_decode_32_safe(&p, end, len, bad);
401 ceph_decode_32_safe(&p, end, len, bad);
405 err = __set_xattr(ci, name, namelen, val, len,
406 0, 0, 0, &xattrs[numattr]);
413 ci->i_xattrs.index_version = ci->i_xattrs.version;
414 ci->i_xattrs.dirty = false;
418 spin_lock(&inode->i_lock);
421 for (i = 0; i < numattr; i++)
425 ci->i_xattrs.names_size = 0;
429 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
433 * 4 bytes for the length, and additional 4 bytes per each xattr name,
434 * 4 bytes per each value
436 int size = 4 + ci->i_xattrs.count*(4 + 4) +
437 ci->i_xattrs.names_size +
438 ci->i_xattrs.vals_size;
439 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
440 ci->i_xattrs.count, ci->i_xattrs.names_size,
441 ci->i_xattrs.vals_size);
444 size += 4 + 4 + name_size + val_size;
450 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
451 * and swap into place.
453 void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
456 struct ceph_inode_xattr *xattr = NULL;
459 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
460 if (ci->i_xattrs.dirty) {
461 int need = __get_required_blob_size(ci, 0, 0);
463 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
465 p = rb_first(&ci->i_xattrs.index);
466 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
468 ceph_encode_32(&dest, ci->i_xattrs.count);
470 xattr = rb_entry(p, struct ceph_inode_xattr, node);
472 ceph_encode_32(&dest, xattr->name_len);
473 memcpy(dest, xattr->name, xattr->name_len);
474 dest += xattr->name_len;
475 ceph_encode_32(&dest, xattr->val_len);
476 memcpy(dest, xattr->val, xattr->val_len);
477 dest += xattr->val_len;
482 /* adjust buffer len; it may be larger than we need */
483 ci->i_xattrs.prealloc_blob->vec.iov_len =
484 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
486 if (ci->i_xattrs.blob)
487 ceph_buffer_put(ci->i_xattrs.blob);
488 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
489 ci->i_xattrs.prealloc_blob = NULL;
490 ci->i_xattrs.dirty = false;
494 ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
497 struct inode *inode = dentry->d_inode;
498 struct ceph_inode_info *ci = ceph_inode(inode);
499 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
501 struct ceph_inode_xattr *xattr;
502 struct ceph_vxattr_cb *vxattr = NULL;
504 if (!ceph_is_valid_xattr(name))
507 /* let's see if a virtual xattr was requested */
509 vxattr = ceph_match_vxattr(vxattrs, name);
511 spin_lock(&inode->i_lock);
512 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
513 ci->i_xattrs.version, ci->i_xattrs.index_version);
515 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
516 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
519 spin_unlock(&inode->i_lock);
520 /* get xattrs from mds (if we don't already have them) */
521 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
526 spin_lock(&inode->i_lock);
528 if (vxattr && vxattr->readonly) {
529 err = vxattr->getxattr_cb(ci, value, size);
533 err = __build_xattrs(inode);
538 err = -ENODATA; /* == ENOATTR */
539 xattr = __get_xattr(ci, name);
542 err = vxattr->getxattr_cb(ci, value, size);
547 if (size && size < xattr->val_len)
550 err = xattr->val_len;
554 memcpy(value, xattr->val, xattr->val_len);
557 spin_unlock(&inode->i_lock);
561 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
563 struct inode *inode = dentry->d_inode;
564 struct ceph_inode_info *ci = ceph_inode(inode);
565 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
572 spin_lock(&inode->i_lock);
573 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
574 ci->i_xattrs.version, ci->i_xattrs.index_version);
576 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
577 (ci->i_xattrs.index_version > ci->i_xattrs.version)) {
580 spin_unlock(&inode->i_lock);
581 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
586 spin_lock(&inode->i_lock);
588 err = __build_xattrs(inode);
594 /* include virtual dir xattrs */
596 for (i = 0; vxattrs[i].name; i++)
597 vir_namelen += strlen(vxattrs[i].name) + 1;
598 /* adding 1 byte per each variable due to the null termination */
599 namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
601 if (size && namelen > size)
608 names = __copy_xattr_names(ci, names);
610 /* virtual xattr names, too */
612 for (i = 0; vxattrs[i].name; i++) {
613 len = sprintf(names, "%s", vxattrs[i].name);
618 spin_unlock(&inode->i_lock);
622 static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
623 const char *value, size_t size, int flags)
625 struct ceph_client *client = ceph_client(dentry->d_sb);
626 struct inode *inode = dentry->d_inode;
627 struct ceph_inode_info *ci = ceph_inode(inode);
628 struct inode *parent_inode = dentry->d_parent->d_inode;
629 struct ceph_mds_request *req;
630 struct ceph_mds_client *mdsc = &client->mdsc;
633 struct page **pages = NULL;
636 /* copy value into some pages */
637 nr_pages = calc_pages_for(0, size);
639 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
643 for (i = 0; i < nr_pages; i++) {
644 pages[i] = alloc_page(GFP_NOFS);
649 kaddr = kmap(pages[i]);
650 memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
651 min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
655 dout("setxattr value=%.*s\n", (int)size, value);
658 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
664 req->r_inode = igrab(inode);
665 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
667 req->r_args.setxattr.flags = cpu_to_le32(flags);
668 req->r_path2 = kstrdup(name, GFP_NOFS);
670 req->r_pages = pages;
671 req->r_num_pages = nr_pages;
672 req->r_data_len = size;
674 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
675 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
676 ceph_mdsc_put_request(req);
677 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
681 for (i = 0; i < nr_pages; i++)
682 __free_page(pages[i]);
688 int ceph_setxattr(struct dentry *dentry, const char *name,
689 const void *value, size_t size, int flags)
691 struct inode *inode = dentry->d_inode;
692 struct ceph_inode_info *ci = ceph_inode(inode);
693 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
695 int name_len = strlen(name);
697 char *newname = NULL;
699 struct ceph_inode_xattr *xattr = NULL;
701 int required_blob_size;
703 if (ceph_snap(inode) != CEPH_NOSNAP)
706 if (!ceph_is_valid_xattr(name))
710 struct ceph_vxattr_cb *vxattr =
711 ceph_match_vxattr(vxattrs, name);
712 if (vxattr && vxattr->readonly)
716 /* preallocate memory for xattr name, value, index node */
718 newname = kmalloc(name_len + 1, GFP_NOFS);
721 memcpy(newname, name, name_len + 1);
724 newval = kmalloc(val_len + 1, GFP_NOFS);
727 memcpy(newval, value, val_len);
728 newval[val_len] = '\0';
731 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
735 spin_lock(&inode->i_lock);
737 issued = __ceph_caps_issued(ci, NULL);
738 if (!(issued & CEPH_CAP_XATTR_EXCL))
740 __build_xattrs(inode);
742 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
744 if (!ci->i_xattrs.prealloc_blob ||
745 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
746 struct ceph_buffer *blob = NULL;
748 spin_unlock(&inode->i_lock);
749 dout(" preaallocating new blob size=%d\n", required_blob_size);
750 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
753 spin_lock(&inode->i_lock);
754 if (ci->i_xattrs.prealloc_blob)
755 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
756 ci->i_xattrs.prealloc_blob = blob;
760 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
761 err = __set_xattr(ci, newname, name_len, newval,
762 val_len, 1, 1, 1, &xattr);
763 __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
764 ci->i_xattrs.dirty = true;
765 inode->i_ctime = CURRENT_TIME;
766 spin_unlock(&inode->i_lock);
771 spin_unlock(&inode->i_lock);
772 err = ceph_sync_setxattr(dentry, name, value, size, flags);
780 static int ceph_send_removexattr(struct dentry *dentry, const char *name)
782 struct ceph_client *client = ceph_client(dentry->d_sb);
783 struct ceph_mds_client *mdsc = &client->mdsc;
784 struct inode *inode = dentry->d_inode;
785 struct inode *parent_inode = dentry->d_parent->d_inode;
786 struct ceph_mds_request *req;
789 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
793 req->r_inode = igrab(inode);
794 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
796 req->r_path2 = kstrdup(name, GFP_NOFS);
798 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
799 ceph_mdsc_put_request(req);
803 int ceph_removexattr(struct dentry *dentry, const char *name)
805 struct inode *inode = dentry->d_inode;
806 struct ceph_inode_info *ci = ceph_inode(inode);
807 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
811 if (ceph_snap(inode) != CEPH_NOSNAP)
814 if (!ceph_is_valid_xattr(name))
818 struct ceph_vxattr_cb *vxattr =
819 ceph_match_vxattr(vxattrs, name);
820 if (vxattr && vxattr->readonly)
824 spin_lock(&inode->i_lock);
825 __build_xattrs(inode);
826 issued = __ceph_caps_issued(ci, NULL);
827 dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
829 if (!(issued & CEPH_CAP_XATTR_EXCL))
832 err = __remove_xattr_by_name(ceph_inode(inode), name);
833 __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
834 ci->i_xattrs.dirty = true;
835 inode->i_ctime = CURRENT_TIME;
837 spin_unlock(&inode->i_lock);
841 spin_unlock(&inode->i_lock);
842 err = ceph_send_removexattr(dentry, name);