Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/ioctl.c |
| 4 | * |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/syscalls.h> |
| 9 | #include <linux/mm.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 10 | #include <linux/capability.h> |
Arnd Bergmann | 2952db0 | 2018-09-11 16:55:03 +0200 | [diff] [blame] | 11 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/file.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/security.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 15 | #include <linux/export.h> |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 17 | #include <linux/writeback.h> |
| 18 | #include <linux/buffer_head.h> |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 19 | #include <linux/falloc.h> |
Ingo Molnar | f361bf4 | 2017-02-03 23:47:37 +0100 | [diff] [blame] | 20 | #include <linux/sched/signal.h> |
| 21 | |
Al Viro | 66cf191 | 2016-01-07 09:53:30 -0500 | [diff] [blame] | 22 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/ioctls.h> |
| 25 | |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 26 | /* So that the fiemap access checks can't overflow on 32 bit machines. */ |
| 27 | #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent)) |
| 28 | |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 29 | /** |
| 30 | * vfs_ioctl - call filesystem specific ioctl methods |
Christoph Hellwig | f6a4c8b | 2008-02-09 00:10:16 -0800 | [diff] [blame] | 31 | * @filp: open file to invoke ioctl method on |
| 32 | * @cmd: ioctl command to execute |
| 33 | * @arg: command-specific argument for ioctl |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 34 | * |
| 35 | * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 36 | * returns -ENOTTY. |
| 37 | * |
| 38 | * Returns 0 on success, -errno on error. |
| 39 | */ |
Al Viro | 66cf191 | 2016-01-07 09:53:30 -0500 | [diff] [blame] | 40 | long vfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | int error = -ENOTTY; |
| 43 | |
Al Viro | 72c2d53 | 2013-09-22 16:27:52 -0400 | [diff] [blame] | 44 | if (!filp->f_op->unlocked_ioctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | goto out; |
| 46 | |
Arnd Bergmann | b19dd42 | 2010-07-04 00:15:10 +0200 | [diff] [blame] | 47 | error = filp->f_op->unlocked_ioctl(filp, cmd, arg); |
| 48 | if (error == -ENOIOCTLCMD) |
Linus Torvalds | 07d106d | 2012-01-05 15:40:12 -0800 | [diff] [blame] | 49 | error = -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | out: |
| 51 | return error; |
| 52 | } |
Miklos Szeredi | 9df6702 | 2018-07-18 15:44:40 +0200 | [diff] [blame] | 53 | EXPORT_SYMBOL(vfs_ioctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 55 | static int ioctl_fibmap(struct file *filp, int __user *p) |
| 56 | { |
Carlos Maiolino | 0d89fda | 2020-01-09 14:30:44 +0100 | [diff] [blame] | 57 | struct inode *inode = file_inode(filp); |
| 58 | int error, ur_block; |
| 59 | sector_t block; |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 60 | |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 61 | if (!capable(CAP_SYS_RAWIO)) |
| 62 | return -EPERM; |
Carlos Maiolino | 0d89fda | 2020-01-09 14:30:44 +0100 | [diff] [blame] | 63 | |
| 64 | error = get_user(ur_block, p); |
| 65 | if (error) |
| 66 | return error; |
| 67 | |
Carlos Maiolino | 324282c | 2020-01-09 14:30:45 +0100 | [diff] [blame] | 68 | if (ur_block < 0) |
| 69 | return -EINVAL; |
| 70 | |
Carlos Maiolino | 0d89fda | 2020-01-09 14:30:44 +0100 | [diff] [blame] | 71 | block = ur_block; |
| 72 | error = bmap(inode, &block); |
| 73 | |
| 74 | if (error) |
| 75 | ur_block = 0; |
| 76 | else |
| 77 | ur_block = block; |
| 78 | |
| 79 | if (put_user(ur_block, p)) |
| 80 | error = -EFAULT; |
| 81 | |
| 82 | return error; |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 85 | /** |
| 86 | * fiemap_fill_next_extent - Fiemap helper function |
| 87 | * @fieinfo: Fiemap context passed into ->fiemap |
| 88 | * @logical: Extent logical start offset, in bytes |
| 89 | * @phys: Extent physical start offset, in bytes |
| 90 | * @len: Extent length, in bytes |
| 91 | * @flags: FIEMAP_EXTENT flags that describe this extent |
| 92 | * |
| 93 | * Called from file system ->fiemap callback. Will populate extent |
| 94 | * info as passed in via arguments and copy to user memory. On |
| 95 | * success, extent count on fieinfo is incremented. |
| 96 | * |
| 97 | * Returns 0 on success, -errno on error, 1 if this was the last |
| 98 | * extent that will fit in user array. |
| 99 | */ |
| 100 | #define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC) |
| 101 | #define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED) |
| 102 | #define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE) |
| 103 | int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical, |
| 104 | u64 phys, u64 len, u32 flags) |
| 105 | { |
| 106 | struct fiemap_extent extent; |
Namhyung Kim | ecf5632 | 2011-01-16 23:28:17 +0900 | [diff] [blame] | 107 | struct fiemap_extent __user *dest = fieinfo->fi_extents_start; |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 108 | |
| 109 | /* only count the extents */ |
| 110 | if (fieinfo->fi_extents_max == 0) { |
| 111 | fieinfo->fi_extents_mapped++; |
| 112 | return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0; |
| 113 | } |
| 114 | |
| 115 | if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max) |
| 116 | return 1; |
| 117 | |
| 118 | if (flags & SET_UNKNOWN_FLAGS) |
| 119 | flags |= FIEMAP_EXTENT_UNKNOWN; |
| 120 | if (flags & SET_NO_UNMOUNTED_IO_FLAGS) |
| 121 | flags |= FIEMAP_EXTENT_ENCODED; |
| 122 | if (flags & SET_NOT_ALIGNED_FLAGS) |
| 123 | flags |= FIEMAP_EXTENT_NOT_ALIGNED; |
| 124 | |
| 125 | memset(&extent, 0, sizeof(extent)); |
| 126 | extent.fe_logical = logical; |
| 127 | extent.fe_physical = phys; |
| 128 | extent.fe_length = len; |
| 129 | extent.fe_flags = flags; |
| 130 | |
| 131 | dest += fieinfo->fi_extents_mapped; |
| 132 | if (copy_to_user(dest, &extent, sizeof(extent))) |
| 133 | return -EFAULT; |
| 134 | |
| 135 | fieinfo->fi_extents_mapped++; |
| 136 | if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max) |
| 137 | return 1; |
| 138 | return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0; |
| 139 | } |
| 140 | EXPORT_SYMBOL(fiemap_fill_next_extent); |
| 141 | |
| 142 | /** |
| 143 | * fiemap_check_flags - check validity of requested flags for fiemap |
| 144 | * @fieinfo: Fiemap context passed into ->fiemap |
| 145 | * @fs_flags: Set of fiemap flags that the file system understands |
| 146 | * |
| 147 | * Called from file system ->fiemap callback. This will compute the |
| 148 | * intersection of valid fiemap flags and those that the fs supports. That |
| 149 | * value is then compared against the user supplied flags. In case of bad user |
| 150 | * flags, the invalid values will be written into the fieinfo structure, and |
| 151 | * -EBADR is returned, which tells ioctl_fiemap() to return those values to |
| 152 | * userspace. For this reason, a return code of -EBADR should be preserved. |
| 153 | * |
| 154 | * Returns 0 on success, -EBADR on bad flags. |
| 155 | */ |
| 156 | int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags) |
| 157 | { |
| 158 | u32 incompat_flags; |
| 159 | |
| 160 | incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags); |
| 161 | if (incompat_flags) { |
| 162 | fieinfo->fi_flags = incompat_flags; |
| 163 | return -EBADR; |
| 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | EXPORT_SYMBOL(fiemap_check_flags); |
| 168 | |
| 169 | static int fiemap_check_ranges(struct super_block *sb, |
| 170 | u64 start, u64 len, u64 *new_len) |
| 171 | { |
Jeff Layton | 5aa98b7 | 2009-09-18 13:05:50 -0700 | [diff] [blame] | 172 | u64 maxbytes = (u64) sb->s_maxbytes; |
| 173 | |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 174 | *new_len = len; |
| 175 | |
| 176 | if (len == 0) |
| 177 | return -EINVAL; |
| 178 | |
Jeff Layton | 5aa98b7 | 2009-09-18 13:05:50 -0700 | [diff] [blame] | 179 | if (start > maxbytes) |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 180 | return -EFBIG; |
| 181 | |
| 182 | /* |
| 183 | * Shrink request scope to what the fs can actually handle. |
| 184 | */ |
Jeff Layton | 5aa98b7 | 2009-09-18 13:05:50 -0700 | [diff] [blame] | 185 | if (len > maxbytes || (maxbytes - len) < start) |
| 186 | *new_len = maxbytes - start; |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Al Viro | 34d3d0e | 2019-04-21 18:45:28 -0400 | [diff] [blame] | 191 | static int ioctl_fiemap(struct file *filp, struct fiemap __user *ufiemap) |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 192 | { |
| 193 | struct fiemap fiemap; |
| 194 | struct fiemap_extent_info fieinfo = { 0, }; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 195 | struct inode *inode = file_inode(filp); |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 196 | struct super_block *sb = inode->i_sb; |
| 197 | u64 len; |
| 198 | int error; |
| 199 | |
| 200 | if (!inode->i_op->fiemap) |
| 201 | return -EOPNOTSUPP; |
| 202 | |
Namhyung Kim | ecf5632 | 2011-01-16 23:28:17 +0900 | [diff] [blame] | 203 | if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap))) |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 204 | return -EFAULT; |
| 205 | |
| 206 | if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS) |
| 207 | return -EINVAL; |
| 208 | |
| 209 | error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length, |
| 210 | &len); |
| 211 | if (error) |
| 212 | return error; |
| 213 | |
| 214 | fieinfo.fi_flags = fiemap.fm_flags; |
| 215 | fieinfo.fi_extents_max = fiemap.fm_extent_count; |
Namhyung Kim | ecf5632 | 2011-01-16 23:28:17 +0900 | [diff] [blame] | 216 | fieinfo.fi_extents_start = ufiemap->fm_extents; |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 217 | |
| 218 | if (fiemap.fm_extent_count != 0 && |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 219 | !access_ok(fieinfo.fi_extents_start, |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 220 | fieinfo.fi_extents_max * sizeof(struct fiemap_extent))) |
| 221 | return -EFAULT; |
| 222 | |
| 223 | if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC) |
| 224 | filemap_write_and_wait(inode->i_mapping); |
| 225 | |
| 226 | error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start, len); |
| 227 | fiemap.fm_flags = fieinfo.fi_flags; |
| 228 | fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped; |
Namhyung Kim | ecf5632 | 2011-01-16 23:28:17 +0900 | [diff] [blame] | 229 | if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap))) |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 230 | error = -EFAULT; |
| 231 | |
| 232 | return error; |
| 233 | } |
| 234 | |
Christoph Hellwig | 04b38d6 | 2015-12-03 12:59:50 +0100 | [diff] [blame] | 235 | static long ioctl_file_clone(struct file *dst_file, unsigned long srcfd, |
| 236 | u64 off, u64 olen, u64 destoff) |
| 237 | { |
| 238 | struct fd src_file = fdget(srcfd); |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 239 | loff_t cloned; |
Christoph Hellwig | 04b38d6 | 2015-12-03 12:59:50 +0100 | [diff] [blame] | 240 | int ret; |
| 241 | |
| 242 | if (!src_file.file) |
| 243 | return -EBADF; |
Amir Goldstein | 913b86e | 2016-09-23 11:38:10 +0300 | [diff] [blame] | 244 | ret = -EXDEV; |
| 245 | if (src_file.file->f_path.mnt != dst_file->f_path.mnt) |
| 246 | goto fdput; |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 247 | cloned = vfs_clone_file_range(src_file.file, off, dst_file, destoff, |
Darrick J. Wong | 452ce65 | 2018-10-30 10:41:56 +1100 | [diff] [blame] | 248 | olen, 0); |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 249 | if (cloned < 0) |
| 250 | ret = cloned; |
| 251 | else if (olen && cloned != olen) |
| 252 | ret = -EINVAL; |
| 253 | else |
| 254 | ret = 0; |
Amir Goldstein | 913b86e | 2016-09-23 11:38:10 +0300 | [diff] [blame] | 255 | fdput: |
Christoph Hellwig | 04b38d6 | 2015-12-03 12:59:50 +0100 | [diff] [blame] | 256 | fdput(src_file); |
| 257 | return ret; |
| 258 | } |
| 259 | |
Al Viro | 34d3d0e | 2019-04-21 18:45:28 -0400 | [diff] [blame] | 260 | static long ioctl_file_clone_range(struct file *file, |
| 261 | struct file_clone_range __user *argp) |
Christoph Hellwig | 04b38d6 | 2015-12-03 12:59:50 +0100 | [diff] [blame] | 262 | { |
| 263 | struct file_clone_range args; |
| 264 | |
| 265 | if (copy_from_user(&args, argp, sizeof(args))) |
| 266 | return -EFAULT; |
| 267 | return ioctl_file_clone(file, args.src_fd, args.src_offset, |
| 268 | args.src_length, args.dest_offset); |
| 269 | } |
| 270 | |
Adrian Bunk | 06270d5 | 2008-10-12 07:15:19 +0300 | [diff] [blame] | 271 | #ifdef CONFIG_BLOCK |
| 272 | |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 273 | static inline sector_t logical_to_blk(struct inode *inode, loff_t offset) |
| 274 | { |
| 275 | return (offset >> inode->i_blkbits); |
| 276 | } |
| 277 | |
| 278 | static inline loff_t blk_to_logical(struct inode *inode, sector_t blk) |
| 279 | { |
| 280 | return (blk << inode->i_blkbits); |
| 281 | } |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 282 | |
Steven Whitehouse | e9079cc | 2008-10-14 14:43:29 +0100 | [diff] [blame] | 283 | /** |
| 284 | * __generic_block_fiemap - FIEMAP for block based inodes (no locking) |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 285 | * @inode: the inode to map |
| 286 | * @fieinfo: the fiemap info struct that will be passed back to userspace |
| 287 | * @start: where to start mapping in the inode |
| 288 | * @len: how much space to map |
| 289 | * @get_block: the fs's get_block function |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 290 | * |
| 291 | * This does FIEMAP for block based inodes. Basically it will just loop |
| 292 | * through get_block until we hit the number of extents we want to map, or we |
| 293 | * go past the end of the file and hit a hole. |
| 294 | * |
| 295 | * If it is possible to have data blocks beyond a hole past @inode->i_size, then |
| 296 | * please do not use this function, it will stop at the first unmapped block |
Steven Whitehouse | e9079cc | 2008-10-14 14:43:29 +0100 | [diff] [blame] | 297 | * beyond i_size. |
| 298 | * |
| 299 | * If you use this function directly, you need to do your own locking. Use |
| 300 | * generic_block_fiemap if you want the locking done for you. |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 301 | */ |
Steven Whitehouse | e9079cc | 2008-10-14 14:43:29 +0100 | [diff] [blame] | 302 | |
| 303 | int __generic_block_fiemap(struct inode *inode, |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 304 | struct fiemap_extent_info *fieinfo, loff_t start, |
| 305 | loff_t len, get_block_t *get_block) |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 306 | { |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 307 | struct buffer_head map_bh; |
| 308 | sector_t start_blk, last_blk; |
| 309 | loff_t isize = i_size_read(inode); |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 310 | u64 logical = 0, phys = 0, size = 0; |
| 311 | u32 flags = FIEMAP_EXTENT_MERGED; |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 312 | bool past_eof = false, whole_file = false; |
| 313 | int ret = 0; |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 314 | |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 315 | ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); |
| 316 | if (ret) |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 317 | return ret; |
| 318 | |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 319 | /* |
| 320 | * Either the i_mutex or other appropriate locking needs to be held |
| 321 | * since we expect isize to not change at all through the duration of |
| 322 | * this call. |
| 323 | */ |
| 324 | if (len >= isize) { |
| 325 | whole_file = true; |
| 326 | len = isize; |
| 327 | } |
| 328 | |
Josef Bacik | d54cdc8 | 2011-02-01 15:52:47 -0800 | [diff] [blame] | 329 | /* |
| 330 | * Some filesystems can't deal with being asked to map less than |
| 331 | * blocksize, so make sure our len is at least block length. |
| 332 | */ |
| 333 | if (logical_to_blk(inode, len) == 0) |
| 334 | len = blk_to_logical(inode, 1); |
| 335 | |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 336 | start_blk = logical_to_blk(inode, start); |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 337 | last_blk = logical_to_blk(inode, start + len - 1); |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 338 | |
| 339 | do { |
| 340 | /* |
| 341 | * we set b_size to the total size we want so it will map as |
| 342 | * many contiguous blocks as possible at once |
| 343 | */ |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 344 | memset(&map_bh, 0, sizeof(struct buffer_head)); |
| 345 | map_bh.b_size = len; |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 346 | |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 347 | ret = get_block(inode, start_blk, &map_bh, 0); |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 348 | if (ret) |
| 349 | break; |
| 350 | |
| 351 | /* HOLE */ |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 352 | if (!buffer_mapped(&map_bh)) { |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 353 | start_blk++; |
| 354 | |
| 355 | /* |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 356 | * We want to handle the case where there is an |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 357 | * allocated block at the front of the file, and then |
| 358 | * nothing but holes up to the end of the file properly, |
| 359 | * to make sure that extent at the front gets properly |
| 360 | * marked with FIEMAP_EXTENT_LAST |
| 361 | */ |
| 362 | if (!past_eof && |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 363 | blk_to_logical(inode, start_blk) >= isize) |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 364 | past_eof = 1; |
| 365 | |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 366 | /* |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 367 | * First hole after going past the EOF, this is our |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 368 | * last extent |
| 369 | */ |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 370 | if (past_eof && size) { |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 371 | flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST; |
| 372 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 373 | phys, size, |
| 374 | flags); |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 375 | } else if (size) { |
| 376 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 377 | phys, size, flags); |
| 378 | size = 0; |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 379 | } |
| 380 | |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 381 | /* if we have holes up to/past EOF then we're done */ |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 382 | if (start_blk > last_blk || past_eof || ret) |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 383 | break; |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 384 | } else { |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 385 | /* |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 386 | * We have gone over the length of what we wanted to |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 387 | * map, and it wasn't the entire file, so add the extent |
| 388 | * we got last time and exit. |
| 389 | * |
| 390 | * This is for the case where say we want to map all the |
| 391 | * way up to the second to the last block in a file, but |
| 392 | * the last block is a hole, making the second to last |
| 393 | * block FIEMAP_EXTENT_LAST. In this case we want to |
| 394 | * see if there is a hole after the second to last block |
| 395 | * so we can mark it properly. If we found data after |
| 396 | * we exceeded the length we were requesting, then we |
| 397 | * are good to go, just add the extent to the fieinfo |
| 398 | * and break |
| 399 | */ |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 400 | if (start_blk > last_blk && !whole_file) { |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 401 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 402 | phys, size, |
| 403 | flags); |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * if size != 0 then we know we already have an extent |
| 409 | * to add, so add it. |
| 410 | */ |
| 411 | if (size) { |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 412 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 413 | phys, size, |
| 414 | flags); |
| 415 | if (ret) |
| 416 | break; |
| 417 | } |
| 418 | |
| 419 | logical = blk_to_logical(inode, start_blk); |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 420 | phys = blk_to_logical(inode, map_bh.b_blocknr); |
| 421 | size = map_bh.b_size; |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 422 | flags = FIEMAP_EXTENT_MERGED; |
| 423 | |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 424 | start_blk += logical_to_blk(inode, size); |
| 425 | |
| 426 | /* |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 427 | * If we are past the EOF, then we need to make sure as |
| 428 | * soon as we find a hole that the last extent we found |
| 429 | * is marked with FIEMAP_EXTENT_LAST |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 430 | */ |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 431 | if (!past_eof && logical + size >= isize) |
| 432 | past_eof = true; |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 433 | } |
| 434 | cond_resched(); |
Dmitry Monakhov | 913e027 | 2015-02-10 14:09:29 -0800 | [diff] [blame] | 435 | if (fatal_signal_pending(current)) { |
| 436 | ret = -EINTR; |
| 437 | break; |
| 438 | } |
| 439 | |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 440 | } while (1); |
| 441 | |
Josef Bacik | 3a3076f | 2010-04-23 12:17:17 -0400 | [diff] [blame] | 442 | /* If ret is 1 then we just hit the end of the extent array */ |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 443 | if (ret == 1) |
| 444 | ret = 0; |
| 445 | |
| 446 | return ret; |
| 447 | } |
Steven Whitehouse | e9079cc | 2008-10-14 14:43:29 +0100 | [diff] [blame] | 448 | EXPORT_SYMBOL(__generic_block_fiemap); |
| 449 | |
| 450 | /** |
| 451 | * generic_block_fiemap - FIEMAP for block based inodes |
| 452 | * @inode: The inode to map |
| 453 | * @fieinfo: The mapping information |
| 454 | * @start: The initial block to map |
| 455 | * @len: The length of the extect to attempt to map |
| 456 | * @get_block: The block mapping function for the fs |
| 457 | * |
| 458 | * Calls __generic_block_fiemap to map the inode, after taking |
| 459 | * the inode's mutex lock. |
| 460 | */ |
| 461 | |
| 462 | int generic_block_fiemap(struct inode *inode, |
| 463 | struct fiemap_extent_info *fieinfo, u64 start, |
| 464 | u64 len, get_block_t *get_block) |
| 465 | { |
| 466 | int ret; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 467 | inode_lock(inode); |
Steven Whitehouse | e9079cc | 2008-10-14 14:43:29 +0100 | [diff] [blame] | 468 | ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 469 | inode_unlock(inode); |
Steven Whitehouse | e9079cc | 2008-10-14 14:43:29 +0100 | [diff] [blame] | 470 | return ret; |
| 471 | } |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 472 | EXPORT_SYMBOL(generic_block_fiemap); |
| 473 | |
Adrian Bunk | 06270d5 | 2008-10-12 07:15:19 +0300 | [diff] [blame] | 474 | #endif /* CONFIG_BLOCK */ |
| 475 | |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 476 | /* |
| 477 | * This provides compatibility with legacy XFS pre-allocation ioctls |
| 478 | * which predate the fallocate syscall. |
| 479 | * |
| 480 | * Only the l_start, l_len and l_whence fields of the 'struct space_resv' |
| 481 | * are used here, rest are ignored. |
| 482 | */ |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 483 | static int ioctl_preallocate(struct file *filp, int mode, void __user *argp) |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 484 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 485 | struct inode *inode = file_inode(filp); |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 486 | struct space_resv sr; |
| 487 | |
| 488 | if (copy_from_user(&sr, argp, sizeof(sr))) |
| 489 | return -EFAULT; |
| 490 | |
| 491 | switch (sr.l_whence) { |
| 492 | case SEEK_SET: |
| 493 | break; |
| 494 | case SEEK_CUR: |
| 495 | sr.l_start += filp->f_pos; |
| 496 | break; |
| 497 | case SEEK_END: |
| 498 | sr.l_start += i_size_read(inode); |
| 499 | break; |
| 500 | default: |
| 501 | return -EINVAL; |
| 502 | } |
| 503 | |
Christoph Hellwig | 837a6e7 | 2019-10-24 22:26:02 -0700 | [diff] [blame] | 504 | return vfs_fallocate(filp, mode | FALLOC_FL_KEEP_SIZE, sr.l_start, |
| 505 | sr.l_len); |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 506 | } |
| 507 | |
Al Viro | 011da44 | 2019-04-21 19:24:03 -0400 | [diff] [blame] | 508 | /* on ia32 l_start is on a 32-bit boundary */ |
| 509 | #if defined CONFIG_COMPAT && defined(CONFIG_X86_64) |
| 510 | /* just account for different alignment */ |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 511 | static int compat_ioctl_preallocate(struct file *file, int mode, |
| 512 | struct space_resv_32 __user *argp) |
Al Viro | 011da44 | 2019-04-21 19:24:03 -0400 | [diff] [blame] | 513 | { |
| 514 | struct inode *inode = file_inode(file); |
| 515 | struct space_resv_32 sr; |
| 516 | |
| 517 | if (copy_from_user(&sr, argp, sizeof(sr))) |
| 518 | return -EFAULT; |
| 519 | |
| 520 | switch (sr.l_whence) { |
| 521 | case SEEK_SET: |
| 522 | break; |
| 523 | case SEEK_CUR: |
| 524 | sr.l_start += file->f_pos; |
| 525 | break; |
| 526 | case SEEK_END: |
| 527 | sr.l_start += i_size_read(inode); |
| 528 | break; |
| 529 | default: |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | |
Linus Torvalds | 97eeb4d | 2019-12-02 14:46:22 -0800 | [diff] [blame] | 533 | return vfs_fallocate(file, mode | FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len); |
Al Viro | 011da44 | 2019-04-21 19:24:03 -0400 | [diff] [blame] | 534 | } |
| 535 | #endif |
| 536 | |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 537 | static int file_ioctl(struct file *filp, unsigned int cmd, int __user *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | switch (cmd) { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 540 | case FIBMAP: |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 541 | return ioctl_fibmap(filp, p); |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 542 | case FS_IOC_RESVSP: |
| 543 | case FS_IOC_RESVSP64: |
Christoph Hellwig | 837a6e7 | 2019-10-24 22:26:02 -0700 | [diff] [blame] | 544 | return ioctl_preallocate(filp, 0, p); |
| 545 | case FS_IOC_UNRESVSP: |
| 546 | case FS_IOC_UNRESVSP64: |
| 547 | return ioctl_preallocate(filp, FALLOC_FL_PUNCH_HOLE, p); |
| 548 | case FS_IOC_ZERO_RANGE: |
| 549 | return ioctl_preallocate(filp, FALLOC_FL_ZERO_RANGE, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 552 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 555 | static int ioctl_fionbio(struct file *filp, int __user *argp) |
| 556 | { |
| 557 | unsigned int flag; |
| 558 | int on, error; |
| 559 | |
| 560 | error = get_user(on, argp); |
| 561 | if (error) |
| 562 | return error; |
| 563 | flag = O_NONBLOCK; |
| 564 | #ifdef __sparc__ |
| 565 | /* SunOS compatibility item. */ |
| 566 | if (O_NONBLOCK != O_NDELAY) |
| 567 | flag |= O_NDELAY; |
| 568 | #endif |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 569 | spin_lock(&filp->f_lock); |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 570 | if (on) |
| 571 | filp->f_flags |= flag; |
| 572 | else |
| 573 | filp->f_flags &= ~flag; |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 574 | spin_unlock(&filp->f_lock); |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 575 | return error; |
| 576 | } |
| 577 | |
| 578 | static int ioctl_fioasync(unsigned int fd, struct file *filp, |
| 579 | int __user *argp) |
| 580 | { |
| 581 | unsigned int flag; |
| 582 | int on, error; |
| 583 | |
| 584 | error = get_user(on, argp); |
| 585 | if (error) |
| 586 | return error; |
| 587 | flag = on ? FASYNC : 0; |
| 588 | |
| 589 | /* Did FASYNC state change ? */ |
| 590 | if ((flag ^ filp->f_flags) & FASYNC) { |
Al Viro | 72c2d53 | 2013-09-22 16:27:52 -0400 | [diff] [blame] | 591 | if (filp->f_op->fasync) |
Jonathan Corbet | 7639842 | 2009-02-01 14:26:59 -0700 | [diff] [blame] | 592 | /* fasync() adjusts filp->f_flags */ |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 593 | error = filp->f_op->fasync(fd, filp, on); |
Jonathan Corbet | 218d11a | 2008-12-05 16:12:48 -0700 | [diff] [blame] | 594 | else |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 595 | error = -ENOTTY; |
| 596 | } |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 597 | return error < 0 ? error : 0; |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 600 | static int ioctl_fsfreeze(struct file *filp) |
| 601 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 602 | struct super_block *sb = file_inode(filp)->i_sb; |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 603 | |
Seth Forshee | f3f1a18 | 2015-02-15 14:35:35 -0600 | [diff] [blame] | 604 | if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 605 | return -EPERM; |
| 606 | |
| 607 | /* If filesystem doesn't support freeze feature, return. */ |
Benjamin Marzinski | 48b6bca | 2014-11-13 20:42:03 -0600 | [diff] [blame] | 608 | if (sb->s_op->freeze_fs == NULL && sb->s_op->freeze_super == NULL) |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 609 | return -EOPNOTSUPP; |
| 610 | |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 611 | /* Freeze */ |
Benjamin Marzinski | 48b6bca | 2014-11-13 20:42:03 -0600 | [diff] [blame] | 612 | if (sb->s_op->freeze_super) |
| 613 | return sb->s_op->freeze_super(sb); |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 614 | return freeze_super(sb); |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static int ioctl_fsthaw(struct file *filp) |
| 618 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 619 | struct super_block *sb = file_inode(filp)->i_sb; |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 620 | |
Seth Forshee | f3f1a18 | 2015-02-15 14:35:35 -0600 | [diff] [blame] | 621 | if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 622 | return -EPERM; |
| 623 | |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 624 | /* Thaw */ |
Benjamin Marzinski | 48b6bca | 2014-11-13 20:42:03 -0600 | [diff] [blame] | 625 | if (sb->s_op->thaw_super) |
| 626 | return sb->s_op->thaw_super(sb); |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 627 | return thaw_super(sb); |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 628 | } |
| 629 | |
Al Viro | 34d3d0e | 2019-04-21 18:45:28 -0400 | [diff] [blame] | 630 | static int ioctl_file_dedupe_range(struct file *file, |
| 631 | struct file_dedupe_range __user *argp) |
Darrick J. Wong | 54dbc15 | 2015-12-19 00:55:59 -0800 | [diff] [blame] | 632 | { |
Darrick J. Wong | 54dbc15 | 2015-12-19 00:55:59 -0800 | [diff] [blame] | 633 | struct file_dedupe_range *same = NULL; |
| 634 | int ret; |
| 635 | unsigned long size; |
| 636 | u16 count; |
| 637 | |
| 638 | if (get_user(count, &argp->dest_count)) { |
| 639 | ret = -EFAULT; |
| 640 | goto out; |
| 641 | } |
| 642 | |
| 643 | size = offsetof(struct file_dedupe_range __user, info[count]); |
Darrick J. Wong | b71dbf1 | 2016-09-14 20:20:44 -0700 | [diff] [blame] | 644 | if (size > PAGE_SIZE) { |
| 645 | ret = -ENOMEM; |
| 646 | goto out; |
| 647 | } |
Darrick J. Wong | 54dbc15 | 2015-12-19 00:55:59 -0800 | [diff] [blame] | 648 | |
| 649 | same = memdup_user(argp, size); |
| 650 | if (IS_ERR(same)) { |
| 651 | ret = PTR_ERR(same); |
| 652 | same = NULL; |
| 653 | goto out; |
| 654 | } |
| 655 | |
Scott Bauer | 10eec60 | 2016-07-27 19:11:29 -0600 | [diff] [blame] | 656 | same->dest_count = count; |
Darrick J. Wong | 54dbc15 | 2015-12-19 00:55:59 -0800 | [diff] [blame] | 657 | ret = vfs_dedupe_file_range(file, same); |
| 658 | if (ret) |
| 659 | goto out; |
| 660 | |
| 661 | ret = copy_to_user(argp, same, size); |
| 662 | if (ret) |
| 663 | ret = -EFAULT; |
| 664 | |
| 665 | out: |
| 666 | kfree(same); |
| 667 | return ret; |
| 668 | } |
| 669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | /* |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 671 | * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | * It's just a simple helper for sys_ioctl and compat_sys_ioctl. |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 673 | * |
| 674 | * When you add any new common ioctls to the switches above and below, |
| 675 | * please ensure they have compatible arguments in compat mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | */ |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 677 | static int do_vfs_ioctl(struct file *filp, unsigned int fd, |
| 678 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | { |
Al Viro | 34d3d0e | 2019-04-21 18:45:28 -0400 | [diff] [blame] | 680 | void __user *argp = (void __user *)arg; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 681 | struct inode *inode = file_inode(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
| 683 | switch (cmd) { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 684 | case FIOCLEX: |
| 685 | set_close_on_exec(fd, 1); |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 686 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 688 | case FIONCLEX: |
| 689 | set_close_on_exec(fd, 0); |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 690 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 692 | case FIONBIO: |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 693 | return ioctl_fionbio(filp, argp); |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 694 | |
| 695 | case FIOASYNC: |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 696 | return ioctl_fioasync(fd, filp, argp); |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 697 | |
| 698 | case FIOQSIZE: |
Namhyung Kim | 27a4f7e6 | 2011-01-17 00:48:17 +0900 | [diff] [blame] | 699 | if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode) || |
| 700 | S_ISLNK(inode->i_mode)) { |
| 701 | loff_t res = inode_get_bytes(inode); |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 702 | return copy_to_user(argp, &res, sizeof(res)) ? |
| 703 | -EFAULT : 0; |
| 704 | } |
| 705 | |
| 706 | return -ENOTTY; |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 707 | |
| 708 | case FIFREEZE: |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 709 | return ioctl_fsfreeze(filp); |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 710 | |
| 711 | case FITHAW: |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 712 | return ioctl_fsthaw(filp); |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 713 | |
Aneesh Kumar K.V | 19ba055 | 2009-05-13 18:12:05 -0400 | [diff] [blame] | 714 | case FS_IOC_FIEMAP: |
Al Viro | 34d3d0e | 2019-04-21 18:45:28 -0400 | [diff] [blame] | 715 | return ioctl_fiemap(filp, argp); |
Aneesh Kumar K.V | 19ba055 | 2009-05-13 18:12:05 -0400 | [diff] [blame] | 716 | |
| 717 | case FIGETBSZ: |
Amir Goldstein | 8f97d1e | 2018-10-11 17:38:14 +0300 | [diff] [blame] | 718 | /* anon_bdev filesystems may not have a block size */ |
| 719 | if (!inode->i_sb->s_blocksize) |
| 720 | return -EINVAL; |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 721 | |
Al Viro | 34d3d0e | 2019-04-21 18:45:28 -0400 | [diff] [blame] | 722 | return put_user(inode->i_sb->s_blocksize, (int __user *)argp); |
Aneesh Kumar K.V | 19ba055 | 2009-05-13 18:12:05 -0400 | [diff] [blame] | 723 | |
Christoph Hellwig | 04b38d6 | 2015-12-03 12:59:50 +0100 | [diff] [blame] | 724 | case FICLONE: |
| 725 | return ioctl_file_clone(filp, arg, 0, 0, 0); |
| 726 | |
| 727 | case FICLONERANGE: |
| 728 | return ioctl_file_clone_range(filp, argp); |
| 729 | |
Darrick J. Wong | 54dbc15 | 2015-12-19 00:55:59 -0800 | [diff] [blame] | 730 | case FIDEDUPERANGE: |
| 731 | return ioctl_file_dedupe_range(filp, argp); |
| 732 | |
Arnd Bergmann | 0a06174 | 2020-02-07 17:55:48 +0100 | [diff] [blame] | 733 | case FIONREAD: |
| 734 | if (!S_ISREG(inode->i_mode)) |
| 735 | return vfs_ioctl(filp, cmd, arg); |
| 736 | |
| 737 | return put_user(i_size_read(inode) - filp->f_pos, |
| 738 | (int __user *)argp); |
| 739 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 740 | default: |
Namhyung Kim | 27a4f7e6 | 2011-01-17 00:48:17 +0900 | [diff] [blame] | 741 | if (S_ISREG(inode->i_mode)) |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 742 | return file_ioctl(filp, cmd, argp); |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 743 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 745 | |
| 746 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 749 | int ksys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 751 | struct fd f = fdget(fd); |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 752 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 754 | if (!f.file) |
| 755 | return -EBADF; |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 756 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 757 | error = security_file_ioctl(f.file, cmd, arg); |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 758 | if (error) |
| 759 | goto out; |
| 760 | |
| 761 | error = do_vfs_ioctl(f.file, fd, cmd, arg); |
| 762 | if (error == -ENOIOCTLCMD) |
| 763 | error = vfs_ioctl(f.file, cmd, arg); |
| 764 | |
| 765 | out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 766 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | return error; |
| 768 | } |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 769 | |
| 770 | SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg) |
| 771 | { |
| 772 | return ksys_ioctl(fd, cmd, arg); |
| 773 | } |
Arnd Bergmann | 2952db0 | 2018-09-11 16:55:03 +0200 | [diff] [blame] | 774 | |
| 775 | #ifdef CONFIG_COMPAT |
| 776 | /** |
| 777 | * compat_ptr_ioctl - generic implementation of .compat_ioctl file operation |
| 778 | * |
| 779 | * This is not normally called as a function, but instead set in struct |
| 780 | * file_operations as |
| 781 | * |
| 782 | * .compat_ioctl = compat_ptr_ioctl, |
| 783 | * |
| 784 | * On most architectures, the compat_ptr_ioctl() just passes all arguments |
| 785 | * to the corresponding ->ioctl handler. The exception is arch/s390, where |
| 786 | * compat_ptr() clears the top bit of a 32-bit pointer value, so user space |
| 787 | * pointers to the second 2GB alias the first 2GB, as is the case for |
| 788 | * native 32-bit s390 user space. |
| 789 | * |
| 790 | * The compat_ptr_ioctl() function must therefore be used only with ioctl |
| 791 | * functions that either ignore the argument or pass a pointer to a |
| 792 | * compatible data type. |
| 793 | * |
| 794 | * If any ioctl command handled by fops->unlocked_ioctl passes a plain |
| 795 | * integer instead of a pointer, or any of the passed data types |
| 796 | * is incompatible between 32-bit and 64-bit architectures, a proper |
| 797 | * handler is required instead of compat_ptr_ioctl. |
| 798 | */ |
| 799 | long compat_ptr_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 800 | { |
| 801 | if (!file->f_op->unlocked_ioctl) |
| 802 | return -ENOIOCTLCMD; |
| 803 | |
| 804 | return file->f_op->unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
| 805 | } |
| 806 | EXPORT_SYMBOL(compat_ptr_ioctl); |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 807 | |
| 808 | COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 809 | compat_ulong_t, arg) |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 810 | { |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 811 | struct fd f = fdget(fd); |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 812 | int error; |
| 813 | |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 814 | if (!f.file) |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 815 | return -EBADF; |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 816 | |
| 817 | /* RED-PEN how should LSM module know it's handling 32bit? */ |
| 818 | error = security_file_ioctl(f.file, cmd, arg); |
| 819 | if (error) |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 820 | goto out; |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 821 | |
| 822 | switch (cmd) { |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 823 | /* FICLONE takes an int argument, so don't use compat_ptr() */ |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 824 | case FICLONE: |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 825 | error = ioctl_file_clone(f.file, arg, 0, 0, 0); |
| 826 | break; |
| 827 | |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 828 | #if defined(CONFIG_X86_64) |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 829 | /* these get messy on amd64 due to alignment differences */ |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 830 | case FS_IOC_RESVSP_32: |
| 831 | case FS_IOC_RESVSP64_32: |
| 832 | error = compat_ioctl_preallocate(f.file, 0, compat_ptr(arg)); |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 833 | break; |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 834 | case FS_IOC_UNRESVSP_32: |
| 835 | case FS_IOC_UNRESVSP64_32: |
| 836 | error = compat_ioctl_preallocate(f.file, FALLOC_FL_PUNCH_HOLE, |
| 837 | compat_ptr(arg)); |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 838 | break; |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 839 | case FS_IOC_ZERO_RANGE_32: |
| 840 | error = compat_ioctl_preallocate(f.file, FALLOC_FL_ZERO_RANGE, |
| 841 | compat_ptr(arg)); |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 842 | break; |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 843 | #endif |
| 844 | |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 845 | /* |
| 846 | * everything else in do_vfs_ioctl() takes either a compatible |
| 847 | * pointer argument or no argument -- call it with a modified |
| 848 | * argument. |
| 849 | */ |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 850 | default: |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 851 | error = do_vfs_ioctl(f.file, fd, cmd, |
| 852 | (unsigned long)compat_ptr(arg)); |
| 853 | if (error != -ENOIOCTLCMD) |
| 854 | break; |
| 855 | |
| 856 | if (f.file->f_op->compat_ioctl) |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 857 | error = f.file->f_op->compat_ioctl(f.file, cmd, arg); |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 858 | if (error == -ENOIOCTLCMD) |
| 859 | error = -ENOTTY; |
| 860 | break; |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 861 | } |
| 862 | |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 863 | out: |
Arnd Bergmann | 77b9040 | 2019-11-27 21:25:36 +0100 | [diff] [blame] | 864 | fdput(f); |
| 865 | |
Arnd Bergmann | 2af563d | 2019-06-07 10:06:42 +0200 | [diff] [blame] | 866 | return error; |
| 867 | } |
Arnd Bergmann | 2952db0 | 2018-09-11 16:55:03 +0200 | [diff] [blame] | 868 | #endif |