Commit 163e885a authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: Kill TRANS_RESET_MEM|TRANS_RESET_ITERS

All iterators should be released now with bch2_trans_iter_put(), so
TRANS_RESET_ITERS shouldn't be needed anymore, and TRANS_RESET_MEM is
always used.

Also convert more code to __bch2_trans_do().
Signed-off-by: default avatarKent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent ac7c51b2
......@@ -1756,6 +1756,8 @@ int bch2_trans_iter_put(struct btree_trans *trans,
if (IS_ERR_OR_NULL(iter))
return 0;
BUG_ON(trans->iters + iter->idx != iter);
ret = btree_iter_err(iter);
if (!(trans->iters_touched & (1ULL << iter->idx)) &&
......@@ -2080,15 +2082,10 @@ void bch2_trans_reset(struct btree_trans *trans, unsigned flags)
bch2_trans_unlink_iters(trans);
if (flags & TRANS_RESET_ITERS)
trans->iters_live = 0;
trans->iters_touched &= trans->iters_live;
trans->need_reset = 0;
trans->nr_updates = 0;
if (flags & TRANS_RESET_MEM)
trans->mem_top = 0;
if (trans->fs_usage_deltas) {
......@@ -2109,6 +2106,12 @@ void bch2_trans_init(struct btree_trans *trans, struct bch_fs *c,
{
memset(trans, 0, offsetof(struct btree_trans, iters_onstack));
/*
* reallocating iterators currently completely breaks
* bch2_trans_iter_put():
*/
expected_nr_iters = BTREE_ITER_MAX;
trans->c = c;
trans->ip = _RET_IP_;
trans->size = ARRAY_SIZE(trans->iters_onstack);
......
......@@ -290,15 +290,13 @@ struct btree_iter *bch2_trans_get_node_iter(struct btree_trans *,
enum btree_id, struct bpos,
unsigned, unsigned, unsigned);
#define TRANS_RESET_ITERS (1 << 0)
#define TRANS_RESET_MEM (1 << 1)
#define TRANS_RESET_NOTRAVERSE (1 << 2)
#define TRANS_RESET_NOTRAVERSE (1 << 0)
void bch2_trans_reset(struct btree_trans *, unsigned);
static inline void bch2_trans_begin(struct btree_trans *trans)
{
return bch2_trans_reset(trans, TRANS_RESET_ITERS|TRANS_RESET_MEM);
return bch2_trans_reset(trans, 0);
}
void *bch2_trans_kmalloc(struct btree_trans *, size_t);
......
......@@ -59,6 +59,7 @@ enum btree_insert_flags {
int bch2_btree_delete_at(struct btree_trans *, struct btree_iter *, unsigned);
int __bch2_btree_insert(struct btree_trans *, enum btree_id, struct bkey_i *);
int bch2_btree_insert(struct bch_fs *, enum btree_id, struct bkey_i *,
struct disk_reservation *, u64 *, int flags);
......@@ -98,17 +99,17 @@ static inline int bch2_trans_commit(struct btree_trans *trans,
return __bch2_trans_commit(trans);
}
#define __bch2_trans_do(_trans, _disk_res, _journal_seq, \
_flags, _reset_flags, _do) \
#define __bch2_trans_do(_trans, _disk_res, _journal_seq, _flags, _do) \
({ \
int _ret; \
\
do { \
bch2_trans_reset(_trans, _reset_flags); \
\
while (1) { \
_ret = (_do) ?: bch2_trans_commit(_trans, (_disk_res), \
(_journal_seq), (_flags)); \
} while (_ret == -EINTR); \
if (_ret != -EINTR) \
break; \
bch2_trans_reset(_trans, 0); \
} \
\
_ret; \
})
......@@ -120,7 +121,7 @@ static inline int bch2_trans_commit(struct btree_trans *trans,
\
bch2_trans_init(&trans, (_c), 0, 0); \
_ret = __bch2_trans_do(&trans, _disk_res, _journal_seq, _flags, \
TRANS_RESET_MEM|TRANS_RESET_ITERS, _do); \
_do); \
_ret2 = bch2_trans_exit(&trans); \
\
_ret ?: _ret2; \
......
......@@ -758,7 +758,7 @@ int __bch2_trans_commit(struct btree_trans *trans)
if (likely(!(trans->flags & BTREE_INSERT_NOCHECK_RW)))
percpu_ref_put(&trans->c->writes);
out_noupdates:
bch2_trans_reset(trans, TRANS_RESET_MEM|TRANS_RESET_NOTRAVERSE);
bch2_trans_reset(trans, !ret ? TRANS_RESET_NOTRAVERSE : 0);
return ret;
err:
......@@ -839,18 +839,21 @@ int bch2_trans_update(struct btree_trans *trans, struct btree_iter *iter,
return 0;
}
static int __bch2_btree_insert(struct btree_trans *trans,
int __bch2_btree_insert(struct btree_trans *trans,
enum btree_id id, struct bkey_i *k)
{
struct btree_iter *iter;
int ret;
iter = bch2_trans_get_iter(trans, id, bkey_start_pos(&k->k),
BTREE_ITER_INTENT);
if (IS_ERR(iter))
return PTR_ERR(iter);
ret = bch2_btree_iter_traverse(iter) ?:
bch2_trans_update(trans, iter, k, 0);
return 0;
bch2_trans_iter_put(trans, iter);
return ret;
}
/**
......@@ -882,7 +885,7 @@ int bch2_btree_delete_at_range(struct btree_trans *trans,
bkey_cmp(iter->pos, end) < 0) {
struct bkey_i delete;
bch2_trans_reset(trans, TRANS_RESET_MEM);
bch2_trans_begin(trans);
bkey_init(&delete.k);
......
......@@ -169,12 +169,12 @@ int bch2_dirent_rename(struct btree_trans *trans,
const struct qstr *dst_name, u64 *dst_inum,
enum bch_rename_mode mode)
{
struct btree_iter *src_iter, *dst_iter;
struct btree_iter *src_iter = NULL, *dst_iter = NULL;
struct bkey_s_c old_src, old_dst;
struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
struct bpos dst_pos =
POS(dst_dir, bch2_dirent_hash(dst_hash, dst_name));
int ret;
int ret = 0;
*src_inum = *dst_inum = 0;
......@@ -191,8 +191,10 @@ int bch2_dirent_rename(struct btree_trans *trans,
: bch2_hash_lookup(trans, bch2_dirent_hash_desc,
dst_hash, dst_dir, dst_name,
BTREE_ITER_INTENT);
if (IS_ERR(dst_iter))
return PTR_ERR(dst_iter);
ret = PTR_ERR_OR_ZERO(dst_iter);
if (ret)
goto out;
old_dst = bch2_btree_iter_peek_slot(dst_iter);
if (mode != BCH_RENAME)
......@@ -202,15 +204,18 @@ int bch2_dirent_rename(struct btree_trans *trans,
src_iter = bch2_hash_lookup(trans, bch2_dirent_hash_desc,
src_hash, src_dir, src_name,
BTREE_ITER_INTENT);
if (IS_ERR(src_iter))
return PTR_ERR(src_iter);
ret = PTR_ERR_OR_ZERO(src_iter);
if (ret)
goto out;
old_src = bch2_btree_iter_peek_slot(src_iter);
*src_inum = le64_to_cpu(bkey_s_c_to_dirent(old_src).v->d_inum);
/* Create new dst key: */
new_dst = dirent_create_key(trans, 0, dst_name, 0);
if (IS_ERR(new_dst))
return PTR_ERR(new_dst);
ret = PTR_ERR_OR_ZERO(new_dst);
if (ret)
goto out;
dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
new_dst->k.p = dst_iter->pos;
......@@ -218,15 +223,18 @@ int bch2_dirent_rename(struct btree_trans *trans,
/* Create new src key: */
if (mode == BCH_RENAME_EXCHANGE) {
new_src = dirent_create_key(trans, 0, src_name, 0);
if (IS_ERR(new_src))
return PTR_ERR(new_src);
ret = PTR_ERR_OR_ZERO(new_src);
if (ret)
goto out;
dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
new_src->k.p = src_iter->pos;
} else {
new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
if (IS_ERR(new_src))
return PTR_ERR(new_src);
ret = PTR_ERR_OR_ZERO(new_src);
if (ret)
goto out;
bkey_init(&new_src->k);
new_src->k.p = src_iter->pos;
......@@ -247,7 +255,7 @@ int bch2_dirent_rename(struct btree_trans *trans,
new_dst->k.p = src_iter->pos;
bch2_trans_update(trans, src_iter,
&new_dst->k_i, 0);
return 0;
goto out;
} else {
/* If we're overwriting, we can't insert new_dst
* at a different slot because it has to
......@@ -261,7 +269,7 @@ int bch2_dirent_rename(struct btree_trans *trans,
ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
src_hash, src_iter);
if (ret < 0)
return ret;
goto out;
if (ret)
new_src->k.type = KEY_TYPE_whiteout;
......@@ -270,7 +278,10 @@ int bch2_dirent_rename(struct btree_trans *trans,
bch2_trans_update(trans, src_iter, &new_src->k_i, 0);
bch2_trans_update(trans, dst_iter, &new_dst->k_i, 0);
return 0;
out:
bch2_trans_iter_put(trans, src_iter);
bch2_trans_iter_put(trans, dst_iter);
return ret;
}
int bch2_dirent_delete_at(struct btree_trans *trans,
......@@ -331,8 +342,6 @@ int bch2_empty_dir_trans(struct btree_trans *trans, u64 dir_inum)
break;
}
}
if (!IS_ERR(iter))
bch2_trans_iter_put(trans, iter);
return ret;
......
......@@ -741,6 +741,8 @@ static int ec_stripe_bkey_insert(struct bch_fs *c,
ret = bch2_trans_commit(&trans, NULL, NULL,
BTREE_INSERT_NOFAIL);
err:
bch2_trans_iter_put(&trans, iter);
if (ret == -EINTR)
goto retry;
......@@ -1201,8 +1203,7 @@ static int __bch2_stripe_write_key(struct btree_trans *trans,
struct btree_iter *iter,
struct stripe *m,
size_t idx,
struct bkey_i_stripe *new_key,
unsigned flags)
struct bkey_i_stripe *new_key)
{
struct bch_fs *c = trans->c;
struct bkey_s_c k;
......@@ -1231,9 +1232,7 @@ static int __bch2_stripe_write_key(struct btree_trans *trans,
spin_unlock(&c->ec_stripes_heap_lock);
bch2_trans_update(trans, iter, &new_key->k_i, 0);
return bch2_trans_commit(trans, NULL, NULL,
BTREE_INSERT_NOFAIL|flags);
return 0;
}
int bch2_stripes_write(struct bch_fs *c, unsigned flags, bool *wrote)
......@@ -1257,12 +1256,10 @@ int bch2_stripes_write(struct bch_fs *c, unsigned flags, bool *wrote)
if (!m->dirty)
continue;
do {
bch2_trans_reset(&trans, TRANS_RESET_MEM);
ret = __bch2_stripe_write_key(&trans, iter, m,
giter.pos, new_key, flags);
} while (ret == -EINTR);
ret = __bch2_trans_do(&trans, NULL, NULL,
BTREE_INSERT_NOFAIL|flags,
__bch2_stripe_write_key(&trans, iter, m,
giter.pos, new_key));
if (ret)
break;
......
......@@ -2620,7 +2620,7 @@ static long bchfs_fallocate(struct bch_inode_info *inode, int mode,
struct bkey_i_reservation reservation;
struct bkey_s_c k;
bch2_trans_reset(&trans, TRANS_RESET_MEM);
bch2_trans_begin(&trans);
k = bch2_btree_iter_peek_slot(iter);
if ((ret = bkey_err(k)))
......
......@@ -81,7 +81,6 @@ static int remove_dirent(struct btree_trans *trans,
return __bch2_trans_do(trans, NULL, NULL,
BTREE_INSERT_NOFAIL|
BTREE_INSERT_LAZY_RW,
TRANS_RESET_MEM,
__remove_dirent(trans, dirent));
}
......@@ -182,8 +181,6 @@ static int hash_redo_key(const struct bch_hash_desc desc,
struct bkey_i delete;
struct bkey_i *tmp;
bch2_trans_reset(trans, TRANS_RESET_MEM);
tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
if (IS_ERR(tmp))
return PTR_ERR(tmp);
......@@ -195,10 +192,7 @@ static int hash_redo_key(const struct bch_hash_desc desc,
bch2_trans_update(trans, k_iter, &delete, 0);
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_NOFAIL|
BTREE_INSERT_LAZY_RW);
tmp, BCH_HASH_SET_MUST_CREATE);
}
static int fsck_hash_delete_at(struct btree_trans *trans,
......@@ -320,10 +314,9 @@ static int hash_check_key(struct btree_trans *trans,
desc.btree_id, k.k->p.offset,
hashed, h->chain->pos.offset,
(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);
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;
......@@ -387,7 +380,6 @@ static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
ret = __bch2_trans_do(trans, NULL, NULL,
BTREE_INSERT_NOFAIL|
BTREE_INSERT_LAZY_RW,
TRANS_RESET_MEM,
(bch2_trans_update(trans, iter, &d->k_i, 0), 0));
if (ret)
goto err;
......@@ -410,11 +402,10 @@ static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
k->k->p.offset, hash, h->chain->pos.offset,
(bch2_bkey_val_to_text(&PBUF(buf), c,
*k), buf))) {
do {
ret = hash_redo_key(bch2_dirent_hash_desc, trans,
h, iter, *k, hash);
} while (ret == -EINTR);
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
......@@ -660,7 +651,6 @@ static int check_dirents(struct bch_fs *c)
ret = __bch2_trans_do(&trans, NULL, NULL,
BTREE_INSERT_NOFAIL|
BTREE_INSERT_LAZY_RW,
TRANS_RESET_MEM,
(bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
kfree(n);
if (ret)
......@@ -1275,7 +1265,6 @@ static int check_inode(struct btree_trans *trans,
ret = __bch2_trans_do(trans, NULL, NULL,
BTREE_INSERT_NOFAIL|
BTREE_INSERT_LAZY_RW,
TRANS_RESET_MEM,
(bch2_trans_update(trans, iter, &p.inode.k_i, 0), 0));
if (ret)
bch_err(c, "error in fsck: error %i "
......
......@@ -335,7 +335,7 @@ int bch2_fpunch_at(struct btree_trans *trans, struct btree_iter *iter,
bch2_disk_reservation_init(c, 0);
struct bkey_i delete;
bch2_trans_reset(trans, TRANS_RESET_MEM);
bch2_trans_begin(trans);
ret = bkey_err(k);
if (ret)
......@@ -409,7 +409,7 @@ int bch2_write_index_default(struct bch_write_op *op)
BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
do {
bch2_trans_reset(&trans, TRANS_RESET_MEM);
bch2_trans_begin(&trans);
k = bch2_keylist_front(keys);
......
......@@ -184,7 +184,7 @@ s64 bch2_remap_range(struct bch_fs *c,
BTREE_ITER_INTENT);
while (1) {
bch2_trans_reset(&trans, TRANS_RESET_MEM);
bch2_trans_begin(&trans);
trans.mem_top = 0;
......
......@@ -163,6 +163,7 @@ bch2_hash_lookup(struct btree_trans *trans,
break;
}
}
bch2_trans_iter_put(trans, iter);
return ERR_PTR(ret ?: -ENOENT);
}
......@@ -187,6 +188,9 @@ bch2_hash_hole(struct btree_trans *trans,
return iter;
}
iter->flags |= BTREE_ITER_KEEP_UNTIL_COMMIT;
bch2_trans_iter_put(trans, iter);
return ERR_PTR(ret ?: -ENOSPC);
}
......
......@@ -43,8 +43,8 @@ static void test_delete(struct bch_fs *c, u64 nr)
ret = bch2_btree_iter_traverse(iter);
BUG_ON(ret);
bch2_trans_update(&trans, iter, &k.k_i, 0);
ret = bch2_trans_commit(&trans, NULL, NULL, 0);
ret = __bch2_trans_do(&trans, NULL, NULL, 0,
bch2_trans_update(&trans, iter, &k.k_i, 0));
BUG_ON(ret);
pr_info("deleting once");
......@@ -75,8 +75,8 @@ static void test_delete_written(struct bch_fs *c, u64 nr)
ret = bch2_btree_iter_traverse(iter);
BUG_ON(ret);
bch2_trans_update(&trans, iter, &k.k_i, 0);
ret = bch2_trans_commit(&trans, NULL, NULL, 0);
ret = __bch2_trans_do(&trans, NULL, NULL, 0,
bch2_trans_update(&trans, iter, &k.k_i, 0));
BUG_ON(ret);
bch2_journal_flush_all_pins(&c->journal);
......@@ -409,18 +409,24 @@ static u64 test_rand(void)
static void rand_insert(struct bch_fs *c, u64 nr)
{
struct btree_trans trans;
struct bkey_i_cookie k;
int ret;
u64 i;
bch2_trans_init(&trans, c, 0, 0);
for (i = 0; i < nr; i++) {
bkey_cookie_init(&k.k_i);
k.k.p.offset = test_rand();
ret = bch2_btree_insert(c, BTREE_ID_DIRENTS, &k.k_i,
NULL, NULL, 0);
ret = __bch2_trans_do(&trans, NULL, NULL, 0,
__bch2_btree_insert(&trans, BTREE_ID_DIRENTS, &k.k_i));
BUG_ON(ret);
}
bch2_trans_exit(&trans);
}
static void rand_lookup(struct bch_fs *c, u64 nr)
......@@ -465,8 +471,9 @@ static void rand_mixed(struct bch_fs *c, u64 nr)
bkey_cookie_init(&k.k_i);
k.k.p = iter->pos;
bch2_trans_update(&trans, iter, &k.k_i, 0);
ret = bch2_trans_commit(&trans, NULL, NULL, 0);
ret = __bch2_trans_do(&trans, NULL, NULL, 0,
bch2_trans_update(&trans, iter, &k.k_i, 0));
BUG_ON(ret);
}
......@@ -476,20 +483,50 @@ static void rand_mixed(struct bch_fs *c, u64 nr)
bch2_trans_exit(&trans);
}
static int __do_delete(struct btree_trans *trans, struct bpos pos)
{
struct btree_iter *iter;
struct bkey_i delete;
struct bkey_s_c k;
int ret = 0;
iter = bch2_trans_get_iter(trans, BTREE_ID_DIRENTS, pos,
BTREE_ITER_INTENT);
ret = PTR_ERR_OR_ZERO(iter);
if (ret)
goto err;
k = bch2_btree_iter_peek(iter);
ret = bkey_err(k);
if (ret)
goto err;
bkey_init(&delete.k);
delete.k.p = k.k->p;
bch2_trans_update(trans, iter, &delete, 0);
err:
bch2_trans_iter_put(trans, iter);
return ret;
}
static void rand_delete(struct bch_fs *c, u64 nr)
{
struct bkey_i k;
struct btree_trans trans;
int ret;
u64 i;
bch2_trans_init(&trans, c, 0, 0);
for (i = 0; i < nr; i++) {
bkey_init(&k.k);
k.k.p.offset = test_rand();
struct bpos pos = POS(0, test_rand());
ret = bch2_btree_insert(c, BTREE_ID_DIRENTS, &k,
NULL, NULL, 0);
ret = __bch2_trans_do(&trans, NULL, NULL, 0,
__do_delete(&trans, pos));
BUG_ON(ret);
}
bch2_trans_exit(&trans);
}
static void seq_insert(struct bch_fs *c, u64 nr)
......@@ -509,8 +546,9 @@ static void seq_insert(struct bch_fs *c, u64 nr)
BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
insert.k.p = iter->pos;
bch2_trans_update(&trans, iter, &insert.k_i, 0);
ret = bch2_trans_commit(&trans, NULL, NULL, 0);
ret = __bch2_trans_do(&trans, NULL, NULL, 0,
bch2_trans_update(&trans, iter, &insert.k_i, 0));
BUG_ON(ret);
if (++i == nr)
......@@ -548,8 +586,9 @@ static void seq_overwrite(struct bch_fs *c, u64 nr)
bkey_reassemble(&u.k_i, k);
bch2_trans_update(&trans, iter, &u.k_i, 0);
ret = bch2_trans_commit(&trans, NULL, NULL, 0);
ret = __bch2_trans_do(&trans, NULL, NULL, 0,
bch2_trans_update(&trans, iter, &u.k_i, 0));
BUG_ON(ret);
}
bch2_trans_exit(&trans);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment