Skip to contentSkip to conversation

btrfs: check snapshot_force_cow earlier in can_nocow_file_extent

PATCH In review jkchen 2 participants 3 messages Feb 16 – Feb 17 1 message not in the archive Btrfs file system

lore
Share & tools
lore

jkchen

author
#0 lore raw

PATCH 1/1 btrfs: check snapshot_force_cow earlier in can_nocow_file_extent

When a snapshot is being created, the atomic counter snapshot_force_cow is incremented to force incoming writes to fallback to COW. This is a critical mechanism to protect the consistency of the snapshot being taken.

Currently, can_nocow_file_extent() checks this counter only after performing several checks, most notably the expensive cross-reference check via btrfs_cross_ref_exist(). btrfs_cross_ref_exist() releases the path and performs a search in the extent tree or backref cache, which involves btree traversals and locking overhead.

This patch moves the snapshot_force_cow check to the very beginning of can_nocow_file_extent().

This reordering is safe and beneficial because:
1. args->writeback_path is invariant for the duration of the call (set by caller run_delalloc_nocow).
2. is_freespace_inode is a static property of the inode.
3. The state of snapshot_force_cow is driven by the btrfs_mksnapshot process. Checking it earlier does not change the outcome of the NOCOW decision, but effectively prunes the expensive code path when a fallback to COW is inevitable.

By failing fast when a snapshot is pending, we avoid the unnecessary overhead of btrfs_cross_ref_exist() and other extent item checks in the scenario where NOCOW is already known to be impossible.

---
 fs/btrfs/inode.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
fs/btrfs/inode.c +5 -5
index 845164485..3549031f3 100644
@@ -1921,6 +1921,11 @@ static int can_nocow_file_extent(struct btrfs_path *path,
 	int ret = 0;
 	bool nowait = path->nowait;
 
+	/* If there are pending snapshots for this root, we must COW. */
+	if (args->writeback_path && !is_freespace_inode &&
+	    atomic_read(&root->snapshot_force_cow))
+		goto out;
+
 	fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
 	extent_type = btrfs_file_extent_type(leaf, fi);
 
@@ -1982,11 +1987,6 @@ static int can_nocow_file_extent(struct btrfs_path *path,
 		path = NULL;
 	}
 
-	/* If there are pending snapshots for this root, we must COW. */
-	if (args->writeback_path && !is_freespace_inode &&
-	    atomic_read(&root->snapshot_force_cow))
-		goto out;
-
 	args->file_extent.num_bytes = min(args->end + 1, extent_end) - args->start;
 	args->file_extent.offset += args->start - key->offset;
 	io_start = args->file_extent.disk_bytenr + args->file_extent.offset;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 23 lines · jkchen

When a snapshot is being created, the atomic counter snapshot_force_cow is incremented to force incoming writes to fallback to COW. This is a critical mechanism to protect the consistency of the snapshot being taken.

Currently, can_nocow_file_extent() checks this counter only after performing several checks, most notably the expensive cross-reference check via btrfs_cross_ref_exist(). btrfs_cross_ref_exist() releases the path and performs a search in the extent tree or backref cache, which involves btree traversals and locking overhead.

This patch moves the snapshot_force_cow check to the very beginning of can_nocow_file_extent().

This reordering is safe and beneficial because:
1. args->writeback_path is invariant for the duration of the call (set by caller run_delalloc_nocow).
2. is_freespace_inode is a static property of the inode.
3. The state of snapshot_force_cow is driven by the btrfs_mksnapshot process. Checking it earlier does not change the outcome of the NOCOW decision, but effectively prunes the expensive code path when a fallback to COW is inevitable.

Quoted 5 lines

By failing fast when a snapshot is pending, we avoid the unnecessary overhead of btrfs_cross_ref_exist() and other extent item checks in the scenario where NOCOW is already known to be impossible.

Looks good, I'll add it to the github for-next branch, thanks.

Quoted 36 lines
---
 fs/btrfs/inode.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
fs/btrfs/inode.c +5 -6
index 845164485..3549031f3 100644
@@ -1921,6 +1921,11 @@ static int can_nocow_file_extent(struct btrfs_path *path,
        int ret = 0;
        bool nowait = path->nowait;
 
+       /* If there are pending snapshots for this root, we must COW. */
+       if (args->writeback_path && !is_freespace_inode &&
+           atomic_read(&root->snapshot_force_cow))
+               goto out;
+
        fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
        extent_type = btrfs_file_extent_type(leaf, fi);
 
@@ -1982,11 +1987,6 @@ static int can_nocow_file_extent(struct btrfs_path *path,
                path = NULL;
        }
 
-       /* If there are pending snapshots for this root, we must COW. */
-       if (args->writeback_path && !is_freespace_inode &&
-           atomic_read(&root->snapshot_force_cow))
-               goto out;
-
        args->file_extent.num_bytes = min(args->end + 1, extent_end) - args->start;
        args->file_extent.offset += args->start - key->offset;
        io_start = args->file_extent.disk_bytenr + args->file_extent.offset;
--

2.43.0

A message that is not in the archive (sent off-list or not yet received): CAJFOPmtqt_u_JUQyntOjr+0W9X_CgcAXCkx8gArR9-L57Et5TA@mail.gmail.com

#3 lore raw
Quoted 9 lines · Guan-Jie Chen

Hi Filipe,

Thanks again for picking up the patch.

I realized that I should use my full real name and my personal email for the Signed-off-by line. Could you please update it to:

Ok, updated it.

Quoted 78 lines

Sorry for the trouble, and thank you for your help.

Best regards,
Chen Guan Jie

Quoted 71 lines · On Tue, Feb 17, 2026 at 19:42 Filipe Manana <[email protected]> wrote:
Quoted 23 lines · jkchen

When a snapshot is being created, the atomic counter snapshot_force_cow is incremented to force incoming writes to fallback to COW. This is a critical mechanism to protect the consistency of the snapshot being taken.

Currently, can_nocow_file_extent() checks this counter only after performing several checks, most notably the expensive cross-reference check via btrfs_cross_ref_exist(). btrfs_cross_ref_exist() releases the path and performs a search in the extent tree or backref cache, which involves btree traversals and locking overhead.

This patch moves the snapshot_force_cow check to the very beginning of can_nocow_file_extent().

This reordering is safe and beneficial because:
1. args->writeback_path is invariant for the duration of the call (set by caller run_delalloc_nocow).
2. is_freespace_inode is a static property of the inode.
3. The state of snapshot_force_cow is driven by the btrfs_mksnapshot process. Checking it earlier does not change the outcome of the NOCOW decision, but effectively prunes the expensive code path when a fallback to COW is inevitable.

Quoted 5 lines

By failing fast when a snapshot is pending, we avoid the unnecessary overhead of btrfs_cross_ref_exist() and other extent item checks in the scenario where NOCOW is already known to be impossible.

Looks good, I'll add it to the github for-next branch, thanks.

Quoted 36 lines
---
 fs/btrfs/inode.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
fs/btrfs/inode.c +5 -6
index 845164485..3549031f3 100644
@@ -1921,6 +1921,11 @@ static int can_nocow_file_extent(struct btrfs_path *path,
        int ret = 0;
        bool nowait = path->nowait;
 
+       /* If there are pending snapshots for this root, we must COW. */
+       if (args->writeback_path && !is_freespace_inode &&
+           atomic_read(&root->snapshot_force_cow))
+               goto out;
+
        fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
        extent_type = btrfs_file_extent_type(leaf, fi);
 
@@ -1982,11 +1987,6 @@ static int can_nocow_file_extent(struct btrfs_path *path,
                path = NULL;
        }
 
-       /* If there are pending snapshots for this root, we must COW. */
-       if (args->writeback_path && !is_freespace_inode &&
-           atomic_read(&root->snapshot_force_cow))
-               goto out;
-
        args->file_extent.num_bytes = min(args->end + 1, extent_end) - args->start;
        args->file_extent.offset += args->start - key->offset;
        io_start = args->file_extent.disk_bytenr + args->file_extent.offset;
--

2.43.0