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