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