blob: acb1713312783ac84520f4740d297c7399af57fc [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 *);
Al Viro293bc982014-02-11 18:37:41 -050025typedef ssize_t (*iter_fn_t)(struct kiocb *, struct iov_iter *);
Al Viroc0bd14af2013-05-04 15:00:54 -040026
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080027const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040029 .read_iter = generic_file_read_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020031 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
34EXPORT_SYMBOL(generic_ro_fops);
35
Al Virocccb5a12010-12-17 07:44:05 -050036static inline int unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070037{
Al Virocccb5a12010-12-17 07:44:05 -050038 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070039}
40
Jie Liu46a1c2c2013-06-25 12:02:13 +080041/**
42 * vfs_setpos - update the file offset for lseek
43 * @file: file structure in question
44 * @offset: file offset to seek to
45 * @maxsize: maximum file size
46 *
47 * This is a low-level filesystem helper for updating the file offset to
48 * the value specified by @offset if the given offset is valid and it is
49 * not equal to the current file offset.
50 *
51 * Return the specified offset on success and -EINVAL on invalid offset.
52 */
53loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070054{
55 if (offset < 0 && !unsigned_offsets(file))
56 return -EINVAL;
57 if (offset > maxsize)
58 return -EINVAL;
59
60 if (offset != file->f_pos) {
61 file->f_pos = offset;
62 file->f_version = 0;
63 }
64 return offset;
65}
Jie Liu46a1c2c2013-06-25 12:02:13 +080066EXPORT_SYMBOL(vfs_setpos);
Andi Kleenef3d0fd2011-09-15 16:06:48 -070067
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020068/**
Andi Kleen57604952011-09-15 16:06:50 -070069 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020070 * @file: file structure to seek on
71 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -080072 * @whence: type of seek
Eric Sandeene8b96eb2012-04-30 13:11:29 -050073 * @size: max size of this file in file system
74 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020075 *
Andi Kleen57604952011-09-15 16:06:50 -070076 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050077 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070078 *
79 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070080 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070081 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
82 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020083 */
Andi Kleen9465efc2008-06-27 11:05:24 +020084loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -080085generic_file_llseek_size(struct file *file, loff_t offset, int whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050086 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Andrew Morton965c8e52012-12-17 15:59:39 -080088 switch (whence) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020089 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050090 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020091 break;
92 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080093 /*
94 * Here we special-case the lseek(fd, 0, SEEK_CUR)
95 * position-querying operation. Avoid rewriting the "same"
96 * f_pos value back to the file because a concurrent read(),
97 * write() or lseek() might have altered it
98 */
99 if (offset == 0)
100 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700101 /*
102 * f_lock protects against read/modify/write race with other
103 * SEEK_CURs. Note that parallel writes and reads behave
104 * like SEEK_SET.
105 */
106 spin_lock(&file->f_lock);
Jie Liu46a1c2c2013-06-25 12:02:13 +0800107 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700108 spin_unlock(&file->f_lock);
109 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -0400110 case SEEK_DATA:
111 /*
112 * In the generic case the entire file is data, so as long as
113 * offset isn't at the end of the file then the offset is data.
114 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500115 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400116 return -ENXIO;
117 break;
118 case SEEK_HOLE:
119 /*
120 * There is a virtual hole at the end of the file, so as long as
121 * offset isn't i_size or larger, return i_size.
122 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500123 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400124 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500125 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400126 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200128
Jie Liu46a1c2c2013-06-25 12:02:13 +0800129 return vfs_setpos(file, offset, maxsize);
Andi Kleen57604952011-09-15 16:06:50 -0700130}
131EXPORT_SYMBOL(generic_file_llseek_size);
132
133/**
134 * generic_file_llseek - generic llseek implementation for regular files
135 * @file: file structure to seek on
136 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800137 * @whence: type of seek
Andi Kleen57604952011-09-15 16:06:50 -0700138 *
139 * This is a generic implemenation of ->llseek useable for all normal local
140 * filesystems. It just updates the file offset to the value specified by
Ming Lei546ae2d2013-04-29 15:06:07 -0700141 * @offset and @whence.
Andi Kleen57604952011-09-15 16:06:50 -0700142 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800143loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
Andi Kleen57604952011-09-15 16:06:50 -0700144{
145 struct inode *inode = file->f_mapping->host;
146
Andrew Morton965c8e52012-12-17 15:59:39 -0800147 return generic_file_llseek_size(file, offset, whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500148 inode->i_sb->s_maxbytes,
149 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
Andi Kleen9465efc2008-06-27 11:05:24 +0200151EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
jan Blunckae6afc32010-05-26 14:44:48 -0700153/**
Al Viro1bf9d142013-06-16 20:27:42 +0400154 * fixed_size_llseek - llseek implementation for fixed-sized devices
155 * @file: file structure to seek on
156 * @offset: file offset to seek to
157 * @whence: type of seek
158 * @size: size of the file
159 *
160 */
161loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
162{
163 switch (whence) {
164 case SEEK_SET: case SEEK_CUR: case SEEK_END:
165 return generic_file_llseek_size(file, offset, whence,
166 size, size);
167 default:
168 return -EINVAL;
169 }
170}
171EXPORT_SYMBOL(fixed_size_llseek);
172
173/**
Al Virob25472f2015-12-05 22:04:48 -0500174 * no_seek_end_llseek - llseek implementation for fixed-sized devices
175 * @file: file structure to seek on
176 * @offset: file offset to seek to
177 * @whence: type of seek
178 *
179 */
180loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
181{
182 switch (whence) {
183 case SEEK_SET: case SEEK_CUR:
184 return generic_file_llseek_size(file, offset, whence,
185 ~0ULL, 0);
186 default:
187 return -EINVAL;
188 }
189}
190EXPORT_SYMBOL(no_seek_end_llseek);
191
192/**
193 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
194 * @file: file structure to seek on
195 * @offset: file offset to seek to
196 * @whence: type of seek
197 * @size: maximal offset allowed
198 *
199 */
200loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
201{
202 switch (whence) {
203 case SEEK_SET: case SEEK_CUR:
204 return generic_file_llseek_size(file, offset, whence,
205 size, 0);
206 default:
207 return -EINVAL;
208 }
209}
210EXPORT_SYMBOL(no_seek_end_llseek_size);
211
212/**
jan Blunckae6afc32010-05-26 14:44:48 -0700213 * noop_llseek - No Operation Performed llseek implementation
214 * @file: file structure to seek on
215 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800216 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700217 *
218 * This is an implementation of ->llseek useable for the rare special case when
219 * userspace expects the seek to succeed but the (device) file is actually not
220 * able to perform the seek. In this case you use noop_llseek() instead of
221 * falling back to the default implementation of ->llseek.
222 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800223loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700224{
225 return file->f_pos;
226}
227EXPORT_SYMBOL(noop_llseek);
228
Andrew Morton965c8e52012-12-17 15:59:39 -0800229loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 return -ESPIPE;
232}
233EXPORT_SYMBOL(no_llseek);
234
Andrew Morton965c8e52012-12-17 15:59:39 -0800235loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Al Viro496ad9a2013-01-23 17:07:38 -0500237 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200238 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Josef Bacik982d8162011-07-18 13:21:35 -0400240 mutex_lock(&inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -0800241 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700242 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400243 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700245 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800246 if (offset == 0) {
247 retval = file->f_pos;
248 goto out;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400251 break;
252 case SEEK_DATA:
253 /*
254 * In the generic case the entire file is data, so as
255 * long as offset isn't at the end of the file then the
256 * offset is data.
257 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300258 if (offset >= inode->i_size) {
259 retval = -ENXIO;
260 goto out;
261 }
Josef Bacik982d8162011-07-18 13:21:35 -0400262 break;
263 case SEEK_HOLE:
264 /*
265 * There is a virtual hole at the end of the file, so
266 * as long as offset isn't i_size or larger, return
267 * i_size.
268 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300269 if (offset >= inode->i_size) {
270 retval = -ENXIO;
271 goto out;
272 }
Josef Bacik982d8162011-07-18 13:21:35 -0400273 offset = inode->i_size;
274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500277 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (offset != file->f_pos) {
279 file->f_pos = offset;
280 file->f_version = 0;
281 }
282 retval = offset;
283 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800284out:
Josef Bacik982d8162011-07-18 13:21:35 -0400285 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return retval;
287}
288EXPORT_SYMBOL(default_llseek);
289
Andrew Morton965c8e52012-12-17 15:59:39 -0800290loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 loff_t (*fn)(struct file *, loff_t, int);
293
294 fn = no_llseek;
295 if (file->f_mode & FMODE_LSEEK) {
Al Viro72c2d532013-09-22 16:27:52 -0400296 if (file->f_op->llseek)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 fn = file->f_op->llseek;
298 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800299 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301EXPORT_SYMBOL(vfs_llseek);
302
Linus Torvalds9c225f22014-03-03 09:36:58 -0800303static inline struct fd fdget_pos(int fd)
304{
Al Virobd2a31d2014-03-04 14:54:22 -0500305 return __to_fd(__fdget_pos(fd));
Linus Torvalds9c225f22014-03-03 09:36:58 -0800306}
307
308static inline void fdput_pos(struct fd f)
309{
310 if (f.flags & FDPUT_POS_UNLOCK)
311 mutex_unlock(&f.file->f_pos_lock);
312 fdput(f);
313}
314
Andrew Morton965c8e52012-12-17 15:59:39 -0800315SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 off_t retval;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800318 struct fd f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400319 if (!f.file)
320 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800323 if (whence <= SEEK_MAX) {
324 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 retval = res;
326 if (res != (loff_t)retval)
327 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
328 }
Linus Torvalds9c225f22014-03-03 09:36:58 -0800329 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return retval;
331}
332
Al Viro561c6732013-02-24 10:52:26 -0500333#ifdef CONFIG_COMPAT
334COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
335{
336 return sys_lseek(fd, offset, whence);
337}
338#endif
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100341SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
342 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800343 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 int retval;
Eric Biggersd7a15f82014-03-16 14:24:08 -0500346 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Al Viro2903ff02012-08-28 12:52:22 -0400349 if (!f.file)
350 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800353 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 goto out_putf;
355
Al Viro2903ff02012-08-28 12:52:22 -0400356 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800357 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 retval = (int)offset;
360 if (offset >= 0) {
361 retval = -EFAULT;
362 if (!copy_to_user(result, &offset, sizeof(offset)))
363 retval = 0;
364 }
365out_putf:
Eric Biggersd7a15f82014-03-16 14:24:08 -0500366 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return retval;
368}
369#endif
370
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100371ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
372{
373 struct kiocb kiocb;
374 ssize_t ret;
375
376 if (!file->f_op->read_iter)
377 return -EINVAL;
378
379 init_sync_kiocb(&kiocb, file);
380 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100381
382 iter->type |= READ;
383 ret = file->f_op->read_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100384 BUG_ON(ret == -EIOCBQUEUED);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100385 if (ret > 0)
386 *ppos = kiocb.ki_pos;
387 return ret;
388}
389EXPORT_SYMBOL(vfs_iter_read);
390
391ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
392{
393 struct kiocb kiocb;
394 ssize_t ret;
395
396 if (!file->f_op->write_iter)
397 return -EINVAL;
398
399 init_sync_kiocb(&kiocb, file);
400 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100401
402 iter->type |= WRITE;
403 ret = file->f_op->write_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100404 BUG_ON(ret == -EIOCBQUEUED);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100405 if (ret > 0)
406 *ppos = kiocb.ki_pos;
407 return ret;
408}
409EXPORT_SYMBOL(vfs_iter_write);
410
Linus Torvaldse28cc712006-01-04 16:20:40 -0800411/*
412 * rw_verify_area doesn't like huge counts. We limit
413 * them to something that fits in "int" so that others
414 * won't have to do range checks all the time.
415 */
Al Viro68d70d02013-06-19 15:26:04 +0400416int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct inode *inode;
419 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100420 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Al Viro496ad9a2013-01-23 17:07:38 -0500422 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800423 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100424 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500426 if (unlikely(pos < 0)) {
427 if (!unsigned_offsets(file))
428 return retval;
429 if (count >= -pos) /* both values are in 0..LLONG_MAX */
430 return -EOVERFLOW;
431 } else if (unlikely((loff_t) (pos + count) < 0)) {
432 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700433 return retval;
434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500436 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
James Morrisc43e2592008-01-12 22:05:48 +1100437 retval = locks_mandatory_area(
Linus Torvaldse28cc712006-01-04 16:20:40 -0800438 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
439 inode, file, pos, count);
440 if (retval < 0)
441 return retval;
442 }
James Morrisc43e2592008-01-12 22:05:48 +1100443 retval = security_file_permission(file,
444 read_write == READ ? MAY_READ : MAY_WRITE);
445 if (retval)
446 return retval;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800447 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Al Viro5d5d5682015-04-03 15:41:18 -0400450static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500451{
452 struct iovec iov = { .iov_base = buf, .iov_len = len };
453 struct kiocb kiocb;
454 struct iov_iter iter;
455 ssize_t ret;
456
457 init_sync_kiocb(&kiocb, filp);
458 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500459 iov_iter_init(&iter, READ, &iov, 1, len);
460
461 ret = filp->f_op->read_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100462 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500463 *ppos = kiocb.ki_pos;
464 return ret;
465}
466
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200467ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
468 loff_t *pos)
469{
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200470 if (file->f_op->read)
Al Viro3d04c8a2015-04-03 15:09:18 -0400471 return file->f_op->read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200472 else if (file->f_op->read_iter)
Al Viro3d04c8a2015-04-03 15:09:18 -0400473 return new_sync_read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200474 else
Al Viro3d04c8a2015-04-03 15:09:18 -0400475 return -EINVAL;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200476}
Al Viro3d04c8a2015-04-03 15:09:18 -0400477EXPORT_SYMBOL(__vfs_read);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
480{
481 ssize_t ret;
482
483 if (!(file->f_mode & FMODE_READ))
484 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500485 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -EINVAL;
487 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
488 return -EFAULT;
489
490 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800491 if (ret >= 0) {
492 count = ret;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200493 ret = __vfs_read(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100494 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500495 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100496 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
James Morrisc43e2592008-01-12 22:05:48 +1100498 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
501 return ret;
502}
503
504EXPORT_SYMBOL(vfs_read);
505
Al Viro5d5d5682015-04-03 15:41:18 -0400506static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500507{
508 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
509 struct kiocb kiocb;
510 struct iov_iter iter;
511 ssize_t ret;
512
513 init_sync_kiocb(&kiocb, filp);
514 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500515 iov_iter_init(&iter, WRITE, &iov, 1, len);
516
517 ret = filp->f_op->write_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100518 BUG_ON(ret == -EIOCBQUEUED);
Al Virof765b132015-04-06 20:50:38 -0400519 if (ret > 0)
520 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500521 return ret;
522}
523
Al Viro493c84c2015-04-03 15:06:43 -0400524ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
525 loff_t *pos)
526{
527 if (file->f_op->write)
528 return file->f_op->write(file, p, count, pos);
Al Viro493c84c2015-04-03 15:06:43 -0400529 else if (file->f_op->write_iter)
530 return new_sync_write(file, p, count, pos);
531 else
532 return -EINVAL;
533}
534EXPORT_SYMBOL(__vfs_write);
535
Al Viro06ae43f2013-03-20 13:19:30 -0400536ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
537{
538 mm_segment_t old_fs;
539 const char __user *p;
540 ssize_t ret;
541
Al Viro7f7f25e2014-02-11 17:49:24 -0500542 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro3e84f482013-03-27 15:20:30 +0000543 return -EINVAL;
544
Al Viro06ae43f2013-03-20 13:19:30 -0400545 old_fs = get_fs();
546 set_fs(get_ds());
547 p = (__force const char __user *)buf;
548 if (count > MAX_RW_COUNT)
549 count = MAX_RW_COUNT;
Al Viro493c84c2015-04-03 15:06:43 -0400550 ret = __vfs_write(file, p, count, pos);
Al Viro06ae43f2013-03-20 13:19:30 -0400551 set_fs(old_fs);
552 if (ret > 0) {
553 fsnotify_modify(file);
554 add_wchar(current, ret);
555 }
556 inc_syscw(current);
557 return ret;
558}
559
Al Viro2ec3a122014-08-19 11:48:09 -0400560EXPORT_SYMBOL(__kernel_write);
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
563{
564 ssize_t ret;
565
566 if (!(file->f_mode & FMODE_WRITE))
567 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500568 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return -EINVAL;
570 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
571 return -EFAULT;
572
573 ret = rw_verify_area(WRITE, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800574 if (ret >= 0) {
575 count = ret;
Al Viro03d95eb2013-03-20 13:04:20 -0400576 file_start_write(file);
Al Viro493c84c2015-04-03 15:06:43 -0400577 ret = __vfs_write(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100578 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500579 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100580 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
James Morrisc43e2592008-01-12 22:05:48 +1100582 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400583 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
586 return ret;
587}
588
589EXPORT_SYMBOL(vfs_write);
590
591static inline loff_t file_pos_read(struct file *file)
592{
593 return file->f_pos;
594}
595
596static inline void file_pos_write(struct file *file, loff_t pos)
597{
598 file->f_pos = pos;
599}
600
Heiko Carstens3cdad422009-01-14 14:14:22 +0100601SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800603 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Al Viro2903ff02012-08-28 12:52:22 -0400606 if (f.file) {
607 loff_t pos = file_pos_read(f.file);
608 ret = vfs_read(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400609 if (ret >= 0)
610 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800611 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return ret;
614}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Heiko Carstens3cdad422009-01-14 14:14:22 +0100616SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
617 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800619 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Al Viro2903ff02012-08-28 12:52:22 -0400622 if (f.file) {
623 loff_t pos = file_pos_read(f.file);
624 ret = vfs_write(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400625 if (ret >= 0)
626 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800627 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
630 return ret;
631}
632
Al Viro4a0fd5b2013-01-21 15:16:58 -0500633SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
634 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Al Viro2903ff02012-08-28 12:52:22 -0400636 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (pos < 0)
640 return -EINVAL;
641
Al Viro2903ff02012-08-28 12:52:22 -0400642 f = fdget(fd);
643 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400645 if (f.file->f_mode & FMODE_PREAD)
646 ret = vfs_read(f.file, buf, count, &pos);
647 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 return ret;
651}
652
Al Viro4a0fd5b2013-01-21 15:16:58 -0500653SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
654 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Al Viro2903ff02012-08-28 12:52:22 -0400656 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if (pos < 0)
660 return -EINVAL;
661
Al Viro2903ff02012-08-28 12:52:22 -0400662 f = fdget(fd);
663 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400665 if (f.file->f_mode & FMODE_PWRITE)
666 ret = vfs_write(f.file, buf, count, &pos);
667 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
670 return ret;
671}
672
673/*
674 * Reduce an iovec's length in-place. Return the resulting number of segments
675 */
676unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
677{
678 unsigned long seg = 0;
679 size_t len = 0;
680
681 while (seg < nr_segs) {
682 seg++;
683 if (len + iov->iov_len >= to) {
684 iov->iov_len = to - len;
685 break;
686 }
687 len += iov->iov_len;
688 iov++;
689 }
690 return seg;
691}
Eric Sandeen19295522008-01-28 23:58:27 -0500692EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Al Viroac15ac02015-03-20 20:10:21 -0400694static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
695 loff_t *ppos, iter_fn_t fn)
Al Viro293bc982014-02-11 18:37:41 -0500696{
697 struct kiocb kiocb;
Al Viro293bc982014-02-11 18:37:41 -0500698 ssize_t ret;
699
700 init_sync_kiocb(&kiocb, filp);
701 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500702
Al Viroac15ac02015-03-20 20:10:21 -0400703 ret = fn(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100704 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500705 *ppos = kiocb.ki_pos;
706 return ret;
707}
708
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700709/* Do it by hand, with file-ops */
Al Viroac15ac02015-03-20 20:10:21 -0400710static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
711 loff_t *ppos, io_fn_t fn)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700712{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700713 ssize_t ret = 0;
714
Al Viroac15ac02015-03-20 20:10:21 -0400715 while (iov_iter_count(iter)) {
716 struct iovec iovec = iov_iter_iovec(iter);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700717 ssize_t nr;
718
Al Viroac15ac02015-03-20 20:10:21 -0400719 nr = fn(filp, iovec.iov_base, iovec.iov_len, ppos);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700720
721 if (nr < 0) {
722 if (!ret)
723 ret = nr;
724 break;
725 }
726 ret += nr;
Al Viroac15ac02015-03-20 20:10:21 -0400727 if (nr != iovec.iov_len)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700728 break;
Al Viroac15ac02015-03-20 20:10:21 -0400729 iov_iter_advance(iter, nr);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700730 }
731
732 return ret;
733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/* A write operation does a read from user space and vice versa */
736#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
737
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700738ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
739 unsigned long nr_segs, unsigned long fast_segs,
740 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700741 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700742{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700743 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700744 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700745 struct iovec *iov = fast_pointer;
746
Linus Torvalds435f49a2010-10-29 10:36:49 -0700747 /*
748 * SuS says "The readv() function *may* fail if the iovcnt argument
749 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
750 * traditionally returned zero for zero segments, so...
751 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700752 if (nr_segs == 0) {
753 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700754 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700755 }
756
Linus Torvalds435f49a2010-10-29 10:36:49 -0700757 /*
758 * First get the "struct iovec" from user memory and
759 * verify all the pointers
760 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700761 if (nr_segs > UIO_MAXIOV) {
762 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700763 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700764 }
765 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700766 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700767 if (iov == NULL) {
768 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700769 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700770 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700771 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700772 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
773 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700774 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700775 }
776
Linus Torvalds435f49a2010-10-29 10:36:49 -0700777 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700778 * According to the Single Unix Specification we should return EINVAL
779 * if an element length is < 0 when cast to ssize_t or if the
780 * total length would overflow the ssize_t return value of the
781 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700782 *
783 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
784 * overflow case.
785 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700786 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700787 for (seg = 0; seg < nr_segs; seg++) {
788 void __user *buf = iov[seg].iov_base;
789 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700790
791 /* see if we we're about to use an invalid len or if
792 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700793 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700794 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700795 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700796 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700797 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700798 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700799 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700800 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700801 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700802 if (len > MAX_RW_COUNT - ret) {
803 len = MAX_RW_COUNT - ret;
804 iov[seg].iov_len = len;
805 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700806 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700807 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700808out:
809 *ret_pointer = iov;
810 return ret;
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813static ssize_t do_readv_writev(int type, struct file *file,
814 const struct iovec __user * uvector,
815 unsigned long nr_segs, loff_t *pos)
816{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 size_t tot_len;
818 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700819 struct iovec *iov = iovstack;
Al Viroac15ac02015-03-20 20:10:21 -0400820 struct iov_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 io_fn_t fn;
Al Viro293bc982014-02-11 18:37:41 -0500823 iter_fn_t iter_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Al Viro0504c072015-03-21 19:40:11 -0400825 ret = import_iovec(type, uvector, nr_segs,
826 ARRAY_SIZE(iovstack), &iov, &iter);
827 if (ret < 0)
828 return ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700829
Al Viro0504c072015-03-21 19:40:11 -0400830 tot_len = iov_iter_count(&iter);
831 if (!tot_len)
832 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800834 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto out;
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (type == READ) {
838 fn = file->f_op->read;
Al Viro293bc982014-02-11 18:37:41 -0500839 iter_fn = file->f_op->read_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 } else {
841 fn = (io_fn_t)file->f_op->write;
Al Viro293bc982014-02-11 18:37:41 -0500842 iter_fn = file->f_op->write_iter;
Al Viro03d95eb2013-03-20 13:04:20 -0400843 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845
Al Viro293bc982014-02-11 18:37:41 -0500846 if (iter_fn)
Al Viroac15ac02015-03-20 20:10:21 -0400847 ret = do_iter_readv_writev(file, &iter, pos, iter_fn);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700848 else
Al Viroac15ac02015-03-20 20:10:21 -0400849 ret = do_loop_readv_writev(file, &iter, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Al Viro03d95eb2013-03-20 13:04:20 -0400851 if (type != READ)
852 file_end_write(file);
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854out:
Al Viro0504c072015-03-21 19:40:11 -0400855 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400856 if ((ret + (type == READ)) > 0) {
857 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500858 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400859 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500860 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
865ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
866 unsigned long vlen, loff_t *pos)
867{
868 if (!(file->f_mode & FMODE_READ))
869 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500870 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return -EINVAL;
872
873 return do_readv_writev(READ, file, vec, vlen, pos);
874}
875
876EXPORT_SYMBOL(vfs_readv);
877
878ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
879 unsigned long vlen, loff_t *pos)
880{
881 if (!(file->f_mode & FMODE_WRITE))
882 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500883 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return -EINVAL;
885
886 return do_readv_writev(WRITE, file, vec, vlen, pos);
887}
888
889EXPORT_SYMBOL(vfs_writev);
890
Heiko Carstens3cdad422009-01-14 14:14:22 +0100891SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
892 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800894 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Al Viro2903ff02012-08-28 12:52:22 -0400897 if (f.file) {
898 loff_t pos = file_pos_read(f.file);
899 ret = vfs_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400900 if (ret >= 0)
901 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800902 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
905 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800906 add_rchar(current, ret);
907 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return ret;
909}
910
Heiko Carstens3cdad422009-01-14 14:14:22 +0100911SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
912 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800914 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Al Viro2903ff02012-08-28 12:52:22 -0400917 if (f.file) {
918 loff_t pos = file_pos_read(f.file);
919 ret = vfs_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400920 if (ret >= 0)
921 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800922 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
925 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800926 add_wchar(current, ret);
927 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return ret;
929}
930
Linus Torvalds601cc112009-04-03 08:03:22 -0700931static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700932{
Linus Torvalds601cc112009-04-03 08:03:22 -0700933#define HALF_LONG_BITS (BITS_PER_LONG / 2)
934 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
935}
936
937SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
938 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
939{
940 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400941 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700942 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700943
944 if (pos < 0)
945 return -EINVAL;
946
Al Viro2903ff02012-08-28 12:52:22 -0400947 f = fdget(fd);
948 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700949 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400950 if (f.file->f_mode & FMODE_PREAD)
951 ret = vfs_readv(f.file, vec, vlen, &pos);
952 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700953 }
954
955 if (ret > 0)
956 add_rchar(current, ret);
957 inc_syscr(current);
958 return ret;
959}
960
961SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700962 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700963{
Linus Torvalds601cc112009-04-03 08:03:22 -0700964 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400965 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700966 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700967
968 if (pos < 0)
969 return -EINVAL;
970
Al Viro2903ff02012-08-28 12:52:22 -0400971 f = fdget(fd);
972 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700973 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400974 if (f.file->f_mode & FMODE_PWRITE)
975 ret = vfs_writev(f.file, vec, vlen, &pos);
976 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700977 }
978
979 if (ret > 0)
980 add_wchar(current, ret);
981 inc_syscw(current);
982 return ret;
983}
984
Al Viro72ec3512013-03-20 10:42:10 -0400985#ifdef CONFIG_COMPAT
986
987static ssize_t compat_do_readv_writev(int type, struct file *file,
988 const struct compat_iovec __user *uvector,
989 unsigned long nr_segs, loff_t *pos)
990{
991 compat_ssize_t tot_len;
992 struct iovec iovstack[UIO_FASTIOV];
993 struct iovec *iov = iovstack;
Al Viroac15ac02015-03-20 20:10:21 -0400994 struct iov_iter iter;
Al Viro72ec3512013-03-20 10:42:10 -0400995 ssize_t ret;
996 io_fn_t fn;
Al Viro293bc982014-02-11 18:37:41 -0500997 iter_fn_t iter_fn;
Al Viro72ec3512013-03-20 10:42:10 -0400998
Al Viro0504c072015-03-21 19:40:11 -0400999 ret = compat_import_iovec(type, uvector, nr_segs,
1000 UIO_FASTIOV, &iov, &iter);
1001 if (ret < 0)
1002 return ret;
Al Viro72ec3512013-03-20 10:42:10 -04001003
Al Viro0504c072015-03-21 19:40:11 -04001004 tot_len = iov_iter_count(&iter);
1005 if (!tot_len)
1006 goto out;
Al Viro72ec3512013-03-20 10:42:10 -04001007 ret = rw_verify_area(type, file, pos, tot_len);
1008 if (ret < 0)
1009 goto out;
1010
Al Viro72ec3512013-03-20 10:42:10 -04001011 if (type == READ) {
1012 fn = file->f_op->read;
Al Viro293bc982014-02-11 18:37:41 -05001013 iter_fn = file->f_op->read_iter;
Al Viro72ec3512013-03-20 10:42:10 -04001014 } else {
1015 fn = (io_fn_t)file->f_op->write;
Al Viro293bc982014-02-11 18:37:41 -05001016 iter_fn = file->f_op->write_iter;
Al Viro03d95eb2013-03-20 13:04:20 -04001017 file_start_write(file);
Al Viro72ec3512013-03-20 10:42:10 -04001018 }
1019
Al Viro293bc982014-02-11 18:37:41 -05001020 if (iter_fn)
Al Viroac15ac02015-03-20 20:10:21 -04001021 ret = do_iter_readv_writev(file, &iter, pos, iter_fn);
Al Viro03d95eb2013-03-20 13:04:20 -04001022 else
Al Viroac15ac02015-03-20 20:10:21 -04001023 ret = do_loop_readv_writev(file, &iter, pos, fn);
Al Viro72ec3512013-03-20 10:42:10 -04001024
Al Viro03d95eb2013-03-20 13:04:20 -04001025 if (type != READ)
1026 file_end_write(file);
1027
Al Viro72ec3512013-03-20 10:42:10 -04001028out:
Al Viro0504c072015-03-21 19:40:11 -04001029 kfree(iov);
Al Viro72ec3512013-03-20 10:42:10 -04001030 if ((ret + (type == READ)) > 0) {
1031 if (type == READ)
1032 fsnotify_access(file);
1033 else
1034 fsnotify_modify(file);
1035 }
1036 return ret;
1037}
1038
1039static size_t compat_readv(struct file *file,
1040 const struct compat_iovec __user *vec,
1041 unsigned long vlen, loff_t *pos)
1042{
1043 ssize_t ret = -EBADF;
1044
1045 if (!(file->f_mode & FMODE_READ))
1046 goto out;
1047
1048 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001049 if (!(file->f_mode & FMODE_CAN_READ))
Al Viro72ec3512013-03-20 10:42:10 -04001050 goto out;
1051
1052 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
1053
1054out:
1055 if (ret > 0)
1056 add_rchar(current, ret);
1057 inc_syscr(current);
1058 return ret;
1059}
1060
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001061COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001062 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001063 compat_ulong_t, vlen)
Al Viro72ec3512013-03-20 10:42:10 -04001064{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001065 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001066 ssize_t ret;
1067 loff_t pos;
1068
1069 if (!f.file)
1070 return -EBADF;
1071 pos = f.file->f_pos;
1072 ret = compat_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001073 if (ret >= 0)
1074 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001075 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001076 return ret;
1077}
1078
Heiko Carstens378a10f2014-03-05 10:43:51 +01001079static long __compat_sys_preadv64(unsigned long fd,
1080 const struct compat_iovec __user *vec,
1081 unsigned long vlen, loff_t pos)
Al Viro72ec3512013-03-20 10:42:10 -04001082{
1083 struct fd f;
1084 ssize_t ret;
1085
1086 if (pos < 0)
1087 return -EINVAL;
1088 f = fdget(fd);
1089 if (!f.file)
1090 return -EBADF;
1091 ret = -ESPIPE;
1092 if (f.file->f_mode & FMODE_PREAD)
1093 ret = compat_readv(f.file, vec, vlen, &pos);
1094 fdput(f);
1095 return ret;
1096}
1097
Heiko Carstens378a10f2014-03-05 10:43:51 +01001098#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1099COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1100 const struct compat_iovec __user *,vec,
1101 unsigned long, vlen, loff_t, pos)
1102{
1103 return __compat_sys_preadv64(fd, vec, vlen, pos);
1104}
1105#endif
1106
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001107COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001108 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001109 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001110{
1111 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001112
1113 return __compat_sys_preadv64(fd, vec, vlen, pos);
Al Viro72ec3512013-03-20 10:42:10 -04001114}
1115
1116static size_t compat_writev(struct file *file,
1117 const struct compat_iovec __user *vec,
1118 unsigned long vlen, loff_t *pos)
1119{
1120 ssize_t ret = -EBADF;
1121
1122 if (!(file->f_mode & FMODE_WRITE))
1123 goto out;
1124
1125 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001126 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro72ec3512013-03-20 10:42:10 -04001127 goto out;
1128
1129 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1130
1131out:
1132 if (ret > 0)
1133 add_wchar(current, ret);
1134 inc_syscw(current);
1135 return ret;
1136}
1137
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001138COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001139 const struct compat_iovec __user *, vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001140 compat_ulong_t, vlen)
Al Viro72ec3512013-03-20 10:42:10 -04001141{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001142 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001143 ssize_t ret;
1144 loff_t pos;
1145
1146 if (!f.file)
1147 return -EBADF;
1148 pos = f.file->f_pos;
1149 ret = compat_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001150 if (ret >= 0)
1151 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001152 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001153 return ret;
1154}
1155
Heiko Carstens378a10f2014-03-05 10:43:51 +01001156static long __compat_sys_pwritev64(unsigned long fd,
1157 const struct compat_iovec __user *vec,
1158 unsigned long vlen, loff_t pos)
Al Viro72ec3512013-03-20 10:42:10 -04001159{
1160 struct fd f;
1161 ssize_t ret;
1162
1163 if (pos < 0)
1164 return -EINVAL;
1165 f = fdget(fd);
1166 if (!f.file)
1167 return -EBADF;
1168 ret = -ESPIPE;
1169 if (f.file->f_mode & FMODE_PWRITE)
1170 ret = compat_writev(f.file, vec, vlen, &pos);
1171 fdput(f);
1172 return ret;
1173}
1174
Heiko Carstens378a10f2014-03-05 10:43:51 +01001175#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1176COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1177 const struct compat_iovec __user *,vec,
1178 unsigned long, vlen, loff_t, pos)
1179{
1180 return __compat_sys_pwritev64(fd, vec, vlen, pos);
1181}
1182#endif
1183
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001184COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001185 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001186 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001187{
1188 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001189
1190 return __compat_sys_pwritev64(fd, vec, vlen, pos);
Al Viro72ec3512013-03-20 10:42:10 -04001191}
1192#endif
1193
Al Viro19f4fc32013-02-24 02:17:03 -05001194static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1195 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Al Viro2903ff02012-08-28 12:52:22 -04001197 struct fd in, out;
1198 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001200 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001202 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 /*
1205 * Get input file, and verify that it is ok..
1206 */
1207 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001208 in = fdget(in_fd);
1209 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001211 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001214 if (!ppos) {
1215 pos = in.file->f_pos;
1216 } else {
1217 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001218 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001220 }
1221 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001222 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001224 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /*
1227 * Get output file, and verify that it is ok..
1228 */
1229 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001230 out = fdget(out_fd);
1231 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001233 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 goto fput_out;
1235 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001236 in_inode = file_inode(in.file);
1237 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001238 out_pos = out.file->f_pos;
1239 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001240 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001242 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (!max)
1245 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (unlikely(pos + count > max)) {
1248 retval = -EOVERFLOW;
1249 if (pos >= max)
1250 goto fput_out;
1251 count = max - pos;
1252 }
1253
Jens Axboed96e6e72007-06-11 12:18:52 +02001254 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001255#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001256 /*
1257 * We need to debate whether we can enable this or not. The
1258 * man page documents EAGAIN return for the output at least,
1259 * and the application is arguably buggy if it doesn't expect
1260 * EAGAIN on a non-blocking file descriptor.
1261 */
Al Viro2903ff02012-08-28 12:52:22 -04001262 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001263 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001264#endif
Al Viro50cd2c52013-05-23 20:10:34 -04001265 file_start_write(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001266 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
Al Viro50cd2c52013-05-23 20:10:34 -04001267 file_end_write(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001270 add_rchar(current, retval);
1271 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001272 fsnotify_access(in.file);
1273 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001274 out.file->f_pos = out_pos;
1275 if (ppos)
1276 *ppos = pos;
1277 else
1278 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001281 inc_syscr(current);
1282 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001283 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 retval = -EOVERFLOW;
1285
1286fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001287 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001289 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290out:
1291 return retval;
1292}
1293
Heiko Carstens002c8972009-01-14 14:14:18 +01001294SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 loff_t pos;
1297 off_t off;
1298 ssize_t ret;
1299
1300 if (offset) {
1301 if (unlikely(get_user(off, offset)))
1302 return -EFAULT;
1303 pos = off;
1304 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1305 if (unlikely(put_user(pos, offset)))
1306 return -EFAULT;
1307 return ret;
1308 }
1309
1310 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1311}
1312
Heiko Carstens002c8972009-01-14 14:14:18 +01001313SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 loff_t pos;
1316 ssize_t ret;
1317
1318 if (offset) {
1319 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1320 return -EFAULT;
1321 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1322 if (unlikely(put_user(pos, offset)))
1323 return -EFAULT;
1324 return ret;
1325 }
1326
1327 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1328}
Al Viro19f4fc32013-02-24 02:17:03 -05001329
1330#ifdef CONFIG_COMPAT
1331COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1332 compat_off_t __user *, offset, compat_size_t, count)
1333{
1334 loff_t pos;
1335 off_t off;
1336 ssize_t ret;
1337
1338 if (offset) {
1339 if (unlikely(get_user(off, offset)))
1340 return -EFAULT;
1341 pos = off;
1342 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1343 if (unlikely(put_user(pos, offset)))
1344 return -EFAULT;
1345 return ret;
1346 }
1347
1348 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1349}
1350
1351COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1352 compat_loff_t __user *, offset, compat_size_t, count)
1353{
1354 loff_t pos;
1355 ssize_t ret;
1356
1357 if (offset) {
1358 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1359 return -EFAULT;
1360 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1361 if (unlikely(put_user(pos, offset)))
1362 return -EFAULT;
1363 return ret;
1364 }
1365
1366 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1367}
1368#endif