From: Dave Kleikamp Date: Fri, 14 Jun 2013 20:54:20 +0000 (-0500) Subject: fs: create file_readable() and file_writable() functions X-Git-Tag: next-20130919~60^2~15 X-Git-Url: https://git.karo-electronics.de/?a=commitdiff_plain;h=4d0344d9ba9c44d1988438c1388be767221d704e;p=karo-tx-linux.git fs: create file_readable() and file_writable() functions Create functions to simplify if file_ops contain either a read or aio_read op, or likewise write or aio_write. We will be adding read_iter and write_iter and don't need to be complicating the code in multiple places. Signed-off-by: Dave Kleikamp Tested-by: Sedat Dilek --- diff --git a/drivers/mtd/nand/nandsim.c b/drivers/mtd/nand/nandsim.c index bdc1d15369f8..414f5225ae8b 100644 --- a/drivers/mtd/nand/nandsim.c +++ b/drivers/mtd/nand/nandsim.c @@ -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 || (!cfile->f_op->read && !cfile->f_op->aio_read)) { + if (!file_readable(cfile)) { 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 (!file_writable(cfile)) { NS_ERR("alloc_device: cache file not writeable\n"); err = -EINVAL; goto err_close; diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c index 08a1a3210a21..cd1431d850c4 100644 --- a/drivers/usb/gadget/storage_common.c +++ b/drivers/usb/gadget/storage_common.c @@ -450,11 +450,11 @@ static 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 (!file_readable(filp)) { LINFO(curlun, "file not readable: %s\n", filename); goto out; } - if (!(filp->f_op->write || filp->f_op->aio_write)) + if (!file_writable(filp)) ro = 1; size = i_size_read(inode->i_mapping->host); diff --git a/fs/read_write.c b/fs/read_write.c index e3cd280b158c..f92f74fae268 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -384,7 +384,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 || (!file->f_op->read && !file->f_op->aio_read)) + if (!file_readable(file)) return -EINVAL; if (unlikely(!access_ok(VERIFY_WRITE, buf, count))) return -EFAULT; @@ -433,7 +433,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 || (!file->f_op->write && !file->f_op->aio_write)) + if (!file_writable(file)) return -EINVAL; old_fs = get_fs(); @@ -460,7 +460,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 || (!file->f_op->write && !file->f_op->aio_write)) + if (!file_writable(file)) return -EINVAL; if (unlikely(!access_ok(VERIFY_READ, buf, count))) return -EFAULT; @@ -778,7 +778,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 || (!file->f_op->aio_read && !file->f_op->read)) + if (!file_readable(file)) return -EINVAL; return do_readv_writev(READ, file, vec, vlen, pos); @@ -791,7 +791,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 || (!file->f_op->aio_write && !file->f_op->write)) + if (!file_writable(file)) return -EINVAL; return do_readv_writev(WRITE, file, vec, vlen, pos); @@ -965,7 +965,7 @@ static size_t compat_readv(struct file *file, goto out; ret = -EINVAL; - if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read)) + if (!file_readable(file)) goto out; ret = compat_do_readv_writev(READ, file, vec, vlen, pos); @@ -1032,7 +1032,7 @@ static size_t compat_writev(struct file *file, goto out; ret = -EINVAL; - if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write)) + if (!file_writable(file)) goto out; ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos); diff --git a/include/linux/fs.h b/include/linux/fs.h index ab4391608ecb..5ffec5e7aa18 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1650,6 +1650,16 @@ struct file_operations { int (*show_fdinfo)(struct seq_file *m, struct file *f); }; +static inline int file_readable(struct file *filp) +{ + return filp && (filp->f_op->read || filp->f_op->aio_read); +} + +static inline int file_writable(struct file *filp) +{ + return filp && (filp->f_op->write || filp->f_op->aio_write); +} + struct inode_operations { struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int); void * (*follow_link) (struct dentry *, struct nameidata *);