1 // SPDX-License-Identifier: GPL-2.0
5 #include "bkey_methods.h"
6 #include "btree_update.h"
13 #include <linux/dcache.h>
14 #include <linux/posix_acl_xattr.h>
15 #include <linux/xattr.h>
17 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned);
19 static u64 bch2_xattr_hash(const struct bch_hash_info *info,
20 const struct xattr_search_key *key)
22 struct bch_str_hash_ctx ctx;
24 bch2_str_hash_init(&ctx, info);
25 bch2_str_hash_update(&ctx, info, &key->type, sizeof(key->type));
26 bch2_str_hash_update(&ctx, info, key->name.name, key->name.len);
28 return bch2_str_hash_end(&ctx, info);
31 static u64 xattr_hash_key(const struct bch_hash_info *info, const void *key)
33 return bch2_xattr_hash(info, key);
36 static u64 xattr_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
38 struct bkey_s_c_xattr x = bkey_s_c_to_xattr(k);
40 return bch2_xattr_hash(info,
41 &X_SEARCH(x.v->x_type, x.v->x_name, x.v->x_name_len));
44 static bool xattr_cmp_key(struct bkey_s_c _l, const void *_r)
46 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
47 const struct xattr_search_key *r = _r;
49 return l.v->x_type != r->type ||
50 l.v->x_name_len != r->name.len ||
51 memcmp(l.v->x_name, r->name.name, r->name.len);
54 static bool xattr_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
56 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
57 struct bkey_s_c_xattr r = bkey_s_c_to_xattr(_r);
59 return l.v->x_type != r.v->x_type ||
60 l.v->x_name_len != r.v->x_name_len ||
61 memcmp(l.v->x_name, r.v->x_name, r.v->x_name_len);
64 const struct bch_hash_desc bch2_xattr_hash_desc = {
65 .btree_id = BTREE_ID_xattrs,
66 .key_type = KEY_TYPE_xattr,
67 .hash_key = xattr_hash_key,
68 .hash_bkey = xattr_hash_bkey,
69 .cmp_key = xattr_cmp_key,
70 .cmp_bkey = xattr_cmp_bkey,
73 int bch2_xattr_invalid(struct bch_fs *c, struct bkey_s_c k,
74 enum bch_validate_flags flags,
77 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
78 unsigned val_u64s = xattr_val_u64s(xattr.v->x_name_len,
79 le16_to_cpu(xattr.v->x_val_len));
82 bkey_fsck_err_on(bkey_val_u64s(k.k) < val_u64s, c, err,
83 xattr_val_size_too_small,
84 "value too small (%zu < %u)",
85 bkey_val_u64s(k.k), val_u64s);
88 val_u64s = xattr_val_u64s(xattr.v->x_name_len,
89 le16_to_cpu(xattr.v->x_val_len) + 4);
91 bkey_fsck_err_on(bkey_val_u64s(k.k) > val_u64s, c, err,
92 xattr_val_size_too_big,
93 "value too big (%zu > %u)",
94 bkey_val_u64s(k.k), val_u64s);
96 bkey_fsck_err_on(!bch2_xattr_type_to_handler(xattr.v->x_type), c, err,
98 "invalid type (%u)", xattr.v->x_type);
100 bkey_fsck_err_on(memchr(xattr.v->x_name, '\0', xattr.v->x_name_len), c, err,
101 xattr_name_invalid_chars,
102 "xattr name has invalid characters");
107 void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
110 const struct xattr_handler *handler;
111 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
113 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
114 if (handler && handler->prefix)
115 prt_printf(out, "%s", handler->prefix);
117 prt_printf(out, "(type %u)", xattr.v->x_type);
119 prt_printf(out, "(unknown type %u)", xattr.v->x_type);
121 unsigned name_len = xattr.v->x_name_len;
122 unsigned val_len = le16_to_cpu(xattr.v->x_val_len);
123 unsigned max_name_val_bytes = bkey_val_bytes(xattr.k) -
124 offsetof(struct bch_xattr, x_name);
126 val_len = min_t(int, val_len, max_name_val_bytes - name_len);
127 name_len = min(name_len, max_name_val_bytes);
129 prt_printf(out, "%.*s:%.*s",
130 name_len, xattr.v->x_name,
131 val_len, (char *) xattr_val(xattr.v));
133 if (xattr.v->x_type == KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS ||
134 xattr.v->x_type == KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT) {
136 bch2_acl_to_text(out, xattr_val(xattr.v),
137 le16_to_cpu(xattr.v->x_val_len));
141 static int bch2_xattr_get_trans(struct btree_trans *trans, struct bch_inode_info *inode,
142 const char *name, void *buffer, size_t size, int type)
144 struct bch_hash_info hash = bch2_hash_info_init(trans->c, &inode->ei_inode);
145 struct xattr_search_key search = X_SEARCH(type, name, strlen(name));
146 struct btree_iter iter;
147 struct bkey_s_c k = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc, &hash,
148 inode_inum(inode), &search, 0);
149 int ret = bkey_err(k);
153 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
154 ret = le16_to_cpu(xattr.v->x_val_len);
159 memcpy(buffer, xattr_val(xattr.v), ret);
161 bch2_trans_iter_exit(trans, &iter);
165 int bch2_xattr_set(struct btree_trans *trans, subvol_inum inum,
166 struct bch_inode_unpacked *inode_u,
167 const struct bch_hash_info *hash_info,
168 const char *name, const void *value, size_t size,
171 struct bch_fs *c = trans->c;
172 struct btree_iter inode_iter = { NULL };
175 ret = bch2_subvol_is_ro_trans(trans, inum.subvol) ?:
176 bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_intent);
180 inode_u->bi_ctime = bch2_current_time(c);
182 ret = bch2_inode_write(trans, &inode_iter, inode_u);
183 bch2_trans_iter_exit(trans, &inode_iter);
189 struct bkey_i_xattr *xattr;
190 unsigned namelen = strlen(name);
191 unsigned u64s = BKEY_U64s +
192 xattr_val_u64s(namelen, size);
197 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
199 return PTR_ERR(xattr);
201 bkey_xattr_init(&xattr->k_i);
202 xattr->k.u64s = u64s;
203 xattr->v.x_type = type;
204 xattr->v.x_name_len = namelen;
205 xattr->v.x_val_len = cpu_to_le16(size);
206 memcpy(xattr->v.x_name, name, namelen);
207 memcpy(xattr_val(&xattr->v), value, size);
209 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
211 (flags & XATTR_CREATE ? STR_HASH_must_create : 0)|
212 (flags & XATTR_REPLACE ? STR_HASH_must_replace : 0));
214 struct xattr_search_key search =
215 X_SEARCH(type, name, strlen(name));
217 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,
218 hash_info, inum, &search);
221 if (bch2_err_matches(ret, ENOENT))
222 ret = flags & XATTR_REPLACE ? -ENODATA : 0;
233 static int __bch2_xattr_emit(const char *prefix,
234 const char *name, size_t name_len,
235 struct xattr_buf *buf)
237 const size_t prefix_len = strlen(prefix);
238 const size_t total_len = prefix_len + name_len + 1;
241 if (buf->used + total_len > buf->len)
244 memcpy(buf->buf + buf->used, prefix, prefix_len);
245 memcpy(buf->buf + buf->used + prefix_len,
247 buf->buf[buf->used + prefix_len + name_len] = '\0';
250 buf->used += total_len;
254 static int bch2_xattr_emit(struct dentry *dentry,
255 const struct bch_xattr *xattr,
256 struct xattr_buf *buf)
258 const struct xattr_handler *handler =
259 bch2_xattr_type_to_handler(xattr->x_type);
261 return handler && (!handler->list || handler->list(dentry))
262 ? __bch2_xattr_emit(handler->prefix ?: handler->name,
263 xattr->x_name, xattr->x_name_len, buf)
267 static int bch2_xattr_list_bcachefs(struct bch_fs *c,
268 struct bch_inode_unpacked *inode,
269 struct xattr_buf *buf,
272 const char *prefix = all ? "bcachefs_effective." : "bcachefs.";
277 for (id = 0; id < Inode_opt_nr; id++) {
278 v = bch2_inode_opt_get(inode, id);
283 !(inode->bi_fields_set & (1 << id)))
286 ret = __bch2_xattr_emit(prefix, bch2_inode_opts[id],
287 strlen(bch2_inode_opts[id]), buf);
295 ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
297 struct bch_fs *c = dentry->d_sb->s_fs_info;
298 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
299 struct btree_trans *trans = bch2_trans_get(c);
300 struct btree_iter iter;
302 struct xattr_buf buf = { .buf = buffer, .len = buffer_size };
303 u64 offset = 0, inum = inode->ei_inode.bi_inum;
307 bch2_trans_begin(trans);
308 iter = (struct btree_iter) { NULL };
310 ret = bch2_subvolume_get_snapshot(trans, inode->ei_subvol, &snapshot);
314 for_each_btree_key_upto_norestart(trans, iter, BTREE_ID_xattrs,
315 SPOS(inum, offset, snapshot),
316 POS(inum, U64_MAX), 0, k, ret) {
317 if (k.k->type != KEY_TYPE_xattr)
320 ret = bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf);
325 offset = iter.pos.offset;
326 bch2_trans_iter_exit(trans, &iter);
328 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
331 bch2_trans_put(trans);
336 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, false);
340 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, true);
346 return bch2_err_class(ret);
349 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
350 struct dentry *dentry, struct inode *vinode,
351 const char *name, void *buffer, size_t size)
353 struct bch_inode_info *inode = to_bch_ei(vinode);
354 struct bch_fs *c = inode->v.i_sb->s_fs_info;
355 int ret = bch2_trans_do(c, NULL, NULL, 0,
356 bch2_xattr_get_trans(trans, inode, name, buffer, size, handler->flags));
358 if (ret < 0 && bch2_err_matches(ret, ENOENT))
361 return bch2_err_class(ret);
364 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
365 struct mnt_idmap *idmap,
366 struct dentry *dentry, struct inode *vinode,
367 const char *name, const void *value,
368 size_t size, int flags)
370 struct bch_inode_info *inode = to_bch_ei(vinode);
371 struct bch_fs *c = inode->v.i_sb->s_fs_info;
372 struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
373 struct bch_inode_unpacked inode_u;
376 ret = bch2_trans_run(c,
377 commit_do(trans, NULL, NULL, 0,
378 bch2_xattr_set(trans, inode_inum(inode), &inode_u,
379 &hash, name, value, size,
380 handler->flags, flags)) ?:
381 (bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME), 0));
383 return bch2_err_class(ret);
386 static const struct xattr_handler bch_xattr_user_handler = {
387 .prefix = XATTR_USER_PREFIX,
388 .get = bch2_xattr_get_handler,
389 .set = bch2_xattr_set_handler,
390 .flags = KEY_TYPE_XATTR_INDEX_USER,
393 static bool bch2_xattr_trusted_list(struct dentry *dentry)
395 return capable(CAP_SYS_ADMIN);
398 static const struct xattr_handler bch_xattr_trusted_handler = {
399 .prefix = XATTR_TRUSTED_PREFIX,
400 .list = bch2_xattr_trusted_list,
401 .get = bch2_xattr_get_handler,
402 .set = bch2_xattr_set_handler,
403 .flags = KEY_TYPE_XATTR_INDEX_TRUSTED,
406 static const struct xattr_handler bch_xattr_security_handler = {
407 .prefix = XATTR_SECURITY_PREFIX,
408 .get = bch2_xattr_get_handler,
409 .set = bch2_xattr_set_handler,
410 .flags = KEY_TYPE_XATTR_INDEX_SECURITY,
413 #ifndef NO_BCACHEFS_FS
415 static int opt_to_inode_opt(int id)
418 #define x(name, ...) \
419 case Opt_##name: return Inode_opt_##name;
427 static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
428 struct dentry *dentry, struct inode *vinode,
429 const char *name, void *buffer, size_t size,
432 struct bch_inode_info *inode = to_bch_ei(vinode);
433 struct bch_fs *c = inode->v.i_sb->s_fs_info;
434 struct bch_opts opts =
435 bch2_inode_opts_to_opts(&inode->ei_inode);
436 const struct bch_option *opt;
437 int id, inode_opt_id;
438 struct printbuf out = PRINTBUF;
442 id = bch2_opt_lookup(name);
443 if (id < 0 || !bch2_opt_is_inode_opt(id))
446 inode_opt_id = opt_to_inode_opt(id);
447 if (inode_opt_id < 0)
450 opt = bch2_opt_table + id;
452 if (!bch2_opt_defined_by_id(&opts, id))
456 !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))
459 v = bch2_opt_get_by_id(&opts, id);
460 bch2_opt_to_text(&out, c, c->disk_sb.sb, opt, v, 0);
464 if (out.allocation_failure) {
470 memcpy(buffer, out.buf, out.pos);
477 static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
478 struct dentry *dentry, struct inode *vinode,
479 const char *name, void *buffer, size_t size)
481 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
482 name, buffer, size, false);
485 struct inode_opt_set {
491 static int inode_opt_set_fn(struct btree_trans *trans,
492 struct bch_inode_info *inode,
493 struct bch_inode_unpacked *bi,
496 struct inode_opt_set *s = p;
499 bi->bi_fields_set |= 1U << s->id;
501 bi->bi_fields_set &= ~(1U << s->id);
503 bch2_inode_opt_set(bi, s->id, s->v);
508 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
509 struct mnt_idmap *idmap,
510 struct dentry *dentry, struct inode *vinode,
511 const char *name, const void *value,
512 size_t size, int flags)
514 struct bch_inode_info *inode = to_bch_ei(vinode);
515 struct bch_fs *c = inode->v.i_sb->s_fs_info;
516 const struct bch_option *opt;
518 struct inode_opt_set s;
519 int opt_id, inode_opt_id, ret;
521 opt_id = bch2_opt_lookup(name);
525 opt = bch2_opt_table + opt_id;
527 inode_opt_id = opt_to_inode_opt(opt_id);
528 if (inode_opt_id < 0)
536 buf = kmalloc(size + 1, GFP_KERNEL);
539 memcpy(buf, value, size);
542 ret = bch2_opt_parse(c, opt, buf, &v, NULL);
548 ret = bch2_opt_check_may_set(c, opt_id, v);
556 * Check if this option was set on the parent - if so, switched
557 * back to inheriting from the parent:
559 * rename() also has to deal with keeping inherited options up
560 * to date - see bch2_reinherit_attrs()
562 spin_lock(&dentry->d_lock);
563 if (!IS_ROOT(dentry)) {
564 struct bch_inode_info *dir =
565 to_bch_ei(d_inode(dentry->d_parent));
567 s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id);
571 spin_unlock(&dentry->d_lock);
576 mutex_lock(&inode->ei_update_lock);
577 if (inode_opt_id == Inode_opt_project) {
579 * inode fields accessible via the xattr interface are stored
580 * with a +1 bias, so that 0 means unset:
582 ret = bch2_set_projid(c, inode, s.v ? s.v - 1 : 0);
587 ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
589 mutex_unlock(&inode->ei_update_lock);
592 (opt_id == Opt_background_target ||
593 opt_id == Opt_background_compression ||
594 (opt_id == Opt_compression && !inode_opt_get(c, &inode->ei_inode, background_compression))))
595 bch2_set_rebalance_needs_scan(c, inode->ei_inode.bi_inum);
598 return bch2_err_class(ret);
601 static const struct xattr_handler bch_xattr_bcachefs_handler = {
602 .prefix = "bcachefs.",
603 .get = bch2_xattr_bcachefs_get,
604 .set = bch2_xattr_bcachefs_set,
607 static int bch2_xattr_bcachefs_get_effective(
608 const struct xattr_handler *handler,
609 struct dentry *dentry, struct inode *vinode,
610 const char *name, void *buffer, size_t size)
612 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
613 name, buffer, size, true);
616 static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
617 .prefix = "bcachefs_effective.",
618 .get = bch2_xattr_bcachefs_get_effective,
619 .set = bch2_xattr_bcachefs_set,
622 #endif /* NO_BCACHEFS_FS */
624 const struct xattr_handler *bch2_xattr_handlers[] = {
625 &bch_xattr_user_handler,
626 #ifdef CONFIG_BCACHEFS_POSIX_ACL
627 &nop_posix_acl_access,
628 &nop_posix_acl_default,
630 &bch_xattr_trusted_handler,
631 &bch_xattr_security_handler,
632 #ifndef NO_BCACHEFS_FS
633 &bch_xattr_bcachefs_handler,
634 &bch_xattr_bcachefs_effective_handler,
639 static const struct xattr_handler *bch_xattr_handler_map[] = {
640 [KEY_TYPE_XATTR_INDEX_USER] = &bch_xattr_user_handler,
641 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] =
642 &nop_posix_acl_access,
643 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT] =
644 &nop_posix_acl_default,
645 [KEY_TYPE_XATTR_INDEX_TRUSTED] = &bch_xattr_trusted_handler,
646 [KEY_TYPE_XATTR_INDEX_SECURITY] = &bch_xattr_security_handler,
649 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
651 return type < ARRAY_SIZE(bch_xattr_handler_map)
652 ? bch_xattr_handler_map[type]