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