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>
19 #include <asm/uaccess.h>
20 #include <asm/unistd.h>
22 const struct file_operations generic_ro_fops = {
23 .llseek = generic_file_llseek,
24 .read = generic_file_read,
25 .mmap = generic_file_readonly_mmap,
26 .sendfile = generic_file_sendfile,
29 EXPORT_SYMBOL(generic_ro_fops);
31 loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
34 struct inode *inode = file->f_mapping->host;
36 mutex_lock(&inode->i_mutex);
39 offset += inode->i_size;
42 offset += file->f_pos;
45 if (offset>=0 && offset<=inode->i_sb->s_maxbytes) {
46 if (offset != file->f_pos) {
52 mutex_unlock(&inode->i_mutex);
56 EXPORT_SYMBOL(generic_file_llseek);
58 loff_t remote_llseek(struct file *file, loff_t offset, int origin)
65 offset += i_size_read(file->f_dentry->d_inode);
68 offset += file->f_pos;
71 if (offset>=0 && offset<=file->f_dentry->d_inode->i_sb->s_maxbytes) {
72 if (offset != file->f_pos) {
81 EXPORT_SYMBOL(remote_llseek);
83 loff_t no_llseek(struct file *file, loff_t offset, int origin)
87 EXPORT_SYMBOL(no_llseek);
89 loff_t default_llseek(struct file *file, loff_t offset, int origin)
96 offset += i_size_read(file->f_dentry->d_inode);
99 offset += file->f_pos;
103 if (offset != file->f_pos) {
104 file->f_pos = offset;
112 EXPORT_SYMBOL(default_llseek);
114 loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
116 loff_t (*fn)(struct file *, loff_t, int);
119 if (file->f_mode & FMODE_LSEEK) {
121 if (file->f_op && file->f_op->llseek)
122 fn = file->f_op->llseek;
124 return fn(file, offset, origin);
126 EXPORT_SYMBOL(vfs_llseek);
128 asmlinkage off_t sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
135 file = fget_light(fd, &fput_needed);
141 loff_t res = vfs_llseek(file, offset, origin);
143 if (res != (loff_t)retval)
144 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
146 fput_light(file, fput_needed);
151 #ifdef __ARCH_WANT_SYS_LLSEEK
152 asmlinkage long sys_llseek(unsigned int fd, unsigned long offset_high,
153 unsigned long offset_low, loff_t __user * result,
162 file = fget_light(fd, &fput_needed);
170 offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
173 retval = (int)offset;
176 if (!copy_to_user(result, &offset, sizeof(offset)))
180 fput_light(file, fput_needed);
187 * rw_verify_area doesn't like huge counts. We limit
188 * them to something that fits in "int" so that others
189 * won't have to do range checks all the time.
191 #define MAX_RW_COUNT (INT_MAX & PAGE_CACHE_MASK)
193 int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
198 if (unlikely((ssize_t) count < 0))
201 if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
204 inode = file->f_dentry->d_inode;
205 if (unlikely(inode->i_flock && MANDATORY_LOCK(inode))) {
206 int retval = locks_mandatory_area(
207 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
208 inode, file, pos, count);
212 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
218 static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
220 set_current_state(TASK_UNINTERRUPTIBLE);
221 if (!kiocbIsKicked(iocb))
224 kiocbClearKicked(iocb);
225 __set_current_state(TASK_RUNNING);
228 ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
230 struct iovec iov = { .iov_base = buf, .iov_len = len };
234 init_sync_kiocb(&kiocb, filp);
235 kiocb.ki_pos = *ppos;
239 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
240 if (ret != -EIOCBRETRY)
242 wait_on_retry_sync_kiocb(&kiocb);
245 if (-EIOCBQUEUED == ret)
246 ret = wait_on_sync_kiocb(&kiocb);
247 *ppos = kiocb.ki_pos;
251 EXPORT_SYMBOL(do_sync_read);
253 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
257 if (!(file->f_mode & FMODE_READ))
259 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
261 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
264 ret = rw_verify_area(READ, file, pos, count);
267 ret = security_file_permission (file, MAY_READ);
269 if (file->f_op->read)
270 ret = file->f_op->read(file, buf, count, pos);
272 ret = do_sync_read(file, buf, count, pos);
274 fsnotify_access(file->f_dentry);
275 current->rchar += ret;
284 EXPORT_SYMBOL(vfs_read);
286 ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
288 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
292 init_sync_kiocb(&kiocb, filp);
293 kiocb.ki_pos = *ppos;
297 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
298 if (ret != -EIOCBRETRY)
300 wait_on_retry_sync_kiocb(&kiocb);
303 if (-EIOCBQUEUED == ret)
304 ret = wait_on_sync_kiocb(&kiocb);
305 *ppos = kiocb.ki_pos;
309 EXPORT_SYMBOL(do_sync_write);
311 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
315 if (!(file->f_mode & FMODE_WRITE))
317 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
319 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
322 ret = rw_verify_area(WRITE, file, pos, count);
325 ret = security_file_permission (file, MAY_WRITE);
327 if (file->f_op->write)
328 ret = file->f_op->write(file, buf, count, pos);
330 ret = do_sync_write(file, buf, count, pos);
332 fsnotify_modify(file->f_dentry);
333 current->wchar += ret;
342 EXPORT_SYMBOL(vfs_write);
344 static inline loff_t file_pos_read(struct file *file)
349 static inline void file_pos_write(struct file *file, loff_t pos)
354 asmlinkage ssize_t sys_read(unsigned int fd, char __user * buf, size_t count)
357 ssize_t ret = -EBADF;
360 file = fget_light(fd, &fput_needed);
362 loff_t pos = file_pos_read(file);
363 ret = vfs_read(file, buf, count, &pos);
364 file_pos_write(file, pos);
365 fput_light(file, fput_needed);
370 EXPORT_SYMBOL_GPL(sys_read);
372 asmlinkage ssize_t sys_write(unsigned int fd, const char __user * buf, size_t count)
375 ssize_t ret = -EBADF;
378 file = fget_light(fd, &fput_needed);
380 loff_t pos = file_pos_read(file);
381 ret = vfs_write(file, buf, count, &pos);
382 file_pos_write(file, pos);
383 fput_light(file, fput_needed);
389 asmlinkage ssize_t sys_pread64(unsigned int fd, char __user *buf,
390 size_t count, loff_t pos)
393 ssize_t ret = -EBADF;
399 file = fget_light(fd, &fput_needed);
402 if (file->f_mode & FMODE_PREAD)
403 ret = vfs_read(file, buf, count, &pos);
404 fput_light(file, fput_needed);
410 asmlinkage ssize_t sys_pwrite64(unsigned int fd, const char __user *buf,
411 size_t count, loff_t pos)
414 ssize_t ret = -EBADF;
420 file = fget_light(fd, &fput_needed);
423 if (file->f_mode & FMODE_PWRITE)
424 ret = vfs_write(file, buf, count, &pos);
425 fput_light(file, fput_needed);
432 * Reduce an iovec's length in-place. Return the resulting number of segments
434 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
436 unsigned long seg = 0;
439 while (seg < nr_segs) {
441 if (len + iov->iov_len >= to) {
442 iov->iov_len = to - len;
451 EXPORT_UNUSED_SYMBOL(iov_shorten); /* June 2006 */
453 /* A write operation does a read from user space and vice versa */
454 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
456 static ssize_t do_readv_writev(int type, struct file *file,
457 const struct iovec __user * uvector,
458 unsigned long nr_segs, loff_t *pos)
460 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
461 typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
464 struct iovec iovstack[UIO_FASTIOV];
465 struct iovec *iov=iovstack, *vector;
472 * SuS says "The readv() function *may* fail if the iovcnt argument
473 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
474 * traditionally returned zero for zero segments, so...
481 * First get the "struct iovec" from user memory and
482 * verify all the pointers
485 if (nr_segs > UIO_MAXIOV)
489 if (nr_segs > UIO_FASTIOV) {
491 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
496 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector)))
500 * Single unix specification:
501 * We should -EINVAL if an element length is not >= 0 and fitting an
502 * ssize_t. The total length is fitting an ssize_t
504 * Be careful here because iov_len is a size_t not an ssize_t
508 for (seg = 0; seg < nr_segs; seg++) {
509 void __user *buf = iov[seg].iov_base;
510 ssize_t len = (ssize_t)iov[seg].iov_len;
512 if (len < 0) /* size_t not fitting an ssize_t .. */
514 if (unlikely(!access_ok(vrfy_dir(type), buf, len)))
517 if ((ssize_t)tot_len < 0) /* maths overflow on the ssize_t */
525 ret = rw_verify_area(type, file, pos, tot_len);
528 ret = security_file_permission(file, type == READ ? MAY_READ : MAY_WRITE);
534 fn = file->f_op->read;
535 fnv = file->f_op->readv;
537 fn = (io_fn_t)file->f_op->write;
538 fnv = file->f_op->writev;
541 ret = fnv(file, iov, nr_segs, pos);
545 /* Do it by hand, with file-ops */
548 while (nr_segs > 0) {
553 base = vector->iov_base;
554 len = vector->iov_len;
558 nr = fn(file, base, len, pos);
571 if ((ret + (type == READ)) > 0) {
573 fsnotify_access(file->f_dentry);
575 fsnotify_modify(file->f_dentry);
583 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
584 unsigned long vlen, loff_t *pos)
586 if (!(file->f_mode & FMODE_READ))
588 if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
591 return do_readv_writev(READ, file, vec, vlen, pos);
594 EXPORT_SYMBOL(vfs_readv);
596 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
597 unsigned long vlen, loff_t *pos)
599 if (!(file->f_mode & FMODE_WRITE))
601 if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
604 return do_readv_writev(WRITE, file, vec, vlen, pos);
607 EXPORT_SYMBOL(vfs_writev);
610 sys_readv(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
613 ssize_t ret = -EBADF;
616 file = fget_light(fd, &fput_needed);
618 loff_t pos = file_pos_read(file);
619 ret = vfs_readv(file, vec, vlen, &pos);
620 file_pos_write(file, pos);
621 fput_light(file, fput_needed);
625 current->rchar += ret;
631 sys_writev(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
634 ssize_t ret = -EBADF;
637 file = fget_light(fd, &fput_needed);
639 loff_t pos = file_pos_read(file);
640 ret = vfs_writev(file, vec, vlen, &pos);
641 file_pos_write(file, pos);
642 fput_light(file, fput_needed);
646 current->wchar += ret;
651 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
652 size_t count, loff_t max)
654 struct file * in_file, * out_file;
655 struct inode * in_inode, * out_inode;
658 int fput_needed_in, fput_needed_out;
661 * Get input file, and verify that it is ok..
664 in_file = fget_light(in_fd, &fput_needed_in);
667 if (!(in_file->f_mode & FMODE_READ))
670 in_inode = in_file->f_dentry->d_inode;
673 if (!in_file->f_op || !in_file->f_op->sendfile)
677 ppos = &in_file->f_pos;
679 if (!(in_file->f_mode & FMODE_PREAD))
681 retval = rw_verify_area(READ, in_file, ppos, count);
686 retval = security_file_permission (in_file, MAY_READ);
691 * Get output file, and verify that it is ok..
694 out_file = fget_light(out_fd, &fput_needed_out);
697 if (!(out_file->f_mode & FMODE_WRITE))
700 if (!out_file->f_op || !out_file->f_op->sendpage)
702 out_inode = out_file->f_dentry->d_inode;
703 retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
708 retval = security_file_permission (out_file, MAY_WRITE);
713 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
717 if (unlikely(pos < 0))
719 if (unlikely(pos + count > max)) {
726 retval = in_file->f_op->sendfile(in_file, ppos, count, file_send_actor, out_file);
729 current->rchar += retval;
730 current->wchar += retval;
739 fput_light(out_file, fput_needed_out);
741 fput_light(in_file, fput_needed_in);
746 asmlinkage ssize_t sys_sendfile(int out_fd, int in_fd, off_t __user *offset, size_t count)
753 if (unlikely(get_user(off, offset)))
756 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
757 if (unlikely(put_user(pos, offset)))
762 return do_sendfile(out_fd, in_fd, NULL, count, 0);
765 asmlinkage ssize_t sys_sendfile64(int out_fd, int in_fd, loff_t __user *offset, size_t count)
771 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
773 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
774 if (unlikely(put_user(pos, offset)))
779 return do_sendfile(out_fd, in_fd, NULL, count, 0);