[PATCH 2/6] f2fs: quota: fix to recheck quota flush after taking node_change in checkpoint

From: Chao Yu

Date: Tue Dec 01 2026 - 06:09:03 EST


From: Chao Yu <chao@xxxxxxxxxx>

block_operations() flushes quota until __need_flush_quota() returns
false, then takes node_change and rechecks F2FS_DIRTY_IMETA. However,
block allocation w/ F2FS_GET_BLOCK_PRE_AIO only holds node_change, so
it can dirty quota in between, and the F2FS_DIRTY_IMETA recheck misses
it once the inode is synced:

checkpoint write (F2FS_GET_BLOCK_PRE_AIO)
block_operations
f2fs_lock_all
__need_flush_quota
: return false
prepare_write_begin
f2fs_map_lock(node_change)
f2fs_reserve_block
: dirty dquot, update i_blocks
f2fs_map_unlock(node_change)
f2fs_write_inode
: clear F2FS_DIRTY_IMETA
f2fs_down_write(node_change)
F2FS_DIRTY_IMETA is 0, go on
do_checkpoint
: persist new i_blocks w/ stale quota,
CP_QUOTA_NEED_FSCK_FLAG is not set

After a sudden power-off, fsck.f2fs reports "Quota file is missing or
invalid quota file content found." w/o quota_need_fsck flag in the
checkpoint. It was hit by f2fs consistency test (fault injection +
fsstress/fio/atomic write + periodic shutdown).

Fix it by rechecking quota after taking node_change, and retrying the
quota flush if quota is dirty, or quota_sem is held, e.g. quota file
is written by f2fs_dquot_acquire(). The retry is still bounded by
DEFAULT_RETRY_QUOTA_FLUSH_COUNT, and the recheck is skipped once
SBI_QUOTA_SKIP_FLUSH is set.

Cc: stable@xxxxxxxxxxxxxxx
Fixes: db6ec53b7e03 ("f2fs: add a rw_sem to cover quota flag changes")
Signed-off-by: Chao Yu <chao@xxxxxxxxxx>
---
fs/f2fs/checkpoint.c | 30 +++++++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c
index a8c98a2acc53..a5efc2a82224 100644
--- a/fs/f2fs/checkpoint.c
+++ b/fs/f2fs/checkpoint.c
@@ -1522,6 +1522,29 @@ static bool __need_flush_quota(struct f2fs_sb_info *sbi)
return ret;
}

+/* Like __need_flush_quota(), but don't clear SBI_QUOTA_NEED_FLUSH. */
+static bool __quota_flush_pending(struct f2fs_sb_info *sbi)
+{
+ bool ret = false;
+
+ if (!is_journalled_quota(sbi))
+ return false;
+
+ /* we gave up flushing quota, don't retry */
+ if (is_sbi_flag_set(sbi, SBI_QUOTA_SKIP_FLUSH))
+ return false;
+
+ /* quota is being updated, e.g. by f2fs_dquot_acquire() */
+ if (!f2fs_down_write_trylock(&sbi->quota_sem))
+ return true;
+ if (!is_sbi_flag_set(sbi, SBI_QUOTA_NEED_REPAIR) &&
+ (is_sbi_flag_set(sbi, SBI_QUOTA_NEED_FLUSH) ||
+ get_nr_caches(sbi, F2FS_DIRTY_QDATA)))
+ ret = true;
+ f2fs_up_write(&sbi->quota_sem);
+ return ret;
+}
+
/*
* Freeze all the FS-operations for checkpoint.
*/
@@ -1574,7 +1597,12 @@ static int block_operations(struct f2fs_sb_info *sbi)
*/
f2fs_down_write(&sbi->node_change);

- if (get_nr_caches(sbi, F2FS_DIRTY_IMETA)) {
+ /*
+ * F2FS_GET_BLOCK_PRE_AIO allocates blocks w/ node_change only, so it
+ * may have updated i_blocks and quota since the checks above.
+ */
+ if (get_nr_caches(sbi, F2FS_DIRTY_IMETA) ||
+ __quota_flush_pending(sbi)) {
f2fs_up_write(&sbi->node_change);
f2fs_unlock_all(sbi);
err = f2fs_sync_inode_meta(sbi);
--
2.49.0