Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/read_write.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 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> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame] | 12 | #include <linux/aio.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 13 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/security.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 15 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/syscalls.h> |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 17 | #include <linux/pagemap.h> |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 18 | #include <linux/splice.h> |
Al Viro | 561c673 | 2013-02-24 10:52:26 -0500 | [diff] [blame] | 19 | #include <linux/compat.h> |
Al Viro | 06ae43f | 2013-03-20 13:19:30 -0400 | [diff] [blame] | 20 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <asm/uaccess.h> |
| 23 | #include <asm/unistd.h> |
| 24 | |
Al Viro | c0bd14af | 2013-05-04 15:00:54 -0400 | [diff] [blame] | 25 | typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *); |
| 26 | typedef ssize_t (*iov_fn_t)(struct kiocb *, const struct iovec *, |
| 27 | unsigned long, loff_t); |
| 28 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 29 | const struct file_operations generic_ro_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | .llseek = generic_file_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 31 | .read = do_sync_read, |
| 32 | .aio_read = generic_file_aio_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | .mmap = generic_file_readonly_mmap, |
Jens Axboe | 534f2aa | 2007-06-01 14:52:37 +0200 | [diff] [blame] | 34 | .splice_read = generic_file_splice_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | EXPORT_SYMBOL(generic_ro_fops); |
| 38 | |
Al Viro | cccb5a1 | 2010-12-17 07:44:05 -0500 | [diff] [blame] | 39 | static inline int unsigned_offsets(struct file *file) |
KAMEZAWA Hiroyuki | 4a3956c | 2010-10-01 14:20:22 -0700 | [diff] [blame] | 40 | { |
Al Viro | cccb5a1 | 2010-12-17 07:44:05 -0500 | [diff] [blame] | 41 | return file->f_mode & FMODE_UNSIGNED_OFFSET; |
KAMEZAWA Hiroyuki | 4a3956c | 2010-10-01 14:20:22 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 44 | static loff_t lseek_execute(struct file *file, struct inode *inode, |
| 45 | loff_t offset, loff_t maxsize) |
| 46 | { |
| 47 | if (offset < 0 && !unsigned_offsets(file)) |
| 48 | return -EINVAL; |
| 49 | if (offset > maxsize) |
| 50 | return -EINVAL; |
| 51 | |
| 52 | if (offset != file->f_pos) { |
| 53 | file->f_pos = offset; |
| 54 | file->f_version = 0; |
| 55 | } |
| 56 | return offset; |
| 57 | } |
| 58 | |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 59 | /** |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 60 | * generic_file_llseek_size - generic llseek implementation for regular files |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 61 | * @file: file structure to seek on |
| 62 | * @offset: file offset to seek to |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 63 | * @whence: type of seek |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 64 | * @size: max size of this file in file system |
| 65 | * @eof: offset used for SEEK_END position |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 66 | * |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 67 | * This is a variant of generic_file_llseek that allows passing in a custom |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 68 | * maximum file size and a custom EOF position, for e.g. hashed directories |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 69 | * |
| 70 | * Synchronization: |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 71 | * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms) |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 72 | * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes. |
| 73 | * read/writes behave like SEEK_SET against seeks. |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 74 | */ |
Andi Kleen | 9465efc | 2008-06-27 11:05:24 +0200 | [diff] [blame] | 75 | loff_t |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 76 | generic_file_llseek_size(struct file *file, loff_t offset, int whence, |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 77 | loff_t maxsize, loff_t eof) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | struct inode *inode = file->f_mapping->host; |
| 80 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 81 | switch (whence) { |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 82 | case SEEK_END: |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 83 | offset += eof; |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 84 | break; |
| 85 | case SEEK_CUR: |
Alain Knaff | 5b6f1eb | 2008-11-10 17:08:08 -0800 | [diff] [blame] | 86 | /* |
| 87 | * Here we special-case the lseek(fd, 0, SEEK_CUR) |
| 88 | * position-querying operation. Avoid rewriting the "same" |
| 89 | * f_pos value back to the file because a concurrent read(), |
| 90 | * write() or lseek() might have altered it |
| 91 | */ |
| 92 | if (offset == 0) |
| 93 | return file->f_pos; |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 94 | /* |
| 95 | * f_lock protects against read/modify/write race with other |
| 96 | * SEEK_CURs. Note that parallel writes and reads behave |
| 97 | * like SEEK_SET. |
| 98 | */ |
| 99 | spin_lock(&file->f_lock); |
| 100 | offset = lseek_execute(file, inode, file->f_pos + offset, |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 101 | maxsize); |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 102 | spin_unlock(&file->f_lock); |
| 103 | return offset; |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 104 | case SEEK_DATA: |
| 105 | /* |
| 106 | * In the generic case the entire file is data, so as long as |
| 107 | * offset isn't at the end of the file then the offset is data. |
| 108 | */ |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 109 | if (offset >= eof) |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 110 | return -ENXIO; |
| 111 | break; |
| 112 | case SEEK_HOLE: |
| 113 | /* |
| 114 | * There is a virtual hole at the end of the file, so as long as |
| 115 | * offset isn't i_size or larger, return i_size. |
| 116 | */ |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 117 | if (offset >= eof) |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 118 | return -ENXIO; |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 119 | offset = eof; |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 120 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 122 | |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 123 | return lseek_execute(file, inode, offset, maxsize); |
| 124 | } |
| 125 | EXPORT_SYMBOL(generic_file_llseek_size); |
| 126 | |
| 127 | /** |
| 128 | * generic_file_llseek - generic llseek implementation for regular files |
| 129 | * @file: file structure to seek on |
| 130 | * @offset: file offset to seek to |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 131 | * @whence: type of seek |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 132 | * |
| 133 | * This is a generic implemenation of ->llseek useable for all normal local |
| 134 | * filesystems. It just updates the file offset to the value specified by |
Ming Lei | 546ae2d | 2013-04-29 15:06:07 -0700 | [diff] [blame] | 135 | * @offset and @whence. |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 136 | */ |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 137 | loff_t generic_file_llseek(struct file *file, loff_t offset, int whence) |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 138 | { |
| 139 | struct inode *inode = file->f_mapping->host; |
| 140 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 141 | return generic_file_llseek_size(file, offset, whence, |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 142 | inode->i_sb->s_maxbytes, |
| 143 | i_size_read(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
Andi Kleen | 9465efc | 2008-06-27 11:05:24 +0200 | [diff] [blame] | 145 | EXPORT_SYMBOL(generic_file_llseek); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
jan Blunck | ae6afc3 | 2010-05-26 14:44:48 -0700 | [diff] [blame] | 147 | /** |
| 148 | * noop_llseek - No Operation Performed llseek implementation |
| 149 | * @file: file structure to seek on |
| 150 | * @offset: file offset to seek to |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 151 | * @whence: type of seek |
jan Blunck | ae6afc3 | 2010-05-26 14:44:48 -0700 | [diff] [blame] | 152 | * |
| 153 | * This is an implementation of ->llseek useable for the rare special case when |
| 154 | * userspace expects the seek to succeed but the (device) file is actually not |
| 155 | * able to perform the seek. In this case you use noop_llseek() instead of |
| 156 | * falling back to the default implementation of ->llseek. |
| 157 | */ |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 158 | loff_t noop_llseek(struct file *file, loff_t offset, int whence) |
jan Blunck | ae6afc3 | 2010-05-26 14:44:48 -0700 | [diff] [blame] | 159 | { |
| 160 | return file->f_pos; |
| 161 | } |
| 162 | EXPORT_SYMBOL(noop_llseek); |
| 163 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 164 | loff_t no_llseek(struct file *file, loff_t offset, int whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
| 166 | return -ESPIPE; |
| 167 | } |
| 168 | EXPORT_SYMBOL(no_llseek); |
| 169 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 170 | loff_t default_llseek(struct file *file, loff_t offset, int whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 172 | struct inode *inode = file_inode(file); |
David Sterba | 16abef0 | 2008-04-22 15:09:22 +0200 | [diff] [blame] | 173 | loff_t retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 175 | mutex_lock(&inode->i_mutex); |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 176 | switch (whence) { |
Chris Snook | 7b8e892 | 2007-05-08 00:24:13 -0700 | [diff] [blame] | 177 | case SEEK_END: |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 178 | offset += i_size_read(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | break; |
Chris Snook | 7b8e892 | 2007-05-08 00:24:13 -0700 | [diff] [blame] | 180 | case SEEK_CUR: |
Alain Knaff | 5b6f1eb | 2008-11-10 17:08:08 -0800 | [diff] [blame] | 181 | if (offset == 0) { |
| 182 | retval = file->f_pos; |
| 183 | goto out; |
| 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | offset += file->f_pos; |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 186 | break; |
| 187 | case SEEK_DATA: |
| 188 | /* |
| 189 | * In the generic case the entire file is data, so as |
| 190 | * long as offset isn't at the end of the file then the |
| 191 | * offset is data. |
| 192 | */ |
Dan Carpenter | bacb2d8 | 2011-07-26 17:25:20 +0300 | [diff] [blame] | 193 | if (offset >= inode->i_size) { |
| 194 | retval = -ENXIO; |
| 195 | goto out; |
| 196 | } |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 197 | break; |
| 198 | case SEEK_HOLE: |
| 199 | /* |
| 200 | * There is a virtual hole at the end of the file, so |
| 201 | * as long as offset isn't i_size or larger, return |
| 202 | * i_size. |
| 203 | */ |
Dan Carpenter | bacb2d8 | 2011-07-26 17:25:20 +0300 | [diff] [blame] | 204 | if (offset >= inode->i_size) { |
| 205 | retval = -ENXIO; |
| 206 | goto out; |
| 207 | } |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 208 | offset = inode->i_size; |
| 209 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | retval = -EINVAL; |
Al Viro | cccb5a1 | 2010-12-17 07:44:05 -0500 | [diff] [blame] | 212 | if (offset >= 0 || unsigned_offsets(file)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | if (offset != file->f_pos) { |
| 214 | file->f_pos = offset; |
| 215 | file->f_version = 0; |
| 216 | } |
| 217 | retval = offset; |
| 218 | } |
Alain Knaff | 5b6f1eb | 2008-11-10 17:08:08 -0800 | [diff] [blame] | 219 | out: |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 220 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | return retval; |
| 222 | } |
| 223 | EXPORT_SYMBOL(default_llseek); |
| 224 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 225 | loff_t vfs_llseek(struct file *file, loff_t offset, int whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
| 227 | loff_t (*fn)(struct file *, loff_t, int); |
| 228 | |
| 229 | fn = no_llseek; |
| 230 | if (file->f_mode & FMODE_LSEEK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | if (file->f_op && file->f_op->llseek) |
| 232 | fn = file->f_op->llseek; |
| 233 | } |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 234 | return fn(file, offset, whence); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | EXPORT_SYMBOL(vfs_llseek); |
| 237 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 238 | SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
| 240 | off_t retval; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 241 | struct fd f = fdget(fd); |
| 242 | if (!f.file) |
| 243 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | retval = -EINVAL; |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 246 | if (whence <= SEEK_MAX) { |
| 247 | loff_t res = vfs_llseek(f.file, offset, whence); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | retval = res; |
| 249 | if (res != (loff_t)retval) |
| 250 | retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */ |
| 251 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 252 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | return retval; |
| 254 | } |
| 255 | |
Al Viro | 561c673 | 2013-02-24 10:52:26 -0500 | [diff] [blame] | 256 | #ifdef CONFIG_COMPAT |
| 257 | COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence) |
| 258 | { |
| 259 | return sys_lseek(fd, offset, whence); |
| 260 | } |
| 261 | #endif |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | #ifdef __ARCH_WANT_SYS_LLSEEK |
Heiko Carstens | 003d7ab | 2009-01-14 14:14:21 +0100 | [diff] [blame] | 264 | SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high, |
| 265 | unsigned long, offset_low, loff_t __user *, result, |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 266 | unsigned int, whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
| 268 | int retval; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 269 | struct fd f = fdget(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | loff_t offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 272 | if (!f.file) |
| 273 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | retval = -EINVAL; |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 276 | if (whence > SEEK_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | goto out_putf; |
| 278 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 279 | offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low, |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 280 | whence); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | retval = (int)offset; |
| 283 | if (offset >= 0) { |
| 284 | retval = -EFAULT; |
| 285 | if (!copy_to_user(result, &offset, sizeof(offset))) |
| 286 | retval = 0; |
| 287 | } |
| 288 | out_putf: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 289 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | return retval; |
| 291 | } |
| 292 | #endif |
| 293 | |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 294 | /* |
| 295 | * rw_verify_area doesn't like huge counts. We limit |
| 296 | * them to something that fits in "int" so that others |
| 297 | * won't have to do range checks all the time. |
| 298 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count) |
| 300 | { |
| 301 | struct inode *inode; |
| 302 | loff_t pos; |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 303 | int retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 305 | inode = file_inode(file); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 306 | if (unlikely((ssize_t) count < 0)) |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 307 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | pos = *ppos; |
Al Viro | cccb5a1 | 2010-12-17 07:44:05 -0500 | [diff] [blame] | 309 | if (unlikely(pos < 0)) { |
| 310 | if (!unsigned_offsets(file)) |
| 311 | return retval; |
| 312 | if (count >= -pos) /* both values are in 0..LLONG_MAX */ |
| 313 | return -EOVERFLOW; |
| 314 | } else if (unlikely((loff_t) (pos + count) < 0)) { |
| 315 | if (!unsigned_offsets(file)) |
KAMEZAWA Hiroyuki | 4a3956c | 2010-10-01 14:20:22 -0700 | [diff] [blame] | 316 | return retval; |
| 317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Pavel Emelyanov | a16877c | 2007-10-01 14:41:11 -0700 | [diff] [blame] | 319 | if (unlikely(inode->i_flock && mandatory_lock(inode))) { |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 320 | retval = locks_mandatory_area( |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 321 | read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE, |
| 322 | inode, file, pos, count); |
| 323 | if (retval < 0) |
| 324 | return retval; |
| 325 | } |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 326 | retval = security_file_permission(file, |
| 327 | read_write == READ ? MAY_READ : MAY_WRITE); |
| 328 | if (retval) |
| 329 | return retval; |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 330 | return count > MAX_RW_COUNT ? MAX_RW_COUNT : count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos) |
| 334 | { |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 335 | struct iovec iov = { .iov_base = buf, .iov_len = len }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | struct kiocb kiocb; |
| 337 | ssize_t ret; |
| 338 | |
| 339 | init_sync_kiocb(&kiocb, filp); |
| 340 | kiocb.ki_pos = *ppos; |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 341 | kiocb.ki_left = len; |
David Howells | 61964eb | 2010-03-24 17:09:19 +0000 | [diff] [blame] | 342 | kiocb.ki_nbytes = len; |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 343 | |
Zach Brown | 41003a7 | 2013-05-07 16:18:25 -0700 | [diff] [blame] | 344 | ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | if (-EIOCBQUEUED == ret) |
| 346 | ret = wait_on_sync_kiocb(&kiocb); |
| 347 | *ppos = kiocb.ki_pos; |
| 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | EXPORT_SYMBOL(do_sync_read); |
| 352 | |
| 353 | ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) |
| 354 | { |
| 355 | ssize_t ret; |
| 356 | |
| 357 | if (!(file->f_mode & FMODE_READ)) |
| 358 | return -EBADF; |
| 359 | if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read)) |
| 360 | return -EINVAL; |
| 361 | if (unlikely(!access_ok(VERIFY_WRITE, buf, count))) |
| 362 | return -EFAULT; |
| 363 | |
| 364 | ret = rw_verify_area(READ, file, pos, count); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 365 | if (ret >= 0) { |
| 366 | count = ret; |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 367 | if (file->f_op->read) |
| 368 | ret = file->f_op->read(file, buf, count, pos); |
| 369 | else |
| 370 | ret = do_sync_read(file, buf, count, pos); |
| 371 | if (ret > 0) { |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 372 | fsnotify_access(file); |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 373 | add_rchar(current, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 375 | inc_syscr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | EXPORT_SYMBOL(vfs_read); |
| 382 | |
| 383 | ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos) |
| 384 | { |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 385 | struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | struct kiocb kiocb; |
| 387 | ssize_t ret; |
| 388 | |
| 389 | init_sync_kiocb(&kiocb, filp); |
| 390 | kiocb.ki_pos = *ppos; |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 391 | kiocb.ki_left = len; |
David Howells | 61964eb | 2010-03-24 17:09:19 +0000 | [diff] [blame] | 392 | kiocb.ki_nbytes = len; |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 393 | |
Zach Brown | 41003a7 | 2013-05-07 16:18:25 -0700 | [diff] [blame] | 394 | ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (-EIOCBQUEUED == ret) |
| 396 | ret = wait_on_sync_kiocb(&kiocb); |
| 397 | *ppos = kiocb.ki_pos; |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | EXPORT_SYMBOL(do_sync_write); |
| 402 | |
Al Viro | 06ae43f | 2013-03-20 13:19:30 -0400 | [diff] [blame] | 403 | ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos) |
| 404 | { |
| 405 | mm_segment_t old_fs; |
| 406 | const char __user *p; |
| 407 | ssize_t ret; |
| 408 | |
Al Viro | 3e84f48 | 2013-03-27 15:20:30 +0000 | [diff] [blame] | 409 | if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write)) |
| 410 | return -EINVAL; |
| 411 | |
Al Viro | 06ae43f | 2013-03-20 13:19:30 -0400 | [diff] [blame] | 412 | old_fs = get_fs(); |
| 413 | set_fs(get_ds()); |
| 414 | p = (__force const char __user *)buf; |
| 415 | if (count > MAX_RW_COUNT) |
| 416 | count = MAX_RW_COUNT; |
| 417 | if (file->f_op->write) |
| 418 | ret = file->f_op->write(file, p, count, pos); |
| 419 | else |
| 420 | ret = do_sync_write(file, p, count, pos); |
| 421 | set_fs(old_fs); |
| 422 | if (ret > 0) { |
| 423 | fsnotify_modify(file); |
| 424 | add_wchar(current, ret); |
| 425 | } |
| 426 | inc_syscw(current); |
| 427 | return ret; |
| 428 | } |
| 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) |
| 431 | { |
| 432 | ssize_t ret; |
| 433 | |
| 434 | if (!(file->f_mode & FMODE_WRITE)) |
| 435 | return -EBADF; |
| 436 | if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write)) |
| 437 | return -EINVAL; |
| 438 | if (unlikely(!access_ok(VERIFY_READ, buf, count))) |
| 439 | return -EFAULT; |
| 440 | |
| 441 | ret = rw_verify_area(WRITE, file, pos, count); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 442 | if (ret >= 0) { |
| 443 | count = ret; |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 444 | file_start_write(file); |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 445 | if (file->f_op->write) |
| 446 | ret = file->f_op->write(file, buf, count, pos); |
| 447 | else |
| 448 | ret = do_sync_write(file, buf, count, pos); |
| 449 | if (ret > 0) { |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 450 | fsnotify_modify(file); |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 451 | add_wchar(current, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 453 | inc_syscw(current); |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 454 | file_end_write(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | return ret; |
| 458 | } |
| 459 | |
| 460 | EXPORT_SYMBOL(vfs_write); |
| 461 | |
| 462 | static inline loff_t file_pos_read(struct file *file) |
| 463 | { |
| 464 | return file->f_pos; |
| 465 | } |
| 466 | |
| 467 | static inline void file_pos_write(struct file *file, loff_t pos) |
| 468 | { |
| 469 | file->f_pos = pos; |
| 470 | } |
| 471 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 472 | SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 474 | struct fd f = fdget(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 477 | if (f.file) { |
| 478 | loff_t pos = file_pos_read(f.file); |
| 479 | ret = vfs_read(f.file, buf, count, &pos); |
| 480 | file_pos_write(f.file, pos); |
| 481 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | return ret; |
| 484 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 486 | SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, |
| 487 | size_t, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 489 | struct fd f = fdget(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 492 | if (f.file) { |
| 493 | loff_t pos = file_pos_read(f.file); |
| 494 | ret = vfs_write(f.file, buf, count, &pos); |
| 495 | file_pos_write(f.file, pos); |
| 496 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | return ret; |
| 500 | } |
| 501 | |
Al Viro | 4a0fd5b | 2013-01-21 15:16:58 -0500 | [diff] [blame] | 502 | SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf, |
| 503 | size_t, count, loff_t, pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 505 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | if (pos < 0) |
| 509 | return -EINVAL; |
| 510 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 511 | f = fdget(fd); |
| 512 | if (f.file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | ret = -ESPIPE; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 514 | if (f.file->f_mode & FMODE_PREAD) |
| 515 | ret = vfs_read(f.file, buf, count, &pos); |
| 516 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | return ret; |
| 520 | } |
| 521 | |
Al Viro | 4a0fd5b | 2013-01-21 15:16:58 -0500 | [diff] [blame] | 522 | SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf, |
| 523 | size_t, count, loff_t, pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 525 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
| 528 | if (pos < 0) |
| 529 | return -EINVAL; |
| 530 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 531 | f = fdget(fd); |
| 532 | if (f.file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | ret = -ESPIPE; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 534 | if (f.file->f_mode & FMODE_PWRITE) |
| 535 | ret = vfs_write(f.file, buf, count, &pos); |
| 536 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Reduce an iovec's length in-place. Return the resulting number of segments |
| 544 | */ |
| 545 | unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to) |
| 546 | { |
| 547 | unsigned long seg = 0; |
| 548 | size_t len = 0; |
| 549 | |
| 550 | while (seg < nr_segs) { |
| 551 | seg++; |
| 552 | if (len + iov->iov_len >= to) { |
| 553 | iov->iov_len = to - len; |
| 554 | break; |
| 555 | } |
| 556 | len += iov->iov_len; |
| 557 | iov++; |
| 558 | } |
| 559 | return seg; |
| 560 | } |
Eric Sandeen | 1929552 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 561 | EXPORT_SYMBOL(iov_shorten); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 563 | static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov, |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 564 | unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn) |
| 565 | { |
| 566 | struct kiocb kiocb; |
| 567 | ssize_t ret; |
| 568 | |
| 569 | init_sync_kiocb(&kiocb, filp); |
| 570 | kiocb.ki_pos = *ppos; |
| 571 | kiocb.ki_left = len; |
| 572 | kiocb.ki_nbytes = len; |
| 573 | |
Zach Brown | 41003a7 | 2013-05-07 16:18:25 -0700 | [diff] [blame] | 574 | ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos); |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 575 | if (ret == -EIOCBQUEUED) |
| 576 | ret = wait_on_sync_kiocb(&kiocb); |
| 577 | *ppos = kiocb.ki_pos; |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | /* Do it by hand, with file-ops */ |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 582 | static ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov, |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 583 | unsigned long nr_segs, loff_t *ppos, io_fn_t fn) |
| 584 | { |
| 585 | struct iovec *vector = iov; |
| 586 | ssize_t ret = 0; |
| 587 | |
| 588 | while (nr_segs > 0) { |
| 589 | void __user *base; |
| 590 | size_t len; |
| 591 | ssize_t nr; |
| 592 | |
| 593 | base = vector->iov_base; |
| 594 | len = vector->iov_len; |
| 595 | vector++; |
| 596 | nr_segs--; |
| 597 | |
| 598 | nr = fn(filp, base, len, ppos); |
| 599 | |
| 600 | if (nr < 0) { |
| 601 | if (!ret) |
| 602 | ret = nr; |
| 603 | break; |
| 604 | } |
| 605 | ret += nr; |
| 606 | if (nr != len) |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | return ret; |
| 611 | } |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | /* A write operation does a read from user space and vice versa */ |
| 614 | #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ) |
| 615 | |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 616 | ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector, |
| 617 | unsigned long nr_segs, unsigned long fast_segs, |
| 618 | struct iovec *fast_pointer, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 619 | struct iovec **ret_pointer) |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 620 | { |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 621 | unsigned long seg; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 622 | ssize_t ret; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 623 | struct iovec *iov = fast_pointer; |
| 624 | |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 625 | /* |
| 626 | * SuS says "The readv() function *may* fail if the iovcnt argument |
| 627 | * was less than or equal to 0, or greater than {IOV_MAX}. Linux has |
| 628 | * traditionally returned zero for zero segments, so... |
| 629 | */ |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 630 | if (nr_segs == 0) { |
| 631 | ret = 0; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 632 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 635 | /* |
| 636 | * First get the "struct iovec" from user memory and |
| 637 | * verify all the pointers |
| 638 | */ |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 639 | if (nr_segs > UIO_MAXIOV) { |
| 640 | ret = -EINVAL; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 641 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 642 | } |
| 643 | if (nr_segs > fast_segs) { |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 644 | iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL); |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 645 | if (iov == NULL) { |
| 646 | ret = -ENOMEM; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 647 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 648 | } |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 649 | } |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 650 | if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) { |
| 651 | ret = -EFAULT; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 652 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 655 | /* |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 656 | * According to the Single Unix Specification we should return EINVAL |
| 657 | * if an element length is < 0 when cast to ssize_t or if the |
| 658 | * total length would overflow the ssize_t return value of the |
| 659 | * system call. |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 660 | * |
| 661 | * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the |
| 662 | * overflow case. |
| 663 | */ |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 664 | ret = 0; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 665 | for (seg = 0; seg < nr_segs; seg++) { |
| 666 | void __user *buf = iov[seg].iov_base; |
| 667 | ssize_t len = (ssize_t)iov[seg].iov_len; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 668 | |
| 669 | /* see if we we're about to use an invalid len or if |
| 670 | * it's about to overflow ssize_t */ |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 671 | if (len < 0) { |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 672 | ret = -EINVAL; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 673 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 674 | } |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 675 | if (type >= 0 |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 676 | && unlikely(!access_ok(vrfy_dir(type), buf, len))) { |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 677 | ret = -EFAULT; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 678 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 679 | } |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 680 | if (len > MAX_RW_COUNT - ret) { |
| 681 | len = MAX_RW_COUNT - ret; |
| 682 | iov[seg].iov_len = len; |
| 683 | } |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 684 | ret += len; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 685 | } |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 686 | out: |
| 687 | *ret_pointer = iov; |
| 688 | return ret; |
| 689 | } |
| 690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | static ssize_t do_readv_writev(int type, struct file *file, |
| 692 | const struct iovec __user * uvector, |
| 693 | unsigned long nr_segs, loff_t *pos) |
| 694 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | size_t tot_len; |
| 696 | struct iovec iovstack[UIO_FASTIOV]; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 697 | struct iovec *iov = iovstack; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | io_fn_t fn; |
| 700 | iov_fn_t fnv; |
| 701 | |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 702 | if (!file->f_op) { |
| 703 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | goto out; |
| 705 | } |
| 706 | |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 707 | ret = rw_copy_check_uvector(type, uvector, nr_segs, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 708 | ARRAY_SIZE(iovstack), iovstack, &iov); |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 709 | if (ret <= 0) |
| 710 | goto out; |
| 711 | |
| 712 | tot_len = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | ret = rw_verify_area(type, file, pos, tot_len); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 714 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | goto out; |
| 716 | |
| 717 | fnv = NULL; |
| 718 | if (type == READ) { |
| 719 | fn = file->f_op->read; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 720 | fnv = file->f_op->aio_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | } else { |
| 722 | fn = (io_fn_t)file->f_op->write; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 723 | fnv = file->f_op->aio_write; |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 724 | file_start_write(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 727 | if (fnv) |
| 728 | ret = do_sync_readv_writev(file, iov, nr_segs, tot_len, |
| 729 | pos, fnv); |
| 730 | else |
| 731 | ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 733 | if (type != READ) |
| 734 | file_end_write(file); |
| 735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | out: |
| 737 | if (iov != iovstack) |
| 738 | kfree(iov); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 739 | if ((ret + (type == READ)) > 0) { |
| 740 | if (type == READ) |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 741 | fsnotify_access(file); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 742 | else |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 743 | fsnotify_modify(file); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | ssize_t vfs_readv(struct file *file, const struct iovec __user *vec, |
| 749 | unsigned long vlen, loff_t *pos) |
| 750 | { |
| 751 | if (!(file->f_mode & FMODE_READ)) |
| 752 | return -EBADF; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 753 | if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | return -EINVAL; |
| 755 | |
| 756 | return do_readv_writev(READ, file, vec, vlen, pos); |
| 757 | } |
| 758 | |
| 759 | EXPORT_SYMBOL(vfs_readv); |
| 760 | |
| 761 | ssize_t vfs_writev(struct file *file, const struct iovec __user *vec, |
| 762 | unsigned long vlen, loff_t *pos) |
| 763 | { |
| 764 | if (!(file->f_mode & FMODE_WRITE)) |
| 765 | return -EBADF; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 766 | if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | return -EINVAL; |
| 768 | |
| 769 | return do_readv_writev(WRITE, file, vec, vlen, pos); |
| 770 | } |
| 771 | |
| 772 | EXPORT_SYMBOL(vfs_writev); |
| 773 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 774 | SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec, |
| 775 | unsigned long, vlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 777 | struct fd f = fdget(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 780 | if (f.file) { |
| 781 | loff_t pos = file_pos_read(f.file); |
| 782 | ret = vfs_readv(f.file, vec, vlen, &pos); |
| 783 | file_pos_write(f.file, pos); |
| 784 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | if (ret > 0) |
Alexey Dobriyan | 4b98d11 | 2007-02-10 01:46:45 -0800 | [diff] [blame] | 788 | add_rchar(current, ret); |
| 789 | inc_syscr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | return ret; |
| 791 | } |
| 792 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 793 | SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec, |
| 794 | unsigned long, vlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 796 | struct fd f = fdget(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 799 | if (f.file) { |
| 800 | loff_t pos = file_pos_read(f.file); |
| 801 | ret = vfs_writev(f.file, vec, vlen, &pos); |
| 802 | file_pos_write(f.file, pos); |
| 803 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | if (ret > 0) |
Alexey Dobriyan | 4b98d11 | 2007-02-10 01:46:45 -0800 | [diff] [blame] | 807 | add_wchar(current, ret); |
| 808 | inc_syscw(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | return ret; |
| 810 | } |
| 811 | |
Linus Torvalds | 601cc11 | 2009-04-03 08:03:22 -0700 | [diff] [blame] | 812 | static inline loff_t pos_from_hilo(unsigned long high, unsigned long low) |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 813 | { |
Linus Torvalds | 601cc11 | 2009-04-03 08:03:22 -0700 | [diff] [blame] | 814 | #define HALF_LONG_BITS (BITS_PER_LONG / 2) |
| 815 | return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low; |
| 816 | } |
| 817 | |
| 818 | SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec, |
| 819 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) |
| 820 | { |
| 821 | loff_t pos = pos_from_hilo(pos_h, pos_l); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 822 | struct fd f; |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 823 | ssize_t ret = -EBADF; |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 824 | |
| 825 | if (pos < 0) |
| 826 | return -EINVAL; |
| 827 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 828 | f = fdget(fd); |
| 829 | if (f.file) { |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 830 | ret = -ESPIPE; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 831 | if (f.file->f_mode & FMODE_PREAD) |
| 832 | ret = vfs_readv(f.file, vec, vlen, &pos); |
| 833 | fdput(f); |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | if (ret > 0) |
| 837 | add_rchar(current, ret); |
| 838 | inc_syscr(current); |
| 839 | return ret; |
| 840 | } |
| 841 | |
| 842 | SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, |
Linus Torvalds | 601cc11 | 2009-04-03 08:03:22 -0700 | [diff] [blame] | 843 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 844 | { |
Linus Torvalds | 601cc11 | 2009-04-03 08:03:22 -0700 | [diff] [blame] | 845 | loff_t pos = pos_from_hilo(pos_h, pos_l); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 846 | struct fd f; |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 847 | ssize_t ret = -EBADF; |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 848 | |
| 849 | if (pos < 0) |
| 850 | return -EINVAL; |
| 851 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 852 | f = fdget(fd); |
| 853 | if (f.file) { |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 854 | ret = -ESPIPE; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 855 | if (f.file->f_mode & FMODE_PWRITE) |
| 856 | ret = vfs_writev(f.file, vec, vlen, &pos); |
| 857 | fdput(f); |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | if (ret > 0) |
| 861 | add_wchar(current, ret); |
| 862 | inc_syscw(current); |
| 863 | return ret; |
| 864 | } |
| 865 | |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 866 | #ifdef CONFIG_COMPAT |
| 867 | |
| 868 | static ssize_t compat_do_readv_writev(int type, struct file *file, |
| 869 | const struct compat_iovec __user *uvector, |
| 870 | unsigned long nr_segs, loff_t *pos) |
| 871 | { |
| 872 | compat_ssize_t tot_len; |
| 873 | struct iovec iovstack[UIO_FASTIOV]; |
| 874 | struct iovec *iov = iovstack; |
| 875 | ssize_t ret; |
| 876 | io_fn_t fn; |
| 877 | iov_fn_t fnv; |
| 878 | |
| 879 | ret = -EINVAL; |
| 880 | if (!file->f_op) |
| 881 | goto out; |
| 882 | |
| 883 | ret = -EFAULT; |
| 884 | if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector))) |
| 885 | goto out; |
| 886 | |
| 887 | ret = compat_rw_copy_check_uvector(type, uvector, nr_segs, |
| 888 | UIO_FASTIOV, iovstack, &iov); |
| 889 | if (ret <= 0) |
| 890 | goto out; |
| 891 | |
| 892 | tot_len = ret; |
| 893 | ret = rw_verify_area(type, file, pos, tot_len); |
| 894 | if (ret < 0) |
| 895 | goto out; |
| 896 | |
| 897 | fnv = NULL; |
| 898 | if (type == READ) { |
| 899 | fn = file->f_op->read; |
| 900 | fnv = file->f_op->aio_read; |
| 901 | } else { |
| 902 | fn = (io_fn_t)file->f_op->write; |
| 903 | fnv = file->f_op->aio_write; |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 904 | file_start_write(file); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 905 | } |
| 906 | |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 907 | if (fnv) |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 908 | ret = do_sync_readv_writev(file, iov, nr_segs, tot_len, |
| 909 | pos, fnv); |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 910 | else |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 911 | ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn); |
| 912 | |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 913 | if (type != READ) |
| 914 | file_end_write(file); |
| 915 | |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 916 | out: |
| 917 | if (iov != iovstack) |
| 918 | kfree(iov); |
| 919 | if ((ret + (type == READ)) > 0) { |
| 920 | if (type == READ) |
| 921 | fsnotify_access(file); |
| 922 | else |
| 923 | fsnotify_modify(file); |
| 924 | } |
| 925 | return ret; |
| 926 | } |
| 927 | |
| 928 | static size_t compat_readv(struct file *file, |
| 929 | const struct compat_iovec __user *vec, |
| 930 | unsigned long vlen, loff_t *pos) |
| 931 | { |
| 932 | ssize_t ret = -EBADF; |
| 933 | |
| 934 | if (!(file->f_mode & FMODE_READ)) |
| 935 | goto out; |
| 936 | |
| 937 | ret = -EINVAL; |
| 938 | if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read)) |
| 939 | goto out; |
| 940 | |
| 941 | ret = compat_do_readv_writev(READ, file, vec, vlen, pos); |
| 942 | |
| 943 | out: |
| 944 | if (ret > 0) |
| 945 | add_rchar(current, ret); |
| 946 | inc_syscr(current); |
| 947 | return ret; |
| 948 | } |
| 949 | |
| 950 | COMPAT_SYSCALL_DEFINE3(readv, unsigned long, fd, |
| 951 | const struct compat_iovec __user *,vec, |
| 952 | unsigned long, vlen) |
| 953 | { |
| 954 | struct fd f = fdget(fd); |
| 955 | ssize_t ret; |
| 956 | loff_t pos; |
| 957 | |
| 958 | if (!f.file) |
| 959 | return -EBADF; |
| 960 | pos = f.file->f_pos; |
| 961 | ret = compat_readv(f.file, vec, vlen, &pos); |
| 962 | f.file->f_pos = pos; |
| 963 | fdput(f); |
| 964 | return ret; |
| 965 | } |
| 966 | |
| 967 | COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd, |
| 968 | const struct compat_iovec __user *,vec, |
| 969 | unsigned long, vlen, loff_t, pos) |
| 970 | { |
| 971 | struct fd f; |
| 972 | ssize_t ret; |
| 973 | |
| 974 | if (pos < 0) |
| 975 | return -EINVAL; |
| 976 | f = fdget(fd); |
| 977 | if (!f.file) |
| 978 | return -EBADF; |
| 979 | ret = -ESPIPE; |
| 980 | if (f.file->f_mode & FMODE_PREAD) |
| 981 | ret = compat_readv(f.file, vec, vlen, &pos); |
| 982 | fdput(f); |
| 983 | return ret; |
| 984 | } |
| 985 | |
| 986 | COMPAT_SYSCALL_DEFINE5(preadv, unsigned long, fd, |
| 987 | const struct compat_iovec __user *,vec, |
| 988 | unsigned long, vlen, u32, pos_low, u32, pos_high) |
| 989 | { |
| 990 | loff_t pos = ((loff_t)pos_high << 32) | pos_low; |
| 991 | return compat_sys_preadv64(fd, vec, vlen, pos); |
| 992 | } |
| 993 | |
| 994 | static size_t compat_writev(struct file *file, |
| 995 | const struct compat_iovec __user *vec, |
| 996 | unsigned long vlen, loff_t *pos) |
| 997 | { |
| 998 | ssize_t ret = -EBADF; |
| 999 | |
| 1000 | if (!(file->f_mode & FMODE_WRITE)) |
| 1001 | goto out; |
| 1002 | |
| 1003 | ret = -EINVAL; |
| 1004 | if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write)) |
| 1005 | goto out; |
| 1006 | |
| 1007 | ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos); |
| 1008 | |
| 1009 | out: |
| 1010 | if (ret > 0) |
| 1011 | add_wchar(current, ret); |
| 1012 | inc_syscw(current); |
| 1013 | return ret; |
| 1014 | } |
| 1015 | |
| 1016 | COMPAT_SYSCALL_DEFINE3(writev, unsigned long, fd, |
| 1017 | const struct compat_iovec __user *, vec, |
| 1018 | unsigned long, vlen) |
| 1019 | { |
| 1020 | struct fd f = fdget(fd); |
| 1021 | ssize_t ret; |
| 1022 | loff_t pos; |
| 1023 | |
| 1024 | if (!f.file) |
| 1025 | return -EBADF; |
| 1026 | pos = f.file->f_pos; |
| 1027 | ret = compat_writev(f.file, vec, vlen, &pos); |
| 1028 | f.file->f_pos = pos; |
| 1029 | fdput(f); |
| 1030 | return ret; |
| 1031 | } |
| 1032 | |
| 1033 | COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd, |
| 1034 | const struct compat_iovec __user *,vec, |
| 1035 | unsigned long, vlen, loff_t, pos) |
| 1036 | { |
| 1037 | struct fd f; |
| 1038 | ssize_t ret; |
| 1039 | |
| 1040 | if (pos < 0) |
| 1041 | return -EINVAL; |
| 1042 | f = fdget(fd); |
| 1043 | if (!f.file) |
| 1044 | return -EBADF; |
| 1045 | ret = -ESPIPE; |
| 1046 | if (f.file->f_mode & FMODE_PWRITE) |
| 1047 | ret = compat_writev(f.file, vec, vlen, &pos); |
| 1048 | fdput(f); |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | COMPAT_SYSCALL_DEFINE5(pwritev, unsigned long, fd, |
| 1053 | const struct compat_iovec __user *,vec, |
| 1054 | unsigned long, vlen, u32, pos_low, u32, pos_high) |
| 1055 | { |
| 1056 | loff_t pos = ((loff_t)pos_high << 32) | pos_low; |
| 1057 | return compat_sys_pwritev64(fd, vec, vlen, pos); |
| 1058 | } |
| 1059 | #endif |
| 1060 | |
Al Viro | 19f4fc3 | 2013-02-24 02:17:03 -0500 | [diff] [blame] | 1061 | static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, |
| 1062 | size_t count, loff_t max) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1064 | struct fd in, out; |
| 1065 | struct inode *in_inode, *out_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | loff_t pos; |
| 1067 | ssize_t retval; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1068 | int fl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
| 1070 | /* |
| 1071 | * Get input file, and verify that it is ok.. |
| 1072 | */ |
| 1073 | retval = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1074 | in = fdget(in_fd); |
| 1075 | if (!in.file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | goto out; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1077 | if (!(in.file->f_mode & FMODE_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | goto fput_in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | retval = -ESPIPE; |
| 1080 | if (!ppos) |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1081 | ppos = &in.file->f_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | else |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1083 | if (!(in.file->f_mode & FMODE_PREAD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | goto fput_in; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1085 | retval = rw_verify_area(READ, in.file, ppos, count); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 1086 | if (retval < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | goto fput_in; |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 1088 | count = retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | /* |
| 1091 | * Get output file, and verify that it is ok.. |
| 1092 | */ |
| 1093 | retval = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1094 | out = fdget(out_fd); |
| 1095 | if (!out.file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | goto fput_in; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1097 | if (!(out.file->f_mode & FMODE_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | goto fput_out; |
| 1099 | retval = -EINVAL; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1100 | in_inode = file_inode(in.file); |
| 1101 | out_inode = file_inode(out.file); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1102 | retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 1103 | if (retval < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | goto fput_out; |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 1105 | count = retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | if (!max) |
| 1108 | max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); |
| 1109 | |
| 1110 | pos = *ppos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | if (unlikely(pos + count > max)) { |
| 1112 | retval = -EOVERFLOW; |
| 1113 | if (pos >= max) |
| 1114 | goto fput_out; |
| 1115 | count = max - pos; |
| 1116 | } |
| 1117 | |
Jens Axboe | d96e6e7 | 2007-06-11 12:18:52 +0200 | [diff] [blame] | 1118 | fl = 0; |
Jens Axboe | 534f2aa | 2007-06-01 14:52:37 +0200 | [diff] [blame] | 1119 | #if 0 |
Jens Axboe | d96e6e7 | 2007-06-11 12:18:52 +0200 | [diff] [blame] | 1120 | /* |
| 1121 | * We need to debate whether we can enable this or not. The |
| 1122 | * man page documents EAGAIN return for the output at least, |
| 1123 | * and the application is arguably buggy if it doesn't expect |
| 1124 | * EAGAIN on a non-blocking file descriptor. |
| 1125 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1126 | if (in.file->f_flags & O_NONBLOCK) |
Jens Axboe | d96e6e7 | 2007-06-11 12:18:52 +0200 | [diff] [blame] | 1127 | fl = SPLICE_F_NONBLOCK; |
Jens Axboe | 534f2aa | 2007-06-01 14:52:37 +0200 | [diff] [blame] | 1128 | #endif |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1129 | retval = do_splice_direct(in.file, ppos, out.file, count, fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | |
| 1131 | if (retval > 0) { |
Alexey Dobriyan | 4b98d11 | 2007-02-10 01:46:45 -0800 | [diff] [blame] | 1132 | add_rchar(current, retval); |
| 1133 | add_wchar(current, retval); |
Scott Wolchok | a68c2f1 | 2012-12-20 15:05:52 -0800 | [diff] [blame] | 1134 | fsnotify_access(in.file); |
| 1135 | fsnotify_modify(out.file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
Alexey Dobriyan | 4b98d11 | 2007-02-10 01:46:45 -0800 | [diff] [blame] | 1138 | inc_syscr(current); |
| 1139 | inc_syscw(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | if (*ppos > max) |
| 1141 | retval = -EOVERFLOW; |
| 1142 | |
| 1143 | fput_out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1144 | fdput(out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | fput_in: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1146 | fdput(in); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | out: |
| 1148 | return retval; |
| 1149 | } |
| 1150 | |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 1151 | SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | { |
| 1153 | loff_t pos; |
| 1154 | off_t off; |
| 1155 | ssize_t ret; |
| 1156 | |
| 1157 | if (offset) { |
| 1158 | if (unlikely(get_user(off, offset))) |
| 1159 | return -EFAULT; |
| 1160 | pos = off; |
| 1161 | ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS); |
| 1162 | if (unlikely(put_user(pos, offset))) |
| 1163 | return -EFAULT; |
| 1164 | return ret; |
| 1165 | } |
| 1166 | |
| 1167 | return do_sendfile(out_fd, in_fd, NULL, count, 0); |
| 1168 | } |
| 1169 | |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 1170 | SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | { |
| 1172 | loff_t pos; |
| 1173 | ssize_t ret; |
| 1174 | |
| 1175 | if (offset) { |
| 1176 | if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) |
| 1177 | return -EFAULT; |
| 1178 | ret = do_sendfile(out_fd, in_fd, &pos, count, 0); |
| 1179 | if (unlikely(put_user(pos, offset))) |
| 1180 | return -EFAULT; |
| 1181 | return ret; |
| 1182 | } |
| 1183 | |
| 1184 | return do_sendfile(out_fd, in_fd, NULL, count, 0); |
| 1185 | } |
Al Viro | 19f4fc3 | 2013-02-24 02:17:03 -0500 | [diff] [blame] | 1186 | |
| 1187 | #ifdef CONFIG_COMPAT |
| 1188 | COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, |
| 1189 | compat_off_t __user *, offset, compat_size_t, count) |
| 1190 | { |
| 1191 | loff_t pos; |
| 1192 | off_t off; |
| 1193 | ssize_t ret; |
| 1194 | |
| 1195 | if (offset) { |
| 1196 | if (unlikely(get_user(off, offset))) |
| 1197 | return -EFAULT; |
| 1198 | pos = off; |
| 1199 | ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS); |
| 1200 | if (unlikely(put_user(pos, offset))) |
| 1201 | return -EFAULT; |
| 1202 | return ret; |
| 1203 | } |
| 1204 | |
| 1205 | return do_sendfile(out_fd, in_fd, NULL, count, 0); |
| 1206 | } |
| 1207 | |
| 1208 | COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, |
| 1209 | compat_loff_t __user *, offset, compat_size_t, count) |
| 1210 | { |
| 1211 | loff_t pos; |
| 1212 | ssize_t ret; |
| 1213 | |
| 1214 | if (offset) { |
| 1215 | if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) |
| 1216 | return -EFAULT; |
| 1217 | ret = do_sendfile(out_fd, in_fd, &pos, count, 0); |
| 1218 | if (unlikely(put_user(pos, offset))) |
| 1219 | return -EFAULT; |
| 1220 | return ret; |
| 1221 | } |
| 1222 | |
| 1223 | return do_sendfile(out_fd, in_fd, NULL, count, 0); |
| 1224 | } |
| 1225 | #endif |