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