blob: e6dd1c2d05920a86469030c7878413ef0fff2fc0 [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>
Badari Pulavartyee0b3e62006-09-30 23:28:47 -070019#include "read_write.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
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080025const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -070027 .read = do_sync_read,
28 .aio_read = generic_file_aio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020030 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
32
33EXPORT_SYMBOL(generic_ro_fops);
34
Al Virocccb5a12010-12-17 07:44:05 -050035static inline int unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070036{
Al Virocccb5a12010-12-17 07:44:05 -050037 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070038}
39
Andi Kleenef3d0fd2011-09-15 16:06:48 -070040static loff_t lseek_execute(struct file *file, struct inode *inode,
41 loff_t offset, loff_t maxsize)
42{
43 if (offset < 0 && !unsigned_offsets(file))
44 return -EINVAL;
45 if (offset > maxsize)
46 return -EINVAL;
47
48 if (offset != file->f_pos) {
49 file->f_pos = offset;
50 file->f_version = 0;
51 }
52 return offset;
53}
54
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020055/**
Andi Kleen57604952011-09-15 16:06:50 -070056 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020057 * @file: file structure to seek on
58 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -080059 * @whence: type of seek
Eric Sandeene8b96eb2012-04-30 13:11:29 -050060 * @size: max size of this file in file system
61 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020062 *
Andi Kleen57604952011-09-15 16:06:50 -070063 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050064 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070065 *
66 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070067 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070068 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
69 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020070 */
Andi Kleen9465efc2008-06-27 11:05:24 +020071loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -080072generic_file_llseek_size(struct file *file, loff_t offset, int whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050073 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct inode *inode = file->f_mapping->host;
76
Andrew Morton965c8e52012-12-17 15:59:39 -080077 switch (whence) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020078 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050079 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020080 break;
81 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080082 /*
83 * Here we special-case the lseek(fd, 0, SEEK_CUR)
84 * position-querying operation. Avoid rewriting the "same"
85 * f_pos value back to the file because a concurrent read(),
86 * write() or lseek() might have altered it
87 */
88 if (offset == 0)
89 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -070090 /*
91 * f_lock protects against read/modify/write race with other
92 * SEEK_CURs. Note that parallel writes and reads behave
93 * like SEEK_SET.
94 */
95 spin_lock(&file->f_lock);
96 offset = lseek_execute(file, inode, file->f_pos + offset,
Andi Kleen57604952011-09-15 16:06:50 -070097 maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -070098 spin_unlock(&file->f_lock);
99 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -0400100 case SEEK_DATA:
101 /*
102 * In the generic case the entire file is data, so as long as
103 * offset isn't at the end of the file then the offset is data.
104 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500105 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400106 return -ENXIO;
107 break;
108 case SEEK_HOLE:
109 /*
110 * There is a virtual hole at the end of the file, so as long as
111 * offset isn't i_size or larger, return i_size.
112 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500113 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400114 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500115 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400116 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200118
Andi Kleen57604952011-09-15 16:06:50 -0700119 return lseek_execute(file, inode, offset, maxsize);
120}
121EXPORT_SYMBOL(generic_file_llseek_size);
122
123/**
124 * generic_file_llseek - generic llseek implementation for regular files
125 * @file: file structure to seek on
126 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800127 * @whence: type of seek
Andi Kleen57604952011-09-15 16:06:50 -0700128 *
129 * This is a generic implemenation of ->llseek useable for all normal local
130 * filesystems. It just updates the file offset to the value specified by
Andrew Morton965c8e52012-12-17 15:59:39 -0800131 * @offset and @whence under i_mutex.
Andi Kleen57604952011-09-15 16:06:50 -0700132 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800133loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
Andi Kleen57604952011-09-15 16:06:50 -0700134{
135 struct inode *inode = file->f_mapping->host;
136
Andrew Morton965c8e52012-12-17 15:59:39 -0800137 return generic_file_llseek_size(file, offset, whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500138 inode->i_sb->s_maxbytes,
139 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
Andi Kleen9465efc2008-06-27 11:05:24 +0200141EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
jan Blunckae6afc32010-05-26 14:44:48 -0700143/**
144 * noop_llseek - No Operation Performed llseek implementation
145 * @file: file structure to seek on
146 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800147 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700148 *
149 * This is an implementation of ->llseek useable for the rare special case when
150 * userspace expects the seek to succeed but the (device) file is actually not
151 * able to perform the seek. In this case you use noop_llseek() instead of
152 * falling back to the default implementation of ->llseek.
153 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800154loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700155{
156 return file->f_pos;
157}
158EXPORT_SYMBOL(noop_llseek);
159
Andrew Morton965c8e52012-12-17 15:59:39 -0800160loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 return -ESPIPE;
163}
164EXPORT_SYMBOL(no_llseek);
165
Andrew Morton965c8e52012-12-17 15:59:39 -0800166loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Al Viro496ad9a2013-01-23 17:07:38 -0500168 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200169 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Josef Bacik982d8162011-07-18 13:21:35 -0400171 mutex_lock(&inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -0800172 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700173 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400174 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700176 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800177 if (offset == 0) {
178 retval = file->f_pos;
179 goto out;
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400182 break;
183 case SEEK_DATA:
184 /*
185 * In the generic case the entire file is data, so as
186 * long as offset isn't at the end of the file then the
187 * offset is data.
188 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300189 if (offset >= inode->i_size) {
190 retval = -ENXIO;
191 goto out;
192 }
Josef Bacik982d8162011-07-18 13:21:35 -0400193 break;
194 case SEEK_HOLE:
195 /*
196 * There is a virtual hole at the end of the file, so
197 * as long as offset isn't i_size or larger, return
198 * i_size.
199 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300200 if (offset >= inode->i_size) {
201 retval = -ENXIO;
202 goto out;
203 }
Josef Bacik982d8162011-07-18 13:21:35 -0400204 offset = inode->i_size;
205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500208 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (offset != file->f_pos) {
210 file->f_pos = offset;
211 file->f_version = 0;
212 }
213 retval = offset;
214 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800215out:
Josef Bacik982d8162011-07-18 13:21:35 -0400216 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return retval;
218}
219EXPORT_SYMBOL(default_llseek);
220
Andrew Morton965c8e52012-12-17 15:59:39 -0800221loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 loff_t (*fn)(struct file *, loff_t, int);
224
225 fn = no_llseek;
226 if (file->f_mode & FMODE_LSEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (file->f_op && file->f_op->llseek)
228 fn = file->f_op->llseek;
229 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800230 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232EXPORT_SYMBOL(vfs_llseek);
233
Andrew Morton965c8e52012-12-17 15:59:39 -0800234SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 off_t retval;
Al Viro2903ff02012-08-28 12:52:22 -0400237 struct fd f = fdget(fd);
238 if (!f.file)
239 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800242 if (whence <= SEEK_MAX) {
243 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 retval = res;
245 if (res != (loff_t)retval)
246 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
247 }
Al Viro2903ff02012-08-28 12:52:22 -0400248 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return retval;
250}
251
Al Viro561c6732013-02-24 10:52:26 -0500252#ifdef CONFIG_COMPAT
253COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
254{
255 return sys_lseek(fd, offset, whence);
256}
257#endif
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100260SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
261 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800262 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 int retval;
Al Viro2903ff02012-08-28 12:52:22 -0400265 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Al Viro2903ff02012-08-28 12:52:22 -0400268 if (!f.file)
269 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800272 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 goto out_putf;
274
Al Viro2903ff02012-08-28 12:52:22 -0400275 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800276 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 retval = (int)offset;
279 if (offset >= 0) {
280 retval = -EFAULT;
281 if (!copy_to_user(result, &offset, sizeof(offset)))
282 retval = 0;
283 }
284out_putf:
Al Viro2903ff02012-08-28 12:52:22 -0400285 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return retval;
287}
288#endif
289
Linus Torvaldse28cc712006-01-04 16:20:40 -0800290/*
291 * rw_verify_area doesn't like huge counts. We limit
292 * them to something that fits in "int" so that others
293 * won't have to do range checks all the time.
294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
296{
297 struct inode *inode;
298 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100299 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Al Viro496ad9a2013-01-23 17:07:38 -0500301 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800302 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100303 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500305 if (unlikely(pos < 0)) {
306 if (!unsigned_offsets(file))
307 return retval;
308 if (count >= -pos) /* both values are in 0..LLONG_MAX */
309 return -EOVERFLOW;
310 } else if (unlikely((loff_t) (pos + count) < 0)) {
311 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700312 return retval;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Pavel Emelyanova16877c2007-10-01 14:41:11 -0700315 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
James Morrisc43e2592008-01-12 22:05:48 +1100316 retval = locks_mandatory_area(
Linus Torvaldse28cc712006-01-04 16:20:40 -0800317 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
318 inode, file, pos, count);
319 if (retval < 0)
320 return retval;
321 }
James Morrisc43e2592008-01-12 22:05:48 +1100322 retval = security_file_permission(file,
323 read_write == READ ? MAY_READ : MAY_WRITE);
324 if (retval)
325 return retval;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800326 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700329static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
330{
331 set_current_state(TASK_UNINTERRUPTIBLE);
332 if (!kiocbIsKicked(iocb))
333 schedule();
334 else
335 kiocbClearKicked(iocb);
336 __set_current_state(TASK_RUNNING);
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
340{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700341 struct iovec iov = { .iov_base = buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct kiocb kiocb;
343 ssize_t ret;
344
345 init_sync_kiocb(&kiocb, filp);
346 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700347 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000348 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700349
350 for (;;) {
351 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
352 if (ret != -EIOCBRETRY)
353 break;
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700354 wait_on_retry_sync_kiocb(&kiocb);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700355 }
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (-EIOCBQUEUED == ret)
358 ret = wait_on_sync_kiocb(&kiocb);
359 *ppos = kiocb.ki_pos;
360 return ret;
361}
362
363EXPORT_SYMBOL(do_sync_read);
364
365ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
366{
367 ssize_t ret;
368
369 if (!(file->f_mode & FMODE_READ))
370 return -EBADF;
371 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
372 return -EINVAL;
373 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
374 return -EFAULT;
375
376 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800377 if (ret >= 0) {
378 count = ret;
James Morrisc43e2592008-01-12 22:05:48 +1100379 if (file->f_op->read)
380 ret = file->f_op->read(file, buf, count, pos);
381 else
382 ret = do_sync_read(file, buf, count, pos);
383 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500384 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100385 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
James Morrisc43e2592008-01-12 22:05:48 +1100387 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
390 return ret;
391}
392
393EXPORT_SYMBOL(vfs_read);
394
395ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
396{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700397 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct kiocb kiocb;
399 ssize_t ret;
400
Al Viro8d71db42013-03-19 21:01:03 -0400401 file_start_write(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 init_sync_kiocb(&kiocb, filp);
403 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700404 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000405 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700406
407 for (;;) {
408 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
409 if (ret != -EIOCBRETRY)
410 break;
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700411 wait_on_retry_sync_kiocb(&kiocb);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700412 }
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (-EIOCBQUEUED == ret)
415 ret = wait_on_sync_kiocb(&kiocb);
416 *ppos = kiocb.ki_pos;
Al Viro8d71db42013-03-19 21:01:03 -0400417 file_end_write(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return ret;
419}
420
421EXPORT_SYMBOL(do_sync_write);
422
Al Viro06ae43f2013-03-20 13:19:30 -0400423ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
424{
425 mm_segment_t old_fs;
426 const char __user *p;
427 ssize_t ret;
428
429 old_fs = get_fs();
430 set_fs(get_ds());
431 p = (__force const char __user *)buf;
432 if (count > MAX_RW_COUNT)
433 count = MAX_RW_COUNT;
434 if (file->f_op->write)
435 ret = file->f_op->write(file, p, count, pos);
436 else
437 ret = do_sync_write(file, p, count, pos);
438 set_fs(old_fs);
439 if (ret > 0) {
440 fsnotify_modify(file);
441 add_wchar(current, ret);
442 }
443 inc_syscw(current);
444 return ret;
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
448{
449 ssize_t ret;
450
451 if (!(file->f_mode & FMODE_WRITE))
452 return -EBADF;
453 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
454 return -EINVAL;
455 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
456 return -EFAULT;
457
458 ret = rw_verify_area(WRITE, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800459 if (ret >= 0) {
460 count = ret;
James Morrisc43e2592008-01-12 22:05:48 +1100461 if (file->f_op->write)
462 ret = file->f_op->write(file, buf, count, pos);
463 else
464 ret = do_sync_write(file, buf, count, pos);
465 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500466 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100467 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
James Morrisc43e2592008-01-12 22:05:48 +1100469 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
472 return ret;
473}
474
475EXPORT_SYMBOL(vfs_write);
476
477static inline loff_t file_pos_read(struct file *file)
478{
479 return file->f_pos;
480}
481
482static inline void file_pos_write(struct file *file, loff_t pos)
483{
484 file->f_pos = pos;
485}
486
Heiko Carstens3cdad422009-01-14 14:14:22 +0100487SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Al Viro2903ff02012-08-28 12:52:22 -0400489 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Al Viro2903ff02012-08-28 12:52:22 -0400492 if (f.file) {
493 loff_t pos = file_pos_read(f.file);
494 ret = vfs_read(f.file, buf, count, &pos);
495 file_pos_write(f.file, pos);
496 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return ret;
499}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Heiko Carstens3cdad422009-01-14 14:14:22 +0100501SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
502 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Al Viro2903ff02012-08-28 12:52:22 -0400504 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Al Viro2903ff02012-08-28 12:52:22 -0400507 if (f.file) {
508 loff_t pos = file_pos_read(f.file);
509 ret = vfs_write(f.file, buf, count, &pos);
510 file_pos_write(f.file, pos);
511 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
514 return ret;
515}
516
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100517SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
518 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Al Viro2903ff02012-08-28 12:52:22 -0400520 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 if (pos < 0)
524 return -EINVAL;
525
Al Viro2903ff02012-08-28 12:52:22 -0400526 f = fdget(fd);
527 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400529 if (f.file->f_mode & FMODE_PREAD)
530 ret = vfs_read(f.file, buf, count, &pos);
531 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
534 return ret;
535}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100536#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
537asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
538{
539 return SYSC_pread64((unsigned int) fd, (char __user *) buf,
540 (size_t) count, pos);
541}
542SYSCALL_ALIAS(sys_pread64, SyS_pread64);
543#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100545SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
546 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Al Viro2903ff02012-08-28 12:52:22 -0400548 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (pos < 0)
552 return -EINVAL;
553
Al Viro2903ff02012-08-28 12:52:22 -0400554 f = fdget(fd);
555 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400557 if (f.file->f_mode & FMODE_PWRITE)
558 ret = vfs_write(f.file, buf, count, &pos);
559 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
562 return ret;
563}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100564#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
565asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
566{
567 return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
568 (size_t) count, pos);
569}
570SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
571#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573/*
574 * Reduce an iovec's length in-place. Return the resulting number of segments
575 */
576unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
577{
578 unsigned long seg = 0;
579 size_t len = 0;
580
581 while (seg < nr_segs) {
582 seg++;
583 if (len + iov->iov_len >= to) {
584 iov->iov_len = to - len;
585 break;
586 }
587 len += iov->iov_len;
588 iov++;
589 }
590 return seg;
591}
Eric Sandeen19295522008-01-28 23:58:27 -0500592EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Al Viro72ec3512013-03-20 10:42:10 -0400594static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700595 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
596{
597 struct kiocb kiocb;
598 ssize_t ret;
599
600 init_sync_kiocb(&kiocb, filp);
601 kiocb.ki_pos = *ppos;
602 kiocb.ki_left = len;
603 kiocb.ki_nbytes = len;
604
605 for (;;) {
606 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
607 if (ret != -EIOCBRETRY)
608 break;
609 wait_on_retry_sync_kiocb(&kiocb);
610 }
611
612 if (ret == -EIOCBQUEUED)
613 ret = wait_on_sync_kiocb(&kiocb);
614 *ppos = kiocb.ki_pos;
615 return ret;
616}
617
618/* Do it by hand, with file-ops */
Al Viro72ec3512013-03-20 10:42:10 -0400619static ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700620 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
621{
622 struct iovec *vector = iov;
623 ssize_t ret = 0;
624
625 while (nr_segs > 0) {
626 void __user *base;
627 size_t len;
628 ssize_t nr;
629
630 base = vector->iov_base;
631 len = vector->iov_len;
632 vector++;
633 nr_segs--;
634
635 nr = fn(filp, base, len, ppos);
636
637 if (nr < 0) {
638 if (!ret)
639 ret = nr;
640 break;
641 }
642 ret += nr;
643 if (nr != len)
644 break;
645 }
646
647 return ret;
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650/* A write operation does a read from user space and vice versa */
651#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
652
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700653ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
654 unsigned long nr_segs, unsigned long fast_segs,
655 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700656 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700657{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700658 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700659 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700660 struct iovec *iov = fast_pointer;
661
Linus Torvalds435f49a2010-10-29 10:36:49 -0700662 /*
663 * SuS says "The readv() function *may* fail if the iovcnt argument
664 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
665 * traditionally returned zero for zero segments, so...
666 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700667 if (nr_segs == 0) {
668 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700669 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700670 }
671
Linus Torvalds435f49a2010-10-29 10:36:49 -0700672 /*
673 * First get the "struct iovec" from user memory and
674 * verify all the pointers
675 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700676 if (nr_segs > UIO_MAXIOV) {
677 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700678 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700679 }
680 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700681 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700682 if (iov == NULL) {
683 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700684 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700685 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700686 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700687 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
688 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700689 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700690 }
691
Linus Torvalds435f49a2010-10-29 10:36:49 -0700692 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700693 * According to the Single Unix Specification we should return EINVAL
694 * if an element length is < 0 when cast to ssize_t or if the
695 * total length would overflow the ssize_t return value of the
696 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700697 *
698 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
699 * overflow case.
700 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700701 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700702 for (seg = 0; seg < nr_segs; seg++) {
703 void __user *buf = iov[seg].iov_base;
704 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700705
706 /* see if we we're about to use an invalid len or if
707 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700708 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700709 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700710 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700711 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700712 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700713 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700714 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700715 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700716 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700717 if (len > MAX_RW_COUNT - ret) {
718 len = MAX_RW_COUNT - ret;
719 iov[seg].iov_len = len;
720 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700721 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700722 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700723out:
724 *ret_pointer = iov;
725 return ret;
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728static ssize_t do_readv_writev(int type, struct file *file,
729 const struct iovec __user * uvector,
730 unsigned long nr_segs, loff_t *pos)
731{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 size_t tot_len;
733 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700734 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 io_fn_t fn;
737 iov_fn_t fnv;
738
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700739 if (!file->f_op) {
740 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 goto out;
742 }
743
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700744 ret = rw_copy_check_uvector(type, uvector, nr_segs,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700745 ARRAY_SIZE(iovstack), iovstack, &iov);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700746 if (ret <= 0)
747 goto out;
748
749 tot_len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800751 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 goto out;
753
754 fnv = NULL;
755 if (type == READ) {
756 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700757 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 } else {
759 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700760 fnv = file->f_op->aio_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
Al Viro8d71db42013-03-19 21:01:03 -0400763 if (fnv) {
764 file_start_write(file);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700765 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
766 pos, fnv);
Al Viro8d71db42013-03-19 21:01:03 -0400767 file_end_write(file);
768 } else
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700769 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771out:
772 if (iov != iovstack)
773 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400774 if ((ret + (type == READ)) > 0) {
775 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500776 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400777 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500778 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
784 unsigned long vlen, loff_t *pos)
785{
786 if (!(file->f_mode & FMODE_READ))
787 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700788 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return -EINVAL;
790
791 return do_readv_writev(READ, file, vec, vlen, pos);
792}
793
794EXPORT_SYMBOL(vfs_readv);
795
796ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
797 unsigned long vlen, loff_t *pos)
798{
799 if (!(file->f_mode & FMODE_WRITE))
800 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700801 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return -EINVAL;
803
804 return do_readv_writev(WRITE, file, vec, vlen, pos);
805}
806
807EXPORT_SYMBOL(vfs_writev);
808
Heiko Carstens3cdad422009-01-14 14:14:22 +0100809SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
810 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Al Viro2903ff02012-08-28 12:52:22 -0400812 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Al Viro2903ff02012-08-28 12:52:22 -0400815 if (f.file) {
816 loff_t pos = file_pos_read(f.file);
817 ret = vfs_readv(f.file, vec, vlen, &pos);
818 file_pos_write(f.file, pos);
819 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
822 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800823 add_rchar(current, ret);
824 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return ret;
826}
827
Heiko Carstens3cdad422009-01-14 14:14:22 +0100828SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
829 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Al Viro2903ff02012-08-28 12:52:22 -0400831 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Al Viro2903ff02012-08-28 12:52:22 -0400834 if (f.file) {
835 loff_t pos = file_pos_read(f.file);
836 ret = vfs_writev(f.file, vec, vlen, &pos);
837 file_pos_write(f.file, pos);
838 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
841 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800842 add_wchar(current, ret);
843 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return ret;
845}
846
Linus Torvalds601cc112009-04-03 08:03:22 -0700847static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700848{
Linus Torvalds601cc112009-04-03 08:03:22 -0700849#define HALF_LONG_BITS (BITS_PER_LONG / 2)
850 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
851}
852
853SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
854 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
855{
856 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400857 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700858 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700859
860 if (pos < 0)
861 return -EINVAL;
862
Al Viro2903ff02012-08-28 12:52:22 -0400863 f = fdget(fd);
864 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700865 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400866 if (f.file->f_mode & FMODE_PREAD)
867 ret = vfs_readv(f.file, vec, vlen, &pos);
868 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700869 }
870
871 if (ret > 0)
872 add_rchar(current, ret);
873 inc_syscr(current);
874 return ret;
875}
876
877SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700878 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700879{
Linus Torvalds601cc112009-04-03 08:03:22 -0700880 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400881 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700882 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700883
884 if (pos < 0)
885 return -EINVAL;
886
Al Viro2903ff02012-08-28 12:52:22 -0400887 f = fdget(fd);
888 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700889 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400890 if (f.file->f_mode & FMODE_PWRITE)
891 ret = vfs_writev(f.file, vec, vlen, &pos);
892 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700893 }
894
895 if (ret > 0)
896 add_wchar(current, ret);
897 inc_syscw(current);
898 return ret;
899}
900
Al Viro72ec3512013-03-20 10:42:10 -0400901#ifdef CONFIG_COMPAT
902
903static ssize_t compat_do_readv_writev(int type, struct file *file,
904 const struct compat_iovec __user *uvector,
905 unsigned long nr_segs, loff_t *pos)
906{
907 compat_ssize_t tot_len;
908 struct iovec iovstack[UIO_FASTIOV];
909 struct iovec *iov = iovstack;
910 ssize_t ret;
911 io_fn_t fn;
912 iov_fn_t fnv;
913
914 ret = -EINVAL;
915 if (!file->f_op)
916 goto out;
917
918 ret = -EFAULT;
919 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
920 goto out;
921
922 ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
923 UIO_FASTIOV, iovstack, &iov);
924 if (ret <= 0)
925 goto out;
926
927 tot_len = ret;
928 ret = rw_verify_area(type, file, pos, tot_len);
929 if (ret < 0)
930 goto out;
931
932 fnv = NULL;
933 if (type == READ) {
934 fn = file->f_op->read;
935 fnv = file->f_op->aio_read;
936 } else {
937 fn = (io_fn_t)file->f_op->write;
938 fnv = file->f_op->aio_write;
939 }
940
941 if (fnv) {
942 file_start_write(file);
943 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
944 pos, fnv);
945 file_end_write(file);
946 } else
947 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
948
949out:
950 if (iov != iovstack)
951 kfree(iov);
952 if ((ret + (type == READ)) > 0) {
953 if (type == READ)
954 fsnotify_access(file);
955 else
956 fsnotify_modify(file);
957 }
958 return ret;
959}
960
961static size_t compat_readv(struct file *file,
962 const struct compat_iovec __user *vec,
963 unsigned long vlen, loff_t *pos)
964{
965 ssize_t ret = -EBADF;
966
967 if (!(file->f_mode & FMODE_READ))
968 goto out;
969
970 ret = -EINVAL;
971 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
972 goto out;
973
974 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
975
976out:
977 if (ret > 0)
978 add_rchar(current, ret);
979 inc_syscr(current);
980 return ret;
981}
982
983COMPAT_SYSCALL_DEFINE3(readv, unsigned long, fd,
984 const struct compat_iovec __user *,vec,
985 unsigned long, vlen)
986{
987 struct fd f = fdget(fd);
988 ssize_t ret;
989 loff_t pos;
990
991 if (!f.file)
992 return -EBADF;
993 pos = f.file->f_pos;
994 ret = compat_readv(f.file, vec, vlen, &pos);
995 f.file->f_pos = pos;
996 fdput(f);
997 return ret;
998}
999
1000COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1001 const struct compat_iovec __user *,vec,
1002 unsigned long, vlen, loff_t, pos)
1003{
1004 struct fd f;
1005 ssize_t ret;
1006
1007 if (pos < 0)
1008 return -EINVAL;
1009 f = fdget(fd);
1010 if (!f.file)
1011 return -EBADF;
1012 ret = -ESPIPE;
1013 if (f.file->f_mode & FMODE_PREAD)
1014 ret = compat_readv(f.file, vec, vlen, &pos);
1015 fdput(f);
1016 return ret;
1017}
1018
1019COMPAT_SYSCALL_DEFINE5(preadv, unsigned long, fd,
1020 const struct compat_iovec __user *,vec,
1021 unsigned long, vlen, u32, pos_low, u32, pos_high)
1022{
1023 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1024 return compat_sys_preadv64(fd, vec, vlen, pos);
1025}
1026
1027static size_t compat_writev(struct file *file,
1028 const struct compat_iovec __user *vec,
1029 unsigned long vlen, loff_t *pos)
1030{
1031 ssize_t ret = -EBADF;
1032
1033 if (!(file->f_mode & FMODE_WRITE))
1034 goto out;
1035
1036 ret = -EINVAL;
1037 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
1038 goto out;
1039
1040 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1041
1042out:
1043 if (ret > 0)
1044 add_wchar(current, ret);
1045 inc_syscw(current);
1046 return ret;
1047}
1048
1049COMPAT_SYSCALL_DEFINE3(writev, unsigned long, fd,
1050 const struct compat_iovec __user *, vec,
1051 unsigned long, vlen)
1052{
1053 struct fd f = fdget(fd);
1054 ssize_t ret;
1055 loff_t pos;
1056
1057 if (!f.file)
1058 return -EBADF;
1059 pos = f.file->f_pos;
1060 ret = compat_writev(f.file, vec, vlen, &pos);
1061 f.file->f_pos = pos;
1062 fdput(f);
1063 return ret;
1064}
1065
1066COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1067 const struct compat_iovec __user *,vec,
1068 unsigned long, vlen, loff_t, pos)
1069{
1070 struct fd f;
1071 ssize_t ret;
1072
1073 if (pos < 0)
1074 return -EINVAL;
1075 f = fdget(fd);
1076 if (!f.file)
1077 return -EBADF;
1078 ret = -ESPIPE;
1079 if (f.file->f_mode & FMODE_PWRITE)
1080 ret = compat_writev(f.file, vec, vlen, &pos);
1081 fdput(f);
1082 return ret;
1083}
1084
1085COMPAT_SYSCALL_DEFINE5(pwritev, unsigned long, fd,
1086 const struct compat_iovec __user *,vec,
1087 unsigned long, vlen, u32, pos_low, u32, pos_high)
1088{
1089 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1090 return compat_sys_pwritev64(fd, vec, vlen, pos);
1091}
1092#endif
1093
Catalin Marinas8f9c0112012-09-19 12:01:52 +01001094ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, size_t count,
1095 loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Al Viro2903ff02012-08-28 12:52:22 -04001097 struct fd in, out;
1098 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 loff_t pos;
1100 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001101 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /*
1104 * Get input file, and verify that it is ok..
1105 */
1106 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001107 in = fdget(in_fd);
1108 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001110 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 retval = -ESPIPE;
1113 if (!ppos)
Al Viro2903ff02012-08-28 12:52:22 -04001114 ppos = &in.file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 else
Al Viro2903ff02012-08-28 12:52:22 -04001116 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001118 retval = rw_verify_area(READ, in.file, ppos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001119 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001121 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /*
1124 * Get output file, and verify that it is ok..
1125 */
1126 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001127 out = fdget(out_fd);
1128 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001130 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 goto fput_out;
1132 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001133 in_inode = file_inode(in.file);
1134 out_inode = file_inode(out.file);
Al Viro2903ff02012-08-28 12:52:22 -04001135 retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001136 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001138 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (!max)
1141 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1142
1143 pos = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (unlikely(pos + count > max)) {
1145 retval = -EOVERFLOW;
1146 if (pos >= max)
1147 goto fput_out;
1148 count = max - pos;
1149 }
1150
Jens Axboed96e6e72007-06-11 12:18:52 +02001151 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001152#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001153 /*
1154 * We need to debate whether we can enable this or not. The
1155 * man page documents EAGAIN return for the output at least,
1156 * and the application is arguably buggy if it doesn't expect
1157 * EAGAIN on a non-blocking file descriptor.
1158 */
Al Viro2903ff02012-08-28 12:52:22 -04001159 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001160 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001161#endif
Al Viro2903ff02012-08-28 12:52:22 -04001162 retval = do_splice_direct(in.file, ppos, out.file, count, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001165 add_rchar(current, retval);
1166 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001167 fsnotify_access(in.file);
1168 fsnotify_modify(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001171 inc_syscr(current);
1172 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (*ppos > max)
1174 retval = -EOVERFLOW;
1175
1176fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001177 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001179 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180out:
1181 return retval;
1182}
1183
Heiko Carstens002c8972009-01-14 14:14:18 +01001184SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 loff_t pos;
1187 off_t off;
1188 ssize_t ret;
1189
1190 if (offset) {
1191 if (unlikely(get_user(off, offset)))
1192 return -EFAULT;
1193 pos = off;
1194 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
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}
1202
Heiko Carstens002c8972009-01-14 14:14:18 +01001203SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 loff_t pos;
1206 ssize_t ret;
1207
1208 if (offset) {
1209 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1210 return -EFAULT;
1211 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1212 if (unlikely(put_user(pos, offset)))
1213 return -EFAULT;
1214 return ret;
1215 }
1216
1217 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1218}