11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/ioctl.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds #include <linux/syscalls.h> 81da177e4SLinus Torvalds #include <linux/mm.h> 916f7e0feSRandy Dunlap #include <linux/capability.h> 101da177e4SLinus Torvalds #include <linux/file.h> 111da177e4SLinus Torvalds #include <linux/fs.h> 121da177e4SLinus Torvalds #include <linux/security.h> 13630d9c47SPaul Gortmaker #include <linux/export.h> 14c9845ff1SErez Zadok #include <linux/uaccess.h> 1568c9d702SJosef Bacik #include <linux/writeback.h> 1668c9d702SJosef Bacik #include <linux/buffer_head.h> 173e63cbb1SAnkit Jain #include <linux/falloc.h> 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <asm/ioctls.h> 201da177e4SLinus Torvalds 21c4b929b8SMark Fasheh /* So that the fiemap access checks can't overflow on 32 bit machines. */ 22c4b929b8SMark Fasheh #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent)) 23c4b929b8SMark Fasheh 24deb21db7SErez Zadok /** 25deb21db7SErez Zadok * vfs_ioctl - call filesystem specific ioctl methods 26f6a4c8bdSChristoph Hellwig * @filp: open file to invoke ioctl method on 27f6a4c8bdSChristoph Hellwig * @cmd: ioctl command to execute 28f6a4c8bdSChristoph Hellwig * @arg: command-specific argument for ioctl 29deb21db7SErez Zadok * 30deb21db7SErez Zadok * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise 31deb21db7SErez Zadok * returns -ENOTTY. 32deb21db7SErez Zadok * 33deb21db7SErez Zadok * Returns 0 on success, -errno on error. 34deb21db7SErez Zadok */ 3567cde595SAdrian Bunk static long vfs_ioctl(struct file *filp, unsigned int cmd, 361da177e4SLinus Torvalds unsigned long arg) 371da177e4SLinus Torvalds { 381da177e4SLinus Torvalds int error = -ENOTTY; 391da177e4SLinus Torvalds 40*72c2d531SAl Viro if (!filp->f_op->unlocked_ioctl) 411da177e4SLinus Torvalds goto out; 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds error = filp->f_op->unlocked_ioctl(filp, cmd, arg); 441da177e4SLinus Torvalds if (error == -ENOIOCTLCMD) 4507d106d0SLinus Torvalds error = -ENOTTY; 461da177e4SLinus Torvalds out: 471da177e4SLinus Torvalds return error; 481da177e4SLinus Torvalds } 491da177e4SLinus Torvalds 50aa81a7c7SErez Zadok static int ioctl_fibmap(struct file *filp, int __user *p) 511da177e4SLinus Torvalds { 521da177e4SLinus Torvalds struct address_space *mapping = filp->f_mapping; 53aa81a7c7SErez Zadok int res, block; 54aa81a7c7SErez Zadok 551da177e4SLinus Torvalds /* do we support this mess? */ 561da177e4SLinus Torvalds if (!mapping->a_ops->bmap) 571da177e4SLinus Torvalds return -EINVAL; 581da177e4SLinus Torvalds if (!capable(CAP_SYS_RAWIO)) 591da177e4SLinus Torvalds return -EPERM; 60aa81a7c7SErez Zadok res = get_user(block, p); 61aa81a7c7SErez Zadok if (res) 62aa81a7c7SErez Zadok return res; 631da177e4SLinus Torvalds res = mapping->a_ops->bmap(mapping, block); 641da177e4SLinus Torvalds return put_user(res, p); 651da177e4SLinus Torvalds } 66aa81a7c7SErez Zadok 67c4b929b8SMark Fasheh /** 68c4b929b8SMark Fasheh * fiemap_fill_next_extent - Fiemap helper function 69c4b929b8SMark Fasheh * @fieinfo: Fiemap context passed into ->fiemap 70c4b929b8SMark Fasheh * @logical: Extent logical start offset, in bytes 71c4b929b8SMark Fasheh * @phys: Extent physical start offset, in bytes 72c4b929b8SMark Fasheh * @len: Extent length, in bytes 73c4b929b8SMark Fasheh * @flags: FIEMAP_EXTENT flags that describe this extent 74c4b929b8SMark Fasheh * 75c4b929b8SMark Fasheh * Called from file system ->fiemap callback. Will populate extent 76c4b929b8SMark Fasheh * info as passed in via arguments and copy to user memory. On 77c4b929b8SMark Fasheh * success, extent count on fieinfo is incremented. 78c4b929b8SMark Fasheh * 79c4b929b8SMark Fasheh * Returns 0 on success, -errno on error, 1 if this was the last 80c4b929b8SMark Fasheh * extent that will fit in user array. 81c4b929b8SMark Fasheh */ 82c4b929b8SMark Fasheh #define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC) 83c4b929b8SMark Fasheh #define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED) 84c4b929b8SMark Fasheh #define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE) 85c4b929b8SMark Fasheh int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical, 86c4b929b8SMark Fasheh u64 phys, u64 len, u32 flags) 87c4b929b8SMark Fasheh { 88c4b929b8SMark Fasheh struct fiemap_extent extent; 89ecf5632dSNamhyung Kim struct fiemap_extent __user *dest = fieinfo->fi_extents_start; 90c4b929b8SMark Fasheh 91c4b929b8SMark Fasheh /* only count the extents */ 92c4b929b8SMark Fasheh if (fieinfo->fi_extents_max == 0) { 93c4b929b8SMark Fasheh fieinfo->fi_extents_mapped++; 94c4b929b8SMark Fasheh return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0; 95c4b929b8SMark Fasheh } 96c4b929b8SMark Fasheh 97c4b929b8SMark Fasheh if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max) 98c4b929b8SMark Fasheh return 1; 99c4b929b8SMark Fasheh 100c4b929b8SMark Fasheh if (flags & SET_UNKNOWN_FLAGS) 101c4b929b8SMark Fasheh flags |= FIEMAP_EXTENT_UNKNOWN; 102c4b929b8SMark Fasheh if (flags & SET_NO_UNMOUNTED_IO_FLAGS) 103c4b929b8SMark Fasheh flags |= FIEMAP_EXTENT_ENCODED; 104c4b929b8SMark Fasheh if (flags & SET_NOT_ALIGNED_FLAGS) 105c4b929b8SMark Fasheh flags |= FIEMAP_EXTENT_NOT_ALIGNED; 106c4b929b8SMark Fasheh 107c4b929b8SMark Fasheh memset(&extent, 0, sizeof(extent)); 108c4b929b8SMark Fasheh extent.fe_logical = logical; 109c4b929b8SMark Fasheh extent.fe_physical = phys; 110c4b929b8SMark Fasheh extent.fe_length = len; 111c4b929b8SMark Fasheh extent.fe_flags = flags; 112c4b929b8SMark Fasheh 113c4b929b8SMark Fasheh dest += fieinfo->fi_extents_mapped; 114c4b929b8SMark Fasheh if (copy_to_user(dest, &extent, sizeof(extent))) 115c4b929b8SMark Fasheh return -EFAULT; 116c4b929b8SMark Fasheh 117c4b929b8SMark Fasheh fieinfo->fi_extents_mapped++; 118c4b929b8SMark Fasheh if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max) 119c4b929b8SMark Fasheh return 1; 120c4b929b8SMark Fasheh return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0; 121c4b929b8SMark Fasheh } 122c4b929b8SMark Fasheh EXPORT_SYMBOL(fiemap_fill_next_extent); 123c4b929b8SMark Fasheh 124c4b929b8SMark Fasheh /** 125c4b929b8SMark Fasheh * fiemap_check_flags - check validity of requested flags for fiemap 126c4b929b8SMark Fasheh * @fieinfo: Fiemap context passed into ->fiemap 127c4b929b8SMark Fasheh * @fs_flags: Set of fiemap flags that the file system understands 128c4b929b8SMark Fasheh * 129c4b929b8SMark Fasheh * Called from file system ->fiemap callback. This will compute the 130c4b929b8SMark Fasheh * intersection of valid fiemap flags and those that the fs supports. That 131c4b929b8SMark Fasheh * value is then compared against the user supplied flags. In case of bad user 132c4b929b8SMark Fasheh * flags, the invalid values will be written into the fieinfo structure, and 133c4b929b8SMark Fasheh * -EBADR is returned, which tells ioctl_fiemap() to return those values to 134c4b929b8SMark Fasheh * userspace. For this reason, a return code of -EBADR should be preserved. 135c4b929b8SMark Fasheh * 136c4b929b8SMark Fasheh * Returns 0 on success, -EBADR on bad flags. 137c4b929b8SMark Fasheh */ 138c4b929b8SMark Fasheh int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags) 139c4b929b8SMark Fasheh { 140c4b929b8SMark Fasheh u32 incompat_flags; 141c4b929b8SMark Fasheh 142c4b929b8SMark Fasheh incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags); 143c4b929b8SMark Fasheh if (incompat_flags) { 144c4b929b8SMark Fasheh fieinfo->fi_flags = incompat_flags; 145c4b929b8SMark Fasheh return -EBADR; 146c4b929b8SMark Fasheh } 147c4b929b8SMark Fasheh return 0; 148c4b929b8SMark Fasheh } 149c4b929b8SMark Fasheh EXPORT_SYMBOL(fiemap_check_flags); 150c4b929b8SMark Fasheh 151c4b929b8SMark Fasheh static int fiemap_check_ranges(struct super_block *sb, 152c4b929b8SMark Fasheh u64 start, u64 len, u64 *new_len) 153c4b929b8SMark Fasheh { 1545aa98b70SJeff Layton u64 maxbytes = (u64) sb->s_maxbytes; 1555aa98b70SJeff Layton 156c4b929b8SMark Fasheh *new_len = len; 157c4b929b8SMark Fasheh 158c4b929b8SMark Fasheh if (len == 0) 159c4b929b8SMark Fasheh return -EINVAL; 160c4b929b8SMark Fasheh 1615aa98b70SJeff Layton if (start > maxbytes) 162c4b929b8SMark Fasheh return -EFBIG; 163c4b929b8SMark Fasheh 164c4b929b8SMark Fasheh /* 165c4b929b8SMark Fasheh * Shrink request scope to what the fs can actually handle. 166c4b929b8SMark Fasheh */ 1675aa98b70SJeff Layton if (len > maxbytes || (maxbytes - len) < start) 1685aa98b70SJeff Layton *new_len = maxbytes - start; 169c4b929b8SMark Fasheh 170c4b929b8SMark Fasheh return 0; 171c4b929b8SMark Fasheh } 172c4b929b8SMark Fasheh 173c4b929b8SMark Fasheh static int ioctl_fiemap(struct file *filp, unsigned long arg) 174c4b929b8SMark Fasheh { 175c4b929b8SMark Fasheh struct fiemap fiemap; 176ecf5632dSNamhyung Kim struct fiemap __user *ufiemap = (struct fiemap __user *) arg; 177c4b929b8SMark Fasheh struct fiemap_extent_info fieinfo = { 0, }; 178496ad9aaSAl Viro struct inode *inode = file_inode(filp); 179c4b929b8SMark Fasheh struct super_block *sb = inode->i_sb; 180c4b929b8SMark Fasheh u64 len; 181c4b929b8SMark Fasheh int error; 182c4b929b8SMark Fasheh 183c4b929b8SMark Fasheh if (!inode->i_op->fiemap) 184c4b929b8SMark Fasheh return -EOPNOTSUPP; 185c4b929b8SMark Fasheh 186ecf5632dSNamhyung Kim if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap))) 187c4b929b8SMark Fasheh return -EFAULT; 188c4b929b8SMark Fasheh 189c4b929b8SMark Fasheh if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS) 190c4b929b8SMark Fasheh return -EINVAL; 191c4b929b8SMark Fasheh 192c4b929b8SMark Fasheh error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length, 193c4b929b8SMark Fasheh &len); 194c4b929b8SMark Fasheh if (error) 195c4b929b8SMark Fasheh return error; 196c4b929b8SMark Fasheh 197c4b929b8SMark Fasheh fieinfo.fi_flags = fiemap.fm_flags; 198c4b929b8SMark Fasheh fieinfo.fi_extents_max = fiemap.fm_extent_count; 199ecf5632dSNamhyung Kim fieinfo.fi_extents_start = ufiemap->fm_extents; 200c4b929b8SMark Fasheh 201c4b929b8SMark Fasheh if (fiemap.fm_extent_count != 0 && 202c4b929b8SMark Fasheh !access_ok(VERIFY_WRITE, fieinfo.fi_extents_start, 203c4b929b8SMark Fasheh fieinfo.fi_extents_max * sizeof(struct fiemap_extent))) 204c4b929b8SMark Fasheh return -EFAULT; 205c4b929b8SMark Fasheh 206c4b929b8SMark Fasheh if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC) 207c4b929b8SMark Fasheh filemap_write_and_wait(inode->i_mapping); 208c4b929b8SMark Fasheh 209c4b929b8SMark Fasheh error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start, len); 210c4b929b8SMark Fasheh fiemap.fm_flags = fieinfo.fi_flags; 211c4b929b8SMark Fasheh fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped; 212ecf5632dSNamhyung Kim if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap))) 213c4b929b8SMark Fasheh error = -EFAULT; 214c4b929b8SMark Fasheh 215c4b929b8SMark Fasheh return error; 216c4b929b8SMark Fasheh } 217c4b929b8SMark Fasheh 21806270d5dSAdrian Bunk #ifdef CONFIG_BLOCK 21906270d5dSAdrian Bunk 2203a3076f4SJosef Bacik static inline sector_t logical_to_blk(struct inode *inode, loff_t offset) 2213a3076f4SJosef Bacik { 2223a3076f4SJosef Bacik return (offset >> inode->i_blkbits); 2233a3076f4SJosef Bacik } 2243a3076f4SJosef Bacik 2253a3076f4SJosef Bacik static inline loff_t blk_to_logical(struct inode *inode, sector_t blk) 2263a3076f4SJosef Bacik { 2273a3076f4SJosef Bacik return (blk << inode->i_blkbits); 2283a3076f4SJosef Bacik } 22968c9d702SJosef Bacik 230e9079cceSSteven Whitehouse /** 231e9079cceSSteven Whitehouse * __generic_block_fiemap - FIEMAP for block based inodes (no locking) 2323a3076f4SJosef Bacik * @inode: the inode to map 2333a3076f4SJosef Bacik * @fieinfo: the fiemap info struct that will be passed back to userspace 2343a3076f4SJosef Bacik * @start: where to start mapping in the inode 2353a3076f4SJosef Bacik * @len: how much space to map 2363a3076f4SJosef Bacik * @get_block: the fs's get_block function 23768c9d702SJosef Bacik * 23868c9d702SJosef Bacik * This does FIEMAP for block based inodes. Basically it will just loop 23968c9d702SJosef Bacik * through get_block until we hit the number of extents we want to map, or we 24068c9d702SJosef Bacik * go past the end of the file and hit a hole. 24168c9d702SJosef Bacik * 24268c9d702SJosef Bacik * If it is possible to have data blocks beyond a hole past @inode->i_size, then 24368c9d702SJosef Bacik * please do not use this function, it will stop at the first unmapped block 244e9079cceSSteven Whitehouse * beyond i_size. 245e9079cceSSteven Whitehouse * 246e9079cceSSteven Whitehouse * If you use this function directly, you need to do your own locking. Use 247e9079cceSSteven Whitehouse * generic_block_fiemap if you want the locking done for you. 24868c9d702SJosef Bacik */ 249e9079cceSSteven Whitehouse 250e9079cceSSteven Whitehouse int __generic_block_fiemap(struct inode *inode, 2513a3076f4SJosef Bacik struct fiemap_extent_info *fieinfo, loff_t start, 2523a3076f4SJosef Bacik loff_t len, get_block_t *get_block) 25368c9d702SJosef Bacik { 2543a3076f4SJosef Bacik struct buffer_head map_bh; 2553a3076f4SJosef Bacik sector_t start_blk, last_blk; 2563a3076f4SJosef Bacik loff_t isize = i_size_read(inode); 25768c9d702SJosef Bacik u64 logical = 0, phys = 0, size = 0; 25868c9d702SJosef Bacik u32 flags = FIEMAP_EXTENT_MERGED; 2593a3076f4SJosef Bacik bool past_eof = false, whole_file = false; 2603a3076f4SJosef Bacik int ret = 0; 26168c9d702SJosef Bacik 2623a3076f4SJosef Bacik ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); 2633a3076f4SJosef Bacik if (ret) 26468c9d702SJosef Bacik return ret; 26568c9d702SJosef Bacik 2663a3076f4SJosef Bacik /* 2673a3076f4SJosef Bacik * Either the i_mutex or other appropriate locking needs to be held 2683a3076f4SJosef Bacik * since we expect isize to not change at all through the duration of 2693a3076f4SJosef Bacik * this call. 2703a3076f4SJosef Bacik */ 2713a3076f4SJosef Bacik if (len >= isize) { 2723a3076f4SJosef Bacik whole_file = true; 2733a3076f4SJosef Bacik len = isize; 2743a3076f4SJosef Bacik } 2753a3076f4SJosef Bacik 276d54cdc8cSJosef Bacik /* 277d54cdc8cSJosef Bacik * Some filesystems can't deal with being asked to map less than 278d54cdc8cSJosef Bacik * blocksize, so make sure our len is at least block length. 279d54cdc8cSJosef Bacik */ 280d54cdc8cSJosef Bacik if (logical_to_blk(inode, len) == 0) 281d54cdc8cSJosef Bacik len = blk_to_logical(inode, 1); 282d54cdc8cSJosef Bacik 28368c9d702SJosef Bacik start_blk = logical_to_blk(inode, start); 2843a3076f4SJosef Bacik last_blk = logical_to_blk(inode, start + len - 1); 28568c9d702SJosef Bacik 28668c9d702SJosef Bacik do { 28768c9d702SJosef Bacik /* 28868c9d702SJosef Bacik * we set b_size to the total size we want so it will map as 28968c9d702SJosef Bacik * many contiguous blocks as possible at once 29068c9d702SJosef Bacik */ 2913a3076f4SJosef Bacik memset(&map_bh, 0, sizeof(struct buffer_head)); 2923a3076f4SJosef Bacik map_bh.b_size = len; 29368c9d702SJosef Bacik 2943a3076f4SJosef Bacik ret = get_block(inode, start_blk, &map_bh, 0); 29568c9d702SJosef Bacik if (ret) 29668c9d702SJosef Bacik break; 29768c9d702SJosef Bacik 29868c9d702SJosef Bacik /* HOLE */ 2993a3076f4SJosef Bacik if (!buffer_mapped(&map_bh)) { 300df3935ffSJosef Bacik start_blk++; 301df3935ffSJosef Bacik 302df3935ffSJosef Bacik /* 3033a3076f4SJosef Bacik * We want to handle the case where there is an 304df3935ffSJosef Bacik * allocated block at the front of the file, and then 305df3935ffSJosef Bacik * nothing but holes up to the end of the file properly, 306df3935ffSJosef Bacik * to make sure that extent at the front gets properly 307df3935ffSJosef Bacik * marked with FIEMAP_EXTENT_LAST 308df3935ffSJosef Bacik */ 309df3935ffSJosef Bacik if (!past_eof && 3103a3076f4SJosef Bacik blk_to_logical(inode, start_blk) >= isize) 311df3935ffSJosef Bacik past_eof = 1; 312df3935ffSJosef Bacik 31368c9d702SJosef Bacik /* 3143a3076f4SJosef Bacik * First hole after going past the EOF, this is our 31568c9d702SJosef Bacik * last extent 31668c9d702SJosef Bacik */ 317df3935ffSJosef Bacik if (past_eof && size) { 31868c9d702SJosef Bacik flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST; 31968c9d702SJosef Bacik ret = fiemap_fill_next_extent(fieinfo, logical, 32068c9d702SJosef Bacik phys, size, 32168c9d702SJosef Bacik flags); 3223a3076f4SJosef Bacik } else if (size) { 3233a3076f4SJosef Bacik ret = fiemap_fill_next_extent(fieinfo, logical, 3243a3076f4SJosef Bacik phys, size, flags); 3253a3076f4SJosef Bacik size = 0; 32668c9d702SJosef Bacik } 32768c9d702SJosef Bacik 32868c9d702SJosef Bacik /* if we have holes up to/past EOF then we're done */ 3293a3076f4SJosef Bacik if (start_blk > last_blk || past_eof || ret) 33068c9d702SJosef Bacik break; 33168c9d702SJosef Bacik } else { 332df3935ffSJosef Bacik /* 3333a3076f4SJosef Bacik * We have gone over the length of what we wanted to 334df3935ffSJosef Bacik * map, and it wasn't the entire file, so add the extent 335df3935ffSJosef Bacik * we got last time and exit. 336df3935ffSJosef Bacik * 337df3935ffSJosef Bacik * This is for the case where say we want to map all the 338df3935ffSJosef Bacik * way up to the second to the last block in a file, but 339df3935ffSJosef Bacik * the last block is a hole, making the second to last 340df3935ffSJosef Bacik * block FIEMAP_EXTENT_LAST. In this case we want to 341df3935ffSJosef Bacik * see if there is a hole after the second to last block 342df3935ffSJosef Bacik * so we can mark it properly. If we found data after 343df3935ffSJosef Bacik * we exceeded the length we were requesting, then we 344df3935ffSJosef Bacik * are good to go, just add the extent to the fieinfo 345df3935ffSJosef Bacik * and break 346df3935ffSJosef Bacik */ 3473a3076f4SJosef Bacik if (start_blk > last_blk && !whole_file) { 348df3935ffSJosef Bacik ret = fiemap_fill_next_extent(fieinfo, logical, 349df3935ffSJosef Bacik phys, size, 350df3935ffSJosef Bacik flags); 351df3935ffSJosef Bacik break; 352df3935ffSJosef Bacik } 353df3935ffSJosef Bacik 354df3935ffSJosef Bacik /* 355df3935ffSJosef Bacik * if size != 0 then we know we already have an extent 356df3935ffSJosef Bacik * to add, so add it. 357df3935ffSJosef Bacik */ 358df3935ffSJosef Bacik if (size) { 35968c9d702SJosef Bacik ret = fiemap_fill_next_extent(fieinfo, logical, 36068c9d702SJosef Bacik phys, size, 36168c9d702SJosef Bacik flags); 36268c9d702SJosef Bacik if (ret) 36368c9d702SJosef Bacik break; 36468c9d702SJosef Bacik } 36568c9d702SJosef Bacik 36668c9d702SJosef Bacik logical = blk_to_logical(inode, start_blk); 3673a3076f4SJosef Bacik phys = blk_to_logical(inode, map_bh.b_blocknr); 3683a3076f4SJosef Bacik size = map_bh.b_size; 36968c9d702SJosef Bacik flags = FIEMAP_EXTENT_MERGED; 37068c9d702SJosef Bacik 37168c9d702SJosef Bacik start_blk += logical_to_blk(inode, size); 37268c9d702SJosef Bacik 37368c9d702SJosef Bacik /* 374df3935ffSJosef Bacik * If we are past the EOF, then we need to make sure as 375df3935ffSJosef Bacik * soon as we find a hole that the last extent we found 376df3935ffSJosef Bacik * is marked with FIEMAP_EXTENT_LAST 37768c9d702SJosef Bacik */ 3783a3076f4SJosef Bacik if (!past_eof && logical + size >= isize) 3793a3076f4SJosef Bacik past_eof = true; 38068c9d702SJosef Bacik } 38168c9d702SJosef Bacik cond_resched(); 38268c9d702SJosef Bacik } while (1); 38368c9d702SJosef Bacik 3843a3076f4SJosef Bacik /* If ret is 1 then we just hit the end of the extent array */ 38568c9d702SJosef Bacik if (ret == 1) 38668c9d702SJosef Bacik ret = 0; 38768c9d702SJosef Bacik 38868c9d702SJosef Bacik return ret; 38968c9d702SJosef Bacik } 390e9079cceSSteven Whitehouse EXPORT_SYMBOL(__generic_block_fiemap); 391e9079cceSSteven Whitehouse 392e9079cceSSteven Whitehouse /** 393e9079cceSSteven Whitehouse * generic_block_fiemap - FIEMAP for block based inodes 394e9079cceSSteven Whitehouse * @inode: The inode to map 395e9079cceSSteven Whitehouse * @fieinfo: The mapping information 396e9079cceSSteven Whitehouse * @start: The initial block to map 397e9079cceSSteven Whitehouse * @len: The length of the extect to attempt to map 398e9079cceSSteven Whitehouse * @get_block: The block mapping function for the fs 399e9079cceSSteven Whitehouse * 400e9079cceSSteven Whitehouse * Calls __generic_block_fiemap to map the inode, after taking 401e9079cceSSteven Whitehouse * the inode's mutex lock. 402e9079cceSSteven Whitehouse */ 403e9079cceSSteven Whitehouse 404e9079cceSSteven Whitehouse int generic_block_fiemap(struct inode *inode, 405e9079cceSSteven Whitehouse struct fiemap_extent_info *fieinfo, u64 start, 406e9079cceSSteven Whitehouse u64 len, get_block_t *get_block) 407e9079cceSSteven Whitehouse { 408e9079cceSSteven Whitehouse int ret; 409e9079cceSSteven Whitehouse mutex_lock(&inode->i_mutex); 410e9079cceSSteven Whitehouse ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block); 411e9079cceSSteven Whitehouse mutex_unlock(&inode->i_mutex); 412e9079cceSSteven Whitehouse return ret; 413e9079cceSSteven Whitehouse } 41468c9d702SJosef Bacik EXPORT_SYMBOL(generic_block_fiemap); 41568c9d702SJosef Bacik 41606270d5dSAdrian Bunk #endif /* CONFIG_BLOCK */ 41706270d5dSAdrian Bunk 4183e63cbb1SAnkit Jain /* 4193e63cbb1SAnkit Jain * This provides compatibility with legacy XFS pre-allocation ioctls 4203e63cbb1SAnkit Jain * which predate the fallocate syscall. 4213e63cbb1SAnkit Jain * 4223e63cbb1SAnkit Jain * Only the l_start, l_len and l_whence fields of the 'struct space_resv' 4233e63cbb1SAnkit Jain * are used here, rest are ignored. 4243e63cbb1SAnkit Jain */ 4253e63cbb1SAnkit Jain int ioctl_preallocate(struct file *filp, void __user *argp) 4263e63cbb1SAnkit Jain { 427496ad9aaSAl Viro struct inode *inode = file_inode(filp); 4283e63cbb1SAnkit Jain struct space_resv sr; 4293e63cbb1SAnkit Jain 4303e63cbb1SAnkit Jain if (copy_from_user(&sr, argp, sizeof(sr))) 4313e63cbb1SAnkit Jain return -EFAULT; 4323e63cbb1SAnkit Jain 4333e63cbb1SAnkit Jain switch (sr.l_whence) { 4343e63cbb1SAnkit Jain case SEEK_SET: 4353e63cbb1SAnkit Jain break; 4363e63cbb1SAnkit Jain case SEEK_CUR: 4373e63cbb1SAnkit Jain sr.l_start += filp->f_pos; 4383e63cbb1SAnkit Jain break; 4393e63cbb1SAnkit Jain case SEEK_END: 4403e63cbb1SAnkit Jain sr.l_start += i_size_read(inode); 4413e63cbb1SAnkit Jain break; 4423e63cbb1SAnkit Jain default: 4433e63cbb1SAnkit Jain return -EINVAL; 4443e63cbb1SAnkit Jain } 4453e63cbb1SAnkit Jain 44672c72bdfSAnna Schumaker return vfs_fallocate(filp, FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len); 4473e63cbb1SAnkit Jain } 4483e63cbb1SAnkit Jain 449aa81a7c7SErez Zadok static int file_ioctl(struct file *filp, unsigned int cmd, 450aa81a7c7SErez Zadok unsigned long arg) 451aa81a7c7SErez Zadok { 452496ad9aaSAl Viro struct inode *inode = file_inode(filp); 453aa81a7c7SErez Zadok int __user *p = (int __user *)arg; 454aa81a7c7SErez Zadok 455aa81a7c7SErez Zadok switch (cmd) { 456aa81a7c7SErez Zadok case FIBMAP: 457aa81a7c7SErez Zadok return ioctl_fibmap(filp, p); 4581da177e4SLinus Torvalds case FIONREAD: 4591da177e4SLinus Torvalds return put_user(i_size_read(inode) - filp->f_pos, p); 4603e63cbb1SAnkit Jain case FS_IOC_RESVSP: 4613e63cbb1SAnkit Jain case FS_IOC_RESVSP64: 4623e63cbb1SAnkit Jain return ioctl_preallocate(filp, p); 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 465deb21db7SErez Zadok return vfs_ioctl(filp, cmd, arg); 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 468aa81a7c7SErez Zadok static int ioctl_fionbio(struct file *filp, int __user *argp) 469aa81a7c7SErez Zadok { 470aa81a7c7SErez Zadok unsigned int flag; 471aa81a7c7SErez Zadok int on, error; 472aa81a7c7SErez Zadok 473aa81a7c7SErez Zadok error = get_user(on, argp); 474aa81a7c7SErez Zadok if (error) 475aa81a7c7SErez Zadok return error; 476aa81a7c7SErez Zadok flag = O_NONBLOCK; 477aa81a7c7SErez Zadok #ifdef __sparc__ 478aa81a7c7SErez Zadok /* SunOS compatibility item. */ 479aa81a7c7SErez Zadok if (O_NONBLOCK != O_NDELAY) 480aa81a7c7SErez Zadok flag |= O_NDELAY; 481aa81a7c7SErez Zadok #endif 482db1dd4d3SJonathan Corbet spin_lock(&filp->f_lock); 483aa81a7c7SErez Zadok if (on) 484aa81a7c7SErez Zadok filp->f_flags |= flag; 485aa81a7c7SErez Zadok else 486aa81a7c7SErez Zadok filp->f_flags &= ~flag; 487db1dd4d3SJonathan Corbet spin_unlock(&filp->f_lock); 488aa81a7c7SErez Zadok return error; 489aa81a7c7SErez Zadok } 490aa81a7c7SErez Zadok 491aa81a7c7SErez Zadok static int ioctl_fioasync(unsigned int fd, struct file *filp, 492aa81a7c7SErez Zadok int __user *argp) 493aa81a7c7SErez Zadok { 494aa81a7c7SErez Zadok unsigned int flag; 495aa81a7c7SErez Zadok int on, error; 496aa81a7c7SErez Zadok 497aa81a7c7SErez Zadok error = get_user(on, argp); 498aa81a7c7SErez Zadok if (error) 499aa81a7c7SErez Zadok return error; 500aa81a7c7SErez Zadok flag = on ? FASYNC : 0; 501aa81a7c7SErez Zadok 502aa81a7c7SErez Zadok /* Did FASYNC state change ? */ 503aa81a7c7SErez Zadok if ((flag ^ filp->f_flags) & FASYNC) { 504*72c2d531SAl Viro if (filp->f_op->fasync) 50576398425SJonathan Corbet /* fasync() adjusts filp->f_flags */ 506aa81a7c7SErez Zadok error = filp->f_op->fasync(fd, filp, on); 507218d11a8SJonathan Corbet else 508aa81a7c7SErez Zadok error = -ENOTTY; 509aa81a7c7SErez Zadok } 51060aa4924SJonathan Corbet return error < 0 ? error : 0; 511aa81a7c7SErez Zadok } 512aa81a7c7SErez Zadok 513fcccf502STakashi Sato static int ioctl_fsfreeze(struct file *filp) 514fcccf502STakashi Sato { 515496ad9aaSAl Viro struct super_block *sb = file_inode(filp)->i_sb; 516fcccf502STakashi Sato 517fcccf502STakashi Sato if (!capable(CAP_SYS_ADMIN)) 518fcccf502STakashi Sato return -EPERM; 519fcccf502STakashi Sato 520fcccf502STakashi Sato /* If filesystem doesn't support freeze feature, return. */ 52148b6bca6SBenjamin Marzinski if (sb->s_op->freeze_fs == NULL && sb->s_op->freeze_super == NULL) 522fcccf502STakashi Sato return -EOPNOTSUPP; 523fcccf502STakashi Sato 524fcccf502STakashi Sato /* Freeze */ 52548b6bca6SBenjamin Marzinski if (sb->s_op->freeze_super) 52648b6bca6SBenjamin Marzinski return sb->s_op->freeze_super(sb); 52718e9e510SJosef Bacik return freeze_super(sb); 528fcccf502STakashi Sato } 529fcccf502STakashi Sato 530fcccf502STakashi Sato static int ioctl_fsthaw(struct file *filp) 531fcccf502STakashi Sato { 532496ad9aaSAl Viro struct super_block *sb = file_inode(filp)->i_sb; 533fcccf502STakashi Sato 534fcccf502STakashi Sato if (!capable(CAP_SYS_ADMIN)) 535fcccf502STakashi Sato return -EPERM; 536fcccf502STakashi Sato 537fcccf502STakashi Sato /* Thaw */ 53848b6bca6SBenjamin Marzinski if (sb->s_op->thaw_super) 53948b6bca6SBenjamin Marzinski return sb->s_op->thaw_super(sb); 54018e9e510SJosef Bacik return thaw_super(sb); 541fcccf502STakashi Sato } 542fcccf502STakashi Sato 5431da177e4SLinus Torvalds /* 5441da177e4SLinus Torvalds * When you add any new common ioctls to the switches above and below 5451da177e4SLinus Torvalds * please update compat_sys_ioctl() too. 5461da177e4SLinus Torvalds * 547deb21db7SErez Zadok * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d. 5481da177e4SLinus Torvalds * It's just a simple helper for sys_ioctl and compat_sys_ioctl. 5491da177e4SLinus Torvalds */ 550deb21db7SErez Zadok int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, 551c9845ff1SErez Zadok unsigned long arg) 5521da177e4SLinus Torvalds { 553aa81a7c7SErez Zadok int error = 0; 554aa81a7c7SErez Zadok int __user *argp = (int __user *)arg; 555496ad9aaSAl Viro struct inode *inode = file_inode(filp); 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds switch (cmd) { 5581da177e4SLinus Torvalds case FIOCLEX: 5591da177e4SLinus Torvalds set_close_on_exec(fd, 1); 5601da177e4SLinus Torvalds break; 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds case FIONCLEX: 5631da177e4SLinus Torvalds set_close_on_exec(fd, 0); 5641da177e4SLinus Torvalds break; 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds case FIONBIO: 567aa81a7c7SErez Zadok error = ioctl_fionbio(filp, argp); 5681da177e4SLinus Torvalds break; 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds case FIOASYNC: 571aa81a7c7SErez Zadok error = ioctl_fioasync(fd, filp, argp); 5721da177e4SLinus Torvalds break; 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds case FIOQSIZE: 57527a4f7e6SNamhyung Kim if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode) || 57627a4f7e6SNamhyung Kim S_ISLNK(inode->i_mode)) { 57727a4f7e6SNamhyung Kim loff_t res = inode_get_bytes(inode); 57827a4f7e6SNamhyung Kim error = copy_to_user(argp, &res, sizeof(res)) ? 57927a4f7e6SNamhyung Kim -EFAULT : 0; 580c9845ff1SErez Zadok } else 5811da177e4SLinus Torvalds error = -ENOTTY; 5821da177e4SLinus Torvalds break; 583fcccf502STakashi Sato 584fcccf502STakashi Sato case FIFREEZE: 585fcccf502STakashi Sato error = ioctl_fsfreeze(filp); 586fcccf502STakashi Sato break; 587fcccf502STakashi Sato 588fcccf502STakashi Sato case FITHAW: 589fcccf502STakashi Sato error = ioctl_fsthaw(filp); 590fcccf502STakashi Sato break; 591fcccf502STakashi Sato 59219ba0559SAneesh Kumar K.V case FS_IOC_FIEMAP: 59319ba0559SAneesh Kumar K.V return ioctl_fiemap(filp, arg); 59419ba0559SAneesh Kumar K.V 59519ba0559SAneesh Kumar K.V case FIGETBSZ: 59627a4f7e6SNamhyung Kim return put_user(inode->i_sb->s_blocksize, argp); 59719ba0559SAneesh Kumar K.V 5981da177e4SLinus Torvalds default: 59927a4f7e6SNamhyung Kim if (S_ISREG(inode->i_mode)) 6001da177e4SLinus Torvalds error = file_ioctl(filp, cmd, arg); 6011da177e4SLinus Torvalds else 602deb21db7SErez Zadok error = vfs_ioctl(filp, cmd, arg); 6031da177e4SLinus Torvalds break; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds return error; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 608a26eab24SHeiko Carstens SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg) 6091da177e4SLinus Torvalds { 6102903ff01SAl Viro int error; 6112903ff01SAl Viro struct fd f = fdget(fd); 6121da177e4SLinus Torvalds 6132903ff01SAl Viro if (!f.file) 6142903ff01SAl Viro return -EBADF; 6152903ff01SAl Viro error = security_file_ioctl(f.file, cmd, arg); 6162903ff01SAl Viro if (!error) 6172903ff01SAl Viro error = do_vfs_ioctl(f.file, fd, cmd, arg); 6182903ff01SAl Viro fdput(f); 6191da177e4SLinus Torvalds return error; 6201da177e4SLinus Torvalds } 621