Skip to content

Commit

Permalink
bcachefs: bch2_trans_verify_not_unlocked_or_in_restart()
Browse files Browse the repository at this point in the history
Fold two asserts into one.

Signed-off-by: Kent Overstreet <[email protected]>
  • Loading branch information
Kent Overstreet committed Nov 12, 2024
1 parent 9960369 commit c4392c3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 40 deletions.
36 changes: 20 additions & 16 deletions fs/bcachefs/btree_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ static int bch2_btree_iter_verify_ret(struct btree_iter *iter, struct bkey_s_c k
void bch2_assert_pos_locked(struct btree_trans *trans, enum btree_id id,
struct bpos pos)
{
bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);

struct btree_path *path;
struct trans_for_each_path_inorder_iter iter;
Expand Down Expand Up @@ -1265,7 +1265,7 @@ __bch2_btree_path_set_pos(struct btree_trans *trans,
{
int cmp = bpos_cmp(new_pos, trans->paths[path_idx].pos);

bch2_trans_verify_not_in_restart(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);
EBUG_ON(!trans->paths[path_idx].ref);

trace_btree_path_set_pos(trans, trans->paths + path_idx, &new_pos);
Expand Down Expand Up @@ -1425,7 +1425,7 @@ void __noreturn bch2_trans_restart_error(struct btree_trans *trans, u32 restart_
(void *) trans->last_begin_ip);
}

void __noreturn bch2_trans_in_restart_error(struct btree_trans *trans)
static void __noreturn bch2_trans_in_restart_error(struct btree_trans *trans)
{
#ifdef CONFIG_BCACHEFS_DEBUG
struct printbuf buf = PRINTBUF;
Expand All @@ -1440,10 +1440,16 @@ void __noreturn bch2_trans_in_restart_error(struct btree_trans *trans)
#endif
}

void __noreturn bch2_trans_unlocked_error(struct btree_trans *trans)
void __noreturn bch2_trans_unlocked_or_in_restart_error(struct btree_trans *trans)
{
panic("trans should be locked, unlocked by %pS\n",
(void *) trans->last_unlock_ip);
if (trans->restarted)
bch2_trans_in_restart_error(trans);

if (!trans->locked)
panic("trans should be locked, unlocked by %pS\n",
(void *) trans->last_unlock_ip);

BUG();
}

noinline __cold
Expand Down Expand Up @@ -1724,8 +1730,7 @@ btree_path_idx_t bch2_path_get(struct btree_trans *trans,
struct trans_for_each_path_inorder_iter iter;
btree_path_idx_t path_pos = 0, path_idx;

bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_in_restart(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);
bch2_trans_verify_locks(trans);

btree_trans_sort_paths(trans);
Expand Down Expand Up @@ -1877,7 +1882,7 @@ bch2_btree_iter_traverse(struct btree_iter *iter)
struct btree_trans *trans = iter->trans;
int ret;

bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);

iter->path = bch2_btree_path_set_pos(trans, iter->path,
btree_iter_search_key(iter),
Expand Down Expand Up @@ -1952,7 +1957,7 @@ struct btree *bch2_btree_iter_next_node(struct btree_iter *iter)
int ret;

EBUG_ON(trans->paths[iter->path].cached);
bch2_trans_verify_not_in_restart(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);
bch2_btree_iter_verify(iter);

ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
Expand Down Expand Up @@ -2161,8 +2166,7 @@ struct bkey_s_c btree_trans_peek_key_cache(struct btree_iter *iter, struct bpos
struct bkey_s_c k;
int ret;

bch2_trans_verify_not_in_restart(trans);
bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);

if ((iter->flags & BTREE_ITER_key_cache_fill) &&
bpos_eq(iter->pos, pos))
Expand Down Expand Up @@ -2302,7 +2306,7 @@ struct bkey_s_c bch2_btree_iter_peek_upto(struct btree_iter *iter, struct bpos e
struct bpos iter_pos;
int ret;

bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);
EBUG_ON((iter->flags & BTREE_ITER_filter_snapshots) && bkey_eq(end, POS_MAX));

if (iter->update_path) {
Expand Down Expand Up @@ -2475,7 +2479,7 @@ struct bkey_s_c bch2_btree_iter_peek_prev(struct btree_iter *iter)
btree_path_idx_t saved_path = 0;
int ret;

bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);
EBUG_ON(btree_iter_path(trans, iter)->cached ||
btree_iter_path(trans, iter)->level);

Expand Down Expand Up @@ -2614,7 +2618,7 @@ struct bkey_s_c bch2_btree_iter_peek_slot(struct btree_iter *iter)
struct bkey_s_c k;
int ret;

bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);
bch2_btree_iter_verify(iter);
bch2_btree_iter_verify_entry_exit(iter);
EBUG_ON(btree_iter_path(trans, iter)->level && (iter->flags & BTREE_ITER_with_key_cache));
Expand Down Expand Up @@ -3136,7 +3140,7 @@ u32 bch2_trans_begin(struct btree_trans *trans)
trans->notrace_relock_fail = false;
}

bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);
return trans->restart_count;
}

Expand Down
20 changes: 6 additions & 14 deletions fs/bcachefs/btree_iter.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ int __must_check bch2_btree_path_traverse_one(struct btree_trans *,
btree_path_idx_t,
unsigned, unsigned long);

static inline void bch2_trans_verify_not_unlocked(struct btree_trans *);
static inline void bch2_trans_verify_not_unlocked_or_in_restart(struct btree_trans *);

static inline int __must_check bch2_btree_path_traverse(struct btree_trans *trans,
btree_path_idx_t path, unsigned flags)
{
bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);

if (trans->paths[path].uptodate < BTREE_ITER_NEED_RELOCK)
return 0;
Expand Down Expand Up @@ -326,20 +326,12 @@ static inline void bch2_trans_verify_not_restarted(struct btree_trans *trans,
bch2_trans_restart_error(trans, restart_count);
}

void __noreturn bch2_trans_in_restart_error(struct btree_trans *);
void __noreturn bch2_trans_unlocked_or_in_restart_error(struct btree_trans *);

static inline void bch2_trans_verify_not_in_restart(struct btree_trans *trans)
static inline void bch2_trans_verify_not_unlocked_or_in_restart(struct btree_trans *trans)
{
if (trans->restarted)
bch2_trans_in_restart_error(trans);
}

void __noreturn bch2_trans_unlocked_error(struct btree_trans *);

static inline void bch2_trans_verify_not_unlocked(struct btree_trans *trans)
{
if (!trans->locked)
bch2_trans_unlocked_error(trans);
if (trans->restarted || !trans->locked)
bch2_trans_unlocked_or_in_restart_error(trans);
}

__always_inline
Expand Down
2 changes: 1 addition & 1 deletion fs/bcachefs/btree_locking.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ static inline int btree_node_lock(struct btree_trans *trans,
int ret = 0;

EBUG_ON(level >= BTREE_MAX_DEPTH);
bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);

if (likely(six_trylock_type(&b->lock, type)) ||
btree_node_lock_increment(trans, b, level, (enum btree_node_locked_type) type) ||
Expand Down
9 changes: 3 additions & 6 deletions fs/bcachefs/btree_trans_commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,7 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
unsigned u64s = 0;
int ret = 0;

bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_in_restart(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);

if (race_fault()) {
trace_and_count(c, trans_restart_fault_inject, trans, trace_ip);
Expand Down Expand Up @@ -1008,8 +1007,7 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
struct bch_fs *c = trans->c;
int ret = 0;

bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_in_restart(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);

if (!trans->nr_updates &&
!trans->journal_entries_u64s)
Expand Down Expand Up @@ -1070,8 +1068,7 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
}
retry:
errored_at = NULL;
bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_in_restart(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);
if (likely(!(flags & BCH_TRANS_COMMIT_no_journal_res)))
memset(&trans->journal_res, 0, sizeof(trans->journal_res));
memset(&trans->fs_usage_delta, 0, sizeof(trans->fs_usage_delta));
Expand Down
3 changes: 1 addition & 2 deletions fs/bcachefs/btree_update_interior.c
Original file line number Diff line number Diff line change
Expand Up @@ -1960,8 +1960,7 @@ int __bch2_foreground_maybe_merge(struct btree_trans *trans,
u64 start_time = local_clock();
int ret = 0;

bch2_trans_verify_not_in_restart(trans);
bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);
BUG_ON(!trans->paths[path].should_be_locked);
BUG_ON(!btree_node_locked(&trans->paths[path], level));

Expand Down
2 changes: 1 addition & 1 deletion fs/bcachefs/btree_update_interior.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ static inline int bch2_foreground_maybe_merge(struct btree_trans *trans,
unsigned level,
unsigned flags)
{
bch2_trans_verify_not_unlocked(trans);
bch2_trans_verify_not_unlocked_or_in_restart(trans);

return bch2_foreground_maybe_merge_sibling(trans, path, level, flags,
btree_prev_sib) ?:
Expand Down

0 comments on commit c4392c3

Please sign in to comment.