From: Daeho Jeong f2fs_skip_inode_update() lets fsync() skip writing the inode when FI_AUTO_RECOVER is set and i_size is block aligned, relying on roll-forward recovery to recover i_size from the position of the fsynced data block. However, do_recover_data() extends i_size only by a block that starts at or beyond the recovered i_size, so it cannot recover a size that grew within the block holding the old size. For example, on a 4KB block filesystem: write(fd, buf, 14KB); fsync(fd); write(fd, buf, 2KB); fsync(fd); - the first fsync() writes the inode with i_size 14KB. - the second write() makes i_size 16KB in the same block. If it is in the same timestamp tick as the first one, the timestamps do not change and the inode is still clean, so FI_AUTO_RECOVER is set. - the block is already allocated, so the second fsync() updates it in place and writes no node. f2fs_skip_inode_update() skips the inode as well, since i_size is block aligned and equals last_disk_size. - recovery finds i_size 14KB in the inode from the first fsync() and has no block to extend it with, so the last 2KB is lost. Repeating a 2KB append and fsync() up to 16KB and then shutting down the filesystem left 117 of 1200 files with 14KB on a 4KB page kernel, and none with this patch. Set FI_AUTO_RECOVER only if the old i_size is block aligned, and clear it otherwise, so that fsync() writes the inode for such a size change. Fixes: 3a3a5ead7b6d ("f2fs: do not recover i_size if it's valid") Cc: stable@vger.kernel.org Signed-off-by: Daeho Jeong --- fs/f2fs/f2fs.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index 50a5f845176d..b328e678d295 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -3676,8 +3676,9 @@ static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size) { bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE); bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER); + loff_t old_size = i_size_read(inode); - if (i_size_read(inode) == i_size) + if (old_size == i_size) return; i_size_write(inode, i_size); @@ -3686,8 +3687,16 @@ static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size) return; f2fs_mark_inode_dirty_sync(inode, true); - if (clean || recover) + + /* + * Roll-forward recovery extends i_size only by a block that starts at + * or beyond the recovered i_size, so it cannot recover a size that grew + * within the block holding the old size. + */ + if ((clean || recover) && !(old_size & ~PAGE_MASK)) set_inode_flag(inode, FI_AUTO_RECOVER); + else + clear_inode_flag(inode, FI_AUTO_RECOVER); } static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth) base-commit: b9d7379d10d43386b59082da8c9a0b111e054781 -- 2.56.0.611.g1030f0b940-goog