]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/xattr.c
Update bcachefs sources to 2724e115d2 bcachefs: Lots of option handling improvements
[bcachefs-tools-debian] / libbcachefs / xattr.c
1
2 #include "bcachefs.h"
3 #include "bkey_methods.h"
4 #include "btree_update.h"
5 #include "extents.h"
6 #include "fs.h"
7 #include "rebalance.h"
8 #include "str_hash.h"
9 #include "xattr.h"
10
11 #include <linux/dcache.h>
12 #include <linux/posix_acl_xattr.h>
13 #include <linux/xattr.h>
14
15 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned);
16
17 static u64 bch2_xattr_hash(const struct bch_hash_info *info,
18                           const struct xattr_search_key *key)
19 {
20         struct bch_str_hash_ctx ctx;
21
22         bch2_str_hash_init(&ctx, info);
23         bch2_str_hash_update(&ctx, info, &key->type, sizeof(key->type));
24         bch2_str_hash_update(&ctx, info, key->name.name, key->name.len);
25
26         return bch2_str_hash_end(&ctx, info);
27 }
28
29 static u64 xattr_hash_key(const struct bch_hash_info *info, const void *key)
30 {
31         return bch2_xattr_hash(info, key);
32 }
33
34 static u64 xattr_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
35 {
36         struct bkey_s_c_xattr x = bkey_s_c_to_xattr(k);
37
38         return bch2_xattr_hash(info,
39                  &X_SEARCH(x.v->x_type, x.v->x_name, x.v->x_name_len));
40 }
41
42 static bool xattr_cmp_key(struct bkey_s_c _l, const void *_r)
43 {
44         struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
45         const struct xattr_search_key *r = _r;
46
47         return l.v->x_type != r->type ||
48                 l.v->x_name_len != r->name.len ||
49                 memcmp(l.v->x_name, r->name.name, r->name.len);
50 }
51
52 static bool xattr_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
53 {
54         struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
55         struct bkey_s_c_xattr r = bkey_s_c_to_xattr(_r);
56
57         return l.v->x_type != r.v->x_type ||
58                 l.v->x_name_len != r.v->x_name_len ||
59                 memcmp(l.v->x_name, r.v->x_name, r.v->x_name_len);
60 }
61
62 const struct bch_hash_desc bch2_xattr_hash_desc = {
63         .btree_id       = BTREE_ID_XATTRS,
64         .key_type       = KEY_TYPE_xattr,
65         .hash_key       = xattr_hash_key,
66         .hash_bkey      = xattr_hash_bkey,
67         .cmp_key        = xattr_cmp_key,
68         .cmp_bkey       = xattr_cmp_bkey,
69 };
70
71 const char *bch2_xattr_invalid(const struct bch_fs *c, struct bkey_s_c k)
72 {
73         const struct xattr_handler *handler;
74         struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
75
76         if (bkey_val_bytes(k.k) < sizeof(struct bch_xattr))
77                 return "value too small";
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                 return "value too small";
83
84         if (bkey_val_u64s(k.k) >
85             xattr_val_u64s(xattr.v->x_name_len,
86                            le16_to_cpu(xattr.v->x_val_len) + 4))
87                 return "value too big";
88
89         handler = bch2_xattr_type_to_handler(xattr.v->x_type);
90         if (!handler)
91                 return "invalid type";
92
93         if (memchr(xattr.v->x_name, '\0', xattr.v->x_name_len))
94                 return "xattr name has invalid characters";
95
96         return NULL;
97 }
98
99 void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
100                         struct bkey_s_c k)
101 {
102         const struct xattr_handler *handler;
103         struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
104
105         handler = bch2_xattr_type_to_handler(xattr.v->x_type);
106         if (handler && handler->prefix)
107                 pr_buf(out, "%s", handler->prefix);
108         else if (handler)
109                 pr_buf(out, "(type %u)", xattr.v->x_type);
110         else
111                 pr_buf(out, "(unknown type %u)", xattr.v->x_type);
112
113         bch_scnmemcpy(out, xattr.v->x_name,
114                       xattr.v->x_name_len);
115         pr_buf(out, ":");
116         bch_scnmemcpy(out, xattr_val(xattr.v),
117                       le16_to_cpu(xattr.v->x_val_len));
118 }
119
120 int bch2_xattr_get(struct bch_fs *c, struct bch_inode_info *inode,
121                    const char *name, void *buffer, size_t size, int type)
122 {
123         struct btree_trans trans;
124         struct btree_iter *iter;
125         struct bkey_s_c_xattr xattr;
126         int ret;
127
128         bch2_trans_init(&trans, c);
129
130         iter = bch2_hash_lookup(&trans, bch2_xattr_hash_desc,
131                                 &inode->ei_str_hash, inode->v.i_ino,
132                                 &X_SEARCH(type, name, strlen(name)),
133                                 0);
134         if (IS_ERR(iter)) {
135                 bch2_trans_exit(&trans);
136                 BUG_ON(PTR_ERR(iter) == -EINTR);
137
138                 return PTR_ERR(iter) == -ENOENT ? -ENODATA : PTR_ERR(iter);
139         }
140
141         xattr = bkey_s_c_to_xattr(bch2_btree_iter_peek_slot(iter));
142         ret = le16_to_cpu(xattr.v->x_val_len);
143         if (buffer) {
144                 if (ret > size)
145                         ret = -ERANGE;
146                 else
147                         memcpy(buffer, xattr_val(xattr.v), ret);
148         }
149
150         bch2_trans_exit(&trans);
151         return ret;
152 }
153
154 int bch2_xattr_set(struct btree_trans *trans, u64 inum,
155                    const struct bch_hash_info *hash_info,
156                    const char *name, const void *value, size_t size,
157                    int type, int flags)
158 {
159         int ret;
160
161         if (value) {
162                 struct bkey_i_xattr *xattr;
163                 unsigned namelen = strlen(name);
164                 unsigned u64s = BKEY_U64s +
165                         xattr_val_u64s(namelen, size);
166
167                 if (u64s > U8_MAX)
168                         return -ERANGE;
169
170                 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
171                 if (IS_ERR(xattr))
172                         return PTR_ERR(xattr);
173
174                 bkey_xattr_init(&xattr->k_i);
175                 xattr->k.u64s           = u64s;
176                 xattr->v.x_type         = type;
177                 xattr->v.x_name_len     = namelen;
178                 xattr->v.x_val_len      = cpu_to_le16(size);
179                 memcpy(xattr->v.x_name, name, namelen);
180                 memcpy(xattr_val(&xattr->v), value, size);
181
182                 ret = __bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
183                               inum, &xattr->k_i,
184                               (flags & XATTR_CREATE ? BCH_HASH_SET_MUST_CREATE : 0)|
185                               (flags & XATTR_REPLACE ? BCH_HASH_SET_MUST_REPLACE : 0));
186         } else {
187                 struct xattr_search_key search =
188                         X_SEARCH(type, name, strlen(name));
189
190                 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,
191                                        hash_info, inum, &search);
192         }
193
194         if (ret == -ENOENT)
195                 ret = flags & XATTR_REPLACE ? -ENODATA : 0;
196
197         return ret;
198 }
199
200 static void __bch2_xattr_emit(const char *prefix,
201                               const char *name, size_t name_len,
202                               char **buffer, size_t *buffer_size,
203                               ssize_t *ret)
204 {
205         const size_t prefix_len = strlen(prefix);
206         const size_t total_len = prefix_len + name_len + 1;
207
208         if (*buffer) {
209                 if (total_len > *buffer_size) {
210                         *ret = -ERANGE;
211                         return;
212                 }
213
214                 memcpy(*buffer, prefix, prefix_len);
215                 memcpy(*buffer + prefix_len,
216                        name, name_len);
217                 (*buffer)[prefix_len + name_len] = '\0';
218
219                 *buffer         += total_len;
220                 *buffer_size    -= total_len;
221         }
222
223         *ret += total_len;
224 }
225
226 static void bch2_xattr_emit(struct dentry *dentry,
227                             const struct bch_xattr *xattr,
228                             char **buffer, size_t *buffer_size,
229                             ssize_t *ret)
230 {
231         const struct xattr_handler *handler =
232                 bch2_xattr_type_to_handler(xattr->x_type);
233
234         if (handler && (!handler->list || handler->list(dentry)))
235                 __bch2_xattr_emit(handler->prefix ?: handler->name,
236                                   xattr->x_name, xattr->x_name_len,
237                                   buffer, buffer_size, ret);
238 }
239
240 static void bch2_xattr_list_bcachefs(struct bch_fs *c,
241                                      struct bch_inode_info *inode,
242                                      char **buffer,
243                                      size_t *buffer_size,
244                                      ssize_t *ret,
245                                      bool all)
246 {
247         const char *prefix = all ? "bcachefs_effective." : "bcachefs.";
248         unsigned id;
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                 __bch2_xattr_emit(prefix,
261                                   bch2_inode_opts[id],
262                                   strlen(bch2_inode_opts[id]),
263                                   buffer, buffer_size, ret);
264                 if (*ret < 0)
265                         break;
266         }
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_iter iter;
274         struct bkey_s_c k;
275         u64 inum = dentry->d_inode->i_ino;
276         ssize_t ret = 0;
277
278         for_each_btree_key(&iter, c, BTREE_ID_XATTRS, POS(inum, 0), 0, k) {
279                 BUG_ON(k.k->p.inode < inum);
280
281                 if (k.k->p.inode > inum)
282                         break;
283
284                 if (k.k->type != KEY_TYPE_xattr)
285                         continue;
286
287                 bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v,
288                                 &buffer, &buffer_size, &ret);
289                 if (ret < 0)
290                         break;
291         }
292         bch2_btree_iter_unlock(&iter);
293
294         if (ret < 0)
295                 return ret;
296
297         bch2_xattr_list_bcachefs(c, inode, &buffer,
298                                  &buffer_size, &ret, false);
299         if (ret < 0)
300                 return ret;
301
302         bch2_xattr_list_bcachefs(c, inode, &buffer,
303                                  &buffer_size, &ret, true);
304         if (ret < 0)
305                 return ret;
306
307         return ret;
308 }
309
310 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
311                                   struct dentry *dentry, struct inode *vinode,
312                                   const char *name, void *buffer, size_t size)
313 {
314         struct bch_inode_info *inode = to_bch_ei(vinode);
315         struct bch_fs *c = inode->v.i_sb->s_fs_info;
316
317         return bch2_xattr_get(c, inode, name, buffer, size, handler->flags);
318 }
319
320 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
321                                   struct dentry *dentry, struct inode *vinode,
322                                   const char *name, const void *value,
323                                   size_t size, int flags)
324 {
325         struct bch_inode_info *inode = to_bch_ei(vinode);
326         struct bch_fs *c = inode->v.i_sb->s_fs_info;
327
328         return bch2_trans_do(c, &inode->ei_journal_seq, BTREE_INSERT_ATOMIC,
329                         bch2_xattr_set(&trans, inode->v.i_ino,
330                                        &inode->ei_str_hash,
331                                        name, value, size,
332                                        handler->flags, flags));
333 }
334
335 static const struct xattr_handler bch_xattr_user_handler = {
336         .prefix = XATTR_USER_PREFIX,
337         .get    = bch2_xattr_get_handler,
338         .set    = bch2_xattr_set_handler,
339         .flags  = KEY_TYPE_XATTR_INDEX_USER,
340 };
341
342 static bool bch2_xattr_trusted_list(struct dentry *dentry)
343 {
344         return capable(CAP_SYS_ADMIN);
345 }
346
347 static const struct xattr_handler bch_xattr_trusted_handler = {
348         .prefix = XATTR_TRUSTED_PREFIX,
349         .list   = bch2_xattr_trusted_list,
350         .get    = bch2_xattr_get_handler,
351         .set    = bch2_xattr_set_handler,
352         .flags  = KEY_TYPE_XATTR_INDEX_TRUSTED,
353 };
354
355 static const struct xattr_handler bch_xattr_security_handler = {
356         .prefix = XATTR_SECURITY_PREFIX,
357         .get    = bch2_xattr_get_handler,
358         .set    = bch2_xattr_set_handler,
359         .flags  = KEY_TYPE_XATTR_INDEX_SECURITY,
360 };
361
362 #ifndef NO_BCACHEFS_FS
363
364 static int opt_to_inode_opt(int id)
365 {
366         switch (id) {
367 #define x(name, ...)                            \
368         case Opt_##name: return Inode_opt_##name;
369         BCH_INODE_OPTS()
370 #undef  x
371         default:
372                 return -1;
373         }
374 }
375
376 static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
377                                 struct dentry *dentry, struct inode *vinode,
378                                 const char *name, void *buffer, size_t size,
379                                 bool all)
380 {
381         struct bch_inode_info *inode = to_bch_ei(vinode);
382         struct bch_fs *c = inode->v.i_sb->s_fs_info;
383         struct bch_opts opts =
384                 bch2_inode_opts_to_opts(bch2_inode_opts_get(&inode->ei_inode));
385         const struct bch_option *opt;
386         int id, inode_opt_id;
387         u64 v;
388
389         id = bch2_opt_lookup(name);
390         if (id < 0 || !bch2_opt_is_inode_opt(id))
391                 return -EINVAL;
392
393         inode_opt_id = opt_to_inode_opt(id);
394         if (inode_opt_id < 0)
395                 return -EINVAL;
396
397         opt = bch2_opt_table + id;
398
399         if (!bch2_opt_defined_by_id(&opts, id))
400                 return -ENODATA;
401
402         if (!all &&
403             !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))
404                 return -ENODATA;
405
406         v = bch2_opt_get_by_id(&opts, id);
407
408         if (!buffer) {
409                 char buf[512];
410                 struct printbuf out = PBUF(buf);
411
412                 bch2_opt_to_text(&out, c, opt, v, 0);
413
414                 return out.pos - buf;
415         } else {
416                 struct printbuf out = _PBUF(buffer, size);
417
418                 bch2_opt_to_text(&out, c, opt, v, 0);
419
420                 return printbuf_remaining(&out)
421                         ? (void *) out.pos - buffer
422                         : -ERANGE;
423         }
424 }
425
426 static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
427                                    struct dentry *dentry, struct inode *vinode,
428                                    const char *name, void *buffer, size_t size)
429 {
430         return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
431                                          name, buffer, size, false);
432 }
433
434 struct inode_opt_set {
435         int                     id;
436         u64                     v;
437         bool                    defined;
438 };
439
440 static int inode_opt_set_fn(struct bch_inode_info *inode,
441                             struct bch_inode_unpacked *bi,
442                             void *p)
443 {
444         struct inode_opt_set *s = p;
445
446         if (s->defined)
447                 bi->bi_fields_set |= 1U << s->id;
448         else
449                 bi->bi_fields_set &= ~(1U << s->id);
450
451         bch2_inode_opt_set(bi, s->id, s->v);
452
453         return 0;
454 }
455
456 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
457                                    struct dentry *dentry, struct inode *vinode,
458                                    const char *name, const void *value,
459                                    size_t size, int flags)
460 {
461         struct bch_inode_info *inode = to_bch_ei(vinode);
462         struct bch_fs *c = inode->v.i_sb->s_fs_info;
463         const struct bch_option *opt;
464         char *buf;
465         struct inode_opt_set s;
466         int opt_id, inode_opt_id, ret;
467
468         opt_id = bch2_opt_lookup(name);
469         if (opt_id < 0)
470                 return -EINVAL;
471
472         opt = bch2_opt_table + opt_id;
473
474         inode_opt_id = opt_to_inode_opt(opt_id);
475         if (inode_opt_id < 0)
476                 return -EINVAL;
477
478         s.id = inode_opt_id;
479
480         if (value) {
481                 u64 v = 0;
482
483                 buf = kmalloc(size + 1, GFP_KERNEL);
484                 if (!buf)
485                         return -ENOMEM;
486                 memcpy(buf, value, size);
487                 buf[size] = '\0';
488
489                 ret = bch2_opt_parse(c, opt, buf, &v);
490                 kfree(buf);
491
492                 if (ret < 0)
493                         return ret;
494
495                 ret = bch2_opt_check_may_set(c, opt_id, v);
496                 if (ret < 0)
497                         return ret;
498
499                 s.v = v + 1;
500                 s.defined = true;
501         } else {
502                 if (!IS_ROOT(dentry)) {
503                         struct bch_inode_info *dir =
504                                 to_bch_ei(d_inode(dentry->d_parent));
505
506                         s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id);
507                 } else {
508                         s.v = 0;
509                 }
510
511                 s.defined = false;
512         }
513
514         mutex_lock(&inode->ei_update_lock);
515         if (inode_opt_id == Inode_opt_project) {
516                 ret = bch2_set_projid(c, inode, s.v);
517                 if (ret)
518                         goto err;
519         }
520
521         ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
522 err:
523         mutex_unlock(&inode->ei_update_lock);
524
525         if (value &&
526             (opt_id == Opt_background_compression ||
527              opt_id == Opt_background_target))
528                 bch2_rebalance_add_work(c, inode->v.i_blocks);
529
530         return ret;
531 }
532
533 static const struct xattr_handler bch_xattr_bcachefs_handler = {
534         .prefix = "bcachefs.",
535         .get    = bch2_xattr_bcachefs_get,
536         .set    = bch2_xattr_bcachefs_set,
537 };
538
539 static int bch2_xattr_bcachefs_get_effective(
540                                 const struct xattr_handler *handler,
541                                 struct dentry *dentry, struct inode *vinode,
542                                 const char *name, void *buffer, size_t size)
543 {
544         return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
545                                          name, buffer, size, true);
546 }
547
548 static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
549         .prefix = "bcachefs_effective.",
550         .get    = bch2_xattr_bcachefs_get_effective,
551         .set    = bch2_xattr_bcachefs_set,
552 };
553
554 #endif /* NO_BCACHEFS_FS */
555
556 const struct xattr_handler *bch2_xattr_handlers[] = {
557         &bch_xattr_user_handler,
558         &posix_acl_access_xattr_handler,
559         &posix_acl_default_xattr_handler,
560         &bch_xattr_trusted_handler,
561         &bch_xattr_security_handler,
562 #ifndef NO_BCACHEFS_FS
563         &bch_xattr_bcachefs_handler,
564         &bch_xattr_bcachefs_effective_handler,
565 #endif
566         NULL
567 };
568
569 static const struct xattr_handler *bch_xattr_handler_map[] = {
570         [KEY_TYPE_XATTR_INDEX_USER]                     = &bch_xattr_user_handler,
571         [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] =
572                 &posix_acl_access_xattr_handler,
573         [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT]        =
574                 &posix_acl_default_xattr_handler,
575         [KEY_TYPE_XATTR_INDEX_TRUSTED]          = &bch_xattr_trusted_handler,
576         [KEY_TYPE_XATTR_INDEX_SECURITY]         = &bch_xattr_security_handler,
577 };
578
579 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
580 {
581         return type < ARRAY_SIZE(bch_xattr_handler_map)
582                 ? bch_xattr_handler_map[type]
583                 : NULL;
584 }