1 // SPDX-License-Identifier: GPL-2.0
4 #include "bkey_methods.h"
5 #include "btree_update.h"
12 #include <linux/dcache.h>
13 #include <linux/posix_acl_xattr.h>
14 #include <linux/xattr.h>
16 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned);
18 static u64 bch2_xattr_hash(const struct bch_hash_info *info,
19 const struct xattr_search_key *key)
21 struct bch_str_hash_ctx ctx;
23 bch2_str_hash_init(&ctx, info);
24 bch2_str_hash_update(&ctx, info, &key->type, sizeof(key->type));
25 bch2_str_hash_update(&ctx, info, key->name.name, key->name.len);
27 return bch2_str_hash_end(&ctx, info);
30 static u64 xattr_hash_key(const struct bch_hash_info *info, const void *key)
32 return bch2_xattr_hash(info, key);
35 static u64 xattr_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
37 struct bkey_s_c_xattr x = bkey_s_c_to_xattr(k);
39 return bch2_xattr_hash(info,
40 &X_SEARCH(x.v->x_type, x.v->x_name, x.v->x_name_len));
43 static bool xattr_cmp_key(struct bkey_s_c _l, const void *_r)
45 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
46 const struct xattr_search_key *r = _r;
48 return l.v->x_type != r->type ||
49 l.v->x_name_len != r->name.len ||
50 memcmp(l.v->x_name, r->name.name, r->name.len);
53 static bool xattr_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
55 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
56 struct bkey_s_c_xattr r = bkey_s_c_to_xattr(_r);
58 return l.v->x_type != r.v->x_type ||
59 l.v->x_name_len != r.v->x_name_len ||
60 memcmp(l.v->x_name, r.v->x_name, r.v->x_name_len);
63 const struct bch_hash_desc bch2_xattr_hash_desc = {
64 .btree_id = BTREE_ID_xattrs,
65 .key_type = KEY_TYPE_xattr,
66 .hash_key = xattr_hash_key,
67 .hash_bkey = xattr_hash_bkey,
68 .cmp_key = xattr_cmp_key,
69 .cmp_bkey = xattr_cmp_bkey,
72 int bch2_xattr_invalid(const struct bch_fs *c, struct bkey_s_c k,
73 unsigned flags, struct printbuf *err)
75 const struct xattr_handler *handler;
76 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
78 if (bkey_val_u64s(k.k) <
79 xattr_val_u64s(xattr.v->x_name_len,
80 le16_to_cpu(xattr.v->x_val_len))) {
81 prt_printf(err, "value too small (%zu < %u)",
83 xattr_val_u64s(xattr.v->x_name_len,
84 le16_to_cpu(xattr.v->x_val_len)));
85 return -BCH_ERR_invalid_bkey;
89 if (bkey_val_u64s(k.k) >
90 xattr_val_u64s(xattr.v->x_name_len,
91 le16_to_cpu(xattr.v->x_val_len) + 4)) {
92 prt_printf(err, "value too big (%zu > %u)",
94 xattr_val_u64s(xattr.v->x_name_len,
95 le16_to_cpu(xattr.v->x_val_len) + 4));
96 return -BCH_ERR_invalid_bkey;
99 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
101 prt_printf(err, "invalid type (%u)", xattr.v->x_type);
102 return -BCH_ERR_invalid_bkey;
105 if (memchr(xattr.v->x_name, '\0', xattr.v->x_name_len)) {
106 prt_printf(err, "xattr name has invalid characters");
107 return -BCH_ERR_invalid_bkey;
113 void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
116 const struct xattr_handler *handler;
117 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
119 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
120 if (handler && handler->prefix)
121 prt_printf(out, "%s", handler->prefix);
123 prt_printf(out, "(type %u)", xattr.v->x_type);
125 prt_printf(out, "(unknown type %u)", xattr.v->x_type);
127 prt_printf(out, "%.*s:%.*s",
130 le16_to_cpu(xattr.v->x_val_len),
131 (char *) xattr_val(xattr.v));
134 static int bch2_xattr_get_trans(struct btree_trans *trans, struct bch_inode_info *inode,
135 const char *name, void *buffer, size_t size, int type)
137 struct bch_hash_info hash = bch2_hash_info_init(trans->c, &inode->ei_inode);
138 struct btree_iter iter;
139 struct bkey_s_c_xattr xattr;
143 ret = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc, &hash,
145 &X_SEARCH(type, name, strlen(name)),
150 k = bch2_btree_iter_peek_slot(&iter);
155 xattr = bkey_s_c_to_xattr(k);
156 ret = le16_to_cpu(xattr.v->x_val_len);
161 memcpy(buffer, xattr_val(xattr.v), ret);
164 bch2_trans_iter_exit(trans, &iter);
166 return ret < 0 && bch2_err_matches(ret, ENOENT) ? -ENODATA : ret;
169 int bch2_xattr_get(struct bch_fs *c, struct bch_inode_info *inode,
170 const char *name, void *buffer, size_t size, int type)
172 return bch2_trans_do(c, NULL, NULL, 0,
173 bch2_xattr_get_trans(&trans, inode, name, buffer, size, type));
176 int bch2_xattr_set(struct btree_trans *trans, subvol_inum inum,
177 const struct bch_hash_info *hash_info,
178 const char *name, const void *value, size_t size,
181 struct btree_iter inode_iter = { NULL };
182 struct bch_inode_unpacked inode_u;
186 * We need to do an inode update so that bi_journal_sync gets updated
189 * Perhaps we should be updating bi_mtime too?
192 ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inum, BTREE_ITER_INTENT) ?:
193 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;
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_init(&trans, c, 0, 0);
320 bch2_trans_begin(&trans);
321 iter = (struct btree_iter) { NULL };
323 ret = bch2_subvolume_get_snapshot(&trans, inode->ei_subvol, &snapshot);
327 for_each_btree_key_upto_norestart(&trans, iter, BTREE_ID_xattrs,
328 SPOS(inum, offset, snapshot),
329 POS(inum, U64_MAX), 0, k, ret) {
330 if (k.k->type != KEY_TYPE_xattr)
333 ret = bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf);
338 offset = iter.pos.offset;
339 bch2_trans_iter_exit(&trans, &iter);
341 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
344 bch2_trans_exit(&trans);
349 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, false);
353 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, true);
359 return bch2_err_class(ret);
362 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
363 struct dentry *dentry, struct inode *vinode,
364 const char *name, void *buffer, size_t size)
366 struct bch_inode_info *inode = to_bch_ei(vinode);
367 struct bch_fs *c = inode->v.i_sb->s_fs_info;
370 ret = bch2_xattr_get(c, inode, name, buffer, size, handler->flags);
371 return bch2_err_class(ret);
374 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
375 struct mnt_idmap *idmap,
376 struct dentry *dentry, struct inode *vinode,
377 const char *name, const void *value,
378 size_t size, int flags)
380 struct bch_inode_info *inode = to_bch_ei(vinode);
381 struct bch_fs *c = inode->v.i_sb->s_fs_info;
382 struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
385 ret = bch2_trans_do(c, NULL, NULL, 0,
386 bch2_xattr_set(&trans, inode_inum(inode), &hash,
388 handler->flags, flags));
389 return bch2_err_class(ret);
392 static const struct xattr_handler bch_xattr_user_handler = {
393 .prefix = XATTR_USER_PREFIX,
394 .get = bch2_xattr_get_handler,
395 .set = bch2_xattr_set_handler,
396 .flags = KEY_TYPE_XATTR_INDEX_USER,
399 static bool bch2_xattr_trusted_list(struct dentry *dentry)
401 return capable(CAP_SYS_ADMIN);
404 static const struct xattr_handler bch_xattr_trusted_handler = {
405 .prefix = XATTR_TRUSTED_PREFIX,
406 .list = bch2_xattr_trusted_list,
407 .get = bch2_xattr_get_handler,
408 .set = bch2_xattr_set_handler,
409 .flags = KEY_TYPE_XATTR_INDEX_TRUSTED,
412 static const struct xattr_handler bch_xattr_security_handler = {
413 .prefix = XATTR_SECURITY_PREFIX,
414 .get = bch2_xattr_get_handler,
415 .set = bch2_xattr_set_handler,
416 .flags = KEY_TYPE_XATTR_INDEX_SECURITY,
419 #ifndef NO_BCACHEFS_FS
421 static int opt_to_inode_opt(int id)
424 #define x(name, ...) \
425 case Opt_##name: return Inode_opt_##name;
433 static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
434 struct dentry *dentry, struct inode *vinode,
435 const char *name, void *buffer, size_t size,
438 struct bch_inode_info *inode = to_bch_ei(vinode);
439 struct bch_fs *c = inode->v.i_sb->s_fs_info;
440 struct bch_opts opts =
441 bch2_inode_opts_to_opts(&inode->ei_inode);
442 const struct bch_option *opt;
443 int id, inode_opt_id;
444 struct printbuf out = PRINTBUF;
448 id = bch2_opt_lookup(name);
449 if (id < 0 || !bch2_opt_is_inode_opt(id))
452 inode_opt_id = opt_to_inode_opt(id);
453 if (inode_opt_id < 0)
456 opt = bch2_opt_table + id;
458 if (!bch2_opt_defined_by_id(&opts, id))
462 !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))
465 v = bch2_opt_get_by_id(&opts, id);
466 bch2_opt_to_text(&out, c, c->disk_sb.sb, opt, v, 0);
470 if (out.allocation_failure) {
476 memcpy(buffer, out.buf, out.pos);
483 static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
484 struct dentry *dentry, struct inode *vinode,
485 const char *name, void *buffer, size_t size)
487 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
488 name, buffer, size, false);
491 struct inode_opt_set {
497 static int inode_opt_set_fn(struct bch_inode_info *inode,
498 struct bch_inode_unpacked *bi,
501 struct inode_opt_set *s = p;
504 bi->bi_fields_set |= 1U << s->id;
506 bi->bi_fields_set &= ~(1U << s->id);
508 bch2_inode_opt_set(bi, s->id, s->v);
513 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
514 struct mnt_idmap *idmap,
515 struct dentry *dentry, struct inode *vinode,
516 const char *name, const void *value,
517 size_t size, int flags)
519 struct bch_inode_info *inode = to_bch_ei(vinode);
520 struct bch_fs *c = inode->v.i_sb->s_fs_info;
521 const struct bch_option *opt;
523 struct inode_opt_set s;
524 int opt_id, inode_opt_id, ret;
526 opt_id = bch2_opt_lookup(name);
530 opt = bch2_opt_table + opt_id;
532 inode_opt_id = opt_to_inode_opt(opt_id);
533 if (inode_opt_id < 0)
541 buf = kmalloc(size + 1, GFP_KERNEL);
544 memcpy(buf, value, size);
547 ret = bch2_opt_parse(c, opt, buf, &v, NULL);
553 ret = bch2_opt_check_may_set(c, opt_id, v);
560 if (!IS_ROOT(dentry)) {
561 struct bch_inode_info *dir =
562 to_bch_ei(d_inode(dentry->d_parent));
564 s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id);
572 mutex_lock(&inode->ei_update_lock);
573 if (inode_opt_id == Inode_opt_project) {
575 * inode fields accessible via the xattr interface are stored
576 * with a +1 bias, so that 0 means unset:
578 ret = bch2_set_projid(c, inode, s.v ? s.v - 1 : 0);
583 ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
585 mutex_unlock(&inode->ei_update_lock);
588 (opt_id == Opt_background_compression ||
589 opt_id == Opt_background_target))
590 bch2_rebalance_add_work(c, inode->v.i_blocks);
592 return bch2_err_class(ret);
595 static const struct xattr_handler bch_xattr_bcachefs_handler = {
596 .prefix = "bcachefs.",
597 .get = bch2_xattr_bcachefs_get,
598 .set = bch2_xattr_bcachefs_set,
601 static int bch2_xattr_bcachefs_get_effective(
602 const struct xattr_handler *handler,
603 struct dentry *dentry, struct inode *vinode,
604 const char *name, void *buffer, size_t size)
606 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
607 name, buffer, size, true);
610 static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
611 .prefix = "bcachefs_effective.",
612 .get = bch2_xattr_bcachefs_get_effective,
613 .set = bch2_xattr_bcachefs_set,
616 #endif /* NO_BCACHEFS_FS */
618 const struct xattr_handler *bch2_xattr_handlers[] = {
619 &bch_xattr_user_handler,
620 #ifdef CONFIG_BCACHEFS_POSIX_ACL
621 &nop_posix_acl_access,
622 &nop_posix_acl_default,
624 &bch_xattr_trusted_handler,
625 &bch_xattr_security_handler,
626 #ifndef NO_BCACHEFS_FS
627 &bch_xattr_bcachefs_handler,
628 &bch_xattr_bcachefs_effective_handler,
633 static const struct xattr_handler *bch_xattr_handler_map[] = {
634 [KEY_TYPE_XATTR_INDEX_USER] = &bch_xattr_user_handler,
635 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] =
636 &nop_posix_acl_access,
637 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT] =
638 &nop_posix_acl_default,
639 [KEY_TYPE_XATTR_INDEX_TRUSTED] = &bch_xattr_trusted_handler,
640 [KEY_TYPE_XATTR_INDEX_SECURITY] = &bch_xattr_security_handler,
643 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
645 return type < ARRAY_SIZE(bch_xattr_handler_map)
646 ? bch_xattr_handler_map[type]