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