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>
19 #include <asm/uaccess.h>
20 #include <asm/unistd.h>
22 const struct file_operations generic_ro_fops
= {
23 .llseek
= generic_file_llseek
,
24 .read
= generic_file_read
,
25 .mmap
= generic_file_readonly_mmap
,
26 .sendfile
= generic_file_sendfile
,
29 EXPORT_SYMBOL(generic_ro_fops
);
31 loff_t
generic_file_llseek(struct file
*file
, loff_t offset
, int origin
)
34 struct inode
*inode
= file
->f_mapping
->host
;
36 mutex_lock(&inode
->i_mutex
);
39 offset
+= inode
->i_size
;
42 offset
+= file
->f_pos
;
45 if (offset
>=0 && offset
<=inode
->i_sb
->s_maxbytes
) {
46 if (offset
!= file
->f_pos
) {
52 mutex_unlock(&inode
->i_mutex
);
56 EXPORT_SYMBOL(generic_file_llseek
);
58 loff_t
remote_llseek(struct file
*file
, loff_t offset
, int origin
)
65 offset
+= i_size_read(file
->f_dentry
->d_inode
);
68 offset
+= file
->f_pos
;
71 if (offset
>=0 && offset
<=file
->f_dentry
->d_inode
->i_sb
->s_maxbytes
) {
72 if (offset
!= file
->f_pos
) {
81 EXPORT_SYMBOL(remote_llseek
);
83 loff_t
no_llseek(struct file
*file
, loff_t offset
, int origin
)
87 EXPORT_SYMBOL(no_llseek
);
89 loff_t
default_llseek(struct file
*file
, loff_t offset
, int origin
)
96 offset
+= i_size_read(file
->f_dentry
->d_inode
);
99 offset
+= file
->f_pos
;
103 if (offset
!= file
->f_pos
) {
104 file
->f_pos
= offset
;
112 EXPORT_SYMBOL(default_llseek
);
114 loff_t
vfs_llseek(struct file
*file
, loff_t offset
, int origin
)
116 loff_t (*fn
)(struct file
*, loff_t
, int);
119 if (file
->f_mode
& FMODE_LSEEK
) {
121 if (file
->f_op
&& file
->f_op
->llseek
)
122 fn
= file
->f_op
->llseek
;
124 return fn(file
, offset
, origin
);
126 EXPORT_SYMBOL(vfs_llseek
);
128 asmlinkage off_t
sys_lseek(unsigned int fd
, off_t offset
, unsigned int origin
)
135 file
= fget_light(fd
, &fput_needed
);
141 loff_t res
= vfs_llseek(file
, offset
, origin
);
143 if (res
!= (loff_t
)retval
)
144 retval
= -EOVERFLOW
; /* LFS: should only happen on 32 bit platforms */
146 fput_light(file
, fput_needed
);
151 #ifdef __ARCH_WANT_SYS_LLSEEK
152 asmlinkage
long sys_llseek(unsigned int fd
, unsigned long offset_high
,
153 unsigned long offset_low
, loff_t __user
* result
,
162 file
= fget_light(fd
, &fput_needed
);
170 offset
= vfs_llseek(file
, ((loff_t
) offset_high
<< 32) | offset_low
,
173 retval
= (int)offset
;
176 if (!copy_to_user(result
, &offset
, sizeof(offset
)))
180 fput_light(file
, fput_needed
);
187 * rw_verify_area doesn't like huge counts. We limit
188 * them to something that fits in "int" so that others
189 * won't have to do range checks all the time.
191 #define MAX_RW_COUNT (INT_MAX & PAGE_CACHE_MASK)
193 int rw_verify_area(int read_write
, struct file
*file
, loff_t
*ppos
, size_t count
)
198 if (unlikely((ssize_t
) count
< 0))
201 if (unlikely((pos
< 0) || (loff_t
) (pos
+ count
) < 0))
204 inode
= file
->f_dentry
->d_inode
;
205 if (inode
->i_flock
&& MANDATORY_LOCK(inode
)) {
206 int retval
= locks_mandatory_area(
207 read_write
== READ
? FLOCK_VERIFY_READ
: FLOCK_VERIFY_WRITE
,
208 inode
, file
, pos
, count
);
212 return count
> MAX_RW_COUNT
? MAX_RW_COUNT
: count
;
218 static void wait_on_retry_sync_kiocb(struct kiocb
*iocb
)
220 set_current_state(TASK_UNINTERRUPTIBLE
);
221 if (!kiocbIsKicked(iocb
))
224 kiocbClearKicked(iocb
);
225 __set_current_state(TASK_RUNNING
);
228 ssize_t
do_sync_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*ppos
)
233 init_sync_kiocb(&kiocb
, filp
);
234 kiocb
.ki_pos
= *ppos
;
235 while (-EIOCBRETRY
==
236 (ret
= filp
->f_op
->aio_read(&kiocb
, buf
, len
, kiocb
.ki_pos
)))
237 wait_on_retry_sync_kiocb(&kiocb
);
239 if (-EIOCBQUEUED
== ret
)
240 ret
= wait_on_sync_kiocb(&kiocb
);
241 *ppos
= kiocb
.ki_pos
;
245 EXPORT_SYMBOL(do_sync_read
);
247 ssize_t
vfs_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*pos
)
251 if (!(file
->f_mode
& FMODE_READ
))
253 if (!file
->f_op
|| (!file
->f_op
->read
&& !file
->f_op
->aio_read
))
255 if (unlikely(!access_ok(VERIFY_WRITE
, buf
, count
)))
258 ret
= rw_verify_area(READ
, file
, pos
, count
);
261 ret
= security_file_permission (file
, MAY_READ
);
263 if (file
->f_op
->read
)
264 ret
= file
->f_op
->read(file
, buf
, count
, pos
);
266 ret
= do_sync_read(file
, buf
, count
, pos
);
268 fsnotify_access(file
->f_dentry
);
269 current
->rchar
+= ret
;
278 EXPORT_SYMBOL(vfs_read
);
280 ssize_t
do_sync_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*ppos
)
285 init_sync_kiocb(&kiocb
, filp
);
286 kiocb
.ki_pos
= *ppos
;
287 while (-EIOCBRETRY
==
288 (ret
= filp
->f_op
->aio_write(&kiocb
, buf
, len
, kiocb
.ki_pos
)))
289 wait_on_retry_sync_kiocb(&kiocb
);
291 if (-EIOCBQUEUED
== ret
)
292 ret
= wait_on_sync_kiocb(&kiocb
);
293 *ppos
= kiocb
.ki_pos
;
297 EXPORT_SYMBOL(do_sync_write
);
299 ssize_t
vfs_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*pos
)
303 if (!(file
->f_mode
& FMODE_WRITE
))
305 if (!file
->f_op
|| (!file
->f_op
->write
&& !file
->f_op
->aio_write
))
307 if (unlikely(!access_ok(VERIFY_READ
, buf
, count
)))
310 ret
= rw_verify_area(WRITE
, file
, pos
, count
);
313 ret
= security_file_permission (file
, MAY_WRITE
);
315 if (file
->f_op
->write
)
316 ret
= file
->f_op
->write(file
, buf
, count
, pos
);
318 ret
= do_sync_write(file
, buf
, count
, pos
);
320 fsnotify_modify(file
->f_dentry
);
321 current
->wchar
+= ret
;
330 EXPORT_SYMBOL(vfs_write
);
332 static inline loff_t
file_pos_read(struct file
*file
)
337 static inline void file_pos_write(struct file
*file
, loff_t pos
)
342 asmlinkage ssize_t
sys_read(unsigned int fd
, char __user
* buf
, size_t count
)
345 ssize_t ret
= -EBADF
;
348 file
= fget_light(fd
, &fput_needed
);
350 loff_t pos
= file_pos_read(file
);
351 ret
= vfs_read(file
, buf
, count
, &pos
);
352 file_pos_write(file
, pos
);
353 fput_light(file
, fput_needed
);
358 EXPORT_SYMBOL_GPL(sys_read
);
360 asmlinkage ssize_t
sys_write(unsigned int fd
, const char __user
* buf
, size_t count
)
363 ssize_t ret
= -EBADF
;
366 file
= fget_light(fd
, &fput_needed
);
368 loff_t pos
= file_pos_read(file
);
369 ret
= vfs_write(file
, buf
, count
, &pos
);
370 file_pos_write(file
, pos
);
371 fput_light(file
, fput_needed
);
377 asmlinkage ssize_t
sys_pread64(unsigned int fd
, char __user
*buf
,
378 size_t count
, loff_t pos
)
381 ssize_t ret
= -EBADF
;
387 file
= fget_light(fd
, &fput_needed
);
390 if (file
->f_mode
& FMODE_PREAD
)
391 ret
= vfs_read(file
, buf
, count
, &pos
);
392 fput_light(file
, fput_needed
);
398 asmlinkage ssize_t
sys_pwrite64(unsigned int fd
, const char __user
*buf
,
399 size_t count
, loff_t pos
)
402 ssize_t ret
= -EBADF
;
408 file
= fget_light(fd
, &fput_needed
);
411 if (file
->f_mode
& FMODE_PWRITE
)
412 ret
= vfs_write(file
, buf
, count
, &pos
);
413 fput_light(file
, fput_needed
);
420 * Reduce an iovec's length in-place. Return the resulting number of segments
422 unsigned long iov_shorten(struct iovec
*iov
, unsigned long nr_segs
, size_t to
)
424 unsigned long seg
= 0;
427 while (seg
< nr_segs
) {
429 if (len
+ iov
->iov_len
>= to
) {
430 iov
->iov_len
= to
- len
;
439 EXPORT_SYMBOL(iov_shorten
);
441 /* A write operation does a read from user space and vice versa */
442 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
444 static ssize_t
do_readv_writev(int type
, struct file
*file
,
445 const struct iovec __user
* uvector
,
446 unsigned long nr_segs
, loff_t
*pos
)
448 typedef ssize_t (*io_fn_t
)(struct file
*, char __user
*, size_t, loff_t
*);
449 typedef ssize_t (*iov_fn_t
)(struct file
*, const struct iovec
*, unsigned long, loff_t
*);
452 struct iovec iovstack
[UIO_FASTIOV
];
453 struct iovec
*iov
=iovstack
, *vector
;
460 * SuS says "The readv() function *may* fail if the iovcnt argument
461 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
462 * traditionally returned zero for zero segments, so...
469 * First get the "struct iovec" from user memory and
470 * verify all the pointers
473 if (nr_segs
> UIO_MAXIOV
)
477 if (nr_segs
> UIO_FASTIOV
) {
479 iov
= kmalloc(nr_segs
*sizeof(struct iovec
), GFP_KERNEL
);
484 if (copy_from_user(iov
, uvector
, nr_segs
*sizeof(*uvector
)))
488 * Single unix specification:
489 * We should -EINVAL if an element length is not >= 0 and fitting an
490 * ssize_t. The total length is fitting an ssize_t
492 * Be careful here because iov_len is a size_t not an ssize_t
496 for (seg
= 0; seg
< nr_segs
; seg
++) {
497 void __user
*buf
= iov
[seg
].iov_base
;
498 ssize_t len
= (ssize_t
)iov
[seg
].iov_len
;
500 if (len
< 0) /* size_t not fitting an ssize_t .. */
502 if (unlikely(!access_ok(vrfy_dir(type
), buf
, len
)))
505 if ((ssize_t
)tot_len
< 0) /* maths overflow on the ssize_t */
513 ret
= rw_verify_area(type
, file
, pos
, tot_len
);
516 ret
= security_file_permission(file
, type
== READ
? MAY_READ
: MAY_WRITE
);
522 fn
= file
->f_op
->read
;
523 fnv
= file
->f_op
->readv
;
525 fn
= (io_fn_t
)file
->f_op
->write
;
526 fnv
= file
->f_op
->writev
;
529 ret
= fnv(file
, iov
, nr_segs
, pos
);
533 /* Do it by hand, with file-ops */
536 while (nr_segs
> 0) {
541 base
= vector
->iov_base
;
542 len
= vector
->iov_len
;
546 nr
= fn(file
, base
, len
, pos
);
559 if ((ret
+ (type
== READ
)) > 0) {
561 fsnotify_access(file
->f_dentry
);
563 fsnotify_modify(file
->f_dentry
);
571 ssize_t
vfs_readv(struct file
*file
, const struct iovec __user
*vec
,
572 unsigned long vlen
, loff_t
*pos
)
574 if (!(file
->f_mode
& FMODE_READ
))
576 if (!file
->f_op
|| (!file
->f_op
->readv
&& !file
->f_op
->read
))
579 return do_readv_writev(READ
, file
, vec
, vlen
, pos
);
582 EXPORT_SYMBOL(vfs_readv
);
584 ssize_t
vfs_writev(struct file
*file
, const struct iovec __user
*vec
,
585 unsigned long vlen
, loff_t
*pos
)
587 if (!(file
->f_mode
& FMODE_WRITE
))
589 if (!file
->f_op
|| (!file
->f_op
->writev
&& !file
->f_op
->write
))
592 return do_readv_writev(WRITE
, file
, vec
, vlen
, pos
);
595 EXPORT_SYMBOL(vfs_writev
);
598 sys_readv(unsigned long fd
, const struct iovec __user
*vec
, unsigned long vlen
)
601 ssize_t ret
= -EBADF
;
604 file
= fget_light(fd
, &fput_needed
);
606 loff_t pos
= file_pos_read(file
);
607 ret
= vfs_readv(file
, vec
, vlen
, &pos
);
608 file_pos_write(file
, pos
);
609 fput_light(file
, fput_needed
);
613 current
->rchar
+= ret
;
619 sys_writev(unsigned long fd
, const struct iovec __user
*vec
, unsigned long vlen
)
622 ssize_t ret
= -EBADF
;
625 file
= fget_light(fd
, &fput_needed
);
627 loff_t pos
= file_pos_read(file
);
628 ret
= vfs_writev(file
, vec
, vlen
, &pos
);
629 file_pos_write(file
, pos
);
630 fput_light(file
, fput_needed
);
634 current
->wchar
+= ret
;
639 static ssize_t
do_sendfile(int out_fd
, int in_fd
, loff_t
*ppos
,
640 size_t count
, loff_t max
)
642 struct file
* in_file
, * out_file
;
643 struct inode
* in_inode
, * out_inode
;
646 int fput_needed_in
, fput_needed_out
;
649 * Get input file, and verify that it is ok..
652 in_file
= fget_light(in_fd
, &fput_needed_in
);
655 if (!(in_file
->f_mode
& FMODE_READ
))
658 in_inode
= in_file
->f_dentry
->d_inode
;
661 if (!in_file
->f_op
|| !in_file
->f_op
->sendfile
)
665 ppos
= &in_file
->f_pos
;
667 if (!(in_file
->f_mode
& FMODE_PREAD
))
669 retval
= rw_verify_area(READ
, in_file
, ppos
, count
);
674 retval
= security_file_permission (in_file
, MAY_READ
);
679 * Get output file, and verify that it is ok..
682 out_file
= fget_light(out_fd
, &fput_needed_out
);
685 if (!(out_file
->f_mode
& FMODE_WRITE
))
688 if (!out_file
->f_op
|| !out_file
->f_op
->sendpage
)
690 out_inode
= out_file
->f_dentry
->d_inode
;
691 retval
= rw_verify_area(WRITE
, out_file
, &out_file
->f_pos
, count
);
696 retval
= security_file_permission (out_file
, MAY_WRITE
);
701 max
= min(in_inode
->i_sb
->s_maxbytes
, out_inode
->i_sb
->s_maxbytes
);
705 if (unlikely(pos
< 0))
707 if (unlikely(pos
+ count
> max
)) {
714 retval
= in_file
->f_op
->sendfile(in_file
, ppos
, count
, file_send_actor
, out_file
);
717 current
->rchar
+= retval
;
718 current
->wchar
+= retval
;
727 fput_light(out_file
, fput_needed_out
);
729 fput_light(in_file
, fput_needed_in
);
734 asmlinkage ssize_t
sys_sendfile(int out_fd
, int in_fd
, off_t __user
*offset
, size_t count
)
741 if (unlikely(get_user(off
, offset
)))
744 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, MAX_NON_LFS
);
745 if (unlikely(put_user(pos
, offset
)))
750 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);
753 asmlinkage ssize_t
sys_sendfile64(int out_fd
, int in_fd
, loff_t __user
*offset
, size_t count
)
759 if (unlikely(copy_from_user(&pos
, offset
, sizeof(loff_t
))))
761 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, 0);
762 if (unlikely(put_user(pos
, offset
)))
767 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);