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/fsnotify.h>
13 #include <linux/security.h>
14 #include <linux/module.h>
15 #include <linux/syscalls.h>
16 #include <linux/pagemap.h>
17 #include <linux/splice.h>
18 #include "read_write.h"
20 #include <asm/uaccess.h>
21 #include <asm/unistd.h>
23 const struct file_operations generic_ro_fops
= {
24 .llseek
= generic_file_llseek
,
26 .aio_read
= generic_file_aio_read
,
27 .mmap
= generic_file_readonly_mmap
,
28 .splice_read
= generic_file_splice_read
,
31 EXPORT_SYMBOL(generic_ro_fops
);
33 static inline int unsigned_offsets(struct file
*file
)
35 return file
->f_mode
& FMODE_UNSIGNED_OFFSET
;
39 * generic_file_llseek_unlocked - lockless generic llseek implementation
40 * @file: file structure to seek on
41 * @offset: file offset to seek to
42 * @origin: type of seek
44 * Updates the file offset to the value specified by @offset and @origin.
45 * Locking must be provided by the caller.
48 generic_file_llseek_unlocked(struct file
*file
, loff_t offset
, int origin
)
50 struct inode
*inode
= file
->f_mapping
->host
;
54 offset
+= inode
->i_size
;
58 * Here we special-case the lseek(fd, 0, SEEK_CUR)
59 * position-querying operation. Avoid rewriting the "same"
60 * f_pos value back to the file because a concurrent read(),
61 * write() or lseek() might have altered it
65 offset
+= file
->f_pos
;
69 * In the generic case the entire file is data, so as long as
70 * offset isn't at the end of the file then the offset is data.
72 if (offset
>= inode
->i_size
)
77 * There is a virtual hole at the end of the file, so as long as
78 * offset isn't i_size or larger, return i_size.
80 if (offset
>= inode
->i_size
)
82 offset
= inode
->i_size
;
86 if (offset
< 0 && !unsigned_offsets(file
))
88 if (offset
> inode
->i_sb
->s_maxbytes
)
91 /* Special lock needed here? */
92 if (offset
!= file
->f_pos
) {
99 EXPORT_SYMBOL(generic_file_llseek_unlocked
);
102 * generic_file_llseek - generic llseek implementation for regular files
103 * @file: file structure to seek on
104 * @offset: file offset to seek to
105 * @origin: type of seek
107 * This is a generic implemenation of ->llseek useable for all normal local
108 * filesystems. It just updates the file offset to the value specified by
109 * @offset and @origin under i_mutex.
111 loff_t
generic_file_llseek(struct file
*file
, loff_t offset
, int origin
)
115 mutex_lock(&file
->f_dentry
->d_inode
->i_mutex
);
116 rval
= generic_file_llseek_unlocked(file
, offset
, origin
);
117 mutex_unlock(&file
->f_dentry
->d_inode
->i_mutex
);
121 EXPORT_SYMBOL(generic_file_llseek
);
124 * noop_llseek - No Operation Performed llseek implementation
125 * @file: file structure to seek on
126 * @offset: file offset to seek to
127 * @origin: type of seek
129 * This is an implementation of ->llseek useable for the rare special case when
130 * userspace expects the seek to succeed but the (device) file is actually not
131 * able to perform the seek. In this case you use noop_llseek() instead of
132 * falling back to the default implementation of ->llseek.
134 loff_t
noop_llseek(struct file
*file
, loff_t offset
, int origin
)
138 EXPORT_SYMBOL(noop_llseek
);
140 loff_t
no_llseek(struct file
*file
, loff_t offset
, int origin
)
144 EXPORT_SYMBOL(no_llseek
);
146 loff_t
default_llseek(struct file
*file
, loff_t offset
, int origin
)
148 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
151 mutex_lock(&inode
->i_mutex
);
154 offset
+= i_size_read(inode
);
158 retval
= file
->f_pos
;
161 offset
+= file
->f_pos
;
165 * In the generic case the entire file is data, so as
166 * long as offset isn't at the end of the file then the
169 if (offset
>= inode
->i_size
)
174 * There is a virtual hole at the end of the file, so
175 * as long as offset isn't i_size or larger, return
178 if (offset
>= inode
->i_size
)
180 offset
= inode
->i_size
;
184 if (offset
>= 0 || unsigned_offsets(file
)) {
185 if (offset
!= file
->f_pos
) {
186 file
->f_pos
= offset
;
192 mutex_unlock(&inode
->i_mutex
);
195 EXPORT_SYMBOL(default_llseek
);
197 loff_t
vfs_llseek(struct file
*file
, loff_t offset
, int origin
)
199 loff_t (*fn
)(struct file
*, loff_t
, int);
202 if (file
->f_mode
& FMODE_LSEEK
) {
203 if (file
->f_op
&& file
->f_op
->llseek
)
204 fn
= file
->f_op
->llseek
;
206 return fn(file
, offset
, origin
);
208 EXPORT_SYMBOL(vfs_llseek
);
210 SYSCALL_DEFINE3(lseek
, unsigned int, fd
, off_t
, offset
, unsigned int, origin
)
217 file
= fget_light(fd
, &fput_needed
);
222 if (origin
<= SEEK_MAX
) {
223 loff_t res
= vfs_llseek(file
, offset
, origin
);
225 if (res
!= (loff_t
)retval
)
226 retval
= -EOVERFLOW
; /* LFS: should only happen on 32 bit platforms */
228 fput_light(file
, fput_needed
);
233 #ifdef __ARCH_WANT_SYS_LLSEEK
234 SYSCALL_DEFINE5(llseek
, unsigned int, fd
, unsigned long, offset_high
,
235 unsigned long, offset_low
, loff_t __user
*, result
,
236 unsigned int, origin
)
244 file
= fget_light(fd
, &fput_needed
);
249 if (origin
> SEEK_MAX
)
252 offset
= vfs_llseek(file
, ((loff_t
) offset_high
<< 32) | offset_low
,
255 retval
= (int)offset
;
258 if (!copy_to_user(result
, &offset
, sizeof(offset
)))
262 fput_light(file
, fput_needed
);
270 * rw_verify_area doesn't like huge counts. We limit
271 * them to something that fits in "int" so that others
272 * won't have to do range checks all the time.
274 int rw_verify_area(int read_write
, struct file
*file
, loff_t
*ppos
, size_t count
)
278 int retval
= -EINVAL
;
280 inode
= file
->f_path
.dentry
->d_inode
;
281 if (unlikely((ssize_t
) count
< 0))
284 if (unlikely(pos
< 0)) {
285 if (!unsigned_offsets(file
))
287 if (count
>= -pos
) /* both values are in 0..LLONG_MAX */
289 } else if (unlikely((loff_t
) (pos
+ count
) < 0)) {
290 if (!unsigned_offsets(file
))
294 if (unlikely(inode
->i_flock
&& mandatory_lock(inode
))) {
295 retval
= locks_mandatory_area(
296 read_write
== READ
? FLOCK_VERIFY_READ
: FLOCK_VERIFY_WRITE
,
297 inode
, file
, pos
, count
);
301 retval
= security_file_permission(file
,
302 read_write
== READ
? MAY_READ
: MAY_WRITE
);
305 return count
> MAX_RW_COUNT
? MAX_RW_COUNT
: count
;
308 static void wait_on_retry_sync_kiocb(struct kiocb
*iocb
)
310 set_current_state(TASK_UNINTERRUPTIBLE
);
311 if (!kiocbIsKicked(iocb
))
314 kiocbClearKicked(iocb
);
315 __set_current_state(TASK_RUNNING
);
318 ssize_t
do_sync_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*ppos
)
320 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
324 init_sync_kiocb(&kiocb
, filp
);
325 kiocb
.ki_pos
= *ppos
;
327 kiocb
.ki_nbytes
= len
;
330 ret
= filp
->f_op
->aio_read(&kiocb
, &iov
, 1, kiocb
.ki_pos
);
331 if (ret
!= -EIOCBRETRY
)
333 wait_on_retry_sync_kiocb(&kiocb
);
336 if (-EIOCBQUEUED
== ret
)
337 ret
= wait_on_sync_kiocb(&kiocb
);
338 *ppos
= kiocb
.ki_pos
;
342 EXPORT_SYMBOL(do_sync_read
);
344 ssize_t
vfs_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*pos
)
348 if (!(file
->f_mode
& FMODE_READ
))
350 if (!file
->f_op
|| (!file
->f_op
->read
&& !file
->f_op
->aio_read
))
352 if (unlikely(!access_ok(VERIFY_WRITE
, buf
, count
)))
355 ret
= rw_verify_area(READ
, file
, pos
, count
);
358 if (file
->f_op
->read
)
359 ret
= file
->f_op
->read(file
, buf
, count
, pos
);
361 ret
= do_sync_read(file
, buf
, count
, pos
);
363 fsnotify_access(file
);
364 add_rchar(current
, ret
);
372 EXPORT_SYMBOL(vfs_read
);
374 ssize_t
do_sync_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*ppos
)
376 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= len
};
380 init_sync_kiocb(&kiocb
, filp
);
381 kiocb
.ki_pos
= *ppos
;
383 kiocb
.ki_nbytes
= len
;
386 ret
= filp
->f_op
->aio_write(&kiocb
, &iov
, 1, kiocb
.ki_pos
);
387 if (ret
!= -EIOCBRETRY
)
389 wait_on_retry_sync_kiocb(&kiocb
);
392 if (-EIOCBQUEUED
== ret
)
393 ret
= wait_on_sync_kiocb(&kiocb
);
394 *ppos
= kiocb
.ki_pos
;
398 EXPORT_SYMBOL(do_sync_write
);
400 ssize_t
vfs_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*pos
)
404 if (!(file
->f_mode
& FMODE_WRITE
))
406 if (!file
->f_op
|| (!file
->f_op
->write
&& !file
->f_op
->aio_write
))
408 if (unlikely(!access_ok(VERIFY_READ
, buf
, count
)))
411 ret
= rw_verify_area(WRITE
, file
, pos
, count
);
414 if (file
->f_op
->write
)
415 ret
= file
->f_op
->write(file
, buf
, count
, pos
);
417 ret
= do_sync_write(file
, buf
, count
, pos
);
419 fsnotify_modify(file
);
420 add_wchar(current
, ret
);
428 EXPORT_SYMBOL(vfs_write
);
430 static inline loff_t
file_pos_read(struct file
*file
)
435 static inline void file_pos_write(struct file
*file
, loff_t pos
)
440 SYSCALL_DEFINE3(read
, unsigned int, fd
, char __user
*, buf
, size_t, count
)
443 ssize_t ret
= -EBADF
;
446 file
= fget_light(fd
, &fput_needed
);
448 loff_t pos
= file_pos_read(file
);
449 ret
= vfs_read(file
, buf
, count
, &pos
);
450 file_pos_write(file
, pos
);
451 fput_light(file
, fput_needed
);
457 SYSCALL_DEFINE3(write
, unsigned int, fd
, const char __user
*, buf
,
461 ssize_t ret
= -EBADF
;
464 file
= fget_light(fd
, &fput_needed
);
466 loff_t pos
= file_pos_read(file
);
467 ret
= vfs_write(file
, buf
, count
, &pos
);
468 file_pos_write(file
, pos
);
469 fput_light(file
, fput_needed
);
475 SYSCALL_DEFINE(pread64
)(unsigned int fd
, char __user
*buf
,
476 size_t count
, loff_t pos
)
479 ssize_t ret
= -EBADF
;
485 file
= fget_light(fd
, &fput_needed
);
488 if (file
->f_mode
& FMODE_PREAD
)
489 ret
= vfs_read(file
, buf
, count
, &pos
);
490 fput_light(file
, fput_needed
);
495 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
496 asmlinkage
long SyS_pread64(long fd
, long buf
, long count
, loff_t pos
)
498 return SYSC_pread64((unsigned int) fd
, (char __user
*) buf
,
499 (size_t) count
, pos
);
501 SYSCALL_ALIAS(sys_pread64
, SyS_pread64
);
504 SYSCALL_DEFINE(pwrite64
)(unsigned int fd
, const char __user
*buf
,
505 size_t count
, loff_t pos
)
508 ssize_t ret
= -EBADF
;
514 file
= fget_light(fd
, &fput_needed
);
517 if (file
->f_mode
& FMODE_PWRITE
)
518 ret
= vfs_write(file
, buf
, count
, &pos
);
519 fput_light(file
, fput_needed
);
524 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
525 asmlinkage
long SyS_pwrite64(long fd
, long buf
, long count
, loff_t pos
)
527 return SYSC_pwrite64((unsigned int) fd
, (const char __user
*) buf
,
528 (size_t) count
, pos
);
530 SYSCALL_ALIAS(sys_pwrite64
, SyS_pwrite64
);
534 * Reduce an iovec's length in-place. Return the resulting number of segments
536 unsigned long iov_shorten(struct iovec
*iov
, unsigned long nr_segs
, size_t to
)
538 unsigned long seg
= 0;
541 while (seg
< nr_segs
) {
543 if (len
+ iov
->iov_len
>= to
) {
544 iov
->iov_len
= to
- len
;
552 EXPORT_SYMBOL(iov_shorten
);
554 ssize_t
do_sync_readv_writev(struct file
*filp
, const struct iovec
*iov
,
555 unsigned long nr_segs
, size_t len
, loff_t
*ppos
, iov_fn_t fn
)
560 init_sync_kiocb(&kiocb
, filp
);
561 kiocb
.ki_pos
= *ppos
;
563 kiocb
.ki_nbytes
= len
;
566 ret
= fn(&kiocb
, iov
, nr_segs
, kiocb
.ki_pos
);
567 if (ret
!= -EIOCBRETRY
)
569 wait_on_retry_sync_kiocb(&kiocb
);
572 if (ret
== -EIOCBQUEUED
)
573 ret
= wait_on_sync_kiocb(&kiocb
);
574 *ppos
= kiocb
.ki_pos
;
578 /* Do it by hand, with file-ops */
579 ssize_t
do_loop_readv_writev(struct file
*filp
, struct iovec
*iov
,
580 unsigned long nr_segs
, loff_t
*ppos
, io_fn_t fn
)
582 struct iovec
*vector
= iov
;
585 while (nr_segs
> 0) {
590 base
= vector
->iov_base
;
591 len
= vector
->iov_len
;
595 nr
= fn(filp
, base
, len
, ppos
);
610 /* A write operation does a read from user space and vice versa */
611 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
613 ssize_t
rw_copy_check_uvector(int type
, const struct iovec __user
* uvector
,
614 unsigned long nr_segs
, unsigned long fast_segs
,
615 struct iovec
*fast_pointer
,
616 struct iovec
**ret_pointer
)
620 struct iovec
*iov
= fast_pointer
;
623 * SuS says "The readv() function *may* fail if the iovcnt argument
624 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
625 * traditionally returned zero for zero segments, so...
633 * First get the "struct iovec" from user memory and
634 * verify all the pointers
636 if (nr_segs
> UIO_MAXIOV
) {
640 if (nr_segs
> fast_segs
) {
641 iov
= kmalloc(nr_segs
*sizeof(struct iovec
), GFP_KERNEL
);
647 if (copy_from_user(iov
, uvector
, nr_segs
*sizeof(*uvector
))) {
653 * According to the Single Unix Specification we should return EINVAL
654 * if an element length is < 0 when cast to ssize_t or if the
655 * total length would overflow the ssize_t return value of the
658 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
662 for (seg
= 0; seg
< nr_segs
; seg
++) {
663 void __user
*buf
= iov
[seg
].iov_base
;
664 ssize_t len
= (ssize_t
)iov
[seg
].iov_len
;
666 /* see if we we're about to use an invalid len or if
667 * it's about to overflow ssize_t */
672 if (unlikely(!access_ok(vrfy_dir(type
), buf
, len
))) {
676 if (len
> MAX_RW_COUNT
- ret
) {
677 len
= MAX_RW_COUNT
- ret
;
678 iov
[seg
].iov_len
= len
;
687 static ssize_t
do_readv_writev(int type
, struct file
*file
,
688 const struct iovec __user
* uvector
,
689 unsigned long nr_segs
, loff_t
*pos
)
692 struct iovec iovstack
[UIO_FASTIOV
];
693 struct iovec
*iov
= iovstack
;
703 ret
= rw_copy_check_uvector(type
, uvector
, nr_segs
,
704 ARRAY_SIZE(iovstack
), iovstack
, &iov
);
709 ret
= rw_verify_area(type
, file
, pos
, tot_len
);
715 fn
= file
->f_op
->read
;
716 fnv
= file
->f_op
->aio_read
;
718 fn
= (io_fn_t
)file
->f_op
->write
;
719 fnv
= file
->f_op
->aio_write
;
723 ret
= do_sync_readv_writev(file
, iov
, nr_segs
, tot_len
,
726 ret
= do_loop_readv_writev(file
, iov
, nr_segs
, pos
, fn
);
731 if ((ret
+ (type
== READ
)) > 0) {
733 fsnotify_access(file
);
735 fsnotify_modify(file
);
740 ssize_t
vfs_readv(struct file
*file
, const struct iovec __user
*vec
,
741 unsigned long vlen
, loff_t
*pos
)
743 if (!(file
->f_mode
& FMODE_READ
))
745 if (!file
->f_op
|| (!file
->f_op
->aio_read
&& !file
->f_op
->read
))
748 return do_readv_writev(READ
, file
, vec
, vlen
, pos
);
751 EXPORT_SYMBOL(vfs_readv
);
753 ssize_t
vfs_writev(struct file
*file
, const struct iovec __user
*vec
,
754 unsigned long vlen
, loff_t
*pos
)
756 if (!(file
->f_mode
& FMODE_WRITE
))
758 if (!file
->f_op
|| (!file
->f_op
->aio_write
&& !file
->f_op
->write
))
761 return do_readv_writev(WRITE
, file
, vec
, vlen
, pos
);
764 EXPORT_SYMBOL(vfs_writev
);
766 SYSCALL_DEFINE3(readv
, unsigned long, fd
, const struct iovec __user
*, vec
,
770 ssize_t ret
= -EBADF
;
773 file
= fget_light(fd
, &fput_needed
);
775 loff_t pos
= file_pos_read(file
);
776 ret
= vfs_readv(file
, vec
, vlen
, &pos
);
777 file_pos_write(file
, pos
);
778 fput_light(file
, fput_needed
);
782 add_rchar(current
, ret
);
787 SYSCALL_DEFINE3(writev
, unsigned long, fd
, const struct iovec __user
*, vec
,
791 ssize_t ret
= -EBADF
;
794 file
= fget_light(fd
, &fput_needed
);
796 loff_t pos
= file_pos_read(file
);
797 ret
= vfs_writev(file
, vec
, vlen
, &pos
);
798 file_pos_write(file
, pos
);
799 fput_light(file
, fput_needed
);
803 add_wchar(current
, ret
);
808 static inline loff_t
pos_from_hilo(unsigned long high
, unsigned long low
)
810 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
811 return (((loff_t
)high
<< HALF_LONG_BITS
) << HALF_LONG_BITS
) | low
;
814 SYSCALL_DEFINE5(preadv
, unsigned long, fd
, const struct iovec __user
*, vec
,
815 unsigned long, vlen
, unsigned long, pos_l
, unsigned long, pos_h
)
817 loff_t pos
= pos_from_hilo(pos_h
, pos_l
);
819 ssize_t ret
= -EBADF
;
825 file
= fget_light(fd
, &fput_needed
);
828 if (file
->f_mode
& FMODE_PREAD
)
829 ret
= vfs_readv(file
, vec
, vlen
, &pos
);
830 fput_light(file
, fput_needed
);
834 add_rchar(current
, ret
);
839 SYSCALL_DEFINE5(pwritev
, unsigned long, fd
, const struct iovec __user
*, vec
,
840 unsigned long, vlen
, unsigned long, pos_l
, unsigned long, pos_h
)
842 loff_t pos
= pos_from_hilo(pos_h
, pos_l
);
844 ssize_t ret
= -EBADF
;
850 file
= fget_light(fd
, &fput_needed
);
853 if (file
->f_mode
& FMODE_PWRITE
)
854 ret
= vfs_writev(file
, vec
, vlen
, &pos
);
855 fput_light(file
, fput_needed
);
859 add_wchar(current
, ret
);
864 static ssize_t
do_sendfile(int out_fd
, int in_fd
, loff_t
*ppos
,
865 size_t count
, loff_t max
)
867 struct file
* in_file
, * out_file
;
868 struct inode
* in_inode
, * out_inode
;
871 int fput_needed_in
, fput_needed_out
, fl
;
874 * Get input file, and verify that it is ok..
877 in_file
= fget_light(in_fd
, &fput_needed_in
);
880 if (!(in_file
->f_mode
& FMODE_READ
))
884 ppos
= &in_file
->f_pos
;
886 if (!(in_file
->f_mode
& FMODE_PREAD
))
888 retval
= rw_verify_area(READ
, in_file
, ppos
, count
);
894 * Get output file, and verify that it is ok..
897 out_file
= fget_light(out_fd
, &fput_needed_out
);
900 if (!(out_file
->f_mode
& FMODE_WRITE
))
903 in_inode
= in_file
->f_path
.dentry
->d_inode
;
904 out_inode
= out_file
->f_path
.dentry
->d_inode
;
905 retval
= rw_verify_area(WRITE
, out_file
, &out_file
->f_pos
, count
);
911 max
= min(in_inode
->i_sb
->s_maxbytes
, out_inode
->i_sb
->s_maxbytes
);
914 if (unlikely(pos
+ count
> max
)) {
924 * We need to debate whether we can enable this or not. The
925 * man page documents EAGAIN return for the output at least,
926 * and the application is arguably buggy if it doesn't expect
927 * EAGAIN on a non-blocking file descriptor.
929 if (in_file
->f_flags
& O_NONBLOCK
)
930 fl
= SPLICE_F_NONBLOCK
;
932 retval
= do_splice_direct(in_file
, ppos
, out_file
, count
, fl
);
935 add_rchar(current
, retval
);
936 add_wchar(current
, retval
);
945 fput_light(out_file
, fput_needed_out
);
947 fput_light(in_file
, fput_needed_in
);
952 SYSCALL_DEFINE4(sendfile
, int, out_fd
, int, in_fd
, off_t __user
*, offset
, size_t, count
)
959 if (unlikely(get_user(off
, offset
)))
962 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, MAX_NON_LFS
);
963 if (unlikely(put_user(pos
, offset
)))
968 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);
971 SYSCALL_DEFINE4(sendfile64
, int, out_fd
, int, in_fd
, loff_t __user
*, offset
, size_t, count
)
977 if (unlikely(copy_from_user(&pos
, offset
, sizeof(loff_t
))))
979 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, 0);
980 if (unlikely(put_user(pos
, offset
)))
985 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);