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