Commit | Line | Data |
---|---|---|
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 | 28 | const 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 | ||
35 | EXPORT_SYMBOL(generic_ro_fops); | |
36 | ||
ddef7ed2 | 37 | static 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 | */ | |
54 | loff_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 | 67 | EXPORT_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 | 85 | loff_t |
965c8e59 | 86 | generic_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 | } |
132 | EXPORT_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 | 144 | loff_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 | 152 | EXPORT_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 | */ | |
162 | loff_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 | } | |
172 | EXPORT_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 | */ | |
181 | loff_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 | } | |
191 | EXPORT_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 | */ | |
201 | loff_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 | } | |
211 | EXPORT_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 | 224 | loff_t noop_llseek(struct file *file, loff_t offset, int whence) |
ae6afc3f B |
225 | { |
226 | return file->f_pos; | |
227 | } | |
228 | EXPORT_SYMBOL(noop_llseek); | |
229 | ||
965c8e59 | 230 | loff_t no_llseek(struct file *file, loff_t offset, int whence) |
1da177e4 LT |
231 | { |
232 | return -ESPIPE; | |
233 | } | |
234 | EXPORT_SYMBOL(no_llseek); | |
235 | ||
965c8e59 | 236 | loff_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 | 285 | out: |
5955102c | 286 | inode_unlock(inode); |
1da177e4 LT |
287 | return retval; |
288 | } | |
289 | EXPORT_SYMBOL(default_llseek); | |
290 | ||
965c8e59 | 291 | loff_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 | } |
302 | EXPORT_SYMBOL(vfs_llseek); | |
303 | ||
bef17329 | 304 | static off_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 |
322 | SYSCALL_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 |
328 | COMPAT_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 | ||
9e62ccec MS |
334 | #if !defined(CONFIG_64BIT) || defined(CONFIG_COMPAT) || \ |
335 | defined(__ARCH_WANT_SYS_LLSEEK) | |
003d7ab4 HC |
336 | SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high, |
337 | unsigned long, offset_low, loff_t __user *, result, | |
965c8e59 | 338 | unsigned int, whence) |
1da177e4 LT |
339 | { |
340 | int retval; | |
d7a15f8d | 341 | struct fd f = fdget_pos(fd); |
1da177e4 | 342 | loff_t offset; |
1da177e4 | 343 | |
2903ff01 AV |
344 | if (!f.file) |
345 | return -EBADF; | |
1da177e4 LT |
346 | |
347 | retval = -EINVAL; | |
965c8e59 | 348 | if (whence > SEEK_MAX) |
1da177e4 LT |
349 | goto out_putf; |
350 | ||
2903ff01 | 351 | offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low, |
965c8e59 | 352 | whence); |
1da177e4 LT |
353 | |
354 | retval = (int)offset; | |
355 | if (offset >= 0) { | |
356 | retval = -EFAULT; | |
357 | if (!copy_to_user(result, &offset, sizeof(offset))) | |
358 | retval = 0; | |
359 | } | |
360 | out_putf: | |
d7a15f8d | 361 | fdput_pos(f); |
1da177e4 LT |
362 | return retval; |
363 | } | |
364 | #endif | |
365 | ||
68d70d03 | 366 | int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count) |
1da177e4 LT |
367 | { |
368 | struct inode *inode; | |
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 | |
438ab720 KS |
375 | /* |
376 | * ranged mandatory locking does not apply to streams - it makes sense | |
377 | * only for files where position has a meaning. | |
378 | */ | |
379 | if (ppos) { | |
380 | loff_t pos = *ppos; | |
381 | ||
382 | if (unlikely(pos < 0)) { | |
383 | if (!unsigned_offsets(file)) | |
384 | return retval; | |
385 | if (count >= -pos) /* both values are in 0..LLONG_MAX */ | |
386 | return -EOVERFLOW; | |
387 | } else if (unlikely((loff_t) (pos + count) < 0)) { | |
388 | if (!unsigned_offsets(file)) | |
389 | return retval; | |
390 | } | |
391 | ||
392 | if (unlikely(inode->i_flctx && mandatory_lock(inode))) { | |
393 | retval = locks_mandatory_area(inode, file, pos, pos + count - 1, | |
394 | read_write == READ ? F_RDLCK : F_WRLCK); | |
395 | if (retval < 0) | |
396 | return retval; | |
397 | } | |
e28cc715 | 398 | } |
438ab720 | 399 | |
bc61384d | 400 | return security_file_permission(file, |
c43e259c | 401 | read_write == READ ? MAY_READ : MAY_WRITE); |
1da177e4 LT |
402 | } |
403 | ||
5d5d5689 | 404 | static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos) |
293bc982 AV |
405 | { |
406 | struct iovec iov = { .iov_base = buf, .iov_len = len }; | |
407 | struct kiocb kiocb; | |
408 | struct iov_iter iter; | |
409 | ssize_t ret; | |
410 | ||
411 | init_sync_kiocb(&kiocb, filp); | |
438ab720 | 412 | kiocb.ki_pos = (ppos ? *ppos : 0); |
293bc982 AV |
413 | iov_iter_init(&iter, READ, &iov, 1, len); |
414 | ||
bb7462b6 | 415 | ret = call_read_iter(filp, &kiocb, &iter); |
599bd19b | 416 | BUG_ON(ret == -EIOCBQUEUED); |
438ab720 KS |
417 | if (ppos) |
418 | *ppos = kiocb.ki_pos; | |
293bc982 AV |
419 | return ret; |
420 | } | |
421 | ||
4d03e3cc CH |
422 | static int warn_unsupported(struct file *file, const char *op) |
423 | { | |
424 | pr_warn_ratelimited( | |
425 | "kernel %s not supported for file %pD4 (pid: %d comm: %.20s)\n", | |
426 | op, file, current->pid, current->comm); | |
427 | return -EINVAL; | |
428 | } | |
429 | ||
61a707c5 CH |
430 | ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) |
431 | { | |
4d03e3cc CH |
432 | struct kvec iov = { |
433 | .iov_base = buf, | |
434 | .iov_len = min_t(size_t, count, MAX_RW_COUNT), | |
435 | }; | |
436 | struct kiocb kiocb; | |
437 | struct iov_iter iter; | |
61a707c5 CH |
438 | ssize_t ret; |
439 | ||
440 | if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ))) | |
441 | return -EINVAL; | |
442 | if (!(file->f_mode & FMODE_CAN_READ)) | |
443 | return -EINVAL; | |
4d03e3cc CH |
444 | /* |
445 | * Also fail if ->read_iter and ->read are both wired up as that | |
446 | * implies very convoluted semantics. | |
447 | */ | |
448 | if (unlikely(!file->f_op->read_iter || file->f_op->read)) | |
449 | return warn_unsupported(file, "read"); | |
61a707c5 | 450 | |
4d03e3cc CH |
451 | init_sync_kiocb(&kiocb, file); |
452 | kiocb.ki_pos = *pos; | |
453 | iov_iter_kvec(&iter, READ, &iov, 1, iov.iov_len); | |
454 | ret = file->f_op->read_iter(&kiocb, &iter); | |
61a707c5 | 455 | if (ret > 0) { |
4d03e3cc | 456 | *pos = kiocb.ki_pos; |
61a707c5 CH |
457 | fsnotify_access(file); |
458 | add_rchar(current, ret); | |
459 | } | |
460 | inc_syscr(current); | |
461 | return ret; | |
462 | } | |
463 | ||
bdd1d2d3 | 464 | ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) |
c41fbad0 | 465 | { |
6209dd91 | 466 | ssize_t ret; |
c41fbad0 | 467 | |
6209dd91 CH |
468 | ret = rw_verify_area(READ, file, pos, count); |
469 | if (ret) | |
470 | return ret; | |
471 | return __kernel_read(file, buf, count, pos); | |
c41fbad0 CH |
472 | } |
473 | EXPORT_SYMBOL(kernel_read); | |
6fb5032e | 474 | |
1da177e4 LT |
475 | ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) |
476 | { | |
477 | ssize_t ret; | |
478 | ||
479 | if (!(file->f_mode & FMODE_READ)) | |
480 | return -EBADF; | |
7f7f25e8 | 481 | if (!(file->f_mode & FMODE_CAN_READ)) |
1da177e4 | 482 | return -EINVAL; |
96d4f267 | 483 | if (unlikely(!access_ok(buf, count))) |
1da177e4 LT |
484 | return -EFAULT; |
485 | ||
486 | ret = rw_verify_area(READ, file, pos, count); | |
775802c0 CH |
487 | if (ret) |
488 | return ret; | |
489 | if (count > MAX_RW_COUNT) | |
490 | count = MAX_RW_COUNT; | |
1da177e4 | 491 | |
775802c0 CH |
492 | if (file->f_op->read) |
493 | ret = file->f_op->read(file, buf, count, pos); | |
494 | else if (file->f_op->read_iter) | |
495 | ret = new_sync_read(file, buf, count, pos); | |
496 | else | |
497 | ret = -EINVAL; | |
498 | if (ret > 0) { | |
499 | fsnotify_access(file); | |
500 | add_rchar(current, ret); | |
501 | } | |
502 | inc_syscr(current); | |
1da177e4 LT |
503 | return ret; |
504 | } | |
505 | ||
5d5d5689 | 506 | static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos) |
293bc982 AV |
507 | { |
508 | struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len }; | |
509 | struct kiocb kiocb; | |
510 | struct iov_iter iter; | |
511 | ssize_t ret; | |
512 | ||
513 | init_sync_kiocb(&kiocb, filp); | |
438ab720 | 514 | kiocb.ki_pos = (ppos ? *ppos : 0); |
293bc982 AV |
515 | iov_iter_init(&iter, WRITE, &iov, 1, len); |
516 | ||
bb7462b6 | 517 | ret = call_write_iter(filp, &kiocb, &iter); |
599bd19b | 518 | BUG_ON(ret == -EIOCBQUEUED); |
438ab720 | 519 | if (ret > 0 && ppos) |
f765b134 | 520 | *ppos = kiocb.ki_pos; |
293bc982 AV |
521 | return ret; |
522 | } | |
523 | ||
81238b2c | 524 | /* caller is responsible for file_start_write/file_end_write */ |
73e18f7c | 525 | ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos) |
06ae43f3 | 526 | { |
4d03e3cc CH |
527 | struct kvec iov = { |
528 | .iov_base = (void *)buf, | |
529 | .iov_len = min_t(size_t, count, MAX_RW_COUNT), | |
530 | }; | |
531 | struct kiocb kiocb; | |
532 | struct iov_iter iter; | |
06ae43f3 AV |
533 | ssize_t ret; |
534 | ||
a01ac27b CH |
535 | if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE))) |
536 | return -EBADF; | |
7f7f25e8 | 537 | if (!(file->f_mode & FMODE_CAN_WRITE)) |
3e84f48e | 538 | return -EINVAL; |
4d03e3cc CH |
539 | /* |
540 | * Also fail if ->write_iter and ->write are both wired up as that | |
541 | * implies very convoluted semantics. | |
542 | */ | |
543 | if (unlikely(!file->f_op->write_iter || file->f_op->write)) | |
544 | return warn_unsupported(file, "write"); | |
3e84f48e | 545 | |
4d03e3cc CH |
546 | init_sync_kiocb(&kiocb, file); |
547 | kiocb.ki_pos = *pos; | |
548 | iov_iter_kvec(&iter, WRITE, &iov, 1, iov.iov_len); | |
549 | ret = file->f_op->write_iter(&kiocb, &iter); | |
06ae43f3 | 550 | if (ret > 0) { |
4d03e3cc | 551 | *pos = kiocb.ki_pos; |
06ae43f3 AV |
552 | fsnotify_modify(file); |
553 | add_wchar(current, ret); | |
554 | } | |
555 | inc_syscw(current); | |
556 | return ret; | |
557 | } | |
2ec3a12a | 558 | |
e13ec939 CH |
559 | ssize_t kernel_write(struct file *file, const void *buf, size_t count, |
560 | loff_t *pos) | |
ac452aca | 561 | { |
81238b2c | 562 | ssize_t ret; |
ac452aca | 563 | |
81238b2c CH |
564 | ret = rw_verify_area(WRITE, file, pos, count); |
565 | if (ret) | |
566 | return ret; | |
ac452aca | 567 | |
81238b2c CH |
568 | file_start_write(file); |
569 | ret = __kernel_write(file, buf, count, pos); | |
570 | file_end_write(file); | |
571 | return ret; | |
ac452aca CH |
572 | } |
573 | EXPORT_SYMBOL(kernel_write); | |
574 | ||
1da177e4 LT |
575 | ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) |
576 | { | |
577 | ssize_t ret; | |
578 | ||
579 | if (!(file->f_mode & FMODE_WRITE)) | |
580 | return -EBADF; | |
7f7f25e8 | 581 | if (!(file->f_mode & FMODE_CAN_WRITE)) |
1da177e4 | 582 | return -EINVAL; |
96d4f267 | 583 | if (unlikely(!access_ok(buf, count))) |
1da177e4 LT |
584 | return -EFAULT; |
585 | ||
586 | ret = rw_verify_area(WRITE, file, pos, count); | |
53ad8626 CH |
587 | if (ret) |
588 | return ret; | |
589 | if (count > MAX_RW_COUNT) | |
590 | count = MAX_RW_COUNT; | |
591 | file_start_write(file); | |
592 | if (file->f_op->write) | |
593 | ret = file->f_op->write(file, buf, count, pos); | |
594 | else if (file->f_op->write_iter) | |
595 | ret = new_sync_write(file, buf, count, pos); | |
596 | else | |
597 | ret = -EINVAL; | |
598 | if (ret > 0) { | |
599 | fsnotify_modify(file); | |
600 | add_wchar(current, ret); | |
1da177e4 | 601 | } |
53ad8626 CH |
602 | inc_syscw(current); |
603 | file_end_write(file); | |
1da177e4 LT |
604 | return ret; |
605 | } | |
606 | ||
438ab720 KS |
607 | /* file_ppos returns &file->f_pos or NULL if file is stream */ |
608 | static inline loff_t *file_ppos(struct file *file) | |
1da177e4 | 609 | { |
438ab720 | 610 | return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos; |
1da177e4 LT |
611 | } |
612 | ||
3ce4a7bf | 613 | ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count) |
1da177e4 | 614 | { |
9c225f26 | 615 | struct fd f = fdget_pos(fd); |
1da177e4 | 616 | ssize_t ret = -EBADF; |
1da177e4 | 617 | |
2903ff01 | 618 | if (f.file) { |
438ab720 KS |
619 | loff_t pos, *ppos = file_ppos(f.file); |
620 | if (ppos) { | |
621 | pos = *ppos; | |
622 | ppos = &pos; | |
623 | } | |
624 | ret = vfs_read(f.file, buf, count, ppos); | |
625 | if (ret >= 0 && ppos) | |
626 | f.file->f_pos = pos; | |
9c225f26 | 627 | fdput_pos(f); |
1da177e4 | 628 | } |
1da177e4 LT |
629 | return ret; |
630 | } | |
1da177e4 | 631 | |
3ce4a7bf DB |
632 | SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count) |
633 | { | |
634 | return ksys_read(fd, buf, count); | |
635 | } | |
636 | ||
e7a3e8b2 | 637 | ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count) |
1da177e4 | 638 | { |
9c225f26 | 639 | struct fd f = fdget_pos(fd); |
1da177e4 | 640 | ssize_t ret = -EBADF; |
1da177e4 | 641 | |
2903ff01 | 642 | if (f.file) { |
438ab720 KS |
643 | loff_t pos, *ppos = file_ppos(f.file); |
644 | if (ppos) { | |
645 | pos = *ppos; | |
646 | ppos = &pos; | |
647 | } | |
648 | ret = vfs_write(f.file, buf, count, ppos); | |
649 | if (ret >= 0 && ppos) | |
650 | f.file->f_pos = pos; | |
9c225f26 | 651 | fdput_pos(f); |
1da177e4 LT |
652 | } |
653 | ||
654 | return ret; | |
655 | } | |
656 | ||
e7a3e8b2 DB |
657 | SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, |
658 | size_t, count) | |
659 | { | |
660 | return ksys_write(fd, buf, count); | |
661 | } | |
662 | ||
36028d5d DB |
663 | ssize_t ksys_pread64(unsigned int fd, char __user *buf, size_t count, |
664 | loff_t pos) | |
1da177e4 | 665 | { |
2903ff01 | 666 | struct fd f; |
1da177e4 | 667 | ssize_t ret = -EBADF; |
1da177e4 LT |
668 | |
669 | if (pos < 0) | |
670 | return -EINVAL; | |
671 | ||
2903ff01 AV |
672 | f = fdget(fd); |
673 | if (f.file) { | |
1da177e4 | 674 | ret = -ESPIPE; |
2903ff01 AV |
675 | if (f.file->f_mode & FMODE_PREAD) |
676 | ret = vfs_read(f.file, buf, count, &pos); | |
677 | fdput(f); | |
1da177e4 LT |
678 | } |
679 | ||
680 | return ret; | |
681 | } | |
682 | ||
36028d5d DB |
683 | SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf, |
684 | size_t, count, loff_t, pos) | |
685 | { | |
686 | return ksys_pread64(fd, buf, count, pos); | |
687 | } | |
688 | ||
689 | ssize_t ksys_pwrite64(unsigned int fd, const char __user *buf, | |
690 | size_t count, loff_t pos) | |
1da177e4 | 691 | { |
2903ff01 | 692 | struct fd f; |
1da177e4 | 693 | ssize_t ret = -EBADF; |
1da177e4 LT |
694 | |
695 | if (pos < 0) | |
696 | return -EINVAL; | |
697 | ||
2903ff01 AV |
698 | f = fdget(fd); |
699 | if (f.file) { | |
1da177e4 | 700 | ret = -ESPIPE; |
2903ff01 AV |
701 | if (f.file->f_mode & FMODE_PWRITE) |
702 | ret = vfs_write(f.file, buf, count, &pos); | |
703 | fdput(f); | |
1da177e4 LT |
704 | } |
705 | ||
706 | return ret; | |
707 | } | |
708 | ||
36028d5d DB |
709 | SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf, |
710 | size_t, count, loff_t, pos) | |
711 | { | |
712 | return ksys_pwrite64(fd, buf, count, pos); | |
713 | } | |
714 | ||
ac15ac06 | 715 | static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter, |
ddef7ed2 | 716 | loff_t *ppos, int type, rwf_t flags) |
293bc982 AV |
717 | { |
718 | struct kiocb kiocb; | |
293bc982 AV |
719 | ssize_t ret; |
720 | ||
721 | init_sync_kiocb(&kiocb, filp); | |
fdd2f5b7 GR |
722 | ret = kiocb_set_rw_flags(&kiocb, flags); |
723 | if (ret) | |
724 | return ret; | |
438ab720 | 725 | kiocb.ki_pos = (ppos ? *ppos : 0); |
293bc982 | 726 | |
0f78d06a | 727 | if (type == READ) |
bb7462b6 | 728 | ret = call_read_iter(filp, &kiocb, iter); |
0f78d06a | 729 | else |
bb7462b6 | 730 | ret = call_write_iter(filp, &kiocb, iter); |
599bd19b | 731 | BUG_ON(ret == -EIOCBQUEUED); |
438ab720 KS |
732 | if (ppos) |
733 | *ppos = kiocb.ki_pos; | |
293bc982 AV |
734 | return ret; |
735 | } | |
736 | ||
ee0b3e67 | 737 | /* Do it by hand, with file-ops */ |
ac15ac06 | 738 | static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter, |
ddef7ed2 | 739 | loff_t *ppos, int type, rwf_t flags) |
ee0b3e67 | 740 | { |
ee0b3e67 BP |
741 | ssize_t ret = 0; |
742 | ||
97be7ebe | 743 | if (flags & ~RWF_HIPRI) |
793b80ef CH |
744 | return -EOPNOTSUPP; |
745 | ||
ac15ac06 AV |
746 | while (iov_iter_count(iter)) { |
747 | struct iovec iovec = iov_iter_iovec(iter); | |
ee0b3e67 BP |
748 | ssize_t nr; |
749 | ||
0f78d06a MS |
750 | if (type == READ) { |
751 | nr = filp->f_op->read(filp, iovec.iov_base, | |
752 | iovec.iov_len, ppos); | |
753 | } else { | |
754 | nr = filp->f_op->write(filp, iovec.iov_base, | |
755 | iovec.iov_len, ppos); | |
756 | } | |
ee0b3e67 BP |
757 | |
758 | if (nr < 0) { | |
759 | if (!ret) | |
760 | ret = nr; | |
761 | break; | |
762 | } | |
763 | ret += nr; | |
ac15ac06 | 764 | if (nr != iovec.iov_len) |
ee0b3e67 | 765 | break; |
ac15ac06 | 766 | iov_iter_advance(iter, nr); |
ee0b3e67 BP |
767 | } |
768 | ||
769 | return ret; | |
770 | } | |
771 | ||
ffecee4f VN |
772 | /** |
773 | * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace | |
774 | * into the kernel and check that it is valid. | |
775 | * | |
776 | * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE. | |
777 | * @uvector: Pointer to the userspace array. | |
778 | * @nr_segs: Number of elements in userspace array. | |
779 | * @fast_segs: Number of elements in @fast_pointer. | |
780 | * @fast_pointer: Pointer to (usually small on-stack) kernel array. | |
781 | * @ret_pointer: (output parameter) Pointer to a variable that will point to | |
782 | * either @fast_pointer, a newly allocated kernel array, or NULL, | |
783 | * depending on which array was used. | |
784 | * | |
785 | * This function copies an array of &struct iovec of @nr_segs from | |
786 | * userspace into the kernel and checks that each element is valid (e.g. | |
787 | * it does not point to a kernel address or cause overflow by being too | |
788 | * large, etc.). | |
789 | * | |
790 | * As an optimization, the caller may provide a pointer to a small | |
791 | * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long | |
792 | * (the size of this array, or 0 if unused, should be given in @fast_segs). | |
793 | * | |
794 | * @ret_pointer will always point to the array that was used, so the | |
795 | * caller must take care not to call kfree() on it e.g. in case the | |
796 | * @fast_pointer array was used and it was allocated on the stack. | |
797 | * | |
798 | * Return: The total number of bytes covered by the iovec array on success | |
799 | * or a negative error code on error. | |
800 | */ | |
eed4e51f BP |
801 | ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector, |
802 | unsigned long nr_segs, unsigned long fast_segs, | |
803 | struct iovec *fast_pointer, | |
ac34ebb3 | 804 | struct iovec **ret_pointer) |
435f49a5 | 805 | { |
eed4e51f | 806 | unsigned long seg; |
435f49a5 | 807 | ssize_t ret; |
eed4e51f BP |
808 | struct iovec *iov = fast_pointer; |
809 | ||
435f49a5 LT |
810 | /* |
811 | * SuS says "The readv() function *may* fail if the iovcnt argument | |
812 | * was less than or equal to 0, or greater than {IOV_MAX}. Linux has | |
813 | * traditionally returned zero for zero segments, so... | |
814 | */ | |
eed4e51f BP |
815 | if (nr_segs == 0) { |
816 | ret = 0; | |
435f49a5 | 817 | goto out; |
eed4e51f BP |
818 | } |
819 | ||
435f49a5 LT |
820 | /* |
821 | * First get the "struct iovec" from user memory and | |
822 | * verify all the pointers | |
823 | */ | |
eed4e51f BP |
824 | if (nr_segs > UIO_MAXIOV) { |
825 | ret = -EINVAL; | |
435f49a5 | 826 | goto out; |
eed4e51f BP |
827 | } |
828 | if (nr_segs > fast_segs) { | |
6da2ec56 | 829 | iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL); |
eed4e51f BP |
830 | if (iov == NULL) { |
831 | ret = -ENOMEM; | |
435f49a5 | 832 | goto out; |
eed4e51f | 833 | } |
435f49a5 | 834 | } |
eed4e51f BP |
835 | if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) { |
836 | ret = -EFAULT; | |
435f49a5 | 837 | goto out; |
eed4e51f BP |
838 | } |
839 | ||
435f49a5 | 840 | /* |
eed4e51f BP |
841 | * According to the Single Unix Specification we should return EINVAL |
842 | * if an element length is < 0 when cast to ssize_t or if the | |
843 | * total length would overflow the ssize_t return value of the | |
844 | * system call. | |
435f49a5 LT |
845 | * |
846 | * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the | |
847 | * overflow case. | |
848 | */ | |
eed4e51f | 849 | ret = 0; |
435f49a5 LT |
850 | for (seg = 0; seg < nr_segs; seg++) { |
851 | void __user *buf = iov[seg].iov_base; | |
852 | ssize_t len = (ssize_t)iov[seg].iov_len; | |
eed4e51f BP |
853 | |
854 | /* see if we we're about to use an invalid len or if | |
855 | * it's about to overflow ssize_t */ | |
435f49a5 | 856 | if (len < 0) { |
eed4e51f | 857 | ret = -EINVAL; |
435f49a5 | 858 | goto out; |
eed4e51f | 859 | } |
ac34ebb3 | 860 | if (type >= 0 |
96d4f267 | 861 | && unlikely(!access_ok(buf, len))) { |
eed4e51f | 862 | ret = -EFAULT; |
435f49a5 LT |
863 | goto out; |
864 | } | |
865 | if (len > MAX_RW_COUNT - ret) { | |
866 | len = MAX_RW_COUNT - ret; | |
867 | iov[seg].iov_len = len; | |
eed4e51f | 868 | } |
eed4e51f | 869 | ret += len; |
435f49a5 | 870 | } |
eed4e51f BP |
871 | out: |
872 | *ret_pointer = iov; | |
873 | return ret; | |
874 | } | |
875 | ||
f5029855 AV |
876 | #ifdef CONFIG_COMPAT |
877 | ssize_t compat_rw_copy_check_uvector(int type, | |
878 | const struct compat_iovec __user *uvector, unsigned long nr_segs, | |
879 | unsigned long fast_segs, struct iovec *fast_pointer, | |
880 | struct iovec **ret_pointer) | |
881 | { | |
882 | compat_ssize_t tot_len; | |
883 | struct iovec *iov = *ret_pointer = fast_pointer; | |
884 | ssize_t ret = 0; | |
885 | int seg; | |
886 | ||
887 | /* | |
888 | * SuS says "The readv() function *may* fail if the iovcnt argument | |
889 | * was less than or equal to 0, or greater than {IOV_MAX}. Linux has | |
890 | * traditionally returned zero for zero segments, so... | |
891 | */ | |
892 | if (nr_segs == 0) | |
893 | goto out; | |
894 | ||
895 | ret = -EINVAL; | |
896 | if (nr_segs > UIO_MAXIOV) | |
897 | goto out; | |
898 | if (nr_segs > fast_segs) { | |
899 | ret = -ENOMEM; | |
6da2ec56 | 900 | iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL); |
f5029855 AV |
901 | if (iov == NULL) |
902 | goto out; | |
903 | } | |
904 | *ret_pointer = iov; | |
905 | ||
906 | ret = -EFAULT; | |
96d4f267 | 907 | if (!access_ok(uvector, nr_segs*sizeof(*uvector))) |
f5029855 AV |
908 | goto out; |
909 | ||
910 | /* | |
911 | * Single unix specification: | |
912 | * We should -EINVAL if an element length is not >= 0 and fitting an | |
913 | * ssize_t. | |
914 | * | |
915 | * In Linux, the total length is limited to MAX_RW_COUNT, there is | |
916 | * no overflow possibility. | |
917 | */ | |
918 | tot_len = 0; | |
919 | ret = -EINVAL; | |
920 | for (seg = 0; seg < nr_segs; seg++) { | |
921 | compat_uptr_t buf; | |
922 | compat_ssize_t len; | |
923 | ||
924 | if (__get_user(len, &uvector->iov_len) || | |
925 | __get_user(buf, &uvector->iov_base)) { | |
926 | ret = -EFAULT; | |
927 | goto out; | |
928 | } | |
929 | if (len < 0) /* size_t not fitting in compat_ssize_t .. */ | |
930 | goto out; | |
931 | if (type >= 0 && | |
96d4f267 | 932 | !access_ok(compat_ptr(buf), len)) { |
f5029855 AV |
933 | ret = -EFAULT; |
934 | goto out; | |
935 | } | |
936 | if (len > MAX_RW_COUNT - tot_len) | |
937 | len = MAX_RW_COUNT - tot_len; | |
938 | tot_len += len; | |
939 | iov->iov_base = compat_ptr(buf); | |
940 | iov->iov_len = (compat_size_t) len; | |
941 | uvector++; | |
942 | iov++; | |
943 | } | |
944 | ret = tot_len; | |
945 | ||
946 | out: | |
947 | return ret; | |
948 | } | |
949 | #endif | |
950 | ||
19c73586 | 951 | static ssize_t do_iter_read(struct file *file, struct iov_iter *iter, |
ddef7ed2 | 952 | loff_t *pos, rwf_t flags) |
1da177e4 | 953 | { |
1da177e4 | 954 | size_t tot_len; |
7687a7a4 | 955 | ssize_t ret = 0; |
1da177e4 | 956 | |
edab5fe3 CH |
957 | if (!(file->f_mode & FMODE_READ)) |
958 | return -EBADF; | |
959 | if (!(file->f_mode & FMODE_CAN_READ)) | |
960 | return -EINVAL; | |
961 | ||
7687a7a4 | 962 | tot_len = iov_iter_count(iter); |
0504c074 AV |
963 | if (!tot_len) |
964 | goto out; | |
19c73586 | 965 | ret = rw_verify_area(READ, file, pos, tot_len); |
e28cc715 | 966 | if (ret < 0) |
19c73586 | 967 | return ret; |
1da177e4 | 968 | |
19c73586 CH |
969 | if (file->f_op->read_iter) |
970 | ret = do_iter_readv_writev(file, iter, pos, READ, flags); | |
ee0b3e67 | 971 | else |
19c73586 | 972 | ret = do_loop_readv_writev(file, iter, pos, READ, flags); |
1da177e4 | 973 | out: |
19c73586 CH |
974 | if (ret >= 0) |
975 | fsnotify_access(file); | |
1da177e4 | 976 | return ret; |
1da177e4 LT |
977 | } |
978 | ||
5dcdc43e JX |
979 | ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb, |
980 | struct iov_iter *iter) | |
981 | { | |
982 | size_t tot_len; | |
983 | ssize_t ret = 0; | |
984 | ||
985 | if (!file->f_op->read_iter) | |
986 | return -EINVAL; | |
987 | if (!(file->f_mode & FMODE_READ)) | |
988 | return -EBADF; | |
989 | if (!(file->f_mode & FMODE_CAN_READ)) | |
990 | return -EINVAL; | |
991 | ||
992 | tot_len = iov_iter_count(iter); | |
993 | if (!tot_len) | |
994 | goto out; | |
995 | ret = rw_verify_area(READ, file, &iocb->ki_pos, tot_len); | |
996 | if (ret < 0) | |
997 | return ret; | |
998 | ||
999 | ret = call_read_iter(file, iocb, iter); | |
1000 | out: | |
1001 | if (ret >= 0) | |
1002 | fsnotify_access(file); | |
1003 | return ret; | |
1004 | } | |
1005 | EXPORT_SYMBOL(vfs_iocb_iter_read); | |
1006 | ||
18e9710e | 1007 | ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos, |
ddef7ed2 | 1008 | rwf_t flags) |
7687a7a4 | 1009 | { |
18e9710e CH |
1010 | if (!file->f_op->read_iter) |
1011 | return -EINVAL; | |
1012 | return do_iter_read(file, iter, ppos, flags); | |
1013 | } | |
1014 | EXPORT_SYMBOL(vfs_iter_read); | |
7687a7a4 | 1015 | |
19c73586 | 1016 | static ssize_t do_iter_write(struct file *file, struct iov_iter *iter, |
ddef7ed2 | 1017 | loff_t *pos, rwf_t flags) |
19c73586 CH |
1018 | { |
1019 | size_t tot_len; | |
1020 | ssize_t ret = 0; | |
03d95eb2 | 1021 | |
edab5fe3 CH |
1022 | if (!(file->f_mode & FMODE_WRITE)) |
1023 | return -EBADF; | |
1024 | if (!(file->f_mode & FMODE_CAN_WRITE)) | |
1025 | return -EINVAL; | |
1026 | ||
19c73586 CH |
1027 | tot_len = iov_iter_count(iter); |
1028 | if (!tot_len) | |
1029 | return 0; | |
1030 | ret = rw_verify_area(WRITE, file, pos, tot_len); | |
7687a7a4 MS |
1031 | if (ret < 0) |
1032 | return ret; | |
1033 | ||
19c73586 CH |
1034 | if (file->f_op->write_iter) |
1035 | ret = do_iter_readv_writev(file, iter, pos, WRITE, flags); | |
1036 | else | |
1037 | ret = do_loop_readv_writev(file, iter, pos, WRITE, flags); | |
19c73586 CH |
1038 | if (ret > 0) |
1039 | fsnotify_modify(file); | |
7687a7a4 MS |
1040 | return ret; |
1041 | } | |
1042 | ||
5dcdc43e JX |
1043 | ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb, |
1044 | struct iov_iter *iter) | |
1045 | { | |
1046 | size_t tot_len; | |
1047 | ssize_t ret = 0; | |
1048 | ||
1049 | if (!file->f_op->write_iter) | |
1050 | return -EINVAL; | |
1051 | if (!(file->f_mode & FMODE_WRITE)) | |
1052 | return -EBADF; | |
1053 | if (!(file->f_mode & FMODE_CAN_WRITE)) | |
1054 | return -EINVAL; | |
1055 | ||
1056 | tot_len = iov_iter_count(iter); | |
1057 | if (!tot_len) | |
1058 | return 0; | |
1059 | ret = rw_verify_area(WRITE, file, &iocb->ki_pos, tot_len); | |
1060 | if (ret < 0) | |
1061 | return ret; | |
1062 | ||
1063 | ret = call_write_iter(file, iocb, iter); | |
1064 | if (ret > 0) | |
1065 | fsnotify_modify(file); | |
1066 | ||
1067 | return ret; | |
1068 | } | |
1069 | EXPORT_SYMBOL(vfs_iocb_iter_write); | |
1070 | ||
abbb6589 | 1071 | ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos, |
ddef7ed2 | 1072 | rwf_t flags) |
abbb6589 CH |
1073 | { |
1074 | if (!file->f_op->write_iter) | |
1075 | return -EINVAL; | |
1076 | return do_iter_write(file, iter, ppos, flags); | |
1077 | } | |
1078 | EXPORT_SYMBOL(vfs_iter_write); | |
1079 | ||
1da177e4 | 1080 | ssize_t vfs_readv(struct file *file, const struct iovec __user *vec, |
ddef7ed2 | 1081 | unsigned long vlen, loff_t *pos, rwf_t flags) |
1da177e4 | 1082 | { |
7687a7a4 MS |
1083 | struct iovec iovstack[UIO_FASTIOV]; |
1084 | struct iovec *iov = iovstack; | |
1085 | struct iov_iter iter; | |
1086 | ssize_t ret; | |
1da177e4 | 1087 | |
251b42a1 | 1088 | ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter); |
edab5fe3 CH |
1089 | if (ret >= 0) { |
1090 | ret = do_iter_read(file, &iter, pos, flags); | |
1091 | kfree(iov); | |
1092 | } | |
1da177e4 | 1093 | |
251b42a1 CH |
1094 | return ret; |
1095 | } | |
1da177e4 | 1096 | |
9725d4ce | 1097 | static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec, |
ddef7ed2 | 1098 | unsigned long vlen, loff_t *pos, rwf_t flags) |
1da177e4 | 1099 | { |
251b42a1 CH |
1100 | struct iovec iovstack[UIO_FASTIOV]; |
1101 | struct iovec *iov = iovstack; | |
1102 | struct iov_iter iter; | |
1103 | ssize_t ret; | |
1da177e4 | 1104 | |
251b42a1 | 1105 | ret = import_iovec(WRITE, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter); |
edab5fe3 | 1106 | if (ret >= 0) { |
62473a2d | 1107 | file_start_write(file); |
edab5fe3 | 1108 | ret = do_iter_write(file, &iter, pos, flags); |
62473a2d | 1109 | file_end_write(file); |
edab5fe3 CH |
1110 | kfree(iov); |
1111 | } | |
251b42a1 | 1112 | return ret; |
1da177e4 | 1113 | } |
1da177e4 | 1114 | |
f17d8b35 | 1115 | static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec, |
ddef7ed2 | 1116 | unsigned long vlen, rwf_t flags) |
1da177e4 | 1117 | { |
9c225f26 | 1118 | struct fd f = fdget_pos(fd); |
1da177e4 | 1119 | ssize_t ret = -EBADF; |
1da177e4 | 1120 | |
2903ff01 | 1121 | if (f.file) { |
438ab720 KS |
1122 | loff_t pos, *ppos = file_ppos(f.file); |
1123 | if (ppos) { | |
1124 | pos = *ppos; | |
1125 | ppos = &pos; | |
1126 | } | |
1127 | ret = vfs_readv(f.file, vec, vlen, ppos, flags); | |
1128 | if (ret >= 0 && ppos) | |
1129 | f.file->f_pos = pos; | |
9c225f26 | 1130 | fdput_pos(f); |
1da177e4 LT |
1131 | } |
1132 | ||
1133 | if (ret > 0) | |
4b98d11b AD |
1134 | add_rchar(current, ret); |
1135 | inc_syscr(current); | |
1da177e4 LT |
1136 | return ret; |
1137 | } | |
1138 | ||
f17d8b35 | 1139 | static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec, |
ddef7ed2 | 1140 | unsigned long vlen, rwf_t flags) |
1da177e4 | 1141 | { |
9c225f26 | 1142 | struct fd f = fdget_pos(fd); |
1da177e4 | 1143 | ssize_t ret = -EBADF; |
1da177e4 | 1144 | |
2903ff01 | 1145 | if (f.file) { |
438ab720 KS |
1146 | loff_t pos, *ppos = file_ppos(f.file); |
1147 | if (ppos) { | |
1148 | pos = *ppos; | |
1149 | ppos = &pos; | |
1150 | } | |
1151 | ret = vfs_writev(f.file, vec, vlen, ppos, flags); | |
1152 | if (ret >= 0 && ppos) | |
1153 | f.file->f_pos = pos; | |
9c225f26 | 1154 | fdput_pos(f); |
1da177e4 LT |
1155 | } |
1156 | ||
1157 | if (ret > 0) | |
4b98d11b AD |
1158 | add_wchar(current, ret); |
1159 | inc_syscw(current); | |
1da177e4 LT |
1160 | return ret; |
1161 | } | |
1162 | ||
601cc11d LT |
1163 | static inline loff_t pos_from_hilo(unsigned long high, unsigned long low) |
1164 | { | |
1165 | #define HALF_LONG_BITS (BITS_PER_LONG / 2) | |
1166 | return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low; | |
1167 | } | |
1168 | ||
f17d8b35 | 1169 | static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec, |
ddef7ed2 | 1170 | unsigned long vlen, loff_t pos, rwf_t flags) |
f3554f4b | 1171 | { |
2903ff01 | 1172 | struct fd f; |
f3554f4b | 1173 | ssize_t ret = -EBADF; |
f3554f4b GH |
1174 | |
1175 | if (pos < 0) | |
1176 | return -EINVAL; | |
1177 | ||
2903ff01 AV |
1178 | f = fdget(fd); |
1179 | if (f.file) { | |
f3554f4b | 1180 | ret = -ESPIPE; |
2903ff01 | 1181 | if (f.file->f_mode & FMODE_PREAD) |
f17d8b35 | 1182 | ret = vfs_readv(f.file, vec, vlen, &pos, flags); |
2903ff01 | 1183 | fdput(f); |
f3554f4b GH |
1184 | } |
1185 | ||
1186 | if (ret > 0) | |
1187 | add_rchar(current, ret); | |
1188 | inc_syscr(current); | |
1189 | return ret; | |
1190 | } | |
1191 | ||
f17d8b35 | 1192 | static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec, |
ddef7ed2 | 1193 | unsigned long vlen, loff_t pos, rwf_t flags) |
f3554f4b | 1194 | { |
2903ff01 | 1195 | struct fd f; |
f3554f4b | 1196 | ssize_t ret = -EBADF; |
f3554f4b GH |
1197 | |
1198 | if (pos < 0) | |
1199 | return -EINVAL; | |
1200 | ||
2903ff01 AV |
1201 | f = fdget(fd); |
1202 | if (f.file) { | |
f3554f4b | 1203 | ret = -ESPIPE; |
2903ff01 | 1204 | if (f.file->f_mode & FMODE_PWRITE) |
f17d8b35 | 1205 | ret = vfs_writev(f.file, vec, vlen, &pos, flags); |
2903ff01 | 1206 | fdput(f); |
f3554f4b GH |
1207 | } |
1208 | ||
1209 | if (ret > 0) | |
1210 | add_wchar(current, ret); | |
1211 | inc_syscw(current); | |
1212 | return ret; | |
1213 | } | |
1214 | ||
f17d8b35 MT |
1215 | SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec, |
1216 | unsigned long, vlen) | |
1217 | { | |
1218 | return do_readv(fd, vec, vlen, 0); | |
1219 | } | |
1220 | ||
1221 | SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec, | |
1222 | unsigned long, vlen) | |
1223 | { | |
1224 | return do_writev(fd, vec, vlen, 0); | |
1225 | } | |
1226 | ||
1227 | SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec, | |
1228 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) | |
1229 | { | |
1230 | loff_t pos = pos_from_hilo(pos_h, pos_l); | |
1231 | ||
1232 | return do_preadv(fd, vec, vlen, pos, 0); | |
1233 | } | |
1234 | ||
1235 | SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec, | |
1236 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h, | |
ddef7ed2 | 1237 | rwf_t, flags) |
f17d8b35 MT |
1238 | { |
1239 | loff_t pos = pos_from_hilo(pos_h, pos_l); | |
1240 | ||
1241 | if (pos == -1) | |
1242 | return do_readv(fd, vec, vlen, flags); | |
1243 | ||
1244 | return do_preadv(fd, vec, vlen, pos, flags); | |
1245 | } | |
1246 | ||
1247 | SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, | |
1248 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) | |
1249 | { | |
1250 | loff_t pos = pos_from_hilo(pos_h, pos_l); | |
1251 | ||
1252 | return do_pwritev(fd, vec, vlen, pos, 0); | |
1253 | } | |
1254 | ||
1255 | SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec, | |
1256 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h, | |
ddef7ed2 | 1257 | rwf_t, flags) |
f17d8b35 MT |
1258 | { |
1259 | loff_t pos = pos_from_hilo(pos_h, pos_l); | |
1260 | ||
1261 | if (pos == -1) | |
1262 | return do_writev(fd, vec, vlen, flags); | |
1263 | ||
1264 | return do_pwritev(fd, vec, vlen, pos, flags); | |
1265 | } | |
1266 | ||
72ec3516 | 1267 | #ifdef CONFIG_COMPAT |
72ec3516 AV |
1268 | static size_t compat_readv(struct file *file, |
1269 | const struct compat_iovec __user *vec, | |
ddef7ed2 | 1270 | unsigned long vlen, loff_t *pos, rwf_t flags) |
72ec3516 | 1271 | { |
72ec3516 AV |
1272 | struct iovec iovstack[UIO_FASTIOV]; |
1273 | struct iovec *iov = iovstack; | |
ac15ac06 | 1274 | struct iov_iter iter; |
72ec3516 | 1275 | ssize_t ret; |
72ec3516 | 1276 | |
26c87fb7 | 1277 | ret = compat_import_iovec(READ, vec, vlen, UIO_FASTIOV, &iov, &iter); |
edab5fe3 CH |
1278 | if (ret >= 0) { |
1279 | ret = do_iter_read(file, &iter, pos, flags); | |
1280 | kfree(iov); | |
1281 | } | |
72ec3516 AV |
1282 | if (ret > 0) |
1283 | add_rchar(current, ret); | |
1284 | inc_syscr(current); | |
1285 | return ret; | |
1286 | } | |
1287 | ||
f17d8b35 MT |
1288 | static size_t do_compat_readv(compat_ulong_t fd, |
1289 | const struct compat_iovec __user *vec, | |
ddef7ed2 | 1290 | compat_ulong_t vlen, rwf_t flags) |
72ec3516 | 1291 | { |
9c225f26 | 1292 | struct fd f = fdget_pos(fd); |
72ec3516 AV |
1293 | ssize_t ret; |
1294 | loff_t pos; | |
1295 | ||
1296 | if (!f.file) | |
1297 | return -EBADF; | |
1298 | pos = f.file->f_pos; | |
f17d8b35 | 1299 | ret = compat_readv(f.file, vec, vlen, &pos, flags); |
5faf153e AV |
1300 | if (ret >= 0) |
1301 | f.file->f_pos = pos; | |
9c225f26 | 1302 | fdput_pos(f); |
72ec3516 | 1303 | return ret; |
f17d8b35 | 1304 | |
72ec3516 AV |
1305 | } |
1306 | ||
f17d8b35 MT |
1307 | COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd, |
1308 | const struct compat_iovec __user *,vec, | |
1309 | compat_ulong_t, vlen) | |
1310 | { | |
1311 | return do_compat_readv(fd, vec, vlen, 0); | |
1312 | } | |
1313 | ||
1314 | static long do_compat_preadv64(unsigned long fd, | |
378a10f3 | 1315 | const struct compat_iovec __user *vec, |
ddef7ed2 | 1316 | unsigned long vlen, loff_t pos, rwf_t flags) |
72ec3516 AV |
1317 | { |
1318 | struct fd f; | |
1319 | ssize_t ret; | |
1320 | ||
1321 | if (pos < 0) | |
1322 | return -EINVAL; | |
1323 | f = fdget(fd); | |
1324 | if (!f.file) | |
1325 | return -EBADF; | |
1326 | ret = -ESPIPE; | |
1327 | if (f.file->f_mode & FMODE_PREAD) | |
f17d8b35 | 1328 | ret = compat_readv(f.file, vec, vlen, &pos, flags); |
72ec3516 AV |
1329 | fdput(f); |
1330 | return ret; | |
1331 | } | |
1332 | ||
378a10f3 HC |
1333 | #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64 |
1334 | COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd, | |
1335 | const struct compat_iovec __user *,vec, | |
1336 | unsigned long, vlen, loff_t, pos) | |
1337 | { | |
f17d8b35 | 1338 | return do_compat_preadv64(fd, vec, vlen, pos, 0); |
378a10f3 HC |
1339 | } |
1340 | #endif | |
1341 | ||
dfd948e3 | 1342 | COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd, |
72ec3516 | 1343 | const struct compat_iovec __user *,vec, |
dfd948e3 | 1344 | compat_ulong_t, vlen, u32, pos_low, u32, pos_high) |
72ec3516 AV |
1345 | { |
1346 | loff_t pos = ((loff_t)pos_high << 32) | pos_low; | |
378a10f3 | 1347 | |
f17d8b35 MT |
1348 | return do_compat_preadv64(fd, vec, vlen, pos, 0); |
1349 | } | |
1350 | ||
3ebfd81f L |
1351 | #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2 |
1352 | COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd, | |
1353 | const struct compat_iovec __user *,vec, | |
ddef7ed2 | 1354 | unsigned long, vlen, loff_t, pos, rwf_t, flags) |
3ebfd81f | 1355 | { |
cc4b1242 AJ |
1356 | if (pos == -1) |
1357 | return do_compat_readv(fd, vec, vlen, flags); | |
1358 | ||
3ebfd81f L |
1359 | return do_compat_preadv64(fd, vec, vlen, pos, flags); |
1360 | } | |
1361 | #endif | |
1362 | ||
f17d8b35 MT |
1363 | COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd, |
1364 | const struct compat_iovec __user *,vec, | |
1365 | compat_ulong_t, vlen, u32, pos_low, u32, pos_high, | |
ddef7ed2 | 1366 | rwf_t, flags) |
f17d8b35 MT |
1367 | { |
1368 | loff_t pos = ((loff_t)pos_high << 32) | pos_low; | |
1369 | ||
1370 | if (pos == -1) | |
1371 | return do_compat_readv(fd, vec, vlen, flags); | |
1372 | ||
1373 | return do_compat_preadv64(fd, vec, vlen, pos, flags); | |
72ec3516 AV |
1374 | } |
1375 | ||
1376 | static size_t compat_writev(struct file *file, | |
1377 | const struct compat_iovec __user *vec, | |
ddef7ed2 | 1378 | unsigned long vlen, loff_t *pos, rwf_t flags) |
72ec3516 | 1379 | { |
26c87fb7 CH |
1380 | struct iovec iovstack[UIO_FASTIOV]; |
1381 | struct iovec *iov = iovstack; | |
1382 | struct iov_iter iter; | |
edab5fe3 | 1383 | ssize_t ret; |
72ec3516 | 1384 | |
26c87fb7 | 1385 | ret = compat_import_iovec(WRITE, vec, vlen, UIO_FASTIOV, &iov, &iter); |
edab5fe3 | 1386 | if (ret >= 0) { |
62473a2d | 1387 | file_start_write(file); |
edab5fe3 | 1388 | ret = do_iter_write(file, &iter, pos, flags); |
62473a2d | 1389 | file_end_write(file); |
edab5fe3 CH |
1390 | kfree(iov); |
1391 | } | |
72ec3516 AV |
1392 | if (ret > 0) |
1393 | add_wchar(current, ret); | |
1394 | inc_syscw(current); | |
1395 | return ret; | |
1396 | } | |
1397 | ||
f17d8b35 MT |
1398 | static size_t do_compat_writev(compat_ulong_t fd, |
1399 | const struct compat_iovec __user* vec, | |
ddef7ed2 | 1400 | compat_ulong_t vlen, rwf_t flags) |
72ec3516 | 1401 | { |
9c225f26 | 1402 | struct fd f = fdget_pos(fd); |
72ec3516 AV |
1403 | ssize_t ret; |
1404 | loff_t pos; | |
1405 | ||
1406 | if (!f.file) | |
1407 | return -EBADF; | |
1408 | pos = f.file->f_pos; | |
f17d8b35 | 1409 | ret = compat_writev(f.file, vec, vlen, &pos, flags); |
5faf153e AV |
1410 | if (ret >= 0) |
1411 | f.file->f_pos = pos; | |
9c225f26 | 1412 | fdput_pos(f); |
72ec3516 AV |
1413 | return ret; |
1414 | } | |
1415 | ||
f17d8b35 MT |
1416 | COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd, |
1417 | const struct compat_iovec __user *, vec, | |
1418 | compat_ulong_t, vlen) | |
1419 | { | |
1420 | return do_compat_writev(fd, vec, vlen, 0); | |
1421 | } | |
1422 | ||
1423 | static long do_compat_pwritev64(unsigned long fd, | |
378a10f3 | 1424 | const struct compat_iovec __user *vec, |
ddef7ed2 | 1425 | unsigned long vlen, loff_t pos, rwf_t flags) |
72ec3516 AV |
1426 | { |
1427 | struct fd f; | |
1428 | ssize_t ret; | |
1429 | ||
1430 | if (pos < 0) | |
1431 | return -EINVAL; | |
1432 | f = fdget(fd); | |
1433 | if (!f.file) | |
1434 | return -EBADF; | |
1435 | ret = -ESPIPE; | |
1436 | if (f.file->f_mode & FMODE_PWRITE) | |
f17d8b35 | 1437 | ret = compat_writev(f.file, vec, vlen, &pos, flags); |
72ec3516 AV |
1438 | fdput(f); |
1439 | return ret; | |
1440 | } | |
1441 | ||
378a10f3 HC |
1442 | #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64 |
1443 | COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd, | |
1444 | const struct compat_iovec __user *,vec, | |
1445 | unsigned long, vlen, loff_t, pos) | |
1446 | { | |
f17d8b35 | 1447 | return do_compat_pwritev64(fd, vec, vlen, pos, 0); |
378a10f3 HC |
1448 | } |
1449 | #endif | |
1450 | ||
dfd948e3 | 1451 | COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd, |
72ec3516 | 1452 | const struct compat_iovec __user *,vec, |
dfd948e3 | 1453 | compat_ulong_t, vlen, u32, pos_low, u32, pos_high) |
72ec3516 AV |
1454 | { |
1455 | loff_t pos = ((loff_t)pos_high << 32) | pos_low; | |
378a10f3 | 1456 | |
f17d8b35 | 1457 | return do_compat_pwritev64(fd, vec, vlen, pos, 0); |
72ec3516 | 1458 | } |
f17d8b35 | 1459 | |
3ebfd81f L |
1460 | #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2 |
1461 | COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd, | |
1462 | const struct compat_iovec __user *,vec, | |
ddef7ed2 | 1463 | unsigned long, vlen, loff_t, pos, rwf_t, flags) |
3ebfd81f | 1464 | { |
cc4b1242 AJ |
1465 | if (pos == -1) |
1466 | return do_compat_writev(fd, vec, vlen, flags); | |
1467 | ||
3ebfd81f L |
1468 | return do_compat_pwritev64(fd, vec, vlen, pos, flags); |
1469 | } | |
1470 | #endif | |
1471 | ||
f17d8b35 MT |
1472 | COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd, |
1473 | const struct compat_iovec __user *,vec, | |
ddef7ed2 | 1474 | compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags) |
f17d8b35 MT |
1475 | { |
1476 | loff_t pos = ((loff_t)pos_high << 32) | pos_low; | |
1477 | ||
1478 | if (pos == -1) | |
1479 | return do_compat_writev(fd, vec, vlen, flags); | |
1480 | ||
1481 | return do_compat_pwritev64(fd, vec, vlen, pos, flags); | |
72ec3516 | 1482 | } |
f17d8b35 | 1483 | |
72ec3516 AV |
1484 | #endif |
1485 | ||
19f4fc3a AV |
1486 | static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, |
1487 | size_t count, loff_t max) | |
1da177e4 | 1488 | { |
2903ff01 AV |
1489 | struct fd in, out; |
1490 | struct inode *in_inode, *out_inode; | |
1da177e4 | 1491 | loff_t pos; |
7995bd28 | 1492 | loff_t out_pos; |
1da177e4 | 1493 | ssize_t retval; |
2903ff01 | 1494 | int fl; |
1da177e4 LT |
1495 | |
1496 | /* | |
1497 | * Get input file, and verify that it is ok.. | |
1498 | */ | |
1499 | retval = -EBADF; | |
2903ff01 AV |
1500 | in = fdget(in_fd); |
1501 | if (!in.file) | |
1da177e4 | 1502 | goto out; |
2903ff01 | 1503 | if (!(in.file->f_mode & FMODE_READ)) |
1da177e4 | 1504 | goto fput_in; |
1da177e4 | 1505 | retval = -ESPIPE; |
7995bd28 AV |
1506 | if (!ppos) { |
1507 | pos = in.file->f_pos; | |
1508 | } else { | |
1509 | pos = *ppos; | |
2903ff01 | 1510 | if (!(in.file->f_mode & FMODE_PREAD)) |
1da177e4 | 1511 | goto fput_in; |
7995bd28 AV |
1512 | } |
1513 | retval = rw_verify_area(READ, in.file, &pos, count); | |
e28cc715 | 1514 | if (retval < 0) |
1da177e4 | 1515 | goto fput_in; |
bc61384d AV |
1516 | if (count > MAX_RW_COUNT) |
1517 | count = MAX_RW_COUNT; | |
1da177e4 | 1518 | |
1da177e4 LT |
1519 | /* |
1520 | * Get output file, and verify that it is ok.. | |
1521 | */ | |
1522 | retval = -EBADF; | |
2903ff01 AV |
1523 | out = fdget(out_fd); |
1524 | if (!out.file) | |
1da177e4 | 1525 | goto fput_in; |
2903ff01 | 1526 | if (!(out.file->f_mode & FMODE_WRITE)) |
1da177e4 | 1527 | goto fput_out; |
496ad9aa AV |
1528 | in_inode = file_inode(in.file); |
1529 | out_inode = file_inode(out.file); | |
7995bd28 AV |
1530 | out_pos = out.file->f_pos; |
1531 | retval = rw_verify_area(WRITE, out.file, &out_pos, count); | |
e28cc715 | 1532 | if (retval < 0) |
1da177e4 LT |
1533 | goto fput_out; |
1534 | ||
1da177e4 LT |
1535 | if (!max) |
1536 | max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); | |
1537 | ||
1da177e4 LT |
1538 | if (unlikely(pos + count > max)) { |
1539 | retval = -EOVERFLOW; | |
1540 | if (pos >= max) | |
1541 | goto fput_out; | |
1542 | count = max - pos; | |
1543 | } | |
1544 | ||
d96e6e71 | 1545 | fl = 0; |
534f2aaa | 1546 | #if 0 |
d96e6e71 JA |
1547 | /* |
1548 | * We need to debate whether we can enable this or not. The | |
1549 | * man page documents EAGAIN return for the output at least, | |
1550 | * and the application is arguably buggy if it doesn't expect | |
1551 | * EAGAIN on a non-blocking file descriptor. | |
1552 | */ | |
2903ff01 | 1553 | if (in.file->f_flags & O_NONBLOCK) |
d96e6e71 | 1554 | fl = SPLICE_F_NONBLOCK; |
534f2aaa | 1555 | #endif |
50cd2c57 | 1556 | file_start_write(out.file); |
7995bd28 | 1557 | retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl); |
50cd2c57 | 1558 | file_end_write(out.file); |
1da177e4 LT |
1559 | |
1560 | if (retval > 0) { | |
4b98d11b AD |
1561 | add_rchar(current, retval); |
1562 | add_wchar(current, retval); | |
a68c2f12 SW |
1563 | fsnotify_access(in.file); |
1564 | fsnotify_modify(out.file); | |
7995bd28 AV |
1565 | out.file->f_pos = out_pos; |
1566 | if (ppos) | |
1567 | *ppos = pos; | |
1568 | else | |
1569 | in.file->f_pos = pos; | |
1da177e4 | 1570 | } |
1da177e4 | 1571 | |
4b98d11b AD |
1572 | inc_syscr(current); |
1573 | inc_syscw(current); | |
7995bd28 | 1574 | if (pos > max) |
1da177e4 LT |
1575 | retval = -EOVERFLOW; |
1576 | ||
1577 | fput_out: | |
2903ff01 | 1578 | fdput(out); |
1da177e4 | 1579 | fput_in: |
2903ff01 | 1580 | fdput(in); |
1da177e4 LT |
1581 | out: |
1582 | return retval; | |
1583 | } | |
1584 | ||
002c8976 | 1585 | SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count) |
1da177e4 LT |
1586 | { |
1587 | loff_t pos; | |
1588 | off_t off; | |
1589 | ssize_t ret; | |
1590 | ||
1591 | if (offset) { | |
1592 | if (unlikely(get_user(off, offset))) | |
1593 | return -EFAULT; | |
1594 | pos = off; | |
1595 | ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS); | |
1596 | if (unlikely(put_user(pos, offset))) | |
1597 | return -EFAULT; | |
1598 | return ret; | |
1599 | } | |
1600 | ||
1601 | return do_sendfile(out_fd, in_fd, NULL, count, 0); | |
1602 | } | |
1603 | ||
002c8976 | 1604 | SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count) |
1da177e4 LT |
1605 | { |
1606 | loff_t pos; | |
1607 | ssize_t ret; | |
1608 | ||
1609 | if (offset) { | |
1610 | if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) | |
1611 | return -EFAULT; | |
1612 | ret = do_sendfile(out_fd, in_fd, &pos, count, 0); | |
1613 | if (unlikely(put_user(pos, offset))) | |
1614 | return -EFAULT; | |
1615 | return ret; | |
1616 | } | |
1617 | ||
1618 | return do_sendfile(out_fd, in_fd, NULL, count, 0); | |
1619 | } | |
19f4fc3a AV |
1620 | |
1621 | #ifdef CONFIG_COMPAT | |
1622 | COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, | |
1623 | compat_off_t __user *, offset, compat_size_t, count) | |
1624 | { | |
1625 | loff_t pos; | |
1626 | off_t off; | |
1627 | ssize_t ret; | |
1628 | ||
1629 | if (offset) { | |
1630 | if (unlikely(get_user(off, offset))) | |
1631 | return -EFAULT; | |
1632 | pos = off; | |
1633 | ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS); | |
1634 | if (unlikely(put_user(pos, offset))) | |
1635 | return -EFAULT; | |
1636 | return ret; | |
1637 | } | |
1638 | ||
1639 | return do_sendfile(out_fd, in_fd, NULL, count, 0); | |
1640 | } | |
1641 | ||
1642 | COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, | |
1643 | compat_loff_t __user *, offset, compat_size_t, count) | |
1644 | { | |
1645 | loff_t pos; | |
1646 | ssize_t ret; | |
1647 | ||
1648 | if (offset) { | |
1649 | if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) | |
1650 | return -EFAULT; | |
1651 | ret = do_sendfile(out_fd, in_fd, &pos, count, 0); | |
1652 | if (unlikely(put_user(pos, offset))) | |
1653 | return -EFAULT; | |
1654 | return ret; | |
1655 | } | |
1656 | ||
1657 | return do_sendfile(out_fd, in_fd, NULL, count, 0); | |
1658 | } | |
1659 | #endif | |
29732938 | 1660 | |
f16acc9d DC |
1661 | /** |
1662 | * generic_copy_file_range - copy data between two files | |
1663 | * @file_in: file structure to read from | |
1664 | * @pos_in: file offset to read from | |
1665 | * @file_out: file structure to write data to | |
1666 | * @pos_out: file offset to write data to | |
1667 | * @len: amount of data to copy | |
1668 | * @flags: copy flags | |
1669 | * | |
1670 | * This is a generic filesystem helper to copy data from one file to another. | |
1671 | * It has no constraints on the source or destination file owners - the files | |
1672 | * can belong to different superblocks and different filesystem types. Short | |
1673 | * copies are allowed. | |
1674 | * | |
1675 | * This should be called from the @file_out filesystem, as per the | |
1676 | * ->copy_file_range() method. | |
1677 | * | |
1678 | * Returns the number of bytes copied or a negative error indicating the | |
1679 | * failure. | |
1680 | */ | |
1681 | ||
1682 | ssize_t generic_copy_file_range(struct file *file_in, loff_t pos_in, | |
1683 | struct file *file_out, loff_t pos_out, | |
1684 | size_t len, unsigned int flags) | |
1685 | { | |
1686 | return do_splice_direct(file_in, &pos_in, file_out, &pos_out, | |
1687 | len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0); | |
1688 | } | |
1689 | EXPORT_SYMBOL(generic_copy_file_range); | |
1690 | ||
64bf5ff5 DC |
1691 | static ssize_t do_copy_file_range(struct file *file_in, loff_t pos_in, |
1692 | struct file *file_out, loff_t pos_out, | |
1693 | size_t len, unsigned int flags) | |
1694 | { | |
5dae222a AG |
1695 | /* |
1696 | * Although we now allow filesystems to handle cross sb copy, passing | |
1697 | * a file of the wrong filesystem type to filesystem driver can result | |
1698 | * in an attempt to dereference the wrong type of ->private_data, so | |
1699 | * avoid doing that until we really have a good reason. NFS defines | |
1700 | * several different file_system_type structures, but they all end up | |
1701 | * using the same ->copy_file_range() function pointer. | |
1702 | */ | |
1703 | if (file_out->f_op->copy_file_range && | |
1704 | file_out->f_op->copy_file_range == file_in->f_op->copy_file_range) | |
64bf5ff5 DC |
1705 | return file_out->f_op->copy_file_range(file_in, pos_in, |
1706 | file_out, pos_out, | |
1707 | len, flags); | |
1708 | ||
1709 | return generic_copy_file_range(file_in, pos_in, file_out, pos_out, len, | |
1710 | flags); | |
1711 | } | |
1712 | ||
29732938 ZB |
1713 | /* |
1714 | * copy_file_range() differs from regular file read and write in that it | |
1715 | * specifically allows return partial success. When it does so is up to | |
1716 | * the copy_file_range method. | |
1717 | */ | |
1718 | ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in, | |
1719 | struct file *file_out, loff_t pos_out, | |
1720 | size_t len, unsigned int flags) | |
1721 | { | |
29732938 ZB |
1722 | ssize_t ret; |
1723 | ||
1724 | if (flags != 0) | |
1725 | return -EINVAL; | |
1726 | ||
96e6e8f4 AG |
1727 | ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len, |
1728 | flags); | |
a3171351 AG |
1729 | if (unlikely(ret)) |
1730 | return ret; | |
11cbfb10 | 1731 | |
29732938 | 1732 | ret = rw_verify_area(READ, file_in, &pos_in, len); |
bc61384d AV |
1733 | if (unlikely(ret)) |
1734 | return ret; | |
1735 | ||
1736 | ret = rw_verify_area(WRITE, file_out, &pos_out, len); | |
1737 | if (unlikely(ret)) | |
29732938 ZB |
1738 | return ret; |
1739 | ||
29732938 ZB |
1740 | if (len == 0) |
1741 | return 0; | |
1742 | ||
bfe219d3 | 1743 | file_start_write(file_out); |
29732938 | 1744 | |
a76b5b04 CH |
1745 | /* |
1746 | * Try cloning first, this is supported by more file systems, and | |
1747 | * more efficient if both clone and copy are supported (e.g. NFS). | |
1748 | */ | |
5dae222a AG |
1749 | if (file_in->f_op->remap_file_range && |
1750 | file_inode(file_in)->i_sb == file_inode(file_out)->i_sb) { | |
42ec3d4c DW |
1751 | loff_t cloned; |
1752 | ||
1753 | cloned = file_in->f_op->remap_file_range(file_in, pos_in, | |
1754 | file_out, pos_out, | |
eca3654e DW |
1755 | min_t(loff_t, MAX_RW_COUNT, len), |
1756 | REMAP_FILE_CAN_SHORTEN); | |
42ec3d4c DW |
1757 | if (cloned > 0) { |
1758 | ret = cloned; | |
a76b5b04 CH |
1759 | goto done; |
1760 | } | |
1761 | } | |
1762 | ||
64bf5ff5 DC |
1763 | ret = do_copy_file_range(file_in, pos_in, file_out, pos_out, len, |
1764 | flags); | |
1765 | WARN_ON_ONCE(ret == -EOPNOTSUPP); | |
a76b5b04 | 1766 | done: |
29732938 ZB |
1767 | if (ret > 0) { |
1768 | fsnotify_access(file_in); | |
1769 | add_rchar(current, ret); | |
1770 | fsnotify_modify(file_out); | |
1771 | add_wchar(current, ret); | |
1772 | } | |
a76b5b04 | 1773 | |
29732938 ZB |
1774 | inc_syscr(current); |
1775 | inc_syscw(current); | |
1776 | ||
bfe219d3 | 1777 | file_end_write(file_out); |
29732938 ZB |
1778 | |
1779 | return ret; | |
1780 | } | |
1781 | EXPORT_SYMBOL(vfs_copy_file_range); | |
1782 | ||
1783 | SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in, | |
1784 | int, fd_out, loff_t __user *, off_out, | |
1785 | size_t, len, unsigned int, flags) | |
1786 | { | |
1787 | loff_t pos_in; | |
1788 | loff_t pos_out; | |
1789 | struct fd f_in; | |
1790 | struct fd f_out; | |
1791 | ssize_t ret = -EBADF; | |
1792 | ||
1793 | f_in = fdget(fd_in); | |
1794 | if (!f_in.file) | |
1795 | goto out2; | |
1796 | ||
1797 | f_out = fdget(fd_out); | |
1798 | if (!f_out.file) | |
1799 | goto out1; | |
1800 | ||
1801 | ret = -EFAULT; | |
1802 | if (off_in) { | |
1803 | if (copy_from_user(&pos_in, off_in, sizeof(loff_t))) | |
1804 | goto out; | |
1805 | } else { | |
1806 | pos_in = f_in.file->f_pos; | |
1807 | } | |
1808 | ||
1809 | if (off_out) { | |
1810 | if (copy_from_user(&pos_out, off_out, sizeof(loff_t))) | |
1811 | goto out; | |
1812 | } else { | |
1813 | pos_out = f_out.file->f_pos; | |
1814 | } | |
1815 | ||
1816 | ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len, | |
1817 | flags); | |
1818 | if (ret > 0) { | |
1819 | pos_in += ret; | |
1820 | pos_out += ret; | |
1821 | ||
1822 | if (off_in) { | |
1823 | if (copy_to_user(off_in, &pos_in, sizeof(loff_t))) | |
1824 | ret = -EFAULT; | |
1825 | } else { | |
1826 | f_in.file->f_pos = pos_in; | |
1827 | } | |
1828 | ||
1829 | if (off_out) { | |
1830 | if (copy_to_user(off_out, &pos_out, sizeof(loff_t))) | |
1831 | ret = -EFAULT; | |
1832 | } else { | |
1833 | f_out.file->f_pos = pos_out; | |
1834 | } | |
1835 | } | |
1836 | ||
1837 | out: | |
1838 | fdput(f_out); | |
1839 | out1: | |
1840 | fdput(f_in); | |
1841 | out2: | |
1842 | return ret; | |
1843 | } | |
04b38d60 | 1844 | |
42ec3d4c DW |
1845 | static int remap_verify_area(struct file *file, loff_t pos, loff_t len, |
1846 | bool write) | |
04b38d60 CH |
1847 | { |
1848 | struct inode *inode = file_inode(file); | |
1849 | ||
42ec3d4c | 1850 | if (unlikely(pos < 0 || len < 0)) |
04b38d60 CH |
1851 | return -EINVAL; |
1852 | ||
1853 | if (unlikely((loff_t) (pos + len) < 0)) | |
1854 | return -EINVAL; | |
1855 | ||
1856 | if (unlikely(inode->i_flctx && mandatory_lock(inode))) { | |
1857 | loff_t end = len ? pos + len - 1 : OFFSET_MAX; | |
1858 | int retval; | |
1859 | ||
1860 | retval = locks_mandatory_area(inode, file, pos, end, | |
1861 | write ? F_WRLCK : F_RDLCK); | |
1862 | if (retval < 0) | |
1863 | return retval; | |
1864 | } | |
1865 | ||
1866 | return security_file_permission(file, write ? MAY_WRITE : MAY_READ); | |
1867 | } | |
07d19dc9 DW |
1868 | /* |
1869 | * Ensure that we don't remap a partial EOF block in the middle of something | |
1870 | * else. Assume that the offsets have already been checked for block | |
1871 | * alignment. | |
1872 | * | |
a5e6ea18 FM |
1873 | * For clone we only link a partial EOF block above or at the destination file's |
1874 | * EOF. For deduplication we accept a partial EOF block only if it ends at the | |
1875 | * destination file's EOF (can not link it into the middle of a file). | |
eca3654e DW |
1876 | * |
1877 | * Shorten the request if possible. | |
07d19dc9 DW |
1878 | */ |
1879 | static int generic_remap_check_len(struct inode *inode_in, | |
1880 | struct inode *inode_out, | |
1881 | loff_t pos_out, | |
42ec3d4c | 1882 | loff_t *len, |
a91ae49b | 1883 | unsigned int remap_flags) |
07d19dc9 DW |
1884 | { |
1885 | u64 blkmask = i_blocksize(inode_in) - 1; | |
eca3654e | 1886 | loff_t new_len = *len; |
07d19dc9 DW |
1887 | |
1888 | if ((*len & blkmask) == 0) | |
1889 | return 0; | |
1890 | ||
a5e6ea18 | 1891 | if (pos_out + *len < i_size_read(inode_out)) |
eca3654e | 1892 | new_len &= ~blkmask; |
07d19dc9 | 1893 | |
eca3654e DW |
1894 | if (new_len == *len) |
1895 | return 0; | |
1896 | ||
1897 | if (remap_flags & REMAP_FILE_CAN_SHORTEN) { | |
1898 | *len = new_len; | |
1899 | return 0; | |
1900 | } | |
1901 | ||
1902 | return (remap_flags & REMAP_FILE_DEDUP) ? -EBADE : -EINVAL; | |
07d19dc9 | 1903 | } |
04b38d60 | 1904 | |
edc58dd0 | 1905 | /* Read a page's worth of file data into the page cache. */ |
c32e5f39 DW |
1906 | static struct page *vfs_dedupe_get_page(struct inode *inode, loff_t offset) |
1907 | { | |
1908 | struct page *page; | |
1909 | ||
1910 | page = read_mapping_page(inode->i_mapping, offset >> PAGE_SHIFT, NULL); | |
1911 | if (IS_ERR(page)) | |
1912 | return page; | |
1913 | if (!PageUptodate(page)) { | |
1914 | put_page(page); | |
1915 | return ERR_PTR(-EIO); | |
1916 | } | |
c32e5f39 DW |
1917 | return page; |
1918 | } | |
1919 | ||
edc58dd0 DW |
1920 | /* |
1921 | * Lock two pages, ensuring that we lock in offset order if the pages are from | |
1922 | * the same file. | |
1923 | */ | |
1924 | static void vfs_lock_two_pages(struct page *page1, struct page *page2) | |
1925 | { | |
1926 | /* Always lock in order of increasing index. */ | |
1927 | if (page1->index > page2->index) | |
1928 | swap(page1, page2); | |
1929 | ||
1930 | lock_page(page1); | |
1931 | if (page1 != page2) | |
1932 | lock_page(page2); | |
1933 | } | |
1934 | ||
1935 | /* Unlock two pages, being careful not to unlock the same page twice. */ | |
1936 | static void vfs_unlock_two_pages(struct page *page1, struct page *page2) | |
1937 | { | |
1938 | unlock_page(page1); | |
1939 | if (page1 != page2) | |
1940 | unlock_page(page2); | |
1941 | } | |
1942 | ||
c32e5f39 DW |
1943 | /* |
1944 | * Compare extents of two files to see if they are the same. | |
1945 | * Caller must have locked both inodes to prevent write races. | |
1946 | */ | |
1947 | static int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff, | |
1948 | struct inode *dest, loff_t destoff, | |
1949 | loff_t len, bool *is_same) | |
1950 | { | |
1951 | loff_t src_poff; | |
1952 | loff_t dest_poff; | |
1953 | void *src_addr; | |
1954 | void *dest_addr; | |
1955 | struct page *src_page; | |
1956 | struct page *dest_page; | |
1957 | loff_t cmp_len; | |
1958 | bool same; | |
1959 | int error; | |
1960 | ||
1961 | error = -EINVAL; | |
1962 | same = true; | |
1963 | while (len) { | |
1964 | src_poff = srcoff & (PAGE_SIZE - 1); | |
1965 | dest_poff = destoff & (PAGE_SIZE - 1); | |
1966 | cmp_len = min(PAGE_SIZE - src_poff, | |
1967 | PAGE_SIZE - dest_poff); | |
1968 | cmp_len = min(cmp_len, len); | |
1969 | if (cmp_len <= 0) | |
1970 | goto out_error; | |
1971 | ||
1972 | src_page = vfs_dedupe_get_page(src, srcoff); | |
1973 | if (IS_ERR(src_page)) { | |
1974 | error = PTR_ERR(src_page); | |
1975 | goto out_error; | |
1976 | } | |
1977 | dest_page = vfs_dedupe_get_page(dest, destoff); | |
1978 | if (IS_ERR(dest_page)) { | |
1979 | error = PTR_ERR(dest_page); | |
c32e5f39 DW |
1980 | put_page(src_page); |
1981 | goto out_error; | |
1982 | } | |
edc58dd0 DW |
1983 | |
1984 | vfs_lock_two_pages(src_page, dest_page); | |
1985 | ||
1986 | /* | |
1987 | * Now that we've locked both pages, make sure they're still | |
1988 | * mapped to the file data we're interested in. If not, | |
1989 | * someone is invalidating pages on us and we lose. | |
1990 | */ | |
1991 | if (!PageUptodate(src_page) || !PageUptodate(dest_page) || | |
1992 | src_page->mapping != src->i_mapping || | |
1993 | dest_page->mapping != dest->i_mapping) { | |
1994 | same = false; | |
1995 | goto unlock; | |
1996 | } | |
1997 | ||
c32e5f39 DW |
1998 | src_addr = kmap_atomic(src_page); |
1999 | dest_addr = kmap_atomic(dest_page); | |
2000 | ||
2001 | flush_dcache_page(src_page); | |
2002 | flush_dcache_page(dest_page); | |
2003 | ||
2004 | if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len)) | |
2005 | same = false; | |
2006 | ||
2007 | kunmap_atomic(dest_addr); | |
2008 | kunmap_atomic(src_addr); | |
edc58dd0 DW |
2009 | unlock: |
2010 | vfs_unlock_two_pages(src_page, dest_page); | |
c32e5f39 DW |
2011 | put_page(dest_page); |
2012 | put_page(src_page); | |
2013 | ||
2014 | if (!same) | |
2015 | break; | |
2016 | ||
2017 | srcoff += cmp_len; | |
2018 | destoff += cmp_len; | |
2019 | len -= cmp_len; | |
2020 | } | |
2021 | ||
2022 | *is_same = same; | |
2023 | return 0; | |
2024 | ||
2025 | out_error: | |
2026 | return error; | |
2027 | } | |
04b38d60 | 2028 | |
876bec6f DW |
2029 | /* |
2030 | * Check that the two inodes are eligible for cloning, the ranges make | |
2031 | * sense, and then flush all dirty data. Caller must ensure that the | |
2032 | * inodes have been locked against any other modifications. | |
22725ce4 | 2033 | * |
8c5c836b DW |
2034 | * If there's an error, then the usual negative error code is returned. |
2035 | * Otherwise returns 0 with *len set to the request length. | |
876bec6f | 2036 | */ |
a83ab01a DW |
2037 | int generic_remap_file_range_prep(struct file *file_in, loff_t pos_in, |
2038 | struct file *file_out, loff_t pos_out, | |
42ec3d4c | 2039 | loff_t *len, unsigned int remap_flags) |
876bec6f | 2040 | { |
1383a7ed DW |
2041 | struct inode *inode_in = file_inode(file_in); |
2042 | struct inode *inode_out = file_inode(file_out); | |
876bec6f DW |
2043 | bool same_inode = (inode_in == inode_out); |
2044 | int ret; | |
2045 | ||
2046 | /* Don't touch certain kinds of inodes */ | |
2047 | if (IS_IMMUTABLE(inode_out)) | |
2048 | return -EPERM; | |
2049 | ||
2050 | if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out)) | |
2051 | return -ETXTBSY; | |
2052 | ||
2053 | /* Don't reflink dirs, pipes, sockets... */ | |
2054 | if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode)) | |
2055 | return -EISDIR; | |
2056 | if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode)) | |
2057 | return -EINVAL; | |
2058 | ||
876bec6f DW |
2059 | /* Zero length dedupe exits immediately; reflink goes to EOF. */ |
2060 | if (*len == 0) { | |
1383a7ed DW |
2061 | loff_t isize = i_size_read(inode_in); |
2062 | ||
a91ae49b | 2063 | if ((remap_flags & REMAP_FILE_DEDUP) || pos_in == isize) |
876bec6f | 2064 | return 0; |
22725ce4 DW |
2065 | if (pos_in > isize) |
2066 | return -EINVAL; | |
876bec6f | 2067 | *len = isize - pos_in; |
2c5773f1 DW |
2068 | if (*len == 0) |
2069 | return 0; | |
876bec6f DW |
2070 | } |
2071 | ||
1383a7ed DW |
2072 | /* Check that we don't violate system file offset limits. */ |
2073 | ret = generic_remap_checks(file_in, pos_in, file_out, pos_out, len, | |
3d28193e | 2074 | remap_flags); |
1383a7ed DW |
2075 | if (ret) |
2076 | return ret; | |
876bec6f DW |
2077 | |
2078 | /* Wait for the completion of any pending IOs on both files */ | |
2079 | inode_dio_wait(inode_in); | |
2080 | if (!same_inode) | |
2081 | inode_dio_wait(inode_out); | |
2082 | ||
2083 | ret = filemap_write_and_wait_range(inode_in->i_mapping, | |
2084 | pos_in, pos_in + *len - 1); | |
2085 | if (ret) | |
2086 | return ret; | |
2087 | ||
2088 | ret = filemap_write_and_wait_range(inode_out->i_mapping, | |
2089 | pos_out, pos_out + *len - 1); | |
2090 | if (ret) | |
2091 | return ret; | |
2092 | ||
2093 | /* | |
2094 | * Check that the extents are the same. | |
2095 | */ | |
a91ae49b | 2096 | if (remap_flags & REMAP_FILE_DEDUP) { |
876bec6f DW |
2097 | bool is_same = false; |
2098 | ||
2099 | ret = vfs_dedupe_file_range_compare(inode_in, pos_in, | |
2100 | inode_out, pos_out, *len, &is_same); | |
2101 | if (ret) | |
2102 | return ret; | |
2103 | if (!is_same) | |
2104 | return -EBADE; | |
2105 | } | |
2106 | ||
07d19dc9 | 2107 | ret = generic_remap_check_len(inode_in, inode_out, pos_out, len, |
a91ae49b | 2108 | remap_flags); |
07d19dc9 DW |
2109 | if (ret) |
2110 | return ret; | |
2111 | ||
8dde90bc | 2112 | /* If can't alter the file contents, we're done. */ |
e38f7f53 AG |
2113 | if (!(remap_flags & REMAP_FILE_DEDUP)) |
2114 | ret = file_modified(file_out); | |
8dde90bc | 2115 | |
e38f7f53 | 2116 | return ret; |
876bec6f | 2117 | } |
a83ab01a | 2118 | EXPORT_SYMBOL(generic_remap_file_range_prep); |
876bec6f | 2119 | |
42ec3d4c | 2120 | loff_t do_clone_file_range(struct file *file_in, loff_t pos_in, |
452ce659 DW |
2121 | struct file *file_out, loff_t pos_out, |
2122 | loff_t len, unsigned int remap_flags) | |
04b38d60 | 2123 | { |
42ec3d4c | 2124 | loff_t ret; |
04b38d60 | 2125 | |
6744557b | 2126 | WARN_ON_ONCE(remap_flags & REMAP_FILE_DEDUP); |
04b38d60 | 2127 | |
913b86e9 AG |
2128 | /* |
2129 | * FICLONE/FICLONERANGE ioctls enforce that src and dest files are on | |
2130 | * the same mount. Practically, they only need to be on the same file | |
2131 | * system. | |
2132 | */ | |
a3171351 | 2133 | if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb) |
04b38d60 CH |
2134 | return -EXDEV; |
2135 | ||
a3171351 AG |
2136 | ret = generic_file_rw_checks(file_in, file_out); |
2137 | if (ret < 0) | |
2138 | return ret; | |
04b38d60 | 2139 | |
2e5dfc99 | 2140 | if (!file_in->f_op->remap_file_range) |
0fcbf996 CH |
2141 | return -EOPNOTSUPP; |
2142 | ||
6095028b | 2143 | ret = remap_verify_area(file_in, pos_in, len, false); |
04b38d60 CH |
2144 | if (ret) |
2145 | return ret; | |
2146 | ||
6095028b | 2147 | ret = remap_verify_area(file_out, pos_out, len, true); |
04b38d60 CH |
2148 | if (ret) |
2149 | return ret; | |
2150 | ||
2e5dfc99 | 2151 | ret = file_in->f_op->remap_file_range(file_in, pos_in, |
452ce659 | 2152 | file_out, pos_out, len, remap_flags); |
42ec3d4c DW |
2153 | if (ret < 0) |
2154 | return ret; | |
04b38d60 | 2155 | |
42ec3d4c DW |
2156 | fsnotify_access(file_in); |
2157 | fsnotify_modify(file_out); | |
04b38d60 CH |
2158 | return ret; |
2159 | } | |
a725356b AG |
2160 | EXPORT_SYMBOL(do_clone_file_range); |
2161 | ||
42ec3d4c | 2162 | loff_t vfs_clone_file_range(struct file *file_in, loff_t pos_in, |
452ce659 DW |
2163 | struct file *file_out, loff_t pos_out, |
2164 | loff_t len, unsigned int remap_flags) | |
a725356b | 2165 | { |
42ec3d4c | 2166 | loff_t ret; |
a725356b AG |
2167 | |
2168 | file_start_write(file_out); | |
452ce659 DW |
2169 | ret = do_clone_file_range(file_in, pos_in, file_out, pos_out, len, |
2170 | remap_flags); | |
a725356b AG |
2171 | file_end_write(file_out); |
2172 | ||
2173 | return ret; | |
2174 | } | |
04b38d60 | 2175 | EXPORT_SYMBOL(vfs_clone_file_range); |
54dbc151 | 2176 | |
5de4480a MF |
2177 | /* Check whether we are allowed to dedupe the destination file */ |
2178 | static bool allow_file_dedupe(struct file *file) | |
2179 | { | |
2180 | if (capable(CAP_SYS_ADMIN)) | |
2181 | return true; | |
2182 | if (file->f_mode & FMODE_WRITE) | |
2183 | return true; | |
2184 | if (uid_eq(current_fsuid(), file_inode(file)->i_uid)) | |
2185 | return true; | |
2186 | if (!inode_permission(file_inode(file), MAY_WRITE)) | |
2187 | return true; | |
2188 | return false; | |
2189 | } | |
2190 | ||
42ec3d4c DW |
2191 | loff_t vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos, |
2192 | struct file *dst_file, loff_t dst_pos, | |
df365836 | 2193 | loff_t len, unsigned int remap_flags) |
1b4f42a1 | 2194 | { |
42ec3d4c | 2195 | loff_t ret; |
1b4f42a1 | 2196 | |
eca3654e DW |
2197 | WARN_ON_ONCE(remap_flags & ~(REMAP_FILE_DEDUP | |
2198 | REMAP_FILE_CAN_SHORTEN)); | |
1b4f42a1 MS |
2199 | |
2200 | ret = mnt_want_write_file(dst_file); | |
2201 | if (ret) | |
2202 | return ret; | |
2203 | ||
6095028b | 2204 | ret = remap_verify_area(dst_file, dst_pos, len, true); |
1b4f42a1 MS |
2205 | if (ret < 0) |
2206 | goto out_drop_write; | |
2207 | ||
85c95f20 | 2208 | ret = -EPERM; |
5de4480a | 2209 | if (!allow_file_dedupe(dst_file)) |
1b4f42a1 MS |
2210 | goto out_drop_write; |
2211 | ||
2212 | ret = -EXDEV; | |
2213 | if (src_file->f_path.mnt != dst_file->f_path.mnt) | |
2214 | goto out_drop_write; | |
2215 | ||
2216 | ret = -EISDIR; | |
2217 | if (S_ISDIR(file_inode(dst_file)->i_mode)) | |
2218 | goto out_drop_write; | |
2219 | ||
2220 | ret = -EINVAL; | |
2e5dfc99 | 2221 | if (!dst_file->f_op->remap_file_range) |
1b4f42a1 MS |
2222 | goto out_drop_write; |
2223 | ||
9aae2050 DW |
2224 | if (len == 0) { |
2225 | ret = 0; | |
2226 | goto out_drop_write; | |
2227 | } | |
2228 | ||
2e5dfc99 | 2229 | ret = dst_file->f_op->remap_file_range(src_file, src_pos, dst_file, |
df365836 | 2230 | dst_pos, len, remap_flags | REMAP_FILE_DEDUP); |
1b4f42a1 MS |
2231 | out_drop_write: |
2232 | mnt_drop_write_file(dst_file); | |
2233 | ||
2234 | return ret; | |
2235 | } | |
f1825366 | 2236 | EXPORT_SYMBOL(vfs_dedupe_file_range_one); |
1b4f42a1 | 2237 | |
54dbc151 DW |
2238 | int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same) |
2239 | { | |
2240 | struct file_dedupe_range_info *info; | |
2241 | struct inode *src = file_inode(file); | |
2242 | u64 off; | |
2243 | u64 len; | |
2244 | int i; | |
2245 | int ret; | |
54dbc151 | 2246 | u16 count = same->dest_count; |
42ec3d4c | 2247 | loff_t deduped; |
54dbc151 DW |
2248 | |
2249 | if (!(file->f_mode & FMODE_READ)) | |
2250 | return -EINVAL; | |
2251 | ||
2252 | if (same->reserved1 || same->reserved2) | |
2253 | return -EINVAL; | |
2254 | ||
2255 | off = same->src_offset; | |
2256 | len = same->src_length; | |
2257 | ||
54dbc151 | 2258 | if (S_ISDIR(src->i_mode)) |
494633fa | 2259 | return -EISDIR; |
54dbc151 | 2260 | |
54dbc151 | 2261 | if (!S_ISREG(src->i_mode)) |
494633fa DC |
2262 | return -EINVAL; |
2263 | ||
2264 | if (!file->f_op->remap_file_range) | |
2265 | return -EOPNOTSUPP; | |
54dbc151 | 2266 | |
6095028b | 2267 | ret = remap_verify_area(file, off, len, false); |
54dbc151 | 2268 | if (ret < 0) |
494633fa | 2269 | return ret; |
54dbc151 DW |
2270 | ret = 0; |
2271 | ||
22725ce4 DW |
2272 | if (off + len > i_size_read(src)) |
2273 | return -EINVAL; | |
2274 | ||
92b66d2c MS |
2275 | /* Arbitrary 1G limit on a single dedupe request, can be raised. */ |
2276 | len = min_t(u64, len, 1 << 30); | |
2277 | ||
54dbc151 DW |
2278 | /* pre-format output fields to sane values */ |
2279 | for (i = 0; i < count; i++) { | |
2280 | same->info[i].bytes_deduped = 0ULL; | |
2281 | same->info[i].status = FILE_DEDUPE_RANGE_SAME; | |
2282 | } | |
2283 | ||
2284 | for (i = 0, info = same->info; i < count; i++, info++) { | |
54dbc151 | 2285 | struct fd dst_fd = fdget(info->dest_fd); |
1b4f42a1 | 2286 | struct file *dst_file = dst_fd.file; |
54dbc151 | 2287 | |
54dbc151 DW |
2288 | if (!dst_file) { |
2289 | info->status = -EBADF; | |
2290 | goto next_loop; | |
2291 | } | |
54dbc151 DW |
2292 | |
2293 | if (info->reserved) { | |
2294 | info->status = -EINVAL; | |
1b4f42a1 | 2295 | goto next_fdput; |
54dbc151 DW |
2296 | } |
2297 | ||
1b4f42a1 | 2298 | deduped = vfs_dedupe_file_range_one(file, off, dst_file, |
df365836 | 2299 | info->dest_offset, len, |
eca3654e | 2300 | REMAP_FILE_CAN_SHORTEN); |
1b4f42a1 MS |
2301 | if (deduped == -EBADE) |
2302 | info->status = FILE_DEDUPE_RANGE_DIFFERS; | |
2303 | else if (deduped < 0) | |
2304 | info->status = deduped; | |
2305 | else | |
2306 | info->bytes_deduped = len; | |
2307 | ||
22762711 | 2308 | next_fdput: |
54dbc151 | 2309 | fdput(dst_fd); |
22762711 | 2310 | next_loop: |
e62e560f | 2311 | if (fatal_signal_pending(current)) |
494633fa | 2312 | break; |
54dbc151 | 2313 | } |
54dbc151 DW |
2314 | return ret; |
2315 | } | |
2316 | EXPORT_SYMBOL(vfs_dedupe_file_range); |