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