Root Cause Analysis
The crash in f2fs_put_super() (fs/f2fs/super.c:2084) reports:
F2FS-fs (loop7): detect filesystem reference count leak during umount, type: 2, count: -1, err: -5, cp_err: 1
Here, type: 2 is F2FS_DIRTY_QDATA in enum count_type (fs/f2fs/f2fs.h:1238-1254), count: -1 is sbi->nr_caches[F2FS_DIRTY_QDATA], err: -5 (-EIO) is returned by f2fs_write_checkpoint() at fs/f2fs/super.c:2038, and cp_err: 1 reflects f2fs_cp_error(sbi) (SBI_CP_ERROR set prior to/during unmount). Notice that f2fs_put_super() loops from i = 0 (F2FS_DIRTY_DENTS) to NR_COUNT_TYPE - 1 (fs/f2fs/super.c:2078-2085), so both F2FS_DIRTY_DENTS (0) and F2FS_DIRTY_DATA (1) are 0, while F2FS_DIRTY_QDATA (2) is -1.
Broken Invariant in Dirty-Page Accounting
sbi->nr_caches[F2FS_DIRTY_QDATA] is modified in only two functions, both in fs/f2fs/f2fs.h:
inode_inc_dirty_pages(struct inode *inode) (fs/f2fs/f2fs.h:2869-2876) increments F2FS_I(inode)->dirty_pages and sbi->nr_caches[F2FS_DIRTY_DENTS] or sbi->nr_caches[F2FS_DIRTY_DATA], and conditionally increments sbi->nr_caches[F2FS_DIRTY_QDATA] if IS_NOQUOTA(inode) (inode->i_flags & S_NOQUOTA) is true.
inode_dec_dirty_pages(struct inode *inode) (fs/f2fs/f2fs.h:2883-2894) decrements F2FS_I(inode)->dirty_pages and sbi->nr_caches[F2FS_DIRTY_DENTS] or sbi->nr_caches[F2FS_DIRTY_DATA], and conditionally decrements sbi->nr_caches[F2FS_DIRTY_QDATA] if IS_NOQUOTA(inode) is true.
For F2FS_DIRTY_DATA to balance to 0 while F2FS_DIRTY_QDATA underflows to -1, a folio belonging to a regular (or symlink) inode must be dirtied (inode_inc_dirty_pages()) while S_NOQUOTA is clear (incrementing F2FS_DIRTY_DATA only), and later written back or invalidated (inode_dec_dirty_pages()) while S_NOQUOTA is set (decrementing both F2FS_DIRTY_DATA and F2FS_DIRTY_QDATA).
How the State Transition Happens
-
Missing synchronization in f2fs_quota_on() (fs/f2fs/super.c:3561-3608):
When quotactl(Q_QUOTAON) is invoked on a regular file (when F2FS_FEATURE_QUOTA_INO is not enabled), f2fs_quota_on() attempts to flush dirty pages before handing the inode to the quota subsystem:
err = f2fs_quota_convert_inline(inode);
...
err = filemap_fdatawrite(inode->i_mapping);
...
err = filemap_fdatawait(inode->i_mapping);
...
err = dquot_quota_on(sb, type, format_id, path);
f2fs_quota_on() does not hold inode_lock(inode), does not hold filemap_invalidate_lock(inode->i_mapping), and does not prevent concurrent writable opens or mmap page faults (e.g. via deny_write_access()).
-
Window between flush and S_NOQUOTA:
Between filemap_fdatawait(inode->i_mapping) (fs/f2fs/super.c:3592) and dquot_quota_on() -> dquot_load_quota_inode() -> vfs_setup_quota_inode() (fs/quota/dquot.c:2365-2367, which acquires inode_lock(inode) and sets inode->i_flags |= S_NOQUOTA), another thread can dirty a folio of inode via write() or a shared mmap fault (f2fs_vm_page_mkwrite() -> folio_mark_dirty() -> f2fs_dirty_data_folio() -> f2fs_update_dirty_folio()). Because S_NOQUOTA is still clear at that moment, inode_inc_dirty_pages(inode) increments F2FS_DIRTY_DATA by 1 and leaves F2FS_DIRTY_QDATA unchanged.
-
Underflow on writeback or invalidation:
Immediately after vfs_setup_quota_inode() sets S_NOQUOTA, dquot_load_quota_inode() (fs/quota/dquot.c:2466-2472) calls dquot_load_quota_sb(). Before check_quota_file() even validates whether the target file is a valid quota file, dquot_load_quota_sb() calls sync_filesystem(sb) (fs/quota/dquot.c:2420). Writing back or invalidating that folio (in f2fs_write_single_data_page() at fs/f2fs/data.c:3425, f2fs_write_compressed_pages() at fs/f2fs/compress.c:1417, or f2fs_invalidate_folio() at fs/f2fs/data.c:4319) calls inode_dec_dirty_pages(inode) while IS_NOQUOTA(inode) is now true:
F2FS_DIRTY_DATA is decremented back to 0.
F2FS_DIRTY_QDATA is decremented from 0 to -1.
Even if dquot_load_quota_sb() subsequently fails (e.g., -EINVAL from check_quota_file() on an arbitrary file) and vfs_cleanup_quota_inode() (fs/quota/dquot.c:2196-2210) clears S_NOQUOTA, the -1 underflow in sbi->nr_caches[F2FS_DIRTY_QDATA] persists.
(Note: A secondary path with the same pattern exists in f2fs_quota_enable() at fs/f2fs/super.c:3410-3436, which sets qf_inode->i_flags |= S_NOQUOTA on qf_inum without flushing dirty folios if a corrupted image or RO->RW remount in __f2fs_remount() exposed qf_inum with dirty pages beforehand; by contrast, f2fs_new_inode()'s fail_drop: at fs/f2fs/namei.c:358 only sets S_NOQUOTA on an unexposed I_NEW inode with no dirty folios.)
-
Failure at unmount:
During umount, f2fs_put_super() checks all sbi->nr_caches[i] counters after dropping caches (fs/f2fs/super.c:2078-2085), finds get_nr_caches(sbi, F2FS_DIRTY_QDATA) == -1, logs the reference count leak error, and hits f2fs_bug_on(sbi, 1) at fs/f2fs/super.c:2084.