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