ioctl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*
  2. * linux/fs/ext4/ioctl.c
  3. *
  4. * Copyright (C) 1993, 1994, 1995
  5. * Remy Card ([email protected])
  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. /**
  21. * Swap memory between @a and @b for @len bytes.
  22. *
  23. * @a: pointer to first memory area
  24. * @b: pointer to second memory area
  25. * @len: number of bytes to swap
  26. *
  27. */
  28. static void memswap(void *a, void *b, size_t len)
  29. {
  30. unsigned char *ap, *bp;
  31. ap = (unsigned char *)a;
  32. bp = (unsigned char *)b;
  33. while (len-- > 0) {
  34. swap(*ap, *bp);
  35. ap++;
  36. bp++;
  37. }
  38. }
  39. /**
  40. * Swap i_data and associated attributes between @inode1 and @inode2.
  41. * This function is used for the primary swap between inode1 and inode2
  42. * and also to revert this primary swap in case of errors.
  43. *
  44. * Therefore you have to make sure, that calling this method twice
  45. * will revert all changes.
  46. *
  47. * @inode1: pointer to first inode
  48. * @inode2: pointer to second inode
  49. */
  50. static void swap_inode_data(struct inode *inode1, struct inode *inode2)
  51. {
  52. loff_t isize;
  53. struct ext4_inode_info *ei1;
  54. struct ext4_inode_info *ei2;
  55. ei1 = EXT4_I(inode1);
  56. ei2 = EXT4_I(inode2);
  57. memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags));
  58. memswap(&inode1->i_version, &inode2->i_version,
  59. sizeof(inode1->i_version));
  60. memswap(&inode1->i_blocks, &inode2->i_blocks,
  61. sizeof(inode1->i_blocks));
  62. memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes));
  63. memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime));
  64. memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime));
  65. memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
  66. memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags));
  67. memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize));
  68. ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
  69. ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
  70. isize = i_size_read(inode1);
  71. i_size_write(inode1, i_size_read(inode2));
  72. i_size_write(inode2, isize);
  73. }
  74. /**
  75. * Swap the information from the given @inode and the inode
  76. * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
  77. * important fields of the inodes.
  78. *
  79. * @sb: the super block of the filesystem
  80. * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
  81. *
  82. */
  83. static long swap_inode_boot_loader(struct super_block *sb,
  84. struct inode *inode)
  85. {
  86. handle_t *handle;
  87. int err;
  88. struct inode *inode_bl;
  89. struct ext4_inode_info *ei_bl;
  90. struct ext4_sb_info *sbi = EXT4_SB(sb);
  91. if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
  92. return -EINVAL;
  93. if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
  94. return -EPERM;
  95. inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
  96. if (IS_ERR(inode_bl))
  97. return PTR_ERR(inode_bl);
  98. ei_bl = EXT4_I(inode_bl);
  99. filemap_flush(inode->i_mapping);
  100. filemap_flush(inode_bl->i_mapping);
  101. /* Protect orig inodes against a truncate and make sure,
  102. * that only 1 swap_inode_boot_loader is running. */
  103. lock_two_nondirectories(inode, inode_bl);
  104. truncate_inode_pages(&inode->i_data, 0);
  105. truncate_inode_pages(&inode_bl->i_data, 0);
  106. /* Wait for all existing dio workers */
  107. ext4_inode_block_unlocked_dio(inode);
  108. ext4_inode_block_unlocked_dio(inode_bl);
  109. inode_dio_wait(inode);
  110. inode_dio_wait(inode_bl);
  111. handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
  112. if (IS_ERR(handle)) {
  113. err = -EINVAL;
  114. goto journal_err_out;
  115. }
  116. /* Protect extent tree against block allocations via delalloc */
  117. ext4_double_down_write_data_sem(inode, inode_bl);
  118. if (inode_bl->i_nlink == 0) {
  119. /* this inode has never been used as a BOOT_LOADER */
  120. set_nlink(inode_bl, 1);
  121. i_uid_write(inode_bl, 0);
  122. i_gid_write(inode_bl, 0);
  123. inode_bl->i_flags = 0;
  124. ei_bl->i_flags = 0;
  125. inode_bl->i_version = 1;
  126. i_size_write(inode_bl, 0);
  127. inode_bl->i_mode = S_IFREG;
  128. if (ext4_has_feature_extents(sb)) {
  129. ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
  130. ext4_ext_tree_init(handle, inode_bl);
  131. } else
  132. memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
  133. }
  134. swap_inode_data(inode, inode_bl);
  135. inode->i_ctime = inode_bl->i_ctime = ext4_current_time(inode);
  136. spin_lock(&sbi->s_next_gen_lock);
  137. inode->i_generation = sbi->s_next_generation++;
  138. inode_bl->i_generation = sbi->s_next_generation++;
  139. spin_unlock(&sbi->s_next_gen_lock);
  140. ext4_discard_preallocations(inode);
  141. err = ext4_mark_inode_dirty(handle, inode);
  142. if (err < 0) {
  143. ext4_warning(inode->i_sb,
  144. "couldn't mark inode #%lu dirty (err %d)",
  145. inode->i_ino, err);
  146. /* Revert all changes: */
  147. swap_inode_data(inode, inode_bl);
  148. } else {
  149. err = ext4_mark_inode_dirty(handle, inode_bl);
  150. if (err < 0) {
  151. ext4_warning(inode_bl->i_sb,
  152. "couldn't mark inode #%lu dirty (err %d)",
  153. inode_bl->i_ino, err);
  154. /* Revert all changes: */
  155. swap_inode_data(inode, inode_bl);
  156. ext4_mark_inode_dirty(handle, inode);
  157. }
  158. }
  159. ext4_journal_stop(handle);
  160. ext4_double_up_write_data_sem(inode, inode_bl);
  161. journal_err_out:
  162. ext4_inode_resume_unlocked_dio(inode);
  163. ext4_inode_resume_unlocked_dio(inode_bl);
  164. unlock_two_nondirectories(inode, inode_bl);
  165. iput(inode_bl);
  166. return err;
  167. }
  168. #ifdef CONFIG_FS_ENCRYPTION
  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. #endif
  178. static int ext4_ioctl_setflags(struct inode *inode,
  179. unsigned int flags)
  180. {
  181. struct ext4_inode_info *ei = EXT4_I(inode);
  182. handle_t *handle = NULL;
  183. int err = -EPERM, migrate = 0;
  184. struct ext4_iloc iloc;
  185. unsigned int oldflags, mask, i;
  186. unsigned int jflag;
  187. /* Is it quota file? Do not allow user to mess with it */
  188. if (IS_NOQUOTA(inode))
  189. goto flags_out;
  190. oldflags = ei->i_flags;
  191. /* The JOURNAL_DATA flag is modifiable only by root */
  192. jflag = flags & EXT4_JOURNAL_DATA_FL;
  193. /*
  194. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  195. * the relevant capability.
  196. *
  197. * This test looks nicer. Thanks to Pauline Middelink
  198. */
  199. if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
  200. if (!capable(CAP_LINUX_IMMUTABLE))
  201. goto flags_out;
  202. }
  203. /*
  204. * The JOURNAL_DATA flag can only be changed by
  205. * the relevant capability.
  206. */
  207. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
  208. if (!capable(CAP_SYS_RESOURCE))
  209. goto flags_out;
  210. }
  211. if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
  212. migrate = 1;
  213. if (flags & EXT4_EOFBLOCKS_FL) {
  214. /* we don't support adding EOFBLOCKS flag */
  215. if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
  216. err = -EOPNOTSUPP;
  217. goto flags_out;
  218. }
  219. } else if (oldflags & EXT4_EOFBLOCKS_FL)
  220. ext4_truncate(inode);
  221. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  222. if (IS_ERR(handle)) {
  223. err = PTR_ERR(handle);
  224. goto flags_out;
  225. }
  226. if (IS_SYNC(inode))
  227. ext4_handle_sync(handle);
  228. err = ext4_reserve_inode_write(handle, inode, &iloc);
  229. if (err)
  230. goto flags_err;
  231. for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
  232. if (!(mask & EXT4_FL_USER_MODIFIABLE))
  233. continue;
  234. if (mask & flags)
  235. ext4_set_inode_flag(inode, i);
  236. else
  237. ext4_clear_inode_flag(inode, i);
  238. }
  239. ext4_set_inode_flags(inode);
  240. inode->i_ctime = ext4_current_time(inode);
  241. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  242. flags_err:
  243. ext4_journal_stop(handle);
  244. if (err)
  245. goto flags_out;
  246. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
  247. err = ext4_change_inode_journal_flag(inode, jflag);
  248. if (err)
  249. goto flags_out;
  250. if (migrate) {
  251. if (flags & EXT4_EXTENTS_FL)
  252. err = ext4_ext_migrate(inode);
  253. else
  254. err = ext4_ind_migrate(inode);
  255. }
  256. flags_out:
  257. return err;
  258. }
  259. #ifdef CONFIG_QUOTA
  260. static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
  261. {
  262. struct inode *inode = file_inode(filp);
  263. struct super_block *sb = inode->i_sb;
  264. struct ext4_inode_info *ei = EXT4_I(inode);
  265. int err, rc;
  266. handle_t *handle;
  267. kprojid_t kprojid;
  268. struct ext4_iloc iloc;
  269. struct ext4_inode *raw_inode;
  270. struct dquot *transfer_to[MAXQUOTAS] = { };
  271. if (!ext4_has_feature_project(sb)) {
  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. err = dquot_initialize(inode);
  301. if (err)
  302. return err;
  303. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  304. EXT4_QUOTA_INIT_BLOCKS(sb) +
  305. EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
  306. if (IS_ERR(handle)) {
  307. err = PTR_ERR(handle);
  308. goto out_unlock;
  309. }
  310. err = ext4_reserve_inode_write(handle, inode, &iloc);
  311. if (err)
  312. goto out_stop;
  313. transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
  314. if (!IS_ERR(transfer_to[PRJQUOTA])) {
  315. err = __dquot_transfer(inode, transfer_to);
  316. dqput(transfer_to[PRJQUOTA]);
  317. if (err)
  318. goto out_dirty;
  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 FIDTRIM:
  638. case FITRIM:
  639. {
  640. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  641. struct fstrim_range range;
  642. int ret = 0;
  643. int flags = cmd == FIDTRIM ? BLKDEV_DISCARD_SECURE : 0;
  644. if (!capable(CAP_SYS_ADMIN))
  645. return -EPERM;
  646. if (!blk_queue_discard(q))
  647. return -EOPNOTSUPP;
  648. if ((flags & BLKDEV_DISCARD_SECURE) && !blk_queue_secure_erase(q))
  649. return -EOPNOTSUPP;
  650. /*
  651. * We haven't replayed the journal, so we cannot use our
  652. * block-bitmap-guided storage zapping commands.
  653. */
  654. if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb))
  655. return -EROFS;
  656. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  657. sizeof(range)))
  658. return -EFAULT;
  659. range.minlen = max((unsigned int)range.minlen,
  660. q->limits.discard_granularity);
  661. ret = ext4_trim_fs(sb, &range, flags);
  662. if (ret < 0)
  663. return ret;
  664. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  665. sizeof(range)))
  666. return -EFAULT;
  667. return 0;
  668. }
  669. case EXT4_IOC_PRECACHE_EXTENTS:
  670. return ext4_ext_precache(inode);
  671. case EXT4_IOC_SET_ENCRYPTION_POLICY:
  672. if (!ext4_has_feature_encrypt(sb))
  673. return -EOPNOTSUPP;
  674. return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
  675. case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
  676. #ifdef CONFIG_FS_ENCRYPTION
  677. int err, err2;
  678. struct ext4_sb_info *sbi = EXT4_SB(sb);
  679. handle_t *handle;
  680. if (!ext4_has_feature_encrypt(sb))
  681. return -EOPNOTSUPP;
  682. if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
  683. err = mnt_want_write_file(filp);
  684. if (err)
  685. return err;
  686. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  687. if (IS_ERR(handle)) {
  688. err = PTR_ERR(handle);
  689. goto pwsalt_err_exit;
  690. }
  691. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  692. if (err)
  693. goto pwsalt_err_journal;
  694. generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
  695. err = ext4_handle_dirty_metadata(handle, NULL,
  696. sbi->s_sbh);
  697. pwsalt_err_journal:
  698. err2 = ext4_journal_stop(handle);
  699. if (err2 && !err)
  700. err = err2;
  701. pwsalt_err_exit:
  702. mnt_drop_write_file(filp);
  703. if (err)
  704. return err;
  705. }
  706. if (copy_to_user((void __user *) arg,
  707. sbi->s_es->s_encrypt_pw_salt, 16))
  708. return -EFAULT;
  709. return 0;
  710. #else
  711. return -EOPNOTSUPP;
  712. #endif
  713. }
  714. case EXT4_IOC_GET_ENCRYPTION_POLICY:
  715. return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
  716. case EXT4_IOC_FSGETXATTR:
  717. {
  718. struct fsxattr fa;
  719. memset(&fa, 0, sizeof(struct fsxattr));
  720. fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
  721. if (ext4_has_feature_project(inode->i_sb)) {
  722. fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
  723. EXT4_I(inode)->i_projid);
  724. }
  725. if (copy_to_user((struct fsxattr __user *)arg,
  726. &fa, sizeof(fa)))
  727. return -EFAULT;
  728. return 0;
  729. }
  730. case EXT4_IOC_FSSETXATTR:
  731. {
  732. struct fsxattr fa;
  733. int err;
  734. if (copy_from_user(&fa, (struct fsxattr __user *)arg,
  735. sizeof(fa)))
  736. return -EFAULT;
  737. /* Make sure caller has proper permission */
  738. if (!inode_owner_or_capable(inode))
  739. return -EACCES;
  740. err = mnt_want_write_file(filp);
  741. if (err)
  742. return err;
  743. flags = ext4_xflags_to_iflags(fa.fsx_xflags);
  744. flags = ext4_mask_flags(inode->i_mode, flags);
  745. inode_lock(inode);
  746. flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
  747. (flags & EXT4_FL_XFLAG_VISIBLE);
  748. err = ext4_ioctl_setflags(inode, flags);
  749. inode_unlock(inode);
  750. mnt_drop_write_file(filp);
  751. if (err)
  752. return err;
  753. err = ext4_ioctl_setproject(filp, fa.fsx_projid);
  754. if (err)
  755. return err;
  756. return 0;
  757. }
  758. default:
  759. return -ENOTTY;
  760. }
  761. }
  762. #ifdef CONFIG_COMPAT
  763. long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  764. {
  765. /* These are just misnamed, they actually get/put from/to user an int */
  766. switch (cmd) {
  767. case EXT4_IOC32_GETFLAGS:
  768. cmd = EXT4_IOC_GETFLAGS;
  769. break;
  770. case EXT4_IOC32_SETFLAGS:
  771. cmd = EXT4_IOC_SETFLAGS;
  772. break;
  773. case EXT4_IOC32_GETVERSION:
  774. cmd = EXT4_IOC_GETVERSION;
  775. break;
  776. case EXT4_IOC32_SETVERSION:
  777. cmd = EXT4_IOC_SETVERSION;
  778. break;
  779. case EXT4_IOC32_GROUP_EXTEND:
  780. cmd = EXT4_IOC_GROUP_EXTEND;
  781. break;
  782. case EXT4_IOC32_GETVERSION_OLD:
  783. cmd = EXT4_IOC_GETVERSION_OLD;
  784. break;
  785. case EXT4_IOC32_SETVERSION_OLD:
  786. cmd = EXT4_IOC_SETVERSION_OLD;
  787. break;
  788. case EXT4_IOC32_GETRSVSZ:
  789. cmd = EXT4_IOC_GETRSVSZ;
  790. break;
  791. case EXT4_IOC32_SETRSVSZ:
  792. cmd = EXT4_IOC_SETRSVSZ;
  793. break;
  794. case EXT4_IOC32_GROUP_ADD: {
  795. struct compat_ext4_new_group_input __user *uinput;
  796. struct ext4_new_group_input input;
  797. mm_segment_t old_fs;
  798. int err;
  799. uinput = compat_ptr(arg);
  800. err = get_user(input.group, &uinput->group);
  801. err |= get_user(input.block_bitmap, &uinput->block_bitmap);
  802. err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
  803. err |= get_user(input.inode_table, &uinput->inode_table);
  804. err |= get_user(input.blocks_count, &uinput->blocks_count);
  805. err |= get_user(input.reserved_blocks,
  806. &uinput->reserved_blocks);
  807. if (err)
  808. return -EFAULT;
  809. old_fs = get_fs();
  810. set_fs(KERNEL_DS);
  811. err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
  812. (unsigned long) &input);
  813. set_fs(old_fs);
  814. return err;
  815. }
  816. case EXT4_IOC_MOVE_EXT:
  817. case EXT4_IOC_RESIZE_FS:
  818. case EXT4_IOC_PRECACHE_EXTENTS:
  819. case EXT4_IOC_SET_ENCRYPTION_POLICY:
  820. case EXT4_IOC_GET_ENCRYPTION_PWSALT:
  821. case EXT4_IOC_GET_ENCRYPTION_POLICY:
  822. break;
  823. default:
  824. return -ENOIOCTLCMD;
  825. }
  826. return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  827. }
  828. #endif