btrfs: some cleanups for the log tree replay code
cover btrfs: some cleanups for the log tree replay code
From: Filipe Manana <[email protected]>
A set of trivial cleanups after running into a bug I'm debugging where replaying directory deletes is failing. Details in the change logs.
Filipe Manana (4):
btrfs: unfold transaction aborts when replaying log trees
btrfs: abort transaction during log replay if walk_log_tree() failed
btrfs: remove redundant path release when replaying a log tree
btrfs: simplify error detection flow during log replay
fs/btrfs/tree-log.c | 64 ++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 27 deletions(-)
Signature · 1 line
2.47.2
PATCH 1/4 btrfs: unfold transaction aborts when replaying log trees
From: Filipe Manana <[email protected]>
We have a single line doing a transaction abort in case either we got an error from btrfs_get_fs_root() different from -ENOENT or we got an error from btrfs_pin_extent_for_log_replay(), making it hard to figure out which function call failed when looking at a transaction abort massages and stack trace in dmesg. Change this to have an explicit transaction abort for each one of the two cases.
--- fs/btrfs/tree-log.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
fs/btrfs/tree-log.c +6 -2
@@ -7255,6 +7255,11 @@ int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) true);if (IS_ERR (wc.replay_dest)) { ret =PTR_ERR (wc.replay_dest); +if (ret != -ENOENT ) { +btrfs_put_root (log); +btrfs_abort_transaction (trans, ret); + goto error; + }/* * We didn't find the subvol, likely because it was
@@ -7267,8 +7272,7 @@ int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)* block from being modified, and we'll just bail for * each subsequent pass. */ -if (ret == -ENOENT ) - ret =btrfs_pin_extent_for_log_replay (trans, log->node); + ret =btrfs_pin_extent_for_log_replay (trans, log->node);btrfs_put_root (log);if (!ret)
Signature · 1 line
2.47.2
PATCH 2/4 btrfs: abort transaction during log replay if walk_log_tree() failed
In reply to [email protected]From: Filipe Manana <[email protected]>
If we failed walking a log tree during replay, we have a missing transaction abort to prevent committing a transaction where we didn't fully replay all the changes from a log tree and therefore can leave the respective subvolume tree in some incosistent state. So add the missing transaction abort.
--- fs/btrfs/tree-log.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
fs/btrfs/tree-log.c +5 -2
@@ -7283,11 +7283,14 @@ int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) wc.replay_dest->log_root = log; ret =btrfs_record_root_in_trans (trans, wc.replay_dest); -if (ret) +if (ret) {/* The loop needs to continue due to the root refs */ btrfs_abort_transaction (trans, ret); -else + }else { ret =walk_log_tree (trans, log, &wc); +if (ret) +btrfs_abort_transaction (trans, ret); + }if (!ret && wc.stage ==LOG_WALK_REPLAY_ALL ) { ret = fixup_inode_link_counts(trans, wc.replay_dest,
Signature · 1 line
2.47.2
PATCH 3/4 btrfs: remove redundant path release when replaying a log tree
In reply to [email protected]From: Filipe Manana <[email protected]>
There's no need to call btrfs_release_path() before calling btrfs_init_root_free_objectid() as we have released the path already at the top of the loop and the previous call to fixup_inode_link_counts() also releases the path. So remove it to simplify the code.
--- fs/btrfs/tree-log.c | 2 -- 1 file changed, 2 deletions(-)
fs/btrfs/tree-log.c +0 -2
@@ -7302,8 +7302,6 @@ int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)if (!ret && wc.stage ==LOG_WALK_REPLAY_ALL ) {struct btrfs_root *root = wc.replay_dest; -btrfs_release_path (path); -/* * We have just replayed everything, and the highest * objectid of fs roots probably has changed in case
Signature · 1 line
2.47.2
PATCH 4/4 btrfs: simplify error detection flow during log replay
In reply to [email protected]From: Filipe Manana <[email protected]>
We have this fuzzy logic at btrfs_recover_log_trees() where we don't abort the transaction and exit immediately after each function call that returned an error, and instead have if-then-else logic or check if the previous function call returned success before calling the next function.
Make the flow more straighforward by immediately aborting the transaction and exiting after each function call failure. This also allows to avoid two consecutive if statements that test the same conditions:
if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
(...)
}--- fs/btrfs/tree-log.c | 53 +++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 24 deletions(-)
fs/btrfs/tree-log.c +29 -24
@@ -7192,8 +7192,6 @@ int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)struct btrfs_path *path;struct btrfs_trans_handle *trans;struct btrfs_key key; -struct btrfs_key found_key; -struct btrfs_root *log;struct btrfs_fs_info *fs_info = log_root_tree->fs_info;struct walk_control wc = { .process_func = process_one_buffer,
@@ -7227,6 +7225,9 @@ int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) key.offset = (u64)-1 ;while (1 ) { +struct btrfs_root *log; +struct btrfs_key found_key; + ret =btrfs_search_slot (NULL , log_root_tree, &key, path,0 ,0 );if (ret <0 ) {
@@ -7255,6 +7256,7 @@ int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) true);if (IS_ERR (wc.replay_dest)) { ret =PTR_ERR (wc.replay_dest); + wc.replay_dest =NULL ;if (ret != -ENOENT ) {btrfs_put_root (log);btrfs_abort_transaction (trans, ret);
@@ -7273,35 +7275,35 @@ int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)* each subsequent pass. */ ret =btrfs_pin_extent_for_log_replay (trans, log->node); -btrfs_put_root (log); - -if (!ret) - goto next; -btrfs_abort_transaction (trans, ret); - goto error; +if (ret) { +btrfs_put_root (log); +btrfs_abort_transaction (trans, ret); + goto error; + } + goto next; } wc.replay_dest->log_root = log; ret =btrfs_record_root_in_trans (trans, wc.replay_dest);if (ret) { -/* The loop needs to continue due to the root refs */ btrfs_abort_transaction (trans, ret); - }else { - ret =walk_log_tree (trans, log, &wc); -if (ret) -btrfs_abort_transaction (trans, ret); + goto next; } -if (!ret && wc.stage ==LOG_WALK_REPLAY_ALL ) { - ret =fixup_inode_link_counts (trans, wc.replay_dest, - path); -if (ret) -btrfs_abort_transaction (trans, ret); + ret =walk_log_tree (trans, log, &wc); +if (ret) { +btrfs_abort_transaction (trans, ret); + goto next; } -if (!ret && wc.stage ==LOG_WALK_REPLAY_ALL ) { +if (wc.stage ==LOG_WALK_REPLAY_ALL ) {struct btrfs_root *root = wc.replay_dest; + ret =fixup_inode_link_counts (trans, wc.replay_dest, path); +if (ret) { +btrfs_abort_transaction (trans, ret); + goto next; + }/* * We have just replayed everything, and the highest * objectid of fs roots probably has changed in case
@@ -7311,17 +7313,20 @@ int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)* could only happen during mount. */ ret =btrfs_init_root_free_objectid (root); -if (ret) +if (ret) {btrfs_abort_transaction (trans, ret); + goto next; + } + } +next: +if (wc.replay_dest) { + wc.replay_dest->log_root =NULL ; +btrfs_put_root (wc.replay_dest); } - - wc.replay_dest->log_root =NULL ; -btrfs_put_root (wc.replay_dest);btrfs_put_root (log);if (ret) goto error; -next:if (found_key.offset ==0 )break ; key.offset = found_key.offset -1 ;
Signature · 1 line
2.47.2
Quoted 4 lines · 在 2025/5/22 03:14, [email protected] 写道:
From: Filipe Manana <[email protected]>
A set of trivial cleanups after running into a bug I'm debugging where replaying directory deletes is failing. Details in the change logs.
Thanks,
Qu
Quoted 10 lines
Filipe Manana (4):
btrfs: unfold transaction aborts when replaying log trees
btrfs: abort transaction during log replay ifwalk_log_tree()failed
btrfs: remove redundant path release when replaying a log tree
btrfs: simplify error detection flow during log replayfs/btrfs/tree-log.c | 64 ++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 27 deletions(-)