Skip to content

Commit

Permalink
replace checking for ->read/->aio_read presence with check in ->f_mode
Browse files Browse the repository at this point in the history
Since we are about to introduce new methods (read_iter/write_iter), the
tests in a bunch of places would have to grow inconveniently.  Check
once (at open() time) and store results in ->f_mode as FMODE_CAN_READ
and FMODE_CAN_WRITE resp.  It might end up being a temporary measure -
once everything switches from ->aio_{read,write} to ->{read,write}_iter
it might make sense to return to open-coded checks.  We'll see...

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
  • Loading branch information
Al Viro committed May 6, 2014
1 parent b318891 commit 7f7f25e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions drivers/mtd/nand/nandsim.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,12 @@ static int alloc_device(struct nandsim *ns)
cfile = filp_open(cache_file, O_CREAT | O_RDWR | O_LARGEFILE, 0600);
if (IS_ERR(cfile))
return PTR_ERR(cfile);
if (!cfile->f_op->read && !cfile->f_op->aio_read) {
if (!(cfile->f_mode & FMODE_CAN_READ)) {
NS_ERR("alloc_device: cache file not readable\n");
err = -EINVAL;
goto err_close;
}
if (!cfile->f_op->write && !cfile->f_op->aio_write) {
if (!(cfile->f_mode & FMODE_CAN_WRITE)) {
NS_ERR("alloc_device: cache file not writeable\n");
err = -EINVAL;
goto err_close;
Expand Down
4 changes: 2 additions & 2 deletions drivers/usb/gadget/storage_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
* If we can't read the file, it's no good.
* If we can't write the file, use it read-only.
*/
if (!(filp->f_op->read || filp->f_op->aio_read)) {
if (!(filp->f_mode & FMODE_CAN_READ)) {
LINFO(curlun, "file not readable: %s\n", filename);
goto out;
}
if (!(filp->f_op->write || filp->f_op->aio_write))
if (!(filp->f_mode & FMODE_CAN_WRITE))
ro = 1;

size = i_size_read(inode->i_mapping->host);
Expand Down
4 changes: 4 additions & 0 deletions fs/file_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ struct file *alloc_file(struct path *path, fmode_t mode,
file->f_path = *path;
file->f_inode = path->dentry->d_inode;
file->f_mapping = path->dentry->d_inode->i_mapping;
if ((mode & FMODE_READ) && likely(fop->read || fop->aio_read))
mode |= FMODE_CAN_READ;
if ((mode & FMODE_WRITE) && likely(fop->write || fop->aio_write))
mode |= FMODE_CAN_WRITE;
file->f_mode = mode;
file->f_op = fop;
if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
Expand Down
4 changes: 4 additions & 0 deletions fs/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,10 @@ static int do_dentry_open(struct file *f,
}
if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
i_readcount_inc(inode);
if ((f->f_mode & FMODE_READ) && likely(f->f_op->read || f->f_op->aio_read))
f->f_mode |= FMODE_CAN_READ;
if ((f->f_mode & FMODE_WRITE) && likely(f->f_op->write || f->f_op->aio_write))
f->f_mode |= FMODE_CAN_WRITE;

f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);

Expand Down
14 changes: 7 additions & 7 deletions fs/read_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)

if (!(file->f_mode & FMODE_READ))
return -EBADF;
if (!file->f_op->read && !file->f_op->aio_read)
if (!(file->f_mode & FMODE_CAN_READ))
return -EINVAL;
if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
return -EFAULT;
Expand Down Expand Up @@ -445,7 +445,7 @@ ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t
const char __user *p;
ssize_t ret;

if (!file->f_op->write && !file->f_op->aio_write)
if (!(file->f_mode & FMODE_CAN_WRITE))
return -EINVAL;

old_fs = get_fs();
Expand All @@ -472,7 +472,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_

if (!(file->f_mode & FMODE_WRITE))
return -EBADF;
if (!file->f_op->write && !file->f_op->aio_write)
if (!(file->f_mode & FMODE_CAN_WRITE))
return -EINVAL;
if (unlikely(!access_ok(VERIFY_READ, buf, count)))
return -EFAULT;
Expand Down Expand Up @@ -785,7 +785,7 @@ ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
{
if (!(file->f_mode & FMODE_READ))
return -EBADF;
if (!file->f_op->aio_read && !file->f_op->read)
if (!(file->f_mode & FMODE_CAN_READ))
return -EINVAL;

return do_readv_writev(READ, file, vec, vlen, pos);
Expand All @@ -798,7 +798,7 @@ ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
{
if (!(file->f_mode & FMODE_WRITE))
return -EBADF;
if (!file->f_op->aio_write && !file->f_op->write)
if (!(file->f_mode & FMODE_CAN_WRITE))
return -EINVAL;

return do_readv_writev(WRITE, file, vec, vlen, pos);
Expand Down Expand Up @@ -964,7 +964,7 @@ static size_t compat_readv(struct file *file,
goto out;

ret = -EINVAL;
if (!file->f_op->aio_read && !file->f_op->read)
if (!(file->f_mode & FMODE_CAN_READ))
goto out;

ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
Expand Down Expand Up @@ -1041,7 +1041,7 @@ static size_t compat_writev(struct file *file,
goto out;

ret = -EINVAL;
if (!file->f_op->aio_write && !file->f_op->write)
if (!(file->f_mode & FMODE_CAN_WRITE))
goto out;

ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
Expand Down
4 changes: 4 additions & 0 deletions include/linux/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ typedef void (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
#define FMODE_ATOMIC_POS ((__force fmode_t)0x8000)
/* Write access to underlying fs */
#define FMODE_WRITER ((__force fmode_t)0x10000)
/* Has read method(s) */
#define FMODE_CAN_READ ((__force fmode_t)0x20000)
/* Has write method(s) */
#define FMODE_CAN_WRITE ((__force fmode_t)0x40000)

/* File was opened by fanotify and shouldn't generate fanotify events */
#define FMODE_NONOTIFY ((__force fmode_t)0x1000000)
Expand Down

0 comments on commit 7f7f25e

Please sign in to comment.