ioctl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * linux/fs/ext4/ioctl.c
  3. *
  4. * Copyright (C) 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/jbd2.h>
  11. #include <linux/capability.h>
  12. #include <linux/time.h>
  13. #include <linux/compat.h>
  14. #include <linux/mount.h>
  15. #include <linux/file.h>
  16. #include <asm/uaccess.h>
  17. #include "ext4_jbd2.h"
  18. #include "ext4.h"
  19. long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  20. {
  21. struct inode *inode = filp->f_dentry->d_inode;
  22. struct ext4_inode_info *ei = EXT4_I(inode);
  23. unsigned int flags;
  24. ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
  25. switch (cmd) {
  26. case EXT4_IOC_GETFLAGS:
  27. ext4_get_inode_flags(ei);
  28. flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
  29. return put_user(flags, (int __user *) arg);
  30. case EXT4_IOC_SETFLAGS: {
  31. handle_t *handle = NULL;
  32. int err, migrate = 0;
  33. struct ext4_iloc iloc;
  34. unsigned int oldflags;
  35. unsigned int jflag;
  36. if (!inode_owner_or_capable(inode))
  37. return -EACCES;
  38. if (get_user(flags, (int __user *) arg))
  39. return -EFAULT;
  40. err = mnt_want_write(filp->f_path.mnt);
  41. if (err)
  42. return err;
  43. flags = ext4_mask_flags(inode->i_mode, flags);
  44. err = -EPERM;
  45. mutex_lock(&inode->i_mutex);
  46. /* Is it quota file? Do not allow user to mess with it */
  47. if (IS_NOQUOTA(inode))
  48. goto flags_out;
  49. oldflags = ei->i_flags;
  50. /* The JOURNAL_DATA flag is modifiable only by root */
  51. jflag = flags & EXT4_JOURNAL_DATA_FL;
  52. /*
  53. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  54. * the relevant capability.
  55. *
  56. * This test looks nicer. Thanks to Pauline Middelink
  57. */
  58. if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
  59. if (!capable(CAP_LINUX_IMMUTABLE))
  60. goto flags_out;
  61. }
  62. /*
  63. * The JOURNAL_DATA flag can only be changed by
  64. * the relevant capability.
  65. */
  66. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
  67. if (!capable(CAP_SYS_RESOURCE))
  68. goto flags_out;
  69. }
  70. if (oldflags & EXT4_EXTENTS_FL) {
  71. /* We don't support clearning extent flags */
  72. if (!(flags & EXT4_EXTENTS_FL)) {
  73. err = -EOPNOTSUPP;
  74. goto flags_out;
  75. }
  76. } else if (flags & EXT4_EXTENTS_FL) {
  77. /* migrate the file */
  78. migrate = 1;
  79. flags &= ~EXT4_EXTENTS_FL;
  80. }
  81. if (flags & EXT4_EOFBLOCKS_FL) {
  82. /* we don't support adding EOFBLOCKS flag */
  83. if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
  84. err = -EOPNOTSUPP;
  85. goto flags_out;
  86. }
  87. } else if (oldflags & EXT4_EOFBLOCKS_FL)
  88. ext4_truncate(inode);
  89. handle = ext4_journal_start(inode, 1);
  90. if (IS_ERR(handle)) {
  91. err = PTR_ERR(handle);
  92. goto flags_out;
  93. }
  94. if (IS_SYNC(inode))
  95. ext4_handle_sync(handle);
  96. err = ext4_reserve_inode_write(handle, inode, &iloc);
  97. if (err)
  98. goto flags_err;
  99. flags = flags & EXT4_FL_USER_MODIFIABLE;
  100. flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
  101. ei->i_flags = flags;
  102. ext4_set_inode_flags(inode);
  103. inode->i_ctime = ext4_current_time(inode);
  104. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  105. flags_err:
  106. ext4_journal_stop(handle);
  107. if (err)
  108. goto flags_out;
  109. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
  110. err = ext4_change_inode_journal_flag(inode, jflag);
  111. if (err)
  112. goto flags_out;
  113. if (migrate)
  114. err = ext4_ext_migrate(inode);
  115. flags_out:
  116. mutex_unlock(&inode->i_mutex);
  117. mnt_drop_write(filp->f_path.mnt);
  118. return err;
  119. }
  120. case EXT4_IOC_GETVERSION:
  121. case EXT4_IOC_GETVERSION_OLD:
  122. return put_user(inode->i_generation, (int __user *) arg);
  123. case EXT4_IOC_SETVERSION:
  124. case EXT4_IOC_SETVERSION_OLD: {
  125. handle_t *handle;
  126. struct ext4_iloc iloc;
  127. __u32 generation;
  128. int err;
  129. if (!inode_owner_or_capable(inode))
  130. return -EPERM;
  131. err = mnt_want_write(filp->f_path.mnt);
  132. if (err)
  133. return err;
  134. if (get_user(generation, (int __user *) arg)) {
  135. err = -EFAULT;
  136. goto setversion_out;
  137. }
  138. handle = ext4_journal_start(inode, 1);
  139. if (IS_ERR(handle)) {
  140. err = PTR_ERR(handle);
  141. goto setversion_out;
  142. }
  143. err = ext4_reserve_inode_write(handle, inode, &iloc);
  144. if (err == 0) {
  145. inode->i_ctime = ext4_current_time(inode);
  146. inode->i_generation = generation;
  147. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  148. }
  149. ext4_journal_stop(handle);
  150. setversion_out:
  151. mnt_drop_write(filp->f_path.mnt);
  152. return err;
  153. }
  154. #ifdef CONFIG_JBD2_DEBUG
  155. case EXT4_IOC_WAIT_FOR_READONLY:
  156. /*
  157. * This is racy - by the time we're woken up and running,
  158. * the superblock could be released. And the module could
  159. * have been unloaded. So sue me.
  160. *
  161. * Returns 1 if it slept, else zero.
  162. */
  163. {
  164. struct super_block *sb = inode->i_sb;
  165. DECLARE_WAITQUEUE(wait, current);
  166. int ret = 0;
  167. set_current_state(TASK_INTERRUPTIBLE);
  168. add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
  169. if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
  170. schedule();
  171. ret = 1;
  172. }
  173. remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
  174. return ret;
  175. }
  176. #endif
  177. case EXT4_IOC_GROUP_EXTEND: {
  178. ext4_fsblk_t n_blocks_count;
  179. struct super_block *sb = inode->i_sb;
  180. int err, err2=0;
  181. if (!capable(CAP_SYS_RESOURCE))
  182. return -EPERM;
  183. if (get_user(n_blocks_count, (__u32 __user *)arg))
  184. return -EFAULT;
  185. err = mnt_want_write(filp->f_path.mnt);
  186. if (err)
  187. return err;
  188. err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
  189. if (EXT4_SB(sb)->s_journal) {
  190. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  191. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  192. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  193. }
  194. if (err == 0)
  195. err = err2;
  196. mnt_drop_write(filp->f_path.mnt);
  197. return err;
  198. }
  199. case EXT4_IOC_MOVE_EXT: {
  200. struct move_extent me;
  201. struct file *donor_filp;
  202. int err;
  203. if (!(filp->f_mode & FMODE_READ) ||
  204. !(filp->f_mode & FMODE_WRITE))
  205. return -EBADF;
  206. if (copy_from_user(&me,
  207. (struct move_extent __user *)arg, sizeof(me)))
  208. return -EFAULT;
  209. me.moved_len = 0;
  210. donor_filp = fget(me.donor_fd);
  211. if (!donor_filp)
  212. return -EBADF;
  213. if (!(donor_filp->f_mode & FMODE_WRITE)) {
  214. err = -EBADF;
  215. goto mext_out;
  216. }
  217. err = mnt_want_write(filp->f_path.mnt);
  218. if (err)
  219. goto mext_out;
  220. err = ext4_move_extents(filp, donor_filp, me.orig_start,
  221. me.donor_start, me.len, &me.moved_len);
  222. mnt_drop_write(filp->f_path.mnt);
  223. if (me.moved_len > 0)
  224. file_remove_suid(donor_filp);
  225. if (copy_to_user((struct move_extent __user *)arg,
  226. &me, sizeof(me)))
  227. err = -EFAULT;
  228. mext_out:
  229. fput(donor_filp);
  230. return err;
  231. }
  232. case EXT4_IOC_GROUP_ADD: {
  233. struct ext4_new_group_data input;
  234. struct super_block *sb = inode->i_sb;
  235. int err, err2=0;
  236. if (!capable(CAP_SYS_RESOURCE))
  237. return -EPERM;
  238. if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
  239. sizeof(input)))
  240. return -EFAULT;
  241. err = mnt_want_write(filp->f_path.mnt);
  242. if (err)
  243. return err;
  244. err = ext4_group_add(sb, &input);
  245. if (EXT4_SB(sb)->s_journal) {
  246. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  247. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  248. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  249. }
  250. if (err == 0)
  251. err = err2;
  252. mnt_drop_write(filp->f_path.mnt);
  253. return err;
  254. }
  255. case EXT4_IOC_MIGRATE:
  256. {
  257. int err;
  258. if (!inode_owner_or_capable(inode))
  259. return -EACCES;
  260. err = mnt_want_write(filp->f_path.mnt);
  261. if (err)
  262. return err;
  263. /*
  264. * inode_mutex prevent write and truncate on the file.
  265. * Read still goes through. We take i_data_sem in
  266. * ext4_ext_swap_inode_data before we switch the
  267. * inode format to prevent read.
  268. */
  269. mutex_lock(&(inode->i_mutex));
  270. err = ext4_ext_migrate(inode);
  271. mutex_unlock(&(inode->i_mutex));
  272. mnt_drop_write(filp->f_path.mnt);
  273. return err;
  274. }
  275. case EXT4_IOC_ALLOC_DA_BLKS:
  276. {
  277. int err;
  278. if (!inode_owner_or_capable(inode))
  279. return -EACCES;
  280. err = mnt_want_write(filp->f_path.mnt);
  281. if (err)
  282. return err;
  283. err = ext4_alloc_da_blocks(inode);
  284. mnt_drop_write(filp->f_path.mnt);
  285. return err;
  286. }
  287. case FITRIM:
  288. {
  289. struct super_block *sb = inode->i_sb;
  290. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  291. struct fstrim_range range;
  292. int ret = 0;
  293. if (!capable(CAP_SYS_ADMIN))
  294. return -EPERM;
  295. if (!blk_queue_discard(q))
  296. return -EOPNOTSUPP;
  297. if (copy_from_user(&range, (struct fstrim_range *)arg,
  298. sizeof(range)))
  299. return -EFAULT;
  300. range.minlen = max((unsigned int)range.minlen,
  301. q->limits.discard_granularity);
  302. ret = ext4_trim_fs(sb, &range);
  303. if (ret < 0)
  304. return ret;
  305. if (copy_to_user((struct fstrim_range *)arg, &range,
  306. sizeof(range)))
  307. return -EFAULT;
  308. return 0;
  309. }
  310. default:
  311. return -ENOTTY;
  312. }
  313. }
  314. #ifdef CONFIG_COMPAT
  315. long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  316. {
  317. /* These are just misnamed, they actually get/put from/to user an int */
  318. switch (cmd) {
  319. case EXT4_IOC32_GETFLAGS:
  320. cmd = EXT4_IOC_GETFLAGS;
  321. break;
  322. case EXT4_IOC32_SETFLAGS:
  323. cmd = EXT4_IOC_SETFLAGS;
  324. break;
  325. case EXT4_IOC32_GETVERSION:
  326. cmd = EXT4_IOC_GETVERSION;
  327. break;
  328. case EXT4_IOC32_SETVERSION:
  329. cmd = EXT4_IOC_SETVERSION;
  330. break;
  331. case EXT4_IOC32_GROUP_EXTEND:
  332. cmd = EXT4_IOC_GROUP_EXTEND;
  333. break;
  334. case EXT4_IOC32_GETVERSION_OLD:
  335. cmd = EXT4_IOC_GETVERSION_OLD;
  336. break;
  337. case EXT4_IOC32_SETVERSION_OLD:
  338. cmd = EXT4_IOC_SETVERSION_OLD;
  339. break;
  340. #ifdef CONFIG_JBD2_DEBUG
  341. case EXT4_IOC32_WAIT_FOR_READONLY:
  342. cmd = EXT4_IOC_WAIT_FOR_READONLY;
  343. break;
  344. #endif
  345. case EXT4_IOC32_GETRSVSZ:
  346. cmd = EXT4_IOC_GETRSVSZ;
  347. break;
  348. case EXT4_IOC32_SETRSVSZ:
  349. cmd = EXT4_IOC_SETRSVSZ;
  350. break;
  351. case EXT4_IOC32_GROUP_ADD: {
  352. struct compat_ext4_new_group_input __user *uinput;
  353. struct ext4_new_group_input input;
  354. mm_segment_t old_fs;
  355. int err;
  356. uinput = compat_ptr(arg);
  357. err = get_user(input.group, &uinput->group);
  358. err |= get_user(input.block_bitmap, &uinput->block_bitmap);
  359. err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
  360. err |= get_user(input.inode_table, &uinput->inode_table);
  361. err |= get_user(input.blocks_count, &uinput->blocks_count);
  362. err |= get_user(input.reserved_blocks,
  363. &uinput->reserved_blocks);
  364. if (err)
  365. return -EFAULT;
  366. old_fs = get_fs();
  367. set_fs(KERNEL_DS);
  368. err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
  369. (unsigned long) &input);
  370. set_fs(old_fs);
  371. return err;
  372. }
  373. case EXT4_IOC_MOVE_EXT:
  374. case FITRIM:
  375. break;
  376. default:
  377. return -ENOIOCTLCMD;
  378. }
  379. return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  380. }
  381. #endif