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