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