ioctl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 <linux/falloc.h>
  18. #include <asm/ioctls.h>
  19. /* So that the fiemap access checks can't overflow on 32 bit machines. */
  20. #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
  21. /**
  22. * vfs_ioctl - call filesystem specific ioctl methods
  23. * @filp: open file to invoke ioctl method on
  24. * @cmd: ioctl command to execute
  25. * @arg: command-specific argument for ioctl
  26. *
  27. * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
  28. * invokes filesystem specific ->ioctl method. If neither method exists,
  29. * returns -ENOTTY.
  30. *
  31. * Returns 0 on success, -errno on error.
  32. */
  33. static long vfs_ioctl(struct file *filp, unsigned int cmd,
  34. unsigned long arg)
  35. {
  36. int error = -ENOTTY;
  37. if (!filp->f_op)
  38. goto out;
  39. if (filp->f_op->unlocked_ioctl) {
  40. error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
  41. if (error == -ENOIOCTLCMD)
  42. error = -EINVAL;
  43. goto out;
  44. } else if (filp->f_op->ioctl) {
  45. lock_kernel();
  46. error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
  47. filp, cmd, arg);
  48. unlock_kernel();
  49. }
  50. out:
  51. return error;
  52. }
  53. static int ioctl_fibmap(struct file *filp, int __user *p)
  54. {
  55. struct address_space *mapping = filp->f_mapping;
  56. int res, block;
  57. /* do we support this mess? */
  58. if (!mapping->a_ops->bmap)
  59. return -EINVAL;
  60. if (!capable(CAP_SYS_RAWIO))
  61. return -EPERM;
  62. res = get_user(block, p);
  63. if (res)
  64. return res;
  65. res = mapping->a_ops->bmap(mapping, block);
  66. return put_user(res, p);
  67. }
  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. /* only count the extents */
  92. if (fieinfo->fi_extents_max == 0) {
  93. fieinfo->fi_extents_mapped++;
  94. return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
  95. }
  96. if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
  97. return 1;
  98. if (flags & SET_UNKNOWN_FLAGS)
  99. flags |= FIEMAP_EXTENT_UNKNOWN;
  100. if (flags & SET_NO_UNMOUNTED_IO_FLAGS)
  101. flags |= FIEMAP_EXTENT_ENCODED;
  102. if (flags & SET_NOT_ALIGNED_FLAGS)
  103. flags |= FIEMAP_EXTENT_NOT_ALIGNED;
  104. memset(&extent, 0, sizeof(extent));
  105. extent.fe_logical = logical;
  106. extent.fe_physical = phys;
  107. extent.fe_length = len;
  108. extent.fe_flags = flags;
  109. dest += fieinfo->fi_extents_mapped;
  110. if (copy_to_user(dest, &extent, sizeof(extent)))
  111. return -EFAULT;
  112. fieinfo->fi_extents_mapped++;
  113. if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
  114. return 1;
  115. return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
  116. }
  117. EXPORT_SYMBOL(fiemap_fill_next_extent);
  118. /**
  119. * fiemap_check_flags - check validity of requested flags for fiemap
  120. * @fieinfo: Fiemap context passed into ->fiemap
  121. * @fs_flags: Set of fiemap flags that the file system understands
  122. *
  123. * Called from file system ->fiemap callback. This will compute the
  124. * intersection of valid fiemap flags and those that the fs supports. That
  125. * value is then compared against the user supplied flags. In case of bad user
  126. * flags, the invalid values will be written into the fieinfo structure, and
  127. * -EBADR is returned, which tells ioctl_fiemap() to return those values to
  128. * userspace. For this reason, a return code of -EBADR should be preserved.
  129. *
  130. * Returns 0 on success, -EBADR on bad flags.
  131. */
  132. int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags)
  133. {
  134. u32 incompat_flags;
  135. incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags);
  136. if (incompat_flags) {
  137. fieinfo->fi_flags = incompat_flags;
  138. return -EBADR;
  139. }
  140. return 0;
  141. }
  142. EXPORT_SYMBOL(fiemap_check_flags);
  143. static int fiemap_check_ranges(struct super_block *sb,
  144. u64 start, u64 len, u64 *new_len)
  145. {
  146. *new_len = len;
  147. if (len == 0)
  148. return -EINVAL;
  149. if (start > sb->s_maxbytes)
  150. return -EFBIG;
  151. /*
  152. * Shrink request scope to what the fs can actually handle.
  153. */
  154. if ((len > sb->s_maxbytes) ||
  155. (sb->s_maxbytes - len) < start)
  156. *new_len = sb->s_maxbytes - start;
  157. return 0;
  158. }
  159. static int ioctl_fiemap(struct file *filp, unsigned long arg)
  160. {
  161. struct fiemap fiemap;
  162. struct fiemap_extent_info fieinfo = { 0, };
  163. struct inode *inode = filp->f_path.dentry->d_inode;
  164. struct super_block *sb = inode->i_sb;
  165. u64 len;
  166. int error;
  167. if (!inode->i_op->fiemap)
  168. return -EOPNOTSUPP;
  169. if (copy_from_user(&fiemap, (struct fiemap __user *)arg,
  170. sizeof(struct fiemap)))
  171. return -EFAULT;
  172. if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
  173. return -EINVAL;
  174. error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length,
  175. &len);
  176. if (error)
  177. return error;
  178. fieinfo.fi_flags = fiemap.fm_flags;
  179. fieinfo.fi_extents_max = fiemap.fm_extent_count;
  180. fieinfo.fi_extents_start = (struct fiemap_extent *)(arg + sizeof(fiemap));
  181. if (fiemap.fm_extent_count != 0 &&
  182. !access_ok(VERIFY_WRITE, fieinfo.fi_extents_start,
  183. fieinfo.fi_extents_max * sizeof(struct fiemap_extent)))
  184. return -EFAULT;
  185. if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
  186. filemap_write_and_wait(inode->i_mapping);
  187. error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start, len);
  188. fiemap.fm_flags = fieinfo.fi_flags;
  189. fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
  190. if (copy_to_user((char *)arg, &fiemap, sizeof(fiemap)))
  191. error = -EFAULT;
  192. return error;
  193. }
  194. #ifdef CONFIG_BLOCK
  195. #define blk_to_logical(inode, blk) (blk << (inode)->i_blkbits)
  196. #define logical_to_blk(inode, offset) (offset >> (inode)->i_blkbits);
  197. /**
  198. * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
  199. * @inode - the inode to map
  200. * @arg - the pointer to userspace where we copy everything to
  201. * @get_block - the fs's get_block function
  202. *
  203. * This does FIEMAP for block based inodes. Basically it will just loop
  204. * through get_block until we hit the number of extents we want to map, or we
  205. * go past the end of the file and hit a hole.
  206. *
  207. * If it is possible to have data blocks beyond a hole past @inode->i_size, then
  208. * please do not use this function, it will stop at the first unmapped block
  209. * beyond i_size.
  210. *
  211. * If you use this function directly, you need to do your own locking. Use
  212. * generic_block_fiemap if you want the locking done for you.
  213. */
  214. int __generic_block_fiemap(struct inode *inode,
  215. struct fiemap_extent_info *fieinfo, u64 start,
  216. u64 len, get_block_t *get_block)
  217. {
  218. struct buffer_head tmp;
  219. unsigned int start_blk;
  220. long long length = 0, map_len = 0;
  221. u64 logical = 0, phys = 0, size = 0;
  222. u32 flags = FIEMAP_EXTENT_MERGED;
  223. int ret = 0, past_eof = 0, whole_file = 0;
  224. if ((ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC)))
  225. return ret;
  226. start_blk = logical_to_blk(inode, start);
  227. length = (long long)min_t(u64, len, i_size_read(inode));
  228. if (length < len)
  229. whole_file = 1;
  230. map_len = length;
  231. do {
  232. /*
  233. * we set b_size to the total size we want so it will map as
  234. * many contiguous blocks as possible at once
  235. */
  236. memset(&tmp, 0, sizeof(struct buffer_head));
  237. tmp.b_size = map_len;
  238. ret = get_block(inode, start_blk, &tmp, 0);
  239. if (ret)
  240. break;
  241. /* HOLE */
  242. if (!buffer_mapped(&tmp)) {
  243. length -= blk_to_logical(inode, 1);
  244. start_blk++;
  245. /*
  246. * we want to handle the case where there is an
  247. * allocated block at the front of the file, and then
  248. * nothing but holes up to the end of the file properly,
  249. * to make sure that extent at the front gets properly
  250. * marked with FIEMAP_EXTENT_LAST
  251. */
  252. if (!past_eof &&
  253. blk_to_logical(inode, start_blk) >=
  254. blk_to_logical(inode, 0)+i_size_read(inode))
  255. past_eof = 1;
  256. /*
  257. * first hole after going past the EOF, this is our
  258. * last extent
  259. */
  260. if (past_eof && size) {
  261. flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
  262. ret = fiemap_fill_next_extent(fieinfo, logical,
  263. phys, size,
  264. flags);
  265. break;
  266. }
  267. /* if we have holes up to/past EOF then we're done */
  268. if (length <= 0 || past_eof)
  269. break;
  270. } else {
  271. /*
  272. * we have gone over the length of what we wanted to
  273. * map, and it wasn't the entire file, so add the extent
  274. * we got last time and exit.
  275. *
  276. * This is for the case where say we want to map all the
  277. * way up to the second to the last block in a file, but
  278. * the last block is a hole, making the second to last
  279. * block FIEMAP_EXTENT_LAST. In this case we want to
  280. * see if there is a hole after the second to last block
  281. * so we can mark it properly. If we found data after
  282. * we exceeded the length we were requesting, then we
  283. * are good to go, just add the extent to the fieinfo
  284. * and break
  285. */
  286. if (length <= 0 && !whole_file) {
  287. ret = fiemap_fill_next_extent(fieinfo, logical,
  288. phys, size,
  289. flags);
  290. break;
  291. }
  292. /*
  293. * if size != 0 then we know we already have an extent
  294. * to add, so add it.
  295. */
  296. if (size) {
  297. ret = fiemap_fill_next_extent(fieinfo, logical,
  298. phys, size,
  299. flags);
  300. if (ret)
  301. break;
  302. }
  303. logical = blk_to_logical(inode, start_blk);
  304. phys = blk_to_logical(inode, tmp.b_blocknr);
  305. size = tmp.b_size;
  306. flags = FIEMAP_EXTENT_MERGED;
  307. length -= tmp.b_size;
  308. start_blk += logical_to_blk(inode, size);
  309. /*
  310. * If we are past the EOF, then we need to make sure as
  311. * soon as we find a hole that the last extent we found
  312. * is marked with FIEMAP_EXTENT_LAST
  313. */
  314. if (!past_eof &&
  315. logical+size >=
  316. blk_to_logical(inode, 0)+i_size_read(inode))
  317. past_eof = 1;
  318. }
  319. cond_resched();
  320. } while (1);
  321. /* if ret is 1 then we just hit the end of the extent array */
  322. if (ret == 1)
  323. ret = 0;
  324. return ret;
  325. }
  326. EXPORT_SYMBOL(__generic_block_fiemap);
  327. /**
  328. * generic_block_fiemap - FIEMAP for block based inodes
  329. * @inode: The inode to map
  330. * @fieinfo: The mapping information
  331. * @start: The initial block to map
  332. * @len: The length of the extect to attempt to map
  333. * @get_block: The block mapping function for the fs
  334. *
  335. * Calls __generic_block_fiemap to map the inode, after taking
  336. * the inode's mutex lock.
  337. */
  338. int generic_block_fiemap(struct inode *inode,
  339. struct fiemap_extent_info *fieinfo, u64 start,
  340. u64 len, get_block_t *get_block)
  341. {
  342. int ret;
  343. mutex_lock(&inode->i_mutex);
  344. ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
  345. mutex_unlock(&inode->i_mutex);
  346. return ret;
  347. }
  348. EXPORT_SYMBOL(generic_block_fiemap);
  349. #endif /* CONFIG_BLOCK */
  350. /*
  351. * This provides compatibility with legacy XFS pre-allocation ioctls
  352. * which predate the fallocate syscall.
  353. *
  354. * Only the l_start, l_len and l_whence fields of the 'struct space_resv'
  355. * are used here, rest are ignored.
  356. */
  357. int ioctl_preallocate(struct file *filp, void __user *argp)
  358. {
  359. struct inode *inode = filp->f_path.dentry->d_inode;
  360. struct space_resv sr;
  361. if (copy_from_user(&sr, argp, sizeof(sr)))
  362. return -EFAULT;
  363. switch (sr.l_whence) {
  364. case SEEK_SET:
  365. break;
  366. case SEEK_CUR:
  367. sr.l_start += filp->f_pos;
  368. break;
  369. case SEEK_END:
  370. sr.l_start += i_size_read(inode);
  371. break;
  372. default:
  373. return -EINVAL;
  374. }
  375. return do_fallocate(filp, FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len);
  376. }
  377. static int file_ioctl(struct file *filp, unsigned int cmd,
  378. unsigned long arg)
  379. {
  380. struct inode *inode = filp->f_path.dentry->d_inode;
  381. int __user *p = (int __user *)arg;
  382. switch (cmd) {
  383. case FIBMAP:
  384. return ioctl_fibmap(filp, p);
  385. case FIONREAD:
  386. return put_user(i_size_read(inode) - filp->f_pos, p);
  387. case FS_IOC_RESVSP:
  388. case FS_IOC_RESVSP64:
  389. return ioctl_preallocate(filp, p);
  390. }
  391. return vfs_ioctl(filp, cmd, arg);
  392. }
  393. static int ioctl_fionbio(struct file *filp, int __user *argp)
  394. {
  395. unsigned int flag;
  396. int on, error;
  397. error = get_user(on, argp);
  398. if (error)
  399. return error;
  400. flag = O_NONBLOCK;
  401. #ifdef __sparc__
  402. /* SunOS compatibility item. */
  403. if (O_NONBLOCK != O_NDELAY)
  404. flag |= O_NDELAY;
  405. #endif
  406. spin_lock(&filp->f_lock);
  407. if (on)
  408. filp->f_flags |= flag;
  409. else
  410. filp->f_flags &= ~flag;
  411. spin_unlock(&filp->f_lock);
  412. return error;
  413. }
  414. static int ioctl_fioasync(unsigned int fd, struct file *filp,
  415. int __user *argp)
  416. {
  417. unsigned int flag;
  418. int on, error;
  419. error = get_user(on, argp);
  420. if (error)
  421. return error;
  422. flag = on ? FASYNC : 0;
  423. /* Did FASYNC state change ? */
  424. if ((flag ^ filp->f_flags) & FASYNC) {
  425. if (filp->f_op && filp->f_op->fasync)
  426. /* fasync() adjusts filp->f_flags */
  427. error = filp->f_op->fasync(fd, filp, on);
  428. else
  429. error = -ENOTTY;
  430. }
  431. return error < 0 ? error : 0;
  432. }
  433. static int ioctl_fsfreeze(struct file *filp)
  434. {
  435. struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
  436. if (!capable(CAP_SYS_ADMIN))
  437. return -EPERM;
  438. /* If filesystem doesn't support freeze feature, return. */
  439. if (sb->s_op->freeze_fs == NULL)
  440. return -EOPNOTSUPP;
  441. /* If a blockdevice-backed filesystem isn't specified, return. */
  442. if (sb->s_bdev == NULL)
  443. return -EINVAL;
  444. /* Freeze */
  445. sb = freeze_bdev(sb->s_bdev);
  446. if (IS_ERR(sb))
  447. return PTR_ERR(sb);
  448. return 0;
  449. }
  450. static int ioctl_fsthaw(struct file *filp)
  451. {
  452. struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
  453. if (!capable(CAP_SYS_ADMIN))
  454. return -EPERM;
  455. /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */
  456. if (sb->s_bdev == NULL)
  457. return -EINVAL;
  458. /* Thaw */
  459. return thaw_bdev(sb->s_bdev, sb);
  460. }
  461. /*
  462. * When you add any new common ioctls to the switches above and below
  463. * please update compat_sys_ioctl() too.
  464. *
  465. * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
  466. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
  467. */
  468. int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
  469. unsigned long arg)
  470. {
  471. int error = 0;
  472. int __user *argp = (int __user *)arg;
  473. switch (cmd) {
  474. case FIOCLEX:
  475. set_close_on_exec(fd, 1);
  476. break;
  477. case FIONCLEX:
  478. set_close_on_exec(fd, 0);
  479. break;
  480. case FIONBIO:
  481. error = ioctl_fionbio(filp, argp);
  482. break;
  483. case FIOASYNC:
  484. error = ioctl_fioasync(fd, filp, argp);
  485. break;
  486. case FIOQSIZE:
  487. if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
  488. S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
  489. S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
  490. loff_t res =
  491. inode_get_bytes(filp->f_path.dentry->d_inode);
  492. error = copy_to_user((loff_t __user *)arg, &res,
  493. sizeof(res)) ? -EFAULT : 0;
  494. } else
  495. error = -ENOTTY;
  496. break;
  497. case FIFREEZE:
  498. error = ioctl_fsfreeze(filp);
  499. break;
  500. case FITHAW:
  501. error = ioctl_fsthaw(filp);
  502. break;
  503. case FS_IOC_FIEMAP:
  504. return ioctl_fiemap(filp, arg);
  505. case FIGETBSZ:
  506. {
  507. struct inode *inode = filp->f_path.dentry->d_inode;
  508. int __user *p = (int __user *)arg;
  509. return put_user(inode->i_sb->s_blocksize, p);
  510. }
  511. default:
  512. if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
  513. error = file_ioctl(filp, cmd, arg);
  514. else
  515. error = vfs_ioctl(filp, cmd, arg);
  516. break;
  517. }
  518. return error;
  519. }
  520. SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
  521. {
  522. struct file *filp;
  523. int error = -EBADF;
  524. int fput_needed;
  525. filp = fget_light(fd, &fput_needed);
  526. if (!filp)
  527. goto out;
  528. error = security_file_ioctl(filp, cmd, arg);
  529. if (error)
  530. goto out_fput;
  531. error = do_vfs_ioctl(filp, fd, cmd, arg);
  532. out_fput:
  533. fput_light(filp, fput_needed);
  534. out:
  535. return error;
  536. }