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