};
 }
 
-#define bch2_trans_do(_c, _journal_seq, _flags, _do)                   \
+#define __bch2_trans_do(_trans, _disk_res, _journal_seq,               \
+                       _flags, _reset_flags, _do)                      \
 ({                                                                     \
-       struct btree_trans trans;                                       \
        int _ret;                                                       \
                                                                        \
-       bch2_trans_init(&trans, (_c), 0, 0);                            \
-                                                                       \
        do {                                                            \
-               bch2_trans_begin(&trans);                               \
+               bch2_trans_reset(_trans, _reset_flags);                 \
                                                                        \
-               _ret = (_do) ?: bch2_trans_commit(&trans, NULL,         \
+               _ret = (_do) ?: bch2_trans_commit(_trans, (_disk_res),  \
                                        (_journal_seq), (_flags));      \
        } while (_ret == -EINTR);                                       \
                                                                        \
-       bch2_trans_exit(&trans);                                        \
        _ret;                                                           \
 })
 
+#define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do)                \
+({                                                                     \
+       struct btree_trans trans;                                       \
+       int _ret, _ret2;                                                \
+                                                                       \
+       bch2_trans_init(&trans, (_c), 0, 0);                            \
+       _ret = __bch2_trans_do(&trans, _disk_res, _journal_seq, _flags, \
+                              TRANS_RESET_MEM|TRANS_RESET_ITERS, _do); \
+       _ret2 = bch2_trans_exit(&trans);                                \
+                                                                       \
+       _ret ?: _ret2;                                                  \
+})
+
 #define trans_for_each_update(_trans, _i)                              \
        for ((_i) = (_trans)->updates;                                  \
             (_i) < (_trans)->updates + (_trans)->nr_updates;           \
 
        goto retry;
 }
 
+static int __bch2_btree_insert(struct btree_trans *trans,
+                              enum btree_id id, struct bkey_i *k)
+{
+       struct btree_iter *iter;
+
+       iter = bch2_trans_get_iter(trans, id, bkey_start_pos(&k->k),
+                                  BTREE_ITER_INTENT);
+       if (IS_ERR(iter))
+               return PTR_ERR(iter);
+
+       bch2_trans_update(trans, iter, k);
+       return 0;
+}
+
 /**
  * bch2_btree_insert - insert keys into the extent btree
  * @c:                 pointer to struct bch_fs
  * @hook:              insert callback
  */
 int bch2_btree_insert(struct bch_fs *c, enum btree_id id,
-                    struct bkey_i *k,
-                    struct disk_reservation *disk_res,
-                    u64 *journal_seq, int flags)
+                     struct bkey_i *k,
+                     struct disk_reservation *disk_res,
+                     u64 *journal_seq, int flags)
 {
-       struct btree_trans trans;
-       struct btree_iter *iter;
-       int ret;
-
-       bch2_trans_init(&trans, c, 0, 0);
-retry:
-       bch2_trans_begin(&trans);
-
-       iter = bch2_trans_get_iter(&trans, id, bkey_start_pos(&k->k),
-                                  BTREE_ITER_INTENT);
-
-       bch2_trans_update(&trans, iter, k);
-
-       ret = bch2_trans_commit(&trans, disk_res, journal_seq, flags);
-       if (ret == -EINTR)
-               goto retry;
-       bch2_trans_exit(&trans);
-
-       return ret;
+       return bch2_trans_do(c, disk_res, journal_seq, flags,
+                            __bch2_btree_insert(&trans, id, k));
 }
 
 int bch2_btree_delete_at_range(struct btree_trans *trans,
 
        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;
        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);
+       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_ATOMIC|
+                              BTREE_INSERT_NOFAIL|
+                              BTREE_INSERT_LAZY_RW,
+                              TRANS_RESET_MEM,
+                              __remove_dirent(trans, dirent));
 }
 
 static int reattach_inode(struct bch_fs *c,
        snprintf(name_buf, sizeof(name_buf), "%llu", inum);
        name = (struct qstr) QSTR(name_buf);
 
-       ret = bch2_trans_do(c, NULL,
+       ret = bch2_trans_do(c, NULL, NULL,
                            BTREE_INSERT_ATOMIC|
                            BTREE_INSERT_LAZY_RW,
                bch2_link_trans(&trans, lostfound_inode->bi_inum,
                         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;
+       bch2_trans_reset(trans, TRANS_RESET_MEM);
+
+       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);
 
-       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, BCH_HASH_SET_MUST_CREATE) ?:
+               bch2_trans_commit(trans, NULL, NULL,
+                                 BTREE_INSERT_ATOMIC|
+                                 BTREE_INSERT_NOFAIL|
+                                 BTREE_INSERT_LAZY_RW);
 }
 
 static int fsck_hash_delete_at(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))) {
+               do {
+                       ret = hash_redo_key(desc, trans, h, k_iter, k, hashed);
+               } while (ret == -EINTR);
+
                if (ret) {
                        bch_err(c, "hash_redo_key err %i", ret);
                        return ret;
 
        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, 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_ATOMIC|
+                                     BTREE_INSERT_NOFAIL|
+                                     BTREE_INSERT_LAZY_RW,
+                                     TRANS_RESET_MEM,
+                       (bch2_trans_update(trans, iter, &d->k_i), 0));
                if (ret)
                        goto err;
 
                     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);
+               do {
+                       ret = hash_redo_key(bch2_dirent_hash_desc, trans,
+                                           h, iter, *k, hash);
+               } while (ret == -EINTR);
+
                if (ret)
                        bch_err(c, "hash_redo_key err %i", ret);
                else
                        bkey_reassemble(&n->k_i, d.s_c);
                        n->v.d_type = mode_to_type(target.bi_mode);
 
-                       bch2_trans_update(&trans, 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_ATOMIC|
+                                             BTREE_INSERT_NOFAIL|
+                                             BTREE_INSERT_LAZY_RW,
+                                             TRANS_RESET_MEM,
+                               (bch2_trans_update(&trans, iter, &n->k_i), 0));
                        kfree(n);
                        if (ret)
                                goto err;
 create_lostfound:
        bch2_inode_init_early(c, lostfound_inode);
 
-       ret = bch2_trans_do(c, NULL,
+       ret = bch2_trans_do(c, NULL, NULL,
                            BTREE_INSERT_ATOMIC|
                            BTREE_INSERT_NOFAIL|
                            BTREE_INSERT_LAZY_RW,
                struct bkey_inode_buf p;
 
                bch2_inode_pack(&p, &u);
-               bch2_trans_update(trans, iter, &p.inode.k_i);
 
-               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_ATOMIC|
+                                     BTREE_INSERT_NOFAIL|
+                                     BTREE_INSERT_LAZY_RW,
+                                     TRANS_RESET_MEM,
+                       (bch2_trans_update(trans, iter, &p.inode.k_i), 0));
+               if (ret)
                        bch_err(c, "error in fsck: error %i "
                                "updating inode", ret);
        }