Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH v2 0/2] ext4: protect quota acquisition against filesystem freezing
@ 2026-11-25 11:10 Ye Bin
  2026-11-25 11:10 ` [PATCH v2 1/2] quota: Forbid Q_XGETQUOTA and Q_XGETNEXTQUOTA for frozen filesystem Ye Bin
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Ye Bin @ 2026-11-25 11:10 UTC (permalink / raw)
  To: tytso, adilger.kernel, linux-ext4; +Cc: jack

From: Ye Bin <yebin10@huawei.com>

This series fixes quota operations running against a frozen filesystem,
and the resulting freeze protection for ext4's dquot acquisition.

Patch 1 makes Q_XGETQUOTA/Q_XGETNEXTQUOTA consistent with Q_GETQUOTA and
Q_GETNEXTQUOTA and forbids them on a frozen filesystem.  These commands
may end up in ->acquire_dquot() when the queried dquot is not cached yet,
and thus may write to the filesystem, just like Q_GETQUOTA/Q_GETNEXTQUOTA
which commit ccf370e43e08 ("quota: Forbid Q_GETQUOTA and Q_GETNEXTQUOTA
for frozen filesystem") already handles.  They were historically treated
as read-only because they come from XFS, where quota is always coherent,
but generic quota now routes them through dquot_get_dqblk()/
dquot_get_next_dqblk() and dqget().  Running them on a frozen filesystem
makes ext4 start a transaction on it and warn.  As suggested by Jan, they
are now handled exactly like the generic commands, including taking write
access, so a Q_XGETQUOTA on a read-only mount will report -EROFS; proper
read-only querying can be added later if somebody actually needs it.

Patch 2 protects ext4_acquire_dquot() itself.  A writable open reaches
the filesystem's ->open() callback without superblock freeze protection,
so ext4_file_open() -> dquot_file_open() -> dquot_initialize() can
acquire a dquot and start a transaction after the filesystem has been
frozen.  Take sb_start_intwrite() around ext4_acquire_dquot() when no
transaction is already running; running transactions are already
protected against freezing.

Patch 2 relies on the preceding patch 1.  Taking freeze protection in
ext4_acquire_dquot() while holding s_umount, as the Q_XGETQUOTA and
Q_XGETNEXTQUOTA paths do, would deadlock against thaw_super(), which
needs s_umount exclusively to release the SB_FREEZE_FS protection.
Patch 1 forbids those commands on a frozen filesystem and thus makes the
new protection in patch 2 safe.  Both patches must be applied together.

Ye Bin (2):
  quota: Forbid Q_XGETQUOTA and Q_XGETNEXTQUOTA for frozen filesystem
  ext4: protect ext4_acquire_dquot against freezing

 fs/ext4/super.c  | 20 +++++++++++++++++++-
 fs/quota/quota.c |  9 ++++-----
 2 files changed, 23 insertions(+), 6 deletions(-)

-- 
2.34.1


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

* [PATCH v2 1/2] quota: Forbid Q_XGETQUOTA and Q_XGETNEXTQUOTA for frozen filesystem
  2026-11-25 11:10 [PATCH v2 0/2] ext4: protect quota acquisition against filesystem freezing Ye Bin
@ 2026-11-25 11:10 ` Ye Bin
  2026-11-25 11:10 ` [PATCH v2 2/2] ext4: protect ext4_acquire_dquot against freezing Ye Bin
  2026-11-25 11:30 ` [PATCH v2 0/2] ext4: protect quota acquisition against filesystem freezing sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: Ye Bin @ 2026-11-25 11:10 UTC (permalink / raw)
  To: tytso, adilger.kernel, linux-ext4; +Cc: jack

From: Ye Bin <yebin10@huawei.com>

Commit ccf370e43e08 (quota: Forbid Q_GETQUOTA and Q_GETNEXTQUOTA for
frozen filesystem) forbade Q_GETQUOTA and Q_GETNEXTQUOTA on a frozen
filesystem because ext4_acquire_dquot() prepares for allocating space for
a new quota structure and OCFS2 needs to update the on-disk structure use
count when a new cluster node loads quota information from disk.  The
same applies to Q_XGETQUOTA and Q_XGETNEXTQUOTA: userspace treats them as
read-only commands, but if the queried dquot is not cached yet,
->get_dqblk()/->get_nextdqblk() call dqget() -> ->acquire_dquot(), which
may write to the filesystem.

Until now this only made ext4 warn about starting a transaction on a
frozen filesystem.  Now that ext4_acquire_dquot() also takes
sb_start_intwrite() to protect against freezing, it turns into a
deadlock: the query holds s_umount for reading and blocks on the
SB_FREEZE_FS freeze protection, which thaw_super() can only release
after taking s_umount exclusively.

So forbid Q_XGETQUOTA and Q_XGETNEXTQUOTA on a frozen filesystem as well
by moving them out of the read-only set in quotactl_cmd_write(), just
like Q_GETQUOTA and Q_GETNEXTQUOTA.  This also makes quotactl_fd() take
write access for them, so a query on a read-only mount now fails with
-EROFS; that is the same behavior the generic quota commands already
have.  If someone needs read-only querying on a frozen or read-only
filesystem, proper read-only dquot lookup can be implemented separately.

Suggested-by: Jan Kara <jack@suse.cz>
Signed-off-by: Ye Bin <yebin10@huawei.com>
---
 fs/quota/quota.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/fs/quota/quota.c b/fs/quota/quota.c
index 94e6407734c7..1669bb09b9f7 100644
--- a/fs/quota/quota.c
+++ b/fs/quota/quota.c
@@ -834,9 +834,10 @@ static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
 static int quotactl_cmd_write(int cmd)
 {
 	/*
-	 * We cannot allow Q_GETQUOTA and Q_GETNEXTQUOTA without write access
-	 * as dquot_acquire() may allocate space for new structure and OCFS2
-	 * needs to increment on-disk use count.
+	 * We cannot allow Q_GETQUOTA, Q_GETNEXTQUOTA, Q_XGETQUOTA and
+	 * Q_XGETNEXTQUOTA without write access as dquot_acquire() may allocate
+	 * space for new structure and OCFS2 needs to increment on-disk use
+	 * count.
 	 */
 	switch (cmd) {
 	case Q_GETFMT:
@@ -844,8 +845,6 @@ static int quotactl_cmd_write(int cmd)
 	case Q_SYNC:
 	case Q_XGETQSTAT:
 	case Q_XGETQSTATV:
-	case Q_XGETQUOTA:
-	case Q_XGETNEXTQUOTA:
 	case Q_XQUOTASYNC:
 		return 0;
 	}
-- 
2.34.1


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

* [PATCH v2 2/2] ext4: protect ext4_acquire_dquot against freezing
  2026-11-25 11:10 [PATCH v2 0/2] ext4: protect quota acquisition against filesystem freezing Ye Bin
  2026-11-25 11:10 ` [PATCH v2 1/2] quota: Forbid Q_XGETQUOTA and Q_XGETNEXTQUOTA for frozen filesystem Ye Bin
@ 2026-11-25 11:10 ` Ye Bin
  2026-11-25 11:30   ` sashiko-bot
  2026-11-25 11:30 ` [PATCH v2 0/2] ext4: protect quota acquisition against filesystem freezing sashiko-bot
  2 siblings, 1 reply; 5+ messages in thread
From: Ye Bin @ 2026-11-25 11:10 UTC (permalink / raw)
  To: tytso, adilger.kernel, linux-ext4; +Cc: jack

From: Ye Bin <yebin10@huawei.com>

A writable open of a regular file reaches the filesystem's ->open()
callback without holding any superblock write (freeze) protection, but
ext4_file_open() -> dquot_file_open() -> dquot_initialize() may need to
acquire a dquot and write the quota file.  If the filesystem reaches
SB_FREEZE_COMPLETE in that window, ext4_journal_check_start() hits

	WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE);

and the open then sleeps in jbd2 until the filesystem is thawed.

Taking freeze protection for every writable open in the VFS would make a
plain open(O_WRONLY) on a frozen filesystem block even when it never
writes, which is an unacceptable behaviour change.  Instead protect the
quota acquisition path itself: take sb_start_intwrite() around
ext4_acquire_dquot() when no transaction is already running.  Running
transactions are already protected against freezing and taking
sb_start_intwrite() inside one can deadlock.

This protection relies on the preceding quota change ("quota: Forbid
Q_XGETQUOTA and Q_XGETNEXTQUOTA for frozen filesystem").  Those commands
are treated as read-only and run with s_umount held for reading, but they
can still reach ext4_acquire_dquot().  Taking sb_start_intwrite() there
would deadlock against thaw_super(), which can only release the
SB_FREEZE_FS protection after taking s_umount exclusively.  The preceding
change forbids those commands on a frozen filesystem, which makes the new
protection safe, so both patches have to be applied together.

Fixes: 907f4554e252 ("dquot: move dquot initialization responsibility into the filesystem")
Suggested-by: Jan Kara <jack@suse.cz>
Signed-off-by: Ye Bin <yebin10@huawei.com>
---
 fs/ext4/super.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index d0415e5948e1..fc63219f5972 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -7110,11 +7110,25 @@ static int ext4_acquire_dquot(struct dquot *dquot)
 {
 	int ret, err;
 	handle_t *handle;
+	bool freeze_protected = false;
+
+	/*
+	 * Trying to sb_start_intwrite() in a running transaction
+	 * can result in a deadlock. Further, running transactions
+	 * are already protected from freezing.
+	 */
+	if (!ext4_journal_current_handle()) {
+		sb_start_intwrite(dquot->dq_sb);
+		freeze_protected = true;
+	}
 
 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
-	if (IS_ERR(handle))
+	if (IS_ERR(handle)) {
+		if (freeze_protected)
+			sb_end_intwrite(dquot->dq_sb);
 		return PTR_ERR(handle);
+	}
 	ret = dquot_acquire(dquot);
 	if (ret < 0)
 		ext4_error_err(dquot->dq_sb, -ret,
@@ -7123,6 +7137,10 @@ static int ext4_acquire_dquot(struct dquot *dquot)
 	err = ext4_journal_stop(handle);
 	if (!ret)
 		ret = err;
+
+	if (freeze_protected)
+		sb_end_intwrite(dquot->dq_sb);
+
 	return ret;
 }
 
-- 
2.34.1


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

* Re: [PATCH v2 2/2] ext4: protect ext4_acquire_dquot against freezing
  2026-11-25 11:10 ` [PATCH v2 2/2] ext4: protect ext4_acquire_dquot against freezing Ye Bin
@ 2026-11-25 11:30   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-25 11:30 UTC (permalink / raw)
  To: Ye Bin, linux-ext4; +Cc: tytso

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ext4: misplaced sb_end_intwrite() in ext4_write_dquot() instead
  of acquire
- [High] ext4: s_umount vs SB_FREEZE_FS deadlock in ext4_acquire_dquot()
  via vfs_ustat()
--

commit 365cb575bc6d55af6ebb9f261bcac19d705f99d4
Author: Ye Bin <yebin10@huawei.com>

ext4: protect ext4_acquire_dquot against freezing

This commit takes sb_start_intwrite() around ext4_acquire_dquot() when no
journal transaction is currently active to protect dquot acquisition
against filesystem freezing.

> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> index 880f664d6c04b..769cae2ae65ff 100644
> --- a/fs/ext4/super.c
> +++ b/fs/ext4/super.c
> @@ -7161,6 +7161,10 @@ static int ext4_write_dquot(struct dquot *dquot)
>  	err = ext4_journal_stop(handle);
>  	if (!ret)
>  		ret = err;
> +
> +	if (freeze_protected)
> +		sb_end_intwrite(dquot->dq_sb);
> +
>  	return ret;
>  }

[Severity: High]
Was this sb_end_intwrite() hunk meant for the end of ext4_acquire_dquot()
instead of ext4_write_dquot()?  freeze_protected is not declared in
ext4_write_dquot(), breaking the build when CONFIG_QUOTA is enabled, and
ext4_acquire_dquot() never releases SB_FREEZE_FS after
ext4_journal_stop().

> @@ -7168,11 +7172,25 @@ static int ext4_acquire_dquot(struct dquot *dquot)
>  {
>  	int ret, err;
>  	handle_t *handle;
> +	bool freeze_protected = false;
> +
> +	/*
> +	 * Trying to sb_start_intwrite() in a running transaction
> +	 * can result in a deadlock. Further, running transactions
> +	 * are already protected from freezing.
> +	 */
> +	if (!ext4_journal_current_handle()) {
> +		sb_start_intwrite(dquot->dq_sb);
> +		freeze_protected = true;
> +	}

[Severity: High]
Can calling sb_start_intwrite() here cause an ABBA deadlock between
s_umount and SB_FREEZE_FS when ustat(2) runs on a frozen filesystem?

Unlike Q_XGETQUOTA, vfs_ustat() acquires s_umount for reading via
user_get_super(dev, false) without checking whether the filesystem is
frozen, then calls statfs_by_dentry(s->s_root, sbuf) -> ext4_statfs().
When the root directory has EXT4_INODE_PROJINHERIT set and project quota
limits are enabled, ext4_statfs() calls ext4_statfs_project() -> dqget().
If the root directory's project dquot is not active in cache, dqget()
invokes ext4_acquire_dquot() with no running transaction:

CPU0                              CPU1
----                              ----
down_read(&s->s_umount)
                                  down_write(&sb->s_umount)
sb_start_intwrite(dquot->dq_sb)
                                  sb_freeze_unlock(sb, SB_FREEZE_FS)

 *** DEADLOCK ***

CPU0: vfs_ustat() -> ext4_statfs() -> ext4_statfs_project() -> dqget() ->
      ext4_acquire_dquot()
CPU1: thaw_super() -> super_lock_excl() -> thaw_super_locked()

Because CPU0 blocks on SB_FREEZE_FS while holding s_umount for reading,
thaw_super() cannot acquire s_umount exclusively to unfreeze the
filesystem.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125111055.3672233-1-yebin@huaweicloud.com?part=2

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

* Re: [PATCH v2 0/2] ext4: protect quota acquisition against filesystem freezing
  2026-11-25 11:10 [PATCH v2 0/2] ext4: protect quota acquisition against filesystem freezing Ye Bin
  2026-11-25 11:10 ` [PATCH v2 1/2] quota: Forbid Q_XGETQUOTA and Q_XGETNEXTQUOTA for frozen filesystem Ye Bin
  2026-11-25 11:10 ` [PATCH v2 2/2] ext4: protect ext4_acquire_dquot against freezing Ye Bin
@ 2026-11-25 11:30 ` sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-25 11:30 UTC (permalink / raw)
  To: Ye Bin, linux-ext4; +Cc: tytso

Sashiko has finished reviewing this 2-patch series:

- Patch 1: no issues found.
- Patch 2 ("ext4: protect ext4_acquire_dquot against freezing"): 2
  potential issues (2 High)

A detailed review has been sent as a separate reply to patch 2.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125111055.3672233-1-yebin@huaweicloud.com

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

end of thread, other threads:[~2026-11-25 11:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 11:10 [PATCH v2 0/2] ext4: protect quota acquisition against filesystem freezing Ye Bin
2026-11-25 11:10 ` [PATCH v2 1/2] quota: Forbid Q_XGETQUOTA and Q_XGETNEXTQUOTA for frozen filesystem Ye Bin
2026-11-25 11:10 ` [PATCH v2 2/2] ext4: protect ext4_acquire_dquot against freezing Ye Bin
2026-11-25 11:30   ` sashiko-bot
2026-11-25 11:30 ` [PATCH v2 0/2] ext4: protect quota acquisition against filesystem freezing sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox