ioctl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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/quotaops.h>
  16. #include <linux/uuid.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. ap = (unsigned char *)a;
  33. bp = (unsigned char *)b;
  34. while (len-- > 0) {
  35. swap(*ap, *bp);
  36. ap++;
  37. bp++;
  38. }
  39. }
  40. /**
  41. * Swap i_data and associated attributes between @inode1 and @inode2.
  42. * This function is used for the primary swap between inode1 and inode2
  43. * and also to revert this primary swap in case of errors.
  44. *
  45. * Therefore you have to make sure, that calling this method twice
  46. * will revert all changes.
  47. *
  48. * @inode1: pointer to first inode
  49. * @inode2: pointer to second inode
  50. */
  51. static void swap_inode_data(struct inode *inode1, struct inode *inode2)
  52. {
  53. loff_t isize;
  54. struct ext4_inode_info *ei1;
  55. struct ext4_inode_info *ei2;
  56. ei1 = EXT4_I(inode1);
  57. ei2 = EXT4_I(inode2);
  58. memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags));
  59. memswap(&inode1->i_version, &inode2->i_version,
  60. sizeof(inode1->i_version));
  61. memswap(&inode1->i_blocks, &inode2->i_blocks,
  62. sizeof(inode1->i_blocks));
  63. memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes));
  64. memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime));
  65. memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime));
  66. memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
  67. memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags));
  68. memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize));
  69. ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
  70. ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
  71. isize = i_size_read(inode1);
  72. i_size_write(inode1, i_size_read(inode2));
  73. i_size_write(inode2, isize);
  74. }
  75. /**
  76. * Swap the information from the given @inode and the inode
  77. * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
  78. * important fields of the inodes.
  79. *
  80. * @sb: the super block of the filesystem
  81. * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
  82. *
  83. */
  84. static long swap_inode_boot_loader(struct super_block *sb,
  85. struct inode *inode)
  86. {
  87. handle_t *handle;
  88. int err;
  89. struct inode *inode_bl;
  90. struct ext4_inode_info *ei_bl;
  91. struct ext4_sb_info *sbi = EXT4_SB(sb);
  92. if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
  93. return -EINVAL;
  94. if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
  95. return -EPERM;
  96. inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
  97. if (IS_ERR(inode_bl))
  98. return PTR_ERR(inode_bl);
  99. ei_bl = EXT4_I(inode_bl);
  100. filemap_flush(inode->i_mapping);
  101. filemap_flush(inode_bl->i_mapping);
  102. /* Protect orig inodes against a truncate and make sure,
  103. * that only 1 swap_inode_boot_loader is running. */
  104. lock_two_nondirectories(inode, inode_bl);
  105. truncate_inode_pages(&inode->i_data, 0);
  106. truncate_inode_pages(&inode_bl->i_data, 0);
  107. /* Wait for all existing dio workers */
  108. ext4_inode_block_unlocked_dio(inode);
  109. ext4_inode_block_unlocked_dio(inode_bl);
  110. inode_dio_wait(inode);
  111. inode_dio_wait(inode_bl);
  112. handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
  113. if (IS_ERR(handle)) {
  114. err = -EINVAL;
  115. goto journal_err_out;
  116. }
  117. /* Protect extent tree against block allocations via delalloc */
  118. ext4_double_down_write_data_sem(inode, inode_bl);
  119. if (inode_bl->i_nlink == 0) {
  120. /* this inode has never been used as a BOOT_LOADER */
  121. set_nlink(inode_bl, 1);
  122. i_uid_write(inode_bl, 0);
  123. i_gid_write(inode_bl, 0);
  124. inode_bl->i_flags = 0;
  125. ei_bl->i_flags = 0;
  126. inode_bl->i_version = 1;
  127. i_size_write(inode_bl, 0);
  128. inode_bl->i_mode = S_IFREG;
  129. if (ext4_has_feature_extents(sb)) {
  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. static int ext4_ioctl_setflags(struct inode *inode,
  178. unsigned int flags)
  179. {
  180. struct ext4_inode_info *ei = EXT4_I(inode);
  181. handle_t *handle = NULL;
  182. int err = -EPERM, migrate = 0;
  183. struct ext4_iloc iloc;
  184. unsigned int oldflags, mask, i;
  185. unsigned int jflag;
  186. /* Is it quota file? Do not allow user to mess with it */
  187. if (IS_NOQUOTA(inode))
  188. goto flags_out;
  189. oldflags = ei->i_flags;
  190. /* The JOURNAL_DATA flag is modifiable only by root */
  191. jflag = flags & EXT4_JOURNAL_DATA_FL;
  192. /*
  193. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  194. * the relevant capability.
  195. *
  196. * This test looks nicer. Thanks to Pauline Middelink
  197. */
  198. if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
  199. if (!capable(CAP_LINUX_IMMUTABLE))
  200. goto flags_out;
  201. }
  202. /*
  203. * The JOURNAL_DATA flag can only be changed by
  204. * the relevant capability.
  205. */
  206. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
  207. if (!capable(CAP_SYS_RESOURCE))
  208. goto flags_out;
  209. }
  210. if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
  211. migrate = 1;
  212. if (flags & EXT4_EOFBLOCKS_FL) {
  213. /* we don't support adding EOFBLOCKS flag */
  214. if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
  215. err = -EOPNOTSUPP;
  216. goto flags_out;
  217. }
  218. } else if (oldflags & EXT4_EOFBLOCKS_FL)
  219. ext4_truncate(inode);
  220. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  221. if (IS_ERR(handle)) {
  222. err = PTR_ERR(handle);
  223. goto flags_out;
  224. }
  225. if (IS_SYNC(inode))
  226. ext4_handle_sync(handle);
  227. err = ext4_reserve_inode_write(handle, inode, &iloc);
  228. if (err)
  229. goto flags_err;
  230. for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
  231. if (!(mask & EXT4_FL_USER_MODIFIABLE))
  232. continue;
  233. if (mask & flags)
  234. ext4_set_inode_flag(inode, i);
  235. else
  236. ext4_clear_inode_flag(inode, i);
  237. }
  238. ext4_set_inode_flags(inode);
  239. inode->i_ctime = ext4_current_time(inode);
  240. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  241. flags_err:
  242. ext4_journal_stop(handle);
  243. if (err)
  244. goto flags_out;
  245. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
  246. err = ext4_change_inode_journal_flag(inode, jflag);
  247. if (err)
  248. goto flags_out;
  249. if (migrate) {
  250. if (flags & EXT4_EXTENTS_FL)
  251. err = ext4_ext_migrate(inode);
  252. else
  253. err = ext4_ind_migrate(inode);
  254. }
  255. flags_out:
  256. return err;
  257. }
  258. #ifdef CONFIG_QUOTA
  259. static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
  260. {
  261. struct inode *inode = file_inode(filp);
  262. struct super_block *sb = inode->i_sb;
  263. struct ext4_inode_info *ei = EXT4_I(inode);
  264. int err, rc;
  265. handle_t *handle;
  266. kprojid_t kprojid;
  267. struct ext4_iloc iloc;
  268. struct ext4_inode *raw_inode;
  269. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  270. EXT4_FEATURE_RO_COMPAT_PROJECT)) {
  271. if (projid != EXT4_DEF_PROJID)
  272. return -EOPNOTSUPP;
  273. else
  274. return 0;
  275. }
  276. if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
  277. return -EOPNOTSUPP;
  278. kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
  279. if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
  280. return 0;
  281. err = mnt_want_write_file(filp);
  282. if (err)
  283. return err;
  284. err = -EPERM;
  285. inode_lock(inode);
  286. /* Is it quota file? Do not allow user to mess with it */
  287. if (IS_NOQUOTA(inode))
  288. goto out_unlock;
  289. err = ext4_get_inode_loc(inode, &iloc);
  290. if (err)
  291. goto out_unlock;
  292. raw_inode = ext4_raw_inode(&iloc);
  293. if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
  294. err = -EOVERFLOW;
  295. brelse(iloc.bh);
  296. goto out_unlock;
  297. }
  298. brelse(iloc.bh);
  299. dquot_initialize(inode);
  300. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  301. EXT4_QUOTA_INIT_BLOCKS(sb) +
  302. EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
  303. if (IS_ERR(handle)) {
  304. err = PTR_ERR(handle);
  305. goto out_unlock;
  306. }
  307. err = ext4_reserve_inode_write(handle, inode, &iloc);
  308. if (err)
  309. goto out_stop;
  310. if (sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
  311. struct dquot *transfer_to[MAXQUOTAS] = { };
  312. transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
  313. if (!IS_ERR(transfer_to[PRJQUOTA])) {
  314. err = __dquot_transfer(inode, transfer_to);
  315. dqput(transfer_to[PRJQUOTA]);
  316. if (err)
  317. goto out_dirty;
  318. }
  319. }
  320. EXT4_I(inode)->i_projid = kprojid;
  321. inode->i_ctime = ext4_current_time(inode);
  322. out_dirty:
  323. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  324. if (!err)
  325. err = rc;
  326. out_stop:
  327. ext4_journal_stop(handle);
  328. out_unlock:
  329. inode_unlock(inode);
  330. mnt_drop_write_file(filp);
  331. return err;
  332. }
  333. #else
  334. static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
  335. {
  336. if (projid != EXT4_DEF_PROJID)
  337. return -EOPNOTSUPP;
  338. return 0;
  339. }
  340. #endif
  341. /* Transfer internal flags to xflags */
  342. static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
  343. {
  344. __u32 xflags = 0;
  345. if (iflags & EXT4_SYNC_FL)
  346. xflags |= FS_XFLAG_SYNC;
  347. if (iflags & EXT4_IMMUTABLE_FL)
  348. xflags |= FS_XFLAG_IMMUTABLE;
  349. if (iflags & EXT4_APPEND_FL)
  350. xflags |= FS_XFLAG_APPEND;
  351. if (iflags & EXT4_NODUMP_FL)
  352. xflags |= FS_XFLAG_NODUMP;
  353. if (iflags & EXT4_NOATIME_FL)
  354. xflags |= FS_XFLAG_NOATIME;
  355. if (iflags & EXT4_PROJINHERIT_FL)
  356. xflags |= FS_XFLAG_PROJINHERIT;
  357. return xflags;
  358. }
  359. /* Transfer xflags flags to internal */
  360. static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
  361. {
  362. unsigned long iflags = 0;
  363. if (xflags & FS_XFLAG_SYNC)
  364. iflags |= EXT4_SYNC_FL;
  365. if (xflags & FS_XFLAG_IMMUTABLE)
  366. iflags |= EXT4_IMMUTABLE_FL;
  367. if (xflags & FS_XFLAG_APPEND)
  368. iflags |= EXT4_APPEND_FL;
  369. if (xflags & FS_XFLAG_NODUMP)
  370. iflags |= EXT4_NODUMP_FL;
  371. if (xflags & FS_XFLAG_NOATIME)
  372. iflags |= EXT4_NOATIME_FL;
  373. if (xflags & FS_XFLAG_PROJINHERIT)
  374. iflags |= EXT4_PROJINHERIT_FL;
  375. return iflags;
  376. }
  377. long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  378. {
  379. struct inode *inode = file_inode(filp);
  380. struct super_block *sb = inode->i_sb;
  381. struct ext4_inode_info *ei = EXT4_I(inode);
  382. unsigned int flags;
  383. ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
  384. switch (cmd) {
  385. case EXT4_IOC_GETFLAGS:
  386. ext4_get_inode_flags(ei);
  387. flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
  388. return put_user(flags, (int __user *) arg);
  389. case EXT4_IOC_SETFLAGS: {
  390. int err;
  391. if (!inode_owner_or_capable(inode))
  392. return -EACCES;
  393. if (get_user(flags, (int __user *) arg))
  394. return -EFAULT;
  395. err = mnt_want_write_file(filp);
  396. if (err)
  397. return err;
  398. flags = ext4_mask_flags(inode->i_mode, flags);
  399. inode_lock(inode);
  400. err = ext4_ioctl_setflags(inode, flags);
  401. inode_unlock(inode);
  402. mnt_drop_write_file(filp);
  403. return err;
  404. }
  405. case EXT4_IOC_GETVERSION:
  406. case EXT4_IOC_GETVERSION_OLD:
  407. return put_user(inode->i_generation, (int __user *) arg);
  408. case EXT4_IOC_SETVERSION:
  409. case EXT4_IOC_SETVERSION_OLD: {
  410. handle_t *handle;
  411. struct ext4_iloc iloc;
  412. __u32 generation;
  413. int err;
  414. if (!inode_owner_or_capable(inode))
  415. return -EPERM;
  416. if (ext4_has_metadata_csum(inode->i_sb)) {
  417. ext4_warning(sb, "Setting inode version is not "
  418. "supported with metadata_csum enabled.");
  419. return -ENOTTY;
  420. }
  421. err = mnt_want_write_file(filp);
  422. if (err)
  423. return err;
  424. if (get_user(generation, (int __user *) arg)) {
  425. err = -EFAULT;
  426. goto setversion_out;
  427. }
  428. inode_lock(inode);
  429. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  430. if (IS_ERR(handle)) {
  431. err = PTR_ERR(handle);
  432. goto unlock_out;
  433. }
  434. err = ext4_reserve_inode_write(handle, inode, &iloc);
  435. if (err == 0) {
  436. inode->i_ctime = ext4_current_time(inode);
  437. inode->i_generation = generation;
  438. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  439. }
  440. ext4_journal_stop(handle);
  441. unlock_out:
  442. inode_unlock(inode);
  443. setversion_out:
  444. mnt_drop_write_file(filp);
  445. return err;
  446. }
  447. case EXT4_IOC_GROUP_EXTEND: {
  448. ext4_fsblk_t n_blocks_count;
  449. int err, err2=0;
  450. err = ext4_resize_begin(sb);
  451. if (err)
  452. return err;
  453. if (get_user(n_blocks_count, (__u32 __user *)arg)) {
  454. err = -EFAULT;
  455. goto group_extend_out;
  456. }
  457. if (ext4_has_feature_bigalloc(sb)) {
  458. ext4_msg(sb, KERN_ERR,
  459. "Online resizing not supported with bigalloc");
  460. err = -EOPNOTSUPP;
  461. goto group_extend_out;
  462. }
  463. err = mnt_want_write_file(filp);
  464. if (err)
  465. goto group_extend_out;
  466. err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
  467. if (EXT4_SB(sb)->s_journal) {
  468. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  469. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  470. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  471. }
  472. if (err == 0)
  473. err = err2;
  474. mnt_drop_write_file(filp);
  475. group_extend_out:
  476. ext4_resize_end(sb);
  477. return err;
  478. }
  479. case EXT4_IOC_MOVE_EXT: {
  480. struct move_extent me;
  481. struct fd donor;
  482. int err;
  483. if (!(filp->f_mode & FMODE_READ) ||
  484. !(filp->f_mode & FMODE_WRITE))
  485. return -EBADF;
  486. if (copy_from_user(&me,
  487. (struct move_extent __user *)arg, sizeof(me)))
  488. return -EFAULT;
  489. me.moved_len = 0;
  490. donor = fdget(me.donor_fd);
  491. if (!donor.file)
  492. return -EBADF;
  493. if (!(donor.file->f_mode & FMODE_WRITE)) {
  494. err = -EBADF;
  495. goto mext_out;
  496. }
  497. if (ext4_has_feature_bigalloc(sb)) {
  498. ext4_msg(sb, KERN_ERR,
  499. "Online defrag not supported with bigalloc");
  500. err = -EOPNOTSUPP;
  501. goto mext_out;
  502. } else if (IS_DAX(inode)) {
  503. ext4_msg(sb, KERN_ERR,
  504. "Online defrag not supported with DAX");
  505. err = -EOPNOTSUPP;
  506. goto mext_out;
  507. }
  508. err = mnt_want_write_file(filp);
  509. if (err)
  510. goto mext_out;
  511. err = ext4_move_extents(filp, donor.file, me.orig_start,
  512. me.donor_start, me.len, &me.moved_len);
  513. mnt_drop_write_file(filp);
  514. if (copy_to_user((struct move_extent __user *)arg,
  515. &me, sizeof(me)))
  516. err = -EFAULT;
  517. mext_out:
  518. fdput(donor);
  519. return err;
  520. }
  521. case EXT4_IOC_GROUP_ADD: {
  522. struct ext4_new_group_data input;
  523. int err, err2=0;
  524. err = ext4_resize_begin(sb);
  525. if (err)
  526. return err;
  527. if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
  528. sizeof(input))) {
  529. err = -EFAULT;
  530. goto group_add_out;
  531. }
  532. if (ext4_has_feature_bigalloc(sb)) {
  533. ext4_msg(sb, KERN_ERR,
  534. "Online resizing not supported with bigalloc");
  535. err = -EOPNOTSUPP;
  536. goto group_add_out;
  537. }
  538. err = mnt_want_write_file(filp);
  539. if (err)
  540. goto group_add_out;
  541. err = ext4_group_add(sb, &input);
  542. if (EXT4_SB(sb)->s_journal) {
  543. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  544. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  545. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  546. }
  547. if (err == 0)
  548. err = err2;
  549. mnt_drop_write_file(filp);
  550. if (!err && ext4_has_group_desc_csum(sb) &&
  551. test_opt(sb, INIT_INODE_TABLE))
  552. err = ext4_register_li_request(sb, input.group);
  553. group_add_out:
  554. ext4_resize_end(sb);
  555. return err;
  556. }
  557. case EXT4_IOC_MIGRATE:
  558. {
  559. int err;
  560. if (!inode_owner_or_capable(inode))
  561. return -EACCES;
  562. err = mnt_want_write_file(filp);
  563. if (err)
  564. return err;
  565. /*
  566. * inode_mutex prevent write and truncate on the file.
  567. * Read still goes through. We take i_data_sem in
  568. * ext4_ext_swap_inode_data before we switch the
  569. * inode format to prevent read.
  570. */
  571. inode_lock((inode));
  572. err = ext4_ext_migrate(inode);
  573. inode_unlock((inode));
  574. mnt_drop_write_file(filp);
  575. return err;
  576. }
  577. case EXT4_IOC_ALLOC_DA_BLKS:
  578. {
  579. int err;
  580. if (!inode_owner_or_capable(inode))
  581. return -EACCES;
  582. err = mnt_want_write_file(filp);
  583. if (err)
  584. return err;
  585. err = ext4_alloc_da_blocks(inode);
  586. mnt_drop_write_file(filp);
  587. return err;
  588. }
  589. case EXT4_IOC_SWAP_BOOT:
  590. {
  591. int err;
  592. if (!(filp->f_mode & FMODE_WRITE))
  593. return -EBADF;
  594. err = mnt_want_write_file(filp);
  595. if (err)
  596. return err;
  597. err = swap_inode_boot_loader(sb, inode);
  598. mnt_drop_write_file(filp);
  599. return err;
  600. }
  601. case EXT4_IOC_RESIZE_FS: {
  602. ext4_fsblk_t n_blocks_count;
  603. int err = 0, err2 = 0;
  604. ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
  605. if (ext4_has_feature_bigalloc(sb)) {
  606. ext4_msg(sb, KERN_ERR,
  607. "Online resizing not (yet) supported with bigalloc");
  608. return -EOPNOTSUPP;
  609. }
  610. if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
  611. sizeof(__u64))) {
  612. return -EFAULT;
  613. }
  614. err = ext4_resize_begin(sb);
  615. if (err)
  616. return err;
  617. err = mnt_want_write_file(filp);
  618. if (err)
  619. goto resizefs_out;
  620. err = ext4_resize_fs(sb, n_blocks_count);
  621. if (EXT4_SB(sb)->s_journal) {
  622. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  623. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  624. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  625. }
  626. if (err == 0)
  627. err = err2;
  628. mnt_drop_write_file(filp);
  629. if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
  630. ext4_has_group_desc_csum(sb) &&
  631. test_opt(sb, INIT_INODE_TABLE))
  632. err = ext4_register_li_request(sb, o_group);
  633. resizefs_out:
  634. ext4_resize_end(sb);
  635. return err;
  636. }
  637. case FITRIM:
  638. {
  639. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  640. struct fstrim_range range;
  641. int ret = 0;
  642. if (!capable(CAP_SYS_ADMIN))
  643. return -EPERM;
  644. if (!blk_queue_discard(q))
  645. return -EOPNOTSUPP;
  646. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  647. sizeof(range)))
  648. return -EFAULT;
  649. range.minlen = max((unsigned int)range.minlen,
  650. q->limits.discard_granularity);
  651. ret = ext4_trim_fs(sb, &range);
  652. if (ret < 0)
  653. return ret;
  654. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  655. sizeof(range)))
  656. return -EFAULT;
  657. return 0;
  658. }
  659. case EXT4_IOC_PRECACHE_EXTENTS:
  660. return ext4_ext_precache(inode);
  661. case EXT4_IOC_SET_ENCRYPTION_POLICY: {
  662. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  663. struct ext4_encryption_policy policy;
  664. int err = 0;
  665. if (copy_from_user(&policy,
  666. (struct ext4_encryption_policy __user *)arg,
  667. sizeof(policy))) {
  668. err = -EFAULT;
  669. goto encryption_policy_out;
  670. }
  671. err = ext4_process_policy(&policy, inode);
  672. encryption_policy_out:
  673. return err;
  674. #else
  675. return -EOPNOTSUPP;
  676. #endif
  677. }
  678. case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
  679. int err, err2;
  680. struct ext4_sb_info *sbi = EXT4_SB(sb);
  681. handle_t *handle;
  682. if (!ext4_sb_has_crypto(sb))
  683. return -EOPNOTSUPP;
  684. if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
  685. err = mnt_want_write_file(filp);
  686. if (err)
  687. return err;
  688. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  689. if (IS_ERR(handle)) {
  690. err = PTR_ERR(handle);
  691. goto pwsalt_err_exit;
  692. }
  693. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  694. if (err)
  695. goto pwsalt_err_journal;
  696. generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
  697. err = ext4_handle_dirty_metadata(handle, NULL,
  698. sbi->s_sbh);
  699. pwsalt_err_journal:
  700. err2 = ext4_journal_stop(handle);
  701. if (err2 && !err)
  702. err = err2;
  703. pwsalt_err_exit:
  704. mnt_drop_write_file(filp);
  705. if (err)
  706. return err;
  707. }
  708. if (copy_to_user((void __user *) arg,
  709. sbi->s_es->s_encrypt_pw_salt, 16))
  710. return -EFAULT;
  711. return 0;
  712. }
  713. case EXT4_IOC_GET_ENCRYPTION_POLICY: {
  714. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  715. struct ext4_encryption_policy policy;
  716. int err = 0;
  717. if (!ext4_encrypted_inode(inode))
  718. return -ENOENT;
  719. err = ext4_get_policy(inode, &policy);
  720. if (err)
  721. return err;
  722. if (copy_to_user((void __user *)arg, &policy, sizeof(policy)))
  723. return -EFAULT;
  724. return 0;
  725. #else
  726. return -EOPNOTSUPP;
  727. #endif
  728. }
  729. case EXT4_IOC_FSGETXATTR:
  730. {
  731. struct fsxattr fa;
  732. memset(&fa, 0, sizeof(struct fsxattr));
  733. ext4_get_inode_flags(ei);
  734. fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
  735. if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  736. EXT4_FEATURE_RO_COMPAT_PROJECT)) {
  737. fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
  738. EXT4_I(inode)->i_projid);
  739. }
  740. if (copy_to_user((struct fsxattr __user *)arg,
  741. &fa, sizeof(fa)))
  742. return -EFAULT;
  743. return 0;
  744. }
  745. case EXT4_IOC_FSSETXATTR:
  746. {
  747. struct fsxattr fa;
  748. int err;
  749. if (copy_from_user(&fa, (struct fsxattr __user *)arg,
  750. sizeof(fa)))
  751. return -EFAULT;
  752. /* Make sure caller has proper permission */
  753. if (!inode_owner_or_capable(inode))
  754. return -EACCES;
  755. err = mnt_want_write_file(filp);
  756. if (err)
  757. return err;
  758. flags = ext4_xflags_to_iflags(fa.fsx_xflags);
  759. flags = ext4_mask_flags(inode->i_mode, flags);
  760. inode_lock(inode);
  761. flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
  762. (flags & EXT4_FL_XFLAG_VISIBLE);
  763. err = ext4_ioctl_setflags(inode, flags);
  764. inode_unlock(inode);
  765. mnt_drop_write_file(filp);
  766. if (err)
  767. return err;
  768. err = ext4_ioctl_setproject(filp, fa.fsx_projid);
  769. if (err)
  770. return err;
  771. return 0;
  772. }
  773. default:
  774. return -ENOTTY;
  775. }
  776. }
  777. #ifdef CONFIG_COMPAT
  778. long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  779. {
  780. /* These are just misnamed, they actually get/put from/to user an int */
  781. switch (cmd) {
  782. case EXT4_IOC32_GETFLAGS:
  783. cmd = EXT4_IOC_GETFLAGS;
  784. break;
  785. case EXT4_IOC32_SETFLAGS:
  786. cmd = EXT4_IOC_SETFLAGS;
  787. break;
  788. case EXT4_IOC32_GETVERSION:
  789. cmd = EXT4_IOC_GETVERSION;
  790. break;
  791. case EXT4_IOC32_SETVERSION:
  792. cmd = EXT4_IOC_SETVERSION;
  793. break;
  794. case EXT4_IOC32_GROUP_EXTEND:
  795. cmd = EXT4_IOC_GROUP_EXTEND;
  796. break;
  797. case EXT4_IOC32_GETVERSION_OLD:
  798. cmd = EXT4_IOC_GETVERSION_OLD;
  799. break;
  800. case EXT4_IOC32_SETVERSION_OLD:
  801. cmd = EXT4_IOC_SETVERSION_OLD;
  802. break;
  803. case EXT4_IOC32_GETRSVSZ:
  804. cmd = EXT4_IOC_GETRSVSZ;
  805. break;
  806. case EXT4_IOC32_SETRSVSZ:
  807. cmd = EXT4_IOC_SETRSVSZ;
  808. break;
  809. case EXT4_IOC32_GROUP_ADD: {
  810. struct compat_ext4_new_group_input __user *uinput;
  811. struct ext4_new_group_input input;
  812. mm_segment_t old_fs;
  813. int err;
  814. uinput = compat_ptr(arg);
  815. err = get_user(input.group, &uinput->group);
  816. err |= get_user(input.block_bitmap, &uinput->block_bitmap);
  817. err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
  818. err |= get_user(input.inode_table, &uinput->inode_table);
  819. err |= get_user(input.blocks_count, &uinput->blocks_count);
  820. err |= get_user(input.reserved_blocks,
  821. &uinput->reserved_blocks);
  822. if (err)
  823. return -EFAULT;
  824. old_fs = get_fs();
  825. set_fs(KERNEL_DS);
  826. err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
  827. (unsigned long) &input);
  828. set_fs(old_fs);
  829. return err;
  830. }
  831. case EXT4_IOC_MOVE_EXT:
  832. case EXT4_IOC_RESIZE_FS:
  833. case EXT4_IOC_PRECACHE_EXTENTS:
  834. case EXT4_IOC_SET_ENCRYPTION_POLICY:
  835. case EXT4_IOC_GET_ENCRYPTION_PWSALT:
  836. case EXT4_IOC_GET_ENCRYPTION_POLICY:
  837. break;
  838. default:
  839. return -ENOIOCTLCMD;
  840. }
  841. return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  842. }
  843. #endif