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 <linux/splice.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);
35 __negative_fpos_check(struct file *file, loff_t pos, size_t count)
38 * pos or pos+count is negative here, check overflow.
39 * too big "count" will be caught in rw_verify_area().
41 if ((pos < 0) && (pos + count < pos))
43 if (file->f_mode & FMODE_UNSIGNED_OFFSET)
49 * generic_file_llseek_unlocked - lockless generic llseek implementation
50 * @file: file structure to seek on
51 * @offset: file offset to seek to
52 * @origin: type of seek
54 * Updates the file offset to the value specified by @offset and @origin.
55 * Locking must be provided by the caller.
58 generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin)
60 struct inode *inode = file->f_mapping->host;
64 offset += inode->i_size;
68 * Here we special-case the lseek(fd, 0, SEEK_CUR)
69 * position-querying operation. Avoid rewriting the "same"
70 * f_pos value back to the file because a concurrent read(),
71 * write() or lseek() might have altered it
75 offset += file->f_pos;
79 if (offset < 0 && __negative_fpos_check(file, offset, 0))
81 if (offset > inode->i_sb->s_maxbytes)
84 /* Special lock needed here? */
85 if (offset != file->f_pos) {
92 EXPORT_SYMBOL(generic_file_llseek_unlocked);
95 * generic_file_llseek - generic llseek implementation for regular files
96 * @file: file structure to seek on
97 * @offset: file offset to seek to
98 * @origin: type of seek
100 * This is a generic implemenation of ->llseek useable for all normal local
101 * filesystems. It just updates the file offset to the value specified by
102 * @offset and @origin under i_mutex.
104 loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
108 mutex_lock(&file->f_dentry->d_inode->i_mutex);
109 rval = generic_file_llseek_unlocked(file, offset, origin);
110 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
114 EXPORT_SYMBOL(generic_file_llseek);
117 * noop_llseek - No Operation Performed llseek implementation
118 * @file: file structure to seek on
119 * @offset: file offset to seek to
120 * @origin: type of seek
122 * This is an implementation of ->llseek useable for the rare special case when
123 * userspace expects the seek to succeed but the (device) file is actually not
124 * able to perform the seek. In this case you use noop_llseek() instead of
125 * falling back to the default implementation of ->llseek.
127 loff_t noop_llseek(struct file *file, loff_t offset, int origin)
131 EXPORT_SYMBOL(noop_llseek);
133 loff_t no_llseek(struct file *file, loff_t offset, int origin)
137 EXPORT_SYMBOL(no_llseek);
139 loff_t default_llseek(struct file *file, loff_t offset, int origin)
143 mutex_lock(&file->f_dentry->d_inode->i_mutex);
146 offset += i_size_read(file->f_path.dentry->d_inode);
150 retval = file->f_pos;
153 offset += file->f_pos;
156 if (offset >= 0 || !__negative_fpos_check(file, offset, 0)) {
157 if (offset != file->f_pos) {
158 file->f_pos = offset;
164 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
167 EXPORT_SYMBOL(default_llseek);
169 loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
171 loff_t (*fn)(struct file *, loff_t, int);
174 if (file->f_mode & FMODE_LSEEK) {
175 if (file->f_op && file->f_op->llseek)
176 fn = file->f_op->llseek;
178 return fn(file, offset, origin);
180 EXPORT_SYMBOL(vfs_llseek);
182 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin)
189 file = fget_light(fd, &fput_needed);
194 if (origin <= SEEK_MAX) {
195 loff_t res = vfs_llseek(file, offset, origin);
197 if (res != (loff_t)retval)
198 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
200 fput_light(file, fput_needed);
205 #ifdef __ARCH_WANT_SYS_LLSEEK
206 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
207 unsigned long, offset_low, loff_t __user *, result,
208 unsigned int, origin)
216 file = fget_light(fd, &fput_needed);
221 if (origin > SEEK_MAX)
224 offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
227 retval = (int)offset;
230 if (!copy_to_user(result, &offset, sizeof(offset)))
234 fput_light(file, fput_needed);
242 * rw_verify_area doesn't like huge counts. We limit
243 * them to something that fits in "int" so that others
244 * won't have to do range checks all the time.
246 #define MAX_RW_COUNT (INT_MAX & PAGE_CACHE_MASK)
248 int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
252 int retval = -EINVAL;
254 inode = file->f_path.dentry->d_inode;
255 if (unlikely((ssize_t) count < 0))
258 if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
259 retval = __negative_fpos_check(file, pos, count);
264 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
265 retval = locks_mandatory_area(
266 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
267 inode, file, pos, count);
271 retval = security_file_permission(file,
272 read_write == READ ? MAY_READ : MAY_WRITE);
275 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
278 static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
280 set_current_state(TASK_UNINTERRUPTIBLE);
281 if (!kiocbIsKicked(iocb))
284 kiocbClearKicked(iocb);
285 __set_current_state(TASK_RUNNING);
288 ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
290 struct iovec iov = { .iov_base = buf, .iov_len = len };
294 init_sync_kiocb(&kiocb, filp);
295 kiocb.ki_pos = *ppos;
297 kiocb.ki_nbytes = len;
300 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
301 if (ret != -EIOCBRETRY)
303 wait_on_retry_sync_kiocb(&kiocb);
306 if (-EIOCBQUEUED == ret)
307 ret = wait_on_sync_kiocb(&kiocb);
308 *ppos = kiocb.ki_pos;
312 EXPORT_SYMBOL(do_sync_read);
314 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
318 if (!(file->f_mode & FMODE_READ))
320 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
322 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
325 ret = rw_verify_area(READ, file, pos, count);
328 if (file->f_op->read)
329 ret = file->f_op->read(file, buf, count, pos);
331 ret = do_sync_read(file, buf, count, pos);
333 fsnotify_access(file);
334 add_rchar(current, ret);
342 EXPORT_SYMBOL(vfs_read);
344 ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
346 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
350 init_sync_kiocb(&kiocb, filp);
351 kiocb.ki_pos = *ppos;
353 kiocb.ki_nbytes = len;
356 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
357 if (ret != -EIOCBRETRY)
359 wait_on_retry_sync_kiocb(&kiocb);
362 if (-EIOCBQUEUED == ret)
363 ret = wait_on_sync_kiocb(&kiocb);
364 *ppos = kiocb.ki_pos;
368 EXPORT_SYMBOL(do_sync_write);
370 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
374 if (!(file->f_mode & FMODE_WRITE))
376 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
378 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
381 ret = rw_verify_area(WRITE, file, pos, count);
384 if (file->f_op->write)
385 ret = file->f_op->write(file, buf, count, pos);
387 ret = do_sync_write(file, buf, count, pos);
389 fsnotify_modify(file);
390 add_wchar(current, ret);
398 EXPORT_SYMBOL(vfs_write);
400 static inline loff_t file_pos_read(struct file *file)
405 static inline void file_pos_write(struct file *file, loff_t pos)
410 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
413 ssize_t ret = -EBADF;
416 file = fget_light(fd, &fput_needed);
418 loff_t pos = file_pos_read(file);
419 ret = vfs_read(file, buf, count, &pos);
420 file_pos_write(file, pos);
421 fput_light(file, fput_needed);
427 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
431 ssize_t ret = -EBADF;
434 file = fget_light(fd, &fput_needed);
436 loff_t pos = file_pos_read(file);
437 ret = vfs_write(file, buf, count, &pos);
438 file_pos_write(file, pos);
439 fput_light(file, fput_needed);
445 SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
446 size_t count, loff_t pos)
449 ssize_t ret = -EBADF;
455 file = fget_light(fd, &fput_needed);
458 if (file->f_mode & FMODE_PREAD)
459 ret = vfs_read(file, buf, count, &pos);
460 fput_light(file, fput_needed);
465 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
466 asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
468 return SYSC_pread64((unsigned int) fd, (char __user *) buf,
469 (size_t) count, pos);
471 SYSCALL_ALIAS(sys_pread64, SyS_pread64);
474 SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
475 size_t count, loff_t pos)
478 ssize_t ret = -EBADF;
484 file = fget_light(fd, &fput_needed);
487 if (file->f_mode & FMODE_PWRITE)
488 ret = vfs_write(file, buf, count, &pos);
489 fput_light(file, fput_needed);
494 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
495 asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
497 return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
498 (size_t) count, pos);
500 SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
504 * Reduce an iovec's length in-place. Return the resulting number of segments
506 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
508 unsigned long seg = 0;
511 while (seg < nr_segs) {
513 if (len + iov->iov_len >= to) {
514 iov->iov_len = to - len;
522 EXPORT_SYMBOL(iov_shorten);
524 ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
525 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
530 init_sync_kiocb(&kiocb, filp);
531 kiocb.ki_pos = *ppos;
533 kiocb.ki_nbytes = len;
536 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
537 if (ret != -EIOCBRETRY)
539 wait_on_retry_sync_kiocb(&kiocb);
542 if (ret == -EIOCBQUEUED)
543 ret = wait_on_sync_kiocb(&kiocb);
544 *ppos = kiocb.ki_pos;
548 /* Do it by hand, with file-ops */
549 ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
550 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
552 struct iovec *vector = iov;
555 while (nr_segs > 0) {
560 base = vector->iov_base;
561 len = vector->iov_len;
565 nr = fn(filp, base, len, ppos);
580 /* A write operation does a read from user space and vice versa */
581 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
583 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
584 unsigned long nr_segs, unsigned long fast_segs,
585 struct iovec *fast_pointer,
586 struct iovec **ret_pointer)
590 struct iovec *iov = fast_pointer;
593 * SuS says "The readv() function *may* fail if the iovcnt argument
594 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
595 * traditionally returned zero for zero segments, so...
603 * First get the "struct iovec" from user memory and
604 * verify all the pointers
606 if (nr_segs > UIO_MAXIOV) {
610 if (nr_segs > fast_segs) {
611 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
617 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
623 * According to the Single Unix Specification we should return EINVAL
624 * if an element length is < 0 when cast to ssize_t or if the
625 * total length would overflow the ssize_t return value of the
629 for (seg = 0; seg < nr_segs; seg++) {
630 void __user *buf = iov[seg].iov_base;
631 ssize_t len = (ssize_t)iov[seg].iov_len;
633 /* see if we we're about to use an invalid len or if
634 * it's about to overflow ssize_t */
635 if (len < 0 || (ret + len < ret)) {
639 if (unlikely(!access_ok(vrfy_dir(type), buf, len))) {
651 static ssize_t do_readv_writev(int type, struct file *file,
652 const struct iovec __user * uvector,
653 unsigned long nr_segs, loff_t *pos)
656 struct iovec iovstack[UIO_FASTIOV];
657 struct iovec *iov = iovstack;
667 ret = rw_copy_check_uvector(type, uvector, nr_segs,
668 ARRAY_SIZE(iovstack), iovstack, &iov);
673 ret = rw_verify_area(type, file, pos, tot_len);
679 fn = file->f_op->read;
680 fnv = file->f_op->aio_read;
682 fn = (io_fn_t)file->f_op->write;
683 fnv = file->f_op->aio_write;
687 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
690 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
695 if ((ret + (type == READ)) > 0) {
697 fsnotify_access(file);
699 fsnotify_modify(file);
704 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
705 unsigned long vlen, loff_t *pos)
707 if (!(file->f_mode & FMODE_READ))
709 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
712 return do_readv_writev(READ, file, vec, vlen, pos);
715 EXPORT_SYMBOL(vfs_readv);
717 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
718 unsigned long vlen, loff_t *pos)
720 if (!(file->f_mode & FMODE_WRITE))
722 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
725 return do_readv_writev(WRITE, file, vec, vlen, pos);
728 EXPORT_SYMBOL(vfs_writev);
730 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
734 ssize_t ret = -EBADF;
737 file = fget_light(fd, &fput_needed);
739 loff_t pos = file_pos_read(file);
740 ret = vfs_readv(file, vec, vlen, &pos);
741 file_pos_write(file, pos);
742 fput_light(file, fput_needed);
746 add_rchar(current, ret);
751 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
755 ssize_t ret = -EBADF;
758 file = fget_light(fd, &fput_needed);
760 loff_t pos = file_pos_read(file);
761 ret = vfs_writev(file, vec, vlen, &pos);
762 file_pos_write(file, pos);
763 fput_light(file, fput_needed);
767 add_wchar(current, ret);
772 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
774 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
775 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
778 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
779 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
781 loff_t pos = pos_from_hilo(pos_h, pos_l);
783 ssize_t ret = -EBADF;
789 file = fget_light(fd, &fput_needed);
792 if (file->f_mode & FMODE_PREAD)
793 ret = vfs_readv(file, vec, vlen, &pos);
794 fput_light(file, fput_needed);
798 add_rchar(current, ret);
803 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
804 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
806 loff_t pos = pos_from_hilo(pos_h, pos_l);
808 ssize_t ret = -EBADF;
814 file = fget_light(fd, &fput_needed);
817 if (file->f_mode & FMODE_PWRITE)
818 ret = vfs_writev(file, vec, vlen, &pos);
819 fput_light(file, fput_needed);
823 add_wchar(current, ret);
828 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
829 size_t count, loff_t max)
831 struct file * in_file, * out_file;
832 struct inode * in_inode, * out_inode;
835 int fput_needed_in, fput_needed_out, fl;
838 * Get input file, and verify that it is ok..
841 in_file = fget_light(in_fd, &fput_needed_in);
844 if (!(in_file->f_mode & FMODE_READ))
848 ppos = &in_file->f_pos;
850 if (!(in_file->f_mode & FMODE_PREAD))
852 retval = rw_verify_area(READ, in_file, ppos, count);
858 * Get output file, and verify that it is ok..
861 out_file = fget_light(out_fd, &fput_needed_out);
864 if (!(out_file->f_mode & FMODE_WRITE))
867 in_inode = in_file->f_path.dentry->d_inode;
868 out_inode = out_file->f_path.dentry->d_inode;
869 retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
875 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
878 if (unlikely(pos + count > max)) {
888 * We need to debate whether we can enable this or not. The
889 * man page documents EAGAIN return for the output at least,
890 * and the application is arguably buggy if it doesn't expect
891 * EAGAIN on a non-blocking file descriptor.
893 if (in_file->f_flags & O_NONBLOCK)
894 fl = SPLICE_F_NONBLOCK;
896 retval = do_splice_direct(in_file, ppos, out_file, count, fl);
899 add_rchar(current, retval);
900 add_wchar(current, retval);
909 fput_light(out_file, fput_needed_out);
911 fput_light(in_file, fput_needed_in);
916 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
923 if (unlikely(get_user(off, offset)))
926 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
927 if (unlikely(put_user(pos, offset)))
932 return do_sendfile(out_fd, in_fd, NULL, count, 0);
935 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
941 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
943 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
944 if (unlikely(put_user(pos, offset)))
949 return do_sendfile(out_fd, in_fd, NULL, count, 0);