f2fs: fix to flush all dirty inodes recovered in readonly fs
authorChao Yu <yuchao0@huawei.com>
Wed, 22 Aug 2018 09:11:05 +0000 (17:11 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Fri, 26 Oct 2018 18:21:39 +0000 (11:21 -0700)
generic/417 reported as blow:

------------[ cut here ]------------
kernel BUG at /home/yuchao/git/devf2fs/inode.c:695!
invalid opcode: 0000 [#1] PREEMPT SMP
CPU: 1 PID: 21697 Comm: umount Tainted: G        W  O      4.18.0-rc2+ #39
Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006
EIP: f2fs_evict_inode+0x556/0x580 [f2fs]
Call Trace:
 ? _raw_spin_unlock+0x2c/0x50
 evict+0xa8/0x170
 dispose_list+0x34/0x40
 evict_inodes+0x118/0x120
 generic_shutdown_super+0x41/0x100
 ? rcu_read_lock_sched_held+0x97/0xa0
 kill_block_super+0x22/0x50
 kill_f2fs_super+0x6f/0x80 [f2fs]
 deactivate_locked_super+0x3d/0x70
 deactivate_super+0x40/0x60
 cleanup_mnt+0x39/0x70
 __cleanup_mnt+0x10/0x20
 task_work_run+0x81/0xa0
 exit_to_usermode_loop+0x59/0xa7
 do_fast_syscall_32+0x1f5/0x22c
 entry_SYSENTER_32+0x53/0x86
EIP: f2fs_evict_inode+0x556/0x580 [f2fs]

It can simply reproduced with scripts:

Enable quota feature during mkfs.

Testcase1:
1. mkfs.f2fs /dev/zram0
2. mount -t f2fs /dev/zram0 /mnt/f2fs
3. xfs_io -f /mnt/f2fs/file -c "pwrite 0 4k" -c "fsync"
4. godown /mnt/f2fs
5. umount /mnt/f2fs
6. mount -t f2fs -o ro /dev/zram0 /mnt/f2fs
7. umount /mnt/f2fs

Testcase2:
1. mkfs.f2fs /dev/zram0
2. mount -t f2fs /dev/zram0 /mnt/f2fs
3. touch /mnt/f2fs/file
4. create process[pid = x] do:
a) open /mnt/f2fs/file;
b) unlink /mnt/f2fs/file
5. godown -f /mnt/f2fs
6. kill process[pid = x]
7. umount /mnt/f2fs
8. mount -t f2fs -o ro /dev/zram0 /mnt/f2fs
9. umount /mnt/f2fs

The reason is: during recovery, i_{c,m}time of inode will be updated, then
the inode can be set dirty w/o being tracked in sbi->inode_list[DIRTY_META]
global list, so later write_checkpoint will not flush such dirty inode into
node page.

Once umount is called, sync_filesystem() in generic_shutdown_super() will
skip syncng dirty inodes due to sb_rdonly check, leaving dirty inodes
there.

To solve this issue, during umount, add remove SB_RDONLY flag in
sb->s_flags, to make sure sync_filesystem() will not be skipped.

Signed-off-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/checkpoint.c
fs/f2fs/f2fs.h
fs/f2fs/recovery.c
fs/f2fs/super.c

index 6b24eb48147807e196ae2294259a6ffee4a78033..504b6eff7a6d0255264df584746c65af3b7b1c9a 100644 (file)
@@ -696,6 +696,8 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
        /* clear Orphan Flag */
        clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
 out:
+       set_sbi_flag(sbi, SBI_IS_RECOVERED);
+
 #ifdef CONFIG_QUOTA
        /* Turn quotas off */
        if (quota_enabled)
index c30b17a6ee52ecb4ca9ea93aea79a63751611e50..2ede7083ffc034a957e1f55b64a4a9c35e006c85 100644 (file)
@@ -1089,6 +1089,7 @@ enum {
        SBI_NEED_SB_WRITE,                      /* need to recover superblock */
        SBI_NEED_CP,                            /* need to checkpoint */
        SBI_IS_SHUTDOWN,                        /* shutdown by ioctl */
+       SBI_IS_RECOVERED,                       /* recovered orphan/data */
 };
 
 enum {
index 501bb0fdda1b3590a011a021ab32a518377d7ccf..e34ca1686e3b555bcb75dc29f598b7c1f8e96ef1 100644 (file)
@@ -697,11 +697,15 @@ skip:
        /* let's drop all the directory inodes for clean checkpoint */
        destroy_fsync_dnodes(&dir_list);
 
-       if (!err && need_writecp) {
-               struct cp_control cpc = {
-                       .reason = CP_RECOVERY,
-               };
-               err = f2fs_write_checkpoint(sbi, &cpc);
+       if (need_writecp) {
+               set_sbi_flag(sbi, SBI_IS_RECOVERED);
+
+               if (!err) {
+                       struct cp_control cpc = {
+                               .reason = CP_RECOVERY,
+                       };
+                       err = f2fs_write_checkpoint(sbi, &cpc);
+               }
        }
 
        kmem_cache_destroy(fsync_entry_slab);
index 6aa571bb0baa5d72df8810374e8b92cc4a3f9a34..f650e51c106097ac10251b9d6a4f5cfdf76aaa5f 100644 (file)
@@ -3179,6 +3179,9 @@ static void kill_f2fs_super(struct super_block *sb)
                        };
                        f2fs_write_checkpoint(sbi, &cpc);
                }
+
+               if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
+                       sb->s_flags &= ~SB_RDONLY;
        }
        kill_block_super(sb);
 }