]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fsck.c
Update bcachefs sources to 8d3093bd9b bcachefs: Evict btree nodes we're deleting
[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, 0, k, ret) {
516                 if (k.k->type != KEY_TYPE_inode)
517                         continue;
518
519                 inode = bkey_s_c_to_inode(k);
520
521                 if (full ||
522                     (inode.v->bi_flags & (BCH_INODE_I_SIZE_DIRTY|
523                                           BCH_INODE_I_SECTORS_DIRTY|
524                                           BCH_INODE_UNLINKED))) {
525                         ret = check_inode(&trans, iter, inode);
526                         if (ret)
527                                 break;
528                 }
529         }
530         bch2_trans_iter_put(&trans, iter);
531
532         BUG_ON(ret == -EINTR);
533
534         return bch2_trans_exit(&trans) ?: ret;
535 }
536
537 static int fix_overlapping_extent(struct btree_trans *trans,
538                                        struct bkey_s_c k, struct bpos cut_at)
539 {
540         struct btree_iter *iter;
541         struct bkey_i *u;
542         int ret;
543
544         u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
545         ret = PTR_ERR_OR_ZERO(u);
546         if (ret)
547                 return ret;
548
549         bkey_reassemble(u, k);
550         bch2_cut_front(cut_at, u);
551
552
553         /*
554          * We don't want to go through the extent_handle_overwrites path:
555          *
556          * XXX: this is going to screw up disk accounting, extent triggers
557          * assume things about extent overwrites - we should be running the
558          * triggers manually here
559          */
560         iter = bch2_trans_get_iter(trans, BTREE_ID_extents, u->k.p,
561                                    BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
562
563         BUG_ON(iter->flags & BTREE_ITER_IS_EXTENTS);
564         bch2_trans_update(trans, iter, u, BTREE_TRIGGER_NORUN);
565         bch2_trans_iter_put(trans, iter);
566
567         return bch2_trans_commit(trans, NULL, NULL,
568                                  BTREE_INSERT_NOFAIL|
569                                  BTREE_INSERT_LAZY_RW);
570 }
571
572 static int inode_backpointer_exists(struct btree_trans *trans,
573                                     struct bch_inode_unpacked *inode)
574 {
575         struct btree_iter *iter;
576         struct bkey_s_c k;
577         int ret;
578
579         iter = bch2_trans_get_iter(trans, BTREE_ID_dirents,
580                                    POS(inode->bi_dir, inode->bi_dir_offset), 0);
581         k = bch2_btree_iter_peek_slot(iter);
582         ret = bkey_err(k);
583         if (ret)
584                 goto out;
585         if (k.k->type != KEY_TYPE_dirent)
586                 goto out;
587
588         ret = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum) == inode->bi_inum;
589 out:
590         bch2_trans_iter_free(trans, iter);
591         return ret;
592 }
593
594 static bool inode_backpointer_matches(struct bkey_s_c_dirent d,
595                                       struct bch_inode_unpacked *inode)
596 {
597         return d.k->p.inode == inode->bi_dir &&
598                 d.k->p.offset == inode->bi_dir_offset;
599 }
600
601 /*
602  * Walk extents: verify that extents have a corresponding S_ISREG inode, and
603  * that i_size an i_sectors are consistent
604  */
605 noinline_for_stack
606 static int check_extents(struct bch_fs *c)
607 {
608         struct inode_walker w = inode_walker_init();
609         struct btree_trans trans;
610         struct btree_iter *iter;
611         struct bkey_s_c k;
612         struct bkey_buf prev;
613         u64 i_sectors = 0;
614         int ret = 0;
615
616         bch2_bkey_buf_init(&prev);
617         prev.k->k = KEY(0, 0, 0);
618         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
619
620         bch_verbose(c, "checking extents");
621
622         iter = bch2_trans_get_iter(&trans, BTREE_ID_extents,
623                                    POS(BCACHEFS_ROOT_INO, 0),
624                                    BTREE_ITER_INTENT);
625 retry:
626         while ((k = bch2_btree_iter_peek(iter)).k &&
627                !(ret = bkey_err(k))) {
628                 if (w.have_inode &&
629                     w.cur_inum != k.k->p.inode &&
630                     !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
631                     fsck_err_on(w.inode.bi_sectors != i_sectors, c,
632                                 "inode %llu has incorrect i_sectors: got %llu, should be %llu",
633                                 w.inode.bi_inum,
634                                 w.inode.bi_sectors, i_sectors)) {
635                         w.inode.bi_sectors = i_sectors;
636
637                         ret = write_inode(&trans, &w.inode, w.snapshot);
638                         if (ret)
639                                 break;
640                 }
641
642                 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
643                         char buf1[200];
644                         char buf2[200];
645
646                         bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
647                         bch2_bkey_val_to_text(&PBUF(buf2), c, k);
648
649                         if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2))
650                                 return fix_overlapping_extent(&trans, k, prev.k->k.p) ?: -EINTR;
651                 }
652
653                 ret = walk_inode(&trans, &w, k.k->p.inode);
654                 if (ret)
655                         break;
656
657                 if (w.first_this_inode)
658                         i_sectors = 0;
659
660                 if (fsck_err_on(!w.have_inode, c,
661                                 "extent type %u for missing inode %llu",
662                                 k.k->type, k.k->p.inode) ||
663                     fsck_err_on(w.have_inode &&
664                                 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
665                                 "extent type %u for non regular file, inode %llu mode %o",
666                                 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
667                         bch2_fs_lazy_rw(c);
668                         return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
669                                                        POS(k.k->p.inode, 0),
670                                                        POS(k.k->p.inode, U64_MAX),
671                                                        NULL) ?: -EINTR;
672                 }
673
674                 if (fsck_err_on(w.have_inode &&
675                                 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
676                                 k.k->type != KEY_TYPE_reservation &&
677                                 k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
678                                 "extent type %u offset %llu past end of inode %llu, i_size %llu",
679                                 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
680                         bch2_fs_lazy_rw(c);
681                         return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
682                                         POS(k.k->p.inode, round_up(w.inode.bi_size, block_bytes(c))),
683                                         POS(k.k->p.inode, U64_MAX),
684                                         NULL) ?: -EINTR;
685                 }
686
687                 if (bkey_extent_is_allocation(k.k))
688                         i_sectors += k.k->size;
689                 bch2_bkey_buf_reassemble(&prev, c, k);
690
691                 bch2_btree_iter_advance(iter);
692         }
693 fsck_err:
694         if (ret == -EINTR)
695                 goto retry;
696         bch2_trans_iter_put(&trans, iter);
697         bch2_bkey_buf_exit(&prev, c);
698         return bch2_trans_exit(&trans) ?: ret;
699 }
700
701 /*
702  * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
703  * validate d_type
704  */
705 noinline_for_stack
706 static int check_dirents(struct bch_fs *c)
707 {
708         struct inode_walker w = inode_walker_init();
709         struct bch_hash_info hash_info;
710         struct btree_trans trans;
711         struct btree_iter *iter;
712         struct bkey_s_c k;
713         char buf[200];
714         unsigned nr_subdirs = 0;
715         int ret = 0;
716
717         bch_verbose(c, "checking dirents");
718
719         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
720
721         iter = bch2_trans_get_iter(&trans, BTREE_ID_dirents,
722                                    POS(BCACHEFS_ROOT_INO, 0), 0);
723 retry:
724         while ((k = bch2_btree_iter_peek(iter)).k &&
725                !(ret = bkey_err(k))) {
726                 struct bkey_s_c_dirent d;
727                 struct bch_inode_unpacked target;
728                 u32 target_snapshot;
729                 bool have_target;
730                 bool backpointer_exists = true;
731                 u64 d_inum;
732
733                 if (w.have_inode &&
734                     w.cur_inum != k.k->p.inode &&
735                     fsck_err_on(w.inode.bi_nlink != nr_subdirs, c,
736                                 "directory %llu with wrong i_nlink: got %u, should be %u",
737                                 w.inode.bi_inum, w.inode.bi_nlink, nr_subdirs)) {
738                         w.inode.bi_nlink = nr_subdirs;
739                         ret = write_inode(&trans, &w.inode, w.snapshot);
740                         if (ret)
741                                 break;
742                 }
743
744                 ret = walk_inode(&trans, &w, k.k->p.inode);
745                 if (ret)
746                         break;
747
748                 if (w.first_this_inode)
749                         nr_subdirs = 0;
750
751                 if (fsck_err_on(!w.have_inode, c,
752                                 "dirent in nonexisting directory:\n%s",
753                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
754                                                        k), buf)) ||
755                     fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
756                                 "dirent in non directory inode type %u:\n%s",
757                                 mode_to_type(w.inode.bi_mode),
758                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
759                                                        k), buf))) {
760                         ret = lockrestart_do(&trans,
761                                         bch2_btree_delete_at(&trans, iter, 0));
762                         if (ret)
763                                 goto err;
764                         goto next;
765                 }
766
767                 if (!w.have_inode)
768                         goto next;
769
770                 if (w.first_this_inode)
771                         hash_info = bch2_hash_info_init(c, &w.inode);
772
773                 ret = hash_check_key(&trans, bch2_dirent_hash_desc,
774                                      &hash_info, iter, k);
775                 if (ret > 0) {
776                         ret = 0;
777                         goto next;
778                 }
779                 if (ret)
780                         goto fsck_err;
781
782                 if (k.k->type != KEY_TYPE_dirent)
783                         goto next;
784
785                 d = bkey_s_c_to_dirent(k);
786                 d_inum = le64_to_cpu(d.v->d_inum);
787
788                 ret = lookup_inode(&trans, d_inum, &target, &target_snapshot);
789                 if (ret && ret != -ENOENT)
790                         break;
791
792                 have_target = !ret;
793                 ret = 0;
794
795                 if (fsck_err_on(!have_target, c,
796                                 "dirent points to missing inode:\n%s",
797                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
798                                                        k), buf))) {
799                         ret = remove_dirent(&trans, d.k->p);
800                         if (ret)
801                                 goto err;
802                         goto next;
803                 }
804
805                 if (!have_target)
806                         goto next;
807
808                 if (!target.bi_dir &&
809                     !target.bi_dir_offset) {
810                         target.bi_dir           = k.k->p.inode;
811                         target.bi_dir_offset    = k.k->p.offset;
812
813                         ret = write_inode(&trans, &target, target_snapshot);
814                         if (ret)
815                                 goto err;
816                 }
817
818                 if (!inode_backpointer_matches(d, &target)) {
819                         ret = inode_backpointer_exists(&trans, &target);
820                         if (ret < 0)
821                                 goto err;
822
823                         backpointer_exists = ret;
824                         ret = 0;
825
826                         if (fsck_err_on(S_ISDIR(target.bi_mode) &&
827                                         backpointer_exists, c,
828                                         "directory %llu with multiple links",
829                                         target.bi_inum)) {
830                                 ret = remove_dirent(&trans, d.k->p);
831                                 if (ret)
832                                         goto err;
833                                 continue;
834                         }
835
836                         if (fsck_err_on(backpointer_exists &&
837                                         !target.bi_nlink, c,
838                                         "inode %llu has multiple links but i_nlink 0",
839                                         d_inum)) {
840                                 target.bi_nlink++;
841                                 target.bi_flags &= ~BCH_INODE_UNLINKED;
842
843                                 ret = write_inode(&trans, &target, target_snapshot);
844                                 if (ret)
845                                         goto err;
846                         }
847
848                         if (fsck_err_on(!backpointer_exists, c,
849                                         "inode %llu has wrong backpointer:\n"
850                                         "got       %llu:%llu\n"
851                                         "should be %llu:%llu",
852                                         d_inum,
853                                         target.bi_dir,
854                                         target.bi_dir_offset,
855                                         k.k->p.inode,
856                                         k.k->p.offset)) {
857                                 target.bi_dir           = k.k->p.inode;
858                                 target.bi_dir_offset    = k.k->p.offset;
859
860                                 ret = write_inode(&trans, &target, target_snapshot);
861                                 if (ret)
862                                         goto err;
863                         }
864                 }
865
866                 if (fsck_err_on(d.v->d_type != mode_to_type(target.bi_mode), c,
867                                 "incorrect d_type: should be %u:\n%s",
868                                 mode_to_type(target.bi_mode),
869                                 (bch2_bkey_val_to_text(&PBUF(buf), c,
870                                                        k), buf))) {
871                         struct bkey_i_dirent *n;
872
873                         n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
874                         if (!n) {
875                                 ret = -ENOMEM;
876                                 goto err;
877                         }
878
879                         bkey_reassemble(&n->k_i, d.s_c);
880                         n->v.d_type = mode_to_type(target.bi_mode);
881
882                         ret = __bch2_trans_do(&trans, NULL, NULL,
883                                               BTREE_INSERT_NOFAIL|
884                                               BTREE_INSERT_LAZY_RW,
885                                 (bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
886                         kfree(n);
887                         if (ret)
888                                 goto err;
889
890                 }
891
892                 nr_subdirs += d.v->d_type == DT_DIR;
893 next:
894                 bch2_btree_iter_advance(iter);
895         }
896 err:
897 fsck_err:
898         if (ret == -EINTR)
899                 goto retry;
900
901         bch2_trans_iter_put(&trans, iter);
902         return bch2_trans_exit(&trans) ?: ret;
903 }
904
905 /*
906  * Walk xattrs: verify that they all have a corresponding inode
907  */
908 noinline_for_stack
909 static int check_xattrs(struct bch_fs *c)
910 {
911         struct inode_walker w = inode_walker_init();
912         struct bch_hash_info hash_info;
913         struct btree_trans trans;
914         struct btree_iter *iter;
915         struct bkey_s_c k;
916         int ret = 0;
917
918         bch_verbose(c, "checking xattrs");
919
920         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
921
922         iter = bch2_trans_get_iter(&trans, BTREE_ID_xattrs,
923                                    POS(BCACHEFS_ROOT_INO, 0), 0);
924 retry:
925         while ((k = bch2_btree_iter_peek(iter)).k &&
926                !(ret = bkey_err(k))) {
927                 ret = walk_inode(&trans, &w, k.k->p.inode);
928                 if (ret)
929                         break;
930
931                 if (fsck_err_on(!w.have_inode, c,
932                                 "xattr for missing inode %llu",
933                                 k.k->p.inode)) {
934                         ret = bch2_btree_delete_at(&trans, iter, 0);
935                         if (ret)
936                                 break;
937                         continue;
938                 }
939
940                 if (w.first_this_inode && w.have_inode)
941                         hash_info = bch2_hash_info_init(c, &w.inode);
942
943                 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
944                                      &hash_info, iter, k);
945                 if (ret)
946                         break;
947
948                 bch2_btree_iter_advance(iter);
949         }
950 fsck_err:
951         if (ret == -EINTR)
952                 goto retry;
953
954         bch2_trans_iter_put(&trans, iter);
955         return bch2_trans_exit(&trans) ?: ret;
956 }
957
958 /* Get root directory, create if it doesn't exist: */
959 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
960 {
961         struct bkey_inode_buf packed;
962         u32 snapshot;
963         int ret;
964
965         bch_verbose(c, "checking root directory");
966
967         ret = bch2_trans_do(c, NULL, NULL, 0,
968                 lookup_inode(&trans, BCACHEFS_ROOT_INO, root_inode, &snapshot));
969         if (ret && ret != -ENOENT)
970                 return ret;
971
972         if (fsck_err_on(ret, c, "root directory missing"))
973                 goto create_root;
974
975         if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
976                         "root inode not a directory"))
977                 goto create_root;
978
979         return 0;
980 fsck_err:
981         return ret;
982 create_root:
983         bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
984                         0, NULL);
985         root_inode->bi_inum = BCACHEFS_ROOT_INO;
986
987         bch2_inode_pack(c, &packed, root_inode);
988
989         return bch2_btree_insert(c, BTREE_ID_inodes, &packed.inode.k_i,
990                                  NULL, NULL,
991                                  BTREE_INSERT_NOFAIL|
992                                  BTREE_INSERT_LAZY_RW);
993 }
994
995 struct pathbuf {
996         size_t          nr;
997         size_t          size;
998
999         struct pathbuf_entry {
1000                 u64     inum;
1001         }               *entries;
1002 };
1003
1004 static int path_down(struct pathbuf *p, u64 inum)
1005 {
1006         if (p->nr == p->size) {
1007                 size_t new_size = max_t(size_t, 256UL, p->size * 2);
1008                 void *n = krealloc(p->entries,
1009                                    new_size * sizeof(p->entries[0]),
1010                                    GFP_KERNEL);
1011                 if (!n) {
1012                         return -ENOMEM;
1013                 }
1014
1015                 p->entries = n;
1016                 p->size = new_size;
1017         };
1018
1019         p->entries[p->nr++] = (struct pathbuf_entry) {
1020                 .inum = inum,
1021         };
1022         return 0;
1023 }
1024
1025 static int check_path(struct btree_trans *trans,
1026                       struct pathbuf *p,
1027                       struct bch_inode_unpacked *inode)
1028 {
1029         struct bch_fs *c = trans->c;
1030         u32 snapshot;
1031         size_t i;
1032         int ret = 0;
1033
1034         p->nr = 0;
1035
1036         while (inode->bi_inum != BCACHEFS_ROOT_INO) {
1037                 ret = lockrestart_do(trans,
1038                         inode_backpointer_exists(trans, inode));
1039                 if (ret < 0)
1040                         break;
1041
1042                 if (!ret) {
1043                         if (fsck_err(c,  "unreachable inode %llu, type %u nlink %u backptr %llu:%llu",
1044                                      inode->bi_inum,
1045                                      mode_to_type(inode->bi_mode),
1046                                      inode->bi_nlink,
1047                                      inode->bi_dir,
1048                                      inode->bi_dir_offset))
1049                                 ret = reattach_inode(trans, inode);
1050                         break;
1051                 }
1052                 ret = 0;
1053
1054                 if (!S_ISDIR(inode->bi_mode))
1055                         break;
1056
1057                 ret = path_down(p, inode->bi_inum);
1058                 if (ret) {
1059                         bch_err(c, "memory allocation failure");
1060                         return ret;
1061                 }
1062
1063                 for (i = 0; i < p->nr; i++) {
1064                         if (inode->bi_dir != p->entries[i].inum)
1065                                 continue;
1066
1067                         /* XXX print path */
1068                         if (!fsck_err(c, "directory structure loop"))
1069                                 return 0;
1070
1071                         ret = lockrestart_do(trans,
1072                                          remove_backpointer(trans, inode));
1073                         if (ret) {
1074                                 bch_err(c, "error removing dirent: %i", ret);
1075                                 break;
1076                         }
1077
1078                         ret = reattach_inode(trans, inode);
1079                         break;
1080                 }
1081
1082                 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1083                 if (ret) {
1084                         /* Should have been caught in dirents pass */
1085                         bch_err(c, "error looking up parent directory: %i", ret);
1086                         break;
1087                 }
1088         }
1089 fsck_err:
1090         if (ret)
1091                 bch_err(c, "%s: err %i", __func__, ret);
1092         return ret;
1093 }
1094
1095 /*
1096  * Check for unreachable inodes, as well as loops in the directory structure:
1097  * After check_dirents(), if an inode backpointer doesn't exist that means it's
1098  * unreachable:
1099  */
1100 static int check_directory_structure(struct bch_fs *c)
1101 {
1102         struct btree_trans trans;
1103         struct btree_iter *iter;
1104         struct bkey_s_c k;
1105         struct bch_inode_unpacked u;
1106         struct pathbuf path = { 0, 0, NULL };
1107         int ret;
1108
1109         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1110
1111         for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN, 0, k, ret) {
1112                 if (k.k->type != KEY_TYPE_inode)
1113                         continue;
1114
1115                 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), &u);
1116                 if (ret) {
1117                         /* Should have been caught earlier in fsck: */
1118                         bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
1119                         break;
1120                 }
1121
1122                 ret = check_path(&trans, &path, &u);
1123                 if (ret)
1124                         break;
1125         }
1126         bch2_trans_iter_put(&trans, iter);
1127
1128         BUG_ON(ret == -EINTR);
1129
1130         kfree(path.entries);
1131
1132         return bch2_trans_exit(&trans) ?: ret;
1133 }
1134
1135 struct nlink_table {
1136         size_t          nr;
1137         size_t          size;
1138
1139         struct nlink {
1140                 u64     inum;
1141                 u32     snapshot;
1142                 u32     count;
1143         }               *d;
1144 };
1145
1146 static int add_nlink(struct nlink_table *t, u64 inum, u32 snapshot)
1147 {
1148         if (t->nr == t->size) {
1149                 size_t new_size = max_t(size_t, 128UL, t->size * 2);
1150                 void *d = kvmalloc(new_size * sizeof(t->d[0]), GFP_KERNEL);
1151                 if (!d) {
1152                         return -ENOMEM;
1153                 }
1154
1155                 memcpy(d, t->d, t->size * sizeof(t->d[0]));
1156                 kvfree(t->d);
1157
1158                 t->d = d;
1159                 t->size = new_size;
1160         }
1161
1162
1163         t->d[t->nr++] = (struct nlink) {
1164                 .inum           = inum,
1165                 .snapshot       = snapshot,
1166         };
1167
1168         return 0;
1169 }
1170
1171 static int nlink_cmp(const void *_l, const void *_r)
1172 {
1173         const struct nlink *l = _l;
1174         const struct nlink *r = _r;
1175
1176         return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
1177 }
1178
1179 static void inc_link(struct bch_fs *c, struct nlink_table *links,
1180                      u64 range_start, u64 range_end, u64 inum)
1181 {
1182         struct nlink *link, key = {
1183                 .inum = inum, .snapshot = U32_MAX,
1184         };
1185
1186         if (inum < range_start || inum >= range_end)
1187                 return;
1188
1189         link = __inline_bsearch(&key, links->d, links->nr,
1190                                 sizeof(links->d[0]), nlink_cmp);
1191         if (link)
1192                 link->count++;
1193 }
1194
1195 noinline_for_stack
1196 static int check_nlinks_find_hardlinks(struct bch_fs *c,
1197                                        struct nlink_table *t,
1198                                        u64 start, u64 *end)
1199 {
1200         struct btree_trans trans;
1201         struct btree_iter *iter;
1202         struct bkey_s_c k;
1203         struct bkey_s_c_inode inode;
1204         struct bch_inode_unpacked u;
1205         int ret = 0;
1206
1207         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1208
1209         for_each_btree_key(&trans, iter, BTREE_ID_inodes,
1210                            POS(0, start), 0, k, ret) {
1211                 if (k.k->type != KEY_TYPE_inode)
1212                         continue;
1213
1214                 inode = bkey_s_c_to_inode(k);
1215
1216                 /*
1217                  * Backpointer and directory structure checks are sufficient for
1218                  * directories, since they can't have hardlinks:
1219                  */
1220                 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
1221                         continue;
1222
1223                 /* Should never fail, checked by bch2_inode_invalid: */
1224                 BUG_ON(bch2_inode_unpack(inode, &u));
1225
1226                 if (!u.bi_nlink)
1227                         continue;
1228
1229                 ret = add_nlink(t, k.k->p.offset, k.k->p.snapshot);
1230                 if (ret) {
1231                         *end = k.k->p.offset;
1232                         ret = 0;
1233                         break;
1234                 }
1235
1236         }
1237         bch2_trans_iter_put(&trans, iter);
1238         bch2_trans_exit(&trans);
1239
1240         if (ret)
1241                 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
1242
1243         return ret;
1244 }
1245
1246 noinline_for_stack
1247 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
1248                                      u64 range_start, u64 range_end)
1249 {
1250         struct btree_trans trans;
1251         struct btree_iter *iter;
1252         struct bkey_s_c k;
1253         struct bkey_s_c_dirent d;
1254         int ret;
1255
1256         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1257
1258         for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN, 0, k, ret) {
1259                 switch (k.k->type) {
1260                 case KEY_TYPE_dirent:
1261                         d = bkey_s_c_to_dirent(k);
1262
1263                         if (d.v->d_type != DT_DIR)
1264                                 inc_link(c, links, range_start, range_end,
1265                                          le64_to_cpu(d.v->d_inum));
1266                         break;
1267                 }
1268
1269                 bch2_trans_cond_resched(&trans);
1270         }
1271         bch2_trans_iter_put(&trans, iter);
1272
1273         ret = bch2_trans_exit(&trans) ?: ret;
1274         if (ret)
1275                 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
1276
1277         return ret;
1278 }
1279
1280 noinline_for_stack
1281 static int check_nlinks_update_hardlinks(struct bch_fs *c,
1282                                struct nlink_table *links,
1283                                u64 range_start, u64 range_end)
1284 {
1285         struct btree_trans trans;
1286         struct btree_iter *iter;
1287         struct bkey_s_c k;
1288         struct bkey_s_c_inode inode;
1289         struct bch_inode_unpacked u;
1290         struct nlink *link = links->d;
1291         int ret = 0;
1292
1293         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1294
1295         for_each_btree_key(&trans, iter, BTREE_ID_inodes,
1296                            POS(0, range_start), 0, k, ret) {
1297                 if (k.k->p.offset >= range_end)
1298                         break;
1299
1300                 if (k.k->type != KEY_TYPE_inode)
1301                         continue;
1302
1303                 inode = bkey_s_c_to_inode(k);
1304                 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
1305                         continue;
1306
1307                 BUG_ON(bch2_inode_unpack(inode, &u));
1308
1309                 if (!u.bi_nlink)
1310                         continue;
1311
1312                 while (link->inum < k.k->p.offset) {
1313                         link++;
1314                         BUG_ON(link >= links->d + links->nr);
1315                 }
1316
1317                 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
1318                                 "inode %llu has wrong i_nlink (type %u i_nlink %u, should be %u)",
1319                                 u.bi_inum, mode_to_type(u.bi_mode),
1320                                 bch2_inode_nlink_get(&u), link->count)) {
1321                         bch2_inode_nlink_set(&u, link->count);
1322
1323                         ret = __bch2_trans_do(&trans, NULL, NULL,
1324                                               BTREE_INSERT_NOFAIL|
1325                                               BTREE_INSERT_LAZY_RW,
1326                                         bch2_inode_write(&trans, iter, &u));
1327                         if (ret)
1328                                 bch_err(c, "error in fsck: error %i updating inode", ret);
1329                 }
1330         }
1331 fsck_err:
1332         bch2_trans_iter_put(&trans, iter);
1333         bch2_trans_exit(&trans);
1334
1335         if (ret)
1336                 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
1337
1338         return ret;
1339 }
1340
1341 noinline_for_stack
1342 static int check_nlinks(struct bch_fs *c)
1343 {
1344         struct nlink_table links = { 0 };
1345         u64 this_iter_range_start, next_iter_range_start = 0;
1346         int ret = 0;
1347
1348         bch_verbose(c, "checking inode nlinks");
1349
1350         do {
1351                 this_iter_range_start = next_iter_range_start;
1352                 next_iter_range_start = U64_MAX;
1353
1354                 ret = check_nlinks_find_hardlinks(c, &links,
1355                                                   this_iter_range_start,
1356                                                   &next_iter_range_start);
1357
1358                 ret = check_nlinks_walk_dirents(c, &links,
1359                                           this_iter_range_start,
1360                                           next_iter_range_start);
1361                 if (ret)
1362                         break;
1363
1364                 ret = check_nlinks_update_hardlinks(c, &links,
1365                                          this_iter_range_start,
1366                                          next_iter_range_start);
1367                 if (ret)
1368                         break;
1369
1370                 links.nr = 0;
1371         } while (next_iter_range_start != U64_MAX);
1372
1373         kvfree(links.d);
1374
1375         return ret;
1376 }
1377
1378 /*
1379  * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1380  * Doesn't fix them yet, mainly because they haven't yet been observed:
1381  */
1382 int bch2_fsck_full(struct bch_fs *c)
1383 {
1384         struct bch_inode_unpacked root_inode;
1385
1386         return  check_inodes(c, true) ?:
1387                 check_extents(c) ?:
1388                 check_dirents(c) ?:
1389                 check_xattrs(c) ?:
1390                 check_root(c, &root_inode) ?:
1391                 check_directory_structure(c) ?:
1392                 check_nlinks(c);
1393 }
1394
1395 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1396 {
1397         return check_inodes(c, false);
1398 }