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