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