diff mbox series

[2/8] btrfs: error when COWing block from a root that is being deleted

Message ID ff0e4083df92ea1a6d36ba8f53271b74b098a0cd.1695731842.git.fdmanana@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs: some fixes and cleanups around btrfs_cow_block() | expand

Commit Message

Filipe Manana Sept. 26, 2023, 12:45 p.m. UTC
From: Filipe Manana <fdmanana@suse.com>

At btrfs_cow_block() we check if the block being COWed belongs to a root
that is being deleted and if so we log an error message. However this is
an unexpected case and it indicates a bug somewhere, so we should return
an error and abort the transaction. So change this in the following ways:

1) Move the check after the checks for a stale transaction, so that if
   those checks pass, we can abort the transaction;

2) Abort the transaction with -EUCLEAN, so that if the issue ever happens
   it can easily be noticed;

3) Change the logged message level from error to critical, and change the
   message itself to print the block's logical address and the ID of the
   root;

4) Return -EUCLEAN to the caller;

5) As this is an unexpected scenario, that should never happen, mark the
   check as unlikely, allowing the compiler to potentially generate better
   code.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/ctree.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index dff2e07ba437..4eef1a7d1db6 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -682,10 +682,6 @@  noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
 	u64 search_start;
 	int ret;
 
-	if (test_bit(BTRFS_ROOT_DELETING, &root->state))
-		btrfs_err(fs_info,
-			"COW'ing blocks on a fs root that's being dropped");
-
 	/*
 	 * COWing must happen through a running transaction, which always
 	 * matches the current fs generation (it's a transaction with a state
@@ -703,6 +699,14 @@  noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
 		return -EUCLEAN;
 	}
 
+	if (unlikely(test_bit(BTRFS_ROOT_DELETING, &root->state))) {
+		btrfs_abort_transaction(trans, -EUCLEAN);
+		btrfs_crit(fs_info,
+		   "attempt to COW block %llu on root %llu that is being deleted",
+			   buf->start, btrfs_root_id(root));
+		return -EUCLEAN;
+	}
+
 	if (!should_cow_block(trans, root, buf)) {
 		*cow_ret = buf;
 		return 0;