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> |
Zach Brown | 2973293 | 2015-11-10 16:53:30 -0500 | [diff] [blame] | 19 | #include <linux/mount.h> |
Wouter van Kesteren | 2feb55f | 2016-02-16 22:20:59 +0100 | [diff] [blame] | 20 | #include <linux/fs.h> |
Al Viro | 06ae43f | 2013-03-20 13:19:30 -0400 | [diff] [blame] | 21 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/uaccess.h> |
| 24 | #include <asm/unistd.h> |
| 25 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 26 | const struct file_operations generic_ro_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | .llseek = generic_file_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 28 | .read_iter = generic_file_read_iter, |
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 | |
Jie Liu | 46a1c2c | 2013-06-25 12:02:13 +0800 | [diff] [blame] | 40 | /** |
| 41 | * vfs_setpos - update the file offset for lseek |
| 42 | * @file: file structure in question |
| 43 | * @offset: file offset to seek to |
| 44 | * @maxsize: maximum file size |
| 45 | * |
| 46 | * This is a low-level filesystem helper for updating the file offset to |
| 47 | * the value specified by @offset if the given offset is valid and it is |
| 48 | * not equal to the current file offset. |
| 49 | * |
| 50 | * Return the specified offset on success and -EINVAL on invalid offset. |
| 51 | */ |
| 52 | loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize) |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 53 | { |
| 54 | if (offset < 0 && !unsigned_offsets(file)) |
| 55 | return -EINVAL; |
| 56 | if (offset > maxsize) |
| 57 | return -EINVAL; |
| 58 | |
| 59 | if (offset != file->f_pos) { |
| 60 | file->f_pos = offset; |
| 61 | file->f_version = 0; |
| 62 | } |
| 63 | return offset; |
| 64 | } |
Jie Liu | 46a1c2c | 2013-06-25 12:02:13 +0800 | [diff] [blame] | 65 | EXPORT_SYMBOL(vfs_setpos); |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 66 | |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 67 | /** |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 68 | * generic_file_llseek_size - generic llseek implementation for regular files |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 69 | * @file: file structure to seek on |
| 70 | * @offset: file offset to seek to |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 71 | * @whence: type of seek |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 72 | * @size: max size of this file in file system |
| 73 | * @eof: offset used for SEEK_END position |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 74 | * |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 75 | * 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] | 76 | * 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] | 77 | * |
| 78 | * Synchronization: |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 79 | * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms) |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 80 | * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes. |
| 81 | * read/writes behave like SEEK_SET against seeks. |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 82 | */ |
Andi Kleen | 9465efc | 2008-06-27 11:05:24 +0200 | [diff] [blame] | 83 | loff_t |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 84 | generic_file_llseek_size(struct file *file, loff_t offset, int whence, |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 85 | loff_t maxsize, loff_t eof) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 87 | switch (whence) { |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 88 | case SEEK_END: |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 89 | offset += eof; |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 90 | break; |
| 91 | case SEEK_CUR: |
Alain Knaff | 5b6f1eb | 2008-11-10 17:08:08 -0800 | [diff] [blame] | 92 | /* |
| 93 | * Here we special-case the lseek(fd, 0, SEEK_CUR) |
| 94 | * position-querying operation. Avoid rewriting the "same" |
| 95 | * f_pos value back to the file because a concurrent read(), |
| 96 | * write() or lseek() might have altered it |
| 97 | */ |
| 98 | if (offset == 0) |
| 99 | return file->f_pos; |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 100 | /* |
| 101 | * f_lock protects against read/modify/write race with other |
| 102 | * SEEK_CURs. Note that parallel writes and reads behave |
| 103 | * like SEEK_SET. |
| 104 | */ |
| 105 | spin_lock(&file->f_lock); |
Jie Liu | 46a1c2c | 2013-06-25 12:02:13 +0800 | [diff] [blame] | 106 | offset = vfs_setpos(file, file->f_pos + offset, maxsize); |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 107 | spin_unlock(&file->f_lock); |
| 108 | return offset; |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 109 | case SEEK_DATA: |
| 110 | /* |
| 111 | * In the generic case the entire file is data, so as long as |
| 112 | * offset isn't at the end of the file then the offset is data. |
| 113 | */ |
Andreas Gruenbacher | f3e2e7f | 2017-09-25 12:23:03 +0200 | [diff] [blame] | 114 | if ((unsigned long long)offset >= eof) |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 115 | return -ENXIO; |
| 116 | break; |
| 117 | case SEEK_HOLE: |
| 118 | /* |
| 119 | * There is a virtual hole at the end of the file, so as long as |
| 120 | * offset isn't i_size or larger, return i_size. |
| 121 | */ |
Andreas Gruenbacher | f3e2e7f | 2017-09-25 12:23:03 +0200 | [diff] [blame] | 122 | if ((unsigned long long)offset >= eof) |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 123 | return -ENXIO; |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 124 | offset = eof; |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 125 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
Christoph Hellwig | 3a8cff4 | 2008-08-11 15:37:17 +0200 | [diff] [blame] | 127 | |
Jie Liu | 46a1c2c | 2013-06-25 12:02:13 +0800 | [diff] [blame] | 128 | return vfs_setpos(file, offset, maxsize); |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 129 | } |
| 130 | EXPORT_SYMBOL(generic_file_llseek_size); |
| 131 | |
| 132 | /** |
| 133 | * generic_file_llseek - generic llseek implementation for regular files |
| 134 | * @file: file structure to seek on |
| 135 | * @offset: file offset to seek to |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 136 | * @whence: type of seek |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 137 | * |
| 138 | * This is a generic implemenation of ->llseek useable for all normal local |
| 139 | * filesystems. It just updates the file offset to the value specified by |
Ming Lei | 546ae2d | 2013-04-29 15:06:07 -0700 | [diff] [blame] | 140 | * @offset and @whence. |
Andi Kleen | 5760495 | 2011-09-15 16:06:50 -0700 | [diff] [blame] | 141 | */ |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 142 | 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] | 143 | { |
| 144 | struct inode *inode = file->f_mapping->host; |
| 145 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 146 | return generic_file_llseek_size(file, offset, whence, |
Eric Sandeen | e8b96eb | 2012-04-30 13:11:29 -0500 | [diff] [blame] | 147 | inode->i_sb->s_maxbytes, |
| 148 | i_size_read(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
Andi Kleen | 9465efc | 2008-06-27 11:05:24 +0200 | [diff] [blame] | 150 | EXPORT_SYMBOL(generic_file_llseek); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
jan Blunck | ae6afc3 | 2010-05-26 14:44:48 -0700 | [diff] [blame] | 152 | /** |
Al Viro | 1bf9d14 | 2013-06-16 20:27:42 +0400 | [diff] [blame] | 153 | * fixed_size_llseek - llseek implementation for fixed-sized devices |
| 154 | * @file: file structure to seek on |
| 155 | * @offset: file offset to seek to |
| 156 | * @whence: type of seek |
| 157 | * @size: size of the file |
| 158 | * |
| 159 | */ |
| 160 | loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size) |
| 161 | { |
| 162 | switch (whence) { |
| 163 | case SEEK_SET: case SEEK_CUR: case SEEK_END: |
| 164 | return generic_file_llseek_size(file, offset, whence, |
| 165 | size, size); |
| 166 | default: |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | } |
| 170 | EXPORT_SYMBOL(fixed_size_llseek); |
| 171 | |
| 172 | /** |
Al Viro | b25472f | 2015-12-05 22:04:48 -0500 | [diff] [blame] | 173 | * no_seek_end_llseek - llseek implementation for fixed-sized devices |
| 174 | * @file: file structure to seek on |
| 175 | * @offset: file offset to seek to |
| 176 | * @whence: type of seek |
| 177 | * |
| 178 | */ |
| 179 | loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence) |
| 180 | { |
| 181 | switch (whence) { |
| 182 | case SEEK_SET: case SEEK_CUR: |
| 183 | return generic_file_llseek_size(file, offset, whence, |
Wouter van Kesteren | 2feb55f | 2016-02-16 22:20:59 +0100 | [diff] [blame] | 184 | OFFSET_MAX, 0); |
Al Viro | b25472f | 2015-12-05 22:04:48 -0500 | [diff] [blame] | 185 | default: |
| 186 | return -EINVAL; |
| 187 | } |
| 188 | } |
| 189 | EXPORT_SYMBOL(no_seek_end_llseek); |
| 190 | |
| 191 | /** |
| 192 | * no_seek_end_llseek_size - llseek implementation for fixed-sized devices |
| 193 | * @file: file structure to seek on |
| 194 | * @offset: file offset to seek to |
| 195 | * @whence: type of seek |
| 196 | * @size: maximal offset allowed |
| 197 | * |
| 198 | */ |
| 199 | loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size) |
| 200 | { |
| 201 | switch (whence) { |
| 202 | case SEEK_SET: case SEEK_CUR: |
| 203 | return generic_file_llseek_size(file, offset, whence, |
| 204 | size, 0); |
| 205 | default: |
| 206 | return -EINVAL; |
| 207 | } |
| 208 | } |
| 209 | EXPORT_SYMBOL(no_seek_end_llseek_size); |
| 210 | |
| 211 | /** |
jan Blunck | ae6afc3 | 2010-05-26 14:44:48 -0700 | [diff] [blame] | 212 | * noop_llseek - No Operation Performed llseek implementation |
| 213 | * @file: file structure to seek on |
| 214 | * @offset: file offset to seek to |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 215 | * @whence: type of seek |
jan Blunck | ae6afc3 | 2010-05-26 14:44:48 -0700 | [diff] [blame] | 216 | * |
| 217 | * This is an implementation of ->llseek useable for the rare special case when |
| 218 | * userspace expects the seek to succeed but the (device) file is actually not |
| 219 | * able to perform the seek. In this case you use noop_llseek() instead of |
| 220 | * falling back to the default implementation of ->llseek. |
| 221 | */ |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 222 | loff_t noop_llseek(struct file *file, loff_t offset, int whence) |
jan Blunck | ae6afc3 | 2010-05-26 14:44:48 -0700 | [diff] [blame] | 223 | { |
| 224 | return file->f_pos; |
| 225 | } |
| 226 | EXPORT_SYMBOL(noop_llseek); |
| 227 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 228 | loff_t no_llseek(struct file *file, loff_t offset, int whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
| 230 | return -ESPIPE; |
| 231 | } |
| 232 | EXPORT_SYMBOL(no_llseek); |
| 233 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 234 | loff_t default_llseek(struct file *file, loff_t offset, int whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 236 | struct inode *inode = file_inode(file); |
David Sterba | 16abef0 | 2008-04-22 15:09:22 +0200 | [diff] [blame] | 237 | loff_t retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 239 | inode_lock(inode); |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 240 | switch (whence) { |
Chris Snook | 7b8e892 | 2007-05-08 00:24:13 -0700 | [diff] [blame] | 241 | case SEEK_END: |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 242 | offset += i_size_read(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | break; |
Chris Snook | 7b8e892 | 2007-05-08 00:24:13 -0700 | [diff] [blame] | 244 | case SEEK_CUR: |
Alain Knaff | 5b6f1eb | 2008-11-10 17:08:08 -0800 | [diff] [blame] | 245 | if (offset == 0) { |
| 246 | retval = file->f_pos; |
| 247 | goto out; |
| 248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | offset += file->f_pos; |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 250 | break; |
| 251 | case SEEK_DATA: |
| 252 | /* |
| 253 | * In the generic case the entire file is data, so as |
| 254 | * long as offset isn't at the end of the file then the |
| 255 | * offset is data. |
| 256 | */ |
Dan Carpenter | bacb2d8 | 2011-07-26 17:25:20 +0300 | [diff] [blame] | 257 | if (offset >= inode->i_size) { |
| 258 | retval = -ENXIO; |
| 259 | goto out; |
| 260 | } |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 261 | break; |
| 262 | case SEEK_HOLE: |
| 263 | /* |
| 264 | * There is a virtual hole at the end of the file, so |
| 265 | * as long as offset isn't i_size or larger, return |
| 266 | * i_size. |
| 267 | */ |
Dan Carpenter | bacb2d8 | 2011-07-26 17:25:20 +0300 | [diff] [blame] | 268 | if (offset >= inode->i_size) { |
| 269 | retval = -ENXIO; |
| 270 | goto out; |
| 271 | } |
Josef Bacik | 982d816 | 2011-07-18 13:21:35 -0400 | [diff] [blame] | 272 | offset = inode->i_size; |
| 273 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | retval = -EINVAL; |
Al Viro | cccb5a1 | 2010-12-17 07:44:05 -0500 | [diff] [blame] | 276 | if (offset >= 0 || unsigned_offsets(file)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | if (offset != file->f_pos) { |
| 278 | file->f_pos = offset; |
| 279 | file->f_version = 0; |
| 280 | } |
| 281 | retval = offset; |
| 282 | } |
Alain Knaff | 5b6f1eb | 2008-11-10 17:08:08 -0800 | [diff] [blame] | 283 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 284 | inode_unlock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | return retval; |
| 286 | } |
| 287 | EXPORT_SYMBOL(default_llseek); |
| 288 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 289 | loff_t vfs_llseek(struct file *file, loff_t offset, int whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | loff_t (*fn)(struct file *, loff_t, int); |
| 292 | |
| 293 | fn = no_llseek; |
| 294 | if (file->f_mode & FMODE_LSEEK) { |
Al Viro | 72c2d53 | 2013-09-22 16:27:52 -0400 | [diff] [blame] | 295 | if (file->f_op->llseek) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | fn = file->f_op->llseek; |
| 297 | } |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 298 | return fn(file, offset, whence); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | EXPORT_SYMBOL(vfs_llseek); |
| 301 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 302 | SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
| 304 | off_t retval; |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 305 | struct fd f = fdget_pos(fd); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 306 | if (!f.file) |
| 307 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | retval = -EINVAL; |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 310 | if (whence <= SEEK_MAX) { |
| 311 | loff_t res = vfs_llseek(f.file, offset, whence); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | retval = res; |
| 313 | if (res != (loff_t)retval) |
| 314 | retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */ |
| 315 | } |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 316 | fdput_pos(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return retval; |
| 318 | } |
| 319 | |
Al Viro | 561c673 | 2013-02-24 10:52:26 -0500 | [diff] [blame] | 320 | #ifdef CONFIG_COMPAT |
| 321 | COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence) |
| 322 | { |
| 323 | return sys_lseek(fd, offset, whence); |
| 324 | } |
| 325 | #endif |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | #ifdef __ARCH_WANT_SYS_LLSEEK |
Heiko Carstens | 003d7ab | 2009-01-14 14:14:21 +0100 | [diff] [blame] | 328 | SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high, |
| 329 | unsigned long, offset_low, loff_t __user *, result, |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 330 | unsigned int, whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { |
| 332 | int retval; |
Eric Biggers | d7a15f8 | 2014-03-16 14:24:08 -0500 | [diff] [blame] | 333 | struct fd f = fdget_pos(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | loff_t offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 336 | if (!f.file) |
| 337 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | retval = -EINVAL; |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 340 | if (whence > SEEK_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | goto out_putf; |
| 342 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 343 | offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low, |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 344 | whence); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | retval = (int)offset; |
| 347 | if (offset >= 0) { |
| 348 | retval = -EFAULT; |
| 349 | if (!copy_to_user(result, &offset, sizeof(offset))) |
| 350 | retval = 0; |
| 351 | } |
| 352 | out_putf: |
Eric Biggers | d7a15f8 | 2014-03-16 14:24:08 -0500 | [diff] [blame] | 353 | fdput_pos(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return retval; |
| 355 | } |
| 356 | #endif |
| 357 | |
Christoph Hellwig | dbe4e19 | 2015-01-25 21:11:59 +0100 | [diff] [blame] | 358 | ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos) |
| 359 | { |
| 360 | struct kiocb kiocb; |
| 361 | ssize_t ret; |
| 362 | |
| 363 | if (!file->f_op->read_iter) |
| 364 | return -EINVAL; |
| 365 | |
| 366 | init_sync_kiocb(&kiocb, file); |
| 367 | kiocb.ki_pos = *ppos; |
Christoph Hellwig | dbe4e19 | 2015-01-25 21:11:59 +0100 | [diff] [blame] | 368 | |
| 369 | iter->type |= READ; |
| 370 | ret = file->f_op->read_iter(&kiocb, iter); |
Christoph Hellwig | 599bd19 | 2015-02-11 19:59:44 +0100 | [diff] [blame] | 371 | BUG_ON(ret == -EIOCBQUEUED); |
Christoph Hellwig | dbe4e19 | 2015-01-25 21:11:59 +0100 | [diff] [blame] | 372 | if (ret > 0) |
| 373 | *ppos = kiocb.ki_pos; |
| 374 | return ret; |
| 375 | } |
| 376 | EXPORT_SYMBOL(vfs_iter_read); |
| 377 | |
| 378 | ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos) |
| 379 | { |
| 380 | struct kiocb kiocb; |
| 381 | ssize_t ret; |
| 382 | |
| 383 | if (!file->f_op->write_iter) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | init_sync_kiocb(&kiocb, file); |
| 387 | kiocb.ki_pos = *ppos; |
Christoph Hellwig | dbe4e19 | 2015-01-25 21:11:59 +0100 | [diff] [blame] | 388 | |
| 389 | iter->type |= WRITE; |
| 390 | ret = file->f_op->write_iter(&kiocb, iter); |
Christoph Hellwig | 599bd19 | 2015-02-11 19:59:44 +0100 | [diff] [blame] | 391 | BUG_ON(ret == -EIOCBQUEUED); |
Jimmy Durand Wesolowski | c5cc933 | 2019-01-31 15:19:39 +0100 | [diff] [blame] | 392 | if (ret > 0) { |
Christoph Hellwig | dbe4e19 | 2015-01-25 21:11:59 +0100 | [diff] [blame] | 393 | *ppos = kiocb.ki_pos; |
Jimmy Durand Wesolowski | c5cc933 | 2019-01-31 15:19:39 +0100 | [diff] [blame] | 394 | fsnotify_modify(file); |
| 395 | } |
Christoph Hellwig | dbe4e19 | 2015-01-25 21:11:59 +0100 | [diff] [blame] | 396 | return ret; |
| 397 | } |
| 398 | EXPORT_SYMBOL(vfs_iter_write); |
| 399 | |
Al Viro | 68d70d0 | 2013-06-19 15:26:04 +0400 | [diff] [blame] | 400 | int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | { |
| 402 | struct inode *inode; |
| 403 | loff_t pos; |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 404 | int retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 406 | inode = file_inode(file); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 407 | if (unlikely((ssize_t) count < 0)) |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 408 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | pos = *ppos; |
Al Viro | cccb5a1 | 2010-12-17 07:44:05 -0500 | [diff] [blame] | 410 | if (unlikely(pos < 0)) { |
| 411 | if (!unsigned_offsets(file)) |
| 412 | return retval; |
| 413 | if (count >= -pos) /* both values are in 0..LLONG_MAX */ |
| 414 | return -EOVERFLOW; |
| 415 | } else if (unlikely((loff_t) (pos + count) < 0)) { |
| 416 | if (!unsigned_offsets(file)) |
KAMEZAWA Hiroyuki | 4a3956c | 2010-10-01 14:20:22 -0700 | [diff] [blame] | 417 | return retval; |
| 418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 420 | if (unlikely(inode->i_flctx && mandatory_lock(inode))) { |
Christoph Hellwig | acc1557 | 2015-12-03 12:59:49 +0100 | [diff] [blame] | 421 | retval = locks_mandatory_area(inode, file, pos, pos + count - 1, |
| 422 | read_write == READ ? F_RDLCK : F_WRLCK); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 423 | if (retval < 0) |
| 424 | return retval; |
| 425 | } |
Al Viro | bc61384 | 2016-03-31 21:48:20 -0400 | [diff] [blame] | 426 | return security_file_permission(file, |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 427 | read_write == READ ? MAY_READ : MAY_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Al Viro | 5d5d568 | 2015-04-03 15:41:18 -0400 | [diff] [blame] | 430 | static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos) |
Al Viro | 293bc98 | 2014-02-11 18:37:41 -0500 | [diff] [blame] | 431 | { |
| 432 | struct iovec iov = { .iov_base = buf, .iov_len = len }; |
| 433 | struct kiocb kiocb; |
| 434 | struct iov_iter iter; |
| 435 | ssize_t ret; |
| 436 | |
| 437 | init_sync_kiocb(&kiocb, filp); |
| 438 | kiocb.ki_pos = *ppos; |
Al Viro | 293bc98 | 2014-02-11 18:37:41 -0500 | [diff] [blame] | 439 | iov_iter_init(&iter, READ, &iov, 1, len); |
| 440 | |
| 441 | ret = filp->f_op->read_iter(&kiocb, &iter); |
Christoph Hellwig | 599bd19 | 2015-02-11 19:59:44 +0100 | [diff] [blame] | 442 | BUG_ON(ret == -EIOCBQUEUED); |
Al Viro | 293bc98 | 2014-02-11 18:37:41 -0500 | [diff] [blame] | 443 | *ppos = kiocb.ki_pos; |
| 444 | return ret; |
| 445 | } |
| 446 | |
Dmitry Kasatkin | 6fb5032 | 2014-11-05 17:01:17 +0200 | [diff] [blame] | 447 | ssize_t __vfs_read(struct file *file, char __user *buf, size_t count, |
| 448 | loff_t *pos) |
| 449 | { |
Dmitry Kasatkin | 6fb5032 | 2014-11-05 17:01:17 +0200 | [diff] [blame] | 450 | if (file->f_op->read) |
Al Viro | 3d04c8a | 2015-04-03 15:09:18 -0400 | [diff] [blame] | 451 | return file->f_op->read(file, buf, count, pos); |
Dmitry Kasatkin | 6fb5032 | 2014-11-05 17:01:17 +0200 | [diff] [blame] | 452 | else if (file->f_op->read_iter) |
Al Viro | 3d04c8a | 2015-04-03 15:09:18 -0400 | [diff] [blame] | 453 | return new_sync_read(file, buf, count, pos); |
Dmitry Kasatkin | 6fb5032 | 2014-11-05 17:01:17 +0200 | [diff] [blame] | 454 | else |
Al Viro | 3d04c8a | 2015-04-03 15:09:18 -0400 | [diff] [blame] | 455 | return -EINVAL; |
Dmitry Kasatkin | 6fb5032 | 2014-11-05 17:01:17 +0200 | [diff] [blame] | 456 | } |
Al Viro | 3d04c8a | 2015-04-03 15:09:18 -0400 | [diff] [blame] | 457 | EXPORT_SYMBOL(__vfs_read); |
Dmitry Kasatkin | 6fb5032 | 2014-11-05 17:01:17 +0200 | [diff] [blame] | 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) |
| 460 | { |
| 461 | ssize_t ret; |
| 462 | |
| 463 | if (!(file->f_mode & FMODE_READ)) |
| 464 | return -EBADF; |
Al Viro | 7f7f25e | 2014-02-11 17:49:24 -0500 | [diff] [blame] | 465 | if (!(file->f_mode & FMODE_CAN_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | return -EINVAL; |
| 467 | if (unlikely(!access_ok(VERIFY_WRITE, buf, count))) |
| 468 | return -EFAULT; |
| 469 | |
| 470 | ret = rw_verify_area(READ, file, pos, count); |
Al Viro | bc61384 | 2016-03-31 21:48:20 -0400 | [diff] [blame] | 471 | if (!ret) { |
| 472 | if (count > MAX_RW_COUNT) |
| 473 | count = MAX_RW_COUNT; |
Dmitry Kasatkin | 6fb5032 | 2014-11-05 17:01:17 +0200 | [diff] [blame] | 474 | ret = __vfs_read(file, buf, count, pos); |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 475 | if (ret > 0) { |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 476 | fsnotify_access(file); |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 477 | add_rchar(current, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 479 | inc_syscr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | EXPORT_SYMBOL(vfs_read); |
| 486 | |
Al Viro | 5d5d568 | 2015-04-03 15:41:18 -0400 | [diff] [blame] | 487 | static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos) |
Al Viro | 293bc98 | 2014-02-11 18:37:41 -0500 | [diff] [blame] | 488 | { |
| 489 | struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len }; |
| 490 | struct kiocb kiocb; |
| 491 | struct iov_iter iter; |
| 492 | ssize_t ret; |
| 493 | |
| 494 | init_sync_kiocb(&kiocb, filp); |
| 495 | kiocb.ki_pos = *ppos; |
Al Viro | 293bc98 | 2014-02-11 18:37:41 -0500 | [diff] [blame] | 496 | iov_iter_init(&iter, WRITE, &iov, 1, len); |
| 497 | |
| 498 | ret = filp->f_op->write_iter(&kiocb, &iter); |
Christoph Hellwig | 599bd19 | 2015-02-11 19:59:44 +0100 | [diff] [blame] | 499 | BUG_ON(ret == -EIOCBQUEUED); |
Al Viro | f765b13 | 2015-04-06 20:50:38 -0400 | [diff] [blame] | 500 | if (ret > 0) |
| 501 | *ppos = kiocb.ki_pos; |
Al Viro | 293bc98 | 2014-02-11 18:37:41 -0500 | [diff] [blame] | 502 | return ret; |
| 503 | } |
| 504 | |
Al Viro | 493c84c | 2015-04-03 15:06:43 -0400 | [diff] [blame] | 505 | ssize_t __vfs_write(struct file *file, const char __user *p, size_t count, |
| 506 | loff_t *pos) |
| 507 | { |
| 508 | if (file->f_op->write) |
| 509 | return file->f_op->write(file, p, count, pos); |
Al Viro | 493c84c | 2015-04-03 15:06:43 -0400 | [diff] [blame] | 510 | else if (file->f_op->write_iter) |
| 511 | return new_sync_write(file, p, count, pos); |
| 512 | else |
| 513 | return -EINVAL; |
| 514 | } |
| 515 | EXPORT_SYMBOL(__vfs_write); |
| 516 | |
Al Viro | 06ae43f | 2013-03-20 13:19:30 -0400 | [diff] [blame] | 517 | ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos) |
| 518 | { |
| 519 | mm_segment_t old_fs; |
| 520 | const char __user *p; |
| 521 | ssize_t ret; |
| 522 | |
Al Viro | 7f7f25e | 2014-02-11 17:49:24 -0500 | [diff] [blame] | 523 | if (!(file->f_mode & FMODE_CAN_WRITE)) |
Al Viro | 3e84f48 | 2013-03-27 15:20:30 +0000 | [diff] [blame] | 524 | return -EINVAL; |
| 525 | |
Al Viro | 06ae43f | 2013-03-20 13:19:30 -0400 | [diff] [blame] | 526 | old_fs = get_fs(); |
| 527 | set_fs(get_ds()); |
| 528 | p = (__force const char __user *)buf; |
| 529 | if (count > MAX_RW_COUNT) |
| 530 | count = MAX_RW_COUNT; |
Al Viro | 493c84c | 2015-04-03 15:06:43 -0400 | [diff] [blame] | 531 | ret = __vfs_write(file, p, count, pos); |
Al Viro | 06ae43f | 2013-03-20 13:19:30 -0400 | [diff] [blame] | 532 | set_fs(old_fs); |
| 533 | if (ret > 0) { |
| 534 | fsnotify_modify(file); |
| 535 | add_wchar(current, ret); |
| 536 | } |
| 537 | inc_syscw(current); |
| 538 | return ret; |
| 539 | } |
| 540 | |
Al Viro | 2ec3a12 | 2014-08-19 11:48:09 -0400 | [diff] [blame] | 541 | EXPORT_SYMBOL(__kernel_write); |
| 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) |
| 544 | { |
| 545 | ssize_t ret; |
| 546 | |
| 547 | if (!(file->f_mode & FMODE_WRITE)) |
| 548 | return -EBADF; |
Al Viro | 7f7f25e | 2014-02-11 17:49:24 -0500 | [diff] [blame] | 549 | if (!(file->f_mode & FMODE_CAN_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | return -EINVAL; |
| 551 | if (unlikely(!access_ok(VERIFY_READ, buf, count))) |
| 552 | return -EFAULT; |
| 553 | |
| 554 | ret = rw_verify_area(WRITE, file, pos, count); |
Al Viro | bc61384 | 2016-03-31 21:48:20 -0400 | [diff] [blame] | 555 | if (!ret) { |
| 556 | if (count > MAX_RW_COUNT) |
| 557 | count = MAX_RW_COUNT; |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 558 | file_start_write(file); |
Al Viro | 493c84c | 2015-04-03 15:06:43 -0400 | [diff] [blame] | 559 | ret = __vfs_write(file, buf, count, pos); |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 560 | if (ret > 0) { |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 561 | fsnotify_modify(file); |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 562 | add_wchar(current, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
James Morris | c43e259 | 2008-01-12 22:05:48 +1100 | [diff] [blame] | 564 | inc_syscw(current); |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 565 | file_end_write(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | EXPORT_SYMBOL(vfs_write); |
| 572 | |
| 573 | static inline loff_t file_pos_read(struct file *file) |
| 574 | { |
Kirill Smelkov | 9c829b6 | 2019-03-26 22:20:43 +0000 | [diff] [blame] | 575 | return file->f_mode & FMODE_STREAM ? 0 : file->f_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static inline void file_pos_write(struct file *file, loff_t pos) |
| 579 | { |
Kirill Smelkov | 9c829b6 | 2019-03-26 22:20:43 +0000 | [diff] [blame] | 580 | if ((file->f_mode & FMODE_STREAM) == 0) |
| 581 | file->f_pos = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 584 | SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | { |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 586 | struct fd f = fdget_pos(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 589 | if (f.file) { |
| 590 | loff_t pos = file_pos_read(f.file); |
| 591 | ret = vfs_read(f.file, buf, count, &pos); |
Al Viro | 5faf153 | 2013-06-15 05:49:36 +0400 | [diff] [blame] | 592 | if (ret >= 0) |
| 593 | file_pos_write(f.file, pos); |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 594 | fdput_pos(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | return ret; |
| 597 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 599 | SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, |
| 600 | size_t, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 602 | struct fd f = fdget_pos(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 605 | if (f.file) { |
| 606 | loff_t pos = file_pos_read(f.file); |
| 607 | ret = vfs_write(f.file, buf, count, &pos); |
Al Viro | 5faf153 | 2013-06-15 05:49:36 +0400 | [diff] [blame] | 608 | if (ret >= 0) |
| 609 | file_pos_write(f.file, pos); |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 610 | fdput_pos(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | return ret; |
| 614 | } |
| 615 | |
Al Viro | 4a0fd5b | 2013-01-21 15:16:58 -0500 | [diff] [blame] | 616 | SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf, |
| 617 | size_t, count, loff_t, pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 619 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | if (pos < 0) |
| 623 | return -EINVAL; |
| 624 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 625 | f = fdget(fd); |
| 626 | if (f.file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | ret = -ESPIPE; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 628 | if (f.file->f_mode & FMODE_PREAD) |
| 629 | ret = vfs_read(f.file, buf, count, &pos); |
| 630 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | return ret; |
| 634 | } |
| 635 | |
Al Viro | 4a0fd5b | 2013-01-21 15:16:58 -0500 | [diff] [blame] | 636 | SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf, |
| 637 | size_t, count, loff_t, pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 639 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
| 642 | if (pos < 0) |
| 643 | return -EINVAL; |
| 644 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 645 | f = fdget(fd); |
| 646 | if (f.file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | ret = -ESPIPE; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 648 | if (f.file->f_mode & FMODE_PWRITE) |
| 649 | ret = vfs_write(f.file, buf, count, &pos); |
| 650 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | return ret; |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Reduce an iovec's length in-place. Return the resulting number of segments |
| 658 | */ |
| 659 | unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to) |
| 660 | { |
| 661 | unsigned long seg = 0; |
| 662 | size_t len = 0; |
| 663 | |
| 664 | while (seg < nr_segs) { |
| 665 | seg++; |
| 666 | if (len + iov->iov_len >= to) { |
| 667 | iov->iov_len = to - len; |
| 668 | break; |
| 669 | } |
| 670 | len += iov->iov_len; |
| 671 | iov++; |
| 672 | } |
| 673 | return seg; |
| 674 | } |
Eric Sandeen | 1929552 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 675 | EXPORT_SYMBOL(iov_shorten); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
Al Viro | ac15ac0 | 2015-03-20 20:10:21 -0400 | [diff] [blame] | 677 | static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter, |
Miklos Szeredi | 0467626 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 678 | loff_t *ppos, int type, int flags) |
Al Viro | 293bc98 | 2014-02-11 18:37:41 -0500 | [diff] [blame] | 679 | { |
| 680 | struct kiocb kiocb; |
Al Viro | 293bc98 | 2014-02-11 18:37:41 -0500 | [diff] [blame] | 681 | ssize_t ret; |
| 682 | |
Christoph Hellwig | e864f39 | 2016-04-07 08:52:03 -0700 | [diff] [blame] | 683 | if (flags & ~(RWF_HIPRI | RWF_DSYNC | RWF_SYNC)) |
Christoph Hellwig | 793b80e | 2016-03-03 16:03:58 +0100 | [diff] [blame] | 684 | return -EOPNOTSUPP; |
| 685 | |
Al Viro | 293bc98 | 2014-02-11 18:37:41 -0500 | [diff] [blame] | 686 | init_sync_kiocb(&kiocb, filp); |
Christoph Hellwig | 97be7eb | 2016-03-03 16:04:01 +0100 | [diff] [blame] | 687 | if (flags & RWF_HIPRI) |
| 688 | kiocb.ki_flags |= IOCB_HIPRI; |
Christoph Hellwig | e864f39 | 2016-04-07 08:52:03 -0700 | [diff] [blame] | 689 | if (flags & RWF_DSYNC) |
| 690 | kiocb.ki_flags |= IOCB_DSYNC; |
| 691 | if (flags & RWF_SYNC) |
| 692 | kiocb.ki_flags |= (IOCB_DSYNC | IOCB_SYNC); |
Al Viro | 293bc98 | 2014-02-11 18:37:41 -0500 | [diff] [blame] | 693 | kiocb.ki_pos = *ppos; |
Al Viro | 293bc98 | 2014-02-11 18:37:41 -0500 | [diff] [blame] | 694 | |
Miklos Szeredi | 0467626 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 695 | if (type == READ) |
| 696 | ret = filp->f_op->read_iter(&kiocb, iter); |
| 697 | else |
| 698 | ret = filp->f_op->write_iter(&kiocb, iter); |
Christoph Hellwig | 599bd19 | 2015-02-11 19:59:44 +0100 | [diff] [blame] | 699 | BUG_ON(ret == -EIOCBQUEUED); |
Al Viro | 293bc98 | 2014-02-11 18:37:41 -0500 | [diff] [blame] | 700 | *ppos = kiocb.ki_pos; |
| 701 | return ret; |
| 702 | } |
| 703 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 704 | /* Do it by hand, with file-ops */ |
Al Viro | ac15ac0 | 2015-03-20 20:10:21 -0400 | [diff] [blame] | 705 | static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter, |
Miklos Szeredi | 0467626 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 706 | loff_t *ppos, int type, int flags) |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 707 | { |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 708 | ssize_t ret = 0; |
| 709 | |
Christoph Hellwig | 97be7eb | 2016-03-03 16:04:01 +0100 | [diff] [blame] | 710 | if (flags & ~RWF_HIPRI) |
Christoph Hellwig | 793b80e | 2016-03-03 16:03:58 +0100 | [diff] [blame] | 711 | return -EOPNOTSUPP; |
| 712 | |
Al Viro | ac15ac0 | 2015-03-20 20:10:21 -0400 | [diff] [blame] | 713 | while (iov_iter_count(iter)) { |
| 714 | struct iovec iovec = iov_iter_iovec(iter); |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 715 | ssize_t nr; |
| 716 | |
Miklos Szeredi | 0467626 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 717 | if (type == READ) { |
| 718 | nr = filp->f_op->read(filp, iovec.iov_base, |
| 719 | iovec.iov_len, ppos); |
| 720 | } else { |
| 721 | nr = filp->f_op->write(filp, iovec.iov_base, |
| 722 | iovec.iov_len, ppos); |
| 723 | } |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 724 | |
| 725 | if (nr < 0) { |
| 726 | if (!ret) |
| 727 | ret = nr; |
| 728 | break; |
| 729 | } |
| 730 | ret += nr; |
Al Viro | ac15ac0 | 2015-03-20 20:10:21 -0400 | [diff] [blame] | 731 | if (nr != iovec.iov_len) |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 732 | break; |
Al Viro | ac15ac0 | 2015-03-20 20:10:21 -0400 | [diff] [blame] | 733 | iov_iter_advance(iter, nr); |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | return ret; |
| 737 | } |
| 738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | /* A write operation does a read from user space and vice versa */ |
| 740 | #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ) |
| 741 | |
Vegard Nossum | ffecee4 | 2016-10-08 11:18:07 +0200 | [diff] [blame] | 742 | /** |
| 743 | * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace |
| 744 | * into the kernel and check that it is valid. |
| 745 | * |
| 746 | * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE. |
| 747 | * @uvector: Pointer to the userspace array. |
| 748 | * @nr_segs: Number of elements in userspace array. |
| 749 | * @fast_segs: Number of elements in @fast_pointer. |
| 750 | * @fast_pointer: Pointer to (usually small on-stack) kernel array. |
| 751 | * @ret_pointer: (output parameter) Pointer to a variable that will point to |
| 752 | * either @fast_pointer, a newly allocated kernel array, or NULL, |
| 753 | * depending on which array was used. |
| 754 | * |
| 755 | * This function copies an array of &struct iovec of @nr_segs from |
| 756 | * userspace into the kernel and checks that each element is valid (e.g. |
| 757 | * it does not point to a kernel address or cause overflow by being too |
| 758 | * large, etc.). |
| 759 | * |
| 760 | * As an optimization, the caller may provide a pointer to a small |
| 761 | * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long |
| 762 | * (the size of this array, or 0 if unused, should be given in @fast_segs). |
| 763 | * |
| 764 | * @ret_pointer will always point to the array that was used, so the |
| 765 | * caller must take care not to call kfree() on it e.g. in case the |
| 766 | * @fast_pointer array was used and it was allocated on the stack. |
| 767 | * |
| 768 | * Return: The total number of bytes covered by the iovec array on success |
| 769 | * or a negative error code on error. |
| 770 | */ |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 771 | ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector, |
| 772 | unsigned long nr_segs, unsigned long fast_segs, |
| 773 | struct iovec *fast_pointer, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 774 | struct iovec **ret_pointer) |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 775 | { |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 776 | unsigned long seg; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 777 | ssize_t ret; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 778 | struct iovec *iov = fast_pointer; |
| 779 | |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 780 | /* |
| 781 | * SuS says "The readv() function *may* fail if the iovcnt argument |
| 782 | * was less than or equal to 0, or greater than {IOV_MAX}. Linux has |
| 783 | * traditionally returned zero for zero segments, so... |
| 784 | */ |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 785 | if (nr_segs == 0) { |
| 786 | ret = 0; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 787 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 790 | /* |
| 791 | * First get the "struct iovec" from user memory and |
| 792 | * verify all the pointers |
| 793 | */ |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 794 | if (nr_segs > UIO_MAXIOV) { |
| 795 | ret = -EINVAL; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 796 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 797 | } |
| 798 | if (nr_segs > fast_segs) { |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 799 | iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL); |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 800 | if (iov == NULL) { |
| 801 | ret = -ENOMEM; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 802 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 803 | } |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 804 | } |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 805 | if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) { |
| 806 | ret = -EFAULT; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 807 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 810 | /* |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 811 | * According to the Single Unix Specification we should return EINVAL |
| 812 | * if an element length is < 0 when cast to ssize_t or if the |
| 813 | * total length would overflow the ssize_t return value of the |
| 814 | * system call. |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 815 | * |
| 816 | * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the |
| 817 | * overflow case. |
| 818 | */ |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 819 | ret = 0; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 820 | for (seg = 0; seg < nr_segs; seg++) { |
| 821 | void __user *buf = iov[seg].iov_base; |
| 822 | ssize_t len = (ssize_t)iov[seg].iov_len; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 823 | |
| 824 | /* see if we we're about to use an invalid len or if |
| 825 | * it's about to overflow ssize_t */ |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 826 | if (len < 0) { |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 827 | ret = -EINVAL; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 828 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 829 | } |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 830 | if (type >= 0 |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 831 | && unlikely(!access_ok(vrfy_dir(type), buf, len))) { |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 832 | ret = -EFAULT; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 833 | goto out; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 834 | } |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 835 | if (len > MAX_RW_COUNT - ret) { |
| 836 | len = MAX_RW_COUNT - ret; |
| 837 | iov[seg].iov_len = len; |
| 838 | } |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 839 | ret += len; |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 840 | } |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 841 | out: |
| 842 | *ret_pointer = iov; |
| 843 | return ret; |
| 844 | } |
| 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | static ssize_t do_readv_writev(int type, struct file *file, |
| 847 | const struct iovec __user * uvector, |
Christoph Hellwig | 793b80e | 2016-03-03 16:03:58 +0100 | [diff] [blame] | 848 | unsigned long nr_segs, loff_t *pos, |
| 849 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | size_t tot_len; |
| 852 | struct iovec iovstack[UIO_FASTIOV]; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 853 | struct iovec *iov = iovstack; |
Al Viro | ac15ac0 | 2015-03-20 20:10:21 -0400 | [diff] [blame] | 854 | struct iov_iter iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
Al Viro | 0504c07 | 2015-03-21 19:40:11 -0400 | [diff] [blame] | 857 | ret = import_iovec(type, uvector, nr_segs, |
| 858 | ARRAY_SIZE(iovstack), &iov, &iter); |
| 859 | if (ret < 0) |
| 860 | return ret; |
Badari Pulavarty | eed4e51 | 2006-09-30 23:28:49 -0700 | [diff] [blame] | 861 | |
Al Viro | 0504c07 | 2015-03-21 19:40:11 -0400 | [diff] [blame] | 862 | tot_len = iov_iter_count(&iter); |
| 863 | if (!tot_len) |
| 864 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | ret = rw_verify_area(type, file, pos, tot_len); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 866 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | goto out; |
| 868 | |
Miklos Szeredi | 0467626 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 869 | if (type != READ) |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 870 | file_start_write(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
Miklos Szeredi | 0467626 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 872 | if ((type == READ && file->f_op->read_iter) || |
| 873 | (type == WRITE && file->f_op->write_iter)) |
| 874 | ret = do_iter_readv_writev(file, &iter, pos, type, flags); |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 875 | else |
Miklos Szeredi | 0467626 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 876 | ret = do_loop_readv_writev(file, &iter, pos, type, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 878 | if (type != READ) |
| 879 | file_end_write(file); |
| 880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | out: |
Al Viro | 0504c07 | 2015-03-21 19:40:11 -0400 | [diff] [blame] | 882 | kfree(iov); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 883 | if ((ret + (type == READ)) > 0) { |
| 884 | if (type == READ) |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 885 | fsnotify_access(file); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 886 | else |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 887 | fsnotify_modify(file); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 888 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | ssize_t vfs_readv(struct file *file, const struct iovec __user *vec, |
Christoph Hellwig | 793b80e | 2016-03-03 16:03:58 +0100 | [diff] [blame] | 893 | unsigned long vlen, loff_t *pos, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | { |
| 895 | if (!(file->f_mode & FMODE_READ)) |
| 896 | return -EBADF; |
Al Viro | 7f7f25e | 2014-02-11 17:49:24 -0500 | [diff] [blame] | 897 | if (!(file->f_mode & FMODE_CAN_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | return -EINVAL; |
| 899 | |
Christoph Hellwig | 793b80e | 2016-03-03 16:03:58 +0100 | [diff] [blame] | 900 | return do_readv_writev(READ, file, vec, vlen, pos, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | EXPORT_SYMBOL(vfs_readv); |
| 904 | |
| 905 | ssize_t vfs_writev(struct file *file, const struct iovec __user *vec, |
Christoph Hellwig | 793b80e | 2016-03-03 16:03:58 +0100 | [diff] [blame] | 906 | unsigned long vlen, loff_t *pos, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | { |
| 908 | if (!(file->f_mode & FMODE_WRITE)) |
| 909 | return -EBADF; |
Al Viro | 7f7f25e | 2014-02-11 17:49:24 -0500 | [diff] [blame] | 910 | if (!(file->f_mode & FMODE_CAN_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | return -EINVAL; |
| 912 | |
Christoph Hellwig | 793b80e | 2016-03-03 16:03:58 +0100 | [diff] [blame] | 913 | return do_readv_writev(WRITE, file, vec, vlen, pos, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | EXPORT_SYMBOL(vfs_writev); |
| 917 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 918 | static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec, |
| 919 | unsigned long vlen, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | { |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 921 | struct fd f = fdget_pos(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 924 | if (f.file) { |
| 925 | loff_t pos = file_pos_read(f.file); |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 926 | ret = vfs_readv(f.file, vec, vlen, &pos, flags); |
Al Viro | 5faf153 | 2013-06-15 05:49:36 +0400 | [diff] [blame] | 927 | if (ret >= 0) |
| 928 | file_pos_write(f.file, pos); |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 929 | fdput_pos(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | if (ret > 0) |
Alexey Dobriyan | 4b98d11 | 2007-02-10 01:46:45 -0800 | [diff] [blame] | 933 | add_rchar(current, ret); |
| 934 | inc_syscr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | return ret; |
| 936 | } |
| 937 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 938 | static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec, |
| 939 | unsigned long vlen, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | { |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 941 | struct fd f = fdget_pos(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | ssize_t ret = -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 944 | if (f.file) { |
| 945 | loff_t pos = file_pos_read(f.file); |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 946 | ret = vfs_writev(f.file, vec, vlen, &pos, flags); |
Al Viro | 5faf153 | 2013-06-15 05:49:36 +0400 | [diff] [blame] | 947 | if (ret >= 0) |
| 948 | file_pos_write(f.file, pos); |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 949 | fdput_pos(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | if (ret > 0) |
Alexey Dobriyan | 4b98d11 | 2007-02-10 01:46:45 -0800 | [diff] [blame] | 953 | add_wchar(current, ret); |
| 954 | inc_syscw(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | return ret; |
| 956 | } |
| 957 | |
Linus Torvalds | 601cc11 | 2009-04-03 08:03:22 -0700 | [diff] [blame] | 958 | 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] | 959 | { |
Linus Torvalds | 601cc11 | 2009-04-03 08:03:22 -0700 | [diff] [blame] | 960 | #define HALF_LONG_BITS (BITS_PER_LONG / 2) |
| 961 | return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low; |
| 962 | } |
| 963 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 964 | static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec, |
| 965 | unsigned long vlen, loff_t pos, int flags) |
Linus Torvalds | 601cc11 | 2009-04-03 08:03:22 -0700 | [diff] [blame] | 966 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 967 | struct fd f; |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 968 | ssize_t ret = -EBADF; |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 969 | |
| 970 | if (pos < 0) |
| 971 | return -EINVAL; |
| 972 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 973 | f = fdget(fd); |
| 974 | if (f.file) { |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 975 | ret = -ESPIPE; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 976 | if (f.file->f_mode & FMODE_PREAD) |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 977 | ret = vfs_readv(f.file, vec, vlen, &pos, flags); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 978 | fdput(f); |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | if (ret > 0) |
| 982 | add_rchar(current, ret); |
| 983 | inc_syscr(current); |
| 984 | return ret; |
| 985 | } |
| 986 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 987 | static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec, |
| 988 | unsigned long vlen, loff_t pos, int flags) |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 989 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 990 | struct fd f; |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 991 | ssize_t ret = -EBADF; |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 992 | |
| 993 | if (pos < 0) |
| 994 | return -EINVAL; |
| 995 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 996 | f = fdget(fd); |
| 997 | if (f.file) { |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 998 | ret = -ESPIPE; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 999 | if (f.file->f_mode & FMODE_PWRITE) |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1000 | ret = vfs_writev(f.file, vec, vlen, &pos, flags); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1001 | fdput(f); |
Gerd Hoffmann | f3554f4 | 2009-04-02 16:59:23 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | if (ret > 0) |
| 1005 | add_wchar(current, ret); |
| 1006 | inc_syscw(current); |
| 1007 | return ret; |
| 1008 | } |
| 1009 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1010 | SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec, |
| 1011 | unsigned long, vlen) |
| 1012 | { |
| 1013 | return do_readv(fd, vec, vlen, 0); |
| 1014 | } |
| 1015 | |
| 1016 | SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec, |
| 1017 | unsigned long, vlen) |
| 1018 | { |
| 1019 | return do_writev(fd, vec, vlen, 0); |
| 1020 | } |
| 1021 | |
| 1022 | SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec, |
| 1023 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) |
| 1024 | { |
| 1025 | loff_t pos = pos_from_hilo(pos_h, pos_l); |
| 1026 | |
| 1027 | return do_preadv(fd, vec, vlen, pos, 0); |
| 1028 | } |
| 1029 | |
| 1030 | SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec, |
| 1031 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h, |
| 1032 | int, flags) |
| 1033 | { |
| 1034 | loff_t pos = pos_from_hilo(pos_h, pos_l); |
| 1035 | |
| 1036 | if (pos == -1) |
| 1037 | return do_readv(fd, vec, vlen, flags); |
| 1038 | |
| 1039 | return do_preadv(fd, vec, vlen, pos, flags); |
| 1040 | } |
| 1041 | |
| 1042 | SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, |
| 1043 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) |
| 1044 | { |
| 1045 | loff_t pos = pos_from_hilo(pos_h, pos_l); |
| 1046 | |
| 1047 | return do_pwritev(fd, vec, vlen, pos, 0); |
| 1048 | } |
| 1049 | |
| 1050 | SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec, |
| 1051 | unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h, |
| 1052 | int, flags) |
| 1053 | { |
| 1054 | loff_t pos = pos_from_hilo(pos_h, pos_l); |
| 1055 | |
| 1056 | if (pos == -1) |
| 1057 | return do_writev(fd, vec, vlen, flags); |
| 1058 | |
| 1059 | return do_pwritev(fd, vec, vlen, pos, flags); |
| 1060 | } |
| 1061 | |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1062 | #ifdef CONFIG_COMPAT |
| 1063 | |
| 1064 | static ssize_t compat_do_readv_writev(int type, struct file *file, |
| 1065 | const struct compat_iovec __user *uvector, |
Christoph Hellwig | 793b80e | 2016-03-03 16:03:58 +0100 | [diff] [blame] | 1066 | unsigned long nr_segs, loff_t *pos, |
| 1067 | int flags) |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1068 | { |
| 1069 | compat_ssize_t tot_len; |
| 1070 | struct iovec iovstack[UIO_FASTIOV]; |
| 1071 | struct iovec *iov = iovstack; |
Al Viro | ac15ac0 | 2015-03-20 20:10:21 -0400 | [diff] [blame] | 1072 | struct iov_iter iter; |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1073 | ssize_t ret; |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1074 | |
Al Viro | 0504c07 | 2015-03-21 19:40:11 -0400 | [diff] [blame] | 1075 | ret = compat_import_iovec(type, uvector, nr_segs, |
| 1076 | UIO_FASTIOV, &iov, &iter); |
| 1077 | if (ret < 0) |
| 1078 | return ret; |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1079 | |
Al Viro | 0504c07 | 2015-03-21 19:40:11 -0400 | [diff] [blame] | 1080 | tot_len = iov_iter_count(&iter); |
| 1081 | if (!tot_len) |
| 1082 | goto out; |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1083 | ret = rw_verify_area(type, file, pos, tot_len); |
| 1084 | if (ret < 0) |
| 1085 | goto out; |
| 1086 | |
Miklos Szeredi | 0467626 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 1087 | if (type != READ) |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 1088 | file_start_write(file); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1089 | |
Miklos Szeredi | 0467626 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 1090 | if ((type == READ && file->f_op->read_iter) || |
| 1091 | (type == WRITE && file->f_op->write_iter)) |
| 1092 | ret = do_iter_readv_writev(file, &iter, pos, type, flags); |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 1093 | else |
Miklos Szeredi | 0467626 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 1094 | ret = do_loop_readv_writev(file, &iter, pos, type, flags); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1095 | |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 1096 | if (type != READ) |
| 1097 | file_end_write(file); |
| 1098 | |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1099 | out: |
Al Viro | 0504c07 | 2015-03-21 19:40:11 -0400 | [diff] [blame] | 1100 | kfree(iov); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1101 | if ((ret + (type == READ)) > 0) { |
| 1102 | if (type == READ) |
| 1103 | fsnotify_access(file); |
| 1104 | else |
| 1105 | fsnotify_modify(file); |
| 1106 | } |
| 1107 | return ret; |
| 1108 | } |
| 1109 | |
| 1110 | static size_t compat_readv(struct file *file, |
| 1111 | const struct compat_iovec __user *vec, |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1112 | unsigned long vlen, loff_t *pos, int flags) |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1113 | { |
| 1114 | ssize_t ret = -EBADF; |
| 1115 | |
| 1116 | if (!(file->f_mode & FMODE_READ)) |
| 1117 | goto out; |
| 1118 | |
| 1119 | ret = -EINVAL; |
Al Viro | 7f7f25e | 2014-02-11 17:49:24 -0500 | [diff] [blame] | 1120 | if (!(file->f_mode & FMODE_CAN_READ)) |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1121 | goto out; |
| 1122 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1123 | ret = compat_do_readv_writev(READ, file, vec, vlen, pos, flags); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1124 | |
| 1125 | out: |
| 1126 | if (ret > 0) |
| 1127 | add_rchar(current, ret); |
| 1128 | inc_syscr(current); |
| 1129 | return ret; |
| 1130 | } |
| 1131 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1132 | static size_t do_compat_readv(compat_ulong_t fd, |
| 1133 | const struct compat_iovec __user *vec, |
| 1134 | compat_ulong_t vlen, int flags) |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1135 | { |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 1136 | struct fd f = fdget_pos(fd); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1137 | ssize_t ret; |
| 1138 | loff_t pos; |
| 1139 | |
| 1140 | if (!f.file) |
| 1141 | return -EBADF; |
| 1142 | pos = f.file->f_pos; |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1143 | ret = compat_readv(f.file, vec, vlen, &pos, flags); |
Al Viro | 5faf153 | 2013-06-15 05:49:36 +0400 | [diff] [blame] | 1144 | if (ret >= 0) |
| 1145 | f.file->f_pos = pos; |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 1146 | fdput_pos(f); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1147 | return ret; |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1148 | |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1149 | } |
| 1150 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1151 | COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd, |
| 1152 | const struct compat_iovec __user *,vec, |
| 1153 | compat_ulong_t, vlen) |
| 1154 | { |
| 1155 | return do_compat_readv(fd, vec, vlen, 0); |
| 1156 | } |
| 1157 | |
| 1158 | static long do_compat_preadv64(unsigned long fd, |
Heiko Carstens | 378a10f | 2014-03-05 10:43:51 +0100 | [diff] [blame] | 1159 | const struct compat_iovec __user *vec, |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1160 | unsigned long vlen, loff_t pos, int flags) |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1161 | { |
| 1162 | struct fd f; |
| 1163 | ssize_t ret; |
| 1164 | |
| 1165 | if (pos < 0) |
| 1166 | return -EINVAL; |
| 1167 | f = fdget(fd); |
| 1168 | if (!f.file) |
| 1169 | return -EBADF; |
| 1170 | ret = -ESPIPE; |
| 1171 | if (f.file->f_mode & FMODE_PREAD) |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1172 | ret = compat_readv(f.file, vec, vlen, &pos, flags); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1173 | fdput(f); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
Heiko Carstens | 378a10f | 2014-03-05 10:43:51 +0100 | [diff] [blame] | 1177 | #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64 |
| 1178 | COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd, |
| 1179 | const struct compat_iovec __user *,vec, |
| 1180 | unsigned long, vlen, loff_t, pos) |
| 1181 | { |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1182 | return do_compat_preadv64(fd, vec, vlen, pos, 0); |
Heiko Carstens | 378a10f | 2014-03-05 10:43:51 +0100 | [diff] [blame] | 1183 | } |
| 1184 | #endif |
| 1185 | |
Heiko Carstens | dfd948e | 2014-01-29 14:05:44 -0800 | [diff] [blame] | 1186 | COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd, |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1187 | const struct compat_iovec __user *,vec, |
Heiko Carstens | dfd948e | 2014-01-29 14:05:44 -0800 | [diff] [blame] | 1188 | compat_ulong_t, vlen, u32, pos_low, u32, pos_high) |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1189 | { |
| 1190 | loff_t pos = ((loff_t)pos_high << 32) | pos_low; |
Heiko Carstens | 378a10f | 2014-03-05 10:43:51 +0100 | [diff] [blame] | 1191 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1192 | return do_compat_preadv64(fd, vec, vlen, pos, 0); |
| 1193 | } |
| 1194 | |
H.J. Lu | 3ebfd81 | 2016-07-14 12:31:53 -0700 | [diff] [blame] | 1195 | #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2 |
| 1196 | COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd, |
| 1197 | const struct compat_iovec __user *,vec, |
| 1198 | unsigned long, vlen, loff_t, pos, int, flags) |
| 1199 | { |
Aurelien Jarno | 24c6f9f | 2018-12-06 20:05:34 +0100 | [diff] [blame] | 1200 | if (pos == -1) |
| 1201 | return do_compat_readv(fd, vec, vlen, flags); |
| 1202 | |
H.J. Lu | 3ebfd81 | 2016-07-14 12:31:53 -0700 | [diff] [blame] | 1203 | return do_compat_preadv64(fd, vec, vlen, pos, flags); |
| 1204 | } |
| 1205 | #endif |
| 1206 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1207 | COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd, |
| 1208 | const struct compat_iovec __user *,vec, |
| 1209 | compat_ulong_t, vlen, u32, pos_low, u32, pos_high, |
| 1210 | int, flags) |
| 1211 | { |
| 1212 | loff_t pos = ((loff_t)pos_high << 32) | pos_low; |
| 1213 | |
| 1214 | if (pos == -1) |
| 1215 | return do_compat_readv(fd, vec, vlen, flags); |
| 1216 | |
| 1217 | return do_compat_preadv64(fd, vec, vlen, pos, flags); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | static size_t compat_writev(struct file *file, |
| 1221 | const struct compat_iovec __user *vec, |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1222 | unsigned long vlen, loff_t *pos, int flags) |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1223 | { |
| 1224 | ssize_t ret = -EBADF; |
| 1225 | |
| 1226 | if (!(file->f_mode & FMODE_WRITE)) |
| 1227 | goto out; |
| 1228 | |
| 1229 | ret = -EINVAL; |
Al Viro | 7f7f25e | 2014-02-11 17:49:24 -0500 | [diff] [blame] | 1230 | if (!(file->f_mode & FMODE_CAN_WRITE)) |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1231 | goto out; |
| 1232 | |
Christoph Hellwig | 222aa34 | 2017-06-16 11:08:24 +0200 | [diff] [blame] | 1233 | ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos, flags); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1234 | |
| 1235 | out: |
| 1236 | if (ret > 0) |
| 1237 | add_wchar(current, ret); |
| 1238 | inc_syscw(current); |
| 1239 | return ret; |
| 1240 | } |
| 1241 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1242 | static size_t do_compat_writev(compat_ulong_t fd, |
| 1243 | const struct compat_iovec __user* vec, |
| 1244 | compat_ulong_t vlen, int flags) |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1245 | { |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 1246 | struct fd f = fdget_pos(fd); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1247 | ssize_t ret; |
| 1248 | loff_t pos; |
| 1249 | |
| 1250 | if (!f.file) |
| 1251 | return -EBADF; |
| 1252 | pos = f.file->f_pos; |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1253 | ret = compat_writev(f.file, vec, vlen, &pos, flags); |
Al Viro | 5faf153 | 2013-06-15 05:49:36 +0400 | [diff] [blame] | 1254 | if (ret >= 0) |
| 1255 | f.file->f_pos = pos; |
Linus Torvalds | 9c225f2 | 2014-03-03 09:36:58 -0800 | [diff] [blame] | 1256 | fdput_pos(f); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1257 | return ret; |
| 1258 | } |
| 1259 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1260 | COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd, |
| 1261 | const struct compat_iovec __user *, vec, |
| 1262 | compat_ulong_t, vlen) |
| 1263 | { |
| 1264 | return do_compat_writev(fd, vec, vlen, 0); |
| 1265 | } |
| 1266 | |
| 1267 | static long do_compat_pwritev64(unsigned long fd, |
Heiko Carstens | 378a10f | 2014-03-05 10:43:51 +0100 | [diff] [blame] | 1268 | const struct compat_iovec __user *vec, |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1269 | unsigned long vlen, loff_t pos, int flags) |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1270 | { |
| 1271 | struct fd f; |
| 1272 | ssize_t ret; |
| 1273 | |
| 1274 | if (pos < 0) |
| 1275 | return -EINVAL; |
| 1276 | f = fdget(fd); |
| 1277 | if (!f.file) |
| 1278 | return -EBADF; |
| 1279 | ret = -ESPIPE; |
| 1280 | if (f.file->f_mode & FMODE_PWRITE) |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1281 | ret = compat_writev(f.file, vec, vlen, &pos, flags); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1282 | fdput(f); |
| 1283 | return ret; |
| 1284 | } |
| 1285 | |
Heiko Carstens | 378a10f | 2014-03-05 10:43:51 +0100 | [diff] [blame] | 1286 | #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64 |
| 1287 | COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd, |
| 1288 | const struct compat_iovec __user *,vec, |
| 1289 | unsigned long, vlen, loff_t, pos) |
| 1290 | { |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1291 | return do_compat_pwritev64(fd, vec, vlen, pos, 0); |
Heiko Carstens | 378a10f | 2014-03-05 10:43:51 +0100 | [diff] [blame] | 1292 | } |
| 1293 | #endif |
| 1294 | |
Heiko Carstens | dfd948e | 2014-01-29 14:05:44 -0800 | [diff] [blame] | 1295 | COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd, |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1296 | const struct compat_iovec __user *,vec, |
Heiko Carstens | dfd948e | 2014-01-29 14:05:44 -0800 | [diff] [blame] | 1297 | compat_ulong_t, vlen, u32, pos_low, u32, pos_high) |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1298 | { |
| 1299 | loff_t pos = ((loff_t)pos_high << 32) | pos_low; |
Heiko Carstens | 378a10f | 2014-03-05 10:43:51 +0100 | [diff] [blame] | 1300 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1301 | return do_compat_pwritev64(fd, vec, vlen, pos, 0); |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1302 | } |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1303 | |
H.J. Lu | 3ebfd81 | 2016-07-14 12:31:53 -0700 | [diff] [blame] | 1304 | #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2 |
| 1305 | COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd, |
| 1306 | const struct compat_iovec __user *,vec, |
| 1307 | unsigned long, vlen, loff_t, pos, int, flags) |
| 1308 | { |
Aurelien Jarno | 24c6f9f | 2018-12-06 20:05:34 +0100 | [diff] [blame] | 1309 | if (pos == -1) |
| 1310 | return do_compat_writev(fd, vec, vlen, flags); |
| 1311 | |
H.J. Lu | 3ebfd81 | 2016-07-14 12:31:53 -0700 | [diff] [blame] | 1312 | return do_compat_pwritev64(fd, vec, vlen, pos, flags); |
| 1313 | } |
| 1314 | #endif |
| 1315 | |
Milosz Tanski | f17d8b3 | 2016-03-03 16:03:59 +0100 | [diff] [blame] | 1316 | COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd, |
| 1317 | const struct compat_iovec __user *,vec, |
| 1318 | compat_ulong_t, vlen, u32, pos_low, u32, pos_high, int, flags) |
| 1319 | { |
| 1320 | loff_t pos = ((loff_t)pos_high << 32) | pos_low; |
| 1321 | |
| 1322 | if (pos == -1) |
| 1323 | return do_compat_writev(fd, vec, vlen, flags); |
| 1324 | |
| 1325 | return do_compat_pwritev64(fd, vec, vlen, pos, flags); |
| 1326 | } |
| 1327 | |
Al Viro | 72ec351 | 2013-03-20 10:42:10 -0400 | [diff] [blame] | 1328 | #endif |
| 1329 | |
Al Viro | 19f4fc3 | 2013-02-24 02:17:03 -0500 | [diff] [blame] | 1330 | static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, |
| 1331 | size_t count, loff_t max) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1333 | struct fd in, out; |
| 1334 | struct inode *in_inode, *out_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | loff_t pos; |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1336 | loff_t out_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | ssize_t retval; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1338 | int fl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | |
| 1340 | /* |
| 1341 | * Get input file, and verify that it is ok.. |
| 1342 | */ |
| 1343 | retval = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1344 | in = fdget(in_fd); |
| 1345 | if (!in.file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | goto out; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1347 | if (!(in.file->f_mode & FMODE_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | goto fput_in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | retval = -ESPIPE; |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1350 | if (!ppos) { |
| 1351 | pos = in.file->f_pos; |
| 1352 | } else { |
| 1353 | pos = *ppos; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1354 | if (!(in.file->f_mode & FMODE_PREAD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | goto fput_in; |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1356 | } |
| 1357 | retval = rw_verify_area(READ, in.file, &pos, count); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 1358 | if (retval < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | goto fput_in; |
Al Viro | bc61384 | 2016-03-31 21:48:20 -0400 | [diff] [blame] | 1360 | if (count > MAX_RW_COUNT) |
| 1361 | count = MAX_RW_COUNT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | /* |
| 1364 | * Get output file, and verify that it is ok.. |
| 1365 | */ |
| 1366 | retval = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1367 | out = fdget(out_fd); |
| 1368 | if (!out.file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | goto fput_in; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1370 | if (!(out.file->f_mode & FMODE_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | goto fput_out; |
| 1372 | retval = -EINVAL; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1373 | in_inode = file_inode(in.file); |
| 1374 | out_inode = file_inode(out.file); |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1375 | out_pos = out.file->f_pos; |
| 1376 | retval = rw_verify_area(WRITE, out.file, &out_pos, count); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 1377 | if (retval < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | goto fput_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | if (!max) |
| 1381 | max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); |
| 1382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | if (unlikely(pos + count > max)) { |
| 1384 | retval = -EOVERFLOW; |
| 1385 | if (pos >= max) |
| 1386 | goto fput_out; |
| 1387 | count = max - pos; |
| 1388 | } |
| 1389 | |
Jens Axboe | d96e6e7 | 2007-06-11 12:18:52 +0200 | [diff] [blame] | 1390 | fl = 0; |
Jens Axboe | 534f2aa | 2007-06-01 14:52:37 +0200 | [diff] [blame] | 1391 | #if 0 |
Jens Axboe | d96e6e7 | 2007-06-11 12:18:52 +0200 | [diff] [blame] | 1392 | /* |
| 1393 | * We need to debate whether we can enable this or not. The |
| 1394 | * man page documents EAGAIN return for the output at least, |
| 1395 | * and the application is arguably buggy if it doesn't expect |
| 1396 | * EAGAIN on a non-blocking file descriptor. |
| 1397 | */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1398 | if (in.file->f_flags & O_NONBLOCK) |
Jens Axboe | d96e6e7 | 2007-06-11 12:18:52 +0200 | [diff] [blame] | 1399 | fl = SPLICE_F_NONBLOCK; |
Jens Axboe | 534f2aa | 2007-06-01 14:52:37 +0200 | [diff] [blame] | 1400 | #endif |
Al Viro | 50cd2c5 | 2013-05-23 20:10:34 -0400 | [diff] [blame] | 1401 | file_start_write(out.file); |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1402 | retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl); |
Al Viro | 50cd2c5 | 2013-05-23 20:10:34 -0400 | [diff] [blame] | 1403 | file_end_write(out.file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
| 1405 | if (retval > 0) { |
Alexey Dobriyan | 4b98d11 | 2007-02-10 01:46:45 -0800 | [diff] [blame] | 1406 | add_rchar(current, retval); |
| 1407 | add_wchar(current, retval); |
Scott Wolchok | a68c2f1 | 2012-12-20 15:05:52 -0800 | [diff] [blame] | 1408 | fsnotify_access(in.file); |
| 1409 | fsnotify_modify(out.file); |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1410 | out.file->f_pos = out_pos; |
| 1411 | if (ppos) |
| 1412 | *ppos = pos; |
| 1413 | else |
| 1414 | in.file->f_pos = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
Alexey Dobriyan | 4b98d11 | 2007-02-10 01:46:45 -0800 | [diff] [blame] | 1417 | inc_syscr(current); |
| 1418 | inc_syscw(current); |
Al Viro | 7995bd2 | 2013-06-20 18:58:36 +0400 | [diff] [blame] | 1419 | if (pos > max) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | retval = -EOVERFLOW; |
| 1421 | |
| 1422 | fput_out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1423 | fdput(out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | fput_in: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1425 | fdput(in); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | out: |
| 1427 | return retval; |
| 1428 | } |
| 1429 | |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 1430 | 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] | 1431 | { |
| 1432 | loff_t pos; |
| 1433 | off_t off; |
| 1434 | ssize_t ret; |
| 1435 | |
| 1436 | if (offset) { |
| 1437 | if (unlikely(get_user(off, offset))) |
| 1438 | return -EFAULT; |
| 1439 | pos = off; |
| 1440 | ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS); |
| 1441 | if (unlikely(put_user(pos, offset))) |
| 1442 | return -EFAULT; |
| 1443 | return ret; |
| 1444 | } |
| 1445 | |
| 1446 | return do_sendfile(out_fd, in_fd, NULL, count, 0); |
| 1447 | } |
| 1448 | |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 1449 | 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] | 1450 | { |
| 1451 | loff_t pos; |
| 1452 | ssize_t ret; |
| 1453 | |
| 1454 | if (offset) { |
| 1455 | if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) |
| 1456 | return -EFAULT; |
| 1457 | ret = do_sendfile(out_fd, in_fd, &pos, count, 0); |
| 1458 | if (unlikely(put_user(pos, offset))) |
| 1459 | return -EFAULT; |
| 1460 | return ret; |
| 1461 | } |
| 1462 | |
| 1463 | return do_sendfile(out_fd, in_fd, NULL, count, 0); |
| 1464 | } |
Al Viro | 19f4fc3 | 2013-02-24 02:17:03 -0500 | [diff] [blame] | 1465 | |
| 1466 | #ifdef CONFIG_COMPAT |
| 1467 | COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, |
| 1468 | compat_off_t __user *, offset, compat_size_t, count) |
| 1469 | { |
| 1470 | loff_t pos; |
| 1471 | off_t off; |
| 1472 | ssize_t ret; |
| 1473 | |
| 1474 | if (offset) { |
| 1475 | if (unlikely(get_user(off, offset))) |
| 1476 | return -EFAULT; |
| 1477 | pos = off; |
| 1478 | ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS); |
| 1479 | if (unlikely(put_user(pos, offset))) |
| 1480 | return -EFAULT; |
| 1481 | return ret; |
| 1482 | } |
| 1483 | |
| 1484 | return do_sendfile(out_fd, in_fd, NULL, count, 0); |
| 1485 | } |
| 1486 | |
| 1487 | COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, |
| 1488 | compat_loff_t __user *, offset, compat_size_t, count) |
| 1489 | { |
| 1490 | loff_t pos; |
| 1491 | ssize_t ret; |
| 1492 | |
| 1493 | if (offset) { |
| 1494 | if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) |
| 1495 | return -EFAULT; |
| 1496 | ret = do_sendfile(out_fd, in_fd, &pos, count, 0); |
| 1497 | if (unlikely(put_user(pos, offset))) |
| 1498 | return -EFAULT; |
| 1499 | return ret; |
| 1500 | } |
| 1501 | |
| 1502 | return do_sendfile(out_fd, in_fd, NULL, count, 0); |
| 1503 | } |
| 1504 | #endif |
Zach Brown | 2973293 | 2015-11-10 16:53:30 -0500 | [diff] [blame] | 1505 | |
| 1506 | /* |
| 1507 | * copy_file_range() differs from regular file read and write in that it |
| 1508 | * specifically allows return partial success. When it does so is up to |
| 1509 | * the copy_file_range method. |
| 1510 | */ |
| 1511 | ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in, |
| 1512 | struct file *file_out, loff_t pos_out, |
| 1513 | size_t len, unsigned int flags) |
| 1514 | { |
| 1515 | struct inode *inode_in = file_inode(file_in); |
| 1516 | struct inode *inode_out = file_inode(file_out); |
| 1517 | ssize_t ret; |
| 1518 | |
| 1519 | if (flags != 0) |
| 1520 | return -EINVAL; |
| 1521 | |
Amir Goldstein | 27db1f0 | 2017-01-31 10:34:56 +0200 | [diff] [blame] | 1522 | if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode)) |
| 1523 | return -EISDIR; |
| 1524 | if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode)) |
| 1525 | return -EINVAL; |
| 1526 | |
Zach Brown | 2973293 | 2015-11-10 16:53:30 -0500 | [diff] [blame] | 1527 | ret = rw_verify_area(READ, file_in, &pos_in, len); |
Al Viro | bc61384 | 2016-03-31 21:48:20 -0400 | [diff] [blame] | 1528 | if (unlikely(ret)) |
| 1529 | return ret; |
| 1530 | |
| 1531 | ret = rw_verify_area(WRITE, file_out, &pos_out, len); |
| 1532 | if (unlikely(ret)) |
Zach Brown | 2973293 | 2015-11-10 16:53:30 -0500 | [diff] [blame] | 1533 | return ret; |
| 1534 | |
| 1535 | if (!(file_in->f_mode & FMODE_READ) || |
| 1536 | !(file_out->f_mode & FMODE_WRITE) || |
Anna Schumaker | eac7005 | 2015-11-10 16:53:33 -0500 | [diff] [blame] | 1537 | (file_out->f_flags & O_APPEND)) |
Zach Brown | 2973293 | 2015-11-10 16:53:30 -0500 | [diff] [blame] | 1538 | return -EBADF; |
| 1539 | |
| 1540 | /* this could be relaxed once a method supports cross-fs copies */ |
| 1541 | if (inode_in->i_sb != inode_out->i_sb) |
| 1542 | return -EXDEV; |
| 1543 | |
| 1544 | if (len == 0) |
| 1545 | return 0; |
| 1546 | |
| 1547 | ret = mnt_want_write_file(file_out); |
| 1548 | if (ret) |
| 1549 | return ret; |
| 1550 | |
Anna Schumaker | eac7005 | 2015-11-10 16:53:33 -0500 | [diff] [blame] | 1551 | ret = -EOPNOTSUPP; |
| 1552 | if (file_out->f_op->copy_file_range) |
| 1553 | ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out, |
| 1554 | pos_out, len, flags); |
| 1555 | if (ret == -EOPNOTSUPP) |
| 1556 | ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out, |
| 1557 | len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0); |
| 1558 | |
Zach Brown | 2973293 | 2015-11-10 16:53:30 -0500 | [diff] [blame] | 1559 | if (ret > 0) { |
| 1560 | fsnotify_access(file_in); |
| 1561 | add_rchar(current, ret); |
| 1562 | fsnotify_modify(file_out); |
| 1563 | add_wchar(current, ret); |
| 1564 | } |
| 1565 | inc_syscr(current); |
| 1566 | inc_syscw(current); |
| 1567 | |
| 1568 | mnt_drop_write_file(file_out); |
| 1569 | |
| 1570 | return ret; |
| 1571 | } |
| 1572 | EXPORT_SYMBOL(vfs_copy_file_range); |
| 1573 | |
| 1574 | SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in, |
| 1575 | int, fd_out, loff_t __user *, off_out, |
| 1576 | size_t, len, unsigned int, flags) |
| 1577 | { |
| 1578 | loff_t pos_in; |
| 1579 | loff_t pos_out; |
| 1580 | struct fd f_in; |
| 1581 | struct fd f_out; |
| 1582 | ssize_t ret = -EBADF; |
| 1583 | |
| 1584 | f_in = fdget(fd_in); |
| 1585 | if (!f_in.file) |
| 1586 | goto out2; |
| 1587 | |
| 1588 | f_out = fdget(fd_out); |
| 1589 | if (!f_out.file) |
| 1590 | goto out1; |
| 1591 | |
| 1592 | ret = -EFAULT; |
| 1593 | if (off_in) { |
| 1594 | if (copy_from_user(&pos_in, off_in, sizeof(loff_t))) |
| 1595 | goto out; |
| 1596 | } else { |
| 1597 | pos_in = f_in.file->f_pos; |
| 1598 | } |
| 1599 | |
| 1600 | if (off_out) { |
| 1601 | if (copy_from_user(&pos_out, off_out, sizeof(loff_t))) |
| 1602 | goto out; |
| 1603 | } else { |
| 1604 | pos_out = f_out.file->f_pos; |
| 1605 | } |
| 1606 | |
| 1607 | ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len, |
| 1608 | flags); |
| 1609 | if (ret > 0) { |
| 1610 | pos_in += ret; |
| 1611 | pos_out += ret; |
| 1612 | |
| 1613 | if (off_in) { |
| 1614 | if (copy_to_user(off_in, &pos_in, sizeof(loff_t))) |
| 1615 | ret = -EFAULT; |
| 1616 | } else { |
| 1617 | f_in.file->f_pos = pos_in; |
| 1618 | } |
| 1619 | |
| 1620 | if (off_out) { |
| 1621 | if (copy_to_user(off_out, &pos_out, sizeof(loff_t))) |
| 1622 | ret = -EFAULT; |
| 1623 | } else { |
| 1624 | f_out.file->f_pos = pos_out; |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | out: |
| 1629 | fdput(f_out); |
| 1630 | out1: |
| 1631 | fdput(f_in); |
| 1632 | out2: |
| 1633 | return ret; |
| 1634 | } |
Christoph Hellwig | 04b38d6 | 2015-12-03 12:59:50 +0100 | [diff] [blame] | 1635 | |
| 1636 | static int clone_verify_area(struct file *file, loff_t pos, u64 len, bool write) |
| 1637 | { |
| 1638 | struct inode *inode = file_inode(file); |
| 1639 | |
| 1640 | if (unlikely(pos < 0)) |
| 1641 | return -EINVAL; |
| 1642 | |
| 1643 | if (unlikely((loff_t) (pos + len) < 0)) |
| 1644 | return -EINVAL; |
| 1645 | |
| 1646 | if (unlikely(inode->i_flctx && mandatory_lock(inode))) { |
| 1647 | loff_t end = len ? pos + len - 1 : OFFSET_MAX; |
| 1648 | int retval; |
| 1649 | |
| 1650 | retval = locks_mandatory_area(inode, file, pos, end, |
| 1651 | write ? F_WRLCK : F_RDLCK); |
| 1652 | if (retval < 0) |
| 1653 | return retval; |
| 1654 | } |
| 1655 | |
| 1656 | return security_file_permission(file, write ? MAY_WRITE : MAY_READ); |
| 1657 | } |
| 1658 | |
| 1659 | int vfs_clone_file_range(struct file *file_in, loff_t pos_in, |
| 1660 | struct file *file_out, loff_t pos_out, u64 len) |
| 1661 | { |
| 1662 | struct inode *inode_in = file_inode(file_in); |
| 1663 | struct inode *inode_out = file_inode(file_out); |
| 1664 | int ret; |
| 1665 | |
| 1666 | if (inode_in->i_sb != inode_out->i_sb || |
| 1667 | file_in->f_path.mnt != file_out->f_path.mnt) |
| 1668 | return -EXDEV; |
| 1669 | |
| 1670 | if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode)) |
| 1671 | return -EISDIR; |
| 1672 | if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode)) |
Darrick J. Wong | d79bdd5 | 2015-12-19 00:55:52 -0800 | [diff] [blame] | 1673 | return -EINVAL; |
Christoph Hellwig | 04b38d6 | 2015-12-03 12:59:50 +0100 | [diff] [blame] | 1674 | |
| 1675 | if (!(file_in->f_mode & FMODE_READ) || |
| 1676 | !(file_out->f_mode & FMODE_WRITE) || |
Christoph Hellwig | 0fcbf99 | 2016-02-26 18:53:12 +0100 | [diff] [blame] | 1677 | (file_out->f_flags & O_APPEND)) |
Christoph Hellwig | 04b38d6 | 2015-12-03 12:59:50 +0100 | [diff] [blame] | 1678 | return -EBADF; |
| 1679 | |
Christoph Hellwig | 0fcbf99 | 2016-02-26 18:53:12 +0100 | [diff] [blame] | 1680 | if (!file_in->f_op->clone_file_range) |
| 1681 | return -EOPNOTSUPP; |
| 1682 | |
Christoph Hellwig | 04b38d6 | 2015-12-03 12:59:50 +0100 | [diff] [blame] | 1683 | ret = clone_verify_area(file_in, pos_in, len, false); |
| 1684 | if (ret) |
| 1685 | return ret; |
| 1686 | |
| 1687 | ret = clone_verify_area(file_out, pos_out, len, true); |
| 1688 | if (ret) |
| 1689 | return ret; |
| 1690 | |
| 1691 | if (pos_in + len > i_size_read(inode_in)) |
| 1692 | return -EINVAL; |
| 1693 | |
| 1694 | ret = mnt_want_write_file(file_out); |
| 1695 | if (ret) |
| 1696 | return ret; |
| 1697 | |
| 1698 | ret = file_in->f_op->clone_file_range(file_in, pos_in, |
| 1699 | file_out, pos_out, len); |
| 1700 | if (!ret) { |
| 1701 | fsnotify_access(file_in); |
| 1702 | fsnotify_modify(file_out); |
| 1703 | } |
| 1704 | |
| 1705 | mnt_drop_write_file(file_out); |
| 1706 | return ret; |
| 1707 | } |
| 1708 | EXPORT_SYMBOL(vfs_clone_file_range); |
Darrick J. Wong | 54dbc15 | 2015-12-19 00:55:59 -0800 | [diff] [blame] | 1709 | |
| 1710 | int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same) |
| 1711 | { |
| 1712 | struct file_dedupe_range_info *info; |
| 1713 | struct inode *src = file_inode(file); |
| 1714 | u64 off; |
| 1715 | u64 len; |
| 1716 | int i; |
| 1717 | int ret; |
| 1718 | bool is_admin = capable(CAP_SYS_ADMIN); |
| 1719 | u16 count = same->dest_count; |
| 1720 | struct file *dst_file; |
| 1721 | loff_t dst_off; |
| 1722 | ssize_t deduped; |
| 1723 | |
| 1724 | if (!(file->f_mode & FMODE_READ)) |
| 1725 | return -EINVAL; |
| 1726 | |
| 1727 | if (same->reserved1 || same->reserved2) |
| 1728 | return -EINVAL; |
| 1729 | |
| 1730 | off = same->src_offset; |
| 1731 | len = same->src_length; |
| 1732 | |
| 1733 | ret = -EISDIR; |
| 1734 | if (S_ISDIR(src->i_mode)) |
| 1735 | goto out; |
| 1736 | |
| 1737 | ret = -EINVAL; |
| 1738 | if (!S_ISREG(src->i_mode)) |
| 1739 | goto out; |
| 1740 | |
| 1741 | ret = clone_verify_area(file, off, len, false); |
| 1742 | if (ret < 0) |
| 1743 | goto out; |
| 1744 | ret = 0; |
| 1745 | |
| 1746 | /* pre-format output fields to sane values */ |
| 1747 | for (i = 0; i < count; i++) { |
| 1748 | same->info[i].bytes_deduped = 0ULL; |
| 1749 | same->info[i].status = FILE_DEDUPE_RANGE_SAME; |
| 1750 | } |
| 1751 | |
| 1752 | for (i = 0, info = same->info; i < count; i++, info++) { |
| 1753 | struct inode *dst; |
| 1754 | struct fd dst_fd = fdget(info->dest_fd); |
| 1755 | |
| 1756 | dst_file = dst_fd.file; |
| 1757 | if (!dst_file) { |
| 1758 | info->status = -EBADF; |
| 1759 | goto next_loop; |
| 1760 | } |
| 1761 | dst = file_inode(dst_file); |
| 1762 | |
| 1763 | ret = mnt_want_write_file(dst_file); |
| 1764 | if (ret) { |
| 1765 | info->status = ret; |
| 1766 | goto next_loop; |
| 1767 | } |
| 1768 | |
| 1769 | dst_off = info->dest_offset; |
| 1770 | ret = clone_verify_area(dst_file, dst_off, len, true); |
| 1771 | if (ret < 0) { |
| 1772 | info->status = ret; |
| 1773 | goto next_file; |
| 1774 | } |
| 1775 | ret = 0; |
| 1776 | |
| 1777 | if (info->reserved) { |
| 1778 | info->status = -EINVAL; |
| 1779 | } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) { |
| 1780 | info->status = -EINVAL; |
| 1781 | } else if (file->f_path.mnt != dst_file->f_path.mnt) { |
| 1782 | info->status = -EXDEV; |
| 1783 | } else if (S_ISDIR(dst->i_mode)) { |
| 1784 | info->status = -EISDIR; |
| 1785 | } else if (dst_file->f_op->dedupe_file_range == NULL) { |
| 1786 | info->status = -EINVAL; |
| 1787 | } else { |
| 1788 | deduped = dst_file->f_op->dedupe_file_range(file, off, |
| 1789 | len, dst_file, |
| 1790 | info->dest_offset); |
| 1791 | if (deduped == -EBADE) |
| 1792 | info->status = FILE_DEDUPE_RANGE_DIFFERS; |
| 1793 | else if (deduped < 0) |
| 1794 | info->status = deduped; |
| 1795 | else |
| 1796 | info->bytes_deduped += deduped; |
| 1797 | } |
| 1798 | |
| 1799 | next_file: |
| 1800 | mnt_drop_write_file(dst_file); |
| 1801 | next_loop: |
| 1802 | fdput(dst_fd); |
Darrick J. Wong | e62e560 | 2016-01-22 16:58:28 -0800 | [diff] [blame] | 1803 | |
| 1804 | if (fatal_signal_pending(current)) |
| 1805 | goto out; |
Darrick J. Wong | 54dbc15 | 2015-12-19 00:55:59 -0800 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | out: |
| 1809 | return ret; |
| 1810 | } |
| 1811 | EXPORT_SYMBOL(vfs_dedupe_file_range); |