ioctl.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 <asm/ioctls.h>
  16. /* So that the fiemap access checks can't overflow on 32 bit machines. */
  17. #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
  18. /**
  19. * vfs_ioctl - call filesystem specific ioctl methods
  20. * @filp: open file to invoke ioctl method on
  21. * @cmd: ioctl command to execute
  22. * @arg: command-specific argument for ioctl
  23. *
  24. * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
  25. * invokes filesystem specific ->ioctl method. If neither method exists,
  26. * returns -ENOTTY.
  27. *
  28. * Returns 0 on success, -errno on error.
  29. */
  30. static long vfs_ioctl(struct file *filp, unsigned int cmd,
  31. unsigned long arg)
  32. {
  33. int error = -ENOTTY;
  34. if (!filp->f_op)
  35. goto out;
  36. if (filp->f_op->unlocked_ioctl) {
  37. error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
  38. if (error == -ENOIOCTLCMD)
  39. error = -EINVAL;
  40. goto out;
  41. } else if (filp->f_op->ioctl) {
  42. lock_kernel();
  43. error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
  44. filp, cmd, arg);
  45. unlock_kernel();
  46. }
  47. out:
  48. return error;
  49. }
  50. static int ioctl_fibmap(struct file *filp, int __user *p)
  51. {
  52. struct address_space *mapping = filp->f_mapping;
  53. int res, block;
  54. /* do we support this mess? */
  55. if (!mapping->a_ops->bmap)
  56. return -EINVAL;
  57. if (!capable(CAP_SYS_RAWIO))
  58. return -EPERM;
  59. res = get_user(block, p);
  60. if (res)
  61. return res;
  62. lock_kernel();
  63. res = mapping->a_ops->bmap(mapping, block);
  64. unlock_kernel();
  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. static int file_ioctl(struct file *filp, unsigned int cmd,
  194. unsigned long arg)
  195. {
  196. struct inode *inode = filp->f_path.dentry->d_inode;
  197. int __user *p = (int __user *)arg;
  198. switch (cmd) {
  199. case FIBMAP:
  200. return ioctl_fibmap(filp, p);
  201. case FS_IOC_FIEMAP:
  202. return ioctl_fiemap(filp, arg);
  203. case FIGETBSZ:
  204. return put_user(inode->i_sb->s_blocksize, p);
  205. case FIONREAD:
  206. return put_user(i_size_read(inode) - filp->f_pos, p);
  207. }
  208. return vfs_ioctl(filp, cmd, arg);
  209. }
  210. static int ioctl_fionbio(struct file *filp, int __user *argp)
  211. {
  212. unsigned int flag;
  213. int on, error;
  214. error = get_user(on, argp);
  215. if (error)
  216. return error;
  217. flag = O_NONBLOCK;
  218. #ifdef __sparc__
  219. /* SunOS compatibility item. */
  220. if (O_NONBLOCK != O_NDELAY)
  221. flag |= O_NDELAY;
  222. #endif
  223. if (on)
  224. filp->f_flags |= flag;
  225. else
  226. filp->f_flags &= ~flag;
  227. return error;
  228. }
  229. static int ioctl_fioasync(unsigned int fd, struct file *filp,
  230. int __user *argp)
  231. {
  232. unsigned int flag;
  233. int on, error;
  234. error = get_user(on, argp);
  235. if (error)
  236. return error;
  237. flag = on ? FASYNC : 0;
  238. /* Did FASYNC state change ? */
  239. if ((flag ^ filp->f_flags) & FASYNC) {
  240. if (filp->f_op && filp->f_op->fasync) {
  241. lock_kernel();
  242. error = filp->f_op->fasync(fd, filp, on);
  243. unlock_kernel();
  244. } else
  245. error = -ENOTTY;
  246. }
  247. if (error)
  248. return error;
  249. if (on)
  250. filp->f_flags |= FASYNC;
  251. else
  252. filp->f_flags &= ~FASYNC;
  253. return error;
  254. }
  255. /*
  256. * When you add any new common ioctls to the switches above and below
  257. * please update compat_sys_ioctl() too.
  258. *
  259. * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
  260. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
  261. */
  262. int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
  263. unsigned long arg)
  264. {
  265. int error = 0;
  266. int __user *argp = (int __user *)arg;
  267. switch (cmd) {
  268. case FIOCLEX:
  269. set_close_on_exec(fd, 1);
  270. break;
  271. case FIONCLEX:
  272. set_close_on_exec(fd, 0);
  273. break;
  274. case FIONBIO:
  275. error = ioctl_fionbio(filp, argp);
  276. break;
  277. case FIOASYNC:
  278. error = ioctl_fioasync(fd, filp, argp);
  279. break;
  280. case FIOQSIZE:
  281. if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
  282. S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
  283. S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
  284. loff_t res =
  285. inode_get_bytes(filp->f_path.dentry->d_inode);
  286. error = copy_to_user((loff_t __user *)arg, &res,
  287. sizeof(res)) ? -EFAULT : 0;
  288. } else
  289. error = -ENOTTY;
  290. break;
  291. default:
  292. if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
  293. error = file_ioctl(filp, cmd, arg);
  294. else
  295. error = vfs_ioctl(filp, cmd, arg);
  296. break;
  297. }
  298. return error;
  299. }
  300. asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
  301. {
  302. struct file *filp;
  303. int error = -EBADF;
  304. int fput_needed;
  305. filp = fget_light(fd, &fput_needed);
  306. if (!filp)
  307. goto out;
  308. error = security_file_ioctl(filp, cmd, arg);
  309. if (error)
  310. goto out_fput;
  311. error = do_vfs_ioctl(filp, fd, cmd, arg);
  312. out_fput:
  313. fput_light(filp, fput_needed);
  314. out:
  315. return error;
  316. }