]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/xattr.c
Update bcachefs sources to 6f603b8d79 bcachefs: some improvements to startup messages...
[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 struct xattr_buf {
201         char            *buf;
202         size_t          len;
203         size_t          used;
204 };
205
206 static int __bch2_xattr_emit(const char *prefix,
207                              const char *name, size_t name_len,
208                              struct xattr_buf *buf)
209 {
210         const size_t prefix_len = strlen(prefix);
211         const size_t total_len = prefix_len + name_len + 1;
212
213         if (buf->buf) {
214                 if (buf->used + total_len > buf->len)
215                         return -ERANGE;
216
217                 memcpy(buf->buf + buf->used, prefix, prefix_len);
218                 memcpy(buf->buf + buf->used + prefix_len,
219                        name, name_len);
220                 buf->buf[buf->used + prefix_len + name_len] = '\0';
221         }
222
223         buf->used += total_len;
224         return 0;
225 }
226
227 static int bch2_xattr_emit(struct dentry *dentry,
228                             const struct bch_xattr *xattr,
229                             struct xattr_buf *buf)
230 {
231         const struct xattr_handler *handler =
232                 bch2_xattr_type_to_handler(xattr->x_type);
233
234         return handler && (!handler->list || handler->list(dentry))
235                 ? __bch2_xattr_emit(handler->prefix ?: handler->name,
236                                     xattr->x_name, xattr->x_name_len, buf)
237                 : 0;
238 }
239
240 static int bch2_xattr_list_bcachefs(struct bch_fs *c,
241                                     struct bch_inode_info *inode,
242                                     struct xattr_buf *buf,
243                                     bool all)
244 {
245         const char *prefix = all ? "bcachefs_effective." : "bcachefs.";
246         unsigned id;
247         int ret = 0;
248         u64 v;
249
250         for (id = 0; id < Inode_opt_nr; id++) {
251                 v = bch2_inode_opt_get(&inode->ei_inode, id);
252                 if (!v)
253                         continue;
254
255                 if (!all &&
256                     !(inode->ei_inode.bi_fields_set & (1 << id)))
257                         continue;
258
259                 ret = __bch2_xattr_emit(prefix, bch2_inode_opts[id],
260                                         strlen(bch2_inode_opts[id]), buf);
261                 if (ret)
262                         break;
263         }
264
265         return ret;
266 }
267
268 ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
269 {
270         struct bch_fs *c = dentry->d_sb->s_fs_info;
271         struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
272         struct btree_trans trans;
273         struct btree_iter *iter;
274         struct bkey_s_c k;
275         struct xattr_buf buf = { .buf = buffer, .len = buffer_size };
276         u64 inum = dentry->d_inode->i_ino;
277         int ret;
278
279         bch2_trans_init(&trans, c);
280
281         for_each_btree_key(&trans, iter, BTREE_ID_XATTRS,
282                            POS(inum, 0), 0, k, ret) {
283                 BUG_ON(k.k->p.inode < inum);
284
285                 if (k.k->p.inode > inum)
286                         break;
287
288                 if (k.k->type != KEY_TYPE_xattr)
289                         continue;
290
291                 ret = bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf);
292                 if (ret)
293                         break;
294         }
295         ret = bch2_trans_exit(&trans) ?: ret;
296
297         if (ret)
298                 return ret;
299
300         ret = bch2_xattr_list_bcachefs(c, inode, &buf, false);
301         if (ret)
302                 return ret;
303
304         ret = bch2_xattr_list_bcachefs(c, inode, &buf, true);
305         if (ret)
306                 return ret;
307
308         return buf.used;
309 }
310
311 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
312                                   struct dentry *dentry, struct inode *vinode,
313                                   const char *name, void *buffer, size_t size)
314 {
315         struct bch_inode_info *inode = to_bch_ei(vinode);
316         struct bch_fs *c = inode->v.i_sb->s_fs_info;
317
318         return bch2_xattr_get(c, inode, name, buffer, size, handler->flags);
319 }
320
321 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
322                                   struct dentry *dentry, struct inode *vinode,
323                                   const char *name, const void *value,
324                                   size_t size, int flags)
325 {
326         struct bch_inode_info *inode = to_bch_ei(vinode);
327         struct bch_fs *c = inode->v.i_sb->s_fs_info;
328
329         return bch2_trans_do(c, &inode->ei_journal_seq, BTREE_INSERT_ATOMIC,
330                         bch2_xattr_set(&trans, inode->v.i_ino,
331                                        &inode->ei_str_hash,
332                                        name, value, size,
333                                        handler->flags, flags));
334 }
335
336 static const struct xattr_handler bch_xattr_user_handler = {
337         .prefix = XATTR_USER_PREFIX,
338         .get    = bch2_xattr_get_handler,
339         .set    = bch2_xattr_set_handler,
340         .flags  = KEY_TYPE_XATTR_INDEX_USER,
341 };
342
343 static bool bch2_xattr_trusted_list(struct dentry *dentry)
344 {
345         return capable(CAP_SYS_ADMIN);
346 }
347
348 static const struct xattr_handler bch_xattr_trusted_handler = {
349         .prefix = XATTR_TRUSTED_PREFIX,
350         .list   = bch2_xattr_trusted_list,
351         .get    = bch2_xattr_get_handler,
352         .set    = bch2_xattr_set_handler,
353         .flags  = KEY_TYPE_XATTR_INDEX_TRUSTED,
354 };
355
356 static const struct xattr_handler bch_xattr_security_handler = {
357         .prefix = XATTR_SECURITY_PREFIX,
358         .get    = bch2_xattr_get_handler,
359         .set    = bch2_xattr_set_handler,
360         .flags  = KEY_TYPE_XATTR_INDEX_SECURITY,
361 };
362
363 #ifndef NO_BCACHEFS_FS
364
365 static int opt_to_inode_opt(int id)
366 {
367         switch (id) {
368 #define x(name, ...)                            \
369         case Opt_##name: return Inode_opt_##name;
370         BCH_INODE_OPTS()
371 #undef  x
372         default:
373                 return -1;
374         }
375 }
376
377 static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
378                                 struct dentry *dentry, struct inode *vinode,
379                                 const char *name, void *buffer, size_t size,
380                                 bool all)
381 {
382         struct bch_inode_info *inode = to_bch_ei(vinode);
383         struct bch_fs *c = inode->v.i_sb->s_fs_info;
384         struct bch_opts opts =
385                 bch2_inode_opts_to_opts(bch2_inode_opts_get(&inode->ei_inode));
386         const struct bch_option *opt;
387         int id, inode_opt_id;
388         u64 v;
389
390         id = bch2_opt_lookup(name);
391         if (id < 0 || !bch2_opt_is_inode_opt(id))
392                 return -EINVAL;
393
394         inode_opt_id = opt_to_inode_opt(id);
395         if (inode_opt_id < 0)
396                 return -EINVAL;
397
398         opt = bch2_opt_table + id;
399
400         if (!bch2_opt_defined_by_id(&opts, id))
401                 return -ENODATA;
402
403         if (!all &&
404             !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))
405                 return -ENODATA;
406
407         v = bch2_opt_get_by_id(&opts, id);
408
409         if (!buffer) {
410                 char buf[512];
411                 struct printbuf out = PBUF(buf);
412
413                 bch2_opt_to_text(&out, c, opt, v, 0);
414
415                 return out.pos - buf;
416         } else {
417                 struct printbuf out = _PBUF(buffer, size);
418
419                 bch2_opt_to_text(&out, c, opt, v, 0);
420
421                 return printbuf_remaining(&out)
422                         ? (void *) out.pos - buffer
423                         : -ERANGE;
424         }
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                 ret = bch2_set_projid(c, inode, s.v);
518                 if (ret)
519                         goto err;
520         }
521
522         ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
523 err:
524         mutex_unlock(&inode->ei_update_lock);
525
526         if (value &&
527             (opt_id == Opt_background_compression ||
528              opt_id == Opt_background_target))
529                 bch2_rebalance_add_work(c, inode->v.i_blocks);
530
531         return ret;
532 }
533
534 static const struct xattr_handler bch_xattr_bcachefs_handler = {
535         .prefix = "bcachefs.",
536         .get    = bch2_xattr_bcachefs_get,
537         .set    = bch2_xattr_bcachefs_set,
538 };
539
540 static int bch2_xattr_bcachefs_get_effective(
541                                 const struct xattr_handler *handler,
542                                 struct dentry *dentry, struct inode *vinode,
543                                 const char *name, void *buffer, size_t size)
544 {
545         return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
546                                          name, buffer, size, true);
547 }
548
549 static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
550         .prefix = "bcachefs_effective.",
551         .get    = bch2_xattr_bcachefs_get_effective,
552         .set    = bch2_xattr_bcachefs_set,
553 };
554
555 #endif /* NO_BCACHEFS_FS */
556
557 const struct xattr_handler *bch2_xattr_handlers[] = {
558         &bch_xattr_user_handler,
559         &posix_acl_access_xattr_handler,
560         &posix_acl_default_xattr_handler,
561         &bch_xattr_trusted_handler,
562         &bch_xattr_security_handler,
563 #ifndef NO_BCACHEFS_FS
564         &bch_xattr_bcachefs_handler,
565         &bch_xattr_bcachefs_effective_handler,
566 #endif
567         NULL
568 };
569
570 static const struct xattr_handler *bch_xattr_handler_map[] = {
571         [KEY_TYPE_XATTR_INDEX_USER]                     = &bch_xattr_user_handler,
572         [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] =
573                 &posix_acl_access_xattr_handler,
574         [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT]        =
575                 &posix_acl_default_xattr_handler,
576         [KEY_TYPE_XATTR_INDEX_TRUSTED]          = &bch_xattr_trusted_handler,
577         [KEY_TYPE_XATTR_INDEX_SECURITY]         = &bch_xattr_security_handler,
578 };
579
580 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
581 {
582         return type < ARRAY_SIZE(bch_xattr_handler_map)
583                 ? bch_xattr_handler_map[type]
584                 : NULL;
585 }