ceph: remove 'ceph.layout' virtual xattr
[cascardo/linux.git] / fs / ceph / xattr.c
1 #include <linux/ceph/ceph_debug.h>
2
3 #include "super.h"
4 #include "mds_client.h"
5
6 #include <linux/ceph/decode.h>
7
8 #include <linux/xattr.h>
9 #include <linux/slab.h>
10
11 #define XATTR_CEPH_PREFIX "ceph."
12 #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
13
14 static bool ceph_is_valid_xattr(const char *name)
15 {
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);
21 }
22
23 /*
24  * These define virtual xattrs exposing the recursive directory
25  * statistics and layout metadata.
26  */
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;
33 };
34
35 /* directories */
36
37 static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
38                                         size_t size)
39 {
40         return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
41 }
42
43 static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
44                                       size_t size)
45 {
46         return snprintf(val, size, "%lld", ci->i_files);
47 }
48
49 static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
50                                         size_t size)
51 {
52         return snprintf(val, size, "%lld", ci->i_subdirs);
53 }
54
55 static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
56                                          size_t size)
57 {
58         return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
59 }
60
61 static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
62                                        size_t size)
63 {
64         return snprintf(val, size, "%lld", ci->i_rfiles);
65 }
66
67 static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
68                                          size_t size)
69 {
70         return snprintf(val, size, "%lld", ci->i_rsubdirs);
71 }
72
73 static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
74                                        size_t size)
75 {
76         return snprintf(val, size, "%lld", ci->i_rbytes);
77 }
78
79 static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
80                                        size_t size)
81 {
82         return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
83                         (long)ci->i_rctime.tv_nsec);
84 }
85
86 #define CEPH_XATTR_NAME(_type, _name)   XATTR_CEPH_PREFIX #_type "." #_name
87
88 #define XATTR_NAME_CEPH(_type, _name) \
89                 { \
90                         .name = CEPH_XATTR_NAME(_type, _name), \
91                         .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
92                         .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
93                         .readonly = true, \
94                 }
95
96 static struct ceph_vxattr ceph_dir_vxattrs[] = {
97         XATTR_NAME_CEPH(dir, entries),
98         XATTR_NAME_CEPH(dir, files),
99         XATTR_NAME_CEPH(dir, subdirs),
100         XATTR_NAME_CEPH(dir, rentries),
101         XATTR_NAME_CEPH(dir, rfiles),
102         XATTR_NAME_CEPH(dir, rsubdirs),
103         XATTR_NAME_CEPH(dir, rbytes),
104         XATTR_NAME_CEPH(dir, rctime),
105         { 0 }   /* Required table terminator */
106 };
107 static size_t ceph_dir_vxattrs_name_size;       /* total size of all names */
108
109 /* files */
110
111 static size_t ceph_vxattrcb_file_layout(struct ceph_inode_info *ci, char *val,
112                                    size_t size)
113 {
114         int ret;
115
116         ret = snprintf(val, size,
117                 "chunk_bytes=%lld\nstripe_count=%lld\nobject_size=%lld\n",
118                 (unsigned long long)ceph_file_layout_su(ci->i_layout),
119                 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
120                 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
121         return ret;
122 }
123
124 static struct ceph_vxattr ceph_file_vxattrs[] = {
125         XATTR_NAME_CEPH(file, layout),
126         { 0 }   /* Required table terminator */
127 };
128 static size_t ceph_file_vxattrs_name_size;      /* total size of all names */
129
130 static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
131 {
132         if (S_ISDIR(inode->i_mode))
133                 return ceph_dir_vxattrs;
134         else if (S_ISREG(inode->i_mode))
135                 return ceph_file_vxattrs;
136         return NULL;
137 }
138
139 static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
140 {
141         if (vxattrs == ceph_dir_vxattrs)
142                 return ceph_dir_vxattrs_name_size;
143         if (vxattrs == ceph_file_vxattrs)
144                 return ceph_file_vxattrs_name_size;
145         BUG();
146
147         return 0;
148 }
149
150 /*
151  * Compute the aggregate size (including terminating '\0') of all
152  * virtual extended attribute names in the given vxattr table.
153  */
154 static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
155 {
156         struct ceph_vxattr *vxattr;
157         size_t size = 0;
158
159         for (vxattr = vxattrs; vxattr->name; vxattr++)
160                 size += vxattr->name_size;
161
162         return size;
163 }
164
165 /* Routines called at initialization and exit time */
166
167 void __init ceph_xattr_init(void)
168 {
169         ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
170         ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
171 }
172
173 void ceph_xattr_exit(void)
174 {
175         ceph_dir_vxattrs_name_size = 0;
176         ceph_file_vxattrs_name_size = 0;
177 }
178
179 static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
180                                                 const char *name)
181 {
182         struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
183
184         if (vxattr) {
185                 while (vxattr->name) {
186                         if (!strcmp(vxattr->name, name))
187                                 return vxattr;
188                         vxattr++;
189                 }
190         }
191
192         return NULL;
193 }
194
195 static int __set_xattr(struct ceph_inode_info *ci,
196                            const char *name, int name_len,
197                            const char *val, int val_len,
198                            int dirty,
199                            int should_free_name, int should_free_val,
200                            struct ceph_inode_xattr **newxattr)
201 {
202         struct rb_node **p;
203         struct rb_node *parent = NULL;
204         struct ceph_inode_xattr *xattr = NULL;
205         int c;
206         int new = 0;
207
208         p = &ci->i_xattrs.index.rb_node;
209         while (*p) {
210                 parent = *p;
211                 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
212                 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
213                 if (c < 0)
214                         p = &(*p)->rb_left;
215                 else if (c > 0)
216                         p = &(*p)->rb_right;
217                 else {
218                         if (name_len == xattr->name_len)
219                                 break;
220                         else if (name_len < xattr->name_len)
221                                 p = &(*p)->rb_left;
222                         else
223                                 p = &(*p)->rb_right;
224                 }
225                 xattr = NULL;
226         }
227
228         if (!xattr) {
229                 new = 1;
230                 xattr = *newxattr;
231                 xattr->name = name;
232                 xattr->name_len = name_len;
233                 xattr->should_free_name = should_free_name;
234
235                 ci->i_xattrs.count++;
236                 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
237         } else {
238                 kfree(*newxattr);
239                 *newxattr = NULL;
240                 if (xattr->should_free_val)
241                         kfree((void *)xattr->val);
242
243                 if (should_free_name) {
244                         kfree((void *)name);
245                         name = xattr->name;
246                 }
247                 ci->i_xattrs.names_size -= xattr->name_len;
248                 ci->i_xattrs.vals_size -= xattr->val_len;
249         }
250         ci->i_xattrs.names_size += name_len;
251         ci->i_xattrs.vals_size += val_len;
252         if (val)
253                 xattr->val = val;
254         else
255                 xattr->val = "";
256
257         xattr->val_len = val_len;
258         xattr->dirty = dirty;
259         xattr->should_free_val = (val && should_free_val);
260
261         if (new) {
262                 rb_link_node(&xattr->node, parent, p);
263                 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
264                 dout("__set_xattr_val p=%p\n", p);
265         }
266
267         dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
268              ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
269
270         return 0;
271 }
272
273 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
274                            const char *name)
275 {
276         struct rb_node **p;
277         struct rb_node *parent = NULL;
278         struct ceph_inode_xattr *xattr = NULL;
279         int name_len = strlen(name);
280         int c;
281
282         p = &ci->i_xattrs.index.rb_node;
283         while (*p) {
284                 parent = *p;
285                 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
286                 c = strncmp(name, xattr->name, xattr->name_len);
287                 if (c == 0 && name_len > xattr->name_len)
288                         c = 1;
289                 if (c < 0)
290                         p = &(*p)->rb_left;
291                 else if (c > 0)
292                         p = &(*p)->rb_right;
293                 else {
294                         dout("__get_xattr %s: found %.*s\n", name,
295                              xattr->val_len, xattr->val);
296                         return xattr;
297                 }
298         }
299
300         dout("__get_xattr %s: not found\n", name);
301
302         return NULL;
303 }
304
305 static void __free_xattr(struct ceph_inode_xattr *xattr)
306 {
307         BUG_ON(!xattr);
308
309         if (xattr->should_free_name)
310                 kfree((void *)xattr->name);
311         if (xattr->should_free_val)
312                 kfree((void *)xattr->val);
313
314         kfree(xattr);
315 }
316
317 static int __remove_xattr(struct ceph_inode_info *ci,
318                           struct ceph_inode_xattr *xattr)
319 {
320         if (!xattr)
321                 return -EOPNOTSUPP;
322
323         rb_erase(&xattr->node, &ci->i_xattrs.index);
324
325         if (xattr->should_free_name)
326                 kfree((void *)xattr->name);
327         if (xattr->should_free_val)
328                 kfree((void *)xattr->val);
329
330         ci->i_xattrs.names_size -= xattr->name_len;
331         ci->i_xattrs.vals_size -= xattr->val_len;
332         ci->i_xattrs.count--;
333         kfree(xattr);
334
335         return 0;
336 }
337
338 static int __remove_xattr_by_name(struct ceph_inode_info *ci,
339                            const char *name)
340 {
341         struct rb_node **p;
342         struct ceph_inode_xattr *xattr;
343         int err;
344
345         p = &ci->i_xattrs.index.rb_node;
346         xattr = __get_xattr(ci, name);
347         err = __remove_xattr(ci, xattr);
348         return err;
349 }
350
351 static char *__copy_xattr_names(struct ceph_inode_info *ci,
352                                 char *dest)
353 {
354         struct rb_node *p;
355         struct ceph_inode_xattr *xattr = NULL;
356
357         p = rb_first(&ci->i_xattrs.index);
358         dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
359
360         while (p) {
361                 xattr = rb_entry(p, struct ceph_inode_xattr, node);
362                 memcpy(dest, xattr->name, xattr->name_len);
363                 dest[xattr->name_len] = '\0';
364
365                 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
366                      xattr->name_len, ci->i_xattrs.names_size);
367
368                 dest += xattr->name_len + 1;
369                 p = rb_next(p);
370         }
371
372         return dest;
373 }
374
375 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
376 {
377         struct rb_node *p, *tmp;
378         struct ceph_inode_xattr *xattr = NULL;
379
380         p = rb_first(&ci->i_xattrs.index);
381
382         dout("__ceph_destroy_xattrs p=%p\n", p);
383
384         while (p) {
385                 xattr = rb_entry(p, struct ceph_inode_xattr, node);
386                 tmp = p;
387                 p = rb_next(tmp);
388                 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
389                      xattr->name_len, xattr->name);
390                 rb_erase(tmp, &ci->i_xattrs.index);
391
392                 __free_xattr(xattr);
393         }
394
395         ci->i_xattrs.names_size = 0;
396         ci->i_xattrs.vals_size = 0;
397         ci->i_xattrs.index_version = 0;
398         ci->i_xattrs.count = 0;
399         ci->i_xattrs.index = RB_ROOT;
400 }
401
402 static int __build_xattrs(struct inode *inode)
403         __releases(ci->i_ceph_lock)
404         __acquires(ci->i_ceph_lock)
405 {
406         u32 namelen;
407         u32 numattr = 0;
408         void *p, *end;
409         u32 len;
410         const char *name, *val;
411         struct ceph_inode_info *ci = ceph_inode(inode);
412         int xattr_version;
413         struct ceph_inode_xattr **xattrs = NULL;
414         int err = 0;
415         int i;
416
417         dout("__build_xattrs() len=%d\n",
418              ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
419
420         if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
421                 return 0; /* already built */
422
423         __ceph_destroy_xattrs(ci);
424
425 start:
426         /* updated internal xattr rb tree */
427         if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
428                 p = ci->i_xattrs.blob->vec.iov_base;
429                 end = p + ci->i_xattrs.blob->vec.iov_len;
430                 ceph_decode_32_safe(&p, end, numattr, bad);
431                 xattr_version = ci->i_xattrs.version;
432                 spin_unlock(&ci->i_ceph_lock);
433
434                 xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
435                                  GFP_NOFS);
436                 err = -ENOMEM;
437                 if (!xattrs)
438                         goto bad_lock;
439                 memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
440                 for (i = 0; i < numattr; i++) {
441                         xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
442                                             GFP_NOFS);
443                         if (!xattrs[i])
444                                 goto bad_lock;
445                 }
446
447                 spin_lock(&ci->i_ceph_lock);
448                 if (ci->i_xattrs.version != xattr_version) {
449                         /* lost a race, retry */
450                         for (i = 0; i < numattr; i++)
451                                 kfree(xattrs[i]);
452                         kfree(xattrs);
453                         xattrs = NULL;
454                         goto start;
455                 }
456                 err = -EIO;
457                 while (numattr--) {
458                         ceph_decode_32_safe(&p, end, len, bad);
459                         namelen = len;
460                         name = p;
461                         p += len;
462                         ceph_decode_32_safe(&p, end, len, bad);
463                         val = p;
464                         p += len;
465
466                         err = __set_xattr(ci, name, namelen, val, len,
467                                           0, 0, 0, &xattrs[numattr]);
468
469                         if (err < 0)
470                                 goto bad;
471                 }
472                 kfree(xattrs);
473         }
474         ci->i_xattrs.index_version = ci->i_xattrs.version;
475         ci->i_xattrs.dirty = false;
476
477         return err;
478 bad_lock:
479         spin_lock(&ci->i_ceph_lock);
480 bad:
481         if (xattrs) {
482                 for (i = 0; i < numattr; i++)
483                         kfree(xattrs[i]);
484                 kfree(xattrs);
485         }
486         ci->i_xattrs.names_size = 0;
487         return err;
488 }
489
490 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
491                                     int val_size)
492 {
493         /*
494          * 4 bytes for the length, and additional 4 bytes per each xattr name,
495          * 4 bytes per each value
496          */
497         int size = 4 + ci->i_xattrs.count*(4 + 4) +
498                              ci->i_xattrs.names_size +
499                              ci->i_xattrs.vals_size;
500         dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
501              ci->i_xattrs.count, ci->i_xattrs.names_size,
502              ci->i_xattrs.vals_size);
503
504         if (name_size)
505                 size += 4 + 4 + name_size + val_size;
506
507         return size;
508 }
509
510 /*
511  * If there are dirty xattrs, reencode xattrs into the prealloc_blob
512  * and swap into place.
513  */
514 void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
515 {
516         struct rb_node *p;
517         struct ceph_inode_xattr *xattr = NULL;
518         void *dest;
519
520         dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
521         if (ci->i_xattrs.dirty) {
522                 int need = __get_required_blob_size(ci, 0, 0);
523
524                 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
525
526                 p = rb_first(&ci->i_xattrs.index);
527                 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
528
529                 ceph_encode_32(&dest, ci->i_xattrs.count);
530                 while (p) {
531                         xattr = rb_entry(p, struct ceph_inode_xattr, node);
532
533                         ceph_encode_32(&dest, xattr->name_len);
534                         memcpy(dest, xattr->name, xattr->name_len);
535                         dest += xattr->name_len;
536                         ceph_encode_32(&dest, xattr->val_len);
537                         memcpy(dest, xattr->val, xattr->val_len);
538                         dest += xattr->val_len;
539
540                         p = rb_next(p);
541                 }
542
543                 /* adjust buffer len; it may be larger than we need */
544                 ci->i_xattrs.prealloc_blob->vec.iov_len =
545                         dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
546
547                 if (ci->i_xattrs.blob)
548                         ceph_buffer_put(ci->i_xattrs.blob);
549                 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
550                 ci->i_xattrs.prealloc_blob = NULL;
551                 ci->i_xattrs.dirty = false;
552                 ci->i_xattrs.version++;
553         }
554 }
555
556 ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
557                       size_t size)
558 {
559         struct inode *inode = dentry->d_inode;
560         struct ceph_inode_info *ci = ceph_inode(inode);
561         int err;
562         struct ceph_inode_xattr *xattr;
563         struct ceph_vxattr *vxattr = NULL;
564
565         if (!ceph_is_valid_xattr(name))
566                 return -ENODATA;
567
568         /* let's see if a virtual xattr was requested */
569         vxattr = ceph_match_vxattr(inode, name);
570
571         spin_lock(&ci->i_ceph_lock);
572         dout("getxattr %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 get_xattr;
578         } else {
579                 spin_unlock(&ci->i_ceph_lock);
580                 /* get xattrs from mds (if we don't already have them) */
581                 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
582                 if (err)
583                         return err;
584         }
585
586         spin_lock(&ci->i_ceph_lock);
587
588         if (vxattr && vxattr->readonly) {
589                 err = vxattr->getxattr_cb(ci, value, size);
590                 goto out;
591         }
592
593         err = __build_xattrs(inode);
594         if (err < 0)
595                 goto out;
596
597 get_xattr:
598         err = -ENODATA;  /* == ENOATTR */
599         xattr = __get_xattr(ci, name);
600         if (!xattr) {
601                 if (vxattr)
602                         err = vxattr->getxattr_cb(ci, value, size);
603                 goto out;
604         }
605
606         err = -ERANGE;
607         if (size && size < xattr->val_len)
608                 goto out;
609
610         err = xattr->val_len;
611         if (size == 0)
612                 goto out;
613
614         memcpy(value, xattr->val, xattr->val_len);
615
616 out:
617         spin_unlock(&ci->i_ceph_lock);
618         return err;
619 }
620
621 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
622 {
623         struct inode *inode = dentry->d_inode;
624         struct ceph_inode_info *ci = ceph_inode(inode);
625         struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
626         u32 vir_namelen = 0;
627         u32 namelen;
628         int err;
629         u32 len;
630         int i;
631
632         spin_lock(&ci->i_ceph_lock);
633         dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
634              ci->i_xattrs.version, ci->i_xattrs.index_version);
635
636         if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
637             (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
638                 goto list_xattr;
639         } else {
640                 spin_unlock(&ci->i_ceph_lock);
641                 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
642                 if (err)
643                         return err;
644         }
645
646         spin_lock(&ci->i_ceph_lock);
647
648         err = __build_xattrs(inode);
649         if (err < 0)
650                 goto out;
651
652 list_xattr:
653         /*
654          * Start with virtual dir xattr names (if any) (including
655          * terminating '\0' characters for each).
656          */
657         vir_namelen = ceph_vxattrs_name_size(vxattrs);
658
659         /* adding 1 byte per each variable due to the null termination */
660         namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
661         err = -ERANGE;
662         if (size && namelen > size)
663                 goto out;
664
665         err = namelen;
666         if (size == 0)
667                 goto out;
668
669         names = __copy_xattr_names(ci, names);
670
671         /* virtual xattr names, too */
672         if (vxattrs)
673                 for (i = 0; vxattrs[i].name; i++) {
674                         len = sprintf(names, "%s", vxattrs[i].name);
675                         names += len + 1;
676                 }
677
678 out:
679         spin_unlock(&ci->i_ceph_lock);
680         return err;
681 }
682
683 static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
684                               const char *value, size_t size, int flags)
685 {
686         struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
687         struct inode *inode = dentry->d_inode;
688         struct ceph_inode_info *ci = ceph_inode(inode);
689         struct inode *parent_inode;
690         struct ceph_mds_request *req;
691         struct ceph_mds_client *mdsc = fsc->mdsc;
692         int err;
693         int i, nr_pages;
694         struct page **pages = NULL;
695         void *kaddr;
696
697         /* copy value into some pages */
698         nr_pages = calc_pages_for(0, size);
699         if (nr_pages) {
700                 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
701                 if (!pages)
702                         return -ENOMEM;
703                 err = -ENOMEM;
704                 for (i = 0; i < nr_pages; i++) {
705                         pages[i] = __page_cache_alloc(GFP_NOFS);
706                         if (!pages[i]) {
707                                 nr_pages = i;
708                                 goto out;
709                         }
710                         kaddr = kmap(pages[i]);
711                         memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
712                                min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
713                 }
714         }
715
716         dout("setxattr value=%.*s\n", (int)size, value);
717
718         /* do request */
719         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
720                                        USE_AUTH_MDS);
721         if (IS_ERR(req)) {
722                 err = PTR_ERR(req);
723                 goto out;
724         }
725         req->r_inode = inode;
726         ihold(inode);
727         req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
728         req->r_num_caps = 1;
729         req->r_args.setxattr.flags = cpu_to_le32(flags);
730         req->r_path2 = kstrdup(name, GFP_NOFS);
731
732         req->r_pages = pages;
733         req->r_num_pages = nr_pages;
734         req->r_data_len = size;
735
736         dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
737         parent_inode = ceph_get_dentry_parent_inode(dentry);
738         err = ceph_mdsc_do_request(mdsc, parent_inode, req);
739         iput(parent_inode);
740         ceph_mdsc_put_request(req);
741         dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
742
743 out:
744         if (pages) {
745                 for (i = 0; i < nr_pages; i++)
746                         __free_page(pages[i]);
747                 kfree(pages);
748         }
749         return err;
750 }
751
752 int ceph_setxattr(struct dentry *dentry, const char *name,
753                   const void *value, size_t size, int flags)
754 {
755         struct inode *inode = dentry->d_inode;
756         struct ceph_vxattr *vxattr;
757         struct ceph_inode_info *ci = ceph_inode(inode);
758         int issued;
759         int err;
760         int dirty;
761         int name_len = strlen(name);
762         int val_len = size;
763         char *newname = NULL;
764         char *newval = NULL;
765         struct ceph_inode_xattr *xattr = NULL;
766         int required_blob_size;
767
768         if (ceph_snap(inode) != CEPH_NOSNAP)
769                 return -EROFS;
770
771         if (!ceph_is_valid_xattr(name))
772                 return -EOPNOTSUPP;
773
774         vxattr = ceph_match_vxattr(inode, name);
775         if (vxattr && vxattr->readonly)
776                 return -EOPNOTSUPP;
777
778         /* preallocate memory for xattr name, value, index node */
779         err = -ENOMEM;
780         newname = kmemdup(name, name_len + 1, GFP_NOFS);
781         if (!newname)
782                 goto out;
783
784         if (val_len) {
785                 newval = kmemdup(value, val_len, GFP_NOFS);
786                 if (!newval)
787                         goto out;
788         }
789
790         xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
791         if (!xattr)
792                 goto out;
793
794         spin_lock(&ci->i_ceph_lock);
795 retry:
796         issued = __ceph_caps_issued(ci, NULL);
797         dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
798         if (!(issued & CEPH_CAP_XATTR_EXCL))
799                 goto do_sync;
800         __build_xattrs(inode);
801
802         required_blob_size = __get_required_blob_size(ci, name_len, val_len);
803
804         if (!ci->i_xattrs.prealloc_blob ||
805             required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
806                 struct ceph_buffer *blob;
807
808                 spin_unlock(&ci->i_ceph_lock);
809                 dout(" preaallocating new blob size=%d\n", required_blob_size);
810                 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
811                 if (!blob)
812                         goto out;
813                 spin_lock(&ci->i_ceph_lock);
814                 if (ci->i_xattrs.prealloc_blob)
815                         ceph_buffer_put(ci->i_xattrs.prealloc_blob);
816                 ci->i_xattrs.prealloc_blob = blob;
817                 goto retry;
818         }
819
820         err = __set_xattr(ci, newname, name_len, newval,
821                           val_len, 1, 1, 1, &xattr);
822
823         dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
824         ci->i_xattrs.dirty = true;
825         inode->i_ctime = CURRENT_TIME;
826
827         spin_unlock(&ci->i_ceph_lock);
828         if (dirty)
829                 __mark_inode_dirty(inode, dirty);
830         return err;
831
832 do_sync:
833         spin_unlock(&ci->i_ceph_lock);
834         err = ceph_sync_setxattr(dentry, name, value, size, flags);
835 out:
836         kfree(newname);
837         kfree(newval);
838         kfree(xattr);
839         return err;
840 }
841
842 static int ceph_send_removexattr(struct dentry *dentry, const char *name)
843 {
844         struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
845         struct ceph_mds_client *mdsc = fsc->mdsc;
846         struct inode *inode = dentry->d_inode;
847         struct inode *parent_inode;
848         struct ceph_mds_request *req;
849         int err;
850
851         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
852                                        USE_AUTH_MDS);
853         if (IS_ERR(req))
854                 return PTR_ERR(req);
855         req->r_inode = inode;
856         ihold(inode);
857         req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
858         req->r_num_caps = 1;
859         req->r_path2 = kstrdup(name, GFP_NOFS);
860
861         parent_inode = ceph_get_dentry_parent_inode(dentry);
862         err = ceph_mdsc_do_request(mdsc, parent_inode, req);
863         iput(parent_inode);
864         ceph_mdsc_put_request(req);
865         return err;
866 }
867
868 int ceph_removexattr(struct dentry *dentry, const char *name)
869 {
870         struct inode *inode = dentry->d_inode;
871         struct ceph_vxattr *vxattr;
872         struct ceph_inode_info *ci = ceph_inode(inode);
873         int issued;
874         int err;
875         int required_blob_size;
876         int dirty;
877
878         if (ceph_snap(inode) != CEPH_NOSNAP)
879                 return -EROFS;
880
881         if (!ceph_is_valid_xattr(name))
882                 return -EOPNOTSUPP;
883
884         vxattr = ceph_match_vxattr(inode, name);
885         if (vxattr && vxattr->readonly)
886                 return -EOPNOTSUPP;
887
888         err = -ENOMEM;
889         spin_lock(&ci->i_ceph_lock);
890 retry:
891         issued = __ceph_caps_issued(ci, NULL);
892         dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
893
894         if (!(issued & CEPH_CAP_XATTR_EXCL))
895                 goto do_sync;
896         __build_xattrs(inode);
897
898         required_blob_size = __get_required_blob_size(ci, 0, 0);
899
900         if (!ci->i_xattrs.prealloc_blob ||
901             required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
902                 struct ceph_buffer *blob;
903
904                 spin_unlock(&ci->i_ceph_lock);
905                 dout(" preaallocating new blob size=%d\n", required_blob_size);
906                 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
907                 if (!blob)
908                         goto out;
909                 spin_lock(&ci->i_ceph_lock);
910                 if (ci->i_xattrs.prealloc_blob)
911                         ceph_buffer_put(ci->i_xattrs.prealloc_blob);
912                 ci->i_xattrs.prealloc_blob = blob;
913                 goto retry;
914         }
915
916         err = __remove_xattr_by_name(ceph_inode(inode), name);
917
918         dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
919         ci->i_xattrs.dirty = true;
920         inode->i_ctime = CURRENT_TIME;
921         spin_unlock(&ci->i_ceph_lock);
922         if (dirty)
923                 __mark_inode_dirty(inode, dirty);
924         return err;
925 do_sync:
926         spin_unlock(&ci->i_ceph_lock);
927         err = ceph_send_removexattr(dentry, name);
928 out:
929         return err;
930 }
931