diff mbox series

btrfs: page to folio conversion in btrfs_truncate_block()

Message ID cn7d3gijpqxtmlytcv4ztac3eb7ukd54co4csitaw6czn6bfxr@3wopycxp755q (mailing list archive)
State New, archived
Headers show
Series btrfs: page to folio conversion in btrfs_truncate_block() | expand

Commit Message

Goldwyn Rodrigues Jan. 11, 2024, 1:56 a.m. UTC
Convert use of struct page to struct folio inside btrfs_truncate_block().
The only page based function is set_page_extent_mapped(). All other
functions have folio equivalents.

Had to use __filemap_get_folio() because filemap_grab_folio() does not
allow passing allocation mask as a parameter.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
---
 fs/btrfs/inode.c | 42 ++++++++++++++++++++----------------------
 1 file changed, 20 insertions(+), 22 deletions(-)

Comments

David Sterba Jan. 11, 2024, 6:40 p.m. UTC | #1
On Wed, Jan 10, 2024 at 07:56:13PM -0600, Goldwyn Rodrigues wrote:
> Convert use of struct page to struct folio inside btrfs_truncate_block().
> The only page based function is set_page_extent_mapped(). All other
> functions have folio equivalents.
> 
> Had to use __filemap_get_folio() because filemap_grab_folio() does not
> allow passing allocation mask as a parameter.
> 
> Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>

Reviewed-by: David Sterba <dsterba@suse.com>

There are some overly long lines, I can fix that unless you'd like to
commit the patch yourself.

> ---
>  fs/btrfs/inode.c | 42 ++++++++++++++++++++----------------------
>  1 file changed, 20 insertions(+), 22 deletions(-)
> 
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index e285ddbcdee0..12c040328742 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -4680,7 +4680,7 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
>  	u32 blocksize = fs_info->sectorsize;
>  	pgoff_t index = from >> PAGE_SHIFT;
>  	unsigned offset = from & (blocksize - 1);
> -	struct page *page;
> +	struct folio *folio;
>  	gfp_t mask = btrfs_alloc_write_mask(mapping);
>  	size_t write_bytes = blocksize;
>  	int ret = 0;
> @@ -4712,8 +4712,8 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
>  		goto out;
>  	}
>  again:
> -	page = find_or_create_page(mapping, index, mask);
> -	if (!page) {
> +	folio = __filemap_get_folio(mapping, index, FGP_LOCK | FGP_ACCESSED | FGP_CREAT, mask);

This line is too long

> +	if (!folio) {
>  		btrfs_delalloc_release_space(inode, data_reserved, block_start,
>  					     blocksize, true);
>  		btrfs_delalloc_release_extents(inode, blocksize);
> @@ -4721,15 +4721,15 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
>  		goto out;
>  	}
>  
> -	if (!PageUptodate(page)) {
> -		ret = btrfs_read_folio(NULL, page_folio(page));
> -		lock_page(page);
> -		if (page->mapping != mapping) {
> -			unlock_page(page);
> -			put_page(page);
> +	if (!folio_test_uptodate(folio)) {
> +		ret = btrfs_read_folio(NULL, folio);
> +		folio_lock(folio);
> +		if (folio->mapping != mapping) {
> +			folio_unlock(folio);
> +			folio_put(folio);
>  			goto again;
>  		}
> -		if (!PageUptodate(page)) {
> +		if (!folio_test_uptodate(folio)) {
>  			ret = -EIO;
>  			goto out_unlock;
>  		}
> @@ -4741,19 +4741,19 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
>  	 * folio private, but left the page in the mapping.  Set the page mapped
>  	 * here to make sure it's properly set for the subpage stuff.
>  	 */
> -	ret = set_page_extent_mapped(page);
> +	ret = set_page_extent_mapped(&folio->page);
>  	if (ret < 0)
>  		goto out_unlock;
>  
> -	wait_on_page_writeback(page);
> +	folio_wait_writeback(folio);
>  
>  	lock_extent(io_tree, block_start, block_end, &cached_state);
>  
>  	ordered = btrfs_lookup_ordered_extent(inode, block_start);
>  	if (ordered) {
>  		unlock_extent(io_tree, block_start, block_end, &cached_state);
> -		unlock_page(page);
> -		put_page(page);
> +		folio_unlock(folio);
> +		folio_put(folio);
>  		btrfs_start_ordered_extent(ordered);
>  		btrfs_put_ordered_extent(ordered);
>  		goto again;
> @@ -4774,15 +4774,13 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
>  		if (!len)
>  			len = blocksize - offset;
>  		if (front)
> -			memzero_page(page, (block_start - page_offset(page)),
> -				     offset);
> +			folio_zero_range(folio, block_start - folio_pos(folio), offset);

Here

>  		else
> -			memzero_page(page, (block_start - page_offset(page)) + offset,
> -				     len);
> +			folio_zero_range(folio, (block_start - folio_pos(folio)) + offset, len);

And here

>  	}
> -	btrfs_folio_clear_checked(fs_info, page_folio(page), block_start,
> +	btrfs_folio_clear_checked(fs_info, folio, block_start,
>  				  block_end + 1 - block_start);
> -	btrfs_folio_set_dirty(fs_info, page_folio(page), block_start,
> +	btrfs_folio_set_dirty(fs_info, folio, block_start,
>  			      block_end + 1 - block_start);
>  	unlock_extent(io_tree, block_start, block_end, &cached_state);
>  
> @@ -4799,8 +4797,8 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
>  					block_start, blocksize, true);
>  	}
>  	btrfs_delalloc_release_extents(inode, blocksize);
> -	unlock_page(page);
> -	put_page(page);
> +	folio_unlock(folio);
> +	folio_put(folio);
>  out:
>  	if (only_release_metadata)
>  		btrfs_check_nocow_unlock(inode);
> -- 
> 2.43.0
> 
> 
> -- 
> Goldwyn
Goldwyn Rodrigues Jan. 12, 2024, 12:05 p.m. UTC | #2
On 19:40 11/01, David Sterba wrote:
> On Wed, Jan 10, 2024 at 07:56:13PM -0600, Goldwyn Rodrigues wrote:
> > Convert use of struct page to struct folio inside btrfs_truncate_block().
> > The only page based function is set_page_extent_mapped(). All other
> > functions have folio equivalents.
> > 
> > Had to use __filemap_get_folio() because filemap_grab_folio() does not
> > allow passing allocation mask as a parameter.
> > 
> > Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
> 
> Reviewed-by: David Sterba <dsterba@suse.com>
> 
> There are some overly long lines, I can fix that unless you'd like to
> commit the patch yourself.

Yes, you can fix them. I will wait for Josef's docs to start committing
myself.
David Sterba Jan. 12, 2024, 3:26 p.m. UTC | #3
On Fri, Jan 12, 2024 at 06:05:42AM -0600, Goldwyn Rodrigues wrote:
> On 19:40 11/01, David Sterba wrote:
> > On Wed, Jan 10, 2024 at 07:56:13PM -0600, Goldwyn Rodrigues wrote:
> > > Convert use of struct page to struct folio inside btrfs_truncate_block().
> > > The only page based function is set_page_extent_mapped(). All other
> > > functions have folio equivalents.
> > > 
> > > Had to use __filemap_get_folio() because filemap_grab_folio() does not
> > > allow passing allocation mask as a parameter.
> > > 
> > > Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
> > 
> > Reviewed-by: David Sterba <dsterba@suse.com>
> > 
> > There are some overly long lines, I can fix that unless you'd like to
> > commit the patch yourself.
> 
> Yes, you can fix them. I will wait for Josef's docs to start committing
> myself.

Ok, added to for-next.
diff mbox series

Patch

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index e285ddbcdee0..12c040328742 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -4680,7 +4680,7 @@  int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
 	u32 blocksize = fs_info->sectorsize;
 	pgoff_t index = from >> PAGE_SHIFT;
 	unsigned offset = from & (blocksize - 1);
-	struct page *page;
+	struct folio *folio;
 	gfp_t mask = btrfs_alloc_write_mask(mapping);
 	size_t write_bytes = blocksize;
 	int ret = 0;
@@ -4712,8 +4712,8 @@  int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
 		goto out;
 	}
 again:
-	page = find_or_create_page(mapping, index, mask);
-	if (!page) {
+	folio = __filemap_get_folio(mapping, index, FGP_LOCK | FGP_ACCESSED | FGP_CREAT, mask);
+	if (!folio) {
 		btrfs_delalloc_release_space(inode, data_reserved, block_start,
 					     blocksize, true);
 		btrfs_delalloc_release_extents(inode, blocksize);
@@ -4721,15 +4721,15 @@  int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
 		goto out;
 	}
 
-	if (!PageUptodate(page)) {
-		ret = btrfs_read_folio(NULL, page_folio(page));
-		lock_page(page);
-		if (page->mapping != mapping) {
-			unlock_page(page);
-			put_page(page);
+	if (!folio_test_uptodate(folio)) {
+		ret = btrfs_read_folio(NULL, folio);
+		folio_lock(folio);
+		if (folio->mapping != mapping) {
+			folio_unlock(folio);
+			folio_put(folio);
 			goto again;
 		}
-		if (!PageUptodate(page)) {
+		if (!folio_test_uptodate(folio)) {
 			ret = -EIO;
 			goto out_unlock;
 		}
@@ -4741,19 +4741,19 @@  int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
 	 * folio private, but left the page in the mapping.  Set the page mapped
 	 * here to make sure it's properly set for the subpage stuff.
 	 */
-	ret = set_page_extent_mapped(page);
+	ret = set_page_extent_mapped(&folio->page);
 	if (ret < 0)
 		goto out_unlock;
 
-	wait_on_page_writeback(page);
+	folio_wait_writeback(folio);
 
 	lock_extent(io_tree, block_start, block_end, &cached_state);
 
 	ordered = btrfs_lookup_ordered_extent(inode, block_start);
 	if (ordered) {
 		unlock_extent(io_tree, block_start, block_end, &cached_state);
-		unlock_page(page);
-		put_page(page);
+		folio_unlock(folio);
+		folio_put(folio);
 		btrfs_start_ordered_extent(ordered);
 		btrfs_put_ordered_extent(ordered);
 		goto again;
@@ -4774,15 +4774,13 @@  int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
 		if (!len)
 			len = blocksize - offset;
 		if (front)
-			memzero_page(page, (block_start - page_offset(page)),
-				     offset);
+			folio_zero_range(folio, block_start - folio_pos(folio), offset);
 		else
-			memzero_page(page, (block_start - page_offset(page)) + offset,
-				     len);
+			folio_zero_range(folio, (block_start - folio_pos(folio)) + offset, len);
 	}
-	btrfs_folio_clear_checked(fs_info, page_folio(page), block_start,
+	btrfs_folio_clear_checked(fs_info, folio, block_start,
 				  block_end + 1 - block_start);
-	btrfs_folio_set_dirty(fs_info, page_folio(page), block_start,
+	btrfs_folio_set_dirty(fs_info, folio, block_start,
 			      block_end + 1 - block_start);
 	unlock_extent(io_tree, block_start, block_end, &cached_state);
 
@@ -4799,8 +4797,8 @@  int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
 					block_start, blocksize, true);
 	}
 	btrfs_delalloc_release_extents(inode, blocksize);
-	unlock_page(page);
-	put_page(page);
+	folio_unlock(folio);
+	folio_put(folio);
 out:
 	if (only_release_metadata)
 		btrfs_check_nocow_unlock(inode);