ioctl.c 23 KB

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