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