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