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 const char *bch2_xattr_invalid(const struct bch_fs *c, struct bkey_s_c k)
74 const struct xattr_handler *handler;
75 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
77 if (bkey_val_bytes(k.k) < sizeof(struct bch_xattr))
78 return "value too small";
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 return "value too small";
85 if (bkey_val_u64s(k.k) >
86 xattr_val_u64s(xattr.v->x_name_len,
87 le16_to_cpu(xattr.v->x_val_len) + 4))
88 return "value too big";
90 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
92 return "invalid type";
94 if (memchr(xattr.v->x_name, '\0', xattr.v->x_name_len))
95 return "xattr name has invalid characters";
100 void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
103 const struct xattr_handler *handler;
104 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
106 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
107 if (handler && handler->prefix)
108 pr_buf(out, "%s", handler->prefix);
110 pr_buf(out, "(type %u)", xattr.v->x_type);
112 pr_buf(out, "(unknown type %u)", xattr.v->x_type);
114 bch_scnmemcpy(out, xattr.v->x_name,
115 xattr.v->x_name_len);
117 bch_scnmemcpy(out, xattr_val(xattr.v),
118 le16_to_cpu(xattr.v->x_val_len));
121 static int bch2_xattr_get_trans(struct btree_trans *trans, struct bch_inode_info *inode,
122 const char *name, void *buffer, size_t size, int type)
124 struct bch_hash_info hash = bch2_hash_info_init(trans->c, &inode->ei_inode);
125 struct btree_iter *iter;
126 struct bkey_s_c_xattr xattr;
130 iter = bch2_hash_lookup(trans, bch2_xattr_hash_desc, &hash,
132 &X_SEARCH(type, name, strlen(name)),
134 ret = PTR_ERR_OR_ZERO(iter);
138 k = bch2_btree_iter_peek_slot(iter);
143 xattr = bkey_s_c_to_xattr(k);
144 ret = le16_to_cpu(xattr.v->x_val_len);
149 memcpy(buffer, xattr_val(xattr.v), ret);
151 bch2_trans_iter_put(trans, iter);
153 return ret == -ENOENT ? -ENODATA : ret;
156 int bch2_xattr_get(struct bch_fs *c, struct bch_inode_info *inode,
157 const char *name, void *buffer, size_t size, int type)
159 return bch2_trans_do(c, NULL, NULL, 0,
160 bch2_xattr_get_trans(&trans, inode, name, buffer, size, type));
163 int bch2_xattr_set(struct btree_trans *trans, u64 inum,
164 const struct bch_hash_info *hash_info,
165 const char *name, const void *value, size_t size,
171 struct bkey_i_xattr *xattr;
172 unsigned namelen = strlen(name);
173 unsigned u64s = BKEY_U64s +
174 xattr_val_u64s(namelen, size);
179 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
181 return PTR_ERR(xattr);
183 bkey_xattr_init(&xattr->k_i);
184 xattr->k.u64s = u64s;
185 xattr->v.x_type = type;
186 xattr->v.x_name_len = namelen;
187 xattr->v.x_val_len = cpu_to_le16(size);
188 memcpy(xattr->v.x_name, name, namelen);
189 memcpy(xattr_val(&xattr->v), value, size);
191 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
193 (flags & XATTR_CREATE ? BCH_HASH_SET_MUST_CREATE : 0)|
194 (flags & XATTR_REPLACE ? BCH_HASH_SET_MUST_REPLACE : 0));
196 struct xattr_search_key search =
197 X_SEARCH(type, name, strlen(name));
199 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,
200 hash_info, inum, &search);
204 ret = flags & XATTR_REPLACE ? -ENODATA : 0;
215 static int __bch2_xattr_emit(const char *prefix,
216 const char *name, size_t name_len,
217 struct xattr_buf *buf)
219 const size_t prefix_len = strlen(prefix);
220 const size_t total_len = prefix_len + name_len + 1;
223 if (buf->used + total_len > buf->len)
226 memcpy(buf->buf + buf->used, prefix, prefix_len);
227 memcpy(buf->buf + buf->used + prefix_len,
229 buf->buf[buf->used + prefix_len + name_len] = '\0';
232 buf->used += total_len;
236 static int bch2_xattr_emit(struct dentry *dentry,
237 const struct bch_xattr *xattr,
238 struct xattr_buf *buf)
240 const struct xattr_handler *handler =
241 bch2_xattr_type_to_handler(xattr->x_type);
243 return handler && (!handler->list || handler->list(dentry))
244 ? __bch2_xattr_emit(handler->prefix ?: handler->name,
245 xattr->x_name, xattr->x_name_len, buf)
249 static int bch2_xattr_list_bcachefs(struct bch_fs *c,
250 struct bch_inode_unpacked *inode,
251 struct xattr_buf *buf,
254 const char *prefix = all ? "bcachefs_effective." : "bcachefs.";
259 for (id = 0; id < Inode_opt_nr; id++) {
260 v = bch2_inode_opt_get(inode, id);
265 !(inode->bi_fields_set & (1 << id)))
268 ret = __bch2_xattr_emit(prefix, bch2_inode_opts[id],
269 strlen(bch2_inode_opts[id]), buf);
277 ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
279 struct bch_fs *c = dentry->d_sb->s_fs_info;
280 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
281 struct btree_trans trans;
282 struct btree_iter *iter;
284 struct xattr_buf buf = { .buf = buffer, .len = buffer_size };
285 u64 inum = dentry->d_inode->i_ino;
288 bch2_trans_init(&trans, c, 0, 0);
290 for_each_btree_key(&trans, iter, BTREE_ID_xattrs,
291 POS(inum, 0), 0, k, ret) {
292 BUG_ON(k.k->p.inode < inum);
294 if (k.k->p.inode > inum)
297 if (k.k->type != KEY_TYPE_xattr)
300 ret = bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf);
304 bch2_trans_iter_put(&trans, iter);
306 ret = bch2_trans_exit(&trans) ?: ret;
311 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, false);
315 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, true);
322 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
323 struct dentry *dentry, struct inode *vinode,
324 const char *name, void *buffer, size_t size)
326 struct bch_inode_info *inode = to_bch_ei(vinode);
327 struct bch_fs *c = inode->v.i_sb->s_fs_info;
329 return bch2_xattr_get(c, inode, name, buffer, size, handler->flags);
332 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
333 struct user_namespace *mnt_userns,
334 struct dentry *dentry, struct inode *vinode,
335 const char *name, const void *value,
336 size_t size, int flags)
338 struct bch_inode_info *inode = to_bch_ei(vinode);
339 struct bch_fs *c = inode->v.i_sb->s_fs_info;
340 struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
342 return bch2_trans_do(c, NULL, &inode->ei_journal_seq, 0,
343 bch2_xattr_set(&trans, inode->v.i_ino, &hash,
345 handler->flags, flags));
348 static const struct xattr_handler bch_xattr_user_handler = {
349 .prefix = XATTR_USER_PREFIX,
350 .get = bch2_xattr_get_handler,
351 .set = bch2_xattr_set_handler,
352 .flags = KEY_TYPE_XATTR_INDEX_USER,
355 static bool bch2_xattr_trusted_list(struct dentry *dentry)
357 return capable(CAP_SYS_ADMIN);
360 static const struct xattr_handler bch_xattr_trusted_handler = {
361 .prefix = XATTR_TRUSTED_PREFIX,
362 .list = bch2_xattr_trusted_list,
363 .get = bch2_xattr_get_handler,
364 .set = bch2_xattr_set_handler,
365 .flags = KEY_TYPE_XATTR_INDEX_TRUSTED,
368 static const struct xattr_handler bch_xattr_security_handler = {
369 .prefix = XATTR_SECURITY_PREFIX,
370 .get = bch2_xattr_get_handler,
371 .set = bch2_xattr_set_handler,
372 .flags = KEY_TYPE_XATTR_INDEX_SECURITY,
375 #ifndef NO_BCACHEFS_FS
377 static int opt_to_inode_opt(int id)
380 #define x(name, ...) \
381 case Opt_##name: return Inode_opt_##name;
389 static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
390 struct dentry *dentry, struct inode *vinode,
391 const char *name, void *buffer, size_t size,
394 struct bch_inode_info *inode = to_bch_ei(vinode);
395 struct bch_fs *c = inode->v.i_sb->s_fs_info;
396 struct bch_opts opts =
397 bch2_inode_opts_to_opts(bch2_inode_opts_get(&inode->ei_inode));
398 const struct bch_option *opt;
399 int id, inode_opt_id;
401 struct printbuf out = PBUF(buf);
405 id = bch2_opt_lookup(name);
406 if (id < 0 || !bch2_opt_is_inode_opt(id))
409 inode_opt_id = opt_to_inode_opt(id);
410 if (inode_opt_id < 0)
413 opt = bch2_opt_table + id;
415 if (!bch2_opt_defined_by_id(&opts, id))
419 !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))
422 v = bch2_opt_get_by_id(&opts, id);
423 bch2_opt_to_text(&out, c, opt, v, 0);
425 val_len = out.pos - buf;
427 if (buffer && val_len > size)
431 memcpy(buffer, buf, val_len);
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)
439 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
440 name, buffer, size, false);
443 struct inode_opt_set {
449 static int inode_opt_set_fn(struct bch_inode_info *inode,
450 struct bch_inode_unpacked *bi,
453 struct inode_opt_set *s = p;
456 bi->bi_fields_set |= 1U << s->id;
458 bi->bi_fields_set &= ~(1U << s->id);
460 bch2_inode_opt_set(bi, s->id, s->v);
465 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
466 struct user_namespace *mnt_userns,
467 struct dentry *dentry, struct inode *vinode,
468 const char *name, const void *value,
469 size_t size, int flags)
471 struct bch_inode_info *inode = to_bch_ei(vinode);
472 struct bch_fs *c = inode->v.i_sb->s_fs_info;
473 const struct bch_option *opt;
475 struct inode_opt_set s;
476 int opt_id, inode_opt_id, ret;
478 opt_id = bch2_opt_lookup(name);
482 opt = bch2_opt_table + opt_id;
484 inode_opt_id = opt_to_inode_opt(opt_id);
485 if (inode_opt_id < 0)
493 buf = kmalloc(size + 1, GFP_KERNEL);
496 memcpy(buf, value, size);
499 ret = bch2_opt_parse(c, opt, buf, &v);
505 ret = bch2_opt_check_may_set(c, opt_id, v);
512 if (!IS_ROOT(dentry)) {
513 struct bch_inode_info *dir =
514 to_bch_ei(d_inode(dentry->d_parent));
516 s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id);
524 mutex_lock(&inode->ei_update_lock);
525 if (inode_opt_id == Inode_opt_project) {
527 * inode fields accessible via the xattr interface are stored
528 * with a +1 bias, so that 0 means unset:
530 ret = bch2_set_projid(c, inode, s.v ? s.v - 1 : 0);
535 ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
537 mutex_unlock(&inode->ei_update_lock);
540 (opt_id == Opt_background_compression ||
541 opt_id == Opt_background_target))
542 bch2_rebalance_add_work(c, inode->v.i_blocks);
547 static const struct xattr_handler bch_xattr_bcachefs_handler = {
548 .prefix = "bcachefs.",
549 .get = bch2_xattr_bcachefs_get,
550 .set = bch2_xattr_bcachefs_set,
553 static int bch2_xattr_bcachefs_get_effective(
554 const struct xattr_handler *handler,
555 struct dentry *dentry, struct inode *vinode,
556 const char *name, void *buffer, size_t size)
558 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
559 name, buffer, size, true);
562 static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
563 .prefix = "bcachefs_effective.",
564 .get = bch2_xattr_bcachefs_get_effective,
565 .set = bch2_xattr_bcachefs_set,
568 #endif /* NO_BCACHEFS_FS */
570 const struct xattr_handler *bch2_xattr_handlers[] = {
571 &bch_xattr_user_handler,
572 #ifdef CONFIG_BCACHEFS_POSIX_ACL
573 &posix_acl_access_xattr_handler,
574 &posix_acl_default_xattr_handler,
576 &bch_xattr_trusted_handler,
577 &bch_xattr_security_handler,
578 #ifndef NO_BCACHEFS_FS
579 &bch_xattr_bcachefs_handler,
580 &bch_xattr_bcachefs_effective_handler,
585 static const struct xattr_handler *bch_xattr_handler_map[] = {
586 [KEY_TYPE_XATTR_INDEX_USER] = &bch_xattr_user_handler,
587 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] =
588 &posix_acl_access_xattr_handler,
589 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT] =
590 &posix_acl_default_xattr_handler,
591 [KEY_TYPE_XATTR_INDEX_TRUSTED] = &bch_xattr_trusted_handler,
592 [KEY_TYPE_XATTR_INDEX_SECURITY] = &bch_xattr_security_handler,
595 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
597 return type < ARRAY_SIZE(bch_xattr_handler_map)
598 ? bch_xattr_handler_map[type]