blob: bce289afd21a7d5d4a7f4fc93fde0c8de66990cd [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);
27
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080028const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -070030 .read = do_sync_read,
31 .aio_read = generic_file_aio_read,
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
Andi Kleenef3d0fd2011-09-15 16:06:48 -070043static loff_t lseek_execute(struct file *file, struct inode *inode,
44 loff_t offset, loff_t maxsize)
45{
46 if (offset < 0 && !unsigned_offsets(file))
47 return -EINVAL;
48 if (offset > maxsize)
49 return -EINVAL;
50
51 if (offset != file->f_pos) {
52 file->f_pos = offset;
53 file->f_version = 0;
54 }
55 return offset;
56}
57
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020058/**
Andi Kleen57604952011-09-15 16:06:50 -070059 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020060 * @file: file structure to seek on
61 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -080062 * @whence: type of seek
Eric Sandeene8b96eb2012-04-30 13:11:29 -050063 * @size: max size of this file in file system
64 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020065 *
Andi Kleen57604952011-09-15 16:06:50 -070066 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050067 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070068 *
69 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070070 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070071 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
72 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020073 */
Andi Kleen9465efc2008-06-27 11:05:24 +020074loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -080075generic_file_llseek_size(struct file *file, loff_t offset, int whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050076 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct inode *inode = file->f_mapping->host;
79
Andrew Morton965c8e52012-12-17 15:59:39 -080080 switch (whence) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020081 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050082 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020083 break;
84 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080085 /*
86 * Here we special-case the lseek(fd, 0, SEEK_CUR)
87 * position-querying operation. Avoid rewriting the "same"
88 * f_pos value back to the file because a concurrent read(),
89 * write() or lseek() might have altered it
90 */
91 if (offset == 0)
92 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -070093 /*
94 * f_lock protects against read/modify/write race with other
95 * SEEK_CURs. Note that parallel writes and reads behave
96 * like SEEK_SET.
97 */
98 spin_lock(&file->f_lock);
99 offset = lseek_execute(file, inode, file->f_pos + offset,
Andi Kleen57604952011-09-15 16:06:50 -0700100 maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700101 spin_unlock(&file->f_lock);
102 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -0400103 case SEEK_DATA:
104 /*
105 * In the generic case the entire file is data, so as long as
106 * offset isn't at the end of the file then the offset is data.
107 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500108 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400109 return -ENXIO;
110 break;
111 case SEEK_HOLE:
112 /*
113 * There is a virtual hole at the end of the file, so as long as
114 * offset isn't i_size or larger, return i_size.
115 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500116 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400117 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500118 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200121
Andi Kleen57604952011-09-15 16:06:50 -0700122 return lseek_execute(file, inode, offset, maxsize);
123}
124EXPORT_SYMBOL(generic_file_llseek_size);
125
126/**
127 * generic_file_llseek - generic llseek implementation for regular files
128 * @file: file structure to seek on
129 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800130 * @whence: type of seek
Andi Kleen57604952011-09-15 16:06:50 -0700131 *
132 * This is a generic implemenation of ->llseek useable for all normal local
133 * filesystems. It just updates the file offset to the value specified by
Ming Lei546ae2d2013-04-29 15:06:07 -0700134 * @offset and @whence.
Andi Kleen57604952011-09-15 16:06:50 -0700135 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800136loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
Andi Kleen57604952011-09-15 16:06:50 -0700137{
138 struct inode *inode = file->f_mapping->host;
139
Andrew Morton965c8e52012-12-17 15:59:39 -0800140 return generic_file_llseek_size(file, offset, whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500141 inode->i_sb->s_maxbytes,
142 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
Andi Kleen9465efc2008-06-27 11:05:24 +0200144EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
jan Blunckae6afc32010-05-26 14:44:48 -0700146/**
147 * noop_llseek - No Operation Performed llseek implementation
148 * @file: file structure to seek on
149 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800150 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700151 *
152 * This is an implementation of ->llseek useable for the rare special case when
153 * userspace expects the seek to succeed but the (device) file is actually not
154 * able to perform the seek. In this case you use noop_llseek() instead of
155 * falling back to the default implementation of ->llseek.
156 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800157loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700158{
159 return file->f_pos;
160}
161EXPORT_SYMBOL(noop_llseek);
162
Andrew Morton965c8e52012-12-17 15:59:39 -0800163loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 return -ESPIPE;
166}
167EXPORT_SYMBOL(no_llseek);
168
Andrew Morton965c8e52012-12-17 15:59:39 -0800169loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Al Viro496ad9a2013-01-23 17:07:38 -0500171 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200172 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Josef Bacik982d8162011-07-18 13:21:35 -0400174 mutex_lock(&inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -0800175 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700176 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400177 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700179 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800180 if (offset == 0) {
181 retval = file->f_pos;
182 goto out;
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400185 break;
186 case SEEK_DATA:
187 /*
188 * In the generic case the entire file is data, so as
189 * long as offset isn't at the end of the file then the
190 * offset is data.
191 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300192 if (offset >= inode->i_size) {
193 retval = -ENXIO;
194 goto out;
195 }
Josef Bacik982d8162011-07-18 13:21:35 -0400196 break;
197 case SEEK_HOLE:
198 /*
199 * There is a virtual hole at the end of the file, so
200 * as long as offset isn't i_size or larger, return
201 * i_size.
202 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300203 if (offset >= inode->i_size) {
204 retval = -ENXIO;
205 goto out;
206 }
Josef Bacik982d8162011-07-18 13:21:35 -0400207 offset = inode->i_size;
208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500211 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (offset != file->f_pos) {
213 file->f_pos = offset;
214 file->f_version = 0;
215 }
216 retval = offset;
217 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800218out:
Josef Bacik982d8162011-07-18 13:21:35 -0400219 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return retval;
221}
222EXPORT_SYMBOL(default_llseek);
223
Andrew Morton965c8e52012-12-17 15:59:39 -0800224loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 loff_t (*fn)(struct file *, loff_t, int);
227
228 fn = no_llseek;
229 if (file->f_mode & FMODE_LSEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (file->f_op && file->f_op->llseek)
231 fn = file->f_op->llseek;
232 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800233 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235EXPORT_SYMBOL(vfs_llseek);
236
Andrew Morton965c8e52012-12-17 15:59:39 -0800237SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 off_t retval;
Al Viro2903ff02012-08-28 12:52:22 -0400240 struct fd f = fdget(fd);
241 if (!f.file)
242 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800245 if (whence <= SEEK_MAX) {
246 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 retval = res;
248 if (res != (loff_t)retval)
249 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
250 }
Al Viro2903ff02012-08-28 12:52:22 -0400251 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return retval;
253}
254
Al Viro561c6732013-02-24 10:52:26 -0500255#ifdef CONFIG_COMPAT
256COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
257{
258 return sys_lseek(fd, offset, whence);
259}
260#endif
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100263SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
264 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800265 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 int retval;
Al Viro2903ff02012-08-28 12:52:22 -0400268 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Al Viro2903ff02012-08-28 12:52:22 -0400271 if (!f.file)
272 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800275 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 goto out_putf;
277
Al Viro2903ff02012-08-28 12:52:22 -0400278 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800279 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 retval = (int)offset;
282 if (offset >= 0) {
283 retval = -EFAULT;
284 if (!copy_to_user(result, &offset, sizeof(offset)))
285 retval = 0;
286 }
287out_putf:
Al Viro2903ff02012-08-28 12:52:22 -0400288 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return retval;
290}
291#endif
292
Linus Torvaldse28cc712006-01-04 16:20:40 -0800293/*
294 * rw_verify_area doesn't like huge counts. We limit
295 * them to something that fits in "int" so that others
296 * won't have to do range checks all the time.
297 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
299{
300 struct inode *inode;
301 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100302 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Al Viro496ad9a2013-01-23 17:07:38 -0500304 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800305 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100306 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500308 if (unlikely(pos < 0)) {
309 if (!unsigned_offsets(file))
310 return retval;
311 if (count >= -pos) /* both values are in 0..LLONG_MAX */
312 return -EOVERFLOW;
313 } else if (unlikely((loff_t) (pos + count) < 0)) {
314 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700315 return retval;
316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Pavel Emelyanova16877c2007-10-01 14:41:11 -0700318 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
James Morrisc43e2592008-01-12 22:05:48 +1100319 retval = locks_mandatory_area(
Linus Torvaldse28cc712006-01-04 16:20:40 -0800320 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
321 inode, file, pos, count);
322 if (retval < 0)
323 return retval;
324 }
James Morrisc43e2592008-01-12 22:05:48 +1100325 retval = security_file_permission(file,
326 read_write == READ ? MAY_READ : MAY_WRITE);
327 if (retval)
328 return retval;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800329 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
333{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700334 struct iovec iov = { .iov_base = buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct kiocb kiocb;
336 ssize_t ret;
337
338 init_sync_kiocb(&kiocb, filp);
339 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700340 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000341 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700342
Zach Brown41003a72013-05-07 16:18:25 -0700343 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (-EIOCBQUEUED == ret)
345 ret = wait_on_sync_kiocb(&kiocb);
346 *ppos = kiocb.ki_pos;
347 return ret;
348}
349
350EXPORT_SYMBOL(do_sync_read);
351
352ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
353{
354 ssize_t ret;
355
356 if (!(file->f_mode & FMODE_READ))
357 return -EBADF;
358 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
359 return -EINVAL;
360 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
361 return -EFAULT;
362
363 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800364 if (ret >= 0) {
365 count = ret;
James Morrisc43e2592008-01-12 22:05:48 +1100366 if (file->f_op->read)
367 ret = file->f_op->read(file, buf, count, pos);
368 else
369 ret = do_sync_read(file, buf, count, pos);
370 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500371 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100372 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
James Morrisc43e2592008-01-12 22:05:48 +1100374 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
377 return ret;
378}
379
380EXPORT_SYMBOL(vfs_read);
381
382ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
383{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700384 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 struct kiocb kiocb;
386 ssize_t ret;
387
388 init_sync_kiocb(&kiocb, filp);
389 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700390 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000391 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700392
Zach Brown41003a72013-05-07 16:18:25 -0700393 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (-EIOCBQUEUED == ret)
395 ret = wait_on_sync_kiocb(&kiocb);
396 *ppos = kiocb.ki_pos;
397 return ret;
398}
399
400EXPORT_SYMBOL(do_sync_write);
401
Al Viro06ae43f2013-03-20 13:19:30 -0400402ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
403{
404 mm_segment_t old_fs;
405 const char __user *p;
406 ssize_t ret;
407
Al Viro3e84f482013-03-27 15:20:30 +0000408 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
409 return -EINVAL;
410
Al Viro06ae43f2013-03-20 13:19:30 -0400411 old_fs = get_fs();
412 set_fs(get_ds());
413 p = (__force const char __user *)buf;
414 if (count > MAX_RW_COUNT)
415 count = MAX_RW_COUNT;
416 if (file->f_op->write)
417 ret = file->f_op->write(file, p, count, pos);
418 else
419 ret = do_sync_write(file, p, count, pos);
420 set_fs(old_fs);
421 if (ret > 0) {
422 fsnotify_modify(file);
423 add_wchar(current, ret);
424 }
425 inc_syscw(current);
426 return ret;
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
430{
431 ssize_t ret;
432
433 if (!(file->f_mode & FMODE_WRITE))
434 return -EBADF;
435 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
436 return -EINVAL;
437 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
438 return -EFAULT;
439
440 ret = rw_verify_area(WRITE, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800441 if (ret >= 0) {
442 count = ret;
Al Viro03d95eb2013-03-20 13:04:20 -0400443 file_start_write(file);
James Morrisc43e2592008-01-12 22:05:48 +1100444 if (file->f_op->write)
445 ret = file->f_op->write(file, buf, count, pos);
446 else
447 ret = do_sync_write(file, buf, count, pos);
448 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500449 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100450 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
James Morrisc43e2592008-01-12 22:05:48 +1100452 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400453 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 return ret;
457}
458
459EXPORT_SYMBOL(vfs_write);
460
461static inline loff_t file_pos_read(struct file *file)
462{
463 return file->f_pos;
464}
465
466static inline void file_pos_write(struct file *file, loff_t pos)
467{
468 file->f_pos = pos;
469}
470
Heiko Carstens3cdad422009-01-14 14:14:22 +0100471SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Al Viro2903ff02012-08-28 12:52:22 -0400473 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Al Viro2903ff02012-08-28 12:52:22 -0400476 if (f.file) {
477 loff_t pos = file_pos_read(f.file);
478 ret = vfs_read(f.file, buf, count, &pos);
479 file_pos_write(f.file, pos);
480 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return ret;
483}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Heiko Carstens3cdad422009-01-14 14:14:22 +0100485SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
486 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Al Viro2903ff02012-08-28 12:52:22 -0400488 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Al Viro2903ff02012-08-28 12:52:22 -0400491 if (f.file) {
492 loff_t pos = file_pos_read(f.file);
493 ret = vfs_write(f.file, buf, count, &pos);
494 file_pos_write(f.file, pos);
495 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
498 return ret;
499}
500
Al Viro4a0fd5b2013-01-21 15:16:58 -0500501SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
502 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Al Viro2903ff02012-08-28 12:52:22 -0400504 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (pos < 0)
508 return -EINVAL;
509
Al Viro2903ff02012-08-28 12:52:22 -0400510 f = fdget(fd);
511 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400513 if (f.file->f_mode & FMODE_PREAD)
514 ret = vfs_read(f.file, buf, count, &pos);
515 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
518 return ret;
519}
520
Al Viro4a0fd5b2013-01-21 15:16:58 -0500521SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
522 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Al Viro2903ff02012-08-28 12:52:22 -0400524 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 if (pos < 0)
528 return -EINVAL;
529
Al Viro2903ff02012-08-28 12:52:22 -0400530 f = fdget(fd);
531 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400533 if (f.file->f_mode & FMODE_PWRITE)
534 ret = vfs_write(f.file, buf, count, &pos);
535 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537
538 return ret;
539}
540
541/*
542 * Reduce an iovec's length in-place. Return the resulting number of segments
543 */
544unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
545{
546 unsigned long seg = 0;
547 size_t len = 0;
548
549 while (seg < nr_segs) {
550 seg++;
551 if (len + iov->iov_len >= to) {
552 iov->iov_len = to - len;
553 break;
554 }
555 len += iov->iov_len;
556 iov++;
557 }
558 return seg;
559}
Eric Sandeen19295522008-01-28 23:58:27 -0500560EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Al Viro72ec3512013-03-20 10:42:10 -0400562static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700563 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
564{
565 struct kiocb kiocb;
566 ssize_t ret;
567
568 init_sync_kiocb(&kiocb, filp);
569 kiocb.ki_pos = *ppos;
570 kiocb.ki_left = len;
571 kiocb.ki_nbytes = len;
572
Zach Brown41003a72013-05-07 16:18:25 -0700573 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700574 if (ret == -EIOCBQUEUED)
575 ret = wait_on_sync_kiocb(&kiocb);
576 *ppos = kiocb.ki_pos;
577 return ret;
578}
579
580/* Do it by hand, with file-ops */
Al Viro72ec3512013-03-20 10:42:10 -0400581static ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700582 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
583{
584 struct iovec *vector = iov;
585 ssize_t ret = 0;
586
587 while (nr_segs > 0) {
588 void __user *base;
589 size_t len;
590 ssize_t nr;
591
592 base = vector->iov_base;
593 len = vector->iov_len;
594 vector++;
595 nr_segs--;
596
597 nr = fn(filp, base, len, ppos);
598
599 if (nr < 0) {
600 if (!ret)
601 ret = nr;
602 break;
603 }
604 ret += nr;
605 if (nr != len)
606 break;
607 }
608
609 return ret;
610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/* A write operation does a read from user space and vice versa */
613#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
614
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700615ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
616 unsigned long nr_segs, unsigned long fast_segs,
617 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700618 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700619{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700620 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700621 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700622 struct iovec *iov = fast_pointer;
623
Linus Torvalds435f49a2010-10-29 10:36:49 -0700624 /*
625 * SuS says "The readv() function *may* fail if the iovcnt argument
626 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
627 * traditionally returned zero for zero segments, so...
628 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700629 if (nr_segs == 0) {
630 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700631 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700632 }
633
Linus Torvalds435f49a2010-10-29 10:36:49 -0700634 /*
635 * First get the "struct iovec" from user memory and
636 * verify all the pointers
637 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700638 if (nr_segs > UIO_MAXIOV) {
639 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700640 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700641 }
642 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700643 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700644 if (iov == NULL) {
645 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700646 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700647 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700648 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700649 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
650 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700651 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700652 }
653
Linus Torvalds435f49a2010-10-29 10:36:49 -0700654 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700655 * According to the Single Unix Specification we should return EINVAL
656 * if an element length is < 0 when cast to ssize_t or if the
657 * total length would overflow the ssize_t return value of the
658 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700659 *
660 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
661 * overflow case.
662 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700663 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700664 for (seg = 0; seg < nr_segs; seg++) {
665 void __user *buf = iov[seg].iov_base;
666 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700667
668 /* see if we we're about to use an invalid len or if
669 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700670 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700671 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700672 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700673 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700674 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700675 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700676 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700677 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700678 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700679 if (len > MAX_RW_COUNT - ret) {
680 len = MAX_RW_COUNT - ret;
681 iov[seg].iov_len = len;
682 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700683 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700684 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700685out:
686 *ret_pointer = iov;
687 return ret;
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static ssize_t do_readv_writev(int type, struct file *file,
691 const struct iovec __user * uvector,
692 unsigned long nr_segs, loff_t *pos)
693{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 size_t tot_len;
695 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700696 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 io_fn_t fn;
699 iov_fn_t fnv;
700
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700701 if (!file->f_op) {
702 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goto out;
704 }
705
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700706 ret = rw_copy_check_uvector(type, uvector, nr_segs,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700707 ARRAY_SIZE(iovstack), iovstack, &iov);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700708 if (ret <= 0)
709 goto out;
710
711 tot_len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800713 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 goto out;
715
716 fnv = NULL;
717 if (type == READ) {
718 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700719 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 } else {
721 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700722 fnv = file->f_op->aio_write;
Al Viro03d95eb2013-03-20 13:04:20 -0400723 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700726 if (fnv)
727 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
728 pos, fnv);
729 else
730 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Al Viro03d95eb2013-03-20 13:04:20 -0400732 if (type != READ)
733 file_end_write(file);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735out:
736 if (iov != iovstack)
737 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400738 if ((ret + (type == READ)) > 0) {
739 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500740 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400741 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500742 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
748 unsigned long vlen, loff_t *pos)
749{
750 if (!(file->f_mode & FMODE_READ))
751 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700752 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return -EINVAL;
754
755 return do_readv_writev(READ, file, vec, vlen, pos);
756}
757
758EXPORT_SYMBOL(vfs_readv);
759
760ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
761 unsigned long vlen, loff_t *pos)
762{
763 if (!(file->f_mode & FMODE_WRITE))
764 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700765 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return -EINVAL;
767
768 return do_readv_writev(WRITE, file, vec, vlen, pos);
769}
770
771EXPORT_SYMBOL(vfs_writev);
772
Heiko Carstens3cdad422009-01-14 14:14:22 +0100773SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
774 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Al Viro2903ff02012-08-28 12:52:22 -0400776 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Al Viro2903ff02012-08-28 12:52:22 -0400779 if (f.file) {
780 loff_t pos = file_pos_read(f.file);
781 ret = vfs_readv(f.file, vec, vlen, &pos);
782 file_pos_write(f.file, pos);
783 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
786 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800787 add_rchar(current, ret);
788 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return ret;
790}
791
Heiko Carstens3cdad422009-01-14 14:14:22 +0100792SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
793 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Al Viro2903ff02012-08-28 12:52:22 -0400795 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Al Viro2903ff02012-08-28 12:52:22 -0400798 if (f.file) {
799 loff_t pos = file_pos_read(f.file);
800 ret = vfs_writev(f.file, vec, vlen, &pos);
801 file_pos_write(f.file, pos);
802 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
805 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800806 add_wchar(current, ret);
807 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return ret;
809}
810
Linus Torvalds601cc112009-04-03 08:03:22 -0700811static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700812{
Linus Torvalds601cc112009-04-03 08:03:22 -0700813#define HALF_LONG_BITS (BITS_PER_LONG / 2)
814 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
815}
816
817SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
818 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
819{
820 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400821 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700822 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700823
824 if (pos < 0)
825 return -EINVAL;
826
Al Viro2903ff02012-08-28 12:52:22 -0400827 f = fdget(fd);
828 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700829 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400830 if (f.file->f_mode & FMODE_PREAD)
831 ret = vfs_readv(f.file, vec, vlen, &pos);
832 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700833 }
834
835 if (ret > 0)
836 add_rchar(current, ret);
837 inc_syscr(current);
838 return ret;
839}
840
841SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700842 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700843{
Linus Torvalds601cc112009-04-03 08:03:22 -0700844 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400845 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700846 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700847
848 if (pos < 0)
849 return -EINVAL;
850
Al Viro2903ff02012-08-28 12:52:22 -0400851 f = fdget(fd);
852 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700853 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400854 if (f.file->f_mode & FMODE_PWRITE)
855 ret = vfs_writev(f.file, vec, vlen, &pos);
856 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700857 }
858
859 if (ret > 0)
860 add_wchar(current, ret);
861 inc_syscw(current);
862 return ret;
863}
864
Al Viro72ec3512013-03-20 10:42:10 -0400865#ifdef CONFIG_COMPAT
866
867static ssize_t compat_do_readv_writev(int type, struct file *file,
868 const struct compat_iovec __user *uvector,
869 unsigned long nr_segs, loff_t *pos)
870{
871 compat_ssize_t tot_len;
872 struct iovec iovstack[UIO_FASTIOV];
873 struct iovec *iov = iovstack;
874 ssize_t ret;
875 io_fn_t fn;
876 iov_fn_t fnv;
877
878 ret = -EINVAL;
879 if (!file->f_op)
880 goto out;
881
882 ret = -EFAULT;
883 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
884 goto out;
885
886 ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
887 UIO_FASTIOV, iovstack, &iov);
888 if (ret <= 0)
889 goto out;
890
891 tot_len = ret;
892 ret = rw_verify_area(type, file, pos, tot_len);
893 if (ret < 0)
894 goto out;
895
896 fnv = NULL;
897 if (type == READ) {
898 fn = file->f_op->read;
899 fnv = file->f_op->aio_read;
900 } else {
901 fn = (io_fn_t)file->f_op->write;
902 fnv = file->f_op->aio_write;
Al Viro03d95eb2013-03-20 13:04:20 -0400903 file_start_write(file);
Al Viro72ec3512013-03-20 10:42:10 -0400904 }
905
Al Viro03d95eb2013-03-20 13:04:20 -0400906 if (fnv)
Al Viro72ec3512013-03-20 10:42:10 -0400907 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
908 pos, fnv);
Al Viro03d95eb2013-03-20 13:04:20 -0400909 else
Al Viro72ec3512013-03-20 10:42:10 -0400910 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
911
Al Viro03d95eb2013-03-20 13:04:20 -0400912 if (type != READ)
913 file_end_write(file);
914
Al Viro72ec3512013-03-20 10:42:10 -0400915out:
916 if (iov != iovstack)
917 kfree(iov);
918 if ((ret + (type == READ)) > 0) {
919 if (type == READ)
920 fsnotify_access(file);
921 else
922 fsnotify_modify(file);
923 }
924 return ret;
925}
926
927static size_t compat_readv(struct file *file,
928 const struct compat_iovec __user *vec,
929 unsigned long vlen, loff_t *pos)
930{
931 ssize_t ret = -EBADF;
932
933 if (!(file->f_mode & FMODE_READ))
934 goto out;
935
936 ret = -EINVAL;
937 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
938 goto out;
939
940 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
941
942out:
943 if (ret > 0)
944 add_rchar(current, ret);
945 inc_syscr(current);
946 return ret;
947}
948
949COMPAT_SYSCALL_DEFINE3(readv, unsigned long, fd,
950 const struct compat_iovec __user *,vec,
951 unsigned long, vlen)
952{
953 struct fd f = fdget(fd);
954 ssize_t ret;
955 loff_t pos;
956
957 if (!f.file)
958 return -EBADF;
959 pos = f.file->f_pos;
960 ret = compat_readv(f.file, vec, vlen, &pos);
961 f.file->f_pos = pos;
962 fdput(f);
963 return ret;
964}
965
966COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
967 const struct compat_iovec __user *,vec,
968 unsigned long, vlen, loff_t, pos)
969{
970 struct fd f;
971 ssize_t ret;
972
973 if (pos < 0)
974 return -EINVAL;
975 f = fdget(fd);
976 if (!f.file)
977 return -EBADF;
978 ret = -ESPIPE;
979 if (f.file->f_mode & FMODE_PREAD)
980 ret = compat_readv(f.file, vec, vlen, &pos);
981 fdput(f);
982 return ret;
983}
984
985COMPAT_SYSCALL_DEFINE5(preadv, unsigned long, fd,
986 const struct compat_iovec __user *,vec,
987 unsigned long, vlen, u32, pos_low, u32, pos_high)
988{
989 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
990 return compat_sys_preadv64(fd, vec, vlen, pos);
991}
992
993static size_t compat_writev(struct file *file,
994 const struct compat_iovec __user *vec,
995 unsigned long vlen, loff_t *pos)
996{
997 ssize_t ret = -EBADF;
998
999 if (!(file->f_mode & FMODE_WRITE))
1000 goto out;
1001
1002 ret = -EINVAL;
1003 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
1004 goto out;
1005
1006 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1007
1008out:
1009 if (ret > 0)
1010 add_wchar(current, ret);
1011 inc_syscw(current);
1012 return ret;
1013}
1014
1015COMPAT_SYSCALL_DEFINE3(writev, unsigned long, fd,
1016 const struct compat_iovec __user *, vec,
1017 unsigned long, vlen)
1018{
1019 struct fd f = fdget(fd);
1020 ssize_t ret;
1021 loff_t pos;
1022
1023 if (!f.file)
1024 return -EBADF;
1025 pos = f.file->f_pos;
1026 ret = compat_writev(f.file, vec, vlen, &pos);
1027 f.file->f_pos = pos;
1028 fdput(f);
1029 return ret;
1030}
1031
1032COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1033 const struct compat_iovec __user *,vec,
1034 unsigned long, vlen, loff_t, pos)
1035{
1036 struct fd f;
1037 ssize_t ret;
1038
1039 if (pos < 0)
1040 return -EINVAL;
1041 f = fdget(fd);
1042 if (!f.file)
1043 return -EBADF;
1044 ret = -ESPIPE;
1045 if (f.file->f_mode & FMODE_PWRITE)
1046 ret = compat_writev(f.file, vec, vlen, &pos);
1047 fdput(f);
1048 return ret;
1049}
1050
1051COMPAT_SYSCALL_DEFINE5(pwritev, unsigned long, fd,
1052 const struct compat_iovec __user *,vec,
1053 unsigned long, vlen, u32, pos_low, u32, pos_high)
1054{
1055 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1056 return compat_sys_pwritev64(fd, vec, vlen, pos);
1057}
1058#endif
1059
Al Viro19f4fc32013-02-24 02:17:03 -05001060static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1061 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Al Viro2903ff02012-08-28 12:52:22 -04001063 struct fd in, out;
1064 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 loff_t pos;
1066 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001067 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /*
1070 * Get input file, and verify that it is ok..
1071 */
1072 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001073 in = fdget(in_fd);
1074 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001076 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 retval = -ESPIPE;
1079 if (!ppos)
Al Viro2903ff02012-08-28 12:52:22 -04001080 ppos = &in.file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 else
Al Viro2903ff02012-08-28 12:52:22 -04001082 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001084 retval = rw_verify_area(READ, in.file, ppos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001085 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001087 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /*
1090 * Get output file, and verify that it is ok..
1091 */
1092 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001093 out = fdget(out_fd);
1094 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001096 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto fput_out;
1098 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001099 in_inode = file_inode(in.file);
1100 out_inode = file_inode(out.file);
Al Viro2903ff02012-08-28 12:52:22 -04001101 retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001102 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001104 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (!max)
1107 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1108
1109 pos = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (unlikely(pos + count > max)) {
1111 retval = -EOVERFLOW;
1112 if (pos >= max)
1113 goto fput_out;
1114 count = max - pos;
1115 }
1116
Jens Axboed96e6e72007-06-11 12:18:52 +02001117 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001118#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001119 /*
1120 * We need to debate whether we can enable this or not. The
1121 * man page documents EAGAIN return for the output at least,
1122 * and the application is arguably buggy if it doesn't expect
1123 * EAGAIN on a non-blocking file descriptor.
1124 */
Al Viro2903ff02012-08-28 12:52:22 -04001125 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001126 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001127#endif
Al Viro2903ff02012-08-28 12:52:22 -04001128 retval = do_splice_direct(in.file, ppos, out.file, count, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001131 add_rchar(current, retval);
1132 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001133 fsnotify_access(in.file);
1134 fsnotify_modify(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001137 inc_syscr(current);
1138 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (*ppos > max)
1140 retval = -EOVERFLOW;
1141
1142fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001143 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001145 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146out:
1147 return retval;
1148}
1149
Heiko Carstens002c8972009-01-14 14:14:18 +01001150SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 loff_t pos;
1153 off_t off;
1154 ssize_t ret;
1155
1156 if (offset) {
1157 if (unlikely(get_user(off, offset)))
1158 return -EFAULT;
1159 pos = off;
1160 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1161 if (unlikely(put_user(pos, offset)))
1162 return -EFAULT;
1163 return ret;
1164 }
1165
1166 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1167}
1168
Heiko Carstens002c8972009-01-14 14:14:18 +01001169SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 loff_t pos;
1172 ssize_t ret;
1173
1174 if (offset) {
1175 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1176 return -EFAULT;
1177 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
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}
Al Viro19f4fc32013-02-24 02:17:03 -05001185
1186#ifdef CONFIG_COMPAT
1187COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1188 compat_off_t __user *, offset, compat_size_t, count)
1189{
1190 loff_t pos;
1191 off_t off;
1192 ssize_t ret;
1193
1194 if (offset) {
1195 if (unlikely(get_user(off, offset)))
1196 return -EFAULT;
1197 pos = off;
1198 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1199 if (unlikely(put_user(pos, offset)))
1200 return -EFAULT;
1201 return ret;
1202 }
1203
1204 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1205}
1206
1207COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1208 compat_loff_t __user *, offset, compat_size_t, count)
1209{
1210 loff_t pos;
1211 ssize_t ret;
1212
1213 if (offset) {
1214 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1215 return -EFAULT;
1216 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1217 if (unlikely(put_user(pos, offset)))
1218 return -EFAULT;
1219 return ret;
1220 }
1221
1222 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1223}
1224#endif