Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH] btrfs: skip clearing EXTENT_DEFRAG for nocow ordered extents
@ 2026-03-30  3:31 Dave Chen
  2026-04-07 11:23 ` Filipe Manana
  0 siblings, 1 reply; 2+ messages in thread
From: Dave Chen @ 2026-03-30  3:31 UTC (permalink / raw)
  To: linux-btrfs, dsterba; +Cc: cccheng, robbieko, Dave Chen

In btrfs_finish_one_ordered(), clear_bits is unconditionally initialized
with EXTENT_DEFRAG.  For nocow ordered extents this is always a no-op
because should_nocow() already forces the COW path when EXTENT_DEFRAG is
set, so a nocow ordered extent can never have EXTENT_DEFRAG on its range.

Although harmless, the unconditional btrfs_clear_extent_bit() call still
performs a cold rbtree lookup under the io tree spinlock on every nocow
write completion.  Avoid this by only adding EXTENT_DEFRAG to clear_bits
for non-nocow ordered extents, and skip the call entirely when there are
no bits to clear.

Signed-off-by: Dave Chen <davechen@synology.com>
Signed-off-by: Robbie Ko <robbieko@synology.com>
---
 fs/btrfs/inode.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index f643a05208720..80daf7e248ab8 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -3203,7 +3203,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
 	bool freespace_inode;
 	bool truncated = false;
 	bool clear_reserved_extent = true;
-	unsigned int clear_bits = EXTENT_DEFRAG;
+	unsigned int clear_bits = 0;
 
 	start = ordered_extent->file_offset;
 	end = start + ordered_extent->num_bytes - 1;
@@ -3214,6 +3214,9 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
 	    !test_bit(BTRFS_ORDERED_ENCODED, &ordered_extent->flags))
 		clear_bits |= EXTENT_DELALLOC_NEW;
 
+	if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
+		clear_bits |= EXTENT_DEFRAG;
+
 	freespace_inode = btrfs_is_free_space_inode(inode);
 	if (!freespace_inode)
 		btrfs_lockdep_acquire(fs_info, btrfs_ordered_extent);
@@ -3345,8 +3348,9 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
 		goto out;
 	}
 out:
-	btrfs_clear_extent_bit(&inode->io_tree, start, end, clear_bits,
-			       &cached_state);
+	if (clear_bits)
+		btrfs_clear_extent_bit(&inode->io_tree, start, end, clear_bits,
+				       &cached_state);
 
 	if (trans)
 		btrfs_end_transaction(trans);
-- 
2.43.0


Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] btrfs: skip clearing EXTENT_DEFRAG for nocow ordered extents
  2026-03-30  3:31 [PATCH] btrfs: skip clearing EXTENT_DEFRAG for nocow ordered extents Dave Chen
@ 2026-04-07 11:23 ` Filipe Manana
  0 siblings, 0 replies; 2+ messages in thread
From: Filipe Manana @ 2026-04-07 11:23 UTC (permalink / raw)
  To: Dave Chen; +Cc: linux-btrfs, dsterba, cccheng, robbieko

On Mon, Mar 30, 2026 at 4:32 AM Dave Chen <davechen@synology.com> wrote:
>
> In btrfs_finish_one_ordered(), clear_bits is unconditionally initialized
> with EXTENT_DEFRAG.  For nocow ordered extents this is always a no-op
> because should_nocow() already forces the COW path when EXTENT_DEFRAG is
> set, so a nocow ordered extent can never have EXTENT_DEFRAG on its range.
>
> Although harmless, the unconditional btrfs_clear_extent_bit() call still
> performs a cold rbtree lookup under the io tree spinlock on every nocow
> write completion.  Avoid this by only adding EXTENT_DEFRAG to clear_bits
> for non-nocow ordered extents, and skip the call entirely when there are
> no bits to clear.
>
> Signed-off-by: Dave Chen <davechen@synology.com>
> Signed-off-by: Robbie Ko <robbieko@synology.com>

Reviewed-by: Filipe Manana <fdmanana@suse.com>

Looks good. Pushed it to the github for-next branch, thanks.

> ---
>  fs/btrfs/inode.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index f643a05208720..80daf7e248ab8 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -3203,7 +3203,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
>         bool freespace_inode;
>         bool truncated = false;
>         bool clear_reserved_extent = true;
> -       unsigned int clear_bits = EXTENT_DEFRAG;
> +       unsigned int clear_bits = 0;
>
>         start = ordered_extent->file_offset;
>         end = start + ordered_extent->num_bytes - 1;
> @@ -3214,6 +3214,9 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
>             !test_bit(BTRFS_ORDERED_ENCODED, &ordered_extent->flags))
>                 clear_bits |= EXTENT_DELALLOC_NEW;
>
> +       if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
> +               clear_bits |= EXTENT_DEFRAG;
> +
>         freespace_inode = btrfs_is_free_space_inode(inode);
>         if (!freespace_inode)
>                 btrfs_lockdep_acquire(fs_info, btrfs_ordered_extent);
> @@ -3345,8 +3348,9 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
>                 goto out;
>         }
>  out:
> -       btrfs_clear_extent_bit(&inode->io_tree, start, end, clear_bits,
> -                              &cached_state);
> +       if (clear_bits)
> +               btrfs_clear_extent_bit(&inode->io_tree, start, end, clear_bits,
> +                                      &cached_state);
>
>         if (trans)
>                 btrfs_end_transaction(trans);
> --
> 2.43.0
>
>
> Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-04-07 11:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-30  3:31 [PATCH] btrfs: skip clearing EXTENT_DEFRAG for nocow ordered extents Dave Chen
2026-04-07 11:23 ` Filipe Manana

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox