ioctl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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 <linux/random.h>
  17. #include <asm/uaccess.h>
  18. #include "ext4_jbd2.h"
  19. #include "ext4.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. ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
  73. ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
  74. ext4_es_lru_del(inode1);
  75. ext4_es_lru_del(inode2);
  76. isize = i_size_read(inode1);
  77. i_size_write(inode1, i_size_read(inode2));
  78. i_size_write(inode2, isize);
  79. }
  80. /**
  81. * Swap the information from the given @inode and the inode
  82. * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
  83. * important fields of the inodes.
  84. *
  85. * @sb: the super block of the filesystem
  86. * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
  87. *
  88. */
  89. static long swap_inode_boot_loader(struct super_block *sb,
  90. struct inode *inode)
  91. {
  92. handle_t *handle;
  93. int err;
  94. struct inode *inode_bl;
  95. struct ext4_inode_info *ei_bl;
  96. struct ext4_sb_info *sbi = EXT4_SB(sb);
  97. if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
  98. return -EINVAL;
  99. if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
  100. return -EPERM;
  101. inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
  102. if (IS_ERR(inode_bl))
  103. return PTR_ERR(inode_bl);
  104. ei_bl = EXT4_I(inode_bl);
  105. filemap_flush(inode->i_mapping);
  106. filemap_flush(inode_bl->i_mapping);
  107. /* Protect orig inodes against a truncate and make sure,
  108. * that only 1 swap_inode_boot_loader is running. */
  109. lock_two_nondirectories(inode, inode_bl);
  110. truncate_inode_pages(&inode->i_data, 0);
  111. truncate_inode_pages(&inode_bl->i_data, 0);
  112. /* Wait for all existing dio workers */
  113. ext4_inode_block_unlocked_dio(inode);
  114. ext4_inode_block_unlocked_dio(inode_bl);
  115. inode_dio_wait(inode);
  116. inode_dio_wait(inode_bl);
  117. handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
  118. if (IS_ERR(handle)) {
  119. err = -EINVAL;
  120. goto journal_err_out;
  121. }
  122. /* Protect extent tree against block allocations via delalloc */
  123. ext4_double_down_write_data_sem(inode, inode_bl);
  124. if (inode_bl->i_nlink == 0) {
  125. /* this inode has never been used as a BOOT_LOADER */
  126. set_nlink(inode_bl, 1);
  127. i_uid_write(inode_bl, 0);
  128. i_gid_write(inode_bl, 0);
  129. inode_bl->i_flags = 0;
  130. ei_bl->i_flags = 0;
  131. inode_bl->i_version = 1;
  132. i_size_write(inode_bl, 0);
  133. inode_bl->i_mode = S_IFREG;
  134. if (EXT4_HAS_INCOMPAT_FEATURE(sb,
  135. EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  136. ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
  137. ext4_ext_tree_init(handle, inode_bl);
  138. } else
  139. memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
  140. }
  141. swap_inode_data(inode, inode_bl);
  142. inode->i_ctime = inode_bl->i_ctime = ext4_current_time(inode);
  143. spin_lock(&sbi->s_next_gen_lock);
  144. inode->i_generation = sbi->s_next_generation++;
  145. inode_bl->i_generation = sbi->s_next_generation++;
  146. spin_unlock(&sbi->s_next_gen_lock);
  147. ext4_discard_preallocations(inode);
  148. err = ext4_mark_inode_dirty(handle, inode);
  149. if (err < 0) {
  150. ext4_warning(inode->i_sb,
  151. "couldn't mark inode #%lu dirty (err %d)",
  152. inode->i_ino, err);
  153. /* Revert all changes: */
  154. swap_inode_data(inode, inode_bl);
  155. } else {
  156. err = ext4_mark_inode_dirty(handle, inode_bl);
  157. if (err < 0) {
  158. ext4_warning(inode_bl->i_sb,
  159. "couldn't mark inode #%lu dirty (err %d)",
  160. inode_bl->i_ino, err);
  161. /* Revert all changes: */
  162. swap_inode_data(inode, inode_bl);
  163. ext4_mark_inode_dirty(handle, inode);
  164. }
  165. }
  166. ext4_journal_stop(handle);
  167. ext4_double_up_write_data_sem(inode, inode_bl);
  168. journal_err_out:
  169. ext4_inode_resume_unlocked_dio(inode);
  170. ext4_inode_resume_unlocked_dio(inode_bl);
  171. unlock_two_nondirectories(inode, inode_bl);
  172. iput(inode_bl);
  173. return err;
  174. }
  175. static int uuid_is_zero(__u8 u[16])
  176. {
  177. int i;
  178. for (i = 0; i < 16; i++)
  179. if (u[i])
  180. return 0;
  181. return 1;
  182. }
  183. long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  184. {
  185. struct inode *inode = file_inode(filp);
  186. struct super_block *sb = inode->i_sb;
  187. struct ext4_inode_info *ei = EXT4_I(inode);
  188. unsigned int flags;
  189. ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
  190. switch (cmd) {
  191. case EXT4_IOC_GETFLAGS:
  192. ext4_get_inode_flags(ei);
  193. flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
  194. return put_user(flags, (int __user *) arg);
  195. case EXT4_IOC_SETFLAGS: {
  196. handle_t *handle = NULL;
  197. int err, migrate = 0;
  198. struct ext4_iloc iloc;
  199. unsigned int oldflags, mask, i;
  200. unsigned int jflag;
  201. if (!inode_owner_or_capable(inode))
  202. return -EACCES;
  203. if (get_user(flags, (int __user *) arg))
  204. return -EFAULT;
  205. err = mnt_want_write_file(filp);
  206. if (err)
  207. return err;
  208. flags = ext4_mask_flags(inode->i_mode, flags);
  209. err = -EPERM;
  210. mutex_lock(&inode->i_mutex);
  211. /* Is it quota file? Do not allow user to mess with it */
  212. if (IS_NOQUOTA(inode))
  213. goto flags_out;
  214. oldflags = ei->i_flags;
  215. /* The JOURNAL_DATA flag is modifiable only by root */
  216. jflag = flags & EXT4_JOURNAL_DATA_FL;
  217. /*
  218. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  219. * the relevant capability.
  220. *
  221. * This test looks nicer. Thanks to Pauline Middelink
  222. */
  223. if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
  224. if (!capable(CAP_LINUX_IMMUTABLE))
  225. goto flags_out;
  226. }
  227. /*
  228. * The JOURNAL_DATA flag can only be changed by
  229. * the relevant capability.
  230. */
  231. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
  232. if (!capable(CAP_SYS_RESOURCE))
  233. goto flags_out;
  234. }
  235. if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
  236. migrate = 1;
  237. if (flags & EXT4_EOFBLOCKS_FL) {
  238. /* we don't support adding EOFBLOCKS flag */
  239. if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
  240. err = -EOPNOTSUPP;
  241. goto flags_out;
  242. }
  243. } else if (oldflags & EXT4_EOFBLOCKS_FL)
  244. ext4_truncate(inode);
  245. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  246. if (IS_ERR(handle)) {
  247. err = PTR_ERR(handle);
  248. goto flags_out;
  249. }
  250. if (IS_SYNC(inode))
  251. ext4_handle_sync(handle);
  252. err = ext4_reserve_inode_write(handle, inode, &iloc);
  253. if (err)
  254. goto flags_err;
  255. for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
  256. if (!(mask & EXT4_FL_USER_MODIFIABLE))
  257. continue;
  258. if (mask & flags)
  259. ext4_set_inode_flag(inode, i);
  260. else
  261. ext4_clear_inode_flag(inode, i);
  262. }
  263. ext4_set_inode_flags(inode);
  264. inode->i_ctime = ext4_current_time(inode);
  265. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  266. flags_err:
  267. ext4_journal_stop(handle);
  268. if (err)
  269. goto flags_out;
  270. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
  271. err = ext4_change_inode_journal_flag(inode, jflag);
  272. if (err)
  273. goto flags_out;
  274. if (migrate) {
  275. if (flags & EXT4_EXTENTS_FL)
  276. err = ext4_ext_migrate(inode);
  277. else
  278. err = ext4_ind_migrate(inode);
  279. }
  280. flags_out:
  281. mutex_unlock(&inode->i_mutex);
  282. mnt_drop_write_file(filp);
  283. return err;
  284. }
  285. case EXT4_IOC_GETVERSION:
  286. case EXT4_IOC_GETVERSION_OLD:
  287. return put_user(inode->i_generation, (int __user *) arg);
  288. case EXT4_IOC_SETVERSION:
  289. case EXT4_IOC_SETVERSION_OLD: {
  290. handle_t *handle;
  291. struct ext4_iloc iloc;
  292. __u32 generation;
  293. int err;
  294. if (!inode_owner_or_capable(inode))
  295. return -EPERM;
  296. if (ext4_has_metadata_csum(inode->i_sb)) {
  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. {
  469. int err;
  470. if (!(filp->f_mode & FMODE_WRITE))
  471. return -EBADF;
  472. err = mnt_want_write_file(filp);
  473. if (err)
  474. return err;
  475. err = swap_inode_boot_loader(sb, inode);
  476. mnt_drop_write_file(filp);
  477. return err;
  478. }
  479. case EXT4_IOC_RESIZE_FS: {
  480. ext4_fsblk_t n_blocks_count;
  481. int err = 0, err2 = 0;
  482. ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
  483. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  484. EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
  485. ext4_msg(sb, KERN_ERR,
  486. "Online resizing not (yet) supported with bigalloc");
  487. return -EOPNOTSUPP;
  488. }
  489. if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
  490. sizeof(__u64))) {
  491. return -EFAULT;
  492. }
  493. err = ext4_resize_begin(sb);
  494. if (err)
  495. return err;
  496. err = mnt_want_write_file(filp);
  497. if (err)
  498. goto resizefs_out;
  499. err = ext4_resize_fs(sb, n_blocks_count);
  500. if (EXT4_SB(sb)->s_journal) {
  501. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  502. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  503. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  504. }
  505. if (err == 0)
  506. err = err2;
  507. mnt_drop_write_file(filp);
  508. if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
  509. ext4_has_group_desc_csum(sb) &&
  510. test_opt(sb, INIT_INODE_TABLE))
  511. err = ext4_register_li_request(sb, o_group);
  512. resizefs_out:
  513. ext4_resize_end(sb);
  514. return err;
  515. }
  516. case FIDTRIM:
  517. case FITRIM:
  518. {
  519. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  520. struct fstrim_range range;
  521. int ret = 0;
  522. int flags = cmd == FIDTRIM ? BLKDEV_DISCARD_SECURE : 0;
  523. if (!capable(CAP_SYS_ADMIN))
  524. return -EPERM;
  525. if (!blk_queue_discard(q))
  526. return -EOPNOTSUPP;
  527. if ((flags & BLKDEV_DISCARD_SECURE) && !blk_queue_secdiscard(q))
  528. return -EOPNOTSUPP;
  529. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  530. sizeof(range)))
  531. return -EFAULT;
  532. range.minlen = max((unsigned int)range.minlen,
  533. q->limits.discard_granularity);
  534. ret = ext4_trim_fs(sb, &range, flags);
  535. if (ret < 0)
  536. return ret;
  537. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  538. sizeof(range)))
  539. return -EFAULT;
  540. return 0;
  541. }
  542. case EXT4_IOC_PRECACHE_EXTENTS:
  543. return ext4_ext_precache(inode);
  544. case EXT4_IOC_SET_ENCRYPTION_POLICY: {
  545. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  546. struct ext4_encryption_policy policy;
  547. int err = 0;
  548. if (copy_from_user(&policy,
  549. (struct ext4_encryption_policy __user *)arg,
  550. sizeof(policy))) {
  551. err = -EFAULT;
  552. goto encryption_policy_out;
  553. }
  554. err = ext4_process_policy(&policy, inode);
  555. encryption_policy_out:
  556. return err;
  557. #else
  558. return -EOPNOTSUPP;
  559. #endif
  560. }
  561. case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
  562. int err, err2;
  563. struct ext4_sb_info *sbi = EXT4_SB(sb);
  564. handle_t *handle;
  565. if (!ext4_sb_has_crypto(sb))
  566. return -EOPNOTSUPP;
  567. if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
  568. err = mnt_want_write_file(filp);
  569. if (err)
  570. return err;
  571. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  572. if (IS_ERR(handle)) {
  573. err = PTR_ERR(handle);
  574. goto pwsalt_err_exit;
  575. }
  576. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  577. if (err)
  578. goto pwsalt_err_journal;
  579. generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
  580. err = ext4_handle_dirty_metadata(handle, NULL,
  581. sbi->s_sbh);
  582. pwsalt_err_journal:
  583. err2 = ext4_journal_stop(handle);
  584. if (err2 && !err)
  585. err = err2;
  586. pwsalt_err_exit:
  587. mnt_drop_write_file(filp);
  588. if (err)
  589. return err;
  590. }
  591. if (copy_to_user((void *) arg, sbi->s_es->s_encrypt_pw_salt,
  592. 16))
  593. return -EFAULT;
  594. return 0;
  595. }
  596. case EXT4_IOC_GET_ENCRYPTION_POLICY: {
  597. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  598. struct ext4_encryption_policy policy;
  599. int err = 0;
  600. if (!ext4_encrypted_inode(inode))
  601. return -ENOENT;
  602. err = ext4_get_policy(inode, &policy);
  603. if (err)
  604. return err;
  605. if (copy_to_user((void *)arg, &policy, sizeof(policy)))
  606. return -EFAULT;
  607. return 0;
  608. #else
  609. return -EOPNOTSUPP;
  610. #endif
  611. }
  612. default:
  613. return -ENOTTY;
  614. }
  615. }
  616. #ifdef CONFIG_COMPAT
  617. long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  618. {
  619. /* These are just misnamed, they actually get/put from/to user an int */
  620. switch (cmd) {
  621. case EXT4_IOC32_GETFLAGS:
  622. cmd = EXT4_IOC_GETFLAGS;
  623. break;
  624. case EXT4_IOC32_SETFLAGS:
  625. cmd = EXT4_IOC_SETFLAGS;
  626. break;
  627. case EXT4_IOC32_GETVERSION:
  628. cmd = EXT4_IOC_GETVERSION;
  629. break;
  630. case EXT4_IOC32_SETVERSION:
  631. cmd = EXT4_IOC_SETVERSION;
  632. break;
  633. case EXT4_IOC32_GROUP_EXTEND:
  634. cmd = EXT4_IOC_GROUP_EXTEND;
  635. break;
  636. case EXT4_IOC32_GETVERSION_OLD:
  637. cmd = EXT4_IOC_GETVERSION_OLD;
  638. break;
  639. case EXT4_IOC32_SETVERSION_OLD:
  640. cmd = EXT4_IOC_SETVERSION_OLD;
  641. break;
  642. case EXT4_IOC32_GETRSVSZ:
  643. cmd = EXT4_IOC_GETRSVSZ;
  644. break;
  645. case EXT4_IOC32_SETRSVSZ:
  646. cmd = EXT4_IOC_SETRSVSZ;
  647. break;
  648. case EXT4_IOC32_GROUP_ADD: {
  649. struct compat_ext4_new_group_input __user *uinput;
  650. struct ext4_new_group_input input;
  651. mm_segment_t old_fs;
  652. int err;
  653. uinput = compat_ptr(arg);
  654. err = get_user(input.group, &uinput->group);
  655. err |= get_user(input.block_bitmap, &uinput->block_bitmap);
  656. err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
  657. err |= get_user(input.inode_table, &uinput->inode_table);
  658. err |= get_user(input.blocks_count, &uinput->blocks_count);
  659. err |= get_user(input.reserved_blocks,
  660. &uinput->reserved_blocks);
  661. if (err)
  662. return -EFAULT;
  663. old_fs = get_fs();
  664. set_fs(KERNEL_DS);
  665. err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
  666. (unsigned long) &input);
  667. set_fs(old_fs);
  668. return err;
  669. }
  670. case EXT4_IOC_MOVE_EXT:
  671. case FITRIM:
  672. case EXT4_IOC_RESIZE_FS:
  673. case EXT4_IOC_PRECACHE_EXTENTS:
  674. case EXT4_IOC_SET_ENCRYPTION_POLICY:
  675. case EXT4_IOC_GET_ENCRYPTION_PWSALT:
  676. case EXT4_IOC_GET_ENCRYPTION_POLICY:
  677. break;
  678. default:
  679. return -ENOIOCTLCMD;
  680. }
  681. return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  682. }
  683. #endif