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
;
38 static loff_t
lseek_execute(struct file
*file
, struct inode
*inode
,
39 loff_t offset
, loff_t maxsize
)
41 if (offset
< 0 && !unsigned_offsets(file
))
46 if (offset
!= file
->f_pos
) {
54 * generic_file_llseek - generic llseek implementation for regular files
55 * @file: file structure to seek on
56 * @offset: file offset to seek to
57 * @origin: type of seek
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.
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.
70 generic_file_llseek(struct file
*file
, loff_t offset
, int origin
)
72 struct inode
*inode
= file
->f_mapping
->host
;
76 offset
+= i_size_read(inode
);
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
88 * f_lock protects against read/modify/write race with other
89 * SEEK_CURs. Note that parallel writes and reads behave
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
);
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.
102 if (offset
>= i_size_read(inode
))
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.
110 if (offset
>= i_size_read(inode
))
112 offset
= i_size_read(inode
);
116 return lseek_execute(file
, inode
, offset
, inode
->i_sb
->s_maxbytes
);
118 EXPORT_SYMBOL(generic_file_llseek
);
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
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.
131 loff_t
noop_llseek(struct file
*file
, loff_t offset
, int origin
)
135 EXPORT_SYMBOL(noop_llseek
);
137 loff_t
no_llseek(struct file
*file
, loff_t offset
, int origin
)
141 EXPORT_SYMBOL(no_llseek
);
143 loff_t
default_llseek(struct file
*file
, loff_t offset
, int origin
)
145 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
148 mutex_lock(&inode
->i_mutex
);
151 offset
+= i_size_read(inode
);
155 retval
= file
->f_pos
;
158 offset
+= file
->f_pos
;
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
166 if (offset
>= inode
->i_size
) {
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
177 if (offset
>= inode
->i_size
) {
181 offset
= inode
->i_size
;
185 if (offset
>= 0 || unsigned_offsets(file
)) {
186 if (offset
!= file
->f_pos
) {
187 file
->f_pos
= offset
;
193 mutex_unlock(&inode
->i_mutex
);
196 EXPORT_SYMBOL(default_llseek
);
198 loff_t
vfs_llseek(struct file
*file
, loff_t offset
, int origin
)
200 loff_t (*fn
)(struct file
*, loff_t
, int);
203 if (file
->f_mode
& FMODE_LSEEK
) {
204 if (file
->f_op
&& file
->f_op
->llseek
)
205 fn
= file
->f_op
->llseek
;
207 return fn(file
, offset
, origin
);
209 EXPORT_SYMBOL(vfs_llseek
);
211 SYSCALL_DEFINE3(lseek
, unsigned int, fd
, off_t
, offset
, unsigned int, origin
)
218 file
= fget_light(fd
, &fput_needed
);
223 if (origin
<= SEEK_MAX
) {
224 loff_t res
= vfs_llseek(file
, offset
, origin
);
226 if (res
!= (loff_t
)retval
)
227 retval
= -EOVERFLOW
; /* LFS: should only happen on 32 bit platforms */
229 fput_light(file
, fput_needed
);
234 #ifdef __ARCH_WANT_SYS_LLSEEK
235 SYSCALL_DEFINE5(llseek
, unsigned int, fd
, unsigned long, offset_high
,
236 unsigned long, offset_low
, loff_t __user
*, result
,
237 unsigned int, origin
)
245 file
= fget_light(fd
, &fput_needed
);
250 if (origin
> SEEK_MAX
)
253 offset
= vfs_llseek(file
, ((loff_t
) offset_high
<< 32) | offset_low
,
256 retval
= (int)offset
;
259 if (!copy_to_user(result
, &offset
, sizeof(offset
)))
263 fput_light(file
, fput_needed
);
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.
275 int rw_verify_area(int read_write
, struct file
*file
, loff_t
*ppos
, size_t count
)
279 int retval
= -EINVAL
;
281 inode
= file
->f_path
.dentry
->d_inode
;
282 if (unlikely((ssize_t
) count
< 0))
285 if (unlikely(pos
< 0)) {
286 if (!unsigned_offsets(file
))
288 if (count
>= -pos
) /* both values are in 0..LLONG_MAX */
290 } else if (unlikely((loff_t
) (pos
+ count
) < 0)) {
291 if (!unsigned_offsets(file
))
295 if (unlikely(inode
->i_flock
&& mandatory_lock(inode
))) {
296 retval
= locks_mandatory_area(
297 read_write
== READ
? FLOCK_VERIFY_READ
: FLOCK_VERIFY_WRITE
,
298 inode
, file
, pos
, count
);
302 retval
= security_file_permission(file
,
303 read_write
== READ
? MAY_READ
: MAY_WRITE
);
306 return count
> MAX_RW_COUNT
? MAX_RW_COUNT
: count
;
309 static void wait_on_retry_sync_kiocb(struct kiocb
*iocb
)
311 set_current_state(TASK_UNINTERRUPTIBLE
);
312 if (!kiocbIsKicked(iocb
))
315 kiocbClearKicked(iocb
);
316 __set_current_state(TASK_RUNNING
);
319 ssize_t
do_sync_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*ppos
)
321 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
325 init_sync_kiocb(&kiocb
, filp
);
326 kiocb
.ki_pos
= *ppos
;
328 kiocb
.ki_nbytes
= len
;
331 ret
= filp
->f_op
->aio_read(&kiocb
, &iov
, 1, kiocb
.ki_pos
);
332 if (ret
!= -EIOCBRETRY
)
334 wait_on_retry_sync_kiocb(&kiocb
);
337 if (-EIOCBQUEUED
== ret
)
338 ret
= wait_on_sync_kiocb(&kiocb
);
339 *ppos
= kiocb
.ki_pos
;
343 EXPORT_SYMBOL(do_sync_read
);
345 ssize_t
vfs_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*pos
)
349 if (!(file
->f_mode
& FMODE_READ
))
351 if (!file
->f_op
|| (!file
->f_op
->read
&& !file
->f_op
->aio_read
))
353 if (unlikely(!access_ok(VERIFY_WRITE
, buf
, count
)))
356 ret
= rw_verify_area(READ
, file
, pos
, count
);
359 if (file
->f_op
->read
)
360 ret
= file
->f_op
->read(file
, buf
, count
, pos
);
362 ret
= do_sync_read(file
, buf
, count
, pos
);
364 fsnotify_access(file
);
365 add_rchar(current
, ret
);
373 EXPORT_SYMBOL(vfs_read
);
375 ssize_t
do_sync_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*ppos
)
377 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= len
};
381 init_sync_kiocb(&kiocb
, filp
);
382 kiocb
.ki_pos
= *ppos
;
384 kiocb
.ki_nbytes
= len
;
387 ret
= filp
->f_op
->aio_write(&kiocb
, &iov
, 1, kiocb
.ki_pos
);
388 if (ret
!= -EIOCBRETRY
)
390 wait_on_retry_sync_kiocb(&kiocb
);
393 if (-EIOCBQUEUED
== ret
)
394 ret
= wait_on_sync_kiocb(&kiocb
);
395 *ppos
= kiocb
.ki_pos
;
399 EXPORT_SYMBOL(do_sync_write
);
401 ssize_t
vfs_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*pos
)
405 if (!(file
->f_mode
& FMODE_WRITE
))
407 if (!file
->f_op
|| (!file
->f_op
->write
&& !file
->f_op
->aio_write
))
409 if (unlikely(!access_ok(VERIFY_READ
, buf
, count
)))
412 ret
= rw_verify_area(WRITE
, file
, pos
, count
);
415 if (file
->f_op
->write
)
416 ret
= file
->f_op
->write(file
, buf
, count
, pos
);
418 ret
= do_sync_write(file
, buf
, count
, pos
);
420 fsnotify_modify(file
);
421 add_wchar(current
, ret
);
429 EXPORT_SYMBOL(vfs_write
);
431 static inline loff_t
file_pos_read(struct file
*file
)
436 static inline void file_pos_write(struct file
*file
, loff_t pos
)
441 SYSCALL_DEFINE3(read
, unsigned int, fd
, char __user
*, buf
, size_t, count
)
444 ssize_t ret
= -EBADF
;
447 file
= fget_light(fd
, &fput_needed
);
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
);
458 SYSCALL_DEFINE3(write
, unsigned int, fd
, const char __user
*, buf
,
462 ssize_t ret
= -EBADF
;
465 file
= fget_light(fd
, &fput_needed
);
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
);
476 SYSCALL_DEFINE(pread64
)(unsigned int fd
, char __user
*buf
,
477 size_t count
, loff_t pos
)
480 ssize_t ret
= -EBADF
;
486 file
= fget_light(fd
, &fput_needed
);
489 if (file
->f_mode
& FMODE_PREAD
)
490 ret
= vfs_read(file
, buf
, count
, &pos
);
491 fput_light(file
, fput_needed
);
496 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
497 asmlinkage
long SyS_pread64(long fd
, long buf
, long count
, loff_t pos
)
499 return SYSC_pread64((unsigned int) fd
, (char __user
*) buf
,
500 (size_t) count
, pos
);
502 SYSCALL_ALIAS(sys_pread64
, SyS_pread64
);
505 SYSCALL_DEFINE(pwrite64
)(unsigned int fd
, const char __user
*buf
,
506 size_t count
, loff_t pos
)
509 ssize_t ret
= -EBADF
;
515 file
= fget_light(fd
, &fput_needed
);
518 if (file
->f_mode
& FMODE_PWRITE
)
519 ret
= vfs_write(file
, buf
, count
, &pos
);
520 fput_light(file
, fput_needed
);
525 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
526 asmlinkage
long SyS_pwrite64(long fd
, long buf
, long count
, loff_t pos
)
528 return SYSC_pwrite64((unsigned int) fd
, (const char __user
*) buf
,
529 (size_t) count
, pos
);
531 SYSCALL_ALIAS(sys_pwrite64
, SyS_pwrite64
);
535 * Reduce an iovec's length in-place. Return the resulting number of segments
537 unsigned long iov_shorten(struct iovec
*iov
, unsigned long nr_segs
, size_t to
)
539 unsigned long seg
= 0;
542 while (seg
< nr_segs
) {
544 if (len
+ iov
->iov_len
>= to
) {
545 iov
->iov_len
= to
- len
;
553 EXPORT_SYMBOL(iov_shorten
);
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
)
561 init_sync_kiocb(&kiocb
, filp
);
562 kiocb
.ki_pos
= *ppos
;
564 kiocb
.ki_nbytes
= len
;
567 ret
= fn(&kiocb
, iov
, nr_segs
, kiocb
.ki_pos
);
568 if (ret
!= -EIOCBRETRY
)
570 wait_on_retry_sync_kiocb(&kiocb
);
573 if (ret
== -EIOCBQUEUED
)
574 ret
= wait_on_sync_kiocb(&kiocb
);
575 *ppos
= kiocb
.ki_pos
;
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
)
583 struct iovec
*vector
= iov
;
586 while (nr_segs
> 0) {
591 base
= vector
->iov_base
;
592 len
= vector
->iov_len
;
596 nr
= fn(filp
, base
, len
, ppos
);
611 /* A write operation does a read from user space and vice versa */
612 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
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
)
621 struct iovec
*iov
= fast_pointer
;
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...
634 * First get the "struct iovec" from user memory and
635 * verify all the pointers
637 if (nr_segs
> UIO_MAXIOV
) {
641 if (nr_segs
> fast_segs
) {
642 iov
= kmalloc(nr_segs
*sizeof(struct iovec
), GFP_KERNEL
);
648 if (copy_from_user(iov
, uvector
, nr_segs
*sizeof(*uvector
))) {
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
659 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
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
;
667 /* see if we we're about to use an invalid len or if
668 * it's about to overflow ssize_t */
673 if (unlikely(!access_ok(vrfy_dir(type
), buf
, len
))) {
677 if (len
> MAX_RW_COUNT
- ret
) {
678 len
= MAX_RW_COUNT
- ret
;
679 iov
[seg
].iov_len
= len
;
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
)
693 struct iovec iovstack
[UIO_FASTIOV
];
694 struct iovec
*iov
= iovstack
;
704 ret
= rw_copy_check_uvector(type
, uvector
, nr_segs
,
705 ARRAY_SIZE(iovstack
), iovstack
, &iov
);
710 ret
= rw_verify_area(type
, file
, pos
, tot_len
);
716 fn
= file
->f_op
->read
;
717 fnv
= file
->f_op
->aio_read
;
719 fn
= (io_fn_t
)file
->f_op
->write
;
720 fnv
= file
->f_op
->aio_write
;
724 ret
= do_sync_readv_writev(file
, iov
, nr_segs
, tot_len
,
727 ret
= do_loop_readv_writev(file
, iov
, nr_segs
, pos
, fn
);
732 if ((ret
+ (type
== READ
)) > 0) {
734 fsnotify_access(file
);
736 fsnotify_modify(file
);
741 ssize_t
vfs_readv(struct file
*file
, const struct iovec __user
*vec
,
742 unsigned long vlen
, loff_t
*pos
)
744 if (!(file
->f_mode
& FMODE_READ
))
746 if (!file
->f_op
|| (!file
->f_op
->aio_read
&& !file
->f_op
->read
))
749 return do_readv_writev(READ
, file
, vec
, vlen
, pos
);
752 EXPORT_SYMBOL(vfs_readv
);
754 ssize_t
vfs_writev(struct file
*file
, const struct iovec __user
*vec
,
755 unsigned long vlen
, loff_t
*pos
)
757 if (!(file
->f_mode
& FMODE_WRITE
))
759 if (!file
->f_op
|| (!file
->f_op
->aio_write
&& !file
->f_op
->write
))
762 return do_readv_writev(WRITE
, file
, vec
, vlen
, pos
);
765 EXPORT_SYMBOL(vfs_writev
);
767 SYSCALL_DEFINE3(readv
, unsigned long, fd
, const struct iovec __user
*, vec
,
771 ssize_t ret
= -EBADF
;
774 file
= fget_light(fd
, &fput_needed
);
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
);
783 add_rchar(current
, ret
);
788 SYSCALL_DEFINE3(writev
, unsigned long, fd
, const struct iovec __user
*, vec
,
792 ssize_t ret
= -EBADF
;
795 file
= fget_light(fd
, &fput_needed
);
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
);
804 add_wchar(current
, ret
);
809 static inline loff_t
pos_from_hilo(unsigned long high
, unsigned long low
)
811 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
812 return (((loff_t
)high
<< HALF_LONG_BITS
) << HALF_LONG_BITS
) | low
;
815 SYSCALL_DEFINE5(preadv
, unsigned long, fd
, const struct iovec __user
*, vec
,
816 unsigned long, vlen
, unsigned long, pos_l
, unsigned long, pos_h
)
818 loff_t pos
= pos_from_hilo(pos_h
, pos_l
);
820 ssize_t ret
= -EBADF
;
826 file
= fget_light(fd
, &fput_needed
);
829 if (file
->f_mode
& FMODE_PREAD
)
830 ret
= vfs_readv(file
, vec
, vlen
, &pos
);
831 fput_light(file
, fput_needed
);
835 add_rchar(current
, ret
);
840 SYSCALL_DEFINE5(pwritev
, unsigned long, fd
, const struct iovec __user
*, vec
,
841 unsigned long, vlen
, unsigned long, pos_l
, unsigned long, pos_h
)
843 loff_t pos
= pos_from_hilo(pos_h
, pos_l
);
845 ssize_t ret
= -EBADF
;
851 file
= fget_light(fd
, &fput_needed
);
854 if (file
->f_mode
& FMODE_PWRITE
)
855 ret
= vfs_writev(file
, vec
, vlen
, &pos
);
856 fput_light(file
, fput_needed
);
860 add_wchar(current
, ret
);
865 static ssize_t
do_sendfile(int out_fd
, int in_fd
, loff_t
*ppos
,
866 size_t count
, loff_t max
)
868 struct file
* in_file
, * out_file
;
869 struct inode
* in_inode
, * out_inode
;
872 int fput_needed_in
, fput_needed_out
, fl
;
875 * Get input file, and verify that it is ok..
878 in_file
= fget_light(in_fd
, &fput_needed_in
);
881 if (!(in_file
->f_mode
& FMODE_READ
))
885 ppos
= &in_file
->f_pos
;
887 if (!(in_file
->f_mode
& FMODE_PREAD
))
889 retval
= rw_verify_area(READ
, in_file
, ppos
, count
);
895 * Get output file, and verify that it is ok..
898 out_file
= fget_light(out_fd
, &fput_needed_out
);
901 if (!(out_file
->f_mode
& FMODE_WRITE
))
904 in_inode
= in_file
->f_path
.dentry
->d_inode
;
905 out_inode
= out_file
->f_path
.dentry
->d_inode
;
906 retval
= rw_verify_area(WRITE
, out_file
, &out_file
->f_pos
, count
);
912 max
= min(in_inode
->i_sb
->s_maxbytes
, out_inode
->i_sb
->s_maxbytes
);
915 if (unlikely(pos
+ count
> max
)) {
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.
930 if (in_file
->f_flags
& O_NONBLOCK
)
931 fl
= SPLICE_F_NONBLOCK
;
933 retval
= do_splice_direct(in_file
, ppos
, out_file
, count
, fl
);
936 add_rchar(current
, retval
);
937 add_wchar(current
, retval
);
946 fput_light(out_file
, fput_needed_out
);
948 fput_light(in_file
, fput_needed_in
);
953 SYSCALL_DEFINE4(sendfile
, int, out_fd
, int, in_fd
, off_t __user
*, offset
, size_t, count
)
960 if (unlikely(get_user(off
, offset
)))
963 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, MAX_NON_LFS
);
964 if (unlikely(put_user(pos
, offset
)))
969 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);
972 SYSCALL_DEFINE4(sendfile64
, int, out_fd
, int, in_fd
, loff_t __user
*, offset
, size_t, count
)
978 if (unlikely(copy_from_user(&pos
, offset
, sizeof(loff_t
))))
980 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, 0);
981 if (unlikely(put_user(pos
, offset
)))
986 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);