ioctl.c 16 KB

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