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"
21 #include <asm/uaccess.h>
22 #include <asm/unistd.h>
24 const struct file_operations generic_ro_fops = {
25 .llseek = generic_file_llseek,
27 .aio_read = generic_file_aio_read,
28 .mmap = generic_file_readonly_mmap,
29 .splice_read = generic_file_splice_read,
32 EXPORT_SYMBOL(generic_ro_fops);
34 static inline int unsigned_offsets(struct file *file)
36 return file->f_mode & FMODE_UNSIGNED_OFFSET;
39 static loff_t lseek_execute(struct file *file, struct inode *inode,
40 loff_t offset, loff_t maxsize)
42 if (offset < 0 && !unsigned_offsets(file))
47 if (offset != file->f_pos) {
55 * generic_file_llseek_size - generic llseek implementation for regular files
56 * @file: file structure to seek on
57 * @offset: file offset to seek to
58 * @whence: type of seek
59 * @size: max size of this file in file system
60 * @eof: offset used for SEEK_END position
62 * This is a variant of generic_file_llseek that allows passing in a custom
63 * maximum file size and a custom EOF position, for e.g. hashed directories
66 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
67 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
68 * read/writes behave like SEEK_SET against seeks.
71 generic_file_llseek_size(struct file *file, loff_t offset, int whence,
72 loff_t maxsize, loff_t eof)
74 struct inode *inode = file->f_mapping->host;
82 * Here we special-case the lseek(fd, 0, SEEK_CUR)
83 * position-querying operation. Avoid rewriting the "same"
84 * f_pos value back to the file because a concurrent read(),
85 * write() or lseek() might have altered it
90 * f_lock protects against read/modify/write race with other
91 * SEEK_CURs. Note that parallel writes and reads behave
94 spin_lock(&file->f_lock);
95 offset = lseek_execute(file, inode, file->f_pos + offset,
97 spin_unlock(&file->f_lock);
101 * In the generic case the entire file is data, so as long as
102 * offset isn't at the end of the file then the offset is data.
109 * There is a virtual hole at the end of the file, so as long as
110 * offset isn't i_size or larger, return i_size.
118 return lseek_execute(file, inode, offset, maxsize);
120 EXPORT_SYMBOL(generic_file_llseek_size);
123 * generic_file_llseek - generic llseek implementation for regular files
124 * @file: file structure to seek on
125 * @offset: file offset to seek to
126 * @whence: type of seek
128 * This is a generic implemenation of ->llseek useable for all normal local
129 * filesystems. It just updates the file offset to the value specified by
130 * @offset and @whence under i_mutex.
132 loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
134 struct inode *inode = file->f_mapping->host;
136 return generic_file_llseek_size(file, offset, whence,
137 inode->i_sb->s_maxbytes,
140 EXPORT_SYMBOL(generic_file_llseek);
143 * noop_llseek - No Operation Performed llseek implementation
144 * @file: file structure to seek on
145 * @offset: file offset to seek to
146 * @whence: type of seek
148 * This is an implementation of ->llseek useable for the rare special case when
149 * userspace expects the seek to succeed but the (device) file is actually not
150 * able to perform the seek. In this case you use noop_llseek() instead of
151 * falling back to the default implementation of ->llseek.
153 loff_t noop_llseek(struct file *file, loff_t offset, int whence)
157 EXPORT_SYMBOL(noop_llseek);
159 loff_t no_llseek(struct file *file, loff_t offset, int whence)
163 EXPORT_SYMBOL(no_llseek);
165 loff_t default_llseek(struct file *file, loff_t offset, int whence)
167 struct inode *inode = file_inode(file);
170 mutex_lock(&inode->i_mutex);
173 offset += i_size_read(inode);
177 retval = file->f_pos;
180 offset += file->f_pos;
184 * In the generic case the entire file is data, so as
185 * long as offset isn't at the end of the file then the
188 if (offset >= inode->i_size) {
195 * There is a virtual hole at the end of the file, so
196 * as long as offset isn't i_size or larger, return
199 if (offset >= inode->i_size) {
203 offset = inode->i_size;
207 if (offset >= 0 || unsigned_offsets(file)) {
208 if (offset != file->f_pos) {
209 file->f_pos = offset;
215 mutex_unlock(&inode->i_mutex);
218 EXPORT_SYMBOL(default_llseek);
220 loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
222 loff_t (*fn)(struct file *, loff_t, int);
225 if (file->f_mode & FMODE_LSEEK) {
226 if (file->f_op && file->f_op->llseek)
227 fn = file->f_op->llseek;
229 return fn(file, offset, whence);
231 EXPORT_SYMBOL(vfs_llseek);
233 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
236 struct fd f = fdget(fd);
241 if (whence <= SEEK_MAX) {
242 loff_t res = vfs_llseek(f.file, offset, whence);
244 if (res != (loff_t)retval)
245 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
252 COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
254 return sys_lseek(fd, offset, whence);
258 #ifdef __ARCH_WANT_SYS_LLSEEK
259 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
260 unsigned long, offset_low, loff_t __user *, result,
261 unsigned int, whence)
264 struct fd f = fdget(fd);
271 if (whence > SEEK_MAX)
274 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
277 retval = (int)offset;
280 if (!copy_to_user(result, &offset, sizeof(offset)))
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_inode(file);
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)
462 struct fd f = fdget(fd);
463 ssize_t ret = -EBADF;
466 loff_t pos = file_pos_read(f.file);
467 ret = vfs_read(f.file, buf, count, &pos);
468 file_pos_write(f.file, pos);
474 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
477 struct fd f = fdget(fd);
478 ssize_t ret = -EBADF;
481 loff_t pos = file_pos_read(f.file);
482 ret = vfs_write(f.file, buf, count, &pos);
483 file_pos_write(f.file, pos);
490 SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
491 size_t, count, loff_t, pos)
494 ssize_t ret = -EBADF;
502 if (f.file->f_mode & FMODE_PREAD)
503 ret = vfs_read(f.file, buf, count, &pos);
510 SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
511 size_t, count, loff_t, pos)
514 ssize_t ret = -EBADF;
522 if (f.file->f_mode & FMODE_PWRITE)
523 ret = vfs_write(f.file, buf, count, &pos);
531 * Reduce an iovec's length in-place. Return the resulting number of segments
533 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
535 unsigned long seg = 0;
538 while (seg < nr_segs) {
540 if (len + iov->iov_len >= to) {
541 iov->iov_len = to - len;
549 EXPORT_SYMBOL(iov_shorten);
551 ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
552 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
557 init_sync_kiocb(&kiocb, filp);
558 kiocb.ki_pos = *ppos;
560 kiocb.ki_nbytes = len;
563 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
564 if (ret != -EIOCBRETRY)
566 wait_on_retry_sync_kiocb(&kiocb);
569 if (ret == -EIOCBQUEUED)
570 ret = wait_on_sync_kiocb(&kiocb);
571 *ppos = kiocb.ki_pos;
575 /* Do it by hand, with file-ops */
576 ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
577 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
579 struct iovec *vector = iov;
582 while (nr_segs > 0) {
587 base = vector->iov_base;
588 len = vector->iov_len;
592 nr = fn(filp, base, len, ppos);
607 /* A write operation does a read from user space and vice versa */
608 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
610 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
611 unsigned long nr_segs, unsigned long fast_segs,
612 struct iovec *fast_pointer,
613 struct iovec **ret_pointer)
617 struct iovec *iov = fast_pointer;
620 * SuS says "The readv() function *may* fail if the iovcnt argument
621 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
622 * traditionally returned zero for zero segments, so...
630 * First get the "struct iovec" from user memory and
631 * verify all the pointers
633 if (nr_segs > UIO_MAXIOV) {
637 if (nr_segs > fast_segs) {
638 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
644 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
650 * According to the Single Unix Specification we should return EINVAL
651 * if an element length is < 0 when cast to ssize_t or if the
652 * total length would overflow the ssize_t return value of the
655 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
659 for (seg = 0; seg < nr_segs; seg++) {
660 void __user *buf = iov[seg].iov_base;
661 ssize_t len = (ssize_t)iov[seg].iov_len;
663 /* see if we we're about to use an invalid len or if
664 * it's about to overflow ssize_t */
670 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
674 if (len > MAX_RW_COUNT - ret) {
675 len = MAX_RW_COUNT - ret;
676 iov[seg].iov_len = len;
685 static ssize_t do_readv_writev(int type, struct file *file,
686 const struct iovec __user * uvector,
687 unsigned long nr_segs, loff_t *pos)
690 struct iovec iovstack[UIO_FASTIOV];
691 struct iovec *iov = iovstack;
701 ret = rw_copy_check_uvector(type, uvector, nr_segs,
702 ARRAY_SIZE(iovstack), iovstack, &iov);
707 ret = rw_verify_area(type, file, pos, tot_len);
713 fn = file->f_op->read;
714 fnv = file->f_op->aio_read;
716 fn = (io_fn_t)file->f_op->write;
717 fnv = file->f_op->aio_write;
721 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
724 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
729 if ((ret + (type == READ)) > 0) {
731 fsnotify_access(file);
733 fsnotify_modify(file);
738 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
739 unsigned long vlen, loff_t *pos)
741 if (!(file->f_mode & FMODE_READ))
743 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
746 return do_readv_writev(READ, file, vec, vlen, pos);
749 EXPORT_SYMBOL(vfs_readv);
751 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
752 unsigned long vlen, loff_t *pos)
754 if (!(file->f_mode & FMODE_WRITE))
756 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
759 return do_readv_writev(WRITE, file, vec, vlen, pos);
762 EXPORT_SYMBOL(vfs_writev);
764 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
767 struct fd f = fdget(fd);
768 ssize_t ret = -EBADF;
771 loff_t pos = file_pos_read(f.file);
772 ret = vfs_readv(f.file, vec, vlen, &pos);
773 file_pos_write(f.file, pos);
778 add_rchar(current, ret);
783 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
786 struct fd f = fdget(fd);
787 ssize_t ret = -EBADF;
790 loff_t pos = file_pos_read(f.file);
791 ret = vfs_writev(f.file, vec, vlen, &pos);
792 file_pos_write(f.file, pos);
797 add_wchar(current, ret);
802 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
804 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
805 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
808 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
809 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
811 loff_t pos = pos_from_hilo(pos_h, pos_l);
813 ssize_t ret = -EBADF;
821 if (f.file->f_mode & FMODE_PREAD)
822 ret = vfs_readv(f.file, vec, vlen, &pos);
827 add_rchar(current, ret);
832 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
833 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
835 loff_t pos = pos_from_hilo(pos_h, pos_l);
837 ssize_t ret = -EBADF;
845 if (f.file->f_mode & FMODE_PWRITE)
846 ret = vfs_writev(f.file, vec, vlen, &pos);
851 add_wchar(current, ret);
856 ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, size_t count,
860 struct inode *in_inode, *out_inode;
866 * Get input file, and verify that it is ok..
872 if (!(in.file->f_mode & FMODE_READ))
876 ppos = &in.file->f_pos;
878 if (!(in.file->f_mode & FMODE_PREAD))
880 retval = rw_verify_area(READ, in.file, ppos, count);
886 * Get output file, and verify that it is ok..
892 if (!(out.file->f_mode & FMODE_WRITE))
895 in_inode = file_inode(in.file);
896 out_inode = file_inode(out.file);
897 retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count);
903 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
906 if (unlikely(pos + count > max)) {
916 * We need to debate whether we can enable this or not. The
917 * man page documents EAGAIN return for the output at least,
918 * and the application is arguably buggy if it doesn't expect
919 * EAGAIN on a non-blocking file descriptor.
921 if (in.file->f_flags & O_NONBLOCK)
922 fl = SPLICE_F_NONBLOCK;
924 retval = do_splice_direct(in.file, ppos, out.file, count, fl);
927 add_rchar(current, retval);
928 add_wchar(current, retval);
929 fsnotify_access(in.file);
930 fsnotify_modify(out.file);
946 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
953 if (unlikely(get_user(off, offset)))
956 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
957 if (unlikely(put_user(pos, offset)))
962 return do_sendfile(out_fd, in_fd, NULL, count, 0);
965 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
971 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
973 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
974 if (unlikely(put_user(pos, offset)))
979 return do_sendfile(out_fd, in_fd, NULL, count, 0);