]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/inode.c
Update bcachefs sources to 078a1a596a bcachefs: Optimize bucket reuse
[bcachefs-tools-debian] / libbcachefs / inode.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "btree_key_cache.h"
5 #include "bkey_methods.h"
6 #include "btree_update.h"
7 #include "buckets.h"
8 #include "error.h"
9 #include "extents.h"
10 #include "extent_update.h"
11 #include "inode.h"
12 #include "str_hash.h"
13 #include "subvolume.h"
14 #include "varint.h"
15
16 #include <linux/random.h>
17
18 #include <asm/unaligned.h>
19
20 const char * const bch2_inode_opts[] = {
21 #define x(name, ...)    #name,
22         BCH_INODE_OPTS()
23 #undef  x
24         NULL,
25 };
26
27 static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
28
29 static int inode_decode_field(const u8 *in, const u8 *end,
30                               u64 out[2], unsigned *out_bits)
31 {
32         __be64 be[2] = { 0, 0 };
33         unsigned bytes, shift;
34         u8 *p;
35
36         if (in >= end)
37                 return -1;
38
39         if (!*in)
40                 return -1;
41
42         /*
43          * position of highest set bit indicates number of bytes:
44          * shift = number of bits to remove in high byte:
45          */
46         shift   = 8 - __fls(*in); /* 1 <= shift <= 8 */
47         bytes   = byte_table[shift - 1];
48
49         if (in + bytes > end)
50                 return -1;
51
52         p = (u8 *) be + 16 - bytes;
53         memcpy(p, in, bytes);
54         *p ^= (1 << 8) >> shift;
55
56         out[0] = be64_to_cpu(be[0]);
57         out[1] = be64_to_cpu(be[1]);
58         *out_bits = out[0] ? 64 + fls64(out[0]) : fls64(out[1]);
59
60         return bytes;
61 }
62
63 void bch2_inode_pack(struct bch_fs *c,
64                      struct bkey_inode_buf *packed,
65                      const struct bch_inode_unpacked *inode)
66 {
67         struct bkey_i_inode_v2 *k = &packed->inode;
68         u8 *out = k->v.fields;
69         u8 *end = (void *) &packed[1];
70         u8 *last_nonzero_field = out;
71         unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
72         unsigned bytes;
73         int ret;
74
75         bkey_inode_v2_init(&packed->inode.k_i);
76         packed->inode.k.p.offset        = inode->bi_inum;
77         packed->inode.v.bi_journal_seq  = cpu_to_le64(inode->bi_journal_seq);
78         packed->inode.v.bi_hash_seed    = inode->bi_hash_seed;
79         packed->inode.v.bi_flags        = cpu_to_le64(inode->bi_flags);
80         packed->inode.v.bi_flags        = cpu_to_le64(inode->bi_flags);
81         packed->inode.v.bi_mode         = cpu_to_le16(inode->bi_mode);
82
83 #define x(_name, _bits)                                                 \
84         nr_fields++;                                                    \
85                                                                         \
86         if (inode->_name) {                                             \
87                 ret = bch2_varint_encode_fast(out, inode->_name);       \
88                 out += ret;                                             \
89                                                                         \
90                 if (_bits > 64)                                         \
91                         *out++ = 0;                                     \
92                                                                         \
93                 last_nonzero_field = out;                               \
94                 last_nonzero_fieldnr = nr_fields;                       \
95         } else {                                                        \
96                 *out++ = 0;                                             \
97                                                                         \
98                 if (_bits > 64)                                         \
99                         *out++ = 0;                                     \
100         }
101
102         BCH_INODE_FIELDS()
103 #undef  x
104         BUG_ON(out > end);
105
106         out = last_nonzero_field;
107         nr_fields = last_nonzero_fieldnr;
108
109         bytes = out - (u8 *) &packed->inode.v;
110         set_bkey_val_bytes(&packed->inode.k, bytes);
111         memset_u64s_tail(&packed->inode.v, 0, bytes);
112
113         SET_INODEv2_NR_FIELDS(&k->v, nr_fields);
114
115         if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
116                 struct bch_inode_unpacked unpacked;
117
118                 int ret = bch2_inode_unpack(bkey_i_to_s_c(&packed->inode.k_i),
119                                            &unpacked);
120                 BUG_ON(ret);
121                 BUG_ON(unpacked.bi_inum         != inode->bi_inum);
122                 BUG_ON(unpacked.bi_hash_seed    != inode->bi_hash_seed);
123                 BUG_ON(unpacked.bi_mode         != inode->bi_mode);
124
125 #define x(_name, _bits) if (unpacked._name != inode->_name)             \
126                         panic("unpacked %llu should be %llu",           \
127                               (u64) unpacked._name, (u64) inode->_name);
128                 BCH_INODE_FIELDS()
129 #undef  x
130         }
131 }
132
133 static noinline int bch2_inode_unpack_v1(struct bkey_s_c_inode inode,
134                                 struct bch_inode_unpacked *unpacked)
135 {
136         const u8 *in = inode.v->fields;
137         const u8 *end = bkey_val_end(inode);
138         u64 field[2];
139         unsigned fieldnr = 0, field_bits;
140         int ret;
141
142 #define x(_name, _bits)                                 \
143         if (fieldnr++ == INODE_NR_FIELDS(inode.v)) {                    \
144                 memset(&unpacked->_name, 0,                             \
145                        sizeof(*unpacked) -                              \
146                        offsetof(struct bch_inode_unpacked, _name));     \
147                 return 0;                                               \
148         }                                                               \
149                                                                         \
150         ret = inode_decode_field(in, end, field, &field_bits);          \
151         if (ret < 0)                                                    \
152                 return ret;                                             \
153                                                                         \
154         if (field_bits > sizeof(unpacked->_name) * 8)                   \
155                 return -1;                                              \
156                                                                         \
157         unpacked->_name = field[1];                                     \
158         in += ret;
159
160         BCH_INODE_FIELDS()
161 #undef  x
162
163         /* XXX: signal if there were more fields than expected? */
164         return 0;
165 }
166
167 static int bch2_inode_unpack_v2(struct bch_inode_unpacked *unpacked,
168                                 const u8 *in, const u8 *end,
169                                 unsigned nr_fields)
170 {
171         unsigned fieldnr = 0;
172         int ret;
173         u64 v[2];
174
175 #define x(_name, _bits)                                                 \
176         if (fieldnr < nr_fields) {                                      \
177                 ret = bch2_varint_decode_fast(in, end, &v[0]);          \
178                 if (ret < 0)                                            \
179                         return ret;                                     \
180                 in += ret;                                              \
181                                                                         \
182                 if (_bits > 64) {                                       \
183                         ret = bch2_varint_decode_fast(in, end, &v[1]);  \
184                         if (ret < 0)                                    \
185                                 return ret;                             \
186                         in += ret;                                      \
187                 } else {                                                \
188                         v[1] = 0;                                       \
189                 }                                                       \
190         } else {                                                        \
191                 v[0] = v[1] = 0;                                        \
192         }                                                               \
193                                                                         \
194         unpacked->_name = v[0];                                         \
195         if (v[1] || v[0] != unpacked->_name)                            \
196                 return -1;                                              \
197         fieldnr++;
198
199         BCH_INODE_FIELDS()
200 #undef  x
201
202         /* XXX: signal if there were more fields than expected? */
203         return 0;
204 }
205
206 int bch2_inode_unpack(struct bkey_s_c k,
207                       struct bch_inode_unpacked *unpacked)
208 {
209         switch (k.k->type) {
210         case KEY_TYPE_inode: {
211                 struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
212
213                 unpacked->bi_inum       = inode.k->p.offset;
214                 unpacked->bi_journal_seq= 0;
215                 unpacked->bi_hash_seed  = inode.v->bi_hash_seed;
216                 unpacked->bi_flags      = le32_to_cpu(inode.v->bi_flags);
217                 unpacked->bi_mode       = le16_to_cpu(inode.v->bi_mode);
218
219                 if (INODE_NEW_VARINT(inode.v)) {
220                         return bch2_inode_unpack_v2(unpacked, inode.v->fields,
221                                                     bkey_val_end(inode),
222                                                     INODE_NR_FIELDS(inode.v));
223                 } else {
224                         return bch2_inode_unpack_v1(inode, unpacked);
225                 }
226                 break;
227         }
228         case KEY_TYPE_inode_v2: {
229                 struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
230
231                 unpacked->bi_inum       = inode.k->p.offset;
232                 unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
233                 unpacked->bi_hash_seed  = inode.v->bi_hash_seed;
234                 unpacked->bi_flags      = le64_to_cpu(inode.v->bi_flags);
235                 unpacked->bi_mode       = le16_to_cpu(inode.v->bi_mode);
236
237                 return bch2_inode_unpack_v2(unpacked, inode.v->fields,
238                                             bkey_val_end(inode),
239                                             INODEv2_NR_FIELDS(inode.v));
240         }
241         default:
242                 BUG();
243         }
244 }
245
246 int bch2_inode_peek(struct btree_trans *trans,
247                     struct btree_iter *iter,
248                     struct bch_inode_unpacked *inode,
249                     subvol_inum inum, unsigned flags)
250 {
251         struct bkey_s_c k;
252         u32 snapshot;
253         int ret;
254
255         if (0 && trans->c->opts.inodes_use_key_cache)
256                 flags |= BTREE_ITER_CACHED;
257
258         ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
259         if (ret)
260                 return ret;
261
262         bch2_trans_iter_init(trans, iter, BTREE_ID_inodes,
263                              SPOS(0, inum.inum, snapshot), flags);
264         k = bch2_btree_iter_peek_slot(iter);
265         ret = bkey_err(k);
266         if (ret)
267                 goto err;
268
269         ret = bkey_is_inode(k.k) ? 0 : -ENOENT;
270         if (ret)
271                 goto err;
272
273         ret = bch2_inode_unpack(k, inode);
274         if (ret)
275                 goto err;
276
277         return 0;
278 err:
279         bch2_trans_iter_exit(trans, iter);
280         return ret;
281 }
282
283 int bch2_inode_write(struct btree_trans *trans,
284                      struct btree_iter *iter,
285                      struct bch_inode_unpacked *inode)
286 {
287         struct bkey_inode_buf *inode_p;
288
289         inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
290         if (IS_ERR(inode_p))
291                 return PTR_ERR(inode_p);
292
293         bch2_inode_pack(trans->c, inode_p, inode);
294         inode_p->inode.k.p.snapshot = iter->snapshot;
295         return bch2_trans_update(trans, iter, &inode_p->inode.k_i, 0);
296 }
297
298 const char *bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k)
299 {
300         struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
301         struct bch_inode_unpacked unpacked;
302
303         if (k.k->p.inode)
304                 return "nonzero k.p.inode";
305
306         if (bkey_val_bytes(k.k) < sizeof(struct bch_inode))
307                 return "incorrect value size";
308
309         if (k.k->p.offset < BLOCKDEV_INODE_MAX)
310                 return "fs inode in blockdev range";
311
312         if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR)
313                 return "invalid str hash type";
314
315         if (bch2_inode_unpack(k, &unpacked))
316                 return "invalid variable length fields";
317
318         if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1)
319                 return "invalid data checksum type";
320
321         if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1)
322                 return "invalid data checksum type";
323
324         if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
325             unpacked.bi_nlink != 0)
326                 return "flagged as unlinked but bi_nlink != 0";
327
328         if (unpacked.bi_subvol && !S_ISDIR(unpacked.bi_mode))
329                 return "subvolume root but not a directory";
330
331         return NULL;
332 }
333
334 const char *bch2_inode_v2_invalid(const struct bch_fs *c, struct bkey_s_c k)
335 {
336         struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
337         struct bch_inode_unpacked unpacked;
338
339         if (k.k->p.inode)
340                 return "nonzero k.p.inode";
341
342         if (bkey_val_bytes(k.k) < sizeof(struct bch_inode))
343                 return "incorrect value size";
344
345         if (k.k->p.offset < BLOCKDEV_INODE_MAX)
346                 return "fs inode in blockdev range";
347
348         if (INODEv2_STR_HASH(inode.v) >= BCH_STR_HASH_NR)
349                 return "invalid str hash type";
350
351         if (bch2_inode_unpack(k, &unpacked))
352                 return "invalid variable length fields";
353
354         if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1)
355                 return "invalid data checksum type";
356
357         if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1)
358                 return "invalid data checksum type";
359
360         if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
361             unpacked.bi_nlink != 0)
362                 return "flagged as unlinked but bi_nlink != 0";
363
364         if (unpacked.bi_subvol && !S_ISDIR(unpacked.bi_mode))
365                 return "subvolume root but not a directory";
366
367         return NULL;
368 }
369
370 static void __bch2_inode_unpacked_to_text(struct printbuf *out, struct bch_inode_unpacked *inode)
371 {
372         pr_buf(out, "mode %o flags %x journal_seq %llu",
373                inode->bi_mode, inode->bi_flags,
374                inode->bi_journal_seq);
375
376 #define x(_name, _bits)                                         \
377         pr_buf(out, " "#_name " %llu", (u64) inode->_name);
378         BCH_INODE_FIELDS()
379 #undef  x
380 }
381
382 void bch2_inode_unpacked_to_text(struct printbuf *out, struct bch_inode_unpacked *inode)
383 {
384         pr_buf(out, "inum: %llu ", inode->bi_inum);
385         __bch2_inode_unpacked_to_text(out, inode);
386 }
387
388 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c,
389                        struct bkey_s_c k)
390 {
391         struct bch_inode_unpacked inode;
392
393         if (bch2_inode_unpack(k, &inode)) {
394                 pr_buf(out, "(unpack error)");
395                 return;
396         }
397
398         __bch2_inode_unpacked_to_text(out, &inode);
399 }
400
401 const char *bch2_inode_generation_invalid(const struct bch_fs *c,
402                                           struct bkey_s_c k)
403 {
404         if (k.k->p.inode)
405                 return "nonzero k.p.inode";
406
407         if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation))
408                 return "incorrect value size";
409
410         return NULL;
411 }
412
413 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
414                                    struct bkey_s_c k)
415 {
416         struct bkey_s_c_inode_generation gen = bkey_s_c_to_inode_generation(k);
417
418         pr_buf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
419 }
420
421 void bch2_inode_init_early(struct bch_fs *c,
422                            struct bch_inode_unpacked *inode_u)
423 {
424         enum bch_str_hash_type str_hash =
425                 bch2_str_hash_opt_to_type(c, c->opts.str_hash);
426
427         memset(inode_u, 0, sizeof(*inode_u));
428
429         /* ick */
430         inode_u->bi_flags |= str_hash << INODE_STR_HASH_OFFSET;
431         get_random_bytes(&inode_u->bi_hash_seed,
432                          sizeof(inode_u->bi_hash_seed));
433 }
434
435 void bch2_inode_init_late(struct bch_inode_unpacked *inode_u, u64 now,
436                           uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
437                           struct bch_inode_unpacked *parent)
438 {
439         inode_u->bi_mode        = mode;
440         inode_u->bi_uid         = uid;
441         inode_u->bi_gid         = gid;
442         inode_u->bi_dev         = rdev;
443         inode_u->bi_atime       = now;
444         inode_u->bi_mtime       = now;
445         inode_u->bi_ctime       = now;
446         inode_u->bi_otime       = now;
447
448         if (parent && parent->bi_mode & S_ISGID) {
449                 inode_u->bi_gid = parent->bi_gid;
450                 if (S_ISDIR(mode))
451                         inode_u->bi_mode |= S_ISGID;
452         }
453
454         if (parent) {
455 #define x(_name, ...)   inode_u->bi_##_name = parent->bi_##_name;
456                 BCH_INODE_OPTS()
457 #undef x
458         }
459 }
460
461 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
462                      uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
463                      struct bch_inode_unpacked *parent)
464 {
465         bch2_inode_init_early(c, inode_u);
466         bch2_inode_init_late(inode_u, bch2_current_time(c),
467                              uid, gid, mode, rdev, parent);
468 }
469
470 static inline u32 bkey_generation(struct bkey_s_c k)
471 {
472         switch (k.k->type) {
473         case KEY_TYPE_inode:
474         case KEY_TYPE_inode_v2:
475                 BUG();
476         case KEY_TYPE_inode_generation:
477                 return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
478         default:
479                 return 0;
480         }
481 }
482
483 /*
484  * This just finds an empty slot:
485  */
486 int bch2_inode_create(struct btree_trans *trans,
487                       struct btree_iter *iter,
488                       struct bch_inode_unpacked *inode_u,
489                       u32 snapshot, u64 cpu)
490 {
491         struct bch_fs *c = trans->c;
492         struct bkey_s_c k;
493         u64 min, max, start, pos, *hint;
494         int ret = 0;
495         unsigned bits = (c->opts.inodes_32bit ? 31 : 63);
496
497         if (c->opts.shard_inode_numbers) {
498                 bits -= c->inode_shard_bits;
499
500                 min = (cpu << bits);
501                 max = (cpu << bits) | ~(ULLONG_MAX << bits);
502
503                 min = max_t(u64, min, BLOCKDEV_INODE_MAX);
504                 hint = c->unused_inode_hints + cpu;
505         } else {
506                 min = BLOCKDEV_INODE_MAX;
507                 max = ~(ULLONG_MAX << bits);
508                 hint = c->unused_inode_hints;
509         }
510
511         start = READ_ONCE(*hint);
512
513         if (start >= max || start < min)
514                 start = min;
515
516         pos = start;
517         bch2_trans_iter_init(trans, iter, BTREE_ID_inodes, POS(0, pos),
518                              BTREE_ITER_ALL_SNAPSHOTS|
519                              BTREE_ITER_INTENT);
520 again:
521         while ((k = bch2_btree_iter_peek(iter)).k &&
522                !(ret = bkey_err(k)) &&
523                bkey_cmp(k.k->p, POS(0, max)) < 0) {
524                 while (pos < iter->pos.offset) {
525                         if (!bch2_btree_key_cache_find(c, BTREE_ID_inodes, POS(0, pos)))
526                                 goto found_slot;
527
528                         pos++;
529                 }
530
531                 if (k.k->p.snapshot == snapshot &&
532                     !bkey_is_inode(k.k) &&
533                     !bch2_btree_key_cache_find(c, BTREE_ID_inodes, SPOS(0, pos, snapshot))) {
534                         bch2_btree_iter_advance(iter);
535                         continue;
536                 }
537
538                 /*
539                  * We don't need to iterate over keys in every snapshot once
540                  * we've found just one:
541                  */
542                 pos = iter->pos.offset + 1;
543                 bch2_btree_iter_set_pos(iter, POS(0, pos));
544         }
545
546         while (!ret && pos < max) {
547                 if (!bch2_btree_key_cache_find(c, BTREE_ID_inodes, POS(0, pos)))
548                         goto found_slot;
549
550                 pos++;
551         }
552
553         if (!ret && start == min)
554                 ret = -ENOSPC;
555
556         if (ret) {
557                 bch2_trans_iter_exit(trans, iter);
558                 return ret;
559         }
560
561         /* Retry from start */
562         pos = start = min;
563         bch2_btree_iter_set_pos(iter, POS(0, pos));
564         goto again;
565 found_slot:
566         bch2_btree_iter_set_pos(iter, SPOS(0, pos, snapshot));
567         k = bch2_btree_iter_peek_slot(iter);
568         ret = bkey_err(k);
569         if (ret) {
570                 bch2_trans_iter_exit(trans, iter);
571                 return ret;
572         }
573
574         /* We may have raced while the iterator wasn't pointing at pos: */
575         if (bkey_is_inode(k.k) ||
576             bch2_btree_key_cache_find(c, BTREE_ID_inodes, k.k->p))
577                 goto again;
578
579         *hint                   = k.k->p.offset;
580         inode_u->bi_inum        = k.k->p.offset;
581         inode_u->bi_generation  = bkey_generation(k);
582         return 0;
583 }
584
585 static int bch2_inode_delete_keys(struct btree_trans *trans,
586                                   subvol_inum inum, enum btree_id id)
587 {
588         struct btree_iter iter;
589         struct bkey_s_c k;
590         struct bkey_i delete;
591         u32 snapshot;
592         int ret = 0;
593
594         /*
595          * We're never going to be deleting extents, no need to use an extent
596          * iterator:
597          */
598         bch2_trans_iter_init(trans, &iter, id, POS(inum.inum, 0),
599                              BTREE_ITER_NOT_EXTENTS|
600                              BTREE_ITER_INTENT);
601
602         while (1) {
603                 bch2_trans_begin(trans);
604
605                 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
606                 if (ret)
607                         goto err;
608
609                 bch2_btree_iter_set_snapshot(&iter, snapshot);
610
611                 k = bch2_btree_iter_peek(&iter);
612                 ret = bkey_err(k);
613                 if (ret)
614                         goto err;
615
616                 if (!k.k || iter.pos.inode != inum.inum)
617                         break;
618
619                 bkey_init(&delete.k);
620                 delete.k.p = iter.pos;
621
622                 ret = bch2_trans_update(trans, &iter, &delete, 0) ?:
623                       bch2_trans_commit(trans, NULL, NULL,
624                                         BTREE_INSERT_NOFAIL);
625 err:
626                 if (ret && ret != -EINTR)
627                         break;
628         }
629
630         bch2_trans_iter_exit(trans, &iter);
631         return ret;
632 }
633
634 int bch2_inode_rm(struct bch_fs *c, subvol_inum inum, bool cached)
635 {
636         struct btree_trans trans;
637         struct btree_iter iter = { NULL };
638         struct bkey_i_inode_generation delete;
639         struct bch_inode_unpacked inode_u;
640         struct bkey_s_c k;
641         unsigned iter_flags = BTREE_ITER_INTENT;
642         u32 snapshot;
643         int ret;
644
645         if (0 && cached && c->opts.inodes_use_key_cache)
646                 iter_flags |= BTREE_ITER_CACHED;
647
648         bch2_trans_init(&trans, c, 0, 1024);
649
650         /*
651          * If this was a directory, there shouldn't be any real dirents left -
652          * but there could be whiteouts (from hash collisions) that we should
653          * delete:
654          *
655          * XXX: the dirent could ideally would delete whiteouts when they're no
656          * longer needed
657          */
658         ret   = bch2_inode_delete_keys(&trans, inum, BTREE_ID_extents) ?:
659                 bch2_inode_delete_keys(&trans, inum, BTREE_ID_xattrs) ?:
660                 bch2_inode_delete_keys(&trans, inum, BTREE_ID_dirents);
661         if (ret)
662                 goto err;
663 retry:
664         bch2_trans_begin(&trans);
665
666         ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
667         if (ret)
668                 goto err;
669
670         bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes,
671                              SPOS(0, inum.inum, snapshot), iter_flags);
672         k = bch2_btree_iter_peek_slot(&iter);
673
674         ret = bkey_err(k);
675         if (ret)
676                 goto err;
677
678         if (!bkey_is_inode(k.k)) {
679                 bch2_fs_inconsistent(trans.c,
680                                      "inode %llu not found when deleting",
681                                      inum.inum);
682                 ret = -EIO;
683                 goto err;
684         }
685
686         bch2_inode_unpack(k, &inode_u);
687
688         /* Subvolume root? */
689         BUG_ON(inode_u.bi_subvol);
690
691         bkey_inode_generation_init(&delete.k_i);
692         delete.k.p = iter.pos;
693         delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
694
695         ret   = bch2_trans_update(&trans, &iter, &delete.k_i, 0) ?:
696                 bch2_trans_commit(&trans, NULL, NULL,
697                                 BTREE_INSERT_NOFAIL);
698 err:
699         bch2_trans_iter_exit(&trans, &iter);
700         if (ret == -EINTR)
701                 goto retry;
702
703         bch2_trans_exit(&trans);
704         return ret;
705 }
706
707 int bch2_inode_find_by_inum_trans(struct btree_trans *trans,
708                                   subvol_inum inum,
709                                   struct bch_inode_unpacked *inode)
710 {
711         struct btree_iter iter;
712         int ret;
713
714         ret = bch2_inode_peek(trans, &iter, inode, inum, 0);
715         if (!ret)
716                 bch2_trans_iter_exit(trans, &iter);
717         return ret;
718 }
719
720 int bch2_inode_find_by_inum(struct bch_fs *c, subvol_inum inum,
721                             struct bch_inode_unpacked *inode)
722 {
723         return bch2_trans_do(c, NULL, NULL, 0,
724                 bch2_inode_find_by_inum_trans(&trans, inum, inode));
725 }