ioctl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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_INCOMPAT_FEATURE(sb,
  129. EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  130. ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
  131. ext4_ext_tree_init(handle, inode_bl);
  132. } else
  133. memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
  134. }
  135. swap_inode_data(inode, inode_bl);
  136. inode->i_ctime = inode_bl->i_ctime = ext4_current_time(inode);
  137. spin_lock(&sbi->s_next_gen_lock);
  138. inode->i_generation = sbi->s_next_generation++;
  139. inode_bl->i_generation = sbi->s_next_generation++;
  140. spin_unlock(&sbi->s_next_gen_lock);
  141. ext4_discard_preallocations(inode);
  142. err = ext4_mark_inode_dirty(handle, inode);
  143. if (err < 0) {
  144. ext4_warning(inode->i_sb,
  145. "couldn't mark inode #%lu dirty (err %d)",
  146. inode->i_ino, err);
  147. /* Revert all changes: */
  148. swap_inode_data(inode, inode_bl);
  149. } else {
  150. err = ext4_mark_inode_dirty(handle, inode_bl);
  151. if (err < 0) {
  152. ext4_warning(inode_bl->i_sb,
  153. "couldn't mark inode #%lu dirty (err %d)",
  154. inode_bl->i_ino, err);
  155. /* Revert all changes: */
  156. swap_inode_data(inode, inode_bl);
  157. ext4_mark_inode_dirty(handle, inode);
  158. }
  159. }
  160. ext4_journal_stop(handle);
  161. ext4_double_up_write_data_sem(inode, inode_bl);
  162. journal_err_out:
  163. ext4_inode_resume_unlocked_dio(inode);
  164. ext4_inode_resume_unlocked_dio(inode_bl);
  165. unlock_two_nondirectories(inode, inode_bl);
  166. iput(inode_bl);
  167. return err;
  168. }
  169. static int uuid_is_zero(__u8 u[16])
  170. {
  171. int i;
  172. for (i = 0; i < 16; i++)
  173. if (u[i])
  174. return 0;
  175. return 1;
  176. }
  177. long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  178. {
  179. struct inode *inode = file_inode(filp);
  180. struct super_block *sb = inode->i_sb;
  181. struct ext4_inode_info *ei = EXT4_I(inode);
  182. unsigned int flags;
  183. ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
  184. switch (cmd) {
  185. case EXT4_IOC_GETFLAGS:
  186. ext4_get_inode_flags(ei);
  187. flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
  188. return put_user(flags, (int __user *) arg);
  189. case EXT4_IOC_SETFLAGS: {
  190. handle_t *handle = NULL;
  191. int err, migrate = 0;
  192. struct ext4_iloc iloc;
  193. unsigned int oldflags, mask, i;
  194. unsigned int jflag;
  195. if (!inode_owner_or_capable(inode))
  196. return -EACCES;
  197. if (get_user(flags, (int __user *) arg))
  198. return -EFAULT;
  199. err = mnt_want_write_file(filp);
  200. if (err)
  201. return err;
  202. flags = ext4_mask_flags(inode->i_mode, flags);
  203. err = -EPERM;
  204. mutex_lock(&inode->i_mutex);
  205. /* Is it quota file? Do not allow user to mess with it */
  206. if (IS_NOQUOTA(inode))
  207. goto flags_out;
  208. oldflags = ei->i_flags;
  209. /* The JOURNAL_DATA flag is modifiable only by root */
  210. jflag = flags & EXT4_JOURNAL_DATA_FL;
  211. /*
  212. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  213. * the relevant capability.
  214. *
  215. * This test looks nicer. Thanks to Pauline Middelink
  216. */
  217. if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
  218. if (!capable(CAP_LINUX_IMMUTABLE))
  219. goto flags_out;
  220. }
  221. /*
  222. * The JOURNAL_DATA flag can only be changed by
  223. * the relevant capability.
  224. */
  225. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
  226. if (!capable(CAP_SYS_RESOURCE))
  227. goto flags_out;
  228. }
  229. if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
  230. migrate = 1;
  231. if (flags & EXT4_EOFBLOCKS_FL) {
  232. /* we don't support adding EOFBLOCKS flag */
  233. if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
  234. err = -EOPNOTSUPP;
  235. goto flags_out;
  236. }
  237. } else if (oldflags & EXT4_EOFBLOCKS_FL)
  238. ext4_truncate(inode);
  239. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  240. if (IS_ERR(handle)) {
  241. err = PTR_ERR(handle);
  242. goto flags_out;
  243. }
  244. if (IS_SYNC(inode))
  245. ext4_handle_sync(handle);
  246. err = ext4_reserve_inode_write(handle, inode, &iloc);
  247. if (err)
  248. goto flags_err;
  249. for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
  250. if (!(mask & EXT4_FL_USER_MODIFIABLE))
  251. continue;
  252. if (mask & flags)
  253. ext4_set_inode_flag(inode, i);
  254. else
  255. ext4_clear_inode_flag(inode, i);
  256. }
  257. ext4_set_inode_flags(inode);
  258. inode->i_ctime = ext4_current_time(inode);
  259. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  260. flags_err:
  261. ext4_journal_stop(handle);
  262. if (err)
  263. goto flags_out;
  264. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
  265. err = ext4_change_inode_journal_flag(inode, jflag);
  266. if (err)
  267. goto flags_out;
  268. if (migrate) {
  269. if (flags & EXT4_EXTENTS_FL)
  270. err = ext4_ext_migrate(inode);
  271. else
  272. err = ext4_ind_migrate(inode);
  273. }
  274. flags_out:
  275. mutex_unlock(&inode->i_mutex);
  276. mnt_drop_write_file(filp);
  277. return err;
  278. }
  279. case EXT4_IOC_GETVERSION:
  280. case EXT4_IOC_GETVERSION_OLD:
  281. return put_user(inode->i_generation, (int __user *) arg);
  282. case EXT4_IOC_SETVERSION:
  283. case EXT4_IOC_SETVERSION_OLD: {
  284. handle_t *handle;
  285. struct ext4_iloc iloc;
  286. __u32 generation;
  287. int err;
  288. if (!inode_owner_or_capable(inode))
  289. return -EPERM;
  290. if (ext4_has_metadata_csum(inode->i_sb)) {
  291. ext4_warning(sb, "Setting inode version is not "
  292. "supported with metadata_csum enabled.");
  293. return -ENOTTY;
  294. }
  295. err = mnt_want_write_file(filp);
  296. if (err)
  297. return err;
  298. if (get_user(generation, (int __user *) arg)) {
  299. err = -EFAULT;
  300. goto setversion_out;
  301. }
  302. mutex_lock(&inode->i_mutex);
  303. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  304. if (IS_ERR(handle)) {
  305. err = PTR_ERR(handle);
  306. goto unlock_out;
  307. }
  308. err = ext4_reserve_inode_write(handle, inode, &iloc);
  309. if (err == 0) {
  310. inode->i_ctime = ext4_current_time(inode);
  311. inode->i_generation = generation;
  312. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  313. }
  314. ext4_journal_stop(handle);
  315. unlock_out:
  316. mutex_unlock(&inode->i_mutex);
  317. setversion_out:
  318. mnt_drop_write_file(filp);
  319. return err;
  320. }
  321. case EXT4_IOC_GROUP_EXTEND: {
  322. ext4_fsblk_t n_blocks_count;
  323. int err, err2=0;
  324. err = ext4_resize_begin(sb);
  325. if (err)
  326. return err;
  327. if (get_user(n_blocks_count, (__u32 __user *)arg)) {
  328. err = -EFAULT;
  329. goto group_extend_out;
  330. }
  331. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  332. EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
  333. ext4_msg(sb, KERN_ERR,
  334. "Online resizing not supported with bigalloc");
  335. err = -EOPNOTSUPP;
  336. goto group_extend_out;
  337. }
  338. err = mnt_want_write_file(filp);
  339. if (err)
  340. goto group_extend_out;
  341. err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
  342. if (EXT4_SB(sb)->s_journal) {
  343. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  344. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  345. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  346. }
  347. if (err == 0)
  348. err = err2;
  349. mnt_drop_write_file(filp);
  350. group_extend_out:
  351. ext4_resize_end(sb);
  352. return err;
  353. }
  354. case EXT4_IOC_MOVE_EXT: {
  355. struct move_extent me;
  356. struct fd donor;
  357. int err;
  358. if (!(filp->f_mode & FMODE_READ) ||
  359. !(filp->f_mode & FMODE_WRITE))
  360. return -EBADF;
  361. if (copy_from_user(&me,
  362. (struct move_extent __user *)arg, sizeof(me)))
  363. return -EFAULT;
  364. me.moved_len = 0;
  365. donor = fdget(me.donor_fd);
  366. if (!donor.file)
  367. return -EBADF;
  368. if (!(donor.file->f_mode & FMODE_WRITE)) {
  369. err = -EBADF;
  370. goto mext_out;
  371. }
  372. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  373. EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
  374. ext4_msg(sb, KERN_ERR,
  375. "Online defrag not supported with bigalloc");
  376. err = -EOPNOTSUPP;
  377. goto mext_out;
  378. }
  379. err = mnt_want_write_file(filp);
  380. if (err)
  381. goto mext_out;
  382. err = ext4_move_extents(filp, donor.file, me.orig_start,
  383. me.donor_start, me.len, &me.moved_len);
  384. mnt_drop_write_file(filp);
  385. if (copy_to_user((struct move_extent __user *)arg,
  386. &me, sizeof(me)))
  387. err = -EFAULT;
  388. mext_out:
  389. fdput(donor);
  390. return err;
  391. }
  392. case EXT4_IOC_GROUP_ADD: {
  393. struct ext4_new_group_data input;
  394. int err, err2=0;
  395. err = ext4_resize_begin(sb);
  396. if (err)
  397. return err;
  398. if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
  399. sizeof(input))) {
  400. err = -EFAULT;
  401. goto group_add_out;
  402. }
  403. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  404. EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
  405. ext4_msg(sb, KERN_ERR,
  406. "Online resizing not supported with bigalloc");
  407. err = -EOPNOTSUPP;
  408. goto group_add_out;
  409. }
  410. err = mnt_want_write_file(filp);
  411. if (err)
  412. goto group_add_out;
  413. err = ext4_group_add(sb, &input);
  414. if (EXT4_SB(sb)->s_journal) {
  415. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  416. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  417. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  418. }
  419. if (err == 0)
  420. err = err2;
  421. mnt_drop_write_file(filp);
  422. if (!err && ext4_has_group_desc_csum(sb) &&
  423. test_opt(sb, INIT_INODE_TABLE))
  424. err = ext4_register_li_request(sb, input.group);
  425. group_add_out:
  426. ext4_resize_end(sb);
  427. return err;
  428. }
  429. case EXT4_IOC_MIGRATE:
  430. {
  431. int err;
  432. if (!inode_owner_or_capable(inode))
  433. return -EACCES;
  434. err = mnt_want_write_file(filp);
  435. if (err)
  436. return err;
  437. /*
  438. * inode_mutex prevent write and truncate on the file.
  439. * Read still goes through. We take i_data_sem in
  440. * ext4_ext_swap_inode_data before we switch the
  441. * inode format to prevent read.
  442. */
  443. mutex_lock(&(inode->i_mutex));
  444. err = ext4_ext_migrate(inode);
  445. mutex_unlock(&(inode->i_mutex));
  446. mnt_drop_write_file(filp);
  447. return err;
  448. }
  449. case EXT4_IOC_ALLOC_DA_BLKS:
  450. {
  451. int err;
  452. if (!inode_owner_or_capable(inode))
  453. return -EACCES;
  454. err = mnt_want_write_file(filp);
  455. if (err)
  456. return err;
  457. err = ext4_alloc_da_blocks(inode);
  458. mnt_drop_write_file(filp);
  459. return err;
  460. }
  461. case EXT4_IOC_SWAP_BOOT:
  462. {
  463. int err;
  464. if (!(filp->f_mode & FMODE_WRITE))
  465. return -EBADF;
  466. err = mnt_want_write_file(filp);
  467. if (err)
  468. return err;
  469. err = swap_inode_boot_loader(sb, inode);
  470. mnt_drop_write_file(filp);
  471. return err;
  472. }
  473. case EXT4_IOC_RESIZE_FS: {
  474. ext4_fsblk_t n_blocks_count;
  475. int err = 0, err2 = 0;
  476. ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
  477. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  478. EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
  479. ext4_msg(sb, KERN_ERR,
  480. "Online resizing not (yet) supported with bigalloc");
  481. return -EOPNOTSUPP;
  482. }
  483. if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
  484. sizeof(__u64))) {
  485. return -EFAULT;
  486. }
  487. err = ext4_resize_begin(sb);
  488. if (err)
  489. return err;
  490. err = mnt_want_write_file(filp);
  491. if (err)
  492. goto resizefs_out;
  493. err = ext4_resize_fs(sb, n_blocks_count);
  494. if (EXT4_SB(sb)->s_journal) {
  495. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  496. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  497. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  498. }
  499. if (err == 0)
  500. err = err2;
  501. mnt_drop_write_file(filp);
  502. if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
  503. ext4_has_group_desc_csum(sb) &&
  504. test_opt(sb, INIT_INODE_TABLE))
  505. err = ext4_register_li_request(sb, o_group);
  506. resizefs_out:
  507. ext4_resize_end(sb);
  508. return err;
  509. }
  510. case FITRIM:
  511. {
  512. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  513. struct fstrim_range range;
  514. int ret = 0;
  515. if (!capable(CAP_SYS_ADMIN))
  516. return -EPERM;
  517. if (!blk_queue_discard(q))
  518. return -EOPNOTSUPP;
  519. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  520. sizeof(range)))
  521. return -EFAULT;
  522. range.minlen = max((unsigned int)range.minlen,
  523. q->limits.discard_granularity);
  524. ret = ext4_trim_fs(sb, &range);
  525. if (ret < 0)
  526. return ret;
  527. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  528. sizeof(range)))
  529. return -EFAULT;
  530. return 0;
  531. }
  532. case EXT4_IOC_PRECACHE_EXTENTS:
  533. return ext4_ext_precache(inode);
  534. case EXT4_IOC_SET_ENCRYPTION_POLICY: {
  535. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  536. struct ext4_encryption_policy policy;
  537. int err = 0;
  538. if (copy_from_user(&policy,
  539. (struct ext4_encryption_policy __user *)arg,
  540. sizeof(policy))) {
  541. err = -EFAULT;
  542. goto encryption_policy_out;
  543. }
  544. err = ext4_process_policy(&policy, inode);
  545. encryption_policy_out:
  546. return err;
  547. #else
  548. return -EOPNOTSUPP;
  549. #endif
  550. }
  551. case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
  552. int err, err2;
  553. struct ext4_sb_info *sbi = EXT4_SB(sb);
  554. handle_t *handle;
  555. if (!ext4_sb_has_crypto(sb))
  556. return -EOPNOTSUPP;
  557. if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
  558. err = mnt_want_write_file(filp);
  559. if (err)
  560. return err;
  561. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  562. if (IS_ERR(handle)) {
  563. err = PTR_ERR(handle);
  564. goto pwsalt_err_exit;
  565. }
  566. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  567. if (err)
  568. goto pwsalt_err_journal;
  569. generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
  570. err = ext4_handle_dirty_metadata(handle, NULL,
  571. sbi->s_sbh);
  572. pwsalt_err_journal:
  573. err2 = ext4_journal_stop(handle);
  574. if (err2 && !err)
  575. err = err2;
  576. pwsalt_err_exit:
  577. mnt_drop_write_file(filp);
  578. if (err)
  579. return err;
  580. }
  581. if (copy_to_user((void __user *) arg,
  582. sbi->s_es->s_encrypt_pw_salt, 16))
  583. return -EFAULT;
  584. return 0;
  585. }
  586. case EXT4_IOC_GET_ENCRYPTION_POLICY: {
  587. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  588. struct ext4_encryption_policy policy;
  589. int err = 0;
  590. if (!ext4_encrypted_inode(inode))
  591. return -ENOENT;
  592. err = ext4_get_policy(inode, &policy);
  593. if (err)
  594. return err;
  595. if (copy_to_user((void __user *)arg, &policy, sizeof(policy)))
  596. return -EFAULT;
  597. return 0;
  598. #else
  599. return -EOPNOTSUPP;
  600. #endif
  601. }
  602. default:
  603. return -ENOTTY;
  604. }
  605. }
  606. #ifdef CONFIG_COMPAT
  607. long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  608. {
  609. /* These are just misnamed, they actually get/put from/to user an int */
  610. switch (cmd) {
  611. case EXT4_IOC32_GETFLAGS:
  612. cmd = EXT4_IOC_GETFLAGS;
  613. break;
  614. case EXT4_IOC32_SETFLAGS:
  615. cmd = EXT4_IOC_SETFLAGS;
  616. break;
  617. case EXT4_IOC32_GETVERSION:
  618. cmd = EXT4_IOC_GETVERSION;
  619. break;
  620. case EXT4_IOC32_SETVERSION:
  621. cmd = EXT4_IOC_SETVERSION;
  622. break;
  623. case EXT4_IOC32_GROUP_EXTEND:
  624. cmd = EXT4_IOC_GROUP_EXTEND;
  625. break;
  626. case EXT4_IOC32_GETVERSION_OLD:
  627. cmd = EXT4_IOC_GETVERSION_OLD;
  628. break;
  629. case EXT4_IOC32_SETVERSION_OLD:
  630. cmd = EXT4_IOC_SETVERSION_OLD;
  631. break;
  632. case EXT4_IOC32_GETRSVSZ:
  633. cmd = EXT4_IOC_GETRSVSZ;
  634. break;
  635. case EXT4_IOC32_SETRSVSZ:
  636. cmd = EXT4_IOC_SETRSVSZ;
  637. break;
  638. case EXT4_IOC32_GROUP_ADD: {
  639. struct compat_ext4_new_group_input __user *uinput;
  640. struct ext4_new_group_input input;
  641. mm_segment_t old_fs;
  642. int err;
  643. uinput = compat_ptr(arg);
  644. err = get_user(input.group, &uinput->group);
  645. err |= get_user(input.block_bitmap, &uinput->block_bitmap);
  646. err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
  647. err |= get_user(input.inode_table, &uinput->inode_table);
  648. err |= get_user(input.blocks_count, &uinput->blocks_count);
  649. err |= get_user(input.reserved_blocks,
  650. &uinput->reserved_blocks);
  651. if (err)
  652. return -EFAULT;
  653. old_fs = get_fs();
  654. set_fs(KERNEL_DS);
  655. err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
  656. (unsigned long) &input);
  657. set_fs(old_fs);
  658. return err;
  659. }
  660. case EXT4_IOC_MOVE_EXT:
  661. case EXT4_IOC_RESIZE_FS:
  662. case EXT4_IOC_PRECACHE_EXTENTS:
  663. case EXT4_IOC_SET_ENCRYPTION_POLICY:
  664. case EXT4_IOC_GET_ENCRYPTION_PWSALT:
  665. case EXT4_IOC_GET_ENCRYPTION_POLICY:
  666. break;
  667. default:
  668. return -ENOIOCTLCMD;
  669. }
  670. return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  671. }
  672. #endif