From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from 22.17.110.36.static.bjtelecom.net ([36.110.17.22]:28724 "EHLO BJEXMBX012.didichuxing.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1751177AbdH1NHi (ORCPT ); Mon, 28 Aug 2017 09:07:38 -0400 Date: Mon, 28 Aug 2017 21:07:27 +0800 From: weiping zhang To: CC: Subject: [PATCH RFC v3] fs: Avoid create file when open() with O_DIRECT if fs not support Message-ID: <20170828130719.GA11360@localhost.didichuxing.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline Sender: linux-fsdevel-owner@vger.kernel.org List-ID: 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 --- 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