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