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