blob: a698eff457fb6e510c0543c9b762cc8c8d702f23 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/read_write.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include <linux/slab.h>
8#include <linux/stat.h>
9#include <linux/fcntl.h>
10#include <linux/file.h>
11#include <linux/uio.h>
Robert Love0eeca282005-07-12 17:06:03 -040012#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/security.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/syscalls.h>
Linus Torvaldse28cc712006-01-04 16:20:40 -080016#include <linux/pagemap.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020017#include <linux/splice.h>
Al Viro561c6732013-02-24 10:52:26 -050018#include <linux/compat.h>
Badari Pulavartyee0b3e62006-09-30 23:28:47 -070019#include "read_write.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/uaccess.h>
22#include <asm/unistd.h>
23
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080024const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -070026 .read = do_sync_read,
27 .aio_read = generic_file_aio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020029 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
31
32EXPORT_SYMBOL(generic_ro_fops);
33
Al Virocccb5a12010-12-17 07:44:05 -050034static inline int unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070035{
Al Virocccb5a12010-12-17 07:44:05 -050036 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070037}
38
Andi Kleenef3d0fd2011-09-15 16:06:48 -070039static loff_t lseek_execute(struct file *file, struct inode *inode,
40 loff_t offset, loff_t maxsize)
41{
42 if (offset < 0 && !unsigned_offsets(file))
43 return -EINVAL;
44 if (offset > maxsize)
45 return -EINVAL;
46
47 if (offset != file->f_pos) {
48 file->f_pos = offset;
49 file->f_version = 0;
50 }
51 return offset;
52}
53
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020054/**
Andi Kleen57604952011-09-15 16:06:50 -070055 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020056 * @file: file structure to seek on
57 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -080058 * @whence: type of seek
Eric Sandeene8b96eb2012-04-30 13:11:29 -050059 * @size: max size of this file in file system
60 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020061 *
Andi Kleen57604952011-09-15 16:06:50 -070062 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050063 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070064 *
65 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070066 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070067 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
68 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020069 */
Andi Kleen9465efc2008-06-27 11:05:24 +020070loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -080071generic_file_llseek_size(struct file *file, loff_t offset, int whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050072 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct inode *inode = file->f_mapping->host;
75
Andrew Morton965c8e52012-12-17 15:59:39 -080076 switch (whence) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020077 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050078 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020079 break;
80 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080081 /*
82 * Here we special-case the lseek(fd, 0, SEEK_CUR)
83 * position-querying operation. Avoid rewriting the "same"
84 * f_pos value back to the file because a concurrent read(),
85 * write() or lseek() might have altered it
86 */
87 if (offset == 0)
88 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -070089 /*
90 * f_lock protects against read/modify/write race with other
91 * SEEK_CURs. Note that parallel writes and reads behave
92 * like SEEK_SET.
93 */
94 spin_lock(&file->f_lock);
95 offset = lseek_execute(file, inode, file->f_pos + offset,
Andi Kleen57604952011-09-15 16:06:50 -070096 maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -070097 spin_unlock(&file->f_lock);
98 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -040099 case SEEK_DATA:
100 /*
101 * In the generic case the entire file is data, so as long as
102 * offset isn't at the end of the file then the offset is data.
103 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500104 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400105 return -ENXIO;
106 break;
107 case SEEK_HOLE:
108 /*
109 * There is a virtual hole at the end of the file, so as long as
110 * offset isn't i_size or larger, return i_size.
111 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500112 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400113 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500114 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200117
Andi Kleen57604952011-09-15 16:06:50 -0700118 return lseek_execute(file, inode, offset, maxsize);
119}
120EXPORT_SYMBOL(generic_file_llseek_size);
121
122/**
123 * generic_file_llseek - generic llseek implementation for regular files
124 * @file: file structure to seek on
125 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800126 * @whence: type of seek
Andi Kleen57604952011-09-15 16:06:50 -0700127 *
128 * This is a generic implemenation of ->llseek useable for all normal local
129 * filesystems. It just updates the file offset to the value specified by
Andrew Morton965c8e52012-12-17 15:59:39 -0800130 * @offset and @whence under i_mutex.
Andi Kleen57604952011-09-15 16:06:50 -0700131 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800132loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
Andi Kleen57604952011-09-15 16:06:50 -0700133{
134 struct inode *inode = file->f_mapping->host;
135
Andrew Morton965c8e52012-12-17 15:59:39 -0800136 return generic_file_llseek_size(file, offset, whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500137 inode->i_sb->s_maxbytes,
138 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
Andi Kleen9465efc2008-06-27 11:05:24 +0200140EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
jan Blunckae6afc32010-05-26 14:44:48 -0700142/**
143 * noop_llseek - No Operation Performed llseek implementation
144 * @file: file structure to seek on
145 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800146 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700147 *
148 * This is an implementation of ->llseek useable for the rare special case when
149 * userspace expects the seek to succeed but the (device) file is actually not
150 * able to perform the seek. In this case you use noop_llseek() instead of
151 * falling back to the default implementation of ->llseek.
152 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800153loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700154{
155 return file->f_pos;
156}
157EXPORT_SYMBOL(noop_llseek);
158
Andrew Morton965c8e52012-12-17 15:59:39 -0800159loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 return -ESPIPE;
162}
163EXPORT_SYMBOL(no_llseek);
164
Andrew Morton965c8e52012-12-17 15:59:39 -0800165loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Al Viro496ad9a2013-01-23 17:07:38 -0500167 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200168 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Josef Bacik982d8162011-07-18 13:21:35 -0400170 mutex_lock(&inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -0800171 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700172 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400173 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700175 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800176 if (offset == 0) {
177 retval = file->f_pos;
178 goto out;
179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400181 break;
182 case SEEK_DATA:
183 /*
184 * In the generic case the entire file is data, so as
185 * long as offset isn't at the end of the file then the
186 * offset is data.
187 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300188 if (offset >= inode->i_size) {
189 retval = -ENXIO;
190 goto out;
191 }
Josef Bacik982d8162011-07-18 13:21:35 -0400192 break;
193 case SEEK_HOLE:
194 /*
195 * There is a virtual hole at the end of the file, so
196 * as long as offset isn't i_size or larger, return
197 * i_size.
198 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300199 if (offset >= inode->i_size) {
200 retval = -ENXIO;
201 goto out;
202 }
Josef Bacik982d8162011-07-18 13:21:35 -0400203 offset = inode->i_size;
204 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500207 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (offset != file->f_pos) {
209 file->f_pos = offset;
210 file->f_version = 0;
211 }
212 retval = offset;
213 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800214out:
Josef Bacik982d8162011-07-18 13:21:35 -0400215 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return retval;
217}
218EXPORT_SYMBOL(default_llseek);
219
Andrew Morton965c8e52012-12-17 15:59:39 -0800220loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 loff_t (*fn)(struct file *, loff_t, int);
223
224 fn = no_llseek;
225 if (file->f_mode & FMODE_LSEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (file->f_op && file->f_op->llseek)
227 fn = file->f_op->llseek;
228 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800229 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231EXPORT_SYMBOL(vfs_llseek);
232
Andrew Morton965c8e52012-12-17 15:59:39 -0800233SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 off_t retval;
Al Viro2903ff02012-08-28 12:52:22 -0400236 struct fd f = fdget(fd);
237 if (!f.file)
238 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800241 if (whence <= SEEK_MAX) {
242 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 retval = res;
244 if (res != (loff_t)retval)
245 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
246 }
Al Viro2903ff02012-08-28 12:52:22 -0400247 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return retval;
249}
250
Al Viro561c6732013-02-24 10:52:26 -0500251#ifdef CONFIG_COMPAT
252COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
253{
254 return sys_lseek(fd, offset, whence);
255}
256#endif
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100259SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
260 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800261 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 int retval;
Al Viro2903ff02012-08-28 12:52:22 -0400264 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Al Viro2903ff02012-08-28 12:52:22 -0400267 if (!f.file)
268 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800271 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 goto out_putf;
273
Al Viro2903ff02012-08-28 12:52:22 -0400274 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800275 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 retval = (int)offset;
278 if (offset >= 0) {
279 retval = -EFAULT;
280 if (!copy_to_user(result, &offset, sizeof(offset)))
281 retval = 0;
282 }
283out_putf:
Al Viro2903ff02012-08-28 12:52:22 -0400284 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return retval;
286}
287#endif
288
Linus Torvaldse28cc712006-01-04 16:20:40 -0800289/*
290 * rw_verify_area doesn't like huge counts. We limit
291 * them to something that fits in "int" so that others
292 * won't have to do range checks all the time.
293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
295{
296 struct inode *inode;
297 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100298 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Al Viro496ad9a2013-01-23 17:07:38 -0500300 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800301 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100302 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500304 if (unlikely(pos < 0)) {
305 if (!unsigned_offsets(file))
306 return retval;
307 if (count >= -pos) /* both values are in 0..LLONG_MAX */
308 return -EOVERFLOW;
309 } else if (unlikely((loff_t) (pos + count) < 0)) {
310 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700311 return retval;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Pavel Emelyanova16877c2007-10-01 14:41:11 -0700314 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
James Morrisc43e2592008-01-12 22:05:48 +1100315 retval = locks_mandatory_area(
Linus Torvaldse28cc712006-01-04 16:20:40 -0800316 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
317 inode, file, pos, count);
318 if (retval < 0)
319 return retval;
320 }
James Morrisc43e2592008-01-12 22:05:48 +1100321 retval = security_file_permission(file,
322 read_write == READ ? MAY_READ : MAY_WRITE);
323 if (retval)
324 return retval;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800325 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700328static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
329{
330 set_current_state(TASK_UNINTERRUPTIBLE);
331 if (!kiocbIsKicked(iocb))
332 schedule();
333 else
334 kiocbClearKicked(iocb);
335 __set_current_state(TASK_RUNNING);
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
339{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700340 struct iovec iov = { .iov_base = buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct kiocb kiocb;
342 ssize_t ret;
343
344 init_sync_kiocb(&kiocb, filp);
345 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700346 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000347 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700348
349 for (;;) {
350 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
351 if (ret != -EIOCBRETRY)
352 break;
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700353 wait_on_retry_sync_kiocb(&kiocb);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700354 }
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (-EIOCBQUEUED == ret)
357 ret = wait_on_sync_kiocb(&kiocb);
358 *ppos = kiocb.ki_pos;
359 return ret;
360}
361
362EXPORT_SYMBOL(do_sync_read);
363
364ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
365{
366 ssize_t ret;
367
368 if (!(file->f_mode & FMODE_READ))
369 return -EBADF;
370 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
371 return -EINVAL;
372 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
373 return -EFAULT;
374
375 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800376 if (ret >= 0) {
377 count = ret;
James Morrisc43e2592008-01-12 22:05:48 +1100378 if (file->f_op->read)
379 ret = file->f_op->read(file, buf, count, pos);
380 else
381 ret = do_sync_read(file, buf, count, pos);
382 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500383 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100384 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
James Morrisc43e2592008-01-12 22:05:48 +1100386 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 return ret;
390}
391
392EXPORT_SYMBOL(vfs_read);
393
394ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
395{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700396 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct kiocb kiocb;
398 ssize_t ret;
399
400 init_sync_kiocb(&kiocb, filp);
401 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700402 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000403 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700404
405 for (;;) {
406 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
407 if (ret != -EIOCBRETRY)
408 break;
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700409 wait_on_retry_sync_kiocb(&kiocb);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700410 }
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (-EIOCBQUEUED == ret)
413 ret = wait_on_sync_kiocb(&kiocb);
414 *ppos = kiocb.ki_pos;
415 return ret;
416}
417
418EXPORT_SYMBOL(do_sync_write);
419
420ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
421{
422 ssize_t ret;
423
424 if (!(file->f_mode & FMODE_WRITE))
425 return -EBADF;
426 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
427 return -EINVAL;
428 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
429 return -EFAULT;
430
431 ret = rw_verify_area(WRITE, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800432 if (ret >= 0) {
433 count = ret;
James Morrisc43e2592008-01-12 22:05:48 +1100434 if (file->f_op->write)
435 ret = file->f_op->write(file, buf, count, pos);
436 else
437 ret = do_sync_write(file, buf, count, pos);
438 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500439 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100440 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
James Morrisc43e2592008-01-12 22:05:48 +1100442 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
445 return ret;
446}
447
448EXPORT_SYMBOL(vfs_write);
449
450static inline loff_t file_pos_read(struct file *file)
451{
452 return file->f_pos;
453}
454
455static inline void file_pos_write(struct file *file, loff_t pos)
456{
457 file->f_pos = pos;
458}
459
Heiko Carstens3cdad422009-01-14 14:14:22 +0100460SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Al Viro2903ff02012-08-28 12:52:22 -0400462 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Al Viro2903ff02012-08-28 12:52:22 -0400465 if (f.file) {
466 loff_t pos = file_pos_read(f.file);
467 ret = vfs_read(f.file, buf, count, &pos);
468 file_pos_write(f.file, pos);
469 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return ret;
472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Heiko Carstens3cdad422009-01-14 14:14:22 +0100474SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
475 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Al Viro2903ff02012-08-28 12:52:22 -0400477 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Al Viro2903ff02012-08-28 12:52:22 -0400480 if (f.file) {
481 loff_t pos = file_pos_read(f.file);
482 ret = vfs_write(f.file, buf, count, &pos);
483 file_pos_write(f.file, pos);
484 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
487 return ret;
488}
489
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100490SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
491 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Al Viro2903ff02012-08-28 12:52:22 -0400493 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 if (pos < 0)
497 return -EINVAL;
498
Al Viro2903ff02012-08-28 12:52:22 -0400499 f = fdget(fd);
500 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400502 if (f.file->f_mode & FMODE_PREAD)
503 ret = vfs_read(f.file, buf, count, &pos);
504 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506
507 return ret;
508}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100509#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
510asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
511{
512 return SYSC_pread64((unsigned int) fd, (char __user *) buf,
513 (size_t) count, pos);
514}
515SYSCALL_ALIAS(sys_pread64, SyS_pread64);
516#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100518SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
519 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Al Viro2903ff02012-08-28 12:52:22 -0400521 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (pos < 0)
525 return -EINVAL;
526
Al Viro2903ff02012-08-28 12:52:22 -0400527 f = fdget(fd);
528 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400530 if (f.file->f_mode & FMODE_PWRITE)
531 ret = vfs_write(f.file, buf, count, &pos);
532 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
535 return ret;
536}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100537#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
538asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
539{
540 return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
541 (size_t) count, pos);
542}
543SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
544#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546/*
547 * Reduce an iovec's length in-place. Return the resulting number of segments
548 */
549unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
550{
551 unsigned long seg = 0;
552 size_t len = 0;
553
554 while (seg < nr_segs) {
555 seg++;
556 if (len + iov->iov_len >= to) {
557 iov->iov_len = to - len;
558 break;
559 }
560 len += iov->iov_len;
561 iov++;
562 }
563 return seg;
564}
Eric Sandeen19295522008-01-28 23:58:27 -0500565EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700567ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
568 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
569{
570 struct kiocb kiocb;
571 ssize_t ret;
572
573 init_sync_kiocb(&kiocb, filp);
574 kiocb.ki_pos = *ppos;
575 kiocb.ki_left = len;
576 kiocb.ki_nbytes = len;
577
578 for (;;) {
579 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
580 if (ret != -EIOCBRETRY)
581 break;
582 wait_on_retry_sync_kiocb(&kiocb);
583 }
584
585 if (ret == -EIOCBQUEUED)
586 ret = wait_on_sync_kiocb(&kiocb);
587 *ppos = kiocb.ki_pos;
588 return ret;
589}
590
591/* Do it by hand, with file-ops */
592ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
593 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
594{
595 struct iovec *vector = iov;
596 ssize_t ret = 0;
597
598 while (nr_segs > 0) {
599 void __user *base;
600 size_t len;
601 ssize_t nr;
602
603 base = vector->iov_base;
604 len = vector->iov_len;
605 vector++;
606 nr_segs--;
607
608 nr = fn(filp, base, len, ppos);
609
610 if (nr < 0) {
611 if (!ret)
612 ret = nr;
613 break;
614 }
615 ret += nr;
616 if (nr != len)
617 break;
618 }
619
620 return ret;
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623/* A write operation does a read from user space and vice versa */
624#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
625
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700626ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
627 unsigned long nr_segs, unsigned long fast_segs,
628 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700629 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700630{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700631 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700632 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700633 struct iovec *iov = fast_pointer;
634
Linus Torvalds435f49a2010-10-29 10:36:49 -0700635 /*
636 * SuS says "The readv() function *may* fail if the iovcnt argument
637 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
638 * traditionally returned zero for zero segments, so...
639 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700640 if (nr_segs == 0) {
641 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700642 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700643 }
644
Linus Torvalds435f49a2010-10-29 10:36:49 -0700645 /*
646 * First get the "struct iovec" from user memory and
647 * verify all the pointers
648 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700649 if (nr_segs > UIO_MAXIOV) {
650 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700651 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700652 }
653 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700654 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700655 if (iov == NULL) {
656 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700657 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700658 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700659 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700660 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
661 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700662 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700663 }
664
Linus Torvalds435f49a2010-10-29 10:36:49 -0700665 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700666 * According to the Single Unix Specification we should return EINVAL
667 * if an element length is < 0 when cast to ssize_t or if the
668 * total length would overflow the ssize_t return value of the
669 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700670 *
671 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
672 * overflow case.
673 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700674 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700675 for (seg = 0; seg < nr_segs; seg++) {
676 void __user *buf = iov[seg].iov_base;
677 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700678
679 /* see if we we're about to use an invalid len or if
680 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700681 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700682 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700683 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700684 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700685 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700686 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700687 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700688 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700689 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700690 if (len > MAX_RW_COUNT - ret) {
691 len = MAX_RW_COUNT - ret;
692 iov[seg].iov_len = len;
693 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700694 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700695 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700696out:
697 *ret_pointer = iov;
698 return ret;
699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static ssize_t do_readv_writev(int type, struct file *file,
702 const struct iovec __user * uvector,
703 unsigned long nr_segs, loff_t *pos)
704{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 size_t tot_len;
706 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700707 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 io_fn_t fn;
710 iov_fn_t fnv;
711
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700712 if (!file->f_op) {
713 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 goto out;
715 }
716
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700717 ret = rw_copy_check_uvector(type, uvector, nr_segs,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700718 ARRAY_SIZE(iovstack), iovstack, &iov);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700719 if (ret <= 0)
720 goto out;
721
722 tot_len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800724 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto out;
726
727 fnv = NULL;
728 if (type == READ) {
729 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700730 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 } else {
732 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700733 fnv = file->f_op->aio_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700736 if (fnv)
737 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
738 pos, fnv);
739 else
740 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742out:
743 if (iov != iovstack)
744 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400745 if ((ret + (type == READ)) > 0) {
746 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500747 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400748 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500749 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
755 unsigned long vlen, loff_t *pos)
756{
757 if (!(file->f_mode & FMODE_READ))
758 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700759 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -EINVAL;
761
762 return do_readv_writev(READ, file, vec, vlen, pos);
763}
764
765EXPORT_SYMBOL(vfs_readv);
766
767ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
768 unsigned long vlen, loff_t *pos)
769{
770 if (!(file->f_mode & FMODE_WRITE))
771 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700772 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return -EINVAL;
774
775 return do_readv_writev(WRITE, file, vec, vlen, pos);
776}
777
778EXPORT_SYMBOL(vfs_writev);
779
Heiko Carstens3cdad422009-01-14 14:14:22 +0100780SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
781 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Al Viro2903ff02012-08-28 12:52:22 -0400783 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Al Viro2903ff02012-08-28 12:52:22 -0400786 if (f.file) {
787 loff_t pos = file_pos_read(f.file);
788 ret = vfs_readv(f.file, vec, vlen, &pos);
789 file_pos_write(f.file, pos);
790 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
793 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800794 add_rchar(current, ret);
795 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return ret;
797}
798
Heiko Carstens3cdad422009-01-14 14:14:22 +0100799SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
800 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Al Viro2903ff02012-08-28 12:52:22 -0400802 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Al Viro2903ff02012-08-28 12:52:22 -0400805 if (f.file) {
806 loff_t pos = file_pos_read(f.file);
807 ret = vfs_writev(f.file, vec, vlen, &pos);
808 file_pos_write(f.file, pos);
809 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
812 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800813 add_wchar(current, ret);
814 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return ret;
816}
817
Linus Torvalds601cc112009-04-03 08:03:22 -0700818static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700819{
Linus Torvalds601cc112009-04-03 08:03:22 -0700820#define HALF_LONG_BITS (BITS_PER_LONG / 2)
821 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
822}
823
824SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
825 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
826{
827 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400828 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700829 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700830
831 if (pos < 0)
832 return -EINVAL;
833
Al Viro2903ff02012-08-28 12:52:22 -0400834 f = fdget(fd);
835 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700836 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400837 if (f.file->f_mode & FMODE_PREAD)
838 ret = vfs_readv(f.file, vec, vlen, &pos);
839 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700840 }
841
842 if (ret > 0)
843 add_rchar(current, ret);
844 inc_syscr(current);
845 return ret;
846}
847
848SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700849 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700850{
Linus Torvalds601cc112009-04-03 08:03:22 -0700851 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400852 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700853 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700854
855 if (pos < 0)
856 return -EINVAL;
857
Al Viro2903ff02012-08-28 12:52:22 -0400858 f = fdget(fd);
859 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700860 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400861 if (f.file->f_mode & FMODE_PWRITE)
862 ret = vfs_writev(f.file, vec, vlen, &pos);
863 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700864 }
865
866 if (ret > 0)
867 add_wchar(current, ret);
868 inc_syscw(current);
869 return ret;
870}
871
Catalin Marinas8f9c0112012-09-19 12:01:52 +0100872ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, size_t count,
873 loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Al Viro2903ff02012-08-28 12:52:22 -0400875 struct fd in, out;
876 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 loff_t pos;
878 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -0400879 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /*
882 * Get input file, and verify that it is ok..
883 */
884 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400885 in = fdget(in_fd);
886 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 goto out;
Al Viro2903ff02012-08-28 12:52:22 -0400888 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 retval = -ESPIPE;
891 if (!ppos)
Al Viro2903ff02012-08-28 12:52:22 -0400892 ppos = &in.file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 else
Al Viro2903ff02012-08-28 12:52:22 -0400894 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -0400896 retval = rw_verify_area(READ, in.file, ppos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800897 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800899 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /*
902 * Get output file, and verify that it is ok..
903 */
904 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400905 out = fdget(out_fd);
906 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -0400908 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 goto fput_out;
910 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -0500911 in_inode = file_inode(in.file);
912 out_inode = file_inode(out.file);
Al Viro2903ff02012-08-28 12:52:22 -0400913 retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800914 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800916 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (!max)
919 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
920
921 pos = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (unlikely(pos + count > max)) {
923 retval = -EOVERFLOW;
924 if (pos >= max)
925 goto fput_out;
926 count = max - pos;
927 }
928
Jens Axboed96e6e72007-06-11 12:18:52 +0200929 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +0200930#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +0200931 /*
932 * We need to debate whether we can enable this or not. The
933 * man page documents EAGAIN return for the output at least,
934 * and the application is arguably buggy if it doesn't expect
935 * EAGAIN on a non-blocking file descriptor.
936 */
Al Viro2903ff02012-08-28 12:52:22 -0400937 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +0200938 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +0200939#endif
Al Viro2903ff02012-08-28 12:52:22 -0400940 retval = do_splice_direct(in.file, ppos, out.file, count, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800943 add_rchar(current, retval);
944 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -0800945 fsnotify_access(in.file);
946 fsnotify_modify(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800949 inc_syscr(current);
950 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (*ppos > max)
952 retval = -EOVERFLOW;
953
954fput_out:
Al Viro2903ff02012-08-28 12:52:22 -0400955 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956fput_in:
Al Viro2903ff02012-08-28 12:52:22 -0400957 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958out:
959 return retval;
960}
961
Heiko Carstens002c8972009-01-14 14:14:18 +0100962SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 loff_t pos;
965 off_t off;
966 ssize_t ret;
967
968 if (offset) {
969 if (unlikely(get_user(off, offset)))
970 return -EFAULT;
971 pos = off;
972 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
973 if (unlikely(put_user(pos, offset)))
974 return -EFAULT;
975 return ret;
976 }
977
978 return do_sendfile(out_fd, in_fd, NULL, count, 0);
979}
980
Heiko Carstens002c8972009-01-14 14:14:18 +0100981SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
983 loff_t pos;
984 ssize_t ret;
985
986 if (offset) {
987 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
988 return -EFAULT;
989 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
990 if (unlikely(put_user(pos, offset)))
991 return -EFAULT;
992 return ret;
993 }
994
995 return do_sendfile(out_fd, in_fd, NULL, count, 0);
996}