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