diff mbox series

[16/16] btrfs: btree_writepages lock extents before pages

Message ID 994c7a74720e3c8589263095704dc7f87cfdb3e7.1668530684.git.rgoldwyn@suse.com (mailing list archive)
State New, archived
Headers show
Series Lock extents before pages | expand

Commit Message

Goldwyn Rodrigues Nov. 15, 2022, 6 p.m. UTC
Lock extents before pages while performing btree_writepages().

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
---
 fs/btrfs/disk-io.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

Comments

Josef Bacik Dec. 13, 2022, 7:20 p.m. UTC | #1
On Tue, Nov 15, 2022 at 12:00:34PM -0600, Goldwyn Rodrigues wrote:
> Lock extents before pages while performing btree_writepages().
> 
> Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
> ---
>  fs/btrfs/disk-io.c | 24 +++++++++++++++++++++++-
>  1 file changed, 23 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
> index 8ac9612f8f27..b7e7c4c9d404 100644
> --- a/fs/btrfs/disk-io.c
> +++ b/fs/btrfs/disk-io.c
> @@ -858,8 +858,25 @@ static int btree_migrate_folio(struct address_space *mapping,
>  static int btree_writepages(struct address_space *mapping,
>  			    struct writeback_control *wbc)
>  {
> +	u64 start, end;
> +	struct btrfs_inode *inode = BTRFS_I(mapping->host);
> +        struct extent_state *cached = NULL;
>  	struct btrfs_fs_info *fs_info;
>  	int ret;
> +	u64 isize = round_up(i_size_read(&inode->vfs_inode), PAGE_SIZE) - 1;
> +
> +	if (wbc->range_cyclic) {
> +		start = mapping->writeback_index << PAGE_SHIFT;
> +		end = isize;
> +	} else {
> +		start = round_down(wbc->range_start, PAGE_SIZE);
> +		end = round_up(wbc->range_end, PAGE_SIZE) - 1;
> +		end = min(isize, end);
> +	}

Same comment here as the extent_writepages case, we need to handle the
possibility of ->writeback_index changing between now and
btree_write_cache_pages.  Thanks,

Josef
diff mbox series

Patch

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 8ac9612f8f27..b7e7c4c9d404 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -858,8 +858,25 @@  static int btree_migrate_folio(struct address_space *mapping,
 static int btree_writepages(struct address_space *mapping,
 			    struct writeback_control *wbc)
 {
+	u64 start, end;
+	struct btrfs_inode *inode = BTRFS_I(mapping->host);
+        struct extent_state *cached = NULL;
 	struct btrfs_fs_info *fs_info;
 	int ret;
+	u64 isize = round_up(i_size_read(&inode->vfs_inode), PAGE_SIZE) - 1;
+
+	if (wbc->range_cyclic) {
+		start = mapping->writeback_index << PAGE_SHIFT;
+		end = isize;
+	} else {
+		start = round_down(wbc->range_start, PAGE_SIZE);
+		end = round_up(wbc->range_end, PAGE_SIZE) - 1;
+		end = min(isize, end);
+	}
+
+	if (start >= end)
+		return 0;
+
 
 	if (wbc->sync_mode == WB_SYNC_NONE) {
 
@@ -874,7 +891,12 @@  static int btree_writepages(struct address_space *mapping,
 		if (ret < 0)
 			return 0;
 	}
-	return btree_write_cache_pages(mapping, wbc);
+
+	lock_extent(&inode->io_tree, start, end, &cached);
+	ret = btree_write_cache_pages(mapping, wbc);
+	unlock_extent(&inode->io_tree, start, end, &cached);
+
+	return ret;
 }
 
 static bool btree_release_folio(struct folio *folio, gfp_t gfp_flags)