]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/inode.c
Update bcachefs sources to 9017d85854 bcachefs: btree_ptr_v2
[bcachefs-tools-debian] / libbcachefs / inode.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 "error.h"
7 #include "extents.h"
8 #include "inode.h"
9 #include "str_hash.h"
10
11 #include <linux/random.h>
12
13 #include <asm/unaligned.h>
14
15 const char * const bch2_inode_opts[] = {
16 #define x(name, ...)    #name,
17         BCH_INODE_OPTS()
18 #undef  x
19         NULL,
20 };
21
22 static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
23 static const u8 bits_table[8] = {
24         1  * 8 - 1,
25         2  * 8 - 2,
26         3  * 8 - 3,
27         4  * 8 - 4,
28         6  * 8 - 5,
29         8  * 8 - 6,
30         10 * 8 - 7,
31         13 * 8 - 8,
32 };
33
34 static int inode_encode_field(u8 *out, u8 *end, u64 hi, u64 lo)
35 {
36         __be64 in[2] = { cpu_to_be64(hi), cpu_to_be64(lo), };
37         unsigned shift, bytes, bits = likely(!hi)
38                 ? fls64(lo)
39                 : fls64(hi) + 64;
40
41         for (shift = 1; shift <= 8; shift++)
42                 if (bits < bits_table[shift - 1])
43                         goto got_shift;
44
45         BUG();
46 got_shift:
47         bytes = byte_table[shift - 1];
48
49         BUG_ON(out + bytes > end);
50
51         memcpy(out, (u8 *) in + 16 - bytes, bytes);
52         *out |= (1 << 8) >> shift;
53
54         return bytes;
55 }
56
57 static int inode_decode_field(const u8 *in, const u8 *end,
58                               u64 out[2], unsigned *out_bits)
59 {
60         __be64 be[2] = { 0, 0 };
61         unsigned bytes, shift;
62         u8 *p;
63
64         if (in >= end)
65                 return -1;
66
67         if (!*in)
68                 return -1;
69
70         /*
71          * position of highest set bit indicates number of bytes:
72          * shift = number of bits to remove in high byte:
73          */
74         shift   = 8 - __fls(*in); /* 1 <= shift <= 8 */
75         bytes   = byte_table[shift - 1];
76
77         if (in + bytes > end)
78                 return -1;
79
80         p = (u8 *) be + 16 - bytes;
81         memcpy(p, in, bytes);
82         *p ^= (1 << 8) >> shift;
83
84         out[0] = be64_to_cpu(be[0]);
85         out[1] = be64_to_cpu(be[1]);
86         *out_bits = out[0] ? 64 + fls64(out[0]) : fls64(out[1]);
87
88         return bytes;
89 }
90
91 void bch2_inode_pack(struct bkey_inode_buf *packed,
92                      const struct bch_inode_unpacked *inode)
93 {
94         u8 *out = packed->inode.v.fields;
95         u8 *end = (void *) &packed[1];
96         u8 *last_nonzero_field = out;
97         unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
98         unsigned bytes;
99
100         bkey_inode_init(&packed->inode.k_i);
101         packed->inode.k.p.inode         = inode->bi_inum;
102         packed->inode.v.bi_hash_seed    = inode->bi_hash_seed;
103         packed->inode.v.bi_flags        = cpu_to_le32(inode->bi_flags);
104         packed->inode.v.bi_mode         = cpu_to_le16(inode->bi_mode);
105
106 #define x(_name, _bits)                                 \
107         out += inode_encode_field(out, end, 0, inode->_name);           \
108         nr_fields++;                                                    \
109                                                                         \
110         if (inode->_name) {                                             \
111                 last_nonzero_field = out;                               \
112                 last_nonzero_fieldnr = nr_fields;                       \
113         }
114
115         BCH_INODE_FIELDS()
116 #undef  x
117
118         out = last_nonzero_field;
119         nr_fields = last_nonzero_fieldnr;
120
121         bytes = out - (u8 *) &packed->inode.v;
122         set_bkey_val_bytes(&packed->inode.k, bytes);
123         memset_u64s_tail(&packed->inode.v, 0, bytes);
124
125         SET_INODE_NR_FIELDS(&packed->inode.v, nr_fields);
126
127         if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
128                 struct bch_inode_unpacked unpacked;
129
130                 int ret = bch2_inode_unpack(inode_i_to_s_c(&packed->inode),
131                                            &unpacked);
132                 BUG_ON(ret);
133                 BUG_ON(unpacked.bi_inum         != inode->bi_inum);
134                 BUG_ON(unpacked.bi_hash_seed    != inode->bi_hash_seed);
135                 BUG_ON(unpacked.bi_mode         != inode->bi_mode);
136
137 #define x(_name, _bits) BUG_ON(unpacked._name != inode->_name);
138                 BCH_INODE_FIELDS()
139 #undef  x
140         }
141 }
142
143 int bch2_inode_unpack(struct bkey_s_c_inode inode,
144                       struct bch_inode_unpacked *unpacked)
145 {
146         const u8 *in = inode.v->fields;
147         const u8 *end = (void *) inode.v + bkey_val_bytes(inode.k);
148         u64 field[2];
149         unsigned fieldnr = 0, field_bits;
150         int ret;
151
152         unpacked->bi_inum       = inode.k->p.inode;
153         unpacked->bi_hash_seed  = inode.v->bi_hash_seed;
154         unpacked->bi_flags      = le32_to_cpu(inode.v->bi_flags);
155         unpacked->bi_mode       = le16_to_cpu(inode.v->bi_mode);
156
157 #define x(_name, _bits)                                 \
158         if (fieldnr++ == INODE_NR_FIELDS(inode.v)) {                    \
159                 memset(&unpacked->_name, 0,                             \
160                        sizeof(*unpacked) -                              \
161                        offsetof(struct bch_inode_unpacked, _name));     \
162                 return 0;                                               \
163         }                                                               \
164                                                                         \
165         ret = inode_decode_field(in, end, field, &field_bits);          \
166         if (ret < 0)                                                    \
167                 return ret;                                             \
168                                                                         \
169         if (field_bits > sizeof(unpacked->_name) * 8)                   \
170                 return -1;                                              \
171                                                                         \
172         unpacked->_name = field[1];                                     \
173         in += ret;
174
175         BCH_INODE_FIELDS()
176 #undef  x
177
178         /* XXX: signal if there were more fields than expected? */
179
180         return 0;
181 }
182
183 struct btree_iter *bch2_inode_peek(struct btree_trans *trans,
184                                    struct bch_inode_unpacked *inode,
185                                    u64 inum, unsigned flags)
186 {
187         struct btree_iter *iter;
188         struct bkey_s_c k;
189         int ret;
190
191         iter = bch2_trans_get_iter(trans, BTREE_ID_INODES, POS(inum, 0),
192                                    BTREE_ITER_SLOTS|flags);
193         if (IS_ERR(iter))
194                 return iter;
195
196         k = bch2_btree_iter_peek_slot(iter);
197         ret = bkey_err(k);
198         if (ret)
199                 goto err;
200
201         ret = k.k->type == KEY_TYPE_inode ? 0 : -EIO;
202         if (ret)
203                 goto err;
204
205         ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode);
206         if (ret)
207                 goto err;
208
209         return iter;
210 err:
211         bch2_trans_iter_put(trans, iter);
212         return ERR_PTR(ret);
213 }
214
215 int bch2_inode_write(struct btree_trans *trans,
216                      struct btree_iter *iter,
217                      struct bch_inode_unpacked *inode)
218 {
219         struct bkey_inode_buf *inode_p;
220
221         inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
222         if (IS_ERR(inode_p))
223                 return PTR_ERR(inode_p);
224
225         bch2_inode_pack(inode_p, inode);
226         bch2_trans_update(trans, iter, &inode_p->inode.k_i, 0);
227         return 0;
228 }
229
230 const char *bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k)
231 {
232                 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
233                 struct bch_inode_unpacked unpacked;
234
235         if (k.k->p.offset)
236                 return "nonzero offset";
237
238         if (bkey_val_bytes(k.k) < sizeof(struct bch_inode))
239                 return "incorrect value size";
240
241         if (k.k->p.inode < BLOCKDEV_INODE_MAX)
242                 return "fs inode in blockdev range";
243
244         if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR)
245                 return "invalid str hash type";
246
247         if (bch2_inode_unpack(inode, &unpacked))
248                 return "invalid variable length fields";
249
250         if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1)
251                 return "invalid data checksum type";
252
253         if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1)
254                 return "invalid data checksum type";
255
256         if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
257             unpacked.bi_nlink != 0)
258                 return "flagged as unlinked but bi_nlink != 0";
259
260         return NULL;
261 }
262
263 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c,
264                        struct bkey_s_c k)
265 {
266         struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
267         struct bch_inode_unpacked unpacked;
268
269         if (bch2_inode_unpack(inode, &unpacked)) {
270                 pr_buf(out, "(unpack error)");
271                 return;
272         }
273
274 #define x(_name, _bits)                                         \
275         pr_buf(out, #_name ": %llu ", (u64) unpacked._name);
276         BCH_INODE_FIELDS()
277 #undef  x
278 }
279
280 const char *bch2_inode_generation_invalid(const struct bch_fs *c,
281                                           struct bkey_s_c k)
282 {
283         if (k.k->p.offset)
284                 return "nonzero offset";
285
286         if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation))
287                 return "incorrect value size";
288
289         return NULL;
290 }
291
292 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
293                                    struct bkey_s_c k)
294 {
295         struct bkey_s_c_inode_generation gen = bkey_s_c_to_inode_generation(k);
296
297         pr_buf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
298 }
299
300 void bch2_inode_init_early(struct bch_fs *c,
301                            struct bch_inode_unpacked *inode_u)
302 {
303         enum bch_str_hash_type str_hash =
304                 bch2_str_hash_opt_to_type(c, c->opts.str_hash);
305
306         memset(inode_u, 0, sizeof(*inode_u));
307
308         /* ick */
309         inode_u->bi_flags |= str_hash << INODE_STR_HASH_OFFSET;
310         get_random_bytes(&inode_u->bi_hash_seed,
311                          sizeof(inode_u->bi_hash_seed));
312 }
313
314 void bch2_inode_init_late(struct bch_inode_unpacked *inode_u, u64 now,
315                           uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
316                           struct bch_inode_unpacked *parent)
317 {
318         inode_u->bi_mode        = mode;
319         inode_u->bi_uid         = uid;
320         inode_u->bi_gid         = gid;
321         inode_u->bi_dev         = rdev;
322         inode_u->bi_atime       = now;
323         inode_u->bi_mtime       = now;
324         inode_u->bi_ctime       = now;
325         inode_u->bi_otime       = now;
326
327         if (parent && parent->bi_mode & S_ISGID) {
328                 inode_u->bi_gid = parent->bi_gid;
329                 if (S_ISDIR(mode))
330                         inode_u->bi_mode |= S_ISGID;
331         }
332
333         if (parent) {
334 #define x(_name, ...)   inode_u->bi_##_name = parent->bi_##_name;
335                 BCH_INODE_OPTS()
336 #undef x
337         }
338 }
339
340 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
341                      uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
342                      struct bch_inode_unpacked *parent)
343 {
344         bch2_inode_init_early(c, inode_u);
345         bch2_inode_init_late(inode_u, bch2_current_time(c),
346                              uid, gid, mode, rdev, parent);
347 }
348
349 static inline u32 bkey_generation(struct bkey_s_c k)
350 {
351         switch (k.k->type) {
352         case KEY_TYPE_inode:
353                 BUG();
354         case KEY_TYPE_inode_generation:
355                 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
356         default:
357                 return 0;
358         }
359 }
360
361 int bch2_inode_create(struct btree_trans *trans,
362                       struct bch_inode_unpacked *inode_u,
363                       u64 min, u64 max, u64 *hint)
364 {
365         struct bkey_inode_buf *inode_p;
366         struct btree_iter *iter = NULL;
367         struct bkey_s_c k;
368         u64 start;
369         int ret;
370
371         if (!max)
372                 max = ULLONG_MAX;
373
374         if (trans->c->opts.inodes_32bit)
375                 max = min_t(u64, max, U32_MAX);
376
377         start = READ_ONCE(*hint);
378
379         if (start >= max || start < min)
380                 start = min;
381
382         inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
383         if (IS_ERR(inode_p))
384                 return PTR_ERR(inode_p);
385 again:
386         for_each_btree_key(trans, iter, BTREE_ID_INODES, POS(start, 0),
387                            BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
388                 if (iter->pos.inode > max)
389                         break;
390
391                 if (k.k->type != KEY_TYPE_inode)
392                         goto found_slot;
393         }
394
395         bch2_trans_iter_put(trans, iter);
396
397         if (ret)
398                 return ret;
399
400         if (start != min) {
401                 /* Retry from start */
402                 start = min;
403                 goto again;
404         }
405
406         return -ENOSPC;
407 found_slot:
408         *hint                   = k.k->p.inode;
409         inode_u->bi_inum        = k.k->p.inode;
410         inode_u->bi_generation  = bkey_generation(k);
411
412         bch2_inode_pack(inode_p, inode_u);
413         bch2_trans_update(trans, iter, &inode_p->inode.k_i, 0);
414         bch2_trans_iter_put(trans, iter);
415         return 0;
416 }
417
418 int bch2_inode_rm(struct bch_fs *c, u64 inode_nr)
419 {
420         struct btree_trans trans;
421         struct btree_iter *iter;
422         struct bkey_i_inode_generation delete;
423         struct bpos start = POS(inode_nr, 0);
424         struct bpos end = POS(inode_nr + 1, 0);
425         int ret;
426
427         /*
428          * If this was a directory, there shouldn't be any real dirents left -
429          * but there could be whiteouts (from hash collisions) that we should
430          * delete:
431          *
432          * XXX: the dirent could ideally would delete whiteouts when they're no
433          * longer needed
434          */
435         ret   = bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
436                                         start, end, NULL) ?:
437                 bch2_btree_delete_range(c, BTREE_ID_XATTRS,
438                                         start, end, NULL) ?:
439                 bch2_btree_delete_range(c, BTREE_ID_DIRENTS,
440                                         start, end, NULL);
441         if (ret)
442                 return ret;
443
444         bch2_trans_init(&trans, c, 0, 0);
445
446         iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS(inode_nr, 0),
447                                    BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
448         do {
449                 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
450                 u32 bi_generation = 0;
451
452                 ret = bkey_err(k);
453                 if (ret)
454                         break;
455
456                 bch2_fs_inconsistent_on(k.k->type != KEY_TYPE_inode, c,
457                                         "inode %llu not found when deleting",
458                                         inode_nr);
459
460                 switch (k.k->type) {
461                 case KEY_TYPE_inode: {
462                         struct bch_inode_unpacked inode_u;
463
464                         if (!bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u))
465                                 bi_generation = inode_u.bi_generation + 1;
466                         break;
467                 }
468                 case KEY_TYPE_inode_generation: {
469                         struct bkey_s_c_inode_generation g =
470                                 bkey_s_c_to_inode_generation(k);
471                         bi_generation = le32_to_cpu(g.v->bi_generation);
472                         break;
473                 }
474                 }
475
476                 if (!bi_generation) {
477                         bkey_init(&delete.k);
478                         delete.k.p.inode = inode_nr;
479                 } else {
480                         bkey_inode_generation_init(&delete.k_i);
481                         delete.k.p.inode = inode_nr;
482                         delete.v.bi_generation = cpu_to_le32(bi_generation);
483                 }
484
485                 bch2_trans_update(&trans, iter, &delete.k_i, 0);
486
487                 ret = bch2_trans_commit(&trans, NULL, NULL,
488                                         BTREE_INSERT_NOFAIL);
489         } while (ret == -EINTR);
490
491         bch2_trans_exit(&trans);
492         return ret;
493 }
494
495 int bch2_inode_find_by_inum_trans(struct btree_trans *trans, u64 inode_nr,
496                                   struct bch_inode_unpacked *inode)
497 {
498         struct btree_iter *iter;
499         struct bkey_s_c k;
500         int ret;
501
502         iter = bch2_trans_get_iter(trans, BTREE_ID_INODES,
503                         POS(inode_nr, 0), BTREE_ITER_SLOTS);
504         if (IS_ERR(iter))
505                 return PTR_ERR(iter);
506
507         k = bch2_btree_iter_peek_slot(iter);
508         ret = bkey_err(k);
509         if (ret)
510                 goto err;
511
512         ret = k.k->type == KEY_TYPE_inode
513                 ? bch2_inode_unpack(bkey_s_c_to_inode(k), inode)
514                 : -ENOENT;
515 err:
516         bch2_trans_iter_put(trans, iter);
517         return ret;
518 }
519
520 int bch2_inode_find_by_inum(struct bch_fs *c, u64 inode_nr,
521                             struct bch_inode_unpacked *inode)
522 {
523         return bch2_trans_do(c, NULL, NULL, 0,
524                 bch2_inode_find_by_inum_trans(&trans, inode_nr, inode));
525 }
526
527 #ifdef CONFIG_BCACHEFS_DEBUG
528 void bch2_inode_pack_test(void)
529 {
530         struct bch_inode_unpacked *u, test_inodes[] = {
531                 {
532                         .bi_atime       = U64_MAX,
533                         .bi_ctime       = U64_MAX,
534                         .bi_mtime       = U64_MAX,
535                         .bi_otime       = U64_MAX,
536                         .bi_size        = U64_MAX,
537                         .bi_sectors     = U64_MAX,
538                         .bi_uid         = U32_MAX,
539                         .bi_gid         = U32_MAX,
540                         .bi_nlink       = U32_MAX,
541                         .bi_generation  = U32_MAX,
542                         .bi_dev         = U32_MAX,
543                 },
544         };
545
546         for (u = test_inodes;
547              u < test_inodes + ARRAY_SIZE(test_inodes);
548              u++) {
549                 struct bkey_inode_buf p;
550
551                 bch2_inode_pack(&p, u);
552         }
553 }
554 #endif