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>
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 static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
208 set_current_state(TASK_UNINTERRUPTIBLE);
209 if (!kiocbIsKicked(iocb))
212 kiocbClearKicked(iocb);
213 __set_current_state(TASK_RUNNING);
216 ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
221 init_sync_kiocb(&kiocb, filp);
222 kiocb.ki_pos = *ppos;
223 while (-EIOCBRETRY ==
224 (ret = filp->f_op->aio_read(&kiocb, buf, len, kiocb.ki_pos)))
225 wait_on_retry_sync_kiocb(&kiocb);
227 if (-EIOCBQUEUED == ret)
228 ret = wait_on_sync_kiocb(&kiocb);
229 *ppos = kiocb.ki_pos;
233 EXPORT_SYMBOL(do_sync_read);
235 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
239 if (!(file->f_mode & FMODE_READ))
241 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
243 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
246 ret = rw_verify_area(READ, file, pos, count);
248 ret = security_file_permission (file, MAY_READ);
250 if (file->f_op->read)
251 ret = file->f_op->read(file, buf, count, pos);
253 ret = do_sync_read(file, buf, count, pos);
255 fsnotify_access(file->f_dentry);
256 current->rchar += ret;
265 EXPORT_SYMBOL(vfs_read);
267 ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
272 init_sync_kiocb(&kiocb, filp);
273 kiocb.ki_pos = *ppos;
274 while (-EIOCBRETRY ==
275 (ret = filp->f_op->aio_write(&kiocb, buf, len, kiocb.ki_pos)))
276 wait_on_retry_sync_kiocb(&kiocb);
278 if (-EIOCBQUEUED == ret)
279 ret = wait_on_sync_kiocb(&kiocb);
280 *ppos = kiocb.ki_pos;
284 EXPORT_SYMBOL(do_sync_write);
286 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
290 if (!(file->f_mode & FMODE_WRITE))
292 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
294 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
297 ret = rw_verify_area(WRITE, file, pos, count);
299 ret = security_file_permission (file, MAY_WRITE);
301 if (file->f_op->write)
302 ret = file->f_op->write(file, buf, count, pos);
304 ret = do_sync_write(file, buf, count, pos);
306 fsnotify_modify(file->f_dentry);
307 current->wchar += ret;
316 EXPORT_SYMBOL(vfs_write);
318 static inline loff_t file_pos_read(struct file *file)
323 static inline void file_pos_write(struct file *file, loff_t pos)
328 asmlinkage ssize_t sys_read(unsigned int fd, char __user * buf, size_t count)
331 ssize_t ret = -EBADF;
334 file = fget_light(fd, &fput_needed);
336 loff_t pos = file_pos_read(file);
337 ret = vfs_read(file, buf, count, &pos);
338 file_pos_write(file, pos);
339 fput_light(file, fput_needed);
344 EXPORT_SYMBOL_GPL(sys_read);
346 asmlinkage ssize_t sys_write(unsigned int fd, const char __user * buf, size_t count)
349 ssize_t ret = -EBADF;
352 file = fget_light(fd, &fput_needed);
354 loff_t pos = file_pos_read(file);
355 ret = vfs_write(file, buf, count, &pos);
356 file_pos_write(file, pos);
357 fput_light(file, fput_needed);
363 asmlinkage ssize_t sys_pread64(unsigned int fd, char __user *buf,
364 size_t count, loff_t pos)
367 ssize_t ret = -EBADF;
373 file = fget_light(fd, &fput_needed);
376 if (file->f_mode & FMODE_PREAD)
377 ret = vfs_read(file, buf, count, &pos);
378 fput_light(file, fput_needed);
384 asmlinkage ssize_t sys_pwrite64(unsigned int fd, const char __user *buf,
385 size_t count, loff_t pos)
388 ssize_t ret = -EBADF;
394 file = fget_light(fd, &fput_needed);
397 if (file->f_mode & FMODE_PWRITE)
398 ret = vfs_write(file, buf, count, &pos);
399 fput_light(file, fput_needed);
406 * Reduce an iovec's length in-place. Return the resulting number of segments
408 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
410 unsigned long seg = 0;
413 while (seg < nr_segs) {
415 if (len + iov->iov_len >= to) {
416 iov->iov_len = to - len;
425 EXPORT_SYMBOL(iov_shorten);
427 /* A write operation does a read from user space and vice versa */
428 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
430 static ssize_t do_readv_writev(int type, struct file *file,
431 const struct iovec __user * uvector,
432 unsigned long nr_segs, loff_t *pos)
434 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
435 typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
438 struct iovec iovstack[UIO_FASTIOV];
439 struct iovec *iov=iovstack, *vector;
446 * SuS says "The readv() function *may* fail if the iovcnt argument
447 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
448 * traditionally returned zero for zero segments, so...
455 * First get the "struct iovec" from user memory and
456 * verify all the pointers
459 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
463 if (nr_segs > UIO_FASTIOV) {
465 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
470 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector)))
474 * Single unix specification:
475 * We should -EINVAL if an element length is not >= 0 and fitting an
476 * ssize_t. The total length is fitting an ssize_t
478 * Be careful here because iov_len is a size_t not an ssize_t
482 for (seg = 0; seg < nr_segs; seg++) {
483 void __user *buf = iov[seg].iov_base;
484 ssize_t len = (ssize_t)iov[seg].iov_len;
486 if (len < 0) /* size_t not fitting an ssize_t .. */
488 if (unlikely(!access_ok(vrfy_dir(type), buf, len)))
491 if ((ssize_t)tot_len < 0) /* maths overflow on the ssize_t */
499 ret = rw_verify_area(type, file, pos, tot_len);
505 fn = file->f_op->read;
506 fnv = file->f_op->readv;
508 fn = (io_fn_t)file->f_op->write;
509 fnv = file->f_op->writev;
512 ret = fnv(file, iov, nr_segs, pos);
516 /* Do it by hand, with file-ops */
519 while (nr_segs > 0) {
524 base = vector->iov_base;
525 len = vector->iov_len;
529 nr = fn(file, base, len, pos);
542 if ((ret + (type == READ)) > 0) {
544 fsnotify_access(file->f_dentry);
546 fsnotify_modify(file->f_dentry);
554 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
555 unsigned long vlen, loff_t *pos)
557 if (!(file->f_mode & FMODE_READ))
559 if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
562 return do_readv_writev(READ, file, vec, vlen, pos);
565 EXPORT_SYMBOL(vfs_readv);
567 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
568 unsigned long vlen, loff_t *pos)
570 if (!(file->f_mode & FMODE_WRITE))
572 if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
575 return do_readv_writev(WRITE, file, vec, vlen, pos);
578 EXPORT_SYMBOL(vfs_writev);
581 sys_readv(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
584 ssize_t ret = -EBADF;
587 file = fget_light(fd, &fput_needed);
589 loff_t pos = file_pos_read(file);
590 ret = vfs_readv(file, vec, vlen, &pos);
591 file_pos_write(file, pos);
592 fput_light(file, fput_needed);
596 current->rchar += ret;
602 sys_writev(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
605 ssize_t ret = -EBADF;
608 file = fget_light(fd, &fput_needed);
610 loff_t pos = file_pos_read(file);
611 ret = vfs_writev(file, vec, vlen, &pos);
612 file_pos_write(file, pos);
613 fput_light(file, fput_needed);
617 current->wchar += ret;
622 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
623 size_t count, loff_t max)
625 struct file * in_file, * out_file;
626 struct inode * in_inode, * out_inode;
629 int fput_needed_in, fput_needed_out;
632 * Get input file, and verify that it is ok..
635 in_file = fget_light(in_fd, &fput_needed_in);
638 if (!(in_file->f_mode & FMODE_READ))
641 in_inode = in_file->f_dentry->d_inode;
644 if (!in_file->f_op || !in_file->f_op->sendfile)
648 ppos = &in_file->f_pos;
650 if (!(in_file->f_mode & FMODE_PREAD))
652 retval = rw_verify_area(READ, in_file, ppos, count);
656 retval = security_file_permission (in_file, MAY_READ);
661 * Get output file, and verify that it is ok..
664 out_file = fget_light(out_fd, &fput_needed_out);
667 if (!(out_file->f_mode & FMODE_WRITE))
670 if (!out_file->f_op || !out_file->f_op->sendpage)
672 out_inode = out_file->f_dentry->d_inode;
673 retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
677 retval = security_file_permission (out_file, MAY_WRITE);
682 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
686 if (unlikely(pos < 0))
688 if (unlikely(pos + count > max)) {
695 retval = in_file->f_op->sendfile(in_file, ppos, count, file_send_actor, out_file);
698 current->rchar += retval;
699 current->wchar += retval;
708 fput_light(out_file, fput_needed_out);
710 fput_light(in_file, fput_needed_in);
715 asmlinkage ssize_t sys_sendfile(int out_fd, int in_fd, off_t __user *offset, size_t count)
722 if (unlikely(get_user(off, offset)))
725 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
726 if (unlikely(put_user(pos, offset)))
731 return do_sendfile(out_fd, in_fd, NULL, count, 0);
734 asmlinkage ssize_t sys_sendfile64(int out_fd, int in_fd, loff_t __user *offset, size_t count)
740 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
742 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
743 if (unlikely(put_user(pos, offset)))
748 return do_sendfile(out_fd, in_fd, NULL, count, 0);