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