ioctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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, past_eof = 0, whole_file = 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. if (length < len)
  230. whole_file = 1;
  231. map_len = length;
  232. do {
  233. /*
  234. * we set b_size to the total size we want so it will map as
  235. * many contiguous blocks as possible at once
  236. */
  237. memset(&tmp, 0, sizeof(struct buffer_head));
  238. tmp.b_size = map_len;
  239. ret = get_block(inode, start_blk, &tmp, 0);
  240. if (ret)
  241. break;
  242. /* HOLE */
  243. if (!buffer_mapped(&tmp)) {
  244. length -= blk_to_logical(inode, 1);
  245. start_blk++;
  246. /*
  247. * we want to handle the case where there is an
  248. * allocated block at the front of the file, and then
  249. * nothing but holes up to the end of the file properly,
  250. * to make sure that extent at the front gets properly
  251. * marked with FIEMAP_EXTENT_LAST
  252. */
  253. if (!past_eof &&
  254. blk_to_logical(inode, start_blk) >=
  255. blk_to_logical(inode, 0)+i_size_read(inode))
  256. past_eof = 1;
  257. /*
  258. * first hole after going past the EOF, this is our
  259. * last extent
  260. */
  261. if (past_eof && size) {
  262. flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
  263. ret = fiemap_fill_next_extent(fieinfo, logical,
  264. phys, size,
  265. flags);
  266. break;
  267. }
  268. /* if we have holes up to/past EOF then we're done */
  269. if (length <= 0 || past_eof)
  270. break;
  271. } else {
  272. /*
  273. * we have gone over the length of what we wanted to
  274. * map, and it wasn't the entire file, so add the extent
  275. * we got last time and exit.
  276. *
  277. * This is for the case where say we want to map all the
  278. * way up to the second to the last block in a file, but
  279. * the last block is a hole, making the second to last
  280. * block FIEMAP_EXTENT_LAST. In this case we want to
  281. * see if there is a hole after the second to last block
  282. * so we can mark it properly. If we found data after
  283. * we exceeded the length we were requesting, then we
  284. * are good to go, just add the extent to the fieinfo
  285. * and break
  286. */
  287. if (length <= 0 && !whole_file) {
  288. ret = fiemap_fill_next_extent(fieinfo, logical,
  289. phys, size,
  290. flags);
  291. break;
  292. }
  293. /*
  294. * if size != 0 then we know we already have an extent
  295. * to add, so add it.
  296. */
  297. if (size) {
  298. ret = fiemap_fill_next_extent(fieinfo, logical,
  299. phys, size,
  300. flags);
  301. if (ret)
  302. break;
  303. }
  304. logical = blk_to_logical(inode, start_blk);
  305. phys = blk_to_logical(inode, tmp.b_blocknr);
  306. size = tmp.b_size;
  307. flags = FIEMAP_EXTENT_MERGED;
  308. length -= tmp.b_size;
  309. start_blk += logical_to_blk(inode, size);
  310. /*
  311. * If we are past the EOF, then we need to make sure as
  312. * soon as we find a hole that the last extent we found
  313. * is marked with FIEMAP_EXTENT_LAST
  314. */
  315. if (!past_eof &&
  316. logical+size >=
  317. blk_to_logical(inode, 0)+i_size_read(inode))
  318. past_eof = 1;
  319. }
  320. cond_resched();
  321. } while (1);
  322. /* if ret is 1 then we just hit the end of the extent array */
  323. if (ret == 1)
  324. ret = 0;
  325. return ret;
  326. }
  327. EXPORT_SYMBOL(__generic_block_fiemap);
  328. /**
  329. * generic_block_fiemap - FIEMAP for block based inodes
  330. * @inode: The inode to map
  331. * @fieinfo: The mapping information
  332. * @start: The initial block to map
  333. * @len: The length of the extect to attempt to map
  334. * @get_block: The block mapping function for the fs
  335. *
  336. * Calls __generic_block_fiemap to map the inode, after taking
  337. * the inode's mutex lock.
  338. */
  339. int generic_block_fiemap(struct inode *inode,
  340. struct fiemap_extent_info *fieinfo, u64 start,
  341. u64 len, get_block_t *get_block)
  342. {
  343. int ret;
  344. mutex_lock(&inode->i_mutex);
  345. ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
  346. mutex_unlock(&inode->i_mutex);
  347. return ret;
  348. }
  349. EXPORT_SYMBOL(generic_block_fiemap);
  350. #endif /* CONFIG_BLOCK */
  351. static int file_ioctl(struct file *filp, unsigned int cmd,
  352. unsigned long arg)
  353. {
  354. struct inode *inode = filp->f_path.dentry->d_inode;
  355. int __user *p = (int __user *)arg;
  356. switch (cmd) {
  357. case FIBMAP:
  358. return ioctl_fibmap(filp, p);
  359. case FS_IOC_FIEMAP:
  360. return ioctl_fiemap(filp, arg);
  361. case FIGETBSZ:
  362. return put_user(inode->i_sb->s_blocksize, p);
  363. case FIONREAD:
  364. return put_user(i_size_read(inode) - filp->f_pos, p);
  365. }
  366. return vfs_ioctl(filp, cmd, arg);
  367. }
  368. static int ioctl_fionbio(struct file *filp, int __user *argp)
  369. {
  370. unsigned int flag;
  371. int on, error;
  372. error = get_user(on, argp);
  373. if (error)
  374. return error;
  375. flag = O_NONBLOCK;
  376. #ifdef __sparc__
  377. /* SunOS compatibility item. */
  378. if (O_NONBLOCK != O_NDELAY)
  379. flag |= O_NDELAY;
  380. #endif
  381. spin_lock(&filp->f_lock);
  382. if (on)
  383. filp->f_flags |= flag;
  384. else
  385. filp->f_flags &= ~flag;
  386. spin_unlock(&filp->f_lock);
  387. return error;
  388. }
  389. static int ioctl_fioasync(unsigned int fd, struct file *filp,
  390. int __user *argp)
  391. {
  392. unsigned int flag;
  393. int on, error;
  394. error = get_user(on, argp);
  395. if (error)
  396. return error;
  397. flag = on ? FASYNC : 0;
  398. /* Did FASYNC state change ? */
  399. if ((flag ^ filp->f_flags) & FASYNC) {
  400. if (filp->f_op && filp->f_op->fasync)
  401. /* fasync() adjusts filp->f_flags */
  402. error = filp->f_op->fasync(fd, filp, on);
  403. else
  404. error = -ENOTTY;
  405. }
  406. return error < 0 ? error : 0;
  407. }
  408. static int ioctl_fsfreeze(struct file *filp)
  409. {
  410. struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
  411. if (!capable(CAP_SYS_ADMIN))
  412. return -EPERM;
  413. /* If filesystem doesn't support freeze feature, return. */
  414. if (sb->s_op->freeze_fs == NULL)
  415. return -EOPNOTSUPP;
  416. /* If a blockdevice-backed filesystem isn't specified, return. */
  417. if (sb->s_bdev == NULL)
  418. return -EINVAL;
  419. /* Freeze */
  420. sb = freeze_bdev(sb->s_bdev);
  421. if (IS_ERR(sb))
  422. return PTR_ERR(sb);
  423. return 0;
  424. }
  425. static int ioctl_fsthaw(struct file *filp)
  426. {
  427. struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
  428. if (!capable(CAP_SYS_ADMIN))
  429. return -EPERM;
  430. /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */
  431. if (sb->s_bdev == NULL)
  432. return -EINVAL;
  433. /* Thaw */
  434. return thaw_bdev(sb->s_bdev, sb);
  435. }
  436. /*
  437. * When you add any new common ioctls to the switches above and below
  438. * please update compat_sys_ioctl() too.
  439. *
  440. * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
  441. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
  442. */
  443. int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
  444. unsigned long arg)
  445. {
  446. int error = 0;
  447. int __user *argp = (int __user *)arg;
  448. switch (cmd) {
  449. case FIOCLEX:
  450. set_close_on_exec(fd, 1);
  451. break;
  452. case FIONCLEX:
  453. set_close_on_exec(fd, 0);
  454. break;
  455. case FIONBIO:
  456. error = ioctl_fionbio(filp, argp);
  457. break;
  458. case FIOASYNC:
  459. error = ioctl_fioasync(fd, filp, argp);
  460. break;
  461. case FIOQSIZE:
  462. if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
  463. S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
  464. S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
  465. loff_t res =
  466. inode_get_bytes(filp->f_path.dentry->d_inode);
  467. error = copy_to_user((loff_t __user *)arg, &res,
  468. sizeof(res)) ? -EFAULT : 0;
  469. } else
  470. error = -ENOTTY;
  471. break;
  472. case FIFREEZE:
  473. error = ioctl_fsfreeze(filp);
  474. break;
  475. case FITHAW:
  476. error = ioctl_fsthaw(filp);
  477. break;
  478. default:
  479. if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
  480. error = file_ioctl(filp, cmd, arg);
  481. else
  482. error = vfs_ioctl(filp, cmd, arg);
  483. break;
  484. }
  485. return error;
  486. }
  487. SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
  488. {
  489. struct file *filp;
  490. int error = -EBADF;
  491. int fput_needed;
  492. filp = fget_light(fd, &fput_needed);
  493. if (!filp)
  494. goto out;
  495. error = security_file_ioctl(filp, cmd, arg);
  496. if (error)
  497. goto out_fput;
  498. error = do_vfs_ioctl(filp, fd, cmd, arg);
  499. out_fput:
  500. fput_light(filp, fput_needed);
  501. out:
  502. return error;
  503. }