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/module.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 file system
60 * This is a variant of generic_file_llseek that allows passing in a custom
64 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
65 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
66 * read/writes behave like SEEK_SET against seeks.
69 generic_file_llseek_size(struct file *file, loff_t offset, int origin,
72 struct inode *inode = file->f_mapping->host;
76 offset += i_size_read(inode);
80 * Here we special-case the lseek(fd, 0, SEEK_CUR)
81 * position-querying operation. Avoid rewriting the "same"
82 * f_pos value back to the file because a concurrent read(),
83 * write() or lseek() might have altered it
88 * f_lock protects against read/modify/write race with other
89 * SEEK_CURs. Note that parallel writes and reads behave
92 spin_lock(&file->f_lock);
93 offset = lseek_execute(file, inode, file->f_pos + offset,
95 spin_unlock(&file->f_lock);
99 * In the generic case the entire file is data, so as long as
100 * offset isn't at the end of the file then the offset is data.
102 if (offset >= i_size_read(inode))
107 * There is a virtual hole at the end of the file, so as long as
108 * offset isn't i_size or larger, return i_size.
110 if (offset >= i_size_read(inode))
112 offset = i_size_read(inode);
116 return lseek_execute(file, inode, offset, maxsize);
118 EXPORT_SYMBOL(generic_file_llseek_size);
121 * generic_file_llseek - generic llseek implementation for regular files
122 * @file: file structure to seek on
123 * @offset: file offset to seek to
124 * @origin: type of seek
126 * This is a generic implemenation of ->llseek useable for all normal local
127 * filesystems. It just updates the file offset to the value specified by
128 * @offset and @origin under i_mutex.
130 loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
132 struct inode *inode = file->f_mapping->host;
134 return generic_file_llseek_size(file, offset, origin,
135 inode->i_sb->s_maxbytes);
137 EXPORT_SYMBOL(generic_file_llseek);
140 * noop_llseek - No Operation Performed llseek implementation
141 * @file: file structure to seek on
142 * @offset: file offset to seek to
143 * @origin: type of seek
145 * This is an implementation of ->llseek useable for the rare special case when
146 * userspace expects the seek to succeed but the (device) file is actually not
147 * able to perform the seek. In this case you use noop_llseek() instead of
148 * falling back to the default implementation of ->llseek.
150 loff_t noop_llseek(struct file *file, loff_t offset, int origin)
154 EXPORT_SYMBOL(noop_llseek);
156 loff_t no_llseek(struct file *file, loff_t offset, int origin)
160 EXPORT_SYMBOL(no_llseek);
162 loff_t default_llseek(struct file *file, loff_t offset, int origin)
164 struct inode *inode = file->f_path.dentry->d_inode;
167 mutex_lock(&inode->i_mutex);
170 offset += i_size_read(inode);
174 retval = file->f_pos;
177 offset += file->f_pos;
181 * In the generic case the entire file is data, so as
182 * long as offset isn't at the end of the file then the
185 if (offset >= inode->i_size) {
192 * There is a virtual hole at the end of the file, so
193 * as long as offset isn't i_size or larger, return
196 if (offset >= inode->i_size) {
200 offset = inode->i_size;
204 if (offset >= 0 || unsigned_offsets(file)) {
205 if (offset != file->f_pos) {
206 file->f_pos = offset;
212 mutex_unlock(&inode->i_mutex);
215 EXPORT_SYMBOL(default_llseek);
217 loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
219 loff_t (*fn)(struct file *, loff_t, int);
222 if (file->f_mode & FMODE_LSEEK) {
223 if (file->f_op && file->f_op->llseek)
224 fn = file->f_op->llseek;
226 return fn(file, offset, origin);
228 EXPORT_SYMBOL(vfs_llseek);
230 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin)
237 file = fget_light(fd, &fput_needed);
242 if (origin <= SEEK_MAX) {
243 loff_t res = vfs_llseek(file, offset, origin);
245 if (res != (loff_t)retval)
246 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
248 fput_light(file, fput_needed);
253 #ifdef __ARCH_WANT_SYS_LLSEEK
254 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
255 unsigned long, offset_low, loff_t __user *, result,
256 unsigned int, origin)
264 file = fget_light(fd, &fput_needed);
269 if (origin > SEEK_MAX)
272 offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
275 retval = (int)offset;
278 if (!copy_to_user(result, &offset, sizeof(offset)))
282 fput_light(file, fput_needed);
290 * rw_verify_area doesn't like huge counts. We limit
291 * them to something that fits in "int" so that others
292 * won't have to do range checks all the time.
294 int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
298 int retval = -EINVAL;
300 inode = file->f_path.dentry->d_inode;
301 if (unlikely((ssize_t) count < 0))
304 if (unlikely(pos < 0)) {
305 if (!unsigned_offsets(file))
307 if (count >= -pos) /* both values are in 0..LLONG_MAX */
309 } else if (unlikely((loff_t) (pos + count) < 0)) {
310 if (!unsigned_offsets(file))
314 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
315 retval = locks_mandatory_area(
316 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
317 inode, file, pos, count);
321 retval = security_file_permission(file,
322 read_write == READ ? MAY_READ : MAY_WRITE);
325 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
328 static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
330 set_current_state(TASK_UNINTERRUPTIBLE);
331 if (!kiocbIsKicked(iocb))
334 kiocbClearKicked(iocb);
335 __set_current_state(TASK_RUNNING);
338 ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
340 struct iovec iov = { .iov_base = buf, .iov_len = len };
344 init_sync_kiocb(&kiocb, filp);
345 kiocb.ki_pos = *ppos;
347 kiocb.ki_nbytes = len;
350 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
351 if (ret != -EIOCBRETRY)
353 wait_on_retry_sync_kiocb(&kiocb);
356 if (-EIOCBQUEUED == ret)
357 ret = wait_on_sync_kiocb(&kiocb);
358 *ppos = kiocb.ki_pos;
362 EXPORT_SYMBOL(do_sync_read);
364 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
368 if (!(file->f_mode & FMODE_READ))
370 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
372 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
375 ret = rw_verify_area(READ, file, pos, count);
378 if (file->f_op->read)
379 ret = file->f_op->read(file, buf, count, pos);
381 ret = do_sync_read(file, buf, count, pos);
383 fsnotify_access(file);
384 add_rchar(current, ret);
392 EXPORT_SYMBOL(vfs_read);
394 ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
396 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
400 init_sync_kiocb(&kiocb, filp);
401 kiocb.ki_pos = *ppos;
403 kiocb.ki_nbytes = len;
406 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
407 if (ret != -EIOCBRETRY)
409 wait_on_retry_sync_kiocb(&kiocb);
412 if (-EIOCBQUEUED == ret)
413 ret = wait_on_sync_kiocb(&kiocb);
414 *ppos = kiocb.ki_pos;
418 EXPORT_SYMBOL(do_sync_write);
420 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
424 if (!(file->f_mode & FMODE_WRITE))
426 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
428 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
431 ret = rw_verify_area(WRITE, file, pos, count);
434 if (file->f_op->write)
435 ret = file->f_op->write(file, buf, count, pos);
437 ret = do_sync_write(file, buf, count, pos);
439 fsnotify_modify(file);
440 add_wchar(current, ret);
448 EXPORT_SYMBOL(vfs_write);
450 static inline loff_t file_pos_read(struct file *file)
455 static inline void file_pos_write(struct file *file, loff_t pos)
460 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
463 ssize_t ret = -EBADF;
466 file = fget_light(fd, &fput_needed);
468 loff_t pos = file_pos_read(file);
469 ret = vfs_read(file, buf, count, &pos);
470 file_pos_write(file, pos);
471 fput_light(file, fput_needed);
477 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
481 ssize_t ret = -EBADF;
484 file = fget_light(fd, &fput_needed);
486 loff_t pos = file_pos_read(file);
487 ret = vfs_write(file, buf, count, &pos);
488 file_pos_write(file, pos);
489 fput_light(file, fput_needed);
495 SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
496 size_t count, loff_t pos)
499 ssize_t ret = -EBADF;
505 file = fget_light(fd, &fput_needed);
508 if (file->f_mode & FMODE_PREAD)
509 ret = vfs_read(file, buf, count, &pos);
510 fput_light(file, fput_needed);
515 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
516 asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
518 return SYSC_pread64((unsigned int) fd, (char __user *) buf,
519 (size_t) count, pos);
521 SYSCALL_ALIAS(sys_pread64, SyS_pread64);
524 SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
525 size_t count, loff_t pos)
528 ssize_t ret = -EBADF;
534 file = fget_light(fd, &fput_needed);
537 if (file->f_mode & FMODE_PWRITE)
538 ret = vfs_write(file, buf, count, &pos);
539 fput_light(file, fput_needed);
544 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
545 asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
547 return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
548 (size_t) count, pos);
550 SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
554 * Reduce an iovec's length in-place. Return the resulting number of segments
556 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
558 unsigned long seg = 0;
561 while (seg < nr_segs) {
563 if (len + iov->iov_len >= to) {
564 iov->iov_len = to - len;
572 EXPORT_SYMBOL(iov_shorten);
574 ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
575 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
580 init_sync_kiocb(&kiocb, filp);
581 kiocb.ki_pos = *ppos;
583 kiocb.ki_nbytes = len;
586 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
587 if (ret != -EIOCBRETRY)
589 wait_on_retry_sync_kiocb(&kiocb);
592 if (ret == -EIOCBQUEUED)
593 ret = wait_on_sync_kiocb(&kiocb);
594 *ppos = kiocb.ki_pos;
598 /* Do it by hand, with file-ops */
599 ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
600 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
602 struct iovec *vector = iov;
605 while (nr_segs > 0) {
610 base = vector->iov_base;
611 len = vector->iov_len;
615 nr = fn(filp, base, len, ppos);
630 /* A write operation does a read from user space and vice versa */
631 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
633 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
634 unsigned long nr_segs, unsigned long fast_segs,
635 struct iovec *fast_pointer,
636 struct iovec **ret_pointer,
641 struct iovec *iov = fast_pointer;
644 * SuS says "The readv() function *may* fail if the iovcnt argument
645 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
646 * traditionally returned zero for zero segments, so...
654 * First get the "struct iovec" from user memory and
655 * verify all the pointers
657 if (nr_segs > UIO_MAXIOV) {
661 if (nr_segs > fast_segs) {
662 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
668 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
674 * According to the Single Unix Specification we should return EINVAL
675 * if an element length is < 0 when cast to ssize_t or if the
676 * total length would overflow the ssize_t return value of the
679 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
683 for (seg = 0; seg < nr_segs; seg++) {
684 void __user *buf = iov[seg].iov_base;
685 ssize_t len = (ssize_t)iov[seg].iov_len;
687 /* see if we we're about to use an invalid len or if
688 * it's about to overflow ssize_t */
694 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
698 if (len > MAX_RW_COUNT - ret) {
699 len = MAX_RW_COUNT - ret;
700 iov[seg].iov_len = len;
709 static ssize_t do_readv_writev(int type, struct file *file,
710 const struct iovec __user * uvector,
711 unsigned long nr_segs, loff_t *pos)
714 struct iovec iovstack[UIO_FASTIOV];
715 struct iovec *iov = iovstack;
725 ret = rw_copy_check_uvector(type, uvector, nr_segs,
726 ARRAY_SIZE(iovstack), iovstack, &iov, 1);
731 ret = rw_verify_area(type, file, pos, tot_len);
737 fn = file->f_op->read;
738 fnv = file->f_op->aio_read;
740 fn = (io_fn_t)file->f_op->write;
741 fnv = file->f_op->aio_write;
745 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
748 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
753 if ((ret + (type == READ)) > 0) {
755 fsnotify_access(file);
757 fsnotify_modify(file);
762 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
763 unsigned long vlen, loff_t *pos)
765 if (!(file->f_mode & FMODE_READ))
767 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
770 return do_readv_writev(READ, file, vec, vlen, pos);
773 EXPORT_SYMBOL(vfs_readv);
775 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
776 unsigned long vlen, loff_t *pos)
778 if (!(file->f_mode & FMODE_WRITE))
780 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
783 return do_readv_writev(WRITE, file, vec, vlen, pos);
786 EXPORT_SYMBOL(vfs_writev);
788 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
792 ssize_t ret = -EBADF;
795 file = fget_light(fd, &fput_needed);
797 loff_t pos = file_pos_read(file);
798 ret = vfs_readv(file, vec, vlen, &pos);
799 file_pos_write(file, pos);
800 fput_light(file, fput_needed);
804 add_rchar(current, ret);
809 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
813 ssize_t ret = -EBADF;
816 file = fget_light(fd, &fput_needed);
818 loff_t pos = file_pos_read(file);
819 ret = vfs_writev(file, vec, vlen, &pos);
820 file_pos_write(file, pos);
821 fput_light(file, fput_needed);
825 add_wchar(current, ret);
830 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
832 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
833 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
836 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
837 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
839 loff_t pos = pos_from_hilo(pos_h, pos_l);
841 ssize_t ret = -EBADF;
847 file = fget_light(fd, &fput_needed);
850 if (file->f_mode & FMODE_PREAD)
851 ret = vfs_readv(file, vec, vlen, &pos);
852 fput_light(file, fput_needed);
856 add_rchar(current, ret);
861 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
862 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
864 loff_t pos = pos_from_hilo(pos_h, pos_l);
866 ssize_t ret = -EBADF;
872 file = fget_light(fd, &fput_needed);
875 if (file->f_mode & FMODE_PWRITE)
876 ret = vfs_writev(file, vec, vlen, &pos);
877 fput_light(file, fput_needed);
881 add_wchar(current, ret);
886 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
887 size_t count, loff_t max)
889 struct file * in_file, * out_file;
890 struct inode * in_inode, * out_inode;
893 int fput_needed_in, fput_needed_out, fl;
896 * Get input file, and verify that it is ok..
899 in_file = fget_light(in_fd, &fput_needed_in);
902 if (!(in_file->f_mode & FMODE_READ))
906 ppos = &in_file->f_pos;
908 if (!(in_file->f_mode & FMODE_PREAD))
910 retval = rw_verify_area(READ, in_file, ppos, count);
916 * Get output file, and verify that it is ok..
919 out_file = fget_light(out_fd, &fput_needed_out);
922 if (!(out_file->f_mode & FMODE_WRITE))
925 in_inode = in_file->f_path.dentry->d_inode;
926 out_inode = out_file->f_path.dentry->d_inode;
927 retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
933 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
936 if (unlikely(pos + count > max)) {
946 * We need to debate whether we can enable this or not. The
947 * man page documents EAGAIN return for the output at least,
948 * and the application is arguably buggy if it doesn't expect
949 * EAGAIN on a non-blocking file descriptor.
951 if (in_file->f_flags & O_NONBLOCK)
952 fl = SPLICE_F_NONBLOCK;
954 retval = do_splice_direct(in_file, ppos, out_file, count, fl);
957 add_rchar(current, retval);
958 add_wchar(current, retval);
967 fput_light(out_file, fput_needed_out);
969 fput_light(in_file, fput_needed_in);
974 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
981 if (unlikely(get_user(off, offset)))
984 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
985 if (unlikely(put_user(pos, offset)))
990 return do_sendfile(out_fd, in_fd, NULL, count, 0);
993 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
999 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1001 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1002 if (unlikely(put_user(pos, offset)))
1007 return do_sendfile(out_fd, in_fd, NULL, count, 0);