Linux filesystem development
 help / color / mirror / Atom feed
From: yebin <yebin@huaweicloud.com>
To: Josef Bacik <josef@toxicpanda.com>
Cc: viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz,
	linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH] fs: hold freeze protection across writable regular file ->open()
Date: Mon, 16 Nov 2026 15:11:10 +0800	[thread overview]
Message-ID: <6AFAAC8E.5090702@huaweicloud.com> (raw)
In-Reply-To: <sxlubqvhlkeg4ukyxakquv4e72o4ib7u2o2jtak4qqs6gz5haw@uoty23uvtlf3>



On 2026/11/13 1:56, Josef Bacik wrote:
> On Thu, Nov 12, 2026 at 09:52:45PM +0800, Ye Bin wrote:
>> 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.
>> ->open() can nevertheless write to the filesystem: ext4_file_open() ->
>> dquot_file_open() -> dquot_initialize() may acquire a dquot and write
>> the quota file, which starts a jbd2 transaction.  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.
>>
>> The hole was created over time:
>>
>>    - Commit 907f4554e252 ("dquot: move dquot initialization responsibility
>>      into the filesystem") moved quota initialization into ->open() and
>>      introduced dquot_file_open(), making ->open() a potentially writing
>>      operation for writable opens.
>>
>>    - Commit eb04c28288bb ("fs: Add freezing handling to mnt_want_write() /
>>      mnt_drop_write()") added sb_start_write() to mnt_want_write() but
>>      deliberately kept the write reference held by an open file on the
>>      non-freezing __mnt_want_write() variant, so the file's own write
>>      reference does not protect its ->open().
>>
>>    - Commit 64894cf84327 ("simplify lookup_open()/atomic_open() - do the
>>      temporary mnt_want_write() early") moved the temporary write count
>>      into do_last() and dropped it before finish_open(), so a writable
>>      open that is not O_TRUNC reaches ->open() with no sb write count at
>>      all.
>>
>> Since then only O_TRUNC opens take mnt_want_write() in do_open().
>>
>> Fix this by taking mnt_want_write() for any writable open of a regular
>> file and holding it across vfs_open()/->open().  Regular files are
>> checked explicitly so that opening device nodes on a read-only mount is
>> not newly rejected.  Read-only opens are unaffected.
>>
>> Fixes: eb04c28288bb ("fs: Add freezing handling to mnt_want_write() / mnt_drop_write()")
>> Signed-off-by: Ye Bin <yebin10@huawei.com>
>> ---
>>   fs/namei.c | 16 ++++++++++++++--
>>   1 file changed, 14 insertions(+), 2 deletions(-)
>>
>> diff --git a/fs/namei.c b/fs/namei.c
>> index f573fd0aacaf..0ee4cf282cf5 100644
>> --- a/fs/namei.c
>> +++ b/fs/namei.c
>> @@ -4906,6 +4906,7 @@ static int do_open(struct nameidata *nd,
>>   	const struct mnt_idmap *idmap;
>>   	int open_flag = op->open_flag;
>>   	bool do_truncate;
>> +	bool got_write;
>>   	int acc_mode;
>>   	int error;
>>
>> @@ -4935,12 +4936,23 @@ static int do_open(struct nameidata *nd,
>>   		return -ENOTDIR;
>>
>>   	do_truncate = false;
>> +	got_write = false;
>>   	acc_mode = op->acc_mode;
>>   	if (file->f_mode & FMODE_CREATED) {
>>   		/* Don't check for write permission, don't truncate */
>>   		open_flag &= ~O_TRUNC;
>>   		acc_mode = 0;
>>   	} else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
>> +		do_truncate = true;
>> +	}
>> +
>> +	/*
>> +	 * ->open() can write to the filesystem, e.g. when it initializes
>> +	 * quota, so writable opens of regular files need freeze protection
>> +	 * as well.  O_TRUNC opens already need it for handle_truncate().
>> +	 */
>> +	if (do_truncate ||
>> +	    (d_is_reg(nd->path.dentry) && (file->f_mode & FMODE_WRITE))) {
>
> This is convoluted, you can get rid of the do_truncate flag altogether,
> just do
>
> if (d_is_reg(nd->path.dentry) &&
>      ((open_flag & O_TRUNC) || (file->f_mode & FMOD_WRITE))) {
> 	// mntwrite thing
> 	go_write = true;
> }
>
> And the delete the extra else if block. Thanks,
>
Thank you for your reply.
At first, I also wanted to remove the 'do_truncate' parameter, but this
parameter is still used later. So I decided to keep it.

do_open() :
...
          if (!error)
                  error = security_file_post_open(file, op->acc_mode);
          if (!error && do_truncate)  // The 'do_truncate' parameter is required.
                  error = handle_truncate(idmap, file);
          if (unlikely(error > 0)) {
                  WARN_ON(1);
                  error = -EINVAL;
          }
...

> Josef
>


  reply	other threads:[~2026-11-16  7:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 13:52 [PATCH] fs: hold freeze protection across writable regular file ->open() Ye Bin
2026-11-12 17:56 ` Josef Bacik
2026-11-16  7:11   ` yebin [this message]
2026-11-20 11:19 ` Christian Brauner
2026-11-20 12:56   ` Jan Kara
2026-11-21  1:19     ` yebin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6AFAAC8E.5090702@huaweicloud.com \
    --to=yebin@huaweicloud.com \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=josef@toxicpanda.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox