Skip to contentSkip to conversation

f2fs: don't skip the inode update for a size grown within a block

PATCH In review Daeho Jeong 2 participants 2 messages Nov 16

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs: don't skip the inode update for a size grown within a block

From: Daeho Jeong <[email protected]>

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);
  <sudden power-off>

- 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")
---
 fs/f2fs/f2fs.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
fs/f2fs/f2fs.h +11 -2
index 50a5f845176d..b328e678d295 100644
@@ -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

Signature · 1 line
2.56.0.611.g1030f0b940-goog
#1 lore raw
Quoted 26 lines · Daeho Jeong

From: Daeho Jeong <[email protected]>

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);
  <sudden power-off>

- 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.

Quoted 6 lines

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.

Please ignore this patch.

I missed that Chao already posted a fix for the same problem:
[PATCH 1/7] f2fs: fix to persist i_size extended inside last block for fsync
https://lore.kernel.org/linux-f2fs-devel/[email protected]/

Sorry for the noise.

Thanks,
Daeho

Quoted 46 lines
Fixes 3a3a5ead7b6d ("f2fs: do not recover i_size if it's valid")
---
 fs/f2fs/f2fs.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
fs/f2fs/f2fs.h +11 -2
index 50a5f845176d..b328e678d295 100644
@@ -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