All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] ext4: require a quota check after unclean fast-commit mounts
@ 2026-12-03  2:54 Wesley Atwell
  2026-12-03  3:24 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Wesley Atwell @ 2026-12-03  2:54 UTC (permalink / raw)
  To: tytso, jack
  Cc: adilger.kernel, libaokun, ojaswin, ritesh.list, yi.zhang,
	linux-ext4, atwellwea

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 <atwellwea@gmail.com>
---
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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] ext4: require a quota check after unclean fast-commit mounts
  2026-12-03  2:54 [PATCH v2] ext4: require a quota check after unclean fast-commit mounts Wesley Atwell
@ 2026-12-03  3:24 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03  3:24 UTC (permalink / raw)
  To: Wesley Atwell; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Medium] ext4: missing EXT4_VALID_FS restore and clear in
  ext4_freeze() and unfreeze
- [Medium] ext4: RO alternate-sb mount replays journal without primary
  repair marker
- [Medium] ext4: failure after ext4_setup_super() leaves clean fs marked
  unclean
- [Medium] ext4: broken barrier check on remount-ro and noload in
  __ext4_remount()
- [Medium] ext4: __ext4_remount() restores EXT4_VALID_FS when
  es->s_last_orphan != 0
- [Medium] ext4: unjournaled orphan cleanup on ro,noload mount in
  __ext4_fill_super()
--

commit ed35d76551f991fed3ccca432a490ffbc38374a5
Author: Wesley Atwell <atwellwea@gmail.com>

ext4: require a quota check after unclean fast-commit mounts

This commit keeps EXT4_VALID_FS cleared on the primary superblock during
writable mounts of filesystems with both quota and fast-commit features
enabled, and refuses writable mounts or remounts after an unclean shutdown
until e2fsck runs.

Link: https://lore.kernel.org/linux-ext4/kernel-7f509bcdfe16388c-v1.atwellwea@gmail.com/

> 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
[ ... ]
> @@ -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;
> +	}

[Severity: Medium]
Should ext4_freeze() and ext4_unfreeze() also restore and re-clear
EXT4_VALID_FS when ext4_fc_quota_guard(sb) is true?

ext4_setup_super() clears EXT4_VALID_FS in es->s_state on disk for the
duration of a writable mount.  When ext4_freeze() flushes the journal and
clears EXT4_FEATURE_INCOMPAT_RECOVER and
EXT4_FEATURE_RO_COMPAT_ORPHAN_PRESENT to prepare a clean on-disk state for
snapshots, EXT4_VALID_FS remains cleared in es->s_state, so any snapshot
taken while frozen fails writable mounts with -EUCLEAN.

[Severity: Medium]
If a later step in __ext4_fill_super() or __ext4_remount() fails after
ext4_setup_super() commits the superblock, does the error path leave a
clean filesystem marked unclean on disk?

For example, during a read-only to read-write remount in __ext4_remount(),
ext4_setup_super() clears EXT4_VALID_FS and sets
EXT4_FEATURE_INCOMPAT_RECOVER on disk before ext4_multi_mount_protect(),
ext4_setup_system_zone(), and ext4_enable_quotas() run.  If any of those
fails, restore_opts reverts sb->s_flags to SB_RDONLY without restoring
es->s_state or clearing the recovery flags on disk, so subsequent writable
remounts or mounts after umount fail with -EUCLEAN until e2fsck is run.

> @@ -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;
> +		}

[Severity: Medium]
Can a read-only mount using an alternate superblock (sbi->s_sb_block != 1)
without noload replay or wipe the journal without recording the repair
marker on the primary superblock?

When sb_rdonly(sb) is true and sbi->s_sb_block != 1, sbi->s_mount_state
and needs_recovery are loaded from the alternate superblock rather than
block 1, and ext4_fc_quota_mark_repair_needed() in ext4_load_journal() is
a no-op because it returns early when sbi->s_sb_block != 1.  If the
alternate superblock has EXT4_VALID_FS cleared and INCOMPAT_RECOVER set
(for example after an online resize updated backup superblocks while
mounted), a read-only mount replays and resets the journal without
reasserting ~EXT4_VALID_FS on the primary superblock; conversely, if the
alternate superblock is clean and lacks INCOMPAT_RECOVER after a crash,
ext4_load_journal() calls jbd2_journal_wipe() and wipes the unrecovered
journal.

> +		/* 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;
> +		}

[Severity: Medium]
When an unclean filesystem with pending orphans is mounted with -o
ro,noload on a writable block device, doesn't __ext4_fill_super() still
run ext4_orphan_cleanup() without replaying the journal?

Exempting test_opt(sb, NOLOAD) here allows the mount to proceed with
sbi->s_journal == NULL into ext4_orphan_cleanup(), which does not check
test_opt(sb, NOLOAD).  When !bdev_read_only(sb->s_bdev),
ext4_orphan_cleanup() temporarily clears SB_RDONLY, enables quotas, and
truncates and deletes orphan inodes using unjournaled handles on top of
the un-replayed journal; if es->s_last_orphan was the only recovery
indicator on an image with EXT4_VALID_FS set, this also clears
es->s_last_orphan on disk without clearing EXT4_VALID_FS.

[ ... ]
> @@ -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;
> +	}

[Severity: Medium]
Does checking !sb_rdonly(sb) here reject remounting a writable filesystem
to read-only when barriers are disabled?

At this point in __ext4_remount(), sb->s_flags still holds the pre-remount
flags while fc->sb_flags holds the requested remount flags, so a rw-to-ro
remount with nobarrier sees !sb_rdonly(sb) as true and fails with -EINVAL.
Also, in __ext4_fill_super(), the barrier check runs before the NOLOAD /
no-journal branch calls clear_opt2(sb, JOURNAL_FAST_COMMIT), so a clean
writable mount with -o noload,nobarrier is rejected with -EINVAL even
though fast commits are disabled.

[ ... ]
> @@ -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);
> +	}

[Severity: Medium]
Can this restore EXT4_VALID_FS on disk while es->s_last_orphan is still
non-zero?

ext4_mark_recovery_complete() checks ext4_orphan_file_empty(sb) but does
not check es->s_last_orphan.  If es->s_last_orphan is non-zero during a
read-write to read-only transition (for example, on an emergency remount
via SB_FORCE that bypasses sb_prepare_remount_readonly(), or after an
error path calls ext4_orphan_del(NULL, inode) on a linked inode),
committing EXT4_VALID_FS with es->s_last_orphan != 0 causes subsequent
mounts—including read-only mounts without noload—to see needs_recovery &&
(sbi->s_mount_state & EXT4_VALID_FS) and fail with -EUCLEAN.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/kernel-ext4-fc-quota-recovery-v2-d1a77368f2d9e49c0f709e35e6b6637186fa2908@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03  3:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  2:54 [PATCH v2] ext4: require a quota check after unclean fast-commit mounts Wesley Atwell
2026-12-03  3:24 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.