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