blob: 932bb3414a967a401edb671c9fc526b0f9e2dd55 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/read_write.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include <linux/slab.h>
8#include <linux/stat.h>
9#include <linux/fcntl.h>
10#include <linux/file.h>
11#include <linux/uio.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070012#include <linux/aio.h>
Robert Love0eeca282005-07-12 17:06:03 -040013#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/security.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050015#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/syscalls.h>
Linus Torvaldse28cc712006-01-04 16:20:40 -080017#include <linux/pagemap.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020018#include <linux/splice.h>
Al Viro561c6732013-02-24 10:52:26 -050019#include <linux/compat.h>
Al Viro06ae43f2013-03-20 13:19:30 -040020#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/uaccess.h>
23#include <asm/unistd.h>
24
Al Viroc0bd14af2013-05-04 15:00:54 -040025typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
26typedef ssize_t (*iov_fn_t)(struct kiocb *, const struct iovec *,
27 unsigned long, loff_t);
28
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080029const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -070031 .read = do_sync_read,
32 .aio_read = generic_file_aio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020034 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
37EXPORT_SYMBOL(generic_ro_fops);
38
Al Virocccb5a12010-12-17 07:44:05 -050039static inline int unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070040{
Al Virocccb5a12010-12-17 07:44:05 -050041 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070042}
43
Jie Liu46a1c2c2013-06-25 12:02:13 +080044/**
45 * vfs_setpos - update the file offset for lseek
46 * @file: file structure in question
47 * @offset: file offset to seek to
48 * @maxsize: maximum file size
49 *
50 * This is a low-level filesystem helper for updating the file offset to
51 * the value specified by @offset if the given offset is valid and it is
52 * not equal to the current file offset.
53 *
54 * Return the specified offset on success and -EINVAL on invalid offset.
55 */
56loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070057{
58 if (offset < 0 && !unsigned_offsets(file))
59 return -EINVAL;
60 if (offset > maxsize)
61 return -EINVAL;
62
63 if (offset != file->f_pos) {
64 file->f_pos = offset;
65 file->f_version = 0;
66 }
67 return offset;
68}
Jie Liu46a1c2c2013-06-25 12:02:13 +080069EXPORT_SYMBOL(vfs_setpos);
Andi Kleenef3d0fd2011-09-15 16:06:48 -070070
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020071/**
Andi Kleen57604952011-09-15 16:06:50 -070072 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020073 * @file: file structure to seek on
74 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -080075 * @whence: type of seek
Eric Sandeene8b96eb2012-04-30 13:11:29 -050076 * @size: max size of this file in file system
77 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020078 *
Andi Kleen57604952011-09-15 16:06:50 -070079 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050080 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070081 *
82 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070083 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070084 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
85 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020086 */
Andi Kleen9465efc2008-06-27 11:05:24 +020087loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -080088generic_file_llseek_size(struct file *file, loff_t offset, int whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050089 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Andrew Morton965c8e52012-12-17 15:59:39 -080091 switch (whence) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020092 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050093 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020094 break;
95 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080096 /*
97 * Here we special-case the lseek(fd, 0, SEEK_CUR)
98 * position-querying operation. Avoid rewriting the "same"
99 * f_pos value back to the file because a concurrent read(),
100 * write() or lseek() might have altered it
101 */
102 if (offset == 0)
103 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700104 /*
105 * f_lock protects against read/modify/write race with other
106 * SEEK_CURs. Note that parallel writes and reads behave
107 * like SEEK_SET.
108 */
109 spin_lock(&file->f_lock);
Jie Liu46a1c2c2013-06-25 12:02:13 +0800110 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700111 spin_unlock(&file->f_lock);
112 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -0400113 case SEEK_DATA:
114 /*
115 * In the generic case the entire file is data, so as long as
116 * offset isn't at the end of the file then the offset is data.
117 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500118 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400119 return -ENXIO;
120 break;
121 case SEEK_HOLE:
122 /*
123 * There is a virtual hole at the end of the file, so as long as
124 * offset isn't i_size or larger, return i_size.
125 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500126 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400127 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500128 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400129 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200131
Jie Liu46a1c2c2013-06-25 12:02:13 +0800132 return vfs_setpos(file, offset, maxsize);
Andi Kleen57604952011-09-15 16:06:50 -0700133}
134EXPORT_SYMBOL(generic_file_llseek_size);
135
136/**
137 * generic_file_llseek - generic llseek implementation for regular files
138 * @file: file structure to seek on
139 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800140 * @whence: type of seek
Andi Kleen57604952011-09-15 16:06:50 -0700141 *
142 * This is a generic implemenation of ->llseek useable for all normal local
143 * filesystems. It just updates the file offset to the value specified by
Ming Lei546ae2d2013-04-29 15:06:07 -0700144 * @offset and @whence.
Andi Kleen57604952011-09-15 16:06:50 -0700145 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800146loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
Andi Kleen57604952011-09-15 16:06:50 -0700147{
148 struct inode *inode = file->f_mapping->host;
149
Andrew Morton965c8e52012-12-17 15:59:39 -0800150 return generic_file_llseek_size(file, offset, whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500151 inode->i_sb->s_maxbytes,
152 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
Andi Kleen9465efc2008-06-27 11:05:24 +0200154EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
jan Blunckae6afc32010-05-26 14:44:48 -0700156/**
Al Viro1bf9d142013-06-16 20:27:42 +0400157 * fixed_size_llseek - llseek implementation for fixed-sized devices
158 * @file: file structure to seek on
159 * @offset: file offset to seek to
160 * @whence: type of seek
161 * @size: size of the file
162 *
163 */
164loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
165{
166 switch (whence) {
167 case SEEK_SET: case SEEK_CUR: case SEEK_END:
168 return generic_file_llseek_size(file, offset, whence,
169 size, size);
170 default:
171 return -EINVAL;
172 }
173}
174EXPORT_SYMBOL(fixed_size_llseek);
175
176/**
jan Blunckae6afc32010-05-26 14:44:48 -0700177 * noop_llseek - No Operation Performed llseek implementation
178 * @file: file structure to seek on
179 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800180 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700181 *
182 * This is an implementation of ->llseek useable for the rare special case when
183 * userspace expects the seek to succeed but the (device) file is actually not
184 * able to perform the seek. In this case you use noop_llseek() instead of
185 * falling back to the default implementation of ->llseek.
186 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800187loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700188{
189 return file->f_pos;
190}
191EXPORT_SYMBOL(noop_llseek);
192
Andrew Morton965c8e52012-12-17 15:59:39 -0800193loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 return -ESPIPE;
196}
197EXPORT_SYMBOL(no_llseek);
198
Andrew Morton965c8e52012-12-17 15:59:39 -0800199loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Al Viro496ad9a2013-01-23 17:07:38 -0500201 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200202 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Josef Bacik982d8162011-07-18 13:21:35 -0400204 mutex_lock(&inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -0800205 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700206 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400207 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700209 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800210 if (offset == 0) {
211 retval = file->f_pos;
212 goto out;
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400215 break;
216 case SEEK_DATA:
217 /*
218 * In the generic case the entire file is data, so as
219 * long as offset isn't at the end of the file then the
220 * offset is data.
221 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300222 if (offset >= inode->i_size) {
223 retval = -ENXIO;
224 goto out;
225 }
Josef Bacik982d8162011-07-18 13:21:35 -0400226 break;
227 case SEEK_HOLE:
228 /*
229 * There is a virtual hole at the end of the file, so
230 * as long as offset isn't i_size or larger, return
231 * i_size.
232 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300233 if (offset >= inode->i_size) {
234 retval = -ENXIO;
235 goto out;
236 }
Josef Bacik982d8162011-07-18 13:21:35 -0400237 offset = inode->i_size;
238 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500241 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (offset != file->f_pos) {
243 file->f_pos = offset;
244 file->f_version = 0;
245 }
246 retval = offset;
247 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800248out:
Josef Bacik982d8162011-07-18 13:21:35 -0400249 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return retval;
251}
252EXPORT_SYMBOL(default_llseek);
253
Andrew Morton965c8e52012-12-17 15:59:39 -0800254loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 loff_t (*fn)(struct file *, loff_t, int);
257
258 fn = no_llseek;
259 if (file->f_mode & FMODE_LSEEK) {
Al Viro72c2d532013-09-22 16:27:52 -0400260 if (file->f_op->llseek)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 fn = file->f_op->llseek;
262 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800263 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265EXPORT_SYMBOL(vfs_llseek);
266
Linus Torvalds9c225f22014-03-03 09:36:58 -0800267/*
268 * We only lock f_pos if we have threads or if the file might be
269 * shared with another process. In both cases we'll have an elevated
270 * file count (done either by fdget() or by fork()).
271 */
272static inline struct fd fdget_pos(int fd)
273{
274 struct fd f = fdget(fd);
275 struct file *file = f.file;
276
277 if (file && (file->f_mode & FMODE_ATOMIC_POS)) {
278 if (file_count(file) > 1) {
279 f.flags |= FDPUT_POS_UNLOCK;
280 mutex_lock(&file->f_pos_lock);
281 }
282 }
283 return f;
284}
285
286static inline void fdput_pos(struct fd f)
287{
288 if (f.flags & FDPUT_POS_UNLOCK)
289 mutex_unlock(&f.file->f_pos_lock);
290 fdput(f);
291}
292
Andrew Morton965c8e52012-12-17 15:59:39 -0800293SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 off_t retval;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800296 struct fd f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400297 if (!f.file)
298 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800301 if (whence <= SEEK_MAX) {
302 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 retval = res;
304 if (res != (loff_t)retval)
305 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
306 }
Linus Torvalds9c225f22014-03-03 09:36:58 -0800307 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return retval;
309}
310
Al Viro561c6732013-02-24 10:52:26 -0500311#ifdef CONFIG_COMPAT
312COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
313{
314 return sys_lseek(fd, offset, whence);
315}
316#endif
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100319SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
320 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800321 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 int retval;
Al Viro2903ff02012-08-28 12:52:22 -0400324 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Al Viro2903ff02012-08-28 12:52:22 -0400327 if (!f.file)
328 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800331 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 goto out_putf;
333
Al Viro2903ff02012-08-28 12:52:22 -0400334 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800335 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 retval = (int)offset;
338 if (offset >= 0) {
339 retval = -EFAULT;
340 if (!copy_to_user(result, &offset, sizeof(offset)))
341 retval = 0;
342 }
343out_putf:
Al Viro2903ff02012-08-28 12:52:22 -0400344 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return retval;
346}
347#endif
348
Linus Torvaldse28cc712006-01-04 16:20:40 -0800349/*
350 * rw_verify_area doesn't like huge counts. We limit
351 * them to something that fits in "int" so that others
352 * won't have to do range checks all the time.
353 */
Al Viro68d70d02013-06-19 15:26:04 +0400354int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct inode *inode;
357 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100358 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Al Viro496ad9a2013-01-23 17:07:38 -0500360 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800361 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100362 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500364 if (unlikely(pos < 0)) {
365 if (!unsigned_offsets(file))
366 return retval;
367 if (count >= -pos) /* both values are in 0..LLONG_MAX */
368 return -EOVERFLOW;
369 } else if (unlikely((loff_t) (pos + count) < 0)) {
370 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700371 return retval;
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Pavel Emelyanova16877c2007-10-01 14:41:11 -0700374 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
James Morrisc43e2592008-01-12 22:05:48 +1100375 retval = locks_mandatory_area(
Linus Torvaldse28cc712006-01-04 16:20:40 -0800376 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
377 inode, file, pos, count);
378 if (retval < 0)
379 return retval;
380 }
James Morrisc43e2592008-01-12 22:05:48 +1100381 retval = security_file_permission(file,
382 read_write == READ ? MAY_READ : MAY_WRITE);
383 if (retval)
384 return retval;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800385 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
389{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700390 struct iovec iov = { .iov_base = buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct kiocb kiocb;
392 ssize_t ret;
393
394 init_sync_kiocb(&kiocb, filp);
395 kiocb.ki_pos = *ppos;
David Howells61964eb2010-03-24 17:09:19 +0000396 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700397
Zach Brown41003a72013-05-07 16:18:25 -0700398 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (-EIOCBQUEUED == ret)
400 ret = wait_on_sync_kiocb(&kiocb);
401 *ppos = kiocb.ki_pos;
402 return ret;
403}
404
405EXPORT_SYMBOL(do_sync_read);
406
407ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
408{
409 ssize_t ret;
410
411 if (!(file->f_mode & FMODE_READ))
412 return -EBADF;
Al Viro72c2d532013-09-22 16:27:52 -0400413 if (!file->f_op->read && !file->f_op->aio_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return -EINVAL;
415 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
416 return -EFAULT;
417
418 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800419 if (ret >= 0) {
420 count = ret;
James Morrisc43e2592008-01-12 22:05:48 +1100421 if (file->f_op->read)
422 ret = file->f_op->read(file, buf, count, pos);
423 else
424 ret = do_sync_read(file, buf, count, pos);
425 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500426 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100427 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
James Morrisc43e2592008-01-12 22:05:48 +1100429 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
432 return ret;
433}
434
435EXPORT_SYMBOL(vfs_read);
436
437ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
438{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700439 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct kiocb kiocb;
441 ssize_t ret;
442
443 init_sync_kiocb(&kiocb, filp);
444 kiocb.ki_pos = *ppos;
David Howells61964eb2010-03-24 17:09:19 +0000445 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700446
Zach Brown41003a72013-05-07 16:18:25 -0700447 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (-EIOCBQUEUED == ret)
449 ret = wait_on_sync_kiocb(&kiocb);
450 *ppos = kiocb.ki_pos;
451 return ret;
452}
453
454EXPORT_SYMBOL(do_sync_write);
455
Al Viro06ae43f2013-03-20 13:19:30 -0400456ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
457{
458 mm_segment_t old_fs;
459 const char __user *p;
460 ssize_t ret;
461
Al Viro72c2d532013-09-22 16:27:52 -0400462 if (!file->f_op->write && !file->f_op->aio_write)
Al Viro3e84f482013-03-27 15:20:30 +0000463 return -EINVAL;
464
Al Viro06ae43f2013-03-20 13:19:30 -0400465 old_fs = get_fs();
466 set_fs(get_ds());
467 p = (__force const char __user *)buf;
468 if (count > MAX_RW_COUNT)
469 count = MAX_RW_COUNT;
470 if (file->f_op->write)
471 ret = file->f_op->write(file, p, count, pos);
472 else
473 ret = do_sync_write(file, p, count, pos);
474 set_fs(old_fs);
475 if (ret > 0) {
476 fsnotify_modify(file);
477 add_wchar(current, ret);
478 }
479 inc_syscw(current);
480 return ret;
481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
484{
485 ssize_t ret;
486
487 if (!(file->f_mode & FMODE_WRITE))
488 return -EBADF;
Al Viro72c2d532013-09-22 16:27:52 -0400489 if (!file->f_op->write && !file->f_op->aio_write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -EINVAL;
491 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
492 return -EFAULT;
493
494 ret = rw_verify_area(WRITE, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800495 if (ret >= 0) {
496 count = ret;
Al Viro03d95eb2013-03-20 13:04:20 -0400497 file_start_write(file);
James Morrisc43e2592008-01-12 22:05:48 +1100498 if (file->f_op->write)
499 ret = file->f_op->write(file, buf, count, pos);
500 else
501 ret = do_sync_write(file, buf, count, pos);
502 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500503 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100504 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
James Morrisc43e2592008-01-12 22:05:48 +1100506 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400507 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
510 return ret;
511}
512
513EXPORT_SYMBOL(vfs_write);
514
515static inline loff_t file_pos_read(struct file *file)
516{
517 return file->f_pos;
518}
519
520static inline void file_pos_write(struct file *file, loff_t pos)
521{
522 file->f_pos = pos;
523}
524
Heiko Carstens3cdad422009-01-14 14:14:22 +0100525SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800527 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Al Viro2903ff02012-08-28 12:52:22 -0400530 if (f.file) {
531 loff_t pos = file_pos_read(f.file);
532 ret = vfs_read(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400533 if (ret >= 0)
534 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800535 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return ret;
538}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Heiko Carstens3cdad422009-01-14 14:14:22 +0100540SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
541 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800543 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Al Viro2903ff02012-08-28 12:52:22 -0400546 if (f.file) {
547 loff_t pos = file_pos_read(f.file);
548 ret = vfs_write(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400549 if (ret >= 0)
550 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800551 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553
554 return ret;
555}
556
Al Viro4a0fd5b2013-01-21 15:16:58 -0500557SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
558 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Al Viro2903ff02012-08-28 12:52:22 -0400560 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 if (pos < 0)
564 return -EINVAL;
565
Al Viro2903ff02012-08-28 12:52:22 -0400566 f = fdget(fd);
567 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400569 if (f.file->f_mode & FMODE_PREAD)
570 ret = vfs_read(f.file, buf, count, &pos);
571 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
574 return ret;
575}
576
Al Viro4a0fd5b2013-01-21 15:16:58 -0500577SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
578 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Al Viro2903ff02012-08-28 12:52:22 -0400580 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 if (pos < 0)
584 return -EINVAL;
585
Al Viro2903ff02012-08-28 12:52:22 -0400586 f = fdget(fd);
587 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400589 if (f.file->f_mode & FMODE_PWRITE)
590 ret = vfs_write(f.file, buf, count, &pos);
591 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
594 return ret;
595}
596
597/*
598 * Reduce an iovec's length in-place. Return the resulting number of segments
599 */
600unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
601{
602 unsigned long seg = 0;
603 size_t len = 0;
604
605 while (seg < nr_segs) {
606 seg++;
607 if (len + iov->iov_len >= to) {
608 iov->iov_len = to - len;
609 break;
610 }
611 len += iov->iov_len;
612 iov++;
613 }
614 return seg;
615}
Eric Sandeen19295522008-01-28 23:58:27 -0500616EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Al Viro72ec3512013-03-20 10:42:10 -0400618static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700619 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
620{
621 struct kiocb kiocb;
622 ssize_t ret;
623
624 init_sync_kiocb(&kiocb, filp);
625 kiocb.ki_pos = *ppos;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700626 kiocb.ki_nbytes = len;
627
Zach Brown41003a72013-05-07 16:18:25 -0700628 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700629 if (ret == -EIOCBQUEUED)
630 ret = wait_on_sync_kiocb(&kiocb);
631 *ppos = kiocb.ki_pos;
632 return ret;
633}
634
635/* Do it by hand, with file-ops */
Al Viro72ec3512013-03-20 10:42:10 -0400636static ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700637 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
638{
639 struct iovec *vector = iov;
640 ssize_t ret = 0;
641
642 while (nr_segs > 0) {
643 void __user *base;
644 size_t len;
645 ssize_t nr;
646
647 base = vector->iov_base;
648 len = vector->iov_len;
649 vector++;
650 nr_segs--;
651
652 nr = fn(filp, base, len, ppos);
653
654 if (nr < 0) {
655 if (!ret)
656 ret = nr;
657 break;
658 }
659 ret += nr;
660 if (nr != len)
661 break;
662 }
663
664 return ret;
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/* A write operation does a read from user space and vice versa */
668#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
669
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700670ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
671 unsigned long nr_segs, unsigned long fast_segs,
672 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700673 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700674{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700675 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700676 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700677 struct iovec *iov = fast_pointer;
678
Linus Torvalds435f49a2010-10-29 10:36:49 -0700679 /*
680 * SuS says "The readv() function *may* fail if the iovcnt argument
681 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
682 * traditionally returned zero for zero segments, so...
683 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700684 if (nr_segs == 0) {
685 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700686 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700687 }
688
Linus Torvalds435f49a2010-10-29 10:36:49 -0700689 /*
690 * First get the "struct iovec" from user memory and
691 * verify all the pointers
692 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700693 if (nr_segs > UIO_MAXIOV) {
694 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700695 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700696 }
697 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700698 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700699 if (iov == NULL) {
700 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700701 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700702 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700703 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700704 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
705 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700706 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700707 }
708
Linus Torvalds435f49a2010-10-29 10:36:49 -0700709 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700710 * According to the Single Unix Specification we should return EINVAL
711 * if an element length is < 0 when cast to ssize_t or if the
712 * total length would overflow the ssize_t return value of the
713 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700714 *
715 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
716 * overflow case.
717 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700718 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700719 for (seg = 0; seg < nr_segs; seg++) {
720 void __user *buf = iov[seg].iov_base;
721 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700722
723 /* see if we we're about to use an invalid len or if
724 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700725 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700726 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700727 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700728 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700729 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700730 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700731 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700732 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700733 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700734 if (len > MAX_RW_COUNT - ret) {
735 len = MAX_RW_COUNT - ret;
736 iov[seg].iov_len = len;
737 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700738 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700739 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700740out:
741 *ret_pointer = iov;
742 return ret;
743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745static ssize_t do_readv_writev(int type, struct file *file,
746 const struct iovec __user * uvector,
747 unsigned long nr_segs, loff_t *pos)
748{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 size_t tot_len;
750 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700751 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 io_fn_t fn;
754 iov_fn_t fnv;
755
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700756 ret = rw_copy_check_uvector(type, uvector, nr_segs,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700757 ARRAY_SIZE(iovstack), iovstack, &iov);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700758 if (ret <= 0)
759 goto out;
760
761 tot_len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800763 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 goto out;
765
766 fnv = NULL;
767 if (type == READ) {
768 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700769 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 } else {
771 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700772 fnv = file->f_op->aio_write;
Al Viro03d95eb2013-03-20 13:04:20 -0400773 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700776 if (fnv)
777 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
778 pos, fnv);
779 else
780 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Al Viro03d95eb2013-03-20 13:04:20 -0400782 if (type != READ)
783 file_end_write(file);
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785out:
786 if (iov != iovstack)
787 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400788 if ((ret + (type == READ)) > 0) {
789 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500790 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400791 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500792 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
798 unsigned long vlen, loff_t *pos)
799{
800 if (!(file->f_mode & FMODE_READ))
801 return -EBADF;
Al Viro72c2d532013-09-22 16:27:52 -0400802 if (!file->f_op->aio_read && !file->f_op->read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -EINVAL;
804
805 return do_readv_writev(READ, file, vec, vlen, pos);
806}
807
808EXPORT_SYMBOL(vfs_readv);
809
810ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
811 unsigned long vlen, loff_t *pos)
812{
813 if (!(file->f_mode & FMODE_WRITE))
814 return -EBADF;
Al Viro72c2d532013-09-22 16:27:52 -0400815 if (!file->f_op->aio_write && !file->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return -EINVAL;
817
818 return do_readv_writev(WRITE, file, vec, vlen, pos);
819}
820
821EXPORT_SYMBOL(vfs_writev);
822
Heiko Carstens3cdad422009-01-14 14:14:22 +0100823SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
824 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800826 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Al Viro2903ff02012-08-28 12:52:22 -0400829 if (f.file) {
830 loff_t pos = file_pos_read(f.file);
831 ret = vfs_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400832 if (ret >= 0)
833 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800834 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
837 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800838 add_rchar(current, ret);
839 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return ret;
841}
842
Heiko Carstens3cdad422009-01-14 14:14:22 +0100843SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
844 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800846 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Al Viro2903ff02012-08-28 12:52:22 -0400849 if (f.file) {
850 loff_t pos = file_pos_read(f.file);
851 ret = vfs_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400852 if (ret >= 0)
853 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800854 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800858 add_wchar(current, ret);
859 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return ret;
861}
862
Linus Torvalds601cc112009-04-03 08:03:22 -0700863static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700864{
Linus Torvalds601cc112009-04-03 08:03:22 -0700865#define HALF_LONG_BITS (BITS_PER_LONG / 2)
866 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
867}
868
869SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
870 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
871{
872 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_PREAD)
883 ret = vfs_readv(f.file, vec, vlen, &pos);
884 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700885 }
886
887 if (ret > 0)
888 add_rchar(current, ret);
889 inc_syscr(current);
890 return ret;
891}
892
893SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700894 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700895{
Linus Torvalds601cc112009-04-03 08:03:22 -0700896 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400897 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700898 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700899
900 if (pos < 0)
901 return -EINVAL;
902
Al Viro2903ff02012-08-28 12:52:22 -0400903 f = fdget(fd);
904 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700905 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400906 if (f.file->f_mode & FMODE_PWRITE)
907 ret = vfs_writev(f.file, vec, vlen, &pos);
908 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700909 }
910
911 if (ret > 0)
912 add_wchar(current, ret);
913 inc_syscw(current);
914 return ret;
915}
916
Al Viro72ec3512013-03-20 10:42:10 -0400917#ifdef CONFIG_COMPAT
918
919static ssize_t compat_do_readv_writev(int type, struct file *file,
920 const struct compat_iovec __user *uvector,
921 unsigned long nr_segs, loff_t *pos)
922{
923 compat_ssize_t tot_len;
924 struct iovec iovstack[UIO_FASTIOV];
925 struct iovec *iov = iovstack;
926 ssize_t ret;
927 io_fn_t fn;
928 iov_fn_t fnv;
929
Al Viro72ec3512013-03-20 10:42:10 -0400930 ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
931 UIO_FASTIOV, iovstack, &iov);
932 if (ret <= 0)
933 goto out;
934
935 tot_len = ret;
936 ret = rw_verify_area(type, file, pos, tot_len);
937 if (ret < 0)
938 goto out;
939
940 fnv = NULL;
941 if (type == READ) {
942 fn = file->f_op->read;
943 fnv = file->f_op->aio_read;
944 } else {
945 fn = (io_fn_t)file->f_op->write;
946 fnv = file->f_op->aio_write;
Al Viro03d95eb2013-03-20 13:04:20 -0400947 file_start_write(file);
Al Viro72ec3512013-03-20 10:42:10 -0400948 }
949
Al Viro03d95eb2013-03-20 13:04:20 -0400950 if (fnv)
Al Viro72ec3512013-03-20 10:42:10 -0400951 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
952 pos, fnv);
Al Viro03d95eb2013-03-20 13:04:20 -0400953 else
Al Viro72ec3512013-03-20 10:42:10 -0400954 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
955
Al Viro03d95eb2013-03-20 13:04:20 -0400956 if (type != READ)
957 file_end_write(file);
958
Al Viro72ec3512013-03-20 10:42:10 -0400959out:
960 if (iov != iovstack)
961 kfree(iov);
962 if ((ret + (type == READ)) > 0) {
963 if (type == READ)
964 fsnotify_access(file);
965 else
966 fsnotify_modify(file);
967 }
968 return ret;
969}
970
971static size_t compat_readv(struct file *file,
972 const struct compat_iovec __user *vec,
973 unsigned long vlen, loff_t *pos)
974{
975 ssize_t ret = -EBADF;
976
977 if (!(file->f_mode & FMODE_READ))
978 goto out;
979
980 ret = -EINVAL;
Al Viro72c2d532013-09-22 16:27:52 -0400981 if (!file->f_op->aio_read && !file->f_op->read)
Al Viro72ec3512013-03-20 10:42:10 -0400982 goto out;
983
984 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
985
986out:
987 if (ret > 0)
988 add_rchar(current, ret);
989 inc_syscr(current);
990 return ret;
991}
992
Heiko Carstensdfd948e2014-01-29 14:05:44 -0800993COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -0400994 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -0800995 compat_ulong_t, vlen)
Al Viro72ec3512013-03-20 10:42:10 -0400996{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800997 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -0400998 ssize_t ret;
999 loff_t pos;
1000
1001 if (!f.file)
1002 return -EBADF;
1003 pos = f.file->f_pos;
1004 ret = compat_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001005 if (ret >= 0)
1006 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001007 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001008 return ret;
1009}
1010
1011COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1012 const struct compat_iovec __user *,vec,
1013 unsigned long, vlen, loff_t, pos)
1014{
1015 struct fd f;
1016 ssize_t ret;
1017
1018 if (pos < 0)
1019 return -EINVAL;
1020 f = fdget(fd);
1021 if (!f.file)
1022 return -EBADF;
1023 ret = -ESPIPE;
1024 if (f.file->f_mode & FMODE_PREAD)
1025 ret = compat_readv(f.file, vec, vlen, &pos);
1026 fdput(f);
1027 return ret;
1028}
1029
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001030COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001031 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001032 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001033{
1034 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1035 return compat_sys_preadv64(fd, vec, vlen, pos);
1036}
1037
1038static size_t compat_writev(struct file *file,
1039 const struct compat_iovec __user *vec,
1040 unsigned long vlen, loff_t *pos)
1041{
1042 ssize_t ret = -EBADF;
1043
1044 if (!(file->f_mode & FMODE_WRITE))
1045 goto out;
1046
1047 ret = -EINVAL;
Al Viro72c2d532013-09-22 16:27:52 -04001048 if (!file->f_op->aio_write && !file->f_op->write)
Al Viro72ec3512013-03-20 10:42:10 -04001049 goto out;
1050
1051 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1052
1053out:
1054 if (ret > 0)
1055 add_wchar(current, ret);
1056 inc_syscw(current);
1057 return ret;
1058}
1059
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001060COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001061 const struct compat_iovec __user *, vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001062 compat_ulong_t, vlen)
Al Viro72ec3512013-03-20 10:42:10 -04001063{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001064 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001065 ssize_t ret;
1066 loff_t pos;
1067
1068 if (!f.file)
1069 return -EBADF;
1070 pos = f.file->f_pos;
1071 ret = compat_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001072 if (ret >= 0)
1073 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001074 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001075 return ret;
1076}
1077
1078COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1079 const struct compat_iovec __user *,vec,
1080 unsigned long, vlen, loff_t, pos)
1081{
1082 struct fd f;
1083 ssize_t ret;
1084
1085 if (pos < 0)
1086 return -EINVAL;
1087 f = fdget(fd);
1088 if (!f.file)
1089 return -EBADF;
1090 ret = -ESPIPE;
1091 if (f.file->f_mode & FMODE_PWRITE)
1092 ret = compat_writev(f.file, vec, vlen, &pos);
1093 fdput(f);
1094 return ret;
1095}
1096
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001097COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001098 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001099 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001100{
1101 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1102 return compat_sys_pwritev64(fd, vec, vlen, pos);
1103}
1104#endif
1105
Al Viro19f4fc32013-02-24 02:17:03 -05001106static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1107 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Al Viro2903ff02012-08-28 12:52:22 -04001109 struct fd in, out;
1110 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001112 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001114 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 /*
1117 * Get input file, and verify that it is ok..
1118 */
1119 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001120 in = fdget(in_fd);
1121 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001123 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001126 if (!ppos) {
1127 pos = in.file->f_pos;
1128 } else {
1129 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001130 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001132 }
1133 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001134 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001136 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /*
1139 * Get output file, and verify that it is ok..
1140 */
1141 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001142 out = fdget(out_fd);
1143 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001145 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 goto fput_out;
1147 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001148 in_inode = file_inode(in.file);
1149 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001150 out_pos = out.file->f_pos;
1151 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001152 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001154 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (!max)
1157 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (unlikely(pos + count > max)) {
1160 retval = -EOVERFLOW;
1161 if (pos >= max)
1162 goto fput_out;
1163 count = max - pos;
1164 }
1165
Jens Axboed96e6e72007-06-11 12:18:52 +02001166 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001167#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001168 /*
1169 * We need to debate whether we can enable this or not. The
1170 * man page documents EAGAIN return for the output at least,
1171 * and the application is arguably buggy if it doesn't expect
1172 * EAGAIN on a non-blocking file descriptor.
1173 */
Al Viro2903ff02012-08-28 12:52:22 -04001174 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001175 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001176#endif
Al Viro50cd2c52013-05-23 20:10:34 -04001177 file_start_write(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001178 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
Al Viro50cd2c52013-05-23 20:10:34 -04001179 file_end_write(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001182 add_rchar(current, retval);
1183 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001184 fsnotify_access(in.file);
1185 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001186 out.file->f_pos = out_pos;
1187 if (ppos)
1188 *ppos = pos;
1189 else
1190 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001193 inc_syscr(current);
1194 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001195 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 retval = -EOVERFLOW;
1197
1198fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001199 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001201 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202out:
1203 return retval;
1204}
1205
Heiko Carstens002c8972009-01-14 14:14:18 +01001206SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 loff_t pos;
1209 off_t off;
1210 ssize_t ret;
1211
1212 if (offset) {
1213 if (unlikely(get_user(off, offset)))
1214 return -EFAULT;
1215 pos = off;
1216 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1217 if (unlikely(put_user(pos, offset)))
1218 return -EFAULT;
1219 return ret;
1220 }
1221
1222 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1223}
1224
Heiko Carstens002c8972009-01-14 14:14:18 +01001225SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 loff_t pos;
1228 ssize_t ret;
1229
1230 if (offset) {
1231 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1232 return -EFAULT;
1233 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1234 if (unlikely(put_user(pos, offset)))
1235 return -EFAULT;
1236 return ret;
1237 }
1238
1239 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1240}
Al Viro19f4fc32013-02-24 02:17:03 -05001241
1242#ifdef CONFIG_COMPAT
1243COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1244 compat_off_t __user *, offset, compat_size_t, count)
1245{
1246 loff_t pos;
1247 off_t off;
1248 ssize_t ret;
1249
1250 if (offset) {
1251 if (unlikely(get_user(off, offset)))
1252 return -EFAULT;
1253 pos = off;
1254 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1255 if (unlikely(put_user(pos, offset)))
1256 return -EFAULT;
1257 return ret;
1258 }
1259
1260 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1261}
1262
1263COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1264 compat_loff_t __user *, offset, compat_size_t, count)
1265{
1266 loff_t pos;
1267 ssize_t ret;
1268
1269 if (offset) {
1270 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1271 return -EFAULT;
1272 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1273 if (unlikely(put_user(pos, offset)))
1274 return -EFAULT;
1275 return ret;
1276 }
1277
1278 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1279}
1280#endif