Commit 3eb26d01 authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: bch2_trans_get_iter() no longer returns errors

Since we now always preallocate the maximum number of iterators when we
initialize a btree transaction, getting an iterator never fails - we can
delete a fair amount of error path code.

This patch also simplifies the iterator allocation code a bit.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent ec3d21a9
......@@ -346,7 +346,7 @@ void bch2_btree_trans_verify_locks(struct btree_trans *trans)
{
struct btree_iter *iter;
trans_for_each_iter_all(trans, iter)
trans_for_each_iter(trans, iter)
bch2_btree_iter_verify_locks(iter);
}
#else
......@@ -2002,110 +2002,37 @@ int bch2_trans_iter_free(struct btree_trans *trans,
return bch2_trans_iter_put(trans, iter);
}
#if 0
static int bch2_trans_realloc_iters(struct btree_trans *trans,
unsigned new_size)
noinline __cold
static void btree_trans_iter_alloc_fail(struct btree_trans *trans)
{
void *p, *new_iters, *new_updates, *new_updates2;
size_t iters_bytes;
size_t updates_bytes;
new_size = roundup_pow_of_two(new_size);
BUG_ON(new_size > BTREE_ITER_MAX);
if (new_size <= trans->size)
return 0;
BUG_ON(trans->used_mempool);
bch2_trans_unlock(trans);
iters_bytes = sizeof(struct btree_iter) * new_size;
updates_bytes = sizeof(struct btree_insert_entry) * new_size;
p = kmalloc(iters_bytes +
updates_bytes +
updates_bytes, GFP_NOFS);
if (p)
goto success;
p = mempool_alloc(&trans->c->btree_iters_pool, GFP_NOFS);
new_size = BTREE_ITER_MAX;
trans->used_mempool = true;
success:
new_iters = p; p += iters_bytes;
new_updates = p; p += updates_bytes;
new_updates2 = p; p += updates_bytes;
memcpy(new_iters, trans->iters,
sizeof(struct btree_iter) * trans->nr_iters);
memcpy(new_updates, trans->updates,
sizeof(struct btree_insert_entry) * trans->nr_updates);
memcpy(new_updates2, trans->updates2,
sizeof(struct btree_insert_entry) * trans->nr_updates2);
if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG))
memset(trans->iters, POISON_FREE,
sizeof(struct btree_iter) * trans->nr_iters +
sizeof(struct btree_insert_entry) * trans->nr_iters);
kfree(trans->iters);
trans->iters = new_iters;
trans->updates = new_updates;
trans->updates2 = new_updates2;
trans->size = new_size;
if (trans->iters_live) {
trace_trans_restart_iters_realloced(trans->ip, trans->size);
return -EINTR;
}
struct btree_iter *iter;
return 0;
trans_for_each_iter(trans, iter)
pr_err("iter: btree %s pos %llu:%llu%s%s%s %ps",
bch2_btree_ids[iter->btree_id],
iter->pos.inode,
iter->pos.offset,
(trans->iters_live & (1ULL << iter->idx)) ? " live" : "",
(trans->iters_touched & (1ULL << iter->idx)) ? " touched" : "",
iter->flags & BTREE_ITER_KEEP_UNTIL_COMMIT ? " keep" : "",
(void *) iter->ip_allocated);
panic("trans iter oveflow\n");
}
#endif
static struct btree_iter *btree_trans_iter_alloc(struct btree_trans *trans)
{
unsigned idx = __ffs64(~trans->iters_linked);
if (idx < trans->nr_iters)
goto got_slot;
if (trans->nr_iters == trans->size) {
struct btree_iter *iter;
BUG_ON(trans->size < BTREE_ITER_MAX);
trans_for_each_iter(trans, iter) {
pr_err("iter: btree %s pos %llu:%llu%s%s%s %ps",
bch2_btree_ids[iter->btree_id],
iter->pos.inode,
iter->pos.offset,
(trans->iters_live & (1ULL << iter->idx)) ? " live" : "",
(trans->iters_touched & (1ULL << iter->idx)) ? " touched" : "",
iter->flags & BTREE_ITER_KEEP_UNTIL_COMMIT ? " keep" : "",
(void *) iter->ip_allocated);
}
unsigned idx;
panic("trans iter oveflow\n");
#if 0
ret = bch2_trans_realloc_iters(trans, trans->size * 2);
if (ret)
return ERR_PTR(ret);
#endif
}
if (unlikely(trans->iters_linked ==
~((~0ULL << 1) << (BTREE_ITER_MAX - 1))))
btree_trans_iter_alloc_fail(trans);
idx = trans->nr_iters++;
BUG_ON(trans->nr_iters > trans->size);
idx = __ffs64(~trans->iters_linked);
trans->iters[idx].idx = idx;
got_slot:
BUG_ON(trans->iters_linked & (1ULL << idx));
trans->iters_linked |= 1ULL << idx;
trans->iters[idx].flags = 0;
trans->iters_linked |= 1ULL << idx;
trans->iters[idx].idx = idx;
trans->iters[idx].flags = 0;
return &trans->iters[idx];
}
......@@ -2141,8 +2068,6 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans,
{
struct btree_iter *iter, *best = NULL;
BUG_ON(trans->nr_iters > BTREE_ITER_MAX);
trans_for_each_iter(trans, iter) {
if (btree_iter_type(iter) != (flags & BTREE_ITER_TYPE))
continue;
......@@ -2160,16 +2085,10 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans,
if (!best) {
iter = btree_trans_iter_alloc(trans);
if (IS_ERR(iter))
return iter;
bch2_btree_iter_init(trans, iter, btree_id, pos, flags);
} else if ((trans->iters_live & (1ULL << best->idx)) ||
(best->flags & BTREE_ITER_KEEP_UNTIL_COMMIT)) {
iter = btree_trans_iter_alloc(trans);
if (IS_ERR(iter))
return iter;
btree_iter_copy(iter, best);
} else {
iter = best;
......@@ -2203,9 +2122,8 @@ struct btree_iter *__bch2_trans_get_iter(struct btree_trans *trans,
struct btree_iter *iter =
__btree_trans_get_iter(trans, btree_id, pos, flags);
if (!IS_ERR(iter))
__bch2_btree_iter_set_pos(iter, pos,
btree_node_type_is_extents(btree_id));
__bch2_btree_iter_set_pos(iter, pos,
btree_node_type_is_extents(btree_id));
return iter;
}
......@@ -2221,7 +2139,6 @@ struct btree_iter *bch2_trans_get_node_iter(struct btree_trans *trans,
flags|BTREE_ITER_NODES);
unsigned i;
BUG_ON(IS_ERR(iter));
BUG_ON(bkey_cmp(iter->pos, pos));
iter->locks_want = locks_want;
......@@ -2241,9 +2158,6 @@ struct btree_iter *__bch2_trans_copy_iter(struct btree_trans *trans,
struct btree_iter *iter;
iter = btree_trans_iter_alloc(trans);
if (IS_ERR(iter))
return iter;
btree_iter_copy(iter, src);
trans->iters_live |= 1ULL << iter->idx;
......@@ -2318,7 +2232,6 @@ void bch2_trans_reset(struct btree_trans *trans, unsigned flags)
trans->iters_touched &= trans->iters_live;
trans->need_reset = 0;
trans->nr_updates = 0;
trans->nr_updates2 = 0;
trans->mem_top = 0;
......@@ -2340,9 +2253,8 @@ void bch2_trans_reset(struct btree_trans *trans, unsigned flags)
static void bch2_trans_alloc_iters(struct btree_trans *trans, struct bch_fs *c)
{
unsigned new_size = BTREE_ITER_MAX;
size_t iters_bytes = sizeof(struct btree_iter) * new_size;
size_t updates_bytes = sizeof(struct btree_insert_entry) * new_size;
size_t iters_bytes = sizeof(struct btree_iter) * BTREE_ITER_MAX;
size_t updates_bytes = sizeof(struct btree_insert_entry) * BTREE_ITER_MAX;
void *p = NULL;
BUG_ON(trans->used_mempool);
......@@ -2356,7 +2268,6 @@ static void bch2_trans_alloc_iters(struct btree_trans *trans, struct bch_fs *c)
trans->iters = p; p += iters_bytes;
trans->updates = p; p += updates_bytes;
trans->updates2 = p; p += updates_bytes;
trans->size = new_size;
}
void bch2_trans_init(struct btree_trans *trans, struct bch_fs *c,
......
......@@ -48,21 +48,16 @@ static inline int btree_iter_err(const struct btree_iter *iter)
/* Iterate over iters within a transaction: */
#define trans_for_each_iter_all(_trans, _iter) \
for (_iter = (_trans)->iters; \
_iter < (_trans)->iters + (_trans)->nr_iters; \
_iter++)
static inline struct btree_iter *
__trans_next_iter(struct btree_trans *trans, unsigned idx)
{
EBUG_ON(idx < trans->nr_iters && trans->iters[idx].idx != idx);
for (; idx < trans->nr_iters; idx++)
if (trans->iters_linked & (1ULL << idx))
return &trans->iters[idx];
u64 l = trans->iters_linked >> idx;
if (!l)
return NULL;
return NULL;
idx += __ffs64(l);
EBUG_ON(trans->iters[idx].idx != idx);
return &trans->iters[idx];
}
#define trans_for_each_iter(_trans, _iter) \
......@@ -240,10 +235,9 @@ static inline int bkey_err(struct bkey_s_c k)
#define for_each_btree_key(_trans, _iter, _btree_id, \
_start, _flags, _k, _ret) \
for ((_ret) = PTR_ERR_OR_ZERO((_iter) = \
bch2_trans_get_iter((_trans), (_btree_id), \
(_start), (_flags))) ?: \
PTR_ERR_OR_ZERO(((_k) = \
for ((_iter) = bch2_trans_get_iter((_trans), (_btree_id), \
(_start), (_flags)), \
(_ret) = PTR_ERR_OR_ZERO(((_k) = \
__bch2_btree_iter_peek(_iter, _flags)).k); \
!_ret && (_k).k; \
(_ret) = PTR_ERR_OR_ZERO(((_k) = \
......@@ -270,9 +264,7 @@ bch2_trans_get_iter(struct btree_trans *trans, enum btree_id btree_id,
{
struct btree_iter *iter =
__bch2_trans_get_iter(trans, btree_id, pos, flags);
if (!IS_ERR(iter))
iter->ip_allocated = _THIS_IP_;
iter->ip_allocated = _THIS_IP_;
return iter;
}
......@@ -284,10 +276,8 @@ bch2_trans_copy_iter(struct btree_trans *trans, struct btree_iter *src)
struct btree_iter *iter =
__bch2_trans_copy_iter(trans, src);
if (!IS_ERR(iter))
iter->ip_allocated = _THIS_IP_;
iter->ip_allocated = _THIS_IP_;
return iter;
}
struct btree_iter *bch2_trans_get_node_iter(struct btree_trans *,
......
......@@ -170,9 +170,6 @@ static int btree_key_cache_fill(struct btree_trans *trans,
iter = bch2_trans_get_iter(trans, ck->key.btree_id,
ck->key.pos, BTREE_ITER_SLOTS);
if (IS_ERR(iter))
return PTR_ERR(iter);
k = bch2_btree_iter_peek_slot(iter);
ret = bkey_err(k);
if (ret) {
......@@ -326,18 +323,11 @@ static int btree_key_cache_flush_pos(struct btree_trans *trans,
b_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
BTREE_ITER_SLOTS|
BTREE_ITER_INTENT);
ret = PTR_ERR_OR_ZERO(b_iter);
if (ret)
goto out;
c_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
BTREE_ITER_CACHED|
BTREE_ITER_CACHED_NOFILL|
BTREE_ITER_CACHED_NOCREATE|
BTREE_ITER_INTENT);
ret = PTR_ERR_OR_ZERO(c_iter);
if (ret)
goto out;
retry:
ret = bch2_btree_iter_traverse(c_iter);
if (ret)
......
......@@ -357,20 +357,17 @@ struct btree_trans {
unsigned long ip;
int srcu_idx;
u64 iters_linked;
u64 iters_live;
u64 iters_touched;
u8 nr_iters;
u8 nr_updates;
u8 nr_updates2;
u8 size;
unsigned used_mempool:1;
unsigned error:1;
unsigned nounlock:1;
unsigned need_reset:1;
unsigned in_traverse_all:1;
u64 iters_linked;
u64 iters_live;
u64 iters_touched;
unsigned mem_top;
unsigned mem_bytes;
void *mem;
......
......@@ -717,7 +717,7 @@ static void bch2_trans_update2(struct btree_trans *trans,
BUG_ON(iter->uptodate > BTREE_ITER_NEED_PEEK);
EBUG_ON(trans->nr_updates2 >= trans->nr_iters);
EBUG_ON(trans->nr_updates2 >= BTREE_ITER_MAX);
iter->flags |= BTREE_ITER_KEEP_UNTIL_COMMIT;
......@@ -750,8 +750,6 @@ static int extent_update_to_keys(struct btree_trans *trans,
return 0;
iter = bch2_trans_copy_iter(trans, orig_iter);
if (IS_ERR(iter))
return PTR_ERR(iter);
iter->flags |= BTREE_ITER_INTENT;
__bch2_btree_iter_set_pos(iter, insert->k.p, false);
......@@ -770,10 +768,6 @@ static int extent_handle_overwrites(struct btree_trans *trans,
int ret = 0;
iter = bch2_trans_get_iter(trans, btree_id, start, BTREE_ITER_INTENT);
ret = PTR_ERR_OR_ZERO(iter);
if (ret)
return ret;
k = bch2_btree_iter_peek_with_updates(iter);
while (k.k && !(ret = bkey_err(k))) {
......@@ -782,8 +776,6 @@ static int extent_handle_overwrites(struct btree_trans *trans,
if (bkey_cmp(bkey_start_pos(k.k), start) < 0) {
update_iter = bch2_trans_copy_iter(trans, iter);
if ((ret = PTR_ERR_OR_ZERO(update_iter)))
goto err;
update = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
if ((ret = PTR_ERR_OR_ZERO(update)))
......@@ -799,8 +791,6 @@ static int extent_handle_overwrites(struct btree_trans *trans,
if (bkey_cmp(k.k->p, end) > 0) {
update_iter = bch2_trans_copy_iter(trans, iter);
if ((ret = PTR_ERR_OR_ZERO(update_iter)))
goto err;
update = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
if ((ret = PTR_ERR_OR_ZERO(update)))
......@@ -814,8 +804,6 @@ static int extent_handle_overwrites(struct btree_trans *trans,
bch2_trans_iter_put(trans, update_iter);
} else {
update_iter = bch2_trans_copy_iter(trans, iter);
if ((ret = PTR_ERR_OR_ZERO(update_iter)))
goto err;
update = bch2_trans_kmalloc(trans, sizeof(struct bkey));
if ((ret = PTR_ERR_OR_ZERO(update)))
......@@ -847,8 +835,6 @@ int __bch2_trans_commit(struct btree_trans *trans)
unsigned u64s;
int ret = 0;
BUG_ON(trans->need_reset);
if (!trans->nr_updates)
goto out_noupdates;
......@@ -1041,10 +1027,6 @@ int bch2_trans_update(struct btree_trans *trans, struct btree_iter *iter,
*/
if (trans->iters_live & (1ULL << i->iter->idx)) {
i->iter = bch2_trans_copy_iter(trans, i->iter);
if (IS_ERR(i->iter)) {
trans->need_reset = true;
return PTR_ERR(i->iter);
}
i->iter->flags |= BTREE_ITER_KEEP_UNTIL_COMMIT;
bch2_trans_iter_put(trans, i->iter);
......@@ -1054,7 +1036,7 @@ int bch2_trans_update(struct btree_trans *trans, struct btree_iter *iter,
bch2_btree_iter_set_pos(i->iter, n.k->k.p);
}
EBUG_ON(trans->nr_updates >= trans->nr_iters);
EBUG_ON(trans->nr_updates >= BTREE_ITER_MAX);
array_insert_item(trans->updates, trans->nr_updates,
i - trans->updates, n);
......@@ -1069,8 +1051,6 @@ int __bch2_btree_insert(struct btree_trans *trans,
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);
......@@ -1117,9 +1097,6 @@ int bch2_btree_delete_range_trans(struct btree_trans *trans, enum btree_id id,
int ret = 0;
iter = bch2_trans_get_iter(trans, id, start, BTREE_ITER_INTENT);
ret = PTR_ERR_OR_ZERO(iter);
if (ret)
return ret;
retry:
while ((k = bch2_btree_iter_peek(iter)).k &&
!(ret = bkey_err(k)) &&
......
......@@ -1575,9 +1575,6 @@ static int trans_get_key(struct btree_trans *trans,
*iter = bch2_trans_get_iter(trans, btree_id, pos,
flags|BTREE_ITER_INTENT);
if (IS_ERR(*iter))
return PTR_ERR(*iter);
*k = __bch2_btree_iter_peek(*iter, flags);
ret = bkey_err(*k);
if (ret)
......@@ -1605,9 +1602,6 @@ static int bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree
BTREE_ITER_CACHED|
BTREE_ITER_CACHED_NOFILL|
BTREE_ITER_INTENT);
if (IS_ERR(iter))
return PTR_ERR(iter);
ret = bch2_btree_iter_traverse(iter);
if (ret) {
bch2_trans_iter_put(trans, iter);
......
......@@ -2489,10 +2489,7 @@ static long bchfs_fcollapse_finsert(struct bch_inode_info *inode,
src = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
POS(inode->v.i_ino, src_start >> 9),
BTREE_ITER_INTENT);
BUG_ON(IS_ERR_OR_NULL(src));
dst = bch2_trans_copy_iter(&trans, src);
BUG_ON(IS_ERR_OR_NULL(dst));
while (1) {
struct disk_reservation disk_res =
......
......@@ -230,7 +230,6 @@ 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, ret) {
if (bkey_cmp(k2.k->p, k.k->p) >= 0)
......@@ -265,10 +264,8 @@ static void hash_set_chain_start(struct btree_trans *trans,
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;
}
......@@ -440,9 +437,6 @@ static int bch2_fix_overlapping_extent(struct btree_trans *trans,
bch2_cut_front(cut_at, u);
u_iter = bch2_trans_copy_iter(trans, iter);
ret = PTR_ERR_OR_ZERO(u_iter);
if (ret)
return ret;
/*
* We don't want to go through the
......
......@@ -302,9 +302,6 @@ struct btree_iter *bch2_inode_peek(struct btree_trans *trans,
iter = bch2_trans_get_iter(trans, BTREE_ID_INODES, POS(0, inum),
BTREE_ITER_CACHED|flags);
if (IS_ERR(iter))
return iter;
k = bch2_btree_iter_peek_cached(iter);
ret = bkey_err(k);
if (ret)
......@@ -640,9 +637,6 @@ int bch2_inode_find_by_inum_trans(struct btree_trans *trans, u64 inode_nr,
iter = bch2_trans_get_iter(trans, BTREE_ID_INODES,
POS(0, inode_nr), BTREE_ITER_CACHED);
if (IS_ERR(iter))
return PTR_ERR(iter);
k = bch2_btree_iter_peek_cached(iter);
ret = bkey_err(k);
if (ret)
......
......@@ -207,8 +207,6 @@ static int sum_sector_overwrites(struct btree_trans *trans,
*delta = 0;
iter = bch2_trans_copy_iter(trans, extent_iter);
if (IS_ERR(iter))
return PTR_ERR(iter);
for_each_btree_key_continue(iter, BTREE_ITER_SLOTS, old, ret) {
if (!may_allocate &&
......@@ -1781,9 +1779,6 @@ static int __bch2_rbio_narrow_crcs(struct btree_trans *trans,
iter = bch2_trans_get_iter(trans, BTREE_ID_EXTENTS, rbio->pos,
BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
if ((ret = PTR_ERR_OR_ZERO(iter)))
goto out;
k = bch2_btree_iter_peek_slot(iter);
if ((ret = bkey_err(k)))
goto out;
......@@ -1991,10 +1986,6 @@ int __bch2_read_indirect_extent(struct btree_trans *trans,
iter = bch2_trans_get_iter(trans, BTREE_ID_REFLINK,
POS(0, reflink_offset),
BTREE_ITER_SLOTS);
ret = PTR_ERR_OR_ZERO(iter);
if (ret)
return ret;
k = bch2_btree_iter_peek_slot(iter);
ret = bkey_err(k);
if (ret)
......
......@@ -443,9 +443,6 @@ static int bch2_extent_replay_key(struct bch_fs *c, enum btree_id btree_id,
bch2_cut_back(atomic_end, split);
split_iter = bch2_trans_copy_iter(&trans, iter);
ret = PTR_ERR_OR_ZERO(split_iter);
if (ret)
goto err;
/*
* It's important that we don't go through the
......@@ -502,8 +499,6 @@ static int __bch2_journal_replay_key(struct btree_trans *trans,
iter = bch2_trans_get_node_iter(trans, id, k->k.p,
BTREE_MAX_DEPTH, level,
BTREE_ITER_INTENT);
if (IS_ERR(iter))
return PTR_ERR(iter);
/*
* iter->flags & BTREE_ITER_IS_EXTENTS triggers the update path to run
......@@ -538,8 +533,7 @@ static int __bch2_alloc_replay_key(struct btree_trans *trans, struct bkey_i *k)
BTREE_ITER_CACHED|
BTREE_ITER_CACHED_NOFILL|
BTREE_ITER_INTENT);
ret = PTR_ERR_OR_ZERO(iter) ?:
bch2_trans_update(trans, iter, k, BTREE_TRIGGER_NORUN);
ret = bch2_trans_update(trans, iter, k, BTREE_TRIGGER_NORUN);
bch2_trans_iter_put(trans, iter);
return ret;
}
......
......@@ -205,8 +205,6 @@ int bch2_hash_needs_whiteout(struct btree_trans *trans,
int ret;
iter = bch2_trans_copy_iter(trans, start);
if (IS_ERR(iter))
return PTR_ERR(iter);
bch2_btree_iter_next_slot(iter);
......@@ -253,11 +251,8 @@ int bch2_hash_set(struct btree_trans *trans,
}
if (!slot &&
!(flags & BCH_HASH_SET_MUST_REPLACE)) {
!(flags & BCH_HASH_SET_MUST_REPLACE))
slot = bch2_trans_copy_iter(trans, iter);
if (IS_ERR(slot))
return PTR_ERR(slot);
}
if (k.k->type != KEY_TYPE_whiteout)
goto not_found;
......
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