From: Ye Bin 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 Signed-off-by: Ye Bin Reviewed-by: Jan Kara --- 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 b021cbeb5f3a..0134f1c7668e 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -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; } -- 2.34.1