]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/xattr.c
Update bcachefs sources to e7f6215768 bcachefs: Fix snapshot_skiplist_good()
[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                        enum bkey_invalid_flags flags,
74                        struct printbuf *err)
75 {
76         const struct xattr_handler *handler;
77         struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
78
79         if (bkey_val_u64s(k.k) <
80             xattr_val_u64s(xattr.v->x_name_len,
81                            le16_to_cpu(xattr.v->x_val_len))) {
82                 prt_printf(err, "value too small (%zu < %u)",
83                        bkey_val_u64s(k.k),
84                        xattr_val_u64s(xattr.v->x_name_len,
85                                       le16_to_cpu(xattr.v->x_val_len)));
86                 return -BCH_ERR_invalid_bkey;
87         }
88
89         /* XXX why +4 ? */
90         if (bkey_val_u64s(k.k) >
91             xattr_val_u64s(xattr.v->x_name_len,
92                            le16_to_cpu(xattr.v->x_val_len) + 4)) {
93                 prt_printf(err, "value too big (%zu > %u)",
94                        bkey_val_u64s(k.k),
95                        xattr_val_u64s(xattr.v->x_name_len,
96                                       le16_to_cpu(xattr.v->x_val_len) + 4));
97                 return -BCH_ERR_invalid_bkey;
98         }
99
100         handler = bch2_xattr_type_to_handler(xattr.v->x_type);
101         if (!handler) {
102                 prt_printf(err, "invalid type (%u)", xattr.v->x_type);
103                 return -BCH_ERR_invalid_bkey;
104         }
105
106         if (memchr(xattr.v->x_name, '\0', xattr.v->x_name_len)) {
107                 prt_printf(err, "xattr name has invalid characters");
108                 return -BCH_ERR_invalid_bkey;
109         }
110
111         return 0;
112 }
113
114 void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
115                         struct bkey_s_c k)
116 {
117         const struct xattr_handler *handler;
118         struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
119
120         handler = bch2_xattr_type_to_handler(xattr.v->x_type);
121         if (handler && handler->prefix)
122                 prt_printf(out, "%s", handler->prefix);
123         else if (handler)
124                 prt_printf(out, "(type %u)", xattr.v->x_type);
125         else
126                 prt_printf(out, "(unknown type %u)", xattr.v->x_type);
127
128         prt_printf(out, "%.*s:%.*s",
129                xattr.v->x_name_len,
130                xattr.v->x_name,
131                le16_to_cpu(xattr.v->x_val_len),
132                (char *) xattr_val(xattr.v));
133 }
134
135 static int bch2_xattr_get_trans(struct btree_trans *trans, struct bch_inode_info *inode,
136                                 const char *name, void *buffer, size_t size, int type)
137 {
138         struct bch_hash_info hash = bch2_hash_info_init(trans->c, &inode->ei_inode);
139         struct xattr_search_key search = X_SEARCH(type, name, strlen(name));
140         struct btree_iter iter;
141         struct bkey_s_c_xattr xattr;
142         struct bkey_s_c k;
143         int ret;
144
145         ret = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc, &hash,
146                                inode_inum(inode), &search, 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_set(struct btree_trans *trans, subvol_inum inum,
170                    struct bch_inode_unpacked *inode_u,
171                    const struct bch_hash_info *hash_info,
172                    const char *name, const void *value, size_t size,
173                    int type, int flags)
174 {
175         struct bch_fs *c = trans->c;
176         struct btree_iter inode_iter = { NULL };
177         int ret;
178
179         ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_INTENT);
180         if (ret)
181                 return ret;
182
183         inode_u->bi_ctime = bch2_current_time(c);
184
185         ret = bch2_inode_write(trans, &inode_iter, inode_u);
186         bch2_trans_iter_exit(trans, &inode_iter);
187
188         if (ret)
189                 return ret;
190
191         if (value) {
192                 struct bkey_i_xattr *xattr;
193                 unsigned namelen = strlen(name);
194                 unsigned u64s = BKEY_U64s +
195                         xattr_val_u64s(namelen, size);
196
197                 if (u64s > U8_MAX)
198                         return -ERANGE;
199
200                 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
201                 if (IS_ERR(xattr))
202                         return PTR_ERR(xattr);
203
204                 bkey_xattr_init(&xattr->k_i);
205                 xattr->k.u64s           = u64s;
206                 xattr->v.x_type         = type;
207                 xattr->v.x_name_len     = namelen;
208                 xattr->v.x_val_len      = cpu_to_le16(size);
209                 memcpy(xattr->v.x_name, name, namelen);
210                 memcpy(xattr_val(&xattr->v), value, size);
211
212                 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
213                               inum, &xattr->k_i,
214                               (flags & XATTR_CREATE ? BCH_HASH_SET_MUST_CREATE : 0)|
215                               (flags & XATTR_REPLACE ? BCH_HASH_SET_MUST_REPLACE : 0));
216         } else {
217                 struct xattr_search_key search =
218                         X_SEARCH(type, name, strlen(name));
219
220                 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,
221                                        hash_info, inum, &search);
222         }
223
224         if (bch2_err_matches(ret, ENOENT))
225                 ret = flags & XATTR_REPLACE ? -ENODATA : 0;
226
227         return ret;
228 }
229
230 struct xattr_buf {
231         char            *buf;
232         size_t          len;
233         size_t          used;
234 };
235
236 static int __bch2_xattr_emit(const char *prefix,
237                              const char *name, size_t name_len,
238                              struct xattr_buf *buf)
239 {
240         const size_t prefix_len = strlen(prefix);
241         const size_t total_len = prefix_len + name_len + 1;
242
243         if (buf->buf) {
244                 if (buf->used + total_len > buf->len)
245                         return -ERANGE;
246
247                 memcpy(buf->buf + buf->used, prefix, prefix_len);
248                 memcpy(buf->buf + buf->used + prefix_len,
249                        name, name_len);
250                 buf->buf[buf->used + prefix_len + name_len] = '\0';
251         }
252
253         buf->used += total_len;
254         return 0;
255 }
256
257 static int bch2_xattr_emit(struct dentry *dentry,
258                             const struct bch_xattr *xattr,
259                             struct xattr_buf *buf)
260 {
261         const struct xattr_handler *handler =
262                 bch2_xattr_type_to_handler(xattr->x_type);
263
264         return handler && (!handler->list || handler->list(dentry))
265                 ? __bch2_xattr_emit(handler->prefix ?: handler->name,
266                                     xattr->x_name, xattr->x_name_len, buf)
267                 : 0;
268 }
269
270 static int bch2_xattr_list_bcachefs(struct bch_fs *c,
271                                     struct bch_inode_unpacked *inode,
272                                     struct xattr_buf *buf,
273                                     bool all)
274 {
275         const char *prefix = all ? "bcachefs_effective." : "bcachefs.";
276         unsigned id;
277         int ret = 0;
278         u64 v;
279
280         for (id = 0; id < Inode_opt_nr; id++) {
281                 v = bch2_inode_opt_get(inode, id);
282                 if (!v)
283                         continue;
284
285                 if (!all &&
286                     !(inode->bi_fields_set & (1 << id)))
287                         continue;
288
289                 ret = __bch2_xattr_emit(prefix, bch2_inode_opts[id],
290                                         strlen(bch2_inode_opts[id]), buf);
291                 if (ret)
292                         break;
293         }
294
295         return ret;
296 }
297
298 ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
299 {
300         struct bch_fs *c = dentry->d_sb->s_fs_info;
301         struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
302         struct btree_trans trans;
303         struct btree_iter iter;
304         struct bkey_s_c k;
305         struct xattr_buf buf = { .buf = buffer, .len = buffer_size };
306         u64 offset = 0, inum = inode->ei_inode.bi_inum;
307         u32 snapshot;
308         int ret;
309
310         bch2_trans_init(&trans, c, 0, 0);
311 retry:
312         bch2_trans_begin(&trans);
313         iter = (struct btree_iter) { NULL };
314
315         ret = bch2_subvolume_get_snapshot(&trans, inode->ei_subvol, &snapshot);
316         if (ret)
317                 goto err;
318
319         for_each_btree_key_upto_norestart(&trans, iter, BTREE_ID_xattrs,
320                            SPOS(inum, offset, snapshot),
321                            POS(inum, U64_MAX), 0, k, ret) {
322                 if (k.k->type != KEY_TYPE_xattr)
323                         continue;
324
325                 ret = bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf);
326                 if (ret)
327                         break;
328         }
329
330         offset = iter.pos.offset;
331         bch2_trans_iter_exit(&trans, &iter);
332 err:
333         if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
334                 goto retry;
335
336         bch2_trans_exit(&trans);
337
338         if (ret)
339                 goto out;
340
341         ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, false);
342         if (ret)
343                 goto out;
344
345         ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, true);
346         if (ret)
347                 goto out;
348
349         return buf.used;
350 out:
351         return bch2_err_class(ret);
352 }
353
354 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
355                                   struct dentry *dentry, struct inode *vinode,
356                                   const char *name, void *buffer, size_t size)
357 {
358         struct bch_inode_info *inode = to_bch_ei(vinode);
359         struct bch_fs *c = inode->v.i_sb->s_fs_info;
360         int ret = bch2_trans_do(c, NULL, NULL, 0,
361                 bch2_xattr_get_trans(&trans, inode, name, buffer, size, handler->flags));
362
363         return bch2_err_class(ret);
364 }
365
366 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
367                                   struct mnt_idmap *idmap,
368                                   struct dentry *dentry, struct inode *vinode,
369                                   const char *name, const void *value,
370                                   size_t size, int flags)
371 {
372         struct bch_inode_info *inode = to_bch_ei(vinode);
373         struct bch_fs *c = inode->v.i_sb->s_fs_info;
374         struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
375         struct bch_inode_unpacked inode_u;
376         struct btree_trans trans;
377         int ret;
378
379         bch2_trans_init(&trans, c, 0, 0);
380
381         ret = commit_do(&trans, NULL, NULL, 0,
382                         bch2_xattr_set(&trans, inode_inum(inode), &inode_u,
383                                        &hash, name, value, size,
384                                        handler->flags, flags));
385         if (!ret)
386                 bch2_inode_update_after_write(&trans, inode, &inode_u, ATTR_CTIME);
387         bch2_trans_exit(&trans);
388
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 btree_trans *trans,
498                             struct bch_inode_info *inode,
499                             struct bch_inode_unpacked *bi,
500                             void *p)
501 {
502         struct inode_opt_set *s = p;
503
504         if (s->defined)
505                 bi->bi_fields_set |= 1U << s->id;
506         else
507                 bi->bi_fields_set &= ~(1U << s->id);
508
509         bch2_inode_opt_set(bi, s->id, s->v);
510
511         return 0;
512 }
513
514 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
515                                    struct mnt_idmap *idmap,
516                                    struct dentry *dentry, struct inode *vinode,
517                                    const char *name, const void *value,
518                                    size_t size, int flags)
519 {
520         struct bch_inode_info *inode = to_bch_ei(vinode);
521         struct bch_fs *c = inode->v.i_sb->s_fs_info;
522         const struct bch_option *opt;
523         char *buf;
524         struct inode_opt_set s;
525         int opt_id, inode_opt_id, ret;
526
527         opt_id = bch2_opt_lookup(name);
528         if (opt_id < 0)
529                 return -EINVAL;
530
531         opt = bch2_opt_table + opt_id;
532
533         inode_opt_id = opt_to_inode_opt(opt_id);
534         if (inode_opt_id < 0)
535                 return -EINVAL;
536
537         s.id = inode_opt_id;
538
539         if (value) {
540                 u64 v = 0;
541
542                 buf = kmalloc(size + 1, GFP_KERNEL);
543                 if (!buf)
544                         return -ENOMEM;
545                 memcpy(buf, value, size);
546                 buf[size] = '\0';
547
548                 ret = bch2_opt_parse(c, opt, buf, &v, NULL);
549                 kfree(buf);
550
551                 if (ret < 0)
552                         return ret;
553
554                 ret = bch2_opt_check_may_set(c, opt_id, v);
555                 if (ret < 0)
556                         return ret;
557
558                 s.v = v + 1;
559                 s.defined = true;
560         } else {
561                 if (!IS_ROOT(dentry)) {
562                         struct bch_inode_info *dir =
563                                 to_bch_ei(d_inode(dentry->d_parent));
564
565                         s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id);
566                 } else {
567                         s.v = 0;
568                 }
569
570                 s.defined = false;
571         }
572
573         mutex_lock(&inode->ei_update_lock);
574         if (inode_opt_id == Inode_opt_project) {
575                 /*
576                  * inode fields accessible via the xattr interface are stored
577                  * with a +1 bias, so that 0 means unset:
578                  */
579                 ret = bch2_set_projid(c, inode, s.v ? s.v - 1 : 0);
580                 if (ret)
581                         goto err;
582         }
583
584         ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
585 err:
586         mutex_unlock(&inode->ei_update_lock);
587
588         if (value &&
589             (opt_id == Opt_background_compression ||
590              opt_id == Opt_background_target))
591                 bch2_rebalance_add_work(c, inode->v.i_blocks);
592
593         return bch2_err_class(ret);
594 }
595
596 static const struct xattr_handler bch_xattr_bcachefs_handler = {
597         .prefix = "bcachefs.",
598         .get    = bch2_xattr_bcachefs_get,
599         .set    = bch2_xattr_bcachefs_set,
600 };
601
602 static int bch2_xattr_bcachefs_get_effective(
603                                 const struct xattr_handler *handler,
604                                 struct dentry *dentry, struct inode *vinode,
605                                 const char *name, void *buffer, size_t size)
606 {
607         return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
608                                          name, buffer, size, true);
609 }
610
611 static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
612         .prefix = "bcachefs_effective.",
613         .get    = bch2_xattr_bcachefs_get_effective,
614         .set    = bch2_xattr_bcachefs_set,
615 };
616
617 #endif /* NO_BCACHEFS_FS */
618
619 const struct xattr_handler *bch2_xattr_handlers[] = {
620         &bch_xattr_user_handler,
621 #ifdef CONFIG_BCACHEFS_POSIX_ACL
622         &nop_posix_acl_access,
623         &nop_posix_acl_default,
624 #endif
625         &bch_xattr_trusted_handler,
626         &bch_xattr_security_handler,
627 #ifndef NO_BCACHEFS_FS
628         &bch_xattr_bcachefs_handler,
629         &bch_xattr_bcachefs_effective_handler,
630 #endif
631         NULL
632 };
633
634 static const struct xattr_handler *bch_xattr_handler_map[] = {
635         [KEY_TYPE_XATTR_INDEX_USER]                     = &bch_xattr_user_handler,
636         [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] =
637                 &nop_posix_acl_access,
638         [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT]        =
639                 &nop_posix_acl_default,
640         [KEY_TYPE_XATTR_INDEX_TRUSTED]          = &bch_xattr_trusted_handler,
641         [KEY_TYPE_XATTR_INDEX_SECURITY]         = &bch_xattr_security_handler,
642 };
643
644 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
645 {
646         return type < ARRAY_SIZE(bch_xattr_handler_map)
647                 ? bch_xattr_handler_map[type]
648                 : NULL;
649 }