blob: f8b8fc1316ab16ac79a3d065b1baa1b3d2f924a2 [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);
Al Viro293bc982014-02-11 18:37:41 -050028typedef ssize_t (*iter_fn_t)(struct kiocb *, struct iov_iter *);
Al Viroc0bd14af2013-05-04 15:00:54 -040029
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080030const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040032 .read = new_sync_read,
33 .read_iter = generic_file_read_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020035 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
38EXPORT_SYMBOL(generic_ro_fops);
39
Al Virocccb5a12010-12-17 07:44:05 -050040static inline int unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070041{
Al Virocccb5a12010-12-17 07:44:05 -050042 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070043}
44
Jie Liu46a1c2c2013-06-25 12:02:13 +080045/**
46 * vfs_setpos - update the file offset for lseek
47 * @file: file structure in question
48 * @offset: file offset to seek to
49 * @maxsize: maximum file size
50 *
51 * This is a low-level filesystem helper for updating the file offset to
52 * the value specified by @offset if the given offset is valid and it is
53 * not equal to the current file offset.
54 *
55 * Return the specified offset on success and -EINVAL on invalid offset.
56 */
57loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070058{
59 if (offset < 0 && !unsigned_offsets(file))
60 return -EINVAL;
61 if (offset > maxsize)
62 return -EINVAL;
63
64 if (offset != file->f_pos) {
65 file->f_pos = offset;
66 file->f_version = 0;
67 }
68 return offset;
69}
Jie Liu46a1c2c2013-06-25 12:02:13 +080070EXPORT_SYMBOL(vfs_setpos);
Andi Kleenef3d0fd2011-09-15 16:06:48 -070071
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020072/**
Andi Kleen57604952011-09-15 16:06:50 -070073 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020074 * @file: file structure to seek on
75 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -080076 * @whence: type of seek
Eric Sandeene8b96eb2012-04-30 13:11:29 -050077 * @size: max size of this file in file system
78 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020079 *
Andi Kleen57604952011-09-15 16:06:50 -070080 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050081 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070082 *
83 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070084 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070085 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
86 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020087 */
Andi Kleen9465efc2008-06-27 11:05:24 +020088loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -080089generic_file_llseek_size(struct file *file, loff_t offset, int whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050090 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Andrew Morton965c8e52012-12-17 15:59:39 -080092 switch (whence) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020093 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050094 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020095 break;
96 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080097 /*
98 * Here we special-case the lseek(fd, 0, SEEK_CUR)
99 * position-querying operation. Avoid rewriting the "same"
100 * f_pos value back to the file because a concurrent read(),
101 * write() or lseek() might have altered it
102 */
103 if (offset == 0)
104 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700105 /*
106 * f_lock protects against read/modify/write race with other
107 * SEEK_CURs. Note that parallel writes and reads behave
108 * like SEEK_SET.
109 */
110 spin_lock(&file->f_lock);
Jie Liu46a1c2c2013-06-25 12:02:13 +0800111 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700112 spin_unlock(&file->f_lock);
113 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -0400114 case SEEK_DATA:
115 /*
116 * In the generic case the entire file is data, so as long as
117 * offset isn't at the end of the file then the offset is data.
118 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500119 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400120 return -ENXIO;
121 break;
122 case SEEK_HOLE:
123 /*
124 * There is a virtual hole at the end of the file, so as long as
125 * offset isn't i_size or larger, return i_size.
126 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500127 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400128 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500129 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200132
Jie Liu46a1c2c2013-06-25 12:02:13 +0800133 return vfs_setpos(file, offset, maxsize);
Andi Kleen57604952011-09-15 16:06:50 -0700134}
135EXPORT_SYMBOL(generic_file_llseek_size);
136
137/**
138 * generic_file_llseek - generic llseek implementation for regular files
139 * @file: file structure to seek on
140 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800141 * @whence: type of seek
Andi Kleen57604952011-09-15 16:06:50 -0700142 *
143 * This is a generic implemenation of ->llseek useable for all normal local
144 * filesystems. It just updates the file offset to the value specified by
Ming Lei546ae2d2013-04-29 15:06:07 -0700145 * @offset and @whence.
Andi Kleen57604952011-09-15 16:06:50 -0700146 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800147loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
Andi Kleen57604952011-09-15 16:06:50 -0700148{
149 struct inode *inode = file->f_mapping->host;
150
Andrew Morton965c8e52012-12-17 15:59:39 -0800151 return generic_file_llseek_size(file, offset, whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500152 inode->i_sb->s_maxbytes,
153 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
Andi Kleen9465efc2008-06-27 11:05:24 +0200155EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
jan Blunckae6afc32010-05-26 14:44:48 -0700157/**
Al Viro1bf9d142013-06-16 20:27:42 +0400158 * fixed_size_llseek - llseek implementation for fixed-sized devices
159 * @file: file structure to seek on
160 * @offset: file offset to seek to
161 * @whence: type of seek
162 * @size: size of the file
163 *
164 */
165loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
166{
167 switch (whence) {
168 case SEEK_SET: case SEEK_CUR: case SEEK_END:
169 return generic_file_llseek_size(file, offset, whence,
170 size, size);
171 default:
172 return -EINVAL;
173 }
174}
175EXPORT_SYMBOL(fixed_size_llseek);
176
177/**
jan Blunckae6afc32010-05-26 14:44:48 -0700178 * noop_llseek - No Operation Performed llseek implementation
179 * @file: file structure to seek on
180 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800181 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700182 *
183 * This is an implementation of ->llseek useable for the rare special case when
184 * userspace expects the seek to succeed but the (device) file is actually not
185 * able to perform the seek. In this case you use noop_llseek() instead of
186 * falling back to the default implementation of ->llseek.
187 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800188loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700189{
190 return file->f_pos;
191}
192EXPORT_SYMBOL(noop_llseek);
193
Andrew Morton965c8e52012-12-17 15:59:39 -0800194loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 return -ESPIPE;
197}
198EXPORT_SYMBOL(no_llseek);
199
Andrew Morton965c8e52012-12-17 15:59:39 -0800200loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Al Viro496ad9a2013-01-23 17:07:38 -0500202 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200203 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Josef Bacik982d8162011-07-18 13:21:35 -0400205 mutex_lock(&inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -0800206 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700207 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400208 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700210 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800211 if (offset == 0) {
212 retval = file->f_pos;
213 goto out;
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400216 break;
217 case SEEK_DATA:
218 /*
219 * In the generic case the entire file is data, so as
220 * long as offset isn't at the end of the file then the
221 * offset is data.
222 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300223 if (offset >= inode->i_size) {
224 retval = -ENXIO;
225 goto out;
226 }
Josef Bacik982d8162011-07-18 13:21:35 -0400227 break;
228 case SEEK_HOLE:
229 /*
230 * There is a virtual hole at the end of the file, so
231 * as long as offset isn't i_size or larger, return
232 * i_size.
233 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300234 if (offset >= inode->i_size) {
235 retval = -ENXIO;
236 goto out;
237 }
Josef Bacik982d8162011-07-18 13:21:35 -0400238 offset = inode->i_size;
239 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500242 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (offset != file->f_pos) {
244 file->f_pos = offset;
245 file->f_version = 0;
246 }
247 retval = offset;
248 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800249out:
Josef Bacik982d8162011-07-18 13:21:35 -0400250 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return retval;
252}
253EXPORT_SYMBOL(default_llseek);
254
Andrew Morton965c8e52012-12-17 15:59:39 -0800255loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 loff_t (*fn)(struct file *, loff_t, int);
258
259 fn = no_llseek;
260 if (file->f_mode & FMODE_LSEEK) {
Al Viro72c2d532013-09-22 16:27:52 -0400261 if (file->f_op->llseek)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 fn = file->f_op->llseek;
263 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800264 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266EXPORT_SYMBOL(vfs_llseek);
267
Linus Torvalds9c225f22014-03-03 09:36:58 -0800268static inline struct fd fdget_pos(int fd)
269{
Al Virobd2a31d2014-03-04 14:54:22 -0500270 return __to_fd(__fdget_pos(fd));
Linus Torvalds9c225f22014-03-03 09:36:58 -0800271}
272
273static inline void fdput_pos(struct fd f)
274{
275 if (f.flags & FDPUT_POS_UNLOCK)
276 mutex_unlock(&f.file->f_pos_lock);
277 fdput(f);
278}
279
Andrew Morton965c8e52012-12-17 15:59:39 -0800280SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 off_t retval;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800283 struct fd f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400284 if (!f.file)
285 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800288 if (whence <= SEEK_MAX) {
289 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 retval = res;
291 if (res != (loff_t)retval)
292 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
293 }
Linus Torvalds9c225f22014-03-03 09:36:58 -0800294 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return retval;
296}
297
Al Viro561c6732013-02-24 10:52:26 -0500298#ifdef CONFIG_COMPAT
299COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
300{
301 return sys_lseek(fd, offset, whence);
302}
303#endif
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100306SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
307 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800308 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 int retval;
Eric Biggersd7a15f82014-03-16 14:24:08 -0500311 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Al Viro2903ff02012-08-28 12:52:22 -0400314 if (!f.file)
315 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800318 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 goto out_putf;
320
Al Viro2903ff02012-08-28 12:52:22 -0400321 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800322 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 retval = (int)offset;
325 if (offset >= 0) {
326 retval = -EFAULT;
327 if (!copy_to_user(result, &offset, sizeof(offset)))
328 retval = 0;
329 }
330out_putf:
Eric Biggersd7a15f82014-03-16 14:24:08 -0500331 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return retval;
333}
334#endif
335
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100336ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
337{
338 struct kiocb kiocb;
339 ssize_t ret;
340
341 if (!file->f_op->read_iter)
342 return -EINVAL;
343
344 init_sync_kiocb(&kiocb, file);
345 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100346
347 iter->type |= READ;
348 ret = file->f_op->read_iter(&kiocb, iter);
349 if (ret == -EIOCBQUEUED)
350 ret = wait_on_sync_kiocb(&kiocb);
351
352 if (ret > 0)
353 *ppos = kiocb.ki_pos;
354 return ret;
355}
356EXPORT_SYMBOL(vfs_iter_read);
357
358ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
359{
360 struct kiocb kiocb;
361 ssize_t ret;
362
363 if (!file->f_op->write_iter)
364 return -EINVAL;
365
366 init_sync_kiocb(&kiocb, file);
367 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100368
369 iter->type |= WRITE;
370 ret = file->f_op->write_iter(&kiocb, iter);
371 if (ret == -EIOCBQUEUED)
372 ret = wait_on_sync_kiocb(&kiocb);
373
374 if (ret > 0)
375 *ppos = kiocb.ki_pos;
376 return ret;
377}
378EXPORT_SYMBOL(vfs_iter_write);
379
Linus Torvaldse28cc712006-01-04 16:20:40 -0800380/*
381 * rw_verify_area doesn't like huge counts. We limit
382 * them to something that fits in "int" so that others
383 * won't have to do range checks all the time.
384 */
Al Viro68d70d02013-06-19 15:26:04 +0400385int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct inode *inode;
388 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100389 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Al Viro496ad9a2013-01-23 17:07:38 -0500391 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800392 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100393 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500395 if (unlikely(pos < 0)) {
396 if (!unsigned_offsets(file))
397 return retval;
398 if (count >= -pos) /* both values are in 0..LLONG_MAX */
399 return -EOVERFLOW;
400 } else if (unlikely((loff_t) (pos + count) < 0)) {
401 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700402 return retval;
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500405 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
James Morrisc43e2592008-01-12 22:05:48 +1100406 retval = locks_mandatory_area(
Linus Torvaldse28cc712006-01-04 16:20:40 -0800407 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
408 inode, file, pos, count);
409 if (retval < 0)
410 return retval;
411 }
James Morrisc43e2592008-01-12 22:05:48 +1100412 retval = security_file_permission(file,
413 read_write == READ ? MAY_READ : MAY_WRITE);
414 if (retval)
415 return retval;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800416 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
420{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700421 struct iovec iov = { .iov_base = buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct kiocb kiocb;
423 ssize_t ret;
424
425 init_sync_kiocb(&kiocb, filp);
426 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700427
Zach Brown41003a72013-05-07 16:18:25 -0700428 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (-EIOCBQUEUED == ret)
430 ret = wait_on_sync_kiocb(&kiocb);
431 *ppos = kiocb.ki_pos;
432 return ret;
433}
434
435EXPORT_SYMBOL(do_sync_read);
436
Al Viro293bc982014-02-11 18:37:41 -0500437ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
438{
439 struct iovec iov = { .iov_base = buf, .iov_len = len };
440 struct kiocb kiocb;
441 struct iov_iter iter;
442 ssize_t ret;
443
444 init_sync_kiocb(&kiocb, filp);
445 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500446 iov_iter_init(&iter, READ, &iov, 1, len);
447
448 ret = filp->f_op->read_iter(&kiocb, &iter);
449 if (-EIOCBQUEUED == ret)
450 ret = wait_on_sync_kiocb(&kiocb);
451 *ppos = kiocb.ki_pos;
452 return ret;
453}
454
455EXPORT_SYMBOL(new_sync_read);
456
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200457ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
458 loff_t *pos)
459{
460 ssize_t ret;
461
462 if (file->f_op->read)
463 ret = file->f_op->read(file, buf, count, pos);
464 else if (file->f_op->aio_read)
465 ret = do_sync_read(file, buf, count, pos);
466 else if (file->f_op->read_iter)
467 ret = new_sync_read(file, buf, count, pos);
468 else
469 ret = -EINVAL;
470
471 return ret;
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
475{
476 ssize_t ret;
477
478 if (!(file->f_mode & FMODE_READ))
479 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500480 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return -EINVAL;
482 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
483 return -EFAULT;
484
485 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800486 if (ret >= 0) {
487 count = ret;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200488 ret = __vfs_read(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100489 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500490 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100491 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
James Morrisc43e2592008-01-12 22:05:48 +1100493 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495
496 return ret;
497}
498
499EXPORT_SYMBOL(vfs_read);
500
501ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
502{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700503 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct kiocb kiocb;
505 ssize_t ret;
506
507 init_sync_kiocb(&kiocb, filp);
508 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700509
Zach Brown41003a72013-05-07 16:18:25 -0700510 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (-EIOCBQUEUED == ret)
512 ret = wait_on_sync_kiocb(&kiocb);
513 *ppos = kiocb.ki_pos;
514 return ret;
515}
516
517EXPORT_SYMBOL(do_sync_write);
518
Al Viro293bc982014-02-11 18:37:41 -0500519ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
520{
521 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
522 struct kiocb kiocb;
523 struct iov_iter iter;
524 ssize_t ret;
525
526 init_sync_kiocb(&kiocb, filp);
527 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500528 iov_iter_init(&iter, WRITE, &iov, 1, len);
529
530 ret = filp->f_op->write_iter(&kiocb, &iter);
531 if (-EIOCBQUEUED == ret)
532 ret = wait_on_sync_kiocb(&kiocb);
533 *ppos = kiocb.ki_pos;
534 return ret;
535}
536
537EXPORT_SYMBOL(new_sync_write);
538
Al Viro06ae43f2013-03-20 13:19:30 -0400539ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
540{
541 mm_segment_t old_fs;
542 const char __user *p;
543 ssize_t ret;
544
Al Viro7f7f25e2014-02-11 17:49:24 -0500545 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro3e84f482013-03-27 15:20:30 +0000546 return -EINVAL;
547
Al Viro06ae43f2013-03-20 13:19:30 -0400548 old_fs = get_fs();
549 set_fs(get_ds());
550 p = (__force const char __user *)buf;
551 if (count > MAX_RW_COUNT)
552 count = MAX_RW_COUNT;
553 if (file->f_op->write)
554 ret = file->f_op->write(file, p, count, pos);
Al Viro293bc982014-02-11 18:37:41 -0500555 else if (file->f_op->aio_write)
Al Viro06ae43f2013-03-20 13:19:30 -0400556 ret = do_sync_write(file, p, count, pos);
Al Viro293bc982014-02-11 18:37:41 -0500557 else
558 ret = new_sync_write(file, p, count, pos);
Al Viro06ae43f2013-03-20 13:19:30 -0400559 set_fs(old_fs);
560 if (ret > 0) {
561 fsnotify_modify(file);
562 add_wchar(current, ret);
563 }
564 inc_syscw(current);
565 return ret;
566}
567
Al Viro2ec3a122014-08-19 11:48:09 -0400568EXPORT_SYMBOL(__kernel_write);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
571{
572 ssize_t ret;
573
574 if (!(file->f_mode & FMODE_WRITE))
575 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500576 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return -EINVAL;
578 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
579 return -EFAULT;
580
581 ret = rw_verify_area(WRITE, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800582 if (ret >= 0) {
583 count = ret;
Al Viro03d95eb2013-03-20 13:04:20 -0400584 file_start_write(file);
James Morrisc43e2592008-01-12 22:05:48 +1100585 if (file->f_op->write)
586 ret = file->f_op->write(file, buf, count, pos);
Al Viro293bc982014-02-11 18:37:41 -0500587 else if (file->f_op->aio_write)
James Morrisc43e2592008-01-12 22:05:48 +1100588 ret = do_sync_write(file, buf, count, pos);
Al Viro293bc982014-02-11 18:37:41 -0500589 else
590 ret = new_sync_write(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100591 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500592 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100593 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
James Morrisc43e2592008-01-12 22:05:48 +1100595 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400596 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
599 return ret;
600}
601
602EXPORT_SYMBOL(vfs_write);
603
604static inline loff_t file_pos_read(struct file *file)
605{
606 return file->f_pos;
607}
608
609static inline void file_pos_write(struct file *file, loff_t pos)
610{
611 file->f_pos = pos;
612}
613
Heiko Carstens3cdad422009-01-14 14:14:22 +0100614SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800616 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Al Viro2903ff02012-08-28 12:52:22 -0400619 if (f.file) {
620 loff_t pos = file_pos_read(f.file);
621 ret = vfs_read(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400622 if (ret >= 0)
623 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800624 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return ret;
627}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Heiko Carstens3cdad422009-01-14 14:14:22 +0100629SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
630 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800632 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Al Viro2903ff02012-08-28 12:52:22 -0400635 if (f.file) {
636 loff_t pos = file_pos_read(f.file);
637 ret = vfs_write(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400638 if (ret >= 0)
639 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800640 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
643 return ret;
644}
645
Al Viro4a0fd5b2013-01-21 15:16:58 -0500646SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
647 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Al Viro2903ff02012-08-28 12:52:22 -0400649 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (pos < 0)
653 return -EINVAL;
654
Al Viro2903ff02012-08-28 12:52:22 -0400655 f = fdget(fd);
656 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400658 if (f.file->f_mode & FMODE_PREAD)
659 ret = vfs_read(f.file, buf, count, &pos);
660 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
663 return ret;
664}
665
Al Viro4a0fd5b2013-01-21 15:16:58 -0500666SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
667 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Al Viro2903ff02012-08-28 12:52:22 -0400669 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 if (pos < 0)
673 return -EINVAL;
674
Al Viro2903ff02012-08-28 12:52:22 -0400675 f = fdget(fd);
676 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400678 if (f.file->f_mode & FMODE_PWRITE)
679 ret = vfs_write(f.file, buf, count, &pos);
680 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
683 return ret;
684}
685
686/*
687 * Reduce an iovec's length in-place. Return the resulting number of segments
688 */
689unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
690{
691 unsigned long seg = 0;
692 size_t len = 0;
693
694 while (seg < nr_segs) {
695 seg++;
696 if (len + iov->iov_len >= to) {
697 iov->iov_len = to - len;
698 break;
699 }
700 len += iov->iov_len;
701 iov++;
702 }
703 return seg;
704}
Eric Sandeen19295522008-01-28 23:58:27 -0500705EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Al Viro293bc982014-02-11 18:37:41 -0500707static ssize_t do_iter_readv_writev(struct file *filp, int rw, const struct iovec *iov,
708 unsigned long nr_segs, size_t len, loff_t *ppos, iter_fn_t fn)
709{
710 struct kiocb kiocb;
711 struct iov_iter iter;
712 ssize_t ret;
713
714 init_sync_kiocb(&kiocb, filp);
715 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500716
717 iov_iter_init(&iter, rw, iov, nr_segs, len);
718 ret = fn(&kiocb, &iter);
719 if (ret == -EIOCBQUEUED)
720 ret = wait_on_sync_kiocb(&kiocb);
721 *ppos = kiocb.ki_pos;
722 return ret;
723}
724
Al Viro72ec3512013-03-20 10:42:10 -0400725static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700726 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
727{
728 struct kiocb kiocb;
729 ssize_t ret;
730
731 init_sync_kiocb(&kiocb, filp);
732 kiocb.ki_pos = *ppos;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700733
Zach Brown41003a72013-05-07 16:18:25 -0700734 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700735 if (ret == -EIOCBQUEUED)
736 ret = wait_on_sync_kiocb(&kiocb);
737 *ppos = kiocb.ki_pos;
738 return ret;
739}
740
741/* Do it by hand, with file-ops */
Al Viro72ec3512013-03-20 10:42:10 -0400742static ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700743 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
744{
745 struct iovec *vector = iov;
746 ssize_t ret = 0;
747
748 while (nr_segs > 0) {
749 void __user *base;
750 size_t len;
751 ssize_t nr;
752
753 base = vector->iov_base;
754 len = vector->iov_len;
755 vector++;
756 nr_segs--;
757
758 nr = fn(filp, base, len, ppos);
759
760 if (nr < 0) {
761 if (!ret)
762 ret = nr;
763 break;
764 }
765 ret += nr;
766 if (nr != len)
767 break;
768 }
769
770 return ret;
771}
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773/* A write operation does a read from user space and vice versa */
774#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
775
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700776ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
777 unsigned long nr_segs, unsigned long fast_segs,
778 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700779 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700780{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700781 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700782 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700783 struct iovec *iov = fast_pointer;
784
Linus Torvalds435f49a2010-10-29 10:36:49 -0700785 /*
786 * SuS says "The readv() function *may* fail if the iovcnt argument
787 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
788 * traditionally returned zero for zero segments, so...
789 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700790 if (nr_segs == 0) {
791 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700792 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700793 }
794
Linus Torvalds435f49a2010-10-29 10:36:49 -0700795 /*
796 * First get the "struct iovec" from user memory and
797 * verify all the pointers
798 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700799 if (nr_segs > UIO_MAXIOV) {
800 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700801 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700802 }
803 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700804 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700805 if (iov == NULL) {
806 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700807 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700808 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700809 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700810 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
811 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700812 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700813 }
814
Linus Torvalds435f49a2010-10-29 10:36:49 -0700815 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700816 * According to the Single Unix Specification we should return EINVAL
817 * if an element length is < 0 when cast to ssize_t or if the
818 * total length would overflow the ssize_t return value of the
819 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700820 *
821 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
822 * overflow case.
823 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700824 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700825 for (seg = 0; seg < nr_segs; seg++) {
826 void __user *buf = iov[seg].iov_base;
827 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700828
829 /* see if we we're about to use an invalid len or if
830 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700831 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700832 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700833 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700834 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700835 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700836 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700837 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700838 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700839 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700840 if (len > MAX_RW_COUNT - ret) {
841 len = MAX_RW_COUNT - ret;
842 iov[seg].iov_len = len;
843 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700844 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700845 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700846out:
847 *ret_pointer = iov;
848 return ret;
849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851static ssize_t do_readv_writev(int type, struct file *file,
852 const struct iovec __user * uvector,
853 unsigned long nr_segs, loff_t *pos)
854{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 size_t tot_len;
856 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700857 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 io_fn_t fn;
860 iov_fn_t fnv;
Al Viro293bc982014-02-11 18:37:41 -0500861 iter_fn_t iter_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700863 ret = rw_copy_check_uvector(type, uvector, nr_segs,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700864 ARRAY_SIZE(iovstack), iovstack, &iov);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700865 if (ret <= 0)
866 goto out;
867
868 tot_len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800870 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 goto out;
872
873 fnv = NULL;
874 if (type == READ) {
875 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700876 fnv = file->f_op->aio_read;
Al Viro293bc982014-02-11 18:37:41 -0500877 iter_fn = file->f_op->read_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 } else {
879 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700880 fnv = file->f_op->aio_write;
Al Viro293bc982014-02-11 18:37:41 -0500881 iter_fn = file->f_op->write_iter;
Al Viro03d95eb2013-03-20 13:04:20 -0400882 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
Al Viro293bc982014-02-11 18:37:41 -0500885 if (iter_fn)
886 ret = do_iter_readv_writev(file, type, iov, nr_segs, tot_len,
887 pos, iter_fn);
888 else if (fnv)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700889 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
890 pos, fnv);
891 else
892 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Al Viro03d95eb2013-03-20 13:04:20 -0400894 if (type != READ)
895 file_end_write(file);
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897out:
898 if (iov != iovstack)
899 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400900 if ((ret + (type == READ)) > 0) {
901 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500902 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400903 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500904 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
910 unsigned long vlen, loff_t *pos)
911{
912 if (!(file->f_mode & FMODE_READ))
913 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500914 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return -EINVAL;
916
917 return do_readv_writev(READ, file, vec, vlen, pos);
918}
919
920EXPORT_SYMBOL(vfs_readv);
921
922ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
923 unsigned long vlen, loff_t *pos)
924{
925 if (!(file->f_mode & FMODE_WRITE))
926 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500927 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return -EINVAL;
929
930 return do_readv_writev(WRITE, file, vec, vlen, pos);
931}
932
933EXPORT_SYMBOL(vfs_writev);
934
Heiko Carstens3cdad422009-01-14 14:14:22 +0100935SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
936 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800938 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Al Viro2903ff02012-08-28 12:52:22 -0400941 if (f.file) {
942 loff_t pos = file_pos_read(f.file);
943 ret = vfs_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400944 if (ret >= 0)
945 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800946 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948
949 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800950 add_rchar(current, ret);
951 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return ret;
953}
954
Heiko Carstens3cdad422009-01-14 14:14:22 +0100955SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
956 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800958 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Al Viro2903ff02012-08-28 12:52:22 -0400961 if (f.file) {
962 loff_t pos = file_pos_read(f.file);
963 ret = vfs_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400964 if (ret >= 0)
965 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800966 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
969 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800970 add_wchar(current, ret);
971 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return ret;
973}
974
Linus Torvalds601cc112009-04-03 08:03:22 -0700975static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700976{
Linus Torvalds601cc112009-04-03 08:03:22 -0700977#define HALF_LONG_BITS (BITS_PER_LONG / 2)
978 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
979}
980
981SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
982 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
983{
984 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400985 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700986 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700987
988 if (pos < 0)
989 return -EINVAL;
990
Al Viro2903ff02012-08-28 12:52:22 -0400991 f = fdget(fd);
992 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700993 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400994 if (f.file->f_mode & FMODE_PREAD)
995 ret = vfs_readv(f.file, vec, vlen, &pos);
996 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700997 }
998
999 if (ret > 0)
1000 add_rchar(current, ret);
1001 inc_syscr(current);
1002 return ret;
1003}
1004
1005SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -07001006 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001007{
Linus Torvalds601cc112009-04-03 08:03:22 -07001008 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -04001009 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001010 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001011
1012 if (pos < 0)
1013 return -EINVAL;
1014
Al Viro2903ff02012-08-28 12:52:22 -04001015 f = fdget(fd);
1016 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001017 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -04001018 if (f.file->f_mode & FMODE_PWRITE)
1019 ret = vfs_writev(f.file, vec, vlen, &pos);
1020 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001021 }
1022
1023 if (ret > 0)
1024 add_wchar(current, ret);
1025 inc_syscw(current);
1026 return ret;
1027}
1028
Al Viro72ec3512013-03-20 10:42:10 -04001029#ifdef CONFIG_COMPAT
1030
1031static ssize_t compat_do_readv_writev(int type, struct file *file,
1032 const struct compat_iovec __user *uvector,
1033 unsigned long nr_segs, loff_t *pos)
1034{
1035 compat_ssize_t tot_len;
1036 struct iovec iovstack[UIO_FASTIOV];
1037 struct iovec *iov = iovstack;
1038 ssize_t ret;
1039 io_fn_t fn;
1040 iov_fn_t fnv;
Al Viro293bc982014-02-11 18:37:41 -05001041 iter_fn_t iter_fn;
Al Viro72ec3512013-03-20 10:42:10 -04001042
Al Viro72ec3512013-03-20 10:42:10 -04001043 ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
1044 UIO_FASTIOV, iovstack, &iov);
1045 if (ret <= 0)
1046 goto out;
1047
1048 tot_len = ret;
1049 ret = rw_verify_area(type, file, pos, tot_len);
1050 if (ret < 0)
1051 goto out;
1052
1053 fnv = NULL;
1054 if (type == READ) {
1055 fn = file->f_op->read;
1056 fnv = file->f_op->aio_read;
Al Viro293bc982014-02-11 18:37:41 -05001057 iter_fn = file->f_op->read_iter;
Al Viro72ec3512013-03-20 10:42:10 -04001058 } else {
1059 fn = (io_fn_t)file->f_op->write;
1060 fnv = file->f_op->aio_write;
Al Viro293bc982014-02-11 18:37:41 -05001061 iter_fn = file->f_op->write_iter;
Al Viro03d95eb2013-03-20 13:04:20 -04001062 file_start_write(file);
Al Viro72ec3512013-03-20 10:42:10 -04001063 }
1064
Al Viro293bc982014-02-11 18:37:41 -05001065 if (iter_fn)
1066 ret = do_iter_readv_writev(file, type, iov, nr_segs, tot_len,
1067 pos, iter_fn);
1068 else if (fnv)
Al Viro72ec3512013-03-20 10:42:10 -04001069 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
1070 pos, fnv);
Al Viro03d95eb2013-03-20 13:04:20 -04001071 else
Al Viro72ec3512013-03-20 10:42:10 -04001072 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
1073
Al Viro03d95eb2013-03-20 13:04:20 -04001074 if (type != READ)
1075 file_end_write(file);
1076
Al Viro72ec3512013-03-20 10:42:10 -04001077out:
1078 if (iov != iovstack)
1079 kfree(iov);
1080 if ((ret + (type == READ)) > 0) {
1081 if (type == READ)
1082 fsnotify_access(file);
1083 else
1084 fsnotify_modify(file);
1085 }
1086 return ret;
1087}
1088
1089static size_t compat_readv(struct file *file,
1090 const struct compat_iovec __user *vec,
1091 unsigned long vlen, loff_t *pos)
1092{
1093 ssize_t ret = -EBADF;
1094
1095 if (!(file->f_mode & FMODE_READ))
1096 goto out;
1097
1098 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001099 if (!(file->f_mode & FMODE_CAN_READ))
Al Viro72ec3512013-03-20 10:42:10 -04001100 goto out;
1101
1102 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
1103
1104out:
1105 if (ret > 0)
1106 add_rchar(current, ret);
1107 inc_syscr(current);
1108 return ret;
1109}
1110
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001111COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001112 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001113 compat_ulong_t, vlen)
Al Viro72ec3512013-03-20 10:42:10 -04001114{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001115 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001116 ssize_t ret;
1117 loff_t pos;
1118
1119 if (!f.file)
1120 return -EBADF;
1121 pos = f.file->f_pos;
1122 ret = compat_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001123 if (ret >= 0)
1124 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001125 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001126 return ret;
1127}
1128
Heiko Carstens378a10f2014-03-05 10:43:51 +01001129static long __compat_sys_preadv64(unsigned long fd,
1130 const struct compat_iovec __user *vec,
1131 unsigned long vlen, loff_t pos)
Al Viro72ec3512013-03-20 10:42:10 -04001132{
1133 struct fd f;
1134 ssize_t ret;
1135
1136 if (pos < 0)
1137 return -EINVAL;
1138 f = fdget(fd);
1139 if (!f.file)
1140 return -EBADF;
1141 ret = -ESPIPE;
1142 if (f.file->f_mode & FMODE_PREAD)
1143 ret = compat_readv(f.file, vec, vlen, &pos);
1144 fdput(f);
1145 return ret;
1146}
1147
Heiko Carstens378a10f2014-03-05 10:43:51 +01001148#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1149COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1150 const struct compat_iovec __user *,vec,
1151 unsigned long, vlen, loff_t, pos)
1152{
1153 return __compat_sys_preadv64(fd, vec, vlen, pos);
1154}
1155#endif
1156
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001157COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001158 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001159 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001160{
1161 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001162
1163 return __compat_sys_preadv64(fd, vec, vlen, pos);
Al Viro72ec3512013-03-20 10:42:10 -04001164}
1165
1166static size_t compat_writev(struct file *file,
1167 const struct compat_iovec __user *vec,
1168 unsigned long vlen, loff_t *pos)
1169{
1170 ssize_t ret = -EBADF;
1171
1172 if (!(file->f_mode & FMODE_WRITE))
1173 goto out;
1174
1175 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001176 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro72ec3512013-03-20 10:42:10 -04001177 goto out;
1178
1179 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1180
1181out:
1182 if (ret > 0)
1183 add_wchar(current, ret);
1184 inc_syscw(current);
1185 return ret;
1186}
1187
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001188COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001189 const struct compat_iovec __user *, vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001190 compat_ulong_t, vlen)
Al Viro72ec3512013-03-20 10:42:10 -04001191{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001192 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001193 ssize_t ret;
1194 loff_t pos;
1195
1196 if (!f.file)
1197 return -EBADF;
1198 pos = f.file->f_pos;
1199 ret = compat_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001200 if (ret >= 0)
1201 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001202 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001203 return ret;
1204}
1205
Heiko Carstens378a10f2014-03-05 10:43:51 +01001206static long __compat_sys_pwritev64(unsigned long fd,
1207 const struct compat_iovec __user *vec,
1208 unsigned long vlen, loff_t pos)
Al Viro72ec3512013-03-20 10:42:10 -04001209{
1210 struct fd f;
1211 ssize_t ret;
1212
1213 if (pos < 0)
1214 return -EINVAL;
1215 f = fdget(fd);
1216 if (!f.file)
1217 return -EBADF;
1218 ret = -ESPIPE;
1219 if (f.file->f_mode & FMODE_PWRITE)
1220 ret = compat_writev(f.file, vec, vlen, &pos);
1221 fdput(f);
1222 return ret;
1223}
1224
Heiko Carstens378a10f2014-03-05 10:43:51 +01001225#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1226COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1227 const struct compat_iovec __user *,vec,
1228 unsigned long, vlen, loff_t, pos)
1229{
1230 return __compat_sys_pwritev64(fd, vec, vlen, pos);
1231}
1232#endif
1233
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001234COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001235 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001236 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001237{
1238 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001239
1240 return __compat_sys_pwritev64(fd, vec, vlen, pos);
Al Viro72ec3512013-03-20 10:42:10 -04001241}
1242#endif
1243
Al Viro19f4fc32013-02-24 02:17:03 -05001244static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1245 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Al Viro2903ff02012-08-28 12:52:22 -04001247 struct fd in, out;
1248 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001250 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001252 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /*
1255 * Get input file, and verify that it is ok..
1256 */
1257 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001258 in = fdget(in_fd);
1259 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001261 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001264 if (!ppos) {
1265 pos = in.file->f_pos;
1266 } else {
1267 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001268 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001270 }
1271 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001272 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001274 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /*
1277 * Get output file, and verify that it is ok..
1278 */
1279 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001280 out = fdget(out_fd);
1281 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001283 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto fput_out;
1285 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001286 in_inode = file_inode(in.file);
1287 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001288 out_pos = out.file->f_pos;
1289 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001290 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001292 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (!max)
1295 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (unlikely(pos + count > max)) {
1298 retval = -EOVERFLOW;
1299 if (pos >= max)
1300 goto fput_out;
1301 count = max - pos;
1302 }
1303
Jens Axboed96e6e72007-06-11 12:18:52 +02001304 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001305#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001306 /*
1307 * We need to debate whether we can enable this or not. The
1308 * man page documents EAGAIN return for the output at least,
1309 * and the application is arguably buggy if it doesn't expect
1310 * EAGAIN on a non-blocking file descriptor.
1311 */
Al Viro2903ff02012-08-28 12:52:22 -04001312 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001313 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001314#endif
Al Viro50cd2c52013-05-23 20:10:34 -04001315 file_start_write(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001316 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
Al Viro50cd2c52013-05-23 20:10:34 -04001317 file_end_write(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001320 add_rchar(current, retval);
1321 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001322 fsnotify_access(in.file);
1323 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001324 out.file->f_pos = out_pos;
1325 if (ppos)
1326 *ppos = pos;
1327 else
1328 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001331 inc_syscr(current);
1332 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001333 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 retval = -EOVERFLOW;
1335
1336fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001337 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001339 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340out:
1341 return retval;
1342}
1343
Heiko Carstens002c8972009-01-14 14:14:18 +01001344SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 loff_t pos;
1347 off_t off;
1348 ssize_t ret;
1349
1350 if (offset) {
1351 if (unlikely(get_user(off, offset)))
1352 return -EFAULT;
1353 pos = off;
1354 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1355 if (unlikely(put_user(pos, offset)))
1356 return -EFAULT;
1357 return ret;
1358 }
1359
1360 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1361}
1362
Heiko Carstens002c8972009-01-14 14:14:18 +01001363SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 loff_t pos;
1366 ssize_t ret;
1367
1368 if (offset) {
1369 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1370 return -EFAULT;
1371 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1372 if (unlikely(put_user(pos, offset)))
1373 return -EFAULT;
1374 return ret;
1375 }
1376
1377 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1378}
Al Viro19f4fc32013-02-24 02:17:03 -05001379
1380#ifdef CONFIG_COMPAT
1381COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1382 compat_off_t __user *, offset, compat_size_t, count)
1383{
1384 loff_t pos;
1385 off_t off;
1386 ssize_t ret;
1387
1388 if (offset) {
1389 if (unlikely(get_user(off, offset)))
1390 return -EFAULT;
1391 pos = off;
1392 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1393 if (unlikely(put_user(pos, offset)))
1394 return -EFAULT;
1395 return ret;
1396 }
1397
1398 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1399}
1400
1401COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1402 compat_loff_t __user *, offset, compat_size_t, count)
1403{
1404 loff_t pos;
1405 ssize_t ret;
1406
1407 if (offset) {
1408 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1409 return -EFAULT;
1410 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1411 if (unlikely(put_user(pos, offset)))
1412 return -EFAULT;
1413 return ret;
1414 }
1415
1416 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1417}
1418#endif