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>
17 #include <asm/uaccess.h>
18 #include <asm/unistd.h>
20 struct file_operations generic_ro_fops
= {
21 .llseek
= generic_file_llseek
,
22 .read
= generic_file_read
,
23 .mmap
= generic_file_readonly_mmap
,
24 .sendfile
= generic_file_sendfile
,
27 EXPORT_SYMBOL(generic_ro_fops
);
29 loff_t
generic_file_llseek(struct file
*file
, loff_t offset
, int origin
)
32 struct inode
*inode
= file
->f_mapping
->host
;
37 offset
+= inode
->i_size
;
40 offset
+= file
->f_pos
;
43 if (offset
>=0 && offset
<=inode
->i_sb
->s_maxbytes
) {
44 if (offset
!= file
->f_pos
) {
54 EXPORT_SYMBOL(generic_file_llseek
);
56 loff_t
remote_llseek(struct file
*file
, loff_t offset
, int origin
)
63 offset
+= i_size_read(file
->f_dentry
->d_inode
);
66 offset
+= file
->f_pos
;
69 if (offset
>=0 && offset
<=file
->f_dentry
->d_inode
->i_sb
->s_maxbytes
) {
70 if (offset
!= file
->f_pos
) {
79 EXPORT_SYMBOL(remote_llseek
);
81 loff_t
no_llseek(struct file
*file
, loff_t offset
, int origin
)
85 EXPORT_SYMBOL(no_llseek
);
87 loff_t
default_llseek(struct file
*file
, loff_t offset
, int origin
)
94 offset
+= i_size_read(file
->f_dentry
->d_inode
);
97 offset
+= file
->f_pos
;
101 if (offset
!= file
->f_pos
) {
102 file
->f_pos
= offset
;
110 EXPORT_SYMBOL(default_llseek
);
112 loff_t
vfs_llseek(struct file
*file
, loff_t offset
, int origin
)
114 loff_t (*fn
)(struct file
*, loff_t
, int);
117 if (file
->f_mode
& FMODE_LSEEK
) {
119 if (file
->f_op
&& file
->f_op
->llseek
)
120 fn
= file
->f_op
->llseek
;
122 return fn(file
, offset
, origin
);
124 EXPORT_SYMBOL(vfs_llseek
);
126 asmlinkage off_t
sys_lseek(unsigned int fd
, off_t offset
, unsigned int origin
)
133 file
= fget_light(fd
, &fput_needed
);
139 loff_t res
= vfs_llseek(file
, offset
, origin
);
141 if (res
!= (loff_t
)retval
)
142 retval
= -EOVERFLOW
; /* LFS: should only happen on 32 bit platforms */
144 fput_light(file
, fput_needed
);
148 EXPORT_SYMBOL_GPL(sys_lseek
);
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
);
185 ssize_t
do_sync_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*ppos
)
190 init_sync_kiocb(&kiocb
, filp
);
191 kiocb
.ki_pos
= *ppos
;
192 ret
= filp
->f_op
->aio_read(&kiocb
, buf
, len
, kiocb
.ki_pos
);
193 if (-EIOCBQUEUED
== ret
)
194 ret
= wait_on_sync_kiocb(&kiocb
);
195 *ppos
= kiocb
.ki_pos
;
199 EXPORT_SYMBOL(do_sync_read
);
201 ssize_t
vfs_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*pos
)
203 struct inode
*inode
= file
->f_dentry
->d_inode
;
206 if (!(file
->f_mode
& FMODE_READ
))
208 if (!file
->f_op
|| (!file
->f_op
->read
&& !file
->f_op
->aio_read
))
211 ret
= locks_verify_area(FLOCK_VERIFY_READ
, inode
, file
, *pos
, count
);
213 ret
= security_file_permission (file
, MAY_READ
);
215 if (file
->f_op
->read
)
216 ret
= file
->f_op
->read(file
, buf
, count
, pos
);
218 ret
= do_sync_read(file
, buf
, count
, pos
);
220 dnotify_parent(file
->f_dentry
, DN_ACCESS
);
227 EXPORT_SYMBOL(vfs_read
);
229 ssize_t
do_sync_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*ppos
)
234 init_sync_kiocb(&kiocb
, filp
);
235 kiocb
.ki_pos
= *ppos
;
236 ret
= filp
->f_op
->aio_write(&kiocb
, buf
, len
, kiocb
.ki_pos
);
237 if (-EIOCBQUEUED
== ret
)
238 ret
= wait_on_sync_kiocb(&kiocb
);
239 *ppos
= kiocb
.ki_pos
;
243 EXPORT_SYMBOL(do_sync_write
);
245 ssize_t
vfs_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*pos
)
247 struct inode
*inode
= file
->f_dentry
->d_inode
;
250 if (!(file
->f_mode
& FMODE_WRITE
))
252 if (!file
->f_op
|| (!file
->f_op
->write
&& !file
->f_op
->aio_write
))
255 ret
= locks_verify_area(FLOCK_VERIFY_WRITE
, inode
, file
, *pos
, count
);
257 ret
= security_file_permission (file
, MAY_WRITE
);
259 if (file
->f_op
->write
)
260 ret
= file
->f_op
->write(file
, buf
, count
, pos
);
262 ret
= do_sync_write(file
, buf
, count
, pos
);
264 dnotify_parent(file
->f_dentry
, DN_MODIFY
);
271 EXPORT_SYMBOL(vfs_write
);
273 static inline loff_t
file_pos_read(struct file
*file
)
278 static inline void file_pos_write(struct file
*file
, loff_t pos
)
283 asmlinkage ssize_t
sys_read(unsigned int fd
, char __user
* buf
, size_t count
)
286 ssize_t ret
= -EBADF
;
289 file
= fget_light(fd
, &fput_needed
);
291 loff_t pos
= file_pos_read(file
);
292 ret
= vfs_read(file
, buf
, count
, &pos
);
293 file_pos_write(file
, pos
);
294 fput_light(file
, fput_needed
);
299 EXPORT_SYMBOL_GPL(sys_read
);
301 asmlinkage ssize_t
sys_write(unsigned int fd
, const char __user
* buf
, size_t count
)
304 ssize_t ret
= -EBADF
;
307 file
= fget_light(fd
, &fput_needed
);
309 loff_t pos
= file_pos_read(file
);
310 ret
= vfs_write(file
, buf
, count
, &pos
);
311 file_pos_write(file
, pos
);
312 fput_light(file
, fput_needed
);
318 asmlinkage ssize_t
sys_pread64(unsigned int fd
, char __user
*buf
,
319 size_t count
, loff_t pos
)
322 ssize_t ret
= -EBADF
;
328 file
= fget_light(fd
, &fput_needed
);
331 if (file
->f_mode
& FMODE_PREAD
)
332 ret
= vfs_read(file
, buf
, count
, &pos
);
333 fput_light(file
, fput_needed
);
339 asmlinkage ssize_t
sys_pwrite64(unsigned int fd
, const char __user
*buf
,
340 size_t count
, loff_t pos
)
343 ssize_t ret
= -EBADF
;
349 file
= fget_light(fd
, &fput_needed
);
352 if (file
->f_mode
& FMODE_PWRITE
)
353 ret
= vfs_write(file
, buf
, count
, &pos
);
354 fput_light(file
, fput_needed
);
361 * Reduce an iovec's length in-place. Return the resulting number of segments
363 unsigned long iov_shorten(struct iovec
*iov
, unsigned long nr_segs
, size_t to
)
365 unsigned long seg
= 0;
368 while (seg
< nr_segs
) {
370 if (len
+ iov
->iov_len
>= to
) {
371 iov
->iov_len
= to
- len
;
380 EXPORT_SYMBOL(iov_shorten
);
382 static ssize_t
do_readv_writev(int type
, struct file
*file
,
383 const struct iovec __user
* uvector
,
384 unsigned long nr_segs
, loff_t
*pos
)
386 typedef ssize_t (*io_fn_t
)(struct file
*, char __user
*, size_t, loff_t
*);
387 typedef ssize_t (*iov_fn_t
)(struct file
*, const struct iovec
*, unsigned long, loff_t
*);
390 struct iovec iovstack
[UIO_FASTIOV
];
391 struct iovec
*iov
=iovstack
, *vector
;
399 * SuS says "The readv() function *may* fail if the iovcnt argument
400 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
401 * traditionally returned zero for zero segments, so...
408 * First get the "struct iovec" from user memory and
409 * verify all the pointers
412 if ((nr_segs
> UIO_MAXIOV
) || (nr_segs
<= 0))
416 if (nr_segs
> UIO_FASTIOV
) {
418 iov
= kmalloc(nr_segs
*sizeof(struct iovec
), GFP_KERNEL
);
423 if (copy_from_user(iov
, uvector
, nr_segs
*sizeof(*uvector
)))
427 * Single unix specification:
428 * We should -EINVAL if an element length is not >= 0 and fitting an
429 * ssize_t. The total length is fitting an ssize_t
431 * Be careful here because iov_len is a size_t not an ssize_t
435 for (seg
= 0; seg
< nr_segs
; seg
++) {
436 ssize_t len
= (ssize_t
)iov
[seg
].iov_len
;
438 if (len
< 0) /* size_t not fitting an ssize_t .. */
441 if ((ssize_t
)tot_len
< 0) /* maths overflow on the ssize_t */
449 inode
= file
->f_dentry
->d_inode
;
450 /* VERIFY_WRITE actually means a read, as we write to user space */
451 ret
= locks_verify_area((type
== READ
452 ? FLOCK_VERIFY_READ
: FLOCK_VERIFY_WRITE
),
453 inode
, file
, *pos
, tot_len
);
459 fn
= file
->f_op
->read
;
460 fnv
= file
->f_op
->readv
;
462 fn
= (io_fn_t
)file
->f_op
->write
;
463 fnv
= file
->f_op
->writev
;
466 ret
= fnv(file
, iov
, nr_segs
, pos
);
470 /* Do it by hand, with file-ops */
473 while (nr_segs
> 0) {
478 base
= vector
->iov_base
;
479 len
= vector
->iov_len
;
483 nr
= fn(file
, base
, len
, pos
);
496 if ((ret
+ (type
== READ
)) > 0)
497 dnotify_parent(file
->f_dentry
,
498 (type
== READ
) ? DN_ACCESS
: DN_MODIFY
);
502 ssize_t
vfs_readv(struct file
*file
, const struct iovec __user
*vec
,
503 unsigned long vlen
, loff_t
*pos
)
505 if (!(file
->f_mode
& FMODE_READ
))
507 if (!file
->f_op
|| (!file
->f_op
->readv
&& !file
->f_op
->read
))
510 return do_readv_writev(READ
, file
, vec
, vlen
, pos
);
513 EXPORT_SYMBOL(vfs_readv
);
515 ssize_t
vfs_writev(struct file
*file
, const struct iovec __user
*vec
,
516 unsigned long vlen
, loff_t
*pos
)
518 if (!(file
->f_mode
& FMODE_WRITE
))
520 if (!file
->f_op
|| (!file
->f_op
->writev
&& !file
->f_op
->write
))
523 return do_readv_writev(WRITE
, file
, vec
, vlen
, pos
);
526 EXPORT_SYMBOL(vfs_writev
);
529 sys_readv(unsigned long fd
, const struct iovec __user
*vec
, unsigned long vlen
)
532 ssize_t ret
= -EBADF
;
535 file
= fget_light(fd
, &fput_needed
);
537 loff_t pos
= file_pos_read(file
);
538 ret
= vfs_readv(file
, vec
, vlen
, &pos
);
539 file_pos_write(file
, pos
);
540 fput_light(file
, fput_needed
);
547 sys_writev(unsigned long fd
, const struct iovec __user
*vec
, unsigned long vlen
)
550 ssize_t ret
= -EBADF
;
553 file
= fget_light(fd
, &fput_needed
);
555 loff_t pos
= file_pos_read(file
);
556 ret
= vfs_writev(file
, vec
, vlen
, &pos
);
557 file_pos_write(file
, pos
);
558 fput_light(file
, fput_needed
);
564 static ssize_t
do_sendfile(int out_fd
, int in_fd
, loff_t
*ppos
,
565 size_t count
, loff_t max
)
567 struct file
* in_file
, * out_file
;
568 struct inode
* in_inode
, * out_inode
;
571 int fput_needed_in
, fput_needed_out
;
574 * Get input file, and verify that it is ok..
577 in_file
= fget_light(in_fd
, &fput_needed_in
);
580 if (!(in_file
->f_mode
& FMODE_READ
))
583 in_inode
= in_file
->f_dentry
->d_inode
;
586 if (!in_file
->f_op
|| !in_file
->f_op
->sendfile
)
590 ppos
= &in_file
->f_pos
;
592 if (!(in_file
->f_mode
& FMODE_PREAD
))
594 retval
= locks_verify_area(FLOCK_VERIFY_READ
, in_inode
, in_file
, *ppos
, count
);
598 retval
= security_file_permission (in_file
, MAY_READ
);
603 * Get output file, and verify that it is ok..
606 out_file
= fget_light(out_fd
, &fput_needed_out
);
609 if (!(out_file
->f_mode
& FMODE_WRITE
))
612 if (!out_file
->f_op
|| !out_file
->f_op
->sendpage
)
614 out_inode
= out_file
->f_dentry
->d_inode
;
615 retval
= locks_verify_area(FLOCK_VERIFY_WRITE
, out_inode
, out_file
, out_file
->f_pos
, count
);
619 retval
= security_file_permission (out_file
, MAY_WRITE
);
624 max
= min(in_inode
->i_sb
->s_maxbytes
, out_inode
->i_sb
->s_maxbytes
);
628 if (unlikely(pos
< 0))
630 if (unlikely(pos
+ count
> max
)) {
637 retval
= in_file
->f_op
->sendfile(in_file
, ppos
, count
, file_send_actor
, out_file
);
643 fput_light(out_file
, fput_needed_out
);
645 fput_light(in_file
, fput_needed_in
);
650 asmlinkage ssize_t
sys_sendfile(int out_fd
, int in_fd
, off_t __user
*offset
, size_t count
)
657 if (unlikely(get_user(off
, offset
)))
660 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, MAX_NON_LFS
);
661 if (unlikely(put_user(pos
, offset
)))
666 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);
669 asmlinkage ssize_t
sys_sendfile64(int out_fd
, int in_fd
, loff_t __user
*offset
, size_t count
)
675 if (unlikely(copy_from_user(&pos
, offset
, sizeof(loff_t
))))
677 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, 0);
678 if (unlikely(put_user(pos
, offset
)))
683 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);