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