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