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/fsnotify.h>
13 #include <linux/security.h>
14 #include <linux/export.h>
15 #include <linux/syscalls.h>
16 #include <linux/pagemap.h>
17 #include <linux/splice.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 .splice_read = generic_file_splice_read,
31 EXPORT_SYMBOL(generic_ro_fops);
33 static inline int unsigned_offsets(struct file *file)
35 return file->f_mode & FMODE_UNSIGNED_OFFSET;
38 static loff_t lseek_execute(struct file *file, struct inode *inode,
39 loff_t offset, loff_t maxsize)
41 if (offset < 0 && !unsigned_offsets(file))
46 if (offset != file->f_pos) {
54 * generic_file_llseek_size - generic llseek implementation for regular files
55 * @file: file structure to seek on
56 * @offset: file offset to seek to
57 * @origin: type of seek
58 * @size: max size of this file in file system
59 * @eof: offset used for SEEK_END position
61 * This is a variant of generic_file_llseek that allows passing in a custom
62 * maximum file size and a custom EOF position, for e.g. hashed directories
65 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
66 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
67 * read/writes behave like SEEK_SET against seeks.
70 generic_file_llseek_size(struct file *file, loff_t offset, int origin,
71 loff_t maxsize, loff_t eof)
73 struct inode *inode = file->f_mapping->host;
81 * Here we special-case the lseek(fd, 0, SEEK_CUR)
82 * position-querying operation. Avoid rewriting the "same"
83 * f_pos value back to the file because a concurrent read(),
84 * write() or lseek() might have altered it
89 * f_lock protects against read/modify/write race with other
90 * SEEK_CURs. Note that parallel writes and reads behave
93 spin_lock(&file->f_lock);
94 offset = lseek_execute(file, inode, file->f_pos + offset,
96 spin_unlock(&file->f_lock);
100 * In the generic case the entire file is data, so as long as
101 * offset isn't at the end of the file then the offset is data.
108 * There is a virtual hole at the end of the file, so as long as
109 * offset isn't i_size or larger, return i_size.
117 return lseek_execute(file, inode, offset, maxsize);
119 EXPORT_SYMBOL(generic_file_llseek_size);
122 * generic_file_llseek - generic llseek implementation for regular files
123 * @file: file structure to seek on
124 * @offset: file offset to seek to
125 * @origin: type of seek
127 * This is a generic implemenation of ->llseek useable for all normal local
128 * filesystems. It just updates the file offset to the value specified by
129 * @offset and @origin under i_mutex.
131 loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
133 struct inode *inode = file->f_mapping->host;
135 return generic_file_llseek_size(file, offset, origin,
136 inode->i_sb->s_maxbytes,
139 EXPORT_SYMBOL(generic_file_llseek);
142 * noop_llseek - No Operation Performed llseek implementation
143 * @file: file structure to seek on
144 * @offset: file offset to seek to
145 * @origin: type of seek
147 * This is an implementation of ->llseek useable for the rare special case when
148 * userspace expects the seek to succeed but the (device) file is actually not
149 * able to perform the seek. In this case you use noop_llseek() instead of
150 * falling back to the default implementation of ->llseek.
152 loff_t noop_llseek(struct file *file, loff_t offset, int origin)
156 EXPORT_SYMBOL(noop_llseek);
158 loff_t no_llseek(struct file *file, loff_t offset, int origin)
162 EXPORT_SYMBOL(no_llseek);
164 loff_t default_llseek(struct file *file, loff_t offset, int origin)
166 struct inode *inode = file->f_path.dentry->d_inode;
169 mutex_lock(&inode->i_mutex);
172 offset += i_size_read(inode);
176 retval = file->f_pos;
179 offset += file->f_pos;
183 * In the generic case the entire file is data, so as
184 * long as offset isn't at the end of the file then the
187 if (offset >= inode->i_size) {
194 * There is a virtual hole at the end of the file, so
195 * as long as offset isn't i_size or larger, return
198 if (offset >= inode->i_size) {
202 offset = inode->i_size;
206 if (offset >= 0 || unsigned_offsets(file)) {
207 if (offset != file->f_pos) {
208 file->f_pos = offset;
214 mutex_unlock(&inode->i_mutex);
217 EXPORT_SYMBOL(default_llseek);
219 loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
221 loff_t (*fn)(struct file *, loff_t, int);
224 if (file->f_mode & FMODE_LSEEK) {
225 if (file->f_op && file->f_op->llseek)
226 fn = file->f_op->llseek;
228 return fn(file, offset, origin);
230 EXPORT_SYMBOL(vfs_llseek);
232 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin)
239 file = fget_light(fd, &fput_needed);
244 if (origin <= SEEK_MAX) {
245 loff_t res = vfs_llseek(file, offset, origin);
247 if (res != (loff_t)retval)
248 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
250 fput_light(file, fput_needed);
255 #ifdef __ARCH_WANT_SYS_LLSEEK
256 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
257 unsigned long, offset_low, loff_t __user *, result,
258 unsigned int, origin)
266 file = fget_light(fd, &fput_needed);
271 if (origin > SEEK_MAX)
274 offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
277 retval = (int)offset;
280 if (!copy_to_user(result, &offset, sizeof(offset)))
284 fput_light(file, fput_needed);
292 * rw_verify_area doesn't like huge counts. We limit
293 * them to something that fits in "int" so that others
294 * won't have to do range checks all the time.
296 int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
300 int retval = -EINVAL;
302 inode = file->f_path.dentry->d_inode;
303 if (unlikely((ssize_t) count < 0))
306 if (unlikely(pos < 0)) {
307 if (!unsigned_offsets(file))
309 if (count >= -pos) /* both values are in 0..LLONG_MAX */
311 } else if (unlikely((loff_t) (pos + count) < 0)) {
312 if (!unsigned_offsets(file))
316 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
317 retval = locks_mandatory_area(
318 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
319 inode, file, pos, count);
323 retval = security_file_permission(file,
324 read_write == READ ? MAY_READ : MAY_WRITE);
327 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
330 static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
332 set_current_state(TASK_UNINTERRUPTIBLE);
333 if (!kiocbIsKicked(iocb))
336 kiocbClearKicked(iocb);
337 __set_current_state(TASK_RUNNING);
340 ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
342 struct iovec iov = { .iov_base = buf, .iov_len = len };
346 init_sync_kiocb(&kiocb, filp);
347 kiocb.ki_pos = *ppos;
349 kiocb.ki_nbytes = len;
352 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
353 if (ret != -EIOCBRETRY)
355 wait_on_retry_sync_kiocb(&kiocb);
358 if (-EIOCBQUEUED == ret)
359 ret = wait_on_sync_kiocb(&kiocb);
360 *ppos = kiocb.ki_pos;
364 EXPORT_SYMBOL(do_sync_read);
366 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
370 if (!(file->f_mode & FMODE_READ))
372 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
374 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
377 ret = rw_verify_area(READ, file, pos, count);
380 if (file->f_op->read)
381 ret = file->f_op->read(file, buf, count, pos);
383 ret = do_sync_read(file, buf, count, pos);
385 fsnotify_access(file);
386 add_rchar(current, ret);
394 EXPORT_SYMBOL(vfs_read);
396 ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
398 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
402 init_sync_kiocb(&kiocb, filp);
403 kiocb.ki_pos = *ppos;
405 kiocb.ki_nbytes = len;
408 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
409 if (ret != -EIOCBRETRY)
411 wait_on_retry_sync_kiocb(&kiocb);
414 if (-EIOCBQUEUED == ret)
415 ret = wait_on_sync_kiocb(&kiocb);
416 *ppos = kiocb.ki_pos;
420 EXPORT_SYMBOL(do_sync_write);
422 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
426 if (!(file->f_mode & FMODE_WRITE))
428 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
430 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
433 ret = rw_verify_area(WRITE, file, pos, count);
436 if (file->f_op->write)
437 ret = file->f_op->write(file, buf, count, pos);
439 ret = do_sync_write(file, buf, count, pos);
441 fsnotify_modify(file);
442 add_wchar(current, ret);
450 EXPORT_SYMBOL(vfs_write);
452 static inline loff_t file_pos_read(struct file *file)
457 static inline void file_pos_write(struct file *file, loff_t pos)
462 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
465 ssize_t ret = -EBADF;
468 file = fget_light(fd, &fput_needed);
470 loff_t pos = file_pos_read(file);
471 ret = vfs_read(file, buf, count, &pos);
472 file_pos_write(file, pos);
473 fput_light(file, fput_needed);
479 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
483 ssize_t ret = -EBADF;
486 file = fget_light(fd, &fput_needed);
488 loff_t pos = file_pos_read(file);
489 ret = vfs_write(file, buf, count, &pos);
490 file_pos_write(file, pos);
491 fput_light(file, fput_needed);
497 SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
498 size_t count, loff_t pos)
501 ssize_t ret = -EBADF;
507 file = fget_light(fd, &fput_needed);
510 if (file->f_mode & FMODE_PREAD)
511 ret = vfs_read(file, buf, count, &pos);
512 fput_light(file, fput_needed);
517 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
518 asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
520 return SYSC_pread64((unsigned int) fd, (char __user *) buf,
521 (size_t) count, pos);
523 SYSCALL_ALIAS(sys_pread64, SyS_pread64);
526 SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
527 size_t count, loff_t pos)
530 ssize_t ret = -EBADF;
536 file = fget_light(fd, &fput_needed);
539 if (file->f_mode & FMODE_PWRITE)
540 ret = vfs_write(file, buf, count, &pos);
541 fput_light(file, fput_needed);
546 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
547 asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
549 return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
550 (size_t) count, pos);
552 SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
556 * Reduce an iovec's length in-place. Return the resulting number of segments
558 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
560 unsigned long seg = 0;
563 while (seg < nr_segs) {
565 if (len + iov->iov_len >= to) {
566 iov->iov_len = to - len;
574 EXPORT_SYMBOL(iov_shorten);
576 ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
577 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
582 init_sync_kiocb(&kiocb, filp);
583 kiocb.ki_pos = *ppos;
585 kiocb.ki_nbytes = len;
588 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
589 if (ret != -EIOCBRETRY)
591 wait_on_retry_sync_kiocb(&kiocb);
594 if (ret == -EIOCBQUEUED)
595 ret = wait_on_sync_kiocb(&kiocb);
596 *ppos = kiocb.ki_pos;
600 /* Do it by hand, with file-ops */
601 ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
602 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
604 struct iovec *vector = iov;
607 while (nr_segs > 0) {
612 base = vector->iov_base;
613 len = vector->iov_len;
617 nr = fn(filp, base, len, ppos);
632 /* A write operation does a read from user space and vice versa */
633 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
635 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
636 unsigned long nr_segs, unsigned long fast_segs,
637 struct iovec *fast_pointer,
638 struct iovec **ret_pointer)
642 struct iovec *iov = fast_pointer;
645 * SuS says "The readv() function *may* fail if the iovcnt argument
646 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
647 * traditionally returned zero for zero segments, so...
655 * First get the "struct iovec" from user memory and
656 * verify all the pointers
658 if (nr_segs > UIO_MAXIOV) {
662 if (nr_segs > fast_segs) {
663 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
669 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
675 * According to the Single Unix Specification we should return EINVAL
676 * if an element length is < 0 when cast to ssize_t or if the
677 * total length would overflow the ssize_t return value of the
680 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
684 for (seg = 0; seg < nr_segs; seg++) {
685 void __user *buf = iov[seg].iov_base;
686 ssize_t len = (ssize_t)iov[seg].iov_len;
688 /* see if we we're about to use an invalid len or if
689 * it's about to overflow ssize_t */
695 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
699 if (len > MAX_RW_COUNT - ret) {
700 len = MAX_RW_COUNT - ret;
701 iov[seg].iov_len = len;
710 static ssize_t do_readv_writev(int type, struct file *file,
711 const struct iovec __user * uvector,
712 unsigned long nr_segs, loff_t *pos)
715 struct iovec iovstack[UIO_FASTIOV];
716 struct iovec *iov = iovstack;
726 ret = rw_copy_check_uvector(type, uvector, nr_segs,
727 ARRAY_SIZE(iovstack), iovstack, &iov);
732 ret = rw_verify_area(type, file, pos, tot_len);
738 fn = file->f_op->read;
739 fnv = file->f_op->aio_read;
741 fn = (io_fn_t)file->f_op->write;
742 fnv = file->f_op->aio_write;
746 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
749 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
754 if ((ret + (type == READ)) > 0) {
756 fsnotify_access(file);
758 fsnotify_modify(file);
763 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
764 unsigned long vlen, loff_t *pos)
766 if (!(file->f_mode & FMODE_READ))
768 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
771 return do_readv_writev(READ, file, vec, vlen, pos);
774 EXPORT_SYMBOL(vfs_readv);
776 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
777 unsigned long vlen, loff_t *pos)
779 if (!(file->f_mode & FMODE_WRITE))
781 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
784 return do_readv_writev(WRITE, file, vec, vlen, pos);
787 EXPORT_SYMBOL(vfs_writev);
789 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
793 ssize_t ret = -EBADF;
796 file = fget_light(fd, &fput_needed);
798 loff_t pos = file_pos_read(file);
799 ret = vfs_readv(file, vec, vlen, &pos);
800 file_pos_write(file, pos);
801 fput_light(file, fput_needed);
805 add_rchar(current, ret);
810 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
814 ssize_t ret = -EBADF;
817 file = fget_light(fd, &fput_needed);
819 loff_t pos = file_pos_read(file);
820 ret = vfs_writev(file, vec, vlen, &pos);
821 file_pos_write(file, pos);
822 fput_light(file, fput_needed);
826 add_wchar(current, ret);
831 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
833 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
834 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
837 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
838 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
840 loff_t pos = pos_from_hilo(pos_h, pos_l);
842 ssize_t ret = -EBADF;
848 file = fget_light(fd, &fput_needed);
851 if (file->f_mode & FMODE_PREAD)
852 ret = vfs_readv(file, vec, vlen, &pos);
853 fput_light(file, fput_needed);
857 add_rchar(current, ret);
862 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
863 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
865 loff_t pos = pos_from_hilo(pos_h, pos_l);
867 ssize_t ret = -EBADF;
873 file = fget_light(fd, &fput_needed);
876 if (file->f_mode & FMODE_PWRITE)
877 ret = vfs_writev(file, vec, vlen, &pos);
878 fput_light(file, fput_needed);
882 add_wchar(current, ret);
887 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
888 size_t count, loff_t max)
890 struct file * in_file, * out_file;
891 struct inode * in_inode, * out_inode;
894 int fput_needed_in, fput_needed_out, fl;
897 * Get input file, and verify that it is ok..
900 in_file = fget_light(in_fd, &fput_needed_in);
903 if (!(in_file->f_mode & FMODE_READ))
907 ppos = &in_file->f_pos;
909 if (!(in_file->f_mode & FMODE_PREAD))
911 retval = rw_verify_area(READ, in_file, ppos, count);
917 * Get output file, and verify that it is ok..
920 out_file = fget_light(out_fd, &fput_needed_out);
923 if (!(out_file->f_mode & FMODE_WRITE))
926 in_inode = in_file->f_path.dentry->d_inode;
927 out_inode = out_file->f_path.dentry->d_inode;
928 retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
934 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
937 if (unlikely(pos + count > max)) {
947 * We need to debate whether we can enable this or not. The
948 * man page documents EAGAIN return for the output at least,
949 * and the application is arguably buggy if it doesn't expect
950 * EAGAIN on a non-blocking file descriptor.
952 if (in_file->f_flags & O_NONBLOCK)
953 fl = SPLICE_F_NONBLOCK;
955 retval = do_splice_direct(in_file, ppos, out_file, count, fl);
958 add_rchar(current, retval);
959 add_wchar(current, retval);
968 fput_light(out_file, fput_needed_out);
970 fput_light(in_file, fput_needed_in);
975 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
982 if (unlikely(get_user(off, offset)))
985 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
986 if (unlikely(put_user(pos, offset)))
991 return do_sendfile(out_fd, in_fd, NULL, count, 0);
994 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
1000 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1002 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1003 if (unlikely(put_user(pos, offset)))
1008 return do_sendfile(out_fd, in_fd, NULL, count, 0);