ioctl.c 53 KB

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