Commit a2ad63da authored by NeilBrown's avatar NeilBrown Committed by akpm

VFS: add FMODE_CAN_ODIRECT file flag

Currently various places test if direct IO is possible on a file by
checking for the existence of the direct_IO address space operation.
This is a poor choice, as the direct_IO operation may not be used - it is
only used if the generic_file_*_iter functions are called for direct IO
and some filesystems - particularly NFS - don't do this.

Instead, introduce a new f_mode flag: FMODE_CAN_ODIRECT and change the
various places to check this (avoiding pointer dereferences).
do_dentry_open() will set this flag if ->direct_IO is present, so
filesystems do not need to be changed.

NFS *is* changed, to set the flag explicitly and discard the direct_IO
entry in the address_space_operations for files.

Other filesystems which currently use noop_direct_IO could usefully be
changed to set this flag instead.

Link: https://lkml.kernel.org/r/164859778128.29473.15189737957277399416.stgit@noble.brownReviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarNeilBrown <neilb@suse.de>
Tested-by: default avatarDavid Howells <dhowells@redhat.com>
Tested-by: default avatarGeert Uytterhoeven <geert+renesas@glider.be>
Cc: Hugh Dickins <hughd@google.com>
Cc: Mel Gorman <mgorman@techsingularity.net>
Cc: Trond Myklebust <trond.myklebust@hammerspace.com>
Cc: Miaohe Lin <linmiaohe@huawei.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 6341a446
...@@ -186,8 +186,8 @@ static void __loop_update_dio(struct loop_device *lo, bool dio) ...@@ -186,8 +186,8 @@ static void __loop_update_dio(struct loop_device *lo, bool dio)
*/ */
if (dio) { if (dio) {
if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && if (queue_logical_block_size(lo->lo_queue) >= sb_bsize &&
!(lo->lo_offset & dio_align) && !(lo->lo_offset & dio_align) &&
mapping->a_ops->direct_IO) (file->f_mode & FMODE_CAN_ODIRECT))
use_dio = true; use_dio = true;
else else
use_dio = false; use_dio = false;
......
...@@ -56,11 +56,10 @@ static int setfl(int fd, struct file * filp, unsigned long arg) ...@@ -56,11 +56,10 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
arg |= O_NONBLOCK; arg |= O_NONBLOCK;
/* Pipe packetized mode is controlled by O_DIRECT flag */ /* Pipe packetized mode is controlled by O_DIRECT flag */
if (!S_ISFIFO(inode->i_mode) && (arg & O_DIRECT)) { if (!S_ISFIFO(inode->i_mode) &&
if (!filp->f_mapping || !filp->f_mapping->a_ops || (arg & O_DIRECT) &&
!filp->f_mapping->a_ops->direct_IO) !(filp->f_mode & FMODE_CAN_ODIRECT))
return -EINVAL; return -EINVAL;
}
if (filp->f_op->check_flags) if (filp->f_op->check_flags)
error = filp->f_op->check_flags(arg); error = filp->f_op->check_flags(arg);
......
...@@ -69,6 +69,8 @@ nfs_file_open(struct inode *inode, struct file *filp) ...@@ -69,6 +69,8 @@ nfs_file_open(struct inode *inode, struct file *filp)
return res; return res;
res = nfs_open(inode, filp); res = nfs_open(inode, filp);
if (res == 0)
filp->f_mode |= FMODE_CAN_ODIRECT;
return res; return res;
} }
...@@ -536,7 +538,6 @@ const struct address_space_operations nfs_file_aops = { ...@@ -536,7 +538,6 @@ const struct address_space_operations nfs_file_aops = {
.write_end = nfs_write_end, .write_end = nfs_write_end,
.invalidate_folio = nfs_invalidate_folio, .invalidate_folio = nfs_invalidate_folio,
.releasepage = nfs_release_page, .releasepage = nfs_release_page,
.direct_IO = nfs_direct_IO,
#ifdef CONFIG_MIGRATION #ifdef CONFIG_MIGRATION
.migratepage = nfs_migrate_page, .migratepage = nfs_migrate_page,
#endif #endif
......
...@@ -834,16 +834,15 @@ static int do_dentry_open(struct file *f, ...@@ -834,16 +834,15 @@ static int do_dentry_open(struct file *f,
if ((f->f_mode & FMODE_WRITE) && if ((f->f_mode & FMODE_WRITE) &&
likely(f->f_op->write || f->f_op->write_iter)) likely(f->f_op->write || f->f_op->write_iter))
f->f_mode |= FMODE_CAN_WRITE; f->f_mode |= FMODE_CAN_WRITE;
if (f->f_mapping->a_ops && f->f_mapping->a_ops->direct_IO)
f->f_mode |= FMODE_CAN_ODIRECT;
f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping); 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) && !(f->f_mode & FMODE_CAN_ODIRECT))
if (f->f_flags & O_DIRECT) { return -EINVAL;
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 * XXX: Huge page cache doesn't support writing yet. Drop all page
......
...@@ -82,11 +82,8 @@ static int ovl_change_flags(struct file *file, unsigned int flags) ...@@ -82,11 +82,8 @@ static int ovl_change_flags(struct file *file, unsigned int flags)
if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode)) if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
return -EPERM; return -EPERM;
if (flags & O_DIRECT) { if ((flags & O_DIRECT) && !(file->f_mode & FMODE_CAN_ODIRECT))
if (!file->f_mapping->a_ops || return -EINVAL;
!file->f_mapping->a_ops->direct_IO)
return -EINVAL;
}
if (file->f_op->check_flags) { if (file->f_op->check_flags) {
err = file->f_op->check_flags(flags); err = file->f_op->check_flags(flags);
...@@ -306,8 +303,7 @@ static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) ...@@ -306,8 +303,7 @@ static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
ret = -EINVAL; ret = -EINVAL;
if (iocb->ki_flags & IOCB_DIRECT && if (iocb->ki_flags & IOCB_DIRECT &&
(!real.file->f_mapping->a_ops || !(real.file->f_mode & FMODE_CAN_ODIRECT))
!real.file->f_mapping->a_ops->direct_IO))
goto out_fdput; goto out_fdput;
old_cred = ovl_override_creds(file_inode(file)->i_sb); old_cred = ovl_override_creds(file_inode(file)->i_sb);
...@@ -367,8 +363,7 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) ...@@ -367,8 +363,7 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
ret = -EINVAL; ret = -EINVAL;
if (iocb->ki_flags & IOCB_DIRECT && if (iocb->ki_flags & IOCB_DIRECT &&
(!real.file->f_mapping->a_ops || !(real.file->f_mode & FMODE_CAN_ODIRECT))
!real.file->f_mapping->a_ops->direct_IO))
goto out_fdput; goto out_fdput;
if (!ovl_should_sync(OVL_FS(inode->i_sb))) if (!ovl_should_sync(OVL_FS(inode->i_sb)))
......
...@@ -162,6 +162,9 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset, ...@@ -162,6 +162,9 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
/* File is stream-like */ /* File is stream-like */
#define FMODE_STREAM ((__force fmode_t)0x200000) #define FMODE_STREAM ((__force fmode_t)0x200000)
/* File supports DIRECT IO */
#define FMODE_CAN_ODIRECT ((__force fmode_t)0x400000)
/* File was opened by fanotify and shouldn't generate fanotify events */ /* File was opened by fanotify and shouldn't generate fanotify events */
#define FMODE_NONOTIFY ((__force fmode_t)0x4000000) #define FMODE_NONOTIFY ((__force fmode_t)0x4000000)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment