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