ceph: add exists_cb to vxattr struct
[linux-2.6/btrfs-unstable.git] / fs / ceph / xattr.c
blob06344da4e9687607e5357d78d22dcb506015cf8e
1 #include <linux/ceph/ceph_debug.h>
3 #include "super.h"
4 #include "mds_client.h"
6 #include <linux/ceph/decode.h>
8 #include <linux/xattr.h>
9 #include <linux/slab.h>
11 #define XATTR_CEPH_PREFIX "ceph."
12 #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
14 static bool ceph_is_valid_xattr(const char *name)
16 return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
17 !strncmp(name, XATTR_SECURITY_PREFIX,
18 XATTR_SECURITY_PREFIX_LEN) ||
19 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
20 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
24 * These define virtual xattrs exposing the recursive directory
25 * statistics and layout metadata.
27 struct ceph_vxattr {
28 char *name;
29 size_t name_size; /* strlen(name) + 1 (for '\0') */
30 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
31 size_t size);
32 bool readonly, hidden;
33 bool (*exists_cb)(struct ceph_inode_info *ci);
36 /* directories */
38 static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
39 size_t size)
41 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
44 static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
45 size_t size)
47 return snprintf(val, size, "%lld", ci->i_files);
50 static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
51 size_t size)
53 return snprintf(val, size, "%lld", ci->i_subdirs);
56 static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
57 size_t size)
59 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
62 static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
63 size_t size)
65 return snprintf(val, size, "%lld", ci->i_rfiles);
68 static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
69 size_t size)
71 return snprintf(val, size, "%lld", ci->i_rsubdirs);
74 static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
75 size_t size)
77 return snprintf(val, size, "%lld", ci->i_rbytes);
80 static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
81 size_t size)
83 return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
84 (long)ci->i_rctime.tv_nsec);
87 #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
89 #define XATTR_NAME_CEPH(_type, _name) \
90 { \
91 .name = CEPH_XATTR_NAME(_type, _name), \
92 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
93 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
94 .readonly = true, \
95 .hidden = false, \
96 .exists_cb = NULL, \
99 static struct ceph_vxattr ceph_dir_vxattrs[] = {
100 XATTR_NAME_CEPH(dir, entries),
101 XATTR_NAME_CEPH(dir, files),
102 XATTR_NAME_CEPH(dir, subdirs),
103 XATTR_NAME_CEPH(dir, rentries),
104 XATTR_NAME_CEPH(dir, rfiles),
105 XATTR_NAME_CEPH(dir, rsubdirs),
106 XATTR_NAME_CEPH(dir, rbytes),
107 XATTR_NAME_CEPH(dir, rctime),
108 { 0 } /* Required table terminator */
110 static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
112 /* files */
114 static size_t ceph_vxattrcb_file_layout(struct ceph_inode_info *ci, char *val,
115 size_t size)
117 int ret;
119 ret = snprintf(val, size,
120 "chunk_bytes=%lld\nstripe_count=%lld\nobject_size=%lld\n",
121 (unsigned long long)ceph_file_layout_su(ci->i_layout),
122 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
123 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
124 return ret;
127 static struct ceph_vxattr ceph_file_vxattrs[] = {
128 XATTR_NAME_CEPH(file, layout),
129 { 0 } /* Required table terminator */
131 static size_t ceph_file_vxattrs_name_size; /* total size of all names */
133 static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
135 if (S_ISDIR(inode->i_mode))
136 return ceph_dir_vxattrs;
137 else if (S_ISREG(inode->i_mode))
138 return ceph_file_vxattrs;
139 return NULL;
142 static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
144 if (vxattrs == ceph_dir_vxattrs)
145 return ceph_dir_vxattrs_name_size;
146 if (vxattrs == ceph_file_vxattrs)
147 return ceph_file_vxattrs_name_size;
148 BUG();
150 return 0;
154 * Compute the aggregate size (including terminating '\0') of all
155 * virtual extended attribute names in the given vxattr table.
157 static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
159 struct ceph_vxattr *vxattr;
160 size_t size = 0;
162 for (vxattr = vxattrs; vxattr->name; vxattr++)
163 if (!vxattr->hidden)
164 size += vxattr->name_size;
166 return size;
169 /* Routines called at initialization and exit time */
171 void __init ceph_xattr_init(void)
173 ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
174 ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
177 void ceph_xattr_exit(void)
179 ceph_dir_vxattrs_name_size = 0;
180 ceph_file_vxattrs_name_size = 0;
183 static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
184 const char *name)
186 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
188 if (vxattr) {
189 while (vxattr->name) {
190 if (!strcmp(vxattr->name, name))
191 return vxattr;
192 vxattr++;
196 return NULL;
199 static int __set_xattr(struct ceph_inode_info *ci,
200 const char *name, int name_len,
201 const char *val, int val_len,
202 int dirty,
203 int should_free_name, int should_free_val,
204 struct ceph_inode_xattr **newxattr)
206 struct rb_node **p;
207 struct rb_node *parent = NULL;
208 struct ceph_inode_xattr *xattr = NULL;
209 int c;
210 int new = 0;
212 p = &ci->i_xattrs.index.rb_node;
213 while (*p) {
214 parent = *p;
215 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
216 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
217 if (c < 0)
218 p = &(*p)->rb_left;
219 else if (c > 0)
220 p = &(*p)->rb_right;
221 else {
222 if (name_len == xattr->name_len)
223 break;
224 else if (name_len < xattr->name_len)
225 p = &(*p)->rb_left;
226 else
227 p = &(*p)->rb_right;
229 xattr = NULL;
232 if (!xattr) {
233 new = 1;
234 xattr = *newxattr;
235 xattr->name = name;
236 xattr->name_len = name_len;
237 xattr->should_free_name = should_free_name;
239 ci->i_xattrs.count++;
240 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
241 } else {
242 kfree(*newxattr);
243 *newxattr = NULL;
244 if (xattr->should_free_val)
245 kfree((void *)xattr->val);
247 if (should_free_name) {
248 kfree((void *)name);
249 name = xattr->name;
251 ci->i_xattrs.names_size -= xattr->name_len;
252 ci->i_xattrs.vals_size -= xattr->val_len;
254 ci->i_xattrs.names_size += name_len;
255 ci->i_xattrs.vals_size += val_len;
256 if (val)
257 xattr->val = val;
258 else
259 xattr->val = "";
261 xattr->val_len = val_len;
262 xattr->dirty = dirty;
263 xattr->should_free_val = (val && should_free_val);
265 if (new) {
266 rb_link_node(&xattr->node, parent, p);
267 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
268 dout("__set_xattr_val p=%p\n", p);
271 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
272 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
274 return 0;
277 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
278 const char *name)
280 struct rb_node **p;
281 struct rb_node *parent = NULL;
282 struct ceph_inode_xattr *xattr = NULL;
283 int name_len = strlen(name);
284 int c;
286 p = &ci->i_xattrs.index.rb_node;
287 while (*p) {
288 parent = *p;
289 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
290 c = strncmp(name, xattr->name, xattr->name_len);
291 if (c == 0 && name_len > xattr->name_len)
292 c = 1;
293 if (c < 0)
294 p = &(*p)->rb_left;
295 else if (c > 0)
296 p = &(*p)->rb_right;
297 else {
298 dout("__get_xattr %s: found %.*s\n", name,
299 xattr->val_len, xattr->val);
300 return xattr;
304 dout("__get_xattr %s: not found\n", name);
306 return NULL;
309 static void __free_xattr(struct ceph_inode_xattr *xattr)
311 BUG_ON(!xattr);
313 if (xattr->should_free_name)
314 kfree((void *)xattr->name);
315 if (xattr->should_free_val)
316 kfree((void *)xattr->val);
318 kfree(xattr);
321 static int __remove_xattr(struct ceph_inode_info *ci,
322 struct ceph_inode_xattr *xattr)
324 if (!xattr)
325 return -EOPNOTSUPP;
327 rb_erase(&xattr->node, &ci->i_xattrs.index);
329 if (xattr->should_free_name)
330 kfree((void *)xattr->name);
331 if (xattr->should_free_val)
332 kfree((void *)xattr->val);
334 ci->i_xattrs.names_size -= xattr->name_len;
335 ci->i_xattrs.vals_size -= xattr->val_len;
336 ci->i_xattrs.count--;
337 kfree(xattr);
339 return 0;
342 static int __remove_xattr_by_name(struct ceph_inode_info *ci,
343 const char *name)
345 struct rb_node **p;
346 struct ceph_inode_xattr *xattr;
347 int err;
349 p = &ci->i_xattrs.index.rb_node;
350 xattr = __get_xattr(ci, name);
351 err = __remove_xattr(ci, xattr);
352 return err;
355 static char *__copy_xattr_names(struct ceph_inode_info *ci,
356 char *dest)
358 struct rb_node *p;
359 struct ceph_inode_xattr *xattr = NULL;
361 p = rb_first(&ci->i_xattrs.index);
362 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
364 while (p) {
365 xattr = rb_entry(p, struct ceph_inode_xattr, node);
366 memcpy(dest, xattr->name, xattr->name_len);
367 dest[xattr->name_len] = '\0';
369 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
370 xattr->name_len, ci->i_xattrs.names_size);
372 dest += xattr->name_len + 1;
373 p = rb_next(p);
376 return dest;
379 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
381 struct rb_node *p, *tmp;
382 struct ceph_inode_xattr *xattr = NULL;
384 p = rb_first(&ci->i_xattrs.index);
386 dout("__ceph_destroy_xattrs p=%p\n", p);
388 while (p) {
389 xattr = rb_entry(p, struct ceph_inode_xattr, node);
390 tmp = p;
391 p = rb_next(tmp);
392 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
393 xattr->name_len, xattr->name);
394 rb_erase(tmp, &ci->i_xattrs.index);
396 __free_xattr(xattr);
399 ci->i_xattrs.names_size = 0;
400 ci->i_xattrs.vals_size = 0;
401 ci->i_xattrs.index_version = 0;
402 ci->i_xattrs.count = 0;
403 ci->i_xattrs.index = RB_ROOT;
406 static int __build_xattrs(struct inode *inode)
407 __releases(ci->i_ceph_lock)
408 __acquires(ci->i_ceph_lock)
410 u32 namelen;
411 u32 numattr = 0;
412 void *p, *end;
413 u32 len;
414 const char *name, *val;
415 struct ceph_inode_info *ci = ceph_inode(inode);
416 int xattr_version;
417 struct ceph_inode_xattr **xattrs = NULL;
418 int err = 0;
419 int i;
421 dout("__build_xattrs() len=%d\n",
422 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
424 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
425 return 0; /* already built */
427 __ceph_destroy_xattrs(ci);
429 start:
430 /* updated internal xattr rb tree */
431 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
432 p = ci->i_xattrs.blob->vec.iov_base;
433 end = p + ci->i_xattrs.blob->vec.iov_len;
434 ceph_decode_32_safe(&p, end, numattr, bad);
435 xattr_version = ci->i_xattrs.version;
436 spin_unlock(&ci->i_ceph_lock);
438 xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
439 GFP_NOFS);
440 err = -ENOMEM;
441 if (!xattrs)
442 goto bad_lock;
443 memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
444 for (i = 0; i < numattr; i++) {
445 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
446 GFP_NOFS);
447 if (!xattrs[i])
448 goto bad_lock;
451 spin_lock(&ci->i_ceph_lock);
452 if (ci->i_xattrs.version != xattr_version) {
453 /* lost a race, retry */
454 for (i = 0; i < numattr; i++)
455 kfree(xattrs[i]);
456 kfree(xattrs);
457 xattrs = NULL;
458 goto start;
460 err = -EIO;
461 while (numattr--) {
462 ceph_decode_32_safe(&p, end, len, bad);
463 namelen = len;
464 name = p;
465 p += len;
466 ceph_decode_32_safe(&p, end, len, bad);
467 val = p;
468 p += len;
470 err = __set_xattr(ci, name, namelen, val, len,
471 0, 0, 0, &xattrs[numattr]);
473 if (err < 0)
474 goto bad;
476 kfree(xattrs);
478 ci->i_xattrs.index_version = ci->i_xattrs.version;
479 ci->i_xattrs.dirty = false;
481 return err;
482 bad_lock:
483 spin_lock(&ci->i_ceph_lock);
484 bad:
485 if (xattrs) {
486 for (i = 0; i < numattr; i++)
487 kfree(xattrs[i]);
488 kfree(xattrs);
490 ci->i_xattrs.names_size = 0;
491 return err;
494 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
495 int val_size)
498 * 4 bytes for the length, and additional 4 bytes per each xattr name,
499 * 4 bytes per each value
501 int size = 4 + ci->i_xattrs.count*(4 + 4) +
502 ci->i_xattrs.names_size +
503 ci->i_xattrs.vals_size;
504 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
505 ci->i_xattrs.count, ci->i_xattrs.names_size,
506 ci->i_xattrs.vals_size);
508 if (name_size)
509 size += 4 + 4 + name_size + val_size;
511 return size;
515 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
516 * and swap into place.
518 void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
520 struct rb_node *p;
521 struct ceph_inode_xattr *xattr = NULL;
522 void *dest;
524 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
525 if (ci->i_xattrs.dirty) {
526 int need = __get_required_blob_size(ci, 0, 0);
528 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
530 p = rb_first(&ci->i_xattrs.index);
531 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
533 ceph_encode_32(&dest, ci->i_xattrs.count);
534 while (p) {
535 xattr = rb_entry(p, struct ceph_inode_xattr, node);
537 ceph_encode_32(&dest, xattr->name_len);
538 memcpy(dest, xattr->name, xattr->name_len);
539 dest += xattr->name_len;
540 ceph_encode_32(&dest, xattr->val_len);
541 memcpy(dest, xattr->val, xattr->val_len);
542 dest += xattr->val_len;
544 p = rb_next(p);
547 /* adjust buffer len; it may be larger than we need */
548 ci->i_xattrs.prealloc_blob->vec.iov_len =
549 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
551 if (ci->i_xattrs.blob)
552 ceph_buffer_put(ci->i_xattrs.blob);
553 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
554 ci->i_xattrs.prealloc_blob = NULL;
555 ci->i_xattrs.dirty = false;
556 ci->i_xattrs.version++;
560 ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
561 size_t size)
563 struct inode *inode = dentry->d_inode;
564 struct ceph_inode_info *ci = ceph_inode(inode);
565 int err;
566 struct ceph_inode_xattr *xattr;
567 struct ceph_vxattr *vxattr = NULL;
569 if (!ceph_is_valid_xattr(name))
570 return -ENODATA;
572 /* let's see if a virtual xattr was requested */
573 vxattr = ceph_match_vxattr(inode, name);
575 spin_lock(&ci->i_ceph_lock);
576 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
577 ci->i_xattrs.version, ci->i_xattrs.index_version);
579 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
580 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
581 goto get_xattr;
582 } else {
583 spin_unlock(&ci->i_ceph_lock);
584 /* get xattrs from mds (if we don't already have them) */
585 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
586 if (err)
587 return err;
590 spin_lock(&ci->i_ceph_lock);
592 if (vxattr && vxattr->readonly) {
593 err = vxattr->getxattr_cb(ci, value, size);
594 goto out;
597 err = __build_xattrs(inode);
598 if (err < 0)
599 goto out;
601 get_xattr:
602 err = -ENODATA; /* == ENOATTR */
603 xattr = __get_xattr(ci, name);
604 if (!xattr) {
605 if (vxattr)
606 err = vxattr->getxattr_cb(ci, value, size);
607 goto out;
610 err = -ERANGE;
611 if (size && size < xattr->val_len)
612 goto out;
614 err = xattr->val_len;
615 if (size == 0)
616 goto out;
618 memcpy(value, xattr->val, xattr->val_len);
620 out:
621 spin_unlock(&ci->i_ceph_lock);
622 return err;
625 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
627 struct inode *inode = dentry->d_inode;
628 struct ceph_inode_info *ci = ceph_inode(inode);
629 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
630 u32 vir_namelen = 0;
631 u32 namelen;
632 int err;
633 u32 len;
634 int i;
636 spin_lock(&ci->i_ceph_lock);
637 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
638 ci->i_xattrs.version, ci->i_xattrs.index_version);
640 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
641 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
642 goto list_xattr;
643 } else {
644 spin_unlock(&ci->i_ceph_lock);
645 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
646 if (err)
647 return err;
650 spin_lock(&ci->i_ceph_lock);
652 err = __build_xattrs(inode);
653 if (err < 0)
654 goto out;
656 list_xattr:
658 * Start with virtual dir xattr names (if any) (including
659 * terminating '\0' characters for each).
661 vir_namelen = ceph_vxattrs_name_size(vxattrs);
663 /* adding 1 byte per each variable due to the null termination */
664 namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
665 err = -ERANGE;
666 if (size && namelen > size)
667 goto out;
669 err = namelen;
670 if (size == 0)
671 goto out;
673 names = __copy_xattr_names(ci, names);
675 /* virtual xattr names, too */
676 if (vxattrs)
677 for (i = 0; vxattrs[i].name; i++) {
678 len = sprintf(names, "%s", vxattrs[i].name);
679 names += len + 1;
682 out:
683 spin_unlock(&ci->i_ceph_lock);
684 return err;
687 static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
688 const char *value, size_t size, int flags)
690 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
691 struct inode *inode = dentry->d_inode;
692 struct ceph_inode_info *ci = ceph_inode(inode);
693 struct inode *parent_inode;
694 struct ceph_mds_request *req;
695 struct ceph_mds_client *mdsc = fsc->mdsc;
696 int err;
697 int i, nr_pages;
698 struct page **pages = NULL;
699 void *kaddr;
701 /* copy value into some pages */
702 nr_pages = calc_pages_for(0, size);
703 if (nr_pages) {
704 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
705 if (!pages)
706 return -ENOMEM;
707 err = -ENOMEM;
708 for (i = 0; i < nr_pages; i++) {
709 pages[i] = __page_cache_alloc(GFP_NOFS);
710 if (!pages[i]) {
711 nr_pages = i;
712 goto out;
714 kaddr = kmap(pages[i]);
715 memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
716 min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
720 dout("setxattr value=%.*s\n", (int)size, value);
722 /* do request */
723 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
724 USE_AUTH_MDS);
725 if (IS_ERR(req)) {
726 err = PTR_ERR(req);
727 goto out;
729 req->r_inode = inode;
730 ihold(inode);
731 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
732 req->r_num_caps = 1;
733 req->r_args.setxattr.flags = cpu_to_le32(flags);
734 req->r_path2 = kstrdup(name, GFP_NOFS);
736 req->r_pages = pages;
737 req->r_num_pages = nr_pages;
738 req->r_data_len = size;
740 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
741 parent_inode = ceph_get_dentry_parent_inode(dentry);
742 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
743 iput(parent_inode);
744 ceph_mdsc_put_request(req);
745 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
747 out:
748 if (pages) {
749 for (i = 0; i < nr_pages; i++)
750 __free_page(pages[i]);
751 kfree(pages);
753 return err;
756 int ceph_setxattr(struct dentry *dentry, const char *name,
757 const void *value, size_t size, int flags)
759 struct inode *inode = dentry->d_inode;
760 struct ceph_vxattr *vxattr;
761 struct ceph_inode_info *ci = ceph_inode(inode);
762 int issued;
763 int err;
764 int dirty;
765 int name_len = strlen(name);
766 int val_len = size;
767 char *newname = NULL;
768 char *newval = NULL;
769 struct ceph_inode_xattr *xattr = NULL;
770 int required_blob_size;
772 if (ceph_snap(inode) != CEPH_NOSNAP)
773 return -EROFS;
775 if (!ceph_is_valid_xattr(name))
776 return -EOPNOTSUPP;
778 vxattr = ceph_match_vxattr(inode, name);
779 if (vxattr && vxattr->readonly)
780 return -EOPNOTSUPP;
782 /* pass any unhandled ceph.* xattrs through to the MDS */
783 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
784 goto do_sync_unlocked;
786 /* preallocate memory for xattr name, value, index node */
787 err = -ENOMEM;
788 newname = kmemdup(name, name_len + 1, GFP_NOFS);
789 if (!newname)
790 goto out;
792 if (val_len) {
793 newval = kmemdup(value, val_len, GFP_NOFS);
794 if (!newval)
795 goto out;
798 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
799 if (!xattr)
800 goto out;
802 spin_lock(&ci->i_ceph_lock);
803 retry:
804 issued = __ceph_caps_issued(ci, NULL);
805 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
806 if (!(issued & CEPH_CAP_XATTR_EXCL))
807 goto do_sync;
808 __build_xattrs(inode);
810 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
812 if (!ci->i_xattrs.prealloc_blob ||
813 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
814 struct ceph_buffer *blob;
816 spin_unlock(&ci->i_ceph_lock);
817 dout(" preaallocating new blob size=%d\n", required_blob_size);
818 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
819 if (!blob)
820 goto out;
821 spin_lock(&ci->i_ceph_lock);
822 if (ci->i_xattrs.prealloc_blob)
823 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
824 ci->i_xattrs.prealloc_blob = blob;
825 goto retry;
828 err = __set_xattr(ci, newname, name_len, newval,
829 val_len, 1, 1, 1, &xattr);
831 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
832 ci->i_xattrs.dirty = true;
833 inode->i_ctime = CURRENT_TIME;
835 spin_unlock(&ci->i_ceph_lock);
836 if (dirty)
837 __mark_inode_dirty(inode, dirty);
838 return err;
840 do_sync:
841 spin_unlock(&ci->i_ceph_lock);
842 do_sync_unlocked:
843 err = ceph_sync_setxattr(dentry, name, value, size, flags);
844 out:
845 kfree(newname);
846 kfree(newval);
847 kfree(xattr);
848 return err;
851 static int ceph_send_removexattr(struct dentry *dentry, const char *name)
853 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
854 struct ceph_mds_client *mdsc = fsc->mdsc;
855 struct inode *inode = dentry->d_inode;
856 struct inode *parent_inode;
857 struct ceph_mds_request *req;
858 int err;
860 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
861 USE_AUTH_MDS);
862 if (IS_ERR(req))
863 return PTR_ERR(req);
864 req->r_inode = inode;
865 ihold(inode);
866 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
867 req->r_num_caps = 1;
868 req->r_path2 = kstrdup(name, GFP_NOFS);
870 parent_inode = ceph_get_dentry_parent_inode(dentry);
871 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
872 iput(parent_inode);
873 ceph_mdsc_put_request(req);
874 return err;
877 int ceph_removexattr(struct dentry *dentry, const char *name)
879 struct inode *inode = dentry->d_inode;
880 struct ceph_vxattr *vxattr;
881 struct ceph_inode_info *ci = ceph_inode(inode);
882 int issued;
883 int err;
884 int required_blob_size;
885 int dirty;
887 if (ceph_snap(inode) != CEPH_NOSNAP)
888 return -EROFS;
890 if (!ceph_is_valid_xattr(name))
891 return -EOPNOTSUPP;
893 vxattr = ceph_match_vxattr(inode, name);
894 if (vxattr && vxattr->readonly)
895 return -EOPNOTSUPP;
897 /* pass any unhandled ceph.* xattrs through to the MDS */
898 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
899 goto do_sync_unlocked;
901 err = -ENOMEM;
902 spin_lock(&ci->i_ceph_lock);
903 retry:
904 issued = __ceph_caps_issued(ci, NULL);
905 dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
907 if (!(issued & CEPH_CAP_XATTR_EXCL))
908 goto do_sync;
909 __build_xattrs(inode);
911 required_blob_size = __get_required_blob_size(ci, 0, 0);
913 if (!ci->i_xattrs.prealloc_blob ||
914 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
915 struct ceph_buffer *blob;
917 spin_unlock(&ci->i_ceph_lock);
918 dout(" preaallocating new blob size=%d\n", required_blob_size);
919 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
920 if (!blob)
921 goto out;
922 spin_lock(&ci->i_ceph_lock);
923 if (ci->i_xattrs.prealloc_blob)
924 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
925 ci->i_xattrs.prealloc_blob = blob;
926 goto retry;
929 err = __remove_xattr_by_name(ceph_inode(inode), name);
931 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
932 ci->i_xattrs.dirty = true;
933 inode->i_ctime = CURRENT_TIME;
934 spin_unlock(&ci->i_ceph_lock);
935 if (dirty)
936 __mark_inode_dirty(inode, dirty);
937 return err;
938 do_sync:
939 spin_unlock(&ci->i_ceph_lock);
940 do_sync_unlocked:
941 err = ceph_send_removexattr(dentry, name);
942 out:
943 return err;