]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/xattr.c
Update bcachefs sources to 3856459b1b bcachefs: bch2_btree_iter_peek_node_and_restart()
[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_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 -BCH_ERR_invalid_bkey;
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 -BCH_ERR_invalid_bkey;
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 -BCH_ERR_invalid_bkey;
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 -BCH_ERR_invalid_bkey;
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 -BCH_ERR_invalid_bkey;
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         int ret;
375
376         ret = bch2_xattr_get(c, inode, name, buffer, size, handler->flags);
377         return bch2_err_class(ret);
378 }
379
380 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
381                                   struct user_namespace *mnt_userns,
382                                   struct dentry *dentry, struct inode *vinode,
383                                   const char *name, const void *value,
384                                   size_t size, int flags)
385 {
386         struct bch_inode_info *inode = to_bch_ei(vinode);
387         struct bch_fs *c = inode->v.i_sb->s_fs_info;
388         struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
389         int ret;
390
391         ret = bch2_trans_do(c, NULL, NULL, 0,
392                         bch2_xattr_set(&trans, inode_inum(inode), &hash,
393                                        name, value, size,
394                                        handler->flags, flags));
395         return bch2_err_class(ret);
396 }
397
398 static const struct xattr_handler bch_xattr_user_handler = {
399         .prefix = XATTR_USER_PREFIX,
400         .get    = bch2_xattr_get_handler,
401         .set    = bch2_xattr_set_handler,
402         .flags  = KEY_TYPE_XATTR_INDEX_USER,
403 };
404
405 static bool bch2_xattr_trusted_list(struct dentry *dentry)
406 {
407         return capable(CAP_SYS_ADMIN);
408 }
409
410 static const struct xattr_handler bch_xattr_trusted_handler = {
411         .prefix = XATTR_TRUSTED_PREFIX,
412         .list   = bch2_xattr_trusted_list,
413         .get    = bch2_xattr_get_handler,
414         .set    = bch2_xattr_set_handler,
415         .flags  = KEY_TYPE_XATTR_INDEX_TRUSTED,
416 };
417
418 static const struct xattr_handler bch_xattr_security_handler = {
419         .prefix = XATTR_SECURITY_PREFIX,
420         .get    = bch2_xattr_get_handler,
421         .set    = bch2_xattr_set_handler,
422         .flags  = KEY_TYPE_XATTR_INDEX_SECURITY,
423 };
424
425 #ifndef NO_BCACHEFS_FS
426
427 static int opt_to_inode_opt(int id)
428 {
429         switch (id) {
430 #define x(name, ...)                            \
431         case Opt_##name: return Inode_opt_##name;
432         BCH_INODE_OPTS()
433 #undef  x
434         default:
435                 return -1;
436         }
437 }
438
439 static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
440                                 struct dentry *dentry, struct inode *vinode,
441                                 const char *name, void *buffer, size_t size,
442                                 bool all)
443 {
444         struct bch_inode_info *inode = to_bch_ei(vinode);
445         struct bch_fs *c = inode->v.i_sb->s_fs_info;
446         struct bch_opts opts =
447                 bch2_inode_opts_to_opts(&inode->ei_inode);
448         const struct bch_option *opt;
449         int id, inode_opt_id;
450         struct printbuf out = PRINTBUF;
451         int ret;
452         u64 v;
453
454         id = bch2_opt_lookup(name);
455         if (id < 0 || !bch2_opt_is_inode_opt(id))
456                 return -EINVAL;
457
458         inode_opt_id = opt_to_inode_opt(id);
459         if (inode_opt_id < 0)
460                 return -EINVAL;
461
462         opt = bch2_opt_table + id;
463
464         if (!bch2_opt_defined_by_id(&opts, id))
465                 return -ENODATA;
466
467         if (!all &&
468             !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))
469                 return -ENODATA;
470
471         v = bch2_opt_get_by_id(&opts, id);
472         bch2_opt_to_text(&out, c, c->disk_sb.sb, opt, v, 0);
473
474         ret = out.pos;
475
476         if (out.allocation_failure) {
477                 ret = -ENOMEM;
478         } else if (buffer) {
479                 if (out.pos > size)
480                         ret = -ERANGE;
481                 else
482                         memcpy(buffer, out.buf, out.pos);
483         }
484
485         printbuf_exit(&out);
486         return ret;
487 }
488
489 static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
490                                    struct dentry *dentry, struct inode *vinode,
491                                    const char *name, void *buffer, size_t size)
492 {
493         return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
494                                          name, buffer, size, false);
495 }
496
497 struct inode_opt_set {
498         int                     id;
499         u64                     v;
500         bool                    defined;
501 };
502
503 static int inode_opt_set_fn(struct bch_inode_info *inode,
504                             struct bch_inode_unpacked *bi,
505                             void *p)
506 {
507         struct inode_opt_set *s = p;
508
509         if (s->defined)
510                 bi->bi_fields_set |= 1U << s->id;
511         else
512                 bi->bi_fields_set &= ~(1U << s->id);
513
514         bch2_inode_opt_set(bi, s->id, s->v);
515
516         return 0;
517 }
518
519 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
520                                    struct user_namespace *mnt_userns,
521                                    struct dentry *dentry, struct inode *vinode,
522                                    const char *name, const void *value,
523                                    size_t size, int flags)
524 {
525         struct bch_inode_info *inode = to_bch_ei(vinode);
526         struct bch_fs *c = inode->v.i_sb->s_fs_info;
527         const struct bch_option *opt;
528         char *buf;
529         struct inode_opt_set s;
530         int opt_id, inode_opt_id, ret;
531
532         opt_id = bch2_opt_lookup(name);
533         if (opt_id < 0)
534                 return -EINVAL;
535
536         opt = bch2_opt_table + opt_id;
537
538         inode_opt_id = opt_to_inode_opt(opt_id);
539         if (inode_opt_id < 0)
540                 return -EINVAL;
541
542         s.id = inode_opt_id;
543
544         if (value) {
545                 u64 v = 0;
546
547                 buf = kmalloc(size + 1, GFP_KERNEL);
548                 if (!buf)
549                         return -ENOMEM;
550                 memcpy(buf, value, size);
551                 buf[size] = '\0';
552
553                 ret = bch2_opt_parse(c, opt, buf, &v, NULL);
554                 kfree(buf);
555
556                 if (ret < 0)
557                         return ret;
558
559                 ret = bch2_opt_check_may_set(c, opt_id, v);
560                 if (ret < 0)
561                         return ret;
562
563                 s.v = v + 1;
564                 s.defined = true;
565         } else {
566                 if (!IS_ROOT(dentry)) {
567                         struct bch_inode_info *dir =
568                                 to_bch_ei(d_inode(dentry->d_parent));
569
570                         s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id);
571                 } else {
572                         s.v = 0;
573                 }
574
575                 s.defined = false;
576         }
577
578         mutex_lock(&inode->ei_update_lock);
579         if (inode_opt_id == Inode_opt_project) {
580                 /*
581                  * inode fields accessible via the xattr interface are stored
582                  * with a +1 bias, so that 0 means unset:
583                  */
584                 ret = bch2_set_projid(c, inode, s.v ? s.v - 1 : 0);
585                 if (ret)
586                         goto err;
587         }
588
589         ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
590 err:
591         mutex_unlock(&inode->ei_update_lock);
592
593         if (value &&
594             (opt_id == Opt_background_compression ||
595              opt_id == Opt_background_target))
596                 bch2_rebalance_add_work(c, inode->v.i_blocks);
597
598         return ret;
599 }
600
601 static 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
607 static 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
616 static 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
624 const struct xattr_handler *bch2_xattr_handlers[] = {
625         &bch_xattr_user_handler,
626 #ifdef CONFIG_BCACHEFS_POSIX_ACL
627         &posix_acl_access_xattr_handler,
628         &posix_acl_default_xattr_handler,
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
639 static 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                 &posix_acl_access_xattr_handler,
643         [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT]        =
644                 &posix_acl_default_xattr_handler,
645         [KEY_TYPE_XATTR_INDEX_TRUSTED]          = &bch_xattr_trusted_handler,
646         [KEY_TYPE_XATTR_INDEX_SECURITY]         = &bch_xattr_security_handler,
647 };
648
649 static 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 }