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