]> git.ipfire.org Git - people/ms/linux.git/blame - fs/read_write.c
fs: implement vfs_iter_read using do_iter_read
[people/ms/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
dbe4e192
CH
359ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
360{
361 struct kiocb kiocb;
362 ssize_t ret;
363
364 if (!file->f_op->write_iter)
365 return -EINVAL;
366
367 init_sync_kiocb(&kiocb, file);
368 kiocb.ki_pos = *ppos;
dbe4e192
CH
369
370 iter->type |= WRITE;
bb7462b6 371 ret = call_write_iter(file, &kiocb, iter);
599bd19b 372 BUG_ON(ret == -EIOCBQUEUED);
dbe4e192
CH
373 if (ret > 0)
374 *ppos = kiocb.ki_pos;
375 return ret;
376}
377EXPORT_SYMBOL(vfs_iter_write);
378
68d70d03 379int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
1da177e4
LT
380{
381 struct inode *inode;
382 loff_t pos;
c43e259c 383 int retval = -EINVAL;
1da177e4 384
496ad9aa 385 inode = file_inode(file);
e28cc715 386 if (unlikely((ssize_t) count < 0))
c43e259c 387 return retval;
1da177e4 388 pos = *ppos;
cccb5a1e
AV
389 if (unlikely(pos < 0)) {
390 if (!unsigned_offsets(file))
391 return retval;
392 if (count >= -pos) /* both values are in 0..LLONG_MAX */
393 return -EOVERFLOW;
394 } else if (unlikely((loff_t) (pos + count) < 0)) {
395 if (!unsigned_offsets(file))
4a3956c7
KH
396 return retval;
397 }
1da177e4 398
bd61e0a9 399 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
acc15575
CH
400 retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
401 read_write == READ ? F_RDLCK : F_WRLCK);
e28cc715
LT
402 if (retval < 0)
403 return retval;
404 }
bc61384d 405 return security_file_permission(file,
c43e259c 406 read_write == READ ? MAY_READ : MAY_WRITE);
1da177e4
LT
407}
408
5d5d5689 409static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
293bc982
AV
410{
411 struct iovec iov = { .iov_base = buf, .iov_len = len };
412 struct kiocb kiocb;
413 struct iov_iter iter;
414 ssize_t ret;
415
416 init_sync_kiocb(&kiocb, filp);
417 kiocb.ki_pos = *ppos;
293bc982
AV
418 iov_iter_init(&iter, READ, &iov, 1, len);
419
bb7462b6 420 ret = call_read_iter(filp, &kiocb, &iter);
599bd19b 421 BUG_ON(ret == -EIOCBQUEUED);
293bc982
AV
422 *ppos = kiocb.ki_pos;
423 return ret;
424}
425
6fb5032e
DK
426ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
427 loff_t *pos)
428{
6fb5032e 429 if (file->f_op->read)
3d04c8a1 430 return file->f_op->read(file, buf, count, pos);
6fb5032e 431 else if (file->f_op->read_iter)
3d04c8a1 432 return new_sync_read(file, buf, count, pos);
6fb5032e 433 else
3d04c8a1 434 return -EINVAL;
6fb5032e 435}
3d04c8a1 436EXPORT_SYMBOL(__vfs_read);
6fb5032e 437
1da177e4
LT
438ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
439{
440 ssize_t ret;
441
442 if (!(file->f_mode & FMODE_READ))
443 return -EBADF;
7f7f25e8 444 if (!(file->f_mode & FMODE_CAN_READ))
1da177e4
LT
445 return -EINVAL;
446 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
447 return -EFAULT;
448
449 ret = rw_verify_area(READ, file, pos, count);
bc61384d
AV
450 if (!ret) {
451 if (count > MAX_RW_COUNT)
452 count = MAX_RW_COUNT;
6fb5032e 453 ret = __vfs_read(file, buf, count, pos);
c43e259c 454 if (ret > 0) {
2a12a9d7 455 fsnotify_access(file);
c43e259c 456 add_rchar(current, ret);
1da177e4 457 }
c43e259c 458 inc_syscr(current);
1da177e4
LT
459 }
460
461 return ret;
462}
463
464EXPORT_SYMBOL(vfs_read);
465
5d5d5689 466static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
293bc982
AV
467{
468 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
469 struct kiocb kiocb;
470 struct iov_iter iter;
471 ssize_t ret;
472
473 init_sync_kiocb(&kiocb, filp);
474 kiocb.ki_pos = *ppos;
293bc982
AV
475 iov_iter_init(&iter, WRITE, &iov, 1, len);
476
bb7462b6 477 ret = call_write_iter(filp, &kiocb, &iter);
599bd19b 478 BUG_ON(ret == -EIOCBQUEUED);
f765b134
AV
479 if (ret > 0)
480 *ppos = kiocb.ki_pos;
293bc982
AV
481 return ret;
482}
483
493c84c0
AV
484ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
485 loff_t *pos)
486{
487 if (file->f_op->write)
488 return file->f_op->write(file, p, count, pos);
493c84c0
AV
489 else if (file->f_op->write_iter)
490 return new_sync_write(file, p, count, pos);
491 else
492 return -EINVAL;
493}
494EXPORT_SYMBOL(__vfs_write);
495
06ae43f3
AV
496ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
497{
498 mm_segment_t old_fs;
499 const char __user *p;
500 ssize_t ret;
501
7f7f25e8 502 if (!(file->f_mode & FMODE_CAN_WRITE))
3e84f48e
AV
503 return -EINVAL;
504
06ae43f3
AV
505 old_fs = get_fs();
506 set_fs(get_ds());
507 p = (__force const char __user *)buf;
508 if (count > MAX_RW_COUNT)
509 count = MAX_RW_COUNT;
493c84c0 510 ret = __vfs_write(file, p, count, pos);
06ae43f3
AV
511 set_fs(old_fs);
512 if (ret > 0) {
513 fsnotify_modify(file);
514 add_wchar(current, ret);
515 }
516 inc_syscw(current);
517 return ret;
518}
519
2ec3a12a
AV
520EXPORT_SYMBOL(__kernel_write);
521
1da177e4
LT
522ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
523{
524 ssize_t ret;
525
526 if (!(file->f_mode & FMODE_WRITE))
527 return -EBADF;
7f7f25e8 528 if (!(file->f_mode & FMODE_CAN_WRITE))
1da177e4
LT
529 return -EINVAL;
530 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
531 return -EFAULT;
532
533 ret = rw_verify_area(WRITE, file, pos, count);
bc61384d
AV
534 if (!ret) {
535 if (count > MAX_RW_COUNT)
536 count = MAX_RW_COUNT;
03d95eb2 537 file_start_write(file);
493c84c0 538 ret = __vfs_write(file, buf, count, pos);
c43e259c 539 if (ret > 0) {
2a12a9d7 540 fsnotify_modify(file);
c43e259c 541 add_wchar(current, ret);
1da177e4 542 }
c43e259c 543 inc_syscw(current);
03d95eb2 544 file_end_write(file);
1da177e4
LT
545 }
546
547 return ret;
548}
549
550EXPORT_SYMBOL(vfs_write);
551
552static inline loff_t file_pos_read(struct file *file)
553{
554 return file->f_pos;
555}
556
557static inline void file_pos_write(struct file *file, loff_t pos)
558{
559 file->f_pos = pos;
560}
561
3cdad428 562SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
1da177e4 563{
9c225f26 564 struct fd f = fdget_pos(fd);
1da177e4 565 ssize_t ret = -EBADF;
1da177e4 566
2903ff01
AV
567 if (f.file) {
568 loff_t pos = file_pos_read(f.file);
569 ret = vfs_read(f.file, buf, count, &pos);
5faf153e
AV
570 if (ret >= 0)
571 file_pos_write(f.file, pos);
9c225f26 572 fdput_pos(f);
1da177e4 573 }
1da177e4
LT
574 return ret;
575}
1da177e4 576
3cdad428
HC
577SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
578 size_t, count)
1da177e4 579{
9c225f26 580 struct fd f = fdget_pos(fd);
1da177e4 581 ssize_t ret = -EBADF;
1da177e4 582
2903ff01
AV
583 if (f.file) {
584 loff_t pos = file_pos_read(f.file);
585 ret = vfs_write(f.file, buf, count, &pos);
5faf153e
AV
586 if (ret >= 0)
587 file_pos_write(f.file, pos);
9c225f26 588 fdput_pos(f);
1da177e4
LT
589 }
590
591 return ret;
592}
593
4a0fd5bf
AV
594SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
595 size_t, count, loff_t, pos)
1da177e4 596{
2903ff01 597 struct fd f;
1da177e4 598 ssize_t ret = -EBADF;
1da177e4
LT
599
600 if (pos < 0)
601 return -EINVAL;
602
2903ff01
AV
603 f = fdget(fd);
604 if (f.file) {
1da177e4 605 ret = -ESPIPE;
2903ff01
AV
606 if (f.file->f_mode & FMODE_PREAD)
607 ret = vfs_read(f.file, buf, count, &pos);
608 fdput(f);
1da177e4
LT
609 }
610
611 return ret;
612}
613
4a0fd5bf
AV
614SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
615 size_t, count, loff_t, pos)
1da177e4 616{
2903ff01 617 struct fd f;
1da177e4 618 ssize_t ret = -EBADF;
1da177e4
LT
619
620 if (pos < 0)
621 return -EINVAL;
622
2903ff01
AV
623 f = fdget(fd);
624 if (f.file) {
1da177e4 625 ret = -ESPIPE;
2903ff01
AV
626 if (f.file->f_mode & FMODE_PWRITE)
627 ret = vfs_write(f.file, buf, count, &pos);
628 fdput(f);
1da177e4
LT
629 }
630
631 return ret;
632}
633
634/*
635 * Reduce an iovec's length in-place. Return the resulting number of segments
636 */
637unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
638{
639 unsigned long seg = 0;
640 size_t len = 0;
641
642 while (seg < nr_segs) {
643 seg++;
644 if (len + iov->iov_len >= to) {
645 iov->iov_len = to - len;
646 break;
647 }
648 len += iov->iov_len;
649 iov++;
650 }
651 return seg;
652}
19295529 653EXPORT_SYMBOL(iov_shorten);
1da177e4 654
ac15ac06 655static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
0f78d06a 656 loff_t *ppos, int type, int flags)
293bc982
AV
657{
658 struct kiocb kiocb;
293bc982
AV
659 ssize_t ret;
660
e864f395 661 if (flags & ~(RWF_HIPRI | RWF_DSYNC | RWF_SYNC))
793b80ef
CH
662 return -EOPNOTSUPP;
663
293bc982 664 init_sync_kiocb(&kiocb, filp);
97be7ebe
CH
665 if (flags & RWF_HIPRI)
666 kiocb.ki_flags |= IOCB_HIPRI;
e864f395
CH
667 if (flags & RWF_DSYNC)
668 kiocb.ki_flags |= IOCB_DSYNC;
669 if (flags & RWF_SYNC)
670 kiocb.ki_flags |= (IOCB_DSYNC | IOCB_SYNC);
293bc982 671 kiocb.ki_pos = *ppos;
293bc982 672
0f78d06a 673 if (type == READ)
bb7462b6 674 ret = call_read_iter(filp, &kiocb, iter);
0f78d06a 675 else
bb7462b6 676 ret = call_write_iter(filp, &kiocb, iter);
599bd19b 677 BUG_ON(ret == -EIOCBQUEUED);
293bc982
AV
678 *ppos = kiocb.ki_pos;
679 return ret;
680}
681
ee0b3e67 682/* Do it by hand, with file-ops */
ac15ac06 683static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
0f78d06a 684 loff_t *ppos, int type, int flags)
ee0b3e67 685{
ee0b3e67
BP
686 ssize_t ret = 0;
687
97be7ebe 688 if (flags & ~RWF_HIPRI)
793b80ef
CH
689 return -EOPNOTSUPP;
690
ac15ac06
AV
691 while (iov_iter_count(iter)) {
692 struct iovec iovec = iov_iter_iovec(iter);
ee0b3e67
BP
693 ssize_t nr;
694
0f78d06a
MS
695 if (type == READ) {
696 nr = filp->f_op->read(filp, iovec.iov_base,
697 iovec.iov_len, ppos);
698 } else {
699 nr = filp->f_op->write(filp, iovec.iov_base,
700 iovec.iov_len, ppos);
701 }
ee0b3e67
BP
702
703 if (nr < 0) {
704 if (!ret)
705 ret = nr;
706 break;
707 }
708 ret += nr;
ac15ac06 709 if (nr != iovec.iov_len)
ee0b3e67 710 break;
ac15ac06 711 iov_iter_advance(iter, nr);
ee0b3e67
BP
712 }
713
714 return ret;
715}
716
1da177e4
LT
717/* A write operation does a read from user space and vice versa */
718#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
719
ffecee4f
VN
720/**
721 * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace
722 * into the kernel and check that it is valid.
723 *
724 * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE.
725 * @uvector: Pointer to the userspace array.
726 * @nr_segs: Number of elements in userspace array.
727 * @fast_segs: Number of elements in @fast_pointer.
728 * @fast_pointer: Pointer to (usually small on-stack) kernel array.
729 * @ret_pointer: (output parameter) Pointer to a variable that will point to
730 * either @fast_pointer, a newly allocated kernel array, or NULL,
731 * depending on which array was used.
732 *
733 * This function copies an array of &struct iovec of @nr_segs from
734 * userspace into the kernel and checks that each element is valid (e.g.
735 * it does not point to a kernel address or cause overflow by being too
736 * large, etc.).
737 *
738 * As an optimization, the caller may provide a pointer to a small
739 * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long
740 * (the size of this array, or 0 if unused, should be given in @fast_segs).
741 *
742 * @ret_pointer will always point to the array that was used, so the
743 * caller must take care not to call kfree() on it e.g. in case the
744 * @fast_pointer array was used and it was allocated on the stack.
745 *
746 * Return: The total number of bytes covered by the iovec array on success
747 * or a negative error code on error.
748 */
eed4e51f
BP
749ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
750 unsigned long nr_segs, unsigned long fast_segs,
751 struct iovec *fast_pointer,
ac34ebb3 752 struct iovec **ret_pointer)
435f49a5 753{
eed4e51f 754 unsigned long seg;
435f49a5 755 ssize_t ret;
eed4e51f
BP
756 struct iovec *iov = fast_pointer;
757
435f49a5
LT
758 /*
759 * SuS says "The readv() function *may* fail if the iovcnt argument
760 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
761 * traditionally returned zero for zero segments, so...
762 */
eed4e51f
BP
763 if (nr_segs == 0) {
764 ret = 0;
435f49a5 765 goto out;
eed4e51f
BP
766 }
767
435f49a5
LT
768 /*
769 * First get the "struct iovec" from user memory and
770 * verify all the pointers
771 */
eed4e51f
BP
772 if (nr_segs > UIO_MAXIOV) {
773 ret = -EINVAL;
435f49a5 774 goto out;
eed4e51f
BP
775 }
776 if (nr_segs > fast_segs) {
435f49a5 777 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
eed4e51f
BP
778 if (iov == NULL) {
779 ret = -ENOMEM;
435f49a5 780 goto out;
eed4e51f 781 }
435f49a5 782 }
eed4e51f
BP
783 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
784 ret = -EFAULT;
435f49a5 785 goto out;
eed4e51f
BP
786 }
787
435f49a5 788 /*
eed4e51f
BP
789 * According to the Single Unix Specification we should return EINVAL
790 * if an element length is < 0 when cast to ssize_t or if the
791 * total length would overflow the ssize_t return value of the
792 * system call.
435f49a5
LT
793 *
794 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
795 * overflow case.
796 */
eed4e51f 797 ret = 0;
435f49a5
LT
798 for (seg = 0; seg < nr_segs; seg++) {
799 void __user *buf = iov[seg].iov_base;
800 ssize_t len = (ssize_t)iov[seg].iov_len;
eed4e51f
BP
801
802 /* see if we we're about to use an invalid len or if
803 * it's about to overflow ssize_t */
435f49a5 804 if (len < 0) {
eed4e51f 805 ret = -EINVAL;
435f49a5 806 goto out;
eed4e51f 807 }
ac34ebb3 808 if (type >= 0
fcf63409 809 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
eed4e51f 810 ret = -EFAULT;
435f49a5
LT
811 goto out;
812 }
813 if (len > MAX_RW_COUNT - ret) {
814 len = MAX_RW_COUNT - ret;
815 iov[seg].iov_len = len;
eed4e51f 816 }
eed4e51f 817 ret += len;
435f49a5 818 }
eed4e51f
BP
819out:
820 *ret_pointer = iov;
821 return ret;
822}
823
f5029855
AV
824#ifdef CONFIG_COMPAT
825ssize_t compat_rw_copy_check_uvector(int type,
826 const struct compat_iovec __user *uvector, unsigned long nr_segs,
827 unsigned long fast_segs, struct iovec *fast_pointer,
828 struct iovec **ret_pointer)
829{
830 compat_ssize_t tot_len;
831 struct iovec *iov = *ret_pointer = fast_pointer;
832 ssize_t ret = 0;
833 int seg;
834
835 /*
836 * SuS says "The readv() function *may* fail if the iovcnt argument
837 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
838 * traditionally returned zero for zero segments, so...
839 */
840 if (nr_segs == 0)
841 goto out;
842
843 ret = -EINVAL;
844 if (nr_segs > UIO_MAXIOV)
845 goto out;
846 if (nr_segs > fast_segs) {
847 ret = -ENOMEM;
848 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
849 if (iov == NULL)
850 goto out;
851 }
852 *ret_pointer = iov;
853
854 ret = -EFAULT;
855 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
856 goto out;
857
858 /*
859 * Single unix specification:
860 * We should -EINVAL if an element length is not >= 0 and fitting an
861 * ssize_t.
862 *
863 * In Linux, the total length is limited to MAX_RW_COUNT, there is
864 * no overflow possibility.
865 */
866 tot_len = 0;
867 ret = -EINVAL;
868 for (seg = 0; seg < nr_segs; seg++) {
869 compat_uptr_t buf;
870 compat_ssize_t len;
871
872 if (__get_user(len, &uvector->iov_len) ||
873 __get_user(buf, &uvector->iov_base)) {
874 ret = -EFAULT;
875 goto out;
876 }
877 if (len < 0) /* size_t not fitting in compat_ssize_t .. */
878 goto out;
879 if (type >= 0 &&
880 !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
881 ret = -EFAULT;
882 goto out;
883 }
884 if (len > MAX_RW_COUNT - tot_len)
885 len = MAX_RW_COUNT - tot_len;
886 tot_len += len;
887 iov->iov_base = compat_ptr(buf);
888 iov->iov_len = (compat_size_t) len;
889 uvector++;
890 iov++;
891 }
892 ret = tot_len;
893
894out:
895 return ret;
896}
897#endif
898
19c73586
CH
899static ssize_t do_iter_read(struct file *file, struct iov_iter *iter,
900 loff_t *pos, int flags)
1da177e4 901{
1da177e4 902 size_t tot_len;
7687a7a4 903 ssize_t ret = 0;
1da177e4 904
edab5fe3
CH
905 if (!(file->f_mode & FMODE_READ))
906 return -EBADF;
907 if (!(file->f_mode & FMODE_CAN_READ))
908 return -EINVAL;
909
7687a7a4 910 tot_len = iov_iter_count(iter);
0504c074
AV
911 if (!tot_len)
912 goto out;
19c73586 913 ret = rw_verify_area(READ, file, pos, tot_len);
e28cc715 914 if (ret < 0)
19c73586 915 return ret;
1da177e4 916
19c73586
CH
917 if (file->f_op->read_iter)
918 ret = do_iter_readv_writev(file, iter, pos, READ, flags);
ee0b3e67 919 else
19c73586
CH
920 ret = do_loop_readv_writev(file, iter, pos, READ, flags);
921out:
922 if (ret >= 0)
923 fsnotify_access(file);
924 return ret;
925}
1da177e4 926
18e9710e
CH
927ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
928 int flags)
929{
930 if (!file->f_op->read_iter)
931 return -EINVAL;
932 return do_iter_read(file, iter, ppos, flags);
933}
934EXPORT_SYMBOL(vfs_iter_read);
935
19c73586
CH
936static ssize_t do_iter_write(struct file *file, struct iov_iter *iter,
937 loff_t *pos, int flags)
938{
939 size_t tot_len;
940 ssize_t ret = 0;
03d95eb2 941
edab5fe3
CH
942 if (!(file->f_mode & FMODE_WRITE))
943 return -EBADF;
944 if (!(file->f_mode & FMODE_CAN_WRITE))
945 return -EINVAL;
946
19c73586
CH
947 tot_len = iov_iter_count(iter);
948 if (!tot_len)
949 return 0;
950 ret = rw_verify_area(WRITE, file, pos, tot_len);
951 if (ret < 0)
952 return ret;
953
954 file_start_write(file);
955 if (file->f_op->write_iter)
956 ret = do_iter_readv_writev(file, iter, pos, WRITE, flags);
957 else
958 ret = do_loop_readv_writev(file, iter, pos, WRITE, flags);
959 file_end_write(file);
960 if (ret > 0)
961 fsnotify_modify(file);
1da177e4 962 return ret;
1da177e4
LT
963}
964
251b42a1
CH
965ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
966 unsigned long vlen, loff_t *pos, int flags)
7687a7a4
MS
967{
968 struct iovec iovstack[UIO_FASTIOV];
969 struct iovec *iov = iovstack;
970 struct iov_iter iter;
971 ssize_t ret;
972
251b42a1 973 ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
edab5fe3
CH
974 if (ret >= 0) {
975 ret = do_iter_read(file, &iter, pos, flags);
976 kfree(iov);
977 }
1da177e4 978
251b42a1
CH
979 return ret;
980}
1da177e4
LT
981EXPORT_SYMBOL(vfs_readv);
982
983ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
793b80ef 984 unsigned long vlen, loff_t *pos, int flags)
1da177e4 985{
251b42a1
CH
986 struct iovec iovstack[UIO_FASTIOV];
987 struct iovec *iov = iovstack;
988 struct iov_iter iter;
989 ssize_t ret;
990
251b42a1 991 ret = import_iovec(WRITE, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
edab5fe3
CH
992 if (ret >= 0) {
993 ret = do_iter_write(file, &iter, pos, flags);
994 kfree(iov);
995 }
251b42a1
CH
996 return ret;
997}
1da177e4
LT
998EXPORT_SYMBOL(vfs_writev);
999
f17d8b35
MT
1000static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
1001 unsigned long vlen, int flags)
1da177e4 1002{
9c225f26 1003 struct fd f = fdget_pos(fd);
1da177e4 1004 ssize_t ret = -EBADF;
1da177e4 1005
2903ff01
AV
1006 if (f.file) {
1007 loff_t pos = file_pos_read(f.file);
f17d8b35 1008 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
5faf153e
AV
1009 if (ret >= 0)
1010 file_pos_write(f.file, pos);
9c225f26 1011 fdput_pos(f);
1da177e4
LT
1012 }
1013
1014 if (ret > 0)
4b98d11b
AD
1015 add_rchar(current, ret);
1016 inc_syscr(current);
1da177e4
LT
1017 return ret;
1018}
1019
f17d8b35
MT
1020static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
1021 unsigned long vlen, int flags)
1da177e4 1022{
9c225f26 1023 struct fd f = fdget_pos(fd);
1da177e4 1024 ssize_t ret = -EBADF;
1da177e4 1025
2903ff01
AV
1026 if (f.file) {
1027 loff_t pos = file_pos_read(f.file);
f17d8b35 1028 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
5faf153e
AV
1029 if (ret >= 0)
1030 file_pos_write(f.file, pos);
9c225f26 1031 fdput_pos(f);
1da177e4
LT
1032 }
1033
1034 if (ret > 0)
4b98d11b
AD
1035 add_wchar(current, ret);
1036 inc_syscw(current);
1da177e4
LT
1037 return ret;
1038}
1039
601cc11d
LT
1040static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
1041{
1042#define HALF_LONG_BITS (BITS_PER_LONG / 2)
1043 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
1044}
1045
f17d8b35
MT
1046static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
1047 unsigned long vlen, loff_t pos, int flags)
f3554f4b 1048{
2903ff01 1049 struct fd f;
f3554f4b 1050 ssize_t ret = -EBADF;
f3554f4b
GH
1051
1052 if (pos < 0)
1053 return -EINVAL;
1054
2903ff01
AV
1055 f = fdget(fd);
1056 if (f.file) {
f3554f4b 1057 ret = -ESPIPE;
2903ff01 1058 if (f.file->f_mode & FMODE_PREAD)
f17d8b35 1059 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
2903ff01 1060 fdput(f);
f3554f4b
GH
1061 }
1062
1063 if (ret > 0)
1064 add_rchar(current, ret);
1065 inc_syscr(current);
1066 return ret;
1067}
1068
f17d8b35
MT
1069static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
1070 unsigned long vlen, loff_t pos, int flags)
f3554f4b 1071{
2903ff01 1072 struct fd f;
f3554f4b 1073 ssize_t ret = -EBADF;
f3554f4b
GH
1074
1075 if (pos < 0)
1076 return -EINVAL;
1077
2903ff01
AV
1078 f = fdget(fd);
1079 if (f.file) {
f3554f4b 1080 ret = -ESPIPE;
2903ff01 1081 if (f.file->f_mode & FMODE_PWRITE)
f17d8b35 1082 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
2903ff01 1083 fdput(f);
f3554f4b
GH
1084 }
1085
1086 if (ret > 0)
1087 add_wchar(current, ret);
1088 inc_syscw(current);
1089 return ret;
1090}
1091
f17d8b35
MT
1092SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
1093 unsigned long, vlen)
1094{
1095 return do_readv(fd, vec, vlen, 0);
1096}
1097
1098SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
1099 unsigned long, vlen)
1100{
1101 return do_writev(fd, vec, vlen, 0);
1102}
1103
1104SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
1105 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1106{
1107 loff_t pos = pos_from_hilo(pos_h, pos_l);
1108
1109 return do_preadv(fd, vec, vlen, pos, 0);
1110}
1111
1112SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
1113 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1114 int, flags)
1115{
1116 loff_t pos = pos_from_hilo(pos_h, pos_l);
1117
1118 if (pos == -1)
1119 return do_readv(fd, vec, vlen, flags);
1120
1121 return do_preadv(fd, vec, vlen, pos, flags);
1122}
1123
1124SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
1125 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1126{
1127 loff_t pos = pos_from_hilo(pos_h, pos_l);
1128
1129 return do_pwritev(fd, vec, vlen, pos, 0);
1130}
1131
1132SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
1133 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1134 int, flags)
1135{
1136 loff_t pos = pos_from_hilo(pos_h, pos_l);
1137
1138 if (pos == -1)
1139 return do_writev(fd, vec, vlen, flags);
1140
1141 return do_pwritev(fd, vec, vlen, pos, flags);
1142}
1143
72ec3516 1144#ifdef CONFIG_COMPAT
72ec3516
AV
1145static size_t compat_readv(struct file *file,
1146 const struct compat_iovec __user *vec,
f17d8b35 1147 unsigned long vlen, loff_t *pos, int flags)
72ec3516 1148{
26c87fb7
CH
1149 struct iovec iovstack[UIO_FASTIOV];
1150 struct iovec *iov = iovstack;
1151 struct iov_iter iter;
edab5fe3 1152 ssize_t ret;
72ec3516 1153
26c87fb7 1154 ret = compat_import_iovec(READ, vec, vlen, UIO_FASTIOV, &iov, &iter);
edab5fe3
CH
1155 if (ret >= 0) {
1156 ret = do_iter_read(file, &iter, pos, flags);
1157 kfree(iov);
1158 }
72ec3516
AV
1159 if (ret > 0)
1160 add_rchar(current, ret);
1161 inc_syscr(current);
1162 return ret;
1163}
1164
f17d8b35
MT
1165static size_t do_compat_readv(compat_ulong_t fd,
1166 const struct compat_iovec __user *vec,
1167 compat_ulong_t vlen, int flags)
72ec3516 1168{
9c225f26 1169 struct fd f = fdget_pos(fd);
72ec3516
AV
1170 ssize_t ret;
1171 loff_t pos;
1172
1173 if (!f.file)
1174 return -EBADF;
1175 pos = f.file->f_pos;
f17d8b35 1176 ret = compat_readv(f.file, vec, vlen, &pos, flags);
5faf153e
AV
1177 if (ret >= 0)
1178 f.file->f_pos = pos;
9c225f26 1179 fdput_pos(f);
72ec3516 1180 return ret;
f17d8b35 1181
72ec3516
AV
1182}
1183
f17d8b35
MT
1184COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
1185 const struct compat_iovec __user *,vec,
1186 compat_ulong_t, vlen)
1187{
1188 return do_compat_readv(fd, vec, vlen, 0);
1189}
1190
1191static long do_compat_preadv64(unsigned long fd,
378a10f3 1192 const struct compat_iovec __user *vec,
f17d8b35 1193 unsigned long vlen, loff_t pos, int flags)
72ec3516
AV
1194{
1195 struct fd f;
1196 ssize_t ret;
1197
1198 if (pos < 0)
1199 return -EINVAL;
1200 f = fdget(fd);
1201 if (!f.file)
1202 return -EBADF;
1203 ret = -ESPIPE;
1204 if (f.file->f_mode & FMODE_PREAD)
f17d8b35 1205 ret = compat_readv(f.file, vec, vlen, &pos, flags);
72ec3516
AV
1206 fdput(f);
1207 return ret;
1208}
1209
378a10f3
HC
1210#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1211COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1212 const struct compat_iovec __user *,vec,
1213 unsigned long, vlen, loff_t, pos)
1214{
f17d8b35 1215 return do_compat_preadv64(fd, vec, vlen, pos, 0);
378a10f3
HC
1216}
1217#endif
1218
dfd948e3 1219COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
72ec3516 1220 const struct compat_iovec __user *,vec,
dfd948e3 1221 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
72ec3516
AV
1222{
1223 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
378a10f3 1224
f17d8b35
MT
1225 return do_compat_preadv64(fd, vec, vlen, pos, 0);
1226}
1227
3ebfd81f
L
1228#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
1229COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
1230 const struct compat_iovec __user *,vec,
1231 unsigned long, vlen, loff_t, pos, int, flags)
1232{
1233 return do_compat_preadv64(fd, vec, vlen, pos, flags);
1234}
1235#endif
1236
f17d8b35
MT
1237COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
1238 const struct compat_iovec __user *,vec,
1239 compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
1240 int, flags)
1241{
1242 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1243
1244 if (pos == -1)
1245 return do_compat_readv(fd, vec, vlen, flags);
1246
1247 return do_compat_preadv64(fd, vec, vlen, pos, flags);
72ec3516
AV
1248}
1249
1250static size_t compat_writev(struct file *file,
1251 const struct compat_iovec __user *vec,
f17d8b35 1252 unsigned long vlen, loff_t *pos, int flags)
72ec3516 1253{
26c87fb7
CH
1254 struct iovec iovstack[UIO_FASTIOV];
1255 struct iovec *iov = iovstack;
1256 struct iov_iter iter;
edab5fe3 1257 ssize_t ret;
72ec3516 1258
26c87fb7 1259 ret = compat_import_iovec(WRITE, vec, vlen, UIO_FASTIOV, &iov, &iter);
edab5fe3
CH
1260 if (ret >= 0) {
1261 ret = do_iter_write(file, &iter, pos, flags);
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);