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