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