]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/fsck.c
Update bcachefs sources to b1a4dc53be bcachefs: Set lost+found mode to 0700
[bcachefs-tools-debian] / libbcachefs / fsck.c
index 53ee1b0e88053e07dbc440c141929cdc49bb0215..0f2308e53d652dfe86344f3e67c26f527b07554e 100644 (file)
@@ -1,9 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0
 
 #include "bcachefs.h"
 #include "btree_update.h"
 #include "dirent.h"
 #include "error.h"
-#include "fs.h"
+#include "fs-common.h"
 #include "fsck.h"
 #include "inode.h"
 #include "keylist.h"
 
 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
 
-static int remove_dirent(struct bch_fs *c, struct btree_iter *iter,
+static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum)
+{
+       struct btree_iter *iter;
+       struct bkey_s_c k;
+       u64 sectors = 0;
+       int ret;
+
+       for_each_btree_key(trans, iter, BTREE_ID_EXTENTS,
+                          POS(inum, 0), 0, k, ret) {
+               if (k.k->p.inode != inum)
+                       break;
+
+               if (bkey_extent_is_allocation(k.k))
+                       sectors += k.k->size;
+       }
+
+       bch2_trans_iter_free(trans, iter);
+
+       return ret ?: sectors;
+}
+
+static int remove_dirent(struct btree_trans *trans,
                         struct bkey_s_c_dirent dirent)
 {
+       struct bch_fs *c = trans->c;
        struct qstr name;
        struct bch_inode_unpacked dir_inode;
        struct bch_hash_info dir_hash_info;
@@ -34,8 +57,8 @@ static int remove_dirent(struct bch_fs *c, struct btree_iter *iter,
        buf[name.len] = '\0';
        name.name = buf;
 
-       /* Unlock iter so we don't deadlock, after copying name: */
-       bch2_btree_iter_unlock(iter);
+       /* Unlock so we don't deadlock, after copying name: */
+       bch2_trans_unlock(trans);
 
        ret = bch2_inode_find_by_inum(c, dir_inum, &dir_inode);
        if (ret) {
@@ -57,9 +80,7 @@ static int reattach_inode(struct bch_fs *c,
                          struct bch_inode_unpacked *lostfound_inode,
                          u64 inum)
 {
-       struct bch_hash_info lostfound_hash_info =
-               bch2_hash_info_init(c, lostfound_inode);
-       struct bkey_inode_buf packed;
+       struct bch_inode_unpacked inode_u;
        char name_buf[20];
        struct qstr name;
        int ret;
@@ -67,27 +88,14 @@ static int reattach_inode(struct bch_fs *c,
        snprintf(name_buf, sizeof(name_buf), "%llu", inum);
        name = (struct qstr) QSTR(name_buf);
 
-       lostfound_inode->bi_nlink++;
-
-       bch2_inode_pack(&packed, lostfound_inode);
-
-       ret = bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
-                               NULL, NULL, BTREE_INSERT_NOFAIL);
-       if (ret) {
-               bch_err(c, "error %i reattaching inode %llu while updating lost+found",
-                       ret, inum);
-               return ret;
-       }
+       ret = bch2_trans_do(c, NULL,
+                           BTREE_INSERT_ATOMIC|
+                           BTREE_INSERT_LAZY_RW,
+               bch2_link_trans(&trans, lostfound_inode->bi_inum,
+                               inum, &inode_u, &name));
+       if (ret)
+               bch_err(c, "error %i reattaching inode %llu", ret, inum);
 
-       ret = bch2_dirent_create(c, lostfound_inode->bi_inum,
-                                &lostfound_hash_info,
-                                DT_DIR, &name, inum, NULL,
-                                BTREE_INSERT_NOFAIL);
-       if (ret) {
-               bch_err(c, "error %i reattaching inode %llu while creating new dirent",
-                       ret, inum);
-               return ret;
-       }
        return ret;
 }
 
@@ -106,18 +114,21 @@ static struct inode_walker inode_walker_init(void)
        };
 }
 
-static int walk_inode(struct bch_fs *c, struct inode_walker *w, u64 inum)
+static int walk_inode(struct btree_trans *trans,
+                     struct inode_walker *w, u64 inum)
 {
-       w->first_this_inode     = inum != w->cur_inum;
-       w->cur_inum             = inum;
-
-       if (w->first_this_inode) {
-               int ret = bch2_inode_find_by_inum(c, inum, &w->inode);
+       if (inum != w->cur_inum) {
+               int ret = bch2_inode_find_by_inum_trans(trans, inum,
+                                                       &w->inode);
 
                if (ret && ret != -ENOENT)
                        return ret;
 
-               w->have_inode = !ret;
+               w->have_inode   = !ret;
+               w->cur_inum     = inum;
+               w->first_this_inode = true;
+       } else {
+               w->first_this_inode = false;
        }
 
        return 0;
@@ -125,33 +136,38 @@ static int walk_inode(struct bch_fs *c, struct inode_walker *w, u64 inum)
 
 struct hash_check {
        struct bch_hash_info    info;
-       struct btree_trans      *trans;
 
        /* start of current chain of hash collisions: */
        struct btree_iter       *chain;
 
        /* next offset in current chain of hash collisions: */
-       u64                     next;
+       u64                     chain_end;
 };
 
-static void hash_check_init(const struct bch_hash_desc desc,
-                           struct btree_trans *trans,
+static void hash_check_init(struct hash_check *h)
+{
+       h->chain = NULL;
+       h->chain_end = 0;
+}
+
+static void hash_stop_chain(struct btree_trans *trans,
                            struct hash_check *h)
 {
-       h->trans = trans;
-       h->chain = bch2_trans_get_iter(trans, desc.btree_id, POS_MIN, 0);
-       h->next = -1;
+       if (h->chain)
+               bch2_trans_iter_free(trans, h->chain);
+       h->chain = NULL;
 }
 
-static void hash_check_set_inode(struct hash_check *h, struct bch_fs *c,
+static void hash_check_set_inode(struct btree_trans *trans,
+                                struct hash_check *h,
                                 const struct bch_inode_unpacked *bi)
 {
-       h->info = bch2_hash_info_init(c, bi);
-       h->next = -1;
+       h->info = bch2_hash_info_init(trans->c, bi);
+       hash_stop_chain(trans, h);
 }
 
 static int hash_redo_key(const struct bch_hash_desc desc,
-                        struct hash_check *h, struct bch_fs *c,
+                        struct btree_trans *trans, struct hash_check *h,
                         struct btree_iter *k_iter, struct bkey_s_c k,
                         u64 hashed)
 {
@@ -164,57 +180,46 @@ static int hash_redo_key(const struct bch_hash_desc desc,
 
        bkey_reassemble(tmp, k);
 
-       ret = bch2_btree_delete_at(k_iter, 0);
+       ret = bch2_btree_delete_at(trans, k_iter, 0);
        if (ret)
                goto err;
 
-       bch2_btree_iter_unlock(k_iter);
-
-       bch2_hash_set(desc, &h->info, c, k_iter->pos.inode, NULL, tmp,
-                     BTREE_INSERT_NOFAIL|
-                     BCH_HASH_SET_MUST_CREATE);
+       bch2_hash_set(trans, desc, &h->info, k_iter->pos.inode,
+                     tmp, BCH_HASH_SET_MUST_CREATE);
+       ret = bch2_trans_commit(trans, NULL, NULL,
+                               BTREE_INSERT_NOFAIL|
+                               BTREE_INSERT_LAZY_RW);
 err:
        kfree(tmp);
        return ret;
 }
 
-/* fsck hasn't been converted to new transactions yet: */
-static int fsck_hash_delete_at(const struct bch_hash_desc desc,
+static int fsck_hash_delete_at(struct btree_trans *trans,
+                              const struct bch_hash_desc desc,
                               struct bch_hash_info *info,
-                              struct btree_iter *orig_iter)
+                              struct btree_iter *iter)
 {
-       struct btree_trans trans;
-       struct btree_iter *iter;
        int ret;
-
-       bch2_btree_iter_unlock(orig_iter);
-
-       bch2_trans_init(&trans, orig_iter->c);
 retry:
-       bch2_trans_begin(&trans);
-
-       iter = bch2_trans_copy_iter(&trans, orig_iter);
-       if (IS_ERR(iter)) {
-               ret = PTR_ERR(iter);
-               goto err;
-       }
-
-       ret   = bch2_hash_delete_at(&trans, desc, info, iter) ?:
-               bch2_trans_commit(&trans, NULL, NULL,
+       ret   = bch2_hash_delete_at(trans, desc, info, iter) ?:
+               bch2_trans_commit(trans, NULL, NULL,
                                  BTREE_INSERT_ATOMIC|
-                                 BTREE_INSERT_NOFAIL);
-err:
-       if (ret == -EINTR)
-               goto retry;
+                                 BTREE_INSERT_NOFAIL|
+                                 BTREE_INSERT_LAZY_RW);
+       if (ret == -EINTR) {
+               ret = bch2_btree_iter_traverse(iter);
+               if (!ret)
+                       goto retry;
+       }
 
-       bch2_trans_exit(&trans);
        return ret;
 }
 
-static int hash_check_duplicates(const struct bch_hash_desc desc,
-                                struct hash_check *h, struct bch_fs *c,
-                                struct btree_iter *k_iter, struct bkey_s_c k)
+static int hash_check_duplicates(struct btree_trans *trans,
+                       const struct bch_hash_desc desc, struct hash_check *h,
+                       struct btree_iter *k_iter, struct bkey_s_c k)
 {
+       struct bch_fs *c = trans->c;
        struct btree_iter *iter;
        struct bkey_s_c k2;
        char buf[200];
@@ -223,10 +228,10 @@ static int hash_check_duplicates(const struct bch_hash_desc desc,
        if (!bkey_cmp(h->chain->pos, k_iter->pos))
                return 0;
 
-       iter = bch2_trans_copy_iter(h->trans, h->chain);
+       iter = bch2_trans_copy_iter(trans, h->chain);
        BUG_ON(IS_ERR(iter));
 
-       for_each_btree_key_continue(iter, 0, k2) {
+       for_each_btree_key_continue(iter, 0, k2, ret) {
                if (bkey_cmp(k2.k->p, k.k->p) >= 0)
                        break;
 
@@ -234,9 +239,8 @@ static int hash_check_duplicates(const struct bch_hash_desc desc,
                                !desc.cmp_bkey(k, k2), c,
                                "duplicate hash table keys:\n%s",
                                (bch2_bkey_val_to_text(&PBUF(buf), c,
-                                                      bkey_type(0, desc.btree_id),
                                                       k), buf))) {
-                       ret = fsck_hash_delete_at(desc, &h->info, k_iter);
+                       ret = fsck_hash_delete_at(trans, desc, &h->info, k_iter);
                        if (ret)
                                return ret;
                        ret = 1;
@@ -244,23 +248,39 @@ static int hash_check_duplicates(const struct bch_hash_desc desc,
                }
        }
 fsck_err:
-       bch2_trans_iter_free(h->trans, iter);
+       bch2_trans_iter_free(trans, iter);
        return ret;
 }
 
-static bool key_has_correct_hash(const struct bch_hash_desc desc,
-                                struct hash_check *h, struct bch_fs *c,
-                                struct btree_iter *k_iter, struct bkey_s_c k)
+static void hash_set_chain_start(struct btree_trans *trans,
+                       const struct bch_hash_desc desc,
+                       struct hash_check *h,
+                       struct btree_iter *k_iter, struct bkey_s_c k)
 {
-       u64 hash;
+       bool hole = (k.k->type != KEY_TYPE_whiteout &&
+                    k.k->type != desc.key_type);
 
-       if (k.k->type != desc.whiteout_type &&
-           k.k->type != desc.key_type)
-               return true;
+       if (hole || k.k->p.offset > h->chain_end + 1)
+               hash_stop_chain(trans, h);
+
+       if (!hole) {
+               if (!h->chain) {
+                       h->chain = bch2_trans_copy_iter(trans, k_iter);
+                       BUG_ON(IS_ERR(h->chain));
+               }
 
-       if (k.k->p.offset != h->next)
-               bch2_btree_iter_copy(h->chain, k_iter);
-       h->next = k.k->p.offset + 1;
+               h->chain_end = k.k->p.offset;
+       }
+}
+
+static bool key_has_correct_hash(struct btree_trans *trans,
+                       const struct bch_hash_desc desc,
+                       struct hash_check *h,
+                       struct btree_iter *k_iter, struct bkey_s_c k)
+{
+       u64 hash;
+
+       hash_set_chain_start(trans, desc, h, k_iter, k);
 
        if (k.k->type != desc.key_type)
                return true;
@@ -271,21 +291,16 @@ static bool key_has_correct_hash(const struct bch_hash_desc desc,
                hash <= k.k->p.offset;
 }
 
-static int hash_check_key(const struct bch_hash_desc desc,
-                         struct hash_check *h, struct bch_fs *c,
-                         struct btree_iter *k_iter, struct bkey_s_c k)
+static int hash_check_key(struct btree_trans *trans,
+                       const struct bch_hash_desc desc, struct hash_check *h,
+                       struct btree_iter *k_iter, struct bkey_s_c k)
 {
+       struct bch_fs *c = trans->c;
        char buf[200];
        u64 hashed;
        int ret = 0;
 
-       if (k.k->type != desc.whiteout_type &&
-           k.k->type != desc.key_type)
-               return 0;
-
-       if (k.k->p.offset != h->next)
-               bch2_btree_iter_copy(h->chain, k_iter);
-       h->next = k.k->p.offset + 1;
+       hash_set_chain_start(trans, desc, h, k_iter, k);
 
        if (k.k->type != desc.key_type)
                return 0;
@@ -299,9 +314,8 @@ static int hash_check_key(const struct bch_hash_desc desc,
                        desc.btree_id, k.k->p.offset,
                        hashed, h->chain->pos.offset,
                        (bch2_bkey_val_to_text(&PBUF(buf), c,
-                                              bkey_type(0, desc.btree_id),
                                               k), buf))) {
-               ret = hash_redo_key(desc, h, c, k_iter, k, hashed);
+               ret = hash_redo_key(desc, trans, h, k_iter, k, hashed);
                if (ret) {
                        bch_err(c, "hash_redo_key err %i", ret);
                        return ret;
@@ -309,21 +323,22 @@ static int hash_check_key(const struct bch_hash_desc desc,
                return 1;
        }
 
-       ret = hash_check_duplicates(desc, h, c, k_iter, k);
+       ret = hash_check_duplicates(trans, desc, h, k_iter, k);
 fsck_err:
        return ret;
 }
 
-static int check_dirent_hash(struct hash_check *h, struct bch_fs *c,
+static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
                             struct btree_iter *iter, struct bkey_s_c *k)
 {
+       struct bch_fs *c = trans->c;
        struct bkey_i_dirent *d = NULL;
        int ret = -EINVAL;
        char buf[200];
        unsigned len;
        u64 hash;
 
-       if (key_has_correct_hash(bch2_dirent_hash_desc, h, c, iter, *k))
+       if (key_has_correct_hash(trans, bch2_dirent_hash_desc, h, iter, *k))
                return 0;
 
        len = bch2_dirent_name_bytes(bkey_s_c_to_dirent(*k));
@@ -361,15 +376,17 @@ static int check_dirent_hash(struct hash_check *h, struct bch_fs *c,
 
        if (fsck_err(c, "dirent with junk at end, was %s (%zu) now %s (%u)",
                     buf, strlen(buf), d->v.d_name, len)) {
-               ret = bch2_btree_insert_at(c, NULL, NULL,
-                                          BTREE_INSERT_NOFAIL,
-                                          BTREE_INSERT_ENTRY(iter, &d->k_i));
+               bch2_trans_update(trans, iter, &d->k_i);
+
+               ret = bch2_trans_commit(trans, NULL, NULL,
+                                       BTREE_INSERT_NOFAIL|
+                                       BTREE_INSERT_LAZY_RW);
                if (ret)
                        goto err;
 
                *k = bch2_btree_iter_peek(iter);
 
-               BUG_ON(k->k->type != BCH_DIRENT);
+               BUG_ON(k->k->type != KEY_TYPE_dirent);
        }
 err:
 fsck_err:
@@ -384,10 +401,9 @@ err_redo:
                     buf, strlen(buf), BTREE_ID_DIRENTS,
                     k->k->p.offset, hash, h->chain->pos.offset,
                     (bch2_bkey_val_to_text(&PBUF(buf), c,
-                                           bkey_type(0, BTREE_ID_DIRENTS),
                                            *k), buf))) {
-               ret = hash_redo_key(bch2_dirent_hash_desc,
-                                   h, c, iter, *k, hash);
+               ret = hash_redo_key(bch2_dirent_hash_desc, trans,
+                                   h, iter, *k, hash);
                if (ret)
                        bch_err(c, "hash_redo_key err %i", ret);
                else
@@ -412,16 +428,21 @@ noinline_for_stack
 static int check_extents(struct bch_fs *c)
 {
        struct inode_walker w = inode_walker_init();
-       struct btree_iter iter;
+       struct btree_trans trans;
+       struct btree_iter *iter;
        struct bkey_s_c k;
        u64 i_sectors;
        int ret = 0;
 
+       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
+
        bch_verbose(c, "checking extents");
 
-       for_each_btree_key(&iter, c, BTREE_ID_EXTENTS,
-                          POS(BCACHEFS_ROOT_INO, 0), 0, k) {
-               ret = walk_inode(c, &w, k.k->p.inode);
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
+                                  POS(BCACHEFS_ROOT_INO, 0), 0);
+retry:
+       for_each_btree_key_continue(iter, 0, k, ret) {
+               ret = walk_inode(&trans, &w, k.k->p.inode);
                if (ret)
                        break;
 
@@ -432,7 +453,7 @@ static int check_extents(struct bch_fs *c)
                        !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
                        "extent type %u for non regular file, inode %llu mode %o",
                        k.k->type, k.k->p.inode, w.inode.bi_mode)) {
-                       bch2_btree_iter_unlock(&iter);
+                       bch2_trans_unlock(&trans);
 
                        ret = bch2_inode_truncate(c, k.k->p.inode, 0);
                        if (ret)
@@ -444,37 +465,37 @@ static int check_extents(struct bch_fs *c)
                        w.have_inode &&
                        !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
                        w.inode.bi_sectors !=
-                       (i_sectors = bch2_count_inode_sectors(c, w.cur_inum)),
+                       (i_sectors = bch2_count_inode_sectors(&trans, w.cur_inum)),
                        c, "i_sectors wrong: got %llu, should be %llu",
                        w.inode.bi_sectors, i_sectors)) {
                        struct bkey_inode_buf p;
 
                        w.inode.bi_sectors = i_sectors;
 
-                       bch2_btree_iter_unlock(&iter);
+                       bch2_trans_unlock(&trans);
 
                        bch2_inode_pack(&p, &w.inode);
 
                        ret = bch2_btree_insert(c, BTREE_ID_INODES,
                                                &p.inode.k_i, NULL, NULL,
-                                               BTREE_INSERT_NOFAIL);
+                                               BTREE_INSERT_NOFAIL|
+                                               BTREE_INSERT_LAZY_RW);
                        if (ret) {
-                               bch_err(c, "error in fs gc: error %i "
-                                       "updating inode", ret);
+                               bch_err(c, "error in fsck: error %i updating inode", ret);
                                goto err;
                        }
 
                        /* revalidate iterator: */
-                       k = bch2_btree_iter_peek(&iter);
+                       k = bch2_btree_iter_peek(iter);
                }
 
                if (fsck_err_on(w.have_inode &&
                        !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
-                       k.k->type != BCH_RESERVATION &&
-                       k.k->p.offset > round_up(w.inode.bi_size, PAGE_SIZE) >> 9, c,
+                       k.k->type != KEY_TYPE_reservation &&
+                       k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
                        "extent type %u offset %llu past end of inode %llu, i_size %llu",
                        k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
-                       bch2_btree_iter_unlock(&iter);
+                       bch2_trans_unlock(&trans);
 
                        ret = bch2_inode_truncate(c, k.k->p.inode,
                                                  w.inode.bi_size);
@@ -485,7 +506,9 @@ static int check_extents(struct bch_fs *c)
        }
 err:
 fsck_err:
-       return bch2_btree_iter_unlock(&iter) ?: ret;
+       if (ret == -EINTR)
+               goto retry;
+       return bch2_trans_exit(&trans) ?: ret;
 }
 
 /*
@@ -506,46 +529,42 @@ static int check_dirents(struct bch_fs *c)
 
        bch_verbose(c, "checking dirents");
 
-       bch2_trans_init(&trans, c);
+       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
 
-       bch2_trans_preload_iters(&trans);
+       hash_check_init(&h);
 
        iter = bch2_trans_get_iter(&trans, BTREE_ID_DIRENTS,
                                   POS(BCACHEFS_ROOT_INO, 0), 0);
-
-       hash_check_init(bch2_dirent_hash_desc, &trans, &h);
-
-       for_each_btree_key_continue(iter, 0, k) {
+retry:
+       for_each_btree_key_continue(iter, 0, k, ret) {
                struct bkey_s_c_dirent d;
                struct bch_inode_unpacked target;
                bool have_target;
                u64 d_inum;
 
-               ret = walk_inode(c, &w, k.k->p.inode);
+               ret = walk_inode(&trans, &w, k.k->p.inode);
                if (ret)
                        break;
 
                if (fsck_err_on(!w.have_inode, c,
                                "dirent in nonexisting directory:\n%s",
                                (bch2_bkey_val_to_text(&PBUF(buf), c,
-                                                      BTREE_ID_DIRENTS,
                                                       k), buf)) ||
                    fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
                                "dirent in non directory inode type %u:\n%s",
                                mode_to_type(w.inode.bi_mode),
                                (bch2_bkey_val_to_text(&PBUF(buf), c,
-                                                      BTREE_ID_DIRENTS,
                                                       k), buf))) {
-                       ret = bch2_btree_delete_at(iter, 0);
+                       ret = bch2_btree_delete_at(&trans, iter, 0);
                        if (ret)
                                goto err;
                        continue;
                }
 
                if (w.first_this_inode && w.have_inode)
-                       hash_check_set_inode(&h, c, &w.inode);
+                       hash_check_set_inode(&trans, &h, &w.inode);
 
-               ret = check_dirent_hash(&h, c, iter, &k);
+               ret = check_dirent_hash(&trans, &h, iter, &k);
                if (ret > 0) {
                        ret = 0;
                        continue;
@@ -556,7 +575,7 @@ static int check_dirents(struct bch_fs *c)
                if (ret)
                        goto fsck_err;
 
-               if (k.k->type != BCH_DIRENT)
+               if (k.k->type != KEY_TYPE_dirent)
                        continue;
 
                d = bkey_s_c_to_dirent(k);
@@ -576,7 +595,7 @@ static int check_dirents(struct bch_fs *c)
                                ".. dirent") ||
                    fsck_err_on(memchr(d.v->d_name, '/', name_len), c,
                                "dirent name has invalid chars")) {
-                       ret = remove_dirent(c, iter, d);
+                       ret = remove_dirent(&trans, d);
                        if (ret)
                                goto err;
                        continue;
@@ -585,15 +604,14 @@ static int check_dirents(struct bch_fs *c)
                if (fsck_err_on(d_inum == d.k->p.inode, c,
                                "dirent points to own directory:\n%s",
                                (bch2_bkey_val_to_text(&PBUF(buf), c,
-                                                      BTREE_ID_DIRENTS,
                                                       k), buf))) {
-                       ret = remove_dirent(c, iter, d);
+                       ret = remove_dirent(&trans, d);
                        if (ret)
                                goto err;
                        continue;
                }
 
-               ret = bch2_inode_find_by_inum(c, d_inum, &target);
+               ret = bch2_inode_find_by_inum_trans(&trans, d_inum, &target);
                if (ret && ret != -ENOENT)
                        break;
 
@@ -603,9 +621,8 @@ static int check_dirents(struct bch_fs *c)
                if (fsck_err_on(!have_target, c,
                                "dirent points to missing inode:\n%s",
                                (bch2_bkey_val_to_text(&PBUF(buf), c,
-                                                      BTREE_ID_DIRENTS,
                                                       k), buf))) {
-                       ret = remove_dirent(c, iter, d);
+                       ret = remove_dirent(&trans, d);
                        if (ret)
                                goto err;
                        continue;
@@ -617,7 +634,6 @@ static int check_dirents(struct bch_fs *c)
                                "incorrect d_type: should be %u:\n%s",
                                mode_to_type(target.bi_mode),
                                (bch2_bkey_val_to_text(&PBUF(buf), c,
-                                                      BTREE_ID_DIRENTS,
                                                       k), buf))) {
                        struct bkey_i_dirent *n;
 
@@ -630,17 +646,24 @@ static int check_dirents(struct bch_fs *c)
                        bkey_reassemble(&n->k_i, d.s_c);
                        n->v.d_type = mode_to_type(target.bi_mode);
 
-                       ret = bch2_btree_insert_at(c, NULL, NULL,
-                                       BTREE_INSERT_NOFAIL,
-                                       BTREE_INSERT_ENTRY(iter, &n->k_i));
+                       bch2_trans_update(&trans, iter, &n->k_i);
+
+                       ret = bch2_trans_commit(&trans, NULL, NULL,
+                                               BTREE_INSERT_NOFAIL|
+                                               BTREE_INSERT_LAZY_RW);
                        kfree(n);
                        if (ret)
                                goto err;
 
                }
        }
+
+       hash_stop_chain(&trans, &h);
 err:
 fsck_err:
+       if (ret == -EINTR)
+               goto retry;
+
        return bch2_trans_exit(&trans) ?: ret;
 }
 
@@ -659,38 +682,39 @@ static int check_xattrs(struct bch_fs *c)
 
        bch_verbose(c, "checking xattrs");
 
-       bch2_trans_init(&trans, c);
+       hash_check_init(&h);
 
-       bch2_trans_preload_iters(&trans);
+       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
 
        iter = bch2_trans_get_iter(&trans, BTREE_ID_XATTRS,
                                   POS(BCACHEFS_ROOT_INO, 0), 0);
-
-       hash_check_init(bch2_xattr_hash_desc, &trans, &h);
-
-       for_each_btree_key_continue(iter, 0, k) {
-               ret = walk_inode(c, &w, k.k->p.inode);
+retry:
+       for_each_btree_key_continue(iter, 0, k, ret) {
+               ret = walk_inode(&trans, &w, k.k->p.inode);
                if (ret)
                        break;
 
                if (fsck_err_on(!w.have_inode, c,
                                "xattr for missing inode %llu",
                                k.k->p.inode)) {
-                       ret = bch2_btree_delete_at(iter, 0);
+                       ret = bch2_btree_delete_at(&trans, iter, 0);
                        if (ret)
                                goto err;
                        continue;
                }
 
                if (w.first_this_inode && w.have_inode)
-                       hash_check_set_inode(&h, c, &w.inode);
+                       hash_check_set_inode(&trans, &h, &w.inode);
 
-               ret = hash_check_key(bch2_xattr_hash_desc, &h, c, iter, k);
+               ret = hash_check_key(&trans, bch2_xattr_hash_desc,
+                                    &h, iter, k);
                if (ret)
                        goto fsck_err;
        }
 err:
 fsck_err:
+       if (ret == -EINTR)
+               goto retry;
        return bch2_trans_exit(&trans) ?: ret;
 }
 
@@ -717,14 +741,16 @@ static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
 fsck_err:
        return ret;
 create_root:
-       bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO,
+       bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
                        0, NULL);
        root_inode->bi_inum = BCACHEFS_ROOT_INO;
 
        bch2_inode_pack(&packed, root_inode);
 
        return bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
-                                NULL, NULL, BTREE_INSERT_NOFAIL);
+                                NULL, NULL,
+                                BTREE_INSERT_NOFAIL|
+                                BTREE_INSERT_LAZY_RW);
 }
 
 /* Get lost+found, create if it doesn't exist: */
@@ -735,7 +761,6 @@ static int check_lostfound(struct bch_fs *c,
        struct qstr lostfound = QSTR("lost+found");
        struct bch_hash_info root_hash_info =
                bch2_hash_info_init(c, root_inode);
-       struct bkey_inode_buf packed;
        u64 inum;
        int ret;
 
@@ -763,30 +788,20 @@ static int check_lostfound(struct bch_fs *c,
 fsck_err:
        return ret;
 create_lostfound:
-       root_inode->bi_nlink++;
-
-       bch2_inode_pack(&packed, root_inode);
-
-       ret = bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
-                               NULL, NULL, BTREE_INSERT_NOFAIL);
-       if (ret)
-               return ret;
-
-       bch2_inode_init(c, lostfound_inode, 0, 0, S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO,
-                       0, root_inode);
-
-       ret = bch2_inode_create(c, lostfound_inode, BLOCKDEV_INODE_MAX, 0,
-                              &c->unused_inode_hint);
-       if (ret)
-               return ret;
-
-       ret = bch2_dirent_create(c, BCACHEFS_ROOT_INO, &root_hash_info, DT_DIR,
-                                &lostfound, lostfound_inode->bi_inum, NULL,
-                                BTREE_INSERT_NOFAIL);
+       bch2_inode_init_early(c, lostfound_inode);
+
+       ret = bch2_trans_do(c, NULL,
+                           BTREE_INSERT_ATOMIC|
+                           BTREE_INSERT_NOFAIL|
+                           BTREE_INSERT_LAZY_RW,
+               bch2_create_trans(&trans,
+                                 BCACHEFS_ROOT_INO, root_inode,
+                                 lostfound_inode, &lostfound,
+                                 0, 0, S_IFDIR|0700, 0, NULL, NULL));
        if (ret)
-               return ret;
+               bch_err(c, "error creating lost+found: %i", ret);
 
-       return 0;
+       return ret;
 }
 
 struct inode_bitmap {
@@ -860,13 +875,16 @@ static int check_directory_structure(struct bch_fs *c,
        struct inode_bitmap dirs_done = { NULL, 0 };
        struct pathbuf path = { 0, 0, NULL };
        struct pathbuf_entry *e;
-       struct btree_iter iter;
+       struct btree_trans trans;
+       struct btree_iter *iter;
        struct bkey_s_c k;
        struct bkey_s_c_dirent dirent;
        bool had_unreachable;
        u64 d_inum;
        int ret = 0;
 
+       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
+
        bch_verbose(c, "checking directory structure");
 
        /* DFS: */
@@ -880,9 +898,8 @@ restart_dfs:
        }
 
        ret = path_down(&path, BCACHEFS_ROOT_INO);
-       if (ret) {
-               return ret;
-       }
+       if (ret)
+               goto err;
 
        while (path.nr) {
 next:
@@ -891,14 +908,14 @@ next:
                if (e->offset == U64_MAX)
                        goto up;
 
-               for_each_btree_key(&iter, c, BTREE_ID_DIRENTS,
-                                  POS(e->inum, e->offset + 1), 0, k) {
+               for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
+                                  POS(e->inum, e->offset + 1), 0, k, ret) {
                        if (k.k->p.inode != e->inum)
                                break;
 
                        e->offset = k.k->p.offset;
 
-                       if (k.k->type != BCH_DIRENT)
+                       if (k.k->type != KEY_TYPE_dirent)
                                continue;
 
                        dirent = bkey_s_c_to_dirent(k);
@@ -911,7 +928,7 @@ next:
                        if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
                                        "directory %llu has multiple hardlinks",
                                        d_inum)) {
-                               ret = remove_dirent(c, &iter, dirent);
+                               ret = remove_dirent(&trans, dirent);
                                if (ret)
                                        goto err;
                                continue;
@@ -928,10 +945,14 @@ next:
                                goto err;
                        }
 
-                       bch2_btree_iter_unlock(&iter);
+                       ret = bch2_trans_iter_free(&trans, iter);
+                       if (ret) {
+                               bch_err(c, "btree error %i in fsck", ret);
+                               goto err;
+                       }
                        goto next;
                }
-               ret = bch2_btree_iter_unlock(&iter);
+               ret = bch2_trans_iter_free(&trans, iter) ?: ret;
                if (ret) {
                        bch_err(c, "btree error %i in fsck", ret);
                        goto err;
@@ -940,20 +961,25 @@ up:
                path.nr--;
        }
 
-       for_each_btree_key(&iter, c, BTREE_ID_INODES, POS_MIN, 0, k) {
-               if (k.k->type != BCH_INODE_FS)
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS_MIN, 0);
+retry:
+       for_each_btree_key_continue(iter, 0, k, ret) {
+               if (k.k->type != KEY_TYPE_inode)
                        continue;
 
                if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
                        continue;
 
-               if (!bch2_empty_dir(c, k.k->p.inode))
+               ret = bch2_empty_dir_trans(&trans, k.k->p.inode);
+               if (ret == -EINTR)
+                       goto retry;
+               if (!ret)
                        continue;
 
                if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.inode), c,
                                "unreachable directory found (inum %llu)",
                                k.k->p.inode)) {
-                       bch2_btree_iter_unlock(&iter);
+                       bch2_trans_unlock(&trans);
 
                        ret = reattach_inode(c, lostfound_inode, k.k->p.inode);
                        if (ret) {
@@ -963,7 +989,7 @@ up:
                        had_unreachable = true;
                }
        }
-       ret = bch2_btree_iter_unlock(&iter);
+       bch2_trans_iter_free(&trans, iter);
        if (ret)
                goto err;
 
@@ -975,15 +1001,12 @@ up:
                memset(&path, 0, sizeof(path));
                goto restart_dfs;
        }
-
-out:
+err:
+fsck_err:
+       ret = bch2_trans_exit(&trans) ?: ret;
        kfree(dirs_done.bits);
        kfree(path.entries);
        return ret;
-err:
-fsck_err:
-       ret = bch2_btree_iter_unlock(&iter) ?: ret;
-       goto out;
 }
 
 struct nlink {
@@ -1004,7 +1027,7 @@ static void inc_link(struct bch_fs *c, nlink_table *links,
 
        link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
        if (!link) {
-               bch_verbose(c, "allocation failed during fs gc - will need another pass");
+               bch_verbose(c, "allocation failed during fsck - will need another pass");
                *range_end = inum;
                return;
        }
@@ -1019,17 +1042,20 @@ noinline_for_stack
 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
                               u64 range_start, u64 *range_end)
 {
-       struct btree_iter iter;
+       struct btree_trans trans;
+       struct btree_iter *iter;
        struct bkey_s_c k;
        struct bkey_s_c_dirent d;
        u64 d_inum;
        int ret;
 
+       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
+
        inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
 
-       for_each_btree_key(&iter, c, BTREE_ID_DIRENTS, POS_MIN, 0, k) {
+       for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS, POS_MIN, 0, k, ret) {
                switch (k.k->type) {
-               case BCH_DIRENT:
+               case KEY_TYPE_dirent:
                        d = bkey_s_c_to_dirent(k);
                        d_inum = le64_to_cpu(d.v->d_inum);
 
@@ -1043,41 +1069,22 @@ static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
                        break;
                }
 
-               bch2_btree_iter_cond_resched(&iter);
+               bch2_trans_cond_resched(&trans);
        }
-       ret = bch2_btree_iter_unlock(&iter);
+       ret = bch2_trans_exit(&trans) ?: ret;
        if (ret)
-               bch_err(c, "error in fs gc: btree error %i while walking dirents", ret);
+               bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
 
        return ret;
 }
 
-s64 bch2_count_inode_sectors(struct bch_fs *c, u64 inum)
-{
-       struct btree_iter iter;
-       struct bkey_s_c k;
-       u64 sectors = 0;
-
-       for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS(inum, 0), 0, k) {
-               if (k.k->p.inode != inum)
-                       break;
-
-               if (bkey_extent_is_allocation(k.k))
-                       sectors += k.k->size;
-       }
-
-       return bch2_btree_iter_unlock(&iter) ?: sectors;
-}
-
 static int check_inode_nlink(struct bch_fs *c,
                             struct bch_inode_unpacked *lostfound_inode,
                             struct bch_inode_unpacked *u,
                             struct nlink *link,
                             bool *do_update)
 {
-       u32 i_nlink = u->bi_flags & BCH_INODE_UNLINKED
-               ? 0
-               : u->bi_nlink + nlink_bias(u->bi_mode);
+       u32 i_nlink = bch2_inode_nlink_get(u);
        u32 real_i_nlink =
                link->count * nlink_bias(u->bi_mode) +
                link->dir_count;
@@ -1156,31 +1163,28 @@ static int check_inode_nlink(struct bch_fs *c,
                            u->bi_inum, i_nlink, real_i_nlink);
 set_i_nlink:
        if (i_nlink != real_i_nlink) {
-               if (real_i_nlink) {
-                       u->bi_nlink = real_i_nlink - nlink_bias(u->bi_mode);
-                       u->bi_flags &= ~BCH_INODE_UNLINKED;
-               } else {
-                       u->bi_nlink = 0;
-                       u->bi_flags |= BCH_INODE_UNLINKED;
-               }
-
+               bch2_inode_nlink_set(u, real_i_nlink);
                *do_update = true;
        }
 fsck_err:
        return ret;
 }
 
-static int check_inode(struct bch_fs *c,
+static int check_inode(struct btree_trans *trans,
                       struct bch_inode_unpacked *lostfound_inode,
                       struct btree_iter *iter,
                       struct bkey_s_c_inode inode,
                       struct nlink *link)
 {
+       struct bch_fs *c = trans->c;
        struct bch_inode_unpacked u;
        bool do_update = false;
        int ret = 0;
 
        ret = bch2_inode_unpack(inode, &u);
+
+       bch2_trans_unlock(trans);
+
        if (bch2_fs_inconsistent_on(ret, c,
                         "error unpacking inode %llu in fsck",
                         inode.k->p.inode))
@@ -1193,22 +1197,22 @@ static int check_inode(struct bch_fs *c,
                        return ret;
        }
 
-       if (u.bi_flags & BCH_INODE_UNLINKED) {
+       if (u.bi_flags & BCH_INODE_UNLINKED &&
+           (!c->sb.clean ||
+            fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
+                     u.bi_inum))) {
                bch_verbose(c, "deleting inode %llu", u.bi_inum);
 
                ret = bch2_inode_rm(c, u.bi_inum);
                if (ret)
-                       bch_err(c, "error in fs gc: error %i "
-                               "while deleting inode", ret);
+                       bch_err(c, "error in fsck: error %i while deleting inode", ret);
                return ret;
        }
 
-       if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY) {
-               fsck_err_on(c->sb.clean, c,
-                           "filesystem marked clean, "
-                           "but inode %llu has i_size dirty",
-                           u.bi_inum);
-
+       if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
+           (!c->sb.clean ||
+            fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
+                     u.bi_inum))) {
                bch_verbose(c, "truncating inode %llu", u.bi_inum);
 
                /*
@@ -1218,8 +1222,7 @@ static int check_inode(struct bch_fs *c,
 
                ret = bch2_inode_truncate(c, u.bi_inum, u.bi_size);
                if (ret) {
-                       bch_err(c, "error in fs gc: error %i "
-                               "truncating inode", ret);
+                       bch_err(c, "error in fsck: error %i truncating inode", ret);
                        return ret;
                }
 
@@ -1233,21 +1236,18 @@ static int check_inode(struct bch_fs *c,
                do_update = true;
        }
 
-       if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY) {
+       if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
+           (!c->sb.clean ||
+            fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
+                     u.bi_inum))) {
                s64 sectors;
 
-               fsck_err_on(c->sb.clean, c,
-                           "filesystem marked clean, "
-                           "but inode %llu has i_sectors dirty",
-                           u.bi_inum);
-
                bch_verbose(c, "recounting sectors for inode %llu",
                            u.bi_inum);
 
-               sectors = bch2_count_inode_sectors(c, u.bi_inum);
+               sectors = bch2_count_inode_sectors(trans, u.bi_inum);
                if (sectors < 0) {
-                       bch_err(c, "error in fs gc: error %i "
-                               "recounting inode sectors",
+                       bch_err(c, "error in fsck: error %i recounting inode sectors",
                                (int) sectors);
                        return sectors;
                }
@@ -1261,12 +1261,13 @@ static int check_inode(struct bch_fs *c,
                struct bkey_inode_buf p;
 
                bch2_inode_pack(&p, &u);
+               bch2_trans_update(trans, iter, &p.inode.k_i);
 
-               ret = bch2_btree_insert_at(c, NULL, NULL,
-                                         BTREE_INSERT_NOFAIL,
-                                         BTREE_INSERT_ENTRY(iter, &p.inode.k_i));
+               ret = bch2_trans_commit(trans, NULL, NULL,
+                                       BTREE_INSERT_NOFAIL|
+                                       BTREE_INSERT_LAZY_RW);
                if (ret && ret != -EINTR)
-                       bch_err(c, "error in fs gc: error %i "
+                       bch_err(c, "error in fsck: error %i "
                                "updating inode", ret);
        }
 fsck_err:
@@ -1279,25 +1280,29 @@ static int bch2_gc_walk_inodes(struct bch_fs *c,
                               nlink_table *links,
                               u64 range_start, u64 range_end)
 {
-       struct btree_iter iter;
+       struct btree_trans trans;
+       struct btree_iter *iter;
        struct bkey_s_c k;
        struct nlink *link, zero_links = { 0, 0 };
        struct genradix_iter nlinks_iter;
        int ret = 0, ret2 = 0;
        u64 nlinks_pos;
 
-       bch2_btree_iter_init(&iter, c, BTREE_ID_INODES, POS(range_start, 0), 0);
+       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
+
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES,
+                                  POS(range_start, 0), 0);
        nlinks_iter = genradix_iter_init(links, 0);
 
-       while ((k = bch2_btree_iter_peek(&iter)).k &&
-              !btree_iter_err(k)) {
+       while ((k = bch2_btree_iter_peek(iter)).k &&
+              !(ret2 = bkey_err(k))) {
 peek_nlinks:   link = genradix_iter_peek(&nlinks_iter, links);
 
-               if (!link && (!k.k || iter.pos.inode >= range_end))
+               if (!link && (!k.k || iter->pos.inode >= range_end))
                        break;
 
                nlinks_pos = range_start + nlinks_iter.pos;
-               if (iter.pos.inode > nlinks_pos) {
+               if (iter->pos.inode > nlinks_pos) {
                        /* Should have been caught by dirents pass: */
                        need_fsck_err_on(link && link->count, c,
                                "missing inode %llu (nlink %u)",
@@ -1306,24 +1311,15 @@ peek_nlinks:    link = genradix_iter_peek(&nlinks_iter, links);
                        goto peek_nlinks;
                }
 
-               if (iter.pos.inode < nlinks_pos || !link)
+               if (iter->pos.inode < nlinks_pos || !link)
                        link = &zero_links;
 
-               if (k.k && k.k->type == BCH_INODE_FS) {
-                       /*
-                        * Avoid potential deadlocks with iter for
-                        * truncate/rm/etc.:
-                        */
-                       bch2_btree_iter_unlock(&iter);
-
-                       ret = check_inode(c, lostfound_inode, &iter,
+               if (k.k && k.k->type == KEY_TYPE_inode) {
+                       ret = check_inode(&trans, lostfound_inode, iter,
                                          bkey_s_c_to_inode(k), link);
                        BUG_ON(ret == -EINTR);
                        if (ret)
                                break;
-
-                       if (link->count)
-                               atomic_long_inc(&c->nr_inodes);
                } else {
                        /* Should have been caught by dirents pass: */
                        need_fsck_err_on(link->count, c,
@@ -1331,16 +1327,17 @@ peek_nlinks:    link = genradix_iter_peek(&nlinks_iter, links);
                                nlinks_pos, link->count);
                }
 
-               if (nlinks_pos == iter.pos.inode)
+               if (nlinks_pos == iter->pos.inode)
                        genradix_iter_advance(&nlinks_iter, links);
 
-               bch2_btree_iter_next(&iter);
-               bch2_btree_iter_cond_resched(&iter);
+               bch2_btree_iter_next(iter);
+               bch2_trans_cond_resched(&trans);
        }
 fsck_err:
-       ret2 = bch2_btree_iter_unlock(&iter);
+       bch2_trans_exit(&trans);
+
        if (ret2)
-               bch_err(c, "error in fs gc: btree error %i while walking inodes", ret2);
+               bch_err(c, "error in fsck: btree error %i while walking inodes", ret2);
 
        return ret ?: ret2;
 }
@@ -1381,103 +1378,59 @@ static int check_inode_nlinks(struct bch_fs *c,
        return ret;
 }
 
-noinline_for_stack
-static int check_inodes_fast(struct bch_fs *c)
-{
-       struct btree_iter iter;
-       struct bkey_s_c k;
-       struct bkey_s_c_inode inode;
-       unsigned long nr_inodes = 0;
-       int ret = 0;
-
-       for_each_btree_key(&iter, c, BTREE_ID_INODES, POS_MIN, 0, k) {
-               if (k.k->type != BCH_INODE_FS)
-                       continue;
-
-               inode = bkey_s_c_to_inode(k);
-
-               if (!(inode.v->bi_flags & BCH_INODE_UNLINKED))
-                       nr_inodes++;
-
-               if (inode.v->bi_flags &
-                   (BCH_INODE_I_SIZE_DIRTY|
-                    BCH_INODE_I_SECTORS_DIRTY|
-                    BCH_INODE_UNLINKED)) {
-                       fsck_err_on(c->sb.clean, c,
-                               "filesystem marked clean but found inode %llu with flags %x",
-                               inode.k->p.inode, inode.v->bi_flags);
-                       ret = check_inode(c, NULL, &iter, inode, NULL);
-                       BUG_ON(ret == -EINTR);
-                       if (ret)
-                               break;
-               }
-       }
-       atomic_long_set(&c->nr_inodes, nr_inodes);
-fsck_err:
-       return bch2_btree_iter_unlock(&iter) ?: ret;
-}
-
 /*
  * Checks for inconsistencies that shouldn't happen, unless we have a bug.
  * Doesn't fix them yet, mainly because they haven't yet been observed:
  */
-static int bch2_fsck_full(struct bch_fs *c)
+int bch2_fsck_full(struct bch_fs *c)
 {
        struct bch_inode_unpacked root_inode, lostfound_inode;
-       int ret;
 
-       bch_verbose(c, "starting fsck:");
-       ret =   check_extents(c) ?:
+       return  check_extents(c) ?:
                check_dirents(c) ?:
                check_xattrs(c) ?:
                check_root(c, &root_inode) ?:
                check_lostfound(c, &root_inode, &lostfound_inode) ?:
                check_directory_structure(c, &lostfound_inode) ?:
                check_inode_nlinks(c, &lostfound_inode);
-
-       bch2_flush_fsck_errs(c);
-       bch_verbose(c, "fsck done");
-
-       return ret;
 }
 
-static int bch2_fsck_inode_nlink(struct bch_fs *c)
+int bch2_fsck_inode_nlink(struct bch_fs *c)
 {
        struct bch_inode_unpacked root_inode, lostfound_inode;
-       int ret;
 
-       bch_verbose(c, "checking inode link counts:");
-       ret =   check_root(c, &root_inode) ?:
+       return  check_root(c, &root_inode) ?:
                check_lostfound(c, &root_inode, &lostfound_inode) ?:
                check_inode_nlinks(c, &lostfound_inode);
-
-       bch2_flush_fsck_errs(c);
-       bch_verbose(c, "done");
-
-       return ret;
 }
 
-static int bch2_fsck_walk_inodes_only(struct bch_fs *c)
+int bch2_fsck_walk_inodes_only(struct bch_fs *c)
 {
+       struct btree_trans trans;
+       struct btree_iter *iter;
+       struct bkey_s_c k;
+       struct bkey_s_c_inode inode;
        int ret;
 
-       bch_verbose(c, "walking inodes:");
-       ret = check_inodes_fast(c);
-
-       bch2_flush_fsck_errs(c);
-       bch_verbose(c, "done");
+       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
 
-       return ret;
-}
+       for_each_btree_key(&trans, iter, BTREE_ID_INODES, POS_MIN, 0, k, ret) {
+               if (k.k->type != KEY_TYPE_inode)
+                       continue;
 
-int bch2_fsck(struct bch_fs *c)
-{
-       if (c->opts.fsck)
-               return bch2_fsck_full(c);
+               inode = bkey_s_c_to_inode(k);
 
-       if (!c->sb.clean &&
-           !(c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK)))
-               return bch2_fsck_inode_nlink(c);
+               if (inode.v->bi_flags &
+                   (BCH_INODE_I_SIZE_DIRTY|
+                    BCH_INODE_I_SECTORS_DIRTY|
+                    BCH_INODE_UNLINKED)) {
+                       ret = check_inode(&trans, NULL, iter, inode, NULL);
+                       BUG_ON(ret == -EINTR);
+                       if (ret)
+                               break;
+               }
+       }
+       BUG_ON(ret == -EINTR);
 
-       return bch2_fsck_walk_inodes_only(c);
+       return bch2_trans_exit(&trans) ?: ret;
 }