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. /**
  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. static int uuid_is_zero(__u8 u[16])
  169. {
  170. int i;
  171. for (i = 0; i < 16; i++)
  172. if (u[i])
  173. return 0;
  174. return 1;
  175. }
  176. static int ext4_ioctl_setflags(struct inode *inode,
  177. unsigned int flags)
  178. {
  179. struct ext4_inode_info *ei = EXT4_I(inode);
  180. handle_t *handle = NULL;
  181. int err = -EPERM, migrate = 0;
  182. struct ext4_iloc iloc;
  183. unsigned int oldflags, mask, i;
  184. unsigned int jflag;
  185. /* Is it quota file? Do not allow user to mess with it */
  186. if (IS_NOQUOTA(inode))
  187. goto flags_out;
  188. oldflags = ei->i_flags;
  189. /* The JOURNAL_DATA flag is modifiable only by root */
  190. jflag = flags & EXT4_JOURNAL_DATA_FL;
  191. /*
  192. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  193. * the relevant capability.
  194. *
  195. * This test looks nicer. Thanks to Pauline Middelink
  196. */
  197. if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
  198. if (!capable(CAP_LINUX_IMMUTABLE))
  199. goto flags_out;
  200. }
  201. /*
  202. * The JOURNAL_DATA flag can only be changed by
  203. * the relevant capability.
  204. */
  205. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
  206. if (!capable(CAP_SYS_RESOURCE))
  207. goto flags_out;
  208. }
  209. if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
  210. migrate = 1;
  211. if (flags & EXT4_EOFBLOCKS_FL) {
  212. /* we don't support adding EOFBLOCKS flag */
  213. if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
  214. err = -EOPNOTSUPP;
  215. goto flags_out;
  216. }
  217. } else if (oldflags & EXT4_EOFBLOCKS_FL)
  218. ext4_truncate(inode);
  219. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  220. if (IS_ERR(handle)) {
  221. err = PTR_ERR(handle);
  222. goto flags_out;
  223. }
  224. if (IS_SYNC(inode))
  225. ext4_handle_sync(handle);
  226. err = ext4_reserve_inode_write(handle, inode, &iloc);
  227. if (err)
  228. goto flags_err;
  229. for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
  230. if (!(mask & EXT4_FL_USER_MODIFIABLE))
  231. continue;
  232. if (mask & flags)
  233. ext4_set_inode_flag(inode, i);
  234. else
  235. ext4_clear_inode_flag(inode, i);
  236. }
  237. ext4_set_inode_flags(inode);
  238. inode->i_ctime = ext4_current_time(inode);
  239. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  240. flags_err:
  241. ext4_journal_stop(handle);
  242. if (err)
  243. goto flags_out;
  244. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
  245. err = ext4_change_inode_journal_flag(inode, jflag);
  246. if (err)
  247. goto flags_out;
  248. if (migrate) {
  249. if (flags & EXT4_EXTENTS_FL)
  250. err = ext4_ext_migrate(inode);
  251. else
  252. err = ext4_ind_migrate(inode);
  253. }
  254. flags_out:
  255. return err;
  256. }
  257. #ifdef CONFIG_QUOTA
  258. static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
  259. {
  260. struct inode *inode = file_inode(filp);
  261. struct super_block *sb = inode->i_sb;
  262. struct ext4_inode_info *ei = EXT4_I(inode);
  263. int err, rc;
  264. handle_t *handle;
  265. kprojid_t kprojid;
  266. struct ext4_iloc iloc;
  267. struct ext4_inode *raw_inode;
  268. struct dquot *transfer_to[MAXQUOTAS] = { };
  269. if (!ext4_has_feature_project(sb)) {
  270. if (projid != EXT4_DEF_PROJID)
  271. return -EOPNOTSUPP;
  272. else
  273. return 0;
  274. }
  275. if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
  276. return -EOPNOTSUPP;
  277. kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
  278. if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
  279. return 0;
  280. err = mnt_want_write_file(filp);
  281. if (err)
  282. return err;
  283. err = -EPERM;
  284. inode_lock(inode);
  285. /* Is it quota file? Do not allow user to mess with it */
  286. if (IS_NOQUOTA(inode))
  287. goto out_unlock;
  288. err = ext4_get_inode_loc(inode, &iloc);
  289. if (err)
  290. goto out_unlock;
  291. raw_inode = ext4_raw_inode(&iloc);
  292. if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
  293. err = -EOVERFLOW;
  294. brelse(iloc.bh);
  295. goto out_unlock;
  296. }
  297. brelse(iloc.bh);
  298. dquot_initialize(inode);
  299. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  300. EXT4_QUOTA_INIT_BLOCKS(sb) +
  301. EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
  302. if (IS_ERR(handle)) {
  303. err = PTR_ERR(handle);
  304. goto out_unlock;
  305. }
  306. err = ext4_reserve_inode_write(handle, inode, &iloc);
  307. if (err)
  308. goto out_stop;
  309. transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
  310. if (!IS_ERR(transfer_to[PRJQUOTA])) {
  311. err = __dquot_transfer(inode, transfer_to);
  312. dqput(transfer_to[PRJQUOTA]);
  313. if (err)
  314. goto out_dirty;
  315. }
  316. EXT4_I(inode)->i_projid = kprojid;
  317. inode->i_ctime = ext4_current_time(inode);
  318. out_dirty:
  319. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  320. if (!err)
  321. err = rc;
  322. out_stop:
  323. ext4_journal_stop(handle);
  324. out_unlock:
  325. inode_unlock(inode);
  326. mnt_drop_write_file(filp);
  327. return err;
  328. }
  329. #else
  330. static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
  331. {
  332. if (projid != EXT4_DEF_PROJID)
  333. return -EOPNOTSUPP;
  334. return 0;
  335. }
  336. #endif
  337. /* Transfer internal flags to xflags */
  338. static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
  339. {
  340. __u32 xflags = 0;
  341. if (iflags & EXT4_SYNC_FL)
  342. xflags |= FS_XFLAG_SYNC;
  343. if (iflags & EXT4_IMMUTABLE_FL)
  344. xflags |= FS_XFLAG_IMMUTABLE;
  345. if (iflags & EXT4_APPEND_FL)
  346. xflags |= FS_XFLAG_APPEND;
  347. if (iflags & EXT4_NODUMP_FL)
  348. xflags |= FS_XFLAG_NODUMP;
  349. if (iflags & EXT4_NOATIME_FL)
  350. xflags |= FS_XFLAG_NOATIME;
  351. if (iflags & EXT4_PROJINHERIT_FL)
  352. xflags |= FS_XFLAG_PROJINHERIT;
  353. return xflags;
  354. }
  355. /* Transfer xflags flags to internal */
  356. static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
  357. {
  358. unsigned long iflags = 0;
  359. if (xflags & FS_XFLAG_SYNC)
  360. iflags |= EXT4_SYNC_FL;
  361. if (xflags & FS_XFLAG_IMMUTABLE)
  362. iflags |= EXT4_IMMUTABLE_FL;
  363. if (xflags & FS_XFLAG_APPEND)
  364. iflags |= EXT4_APPEND_FL;
  365. if (xflags & FS_XFLAG_NODUMP)
  366. iflags |= EXT4_NODUMP_FL;
  367. if (xflags & FS_XFLAG_NOATIME)
  368. iflags |= EXT4_NOATIME_FL;
  369. if (xflags & FS_XFLAG_PROJINHERIT)
  370. iflags |= EXT4_PROJINHERIT_FL;
  371. return iflags;
  372. }
  373. long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  374. {
  375. struct inode *inode = file_inode(filp);
  376. struct super_block *sb = inode->i_sb;
  377. struct ext4_inode_info *ei = EXT4_I(inode);
  378. unsigned int flags;
  379. ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
  380. switch (cmd) {
  381. case EXT4_IOC_GETFLAGS:
  382. ext4_get_inode_flags(ei);
  383. flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
  384. return put_user(flags, (int __user *) arg);
  385. case EXT4_IOC_SETFLAGS: {
  386. int err;
  387. if (!inode_owner_or_capable(inode))
  388. return -EACCES;
  389. if (get_user(flags, (int __user *) arg))
  390. return -EFAULT;
  391. err = mnt_want_write_file(filp);
  392. if (err)
  393. return err;
  394. flags = ext4_mask_flags(inode->i_mode, flags);
  395. inode_lock(inode);
  396. err = ext4_ioctl_setflags(inode, flags);
  397. inode_unlock(inode);
  398. mnt_drop_write_file(filp);
  399. return err;
  400. }
  401. case EXT4_IOC_GETVERSION:
  402. case EXT4_IOC_GETVERSION_OLD:
  403. return put_user(inode->i_generation, (int __user *) arg);
  404. case EXT4_IOC_SETVERSION:
  405. case EXT4_IOC_SETVERSION_OLD: {
  406. handle_t *handle;
  407. struct ext4_iloc iloc;
  408. __u32 generation;
  409. int err;
  410. if (!inode_owner_or_capable(inode))
  411. return -EPERM;
  412. if (ext4_has_metadata_csum(inode->i_sb)) {
  413. ext4_warning(sb, "Setting inode version is not "
  414. "supported with metadata_csum enabled.");
  415. return -ENOTTY;
  416. }
  417. err = mnt_want_write_file(filp);
  418. if (err)
  419. return err;
  420. if (get_user(generation, (int __user *) arg)) {
  421. err = -EFAULT;
  422. goto setversion_out;
  423. }
  424. inode_lock(inode);
  425. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  426. if (IS_ERR(handle)) {
  427. err = PTR_ERR(handle);
  428. goto unlock_out;
  429. }
  430. err = ext4_reserve_inode_write(handle, inode, &iloc);
  431. if (err == 0) {
  432. inode->i_ctime = ext4_current_time(inode);
  433. inode->i_generation = generation;
  434. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  435. }
  436. ext4_journal_stop(handle);
  437. unlock_out:
  438. inode_unlock(inode);
  439. setversion_out:
  440. mnt_drop_write_file(filp);
  441. return err;
  442. }
  443. case EXT4_IOC_GROUP_EXTEND: {
  444. ext4_fsblk_t n_blocks_count;
  445. int err, err2=0;
  446. err = ext4_resize_begin(sb);
  447. if (err)
  448. return err;
  449. if (get_user(n_blocks_count, (__u32 __user *)arg)) {
  450. err = -EFAULT;
  451. goto group_extend_out;
  452. }
  453. if (ext4_has_feature_bigalloc(sb)) {
  454. ext4_msg(sb, KERN_ERR,
  455. "Online resizing not supported with bigalloc");
  456. err = -EOPNOTSUPP;
  457. goto group_extend_out;
  458. }
  459. err = mnt_want_write_file(filp);
  460. if (err)
  461. goto group_extend_out;
  462. err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
  463. if (EXT4_SB(sb)->s_journal) {
  464. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  465. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  466. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  467. }
  468. if (err == 0)
  469. err = err2;
  470. mnt_drop_write_file(filp);
  471. group_extend_out:
  472. ext4_resize_end(sb);
  473. return err;
  474. }
  475. case EXT4_IOC_MOVE_EXT: {
  476. struct move_extent me;
  477. struct fd donor;
  478. int err;
  479. if (!(filp->f_mode & FMODE_READ) ||
  480. !(filp->f_mode & FMODE_WRITE))
  481. return -EBADF;
  482. if (copy_from_user(&me,
  483. (struct move_extent __user *)arg, sizeof(me)))
  484. return -EFAULT;
  485. me.moved_len = 0;
  486. donor = fdget(me.donor_fd);
  487. if (!donor.file)
  488. return -EBADF;
  489. if (!(donor.file->f_mode & FMODE_WRITE)) {
  490. err = -EBADF;
  491. goto mext_out;
  492. }
  493. if (ext4_has_feature_bigalloc(sb)) {
  494. ext4_msg(sb, KERN_ERR,
  495. "Online defrag not supported with bigalloc");
  496. err = -EOPNOTSUPP;
  497. goto mext_out;
  498. } else if (IS_DAX(inode)) {
  499. ext4_msg(sb, KERN_ERR,
  500. "Online defrag not supported with DAX");
  501. err = -EOPNOTSUPP;
  502. goto mext_out;
  503. }
  504. err = mnt_want_write_file(filp);
  505. if (err)
  506. goto mext_out;
  507. err = ext4_move_extents(filp, donor.file, me.orig_start,
  508. me.donor_start, me.len, &me.moved_len);
  509. mnt_drop_write_file(filp);
  510. if (copy_to_user((struct move_extent __user *)arg,
  511. &me, sizeof(me)))
  512. err = -EFAULT;
  513. mext_out:
  514. fdput(donor);
  515. return err;
  516. }
  517. case EXT4_IOC_GROUP_ADD: {
  518. struct ext4_new_group_data input;
  519. int err, err2=0;
  520. err = ext4_resize_begin(sb);
  521. if (err)
  522. return err;
  523. if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
  524. sizeof(input))) {
  525. err = -EFAULT;
  526. goto group_add_out;
  527. }
  528. if (ext4_has_feature_bigalloc(sb)) {
  529. ext4_msg(sb, KERN_ERR,
  530. "Online resizing not supported with bigalloc");
  531. err = -EOPNOTSUPP;
  532. goto group_add_out;
  533. }
  534. err = mnt_want_write_file(filp);
  535. if (err)
  536. goto group_add_out;
  537. err = ext4_group_add(sb, &input);
  538. if (EXT4_SB(sb)->s_journal) {
  539. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  540. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  541. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  542. }
  543. if (err == 0)
  544. err = err2;
  545. mnt_drop_write_file(filp);
  546. if (!err && ext4_has_group_desc_csum(sb) &&
  547. test_opt(sb, INIT_INODE_TABLE))
  548. err = ext4_register_li_request(sb, input.group);
  549. group_add_out:
  550. ext4_resize_end(sb);
  551. return err;
  552. }
  553. case EXT4_IOC_MIGRATE:
  554. {
  555. int err;
  556. if (!inode_owner_or_capable(inode))
  557. return -EACCES;
  558. err = mnt_want_write_file(filp);
  559. if (err)
  560. return err;
  561. /*
  562. * inode_mutex prevent write and truncate on the file.
  563. * Read still goes through. We take i_data_sem in
  564. * ext4_ext_swap_inode_data before we switch the
  565. * inode format to prevent read.
  566. */
  567. inode_lock((inode));
  568. err = ext4_ext_migrate(inode);
  569. inode_unlock((inode));
  570. mnt_drop_write_file(filp);
  571. return err;
  572. }
  573. case EXT4_IOC_ALLOC_DA_BLKS:
  574. {
  575. int err;
  576. if (!inode_owner_or_capable(inode))
  577. return -EACCES;
  578. err = mnt_want_write_file(filp);
  579. if (err)
  580. return err;
  581. err = ext4_alloc_da_blocks(inode);
  582. mnt_drop_write_file(filp);
  583. return err;
  584. }
  585. case EXT4_IOC_SWAP_BOOT:
  586. {
  587. int err;
  588. if (!(filp->f_mode & FMODE_WRITE))
  589. return -EBADF;
  590. err = mnt_want_write_file(filp);
  591. if (err)
  592. return err;
  593. err = swap_inode_boot_loader(sb, inode);
  594. mnt_drop_write_file(filp);
  595. return err;
  596. }
  597. case EXT4_IOC_RESIZE_FS: {
  598. ext4_fsblk_t n_blocks_count;
  599. int err = 0, err2 = 0;
  600. ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
  601. if (ext4_has_feature_bigalloc(sb)) {
  602. ext4_msg(sb, KERN_ERR,
  603. "Online resizing not (yet) supported with bigalloc");
  604. return -EOPNOTSUPP;
  605. }
  606. if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
  607. sizeof(__u64))) {
  608. return -EFAULT;
  609. }
  610. err = ext4_resize_begin(sb);
  611. if (err)
  612. return err;
  613. err = mnt_want_write_file(filp);
  614. if (err)
  615. goto resizefs_out;
  616. err = ext4_resize_fs(sb, n_blocks_count);
  617. if (EXT4_SB(sb)->s_journal) {
  618. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  619. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  620. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  621. }
  622. if (err == 0)
  623. err = err2;
  624. mnt_drop_write_file(filp);
  625. if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
  626. ext4_has_group_desc_csum(sb) &&
  627. test_opt(sb, INIT_INODE_TABLE))
  628. err = ext4_register_li_request(sb, o_group);
  629. resizefs_out:
  630. ext4_resize_end(sb);
  631. return err;
  632. }
  633. case FITRIM:
  634. {
  635. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  636. struct fstrim_range range;
  637. int ret = 0;
  638. if (!capable(CAP_SYS_ADMIN))
  639. return -EPERM;
  640. if (!blk_queue_discard(q))
  641. return -EOPNOTSUPP;
  642. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  643. sizeof(range)))
  644. return -EFAULT;
  645. range.minlen = max((unsigned int)range.minlen,
  646. q->limits.discard_granularity);
  647. ret = ext4_trim_fs(sb, &range);
  648. if (ret < 0)
  649. return ret;
  650. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  651. sizeof(range)))
  652. return -EFAULT;
  653. return 0;
  654. }
  655. case EXT4_IOC_PRECACHE_EXTENTS:
  656. return ext4_ext_precache(inode);
  657. case EXT4_IOC_SET_ENCRYPTION_POLICY: {
  658. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  659. struct fscrypt_policy policy;
  660. if (!ext4_has_feature_encrypt(sb))
  661. return -EOPNOTSUPP;
  662. if (copy_from_user(&policy,
  663. (struct fscrypt_policy __user *)arg,
  664. sizeof(policy)))
  665. return -EFAULT;
  666. return fscrypt_process_policy(filp, &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_feature_project(inode->i_sb)) {
  729. fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
  730. EXT4_I(inode)->i_projid);
  731. }
  732. if (copy_to_user((struct fsxattr __user *)arg,
  733. &fa, sizeof(fa)))
  734. return -EFAULT;
  735. return 0;
  736. }
  737. case EXT4_IOC_FSSETXATTR:
  738. {
  739. struct fsxattr fa;
  740. int err;
  741. if (copy_from_user(&fa, (struct fsxattr __user *)arg,
  742. sizeof(fa)))
  743. return -EFAULT;
  744. /* Make sure caller has proper permission */
  745. if (!inode_owner_or_capable(inode))
  746. return -EACCES;
  747. err = mnt_want_write_file(filp);
  748. if (err)
  749. return err;
  750. flags = ext4_xflags_to_iflags(fa.fsx_xflags);
  751. flags = ext4_mask_flags(inode->i_mode, flags);
  752. inode_lock(inode);
  753. flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
  754. (flags & EXT4_FL_XFLAG_VISIBLE);
  755. err = ext4_ioctl_setflags(inode, flags);
  756. inode_unlock(inode);
  757. mnt_drop_write_file(filp);
  758. if (err)
  759. return err;
  760. err = ext4_ioctl_setproject(filp, fa.fsx_projid);
  761. if (err)
  762. return err;
  763. return 0;
  764. }
  765. default:
  766. return -ENOTTY;
  767. }
  768. }
  769. #ifdef CONFIG_COMPAT
  770. long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  771. {
  772. /* These are just misnamed, they actually get/put from/to user an int */
  773. switch (cmd) {
  774. case EXT4_IOC32_GETFLAGS:
  775. cmd = EXT4_IOC_GETFLAGS;
  776. break;
  777. case EXT4_IOC32_SETFLAGS:
  778. cmd = EXT4_IOC_SETFLAGS;
  779. break;
  780. case EXT4_IOC32_GETVERSION:
  781. cmd = EXT4_IOC_GETVERSION;
  782. break;
  783. case EXT4_IOC32_SETVERSION:
  784. cmd = EXT4_IOC_SETVERSION;
  785. break;
  786. case EXT4_IOC32_GROUP_EXTEND:
  787. cmd = EXT4_IOC_GROUP_EXTEND;
  788. break;
  789. case EXT4_IOC32_GETVERSION_OLD:
  790. cmd = EXT4_IOC_GETVERSION_OLD;
  791. break;
  792. case EXT4_IOC32_SETVERSION_OLD:
  793. cmd = EXT4_IOC_SETVERSION_OLD;
  794. break;
  795. case EXT4_IOC32_GETRSVSZ:
  796. cmd = EXT4_IOC_GETRSVSZ;
  797. break;
  798. case EXT4_IOC32_SETRSVSZ:
  799. cmd = EXT4_IOC_SETRSVSZ;
  800. break;
  801. case EXT4_IOC32_GROUP_ADD: {
  802. struct compat_ext4_new_group_input __user *uinput;
  803. struct ext4_new_group_input input;
  804. mm_segment_t old_fs;
  805. int err;
  806. uinput = compat_ptr(arg);
  807. err = get_user(input.group, &uinput->group);
  808. err |= get_user(input.block_bitmap, &uinput->block_bitmap);
  809. err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
  810. err |= get_user(input.inode_table, &uinput->inode_table);
  811. err |= get_user(input.blocks_count, &uinput->blocks_count);
  812. err |= get_user(input.reserved_blocks,
  813. &uinput->reserved_blocks);
  814. if (err)
  815. return -EFAULT;
  816. old_fs = get_fs();
  817. set_fs(KERNEL_DS);
  818. err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
  819. (unsigned long) &input);
  820. set_fs(old_fs);
  821. return err;
  822. }
  823. case EXT4_IOC_MOVE_EXT:
  824. case EXT4_IOC_RESIZE_FS:
  825. case EXT4_IOC_PRECACHE_EXTENTS:
  826. case EXT4_IOC_SET_ENCRYPTION_POLICY:
  827. case EXT4_IOC_GET_ENCRYPTION_PWSALT:
  828. case EXT4_IOC_GET_ENCRYPTION_POLICY:
  829. break;
  830. default:
  831. return -ENOIOCTLCMD;
  832. }
  833. return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  834. }
  835. #endif