blob: 1adfb691e4f152444d8b7a080c1159057bbed9ad [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
Eric Sandeene8b96eb2012-04-30 13:11:29 -050058 * @size: max size of this file in file system
59 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020060 *
Andi Kleen57604952011-09-15 16:06:50 -070061 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050062 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070063 *
64 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070065 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070066 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
67 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020068 */
Andi Kleen9465efc2008-06-27 11:05:24 +020069loff_t
Andi Kleen57604952011-09-15 16:06:50 -070070generic_file_llseek_size(struct file *file, loff_t offset, int origin,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050071 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct inode *inode = file->f_mapping->host;
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 switch (origin) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020076 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050077 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020078 break;
79 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080080 /*
81 * Here we special-case the lseek(fd, 0, SEEK_CUR)
82 * position-querying operation. Avoid rewriting the "same"
83 * f_pos value back to the file because a concurrent read(),
84 * write() or lseek() might have altered it
85 */
86 if (offset == 0)
87 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -070088 /*
89 * f_lock protects against read/modify/write race with other
90 * SEEK_CURs. Note that parallel writes and reads behave
91 * like SEEK_SET.
92 */
93 spin_lock(&file->f_lock);
94 offset = lseek_execute(file, inode, file->f_pos + offset,
Andi Kleen57604952011-09-15 16:06:50 -070095 maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -070096 spin_unlock(&file->f_lock);
97 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -040098 case SEEK_DATA:
99 /*
100 * In the generic case the entire file is data, so as long as
101 * offset isn't at the end of the file then the offset is data.
102 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500103 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400104 return -ENXIO;
105 break;
106 case SEEK_HOLE:
107 /*
108 * There is a virtual hole at the end of the file, so as long as
109 * offset isn't i_size or larger, return i_size.
110 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500111 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400112 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500113 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200116
Andi Kleen57604952011-09-15 16:06:50 -0700117 return lseek_execute(file, inode, offset, maxsize);
118}
119EXPORT_SYMBOL(generic_file_llseek_size);
120
121/**
122 * generic_file_llseek - generic llseek implementation for regular files
123 * @file: file structure to seek on
124 * @offset: file offset to seek to
125 * @origin: type of seek
126 *
127 * This is a generic implemenation of ->llseek useable for all normal local
128 * filesystems. It just updates the file offset to the value specified by
129 * @offset and @origin under i_mutex.
130 */
131loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
132{
133 struct inode *inode = file->f_mapping->host;
134
135 return generic_file_llseek_size(file, offset, origin,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500136 inode->i_sb->s_maxbytes,
137 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
Andi Kleen9465efc2008-06-27 11:05:24 +0200139EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
jan Blunckae6afc32010-05-26 14:44:48 -0700141/**
142 * noop_llseek - No Operation Performed llseek implementation
143 * @file: file structure to seek on
144 * @offset: file offset to seek to
145 * @origin: type of seek
146 *
147 * This is an implementation of ->llseek useable for the rare special case when
148 * userspace expects the seek to succeed but the (device) file is actually not
149 * able to perform the seek. In this case you use noop_llseek() instead of
150 * falling back to the default implementation of ->llseek.
151 */
152loff_t noop_llseek(struct file *file, loff_t offset, int origin)
153{
154 return file->f_pos;
155}
156EXPORT_SYMBOL(noop_llseek);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158loff_t no_llseek(struct file *file, loff_t offset, int origin)
159{
160 return -ESPIPE;
161}
162EXPORT_SYMBOL(no_llseek);
163
164loff_t default_llseek(struct file *file, loff_t offset, int origin)
165{
Josef Bacik982d8162011-07-18 13:21:35 -0400166 struct inode *inode = file->f_path.dentry->d_inode;
David Sterba16abef02008-04-22 15:09:22 +0200167 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Josef Bacik982d8162011-07-18 13:21:35 -0400169 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 switch (origin) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700171 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400172 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700174 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800175 if (offset == 0) {
176 retval = file->f_pos;
177 goto out;
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400180 break;
181 case SEEK_DATA:
182 /*
183 * In the generic case the entire file is data, so as
184 * long as offset isn't at the end of the file then the
185 * offset is data.
186 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300187 if (offset >= inode->i_size) {
188 retval = -ENXIO;
189 goto out;
190 }
Josef Bacik982d8162011-07-18 13:21:35 -0400191 break;
192 case SEEK_HOLE:
193 /*
194 * There is a virtual hole at the end of the file, so
195 * as long as offset isn't i_size or larger, return
196 * i_size.
197 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300198 if (offset >= inode->i_size) {
199 retval = -ENXIO;
200 goto out;
201 }
Josef Bacik982d8162011-07-18 13:21:35 -0400202 offset = inode->i_size;
203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500206 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (offset != file->f_pos) {
208 file->f_pos = offset;
209 file->f_version = 0;
210 }
211 retval = offset;
212 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800213out:
Josef Bacik982d8162011-07-18 13:21:35 -0400214 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return retval;
216}
217EXPORT_SYMBOL(default_llseek);
218
219loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
220{
221 loff_t (*fn)(struct file *, loff_t, int);
222
223 fn = no_llseek;
224 if (file->f_mode & FMODE_LSEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (file->f_op && file->f_op->llseek)
226 fn = file->f_op->llseek;
227 }
228 return fn(file, offset, origin);
229}
230EXPORT_SYMBOL(vfs_llseek);
231
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100232SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 off_t retval;
235 struct file * file;
236 int fput_needed;
237
238 retval = -EBADF;
239 file = fget_light(fd, &fput_needed);
240 if (!file)
241 goto bad;
242
243 retval = -EINVAL;
Chris Snook1ae70752007-05-08 00:24:15 -0700244 if (origin <= SEEK_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 loff_t res = vfs_llseek(file, offset, origin);
246 retval = res;
247 if (res != (loff_t)retval)
248 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
249 }
250 fput_light(file, fput_needed);
251bad:
252 return retval;
253}
254
255#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100256SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
257 unsigned long, offset_low, loff_t __user *, result,
258 unsigned int, origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 int retval;
261 struct file * file;
262 loff_t offset;
263 int fput_needed;
264
265 retval = -EBADF;
266 file = fget_light(fd, &fput_needed);
267 if (!file)
268 goto bad;
269
270 retval = -EINVAL;
Chris Snook1ae70752007-05-08 00:24:15 -0700271 if (origin > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 goto out_putf;
273
274 offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
275 origin);
276
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:
284 fput_light(file, fput_needed);
285bad:
286 return retval;
287}
288#endif
289
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700290
Linus Torvaldse28cc712006-01-04 16:20:40 -0800291/*
292 * rw_verify_area doesn't like huge counts. We limit
293 * them to something that fits in "int" so that others
294 * won't have to do range checks all the time.
295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
297{
298 struct inode *inode;
299 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100300 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Eric Dumazet163da952007-02-12 00:52:24 -0800302 inode = file->f_path.dentry->d_inode;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800303 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100304 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500306 if (unlikely(pos < 0)) {
307 if (!unsigned_offsets(file))
308 return retval;
309 if (count >= -pos) /* both values are in 0..LLONG_MAX */
310 return -EOVERFLOW;
311 } else if (unlikely((loff_t) (pos + count) < 0)) {
312 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700313 return retval;
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Pavel Emelyanova16877c2007-10-01 14:41:11 -0700316 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
James Morrisc43e2592008-01-12 22:05:48 +1100317 retval = locks_mandatory_area(
Linus Torvaldse28cc712006-01-04 16:20:40 -0800318 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
319 inode, file, pos, count);
320 if (retval < 0)
321 return retval;
322 }
James Morrisc43e2592008-01-12 22:05:48 +1100323 retval = security_file_permission(file,
324 read_write == READ ? MAY_READ : MAY_WRITE);
325 if (retval)
326 return retval;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800327 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700330static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
331{
332 set_current_state(TASK_UNINTERRUPTIBLE);
333 if (!kiocbIsKicked(iocb))
334 schedule();
335 else
336 kiocbClearKicked(iocb);
337 __set_current_state(TASK_RUNNING);
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
341{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700342 struct iovec iov = { .iov_base = buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct kiocb kiocb;
344 ssize_t ret;
345
346 init_sync_kiocb(&kiocb, filp);
347 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700348 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000349 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700350
351 for (;;) {
352 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
353 if (ret != -EIOCBRETRY)
354 break;
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700355 wait_on_retry_sync_kiocb(&kiocb);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700356 }
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (-EIOCBQUEUED == ret)
359 ret = wait_on_sync_kiocb(&kiocb);
360 *ppos = kiocb.ki_pos;
361 return ret;
362}
363
364EXPORT_SYMBOL(do_sync_read);
365
366ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
367{
368 ssize_t ret;
369
370 if (!(file->f_mode & FMODE_READ))
371 return -EBADF;
372 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
373 return -EINVAL;
374 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
375 return -EFAULT;
376
377 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800378 if (ret >= 0) {
379 count = ret;
James Morrisc43e2592008-01-12 22:05:48 +1100380 if (file->f_op->read)
381 ret = file->f_op->read(file, buf, count, pos);
382 else
383 ret = do_sync_read(file, buf, count, pos);
384 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500385 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100386 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
James Morrisc43e2592008-01-12 22:05:48 +1100388 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
391 return ret;
392}
393
394EXPORT_SYMBOL(vfs_read);
395
396ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
397{
Badari Pulavarty027445c2006-09-30 23:28:46 -0700398 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct kiocb kiocb;
400 ssize_t ret;
401
402 init_sync_kiocb(&kiocb, filp);
403 kiocb.ki_pos = *ppos;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700404 kiocb.ki_left = len;
David Howells61964eb2010-03-24 17:09:19 +0000405 kiocb.ki_nbytes = len;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700406
407 for (;;) {
408 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
409 if (ret != -EIOCBRETRY)
410 break;
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700411 wait_on_retry_sync_kiocb(&kiocb);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700412 }
Benjamin LaHaise63e68802005-06-23 00:10:27 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (-EIOCBQUEUED == ret)
415 ret = wait_on_sync_kiocb(&kiocb);
416 *ppos = kiocb.ki_pos;
417 return ret;
418}
419
420EXPORT_SYMBOL(do_sync_write);
421
422ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
423{
424 ssize_t ret;
425
426 if (!(file->f_mode & FMODE_WRITE))
427 return -EBADF;
428 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
429 return -EINVAL;
430 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
431 return -EFAULT;
432
433 ret = rw_verify_area(WRITE, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800434 if (ret >= 0) {
435 count = ret;
James Morrisc43e2592008-01-12 22:05:48 +1100436 if (file->f_op->write)
437 ret = file->f_op->write(file, buf, count, pos);
438 else
439 ret = do_sync_write(file, buf, count, pos);
440 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500441 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100442 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
James Morrisc43e2592008-01-12 22:05:48 +1100444 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
447 return ret;
448}
449
450EXPORT_SYMBOL(vfs_write);
451
452static inline loff_t file_pos_read(struct file *file)
453{
454 return file->f_pos;
455}
456
457static inline void file_pos_write(struct file *file, loff_t pos)
458{
459 file->f_pos = pos;
460}
461
Heiko Carstens3cdad422009-01-14 14:14:22 +0100462SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct file *file;
465 ssize_t ret = -EBADF;
466 int fput_needed;
467
468 file = fget_light(fd, &fput_needed);
469 if (file) {
470 loff_t pos = file_pos_read(file);
471 ret = vfs_read(file, buf, count, &pos);
472 file_pos_write(file, pos);
473 fput_light(file, fput_needed);
474 }
475
476 return ret;
477}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Heiko Carstens3cdad422009-01-14 14:14:22 +0100479SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
480 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct file *file;
483 ssize_t ret = -EBADF;
484 int fput_needed;
485
486 file = fget_light(fd, &fput_needed);
487 if (file) {
488 loff_t pos = file_pos_read(file);
489 ret = vfs_write(file, buf, count, &pos);
490 file_pos_write(file, pos);
491 fput_light(file, fput_needed);
492 }
493
494 return ret;
495}
496
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100497SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
498 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 struct file *file;
501 ssize_t ret = -EBADF;
502 int fput_needed;
503
504 if (pos < 0)
505 return -EINVAL;
506
507 file = fget_light(fd, &fput_needed);
508 if (file) {
509 ret = -ESPIPE;
510 if (file->f_mode & FMODE_PREAD)
511 ret = vfs_read(file, buf, count, &pos);
512 fput_light(file, fput_needed);
513 }
514
515 return ret;
516}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100517#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
518asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
519{
520 return SYSC_pread64((unsigned int) fd, (char __user *) buf,
521 (size_t) count, pos);
522}
523SYSCALL_ALIAS(sys_pread64, SyS_pread64);
524#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100526SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
527 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct file *file;
530 ssize_t ret = -EBADF;
531 int fput_needed;
532
533 if (pos < 0)
534 return -EINVAL;
535
536 file = fget_light(fd, &fput_needed);
537 if (file) {
538 ret = -ESPIPE;
539 if (file->f_mode & FMODE_PWRITE)
540 ret = vfs_write(file, buf, count, &pos);
541 fput_light(file, fput_needed);
542 }
543
544 return ret;
545}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100546#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
547asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
548{
549 return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
550 (size_t) count, pos);
551}
552SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
553#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555/*
556 * Reduce an iovec's length in-place. Return the resulting number of segments
557 */
558unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
559{
560 unsigned long seg = 0;
561 size_t len = 0;
562
563 while (seg < nr_segs) {
564 seg++;
565 if (len + iov->iov_len >= to) {
566 iov->iov_len = to - len;
567 break;
568 }
569 len += iov->iov_len;
570 iov++;
571 }
572 return seg;
573}
Eric Sandeen19295522008-01-28 23:58:27 -0500574EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700576ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
577 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
578{
579 struct kiocb kiocb;
580 ssize_t ret;
581
582 init_sync_kiocb(&kiocb, filp);
583 kiocb.ki_pos = *ppos;
584 kiocb.ki_left = len;
585 kiocb.ki_nbytes = len;
586
587 for (;;) {
588 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
589 if (ret != -EIOCBRETRY)
590 break;
591 wait_on_retry_sync_kiocb(&kiocb);
592 }
593
594 if (ret == -EIOCBQUEUED)
595 ret = wait_on_sync_kiocb(&kiocb);
596 *ppos = kiocb.ki_pos;
597 return ret;
598}
599
600/* Do it by hand, with file-ops */
601ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
602 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
603{
604 struct iovec *vector = iov;
605 ssize_t ret = 0;
606
607 while (nr_segs > 0) {
608 void __user *base;
609 size_t len;
610 ssize_t nr;
611
612 base = vector->iov_base;
613 len = vector->iov_len;
614 vector++;
615 nr_segs--;
616
617 nr = fn(filp, base, len, ppos);
618
619 if (nr < 0) {
620 if (!ret)
621 ret = nr;
622 break;
623 }
624 ret += nr;
625 if (nr != len)
626 break;
627 }
628
629 return ret;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/* A write operation does a read from user space and vice versa */
633#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
634
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700635ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
636 unsigned long nr_segs, unsigned long fast_segs,
637 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700638 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700639{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700640 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700641 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700642 struct iovec *iov = fast_pointer;
643
Linus Torvalds435f49a2010-10-29 10:36:49 -0700644 /*
645 * SuS says "The readv() function *may* fail if the iovcnt argument
646 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
647 * traditionally returned zero for zero segments, so...
648 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700649 if (nr_segs == 0) {
650 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700651 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700652 }
653
Linus Torvalds435f49a2010-10-29 10:36:49 -0700654 /*
655 * First get the "struct iovec" from user memory and
656 * verify all the pointers
657 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700658 if (nr_segs > UIO_MAXIOV) {
659 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700660 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700661 }
662 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700663 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700664 if (iov == NULL) {
665 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700666 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700667 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700668 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700669 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
670 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700671 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700672 }
673
Linus Torvalds435f49a2010-10-29 10:36:49 -0700674 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700675 * According to the Single Unix Specification we should return EINVAL
676 * if an element length is < 0 when cast to ssize_t or if the
677 * total length would overflow the ssize_t return value of the
678 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700679 *
680 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
681 * overflow case.
682 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700683 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700684 for (seg = 0; seg < nr_segs; seg++) {
685 void __user *buf = iov[seg].iov_base;
686 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700687
688 /* see if we we're about to use an invalid len or if
689 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700690 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700691 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700692 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700693 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700694 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700695 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700696 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700697 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700698 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700699 if (len > MAX_RW_COUNT - ret) {
700 len = MAX_RW_COUNT - ret;
701 iov[seg].iov_len = len;
702 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700703 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700704 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700705out:
706 *ret_pointer = iov;
707 return ret;
708}
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710static ssize_t do_readv_writev(int type, struct file *file,
711 const struct iovec __user * uvector,
712 unsigned long nr_segs, loff_t *pos)
713{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 size_t tot_len;
715 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700716 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 io_fn_t fn;
719 iov_fn_t fnv;
720
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700721 if (!file->f_op) {
722 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 goto out;
724 }
725
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700726 ret = rw_copy_check_uvector(type, uvector, nr_segs,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700727 ARRAY_SIZE(iovstack), iovstack, &iov);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700728 if (ret <= 0)
729 goto out;
730
731 tot_len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800733 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 goto out;
735
736 fnv = NULL;
737 if (type == READ) {
738 fn = file->f_op->read;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700739 fnv = file->f_op->aio_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 } else {
741 fn = (io_fn_t)file->f_op->write;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700742 fnv = file->f_op->aio_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700745 if (fnv)
746 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
747 pos, fnv);
748 else
749 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751out:
752 if (iov != iovstack)
753 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400754 if ((ret + (type == READ)) > 0) {
755 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500756 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400757 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500758 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
764 unsigned long vlen, loff_t *pos)
765{
766 if (!(file->f_mode & FMODE_READ))
767 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700768 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return -EINVAL;
770
771 return do_readv_writev(READ, file, vec, vlen, pos);
772}
773
774EXPORT_SYMBOL(vfs_readv);
775
776ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
777 unsigned long vlen, loff_t *pos)
778{
779 if (!(file->f_mode & FMODE_WRITE))
780 return -EBADF;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700781 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EINVAL;
783
784 return do_readv_writev(WRITE, file, vec, vlen, pos);
785}
786
787EXPORT_SYMBOL(vfs_writev);
788
Heiko Carstens3cdad422009-01-14 14:14:22 +0100789SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
790 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 struct file *file;
793 ssize_t ret = -EBADF;
794 int fput_needed;
795
796 file = fget_light(fd, &fput_needed);
797 if (file) {
798 loff_t pos = file_pos_read(file);
799 ret = vfs_readv(file, vec, vlen, &pos);
800 file_pos_write(file, pos);
801 fput_light(file, fput_needed);
802 }
803
804 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800805 add_rchar(current, ret);
806 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return ret;
808}
809
Heiko Carstens3cdad422009-01-14 14:14:22 +0100810SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
811 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 struct file *file;
814 ssize_t ret = -EBADF;
815 int fput_needed;
816
817 file = fget_light(fd, &fput_needed);
818 if (file) {
819 loff_t pos = file_pos_read(file);
820 ret = vfs_writev(file, vec, vlen, &pos);
821 file_pos_write(file, pos);
822 fput_light(file, fput_needed);
823 }
824
825 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800826 add_wchar(current, ret);
827 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return ret;
829}
830
Linus Torvalds601cc112009-04-03 08:03:22 -0700831static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700832{
Linus Torvalds601cc112009-04-03 08:03:22 -0700833#define HALF_LONG_BITS (BITS_PER_LONG / 2)
834 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
835}
836
837SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
838 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
839{
840 loff_t pos = pos_from_hilo(pos_h, pos_l);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700841 struct file *file;
842 ssize_t ret = -EBADF;
843 int fput_needed;
844
845 if (pos < 0)
846 return -EINVAL;
847
848 file = fget_light(fd, &fput_needed);
849 if (file) {
850 ret = -ESPIPE;
851 if (file->f_mode & FMODE_PREAD)
852 ret = vfs_readv(file, vec, vlen, &pos);
853 fput_light(file, fput_needed);
854 }
855
856 if (ret > 0)
857 add_rchar(current, ret);
858 inc_syscr(current);
859 return ret;
860}
861
862SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700863 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700864{
Linus Torvalds601cc112009-04-03 08:03:22 -0700865 loff_t pos = pos_from_hilo(pos_h, pos_l);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700866 struct file *file;
867 ssize_t ret = -EBADF;
868 int fput_needed;
869
870 if (pos < 0)
871 return -EINVAL;
872
873 file = fget_light(fd, &fput_needed);
874 if (file) {
875 ret = -ESPIPE;
876 if (file->f_mode & FMODE_PWRITE)
877 ret = vfs_writev(file, vec, vlen, &pos);
878 fput_light(file, fput_needed);
879 }
880
881 if (ret > 0)
882 add_wchar(current, ret);
883 inc_syscw(current);
884 return ret;
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
888 size_t count, loff_t max)
889{
890 struct file * in_file, * out_file;
891 struct inode * in_inode, * out_inode;
892 loff_t pos;
893 ssize_t retval;
Jens Axboe534f2aa2007-06-01 14:52:37 +0200894 int fput_needed_in, fput_needed_out, fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 /*
897 * Get input file, and verify that it is ok..
898 */
899 retval = -EBADF;
900 in_file = fget_light(in_fd, &fput_needed_in);
901 if (!in_file)
902 goto out;
903 if (!(in_file->f_mode & FMODE_READ))
904 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 retval = -ESPIPE;
906 if (!ppos)
907 ppos = &in_file->f_pos;
908 else
909 if (!(in_file->f_mode & FMODE_PREAD))
910 goto fput_in;
911 retval = rw_verify_area(READ, in_file, ppos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800912 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800914 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /*
917 * Get output file, and verify that it is ok..
918 */
919 retval = -EBADF;
920 out_file = fget_light(out_fd, &fput_needed_out);
921 if (!out_file)
922 goto fput_in;
923 if (!(out_file->f_mode & FMODE_WRITE))
924 goto fput_out;
925 retval = -EINVAL;
Miklos Szeredi68181732009-05-07 15:37:36 +0200926 in_inode = in_file->f_path.dentry->d_inode;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800927 out_inode = out_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800929 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800931 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (!max)
934 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
935
936 pos = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (unlikely(pos + count > max)) {
938 retval = -EOVERFLOW;
939 if (pos >= max)
940 goto fput_out;
941 count = max - pos;
942 }
943
Jens Axboed96e6e72007-06-11 12:18:52 +0200944 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +0200945#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +0200946 /*
947 * We need to debate whether we can enable this or not. The
948 * man page documents EAGAIN return for the output at least,
949 * and the application is arguably buggy if it doesn't expect
950 * EAGAIN on a non-blocking file descriptor.
951 */
952 if (in_file->f_flags & O_NONBLOCK)
953 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +0200954#endif
Jens Axboed96e6e72007-06-11 12:18:52 +0200955 retval = do_splice_direct(in_file, ppos, out_file, count, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800958 add_rchar(current, retval);
959 add_wchar(current, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800962 inc_syscr(current);
963 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (*ppos > max)
965 retval = -EOVERFLOW;
966
967fput_out:
968 fput_light(out_file, fput_needed_out);
969fput_in:
970 fput_light(in_file, fput_needed_in);
971out:
972 return retval;
973}
974
Heiko Carstens002c8972009-01-14 14:14:18 +0100975SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 loff_t pos;
978 off_t off;
979 ssize_t ret;
980
981 if (offset) {
982 if (unlikely(get_user(off, offset)))
983 return -EFAULT;
984 pos = off;
985 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
986 if (unlikely(put_user(pos, offset)))
987 return -EFAULT;
988 return ret;
989 }
990
991 return do_sendfile(out_fd, in_fd, NULL, count, 0);
992}
993
Heiko Carstens002c8972009-01-14 14:14:18 +0100994SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 loff_t pos;
997 ssize_t ret;
998
999 if (offset) {
1000 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1001 return -EFAULT;
1002 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1003 if (unlikely(put_user(pos, offset)))
1004 return -EFAULT;
1005 return ret;
1006 }
1007
1008 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1009}