2 * linux/fs/read_write.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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>
12 #include <linux/smp_lock.h>
13 #include <linux/fsnotify.h>
14 #include <linux/security.h>
15 #include <linux/module.h>
16 #include <linux/syscalls.h>
17 #include <linux/pagemap.h>
18 #include <linux/splice.h>
19 #include "read_write.h"
21 #include <asm/uaccess.h>
22 #include <asm/unistd.h>
24 const struct file_operations generic_ro_fops
= {
25 .llseek
= generic_file_llseek
,
27 .aio_read
= generic_file_aio_read
,
28 .mmap
= generic_file_readonly_mmap
,
29 .splice_read
= generic_file_splice_read
,
32 EXPORT_SYMBOL(generic_ro_fops
);
34 loff_t
generic_file_llseek(struct file
*file
, loff_t offset
, int origin
)
37 struct inode
*inode
= file
->f_mapping
->host
;
39 mutex_lock(&inode
->i_mutex
);
42 offset
+= inode
->i_size
;
45 offset
+= file
->f_pos
;
48 if (offset
>=0 && offset
<=inode
->i_sb
->s_maxbytes
) {
49 if (offset
!= file
->f_pos
) {
55 mutex_unlock(&inode
->i_mutex
);
59 EXPORT_SYMBOL(generic_file_llseek
);
61 loff_t
remote_llseek(struct file
*file
, loff_t offset
, int origin
)
68 offset
+= i_size_read(file
->f_path
.dentry
->d_inode
);
71 offset
+= file
->f_pos
;
74 if (offset
>=0 && offset
<=file
->f_path
.dentry
->d_inode
->i_sb
->s_maxbytes
) {
75 if (offset
!= file
->f_pos
) {
84 EXPORT_SYMBOL(remote_llseek
);
86 loff_t
no_llseek(struct file
*file
, loff_t offset
, int origin
)
90 EXPORT_SYMBOL(no_llseek
);
92 loff_t
default_llseek(struct file
*file
, loff_t offset
, int origin
)
99 offset
+= i_size_read(file
->f_path
.dentry
->d_inode
);
102 offset
+= file
->f_pos
;
106 if (offset
!= file
->f_pos
) {
107 file
->f_pos
= offset
;
115 EXPORT_SYMBOL(default_llseek
);
117 loff_t
vfs_llseek(struct file
*file
, loff_t offset
, int origin
)
119 loff_t (*fn
)(struct file
*, loff_t
, int);
122 if (file
->f_mode
& FMODE_LSEEK
) {
124 if (file
->f_op
&& file
->f_op
->llseek
)
125 fn
= file
->f_op
->llseek
;
127 return fn(file
, offset
, origin
);
129 EXPORT_SYMBOL(vfs_llseek
);
131 asmlinkage off_t
sys_lseek(unsigned int fd
, off_t offset
, unsigned int origin
)
138 file
= fget_light(fd
, &fput_needed
);
143 if (origin
<= SEEK_MAX
) {
144 loff_t res
= vfs_llseek(file
, offset
, origin
);
146 if (res
!= (loff_t
)retval
)
147 retval
= -EOVERFLOW
; /* LFS: should only happen on 32 bit platforms */
149 fput_light(file
, fput_needed
);
154 #ifdef __ARCH_WANT_SYS_LLSEEK
155 asmlinkage
long sys_llseek(unsigned int fd
, unsigned long offset_high
,
156 unsigned long offset_low
, loff_t __user
* result
,
165 file
= fget_light(fd
, &fput_needed
);
170 if (origin
> SEEK_MAX
)
173 offset
= vfs_llseek(file
, ((loff_t
) offset_high
<< 32) | offset_low
,
176 retval
= (int)offset
;
179 if (!copy_to_user(result
, &offset
, sizeof(offset
)))
183 fput_light(file
, fput_needed
);
190 * rw_verify_area doesn't like huge counts. We limit
191 * them to something that fits in "int" so that others
192 * won't have to do range checks all the time.
194 #define MAX_RW_COUNT (INT_MAX & PAGE_CACHE_MASK)
196 int rw_verify_area(int read_write
, struct file
*file
, loff_t
*ppos
, size_t count
)
201 inode
= file
->f_path
.dentry
->d_inode
;
202 if (unlikely((ssize_t
) count
< 0))
205 if (unlikely((pos
< 0) || (loff_t
) (pos
+ count
) < 0))
208 if (unlikely(inode
->i_flock
&& MANDATORY_LOCK(inode
))) {
209 int retval
= locks_mandatory_area(
210 read_write
== READ
? FLOCK_VERIFY_READ
: FLOCK_VERIFY_WRITE
,
211 inode
, file
, pos
, count
);
215 return count
> MAX_RW_COUNT
? MAX_RW_COUNT
: count
;
221 static void wait_on_retry_sync_kiocb(struct kiocb
*iocb
)
223 set_current_state(TASK_UNINTERRUPTIBLE
);
224 if (!kiocbIsKicked(iocb
))
227 kiocbClearKicked(iocb
);
228 __set_current_state(TASK_RUNNING
);
231 ssize_t
do_sync_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*ppos
)
233 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
237 init_sync_kiocb(&kiocb
, filp
);
238 kiocb
.ki_pos
= *ppos
;
242 ret
= filp
->f_op
->aio_read(&kiocb
, &iov
, 1, kiocb
.ki_pos
);
243 if (ret
!= -EIOCBRETRY
)
245 wait_on_retry_sync_kiocb(&kiocb
);
248 if (-EIOCBQUEUED
== ret
)
249 ret
= wait_on_sync_kiocb(&kiocb
);
250 *ppos
= kiocb
.ki_pos
;
254 EXPORT_SYMBOL(do_sync_read
);
256 ssize_t
vfs_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*pos
)
260 if (!(file
->f_mode
& FMODE_READ
))
262 if (!file
->f_op
|| (!file
->f_op
->read
&& !file
->f_op
->aio_read
))
264 if (unlikely(!access_ok(VERIFY_WRITE
, buf
, count
)))
267 ret
= rw_verify_area(READ
, file
, pos
, count
);
270 ret
= security_file_permission (file
, MAY_READ
);
272 if (file
->f_op
->read
)
273 ret
= file
->f_op
->read(file
, buf
, count
, pos
);
275 ret
= do_sync_read(file
, buf
, count
, pos
);
277 fsnotify_access(file
->f_path
.dentry
);
278 add_rchar(current
, ret
);
287 EXPORT_SYMBOL(vfs_read
);
289 ssize_t
do_sync_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*ppos
)
291 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= len
};
295 init_sync_kiocb(&kiocb
, filp
);
296 kiocb
.ki_pos
= *ppos
;
300 ret
= filp
->f_op
->aio_write(&kiocb
, &iov
, 1, kiocb
.ki_pos
);
301 if (ret
!= -EIOCBRETRY
)
303 wait_on_retry_sync_kiocb(&kiocb
);
306 if (-EIOCBQUEUED
== ret
)
307 ret
= wait_on_sync_kiocb(&kiocb
);
308 *ppos
= kiocb
.ki_pos
;
312 EXPORT_SYMBOL(do_sync_write
);
314 ssize_t
vfs_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*pos
)
318 if (!(file
->f_mode
& FMODE_WRITE
))
320 if (!file
->f_op
|| (!file
->f_op
->write
&& !file
->f_op
->aio_write
))
322 if (unlikely(!access_ok(VERIFY_READ
, buf
, count
)))
325 ret
= rw_verify_area(WRITE
, file
, pos
, count
);
328 ret
= security_file_permission (file
, MAY_WRITE
);
330 if (file
->f_op
->write
)
331 ret
= file
->f_op
->write(file
, buf
, count
, pos
);
333 ret
= do_sync_write(file
, buf
, count
, pos
);
335 fsnotify_modify(file
->f_path
.dentry
);
336 add_wchar(current
, ret
);
345 EXPORT_SYMBOL(vfs_write
);
347 static inline loff_t
file_pos_read(struct file
*file
)
352 static inline void file_pos_write(struct file
*file
, loff_t pos
)
357 asmlinkage ssize_t
sys_read(unsigned int fd
, char __user
* buf
, size_t count
)
360 ssize_t ret
= -EBADF
;
363 file
= fget_light(fd
, &fput_needed
);
365 loff_t pos
= file_pos_read(file
);
366 ret
= vfs_read(file
, buf
, count
, &pos
);
367 file_pos_write(file
, pos
);
368 fput_light(file
, fput_needed
);
373 EXPORT_SYMBOL_GPL(sys_read
);
375 asmlinkage ssize_t
sys_write(unsigned int fd
, const char __user
* buf
, size_t count
)
378 ssize_t ret
= -EBADF
;
381 file
= fget_light(fd
, &fput_needed
);
383 loff_t pos
= file_pos_read(file
);
384 ret
= vfs_write(file
, buf
, count
, &pos
);
385 file_pos_write(file
, pos
);
386 fput_light(file
, fput_needed
);
392 asmlinkage ssize_t
sys_pread64(unsigned int fd
, char __user
*buf
,
393 size_t count
, loff_t pos
)
396 ssize_t ret
= -EBADF
;
402 file
= fget_light(fd
, &fput_needed
);
405 if (file
->f_mode
& FMODE_PREAD
)
406 ret
= vfs_read(file
, buf
, count
, &pos
);
407 fput_light(file
, fput_needed
);
413 asmlinkage ssize_t
sys_pwrite64(unsigned int fd
, const char __user
*buf
,
414 size_t count
, loff_t pos
)
417 ssize_t ret
= -EBADF
;
423 file
= fget_light(fd
, &fput_needed
);
426 if (file
->f_mode
& FMODE_PWRITE
)
427 ret
= vfs_write(file
, buf
, count
, &pos
);
428 fput_light(file
, fput_needed
);
435 * Reduce an iovec's length in-place. Return the resulting number of segments
437 unsigned long iov_shorten(struct iovec
*iov
, unsigned long nr_segs
, size_t to
)
439 unsigned long seg
= 0;
442 while (seg
< nr_segs
) {
444 if (len
+ iov
->iov_len
>= to
) {
445 iov
->iov_len
= to
- len
;
454 ssize_t
do_sync_readv_writev(struct file
*filp
, const struct iovec
*iov
,
455 unsigned long nr_segs
, size_t len
, loff_t
*ppos
, iov_fn_t fn
)
460 init_sync_kiocb(&kiocb
, filp
);
461 kiocb
.ki_pos
= *ppos
;
463 kiocb
.ki_nbytes
= len
;
466 ret
= fn(&kiocb
, iov
, nr_segs
, kiocb
.ki_pos
);
467 if (ret
!= -EIOCBRETRY
)
469 wait_on_retry_sync_kiocb(&kiocb
);
472 if (ret
== -EIOCBQUEUED
)
473 ret
= wait_on_sync_kiocb(&kiocb
);
474 *ppos
= kiocb
.ki_pos
;
478 /* Do it by hand, with file-ops */
479 ssize_t
do_loop_readv_writev(struct file
*filp
, struct iovec
*iov
,
480 unsigned long nr_segs
, loff_t
*ppos
, io_fn_t fn
)
482 struct iovec
*vector
= iov
;
485 while (nr_segs
> 0) {
490 base
= vector
->iov_base
;
491 len
= vector
->iov_len
;
495 nr
= fn(filp
, base
, len
, ppos
);
510 /* A write operation does a read from user space and vice versa */
511 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
513 ssize_t
rw_copy_check_uvector(int type
, const struct iovec __user
* uvector
,
514 unsigned long nr_segs
, unsigned long fast_segs
,
515 struct iovec
*fast_pointer
,
516 struct iovec
**ret_pointer
)
520 struct iovec
*iov
= fast_pointer
;
523 * SuS says "The readv() function *may* fail if the iovcnt argument
524 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
525 * traditionally returned zero for zero segments, so...
533 * First get the "struct iovec" from user memory and
534 * verify all the pointers
536 if (nr_segs
> UIO_MAXIOV
) {
540 if (nr_segs
> fast_segs
) {
541 iov
= kmalloc(nr_segs
*sizeof(struct iovec
), GFP_KERNEL
);
547 if (copy_from_user(iov
, uvector
, nr_segs
*sizeof(*uvector
))) {
553 * According to the Single Unix Specification we should return EINVAL
554 * if an element length is < 0 when cast to ssize_t or if the
555 * total length would overflow the ssize_t return value of the
559 for (seg
= 0; seg
< nr_segs
; seg
++) {
560 void __user
*buf
= iov
[seg
].iov_base
;
561 ssize_t len
= (ssize_t
)iov
[seg
].iov_len
;
563 /* see if we we're about to use an invalid len or if
564 * it's about to overflow ssize_t */
565 if (len
< 0 || (ret
+ len
< ret
)) {
569 if (unlikely(!access_ok(vrfy_dir(type
), buf
, len
))) {
581 static ssize_t
do_readv_writev(int type
, struct file
*file
,
582 const struct iovec __user
* uvector
,
583 unsigned long nr_segs
, loff_t
*pos
)
586 struct iovec iovstack
[UIO_FASTIOV
];
587 struct iovec
*iov
= iovstack
;
597 ret
= rw_copy_check_uvector(type
, uvector
, nr_segs
,
598 ARRAY_SIZE(iovstack
), iovstack
, &iov
);
603 ret
= rw_verify_area(type
, file
, pos
, tot_len
);
606 ret
= security_file_permission(file
, type
== READ
? MAY_READ
: MAY_WRITE
);
612 fn
= file
->f_op
->read
;
613 fnv
= file
->f_op
->aio_read
;
615 fn
= (io_fn_t
)file
->f_op
->write
;
616 fnv
= file
->f_op
->aio_write
;
620 ret
= do_sync_readv_writev(file
, iov
, nr_segs
, tot_len
,
623 ret
= do_loop_readv_writev(file
, iov
, nr_segs
, pos
, fn
);
628 if ((ret
+ (type
== READ
)) > 0) {
630 fsnotify_access(file
->f_path
.dentry
);
632 fsnotify_modify(file
->f_path
.dentry
);
637 ssize_t
vfs_readv(struct file
*file
, const struct iovec __user
*vec
,
638 unsigned long vlen
, loff_t
*pos
)
640 if (!(file
->f_mode
& FMODE_READ
))
642 if (!file
->f_op
|| (!file
->f_op
->aio_read
&& !file
->f_op
->read
))
645 return do_readv_writev(READ
, file
, vec
, vlen
, pos
);
648 EXPORT_SYMBOL(vfs_readv
);
650 ssize_t
vfs_writev(struct file
*file
, const struct iovec __user
*vec
,
651 unsigned long vlen
, loff_t
*pos
)
653 if (!(file
->f_mode
& FMODE_WRITE
))
655 if (!file
->f_op
|| (!file
->f_op
->aio_write
&& !file
->f_op
->write
))
658 return do_readv_writev(WRITE
, file
, vec
, vlen
, pos
);
661 EXPORT_SYMBOL(vfs_writev
);
664 sys_readv(unsigned long fd
, const struct iovec __user
*vec
, unsigned long vlen
)
667 ssize_t ret
= -EBADF
;
670 file
= fget_light(fd
, &fput_needed
);
672 loff_t pos
= file_pos_read(file
);
673 ret
= vfs_readv(file
, vec
, vlen
, &pos
);
674 file_pos_write(file
, pos
);
675 fput_light(file
, fput_needed
);
679 add_rchar(current
, ret
);
685 sys_writev(unsigned long fd
, const struct iovec __user
*vec
, unsigned long vlen
)
688 ssize_t ret
= -EBADF
;
691 file
= fget_light(fd
, &fput_needed
);
693 loff_t pos
= file_pos_read(file
);
694 ret
= vfs_writev(file
, vec
, vlen
, &pos
);
695 file_pos_write(file
, pos
);
696 fput_light(file
, fput_needed
);
700 add_wchar(current
, ret
);
705 static ssize_t
do_sendfile(int out_fd
, int in_fd
, loff_t
*ppos
,
706 size_t count
, loff_t max
)
708 struct file
* in_file
, * out_file
;
709 struct inode
* in_inode
, * out_inode
;
712 int fput_needed_in
, fput_needed_out
, fl
;
715 * Get input file, and verify that it is ok..
718 in_file
= fget_light(in_fd
, &fput_needed_in
);
721 if (!(in_file
->f_mode
& FMODE_READ
))
724 in_inode
= in_file
->f_path
.dentry
->d_inode
;
727 if (!in_file
->f_op
|| !in_file
->f_op
->splice_read
)
731 ppos
= &in_file
->f_pos
;
733 if (!(in_file
->f_mode
& FMODE_PREAD
))
735 retval
= rw_verify_area(READ
, in_file
, ppos
, count
);
740 retval
= security_file_permission (in_file
, MAY_READ
);
745 * Get output file, and verify that it is ok..
748 out_file
= fget_light(out_fd
, &fput_needed_out
);
751 if (!(out_file
->f_mode
& FMODE_WRITE
))
754 if (!out_file
->f_op
|| !out_file
->f_op
->sendpage
)
756 out_inode
= out_file
->f_path
.dentry
->d_inode
;
757 retval
= rw_verify_area(WRITE
, out_file
, &out_file
->f_pos
, count
);
762 retval
= security_file_permission (out_file
, MAY_WRITE
);
767 max
= min(in_inode
->i_sb
->s_maxbytes
, out_inode
->i_sb
->s_maxbytes
);
771 if (unlikely(pos
< 0))
773 if (unlikely(pos
+ count
> max
)) {
783 * We need to debate whether we can enable this or not. The
784 * man page documents EAGAIN return for the output at least,
785 * and the application is arguably buggy if it doesn't expect
786 * EAGAIN on a non-blocking file descriptor.
788 if (in_file
->f_flags
& O_NONBLOCK
)
789 fl
= SPLICE_F_NONBLOCK
;
791 retval
= do_splice_direct(in_file
, ppos
, out_file
, count
, fl
);
794 add_rchar(current
, retval
);
795 add_wchar(current
, retval
);
804 fput_light(out_file
, fput_needed_out
);
806 fput_light(in_file
, fput_needed_in
);
811 asmlinkage ssize_t
sys_sendfile(int out_fd
, int in_fd
, off_t __user
*offset
, size_t count
)
818 if (unlikely(get_user(off
, offset
)))
821 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, MAX_NON_LFS
);
822 if (unlikely(put_user(pos
, offset
)))
827 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);
830 asmlinkage ssize_t
sys_sendfile64(int out_fd
, int in_fd
, loff_t __user
*offset
, size_t count
)
836 if (unlikely(copy_from_user(&pos
, offset
, sizeof(loff_t
))))
838 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, 0);
839 if (unlikely(put_user(pos
, offset
)))
844 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);