ioctl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. * @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. int generic_block_fiemap(struct inode *inode,
  212. struct fiemap_extent_info *fieinfo, u64 start,
  213. u64 len, get_block_t *get_block)
  214. {
  215. struct buffer_head tmp;
  216. unsigned int start_blk;
  217. long long length = 0, map_len = 0;
  218. u64 logical = 0, phys = 0, size = 0;
  219. u32 flags = FIEMAP_EXTENT_MERGED;
  220. int ret = 0;
  221. if ((ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC)))
  222. return ret;
  223. start_blk = logical_to_blk(inode, start);
  224. /* guard against change */
  225. mutex_lock(&inode->i_mutex);
  226. length = (long long)min_t(u64, len, i_size_read(inode));
  227. map_len = length;
  228. do {
  229. /*
  230. * we set b_size to the total size we want so it will map as
  231. * many contiguous blocks as possible at once
  232. */
  233. memset(&tmp, 0, sizeof(struct buffer_head));
  234. tmp.b_size = map_len;
  235. ret = get_block(inode, start_blk, &tmp, 0);
  236. if (ret)
  237. break;
  238. /* HOLE */
  239. if (!buffer_mapped(&tmp)) {
  240. /*
  241. * first hole after going past the EOF, this is our
  242. * last extent
  243. */
  244. if (length <= 0) {
  245. flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
  246. ret = fiemap_fill_next_extent(fieinfo, logical,
  247. phys, size,
  248. flags);
  249. break;
  250. }
  251. length -= blk_to_logical(inode, 1);
  252. /* if we have holes up to/past EOF then we're done */
  253. if (length <= 0)
  254. break;
  255. start_blk++;
  256. } else {
  257. if (length <= 0 && size) {
  258. ret = fiemap_fill_next_extent(fieinfo, logical,
  259. phys, size,
  260. flags);
  261. if (ret)
  262. break;
  263. }
  264. logical = blk_to_logical(inode, start_blk);
  265. phys = blk_to_logical(inode, tmp.b_blocknr);
  266. size = tmp.b_size;
  267. flags = FIEMAP_EXTENT_MERGED;
  268. length -= tmp.b_size;
  269. start_blk += logical_to_blk(inode, size);
  270. /*
  271. * if we are past the EOF we need to loop again to see
  272. * if there is a hole so we can mark this extent as the
  273. * last one, and if not keep mapping things until we
  274. * find a hole, or we run out of slots in the extent
  275. * array
  276. */
  277. if (length <= 0)
  278. continue;
  279. ret = fiemap_fill_next_extent(fieinfo, logical, phys,
  280. size, flags);
  281. if (ret)
  282. break;
  283. }
  284. cond_resched();
  285. } while (1);
  286. mutex_unlock(&inode->i_mutex);
  287. /* if ret is 1 then we just hit the end of the extent array */
  288. if (ret == 1)
  289. ret = 0;
  290. return ret;
  291. }
  292. EXPORT_SYMBOL(generic_block_fiemap);
  293. #endif /* CONFIG_BLOCK */
  294. static int file_ioctl(struct file *filp, unsigned int cmd,
  295. unsigned long arg)
  296. {
  297. struct inode *inode = filp->f_path.dentry->d_inode;
  298. int __user *p = (int __user *)arg;
  299. switch (cmd) {
  300. case FIBMAP:
  301. return ioctl_fibmap(filp, p);
  302. case FS_IOC_FIEMAP:
  303. return ioctl_fiemap(filp, arg);
  304. case FIGETBSZ:
  305. return put_user(inode->i_sb->s_blocksize, p);
  306. case FIONREAD:
  307. return put_user(i_size_read(inode) - filp->f_pos, p);
  308. }
  309. return vfs_ioctl(filp, cmd, arg);
  310. }
  311. static int ioctl_fionbio(struct file *filp, int __user *argp)
  312. {
  313. unsigned int flag;
  314. int on, error;
  315. error = get_user(on, argp);
  316. if (error)
  317. return error;
  318. flag = O_NONBLOCK;
  319. #ifdef __sparc__
  320. /* SunOS compatibility item. */
  321. if (O_NONBLOCK != O_NDELAY)
  322. flag |= O_NDELAY;
  323. #endif
  324. if (on)
  325. filp->f_flags |= flag;
  326. else
  327. filp->f_flags &= ~flag;
  328. return error;
  329. }
  330. static int ioctl_fioasync(unsigned int fd, struct file *filp,
  331. int __user *argp)
  332. {
  333. unsigned int flag;
  334. int on, error;
  335. error = get_user(on, argp);
  336. if (error)
  337. return error;
  338. flag = on ? FASYNC : 0;
  339. /* Did FASYNC state change ? */
  340. if ((flag ^ filp->f_flags) & FASYNC) {
  341. if (filp->f_op && filp->f_op->fasync) {
  342. lock_kernel();
  343. error = filp->f_op->fasync(fd, filp, on);
  344. unlock_kernel();
  345. } else
  346. error = -ENOTTY;
  347. }
  348. if (error)
  349. return error;
  350. if (on)
  351. filp->f_flags |= FASYNC;
  352. else
  353. filp->f_flags &= ~FASYNC;
  354. return error;
  355. }
  356. /*
  357. * When you add any new common ioctls to the switches above and below
  358. * please update compat_sys_ioctl() too.
  359. *
  360. * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
  361. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
  362. */
  363. int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
  364. unsigned long arg)
  365. {
  366. int error = 0;
  367. int __user *argp = (int __user *)arg;
  368. switch (cmd) {
  369. case FIOCLEX:
  370. set_close_on_exec(fd, 1);
  371. break;
  372. case FIONCLEX:
  373. set_close_on_exec(fd, 0);
  374. break;
  375. case FIONBIO:
  376. error = ioctl_fionbio(filp, argp);
  377. break;
  378. case FIOASYNC:
  379. error = ioctl_fioasync(fd, filp, argp);
  380. break;
  381. case FIOQSIZE:
  382. if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
  383. S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
  384. S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
  385. loff_t res =
  386. inode_get_bytes(filp->f_path.dentry->d_inode);
  387. error = copy_to_user((loff_t __user *)arg, &res,
  388. sizeof(res)) ? -EFAULT : 0;
  389. } else
  390. error = -ENOTTY;
  391. break;
  392. default:
  393. if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
  394. error = file_ioctl(filp, cmd, arg);
  395. else
  396. error = vfs_ioctl(filp, cmd, arg);
  397. break;
  398. }
  399. return error;
  400. }
  401. asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
  402. {
  403. struct file *filp;
  404. int error = -EBADF;
  405. int fput_needed;
  406. filp = fget_light(fd, &fput_needed);
  407. if (!filp)
  408. goto out;
  409. error = security_file_ioctl(filp, cmd, arg);
  410. if (error)
  411. goto out_fput;
  412. error = do_vfs_ioctl(filp, fd, cmd, arg);
  413. out_fput:
  414. fput_light(filp, fput_needed);
  415. out:
  416. return error;
  417. }