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