ioctl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. struct dquot *transfer_to[MAXQUOTAS] = { };
  270. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  271. EXT4_FEATURE_RO_COMPAT_PROJECT)) {
  272. if (projid != EXT4_DEF_PROJID)
  273. return -EOPNOTSUPP;
  274. else
  275. return 0;
  276. }
  277. if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
  278. return -EOPNOTSUPP;
  279. kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
  280. if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
  281. return 0;
  282. err = mnt_want_write_file(filp);
  283. if (err)
  284. return err;
  285. err = -EPERM;
  286. inode_lock(inode);
  287. /* Is it quota file? Do not allow user to mess with it */
  288. if (IS_NOQUOTA(inode))
  289. goto out_unlock;
  290. err = ext4_get_inode_loc(inode, &iloc);
  291. if (err)
  292. goto out_unlock;
  293. raw_inode = ext4_raw_inode(&iloc);
  294. if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
  295. err = -EOVERFLOW;
  296. brelse(iloc.bh);
  297. goto out_unlock;
  298. }
  299. brelse(iloc.bh);
  300. dquot_initialize(inode);
  301. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  302. EXT4_QUOTA_INIT_BLOCKS(sb) +
  303. EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
  304. if (IS_ERR(handle)) {
  305. err = PTR_ERR(handle);
  306. goto out_unlock;
  307. }
  308. err = ext4_reserve_inode_write(handle, inode, &iloc);
  309. if (err)
  310. goto out_stop;
  311. transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
  312. if (!IS_ERR(transfer_to[PRJQUOTA])) {
  313. err = __dquot_transfer(inode, transfer_to);
  314. dqput(transfer_to[PRJQUOTA]);
  315. if (err)
  316. goto out_dirty;
  317. }
  318. EXT4_I(inode)->i_projid = kprojid;
  319. inode->i_ctime = ext4_current_time(inode);
  320. out_dirty:
  321. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  322. if (!err)
  323. err = rc;
  324. out_stop:
  325. ext4_journal_stop(handle);
  326. out_unlock:
  327. inode_unlock(inode);
  328. mnt_drop_write_file(filp);
  329. return err;
  330. }
  331. #else
  332. static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
  333. {
  334. if (projid != EXT4_DEF_PROJID)
  335. return -EOPNOTSUPP;
  336. return 0;
  337. }
  338. #endif
  339. /* Transfer internal flags to xflags */
  340. static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
  341. {
  342. __u32 xflags = 0;
  343. if (iflags & EXT4_SYNC_FL)
  344. xflags |= FS_XFLAG_SYNC;
  345. if (iflags & EXT4_IMMUTABLE_FL)
  346. xflags |= FS_XFLAG_IMMUTABLE;
  347. if (iflags & EXT4_APPEND_FL)
  348. xflags |= FS_XFLAG_APPEND;
  349. if (iflags & EXT4_NODUMP_FL)
  350. xflags |= FS_XFLAG_NODUMP;
  351. if (iflags & EXT4_NOATIME_FL)
  352. xflags |= FS_XFLAG_NOATIME;
  353. if (iflags & EXT4_PROJINHERIT_FL)
  354. xflags |= FS_XFLAG_PROJINHERIT;
  355. return xflags;
  356. }
  357. /* Transfer xflags flags to internal */
  358. static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
  359. {
  360. unsigned long iflags = 0;
  361. if (xflags & FS_XFLAG_SYNC)
  362. iflags |= EXT4_SYNC_FL;
  363. if (xflags & FS_XFLAG_IMMUTABLE)
  364. iflags |= EXT4_IMMUTABLE_FL;
  365. if (xflags & FS_XFLAG_APPEND)
  366. iflags |= EXT4_APPEND_FL;
  367. if (xflags & FS_XFLAG_NODUMP)
  368. iflags |= EXT4_NODUMP_FL;
  369. if (xflags & FS_XFLAG_NOATIME)
  370. iflags |= EXT4_NOATIME_FL;
  371. if (xflags & FS_XFLAG_PROJINHERIT)
  372. iflags |= EXT4_PROJINHERIT_FL;
  373. return iflags;
  374. }
  375. long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  376. {
  377. struct inode *inode = file_inode(filp);
  378. struct super_block *sb = inode->i_sb;
  379. struct ext4_inode_info *ei = EXT4_I(inode);
  380. unsigned int flags;
  381. ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
  382. switch (cmd) {
  383. case EXT4_IOC_GETFLAGS:
  384. ext4_get_inode_flags(ei);
  385. flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
  386. return put_user(flags, (int __user *) arg);
  387. case EXT4_IOC_SETFLAGS: {
  388. int err;
  389. if (!inode_owner_or_capable(inode))
  390. return -EACCES;
  391. if (get_user(flags, (int __user *) arg))
  392. return -EFAULT;
  393. err = mnt_want_write_file(filp);
  394. if (err)
  395. return err;
  396. flags = ext4_mask_flags(inode->i_mode, flags);
  397. inode_lock(inode);
  398. err = ext4_ioctl_setflags(inode, flags);
  399. inode_unlock(inode);
  400. mnt_drop_write_file(filp);
  401. return err;
  402. }
  403. case EXT4_IOC_GETVERSION:
  404. case EXT4_IOC_GETVERSION_OLD:
  405. return put_user(inode->i_generation, (int __user *) arg);
  406. case EXT4_IOC_SETVERSION:
  407. case EXT4_IOC_SETVERSION_OLD: {
  408. handle_t *handle;
  409. struct ext4_iloc iloc;
  410. __u32 generation;
  411. int err;
  412. if (!inode_owner_or_capable(inode))
  413. return -EPERM;
  414. if (ext4_has_metadata_csum(inode->i_sb)) {
  415. ext4_warning(sb, "Setting inode version is not "
  416. "supported with metadata_csum enabled.");
  417. return -ENOTTY;
  418. }
  419. err = mnt_want_write_file(filp);
  420. if (err)
  421. return err;
  422. if (get_user(generation, (int __user *) arg)) {
  423. err = -EFAULT;
  424. goto setversion_out;
  425. }
  426. inode_lock(inode);
  427. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  428. if (IS_ERR(handle)) {
  429. err = PTR_ERR(handle);
  430. goto unlock_out;
  431. }
  432. err = ext4_reserve_inode_write(handle, inode, &iloc);
  433. if (err == 0) {
  434. inode->i_ctime = ext4_current_time(inode);
  435. inode->i_generation = generation;
  436. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  437. }
  438. ext4_journal_stop(handle);
  439. unlock_out:
  440. inode_unlock(inode);
  441. setversion_out:
  442. mnt_drop_write_file(filp);
  443. return err;
  444. }
  445. case EXT4_IOC_GROUP_EXTEND: {
  446. ext4_fsblk_t n_blocks_count;
  447. int err, err2=0;
  448. err = ext4_resize_begin(sb);
  449. if (err)
  450. return err;
  451. if (get_user(n_blocks_count, (__u32 __user *)arg)) {
  452. err = -EFAULT;
  453. goto group_extend_out;
  454. }
  455. if (ext4_has_feature_bigalloc(sb)) {
  456. ext4_msg(sb, KERN_ERR,
  457. "Online resizing not supported with bigalloc");
  458. err = -EOPNOTSUPP;
  459. goto group_extend_out;
  460. }
  461. err = mnt_want_write_file(filp);
  462. if (err)
  463. goto group_extend_out;
  464. err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
  465. if (EXT4_SB(sb)->s_journal) {
  466. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  467. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  468. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  469. }
  470. if (err == 0)
  471. err = err2;
  472. mnt_drop_write_file(filp);
  473. group_extend_out:
  474. ext4_resize_end(sb);
  475. return err;
  476. }
  477. case EXT4_IOC_MOVE_EXT: {
  478. struct move_extent me;
  479. struct fd donor;
  480. int err;
  481. if (!(filp->f_mode & FMODE_READ) ||
  482. !(filp->f_mode & FMODE_WRITE))
  483. return -EBADF;
  484. if (copy_from_user(&me,
  485. (struct move_extent __user *)arg, sizeof(me)))
  486. return -EFAULT;
  487. me.moved_len = 0;
  488. donor = fdget(me.donor_fd);
  489. if (!donor.file)
  490. return -EBADF;
  491. if (!(donor.file->f_mode & FMODE_WRITE)) {
  492. err = -EBADF;
  493. goto mext_out;
  494. }
  495. if (ext4_has_feature_bigalloc(sb)) {
  496. ext4_msg(sb, KERN_ERR,
  497. "Online defrag not supported with bigalloc");
  498. err = -EOPNOTSUPP;
  499. goto mext_out;
  500. } else if (IS_DAX(inode)) {
  501. ext4_msg(sb, KERN_ERR,
  502. "Online defrag not supported with DAX");
  503. err = -EOPNOTSUPP;
  504. goto mext_out;
  505. }
  506. err = mnt_want_write_file(filp);
  507. if (err)
  508. goto mext_out;
  509. err = ext4_move_extents(filp, donor.file, me.orig_start,
  510. me.donor_start, me.len, &me.moved_len);
  511. mnt_drop_write_file(filp);
  512. if (copy_to_user((struct move_extent __user *)arg,
  513. &me, sizeof(me)))
  514. err = -EFAULT;
  515. mext_out:
  516. fdput(donor);
  517. return err;
  518. }
  519. case EXT4_IOC_GROUP_ADD: {
  520. struct ext4_new_group_data input;
  521. int err, err2=0;
  522. err = ext4_resize_begin(sb);
  523. if (err)
  524. return err;
  525. if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
  526. sizeof(input))) {
  527. err = -EFAULT;
  528. goto group_add_out;
  529. }
  530. if (ext4_has_feature_bigalloc(sb)) {
  531. ext4_msg(sb, KERN_ERR,
  532. "Online resizing not supported with bigalloc");
  533. err = -EOPNOTSUPP;
  534. goto group_add_out;
  535. }
  536. err = mnt_want_write_file(filp);
  537. if (err)
  538. goto group_add_out;
  539. err = ext4_group_add(sb, &input);
  540. if (EXT4_SB(sb)->s_journal) {
  541. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  542. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  543. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  544. }
  545. if (err == 0)
  546. err = err2;
  547. mnt_drop_write_file(filp);
  548. if (!err && ext4_has_group_desc_csum(sb) &&
  549. test_opt(sb, INIT_INODE_TABLE))
  550. err = ext4_register_li_request(sb, input.group);
  551. group_add_out:
  552. ext4_resize_end(sb);
  553. return err;
  554. }
  555. case EXT4_IOC_MIGRATE:
  556. {
  557. int err;
  558. if (!inode_owner_or_capable(inode))
  559. return -EACCES;
  560. err = mnt_want_write_file(filp);
  561. if (err)
  562. return err;
  563. /*
  564. * inode_mutex prevent write and truncate on the file.
  565. * Read still goes through. We take i_data_sem in
  566. * ext4_ext_swap_inode_data before we switch the
  567. * inode format to prevent read.
  568. */
  569. inode_lock((inode));
  570. err = ext4_ext_migrate(inode);
  571. inode_unlock((inode));
  572. mnt_drop_write_file(filp);
  573. return err;
  574. }
  575. case EXT4_IOC_ALLOC_DA_BLKS:
  576. {
  577. int err;
  578. if (!inode_owner_or_capable(inode))
  579. return -EACCES;
  580. err = mnt_want_write_file(filp);
  581. if (err)
  582. return err;
  583. err = ext4_alloc_da_blocks(inode);
  584. mnt_drop_write_file(filp);
  585. return err;
  586. }
  587. case EXT4_IOC_SWAP_BOOT:
  588. {
  589. int err;
  590. if (!(filp->f_mode & FMODE_WRITE))
  591. return -EBADF;
  592. err = mnt_want_write_file(filp);
  593. if (err)
  594. return err;
  595. err = swap_inode_boot_loader(sb, inode);
  596. mnt_drop_write_file(filp);
  597. return err;
  598. }
  599. case EXT4_IOC_RESIZE_FS: {
  600. ext4_fsblk_t n_blocks_count;
  601. int err = 0, err2 = 0;
  602. ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
  603. if (ext4_has_feature_bigalloc(sb)) {
  604. ext4_msg(sb, KERN_ERR,
  605. "Online resizing not (yet) supported with bigalloc");
  606. return -EOPNOTSUPP;
  607. }
  608. if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
  609. sizeof(__u64))) {
  610. return -EFAULT;
  611. }
  612. err = ext4_resize_begin(sb);
  613. if (err)
  614. return err;
  615. err = mnt_want_write_file(filp);
  616. if (err)
  617. goto resizefs_out;
  618. err = ext4_resize_fs(sb, n_blocks_count);
  619. if (EXT4_SB(sb)->s_journal) {
  620. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  621. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  622. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  623. }
  624. if (err == 0)
  625. err = err2;
  626. mnt_drop_write_file(filp);
  627. if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
  628. ext4_has_group_desc_csum(sb) &&
  629. test_opt(sb, INIT_INODE_TABLE))
  630. err = ext4_register_li_request(sb, o_group);
  631. resizefs_out:
  632. ext4_resize_end(sb);
  633. return err;
  634. }
  635. case FITRIM:
  636. {
  637. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  638. struct fstrim_range range;
  639. int ret = 0;
  640. if (!capable(CAP_SYS_ADMIN))
  641. return -EPERM;
  642. if (!blk_queue_discard(q))
  643. return -EOPNOTSUPP;
  644. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  645. sizeof(range)))
  646. return -EFAULT;
  647. range.minlen = max((unsigned int)range.minlen,
  648. q->limits.discard_granularity);
  649. ret = ext4_trim_fs(sb, &range);
  650. if (ret < 0)
  651. return ret;
  652. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  653. sizeof(range)))
  654. return -EFAULT;
  655. return 0;
  656. }
  657. case EXT4_IOC_PRECACHE_EXTENTS:
  658. return ext4_ext_precache(inode);
  659. case EXT4_IOC_SET_ENCRYPTION_POLICY: {
  660. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  661. struct fscrypt_policy policy;
  662. if (copy_from_user(&policy,
  663. (struct fscrypt_policy __user *)arg,
  664. sizeof(policy)))
  665. return -EFAULT;
  666. return fscrypt_process_policy(inode, &policy);
  667. #else
  668. return -EOPNOTSUPP;
  669. #endif
  670. }
  671. case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
  672. int err, err2;
  673. struct ext4_sb_info *sbi = EXT4_SB(sb);
  674. handle_t *handle;
  675. if (!ext4_sb_has_crypto(sb))
  676. return -EOPNOTSUPP;
  677. if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
  678. err = mnt_want_write_file(filp);
  679. if (err)
  680. return err;
  681. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  682. if (IS_ERR(handle)) {
  683. err = PTR_ERR(handle);
  684. goto pwsalt_err_exit;
  685. }
  686. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  687. if (err)
  688. goto pwsalt_err_journal;
  689. generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
  690. err = ext4_handle_dirty_metadata(handle, NULL,
  691. sbi->s_sbh);
  692. pwsalt_err_journal:
  693. err2 = ext4_journal_stop(handle);
  694. if (err2 && !err)
  695. err = err2;
  696. pwsalt_err_exit:
  697. mnt_drop_write_file(filp);
  698. if (err)
  699. return err;
  700. }
  701. if (copy_to_user((void __user *) arg,
  702. sbi->s_es->s_encrypt_pw_salt, 16))
  703. return -EFAULT;
  704. return 0;
  705. }
  706. case EXT4_IOC_GET_ENCRYPTION_POLICY: {
  707. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  708. struct fscrypt_policy policy;
  709. int err = 0;
  710. if (!ext4_encrypted_inode(inode))
  711. return -ENOENT;
  712. err = fscrypt_get_policy(inode, &policy);
  713. if (err)
  714. return err;
  715. if (copy_to_user((void __user *)arg, &policy, sizeof(policy)))
  716. return -EFAULT;
  717. return 0;
  718. #else
  719. return -EOPNOTSUPP;
  720. #endif
  721. }
  722. case EXT4_IOC_FSGETXATTR:
  723. {
  724. struct fsxattr fa;
  725. memset(&fa, 0, sizeof(struct fsxattr));
  726. ext4_get_inode_flags(ei);
  727. fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
  728. if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  729. EXT4_FEATURE_RO_COMPAT_PROJECT)) {
  730. fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
  731. EXT4_I(inode)->i_projid);
  732. }
  733. if (copy_to_user((struct fsxattr __user *)arg,
  734. &fa, sizeof(fa)))
  735. return -EFAULT;
  736. return 0;
  737. }
  738. case EXT4_IOC_FSSETXATTR:
  739. {
  740. struct fsxattr fa;
  741. int err;
  742. if (copy_from_user(&fa, (struct fsxattr __user *)arg,
  743. sizeof(fa)))
  744. return -EFAULT;
  745. /* Make sure caller has proper permission */
  746. if (!inode_owner_or_capable(inode))
  747. return -EACCES;
  748. err = mnt_want_write_file(filp);
  749. if (err)
  750. return err;
  751. flags = ext4_xflags_to_iflags(fa.fsx_xflags);
  752. flags = ext4_mask_flags(inode->i_mode, flags);
  753. inode_lock(inode);
  754. flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
  755. (flags & EXT4_FL_XFLAG_VISIBLE);
  756. err = ext4_ioctl_setflags(inode, flags);
  757. inode_unlock(inode);
  758. mnt_drop_write_file(filp);
  759. if (err)
  760. return err;
  761. err = ext4_ioctl_setproject(filp, fa.fsx_projid);
  762. if (err)
  763. return err;
  764. return 0;
  765. }
  766. default:
  767. return -ENOTTY;
  768. }
  769. }
  770. #ifdef CONFIG_COMPAT
  771. long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  772. {
  773. /* These are just misnamed, they actually get/put from/to user an int */
  774. switch (cmd) {
  775. case EXT4_IOC32_GETFLAGS:
  776. cmd = EXT4_IOC_GETFLAGS;
  777. break;
  778. case EXT4_IOC32_SETFLAGS:
  779. cmd = EXT4_IOC_SETFLAGS;
  780. break;
  781. case EXT4_IOC32_GETVERSION:
  782. cmd = EXT4_IOC_GETVERSION;
  783. break;
  784. case EXT4_IOC32_SETVERSION:
  785. cmd = EXT4_IOC_SETVERSION;
  786. break;
  787. case EXT4_IOC32_GROUP_EXTEND:
  788. cmd = EXT4_IOC_GROUP_EXTEND;
  789. break;
  790. case EXT4_IOC32_GETVERSION_OLD:
  791. cmd = EXT4_IOC_GETVERSION_OLD;
  792. break;
  793. case EXT4_IOC32_SETVERSION_OLD:
  794. cmd = EXT4_IOC_SETVERSION_OLD;
  795. break;
  796. case EXT4_IOC32_GETRSVSZ:
  797. cmd = EXT4_IOC_GETRSVSZ;
  798. break;
  799. case EXT4_IOC32_SETRSVSZ:
  800. cmd = EXT4_IOC_SETRSVSZ;
  801. break;
  802. case EXT4_IOC32_GROUP_ADD: {
  803. struct compat_ext4_new_group_input __user *uinput;
  804. struct ext4_new_group_input input;
  805. mm_segment_t old_fs;
  806. int err;
  807. uinput = compat_ptr(arg);
  808. err = get_user(input.group, &uinput->group);
  809. err |= get_user(input.block_bitmap, &uinput->block_bitmap);
  810. err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
  811. err |= get_user(input.inode_table, &uinput->inode_table);
  812. err |= get_user(input.blocks_count, &uinput->blocks_count);
  813. err |= get_user(input.reserved_blocks,
  814. &uinput->reserved_blocks);
  815. if (err)
  816. return -EFAULT;
  817. old_fs = get_fs();
  818. set_fs(KERNEL_DS);
  819. err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
  820. (unsigned long) &input);
  821. set_fs(old_fs);
  822. return err;
  823. }
  824. case EXT4_IOC_MOVE_EXT:
  825. case EXT4_IOC_RESIZE_FS:
  826. case EXT4_IOC_PRECACHE_EXTENTS:
  827. case EXT4_IOC_SET_ENCRYPTION_POLICY:
  828. case EXT4_IOC_GET_ENCRYPTION_PWSALT:
  829. case EXT4_IOC_GET_ENCRYPTION_POLICY:
  830. break;
  831. default:
  832. return -ENOIOCTLCMD;
  833. }
  834. return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  835. }
  836. #endif