diff mbox series

btrfs: zoned: fix linked list corruption after log root tree allocation failure

Message ID b4746dac89274ef11314a6abacc58a27c5935a1f.1615475093.git.fdmanana@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs: zoned: fix linked list corruption after log root tree allocation failure | expand

Commit Message

Filipe Manana March 11, 2021, 3:13 p.m. UTC
From: Filipe Manana <fdmanana@suse.com>

When using a zoned filesystem, while syncing the log, if we fail to
allocate the root node for the log root tree, we are not removing the
log context we allocated on stack from the list of log contextes of the
log root tree. This means after the return from btrfs_sync_log() we get
a corrupted linked list.

Fix this by allocating the node before adding our stack allocated context
to the list of log contextes of the log root tree.

Fixes: 3ddebf27fcd3a9 ("btrfs: zoned: reorder log node allocation on zoned filesystem")
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/tree-log.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Johannes Thumshirn March 11, 2021, 3:29 p.m. UTC | #1
Looks good, Thanks!
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Naohiro Aota March 15, 2021, 8:46 a.m. UTC | #2
Looks good to me. Thanks.
Reviewed-by: Naohiro Aota <naohiro.aota@wdc.com>

On Thu, Mar 11, 2021 at 03:13:30PM +0000, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> When using a zoned filesystem, while syncing the log, if we fail to
> allocate the root node for the log root tree, we are not removing the
> log context we allocated on stack from the list of log contextes of the
> log root tree. This means after the return from btrfs_sync_log() we get
> a corrupted linked list.
> 
> Fix this by allocating the node before adding our stack allocated context
> to the list of log contextes of the log root tree.
> 
> Fixes: 3ddebf27fcd3a9 ("btrfs: zoned: reorder log node allocation on zoned filesystem")
> Signed-off-by: Filipe Manana <fdmanana@suse.com>
> ---
>  fs/btrfs/tree-log.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
> index 2f1acc9aea9e..92a368627791 100644
> --- a/fs/btrfs/tree-log.c
> +++ b/fs/btrfs/tree-log.c
> @@ -3169,10 +3169,6 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
>  
>  	mutex_lock(&log_root_tree->log_mutex);
>  
> -	index2 = log_root_tree->log_transid % 2;
> -	list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
> -	root_log_ctx.log_transid = log_root_tree->log_transid;
> -
>  	if (btrfs_is_zoned(fs_info)) {
>  		if (!log_root_tree->node) {
>  			ret = btrfs_alloc_log_tree_node(trans, log_root_tree);
> @@ -3183,6 +3179,10 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
>  		}
>  	}
>  
> +	index2 = log_root_tree->log_transid % 2;
> +	list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
> +	root_log_ctx.log_transid = log_root_tree->log_transid;
> +
>  	/*
>  	 * Now we are safe to update the log_root_tree because we're under the
>  	 * log_mutex, and we're a current writer so we're holding the commit
> -- 
> 2.28.0
>
David Sterba March 15, 2021, 3:34 p.m. UTC | #3
On Thu, Mar 11, 2021 at 03:13:30PM +0000, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> When using a zoned filesystem, while syncing the log, if we fail to
> allocate the root node for the log root tree, we are not removing the
> log context we allocated on stack from the list of log contextes of the
> log root tree. This means after the return from btrfs_sync_log() we get
> a corrupted linked list.
> 
> Fix this by allocating the node before adding our stack allocated context
> to the list of log contextes of the log root tree.
> 
> Fixes: 3ddebf27fcd3a9 ("btrfs: zoned: reorder log node allocation on zoned filesystem")
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Added to misc-next, thanks.
diff mbox series

Patch

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 2f1acc9aea9e..92a368627791 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -3169,10 +3169,6 @@  int btrfs_sync_log(struct btrfs_trans_handle *trans,
 
 	mutex_lock(&log_root_tree->log_mutex);
 
-	index2 = log_root_tree->log_transid % 2;
-	list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
-	root_log_ctx.log_transid = log_root_tree->log_transid;
-
 	if (btrfs_is_zoned(fs_info)) {
 		if (!log_root_tree->node) {
 			ret = btrfs_alloc_log_tree_node(trans, log_root_tree);
@@ -3183,6 +3179,10 @@  int btrfs_sync_log(struct btrfs_trans_handle *trans,
 		}
 	}
 
+	index2 = log_root_tree->log_transid % 2;
+	list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
+	root_log_ctx.log_transid = log_root_tree->log_transid;
+
 	/*
 	 * Now we are safe to update the log_root_tree because we're under the
 	 * log_mutex, and we're a current writer so we're holding the commit