Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ioctl.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/syscalls.h> |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/smp_lock.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 10 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/file.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/security.h> |
| 14 | #include <linux/module.h> |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 15 | #include <linux/uaccess.h> |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 16 | #include <linux/writeback.h> |
| 17 | #include <linux/buffer_head.h> |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 18 | #include <linux/falloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/ioctls.h> |
| 21 | |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 22 | /* So that the fiemap access checks can't overflow on 32 bit machines. */ |
| 23 | #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent)) |
| 24 | |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 25 | /** |
| 26 | * vfs_ioctl - call filesystem specific ioctl methods |
Christoph Hellwig | f6a4c8b | 2008-02-09 00:10:16 -0800 | [diff] [blame] | 27 | * @filp: open file to invoke ioctl method on |
| 28 | * @cmd: ioctl command to execute |
| 29 | * @arg: command-specific argument for ioctl |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 30 | * |
| 31 | * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise |
Christoph Hellwig | f6a4c8b | 2008-02-09 00:10:16 -0800 | [diff] [blame] | 32 | * invokes filesystem specific ->ioctl method. If neither method exists, |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 33 | * returns -ENOTTY. |
| 34 | * |
| 35 | * Returns 0 on success, -errno on error. |
| 36 | */ |
Adrian Bunk | 67cde59 | 2008-04-29 00:58:55 -0700 | [diff] [blame] | 37 | static long vfs_ioctl(struct file *filp, unsigned int cmd, |
| 38 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
| 40 | int error = -ENOTTY; |
| 41 | |
| 42 | if (!filp->f_op) |
| 43 | goto out; |
| 44 | |
| 45 | if (filp->f_op->unlocked_ioctl) { |
| 46 | error = filp->f_op->unlocked_ioctl(filp, cmd, arg); |
| 47 | if (error == -ENOIOCTLCMD) |
| 48 | error = -EINVAL; |
| 49 | goto out; |
Andrew Morton | 64d67d2 | 2007-07-15 23:41:02 -0700 | [diff] [blame] | 50 | } else if (filp->f_op->ioctl) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | lock_kernel(); |
Andrew Morton | 64d67d2 | 2007-07-15 23:41:02 -0700 | [diff] [blame] | 52 | error = filp->f_op->ioctl(filp->f_path.dentry->d_inode, |
| 53 | filp, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | unlock_kernel(); |
| 55 | } |
| 56 | |
| 57 | out: |
| 58 | return error; |
| 59 | } |
| 60 | |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 61 | static int ioctl_fibmap(struct file *filp, int __user *p) |
| 62 | { |
| 63 | struct address_space *mapping = filp->f_mapping; |
| 64 | int res, block; |
| 65 | |
| 66 | /* do we support this mess? */ |
| 67 | if (!mapping->a_ops->bmap) |
| 68 | return -EINVAL; |
| 69 | if (!capable(CAP_SYS_RAWIO)) |
| 70 | return -EPERM; |
| 71 | res = get_user(block, p); |
| 72 | if (res) |
| 73 | return res; |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 74 | res = mapping->a_ops->bmap(mapping, block); |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 75 | return put_user(res, p); |
| 76 | } |
| 77 | |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 78 | /** |
| 79 | * fiemap_fill_next_extent - Fiemap helper function |
| 80 | * @fieinfo: Fiemap context passed into ->fiemap |
| 81 | * @logical: Extent logical start offset, in bytes |
| 82 | * @phys: Extent physical start offset, in bytes |
| 83 | * @len: Extent length, in bytes |
| 84 | * @flags: FIEMAP_EXTENT flags that describe this extent |
| 85 | * |
| 86 | * Called from file system ->fiemap callback. Will populate extent |
| 87 | * info as passed in via arguments and copy to user memory. On |
| 88 | * success, extent count on fieinfo is incremented. |
| 89 | * |
| 90 | * Returns 0 on success, -errno on error, 1 if this was the last |
| 91 | * extent that will fit in user array. |
| 92 | */ |
| 93 | #define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC) |
| 94 | #define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED) |
| 95 | #define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE) |
| 96 | int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical, |
| 97 | u64 phys, u64 len, u32 flags) |
| 98 | { |
| 99 | struct fiemap_extent extent; |
| 100 | struct fiemap_extent *dest = fieinfo->fi_extents_start; |
| 101 | |
| 102 | /* only count the extents */ |
| 103 | if (fieinfo->fi_extents_max == 0) { |
| 104 | fieinfo->fi_extents_mapped++; |
| 105 | return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0; |
| 106 | } |
| 107 | |
| 108 | if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max) |
| 109 | return 1; |
| 110 | |
| 111 | if (flags & SET_UNKNOWN_FLAGS) |
| 112 | flags |= FIEMAP_EXTENT_UNKNOWN; |
| 113 | if (flags & SET_NO_UNMOUNTED_IO_FLAGS) |
| 114 | flags |= FIEMAP_EXTENT_ENCODED; |
| 115 | if (flags & SET_NOT_ALIGNED_FLAGS) |
| 116 | flags |= FIEMAP_EXTENT_NOT_ALIGNED; |
| 117 | |
| 118 | memset(&extent, 0, sizeof(extent)); |
| 119 | extent.fe_logical = logical; |
| 120 | extent.fe_physical = phys; |
| 121 | extent.fe_length = len; |
| 122 | extent.fe_flags = flags; |
| 123 | |
| 124 | dest += fieinfo->fi_extents_mapped; |
| 125 | if (copy_to_user(dest, &extent, sizeof(extent))) |
| 126 | return -EFAULT; |
| 127 | |
| 128 | fieinfo->fi_extents_mapped++; |
| 129 | if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max) |
| 130 | return 1; |
| 131 | return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0; |
| 132 | } |
| 133 | EXPORT_SYMBOL(fiemap_fill_next_extent); |
| 134 | |
| 135 | /** |
| 136 | * fiemap_check_flags - check validity of requested flags for fiemap |
| 137 | * @fieinfo: Fiemap context passed into ->fiemap |
| 138 | * @fs_flags: Set of fiemap flags that the file system understands |
| 139 | * |
| 140 | * Called from file system ->fiemap callback. This will compute the |
| 141 | * intersection of valid fiemap flags and those that the fs supports. That |
| 142 | * value is then compared against the user supplied flags. In case of bad user |
| 143 | * flags, the invalid values will be written into the fieinfo structure, and |
| 144 | * -EBADR is returned, which tells ioctl_fiemap() to return those values to |
| 145 | * userspace. For this reason, a return code of -EBADR should be preserved. |
| 146 | * |
| 147 | * Returns 0 on success, -EBADR on bad flags. |
| 148 | */ |
| 149 | int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags) |
| 150 | { |
| 151 | u32 incompat_flags; |
| 152 | |
| 153 | incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags); |
| 154 | if (incompat_flags) { |
| 155 | fieinfo->fi_flags = incompat_flags; |
| 156 | return -EBADR; |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | EXPORT_SYMBOL(fiemap_check_flags); |
| 161 | |
| 162 | static int fiemap_check_ranges(struct super_block *sb, |
| 163 | u64 start, u64 len, u64 *new_len) |
| 164 | { |
Jeff Layton | 5aa98b7 | 2009-09-18 13:05:50 -0700 | [diff] [blame] | 165 | u64 maxbytes = (u64) sb->s_maxbytes; |
| 166 | |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 167 | *new_len = len; |
| 168 | |
| 169 | if (len == 0) |
| 170 | return -EINVAL; |
| 171 | |
Jeff Layton | 5aa98b7 | 2009-09-18 13:05:50 -0700 | [diff] [blame] | 172 | if (start > maxbytes) |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 173 | return -EFBIG; |
| 174 | |
| 175 | /* |
| 176 | * Shrink request scope to what the fs can actually handle. |
| 177 | */ |
Jeff Layton | 5aa98b7 | 2009-09-18 13:05:50 -0700 | [diff] [blame] | 178 | if (len > maxbytes || (maxbytes - len) < start) |
| 179 | *new_len = maxbytes - start; |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int ioctl_fiemap(struct file *filp, unsigned long arg) |
| 185 | { |
| 186 | struct fiemap fiemap; |
| 187 | struct fiemap_extent_info fieinfo = { 0, }; |
| 188 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 189 | struct super_block *sb = inode->i_sb; |
| 190 | u64 len; |
| 191 | int error; |
| 192 | |
| 193 | if (!inode->i_op->fiemap) |
| 194 | return -EOPNOTSUPP; |
| 195 | |
| 196 | if (copy_from_user(&fiemap, (struct fiemap __user *)arg, |
| 197 | sizeof(struct fiemap))) |
| 198 | return -EFAULT; |
| 199 | |
| 200 | if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length, |
| 204 | &len); |
| 205 | if (error) |
| 206 | return error; |
| 207 | |
| 208 | fieinfo.fi_flags = fiemap.fm_flags; |
| 209 | fieinfo.fi_extents_max = fiemap.fm_extent_count; |
| 210 | fieinfo.fi_extents_start = (struct fiemap_extent *)(arg + sizeof(fiemap)); |
| 211 | |
| 212 | if (fiemap.fm_extent_count != 0 && |
| 213 | !access_ok(VERIFY_WRITE, fieinfo.fi_extents_start, |
| 214 | fieinfo.fi_extents_max * sizeof(struct fiemap_extent))) |
| 215 | return -EFAULT; |
| 216 | |
| 217 | if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC) |
| 218 | filemap_write_and_wait(inode->i_mapping); |
| 219 | |
| 220 | error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start, len); |
| 221 | fiemap.fm_flags = fieinfo.fi_flags; |
| 222 | fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped; |
| 223 | if (copy_to_user((char *)arg, &fiemap, sizeof(fiemap))) |
| 224 | error = -EFAULT; |
| 225 | |
| 226 | return error; |
| 227 | } |
| 228 | |
Adrian Bunk | 06270d5 | 2008-10-12 07:15:19 +0300 | [diff] [blame] | 229 | #ifdef CONFIG_BLOCK |
| 230 | |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 231 | #define blk_to_logical(inode, blk) (blk << (inode)->i_blkbits) |
| 232 | #define logical_to_blk(inode, offset) (offset >> (inode)->i_blkbits); |
| 233 | |
Steven Whitehouse | e9079cc | 2008-10-14 14:43:29 +0100 | [diff] [blame] | 234 | /** |
| 235 | * __generic_block_fiemap - FIEMAP for block based inodes (no locking) |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 236 | * @inode - the inode to map |
| 237 | * @arg - the pointer to userspace where we copy everything to |
| 238 | * @get_block - the fs's get_block function |
| 239 | * |
| 240 | * This does FIEMAP for block based inodes. Basically it will just loop |
| 241 | * through get_block until we hit the number of extents we want to map, or we |
| 242 | * go past the end of the file and hit a hole. |
| 243 | * |
| 244 | * If it is possible to have data blocks beyond a hole past @inode->i_size, then |
| 245 | * 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] | 246 | * beyond i_size. |
| 247 | * |
| 248 | * If you use this function directly, you need to do your own locking. Use |
| 249 | * generic_block_fiemap if you want the locking done for you. |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 250 | */ |
Steven Whitehouse | e9079cc | 2008-10-14 14:43:29 +0100 | [diff] [blame] | 251 | |
| 252 | int __generic_block_fiemap(struct inode *inode, |
| 253 | struct fiemap_extent_info *fieinfo, u64 start, |
| 254 | u64 len, get_block_t *get_block) |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 255 | { |
| 256 | struct buffer_head tmp; |
Mike Hommey | e04b5ef | 2009-11-11 14:26:55 -0800 | [diff] [blame] | 257 | unsigned long long start_blk; |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 258 | long long length = 0, map_len = 0; |
| 259 | u64 logical = 0, phys = 0, size = 0; |
| 260 | u32 flags = FIEMAP_EXTENT_MERGED; |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 261 | int ret = 0, past_eof = 0, whole_file = 0; |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 262 | |
| 263 | if ((ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC))) |
| 264 | return ret; |
| 265 | |
| 266 | start_blk = logical_to_blk(inode, start); |
| 267 | |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 268 | length = (long long)min_t(u64, len, i_size_read(inode)); |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 269 | if (length < len) |
| 270 | whole_file = 1; |
| 271 | |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 272 | map_len = length; |
| 273 | |
| 274 | do { |
| 275 | /* |
| 276 | * we set b_size to the total size we want so it will map as |
| 277 | * many contiguous blocks as possible at once |
| 278 | */ |
| 279 | memset(&tmp, 0, sizeof(struct buffer_head)); |
| 280 | tmp.b_size = map_len; |
| 281 | |
| 282 | ret = get_block(inode, start_blk, &tmp, 0); |
| 283 | if (ret) |
| 284 | break; |
| 285 | |
| 286 | /* HOLE */ |
| 287 | if (!buffer_mapped(&tmp)) { |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 288 | length -= blk_to_logical(inode, 1); |
| 289 | start_blk++; |
| 290 | |
| 291 | /* |
| 292 | * we want to handle the case where there is an |
| 293 | * allocated block at the front of the file, and then |
| 294 | * nothing but holes up to the end of the file properly, |
| 295 | * to make sure that extent at the front gets properly |
| 296 | * marked with FIEMAP_EXTENT_LAST |
| 297 | */ |
| 298 | if (!past_eof && |
| 299 | blk_to_logical(inode, start_blk) >= |
| 300 | blk_to_logical(inode, 0)+i_size_read(inode)) |
| 301 | past_eof = 1; |
| 302 | |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 303 | /* |
| 304 | * first hole after going past the EOF, this is our |
| 305 | * last extent |
| 306 | */ |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 307 | if (past_eof && size) { |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 308 | flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST; |
| 309 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 310 | phys, size, |
| 311 | flags); |
| 312 | break; |
| 313 | } |
| 314 | |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 315 | /* if we have holes up to/past EOF then we're done */ |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 316 | if (length <= 0 || past_eof) |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 317 | break; |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 318 | } else { |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 319 | /* |
| 320 | * we have gone over the length of what we wanted to |
| 321 | * map, and it wasn't the entire file, so add the extent |
| 322 | * we got last time and exit. |
| 323 | * |
| 324 | * This is for the case where say we want to map all the |
| 325 | * way up to the second to the last block in a file, but |
| 326 | * the last block is a hole, making the second to last |
| 327 | * block FIEMAP_EXTENT_LAST. In this case we want to |
| 328 | * see if there is a hole after the second to last block |
| 329 | * so we can mark it properly. If we found data after |
| 330 | * we exceeded the length we were requesting, then we |
| 331 | * are good to go, just add the extent to the fieinfo |
| 332 | * and break |
| 333 | */ |
| 334 | if (length <= 0 && !whole_file) { |
| 335 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 336 | phys, size, |
| 337 | flags); |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * if size != 0 then we know we already have an extent |
| 343 | * to add, so add it. |
| 344 | */ |
| 345 | if (size) { |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 346 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 347 | phys, size, |
| 348 | flags); |
| 349 | if (ret) |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | logical = blk_to_logical(inode, start_blk); |
| 354 | phys = blk_to_logical(inode, tmp.b_blocknr); |
| 355 | size = tmp.b_size; |
| 356 | flags = FIEMAP_EXTENT_MERGED; |
| 357 | |
| 358 | length -= tmp.b_size; |
| 359 | start_blk += logical_to_blk(inode, size); |
| 360 | |
| 361 | /* |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 362 | * If we are past the EOF, then we need to make sure as |
| 363 | * soon as we find a hole that the last extent we found |
| 364 | * is marked with FIEMAP_EXTENT_LAST |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 365 | */ |
Josef Bacik | df3935f | 2009-05-06 16:02:53 -0700 | [diff] [blame] | 366 | if (!past_eof && |
| 367 | logical+size >= |
| 368 | blk_to_logical(inode, 0)+i_size_read(inode)) |
| 369 | past_eof = 1; |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 370 | } |
| 371 | cond_resched(); |
| 372 | } while (1); |
| 373 | |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 374 | /* if ret is 1 then we just hit the end of the extent array */ |
| 375 | if (ret == 1) |
| 376 | ret = 0; |
| 377 | |
| 378 | return ret; |
| 379 | } |
Steven Whitehouse | e9079cc | 2008-10-14 14:43:29 +0100 | [diff] [blame] | 380 | EXPORT_SYMBOL(__generic_block_fiemap); |
| 381 | |
| 382 | /** |
| 383 | * generic_block_fiemap - FIEMAP for block based inodes |
| 384 | * @inode: The inode to map |
| 385 | * @fieinfo: The mapping information |
| 386 | * @start: The initial block to map |
| 387 | * @len: The length of the extect to attempt to map |
| 388 | * @get_block: The block mapping function for the fs |
| 389 | * |
| 390 | * Calls __generic_block_fiemap to map the inode, after taking |
| 391 | * the inode's mutex lock. |
| 392 | */ |
| 393 | |
| 394 | int generic_block_fiemap(struct inode *inode, |
| 395 | struct fiemap_extent_info *fieinfo, u64 start, |
| 396 | u64 len, get_block_t *get_block) |
| 397 | { |
| 398 | int ret; |
| 399 | mutex_lock(&inode->i_mutex); |
| 400 | ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block); |
| 401 | mutex_unlock(&inode->i_mutex); |
| 402 | return ret; |
| 403 | } |
Josef Bacik | 68c9d70 | 2008-10-03 17:32:43 -0400 | [diff] [blame] | 404 | EXPORT_SYMBOL(generic_block_fiemap); |
| 405 | |
Adrian Bunk | 06270d5 | 2008-10-12 07:15:19 +0300 | [diff] [blame] | 406 | #endif /* CONFIG_BLOCK */ |
| 407 | |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 408 | /* |
| 409 | * This provides compatibility with legacy XFS pre-allocation ioctls |
| 410 | * which predate the fallocate syscall. |
| 411 | * |
| 412 | * Only the l_start, l_len and l_whence fields of the 'struct space_resv' |
| 413 | * are used here, rest are ignored. |
| 414 | */ |
| 415 | int ioctl_preallocate(struct file *filp, void __user *argp) |
| 416 | { |
| 417 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 418 | struct space_resv sr; |
| 419 | |
| 420 | if (copy_from_user(&sr, argp, sizeof(sr))) |
| 421 | return -EFAULT; |
| 422 | |
| 423 | switch (sr.l_whence) { |
| 424 | case SEEK_SET: |
| 425 | break; |
| 426 | case SEEK_CUR: |
| 427 | sr.l_start += filp->f_pos; |
| 428 | break; |
| 429 | case SEEK_END: |
| 430 | sr.l_start += i_size_read(inode); |
| 431 | break; |
| 432 | default: |
| 433 | return -EINVAL; |
| 434 | } |
| 435 | |
| 436 | return do_fallocate(filp, FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len); |
| 437 | } |
| 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | static int file_ioctl(struct file *filp, unsigned int cmd, |
| 440 | unsigned long arg) |
| 441 | { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 442 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | int __user *p = (int __user *)arg; |
| 444 | |
| 445 | switch (cmd) { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 446 | case FIBMAP: |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 447 | return ioctl_fibmap(filp, p); |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 448 | case FIONREAD: |
| 449 | return put_user(i_size_read(inode) - filp->f_pos, p); |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 450 | case FS_IOC_RESVSP: |
| 451 | case FS_IOC_RESVSP64: |
| 452 | return ioctl_preallocate(filp, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 455 | return vfs_ioctl(filp, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 458 | static int ioctl_fionbio(struct file *filp, int __user *argp) |
| 459 | { |
| 460 | unsigned int flag; |
| 461 | int on, error; |
| 462 | |
| 463 | error = get_user(on, argp); |
| 464 | if (error) |
| 465 | return error; |
| 466 | flag = O_NONBLOCK; |
| 467 | #ifdef __sparc__ |
| 468 | /* SunOS compatibility item. */ |
| 469 | if (O_NONBLOCK != O_NDELAY) |
| 470 | flag |= O_NDELAY; |
| 471 | #endif |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 472 | spin_lock(&filp->f_lock); |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 473 | if (on) |
| 474 | filp->f_flags |= flag; |
| 475 | else |
| 476 | filp->f_flags &= ~flag; |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 477 | spin_unlock(&filp->f_lock); |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 478 | return error; |
| 479 | } |
| 480 | |
| 481 | static int ioctl_fioasync(unsigned int fd, struct file *filp, |
| 482 | int __user *argp) |
| 483 | { |
| 484 | unsigned int flag; |
| 485 | int on, error; |
| 486 | |
| 487 | error = get_user(on, argp); |
| 488 | if (error) |
| 489 | return error; |
| 490 | flag = on ? FASYNC : 0; |
| 491 | |
| 492 | /* Did FASYNC state change ? */ |
| 493 | if ((flag ^ filp->f_flags) & FASYNC) { |
Jonathan Corbet | 218d11a | 2008-12-05 16:12:48 -0700 | [diff] [blame] | 494 | if (filp->f_op && filp->f_op->fasync) |
Jonathan Corbet | 7639842 | 2009-02-01 14:26:59 -0700 | [diff] [blame] | 495 | /* fasync() adjusts filp->f_flags */ |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 496 | error = filp->f_op->fasync(fd, filp, on); |
Jonathan Corbet | 218d11a | 2008-12-05 16:12:48 -0700 | [diff] [blame] | 497 | else |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 498 | error = -ENOTTY; |
| 499 | } |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 500 | return error < 0 ? error : 0; |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 501 | } |
| 502 | |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 503 | static int ioctl_fsfreeze(struct file *filp) |
| 504 | { |
| 505 | struct super_block *sb = filp->f_path.dentry->d_inode->i_sb; |
| 506 | |
| 507 | if (!capable(CAP_SYS_ADMIN)) |
| 508 | return -EPERM; |
| 509 | |
| 510 | /* If filesystem doesn't support freeze feature, return. */ |
| 511 | if (sb->s_op->freeze_fs == NULL) |
| 512 | return -EOPNOTSUPP; |
| 513 | |
| 514 | /* If a blockdevice-backed filesystem isn't specified, return. */ |
| 515 | if (sb->s_bdev == NULL) |
| 516 | return -EINVAL; |
| 517 | |
| 518 | /* Freeze */ |
| 519 | sb = freeze_bdev(sb->s_bdev); |
| 520 | if (IS_ERR(sb)) |
| 521 | return PTR_ERR(sb); |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static int ioctl_fsthaw(struct file *filp) |
| 526 | { |
| 527 | struct super_block *sb = filp->f_path.dentry->d_inode->i_sb; |
| 528 | |
| 529 | if (!capable(CAP_SYS_ADMIN)) |
| 530 | return -EPERM; |
| 531 | |
| 532 | /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */ |
| 533 | if (sb->s_bdev == NULL) |
| 534 | return -EINVAL; |
| 535 | |
| 536 | /* Thaw */ |
| 537 | return thaw_bdev(sb->s_bdev, sb); |
| 538 | } |
| 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | /* |
| 541 | * When you add any new common ioctls to the switches above and below |
| 542 | * please update compat_sys_ioctl() too. |
| 543 | * |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 544 | * 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] | 545 | * It's just a simple helper for sys_ioctl and compat_sys_ioctl. |
| 546 | */ |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 547 | int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, |
| 548 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | { |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 550 | int error = 0; |
| 551 | int __user *argp = (int __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
| 553 | switch (cmd) { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 554 | case FIOCLEX: |
| 555 | set_close_on_exec(fd, 1); |
| 556 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 558 | case FIONCLEX: |
| 559 | set_close_on_exec(fd, 0); |
| 560 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 562 | case FIONBIO: |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 563 | error = ioctl_fionbio(filp, argp); |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 564 | break; |
| 565 | |
| 566 | case FIOASYNC: |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 567 | error = ioctl_fioasync(fd, filp, argp); |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 568 | break; |
| 569 | |
| 570 | case FIOQSIZE: |
| 571 | if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) || |
| 572 | S_ISREG(filp->f_path.dentry->d_inode->i_mode) || |
| 573 | S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) { |
| 574 | loff_t res = |
| 575 | inode_get_bytes(filp->f_path.dentry->d_inode); |
| 576 | error = copy_to_user((loff_t __user *)arg, &res, |
| 577 | sizeof(res)) ? -EFAULT : 0; |
| 578 | } else |
| 579 | error = -ENOTTY; |
| 580 | break; |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 581 | |
| 582 | case FIFREEZE: |
| 583 | error = ioctl_fsfreeze(filp); |
| 584 | break; |
| 585 | |
| 586 | case FITHAW: |
| 587 | error = ioctl_fsthaw(filp); |
| 588 | break; |
| 589 | |
Aneesh Kumar K.V | 19ba055 | 2009-05-13 18:12:05 -0400 | [diff] [blame] | 590 | case FS_IOC_FIEMAP: |
| 591 | return ioctl_fiemap(filp, arg); |
| 592 | |
| 593 | case FIGETBSZ: |
| 594 | { |
| 595 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 596 | int __user *p = (int __user *)arg; |
| 597 | return put_user(inode->i_sb->s_blocksize, p); |
| 598 | } |
| 599 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 600 | default: |
| 601 | if (S_ISREG(filp->f_path.dentry->d_inode->i_mode)) |
| 602 | error = file_ioctl(filp, cmd, arg); |
| 603 | else |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 604 | error = vfs_ioctl(filp, cmd, arg); |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 605 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
| 607 | return error; |
| 608 | } |
| 609 | |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 610 | SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 612 | struct file *filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | int error = -EBADF; |
| 614 | int fput_needed; |
| 615 | |
| 616 | filp = fget_light(fd, &fput_needed); |
| 617 | if (!filp) |
| 618 | goto out; |
| 619 | |
| 620 | error = security_file_ioctl(filp, cmd, arg); |
| 621 | if (error) |
| 622 | goto out_fput; |
| 623 | |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 624 | error = do_vfs_ioctl(filp, fd, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | out_fput: |
| 626 | fput_light(filp, fput_needed); |
| 627 | out: |
| 628 | return error; |
| 629 | } |