ioctl.c 19 KB

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