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