d940d14f692237e6e808d3c607725a1927807bc2
[safe/jmp/linux-2.6] / fs / ceph / xattr.c
1 #include "ceph_debug.h"
2 #include "super.h"
3 #include "decode.h"
4
5 #include <linux/xattr.h>
6 #include <linux/slab.h>
7
8 static bool ceph_is_valid_xattr(const char *name)
9 {
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);
14 }
15
16 /*
17  * These define virtual xattrs exposing the recursive directory
18  * statistics and layout metadata.
19  */
20 struct ceph_vxattr_cb {
21         bool readonly;
22         char *name;
23         size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
24                               size_t size);
25 };
26
27 /* directories */
28
29 static size_t ceph_vxattrcb_entries(struct ceph_inode_info *ci, char *val,
30                                         size_t size)
31 {
32         return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
33 }
34
35 static size_t ceph_vxattrcb_files(struct ceph_inode_info *ci, char *val,
36                                       size_t size)
37 {
38         return snprintf(val, size, "%lld", ci->i_files);
39 }
40
41 static size_t ceph_vxattrcb_subdirs(struct ceph_inode_info *ci, char *val,
42                                         size_t size)
43 {
44         return snprintf(val, size, "%lld", ci->i_subdirs);
45 }
46
47 static size_t ceph_vxattrcb_rentries(struct ceph_inode_info *ci, char *val,
48                                          size_t size)
49 {
50         return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
51 }
52
53 static size_t ceph_vxattrcb_rfiles(struct ceph_inode_info *ci, char *val,
54                                        size_t size)
55 {
56         return snprintf(val, size, "%lld", ci->i_rfiles);
57 }
58
59 static size_t ceph_vxattrcb_rsubdirs(struct ceph_inode_info *ci, char *val,
60                                          size_t size)
61 {
62         return snprintf(val, size, "%lld", ci->i_rsubdirs);
63 }
64
65 static size_t ceph_vxattrcb_rbytes(struct ceph_inode_info *ci, char *val,
66                                        size_t size)
67 {
68         return snprintf(val, size, "%lld", ci->i_rbytes);
69 }
70
71 static size_t ceph_vxattrcb_rctime(struct ceph_inode_info *ci, char *val,
72                                        size_t size)
73 {
74         return snprintf(val, size, "%ld.%ld", (long)ci->i_rctime.tv_sec,
75                         (long)ci->i_rctime.tv_nsec);
76 }
77
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},
87         { true, NULL, NULL }
88 };
89
90 /* files */
91
92 static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
93                                    size_t size)
94 {
95         int ret;
96
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(
105                                     ci->i_layout));
106         return ret;
107 }
108
109 static struct ceph_vxattr_cb ceph_file_vxattrs[] = {
110         { true, "user.ceph.layout", ceph_vxattrcb_layout},
111         { NULL, NULL }
112 };
113
114 static struct ceph_vxattr_cb *ceph_inode_vxattrs(struct inode *inode)
115 {
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;
120         return NULL;
121 }
122
123 static struct ceph_vxattr_cb *ceph_match_vxattr(struct ceph_vxattr_cb *vxattr,
124                                                 const char *name)
125 {
126         do {
127                 if (strcmp(vxattr->name, name) == 0)
128                         return vxattr;
129                 vxattr++;
130         } while (vxattr->name);
131         return NULL;
132 }
133
134 static int __set_xattr(struct ceph_inode_info *ci,
135                            const char *name, int name_len,
136                            const char *val, int val_len,
137                            int dirty,
138                            int should_free_name, int should_free_val,
139                            struct ceph_inode_xattr **newxattr)
140 {
141         struct rb_node **p;
142         struct rb_node *parent = NULL;
143         struct ceph_inode_xattr *xattr = NULL;
144         int c;
145         int new = 0;
146
147         p = &ci->i_xattrs.index.rb_node;
148         while (*p) {
149                 parent = *p;
150                 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
151                 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
152                 if (c < 0)
153                         p = &(*p)->rb_left;
154                 else if (c > 0)
155                         p = &(*p)->rb_right;
156                 else {
157                         if (name_len == xattr->name_len)
158                                 break;
159                         else if (name_len < xattr->name_len)
160                                 p = &(*p)->rb_left;
161                         else
162                                 p = &(*p)->rb_right;
163                 }
164                 xattr = NULL;
165         }
166
167         if (!xattr) {
168                 new = 1;
169                 xattr = *newxattr;
170                 xattr->name = name;
171                 xattr->name_len = name_len;
172                 xattr->should_free_name = should_free_name;
173
174                 ci->i_xattrs.count++;
175                 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
176         } else {
177                 kfree(*newxattr);
178                 *newxattr = NULL;
179                 if (xattr->should_free_val)
180                         kfree((void *)xattr->val);
181
182                 if (should_free_name) {
183                         kfree((void *)name);
184                         name = xattr->name;
185                 }
186                 ci->i_xattrs.names_size -= xattr->name_len;
187                 ci->i_xattrs.vals_size -= xattr->val_len;
188         }
189         ci->i_xattrs.names_size += name_len;
190         ci->i_xattrs.vals_size += val_len;
191         if (val)
192                 xattr->val = val;
193         else
194                 xattr->val = "";
195
196         xattr->val_len = val_len;
197         xattr->dirty = dirty;
198         xattr->should_free_val = (val && should_free_val);
199
200         if (new) {
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);
204         }
205
206         dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
207              ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
208
209         return 0;
210 }
211
212 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
213                            const char *name)
214 {
215         struct rb_node **p;
216         struct rb_node *parent = NULL;
217         struct ceph_inode_xattr *xattr = NULL;
218         int c;
219
220         p = &ci->i_xattrs.index.rb_node;
221         while (*p) {
222                 parent = *p;
223                 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
224                 c = strncmp(name, xattr->name, xattr->name_len);
225                 if (c < 0)
226                         p = &(*p)->rb_left;
227                 else if (c > 0)
228                         p = &(*p)->rb_right;
229                 else {
230                         dout("__get_xattr %s: found %.*s\n", name,
231                              xattr->val_len, xattr->val);
232                         return xattr;
233                 }
234         }
235
236         dout("__get_xattr %s: not found\n", name);
237
238         return NULL;
239 }
240
241 static void __free_xattr(struct ceph_inode_xattr *xattr)
242 {
243         BUG_ON(!xattr);
244
245         if (xattr->should_free_name)
246                 kfree((void *)xattr->name);
247         if (xattr->should_free_val)
248                 kfree((void *)xattr->val);
249
250         kfree(xattr);
251 }
252
253 static int __remove_xattr(struct ceph_inode_info *ci,
254                           struct ceph_inode_xattr *xattr)
255 {
256         if (!xattr)
257                 return -EOPNOTSUPP;
258
259         rb_erase(&xattr->node, &ci->i_xattrs.index);
260
261         if (xattr->should_free_name)
262                 kfree((void *)xattr->name);
263         if (xattr->should_free_val)
264                 kfree((void *)xattr->val);
265
266         ci->i_xattrs.names_size -= xattr->name_len;
267         ci->i_xattrs.vals_size -= xattr->val_len;
268         ci->i_xattrs.count--;
269         kfree(xattr);
270
271         return 0;
272 }
273
274 static int __remove_xattr_by_name(struct ceph_inode_info *ci,
275                            const char *name)
276 {
277         struct rb_node **p;
278         struct ceph_inode_xattr *xattr;
279         int err;
280
281         p = &ci->i_xattrs.index.rb_node;
282         xattr = __get_xattr(ci, name);
283         err = __remove_xattr(ci, xattr);
284         return err;
285 }
286
287 static char *__copy_xattr_names(struct ceph_inode_info *ci,
288                                 char *dest)
289 {
290         struct rb_node *p;
291         struct ceph_inode_xattr *xattr = NULL;
292
293         p = rb_first(&ci->i_xattrs.index);
294         dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
295
296         while (p) {
297                 xattr = rb_entry(p, struct ceph_inode_xattr, node);
298                 memcpy(dest, xattr->name, xattr->name_len);
299                 dest[xattr->name_len] = '\0';
300
301                 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
302                      xattr->name_len, ci->i_xattrs.names_size);
303
304                 dest += xattr->name_len + 1;
305                 p = rb_next(p);
306         }
307
308         return dest;
309 }
310
311 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
312 {
313         struct rb_node *p, *tmp;
314         struct ceph_inode_xattr *xattr = NULL;
315
316         p = rb_first(&ci->i_xattrs.index);
317
318         dout("__ceph_destroy_xattrs p=%p\n", p);
319
320         while (p) {
321                 xattr = rb_entry(p, struct ceph_inode_xattr, node);
322                 tmp = p;
323                 p = rb_next(tmp);
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);
327
328                 __free_xattr(xattr);
329         }
330
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;
336 }
337
338 static int __build_xattrs(struct inode *inode)
339 {
340         u32 namelen;
341         u32 numattr = 0;
342         void *p, *end;
343         u32 len;
344         const char *name, *val;
345         struct ceph_inode_info *ci = ceph_inode(inode);
346         int xattr_version;
347         struct ceph_inode_xattr **xattrs = NULL;
348         int err = 0;
349         int i;
350
351         dout("__build_xattrs() len=%d\n",
352              ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
353
354         if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
355                 return 0; /* already built */
356
357         __ceph_destroy_xattrs(ci);
358
359 start:
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);
367
368                 xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
369                                  GFP_NOFS);
370                 err = -ENOMEM;
371                 if (!xattrs)
372                         goto bad_lock;
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),
376                                             GFP_NOFS);
377                         if (!xattrs[i])
378                                 goto bad_lock;
379                 }
380
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++)
385                                 kfree(xattrs[i]);
386                         kfree(xattrs);
387                         goto start;
388                 }
389                 err = -EIO;
390                 while (numattr--) {
391                         ceph_decode_32_safe(&p, end, len, bad);
392                         namelen = len;
393                         name = p;
394                         p += len;
395                         ceph_decode_32_safe(&p, end, len, bad);
396                         val = p;
397                         p += len;
398
399                         err = __set_xattr(ci, name, namelen, val, len,
400                                           0, 0, 0, &xattrs[numattr]);
401
402                         if (err < 0)
403                                 goto bad;
404                 }
405                 kfree(xattrs);
406         }
407         ci->i_xattrs.index_version = ci->i_xattrs.version;
408         ci->i_xattrs.dirty = false;
409
410         return err;
411 bad_lock:
412         spin_lock(&inode->i_lock);
413 bad:
414         if (xattrs) {
415                 for (i = 0; i < numattr; i++)
416                         kfree(xattrs[i]);
417                 kfree(xattrs);
418         }
419         ci->i_xattrs.names_size = 0;
420         return err;
421 }
422
423 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
424                                     int val_size)
425 {
426         /*
427          * 4 bytes for the length, and additional 4 bytes per each xattr name,
428          * 4 bytes per each value
429          */
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);
436
437         if (name_size)
438                 size += 4 + 4 + name_size + val_size;
439
440         return size;
441 }
442
443 /*
444  * If there are dirty xattrs, reencode xattrs into the prealloc_blob
445  * and swap into place.
446  */
447 void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
448 {
449         struct rb_node *p;
450         struct ceph_inode_xattr *xattr = NULL;
451         void *dest;
452
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);
456
457                 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
458
459                 p = rb_first(&ci->i_xattrs.index);
460                 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
461
462                 ceph_encode_32(&dest, ci->i_xattrs.count);
463                 while (p) {
464                         xattr = rb_entry(p, struct ceph_inode_xattr, node);
465
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;
472
473                         p = rb_next(p);
474                 }
475
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;
479
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;
485         }
486 }
487
488 ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
489                       size_t size)
490 {
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);
494         int err;
495         struct ceph_inode_xattr *xattr;
496         struct ceph_vxattr_cb *vxattr = NULL;
497
498         if (!ceph_is_valid_xattr(name))
499                 return -ENODATA;
500
501         /* let's see if a virtual xattr was requested */
502         if (vxattrs)
503                 vxattr = ceph_match_vxattr(vxattrs, name);
504
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);
508
509         if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
510             (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
511                 goto get_xattr;
512         } else {
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);
516                 if (err)
517                         return err;
518         }
519
520         spin_lock(&inode->i_lock);
521
522         if (vxattr && vxattr->readonly) {
523                 err = vxattr->getxattr_cb(ci, value, size);
524                 goto out;
525         }
526
527         err = __build_xattrs(inode);
528         if (err < 0)
529                 goto out;
530
531 get_xattr:
532         err = -ENODATA;  /* == ENOATTR */
533         xattr = __get_xattr(ci, name);
534         if (!xattr) {
535                 if (vxattr)
536                         err = vxattr->getxattr_cb(ci, value, size);
537                 goto out;
538         }
539
540         err = -ERANGE;
541         if (size && size < xattr->val_len)
542                 goto out;
543
544         err = xattr->val_len;
545         if (size == 0)
546                 goto out;
547
548         memcpy(value, xattr->val, xattr->val_len);
549
550 out:
551         spin_unlock(&inode->i_lock);
552         return err;
553 }
554
555 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
556 {
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);
560         u32 vir_namelen = 0;
561         u32 namelen;
562         int err;
563         u32 len;
564         int i;
565
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);
569
570         if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
571             (ci->i_xattrs.index_version > ci->i_xattrs.version)) {
572                 goto list_xattr;
573         } else {
574                 spin_unlock(&inode->i_lock);
575                 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
576                 if (err)
577                         return err;
578         }
579
580         spin_lock(&inode->i_lock);
581
582         err = __build_xattrs(inode);
583         if (err < 0)
584                 goto out;
585
586 list_xattr:
587         vir_namelen = 0;
588         /* include virtual dir xattrs */
589         if (vxattrs)
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;
594         err = -ERANGE;
595         if (size && namelen > size)
596                 goto out;
597
598         err = namelen;
599         if (size == 0)
600                 goto out;
601
602         names = __copy_xattr_names(ci, names);
603
604         /* virtual xattr names, too */
605         if (vxattrs)
606                 for (i = 0; vxattrs[i].name; i++) {
607                         len = sprintf(names, "%s", vxattrs[i].name);
608                         names += len + 1;
609                 }
610
611 out:
612         spin_unlock(&inode->i_lock);
613         return err;
614 }
615
616 static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
617                               const char *value, size_t size, int flags)
618 {
619         struct ceph_client *client = ceph_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;
625         int err;
626         int i, nr_pages;
627         struct page **pages = NULL;
628         void *kaddr;
629
630         /* copy value into some pages */
631         nr_pages = calc_pages_for(0, size);
632         if (nr_pages) {
633                 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
634                 if (!pages)
635                         return -ENOMEM;
636                 err = -ENOMEM;
637                 for (i = 0; i < nr_pages; i++) {
638                         pages[i] = __page_cache_alloc(GFP_NOFS);
639                         if (!pages[i]) {
640                                 nr_pages = i;
641                                 goto out;
642                         }
643                         kaddr = kmap(pages[i]);
644                         memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
645                                min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
646                 }
647         }
648
649         dout("setxattr value=%.*s\n", (int)size, value);
650
651         /* do request */
652         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
653                                        USE_AUTH_MDS);
654         if (IS_ERR(req)) {
655                 err = PTR_ERR(req);
656                 goto out;
657         }
658         req->r_inode = igrab(inode);
659         req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
660         req->r_num_caps = 1;
661         req->r_args.setxattr.flags = cpu_to_le32(flags);
662         req->r_path2 = kstrdup(name, GFP_NOFS);
663
664         req->r_pages = pages;
665         req->r_num_pages = nr_pages;
666         req->r_data_len = size;
667
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);
672
673 out:
674         if (pages) {
675                 for (i = 0; i < nr_pages; i++)
676                         __free_page(pages[i]);
677                 kfree(pages);
678         }
679         return err;
680 }
681
682 int ceph_setxattr(struct dentry *dentry, const char *name,
683                   const void *value, size_t size, int flags)
684 {
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);
688         int err;
689         int name_len = strlen(name);
690         int val_len = size;
691         char *newname = NULL;
692         char *newval = NULL;
693         struct ceph_inode_xattr *xattr = NULL;
694         int issued;
695         int required_blob_size;
696
697         if (ceph_snap(inode) != CEPH_NOSNAP)
698                 return -EROFS;
699
700         if (!ceph_is_valid_xattr(name))
701                 return -EOPNOTSUPP;
702
703         if (vxattrs) {
704                 struct ceph_vxattr_cb *vxattr =
705                         ceph_match_vxattr(vxattrs, name);
706                 if (vxattr && vxattr->readonly)
707                         return -EOPNOTSUPP;
708         }
709
710         /* preallocate memory for xattr name, value, index node */
711         err = -ENOMEM;
712         newname = kmalloc(name_len + 1, GFP_NOFS);
713         if (!newname)
714                 goto out;
715         memcpy(newname, name, name_len + 1);
716
717         if (val_len) {
718                 newval = kmalloc(val_len + 1, GFP_NOFS);
719                 if (!newval)
720                         goto out;
721                 memcpy(newval, value, val_len);
722                 newval[val_len] = '\0';
723         }
724
725         xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
726         if (!xattr)
727                 goto out;
728
729         spin_lock(&inode->i_lock);
730 retry:
731         issued = __ceph_caps_issued(ci, NULL);
732         if (!(issued & CEPH_CAP_XATTR_EXCL))
733                 goto do_sync;
734         __build_xattrs(inode);
735
736         required_blob_size = __get_required_blob_size(ci, name_len, val_len);
737
738         if (!ci->i_xattrs.prealloc_blob ||
739             required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
740                 struct ceph_buffer *blob = NULL;
741
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);
745                 if (!blob)
746                         goto out;
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;
751                 goto retry;
752         }
753
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);
761
762         return err;
763
764 do_sync:
765         spin_unlock(&inode->i_lock);
766         err = ceph_sync_setxattr(dentry, name, value, size, flags);
767 out:
768         kfree(newname);
769         kfree(newval);
770         kfree(xattr);
771         return err;
772 }
773
774 static int ceph_send_removexattr(struct dentry *dentry, const char *name)
775 {
776         struct ceph_client *client = ceph_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;
781         int err;
782
783         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
784                                        USE_AUTH_MDS);
785         if (IS_ERR(req))
786                 return PTR_ERR(req);
787         req->r_inode = igrab(inode);
788         req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
789         req->r_num_caps = 1;
790         req->r_path2 = kstrdup(name, GFP_NOFS);
791
792         err = ceph_mdsc_do_request(mdsc, parent_inode, req);
793         ceph_mdsc_put_request(req);
794         return err;
795 }
796
797 int ceph_removexattr(struct dentry *dentry, const char *name)
798 {
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);
802         int issued;
803         int err;
804
805         if (ceph_snap(inode) != CEPH_NOSNAP)
806                 return -EROFS;
807
808         if (!ceph_is_valid_xattr(name))
809                 return -EOPNOTSUPP;
810
811         if (vxattrs) {
812                 struct ceph_vxattr_cb *vxattr =
813                         ceph_match_vxattr(vxattrs, name);
814                 if (vxattr && vxattr->readonly)
815                         return -EOPNOTSUPP;
816         }
817
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));
822
823         if (!(issued & CEPH_CAP_XATTR_EXCL))
824                 goto do_sync;
825
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;
830
831         spin_unlock(&inode->i_lock);
832
833         return err;
834 do_sync:
835         spin_unlock(&inode->i_lock);
836         err = ceph_send_removexattr(dentry, name);
837         return err;
838 }
839