diff mbox series

[v4,54/68] btrfs: file: calculate reserve space based on PAGE_SIZE for buffered write

Message ID 20201021062554.68132-55-wqu@suse.com
State New, archived
Headers show
Series btrfs: add basic rw support for subpage sector size | expand

Commit Message

Qu Wenruo Oct. 21, 2020, 6:25 a.m. UTC
In theory btrfs_buffered_write() should reserve space using sector size.
But for now let's base all reserve on PAGE_SIZE, this would make later
subpage support to always submit full page write.

This would cause more data space usage, but greatly simplify the subpage
data write support.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/file.c | 38 +++++++++++---------------------------
 1 file changed, 11 insertions(+), 27 deletions(-)
diff mbox series

Patch

diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index a2009127ef96..564784a5c0c0 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -1650,7 +1650,6 @@  static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
 	while (iov_iter_count(i) > 0) {
 		struct extent_state *cached_state = NULL;
 		size_t offset = offset_in_page(pos);
-		size_t sector_offset;
 		size_t write_bytes = min(iov_iter_count(i),
 					 nrptrs * (size_t)PAGE_SIZE -
 					 offset);
@@ -1659,7 +1658,6 @@  static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
 		size_t reserve_bytes;
 		size_t dirty_pages;
 		size_t copied;
-		size_t dirty_sectors;
 		size_t num_sectors;
 		int extents_locked;
 
@@ -1675,9 +1673,7 @@  static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
 		}
 
 		only_release_metadata = false;
-		sector_offset = pos & (fs_info->sectorsize - 1);
-		reserve_bytes = round_up(write_bytes + sector_offset,
-				fs_info->sectorsize);
+		reserve_bytes = round_up(write_bytes + offset, PAGE_SIZE);
 
 		extent_changeset_release(data_reserved);
 		ret = btrfs_check_data_free_space(BTRFS_I(inode),
@@ -1697,9 +1693,8 @@  static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
 				 */
 				num_pages = DIV_ROUND_UP(write_bytes + offset,
 							 PAGE_SIZE);
-				reserve_bytes = round_up(write_bytes +
-							 sector_offset,
-							 fs_info->sectorsize);
+				reserve_bytes = round_up(write_bytes + offset,
+							 PAGE_SIZE);
 			} else {
 				break;
 			}
@@ -1750,9 +1745,6 @@  static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
 		copied = btrfs_copy_from_user(pos, write_bytes, pages, i);
 
 		num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes);
-		dirty_sectors = round_up(copied + sector_offset,
-					fs_info->sectorsize);
-		dirty_sectors = BTRFS_BYTES_TO_BLKS(fs_info, dirty_sectors);
 
 		/*
 		 * if we have trouble faulting in the pages, fall
@@ -1763,35 +1755,29 @@  static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
 
 		if (copied == 0) {
 			force_page_uptodate = true;
-			dirty_sectors = 0;
 			dirty_pages = 0;
 		} else {
 			force_page_uptodate = false;
-			dirty_pages = DIV_ROUND_UP(copied + offset,
-						   PAGE_SIZE);
+			dirty_pages = DIV_ROUND_UP(copied + offset, PAGE_SIZE);
 		}
 
-		if (num_sectors > dirty_sectors) {
+		if (num_pages > dirty_pages) {
 			/* release everything except the sectors we dirtied */
-			release_bytes -= dirty_sectors <<
-						fs_info->sb->s_blocksize_bits;
+			release_bytes -= dirty_pages << PAGE_SHIFT;
 			if (only_release_metadata) {
 				btrfs_delalloc_release_metadata(BTRFS_I(inode),
 							release_bytes, true);
 			} else {
 				u64 __pos;
 
-				__pos = round_down(pos,
-						   fs_info->sectorsize) +
+				__pos = round_down(pos, PAGE_SIZE) +
 					(dirty_pages << PAGE_SHIFT);
 				btrfs_delalloc_release_space(BTRFS_I(inode),
 						data_reserved, __pos,
 						release_bytes, true);
 			}
 		}
-
-		release_bytes = round_up(copied + sector_offset,
-					fs_info->sectorsize);
+		release_bytes = round_up(copied + offset, PAGE_SIZE);
 
 		if (copied > 0)
 			ret = btrfs_dirty_pages(BTRFS_I(inode), pages,
@@ -1822,10 +1808,8 @@  static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
 			btrfs_check_nocow_unlock(BTRFS_I(inode));
 
 		if (only_release_metadata && copied > 0) {
-			lockstart = round_down(pos,
-					       fs_info->sectorsize);
-			lockend = round_up(pos + copied,
-					   fs_info->sectorsize) - 1;
+			lockstart = round_down(pos, PAGE_SIZE);
+			lockend = round_up(pos + copied, PAGE_SIZE) - 1;
 
 			set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
 				       lockend, EXTENT_NORESERVE, NULL,
@@ -1852,7 +1836,7 @@  static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
 		} else {
 			btrfs_delalloc_release_space(BTRFS_I(inode),
 					data_reserved,
-					round_down(pos, fs_info->sectorsize),
+					round_down(pos, PAGE_SIZE),
 					release_bytes, true);
 		}
 	}