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>
18 #include "read_write.h"
20 #include <asm/uaccess.h>
21 #include <asm/unistd.h>
23 const struct file_operations generic_ro_fops = {
24 .llseek = generic_file_llseek,
26 .aio_read = generic_file_aio_read,
27 .mmap = generic_file_readonly_mmap,
28 .sendfile = generic_file_sendfile,
31 EXPORT_SYMBOL(generic_ro_fops);
33 loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
36 struct inode *inode = file->f_mapping->host;
38 mutex_lock(&inode->i_mutex);
41 offset += inode->i_size;
44 offset += file->f_pos;
47 if (offset>=0 && offset<=inode->i_sb->s_maxbytes) {
48 if (offset != file->f_pos) {
54 mutex_unlock(&inode->i_mutex);
58 EXPORT_SYMBOL(generic_file_llseek);
60 loff_t remote_llseek(struct file *file, loff_t offset, int origin)
67 offset += i_size_read(file->f_dentry->d_inode);
70 offset += file->f_pos;
73 if (offset>=0 && offset<=file->f_dentry->d_inode->i_sb->s_maxbytes) {
74 if (offset != file->f_pos) {
83 EXPORT_SYMBOL(remote_llseek);
85 loff_t no_llseek(struct file *file, loff_t offset, int origin)
89 EXPORT_SYMBOL(no_llseek);
91 loff_t default_llseek(struct file *file, loff_t offset, int origin)
98 offset += i_size_read(file->f_dentry->d_inode);
101 offset += file->f_pos;
105 if (offset != file->f_pos) {
106 file->f_pos = offset;
114 EXPORT_SYMBOL(default_llseek);
116 loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
118 loff_t (*fn)(struct file *, loff_t, int);
121 if (file->f_mode & FMODE_LSEEK) {
123 if (file->f_op && file->f_op->llseek)
124 fn = file->f_op->llseek;
126 return fn(file, offset, origin);
128 EXPORT_SYMBOL(vfs_llseek);
130 asmlinkage off_t sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
137 file = fget_light(fd, &fput_needed);
143 loff_t res = vfs_llseek(file, offset, origin);
145 if (res != (loff_t)retval)
146 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
148 fput_light(file, fput_needed);
153 #ifdef __ARCH_WANT_SYS_LLSEEK
154 asmlinkage long sys_llseek(unsigned int fd, unsigned long offset_high,
155 unsigned long offset_low, loff_t __user * result,
164 file = fget_light(fd, &fput_needed);
172 offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
175 retval = (int)offset;
178 if (!copy_to_user(result, &offset, sizeof(offset)))
182 fput_light(file, fput_needed);
189 * rw_verify_area doesn't like huge counts. We limit
190 * them to something that fits in "int" so that others
191 * won't have to do range checks all the time.
193 #define MAX_RW_COUNT (INT_MAX & PAGE_CACHE_MASK)
195 int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
200 if (unlikely((ssize_t) count < 0))
203 if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
206 inode = file->f_dentry->d_inode;
207 if (unlikely(inode->i_flock && MANDATORY_LOCK(inode))) {
208 int retval = locks_mandatory_area(
209 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
210 inode, file, pos, count);
214 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
220 static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
222 set_current_state(TASK_UNINTERRUPTIBLE);
223 if (!kiocbIsKicked(iocb))
226 kiocbClearKicked(iocb);
227 __set_current_state(TASK_RUNNING);
230 ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
232 struct iovec iov = { .iov_base = buf, .iov_len = len };
236 init_sync_kiocb(&kiocb, filp);
237 kiocb.ki_pos = *ppos;
241 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
242 if (ret != -EIOCBRETRY)
244 wait_on_retry_sync_kiocb(&kiocb);
247 if (-EIOCBQUEUED == ret)
248 ret = wait_on_sync_kiocb(&kiocb);
249 *ppos = kiocb.ki_pos;
253 EXPORT_SYMBOL(do_sync_read);
255 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
259 if (!(file->f_mode & FMODE_READ))
261 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
263 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
266 ret = rw_verify_area(READ, file, pos, count);
269 ret = security_file_permission (file, MAY_READ);
271 if (file->f_op->read)
272 ret = file->f_op->read(file, buf, count, pos);
274 ret = do_sync_read(file, buf, count, pos);
276 fsnotify_access(file->f_dentry);
277 current->rchar += ret;
286 EXPORT_SYMBOL(vfs_read);
288 ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
290 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
294 init_sync_kiocb(&kiocb, filp);
295 kiocb.ki_pos = *ppos;
299 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
300 if (ret != -EIOCBRETRY)
302 wait_on_retry_sync_kiocb(&kiocb);
305 if (-EIOCBQUEUED == ret)
306 ret = wait_on_sync_kiocb(&kiocb);
307 *ppos = kiocb.ki_pos;
311 EXPORT_SYMBOL(do_sync_write);
313 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
317 if (!(file->f_mode & FMODE_WRITE))
319 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
321 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
324 ret = rw_verify_area(WRITE, file, pos, count);
327 ret = security_file_permission (file, MAY_WRITE);
329 if (file->f_op->write)
330 ret = file->f_op->write(file, buf, count, pos);
332 ret = do_sync_write(file, buf, count, pos);
334 fsnotify_modify(file->f_dentry);
335 current->wchar += ret;
344 EXPORT_SYMBOL(vfs_write);
346 static inline loff_t file_pos_read(struct file *file)
351 static inline void file_pos_write(struct file *file, loff_t pos)
356 asmlinkage ssize_t sys_read(unsigned int fd, char __user * buf, size_t count)
359 ssize_t ret = -EBADF;
362 file = fget_light(fd, &fput_needed);
364 loff_t pos = file_pos_read(file);
365 ret = vfs_read(file, buf, count, &pos);
366 file_pos_write(file, pos);
367 fput_light(file, fput_needed);
372 EXPORT_SYMBOL_GPL(sys_read);
374 asmlinkage ssize_t sys_write(unsigned int fd, const char __user * buf, size_t count)
377 ssize_t ret = -EBADF;
380 file = fget_light(fd, &fput_needed);
382 loff_t pos = file_pos_read(file);
383 ret = vfs_write(file, buf, count, &pos);
384 file_pos_write(file, pos);
385 fput_light(file, fput_needed);
391 asmlinkage ssize_t sys_pread64(unsigned int fd, char __user *buf,
392 size_t count, loff_t pos)
395 ssize_t ret = -EBADF;
401 file = fget_light(fd, &fput_needed);
404 if (file->f_mode & FMODE_PREAD)
405 ret = vfs_read(file, buf, count, &pos);
406 fput_light(file, fput_needed);
412 asmlinkage ssize_t sys_pwrite64(unsigned int fd, const char __user *buf,
413 size_t count, loff_t pos)
416 ssize_t ret = -EBADF;
422 file = fget_light(fd, &fput_needed);
425 if (file->f_mode & FMODE_PWRITE)
426 ret = vfs_write(file, buf, count, &pos);
427 fput_light(file, fput_needed);
434 * Reduce an iovec's length in-place. Return the resulting number of segments
436 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
438 unsigned long seg = 0;
441 while (seg < nr_segs) {
443 if (len + iov->iov_len >= to) {
444 iov->iov_len = to - len;
453 EXPORT_UNUSED_SYMBOL(iov_shorten); /* June 2006 */
455 ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
456 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
461 init_sync_kiocb(&kiocb, filp);
462 kiocb.ki_pos = *ppos;
464 kiocb.ki_nbytes = len;
467 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
468 if (ret != -EIOCBRETRY)
470 wait_on_retry_sync_kiocb(&kiocb);
473 if (ret == -EIOCBQUEUED)
474 ret = wait_on_sync_kiocb(&kiocb);
475 *ppos = kiocb.ki_pos;
479 /* Do it by hand, with file-ops */
480 ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
481 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
483 struct iovec *vector = iov;
486 while (nr_segs > 0) {
491 base = vector->iov_base;
492 len = vector->iov_len;
496 nr = fn(filp, base, len, ppos);
511 /* A write operation does a read from user space and vice versa */
512 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
514 static ssize_t do_readv_writev(int type, struct file *file,
515 const struct iovec __user * uvector,
516 unsigned long nr_segs, loff_t *pos)
519 struct iovec iovstack[UIO_FASTIOV];
520 struct iovec *iov = iovstack;
527 * SuS says "The readv() function *may* fail if the iovcnt argument
528 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
529 * traditionally returned zero for zero segments, so...
536 * First get the "struct iovec" from user memory and
537 * verify all the pointers
540 if (nr_segs > UIO_MAXIOV)
544 if (nr_segs > UIO_FASTIOV) {
546 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
551 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector)))
555 * Single unix specification:
556 * We should -EINVAL if an element length is not >= 0 and fitting an
557 * ssize_t. The total length is fitting an ssize_t
559 * Be careful here because iov_len is a size_t not an ssize_t
563 for (seg = 0; seg < nr_segs; seg++) {
564 void __user *buf = iov[seg].iov_base;
565 ssize_t len = (ssize_t)iov[seg].iov_len;
567 if (len < 0) /* size_t not fitting an ssize_t .. */
569 if (unlikely(!access_ok(vrfy_dir(type), buf, len)))
572 if ((ssize_t)tot_len < 0) /* maths overflow on the ssize_t */
580 ret = rw_verify_area(type, file, pos, tot_len);
583 ret = security_file_permission(file, type == READ ? MAY_READ : MAY_WRITE);
589 fn = file->f_op->read;
590 fnv = file->f_op->aio_read;
592 fn = (io_fn_t)file->f_op->write;
593 fnv = file->f_op->aio_write;
597 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
600 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
605 if ((ret + (type == READ)) > 0) {
607 fsnotify_access(file->f_dentry);
609 fsnotify_modify(file->f_dentry);
617 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
618 unsigned long vlen, loff_t *pos)
620 if (!(file->f_mode & FMODE_READ))
622 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
625 return do_readv_writev(READ, file, vec, vlen, pos);
628 EXPORT_SYMBOL(vfs_readv);
630 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
631 unsigned long vlen, loff_t *pos)
633 if (!(file->f_mode & FMODE_WRITE))
635 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
638 return do_readv_writev(WRITE, file, vec, vlen, pos);
641 EXPORT_SYMBOL(vfs_writev);
644 sys_readv(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
647 ssize_t ret = -EBADF;
650 file = fget_light(fd, &fput_needed);
652 loff_t pos = file_pos_read(file);
653 ret = vfs_readv(file, vec, vlen, &pos);
654 file_pos_write(file, pos);
655 fput_light(file, fput_needed);
659 current->rchar += ret;
665 sys_writev(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
668 ssize_t ret = -EBADF;
671 file = fget_light(fd, &fput_needed);
673 loff_t pos = file_pos_read(file);
674 ret = vfs_writev(file, vec, vlen, &pos);
675 file_pos_write(file, pos);
676 fput_light(file, fput_needed);
680 current->wchar += ret;
685 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
686 size_t count, loff_t max)
688 struct file * in_file, * out_file;
689 struct inode * in_inode, * out_inode;
692 int fput_needed_in, fput_needed_out;
695 * Get input file, and verify that it is ok..
698 in_file = fget_light(in_fd, &fput_needed_in);
701 if (!(in_file->f_mode & FMODE_READ))
704 in_inode = in_file->f_dentry->d_inode;
707 if (!in_file->f_op || !in_file->f_op->sendfile)
711 ppos = &in_file->f_pos;
713 if (!(in_file->f_mode & FMODE_PREAD))
715 retval = rw_verify_area(READ, in_file, ppos, count);
720 retval = security_file_permission (in_file, MAY_READ);
725 * Get output file, and verify that it is ok..
728 out_file = fget_light(out_fd, &fput_needed_out);
731 if (!(out_file->f_mode & FMODE_WRITE))
734 if (!out_file->f_op || !out_file->f_op->sendpage)
736 out_inode = out_file->f_dentry->d_inode;
737 retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
742 retval = security_file_permission (out_file, MAY_WRITE);
747 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
751 if (unlikely(pos < 0))
753 if (unlikely(pos + count > max)) {
760 retval = in_file->f_op->sendfile(in_file, ppos, count, file_send_actor, out_file);
763 current->rchar += retval;
764 current->wchar += retval;
773 fput_light(out_file, fput_needed_out);
775 fput_light(in_file, fput_needed_in);
780 asmlinkage ssize_t sys_sendfile(int out_fd, int in_fd, off_t __user *offset, size_t count)
787 if (unlikely(get_user(off, offset)))
790 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
791 if (unlikely(put_user(pos, offset)))
796 return do_sendfile(out_fd, in_fd, NULL, count, 0);
799 asmlinkage ssize_t sys_sendfile64(int out_fd, int in_fd, loff_t __user *offset, size_t count)
805 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
807 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
808 if (unlikely(put_user(pos, offset)))
813 return do_sendfile(out_fd, in_fd, NULL, count, 0);