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