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