f2fs: fix return value of f2fs_recover_fsync_data()
Chao Yu
maintainerPATCH 1/2 f2fs: fix return value of f2fs_recover_fsync_data()
With below scripts, it will trigger panic in f2fs:
mkfs.f2fs -f /dev/vdd
mount /dev/vdd /mnt/f2fs
touch /mnt/f2fs/foo
sync
echo 111 >> /mnt/f2fs/foof2fs_io fsync /mnt/f2fs/foof2fs_io shutdown 2 /mnt/f2fs
umount /mnt/f2fs
mount -o ro,norecovery /dev/vdd /mnt/f2fs or
mount -o ro,disable_roll_forward /dev/vdd /mnt/f2fs
F2FS-fs (vdd): f2fs_recover_fsync_data: recovery fsync data, check_only: 0 F2FS-fs (vdd): Mounted with checkpoint version = 7f5c361f F2FS-fs (vdd): Stopped filesystem due to reason: 0
F2FS-fs (vdd): f2fs_recover_fsync_data: recovery fsync data, check_only: 1 Filesystem f2fs get_tree() didn't set fc->root, returned 1
------------[ cut here ]------------ kernel BUG at fs/super.c:1761! Oops: invalid opcode: 0000 [#1] SMP PTI CPU: 3 UID: 0 PID: 722 Comm: mount Not tainted 6.18.0-rc2+ #721 PREEMPT(voluntary) Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 RIP: 0010:vfs_get_tree.cold+0x18/0x1a Call Trace: <TASK> fc_mount+0x13/0xa0 path_mount+0x34e/0xc50 __x64_sys_mount+0x121/0x150 do_syscall_64+0x84/0x800 entry_SYSCALL_64_after_hwframe+0x76/0x7e RIP: 0033:0x7fa6cc126cfe
The root cause is we missed to handle error number returned from f2fs_recover_fsync_data() when mounting image w/ ro,norecovery or ro,disable_roll_forward mount option, result in returning a positive error number to vfs_get_tree(), fix it.
--- fs/f2fs/super.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
fs/f2fs/super.c +9 -5
@@ -5211,11 +5211,15 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) } }else { err =f2fs_recover_fsync_data (sbi, true); - -if (!f2fs_readonly (sb) && err >0 ) { - err = -EINVAL ; -f2fs_err (sbi,"Need to recover fsync data" ); - goto free_meta; +if (err >0 ) { +if (!f2fs_readonly (sb)) { +f2fs_err (sbi,"Need to recover fsync data" ); + err = -EINVAL ; + goto free_meta; + }else { +f2fs_info (sbi,"drop all fsynced data" ); + err =0 ; + } } }
Signature · 1 line
2.49.0
Chao Yu
maintainerPATCH 2/2 f2fs: fix to detect recoverable inode during dryrun of find_fsync_dnodes()
mkfs.f2fs -f /dev/vdd
mount /dev/vdd /mnt/f2fs
touch /mnt/f2fs/foo
sync # avoid CP_UMOUNT_FLAG in last f2fs_checkpoint.ckpt_flags
touch /mnt/f2fs/barf2fs_io fsync /mnt/f2fs/barf2fs_io shutdown 2 /mnt/f2fs
umount /mnt/f2fs
blockdev --setro /dev/vdd
mount /dev/vdd /mnt/f2fs
mount: /mnt/f2fs: WARNING: source write-protected, mounted read-only.
For the case if we create and fsync a new inode before sudden power-cut, without norecovery or disable_roll_forward mount option, the following mount will succeed w/o recovering last fsynced inode.
The problem here is that we only check inode_list list after find_fsync_dnodes() in f2fs_recover_fsync_data() to find out whether there is recoverable data in the iamge, but there is a missed case, if last fsynced inode is not existing in last checkpoint, then, we will fail to get its inode due to nat of inode node is not existing in last checkpoint, so the inode won't be linked in inode_list.
Let's detect such case in dyrun mode to fix this issue.
After this change, mount will fail as expected below:
mount: /mnt/f2fs: cannot mount /dev/vdd read-only.
dmesg(1) may have more information after failed mount system call.
demsg:
F2FS-fs (vdd): Need to recover fsync data, but write access unavailable, please try mount w/ disable_roll_forward or norecovery
--- fs/f2fs/recovery.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
fs/f2fs/recovery.c +12 -8
@@ -399,7 +399,7 @@ static int sanity_check_node_chain(struct f2fs_sb_info *sbi, block_t blkaddr, }static int find_fsync_dnodes (struct f2fs_sb_info *sbi,struct list_head *head, -bool check_only) +bool check_only,bool *new_inode) {struct curseg_info *curseg;block_t blkaddr, blkaddr_fast;
@@ -447,16 +447,19 @@ static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head, quota_inode = true; } -/* -* CP | dnode(F) | inode(DF) -* For this case, we should not give up now. -*/ entry =add_fsync_inode (sbi, head,ino_of_node (folio), quota_inode);if (IS_ERR (entry)) { err =PTR_ERR (entry); -if (err == -ENOENT ) +/* +* CP | dnode(F) | inode(DF) +* For this case, we should not give up now. +*/ +if (err == -ENOENT ) { +if (check_only) + *new_inode = true; goto next; + }f2fs_folio_put (folio, true);break ; }
@@ -875,6 +878,7 @@ int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)int ret =0 ;unsigned long s_flags = sbi->sb->s_flags;bool need_writecp = false; +bool new_inode = false;f2fs_notice (sbi,"f2fs_recover_fsync_data: recovery fsync data, " "check_only: %d" , check_only);
@@ -890,8 +894,8 @@ int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)f2fs_down_write (&sbi->cp_global_sem);/* step #1: find fsynced inode numbers */ - err =find_fsync_dnodes (sbi, &inode_list, check_only); -if (err ||list_empty (&inode_list)) + err =find_fsync_dnodes (sbi, &inode_list, check_only, &new_inode); +if (err <0 || (list_empty (&inode_list) && (!check_only || !new_inode))) goto skip;if (check_only) {
Signature · 1 line
2.49.0
This series was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:
Hello:
This series was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:
Quoted 14 lines · On Wed, 5 Nov 2025 14:50:22 +0800 you wrote:
With below scripts, it will trigger panic in f2fs:
mkfs.f2fs -f
/dev/vdd
mount/dev/vdd/mnt/f2fs
touch/mnt/f2fs/foo
sync
echo 111 >>/mnt/f2fs/foof2fs_iofsync/mnt/f2fs/foof2fs_ioshutdown 2/mnt/f2fs
umount/mnt/f2fs
mount -o ro,norecovery/dev/vdd/mnt/f2fsor
mount -o ro,disable_roll_forward/dev/vdd/mnt/f2fs
Quoted 1 line
[...]
Here is the summary with links:
- [f2fs-dev,1/2] f2fs: fix return value of f2fs_recover_fsync_data()
https://git.kernel.org/jaegeuk/f2fs/c/d05faa07e7da - [f2fs-dev,2/2] f2fs: fix to detect recoverable inode during dryrun of find_fsync_dnodes() https://git.kernel.org/jaegeuk/f2fs/c/ca244e9242a1
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html