sockfd_lookup_light(): switch to fdget^W^Waway from fget_light
[linux-2.6-block.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>
a27bb332 12#include <linux/aio.h>
0eeca283 13#include <linux/fsnotify.h>
1da177e4 14#include <linux/security.h>
630d9c47 15#include <linux/export.h>
1da177e4 16#include <linux/syscalls.h>
e28cc715 17#include <linux/pagemap.h>
d6b29d7c 18#include <linux/splice.h>
561c6731 19#include <linux/compat.h>
06ae43f3 20#include "internal.h"
1da177e4
LT
21
22#include <asm/uaccess.h>
23#include <asm/unistd.h>
24
c0bd14af
AV
25typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
26typedef ssize_t (*iov_fn_t)(struct kiocb *, const struct iovec *,
27 unsigned long, loff_t);
28
4b6f5d20 29const struct file_operations generic_ro_fops = {
1da177e4 30 .llseek = generic_file_llseek,
543ade1f
BP
31 .read = do_sync_read,
32 .aio_read = generic_file_aio_read,
1da177e4 33 .mmap = generic_file_readonly_mmap,
534f2aaa 34 .splice_read = generic_file_splice_read,
1da177e4
LT
35};
36
37EXPORT_SYMBOL(generic_ro_fops);
38
cccb5a1e 39static inline int unsigned_offsets(struct file *file)
4a3956c7 40{
cccb5a1e 41 return file->f_mode & FMODE_UNSIGNED_OFFSET;
4a3956c7
KH
42}
43
46a1c2c7
JL
44/**
45 * vfs_setpos - update the file offset for lseek
46 * @file: file structure in question
47 * @offset: file offset to seek to
48 * @maxsize: maximum file size
49 *
50 * This is a low-level filesystem helper for updating the file offset to
51 * the value specified by @offset if the given offset is valid and it is
52 * not equal to the current file offset.
53 *
54 * Return the specified offset on success and -EINVAL on invalid offset.
55 */
56loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
ef3d0fd2
AK
57{
58 if (offset < 0 && !unsigned_offsets(file))
59 return -EINVAL;
60 if (offset > maxsize)
61 return -EINVAL;
62
63 if (offset != file->f_pos) {
64 file->f_pos = offset;
65 file->f_version = 0;
66 }
67 return offset;
68}
46a1c2c7 69EXPORT_SYMBOL(vfs_setpos);
ef3d0fd2 70
3a8cff4f 71/**
5760495a 72 * generic_file_llseek_size - generic llseek implementation for regular files
3a8cff4f
CH
73 * @file: file structure to seek on
74 * @offset: file offset to seek to
965c8e59 75 * @whence: type of seek
e8b96eb5
ES
76 * @size: max size of this file in file system
77 * @eof: offset used for SEEK_END position
3a8cff4f 78 *
5760495a 79 * This is a variant of generic_file_llseek that allows passing in a custom
e8b96eb5 80 * maximum file size and a custom EOF position, for e.g. hashed directories
ef3d0fd2
AK
81 *
82 * Synchronization:
5760495a 83 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
ef3d0fd2
AK
84 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
85 * read/writes behave like SEEK_SET against seeks.
3a8cff4f 86 */
9465efc9 87loff_t
965c8e59 88generic_file_llseek_size(struct file *file, loff_t offset, int whence,
e8b96eb5 89 loff_t maxsize, loff_t eof)
1da177e4 90{
965c8e59 91 switch (whence) {
3a8cff4f 92 case SEEK_END:
e8b96eb5 93 offset += eof;
3a8cff4f
CH
94 break;
95 case SEEK_CUR:
5b6f1eb9
AK
96 /*
97 * Here we special-case the lseek(fd, 0, SEEK_CUR)
98 * position-querying operation. Avoid rewriting the "same"
99 * f_pos value back to the file because a concurrent read(),
100 * write() or lseek() might have altered it
101 */
102 if (offset == 0)
103 return file->f_pos;
ef3d0fd2
AK
104 /*
105 * f_lock protects against read/modify/write race with other
106 * SEEK_CURs. Note that parallel writes and reads behave
107 * like SEEK_SET.
108 */
109 spin_lock(&file->f_lock);
46a1c2c7 110 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
ef3d0fd2
AK
111 spin_unlock(&file->f_lock);
112 return offset;
982d8165
JB
113 case SEEK_DATA:
114 /*
115 * In the generic case the entire file is data, so as long as
116 * offset isn't at the end of the file then the offset is data.
117 */
e8b96eb5 118 if (offset >= eof)
982d8165
JB
119 return -ENXIO;
120 break;
121 case SEEK_HOLE:
122 /*
123 * There is a virtual hole at the end of the file, so as long as
124 * offset isn't i_size or larger, return i_size.
125 */
e8b96eb5 126 if (offset >= eof)
982d8165 127 return -ENXIO;
e8b96eb5 128 offset = eof;
982d8165 129 break;
1da177e4 130 }
3a8cff4f 131
46a1c2c7 132 return vfs_setpos(file, offset, maxsize);
5760495a
AK
133}
134EXPORT_SYMBOL(generic_file_llseek_size);
135
136/**
137 * generic_file_llseek - generic llseek implementation for regular files
138 * @file: file structure to seek on
139 * @offset: file offset to seek to
965c8e59 140 * @whence: type of seek
5760495a
AK
141 *
142 * This is a generic implemenation of ->llseek useable for all normal local
143 * filesystems. It just updates the file offset to the value specified by
546ae2d2 144 * @offset and @whence.
5760495a 145 */
965c8e59 146loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
5760495a
AK
147{
148 struct inode *inode = file->f_mapping->host;
149
965c8e59 150 return generic_file_llseek_size(file, offset, whence,
e8b96eb5
ES
151 inode->i_sb->s_maxbytes,
152 i_size_read(inode));
1da177e4 153}
9465efc9 154EXPORT_SYMBOL(generic_file_llseek);
1da177e4 155
1bf9d14d
AV
156/**
157 * fixed_size_llseek - llseek implementation for fixed-sized devices
158 * @file: file structure to seek on
159 * @offset: file offset to seek to
160 * @whence: type of seek
161 * @size: size of the file
162 *
163 */
164loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
165{
166 switch (whence) {
167 case SEEK_SET: case SEEK_CUR: case SEEK_END:
168 return generic_file_llseek_size(file, offset, whence,
169 size, size);
170 default:
171 return -EINVAL;
172 }
173}
174EXPORT_SYMBOL(fixed_size_llseek);
175
ae6afc3f
B
176/**
177 * noop_llseek - No Operation Performed llseek implementation
178 * @file: file structure to seek on
179 * @offset: file offset to seek to
965c8e59 180 * @whence: type of seek
ae6afc3f
B
181 *
182 * This is an implementation of ->llseek useable for the rare special case when
183 * userspace expects the seek to succeed but the (device) file is actually not
184 * able to perform the seek. In this case you use noop_llseek() instead of
185 * falling back to the default implementation of ->llseek.
186 */
965c8e59 187loff_t noop_llseek(struct file *file, loff_t offset, int whence)
ae6afc3f
B
188{
189 return file->f_pos;
190}
191EXPORT_SYMBOL(noop_llseek);
192
965c8e59 193loff_t no_llseek(struct file *file, loff_t offset, int whence)
1da177e4
LT
194{
195 return -ESPIPE;
196}
197EXPORT_SYMBOL(no_llseek);
198
965c8e59 199loff_t default_llseek(struct file *file, loff_t offset, int whence)
1da177e4 200{
496ad9aa 201 struct inode *inode = file_inode(file);
16abef0e 202 loff_t retval;
1da177e4 203
982d8165 204 mutex_lock(&inode->i_mutex);
965c8e59 205 switch (whence) {
7b8e8924 206 case SEEK_END:
982d8165 207 offset += i_size_read(inode);
1da177e4 208 break;
7b8e8924 209 case SEEK_CUR:
5b6f1eb9
AK
210 if (offset == 0) {
211 retval = file->f_pos;
212 goto out;
213 }
1da177e4 214 offset += file->f_pos;
982d8165
JB
215 break;
216 case SEEK_DATA:
217 /*
218 * In the generic case the entire file is data, so as
219 * long as offset isn't at the end of the file then the
220 * offset is data.
221 */
bacb2d81
DC
222 if (offset >= inode->i_size) {
223 retval = -ENXIO;
224 goto out;
225 }
982d8165
JB
226 break;
227 case SEEK_HOLE:
228 /*
229 * There is a virtual hole at the end of the file, so
230 * as long as offset isn't i_size or larger, return
231 * i_size.
232 */
bacb2d81
DC
233 if (offset >= inode->i_size) {
234 retval = -ENXIO;
235 goto out;
236 }
982d8165
JB
237 offset = inode->i_size;
238 break;
1da177e4
LT
239 }
240 retval = -EINVAL;
cccb5a1e 241 if (offset >= 0 || unsigned_offsets(file)) {
1da177e4
LT
242 if (offset != file->f_pos) {
243 file->f_pos = offset;
244 file->f_version = 0;
245 }
246 retval = offset;
247 }
5b6f1eb9 248out:
982d8165 249 mutex_unlock(&inode->i_mutex);
1da177e4
LT
250 return retval;
251}
252EXPORT_SYMBOL(default_llseek);
253
965c8e59 254loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
1da177e4
LT
255{
256 loff_t (*fn)(struct file *, loff_t, int);
257
258 fn = no_llseek;
259 if (file->f_mode & FMODE_LSEEK) {
72c2d531 260 if (file->f_op->llseek)
1da177e4
LT
261 fn = file->f_op->llseek;
262 }
965c8e59 263 return fn(file, offset, whence);
1da177e4
LT
264}
265EXPORT_SYMBOL(vfs_llseek);
266
9c225f26
LT
267/*
268 * We only lock f_pos if we have threads or if the file might be
269 * shared with another process. In both cases we'll have an elevated
270 * file count (done either by fdget() or by fork()).
271 */
272static inline struct fd fdget_pos(int fd)
273{
274 struct fd f = fdget(fd);
275 struct file *file = f.file;
276
277 if (file && (file->f_mode & FMODE_ATOMIC_POS)) {
278 if (file_count(file) > 1) {
279 f.flags |= FDPUT_POS_UNLOCK;
280 mutex_lock(&file->f_pos_lock);
281 }
282 }
283 return f;
284}
285
286static inline void fdput_pos(struct fd f)
287{
288 if (f.flags & FDPUT_POS_UNLOCK)
289 mutex_unlock(&f.file->f_pos_lock);
290 fdput(f);
291}
292
965c8e59 293SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
1da177e4
LT
294{
295 off_t retval;
9c225f26 296 struct fd f = fdget_pos(fd);
2903ff01
AV
297 if (!f.file)
298 return -EBADF;
1da177e4
LT
299
300 retval = -EINVAL;
965c8e59
AM
301 if (whence <= SEEK_MAX) {
302 loff_t res = vfs_llseek(f.file, offset, whence);
1da177e4
LT
303 retval = res;
304 if (res != (loff_t)retval)
305 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
306 }
9c225f26 307 fdput_pos(f);
1da177e4
LT
308 return retval;
309}
310
561c6731
AV
311#ifdef CONFIG_COMPAT
312COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
313{
314 return sys_lseek(fd, offset, whence);
315}
316#endif
317
1da177e4 318#ifdef __ARCH_WANT_SYS_LLSEEK
003d7ab4
HC
319SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
320 unsigned long, offset_low, loff_t __user *, result,
965c8e59 321 unsigned int, whence)
1da177e4
LT
322{
323 int retval;
2903ff01 324 struct fd f = fdget(fd);
1da177e4 325 loff_t offset;
1da177e4 326
2903ff01
AV
327 if (!f.file)
328 return -EBADF;
1da177e4
LT
329
330 retval = -EINVAL;
965c8e59 331 if (whence > SEEK_MAX)
1da177e4
LT
332 goto out_putf;
333
2903ff01 334 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
965c8e59 335 whence);
1da177e4
LT
336
337 retval = (int)offset;
338 if (offset >= 0) {
339 retval = -EFAULT;
340 if (!copy_to_user(result, &offset, sizeof(offset)))
341 retval = 0;
342 }
343out_putf:
2903ff01 344 fdput(f);
1da177e4
LT
345 return retval;
346}
347#endif
348
e28cc715
LT
349/*
350 * rw_verify_area doesn't like huge counts. We limit
351 * them to something that fits in "int" so that others
352 * won't have to do range checks all the time.
353 */
68d70d03 354int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
1da177e4
LT
355{
356 struct inode *inode;
357 loff_t pos;
c43e259c 358 int retval = -EINVAL;
1da177e4 359
496ad9aa 360 inode = file_inode(file);
e28cc715 361 if (unlikely((ssize_t) count < 0))
c43e259c 362 return retval;
1da177e4 363 pos = *ppos;
cccb5a1e
AV
364 if (unlikely(pos < 0)) {
365 if (!unsigned_offsets(file))
366 return retval;
367 if (count >= -pos) /* both values are in 0..LLONG_MAX */
368 return -EOVERFLOW;
369 } else if (unlikely((loff_t) (pos + count) < 0)) {
370 if (!unsigned_offsets(file))
4a3956c7
KH
371 return retval;
372 }
1da177e4 373
a16877ca 374 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
c43e259c 375 retval = locks_mandatory_area(
e28cc715
LT
376 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
377 inode, file, pos, count);
378 if (retval < 0)
379 return retval;
380 }
c43e259c
JM
381 retval = security_file_permission(file,
382 read_write == READ ? MAY_READ : MAY_WRITE);
383 if (retval)
384 return retval;
e28cc715 385 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
1da177e4
LT
386}
387
388ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
389{
027445c3 390 struct iovec iov = { .iov_base = buf, .iov_len = len };
1da177e4
LT
391 struct kiocb kiocb;
392 ssize_t ret;
393
394 init_sync_kiocb(&kiocb, filp);
395 kiocb.ki_pos = *ppos;
61964eba 396 kiocb.ki_nbytes = len;
027445c3 397
41003a7b 398 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
1da177e4
LT
399 if (-EIOCBQUEUED == ret)
400 ret = wait_on_sync_kiocb(&kiocb);
401 *ppos = kiocb.ki_pos;
402 return ret;
403}
404
405EXPORT_SYMBOL(do_sync_read);
406
407ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
408{
409 ssize_t ret;
410
411 if (!(file->f_mode & FMODE_READ))
412 return -EBADF;
72c2d531 413 if (!file->f_op->read && !file->f_op->aio_read)
1da177e4
LT
414 return -EINVAL;
415 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
416 return -EFAULT;
417
418 ret = rw_verify_area(READ, file, pos, count);
e28cc715
LT
419 if (ret >= 0) {
420 count = ret;
c43e259c
JM
421 if (file->f_op->read)
422 ret = file->f_op->read(file, buf, count, pos);
423 else
424 ret = do_sync_read(file, buf, count, pos);
425 if (ret > 0) {
2a12a9d7 426 fsnotify_access(file);
c43e259c 427 add_rchar(current, ret);
1da177e4 428 }
c43e259c 429 inc_syscr(current);
1da177e4
LT
430 }
431
432 return ret;
433}
434
435EXPORT_SYMBOL(vfs_read);
436
437ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
438{
027445c3 439 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
1da177e4
LT
440 struct kiocb kiocb;
441 ssize_t ret;
442
443 init_sync_kiocb(&kiocb, filp);
444 kiocb.ki_pos = *ppos;
61964eba 445 kiocb.ki_nbytes = len;
027445c3 446
41003a7b 447 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
1da177e4
LT
448 if (-EIOCBQUEUED == ret)
449 ret = wait_on_sync_kiocb(&kiocb);
450 *ppos = kiocb.ki_pos;
451 return ret;
452}
453
454EXPORT_SYMBOL(do_sync_write);
455
06ae43f3
AV
456ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
457{
458 mm_segment_t old_fs;
459 const char __user *p;
460 ssize_t ret;
461
72c2d531 462 if (!file->f_op->write && !file->f_op->aio_write)
3e84f48e
AV
463 return -EINVAL;
464
06ae43f3
AV
465 old_fs = get_fs();
466 set_fs(get_ds());
467 p = (__force const char __user *)buf;
468 if (count > MAX_RW_COUNT)
469 count = MAX_RW_COUNT;
470 if (file->f_op->write)
471 ret = file->f_op->write(file, p, count, pos);
472 else
473 ret = do_sync_write(file, p, count, pos);
474 set_fs(old_fs);
475 if (ret > 0) {
476 fsnotify_modify(file);
477 add_wchar(current, ret);
478 }
479 inc_syscw(current);
480 return ret;
481}
482
1da177e4
LT
483ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
484{
485 ssize_t ret;
486
487 if (!(file->f_mode & FMODE_WRITE))
488 return -EBADF;
72c2d531 489 if (!file->f_op->write && !file->f_op->aio_write)
1da177e4
LT
490 return -EINVAL;
491 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
492 return -EFAULT;
493
494 ret = rw_verify_area(WRITE, file, pos, count);
e28cc715
LT
495 if (ret >= 0) {
496 count = ret;
03d95eb2 497 file_start_write(file);
c43e259c
JM
498 if (file->f_op->write)
499 ret = file->f_op->write(file, buf, count, pos);
500 else
501 ret = do_sync_write(file, buf, count, pos);
502 if (ret > 0) {
2a12a9d7 503 fsnotify_modify(file);
c43e259c 504 add_wchar(current, ret);
1da177e4 505 }
c43e259c 506 inc_syscw(current);
03d95eb2 507 file_end_write(file);
1da177e4
LT
508 }
509
510 return ret;
511}
512
513EXPORT_SYMBOL(vfs_write);
514
515static inline loff_t file_pos_read(struct file *file)
516{
517 return file->f_pos;
518}
519
520static inline void file_pos_write(struct file *file, loff_t pos)
521{
522 file->f_pos = pos;
523}
524
3cdad428 525SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
1da177e4 526{
9c225f26 527 struct fd f = fdget_pos(fd);
1da177e4 528 ssize_t ret = -EBADF;
1da177e4 529
2903ff01
AV
530 if (f.file) {
531 loff_t pos = file_pos_read(f.file);
532 ret = vfs_read(f.file, buf, count, &pos);
5faf153e
AV
533 if (ret >= 0)
534 file_pos_write(f.file, pos);
9c225f26 535 fdput_pos(f);
1da177e4 536 }
1da177e4
LT
537 return ret;
538}
1da177e4 539
3cdad428
HC
540SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
541 size_t, count)
1da177e4 542{
9c225f26 543 struct fd f = fdget_pos(fd);
1da177e4 544 ssize_t ret = -EBADF;
1da177e4 545
2903ff01
AV
546 if (f.file) {
547 loff_t pos = file_pos_read(f.file);
548 ret = vfs_write(f.file, buf, count, &pos);
5faf153e
AV
549 if (ret >= 0)
550 file_pos_write(f.file, pos);
9c225f26 551 fdput_pos(f);
1da177e4
LT
552 }
553
554 return ret;
555}
556
4a0fd5bf
AV
557SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
558 size_t, count, loff_t, pos)
1da177e4 559{
2903ff01 560 struct fd f;
1da177e4 561 ssize_t ret = -EBADF;
1da177e4
LT
562
563 if (pos < 0)
564 return -EINVAL;
565
2903ff01
AV
566 f = fdget(fd);
567 if (f.file) {
1da177e4 568 ret = -ESPIPE;
2903ff01
AV
569 if (f.file->f_mode & FMODE_PREAD)
570 ret = vfs_read(f.file, buf, count, &pos);
571 fdput(f);
1da177e4
LT
572 }
573
574 return ret;
575}
576
4a0fd5bf
AV
577SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
578 size_t, count, loff_t, pos)
1da177e4 579{
2903ff01 580 struct fd f;
1da177e4 581 ssize_t ret = -EBADF;
1da177e4
LT
582
583 if (pos < 0)
584 return -EINVAL;
585
2903ff01
AV
586 f = fdget(fd);
587 if (f.file) {
1da177e4 588 ret = -ESPIPE;
2903ff01
AV
589 if (f.file->f_mode & FMODE_PWRITE)
590 ret = vfs_write(f.file, buf, count, &pos);
591 fdput(f);
1da177e4
LT
592 }
593
594 return ret;
595}
596
597/*
598 * Reduce an iovec's length in-place. Return the resulting number of segments
599 */
600unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
601{
602 unsigned long seg = 0;
603 size_t len = 0;
604
605 while (seg < nr_segs) {
606 seg++;
607 if (len + iov->iov_len >= to) {
608 iov->iov_len = to - len;
609 break;
610 }
611 len += iov->iov_len;
612 iov++;
613 }
614 return seg;
615}
19295529 616EXPORT_SYMBOL(iov_shorten);
1da177e4 617
72ec3516 618static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
ee0b3e67
BP
619 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
620{
621 struct kiocb kiocb;
622 ssize_t ret;
623
624 init_sync_kiocb(&kiocb, filp);
625 kiocb.ki_pos = *ppos;
ee0b3e67
BP
626 kiocb.ki_nbytes = len;
627
41003a7b 628 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
ee0b3e67
BP
629 if (ret == -EIOCBQUEUED)
630 ret = wait_on_sync_kiocb(&kiocb);
631 *ppos = kiocb.ki_pos;
632 return ret;
633}
634
635/* Do it by hand, with file-ops */
72ec3516 636static ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
ee0b3e67
BP
637 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
638{
639 struct iovec *vector = iov;
640 ssize_t ret = 0;
641
642 while (nr_segs > 0) {
643 void __user *base;
644 size_t len;
645 ssize_t nr;
646
647 base = vector->iov_base;
648 len = vector->iov_len;
649 vector++;
650 nr_segs--;
651
652 nr = fn(filp, base, len, ppos);
653
654 if (nr < 0) {
655 if (!ret)
656 ret = nr;
657 break;
658 }
659 ret += nr;
660 if (nr != len)
661 break;
662 }
663
664 return ret;
665}
666
1da177e4
LT
667/* A write operation does a read from user space and vice versa */
668#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
669
eed4e51f
BP
670ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
671 unsigned long nr_segs, unsigned long fast_segs,
672 struct iovec *fast_pointer,
ac34ebb3 673 struct iovec **ret_pointer)
435f49a5 674{
eed4e51f 675 unsigned long seg;
435f49a5 676 ssize_t ret;
eed4e51f
BP
677 struct iovec *iov = fast_pointer;
678
435f49a5
LT
679 /*
680 * SuS says "The readv() function *may* fail if the iovcnt argument
681 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
682 * traditionally returned zero for zero segments, so...
683 */
eed4e51f
BP
684 if (nr_segs == 0) {
685 ret = 0;
435f49a5 686 goto out;
eed4e51f
BP
687 }
688
435f49a5
LT
689 /*
690 * First get the "struct iovec" from user memory and
691 * verify all the pointers
692 */
eed4e51f
BP
693 if (nr_segs > UIO_MAXIOV) {
694 ret = -EINVAL;
435f49a5 695 goto out;
eed4e51f
BP
696 }
697 if (nr_segs > fast_segs) {
435f49a5 698 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
eed4e51f
BP
699 if (iov == NULL) {
700 ret = -ENOMEM;
435f49a5 701 goto out;
eed4e51f 702 }
435f49a5 703 }
eed4e51f
BP
704 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
705 ret = -EFAULT;
435f49a5 706 goto out;
eed4e51f
BP
707 }
708
435f49a5 709 /*
eed4e51f
BP
710 * According to the Single Unix Specification we should return EINVAL
711 * if an element length is < 0 when cast to ssize_t or if the
712 * total length would overflow the ssize_t return value of the
713 * system call.
435f49a5
LT
714 *
715 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
716 * overflow case.
717 */
eed4e51f 718 ret = 0;
435f49a5
LT
719 for (seg = 0; seg < nr_segs; seg++) {
720 void __user *buf = iov[seg].iov_base;
721 ssize_t len = (ssize_t)iov[seg].iov_len;
eed4e51f
BP
722
723 /* see if we we're about to use an invalid len or if
724 * it's about to overflow ssize_t */
435f49a5 725 if (len < 0) {
eed4e51f 726 ret = -EINVAL;
435f49a5 727 goto out;
eed4e51f 728 }
ac34ebb3 729 if (type >= 0
fcf63409 730 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
eed4e51f 731 ret = -EFAULT;
435f49a5
LT
732 goto out;
733 }
734 if (len > MAX_RW_COUNT - ret) {
735 len = MAX_RW_COUNT - ret;
736 iov[seg].iov_len = len;
eed4e51f 737 }
eed4e51f 738 ret += len;
435f49a5 739 }
eed4e51f
BP
740out:
741 *ret_pointer = iov;
742 return ret;
743}
744
1da177e4
LT
745static ssize_t do_readv_writev(int type, struct file *file,
746 const struct iovec __user * uvector,
747 unsigned long nr_segs, loff_t *pos)
748{
1da177e4
LT
749 size_t tot_len;
750 struct iovec iovstack[UIO_FASTIOV];
ee0b3e67 751 struct iovec *iov = iovstack;
1da177e4 752 ssize_t ret;
1da177e4
LT
753 io_fn_t fn;
754 iov_fn_t fnv;
755
eed4e51f 756 ret = rw_copy_check_uvector(type, uvector, nr_segs,
ac34ebb3 757 ARRAY_SIZE(iovstack), iovstack, &iov);
eed4e51f 758 if (ret <= 0)
1da177e4 759 goto out;
1da177e4 760
eed4e51f 761 tot_len = ret;
1da177e4 762 ret = rw_verify_area(type, file, pos, tot_len);
e28cc715 763 if (ret < 0)
411b67b4 764 goto out;
1da177e4
LT
765
766 fnv = NULL;
767 if (type == READ) {
768 fn = file->f_op->read;
ee0b3e67 769 fnv = file->f_op->aio_read;
1da177e4
LT
770 } else {
771 fn = (io_fn_t)file->f_op->write;
ee0b3e67 772 fnv = file->f_op->aio_write;
03d95eb2 773 file_start_write(file);
1da177e4
LT
774 }
775
ee0b3e67
BP
776 if (fnv)
777 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
778 pos, fnv);
779 else
780 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
1da177e4 781
03d95eb2
AV
782 if (type != READ)
783 file_end_write(file);
784
1da177e4
LT
785out:
786 if (iov != iovstack)
787 kfree(iov);
0eeca283
RL
788 if ((ret + (type == READ)) > 0) {
789 if (type == READ)
2a12a9d7 790 fsnotify_access(file);
0eeca283 791 else
2a12a9d7 792 fsnotify_modify(file);
0eeca283 793 }
1da177e4 794 return ret;
1da177e4
LT
795}
796
797ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
798 unsigned long vlen, loff_t *pos)
799{
800 if (!(file->f_mode & FMODE_READ))
801 return -EBADF;
72c2d531 802 if (!file->f_op->aio_read && !file->f_op->read)
1da177e4
LT
803 return -EINVAL;
804
805 return do_readv_writev(READ, file, vec, vlen, pos);
806}
807
808EXPORT_SYMBOL(vfs_readv);
809
810ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
811 unsigned long vlen, loff_t *pos)
812{
813 if (!(file->f_mode & FMODE_WRITE))
814 return -EBADF;
72c2d531 815 if (!file->f_op->aio_write && !file->f_op->write)
1da177e4
LT
816 return -EINVAL;
817
818 return do_readv_writev(WRITE, file, vec, vlen, pos);
819}
820
821EXPORT_SYMBOL(vfs_writev);
822
3cdad428
HC
823SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
824 unsigned long, vlen)
1da177e4 825{
9c225f26 826 struct fd f = fdget_pos(fd);
1da177e4 827 ssize_t ret = -EBADF;
1da177e4 828
2903ff01
AV
829 if (f.file) {
830 loff_t pos = file_pos_read(f.file);
831 ret = vfs_readv(f.file, vec, vlen, &pos);
5faf153e
AV
832 if (ret >= 0)
833 file_pos_write(f.file, pos);
9c225f26 834 fdput_pos(f);
1da177e4
LT
835 }
836
837 if (ret > 0)
4b98d11b
AD
838 add_rchar(current, ret);
839 inc_syscr(current);
1da177e4
LT
840 return ret;
841}
842
3cdad428
HC
843SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
844 unsigned long, vlen)
1da177e4 845{
9c225f26 846 struct fd f = fdget_pos(fd);
1da177e4 847 ssize_t ret = -EBADF;
1da177e4 848
2903ff01
AV
849 if (f.file) {
850 loff_t pos = file_pos_read(f.file);
851 ret = vfs_writev(f.file, vec, vlen, &pos);
5faf153e
AV
852 if (ret >= 0)
853 file_pos_write(f.file, pos);
9c225f26 854 fdput_pos(f);
1da177e4
LT
855 }
856
857 if (ret > 0)
4b98d11b
AD
858 add_wchar(current, ret);
859 inc_syscw(current);
1da177e4
LT
860 return ret;
861}
862
601cc11d
LT
863static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
864{
865#define HALF_LONG_BITS (BITS_PER_LONG / 2)
866 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
867}
868
f3554f4b 869SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
601cc11d 870 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
f3554f4b 871{
601cc11d 872 loff_t pos = pos_from_hilo(pos_h, pos_l);
2903ff01 873 struct fd f;
f3554f4b 874 ssize_t ret = -EBADF;
f3554f4b
GH
875
876 if (pos < 0)
877 return -EINVAL;
878
2903ff01
AV
879 f = fdget(fd);
880 if (f.file) {
f3554f4b 881 ret = -ESPIPE;
2903ff01
AV
882 if (f.file->f_mode & FMODE_PREAD)
883 ret = vfs_readv(f.file, vec, vlen, &pos);
884 fdput(f);
f3554f4b
GH
885 }
886
887 if (ret > 0)
888 add_rchar(current, ret);
889 inc_syscr(current);
890 return ret;
891}
892
893SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
601cc11d 894 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
f3554f4b 895{
601cc11d 896 loff_t pos = pos_from_hilo(pos_h, pos_l);
2903ff01 897 struct fd f;
f3554f4b 898 ssize_t ret = -EBADF;
f3554f4b
GH
899
900 if (pos < 0)
901 return -EINVAL;
902
2903ff01
AV
903 f = fdget(fd);
904 if (f.file) {
f3554f4b 905 ret = -ESPIPE;
2903ff01
AV
906 if (f.file->f_mode & FMODE_PWRITE)
907 ret = vfs_writev(f.file, vec, vlen, &pos);
908 fdput(f);
f3554f4b
GH
909 }
910
911 if (ret > 0)
912 add_wchar(current, ret);
913 inc_syscw(current);
914 return ret;
915}
916
72ec3516
AV
917#ifdef CONFIG_COMPAT
918
919static ssize_t compat_do_readv_writev(int type, struct file *file,
920 const struct compat_iovec __user *uvector,
921 unsigned long nr_segs, loff_t *pos)
922{
923 compat_ssize_t tot_len;
924 struct iovec iovstack[UIO_FASTIOV];
925 struct iovec *iov = iovstack;
926 ssize_t ret;
927 io_fn_t fn;
928 iov_fn_t fnv;
929
72ec3516
AV
930 ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
931 UIO_FASTIOV, iovstack, &iov);
932 if (ret <= 0)
933 goto out;
934
935 tot_len = ret;
936 ret = rw_verify_area(type, file, pos, tot_len);
937 if (ret < 0)
938 goto out;
939
940 fnv = NULL;
941 if (type == READ) {
942 fn = file->f_op->read;
943 fnv = file->f_op->aio_read;
944 } else {
945 fn = (io_fn_t)file->f_op->write;
946 fnv = file->f_op->aio_write;
03d95eb2 947 file_start_write(file);
72ec3516
AV
948 }
949
03d95eb2 950 if (fnv)
72ec3516
AV
951 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
952 pos, fnv);
03d95eb2 953 else
72ec3516
AV
954 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
955
03d95eb2
AV
956 if (type != READ)
957 file_end_write(file);
958
72ec3516
AV
959out:
960 if (iov != iovstack)
961 kfree(iov);
962 if ((ret + (type == READ)) > 0) {
963 if (type == READ)
964 fsnotify_access(file);
965 else
966 fsnotify_modify(file);
967 }
968 return ret;
969}
970
971static size_t compat_readv(struct file *file,
972 const struct compat_iovec __user *vec,
973 unsigned long vlen, loff_t *pos)
974{
975 ssize_t ret = -EBADF;
976
977 if (!(file->f_mode & FMODE_READ))
978 goto out;
979
980 ret = -EINVAL;
72c2d531 981 if (!file->f_op->aio_read && !file->f_op->read)
72ec3516
AV
982 goto out;
983
984 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
985
986out:
987 if (ret > 0)
988 add_rchar(current, ret);
989 inc_syscr(current);
990 return ret;
991}
992
dfd948e3 993COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
72ec3516 994 const struct compat_iovec __user *,vec,
dfd948e3 995 compat_ulong_t, vlen)
72ec3516 996{
9c225f26 997 struct fd f = fdget_pos(fd);
72ec3516
AV
998 ssize_t ret;
999 loff_t pos;
1000
1001 if (!f.file)
1002 return -EBADF;
1003 pos = f.file->f_pos;
1004 ret = compat_readv(f.file, vec, vlen, &pos);
5faf153e
AV
1005 if (ret >= 0)
1006 f.file->f_pos = pos;
9c225f26 1007 fdput_pos(f);
72ec3516
AV
1008 return ret;
1009}
1010
1011COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1012 const struct compat_iovec __user *,vec,
1013 unsigned long, vlen, loff_t, pos)
1014{
1015 struct fd f;
1016 ssize_t ret;
1017
1018 if (pos < 0)
1019 return -EINVAL;
1020 f = fdget(fd);
1021 if (!f.file)
1022 return -EBADF;
1023 ret = -ESPIPE;
1024 if (f.file->f_mode & FMODE_PREAD)
1025 ret = compat_readv(f.file, vec, vlen, &pos);
1026 fdput(f);
1027 return ret;
1028}
1029
dfd948e3 1030COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
72ec3516 1031 const struct compat_iovec __user *,vec,
dfd948e3 1032 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
72ec3516
AV
1033{
1034 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1035 return compat_sys_preadv64(fd, vec, vlen, pos);
1036}
1037
1038static size_t compat_writev(struct file *file,
1039 const struct compat_iovec __user *vec,
1040 unsigned long vlen, loff_t *pos)
1041{
1042 ssize_t ret = -EBADF;
1043
1044 if (!(file->f_mode & FMODE_WRITE))
1045 goto out;
1046
1047 ret = -EINVAL;
72c2d531 1048 if (!file->f_op->aio_write && !file->f_op->write)
72ec3516
AV
1049 goto out;
1050
1051 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1052
1053out:
1054 if (ret > 0)
1055 add_wchar(current, ret);
1056 inc_syscw(current);
1057 return ret;
1058}
1059
dfd948e3 1060COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
72ec3516 1061 const struct compat_iovec __user *, vec,
dfd948e3 1062 compat_ulong_t, vlen)
72ec3516 1063{
9c225f26 1064 struct fd f = fdget_pos(fd);
72ec3516
AV
1065 ssize_t ret;
1066 loff_t pos;
1067
1068 if (!f.file)
1069 return -EBADF;
1070 pos = f.file->f_pos;
1071 ret = compat_writev(f.file, vec, vlen, &pos);
5faf153e
AV
1072 if (ret >= 0)
1073 f.file->f_pos = pos;
9c225f26 1074 fdput_pos(f);
72ec3516
AV
1075 return ret;
1076}
1077
1078COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1079 const struct compat_iovec __user *,vec,
1080 unsigned long, vlen, loff_t, pos)
1081{
1082 struct fd f;
1083 ssize_t ret;
1084
1085 if (pos < 0)
1086 return -EINVAL;
1087 f = fdget(fd);
1088 if (!f.file)
1089 return -EBADF;
1090 ret = -ESPIPE;
1091 if (f.file->f_mode & FMODE_PWRITE)
1092 ret = compat_writev(f.file, vec, vlen, &pos);
1093 fdput(f);
1094 return ret;
1095}
1096
dfd948e3 1097COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
72ec3516 1098 const struct compat_iovec __user *,vec,
dfd948e3 1099 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
72ec3516
AV
1100{
1101 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1102 return compat_sys_pwritev64(fd, vec, vlen, pos);
1103}
1104#endif
1105
19f4fc3a
AV
1106static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1107 size_t count, loff_t max)
1da177e4 1108{
2903ff01
AV
1109 struct fd in, out;
1110 struct inode *in_inode, *out_inode;
1da177e4 1111 loff_t pos;
7995bd28 1112 loff_t out_pos;
1da177e4 1113 ssize_t retval;
2903ff01 1114 int fl;
1da177e4
LT
1115
1116 /*
1117 * Get input file, and verify that it is ok..
1118 */
1119 retval = -EBADF;
2903ff01
AV
1120 in = fdget(in_fd);
1121 if (!in.file)
1da177e4 1122 goto out;
2903ff01 1123 if (!(in.file->f_mode & FMODE_READ))
1da177e4 1124 goto fput_in;
1da177e4 1125 retval = -ESPIPE;
7995bd28
AV
1126 if (!ppos) {
1127 pos = in.file->f_pos;
1128 } else {
1129 pos = *ppos;
2903ff01 1130 if (!(in.file->f_mode & FMODE_PREAD))
1da177e4 1131 goto fput_in;
7995bd28
AV
1132 }
1133 retval = rw_verify_area(READ, in.file, &pos, count);
e28cc715 1134 if (retval < 0)
1da177e4 1135 goto fput_in;
e28cc715 1136 count = retval;
1da177e4 1137
1da177e4
LT
1138 /*
1139 * Get output file, and verify that it is ok..
1140 */
1141 retval = -EBADF;
2903ff01
AV
1142 out = fdget(out_fd);
1143 if (!out.file)
1da177e4 1144 goto fput_in;
2903ff01 1145 if (!(out.file->f_mode & FMODE_WRITE))
1da177e4
LT
1146 goto fput_out;
1147 retval = -EINVAL;
496ad9aa
AV
1148 in_inode = file_inode(in.file);
1149 out_inode = file_inode(out.file);
7995bd28
AV
1150 out_pos = out.file->f_pos;
1151 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
e28cc715 1152 if (retval < 0)
1da177e4 1153 goto fput_out;
e28cc715 1154 count = retval;
1da177e4 1155
1da177e4
LT
1156 if (!max)
1157 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1158
1da177e4
LT
1159 if (unlikely(pos + count > max)) {
1160 retval = -EOVERFLOW;
1161 if (pos >= max)
1162 goto fput_out;
1163 count = max - pos;
1164 }
1165
d96e6e71 1166 fl = 0;
534f2aaa 1167#if 0
d96e6e71
JA
1168 /*
1169 * We need to debate whether we can enable this or not. The
1170 * man page documents EAGAIN return for the output at least,
1171 * and the application is arguably buggy if it doesn't expect
1172 * EAGAIN on a non-blocking file descriptor.
1173 */
2903ff01 1174 if (in.file->f_flags & O_NONBLOCK)
d96e6e71 1175 fl = SPLICE_F_NONBLOCK;
534f2aaa 1176#endif
50cd2c57 1177 file_start_write(out.file);
7995bd28 1178 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
50cd2c57 1179 file_end_write(out.file);
1da177e4
LT
1180
1181 if (retval > 0) {
4b98d11b
AD
1182 add_rchar(current, retval);
1183 add_wchar(current, retval);
a68c2f12
SW
1184 fsnotify_access(in.file);
1185 fsnotify_modify(out.file);
7995bd28
AV
1186 out.file->f_pos = out_pos;
1187 if (ppos)
1188 *ppos = pos;
1189 else
1190 in.file->f_pos = pos;
1da177e4 1191 }
1da177e4 1192
4b98d11b
AD
1193 inc_syscr(current);
1194 inc_syscw(current);
7995bd28 1195 if (pos > max)
1da177e4
LT
1196 retval = -EOVERFLOW;
1197
1198fput_out:
2903ff01 1199 fdput(out);
1da177e4 1200fput_in:
2903ff01 1201 fdput(in);
1da177e4
LT
1202out:
1203 return retval;
1204}
1205
002c8976 1206SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
1da177e4
LT
1207{
1208 loff_t pos;
1209 off_t off;
1210 ssize_t ret;
1211
1212 if (offset) {
1213 if (unlikely(get_user(off, offset)))
1214 return -EFAULT;
1215 pos = off;
1216 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1217 if (unlikely(put_user(pos, offset)))
1218 return -EFAULT;
1219 return ret;
1220 }
1221
1222 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1223}
1224
002c8976 1225SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
1da177e4
LT
1226{
1227 loff_t pos;
1228 ssize_t ret;
1229
1230 if (offset) {
1231 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1232 return -EFAULT;
1233 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1234 if (unlikely(put_user(pos, offset)))
1235 return -EFAULT;
1236 return ret;
1237 }
1238
1239 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1240}
19f4fc3a
AV
1241
1242#ifdef CONFIG_COMPAT
1243COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1244 compat_off_t __user *, offset, compat_size_t, count)
1245{
1246 loff_t pos;
1247 off_t off;
1248 ssize_t ret;
1249
1250 if (offset) {
1251 if (unlikely(get_user(off, offset)))
1252 return -EFAULT;
1253 pos = off;
1254 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1255 if (unlikely(put_user(pos, offset)))
1256 return -EFAULT;
1257 return ret;
1258 }
1259
1260 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1261}
1262
1263COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1264 compat_loff_t __user *, offset, compat_size_t, count)
1265{
1266 loff_t pos;
1267 ssize_t ret;
1268
1269 if (offset) {
1270 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1271 return -EFAULT;
1272 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1273 if (unlikely(put_user(pos, offset)))
1274 return -EFAULT;
1275 return ret;
1276 }
1277
1278 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1279}
1280#endif