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/dnotify.h>
14 #include <linux/security.h>
15 #include <linux/module.h>
16 #include <linux/syscalls.h>
18 #include <asm/uaccess.h>
19 #include <asm/unistd.h>
21 struct file_operations generic_ro_fops
= {
22 .llseek
= generic_file_llseek
,
23 .read
= generic_file_read
,
24 .mmap
= generic_file_readonly_mmap
,
25 .sendfile
= generic_file_sendfile
,
28 EXPORT_SYMBOL(generic_ro_fops
);
30 loff_t
generic_file_llseek(struct file
*file
, loff_t offset
, int origin
)
33 struct inode
*inode
= file
->f_mapping
->host
;
38 offset
+= inode
->i_size
;
41 offset
+= file
->f_pos
;
44 if (offset
>=0 && offset
<=inode
->i_sb
->s_maxbytes
) {
45 if (offset
!= file
->f_pos
) {
55 EXPORT_SYMBOL(generic_file_llseek
);
57 loff_t
remote_llseek(struct file
*file
, loff_t offset
, int origin
)
64 offset
+= i_size_read(file
->f_dentry
->d_inode
);
67 offset
+= file
->f_pos
;
70 if (offset
>=0 && offset
<=file
->f_dentry
->d_inode
->i_sb
->s_maxbytes
) {
71 if (offset
!= file
->f_pos
) {
80 EXPORT_SYMBOL(remote_llseek
);
82 loff_t
no_llseek(struct file
*file
, loff_t offset
, int origin
)
86 EXPORT_SYMBOL(no_llseek
);
88 loff_t
default_llseek(struct file
*file
, loff_t offset
, int origin
)
95 offset
+= i_size_read(file
->f_dentry
->d_inode
);
98 offset
+= file
->f_pos
;
102 if (offset
!= file
->f_pos
) {
103 file
->f_pos
= offset
;
111 EXPORT_SYMBOL(default_llseek
);
113 loff_t
vfs_llseek(struct file
*file
, loff_t offset
, int origin
)
115 loff_t (*fn
)(struct file
*, loff_t
, int);
118 if (file
->f_mode
& FMODE_LSEEK
) {
120 if (file
->f_op
&& file
->f_op
->llseek
)
121 fn
= file
->f_op
->llseek
;
123 return fn(file
, offset
, origin
);
125 EXPORT_SYMBOL(vfs_llseek
);
127 asmlinkage off_t
sys_lseek(unsigned int fd
, off_t offset
, unsigned int origin
)
134 file
= fget_light(fd
, &fput_needed
);
140 loff_t res
= vfs_llseek(file
, offset
, origin
);
142 if (res
!= (loff_t
)retval
)
143 retval
= -EOVERFLOW
; /* LFS: should only happen on 32 bit platforms */
145 fput_light(file
, fput_needed
);
150 #ifdef __ARCH_WANT_SYS_LLSEEK
151 asmlinkage
long sys_llseek(unsigned int fd
, unsigned long offset_high
,
152 unsigned long offset_low
, loff_t __user
* result
,
161 file
= fget_light(fd
, &fput_needed
);
169 offset
= vfs_llseek(file
, ((loff_t
) offset_high
<< 32) | offset_low
,
172 retval
= (int)offset
;
175 if (!copy_to_user(result
, &offset
, sizeof(offset
)))
179 fput_light(file
, fput_needed
);
186 int rw_verify_area(int read_write
, struct file
*file
, loff_t
*ppos
, size_t count
)
191 if (unlikely(count
> file
->f_maxcount
))
194 if (unlikely((pos
< 0) || (loff_t
) (pos
+ count
) < 0))
197 inode
= file
->f_dentry
->d_inode
;
198 if (inode
->i_flock
&& MANDATORY_LOCK(inode
))
199 return locks_mandatory_area(read_write
== READ
? FLOCK_VERIFY_READ
: FLOCK_VERIFY_WRITE
, inode
, file
, pos
, count
);
206 ssize_t
do_sync_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*ppos
)
211 init_sync_kiocb(&kiocb
, filp
);
212 kiocb
.ki_pos
= *ppos
;
213 ret
= filp
->f_op
->aio_read(&kiocb
, buf
, len
, kiocb
.ki_pos
);
214 if (-EIOCBQUEUED
== ret
)
215 ret
= wait_on_sync_kiocb(&kiocb
);
216 *ppos
= kiocb
.ki_pos
;
220 EXPORT_SYMBOL(do_sync_read
);
222 ssize_t
vfs_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*pos
)
226 if (!(file
->f_mode
& FMODE_READ
))
228 if (!file
->f_op
|| (!file
->f_op
->read
&& !file
->f_op
->aio_read
))
230 if (unlikely(!access_ok(VERIFY_WRITE
, buf
, count
)))
233 ret
= rw_verify_area(READ
, file
, pos
, count
);
235 ret
= security_file_permission (file
, MAY_READ
);
237 if (file
->f_op
->read
)
238 ret
= file
->f_op
->read(file
, buf
, count
, pos
);
240 ret
= do_sync_read(file
, buf
, count
, pos
);
242 dnotify_parent(file
->f_dentry
, DN_ACCESS
);
243 current
->rchar
+= ret
;
252 EXPORT_SYMBOL(vfs_read
);
254 ssize_t
do_sync_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*ppos
)
259 init_sync_kiocb(&kiocb
, filp
);
260 kiocb
.ki_pos
= *ppos
;
261 ret
= filp
->f_op
->aio_write(&kiocb
, buf
, len
, kiocb
.ki_pos
);
262 if (-EIOCBQUEUED
== ret
)
263 ret
= wait_on_sync_kiocb(&kiocb
);
264 *ppos
= kiocb
.ki_pos
;
268 EXPORT_SYMBOL(do_sync_write
);
270 ssize_t
vfs_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*pos
)
274 if (!(file
->f_mode
& FMODE_WRITE
))
276 if (!file
->f_op
|| (!file
->f_op
->write
&& !file
->f_op
->aio_write
))
278 if (unlikely(!access_ok(VERIFY_READ
, buf
, count
)))
281 ret
= rw_verify_area(WRITE
, file
, pos
, count
);
283 ret
= security_file_permission (file
, MAY_WRITE
);
285 if (file
->f_op
->write
)
286 ret
= file
->f_op
->write(file
, buf
, count
, pos
);
288 ret
= do_sync_write(file
, buf
, count
, pos
);
290 dnotify_parent(file
->f_dentry
, DN_MODIFY
);
291 current
->wchar
+= ret
;
300 EXPORT_SYMBOL(vfs_write
);
302 static inline loff_t
file_pos_read(struct file
*file
)
307 static inline void file_pos_write(struct file
*file
, loff_t pos
)
312 asmlinkage ssize_t
sys_read(unsigned int fd
, char __user
* buf
, size_t count
)
315 ssize_t ret
= -EBADF
;
318 file
= fget_light(fd
, &fput_needed
);
320 loff_t pos
= file_pos_read(file
);
321 ret
= vfs_read(file
, buf
, count
, &pos
);
322 file_pos_write(file
, pos
);
323 fput_light(file
, fput_needed
);
328 EXPORT_SYMBOL_GPL(sys_read
);
330 asmlinkage ssize_t
sys_write(unsigned int fd
, const char __user
* buf
, size_t count
)
333 ssize_t ret
= -EBADF
;
336 file
= fget_light(fd
, &fput_needed
);
338 loff_t pos
= file_pos_read(file
);
339 ret
= vfs_write(file
, buf
, count
, &pos
);
340 file_pos_write(file
, pos
);
341 fput_light(file
, fput_needed
);
347 asmlinkage ssize_t
sys_pread64(unsigned int fd
, char __user
*buf
,
348 size_t count
, loff_t pos
)
351 ssize_t ret
= -EBADF
;
357 file
= fget_light(fd
, &fput_needed
);
360 if (file
->f_mode
& FMODE_PREAD
)
361 ret
= vfs_read(file
, buf
, count
, &pos
);
362 fput_light(file
, fput_needed
);
368 asmlinkage ssize_t
sys_pwrite64(unsigned int fd
, const char __user
*buf
,
369 size_t count
, loff_t pos
)
372 ssize_t ret
= -EBADF
;
378 file
= fget_light(fd
, &fput_needed
);
381 if (file
->f_mode
& FMODE_PWRITE
)
382 ret
= vfs_write(file
, buf
, count
, &pos
);
383 fput_light(file
, fput_needed
);
390 * Reduce an iovec's length in-place. Return the resulting number of segments
392 unsigned long iov_shorten(struct iovec
*iov
, unsigned long nr_segs
, size_t to
)
394 unsigned long seg
= 0;
397 while (seg
< nr_segs
) {
399 if (len
+ iov
->iov_len
>= to
) {
400 iov
->iov_len
= to
- len
;
409 EXPORT_SYMBOL(iov_shorten
);
411 /* A write operation does a read from user space and vice versa */
412 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
414 static ssize_t
do_readv_writev(int type
, struct file
*file
,
415 const struct iovec __user
* uvector
,
416 unsigned long nr_segs
, loff_t
*pos
)
418 typedef ssize_t (*io_fn_t
)(struct file
*, char __user
*, size_t, loff_t
*);
419 typedef ssize_t (*iov_fn_t
)(struct file
*, const struct iovec
*, unsigned long, loff_t
*);
422 struct iovec iovstack
[UIO_FASTIOV
];
423 struct iovec
*iov
=iovstack
, *vector
;
430 * SuS says "The readv() function *may* fail if the iovcnt argument
431 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
432 * traditionally returned zero for zero segments, so...
439 * First get the "struct iovec" from user memory and
440 * verify all the pointers
443 if ((nr_segs
> UIO_MAXIOV
) || (nr_segs
<= 0))
447 if (nr_segs
> UIO_FASTIOV
) {
449 iov
= kmalloc(nr_segs
*sizeof(struct iovec
), GFP_KERNEL
);
454 if (copy_from_user(iov
, uvector
, nr_segs
*sizeof(*uvector
)))
458 * Single unix specification:
459 * We should -EINVAL if an element length is not >= 0 and fitting an
460 * ssize_t. The total length is fitting an ssize_t
462 * Be careful here because iov_len is a size_t not an ssize_t
466 for (seg
= 0; seg
< nr_segs
; seg
++) {
467 void __user
*buf
= iov
[seg
].iov_base
;
468 ssize_t len
= (ssize_t
)iov
[seg
].iov_len
;
470 if (len
< 0) /* size_t not fitting an ssize_t .. */
472 if (unlikely(!access_ok(vrfy_dir(type
), buf
, len
)))
475 if ((ssize_t
)tot_len
< 0) /* maths overflow on the ssize_t */
483 ret
= rw_verify_area(type
, file
, pos
, tot_len
);
489 fn
= file
->f_op
->read
;
490 fnv
= file
->f_op
->readv
;
492 fn
= (io_fn_t
)file
->f_op
->write
;
493 fnv
= file
->f_op
->writev
;
496 ret
= fnv(file
, iov
, nr_segs
, pos
);
500 /* Do it by hand, with file-ops */
503 while (nr_segs
> 0) {
508 base
= vector
->iov_base
;
509 len
= vector
->iov_len
;
513 nr
= fn(file
, base
, len
, pos
);
526 if ((ret
+ (type
== READ
)) > 0)
527 dnotify_parent(file
->f_dentry
,
528 (type
== READ
) ? DN_ACCESS
: DN_MODIFY
);
535 ssize_t
vfs_readv(struct file
*file
, const struct iovec __user
*vec
,
536 unsigned long vlen
, loff_t
*pos
)
538 if (!(file
->f_mode
& FMODE_READ
))
540 if (!file
->f_op
|| (!file
->f_op
->readv
&& !file
->f_op
->read
))
543 return do_readv_writev(READ
, file
, vec
, vlen
, pos
);
546 EXPORT_SYMBOL(vfs_readv
);
548 ssize_t
vfs_writev(struct file
*file
, const struct iovec __user
*vec
,
549 unsigned long vlen
, loff_t
*pos
)
551 if (!(file
->f_mode
& FMODE_WRITE
))
553 if (!file
->f_op
|| (!file
->f_op
->writev
&& !file
->f_op
->write
))
556 return do_readv_writev(WRITE
, file
, vec
, vlen
, pos
);
559 EXPORT_SYMBOL(vfs_writev
);
562 sys_readv(unsigned long fd
, const struct iovec __user
*vec
, unsigned long vlen
)
565 ssize_t ret
= -EBADF
;
568 file
= fget_light(fd
, &fput_needed
);
570 loff_t pos
= file_pos_read(file
);
571 ret
= vfs_readv(file
, vec
, vlen
, &pos
);
572 file_pos_write(file
, pos
);
573 fput_light(file
, fput_needed
);
577 current
->rchar
+= ret
;
583 sys_writev(unsigned long fd
, const struct iovec __user
*vec
, unsigned long vlen
)
586 ssize_t ret
= -EBADF
;
589 file
= fget_light(fd
, &fput_needed
);
591 loff_t pos
= file_pos_read(file
);
592 ret
= vfs_writev(file
, vec
, vlen
, &pos
);
593 file_pos_write(file
, pos
);
594 fput_light(file
, fput_needed
);
598 current
->wchar
+= ret
;
603 static ssize_t
do_sendfile(int out_fd
, int in_fd
, loff_t
*ppos
,
604 size_t count
, loff_t max
)
606 struct file
* in_file
, * out_file
;
607 struct inode
* in_inode
, * out_inode
;
610 int fput_needed_in
, fput_needed_out
;
613 * Get input file, and verify that it is ok..
616 in_file
= fget_light(in_fd
, &fput_needed_in
);
619 if (!(in_file
->f_mode
& FMODE_READ
))
622 in_inode
= in_file
->f_dentry
->d_inode
;
625 if (!in_file
->f_op
|| !in_file
->f_op
->sendfile
)
629 ppos
= &in_file
->f_pos
;
631 if (!(in_file
->f_mode
& FMODE_PREAD
))
633 retval
= rw_verify_area(READ
, in_file
, ppos
, count
);
637 retval
= security_file_permission (in_file
, MAY_READ
);
642 * Get output file, and verify that it is ok..
645 out_file
= fget_light(out_fd
, &fput_needed_out
);
648 if (!(out_file
->f_mode
& FMODE_WRITE
))
651 if (!out_file
->f_op
|| !out_file
->f_op
->sendpage
)
653 out_inode
= out_file
->f_dentry
->d_inode
;
654 retval
= rw_verify_area(WRITE
, out_file
, &out_file
->f_pos
, count
);
658 retval
= security_file_permission (out_file
, MAY_WRITE
);
663 max
= min(in_inode
->i_sb
->s_maxbytes
, out_inode
->i_sb
->s_maxbytes
);
667 if (unlikely(pos
< 0))
669 if (unlikely(pos
+ count
> max
)) {
676 retval
= in_file
->f_op
->sendfile(in_file
, ppos
, count
, file_send_actor
, out_file
);
679 current
->rchar
+= retval
;
680 current
->wchar
+= retval
;
689 fput_light(out_file
, fput_needed_out
);
691 fput_light(in_file
, fput_needed_in
);
696 asmlinkage ssize_t
sys_sendfile(int out_fd
, int in_fd
, off_t __user
*offset
, size_t count
)
703 if (unlikely(get_user(off
, offset
)))
706 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, MAX_NON_LFS
);
707 if (unlikely(put_user(pos
, offset
)))
712 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);
715 asmlinkage ssize_t
sys_sendfile64(int out_fd
, int in_fd
, loff_t __user
*offset
, size_t count
)
721 if (unlikely(copy_from_user(&pos
, offset
, sizeof(loff_t
))))
723 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, 0);
724 if (unlikely(put_user(pos
, offset
)))
729 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);