3 #include "bkey_methods.h"
4 #include "btree_update.h"
11 #include <linux/dcache.h>
12 #include <linux/posix_acl_xattr.h>
13 #include <linux/xattr.h>
15 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned);
17 static u64 bch2_xattr_hash(const struct bch_hash_info *info,
18 const struct xattr_search_key *key)
20 struct bch_str_hash_ctx ctx;
22 bch2_str_hash_init(&ctx, info);
23 bch2_str_hash_update(&ctx, info, &key->type, sizeof(key->type));
24 bch2_str_hash_update(&ctx, info, key->name.name, key->name.len);
26 return bch2_str_hash_end(&ctx, info);
29 static u64 xattr_hash_key(const struct bch_hash_info *info, const void *key)
31 return bch2_xattr_hash(info, key);
34 static u64 xattr_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
36 struct bkey_s_c_xattr x = bkey_s_c_to_xattr(k);
38 return bch2_xattr_hash(info,
39 &X_SEARCH(x.v->x_type, x.v->x_name, x.v->x_name_len));
42 static bool xattr_cmp_key(struct bkey_s_c _l, const void *_r)
44 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
45 const struct xattr_search_key *r = _r;
47 return l.v->x_type != r->type ||
48 l.v->x_name_len != r->name.len ||
49 memcmp(l.v->x_name, r->name.name, r->name.len);
52 static bool xattr_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
54 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
55 struct bkey_s_c_xattr r = bkey_s_c_to_xattr(_r);
57 return l.v->x_type != r.v->x_type ||
58 l.v->x_name_len != r.v->x_name_len ||
59 memcmp(l.v->x_name, r.v->x_name, r.v->x_name_len);
62 const struct bch_hash_desc bch2_xattr_hash_desc = {
63 .btree_id = BTREE_ID_XATTRS,
64 .key_type = KEY_TYPE_xattr,
65 .hash_key = xattr_hash_key,
66 .hash_bkey = xattr_hash_bkey,
67 .cmp_key = xattr_cmp_key,
68 .cmp_bkey = xattr_cmp_bkey,
71 const char *bch2_xattr_invalid(const struct bch_fs *c, struct bkey_s_c k)
73 const struct xattr_handler *handler;
74 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
76 if (bkey_val_bytes(k.k) < sizeof(struct bch_xattr))
77 return "value too small";
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 return "value too small";
84 if (bkey_val_u64s(k.k) >
85 xattr_val_u64s(xattr.v->x_name_len,
86 le16_to_cpu(xattr.v->x_val_len) + 4))
87 return "value too big";
89 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
91 return "invalid type";
93 if (memchr(xattr.v->x_name, '\0', xattr.v->x_name_len))
94 return "xattr name has invalid characters";
99 void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
102 const struct xattr_handler *handler;
103 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
105 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
106 if (handler && handler->prefix)
107 pr_buf(out, "%s", handler->prefix);
109 pr_buf(out, "(type %u)", xattr.v->x_type);
111 pr_buf(out, "(unknown type %u)", xattr.v->x_type);
113 bch_scnmemcpy(out, xattr.v->x_name,
114 xattr.v->x_name_len);
116 bch_scnmemcpy(out, xattr_val(xattr.v),
117 le16_to_cpu(xattr.v->x_val_len));
120 int bch2_xattr_get(struct bch_fs *c, struct bch_inode_info *inode,
121 const char *name, void *buffer, size_t size, int type)
123 struct btree_trans trans;
124 struct btree_iter *iter;
125 struct bkey_s_c_xattr xattr;
128 bch2_trans_init(&trans, c, 0, 0);
130 iter = bch2_hash_lookup(&trans, bch2_xattr_hash_desc,
131 &inode->ei_str_hash, inode->v.i_ino,
132 &X_SEARCH(type, name, strlen(name)),
135 bch2_trans_exit(&trans);
136 BUG_ON(PTR_ERR(iter) == -EINTR);
138 return PTR_ERR(iter) == -ENOENT ? -ENODATA : PTR_ERR(iter);
141 xattr = bkey_s_c_to_xattr(bch2_btree_iter_peek_slot(iter));
142 ret = le16_to_cpu(xattr.v->x_val_len);
147 memcpy(buffer, xattr_val(xattr.v), ret);
150 bch2_trans_exit(&trans);
154 int bch2_xattr_set(struct btree_trans *trans, u64 inum,
155 const struct bch_hash_info *hash_info,
156 const char *name, const void *value, size_t size,
162 struct bkey_i_xattr *xattr;
163 unsigned namelen = strlen(name);
164 unsigned u64s = BKEY_U64s +
165 xattr_val_u64s(namelen, size);
170 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
172 return PTR_ERR(xattr);
174 bkey_xattr_init(&xattr->k_i);
175 xattr->k.u64s = u64s;
176 xattr->v.x_type = type;
177 xattr->v.x_name_len = namelen;
178 xattr->v.x_val_len = cpu_to_le16(size);
179 memcpy(xattr->v.x_name, name, namelen);
180 memcpy(xattr_val(&xattr->v), value, size);
182 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
184 (flags & XATTR_CREATE ? BCH_HASH_SET_MUST_CREATE : 0)|
185 (flags & XATTR_REPLACE ? BCH_HASH_SET_MUST_REPLACE : 0));
187 struct xattr_search_key search =
188 X_SEARCH(type, name, strlen(name));
190 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,
191 hash_info, inum, &search);
195 ret = flags & XATTR_REPLACE ? -ENODATA : 0;
206 static int __bch2_xattr_emit(const char *prefix,
207 const char *name, size_t name_len,
208 struct xattr_buf *buf)
210 const size_t prefix_len = strlen(prefix);
211 const size_t total_len = prefix_len + name_len + 1;
214 if (buf->used + total_len > buf->len)
217 memcpy(buf->buf + buf->used, prefix, prefix_len);
218 memcpy(buf->buf + buf->used + prefix_len,
220 buf->buf[buf->used + prefix_len + name_len] = '\0';
223 buf->used += total_len;
227 static int bch2_xattr_emit(struct dentry *dentry,
228 const struct bch_xattr *xattr,
229 struct xattr_buf *buf)
231 const struct xattr_handler *handler =
232 bch2_xattr_type_to_handler(xattr->x_type);
234 return handler && (!handler->list || handler->list(dentry))
235 ? __bch2_xattr_emit(handler->prefix ?: handler->name,
236 xattr->x_name, xattr->x_name_len, buf)
240 static int bch2_xattr_list_bcachefs(struct bch_fs *c,
241 struct bch_inode_info *inode,
242 struct xattr_buf *buf,
245 const char *prefix = all ? "bcachefs_effective." : "bcachefs.";
250 for (id = 0; id < Inode_opt_nr; id++) {
251 v = bch2_inode_opt_get(&inode->ei_inode, id);
256 !(inode->ei_inode.bi_fields_set & (1 << id)))
259 ret = __bch2_xattr_emit(prefix, bch2_inode_opts[id],
260 strlen(bch2_inode_opts[id]), buf);
268 ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
270 struct bch_fs *c = dentry->d_sb->s_fs_info;
271 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
272 struct btree_trans trans;
273 struct btree_iter *iter;
275 struct xattr_buf buf = { .buf = buffer, .len = buffer_size };
276 u64 inum = dentry->d_inode->i_ino;
279 bch2_trans_init(&trans, c, 0, 0);
281 for_each_btree_key(&trans, iter, BTREE_ID_XATTRS,
282 POS(inum, 0), 0, k, ret) {
283 BUG_ON(k.k->p.inode < inum);
285 if (k.k->p.inode > inum)
288 if (k.k->type != KEY_TYPE_xattr)
291 ret = bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf);
295 ret = bch2_trans_exit(&trans) ?: ret;
300 ret = bch2_xattr_list_bcachefs(c, inode, &buf, false);
304 ret = bch2_xattr_list_bcachefs(c, inode, &buf, true);
311 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
312 struct dentry *dentry, struct inode *vinode,
313 const char *name, void *buffer, size_t size)
315 struct bch_inode_info *inode = to_bch_ei(vinode);
316 struct bch_fs *c = inode->v.i_sb->s_fs_info;
318 return bch2_xattr_get(c, inode, name, buffer, size, handler->flags);
321 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
322 struct dentry *dentry, struct inode *vinode,
323 const char *name, const void *value,
324 size_t size, int flags)
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_trans_do(c, &inode->ei_journal_seq, BTREE_INSERT_ATOMIC,
330 bch2_xattr_set(&trans, inode->v.i_ino,
333 handler->flags, flags));
336 static const struct xattr_handler bch_xattr_user_handler = {
337 .prefix = XATTR_USER_PREFIX,
338 .get = bch2_xattr_get_handler,
339 .set = bch2_xattr_set_handler,
340 .flags = KEY_TYPE_XATTR_INDEX_USER,
343 static bool bch2_xattr_trusted_list(struct dentry *dentry)
345 return capable(CAP_SYS_ADMIN);
348 static const struct xattr_handler bch_xattr_trusted_handler = {
349 .prefix = XATTR_TRUSTED_PREFIX,
350 .list = bch2_xattr_trusted_list,
351 .get = bch2_xattr_get_handler,
352 .set = bch2_xattr_set_handler,
353 .flags = KEY_TYPE_XATTR_INDEX_TRUSTED,
356 static const struct xattr_handler bch_xattr_security_handler = {
357 .prefix = XATTR_SECURITY_PREFIX,
358 .get = bch2_xattr_get_handler,
359 .set = bch2_xattr_set_handler,
360 .flags = KEY_TYPE_XATTR_INDEX_SECURITY,
363 #ifndef NO_BCACHEFS_FS
365 static int opt_to_inode_opt(int id)
368 #define x(name, ...) \
369 case Opt_##name: return Inode_opt_##name;
377 static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
378 struct dentry *dentry, struct inode *vinode,
379 const char *name, void *buffer, size_t size,
382 struct bch_inode_info *inode = to_bch_ei(vinode);
383 struct bch_fs *c = inode->v.i_sb->s_fs_info;
384 struct bch_opts opts =
385 bch2_inode_opts_to_opts(bch2_inode_opts_get(&inode->ei_inode));
386 const struct bch_option *opt;
387 int id, inode_opt_id;
390 id = bch2_opt_lookup(name);
391 if (id < 0 || !bch2_opt_is_inode_opt(id))
394 inode_opt_id = opt_to_inode_opt(id);
395 if (inode_opt_id < 0)
398 opt = bch2_opt_table + id;
400 if (!bch2_opt_defined_by_id(&opts, id))
404 !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))
407 v = bch2_opt_get_by_id(&opts, id);
411 struct printbuf out = PBUF(buf);
413 bch2_opt_to_text(&out, c, opt, v, 0);
415 return out.pos - buf;
417 struct printbuf out = _PBUF(buffer, size);
419 bch2_opt_to_text(&out, c, opt, v, 0);
421 return printbuf_remaining(&out)
422 ? (void *) out.pos - buffer
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)
431 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
432 name, buffer, size, false);
435 struct inode_opt_set {
441 static int inode_opt_set_fn(struct bch_inode_info *inode,
442 struct bch_inode_unpacked *bi,
445 struct inode_opt_set *s = p;
448 bi->bi_fields_set |= 1U << s->id;
450 bi->bi_fields_set &= ~(1U << s->id);
452 bch2_inode_opt_set(bi, s->id, s->v);
457 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
458 struct dentry *dentry, struct inode *vinode,
459 const char *name, const void *value,
460 size_t size, int flags)
462 struct bch_inode_info *inode = to_bch_ei(vinode);
463 struct bch_fs *c = inode->v.i_sb->s_fs_info;
464 const struct bch_option *opt;
466 struct inode_opt_set s;
467 int opt_id, inode_opt_id, ret;
469 opt_id = bch2_opt_lookup(name);
473 opt = bch2_opt_table + opt_id;
475 inode_opt_id = opt_to_inode_opt(opt_id);
476 if (inode_opt_id < 0)
484 buf = kmalloc(size + 1, GFP_KERNEL);
487 memcpy(buf, value, size);
490 ret = bch2_opt_parse(c, opt, buf, &v);
496 ret = bch2_opt_check_may_set(c, opt_id, v);
503 if (!IS_ROOT(dentry)) {
504 struct bch_inode_info *dir =
505 to_bch_ei(d_inode(dentry->d_parent));
507 s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id);
515 mutex_lock(&inode->ei_update_lock);
516 if (inode_opt_id == Inode_opt_project) {
517 ret = bch2_set_projid(c, inode, s.v);
522 ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
524 mutex_unlock(&inode->ei_update_lock);
527 (opt_id == Opt_background_compression ||
528 opt_id == Opt_background_target))
529 bch2_rebalance_add_work(c, inode->v.i_blocks);
534 static const struct xattr_handler bch_xattr_bcachefs_handler = {
535 .prefix = "bcachefs.",
536 .get = bch2_xattr_bcachefs_get,
537 .set = bch2_xattr_bcachefs_set,
540 static int bch2_xattr_bcachefs_get_effective(
541 const struct xattr_handler *handler,
542 struct dentry *dentry, struct inode *vinode,
543 const char *name, void *buffer, size_t size)
545 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
546 name, buffer, size, true);
549 static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
550 .prefix = "bcachefs_effective.",
551 .get = bch2_xattr_bcachefs_get_effective,
552 .set = bch2_xattr_bcachefs_set,
555 #endif /* NO_BCACHEFS_FS */
557 const struct xattr_handler *bch2_xattr_handlers[] = {
558 &bch_xattr_user_handler,
559 &posix_acl_access_xattr_handler,
560 &posix_acl_default_xattr_handler,
561 &bch_xattr_trusted_handler,
562 &bch_xattr_security_handler,
563 #ifndef NO_BCACHEFS_FS
564 &bch_xattr_bcachefs_handler,
565 &bch_xattr_bcachefs_effective_handler,
570 static const struct xattr_handler *bch_xattr_handler_map[] = {
571 [KEY_TYPE_XATTR_INDEX_USER] = &bch_xattr_user_handler,
572 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] =
573 &posix_acl_access_xattr_handler,
574 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT] =
575 &posix_acl_default_xattr_handler,
576 [KEY_TYPE_XATTR_INDEX_TRUSTED] = &bch_xattr_trusted_handler,
577 [KEY_TYPE_XATTR_INDEX_SECURITY] = &bch_xattr_security_handler,
580 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
582 return type < ARRAY_SIZE(bch_xattr_handler_map)
583 ? bch_xattr_handler_map[type]