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