ioctl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * linux/fs/ioctl.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/syscalls.h>
  7. #include <linux/mm.h>
  8. #include <linux/smp_lock.h>
  9. #include <linux/capability.h>
  10. #include <linux/file.h>
  11. #include <linux/fs.h>
  12. #include <linux/security.h>
  13. #include <linux/module.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/writeback.h>
  16. #include <linux/buffer_head.h>
  17. #include <asm/ioctls.h>
  18. /* So that the fiemap access checks can't overflow on 32 bit machines. */
  19. #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
  20. /**
  21. * vfs_ioctl - call filesystem specific ioctl methods
  22. * @filp: open file to invoke ioctl method on
  23. * @cmd: ioctl command to execute
  24. * @arg: command-specific argument for ioctl
  25. *
  26. * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
  27. * invokes filesystem specific ->ioctl method. If neither method exists,
  28. * returns -ENOTTY.
  29. *
  30. * Returns 0 on success, -errno on error.
  31. */
  32. static long vfs_ioctl(struct file *filp, unsigned int cmd,
  33. unsigned long arg)
  34. {
  35. int error = -ENOTTY;
  36. if (!filp->f_op)
  37. goto out;
  38. if (filp->f_op->unlocked_ioctl) {
  39. error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
  40. if (error == -ENOIOCTLCMD)
  41. error = -EINVAL;
  42. goto out;
  43. } else if (filp->f_op->ioctl) {
  44. lock_kernel();
  45. error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
  46. filp, cmd, arg);
  47. unlock_kernel();
  48. }
  49. out:
  50. return error;
  51. }
  52. static int ioctl_fibmap(struct file *filp, int __user *p)
  53. {
  54. struct address_space *mapping = filp->f_mapping;
  55. int res, block;
  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;
  64. lock_kernel();
  65. res = mapping->a_ops->bmap(mapping, block);
  66. unlock_kernel();
  67. return put_user(res, p);
  68. }
  69. /**
  70. * fiemap_fill_next_extent - Fiemap helper function
  71. * @fieinfo: Fiemap context passed into ->fiemap
  72. * @logical: Extent logical start offset, in bytes
  73. * @phys: Extent physical start offset, in bytes
  74. * @len: Extent length, in bytes
  75. * @flags: FIEMAP_EXTENT flags that describe this extent
  76. *
  77. * Called from file system ->fiemap callback. Will populate extent
  78. * info as passed in via arguments and copy to user memory. On
  79. * success, extent count on fieinfo is incremented.
  80. *
  81. * Returns 0 on success, -errno on error, 1 if this was the last
  82. * extent that will fit in user array.
  83. */
  84. #define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC)
  85. #define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED)
  86. #define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE)
  87. int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
  88. u64 phys, u64 len, u32 flags)
  89. {
  90. struct fiemap_extent extent;
  91. struct fiemap_extent *dest = fieinfo->fi_extents_start;
  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. if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
  98. return 1;
  99. if (flags & SET_UNKNOWN_FLAGS)
  100. flags |= FIEMAP_EXTENT_UNKNOWN;
  101. if (flags & SET_NO_UNMOUNTED_IO_FLAGS)
  102. flags |= FIEMAP_EXTENT_ENCODED;
  103. if (flags & SET_NOT_ALIGNED_FLAGS)
  104. flags |= FIEMAP_EXTENT_NOT_ALIGNED;
  105. memset(&extent, 0, sizeof(extent));
  106. extent.fe_logical = logical;
  107. extent.fe_physical = phys;
  108. extent.fe_length = len;
  109. extent.fe_flags = flags;
  110. dest += fieinfo->fi_extents_mapped;
  111. if (copy_to_user(dest, &extent, sizeof(extent)))
  112. return -EFAULT;
  113. fieinfo->fi_extents_mapped++;
  114. if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
  115. return 1;
  116. return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
  117. }
  118. EXPORT_SYMBOL(fiemap_fill_next_extent);
  119. /**
  120. * fiemap_check_flags - check validity of requested flags for fiemap
  121. * @fieinfo: Fiemap context passed into ->fiemap
  122. * @fs_flags: Set of fiemap flags that the file system understands
  123. *
  124. * Called from file system ->fiemap callback. This will compute the
  125. * intersection of valid fiemap flags and those that the fs supports. That
  126. * value is then compared against the user supplied flags. In case of bad user
  127. * flags, the invalid values will be written into the fieinfo structure, and
  128. * -EBADR is returned, which tells ioctl_fiemap() to return those values to
  129. * userspace. For this reason, a return code of -EBADR should be preserved.
  130. *
  131. * Returns 0 on success, -EBADR on bad flags.
  132. */
  133. int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags)
  134. {
  135. u32 incompat_flags;
  136. incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags);
  137. if (incompat_flags) {
  138. fieinfo->fi_flags = incompat_flags;
  139. return -EBADR;
  140. }
  141. return 0;
  142. }
  143. EXPORT_SYMBOL(fiemap_check_flags);
  144. static int fiemap_check_ranges(struct super_block *sb,
  145. u64 start, u64 len, u64 *new_len)
  146. {
  147. *new_len = len;
  148. if (len == 0)
  149. return -EINVAL;
  150. if (start > sb->s_maxbytes)
  151. return -EFBIG;
  152. /*
  153. * Shrink request scope to what the fs can actually handle.
  154. */
  155. if ((len > sb->s_maxbytes) ||
  156. (sb->s_maxbytes - len) < start)
  157. *new_len = sb->s_maxbytes - start;
  158. return 0;
  159. }
  160. static int ioctl_fiemap(struct file *filp, unsigned long arg)
  161. {
  162. struct fiemap fiemap;
  163. struct fiemap_extent_info fieinfo = { 0, };
  164. struct inode *inode = filp->f_path.dentry->d_inode;
  165. struct super_block *sb = inode->i_sb;
  166. u64 len;
  167. int error;
  168. if (!inode->i_op->fiemap)
  169. return -EOPNOTSUPP;
  170. if (copy_from_user(&fiemap, (struct fiemap __user *)arg,
  171. sizeof(struct fiemap)))
  172. return -EFAULT;
  173. if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
  174. return -EINVAL;
  175. error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length,
  176. &len);
  177. if (error)
  178. return error;
  179. fieinfo.fi_flags = fiemap.fm_flags;
  180. fieinfo.fi_extents_max = fiemap.fm_extent_count;
  181. fieinfo.fi_extents_start = (struct fiemap_extent *)(arg + sizeof(fiemap));
  182. if (fiemap.fm_extent_count != 0 &&
  183. !access_ok(VERIFY_WRITE, fieinfo.fi_extents_start,
  184. fieinfo.fi_extents_max * sizeof(struct fiemap_extent)))
  185. return -EFAULT;
  186. if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
  187. filemap_write_and_wait(inode->i_mapping);
  188. error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start, len);
  189. fiemap.fm_flags = fieinfo.fi_flags;
  190. fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
  191. if (copy_to_user((char *)arg, &fiemap, sizeof(fiemap)))
  192. error = -EFAULT;
  193. return error;
  194. }
  195. #ifdef CONFIG_BLOCK
  196. #define blk_to_logical(inode, blk) (blk << (inode)->i_blkbits)
  197. #define logical_to_blk(inode, offset) (offset >> (inode)->i_blkbits);
  198. /**
  199. * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
  200. * @inode - the inode to map
  201. * @arg - the pointer to userspace where we copy everything to
  202. * @get_block - the fs's get_block function
  203. *
  204. * This does FIEMAP for block based inodes. Basically it will just loop
  205. * through get_block until we hit the number of extents we want to map, or we
  206. * go past the end of the file and hit a hole.
  207. *
  208. * If it is possible to have data blocks beyond a hole past @inode->i_size, then
  209. * please do not use this function, it will stop at the first unmapped block
  210. * beyond i_size.
  211. *
  212. * If you use this function directly, you need to do your own locking. Use
  213. * generic_block_fiemap if you want the locking done for you.
  214. */
  215. int __generic_block_fiemap(struct inode *inode,
  216. struct fiemap_extent_info *fieinfo, u64 start,
  217. u64 len, get_block_t *get_block)
  218. {
  219. struct buffer_head tmp;
  220. unsigned int start_blk;
  221. long long length = 0, map_len = 0;
  222. u64 logical = 0, phys = 0, size = 0;
  223. u32 flags = FIEMAP_EXTENT_MERGED;
  224. int ret = 0;
  225. if ((ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC)))
  226. return ret;
  227. start_blk = logical_to_blk(inode, start);
  228. length = (long long)min_t(u64, len, i_size_read(inode));
  229. map_len = length;
  230. do {
  231. /*
  232. * we set b_size to the total size we want so it will map as
  233. * many contiguous blocks as possible at once
  234. */
  235. memset(&tmp, 0, sizeof(struct buffer_head));
  236. tmp.b_size = map_len;
  237. ret = get_block(inode, start_blk, &tmp, 0);
  238. if (ret)
  239. break;
  240. /* HOLE */
  241. if (!buffer_mapped(&tmp)) {
  242. /*
  243. * first hole after going past the EOF, this is our
  244. * last extent
  245. */
  246. if (length <= 0) {
  247. flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
  248. ret = fiemap_fill_next_extent(fieinfo, logical,
  249. phys, size,
  250. flags);
  251. break;
  252. }
  253. length -= blk_to_logical(inode, 1);
  254. /* if we have holes up to/past EOF then we're done */
  255. if (length <= 0)
  256. break;
  257. start_blk++;
  258. } else {
  259. if (length <= 0 && size) {
  260. ret = fiemap_fill_next_extent(fieinfo, logical,
  261. phys, size,
  262. flags);
  263. if (ret)
  264. break;
  265. }
  266. logical = blk_to_logical(inode, start_blk);
  267. phys = blk_to_logical(inode, tmp.b_blocknr);
  268. size = tmp.b_size;
  269. flags = FIEMAP_EXTENT_MERGED;
  270. length -= tmp.b_size;
  271. start_blk += logical_to_blk(inode, size);
  272. /*
  273. * if we are past the EOF we need to loop again to see
  274. * if there is a hole so we can mark this extent as the
  275. * last one, and if not keep mapping things until we
  276. * find a hole, or we run out of slots in the extent
  277. * array
  278. */
  279. if (length <= 0)
  280. continue;
  281. ret = fiemap_fill_next_extent(fieinfo, logical, phys,
  282. size, flags);
  283. if (ret)
  284. break;
  285. }
  286. cond_resched();
  287. } while (1);
  288. /* if ret is 1 then we just hit the end of the extent array */
  289. if (ret == 1)
  290. ret = 0;
  291. return ret;
  292. }
  293. EXPORT_SYMBOL(__generic_block_fiemap);
  294. /**
  295. * generic_block_fiemap - FIEMAP for block based inodes
  296. * @inode: The inode to map
  297. * @fieinfo: The mapping information
  298. * @start: The initial block to map
  299. * @len: The length of the extect to attempt to map
  300. * @get_block: The block mapping function for the fs
  301. *
  302. * Calls __generic_block_fiemap to map the inode, after taking
  303. * the inode's mutex lock.
  304. */
  305. int generic_block_fiemap(struct inode *inode,
  306. struct fiemap_extent_info *fieinfo, u64 start,
  307. u64 len, get_block_t *get_block)
  308. {
  309. int ret;
  310. mutex_lock(&inode->i_mutex);
  311. ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
  312. mutex_unlock(&inode->i_mutex);
  313. return ret;
  314. }
  315. EXPORT_SYMBOL(generic_block_fiemap);
  316. #endif /* CONFIG_BLOCK */
  317. static int file_ioctl(struct file *filp, unsigned int cmd,
  318. unsigned long arg)
  319. {
  320. struct inode *inode = filp->f_path.dentry->d_inode;
  321. int __user *p = (int __user *)arg;
  322. switch (cmd) {
  323. case FIBMAP:
  324. return ioctl_fibmap(filp, p);
  325. case FS_IOC_FIEMAP:
  326. return ioctl_fiemap(filp, arg);
  327. case FIGETBSZ:
  328. return put_user(inode->i_sb->s_blocksize, p);
  329. case FIONREAD:
  330. return put_user(i_size_read(inode) - filp->f_pos, p);
  331. }
  332. return vfs_ioctl(filp, cmd, arg);
  333. }
  334. static int ioctl_fionbio(struct file *filp, int __user *argp)
  335. {
  336. unsigned int flag;
  337. int on, error;
  338. error = get_user(on, argp);
  339. if (error)
  340. return error;
  341. flag = O_NONBLOCK;
  342. #ifdef __sparc__
  343. /* SunOS compatibility item. */
  344. if (O_NONBLOCK != O_NDELAY)
  345. flag |= O_NDELAY;
  346. #endif
  347. spin_lock(&filp->f_lock);
  348. if (on)
  349. filp->f_flags |= flag;
  350. else
  351. filp->f_flags &= ~flag;
  352. spin_unlock(&filp->f_lock);
  353. return error;
  354. }
  355. static int ioctl_fioasync(unsigned int fd, struct file *filp,
  356. int __user *argp)
  357. {
  358. unsigned int flag;
  359. int on, error;
  360. error = get_user(on, argp);
  361. if (error)
  362. return error;
  363. flag = on ? FASYNC : 0;
  364. /* Did FASYNC state change ? */
  365. if ((flag ^ filp->f_flags) & FASYNC) {
  366. if (filp->f_op && filp->f_op->fasync)
  367. /* fasync() adjusts filp->f_flags */
  368. error = filp->f_op->fasync(fd, filp, on);
  369. else
  370. error = -ENOTTY;
  371. }
  372. return error < 0 ? error : 0;
  373. }
  374. static int ioctl_fsfreeze(struct file *filp)
  375. {
  376. struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
  377. if (!capable(CAP_SYS_ADMIN))
  378. return -EPERM;
  379. /* If filesystem doesn't support freeze feature, return. */
  380. if (sb->s_op->freeze_fs == NULL)
  381. return -EOPNOTSUPP;
  382. /* If a blockdevice-backed filesystem isn't specified, return. */
  383. if (sb->s_bdev == NULL)
  384. return -EINVAL;
  385. /* Freeze */
  386. sb = freeze_bdev(sb->s_bdev);
  387. if (IS_ERR(sb))
  388. return PTR_ERR(sb);
  389. return 0;
  390. }
  391. static int ioctl_fsthaw(struct file *filp)
  392. {
  393. struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
  394. if (!capable(CAP_SYS_ADMIN))
  395. return -EPERM;
  396. /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */
  397. if (sb->s_bdev == NULL)
  398. return -EINVAL;
  399. /* Thaw */
  400. return thaw_bdev(sb->s_bdev, sb);
  401. }
  402. /*
  403. * When you add any new common ioctls to the switches above and below
  404. * please update compat_sys_ioctl() too.
  405. *
  406. * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
  407. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
  408. */
  409. int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
  410. unsigned long arg)
  411. {
  412. int error = 0;
  413. int __user *argp = (int __user *)arg;
  414. switch (cmd) {
  415. case FIOCLEX:
  416. set_close_on_exec(fd, 1);
  417. break;
  418. case FIONCLEX:
  419. set_close_on_exec(fd, 0);
  420. break;
  421. case FIONBIO:
  422. error = ioctl_fionbio(filp, argp);
  423. break;
  424. case FIOASYNC:
  425. error = ioctl_fioasync(fd, filp, argp);
  426. break;
  427. case FIOQSIZE:
  428. if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
  429. S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
  430. S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
  431. loff_t res =
  432. inode_get_bytes(filp->f_path.dentry->d_inode);
  433. error = copy_to_user((loff_t __user *)arg, &res,
  434. sizeof(res)) ? -EFAULT : 0;
  435. } else
  436. error = -ENOTTY;
  437. break;
  438. case FIFREEZE:
  439. error = ioctl_fsfreeze(filp);
  440. break;
  441. case FITHAW:
  442. error = ioctl_fsthaw(filp);
  443. break;
  444. default:
  445. if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
  446. error = file_ioctl(filp, cmd, arg);
  447. else
  448. error = vfs_ioctl(filp, cmd, arg);
  449. break;
  450. }
  451. return error;
  452. }
  453. SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
  454. {
  455. struct file *filp;
  456. int error = -EBADF;
  457. int fput_needed;
  458. filp = fget_light(fd, &fput_needed);
  459. if (!filp)
  460. goto out;
  461. error = security_file_ioctl(filp, cmd, arg);
  462. if (error)
  463. goto out_fput;
  464. error = do_vfs_ioctl(filp, fd, cmd, arg);
  465. out_fput:
  466. fput_light(filp, fput_needed);
  467. out:
  468. return error;
  469. }