fs: add vfs_iter_{read,write} helpers
authorChristoph Hellwig <hch@lst.de>
Sun, 25 Jan 2015 20:11:59 +0000 (21:11 +0100)
committerAl Viro <viro@zeniv.linux.org.uk>
Thu, 29 Jan 2015 05:13:13 +0000 (00:13 -0500)
Simple helpers that pass an arbitrary iov_iter to filesystems.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
fs/read_write.c
fs/splice.c
include/linux/fs.h

index c0805c93b6fa18d34b9b579d5c470af2a5a66b1b..ab4f26a7d5cb55dfc1bee45f2cb2b51755c55814 100644 (file)
@@ -333,6 +333,52 @@ out_putf:
 }
 #endif
 
+ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
+{
+       struct kiocb kiocb;
+       ssize_t ret;
+
+       if (!file->f_op->read_iter)
+               return -EINVAL;
+
+       init_sync_kiocb(&kiocb, file);
+       kiocb.ki_pos = *ppos;
+       kiocb.ki_nbytes = iov_iter_count(iter);
+
+       iter->type |= READ;
+       ret = file->f_op->read_iter(&kiocb, iter);
+       if (ret == -EIOCBQUEUED)
+               ret = wait_on_sync_kiocb(&kiocb);
+
+       if (ret > 0)
+               *ppos = kiocb.ki_pos;
+       return ret;
+}
+EXPORT_SYMBOL(vfs_iter_read);
+
+ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
+{
+       struct kiocb kiocb;
+       ssize_t ret;
+
+       if (!file->f_op->write_iter)
+               return -EINVAL;
+
+       init_sync_kiocb(&kiocb, file);
+       kiocb.ki_pos = *ppos;
+       kiocb.ki_nbytes = iov_iter_count(iter);
+
+       iter->type |= WRITE;
+       ret = file->f_op->write_iter(&kiocb, iter);
+       if (ret == -EIOCBQUEUED)
+               ret = wait_on_sync_kiocb(&kiocb);
+
+       if (ret > 0)
+               *ppos = kiocb.ki_pos;
+       return ret;
+}
+EXPORT_SYMBOL(vfs_iter_write);
+
 /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others
index 7c7176f2d1e7242464a179aa3403e005cd453f2d..7968da96bebbb5d1cd087cbfa2ece65c09cc8b4a 100644 (file)
@@ -961,7 +961,6 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
        splice_from_pipe_begin(&sd);
        while (sd.total_len) {
                struct iov_iter from;
-               struct kiocb kiocb;
                size_t left;
                int n, idx;
 
@@ -1005,26 +1004,15 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
                        left -= this_len;
                }
 
-               /* ... iov_iter */
                iov_iter_bvec(&from, ITER_BVEC | WRITE, array, n,
                              sd.total_len - left);
-
-               /* ... and iocb */
-               init_sync_kiocb(&kiocb, out);
-               kiocb.ki_pos = sd.pos;
-               kiocb.ki_nbytes = sd.total_len - left;
-
-               /* now, send it */
-               ret = out->f_op->write_iter(&kiocb, &from);
-               if (-EIOCBQUEUED == ret)
-                       ret = wait_on_sync_kiocb(&kiocb);
-
+               ret = vfs_iter_write(out, &from, &sd.pos);
                if (ret <= 0)
                        break;
 
                sd.num_spliced += ret;
                sd.total_len -= ret;
-               *ppos = sd.pos = kiocb.ki_pos;
+               *ppos = sd.pos;
 
                /* dismiss the fully eaten buffers, adjust the partial one */
                while (ret) {
index 42efe13077b6c1b8dd139c6cea7a241e5d6b320d..1f3c4395254093aaf57ddb14e33f8c289d6add3e 100644 (file)
@@ -2494,6 +2494,9 @@ extern ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t l
 extern ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos);
 extern ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos);
 
+ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos);
+ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos);
+
 /* fs/block_dev.c */
 extern ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to);
 extern ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from);