]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/xattr.c
Update bcachefs sources to eab3b355cf bcachefs: trace transaction restarts
[bcachefs-tools-debian] / libbcachefs / xattr.c
1
2 #include "bcachefs.h"
3 #include "bkey_methods.h"
4 #include "btree_update.h"
5 #include "compress.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       = BCH_XATTR,
66         .whiteout_type  = BCH_XATTR_WHITEOUT,
67         .hash_key       = xattr_hash_key,
68         .hash_bkey      = xattr_hash_bkey,
69         .cmp_key        = xattr_cmp_key,
70         .cmp_bkey       = xattr_cmp_bkey,
71 };
72
73 const char *bch2_xattr_invalid(const struct bch_fs *c, struct bkey_s_c k)
74 {
75         const struct xattr_handler *handler;
76         struct bkey_s_c_xattr xattr;
77
78         switch (k.k->type) {
79         case BCH_XATTR:
80                 if (bkey_val_bytes(k.k) < sizeof(struct bch_xattr))
81                         return "value too small";
82
83                 xattr = bkey_s_c_to_xattr(k);
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)))
88                         return "value too small";
89
90                 if (bkey_val_u64s(k.k) >
91                         xattr_val_u64s(xattr.v->x_name_len,
92                                        le16_to_cpu(xattr.v->x_val_len) + 4))
93                         return "value too big";
94
95                 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
96                 if (!handler)
97                         return "invalid type";
98
99                 if (memchr(xattr.v->x_name, '\0', xattr.v->x_name_len))
100                         return "xattr name has invalid characters";
101
102                 return NULL;
103         case BCH_XATTR_WHITEOUT:
104                 return bkey_val_bytes(k.k) != 0
105                         ? "value size should be zero"
106                         : NULL;
107
108         default:
109                 return "invalid type";
110         }
111 }
112
113 void bch2_xattr_to_text(struct bch_fs *c, char *buf,
114                         size_t size, struct bkey_s_c k)
115 {
116         const struct xattr_handler *handler;
117         struct bkey_s_c_xattr xattr;
118         size_t n = 0;
119
120         switch (k.k->type) {
121         case BCH_XATTR:
122                 xattr = bkey_s_c_to_xattr(k);
123
124                 handler = bch2_xattr_type_to_handler(xattr.v->x_type);
125                 if (handler && handler->prefix)
126                         n += scnprintf(buf + n, size - n, "%s", handler->prefix);
127                 else if (handler)
128                         n += scnprintf(buf + n, size - n, "(type %u)",
129                                        xattr.v->x_type);
130                 else
131                         n += scnprintf(buf + n, size - n, "(unknown type %u)",
132                                        xattr.v->x_type);
133
134                 n += bch_scnmemcpy(buf + n, size - n, xattr.v->x_name,
135                                    xattr.v->x_name_len);
136                 n += scnprintf(buf + n, size - n, ":");
137                 n += bch_scnmemcpy(buf + n, size - n, xattr_val(xattr.v),
138                                    le16_to_cpu(xattr.v->x_val_len));
139                 break;
140         case BCH_XATTR_WHITEOUT:
141                 scnprintf(buf, size, "whiteout");
142                 break;
143         }
144 }
145
146 int bch2_xattr_get(struct bch_fs *c, struct bch_inode_info *inode,
147                    const char *name, void *buffer, size_t size, int type)
148 {
149         struct btree_trans trans;
150         struct btree_iter *iter;
151         struct bkey_s_c_xattr xattr;
152         int ret;
153
154         bch2_trans_init(&trans, c);
155
156         iter = bch2_hash_lookup(&trans, bch2_xattr_hash_desc,
157                                 &inode->ei_str_hash, inode->v.i_ino,
158                                 &X_SEARCH(type, name, strlen(name)),
159                                 0);
160         if (IS_ERR(iter)) {
161                 bch2_trans_exit(&trans);
162                 BUG_ON(PTR_ERR(iter) == -EINTR);
163
164                 return PTR_ERR(iter) == -ENOENT ? -ENODATA : PTR_ERR(iter);
165         }
166
167         xattr = bkey_s_c_to_xattr(bch2_btree_iter_peek_slot(iter));
168         ret = le16_to_cpu(xattr.v->x_val_len);
169         if (buffer) {
170                 if (ret > size)
171                         ret = -ERANGE;
172                 else
173                         memcpy(buffer, xattr_val(xattr.v), ret);
174         }
175
176         bch2_trans_exit(&trans);
177         return ret;
178 }
179
180 int bch2_xattr_set(struct btree_trans *trans, u64 inum,
181                    const struct bch_hash_info *hash_info,
182                    const char *name, const void *value, size_t size,
183                    int type, int flags)
184 {
185         int ret;
186
187         if (value) {
188                 struct bkey_i_xattr *xattr;
189                 unsigned namelen = strlen(name);
190                 unsigned u64s = BKEY_U64s +
191                         xattr_val_u64s(namelen, size);
192
193                 if (u64s > U8_MAX)
194                         return -ERANGE;
195
196                 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
197                 if (IS_ERR(xattr))
198                         return PTR_ERR(xattr);
199
200                 bkey_xattr_init(&xattr->k_i);
201                 xattr->k.u64s           = u64s;
202                 xattr->v.x_type         = type;
203                 xattr->v.x_name_len     = namelen;
204                 xattr->v.x_val_len      = cpu_to_le16(size);
205                 memcpy(xattr->v.x_name, name, namelen);
206                 memcpy(xattr_val(&xattr->v), value, size);
207
208                 ret = __bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
209                               inum, &xattr->k_i,
210                               (flags & XATTR_CREATE ? BCH_HASH_SET_MUST_CREATE : 0)|
211                               (flags & XATTR_REPLACE ? BCH_HASH_SET_MUST_REPLACE : 0));
212         } else {
213                 struct xattr_search_key search =
214                         X_SEARCH(type, name, strlen(name));
215
216                 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,
217                                        hash_info, inum, &search);
218         }
219
220         if (ret == -ENOENT)
221                 ret = flags & XATTR_REPLACE ? -ENODATA : 0;
222
223         return ret;
224 }
225
226 static size_t bch2_xattr_emit(struct dentry *dentry,
227                              const struct bch_xattr *xattr,
228                              char *buffer, size_t buffer_size)
229 {
230         const struct xattr_handler *handler =
231                 bch2_xattr_type_to_handler(xattr->x_type);
232
233         if (handler && (!handler->list || handler->list(dentry))) {
234                 const char *prefix = handler->prefix ?: handler->name;
235                 const size_t prefix_len = strlen(prefix);
236                 const size_t total_len = prefix_len + xattr->x_name_len + 1;
237
238                 if (buffer && total_len <= buffer_size) {
239                         memcpy(buffer, prefix, prefix_len);
240                         memcpy(buffer + prefix_len,
241                                xattr->x_name, xattr->x_name_len);
242                         buffer[prefix_len + xattr->x_name_len] = '\0';
243                 }
244
245                 return total_len;
246         } else {
247                 return 0;
248         }
249 }
250
251 ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
252 {
253         struct bch_fs *c = dentry->d_sb->s_fs_info;
254         struct btree_iter iter;
255         struct bkey_s_c k;
256         const struct bch_xattr *xattr;
257         u64 inum = dentry->d_inode->i_ino;
258         ssize_t ret = 0;
259         size_t len;
260
261         for_each_btree_key(&iter, c, BTREE_ID_XATTRS, POS(inum, 0), 0, k) {
262                 BUG_ON(k.k->p.inode < inum);
263
264                 if (k.k->p.inode > inum)
265                         break;
266
267                 if (k.k->type != BCH_XATTR)
268                         continue;
269
270                 xattr = bkey_s_c_to_xattr(k).v;
271
272                 len = bch2_xattr_emit(dentry, xattr, buffer, buffer_size);
273                 if (buffer) {
274                         if (len > buffer_size) {
275                                 bch2_btree_iter_unlock(&iter);
276                                 return -ERANGE;
277                         }
278
279                         buffer += len;
280                         buffer_size -= len;
281                 }
282
283                 ret += len;
284
285         }
286         bch2_btree_iter_unlock(&iter);
287
288         return ret;
289 }
290
291 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
292                                   struct dentry *dentry, struct inode *vinode,
293                                   const char *name, void *buffer, size_t size)
294 {
295         struct bch_inode_info *inode = to_bch_ei(vinode);
296         struct bch_fs *c = inode->v.i_sb->s_fs_info;
297
298         return bch2_xattr_get(c, inode, name, buffer, size, handler->flags);
299 }
300
301 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
302                                   struct dentry *dentry, struct inode *vinode,
303                                   const char *name, const void *value,
304                                   size_t size, int flags)
305 {
306         struct bch_inode_info *inode = to_bch_ei(vinode);
307         struct bch_fs *c = inode->v.i_sb->s_fs_info;
308
309         return bch2_trans_do(c, &inode->ei_journal_seq, BTREE_INSERT_ATOMIC,
310                         bch2_xattr_set(&trans, inode->v.i_ino,
311                                        &inode->ei_str_hash,
312                                        name, value, size,
313                                        handler->flags, flags));
314 }
315
316 static const struct xattr_handler bch_xattr_user_handler = {
317         .prefix = XATTR_USER_PREFIX,
318         .get    = bch2_xattr_get_handler,
319         .set    = bch2_xattr_set_handler,
320         .flags  = BCH_XATTR_INDEX_USER,
321 };
322
323 static bool bch2_xattr_trusted_list(struct dentry *dentry)
324 {
325         return capable(CAP_SYS_ADMIN);
326 }
327
328 static const struct xattr_handler bch_xattr_trusted_handler = {
329         .prefix = XATTR_TRUSTED_PREFIX,
330         .list   = bch2_xattr_trusted_list,
331         .get    = bch2_xattr_get_handler,
332         .set    = bch2_xattr_set_handler,
333         .flags  = BCH_XATTR_INDEX_TRUSTED,
334 };
335
336 static const struct xattr_handler bch_xattr_security_handler = {
337         .prefix = XATTR_SECURITY_PREFIX,
338         .get    = bch2_xattr_get_handler,
339         .set    = bch2_xattr_set_handler,
340         .flags  = BCH_XATTR_INDEX_SECURITY,
341 };
342
343 #ifndef NO_BCACHEFS_FS
344
345 static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
346                                    struct dentry *dentry, struct inode *vinode,
347                                    const char *name, void *buffer, size_t size)
348 {
349         struct bch_inode_info *inode = to_bch_ei(vinode);
350         struct bch_fs *c = inode->v.i_sb->s_fs_info;
351         struct bch_opts opts =
352                 bch2_inode_opts_to_opts(bch2_inode_opts_get(&inode->ei_inode));
353         const struct bch_option *opt;
354         int ret, id;
355         u64 v;
356
357         id = bch2_opt_lookup(name);
358         if (id < 0 || !bch2_opt_is_inode_opt(id))
359                 return -EINVAL;
360
361         opt = bch2_opt_table + id;
362
363         if (!bch2_opt_defined_by_id(&opts, id))
364                 return -ENODATA;
365
366         v = bch2_opt_get_by_id(&opts, id);
367
368         ret = bch2_opt_to_text(c, buffer, size, opt, v, 0);
369
370         return ret < size || !buffer ? ret : -ERANGE;
371 }
372
373 struct inode_opt_set {
374         int                     id;
375         u64                     v;
376         bool                    defined;
377 };
378
379 static int inode_opt_set_fn(struct bch_inode_info *inode,
380                             struct bch_inode_unpacked *bi,
381                             void *p)
382 {
383         struct inode_opt_set *s = p;
384
385         if (s->defined)
386                 bch2_inode_opt_set(bi, s->id, s->v);
387         else
388                 bch2_inode_opt_clear(bi, s->id);
389         return 0;
390 }
391
392 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
393                                    struct dentry *dentry, struct inode *vinode,
394                                    const char *name, const void *value,
395                                    size_t size, int flags)
396 {
397         struct bch_inode_info *inode = to_bch_ei(vinode);
398         struct bch_fs *c = inode->v.i_sb->s_fs_info;
399         const struct bch_option *opt;
400         char *buf;
401         struct inode_opt_set s;
402         int ret;
403
404         s.id = bch2_opt_lookup(name);
405         if (s.id < 0 || !bch2_opt_is_inode_opt(s.id))
406                 return -EINVAL;
407
408         opt = bch2_opt_table + s.id;
409
410         if (value) {
411                 buf = kmalloc(size + 1, GFP_KERNEL);
412                 if (!buf)
413                         return -ENOMEM;
414                 memcpy(buf, value, size);
415                 buf[size] = '\0';
416
417                 ret = bch2_opt_parse(c, opt, buf, &s.v);
418                 kfree(buf);
419
420                 if (ret < 0)
421                         return ret;
422
423                 if (s.id == Opt_compression ||
424                     s.id == Opt_background_compression) {
425                         ret = bch2_check_set_has_compressed_data(c, s.v);
426                         if (ret)
427                                 return ret;
428                 }
429
430                 s.defined = true;
431         } else {
432                 s.defined = false;
433         }
434
435         mutex_lock(&inode->ei_update_lock);
436         ret = __bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
437         mutex_unlock(&inode->ei_update_lock);
438
439         if (value &&
440             (s.id == Opt_background_compression ||
441              s.id == Opt_background_target))
442                 bch2_rebalance_add_work(c, inode->v.i_blocks);
443
444         return ret;
445 }
446
447 static const struct xattr_handler bch_xattr_bcachefs_handler = {
448         .prefix = "bcachefs.",
449         .get    = bch2_xattr_bcachefs_get,
450         .set    = bch2_xattr_bcachefs_set,
451 };
452
453 #endif /* NO_BCACHEFS_FS */
454
455 const struct xattr_handler *bch2_xattr_handlers[] = {
456         &bch_xattr_user_handler,
457         &posix_acl_access_xattr_handler,
458         &posix_acl_default_xattr_handler,
459         &bch_xattr_trusted_handler,
460         &bch_xattr_security_handler,
461 #ifndef NO_BCACHEFS_FS
462         &bch_xattr_bcachefs_handler,
463 #endif
464         NULL
465 };
466
467 static const struct xattr_handler *bch_xattr_handler_map[] = {
468         [BCH_XATTR_INDEX_USER]                  = &bch_xattr_user_handler,
469         [BCH_XATTR_INDEX_POSIX_ACL_ACCESS]      =
470                 &posix_acl_access_xattr_handler,
471         [BCH_XATTR_INDEX_POSIX_ACL_DEFAULT]     =
472                 &posix_acl_default_xattr_handler,
473         [BCH_XATTR_INDEX_TRUSTED]               = &bch_xattr_trusted_handler,
474         [BCH_XATTR_INDEX_SECURITY]              = &bch_xattr_security_handler,
475 };
476
477 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
478 {
479         return type < ARRAY_SIZE(bch_xattr_handler_map)
480                 ? bch_xattr_handler_map[type]
481                 : NULL;
482 }