blob: 76e324e8ce8d35dc30f51a487db8d7c3b816b164 [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);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100349 BUG_ON(ret == -EIOCBQUEUED);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100350 if (ret > 0)
351 *ppos = kiocb.ki_pos;
352 return ret;
353}
354EXPORT_SYMBOL(vfs_iter_read);
355
356ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
357{
358 struct kiocb kiocb;
359 ssize_t ret;
360
361 if (!file->f_op->write_iter)
362 return -EINVAL;
363
364 init_sync_kiocb(&kiocb, file);
365 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100366
367 iter->type |= WRITE;
368 ret = file->f_op->write_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100369 BUG_ON(ret == -EIOCBQUEUED);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100370 if (ret > 0)
371 *ppos = kiocb.ki_pos;
372 return ret;
373}
374EXPORT_SYMBOL(vfs_iter_write);
375
Linus Torvaldse28cc712006-01-04 16:20:40 -0800376/*
377 * rw_verify_area doesn't like huge counts. We limit
378 * them to something that fits in "int" so that others
379 * won't have to do range checks all the time.
380 */
Al Viro68d70d02013-06-19 15:26:04 +0400381int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct inode *inode;
384 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100385 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Al Viro496ad9a2013-01-23 17:07:38 -0500387 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800388 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100389 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500391 if (unlikely(pos < 0)) {
392 if (!unsigned_offsets(file))
393 return retval;
394 if (count >= -pos) /* both values are in 0..LLONG_MAX */
395 return -EOVERFLOW;
396 } else if (unlikely((loff_t) (pos + count) < 0)) {
397 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700398 return retval;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500401 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
James Morrisc43e2592008-01-12 22:05:48 +1100402 retval = locks_mandatory_area(
Linus Torvaldse28cc712006-01-04 16:20:40 -0800403 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
404 inode, file, pos, count);
405 if (retval < 0)
406 return retval;
407 }
James Morrisc43e2592008-01-12 22:05:48 +1100408 retval = security_file_permission(file,
409 read_write == READ ? MAY_READ : MAY_WRITE);
410 if (retval)
411 return retval;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800412 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
415ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
416{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700417 struct iovec iov = { .iov_base = buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct kiocb kiocb;
419 ssize_t ret;
420
421 init_sync_kiocb(&kiocb, filp);
422 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700423
Zach Brown41003a72013-05-07 16:18:25 -0700424 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100425 BUG_ON(ret == -EIOCBQUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *ppos = kiocb.ki_pos;
427 return ret;
428}
429
430EXPORT_SYMBOL(do_sync_read);
431
Al Viro293bc982014-02-11 18:37:41 -0500432ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
433{
434 struct iovec iov = { .iov_base = buf, .iov_len = len };
435 struct kiocb kiocb;
436 struct iov_iter iter;
437 ssize_t ret;
438
439 init_sync_kiocb(&kiocb, filp);
440 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500441 iov_iter_init(&iter, READ, &iov, 1, len);
442
443 ret = filp->f_op->read_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100444 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500445 *ppos = kiocb.ki_pos;
446 return ret;
447}
448
449EXPORT_SYMBOL(new_sync_read);
450
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200451ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
452 loff_t *pos)
453{
454 ssize_t ret;
455
456 if (file->f_op->read)
457 ret = file->f_op->read(file, buf, count, pos);
458 else if (file->f_op->aio_read)
459 ret = do_sync_read(file, buf, count, pos);
460 else if (file->f_op->read_iter)
461 ret = new_sync_read(file, buf, count, pos);
462 else
463 ret = -EINVAL;
464
465 return ret;
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
469{
470 ssize_t ret;
471
472 if (!(file->f_mode & FMODE_READ))
473 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500474 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return -EINVAL;
476 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
477 return -EFAULT;
478
479 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800480 if (ret >= 0) {
481 count = ret;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200482 ret = __vfs_read(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100483 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500484 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100485 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
James Morrisc43e2592008-01-12 22:05:48 +1100487 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
490 return ret;
491}
492
493EXPORT_SYMBOL(vfs_read);
494
495ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
496{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700497 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct kiocb kiocb;
499 ssize_t ret;
500
501 init_sync_kiocb(&kiocb, filp);
502 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700503
Zach Brown41003a72013-05-07 16:18:25 -0700504 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100505 BUG_ON(ret == -EIOCBQUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 *ppos = kiocb.ki_pos;
507 return ret;
508}
509
510EXPORT_SYMBOL(do_sync_write);
511
Al Viro293bc982014-02-11 18:37:41 -0500512ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
513{
514 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
515 struct kiocb kiocb;
516 struct iov_iter iter;
517 ssize_t ret;
518
519 init_sync_kiocb(&kiocb, filp);
520 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500521 iov_iter_init(&iter, WRITE, &iov, 1, len);
522
523 ret = filp->f_op->write_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100524 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500525 *ppos = kiocb.ki_pos;
526 return ret;
527}
528
529EXPORT_SYMBOL(new_sync_write);
530
Al Viro06ae43f2013-03-20 13:19:30 -0400531ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
532{
533 mm_segment_t old_fs;
534 const char __user *p;
535 ssize_t ret;
536
Al Viro7f7f25e2014-02-11 17:49:24 -0500537 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro3e84f482013-03-27 15:20:30 +0000538 return -EINVAL;
539
Al Viro06ae43f2013-03-20 13:19:30 -0400540 old_fs = get_fs();
541 set_fs(get_ds());
542 p = (__force const char __user *)buf;
543 if (count > MAX_RW_COUNT)
544 count = MAX_RW_COUNT;
545 if (file->f_op->write)
546 ret = file->f_op->write(file, p, count, pos);
Al Viro293bc982014-02-11 18:37:41 -0500547 else if (file->f_op->aio_write)
Al Viro06ae43f2013-03-20 13:19:30 -0400548 ret = do_sync_write(file, p, count, pos);
Al Viro293bc982014-02-11 18:37:41 -0500549 else
550 ret = new_sync_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);
James Morrisc43e2592008-01-12 22:05:48 +1100577 if (file->f_op->write)
578 ret = file->f_op->write(file, buf, count, pos);
Al Viro293bc982014-02-11 18:37:41 -0500579 else if (file->f_op->aio_write)
James Morrisc43e2592008-01-12 22:05:48 +1100580 ret = do_sync_write(file, buf, count, pos);
Al Viro293bc982014-02-11 18:37:41 -0500581 else
582 ret = new_sync_write(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100583 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500584 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100585 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
James Morrisc43e2592008-01-12 22:05:48 +1100587 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400588 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
591 return ret;
592}
593
594EXPORT_SYMBOL(vfs_write);
595
596static inline loff_t file_pos_read(struct file *file)
597{
598 return file->f_pos;
599}
600
601static inline void file_pos_write(struct file *file, loff_t pos)
602{
603 file->f_pos = pos;
604}
605
Heiko Carstens3cdad422009-01-14 14:14:22 +0100606SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800608 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Al Viro2903ff02012-08-28 12:52:22 -0400611 if (f.file) {
612 loff_t pos = file_pos_read(f.file);
613 ret = vfs_read(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400614 if (ret >= 0)
615 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800616 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return ret;
619}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Heiko Carstens3cdad422009-01-14 14:14:22 +0100621SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
622 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800624 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Al Viro2903ff02012-08-28 12:52:22 -0400627 if (f.file) {
628 loff_t pos = file_pos_read(f.file);
629 ret = vfs_write(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400630 if (ret >= 0)
631 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800632 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634
635 return ret;
636}
637
Al Viro4a0fd5b2013-01-21 15:16:58 -0500638SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
639 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Al Viro2903ff02012-08-28 12:52:22 -0400641 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if (pos < 0)
645 return -EINVAL;
646
Al Viro2903ff02012-08-28 12:52:22 -0400647 f = fdget(fd);
648 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400650 if (f.file->f_mode & FMODE_PREAD)
651 ret = vfs_read(f.file, buf, count, &pos);
652 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
655 return ret;
656}
657
Al Viro4a0fd5b2013-01-21 15:16:58 -0500658SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
659 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Al Viro2903ff02012-08-28 12:52:22 -0400661 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 if (pos < 0)
665 return -EINVAL;
666
Al Viro2903ff02012-08-28 12:52:22 -0400667 f = fdget(fd);
668 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400670 if (f.file->f_mode & FMODE_PWRITE)
671 ret = vfs_write(f.file, buf, count, &pos);
672 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
675 return ret;
676}
677
678/*
679 * Reduce an iovec's length in-place. Return the resulting number of segments
680 */
681unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
682{
683 unsigned long seg = 0;
684 size_t len = 0;
685
686 while (seg < nr_segs) {
687 seg++;
688 if (len + iov->iov_len >= to) {
689 iov->iov_len = to - len;
690 break;
691 }
692 len += iov->iov_len;
693 iov++;
694 }
695 return seg;
696}
Eric Sandeen19295522008-01-28 23:58:27 -0500697EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Al Viro293bc982014-02-11 18:37:41 -0500699static ssize_t do_iter_readv_writev(struct file *filp, int rw, const struct iovec *iov,
700 unsigned long nr_segs, size_t len, loff_t *ppos, iter_fn_t fn)
701{
702 struct kiocb kiocb;
703 struct iov_iter iter;
704 ssize_t ret;
705
706 init_sync_kiocb(&kiocb, filp);
707 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500708
709 iov_iter_init(&iter, rw, iov, nr_segs, len);
710 ret = fn(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100711 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500712 *ppos = kiocb.ki_pos;
713 return ret;
714}
715
Al Viro72ec3512013-03-20 10:42:10 -0400716static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700717 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
718{
719 struct kiocb kiocb;
720 ssize_t ret;
721
722 init_sync_kiocb(&kiocb, filp);
723 kiocb.ki_pos = *ppos;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700724
Zach Brown41003a72013-05-07 16:18:25 -0700725 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100726 BUG_ON(ret == -EIOCBQUEUED);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700727 *ppos = kiocb.ki_pos;
728 return ret;
729}
730
731/* Do it by hand, with file-ops */
Al Viro72ec3512013-03-20 10:42:10 -0400732static ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700733 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
734{
735 struct iovec *vector = iov;
736 ssize_t ret = 0;
737
738 while (nr_segs > 0) {
739 void __user *base;
740 size_t len;
741 ssize_t nr;
742
743 base = vector->iov_base;
744 len = vector->iov_len;
745 vector++;
746 nr_segs--;
747
748 nr = fn(filp, base, len, ppos);
749
750 if (nr < 0) {
751 if (!ret)
752 ret = nr;
753 break;
754 }
755 ret += nr;
756 if (nr != len)
757 break;
758 }
759
760 return ret;
761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/* A write operation does a read from user space and vice versa */
764#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
765
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700766ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
767 unsigned long nr_segs, unsigned long fast_segs,
768 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700769 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700770{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700771 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700772 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700773 struct iovec *iov = fast_pointer;
774
Linus Torvalds435f49a2010-10-29 10:36:49 -0700775 /*
776 * SuS says "The readv() function *may* fail if the iovcnt argument
777 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
778 * traditionally returned zero for zero segments, so...
779 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700780 if (nr_segs == 0) {
781 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700782 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700783 }
784
Linus Torvalds435f49a2010-10-29 10:36:49 -0700785 /*
786 * First get the "struct iovec" from user memory and
787 * verify all the pointers
788 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700789 if (nr_segs > UIO_MAXIOV) {
790 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700791 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700792 }
793 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700794 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700795 if (iov == NULL) {
796 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700797 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700798 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700799 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700800 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
801 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700802 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700803 }
804
Linus Torvalds435f49a2010-10-29 10:36:49 -0700805 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700806 * According to the Single Unix Specification we should return EINVAL
807 * if an element length is < 0 when cast to ssize_t or if the
808 * total length would overflow the ssize_t return value of the
809 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700810 *
811 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
812 * overflow case.
813 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700814 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700815 for (seg = 0; seg < nr_segs; seg++) {
816 void __user *buf = iov[seg].iov_base;
817 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700818
819 /* see if we we're about to use an invalid len or if
820 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700821 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700822 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700823 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700824 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700825 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700826 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700827 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700828 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700829 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700830 if (len > MAX_RW_COUNT - ret) {
831 len = MAX_RW_COUNT - ret;
832 iov[seg].iov_len = len;
833 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700834 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700835 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700836out:
837 *ret_pointer = iov;
838 return ret;
839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841static ssize_t do_readv_writev(int type, struct file *file,
842 const struct iovec __user * uvector,
843 unsigned long nr_segs, loff_t *pos)
844{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 size_t tot_len;
846 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700847 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 io_fn_t fn;
850 iov_fn_t fnv;
Al Viro293bc982014-02-11 18:37:41 -0500851 iter_fn_t iter_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700853 ret = rw_copy_check_uvector(type, uvector, nr_segs,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700854 ARRAY_SIZE(iovstack), iovstack, &iov);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700855 if (ret <= 0)
856 goto out;
857
858 tot_len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800860 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 goto out;
862
863 fnv = NULL;
864 if (type == READ) {
865 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700866 fnv = file->f_op->aio_read;
Al Viro293bc982014-02-11 18:37:41 -0500867 iter_fn = file->f_op->read_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 } else {
869 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700870 fnv = file->f_op->aio_write;
Al Viro293bc982014-02-11 18:37:41 -0500871 iter_fn = file->f_op->write_iter;
Al Viro03d95eb2013-03-20 13:04:20 -0400872 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
Al Viro293bc982014-02-11 18:37:41 -0500875 if (iter_fn)
876 ret = do_iter_readv_writev(file, type, iov, nr_segs, tot_len,
877 pos, iter_fn);
878 else if (fnv)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700879 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
880 pos, fnv);
881 else
882 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Al Viro03d95eb2013-03-20 13:04:20 -0400884 if (type != READ)
885 file_end_write(file);
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887out:
888 if (iov != iovstack)
889 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400890 if ((ret + (type == READ)) > 0) {
891 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500892 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400893 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500894 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
900 unsigned long vlen, loff_t *pos)
901{
902 if (!(file->f_mode & FMODE_READ))
903 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500904 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EINVAL;
906
907 return do_readv_writev(READ, file, vec, vlen, pos);
908}
909
910EXPORT_SYMBOL(vfs_readv);
911
912ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
913 unsigned long vlen, loff_t *pos)
914{
915 if (!(file->f_mode & FMODE_WRITE))
916 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500917 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return -EINVAL;
919
920 return do_readv_writev(WRITE, file, vec, vlen, pos);
921}
922
923EXPORT_SYMBOL(vfs_writev);
924
Heiko Carstens3cdad422009-01-14 14:14:22 +0100925SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
926 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800928 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Al Viro2903ff02012-08-28 12:52:22 -0400931 if (f.file) {
932 loff_t pos = file_pos_read(f.file);
933 ret = vfs_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400934 if (ret >= 0)
935 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800936 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800940 add_rchar(current, ret);
941 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return ret;
943}
944
Heiko Carstens3cdad422009-01-14 14:14:22 +0100945SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
946 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800948 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Al Viro2903ff02012-08-28 12:52:22 -0400951 if (f.file) {
952 loff_t pos = file_pos_read(f.file);
953 ret = vfs_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400954 if (ret >= 0)
955 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800956 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
959 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800960 add_wchar(current, ret);
961 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return ret;
963}
964
Linus Torvalds601cc112009-04-03 08:03:22 -0700965static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700966{
Linus Torvalds601cc112009-04-03 08:03:22 -0700967#define HALF_LONG_BITS (BITS_PER_LONG / 2)
968 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
969}
970
971SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
972 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
973{
974 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400975 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700976 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700977
978 if (pos < 0)
979 return -EINVAL;
980
Al Viro2903ff02012-08-28 12:52:22 -0400981 f = fdget(fd);
982 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700983 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400984 if (f.file->f_mode & FMODE_PREAD)
985 ret = vfs_readv(f.file, vec, vlen, &pos);
986 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700987 }
988
989 if (ret > 0)
990 add_rchar(current, ret);
991 inc_syscr(current);
992 return ret;
993}
994
995SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700996 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700997{
Linus Torvalds601cc112009-04-03 08:03:22 -0700998 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400999 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001000 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001001
1002 if (pos < 0)
1003 return -EINVAL;
1004
Al Viro2903ff02012-08-28 12:52:22 -04001005 f = fdget(fd);
1006 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001007 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -04001008 if (f.file->f_mode & FMODE_PWRITE)
1009 ret = vfs_writev(f.file, vec, vlen, &pos);
1010 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001011 }
1012
1013 if (ret > 0)
1014 add_wchar(current, ret);
1015 inc_syscw(current);
1016 return ret;
1017}
1018
Al Viro72ec3512013-03-20 10:42:10 -04001019#ifdef CONFIG_COMPAT
1020
1021static ssize_t compat_do_readv_writev(int type, struct file *file,
1022 const struct compat_iovec __user *uvector,
1023 unsigned long nr_segs, loff_t *pos)
1024{
1025 compat_ssize_t tot_len;
1026 struct iovec iovstack[UIO_FASTIOV];
1027 struct iovec *iov = iovstack;
1028 ssize_t ret;
1029 io_fn_t fn;
1030 iov_fn_t fnv;
Al Viro293bc982014-02-11 18:37:41 -05001031 iter_fn_t iter_fn;
Al Viro72ec3512013-03-20 10:42:10 -04001032
Al Viro72ec3512013-03-20 10:42:10 -04001033 ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
1034 UIO_FASTIOV, iovstack, &iov);
1035 if (ret <= 0)
1036 goto out;
1037
1038 tot_len = ret;
1039 ret = rw_verify_area(type, file, pos, tot_len);
1040 if (ret < 0)
1041 goto out;
1042
1043 fnv = NULL;
1044 if (type == READ) {
1045 fn = file->f_op->read;
1046 fnv = file->f_op->aio_read;
Al Viro293bc982014-02-11 18:37:41 -05001047 iter_fn = file->f_op->read_iter;
Al Viro72ec3512013-03-20 10:42:10 -04001048 } else {
1049 fn = (io_fn_t)file->f_op->write;
1050 fnv = file->f_op->aio_write;
Al Viro293bc982014-02-11 18:37:41 -05001051 iter_fn = file->f_op->write_iter;
Al Viro03d95eb2013-03-20 13:04:20 -04001052 file_start_write(file);
Al Viro72ec3512013-03-20 10:42:10 -04001053 }
1054
Al Viro293bc982014-02-11 18:37:41 -05001055 if (iter_fn)
1056 ret = do_iter_readv_writev(file, type, iov, nr_segs, tot_len,
1057 pos, iter_fn);
1058 else if (fnv)
Al Viro72ec3512013-03-20 10:42:10 -04001059 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
1060 pos, fnv);
Al Viro03d95eb2013-03-20 13:04:20 -04001061 else
Al Viro72ec3512013-03-20 10:42:10 -04001062 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
1063
Al Viro03d95eb2013-03-20 13:04:20 -04001064 if (type != READ)
1065 file_end_write(file);
1066
Al Viro72ec3512013-03-20 10:42:10 -04001067out:
1068 if (iov != iovstack)
1069 kfree(iov);
1070 if ((ret + (type == READ)) > 0) {
1071 if (type == READ)
1072 fsnotify_access(file);
1073 else
1074 fsnotify_modify(file);
1075 }
1076 return ret;
1077}
1078
1079static size_t compat_readv(struct file *file,
1080 const struct compat_iovec __user *vec,
1081 unsigned long vlen, loff_t *pos)
1082{
1083 ssize_t ret = -EBADF;
1084
1085 if (!(file->f_mode & FMODE_READ))
1086 goto out;
1087
1088 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001089 if (!(file->f_mode & FMODE_CAN_READ))
Al Viro72ec3512013-03-20 10:42:10 -04001090 goto out;
1091
1092 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
1093
1094out:
1095 if (ret > 0)
1096 add_rchar(current, ret);
1097 inc_syscr(current);
1098 return ret;
1099}
1100
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001101COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001102 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001103 compat_ulong_t, vlen)
Al Viro72ec3512013-03-20 10:42:10 -04001104{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001105 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001106 ssize_t ret;
1107 loff_t pos;
1108
1109 if (!f.file)
1110 return -EBADF;
1111 pos = f.file->f_pos;
1112 ret = compat_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001113 if (ret >= 0)
1114 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001115 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001116 return ret;
1117}
1118
Heiko Carstens378a10f2014-03-05 10:43:51 +01001119static long __compat_sys_preadv64(unsigned long fd,
1120 const struct compat_iovec __user *vec,
1121 unsigned long vlen, loff_t pos)
Al Viro72ec3512013-03-20 10:42:10 -04001122{
1123 struct fd f;
1124 ssize_t ret;
1125
1126 if (pos < 0)
1127 return -EINVAL;
1128 f = fdget(fd);
1129 if (!f.file)
1130 return -EBADF;
1131 ret = -ESPIPE;
1132 if (f.file->f_mode & FMODE_PREAD)
1133 ret = compat_readv(f.file, vec, vlen, &pos);
1134 fdput(f);
1135 return ret;
1136}
1137
Heiko Carstens378a10f2014-03-05 10:43:51 +01001138#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1139COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1140 const struct compat_iovec __user *,vec,
1141 unsigned long, vlen, loff_t, pos)
1142{
1143 return __compat_sys_preadv64(fd, vec, vlen, pos);
1144}
1145#endif
1146
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001147COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001148 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001149 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001150{
1151 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001152
1153 return __compat_sys_preadv64(fd, vec, vlen, pos);
Al Viro72ec3512013-03-20 10:42:10 -04001154}
1155
1156static size_t compat_writev(struct file *file,
1157 const struct compat_iovec __user *vec,
1158 unsigned long vlen, loff_t *pos)
1159{
1160 ssize_t ret = -EBADF;
1161
1162 if (!(file->f_mode & FMODE_WRITE))
1163 goto out;
1164
1165 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001166 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro72ec3512013-03-20 10:42:10 -04001167 goto out;
1168
1169 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1170
1171out:
1172 if (ret > 0)
1173 add_wchar(current, ret);
1174 inc_syscw(current);
1175 return ret;
1176}
1177
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001178COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001179 const struct compat_iovec __user *, vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001180 compat_ulong_t, vlen)
Al Viro72ec3512013-03-20 10:42:10 -04001181{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001182 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001183 ssize_t ret;
1184 loff_t pos;
1185
1186 if (!f.file)
1187 return -EBADF;
1188 pos = f.file->f_pos;
1189 ret = compat_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001190 if (ret >= 0)
1191 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001192 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001193 return ret;
1194}
1195
Heiko Carstens378a10f2014-03-05 10:43:51 +01001196static long __compat_sys_pwritev64(unsigned long fd,
1197 const struct compat_iovec __user *vec,
1198 unsigned long vlen, loff_t pos)
Al Viro72ec3512013-03-20 10:42:10 -04001199{
1200 struct fd f;
1201 ssize_t ret;
1202
1203 if (pos < 0)
1204 return -EINVAL;
1205 f = fdget(fd);
1206 if (!f.file)
1207 return -EBADF;
1208 ret = -ESPIPE;
1209 if (f.file->f_mode & FMODE_PWRITE)
1210 ret = compat_writev(f.file, vec, vlen, &pos);
1211 fdput(f);
1212 return ret;
1213}
1214
Heiko Carstens378a10f2014-03-05 10:43:51 +01001215#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1216COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1217 const struct compat_iovec __user *,vec,
1218 unsigned long, vlen, loff_t, pos)
1219{
1220 return __compat_sys_pwritev64(fd, vec, vlen, pos);
1221}
1222#endif
1223
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001224COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001225 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001226 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001227{
1228 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001229
1230 return __compat_sys_pwritev64(fd, vec, vlen, pos);
Al Viro72ec3512013-03-20 10:42:10 -04001231}
1232#endif
1233
Al Viro19f4fc32013-02-24 02:17:03 -05001234static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1235 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Al Viro2903ff02012-08-28 12:52:22 -04001237 struct fd in, out;
1238 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001240 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001242 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 /*
1245 * Get input file, and verify that it is ok..
1246 */
1247 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001248 in = fdget(in_fd);
1249 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001251 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001254 if (!ppos) {
1255 pos = in.file->f_pos;
1256 } else {
1257 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001258 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001260 }
1261 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001262 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001264 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /*
1267 * Get output file, and verify that it is ok..
1268 */
1269 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001270 out = fdget(out_fd);
1271 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001273 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 goto fput_out;
1275 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001276 in_inode = file_inode(in.file);
1277 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001278 out_pos = out.file->f_pos;
1279 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001280 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001282 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (!max)
1285 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (unlikely(pos + count > max)) {
1288 retval = -EOVERFLOW;
1289 if (pos >= max)
1290 goto fput_out;
1291 count = max - pos;
1292 }
1293
Jens Axboed96e6e72007-06-11 12:18:52 +02001294 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001295#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001296 /*
1297 * We need to debate whether we can enable this or not. The
1298 * man page documents EAGAIN return for the output at least,
1299 * and the application is arguably buggy if it doesn't expect
1300 * EAGAIN on a non-blocking file descriptor.
1301 */
Al Viro2903ff02012-08-28 12:52:22 -04001302 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001303 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001304#endif
Al Viro50cd2c52013-05-23 20:10:34 -04001305 file_start_write(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001306 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
Al Viro50cd2c52013-05-23 20:10:34 -04001307 file_end_write(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001310 add_rchar(current, retval);
1311 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001312 fsnotify_access(in.file);
1313 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001314 out.file->f_pos = out_pos;
1315 if (ppos)
1316 *ppos = pos;
1317 else
1318 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001321 inc_syscr(current);
1322 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001323 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 retval = -EOVERFLOW;
1325
1326fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001327 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001329 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330out:
1331 return retval;
1332}
1333
Heiko Carstens002c8972009-01-14 14:14:18 +01001334SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 loff_t pos;
1337 off_t off;
1338 ssize_t ret;
1339
1340 if (offset) {
1341 if (unlikely(get_user(off, offset)))
1342 return -EFAULT;
1343 pos = off;
1344 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1345 if (unlikely(put_user(pos, offset)))
1346 return -EFAULT;
1347 return ret;
1348 }
1349
1350 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1351}
1352
Heiko Carstens002c8972009-01-14 14:14:18 +01001353SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 loff_t pos;
1356 ssize_t ret;
1357
1358 if (offset) {
1359 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1360 return -EFAULT;
1361 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1362 if (unlikely(put_user(pos, offset)))
1363 return -EFAULT;
1364 return ret;
1365 }
1366
1367 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1368}
Al Viro19f4fc32013-02-24 02:17:03 -05001369
1370#ifdef CONFIG_COMPAT
1371COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1372 compat_off_t __user *, offset, compat_size_t, count)
1373{
1374 loff_t pos;
1375 off_t off;
1376 ssize_t ret;
1377
1378 if (offset) {
1379 if (unlikely(get_user(off, offset)))
1380 return -EFAULT;
1381 pos = off;
1382 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1383 if (unlikely(put_user(pos, offset)))
1384 return -EFAULT;
1385 return ret;
1386 }
1387
1388 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1389}
1390
1391COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1392 compat_loff_t __user *, offset, compat_size_t, count)
1393{
1394 loff_t pos;
1395 ssize_t ret;
1396
1397 if (offset) {
1398 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1399 return -EFAULT;
1400 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1401 if (unlikely(put_user(pos, offset)))
1402 return -EFAULT;
1403 return ret;
1404 }
1405
1406 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1407}
1408#endif