Fast-commit replay can restore an fsynced file's extents without replaying the matching internal quota-file updates. The file then survives a crash with too little quota charged, allowing its owner to exceed a hard limit. For filesystems with the quota and fast-commit features, keep the superblock marked unclean during a writable mount. Refuse recovery on a writable mount until e2fsck replays the journal and reconciles quota usage. A read-only mount may replay when the repair marker is already durable; it leaves that marker set until e2fsck completes. Restore the clean state only after a successful journal shutdown. Ordinary quota-accounted writes can continue to use fast commits; the full-check cost moves to recovery of unclean mounts. Writable mounts using an alternate superblock are refused because the repair marker must be recorded on the primary superblock. Read-only alternate-superblock mounts remain available. The barrier requirement applies only when fast commits are enabled at runtime. Write the marker with its checksum under the superblock buffer lock, including when a clean filesystem is mounted writable, so a concurrent MMP error writer cannot submit a mismatched checksum. Journal replay can restore an older superblock image with the clean bit set. Reassert the marker after replay, before read-only recovery clears the journal's recovery flag. Existing quota and fast-commit filesystems need one forced e2fsck check before their first writable mount with this change. The marker cannot detect an undercharge left on a clean-looking image by older recovery. On the ext4 dev kernel, a QEMU guest fsynced an 8 MiB file and lost power. Direct kernel recovery kept the file but charged only its previous 4 MiB and allowed a further 10 MiB write under a 16 MiB hard limit. With this change, direct recovery was refused, stock e2fsck -p restored the 8,392,704-byte charge, and the follow-up write stopped at the limit. Read-only recovery of an old crash image with a clean superblock logged in the journal kept the repair marker set. The next writable mount still required e2fsck, which restored the quota charge and hard-limit check. This contribution was developed with AI assistance. Fixes: aa75f4d3daaeb ("ext4: main fast-commit commit path") Link: https://lore.kernel.org/linux-ext4/kernel-7f509bcdfe16388c-v1.atwellwea@gmail.com/ Signed-off-by: Wesley Atwell --- Changes since v1: - Keep routine quota writes eligible for fast commits and require a full quota check only after an unclean shutdown. - Preserve the repair marker through read-only replay, including older logs. fs/ext4/super.c | 134 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 122 insertions(+), 12 deletions(-) diff --git a/fs/ext4/super.c b/fs/ext4/super.c index 880f664d6c04b..00d4150ba9ad1 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -71,6 +71,7 @@ static int ext4_load_journal(struct super_block *, struct ext4_super_block *, static int ext4_show_options(struct seq_file *seq, struct dentry *root); static void ext4_update_super(struct super_block *sb); static int ext4_commit_super(struct super_block *sb); +static int ext4_commit_super_invalid(struct super_block *sb); static int ext4_mark_recovery_complete(struct super_block *sb, struct ext4_super_block *es); static int ext4_clear_journal_err(struct super_block *sb, @@ -3207,6 +3208,29 @@ int ext4_seq_options_show(struct seq_file *seq, void *offset) } EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_seq_options_show); +static bool ext4_fc_quota_guard(struct super_block *sb) +{ + return ext4_has_feature_quota(sb) && + ext4_has_feature_fast_commit(sb); +} + +static int ext4_fc_quota_mark_repair_needed(struct super_block *sb) +{ + struct ext4_sb_info *sbi = EXT4_SB(sb); + struct ext4_super_block *es = sbi->s_es; + int err; + + if (!(le16_to_cpu(es->s_state) & EXT4_VALID_FS) || + sbi->s_sb_block != 1 || bdev_read_only(sb->s_bdev)) + return 0; + + /* Keep the fsck requirement through journal-only recovery. */ + err = ext4_commit_super_invalid(sb); + if (err || test_opt(sb, BARRIER)) + return err; + return blkdev_issue_flush(sb->s_bdev); +} + static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es, int read_only) { @@ -3240,6 +3264,11 @@ static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es, ext4_msg(sb, KERN_WARNING, "warning: checktime reached, " "running e2fsck is recommended"); + if (ext4_fc_quota_guard(sb)) { + err = ext4_fc_quota_mark_repair_needed(sb); + if (err) + goto done; + } if (!sbi->s_journal) es->s_state &= cpu_to_le16(~EXT4_VALID_FS); if (!(__s16) le16_to_cpu(es->s_max_mnt_count)) @@ -5618,6 +5647,37 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb) goto failed_mount3a; } + if (ext4_fc_quota_guard(sb)) { + if (sbi->s_sb_block != 1 && !sb_rdonly(sb)) { + ext4_msg(sb, KERN_ERR, + "quota fast commit requires primary superblock"); + err = -EOPNOTSUPP; + goto failed_mount3a; + } + /* RO replay is safe when the repair marker is already durable. */ + if ((needs_recovery || !(sbi->s_mount_state & EXT4_VALID_FS)) && + (!sb_rdonly(sb) || + (!test_opt(sb, NOLOAD) && + (sbi->s_mount_state & EXT4_VALID_FS)))) { + if (needs_recovery && !sb_rdonly(sb)) { + err = ext4_fc_quota_mark_repair_needed(sb); + if (err) + goto failed_mount3a; + } + ext4_msg(sb, KERN_ERR, + "run e2fsck before quota fast-commit recovery"); + err = -EUCLEAN; + goto failed_mount3a; + } + if (!sb_rdonly(sb) && !test_opt(sb, BARRIER) && + test_opt2(sb, JOURNAL_FAST_COMMIT)) { + ext4_msg(sb, KERN_ERR, + "quota fast commit requires barriers"); + err = -EINVAL; + goto failed_mount3a; + } + } + err = -EINVAL; /* * The first inode we look at is the journal inode. Don't try @@ -6314,6 +6374,13 @@ static int ext4_load_journal(struct super_block *sb, err = jbd2_journal_load(journal); if (enable_write) sb->s_flags |= SB_RDONLY; + /* Replay can restore an older, clean superblock image. */ + if (ext4_fc_quota_guard(sb) && + !(EXT4_SB(sb)->s_mount_state & EXT4_VALID_FS)) { + int err2 = ext4_fc_quota_mark_repair_needed(sb); + + err = err ? : err2; + } if (save && memcmp(((char *) es) + EXT4_S_ERR_START, save, EXT4_S_ERR_LEN)) { memcpy(((char *) es) + EXT4_S_ERR_START, @@ -6440,16 +6507,23 @@ static void ext4_update_super(struct super_block *sb) unlock_buffer(sbh); } -static int ext4_commit_super(struct super_block *sb) +static int ext4_write_super(struct super_block *sb, bool invalidate) { struct buffer_head *sbh = EXT4_SB(sb)->s_sbh; if (!sbh) return -EINVAL; - ext4_update_super(sb); + if (!invalidate) + ext4_update_super(sb); lock_buffer(sbh); + if (invalidate) { + /* Submit the repair marker with its matching checksum. */ + EXT4_SB(sb)->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS); + ext4_superblock_csum_set(sb); + } + /* Buffer got discarded which means block device got invalidated */ if (!buffer_mapped(sbh)) { unlock_buffer(sbh); @@ -6485,6 +6559,16 @@ static int ext4_commit_super(struct super_block *sb) return 0; } +static int ext4_commit_super(struct super_block *sb) +{ + return ext4_write_super(sb, false); +} + +static int ext4_commit_super_invalid(struct super_block *sb) +{ + return ext4_write_super(sb, true); +} + /* * Have we just finished recovery? If so, and if we are mounting (or * remounting) the filesystem readonly, then we will end up with a @@ -6711,6 +6795,7 @@ static int __ext4_remount(struct fs_context *fc, struct super_block *sb) ext4_group_t g; int err = 0; int alloc_ctx; + bool finish_fc_quota_ro = false; #ifdef CONFIG_QUOTA int enable_quota = 0; int i, j; @@ -6811,6 +6896,15 @@ static int __ext4_remount(struct fs_context *fc, struct super_block *sb) es = sbi->s_es; + if (ext4_fc_quota_guard(sb) && !test_opt(sb, BARRIER) && + test_opt2(sb, JOURNAL_FAST_COMMIT) && + (!sb_rdonly(sb) || !(fc->sb_flags & SB_RDONLY))) { + ext4_msg(sb, KERN_ERR, + "quota fast commit requires barriers"); + err = -EINVAL; + goto restore_opts; + } + if (sbi->s_journal) { ext4_init_journal_params(sb, sbi->s_journal); set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio); @@ -6844,18 +6938,27 @@ static int __ext4_remount(struct fs_context *fc, struct super_block *sb) * readonly, and if so set the rdonly flag and then * mark the partition as valid again. */ - if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) && - (sbi->s_mount_state & EXT4_VALID_FS)) - es->s_state = cpu_to_le16(sbi->s_mount_state); - - if (sbi->s_journal) { - /* - * We let remount-ro finish even if marking fs - * as clean failed... - */ - ext4_mark_recovery_complete(sb, es); + if (ext4_fc_quota_guard(sb) && sbi->s_journal) { + finish_fc_quota_ro = true; + } else { + if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) && + (sbi->s_mount_state & EXT4_VALID_FS)) + es->s_state = cpu_to_le16(sbi->s_mount_state); + if (sbi->s_journal) + ext4_mark_recovery_complete(sb, es); } } else { + if (ext4_fc_quota_guard(sb) && + (sbi->s_sb_block != 1 || + !(le16_to_cpu(es->s_state) & EXT4_VALID_FS) || + es->s_last_orphan || + ext4_has_feature_orphan_present(sb) || + ext4_has_feature_journal_needs_recovery(sb))) { + ext4_msg(sb, KERN_ERR, + "run e2fsck before writable quota fast-commit remount"); + err = -EUCLEAN; + goto restore_opts; + } /* Make sure we can mount this feature set readwrite */ if (ext4_has_feature_readonly(sb) || !ext4_feature_set_ok(sb, 0)) { @@ -6981,6 +7084,13 @@ static int __ext4_remount(struct fs_context *fc, struct super_block *sb) */ if (test_opt2(sb, ABORT)) ext4_abort(sb, ESHUTDOWN, "Abort forced by user"); + if (finish_fc_quota_ro && !ext4_emergency_state(sb) && + !ext4_mark_recovery_complete(sb, es) && + !(es->s_state & cpu_to_le16(EXT4_VALID_FS)) && + (sbi->s_mount_state & EXT4_VALID_FS)) { + es->s_state = cpu_to_le16(sbi->s_mount_state); + ext4_commit_super(sb); + } return 0; base-commit: aceadbccd428fb8e6593b27a3091d961f80225d1