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