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