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