ioctl.c 28 KB

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