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