1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3 #include <linux/ceph/pagelist.h>
6 #include "mds_client.h"
8 #include <linux/ceph/decode.h>
10 #include <linux/xattr.h>
11 #include <linux/security.h>
12 #include <linux/posix_acl_xattr.h>
13 #include <linux/slab.h>
15 #define XATTR_CEPH_PREFIX "ceph."
16 #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
18 static int __remove_xattr(struct ceph_inode_info *ci,
19 struct ceph_inode_xattr *xattr);
21 static bool ceph_is_valid_xattr(const char *name)
23 return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
24 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
25 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
29 * These define virtual xattrs exposing the recursive directory
30 * statistics and layout metadata.
34 size_t name_size; /* strlen(name) + 1 (for '\0') */
35 ssize_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
37 bool (*exists_cb)(struct ceph_inode_info *ci);
41 #define VXATTR_FLAG_READONLY (1<<0)
42 #define VXATTR_FLAG_HIDDEN (1<<1)
43 #define VXATTR_FLAG_RSTAT (1<<2)
47 static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
49 struct ceph_file_layout *fl = &ci->i_layout;
50 return (fl->stripe_unit > 0 || fl->stripe_count > 0 ||
51 fl->object_size > 0 || fl->pool_id >= 0 ||
52 rcu_dereference_raw(fl->pool_ns) != NULL);
55 static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
58 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
59 struct ceph_osd_client *osdc = &fsc->client->osdc;
60 struct ceph_string *pool_ns;
61 s64 pool = ci->i_layout.pool_id;
62 const char *pool_name;
63 const char *ns_field = " pool_namespace=";
65 size_t len, total_len = 0;
68 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
70 dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
71 down_read(&osdc->lock);
72 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
74 len = snprintf(buf, sizeof(buf),
75 "stripe_unit=%u stripe_count=%u object_size=%u pool=",
76 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
77 ci->i_layout.object_size);
78 total_len = len + strlen(pool_name);
80 len = snprintf(buf, sizeof(buf),
81 "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
82 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
83 ci->i_layout.object_size, pool);
88 total_len += strlen(ns_field) + pool_ns->len;
91 if (size >= total_len) {
92 memcpy(val, buf, len);
95 len = strlen(pool_name);
96 memcpy(val + ret, pool_name, len);
100 len = strlen(ns_field);
101 memcpy(val + ret, ns_field, len);
103 memcpy(val + ret, pool_ns->str, pool_ns->len);
107 up_read(&osdc->lock);
108 ceph_put_string(pool_ns);
113 * The convention with strings in xattrs is that they should not be NULL
114 * terminated, since we're returning the length with them. snprintf always
115 * NULL terminates however, so call it on a temporary buffer and then memcpy
116 * the result into place.
118 static int ceph_fmt_xattr(char *val, size_t size, const char *fmt, ...)
122 char buf[96]; /* NB: reevaluate size if new vxattrs are added */
125 ret = vsnprintf(buf, size ? sizeof(buf) : 0, fmt, args);
129 if (size && ret + 1 > sizeof(buf)) {
130 WARN_ONCE(true, "Returned length too big (%d)", ret);
135 memcpy(val, buf, ret);
139 static ssize_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
140 char *val, size_t size)
142 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_unit);
145 static ssize_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
146 char *val, size_t size)
148 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_count);
151 static ssize_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
152 char *val, size_t size)
154 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.object_size);
157 static ssize_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
158 char *val, size_t size)
161 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
162 struct ceph_osd_client *osdc = &fsc->client->osdc;
163 s64 pool = ci->i_layout.pool_id;
164 const char *pool_name;
166 down_read(&osdc->lock);
167 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
169 ret = strlen(pool_name);
171 memcpy(val, pool_name, ret);
173 ret = ceph_fmt_xattr(val, size, "%lld", pool);
175 up_read(&osdc->lock);
179 static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
180 char *val, size_t size)
183 struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
188 memcpy(val, ns->str, ret);
196 static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
199 return ceph_fmt_xattr(val, size, "%lld", ci->i_files + ci->i_subdirs);
202 static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
205 return ceph_fmt_xattr(val, size, "%lld", ci->i_files);
208 static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
211 return ceph_fmt_xattr(val, size, "%lld", ci->i_subdirs);
214 static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
217 return ceph_fmt_xattr(val, size, "%lld",
218 ci->i_rfiles + ci->i_rsubdirs);
221 static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
224 return ceph_fmt_xattr(val, size, "%lld", ci->i_rfiles);
227 static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
230 return ceph_fmt_xattr(val, size, "%lld", ci->i_rsubdirs);
233 static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
236 return ceph_fmt_xattr(val, size, "%lld", ci->i_rbytes);
239 static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
242 return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
243 ci->i_rctime.tv_nsec);
247 static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci)
249 return ci->i_dir_pin != -ENODATA;
252 static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val,
255 return ceph_fmt_xattr(val, size, "%d", (int)ci->i_dir_pin);
259 static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
262 spin_lock(&ci->i_ceph_lock);
263 if ((ci->i_max_files || ci->i_max_bytes) &&
264 ci->i_vino.snap == CEPH_NOSNAP &&
266 ci->i_snap_realm->ino == ci->i_vino.ino)
268 spin_unlock(&ci->i_ceph_lock);
272 static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
275 return ceph_fmt_xattr(val, size, "max_bytes=%llu max_files=%llu",
276 ci->i_max_bytes, ci->i_max_files);
279 static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
280 char *val, size_t size)
282 return ceph_fmt_xattr(val, size, "%llu", ci->i_max_bytes);
285 static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
286 char *val, size_t size)
288 return ceph_fmt_xattr(val, size, "%llu", ci->i_max_files);
292 static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci)
294 return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0);
297 static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val,
300 return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec,
301 ci->i_snap_btime.tv_nsec);
304 #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
305 #define CEPH_XATTR_NAME2(_type, _name, _name2) \
306 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
308 #define XATTR_NAME_CEPH(_type, _name, _flags) \
310 .name = CEPH_XATTR_NAME(_type, _name), \
311 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
312 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
314 .flags = (VXATTR_FLAG_READONLY | _flags), \
316 #define XATTR_RSTAT_FIELD(_type, _name) \
317 XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
318 #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
320 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
321 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
322 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
323 .exists_cb = ceph_vxattrcb_layout_exists, \
324 .flags = VXATTR_FLAG_HIDDEN, \
326 #define XATTR_QUOTA_FIELD(_type, _name) \
328 .name = CEPH_XATTR_NAME(_type, _name), \
329 .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
330 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
331 .exists_cb = ceph_vxattrcb_quota_exists, \
332 .flags = VXATTR_FLAG_HIDDEN, \
335 static struct ceph_vxattr ceph_dir_vxattrs[] = {
337 .name = "ceph.dir.layout",
338 .name_size = sizeof("ceph.dir.layout"),
339 .getxattr_cb = ceph_vxattrcb_layout,
340 .exists_cb = ceph_vxattrcb_layout_exists,
341 .flags = VXATTR_FLAG_HIDDEN,
343 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
344 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
345 XATTR_LAYOUT_FIELD(dir, layout, object_size),
346 XATTR_LAYOUT_FIELD(dir, layout, pool),
347 XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
348 XATTR_NAME_CEPH(dir, entries, 0),
349 XATTR_NAME_CEPH(dir, files, 0),
350 XATTR_NAME_CEPH(dir, subdirs, 0),
351 XATTR_RSTAT_FIELD(dir, rentries),
352 XATTR_RSTAT_FIELD(dir, rfiles),
353 XATTR_RSTAT_FIELD(dir, rsubdirs),
354 XATTR_RSTAT_FIELD(dir, rbytes),
355 XATTR_RSTAT_FIELD(dir, rctime),
357 .name = "ceph.dir.pin",
358 .name_size = sizeof("ceph.dir.pin"),
359 .getxattr_cb = ceph_vxattrcb_dir_pin,
360 .exists_cb = ceph_vxattrcb_dir_pin_exists,
361 .flags = VXATTR_FLAG_HIDDEN,
364 .name = "ceph.quota",
365 .name_size = sizeof("ceph.quota"),
366 .getxattr_cb = ceph_vxattrcb_quota,
367 .exists_cb = ceph_vxattrcb_quota_exists,
368 .flags = VXATTR_FLAG_HIDDEN,
370 XATTR_QUOTA_FIELD(quota, max_bytes),
371 XATTR_QUOTA_FIELD(quota, max_files),
373 .name = "ceph.snap.btime",
374 .name_size = sizeof("ceph.snap.btime"),
375 .getxattr_cb = ceph_vxattrcb_snap_btime,
376 .exists_cb = ceph_vxattrcb_snap_btime_exists,
377 .flags = VXATTR_FLAG_READONLY,
379 { .name = NULL, 0 } /* Required table terminator */
384 static struct ceph_vxattr ceph_file_vxattrs[] = {
386 .name = "ceph.file.layout",
387 .name_size = sizeof("ceph.file.layout"),
388 .getxattr_cb = ceph_vxattrcb_layout,
389 .exists_cb = ceph_vxattrcb_layout_exists,
390 .flags = VXATTR_FLAG_HIDDEN,
392 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
393 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
394 XATTR_LAYOUT_FIELD(file, layout, object_size),
395 XATTR_LAYOUT_FIELD(file, layout, pool),
396 XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
398 .name = "ceph.snap.btime",
399 .name_size = sizeof("ceph.snap.btime"),
400 .getxattr_cb = ceph_vxattrcb_snap_btime,
401 .exists_cb = ceph_vxattrcb_snap_btime_exists,
402 .flags = VXATTR_FLAG_READONLY,
404 { .name = NULL, 0 } /* Required table terminator */
407 static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
409 if (S_ISDIR(inode->i_mode))
410 return ceph_dir_vxattrs;
411 else if (S_ISREG(inode->i_mode))
412 return ceph_file_vxattrs;
416 static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
419 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
422 while (vxattr->name) {
423 if (!strcmp(vxattr->name, name))
432 static int __set_xattr(struct ceph_inode_info *ci,
433 const char *name, int name_len,
434 const char *val, int val_len,
435 int flags, int update_xattr,
436 struct ceph_inode_xattr **newxattr)
439 struct rb_node *parent = NULL;
440 struct ceph_inode_xattr *xattr = NULL;
444 p = &ci->i_xattrs.index.rb_node;
447 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
448 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
454 if (name_len == xattr->name_len)
456 else if (name_len < xattr->name_len)
467 if (xattr && (flags & XATTR_CREATE))
469 else if (!xattr && (flags & XATTR_REPLACE))
477 if (update_xattr < 0) {
479 __remove_xattr(ci, xattr);
490 xattr->name_len = name_len;
491 xattr->should_free_name = update_xattr;
493 ci->i_xattrs.count++;
494 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
498 if (xattr->should_free_val)
499 kfree((void *)xattr->val);
505 ci->i_xattrs.names_size -= xattr->name_len;
506 ci->i_xattrs.vals_size -= xattr->val_len;
508 ci->i_xattrs.names_size += name_len;
509 ci->i_xattrs.vals_size += val_len;
515 xattr->val_len = val_len;
516 xattr->dirty = update_xattr;
517 xattr->should_free_val = (val && update_xattr);
520 rb_link_node(&xattr->node, parent, p);
521 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
522 dout("__set_xattr_val p=%p\n", p);
525 dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n",
526 ceph_vinop(&ci->vfs_inode), xattr, name_len, name, val_len, val);
531 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
535 struct rb_node *parent = NULL;
536 struct ceph_inode_xattr *xattr = NULL;
537 int name_len = strlen(name);
540 p = &ci->i_xattrs.index.rb_node;
543 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
544 c = strncmp(name, xattr->name, xattr->name_len);
545 if (c == 0 && name_len > xattr->name_len)
552 dout("__get_xattr %s: found %.*s\n", name,
553 xattr->val_len, xattr->val);
558 dout("__get_xattr %s: not found\n", name);
563 static void __free_xattr(struct ceph_inode_xattr *xattr)
567 if (xattr->should_free_name)
568 kfree((void *)xattr->name);
569 if (xattr->should_free_val)
570 kfree((void *)xattr->val);
575 static int __remove_xattr(struct ceph_inode_info *ci,
576 struct ceph_inode_xattr *xattr)
581 rb_erase(&xattr->node, &ci->i_xattrs.index);
583 if (xattr->should_free_name)
584 kfree((void *)xattr->name);
585 if (xattr->should_free_val)
586 kfree((void *)xattr->val);
588 ci->i_xattrs.names_size -= xattr->name_len;
589 ci->i_xattrs.vals_size -= xattr->val_len;
590 ci->i_xattrs.count--;
596 static char *__copy_xattr_names(struct ceph_inode_info *ci,
600 struct ceph_inode_xattr *xattr = NULL;
602 p = rb_first(&ci->i_xattrs.index);
603 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
606 xattr = rb_entry(p, struct ceph_inode_xattr, node);
607 memcpy(dest, xattr->name, xattr->name_len);
608 dest[xattr->name_len] = '\0';
610 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
611 xattr->name_len, ci->i_xattrs.names_size);
613 dest += xattr->name_len + 1;
620 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
622 struct rb_node *p, *tmp;
623 struct ceph_inode_xattr *xattr = NULL;
625 p = rb_first(&ci->i_xattrs.index);
627 dout("__ceph_destroy_xattrs p=%p\n", p);
630 xattr = rb_entry(p, struct ceph_inode_xattr, node);
633 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
634 xattr->name_len, xattr->name);
635 rb_erase(tmp, &ci->i_xattrs.index);
640 ci->i_xattrs.names_size = 0;
641 ci->i_xattrs.vals_size = 0;
642 ci->i_xattrs.index_version = 0;
643 ci->i_xattrs.count = 0;
644 ci->i_xattrs.index = RB_ROOT;
647 static int __build_xattrs(struct inode *inode)
648 __releases(ci->i_ceph_lock)
649 __acquires(ci->i_ceph_lock)
655 const char *name, *val;
656 struct ceph_inode_info *ci = ceph_inode(inode);
658 struct ceph_inode_xattr **xattrs = NULL;
662 dout("__build_xattrs() len=%d\n",
663 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
665 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
666 return 0; /* already built */
668 __ceph_destroy_xattrs(ci);
671 /* updated internal xattr rb tree */
672 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
673 p = ci->i_xattrs.blob->vec.iov_base;
674 end = p + ci->i_xattrs.blob->vec.iov_len;
675 ceph_decode_32_safe(&p, end, numattr, bad);
676 xattr_version = ci->i_xattrs.version;
677 spin_unlock(&ci->i_ceph_lock);
679 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
685 for (i = 0; i < numattr; i++) {
686 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
692 spin_lock(&ci->i_ceph_lock);
693 if (ci->i_xattrs.version != xattr_version) {
694 /* lost a race, retry */
695 for (i = 0; i < numattr; i++)
703 ceph_decode_32_safe(&p, end, len, bad);
707 ceph_decode_32_safe(&p, end, len, bad);
711 err = __set_xattr(ci, name, namelen, val, len,
712 0, 0, &xattrs[numattr]);
719 ci->i_xattrs.index_version = ci->i_xattrs.version;
720 ci->i_xattrs.dirty = false;
724 spin_lock(&ci->i_ceph_lock);
727 for (i = 0; i < numattr; i++)
731 ci->i_xattrs.names_size = 0;
735 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
739 * 4 bytes for the length, and additional 4 bytes per each xattr name,
740 * 4 bytes per each value
742 int size = 4 + ci->i_xattrs.count*(4 + 4) +
743 ci->i_xattrs.names_size +
744 ci->i_xattrs.vals_size;
745 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
746 ci->i_xattrs.count, ci->i_xattrs.names_size,
747 ci->i_xattrs.vals_size);
750 size += 4 + 4 + name_size + val_size;
756 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
757 * and swap into place.
759 void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
762 struct ceph_inode_xattr *xattr = NULL;
765 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
766 if (ci->i_xattrs.dirty) {
767 int need = __get_required_blob_size(ci, 0, 0);
769 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
771 p = rb_first(&ci->i_xattrs.index);
772 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
774 ceph_encode_32(&dest, ci->i_xattrs.count);
776 xattr = rb_entry(p, struct ceph_inode_xattr, node);
778 ceph_encode_32(&dest, xattr->name_len);
779 memcpy(dest, xattr->name, xattr->name_len);
780 dest += xattr->name_len;
781 ceph_encode_32(&dest, xattr->val_len);
782 memcpy(dest, xattr->val, xattr->val_len);
783 dest += xattr->val_len;
788 /* adjust buffer len; it may be larger than we need */
789 ci->i_xattrs.prealloc_blob->vec.iov_len =
790 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
792 if (ci->i_xattrs.blob)
793 ceph_buffer_put(ci->i_xattrs.blob);
794 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
795 ci->i_xattrs.prealloc_blob = NULL;
796 ci->i_xattrs.dirty = false;
797 ci->i_xattrs.version++;
801 static inline int __get_request_mask(struct inode *in) {
802 struct ceph_mds_request *req = current->journal_info;
804 if (req && req->r_target_inode == in) {
805 if (req->r_op == CEPH_MDS_OP_LOOKUP ||
806 req->r_op == CEPH_MDS_OP_LOOKUPINO ||
807 req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
808 req->r_op == CEPH_MDS_OP_GETATTR) {
809 mask = le32_to_cpu(req->r_args.getattr.mask);
810 } else if (req->r_op == CEPH_MDS_OP_OPEN ||
811 req->r_op == CEPH_MDS_OP_CREATE) {
812 mask = le32_to_cpu(req->r_args.open.mask);
818 ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
821 struct ceph_inode_info *ci = ceph_inode(inode);
822 struct ceph_inode_xattr *xattr;
823 struct ceph_vxattr *vxattr = NULL;
827 /* let's see if a virtual xattr was requested */
828 vxattr = ceph_match_vxattr(inode, name);
831 if (vxattr->flags & VXATTR_FLAG_RSTAT)
832 mask |= CEPH_STAT_RSTAT;
833 err = ceph_do_getattr(inode, mask, true);
837 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
838 err = vxattr->getxattr_cb(ci, value, size);
839 if (size && size < err)
845 req_mask = __get_request_mask(inode);
847 spin_lock(&ci->i_ceph_lock);
848 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
849 ci->i_xattrs.version, ci->i_xattrs.index_version);
851 if (ci->i_xattrs.version == 0 ||
852 !((req_mask & CEPH_CAP_XATTR_SHARED) ||
853 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
854 spin_unlock(&ci->i_ceph_lock);
856 /* security module gets xattr while filling trace */
857 if (current->journal_info) {
858 pr_warn_ratelimited("sync getxattr %p "
859 "during filling trace\n", inode);
863 /* get xattrs from mds (if we don't already have them) */
864 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
867 spin_lock(&ci->i_ceph_lock);
870 err = __build_xattrs(inode);
874 err = -ENODATA; /* == ENOATTR */
875 xattr = __get_xattr(ci, name);
880 if (size && size < xattr->val_len)
883 err = xattr->val_len;
887 memcpy(value, xattr->val, xattr->val_len);
889 if (current->journal_info &&
890 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
891 ci->i_ceph_flags |= CEPH_I_SEC_INITED;
893 spin_unlock(&ci->i_ceph_lock);
897 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
899 struct inode *inode = d_inode(dentry);
900 struct ceph_inode_info *ci = ceph_inode(inode);
901 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
902 bool len_only = (size == 0);
907 spin_lock(&ci->i_ceph_lock);
908 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
909 ci->i_xattrs.version, ci->i_xattrs.index_version);
911 if (ci->i_xattrs.version == 0 ||
912 !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
913 spin_unlock(&ci->i_ceph_lock);
914 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
917 spin_lock(&ci->i_ceph_lock);
920 err = __build_xattrs(inode);
924 /* add 1 byte for each xattr due to the null termination */
925 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
927 if (namelen > size) {
931 names = __copy_xattr_names(ci, names);
936 /* virtual xattr names, too */
938 for (i = 0; vxattrs[i].name; i++) {
941 if (vxattrs[i].flags & VXATTR_FLAG_HIDDEN)
943 if (vxattrs[i].exists_cb && !vxattrs[i].exists_cb(ci))
946 this_len = strlen(vxattrs[i].name) + 1;
951 if (this_len > size) {
956 memcpy(names, vxattrs[i].name, this_len);
963 spin_unlock(&ci->i_ceph_lock);
967 static int ceph_sync_setxattr(struct inode *inode, const char *name,
968 const char *value, size_t size, int flags)
970 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
971 struct ceph_inode_info *ci = ceph_inode(inode);
972 struct ceph_mds_request *req;
973 struct ceph_mds_client *mdsc = fsc->mdsc;
974 struct ceph_pagelist *pagelist = NULL;
975 int op = CEPH_MDS_OP_SETXATTR;
979 /* copy value into pagelist */
980 pagelist = ceph_pagelist_alloc(GFP_NOFS);
984 err = ceph_pagelist_append(pagelist, value, size);
988 if (flags & CEPH_XATTR_REPLACE)
989 op = CEPH_MDS_OP_RMXATTR;
991 flags |= CEPH_XATTR_REMOVE;
994 dout("setxattr value=%.*s\n", (int)size, value);
997 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1003 req->r_path2 = kstrdup(name, GFP_NOFS);
1004 if (!req->r_path2) {
1005 ceph_mdsc_put_request(req);
1010 if (op == CEPH_MDS_OP_SETXATTR) {
1011 req->r_args.setxattr.flags = cpu_to_le32(flags);
1012 req->r_pagelist = pagelist;
1016 req->r_inode = inode;
1018 req->r_num_caps = 1;
1019 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1021 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
1022 err = ceph_mdsc_do_request(mdsc, NULL, req);
1023 ceph_mdsc_put_request(req);
1024 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
1028 ceph_pagelist_release(pagelist);
1032 int __ceph_setxattr(struct inode *inode, const char *name,
1033 const void *value, size_t size, int flags)
1035 struct ceph_vxattr *vxattr;
1036 struct ceph_inode_info *ci = ceph_inode(inode);
1037 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1038 struct ceph_cap_flush *prealloc_cf = NULL;
1042 int name_len = strlen(name);
1044 char *newname = NULL;
1045 char *newval = NULL;
1046 struct ceph_inode_xattr *xattr = NULL;
1047 int required_blob_size;
1048 bool check_realm = false;
1049 bool lock_snap_rwsem = false;
1051 if (ceph_snap(inode) != CEPH_NOSNAP)
1054 vxattr = ceph_match_vxattr(inode, name);
1056 if (vxattr->flags & VXATTR_FLAG_READONLY)
1058 if (value && !strncmp(vxattr->name, "ceph.quota", 10))
1062 /* pass any unhandled ceph.* xattrs through to the MDS */
1063 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1064 goto do_sync_unlocked;
1066 /* preallocate memory for xattr name, value, index node */
1068 newname = kmemdup(name, name_len + 1, GFP_NOFS);
1073 newval = kmemdup(value, val_len, GFP_NOFS);
1078 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1082 prealloc_cf = ceph_alloc_cap_flush();
1086 spin_lock(&ci->i_ceph_lock);
1088 issued = __ceph_caps_issued(ci, NULL);
1089 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
1092 if (!lock_snap_rwsem && !ci->i_head_snapc) {
1093 lock_snap_rwsem = true;
1094 if (!down_read_trylock(&mdsc->snap_rwsem)) {
1095 spin_unlock(&ci->i_ceph_lock);
1096 down_read(&mdsc->snap_rwsem);
1097 spin_lock(&ci->i_ceph_lock);
1102 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
1103 __build_xattrs(inode);
1105 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1107 if (!ci->i_xattrs.prealloc_blob ||
1108 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
1109 struct ceph_buffer *blob;
1111 spin_unlock(&ci->i_ceph_lock);
1112 dout(" preaallocating new blob size=%d\n", required_blob_size);
1113 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
1115 goto do_sync_unlocked;
1116 spin_lock(&ci->i_ceph_lock);
1117 if (ci->i_xattrs.prealloc_blob)
1118 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
1119 ci->i_xattrs.prealloc_blob = blob;
1123 err = __set_xattr(ci, newname, name_len, newval, val_len,
1124 flags, value ? 1 : -1, &xattr);
1127 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1129 ci->i_xattrs.dirty = true;
1130 inode->i_ctime = current_time(inode);
1133 spin_unlock(&ci->i_ceph_lock);
1134 if (lock_snap_rwsem)
1135 up_read(&mdsc->snap_rwsem);
1137 __mark_inode_dirty(inode, dirty);
1138 ceph_free_cap_flush(prealloc_cf);
1142 spin_unlock(&ci->i_ceph_lock);
1144 if (lock_snap_rwsem)
1145 up_read(&mdsc->snap_rwsem);
1147 /* security module set xattr while filling trace */
1148 if (current->journal_info) {
1149 pr_warn_ratelimited("sync setxattr %p "
1150 "during filling trace\n", inode);
1153 err = ceph_sync_setxattr(inode, name, value, size, flags);
1154 if (err >= 0 && check_realm) {
1155 /* check if snaprealm was created for quota inode */
1156 spin_lock(&ci->i_ceph_lock);
1157 if ((ci->i_max_files || ci->i_max_bytes) &&
1158 !(ci->i_snap_realm &&
1159 ci->i_snap_realm->ino == ci->i_vino.ino))
1161 spin_unlock(&ci->i_ceph_lock);
1165 ceph_free_cap_flush(prealloc_cf);
1172 static int ceph_get_xattr_handler(const struct xattr_handler *handler,
1173 struct dentry *dentry, struct inode *inode,
1174 const char *name, void *value, size_t size)
1176 if (!ceph_is_valid_xattr(name))
1178 return __ceph_getxattr(inode, name, value, size);
1181 static int ceph_set_xattr_handler(const struct xattr_handler *handler,
1182 struct dentry *unused, struct inode *inode,
1183 const char *name, const void *value,
1184 size_t size, int flags)
1186 if (!ceph_is_valid_xattr(name))
1188 return __ceph_setxattr(inode, name, value, size, flags);
1191 static const struct xattr_handler ceph_other_xattr_handler = {
1192 .prefix = "", /* match any name => handlers called with full name */
1193 .get = ceph_get_xattr_handler,
1194 .set = ceph_set_xattr_handler,
1197 #ifdef CONFIG_SECURITY
1198 bool ceph_security_xattr_wanted(struct inode *in)
1200 return in->i_security != NULL;
1203 bool ceph_security_xattr_deadlock(struct inode *in)
1205 struct ceph_inode_info *ci;
1207 if (!in->i_security)
1209 ci = ceph_inode(in);
1210 spin_lock(&ci->i_ceph_lock);
1211 ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1212 !(ci->i_xattrs.version > 0 &&
1213 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1214 spin_unlock(&ci->i_ceph_lock);
1218 #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1219 int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
1220 struct ceph_acl_sec_ctx *as_ctx)
1222 struct ceph_pagelist *pagelist = as_ctx->pagelist;
1227 err = security_dentry_init_security(dentry, mode, &dentry->d_name,
1229 &as_ctx->sec_ctxlen);
1231 WARN_ON_ONCE(err != -EOPNOTSUPP);
1232 err = 0; /* do nothing */
1238 pagelist = ceph_pagelist_alloc(GFP_KERNEL);
1241 err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
1244 ceph_pagelist_encode_32(pagelist, 1);
1248 * FIXME: Make security_dentry_init_security() generic. Currently
1249 * It only supports single security module and only selinux has
1250 * dentry_init_security hook.
1252 name = XATTR_NAME_SELINUX;
1253 name_len = strlen(name);
1254 err = ceph_pagelist_reserve(pagelist,
1255 4 * 2 + name_len + as_ctx->sec_ctxlen);
1259 if (as_ctx->pagelist) {
1260 /* update count of KV pairs */
1261 BUG_ON(pagelist->length <= sizeof(__le32));
1262 if (list_is_singular(&pagelist->head)) {
1263 le32_add_cpu((__le32*)pagelist->mapped_tail, 1);
1265 struct page *page = list_first_entry(&pagelist->head,
1267 void *addr = kmap_atomic(page);
1268 le32_add_cpu((__le32*)addr, 1);
1269 kunmap_atomic(addr);
1272 as_ctx->pagelist = pagelist;
1275 ceph_pagelist_encode_32(pagelist, name_len);
1276 ceph_pagelist_append(pagelist, name, name_len);
1278 ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen);
1279 ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1283 if (pagelist && !as_ctx->pagelist)
1284 ceph_pagelist_release(pagelist);
1288 void ceph_security_invalidate_secctx(struct inode *inode)
1290 security_inode_invalidate_secctx(inode);
1293 static int ceph_xattr_set_security_label(const struct xattr_handler *handler,
1294 struct dentry *unused, struct inode *inode,
1295 const char *key, const void *buf,
1296 size_t buflen, int flags)
1298 if (security_ismaclabel(key)) {
1299 const char *name = xattr_full_name(handler, key);
1300 return __ceph_setxattr(inode, name, buf, buflen, flags);
1305 static int ceph_xattr_get_security_label(const struct xattr_handler *handler,
1306 struct dentry *unused, struct inode *inode,
1307 const char *key, void *buf, size_t buflen)
1309 if (security_ismaclabel(key)) {
1310 const char *name = xattr_full_name(handler, key);
1311 return __ceph_getxattr(inode, name, buf, buflen);
1316 static const struct xattr_handler ceph_security_label_handler = {
1317 .prefix = XATTR_SECURITY_PREFIX,
1318 .get = ceph_xattr_get_security_label,
1319 .set = ceph_xattr_set_security_label,
1324 void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
1326 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1327 posix_acl_release(as_ctx->acl);
1328 posix_acl_release(as_ctx->default_acl);
1330 #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1331 security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1333 if (as_ctx->pagelist)
1334 ceph_pagelist_release(as_ctx->pagelist);
1338 * List of handlers for synthetic system.* attributes. Other
1339 * attributes are handled directly.
1341 const struct xattr_handler *ceph_xattr_handlers[] = {
1342 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1343 &posix_acl_access_xattr_handler,
1344 &posix_acl_default_xattr_handler,
1346 #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1347 &ceph_security_label_handler,
1349 &ceph_other_xattr_handler,