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);
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;
200 static void __bch2_xattr_emit(const char *prefix,
201 const char *name, size_t name_len,
202 char **buffer, size_t *buffer_size,
205 const size_t prefix_len = strlen(prefix);
206 const size_t total_len = prefix_len + name_len + 1;
209 if (total_len > *buffer_size) {
214 memcpy(*buffer, prefix, prefix_len);
215 memcpy(*buffer + prefix_len,
217 (*buffer)[prefix_len + name_len] = '\0';
219 *buffer += total_len;
220 *buffer_size -= total_len;
226 static void bch2_xattr_emit(struct dentry *dentry,
227 const struct bch_xattr *xattr,
228 char **buffer, size_t *buffer_size,
231 const struct xattr_handler *handler =
232 bch2_xattr_type_to_handler(xattr->x_type);
234 if (handler && (!handler->list || handler->list(dentry)))
235 __bch2_xattr_emit(handler->prefix ?: handler->name,
236 xattr->x_name, xattr->x_name_len,
237 buffer, buffer_size, ret);
240 static void bch2_xattr_list_bcachefs(struct bch_fs *c,
241 struct bch_inode_info *inode,
247 const char *prefix = all ? "bcachefs_effective." : "bcachefs.";
251 for (id = 0; id < Inode_opt_nr; id++) {
252 v = bch2_inode_opt_get(&inode->ei_inode, id);
257 !(inode->ei_inode.bi_fields_set & (1 << id)))
260 __bch2_xattr_emit(prefix,
262 strlen(bch2_inode_opts[id]),
263 buffer, buffer_size, ret);
269 ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
271 struct bch_fs *c = dentry->d_sb->s_fs_info;
272 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
273 struct btree_iter iter;
275 u64 inum = dentry->d_inode->i_ino;
278 for_each_btree_key(&iter, c, BTREE_ID_XATTRS, POS(inum, 0), 0, k) {
279 BUG_ON(k.k->p.inode < inum);
281 if (k.k->p.inode > inum)
284 if (k.k->type != KEY_TYPE_xattr)
287 bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v,
288 &buffer, &buffer_size, &ret);
292 bch2_btree_iter_unlock(&iter);
297 bch2_xattr_list_bcachefs(c, inode, &buffer,
298 &buffer_size, &ret, false);
302 bch2_xattr_list_bcachefs(c, inode, &buffer,
303 &buffer_size, &ret, true);
310 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
311 struct dentry *dentry, struct inode *vinode,
312 const char *name, void *buffer, size_t size)
314 struct bch_inode_info *inode = to_bch_ei(vinode);
315 struct bch_fs *c = inode->v.i_sb->s_fs_info;
317 return bch2_xattr_get(c, inode, name, buffer, size, handler->flags);
320 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
321 struct dentry *dentry, struct inode *vinode,
322 const char *name, const void *value,
323 size_t size, int flags)
325 struct bch_inode_info *inode = to_bch_ei(vinode);
326 struct bch_fs *c = inode->v.i_sb->s_fs_info;
328 return bch2_trans_do(c, &inode->ei_journal_seq, BTREE_INSERT_ATOMIC,
329 bch2_xattr_set(&trans, inode->v.i_ino,
332 handler->flags, flags));
335 static const struct xattr_handler bch_xattr_user_handler = {
336 .prefix = XATTR_USER_PREFIX,
337 .get = bch2_xattr_get_handler,
338 .set = bch2_xattr_set_handler,
339 .flags = KEY_TYPE_XATTR_INDEX_USER,
342 static bool bch2_xattr_trusted_list(struct dentry *dentry)
344 return capable(CAP_SYS_ADMIN);
347 static const struct xattr_handler bch_xattr_trusted_handler = {
348 .prefix = XATTR_TRUSTED_PREFIX,
349 .list = bch2_xattr_trusted_list,
350 .get = bch2_xattr_get_handler,
351 .set = bch2_xattr_set_handler,
352 .flags = KEY_TYPE_XATTR_INDEX_TRUSTED,
355 static const struct xattr_handler bch_xattr_security_handler = {
356 .prefix = XATTR_SECURITY_PREFIX,
357 .get = bch2_xattr_get_handler,
358 .set = bch2_xattr_set_handler,
359 .flags = KEY_TYPE_XATTR_INDEX_SECURITY,
362 #ifndef NO_BCACHEFS_FS
364 static int opt_to_inode_opt(int id)
367 #define x(name, ...) \
368 case Opt_##name: return Inode_opt_##name;
376 static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
377 struct dentry *dentry, struct inode *vinode,
378 const char *name, void *buffer, size_t size,
381 struct bch_inode_info *inode = to_bch_ei(vinode);
382 struct bch_fs *c = inode->v.i_sb->s_fs_info;
383 struct bch_opts opts =
384 bch2_inode_opts_to_opts(bch2_inode_opts_get(&inode->ei_inode));
385 const struct bch_option *opt;
386 int id, inode_opt_id;
389 id = bch2_opt_lookup(name);
390 if (id < 0 || !bch2_opt_is_inode_opt(id))
393 inode_opt_id = opt_to_inode_opt(id);
394 if (inode_opt_id < 0)
397 opt = bch2_opt_table + id;
399 if (!bch2_opt_defined_by_id(&opts, id))
403 !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))
406 v = bch2_opt_get_by_id(&opts, id);
410 struct printbuf out = PBUF(buf);
412 bch2_opt_to_text(&out, c, opt, v, 0);
414 return out.pos - buf;
416 struct printbuf out = _PBUF(buffer, size);
418 bch2_opt_to_text(&out, c, opt, v, 0);
420 return printbuf_remaining(&out)
421 ? (void *) out.pos - buffer
426 static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
427 struct dentry *dentry, struct inode *vinode,
428 const char *name, void *buffer, size_t size)
430 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
431 name, buffer, size, false);
434 struct inode_opt_set {
440 static int inode_opt_set_fn(struct bch_inode_info *inode,
441 struct bch_inode_unpacked *bi,
444 struct inode_opt_set *s = p;
447 bi->bi_fields_set |= 1U << s->id;
449 bi->bi_fields_set &= ~(1U << s->id);
451 bch2_inode_opt_set(bi, s->id, s->v);
456 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
457 struct dentry *dentry, struct inode *vinode,
458 const char *name, const void *value,
459 size_t size, int flags)
461 struct bch_inode_info *inode = to_bch_ei(vinode);
462 struct bch_fs *c = inode->v.i_sb->s_fs_info;
463 const struct bch_option *opt;
465 struct inode_opt_set s;
466 int opt_id, inode_opt_id, ret;
468 opt_id = bch2_opt_lookup(name);
472 opt = bch2_opt_table + opt_id;
474 inode_opt_id = opt_to_inode_opt(opt_id);
475 if (inode_opt_id < 0)
483 buf = kmalloc(size + 1, GFP_KERNEL);
486 memcpy(buf, value, size);
489 ret = bch2_opt_parse(c, opt, buf, &v);
495 ret = bch2_opt_check_may_set(c, opt_id, v);
502 if (!IS_ROOT(dentry)) {
503 struct bch_inode_info *dir =
504 to_bch_ei(d_inode(dentry->d_parent));
506 s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id);
514 mutex_lock(&inode->ei_update_lock);
515 if (inode_opt_id == Inode_opt_project) {
516 ret = bch2_set_projid(c, inode, s.v);
521 ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
523 mutex_unlock(&inode->ei_update_lock);
526 (opt_id == Opt_background_compression ||
527 opt_id == Opt_background_target))
528 bch2_rebalance_add_work(c, inode->v.i_blocks);
533 static const struct xattr_handler bch_xattr_bcachefs_handler = {
534 .prefix = "bcachefs.",
535 .get = bch2_xattr_bcachefs_get,
536 .set = bch2_xattr_bcachefs_set,
539 static int bch2_xattr_bcachefs_get_effective(
540 const struct xattr_handler *handler,
541 struct dentry *dentry, struct inode *vinode,
542 const char *name, void *buffer, size_t size)
544 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
545 name, buffer, size, true);
548 static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
549 .prefix = "bcachefs_effective.",
550 .get = bch2_xattr_bcachefs_get_effective,
551 .set = bch2_xattr_bcachefs_set,
554 #endif /* NO_BCACHEFS_FS */
556 const struct xattr_handler *bch2_xattr_handlers[] = {
557 &bch_xattr_user_handler,
558 &posix_acl_access_xattr_handler,
559 &posix_acl_default_xattr_handler,
560 &bch_xattr_trusted_handler,
561 &bch_xattr_security_handler,
562 #ifndef NO_BCACHEFS_FS
563 &bch_xattr_bcachefs_handler,
564 &bch_xattr_bcachefs_effective_handler,
569 static const struct xattr_handler *bch_xattr_handler_map[] = {
570 [KEY_TYPE_XATTR_INDEX_USER] = &bch_xattr_user_handler,
571 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] =
572 &posix_acl_access_xattr_handler,
573 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT] =
574 &posix_acl_default_xattr_handler,
575 [KEY_TYPE_XATTR_INDEX_TRUSTED] = &bch_xattr_trusted_handler,
576 [KEY_TYPE_XATTR_INDEX_SECURITY] = &bch_xattr_security_handler,
579 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
581 return type < ARRAY_SIZE(bch_xattr_handler_map)
582 ? bch_xattr_handler_map[type]