@@ -1104,6 +1104,7 @@ ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
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)
ilock_flags |= BTRFS_ILOCK_TRY;
@@ -1123,13 +1124,10 @@ ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
pos = iocb->ki_pos;
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), PAGE_SIZE - offset);
+ size_t write_bytes = min(iov_iter_count(i), PAGE_SIZE - offset_in_page(pos));
size_t reserve_bytes;
+ size_t dirty_bytes;
size_t copied;
- size_t dirty_sectors;
- size_t num_sectors;
struct folio *folio = NULL;
int extents_locked;
bool force_page_uptodate = false;
@@ -1148,7 +1146,6 @@ ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
}
only_release_metadata = false;
- sector_offset = pos & (fs_info->sectorsize - 1);
extent_changeset_release(data_reserved);
ret = btrfs_check_data_free_space(BTRFS_I(inode),
@@ -1178,8 +1175,8 @@ ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *i)
only_release_metadata = true;
}
- reserve_bytes = round_up(write_bytes + sector_offset,
- fs_info->sectorsize);
+ reserve_bytes = round_up(pos + write_bytes, sectorsize) -
+ round_down(pos, sectorsize);
WARN_ON(reserve_bytes == 0);
ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
reserve_bytes,
@@ -1244,35 +1241,29 @@ ssize_t btrfs_buffered_write(struct kiocb *iocb, struct iov_iter *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 (copied == 0) {
+ if (copied == 0)
force_page_uptodate = true;
- dirty_sectors = 0;
- } else {
+ else
force_page_uptodate = false;
- }
- if (num_sectors > dirty_sectors) {
+ dirty_bytes = round_up(pos + copied, sectorsize) -
+ round_down(pos, sectorsize);
+
+ if (reserve_bytes > dirty_bytes) {
/* release everything except the sectors we dirtied */
- release_bytes -= dirty_sectors << fs_info->sectorsize_bits;
if (only_release_metadata) {
btrfs_delalloc_release_metadata(BTRFS_I(inode),
- release_bytes, true);
+ reserve_bytes - dirty_bytes, true);
} else {
u64 release_start = round_up(pos + copied,
fs_info->sectorsize);
btrfs_delalloc_release_space(BTRFS_I(inode),
data_reserved, release_start,
- release_bytes, true);
+ reserve_bytes - dirty_bytes, true);
}
}
- release_bytes = round_up(copied + sector_offset,
- fs_info->sectorsize);
+ release_bytes = dirty_bytes;
ret = btrfs_dirty_folio(BTRFS_I(inode), folio, pos, copied,
&cached_state, only_release_metadata);
We have too many variables with similar naming, which makes code much harder to review/refactor. Some examples are: - @offset and @sector_offset The former is the offset_in_page(), the latter is offset inside the sector. - @dirty_sectors and @num_sectors The former is more or less common, the number of sectors for the copied range. Meanwhile the latter is the number of sectors for reserved bytes. And we are mixing bytes and sectors: - @dirty_sectors and @copied The former is the number of sectors for the block aligned range, meanwhile @copied is the unaligned length returned by copy_folio_from_iter_atomic() Fix all those shenanigans by: - Remove all number-of-sectors variables All calculation will now be based on bytes. The number-of-sectors calculation will be only done when necessary, and in fact there will be no call site requiring such result. - Remove offset-in-page/offset-in-sector variables The offset-in-page result is only utilized once in calculating @write_bytes. So it can be open-coded. The offset-in-sector is utilized to calculate the number-of-sectors, which will be done in a more unified way. - Calculate sector aligned length in-place using round_down() and round_up() Now for every sector aligned length, we go something like: reserved_bytes = round_up(pos + write_bytes, sectorsize) - round_down(pos, sectorsize); So no need for any offset-in-sector helper variable. This removes the following variables: - offset - sector_offset All call sites are open-coded or replaced completely. - dirty_sectors Replace by @dirty_bytes. - num_sectors We use @dirty_bytes and @reserve_bytes to calculate which range needs to be release. Signed-off-by: Qu Wenruo <wqu@suse.com> --- fs/btrfs/file.c | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-)