Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2
3#include "bcachefs.h"
4#include "acl.h"
5#include "bkey_methods.h"
6#include "btree_update.h"
7#include "extents.h"
8#include "fs.h"
9#include "rebalance.h"
10#include "str_hash.h"
11#include "xattr.h"
12
13#include <linux/dcache.h>
14#include <linux/posix_acl_xattr.h>
15#include <linux/xattr.h>
16
17static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned);
18
19static u64 bch2_xattr_hash(const struct bch_hash_info *info,
20 const struct xattr_search_key *key)
21{
22 struct bch_str_hash_ctx ctx;
23
24 bch2_str_hash_init(&ctx, info);
25 bch2_str_hash_update(&ctx, info, &key->type, sizeof(key->type));
26 bch2_str_hash_update(&ctx, info, key->name.name, key->name.len);
27
28 return bch2_str_hash_end(&ctx, info);
29}
30
31static u64 xattr_hash_key(const struct bch_hash_info *info, const void *key)
32{
33 return bch2_xattr_hash(info, key);
34}
35
36static u64 xattr_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
37{
38 struct bkey_s_c_xattr x = bkey_s_c_to_xattr(k);
39
40 return bch2_xattr_hash(info,
41 &X_SEARCH(x.v->x_type, x.v->x_name, x.v->x_name_len));
42}
43
44static bool xattr_cmp_key(struct bkey_s_c _l, const void *_r)
45{
46 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
47 const struct xattr_search_key *r = _r;
48
49 return l.v->x_type != r->type ||
50 l.v->x_name_len != r->name.len ||
51 memcmp(l.v->x_name, r->name.name, r->name.len);
52}
53
54static bool xattr_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
55{
56 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
57 struct bkey_s_c_xattr r = bkey_s_c_to_xattr(_r);
58
59 return l.v->x_type != r.v->x_type ||
60 l.v->x_name_len != r.v->x_name_len ||
61 memcmp(l.v->x_name, r.v->x_name, r.v->x_name_len);
62}
63
64const struct bch_hash_desc bch2_xattr_hash_desc = {
65 .btree_id = BTREE_ID_xattrs,
66 .key_type = KEY_TYPE_xattr,
67 .hash_key = xattr_hash_key,
68 .hash_bkey = xattr_hash_bkey,
69 .cmp_key = xattr_cmp_key,
70 .cmp_bkey = xattr_cmp_bkey,
71};
72
73int bch2_xattr_validate(struct bch_fs *c, struct bkey_s_c k,
74 enum bch_validate_flags flags)
75{
76 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
77 unsigned val_u64s = xattr_val_u64s(xattr.v->x_name_len,
78 le16_to_cpu(xattr.v->x_val_len));
79 int ret = 0;
80
81 bkey_fsck_err_on(bkey_val_u64s(k.k) < val_u64s,
82 c, xattr_val_size_too_small,
83 "value too small (%zu < %u)",
84 bkey_val_u64s(k.k), val_u64s);
85
86 /* XXX why +4 ? */
87 val_u64s = xattr_val_u64s(xattr.v->x_name_len,
88 le16_to_cpu(xattr.v->x_val_len) + 4);
89
90 bkey_fsck_err_on(bkey_val_u64s(k.k) > val_u64s,
91 c, xattr_val_size_too_big,
92 "value too big (%zu > %u)",
93 bkey_val_u64s(k.k), val_u64s);
94
95 bkey_fsck_err_on(!bch2_xattr_type_to_handler(xattr.v->x_type),
96 c, xattr_invalid_type,
97 "invalid type (%u)", xattr.v->x_type);
98
99 bkey_fsck_err_on(memchr(xattr.v->x_name, '\0', xattr.v->x_name_len),
100 c, xattr_name_invalid_chars,
101 "xattr name has invalid characters");
102fsck_err:
103 return ret;
104}
105
106void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
107 struct bkey_s_c k)
108{
109 const struct xattr_handler *handler;
110 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
111
112 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
113 if (handler && handler->prefix)
114 prt_printf(out, "%s", handler->prefix);
115 else if (handler)
116 prt_printf(out, "(type %u)", xattr.v->x_type);
117 else
118 prt_printf(out, "(unknown type %u)", xattr.v->x_type);
119
120 unsigned name_len = xattr.v->x_name_len;
121 unsigned val_len = le16_to_cpu(xattr.v->x_val_len);
122 unsigned max_name_val_bytes = bkey_val_bytes(xattr.k) -
123 offsetof(struct bch_xattr, x_name);
124
125 val_len = min_t(int, val_len, max_name_val_bytes - name_len);
126 name_len = min(name_len, max_name_val_bytes);
127
128 prt_printf(out, "%.*s:%.*s",
129 name_len, xattr.v->x_name,
130 val_len, (char *) xattr_val(xattr.v));
131
132 if (xattr.v->x_type == KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS ||
133 xattr.v->x_type == KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT) {
134 prt_char(out, ' ');
135 bch2_acl_to_text(out, xattr_val(xattr.v),
136 le16_to_cpu(xattr.v->x_val_len));
137 }
138}
139
140static int bch2_xattr_get_trans(struct btree_trans *trans, struct bch_inode_info *inode,
141 const char *name, void *buffer, size_t size, int type)
142{
143 struct bch_hash_info hash = bch2_hash_info_init(trans->c, &inode->ei_inode);
144 struct xattr_search_key search = X_SEARCH(type, name, strlen(name));
145 struct btree_iter iter;
146 struct bkey_s_c k = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc, &hash,
147 inode_inum(inode), &search, 0);
148 int ret = bkey_err(k);
149 if (ret)
150 return ret;
151
152 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
153 ret = le16_to_cpu(xattr.v->x_val_len);
154 if (buffer) {
155 if (ret > size)
156 ret = -ERANGE;
157 else
158 memcpy(buffer, xattr_val(xattr.v), ret);
159 }
160 bch2_trans_iter_exit(trans, &iter);
161 return ret;
162}
163
164int bch2_xattr_set(struct btree_trans *trans, subvol_inum inum,
165 struct bch_inode_unpacked *inode_u,
166 const struct bch_hash_info *hash_info,
167 const char *name, const void *value, size_t size,
168 int type, int flags)
169{
170 struct bch_fs *c = trans->c;
171 struct btree_iter inode_iter = { NULL };
172 int ret;
173
174 ret = bch2_subvol_is_ro_trans(trans, inum.subvol) ?:
175 bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_intent);
176 if (ret)
177 return ret;
178
179 inode_u->bi_ctime = bch2_current_time(c);
180
181 ret = bch2_inode_write(trans, &inode_iter, inode_u);
182 bch2_trans_iter_exit(trans, &inode_iter);
183
184 if (ret)
185 return ret;
186
187 if (value) {
188 struct bkey_i_xattr *xattr;
189 unsigned namelen = strlen(name);
190 unsigned u64s = BKEY_U64s +
191 xattr_val_u64s(namelen, size);
192
193 if (u64s > U8_MAX)
194 return -ERANGE;
195
196 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
197 if (IS_ERR(xattr))
198 return PTR_ERR(xattr);
199
200 bkey_xattr_init(&xattr->k_i);
201 xattr->k.u64s = u64s;
202 xattr->v.x_type = type;
203 xattr->v.x_name_len = namelen;
204 xattr->v.x_val_len = cpu_to_le16(size);
205 memcpy(xattr->v.x_name, name, namelen);
206 memcpy(xattr_val(&xattr->v), value, size);
207
208 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
209 inum, &xattr->k_i,
210 (flags & XATTR_CREATE ? STR_HASH_must_create : 0)|
211 (flags & XATTR_REPLACE ? STR_HASH_must_replace : 0));
212 } else {
213 struct xattr_search_key search =
214 X_SEARCH(type, name, strlen(name));
215
216 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,
217 hash_info, inum, &search);
218 }
219
220 if (bch2_err_matches(ret, ENOENT))
221 ret = flags & XATTR_REPLACE ? -ENODATA : 0;
222
223 return ret;
224}
225
226struct xattr_buf {
227 char *buf;
228 size_t len;
229 size_t used;
230};
231
232static int __bch2_xattr_emit(const char *prefix,
233 const char *name, size_t name_len,
234 struct xattr_buf *buf)
235{
236 const size_t prefix_len = strlen(prefix);
237 const size_t total_len = prefix_len + name_len + 1;
238
239 if (buf->buf) {
240 if (buf->used + total_len > buf->len)
241 return -ERANGE;
242
243 memcpy(buf->buf + buf->used, prefix, prefix_len);
244 memcpy(buf->buf + buf->used + prefix_len,
245 name, name_len);
246 buf->buf[buf->used + prefix_len + name_len] = '\0';
247 }
248
249 buf->used += total_len;
250 return 0;
251}
252
253static int bch2_xattr_emit(struct dentry *dentry,
254 const struct bch_xattr *xattr,
255 struct xattr_buf *buf)
256{
257 const struct xattr_handler *handler =
258 bch2_xattr_type_to_handler(xattr->x_type);
259
260 return handler && (!handler->list || handler->list(dentry))
261 ? __bch2_xattr_emit(handler->prefix ?: handler->name,
262 xattr->x_name, xattr->x_name_len, buf)
263 : 0;
264}
265
266static int bch2_xattr_list_bcachefs(struct bch_fs *c,
267 struct bch_inode_unpacked *inode,
268 struct xattr_buf *buf,
269 bool all)
270{
271 const char *prefix = all ? "bcachefs_effective." : "bcachefs.";
272 unsigned id;
273 int ret = 0;
274 u64 v;
275
276 for (id = 0; id < Inode_opt_nr; id++) {
277 v = bch2_inode_opt_get(inode, id);
278 if (!v)
279 continue;
280
281 if (!all &&
282 !(inode->bi_fields_set & (1 << id)))
283 continue;
284
285 ret = __bch2_xattr_emit(prefix, bch2_inode_opts[id],
286 strlen(bch2_inode_opts[id]), buf);
287 if (ret)
288 break;
289 }
290
291 return ret;
292}
293
294ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
295{
296 struct bch_fs *c = dentry->d_sb->s_fs_info;
297 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
298 struct btree_trans *trans = bch2_trans_get(c);
299 struct btree_iter iter;
300 struct bkey_s_c k;
301 struct xattr_buf buf = { .buf = buffer, .len = buffer_size };
302 u64 offset = 0, inum = inode->ei_inode.bi_inum;
303 u32 snapshot;
304 int ret;
305retry:
306 bch2_trans_begin(trans);
307 iter = (struct btree_iter) { NULL };
308
309 ret = bch2_subvolume_get_snapshot(trans, inode->ei_subvol, &snapshot);
310 if (ret)
311 goto err;
312
313 for_each_btree_key_upto_norestart(trans, iter, BTREE_ID_xattrs,
314 SPOS(inum, offset, snapshot),
315 POS(inum, U64_MAX), 0, k, ret) {
316 if (k.k->type != KEY_TYPE_xattr)
317 continue;
318
319 ret = bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf);
320 if (ret)
321 break;
322 }
323
324 offset = iter.pos.offset;
325 bch2_trans_iter_exit(trans, &iter);
326err:
327 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
328 goto retry;
329
330 bch2_trans_put(trans);
331
332 if (ret)
333 goto out;
334
335 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, false);
336 if (ret)
337 goto out;
338
339 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, true);
340 if (ret)
341 goto out;
342
343 return buf.used;
344out:
345 return bch2_err_class(ret);
346}
347
348static int bch2_xattr_get_handler(const struct xattr_handler *handler,
349 struct dentry *dentry, struct inode *vinode,
350 const char *name, void *buffer, size_t size)
351{
352 struct bch_inode_info *inode = to_bch_ei(vinode);
353 struct bch_fs *c = inode->v.i_sb->s_fs_info;
354 int ret = bch2_trans_do(c, NULL, NULL, 0,
355 bch2_xattr_get_trans(trans, inode, name, buffer, size, handler->flags));
356
357 if (ret < 0 && bch2_err_matches(ret, ENOENT))
358 ret = -ENODATA;
359
360 return bch2_err_class(ret);
361}
362
363static int bch2_xattr_set_handler(const struct xattr_handler *handler,
364 struct mnt_idmap *idmap,
365 struct dentry *dentry, struct inode *vinode,
366 const char *name, const void *value,
367 size_t size, int flags)
368{
369 struct bch_inode_info *inode = to_bch_ei(vinode);
370 struct bch_fs *c = inode->v.i_sb->s_fs_info;
371 struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
372 struct bch_inode_unpacked inode_u;
373 int ret;
374
375 ret = bch2_trans_run(c,
376 commit_do(trans, NULL, NULL, 0,
377 bch2_xattr_set(trans, inode_inum(inode), &inode_u,
378 &hash, name, value, size,
379 handler->flags, flags)) ?:
380 (bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME), 0));
381
382 return bch2_err_class(ret);
383}
384
385static const struct xattr_handler bch_xattr_user_handler = {
386 .prefix = XATTR_USER_PREFIX,
387 .get = bch2_xattr_get_handler,
388 .set = bch2_xattr_set_handler,
389 .flags = KEY_TYPE_XATTR_INDEX_USER,
390};
391
392static bool bch2_xattr_trusted_list(struct dentry *dentry)
393{
394 return capable(CAP_SYS_ADMIN);
395}
396
397static const struct xattr_handler bch_xattr_trusted_handler = {
398 .prefix = XATTR_TRUSTED_PREFIX,
399 .list = bch2_xattr_trusted_list,
400 .get = bch2_xattr_get_handler,
401 .set = bch2_xattr_set_handler,
402 .flags = KEY_TYPE_XATTR_INDEX_TRUSTED,
403};
404
405static const struct xattr_handler bch_xattr_security_handler = {
406 .prefix = XATTR_SECURITY_PREFIX,
407 .get = bch2_xattr_get_handler,
408 .set = bch2_xattr_set_handler,
409 .flags = KEY_TYPE_XATTR_INDEX_SECURITY,
410};
411
412#ifndef NO_BCACHEFS_FS
413
414static int opt_to_inode_opt(int id)
415{
416 switch (id) {
417#define x(name, ...) \
418 case Opt_##name: return Inode_opt_##name;
419 BCH_INODE_OPTS()
420#undef x
421 default:
422 return -1;
423 }
424}
425
426static 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,
429 bool all)
430{
431 struct bch_inode_info *inode = to_bch_ei(vinode);
432 struct bch_fs *c = inode->v.i_sb->s_fs_info;
433 struct bch_opts opts =
434 bch2_inode_opts_to_opts(&inode->ei_inode);
435 const struct bch_option *opt;
436 int id, inode_opt_id;
437 struct printbuf out = PRINTBUF;
438 int ret;
439 u64 v;
440
441 id = bch2_opt_lookup(name);
442 if (id < 0 || !bch2_opt_is_inode_opt(id))
443 return -EINVAL;
444
445 inode_opt_id = opt_to_inode_opt(id);
446 if (inode_opt_id < 0)
447 return -EINVAL;
448
449 opt = bch2_opt_table + id;
450
451 if (!bch2_opt_defined_by_id(&opts, id))
452 return -ENODATA;
453
454 if (!all &&
455 !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))
456 return -ENODATA;
457
458 v = bch2_opt_get_by_id(&opts, id);
459 bch2_opt_to_text(&out, c, c->disk_sb.sb, opt, v, 0);
460
461 ret = out.pos;
462
463 if (out.allocation_failure) {
464 ret = -ENOMEM;
465 } else if (buffer) {
466 if (out.pos > size)
467 ret = -ERANGE;
468 else
469 memcpy(buffer, out.buf, out.pos);
470 }
471
472 printbuf_exit(&out);
473 return ret;
474}
475
476static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
477 struct dentry *dentry, struct inode *vinode,
478 const char *name, void *buffer, size_t size)
479{
480 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
481 name, buffer, size, false);
482}
483
484struct inode_opt_set {
485 int id;
486 u64 v;
487 bool defined;
488};
489
490static int inode_opt_set_fn(struct btree_trans *trans,
491 struct bch_inode_info *inode,
492 struct bch_inode_unpacked *bi,
493 void *p)
494{
495 struct inode_opt_set *s = p;
496
497 if (s->defined)
498 bi->bi_fields_set |= 1U << s->id;
499 else
500 bi->bi_fields_set &= ~(1U << s->id);
501
502 bch2_inode_opt_set(bi, s->id, s->v);
503
504 return 0;
505}
506
507static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
508 struct mnt_idmap *idmap,
509 struct dentry *dentry, struct inode *vinode,
510 const char *name, const void *value,
511 size_t size, int flags)
512{
513 struct bch_inode_info *inode = to_bch_ei(vinode);
514 struct bch_fs *c = inode->v.i_sb->s_fs_info;
515 const struct bch_option *opt;
516 char *buf;
517 struct inode_opt_set s;
518 int opt_id, inode_opt_id, ret;
519
520 opt_id = bch2_opt_lookup(name);
521 if (opt_id < 0)
522 return -EINVAL;
523
524 opt = bch2_opt_table + opt_id;
525
526 inode_opt_id = opt_to_inode_opt(opt_id);
527 if (inode_opt_id < 0)
528 return -EINVAL;
529
530 s.id = inode_opt_id;
531
532 if (value) {
533 u64 v = 0;
534
535 buf = kmalloc(size + 1, GFP_KERNEL);
536 if (!buf)
537 return -ENOMEM;
538 memcpy(buf, value, size);
539 buf[size] = '\0';
540
541 ret = bch2_opt_parse(c, opt, buf, &v, NULL);
542 kfree(buf);
543
544 if (ret < 0)
545 goto err_class_exit;
546
547 ret = bch2_opt_check_may_set(c, opt_id, v);
548 if (ret < 0)
549 goto err_class_exit;
550
551 s.v = v + 1;
552 s.defined = true;
553 } else {
554 /*
555 * Check if this option was set on the parent - if so, switched
556 * back to inheriting from the parent:
557 *
558 * rename() also has to deal with keeping inherited options up
559 * to date - see bch2_reinherit_attrs()
560 */
561 spin_lock(&dentry->d_lock);
562 if (!IS_ROOT(dentry)) {
563 struct bch_inode_info *dir =
564 to_bch_ei(d_inode(dentry->d_parent));
565
566 s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id);
567 } else {
568 s.v = 0;
569 }
570 spin_unlock(&dentry->d_lock);
571
572 s.defined = false;
573 }
574
575 mutex_lock(&inode->ei_update_lock);
576 if (inode_opt_id == Inode_opt_project) {
577 /*
578 * inode fields accessible via the xattr interface are stored
579 * with a +1 bias, so that 0 means unset:
580 */
581 ret = bch2_set_projid(c, inode, s.v ? s.v - 1 : 0);
582 if (ret)
583 goto err;
584 }
585
586 ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
587err:
588 mutex_unlock(&inode->ei_update_lock);
589
590 if (value &&
591 (opt_id == Opt_background_target ||
592 opt_id == Opt_background_compression ||
593 (opt_id == Opt_compression && !inode_opt_get(c, &inode->ei_inode, background_compression))))
594 bch2_set_rebalance_needs_scan(c, inode->ei_inode.bi_inum);
595
596err_class_exit:
597 return bch2_err_class(ret);
598}
599
600static const struct xattr_handler bch_xattr_bcachefs_handler = {
601 .prefix = "bcachefs.",
602 .get = bch2_xattr_bcachefs_get,
603 .set = bch2_xattr_bcachefs_set,
604};
605
606static int bch2_xattr_bcachefs_get_effective(
607 const struct xattr_handler *handler,
608 struct dentry *dentry, struct inode *vinode,
609 const char *name, void *buffer, size_t size)
610{
611 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
612 name, buffer, size, true);
613}
614
615/* Noop - xattrs in the bcachefs_effective namespace are inherited */
616static int bch2_xattr_bcachefs_set_effective(const struct xattr_handler *handler,
617 struct mnt_idmap *idmap,
618 struct dentry *dentry, struct inode *vinode,
619 const char *name, const void *value,
620 size_t size, int flags)
621{
622 return 0;
623}
624
625static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
626 .prefix = "bcachefs_effective.",
627 .get = bch2_xattr_bcachefs_get_effective,
628 .set = bch2_xattr_bcachefs_set_effective,
629};
630
631#endif /* NO_BCACHEFS_FS */
632
633const struct xattr_handler *bch2_xattr_handlers[] = {
634 &bch_xattr_user_handler,
635#ifdef CONFIG_BCACHEFS_POSIX_ACL
636 &nop_posix_acl_access,
637 &nop_posix_acl_default,
638#endif
639 &bch_xattr_trusted_handler,
640 &bch_xattr_security_handler,
641#ifndef NO_BCACHEFS_FS
642 &bch_xattr_bcachefs_handler,
643 &bch_xattr_bcachefs_effective_handler,
644#endif
645 NULL
646};
647
648static const struct xattr_handler *bch_xattr_handler_map[] = {
649 [KEY_TYPE_XATTR_INDEX_USER] = &bch_xattr_user_handler,
650 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] =
651 &nop_posix_acl_access,
652 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT] =
653 &nop_posix_acl_default,
654 [KEY_TYPE_XATTR_INDEX_TRUSTED] = &bch_xattr_trusted_handler,
655 [KEY_TYPE_XATTR_INDEX_SECURITY] = &bch_xattr_security_handler,
656};
657
658static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
659{
660 return type < ARRAY_SIZE(bch_xattr_handler_map)
661 ? bch_xattr_handler_map[type]
662 : NULL;
663}