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 <linux/compat.h>
19 #include "read_write.h"
22 #include <asm/uaccess.h>
23 #include <asm/unistd.h>
25 const struct file_operations generic_ro_fops = {
26 .llseek = generic_file_llseek,
28 .aio_read = generic_file_aio_read,
29 .mmap = generic_file_readonly_mmap,
30 .splice_read = generic_file_splice_read,
33 EXPORT_SYMBOL(generic_ro_fops);
35 static inline int unsigned_offsets(struct file *file)
37 return file->f_mode & FMODE_UNSIGNED_OFFSET;
40 static loff_t lseek_execute(struct file *file, struct inode *inode,
41 loff_t offset, loff_t maxsize)
43 if (offset < 0 && !unsigned_offsets(file))
48 if (offset != file->f_pos) {
56 * generic_file_llseek_size - generic llseek implementation for regular files
57 * @file: file structure to seek on
58 * @offset: file offset to seek to
59 * @whence: type of seek
60 * @size: max size of this file in file system
61 * @eof: offset used for SEEK_END position
63 * This is a variant of generic_file_llseek that allows passing in a custom
64 * maximum file size and a custom EOF position, for e.g. hashed directories
67 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
68 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
69 * read/writes behave like SEEK_SET against seeks.
72 generic_file_llseek_size(struct file *file, loff_t offset, int whence,
73 loff_t maxsize, loff_t eof)
75 struct inode *inode = file->f_mapping->host;
83 * Here we special-case the lseek(fd, 0, SEEK_CUR)
84 * position-querying operation. Avoid rewriting the "same"
85 * f_pos value back to the file because a concurrent read(),
86 * write() or lseek() might have altered it
91 * f_lock protects against read/modify/write race with other
92 * SEEK_CURs. Note that parallel writes and reads behave
95 spin_lock(&file->f_lock);
96 offset = lseek_execute(file, inode, file->f_pos + offset,
98 spin_unlock(&file->f_lock);
102 * In the generic case the entire file is data, so as long as
103 * offset isn't at the end of the file then the offset is data.
110 * There is a virtual hole at the end of the file, so as long as
111 * offset isn't i_size or larger, return i_size.
119 return lseek_execute(file, inode, offset, maxsize);
121 EXPORT_SYMBOL(generic_file_llseek_size);
124 * generic_file_llseek - generic llseek implementation for regular files
125 * @file: file structure to seek on
126 * @offset: file offset to seek to
127 * @whence: type of seek
129 * This is a generic implemenation of ->llseek useable for all normal local
130 * filesystems. It just updates the file offset to the value specified by
131 * @offset and @whence under i_mutex.
133 loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
135 struct inode *inode = file->f_mapping->host;
137 return generic_file_llseek_size(file, offset, whence,
138 inode->i_sb->s_maxbytes,
141 EXPORT_SYMBOL(generic_file_llseek);
144 * noop_llseek - No Operation Performed llseek implementation
145 * @file: file structure to seek on
146 * @offset: file offset to seek to
147 * @whence: type of seek
149 * This is an implementation of ->llseek useable for the rare special case when
150 * userspace expects the seek to succeed but the (device) file is actually not
151 * able to perform the seek. In this case you use noop_llseek() instead of
152 * falling back to the default implementation of ->llseek.
154 loff_t noop_llseek(struct file *file, loff_t offset, int whence)
158 EXPORT_SYMBOL(noop_llseek);
160 loff_t no_llseek(struct file *file, loff_t offset, int whence)
164 EXPORT_SYMBOL(no_llseek);
166 loff_t default_llseek(struct file *file, loff_t offset, int whence)
168 struct inode *inode = file_inode(file);
171 mutex_lock(&inode->i_mutex);
174 offset += i_size_read(inode);
178 retval = file->f_pos;
181 offset += file->f_pos;
185 * In the generic case the entire file is data, so as
186 * long as offset isn't at the end of the file then the
189 if (offset >= inode->i_size) {
196 * There is a virtual hole at the end of the file, so
197 * as long as offset isn't i_size or larger, return
200 if (offset >= inode->i_size) {
204 offset = inode->i_size;
208 if (offset >= 0 || unsigned_offsets(file)) {
209 if (offset != file->f_pos) {
210 file->f_pos = offset;
216 mutex_unlock(&inode->i_mutex);
219 EXPORT_SYMBOL(default_llseek);
221 loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
223 loff_t (*fn)(struct file *, loff_t, int);
226 if (file->f_mode & FMODE_LSEEK) {
227 if (file->f_op && file->f_op->llseek)
228 fn = file->f_op->llseek;
230 return fn(file, offset, whence);
232 EXPORT_SYMBOL(vfs_llseek);
234 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
237 struct fd f = fdget(fd);
242 if (whence <= SEEK_MAX) {
243 loff_t res = vfs_llseek(f.file, offset, whence);
245 if (res != (loff_t)retval)
246 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
253 COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
255 return sys_lseek(fd, offset, whence);
259 #ifdef __ARCH_WANT_SYS_LLSEEK
260 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
261 unsigned long, offset_low, loff_t __user *, result,
262 unsigned int, whence)
265 struct fd f = fdget(fd);
272 if (whence > SEEK_MAX)
275 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
278 retval = (int)offset;
281 if (!copy_to_user(result, &offset, sizeof(offset)))
291 * rw_verify_area doesn't like huge counts. We limit
292 * them to something that fits in "int" so that others
293 * won't have to do range checks all the time.
295 int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
299 int retval = -EINVAL;
301 inode = file_inode(file);
302 if (unlikely((ssize_t) count < 0))
305 if (unlikely(pos < 0)) {
306 if (!unsigned_offsets(file))
308 if (count >= -pos) /* both values are in 0..LLONG_MAX */
310 } else if (unlikely((loff_t) (pos + count) < 0)) {
311 if (!unsigned_offsets(file))
315 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
316 retval = locks_mandatory_area(
317 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
318 inode, file, pos, count);
322 retval = security_file_permission(file,
323 read_write == READ ? MAY_READ : MAY_WRITE);
326 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
329 static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
331 set_current_state(TASK_UNINTERRUPTIBLE);
332 if (!kiocbIsKicked(iocb))
335 kiocbClearKicked(iocb);
336 __set_current_state(TASK_RUNNING);
339 ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
341 struct iovec iov = { .iov_base = buf, .iov_len = len };
345 init_sync_kiocb(&kiocb, filp);
346 kiocb.ki_pos = *ppos;
348 kiocb.ki_nbytes = len;
351 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
352 if (ret != -EIOCBRETRY)
354 wait_on_retry_sync_kiocb(&kiocb);
357 if (-EIOCBQUEUED == ret)
358 ret = wait_on_sync_kiocb(&kiocb);
359 *ppos = kiocb.ki_pos;
363 EXPORT_SYMBOL(do_sync_read);
365 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
369 if (!(file->f_mode & FMODE_READ))
371 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
373 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
376 ret = rw_verify_area(READ, file, pos, count);
379 if (file->f_op->read)
380 ret = file->f_op->read(file, buf, count, pos);
382 ret = do_sync_read(file, buf, count, pos);
384 fsnotify_access(file);
385 add_rchar(current, ret);
393 EXPORT_SYMBOL(vfs_read);
395 ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
397 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
401 file_start_write(filp);
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;
417 file_end_write(filp);
421 EXPORT_SYMBOL(do_sync_write);
423 ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
426 const char __user *p;
431 p = (__force const char __user *)buf;
432 if (count > MAX_RW_COUNT)
433 count = MAX_RW_COUNT;
434 if (file->f_op->write)
435 ret = file->f_op->write(file, p, count, pos);
437 ret = do_sync_write(file, p, count, pos);
440 fsnotify_modify(file);
441 add_wchar(current, ret);
447 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
451 if (!(file->f_mode & FMODE_WRITE))
453 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
455 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
458 ret = rw_verify_area(WRITE, file, pos, count);
461 if (file->f_op->write)
462 ret = file->f_op->write(file, buf, count, pos);
464 ret = do_sync_write(file, buf, count, pos);
466 fsnotify_modify(file);
467 add_wchar(current, ret);
475 EXPORT_SYMBOL(vfs_write);
477 static inline loff_t file_pos_read(struct file *file)
482 static inline void file_pos_write(struct file *file, loff_t pos)
487 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
489 struct fd f = fdget(fd);
490 ssize_t ret = -EBADF;
493 loff_t pos = file_pos_read(f.file);
494 ret = vfs_read(f.file, buf, count, &pos);
495 file_pos_write(f.file, pos);
501 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
504 struct fd f = fdget(fd);
505 ssize_t ret = -EBADF;
508 loff_t pos = file_pos_read(f.file);
509 ret = vfs_write(f.file, buf, count, &pos);
510 file_pos_write(f.file, pos);
517 SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
518 size_t count, loff_t pos)
521 ssize_t ret = -EBADF;
529 if (f.file->f_mode & FMODE_PREAD)
530 ret = vfs_read(f.file, buf, count, &pos);
536 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
537 asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
539 return SYSC_pread64((unsigned int) fd, (char __user *) buf,
540 (size_t) count, pos);
542 SYSCALL_ALIAS(sys_pread64, SyS_pread64);
545 SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
546 size_t count, loff_t pos)
549 ssize_t ret = -EBADF;
557 if (f.file->f_mode & FMODE_PWRITE)
558 ret = vfs_write(f.file, buf, count, &pos);
564 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
565 asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
567 return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
568 (size_t) count, pos);
570 SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
574 * Reduce an iovec's length in-place. Return the resulting number of segments
576 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
578 unsigned long seg = 0;
581 while (seg < nr_segs) {
583 if (len + iov->iov_len >= to) {
584 iov->iov_len = to - len;
592 EXPORT_SYMBOL(iov_shorten);
594 ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
595 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
600 init_sync_kiocb(&kiocb, filp);
601 kiocb.ki_pos = *ppos;
603 kiocb.ki_nbytes = len;
606 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
607 if (ret != -EIOCBRETRY)
609 wait_on_retry_sync_kiocb(&kiocb);
612 if (ret == -EIOCBQUEUED)
613 ret = wait_on_sync_kiocb(&kiocb);
614 *ppos = kiocb.ki_pos;
618 /* Do it by hand, with file-ops */
619 ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
620 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
622 struct iovec *vector = iov;
625 while (nr_segs > 0) {
630 base = vector->iov_base;
631 len = vector->iov_len;
635 nr = fn(filp, base, len, ppos);
650 /* A write operation does a read from user space and vice versa */
651 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
653 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
654 unsigned long nr_segs, unsigned long fast_segs,
655 struct iovec *fast_pointer,
656 struct iovec **ret_pointer)
660 struct iovec *iov = fast_pointer;
663 * SuS says "The readv() function *may* fail if the iovcnt argument
664 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
665 * traditionally returned zero for zero segments, so...
673 * First get the "struct iovec" from user memory and
674 * verify all the pointers
676 if (nr_segs > UIO_MAXIOV) {
680 if (nr_segs > fast_segs) {
681 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
687 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
693 * According to the Single Unix Specification we should return EINVAL
694 * if an element length is < 0 when cast to ssize_t or if the
695 * total length would overflow the ssize_t return value of the
698 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
702 for (seg = 0; seg < nr_segs; seg++) {
703 void __user *buf = iov[seg].iov_base;
704 ssize_t len = (ssize_t)iov[seg].iov_len;
706 /* see if we we're about to use an invalid len or if
707 * it's about to overflow ssize_t */
713 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
717 if (len > MAX_RW_COUNT - ret) {
718 len = MAX_RW_COUNT - ret;
719 iov[seg].iov_len = len;
728 static ssize_t do_readv_writev(int type, struct file *file,
729 const struct iovec __user * uvector,
730 unsigned long nr_segs, loff_t *pos)
733 struct iovec iovstack[UIO_FASTIOV];
734 struct iovec *iov = iovstack;
744 ret = rw_copy_check_uvector(type, uvector, nr_segs,
745 ARRAY_SIZE(iovstack), iovstack, &iov);
750 ret = rw_verify_area(type, file, pos, tot_len);
756 fn = file->f_op->read;
757 fnv = file->f_op->aio_read;
759 fn = (io_fn_t)file->f_op->write;
760 fnv = file->f_op->aio_write;
764 file_start_write(file);
765 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
767 file_end_write(file);
769 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
774 if ((ret + (type == READ)) > 0) {
776 fsnotify_access(file);
778 fsnotify_modify(file);
783 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
784 unsigned long vlen, loff_t *pos)
786 if (!(file->f_mode & FMODE_READ))
788 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
791 return do_readv_writev(READ, file, vec, vlen, pos);
794 EXPORT_SYMBOL(vfs_readv);
796 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
797 unsigned long vlen, loff_t *pos)
799 if (!(file->f_mode & FMODE_WRITE))
801 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
804 return do_readv_writev(WRITE, file, vec, vlen, pos);
807 EXPORT_SYMBOL(vfs_writev);
809 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
812 struct fd f = fdget(fd);
813 ssize_t ret = -EBADF;
816 loff_t pos = file_pos_read(f.file);
817 ret = vfs_readv(f.file, vec, vlen, &pos);
818 file_pos_write(f.file, pos);
823 add_rchar(current, ret);
828 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
831 struct fd f = fdget(fd);
832 ssize_t ret = -EBADF;
835 loff_t pos = file_pos_read(f.file);
836 ret = vfs_writev(f.file, vec, vlen, &pos);
837 file_pos_write(f.file, pos);
842 add_wchar(current, ret);
847 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
849 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
850 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
853 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
854 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
856 loff_t pos = pos_from_hilo(pos_h, pos_l);
858 ssize_t ret = -EBADF;
866 if (f.file->f_mode & FMODE_PREAD)
867 ret = vfs_readv(f.file, vec, vlen, &pos);
872 add_rchar(current, ret);
877 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
878 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
880 loff_t pos = pos_from_hilo(pos_h, pos_l);
882 ssize_t ret = -EBADF;
890 if (f.file->f_mode & FMODE_PWRITE)
891 ret = vfs_writev(f.file, vec, vlen, &pos);
896 add_wchar(current, ret);
901 ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, size_t count,
905 struct inode *in_inode, *out_inode;
911 * Get input file, and verify that it is ok..
917 if (!(in.file->f_mode & FMODE_READ))
921 ppos = &in.file->f_pos;
923 if (!(in.file->f_mode & FMODE_PREAD))
925 retval = rw_verify_area(READ, in.file, ppos, count);
931 * Get output file, and verify that it is ok..
937 if (!(out.file->f_mode & FMODE_WRITE))
940 in_inode = file_inode(in.file);
941 out_inode = file_inode(out.file);
942 retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count);
948 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
951 if (unlikely(pos + count > max)) {
961 * We need to debate whether we can enable this or not. The
962 * man page documents EAGAIN return for the output at least,
963 * and the application is arguably buggy if it doesn't expect
964 * EAGAIN on a non-blocking file descriptor.
966 if (in.file->f_flags & O_NONBLOCK)
967 fl = SPLICE_F_NONBLOCK;
969 retval = do_splice_direct(in.file, ppos, out.file, count, fl);
972 add_rchar(current, retval);
973 add_wchar(current, retval);
974 fsnotify_access(in.file);
975 fsnotify_modify(out.file);
991 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
998 if (unlikely(get_user(off, offset)))
1001 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1002 if (unlikely(put_user(pos, offset)))
1007 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1010 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
1016 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1018 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1019 if (unlikely(put_user(pos, offset)))
1024 return do_sendfile(out_fd, in_fd, NULL, count, 0);