blob: ffc99d22e0a3656711f14ac7e094cc954d1d90bd [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>
Badari Pulavartyee0b3e62006-09-30 23:28:47 -070018#include "read_write.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/uaccess.h>
21#include <asm/unistd.h>
22
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080023const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -070025 .read = do_sync_read,
26 .aio_read = generic_file_aio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020028 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029};
30
31EXPORT_SYMBOL(generic_ro_fops);
32
Al Virocccb5a12010-12-17 07:44:05 -050033static inline int unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070034{
Al Virocccb5a12010-12-17 07:44:05 -050035 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070036}
37
Andi Kleenef3d0fd2011-09-15 16:06:48 -070038static loff_t lseek_execute(struct file *file, struct inode *inode,
39 loff_t offset, loff_t maxsize)
40{
41 if (offset < 0 && !unsigned_offsets(file))
42 return -EINVAL;
43 if (offset > maxsize)
44 return -EINVAL;
45
46 if (offset != file->f_pos) {
47 file->f_pos = offset;
48 file->f_version = 0;
49 }
50 return offset;
51}
52
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020053/**
Andi Kleen57604952011-09-15 16:06:50 -070054 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020055 * @file: file structure to seek on
56 * @offset: file offset to seek to
57 * @origin: type of seek
Andi Kleen57604952011-09-15 16:06:50 -070058 * @size: max size of file system
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020059 *
Andi Kleen57604952011-09-15 16:06:50 -070060 * This is a variant of generic_file_llseek that allows passing in a custom
61 * file size.
Andi Kleenef3d0fd2011-09-15 16:06:48 -070062 *
63 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070064 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070065 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
66 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020067 */
Andi Kleen9465efc2008-06-27 11:05:24 +020068loff_t
Andi Kleen57604952011-09-15 16:06:50 -070069generic_file_llseek_size(struct file *file, loff_t offset, int origin,
70 loff_t maxsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 struct inode *inode = file->f_mapping->host;
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 switch (origin) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020075 case SEEK_END:
Andi Kleenef3d0fd2011-09-15 16:06:48 -070076 offset += i_size_read(inode);
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020077 break;
78 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080079 /*
80 * Here we special-case the lseek(fd, 0, SEEK_CUR)
81 * position-querying operation. Avoid rewriting the "same"
82 * f_pos value back to the file because a concurrent read(),
83 * write() or lseek() might have altered it
84 */
85 if (offset == 0)
86 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -070087 /*
88 * f_lock protects against read/modify/write race with other
89 * SEEK_CURs. Note that parallel writes and reads behave
90 * like SEEK_SET.
91 */
92 spin_lock(&file->f_lock);
93 offset = lseek_execute(file, inode, file->f_pos + offset,
Andi Kleen57604952011-09-15 16:06:50 -070094 maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -070095 spin_unlock(&file->f_lock);
96 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -040097 case SEEK_DATA:
98 /*
99 * In the generic case the entire file is data, so as long as
100 * offset isn't at the end of the file then the offset is data.
101 */
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700102 if (offset >= i_size_read(inode))
Josef Bacik982d8162011-07-18 13:21:35 -0400103 return -ENXIO;
104 break;
105 case SEEK_HOLE:
106 /*
107 * There is a virtual hole at the end of the file, so as long as
108 * offset isn't i_size or larger, return i_size.
109 */
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700110 if (offset >= i_size_read(inode))
Josef Bacik982d8162011-07-18 13:21:35 -0400111 return -ENXIO;
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700112 offset = i_size_read(inode);
Josef Bacik982d8162011-07-18 13:21:35 -0400113 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200115
Andi Kleen57604952011-09-15 16:06:50 -0700116 return lseek_execute(file, inode, offset, maxsize);
117}
118EXPORT_SYMBOL(generic_file_llseek_size);
119
120/**
121 * generic_file_llseek - generic llseek implementation for regular files
122 * @file: file structure to seek on
123 * @offset: file offset to seek to
124 * @origin: type of seek
125 *
126 * This is a generic implemenation of ->llseek useable for all normal local
127 * filesystems. It just updates the file offset to the value specified by
128 * @offset and @origin under i_mutex.
129 */
130loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
131{
132 struct inode *inode = file->f_mapping->host;
133
134 return generic_file_llseek_size(file, offset, origin,
135 inode->i_sb->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
Andi Kleen9465efc2008-06-27 11:05:24 +0200137EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
jan Blunckae6afc32010-05-26 14:44:48 -0700139/**
140 * noop_llseek - No Operation Performed llseek implementation
141 * @file: file structure to seek on
142 * @offset: file offset to seek to
143 * @origin: type of seek
144 *
145 * This is an implementation of ->llseek useable for the rare special case when
146 * userspace expects the seek to succeed but the (device) file is actually not
147 * able to perform the seek. In this case you use noop_llseek() instead of
148 * falling back to the default implementation of ->llseek.
149 */
150loff_t noop_llseek(struct file *file, loff_t offset, int origin)
151{
152 return file->f_pos;
153}
154EXPORT_SYMBOL(noop_llseek);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156loff_t no_llseek(struct file *file, loff_t offset, int origin)
157{
158 return -ESPIPE;
159}
160EXPORT_SYMBOL(no_llseek);
161
162loff_t default_llseek(struct file *file, loff_t offset, int origin)
163{
Josef Bacik982d8162011-07-18 13:21:35 -0400164 struct inode *inode = file->f_path.dentry->d_inode;
David Sterba16abef02008-04-22 15:09:22 +0200165 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Josef Bacik982d8162011-07-18 13:21:35 -0400167 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 switch (origin) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700169 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400170 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700172 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800173 if (offset == 0) {
174 retval = file->f_pos;
175 goto out;
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400178 break;
179 case SEEK_DATA:
180 /*
181 * In the generic case the entire file is data, so as
182 * long as offset isn't at the end of the file then the
183 * offset is data.
184 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300185 if (offset >= inode->i_size) {
186 retval = -ENXIO;
187 goto out;
188 }
Josef Bacik982d8162011-07-18 13:21:35 -0400189 break;
190 case SEEK_HOLE:
191 /*
192 * There is a virtual hole at the end of the file, so
193 * as long as offset isn't i_size or larger, return
194 * i_size.
195 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300196 if (offset >= inode->i_size) {
197 retval = -ENXIO;
198 goto out;
199 }
Josef Bacik982d8162011-07-18 13:21:35 -0400200 offset = inode->i_size;
201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500204 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (offset != file->f_pos) {
206 file->f_pos = offset;
207 file->f_version = 0;
208 }
209 retval = offset;
210 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800211out:
Josef Bacik982d8162011-07-18 13:21:35 -0400212 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return retval;
214}
215EXPORT_SYMBOL(default_llseek);
216
217loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
218{
219 loff_t (*fn)(struct file *, loff_t, int);
220
221 fn = no_llseek;
222 if (file->f_mode & FMODE_LSEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (file->f_op && file->f_op->llseek)
224 fn = file->f_op->llseek;
225 }
226 return fn(file, offset, origin);
227}
228EXPORT_SYMBOL(vfs_llseek);
229
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100230SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 off_t retval;
233 struct file * file;
234 int fput_needed;
235
236 retval = -EBADF;
237 file = fget_light(fd, &fput_needed);
238 if (!file)
239 goto bad;
240
241 retval = -EINVAL;
Chris Snook1ae70752007-05-08 00:24:15 -0700242 if (origin <= SEEK_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 loff_t res = vfs_llseek(file, offset, origin);
244 retval = res;
245 if (res != (loff_t)retval)
246 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
247 }
248 fput_light(file, fput_needed);
249bad:
250 return retval;
251}
252
253#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100254SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
255 unsigned long, offset_low, loff_t __user *, result,
256 unsigned int, origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 int retval;
259 struct file * file;
260 loff_t offset;
261 int fput_needed;
262
263 retval = -EBADF;
264 file = fget_light(fd, &fput_needed);
265 if (!file)
266 goto bad;
267
268 retval = -EINVAL;
Chris Snook1ae70752007-05-08 00:24:15 -0700269 if (origin > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto out_putf;
271
272 offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
273 origin);
274
275 retval = (int)offset;
276 if (offset >= 0) {
277 retval = -EFAULT;
278 if (!copy_to_user(result, &offset, sizeof(offset)))
279 retval = 0;
280 }
281out_putf:
282 fput_light(file, fput_needed);
283bad:
284 return retval;
285}
286#endif
287
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700288
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
Eric Dumazet163da952007-02-12 00:52:24 -0800300 inode = file->f_path.dentry->d_inode;
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{
462 struct file *file;
463 ssize_t ret = -EBADF;
464 int fput_needed;
465
466 file = fget_light(fd, &fput_needed);
467 if (file) {
468 loff_t pos = file_pos_read(file);
469 ret = vfs_read(file, buf, count, &pos);
470 file_pos_write(file, pos);
471 fput_light(file, fput_needed);
472 }
473
474 return ret;
475}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Heiko Carstens3cdad422009-01-14 14:14:22 +0100477SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
478 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 struct file *file;
481 ssize_t ret = -EBADF;
482 int fput_needed;
483
484 file = fget_light(fd, &fput_needed);
485 if (file) {
486 loff_t pos = file_pos_read(file);
487 ret = vfs_write(file, buf, count, &pos);
488 file_pos_write(file, pos);
489 fput_light(file, fput_needed);
490 }
491
492 return ret;
493}
494
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100495SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
496 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct file *file;
499 ssize_t ret = -EBADF;
500 int fput_needed;
501
502 if (pos < 0)
503 return -EINVAL;
504
505 file = fget_light(fd, &fput_needed);
506 if (file) {
507 ret = -ESPIPE;
508 if (file->f_mode & FMODE_PREAD)
509 ret = vfs_read(file, buf, count, &pos);
510 fput_light(file, fput_needed);
511 }
512
513 return ret;
514}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100515#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
516asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
517{
518 return SYSC_pread64((unsigned int) fd, (char __user *) buf,
519 (size_t) count, pos);
520}
521SYSCALL_ALIAS(sys_pread64, SyS_pread64);
522#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100524SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
525 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 struct file *file;
528 ssize_t ret = -EBADF;
529 int fput_needed;
530
531 if (pos < 0)
532 return -EINVAL;
533
534 file = fget_light(fd, &fput_needed);
535 if (file) {
536 ret = -ESPIPE;
537 if (file->f_mode & FMODE_PWRITE)
538 ret = vfs_write(file, buf, count, &pos);
539 fput_light(file, fput_needed);
540 }
541
542 return ret;
543}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100544#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
545asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
546{
547 return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
548 (size_t) count, pos);
549}
550SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
551#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553/*
554 * Reduce an iovec's length in-place. Return the resulting number of segments
555 */
556unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
557{
558 unsigned long seg = 0;
559 size_t len = 0;
560
561 while (seg < nr_segs) {
562 seg++;
563 if (len + iov->iov_len >= to) {
564 iov->iov_len = to - len;
565 break;
566 }
567 len += iov->iov_len;
568 iov++;
569 }
570 return seg;
571}
Eric Sandeen19295522008-01-28 23:58:27 -0500572EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700574ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
575 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
576{
577 struct kiocb kiocb;
578 ssize_t ret;
579
580 init_sync_kiocb(&kiocb, filp);
581 kiocb.ki_pos = *ppos;
582 kiocb.ki_left = len;
583 kiocb.ki_nbytes = len;
584
585 for (;;) {
586 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
587 if (ret != -EIOCBRETRY)
588 break;
589 wait_on_retry_sync_kiocb(&kiocb);
590 }
591
592 if (ret == -EIOCBQUEUED)
593 ret = wait_on_sync_kiocb(&kiocb);
594 *ppos = kiocb.ki_pos;
595 return ret;
596}
597
598/* Do it by hand, with file-ops */
599ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
600 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
601{
602 struct iovec *vector = iov;
603 ssize_t ret = 0;
604
605 while (nr_segs > 0) {
606 void __user *base;
607 size_t len;
608 ssize_t nr;
609
610 base = vector->iov_base;
611 len = vector->iov_len;
612 vector++;
613 nr_segs--;
614
615 nr = fn(filp, base, len, ppos);
616
617 if (nr < 0) {
618 if (!ret)
619 ret = nr;
620 break;
621 }
622 ret += nr;
623 if (nr != len)
624 break;
625 }
626
627 return ret;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/* A write operation does a read from user space and vice versa */
631#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
632
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700633ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
634 unsigned long nr_segs, unsigned long fast_segs,
635 struct iovec *fast_pointer,
Christopher Yeohfcf63402011-10-31 17:06:39 -0700636 struct iovec **ret_pointer,
637 int check_access)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700638{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700639 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700640 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700641 struct iovec *iov = fast_pointer;
642
Linus Torvalds435f49a2010-10-29 10:36:49 -0700643 /*
644 * SuS says "The readv() function *may* fail if the iovcnt argument
645 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
646 * traditionally returned zero for zero segments, so...
647 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700648 if (nr_segs == 0) {
649 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700650 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700651 }
652
Linus Torvalds435f49a2010-10-29 10:36:49 -0700653 /*
654 * First get the "struct iovec" from user memory and
655 * verify all the pointers
656 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700657 if (nr_segs > UIO_MAXIOV) {
658 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700659 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700660 }
661 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700662 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700663 if (iov == NULL) {
664 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700665 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700666 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700667 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700668 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
669 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700670 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700671 }
672
Linus Torvalds435f49a2010-10-29 10:36:49 -0700673 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700674 * According to the Single Unix Specification we should return EINVAL
675 * if an element length is < 0 when cast to ssize_t or if the
676 * total length would overflow the ssize_t return value of the
677 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700678 *
679 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
680 * overflow case.
681 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700682 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700683 for (seg = 0; seg < nr_segs; seg++) {
684 void __user *buf = iov[seg].iov_base;
685 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700686
687 /* see if we we're about to use an invalid len or if
688 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700689 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700690 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700691 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700692 }
Christopher Yeohfcf63402011-10-31 17:06:39 -0700693 if (check_access
694 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700695 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700696 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700697 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700698 if (len > MAX_RW_COUNT - ret) {
699 len = MAX_RW_COUNT - ret;
700 iov[seg].iov_len = len;
701 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700702 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700703 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700704out:
705 *ret_pointer = iov;
706 return ret;
707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709static ssize_t do_readv_writev(int type, struct file *file,
710 const struct iovec __user * uvector,
711 unsigned long nr_segs, loff_t *pos)
712{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 size_t tot_len;
714 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700715 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 io_fn_t fn;
718 iov_fn_t fnv;
719
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700720 if (!file->f_op) {
721 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 goto out;
723 }
724
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700725 ret = rw_copy_check_uvector(type, uvector, nr_segs,
Christopher Yeohfcf63402011-10-31 17:06:39 -0700726 ARRAY_SIZE(iovstack), iovstack, &iov, 1);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700727 if (ret <= 0)
728 goto out;
729
730 tot_len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800732 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 goto out;
734
735 fnv = NULL;
736 if (type == READ) {
737 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700738 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 } else {
740 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700741 fnv = file->f_op->aio_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700744 if (fnv)
745 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
746 pos, fnv);
747 else
748 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750out:
751 if (iov != iovstack)
752 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400753 if ((ret + (type == READ)) > 0) {
754 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500755 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400756 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500757 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
763 unsigned long vlen, loff_t *pos)
764{
765 if (!(file->f_mode & FMODE_READ))
766 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700767 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return -EINVAL;
769
770 return do_readv_writev(READ, file, vec, vlen, pos);
771}
772
773EXPORT_SYMBOL(vfs_readv);
774
775ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
776 unsigned long vlen, loff_t *pos)
777{
778 if (!(file->f_mode & FMODE_WRITE))
779 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700780 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -EINVAL;
782
783 return do_readv_writev(WRITE, file, vec, vlen, pos);
784}
785
786EXPORT_SYMBOL(vfs_writev);
787
Heiko Carstens3cdad422009-01-14 14:14:22 +0100788SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
789 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 struct file *file;
792 ssize_t ret = -EBADF;
793 int fput_needed;
794
795 file = fget_light(fd, &fput_needed);
796 if (file) {
797 loff_t pos = file_pos_read(file);
798 ret = vfs_readv(file, vec, vlen, &pos);
799 file_pos_write(file, pos);
800 fput_light(file, fput_needed);
801 }
802
803 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800804 add_rchar(current, ret);
805 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return ret;
807}
808
Heiko Carstens3cdad422009-01-14 14:14:22 +0100809SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
810 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct file *file;
813 ssize_t ret = -EBADF;
814 int fput_needed;
815
816 file = fget_light(fd, &fput_needed);
817 if (file) {
818 loff_t pos = file_pos_read(file);
819 ret = vfs_writev(file, vec, vlen, &pos);
820 file_pos_write(file, pos);
821 fput_light(file, fput_needed);
822 }
823
824 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800825 add_wchar(current, ret);
826 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return ret;
828}
829
Linus Torvalds601cc112009-04-03 08:03:22 -0700830static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700831{
Linus Torvalds601cc112009-04-03 08:03:22 -0700832#define HALF_LONG_BITS (BITS_PER_LONG / 2)
833 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
834}
835
836SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
837 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
838{
839 loff_t pos = pos_from_hilo(pos_h, pos_l);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700840 struct file *file;
841 ssize_t ret = -EBADF;
842 int fput_needed;
843
844 if (pos < 0)
845 return -EINVAL;
846
847 file = fget_light(fd, &fput_needed);
848 if (file) {
849 ret = -ESPIPE;
850 if (file->f_mode & FMODE_PREAD)
851 ret = vfs_readv(file, vec, vlen, &pos);
852 fput_light(file, fput_needed);
853 }
854
855 if (ret > 0)
856 add_rchar(current, ret);
857 inc_syscr(current);
858 return ret;
859}
860
861SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700862 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700863{
Linus Torvalds601cc112009-04-03 08:03:22 -0700864 loff_t pos = pos_from_hilo(pos_h, pos_l);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700865 struct file *file;
866 ssize_t ret = -EBADF;
867 int fput_needed;
868
869 if (pos < 0)
870 return -EINVAL;
871
872 file = fget_light(fd, &fput_needed);
873 if (file) {
874 ret = -ESPIPE;
875 if (file->f_mode & FMODE_PWRITE)
876 ret = vfs_writev(file, vec, vlen, &pos);
877 fput_light(file, fput_needed);
878 }
879
880 if (ret > 0)
881 add_wchar(current, ret);
882 inc_syscw(current);
883 return ret;
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
887 size_t count, loff_t max)
888{
889 struct file * in_file, * out_file;
890 struct inode * in_inode, * out_inode;
891 loff_t pos;
892 ssize_t retval;
Jens Axboe534f2aa2007-06-01 14:52:37 +0200893 int fput_needed_in, fput_needed_out, fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /*
896 * Get input file, and verify that it is ok..
897 */
898 retval = -EBADF;
899 in_file = fget_light(in_fd, &fput_needed_in);
900 if (!in_file)
901 goto out;
902 if (!(in_file->f_mode & FMODE_READ))
903 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 retval = -ESPIPE;
905 if (!ppos)
906 ppos = &in_file->f_pos;
907 else
908 if (!(in_file->f_mode & FMODE_PREAD))
909 goto fput_in;
910 retval = rw_verify_area(READ, in_file, ppos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800911 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800913 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /*
916 * Get output file, and verify that it is ok..
917 */
918 retval = -EBADF;
919 out_file = fget_light(out_fd, &fput_needed_out);
920 if (!out_file)
921 goto fput_in;
922 if (!(out_file->f_mode & FMODE_WRITE))
923 goto fput_out;
924 retval = -EINVAL;
Miklos Szeredi68181732009-05-07 15:37:36 +0200925 in_inode = in_file->f_path.dentry->d_inode;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800926 out_inode = out_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800928 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800930 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (!max)
933 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
934
935 pos = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (unlikely(pos + count > max)) {
937 retval = -EOVERFLOW;
938 if (pos >= max)
939 goto fput_out;
940 count = max - pos;
941 }
942
Jens Axboed96e6e72007-06-11 12:18:52 +0200943 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +0200944#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +0200945 /*
946 * We need to debate whether we can enable this or not. The
947 * man page documents EAGAIN return for the output at least,
948 * and the application is arguably buggy if it doesn't expect
949 * EAGAIN on a non-blocking file descriptor.
950 */
951 if (in_file->f_flags & O_NONBLOCK)
952 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +0200953#endif
Jens Axboed96e6e72007-06-11 12:18:52 +0200954 retval = do_splice_direct(in_file, ppos, out_file, count, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800957 add_rchar(current, retval);
958 add_wchar(current, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800961 inc_syscr(current);
962 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (*ppos > max)
964 retval = -EOVERFLOW;
965
966fput_out:
967 fput_light(out_file, fput_needed_out);
968fput_in:
969 fput_light(in_file, fput_needed_in);
970out:
971 return retval;
972}
973
Heiko Carstens002c8972009-01-14 14:14:18 +0100974SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 loff_t pos;
977 off_t off;
978 ssize_t ret;
979
980 if (offset) {
981 if (unlikely(get_user(off, offset)))
982 return -EFAULT;
983 pos = off;
984 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
985 if (unlikely(put_user(pos, offset)))
986 return -EFAULT;
987 return ret;
988 }
989
990 return do_sendfile(out_fd, in_fd, NULL, count, 0);
991}
992
Heiko Carstens002c8972009-01-14 14:14:18 +0100993SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 loff_t pos;
996 ssize_t ret;
997
998 if (offset) {
999 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1000 return -EFAULT;
1001 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1002 if (unlikely(put_user(pos, offset)))
1003 return -EFAULT;
1004 return ret;
1005 }
1006
1007 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1008}