ioctl.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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/uaccess.h>
  19. #include <linux/delay.h>
  20. #include <linux/iversion.h>
  21. #include <linux/fileattr.h>
  22. #include <linux/uuid.h>
  23. #include "ext4_jbd2.h"
  24. #include "ext4.h"
  25. #include <linux/fsmap.h>
  26. #include "fsmap.h"
  27. #include <trace/events/ext4.h>
  28. typedef void ext4_update_sb_callback(struct ext4_super_block *es,
  29. const void *arg);
  30. /*
  31. * Superblock modification callback function for changing file system
  32. * label
  33. */
  34. static void ext4_sb_setlabel(struct ext4_super_block *es, const void *arg)
  35. {
  36. /* Sanity check, this should never happen */
  37. BUILD_BUG_ON(sizeof(es->s_volume_name) < EXT4_LABEL_MAX);
  38. memcpy(es->s_volume_name, (char *)arg, EXT4_LABEL_MAX);
  39. }
  40. /*
  41. * Superblock modification callback function for changing file system
  42. * UUID.
  43. */
  44. static void ext4_sb_setuuid(struct ext4_super_block *es, const void *arg)
  45. {
  46. memcpy(es->s_uuid, (__u8 *)arg, UUID_SIZE);
  47. }
  48. static
  49. int ext4_update_primary_sb(struct super_block *sb, handle_t *handle,
  50. ext4_update_sb_callback func,
  51. const void *arg)
  52. {
  53. int err = 0;
  54. struct ext4_sb_info *sbi = EXT4_SB(sb);
  55. struct buffer_head *bh = sbi->s_sbh;
  56. struct ext4_super_block *es = sbi->s_es;
  57. trace_ext4_update_sb(sb, bh->b_blocknr, 1);
  58. BUFFER_TRACE(bh, "get_write_access");
  59. err = ext4_journal_get_write_access(handle, sb,
  60. bh,
  61. EXT4_JTR_NONE);
  62. if (err)
  63. goto out_err;
  64. lock_buffer(bh);
  65. func(es, arg);
  66. ext4_superblock_csum_set(sb);
  67. unlock_buffer(bh);
  68. if (buffer_write_io_error(bh) || !buffer_uptodate(bh)) {
  69. ext4_msg(sbi->s_sb, KERN_ERR, "previous I/O error to "
  70. "superblock detected");
  71. clear_buffer_write_io_error(bh);
  72. set_buffer_uptodate(bh);
  73. }
  74. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  75. if (err)
  76. goto out_err;
  77. err = sync_dirty_buffer(bh);
  78. out_err:
  79. ext4_std_error(sb, err);
  80. return err;
  81. }
  82. /*
  83. * Update one backup superblock in the group 'grp' using the callback
  84. * function 'func' and argument 'arg'. If the handle is NULL the
  85. * modification is not journalled.
  86. *
  87. * Returns: 0 when no modification was done (no superblock in the group)
  88. * 1 when the modification was successful
  89. * <0 on error
  90. */
  91. static int ext4_update_backup_sb(struct super_block *sb,
  92. handle_t *handle, ext4_group_t grp,
  93. ext4_update_sb_callback func, const void *arg)
  94. {
  95. int err = 0;
  96. ext4_fsblk_t sb_block;
  97. struct buffer_head *bh;
  98. unsigned long offset = 0;
  99. struct ext4_super_block *es;
  100. if (!ext4_bg_has_super(sb, grp))
  101. return 0;
  102. /*
  103. * For the group 0 there is always 1k padding, so we have
  104. * either adjust offset, or sb_block depending on blocksize
  105. */
  106. if (grp == 0) {
  107. sb_block = 1 * EXT4_MIN_BLOCK_SIZE;
  108. offset = do_div(sb_block, sb->s_blocksize);
  109. } else {
  110. sb_block = ext4_group_first_block_no(sb, grp);
  111. offset = 0;
  112. }
  113. trace_ext4_update_sb(sb, sb_block, handle ? 1 : 0);
  114. bh = ext4_sb_bread(sb, sb_block, 0);
  115. if (IS_ERR(bh))
  116. return PTR_ERR(bh);
  117. if (handle) {
  118. BUFFER_TRACE(bh, "get_write_access");
  119. err = ext4_journal_get_write_access(handle, sb,
  120. bh,
  121. EXT4_JTR_NONE);
  122. if (err)
  123. goto out_bh;
  124. }
  125. es = (struct ext4_super_block *) (bh->b_data + offset);
  126. lock_buffer(bh);
  127. if (ext4_has_metadata_csum(sb) &&
  128. es->s_checksum != ext4_superblock_csum(sb, es)) {
  129. ext4_msg(sb, KERN_ERR, "Invalid checksum for backup "
  130. "superblock %llu", sb_block);
  131. unlock_buffer(bh);
  132. goto out_bh;
  133. }
  134. func(es, arg);
  135. if (ext4_has_metadata_csum(sb))
  136. es->s_checksum = ext4_superblock_csum(sb, es);
  137. set_buffer_uptodate(bh);
  138. unlock_buffer(bh);
  139. if (handle) {
  140. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  141. if (err)
  142. goto out_bh;
  143. } else {
  144. BUFFER_TRACE(bh, "marking dirty");
  145. mark_buffer_dirty(bh);
  146. }
  147. err = sync_dirty_buffer(bh);
  148. out_bh:
  149. brelse(bh);
  150. ext4_std_error(sb, err);
  151. return (err) ? err : 1;
  152. }
  153. /*
  154. * Update primary and backup superblocks using the provided function
  155. * func and argument arg.
  156. *
  157. * Only the primary superblock and at most two backup superblock
  158. * modifications are journalled; the rest is modified without journal.
  159. * This is safe because e2fsck will re-write them if there is a problem,
  160. * and we're very unlikely to ever need more than two backups.
  161. */
  162. static
  163. int ext4_update_superblocks_fn(struct super_block *sb,
  164. ext4_update_sb_callback func,
  165. const void *arg)
  166. {
  167. handle_t *handle;
  168. ext4_group_t ngroups;
  169. unsigned int three = 1;
  170. unsigned int five = 5;
  171. unsigned int seven = 7;
  172. int err = 0, ret, i;
  173. ext4_group_t grp, primary_grp;
  174. struct ext4_sb_info *sbi = EXT4_SB(sb);
  175. /*
  176. * We can't update superblocks while the online resize is running
  177. */
  178. if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING,
  179. &sbi->s_ext4_flags)) {
  180. ext4_msg(sb, KERN_ERR, "Can't modify superblock while"
  181. "performing online resize");
  182. return -EBUSY;
  183. }
  184. /*
  185. * We're only going to update primary superblock and two
  186. * backup superblocks in this transaction.
  187. */
  188. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 3);
  189. if (IS_ERR(handle)) {
  190. err = PTR_ERR(handle);
  191. goto out;
  192. }
  193. /* Update primary superblock */
  194. err = ext4_update_primary_sb(sb, handle, func, arg);
  195. if (err) {
  196. ext4_msg(sb, KERN_ERR, "Failed to update primary "
  197. "superblock");
  198. goto out_journal;
  199. }
  200. primary_grp = ext4_get_group_number(sb, sbi->s_sbh->b_blocknr);
  201. ngroups = ext4_get_groups_count(sb);
  202. /*
  203. * Update backup superblocks. We have to start from group 0
  204. * because it might not be where the primary superblock is
  205. * if the fs is mounted with -o sb=<backup_sb_block>
  206. */
  207. i = 0;
  208. grp = 0;
  209. while (grp < ngroups) {
  210. /* Skip primary superblock */
  211. if (grp == primary_grp)
  212. goto next_grp;
  213. ret = ext4_update_backup_sb(sb, handle, grp, func, arg);
  214. if (ret < 0) {
  215. /* Ignore bad checksum; try to update next sb */
  216. if (ret == -EFSBADCRC)
  217. goto next_grp;
  218. err = ret;
  219. goto out_journal;
  220. }
  221. i += ret;
  222. if (handle && i > 1) {
  223. /*
  224. * We're only journalling primary superblock and
  225. * two backup superblocks; the rest is not
  226. * journalled.
  227. */
  228. err = ext4_journal_stop(handle);
  229. if (err)
  230. goto out;
  231. handle = NULL;
  232. }
  233. next_grp:
  234. grp = ext4_list_backups(sb, &three, &five, &seven);
  235. }
  236. out_journal:
  237. if (handle) {
  238. ret = ext4_journal_stop(handle);
  239. if (ret && !err)
  240. err = ret;
  241. }
  242. out:
  243. clear_bit_unlock(EXT4_FLAGS_RESIZING, &sbi->s_ext4_flags);
  244. smp_mb__after_atomic();
  245. return err ? err : 0;
  246. }
  247. /*
  248. * Swap memory between @a and @b for @len bytes.
  249. *
  250. * @a: pointer to first memory area
  251. * @b: pointer to second memory area
  252. * @len: number of bytes to swap
  253. *
  254. */
  255. static void memswap(void *a, void *b, size_t len)
  256. {
  257. unsigned char *ap, *bp;
  258. ap = (unsigned char *)a;
  259. bp = (unsigned char *)b;
  260. while (len-- > 0) {
  261. swap(*ap, *bp);
  262. ap++;
  263. bp++;
  264. }
  265. }
  266. /*
  267. * Swap i_data and associated attributes between @inode1 and @inode2.
  268. * This function is used for the primary swap between inode1 and inode2
  269. * and also to revert this primary swap in case of errors.
  270. *
  271. * Therefore you have to make sure, that calling this method twice
  272. * will revert all changes.
  273. *
  274. * @inode1: pointer to first inode
  275. * @inode2: pointer to second inode
  276. */
  277. static void swap_inode_data(struct inode *inode1, struct inode *inode2)
  278. {
  279. loff_t isize;
  280. struct ext4_inode_info *ei1;
  281. struct ext4_inode_info *ei2;
  282. unsigned long tmp;
  283. struct timespec64 ts1, ts2;
  284. ei1 = EXT4_I(inode1);
  285. ei2 = EXT4_I(inode2);
  286. swap(inode1->i_version, inode2->i_version);
  287. ts1 = inode_get_atime(inode1);
  288. ts2 = inode_get_atime(inode2);
  289. inode_set_atime_to_ts(inode1, ts2);
  290. inode_set_atime_to_ts(inode2, ts1);
  291. ts1 = inode_get_mtime(inode1);
  292. ts2 = inode_get_mtime(inode2);
  293. inode_set_mtime_to_ts(inode1, ts2);
  294. inode_set_mtime_to_ts(inode2, ts1);
  295. memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
  296. tmp = ei1->i_flags & EXT4_FL_SHOULD_SWAP;
  297. ei1->i_flags = (ei2->i_flags & EXT4_FL_SHOULD_SWAP) |
  298. (ei1->i_flags & ~EXT4_FL_SHOULD_SWAP);
  299. ei2->i_flags = tmp | (ei2->i_flags & ~EXT4_FL_SHOULD_SWAP);
  300. swap(ei1->i_disksize, ei2->i_disksize);
  301. ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
  302. ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
  303. isize = i_size_read(inode1);
  304. i_size_write(inode1, i_size_read(inode2));
  305. i_size_write(inode2, isize);
  306. }
  307. void ext4_reset_inode_seed(struct inode *inode)
  308. {
  309. struct ext4_inode_info *ei = EXT4_I(inode);
  310. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  311. __le32 inum = cpu_to_le32(inode->i_ino);
  312. __le32 gen = cpu_to_le32(inode->i_generation);
  313. __u32 csum;
  314. if (!ext4_has_metadata_csum(inode->i_sb))
  315. return;
  316. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum));
  317. ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen));
  318. }
  319. /*
  320. * Swap the information from the given @inode and the inode
  321. * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
  322. * important fields of the inodes.
  323. *
  324. * @sb: the super block of the filesystem
  325. * @idmap: idmap of the mount the inode was found from
  326. * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
  327. *
  328. */
  329. static long swap_inode_boot_loader(struct super_block *sb,
  330. struct mnt_idmap *idmap,
  331. struct inode *inode)
  332. {
  333. handle_t *handle;
  334. int err;
  335. struct inode *inode_bl;
  336. struct ext4_inode_info *ei_bl;
  337. qsize_t size, size_bl, diff;
  338. blkcnt_t blocks;
  339. unsigned short bytes;
  340. inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO,
  341. EXT4_IGET_SPECIAL | EXT4_IGET_BAD);
  342. if (IS_ERR(inode_bl))
  343. return PTR_ERR(inode_bl);
  344. ei_bl = EXT4_I(inode_bl);
  345. /* Protect orig inodes against a truncate and make sure,
  346. * that only 1 swap_inode_boot_loader is running. */
  347. lock_two_nondirectories(inode, inode_bl);
  348. if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
  349. IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
  350. (EXT4_I(inode)->i_flags & EXT4_JOURNAL_DATA_FL) ||
  351. ext4_has_inline_data(inode)) {
  352. err = -EINVAL;
  353. goto journal_err_out;
  354. }
  355. if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
  356. !inode_owner_or_capable(idmap, inode) ||
  357. !capable(CAP_SYS_ADMIN)) {
  358. err = -EPERM;
  359. goto journal_err_out;
  360. }
  361. filemap_invalidate_lock(inode->i_mapping);
  362. err = filemap_write_and_wait(inode->i_mapping);
  363. if (err)
  364. goto err_out;
  365. err = filemap_write_and_wait(inode_bl->i_mapping);
  366. if (err)
  367. goto err_out;
  368. /* Wait for all existing dio workers */
  369. inode_dio_wait(inode);
  370. inode_dio_wait(inode_bl);
  371. truncate_inode_pages(&inode->i_data, 0);
  372. truncate_inode_pages(&inode_bl->i_data, 0);
  373. handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
  374. if (IS_ERR(handle)) {
  375. err = -EINVAL;
  376. goto err_out;
  377. }
  378. ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_SWAP_BOOT, handle);
  379. /* Protect extent tree against block allocations via delalloc */
  380. ext4_double_down_write_data_sem(inode, inode_bl);
  381. if (is_bad_inode(inode_bl) || !S_ISREG(inode_bl->i_mode)) {
  382. /* this inode has never been used as a BOOT_LOADER */
  383. set_nlink(inode_bl, 1);
  384. i_uid_write(inode_bl, 0);
  385. i_gid_write(inode_bl, 0);
  386. inode_bl->i_flags = 0;
  387. ei_bl->i_flags = 0;
  388. inode_set_iversion(inode_bl, 1);
  389. i_size_write(inode_bl, 0);
  390. EXT4_I(inode_bl)->i_disksize = inode_bl->i_size;
  391. inode_bl->i_mode = S_IFREG;
  392. if (ext4_has_feature_extents(sb)) {
  393. ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
  394. ext4_ext_tree_init(handle, inode_bl);
  395. } else
  396. memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
  397. }
  398. err = dquot_initialize(inode);
  399. if (err)
  400. goto err_out1;
  401. size = (qsize_t)(inode->i_blocks) * (1 << 9) + inode->i_bytes;
  402. size_bl = (qsize_t)(inode_bl->i_blocks) * (1 << 9) + inode_bl->i_bytes;
  403. diff = size - size_bl;
  404. swap_inode_data(inode, inode_bl);
  405. inode_set_ctime_current(inode);
  406. inode_set_ctime_current(inode_bl);
  407. inode_inc_iversion(inode);
  408. inode->i_generation = get_random_u32();
  409. inode_bl->i_generation = get_random_u32();
  410. ext4_reset_inode_seed(inode);
  411. ext4_reset_inode_seed(inode_bl);
  412. ext4_discard_preallocations(inode);
  413. err = ext4_mark_inode_dirty(handle, inode);
  414. if (err < 0) {
  415. /* No need to update quota information. */
  416. ext4_warning(inode->i_sb,
  417. "couldn't mark inode #%lu dirty (err %d)",
  418. inode->i_ino, err);
  419. /* Revert all changes: */
  420. swap_inode_data(inode, inode_bl);
  421. ext4_mark_inode_dirty(handle, inode);
  422. goto err_out1;
  423. }
  424. blocks = inode_bl->i_blocks;
  425. bytes = inode_bl->i_bytes;
  426. inode_bl->i_blocks = inode->i_blocks;
  427. inode_bl->i_bytes = inode->i_bytes;
  428. err = ext4_mark_inode_dirty(handle, inode_bl);
  429. if (err < 0) {
  430. /* No need to update quota information. */
  431. ext4_warning(inode_bl->i_sb,
  432. "couldn't mark inode #%lu dirty (err %d)",
  433. inode_bl->i_ino, err);
  434. goto revert;
  435. }
  436. /* Bootloader inode should not be counted into quota information. */
  437. if (diff > 0)
  438. dquot_free_space(inode, diff);
  439. else
  440. err = dquot_alloc_space(inode, -1 * diff);
  441. if (err < 0) {
  442. revert:
  443. /* Revert all changes: */
  444. inode_bl->i_blocks = blocks;
  445. inode_bl->i_bytes = bytes;
  446. swap_inode_data(inode, inode_bl);
  447. ext4_mark_inode_dirty(handle, inode);
  448. ext4_mark_inode_dirty(handle, inode_bl);
  449. }
  450. err_out1:
  451. ext4_journal_stop(handle);
  452. ext4_double_up_write_data_sem(inode, inode_bl);
  453. err_out:
  454. filemap_invalidate_unlock(inode->i_mapping);
  455. journal_err_out:
  456. unlock_two_nondirectories(inode, inode_bl);
  457. iput(inode_bl);
  458. return err;
  459. }
  460. /*
  461. * If immutable is set and we are not clearing it, we're not allowed to change
  462. * anything else in the inode. Don't error out if we're only trying to set
  463. * immutable on an immutable file.
  464. */
  465. static int ext4_ioctl_check_immutable(struct inode *inode, __u32 new_projid,
  466. unsigned int flags)
  467. {
  468. struct ext4_inode_info *ei = EXT4_I(inode);
  469. unsigned int oldflags = ei->i_flags;
  470. if (!(oldflags & EXT4_IMMUTABLE_FL) || !(flags & EXT4_IMMUTABLE_FL))
  471. return 0;
  472. if ((oldflags & ~EXT4_IMMUTABLE_FL) != (flags & ~EXT4_IMMUTABLE_FL))
  473. return -EPERM;
  474. if (ext4_has_feature_project(inode->i_sb) &&
  475. __kprojid_val(ei->i_projid) != new_projid)
  476. return -EPERM;
  477. return 0;
  478. }
  479. static void ext4_dax_dontcache(struct inode *inode, unsigned int flags)
  480. {
  481. struct ext4_inode_info *ei = EXT4_I(inode);
  482. if (S_ISDIR(inode->i_mode))
  483. return;
  484. if (test_opt2(inode->i_sb, DAX_NEVER) ||
  485. test_opt(inode->i_sb, DAX_ALWAYS))
  486. return;
  487. if ((ei->i_flags ^ flags) & EXT4_DAX_FL)
  488. d_mark_dontcache(inode);
  489. }
  490. static bool dax_compatible(struct inode *inode, unsigned int oldflags,
  491. unsigned int flags)
  492. {
  493. /* Allow the DAX flag to be changed on inline directories */
  494. if (S_ISDIR(inode->i_mode)) {
  495. flags &= ~EXT4_INLINE_DATA_FL;
  496. oldflags &= ~EXT4_INLINE_DATA_FL;
  497. }
  498. if (flags & EXT4_DAX_FL) {
  499. if ((oldflags & EXT4_DAX_MUT_EXCL) ||
  500. ext4_test_inode_state(inode,
  501. EXT4_STATE_VERITY_IN_PROGRESS)) {
  502. return false;
  503. }
  504. }
  505. if ((flags & EXT4_DAX_MUT_EXCL) && (oldflags & EXT4_DAX_FL))
  506. return false;
  507. return true;
  508. }
  509. static int ext4_ioctl_setflags(struct inode *inode,
  510. unsigned int flags)
  511. {
  512. struct ext4_inode_info *ei = EXT4_I(inode);
  513. handle_t *handle = NULL;
  514. int err = -EPERM, migrate = 0;
  515. struct ext4_iloc iloc;
  516. unsigned int oldflags, mask, i;
  517. struct super_block *sb = inode->i_sb;
  518. /* Is it quota file? Do not allow user to mess with it */
  519. if (ext4_is_quota_file(inode))
  520. goto flags_out;
  521. oldflags = ei->i_flags;
  522. /*
  523. * The JOURNAL_DATA flag can only be changed by
  524. * the relevant capability.
  525. */
  526. if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
  527. if (!capable(CAP_SYS_RESOURCE))
  528. goto flags_out;
  529. }
  530. if (!dax_compatible(inode, oldflags, flags)) {
  531. err = -EOPNOTSUPP;
  532. goto flags_out;
  533. }
  534. if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
  535. migrate = 1;
  536. if ((flags ^ oldflags) & EXT4_CASEFOLD_FL) {
  537. if (!ext4_has_feature_casefold(sb)) {
  538. err = -EOPNOTSUPP;
  539. goto flags_out;
  540. }
  541. if (!S_ISDIR(inode->i_mode)) {
  542. err = -ENOTDIR;
  543. goto flags_out;
  544. }
  545. if (!ext4_empty_dir(inode)) {
  546. err = -ENOTEMPTY;
  547. goto flags_out;
  548. }
  549. }
  550. /*
  551. * Wait for all pending directio and then flush all the dirty pages
  552. * for this file. The flush marks all the pages readonly, so any
  553. * subsequent attempt to write to the file (particularly mmap pages)
  554. * will come through the filesystem and fail.
  555. */
  556. if (S_ISREG(inode->i_mode) && !IS_IMMUTABLE(inode) &&
  557. (flags & EXT4_IMMUTABLE_FL)) {
  558. inode_dio_wait(inode);
  559. err = filemap_write_and_wait(inode->i_mapping);
  560. if (err)
  561. goto flags_out;
  562. }
  563. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  564. if (IS_ERR(handle)) {
  565. err = PTR_ERR(handle);
  566. goto flags_out;
  567. }
  568. if (IS_SYNC(inode))
  569. ext4_handle_sync(handle);
  570. err = ext4_reserve_inode_write(handle, inode, &iloc);
  571. if (err)
  572. goto flags_err;
  573. ext4_dax_dontcache(inode, flags);
  574. for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
  575. if (!(mask & EXT4_FL_USER_MODIFIABLE))
  576. continue;
  577. /* These flags get special treatment later */
  578. if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
  579. continue;
  580. if (mask & flags)
  581. ext4_set_inode_flag(inode, i);
  582. else
  583. ext4_clear_inode_flag(inode, i);
  584. }
  585. ext4_set_inode_flags(inode, false);
  586. inode_set_ctime_current(inode);
  587. inode_inc_iversion(inode);
  588. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  589. flags_err:
  590. ext4_journal_stop(handle);
  591. if (err)
  592. goto flags_out;
  593. if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
  594. /*
  595. * Changes to the journaling mode can cause unsafe changes to
  596. * S_DAX if the inode is DAX
  597. */
  598. if (IS_DAX(inode)) {
  599. err = -EBUSY;
  600. goto flags_out;
  601. }
  602. err = ext4_change_inode_journal_flag(inode,
  603. flags & EXT4_JOURNAL_DATA_FL);
  604. if (err)
  605. goto flags_out;
  606. }
  607. if (migrate) {
  608. if (flags & EXT4_EXTENTS_FL)
  609. err = ext4_ext_migrate(inode);
  610. else
  611. err = ext4_ind_migrate(inode);
  612. }
  613. flags_out:
  614. return err;
  615. }
  616. #ifdef CONFIG_QUOTA
  617. static int ext4_ioctl_setproject(struct inode *inode, __u32 projid)
  618. {
  619. struct super_block *sb = inode->i_sb;
  620. struct ext4_inode_info *ei = EXT4_I(inode);
  621. int err, rc;
  622. handle_t *handle;
  623. kprojid_t kprojid;
  624. struct ext4_iloc iloc;
  625. struct ext4_inode *raw_inode;
  626. struct dquot *transfer_to[MAXQUOTAS] = { };
  627. if (!ext4_has_feature_project(sb)) {
  628. if (projid != EXT4_DEF_PROJID)
  629. return -EOPNOTSUPP;
  630. else
  631. return 0;
  632. }
  633. if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
  634. return -EOPNOTSUPP;
  635. kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
  636. if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
  637. return 0;
  638. err = -EPERM;
  639. /* Is it quota file? Do not allow user to mess with it */
  640. if (ext4_is_quota_file(inode))
  641. return err;
  642. err = dquot_initialize(inode);
  643. if (err)
  644. return err;
  645. err = ext4_get_inode_loc(inode, &iloc);
  646. if (err)
  647. return err;
  648. raw_inode = ext4_raw_inode(&iloc);
  649. if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
  650. err = ext4_expand_extra_isize(inode,
  651. EXT4_SB(sb)->s_want_extra_isize,
  652. &iloc);
  653. if (err)
  654. return err;
  655. } else {
  656. brelse(iloc.bh);
  657. }
  658. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  659. EXT4_QUOTA_INIT_BLOCKS(sb) +
  660. EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
  661. if (IS_ERR(handle))
  662. return PTR_ERR(handle);
  663. err = ext4_reserve_inode_write(handle, inode, &iloc);
  664. if (err)
  665. goto out_stop;
  666. transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
  667. if (!IS_ERR(transfer_to[PRJQUOTA])) {
  668. /* __dquot_transfer() calls back ext4_get_inode_usage() which
  669. * counts xattr inode references.
  670. */
  671. down_read(&EXT4_I(inode)->xattr_sem);
  672. err = __dquot_transfer(inode, transfer_to);
  673. up_read(&EXT4_I(inode)->xattr_sem);
  674. dqput(transfer_to[PRJQUOTA]);
  675. if (err)
  676. goto out_dirty;
  677. }
  678. EXT4_I(inode)->i_projid = kprojid;
  679. inode_set_ctime_current(inode);
  680. inode_inc_iversion(inode);
  681. out_dirty:
  682. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  683. if (!err)
  684. err = rc;
  685. out_stop:
  686. ext4_journal_stop(handle);
  687. return err;
  688. }
  689. #else
  690. static int ext4_ioctl_setproject(struct inode *inode, __u32 projid)
  691. {
  692. if (projid != EXT4_DEF_PROJID)
  693. return -EOPNOTSUPP;
  694. return 0;
  695. }
  696. #endif
  697. int ext4_force_shutdown(struct super_block *sb, u32 flags)
  698. {
  699. struct ext4_sb_info *sbi = EXT4_SB(sb);
  700. int ret;
  701. if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
  702. return -EINVAL;
  703. if (ext4_forced_shutdown(sb))
  704. return 0;
  705. ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
  706. trace_ext4_shutdown(sb, flags);
  707. switch (flags) {
  708. case EXT4_GOING_FLAGS_DEFAULT:
  709. ret = bdev_freeze(sb->s_bdev);
  710. if (ret)
  711. return ret;
  712. set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
  713. bdev_thaw(sb->s_bdev);
  714. break;
  715. case EXT4_GOING_FLAGS_LOGFLUSH:
  716. set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
  717. if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
  718. (void) ext4_force_commit(sb);
  719. jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
  720. }
  721. break;
  722. case EXT4_GOING_FLAGS_NOLOGFLUSH:
  723. set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
  724. if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
  725. jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
  726. break;
  727. default:
  728. return -EINVAL;
  729. }
  730. clear_opt(sb, DISCARD);
  731. return 0;
  732. }
  733. static int ext4_ioctl_shutdown(struct super_block *sb, unsigned long arg)
  734. {
  735. u32 flags;
  736. if (!capable(CAP_SYS_ADMIN))
  737. return -EPERM;
  738. if (get_user(flags, (__u32 __user *)arg))
  739. return -EFAULT;
  740. return ext4_force_shutdown(sb, flags);
  741. }
  742. struct getfsmap_info {
  743. struct super_block *gi_sb;
  744. struct fsmap_head __user *gi_data;
  745. unsigned int gi_idx;
  746. __u32 gi_last_flags;
  747. };
  748. static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
  749. {
  750. struct getfsmap_info *info = priv;
  751. struct fsmap fm;
  752. trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
  753. info->gi_last_flags = xfm->fmr_flags;
  754. ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
  755. if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
  756. sizeof(struct fsmap)))
  757. return -EFAULT;
  758. return 0;
  759. }
  760. static int ext4_ioc_getfsmap(struct super_block *sb,
  761. struct fsmap_head __user *arg)
  762. {
  763. struct getfsmap_info info = { NULL };
  764. struct ext4_fsmap_head xhead = {0};
  765. struct fsmap_head head;
  766. bool aborted = false;
  767. int error;
  768. if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
  769. return -EFAULT;
  770. if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
  771. memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
  772. sizeof(head.fmh_keys[0].fmr_reserved)) ||
  773. memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
  774. sizeof(head.fmh_keys[1].fmr_reserved)))
  775. return -EINVAL;
  776. /*
  777. * ext4 doesn't report file extents at all, so the only valid
  778. * file offsets are the magic ones (all zeroes or all ones).
  779. */
  780. if (head.fmh_keys[0].fmr_offset ||
  781. (head.fmh_keys[1].fmr_offset != 0 &&
  782. head.fmh_keys[1].fmr_offset != -1ULL))
  783. return -EINVAL;
  784. xhead.fmh_iflags = head.fmh_iflags;
  785. xhead.fmh_count = head.fmh_count;
  786. ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
  787. ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
  788. trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
  789. trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
  790. info.gi_sb = sb;
  791. info.gi_data = arg;
  792. error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
  793. if (error == EXT4_QUERY_RANGE_ABORT)
  794. aborted = true;
  795. else if (error)
  796. return error;
  797. /* If we didn't abort, set the "last" flag in the last fmx */
  798. if (!aborted && info.gi_idx) {
  799. info.gi_last_flags |= FMR_OF_LAST;
  800. if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
  801. &info.gi_last_flags,
  802. sizeof(info.gi_last_flags)))
  803. return -EFAULT;
  804. }
  805. /* copy back header */
  806. head.fmh_entries = xhead.fmh_entries;
  807. head.fmh_oflags = xhead.fmh_oflags;
  808. if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
  809. return -EFAULT;
  810. return 0;
  811. }
  812. static long ext4_ioctl_group_add(struct file *file,
  813. struct ext4_new_group_data *input)
  814. {
  815. struct super_block *sb = file_inode(file)->i_sb;
  816. int err, err2=0;
  817. err = ext4_resize_begin(sb);
  818. if (err)
  819. return err;
  820. if (ext4_has_feature_bigalloc(sb)) {
  821. ext4_msg(sb, KERN_ERR,
  822. "Online resizing not supported with bigalloc");
  823. err = -EOPNOTSUPP;
  824. goto group_add_out;
  825. }
  826. err = mnt_want_write_file(file);
  827. if (err)
  828. goto group_add_out;
  829. err = ext4_group_add(sb, input);
  830. if (EXT4_SB(sb)->s_journal) {
  831. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  832. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0);
  833. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  834. }
  835. if (err == 0)
  836. err = err2;
  837. mnt_drop_write_file(file);
  838. if (!err && ext4_has_group_desc_csum(sb) &&
  839. test_opt(sb, INIT_INODE_TABLE))
  840. err = ext4_register_li_request(sb, input->group);
  841. group_add_out:
  842. err2 = ext4_resize_end(sb, false);
  843. if (err == 0)
  844. err = err2;
  845. return err;
  846. }
  847. int ext4_fileattr_get(struct dentry *dentry, struct fileattr *fa)
  848. {
  849. struct inode *inode = d_inode(dentry);
  850. struct ext4_inode_info *ei = EXT4_I(inode);
  851. u32 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
  852. if (S_ISREG(inode->i_mode))
  853. flags &= ~FS_PROJINHERIT_FL;
  854. fileattr_fill_flags(fa, flags);
  855. if (ext4_has_feature_project(inode->i_sb))
  856. fa->fsx_projid = from_kprojid(&init_user_ns, ei->i_projid);
  857. return 0;
  858. }
  859. int ext4_fileattr_set(struct mnt_idmap *idmap,
  860. struct dentry *dentry, struct fileattr *fa)
  861. {
  862. struct inode *inode = d_inode(dentry);
  863. u32 flags = fa->flags;
  864. int err = -EOPNOTSUPP;
  865. if (flags & ~EXT4_FL_USER_VISIBLE)
  866. goto out;
  867. /*
  868. * chattr(1) grabs flags via GETFLAGS, modifies the result and
  869. * passes that to SETFLAGS. So we cannot easily make SETFLAGS
  870. * more restrictive than just silently masking off visible but
  871. * not settable flags as we always did.
  872. */
  873. flags &= EXT4_FL_USER_MODIFIABLE;
  874. if (ext4_mask_flags(inode->i_mode, flags) != flags)
  875. goto out;
  876. err = ext4_ioctl_check_immutable(inode, fa->fsx_projid, flags);
  877. if (err)
  878. goto out;
  879. err = ext4_ioctl_setflags(inode, flags);
  880. if (err)
  881. goto out;
  882. err = ext4_ioctl_setproject(inode, fa->fsx_projid);
  883. out:
  884. return err;
  885. }
  886. /* So that the fiemap access checks can't overflow on 32 bit machines. */
  887. #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
  888. static int ext4_ioctl_get_es_cache(struct file *filp, unsigned long arg)
  889. {
  890. struct fiemap fiemap;
  891. struct fiemap __user *ufiemap = (struct fiemap __user *) arg;
  892. struct fiemap_extent_info fieinfo = { 0, };
  893. struct inode *inode = file_inode(filp);
  894. int error;
  895. if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap)))
  896. return -EFAULT;
  897. if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
  898. return -EINVAL;
  899. fieinfo.fi_flags = fiemap.fm_flags;
  900. fieinfo.fi_extents_max = fiemap.fm_extent_count;
  901. fieinfo.fi_extents_start = ufiemap->fm_extents;
  902. error = ext4_get_es_cache(inode, &fieinfo, fiemap.fm_start,
  903. fiemap.fm_length);
  904. fiemap.fm_flags = fieinfo.fi_flags;
  905. fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
  906. if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap)))
  907. error = -EFAULT;
  908. return error;
  909. }
  910. static int ext4_ioctl_checkpoint(struct file *filp, unsigned long arg)
  911. {
  912. int err = 0;
  913. __u32 flags = 0;
  914. unsigned int flush_flags = 0;
  915. struct super_block *sb = file_inode(filp)->i_sb;
  916. if (copy_from_user(&flags, (__u32 __user *)arg,
  917. sizeof(__u32)))
  918. return -EFAULT;
  919. if (!capable(CAP_SYS_ADMIN))
  920. return -EPERM;
  921. /* check for invalid bits set */
  922. if ((flags & ~EXT4_IOC_CHECKPOINT_FLAG_VALID) ||
  923. ((flags & JBD2_JOURNAL_FLUSH_DISCARD) &&
  924. (flags & JBD2_JOURNAL_FLUSH_ZEROOUT)))
  925. return -EINVAL;
  926. if (!EXT4_SB(sb)->s_journal)
  927. return -ENODEV;
  928. if ((flags & JBD2_JOURNAL_FLUSH_DISCARD) &&
  929. !bdev_max_discard_sectors(EXT4_SB(sb)->s_journal->j_dev))
  930. return -EOPNOTSUPP;
  931. if (flags & EXT4_IOC_CHECKPOINT_FLAG_DRY_RUN)
  932. return 0;
  933. if (flags & EXT4_IOC_CHECKPOINT_FLAG_DISCARD)
  934. flush_flags |= JBD2_JOURNAL_FLUSH_DISCARD;
  935. if (flags & EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT) {
  936. flush_flags |= JBD2_JOURNAL_FLUSH_ZEROOUT;
  937. pr_info_ratelimited("warning: checkpointing journal with EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT can be slow");
  938. }
  939. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  940. err = jbd2_journal_flush(EXT4_SB(sb)->s_journal, flush_flags);
  941. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  942. return err;
  943. }
  944. static int ext4_ioctl_setlabel(struct file *filp, const char __user *user_label)
  945. {
  946. size_t len;
  947. int ret = 0;
  948. char new_label[EXT4_LABEL_MAX + 1];
  949. struct super_block *sb = file_inode(filp)->i_sb;
  950. if (!capable(CAP_SYS_ADMIN))
  951. return -EPERM;
  952. /*
  953. * Copy the maximum length allowed for ext4 label with one more to
  954. * find the required terminating null byte in order to test the
  955. * label length. The on disk label doesn't need to be null terminated.
  956. */
  957. if (copy_from_user(new_label, user_label, EXT4_LABEL_MAX + 1))
  958. return -EFAULT;
  959. len = strnlen(new_label, EXT4_LABEL_MAX + 1);
  960. if (len > EXT4_LABEL_MAX)
  961. return -EINVAL;
  962. /*
  963. * Clear the buffer after the new label
  964. */
  965. memset(new_label + len, 0, EXT4_LABEL_MAX - len);
  966. ret = mnt_want_write_file(filp);
  967. if (ret)
  968. return ret;
  969. ret = ext4_update_superblocks_fn(sb, ext4_sb_setlabel, new_label);
  970. mnt_drop_write_file(filp);
  971. return ret;
  972. }
  973. static int ext4_ioctl_getlabel(struct ext4_sb_info *sbi, char __user *user_label)
  974. {
  975. char label[EXT4_LABEL_MAX + 1];
  976. /*
  977. * EXT4_LABEL_MAX must always be smaller than FSLABEL_MAX because
  978. * FSLABEL_MAX must include terminating null byte, while s_volume_name
  979. * does not have to.
  980. */
  981. BUILD_BUG_ON(EXT4_LABEL_MAX >= FSLABEL_MAX);
  982. lock_buffer(sbi->s_sbh);
  983. memtostr_pad(label, sbi->s_es->s_volume_name);
  984. unlock_buffer(sbi->s_sbh);
  985. if (copy_to_user(user_label, label, sizeof(label)))
  986. return -EFAULT;
  987. return 0;
  988. }
  989. static int ext4_ioctl_getuuid(struct ext4_sb_info *sbi,
  990. struct fsuuid __user *ufsuuid)
  991. {
  992. struct fsuuid fsuuid;
  993. __u8 uuid[UUID_SIZE];
  994. if (copy_from_user(&fsuuid, ufsuuid, sizeof(fsuuid)))
  995. return -EFAULT;
  996. if (fsuuid.fsu_len == 0) {
  997. fsuuid.fsu_len = UUID_SIZE;
  998. if (copy_to_user(&ufsuuid->fsu_len, &fsuuid.fsu_len,
  999. sizeof(fsuuid.fsu_len)))
  1000. return -EFAULT;
  1001. return 0;
  1002. }
  1003. if (fsuuid.fsu_len < UUID_SIZE || fsuuid.fsu_flags != 0)
  1004. return -EINVAL;
  1005. lock_buffer(sbi->s_sbh);
  1006. memcpy(uuid, sbi->s_es->s_uuid, UUID_SIZE);
  1007. unlock_buffer(sbi->s_sbh);
  1008. fsuuid.fsu_len = UUID_SIZE;
  1009. if (copy_to_user(ufsuuid, &fsuuid, sizeof(fsuuid)) ||
  1010. copy_to_user(&ufsuuid->fsu_uuid[0], uuid, UUID_SIZE))
  1011. return -EFAULT;
  1012. return 0;
  1013. }
  1014. static int ext4_ioctl_setuuid(struct file *filp,
  1015. const struct fsuuid __user *ufsuuid)
  1016. {
  1017. int ret = 0;
  1018. struct super_block *sb = file_inode(filp)->i_sb;
  1019. struct fsuuid fsuuid;
  1020. __u8 uuid[UUID_SIZE];
  1021. if (!capable(CAP_SYS_ADMIN))
  1022. return -EPERM;
  1023. /*
  1024. * If any checksums (group descriptors or metadata) are being used
  1025. * then the checksum seed feature is required to change the UUID.
  1026. */
  1027. if (((ext4_has_feature_gdt_csum(sb) || ext4_has_metadata_csum(sb))
  1028. && !ext4_has_feature_csum_seed(sb))
  1029. || ext4_has_feature_stable_inodes(sb))
  1030. return -EOPNOTSUPP;
  1031. if (copy_from_user(&fsuuid, ufsuuid, sizeof(fsuuid)))
  1032. return -EFAULT;
  1033. if (fsuuid.fsu_len != UUID_SIZE || fsuuid.fsu_flags != 0)
  1034. return -EINVAL;
  1035. if (copy_from_user(uuid, &ufsuuid->fsu_uuid[0], UUID_SIZE))
  1036. return -EFAULT;
  1037. ret = mnt_want_write_file(filp);
  1038. if (ret)
  1039. return ret;
  1040. ret = ext4_update_superblocks_fn(sb, ext4_sb_setuuid, &uuid);
  1041. mnt_drop_write_file(filp);
  1042. return ret;
  1043. }
  1044. static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1045. {
  1046. struct inode *inode = file_inode(filp);
  1047. struct super_block *sb = inode->i_sb;
  1048. struct mnt_idmap *idmap = file_mnt_idmap(filp);
  1049. ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
  1050. switch (cmd) {
  1051. case FS_IOC_GETFSMAP:
  1052. return ext4_ioc_getfsmap(sb, (void __user *)arg);
  1053. case EXT4_IOC_GETVERSION:
  1054. case EXT4_IOC_GETVERSION_OLD:
  1055. return put_user(inode->i_generation, (int __user *) arg);
  1056. case EXT4_IOC_SETVERSION:
  1057. case EXT4_IOC_SETVERSION_OLD: {
  1058. handle_t *handle;
  1059. struct ext4_iloc iloc;
  1060. __u32 generation;
  1061. int err;
  1062. if (!inode_owner_or_capable(idmap, inode))
  1063. return -EPERM;
  1064. if (ext4_has_metadata_csum(inode->i_sb)) {
  1065. ext4_warning(sb, "Setting inode version is not "
  1066. "supported with metadata_csum enabled.");
  1067. return -ENOTTY;
  1068. }
  1069. err = mnt_want_write_file(filp);
  1070. if (err)
  1071. return err;
  1072. if (get_user(generation, (int __user *) arg)) {
  1073. err = -EFAULT;
  1074. goto setversion_out;
  1075. }
  1076. inode_lock(inode);
  1077. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  1078. if (IS_ERR(handle)) {
  1079. err = PTR_ERR(handle);
  1080. goto unlock_out;
  1081. }
  1082. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1083. if (err == 0) {
  1084. inode_set_ctime_current(inode);
  1085. inode_inc_iversion(inode);
  1086. inode->i_generation = generation;
  1087. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1088. }
  1089. ext4_journal_stop(handle);
  1090. unlock_out:
  1091. inode_unlock(inode);
  1092. setversion_out:
  1093. mnt_drop_write_file(filp);
  1094. return err;
  1095. }
  1096. case EXT4_IOC_GROUP_EXTEND: {
  1097. ext4_fsblk_t n_blocks_count;
  1098. int err, err2=0;
  1099. err = ext4_resize_begin(sb);
  1100. if (err)
  1101. return err;
  1102. if (get_user(n_blocks_count, (__u32 __user *)arg)) {
  1103. err = -EFAULT;
  1104. goto group_extend_out;
  1105. }
  1106. if (ext4_has_feature_bigalloc(sb)) {
  1107. ext4_msg(sb, KERN_ERR,
  1108. "Online resizing not supported with bigalloc");
  1109. err = -EOPNOTSUPP;
  1110. goto group_extend_out;
  1111. }
  1112. err = mnt_want_write_file(filp);
  1113. if (err)
  1114. goto group_extend_out;
  1115. err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
  1116. if (EXT4_SB(sb)->s_journal) {
  1117. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  1118. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0);
  1119. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  1120. }
  1121. if (err == 0)
  1122. err = err2;
  1123. mnt_drop_write_file(filp);
  1124. group_extend_out:
  1125. err2 = ext4_resize_end(sb, false);
  1126. if (err == 0)
  1127. err = err2;
  1128. return err;
  1129. }
  1130. case EXT4_IOC_MOVE_EXT: {
  1131. struct move_extent me;
  1132. struct fd donor;
  1133. int err;
  1134. if (!(filp->f_mode & FMODE_READ) ||
  1135. !(filp->f_mode & FMODE_WRITE))
  1136. return -EBADF;
  1137. if (copy_from_user(&me,
  1138. (struct move_extent __user *)arg, sizeof(me)))
  1139. return -EFAULT;
  1140. me.moved_len = 0;
  1141. donor = fdget(me.donor_fd);
  1142. if (!fd_file(donor))
  1143. return -EBADF;
  1144. if (!(fd_file(donor)->f_mode & FMODE_WRITE)) {
  1145. err = -EBADF;
  1146. goto mext_out;
  1147. }
  1148. if (ext4_has_feature_bigalloc(sb)) {
  1149. ext4_msg(sb, KERN_ERR,
  1150. "Online defrag not supported with bigalloc");
  1151. err = -EOPNOTSUPP;
  1152. goto mext_out;
  1153. } else if (IS_DAX(inode)) {
  1154. ext4_msg(sb, KERN_ERR,
  1155. "Online defrag not supported with DAX");
  1156. err = -EOPNOTSUPP;
  1157. goto mext_out;
  1158. }
  1159. err = mnt_want_write_file(filp);
  1160. if (err)
  1161. goto mext_out;
  1162. err = ext4_move_extents(filp, fd_file(donor), me.orig_start,
  1163. me.donor_start, me.len, &me.moved_len);
  1164. mnt_drop_write_file(filp);
  1165. if (copy_to_user((struct move_extent __user *)arg,
  1166. &me, sizeof(me)))
  1167. err = -EFAULT;
  1168. mext_out:
  1169. fdput(donor);
  1170. return err;
  1171. }
  1172. case EXT4_IOC_GROUP_ADD: {
  1173. struct ext4_new_group_data input;
  1174. if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
  1175. sizeof(input)))
  1176. return -EFAULT;
  1177. return ext4_ioctl_group_add(filp, &input);
  1178. }
  1179. case EXT4_IOC_MIGRATE:
  1180. {
  1181. int err;
  1182. if (!inode_owner_or_capable(idmap, inode))
  1183. return -EACCES;
  1184. err = mnt_want_write_file(filp);
  1185. if (err)
  1186. return err;
  1187. /*
  1188. * inode_mutex prevent write and truncate on the file.
  1189. * Read still goes through. We take i_data_sem in
  1190. * ext4_ext_swap_inode_data before we switch the
  1191. * inode format to prevent read.
  1192. */
  1193. inode_lock((inode));
  1194. err = ext4_ext_migrate(inode);
  1195. inode_unlock((inode));
  1196. mnt_drop_write_file(filp);
  1197. return err;
  1198. }
  1199. case EXT4_IOC_ALLOC_DA_BLKS:
  1200. {
  1201. int err;
  1202. if (!inode_owner_or_capable(idmap, inode))
  1203. return -EACCES;
  1204. err = mnt_want_write_file(filp);
  1205. if (err)
  1206. return err;
  1207. err = ext4_alloc_da_blocks(inode);
  1208. mnt_drop_write_file(filp);
  1209. return err;
  1210. }
  1211. case EXT4_IOC_SWAP_BOOT:
  1212. {
  1213. int err;
  1214. if (!(filp->f_mode & FMODE_WRITE))
  1215. return -EBADF;
  1216. err = mnt_want_write_file(filp);
  1217. if (err)
  1218. return err;
  1219. err = swap_inode_boot_loader(sb, idmap, inode);
  1220. mnt_drop_write_file(filp);
  1221. return err;
  1222. }
  1223. case EXT4_IOC_RESIZE_FS: {
  1224. ext4_fsblk_t n_blocks_count;
  1225. int err = 0, err2 = 0;
  1226. ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
  1227. if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
  1228. sizeof(__u64))) {
  1229. return -EFAULT;
  1230. }
  1231. err = ext4_resize_begin(sb);
  1232. if (err)
  1233. return err;
  1234. err = mnt_want_write_file(filp);
  1235. if (err)
  1236. goto resizefs_out;
  1237. err = ext4_resize_fs(sb, n_blocks_count);
  1238. if (EXT4_SB(sb)->s_journal) {
  1239. ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_RESIZE, NULL);
  1240. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  1241. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0);
  1242. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  1243. }
  1244. if (err == 0)
  1245. err = err2;
  1246. mnt_drop_write_file(filp);
  1247. if (!err && (o_group < EXT4_SB(sb)->s_groups_count) &&
  1248. ext4_has_group_desc_csum(sb) &&
  1249. test_opt(sb, INIT_INODE_TABLE))
  1250. err = ext4_register_li_request(sb, o_group);
  1251. resizefs_out:
  1252. err2 = ext4_resize_end(sb, true);
  1253. if (err == 0)
  1254. err = err2;
  1255. return err;
  1256. }
  1257. case FITRIM:
  1258. {
  1259. struct fstrim_range range;
  1260. int ret = 0;
  1261. if (!capable(CAP_SYS_ADMIN))
  1262. return -EPERM;
  1263. if (!bdev_max_discard_sectors(sb->s_bdev))
  1264. return -EOPNOTSUPP;
  1265. /*
  1266. * We haven't replayed the journal, so we cannot use our
  1267. * block-bitmap-guided storage zapping commands.
  1268. */
  1269. if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb))
  1270. return -EROFS;
  1271. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  1272. sizeof(range)))
  1273. return -EFAULT;
  1274. ret = ext4_trim_fs(sb, &range);
  1275. if (ret < 0)
  1276. return ret;
  1277. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  1278. sizeof(range)))
  1279. return -EFAULT;
  1280. return 0;
  1281. }
  1282. case EXT4_IOC_PRECACHE_EXTENTS:
  1283. return ext4_ext_precache(inode);
  1284. case FS_IOC_SET_ENCRYPTION_POLICY:
  1285. if (!ext4_has_feature_encrypt(sb))
  1286. return -EOPNOTSUPP;
  1287. return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
  1288. case FS_IOC_GET_ENCRYPTION_PWSALT:
  1289. return ext4_ioctl_get_encryption_pwsalt(filp, (void __user *)arg);
  1290. case FS_IOC_GET_ENCRYPTION_POLICY:
  1291. if (!ext4_has_feature_encrypt(sb))
  1292. return -EOPNOTSUPP;
  1293. return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
  1294. case FS_IOC_GET_ENCRYPTION_POLICY_EX:
  1295. if (!ext4_has_feature_encrypt(sb))
  1296. return -EOPNOTSUPP;
  1297. return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
  1298. case FS_IOC_ADD_ENCRYPTION_KEY:
  1299. if (!ext4_has_feature_encrypt(sb))
  1300. return -EOPNOTSUPP;
  1301. return fscrypt_ioctl_add_key(filp, (void __user *)arg);
  1302. case FS_IOC_REMOVE_ENCRYPTION_KEY:
  1303. if (!ext4_has_feature_encrypt(sb))
  1304. return -EOPNOTSUPP;
  1305. return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
  1306. case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
  1307. if (!ext4_has_feature_encrypt(sb))
  1308. return -EOPNOTSUPP;
  1309. return fscrypt_ioctl_remove_key_all_users(filp,
  1310. (void __user *)arg);
  1311. case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
  1312. if (!ext4_has_feature_encrypt(sb))
  1313. return -EOPNOTSUPP;
  1314. return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
  1315. case FS_IOC_GET_ENCRYPTION_NONCE:
  1316. if (!ext4_has_feature_encrypt(sb))
  1317. return -EOPNOTSUPP;
  1318. return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
  1319. case EXT4_IOC_CLEAR_ES_CACHE:
  1320. {
  1321. if (!inode_owner_or_capable(idmap, inode))
  1322. return -EACCES;
  1323. ext4_clear_inode_es(inode);
  1324. return 0;
  1325. }
  1326. case EXT4_IOC_GETSTATE:
  1327. {
  1328. __u32 state = 0;
  1329. if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED))
  1330. state |= EXT4_STATE_FLAG_EXT_PRECACHED;
  1331. if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
  1332. state |= EXT4_STATE_FLAG_NEW;
  1333. if (ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY))
  1334. state |= EXT4_STATE_FLAG_NEWENTRY;
  1335. if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE))
  1336. state |= EXT4_STATE_FLAG_DA_ALLOC_CLOSE;
  1337. return put_user(state, (__u32 __user *) arg);
  1338. }
  1339. case EXT4_IOC_GET_ES_CACHE:
  1340. return ext4_ioctl_get_es_cache(filp, arg);
  1341. case EXT4_IOC_SHUTDOWN:
  1342. return ext4_ioctl_shutdown(sb, arg);
  1343. case FS_IOC_ENABLE_VERITY:
  1344. if (!ext4_has_feature_verity(sb))
  1345. return -EOPNOTSUPP;
  1346. return fsverity_ioctl_enable(filp, (const void __user *)arg);
  1347. case FS_IOC_MEASURE_VERITY:
  1348. if (!ext4_has_feature_verity(sb))
  1349. return -EOPNOTSUPP;
  1350. return fsverity_ioctl_measure(filp, (void __user *)arg);
  1351. case FS_IOC_READ_VERITY_METADATA:
  1352. if (!ext4_has_feature_verity(sb))
  1353. return -EOPNOTSUPP;
  1354. return fsverity_ioctl_read_metadata(filp,
  1355. (const void __user *)arg);
  1356. case EXT4_IOC_CHECKPOINT:
  1357. return ext4_ioctl_checkpoint(filp, arg);
  1358. case FS_IOC_GETFSLABEL:
  1359. return ext4_ioctl_getlabel(EXT4_SB(sb), (void __user *)arg);
  1360. case FS_IOC_SETFSLABEL:
  1361. return ext4_ioctl_setlabel(filp,
  1362. (const void __user *)arg);
  1363. case EXT4_IOC_GETFSUUID:
  1364. return ext4_ioctl_getuuid(EXT4_SB(sb), (void __user *)arg);
  1365. case EXT4_IOC_SETFSUUID:
  1366. return ext4_ioctl_setuuid(filp, (const void __user *)arg);
  1367. default:
  1368. return -ENOTTY;
  1369. }
  1370. }
  1371. long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1372. {
  1373. return __ext4_ioctl(filp, cmd, arg);
  1374. }
  1375. #ifdef CONFIG_COMPAT
  1376. long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1377. {
  1378. /* These are just misnamed, they actually get/put from/to user an int */
  1379. switch (cmd) {
  1380. case EXT4_IOC32_GETVERSION:
  1381. cmd = EXT4_IOC_GETVERSION;
  1382. break;
  1383. case EXT4_IOC32_SETVERSION:
  1384. cmd = EXT4_IOC_SETVERSION;
  1385. break;
  1386. case EXT4_IOC32_GROUP_EXTEND:
  1387. cmd = EXT4_IOC_GROUP_EXTEND;
  1388. break;
  1389. case EXT4_IOC32_GETVERSION_OLD:
  1390. cmd = EXT4_IOC_GETVERSION_OLD;
  1391. break;
  1392. case EXT4_IOC32_SETVERSION_OLD:
  1393. cmd = EXT4_IOC_SETVERSION_OLD;
  1394. break;
  1395. case EXT4_IOC32_GETRSVSZ:
  1396. cmd = EXT4_IOC_GETRSVSZ;
  1397. break;
  1398. case EXT4_IOC32_SETRSVSZ:
  1399. cmd = EXT4_IOC_SETRSVSZ;
  1400. break;
  1401. case EXT4_IOC32_GROUP_ADD: {
  1402. struct compat_ext4_new_group_input __user *uinput;
  1403. struct ext4_new_group_data input;
  1404. int err;
  1405. uinput = compat_ptr(arg);
  1406. err = get_user(input.group, &uinput->group);
  1407. err |= get_user(input.block_bitmap, &uinput->block_bitmap);
  1408. err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
  1409. err |= get_user(input.inode_table, &uinput->inode_table);
  1410. err |= get_user(input.blocks_count, &uinput->blocks_count);
  1411. err |= get_user(input.reserved_blocks,
  1412. &uinput->reserved_blocks);
  1413. if (err)
  1414. return -EFAULT;
  1415. return ext4_ioctl_group_add(file, &input);
  1416. }
  1417. case EXT4_IOC_MOVE_EXT:
  1418. case EXT4_IOC_RESIZE_FS:
  1419. case FITRIM:
  1420. case EXT4_IOC_PRECACHE_EXTENTS:
  1421. case FS_IOC_SET_ENCRYPTION_POLICY:
  1422. case FS_IOC_GET_ENCRYPTION_PWSALT:
  1423. case FS_IOC_GET_ENCRYPTION_POLICY:
  1424. case FS_IOC_GET_ENCRYPTION_POLICY_EX:
  1425. case FS_IOC_ADD_ENCRYPTION_KEY:
  1426. case FS_IOC_REMOVE_ENCRYPTION_KEY:
  1427. case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
  1428. case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
  1429. case FS_IOC_GET_ENCRYPTION_NONCE:
  1430. case EXT4_IOC_SHUTDOWN:
  1431. case FS_IOC_GETFSMAP:
  1432. case FS_IOC_ENABLE_VERITY:
  1433. case FS_IOC_MEASURE_VERITY:
  1434. case FS_IOC_READ_VERITY_METADATA:
  1435. case EXT4_IOC_CLEAR_ES_CACHE:
  1436. case EXT4_IOC_GETSTATE:
  1437. case EXT4_IOC_GET_ES_CACHE:
  1438. case EXT4_IOC_CHECKPOINT:
  1439. case FS_IOC_GETFSLABEL:
  1440. case FS_IOC_SETFSLABEL:
  1441. case EXT4_IOC_GETFSUUID:
  1442. case EXT4_IOC_SETFSUUID:
  1443. break;
  1444. default:
  1445. return -ENOIOCTLCMD;
  1446. }
  1447. return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  1448. }
  1449. #endif
  1450. static void set_overhead(struct ext4_super_block *es, const void *arg)
  1451. {
  1452. es->s_overhead_clusters = cpu_to_le32(*((unsigned long *) arg));
  1453. }
  1454. int ext4_update_overhead(struct super_block *sb, bool force)
  1455. {
  1456. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1457. if (sb_rdonly(sb))
  1458. return 0;
  1459. if (!force &&
  1460. (sbi->s_overhead == 0 ||
  1461. sbi->s_overhead == le32_to_cpu(sbi->s_es->s_overhead_clusters)))
  1462. return 0;
  1463. return ext4_update_superblocks_fn(sb, set_overhead, &sbi->s_overhead);
  1464. }