linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Qinghua Jin <qhjin.dev@gmail.com>
To: unlisted-recipients:; (no To-header on input)
Cc: qhjin.dev@gmail.com, Colin Ian King <colin.king@canonical.com>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2] vfs: fix bug when opening a file with O_DIRECT on a file system that does not support it will leave an empty file
Date: Tue,  4 Jan 2022 17:42:17 +0800	[thread overview]
Message-ID: <20220104094217.99187-1-qhjin.dev@gmail.com> (raw)

Colin Ian King reported the following

1. create a minix file system and mount it
2. open a file on the file system with O_RDWR | O_CREAT | O_TRUNC | O_DIRECT
3. open fails with -EINVAL but leaves an empty file behind.  All other open() failures don't leave the
failed open files behind.

The reason is because when checking the O_DIRECT in do_dentry_open, the inode has created, and later err
processing can't remove the inode.

The patch will remove the file in last step of open in do_open().

Signed-off-by: Qinghua Jin <qhjin.dev@gmail.com>
Reported-by:  Colin Ian King <colin.king@canonical.com>
---
 fs/namei.c | 6 ++++++
 fs/open.c  | 6 ------
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/fs/namei.c b/fs/namei.c
index 1f9d2187c765..081feb804154 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -3426,6 +3426,12 @@ static int do_open(struct nameidata *nd,
 		error = vfs_open(&nd->path, file);
 	if (!error)
 		error = ima_file_check(file, op->acc_mode);
+	if (!error && (file->f_flags & O_DIRECT)) {
+		if (!file->f_mapping->a_ops || !file->f_mapping->a_ops->direct_IO) {
+			do_unlinkat(AT_FDCWD, getname_kernel(nd->name->name));
+			return -EINVAL;
+		}
+	}
 	if (!error && do_truncate)
 		error = handle_truncate(mnt_userns, file);
 	if (unlikely(error > 0)) {
diff --git a/fs/open.c b/fs/open.c
index f732fb94600c..2829c3613c0f 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -838,12 +838,6 @@ static int do_dentry_open(struct file *f,
 
 	file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
 
-	/* NB: we're sure to have correct a_ops only after f_op->open */
-	if (f->f_flags & O_DIRECT) {
-		if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
-			return -EINVAL;
-	}
-
 	/*
 	 * XXX: Huge page cache doesn't support writing yet. Drop all page
 	 * cache for this file before processing writes.
-- 
2.30.2


             reply	other threads:[~2022-01-04  9:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-04  9:42 Qinghua Jin [this message]
     [not found] ` <YdRdBcUAp7Mgp4pV@zeniv-ca.linux.org.uk>
     [not found]   ` <CACL7WENRAkZKo5sx5HvRJ_e9KXmM-SyOutb8BAthJzfH1b2vDA@mail.gmail.com>
2022-01-04 21:40     ` [PATCH v2] vfs: fix bug when opening a file with O_DIRECT on a file system that does not support it will leave an empty file Al Viro
2022-01-06 13:52 ` [vfs] bf75e8b684: WARNING:possible_recursive_locking_detected kernel test robot

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=20220104094217.99187-1-qhjin.dev@gmail.com \
    --to=qhjin.dev@gmail.com \
    --cc=colin.king@canonical.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@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;
as well as URLs for NNTP newsgroup(s).