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