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