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 enum bkey_invalid_flags flags,
76 const struct xattr_handler *handler;
77 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
79 if (bkey_val_u64s(k.k) <
80 xattr_val_u64s(xattr.v->x_name_len,
81 le16_to_cpu(xattr.v->x_val_len))) {
82 prt_printf(err, "value too small (%zu < %u)",
84 xattr_val_u64s(xattr.v->x_name_len,
85 le16_to_cpu(xattr.v->x_val_len)));
86 return -BCH_ERR_invalid_bkey;
90 if (bkey_val_u64s(k.k) >
91 xattr_val_u64s(xattr.v->x_name_len,
92 le16_to_cpu(xattr.v->x_val_len) + 4)) {
93 prt_printf(err, "value too big (%zu > %u)",
95 xattr_val_u64s(xattr.v->x_name_len,
96 le16_to_cpu(xattr.v->x_val_len) + 4));
97 return -BCH_ERR_invalid_bkey;
100 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
102 prt_printf(err, "invalid type (%u)", xattr.v->x_type);
103 return -BCH_ERR_invalid_bkey;
106 if (memchr(xattr.v->x_name, '\0', xattr.v->x_name_len)) {
107 prt_printf(err, "xattr name has invalid characters");
108 return -BCH_ERR_invalid_bkey;
114 void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
117 const struct xattr_handler *handler;
118 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
120 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
121 if (handler && handler->prefix)
122 prt_printf(out, "%s", handler->prefix);
124 prt_printf(out, "(type %u)", xattr.v->x_type);
126 prt_printf(out, "(unknown type %u)", xattr.v->x_type);
128 prt_printf(out, "%.*s:%.*s",
131 le16_to_cpu(xattr.v->x_val_len),
132 (char *) xattr_val(xattr.v));
135 static int bch2_xattr_get_trans(struct btree_trans *trans, struct bch_inode_info *inode,
136 const char *name, void *buffer, size_t size, int type)
138 struct bch_hash_info hash = bch2_hash_info_init(trans->c, &inode->ei_inode);
139 struct xattr_search_key search = X_SEARCH(type, name, strlen(name));
140 struct btree_iter iter;
141 struct bkey_s_c_xattr xattr;
145 ret = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc, &hash,
146 inode_inum(inode), &search, 0);
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_set(struct btree_trans *trans, subvol_inum inum,
170 struct bch_inode_unpacked *inode_u,
171 const struct bch_hash_info *hash_info,
172 const char *name, const void *value, size_t size,
175 struct bch_fs *c = trans->c;
176 struct btree_iter inode_iter = { NULL };
179 ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_INTENT);
183 inode_u->bi_ctime = bch2_current_time(c);
185 ret = bch2_inode_write(trans, &inode_iter, inode_u);
186 bch2_trans_iter_exit(trans, &inode_iter);
192 struct bkey_i_xattr *xattr;
193 unsigned namelen = strlen(name);
194 unsigned u64s = BKEY_U64s +
195 xattr_val_u64s(namelen, size);
200 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
202 return PTR_ERR(xattr);
204 bkey_xattr_init(&xattr->k_i);
205 xattr->k.u64s = u64s;
206 xattr->v.x_type = type;
207 xattr->v.x_name_len = namelen;
208 xattr->v.x_val_len = cpu_to_le16(size);
209 memcpy(xattr->v.x_name, name, namelen);
210 memcpy(xattr_val(&xattr->v), value, size);
212 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
214 (flags & XATTR_CREATE ? BCH_HASH_SET_MUST_CREATE : 0)|
215 (flags & XATTR_REPLACE ? BCH_HASH_SET_MUST_REPLACE : 0));
217 struct xattr_search_key search =
218 X_SEARCH(type, name, strlen(name));
220 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,
221 hash_info, inum, &search);
224 if (bch2_err_matches(ret, ENOENT))
225 ret = flags & XATTR_REPLACE ? -ENODATA : 0;
236 static int __bch2_xattr_emit(const char *prefix,
237 const char *name, size_t name_len,
238 struct xattr_buf *buf)
240 const size_t prefix_len = strlen(prefix);
241 const size_t total_len = prefix_len + name_len + 1;
244 if (buf->used + total_len > buf->len)
247 memcpy(buf->buf + buf->used, prefix, prefix_len);
248 memcpy(buf->buf + buf->used + prefix_len,
250 buf->buf[buf->used + prefix_len + name_len] = '\0';
253 buf->used += total_len;
257 static int bch2_xattr_emit(struct dentry *dentry,
258 const struct bch_xattr *xattr,
259 struct xattr_buf *buf)
261 const struct xattr_handler *handler =
262 bch2_xattr_type_to_handler(xattr->x_type);
264 return handler && (!handler->list || handler->list(dentry))
265 ? __bch2_xattr_emit(handler->prefix ?: handler->name,
266 xattr->x_name, xattr->x_name_len, buf)
270 static int bch2_xattr_list_bcachefs(struct bch_fs *c,
271 struct bch_inode_unpacked *inode,
272 struct xattr_buf *buf,
275 const char *prefix = all ? "bcachefs_effective." : "bcachefs.";
280 for (id = 0; id < Inode_opt_nr; id++) {
281 v = bch2_inode_opt_get(inode, id);
286 !(inode->bi_fields_set & (1 << id)))
289 ret = __bch2_xattr_emit(prefix, bch2_inode_opts[id],
290 strlen(bch2_inode_opts[id]), buf);
298 ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
300 struct bch_fs *c = dentry->d_sb->s_fs_info;
301 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
302 struct btree_trans trans;
303 struct btree_iter iter;
305 struct xattr_buf buf = { .buf = buffer, .len = buffer_size };
306 u64 offset = 0, inum = inode->ei_inode.bi_inum;
310 bch2_trans_init(&trans, c, 0, 0);
312 bch2_trans_begin(&trans);
313 iter = (struct btree_iter) { NULL };
315 ret = bch2_subvolume_get_snapshot(&trans, inode->ei_subvol, &snapshot);
319 for_each_btree_key_upto_norestart(&trans, iter, BTREE_ID_xattrs,
320 SPOS(inum, offset, snapshot),
321 POS(inum, U64_MAX), 0, k, ret) {
322 if (k.k->type != KEY_TYPE_xattr)
325 ret = bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf);
330 offset = iter.pos.offset;
331 bch2_trans_iter_exit(&trans, &iter);
333 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
336 bch2_trans_exit(&trans);
341 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, false);
345 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, true);
351 return bch2_err_class(ret);
354 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
355 struct dentry *dentry, struct inode *vinode,
356 const char *name, void *buffer, size_t size)
358 struct bch_inode_info *inode = to_bch_ei(vinode);
359 struct bch_fs *c = inode->v.i_sb->s_fs_info;
360 int ret = bch2_trans_do(c, NULL, NULL, 0,
361 bch2_xattr_get_trans(&trans, inode, name, buffer, size, handler->flags));
363 return bch2_err_class(ret);
366 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
367 struct mnt_idmap *idmap,
368 struct dentry *dentry, struct inode *vinode,
369 const char *name, const void *value,
370 size_t size, int flags)
372 struct bch_inode_info *inode = to_bch_ei(vinode);
373 struct bch_fs *c = inode->v.i_sb->s_fs_info;
374 struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
375 struct bch_inode_unpacked inode_u;
376 struct btree_trans trans;
379 bch2_trans_init(&trans, c, 0, 0);
381 ret = commit_do(&trans, NULL, NULL, 0,
382 bch2_xattr_set(&trans, inode_inum(inode), &inode_u,
383 &hash, name, value, size,
384 handler->flags, flags));
386 bch2_inode_update_after_write(&trans, inode, &inode_u, ATTR_CTIME);
387 bch2_trans_exit(&trans);
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]