ioctl.c 15 KB

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