ext4: protect quota acquisition against filesystem freezing
Subsystems (3)
v3 cover ext4: protect quota acquisition against filesystem freezing
From: Ye Bin <[email protected]>
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.
Changes in v3:
- Rework the quota patch to simply forbid Q_XGETQUOTA/Q_XGETNEXTQUOTA on a frozen filesystem like Q_GETQUOTA/Q_GETNEXTQUOTA, as suggested by Jan Kara, instead of only waiting for the filesystem to thaw.
- Document in the ext4 patch that it depends on the preceding quota patch.
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(-)
Signature · 1 line
2.34.1
PATCH v3 1/2 quota: Forbid Q_XGETQUOTA and Q_XGETNEXTQUOTA for frozen filesystem
From: Ye Bin <[email protected]>
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.
--- fs/quota/quota.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
fs/quota/quota.c +4 -5
@@ -767,9 +767,10 @@ static int quota_rmxquota(struct super_block *sb, void __user *addr)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 :
@@ -777,8 +778,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 ; }
Signature · 1 line
2.34.1
PATCH v3 2/2 ext4: protect ext4_acquire_dquot against freezing
In reply to Ye BinFrom: Ye Bin <[email protected]>
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.
--- fs/ext4/super.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
fs/ext4/super.c +19 -1
@@ -7177,11 +7177,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,
@@ -7190,6 +7204,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; }
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v3 2/2] ext4: protect ext4_acquire_dquot against freezing
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] ext4: s_umount vs SB_FREEZE_FS deadlock in ext4_acquire_dquot() via vfs_ustat() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] ext4: s_umount vs SB_FREEZE_FS deadlock in ext4_acquire_dquot() via vfs_ustat()
--
commit 5d68ea2752fdd861a0ff07e25a26ec6f31917f7a
Author: Ye Bin <[email protected]>
ext4: protect ext4_acquire_dquot against freezing
This commit takes sb_start_intwrite() in ext4_acquire_dquot() when no transaction is running so that quota acquisition during file open does not start a transaction on a frozen filesystem.
Quoted 9 lines
fs/ext4/super.c +1
-0@@ -7168,11 +7168,25 @@ static int ext4_acquire_dquot(struct dquot *dquot) {int ret, err;handle_t *handle; +bool freeze_protected = false;
fs/ext4/super.c static int ext4_acquire_dquot(struct dquot *dquot) · line 7181
Quoted 10 lines
fs/ext4/super.c +10
-0+ +/* +* 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 this cause an ABBA deadlock between sb->s_umount and SB_FREEZE_FS when ustat(2) runs on a frozen filesystem?
Commit "quota: Forbid Q_XGETQUOTA and Q_XGETNEXTQUOTA for frozen filesystem" blocks quotactl from reaching ext4_acquire_dquot() on a frozen superblock, but vfs_ustat() also acquires s_umount for reading via user_get_super() without checking whether the filesystem is frozen:
fs/statfs.c:vfs_ustat() {
struct super_block *s = user_get_super(dev, false);
int err;
if (!s)
return -EINVAL;
err = statfs_by_dentry(s->s_root, sbuf);
drop_super(s);
return err;
}When project quota limits are enabled and the root directory inode has EXT4_INODE_PROJINHERIT set, ext4_statfs() calls ext4_statfs_project() -> dqget(). Because the root inode is a directory, its project dquot is not kept attached in i_dquot[PRJQUOTA], so if no other inode holds an active reference to that project dquot when the filesystem is frozen, dqget() invokes ext4_acquire_dquot() with no running transaction:
CPU0 CPU1
---- ----
freeze_super()
/* freezes at SB_FREEZE_COMPLETE */
vfs_ustat()
user_get_super()
down_read(&sb->s_umount)
...
ext4_acquire_dquot()
sb_start_intwrite(sb)
thaw_super()
super_lock_excl(sb)
down_write(&sb->s_umount)*** DEADLOCK ***
CPU0: vfs_ustat() -> statfs_by_dentry() -> ext4_statfs() ->
ext4_statfs_project() -> dqget() -> ext4_acquire_dquot()
CPU1: thaw_super() -> super_lock_excl()
CPU0 blocks in sb_start_intwrite() waiting for SB_FREEZE_FS while holding s_umount for reading, and thaw_super() cannot unlock SB_FREEZE_FS until it acquires s_umount exclusively, leaving the filesystem permanently frozen.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewSashiko has finished reviewing this 2-patch series:
Sashiko has finished reviewing this 2-patch series:
- Patch 1: no issues found.
- Patch 2 ("ext4: protect ext4_acquire_dquot against freezing"): 1 potential issue (1 High)
A detailed review has been sent as a separate reply to patch 2.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]