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