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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/ioctls.h> |
| 20 | |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 21 | /* So that the fiemap access checks can't overflow on 32 bit machines. */ |
| 22 | #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent)) |
| 23 | |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 24 | /** |
| 25 | * vfs_ioctl - call filesystem specific ioctl methods |
Christoph Hellwig | f6a4c8b | 2008-02-09 00:10:16 -0800 | [diff] [blame] | 26 | * @filp: open file to invoke ioctl method on |
| 27 | * @cmd: ioctl command to execute |
| 28 | * @arg: command-specific argument for ioctl |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 29 | * |
| 30 | * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise |
Christoph Hellwig | f6a4c8b | 2008-02-09 00:10:16 -0800 | [diff] [blame] | 31 | * invokes filesystem specific ->ioctl method. If neither method exists, |
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 | |
| 41 | if (!filp->f_op) |
| 42 | goto out; |
| 43 | |
| 44 | if (filp->f_op->unlocked_ioctl) { |
| 45 | error = filp->f_op->unlocked_ioctl(filp, cmd, arg); |
| 46 | if (error == -ENOIOCTLCMD) |
| 47 | error = -EINVAL; |
| 48 | goto out; |
Andrew Morton | 64d67d2 | 2007-07-15 23:41:02 -0700 | [diff] [blame] | 49 | } else if (filp->f_op->ioctl) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | lock_kernel(); |
Andrew Morton | 64d67d2 | 2007-07-15 23:41:02 -0700 | [diff] [blame] | 51 | error = filp->f_op->ioctl(filp->f_path.dentry->d_inode, |
| 52 | filp, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | unlock_kernel(); |
| 54 | } |
| 55 | |
| 56 | out: |
| 57 | return error; |
| 58 | } |
| 59 | |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 60 | static int ioctl_fibmap(struct file *filp, int __user *p) |
| 61 | { |
| 62 | struct address_space *mapping = filp->f_mapping; |
| 63 | int res, block; |
| 64 | |
| 65 | /* do we support this mess? */ |
| 66 | if (!mapping->a_ops->bmap) |
| 67 | return -EINVAL; |
| 68 | if (!capable(CAP_SYS_RAWIO)) |
| 69 | return -EPERM; |
| 70 | res = get_user(block, p); |
| 71 | if (res) |
| 72 | return res; |
| 73 | lock_kernel(); |
| 74 | res = mapping->a_ops->bmap(mapping, block); |
| 75 | unlock_kernel(); |
| 76 | return put_user(res, p); |
| 77 | } |
| 78 | |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 79 | /** |
| 80 | * fiemap_fill_next_extent - Fiemap helper function |
| 81 | * @fieinfo: Fiemap context passed into ->fiemap |
| 82 | * @logical: Extent logical start offset, in bytes |
| 83 | * @phys: Extent physical start offset, in bytes |
| 84 | * @len: Extent length, in bytes |
| 85 | * @flags: FIEMAP_EXTENT flags that describe this extent |
| 86 | * |
| 87 | * Called from file system ->fiemap callback. Will populate extent |
| 88 | * info as passed in via arguments and copy to user memory. On |
| 89 | * success, extent count on fieinfo is incremented. |
| 90 | * |
| 91 | * Returns 0 on success, -errno on error, 1 if this was the last |
| 92 | * extent that will fit in user array. |
| 93 | */ |
| 94 | #define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC) |
| 95 | #define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED) |
| 96 | #define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE) |
| 97 | int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical, |
| 98 | u64 phys, u64 len, u32 flags) |
| 99 | { |
| 100 | struct fiemap_extent extent; |
| 101 | struct fiemap_extent *dest = fieinfo->fi_extents_start; |
| 102 | |
| 103 | /* only count the extents */ |
| 104 | if (fieinfo->fi_extents_max == 0) { |
| 105 | fieinfo->fi_extents_mapped++; |
| 106 | return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0; |
| 107 | } |
| 108 | |
| 109 | if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max) |
| 110 | return 1; |
| 111 | |
| 112 | if (flags & SET_UNKNOWN_FLAGS) |
| 113 | flags |= FIEMAP_EXTENT_UNKNOWN; |
| 114 | if (flags & SET_NO_UNMOUNTED_IO_FLAGS) |
| 115 | flags |= FIEMAP_EXTENT_ENCODED; |
| 116 | if (flags & SET_NOT_ALIGNED_FLAGS) |
| 117 | flags |= FIEMAP_EXTENT_NOT_ALIGNED; |
| 118 | |
| 119 | memset(&extent, 0, sizeof(extent)); |
| 120 | extent.fe_logical = logical; |
| 121 | extent.fe_physical = phys; |
| 122 | extent.fe_length = len; |
| 123 | extent.fe_flags = flags; |
| 124 | |
| 125 | dest += fieinfo->fi_extents_mapped; |
| 126 | if (copy_to_user(dest, &extent, sizeof(extent))) |
| 127 | return -EFAULT; |
| 128 | |
| 129 | fieinfo->fi_extents_mapped++; |
| 130 | if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max) |
| 131 | return 1; |
| 132 | return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0; |
| 133 | } |
| 134 | EXPORT_SYMBOL(fiemap_fill_next_extent); |
| 135 | |
| 136 | /** |
| 137 | * fiemap_check_flags - check validity of requested flags for fiemap |
| 138 | * @fieinfo: Fiemap context passed into ->fiemap |
| 139 | * @fs_flags: Set of fiemap flags that the file system understands |
| 140 | * |
| 141 | * Called from file system ->fiemap callback. This will compute the |
| 142 | * intersection of valid fiemap flags and those that the fs supports. That |
| 143 | * value is then compared against the user supplied flags. In case of bad user |
| 144 | * flags, the invalid values will be written into the fieinfo structure, and |
| 145 | * -EBADR is returned, which tells ioctl_fiemap() to return those values to |
| 146 | * userspace. For this reason, a return code of -EBADR should be preserved. |
| 147 | * |
| 148 | * Returns 0 on success, -EBADR on bad flags. |
| 149 | */ |
| 150 | int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags) |
| 151 | { |
| 152 | u32 incompat_flags; |
| 153 | |
| 154 | incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags); |
| 155 | if (incompat_flags) { |
| 156 | fieinfo->fi_flags = incompat_flags; |
| 157 | return -EBADR; |
| 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | EXPORT_SYMBOL(fiemap_check_flags); |
| 162 | |
| 163 | static int fiemap_check_ranges(struct super_block *sb, |
| 164 | u64 start, u64 len, u64 *new_len) |
| 165 | { |
| 166 | *new_len = len; |
| 167 | |
| 168 | if (len == 0) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | if (start > sb->s_maxbytes) |
| 172 | return -EFBIG; |
| 173 | |
| 174 | /* |
| 175 | * Shrink request scope to what the fs can actually handle. |
| 176 | */ |
| 177 | if ((len > sb->s_maxbytes) || |
| 178 | (sb->s_maxbytes - len) < start) |
| 179 | *new_len = sb->s_maxbytes - start; |
| 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 | |
| 234 | /* |
| 235 | * @inode - the inode to map |
| 236 | * @arg - the pointer to userspace where we copy everything to |
| 237 | * @get_block - the fs's get_block function |
| 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 |
| 245 | * beyond i_size |
| 246 | */ |
| 247 | int generic_block_fiemap(struct inode *inode, |
| 248 | struct fiemap_extent_info *fieinfo, u64 start, |
| 249 | u64 len, get_block_t *get_block) |
| 250 | { |
| 251 | struct buffer_head tmp; |
| 252 | unsigned int start_blk; |
| 253 | long long length = 0, map_len = 0; |
| 254 | u64 logical = 0, phys = 0, size = 0; |
| 255 | u32 flags = FIEMAP_EXTENT_MERGED; |
| 256 | int ret = 0; |
| 257 | |
| 258 | if ((ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC))) |
| 259 | return ret; |
| 260 | |
| 261 | start_blk = logical_to_blk(inode, start); |
| 262 | |
| 263 | /* guard against change */ |
| 264 | mutex_lock(&inode->i_mutex); |
| 265 | |
| 266 | length = (long long)min_t(u64, len, i_size_read(inode)); |
| 267 | map_len = length; |
| 268 | |
| 269 | do { |
| 270 | /* |
| 271 | * we set b_size to the total size we want so it will map as |
| 272 | * many contiguous blocks as possible at once |
| 273 | */ |
| 274 | memset(&tmp, 0, sizeof(struct buffer_head)); |
| 275 | tmp.b_size = map_len; |
| 276 | |
| 277 | ret = get_block(inode, start_blk, &tmp, 0); |
| 278 | if (ret) |
| 279 | break; |
| 280 | |
| 281 | /* HOLE */ |
| 282 | if (!buffer_mapped(&tmp)) { |
| 283 | /* |
| 284 | * first hole after going past the EOF, this is our |
| 285 | * last extent |
| 286 | */ |
| 287 | if (length <= 0) { |
| 288 | flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST; |
| 289 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 290 | phys, size, |
| 291 | flags); |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | length -= blk_to_logical(inode, 1); |
| 296 | |
| 297 | /* if we have holes up to/past EOF then we're done */ |
| 298 | if (length <= 0) |
| 299 | break; |
| 300 | |
| 301 | start_blk++; |
| 302 | } else { |
| 303 | if (length <= 0 && size) { |
| 304 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 305 | phys, size, |
| 306 | flags); |
| 307 | if (ret) |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | logical = blk_to_logical(inode, start_blk); |
| 312 | phys = blk_to_logical(inode, tmp.b_blocknr); |
| 313 | size = tmp.b_size; |
| 314 | flags = FIEMAP_EXTENT_MERGED; |
| 315 | |
| 316 | length -= tmp.b_size; |
| 317 | start_blk += logical_to_blk(inode, size); |
| 318 | |
| 319 | /* |
| 320 | * if we are past the EOF we need to loop again to see |
| 321 | * if there is a hole so we can mark this extent as the |
| 322 | * last one, and if not keep mapping things until we |
| 323 | * find a hole, or we run out of slots in the extent |
| 324 | * array |
| 325 | */ |
| 326 | if (length <= 0) |
| 327 | continue; |
| 328 | |
| 329 | ret = fiemap_fill_next_extent(fieinfo, logical, phys, |
| 330 | size, flags); |
| 331 | if (ret) |
| 332 | break; |
| 333 | } |
| 334 | cond_resched(); |
| 335 | } while (1); |
| 336 | |
| 337 | mutex_unlock(&inode->i_mutex); |
| 338 | |
| 339 | /* if ret is 1 then we just hit the end of the extent array */ |
| 340 | if (ret == 1) |
| 341 | ret = 0; |
| 342 | |
| 343 | return ret; |
| 344 | } |
| 345 | EXPORT_SYMBOL(generic_block_fiemap); |
| 346 | |
Adrian Bunk | 06270d5 | 2008-10-12 07:15:19 +0300 | [diff] [blame] | 347 | #endif /* CONFIG_BLOCK */ |
| 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | static int file_ioctl(struct file *filp, unsigned int cmd, |
| 350 | unsigned long arg) |
| 351 | { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 352 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | int __user *p = (int __user *)arg; |
| 354 | |
| 355 | switch (cmd) { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 356 | case FIBMAP: |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 357 | return ioctl_fibmap(filp, p); |
Mark Fasheh | c4b929b | 2008-10-08 19:44:18 -0400 | [diff] [blame] | 358 | case FS_IOC_FIEMAP: |
| 359 | return ioctl_fiemap(filp, arg); |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 360 | case FIGETBSZ: |
| 361 | return put_user(inode->i_sb->s_blocksize, p); |
| 362 | case FIONREAD: |
| 363 | return put_user(i_size_read(inode) - filp->f_pos, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 366 | return vfs_ioctl(filp, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 369 | static int ioctl_fionbio(struct file *filp, int __user *argp) |
| 370 | { |
| 371 | unsigned int flag; |
| 372 | int on, error; |
| 373 | |
| 374 | error = get_user(on, argp); |
| 375 | if (error) |
| 376 | return error; |
| 377 | flag = O_NONBLOCK; |
| 378 | #ifdef __sparc__ |
| 379 | /* SunOS compatibility item. */ |
| 380 | if (O_NONBLOCK != O_NDELAY) |
| 381 | flag |= O_NDELAY; |
| 382 | #endif |
| 383 | if (on) |
| 384 | filp->f_flags |= flag; |
| 385 | else |
| 386 | filp->f_flags &= ~flag; |
| 387 | return error; |
| 388 | } |
| 389 | |
| 390 | static int ioctl_fioasync(unsigned int fd, struct file *filp, |
| 391 | int __user *argp) |
| 392 | { |
| 393 | unsigned int flag; |
| 394 | int on, error; |
| 395 | |
| 396 | error = get_user(on, argp); |
| 397 | if (error) |
| 398 | return error; |
| 399 | flag = on ? FASYNC : 0; |
| 400 | |
| 401 | /* Did FASYNC state change ? */ |
| 402 | if ((flag ^ filp->f_flags) & FASYNC) { |
Jonathan Corbet | 218d11a | 2008-12-05 16:12:48 -0700 | [diff] [blame] | 403 | if (filp->f_op && filp->f_op->fasync) |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 404 | error = filp->f_op->fasync(fd, filp, on); |
Jonathan Corbet | 218d11a | 2008-12-05 16:12:48 -0700 | [diff] [blame] | 405 | else |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 406 | error = -ENOTTY; |
| 407 | } |
| 408 | if (error) |
| 409 | return error; |
| 410 | |
| 411 | if (on) |
| 412 | filp->f_flags |= FASYNC; |
| 413 | else |
| 414 | filp->f_flags &= ~FASYNC; |
| 415 | return error; |
| 416 | } |
| 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | /* |
| 419 | * When you add any new common ioctls to the switches above and below |
| 420 | * please update compat_sys_ioctl() too. |
| 421 | * |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 422 | * 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] | 423 | * It's just a simple helper for sys_ioctl and compat_sys_ioctl. |
| 424 | */ |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 425 | int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, |
| 426 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 428 | int error = 0; |
| 429 | int __user *argp = (int __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | switch (cmd) { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 432 | case FIOCLEX: |
| 433 | set_close_on_exec(fd, 1); |
| 434 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 436 | case FIONCLEX: |
| 437 | set_close_on_exec(fd, 0); |
| 438 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 440 | case FIONBIO: |
Jonathan Corbet | 218d11a | 2008-12-05 16:12:48 -0700 | [diff] [blame] | 441 | /* BKL needed to avoid races tweaking f_flags */ |
| 442 | lock_kernel(); |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 443 | error = ioctl_fionbio(filp, argp); |
Jonathan Corbet | 218d11a | 2008-12-05 16:12:48 -0700 | [diff] [blame] | 444 | unlock_kernel(); |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 445 | break; |
| 446 | |
| 447 | case FIOASYNC: |
Jonathan Corbet | 218d11a | 2008-12-05 16:12:48 -0700 | [diff] [blame] | 448 | /* BKL needed to avoid races tweaking f_flags */ |
| 449 | lock_kernel(); |
Erez Zadok | aa81a7c | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 450 | error = ioctl_fioasync(fd, filp, argp); |
Jonathan Corbet | 218d11a | 2008-12-05 16:12:48 -0700 | [diff] [blame] | 451 | unlock_kernel(); |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 452 | break; |
| 453 | |
| 454 | case FIOQSIZE: |
| 455 | if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) || |
| 456 | S_ISREG(filp->f_path.dentry->d_inode->i_mode) || |
| 457 | S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) { |
| 458 | loff_t res = |
| 459 | inode_get_bytes(filp->f_path.dentry->d_inode); |
| 460 | error = copy_to_user((loff_t __user *)arg, &res, |
| 461 | sizeof(res)) ? -EFAULT : 0; |
| 462 | } else |
| 463 | error = -ENOTTY; |
| 464 | break; |
| 465 | default: |
| 466 | if (S_ISREG(filp->f_path.dentry->d_inode->i_mode)) |
| 467 | error = file_ioctl(filp, cmd, arg); |
| 468 | else |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 469 | error = vfs_ioctl(filp, cmd, arg); |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 470 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
| 472 | return error; |
| 473 | } |
| 474 | |
| 475 | asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg) |
| 476 | { |
Erez Zadok | c9845ff | 2008-02-07 00:13:23 -0800 | [diff] [blame] | 477 | struct file *filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | int error = -EBADF; |
| 479 | int fput_needed; |
| 480 | |
| 481 | filp = fget_light(fd, &fput_needed); |
| 482 | if (!filp) |
| 483 | goto out; |
| 484 | |
| 485 | error = security_file_ioctl(filp, cmd, arg); |
| 486 | if (error) |
| 487 | goto out_fput; |
| 488 | |
Erez Zadok | deb21db | 2008-02-07 00:13:25 -0800 | [diff] [blame] | 489 | error = do_vfs_ioctl(filp, fd, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | out_fput: |
| 491 | fput_light(filp, fput_needed); |
| 492 | out: |
| 493 | return error; |
| 494 | } |