linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: weiping zhang <zhangweiping@didichuxing.com>
To: <viro@zeniv.linux.org.uk>
Cc: <linux-fsdevel@vger.kernel.org>
Subject: [PATCH RFC v3] fs: Avoid create file when open() with O_DIRECT if fs not support
Date: Mon, 28 Aug 2017 21:07:27 +0800	[thread overview]
Message-ID: <20170828130719.GA11360@localhost.didichuxing.com> (raw)

Some filesystems not support O_DIRECT, when user open with O_DIRECT ,
an errno -EINVAL return to userspace by open_check_o_direct, but the
file has been created, it's a strange thing. Add a checking for that
can avoid creating file if fs not support O_DIRECT.

Signed-off-by: weiping zhang <zhangweiping@didichuxing.com>
---
 fs/internal.h |  1 +
 fs/namei.c    |  9 +++++++++
 fs/open.c     | 11 +++++++++++
 3 files changed, 21 insertions(+)

diff --git a/fs/internal.h b/fs/internal.h
index 9676fe1..6393d05 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -109,6 +109,7 @@ extern struct file *do_file_open_root(struct dentry *, struct vfsmount *,
 		const char *, const struct open_flags *);
 
 extern int open_check_o_direct(struct file *f);
+extern bool open_create_check_o_direct(struct dentry *dentry);
 extern int vfs_open(const struct path *, struct file *, const struct cred *);
 extern struct file *filp_clone_open(struct file *);
 
diff --git a/fs/namei.c b/fs/namei.c
index ddb6a7c..f5a6f81 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -3201,6 +3201,15 @@ static int lookup_open(struct nameidata *nd, struct path *path,
 						open_flag & O_EXCL);
 		if (error)
 			goto out_dput;
+		/* if fs not support direct io, delete this inode */
+		if (unlikely(open_flag & O_DIRECT) &&
+			!(open_create_check_o_direct(dentry)) &&
+			dir_inode->i_op->unlink) {
+			error = dir_inode->i_op->unlink(dir_inode, dentry);
+			if (error == 0)
+				error = -EINVAL;
+			goto out_dput;
+		}
 		fsnotify_create(dir_inode, dentry);
 	}
 	if (unlikely(create_error) && !dentry->d_inode) {
diff --git a/fs/open.c b/fs/open.c
index 35bb784..4969502 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -692,6 +692,17 @@ int open_check_o_direct(struct file *f)
 	return 0;
 }
 
+bool open_create_check_o_direct(struct dentry *dentry)
+{
+	struct inode *inode = dentry->d_inode;
+
+	if (inode->i_mapping && inode->i_mapping->a_ops &&
+		inode->i_mapping->a_ops->direct_IO)
+		return true;
+
+	return false;
+}
+
 static int do_dentry_open(struct file *f,
 			  struct inode *inode,
 			  int (*open)(struct inode *, struct file *),
-- 
2.9.4

             reply	other threads:[~2017-08-28 13:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-28 13:07 weiping zhang [this message]
2017-09-01  1:36 ` [PATCH RFC v3] fs: Avoid create file when open() with O_DIRECT if fs not support weiping zhang

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=20170828130719.GA11360@localhost.didichuxing.com \
    --to=zhangweiping@didichuxing.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;
as well as URLs for NNTP newsgroup(s).