ioctl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ioctl.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. #include <linux/syscalls.h>
  8. #include <linux/mm.h>
  9. #include <linux/capability.h>
  10. #include <linux/compat.h>
  11. #include <linux/file.h>
  12. #include <linux/fs.h>
  13. #include <linux/security.h>
  14. #include <linux/export.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/writeback.h>
  17. #include <linux/buffer_head.h>
  18. #include <linux/falloc.h>
  19. #include <linux/sched/signal.h>
  20. #include <linux/fiemap.h>
  21. #include "internal.h"
  22. #include <asm/ioctls.h>
  23. /* So that the fiemap access checks can't overflow on 32 bit machines. */
  24. #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
  25. /**
  26. * vfs_ioctl - call filesystem specific ioctl methods
  27. * @filp: open file to invoke ioctl method on
  28. * @cmd: ioctl command to execute
  29. * @arg: command-specific argument for ioctl
  30. *
  31. * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
  32. * returns -ENOTTY.
  33. *
  34. * Returns 0 on success, -errno on error.
  35. */
  36. long vfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  37. {
  38. int error = -ENOTTY;
  39. if (!filp->f_op->unlocked_ioctl)
  40. goto out;
  41. error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
  42. if (error == -ENOIOCTLCMD)
  43. error = -ENOTTY;
  44. out:
  45. return error;
  46. }
  47. EXPORT_SYMBOL(vfs_ioctl);
  48. static int ioctl_fibmap(struct file *filp, int __user *p)
  49. {
  50. struct inode *inode = file_inode(filp);
  51. struct super_block *sb = inode->i_sb;
  52. int error, ur_block;
  53. sector_t block;
  54. if (!capable(CAP_SYS_RAWIO))
  55. return -EPERM;
  56. error = get_user(ur_block, p);
  57. if (error)
  58. return error;
  59. if (ur_block < 0)
  60. return -EINVAL;
  61. block = ur_block;
  62. error = bmap(inode, &block);
  63. if (block > INT_MAX) {
  64. error = -ERANGE;
  65. pr_warn_ratelimited("[%s/%d] FS: %s File: %pD4 would truncate fibmap result\n",
  66. current->comm, task_pid_nr(current),
  67. sb->s_id, filp);
  68. }
  69. if (error)
  70. ur_block = 0;
  71. else
  72. ur_block = block;
  73. if (put_user(ur_block, p))
  74. error = -EFAULT;
  75. return error;
  76. }
  77. /**
  78. * fiemap_fill_next_extent - Fiemap helper function
  79. * @fieinfo: Fiemap context passed into ->fiemap
  80. * @logical: Extent logical start offset, in bytes
  81. * @phys: Extent physical start offset, in bytes
  82. * @len: Extent length, in bytes
  83. * @flags: FIEMAP_EXTENT flags that describe this extent
  84. *
  85. * Called from file system ->fiemap callback. Will populate extent
  86. * info as passed in via arguments and copy to user memory. On
  87. * success, extent count on fieinfo is incremented.
  88. *
  89. * Returns 0 on success, -errno on error, 1 if this was the last
  90. * extent that will fit in user array.
  91. */
  92. #define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC)
  93. #define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED)
  94. #define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE)
  95. int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
  96. u64 phys, u64 len, u32 flags)
  97. {
  98. struct fiemap_extent extent;
  99. struct fiemap_extent __user *dest = fieinfo->fi_extents_start;
  100. /* only count the extents */
  101. if (fieinfo->fi_extents_max == 0) {
  102. fieinfo->fi_extents_mapped++;
  103. return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
  104. }
  105. if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
  106. return 1;
  107. if (flags & SET_UNKNOWN_FLAGS)
  108. flags |= FIEMAP_EXTENT_UNKNOWN;
  109. if (flags & SET_NO_UNMOUNTED_IO_FLAGS)
  110. flags |= FIEMAP_EXTENT_ENCODED;
  111. if (flags & SET_NOT_ALIGNED_FLAGS)
  112. flags |= FIEMAP_EXTENT_NOT_ALIGNED;
  113. memset(&extent, 0, sizeof(extent));
  114. extent.fe_logical = logical;
  115. extent.fe_physical = phys;
  116. extent.fe_length = len;
  117. extent.fe_flags = flags;
  118. dest += fieinfo->fi_extents_mapped;
  119. if (copy_to_user(dest, &extent, sizeof(extent)))
  120. return -EFAULT;
  121. fieinfo->fi_extents_mapped++;
  122. if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
  123. return 1;
  124. return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
  125. }
  126. EXPORT_SYMBOL_NS(fiemap_fill_next_extent, ANDROID_GKI_VFS_EXPORT_ONLY);
  127. /**
  128. * fiemap_prep - check validity of requested flags for fiemap
  129. * @inode: Inode to operate on
  130. * @fieinfo: Fiemap context passed into ->fiemap
  131. * @start: Start of the mapped range
  132. * @len: Length of the mapped range, can be truncated by this function.
  133. * @supported_flags: Set of fiemap flags that the file system understands
  134. *
  135. * This function must be called from each ->fiemap instance to validate the
  136. * fiemap request against the file system parameters.
  137. *
  138. * Returns 0 on success, or a negative error on failure.
  139. */
  140. int fiemap_prep(struct inode *inode, struct fiemap_extent_info *fieinfo,
  141. u64 start, u64 *len, u32 supported_flags)
  142. {
  143. u64 maxbytes = inode->i_sb->s_maxbytes;
  144. u32 incompat_flags;
  145. int ret = 0;
  146. if (*len == 0)
  147. return -EINVAL;
  148. if (start > maxbytes)
  149. return -EFBIG;
  150. /*
  151. * Shrink request scope to what the fs can actually handle.
  152. */
  153. if (*len > maxbytes || (maxbytes - *len) < start)
  154. *len = maxbytes - start;
  155. supported_flags |= FIEMAP_FLAG_SYNC;
  156. supported_flags &= FIEMAP_FLAGS_COMPAT;
  157. incompat_flags = fieinfo->fi_flags & ~supported_flags;
  158. if (incompat_flags) {
  159. fieinfo->fi_flags = incompat_flags;
  160. return -EBADR;
  161. }
  162. if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC)
  163. ret = filemap_write_and_wait(inode->i_mapping);
  164. return ret;
  165. }
  166. EXPORT_SYMBOL_NS(fiemap_prep, ANDROID_GKI_VFS_EXPORT_ONLY);
  167. static int ioctl_fiemap(struct file *filp, struct fiemap __user *ufiemap)
  168. {
  169. struct fiemap fiemap;
  170. struct fiemap_extent_info fieinfo = { 0, };
  171. struct inode *inode = file_inode(filp);
  172. int error;
  173. if (!inode->i_op->fiemap)
  174. return -EOPNOTSUPP;
  175. if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap)))
  176. return -EFAULT;
  177. if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
  178. return -EINVAL;
  179. fieinfo.fi_flags = fiemap.fm_flags;
  180. fieinfo.fi_extents_max = fiemap.fm_extent_count;
  181. fieinfo.fi_extents_start = ufiemap->fm_extents;
  182. error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start,
  183. fiemap.fm_length);
  184. fiemap.fm_flags = fieinfo.fi_flags;
  185. fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
  186. if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap)))
  187. error = -EFAULT;
  188. return error;
  189. }
  190. static long ioctl_file_clone(struct file *dst_file, unsigned long srcfd,
  191. u64 off, u64 olen, u64 destoff)
  192. {
  193. struct fd src_file = fdget(srcfd);
  194. loff_t cloned;
  195. int ret;
  196. if (!src_file.file)
  197. return -EBADF;
  198. ret = -EXDEV;
  199. if (src_file.file->f_path.mnt != dst_file->f_path.mnt)
  200. goto fdput;
  201. cloned = vfs_clone_file_range(src_file.file, off, dst_file, destoff,
  202. olen, 0);
  203. if (cloned < 0)
  204. ret = cloned;
  205. else if (olen && cloned != olen)
  206. ret = -EINVAL;
  207. else
  208. ret = 0;
  209. fdput:
  210. fdput(src_file);
  211. return ret;
  212. }
  213. static long ioctl_file_clone_range(struct file *file,
  214. struct file_clone_range __user *argp)
  215. {
  216. struct file_clone_range args;
  217. if (copy_from_user(&args, argp, sizeof(args)))
  218. return -EFAULT;
  219. return ioctl_file_clone(file, args.src_fd, args.src_offset,
  220. args.src_length, args.dest_offset);
  221. }
  222. #ifdef CONFIG_BLOCK
  223. static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
  224. {
  225. return (offset >> inode->i_blkbits);
  226. }
  227. static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
  228. {
  229. return (blk << inode->i_blkbits);
  230. }
  231. /**
  232. * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
  233. * @inode: the inode to map
  234. * @fieinfo: the fiemap info struct that will be passed back to userspace
  235. * @start: where to start mapping in the inode
  236. * @len: how much space to map
  237. * @get_block: the fs's get_block function
  238. *
  239. * This does FIEMAP for block based inodes. Basically it will just loop
  240. * through get_block until we hit the number of extents we want to map, or we
  241. * go past the end of the file and hit a hole.
  242. *
  243. * If it is possible to have data blocks beyond a hole past @inode->i_size, then
  244. * please do not use this function, it will stop at the first unmapped block
  245. * beyond i_size.
  246. *
  247. * If you use this function directly, you need to do your own locking. Use
  248. * generic_block_fiemap if you want the locking done for you.
  249. */
  250. static int __generic_block_fiemap(struct inode *inode,
  251. struct fiemap_extent_info *fieinfo, loff_t start,
  252. loff_t len, get_block_t *get_block)
  253. {
  254. struct buffer_head map_bh;
  255. sector_t start_blk, last_blk;
  256. loff_t isize = i_size_read(inode);
  257. u64 logical = 0, phys = 0, size = 0;
  258. u32 flags = FIEMAP_EXTENT_MERGED;
  259. bool past_eof = false, whole_file = false;
  260. int ret = 0;
  261. ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_SYNC);
  262. if (ret)
  263. return ret;
  264. /*
  265. * Either the i_mutex or other appropriate locking needs to be held
  266. * since we expect isize to not change at all through the duration of
  267. * this call.
  268. */
  269. if (len >= isize) {
  270. whole_file = true;
  271. len = isize;
  272. }
  273. /*
  274. * Some filesystems can't deal with being asked to map less than
  275. * blocksize, so make sure our len is at least block length.
  276. */
  277. if (logical_to_blk(inode, len) == 0)
  278. len = blk_to_logical(inode, 1);
  279. start_blk = logical_to_blk(inode, start);
  280. last_blk = logical_to_blk(inode, start + len - 1);
  281. do {
  282. /*
  283. * we set b_size to the total size we want so it will map as
  284. * many contiguous blocks as possible at once
  285. */
  286. memset(&map_bh, 0, sizeof(struct buffer_head));
  287. map_bh.b_size = len;
  288. ret = get_block(inode, start_blk, &map_bh, 0);
  289. if (ret)
  290. break;
  291. /* HOLE */
  292. if (!buffer_mapped(&map_bh)) {
  293. start_blk++;
  294. /*
  295. * We want to handle the case where there is an
  296. * allocated block at the front of the file, and then
  297. * nothing but holes up to the end of the file properly,
  298. * to make sure that extent at the front gets properly
  299. * marked with FIEMAP_EXTENT_LAST
  300. */
  301. if (!past_eof &&
  302. blk_to_logical(inode, start_blk) >= isize)
  303. past_eof = 1;
  304. /*
  305. * First hole after going past the EOF, this is our
  306. * last extent
  307. */
  308. if (past_eof && size) {
  309. flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
  310. ret = fiemap_fill_next_extent(fieinfo, logical,
  311. phys, size,
  312. flags);
  313. } else if (size) {
  314. ret = fiemap_fill_next_extent(fieinfo, logical,
  315. phys, size, flags);
  316. size = 0;
  317. }
  318. /* if we have holes up to/past EOF then we're done */
  319. if (start_blk > last_blk || past_eof || ret)
  320. break;
  321. } else {
  322. /*
  323. * We have gone over the length of what we wanted to
  324. * map, and it wasn't the entire file, so add the extent
  325. * we got last time and exit.
  326. *
  327. * This is for the case where say we want to map all the
  328. * way up to the second to the last block in a file, but
  329. * the last block is a hole, making the second to last
  330. * block FIEMAP_EXTENT_LAST. In this case we want to
  331. * see if there is a hole after the second to last block
  332. * so we can mark it properly. If we found data after
  333. * we exceeded the length we were requesting, then we
  334. * are good to go, just add the extent to the fieinfo
  335. * and break
  336. */
  337. if (start_blk > last_blk && !whole_file) {
  338. ret = fiemap_fill_next_extent(fieinfo, logical,
  339. phys, size,
  340. flags);
  341. break;
  342. }
  343. /*
  344. * if size != 0 then we know we already have an extent
  345. * to add, so add it.
  346. */
  347. if (size) {
  348. ret = fiemap_fill_next_extent(fieinfo, logical,
  349. phys, size,
  350. flags);
  351. if (ret)
  352. break;
  353. }
  354. logical = blk_to_logical(inode, start_blk);
  355. phys = blk_to_logical(inode, map_bh.b_blocknr);
  356. size = map_bh.b_size;
  357. flags = FIEMAP_EXTENT_MERGED;
  358. start_blk += logical_to_blk(inode, size);
  359. /*
  360. * If we are past the EOF, then we need to make sure as
  361. * soon as we find a hole that the last extent we found
  362. * is marked with FIEMAP_EXTENT_LAST
  363. */
  364. if (!past_eof && logical + size >= isize)
  365. past_eof = true;
  366. }
  367. cond_resched();
  368. if (fatal_signal_pending(current)) {
  369. ret = -EINTR;
  370. break;
  371. }
  372. } while (1);
  373. /* If ret is 1 then we just hit the end of the extent array */
  374. if (ret == 1)
  375. ret = 0;
  376. return ret;
  377. }
  378. /**
  379. * generic_block_fiemap - FIEMAP for block based inodes
  380. * @inode: The inode to map
  381. * @fieinfo: The mapping information
  382. * @start: The initial block to map
  383. * @len: The length of the extect to attempt to map
  384. * @get_block: The block mapping function for the fs
  385. *
  386. * Calls __generic_block_fiemap to map the inode, after taking
  387. * the inode's mutex lock.
  388. */
  389. int generic_block_fiemap(struct inode *inode,
  390. struct fiemap_extent_info *fieinfo, u64 start,
  391. u64 len, get_block_t *get_block)
  392. {
  393. int ret;
  394. inode_lock(inode);
  395. ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
  396. inode_unlock(inode);
  397. return ret;
  398. }
  399. EXPORT_SYMBOL(generic_block_fiemap);
  400. #endif /* CONFIG_BLOCK */
  401. /*
  402. * This provides compatibility with legacy XFS pre-allocation ioctls
  403. * which predate the fallocate syscall.
  404. *
  405. * Only the l_start, l_len and l_whence fields of the 'struct space_resv'
  406. * are used here, rest are ignored.
  407. */
  408. static int ioctl_preallocate(struct file *filp, int mode, void __user *argp)
  409. {
  410. struct inode *inode = file_inode(filp);
  411. struct space_resv sr;
  412. if (copy_from_user(&sr, argp, sizeof(sr)))
  413. return -EFAULT;
  414. switch (sr.l_whence) {
  415. case SEEK_SET:
  416. break;
  417. case SEEK_CUR:
  418. sr.l_start += filp->f_pos;
  419. break;
  420. case SEEK_END:
  421. sr.l_start += i_size_read(inode);
  422. break;
  423. default:
  424. return -EINVAL;
  425. }
  426. return vfs_fallocate(filp, mode | FALLOC_FL_KEEP_SIZE, sr.l_start,
  427. sr.l_len);
  428. }
  429. /* on ia32 l_start is on a 32-bit boundary */
  430. #if defined CONFIG_COMPAT && defined(CONFIG_X86_64)
  431. /* just account for different alignment */
  432. static int compat_ioctl_preallocate(struct file *file, int mode,
  433. struct space_resv_32 __user *argp)
  434. {
  435. struct inode *inode = file_inode(file);
  436. struct space_resv_32 sr;
  437. if (copy_from_user(&sr, argp, sizeof(sr)))
  438. return -EFAULT;
  439. switch (sr.l_whence) {
  440. case SEEK_SET:
  441. break;
  442. case SEEK_CUR:
  443. sr.l_start += file->f_pos;
  444. break;
  445. case SEEK_END:
  446. sr.l_start += i_size_read(inode);
  447. break;
  448. default:
  449. return -EINVAL;
  450. }
  451. return vfs_fallocate(file, mode | FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len);
  452. }
  453. #endif
  454. static int file_ioctl(struct file *filp, unsigned int cmd, int __user *p)
  455. {
  456. switch (cmd) {
  457. case FIBMAP:
  458. return ioctl_fibmap(filp, p);
  459. case FS_IOC_RESVSP:
  460. case FS_IOC_RESVSP64:
  461. return ioctl_preallocate(filp, 0, p);
  462. case FS_IOC_UNRESVSP:
  463. case FS_IOC_UNRESVSP64:
  464. return ioctl_preallocate(filp, FALLOC_FL_PUNCH_HOLE, p);
  465. case FS_IOC_ZERO_RANGE:
  466. return ioctl_preallocate(filp, FALLOC_FL_ZERO_RANGE, p);
  467. }
  468. return -ENOIOCTLCMD;
  469. }
  470. static int ioctl_fionbio(struct file *filp, int __user *argp)
  471. {
  472. unsigned int flag;
  473. int on, error;
  474. error = get_user(on, argp);
  475. if (error)
  476. return error;
  477. flag = O_NONBLOCK;
  478. #ifdef __sparc__
  479. /* SunOS compatibility item. */
  480. if (O_NONBLOCK != O_NDELAY)
  481. flag |= O_NDELAY;
  482. #endif
  483. spin_lock(&filp->f_lock);
  484. if (on)
  485. filp->f_flags |= flag;
  486. else
  487. filp->f_flags &= ~flag;
  488. spin_unlock(&filp->f_lock);
  489. return error;
  490. }
  491. static int ioctl_fioasync(unsigned int fd, struct file *filp,
  492. int __user *argp)
  493. {
  494. unsigned int flag;
  495. int on, error;
  496. error = get_user(on, argp);
  497. if (error)
  498. return error;
  499. flag = on ? FASYNC : 0;
  500. /* Did FASYNC state change ? */
  501. if ((flag ^ filp->f_flags) & FASYNC) {
  502. if (filp->f_op->fasync)
  503. /* fasync() adjusts filp->f_flags */
  504. error = filp->f_op->fasync(fd, filp, on);
  505. else
  506. error = -ENOTTY;
  507. }
  508. return error < 0 ? error : 0;
  509. }
  510. static int ioctl_fsfreeze(struct file *filp)
  511. {
  512. struct super_block *sb = file_inode(filp)->i_sb;
  513. if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
  514. return -EPERM;
  515. /* If filesystem doesn't support freeze feature, return. */
  516. if (sb->s_op->freeze_fs == NULL && sb->s_op->freeze_super == NULL)
  517. return -EOPNOTSUPP;
  518. /* Freeze */
  519. if (sb->s_op->freeze_super)
  520. return sb->s_op->freeze_super(sb);
  521. return freeze_super(sb);
  522. }
  523. static int ioctl_fsthaw(struct file *filp)
  524. {
  525. struct super_block *sb = file_inode(filp)->i_sb;
  526. if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
  527. return -EPERM;
  528. /* Thaw */
  529. if (sb->s_op->thaw_super)
  530. return sb->s_op->thaw_super(sb);
  531. return thaw_super(sb);
  532. }
  533. static int ioctl_file_dedupe_range(struct file *file,
  534. struct file_dedupe_range __user *argp)
  535. {
  536. struct file_dedupe_range *same = NULL;
  537. int ret;
  538. unsigned long size;
  539. u16 count;
  540. if (get_user(count, &argp->dest_count)) {
  541. ret = -EFAULT;
  542. goto out;
  543. }
  544. size = offsetof(struct file_dedupe_range __user, info[count]);
  545. if (size > PAGE_SIZE) {
  546. ret = -ENOMEM;
  547. goto out;
  548. }
  549. same = memdup_user(argp, size);
  550. if (IS_ERR(same)) {
  551. ret = PTR_ERR(same);
  552. same = NULL;
  553. goto out;
  554. }
  555. same->dest_count = count;
  556. ret = vfs_dedupe_file_range(file, same);
  557. if (ret)
  558. goto out;
  559. ret = copy_to_user(argp, same, size);
  560. if (ret)
  561. ret = -EFAULT;
  562. out:
  563. kfree(same);
  564. return ret;
  565. }
  566. /*
  567. * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
  568. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
  569. *
  570. * When you add any new common ioctls to the switches above and below,
  571. * please ensure they have compatible arguments in compat mode.
  572. */
  573. static int do_vfs_ioctl(struct file *filp, unsigned int fd,
  574. unsigned int cmd, unsigned long arg)
  575. {
  576. void __user *argp = (void __user *)arg;
  577. struct inode *inode = file_inode(filp);
  578. switch (cmd) {
  579. case FIOCLEX:
  580. set_close_on_exec(fd, 1);
  581. return 0;
  582. case FIONCLEX:
  583. set_close_on_exec(fd, 0);
  584. return 0;
  585. case FIONBIO:
  586. return ioctl_fionbio(filp, argp);
  587. case FIOASYNC:
  588. return ioctl_fioasync(fd, filp, argp);
  589. case FIOQSIZE:
  590. if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode) ||
  591. S_ISLNK(inode->i_mode)) {
  592. loff_t res = inode_get_bytes(inode);
  593. return copy_to_user(argp, &res, sizeof(res)) ?
  594. -EFAULT : 0;
  595. }
  596. return -ENOTTY;
  597. case FIFREEZE:
  598. return ioctl_fsfreeze(filp);
  599. case FITHAW:
  600. return ioctl_fsthaw(filp);
  601. case FS_IOC_FIEMAP:
  602. return ioctl_fiemap(filp, argp);
  603. case FIGETBSZ:
  604. /* anon_bdev filesystems may not have a block size */
  605. if (!inode->i_sb->s_blocksize)
  606. return -EINVAL;
  607. return put_user(inode->i_sb->s_blocksize, (int __user *)argp);
  608. case FICLONE:
  609. return ioctl_file_clone(filp, arg, 0, 0, 0);
  610. case FICLONERANGE:
  611. return ioctl_file_clone_range(filp, argp);
  612. case FIDEDUPERANGE:
  613. return ioctl_file_dedupe_range(filp, argp);
  614. case FIONREAD:
  615. if (!S_ISREG(inode->i_mode))
  616. return vfs_ioctl(filp, cmd, arg);
  617. return put_user(i_size_read(inode) - filp->f_pos,
  618. (int __user *)argp);
  619. default:
  620. if (S_ISREG(inode->i_mode))
  621. return file_ioctl(filp, cmd, argp);
  622. break;
  623. }
  624. return -ENOIOCTLCMD;
  625. }
  626. SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
  627. {
  628. struct fd f = fdget(fd);
  629. int error;
  630. if (!f.file)
  631. return -EBADF;
  632. error = security_file_ioctl(f.file, cmd, arg);
  633. if (error)
  634. goto out;
  635. error = do_vfs_ioctl(f.file, fd, cmd, arg);
  636. if (error == -ENOIOCTLCMD)
  637. error = vfs_ioctl(f.file, cmd, arg);
  638. out:
  639. fdput(f);
  640. return error;
  641. }
  642. #ifdef CONFIG_COMPAT
  643. /**
  644. * compat_ptr_ioctl - generic implementation of .compat_ioctl file operation
  645. *
  646. * This is not normally called as a function, but instead set in struct
  647. * file_operations as
  648. *
  649. * .compat_ioctl = compat_ptr_ioctl,
  650. *
  651. * On most architectures, the compat_ptr_ioctl() just passes all arguments
  652. * to the corresponding ->ioctl handler. The exception is arch/s390, where
  653. * compat_ptr() clears the top bit of a 32-bit pointer value, so user space
  654. * pointers to the second 2GB alias the first 2GB, as is the case for
  655. * native 32-bit s390 user space.
  656. *
  657. * The compat_ptr_ioctl() function must therefore be used only with ioctl
  658. * functions that either ignore the argument or pass a pointer to a
  659. * compatible data type.
  660. *
  661. * If any ioctl command handled by fops->unlocked_ioctl passes a plain
  662. * integer instead of a pointer, or any of the passed data types
  663. * is incompatible between 32-bit and 64-bit architectures, a proper
  664. * handler is required instead of compat_ptr_ioctl.
  665. */
  666. long compat_ptr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  667. {
  668. if (!file->f_op->unlocked_ioctl)
  669. return -ENOIOCTLCMD;
  670. return file->f_op->unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  671. }
  672. EXPORT_SYMBOL(compat_ptr_ioctl);
  673. COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
  674. compat_ulong_t, arg)
  675. {
  676. struct fd f = fdget(fd);
  677. int error;
  678. if (!f.file)
  679. return -EBADF;
  680. /* RED-PEN how should LSM module know it's handling 32bit? */
  681. error = security_file_ioctl(f.file, cmd, arg);
  682. if (error)
  683. goto out;
  684. switch (cmd) {
  685. /* FICLONE takes an int argument, so don't use compat_ptr() */
  686. case FICLONE:
  687. error = ioctl_file_clone(f.file, arg, 0, 0, 0);
  688. break;
  689. #if defined(CONFIG_X86_64)
  690. /* these get messy on amd64 due to alignment differences */
  691. case FS_IOC_RESVSP_32:
  692. case FS_IOC_RESVSP64_32:
  693. error = compat_ioctl_preallocate(f.file, 0, compat_ptr(arg));
  694. break;
  695. case FS_IOC_UNRESVSP_32:
  696. case FS_IOC_UNRESVSP64_32:
  697. error = compat_ioctl_preallocate(f.file, FALLOC_FL_PUNCH_HOLE,
  698. compat_ptr(arg));
  699. break;
  700. case FS_IOC_ZERO_RANGE_32:
  701. error = compat_ioctl_preallocate(f.file, FALLOC_FL_ZERO_RANGE,
  702. compat_ptr(arg));
  703. break;
  704. #endif
  705. /*
  706. * everything else in do_vfs_ioctl() takes either a compatible
  707. * pointer argument or no argument -- call it with a modified
  708. * argument.
  709. */
  710. default:
  711. error = do_vfs_ioctl(f.file, fd, cmd,
  712. (unsigned long)compat_ptr(arg));
  713. if (error != -ENOIOCTLCMD)
  714. break;
  715. if (f.file->f_op->compat_ioctl)
  716. error = f.file->f_op->compat_ioctl(f.file, cmd, arg);
  717. if (error == -ENOIOCTLCMD)
  718. error = -ENOTTY;
  719. break;
  720. }
  721. out:
  722. fdput(f);
  723. return error;
  724. }
  725. #endif