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