Commit 42590b53 authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: bch2_btree_delete_range_trans() now returns -BCH_ERR_transaction_restart_nested

The new convention is that functions that handle transaction restarts
within an existing transaction context should return
-BCH_ERR_transaction_restart_nested when they did so, since they
invalidated the outer transaction context.

This also means bch2_btree_delete_range_trans() is changed to only call
bch2_trans_begin() after a transaction restart, not on every loop
iteration.

This is to fix a bug in fsck, in check_inode() when we truncate an inode
with BCH_INODE_I_SIZE_DIRTY set.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@gmail.com>
parent efd0d038
...@@ -1713,15 +1713,16 @@ int bch2_btree_delete_range_trans(struct btree_trans *trans, enum btree_id id, ...@@ -1713,15 +1713,16 @@ int bch2_btree_delete_range_trans(struct btree_trans *trans, enum btree_id id,
unsigned update_flags, unsigned update_flags,
u64 *journal_seq) u64 *journal_seq)
{ {
u32 restart_count = trans->restart_count;
struct btree_iter iter; struct btree_iter iter;
struct bkey_s_c k; struct bkey_s_c k;
int ret = 0; int ret = 0;
bch2_trans_iter_init(trans, &iter, id, start, BTREE_ITER_INTENT); bch2_trans_iter_init(trans, &iter, id, start, BTREE_ITER_INTENT);
retry: retry:
while ((bch2_trans_begin(trans), while ((k = bch2_btree_iter_peek(&iter)).k &&
(k = bch2_btree_iter_peek(&iter)).k) && !(ret = bkey_err(k) ?:
!(ret = bkey_err(k)) && btree_trans_too_many_iters(trans)) &&
bkey_cmp(iter.pos, end) < 0) { bkey_cmp(iter.pos, end) < 0) {
struct disk_reservation disk_res = struct disk_reservation disk_res =
bch2_disk_reservation_init(trans->c, 0); bch2_disk_reservation_init(trans->c, 0);
...@@ -1767,11 +1768,15 @@ int bch2_btree_delete_range_trans(struct btree_trans *trans, enum btree_id id, ...@@ -1767,11 +1768,15 @@ int bch2_btree_delete_range_trans(struct btree_trans *trans, enum btree_id id,
} }
if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) { if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
bch2_trans_begin(trans);
ret = 0; ret = 0;
goto retry; goto retry;
} }
bch2_trans_iter_exit(trans, &iter); bch2_trans_iter_exit(trans, &iter);
if (!ret && trans_was_restarted(trans, restart_count))
ret = -BCH_ERR_transaction_restart_nested;
return ret; return ret;
} }
...@@ -1785,8 +1790,12 @@ int bch2_btree_delete_range(struct bch_fs *c, enum btree_id id, ...@@ -1785,8 +1790,12 @@ int bch2_btree_delete_range(struct bch_fs *c, enum btree_id id,
unsigned update_flags, unsigned update_flags,
u64 *journal_seq) u64 *journal_seq)
{ {
return bch2_trans_run(c, int ret = bch2_trans_run(c,
bch2_btree_delete_range_trans(&trans, id, start, end, update_flags, journal_seq)); bch2_btree_delete_range_trans(&trans, id, start, end,
update_flags, journal_seq));
if (ret == -BCH_ERR_transaction_restart_nested)
ret = 0;
return ret;
} }
int bch2_trans_log_msg(struct btree_trans *trans, const char *msg) int bch2_trans_log_msg(struct btree_trans *trans, const char *msg)
......
...@@ -19,6 +19,10 @@ ...@@ -19,6 +19,10 @@
#define QSTR(n) { { { .len = strlen(n) } }, .name = n } #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
/*
* XXX: this is handling transaction restarts without returning
* -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
*/
static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum, static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
u32 snapshot) u32 snapshot)
{ {
...@@ -239,18 +243,20 @@ static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot) ...@@ -239,18 +243,20 @@ static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
struct bkey_s_c k; struct bkey_s_c k;
int ret; int ret;
ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents, do {
SPOS(inum, 0, snapshot), ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
SPOS(inum, U64_MAX, snapshot), SPOS(inum, 0, snapshot),
0, NULL) ?: SPOS(inum, U64_MAX, snapshot),
bch2_btree_delete_range_trans(trans, BTREE_ID_dirents, 0, NULL) ?:
SPOS(inum, 0, snapshot), bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
SPOS(inum, U64_MAX, snapshot), SPOS(inum, 0, snapshot),
0, NULL) ?: SPOS(inum, U64_MAX, snapshot),
bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs, 0, NULL) ?:
SPOS(inum, 0, snapshot), bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
SPOS(inum, U64_MAX, snapshot), SPOS(inum, 0, snapshot),
0, NULL); SPOS(inum, U64_MAX, snapshot),
0, NULL);
} while (ret == -BCH_ERR_transaction_restart_nested);
if (ret) if (ret)
goto err; goto err;
retry: retry:
......
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