Skip to content

Commit 7f7f25e

Browse files
author
Al Viro
committed
replace checking for ->read/->aio_read presence with check in ->f_mode
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>
1 parent b318891 commit 7f7f25e

6 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎drivers/mtd/nand/nandsim.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,12 @@ static int alloc_device(struct nandsim *ns)
575575
cfile = filp_open(cache_file, O_CREAT | O_RDWR | O_LARGEFILE, 0600);
576576
if (IS_ERR(cfile))
577577
return PTR_ERR(cfile);
578-
if (!cfile->f_op->read && !cfile->f_op->aio_read) {
578+
if (!(cfile->f_mode & FMODE_CAN_READ)) {
579579
NS_ERR("alloc_device: cache file not readable\n");
580580
err = -EINVAL;
581581
goto err_close;
582582
}
583-
if (!cfile->f_op->write && !cfile->f_op->aio_write) {
583+
if (!(cfile->f_mode & FMODE_CAN_WRITE)) {
584584
NS_ERR("alloc_device: cache file not writeable\n");
585585
err = -EINVAL;
586586
goto err_close;

‎drivers/usb/gadget/storage_common.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
220220
* If we can't read the file, it's no good.
221221
* If we can't write the file, use it read-only.
222222
*/
223-
if (!(filp->f_op->read || filp->f_op->aio_read)) {
223+
if (!(filp->f_mode & FMODE_CAN_READ)) {
224224
LINFO(curlun, "file not readable: %s\n", filename);
225225
goto out;
226226
}
227-
if (!(filp->f_op->write || filp->f_op->aio_write))
227+
if (!(filp->f_mode & FMODE_CAN_WRITE))
228228
ro = 1;
229229

230230
size = i_size_read(inode->i_mapping->host);

‎fs/file_table.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ struct file *alloc_file(struct path *path, fmode_t mode,
175175
file->f_path = *path;
176176
file->f_inode = path->dentry->d_inode;
177177
file->f_mapping = path->dentry->d_inode->i_mapping;
178+
if ((mode & FMODE_READ) && likely(fop->read || fop->aio_read))
179+
mode |= FMODE_CAN_READ;
180+
if ((mode & FMODE_WRITE) && likely(fop->write || fop->aio_write))
181+
mode |= FMODE_CAN_WRITE;
178182
file->f_mode = mode;
179183
file->f_op = fop;
180184
if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)

‎fs/open.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,10 @@ static int do_dentry_open(struct file *f,
725725
}
726726
if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
727727
i_readcount_inc(inode);
728+
if ((f->f_mode & FMODE_READ) && likely(f->f_op->read || f->f_op->aio_read))
729+
f->f_mode |= FMODE_CAN_READ;
730+
if ((f->f_mode & FMODE_WRITE) && likely(f->f_op->write || f->f_op->aio_write))
731+
f->f_mode |= FMODE_CAN_WRITE;
728732

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

‎fs/read_write.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
396396

397397
if (!(file->f_mode & FMODE_READ))
398398
return -EBADF;
399-
if (!file->f_op->read && !file->f_op->aio_read)
399+
if (!(file->f_mode & FMODE_CAN_READ))
400400
return -EINVAL;
401401
if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
402402
return -EFAULT;
@@ -445,7 +445,7 @@ ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t
445445
const char __user *p;
446446
ssize_t ret;
447447

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

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

473473
if (!(file->f_mode & FMODE_WRITE))
474474
return -EBADF;
475-
if (!file->f_op->write && !file->f_op->aio_write)
475+
if (!(file->f_mode & FMODE_CAN_WRITE))
476476
return -EINVAL;
477477
if (unlikely(!access_ok(VERIFY_READ, buf, count)))
478478
return -EFAULT;
@@ -785,7 +785,7 @@ ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
785785
{
786786
if (!(file->f_mode & FMODE_READ))
787787
return -EBADF;
788-
if (!file->f_op->aio_read && !file->f_op->read)
788+
if (!(file->f_mode & FMODE_CAN_READ))
789789
return -EINVAL;
790790

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

804804
return do_readv_writev(WRITE, file, vec, vlen, pos);
@@ -964,7 +964,7 @@ static size_t compat_readv(struct file *file,
964964
goto out;
965965

966966
ret = -EINVAL;
967-
if (!file->f_op->aio_read && !file->f_op->read)
967+
if (!(file->f_mode & FMODE_CAN_READ))
968968
goto out;
969969

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

10431043
ret = -EINVAL;
1044-
if (!file->f_op->aio_write && !file->f_op->write)
1044+
if (!(file->f_mode & FMODE_CAN_WRITE))
10451045
goto out;
10461046

10471047
ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);

‎include/linux/fs.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ typedef void (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
128128
#define FMODE_ATOMIC_POS ((__force fmode_t)0x8000)
129129
/* Write access to underlying fs */
130130
#define FMODE_WRITER ((__force fmode_t)0x10000)
131+
/* Has read method(s) */
132+
#define FMODE_CAN_READ ((__force fmode_t)0x20000)
133+
/* Has write method(s) */
134+
#define FMODE_CAN_WRITE ((__force fmode_t)0x40000)
131135

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

0 commit comments

Comments
 (0)