blob: 3ae8eee3b82ab111f1b399fae12da7a40d0cac7e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/read_write.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include <linux/slab.h>
8#include <linux/stat.h>
9#include <linux/fcntl.h>
10#include <linux/file.h>
11#include <linux/uio.h>
Robert Love0eeca282005-07-12 17:06:03 -040012#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/security.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/syscalls.h>
Linus Torvaldse28cc712006-01-04 16:20:40 -080016#include <linux/pagemap.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020017#include <linux/splice.h>
Al Viro561c6732013-02-24 10:52:26 -050018#include <linux/compat.h>
Al Viro06ae43f2013-03-20 13:19:30 -040019#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/uaccess.h>
22#include <asm/unistd.h>
23
Al Viroc0bd14af2013-05-04 15:00:54 -040024typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
25typedef ssize_t (*iov_fn_t)(struct kiocb *, const struct iovec *,
26 unsigned long, loff_t);
Al Viro293bc982014-02-11 18:37:41 -050027typedef ssize_t (*iter_fn_t)(struct kiocb *, struct iov_iter *);
Al Viroc0bd14af2013-05-04 15:00:54 -040028
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080029const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040031 .read_iter = generic_file_read_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020033 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
36EXPORT_SYMBOL(generic_ro_fops);
37
Al Virocccb5a12010-12-17 07:44:05 -050038static inline int unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070039{
Al Virocccb5a12010-12-17 07:44:05 -050040 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070041}
42
Jie Liu46a1c2c2013-06-25 12:02:13 +080043/**
44 * vfs_setpos - update the file offset for lseek
45 * @file: file structure in question
46 * @offset: file offset to seek to
47 * @maxsize: maximum file size
48 *
49 * This is a low-level filesystem helper for updating the file offset to
50 * the value specified by @offset if the given offset is valid and it is
51 * not equal to the current file offset.
52 *
53 * Return the specified offset on success and -EINVAL on invalid offset.
54 */
55loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070056{
57 if (offset < 0 && !unsigned_offsets(file))
58 return -EINVAL;
59 if (offset > maxsize)
60 return -EINVAL;
61
62 if (offset != file->f_pos) {
63 file->f_pos = offset;
64 file->f_version = 0;
65 }
66 return offset;
67}
Jie Liu46a1c2c2013-06-25 12:02:13 +080068EXPORT_SYMBOL(vfs_setpos);
Andi Kleenef3d0fd2011-09-15 16:06:48 -070069
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020070/**
Andi Kleen57604952011-09-15 16:06:50 -070071 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020072 * @file: file structure to seek on
73 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -080074 * @whence: type of seek
Eric Sandeene8b96eb2012-04-30 13:11:29 -050075 * @size: max size of this file in file system
76 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020077 *
Andi Kleen57604952011-09-15 16:06:50 -070078 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050079 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070080 *
81 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070082 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070083 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
84 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020085 */
Andi Kleen9465efc2008-06-27 11:05:24 +020086loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -080087generic_file_llseek_size(struct file *file, loff_t offset, int whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050088 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Andrew Morton965c8e52012-12-17 15:59:39 -080090 switch (whence) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020091 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050092 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020093 break;
94 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080095 /*
96 * Here we special-case the lseek(fd, 0, SEEK_CUR)
97 * position-querying operation. Avoid rewriting the "same"
98 * f_pos value back to the file because a concurrent read(),
99 * write() or lseek() might have altered it
100 */
101 if (offset == 0)
102 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700103 /*
104 * f_lock protects against read/modify/write race with other
105 * SEEK_CURs. Note that parallel writes and reads behave
106 * like SEEK_SET.
107 */
108 spin_lock(&file->f_lock);
Jie Liu46a1c2c2013-06-25 12:02:13 +0800109 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700110 spin_unlock(&file->f_lock);
111 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -0400112 case SEEK_DATA:
113 /*
114 * In the generic case the entire file is data, so as long as
115 * offset isn't at the end of the file then the offset is data.
116 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500117 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400118 return -ENXIO;
119 break;
120 case SEEK_HOLE:
121 /*
122 * There is a virtual hole at the end of the file, so as long as
123 * offset isn't i_size or larger, return i_size.
124 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500125 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400126 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500127 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400128 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200130
Jie Liu46a1c2c2013-06-25 12:02:13 +0800131 return vfs_setpos(file, offset, maxsize);
Andi Kleen57604952011-09-15 16:06:50 -0700132}
133EXPORT_SYMBOL(generic_file_llseek_size);
134
135/**
136 * generic_file_llseek - generic llseek implementation for regular files
137 * @file: file structure to seek on
138 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800139 * @whence: type of seek
Andi Kleen57604952011-09-15 16:06:50 -0700140 *
141 * This is a generic implemenation of ->llseek useable for all normal local
142 * filesystems. It just updates the file offset to the value specified by
Ming Lei546ae2d2013-04-29 15:06:07 -0700143 * @offset and @whence.
Andi Kleen57604952011-09-15 16:06:50 -0700144 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800145loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
Andi Kleen57604952011-09-15 16:06:50 -0700146{
147 struct inode *inode = file->f_mapping->host;
148
Andrew Morton965c8e52012-12-17 15:59:39 -0800149 return generic_file_llseek_size(file, offset, whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500150 inode->i_sb->s_maxbytes,
151 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
Andi Kleen9465efc2008-06-27 11:05:24 +0200153EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
jan Blunckae6afc32010-05-26 14:44:48 -0700155/**
Al Viro1bf9d142013-06-16 20:27:42 +0400156 * fixed_size_llseek - llseek implementation for fixed-sized devices
157 * @file: file structure to seek on
158 * @offset: file offset to seek to
159 * @whence: type of seek
160 * @size: size of the file
161 *
162 */
163loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
164{
165 switch (whence) {
166 case SEEK_SET: case SEEK_CUR: case SEEK_END:
167 return generic_file_llseek_size(file, offset, whence,
168 size, size);
169 default:
170 return -EINVAL;
171 }
172}
173EXPORT_SYMBOL(fixed_size_llseek);
174
175/**
jan Blunckae6afc32010-05-26 14:44:48 -0700176 * noop_llseek - No Operation Performed llseek implementation
177 * @file: file structure to seek on
178 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800179 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700180 *
181 * This is an implementation of ->llseek useable for the rare special case when
182 * userspace expects the seek to succeed but the (device) file is actually not
183 * able to perform the seek. In this case you use noop_llseek() instead of
184 * falling back to the default implementation of ->llseek.
185 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800186loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700187{
188 return file->f_pos;
189}
190EXPORT_SYMBOL(noop_llseek);
191
Andrew Morton965c8e52012-12-17 15:59:39 -0800192loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 return -ESPIPE;
195}
196EXPORT_SYMBOL(no_llseek);
197
Andrew Morton965c8e52012-12-17 15:59:39 -0800198loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Al Viro496ad9a2013-01-23 17:07:38 -0500200 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200201 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Josef Bacik982d8162011-07-18 13:21:35 -0400203 mutex_lock(&inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -0800204 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700205 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400206 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700208 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800209 if (offset == 0) {
210 retval = file->f_pos;
211 goto out;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400214 break;
215 case SEEK_DATA:
216 /*
217 * In the generic case the entire file is data, so as
218 * long as offset isn't at the end of the file then the
219 * offset is data.
220 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300221 if (offset >= inode->i_size) {
222 retval = -ENXIO;
223 goto out;
224 }
Josef Bacik982d8162011-07-18 13:21:35 -0400225 break;
226 case SEEK_HOLE:
227 /*
228 * There is a virtual hole at the end of the file, so
229 * as long as offset isn't i_size or larger, return
230 * i_size.
231 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300232 if (offset >= inode->i_size) {
233 retval = -ENXIO;
234 goto out;
235 }
Josef Bacik982d8162011-07-18 13:21:35 -0400236 offset = inode->i_size;
237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500240 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (offset != file->f_pos) {
242 file->f_pos = offset;
243 file->f_version = 0;
244 }
245 retval = offset;
246 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800247out:
Josef Bacik982d8162011-07-18 13:21:35 -0400248 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return retval;
250}
251EXPORT_SYMBOL(default_llseek);
252
Andrew Morton965c8e52012-12-17 15:59:39 -0800253loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 loff_t (*fn)(struct file *, loff_t, int);
256
257 fn = no_llseek;
258 if (file->f_mode & FMODE_LSEEK) {
Al Viro72c2d532013-09-22 16:27:52 -0400259 if (file->f_op->llseek)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 fn = file->f_op->llseek;
261 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800262 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264EXPORT_SYMBOL(vfs_llseek);
265
Linus Torvalds9c225f22014-03-03 09:36:58 -0800266static inline struct fd fdget_pos(int fd)
267{
Al Virobd2a31d2014-03-04 14:54:22 -0500268 return __to_fd(__fdget_pos(fd));
Linus Torvalds9c225f22014-03-03 09:36:58 -0800269}
270
271static inline void fdput_pos(struct fd f)
272{
273 if (f.flags & FDPUT_POS_UNLOCK)
274 mutex_unlock(&f.file->f_pos_lock);
275 fdput(f);
276}
277
Andrew Morton965c8e52012-12-17 15:59:39 -0800278SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 off_t retval;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800281 struct fd f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400282 if (!f.file)
283 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800286 if (whence <= SEEK_MAX) {
287 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 retval = res;
289 if (res != (loff_t)retval)
290 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
291 }
Linus Torvalds9c225f22014-03-03 09:36:58 -0800292 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return retval;
294}
295
Al Viro561c6732013-02-24 10:52:26 -0500296#ifdef CONFIG_COMPAT
297COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
298{
299 return sys_lseek(fd, offset, whence);
300}
301#endif
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100304SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
305 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800306 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 int retval;
Eric Biggersd7a15f82014-03-16 14:24:08 -0500309 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Al Viro2903ff02012-08-28 12:52:22 -0400312 if (!f.file)
313 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800316 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 goto out_putf;
318
Al Viro2903ff02012-08-28 12:52:22 -0400319 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800320 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 retval = (int)offset;
323 if (offset >= 0) {
324 retval = -EFAULT;
325 if (!copy_to_user(result, &offset, sizeof(offset)))
326 retval = 0;
327 }
328out_putf:
Eric Biggersd7a15f82014-03-16 14:24:08 -0500329 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return retval;
331}
332#endif
333
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100334ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
335{
336 struct kiocb kiocb;
337 ssize_t ret;
338
339 if (!file->f_op->read_iter)
340 return -EINVAL;
341
342 init_sync_kiocb(&kiocb, file);
343 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100344
345 iter->type |= READ;
346 ret = file->f_op->read_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100347 BUG_ON(ret == -EIOCBQUEUED);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100348 if (ret > 0)
349 *ppos = kiocb.ki_pos;
350 return ret;
351}
352EXPORT_SYMBOL(vfs_iter_read);
353
354ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
355{
356 struct kiocb kiocb;
357 ssize_t ret;
358
359 if (!file->f_op->write_iter)
360 return -EINVAL;
361
362 init_sync_kiocb(&kiocb, file);
363 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100364
365 iter->type |= WRITE;
366 ret = file->f_op->write_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100367 BUG_ON(ret == -EIOCBQUEUED);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100368 if (ret > 0)
369 *ppos = kiocb.ki_pos;
370 return ret;
371}
372EXPORT_SYMBOL(vfs_iter_write);
373
Linus Torvaldse28cc712006-01-04 16:20:40 -0800374/*
375 * rw_verify_area doesn't like huge counts. We limit
376 * them to something that fits in "int" so that others
377 * won't have to do range checks all the time.
378 */
Al Viro68d70d02013-06-19 15:26:04 +0400379int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct inode *inode;
382 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100383 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Al Viro496ad9a2013-01-23 17:07:38 -0500385 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800386 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100387 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500389 if (unlikely(pos < 0)) {
390 if (!unsigned_offsets(file))
391 return retval;
392 if (count >= -pos) /* both values are in 0..LLONG_MAX */
393 return -EOVERFLOW;
394 } else if (unlikely((loff_t) (pos + count) < 0)) {
395 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700396 return retval;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500399 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
James Morrisc43e2592008-01-12 22:05:48 +1100400 retval = locks_mandatory_area(
Linus Torvaldse28cc712006-01-04 16:20:40 -0800401 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
402 inode, file, pos, count);
403 if (retval < 0)
404 return retval;
405 }
James Morrisc43e2592008-01-12 22:05:48 +1100406 retval = security_file_permission(file,
407 read_write == READ ? MAY_READ : MAY_WRITE);
408 if (retval)
409 return retval;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800410 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
414{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700415 struct iovec iov = { .iov_base = buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct kiocb kiocb;
417 ssize_t ret;
418
419 init_sync_kiocb(&kiocb, filp);
420 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700421
Zach Brown41003a72013-05-07 16:18:25 -0700422 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100423 BUG_ON(ret == -EIOCBQUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 *ppos = kiocb.ki_pos;
425 return ret;
426}
427
428EXPORT_SYMBOL(do_sync_read);
429
Al Viro5d5d5682015-04-03 15:41:18 -0400430static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500431{
432 struct iovec iov = { .iov_base = buf, .iov_len = len };
433 struct kiocb kiocb;
434 struct iov_iter iter;
435 ssize_t ret;
436
437 init_sync_kiocb(&kiocb, filp);
438 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500439 iov_iter_init(&iter, READ, &iov, 1, len);
440
441 ret = filp->f_op->read_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100442 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500443 *ppos = kiocb.ki_pos;
444 return ret;
445}
446
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200447ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
448 loff_t *pos)
449{
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200450 if (file->f_op->read)
Al Viro3d04c8a2015-04-03 15:09:18 -0400451 return file->f_op->read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200452 else if (file->f_op->aio_read)
Al Viro3d04c8a2015-04-03 15:09:18 -0400453 return do_sync_read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200454 else if (file->f_op->read_iter)
Al Viro3d04c8a2015-04-03 15:09:18 -0400455 return new_sync_read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200456 else
Al Viro3d04c8a2015-04-03 15:09:18 -0400457 return -EINVAL;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200458}
Al Viro3d04c8a2015-04-03 15:09:18 -0400459EXPORT_SYMBOL(__vfs_read);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
462{
463 ssize_t ret;
464
465 if (!(file->f_mode & FMODE_READ))
466 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500467 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return -EINVAL;
469 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
470 return -EFAULT;
471
472 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800473 if (ret >= 0) {
474 count = ret;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200475 ret = __vfs_read(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100476 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500477 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100478 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
James Morrisc43e2592008-01-12 22:05:48 +1100480 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482
483 return ret;
484}
485
486EXPORT_SYMBOL(vfs_read);
487
488ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
489{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700490 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct kiocb kiocb;
492 ssize_t ret;
493
494 init_sync_kiocb(&kiocb, filp);
495 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700496
Zach Brown41003a72013-05-07 16:18:25 -0700497 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100498 BUG_ON(ret == -EIOCBQUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *ppos = kiocb.ki_pos;
500 return ret;
501}
502
503EXPORT_SYMBOL(do_sync_write);
504
Al Viro5d5d5682015-04-03 15:41:18 -0400505static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500506{
507 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
508 struct kiocb kiocb;
509 struct iov_iter iter;
510 ssize_t ret;
511
512 init_sync_kiocb(&kiocb, filp);
513 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500514 iov_iter_init(&iter, WRITE, &iov, 1, len);
515
516 ret = filp->f_op->write_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100517 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500518 *ppos = kiocb.ki_pos;
519 return ret;
520}
521
Al Viro493c84c2015-04-03 15:06:43 -0400522ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
523 loff_t *pos)
524{
525 if (file->f_op->write)
526 return file->f_op->write(file, p, count, pos);
527 else if (file->f_op->aio_write)
528 return do_sync_write(file, p, count, pos);
529 else if (file->f_op->write_iter)
530 return new_sync_write(file, p, count, pos);
531 else
532 return -EINVAL;
533}
534EXPORT_SYMBOL(__vfs_write);
535
Al Viro06ae43f2013-03-20 13:19:30 -0400536ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
537{
538 mm_segment_t old_fs;
539 const char __user *p;
540 ssize_t ret;
541
Al Viro7f7f25e2014-02-11 17:49:24 -0500542 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro3e84f482013-03-27 15:20:30 +0000543 return -EINVAL;
544
Al Viro06ae43f2013-03-20 13:19:30 -0400545 old_fs = get_fs();
546 set_fs(get_ds());
547 p = (__force const char __user *)buf;
548 if (count > MAX_RW_COUNT)
549 count = MAX_RW_COUNT;
Al Viro493c84c2015-04-03 15:06:43 -0400550 ret = __vfs_write(file, p, count, pos);
Al Viro06ae43f2013-03-20 13:19:30 -0400551 set_fs(old_fs);
552 if (ret > 0) {
553 fsnotify_modify(file);
554 add_wchar(current, ret);
555 }
556 inc_syscw(current);
557 return ret;
558}
559
Al Viro2ec3a122014-08-19 11:48:09 -0400560EXPORT_SYMBOL(__kernel_write);
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
563{
564 ssize_t ret;
565
566 if (!(file->f_mode & FMODE_WRITE))
567 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500568 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return -EINVAL;
570 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
571 return -EFAULT;
572
573 ret = rw_verify_area(WRITE, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800574 if (ret >= 0) {
575 count = ret;
Al Viro03d95eb2013-03-20 13:04:20 -0400576 file_start_write(file);
Al Viro493c84c2015-04-03 15:06:43 -0400577 ret = __vfs_write(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100578 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500579 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100580 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
James Morrisc43e2592008-01-12 22:05:48 +1100582 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400583 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
586 return ret;
587}
588
589EXPORT_SYMBOL(vfs_write);
590
591static inline loff_t file_pos_read(struct file *file)
592{
593 return file->f_pos;
594}
595
596static inline void file_pos_write(struct file *file, loff_t pos)
597{
598 file->f_pos = pos;
599}
600
Heiko Carstens3cdad422009-01-14 14:14:22 +0100601SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800603 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Al Viro2903ff02012-08-28 12:52:22 -0400606 if (f.file) {
607 loff_t pos = file_pos_read(f.file);
608 ret = vfs_read(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400609 if (ret >= 0)
610 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800611 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return ret;
614}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Heiko Carstens3cdad422009-01-14 14:14:22 +0100616SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
617 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800619 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Al Viro2903ff02012-08-28 12:52:22 -0400622 if (f.file) {
623 loff_t pos = file_pos_read(f.file);
624 ret = vfs_write(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400625 if (ret >= 0)
626 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800627 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
630 return ret;
631}
632
Al Viro4a0fd5b2013-01-21 15:16:58 -0500633SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
634 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Al Viro2903ff02012-08-28 12:52:22 -0400636 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (pos < 0)
640 return -EINVAL;
641
Al Viro2903ff02012-08-28 12:52:22 -0400642 f = fdget(fd);
643 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400645 if (f.file->f_mode & FMODE_PREAD)
646 ret = vfs_read(f.file, buf, count, &pos);
647 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 return ret;
651}
652
Al Viro4a0fd5b2013-01-21 15:16:58 -0500653SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
654 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Al Viro2903ff02012-08-28 12:52:22 -0400656 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if (pos < 0)
660 return -EINVAL;
661
Al Viro2903ff02012-08-28 12:52:22 -0400662 f = fdget(fd);
663 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400665 if (f.file->f_mode & FMODE_PWRITE)
666 ret = vfs_write(f.file, buf, count, &pos);
667 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
670 return ret;
671}
672
673/*
674 * Reduce an iovec's length in-place. Return the resulting number of segments
675 */
676unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
677{
678 unsigned long seg = 0;
679 size_t len = 0;
680
681 while (seg < nr_segs) {
682 seg++;
683 if (len + iov->iov_len >= to) {
684 iov->iov_len = to - len;
685 break;
686 }
687 len += iov->iov_len;
688 iov++;
689 }
690 return seg;
691}
Eric Sandeen19295522008-01-28 23:58:27 -0500692EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Al Viroac15ac02015-03-20 20:10:21 -0400694static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
695 loff_t *ppos, iter_fn_t fn)
Al Viro293bc982014-02-11 18:37:41 -0500696{
697 struct kiocb kiocb;
Al Viro293bc982014-02-11 18:37:41 -0500698 ssize_t ret;
699
700 init_sync_kiocb(&kiocb, filp);
701 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500702
Al Viroac15ac02015-03-20 20:10:21 -0400703 ret = fn(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100704 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500705 *ppos = kiocb.ki_pos;
706 return ret;
707}
708
Al Viroac15ac02015-03-20 20:10:21 -0400709static ssize_t do_sync_readv_writev(struct file *filp, struct iov_iter *iter,
710 loff_t *ppos, iov_fn_t fn)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700711{
712 struct kiocb kiocb;
713 ssize_t ret;
714
715 init_sync_kiocb(&kiocb, filp);
716 kiocb.ki_pos = *ppos;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700717
Al Viroac15ac02015-03-20 20:10:21 -0400718 ret = fn(&kiocb, iter->iov, iter->nr_segs, kiocb.ki_pos);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100719 BUG_ON(ret == -EIOCBQUEUED);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700720 *ppos = kiocb.ki_pos;
721 return ret;
722}
723
724/* Do it by hand, with file-ops */
Al Viroac15ac02015-03-20 20:10:21 -0400725static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
726 loff_t *ppos, io_fn_t fn)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700727{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700728 ssize_t ret = 0;
729
Al Viroac15ac02015-03-20 20:10:21 -0400730 while (iov_iter_count(iter)) {
731 struct iovec iovec = iov_iter_iovec(iter);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700732 ssize_t nr;
733
Al Viroac15ac02015-03-20 20:10:21 -0400734 nr = fn(filp, iovec.iov_base, iovec.iov_len, ppos);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700735
736 if (nr < 0) {
737 if (!ret)
738 ret = nr;
739 break;
740 }
741 ret += nr;
Al Viroac15ac02015-03-20 20:10:21 -0400742 if (nr != iovec.iov_len)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700743 break;
Al Viroac15ac02015-03-20 20:10:21 -0400744 iov_iter_advance(iter, nr);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700745 }
746
747 return ret;
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750/* A write operation does a read from user space and vice versa */
751#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
752
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700753ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
754 unsigned long nr_segs, unsigned long fast_segs,
755 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700756 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700757{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700758 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700759 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700760 struct iovec *iov = fast_pointer;
761
Linus Torvalds435f49a2010-10-29 10:36:49 -0700762 /*
763 * SuS says "The readv() function *may* fail if the iovcnt argument
764 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
765 * traditionally returned zero for zero segments, so...
766 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700767 if (nr_segs == 0) {
768 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700769 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700770 }
771
Linus Torvalds435f49a2010-10-29 10:36:49 -0700772 /*
773 * First get the "struct iovec" from user memory and
774 * verify all the pointers
775 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700776 if (nr_segs > UIO_MAXIOV) {
777 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700778 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700779 }
780 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700781 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700782 if (iov == NULL) {
783 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700784 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700785 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700786 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700787 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
788 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700789 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700790 }
791
Linus Torvalds435f49a2010-10-29 10:36:49 -0700792 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700793 * According to the Single Unix Specification we should return EINVAL
794 * if an element length is < 0 when cast to ssize_t or if the
795 * total length would overflow the ssize_t return value of the
796 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700797 *
798 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
799 * overflow case.
800 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700801 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700802 for (seg = 0; seg < nr_segs; seg++) {
803 void __user *buf = iov[seg].iov_base;
804 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700805
806 /* see if we we're about to use an invalid len or if
807 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700808 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700809 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700810 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700811 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700812 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700813 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700814 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700815 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700816 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700817 if (len > MAX_RW_COUNT - ret) {
818 len = MAX_RW_COUNT - ret;
819 iov[seg].iov_len = len;
820 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700821 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700822 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700823out:
824 *ret_pointer = iov;
825 return ret;
826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828static ssize_t do_readv_writev(int type, struct file *file,
829 const struct iovec __user * uvector,
830 unsigned long nr_segs, loff_t *pos)
831{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 size_t tot_len;
833 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700834 struct iovec *iov = iovstack;
Al Viroac15ac02015-03-20 20:10:21 -0400835 struct iov_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 io_fn_t fn;
838 iov_fn_t fnv;
Al Viro293bc982014-02-11 18:37:41 -0500839 iter_fn_t iter_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Al Viro0504c072015-03-21 19:40:11 -0400841 ret = import_iovec(type, uvector, nr_segs,
842 ARRAY_SIZE(iovstack), &iov, &iter);
843 if (ret < 0)
844 return ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700845
Al Viro0504c072015-03-21 19:40:11 -0400846 tot_len = iov_iter_count(&iter);
847 if (!tot_len)
848 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800850 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 goto out;
852
853 fnv = NULL;
854 if (type == READ) {
855 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700856 fnv = file->f_op->aio_read;
Al Viro293bc982014-02-11 18:37:41 -0500857 iter_fn = file->f_op->read_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 } else {
859 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700860 fnv = file->f_op->aio_write;
Al Viro293bc982014-02-11 18:37:41 -0500861 iter_fn = file->f_op->write_iter;
Al Viro03d95eb2013-03-20 13:04:20 -0400862 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Al Viro293bc982014-02-11 18:37:41 -0500865 if (iter_fn)
Al Viroac15ac02015-03-20 20:10:21 -0400866 ret = do_iter_readv_writev(file, &iter, pos, iter_fn);
Al Viro293bc982014-02-11 18:37:41 -0500867 else if (fnv)
Al Viroac15ac02015-03-20 20:10:21 -0400868 ret = do_sync_readv_writev(file, &iter, pos, fnv);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700869 else
Al Viroac15ac02015-03-20 20:10:21 -0400870 ret = do_loop_readv_writev(file, &iter, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Al Viro03d95eb2013-03-20 13:04:20 -0400872 if (type != READ)
873 file_end_write(file);
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875out:
Al Viro0504c072015-03-21 19:40:11 -0400876 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400877 if ((ret + (type == READ)) > 0) {
878 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500879 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400880 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500881 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
887 unsigned long vlen, loff_t *pos)
888{
889 if (!(file->f_mode & FMODE_READ))
890 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500891 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -EINVAL;
893
894 return do_readv_writev(READ, file, vec, vlen, pos);
895}
896
897EXPORT_SYMBOL(vfs_readv);
898
899ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
900 unsigned long vlen, loff_t *pos)
901{
902 if (!(file->f_mode & FMODE_WRITE))
903 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500904 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EINVAL;
906
907 return do_readv_writev(WRITE, file, vec, vlen, pos);
908}
909
910EXPORT_SYMBOL(vfs_writev);
911
Heiko Carstens3cdad422009-01-14 14:14:22 +0100912SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
913 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800915 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Al Viro2903ff02012-08-28 12:52:22 -0400918 if (f.file) {
919 loff_t pos = file_pos_read(f.file);
920 ret = vfs_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400921 if (ret >= 0)
922 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800923 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925
926 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800927 add_rchar(current, ret);
928 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return ret;
930}
931
Heiko Carstens3cdad422009-01-14 14:14:22 +0100932SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
933 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800935 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Al Viro2903ff02012-08-28 12:52:22 -0400938 if (f.file) {
939 loff_t pos = file_pos_read(f.file);
940 ret = vfs_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400941 if (ret >= 0)
942 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800943 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
946 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800947 add_wchar(current, ret);
948 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return ret;
950}
951
Linus Torvalds601cc112009-04-03 08:03:22 -0700952static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700953{
Linus Torvalds601cc112009-04-03 08:03:22 -0700954#define HALF_LONG_BITS (BITS_PER_LONG / 2)
955 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
956}
957
958SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
959 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
960{
961 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400962 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700963 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700964
965 if (pos < 0)
966 return -EINVAL;
967
Al Viro2903ff02012-08-28 12:52:22 -0400968 f = fdget(fd);
969 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700970 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400971 if (f.file->f_mode & FMODE_PREAD)
972 ret = vfs_readv(f.file, vec, vlen, &pos);
973 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700974 }
975
976 if (ret > 0)
977 add_rchar(current, ret);
978 inc_syscr(current);
979 return ret;
980}
981
982SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700983 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700984{
Linus Torvalds601cc112009-04-03 08:03:22 -0700985 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400986 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700987 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700988
989 if (pos < 0)
990 return -EINVAL;
991
Al Viro2903ff02012-08-28 12:52:22 -0400992 f = fdget(fd);
993 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700994 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400995 if (f.file->f_mode & FMODE_PWRITE)
996 ret = vfs_writev(f.file, vec, vlen, &pos);
997 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700998 }
999
1000 if (ret > 0)
1001 add_wchar(current, ret);
1002 inc_syscw(current);
1003 return ret;
1004}
1005
Al Viro72ec3512013-03-20 10:42:10 -04001006#ifdef CONFIG_COMPAT
1007
1008static ssize_t compat_do_readv_writev(int type, struct file *file,
1009 const struct compat_iovec __user *uvector,
1010 unsigned long nr_segs, loff_t *pos)
1011{
1012 compat_ssize_t tot_len;
1013 struct iovec iovstack[UIO_FASTIOV];
1014 struct iovec *iov = iovstack;
Al Viroac15ac02015-03-20 20:10:21 -04001015 struct iov_iter iter;
Al Viro72ec3512013-03-20 10:42:10 -04001016 ssize_t ret;
1017 io_fn_t fn;
1018 iov_fn_t fnv;
Al Viro293bc982014-02-11 18:37:41 -05001019 iter_fn_t iter_fn;
Al Viro72ec3512013-03-20 10:42:10 -04001020
Al Viro0504c072015-03-21 19:40:11 -04001021 ret = compat_import_iovec(type, uvector, nr_segs,
1022 UIO_FASTIOV, &iov, &iter);
1023 if (ret < 0)
1024 return ret;
Al Viro72ec3512013-03-20 10:42:10 -04001025
Al Viro0504c072015-03-21 19:40:11 -04001026 tot_len = iov_iter_count(&iter);
1027 if (!tot_len)
1028 goto out;
Al Viro72ec3512013-03-20 10:42:10 -04001029 ret = rw_verify_area(type, file, pos, tot_len);
1030 if (ret < 0)
1031 goto out;
1032
1033 fnv = NULL;
1034 if (type == READ) {
1035 fn = file->f_op->read;
1036 fnv = file->f_op->aio_read;
Al Viro293bc982014-02-11 18:37:41 -05001037 iter_fn = file->f_op->read_iter;
Al Viro72ec3512013-03-20 10:42:10 -04001038 } else {
1039 fn = (io_fn_t)file->f_op->write;
1040 fnv = file->f_op->aio_write;
Al Viro293bc982014-02-11 18:37:41 -05001041 iter_fn = file->f_op->write_iter;
Al Viro03d95eb2013-03-20 13:04:20 -04001042 file_start_write(file);
Al Viro72ec3512013-03-20 10:42:10 -04001043 }
1044
Al Viro293bc982014-02-11 18:37:41 -05001045 if (iter_fn)
Al Viroac15ac02015-03-20 20:10:21 -04001046 ret = do_iter_readv_writev(file, &iter, pos, iter_fn);
Al Viro293bc982014-02-11 18:37:41 -05001047 else if (fnv)
Al Viroac15ac02015-03-20 20:10:21 -04001048 ret = do_sync_readv_writev(file, &iter, pos, fnv);
Al Viro03d95eb2013-03-20 13:04:20 -04001049 else
Al Viroac15ac02015-03-20 20:10:21 -04001050 ret = do_loop_readv_writev(file, &iter, pos, fn);
Al Viro72ec3512013-03-20 10:42:10 -04001051
Al Viro03d95eb2013-03-20 13:04:20 -04001052 if (type != READ)
1053 file_end_write(file);
1054
Al Viro72ec3512013-03-20 10:42:10 -04001055out:
Al Viro0504c072015-03-21 19:40:11 -04001056 kfree(iov);
Al Viro72ec3512013-03-20 10:42:10 -04001057 if ((ret + (type == READ)) > 0) {
1058 if (type == READ)
1059 fsnotify_access(file);
1060 else
1061 fsnotify_modify(file);
1062 }
1063 return ret;
1064}
1065
1066static size_t compat_readv(struct file *file,
1067 const struct compat_iovec __user *vec,
1068 unsigned long vlen, loff_t *pos)
1069{
1070 ssize_t ret = -EBADF;
1071
1072 if (!(file->f_mode & FMODE_READ))
1073 goto out;
1074
1075 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001076 if (!(file->f_mode & FMODE_CAN_READ))
Al Viro72ec3512013-03-20 10:42:10 -04001077 goto out;
1078
1079 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
1080
1081out:
1082 if (ret > 0)
1083 add_rchar(current, ret);
1084 inc_syscr(current);
1085 return ret;
1086}
1087
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001088COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001089 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001090 compat_ulong_t, vlen)
Al Viro72ec3512013-03-20 10:42:10 -04001091{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001092 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001093 ssize_t ret;
1094 loff_t pos;
1095
1096 if (!f.file)
1097 return -EBADF;
1098 pos = f.file->f_pos;
1099 ret = compat_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001100 if (ret >= 0)
1101 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001102 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001103 return ret;
1104}
1105
Heiko Carstens378a10f2014-03-05 10:43:51 +01001106static long __compat_sys_preadv64(unsigned long fd,
1107 const struct compat_iovec __user *vec,
1108 unsigned long vlen, loff_t pos)
Al Viro72ec3512013-03-20 10:42:10 -04001109{
1110 struct fd f;
1111 ssize_t ret;
1112
1113 if (pos < 0)
1114 return -EINVAL;
1115 f = fdget(fd);
1116 if (!f.file)
1117 return -EBADF;
1118 ret = -ESPIPE;
1119 if (f.file->f_mode & FMODE_PREAD)
1120 ret = compat_readv(f.file, vec, vlen, &pos);
1121 fdput(f);
1122 return ret;
1123}
1124
Heiko Carstens378a10f2014-03-05 10:43:51 +01001125#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1126COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1127 const struct compat_iovec __user *,vec,
1128 unsigned long, vlen, loff_t, pos)
1129{
1130 return __compat_sys_preadv64(fd, vec, vlen, pos);
1131}
1132#endif
1133
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001134COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001135 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001136 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001137{
1138 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001139
1140 return __compat_sys_preadv64(fd, vec, vlen, pos);
Al Viro72ec3512013-03-20 10:42:10 -04001141}
1142
1143static size_t compat_writev(struct file *file,
1144 const struct compat_iovec __user *vec,
1145 unsigned long vlen, loff_t *pos)
1146{
1147 ssize_t ret = -EBADF;
1148
1149 if (!(file->f_mode & FMODE_WRITE))
1150 goto out;
1151
1152 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001153 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro72ec3512013-03-20 10:42:10 -04001154 goto out;
1155
1156 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1157
1158out:
1159 if (ret > 0)
1160 add_wchar(current, ret);
1161 inc_syscw(current);
1162 return ret;
1163}
1164
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001165COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001166 const struct compat_iovec __user *, vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001167 compat_ulong_t, vlen)
Al Viro72ec3512013-03-20 10:42:10 -04001168{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001169 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001170 ssize_t ret;
1171 loff_t pos;
1172
1173 if (!f.file)
1174 return -EBADF;
1175 pos = f.file->f_pos;
1176 ret = compat_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001177 if (ret >= 0)
1178 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001179 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001180 return ret;
1181}
1182
Heiko Carstens378a10f2014-03-05 10:43:51 +01001183static long __compat_sys_pwritev64(unsigned long fd,
1184 const struct compat_iovec __user *vec,
1185 unsigned long vlen, loff_t pos)
Al Viro72ec3512013-03-20 10:42:10 -04001186{
1187 struct fd f;
1188 ssize_t ret;
1189
1190 if (pos < 0)
1191 return -EINVAL;
1192 f = fdget(fd);
1193 if (!f.file)
1194 return -EBADF;
1195 ret = -ESPIPE;
1196 if (f.file->f_mode & FMODE_PWRITE)
1197 ret = compat_writev(f.file, vec, vlen, &pos);
1198 fdput(f);
1199 return ret;
1200}
1201
Heiko Carstens378a10f2014-03-05 10:43:51 +01001202#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1203COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1204 const struct compat_iovec __user *,vec,
1205 unsigned long, vlen, loff_t, pos)
1206{
1207 return __compat_sys_pwritev64(fd, vec, vlen, pos);
1208}
1209#endif
1210
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001211COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001212 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001213 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001214{
1215 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001216
1217 return __compat_sys_pwritev64(fd, vec, vlen, pos);
Al Viro72ec3512013-03-20 10:42:10 -04001218}
1219#endif
1220
Al Viro19f4fc32013-02-24 02:17:03 -05001221static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1222 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Al Viro2903ff02012-08-28 12:52:22 -04001224 struct fd in, out;
1225 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001227 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001229 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /*
1232 * Get input file, and verify that it is ok..
1233 */
1234 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001235 in = fdget(in_fd);
1236 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001238 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001241 if (!ppos) {
1242 pos = in.file->f_pos;
1243 } else {
1244 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001245 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001247 }
1248 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001249 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001251 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /*
1254 * Get output file, and verify that it is ok..
1255 */
1256 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001257 out = fdget(out_fd);
1258 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001260 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 goto fput_out;
1262 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001263 in_inode = file_inode(in.file);
1264 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001265 out_pos = out.file->f_pos;
1266 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001267 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001269 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (!max)
1272 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (unlikely(pos + count > max)) {
1275 retval = -EOVERFLOW;
1276 if (pos >= max)
1277 goto fput_out;
1278 count = max - pos;
1279 }
1280
Jens Axboed96e6e72007-06-11 12:18:52 +02001281 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001282#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001283 /*
1284 * We need to debate whether we can enable this or not. The
1285 * man page documents EAGAIN return for the output at least,
1286 * and the application is arguably buggy if it doesn't expect
1287 * EAGAIN on a non-blocking file descriptor.
1288 */
Al Viro2903ff02012-08-28 12:52:22 -04001289 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001290 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001291#endif
Al Viro50cd2c52013-05-23 20:10:34 -04001292 file_start_write(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001293 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
Al Viro50cd2c52013-05-23 20:10:34 -04001294 file_end_write(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001297 add_rchar(current, retval);
1298 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001299 fsnotify_access(in.file);
1300 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001301 out.file->f_pos = out_pos;
1302 if (ppos)
1303 *ppos = pos;
1304 else
1305 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001308 inc_syscr(current);
1309 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001310 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 retval = -EOVERFLOW;
1312
1313fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001314 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001316 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317out:
1318 return retval;
1319}
1320
Heiko Carstens002c8972009-01-14 14:14:18 +01001321SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 loff_t pos;
1324 off_t off;
1325 ssize_t ret;
1326
1327 if (offset) {
1328 if (unlikely(get_user(off, offset)))
1329 return -EFAULT;
1330 pos = off;
1331 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1332 if (unlikely(put_user(pos, offset)))
1333 return -EFAULT;
1334 return ret;
1335 }
1336
1337 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1338}
1339
Heiko Carstens002c8972009-01-14 14:14:18 +01001340SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 loff_t pos;
1343 ssize_t ret;
1344
1345 if (offset) {
1346 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1347 return -EFAULT;
1348 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1349 if (unlikely(put_user(pos, offset)))
1350 return -EFAULT;
1351 return ret;
1352 }
1353
1354 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1355}
Al Viro19f4fc32013-02-24 02:17:03 -05001356
1357#ifdef CONFIG_COMPAT
1358COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1359 compat_off_t __user *, offset, compat_size_t, count)
1360{
1361 loff_t pos;
1362 off_t off;
1363 ssize_t ret;
1364
1365 if (offset) {
1366 if (unlikely(get_user(off, offset)))
1367 return -EFAULT;
1368 pos = off;
1369 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1370 if (unlikely(put_user(pos, offset)))
1371 return -EFAULT;
1372 return ret;
1373 }
1374
1375 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1376}
1377
1378COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1379 compat_loff_t __user *, offset, compat_size_t, count)
1380{
1381 loff_t pos;
1382 ssize_t ret;
1383
1384 if (offset) {
1385 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1386 return -EFAULT;
1387 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1388 if (unlikely(put_user(pos, offset)))
1389 return -EFAULT;
1390 return ret;
1391 }
1392
1393 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1394}
1395#endif