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