ioctl.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/ioctl.c
  4. *
  5. * Copyright (C) 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/capability.h>
  12. #include <linux/time.h>
  13. #include <linux/compat.h>
  14. #include <linux/mount.h>
  15. #include <linux/file.h>
  16. #include <linux/quotaops.h>
  17. #include <linux/random.h>
  18. #include <linux/uuid.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/delay.h>
  21. #include "ext4_jbd2.h"
  22. #include "ext4.h"
  23. #include <linux/fsmap.h>
  24. #include "fsmap.h"
  25. #include <trace/events/ext4.h>
  26. /**
  27. * Swap memory between @a and @b for @len bytes.
  28. *
  29. * @a: pointer to first memory area
  30. * @b: pointer to second memory area
  31. * @len: number of bytes to swap
  32. *
  33. */
  34. static void memswap(void *a, void *b, size_t len)
  35. {
  36. unsigned char *ap, *bp;
  37. ap = (unsigned char *)a;
  38. bp = (unsigned char *)b;
  39. while (len-- > 0) {
  40. swap(*ap, *bp);
  41. ap++;
  42. bp++;
  43. }
  44. }
  45. /**
  46. * Swap i_data and associated attributes between @inode1 and @inode2.
  47. * This function is used for the primary swap between inode1 and inode2
  48. * and also to revert this primary swap in case of errors.
  49. *
  50. * Therefore you have to make sure, that calling this method twice
  51. * will revert all changes.
  52. *
  53. * @inode1: pointer to first inode
  54. * @inode2: pointer to second inode
  55. */
  56. static void swap_inode_data(struct inode *inode1, struct inode *inode2)
  57. {
  58. loff_t isize;
  59. struct ext4_inode_info *ei1;
  60. struct ext4_inode_info *ei2;
  61. ei1 = EXT4_I(inode1);
  62. ei2 = EXT4_I(inode2);
  63. swap(inode1->i_flags, inode2->i_flags);
  64. swap(inode1->i_version, inode2->i_version);
  65. swap(inode1->i_blocks, inode2->i_blocks);
  66. swap(inode1->i_bytes, inode2->i_bytes);
  67. swap(inode1->i_atime, inode2->i_atime);
  68. swap(inode1->i_mtime, inode2->i_mtime);
  69. memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
  70. swap(ei1->i_flags, ei2->i_flags);
  71. swap(ei1->i_disksize, ei2->i_disksize);
  72. ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
  73. ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
  74. isize = i_size_read(inode1);
  75. i_size_write(inode1, i_size_read(inode2));
  76. i_size_write(inode2, isize);
  77. }
  78. /**
  79. * Swap the information from the given @inode and the inode
  80. * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
  81. * important fields of the inodes.
  82. *
  83. * @sb: the super block of the filesystem
  84. * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
  85. *
  86. */
  87. static long swap_inode_boot_loader(struct super_block *sb,
  88. struct inode *inode)
  89. {
  90. handle_t *handle;
  91. int err;
  92. struct inode *inode_bl;
  93. struct ext4_inode_info *ei_bl;
  94. if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
  95. return -EINVAL;
  96. if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
  97. return -EPERM;
  98. inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
  99. if (IS_ERR(inode_bl))
  100. return PTR_ERR(inode_bl);
  101. ei_bl = EXT4_I(inode_bl);
  102. filemap_flush(inode->i_mapping);
  103. filemap_flush(inode_bl->i_mapping);
  104. /* Protect orig inodes against a truncate and make sure,
  105. * that only 1 swap_inode_boot_loader is running. */
  106. lock_two_nondirectories(inode, inode_bl);
  107. truncate_inode_pages(&inode->i_data, 0);
  108. truncate_inode_pages(&inode_bl->i_data, 0);
  109. /* Wait for all existing dio workers */
  110. ext4_inode_block_unlocked_dio(inode);
  111. ext4_inode_block_unlocked_dio(inode_bl);
  112. inode_dio_wait(inode);
  113. inode_dio_wait(inode_bl);
  114. handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
  115. if (IS_ERR(handle)) {
  116. err = -EINVAL;
  117. goto journal_err_out;
  118. }
  119. /* Protect extent tree against block allocations via delalloc */
  120. ext4_double_down_write_data_sem(inode, inode_bl);
  121. if (inode_bl->i_nlink == 0) {
  122. /* this inode has never been used as a BOOT_LOADER */
  123. set_nlink(inode_bl, 1);
  124. i_uid_write(inode_bl, 0);
  125. i_gid_write(inode_bl, 0);
  126. inode_bl->i_flags = 0;
  127. ei_bl->i_flags = 0;
  128. inode_bl->i_version = 1;
  129. i_size_write(inode_bl, 0);
  130. inode_bl->i_mode = S_IFREG;
  131. if (ext4_has_feature_extents(sb)) {
  132. ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
  133. ext4_ext_tree_init(handle, inode_bl);
  134. } else
  135. memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
  136. }
  137. swap_inode_data(inode, inode_bl);
  138. inode->i_ctime = inode_bl->i_ctime = current_time(inode);
  139. inode->i_generation = prandom_u32();
  140. inode_bl->i_generation = prandom_u32();
  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. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  170. static int uuid_is_zero(__u8 u[16])
  171. {
  172. int i;
  173. for (i = 0; i < 16; i++)
  174. if (u[i])
  175. return 0;
  176. return 1;
  177. }
  178. #endif
  179. static int ext4_ioctl_setflags(struct inode *inode,
  180. unsigned int flags)
  181. {
  182. struct ext4_inode_info *ei = EXT4_I(inode);
  183. handle_t *handle = NULL;
  184. int err = -EPERM, migrate = 0;
  185. struct ext4_iloc iloc;
  186. unsigned int oldflags, mask, i;
  187. unsigned int jflag;
  188. /* Is it quota file? Do not allow user to mess with it */
  189. if (ext4_is_quota_file(inode))
  190. goto flags_out;
  191. oldflags = ei->i_flags;
  192. /* The JOURNAL_DATA flag is modifiable only by root */
  193. jflag = flags & EXT4_JOURNAL_DATA_FL;
  194. /*
  195. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  196. * the relevant capability.
  197. *
  198. * This test looks nicer. Thanks to Pauline Middelink
  199. */
  200. if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
  201. if (!capable(CAP_LINUX_IMMUTABLE))
  202. goto flags_out;
  203. }
  204. /*
  205. * The JOURNAL_DATA flag can only be changed by
  206. * the relevant capability.
  207. */
  208. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
  209. if (!capable(CAP_SYS_RESOURCE))
  210. goto flags_out;
  211. }
  212. if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
  213. migrate = 1;
  214. if (flags & EXT4_EOFBLOCKS_FL) {
  215. /* we don't support adding EOFBLOCKS flag */
  216. if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
  217. err = -EOPNOTSUPP;
  218. goto flags_out;
  219. }
  220. } else if (oldflags & EXT4_EOFBLOCKS_FL) {
  221. err = ext4_truncate(inode);
  222. if (err)
  223. goto flags_out;
  224. }
  225. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  226. if (IS_ERR(handle)) {
  227. err = PTR_ERR(handle);
  228. goto flags_out;
  229. }
  230. if (IS_SYNC(inode))
  231. ext4_handle_sync(handle);
  232. err = ext4_reserve_inode_write(handle, inode, &iloc);
  233. if (err)
  234. goto flags_err;
  235. for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
  236. if (!(mask & EXT4_FL_USER_MODIFIABLE))
  237. continue;
  238. /* These flags get special treatment later */
  239. if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
  240. continue;
  241. if (mask & flags)
  242. ext4_set_inode_flag(inode, i);
  243. else
  244. ext4_clear_inode_flag(inode, i);
  245. }
  246. ext4_set_inode_flags(inode);
  247. inode->i_ctime = current_time(inode);
  248. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  249. flags_err:
  250. ext4_journal_stop(handle);
  251. if (err)
  252. goto flags_out;
  253. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
  254. /*
  255. * Changes to the journaling mode can cause unsafe changes to
  256. * S_DAX if we are using the DAX mount option.
  257. */
  258. if (test_opt(inode->i_sb, DAX)) {
  259. err = -EBUSY;
  260. goto flags_out;
  261. }
  262. err = ext4_change_inode_journal_flag(inode, jflag);
  263. if (err)
  264. goto flags_out;
  265. }
  266. if (migrate) {
  267. if (flags & EXT4_EXTENTS_FL)
  268. err = ext4_ext_migrate(inode);
  269. else
  270. err = ext4_ind_migrate(inode);
  271. }
  272. flags_out:
  273. return err;
  274. }
  275. #ifdef CONFIG_QUOTA
  276. static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
  277. {
  278. struct inode *inode = file_inode(filp);
  279. struct super_block *sb = inode->i_sb;
  280. struct ext4_inode_info *ei = EXT4_I(inode);
  281. int err, rc;
  282. handle_t *handle;
  283. kprojid_t kprojid;
  284. struct ext4_iloc iloc;
  285. struct ext4_inode *raw_inode;
  286. struct dquot *transfer_to[MAXQUOTAS] = { };
  287. if (!ext4_has_feature_project(sb)) {
  288. if (projid != EXT4_DEF_PROJID)
  289. return -EOPNOTSUPP;
  290. else
  291. return 0;
  292. }
  293. if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
  294. return -EOPNOTSUPP;
  295. kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
  296. if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
  297. return 0;
  298. err = mnt_want_write_file(filp);
  299. if (err)
  300. return err;
  301. err = -EPERM;
  302. inode_lock(inode);
  303. /* Is it quota file? Do not allow user to mess with it */
  304. if (ext4_is_quota_file(inode))
  305. goto out_unlock;
  306. err = ext4_get_inode_loc(inode, &iloc);
  307. if (err)
  308. goto out_unlock;
  309. raw_inode = ext4_raw_inode(&iloc);
  310. if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
  311. err = ext4_expand_extra_isize(inode,
  312. EXT4_SB(sb)->s_want_extra_isize,
  313. &iloc);
  314. if (err)
  315. goto out_unlock;
  316. } else {
  317. brelse(iloc.bh);
  318. }
  319. dquot_initialize(inode);
  320. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  321. EXT4_QUOTA_INIT_BLOCKS(sb) +
  322. EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
  323. if (IS_ERR(handle)) {
  324. err = PTR_ERR(handle);
  325. goto out_unlock;
  326. }
  327. err = ext4_reserve_inode_write(handle, inode, &iloc);
  328. if (err)
  329. goto out_stop;
  330. transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
  331. if (!IS_ERR(transfer_to[PRJQUOTA])) {
  332. /* __dquot_transfer() calls back ext4_get_inode_usage() which
  333. * counts xattr inode references.
  334. */
  335. down_read(&EXT4_I(inode)->xattr_sem);
  336. err = __dquot_transfer(inode, transfer_to);
  337. up_read(&EXT4_I(inode)->xattr_sem);
  338. dqput(transfer_to[PRJQUOTA]);
  339. if (err)
  340. goto out_dirty;
  341. }
  342. EXT4_I(inode)->i_projid = kprojid;
  343. inode->i_ctime = current_time(inode);
  344. out_dirty:
  345. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  346. if (!err)
  347. err = rc;
  348. out_stop:
  349. ext4_journal_stop(handle);
  350. out_unlock:
  351. inode_unlock(inode);
  352. mnt_drop_write_file(filp);
  353. return err;
  354. }
  355. #else
  356. static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
  357. {
  358. if (projid != EXT4_DEF_PROJID)
  359. return -EOPNOTSUPP;
  360. return 0;
  361. }
  362. #endif
  363. /* Transfer internal flags to xflags */
  364. static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
  365. {
  366. __u32 xflags = 0;
  367. if (iflags & EXT4_SYNC_FL)
  368. xflags |= FS_XFLAG_SYNC;
  369. if (iflags & EXT4_IMMUTABLE_FL)
  370. xflags |= FS_XFLAG_IMMUTABLE;
  371. if (iflags & EXT4_APPEND_FL)
  372. xflags |= FS_XFLAG_APPEND;
  373. if (iflags & EXT4_NODUMP_FL)
  374. xflags |= FS_XFLAG_NODUMP;
  375. if (iflags & EXT4_NOATIME_FL)
  376. xflags |= FS_XFLAG_NOATIME;
  377. if (iflags & EXT4_PROJINHERIT_FL)
  378. xflags |= FS_XFLAG_PROJINHERIT;
  379. return xflags;
  380. }
  381. #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
  382. FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
  383. FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
  384. /* Transfer xflags flags to internal */
  385. static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
  386. {
  387. unsigned long iflags = 0;
  388. if (xflags & FS_XFLAG_SYNC)
  389. iflags |= EXT4_SYNC_FL;
  390. if (xflags & FS_XFLAG_IMMUTABLE)
  391. iflags |= EXT4_IMMUTABLE_FL;
  392. if (xflags & FS_XFLAG_APPEND)
  393. iflags |= EXT4_APPEND_FL;
  394. if (xflags & FS_XFLAG_NODUMP)
  395. iflags |= EXT4_NODUMP_FL;
  396. if (xflags & FS_XFLAG_NOATIME)
  397. iflags |= EXT4_NOATIME_FL;
  398. if (xflags & FS_XFLAG_PROJINHERIT)
  399. iflags |= EXT4_PROJINHERIT_FL;
  400. return iflags;
  401. }
  402. static int ext4_shutdown(struct super_block *sb, unsigned long arg)
  403. {
  404. struct ext4_sb_info *sbi = EXT4_SB(sb);
  405. __u32 flags;
  406. if (!capable(CAP_SYS_ADMIN))
  407. return -EPERM;
  408. if (get_user(flags, (__u32 __user *)arg))
  409. return -EFAULT;
  410. if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
  411. return -EINVAL;
  412. if (ext4_forced_shutdown(sbi))
  413. return 0;
  414. ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
  415. switch (flags) {
  416. case EXT4_GOING_FLAGS_DEFAULT:
  417. freeze_bdev(sb->s_bdev);
  418. set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
  419. thaw_bdev(sb->s_bdev, sb);
  420. break;
  421. case EXT4_GOING_FLAGS_LOGFLUSH:
  422. set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
  423. if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
  424. (void) ext4_force_commit(sb);
  425. jbd2_journal_abort(sbi->s_journal, 0);
  426. }
  427. break;
  428. case EXT4_GOING_FLAGS_NOLOGFLUSH:
  429. set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
  430. if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
  431. msleep(100);
  432. jbd2_journal_abort(sbi->s_journal, 0);
  433. }
  434. break;
  435. default:
  436. return -EINVAL;
  437. }
  438. clear_opt(sb, DISCARD);
  439. return 0;
  440. }
  441. struct getfsmap_info {
  442. struct super_block *gi_sb;
  443. struct fsmap_head __user *gi_data;
  444. unsigned int gi_idx;
  445. __u32 gi_last_flags;
  446. };
  447. static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
  448. {
  449. struct getfsmap_info *info = priv;
  450. struct fsmap fm;
  451. trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
  452. info->gi_last_flags = xfm->fmr_flags;
  453. ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
  454. if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
  455. sizeof(struct fsmap)))
  456. return -EFAULT;
  457. return 0;
  458. }
  459. static int ext4_ioc_getfsmap(struct super_block *sb,
  460. struct fsmap_head __user *arg)
  461. {
  462. struct getfsmap_info info = {0};
  463. struct ext4_fsmap_head xhead = {0};
  464. struct fsmap_head head;
  465. bool aborted = false;
  466. int error;
  467. if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
  468. return -EFAULT;
  469. if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
  470. memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
  471. sizeof(head.fmh_keys[0].fmr_reserved)) ||
  472. memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
  473. sizeof(head.fmh_keys[1].fmr_reserved)))
  474. return -EINVAL;
  475. /*
  476. * ext4 doesn't report file extents at all, so the only valid
  477. * file offsets are the magic ones (all zeroes or all ones).
  478. */
  479. if (head.fmh_keys[0].fmr_offset ||
  480. (head.fmh_keys[1].fmr_offset != 0 &&
  481. head.fmh_keys[1].fmr_offset != -1ULL))
  482. return -EINVAL;
  483. xhead.fmh_iflags = head.fmh_iflags;
  484. xhead.fmh_count = head.fmh_count;
  485. ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
  486. ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
  487. trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
  488. trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
  489. info.gi_sb = sb;
  490. info.gi_data = arg;
  491. error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
  492. if (error == EXT4_QUERY_RANGE_ABORT) {
  493. error = 0;
  494. aborted = true;
  495. } else if (error)
  496. return error;
  497. /* If we didn't abort, set the "last" flag in the last fmx */
  498. if (!aborted && info.gi_idx) {
  499. info.gi_last_flags |= FMR_OF_LAST;
  500. if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
  501. &info.gi_last_flags,
  502. sizeof(info.gi_last_flags)))
  503. return -EFAULT;
  504. }
  505. /* copy back header */
  506. head.fmh_entries = xhead.fmh_entries;
  507. head.fmh_oflags = xhead.fmh_oflags;
  508. if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
  509. return -EFAULT;
  510. return 0;
  511. }
  512. static long ext4_ioctl_group_add(struct file *file,
  513. struct ext4_new_group_data *input)
  514. {
  515. struct super_block *sb = file_inode(file)->i_sb;
  516. int err, err2=0;
  517. err = ext4_resize_begin(sb);
  518. if (err)
  519. return err;
  520. if (ext4_has_feature_bigalloc(sb)) {
  521. ext4_msg(sb, KERN_ERR,
  522. "Online resizing not supported with bigalloc");
  523. err = -EOPNOTSUPP;
  524. goto group_add_out;
  525. }
  526. err = mnt_want_write_file(file);
  527. if (err)
  528. goto group_add_out;
  529. err = ext4_group_add(sb, input);
  530. if (EXT4_SB(sb)->s_journal) {
  531. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  532. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  533. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  534. }
  535. if (err == 0)
  536. err = err2;
  537. mnt_drop_write_file(file);
  538. if (!err && ext4_has_group_desc_csum(sb) &&
  539. test_opt(sb, INIT_INODE_TABLE))
  540. err = ext4_register_li_request(sb, input->group);
  541. group_add_out:
  542. ext4_resize_end(sb);
  543. return err;
  544. }
  545. long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  546. {
  547. struct inode *inode = file_inode(filp);
  548. struct super_block *sb = inode->i_sb;
  549. struct ext4_inode_info *ei = EXT4_I(inode);
  550. unsigned int flags;
  551. ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
  552. switch (cmd) {
  553. case FS_IOC_GETFSMAP:
  554. return ext4_ioc_getfsmap(sb, (void __user *)arg);
  555. case EXT4_IOC_GETFLAGS:
  556. flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
  557. return put_user(flags, (int __user *) arg);
  558. case EXT4_IOC_SETFLAGS: {
  559. int err;
  560. if (!inode_owner_or_capable(inode))
  561. return -EACCES;
  562. if (get_user(flags, (int __user *) arg))
  563. return -EFAULT;
  564. if (flags & ~EXT4_FL_USER_VISIBLE)
  565. return -EOPNOTSUPP;
  566. /*
  567. * chattr(1) grabs flags via GETFLAGS, modifies the result and
  568. * passes that to SETFLAGS. So we cannot easily make SETFLAGS
  569. * more restrictive than just silently masking off visible but
  570. * not settable flags as we always did.
  571. */
  572. flags &= EXT4_FL_USER_MODIFIABLE;
  573. if (ext4_mask_flags(inode->i_mode, flags) != flags)
  574. return -EOPNOTSUPP;
  575. err = mnt_want_write_file(filp);
  576. if (err)
  577. return err;
  578. inode_lock(inode);
  579. err = ext4_ioctl_setflags(inode, flags);
  580. inode_unlock(inode);
  581. mnt_drop_write_file(filp);
  582. return err;
  583. }
  584. case EXT4_IOC_GETVERSION:
  585. case EXT4_IOC_GETVERSION_OLD:
  586. return put_user(inode->i_generation, (int __user *) arg);
  587. case EXT4_IOC_SETVERSION:
  588. case EXT4_IOC_SETVERSION_OLD: {
  589. handle_t *handle;
  590. struct ext4_iloc iloc;
  591. __u32 generation;
  592. int err;
  593. if (!inode_owner_or_capable(inode))
  594. return -EPERM;
  595. if (ext4_has_metadata_csum(inode->i_sb)) {
  596. ext4_warning(sb, "Setting inode version is not "
  597. "supported with metadata_csum enabled.");
  598. return -ENOTTY;
  599. }
  600. err = mnt_want_write_file(filp);
  601. if (err)
  602. return err;
  603. if (get_user(generation, (int __user *) arg)) {
  604. err = -EFAULT;
  605. goto setversion_out;
  606. }
  607. inode_lock(inode);
  608. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  609. if (IS_ERR(handle)) {
  610. err = PTR_ERR(handle);
  611. goto unlock_out;
  612. }
  613. err = ext4_reserve_inode_write(handle, inode, &iloc);
  614. if (err == 0) {
  615. inode->i_ctime = current_time(inode);
  616. inode->i_generation = generation;
  617. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  618. }
  619. ext4_journal_stop(handle);
  620. unlock_out:
  621. inode_unlock(inode);
  622. setversion_out:
  623. mnt_drop_write_file(filp);
  624. return err;
  625. }
  626. case EXT4_IOC_GROUP_EXTEND: {
  627. ext4_fsblk_t n_blocks_count;
  628. int err, err2=0;
  629. err = ext4_resize_begin(sb);
  630. if (err)
  631. return err;
  632. if (get_user(n_blocks_count, (__u32 __user *)arg)) {
  633. err = -EFAULT;
  634. goto group_extend_out;
  635. }
  636. if (ext4_has_feature_bigalloc(sb)) {
  637. ext4_msg(sb, KERN_ERR,
  638. "Online resizing not supported with bigalloc");
  639. err = -EOPNOTSUPP;
  640. goto group_extend_out;
  641. }
  642. err = mnt_want_write_file(filp);
  643. if (err)
  644. goto group_extend_out;
  645. err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
  646. if (EXT4_SB(sb)->s_journal) {
  647. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  648. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  649. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  650. }
  651. if (err == 0)
  652. err = err2;
  653. mnt_drop_write_file(filp);
  654. group_extend_out:
  655. ext4_resize_end(sb);
  656. return err;
  657. }
  658. case EXT4_IOC_MOVE_EXT: {
  659. struct move_extent me;
  660. struct fd donor;
  661. int err;
  662. if (!(filp->f_mode & FMODE_READ) ||
  663. !(filp->f_mode & FMODE_WRITE))
  664. return -EBADF;
  665. if (copy_from_user(&me,
  666. (struct move_extent __user *)arg, sizeof(me)))
  667. return -EFAULT;
  668. me.moved_len = 0;
  669. donor = fdget(me.donor_fd);
  670. if (!donor.file)
  671. return -EBADF;
  672. if (!(donor.file->f_mode & FMODE_WRITE)) {
  673. err = -EBADF;
  674. goto mext_out;
  675. }
  676. if (ext4_has_feature_bigalloc(sb)) {
  677. ext4_msg(sb, KERN_ERR,
  678. "Online defrag not supported with bigalloc");
  679. err = -EOPNOTSUPP;
  680. goto mext_out;
  681. } else if (IS_DAX(inode)) {
  682. ext4_msg(sb, KERN_ERR,
  683. "Online defrag not supported with DAX");
  684. err = -EOPNOTSUPP;
  685. goto mext_out;
  686. }
  687. err = mnt_want_write_file(filp);
  688. if (err)
  689. goto mext_out;
  690. err = ext4_move_extents(filp, donor.file, me.orig_start,
  691. me.donor_start, me.len, &me.moved_len);
  692. mnt_drop_write_file(filp);
  693. if (copy_to_user((struct move_extent __user *)arg,
  694. &me, sizeof(me)))
  695. err = -EFAULT;
  696. mext_out:
  697. fdput(donor);
  698. return err;
  699. }
  700. case EXT4_IOC_GROUP_ADD: {
  701. struct ext4_new_group_data input;
  702. if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
  703. sizeof(input)))
  704. return -EFAULT;
  705. return ext4_ioctl_group_add(filp, &input);
  706. }
  707. case EXT4_IOC_MIGRATE:
  708. {
  709. int err;
  710. if (!inode_owner_or_capable(inode))
  711. return -EACCES;
  712. err = mnt_want_write_file(filp);
  713. if (err)
  714. return err;
  715. /*
  716. * inode_mutex prevent write and truncate on the file.
  717. * Read still goes through. We take i_data_sem in
  718. * ext4_ext_swap_inode_data before we switch the
  719. * inode format to prevent read.
  720. */
  721. inode_lock((inode));
  722. err = ext4_ext_migrate(inode);
  723. inode_unlock((inode));
  724. mnt_drop_write_file(filp);
  725. return err;
  726. }
  727. case EXT4_IOC_ALLOC_DA_BLKS:
  728. {
  729. int err;
  730. if (!inode_owner_or_capable(inode))
  731. return -EACCES;
  732. err = mnt_want_write_file(filp);
  733. if (err)
  734. return err;
  735. err = ext4_alloc_da_blocks(inode);
  736. mnt_drop_write_file(filp);
  737. return err;
  738. }
  739. case EXT4_IOC_SWAP_BOOT:
  740. {
  741. int err;
  742. if (!(filp->f_mode & FMODE_WRITE))
  743. return -EBADF;
  744. err = mnt_want_write_file(filp);
  745. if (err)
  746. return err;
  747. err = swap_inode_boot_loader(sb, inode);
  748. mnt_drop_write_file(filp);
  749. return err;
  750. }
  751. case EXT4_IOC_RESIZE_FS: {
  752. ext4_fsblk_t n_blocks_count;
  753. int err = 0, err2 = 0;
  754. ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
  755. if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
  756. sizeof(__u64))) {
  757. return -EFAULT;
  758. }
  759. err = ext4_resize_begin(sb);
  760. if (err)
  761. return err;
  762. err = mnt_want_write_file(filp);
  763. if (err)
  764. goto resizefs_out;
  765. err = ext4_resize_fs(sb, n_blocks_count);
  766. if (EXT4_SB(sb)->s_journal) {
  767. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  768. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  769. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  770. }
  771. if (err == 0)
  772. err = err2;
  773. mnt_drop_write_file(filp);
  774. if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
  775. ext4_has_group_desc_csum(sb) &&
  776. test_opt(sb, INIT_INODE_TABLE))
  777. err = ext4_register_li_request(sb, o_group);
  778. resizefs_out:
  779. ext4_resize_end(sb);
  780. return err;
  781. }
  782. case FITRIM:
  783. {
  784. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  785. struct fstrim_range range;
  786. int ret = 0;
  787. if (!capable(CAP_SYS_ADMIN))
  788. return -EPERM;
  789. if (!blk_queue_discard(q))
  790. return -EOPNOTSUPP;
  791. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  792. sizeof(range)))
  793. return -EFAULT;
  794. range.minlen = max((unsigned int)range.minlen,
  795. q->limits.discard_granularity);
  796. ret = ext4_trim_fs(sb, &range);
  797. if (ret < 0)
  798. return ret;
  799. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  800. sizeof(range)))
  801. return -EFAULT;
  802. return 0;
  803. }
  804. case EXT4_IOC_PRECACHE_EXTENTS:
  805. return ext4_ext_precache(inode);
  806. case EXT4_IOC_SET_ENCRYPTION_POLICY:
  807. if (!ext4_has_feature_encrypt(sb))
  808. return -EOPNOTSUPP;
  809. return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
  810. case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
  811. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  812. int err, err2;
  813. struct ext4_sb_info *sbi = EXT4_SB(sb);
  814. handle_t *handle;
  815. if (!ext4_has_feature_encrypt(sb))
  816. return -EOPNOTSUPP;
  817. if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
  818. err = mnt_want_write_file(filp);
  819. if (err)
  820. return err;
  821. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  822. if (IS_ERR(handle)) {
  823. err = PTR_ERR(handle);
  824. goto pwsalt_err_exit;
  825. }
  826. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  827. if (err)
  828. goto pwsalt_err_journal;
  829. generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
  830. err = ext4_handle_dirty_metadata(handle, NULL,
  831. sbi->s_sbh);
  832. pwsalt_err_journal:
  833. err2 = ext4_journal_stop(handle);
  834. if (err2 && !err)
  835. err = err2;
  836. pwsalt_err_exit:
  837. mnt_drop_write_file(filp);
  838. if (err)
  839. return err;
  840. }
  841. if (copy_to_user((void __user *) arg,
  842. sbi->s_es->s_encrypt_pw_salt, 16))
  843. return -EFAULT;
  844. return 0;
  845. #else
  846. return -EOPNOTSUPP;
  847. #endif
  848. }
  849. case EXT4_IOC_GET_ENCRYPTION_POLICY:
  850. return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
  851. case EXT4_IOC_FSGETXATTR:
  852. {
  853. struct fsxattr fa;
  854. memset(&fa, 0, sizeof(struct fsxattr));
  855. fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
  856. if (ext4_has_feature_project(inode->i_sb)) {
  857. fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
  858. EXT4_I(inode)->i_projid);
  859. }
  860. if (copy_to_user((struct fsxattr __user *)arg,
  861. &fa, sizeof(fa)))
  862. return -EFAULT;
  863. return 0;
  864. }
  865. case EXT4_IOC_FSSETXATTR:
  866. {
  867. struct fsxattr fa;
  868. int err;
  869. if (copy_from_user(&fa, (struct fsxattr __user *)arg,
  870. sizeof(fa)))
  871. return -EFAULT;
  872. /* Make sure caller has proper permission */
  873. if (!inode_owner_or_capable(inode))
  874. return -EACCES;
  875. if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
  876. return -EOPNOTSUPP;
  877. flags = ext4_xflags_to_iflags(fa.fsx_xflags);
  878. if (ext4_mask_flags(inode->i_mode, flags) != flags)
  879. return -EOPNOTSUPP;
  880. err = mnt_want_write_file(filp);
  881. if (err)
  882. return err;
  883. inode_lock(inode);
  884. flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
  885. (flags & EXT4_FL_XFLAG_VISIBLE);
  886. err = ext4_ioctl_setflags(inode, flags);
  887. inode_unlock(inode);
  888. mnt_drop_write_file(filp);
  889. if (err)
  890. return err;
  891. err = ext4_ioctl_setproject(filp, fa.fsx_projid);
  892. if (err)
  893. return err;
  894. return 0;
  895. }
  896. case EXT4_IOC_SHUTDOWN:
  897. return ext4_shutdown(sb, arg);
  898. default:
  899. return -ENOTTY;
  900. }
  901. }
  902. #ifdef CONFIG_COMPAT
  903. long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  904. {
  905. /* These are just misnamed, they actually get/put from/to user an int */
  906. switch (cmd) {
  907. case EXT4_IOC32_GETFLAGS:
  908. cmd = EXT4_IOC_GETFLAGS;
  909. break;
  910. case EXT4_IOC32_SETFLAGS:
  911. cmd = EXT4_IOC_SETFLAGS;
  912. break;
  913. case EXT4_IOC32_GETVERSION:
  914. cmd = EXT4_IOC_GETVERSION;
  915. break;
  916. case EXT4_IOC32_SETVERSION:
  917. cmd = EXT4_IOC_SETVERSION;
  918. break;
  919. case EXT4_IOC32_GROUP_EXTEND:
  920. cmd = EXT4_IOC_GROUP_EXTEND;
  921. break;
  922. case EXT4_IOC32_GETVERSION_OLD:
  923. cmd = EXT4_IOC_GETVERSION_OLD;
  924. break;
  925. case EXT4_IOC32_SETVERSION_OLD:
  926. cmd = EXT4_IOC_SETVERSION_OLD;
  927. break;
  928. case EXT4_IOC32_GETRSVSZ:
  929. cmd = EXT4_IOC_GETRSVSZ;
  930. break;
  931. case EXT4_IOC32_SETRSVSZ:
  932. cmd = EXT4_IOC_SETRSVSZ;
  933. break;
  934. case EXT4_IOC32_GROUP_ADD: {
  935. struct compat_ext4_new_group_input __user *uinput;
  936. struct ext4_new_group_data input;
  937. int err;
  938. uinput = compat_ptr(arg);
  939. err = get_user(input.group, &uinput->group);
  940. err |= get_user(input.block_bitmap, &uinput->block_bitmap);
  941. err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
  942. err |= get_user(input.inode_table, &uinput->inode_table);
  943. err |= get_user(input.blocks_count, &uinput->blocks_count);
  944. err |= get_user(input.reserved_blocks,
  945. &uinput->reserved_blocks);
  946. if (err)
  947. return -EFAULT;
  948. return ext4_ioctl_group_add(file, &input);
  949. }
  950. case EXT4_IOC_MOVE_EXT:
  951. case EXT4_IOC_RESIZE_FS:
  952. case EXT4_IOC_PRECACHE_EXTENTS:
  953. case EXT4_IOC_SET_ENCRYPTION_POLICY:
  954. case EXT4_IOC_GET_ENCRYPTION_PWSALT:
  955. case EXT4_IOC_GET_ENCRYPTION_POLICY:
  956. case EXT4_IOC_SHUTDOWN:
  957. case FS_IOC_GETFSMAP:
  958. break;
  959. default:
  960. return -ENOIOCTLCMD;
  961. }
  962. return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  963. }
  964. #endif