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