diff mbox series

[05/17] btrfs: lock extent when doing inline extent in compression

Message ID 99be1657b5273f3a9d24e53f6be9303b381a51eb.1713363472.git.josef@toxicpanda.com (mailing list archive)
State New, archived
Headers show
Series btrfs: restrain lock extent usage during writeback | expand

Commit Message

Josef Bacik April 17, 2024, 2:35 p.m. UTC
We currently don't lock the extent when we're doing a
cow_file_range_inline() for a compressed extent.  This isn't a problem
necessarily, but it's inconsistent with the rest of our usage of
cow_file_range_inline().  This also leads to some extra weird logic
around whether the extent is locked or not.  Fix this to lock the extent
before calling cow_file_range_inline() in compression to make it
consistent with the rest of the inline users.  In future patches this
will be pushed down into the cow_file_range_inline() helper, so we're
fine with the quick and dirty locking here.  This patch exists to make
the behavior change obvious.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/inode.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

Comments

Johannes Thumshirn April 17, 2024, 4:29 p.m. UTC | #1
On 17.04.24 16:36, Josef Bacik wrote:
> We currently don't lock the extent when we're doing a
> cow_file_range_inline() for a compressed extent.  This isn't a problem
> necessarily, but it's inconsistent with the rest of our usage of
> cow_file_range_inline().  This also leads to some extra weird logic
> around whether the extent is locked or not.  Fix this to lock the extent
> before calling cow_file_range_inline() in compression to make it
> consistent with the rest of the inline users.  In future patches this
> will be pushed down into the cow_file_range_inline() helper, so we're
> fine with the quick and dirty locking here.  This patch exists to make
> the behavior change obvious.

But in btrfs_do_encoded_write() we're still not locking the compressed 
extents and call __cow_file_range_inline().

This deviation should at least be documented here.
Josef Bacik April 19, 2024, 6:52 p.m. UTC | #2
On Wed, Apr 17, 2024 at 04:29:52PM +0000, Johannes Thumshirn wrote:
> On 17.04.24 16:36, Josef Bacik wrote:
> > We currently don't lock the extent when we're doing a
> > cow_file_range_inline() for a compressed extent.  This isn't a problem
> > necessarily, but it's inconsistent with the rest of our usage of
> > cow_file_range_inline().  This also leads to some extra weird logic
> > around whether the extent is locked or not.  Fix this to lock the extent
> > before calling cow_file_range_inline() in compression to make it
> > consistent with the rest of the inline users.  In future patches this
> > will be pushed down into the cow_file_range_inline() helper, so we're
> > fine with the quick and dirty locking here.  This patch exists to make
> > the behavior change obvious.
> 
> But in btrfs_do_encoded_write() we're still not locking the compressed 
> extents and call __cow_file_range_inline().

We are though, the extent is locked the entire operation during encoded writes.
Thanks,

Josef
diff mbox series

Patch

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 6fd866a793bb..ba74476ac423 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -742,10 +742,10 @@  static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 offset,
 					  size_t compressed_size,
 					  int compress_type,
 					  struct folio *compressed_folio,
-					  bool update_i_size, bool locked)
+					  bool update_i_size)
 {
 	unsigned long clear_flags = EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
-		EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING;
+		EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING | EXTENT_LOCKED;
 	u64 size = min_t(u64, i_size_read(&inode->vfs_inode), end + 1);
 	int ret;
 
@@ -755,9 +755,6 @@  static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 offset,
 	if (ret > 0)
 		return ret;
 
-	if (locked)
-		clear_flags |= EXTENT_LOCKED;
-
 	extent_clear_unlock_delalloc(inode, offset, end, NULL, clear_flags,
 				     PAGE_UNLOCK | PAGE_START_WRITEBACK |
 				     PAGE_END_WRITEBACK);
@@ -1031,18 +1028,19 @@  static void compress_file_range(struct btrfs_work *work)
 	 * Check cow_file_range() for why we don't even try to create inline
 	 * extent for the subpage case.
 	 */
+	lock_extent(&inode->io_tree, start, end, NULL);
 	if (total_in < actual_end)
 		ret = cow_file_range_inline(inode, start, end, 0,
-					    BTRFS_COMPRESS_NONE, NULL, false,
-					    false);
+					    BTRFS_COMPRESS_NONE, NULL, false);
 	else
 		ret = cow_file_range_inline(inode, start, end, total_compressed,
-					    compress_type, folios[0], false, false);
+					    compress_type, folios[0], false);
 	if (ret <= 0) {
 		if (ret < 0)
 			mapping_set_error(mapping, -EIO);
 		goto free_pages;
 	}
+	unlock_extent(&inode->io_tree, start, end, NULL);
 
 	/*
 	 * We aren't doing an inline extent. Round the compressed size up to a
@@ -1352,8 +1350,7 @@  static noinline int cow_file_range(struct btrfs_inode *inode,
 	if (!no_inline) {
 		/* lets try to make an inline extent */
 		ret = cow_file_range_inline(inode, start, end, 0,
-					    BTRFS_COMPRESS_NONE, NULL, false,
-					    true);
+					    BTRFS_COMPRESS_NONE, NULL, false);
 		if (ret <= 0) {
 			/*
 			 * We succeeded, return 1 so the caller knows we're done