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