Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/read_write.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | */ | |
6 | ||
7 | #include <linux/slab.h> | |
8 | #include <linux/stat.h> | |
9 | #include <linux/fcntl.h> | |
10 | #include <linux/file.h> | |
11 | #include <linux/uio.h> | |
0eeca283 | 12 | #include <linux/fsnotify.h> |
1da177e4 LT |
13 | #include <linux/security.h> |
14 | #include <linux/module.h> | |
15 | #include <linux/syscalls.h> | |
e28cc715 | 16 | #include <linux/pagemap.h> |
d6b29d7c | 17 | #include <linux/splice.h> |
ee0b3e67 | 18 | #include "read_write.h" |
1da177e4 LT |
19 | |
20 | #include <asm/uaccess.h> | |
21 | #include <asm/unistd.h> | |
22 | ||
4b6f5d20 | 23 | const struct file_operations generic_ro_fops = { |
1da177e4 | 24 | .llseek = generic_file_llseek, |
543ade1f BP |
25 | .read = do_sync_read, |
26 | .aio_read = generic_file_aio_read, | |
1da177e4 | 27 | .mmap = generic_file_readonly_mmap, |
534f2aaa | 28 | .splice_read = generic_file_splice_read, |
1da177e4 LT |
29 | }; |
30 | ||
31 | EXPORT_SYMBOL(generic_ro_fops); | |
32 | ||
cccb5a1e | 33 | static inline int unsigned_offsets(struct file *file) |
4a3956c7 | 34 | { |
cccb5a1e | 35 | return file->f_mode & FMODE_UNSIGNED_OFFSET; |
4a3956c7 KH |
36 | } |
37 | ||
ef3d0fd2 AK |
38 | static loff_t lseek_execute(struct file *file, struct inode *inode, |
39 | loff_t offset, loff_t maxsize) | |
40 | { | |
41 | if (offset < 0 && !unsigned_offsets(file)) | |
42 | return -EINVAL; | |
43 | if (offset > maxsize) | |
44 | return -EINVAL; | |
45 | ||
46 | if (offset != file->f_pos) { | |
47 | file->f_pos = offset; | |
48 | file->f_version = 0; | |
49 | } | |
50 | return offset; | |
51 | } | |
52 | ||
3a8cff4f | 53 | /** |
ef3d0fd2 | 54 | * generic_file_llseek - generic llseek implementation for regular files |
3a8cff4f CH |
55 | * @file: file structure to seek on |
56 | * @offset: file offset to seek to | |
57 | * @origin: type of seek | |
58 | * | |
ef3d0fd2 AK |
59 | * This is a generic implemenation of ->llseek usable for all normal local |
60 | * filesystems. It just updates the file offset to the value specified by | |
61 | * @offset and @origin under i_mutex. | |
62 | * | |
63 | * Synchronization: | |
64 | * SEEK_SET is unsynchronized (but atomic on 64bit platforms) | |
65 | * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes. | |
66 | * read/writes behave like SEEK_SET against seeks. | |
67 | * SEEK_END | |
3a8cff4f | 68 | */ |
9465efc9 | 69 | loff_t |
ef3d0fd2 | 70 | generic_file_llseek(struct file *file, loff_t offset, int origin) |
1da177e4 | 71 | { |
1da177e4 LT |
72 | struct inode *inode = file->f_mapping->host; |
73 | ||
1da177e4 | 74 | switch (origin) { |
3a8cff4f | 75 | case SEEK_END: |
ef3d0fd2 | 76 | offset += i_size_read(inode); |
3a8cff4f CH |
77 | break; |
78 | case SEEK_CUR: | |
5b6f1eb9 AK |
79 | /* |
80 | * Here we special-case the lseek(fd, 0, SEEK_CUR) | |
81 | * position-querying operation. Avoid rewriting the "same" | |
82 | * f_pos value back to the file because a concurrent read(), | |
83 | * write() or lseek() might have altered it | |
84 | */ | |
85 | if (offset == 0) | |
86 | return file->f_pos; | |
ef3d0fd2 AK |
87 | /* |
88 | * f_lock protects against read/modify/write race with other | |
89 | * SEEK_CURs. Note that parallel writes and reads behave | |
90 | * like SEEK_SET. | |
91 | */ | |
92 | spin_lock(&file->f_lock); | |
93 | offset = lseek_execute(file, inode, file->f_pos + offset, | |
94 | inode->i_sb->s_maxbytes); | |
95 | spin_unlock(&file->f_lock); | |
96 | return offset; | |
982d8165 JB |
97 | case SEEK_DATA: |
98 | /* | |
99 | * In the generic case the entire file is data, so as long as | |
100 | * offset isn't at the end of the file then the offset is data. | |
101 | */ | |
ef3d0fd2 | 102 | if (offset >= i_size_read(inode)) |
982d8165 JB |
103 | return -ENXIO; |
104 | break; | |
105 | case SEEK_HOLE: | |
106 | /* | |
107 | * There is a virtual hole at the end of the file, so as long as | |
108 | * offset isn't i_size or larger, return i_size. | |
109 | */ | |
ef3d0fd2 | 110 | if (offset >= i_size_read(inode)) |
982d8165 | 111 | return -ENXIO; |
ef3d0fd2 | 112 | offset = i_size_read(inode); |
982d8165 | 113 | break; |
1da177e4 | 114 | } |
3a8cff4f | 115 | |
ef3d0fd2 | 116 | return lseek_execute(file, inode, offset, inode->i_sb->s_maxbytes); |
1da177e4 | 117 | } |
9465efc9 | 118 | EXPORT_SYMBOL(generic_file_llseek); |
1da177e4 | 119 | |
ae6afc3f B |
120 | /** |
121 | * noop_llseek - No Operation Performed llseek implementation | |
122 | * @file: file structure to seek on | |
123 | * @offset: file offset to seek to | |
124 | * @origin: type of seek | |
125 | * | |
126 | * This is an implementation of ->llseek useable for the rare special case when | |
127 | * userspace expects the seek to succeed but the (device) file is actually not | |
128 | * able to perform the seek. In this case you use noop_llseek() instead of | |
129 | * falling back to the default implementation of ->llseek. | |
130 | */ | |
131 | loff_t noop_llseek(struct file *file, loff_t offset, int origin) | |
132 | { | |
133 | return file->f_pos; | |
134 | } | |
135 | EXPORT_SYMBOL(noop_llseek); | |
136 | ||
1da177e4 LT |
137 | loff_t no_llseek(struct file *file, loff_t offset, int origin) |
138 | { | |
139 | return -ESPIPE; | |
140 | } | |
141 | EXPORT_SYMBOL(no_llseek); | |
142 | ||
143 | loff_t default_llseek(struct file *file, loff_t offset, int origin) | |
144 | { | |
982d8165 | 145 | struct inode *inode = file->f_path.dentry->d_inode; |
16abef0e | 146 | loff_t retval; |
1da177e4 | 147 | |
982d8165 | 148 | mutex_lock(&inode->i_mutex); |
1da177e4 | 149 | switch (origin) { |
7b8e8924 | 150 | case SEEK_END: |
982d8165 | 151 | offset += i_size_read(inode); |
1da177e4 | 152 | break; |
7b8e8924 | 153 | case SEEK_CUR: |
5b6f1eb9 AK |
154 | if (offset == 0) { |
155 | retval = file->f_pos; | |
156 | goto out; | |
157 | } | |
1da177e4 | 158 | offset += file->f_pos; |
982d8165 JB |
159 | break; |
160 | case SEEK_DATA: | |
161 | /* | |
162 | * In the generic case the entire file is data, so as | |
163 | * long as offset isn't at the end of the file then the | |
164 | * offset is data. | |
165 | */ | |
bacb2d81 DC |
166 | if (offset >= inode->i_size) { |
167 | retval = -ENXIO; | |
168 | goto out; | |
169 | } | |
982d8165 JB |
170 | break; |
171 | case SEEK_HOLE: | |
172 | /* | |
173 | * There is a virtual hole at the end of the file, so | |
174 | * as long as offset isn't i_size or larger, return | |
175 | * i_size. | |
176 | */ | |
bacb2d81 DC |
177 | if (offset >= inode->i_size) { |
178 | retval = -ENXIO; | |
179 | goto out; | |
180 | } | |
982d8165 JB |
181 | offset = inode->i_size; |
182 | break; | |
1da177e4 LT |
183 | } |
184 | retval = -EINVAL; | |
cccb5a1e | 185 | if (offset >= 0 || unsigned_offsets(file)) { |
1da177e4 LT |
186 | if (offset != file->f_pos) { |
187 | file->f_pos = offset; | |
188 | file->f_version = 0; | |
189 | } | |
190 | retval = offset; | |
191 | } | |
5b6f1eb9 | 192 | out: |
982d8165 | 193 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
194 | return retval; |
195 | } | |
196 | EXPORT_SYMBOL(default_llseek); | |
197 | ||
198 | loff_t vfs_llseek(struct file *file, loff_t offset, int origin) | |
199 | { | |
200 | loff_t (*fn)(struct file *, loff_t, int); | |
201 | ||
202 | fn = no_llseek; | |
203 | if (file->f_mode & FMODE_LSEEK) { | |
1da177e4 LT |
204 | if (file->f_op && file->f_op->llseek) |
205 | fn = file->f_op->llseek; | |
206 | } | |
207 | return fn(file, offset, origin); | |
208 | } | |
209 | EXPORT_SYMBOL(vfs_llseek); | |
210 | ||
003d7ab4 | 211 | SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin) |
1da177e4 LT |
212 | { |
213 | off_t retval; | |
214 | struct file * file; | |
215 | int fput_needed; | |
216 | ||
217 | retval = -EBADF; | |
218 | file = fget_light(fd, &fput_needed); | |
219 | if (!file) | |
220 | goto bad; | |
221 | ||
222 | retval = -EINVAL; | |
1ae7075b | 223 | if (origin <= SEEK_MAX) { |
1da177e4 LT |
224 | loff_t res = vfs_llseek(file, offset, origin); |
225 | retval = res; | |
226 | if (res != (loff_t)retval) | |
227 | retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */ | |
228 | } | |
229 | fput_light(file, fput_needed); | |
230 | bad: | |
231 | return retval; | |
232 | } | |
233 | ||
234 | #ifdef __ARCH_WANT_SYS_LLSEEK | |
003d7ab4 HC |
235 | SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high, |
236 | unsigned long, offset_low, loff_t __user *, result, | |
237 | unsigned int, origin) | |
1da177e4 LT |
238 | { |
239 | int retval; | |
240 | struct file * file; | |
241 | loff_t offset; | |
242 | int fput_needed; | |
243 | ||
244 | retval = -EBADF; | |
245 | file = fget_light(fd, &fput_needed); | |
246 | if (!file) | |
247 | goto bad; | |
248 | ||
249 | retval = -EINVAL; | |
1ae7075b | 250 | if (origin > SEEK_MAX) |
1da177e4 LT |
251 | goto out_putf; |
252 | ||
253 | offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low, | |
254 | origin); | |
255 | ||
256 | retval = (int)offset; | |
257 | if (offset >= 0) { | |
258 | retval = -EFAULT; | |
259 | if (!copy_to_user(result, &offset, sizeof(offset))) | |
260 | retval = 0; | |
261 | } | |
262 | out_putf: | |
263 | fput_light(file, fput_needed); | |
264 | bad: | |
265 | return retval; | |
266 | } | |
267 | #endif | |
268 | ||
4a3956c7 | 269 | |
e28cc715 LT |
270 | /* |
271 | * rw_verify_area doesn't like huge counts. We limit | |
272 | * them to something that fits in "int" so that others | |
273 | * won't have to do range checks all the time. | |
274 | */ | |
1da177e4 LT |
275 | int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count) |
276 | { | |
277 | struct inode *inode; | |
278 | loff_t pos; | |
c43e259c | 279 | int retval = -EINVAL; |
1da177e4 | 280 | |
163da958 | 281 | inode = file->f_path.dentry->d_inode; |
e28cc715 | 282 | if (unlikely((ssize_t) count < 0)) |
c43e259c | 283 | return retval; |
1da177e4 | 284 | pos = *ppos; |
cccb5a1e AV |
285 | if (unlikely(pos < 0)) { |
286 | if (!unsigned_offsets(file)) | |
287 | return retval; | |
288 | if (count >= -pos) /* both values are in 0..LLONG_MAX */ | |
289 | return -EOVERFLOW; | |
290 | } else if (unlikely((loff_t) (pos + count) < 0)) { | |
291 | if (!unsigned_offsets(file)) | |
4a3956c7 KH |
292 | return retval; |
293 | } | |
1da177e4 | 294 | |
a16877ca | 295 | if (unlikely(inode->i_flock && mandatory_lock(inode))) { |
c43e259c | 296 | retval = locks_mandatory_area( |
e28cc715 LT |
297 | read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE, |
298 | inode, file, pos, count); | |
299 | if (retval < 0) | |
300 | return retval; | |
301 | } | |
c43e259c JM |
302 | retval = security_file_permission(file, |
303 | read_write == READ ? MAY_READ : MAY_WRITE); | |
304 | if (retval) | |
305 | return retval; | |
e28cc715 | 306 | return count > MAX_RW_COUNT ? MAX_RW_COUNT : count; |
1da177e4 LT |
307 | } |
308 | ||
63e68809 BL |
309 | static void wait_on_retry_sync_kiocb(struct kiocb *iocb) |
310 | { | |
311 | set_current_state(TASK_UNINTERRUPTIBLE); | |
312 | if (!kiocbIsKicked(iocb)) | |
313 | schedule(); | |
314 | else | |
315 | kiocbClearKicked(iocb); | |
316 | __set_current_state(TASK_RUNNING); | |
317 | } | |
318 | ||
1da177e4 LT |
319 | ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos) |
320 | { | |
027445c3 | 321 | struct iovec iov = { .iov_base = buf, .iov_len = len }; |
1da177e4 LT |
322 | struct kiocb kiocb; |
323 | ssize_t ret; | |
324 | ||
325 | init_sync_kiocb(&kiocb, filp); | |
326 | kiocb.ki_pos = *ppos; | |
027445c3 | 327 | kiocb.ki_left = len; |
61964eba | 328 | kiocb.ki_nbytes = len; |
027445c3 BP |
329 | |
330 | for (;;) { | |
331 | ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos); | |
332 | if (ret != -EIOCBRETRY) | |
333 | break; | |
63e68809 | 334 | wait_on_retry_sync_kiocb(&kiocb); |
027445c3 | 335 | } |
63e68809 | 336 | |
1da177e4 LT |
337 | if (-EIOCBQUEUED == ret) |
338 | ret = wait_on_sync_kiocb(&kiocb); | |
339 | *ppos = kiocb.ki_pos; | |
340 | return ret; | |
341 | } | |
342 | ||
343 | EXPORT_SYMBOL(do_sync_read); | |
344 | ||
345 | ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) | |
346 | { | |
347 | ssize_t ret; | |
348 | ||
349 | if (!(file->f_mode & FMODE_READ)) | |
350 | return -EBADF; | |
351 | if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read)) | |
352 | return -EINVAL; | |
353 | if (unlikely(!access_ok(VERIFY_WRITE, buf, count))) | |
354 | return -EFAULT; | |
355 | ||
356 | ret = rw_verify_area(READ, file, pos, count); | |
e28cc715 LT |
357 | if (ret >= 0) { |
358 | count = ret; | |
c43e259c JM |
359 | if (file->f_op->read) |
360 | ret = file->f_op->read(file, buf, count, pos); | |
361 | else | |
362 | ret = do_sync_read(file, buf, count, pos); | |
363 | if (ret > 0) { | |
2a12a9d7 | 364 | fsnotify_access(file); |
c43e259c | 365 | add_rchar(current, ret); |
1da177e4 | 366 | } |
c43e259c | 367 | inc_syscr(current); |
1da177e4 LT |
368 | } |
369 | ||
370 | return ret; | |
371 | } | |
372 | ||
373 | EXPORT_SYMBOL(vfs_read); | |
374 | ||
375 | ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos) | |
376 | { | |
027445c3 | 377 | struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len }; |
1da177e4 LT |
378 | struct kiocb kiocb; |
379 | ssize_t ret; | |
380 | ||
381 | init_sync_kiocb(&kiocb, filp); | |
382 | kiocb.ki_pos = *ppos; | |
027445c3 | 383 | kiocb.ki_left = len; |
61964eba | 384 | kiocb.ki_nbytes = len; |
027445c3 BP |
385 | |
386 | for (;;) { | |
387 | ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos); | |
388 | if (ret != -EIOCBRETRY) | |
389 | break; | |
63e68809 | 390 | wait_on_retry_sync_kiocb(&kiocb); |
027445c3 | 391 | } |
63e68809 | 392 | |
1da177e4 LT |
393 | if (-EIOCBQUEUED == ret) |
394 | ret = wait_on_sync_kiocb(&kiocb); | |
395 | *ppos = kiocb.ki_pos; | |
396 | return ret; | |
397 | } | |
398 | ||
399 | EXPORT_SYMBOL(do_sync_write); | |
400 | ||
401 | ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) | |
402 | { | |
403 | ssize_t ret; | |
404 | ||
405 | if (!(file->f_mode & FMODE_WRITE)) | |
406 | return -EBADF; | |
407 | if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write)) | |
408 | return -EINVAL; | |
409 | if (unlikely(!access_ok(VERIFY_READ, buf, count))) | |
410 | return -EFAULT; | |
411 | ||
412 | ret = rw_verify_area(WRITE, file, pos, count); | |
e28cc715 LT |
413 | if (ret >= 0) { |
414 | count = ret; | |
c43e259c JM |
415 | if (file->f_op->write) |
416 | ret = file->f_op->write(file, buf, count, pos); | |
417 | else | |
418 | ret = do_sync_write(file, buf, count, pos); | |
419 | if (ret > 0) { | |
2a12a9d7 | 420 | fsnotify_modify(file); |
c43e259c | 421 | add_wchar(current, ret); |
1da177e4 | 422 | } |
c43e259c | 423 | inc_syscw(current); |
1da177e4 LT |
424 | } |
425 | ||
426 | return ret; | |
427 | } | |
428 | ||
429 | EXPORT_SYMBOL(vfs_write); | |
430 | ||
431 | static inline loff_t file_pos_read(struct file *file) | |
432 | { | |
433 | return file->f_pos; | |
434 | } | |
435 | ||
436 | static inline void file_pos_write(struct file *file, loff_t pos) | |
437 | { | |
438 | file->f_pos = pos; | |
439 | } | |
440 | ||
3cdad428 | 441 | SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count) |
1da177e4 LT |
442 | { |
443 | struct file *file; | |
444 | ssize_t ret = -EBADF; | |
445 | int fput_needed; | |
446 | ||
447 | file = fget_light(fd, &fput_needed); | |
448 | if (file) { | |
449 | loff_t pos = file_pos_read(file); | |
450 | ret = vfs_read(file, buf, count, &pos); | |
451 | file_pos_write(file, pos); | |
452 | fput_light(file, fput_needed); | |
453 | } | |
454 | ||
455 | return ret; | |
456 | } | |
1da177e4 | 457 | |
3cdad428 HC |
458 | SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, |
459 | size_t, count) | |
1da177e4 LT |
460 | { |
461 | struct file *file; | |
462 | ssize_t ret = -EBADF; | |
463 | int fput_needed; | |
464 | ||
465 | file = fget_light(fd, &fput_needed); | |
466 | if (file) { | |
467 | loff_t pos = file_pos_read(file); | |
468 | ret = vfs_write(file, buf, count, &pos); | |
469 | file_pos_write(file, pos); | |
470 | fput_light(file, fput_needed); | |
471 | } | |
472 | ||
473 | return ret; | |
474 | } | |
475 | ||
6673e0c3 HC |
476 | SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf, |
477 | size_t count, loff_t pos) | |
1da177e4 LT |
478 | { |
479 | struct file *file; | |
480 | ssize_t ret = -EBADF; | |
481 | int fput_needed; | |
482 | ||
483 | if (pos < 0) | |
484 | return -EINVAL; | |
485 | ||
486 | file = fget_light(fd, &fput_needed); | |
487 | if (file) { | |
488 | ret = -ESPIPE; | |
489 | if (file->f_mode & FMODE_PREAD) | |
490 | ret = vfs_read(file, buf, count, &pos); | |
491 | fput_light(file, fput_needed); | |
492 | } | |
493 | ||
494 | return ret; | |
495 | } | |
6673e0c3 HC |
496 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
497 | asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos) | |
498 | { | |
499 | return SYSC_pread64((unsigned int) fd, (char __user *) buf, | |
500 | (size_t) count, pos); | |
501 | } | |
502 | SYSCALL_ALIAS(sys_pread64, SyS_pread64); | |
503 | #endif | |
1da177e4 | 504 | |
6673e0c3 HC |
505 | SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf, |
506 | size_t count, loff_t pos) | |
1da177e4 LT |
507 | { |
508 | struct file *file; | |
509 | ssize_t ret = -EBADF; | |
510 | int fput_needed; | |
511 | ||
512 | if (pos < 0) | |
513 | return -EINVAL; | |
514 | ||
515 | file = fget_light(fd, &fput_needed); | |
516 | if (file) { | |
517 | ret = -ESPIPE; | |
518 | if (file->f_mode & FMODE_PWRITE) | |
519 | ret = vfs_write(file, buf, count, &pos); | |
520 | fput_light(file, fput_needed); | |
521 | } | |
522 | ||
523 | return ret; | |
524 | } | |
6673e0c3 HC |
525 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
526 | asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos) | |
527 | { | |
528 | return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf, | |
529 | (size_t) count, pos); | |
530 | } | |
531 | SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64); | |
532 | #endif | |
1da177e4 LT |
533 | |
534 | /* | |
535 | * Reduce an iovec's length in-place. Return the resulting number of segments | |
536 | */ | |
537 | unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to) | |
538 | { | |
539 | unsigned long seg = 0; | |
540 | size_t len = 0; | |
541 | ||
542 | while (seg < nr_segs) { | |
543 | seg++; | |
544 | if (len + iov->iov_len >= to) { | |
545 | iov->iov_len = to - len; | |
546 | break; | |
547 | } | |
548 | len += iov->iov_len; | |
549 | iov++; | |
550 | } | |
551 | return seg; | |
552 | } | |
19295529 | 553 | EXPORT_SYMBOL(iov_shorten); |
1da177e4 | 554 | |
ee0b3e67 BP |
555 | ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov, |
556 | unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn) | |
557 | { | |
558 | struct kiocb kiocb; | |
559 | ssize_t ret; | |
560 | ||
561 | init_sync_kiocb(&kiocb, filp); | |
562 | kiocb.ki_pos = *ppos; | |
563 | kiocb.ki_left = len; | |
564 | kiocb.ki_nbytes = len; | |
565 | ||
566 | for (;;) { | |
567 | ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos); | |
568 | if (ret != -EIOCBRETRY) | |
569 | break; | |
570 | wait_on_retry_sync_kiocb(&kiocb); | |
571 | } | |
572 | ||
573 | if (ret == -EIOCBQUEUED) | |
574 | ret = wait_on_sync_kiocb(&kiocb); | |
575 | *ppos = kiocb.ki_pos; | |
576 | return ret; | |
577 | } | |
578 | ||
579 | /* Do it by hand, with file-ops */ | |
580 | ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov, | |
581 | unsigned long nr_segs, loff_t *ppos, io_fn_t fn) | |
582 | { | |
583 | struct iovec *vector = iov; | |
584 | ssize_t ret = 0; | |
585 | ||
586 | while (nr_segs > 0) { | |
587 | void __user *base; | |
588 | size_t len; | |
589 | ssize_t nr; | |
590 | ||
591 | base = vector->iov_base; | |
592 | len = vector->iov_len; | |
593 | vector++; | |
594 | nr_segs--; | |
595 | ||
596 | nr = fn(filp, base, len, ppos); | |
597 | ||
598 | if (nr < 0) { | |
599 | if (!ret) | |
600 | ret = nr; | |
601 | break; | |
602 | } | |
603 | ret += nr; | |
604 | if (nr != len) | |
605 | break; | |
606 | } | |
607 | ||
608 | return ret; | |
609 | } | |
610 | ||
1da177e4 LT |
611 | /* A write operation does a read from user space and vice versa */ |
612 | #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ) | |
613 | ||
eed4e51f BP |
614 | ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector, |
615 | unsigned long nr_segs, unsigned long fast_segs, | |
616 | struct iovec *fast_pointer, | |
617 | struct iovec **ret_pointer) | |
435f49a5 | 618 | { |
eed4e51f | 619 | unsigned long seg; |
435f49a5 | 620 | ssize_t ret; |
eed4e51f BP |
621 | struct iovec *iov = fast_pointer; |
622 | ||
435f49a5 LT |
623 | /* |
624 | * SuS says "The readv() function *may* fail if the iovcnt argument | |
625 | * was less than or equal to 0, or greater than {IOV_MAX}. Linux has | |
626 | * traditionally returned zero for zero segments, so... | |
627 | */ | |
eed4e51f BP |
628 | if (nr_segs == 0) { |
629 | ret = 0; | |
435f49a5 | 630 | goto out; |
eed4e51f BP |
631 | } |
632 | ||
435f49a5 LT |
633 | /* |
634 | * First get the "struct iovec" from user memory and | |
635 | * verify all the pointers | |
636 | */ | |
eed4e51f BP |
637 | if (nr_segs > UIO_MAXIOV) { |
638 | ret = -EINVAL; | |
435f49a5 | 639 | goto out; |
eed4e51f BP |
640 | } |
641 | if (nr_segs > fast_segs) { | |
435f49a5 | 642 | iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL); |
eed4e51f BP |
643 | if (iov == NULL) { |
644 | ret = -ENOMEM; | |
435f49a5 | 645 | goto out; |
eed4e51f | 646 | } |
435f49a5 | 647 | } |
eed4e51f BP |
648 | if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) { |
649 | ret = -EFAULT; | |
435f49a5 | 650 | goto out; |
eed4e51f BP |
651 | } |
652 | ||
435f49a5 | 653 | /* |
eed4e51f BP |
654 | * According to the Single Unix Specification we should return EINVAL |
655 | * if an element length is < 0 when cast to ssize_t or if the | |
656 | * total length would overflow the ssize_t return value of the | |
657 | * system call. | |
435f49a5 LT |
658 | * |
659 | * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the | |
660 | * overflow case. | |
661 | */ | |
eed4e51f | 662 | ret = 0; |
435f49a5 LT |
663 | for (seg = 0; seg < nr_segs; seg++) { |
664 | void __user *buf = iov[seg].iov_base; | |
665 | ssize_t len = (ssize_t)iov[seg].iov_len; | |
eed4e51f BP |
666 | |
667 | /* see if we we're about to use an invalid len or if | |
668 | * it's about to overflow ssize_t */ | |
435f49a5 | 669 | if (len < 0) { |
eed4e51f | 670 | ret = -EINVAL; |
435f49a5 | 671 | goto out; |
eed4e51f BP |
672 | } |
673 | if (unlikely(!access_ok(vrfy_dir(type), buf, len))) { | |
674 | ret = -EFAULT; | |
435f49a5 LT |
675 | goto out; |
676 | } | |
677 | if (len > MAX_RW_COUNT - ret) { | |
678 | len = MAX_RW_COUNT - ret; | |
679 | iov[seg].iov_len = len; | |
eed4e51f | 680 | } |
eed4e51f | 681 | ret += len; |
435f49a5 | 682 | } |
eed4e51f BP |
683 | out: |
684 | *ret_pointer = iov; | |
685 | return ret; | |
686 | } | |
687 | ||
1da177e4 LT |
688 | static ssize_t do_readv_writev(int type, struct file *file, |
689 | const struct iovec __user * uvector, | |
690 | unsigned long nr_segs, loff_t *pos) | |
691 | { | |
1da177e4 LT |
692 | size_t tot_len; |
693 | struct iovec iovstack[UIO_FASTIOV]; | |
ee0b3e67 | 694 | struct iovec *iov = iovstack; |
1da177e4 | 695 | ssize_t ret; |
1da177e4 LT |
696 | io_fn_t fn; |
697 | iov_fn_t fnv; | |
698 | ||
eed4e51f BP |
699 | if (!file->f_op) { |
700 | ret = -EINVAL; | |
1da177e4 | 701 | goto out; |
1da177e4 | 702 | } |
1da177e4 | 703 | |
eed4e51f BP |
704 | ret = rw_copy_check_uvector(type, uvector, nr_segs, |
705 | ARRAY_SIZE(iovstack), iovstack, &iov); | |
706 | if (ret <= 0) | |
1da177e4 | 707 | goto out; |
1da177e4 | 708 | |
eed4e51f | 709 | tot_len = ret; |
1da177e4 | 710 | ret = rw_verify_area(type, file, pos, tot_len); |
e28cc715 | 711 | if (ret < 0) |
411b67b4 | 712 | goto out; |
1da177e4 LT |
713 | |
714 | fnv = NULL; | |
715 | if (type == READ) { | |
716 | fn = file->f_op->read; | |
ee0b3e67 | 717 | fnv = file->f_op->aio_read; |
1da177e4 LT |
718 | } else { |
719 | fn = (io_fn_t)file->f_op->write; | |
ee0b3e67 | 720 | fnv = file->f_op->aio_write; |
1da177e4 LT |
721 | } |
722 | ||
ee0b3e67 BP |
723 | if (fnv) |
724 | ret = do_sync_readv_writev(file, iov, nr_segs, tot_len, | |
725 | pos, fnv); | |
726 | else | |
727 | ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn); | |
1da177e4 | 728 | |
1da177e4 LT |
729 | out: |
730 | if (iov != iovstack) | |
731 | kfree(iov); | |
0eeca283 RL |
732 | if ((ret + (type == READ)) > 0) { |
733 | if (type == READ) | |
2a12a9d7 | 734 | fsnotify_access(file); |
0eeca283 | 735 | else |
2a12a9d7 | 736 | fsnotify_modify(file); |
0eeca283 | 737 | } |
1da177e4 | 738 | return ret; |
1da177e4 LT |
739 | } |
740 | ||
741 | ssize_t vfs_readv(struct file *file, const struct iovec __user *vec, | |
742 | unsigned long vlen, loff_t *pos) | |
743 | { | |
744 | if (!(file->f_mode & FMODE_READ)) | |
745 | return -EBADF; | |
ee0b3e67 | 746 | if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read)) |
1da177e4 LT |
747 | return -EINVAL; |
748 | ||
749 | return do_readv_writev(READ, file, vec, vlen, pos); | |
750 | } | |
751 | ||
752 | EXPORT_SYMBOL(vfs_readv); | |
753 | ||
754 | ssize_t vfs_writev(struct file *file, const struct iovec __user *vec, | |
755 | unsigned long vlen, loff_t *pos) | |
756 | { | |
757 | if (!(file->f_mode & FMODE_WRITE)) | |
758 | return -EBADF; | |
ee0b3e67 | 759 | if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write)) |
1da177e4 LT |
760 | return -EINVAL; |
761 | ||
762 | return do_readv_writev(WRITE, file, vec, vlen, pos); | |
763 | } | |
764 | ||
765 | EXPORT_SYMBOL(vfs_writev); | |
766 | ||
3cdad428 HC |
767 | SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec, |
768 | unsigned long, vlen) | |
1da177e4 LT |
769 | { |
770 | struct file *file; | |
771 | ssize_t ret = -EBADF; | |
772 | int fput_needed; | |
773 | ||
774 | file = fget_light(fd, &fput_needed); | |
775 | if (file) { | |
776 | loff_t pos = file_pos_read(file); | |
777 | ret = vfs_readv(file, vec, vlen, &pos); | |
778 | file_pos_write(file, pos); | |
779 | fput_light(file, fput_needed); | |
780 | } | |
781 | ||
782 | if (ret > 0) | |
4b98d11b AD |
783 | add_rchar(current, ret); |
784 | inc_syscr(current); | |
1da177e4 LT |
785 | return ret; |
786 | } | |
787 | ||
3cdad428 HC |
788 | SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec, |
789 | unsigned long, vlen) | |
1da177e4 LT |
790 | { |
791 | struct file *file; | |
792 | ssize_t ret = -EBADF; | |
793 | int fput_needed; | |
794 | ||
795 | file = fget_light(fd, &fput_needed); | |
796 | if (file) { | |
797 | loff_t pos = file_pos_read(file); | |
798 | ret = vfs_writev(file, vec, vlen, &pos); | |
799 | file_pos_write(file, pos); | |
800 | fput_light(file, fput_needed); | |
801 | } | |
802 | ||
803 | if (ret > 0) | |
4b98d11b AD |
804 | add_wchar(current, ret); |
805 | inc_syscw(current); | |
1da177e4 LT |
806 | return ret; |
807 | } | |
808 | ||
601cc11d LT |
809 | static inline loff_t pos_from_hilo(unsigned long high, unsigned long low) |
810 | { | |
811 | #define HALF_LONG_BITS (BITS_PER_LONG / 2) | |
812 | return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low; | |
813 | } | |
814 | ||
f3554f4b | 815 | SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec, |
601cc11d | 816 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) |
f3554f4b | 817 | { |
601cc11d | 818 | loff_t pos = pos_from_hilo(pos_h, pos_l); |
f3554f4b GH |
819 | struct file *file; |
820 | ssize_t ret = -EBADF; | |
821 | int fput_needed; | |
822 | ||
823 | if (pos < 0) | |
824 | return -EINVAL; | |
825 | ||
826 | file = fget_light(fd, &fput_needed); | |
827 | if (file) { | |
828 | ret = -ESPIPE; | |
829 | if (file->f_mode & FMODE_PREAD) | |
830 | ret = vfs_readv(file, vec, vlen, &pos); | |
831 | fput_light(file, fput_needed); | |
832 | } | |
833 | ||
834 | if (ret > 0) | |
835 | add_rchar(current, ret); | |
836 | inc_syscr(current); | |
837 | return ret; | |
838 | } | |
839 | ||
840 | SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, | |
601cc11d | 841 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) |
f3554f4b | 842 | { |
601cc11d | 843 | loff_t pos = pos_from_hilo(pos_h, pos_l); |
f3554f4b GH |
844 | struct file *file; |
845 | ssize_t ret = -EBADF; | |
846 | int fput_needed; | |
847 | ||
848 | if (pos < 0) | |
849 | return -EINVAL; | |
850 | ||
851 | file = fget_light(fd, &fput_needed); | |
852 | if (file) { | |
853 | ret = -ESPIPE; | |
854 | if (file->f_mode & FMODE_PWRITE) | |
855 | ret = vfs_writev(file, vec, vlen, &pos); | |
856 | fput_light(file, fput_needed); | |
857 | } | |
858 | ||
859 | if (ret > 0) | |
860 | add_wchar(current, ret); | |
861 | inc_syscw(current); | |
862 | return ret; | |
863 | } | |
864 | ||
1da177e4 LT |
865 | static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, |
866 | size_t count, loff_t max) | |
867 | { | |
868 | struct file * in_file, * out_file; | |
869 | struct inode * in_inode, * out_inode; | |
870 | loff_t pos; | |
871 | ssize_t retval; | |
534f2aaa | 872 | int fput_needed_in, fput_needed_out, fl; |
1da177e4 LT |
873 | |
874 | /* | |
875 | * Get input file, and verify that it is ok.. | |
876 | */ | |
877 | retval = -EBADF; | |
878 | in_file = fget_light(in_fd, &fput_needed_in); | |
879 | if (!in_file) | |
880 | goto out; | |
881 | if (!(in_file->f_mode & FMODE_READ)) | |
882 | goto fput_in; | |
1da177e4 LT |
883 | retval = -ESPIPE; |
884 | if (!ppos) | |
885 | ppos = &in_file->f_pos; | |
886 | else | |
887 | if (!(in_file->f_mode & FMODE_PREAD)) | |
888 | goto fput_in; | |
889 | retval = rw_verify_area(READ, in_file, ppos, count); | |
e28cc715 | 890 | if (retval < 0) |
1da177e4 | 891 | goto fput_in; |
e28cc715 | 892 | count = retval; |
1da177e4 | 893 | |
1da177e4 LT |
894 | /* |
895 | * Get output file, and verify that it is ok.. | |
896 | */ | |
897 | retval = -EBADF; | |
898 | out_file = fget_light(out_fd, &fput_needed_out); | |
899 | if (!out_file) | |
900 | goto fput_in; | |
901 | if (!(out_file->f_mode & FMODE_WRITE)) | |
902 | goto fput_out; | |
903 | retval = -EINVAL; | |
6818173b | 904 | in_inode = in_file->f_path.dentry->d_inode; |
0f7fc9e4 | 905 | out_inode = out_file->f_path.dentry->d_inode; |
1da177e4 | 906 | retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count); |
e28cc715 | 907 | if (retval < 0) |
1da177e4 | 908 | goto fput_out; |
e28cc715 | 909 | count = retval; |
1da177e4 | 910 | |
1da177e4 LT |
911 | if (!max) |
912 | max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); | |
913 | ||
914 | pos = *ppos; | |
1da177e4 LT |
915 | if (unlikely(pos + count > max)) { |
916 | retval = -EOVERFLOW; | |
917 | if (pos >= max) | |
918 | goto fput_out; | |
919 | count = max - pos; | |
920 | } | |
921 | ||
d96e6e71 | 922 | fl = 0; |
534f2aaa | 923 | #if 0 |
d96e6e71 JA |
924 | /* |
925 | * We need to debate whether we can enable this or not. The | |
926 | * man page documents EAGAIN return for the output at least, | |
927 | * and the application is arguably buggy if it doesn't expect | |
928 | * EAGAIN on a non-blocking file descriptor. | |
929 | */ | |
930 | if (in_file->f_flags & O_NONBLOCK) | |
931 | fl = SPLICE_F_NONBLOCK; | |
534f2aaa | 932 | #endif |
d96e6e71 | 933 | retval = do_splice_direct(in_file, ppos, out_file, count, fl); |
1da177e4 LT |
934 | |
935 | if (retval > 0) { | |
4b98d11b AD |
936 | add_rchar(current, retval); |
937 | add_wchar(current, retval); | |
1da177e4 | 938 | } |
1da177e4 | 939 | |
4b98d11b AD |
940 | inc_syscr(current); |
941 | inc_syscw(current); | |
1da177e4 LT |
942 | if (*ppos > max) |
943 | retval = -EOVERFLOW; | |
944 | ||
945 | fput_out: | |
946 | fput_light(out_file, fput_needed_out); | |
947 | fput_in: | |
948 | fput_light(in_file, fput_needed_in); | |
949 | out: | |
950 | return retval; | |
951 | } | |
952 | ||
002c8976 | 953 | SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count) |
1da177e4 LT |
954 | { |
955 | loff_t pos; | |
956 | off_t off; | |
957 | ssize_t ret; | |
958 | ||
959 | if (offset) { | |
960 | if (unlikely(get_user(off, offset))) | |
961 | return -EFAULT; | |
962 | pos = off; | |
963 | ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS); | |
964 | if (unlikely(put_user(pos, offset))) | |
965 | return -EFAULT; | |
966 | return ret; | |
967 | } | |
968 | ||
969 | return do_sendfile(out_fd, in_fd, NULL, count, 0); | |
970 | } | |
971 | ||
002c8976 | 972 | SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count) |
1da177e4 LT |
973 | { |
974 | loff_t pos; | |
975 | ssize_t ret; | |
976 | ||
977 | if (offset) { | |
978 | if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) | |
979 | return -EFAULT; | |
980 | ret = do_sendfile(out_fd, in_fd, &pos, count, 0); | |
981 | if (unlikely(put_user(pos, offset))) | |
982 | return -EFAULT; | |
983 | return ret; | |
984 | } | |
985 | ||
986 | return do_sendfile(out_fd, in_fd, NULL, count, 0); | |
987 | } |