blob: 90ba3b350e5063daa547f35814fd5e4c8c660a4f [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
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700332static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
333{
334 set_current_state(TASK_UNINTERRUPTIBLE);
335 if (!kiocbIsKicked(iocb))
336 schedule();
337 else
338 kiocbClearKicked(iocb);
339 __set_current_state(TASK_RUNNING);
340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
343{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700344 struct iovec iov = { .iov_base = buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct kiocb kiocb;
346 ssize_t ret;
347
348 init_sync_kiocb(&kiocb, filp);
349 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700350 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000351 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700352
353 for (;;) {
354 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
355 if (ret != -EIOCBRETRY)
356 break;
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700357 wait_on_retry_sync_kiocb(&kiocb);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700358 }
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (-EIOCBQUEUED == ret)
361 ret = wait_on_sync_kiocb(&kiocb);
362 *ppos = kiocb.ki_pos;
363 return ret;
364}
365
366EXPORT_SYMBOL(do_sync_read);
367
368ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
369{
370 ssize_t ret;
371
372 if (!(file->f_mode & FMODE_READ))
373 return -EBADF;
374 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
375 return -EINVAL;
376 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
377 return -EFAULT;
378
379 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800380 if (ret >= 0) {
381 count = ret;
James Morrisc43e2592008-01-12 22:05:48 +1100382 if (file->f_op->read)
383 ret = file->f_op->read(file, buf, count, pos);
384 else
385 ret = do_sync_read(file, buf, count, pos);
386 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500387 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100388 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
James Morrisc43e2592008-01-12 22:05:48 +1100390 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 return ret;
394}
395
396EXPORT_SYMBOL(vfs_read);
397
398ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
399{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700400 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct kiocb kiocb;
402 ssize_t ret;
403
404 init_sync_kiocb(&kiocb, filp);
405 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700406 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000407 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700408
409 for (;;) {
410 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
411 if (ret != -EIOCBRETRY)
412 break;
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700413 wait_on_retry_sync_kiocb(&kiocb);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700414 }
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (-EIOCBQUEUED == ret)
417 ret = wait_on_sync_kiocb(&kiocb);
418 *ppos = kiocb.ki_pos;
419 return ret;
420}
421
422EXPORT_SYMBOL(do_sync_write);
423
Al Viro06ae43f2013-03-20 13:19:30 -0400424ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
425{
426 mm_segment_t old_fs;
427 const char __user *p;
428 ssize_t ret;
429
Al Viro3e84f482013-03-27 15:20:30 +0000430 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
431 return -EINVAL;
432
Al Viro06ae43f2013-03-20 13:19:30 -0400433 old_fs = get_fs();
434 set_fs(get_ds());
435 p = (__force const char __user *)buf;
436 if (count > MAX_RW_COUNT)
437 count = MAX_RW_COUNT;
438 if (file->f_op->write)
439 ret = file->f_op->write(file, p, count, pos);
440 else
441 ret = do_sync_write(file, p, count, pos);
442 set_fs(old_fs);
443 if (ret > 0) {
444 fsnotify_modify(file);
445 add_wchar(current, ret);
446 }
447 inc_syscw(current);
448 return ret;
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
452{
453 ssize_t ret;
454
455 if (!(file->f_mode & FMODE_WRITE))
456 return -EBADF;
457 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
458 return -EINVAL;
459 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
460 return -EFAULT;
461
462 ret = rw_verify_area(WRITE, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800463 if (ret >= 0) {
464 count = ret;
Al Viro03d95eb2013-03-20 13:04:20 -0400465 file_start_write(file);
James Morrisc43e2592008-01-12 22:05:48 +1100466 if (file->f_op->write)
467 ret = file->f_op->write(file, buf, count, pos);
468 else
469 ret = do_sync_write(file, buf, count, pos);
470 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500471 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100472 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
James Morrisc43e2592008-01-12 22:05:48 +1100474 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400475 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477
478 return ret;
479}
480
481EXPORT_SYMBOL(vfs_write);
482
483static inline loff_t file_pos_read(struct file *file)
484{
485 return file->f_pos;
486}
487
488static inline void file_pos_write(struct file *file, loff_t pos)
489{
490 file->f_pos = pos;
491}
492
Heiko Carstens3cdad422009-01-14 14:14:22 +0100493SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Al Viro2903ff02012-08-28 12:52:22 -0400495 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Al Viro2903ff02012-08-28 12:52:22 -0400498 if (f.file) {
499 loff_t pos = file_pos_read(f.file);
500 ret = vfs_read(f.file, buf, count, &pos);
501 file_pos_write(f.file, pos);
502 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return ret;
505}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Heiko Carstens3cdad422009-01-14 14:14:22 +0100507SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
508 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Al Viro2903ff02012-08-28 12:52:22 -0400510 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Al Viro2903ff02012-08-28 12:52:22 -0400513 if (f.file) {
514 loff_t pos = file_pos_read(f.file);
515 ret = vfs_write(f.file, buf, count, &pos);
516 file_pos_write(f.file, pos);
517 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
520 return ret;
521}
522
Al Viro4a0fd5b2013-01-21 15:16:58 -0500523SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
524 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Al Viro2903ff02012-08-28 12:52:22 -0400526 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (pos < 0)
530 return -EINVAL;
531
Al Viro2903ff02012-08-28 12:52:22 -0400532 f = fdget(fd);
533 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400535 if (f.file->f_mode & FMODE_PREAD)
536 ret = vfs_read(f.file, buf, count, &pos);
537 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 return ret;
541}
542
Al Viro4a0fd5b2013-01-21 15:16:58 -0500543SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
544 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Al Viro2903ff02012-08-28 12:52:22 -0400546 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if (pos < 0)
550 return -EINVAL;
551
Al Viro2903ff02012-08-28 12:52:22 -0400552 f = fdget(fd);
553 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400555 if (f.file->f_mode & FMODE_PWRITE)
556 ret = vfs_write(f.file, buf, count, &pos);
557 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
560 return ret;
561}
562
563/*
564 * Reduce an iovec's length in-place. Return the resulting number of segments
565 */
566unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
567{
568 unsigned long seg = 0;
569 size_t len = 0;
570
571 while (seg < nr_segs) {
572 seg++;
573 if (len + iov->iov_len >= to) {
574 iov->iov_len = to - len;
575 break;
576 }
577 len += iov->iov_len;
578 iov++;
579 }
580 return seg;
581}
Eric Sandeen19295522008-01-28 23:58:27 -0500582EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Al Viro72ec3512013-03-20 10:42:10 -0400584static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700585 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
586{
587 struct kiocb kiocb;
588 ssize_t ret;
589
590 init_sync_kiocb(&kiocb, filp);
591 kiocb.ki_pos = *ppos;
592 kiocb.ki_left = len;
593 kiocb.ki_nbytes = len;
594
595 for (;;) {
596 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
597 if (ret != -EIOCBRETRY)
598 break;
599 wait_on_retry_sync_kiocb(&kiocb);
600 }
601
602 if (ret == -EIOCBQUEUED)
603 ret = wait_on_sync_kiocb(&kiocb);
604 *ppos = kiocb.ki_pos;
605 return ret;
606}
607
608/* Do it by hand, with file-ops */
Al Viro72ec3512013-03-20 10:42:10 -0400609static ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700610 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
611{
612 struct iovec *vector = iov;
613 ssize_t ret = 0;
614
615 while (nr_segs > 0) {
616 void __user *base;
617 size_t len;
618 ssize_t nr;
619
620 base = vector->iov_base;
621 len = vector->iov_len;
622 vector++;
623 nr_segs--;
624
625 nr = fn(filp, base, len, ppos);
626
627 if (nr < 0) {
628 if (!ret)
629 ret = nr;
630 break;
631 }
632 ret += nr;
633 if (nr != len)
634 break;
635 }
636
637 return ret;
638}
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640/* A write operation does a read from user space and vice versa */
641#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
642
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700643ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
644 unsigned long nr_segs, unsigned long fast_segs,
645 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700646 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700647{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700648 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700649 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700650 struct iovec *iov = fast_pointer;
651
Linus Torvalds435f49a2010-10-29 10:36:49 -0700652 /*
653 * SuS says "The readv() function *may* fail if the iovcnt argument
654 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
655 * traditionally returned zero for zero segments, so...
656 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700657 if (nr_segs == 0) {
658 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700659 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700660 }
661
Linus Torvalds435f49a2010-10-29 10:36:49 -0700662 /*
663 * First get the "struct iovec" from user memory and
664 * verify all the pointers
665 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700666 if (nr_segs > UIO_MAXIOV) {
667 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700668 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700669 }
670 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700671 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700672 if (iov == NULL) {
673 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700674 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700675 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700676 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700677 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
678 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700679 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700680 }
681
Linus Torvalds435f49a2010-10-29 10:36:49 -0700682 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700683 * According to the Single Unix Specification we should return EINVAL
684 * if an element length is < 0 when cast to ssize_t or if the
685 * total length would overflow the ssize_t return value of the
686 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700687 *
688 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
689 * overflow case.
690 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700691 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700692 for (seg = 0; seg < nr_segs; seg++) {
693 void __user *buf = iov[seg].iov_base;
694 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700695
696 /* see if we we're about to use an invalid len or if
697 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700698 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700699 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700700 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700701 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700702 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700703 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700704 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700705 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700706 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700707 if (len > MAX_RW_COUNT - ret) {
708 len = MAX_RW_COUNT - ret;
709 iov[seg].iov_len = len;
710 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700711 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700712 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700713out:
714 *ret_pointer = iov;
715 return ret;
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static ssize_t do_readv_writev(int type, struct file *file,
719 const struct iovec __user * uvector,
720 unsigned long nr_segs, loff_t *pos)
721{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 size_t tot_len;
723 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700724 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 io_fn_t fn;
727 iov_fn_t fnv;
728
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700729 if (!file->f_op) {
730 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto out;
732 }
733
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700734 ret = rw_copy_check_uvector(type, uvector, nr_segs,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700735 ARRAY_SIZE(iovstack), iovstack, &iov);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700736 if (ret <= 0)
737 goto out;
738
739 tot_len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800741 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 goto out;
743
744 fnv = NULL;
745 if (type == READ) {
746 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700747 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 } else {
749 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700750 fnv = file->f_op->aio_write;
Al Viro03d95eb2013-03-20 13:04:20 -0400751 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700754 if (fnv)
755 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
756 pos, fnv);
757 else
758 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Al Viro03d95eb2013-03-20 13:04:20 -0400760 if (type != READ)
761 file_end_write(file);
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763out:
764 if (iov != iovstack)
765 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400766 if ((ret + (type == READ)) > 0) {
767 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500768 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400769 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500770 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
776 unsigned long vlen, loff_t *pos)
777{
778 if (!(file->f_mode & FMODE_READ))
779 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700780 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -EINVAL;
782
783 return do_readv_writev(READ, file, vec, vlen, pos);
784}
785
786EXPORT_SYMBOL(vfs_readv);
787
788ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
789 unsigned long vlen, loff_t *pos)
790{
791 if (!(file->f_mode & FMODE_WRITE))
792 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700793 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return -EINVAL;
795
796 return do_readv_writev(WRITE, file, vec, vlen, pos);
797}
798
799EXPORT_SYMBOL(vfs_writev);
800
Heiko Carstens3cdad422009-01-14 14:14:22 +0100801SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
802 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Al Viro2903ff02012-08-28 12:52:22 -0400804 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Al Viro2903ff02012-08-28 12:52:22 -0400807 if (f.file) {
808 loff_t pos = file_pos_read(f.file);
809 ret = vfs_readv(f.file, vec, vlen, &pos);
810 file_pos_write(f.file, pos);
811 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
814 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800815 add_rchar(current, ret);
816 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return ret;
818}
819
Heiko Carstens3cdad422009-01-14 14:14:22 +0100820SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
821 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Al Viro2903ff02012-08-28 12:52:22 -0400823 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Al Viro2903ff02012-08-28 12:52:22 -0400826 if (f.file) {
827 loff_t pos = file_pos_read(f.file);
828 ret = vfs_writev(f.file, vec, vlen, &pos);
829 file_pos_write(f.file, pos);
830 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800834 add_wchar(current, ret);
835 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return ret;
837}
838
Linus Torvalds601cc112009-04-03 08:03:22 -0700839static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700840{
Linus Torvalds601cc112009-04-03 08:03:22 -0700841#define HALF_LONG_BITS (BITS_PER_LONG / 2)
842 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
843}
844
845SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
846 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
847{
848 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400849 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700850 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700851
852 if (pos < 0)
853 return -EINVAL;
854
Al Viro2903ff02012-08-28 12:52:22 -0400855 f = fdget(fd);
856 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700857 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400858 if (f.file->f_mode & FMODE_PREAD)
859 ret = vfs_readv(f.file, vec, vlen, &pos);
860 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700861 }
862
863 if (ret > 0)
864 add_rchar(current, ret);
865 inc_syscr(current);
866 return ret;
867}
868
869SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700870 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700871{
Linus Torvalds601cc112009-04-03 08:03:22 -0700872 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400873 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700874 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700875
876 if (pos < 0)
877 return -EINVAL;
878
Al Viro2903ff02012-08-28 12:52:22 -0400879 f = fdget(fd);
880 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700881 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400882 if (f.file->f_mode & FMODE_PWRITE)
883 ret = vfs_writev(f.file, vec, vlen, &pos);
884 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700885 }
886
887 if (ret > 0)
888 add_wchar(current, ret);
889 inc_syscw(current);
890 return ret;
891}
892
Al Viro72ec3512013-03-20 10:42:10 -0400893#ifdef CONFIG_COMPAT
894
895static ssize_t compat_do_readv_writev(int type, struct file *file,
896 const struct compat_iovec __user *uvector,
897 unsigned long nr_segs, loff_t *pos)
898{
899 compat_ssize_t tot_len;
900 struct iovec iovstack[UIO_FASTIOV];
901 struct iovec *iov = iovstack;
902 ssize_t ret;
903 io_fn_t fn;
904 iov_fn_t fnv;
905
906 ret = -EINVAL;
907 if (!file->f_op)
908 goto out;
909
910 ret = -EFAULT;
911 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
912 goto out;
913
914 ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
915 UIO_FASTIOV, iovstack, &iov);
916 if (ret <= 0)
917 goto out;
918
919 tot_len = ret;
920 ret = rw_verify_area(type, file, pos, tot_len);
921 if (ret < 0)
922 goto out;
923
924 fnv = NULL;
925 if (type == READ) {
926 fn = file->f_op->read;
927 fnv = file->f_op->aio_read;
928 } else {
929 fn = (io_fn_t)file->f_op->write;
930 fnv = file->f_op->aio_write;
Al Viro03d95eb2013-03-20 13:04:20 -0400931 file_start_write(file);
Al Viro72ec3512013-03-20 10:42:10 -0400932 }
933
Al Viro03d95eb2013-03-20 13:04:20 -0400934 if (fnv)
Al Viro72ec3512013-03-20 10:42:10 -0400935 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
936 pos, fnv);
Al Viro03d95eb2013-03-20 13:04:20 -0400937 else
Al Viro72ec3512013-03-20 10:42:10 -0400938 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
939
Al Viro03d95eb2013-03-20 13:04:20 -0400940 if (type != READ)
941 file_end_write(file);
942
Al Viro72ec3512013-03-20 10:42:10 -0400943out:
944 if (iov != iovstack)
945 kfree(iov);
946 if ((ret + (type == READ)) > 0) {
947 if (type == READ)
948 fsnotify_access(file);
949 else
950 fsnotify_modify(file);
951 }
952 return ret;
953}
954
955static size_t compat_readv(struct file *file,
956 const struct compat_iovec __user *vec,
957 unsigned long vlen, loff_t *pos)
958{
959 ssize_t ret = -EBADF;
960
961 if (!(file->f_mode & FMODE_READ))
962 goto out;
963
964 ret = -EINVAL;
965 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
966 goto out;
967
968 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
969
970out:
971 if (ret > 0)
972 add_rchar(current, ret);
973 inc_syscr(current);
974 return ret;
975}
976
977COMPAT_SYSCALL_DEFINE3(readv, unsigned long, fd,
978 const struct compat_iovec __user *,vec,
979 unsigned long, vlen)
980{
981 struct fd f = fdget(fd);
982 ssize_t ret;
983 loff_t pos;
984
985 if (!f.file)
986 return -EBADF;
987 pos = f.file->f_pos;
988 ret = compat_readv(f.file, vec, vlen, &pos);
989 f.file->f_pos = pos;
990 fdput(f);
991 return ret;
992}
993
994COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
995 const struct compat_iovec __user *,vec,
996 unsigned long, vlen, loff_t, pos)
997{
998 struct fd f;
999 ssize_t ret;
1000
1001 if (pos < 0)
1002 return -EINVAL;
1003 f = fdget(fd);
1004 if (!f.file)
1005 return -EBADF;
1006 ret = -ESPIPE;
1007 if (f.file->f_mode & FMODE_PREAD)
1008 ret = compat_readv(f.file, vec, vlen, &pos);
1009 fdput(f);
1010 return ret;
1011}
1012
1013COMPAT_SYSCALL_DEFINE5(preadv, unsigned long, fd,
1014 const struct compat_iovec __user *,vec,
1015 unsigned long, vlen, u32, pos_low, u32, pos_high)
1016{
1017 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1018 return compat_sys_preadv64(fd, vec, vlen, pos);
1019}
1020
1021static size_t compat_writev(struct file *file,
1022 const struct compat_iovec __user *vec,
1023 unsigned long vlen, loff_t *pos)
1024{
1025 ssize_t ret = -EBADF;
1026
1027 if (!(file->f_mode & FMODE_WRITE))
1028 goto out;
1029
1030 ret = -EINVAL;
1031 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
1032 goto out;
1033
1034 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1035
1036out:
1037 if (ret > 0)
1038 add_wchar(current, ret);
1039 inc_syscw(current);
1040 return ret;
1041}
1042
1043COMPAT_SYSCALL_DEFINE3(writev, unsigned long, fd,
1044 const struct compat_iovec __user *, vec,
1045 unsigned long, vlen)
1046{
1047 struct fd f = fdget(fd);
1048 ssize_t ret;
1049 loff_t pos;
1050
1051 if (!f.file)
1052 return -EBADF;
1053 pos = f.file->f_pos;
1054 ret = compat_writev(f.file, vec, vlen, &pos);
1055 f.file->f_pos = pos;
1056 fdput(f);
1057 return ret;
1058}
1059
1060COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1061 const struct compat_iovec __user *,vec,
1062 unsigned long, vlen, loff_t, pos)
1063{
1064 struct fd f;
1065 ssize_t ret;
1066
1067 if (pos < 0)
1068 return -EINVAL;
1069 f = fdget(fd);
1070 if (!f.file)
1071 return -EBADF;
1072 ret = -ESPIPE;
1073 if (f.file->f_mode & FMODE_PWRITE)
1074 ret = compat_writev(f.file, vec, vlen, &pos);
1075 fdput(f);
1076 return ret;
1077}
1078
1079COMPAT_SYSCALL_DEFINE5(pwritev, unsigned long, fd,
1080 const struct compat_iovec __user *,vec,
1081 unsigned long, vlen, u32, pos_low, u32, pos_high)
1082{
1083 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1084 return compat_sys_pwritev64(fd, vec, vlen, pos);
1085}
1086#endif
1087
Al Viro19f4fc32013-02-24 02:17:03 -05001088static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1089 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Al Viro2903ff02012-08-28 12:52:22 -04001091 struct fd in, out;
1092 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 loff_t pos;
1094 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001095 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /*
1098 * Get input file, and verify that it is ok..
1099 */
1100 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001101 in = fdget(in_fd);
1102 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001104 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 retval = -ESPIPE;
1107 if (!ppos)
Al Viro2903ff02012-08-28 12:52:22 -04001108 ppos = &in.file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 else
Al Viro2903ff02012-08-28 12:52:22 -04001110 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001112 retval = rw_verify_area(READ, in.file, ppos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001113 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001115 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /*
1118 * Get output file, and verify that it is ok..
1119 */
1120 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001121 out = fdget(out_fd);
1122 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001124 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 goto fput_out;
1126 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001127 in_inode = file_inode(in.file);
1128 out_inode = file_inode(out.file);
Al Viro2903ff02012-08-28 12:52:22 -04001129 retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001130 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001132 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (!max)
1135 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1136
1137 pos = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (unlikely(pos + count > max)) {
1139 retval = -EOVERFLOW;
1140 if (pos >= max)
1141 goto fput_out;
1142 count = max - pos;
1143 }
1144
Jens Axboed96e6e72007-06-11 12:18:52 +02001145 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001146#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001147 /*
1148 * We need to debate whether we can enable this or not. The
1149 * man page documents EAGAIN return for the output at least,
1150 * and the application is arguably buggy if it doesn't expect
1151 * EAGAIN on a non-blocking file descriptor.
1152 */
Al Viro2903ff02012-08-28 12:52:22 -04001153 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001154 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001155#endif
Al Viro2903ff02012-08-28 12:52:22 -04001156 retval = do_splice_direct(in.file, ppos, out.file, count, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001159 add_rchar(current, retval);
1160 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001161 fsnotify_access(in.file);
1162 fsnotify_modify(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001165 inc_syscr(current);
1166 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (*ppos > max)
1168 retval = -EOVERFLOW;
1169
1170fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001171 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001173 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174out:
1175 return retval;
1176}
1177
Heiko Carstens002c8972009-01-14 14:14:18 +01001178SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 loff_t pos;
1181 off_t off;
1182 ssize_t ret;
1183
1184 if (offset) {
1185 if (unlikely(get_user(off, offset)))
1186 return -EFAULT;
1187 pos = off;
1188 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1189 if (unlikely(put_user(pos, offset)))
1190 return -EFAULT;
1191 return ret;
1192 }
1193
1194 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1195}
1196
Heiko Carstens002c8972009-01-14 14:14:18 +01001197SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 loff_t pos;
1200 ssize_t ret;
1201
1202 if (offset) {
1203 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1204 return -EFAULT;
1205 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1206 if (unlikely(put_user(pos, offset)))
1207 return -EFAULT;
1208 return ret;
1209 }
1210
1211 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1212}
Al Viro19f4fc32013-02-24 02:17:03 -05001213
1214#ifdef CONFIG_COMPAT
1215COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1216 compat_off_t __user *, offset, compat_size_t, count)
1217{
1218 loff_t pos;
1219 off_t off;
1220 ssize_t ret;
1221
1222 if (offset) {
1223 if (unlikely(get_user(off, offset)))
1224 return -EFAULT;
1225 pos = off;
1226 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1227 if (unlikely(put_user(pos, offset)))
1228 return -EFAULT;
1229 return ret;
1230 }
1231
1232 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1233}
1234
1235COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1236 compat_loff_t __user *, offset, compat_size_t, count)
1237{
1238 loff_t pos;
1239 ssize_t ret;
1240
1241 if (offset) {
1242 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1243 return -EFAULT;
1244 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1245 if (unlikely(put_user(pos, offset)))
1246 return -EFAULT;
1247 return ret;
1248 }
1249
1250 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1251}
1252#endif