OSDN Git Service

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