]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fsck.c
Update bcachefs sources to 9e76e8d98c bcachefs: Fix uninitialized field in hash_check...
[bcachefs-tools-debian] / libbcachefs / fsck.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "btree_update.h"
5 #include "dirent.h"
6 #include "error.h"
7 #include "fs-common.h"
8 #include "fsck.h"
9 #include "inode.h"
10 #include "keylist.h"
11 #include "super.h"
12 #include "xattr.h"
13
14 #include <linux/dcache.h> /* struct qstr */
15 #include <linux/generic-radix-tree.h>
16
17 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
18
19 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum)
20 {
21         struct btree_iter *iter;
22         struct bkey_s_c k;
23         u64 sectors = 0;
24         int ret;
25
26         for_each_btree_key(trans, iter, BTREE_ID_EXTENTS,
27                            POS(inum, 0), 0, k, ret) {
28                 if (k.k->p.inode != inum)
29                         break;
30
31                 if (bkey_extent_is_allocation(k.k))
32                         sectors += k.k->size;
33         }
34
35         bch2_trans_iter_free(trans, iter);
36
37         return ret ?: sectors;
38 }
39
40 static int remove_dirent(struct btree_trans *trans,
41                          struct bkey_s_c_dirent dirent)
42 {
43         struct bch_fs *c = trans->c;
44         struct qstr name;
45         struct bch_inode_unpacked dir_inode;
46         struct bch_hash_info dir_hash_info;
47         u64 dir_inum = dirent.k->p.inode;
48         int ret;
49         char *buf;
50
51         name.len = bch2_dirent_name_bytes(dirent);
52         buf = kmalloc(name.len + 1, GFP_KERNEL);
53         if (!buf)
54                 return -ENOMEM;
55
56         memcpy(buf, dirent.v->d_name, name.len);
57         buf[name.len] = '\0';
58         name.name = buf;
59
60         /* Unlock so we don't deadlock, after copying name: */
61         bch2_trans_unlock(trans);
62
63         ret = bch2_inode_find_by_inum(c, dir_inum, &dir_inode);
64         if (ret) {
65                 bch_err(c, "remove_dirent: err %i looking up directory inode", ret);
66                 goto err;
67         }
68
69         dir_hash_info = bch2_hash_info_init(c, &dir_inode);
70
71         ret = bch2_dirent_delete(c, dir_inum, &dir_hash_info, &name, NULL);
72         if (ret)
73                 bch_err(c, "remove_dirent: err %i deleting dirent", ret);
74 err:
75         kfree(buf);
76         return ret;
77 }
78
79 static int reattach_inode(struct bch_fs *c,
80                           struct bch_inode_unpacked *lostfound_inode,
81                           u64 inum)
82 {
83         struct bch_inode_unpacked inode_u;
84         char name_buf[20];
85         struct qstr name;
86         int ret;
87
88         snprintf(name_buf, sizeof(name_buf), "%llu", inum);
89         name = (struct qstr) QSTR(name_buf);
90
91         ret = bch2_trans_do(c, NULL,
92                             BTREE_INSERT_ATOMIC|
93                             BTREE_INSERT_LAZY_RW,
94                 bch2_link_trans(&trans, lostfound_inode->bi_inum,
95                                 inum, &inode_u, &name));
96         if (ret)
97                 bch_err(c, "error %i reattaching inode %llu", ret, inum);
98
99         return ret;
100 }
101
102 struct inode_walker {
103         bool                    first_this_inode;
104         bool                    have_inode;
105         u64                     cur_inum;
106         struct bch_inode_unpacked inode;
107 };
108
109 static struct inode_walker inode_walker_init(void)
110 {
111         return (struct inode_walker) {
112                 .cur_inum       = -1,
113                 .have_inode     = false,
114         };
115 }
116
117 static int walk_inode(struct btree_trans *trans,
118                       struct inode_walker *w, u64 inum)
119 {
120         if (inum != w->cur_inum) {
121                 int ret = bch2_inode_find_by_inum_trans(trans, inum,
122                                                         &w->inode);
123
124                 if (ret && ret != -ENOENT)
125                         return ret;
126
127                 w->have_inode   = !ret;
128                 w->cur_inum     = inum;
129                 w->first_this_inode = true;
130         } else {
131                 w->first_this_inode = false;
132         }
133
134         return 0;
135 }
136
137 struct hash_check {
138         struct bch_hash_info    info;
139
140         /* start of current chain of hash collisions: */
141         struct btree_iter       *chain;
142
143         /* next offset in current chain of hash collisions: */
144         u64                     chain_end;
145 };
146
147 static void hash_check_init(struct hash_check *h)
148 {
149         h->chain = NULL;
150         h->chain_end = 0;
151 }
152
153 static void hash_stop_chain(struct btree_trans *trans,
154                             struct hash_check *h)
155 {
156         if (h->chain)
157                 bch2_trans_iter_free(trans, h->chain);
158         h->chain = NULL;
159 }
160
161 static void hash_check_set_inode(struct btree_trans *trans,
162                                  struct hash_check *h,
163                                  const struct bch_inode_unpacked *bi)
164 {
165         h->info = bch2_hash_info_init(trans->c, bi);
166         hash_stop_chain(trans, h);
167 }
168
169 static int hash_redo_key(const struct bch_hash_desc desc,
170                          struct btree_trans *trans, struct hash_check *h,
171                          struct btree_iter *k_iter, struct bkey_s_c k,
172                          u64 hashed)
173 {
174         struct bkey_i *tmp;
175         int ret = 0;
176
177         tmp = kmalloc(bkey_bytes(k.k), GFP_KERNEL);
178         if (!tmp)
179                 return -ENOMEM;
180
181         bkey_reassemble(tmp, k);
182
183         ret = bch2_btree_delete_at(trans, k_iter, 0);
184         if (ret)
185                 goto err;
186
187         bch2_hash_set(trans, desc, &h->info, k_iter->pos.inode,
188                       tmp, BCH_HASH_SET_MUST_CREATE);
189         ret = bch2_trans_commit(trans, NULL, NULL,
190                                 BTREE_INSERT_NOFAIL|
191                                 BTREE_INSERT_LAZY_RW);
192 err:
193         kfree(tmp);
194         return ret;
195 }
196
197 static int fsck_hash_delete_at(struct btree_trans *trans,
198                                const struct bch_hash_desc desc,
199                                struct bch_hash_info *info,
200                                struct btree_iter *iter)
201 {
202         int ret;
203 retry:
204         ret   = bch2_hash_delete_at(trans, desc, info, iter) ?:
205                 bch2_trans_commit(trans, NULL, NULL,
206                                   BTREE_INSERT_ATOMIC|
207                                   BTREE_INSERT_NOFAIL|
208                                   BTREE_INSERT_LAZY_RW);
209         if (ret == -EINTR) {
210                 ret = bch2_btree_iter_traverse(iter);
211                 if (!ret)
212                         goto retry;
213         }
214
215         return ret;
216 }
217
218 static int hash_check_duplicates(struct btree_trans *trans,
219                         const struct bch_hash_desc desc, struct hash_check *h,
220                         struct btree_iter *k_iter, struct bkey_s_c k)
221 {
222         struct bch_fs *c = trans->c;
223         struct btree_iter *iter;
224         struct bkey_s_c k2;
225         char buf[200];
226         int ret = 0;
227
228         if (!bkey_cmp(h->chain->pos, k_iter->pos))
229                 return 0;
230
231         iter = bch2_trans_copy_iter(trans, h->chain);
232         BUG_ON(IS_ERR(iter));
233
234         for_each_btree_key_continue(iter, 0, k2, ret) {
235                 if (bkey_cmp(k2.k->p, k.k->p) >= 0)
236                         break;
237
238                 if (fsck_err_on(k2.k->type == desc.key_type &&
239                                 !desc.cmp_bkey(k, k2), c,
240                                 "duplicate hash table keys:\n%s",
241                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
242                                                        k), buf))) {
243                         ret = fsck_hash_delete_at(trans, desc, &h->info, k_iter);
244                         if (ret)
245                                 return ret;
246                         ret = 1;
247                         break;
248                 }
249         }
250 fsck_err:
251         bch2_trans_iter_free(trans, iter);
252         return ret;
253 }
254
255 static void hash_set_chain_start(struct btree_trans *trans,
256                         const struct bch_hash_desc desc,
257                         struct hash_check *h,
258                         struct btree_iter *k_iter, struct bkey_s_c k)
259 {
260         bool hole = (k.k->type != KEY_TYPE_whiteout &&
261                      k.k->type != desc.key_type);
262
263         if (hole || k.k->p.offset > h->chain_end + 1)
264                 hash_stop_chain(trans, h);
265
266         if (!hole) {
267                 if (!h->chain) {
268                         h->chain = bch2_trans_copy_iter(trans, k_iter);
269                         BUG_ON(IS_ERR(h->chain));
270                 }
271
272                 h->chain_end = k.k->p.offset;
273         }
274 }
275
276 static bool key_has_correct_hash(struct btree_trans *trans,
277                         const struct bch_hash_desc desc,
278                         struct hash_check *h,
279                         struct btree_iter *k_iter, struct bkey_s_c k)
280 {
281         u64 hash;
282
283         hash_set_chain_start(trans, desc, h, k_iter, k);
284
285         if (k.k->type != desc.key_type)
286                 return true;
287
288         hash = desc.hash_bkey(&h->info, k);
289
290         return hash >= h->chain->pos.offset &&
291                 hash <= k.k->p.offset;
292 }
293
294 static int hash_check_key(struct btree_trans *trans,
295                         const struct bch_hash_desc desc, struct hash_check *h,
296                         struct btree_iter *k_iter, struct bkey_s_c k)
297 {
298         struct bch_fs *c = trans->c;
299         char buf[200];
300         u64 hashed;
301         int ret = 0;
302
303         hash_set_chain_start(trans, desc, h, k_iter, k);
304
305         if (k.k->type != desc.key_type)
306                 return 0;
307
308         hashed = desc.hash_bkey(&h->info, k);
309
310         if (fsck_err_on(hashed < h->chain->pos.offset ||
311                         hashed > k.k->p.offset, c,
312                         "hash table key at wrong offset: btree %u, %llu, "
313                         "hashed to %llu chain starts at %llu\n%s",
314                         desc.btree_id, k.k->p.offset,
315                         hashed, h->chain->pos.offset,
316                         (bch2_bkey_val_to_text(&PBUF(buf), c,
317                                                k), buf))) {
318                 ret = hash_redo_key(desc, trans, h, k_iter, k, hashed);
319                 if (ret) {
320                         bch_err(c, "hash_redo_key err %i", ret);
321                         return ret;
322                 }
323                 return 1;
324         }
325
326         ret = hash_check_duplicates(trans, desc, h, k_iter, k);
327 fsck_err:
328         return ret;
329 }
330
331 static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
332                              struct btree_iter *iter, struct bkey_s_c *k)
333 {
334         struct bch_fs *c = trans->c;
335         struct bkey_i_dirent *d = NULL;
336         int ret = -EINVAL;
337         char buf[200];
338         unsigned len;
339         u64 hash;
340
341         if (key_has_correct_hash(trans, bch2_dirent_hash_desc, h, iter, *k))
342                 return 0;
343
344         len = bch2_dirent_name_bytes(bkey_s_c_to_dirent(*k));
345         BUG_ON(!len);
346
347         memcpy(buf, bkey_s_c_to_dirent(*k).v->d_name, len);
348         buf[len] = '\0';
349
350         d = kmalloc(bkey_bytes(k->k), GFP_KERNEL);
351         if (!d) {
352                 bch_err(c, "memory allocation failure");
353                 return -ENOMEM;
354         }
355
356         bkey_reassemble(&d->k_i, *k);
357
358         do {
359                 --len;
360                 if (!len)
361                         goto err_redo;
362
363                 d->k.u64s = BKEY_U64s + dirent_val_u64s(len);
364
365                 BUG_ON(bkey_val_bytes(&d->k) <
366                        offsetof(struct bch_dirent, d_name) + len);
367
368                 memset(d->v.d_name + len, 0,
369                        bkey_val_bytes(&d->k) -
370                        offsetof(struct bch_dirent, d_name) - len);
371
372                 hash = bch2_dirent_hash_desc.hash_bkey(&h->info,
373                                                 bkey_i_to_s_c(&d->k_i));
374         } while (hash < h->chain->pos.offset ||
375                  hash > k->k->p.offset);
376
377         if (fsck_err(c, "dirent with junk at end, was %s (%zu) now %s (%u)",
378                      buf, strlen(buf), d->v.d_name, len)) {
379                 bch2_trans_update(trans, iter, &d->k_i);
380
381                 ret = bch2_trans_commit(trans, NULL, NULL,
382                                         BTREE_INSERT_NOFAIL|
383                                         BTREE_INSERT_LAZY_RW);
384                 if (ret)
385                         goto err;
386
387                 *k = bch2_btree_iter_peek(iter);
388
389                 BUG_ON(k->k->type != KEY_TYPE_dirent);
390         }
391 err:
392 fsck_err:
393         kfree(d);
394         return ret;
395 err_redo:
396         hash = bch2_dirent_hash_desc.hash_bkey(&h->info, *k);
397
398         if (fsck_err(c, "cannot fix dirent by removing trailing garbage %s (%zu)\n"
399                      "hash table key at wrong offset: btree %u, offset %llu, "
400                      "hashed to %llu chain starts at %llu\n%s",
401                      buf, strlen(buf), BTREE_ID_DIRENTS,
402                      k->k->p.offset, hash, h->chain->pos.offset,
403                      (bch2_bkey_val_to_text(&PBUF(buf), c,
404                                             *k), buf))) {
405                 ret = hash_redo_key(bch2_dirent_hash_desc, trans,
406                                     h, iter, *k, hash);
407                 if (ret)
408                         bch_err(c, "hash_redo_key err %i", ret);
409                 else
410                         ret = 1;
411         }
412
413         goto err;
414 }
415
416 static int bch2_inode_truncate(struct bch_fs *c, u64 inode_nr, u64 new_size)
417 {
418         return bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
419                         POS(inode_nr, round_up(new_size, block_bytes(c)) >> 9),
420                         POS(inode_nr + 1, 0), NULL);
421 }
422
423 /*
424  * Walk extents: verify that extents have a corresponding S_ISREG inode, and
425  * that i_size an i_sectors are consistent
426  */
427 noinline_for_stack
428 static int check_extents(struct bch_fs *c)
429 {
430         struct inode_walker w = inode_walker_init();
431         struct btree_trans trans;
432         struct btree_iter *iter;
433         struct bkey_s_c k;
434         u64 i_sectors;
435         int ret = 0;
436
437         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
438
439         bch_verbose(c, "checking extents");
440
441         iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
442                                    POS(BCACHEFS_ROOT_INO, 0), 0);
443 retry:
444         for_each_btree_key_continue(iter, 0, k, ret) {
445                 ret = walk_inode(&trans, &w, k.k->p.inode);
446                 if (ret)
447                         break;
448
449                 if (fsck_err_on(!w.have_inode, c,
450                         "extent type %u for missing inode %llu",
451                         k.k->type, k.k->p.inode) ||
452                     fsck_err_on(w.have_inode &&
453                         !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
454                         "extent type %u for non regular file, inode %llu mode %o",
455                         k.k->type, k.k->p.inode, w.inode.bi_mode)) {
456                         bch2_trans_unlock(&trans);
457
458                         ret = bch2_inode_truncate(c, k.k->p.inode, 0);
459                         if (ret)
460                                 goto err;
461                         continue;
462                 }
463
464                 if (fsck_err_on(w.first_this_inode &&
465                         w.have_inode &&
466                         !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
467                         w.inode.bi_sectors !=
468                         (i_sectors = bch2_count_inode_sectors(&trans, w.cur_inum)),
469                         c, "i_sectors wrong: got %llu, should be %llu",
470                         w.inode.bi_sectors, i_sectors)) {
471                         struct bkey_inode_buf p;
472
473                         w.inode.bi_sectors = i_sectors;
474
475                         bch2_trans_unlock(&trans);
476
477                         bch2_inode_pack(&p, &w.inode);
478
479                         ret = bch2_btree_insert(c, BTREE_ID_INODES,
480                                                 &p.inode.k_i, NULL, NULL,
481                                                 BTREE_INSERT_NOFAIL|
482                                                 BTREE_INSERT_LAZY_RW);
483                         if (ret) {
484                                 bch_err(c, "error in fsck: error %i updating inode", ret);
485                                 goto err;
486                         }
487
488                         /* revalidate iterator: */
489                         k = bch2_btree_iter_peek(iter);
490                 }
491
492                 if (fsck_err_on(w.have_inode &&
493                         !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
494                         k.k->type != KEY_TYPE_reservation &&
495                         k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
496                         "extent type %u offset %llu past end of inode %llu, i_size %llu",
497                         k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
498                         bch2_trans_unlock(&trans);
499
500                         ret = bch2_inode_truncate(c, k.k->p.inode,
501                                                   w.inode.bi_size);
502                         if (ret)
503                                 goto err;
504                         continue;
505                 }
506         }
507 err:
508 fsck_err:
509         if (ret == -EINTR)
510                 goto retry;
511         return bch2_trans_exit(&trans) ?: ret;
512 }
513
514 /*
515  * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
516  * validate d_type
517  */
518 noinline_for_stack
519 static int check_dirents(struct bch_fs *c)
520 {
521         struct inode_walker w = inode_walker_init();
522         struct hash_check h;
523         struct btree_trans trans;
524         struct btree_iter *iter;
525         struct bkey_s_c k;
526         unsigned name_len;
527         char buf[200];
528         int ret = 0;
529
530         bch_verbose(c, "checking dirents");
531
532         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
533
534         hash_check_init(&h);
535
536         iter = bch2_trans_get_iter(&trans, BTREE_ID_DIRENTS,
537                                    POS(BCACHEFS_ROOT_INO, 0), 0);
538 retry:
539         for_each_btree_key_continue(iter, 0, k, ret) {
540                 struct bkey_s_c_dirent d;
541                 struct bch_inode_unpacked target;
542                 bool have_target;
543                 u64 d_inum;
544
545                 ret = walk_inode(&trans, &w, k.k->p.inode);
546                 if (ret)
547                         break;
548
549                 if (fsck_err_on(!w.have_inode, c,
550                                 "dirent in nonexisting directory:\n%s",
551                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
552                                                        k), buf)) ||
553                     fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
554                                 "dirent in non directory inode type %u:\n%s",
555                                 mode_to_type(w.inode.bi_mode),
556                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
557                                                        k), buf))) {
558                         ret = bch2_btree_delete_at(&trans, iter, 0);
559                         if (ret)
560                                 goto err;
561                         continue;
562                 }
563
564                 if (w.first_this_inode && w.have_inode)
565                         hash_check_set_inode(&trans, &h, &w.inode);
566
567                 ret = check_dirent_hash(&trans, &h, iter, &k);
568                 if (ret > 0) {
569                         ret = 0;
570                         continue;
571                 }
572                 if (ret)
573                         goto fsck_err;
574
575                 if (ret)
576                         goto fsck_err;
577
578                 if (k.k->type != KEY_TYPE_dirent)
579                         continue;
580
581                 d = bkey_s_c_to_dirent(k);
582                 d_inum = le64_to_cpu(d.v->d_inum);
583
584                 name_len = bch2_dirent_name_bytes(d);
585
586                 if (fsck_err_on(!name_len, c, "empty dirent") ||
587                     fsck_err_on(name_len == 1 &&
588                                 !memcmp(d.v->d_name, ".", 1), c,
589                                 ". dirent") ||
590                     fsck_err_on(name_len == 2 &&
591                                 !memcmp(d.v->d_name, "..", 2), c,
592                                 ".. dirent") ||
593                     fsck_err_on(name_len == 2 &&
594                                 !memcmp(d.v->d_name, "..", 2), c,
595                                 ".. dirent") ||
596                     fsck_err_on(memchr(d.v->d_name, '/', name_len), c,
597                                 "dirent name has invalid chars")) {
598                         ret = remove_dirent(&trans, d);
599                         if (ret)
600                                 goto err;
601                         continue;
602                 }
603
604                 if (fsck_err_on(d_inum == d.k->p.inode, c,
605                                 "dirent points to own directory:\n%s",
606                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
607                                                        k), buf))) {
608                         ret = remove_dirent(&trans, d);
609                         if (ret)
610                                 goto err;
611                         continue;
612                 }
613
614                 ret = bch2_inode_find_by_inum_trans(&trans, d_inum, &target);
615                 if (ret && ret != -ENOENT)
616                         break;
617
618                 have_target = !ret;
619                 ret = 0;
620
621                 if (fsck_err_on(!have_target, c,
622                                 "dirent points to missing inode:\n%s",
623                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
624                                                        k), buf))) {
625                         ret = remove_dirent(&trans, d);
626                         if (ret)
627                                 goto err;
628                         continue;
629                 }
630
631                 if (fsck_err_on(have_target &&
632                                 d.v->d_type !=
633                                 mode_to_type(target.bi_mode), c,
634                                 "incorrect d_type: should be %u:\n%s",
635                                 mode_to_type(target.bi_mode),
636                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
637                                                        k), buf))) {
638                         struct bkey_i_dirent *n;
639
640                         n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
641                         if (!n) {
642                                 ret = -ENOMEM;
643                                 goto err;
644                         }
645
646                         bkey_reassemble(&n->k_i, d.s_c);
647                         n->v.d_type = mode_to_type(target.bi_mode);
648
649                         bch2_trans_update(&trans, iter, &n->k_i);
650
651                         ret = bch2_trans_commit(&trans, NULL, NULL,
652                                                 BTREE_INSERT_NOFAIL|
653                                                 BTREE_INSERT_LAZY_RW);
654                         kfree(n);
655                         if (ret)
656                                 goto err;
657
658                 }
659         }
660
661         hash_stop_chain(&trans, &h);
662 err:
663 fsck_err:
664         if (ret == -EINTR)
665                 goto retry;
666
667         return bch2_trans_exit(&trans) ?: ret;
668 }
669
670 /*
671  * Walk xattrs: verify that they all have a corresponding inode
672  */
673 noinline_for_stack
674 static int check_xattrs(struct bch_fs *c)
675 {
676         struct inode_walker w = inode_walker_init();
677         struct hash_check h;
678         struct btree_trans trans;
679         struct btree_iter *iter;
680         struct bkey_s_c k;
681         int ret = 0;
682
683         bch_verbose(c, "checking xattrs");
684
685         hash_check_init(&h);
686
687         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
688
689         iter = bch2_trans_get_iter(&trans, BTREE_ID_XATTRS,
690                                    POS(BCACHEFS_ROOT_INO, 0), 0);
691 retry:
692         for_each_btree_key_continue(iter, 0, k, ret) {
693                 ret = walk_inode(&trans, &w, k.k->p.inode);
694                 if (ret)
695                         break;
696
697                 if (fsck_err_on(!w.have_inode, c,
698                                 "xattr for missing inode %llu",
699                                 k.k->p.inode)) {
700                         ret = bch2_btree_delete_at(&trans, iter, 0);
701                         if (ret)
702                                 goto err;
703                         continue;
704                 }
705
706                 if (w.first_this_inode && w.have_inode)
707                         hash_check_set_inode(&trans, &h, &w.inode);
708
709                 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
710                                      &h, iter, k);
711                 if (ret)
712                         goto fsck_err;
713         }
714 err:
715 fsck_err:
716         if (ret == -EINTR)
717                 goto retry;
718         return bch2_trans_exit(&trans) ?: ret;
719 }
720
721 /* Get root directory, create if it doesn't exist: */
722 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
723 {
724         struct bkey_inode_buf packed;
725         int ret;
726
727         bch_verbose(c, "checking root directory");
728
729         ret = bch2_inode_find_by_inum(c, BCACHEFS_ROOT_INO, root_inode);
730         if (ret && ret != -ENOENT)
731                 return ret;
732
733         if (fsck_err_on(ret, c, "root directory missing"))
734                 goto create_root;
735
736         if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
737                         "root inode not a directory"))
738                 goto create_root;
739
740         return 0;
741 fsck_err:
742         return ret;
743 create_root:
744         bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
745                         0, NULL);
746         root_inode->bi_inum = BCACHEFS_ROOT_INO;
747
748         bch2_inode_pack(&packed, root_inode);
749
750         return bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
751                                  NULL, NULL,
752                                  BTREE_INSERT_NOFAIL|
753                                  BTREE_INSERT_LAZY_RW);
754 }
755
756 /* Get lost+found, create if it doesn't exist: */
757 static int check_lostfound(struct bch_fs *c,
758                            struct bch_inode_unpacked *root_inode,
759                            struct bch_inode_unpacked *lostfound_inode)
760 {
761         struct qstr lostfound = QSTR("lost+found");
762         struct bch_hash_info root_hash_info =
763                 bch2_hash_info_init(c, root_inode);
764         u64 inum;
765         int ret;
766
767         bch_verbose(c, "checking lost+found");
768
769         inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
770                                  &lostfound);
771         if (!inum) {
772                 bch_notice(c, "creating lost+found");
773                 goto create_lostfound;
774         }
775
776         ret = bch2_inode_find_by_inum(c, inum, lostfound_inode);
777         if (ret && ret != -ENOENT)
778                 return ret;
779
780         if (fsck_err_on(ret, c, "lost+found missing"))
781                 goto create_lostfound;
782
783         if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
784                         "lost+found inode not a directory"))
785                 goto create_lostfound;
786
787         return 0;
788 fsck_err:
789         return ret;
790 create_lostfound:
791         bch2_inode_init_early(c, lostfound_inode);
792
793         ret = bch2_trans_do(c, NULL,
794                             BTREE_INSERT_ATOMIC|
795                             BTREE_INSERT_NOFAIL|
796                             BTREE_INSERT_LAZY_RW,
797                 bch2_create_trans(&trans,
798                                   BCACHEFS_ROOT_INO, root_inode,
799                                   lostfound_inode, &lostfound,
800                                   0, 0, S_IFDIR|0755, 0, NULL, NULL));
801         if (ret)
802                 bch_err(c, "error creating lost+found: %i", ret);
803
804         return ret;
805 }
806
807 struct inode_bitmap {
808         unsigned long   *bits;
809         size_t          size;
810 };
811
812 static inline bool inode_bitmap_test(struct inode_bitmap *b, size_t nr)
813 {
814         return nr < b->size ? test_bit(nr, b->bits) : false;
815 }
816
817 static inline int inode_bitmap_set(struct inode_bitmap *b, size_t nr)
818 {
819         if (nr >= b->size) {
820                 size_t new_size = max_t(size_t, max_t(size_t,
821                                         PAGE_SIZE * 8,
822                                         b->size * 2),
823                                         nr + 1);
824                 void *n;
825
826                 new_size = roundup_pow_of_two(new_size);
827                 n = krealloc(b->bits, new_size / 8, GFP_KERNEL|__GFP_ZERO);
828                 if (!n) {
829                         return -ENOMEM;
830                 }
831
832                 b->bits = n;
833                 b->size = new_size;
834         }
835
836         __set_bit(nr, b->bits);
837         return 0;
838 }
839
840 struct pathbuf {
841         size_t          nr;
842         size_t          size;
843
844         struct pathbuf_entry {
845                 u64     inum;
846                 u64     offset;
847         }               *entries;
848 };
849
850 static int path_down(struct pathbuf *p, u64 inum)
851 {
852         if (p->nr == p->size) {
853                 size_t new_size = max_t(size_t, 256UL, p->size * 2);
854                 void *n = krealloc(p->entries,
855                                    new_size * sizeof(p->entries[0]),
856                                    GFP_KERNEL);
857                 if (!n)
858                         return -ENOMEM;
859
860                 p->entries = n;
861                 p->size = new_size;
862         };
863
864         p->entries[p->nr++] = (struct pathbuf_entry) {
865                 .inum = inum,
866                 .offset = 0,
867         };
868         return 0;
869 }
870
871 noinline_for_stack
872 static int check_directory_structure(struct bch_fs *c,
873                                      struct bch_inode_unpacked *lostfound_inode)
874 {
875         struct inode_bitmap dirs_done = { NULL, 0 };
876         struct pathbuf path = { 0, 0, NULL };
877         struct pathbuf_entry *e;
878         struct btree_trans trans;
879         struct btree_iter *iter;
880         struct bkey_s_c k;
881         struct bkey_s_c_dirent dirent;
882         bool had_unreachable;
883         u64 d_inum;
884         int ret = 0;
885
886         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
887
888         bch_verbose(c, "checking directory structure");
889
890         /* DFS: */
891 restart_dfs:
892         had_unreachable = false;
893
894         ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
895         if (ret) {
896                 bch_err(c, "memory allocation failure in inode_bitmap_set()");
897                 goto err;
898         }
899
900         ret = path_down(&path, BCACHEFS_ROOT_INO);
901         if (ret)
902                 goto err;
903
904         while (path.nr) {
905 next:
906                 e = &path.entries[path.nr - 1];
907
908                 if (e->offset == U64_MAX)
909                         goto up;
910
911                 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
912                                    POS(e->inum, e->offset + 1), 0, k, ret) {
913                         if (k.k->p.inode != e->inum)
914                                 break;
915
916                         e->offset = k.k->p.offset;
917
918                         if (k.k->type != KEY_TYPE_dirent)
919                                 continue;
920
921                         dirent = bkey_s_c_to_dirent(k);
922
923                         if (dirent.v->d_type != DT_DIR)
924                                 continue;
925
926                         d_inum = le64_to_cpu(dirent.v->d_inum);
927
928                         if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
929                                         "directory %llu has multiple hardlinks",
930                                         d_inum)) {
931                                 ret = remove_dirent(&trans, dirent);
932                                 if (ret)
933                                         goto err;
934                                 continue;
935                         }
936
937                         ret = inode_bitmap_set(&dirs_done, d_inum);
938                         if (ret) {
939                                 bch_err(c, "memory allocation failure in inode_bitmap_set()");
940                                 goto err;
941                         }
942
943                         ret = path_down(&path, d_inum);
944                         if (ret) {
945                                 goto err;
946                         }
947
948                         ret = bch2_trans_iter_free(&trans, iter);
949                         if (ret) {
950                                 bch_err(c, "btree error %i in fsck", ret);
951                                 goto err;
952                         }
953                         goto next;
954                 }
955                 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
956                 if (ret) {
957                         bch_err(c, "btree error %i in fsck", ret);
958                         goto err;
959                 }
960 up:
961                 path.nr--;
962         }
963
964         iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS_MIN, 0);
965 retry:
966         for_each_btree_key_continue(iter, 0, k, ret) {
967                 if (k.k->type != KEY_TYPE_inode)
968                         continue;
969
970                 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
971                         continue;
972
973                 ret = bch2_empty_dir_trans(&trans, k.k->p.inode);
974                 if (ret == -EINTR)
975                         goto retry;
976                 if (!ret)
977                         continue;
978
979                 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.inode), c,
980                                 "unreachable directory found (inum %llu)",
981                                 k.k->p.inode)) {
982                         bch2_trans_unlock(&trans);
983
984                         ret = reattach_inode(c, lostfound_inode, k.k->p.inode);
985                         if (ret) {
986                                 goto err;
987                         }
988
989                         had_unreachable = true;
990                 }
991         }
992         bch2_trans_iter_free(&trans, iter);
993         if (ret)
994                 goto err;
995
996         if (had_unreachable) {
997                 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
998                 kfree(dirs_done.bits);
999                 kfree(path.entries);
1000                 memset(&dirs_done, 0, sizeof(dirs_done));
1001                 memset(&path, 0, sizeof(path));
1002                 goto restart_dfs;
1003         }
1004 err:
1005 fsck_err:
1006         ret = bch2_trans_exit(&trans) ?: ret;
1007         kfree(dirs_done.bits);
1008         kfree(path.entries);
1009         return ret;
1010 }
1011
1012 struct nlink {
1013         u32     count;
1014         u32     dir_count;
1015 };
1016
1017 typedef GENRADIX(struct nlink) nlink_table;
1018
1019 static void inc_link(struct bch_fs *c, nlink_table *links,
1020                      u64 range_start, u64 *range_end,
1021                      u64 inum, bool dir)
1022 {
1023         struct nlink *link;
1024
1025         if (inum < range_start || inum >= *range_end)
1026                 return;
1027
1028         link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
1029         if (!link) {
1030                 bch_verbose(c, "allocation failed during fsck - will need another pass");
1031                 *range_end = inum;
1032                 return;
1033         }
1034
1035         if (dir)
1036                 link->dir_count++;
1037         else
1038                 link->count++;
1039 }
1040
1041 noinline_for_stack
1042 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
1043                                u64 range_start, u64 *range_end)
1044 {
1045         struct btree_trans trans;
1046         struct btree_iter *iter;
1047         struct bkey_s_c k;
1048         struct bkey_s_c_dirent d;
1049         u64 d_inum;
1050         int ret;
1051
1052         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1053
1054         inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
1055
1056         for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS, POS_MIN, 0, k, ret) {
1057                 switch (k.k->type) {
1058                 case KEY_TYPE_dirent:
1059                         d = bkey_s_c_to_dirent(k);
1060                         d_inum = le64_to_cpu(d.v->d_inum);
1061
1062                         if (d.v->d_type == DT_DIR)
1063                                 inc_link(c, links, range_start, range_end,
1064                                          d.k->p.inode, true);
1065
1066                         inc_link(c, links, range_start, range_end,
1067                                  d_inum, false);
1068
1069                         break;
1070                 }
1071
1072                 bch2_trans_cond_resched(&trans);
1073         }
1074         ret = bch2_trans_exit(&trans) ?: ret;
1075         if (ret)
1076                 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
1077
1078         return ret;
1079 }
1080
1081 static int check_inode_nlink(struct bch_fs *c,
1082                              struct bch_inode_unpacked *lostfound_inode,
1083                              struct bch_inode_unpacked *u,
1084                              struct nlink *link,
1085                              bool *do_update)
1086 {
1087         u32 i_nlink = bch2_inode_nlink_get(u);
1088         u32 real_i_nlink =
1089                 link->count * nlink_bias(u->bi_mode) +
1090                 link->dir_count;
1091         int ret = 0;
1092
1093         /*
1094          * These should have been caught/fixed by earlier passes, we don't
1095          * repair them here:
1096          */
1097         if (S_ISDIR(u->bi_mode) && link->count > 1) {
1098                 need_fsck_err(c, "directory %llu with multiple hardlinks: %u",
1099                               u->bi_inum, link->count);
1100                 return 0;
1101         }
1102
1103         if (S_ISDIR(u->bi_mode) && !link->count) {
1104                 need_fsck_err(c, "unreachable directory found (inum %llu)",
1105                               u->bi_inum);
1106                 return 0;
1107         }
1108
1109         if (!S_ISDIR(u->bi_mode) && link->dir_count) {
1110                 need_fsck_err(c, "non directory with subdirectories",
1111                               u->bi_inum);
1112                 return 0;
1113         }
1114
1115         if (!link->count &&
1116             !(u->bi_flags & BCH_INODE_UNLINKED) &&
1117             (c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
1118                 if (fsck_err(c, "unreachable inode %llu not marked as unlinked (type %u)",
1119                              u->bi_inum, mode_to_type(u->bi_mode)) ==
1120                     FSCK_ERR_IGNORE)
1121                         return 0;
1122
1123                 ret = reattach_inode(c, lostfound_inode, u->bi_inum);
1124                 if (ret)
1125                         return ret;
1126
1127                 link->count = 1;
1128                 real_i_nlink = nlink_bias(u->bi_mode) + link->dir_count;
1129                 goto set_i_nlink;
1130         }
1131
1132         if (i_nlink < link->count) {
1133                 if (fsck_err(c, "inode %llu i_link too small (%u < %u, type %i)",
1134                              u->bi_inum, i_nlink, link->count,
1135                              mode_to_type(u->bi_mode)) == FSCK_ERR_IGNORE)
1136                         return 0;
1137                 goto set_i_nlink;
1138         }
1139
1140         if (i_nlink != real_i_nlink &&
1141             c->sb.clean) {
1142                 if (fsck_err(c, "filesystem marked clean, "
1143                              "but inode %llu has wrong i_nlink "
1144                              "(type %u i_nlink %u, should be %u)",
1145                              u->bi_inum, mode_to_type(u->bi_mode),
1146                              i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1147                         return 0;
1148                 goto set_i_nlink;
1149         }
1150
1151         if (i_nlink != real_i_nlink &&
1152             (c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
1153                 if (fsck_err(c, "inode %llu has wrong i_nlink "
1154                              "(type %u i_nlink %u, should be %u)",
1155                              u->bi_inum, mode_to_type(u->bi_mode),
1156                              i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1157                         return 0;
1158                 goto set_i_nlink;
1159         }
1160
1161         if (real_i_nlink && i_nlink != real_i_nlink)
1162                 bch_verbose(c, "setting inode %llu nlink from %u to %u",
1163                             u->bi_inum, i_nlink, real_i_nlink);
1164 set_i_nlink:
1165         if (i_nlink != real_i_nlink) {
1166                 bch2_inode_nlink_set(u, real_i_nlink);
1167                 *do_update = true;
1168         }
1169 fsck_err:
1170         return ret;
1171 }
1172
1173 static int check_inode(struct btree_trans *trans,
1174                        struct bch_inode_unpacked *lostfound_inode,
1175                        struct btree_iter *iter,
1176                        struct bkey_s_c_inode inode,
1177                        struct nlink *link)
1178 {
1179         struct bch_fs *c = trans->c;
1180         struct bch_inode_unpacked u;
1181         bool do_update = false;
1182         int ret = 0;
1183
1184         ret = bch2_inode_unpack(inode, &u);
1185
1186         bch2_trans_unlock(trans);
1187
1188         if (bch2_fs_inconsistent_on(ret, c,
1189                          "error unpacking inode %llu in fsck",
1190                          inode.k->p.inode))
1191                 return ret;
1192
1193         if (link) {
1194                 ret = check_inode_nlink(c, lostfound_inode, &u, link,
1195                                         &do_update);
1196                 if (ret)
1197                         return ret;
1198         }
1199
1200         if (u.bi_flags & BCH_INODE_UNLINKED &&
1201             (!c->sb.clean ||
1202              fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
1203                       u.bi_inum))) {
1204                 bch_verbose(c, "deleting inode %llu", u.bi_inum);
1205
1206                 ret = bch2_inode_rm(c, u.bi_inum);
1207                 if (ret)
1208                         bch_err(c, "error in fsck: error %i while deleting inode", ret);
1209                 return ret;
1210         }
1211
1212         if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
1213             (!c->sb.clean ||
1214              fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
1215                       u.bi_inum))) {
1216                 bch_verbose(c, "truncating inode %llu", u.bi_inum);
1217
1218                 /*
1219                  * XXX: need to truncate partial blocks too here - or ideally
1220                  * just switch units to bytes and that issue goes away
1221                  */
1222
1223                 ret = bch2_inode_truncate(c, u.bi_inum, u.bi_size);
1224                 if (ret) {
1225                         bch_err(c, "error in fsck: error %i truncating inode", ret);
1226                         return ret;
1227                 }
1228
1229                 /*
1230                  * We truncated without our normal sector accounting hook, just
1231                  * make sure we recalculate it:
1232                  */
1233                 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
1234
1235                 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
1236                 do_update = true;
1237         }
1238
1239         if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
1240             (!c->sb.clean ||
1241              fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
1242                       u.bi_inum))) {
1243                 s64 sectors;
1244
1245                 bch_verbose(c, "recounting sectors for inode %llu",
1246                             u.bi_inum);
1247
1248                 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
1249                 if (sectors < 0) {
1250                         bch_err(c, "error in fsck: error %i recounting inode sectors",
1251                                 (int) sectors);
1252                         return sectors;
1253                 }
1254
1255                 u.bi_sectors = sectors;
1256                 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
1257                 do_update = true;
1258         }
1259
1260         if (do_update) {
1261                 struct bkey_inode_buf p;
1262
1263                 bch2_inode_pack(&p, &u);
1264                 bch2_trans_update(trans, iter, &p.inode.k_i);
1265
1266                 ret = bch2_trans_commit(trans, NULL, NULL,
1267                                         BTREE_INSERT_NOFAIL|
1268                                         BTREE_INSERT_LAZY_RW);
1269                 if (ret && ret != -EINTR)
1270                         bch_err(c, "error in fsck: error %i "
1271                                 "updating inode", ret);
1272         }
1273 fsck_err:
1274         return ret;
1275 }
1276
1277 noinline_for_stack
1278 static int bch2_gc_walk_inodes(struct bch_fs *c,
1279                                struct bch_inode_unpacked *lostfound_inode,
1280                                nlink_table *links,
1281                                u64 range_start, u64 range_end)
1282 {
1283         struct btree_trans trans;
1284         struct btree_iter *iter;
1285         struct bkey_s_c k;
1286         struct nlink *link, zero_links = { 0, 0 };
1287         struct genradix_iter nlinks_iter;
1288         int ret = 0, ret2 = 0;
1289         u64 nlinks_pos;
1290
1291         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1292
1293         iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES,
1294                                    POS(range_start, 0), 0);
1295         nlinks_iter = genradix_iter_init(links, 0);
1296
1297         while ((k = bch2_btree_iter_peek(iter)).k &&
1298                !(ret2 = bkey_err(k))) {
1299 peek_nlinks:    link = genradix_iter_peek(&nlinks_iter, links);
1300
1301                 if (!link && (!k.k || iter->pos.inode >= range_end))
1302                         break;
1303
1304                 nlinks_pos = range_start + nlinks_iter.pos;
1305                 if (iter->pos.inode > nlinks_pos) {
1306                         /* Should have been caught by dirents pass: */
1307                         need_fsck_err_on(link && link->count, c,
1308                                 "missing inode %llu (nlink %u)",
1309                                 nlinks_pos, link->count);
1310                         genradix_iter_advance(&nlinks_iter, links);
1311                         goto peek_nlinks;
1312                 }
1313
1314                 if (iter->pos.inode < nlinks_pos || !link)
1315                         link = &zero_links;
1316
1317                 if (k.k && k.k->type == KEY_TYPE_inode) {
1318                         ret = check_inode(&trans, lostfound_inode, iter,
1319                                           bkey_s_c_to_inode(k), link);
1320                         BUG_ON(ret == -EINTR);
1321                         if (ret)
1322                                 break;
1323                 } else {
1324                         /* Should have been caught by dirents pass: */
1325                         need_fsck_err_on(link->count, c,
1326                                 "missing inode %llu (nlink %u)",
1327                                 nlinks_pos, link->count);
1328                 }
1329
1330                 if (nlinks_pos == iter->pos.inode)
1331                         genradix_iter_advance(&nlinks_iter, links);
1332
1333                 bch2_btree_iter_next(iter);
1334                 bch2_trans_cond_resched(&trans);
1335         }
1336 fsck_err:
1337         bch2_trans_exit(&trans);
1338
1339         if (ret2)
1340                 bch_err(c, "error in fsck: btree error %i while walking inodes", ret2);
1341
1342         return ret ?: ret2;
1343 }
1344
1345 noinline_for_stack
1346 static int check_inode_nlinks(struct bch_fs *c,
1347                               struct bch_inode_unpacked *lostfound_inode)
1348 {
1349         nlink_table links;
1350         u64 this_iter_range_start, next_iter_range_start = 0;
1351         int ret = 0;
1352
1353         bch_verbose(c, "checking inode nlinks");
1354
1355         genradix_init(&links);
1356
1357         do {
1358                 this_iter_range_start = next_iter_range_start;
1359                 next_iter_range_start = U64_MAX;
1360
1361                 ret = bch2_gc_walk_dirents(c, &links,
1362                                           this_iter_range_start,
1363                                           &next_iter_range_start);
1364                 if (ret)
1365                         break;
1366
1367                 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1368                                          this_iter_range_start,
1369                                          next_iter_range_start);
1370                 if (ret)
1371                         break;
1372
1373                 genradix_free(&links);
1374         } while (next_iter_range_start != U64_MAX);
1375
1376         genradix_free(&links);
1377
1378         return ret;
1379 }
1380
1381 /*
1382  * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1383  * Doesn't fix them yet, mainly because they haven't yet been observed:
1384  */
1385 int bch2_fsck_full(struct bch_fs *c)
1386 {
1387         struct bch_inode_unpacked root_inode, lostfound_inode;
1388
1389         return  check_extents(c) ?:
1390                 check_dirents(c) ?:
1391                 check_xattrs(c) ?:
1392                 check_root(c, &root_inode) ?:
1393                 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1394                 check_directory_structure(c, &lostfound_inode) ?:
1395                 check_inode_nlinks(c, &lostfound_inode);
1396 }
1397
1398 int bch2_fsck_inode_nlink(struct bch_fs *c)
1399 {
1400         struct bch_inode_unpacked root_inode, lostfound_inode;
1401
1402         return  check_root(c, &root_inode) ?:
1403                 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1404                 check_inode_nlinks(c, &lostfound_inode);
1405 }
1406
1407 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1408 {
1409         struct btree_trans trans;
1410         struct btree_iter *iter;
1411         struct bkey_s_c k;
1412         struct bkey_s_c_inode inode;
1413         int ret;
1414
1415         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1416
1417         for_each_btree_key(&trans, iter, BTREE_ID_INODES, POS_MIN, 0, k, ret) {
1418                 if (k.k->type != KEY_TYPE_inode)
1419                         continue;
1420
1421                 inode = bkey_s_c_to_inode(k);
1422
1423                 if (inode.v->bi_flags &
1424                     (BCH_INODE_I_SIZE_DIRTY|
1425                      BCH_INODE_I_SECTORS_DIRTY|
1426                      BCH_INODE_UNLINKED)) {
1427                         ret = check_inode(&trans, NULL, iter, inode, NULL);
1428                         BUG_ON(ret == -EINTR);
1429                         if (ret)
1430                                 break;
1431                 }
1432         }
1433         BUG_ON(ret == -EINTR);
1434
1435         return bch2_trans_exit(&trans) ?: ret;
1436 }