diff mbox series

[v2,3/3] btrfs: remove the out-of-loop cleanup from btrfs_buffered_write()

Message ID 05e341dfada9ea89ad578e9d42e5dc495f4556a2.1731396107.git.wqu@suse.com (mailing list archive)
State New
Headers show
Series btrfs: btrfs_buffered_write() cleanups | expand

Commit Message

Qu Wenruo Nov. 12, 2024, 7:23 a.m. UTC
Inside btrfs_buffered_write() we have an out-of-loop cleanup, which is
only affecting the following call sites:

- balance_dirty_pages_ratelimited_flags() failure
- prepare_one_folio() failure
- lock_and_cleanup_extent_if_need() failure
- btrfs_dirty_folio() failure

This patch extracts the out-of-loop cleanup into a helper,
buffered_write_release() so that above call sites can call that helper
before break, and remove the out-of-loop cleanup.

This allows us to:

- Remove @release_bytes completely
  Now every call site can directly use @pos, @write_bytes (or @copied)
  at the cleanup helper.

- Move @only_release_metadata into the loop

Thus making later migration to iomap a little easier.

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

Patch

diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 5fba2e44c630..5bfa040d3856 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -1087,6 +1087,27 @@  int btrfs_write_check(struct kiocb *iocb, size_t count)
 	return 0;
 }
 
+static void buffered_write_release(struct btrfs_inode *binode,
+		struct extent_changeset *data_reserved, u64 pos, u64 len,
+		bool only_release_metadata)
+{
+	const u32 sectorsize = binode->root->fs_info->sectorsize;
+	u64 aligned_start;
+	u64 aligned_len;
+
+	if (!len)
+		return;
+	aligned_start = round_down(pos, sectorsize);
+	aligned_len = round_up(pos + len, sectorsize) - aligned_start;
+
+	if (only_release_metadata) {
+		btrfs_check_nocow_unlock(binode);
+		btrfs_delalloc_release_metadata(binode, aligned_len, true);
+	} else {
+		btrfs_delalloc_release_space(binode, data_reserved, pos, len, true);
+	}
+}
+
 ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
 {
 	struct file *file = iocb->ki_filp;
@@ -1094,7 +1115,6 @@  ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
 	struct inode *inode = file_inode(file);
 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
 	struct extent_changeset *data_reserved = NULL;
-	u64 release_bytes = 0;
 	u64 lockstart;
 	u64 lockend;
 	size_t num_written = 0;
@@ -1103,7 +1123,6 @@  ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
 	unsigned int ilock_flags = 0;
 	const bool nowait = (iocb->ki_flags & IOCB_NOWAIT);
 	unsigned int bdp_flags = (nowait ? BDP_ASYNC : 0);
-	bool only_release_metadata = false;
 	const u32 sectorsize = fs_info->sectorsize;
 
 	if (nowait)
@@ -1131,6 +1150,7 @@  ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
 		struct folio *folio = NULL;
 		int extents_locked;
 		bool force_page_uptodate = false;
+		bool only_release_metadata = false;
 
 		/*
 		 * Fault pages before locking them in prepare_one_folio()
@@ -1145,8 +1165,6 @@  ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
 			break;
 		}
 
-		only_release_metadata = false;
-
 		extent_changeset_release(data_reserved);
 		ret = btrfs_check_data_free_space(BTRFS_I(inode),
 						  &data_reserved, pos,
@@ -1194,11 +1212,12 @@  ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
 			break;
 		}
 
-		release_bytes = reserve_bytes;
 again:
 		ret = balance_dirty_pages_ratelimited_flags(inode->i_mapping, bdp_flags);
 		if (ret) {
 			btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes);
+			buffered_write_release(BTRFS_I(inode), data_reserved,
+					       pos, write_bytes, only_release_metadata);
 			break;
 		}
 
@@ -1207,6 +1226,8 @@  ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
 		if (ret) {
 			btrfs_delalloc_release_extents(BTRFS_I(inode),
 						       reserve_bytes);
+			buffered_write_release(BTRFS_I(inode), data_reserved,
+					       pos, write_bytes, only_release_metadata);
 			break;
 		}
 
@@ -1219,6 +1240,8 @@  ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
 
 			btrfs_delalloc_release_extents(BTRFS_I(inode),
 						       reserve_bytes);
+			buffered_write_release(BTRFS_I(inode), data_reserved,
+					       pos, write_bytes, only_release_metadata);
 			ret = extents_locked;
 			break;
 		}
@@ -1263,8 +1286,6 @@  ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
 			}
 		}
 
-		release_bytes = dirty_bytes;
-
 		ret = btrfs_dirty_folio(BTRFS_I(inode), folio, pos, copied,
 					&cached_state, only_release_metadata);
 
@@ -1284,10 +1305,11 @@  ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
 		btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes);
 		if (ret) {
 			btrfs_drop_folio(fs_info, folio, pos, copied);
+			buffered_write_release(BTRFS_I(inode), data_reserved,
+					       pos, copied, only_release_metadata);
 			break;
 		}
 
-		release_bytes = 0;
 		if (only_release_metadata)
 			btrfs_check_nocow_unlock(BTRFS_I(inode));
 
@@ -1299,19 +1321,6 @@  ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
 		num_written += copied;
 	}
 
-	if (release_bytes) {
-		if (only_release_metadata) {
-			btrfs_check_nocow_unlock(BTRFS_I(inode));
-			btrfs_delalloc_release_metadata(BTRFS_I(inode),
-					release_bytes, true);
-		} else {
-			btrfs_delalloc_release_space(BTRFS_I(inode),
-					data_reserved,
-					round_down(pos, fs_info->sectorsize),
-					release_bytes, true);
-		}
-	}
-
 	extent_changeset_free(data_reserved);
 	if (num_written > 0) {
 		pagecache_isize_extended(inode, old_isize, iocb->ki_pos);