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_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);
30 * These define virtual xattrs exposing the recursive directory
31 * statistics and layout metadata.
35 size_t name_size; /* strlen(name) + 1 (for '\0') */
36 ssize_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
38 bool (*exists_cb)(struct ceph_inode_info *ci);
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)
49 static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
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);
57 static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
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=";
67 size_t len, total_len = 0;
70 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
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);
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);
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);
90 total_len += strlen(ns_field) + pool_ns->len;
93 if (size >= total_len) {
94 memcpy(val, buf, len);
97 len = strlen(pool_name);
98 memcpy(val + ret, pool_name, len);
102 len = strlen(ns_field);
103 memcpy(val + ret, ns_field, len);
105 memcpy(val + ret, pool_ns->str, pool_ns->len);
109 up_read(&osdc->lock);
110 ceph_put_string(pool_ns);
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.
120 static __printf(3, 4)
121 int ceph_fmt_xattr(char *val, size_t size, const char *fmt, ...)
125 char buf[96]; /* NB: reevaluate size if new vxattrs are added */
128 ret = vsnprintf(buf, size ? sizeof(buf) : 0, fmt, args);
132 if (size && ret + 1 > sizeof(buf)) {
133 WARN_ONCE(true, "Returned length too big (%d)", ret);
138 memcpy(val, buf, ret);
142 static ssize_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
143 char *val, size_t size)
145 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_unit);
148 static ssize_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
149 char *val, size_t size)
151 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_count);
154 static ssize_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
155 char *val, size_t size)
157 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.object_size);
160 static ssize_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
161 char *val, size_t size)
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;
169 down_read(&osdc->lock);
170 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
172 ret = strlen(pool_name);
174 memcpy(val, pool_name, ret);
176 ret = ceph_fmt_xattr(val, size, "%lld", pool);
178 up_read(&osdc->lock);
182 static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
183 char *val, size_t size)
186 struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
191 memcpy(val, ns->str, ret);
199 static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
202 return ceph_fmt_xattr(val, size, "%lld", ci->i_files + ci->i_subdirs);
205 static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
208 return ceph_fmt_xattr(val, size, "%lld", ci->i_files);
211 static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
214 return ceph_fmt_xattr(val, size, "%lld", ci->i_subdirs);
217 static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
220 return ceph_fmt_xattr(val, size, "%lld",
221 ci->i_rfiles + ci->i_rsubdirs);
224 static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
227 return ceph_fmt_xattr(val, size, "%lld", ci->i_rfiles);
230 static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
233 return ceph_fmt_xattr(val, size, "%lld", ci->i_rsubdirs);
236 static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
239 return ceph_fmt_xattr(val, size, "%lld", ci->i_rbytes);
242 static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
245 return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
246 ci->i_rctime.tv_nsec);
250 static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci)
252 return ci->i_dir_pin != -ENODATA;
255 static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val,
258 return ceph_fmt_xattr(val, size, "%d", (int)ci->i_dir_pin);
262 static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
265 spin_lock(&ci->i_ceph_lock);
266 if ((ci->i_max_files || ci->i_max_bytes) &&
267 ci->i_vino.snap == CEPH_NOSNAP &&
269 ci->i_snap_realm->ino == ci->i_vino.ino)
271 spin_unlock(&ci->i_ceph_lock);
275 static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
278 return ceph_fmt_xattr(val, size, "max_bytes=%llu max_files=%llu",
279 ci->i_max_bytes, ci->i_max_files);
282 static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
283 char *val, size_t size)
285 return ceph_fmt_xattr(val, size, "%llu", ci->i_max_bytes);
288 static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
289 char *val, size_t size)
291 return ceph_fmt_xattr(val, size, "%llu", ci->i_max_files);
295 static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci)
297 return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0);
300 static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val,
303 return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec,
304 ci->i_snap_btime.tv_nsec);
307 static ssize_t ceph_vxattrcb_cluster_fsid(struct ceph_inode_info *ci,
308 char *val, size_t size)
310 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
312 return ceph_fmt_xattr(val, size, "%pU", &fsc->client->fsid);
315 static ssize_t ceph_vxattrcb_client_id(struct ceph_inode_info *ci,
316 char *val, size_t size)
318 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
320 return ceph_fmt_xattr(val, size, "client%lld",
321 ceph_client_gid(fsc->client));
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
328 #define XATTR_NAME_CEPH(_type, _name, _flags) \
330 .name = CEPH_XATTR_NAME(_type, _name), \
331 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
332 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
334 .flags = (VXATTR_FLAG_READONLY | _flags), \
336 #define XATTR_RSTAT_FIELD(_type, _name) \
337 XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
338 #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
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, \
346 #define XATTR_QUOTA_FIELD(_type, _name) \
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, \
355 static struct ceph_vxattr ceph_dir_vxattrs[] = {
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,
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),
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,
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,
390 XATTR_QUOTA_FIELD(quota, max_bytes),
391 XATTR_QUOTA_FIELD(quota, max_files),
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,
399 { .name = NULL, 0 } /* Required table terminator */
404 static struct ceph_vxattr ceph_file_vxattrs[] = {
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,
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),
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,
424 { .name = NULL, 0 } /* Required table terminator */
427 static struct ceph_vxattr ceph_common_vxattrs[] = {
429 .name = "ceph.cluster_fsid",
430 .name_size = sizeof("ceph.cluster_fsid"),
431 .getxattr_cb = ceph_vxattrcb_cluster_fsid,
433 .flags = VXATTR_FLAG_READONLY,
436 .name = "ceph.client_id",
437 .name_size = sizeof("ceph.client_id"),
438 .getxattr_cb = ceph_vxattrcb_client_id,
440 .flags = VXATTR_FLAG_READONLY,
442 { .name = NULL, 0 } /* Required table terminator */
445 static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
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;
454 static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
457 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
460 while (vxattr->name) {
461 if (!strcmp(vxattr->name, name))
467 vxattr = ceph_common_vxattrs;
468 while (vxattr->name) {
469 if (!strcmp(vxattr->name, name))
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)
484 struct rb_node *parent = NULL;
485 struct ceph_inode_xattr *xattr = NULL;
489 p = &ci->i_xattrs.index.rb_node;
492 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
493 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
499 if (name_len == xattr->name_len)
501 else if (name_len < xattr->name_len)
512 if (xattr && (flags & XATTR_CREATE))
514 else if (!xattr && (flags & XATTR_REPLACE))
522 if (update_xattr < 0) {
524 __remove_xattr(ci, xattr);
535 xattr->name_len = name_len;
536 xattr->should_free_name = update_xattr;
538 ci->i_xattrs.count++;
539 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
543 if (xattr->should_free_val)
550 ci->i_xattrs.names_size -= xattr->name_len;
551 ci->i_xattrs.vals_size -= xattr->val_len;
553 ci->i_xattrs.names_size += name_len;
554 ci->i_xattrs.vals_size += val_len;
560 xattr->val_len = val_len;
561 xattr->dirty = update_xattr;
562 xattr->should_free_val = (val && update_xattr);
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);
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);
576 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
580 struct rb_node *parent = NULL;
581 struct ceph_inode_xattr *xattr = NULL;
582 int name_len = strlen(name);
585 p = &ci->i_xattrs.index.rb_node;
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)
597 dout("__get_xattr %s: found %.*s\n", name,
598 xattr->val_len, xattr->val);
603 dout("__get_xattr %s: not found\n", name);
608 static void __free_xattr(struct ceph_inode_xattr *xattr)
612 if (xattr->should_free_name)
614 if (xattr->should_free_val)
620 static int __remove_xattr(struct ceph_inode_info *ci,
621 struct ceph_inode_xattr *xattr)
626 rb_erase(&xattr->node, &ci->i_xattrs.index);
628 if (xattr->should_free_name)
630 if (xattr->should_free_val)
633 ci->i_xattrs.names_size -= xattr->name_len;
634 ci->i_xattrs.vals_size -= xattr->val_len;
635 ci->i_xattrs.count--;
641 static char *__copy_xattr_names(struct ceph_inode_info *ci,
645 struct ceph_inode_xattr *xattr = NULL;
647 p = rb_first(&ci->i_xattrs.index);
648 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
651 xattr = rb_entry(p, struct ceph_inode_xattr, node);
652 memcpy(dest, xattr->name, xattr->name_len);
653 dest[xattr->name_len] = '\0';
655 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
656 xattr->name_len, ci->i_xattrs.names_size);
658 dest += xattr->name_len + 1;
665 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
667 struct rb_node *p, *tmp;
668 struct ceph_inode_xattr *xattr = NULL;
670 p = rb_first(&ci->i_xattrs.index);
672 dout("__ceph_destroy_xattrs p=%p\n", p);
675 xattr = rb_entry(p, struct ceph_inode_xattr, node);
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);
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;
692 static int __build_xattrs(struct inode *inode)
693 __releases(ci->i_ceph_lock)
694 __acquires(ci->i_ceph_lock)
700 const char *name, *val;
701 struct ceph_inode_info *ci = ceph_inode(inode);
703 struct ceph_inode_xattr **xattrs = NULL;
707 dout("__build_xattrs() len=%d\n",
708 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
710 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
711 return 0; /* already built */
713 __ceph_destroy_xattrs(ci);
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);
724 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
730 for (i = 0; i < numattr; i++) {
731 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
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++)
748 ceph_decode_32_safe(&p, end, len, bad);
752 ceph_decode_32_safe(&p, end, len, bad);
756 err = __set_xattr(ci, name, namelen, val, len,
757 0, 0, &xattrs[numattr]);
764 ci->i_xattrs.index_version = ci->i_xattrs.version;
765 ci->i_xattrs.dirty = false;
769 spin_lock(&ci->i_ceph_lock);
772 for (i = 0; i < numattr; i++)
776 ci->i_xattrs.names_size = 0;
780 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
784 * 4 bytes for the length, and additional 4 bytes per each xattr name,
785 * 4 bytes per each value
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);
795 size += 4 + 4 + name_size + val_size;
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
806 struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
809 struct ceph_inode_xattr *xattr = NULL;
810 struct ceph_buffer *old_blob = NULL;
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);
817 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
819 p = rb_first(&ci->i_xattrs.index);
820 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
822 ceph_encode_32(&dest, ci->i_xattrs.count);
824 xattr = rb_entry(p, struct ceph_inode_xattr, node);
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;
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;
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++;
851 static inline int __get_request_mask(struct inode *in) {
852 struct ceph_mds_request *req = current->journal_info;
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);
868 ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
871 struct ceph_inode_info *ci = ceph_inode(inode);
872 struct ceph_inode_xattr *xattr;
873 struct ceph_vxattr *vxattr = NULL;
877 /* let's see if a virtual xattr was requested */
878 vxattr = ceph_match_vxattr(inode, name);
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);
889 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
890 err = vxattr->getxattr_cb(ci, value, size);
891 if (size && size < err)
897 req_mask = __get_request_mask(inode);
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);
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);
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);
915 /* get xattrs from mds (if we don't already have them) */
916 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
919 spin_lock(&ci->i_ceph_lock);
922 err = __build_xattrs(inode);
926 err = -ENODATA; /* == ENOATTR */
927 xattr = __get_xattr(ci, name);
932 if (size && size < xattr->val_len)
935 err = xattr->val_len;
939 memcpy(value, xattr->val, xattr->val_len);
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;
946 spin_unlock(&ci->i_ceph_lock);
950 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
952 struct inode *inode = d_inode(dentry);
953 struct ceph_inode_info *ci = ceph_inode(inode);
954 bool len_only = (size == 0);
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);
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);
968 spin_lock(&ci->i_ceph_lock);
971 err = __build_xattrs(inode);
975 /* add 1 byte for each xattr due to the null termination */
976 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
978 if (namelen > size) {
982 names = __copy_xattr_names(ci, names);
987 spin_unlock(&ci->i_ceph_lock);
991 static int ceph_sync_setxattr(struct inode *inode, const char *name,
992 const char *value, size_t size, int flags)
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;
1003 /* copy value into pagelist */
1004 pagelist = ceph_pagelist_alloc(GFP_NOFS);
1008 err = ceph_pagelist_append(pagelist, value, size);
1011 } else if (!value) {
1012 if (flags & CEPH_XATTR_REPLACE)
1013 op = CEPH_MDS_OP_RMXATTR;
1015 flags |= CEPH_XATTR_REMOVE;
1018 dout("setxattr value=%.*s\n", (int)size, value);
1021 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1027 req->r_path2 = kstrdup(name, GFP_NOFS);
1028 if (!req->r_path2) {
1029 ceph_mdsc_put_request(req);
1034 if (op == CEPH_MDS_OP_SETXATTR) {
1035 req->r_args.setxattr.flags = cpu_to_le32(flags);
1036 req->r_pagelist = pagelist;
1040 req->r_inode = inode;
1042 req->r_num_caps = 1;
1043 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
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);
1052 ceph_pagelist_release(pagelist);
1056 int __ceph_setxattr(struct inode *inode, const char *name,
1057 const void *value, size_t size, int flags)
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;
1067 int name_len = strlen(name);
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;
1076 if (ceph_snap(inode) != CEPH_NOSNAP)
1079 vxattr = ceph_match_vxattr(inode, name);
1081 if (vxattr->flags & VXATTR_FLAG_READONLY)
1083 if (value && !strncmp(vxattr->name, "ceph.quota", 10))
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;
1091 /* preallocate memory for xattr name, value, index node */
1093 newname = kmemdup(name, name_len + 1, GFP_NOFS);
1098 newval = kmemdup(value, val_len, GFP_NOFS);
1103 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1107 prealloc_cf = ceph_alloc_cap_flush();
1111 spin_lock(&ci->i_ceph_lock);
1113 issued = __ceph_caps_issued(ci, NULL);
1114 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
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);
1127 dout("setxattr %p name '%s' issued %s\n", inode, name,
1128 ceph_cap_string(issued));
1129 __build_xattrs(inode);
1131 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1133 if (!ci->i_xattrs.prealloc_blob ||
1134 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
1135 struct ceph_buffer *blob;
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);
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;
1151 err = __set_xattr(ci, newname, name_len, newval, val_len,
1152 flags, value ? 1 : -1, &xattr);
1155 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1157 ci->i_xattrs.dirty = true;
1158 inode->i_ctime = current_time(inode);
1161 spin_unlock(&ci->i_ceph_lock);
1162 ceph_buffer_put(old_blob);
1163 if (lock_snap_rwsem)
1164 up_read(&mdsc->snap_rwsem);
1166 __mark_inode_dirty(inode, dirty);
1167 ceph_free_cap_flush(prealloc_cf);
1171 spin_unlock(&ci->i_ceph_lock);
1173 if (lock_snap_rwsem)
1174 up_read(&mdsc->snap_rwsem);
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);
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))
1190 spin_unlock(&ci->i_ceph_lock);
1194 ceph_free_cap_flush(prealloc_cf);
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)
1205 if (!ceph_is_valid_xattr(name))
1207 return __ceph_getxattr(inode, name, value, size);
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)
1215 if (!ceph_is_valid_xattr(name))
1217 return __ceph_setxattr(inode, name, value, size, flags);
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,
1226 #ifdef CONFIG_SECURITY
1227 bool ceph_security_xattr_wanted(struct inode *in)
1229 return in->i_security != NULL;
1232 bool ceph_security_xattr_deadlock(struct inode *in)
1234 struct ceph_inode_info *ci;
1236 if (!in->i_security)
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);
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)
1251 struct ceph_pagelist *pagelist = as_ctx->pagelist;
1256 err = security_dentry_init_security(dentry, mode, &dentry->d_name,
1258 &as_ctx->sec_ctxlen);
1260 WARN_ON_ONCE(err != -EOPNOTSUPP);
1261 err = 0; /* do nothing */
1267 pagelist = ceph_pagelist_alloc(GFP_KERNEL);
1270 err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
1273 ceph_pagelist_encode_32(pagelist, 1);
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.
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);
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);
1294 struct page *page = list_first_entry(&pagelist->head,
1296 void *addr = kmap_atomic(page);
1297 le32_add_cpu((__le32*)addr, 1);
1298 kunmap_atomic(addr);
1301 as_ctx->pagelist = pagelist;
1304 ceph_pagelist_encode_32(pagelist, name_len);
1305 ceph_pagelist_append(pagelist, name, name_len);
1307 ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen);
1308 ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1312 if (pagelist && !as_ctx->pagelist)
1313 ceph_pagelist_release(pagelist);
1316 #endif /* CONFIG_CEPH_FS_SECURITY_LABEL */
1317 #endif /* CONFIG_SECURITY */
1319 void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
1321 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1322 posix_acl_release(as_ctx->acl);
1323 posix_acl_release(as_ctx->default_acl);
1325 #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1326 security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1328 if (as_ctx->pagelist)
1329 ceph_pagelist_release(as_ctx->pagelist);
1333 * List of handlers for synthetic system.* attributes. Other
1334 * attributes are handled directly.
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,
1341 &ceph_other_xattr_handler,