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>
19 struct file_operations generic_ro_fops
= {
20 .llseek
= generic_file_llseek
,
21 .read
= generic_file_read
,
22 .mmap
= generic_file_readonly_mmap
,
23 .sendfile
= generic_file_sendfile
,
26 EXPORT_SYMBOL(generic_ro_fops
);
28 loff_t
generic_file_llseek(struct file
*file
, loff_t offset
, int origin
)
31 struct inode
*inode
= file
->f_mapping
->host
;
36 offset
+= inode
->i_size
;
39 offset
+= file
->f_pos
;
42 if (offset
>=0 && offset
<=inode
->i_sb
->s_maxbytes
) {
43 if (offset
!= file
->f_pos
) {
53 EXPORT_SYMBOL(generic_file_llseek
);
55 loff_t
remote_llseek(struct file
*file
, loff_t offset
, int origin
)
62 offset
+= i_size_read(file
->f_dentry
->d_inode
);
65 offset
+= file
->f_pos
;
68 if (offset
>=0 && offset
<=file
->f_dentry
->d_inode
->i_sb
->s_maxbytes
) {
69 if (offset
!= file
->f_pos
) {
79 EXPORT_SYMBOL(remote_llseek
);
81 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
;
112 EXPORT_SYMBOL(default_llseek
);
114 static inline loff_t
llseek(struct file
*file
, loff_t offset
, int origin
)
116 loff_t (*fn
)(struct file
*, loff_t
, int);
119 if (file
->f_op
&& file
->f_op
->llseek
)
120 fn
= file
->f_op
->llseek
;
121 return fn(file
, offset
, origin
);
124 asmlinkage off_t
sys_lseek(unsigned int fd
, off_t offset
, unsigned int origin
)
131 file
= fget_light(fd
, &fput_needed
);
137 loff_t res
= llseek(file
, offset
, origin
);
139 if (res
!= (loff_t
)retval
)
140 retval
= -EOVERFLOW
; /* LFS: should only happen on 32 bit platforms */
142 fput_light(file
, fput_needed
);
146 EXPORT_SYMBOL_GPL(sys_lseek
);
148 #if !defined(__alpha__)
149 asmlinkage
long sys_llseek(unsigned int fd
, unsigned long offset_high
,
150 unsigned long offset_low
, loff_t __user
* result
,
159 file
= fget_light(fd
, &fput_needed
);
167 offset
= llseek(file
, ((loff_t
) offset_high
<< 32) | offset_low
,
170 retval
= (int)offset
;
173 if (!copy_to_user(result
, &offset
, sizeof(offset
)))
177 fput_light(file
, fput_needed
);
183 ssize_t
do_sync_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*ppos
)
188 init_sync_kiocb(&kiocb
, filp
);
189 kiocb
.ki_pos
= *ppos
;
190 ret
= filp
->f_op
->aio_read(&kiocb
, buf
, len
, kiocb
.ki_pos
);
191 if (-EIOCBQUEUED
== ret
)
192 ret
= wait_on_sync_kiocb(&kiocb
);
193 *ppos
= kiocb
.ki_pos
;
197 EXPORT_SYMBOL(do_sync_read
);
199 ssize_t
vfs_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*pos
)
201 struct inode
*inode
= file
->f_dentry
->d_inode
;
204 if (!(file
->f_mode
& FMODE_READ
))
206 if (!file
->f_op
|| (!file
->f_op
->read
&& !file
->f_op
->aio_read
))
209 ret
= locks_verify_area(FLOCK_VERIFY_READ
, inode
, file
, *pos
, count
);
211 ret
= security_file_permission (file
, MAY_READ
);
213 if (file
->f_op
->read
)
214 ret
= file
->f_op
->read(file
, buf
, count
, pos
);
216 ret
= do_sync_read(file
, buf
, count
, pos
);
218 dnotify_parent(file
->f_dentry
, DN_ACCESS
);
225 EXPORT_SYMBOL(vfs_read
);
227 ssize_t
do_sync_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*ppos
)
232 init_sync_kiocb(&kiocb
, filp
);
233 kiocb
.ki_pos
= *ppos
;
234 ret
= filp
->f_op
->aio_write(&kiocb
, buf
, len
, kiocb
.ki_pos
);
235 if (-EIOCBQUEUED
== ret
)
236 ret
= wait_on_sync_kiocb(&kiocb
);
237 *ppos
= kiocb
.ki_pos
;
241 EXPORT_SYMBOL(do_sync_write
);
243 ssize_t
vfs_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*pos
)
245 struct inode
*inode
= file
->f_dentry
->d_inode
;
248 if (!(file
->f_mode
& FMODE_WRITE
))
250 if (!file
->f_op
|| (!file
->f_op
->write
&& !file
->f_op
->aio_write
))
253 ret
= locks_verify_area(FLOCK_VERIFY_WRITE
, inode
, file
, *pos
, count
);
255 ret
= security_file_permission (file
, MAY_WRITE
);
257 if (file
->f_op
->write
)
258 ret
= file
->f_op
->write(file
, buf
, count
, pos
);
260 ret
= do_sync_write(file
, buf
, count
, pos
);
262 dnotify_parent(file
->f_dentry
, DN_MODIFY
);
269 EXPORT_SYMBOL(vfs_write
);
271 asmlinkage ssize_t
sys_read(unsigned int fd
, char __user
* buf
, size_t count
)
274 ssize_t ret
= -EBADF
;
277 file
= fget_light(fd
, &fput_needed
);
279 ret
= vfs_read(file
, buf
, count
, &file
->f_pos
);
280 fput_light(file
, fput_needed
);
285 EXPORT_SYMBOL_GPL(sys_read
);
287 asmlinkage ssize_t
sys_write(unsigned int fd
, const char __user
* buf
, size_t count
)
290 ssize_t ret
= -EBADF
;
293 file
= fget_light(fd
, &fput_needed
);
295 ret
= vfs_write(file
, buf
, count
, &file
->f_pos
);
296 fput_light(file
, fput_needed
);
302 asmlinkage ssize_t
sys_pread64(unsigned int fd
, char __user
*buf
,
303 size_t count
, loff_t pos
)
306 ssize_t ret
= -EBADF
;
312 file
= fget_light(fd
, &fput_needed
);
314 ret
= vfs_read(file
, buf
, count
, &pos
);
315 fput_light(file
, fput_needed
);
321 asmlinkage ssize_t
sys_pwrite64(unsigned int fd
, const char __user
*buf
,
322 size_t count
, loff_t pos
)
325 ssize_t ret
= -EBADF
;
331 file
= fget_light(fd
, &fput_needed
);
333 ret
= vfs_write(file
, buf
, count
, &pos
);
334 fput_light(file
, fput_needed
);
341 * Reduce an iovec's length in-place. Return the resulting number of segments
343 unsigned long iov_shorten(struct iovec
*iov
, unsigned long nr_segs
, size_t to
)
345 unsigned long seg
= 0;
348 while (seg
< nr_segs
) {
350 if (len
+ iov
->iov_len
>= to
) {
351 iov
->iov_len
= to
- len
;
360 EXPORT_SYMBOL(iov_shorten
);
362 static ssize_t
do_readv_writev(int type
, struct file
*file
,
363 const struct iovec __user
* uvector
,
364 unsigned long nr_segs
, loff_t
*pos
)
366 typedef ssize_t (*io_fn_t
)(struct file
*, char __user
*, size_t, loff_t
*);
367 typedef ssize_t (*iov_fn_t
)(struct file
*, const struct iovec
*, unsigned long, loff_t
*);
370 struct iovec iovstack
[UIO_FASTIOV
];
371 struct iovec
*iov
=iovstack
, *vector
;
379 * SuS says "The readv() function *may* fail if the iovcnt argument
380 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
381 * traditionally returned zero for zero segments, so...
388 * First get the "struct iovec" from user memory and
389 * verify all the pointers
392 if ((nr_segs
> UIO_MAXIOV
) || (nr_segs
<= 0))
396 if (nr_segs
> UIO_FASTIOV
) {
398 iov
= kmalloc(nr_segs
*sizeof(struct iovec
), GFP_KERNEL
);
403 if (copy_from_user(iov
, uvector
, nr_segs
*sizeof(*uvector
)))
407 * Single unix specification:
408 * We should -EINVAL if an element length is not >= 0 and fitting an
409 * ssize_t. The total length is fitting an ssize_t
411 * Be careful here because iov_len is a size_t not an ssize_t
415 for (seg
= 0 ; seg
< nr_segs
; seg
++) {
416 ssize_t tmp
= tot_len
;
417 ssize_t len
= (ssize_t
)iov
[seg
].iov_len
;
418 if (len
< 0) /* size_t not fitting an ssize_t .. */
421 if (tot_len
< tmp
) /* maths overflow on the ssize_t */
429 inode
= file
->f_dentry
->d_inode
;
430 /* VERIFY_WRITE actually means a read, as we write to user space */
431 ret
= locks_verify_area((type
== READ
432 ? FLOCK_VERIFY_READ
: FLOCK_VERIFY_WRITE
),
433 inode
, file
, *pos
, tot_len
);
439 fn
= file
->f_op
->read
;
440 fnv
= file
->f_op
->readv
;
442 fn
= (io_fn_t
)file
->f_op
->write
;
443 fnv
= file
->f_op
->writev
;
446 ret
= fnv(file
, iov
, nr_segs
, pos
);
450 /* Do it by hand, with file-ops */
453 while (nr_segs
> 0) {
458 base
= vector
->iov_base
;
459 len
= vector
->iov_len
;
463 nr
= fn(file
, base
, len
, pos
);
476 if ((ret
+ (type
== READ
)) > 0)
477 dnotify_parent(file
->f_dentry
,
478 (type
== READ
) ? DN_ACCESS
: DN_MODIFY
);
482 ssize_t
vfs_readv(struct file
*file
, const struct iovec __user
*vec
,
483 unsigned long vlen
, loff_t
*pos
)
485 if (!(file
->f_mode
& FMODE_READ
))
487 if (!file
->f_op
|| (!file
->f_op
->readv
&& !file
->f_op
->read
))
490 return do_readv_writev(READ
, file
, vec
, vlen
, pos
);
493 EXPORT_SYMBOL(vfs_readv
);
495 ssize_t
vfs_writev(struct file
*file
, const struct iovec __user
*vec
,
496 unsigned long vlen
, loff_t
*pos
)
498 if (!(file
->f_mode
& FMODE_WRITE
))
500 if (!file
->f_op
|| (!file
->f_op
->writev
&& !file
->f_op
->write
))
503 return do_readv_writev(WRITE
, file
, vec
, vlen
, pos
);
506 EXPORT_SYMBOL(vfs_writev
);
509 sys_readv(unsigned long fd
, const struct iovec __user
*vec
, unsigned long vlen
)
512 ssize_t ret
= -EBADF
;
515 file
= fget_light(fd
, &fput_needed
);
517 ret
= vfs_readv(file
, vec
, vlen
, &file
->f_pos
);
518 fput_light(file
, fput_needed
);
525 sys_writev(unsigned long fd
, const struct iovec __user
*vec
, unsigned long vlen
)
528 ssize_t ret
= -EBADF
;
531 file
= fget_light(fd
, &fput_needed
);
533 ret
= vfs_writev(file
, vec
, vlen
, &file
->f_pos
);
534 fput_light(file
, fput_needed
);
540 static ssize_t
do_sendfile(int out_fd
, int in_fd
, loff_t
*ppos
,
541 size_t count
, loff_t max
)
543 struct file
* in_file
, * out_file
;
544 struct inode
* in_inode
, * out_inode
;
547 int fput_needed_in
, fput_needed_out
;
550 * Get input file, and verify that it is ok..
553 in_file
= fget_light(in_fd
, &fput_needed_in
);
556 if (!(in_file
->f_mode
& FMODE_READ
))
559 in_inode
= in_file
->f_dentry
->d_inode
;
562 if (!in_file
->f_op
|| !in_file
->f_op
->sendfile
)
565 ppos
= &in_file
->f_pos
;
566 retval
= locks_verify_area(FLOCK_VERIFY_READ
, in_inode
, in_file
, *ppos
, count
);
570 retval
= security_file_permission (in_file
, MAY_READ
);
575 * Get output file, and verify that it is ok..
578 out_file
= fget_light(out_fd
, &fput_needed_out
);
581 if (!(out_file
->f_mode
& FMODE_WRITE
))
584 if (!out_file
->f_op
|| !out_file
->f_op
->sendpage
)
586 out_inode
= out_file
->f_dentry
->d_inode
;
587 retval
= locks_verify_area(FLOCK_VERIFY_WRITE
, out_inode
, out_file
, out_file
->f_pos
, count
);
591 retval
= security_file_permission (out_file
, MAY_WRITE
);
596 max
= min(in_inode
->i_sb
->s_maxbytes
, out_inode
->i_sb
->s_maxbytes
);
600 if (unlikely(pos
< 0))
602 if (unlikely(pos
+ count
> max
)) {
609 retval
= in_file
->f_op
->sendfile(in_file
, ppos
, count
, file_send_actor
, out_file
);
615 fput_light(out_file
, fput_needed_out
);
617 fput_light(in_file
, fput_needed_in
);
622 asmlinkage ssize_t
sys_sendfile(int out_fd
, int in_fd
, off_t __user
*offset
, size_t count
)
629 if (unlikely(get_user(off
, offset
)))
632 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, MAX_NON_LFS
);
633 if (unlikely(put_user(pos
, offset
)))
638 return do_sendfile(out_fd
, in_fd
, NULL
, count
, MAX_NON_LFS
);
641 asmlinkage ssize_t
sys_sendfile64(int out_fd
, int in_fd
, loff_t __user
*offset
, size_t count
)
647 if (unlikely(copy_from_user(&pos
, offset
, sizeof(loff_t
))))
649 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, 0);
650 if (unlikely(put_user(pos
, offset
)))
655 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);