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