ioctl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. int ret;
  192. if (!src_file.file)
  193. return -EBADF;
  194. ret = -EXDEV;
  195. if (src_file.file->f_path.mnt != dst_file->f_path.mnt)
  196. goto fdput;
  197. ret = vfs_clone_file_range(src_file.file, off, dst_file, destoff, olen);
  198. fdput:
  199. fdput(src_file);
  200. return ret;
  201. }
  202. static long ioctl_file_clone_range(struct file *file, void __user *argp)
  203. {
  204. struct file_clone_range args;
  205. if (copy_from_user(&args, argp, sizeof(args)))
  206. return -EFAULT;
  207. return ioctl_file_clone(file, args.src_fd, args.src_offset,
  208. args.src_length, args.dest_offset);
  209. }
  210. #ifdef CONFIG_BLOCK
  211. static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
  212. {
  213. return (offset >> inode->i_blkbits);
  214. }
  215. static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
  216. {
  217. return (blk << inode->i_blkbits);
  218. }
  219. /**
  220. * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
  221. * @inode: the inode to map
  222. * @fieinfo: the fiemap info struct that will be passed back to userspace
  223. * @start: where to start mapping in the inode
  224. * @len: how much space to map
  225. * @get_block: the fs's get_block function
  226. *
  227. * This does FIEMAP for block based inodes. Basically it will just loop
  228. * through get_block until we hit the number of extents we want to map, or we
  229. * go past the end of the file and hit a hole.
  230. *
  231. * If it is possible to have data blocks beyond a hole past @inode->i_size, then
  232. * please do not use this function, it will stop at the first unmapped block
  233. * beyond i_size.
  234. *
  235. * If you use this function directly, you need to do your own locking. Use
  236. * generic_block_fiemap if you want the locking done for you.
  237. */
  238. int __generic_block_fiemap(struct inode *inode,
  239. struct fiemap_extent_info *fieinfo, loff_t start,
  240. loff_t len, get_block_t *get_block)
  241. {
  242. struct buffer_head map_bh;
  243. sector_t start_blk, last_blk;
  244. loff_t isize = i_size_read(inode);
  245. u64 logical = 0, phys = 0, size = 0;
  246. u32 flags = FIEMAP_EXTENT_MERGED;
  247. bool past_eof = false, whole_file = false;
  248. int ret = 0;
  249. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  250. if (ret)
  251. return ret;
  252. /*
  253. * Either the i_mutex or other appropriate locking needs to be held
  254. * since we expect isize to not change at all through the duration of
  255. * this call.
  256. */
  257. if (len >= isize) {
  258. whole_file = true;
  259. len = isize;
  260. }
  261. /*
  262. * Some filesystems can't deal with being asked to map less than
  263. * blocksize, so make sure our len is at least block length.
  264. */
  265. if (logical_to_blk(inode, len) == 0)
  266. len = blk_to_logical(inode, 1);
  267. start_blk = logical_to_blk(inode, start);
  268. last_blk = logical_to_blk(inode, start + len - 1);
  269. do {
  270. /*
  271. * we set b_size to the total size we want so it will map as
  272. * many contiguous blocks as possible at once
  273. */
  274. memset(&map_bh, 0, sizeof(struct buffer_head));
  275. map_bh.b_size = len;
  276. ret = get_block(inode, start_blk, &map_bh, 0);
  277. if (ret)
  278. break;
  279. /* HOLE */
  280. if (!buffer_mapped(&map_bh)) {
  281. start_blk++;
  282. /*
  283. * We want to handle the case where there is an
  284. * allocated block at the front of the file, and then
  285. * nothing but holes up to the end of the file properly,
  286. * to make sure that extent at the front gets properly
  287. * marked with FIEMAP_EXTENT_LAST
  288. */
  289. if (!past_eof &&
  290. blk_to_logical(inode, start_blk) >= isize)
  291. past_eof = 1;
  292. /*
  293. * First hole after going past the EOF, this is our
  294. * last extent
  295. */
  296. if (past_eof && size) {
  297. flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
  298. ret = fiemap_fill_next_extent(fieinfo, logical,
  299. phys, size,
  300. flags);
  301. } else if (size) {
  302. ret = fiemap_fill_next_extent(fieinfo, logical,
  303. phys, size, flags);
  304. size = 0;
  305. }
  306. /* if we have holes up to/past EOF then we're done */
  307. if (start_blk > last_blk || past_eof || ret)
  308. break;
  309. } else {
  310. /*
  311. * We have gone over the length of what we wanted to
  312. * map, and it wasn't the entire file, so add the extent
  313. * we got last time and exit.
  314. *
  315. * This is for the case where say we want to map all the
  316. * way up to the second to the last block in a file, but
  317. * the last block is a hole, making the second to last
  318. * block FIEMAP_EXTENT_LAST. In this case we want to
  319. * see if there is a hole after the second to last block
  320. * so we can mark it properly. If we found data after
  321. * we exceeded the length we were requesting, then we
  322. * are good to go, just add the extent to the fieinfo
  323. * and break
  324. */
  325. if (start_blk > last_blk && !whole_file) {
  326. ret = fiemap_fill_next_extent(fieinfo, logical,
  327. phys, size,
  328. flags);
  329. break;
  330. }
  331. /*
  332. * if size != 0 then we know we already have an extent
  333. * to add, so add it.
  334. */
  335. if (size) {
  336. ret = fiemap_fill_next_extent(fieinfo, logical,
  337. phys, size,
  338. flags);
  339. if (ret)
  340. break;
  341. }
  342. logical = blk_to_logical(inode, start_blk);
  343. phys = blk_to_logical(inode, map_bh.b_blocknr);
  344. size = map_bh.b_size;
  345. flags = FIEMAP_EXTENT_MERGED;
  346. start_blk += logical_to_blk(inode, size);
  347. /*
  348. * If we are past the EOF, then we need to make sure as
  349. * soon as we find a hole that the last extent we found
  350. * is marked with FIEMAP_EXTENT_LAST
  351. */
  352. if (!past_eof && logical + size >= isize)
  353. past_eof = true;
  354. }
  355. cond_resched();
  356. if (fatal_signal_pending(current)) {
  357. ret = -EINTR;
  358. break;
  359. }
  360. } while (1);
  361. /* If ret is 1 then we just hit the end of the extent array */
  362. if (ret == 1)
  363. ret = 0;
  364. return ret;
  365. }
  366. EXPORT_SYMBOL(__generic_block_fiemap);
  367. /**
  368. * generic_block_fiemap - FIEMAP for block based inodes
  369. * @inode: The inode to map
  370. * @fieinfo: The mapping information
  371. * @start: The initial block to map
  372. * @len: The length of the extect to attempt to map
  373. * @get_block: The block mapping function for the fs
  374. *
  375. * Calls __generic_block_fiemap to map the inode, after taking
  376. * the inode's mutex lock.
  377. */
  378. int generic_block_fiemap(struct inode *inode,
  379. struct fiemap_extent_info *fieinfo, u64 start,
  380. u64 len, get_block_t *get_block)
  381. {
  382. int ret;
  383. inode_lock(inode);
  384. ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
  385. inode_unlock(inode);
  386. return ret;
  387. }
  388. EXPORT_SYMBOL(generic_block_fiemap);
  389. #endif /* CONFIG_BLOCK */
  390. /*
  391. * This provides compatibility with legacy XFS pre-allocation ioctls
  392. * which predate the fallocate syscall.
  393. *
  394. * Only the l_start, l_len and l_whence fields of the 'struct space_resv'
  395. * are used here, rest are ignored.
  396. */
  397. int ioctl_preallocate(struct file *filp, void __user *argp)
  398. {
  399. struct inode *inode = file_inode(filp);
  400. struct space_resv sr;
  401. if (copy_from_user(&sr, argp, sizeof(sr)))
  402. return -EFAULT;
  403. switch (sr.l_whence) {
  404. case SEEK_SET:
  405. break;
  406. case SEEK_CUR:
  407. sr.l_start += filp->f_pos;
  408. break;
  409. case SEEK_END:
  410. sr.l_start += i_size_read(inode);
  411. break;
  412. default:
  413. return -EINVAL;
  414. }
  415. return vfs_fallocate(filp, FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len);
  416. }
  417. static int file_ioctl(struct file *filp, unsigned int cmd,
  418. unsigned long arg)
  419. {
  420. struct inode *inode = file_inode(filp);
  421. int __user *p = (int __user *)arg;
  422. switch (cmd) {
  423. case FIBMAP:
  424. return ioctl_fibmap(filp, p);
  425. case FIONREAD:
  426. return put_user(i_size_read(inode) - filp->f_pos, p);
  427. case FS_IOC_RESVSP:
  428. case FS_IOC_RESVSP64:
  429. return ioctl_preallocate(filp, p);
  430. }
  431. return vfs_ioctl(filp, cmd, arg);
  432. }
  433. static int ioctl_fionbio(struct file *filp, int __user *argp)
  434. {
  435. unsigned int flag;
  436. int on, error;
  437. error = get_user(on, argp);
  438. if (error)
  439. return error;
  440. flag = O_NONBLOCK;
  441. #ifdef __sparc__
  442. /* SunOS compatibility item. */
  443. if (O_NONBLOCK != O_NDELAY)
  444. flag |= O_NDELAY;
  445. #endif
  446. spin_lock(&filp->f_lock);
  447. if (on)
  448. filp->f_flags |= flag;
  449. else
  450. filp->f_flags &= ~flag;
  451. spin_unlock(&filp->f_lock);
  452. return error;
  453. }
  454. static int ioctl_fioasync(unsigned int fd, struct file *filp,
  455. int __user *argp)
  456. {
  457. unsigned int flag;
  458. int on, error;
  459. error = get_user(on, argp);
  460. if (error)
  461. return error;
  462. flag = on ? FASYNC : 0;
  463. /* Did FASYNC state change ? */
  464. if ((flag ^ filp->f_flags) & FASYNC) {
  465. if (filp->f_op->fasync)
  466. /* fasync() adjusts filp->f_flags */
  467. error = filp->f_op->fasync(fd, filp, on);
  468. else
  469. error = -ENOTTY;
  470. }
  471. return error < 0 ? error : 0;
  472. }
  473. static int ioctl_fsfreeze(struct file *filp)
  474. {
  475. struct super_block *sb = file_inode(filp)->i_sb;
  476. if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
  477. return -EPERM;
  478. /* If filesystem doesn't support freeze feature, return. */
  479. if (sb->s_op->freeze_fs == NULL && sb->s_op->freeze_super == NULL)
  480. return -EOPNOTSUPP;
  481. /* Freeze */
  482. if (sb->s_op->freeze_super)
  483. return sb->s_op->freeze_super(sb);
  484. return freeze_super(sb);
  485. }
  486. static int ioctl_fsthaw(struct file *filp)
  487. {
  488. struct super_block *sb = file_inode(filp)->i_sb;
  489. if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
  490. return -EPERM;
  491. /* Thaw */
  492. if (sb->s_op->thaw_super)
  493. return sb->s_op->thaw_super(sb);
  494. return thaw_super(sb);
  495. }
  496. static int ioctl_file_dedupe_range(struct file *file, void __user *arg)
  497. {
  498. struct file_dedupe_range __user *argp = arg;
  499. struct file_dedupe_range *same = NULL;
  500. int ret;
  501. unsigned long size;
  502. u16 count;
  503. if (get_user(count, &argp->dest_count)) {
  504. ret = -EFAULT;
  505. goto out;
  506. }
  507. size = offsetof(struct file_dedupe_range __user, info[count]);
  508. if (size > PAGE_SIZE) {
  509. ret = -ENOMEM;
  510. goto out;
  511. }
  512. same = memdup_user(argp, size);
  513. if (IS_ERR(same)) {
  514. ret = PTR_ERR(same);
  515. same = NULL;
  516. goto out;
  517. }
  518. same->dest_count = count;
  519. ret = vfs_dedupe_file_range(file, same);
  520. if (ret)
  521. goto out;
  522. ret = copy_to_user(argp, same, size);
  523. if (ret)
  524. ret = -EFAULT;
  525. out:
  526. kfree(same);
  527. return ret;
  528. }
  529. /*
  530. * When you add any new common ioctls to the switches above and below
  531. * please update compat_sys_ioctl() too.
  532. *
  533. * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
  534. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
  535. */
  536. int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
  537. unsigned long arg)
  538. {
  539. int error = 0;
  540. int __user *argp = (int __user *)arg;
  541. struct inode *inode = file_inode(filp);
  542. switch (cmd) {
  543. case FIOCLEX:
  544. set_close_on_exec(fd, 1);
  545. break;
  546. case FIONCLEX:
  547. set_close_on_exec(fd, 0);
  548. break;
  549. case FIONBIO:
  550. error = ioctl_fionbio(filp, argp);
  551. break;
  552. case FIOASYNC:
  553. error = ioctl_fioasync(fd, filp, argp);
  554. break;
  555. case FIOQSIZE:
  556. if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode) ||
  557. S_ISLNK(inode->i_mode)) {
  558. loff_t res = inode_get_bytes(inode);
  559. error = copy_to_user(argp, &res, sizeof(res)) ?
  560. -EFAULT : 0;
  561. } else
  562. error = -ENOTTY;
  563. break;
  564. case FIFREEZE:
  565. error = ioctl_fsfreeze(filp);
  566. break;
  567. case FITHAW:
  568. error = ioctl_fsthaw(filp);
  569. break;
  570. case FS_IOC_FIEMAP:
  571. return ioctl_fiemap(filp, arg);
  572. case FIGETBSZ:
  573. /* anon_bdev filesystems may not have a block size */
  574. if (!inode->i_sb->s_blocksize)
  575. return -EINVAL;
  576. return put_user(inode->i_sb->s_blocksize, argp);
  577. case FICLONE:
  578. return ioctl_file_clone(filp, arg, 0, 0, 0);
  579. case FICLONERANGE:
  580. return ioctl_file_clone_range(filp, argp);
  581. case FIDEDUPERANGE:
  582. return ioctl_file_dedupe_range(filp, argp);
  583. default:
  584. if (S_ISREG(inode->i_mode))
  585. error = file_ioctl(filp, cmd, arg);
  586. else
  587. error = vfs_ioctl(filp, cmd, arg);
  588. break;
  589. }
  590. return error;
  591. }
  592. int ksys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
  593. {
  594. int error;
  595. struct fd f = fdget(fd);
  596. if (!f.file)
  597. return -EBADF;
  598. error = security_file_ioctl(f.file, cmd, arg);
  599. if (!error)
  600. error = do_vfs_ioctl(f.file, fd, cmd, arg);
  601. fdput(f);
  602. return error;
  603. }
  604. SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
  605. {
  606. return ksys_ioctl(fd, cmd, arg);
  607. }