[PATCH RFC v3] fs: Avoid create file when open() with O_DIRECT if fs not support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxx>
---
 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




[Index of Archives]     [Linux Ext4 Filesystem]     [Union Filesystem]     [Filesystem Testing]     [Ceph Users]     [Ecryptfs]     [AutoFS]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Cachefs]     [Reiser Filesystem]     [Linux RAID]     [Samba]     [Device Mapper]     [CEPH Development]
  Powered by Linux