f2fs: use adjusted write range after f2fs_write_checks()
PATCH v2 1/1 f2fs: use adjusted write range after f2fs_write_checks()
generic_write_checks() in f2fs_write_checks() can adjust iocb->ki_pos for append writes and truncate the iterator to limit the number of bytes to write.
In f2fs_file_write_iter(), the pinned-file overwrite check currently uses the position and count saved before f2fs_write_checks(), so it can check a range different from the actual write range.
The forced buffered I/O cleanup also uses orig_pos saved before f2fs_write_checks(). For O_APPEND writes, this can make the cleanup flush and invalidate the wrong page cache range.
Move the pinned-file overwrite check after f2fs_write_checks() and use the adjusted iocb->ki_pos and iov_iter_count(from). Also save the adjusted write position and use it for the forced buffered I/O cleanup.
---
V1 -> V2: Move the pinned-file overwrite check after f2fs_write_checks() and use the adjusted write position and iterator count
fs/f2fs/file.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
fs/f2fs/file.c +9 -8
@@ -5641,9 +5641,8 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)bool dio;bool may_need_sync = true;int preallocated; -const loff_t pos = iocb->ki_pos; -const ssize_t count =iov_iter_count (from);ssize_t ret; +loff_t bufio_start_pos;if (unlikely (f2fs_cp_error (F2FS_I_SB (inode)))) { ret = -EIO ;
@@ -5664,15 +5663,17 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)inode_lock (inode); } + ret =f2fs_write_checks (iocb, from); +if (ret <=0 ) + goto out_unlock; +if (f2fs_is_pinned_file (inode) && - !f2fs_overwrite_io (inode, pos, count)) { + !f2fs_overwrite_io (inode, iocb->ki_pos,iov_iter_count (from))) { ret = -EIO ; goto out_unlock; } - ret =f2fs_write_checks (iocb, from); -if (ret <=0 ) - goto out_unlock; + bufio_start_pos = iocb->ki_pos;/* Determine whether we will do a direct write or a buffered write. */ dio =f2fs_should_use_dio (inode, iocb, from);
@@ -5727,8 +5728,8 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)*/ if (ret >0 && !dio && (iocb->ki_flags &IOCB_DIRECT ))f2fs_flush_buffered_write (iocb->ki_filp->f_mapping, - orig_pos, - orig_pos + ret -1 ); + bufio_start_pos, + bufio_start_pos + ret -1 );return ret; }
base-commit: dafb84f092a6387748b2df4c8f647d46873bc1a0
Signature · 1 line
2.53.0
Quoted 12 lines · Seongjae Jeong
generic_write_checks()inf2fs_write_checks()can adjustiocb->ki_posfor append writes and truncate the iterator to limit the number of bytes to write.In
f2fs_file_write_iter(), the pinned-file overwrite check currently uses the position and count saved beforef2fs_write_checks(), so it can check a range different from the actual write range.The forced buffered I/O cleanup also uses
orig_possaved beforef2fs_write_checks(). ForO_APPENDwrites, this can make the cleanup flush and invalidate the wrong page cache range.
Quoted 7 lines
Move the pinned-file overwrite check after
f2fs_write_checks()and use the adjustediocb->ki_posandiov_iter_count(from). Also save the adjusted write position and use it for the forced buffered I/O cleanup.Fixes 3fdd89b452c2 ("f2fs: prevent writing without fallocate() for pinned files")Fixes 92318f20d703 ("f2fs: preserve direct write semantics when buffering is forced")Signed-off-by Seongjae Jeong <[email protected]>
Thanks,
This patch was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:
Hello:
This patch was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:
Quoted 9 lines · On Mon, 24 Aug 2026 02:32:31 +0000 you wrote:
generic_write_checks()inf2fs_write_checks()can adjustiocb->ki_posfor append writes and truncate the iterator to limit the number of bytes to write.In
f2fs_file_write_iter(), the pinned-file overwrite check currently uses the position and count saved beforef2fs_write_checks(), so it can check a range different from the actual write range.[...]
Here is the summary with links:
- [f2fs-dev,v2] f2fs: use adjusted write range after f2fs_write_checks()
https://git.kernel.org/jaegeuk/f2fs/c/3b2c5d35cf43
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html