Skip to contentSkip to conversation

btrfs: fix COW handling in run_delalloc_nocow function

PATCH Superseded davechen 2 participants 2 messages Apr 14, 2025 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

davechen

author
#0 lore raw

PATCH 1/1 btrfs: fix COW handling in run_delalloc_nocow function

In run_delalloc_nocow(), when the found btrfs_key's offset > cur_offset, it indicates a gap between he current processing region and the next file extent. The original code would directly jump to the "must_cow" label, which implicitly increments the slot and forces a fallback to COW. This behavior might skip an extent item and result in an overestimated COW fallback range.

This patch modifies the logic so that when a gap is detected:
- If no COW range is already being recorded (cow_start is unset), cow_start is set to cur_offset.
- cur_offset is then advanced to the beginning of the next extent (extent_end).
- Instead of jumping to "must_cow", control flows directly to "next_slot" so that the same extent item can be reexamined properly.

The change ensures that we accurately account for the extent gap and avoid accidentally extending the range that needs to fallback to COW.

---
 fs/btrfs/inode.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/btrfs/inode.c +4 -1
index 5b842276573e..58457bdf984d 100644
@@ -2160,7 +2160,10 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
 		if (found_key.offset > cur_offset) {
 			extent_end = found_key.offset;
 			extent_type = 0;
-			goto must_cow;
+			if (cow_start == (u64)-1)
+				cow_start = cur_offset;
+			cur_offset = extent_end;
+			goto next_slot;
 		}
 
 		/*
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · davechen

In run_delalloc_nocow(), when the found btrfs_key's offset > cur_offset, it indicates a gap between he current processing region and

he -> the

Quoted 2 lines

the next file extent. The original code would directly jump to the "must_cow" label, which implicitly increments the slot and

Implicitly? It's very explicit, we have the expression "path->slots[0]++". So just remove the "implicitly" word.

Quoted 24 lines

forces a fallback to COW. This behavior might skip an extent item and result in an overestimated COW fallback range.

This patch modifies the logic so that when a gap is detected:
- If no COW range is already being recorded (cow_start is unset), cow_start is set to cur_offset.
- cur_offset is then advanced to the beginning of the next extent (extent_end).
- Instead of jumping to "must_cow", control flows directly to "next_slot" so that the same extent item can be reexamined properly.

The change ensures that we accurately account for the extent gap and avoid accidentally extending the range that needs to fallback to COW.

---
 fs/btrfs/inode.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/btrfs/inode.c +0 -0
index 5b842276573e..58457bdf984d 100644

fs/btrfs/inode.c static noinline int run_delalloc_nocow(struct btrfs_inode *inode, · line 2166

Quoted 8 lines
fs/btrfs/inode.c +4 -1
@@ -2160,7 +2160,10 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
                if (found_key.offset > cur_offset) {
                        extent_end = found_key.offset;
                        extent_type = 0;
-                       goto must_cow;
+                       if (cow_start == (u64)-1)
+                               cow_start = cur_offset;
+                       cur_offset = extent_end;
+                       goto next_slot;

Now that we jump to "next_slot" and not "must_cow" anymore, we should also:

1) remove the line above that sets extent_type to 0, no longer needed;

2) no need for setting extent_end either, we can just remove it and set cur_offset to found_key.offset.

Otherwise it looks good, and with those minor changes made:

Thanks.

Quoted 7 lines
fs/btrfs/inode.c +0 -1
                }
 
                /*
--

2.43.0