]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/read_write.c
switch signalfd{,4}() to COMPAT_SYSCALL_DEFINE
[mirror_ubuntu-artful-kernel.git] / fs / read_write.c
CommitLineData
1da177e4
LT
1/*
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>
0eeca283 12#include <linux/fsnotify.h>
1da177e4 13#include <linux/security.h>
630d9c47 14#include <linux/export.h>
1da177e4 15#include <linux/syscalls.h>
e28cc715 16#include <linux/pagemap.h>
d6b29d7c 17#include <linux/splice.h>
561c6731 18#include <linux/compat.h>
ee0b3e67 19#include "read_write.h"
1da177e4
LT
20
21#include <asm/uaccess.h>
22#include <asm/unistd.h>
23
4b6f5d20 24const struct file_operations generic_ro_fops = {
1da177e4 25 .llseek = generic_file_llseek,
543ade1f
BP
26 .read = do_sync_read,
27 .aio_read = generic_file_aio_read,
1da177e4 28 .mmap = generic_file_readonly_mmap,
534f2aaa 29 .splice_read = generic_file_splice_read,
1da177e4
LT
30};
31
32EXPORT_SYMBOL(generic_ro_fops);
33
cccb5a1e 34static inline int unsigned_offsets(struct file *file)
4a3956c7 35{
cccb5a1e 36 return file->f_mode & FMODE_UNSIGNED_OFFSET;
4a3956c7
KH
37}
38
ef3d0fd2
AK
39static 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
3a8cff4f 54/**
5760495a 55 * generic_file_llseek_size - generic llseek implementation for regular files
3a8cff4f
CH
56 * @file: file structure to seek on
57 * @offset: file offset to seek to
965c8e59 58 * @whence: type of seek
e8b96eb5
ES
59 * @size: max size of this file in file system
60 * @eof: offset used for SEEK_END position
3a8cff4f 61 *
5760495a 62 * This is a variant of generic_file_llseek that allows passing in a custom
e8b96eb5 63 * maximum file size and a custom EOF position, for e.g. hashed directories
ef3d0fd2
AK
64 *
65 * Synchronization:
5760495a 66 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
ef3d0fd2
AK
67 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
68 * read/writes behave like SEEK_SET against seeks.
3a8cff4f 69 */
9465efc9 70loff_t
965c8e59 71generic_file_llseek_size(struct file *file, loff_t offset, int whence,
e8b96eb5 72 loff_t maxsize, loff_t eof)
1da177e4 73{
1da177e4
LT
74 struct inode *inode = file->f_mapping->host;
75
965c8e59 76 switch (whence) {
3a8cff4f 77 case SEEK_END:
e8b96eb5 78 offset += eof;
3a8cff4f
CH
79 break;
80 case SEEK_CUR:
5b6f1eb9
AK
81 /*
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;
ef3d0fd2
AK
89 /*
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,
5760495a 96 maxsize);
ef3d0fd2
AK
97 spin_unlock(&file->f_lock);
98 return offset;
982d8165
JB
99 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 */
e8b96eb5 104 if (offset >= eof)
982d8165
JB
105 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 */
e8b96eb5 112 if (offset >= eof)
982d8165 113 return -ENXIO;
e8b96eb5 114 offset = eof;
982d8165 115 break;
1da177e4 116 }
3a8cff4f 117
5760495a
AK
118 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
965c8e59 126 * @whence: type of seek
5760495a
AK
127 *
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
965c8e59 130 * @offset and @whence under i_mutex.
5760495a 131 */
965c8e59 132loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
5760495a
AK
133{
134 struct inode *inode = file->f_mapping->host;
135
965c8e59 136 return generic_file_llseek_size(file, offset, whence,
e8b96eb5
ES
137 inode->i_sb->s_maxbytes,
138 i_size_read(inode));
1da177e4 139}
9465efc9 140EXPORT_SYMBOL(generic_file_llseek);
1da177e4 141
ae6afc3f
B
142/**
143 * noop_llseek - No Operation Performed llseek implementation
144 * @file: file structure to seek on
145 * @offset: file offset to seek to
965c8e59 146 * @whence: type of seek
ae6afc3f
B
147 *
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 */
965c8e59 153loff_t noop_llseek(struct file *file, loff_t offset, int whence)
ae6afc3f
B
154{
155 return file->f_pos;
156}
157EXPORT_SYMBOL(noop_llseek);
158
965c8e59 159loff_t no_llseek(struct file *file, loff_t offset, int whence)
1da177e4
LT
160{
161 return -ESPIPE;
162}
163EXPORT_SYMBOL(no_llseek);
164
965c8e59 165loff_t default_llseek(struct file *file, loff_t offset, int whence)
1da177e4 166{
496ad9aa 167 struct inode *inode = file_inode(file);
16abef0e 168 loff_t retval;
1da177e4 169
982d8165 170 mutex_lock(&inode->i_mutex);
965c8e59 171 switch (whence) {
7b8e8924 172 case SEEK_END:
982d8165 173 offset += i_size_read(inode);
1da177e4 174 break;
7b8e8924 175 case SEEK_CUR:
5b6f1eb9
AK
176 if (offset == 0) {
177 retval = file->f_pos;
178 goto out;
179 }
1da177e4 180 offset += file->f_pos;
982d8165
JB
181 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 */
bacb2d81
DC
188 if (offset >= inode->i_size) {
189 retval = -ENXIO;
190 goto out;
191 }
982d8165
JB
192 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 */
bacb2d81
DC
199 if (offset >= inode->i_size) {
200 retval = -ENXIO;
201 goto out;
202 }
982d8165
JB
203 offset = inode->i_size;
204 break;
1da177e4
LT
205 }
206 retval = -EINVAL;
cccb5a1e 207 if (offset >= 0 || unsigned_offsets(file)) {
1da177e4
LT
208 if (offset != file->f_pos) {
209 file->f_pos = offset;
210 file->f_version = 0;
211 }
212 retval = offset;
213 }
5b6f1eb9 214out:
982d8165 215 mutex_unlock(&inode->i_mutex);
1da177e4
LT
216 return retval;
217}
218EXPORT_SYMBOL(default_llseek);
219
965c8e59 220loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
1da177e4
LT
221{
222 loff_t (*fn)(struct file *, loff_t, int);
223
224 fn = no_llseek;
225 if (file->f_mode & FMODE_LSEEK) {
1da177e4
LT
226 if (file->f_op && file->f_op->llseek)
227 fn = file->f_op->llseek;
228 }
965c8e59 229 return fn(file, offset, whence);
1da177e4
LT
230}
231EXPORT_SYMBOL(vfs_llseek);
232
965c8e59 233SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
1da177e4
LT
234{
235 off_t retval;
2903ff01
AV
236 struct fd f = fdget(fd);
237 if (!f.file)
238 return -EBADF;
1da177e4
LT
239
240 retval = -EINVAL;
965c8e59
AM
241 if (whence <= SEEK_MAX) {
242 loff_t res = vfs_llseek(f.file, offset, whence);
1da177e4
LT
243 retval = res;
244 if (res != (loff_t)retval)
245 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
246 }
2903ff01 247 fdput(f);
1da177e4
LT
248 return retval;
249}
250
561c6731
AV
251#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
1da177e4 258#ifdef __ARCH_WANT_SYS_LLSEEK
003d7ab4
HC
259SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
260 unsigned long, offset_low, loff_t __user *, result,
965c8e59 261 unsigned int, whence)
1da177e4
LT
262{
263 int retval;
2903ff01 264 struct fd f = fdget(fd);
1da177e4 265 loff_t offset;
1da177e4 266
2903ff01
AV
267 if (!f.file)
268 return -EBADF;
1da177e4
LT
269
270 retval = -EINVAL;
965c8e59 271 if (whence > SEEK_MAX)
1da177e4
LT
272 goto out_putf;
273
2903ff01 274 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
965c8e59 275 whence);
1da177e4
LT
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:
2903ff01 284 fdput(f);
1da177e4
LT
285 return retval;
286}
287#endif
288
e28cc715
LT
289/*
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 */
1da177e4
LT
294int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
295{
296 struct inode *inode;
297 loff_t pos;
c43e259c 298 int retval = -EINVAL;
1da177e4 299
496ad9aa 300 inode = file_inode(file);
e28cc715 301 if (unlikely((ssize_t) count < 0))
c43e259c 302 return retval;
1da177e4 303 pos = *ppos;
cccb5a1e
AV
304 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))
4a3956c7
KH
311 return retval;
312 }
1da177e4 313
a16877ca 314 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
c43e259c 315 retval = locks_mandatory_area(
e28cc715
LT
316 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
317 inode, file, pos, count);
318 if (retval < 0)
319 return retval;
320 }
c43e259c
JM
321 retval = security_file_permission(file,
322 read_write == READ ? MAY_READ : MAY_WRITE);
323 if (retval)
324 return retval;
e28cc715 325 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
1da177e4
LT
326}
327
63e68809
BL
328static 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
1da177e4
LT
338ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
339{
027445c3 340 struct iovec iov = { .iov_base = buf, .iov_len = len };
1da177e4
LT
341 struct kiocb kiocb;
342 ssize_t ret;
343
344 init_sync_kiocb(&kiocb, filp);
345 kiocb.ki_pos = *ppos;
027445c3 346 kiocb.ki_left = len;
61964eba 347 kiocb.ki_nbytes = len;
027445c3
BP
348
349 for (;;) {
350 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
351 if (ret != -EIOCBRETRY)
352 break;
63e68809 353 wait_on_retry_sync_kiocb(&kiocb);
027445c3 354 }
63e68809 355
1da177e4
LT
356 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);
e28cc715
LT
376 if (ret >= 0) {
377 count = ret;
c43e259c
JM
378 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) {
2a12a9d7 383 fsnotify_access(file);
c43e259c 384 add_rchar(current, ret);
1da177e4 385 }
c43e259c 386 inc_syscr(current);
1da177e4
LT
387 }
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{
027445c3 396 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
1da177e4
LT
397 struct kiocb kiocb;
398 ssize_t ret;
399
400 init_sync_kiocb(&kiocb, filp);
401 kiocb.ki_pos = *ppos;
027445c3 402 kiocb.ki_left = len;
61964eba 403 kiocb.ki_nbytes = len;
027445c3
BP
404
405 for (;;) {
406 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
407 if (ret != -EIOCBRETRY)
408 break;
63e68809 409 wait_on_retry_sync_kiocb(&kiocb);
027445c3 410 }
63e68809 411
1da177e4
LT
412 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);
e28cc715
LT
432 if (ret >= 0) {
433 count = ret;
c43e259c
JM
434 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) {
2a12a9d7 439 fsnotify_modify(file);
c43e259c 440 add_wchar(current, ret);
1da177e4 441 }
c43e259c 442 inc_syscw(current);
1da177e4
LT
443 }
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
3cdad428 460SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
1da177e4 461{
2903ff01 462 struct fd f = fdget(fd);
1da177e4 463 ssize_t ret = -EBADF;
1da177e4 464
2903ff01
AV
465 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);
1da177e4 470 }
1da177e4
LT
471 return ret;
472}
1da177e4 473
3cdad428
HC
474SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
475 size_t, count)
1da177e4 476{
2903ff01 477 struct fd f = fdget(fd);
1da177e4 478 ssize_t ret = -EBADF;
1da177e4 479
2903ff01
AV
480 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);
1da177e4
LT
485 }
486
487 return ret;
488}
489
4a0fd5bf
AV
490SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
491 size_t, count, loff_t, pos)
1da177e4 492{
2903ff01 493 struct fd f;
1da177e4 494 ssize_t ret = -EBADF;
1da177e4
LT
495
496 if (pos < 0)
497 return -EINVAL;
498
2903ff01
AV
499 f = fdget(fd);
500 if (f.file) {
1da177e4 501 ret = -ESPIPE;
2903ff01
AV
502 if (f.file->f_mode & FMODE_PREAD)
503 ret = vfs_read(f.file, buf, count, &pos);
504 fdput(f);
1da177e4
LT
505 }
506
507 return ret;
508}
509
4a0fd5bf
AV
510SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
511 size_t, count, loff_t, pos)
1da177e4 512{
2903ff01 513 struct fd f;
1da177e4 514 ssize_t ret = -EBADF;
1da177e4
LT
515
516 if (pos < 0)
517 return -EINVAL;
518
2903ff01
AV
519 f = fdget(fd);
520 if (f.file) {
1da177e4 521 ret = -ESPIPE;
2903ff01
AV
522 if (f.file->f_mode & FMODE_PWRITE)
523 ret = vfs_write(f.file, buf, count, &pos);
524 fdput(f);
1da177e4
LT
525 }
526
527 return ret;
528}
529
530/*
531 * Reduce an iovec's length in-place. Return the resulting number of segments
532 */
533unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
534{
535 unsigned long seg = 0;
536 size_t len = 0;
537
538 while (seg < nr_segs) {
539 seg++;
540 if (len + iov->iov_len >= to) {
541 iov->iov_len = to - len;
542 break;
543 }
544 len += iov->iov_len;
545 iov++;
546 }
547 return seg;
548}
19295529 549EXPORT_SYMBOL(iov_shorten);
1da177e4 550
ee0b3e67
BP
551ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
552 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
553{
554 struct kiocb kiocb;
555 ssize_t ret;
556
557 init_sync_kiocb(&kiocb, filp);
558 kiocb.ki_pos = *ppos;
559 kiocb.ki_left = len;
560 kiocb.ki_nbytes = len;
561
562 for (;;) {
563 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
564 if (ret != -EIOCBRETRY)
565 break;
566 wait_on_retry_sync_kiocb(&kiocb);
567 }
568
569 if (ret == -EIOCBQUEUED)
570 ret = wait_on_sync_kiocb(&kiocb);
571 *ppos = kiocb.ki_pos;
572 return ret;
573}
574
575/* Do it by hand, with file-ops */
576ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
577 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
578{
579 struct iovec *vector = iov;
580 ssize_t ret = 0;
581
582 while (nr_segs > 0) {
583 void __user *base;
584 size_t len;
585 ssize_t nr;
586
587 base = vector->iov_base;
588 len = vector->iov_len;
589 vector++;
590 nr_segs--;
591
592 nr = fn(filp, base, len, ppos);
593
594 if (nr < 0) {
595 if (!ret)
596 ret = nr;
597 break;
598 }
599 ret += nr;
600 if (nr != len)
601 break;
602 }
603
604 return ret;
605}
606
1da177e4
LT
607/* A write operation does a read from user space and vice versa */
608#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
609
eed4e51f
BP
610ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
611 unsigned long nr_segs, unsigned long fast_segs,
612 struct iovec *fast_pointer,
ac34ebb3 613 struct iovec **ret_pointer)
435f49a5 614{
eed4e51f 615 unsigned long seg;
435f49a5 616 ssize_t ret;
eed4e51f
BP
617 struct iovec *iov = fast_pointer;
618
435f49a5
LT
619 /*
620 * SuS says "The readv() function *may* fail if the iovcnt argument
621 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
622 * traditionally returned zero for zero segments, so...
623 */
eed4e51f
BP
624 if (nr_segs == 0) {
625 ret = 0;
435f49a5 626 goto out;
eed4e51f
BP
627 }
628
435f49a5
LT
629 /*
630 * First get the "struct iovec" from user memory and
631 * verify all the pointers
632 */
eed4e51f
BP
633 if (nr_segs > UIO_MAXIOV) {
634 ret = -EINVAL;
435f49a5 635 goto out;
eed4e51f
BP
636 }
637 if (nr_segs > fast_segs) {
435f49a5 638 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
eed4e51f
BP
639 if (iov == NULL) {
640 ret = -ENOMEM;
435f49a5 641 goto out;
eed4e51f 642 }
435f49a5 643 }
eed4e51f
BP
644 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
645 ret = -EFAULT;
435f49a5 646 goto out;
eed4e51f
BP
647 }
648
435f49a5 649 /*
eed4e51f
BP
650 * According to the Single Unix Specification we should return EINVAL
651 * if an element length is < 0 when cast to ssize_t or if the
652 * total length would overflow the ssize_t return value of the
653 * system call.
435f49a5
LT
654 *
655 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
656 * overflow case.
657 */
eed4e51f 658 ret = 0;
435f49a5
LT
659 for (seg = 0; seg < nr_segs; seg++) {
660 void __user *buf = iov[seg].iov_base;
661 ssize_t len = (ssize_t)iov[seg].iov_len;
eed4e51f
BP
662
663 /* see if we we're about to use an invalid len or if
664 * it's about to overflow ssize_t */
435f49a5 665 if (len < 0) {
eed4e51f 666 ret = -EINVAL;
435f49a5 667 goto out;
eed4e51f 668 }
ac34ebb3 669 if (type >= 0
fcf63409 670 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
eed4e51f 671 ret = -EFAULT;
435f49a5
LT
672 goto out;
673 }
674 if (len > MAX_RW_COUNT - ret) {
675 len = MAX_RW_COUNT - ret;
676 iov[seg].iov_len = len;
eed4e51f 677 }
eed4e51f 678 ret += len;
435f49a5 679 }
eed4e51f
BP
680out:
681 *ret_pointer = iov;
682 return ret;
683}
684
1da177e4
LT
685static ssize_t do_readv_writev(int type, struct file *file,
686 const struct iovec __user * uvector,
687 unsigned long nr_segs, loff_t *pos)
688{
1da177e4
LT
689 size_t tot_len;
690 struct iovec iovstack[UIO_FASTIOV];
ee0b3e67 691 struct iovec *iov = iovstack;
1da177e4 692 ssize_t ret;
1da177e4
LT
693 io_fn_t fn;
694 iov_fn_t fnv;
695
eed4e51f
BP
696 if (!file->f_op) {
697 ret = -EINVAL;
1da177e4 698 goto out;
1da177e4 699 }
1da177e4 700
eed4e51f 701 ret = rw_copy_check_uvector(type, uvector, nr_segs,
ac34ebb3 702 ARRAY_SIZE(iovstack), iovstack, &iov);
eed4e51f 703 if (ret <= 0)
1da177e4 704 goto out;
1da177e4 705
eed4e51f 706 tot_len = ret;
1da177e4 707 ret = rw_verify_area(type, file, pos, tot_len);
e28cc715 708 if (ret < 0)
411b67b4 709 goto out;
1da177e4
LT
710
711 fnv = NULL;
712 if (type == READ) {
713 fn = file->f_op->read;
ee0b3e67 714 fnv = file->f_op->aio_read;
1da177e4
LT
715 } else {
716 fn = (io_fn_t)file->f_op->write;
ee0b3e67 717 fnv = file->f_op->aio_write;
1da177e4
LT
718 }
719
ee0b3e67
BP
720 if (fnv)
721 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
722 pos, fnv);
723 else
724 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
1da177e4 725
1da177e4
LT
726out:
727 if (iov != iovstack)
728 kfree(iov);
0eeca283
RL
729 if ((ret + (type == READ)) > 0) {
730 if (type == READ)
2a12a9d7 731 fsnotify_access(file);
0eeca283 732 else
2a12a9d7 733 fsnotify_modify(file);
0eeca283 734 }
1da177e4 735 return ret;
1da177e4
LT
736}
737
738ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
739 unsigned long vlen, loff_t *pos)
740{
741 if (!(file->f_mode & FMODE_READ))
742 return -EBADF;
ee0b3e67 743 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
1da177e4
LT
744 return -EINVAL;
745
746 return do_readv_writev(READ, file, vec, vlen, pos);
747}
748
749EXPORT_SYMBOL(vfs_readv);
750
751ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
752 unsigned long vlen, loff_t *pos)
753{
754 if (!(file->f_mode & FMODE_WRITE))
755 return -EBADF;
ee0b3e67 756 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
1da177e4
LT
757 return -EINVAL;
758
759 return do_readv_writev(WRITE, file, vec, vlen, pos);
760}
761
762EXPORT_SYMBOL(vfs_writev);
763
3cdad428
HC
764SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
765 unsigned long, vlen)
1da177e4 766{
2903ff01 767 struct fd f = fdget(fd);
1da177e4 768 ssize_t ret = -EBADF;
1da177e4 769
2903ff01
AV
770 if (f.file) {
771 loff_t pos = file_pos_read(f.file);
772 ret = vfs_readv(f.file, vec, vlen, &pos);
773 file_pos_write(f.file, pos);
774 fdput(f);
1da177e4
LT
775 }
776
777 if (ret > 0)
4b98d11b
AD
778 add_rchar(current, ret);
779 inc_syscr(current);
1da177e4
LT
780 return ret;
781}
782
3cdad428
HC
783SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
784 unsigned long, vlen)
1da177e4 785{
2903ff01 786 struct fd f = fdget(fd);
1da177e4 787 ssize_t ret = -EBADF;
1da177e4 788
2903ff01
AV
789 if (f.file) {
790 loff_t pos = file_pos_read(f.file);
791 ret = vfs_writev(f.file, vec, vlen, &pos);
792 file_pos_write(f.file, pos);
793 fdput(f);
1da177e4
LT
794 }
795
796 if (ret > 0)
4b98d11b
AD
797 add_wchar(current, ret);
798 inc_syscw(current);
1da177e4
LT
799 return ret;
800}
801
601cc11d
LT
802static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
803{
804#define HALF_LONG_BITS (BITS_PER_LONG / 2)
805 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
806}
807
f3554f4b 808SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
601cc11d 809 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
f3554f4b 810{
601cc11d 811 loff_t pos = pos_from_hilo(pos_h, pos_l);
2903ff01 812 struct fd f;
f3554f4b 813 ssize_t ret = -EBADF;
f3554f4b
GH
814
815 if (pos < 0)
816 return -EINVAL;
817
2903ff01
AV
818 f = fdget(fd);
819 if (f.file) {
f3554f4b 820 ret = -ESPIPE;
2903ff01
AV
821 if (f.file->f_mode & FMODE_PREAD)
822 ret = vfs_readv(f.file, vec, vlen, &pos);
823 fdput(f);
f3554f4b
GH
824 }
825
826 if (ret > 0)
827 add_rchar(current, ret);
828 inc_syscr(current);
829 return ret;
830}
831
832SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
601cc11d 833 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
f3554f4b 834{
601cc11d 835 loff_t pos = pos_from_hilo(pos_h, pos_l);
2903ff01 836 struct fd f;
f3554f4b 837 ssize_t ret = -EBADF;
f3554f4b
GH
838
839 if (pos < 0)
840 return -EINVAL;
841
2903ff01
AV
842 f = fdget(fd);
843 if (f.file) {
f3554f4b 844 ret = -ESPIPE;
2903ff01
AV
845 if (f.file->f_mode & FMODE_PWRITE)
846 ret = vfs_writev(f.file, vec, vlen, &pos);
847 fdput(f);
f3554f4b
GH
848 }
849
850 if (ret > 0)
851 add_wchar(current, ret);
852 inc_syscw(current);
853 return ret;
854}
855
8f9c0119
CM
856ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, size_t count,
857 loff_t max)
1da177e4 858{
2903ff01
AV
859 struct fd in, out;
860 struct inode *in_inode, *out_inode;
1da177e4
LT
861 loff_t pos;
862 ssize_t retval;
2903ff01 863 int fl;
1da177e4
LT
864
865 /*
866 * Get input file, and verify that it is ok..
867 */
868 retval = -EBADF;
2903ff01
AV
869 in = fdget(in_fd);
870 if (!in.file)
1da177e4 871 goto out;
2903ff01 872 if (!(in.file->f_mode & FMODE_READ))
1da177e4 873 goto fput_in;
1da177e4
LT
874 retval = -ESPIPE;
875 if (!ppos)
2903ff01 876 ppos = &in.file->f_pos;
1da177e4 877 else
2903ff01 878 if (!(in.file->f_mode & FMODE_PREAD))
1da177e4 879 goto fput_in;
2903ff01 880 retval = rw_verify_area(READ, in.file, ppos, count);
e28cc715 881 if (retval < 0)
1da177e4 882 goto fput_in;
e28cc715 883 count = retval;
1da177e4 884
1da177e4
LT
885 /*
886 * Get output file, and verify that it is ok..
887 */
888 retval = -EBADF;
2903ff01
AV
889 out = fdget(out_fd);
890 if (!out.file)
1da177e4 891 goto fput_in;
2903ff01 892 if (!(out.file->f_mode & FMODE_WRITE))
1da177e4
LT
893 goto fput_out;
894 retval = -EINVAL;
496ad9aa
AV
895 in_inode = file_inode(in.file);
896 out_inode = file_inode(out.file);
2903ff01 897 retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count);
e28cc715 898 if (retval < 0)
1da177e4 899 goto fput_out;
e28cc715 900 count = retval;
1da177e4 901
1da177e4
LT
902 if (!max)
903 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
904
905 pos = *ppos;
1da177e4
LT
906 if (unlikely(pos + count > max)) {
907 retval = -EOVERFLOW;
908 if (pos >= max)
909 goto fput_out;
910 count = max - pos;
911 }
912
d96e6e71 913 fl = 0;
534f2aaa 914#if 0
d96e6e71
JA
915 /*
916 * We need to debate whether we can enable this or not. The
917 * man page documents EAGAIN return for the output at least,
918 * and the application is arguably buggy if it doesn't expect
919 * EAGAIN on a non-blocking file descriptor.
920 */
2903ff01 921 if (in.file->f_flags & O_NONBLOCK)
d96e6e71 922 fl = SPLICE_F_NONBLOCK;
534f2aaa 923#endif
2903ff01 924 retval = do_splice_direct(in.file, ppos, out.file, count, fl);
1da177e4
LT
925
926 if (retval > 0) {
4b98d11b
AD
927 add_rchar(current, retval);
928 add_wchar(current, retval);
a68c2f12
SW
929 fsnotify_access(in.file);
930 fsnotify_modify(out.file);
1da177e4 931 }
1da177e4 932
4b98d11b
AD
933 inc_syscr(current);
934 inc_syscw(current);
1da177e4
LT
935 if (*ppos > max)
936 retval = -EOVERFLOW;
937
938fput_out:
2903ff01 939 fdput(out);
1da177e4 940fput_in:
2903ff01 941 fdput(in);
1da177e4
LT
942out:
943 return retval;
944}
945
002c8976 946SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
1da177e4
LT
947{
948 loff_t pos;
949 off_t off;
950 ssize_t ret;
951
952 if (offset) {
953 if (unlikely(get_user(off, offset)))
954 return -EFAULT;
955 pos = off;
956 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
957 if (unlikely(put_user(pos, offset)))
958 return -EFAULT;
959 return ret;
960 }
961
962 return do_sendfile(out_fd, in_fd, NULL, count, 0);
963}
964
002c8976 965SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
1da177e4
LT
966{
967 loff_t pos;
968 ssize_t ret;
969
970 if (offset) {
971 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
972 return -EFAULT;
973 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
974 if (unlikely(put_user(pos, offset)))
975 return -EFAULT;
976 return ret;
977 }
978
979 return do_sendfile(out_fd, in_fd, NULL, count, 0);
980}