ioctl.c 23 KB

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