]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/fsck.c
Update bcachefs sources to 18686af684 bcachefs: Inode backpointers
[bcachefs-tools-debian] / libbcachefs / fsck.c
index e3738757b6a0cb9f81e87384f13b5a71c46cfdd1..62788ae15eff3f9cf5078afaa93f9bd24f89f2db 100644 (file)
@@ -1,10 +1,11 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include "bcachefs.h"
+#include "bkey_buf.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"
@@ -23,7 +24,7 @@ static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum)
        u64 sectors = 0;
        int ret;
 
-       for_each_btree_key(trans, iter, BTREE_ID_EXTENTS,
+       for_each_btree_key(trans, iter, BTREE_ID_extents,
                           POS(inum, 0), 0, k, ret) {
                if (k.k->p.inode != inum)
                        break;
@@ -37,8 +38,8 @@ static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum)
        return ret ?: sectors;
 }
 
-static int remove_dirent(struct btree_trans *trans,
-                        struct bkey_s_c_dirent dirent)
+static int __remove_dirent(struct btree_trans *trans,
+                          struct bkey_s_c_dirent dirent)
 {
        struct bch_fs *c = trans->c;
        struct qstr name;
@@ -49,40 +50,46 @@ static int remove_dirent(struct btree_trans *trans,
        char *buf;
 
        name.len = bch2_dirent_name_bytes(dirent);
-       buf = kmalloc(name.len + 1, GFP_KERNEL);
-       if (!buf)
-               return -ENOMEM;
+       buf = bch2_trans_kmalloc(trans, name.len + 1);
+       if (IS_ERR(buf))
+               return PTR_ERR(buf);
 
        memcpy(buf, dirent.v->d_name, name.len);
        buf[name.len] = '\0';
        name.name = buf;
 
-       /* 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) {
+       ret = __bch2_inode_find_by_inum_trans(trans, dir_inum, &dir_inode, 0);
+       if (ret && ret != -EINTR)
                bch_err(c, "remove_dirent: err %i looking up directory inode", ret);
-               goto err;
-       }
+       if (ret)
+               return ret;
 
        dir_hash_info = bch2_hash_info_init(c, &dir_inode);
 
-       ret = bch2_dirent_delete(c, dir_inum, &dir_hash_info, &name, NULL);
-       if (ret)
+       ret = bch2_hash_delete(trans, bch2_dirent_hash_desc,
+                              &dir_hash_info, dir_inum, &name);
+       if (ret && ret != -EINTR)
                bch_err(c, "remove_dirent: err %i deleting dirent", ret);
-err:
-       kfree(buf);
-       return ret;
+       if (ret)
+               return ret;
+
+       return 0;
+}
+
+static int remove_dirent(struct btree_trans *trans,
+                        struct bkey_s_c_dirent dirent)
+{
+       return __bch2_trans_do(trans, NULL, NULL,
+                              BTREE_INSERT_NOFAIL|
+                              BTREE_INSERT_LAZY_RW,
+                              __remove_dirent(trans, dirent));
 }
 
 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 dir_u, inode_u;
        char name_buf[20];
        struct qstr name;
        int ret;
@@ -90,30 +97,13 @@ 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|
-                               BTREE_INSERT_LAZY_RW);
-       if (ret) {
-               bch_err(c, "error %i reattaching inode %llu while updating lost+found",
-                       ret, inum);
-               return ret;
-       }
+       ret = bch2_trans_do(c, NULL, NULL,
+                           BTREE_INSERT_LAZY_RW,
+               bch2_link_trans(&trans, lostfound_inode->bi_inum,
+                               inum, &dir_u, &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|
-                                BTREE_INSERT_LAZY_RW);
-       if (ret) {
-               bch_err(c, "error %i reattaching inode %llu while creating new dirent",
-                       ret, inum);
-               return ret;
-       }
        return ret;
 }
 
@@ -136,8 +126,8 @@ static int walk_inode(struct btree_trans *trans,
                      struct inode_walker *w, u64 inum)
 {
        if (inum != w->cur_inum) {
-               int ret = bch2_inode_find_by_inum_trans(trans, inum,
-                                                       &w->inode);
+               int ret = __bch2_inode_find_by_inum_trans(trans, inum,
+                                                         &w->inode, 0);
 
                if (ret && ret != -ENOENT)
                        return ret;
@@ -165,6 +155,7 @@ struct hash_check {
 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,
@@ -188,27 +179,21 @@ static int hash_redo_key(const struct bch_hash_desc desc,
                         struct btree_iter *k_iter, struct bkey_s_c k,
                         u64 hashed)
 {
+       struct bkey_i delete;
        struct bkey_i *tmp;
-       int ret = 0;
 
-       tmp = kmalloc(bkey_bytes(k.k), GFP_KERNEL);
-       if (!tmp)
-               return -ENOMEM;
+       tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
+       if (IS_ERR(tmp))
+               return PTR_ERR(tmp);
 
        bkey_reassemble(tmp, k);
 
-       ret = bch2_btree_delete_at(trans, k_iter, 0);
-       if (ret)
-               goto err;
+       bkey_init(&delete.k);
+       delete.k.p = k_iter->pos;
+       bch2_trans_update(trans, k_iter, &delete, 0);
 
-       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;
+       return bch2_hash_set(trans, desc, &h->info, k_iter->pos.inode,
+                            tmp, 0);
 }
 
 static int fsck_hash_delete_at(struct btree_trans *trans,
@@ -220,7 +205,6 @@ static int fsck_hash_delete_at(struct btree_trans *trans,
 retry:
        ret   = bch2_hash_delete_at(trans, desc, info, iter) ?:
                bch2_trans_commit(trans, NULL, NULL,
-                                 BTREE_INSERT_ATOMIC|
                                  BTREE_INSERT_NOFAIL|
                                  BTREE_INSERT_LAZY_RW);
        if (ret == -EINTR) {
@@ -246,9 +230,8 @@ static int hash_check_duplicates(struct btree_trans *trans,
                return 0;
 
        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;
 
@@ -274,17 +257,15 @@ static void hash_set_chain_start(struct btree_trans *trans,
                        struct hash_check *h,
                        struct btree_iter *k_iter, struct bkey_s_c k)
 {
-       bool hole = (k.k->type != KEY_TYPE_whiteout &&
+       bool hole = (k.k->type != KEY_TYPE_hash_whiteout &&
                     k.k->type != desc.key_type);
 
        if (hole || k.k->p.offset > h->chain_end + 1)
                hash_stop_chain(trans, h);
 
        if (!hole) {
-               if (!h->chain) {
+               if (!h->chain)
                        h->chain = bch2_trans_copy_iter(trans, k_iter);
-                       BUG_ON(IS_ERR(h->chain));
-               }
 
                h->chain_end = k.k->p.offset;
        }
@@ -330,14 +311,15 @@ static int hash_check_key(struct btree_trans *trans,
                        "hashed to %llu chain starts at %llu\n%s",
                        desc.btree_id, k.k->p.offset,
                        hashed, h->chain->pos.offset,
-                       (bch2_bkey_val_to_text(&PBUF(buf), c,
-                                              k), buf))) {
-               ret = hash_redo_key(desc, trans, h, k_iter, k, hashed);
+                       (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf))) {
+               ret = __bch2_trans_do(trans, NULL, NULL,
+                                     BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
+                       hash_redo_key(desc, trans, h, k_iter, k, hashed));
                if (ret) {
                        bch_err(c, "hash_redo_key err %i", ret);
                        return ret;
                }
-               return 1;
+               return -EINTR;
        }
 
        ret = hash_check_duplicates(trans, desc, h, k_iter, k);
@@ -393,11 +375,10 @@ static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
 
        if (fsck_err(c, "dirent with junk at end, was %s (%zu) now %s (%u)",
                     buf, strlen(buf), d->v.d_name, len)) {
-               bch2_trans_update(trans, BTREE_INSERT_ENTRY(iter, &d->k_i));
-
-               ret = bch2_trans_commit(trans, NULL, NULL,
-                                       BTREE_INSERT_NOFAIL|
-                                       BTREE_INSERT_LAZY_RW);
+               ret = __bch2_trans_do(trans, NULL, NULL,
+                                     BTREE_INSERT_NOFAIL|
+                                     BTREE_INSERT_LAZY_RW,
+                       (bch2_trans_update(trans, iter, &d->k_i, 0), 0));
                if (ret)
                        goto err;
 
@@ -415,12 +396,14 @@ err_redo:
        if (fsck_err(c, "cannot fix dirent by removing trailing garbage %s (%zu)\n"
                     "hash table key at wrong offset: btree %u, offset %llu, "
                     "hashed to %llu chain starts at %llu\n%s",
-                    buf, strlen(buf), BTREE_ID_DIRENTS,
+                    buf, strlen(buf), BTREE_ID_dirents,
                     k->k->p.offset, hash, h->chain->pos.offset,
                     (bch2_bkey_val_to_text(&PBUF(buf), c,
                                            *k), buf))) {
-               ret = hash_redo_key(bch2_dirent_hash_desc, trans,
-                                   h, iter, *k, hash);
+               ret = __bch2_trans_do(trans, NULL, NULL,
+                                     BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
+                       hash_redo_key(bch2_dirent_hash_desc, trans,
+                                     h, iter, *k, hash));
                if (ret)
                        bch_err(c, "hash_redo_key err %i", ret);
                else
@@ -430,11 +413,39 @@ err_redo:
        goto err;
 }
 
-static int bch2_inode_truncate(struct bch_fs *c, u64 inode_nr, u64 new_size)
+static int fix_overlapping_extent(struct btree_trans *trans,
+                                      struct bkey_s_c k, struct bpos cut_at)
 {
-       return bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
-                       POS(inode_nr, round_up(new_size, block_bytes(c)) >> 9),
-                       POS(inode_nr + 1, 0), NULL);
+       struct btree_iter *iter;
+       struct bkey_i *u;
+       int ret;
+
+       u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
+       ret = PTR_ERR_OR_ZERO(u);
+       if (ret)
+               return ret;
+
+       bkey_reassemble(u, k);
+       bch2_cut_front(cut_at, u);
+
+
+       /*
+        * We don't want to go through the extent_handle_overwrites path:
+        *
+        * XXX: this is going to screw up disk accounting, extent triggers
+        * assume things about extent overwrites - we should be running the
+        * triggers manually here
+        */
+       iter = bch2_trans_get_iter(trans, BTREE_ID_extents, u->k.p,
+                                  BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
+
+       BUG_ON(iter->flags & BTREE_ITER_IS_EXTENTS);
+       bch2_trans_update(trans, iter, u, BTREE_TRIGGER_NORUN);
+       bch2_trans_iter_put(trans, iter);
+
+       return bch2_trans_commit(trans, NULL, NULL,
+                                BTREE_INSERT_NOFAIL|
+                                BTREE_INSERT_LAZY_RW);
 }
 
 /*
@@ -448,83 +459,101 @@ static int check_extents(struct bch_fs *c)
        struct btree_trans trans;
        struct btree_iter *iter;
        struct bkey_s_c k;
-       u64 i_sectors;
+       struct bkey_buf prev;
+       u64 i_sectors = 0;
        int ret = 0;
 
+       bch2_bkey_buf_init(&prev);
+       prev.k->k = KEY(0, 0, 0);
        bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
 
        bch_verbose(c, "checking extents");
 
-       iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
-                                  POS(BCACHEFS_ROOT_INO, 0), 0);
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_extents,
+                                  POS(BCACHEFS_ROOT_INO, 0),
+                                  BTREE_ITER_INTENT);
 retry:
-       for_each_btree_key_continue(iter, 0, k) {
-               ret = walk_inode(&trans, &w, k.k->p.inode);
-               if (ret)
-                       break;
+       while ((k = bch2_btree_iter_peek(iter)).k &&
+              !(ret = bkey_err(k))) {
+               if (w.have_inode &&
+                   w.cur_inum != k.k->p.inode &&
+                   !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
+                   fsck_err_on(w.inode.bi_sectors != i_sectors, c,
+                               "inode %llu has incorrect i_sectors: got %llu, should be %llu",
+                               w.inode.bi_inum,
+                               w.inode.bi_sectors, i_sectors)) {
+                       struct btree_iter *inode_iter =
+                               bch2_trans_get_iter(&trans, BTREE_ID_inodes,
+                                                   POS(0, w.cur_inum),
+                                                   BTREE_ITER_INTENT);
 
-               if (fsck_err_on(!w.have_inode, c,
-                       "extent type %u for missing inode %llu",
-                       k.k->type, k.k->p.inode) ||
-                   fsck_err_on(w.have_inode &&
-                       !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_trans_unlock(&trans);
+                       w.inode.bi_sectors = i_sectors;
 
-                       ret = bch2_inode_truncate(c, k.k->p.inode, 0);
+                       ret = __bch2_trans_do(&trans, NULL, NULL,
+                                             BTREE_INSERT_NOFAIL|
+                                             BTREE_INSERT_LAZY_RW,
+                                             bch2_inode_write(&trans, inode_iter, &w.inode));
+                       bch2_trans_iter_put(&trans, inode_iter);
                        if (ret)
-                               goto err;
-                       continue;
+                               break;
                }
 
-               if (fsck_err_on(w.first_this_inode &&
-                       w.have_inode &&
-                       !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
-                       w.inode.bi_sectors !=
-                       (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;
+               if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
+                       char buf1[200];
+                       char buf2[200];
 
-                       w.inode.bi_sectors = i_sectors;
+                       bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
+                       bch2_bkey_val_to_text(&PBUF(buf2), c, k);
 
-                       bch2_trans_unlock(&trans);
+                       if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2))
+                               return fix_overlapping_extent(&trans, k, prev.k->k.p) ?: -EINTR;
+               }
 
-                       bch2_inode_pack(&p, &w.inode);
+               ret = walk_inode(&trans, &w, k.k->p.inode);
+               if (ret)
+                       break;
 
-                       ret = bch2_btree_insert(c, BTREE_ID_INODES,
-                                               &p.inode.k_i, NULL, NULL,
-                                               BTREE_INSERT_NOFAIL|
-                                               BTREE_INSERT_LAZY_RW);
-                       if (ret) {
-                               bch_err(c, "error in fsck: error %i updating inode", ret);
-                               goto err;
-                       }
+               if (w.first_this_inode)
+                       i_sectors = 0;
 
-                       /* revalidate iterator: */
-                       k = bch2_btree_iter_peek(iter);
+               if (fsck_err_on(!w.have_inode, c,
+                               "extent type %u for missing inode %llu",
+                               k.k->type, k.k->p.inode) ||
+                   fsck_err_on(w.have_inode &&
+                               !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_fs_lazy_rw(c);
+                       return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
+                                                      POS(k.k->p.inode, 0),
+                                                      POS(k.k->p.inode, U64_MAX),
+                                                      NULL) ?: -EINTR;
                }
 
                if (fsck_err_on(w.have_inode &&
-                       !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
-                       k.k->type != KEY_TYPE_reservation &&
-                       k.k->p.offset > round_up(w.inode.bi_size, PAGE_SIZE) >> 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_trans_unlock(&trans);
-
-                       ret = bch2_inode_truncate(c, k.k->p.inode,
-                                                 w.inode.bi_size);
-                       if (ret)
-                               goto err;
-                       continue;
+                               !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
+                               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_fs_lazy_rw(c);
+                       return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
+                                       POS(k.k->p.inode, round_up(w.inode.bi_size, block_bytes(c))),
+                                       POS(k.k->p.inode, U64_MAX),
+                                       NULL) ?: -EINTR;
                }
+
+               if (bkey_extent_is_allocation(k.k))
+                       i_sectors += k.k->size;
+               bch2_bkey_buf_reassemble(&prev, c, k);
+
+               bch2_btree_iter_advance(iter);
        }
-err:
 fsck_err:
        if (ret == -EINTR)
                goto retry;
+       bch2_trans_iter_put(&trans, iter);
+       bch2_bkey_buf_exit(&prev, c);
        return bch2_trans_exit(&trans) ?: ret;
 }
 
@@ -550,10 +579,11 @@ static int check_dirents(struct bch_fs *c)
 
        hash_check_init(&h);
 
-       iter = bch2_trans_get_iter(&trans, BTREE_ID_DIRENTS,
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_dirents,
                                   POS(BCACHEFS_ROOT_INO, 0), 0);
 retry:
-       for_each_btree_key_continue(iter, 0, k) {
+       while ((k = bch2_btree_iter_peek(iter)).k &&
+              !(ret = bkey_err(k))) {
                struct bkey_s_c_dirent d;
                struct bch_inode_unpacked target;
                bool have_target;
@@ -628,7 +658,7 @@ retry:
                        continue;
                }
 
-               ret = bch2_inode_find_by_inum_trans(&trans, d_inum, &target);
+               ret = __bch2_inode_find_by_inum_trans(&trans, d_inum, &target, 0);
                if (ret && ret != -ENOENT)
                        break;
 
@@ -645,6 +675,39 @@ retry:
                        continue;
                }
 
+               if (!target.bi_nlink &&
+                   !(target.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) &&
+                   (target.bi_dir != k.k->p.inode ||
+                    target.bi_dir_offset != k.k->p.offset) &&
+                   (fsck_err_on(c->sb.version >= bcachefs_metadata_version_inode_backpointers, c,
+                                "inode %llu has wrong backpointer:\n"
+                                "got       %llu:%llu\n"
+                                "should be %llu:%llu",
+                                d_inum,
+                                target.bi_dir,
+                                target.bi_dir_offset,
+                                k.k->p.inode,
+                                k.k->p.offset) ||
+                    c->opts.version_upgrade)) {
+                       struct bkey_inode_buf p;
+
+                       target.bi_dir           = k.k->p.inode;
+                       target.bi_dir_offset    = k.k->p.offset;
+                       bch2_trans_unlock(&trans);
+
+                       bch2_inode_pack(c, &p, &target);
+
+                       ret = bch2_btree_insert(c, BTREE_ID_inodes,
+                                               &p.inode.k_i, NULL, NULL,
+                                               BTREE_INSERT_NOFAIL|
+                                               BTREE_INSERT_LAZY_RW);
+                       if (ret) {
+                               bch_err(c, "error in fsck: error %i updating inode", ret);
+                               goto err;
+                       }
+                       continue;
+               }
+
                if (fsck_err_on(have_target &&
                                d.v->d_type !=
                                mode_to_type(target.bi_mode), c,
@@ -663,17 +726,17 @@ retry:
                        bkey_reassemble(&n->k_i, d.s_c);
                        n->v.d_type = mode_to_type(target.bi_mode);
 
-                       bch2_trans_update(&trans,
-                               BTREE_INSERT_ENTRY(iter, &n->k_i));
-
-                       ret = bch2_trans_commit(&trans, NULL, NULL,
-                                               BTREE_INSERT_NOFAIL|
-                                               BTREE_INSERT_LAZY_RW);
+                       ret = __bch2_trans_do(&trans, NULL, NULL,
+                                             BTREE_INSERT_NOFAIL|
+                                             BTREE_INSERT_LAZY_RW,
+                               (bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
                        kfree(n);
                        if (ret)
                                goto err;
 
                }
+
+               bch2_btree_iter_advance(iter);
        }
 
        hash_stop_chain(&trans, &h);
@@ -682,6 +745,8 @@ fsck_err:
        if (ret == -EINTR)
                goto retry;
 
+       bch2_trans_iter_put(&trans, h.chain);
+       bch2_trans_iter_put(&trans, iter);
        return bch2_trans_exit(&trans) ?: ret;
 }
 
@@ -704,10 +769,11 @@ static int check_xattrs(struct bch_fs *c)
 
        bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
 
-       iter = bch2_trans_get_iter(&trans, BTREE_ID_XATTRS,
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_xattrs,
                                   POS(BCACHEFS_ROOT_INO, 0), 0);
 retry:
-       for_each_btree_key_continue(iter, 0, k) {
+       while ((k = bch2_btree_iter_peek(iter)).k &&
+              !(ret = bkey_err(k))) {
                ret = walk_inode(&trans, &w, k.k->p.inode);
                if (ret)
                        break;
@@ -717,7 +783,7 @@ retry:
                                k.k->p.inode)) {
                        ret = bch2_btree_delete_at(&trans, iter, 0);
                        if (ret)
-                               goto err;
+                               break;
                        continue;
                }
 
@@ -727,12 +793,16 @@ retry:
                ret = hash_check_key(&trans, bch2_xattr_hash_desc,
                                     &h, iter, k);
                if (ret)
-                       goto fsck_err;
+                       break;
+
+               bch2_btree_iter_advance(iter);
        }
-err:
 fsck_err:
        if (ret == -EINTR)
                goto retry;
+
+       bch2_trans_iter_put(&trans, h.chain);
+       bch2_trans_iter_put(&trans, iter);
        return bch2_trans_exit(&trans) ?: ret;
 }
 
@@ -744,7 +814,9 @@ static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
 
        bch_verbose(c, "checking root directory");
 
-       ret = bch2_inode_find_by_inum(c, BCACHEFS_ROOT_INO, root_inode);
+       ret = bch2_trans_do(c, NULL, NULL, 0,
+               __bch2_inode_find_by_inum_trans(&trans, BCACHEFS_ROOT_INO,
+                                               root_inode, 0));
        if (ret && ret != -ENOENT)
                return ret;
 
@@ -759,13 +831,13 @@ 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);
+       bch2_inode_pack(c, &packed, root_inode);
 
-       return bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
+       return bch2_btree_insert(c, BTREE_ID_inodes, &packed.inode.k_i,
                                 NULL, NULL,
                                 BTREE_INSERT_NOFAIL|
                                 BTREE_INSERT_LAZY_RW);
@@ -779,7 +851,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;
 
@@ -792,7 +863,8 @@ static int check_lostfound(struct bch_fs *c,
                goto create_lostfound;
        }
 
-       ret = bch2_inode_find_by_inum(c, inum, lostfound_inode);
+       ret = bch2_trans_do(c, NULL, NULL, 0,
+               __bch2_inode_find_by_inum_trans(&trans, inum, lostfound_inode, 0));
        if (ret && ret != -ENOENT)
                return ret;
 
@@ -807,65 +879,37 @@ 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|
-                               BTREE_INSERT_LAZY_RW);
-       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|
-                                BTREE_INSERT_LAZY_RW);
+       bch2_inode_init_early(c, lostfound_inode);
+
+       ret = bch2_trans_do(c, NULL, NULL,
+                           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 {
-       unsigned long   *bits;
-       size_t          size;
-};
+typedef GENRADIX(unsigned long) inode_bitmap;
 
-static inline bool inode_bitmap_test(struct inode_bitmap *b, size_t nr)
+static inline bool inode_bitmap_test(inode_bitmap *b, size_t nr)
 {
-       return nr < b->size ? test_bit(nr, b->bits) : false;
+       unsigned long *w = genradix_ptr(b, nr / BITS_PER_LONG);
+       return w ? test_bit(nr & (BITS_PER_LONG - 1), w) : false;
 }
 
-static inline int inode_bitmap_set(struct inode_bitmap *b, size_t nr)
+static inline int inode_bitmap_set(inode_bitmap *b, size_t nr)
 {
-       if (nr >= b->size) {
-               size_t new_size = max_t(size_t, max_t(size_t,
-                                       PAGE_SIZE * 8,
-                                       b->size * 2),
-                                       nr + 1);
-               void *n;
-
-               new_size = roundup_pow_of_two(new_size);
-               n = krealloc(b->bits, new_size / 8, GFP_KERNEL|__GFP_ZERO);
-               if (!n) {
-                       return -ENOMEM;
-               }
+       unsigned long *w = genradix_ptr_alloc(b, nr / BITS_PER_LONG, GFP_KERNEL);
 
-               b->bits = n;
-               b->size = new_size;
-       }
+       if (!w)
+               return -ENOMEM;
 
-       __set_bit(nr, b->bits);
+       *w |= 1UL << (nr & (BITS_PER_LONG - 1));
        return 0;
 }
 
@@ -904,7 +948,7 @@ noinline_for_stack
 static int check_directory_structure(struct bch_fs *c,
                                     struct bch_inode_unpacked *lostfound_inode)
 {
-       struct inode_bitmap dirs_done = { NULL, 0 };
+       inode_bitmap dirs_done;
        struct pathbuf path = { 0, 0, NULL };
        struct pathbuf_entry *e;
        struct btree_trans trans;
@@ -921,6 +965,7 @@ static int check_directory_structure(struct bch_fs *c,
 
        /* DFS: */
 restart_dfs:
+       genradix_init(&dirs_done);
        had_unreachable = false;
 
        ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
@@ -940,7 +985,7 @@ next:
                if (e->offset == U64_MAX)
                        goto up;
 
-               for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
+               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;
@@ -993,9 +1038,9 @@ up:
                path.nr--;
        }
 
-       iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS_MIN, 0);
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_inodes, POS_MIN, 0);
 retry:
-       for_each_btree_key_continue(iter, 0, k) {
+       for_each_btree_key_continue(iter, 0, k, ret) {
                if (k.k->type != KEY_TYPE_inode)
                        continue;
 
@@ -1008,12 +1053,12 @@ retry:
                if (!ret)
                        continue;
 
-               if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.inode), c,
+               if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.offset), c,
                                "unreachable directory found (inum %llu)",
-                               k.k->p.inode)) {
+                               k.k->p.offset)) {
                        bch2_trans_unlock(&trans);
 
-                       ret = reattach_inode(c, lostfound_inode, k.k->p.inode);
+                       ret = reattach_inode(c, lostfound_inode, k.k->p.offset);
                        if (ret) {
                                goto err;
                        }
@@ -1021,13 +1066,13 @@ retry:
                        had_unreachable = true;
                }
        }
-       ret = bch2_trans_iter_free(&trans, iter);
+       bch2_trans_iter_free(&trans, iter);
        if (ret)
                goto err;
 
        if (had_unreachable) {
                bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
-               kfree(dirs_done.bits);
+               genradix_free(&dirs_done);
                kfree(path.entries);
                memset(&dirs_done, 0, sizeof(dirs_done));
                memset(&path, 0, sizeof(path));
@@ -1036,7 +1081,7 @@ retry:
 err:
 fsck_err:
        ret = bch2_trans_exit(&trans) ?: ret;
-       kfree(dirs_done.bits);
+       genradix_free(&dirs_done);
        kfree(path.entries);
        return ret;
 }
@@ -1057,6 +1102,11 @@ static void inc_link(struct bch_fs *c, nlink_table *links,
        if (inum < range_start || inum >= *range_end)
                return;
 
+       if (inum - range_start >= SIZE_MAX / sizeof(struct nlink)) {
+               *range_end = inum;
+               return;
+       }
+
        link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
        if (!link) {
                bch_verbose(c, "allocation failed during fsck - will need another pass");
@@ -1085,7 +1135,7 @@ static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
 
        inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
 
-       for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS, POS_MIN, 0, k, ret) {
+       for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN, 0, k, ret) {
                switch (k.k->type) {
                case KEY_TYPE_dirent:
                        d = bkey_s_c_to_dirent(k);
@@ -1103,6 +1153,8 @@ static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
 
                bch2_trans_cond_resched(&trans);
        }
+       bch2_trans_iter_put(&trans, iter);
+
        ret = bch2_trans_exit(&trans) ?: ret;
        if (ret)
                bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
@@ -1116,9 +1168,7 @@ static int check_inode_nlink(struct bch_fs *c,
                             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;
@@ -1141,14 +1191,14 @@ static int check_inode_nlink(struct bch_fs *c,
        }
 
        if (!S_ISDIR(u->bi_mode) && link->dir_count) {
-               need_fsck_err(c, "non directory with subdirectories",
+               need_fsck_err(c, "non directory with subdirectories (inum %llu)",
                              u->bi_inum);
                return 0;
        }
 
        if (!link->count &&
            !(u->bi_flags & BCH_INODE_UNLINKED) &&
-           (c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
+           (c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
                if (fsck_err(c, "unreachable inode %llu not marked as unlinked (type %u)",
                             u->bi_inum, mode_to_type(u->bi_mode)) ==
                    FSCK_ERR_IGNORE)
@@ -1183,7 +1233,7 @@ static int check_inode_nlink(struct bch_fs *c,
        }
 
        if (i_nlink != real_i_nlink &&
-           (c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
+           (c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
                if (fsck_err(c, "inode %llu has wrong i_nlink "
                             "(type %u i_nlink %u, should be %u)",
                             u->bi_inum, mode_to_type(u->bi_mode),
@@ -1197,14 +1247,7 @@ 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:
@@ -1244,7 +1287,9 @@ static int check_inode(struct btree_trans *trans,
                      u.bi_inum))) {
                bch_verbose(c, "deleting inode %llu", u.bi_inum);
 
-               ret = bch2_inode_rm(c, u.bi_inum);
+               bch2_fs_lazy_rw(c);
+
+               ret = bch2_inode_rm(c, u.bi_inum, false);
                if (ret)
                        bch_err(c, "error in fsck: error %i while deleting inode", ret);
                return ret;
@@ -1256,12 +1301,16 @@ static int check_inode(struct btree_trans *trans,
                      u.bi_inum))) {
                bch_verbose(c, "truncating inode %llu", u.bi_inum);
 
+               bch2_fs_lazy_rw(c);
+
                /*
                 * XXX: need to truncate partial blocks too here - or ideally
                 * just switch units to bytes and that issue goes away
                 */
-
-               ret = bch2_inode_truncate(c, u.bi_inum, u.bi_size);
+               ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
+                               POS(u.bi_inum, round_up(u.bi_size, block_bytes(c))),
+                               POS(u.bi_inum, U64_MAX),
+                               NULL);
                if (ret) {
                        bch_err(c, "error in fsck: error %i truncating inode", ret);
                        return ret;
@@ -1298,16 +1347,26 @@ static int check_inode(struct btree_trans *trans,
                do_update = true;
        }
 
+       if (!S_ISDIR(u.bi_mode) &&
+           u.bi_nlink &&
+           !(u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) &&
+           (fsck_err_on(c->sb.version >= bcachefs_metadata_version_inode_backpointers, c,
+                        "inode missing BCH_INODE_BACKPTR_UNTRUSTED flags") ||
+            c->opts.version_upgrade)) {
+               u.bi_flags |= BCH_INODE_BACKPTR_UNTRUSTED;
+               do_update = true;
+       }
+
        if (do_update) {
                struct bkey_inode_buf p;
 
-               bch2_inode_pack(&p, &u);
-               bch2_trans_update(trans, BTREE_INSERT_ENTRY(iter, &p.inode.k_i));
+               bch2_inode_pack(c, &p, &u);
 
-               ret = bch2_trans_commit(trans, NULL, NULL,
-                                       BTREE_INSERT_NOFAIL|
-                                       BTREE_INSERT_LAZY_RW);
-               if (ret && ret != -EINTR)
+               ret = __bch2_trans_do(trans, NULL, NULL,
+                                     BTREE_INSERT_NOFAIL|
+                                     BTREE_INSERT_LAZY_RW,
+                       (bch2_trans_update(trans, iter, &p.inode.k_i, 0), 0));
+               if (ret)
                        bch_err(c, "error in fsck: error %i "
                                "updating inode", ret);
        }
@@ -1331,28 +1390,30 @@ static int bch2_gc_walk_inodes(struct bch_fs *c,
 
        bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
 
-       iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES,
-                                  POS(range_start, 0), 0);
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_inodes,
+                                  POS(0, range_start), 0);
        nlinks_iter = genradix_iter_init(links, 0);
 
        while ((k = bch2_btree_iter_peek(iter)).k &&
-              !(ret2 = bkey_err(k))) {
+              !(ret2 = bkey_err(k)) &&
+              iter->pos.offset < range_end) {
 peek_nlinks:   link = genradix_iter_peek(&nlinks_iter, links);
 
-               if (!link && (!k.k || iter->pos.inode >= range_end))
+               if (!link && (!k.k || iter->pos.offset >= range_end))
                        break;
 
                nlinks_pos = range_start + nlinks_iter.pos;
-               if (iter->pos.inode > nlinks_pos) {
+
+               if (link && nlinks_pos < iter->pos.offset) {
                        /* Should have been caught by dirents pass: */
-                       need_fsck_err_on(link && link->count, c,
+                       need_fsck_err_on(link->count, c,
                                "missing inode %llu (nlink %u)",
                                nlinks_pos, link->count);
                        genradix_iter_advance(&nlinks_iter, links);
                        goto peek_nlinks;
                }
 
-               if (iter->pos.inode < nlinks_pos || !link)
+               if (!link || nlinks_pos > iter->pos.offset)
                        link = &zero_links;
 
                if (k.k && k.k->type == KEY_TYPE_inode) {
@@ -1368,13 +1429,14 @@ peek_nlinks:    link = genradix_iter_peek(&nlinks_iter, links);
                                nlinks_pos, link->count);
                }
 
-               if (nlinks_pos == iter->pos.inode)
+               if (nlinks_pos == iter->pos.offset)
                        genradix_iter_advance(&nlinks_iter, links);
 
-               bch2_btree_iter_next(iter);
+               bch2_btree_iter_advance(iter);
                bch2_trans_cond_resched(&trans);
        }
 fsck_err:
+       bch2_trans_iter_put(&trans, iter);
        bch2_trans_exit(&trans);
 
        if (ret2)
@@ -1455,7 +1517,7 @@ int bch2_fsck_walk_inodes_only(struct bch_fs *c)
 
        bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
 
-       for_each_btree_key(&trans, iter, BTREE_ID_INODES, POS_MIN, 0, k, ret) {
+       for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN, 0, k, ret) {
                if (k.k->type != KEY_TYPE_inode)
                        continue;
 
@@ -1466,11 +1528,12 @@ int bch2_fsck_walk_inodes_only(struct bch_fs *c)
                     BCH_INODE_I_SECTORS_DIRTY|
                     BCH_INODE_UNLINKED)) {
                        ret = check_inode(&trans, NULL, iter, inode, NULL);
-                       BUG_ON(ret == -EINTR);
                        if (ret)
                                break;
                }
        }
+       bch2_trans_iter_put(&trans, iter);
+
        BUG_ON(ret == -EINTR);
 
        return bch2_trans_exit(&trans) ?: ret;