blob: f646c8b565b913d5cbe02a5dafb2aba5a4b163ef [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070012#include <linux/aio.h>
Robert Love0eeca282005-07-12 17:06:03 -040013#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/security.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050015#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/syscalls.h>
Linus Torvaldse28cc712006-01-04 16:20:40 -080017#include <linux/pagemap.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020018#include <linux/splice.h>
Al Viro561c6732013-02-24 10:52:26 -050019#include <linux/compat.h>
Al Viro06ae43f2013-03-20 13:19:30 -040020#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/uaccess.h>
23#include <asm/unistd.h>
24
Al Viroc0bd14af2013-05-04 15:00:54 -040025typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
26typedef ssize_t (*iov_fn_t)(struct kiocb *, const struct iovec *,
27 unsigned long, loff_t);
28
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,
Badari Pulavarty543ade12006-09-30 23:28:48 -070031 .read = do_sync_read,
32 .aio_read = generic_file_aio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020034 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
37EXPORT_SYMBOL(generic_ro_fops);
38
Al Virocccb5a12010-12-17 07:44:05 -050039static inline int unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070040{
Al Virocccb5a12010-12-17 07:44:05 -050041 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070042}
43
Andi Kleenef3d0fd2011-09-15 16:06:48 -070044static loff_t lseek_execute(struct file *file, struct inode *inode,
45 loff_t offset, loff_t maxsize)
46{
47 if (offset < 0 && !unsigned_offsets(file))
48 return -EINVAL;
49 if (offset > maxsize)
50 return -EINVAL;
51
52 if (offset != file->f_pos) {
53 file->f_pos = offset;
54 file->f_version = 0;
55 }
56 return offset;
57}
58
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020059/**
Andi Kleen57604952011-09-15 16:06:50 -070060 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020061 * @file: file structure to seek on
62 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -080063 * @whence: type of seek
Eric Sandeene8b96eb2012-04-30 13:11:29 -050064 * @size: max size of this file in file system
65 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020066 *
Andi Kleen57604952011-09-15 16:06:50 -070067 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050068 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070069 *
70 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070071 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070072 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
73 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020074 */
Andi Kleen9465efc2008-06-27 11:05:24 +020075loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -080076generic_file_llseek_size(struct file *file, loff_t offset, int whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050077 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 struct inode *inode = file->f_mapping->host;
80
Andrew Morton965c8e52012-12-17 15:59:39 -080081 switch (whence) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020082 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050083 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020084 break;
85 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080086 /*
87 * Here we special-case the lseek(fd, 0, SEEK_CUR)
88 * position-querying operation. Avoid rewriting the "same"
89 * f_pos value back to the file because a concurrent read(),
90 * write() or lseek() might have altered it
91 */
92 if (offset == 0)
93 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -070094 /*
95 * f_lock protects against read/modify/write race with other
96 * SEEK_CURs. Note that parallel writes and reads behave
97 * like SEEK_SET.
98 */
99 spin_lock(&file->f_lock);
100 offset = lseek_execute(file, inode, file->f_pos + offset,
Andi Kleen57604952011-09-15 16:06:50 -0700101 maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700102 spin_unlock(&file->f_lock);
103 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -0400104 case SEEK_DATA:
105 /*
106 * In the generic case the entire file is data, so as long as
107 * offset isn't at the end of the file then the offset is data.
108 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500109 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400110 return -ENXIO;
111 break;
112 case SEEK_HOLE:
113 /*
114 * There is a virtual hole at the end of the file, so as long as
115 * offset isn't i_size or larger, return i_size.
116 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500117 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400118 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500119 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200122
Andi Kleen57604952011-09-15 16:06:50 -0700123 return lseek_execute(file, inode, offset, maxsize);
124}
125EXPORT_SYMBOL(generic_file_llseek_size);
126
127/**
128 * generic_file_llseek - generic llseek implementation for regular files
129 * @file: file structure to seek on
130 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800131 * @whence: type of seek
Andi Kleen57604952011-09-15 16:06:50 -0700132 *
133 * This is a generic implemenation of ->llseek useable for all normal local
134 * filesystems. It just updates the file offset to the value specified by
Ming Lei546ae2d2013-04-29 15:06:07 -0700135 * @offset and @whence.
Andi Kleen57604952011-09-15 16:06:50 -0700136 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800137loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
Andi Kleen57604952011-09-15 16:06:50 -0700138{
139 struct inode *inode = file->f_mapping->host;
140
Andrew Morton965c8e52012-12-17 15:59:39 -0800141 return generic_file_llseek_size(file, offset, whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500142 inode->i_sb->s_maxbytes,
143 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
Andi Kleen9465efc2008-06-27 11:05:24 +0200145EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
jan Blunckae6afc32010-05-26 14:44:48 -0700147/**
148 * noop_llseek - No Operation Performed llseek implementation
149 * @file: file structure to seek on
150 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800151 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700152 *
153 * This is an implementation of ->llseek useable for the rare special case when
154 * userspace expects the seek to succeed but the (device) file is actually not
155 * able to perform the seek. In this case you use noop_llseek() instead of
156 * falling back to the default implementation of ->llseek.
157 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800158loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700159{
160 return file->f_pos;
161}
162EXPORT_SYMBOL(noop_llseek);
163
Andrew Morton965c8e52012-12-17 15:59:39 -0800164loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 return -ESPIPE;
167}
168EXPORT_SYMBOL(no_llseek);
169
Andrew Morton965c8e52012-12-17 15:59:39 -0800170loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Al Viro496ad9a2013-01-23 17:07:38 -0500172 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200173 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Josef Bacik982d8162011-07-18 13:21:35 -0400175 mutex_lock(&inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -0800176 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700177 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400178 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700180 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800181 if (offset == 0) {
182 retval = file->f_pos;
183 goto out;
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400186 break;
187 case SEEK_DATA:
188 /*
189 * In the generic case the entire file is data, so as
190 * long as offset isn't at the end of the file then the
191 * offset is data.
192 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300193 if (offset >= inode->i_size) {
194 retval = -ENXIO;
195 goto out;
196 }
Josef Bacik982d8162011-07-18 13:21:35 -0400197 break;
198 case SEEK_HOLE:
199 /*
200 * There is a virtual hole at the end of the file, so
201 * as long as offset isn't i_size or larger, return
202 * i_size.
203 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300204 if (offset >= inode->i_size) {
205 retval = -ENXIO;
206 goto out;
207 }
Josef Bacik982d8162011-07-18 13:21:35 -0400208 offset = inode->i_size;
209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500212 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (offset != file->f_pos) {
214 file->f_pos = offset;
215 file->f_version = 0;
216 }
217 retval = offset;
218 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800219out:
Josef Bacik982d8162011-07-18 13:21:35 -0400220 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return retval;
222}
223EXPORT_SYMBOL(default_llseek);
224
Andrew Morton965c8e52012-12-17 15:59:39 -0800225loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 loff_t (*fn)(struct file *, loff_t, int);
228
229 fn = no_llseek;
230 if (file->f_mode & FMODE_LSEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (file->f_op && file->f_op->llseek)
232 fn = file->f_op->llseek;
233 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800234 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236EXPORT_SYMBOL(vfs_llseek);
237
Andrew Morton965c8e52012-12-17 15:59:39 -0800238SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 off_t retval;
Al Viro2903ff02012-08-28 12:52:22 -0400241 struct fd f = fdget(fd);
242 if (!f.file)
243 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800246 if (whence <= SEEK_MAX) {
247 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 retval = res;
249 if (res != (loff_t)retval)
250 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
251 }
Al Viro2903ff02012-08-28 12:52:22 -0400252 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return retval;
254}
255
Al Viro561c6732013-02-24 10:52:26 -0500256#ifdef CONFIG_COMPAT
257COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
258{
259 return sys_lseek(fd, offset, whence);
260}
261#endif
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100264SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
265 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800266 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 int retval;
Al Viro2903ff02012-08-28 12:52:22 -0400269 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Al Viro2903ff02012-08-28 12:52:22 -0400272 if (!f.file)
273 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800276 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 goto out_putf;
278
Al Viro2903ff02012-08-28 12:52:22 -0400279 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800280 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 retval = (int)offset;
283 if (offset >= 0) {
284 retval = -EFAULT;
285 if (!copy_to_user(result, &offset, sizeof(offset)))
286 retval = 0;
287 }
288out_putf:
Al Viro2903ff02012-08-28 12:52:22 -0400289 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return retval;
291}
292#endif
293
Linus Torvaldse28cc712006-01-04 16:20:40 -0800294/*
295 * rw_verify_area doesn't like huge counts. We limit
296 * them to something that fits in "int" so that others
297 * won't have to do range checks all the time.
298 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
300{
301 struct inode *inode;
302 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100303 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Al Viro496ad9a2013-01-23 17:07:38 -0500305 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800306 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100307 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500309 if (unlikely(pos < 0)) {
310 if (!unsigned_offsets(file))
311 return retval;
312 if (count >= -pos) /* both values are in 0..LLONG_MAX */
313 return -EOVERFLOW;
314 } else if (unlikely((loff_t) (pos + count) < 0)) {
315 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700316 return retval;
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Pavel Emelyanova16877c2007-10-01 14:41:11 -0700319 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
James Morrisc43e2592008-01-12 22:05:48 +1100320 retval = locks_mandatory_area(
Linus Torvaldse28cc712006-01-04 16:20:40 -0800321 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
322 inode, file, pos, count);
323 if (retval < 0)
324 return retval;
325 }
James Morrisc43e2592008-01-12 22:05:48 +1100326 retval = security_file_permission(file,
327 read_write == READ ? MAY_READ : MAY_WRITE);
328 if (retval)
329 return retval;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800330 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
334{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700335 struct iovec iov = { .iov_base = buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct kiocb kiocb;
337 ssize_t ret;
338
339 init_sync_kiocb(&kiocb, filp);
340 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700341 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000342 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700343
Zach Brown41003a72013-05-07 16:18:25 -0700344 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (-EIOCBQUEUED == ret)
346 ret = wait_on_sync_kiocb(&kiocb);
347 *ppos = kiocb.ki_pos;
348 return ret;
349}
350
351EXPORT_SYMBOL(do_sync_read);
352
353ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
354{
355 ssize_t ret;
356
357 if (!(file->f_mode & FMODE_READ))
358 return -EBADF;
359 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
360 return -EINVAL;
361 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
362 return -EFAULT;
363
364 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800365 if (ret >= 0) {
366 count = ret;
James Morrisc43e2592008-01-12 22:05:48 +1100367 if (file->f_op->read)
368 ret = file->f_op->read(file, buf, count, pos);
369 else
370 ret = do_sync_read(file, buf, count, pos);
371 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500372 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100373 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
James Morrisc43e2592008-01-12 22:05:48 +1100375 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 return ret;
379}
380
381EXPORT_SYMBOL(vfs_read);
382
383ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
384{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700385 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct kiocb kiocb;
387 ssize_t ret;
388
389 init_sync_kiocb(&kiocb, filp);
390 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700391 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000392 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700393
Zach Brown41003a72013-05-07 16:18:25 -0700394 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (-EIOCBQUEUED == ret)
396 ret = wait_on_sync_kiocb(&kiocb);
397 *ppos = kiocb.ki_pos;
398 return ret;
399}
400
401EXPORT_SYMBOL(do_sync_write);
402
Al Viro06ae43f2013-03-20 13:19:30 -0400403ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
404{
405 mm_segment_t old_fs;
406 const char __user *p;
407 ssize_t ret;
408
Al Viro3e84f482013-03-27 15:20:30 +0000409 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
410 return -EINVAL;
411
Al Viro06ae43f2013-03-20 13:19:30 -0400412 old_fs = get_fs();
413 set_fs(get_ds());
414 p = (__force const char __user *)buf;
415 if (count > MAX_RW_COUNT)
416 count = MAX_RW_COUNT;
417 if (file->f_op->write)
418 ret = file->f_op->write(file, p, count, pos);
419 else
420 ret = do_sync_write(file, p, count, pos);
421 set_fs(old_fs);
422 if (ret > 0) {
423 fsnotify_modify(file);
424 add_wchar(current, ret);
425 }
426 inc_syscw(current);
427 return ret;
428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
431{
432 ssize_t ret;
433
434 if (!(file->f_mode & FMODE_WRITE))
435 return -EBADF;
436 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
437 return -EINVAL;
438 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
439 return -EFAULT;
440
441 ret = rw_verify_area(WRITE, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800442 if (ret >= 0) {
443 count = ret;
Al Viro03d95eb2013-03-20 13:04:20 -0400444 file_start_write(file);
James Morrisc43e2592008-01-12 22:05:48 +1100445 if (file->f_op->write)
446 ret = file->f_op->write(file, buf, count, pos);
447 else
448 ret = do_sync_write(file, buf, count, pos);
449 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500450 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100451 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
James Morrisc43e2592008-01-12 22:05:48 +1100453 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400454 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456
457 return ret;
458}
459
460EXPORT_SYMBOL(vfs_write);
461
462static inline loff_t file_pos_read(struct file *file)
463{
464 return file->f_pos;
465}
466
467static inline void file_pos_write(struct file *file, loff_t pos)
468{
469 file->f_pos = pos;
470}
471
Heiko Carstens3cdad422009-01-14 14:14:22 +0100472SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Al Viro2903ff02012-08-28 12:52:22 -0400474 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Al Viro2903ff02012-08-28 12:52:22 -0400477 if (f.file) {
478 loff_t pos = file_pos_read(f.file);
479 ret = vfs_read(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400480 if (ret >= 0)
481 file_pos_write(f.file, pos);
Al Viro2903ff02012-08-28 12:52:22 -0400482 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return ret;
485}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Heiko Carstens3cdad422009-01-14 14:14:22 +0100487SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
488 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Al Viro2903ff02012-08-28 12:52:22 -0400490 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Al Viro2903ff02012-08-28 12:52:22 -0400493 if (f.file) {
494 loff_t pos = file_pos_read(f.file);
495 ret = vfs_write(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400496 if (ret >= 0)
497 file_pos_write(f.file, pos);
Al Viro2903ff02012-08-28 12:52:22 -0400498 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
501 return ret;
502}
503
Al Viro4a0fd5b2013-01-21 15:16:58 -0500504SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
505 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Al Viro2903ff02012-08-28 12:52:22 -0400507 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (pos < 0)
511 return -EINVAL;
512
Al Viro2903ff02012-08-28 12:52:22 -0400513 f = fdget(fd);
514 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400516 if (f.file->f_mode & FMODE_PREAD)
517 ret = vfs_read(f.file, buf, count, &pos);
518 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 return ret;
522}
523
Al Viro4a0fd5b2013-01-21 15:16:58 -0500524SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
525 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Al Viro2903ff02012-08-28 12:52:22 -0400527 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (pos < 0)
531 return -EINVAL;
532
Al Viro2903ff02012-08-28 12:52:22 -0400533 f = fdget(fd);
534 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400536 if (f.file->f_mode & FMODE_PWRITE)
537 ret = vfs_write(f.file, buf, count, &pos);
538 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
541 return ret;
542}
543
544/*
545 * Reduce an iovec's length in-place. Return the resulting number of segments
546 */
547unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
548{
549 unsigned long seg = 0;
550 size_t len = 0;
551
552 while (seg < nr_segs) {
553 seg++;
554 if (len + iov->iov_len >= to) {
555 iov->iov_len = to - len;
556 break;
557 }
558 len += iov->iov_len;
559 iov++;
560 }
561 return seg;
562}
Eric Sandeen19295522008-01-28 23:58:27 -0500563EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Al Viro72ec3512013-03-20 10:42:10 -0400565static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700566 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
567{
568 struct kiocb kiocb;
569 ssize_t ret;
570
571 init_sync_kiocb(&kiocb, filp);
572 kiocb.ki_pos = *ppos;
573 kiocb.ki_left = len;
574 kiocb.ki_nbytes = len;
575
Zach Brown41003a72013-05-07 16:18:25 -0700576 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700577 if (ret == -EIOCBQUEUED)
578 ret = wait_on_sync_kiocb(&kiocb);
579 *ppos = kiocb.ki_pos;
580 return ret;
581}
582
583/* Do it by hand, with file-ops */
Al Viro72ec3512013-03-20 10:42:10 -0400584static ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700585 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
586{
587 struct iovec *vector = iov;
588 ssize_t ret = 0;
589
590 while (nr_segs > 0) {
591 void __user *base;
592 size_t len;
593 ssize_t nr;
594
595 base = vector->iov_base;
596 len = vector->iov_len;
597 vector++;
598 nr_segs--;
599
600 nr = fn(filp, base, len, ppos);
601
602 if (nr < 0) {
603 if (!ret)
604 ret = nr;
605 break;
606 }
607 ret += nr;
608 if (nr != len)
609 break;
610 }
611
612 return ret;
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/* A write operation does a read from user space and vice versa */
616#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
617
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700618ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
619 unsigned long nr_segs, unsigned long fast_segs,
620 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700621 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700622{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700623 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700624 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700625 struct iovec *iov = fast_pointer;
626
Linus Torvalds435f49a2010-10-29 10:36:49 -0700627 /*
628 * SuS says "The readv() function *may* fail if the iovcnt argument
629 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
630 * traditionally returned zero for zero segments, so...
631 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700632 if (nr_segs == 0) {
633 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700634 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700635 }
636
Linus Torvalds435f49a2010-10-29 10:36:49 -0700637 /*
638 * First get the "struct iovec" from user memory and
639 * verify all the pointers
640 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700641 if (nr_segs > UIO_MAXIOV) {
642 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700643 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700644 }
645 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700646 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700647 if (iov == NULL) {
648 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700649 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700650 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700651 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700652 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
653 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700654 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700655 }
656
Linus Torvalds435f49a2010-10-29 10:36:49 -0700657 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700658 * According to the Single Unix Specification we should return EINVAL
659 * if an element length is < 0 when cast to ssize_t or if the
660 * total length would overflow the ssize_t return value of the
661 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700662 *
663 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
664 * overflow case.
665 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700666 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700667 for (seg = 0; seg < nr_segs; seg++) {
668 void __user *buf = iov[seg].iov_base;
669 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700670
671 /* see if we we're about to use an invalid len or if
672 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700673 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700674 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700675 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700676 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700677 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700678 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700679 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700680 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700681 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700682 if (len > MAX_RW_COUNT - ret) {
683 len = MAX_RW_COUNT - ret;
684 iov[seg].iov_len = len;
685 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700686 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700687 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700688out:
689 *ret_pointer = iov;
690 return ret;
691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693static ssize_t do_readv_writev(int type, struct file *file,
694 const struct iovec __user * uvector,
695 unsigned long nr_segs, loff_t *pos)
696{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 size_t tot_len;
698 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700699 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 io_fn_t fn;
702 iov_fn_t fnv;
703
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700704 if (!file->f_op) {
705 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 goto out;
707 }
708
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700709 ret = rw_copy_check_uvector(type, uvector, nr_segs,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700710 ARRAY_SIZE(iovstack), iovstack, &iov);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700711 if (ret <= 0)
712 goto out;
713
714 tot_len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800716 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto out;
718
719 fnv = NULL;
720 if (type == READ) {
721 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700722 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 } else {
724 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700725 fnv = file->f_op->aio_write;
Al Viro03d95eb2013-03-20 13:04:20 -0400726 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700729 if (fnv)
730 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
731 pos, fnv);
732 else
733 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Al Viro03d95eb2013-03-20 13:04:20 -0400735 if (type != READ)
736 file_end_write(file);
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738out:
739 if (iov != iovstack)
740 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400741 if ((ret + (type == READ)) > 0) {
742 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500743 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400744 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500745 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
751 unsigned long vlen, loff_t *pos)
752{
753 if (!(file->f_mode & FMODE_READ))
754 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700755 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return -EINVAL;
757
758 return do_readv_writev(READ, file, vec, vlen, pos);
759}
760
761EXPORT_SYMBOL(vfs_readv);
762
763ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
764 unsigned long vlen, loff_t *pos)
765{
766 if (!(file->f_mode & FMODE_WRITE))
767 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700768 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return -EINVAL;
770
771 return do_readv_writev(WRITE, file, vec, vlen, pos);
772}
773
774EXPORT_SYMBOL(vfs_writev);
775
Heiko Carstens3cdad422009-01-14 14:14:22 +0100776SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
777 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Al Viro2903ff02012-08-28 12:52:22 -0400779 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Al Viro2903ff02012-08-28 12:52:22 -0400782 if (f.file) {
783 loff_t pos = file_pos_read(f.file);
784 ret = vfs_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400785 if (ret >= 0)
786 file_pos_write(f.file, pos);
Al Viro2903ff02012-08-28 12:52:22 -0400787 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
790 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800791 add_rchar(current, ret);
792 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return ret;
794}
795
Heiko Carstens3cdad422009-01-14 14:14:22 +0100796SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
797 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Al Viro2903ff02012-08-28 12:52:22 -0400799 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Al Viro2903ff02012-08-28 12:52:22 -0400802 if (f.file) {
803 loff_t pos = file_pos_read(f.file);
804 ret = vfs_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400805 if (ret >= 0)
806 file_pos_write(f.file, pos);
Al Viro2903ff02012-08-28 12:52:22 -0400807 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
810 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800811 add_wchar(current, ret);
812 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return ret;
814}
815
Linus Torvalds601cc112009-04-03 08:03:22 -0700816static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700817{
Linus Torvalds601cc112009-04-03 08:03:22 -0700818#define HALF_LONG_BITS (BITS_PER_LONG / 2)
819 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
820}
821
822SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
823 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
824{
825 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400826 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700827 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700828
829 if (pos < 0)
830 return -EINVAL;
831
Al Viro2903ff02012-08-28 12:52:22 -0400832 f = fdget(fd);
833 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700834 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400835 if (f.file->f_mode & FMODE_PREAD)
836 ret = vfs_readv(f.file, vec, vlen, &pos);
837 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700838 }
839
840 if (ret > 0)
841 add_rchar(current, ret);
842 inc_syscr(current);
843 return ret;
844}
845
846SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700847 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700848{
Linus Torvalds601cc112009-04-03 08:03:22 -0700849 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400850 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700851 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700852
853 if (pos < 0)
854 return -EINVAL;
855
Al Viro2903ff02012-08-28 12:52:22 -0400856 f = fdget(fd);
857 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700858 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400859 if (f.file->f_mode & FMODE_PWRITE)
860 ret = vfs_writev(f.file, vec, vlen, &pos);
861 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700862 }
863
864 if (ret > 0)
865 add_wchar(current, ret);
866 inc_syscw(current);
867 return ret;
868}
869
Al Viro72ec3512013-03-20 10:42:10 -0400870#ifdef CONFIG_COMPAT
871
872static ssize_t compat_do_readv_writev(int type, struct file *file,
873 const struct compat_iovec __user *uvector,
874 unsigned long nr_segs, loff_t *pos)
875{
876 compat_ssize_t tot_len;
877 struct iovec iovstack[UIO_FASTIOV];
878 struct iovec *iov = iovstack;
879 ssize_t ret;
880 io_fn_t fn;
881 iov_fn_t fnv;
882
883 ret = -EINVAL;
884 if (!file->f_op)
885 goto out;
886
887 ret = -EFAULT;
888 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
889 goto out;
890
891 ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
892 UIO_FASTIOV, iovstack, &iov);
893 if (ret <= 0)
894 goto out;
895
896 tot_len = ret;
897 ret = rw_verify_area(type, file, pos, tot_len);
898 if (ret < 0)
899 goto out;
900
901 fnv = NULL;
902 if (type == READ) {
903 fn = file->f_op->read;
904 fnv = file->f_op->aio_read;
905 } else {
906 fn = (io_fn_t)file->f_op->write;
907 fnv = file->f_op->aio_write;
Al Viro03d95eb2013-03-20 13:04:20 -0400908 file_start_write(file);
Al Viro72ec3512013-03-20 10:42:10 -0400909 }
910
Al Viro03d95eb2013-03-20 13:04:20 -0400911 if (fnv)
Al Viro72ec3512013-03-20 10:42:10 -0400912 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
913 pos, fnv);
Al Viro03d95eb2013-03-20 13:04:20 -0400914 else
Al Viro72ec3512013-03-20 10:42:10 -0400915 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
916
Al Viro03d95eb2013-03-20 13:04:20 -0400917 if (type != READ)
918 file_end_write(file);
919
Al Viro72ec3512013-03-20 10:42:10 -0400920out:
921 if (iov != iovstack)
922 kfree(iov);
923 if ((ret + (type == READ)) > 0) {
924 if (type == READ)
925 fsnotify_access(file);
926 else
927 fsnotify_modify(file);
928 }
929 return ret;
930}
931
932static size_t compat_readv(struct file *file,
933 const struct compat_iovec __user *vec,
934 unsigned long vlen, loff_t *pos)
935{
936 ssize_t ret = -EBADF;
937
938 if (!(file->f_mode & FMODE_READ))
939 goto out;
940
941 ret = -EINVAL;
942 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
943 goto out;
944
945 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
946
947out:
948 if (ret > 0)
949 add_rchar(current, ret);
950 inc_syscr(current);
951 return ret;
952}
953
954COMPAT_SYSCALL_DEFINE3(readv, unsigned long, fd,
955 const struct compat_iovec __user *,vec,
956 unsigned long, vlen)
957{
958 struct fd f = fdget(fd);
959 ssize_t ret;
960 loff_t pos;
961
962 if (!f.file)
963 return -EBADF;
964 pos = f.file->f_pos;
965 ret = compat_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400966 if (ret >= 0)
967 f.file->f_pos = pos;
Al Viro72ec3512013-03-20 10:42:10 -0400968 fdput(f);
969 return ret;
970}
971
972COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
973 const struct compat_iovec __user *,vec,
974 unsigned long, vlen, loff_t, pos)
975{
976 struct fd f;
977 ssize_t ret;
978
979 if (pos < 0)
980 return -EINVAL;
981 f = fdget(fd);
982 if (!f.file)
983 return -EBADF;
984 ret = -ESPIPE;
985 if (f.file->f_mode & FMODE_PREAD)
986 ret = compat_readv(f.file, vec, vlen, &pos);
987 fdput(f);
988 return ret;
989}
990
991COMPAT_SYSCALL_DEFINE5(preadv, unsigned long, fd,
992 const struct compat_iovec __user *,vec,
993 unsigned long, vlen, u32, pos_low, u32, pos_high)
994{
995 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
996 return compat_sys_preadv64(fd, vec, vlen, pos);
997}
998
999static size_t compat_writev(struct file *file,
1000 const struct compat_iovec __user *vec,
1001 unsigned long vlen, loff_t *pos)
1002{
1003 ssize_t ret = -EBADF;
1004
1005 if (!(file->f_mode & FMODE_WRITE))
1006 goto out;
1007
1008 ret = -EINVAL;
1009 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
1010 goto out;
1011
1012 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1013
1014out:
1015 if (ret > 0)
1016 add_wchar(current, ret);
1017 inc_syscw(current);
1018 return ret;
1019}
1020
1021COMPAT_SYSCALL_DEFINE3(writev, unsigned long, fd,
1022 const struct compat_iovec __user *, vec,
1023 unsigned long, vlen)
1024{
1025 struct fd f = fdget(fd);
1026 ssize_t ret;
1027 loff_t pos;
1028
1029 if (!f.file)
1030 return -EBADF;
1031 pos = f.file->f_pos;
1032 ret = compat_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001033 if (ret >= 0)
1034 f.file->f_pos = pos;
Al Viro72ec3512013-03-20 10:42:10 -04001035 fdput(f);
1036 return ret;
1037}
1038
1039COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1040 const struct compat_iovec __user *,vec,
1041 unsigned long, vlen, loff_t, pos)
1042{
1043 struct fd f;
1044 ssize_t ret;
1045
1046 if (pos < 0)
1047 return -EINVAL;
1048 f = fdget(fd);
1049 if (!f.file)
1050 return -EBADF;
1051 ret = -ESPIPE;
1052 if (f.file->f_mode & FMODE_PWRITE)
1053 ret = compat_writev(f.file, vec, vlen, &pos);
1054 fdput(f);
1055 return ret;
1056}
1057
1058COMPAT_SYSCALL_DEFINE5(pwritev, unsigned long, fd,
1059 const struct compat_iovec __user *,vec,
1060 unsigned long, vlen, u32, pos_low, u32, pos_high)
1061{
1062 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1063 return compat_sys_pwritev64(fd, vec, vlen, pos);
1064}
1065#endif
1066
Al Viro19f4fc32013-02-24 02:17:03 -05001067static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1068 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Al Viro2903ff02012-08-28 12:52:22 -04001070 struct fd in, out;
1071 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001073 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001075 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /*
1078 * Get input file, and verify that it is ok..
1079 */
1080 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001081 in = fdget(in_fd);
1082 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001084 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001087 if (!ppos) {
1088 pos = in.file->f_pos;
1089 } else {
1090 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001091 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001093 }
1094 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001095 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001097 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /*
1100 * Get output file, and verify that it is ok..
1101 */
1102 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001103 out = fdget(out_fd);
1104 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001106 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 goto fput_out;
1108 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001109 in_inode = file_inode(in.file);
1110 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001111 out_pos = out.file->f_pos;
1112 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001113 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001115 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!max)
1118 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (unlikely(pos + count > max)) {
1121 retval = -EOVERFLOW;
1122 if (pos >= max)
1123 goto fput_out;
1124 count = max - pos;
1125 }
1126
Jens Axboed96e6e72007-06-11 12:18:52 +02001127 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001128#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001129 /*
1130 * We need to debate whether we can enable this or not. The
1131 * man page documents EAGAIN return for the output at least,
1132 * and the application is arguably buggy if it doesn't expect
1133 * EAGAIN on a non-blocking file descriptor.
1134 */
Al Viro2903ff02012-08-28 12:52:22 -04001135 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001136 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001137#endif
Al Viro50cd2c52013-05-23 20:10:34 -04001138 file_start_write(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001139 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
Al Viro50cd2c52013-05-23 20:10:34 -04001140 file_end_write(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001143 add_rchar(current, retval);
1144 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001145 fsnotify_access(in.file);
1146 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001147 out.file->f_pos = out_pos;
1148 if (ppos)
1149 *ppos = pos;
1150 else
1151 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001154 inc_syscr(current);
1155 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001156 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 retval = -EOVERFLOW;
1158
1159fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001160 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001162 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163out:
1164 return retval;
1165}
1166
Heiko Carstens002c8972009-01-14 14:14:18 +01001167SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 loff_t pos;
1170 off_t off;
1171 ssize_t ret;
1172
1173 if (offset) {
1174 if (unlikely(get_user(off, offset)))
1175 return -EFAULT;
1176 pos = off;
1177 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1178 if (unlikely(put_user(pos, offset)))
1179 return -EFAULT;
1180 return ret;
1181 }
1182
1183 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1184}
1185
Heiko Carstens002c8972009-01-14 14:14:18 +01001186SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 loff_t pos;
1189 ssize_t ret;
1190
1191 if (offset) {
1192 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1193 return -EFAULT;
1194 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1195 if (unlikely(put_user(pos, offset)))
1196 return -EFAULT;
1197 return ret;
1198 }
1199
1200 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1201}
Al Viro19f4fc32013-02-24 02:17:03 -05001202
1203#ifdef CONFIG_COMPAT
1204COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1205 compat_off_t __user *, offset, compat_size_t, count)
1206{
1207 loff_t pos;
1208 off_t off;
1209 ssize_t ret;
1210
1211 if (offset) {
1212 if (unlikely(get_user(off, offset)))
1213 return -EFAULT;
1214 pos = off;
1215 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1216 if (unlikely(put_user(pos, offset)))
1217 return -EFAULT;
1218 return ret;
1219 }
1220
1221 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1222}
1223
1224COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1225 compat_loff_t __user *, offset, compat_size_t, count)
1226{
1227 loff_t pos;
1228 ssize_t ret;
1229
1230 if (offset) {
1231 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1232 return -EFAULT;
1233 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1234 if (unlikely(put_user(pos, offset)))
1235 return -EFAULT;
1236 return ret;
1237 }
1238
1239 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1240}
1241#endif