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(const struct bch_fs *c, struct bkey_s_c k,
74 enum bkey_invalid_flags flags,
77 const struct xattr_handler *handler;
78 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
80 if (bkey_val_u64s(k.k) <
81 xattr_val_u64s(xattr.v->x_name_len,
82 le16_to_cpu(xattr.v->x_val_len))) {
83 prt_printf(err, "value too small (%zu < %u)",
85 xattr_val_u64s(xattr.v->x_name_len,
86 le16_to_cpu(xattr.v->x_val_len)));
87 return -BCH_ERR_invalid_bkey;
91 if (bkey_val_u64s(k.k) >
92 xattr_val_u64s(xattr.v->x_name_len,
93 le16_to_cpu(xattr.v->x_val_len) + 4)) {
94 prt_printf(err, "value too big (%zu > %u)",
96 xattr_val_u64s(xattr.v->x_name_len,
97 le16_to_cpu(xattr.v->x_val_len) + 4));
98 return -BCH_ERR_invalid_bkey;
101 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
103 prt_printf(err, "invalid type (%u)", xattr.v->x_type);
104 return -BCH_ERR_invalid_bkey;
107 if (memchr(xattr.v->x_name, '\0', xattr.v->x_name_len)) {
108 prt_printf(err, "xattr name has invalid characters");
109 return -BCH_ERR_invalid_bkey;
115 void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
118 const struct xattr_handler *handler;
119 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
121 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
122 if (handler && handler->prefix)
123 prt_printf(out, "%s", handler->prefix);
125 prt_printf(out, "(type %u)", xattr.v->x_type);
127 prt_printf(out, "(unknown type %u)", xattr.v->x_type);
129 prt_printf(out, "%.*s:%.*s",
132 le16_to_cpu(xattr.v->x_val_len),
133 (char *) xattr_val(xattr.v));
135 if (xattr.v->x_type == KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS ||
136 xattr.v->x_type == KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT) {
138 bch2_acl_to_text(out, xattr_val(xattr.v),
139 le16_to_cpu(xattr.v->x_val_len));
143 static int bch2_xattr_get_trans(struct btree_trans *trans, struct bch_inode_info *inode,
144 const char *name, void *buffer, size_t size, int type)
146 struct bch_hash_info hash = bch2_hash_info_init(trans->c, &inode->ei_inode);
147 struct xattr_search_key search = X_SEARCH(type, name, strlen(name));
148 struct btree_iter iter;
149 struct bkey_s_c_xattr xattr;
153 ret = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc, &hash,
154 inode_inum(inode), &search, 0);
158 k = bch2_btree_iter_peek_slot(&iter);
163 xattr = bkey_s_c_to_xattr(k);
164 ret = le16_to_cpu(xattr.v->x_val_len);
169 memcpy(buffer, xattr_val(xattr.v), ret);
172 bch2_trans_iter_exit(trans, &iter);
174 return ret < 0 && bch2_err_matches(ret, ENOENT) ? -ENODATA : ret;
177 int bch2_xattr_set(struct btree_trans *trans, subvol_inum inum,
178 struct bch_inode_unpacked *inode_u,
179 const struct bch_hash_info *hash_info,
180 const char *name, const void *value, size_t size,
183 struct bch_fs *c = trans->c;
184 struct btree_iter inode_iter = { NULL };
187 ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_INTENT);
191 inode_u->bi_ctime = bch2_current_time(c);
193 ret = bch2_inode_write(trans, &inode_iter, inode_u);
194 bch2_trans_iter_exit(trans, &inode_iter);
200 struct bkey_i_xattr *xattr;
201 unsigned namelen = strlen(name);
202 unsigned u64s = BKEY_U64s +
203 xattr_val_u64s(namelen, size);
208 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
210 return PTR_ERR(xattr);
212 bkey_xattr_init(&xattr->k_i);
213 xattr->k.u64s = u64s;
214 xattr->v.x_type = type;
215 xattr->v.x_name_len = namelen;
216 xattr->v.x_val_len = cpu_to_le16(size);
217 memcpy(xattr->v.x_name, name, namelen);
218 memcpy(xattr_val(&xattr->v), value, size);
220 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
222 (flags & XATTR_CREATE ? BCH_HASH_SET_MUST_CREATE : 0)|
223 (flags & XATTR_REPLACE ? BCH_HASH_SET_MUST_REPLACE : 0));
225 struct xattr_search_key search =
226 X_SEARCH(type, name, strlen(name));
228 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,
229 hash_info, inum, &search);
232 if (bch2_err_matches(ret, ENOENT))
233 ret = flags & XATTR_REPLACE ? -ENODATA : 0;
244 static int __bch2_xattr_emit(const char *prefix,
245 const char *name, size_t name_len,
246 struct xattr_buf *buf)
248 const size_t prefix_len = strlen(prefix);
249 const size_t total_len = prefix_len + name_len + 1;
252 if (buf->used + total_len > buf->len)
255 memcpy(buf->buf + buf->used, prefix, prefix_len);
256 memcpy(buf->buf + buf->used + prefix_len,
258 buf->buf[buf->used + prefix_len + name_len] = '\0';
261 buf->used += total_len;
265 static int bch2_xattr_emit(struct dentry *dentry,
266 const struct bch_xattr *xattr,
267 struct xattr_buf *buf)
269 const struct xattr_handler *handler =
270 bch2_xattr_type_to_handler(xattr->x_type);
272 return handler && (!handler->list || handler->list(dentry))
273 ? __bch2_xattr_emit(handler->prefix ?: handler->name,
274 xattr->x_name, xattr->x_name_len, buf)
278 static int bch2_xattr_list_bcachefs(struct bch_fs *c,
279 struct bch_inode_unpacked *inode,
280 struct xattr_buf *buf,
283 const char *prefix = all ? "bcachefs_effective." : "bcachefs.";
288 for (id = 0; id < Inode_opt_nr; id++) {
289 v = bch2_inode_opt_get(inode, id);
294 !(inode->bi_fields_set & (1 << id)))
297 ret = __bch2_xattr_emit(prefix, bch2_inode_opts[id],
298 strlen(bch2_inode_opts[id]), buf);
306 ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
308 struct bch_fs *c = dentry->d_sb->s_fs_info;
309 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
310 struct btree_trans *trans = bch2_trans_get(c);
311 struct btree_iter iter;
313 struct xattr_buf buf = { .buf = buffer, .len = buffer_size };
314 u64 offset = 0, inum = inode->ei_inode.bi_inum;
318 bch2_trans_begin(trans);
319 iter = (struct btree_iter) { NULL };
321 ret = bch2_subvolume_get_snapshot(trans, inode->ei_subvol, &snapshot);
325 for_each_btree_key_upto_norestart(trans, iter, BTREE_ID_xattrs,
326 SPOS(inum, offset, snapshot),
327 POS(inum, U64_MAX), 0, k, ret) {
328 if (k.k->type != KEY_TYPE_xattr)
331 ret = bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf);
336 offset = iter.pos.offset;
337 bch2_trans_iter_exit(trans, &iter);
339 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
342 bch2_trans_put(trans);
347 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, false);
351 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, true);
357 return bch2_err_class(ret);
360 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
361 struct dentry *dentry, struct inode *vinode,
362 const char *name, void *buffer, size_t size)
364 struct bch_inode_info *inode = to_bch_ei(vinode);
365 struct bch_fs *c = inode->v.i_sb->s_fs_info;
366 int ret = bch2_trans_do(c, NULL, NULL, 0,
367 bch2_xattr_get_trans(trans, inode, name, buffer, size, handler->flags));
369 return bch2_err_class(ret);
372 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
373 struct mnt_idmap *idmap,
374 struct dentry *dentry, struct inode *vinode,
375 const char *name, const void *value,
376 size_t size, int flags)
378 struct bch_inode_info *inode = to_bch_ei(vinode);
379 struct bch_fs *c = inode->v.i_sb->s_fs_info;
380 struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
381 struct bch_inode_unpacked inode_u;
384 ret = bch2_trans_run(c,
385 commit_do(trans, NULL, NULL, 0,
386 bch2_xattr_set(trans, inode_inum(inode), &inode_u,
387 &hash, name, value, size,
388 handler->flags, flags)) ?:
389 (bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME), 0));
391 return bch2_err_class(ret);
394 static const struct xattr_handler bch_xattr_user_handler = {
395 .prefix = XATTR_USER_PREFIX,
396 .get = bch2_xattr_get_handler,
397 .set = bch2_xattr_set_handler,
398 .flags = KEY_TYPE_XATTR_INDEX_USER,
401 static bool bch2_xattr_trusted_list(struct dentry *dentry)
403 return capable(CAP_SYS_ADMIN);
406 static const struct xattr_handler bch_xattr_trusted_handler = {
407 .prefix = XATTR_TRUSTED_PREFIX,
408 .list = bch2_xattr_trusted_list,
409 .get = bch2_xattr_get_handler,
410 .set = bch2_xattr_set_handler,
411 .flags = KEY_TYPE_XATTR_INDEX_TRUSTED,
414 static const struct xattr_handler bch_xattr_security_handler = {
415 .prefix = XATTR_SECURITY_PREFIX,
416 .get = bch2_xattr_get_handler,
417 .set = bch2_xattr_set_handler,
418 .flags = KEY_TYPE_XATTR_INDEX_SECURITY,
421 #ifndef NO_BCACHEFS_FS
423 static int opt_to_inode_opt(int id)
426 #define x(name, ...) \
427 case Opt_##name: return Inode_opt_##name;
435 static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
436 struct dentry *dentry, struct inode *vinode,
437 const char *name, void *buffer, size_t size,
440 struct bch_inode_info *inode = to_bch_ei(vinode);
441 struct bch_fs *c = inode->v.i_sb->s_fs_info;
442 struct bch_opts opts =
443 bch2_inode_opts_to_opts(&inode->ei_inode);
444 const struct bch_option *opt;
445 int id, inode_opt_id;
446 struct printbuf out = PRINTBUF;
450 id = bch2_opt_lookup(name);
451 if (id < 0 || !bch2_opt_is_inode_opt(id))
454 inode_opt_id = opt_to_inode_opt(id);
455 if (inode_opt_id < 0)
458 opt = bch2_opt_table + id;
460 if (!bch2_opt_defined_by_id(&opts, id))
464 !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))
467 v = bch2_opt_get_by_id(&opts, id);
468 bch2_opt_to_text(&out, c, c->disk_sb.sb, opt, v, 0);
472 if (out.allocation_failure) {
478 memcpy(buffer, out.buf, out.pos);
485 static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
486 struct dentry *dentry, struct inode *vinode,
487 const char *name, void *buffer, size_t size)
489 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
490 name, buffer, size, false);
493 struct inode_opt_set {
499 static int inode_opt_set_fn(struct btree_trans *trans,
500 struct bch_inode_info *inode,
501 struct bch_inode_unpacked *bi,
504 struct inode_opt_set *s = p;
507 bi->bi_fields_set |= 1U << s->id;
509 bi->bi_fields_set &= ~(1U << s->id);
511 bch2_inode_opt_set(bi, s->id, s->v);
516 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
517 struct mnt_idmap *idmap,
518 struct dentry *dentry, struct inode *vinode,
519 const char *name, const void *value,
520 size_t size, int flags)
522 struct bch_inode_info *inode = to_bch_ei(vinode);
523 struct bch_fs *c = inode->v.i_sb->s_fs_info;
524 const struct bch_option *opt;
526 struct inode_opt_set s;
527 int opt_id, inode_opt_id, ret;
529 opt_id = bch2_opt_lookup(name);
533 opt = bch2_opt_table + opt_id;
535 inode_opt_id = opt_to_inode_opt(opt_id);
536 if (inode_opt_id < 0)
544 buf = kmalloc(size + 1, GFP_KERNEL);
547 memcpy(buf, value, size);
550 ret = bch2_opt_parse(c, opt, buf, &v, NULL);
556 ret = bch2_opt_check_may_set(c, opt_id, v);
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);
575 mutex_lock(&inode->ei_update_lock);
576 if (inode_opt_id == Inode_opt_project) {
578 * inode fields accessible via the xattr interface are stored
579 * with a +1 bias, so that 0 means unset:
581 ret = bch2_set_projid(c, inode, s.v ? s.v - 1 : 0);
586 ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
588 mutex_unlock(&inode->ei_update_lock);
591 (opt_id == Opt_background_compression ||
592 opt_id == Opt_background_target))
593 bch2_rebalance_add_work(c, inode->v.i_blocks);
595 return bch2_err_class(ret);
598 static const struct xattr_handler bch_xattr_bcachefs_handler = {
599 .prefix = "bcachefs.",
600 .get = bch2_xattr_bcachefs_get,
601 .set = bch2_xattr_bcachefs_set,
604 static int bch2_xattr_bcachefs_get_effective(
605 const struct xattr_handler *handler,
606 struct dentry *dentry, struct inode *vinode,
607 const char *name, void *buffer, size_t size)
609 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
610 name, buffer, size, true);
613 static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
614 .prefix = "bcachefs_effective.",
615 .get = bch2_xattr_bcachefs_get_effective,
616 .set = bch2_xattr_bcachefs_set,
619 #endif /* NO_BCACHEFS_FS */
621 const struct xattr_handler *bch2_xattr_handlers[] = {
622 &bch_xattr_user_handler,
623 #ifdef CONFIG_BCACHEFS_POSIX_ACL
624 &nop_posix_acl_access,
625 &nop_posix_acl_default,
627 &bch_xattr_trusted_handler,
628 &bch_xattr_security_handler,
629 #ifndef NO_BCACHEFS_FS
630 &bch_xattr_bcachefs_handler,
631 &bch_xattr_bcachefs_effective_handler,
636 static const struct xattr_handler *bch_xattr_handler_map[] = {
637 [KEY_TYPE_XATTR_INDEX_USER] = &bch_xattr_user_handler,
638 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] =
639 &nop_posix_acl_access,
640 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT] =
641 &nop_posix_acl_default,
642 [KEY_TYPE_XATTR_INDEX_TRUSTED] = &bch_xattr_trusted_handler,
643 [KEY_TYPE_XATTR_INDEX_SECURITY] = &bch_xattr_security_handler,
646 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
648 return type < ARRAY_SIZE(bch_xattr_handler_map)
649 ? bch_xattr_handler_map[type]