]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fsck.c
Update bcachefs sources to ae6f512de8 bcachefs: Fix out of bounds read in fs usage...
[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         bch2_trans_update(trans, k_iter, delete, 0);
309
310         return bch2_hash_set(trans, desc, hash_info, k_iter->pos.inode, tmp, 0);
311 }
312
313 static int fsck_hash_delete_at(struct btree_trans *trans,
314                                const struct bch_hash_desc desc,
315                                struct bch_hash_info *info,
316                                struct btree_iter *iter)
317 {
318         int ret;
319 retry:
320         ret   = bch2_hash_delete_at(trans, desc, info, iter) ?:
321                 bch2_trans_commit(trans, NULL, NULL,
322                                   BTREE_INSERT_NOFAIL|
323                                   BTREE_INSERT_LAZY_RW);
324         if (ret == -EINTR) {
325                 ret = bch2_btree_iter_traverse(iter);
326                 if (!ret)
327                         goto retry;
328         }
329
330         return ret;
331 }
332
333 static int hash_check_key(struct btree_trans *trans,
334                           const struct bch_hash_desc desc,
335                           struct bch_hash_info *hash_info,
336                           struct btree_iter *k_iter, struct bkey_s_c hash_k)
337 {
338         struct bch_fs *c = trans->c;
339         struct btree_iter *iter = NULL;
340         char buf[200];
341         struct bkey_s_c k;
342         u64 hash;
343         int ret = 0;
344
345         if (hash_k.k->type != desc.key_type)
346                 return 0;
347
348         hash = desc.hash_bkey(hash_info, hash_k);
349
350         if (likely(hash == hash_k.k->p.offset))
351                 return 0;
352
353         if (hash_k.k->p.offset < hash)
354                 goto bad_hash;
355
356         for_each_btree_key(trans, iter, desc.btree_id, POS(hash_k.k->p.inode, hash),
357                            BTREE_ITER_SLOTS, k, ret) {
358                 if (!bkey_cmp(k.k->p, hash_k.k->p))
359                         break;
360
361                 if (fsck_err_on(k.k->type == desc.key_type &&
362                                 !desc.cmp_bkey(k, hash_k), c,
363                                 "duplicate hash table keys:\n%s",
364                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
365                                                        hash_k), buf))) {
366                         ret = fsck_hash_delete_at(trans, desc, hash_info, k_iter);
367                         if (ret)
368                                 return ret;
369                         ret = 1;
370                         break;
371                 }
372
373                 if (bkey_deleted(k.k)) {
374                         bch2_trans_iter_free(trans, iter);
375                         goto bad_hash;
376                 }
377
378         }
379         bch2_trans_iter_free(trans, iter);
380         return ret;
381 bad_hash:
382         if (fsck_err(c, "hash table key at wrong offset: btree %u inode %llu offset %llu, "
383                      "hashed to %llu\n%s",
384                      desc.btree_id, hash_k.k->p.inode, hash_k.k->p.offset, hash,
385                      (bch2_bkey_val_to_text(&PBUF(buf), c, hash_k), buf)) == FSCK_ERR_IGNORE)
386                 return 0;
387
388         ret = __bch2_trans_do(trans, NULL, NULL,
389                               BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
390                 hash_redo_key(trans, desc, hash_info, k_iter, hash_k));
391         if (ret) {
392                 bch_err(c, "hash_redo_key err %i", ret);
393                 return ret;
394         }
395         return -EINTR;
396 fsck_err:
397         return ret;
398 }
399
400 static int check_inode(struct btree_trans *trans,
401                        struct btree_iter *iter,
402                        struct bkey_s_c_inode inode)
403 {
404         struct bch_fs *c = trans->c;
405         struct bch_inode_unpacked u;
406         bool do_update = false;
407         int ret = 0;
408
409         ret = bch2_inode_unpack(inode, &u);
410
411         if (bch2_fs_inconsistent_on(ret, c,
412                          "error unpacking inode %llu in fsck",
413                          inode.k->p.inode))
414                 return ret;
415
416         if (u.bi_flags & BCH_INODE_UNLINKED &&
417             (!c->sb.clean ||
418              fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
419                       u.bi_inum))) {
420                 bch_verbose(c, "deleting inode %llu", u.bi_inum);
421
422                 bch2_trans_unlock(trans);
423                 bch2_fs_lazy_rw(c);
424
425                 ret = bch2_inode_rm(c, u.bi_inum, false);
426                 if (ret)
427                         bch_err(c, "error in fsck: error %i while deleting inode", ret);
428                 return ret;
429         }
430
431         if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
432             (!c->sb.clean ||
433              fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
434                       u.bi_inum))) {
435                 bch_verbose(c, "truncating inode %llu", u.bi_inum);
436
437                 bch2_trans_unlock(trans);
438                 bch2_fs_lazy_rw(c);
439
440                 /*
441                  * XXX: need to truncate partial blocks too here - or ideally
442                  * just switch units to bytes and that issue goes away
443                  */
444                 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
445                                 POS(u.bi_inum, round_up(u.bi_size, block_bytes(c))),
446                                 POS(u.bi_inum, U64_MAX),
447                                 NULL);
448                 if (ret) {
449                         bch_err(c, "error in fsck: error %i truncating inode", ret);
450                         return ret;
451                 }
452
453                 /*
454                  * We truncated without our normal sector accounting hook, just
455                  * make sure we recalculate it:
456                  */
457                 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
458
459                 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
460                 do_update = true;
461         }
462
463         if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
464             (!c->sb.clean ||
465              fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
466                       u.bi_inum))) {
467                 s64 sectors;
468
469                 bch_verbose(c, "recounting sectors for inode %llu",
470                             u.bi_inum);
471
472                 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
473                 if (sectors < 0) {
474                         bch_err(c, "error in fsck: error %i recounting inode sectors",
475                                 (int) sectors);
476                         return sectors;
477                 }
478
479                 u.bi_sectors = sectors;
480                 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
481                 do_update = true;
482         }
483
484         if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
485                 u.bi_dir = 0;
486                 u.bi_dir_offset = 0;
487                 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
488                 do_update = true;
489         }
490
491         if (do_update) {
492                 ret = __bch2_trans_do(trans, NULL, NULL,
493                                       BTREE_INSERT_NOFAIL|
494                                       BTREE_INSERT_LAZY_RW,
495                                 bch2_inode_write(trans, iter, &u));
496                 if (ret)
497                         bch_err(c, "error in fsck: error %i "
498                                 "updating inode", ret);
499         }
500 fsck_err:
501         return ret;
502 }
503
504 noinline_for_stack
505 static int check_inodes(struct bch_fs *c, bool full)
506 {
507         struct btree_trans trans;
508         struct btree_iter *iter;
509         struct bkey_s_c k;
510         struct bkey_s_c_inode inode;
511         int ret;
512
513         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
514
515         for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
516                            BTREE_ITER_INTENT|
517                            BTREE_ITER_PREFETCH, k, ret) {
518                 if (k.k->type != KEY_TYPE_inode)
519                         continue;
520
521                 inode = bkey_s_c_to_inode(k);
522
523                 if (full ||
524                     (inode.v->bi_flags & (BCH_INODE_I_SIZE_DIRTY|
525                                           BCH_INODE_I_SECTORS_DIRTY|
526                                           BCH_INODE_UNLINKED))) {
527                         ret = check_inode(&trans, iter, inode);
528                         if (ret)
529                                 break;
530                 }
531         }
532         bch2_trans_iter_put(&trans, iter);
533
534         BUG_ON(ret == -EINTR);
535
536         return bch2_trans_exit(&trans) ?: ret;
537 }
538
539 static int fix_overlapping_extent(struct btree_trans *trans,
540                                        struct bkey_s_c k, struct bpos cut_at)
541 {
542         struct btree_iter *iter;
543         struct bkey_i *u;
544         int ret;
545
546         u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
547         ret = PTR_ERR_OR_ZERO(u);
548         if (ret)
549                 return ret;
550
551         bkey_reassemble(u, k);
552         bch2_cut_front(cut_at, u);
553
554
555         /*
556          * We don't want to go through the extent_handle_overwrites path:
557          *
558          * XXX: this is going to screw up disk accounting, extent triggers
559          * assume things about extent overwrites - we should be running the
560          * triggers manually here
561          */
562         iter = bch2_trans_get_iter(trans, BTREE_ID_extents, u->k.p,
563                                    BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
564
565         BUG_ON(iter->flags & BTREE_ITER_IS_EXTENTS);
566         bch2_trans_update(trans, iter, u, BTREE_TRIGGER_NORUN);
567         bch2_trans_iter_put(trans, iter);
568
569         return bch2_trans_commit(trans, NULL, NULL,
570                                  BTREE_INSERT_NOFAIL|
571                                  BTREE_INSERT_LAZY_RW);
572 }
573
574 static int inode_backpointer_exists(struct btree_trans *trans,
575                                     struct bch_inode_unpacked *inode)
576 {
577         struct btree_iter *iter;
578         struct bkey_s_c k;
579         int ret;
580
581         iter = bch2_trans_get_iter(trans, BTREE_ID_dirents,
582                                    POS(inode->bi_dir, inode->bi_dir_offset), 0);
583         k = bch2_btree_iter_peek_slot(iter);
584         ret = bkey_err(k);
585         if (ret)
586                 goto out;
587         if (k.k->type != KEY_TYPE_dirent)
588                 goto out;
589
590         ret = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum) == inode->bi_inum;
591 out:
592         bch2_trans_iter_free(trans, iter);
593         return ret;
594 }
595
596 static bool inode_backpointer_matches(struct bkey_s_c_dirent d,
597                                       struct bch_inode_unpacked *inode)
598 {
599         return d.k->p.inode == inode->bi_dir &&
600                 d.k->p.offset == inode->bi_dir_offset;
601 }
602
603 /*
604  * Walk extents: verify that extents have a corresponding S_ISREG inode, and
605  * that i_size an i_sectors are consistent
606  */
607 noinline_for_stack
608 static int check_extents(struct bch_fs *c)
609 {
610         struct inode_walker w = inode_walker_init();
611         struct btree_trans trans;
612         struct btree_iter *iter;
613         struct bkey_s_c k;
614         struct bkey_buf prev;
615         u64 i_sectors = 0;
616         int ret = 0;
617
618         bch2_bkey_buf_init(&prev);
619         prev.k->k = KEY(0, 0, 0);
620         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
621
622         bch_verbose(c, "checking extents");
623
624         iter = bch2_trans_get_iter(&trans, BTREE_ID_extents,
625                                    POS(BCACHEFS_ROOT_INO, 0),
626                                    BTREE_ITER_INTENT|
627                                    BTREE_ITER_PREFETCH);
628 retry:
629         while ((k = bch2_btree_iter_peek(iter)).k &&
630                !(ret = bkey_err(k))) {
631                 if (w.have_inode &&
632                     w.cur_inum != k.k->p.inode &&
633                     !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
634                     fsck_err_on(w.inode.bi_sectors != i_sectors, c,
635                                 "inode %llu has incorrect i_sectors: got %llu, should be %llu",
636                                 w.inode.bi_inum,
637                                 w.inode.bi_sectors, i_sectors)) {
638                         w.inode.bi_sectors = i_sectors;
639
640                         ret = write_inode(&trans, &w.inode, w.snapshot);
641                         if (ret)
642                                 break;
643                 }
644
645                 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
646                         char buf1[200];
647                         char buf2[200];
648
649                         bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
650                         bch2_bkey_val_to_text(&PBUF(buf2), c, k);
651
652                         if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2))
653                                 return fix_overlapping_extent(&trans, k, prev.k->k.p) ?: -EINTR;
654                 }
655
656                 ret = walk_inode(&trans, &w, k.k->p.inode);
657                 if (ret)
658                         break;
659
660                 if (w.first_this_inode)
661                         i_sectors = 0;
662
663                 if (fsck_err_on(!w.have_inode, c,
664                                 "extent type %u for missing inode %llu",
665                                 k.k->type, k.k->p.inode) ||
666                     fsck_err_on(w.have_inode &&
667                                 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
668                                 "extent type %u for non regular file, inode %llu mode %o",
669                                 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
670                         bch2_fs_lazy_rw(c);
671                         return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
672                                                        POS(k.k->p.inode, 0),
673                                                        POS(k.k->p.inode, U64_MAX),
674                                                        NULL) ?: -EINTR;
675                 }
676
677                 if (fsck_err_on(w.have_inode &&
678                                 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
679                                 k.k->type != KEY_TYPE_reservation &&
680                                 k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
681                                 "extent type %u offset %llu past end of inode %llu, i_size %llu",
682                                 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
683                         bch2_fs_lazy_rw(c);
684                         return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
685                                         POS(k.k->p.inode, round_up(w.inode.bi_size, block_bytes(c))),
686                                         POS(k.k->p.inode, U64_MAX),
687                                         NULL) ?: -EINTR;
688                 }
689
690                 if (bkey_extent_is_allocation(k.k))
691                         i_sectors += k.k->size;
692                 bch2_bkey_buf_reassemble(&prev, c, k);
693
694                 bch2_btree_iter_advance(iter);
695         }
696 fsck_err:
697         if (ret == -EINTR)
698                 goto retry;
699         bch2_trans_iter_put(&trans, iter);
700         bch2_bkey_buf_exit(&prev, c);
701         return bch2_trans_exit(&trans) ?: ret;
702 }
703
704 /*
705  * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
706  * validate d_type
707  */
708 noinline_for_stack
709 static int check_dirents(struct bch_fs *c)
710 {
711         struct inode_walker w = inode_walker_init();
712         struct bch_hash_info hash_info;
713         struct btree_trans trans;
714         struct btree_iter *iter;
715         struct bkey_s_c k;
716         char buf[200];
717         unsigned nr_subdirs = 0;
718         int ret = 0;
719
720         bch_verbose(c, "checking dirents");
721
722         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
723
724         iter = bch2_trans_get_iter(&trans, BTREE_ID_dirents,
725                                    POS(BCACHEFS_ROOT_INO, 0),
726                                    BTREE_ITER_INTENT|
727                                    BTREE_ITER_PREFETCH);
728 retry:
729         while ((k = bch2_btree_iter_peek(iter)).k &&
730                !(ret = bkey_err(k))) {
731                 struct bkey_s_c_dirent d;
732                 struct bch_inode_unpacked target;
733                 u32 target_snapshot;
734                 bool have_target;
735                 bool backpointer_exists = true;
736                 u64 d_inum;
737
738                 if (w.have_inode &&
739                     w.cur_inum != k.k->p.inode &&
740                     fsck_err_on(w.inode.bi_nlink != nr_subdirs, c,
741                                 "directory %llu with wrong i_nlink: got %u, should be %u",
742                                 w.inode.bi_inum, w.inode.bi_nlink, nr_subdirs)) {
743                         w.inode.bi_nlink = nr_subdirs;
744                         ret = write_inode(&trans, &w.inode, w.snapshot);
745                         if (ret)
746                                 break;
747                 }
748
749                 ret = walk_inode(&trans, &w, k.k->p.inode);
750                 if (ret)
751                         break;
752
753                 if (w.first_this_inode)
754                         nr_subdirs = 0;
755
756                 if (fsck_err_on(!w.have_inode, c,
757                                 "dirent in nonexisting directory:\n%s",
758                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
759                                                        k), buf)) ||
760                     fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
761                                 "dirent in non directory inode type %u:\n%s",
762                                 mode_to_type(w.inode.bi_mode),
763                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
764                                                        k), buf))) {
765                         ret = lockrestart_do(&trans,
766                                         bch2_btree_delete_at(&trans, iter, 0));
767                         if (ret)
768                                 goto err;
769                         goto next;
770                 }
771
772                 if (!w.have_inode)
773                         goto next;
774
775                 if (w.first_this_inode)
776                         hash_info = bch2_hash_info_init(c, &w.inode);
777
778                 ret = hash_check_key(&trans, bch2_dirent_hash_desc,
779                                      &hash_info, iter, k);
780                 if (ret > 0) {
781                         ret = 0;
782                         goto next;
783                 }
784                 if (ret)
785                         goto fsck_err;
786
787                 if (k.k->type != KEY_TYPE_dirent)
788                         goto next;
789
790                 d = bkey_s_c_to_dirent(k);
791                 d_inum = le64_to_cpu(d.v->d_inum);
792
793                 ret = lookup_inode(&trans, d_inum, &target, &target_snapshot);
794                 if (ret && ret != -ENOENT)
795                         break;
796
797                 have_target = !ret;
798                 ret = 0;
799
800                 if (fsck_err_on(!have_target, c,
801                                 "dirent points to missing inode:\n%s",
802                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
803                                                        k), buf))) {
804                         ret = remove_dirent(&trans, d.k->p);
805                         if (ret)
806                                 goto err;
807                         goto next;
808                 }
809
810                 if (!have_target)
811                         goto next;
812
813                 if (!target.bi_dir &&
814                     !target.bi_dir_offset) {
815                         target.bi_dir           = k.k->p.inode;
816                         target.bi_dir_offset    = k.k->p.offset;
817
818                         ret = write_inode(&trans, &target, target_snapshot);
819                         if (ret)
820                                 goto err;
821                 }
822
823                 if (!inode_backpointer_matches(d, &target)) {
824                         ret = inode_backpointer_exists(&trans, &target);
825                         if (ret < 0)
826                                 goto err;
827
828                         backpointer_exists = ret;
829                         ret = 0;
830
831                         if (fsck_err_on(S_ISDIR(target.bi_mode) &&
832                                         backpointer_exists, c,
833                                         "directory %llu with multiple links",
834                                         target.bi_inum)) {
835                                 ret = remove_dirent(&trans, d.k->p);
836                                 if (ret)
837                                         goto err;
838                                 continue;
839                         }
840
841                         if (fsck_err_on(backpointer_exists &&
842                                         !target.bi_nlink, c,
843                                         "inode %llu has multiple links but i_nlink 0",
844                                         d_inum)) {
845                                 target.bi_nlink++;
846                                 target.bi_flags &= ~BCH_INODE_UNLINKED;
847
848                                 ret = write_inode(&trans, &target, target_snapshot);
849                                 if (ret)
850                                         goto err;
851                         }
852
853                         if (fsck_err_on(!backpointer_exists, c,
854                                         "inode %llu has wrong backpointer:\n"
855                                         "got       %llu:%llu\n"
856                                         "should be %llu:%llu",
857                                         d_inum,
858                                         target.bi_dir,
859                                         target.bi_dir_offset,
860                                         k.k->p.inode,
861                                         k.k->p.offset)) {
862                                 target.bi_dir           = k.k->p.inode;
863                                 target.bi_dir_offset    = k.k->p.offset;
864
865                                 ret = write_inode(&trans, &target, target_snapshot);
866                                 if (ret)
867                                         goto err;
868                         }
869                 }
870
871                 if (fsck_err_on(d.v->d_type != mode_to_type(target.bi_mode), c,
872                                 "incorrect d_type: should be %u:\n%s",
873                                 mode_to_type(target.bi_mode),
874                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
875                                                        k), buf))) {
876                         struct bkey_i_dirent *n;
877
878                         n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
879                         if (!n) {
880                                 ret = -ENOMEM;
881                                 goto err;
882                         }
883
884                         bkey_reassemble(&n->k_i, d.s_c);
885                         n->v.d_type = mode_to_type(target.bi_mode);
886
887                         ret = __bch2_trans_do(&trans, NULL, NULL,
888                                               BTREE_INSERT_NOFAIL|
889                                               BTREE_INSERT_LAZY_RW,
890                                 (bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
891                         kfree(n);
892                         if (ret)
893                                 goto err;
894
895                 }
896
897                 nr_subdirs += d.v->d_type == DT_DIR;
898 next:
899                 bch2_btree_iter_advance(iter);
900         }
901 err:
902 fsck_err:
903         if (ret == -EINTR)
904                 goto retry;
905
906         bch2_trans_iter_put(&trans, iter);
907         return bch2_trans_exit(&trans) ?: ret;
908 }
909
910 /*
911  * Walk xattrs: verify that they all have a corresponding inode
912  */
913 noinline_for_stack
914 static int check_xattrs(struct bch_fs *c)
915 {
916         struct inode_walker w = inode_walker_init();
917         struct bch_hash_info hash_info;
918         struct btree_trans trans;
919         struct btree_iter *iter;
920         struct bkey_s_c k;
921         int ret = 0;
922
923         bch_verbose(c, "checking xattrs");
924
925         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
926
927         iter = bch2_trans_get_iter(&trans, BTREE_ID_xattrs,
928                                    POS(BCACHEFS_ROOT_INO, 0),
929                                    BTREE_ITER_INTENT|
930                                    BTREE_ITER_PREFETCH);
931 retry:
932         while ((k = bch2_btree_iter_peek(iter)).k &&
933                !(ret = bkey_err(k))) {
934                 ret = walk_inode(&trans, &w, k.k->p.inode);
935                 if (ret)
936                         break;
937
938                 if (fsck_err_on(!w.have_inode, c,
939                                 "xattr for missing inode %llu",
940                                 k.k->p.inode)) {
941                         ret = bch2_btree_delete_at(&trans, iter, 0);
942                         if (ret)
943                                 break;
944                         continue;
945                 }
946
947                 if (w.first_this_inode && w.have_inode)
948                         hash_info = bch2_hash_info_init(c, &w.inode);
949
950                 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
951                                      &hash_info, iter, k);
952                 if (ret)
953                         break;
954
955                 bch2_btree_iter_advance(iter);
956         }
957 fsck_err:
958         if (ret == -EINTR)
959                 goto retry;
960
961         bch2_trans_iter_put(&trans, iter);
962         return bch2_trans_exit(&trans) ?: ret;
963 }
964
965 /* Get root directory, create if it doesn't exist: */
966 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
967 {
968         struct bkey_inode_buf packed;
969         u32 snapshot;
970         int ret;
971
972         bch_verbose(c, "checking root directory");
973
974         ret = bch2_trans_do(c, NULL, NULL, 0,
975                 lookup_inode(&trans, BCACHEFS_ROOT_INO, root_inode, &snapshot));
976         if (ret && ret != -ENOENT)
977                 return ret;
978
979         if (fsck_err_on(ret, c, "root directory missing"))
980                 goto create_root;
981
982         if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
983                         "root inode not a directory"))
984                 goto create_root;
985
986         return 0;
987 fsck_err:
988         return ret;
989 create_root:
990         bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
991                         0, NULL);
992         root_inode->bi_inum = BCACHEFS_ROOT_INO;
993
994         bch2_inode_pack(c, &packed, root_inode);
995
996         return bch2_btree_insert(c, BTREE_ID_inodes, &packed.inode.k_i,
997                                  NULL, NULL,
998                                  BTREE_INSERT_NOFAIL|
999                                  BTREE_INSERT_LAZY_RW);
1000 }
1001
1002 struct pathbuf {
1003         size_t          nr;
1004         size_t          size;
1005
1006         struct pathbuf_entry {
1007                 u64     inum;
1008         }               *entries;
1009 };
1010
1011 static int path_down(struct pathbuf *p, u64 inum)
1012 {
1013         if (p->nr == p->size) {
1014                 size_t new_size = max_t(size_t, 256UL, p->size * 2);
1015                 void *n = krealloc(p->entries,
1016                                    new_size * sizeof(p->entries[0]),
1017                                    GFP_KERNEL);
1018                 if (!n) {
1019                         return -ENOMEM;
1020                 }
1021
1022                 p->entries = n;
1023                 p->size = new_size;
1024         };
1025
1026         p->entries[p->nr++] = (struct pathbuf_entry) {
1027                 .inum = inum,
1028         };
1029         return 0;
1030 }
1031
1032 static int check_path(struct btree_trans *trans,
1033                       struct pathbuf *p,
1034                       struct bch_inode_unpacked *inode)
1035 {
1036         struct bch_fs *c = trans->c;
1037         u32 snapshot;
1038         size_t i;
1039         int ret = 0;
1040
1041         p->nr = 0;
1042
1043         while (inode->bi_inum != BCACHEFS_ROOT_INO) {
1044                 ret = lockrestart_do(trans,
1045                         inode_backpointer_exists(trans, inode));
1046                 if (ret < 0)
1047                         break;
1048
1049                 if (!ret) {
1050                         if (fsck_err(c,  "unreachable inode %llu, type %u nlink %u backptr %llu:%llu",
1051                                      inode->bi_inum,
1052                                      mode_to_type(inode->bi_mode),
1053                                      inode->bi_nlink,
1054                                      inode->bi_dir,
1055                                      inode->bi_dir_offset))
1056                                 ret = reattach_inode(trans, inode);
1057                         break;
1058                 }
1059                 ret = 0;
1060
1061                 if (!S_ISDIR(inode->bi_mode))
1062                         break;
1063
1064                 ret = path_down(p, inode->bi_inum);
1065                 if (ret) {
1066                         bch_err(c, "memory allocation failure");
1067                         return ret;
1068                 }
1069
1070                 for (i = 0; i < p->nr; i++) {
1071                         if (inode->bi_dir != p->entries[i].inum)
1072                                 continue;
1073
1074                         /* XXX print path */
1075                         if (!fsck_err(c, "directory structure loop"))
1076                                 return 0;
1077
1078                         ret = lockrestart_do(trans,
1079                                          remove_backpointer(trans, inode));
1080                         if (ret) {
1081                                 bch_err(c, "error removing dirent: %i", ret);
1082                                 break;
1083                         }
1084
1085                         ret = reattach_inode(trans, inode);
1086                         break;
1087                 }
1088
1089                 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1090                 if (ret) {
1091                         /* Should have been caught in dirents pass */
1092                         bch_err(c, "error looking up parent directory: %i", ret);
1093                         break;
1094                 }
1095         }
1096 fsck_err:
1097         if (ret)
1098                 bch_err(c, "%s: err %i", __func__, ret);
1099         return ret;
1100 }
1101
1102 /*
1103  * Check for unreachable inodes, as well as loops in the directory structure:
1104  * After check_dirents(), if an inode backpointer doesn't exist that means it's
1105  * unreachable:
1106  */
1107 static int check_directory_structure(struct bch_fs *c)
1108 {
1109         struct btree_trans trans;
1110         struct btree_iter *iter;
1111         struct bkey_s_c k;
1112         struct bch_inode_unpacked u;
1113         struct pathbuf path = { 0, 0, NULL };
1114         int ret;
1115
1116         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1117
1118         for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
1119                            BTREE_ITER_INTENT|
1120                            BTREE_ITER_PREFETCH, k, ret) {
1121                 if (k.k->type != KEY_TYPE_inode)
1122                         continue;
1123
1124                 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), &u);
1125                 if (ret) {
1126                         /* Should have been caught earlier in fsck: */
1127                         bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
1128                         break;
1129                 }
1130
1131                 ret = check_path(&trans, &path, &u);
1132                 if (ret)
1133                         break;
1134         }
1135         bch2_trans_iter_put(&trans, iter);
1136
1137         BUG_ON(ret == -EINTR);
1138
1139         kfree(path.entries);
1140
1141         return bch2_trans_exit(&trans) ?: ret;
1142 }
1143
1144 struct nlink_table {
1145         size_t          nr;
1146         size_t          size;
1147
1148         struct nlink {
1149                 u64     inum;
1150                 u32     snapshot;
1151                 u32     count;
1152         }               *d;
1153 };
1154
1155 static int add_nlink(struct nlink_table *t, u64 inum, u32 snapshot)
1156 {
1157         if (t->nr == t->size) {
1158                 size_t new_size = max_t(size_t, 128UL, t->size * 2);
1159                 void *d = kvmalloc(new_size * sizeof(t->d[0]), GFP_KERNEL);
1160                 if (!d) {
1161                         return -ENOMEM;
1162                 }
1163
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 }