ioctl.c 19 KB

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