From: Jinliang Zheng <alexjlzheng@xxxxxxxxxxx> With iomap_folio_state, we can identify uptodate states at the block level, and a read_folio reading can correctly handle partially uptodate folios. Therefore, when a partial write occurs, accept the block-aligned partial write instead of rejecting the entire write. For example, suppose a folio is 2MB, blocksize is 4kB, and the copied bytes are 2MB-3kB. Without this patchset, we'd need to recopy from the beginning of the folio in the next iteration, which means 2MB-3kB of bytes is copy duplicately. |<-------------------- 2MB -------------------->| +-------+-------+-------+-------+-------+-------+ | block | ... | block | block | ... | block | folio +-------+-------+-------+-------+-------+-------+ |<-4kB->| |<--------------- copied 2MB-3kB --------->| first time copied |<-------- 1MB -------->| next time we need copy (chunk /= 2) |<-------- 1MB -------->| next next time we need copy. |<------ 2MB-3kB bytes duplicate copy ---->| With this patchset, we can accept 2MB-4kB of bytes, which is block-aligned. This means we only need to process the remaining 4kB in the next iteration, which means there's only 1kB we need to copy duplicately. |<-------------------- 2MB -------------------->| +-------+-------+-------+-------+-------+-------+ | block | ... | block | block | ... | block | folio +-------+-------+-------+-------+-------+-------+ |<-4kB->| |<--------------- copied 2MB-3kB --------->| first time copied |<-4kB->| next time we need copy |<>| only 1kB bytes duplicate copy Although partial writes are inherently a relatively unusual situation and do not account for a large proportion of performance testing, the optimization here still makes sense in large-scale data centers. Signed-off-by: Jinliang Zheng <alexjlzheng@xxxxxxxxxxx> --- fs/iomap/buffered-io.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index 7b9193f8243a..743e369b64d4 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -873,6 +873,25 @@ static int iomap_write_begin(struct iomap_iter *iter, return status; } +static int iomap_trim_tail_partial(struct inode *inode, loff_t pos, + size_t copied, struct folio *folio) +{ + struct iomap_folio_state *ifs = folio->private; + unsigned block_size, last_blk, last_blk_bytes; + + if (!ifs || !copied) + return 0; + + block_size = 1 << inode->i_blkbits; + last_blk = offset_in_folio(folio, pos + copied - 1) >> inode->i_blkbits; + last_blk_bytes = (pos + copied) & (block_size - 1); + + if (!ifs_block_is_uptodate(ifs, last_blk)) + copied -= min(copied, last_blk_bytes); + + return copied; +} + static int __iomap_write_end(struct inode *inode, loff_t pos, size_t len, size_t copied, struct folio *folio) { @@ -886,12 +905,15 @@ static int __iomap_write_end(struct inode *inode, loff_t pos, size_t len, * read_folio might come in and destroy our partial write. * * Do the simplest thing and just treat any short write to a - * non-uptodate page as a zero-length write, and force the caller to - * redo the whole thing. + * non-uptodate block as a zero-length write, and force the caller to + * redo the things begin from the block. */ - if (unlikely(copied < len && !folio_test_uptodate(folio))) - return 0; - iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), len); + if (unlikely(copied < len && !folio_test_uptodate(folio))) { + copied = iomap_trim_tail_partial(inode, pos, copied, folio); + if (!copied) + return 0; + } + iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), copied); iomap_set_range_dirty(folio, offset_in_folio(folio, pos), copied); filemap_dirty_folio(inode->i_mapping, folio); return copied; -- 2.49.0