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