ioctl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. #include "ext4_extents.h"
  20. #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
  21. /**
  22. * Swap memory between @a and @b for @len bytes.
  23. *
  24. * @a: pointer to first memory area
  25. * @b: pointer to second memory area
  26. * @len: number of bytes to swap
  27. *
  28. */
  29. static void memswap(void *a, void *b, size_t len)
  30. {
  31. unsigned char *ap, *bp;
  32. unsigned char tmp;
  33. ap = (unsigned char *)a;
  34. bp = (unsigned char *)b;
  35. while (len-- > 0) {
  36. tmp = *ap;
  37. *ap = *bp;
  38. *bp = tmp;
  39. ap++;
  40. bp++;
  41. }
  42. }
  43. /**
  44. * Swap i_data and associated attributes between @inode1 and @inode2.
  45. * This function is used for the primary swap between inode1 and inode2
  46. * and also to revert this primary swap in case of errors.
  47. *
  48. * Therefore you have to make sure, that calling this method twice
  49. * will revert all changes.
  50. *
  51. * @inode1: pointer to first inode
  52. * @inode2: pointer to second inode
  53. */
  54. static void swap_inode_data(struct inode *inode1, struct inode *inode2)
  55. {
  56. loff_t isize;
  57. struct ext4_inode_info *ei1;
  58. struct ext4_inode_info *ei2;
  59. ei1 = EXT4_I(inode1);
  60. ei2 = EXT4_I(inode2);
  61. memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags));
  62. memswap(&inode1->i_version, &inode2->i_version,
  63. sizeof(inode1->i_version));
  64. memswap(&inode1->i_blocks, &inode2->i_blocks,
  65. sizeof(inode1->i_blocks));
  66. memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes));
  67. memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime));
  68. memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime));
  69. memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
  70. memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags));
  71. memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize));
  72. memswap(&ei1->i_es_tree, &ei2->i_es_tree, sizeof(ei1->i_es_tree));
  73. memswap(&ei1->i_es_lru_nr, &ei2->i_es_lru_nr, sizeof(ei1->i_es_lru_nr));
  74. isize = i_size_read(inode1);
  75. i_size_write(inode1, i_size_read(inode2));
  76. i_size_write(inode2, isize);
  77. }
  78. /**
  79. * Swap the information from the given @inode and the inode
  80. * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
  81. * important fields of the inodes.
  82. *
  83. * @sb: the super block of the filesystem
  84. * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
  85. *
  86. */
  87. static long swap_inode_boot_loader(struct super_block *sb,
  88. struct inode *inode)
  89. {
  90. handle_t *handle;
  91. int err;
  92. struct inode *inode_bl;
  93. struct ext4_inode_info *ei;
  94. struct ext4_inode_info *ei_bl;
  95. struct ext4_sb_info *sbi;
  96. if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode)) {
  97. err = -EINVAL;
  98. goto swap_boot_out;
  99. }
  100. if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) {
  101. err = -EPERM;
  102. goto swap_boot_out;
  103. }
  104. sbi = EXT4_SB(sb);
  105. ei = EXT4_I(inode);
  106. inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
  107. if (IS_ERR(inode_bl)) {
  108. err = PTR_ERR(inode_bl);
  109. goto swap_boot_out;
  110. }
  111. ei_bl = EXT4_I(inode_bl);
  112. filemap_flush(inode->i_mapping);
  113. filemap_flush(inode_bl->i_mapping);
  114. /* Protect orig inodes against a truncate and make sure,
  115. * that only 1 swap_inode_boot_loader is running. */
  116. ext4_inode_double_lock(inode, inode_bl);
  117. truncate_inode_pages(&inode->i_data, 0);
  118. truncate_inode_pages(&inode_bl->i_data, 0);
  119. /* Wait for all existing dio workers */
  120. ext4_inode_block_unlocked_dio(inode);
  121. ext4_inode_block_unlocked_dio(inode_bl);
  122. inode_dio_wait(inode);
  123. inode_dio_wait(inode_bl);
  124. handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
  125. if (IS_ERR(handle)) {
  126. err = -EINVAL;
  127. goto swap_boot_out;
  128. }
  129. /* Protect extent tree against block allocations via delalloc */
  130. ext4_double_down_write_data_sem(inode, inode_bl);
  131. if (inode_bl->i_nlink == 0) {
  132. /* this inode has never been used as a BOOT_LOADER */
  133. set_nlink(inode_bl, 1);
  134. i_uid_write(inode_bl, 0);
  135. i_gid_write(inode_bl, 0);
  136. inode_bl->i_flags = 0;
  137. ei_bl->i_flags = 0;
  138. inode_bl->i_version = 1;
  139. i_size_write(inode_bl, 0);
  140. inode_bl->i_mode = S_IFREG;
  141. if (EXT4_HAS_INCOMPAT_FEATURE(sb,
  142. EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  143. ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
  144. ext4_ext_tree_init(handle, inode_bl);
  145. } else
  146. memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
  147. }
  148. swap_inode_data(inode, inode_bl);
  149. inode->i_ctime = inode_bl->i_ctime = ext4_current_time(inode);
  150. spin_lock(&sbi->s_next_gen_lock);
  151. inode->i_generation = sbi->s_next_generation++;
  152. inode_bl->i_generation = sbi->s_next_generation++;
  153. spin_unlock(&sbi->s_next_gen_lock);
  154. ext4_discard_preallocations(inode);
  155. err = ext4_mark_inode_dirty(handle, inode);
  156. if (err < 0) {
  157. ext4_warning(inode->i_sb,
  158. "couldn't mark inode #%lu dirty (err %d)",
  159. inode->i_ino, err);
  160. /* Revert all changes: */
  161. swap_inode_data(inode, inode_bl);
  162. } else {
  163. err = ext4_mark_inode_dirty(handle, inode_bl);
  164. if (err < 0) {
  165. ext4_warning(inode_bl->i_sb,
  166. "couldn't mark inode #%lu dirty (err %d)",
  167. inode_bl->i_ino, err);
  168. /* Revert all changes: */
  169. swap_inode_data(inode, inode_bl);
  170. ext4_mark_inode_dirty(handle, inode);
  171. }
  172. }
  173. ext4_journal_stop(handle);
  174. ext4_double_up_write_data_sem(inode, inode_bl);
  175. ext4_inode_resume_unlocked_dio(inode);
  176. ext4_inode_resume_unlocked_dio(inode_bl);
  177. ext4_inode_double_unlock(inode, inode_bl);
  178. iput(inode_bl);
  179. swap_boot_out:
  180. return err;
  181. }
  182. long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  183. {
  184. struct inode *inode = file_inode(filp);
  185. struct super_block *sb = inode->i_sb;
  186. struct ext4_inode_info *ei = EXT4_I(inode);
  187. unsigned int flags;
  188. ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
  189. switch (cmd) {
  190. case EXT4_IOC_GETFLAGS:
  191. ext4_get_inode_flags(ei);
  192. flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
  193. return put_user(flags, (int __user *) arg);
  194. case EXT4_IOC_SETFLAGS: {
  195. handle_t *handle = NULL;
  196. int err, migrate = 0;
  197. struct ext4_iloc iloc;
  198. unsigned int oldflags, mask, i;
  199. unsigned int jflag;
  200. if (!inode_owner_or_capable(inode))
  201. return -EACCES;
  202. if (get_user(flags, (int __user *) arg))
  203. return -EFAULT;
  204. err = mnt_want_write_file(filp);
  205. if (err)
  206. return err;
  207. flags = ext4_mask_flags(inode->i_mode, flags);
  208. err = -EPERM;
  209. mutex_lock(&inode->i_mutex);
  210. /* Is it quota file? Do not allow user to mess with it */
  211. if (IS_NOQUOTA(inode))
  212. goto flags_out;
  213. oldflags = ei->i_flags;
  214. /* The JOURNAL_DATA flag is modifiable only by root */
  215. jflag = flags & EXT4_JOURNAL_DATA_FL;
  216. /*
  217. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  218. * the relevant capability.
  219. *
  220. * This test looks nicer. Thanks to Pauline Middelink
  221. */
  222. if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
  223. if (!capable(CAP_LINUX_IMMUTABLE))
  224. goto flags_out;
  225. }
  226. /*
  227. * The JOURNAL_DATA flag can only be changed by
  228. * the relevant capability.
  229. */
  230. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
  231. if (!capable(CAP_SYS_RESOURCE))
  232. goto flags_out;
  233. }
  234. if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
  235. migrate = 1;
  236. if (flags & EXT4_EOFBLOCKS_FL) {
  237. /* we don't support adding EOFBLOCKS flag */
  238. if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
  239. err = -EOPNOTSUPP;
  240. goto flags_out;
  241. }
  242. } else if (oldflags & EXT4_EOFBLOCKS_FL)
  243. ext4_truncate(inode);
  244. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  245. if (IS_ERR(handle)) {
  246. err = PTR_ERR(handle);
  247. goto flags_out;
  248. }
  249. if (IS_SYNC(inode))
  250. ext4_handle_sync(handle);
  251. err = ext4_reserve_inode_write(handle, inode, &iloc);
  252. if (err)
  253. goto flags_err;
  254. for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
  255. if (!(mask & EXT4_FL_USER_MODIFIABLE))
  256. continue;
  257. if (mask & flags)
  258. ext4_set_inode_flag(inode, i);
  259. else
  260. ext4_clear_inode_flag(inode, i);
  261. }
  262. ext4_set_inode_flags(inode);
  263. inode->i_ctime = ext4_current_time(inode);
  264. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  265. flags_err:
  266. ext4_journal_stop(handle);
  267. if (err)
  268. goto flags_out;
  269. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
  270. err = ext4_change_inode_journal_flag(inode, jflag);
  271. if (err)
  272. goto flags_out;
  273. if (migrate) {
  274. if (flags & EXT4_EXTENTS_FL)
  275. err = ext4_ext_migrate(inode);
  276. else
  277. err = ext4_ind_migrate(inode);
  278. }
  279. flags_out:
  280. mutex_unlock(&inode->i_mutex);
  281. mnt_drop_write_file(filp);
  282. return err;
  283. }
  284. case EXT4_IOC_GETVERSION:
  285. case EXT4_IOC_GETVERSION_OLD:
  286. return put_user(inode->i_generation, (int __user *) arg);
  287. case EXT4_IOC_SETVERSION:
  288. case EXT4_IOC_SETVERSION_OLD: {
  289. handle_t *handle;
  290. struct ext4_iloc iloc;
  291. __u32 generation;
  292. int err;
  293. if (!inode_owner_or_capable(inode))
  294. return -EPERM;
  295. if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  296. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
  297. ext4_warning(sb, "Setting inode version is not "
  298. "supported with metadata_csum enabled.");
  299. return -ENOTTY;
  300. }
  301. err = mnt_want_write_file(filp);
  302. if (err)
  303. return err;
  304. if (get_user(generation, (int __user *) arg)) {
  305. err = -EFAULT;
  306. goto setversion_out;
  307. }
  308. mutex_lock(&inode->i_mutex);
  309. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  310. if (IS_ERR(handle)) {
  311. err = PTR_ERR(handle);
  312. goto unlock_out;
  313. }
  314. err = ext4_reserve_inode_write(handle, inode, &iloc);
  315. if (err == 0) {
  316. inode->i_ctime = ext4_current_time(inode);
  317. inode->i_generation = generation;
  318. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  319. }
  320. ext4_journal_stop(handle);
  321. unlock_out:
  322. mutex_unlock(&inode->i_mutex);
  323. setversion_out:
  324. mnt_drop_write_file(filp);
  325. return err;
  326. }
  327. case EXT4_IOC_GROUP_EXTEND: {
  328. ext4_fsblk_t n_blocks_count;
  329. int err, err2=0;
  330. err = ext4_resize_begin(sb);
  331. if (err)
  332. return err;
  333. if (get_user(n_blocks_count, (__u32 __user *)arg)) {
  334. err = -EFAULT;
  335. goto group_extend_out;
  336. }
  337. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  338. EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
  339. ext4_msg(sb, KERN_ERR,
  340. "Online resizing not supported with bigalloc");
  341. err = -EOPNOTSUPP;
  342. goto group_extend_out;
  343. }
  344. err = mnt_want_write_file(filp);
  345. if (err)
  346. goto group_extend_out;
  347. err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
  348. if (EXT4_SB(sb)->s_journal) {
  349. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  350. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  351. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  352. }
  353. if (err == 0)
  354. err = err2;
  355. mnt_drop_write_file(filp);
  356. group_extend_out:
  357. ext4_resize_end(sb);
  358. return err;
  359. }
  360. case EXT4_IOC_MOVE_EXT: {
  361. struct move_extent me;
  362. struct fd donor;
  363. int err;
  364. if (!(filp->f_mode & FMODE_READ) ||
  365. !(filp->f_mode & FMODE_WRITE))
  366. return -EBADF;
  367. if (copy_from_user(&me,
  368. (struct move_extent __user *)arg, sizeof(me)))
  369. return -EFAULT;
  370. me.moved_len = 0;
  371. donor = fdget(me.donor_fd);
  372. if (!donor.file)
  373. return -EBADF;
  374. if (!(donor.file->f_mode & FMODE_WRITE)) {
  375. err = -EBADF;
  376. goto mext_out;
  377. }
  378. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  379. EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
  380. ext4_msg(sb, KERN_ERR,
  381. "Online defrag not supported with bigalloc");
  382. err = -EOPNOTSUPP;
  383. goto mext_out;
  384. }
  385. err = mnt_want_write_file(filp);
  386. if (err)
  387. goto mext_out;
  388. err = ext4_move_extents(filp, donor.file, me.orig_start,
  389. me.donor_start, me.len, &me.moved_len);
  390. mnt_drop_write_file(filp);
  391. if (copy_to_user((struct move_extent __user *)arg,
  392. &me, sizeof(me)))
  393. err = -EFAULT;
  394. mext_out:
  395. fdput(donor);
  396. return err;
  397. }
  398. case EXT4_IOC_GROUP_ADD: {
  399. struct ext4_new_group_data input;
  400. int err, err2=0;
  401. err = ext4_resize_begin(sb);
  402. if (err)
  403. return err;
  404. if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
  405. sizeof(input))) {
  406. err = -EFAULT;
  407. goto group_add_out;
  408. }
  409. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  410. EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
  411. ext4_msg(sb, KERN_ERR,
  412. "Online resizing not supported with bigalloc");
  413. err = -EOPNOTSUPP;
  414. goto group_add_out;
  415. }
  416. err = mnt_want_write_file(filp);
  417. if (err)
  418. goto group_add_out;
  419. err = ext4_group_add(sb, &input);
  420. if (EXT4_SB(sb)->s_journal) {
  421. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  422. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  423. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  424. }
  425. if (err == 0)
  426. err = err2;
  427. mnt_drop_write_file(filp);
  428. if (!err && ext4_has_group_desc_csum(sb) &&
  429. test_opt(sb, INIT_INODE_TABLE))
  430. err = ext4_register_li_request(sb, input.group);
  431. group_add_out:
  432. ext4_resize_end(sb);
  433. return err;
  434. }
  435. case EXT4_IOC_MIGRATE:
  436. {
  437. int err;
  438. if (!inode_owner_or_capable(inode))
  439. return -EACCES;
  440. err = mnt_want_write_file(filp);
  441. if (err)
  442. return err;
  443. /*
  444. * inode_mutex prevent write and truncate on the file.
  445. * Read still goes through. We take i_data_sem in
  446. * ext4_ext_swap_inode_data before we switch the
  447. * inode format to prevent read.
  448. */
  449. mutex_lock(&(inode->i_mutex));
  450. err = ext4_ext_migrate(inode);
  451. mutex_unlock(&(inode->i_mutex));
  452. mnt_drop_write_file(filp);
  453. return err;
  454. }
  455. case EXT4_IOC_ALLOC_DA_BLKS:
  456. {
  457. int err;
  458. if (!inode_owner_or_capable(inode))
  459. return -EACCES;
  460. err = mnt_want_write_file(filp);
  461. if (err)
  462. return err;
  463. err = ext4_alloc_da_blocks(inode);
  464. mnt_drop_write_file(filp);
  465. return err;
  466. }
  467. case EXT4_IOC_SWAP_BOOT:
  468. if (!(filp->f_mode & FMODE_WRITE))
  469. return -EBADF;
  470. return swap_inode_boot_loader(sb, inode);
  471. case EXT4_IOC_RESIZE_FS: {
  472. ext4_fsblk_t n_blocks_count;
  473. int err = 0, err2 = 0;
  474. ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
  475. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  476. EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
  477. ext4_msg(sb, KERN_ERR,
  478. "Online resizing not (yet) supported with bigalloc");
  479. return -EOPNOTSUPP;
  480. }
  481. if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
  482. sizeof(__u64))) {
  483. return -EFAULT;
  484. }
  485. err = ext4_resize_begin(sb);
  486. if (err)
  487. return err;
  488. err = mnt_want_write_file(filp);
  489. if (err)
  490. goto resizefs_out;
  491. err = ext4_resize_fs(sb, n_blocks_count);
  492. if (EXT4_SB(sb)->s_journal) {
  493. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  494. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  495. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  496. }
  497. if (err == 0)
  498. err = err2;
  499. mnt_drop_write_file(filp);
  500. if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
  501. ext4_has_group_desc_csum(sb) &&
  502. test_opt(sb, INIT_INODE_TABLE))
  503. err = ext4_register_li_request(sb, o_group);
  504. resizefs_out:
  505. ext4_resize_end(sb);
  506. return err;
  507. }
  508. case FITRIM:
  509. {
  510. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  511. struct fstrim_range range;
  512. int ret = 0;
  513. if (!capable(CAP_SYS_ADMIN))
  514. return -EPERM;
  515. if (!blk_queue_discard(q))
  516. return -EOPNOTSUPP;
  517. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  518. sizeof(range)))
  519. return -EFAULT;
  520. range.minlen = max((unsigned int)range.minlen,
  521. q->limits.discard_granularity);
  522. ret = ext4_trim_fs(sb, &range);
  523. if (ret < 0)
  524. return ret;
  525. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  526. sizeof(range)))
  527. return -EFAULT;
  528. return 0;
  529. }
  530. default:
  531. return -ENOTTY;
  532. }
  533. }
  534. #ifdef CONFIG_COMPAT
  535. long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  536. {
  537. /* These are just misnamed, they actually get/put from/to user an int */
  538. switch (cmd) {
  539. case EXT4_IOC32_GETFLAGS:
  540. cmd = EXT4_IOC_GETFLAGS;
  541. break;
  542. case EXT4_IOC32_SETFLAGS:
  543. cmd = EXT4_IOC_SETFLAGS;
  544. break;
  545. case EXT4_IOC32_GETVERSION:
  546. cmd = EXT4_IOC_GETVERSION;
  547. break;
  548. case EXT4_IOC32_SETVERSION:
  549. cmd = EXT4_IOC_SETVERSION;
  550. break;
  551. case EXT4_IOC32_GROUP_EXTEND:
  552. cmd = EXT4_IOC_GROUP_EXTEND;
  553. break;
  554. case EXT4_IOC32_GETVERSION_OLD:
  555. cmd = EXT4_IOC_GETVERSION_OLD;
  556. break;
  557. case EXT4_IOC32_SETVERSION_OLD:
  558. cmd = EXT4_IOC_SETVERSION_OLD;
  559. break;
  560. case EXT4_IOC32_GETRSVSZ:
  561. cmd = EXT4_IOC_GETRSVSZ;
  562. break;
  563. case EXT4_IOC32_SETRSVSZ:
  564. cmd = EXT4_IOC_SETRSVSZ;
  565. break;
  566. case EXT4_IOC32_GROUP_ADD: {
  567. struct compat_ext4_new_group_input __user *uinput;
  568. struct ext4_new_group_input input;
  569. mm_segment_t old_fs;
  570. int err;
  571. uinput = compat_ptr(arg);
  572. err = get_user(input.group, &uinput->group);
  573. err |= get_user(input.block_bitmap, &uinput->block_bitmap);
  574. err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
  575. err |= get_user(input.inode_table, &uinput->inode_table);
  576. err |= get_user(input.blocks_count, &uinput->blocks_count);
  577. err |= get_user(input.reserved_blocks,
  578. &uinput->reserved_blocks);
  579. if (err)
  580. return -EFAULT;
  581. old_fs = get_fs();
  582. set_fs(KERNEL_DS);
  583. err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
  584. (unsigned long) &input);
  585. set_fs(old_fs);
  586. return err;
  587. }
  588. case EXT4_IOC_MOVE_EXT:
  589. case FITRIM:
  590. case EXT4_IOC_RESIZE_FS:
  591. break;
  592. default:
  593. return -ENOIOCTLCMD;
  594. }
  595. return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  596. }
  597. #endif