Skip to contentSkip to conversation

btrfs: fix nocow sequence in btrfs_run_delalloc_range()

PATCH In review Goldwyn Rodrigues 3 participants 3 messages Mar 4, 2021 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: fix nocow sequence in btrfs_run_delalloc_range()

need_force_cow will evaluate to false if both BTRFS_INODE_NODATACOW and BTRFS_INODE_PREALLOC are not set. Change the function to should_cow() instead and correct the conditions so should_nocow() returns true if either BTRFS_INODE_NODATACOW or BTRFS_INODE_PREALLOC, but it is not a defrag extent.

Fixes 7e33213f8ccc btrfs: remove force argument from run_delalloc_nocow()
---
 fs/btrfs/inode.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)
fs/btrfs/inode.c +10 -17
index 0b133fda4f5d..ceb6ca7c571d 100644
@@ -1864,23 +1864,16 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
 	return ret;
 }
 
-static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
+static inline bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
 {
-
-	if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
-	    !(inode->flags & BTRFS_INODE_PREALLOC))
-		return 0;
-
-	/*
-	 * @defrag_bytes is a hint value, no spinlock held here,
-	 * if is not zero, it means the file is defragging.
-	 * Force cow if given extent needs to be defragged.
-	 */
-	if (inode->defrag_bytes &&
-	    test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
-		return 1;
-
-	return 0;
+	if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
+		if (inode->defrag_bytes &&
+		    test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
+				   0, NULL))
+			return false;
+		return true;
+	}
+	return false;
 }
 
 /*
@@ -1894,7 +1887,7 @@ int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page
 	int ret;
 	const bool zoned = btrfs_is_zoned(inode->root->fs_info);
 
-	if (!need_force_cow(inode, start, end)) {
+	if (should_nocow(inode, start, end)) {
 		ASSERT(!zoned);
 		ret = run_delalloc_nocow(inode, locked_page, start, end,
 					 page_started, nr_written);
Signature · 5 lines
2.30.1


-- 
Goldwyn
Tested-by Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Goldwyn Rodrigues

need_force_cow will evaluate to false if both BTRFS_INODE_NODATACOW and BTRFS_INODE_PREALLOC are not set. Change the function to should_cow() instead and correct the conditions so should_nocow() returns true if either BTRFS_INODE_NODATACOW or BTRFS_INODE_PREALLOC, but it is not a defrag extent.

Fixes 7e33213f8ccc btrfs: remove force argument from run_delalloc_nocow()

This fixes the ASSERT() I've seen with zoned filesystems.

Thanks,
Johannes

#2 lore raw
In reply to Goldwyn Rodrigues
Quoted 7 lines · Goldwyn Rodrigues

need_force_cow will evaluate to false if both BTRFS_INODE_NODATACOW and BTRFS_INODE_PREALLOC are not set. Change the function to should_cow() instead and correct the conditions so should_nocow() returns true if either BTRFS_INODE_NODATACOW or BTRFS_INODE_PREALLOC, but it is not a defrag extent.

Fixes 7e33213f8ccc btrfs: remove force argument from run_delalloc_nocow()

It would be better to send v2 of that patch, a cleanup with a fix should be just one patch.