ioctl.c 18 KB

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