fs: don't allow kernel reads and writes without iter ops
[linux-2.6-microblaze.git] / fs / read_write.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/read_write.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  */
7
8 #include <linux/slab.h>
9 #include <linux/stat.h>
10 #include <linux/sched/xacct.h>
11 #include <linux/fcntl.h>
12 #include <linux/file.h>
13 #include <linux/uio.h>
14 #include <linux/fsnotify.h>
15 #include <linux/security.h>
16 #include <linux/export.h>
17 #include <linux/syscalls.h>
18 #include <linux/pagemap.h>
19 #include <linux/splice.h>
20 #include <linux/compat.h>
21 #include <linux/mount.h>
22 #include <linux/fs.h>
23 #include "internal.h"
24
25 #include <linux/uaccess.h>
26 #include <asm/unistd.h>
27
28 const struct file_operations generic_ro_fops = {
29         .llseek         = generic_file_llseek,
30         .read_iter      = generic_file_read_iter,
31         .mmap           = generic_file_readonly_mmap,
32         .splice_read    = generic_file_splice_read,
33 };
34
35 EXPORT_SYMBOL(generic_ro_fops);
36
37 static inline bool unsigned_offsets(struct file *file)
38 {
39         return file->f_mode & FMODE_UNSIGNED_OFFSET;
40 }
41
42 /**
43  * vfs_setpos - update the file offset for lseek
44  * @file:       file structure in question
45  * @offset:     file offset to seek to
46  * @maxsize:    maximum file size
47  *
48  * This is a low-level filesystem helper for updating the file offset to
49  * the value specified by @offset if the given offset is valid and it is
50  * not equal to the current file offset.
51  *
52  * Return the specified offset on success and -EINVAL on invalid offset.
53  */
54 loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
55 {
56         if (offset < 0 && !unsigned_offsets(file))
57                 return -EINVAL;
58         if (offset > maxsize)
59                 return -EINVAL;
60
61         if (offset != file->f_pos) {
62                 file->f_pos = offset;
63                 file->f_version = 0;
64         }
65         return offset;
66 }
67 EXPORT_SYMBOL(vfs_setpos);
68
69 /**
70  * generic_file_llseek_size - generic llseek implementation for regular files
71  * @file:       file structure to seek on
72  * @offset:     file offset to seek to
73  * @whence:     type of seek
74  * @size:       max size of this file in file system
75  * @eof:        offset used for SEEK_END position
76  *
77  * This is a variant of generic_file_llseek that allows passing in a custom
78  * maximum file size and a custom EOF position, for e.g. hashed directories
79  *
80  * Synchronization:
81  * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
82  * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
83  * read/writes behave like SEEK_SET against seeks.
84  */
85 loff_t
86 generic_file_llseek_size(struct file *file, loff_t offset, int whence,
87                 loff_t maxsize, loff_t eof)
88 {
89         switch (whence) {
90         case SEEK_END:
91                 offset += eof;
92                 break;
93         case SEEK_CUR:
94                 /*
95                  * Here we special-case the lseek(fd, 0, SEEK_CUR)
96                  * position-querying operation.  Avoid rewriting the "same"
97                  * f_pos value back to the file because a concurrent read(),
98                  * write() or lseek() might have altered it
99                  */
100                 if (offset == 0)
101                         return file->f_pos;
102                 /*
103                  * f_lock protects against read/modify/write race with other
104                  * SEEK_CURs. Note that parallel writes and reads behave
105                  * like SEEK_SET.
106                  */
107                 spin_lock(&file->f_lock);
108                 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
109                 spin_unlock(&file->f_lock);
110                 return offset;
111         case SEEK_DATA:
112                 /*
113                  * In the generic case the entire file is data, so as long as
114                  * offset isn't at the end of the file then the offset is data.
115                  */
116                 if ((unsigned long long)offset >= eof)
117                         return -ENXIO;
118                 break;
119         case SEEK_HOLE:
120                 /*
121                  * There is a virtual hole at the end of the file, so as long as
122                  * offset isn't i_size or larger, return i_size.
123                  */
124                 if ((unsigned long long)offset >= eof)
125                         return -ENXIO;
126                 offset = eof;
127                 break;
128         }
129
130         return vfs_setpos(file, offset, maxsize);
131 }
132 EXPORT_SYMBOL(generic_file_llseek_size);
133
134 /**
135  * generic_file_llseek - generic llseek implementation for regular files
136  * @file:       file structure to seek on
137  * @offset:     file offset to seek to
138  * @whence:     type of seek
139  *
140  * This is a generic implemenation of ->llseek useable for all normal local
141  * filesystems.  It just updates the file offset to the value specified by
142  * @offset and @whence.
143  */
144 loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
145 {
146         struct inode *inode = file->f_mapping->host;
147
148         return generic_file_llseek_size(file, offset, whence,
149                                         inode->i_sb->s_maxbytes,
150                                         i_size_read(inode));
151 }
152 EXPORT_SYMBOL(generic_file_llseek);
153
154 /**
155  * fixed_size_llseek - llseek implementation for fixed-sized devices
156  * @file:       file structure to seek on
157  * @offset:     file offset to seek to
158  * @whence:     type of seek
159  * @size:       size of the file
160  *
161  */
162 loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
163 {
164         switch (whence) {
165         case SEEK_SET: case SEEK_CUR: case SEEK_END:
166                 return generic_file_llseek_size(file, offset, whence,
167                                                 size, size);
168         default:
169                 return -EINVAL;
170         }
171 }
172 EXPORT_SYMBOL(fixed_size_llseek);
173
174 /**
175  * no_seek_end_llseek - llseek implementation for fixed-sized devices
176  * @file:       file structure to seek on
177  * @offset:     file offset to seek to
178  * @whence:     type of seek
179  *
180  */
181 loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
182 {
183         switch (whence) {
184         case SEEK_SET: case SEEK_CUR:
185                 return generic_file_llseek_size(file, offset, whence,
186                                                 OFFSET_MAX, 0);
187         default:
188                 return -EINVAL;
189         }
190 }
191 EXPORT_SYMBOL(no_seek_end_llseek);
192
193 /**
194  * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
195  * @file:       file structure to seek on
196  * @offset:     file offset to seek to
197  * @whence:     type of seek
198  * @size:       maximal offset allowed
199  *
200  */
201 loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
202 {
203         switch (whence) {
204         case SEEK_SET: case SEEK_CUR:
205                 return generic_file_llseek_size(file, offset, whence,
206                                                 size, 0);
207         default:
208                 return -EINVAL;
209         }
210 }
211 EXPORT_SYMBOL(no_seek_end_llseek_size);
212
213 /**
214  * noop_llseek - No Operation Performed llseek implementation
215  * @file:       file structure to seek on
216  * @offset:     file offset to seek to
217  * @whence:     type of seek
218  *
219  * This is an implementation of ->llseek useable for the rare special case when
220  * userspace expects the seek to succeed but the (device) file is actually not
221  * able to perform the seek. In this case you use noop_llseek() instead of
222  * falling back to the default implementation of ->llseek.
223  */
224 loff_t noop_llseek(struct file *file, loff_t offset, int whence)
225 {
226         return file->f_pos;
227 }
228 EXPORT_SYMBOL(noop_llseek);
229
230 loff_t no_llseek(struct file *file, loff_t offset, int whence)
231 {
232         return -ESPIPE;
233 }
234 EXPORT_SYMBOL(no_llseek);
235
236 loff_t default_llseek(struct file *file, loff_t offset, int whence)
237 {
238         struct inode *inode = file_inode(file);
239         loff_t retval;
240
241         inode_lock(inode);
242         switch (whence) {
243                 case SEEK_END:
244                         offset += i_size_read(inode);
245                         break;
246                 case SEEK_CUR:
247                         if (offset == 0) {
248                                 retval = file->f_pos;
249                                 goto out;
250                         }
251                         offset += file->f_pos;
252                         break;
253                 case SEEK_DATA:
254                         /*
255                          * In the generic case the entire file is data, so as
256                          * long as offset isn't at the end of the file then the
257                          * offset is data.
258                          */
259                         if (offset >= inode->i_size) {
260                                 retval = -ENXIO;
261                                 goto out;
262                         }
263                         break;
264                 case SEEK_HOLE:
265                         /*
266                          * There is a virtual hole at the end of the file, so
267                          * as long as offset isn't i_size or larger, return
268                          * i_size.
269                          */
270                         if (offset >= inode->i_size) {
271                                 retval = -ENXIO;
272                                 goto out;
273                         }
274                         offset = inode->i_size;
275                         break;
276         }
277         retval = -EINVAL;
278         if (offset >= 0 || unsigned_offsets(file)) {
279                 if (offset != file->f_pos) {
280                         file->f_pos = offset;
281                         file->f_version = 0;
282                 }
283                 retval = offset;
284         }
285 out:
286         inode_unlock(inode);
287         return retval;
288 }
289 EXPORT_SYMBOL(default_llseek);
290
291 loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
292 {
293         loff_t (*fn)(struct file *, loff_t, int);
294
295         fn = no_llseek;
296         if (file->f_mode & FMODE_LSEEK) {
297                 if (file->f_op->llseek)
298                         fn = file->f_op->llseek;
299         }
300         return fn(file, offset, whence);
301 }
302 EXPORT_SYMBOL(vfs_llseek);
303
304 static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence)
305 {
306         off_t retval;
307         struct fd f = fdget_pos(fd);
308         if (!f.file)
309                 return -EBADF;
310
311         retval = -EINVAL;
312         if (whence <= SEEK_MAX) {
313                 loff_t res = vfs_llseek(f.file, offset, whence);
314                 retval = res;
315                 if (res != (loff_t)retval)
316                         retval = -EOVERFLOW;    /* LFS: should only happen on 32 bit platforms */
317         }
318         fdput_pos(f);
319         return retval;
320 }
321
322 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
323 {
324         return ksys_lseek(fd, offset, whence);
325 }
326
327 #ifdef CONFIG_COMPAT
328 COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
329 {
330         return ksys_lseek(fd, offset, whence);
331 }
332 #endif
333
334 #if !defined(CONFIG_64BIT) || defined(CONFIG_COMPAT) || \
335         defined(__ARCH_WANT_SYS_LLSEEK)
336 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
337                 unsigned long, offset_low, loff_t __user *, result,
338                 unsigned int, whence)
339 {
340         int retval;
341         struct fd f = fdget_pos(fd);
342         loff_t offset;
343
344         if (!f.file)
345                 return -EBADF;
346
347         retval = -EINVAL;
348         if (whence > SEEK_MAX)
349                 goto out_putf;
350
351         offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
352                         whence);
353
354         retval = (int)offset;
355         if (offset >= 0) {
356                 retval = -EFAULT;
357                 if (!copy_to_user(result, &offset, sizeof(offset)))
358                         retval = 0;
359         }
360 out_putf:
361         fdput_pos(f);
362         return retval;
363 }
364 #endif
365
366 int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
367 {
368         struct inode *inode;
369         int retval = -EINVAL;
370
371         inode = file_inode(file);
372         if (unlikely((ssize_t) count < 0))
373                 return retval;
374
375         /*
376          * ranged mandatory locking does not apply to streams - it makes sense
377          * only for files where position has a meaning.
378          */
379         if (ppos) {
380                 loff_t pos = *ppos;
381
382                 if (unlikely(pos < 0)) {
383                         if (!unsigned_offsets(file))
384                                 return retval;
385                         if (count >= -pos) /* both values are in 0..LLONG_MAX */
386                                 return -EOVERFLOW;
387                 } else if (unlikely((loff_t) (pos + count) < 0)) {
388                         if (!unsigned_offsets(file))
389                                 return retval;
390                 }
391
392                 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
393                         retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
394                                         read_write == READ ? F_RDLCK : F_WRLCK);
395                         if (retval < 0)
396                                 return retval;
397                 }
398         }
399
400         return security_file_permission(file,
401                                 read_write == READ ? MAY_READ : MAY_WRITE);
402 }
403
404 static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
405 {
406         struct iovec iov = { .iov_base = buf, .iov_len = len };
407         struct kiocb kiocb;
408         struct iov_iter iter;
409         ssize_t ret;
410
411         init_sync_kiocb(&kiocb, filp);
412         kiocb.ki_pos = (ppos ? *ppos : 0);
413         iov_iter_init(&iter, READ, &iov, 1, len);
414
415         ret = call_read_iter(filp, &kiocb, &iter);
416         BUG_ON(ret == -EIOCBQUEUED);
417         if (ppos)
418                 *ppos = kiocb.ki_pos;
419         return ret;
420 }
421
422 static int warn_unsupported(struct file *file, const char *op)
423 {
424         pr_warn_ratelimited(
425                 "kernel %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
426                 op, file, current->pid, current->comm);
427         return -EINVAL;
428 }
429
430 ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
431 {
432         struct kvec iov = {
433                 .iov_base       = buf,
434                 .iov_len        = min_t(size_t, count, MAX_RW_COUNT),
435         };
436         struct kiocb kiocb;
437         struct iov_iter iter;
438         ssize_t ret;
439
440         if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ)))
441                 return -EINVAL;
442         if (!(file->f_mode & FMODE_CAN_READ))
443                 return -EINVAL;
444         /*
445          * Also fail if ->read_iter and ->read are both wired up as that
446          * implies very convoluted semantics.
447          */
448         if (unlikely(!file->f_op->read_iter || file->f_op->read))
449                 return warn_unsupported(file, "read");
450
451         init_sync_kiocb(&kiocb, file);
452         kiocb.ki_pos = *pos;
453         iov_iter_kvec(&iter, READ, &iov, 1, iov.iov_len);
454         ret = file->f_op->read_iter(&kiocb, &iter);
455         if (ret > 0) {
456                 *pos = kiocb.ki_pos;
457                 fsnotify_access(file);
458                 add_rchar(current, ret);
459         }
460         inc_syscr(current);
461         return ret;
462 }
463
464 ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
465 {
466         ssize_t ret;
467
468         ret = rw_verify_area(READ, file, pos, count);
469         if (ret)
470                 return ret;
471         return __kernel_read(file, buf, count, pos);
472 }
473 EXPORT_SYMBOL(kernel_read);
474
475 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
476 {
477         ssize_t ret;
478
479         if (!(file->f_mode & FMODE_READ))
480                 return -EBADF;
481         if (!(file->f_mode & FMODE_CAN_READ))
482                 return -EINVAL;
483         if (unlikely(!access_ok(buf, count)))
484                 return -EFAULT;
485
486         ret = rw_verify_area(READ, file, pos, count);
487         if (ret)
488                 return ret;
489         if (count > MAX_RW_COUNT)
490                 count =  MAX_RW_COUNT;
491
492         if (file->f_op->read)
493                 ret = file->f_op->read(file, buf, count, pos);
494         else if (file->f_op->read_iter)
495                 ret = new_sync_read(file, buf, count, pos);
496         else
497                 ret = -EINVAL;
498         if (ret > 0) {
499                 fsnotify_access(file);
500                 add_rchar(current, ret);
501         }
502         inc_syscr(current);
503         return ret;
504 }
505
506 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
507 {
508         struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
509         struct kiocb kiocb;
510         struct iov_iter iter;
511         ssize_t ret;
512
513         init_sync_kiocb(&kiocb, filp);
514         kiocb.ki_pos = (ppos ? *ppos : 0);
515         iov_iter_init(&iter, WRITE, &iov, 1, len);
516
517         ret = call_write_iter(filp, &kiocb, &iter);
518         BUG_ON(ret == -EIOCBQUEUED);
519         if (ret > 0 && ppos)
520                 *ppos = kiocb.ki_pos;
521         return ret;
522 }
523
524 /* caller is responsible for file_start_write/file_end_write */
525 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
526 {
527         struct kvec iov = {
528                 .iov_base       = (void *)buf,
529                 .iov_len        = min_t(size_t, count, MAX_RW_COUNT),
530         };
531         struct kiocb kiocb;
532         struct iov_iter iter;
533         ssize_t ret;
534
535         if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE)))
536                 return -EBADF;
537         if (!(file->f_mode & FMODE_CAN_WRITE))
538                 return -EINVAL;
539         /*
540          * Also fail if ->write_iter and ->write are both wired up as that
541          * implies very convoluted semantics.
542          */
543         if (unlikely(!file->f_op->write_iter || file->f_op->write))
544                 return warn_unsupported(file, "write");
545
546         init_sync_kiocb(&kiocb, file);
547         kiocb.ki_pos = *pos;
548         iov_iter_kvec(&iter, WRITE, &iov, 1, iov.iov_len);
549         ret = file->f_op->write_iter(&kiocb, &iter);
550         if (ret > 0) {
551                 *pos = kiocb.ki_pos;
552                 fsnotify_modify(file);
553                 add_wchar(current, ret);
554         }
555         inc_syscw(current);
556         return ret;
557 }
558
559 ssize_t kernel_write(struct file *file, const void *buf, size_t count,
560                             loff_t *pos)
561 {
562         ssize_t ret;
563
564         ret = rw_verify_area(WRITE, file, pos, count);
565         if (ret)
566                 return ret;
567
568         file_start_write(file);
569         ret =  __kernel_write(file, buf, count, pos);
570         file_end_write(file);
571         return ret;
572 }
573 EXPORT_SYMBOL(kernel_write);
574
575 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
576 {
577         ssize_t ret;
578
579         if (!(file->f_mode & FMODE_WRITE))
580                 return -EBADF;
581         if (!(file->f_mode & FMODE_CAN_WRITE))
582                 return -EINVAL;
583         if (unlikely(!access_ok(buf, count)))
584                 return -EFAULT;
585
586         ret = rw_verify_area(WRITE, file, pos, count);
587         if (ret)
588                 return ret;
589         if (count > MAX_RW_COUNT)
590                 count =  MAX_RW_COUNT;
591         file_start_write(file);
592         if (file->f_op->write)
593                 ret = file->f_op->write(file, buf, count, pos);
594         else if (file->f_op->write_iter)
595                 ret = new_sync_write(file, buf, count, pos);
596         else
597                 ret = -EINVAL;
598         if (ret > 0) {
599                 fsnotify_modify(file);
600                 add_wchar(current, ret);
601         }
602         inc_syscw(current);
603         file_end_write(file);
604         return ret;
605 }
606
607 /* file_ppos returns &file->f_pos or NULL if file is stream */
608 static inline loff_t *file_ppos(struct file *file)
609 {
610         return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos;
611 }
612
613 ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
614 {
615         struct fd f = fdget_pos(fd);
616         ssize_t ret = -EBADF;
617
618         if (f.file) {
619                 loff_t pos, *ppos = file_ppos(f.file);
620                 if (ppos) {
621                         pos = *ppos;
622                         ppos = &pos;
623                 }
624                 ret = vfs_read(f.file, buf, count, ppos);
625                 if (ret >= 0 && ppos)
626                         f.file->f_pos = pos;
627                 fdput_pos(f);
628         }
629         return ret;
630 }
631
632 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
633 {
634         return ksys_read(fd, buf, count);
635 }
636
637 ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
638 {
639         struct fd f = fdget_pos(fd);
640         ssize_t ret = -EBADF;
641
642         if (f.file) {
643                 loff_t pos, *ppos = file_ppos(f.file);
644                 if (ppos) {
645                         pos = *ppos;
646                         ppos = &pos;
647                 }
648                 ret = vfs_write(f.file, buf, count, ppos);
649                 if (ret >= 0 && ppos)
650                         f.file->f_pos = pos;
651                 fdput_pos(f);
652         }
653
654         return ret;
655 }
656
657 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
658                 size_t, count)
659 {
660         return ksys_write(fd, buf, count);
661 }
662
663 ssize_t ksys_pread64(unsigned int fd, char __user *buf, size_t count,
664                      loff_t pos)
665 {
666         struct fd f;
667         ssize_t ret = -EBADF;
668
669         if (pos < 0)
670                 return -EINVAL;
671
672         f = fdget(fd);
673         if (f.file) {
674                 ret = -ESPIPE;
675                 if (f.file->f_mode & FMODE_PREAD)
676                         ret = vfs_read(f.file, buf, count, &pos);
677                 fdput(f);
678         }
679
680         return ret;
681 }
682
683 SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
684                         size_t, count, loff_t, pos)
685 {
686         return ksys_pread64(fd, buf, count, pos);
687 }
688
689 ssize_t ksys_pwrite64(unsigned int fd, const char __user *buf,
690                       size_t count, loff_t pos)
691 {
692         struct fd f;
693         ssize_t ret = -EBADF;
694
695         if (pos < 0)
696                 return -EINVAL;
697
698         f = fdget(fd);
699         if (f.file) {
700                 ret = -ESPIPE;
701                 if (f.file->f_mode & FMODE_PWRITE)  
702                         ret = vfs_write(f.file, buf, count, &pos);
703                 fdput(f);
704         }
705
706         return ret;
707 }
708
709 SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
710                          size_t, count, loff_t, pos)
711 {
712         return ksys_pwrite64(fd, buf, count, pos);
713 }
714
715 static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
716                 loff_t *ppos, int type, rwf_t flags)
717 {
718         struct kiocb kiocb;
719         ssize_t ret;
720
721         init_sync_kiocb(&kiocb, filp);
722         ret = kiocb_set_rw_flags(&kiocb, flags);
723         if (ret)
724                 return ret;
725         kiocb.ki_pos = (ppos ? *ppos : 0);
726
727         if (type == READ)
728                 ret = call_read_iter(filp, &kiocb, iter);
729         else
730                 ret = call_write_iter(filp, &kiocb, iter);
731         BUG_ON(ret == -EIOCBQUEUED);
732         if (ppos)
733                 *ppos = kiocb.ki_pos;
734         return ret;
735 }
736
737 /* Do it by hand, with file-ops */
738 static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
739                 loff_t *ppos, int type, rwf_t flags)
740 {
741         ssize_t ret = 0;
742
743         if (flags & ~RWF_HIPRI)
744                 return -EOPNOTSUPP;
745
746         while (iov_iter_count(iter)) {
747                 struct iovec iovec = iov_iter_iovec(iter);
748                 ssize_t nr;
749
750                 if (type == READ) {
751                         nr = filp->f_op->read(filp, iovec.iov_base,
752                                               iovec.iov_len, ppos);
753                 } else {
754                         nr = filp->f_op->write(filp, iovec.iov_base,
755                                                iovec.iov_len, ppos);
756                 }
757
758                 if (nr < 0) {
759                         if (!ret)
760                                 ret = nr;
761                         break;
762                 }
763                 ret += nr;
764                 if (nr != iovec.iov_len)
765                         break;
766                 iov_iter_advance(iter, nr);
767         }
768
769         return ret;
770 }
771
772 /**
773  * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace
774  *     into the kernel and check that it is valid.
775  *
776  * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE.
777  * @uvector: Pointer to the userspace array.
778  * @nr_segs: Number of elements in userspace array.
779  * @fast_segs: Number of elements in @fast_pointer.
780  * @fast_pointer: Pointer to (usually small on-stack) kernel array.
781  * @ret_pointer: (output parameter) Pointer to a variable that will point to
782  *     either @fast_pointer, a newly allocated kernel array, or NULL,
783  *     depending on which array was used.
784  *
785  * This function copies an array of &struct iovec of @nr_segs from
786  * userspace into the kernel and checks that each element is valid (e.g.
787  * it does not point to a kernel address or cause overflow by being too
788  * large, etc.).
789  *
790  * As an optimization, the caller may provide a pointer to a small
791  * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long
792  * (the size of this array, or 0 if unused, should be given in @fast_segs).
793  *
794  * @ret_pointer will always point to the array that was used, so the
795  * caller must take care not to call kfree() on it e.g. in case the
796  * @fast_pointer array was used and it was allocated on the stack.
797  *
798  * Return: The total number of bytes covered by the iovec array on success
799  *   or a negative error code on error.
800  */
801 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
802                               unsigned long nr_segs, unsigned long fast_segs,
803                               struct iovec *fast_pointer,
804                               struct iovec **ret_pointer)
805 {
806         unsigned long seg;
807         ssize_t ret;
808         struct iovec *iov = fast_pointer;
809
810         /*
811          * SuS says "The readv() function *may* fail if the iovcnt argument
812          * was less than or equal to 0, or greater than {IOV_MAX}.  Linux has
813          * traditionally returned zero for zero segments, so...
814          */
815         if (nr_segs == 0) {
816                 ret = 0;
817                 goto out;
818         }
819
820         /*
821          * First get the "struct iovec" from user memory and
822          * verify all the pointers
823          */
824         if (nr_segs > UIO_MAXIOV) {
825                 ret = -EINVAL;
826                 goto out;
827         }
828         if (nr_segs > fast_segs) {
829                 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
830                 if (iov == NULL) {
831                         ret = -ENOMEM;
832                         goto out;
833                 }
834         }
835         if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
836                 ret = -EFAULT;
837                 goto out;
838         }
839
840         /*
841          * According to the Single Unix Specification we should return EINVAL
842          * if an element length is < 0 when cast to ssize_t or if the
843          * total length would overflow the ssize_t return value of the
844          * system call.
845          *
846          * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
847          * overflow case.
848          */
849         ret = 0;
850         for (seg = 0; seg < nr_segs; seg++) {
851                 void __user *buf = iov[seg].iov_base;
852                 ssize_t len = (ssize_t)iov[seg].iov_len;
853
854                 /* see if we we're about to use an invalid len or if
855                  * it's about to overflow ssize_t */
856                 if (len < 0) {
857                         ret = -EINVAL;
858                         goto out;
859                 }
860                 if (type >= 0
861                     && unlikely(!access_ok(buf, len))) {
862                         ret = -EFAULT;
863                         goto out;
864                 }
865                 if (len > MAX_RW_COUNT - ret) {
866                         len = MAX_RW_COUNT - ret;
867                         iov[seg].iov_len = len;
868                 }
869                 ret += len;
870         }
871 out:
872         *ret_pointer = iov;
873         return ret;
874 }
875
876 #ifdef CONFIG_COMPAT
877 ssize_t compat_rw_copy_check_uvector(int type,
878                 const struct compat_iovec __user *uvector, unsigned long nr_segs,
879                 unsigned long fast_segs, struct iovec *fast_pointer,
880                 struct iovec **ret_pointer)
881 {
882         compat_ssize_t tot_len;
883         struct iovec *iov = *ret_pointer = fast_pointer;
884         ssize_t ret = 0;
885         int seg;
886
887         /*
888          * SuS says "The readv() function *may* fail if the iovcnt argument
889          * was less than or equal to 0, or greater than {IOV_MAX}.  Linux has
890          * traditionally returned zero for zero segments, so...
891          */
892         if (nr_segs == 0)
893                 goto out;
894
895         ret = -EINVAL;
896         if (nr_segs > UIO_MAXIOV)
897                 goto out;
898         if (nr_segs > fast_segs) {
899                 ret = -ENOMEM;
900                 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
901                 if (iov == NULL)
902                         goto out;
903         }
904         *ret_pointer = iov;
905
906         ret = -EFAULT;
907         if (!access_ok(uvector, nr_segs*sizeof(*uvector)))
908                 goto out;
909
910         /*
911          * Single unix specification:
912          * We should -EINVAL if an element length is not >= 0 and fitting an
913          * ssize_t.
914          *
915          * In Linux, the total length is limited to MAX_RW_COUNT, there is
916          * no overflow possibility.
917          */
918         tot_len = 0;
919         ret = -EINVAL;
920         for (seg = 0; seg < nr_segs; seg++) {
921                 compat_uptr_t buf;
922                 compat_ssize_t len;
923
924                 if (__get_user(len, &uvector->iov_len) ||
925                    __get_user(buf, &uvector->iov_base)) {
926                         ret = -EFAULT;
927                         goto out;
928                 }
929                 if (len < 0)    /* size_t not fitting in compat_ssize_t .. */
930                         goto out;
931                 if (type >= 0 &&
932                     !access_ok(compat_ptr(buf), len)) {
933                         ret = -EFAULT;
934                         goto out;
935                 }
936                 if (len > MAX_RW_COUNT - tot_len)
937                         len = MAX_RW_COUNT - tot_len;
938                 tot_len += len;
939                 iov->iov_base = compat_ptr(buf);
940                 iov->iov_len = (compat_size_t) len;
941                 uvector++;
942                 iov++;
943         }
944         ret = tot_len;
945
946 out:
947         return ret;
948 }
949 #endif
950
951 static ssize_t do_iter_read(struct file *file, struct iov_iter *iter,
952                 loff_t *pos, rwf_t flags)
953 {
954         size_t tot_len;
955         ssize_t ret = 0;
956
957         if (!(file->f_mode & FMODE_READ))
958                 return -EBADF;
959         if (!(file->f_mode & FMODE_CAN_READ))
960                 return -EINVAL;
961
962         tot_len = iov_iter_count(iter);
963         if (!tot_len)
964                 goto out;
965         ret = rw_verify_area(READ, file, pos, tot_len);
966         if (ret < 0)
967                 return ret;
968
969         if (file->f_op->read_iter)
970                 ret = do_iter_readv_writev(file, iter, pos, READ, flags);
971         else
972                 ret = do_loop_readv_writev(file, iter, pos, READ, flags);
973 out:
974         if (ret >= 0)
975                 fsnotify_access(file);
976         return ret;
977 }
978
979 ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb,
980                            struct iov_iter *iter)
981 {
982         size_t tot_len;
983         ssize_t ret = 0;
984
985         if (!file->f_op->read_iter)
986                 return -EINVAL;
987         if (!(file->f_mode & FMODE_READ))
988                 return -EBADF;
989         if (!(file->f_mode & FMODE_CAN_READ))
990                 return -EINVAL;
991
992         tot_len = iov_iter_count(iter);
993         if (!tot_len)
994                 goto out;
995         ret = rw_verify_area(READ, file, &iocb->ki_pos, tot_len);
996         if (ret < 0)
997                 return ret;
998
999         ret = call_read_iter(file, iocb, iter);
1000 out:
1001         if (ret >= 0)
1002                 fsnotify_access(file);
1003         return ret;
1004 }
1005 EXPORT_SYMBOL(vfs_iocb_iter_read);
1006
1007 ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
1008                 rwf_t flags)
1009 {
1010         if (!file->f_op->read_iter)
1011                 return -EINVAL;
1012         return do_iter_read(file, iter, ppos, flags);
1013 }
1014 EXPORT_SYMBOL(vfs_iter_read);
1015
1016 static ssize_t do_iter_write(struct file *file, struct iov_iter *iter,
1017                 loff_t *pos, rwf_t flags)
1018 {
1019         size_t tot_len;
1020         ssize_t ret = 0;
1021
1022         if (!(file->f_mode & FMODE_WRITE))
1023                 return -EBADF;
1024         if (!(file->f_mode & FMODE_CAN_WRITE))
1025                 return -EINVAL;
1026
1027         tot_len = iov_iter_count(iter);
1028         if (!tot_len)
1029                 return 0;
1030         ret = rw_verify_area(WRITE, file, pos, tot_len);
1031         if (ret < 0)
1032                 return ret;
1033
1034         if (file->f_op->write_iter)
1035                 ret = do_iter_readv_writev(file, iter, pos, WRITE, flags);
1036         else
1037                 ret = do_loop_readv_writev(file, iter, pos, WRITE, flags);
1038         if (ret > 0)
1039                 fsnotify_modify(file);
1040         return ret;
1041 }
1042
1043 ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
1044                             struct iov_iter *iter)
1045 {
1046         size_t tot_len;
1047         ssize_t ret = 0;
1048
1049         if (!file->f_op->write_iter)
1050                 return -EINVAL;
1051         if (!(file->f_mode & FMODE_WRITE))
1052                 return -EBADF;
1053         if (!(file->f_mode & FMODE_CAN_WRITE))
1054                 return -EINVAL;
1055
1056         tot_len = iov_iter_count(iter);
1057         if (!tot_len)
1058                 return 0;
1059         ret = rw_verify_area(WRITE, file, &iocb->ki_pos, tot_len);
1060         if (ret < 0)
1061                 return ret;
1062
1063         ret = call_write_iter(file, iocb, iter);
1064         if (ret > 0)
1065                 fsnotify_modify(file);
1066
1067         return ret;
1068 }
1069 EXPORT_SYMBOL(vfs_iocb_iter_write);
1070
1071 ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
1072                 rwf_t flags)
1073 {
1074         if (!file->f_op->write_iter)
1075                 return -EINVAL;
1076         return do_iter_write(file, iter, ppos, flags);
1077 }
1078 EXPORT_SYMBOL(vfs_iter_write);
1079
1080 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
1081                   unsigned long vlen, loff_t *pos, rwf_t flags)
1082 {
1083         struct iovec iovstack[UIO_FASTIOV];
1084         struct iovec *iov = iovstack;
1085         struct iov_iter iter;
1086         ssize_t ret;
1087
1088         ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
1089         if (ret >= 0) {
1090                 ret = do_iter_read(file, &iter, pos, flags);
1091                 kfree(iov);
1092         }
1093
1094         return ret;
1095 }
1096
1097 static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
1098                    unsigned long vlen, loff_t *pos, rwf_t flags)
1099 {
1100         struct iovec iovstack[UIO_FASTIOV];
1101         struct iovec *iov = iovstack;
1102         struct iov_iter iter;
1103         ssize_t ret;
1104
1105         ret = import_iovec(WRITE, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
1106         if (ret >= 0) {
1107                 file_start_write(file);
1108                 ret = do_iter_write(file, &iter, pos, flags);
1109                 file_end_write(file);
1110                 kfree(iov);
1111         }
1112         return ret;
1113 }
1114
1115 static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
1116                         unsigned long vlen, rwf_t flags)
1117 {
1118         struct fd f = fdget_pos(fd);
1119         ssize_t ret = -EBADF;
1120
1121         if (f.file) {
1122                 loff_t pos, *ppos = file_ppos(f.file);
1123                 if (ppos) {
1124                         pos = *ppos;
1125                         ppos = &pos;
1126                 }
1127                 ret = vfs_readv(f.file, vec, vlen, ppos, flags);
1128                 if (ret >= 0 && ppos)
1129                         f.file->f_pos = pos;
1130                 fdput_pos(f);
1131         }
1132
1133         if (ret > 0)
1134                 add_rchar(current, ret);
1135         inc_syscr(current);
1136         return ret;
1137 }
1138
1139 static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
1140                          unsigned long vlen, rwf_t flags)
1141 {
1142         struct fd f = fdget_pos(fd);
1143         ssize_t ret = -EBADF;
1144
1145         if (f.file) {
1146                 loff_t pos, *ppos = file_ppos(f.file);
1147                 if (ppos) {
1148                         pos = *ppos;
1149                         ppos = &pos;
1150                 }
1151                 ret = vfs_writev(f.file, vec, vlen, ppos, flags);
1152                 if (ret >= 0 && ppos)
1153                         f.file->f_pos = pos;
1154                 fdput_pos(f);
1155         }
1156
1157         if (ret > 0)
1158                 add_wchar(current, ret);
1159         inc_syscw(current);
1160         return ret;
1161 }
1162
1163 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
1164 {
1165 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
1166         return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
1167 }
1168
1169 static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
1170                          unsigned long vlen, loff_t pos, rwf_t flags)
1171 {
1172         struct fd f;
1173         ssize_t ret = -EBADF;
1174
1175         if (pos < 0)
1176                 return -EINVAL;
1177
1178         f = fdget(fd);
1179         if (f.file) {
1180                 ret = -ESPIPE;
1181                 if (f.file->f_mode & FMODE_PREAD)
1182                         ret = vfs_readv(f.file, vec, vlen, &pos, flags);
1183                 fdput(f);
1184         }
1185
1186         if (ret > 0)
1187                 add_rchar(current, ret);
1188         inc_syscr(current);
1189         return ret;
1190 }
1191
1192 static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
1193                           unsigned long vlen, loff_t pos, rwf_t flags)
1194 {
1195         struct fd f;
1196         ssize_t ret = -EBADF;
1197
1198         if (pos < 0)
1199                 return -EINVAL;
1200
1201         f = fdget(fd);
1202         if (f.file) {
1203                 ret = -ESPIPE;
1204                 if (f.file->f_mode & FMODE_PWRITE)
1205                         ret = vfs_writev(f.file, vec, vlen, &pos, flags);
1206                 fdput(f);
1207         }
1208
1209         if (ret > 0)
1210                 add_wchar(current, ret);
1211         inc_syscw(current);
1212         return ret;
1213 }
1214
1215 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
1216                 unsigned long, vlen)
1217 {
1218         return do_readv(fd, vec, vlen, 0);
1219 }
1220
1221 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
1222                 unsigned long, vlen)
1223 {
1224         return do_writev(fd, vec, vlen, 0);
1225 }
1226
1227 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
1228                 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1229 {
1230         loff_t pos = pos_from_hilo(pos_h, pos_l);
1231
1232         return do_preadv(fd, vec, vlen, pos, 0);
1233 }
1234
1235 SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
1236                 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1237                 rwf_t, flags)
1238 {
1239         loff_t pos = pos_from_hilo(pos_h, pos_l);
1240
1241         if (pos == -1)
1242                 return do_readv(fd, vec, vlen, flags);
1243
1244         return do_preadv(fd, vec, vlen, pos, flags);
1245 }
1246
1247 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
1248                 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1249 {
1250         loff_t pos = pos_from_hilo(pos_h, pos_l);
1251
1252         return do_pwritev(fd, vec, vlen, pos, 0);
1253 }
1254
1255 SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
1256                 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1257                 rwf_t, flags)
1258 {
1259         loff_t pos = pos_from_hilo(pos_h, pos_l);
1260
1261         if (pos == -1)
1262                 return do_writev(fd, vec, vlen, flags);
1263
1264         return do_pwritev(fd, vec, vlen, pos, flags);
1265 }
1266
1267 #ifdef CONFIG_COMPAT
1268 static size_t compat_readv(struct file *file,
1269                            const struct compat_iovec __user *vec,
1270                            unsigned long vlen, loff_t *pos, rwf_t flags)
1271 {
1272         struct iovec iovstack[UIO_FASTIOV];
1273         struct iovec *iov = iovstack;
1274         struct iov_iter iter;
1275         ssize_t ret;
1276
1277         ret = compat_import_iovec(READ, vec, vlen, UIO_FASTIOV, &iov, &iter);
1278         if (ret >= 0) {
1279                 ret = do_iter_read(file, &iter, pos, flags);
1280                 kfree(iov);
1281         }
1282         if (ret > 0)
1283                 add_rchar(current, ret);
1284         inc_syscr(current);
1285         return ret;
1286 }
1287
1288 static size_t do_compat_readv(compat_ulong_t fd,
1289                                  const struct compat_iovec __user *vec,
1290                                  compat_ulong_t vlen, rwf_t flags)
1291 {
1292         struct fd f = fdget_pos(fd);
1293         ssize_t ret;
1294         loff_t pos;
1295
1296         if (!f.file)
1297                 return -EBADF;
1298         pos = f.file->f_pos;
1299         ret = compat_readv(f.file, vec, vlen, &pos, flags);
1300         if (ret >= 0)
1301                 f.file->f_pos = pos;
1302         fdput_pos(f);
1303         return ret;
1304
1305 }
1306
1307 COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
1308                 const struct compat_iovec __user *,vec,
1309                 compat_ulong_t, vlen)
1310 {
1311         return do_compat_readv(fd, vec, vlen, 0);
1312 }
1313
1314 static long do_compat_preadv64(unsigned long fd,
1315                                   const struct compat_iovec __user *vec,
1316                                   unsigned long vlen, loff_t pos, rwf_t flags)
1317 {
1318         struct fd f;
1319         ssize_t ret;
1320
1321         if (pos < 0)
1322                 return -EINVAL;
1323         f = fdget(fd);
1324         if (!f.file)
1325                 return -EBADF;
1326         ret = -ESPIPE;
1327         if (f.file->f_mode & FMODE_PREAD)
1328                 ret = compat_readv(f.file, vec, vlen, &pos, flags);
1329         fdput(f);
1330         return ret;
1331 }
1332
1333 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1334 COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1335                 const struct compat_iovec __user *,vec,
1336                 unsigned long, vlen, loff_t, pos)
1337 {
1338         return do_compat_preadv64(fd, vec, vlen, pos, 0);
1339 }
1340 #endif
1341
1342 COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
1343                 const struct compat_iovec __user *,vec,
1344                 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1345 {
1346         loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1347
1348         return do_compat_preadv64(fd, vec, vlen, pos, 0);
1349 }
1350
1351 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
1352 COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
1353                 const struct compat_iovec __user *,vec,
1354                 unsigned long, vlen, loff_t, pos, rwf_t, flags)
1355 {
1356         if (pos == -1)
1357                 return do_compat_readv(fd, vec, vlen, flags);
1358
1359         return do_compat_preadv64(fd, vec, vlen, pos, flags);
1360 }
1361 #endif
1362
1363 COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
1364                 const struct compat_iovec __user *,vec,
1365                 compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
1366                 rwf_t, flags)
1367 {
1368         loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1369
1370         if (pos == -1)
1371                 return do_compat_readv(fd, vec, vlen, flags);
1372
1373         return do_compat_preadv64(fd, vec, vlen, pos, flags);
1374 }
1375
1376 static size_t compat_writev(struct file *file,
1377                             const struct compat_iovec __user *vec,
1378                             unsigned long vlen, loff_t *pos, rwf_t flags)
1379 {
1380         struct iovec iovstack[UIO_FASTIOV];
1381         struct iovec *iov = iovstack;
1382         struct iov_iter iter;
1383         ssize_t ret;
1384
1385         ret = compat_import_iovec(WRITE, vec, vlen, UIO_FASTIOV, &iov, &iter);
1386         if (ret >= 0) {
1387                 file_start_write(file);
1388                 ret = do_iter_write(file, &iter, pos, flags);
1389                 file_end_write(file);
1390                 kfree(iov);
1391         }
1392         if (ret > 0)
1393                 add_wchar(current, ret);
1394         inc_syscw(current);
1395         return ret;
1396 }
1397
1398 static size_t do_compat_writev(compat_ulong_t fd,
1399                                   const struct compat_iovec __user* vec,
1400                                   compat_ulong_t vlen, rwf_t flags)
1401 {
1402         struct fd f = fdget_pos(fd);
1403         ssize_t ret;
1404         loff_t pos;
1405
1406         if (!f.file)
1407                 return -EBADF;
1408         pos = f.file->f_pos;
1409         ret = compat_writev(f.file, vec, vlen, &pos, flags);
1410         if (ret >= 0)
1411                 f.file->f_pos = pos;
1412         fdput_pos(f);
1413         return ret;
1414 }
1415
1416 COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
1417                 const struct compat_iovec __user *, vec,
1418                 compat_ulong_t, vlen)
1419 {
1420         return do_compat_writev(fd, vec, vlen, 0);
1421 }
1422
1423 static long do_compat_pwritev64(unsigned long fd,
1424                                    const struct compat_iovec __user *vec,
1425                                    unsigned long vlen, loff_t pos, rwf_t flags)
1426 {
1427         struct fd f;
1428         ssize_t ret;
1429
1430         if (pos < 0)
1431                 return -EINVAL;
1432         f = fdget(fd);
1433         if (!f.file)
1434                 return -EBADF;
1435         ret = -ESPIPE;
1436         if (f.file->f_mode & FMODE_PWRITE)
1437                 ret = compat_writev(f.file, vec, vlen, &pos, flags);
1438         fdput(f);
1439         return ret;
1440 }
1441
1442 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1443 COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1444                 const struct compat_iovec __user *,vec,
1445                 unsigned long, vlen, loff_t, pos)
1446 {
1447         return do_compat_pwritev64(fd, vec, vlen, pos, 0);
1448 }
1449 #endif
1450
1451 COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
1452                 const struct compat_iovec __user *,vec,
1453                 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1454 {
1455         loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1456
1457         return do_compat_pwritev64(fd, vec, vlen, pos, 0);
1458 }
1459
1460 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
1461 COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
1462                 const struct compat_iovec __user *,vec,
1463                 unsigned long, vlen, loff_t, pos, rwf_t, flags)
1464 {
1465         if (pos == -1)
1466                 return do_compat_writev(fd, vec, vlen, flags);
1467
1468         return do_compat_pwritev64(fd, vec, vlen, pos, flags);
1469 }
1470 #endif
1471
1472 COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
1473                 const struct compat_iovec __user *,vec,
1474                 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags)
1475 {
1476         loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1477
1478         if (pos == -1)
1479                 return do_compat_writev(fd, vec, vlen, flags);
1480
1481         return do_compat_pwritev64(fd, vec, vlen, pos, flags);
1482 }
1483
1484 #endif
1485
1486 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1487                            size_t count, loff_t max)
1488 {
1489         struct fd in, out;
1490         struct inode *in_inode, *out_inode;
1491         loff_t pos;
1492         loff_t out_pos;
1493         ssize_t retval;
1494         int fl;
1495
1496         /*
1497          * Get input file, and verify that it is ok..
1498          */
1499         retval = -EBADF;
1500         in = fdget(in_fd);
1501         if (!in.file)
1502                 goto out;
1503         if (!(in.file->f_mode & FMODE_READ))
1504                 goto fput_in;
1505         retval = -ESPIPE;
1506         if (!ppos) {
1507                 pos = in.file->f_pos;
1508         } else {
1509                 pos = *ppos;
1510                 if (!(in.file->f_mode & FMODE_PREAD))
1511                         goto fput_in;
1512         }
1513         retval = rw_verify_area(READ, in.file, &pos, count);
1514         if (retval < 0)
1515                 goto fput_in;
1516         if (count > MAX_RW_COUNT)
1517                 count =  MAX_RW_COUNT;
1518
1519         /*
1520          * Get output file, and verify that it is ok..
1521          */
1522         retval = -EBADF;
1523         out = fdget(out_fd);
1524         if (!out.file)
1525                 goto fput_in;
1526         if (!(out.file->f_mode & FMODE_WRITE))
1527                 goto fput_out;
1528         in_inode = file_inode(in.file);
1529         out_inode = file_inode(out.file);
1530         out_pos = out.file->f_pos;
1531         retval = rw_verify_area(WRITE, out.file, &out_pos, count);
1532         if (retval < 0)
1533                 goto fput_out;
1534
1535         if (!max)
1536                 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1537
1538         if (unlikely(pos + count > max)) {
1539                 retval = -EOVERFLOW;
1540                 if (pos >= max)
1541                         goto fput_out;
1542                 count = max - pos;
1543         }
1544
1545         fl = 0;
1546 #if 0
1547         /*
1548          * We need to debate whether we can enable this or not. The
1549          * man page documents EAGAIN return for the output at least,
1550          * and the application is arguably buggy if it doesn't expect
1551          * EAGAIN on a non-blocking file descriptor.
1552          */
1553         if (in.file->f_flags & O_NONBLOCK)
1554                 fl = SPLICE_F_NONBLOCK;
1555 #endif
1556         file_start_write(out.file);
1557         retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
1558         file_end_write(out.file);
1559
1560         if (retval > 0) {
1561                 add_rchar(current, retval);
1562                 add_wchar(current, retval);
1563                 fsnotify_access(in.file);
1564                 fsnotify_modify(out.file);
1565                 out.file->f_pos = out_pos;
1566                 if (ppos)
1567                         *ppos = pos;
1568                 else
1569                         in.file->f_pos = pos;
1570         }
1571
1572         inc_syscr(current);
1573         inc_syscw(current);
1574         if (pos > max)
1575                 retval = -EOVERFLOW;
1576
1577 fput_out:
1578         fdput(out);
1579 fput_in:
1580         fdput(in);
1581 out:
1582         return retval;
1583 }
1584
1585 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
1586 {
1587         loff_t pos;
1588         off_t off;
1589         ssize_t ret;
1590
1591         if (offset) {
1592                 if (unlikely(get_user(off, offset)))
1593                         return -EFAULT;
1594                 pos = off;
1595                 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1596                 if (unlikely(put_user(pos, offset)))
1597                         return -EFAULT;
1598                 return ret;
1599         }
1600
1601         return do_sendfile(out_fd, in_fd, NULL, count, 0);
1602 }
1603
1604 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
1605 {
1606         loff_t pos;
1607         ssize_t ret;
1608
1609         if (offset) {
1610                 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1611                         return -EFAULT;
1612                 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1613                 if (unlikely(put_user(pos, offset)))
1614                         return -EFAULT;
1615                 return ret;
1616         }
1617
1618         return do_sendfile(out_fd, in_fd, NULL, count, 0);
1619 }
1620
1621 #ifdef CONFIG_COMPAT
1622 COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1623                 compat_off_t __user *, offset, compat_size_t, count)
1624 {
1625         loff_t pos;
1626         off_t off;
1627         ssize_t ret;
1628
1629         if (offset) {
1630                 if (unlikely(get_user(off, offset)))
1631                         return -EFAULT;
1632                 pos = off;
1633                 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1634                 if (unlikely(put_user(pos, offset)))
1635                         return -EFAULT;
1636                 return ret;
1637         }
1638
1639         return do_sendfile(out_fd, in_fd, NULL, count, 0);
1640 }
1641
1642 COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1643                 compat_loff_t __user *, offset, compat_size_t, count)
1644 {
1645         loff_t pos;
1646         ssize_t ret;
1647
1648         if (offset) {
1649                 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1650                         return -EFAULT;
1651                 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1652                 if (unlikely(put_user(pos, offset)))
1653                         return -EFAULT;
1654                 return ret;
1655         }
1656
1657         return do_sendfile(out_fd, in_fd, NULL, count, 0);
1658 }
1659 #endif
1660
1661 /**
1662  * generic_copy_file_range - copy data between two files
1663  * @file_in:    file structure to read from
1664  * @pos_in:     file offset to read from
1665  * @file_out:   file structure to write data to
1666  * @pos_out:    file offset to write data to
1667  * @len:        amount of data to copy
1668  * @flags:      copy flags
1669  *
1670  * This is a generic filesystem helper to copy data from one file to another.
1671  * It has no constraints on the source or destination file owners - the files
1672  * can belong to different superblocks and different filesystem types. Short
1673  * copies are allowed.
1674  *
1675  * This should be called from the @file_out filesystem, as per the
1676  * ->copy_file_range() method.
1677  *
1678  * Returns the number of bytes copied or a negative error indicating the
1679  * failure.
1680  */
1681
1682 ssize_t generic_copy_file_range(struct file *file_in, loff_t pos_in,
1683                                 struct file *file_out, loff_t pos_out,
1684                                 size_t len, unsigned int flags)
1685 {
1686         return do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1687                                 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
1688 }
1689 EXPORT_SYMBOL(generic_copy_file_range);
1690
1691 static ssize_t do_copy_file_range(struct file *file_in, loff_t pos_in,
1692                                   struct file *file_out, loff_t pos_out,
1693                                   size_t len, unsigned int flags)
1694 {
1695         /*
1696          * Although we now allow filesystems to handle cross sb copy, passing
1697          * a file of the wrong filesystem type to filesystem driver can result
1698          * in an attempt to dereference the wrong type of ->private_data, so
1699          * avoid doing that until we really have a good reason.  NFS defines
1700          * several different file_system_type structures, but they all end up
1701          * using the same ->copy_file_range() function pointer.
1702          */
1703         if (file_out->f_op->copy_file_range &&
1704             file_out->f_op->copy_file_range == file_in->f_op->copy_file_range)
1705                 return file_out->f_op->copy_file_range(file_in, pos_in,
1706                                                        file_out, pos_out,
1707                                                        len, flags);
1708
1709         return generic_copy_file_range(file_in, pos_in, file_out, pos_out, len,
1710                                        flags);
1711 }
1712
1713 /*
1714  * copy_file_range() differs from regular file read and write in that it
1715  * specifically allows return partial success.  When it does so is up to
1716  * the copy_file_range method.
1717  */
1718 ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1719                             struct file *file_out, loff_t pos_out,
1720                             size_t len, unsigned int flags)
1721 {
1722         ssize_t ret;
1723
1724         if (flags != 0)
1725                 return -EINVAL;
1726
1727         ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len,
1728                                        flags);
1729         if (unlikely(ret))
1730                 return ret;
1731
1732         ret = rw_verify_area(READ, file_in, &pos_in, len);
1733         if (unlikely(ret))
1734                 return ret;
1735
1736         ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1737         if (unlikely(ret))
1738                 return ret;
1739
1740         if (len == 0)
1741                 return 0;
1742
1743         file_start_write(file_out);
1744
1745         /*
1746          * Try cloning first, this is supported by more file systems, and
1747          * more efficient if both clone and copy are supported (e.g. NFS).
1748          */
1749         if (file_in->f_op->remap_file_range &&
1750             file_inode(file_in)->i_sb == file_inode(file_out)->i_sb) {
1751                 loff_t cloned;
1752
1753                 cloned = file_in->f_op->remap_file_range(file_in, pos_in,
1754                                 file_out, pos_out,
1755                                 min_t(loff_t, MAX_RW_COUNT, len),
1756                                 REMAP_FILE_CAN_SHORTEN);
1757                 if (cloned > 0) {
1758                         ret = cloned;
1759                         goto done;
1760                 }
1761         }
1762
1763         ret = do_copy_file_range(file_in, pos_in, file_out, pos_out, len,
1764                                 flags);
1765         WARN_ON_ONCE(ret == -EOPNOTSUPP);
1766 done:
1767         if (ret > 0) {
1768                 fsnotify_access(file_in);
1769                 add_rchar(current, ret);
1770                 fsnotify_modify(file_out);
1771                 add_wchar(current, ret);
1772         }
1773
1774         inc_syscr(current);
1775         inc_syscw(current);
1776
1777         file_end_write(file_out);
1778
1779         return ret;
1780 }
1781 EXPORT_SYMBOL(vfs_copy_file_range);
1782
1783 SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1784                 int, fd_out, loff_t __user *, off_out,
1785                 size_t, len, unsigned int, flags)
1786 {
1787         loff_t pos_in;
1788         loff_t pos_out;
1789         struct fd f_in;
1790         struct fd f_out;
1791         ssize_t ret = -EBADF;
1792
1793         f_in = fdget(fd_in);
1794         if (!f_in.file)
1795                 goto out2;
1796
1797         f_out = fdget(fd_out);
1798         if (!f_out.file)
1799                 goto out1;
1800
1801         ret = -EFAULT;
1802         if (off_in) {
1803                 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1804                         goto out;
1805         } else {
1806                 pos_in = f_in.file->f_pos;
1807         }
1808
1809         if (off_out) {
1810                 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1811                         goto out;
1812         } else {
1813                 pos_out = f_out.file->f_pos;
1814         }
1815
1816         ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
1817                                   flags);
1818         if (ret > 0) {
1819                 pos_in += ret;
1820                 pos_out += ret;
1821
1822                 if (off_in) {
1823                         if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1824                                 ret = -EFAULT;
1825                 } else {
1826                         f_in.file->f_pos = pos_in;
1827                 }
1828
1829                 if (off_out) {
1830                         if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1831                                 ret = -EFAULT;
1832                 } else {
1833                         f_out.file->f_pos = pos_out;
1834                 }
1835         }
1836
1837 out:
1838         fdput(f_out);
1839 out1:
1840         fdput(f_in);
1841 out2:
1842         return ret;
1843 }
1844
1845 static int remap_verify_area(struct file *file, loff_t pos, loff_t len,
1846                              bool write)
1847 {
1848         struct inode *inode = file_inode(file);
1849
1850         if (unlikely(pos < 0 || len < 0))
1851                 return -EINVAL;
1852
1853          if (unlikely((loff_t) (pos + len) < 0))
1854                 return -EINVAL;
1855
1856         if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
1857                 loff_t end = len ? pos + len - 1 : OFFSET_MAX;
1858                 int retval;
1859
1860                 retval = locks_mandatory_area(inode, file, pos, end,
1861                                 write ? F_WRLCK : F_RDLCK);
1862                 if (retval < 0)
1863                         return retval;
1864         }
1865
1866         return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
1867 }
1868 /*
1869  * Ensure that we don't remap a partial EOF block in the middle of something
1870  * else.  Assume that the offsets have already been checked for block
1871  * alignment.
1872  *
1873  * For clone we only link a partial EOF block above or at the destination file's
1874  * EOF.  For deduplication we accept a partial EOF block only if it ends at the
1875  * destination file's EOF (can not link it into the middle of a file).
1876  *
1877  * Shorten the request if possible.
1878  */
1879 static int generic_remap_check_len(struct inode *inode_in,
1880                                    struct inode *inode_out,
1881                                    loff_t pos_out,
1882                                    loff_t *len,
1883                                    unsigned int remap_flags)
1884 {
1885         u64 blkmask = i_blocksize(inode_in) - 1;
1886         loff_t new_len = *len;
1887
1888         if ((*len & blkmask) == 0)
1889                 return 0;
1890
1891         if (pos_out + *len < i_size_read(inode_out))
1892                 new_len &= ~blkmask;
1893
1894         if (new_len == *len)
1895                 return 0;
1896
1897         if (remap_flags & REMAP_FILE_CAN_SHORTEN) {
1898                 *len = new_len;
1899                 return 0;
1900         }
1901
1902         return (remap_flags & REMAP_FILE_DEDUP) ? -EBADE : -EINVAL;
1903 }
1904
1905 /* Read a page's worth of file data into the page cache. */
1906 static struct page *vfs_dedupe_get_page(struct inode *inode, loff_t offset)
1907 {
1908         struct page *page;
1909
1910         page = read_mapping_page(inode->i_mapping, offset >> PAGE_SHIFT, NULL);
1911         if (IS_ERR(page))
1912                 return page;
1913         if (!PageUptodate(page)) {
1914                 put_page(page);
1915                 return ERR_PTR(-EIO);
1916         }
1917         return page;
1918 }
1919
1920 /*
1921  * Lock two pages, ensuring that we lock in offset order if the pages are from
1922  * the same file.
1923  */
1924 static void vfs_lock_two_pages(struct page *page1, struct page *page2)
1925 {
1926         /* Always lock in order of increasing index. */
1927         if (page1->index > page2->index)
1928                 swap(page1, page2);
1929
1930         lock_page(page1);
1931         if (page1 != page2)
1932                 lock_page(page2);
1933 }
1934
1935 /* Unlock two pages, being careful not to unlock the same page twice. */
1936 static void vfs_unlock_two_pages(struct page *page1, struct page *page2)
1937 {
1938         unlock_page(page1);
1939         if (page1 != page2)
1940                 unlock_page(page2);
1941 }
1942
1943 /*
1944  * Compare extents of two files to see if they are the same.
1945  * Caller must have locked both inodes to prevent write races.
1946  */
1947 static int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff,
1948                                          struct inode *dest, loff_t destoff,
1949                                          loff_t len, bool *is_same)
1950 {
1951         loff_t src_poff;
1952         loff_t dest_poff;
1953         void *src_addr;
1954         void *dest_addr;
1955         struct page *src_page;
1956         struct page *dest_page;
1957         loff_t cmp_len;
1958         bool same;
1959         int error;
1960
1961         error = -EINVAL;
1962         same = true;
1963         while (len) {
1964                 src_poff = srcoff & (PAGE_SIZE - 1);
1965                 dest_poff = destoff & (PAGE_SIZE - 1);
1966                 cmp_len = min(PAGE_SIZE - src_poff,
1967                               PAGE_SIZE - dest_poff);
1968                 cmp_len = min(cmp_len, len);
1969                 if (cmp_len <= 0)
1970                         goto out_error;
1971
1972                 src_page = vfs_dedupe_get_page(src, srcoff);
1973                 if (IS_ERR(src_page)) {
1974                         error = PTR_ERR(src_page);
1975                         goto out_error;
1976                 }
1977                 dest_page = vfs_dedupe_get_page(dest, destoff);
1978                 if (IS_ERR(dest_page)) {
1979                         error = PTR_ERR(dest_page);
1980                         put_page(src_page);
1981                         goto out_error;
1982                 }
1983
1984                 vfs_lock_two_pages(src_page, dest_page);
1985
1986                 /*
1987                  * Now that we've locked both pages, make sure they're still
1988                  * mapped to the file data we're interested in.  If not,
1989                  * someone is invalidating pages on us and we lose.
1990                  */
1991                 if (!PageUptodate(src_page) || !PageUptodate(dest_page) ||
1992                     src_page->mapping != src->i_mapping ||
1993                     dest_page->mapping != dest->i_mapping) {
1994                         same = false;
1995                         goto unlock;
1996                 }
1997
1998                 src_addr = kmap_atomic(src_page);
1999                 dest_addr = kmap_atomic(dest_page);
2000
2001                 flush_dcache_page(src_page);
2002                 flush_dcache_page(dest_page);
2003
2004                 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
2005                         same = false;
2006
2007                 kunmap_atomic(dest_addr);
2008                 kunmap_atomic(src_addr);
2009 unlock:
2010                 vfs_unlock_two_pages(src_page, dest_page);
2011                 put_page(dest_page);
2012                 put_page(src_page);
2013
2014                 if (!same)
2015                         break;
2016
2017                 srcoff += cmp_len;
2018                 destoff += cmp_len;
2019                 len -= cmp_len;
2020         }
2021
2022         *is_same = same;
2023         return 0;
2024
2025 out_error:
2026         return error;
2027 }
2028
2029 /*
2030  * Check that the two inodes are eligible for cloning, the ranges make
2031  * sense, and then flush all dirty data.  Caller must ensure that the
2032  * inodes have been locked against any other modifications.
2033  *
2034  * If there's an error, then the usual negative error code is returned.
2035  * Otherwise returns 0 with *len set to the request length.
2036  */
2037 int generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
2038                                   struct file *file_out, loff_t pos_out,
2039                                   loff_t *len, unsigned int remap_flags)
2040 {
2041         struct inode *inode_in = file_inode(file_in);
2042         struct inode *inode_out = file_inode(file_out);
2043         bool same_inode = (inode_in == inode_out);
2044         int ret;
2045
2046         /* Don't touch certain kinds of inodes */
2047         if (IS_IMMUTABLE(inode_out))
2048                 return -EPERM;
2049
2050         if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
2051                 return -ETXTBSY;
2052
2053         /* Don't reflink dirs, pipes, sockets... */
2054         if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
2055                 return -EISDIR;
2056         if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
2057                 return -EINVAL;
2058
2059         /* Zero length dedupe exits immediately; reflink goes to EOF. */
2060         if (*len == 0) {
2061                 loff_t isize = i_size_read(inode_in);
2062
2063                 if ((remap_flags & REMAP_FILE_DEDUP) || pos_in == isize)
2064                         return 0;
2065                 if (pos_in > isize)
2066                         return -EINVAL;
2067                 *len = isize - pos_in;
2068                 if (*len == 0)
2069                         return 0;
2070         }
2071
2072         /* Check that we don't violate system file offset limits. */
2073         ret = generic_remap_checks(file_in, pos_in, file_out, pos_out, len,
2074                         remap_flags);
2075         if (ret)
2076                 return ret;
2077
2078         /* Wait for the completion of any pending IOs on both files */
2079         inode_dio_wait(inode_in);
2080         if (!same_inode)
2081                 inode_dio_wait(inode_out);
2082
2083         ret = filemap_write_and_wait_range(inode_in->i_mapping,
2084                         pos_in, pos_in + *len - 1);
2085         if (ret)
2086                 return ret;
2087
2088         ret = filemap_write_and_wait_range(inode_out->i_mapping,
2089                         pos_out, pos_out + *len - 1);
2090         if (ret)
2091                 return ret;
2092
2093         /*
2094          * Check that the extents are the same.
2095          */
2096         if (remap_flags & REMAP_FILE_DEDUP) {
2097                 bool            is_same = false;
2098
2099                 ret = vfs_dedupe_file_range_compare(inode_in, pos_in,
2100                                 inode_out, pos_out, *len, &is_same);
2101                 if (ret)
2102                         return ret;
2103                 if (!is_same)
2104                         return -EBADE;
2105         }
2106
2107         ret = generic_remap_check_len(inode_in, inode_out, pos_out, len,
2108                         remap_flags);
2109         if (ret)
2110                 return ret;
2111
2112         /* If can't alter the file contents, we're done. */
2113         if (!(remap_flags & REMAP_FILE_DEDUP))
2114                 ret = file_modified(file_out);
2115
2116         return ret;
2117 }
2118 EXPORT_SYMBOL(generic_remap_file_range_prep);
2119
2120 loff_t do_clone_file_range(struct file *file_in, loff_t pos_in,
2121                            struct file *file_out, loff_t pos_out,
2122                            loff_t len, unsigned int remap_flags)
2123 {
2124         loff_t ret;
2125
2126         WARN_ON_ONCE(remap_flags & REMAP_FILE_DEDUP);
2127
2128         /*
2129          * FICLONE/FICLONERANGE ioctls enforce that src and dest files are on
2130          * the same mount. Practically, they only need to be on the same file
2131          * system.
2132          */
2133         if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
2134                 return -EXDEV;
2135
2136         ret = generic_file_rw_checks(file_in, file_out);
2137         if (ret < 0)
2138                 return ret;
2139
2140         if (!file_in->f_op->remap_file_range)
2141                 return -EOPNOTSUPP;
2142
2143         ret = remap_verify_area(file_in, pos_in, len, false);
2144         if (ret)
2145                 return ret;
2146
2147         ret = remap_verify_area(file_out, pos_out, len, true);
2148         if (ret)
2149                 return ret;
2150
2151         ret = file_in->f_op->remap_file_range(file_in, pos_in,
2152                         file_out, pos_out, len, remap_flags);
2153         if (ret < 0)
2154                 return ret;
2155
2156         fsnotify_access(file_in);
2157         fsnotify_modify(file_out);
2158         return ret;
2159 }
2160 EXPORT_SYMBOL(do_clone_file_range);
2161
2162 loff_t vfs_clone_file_range(struct file *file_in, loff_t pos_in,
2163                             struct file *file_out, loff_t pos_out,
2164                             loff_t len, unsigned int remap_flags)
2165 {
2166         loff_t ret;
2167
2168         file_start_write(file_out);
2169         ret = do_clone_file_range(file_in, pos_in, file_out, pos_out, len,
2170                                   remap_flags);
2171         file_end_write(file_out);
2172
2173         return ret;
2174 }
2175 EXPORT_SYMBOL(vfs_clone_file_range);
2176
2177 /* Check whether we are allowed to dedupe the destination file */
2178 static bool allow_file_dedupe(struct file *file)
2179 {
2180         if (capable(CAP_SYS_ADMIN))
2181                 return true;
2182         if (file->f_mode & FMODE_WRITE)
2183                 return true;
2184         if (uid_eq(current_fsuid(), file_inode(file)->i_uid))
2185                 return true;
2186         if (!inode_permission(file_inode(file), MAY_WRITE))
2187                 return true;
2188         return false;
2189 }
2190
2191 loff_t vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos,
2192                                  struct file *dst_file, loff_t dst_pos,
2193                                  loff_t len, unsigned int remap_flags)
2194 {
2195         loff_t ret;
2196
2197         WARN_ON_ONCE(remap_flags & ~(REMAP_FILE_DEDUP |
2198                                      REMAP_FILE_CAN_SHORTEN));
2199
2200         ret = mnt_want_write_file(dst_file);
2201         if (ret)
2202                 return ret;
2203
2204         ret = remap_verify_area(dst_file, dst_pos, len, true);
2205         if (ret < 0)
2206                 goto out_drop_write;
2207
2208         ret = -EPERM;
2209         if (!allow_file_dedupe(dst_file))
2210                 goto out_drop_write;
2211
2212         ret = -EXDEV;
2213         if (src_file->f_path.mnt != dst_file->f_path.mnt)
2214                 goto out_drop_write;
2215
2216         ret = -EISDIR;
2217         if (S_ISDIR(file_inode(dst_file)->i_mode))
2218                 goto out_drop_write;
2219
2220         ret = -EINVAL;
2221         if (!dst_file->f_op->remap_file_range)
2222                 goto out_drop_write;
2223
2224         if (len == 0) {
2225                 ret = 0;
2226                 goto out_drop_write;
2227         }
2228
2229         ret = dst_file->f_op->remap_file_range(src_file, src_pos, dst_file,
2230                         dst_pos, len, remap_flags | REMAP_FILE_DEDUP);
2231 out_drop_write:
2232         mnt_drop_write_file(dst_file);
2233
2234         return ret;
2235 }
2236 EXPORT_SYMBOL(vfs_dedupe_file_range_one);
2237
2238 int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
2239 {
2240         struct file_dedupe_range_info *info;
2241         struct inode *src = file_inode(file);
2242         u64 off;
2243         u64 len;
2244         int i;
2245         int ret;
2246         u16 count = same->dest_count;
2247         loff_t deduped;
2248
2249         if (!(file->f_mode & FMODE_READ))
2250                 return -EINVAL;
2251
2252         if (same->reserved1 || same->reserved2)
2253                 return -EINVAL;
2254
2255         off = same->src_offset;
2256         len = same->src_length;
2257
2258         if (S_ISDIR(src->i_mode))
2259                 return -EISDIR;
2260
2261         if (!S_ISREG(src->i_mode))
2262                 return -EINVAL;
2263
2264         if (!file->f_op->remap_file_range)
2265                 return -EOPNOTSUPP;
2266
2267         ret = remap_verify_area(file, off, len, false);
2268         if (ret < 0)
2269                 return ret;
2270         ret = 0;
2271
2272         if (off + len > i_size_read(src))
2273                 return -EINVAL;
2274
2275         /* Arbitrary 1G limit on a single dedupe request, can be raised. */
2276         len = min_t(u64, len, 1 << 30);
2277
2278         /* pre-format output fields to sane values */
2279         for (i = 0; i < count; i++) {
2280                 same->info[i].bytes_deduped = 0ULL;
2281                 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
2282         }
2283
2284         for (i = 0, info = same->info; i < count; i++, info++) {
2285                 struct fd dst_fd = fdget(info->dest_fd);
2286                 struct file *dst_file = dst_fd.file;
2287
2288                 if (!dst_file) {
2289                         info->status = -EBADF;
2290                         goto next_loop;
2291                 }
2292
2293                 if (info->reserved) {
2294                         info->status = -EINVAL;
2295                         goto next_fdput;
2296                 }
2297
2298                 deduped = vfs_dedupe_file_range_one(file, off, dst_file,
2299                                                     info->dest_offset, len,
2300                                                     REMAP_FILE_CAN_SHORTEN);
2301                 if (deduped == -EBADE)
2302                         info->status = FILE_DEDUPE_RANGE_DIFFERS;
2303                 else if (deduped < 0)
2304                         info->status = deduped;
2305                 else
2306                         info->bytes_deduped = len;
2307
2308 next_fdput:
2309                 fdput(dst_fd);
2310 next_loop:
2311                 if (fatal_signal_pending(current))
2312                         break;
2313         }
2314         return ret;
2315 }
2316 EXPORT_SYMBOL(vfs_dedupe_file_range);