resize.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/resize.c
  4. *
  5. * Support for resizing an ext4 filesystem while it is mounted.
  6. *
  7. * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com>
  8. *
  9. * This could probably be made into a module, because it is not often in use.
  10. */
  11. #define EXT4FS_DEBUG
  12. #include <linux/errno.h>
  13. #include <linux/slab.h>
  14. #include "ext4_jbd2.h"
  15. int ext4_resize_begin(struct super_block *sb)
  16. {
  17. struct ext4_sb_info *sbi = EXT4_SB(sb);
  18. int ret = 0;
  19. if (!capable(CAP_SYS_RESOURCE))
  20. return -EPERM;
  21. /*
  22. * If we are not using the primary superblock/GDT copy don't resize,
  23. * because the user tools have no way of handling this. Probably a
  24. * bad time to do it anyways.
  25. */
  26. if (EXT4_B2C(sbi, sbi->s_sbh->b_blocknr) !=
  27. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
  28. ext4_warning(sb, "won't resize using backup superblock at %llu",
  29. (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
  30. return -EPERM;
  31. }
  32. /*
  33. * We are not allowed to do online-resizing on a filesystem mounted
  34. * with error, because it can destroy the filesystem easily.
  35. */
  36. if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
  37. ext4_warning(sb, "There are errors in the filesystem, "
  38. "so online resizing is not allowed");
  39. return -EPERM;
  40. }
  41. if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING,
  42. &EXT4_SB(sb)->s_ext4_flags))
  43. ret = -EBUSY;
  44. return ret;
  45. }
  46. void ext4_resize_end(struct super_block *sb)
  47. {
  48. clear_bit_unlock(EXT4_FLAGS_RESIZING, &EXT4_SB(sb)->s_ext4_flags);
  49. smp_mb__after_atomic();
  50. }
  51. static ext4_group_t ext4_meta_bg_first_group(struct super_block *sb,
  52. ext4_group_t group) {
  53. return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) <<
  54. EXT4_DESC_PER_BLOCK_BITS(sb);
  55. }
  56. static ext4_fsblk_t ext4_meta_bg_first_block_no(struct super_block *sb,
  57. ext4_group_t group) {
  58. group = ext4_meta_bg_first_group(sb, group);
  59. return ext4_group_first_block_no(sb, group);
  60. }
  61. static ext4_grpblk_t ext4_group_overhead_blocks(struct super_block *sb,
  62. ext4_group_t group) {
  63. ext4_grpblk_t overhead;
  64. overhead = ext4_bg_num_gdb(sb, group);
  65. if (ext4_bg_has_super(sb, group))
  66. overhead += 1 +
  67. le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
  68. return overhead;
  69. }
  70. #define outside(b, first, last) ((b) < (first) || (b) >= (last))
  71. #define inside(b, first, last) ((b) >= (first) && (b) < (last))
  72. static int verify_group_input(struct super_block *sb,
  73. struct ext4_new_group_data *input)
  74. {
  75. struct ext4_sb_info *sbi = EXT4_SB(sb);
  76. struct ext4_super_block *es = sbi->s_es;
  77. ext4_fsblk_t start = ext4_blocks_count(es);
  78. ext4_fsblk_t end = start + input->blocks_count;
  79. ext4_group_t group = input->group;
  80. ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
  81. unsigned overhead;
  82. ext4_fsblk_t metaend;
  83. struct buffer_head *bh = NULL;
  84. ext4_grpblk_t free_blocks_count, offset;
  85. int err = -EINVAL;
  86. if (group != sbi->s_groups_count) {
  87. ext4_warning(sb, "Cannot add at group %u (only %u groups)",
  88. input->group, sbi->s_groups_count);
  89. return -EINVAL;
  90. }
  91. overhead = ext4_group_overhead_blocks(sb, group);
  92. metaend = start + overhead;
  93. input->free_clusters_count = free_blocks_count =
  94. input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
  95. if (test_opt(sb, DEBUG))
  96. printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
  97. "(%d free, %u reserved)\n",
  98. ext4_bg_has_super(sb, input->group) ? "normal" :
  99. "no-super", input->group, input->blocks_count,
  100. free_blocks_count, input->reserved_blocks);
  101. ext4_get_group_no_and_offset(sb, start, NULL, &offset);
  102. if (offset != 0)
  103. ext4_warning(sb, "Last group not full");
  104. else if (input->reserved_blocks > input->blocks_count / 5)
  105. ext4_warning(sb, "Reserved blocks too high (%u)",
  106. input->reserved_blocks);
  107. else if (free_blocks_count < 0)
  108. ext4_warning(sb, "Bad blocks count %u",
  109. input->blocks_count);
  110. else if (!(bh = sb_bread(sb, end - 1)))
  111. ext4_warning(sb, "Cannot read last block (%llu)",
  112. end - 1);
  113. else if (outside(input->block_bitmap, start, end))
  114. ext4_warning(sb, "Block bitmap not in group (block %llu)",
  115. (unsigned long long)input->block_bitmap);
  116. else if (outside(input->inode_bitmap, start, end))
  117. ext4_warning(sb, "Inode bitmap not in group (block %llu)",
  118. (unsigned long long)input->inode_bitmap);
  119. else if (outside(input->inode_table, start, end) ||
  120. outside(itend - 1, start, end))
  121. ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)",
  122. (unsigned long long)input->inode_table, itend - 1);
  123. else if (input->inode_bitmap == input->block_bitmap)
  124. ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)",
  125. (unsigned long long)input->block_bitmap);
  126. else if (inside(input->block_bitmap, input->inode_table, itend))
  127. ext4_warning(sb, "Block bitmap (%llu) in inode table "
  128. "(%llu-%llu)",
  129. (unsigned long long)input->block_bitmap,
  130. (unsigned long long)input->inode_table, itend - 1);
  131. else if (inside(input->inode_bitmap, input->inode_table, itend))
  132. ext4_warning(sb, "Inode bitmap (%llu) in inode table "
  133. "(%llu-%llu)",
  134. (unsigned long long)input->inode_bitmap,
  135. (unsigned long long)input->inode_table, itend - 1);
  136. else if (inside(input->block_bitmap, start, metaend))
  137. ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)",
  138. (unsigned long long)input->block_bitmap,
  139. start, metaend - 1);
  140. else if (inside(input->inode_bitmap, start, metaend))
  141. ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)",
  142. (unsigned long long)input->inode_bitmap,
  143. start, metaend - 1);
  144. else if (inside(input->inode_table, start, metaend) ||
  145. inside(itend - 1, start, metaend))
  146. ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table "
  147. "(%llu-%llu)",
  148. (unsigned long long)input->inode_table,
  149. itend - 1, start, metaend - 1);
  150. else
  151. err = 0;
  152. brelse(bh);
  153. return err;
  154. }
  155. /*
  156. * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex
  157. * group each time.
  158. */
  159. struct ext4_new_flex_group_data {
  160. struct ext4_new_group_data *groups; /* new_group_data for groups
  161. in the flex group */
  162. __u16 *bg_flags; /* block group flags of groups
  163. in @groups */
  164. ext4_group_t count; /* number of groups in @groups
  165. */
  166. };
  167. /*
  168. * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of
  169. * @flexbg_size.
  170. *
  171. * Returns NULL on failure otherwise address of the allocated structure.
  172. */
  173. static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size)
  174. {
  175. struct ext4_new_flex_group_data *flex_gd;
  176. flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS);
  177. if (flex_gd == NULL)
  178. goto out3;
  179. if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_group_data))
  180. goto out2;
  181. flex_gd->count = flexbg_size;
  182. flex_gd->groups = kmalloc_array(flexbg_size,
  183. sizeof(struct ext4_new_group_data),
  184. GFP_NOFS);
  185. if (flex_gd->groups == NULL)
  186. goto out2;
  187. flex_gd->bg_flags = kmalloc_array(flexbg_size, sizeof(__u16),
  188. GFP_NOFS);
  189. if (flex_gd->bg_flags == NULL)
  190. goto out1;
  191. return flex_gd;
  192. out1:
  193. kfree(flex_gd->groups);
  194. out2:
  195. kfree(flex_gd);
  196. out3:
  197. return NULL;
  198. }
  199. static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd)
  200. {
  201. kfree(flex_gd->bg_flags);
  202. kfree(flex_gd->groups);
  203. kfree(flex_gd);
  204. }
  205. /*
  206. * ext4_alloc_group_tables() allocates block bitmaps, inode bitmaps
  207. * and inode tables for a flex group.
  208. *
  209. * This function is used by 64bit-resize. Note that this function allocates
  210. * group tables from the 1st group of groups contained by @flexgd, which may
  211. * be a partial of a flex group.
  212. *
  213. * @sb: super block of fs to which the groups belongs
  214. *
  215. * Returns 0 on a successful allocation of the metadata blocks in the
  216. * block group.
  217. */
  218. static int ext4_alloc_group_tables(struct super_block *sb,
  219. struct ext4_new_flex_group_data *flex_gd,
  220. int flexbg_size)
  221. {
  222. struct ext4_new_group_data *group_data = flex_gd->groups;
  223. ext4_fsblk_t start_blk;
  224. ext4_fsblk_t last_blk;
  225. ext4_group_t src_group;
  226. ext4_group_t bb_index = 0;
  227. ext4_group_t ib_index = 0;
  228. ext4_group_t it_index = 0;
  229. ext4_group_t group;
  230. ext4_group_t last_group;
  231. unsigned overhead;
  232. __u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0;
  233. int i;
  234. BUG_ON(flex_gd->count == 0 || group_data == NULL);
  235. src_group = group_data[0].group;
  236. last_group = src_group + flex_gd->count - 1;
  237. BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) !=
  238. (last_group & ~(flexbg_size - 1))));
  239. next_group:
  240. group = group_data[0].group;
  241. if (src_group >= group_data[0].group + flex_gd->count)
  242. return -ENOSPC;
  243. start_blk = ext4_group_first_block_no(sb, src_group);
  244. last_blk = start_blk + group_data[src_group - group].blocks_count;
  245. overhead = ext4_group_overhead_blocks(sb, src_group);
  246. start_blk += overhead;
  247. /* We collect contiguous blocks as much as possible. */
  248. src_group++;
  249. for (; src_group <= last_group; src_group++) {
  250. overhead = ext4_group_overhead_blocks(sb, src_group);
  251. if (overhead == 0)
  252. last_blk += group_data[src_group - group].blocks_count;
  253. else
  254. break;
  255. }
  256. /* Allocate block bitmaps */
  257. for (; bb_index < flex_gd->count; bb_index++) {
  258. if (start_blk >= last_blk)
  259. goto next_group;
  260. group_data[bb_index].block_bitmap = start_blk++;
  261. group = ext4_get_group_number(sb, start_blk - 1);
  262. group -= group_data[0].group;
  263. group_data[group].mdata_blocks++;
  264. flex_gd->bg_flags[group] &= uninit_mask;
  265. }
  266. /* Allocate inode bitmaps */
  267. for (; ib_index < flex_gd->count; ib_index++) {
  268. if (start_blk >= last_blk)
  269. goto next_group;
  270. group_data[ib_index].inode_bitmap = start_blk++;
  271. group = ext4_get_group_number(sb, start_blk - 1);
  272. group -= group_data[0].group;
  273. group_data[group].mdata_blocks++;
  274. flex_gd->bg_flags[group] &= uninit_mask;
  275. }
  276. /* Allocate inode tables */
  277. for (; it_index < flex_gd->count; it_index++) {
  278. unsigned int itb = EXT4_SB(sb)->s_itb_per_group;
  279. ext4_fsblk_t next_group_start;
  280. if (start_blk + itb > last_blk)
  281. goto next_group;
  282. group_data[it_index].inode_table = start_blk;
  283. group = ext4_get_group_number(sb, start_blk);
  284. next_group_start = ext4_group_first_block_no(sb, group + 1);
  285. group -= group_data[0].group;
  286. if (start_blk + itb > next_group_start) {
  287. flex_gd->bg_flags[group + 1] &= uninit_mask;
  288. overhead = start_blk + itb - next_group_start;
  289. group_data[group + 1].mdata_blocks += overhead;
  290. itb -= overhead;
  291. }
  292. group_data[group].mdata_blocks += itb;
  293. flex_gd->bg_flags[group] &= uninit_mask;
  294. start_blk += EXT4_SB(sb)->s_itb_per_group;
  295. }
  296. /* Update free clusters count to exclude metadata blocks */
  297. for (i = 0; i < flex_gd->count; i++) {
  298. group_data[i].free_clusters_count -=
  299. EXT4_NUM_B2C(EXT4_SB(sb),
  300. group_data[i].mdata_blocks);
  301. }
  302. if (test_opt(sb, DEBUG)) {
  303. int i;
  304. group = group_data[0].group;
  305. printk(KERN_DEBUG "EXT4-fs: adding a flex group with "
  306. "%d groups, flexbg size is %d:\n", flex_gd->count,
  307. flexbg_size);
  308. for (i = 0; i < flex_gd->count; i++) {
  309. ext4_debug(
  310. "adding %s group %u: %u blocks (%d free, %d mdata blocks)\n",
  311. ext4_bg_has_super(sb, group + i) ? "normal" :
  312. "no-super", group + i,
  313. group_data[i].blocks_count,
  314. group_data[i].free_clusters_count,
  315. group_data[i].mdata_blocks);
  316. }
  317. }
  318. return 0;
  319. }
  320. static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
  321. ext4_fsblk_t blk)
  322. {
  323. struct buffer_head *bh;
  324. int err;
  325. bh = sb_getblk(sb, blk);
  326. if (unlikely(!bh))
  327. return ERR_PTR(-ENOMEM);
  328. BUFFER_TRACE(bh, "get_write_access");
  329. if ((err = ext4_journal_get_write_access(handle, bh))) {
  330. brelse(bh);
  331. bh = ERR_PTR(err);
  332. } else {
  333. memset(bh->b_data, 0, sb->s_blocksize);
  334. set_buffer_uptodate(bh);
  335. }
  336. return bh;
  337. }
  338. /*
  339. * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA.
  340. * If that fails, restart the transaction & regain write access for the
  341. * buffer head which is used for block_bitmap modifications.
  342. */
  343. static int extend_or_restart_transaction(handle_t *handle, int thresh)
  344. {
  345. int err;
  346. if (ext4_handle_has_enough_credits(handle, thresh))
  347. return 0;
  348. err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA);
  349. if (err < 0)
  350. return err;
  351. if (err) {
  352. err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA);
  353. if (err)
  354. return err;
  355. }
  356. return 0;
  357. }
  358. /*
  359. * set_flexbg_block_bitmap() mark clusters [@first_cluster, @last_cluster] used.
  360. *
  361. * Helper function for ext4_setup_new_group_blocks() which set .
  362. *
  363. * @sb: super block
  364. * @handle: journal handle
  365. * @flex_gd: flex group data
  366. */
  367. static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle,
  368. struct ext4_new_flex_group_data *flex_gd,
  369. ext4_fsblk_t first_cluster, ext4_fsblk_t last_cluster)
  370. {
  371. struct ext4_sb_info *sbi = EXT4_SB(sb);
  372. ext4_group_t count = last_cluster - first_cluster + 1;
  373. ext4_group_t count2;
  374. ext4_debug("mark clusters [%llu-%llu] used\n", first_cluster,
  375. last_cluster);
  376. for (count2 = count; count > 0;
  377. count -= count2, first_cluster += count2) {
  378. ext4_fsblk_t start;
  379. struct buffer_head *bh;
  380. ext4_group_t group;
  381. int err;
  382. group = ext4_get_group_number(sb, EXT4_C2B(sbi, first_cluster));
  383. start = EXT4_B2C(sbi, ext4_group_first_block_no(sb, group));
  384. group -= flex_gd->groups[0].group;
  385. count2 = EXT4_CLUSTERS_PER_GROUP(sb) - (first_cluster - start);
  386. if (count2 > count)
  387. count2 = count;
  388. if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) {
  389. BUG_ON(flex_gd->count > 1);
  390. continue;
  391. }
  392. err = extend_or_restart_transaction(handle, 1);
  393. if (err)
  394. return err;
  395. bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap);
  396. if (unlikely(!bh))
  397. return -ENOMEM;
  398. BUFFER_TRACE(bh, "get_write_access");
  399. err = ext4_journal_get_write_access(handle, bh);
  400. if (err) {
  401. brelse(bh);
  402. return err;
  403. }
  404. ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n",
  405. first_cluster, first_cluster - start, count2);
  406. ext4_set_bits(bh->b_data, first_cluster - start, count2);
  407. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  408. brelse(bh);
  409. if (unlikely(err))
  410. return err;
  411. }
  412. return 0;
  413. }
  414. /*
  415. * Set up the block and inode bitmaps, and the inode table for the new groups.
  416. * This doesn't need to be part of the main transaction, since we are only
  417. * changing blocks outside the actual filesystem. We still do journaling to
  418. * ensure the recovery is correct in case of a failure just after resize.
  419. * If any part of this fails, we simply abort the resize.
  420. *
  421. * setup_new_flex_group_blocks handles a flex group as follow:
  422. * 1. copy super block and GDT, and initialize group tables if necessary.
  423. * In this step, we only set bits in blocks bitmaps for blocks taken by
  424. * super block and GDT.
  425. * 2. allocate group tables in block bitmaps, that is, set bits in block
  426. * bitmap for blocks taken by group tables.
  427. */
  428. static int setup_new_flex_group_blocks(struct super_block *sb,
  429. struct ext4_new_flex_group_data *flex_gd)
  430. {
  431. int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group};
  432. ext4_fsblk_t start;
  433. ext4_fsblk_t block;
  434. struct ext4_sb_info *sbi = EXT4_SB(sb);
  435. struct ext4_super_block *es = sbi->s_es;
  436. struct ext4_new_group_data *group_data = flex_gd->groups;
  437. __u16 *bg_flags = flex_gd->bg_flags;
  438. handle_t *handle;
  439. ext4_group_t group, count;
  440. struct buffer_head *bh = NULL;
  441. int reserved_gdb, i, j, err = 0, err2;
  442. int meta_bg;
  443. BUG_ON(!flex_gd->count || !group_data ||
  444. group_data[0].group != sbi->s_groups_count);
  445. reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
  446. meta_bg = ext4_has_feature_meta_bg(sb);
  447. /* This transaction may be extended/restarted along the way */
  448. handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
  449. if (IS_ERR(handle))
  450. return PTR_ERR(handle);
  451. group = group_data[0].group;
  452. for (i = 0; i < flex_gd->count; i++, group++) {
  453. unsigned long gdblocks;
  454. ext4_grpblk_t overhead;
  455. gdblocks = ext4_bg_num_gdb(sb, group);
  456. start = ext4_group_first_block_no(sb, group);
  457. if (meta_bg == 0 && !ext4_bg_has_super(sb, group))
  458. goto handle_itb;
  459. if (meta_bg == 1) {
  460. ext4_group_t first_group;
  461. first_group = ext4_meta_bg_first_group(sb, group);
  462. if (first_group != group + 1 &&
  463. first_group != group + EXT4_DESC_PER_BLOCK(sb) - 1)
  464. goto handle_itb;
  465. }
  466. block = start + ext4_bg_has_super(sb, group);
  467. /* Copy all of the GDT blocks into the backup in this group */
  468. for (j = 0; j < gdblocks; j++, block++) {
  469. struct buffer_head *gdb;
  470. ext4_debug("update backup group %#04llx\n", block);
  471. err = extend_or_restart_transaction(handle, 1);
  472. if (err)
  473. goto out;
  474. gdb = sb_getblk(sb, block);
  475. if (unlikely(!gdb)) {
  476. err = -ENOMEM;
  477. goto out;
  478. }
  479. BUFFER_TRACE(gdb, "get_write_access");
  480. err = ext4_journal_get_write_access(handle, gdb);
  481. if (err) {
  482. brelse(gdb);
  483. goto out;
  484. }
  485. memcpy(gdb->b_data, sbi->s_group_desc[j]->b_data,
  486. gdb->b_size);
  487. set_buffer_uptodate(gdb);
  488. err = ext4_handle_dirty_metadata(handle, NULL, gdb);
  489. if (unlikely(err)) {
  490. brelse(gdb);
  491. goto out;
  492. }
  493. brelse(gdb);
  494. }
  495. /* Zero out all of the reserved backup group descriptor
  496. * table blocks
  497. */
  498. if (ext4_bg_has_super(sb, group)) {
  499. err = sb_issue_zeroout(sb, gdblocks + start + 1,
  500. reserved_gdb, GFP_NOFS);
  501. if (err)
  502. goto out;
  503. }
  504. handle_itb:
  505. /* Initialize group tables of the grop @group */
  506. if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED))
  507. goto handle_bb;
  508. /* Zero out all of the inode table blocks */
  509. block = group_data[i].inode_table;
  510. ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
  511. block, sbi->s_itb_per_group);
  512. err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group,
  513. GFP_NOFS);
  514. if (err)
  515. goto out;
  516. handle_bb:
  517. if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT)
  518. goto handle_ib;
  519. /* Initialize block bitmap of the @group */
  520. block = group_data[i].block_bitmap;
  521. err = extend_or_restart_transaction(handle, 1);
  522. if (err)
  523. goto out;
  524. bh = bclean(handle, sb, block);
  525. if (IS_ERR(bh)) {
  526. err = PTR_ERR(bh);
  527. goto out;
  528. }
  529. overhead = ext4_group_overhead_blocks(sb, group);
  530. if (overhead != 0) {
  531. ext4_debug("mark backup superblock %#04llx (+0)\n",
  532. start);
  533. ext4_set_bits(bh->b_data, 0,
  534. EXT4_NUM_B2C(sbi, overhead));
  535. }
  536. ext4_mark_bitmap_end(EXT4_B2C(sbi, group_data[i].blocks_count),
  537. sb->s_blocksize * 8, bh->b_data);
  538. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  539. brelse(bh);
  540. if (err)
  541. goto out;
  542. handle_ib:
  543. if (bg_flags[i] & EXT4_BG_INODE_UNINIT)
  544. continue;
  545. /* Initialize inode bitmap of the @group */
  546. block = group_data[i].inode_bitmap;
  547. err = extend_or_restart_transaction(handle, 1);
  548. if (err)
  549. goto out;
  550. /* Mark unused entries in inode bitmap used */
  551. bh = bclean(handle, sb, block);
  552. if (IS_ERR(bh)) {
  553. err = PTR_ERR(bh);
  554. goto out;
  555. }
  556. ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
  557. sb->s_blocksize * 8, bh->b_data);
  558. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  559. brelse(bh);
  560. if (err)
  561. goto out;
  562. }
  563. /* Mark group tables in block bitmap */
  564. for (j = 0; j < GROUP_TABLE_COUNT; j++) {
  565. count = group_table_count[j];
  566. start = (&group_data[0].block_bitmap)[j];
  567. block = start;
  568. for (i = 1; i < flex_gd->count; i++) {
  569. block += group_table_count[j];
  570. if (block == (&group_data[i].block_bitmap)[j]) {
  571. count += group_table_count[j];
  572. continue;
  573. }
  574. err = set_flexbg_block_bitmap(sb, handle,
  575. flex_gd,
  576. EXT4_B2C(sbi, start),
  577. EXT4_B2C(sbi,
  578. start + count
  579. - 1));
  580. if (err)
  581. goto out;
  582. count = group_table_count[j];
  583. start = (&group_data[i].block_bitmap)[j];
  584. block = start;
  585. }
  586. if (count) {
  587. err = set_flexbg_block_bitmap(sb, handle,
  588. flex_gd,
  589. EXT4_B2C(sbi, start),
  590. EXT4_B2C(sbi,
  591. start + count
  592. - 1));
  593. if (err)
  594. goto out;
  595. }
  596. }
  597. out:
  598. err2 = ext4_journal_stop(handle);
  599. if (err2 && !err)
  600. err = err2;
  601. return err;
  602. }
  603. /*
  604. * Iterate through the groups which hold BACKUP superblock/GDT copies in an
  605. * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
  606. * calling this for the first time. In a sparse filesystem it will be the
  607. * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
  608. * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
  609. */
  610. static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
  611. unsigned *five, unsigned *seven)
  612. {
  613. unsigned *min = three;
  614. int mult = 3;
  615. unsigned ret;
  616. if (!ext4_has_feature_sparse_super(sb)) {
  617. ret = *min;
  618. *min += 1;
  619. return ret;
  620. }
  621. if (*five < *min) {
  622. min = five;
  623. mult = 5;
  624. }
  625. if (*seven < *min) {
  626. min = seven;
  627. mult = 7;
  628. }
  629. ret = *min;
  630. *min *= mult;
  631. return ret;
  632. }
  633. /*
  634. * Check that all of the backup GDT blocks are held in the primary GDT block.
  635. * It is assumed that they are stored in group order. Returns the number of
  636. * groups in current filesystem that have BACKUPS, or -ve error code.
  637. */
  638. static int verify_reserved_gdb(struct super_block *sb,
  639. ext4_group_t end,
  640. struct buffer_head *primary)
  641. {
  642. const ext4_fsblk_t blk = primary->b_blocknr;
  643. unsigned three = 1;
  644. unsigned five = 5;
  645. unsigned seven = 7;
  646. unsigned grp;
  647. __le32 *p = (__le32 *)primary->b_data;
  648. int gdbackups = 0;
  649. while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
  650. if (le32_to_cpu(*p++) !=
  651. grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
  652. ext4_warning(sb, "reserved GDT %llu"
  653. " missing grp %d (%llu)",
  654. blk, grp,
  655. grp *
  656. (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
  657. blk);
  658. return -EINVAL;
  659. }
  660. if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
  661. return -EFBIG;
  662. }
  663. return gdbackups;
  664. }
  665. /*
  666. * Called when we need to bring a reserved group descriptor table block into
  667. * use from the resize inode. The primary copy of the new GDT block currently
  668. * is an indirect block (under the double indirect block in the resize inode).
  669. * The new backup GDT blocks will be stored as leaf blocks in this indirect
  670. * block, in group order. Even though we know all the block numbers we need,
  671. * we check to ensure that the resize inode has actually reserved these blocks.
  672. *
  673. * Don't need to update the block bitmaps because the blocks are still in use.
  674. *
  675. * We get all of the error cases out of the way, so that we are sure to not
  676. * fail once we start modifying the data on disk, because JBD has no rollback.
  677. */
  678. static int add_new_gdb(handle_t *handle, struct inode *inode,
  679. ext4_group_t group)
  680. {
  681. struct super_block *sb = inode->i_sb;
  682. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  683. unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  684. ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
  685. struct buffer_head **o_group_desc, **n_group_desc;
  686. struct buffer_head *dind;
  687. struct buffer_head *gdb_bh;
  688. int gdbackups;
  689. struct ext4_iloc iloc;
  690. __le32 *data;
  691. int err;
  692. if (test_opt(sb, DEBUG))
  693. printk(KERN_DEBUG
  694. "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
  695. gdb_num);
  696. gdb_bh = sb_bread(sb, gdblock);
  697. if (!gdb_bh)
  698. return -EIO;
  699. gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
  700. if (gdbackups < 0) {
  701. err = gdbackups;
  702. goto exit_bh;
  703. }
  704. data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
  705. dind = sb_bread(sb, le32_to_cpu(*data));
  706. if (!dind) {
  707. err = -EIO;
  708. goto exit_bh;
  709. }
  710. data = (__le32 *)dind->b_data;
  711. if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
  712. ext4_warning(sb, "new group %u GDT block %llu not reserved",
  713. group, gdblock);
  714. err = -EINVAL;
  715. goto exit_dind;
  716. }
  717. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  718. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  719. if (unlikely(err))
  720. goto exit_dind;
  721. BUFFER_TRACE(gdb_bh, "get_write_access");
  722. err = ext4_journal_get_write_access(handle, gdb_bh);
  723. if (unlikely(err))
  724. goto exit_dind;
  725. BUFFER_TRACE(dind, "get_write_access");
  726. err = ext4_journal_get_write_access(handle, dind);
  727. if (unlikely(err))
  728. ext4_std_error(sb, err);
  729. /* ext4_reserve_inode_write() gets a reference on the iloc */
  730. err = ext4_reserve_inode_write(handle, inode, &iloc);
  731. if (unlikely(err))
  732. goto exit_dind;
  733. n_group_desc = ext4_kvmalloc((gdb_num + 1) *
  734. sizeof(struct buffer_head *),
  735. GFP_NOFS);
  736. if (!n_group_desc) {
  737. err = -ENOMEM;
  738. ext4_warning(sb, "not enough memory for %lu groups",
  739. gdb_num + 1);
  740. goto exit_inode;
  741. }
  742. /*
  743. * Finally, we have all of the possible failures behind us...
  744. *
  745. * Remove new GDT block from inode double-indirect block and clear out
  746. * the new GDT block for use (which also "frees" the backup GDT blocks
  747. * from the reserved inode). We don't need to change the bitmaps for
  748. * these blocks, because they are marked as in-use from being in the
  749. * reserved inode, and will become GDT blocks (primary and backup).
  750. */
  751. data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
  752. err = ext4_handle_dirty_metadata(handle, NULL, dind);
  753. if (unlikely(err)) {
  754. ext4_std_error(sb, err);
  755. goto exit_inode;
  756. }
  757. inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >>
  758. (9 - EXT4_SB(sb)->s_cluster_bits);
  759. ext4_mark_iloc_dirty(handle, inode, &iloc);
  760. memset(gdb_bh->b_data, 0, sb->s_blocksize);
  761. err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
  762. if (unlikely(err)) {
  763. ext4_std_error(sb, err);
  764. iloc.bh = NULL;
  765. goto exit_inode;
  766. }
  767. brelse(dind);
  768. o_group_desc = EXT4_SB(sb)->s_group_desc;
  769. memcpy(n_group_desc, o_group_desc,
  770. EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
  771. n_group_desc[gdb_num] = gdb_bh;
  772. EXT4_SB(sb)->s_group_desc = n_group_desc;
  773. EXT4_SB(sb)->s_gdb_count++;
  774. kvfree(o_group_desc);
  775. le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
  776. err = ext4_handle_dirty_super(handle, sb);
  777. if (err)
  778. ext4_std_error(sb, err);
  779. return err;
  780. exit_inode:
  781. kvfree(n_group_desc);
  782. brelse(iloc.bh);
  783. exit_dind:
  784. brelse(dind);
  785. exit_bh:
  786. brelse(gdb_bh);
  787. ext4_debug("leaving with error %d\n", err);
  788. return err;
  789. }
  790. /*
  791. * add_new_gdb_meta_bg is the sister of add_new_gdb.
  792. */
  793. static int add_new_gdb_meta_bg(struct super_block *sb,
  794. handle_t *handle, ext4_group_t group) {
  795. ext4_fsblk_t gdblock;
  796. struct buffer_head *gdb_bh;
  797. struct buffer_head **o_group_desc, **n_group_desc;
  798. unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  799. int err;
  800. gdblock = ext4_meta_bg_first_block_no(sb, group) +
  801. ext4_bg_has_super(sb, group);
  802. gdb_bh = sb_bread(sb, gdblock);
  803. if (!gdb_bh)
  804. return -EIO;
  805. n_group_desc = ext4_kvmalloc((gdb_num + 1) *
  806. sizeof(struct buffer_head *),
  807. GFP_NOFS);
  808. if (!n_group_desc) {
  809. brelse(gdb_bh);
  810. err = -ENOMEM;
  811. ext4_warning(sb, "not enough memory for %lu groups",
  812. gdb_num + 1);
  813. return err;
  814. }
  815. o_group_desc = EXT4_SB(sb)->s_group_desc;
  816. memcpy(n_group_desc, o_group_desc,
  817. EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
  818. n_group_desc[gdb_num] = gdb_bh;
  819. EXT4_SB(sb)->s_group_desc = n_group_desc;
  820. EXT4_SB(sb)->s_gdb_count++;
  821. kvfree(o_group_desc);
  822. BUFFER_TRACE(gdb_bh, "get_write_access");
  823. err = ext4_journal_get_write_access(handle, gdb_bh);
  824. return err;
  825. }
  826. /*
  827. * Called when we are adding a new group which has a backup copy of each of
  828. * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
  829. * We need to add these reserved backup GDT blocks to the resize inode, so
  830. * that they are kept for future resizing and not allocated to files.
  831. *
  832. * Each reserved backup GDT block will go into a different indirect block.
  833. * The indirect blocks are actually the primary reserved GDT blocks,
  834. * so we know in advance what their block numbers are. We only get the
  835. * double-indirect block to verify it is pointing to the primary reserved
  836. * GDT blocks so we don't overwrite a data block by accident. The reserved
  837. * backup GDT blocks are stored in their reserved primary GDT block.
  838. */
  839. static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
  840. ext4_group_t group)
  841. {
  842. struct super_block *sb = inode->i_sb;
  843. int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
  844. int cluster_bits = EXT4_SB(sb)->s_cluster_bits;
  845. struct buffer_head **primary;
  846. struct buffer_head *dind;
  847. struct ext4_iloc iloc;
  848. ext4_fsblk_t blk;
  849. __le32 *data, *end;
  850. int gdbackups = 0;
  851. int res, i;
  852. int err;
  853. primary = kmalloc_array(reserved_gdb, sizeof(*primary), GFP_NOFS);
  854. if (!primary)
  855. return -ENOMEM;
  856. data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
  857. dind = sb_bread(sb, le32_to_cpu(*data));
  858. if (!dind) {
  859. err = -EIO;
  860. goto exit_free;
  861. }
  862. blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
  863. data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
  864. EXT4_ADDR_PER_BLOCK(sb));
  865. end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
  866. /* Get each reserved primary GDT block and verify it holds backups */
  867. for (res = 0; res < reserved_gdb; res++, blk++) {
  868. if (le32_to_cpu(*data) != blk) {
  869. ext4_warning(sb, "reserved block %llu"
  870. " not at offset %ld",
  871. blk,
  872. (long)(data - (__le32 *)dind->b_data));
  873. err = -EINVAL;
  874. goto exit_bh;
  875. }
  876. primary[res] = sb_bread(sb, blk);
  877. if (!primary[res]) {
  878. err = -EIO;
  879. goto exit_bh;
  880. }
  881. gdbackups = verify_reserved_gdb(sb, group, primary[res]);
  882. if (gdbackups < 0) {
  883. brelse(primary[res]);
  884. err = gdbackups;
  885. goto exit_bh;
  886. }
  887. if (++data >= end)
  888. data = (__le32 *)dind->b_data;
  889. }
  890. for (i = 0; i < reserved_gdb; i++) {
  891. BUFFER_TRACE(primary[i], "get_write_access");
  892. if ((err = ext4_journal_get_write_access(handle, primary[i])))
  893. goto exit_bh;
  894. }
  895. if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
  896. goto exit_bh;
  897. /*
  898. * Finally we can add each of the reserved backup GDT blocks from
  899. * the new group to its reserved primary GDT block.
  900. */
  901. blk = group * EXT4_BLOCKS_PER_GROUP(sb);
  902. for (i = 0; i < reserved_gdb; i++) {
  903. int err2;
  904. data = (__le32 *)primary[i]->b_data;
  905. /* printk("reserving backup %lu[%u] = %lu\n",
  906. primary[i]->b_blocknr, gdbackups,
  907. blk + primary[i]->b_blocknr); */
  908. data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
  909. err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
  910. if (!err)
  911. err = err2;
  912. }
  913. inode->i_blocks += reserved_gdb * sb->s_blocksize >> (9 - cluster_bits);
  914. ext4_mark_iloc_dirty(handle, inode, &iloc);
  915. exit_bh:
  916. while (--res >= 0)
  917. brelse(primary[res]);
  918. brelse(dind);
  919. exit_free:
  920. kfree(primary);
  921. return err;
  922. }
  923. /*
  924. * Update the backup copies of the ext4 metadata. These don't need to be part
  925. * of the main resize transaction, because e2fsck will re-write them if there
  926. * is a problem (basically only OOM will cause a problem). However, we
  927. * _should_ update the backups if possible, in case the primary gets trashed
  928. * for some reason and we need to run e2fsck from a backup superblock. The
  929. * important part is that the new block and inode counts are in the backup
  930. * superblocks, and the location of the new group metadata in the GDT backups.
  931. *
  932. * We do not need take the s_resize_lock for this, because these
  933. * blocks are not otherwise touched by the filesystem code when it is
  934. * mounted. We don't need to worry about last changing from
  935. * sbi->s_groups_count, because the worst that can happen is that we
  936. * do not copy the full number of backups at this time. The resize
  937. * which changed s_groups_count will backup again.
  938. */
  939. static void update_backups(struct super_block *sb, sector_t blk_off, char *data,
  940. int size, int meta_bg)
  941. {
  942. struct ext4_sb_info *sbi = EXT4_SB(sb);
  943. ext4_group_t last;
  944. const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
  945. unsigned three = 1;
  946. unsigned five = 5;
  947. unsigned seven = 7;
  948. ext4_group_t group = 0;
  949. int rest = sb->s_blocksize - size;
  950. handle_t *handle;
  951. int err = 0, err2;
  952. handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA);
  953. if (IS_ERR(handle)) {
  954. group = 1;
  955. err = PTR_ERR(handle);
  956. goto exit_err;
  957. }
  958. if (meta_bg == 0) {
  959. group = ext4_list_backups(sb, &three, &five, &seven);
  960. last = sbi->s_groups_count;
  961. } else {
  962. group = ext4_get_group_number(sb, blk_off) + 1;
  963. last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2);
  964. }
  965. while (group < sbi->s_groups_count) {
  966. struct buffer_head *bh;
  967. ext4_fsblk_t backup_block;
  968. /* Out of journal space, and can't get more - abort - so sad */
  969. if (ext4_handle_valid(handle) &&
  970. handle->h_buffer_credits == 0 &&
  971. ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
  972. (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
  973. break;
  974. if (meta_bg == 0)
  975. backup_block = ((ext4_fsblk_t)group) * bpg + blk_off;
  976. else
  977. backup_block = (ext4_group_first_block_no(sb, group) +
  978. ext4_bg_has_super(sb, group));
  979. bh = sb_getblk(sb, backup_block);
  980. if (unlikely(!bh)) {
  981. err = -ENOMEM;
  982. break;
  983. }
  984. ext4_debug("update metadata backup %llu(+%llu)\n",
  985. backup_block, backup_block -
  986. ext4_group_first_block_no(sb, group));
  987. BUFFER_TRACE(bh, "get_write_access");
  988. if ((err = ext4_journal_get_write_access(handle, bh))) {
  989. brelse(bh);
  990. break;
  991. }
  992. lock_buffer(bh);
  993. memcpy(bh->b_data, data, size);
  994. if (rest)
  995. memset(bh->b_data + size, 0, rest);
  996. set_buffer_uptodate(bh);
  997. unlock_buffer(bh);
  998. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  999. if (unlikely(err))
  1000. ext4_std_error(sb, err);
  1001. brelse(bh);
  1002. if (meta_bg == 0)
  1003. group = ext4_list_backups(sb, &three, &five, &seven);
  1004. else if (group == last)
  1005. break;
  1006. else
  1007. group = last;
  1008. }
  1009. if ((err2 = ext4_journal_stop(handle)) && !err)
  1010. err = err2;
  1011. /*
  1012. * Ugh! Need to have e2fsck write the backup copies. It is too
  1013. * late to revert the resize, we shouldn't fail just because of
  1014. * the backup copies (they are only needed in case of corruption).
  1015. *
  1016. * However, if we got here we have a journal problem too, so we
  1017. * can't really start a transaction to mark the superblock.
  1018. * Chicken out and just set the flag on the hope it will be written
  1019. * to disk, and if not - we will simply wait until next fsck.
  1020. */
  1021. exit_err:
  1022. if (err) {
  1023. ext4_warning(sb, "can't update backup for group %u (err %d), "
  1024. "forcing fsck on next reboot", group, err);
  1025. sbi->s_mount_state &= ~EXT4_VALID_FS;
  1026. sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
  1027. mark_buffer_dirty(sbi->s_sbh);
  1028. }
  1029. }
  1030. /*
  1031. * ext4_add_new_descs() adds @count group descriptor of groups
  1032. * starting at @group
  1033. *
  1034. * @handle: journal handle
  1035. * @sb: super block
  1036. * @group: the group no. of the first group desc to be added
  1037. * @resize_inode: the resize inode
  1038. * @count: number of group descriptors to be added
  1039. */
  1040. static int ext4_add_new_descs(handle_t *handle, struct super_block *sb,
  1041. ext4_group_t group, struct inode *resize_inode,
  1042. ext4_group_t count)
  1043. {
  1044. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1045. struct ext4_super_block *es = sbi->s_es;
  1046. struct buffer_head *gdb_bh;
  1047. int i, gdb_off, gdb_num, err = 0;
  1048. int meta_bg;
  1049. meta_bg = ext4_has_feature_meta_bg(sb);
  1050. for (i = 0; i < count; i++, group++) {
  1051. int reserved_gdb = ext4_bg_has_super(sb, group) ?
  1052. le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
  1053. gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
  1054. gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  1055. /*
  1056. * We will only either add reserved group blocks to a backup group
  1057. * or remove reserved blocks for the first group in a new group block.
  1058. * Doing both would be mean more complex code, and sane people don't
  1059. * use non-sparse filesystems anymore. This is already checked above.
  1060. */
  1061. if (gdb_off) {
  1062. gdb_bh = sbi->s_group_desc[gdb_num];
  1063. BUFFER_TRACE(gdb_bh, "get_write_access");
  1064. err = ext4_journal_get_write_access(handle, gdb_bh);
  1065. if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group))
  1066. err = reserve_backup_gdb(handle, resize_inode, group);
  1067. } else if (meta_bg != 0) {
  1068. err = add_new_gdb_meta_bg(sb, handle, group);
  1069. } else {
  1070. err = add_new_gdb(handle, resize_inode, group);
  1071. }
  1072. if (err)
  1073. break;
  1074. }
  1075. return err;
  1076. }
  1077. static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block)
  1078. {
  1079. struct buffer_head *bh = sb_getblk(sb, block);
  1080. if (unlikely(!bh))
  1081. return NULL;
  1082. if (!bh_uptodate_or_lock(bh)) {
  1083. if (bh_submit_read(bh) < 0) {
  1084. brelse(bh);
  1085. return NULL;
  1086. }
  1087. }
  1088. return bh;
  1089. }
  1090. static int ext4_set_bitmap_checksums(struct super_block *sb,
  1091. ext4_group_t group,
  1092. struct ext4_group_desc *gdp,
  1093. struct ext4_new_group_data *group_data)
  1094. {
  1095. struct buffer_head *bh;
  1096. if (!ext4_has_metadata_csum(sb))
  1097. return 0;
  1098. bh = ext4_get_bitmap(sb, group_data->inode_bitmap);
  1099. if (!bh)
  1100. return -EIO;
  1101. ext4_inode_bitmap_csum_set(sb, group, gdp, bh,
  1102. EXT4_INODES_PER_GROUP(sb) / 8);
  1103. brelse(bh);
  1104. bh = ext4_get_bitmap(sb, group_data->block_bitmap);
  1105. if (!bh)
  1106. return -EIO;
  1107. ext4_block_bitmap_csum_set(sb, group, gdp, bh);
  1108. brelse(bh);
  1109. return 0;
  1110. }
  1111. /*
  1112. * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg
  1113. */
  1114. static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb,
  1115. struct ext4_new_flex_group_data *flex_gd)
  1116. {
  1117. struct ext4_new_group_data *group_data = flex_gd->groups;
  1118. struct ext4_group_desc *gdp;
  1119. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1120. struct buffer_head *gdb_bh;
  1121. ext4_group_t group;
  1122. __u16 *bg_flags = flex_gd->bg_flags;
  1123. int i, gdb_off, gdb_num, err = 0;
  1124. for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) {
  1125. group = group_data->group;
  1126. gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
  1127. gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  1128. /*
  1129. * get_write_access() has been called on gdb_bh by ext4_add_new_desc().
  1130. */
  1131. gdb_bh = sbi->s_group_desc[gdb_num];
  1132. /* Update group descriptor block for new group */
  1133. gdp = (struct ext4_group_desc *)(gdb_bh->b_data +
  1134. gdb_off * EXT4_DESC_SIZE(sb));
  1135. memset(gdp, 0, EXT4_DESC_SIZE(sb));
  1136. ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap);
  1137. ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap);
  1138. err = ext4_set_bitmap_checksums(sb, group, gdp, group_data);
  1139. if (err) {
  1140. ext4_std_error(sb, err);
  1141. break;
  1142. }
  1143. ext4_inode_table_set(sb, gdp, group_data->inode_table);
  1144. ext4_free_group_clusters_set(sb, gdp,
  1145. group_data->free_clusters_count);
  1146. ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
  1147. if (ext4_has_group_desc_csum(sb))
  1148. ext4_itable_unused_set(sb, gdp,
  1149. EXT4_INODES_PER_GROUP(sb));
  1150. gdp->bg_flags = cpu_to_le16(*bg_flags);
  1151. ext4_group_desc_csum_set(sb, group, gdp);
  1152. err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
  1153. if (unlikely(err)) {
  1154. ext4_std_error(sb, err);
  1155. break;
  1156. }
  1157. /*
  1158. * We can allocate memory for mb_alloc based on the new group
  1159. * descriptor
  1160. */
  1161. err = ext4_mb_add_groupinfo(sb, group, gdp);
  1162. if (err)
  1163. break;
  1164. }
  1165. return err;
  1166. }
  1167. /*
  1168. * ext4_update_super() updates the super block so that the newly added
  1169. * groups can be seen by the filesystem.
  1170. *
  1171. * @sb: super block
  1172. * @flex_gd: new added groups
  1173. */
  1174. static void ext4_update_super(struct super_block *sb,
  1175. struct ext4_new_flex_group_data *flex_gd)
  1176. {
  1177. ext4_fsblk_t blocks_count = 0;
  1178. ext4_fsblk_t free_blocks = 0;
  1179. ext4_fsblk_t reserved_blocks = 0;
  1180. struct ext4_new_group_data *group_data = flex_gd->groups;
  1181. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1182. struct ext4_super_block *es = sbi->s_es;
  1183. int i;
  1184. BUG_ON(flex_gd->count == 0 || group_data == NULL);
  1185. /*
  1186. * Make the new blocks and inodes valid next. We do this before
  1187. * increasing the group count so that once the group is enabled,
  1188. * all of its blocks and inodes are already valid.
  1189. *
  1190. * We always allocate group-by-group, then block-by-block or
  1191. * inode-by-inode within a group, so enabling these
  1192. * blocks/inodes before the group is live won't actually let us
  1193. * allocate the new space yet.
  1194. */
  1195. for (i = 0; i < flex_gd->count; i++) {
  1196. blocks_count += group_data[i].blocks_count;
  1197. free_blocks += EXT4_C2B(sbi, group_data[i].free_clusters_count);
  1198. }
  1199. reserved_blocks = ext4_r_blocks_count(es) * 100;
  1200. reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es));
  1201. reserved_blocks *= blocks_count;
  1202. do_div(reserved_blocks, 100);
  1203. ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count);
  1204. ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks);
  1205. le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) *
  1206. flex_gd->count);
  1207. le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) *
  1208. flex_gd->count);
  1209. ext4_debug("free blocks count %llu", ext4_free_blocks_count(es));
  1210. /*
  1211. * We need to protect s_groups_count against other CPUs seeing
  1212. * inconsistent state in the superblock.
  1213. *
  1214. * The precise rules we use are:
  1215. *
  1216. * * Writers must perform a smp_wmb() after updating all
  1217. * dependent data and before modifying the groups count
  1218. *
  1219. * * Readers must perform an smp_rmb() after reading the groups
  1220. * count and before reading any dependent data.
  1221. *
  1222. * NB. These rules can be relaxed when checking the group count
  1223. * while freeing data, as we can only allocate from a block
  1224. * group after serialising against the group count, and we can
  1225. * only then free after serialising in turn against that
  1226. * allocation.
  1227. */
  1228. smp_wmb();
  1229. /* Update the global fs size fields */
  1230. sbi->s_groups_count += flex_gd->count;
  1231. sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
  1232. (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
  1233. /* Update the reserved block counts only once the new group is
  1234. * active. */
  1235. ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
  1236. reserved_blocks);
  1237. /* Update the free space counts */
  1238. percpu_counter_add(&sbi->s_freeclusters_counter,
  1239. EXT4_NUM_B2C(sbi, free_blocks));
  1240. percpu_counter_add(&sbi->s_freeinodes_counter,
  1241. EXT4_INODES_PER_GROUP(sb) * flex_gd->count);
  1242. ext4_debug("free blocks count %llu",
  1243. percpu_counter_read(&sbi->s_freeclusters_counter));
  1244. if (ext4_has_feature_flex_bg(sb) && sbi->s_log_groups_per_flex) {
  1245. ext4_group_t flex_group;
  1246. flex_group = ext4_flex_group(sbi, group_data[0].group);
  1247. atomic64_add(EXT4_NUM_B2C(sbi, free_blocks),
  1248. &sbi->s_flex_groups[flex_group].free_clusters);
  1249. atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count,
  1250. &sbi->s_flex_groups[flex_group].free_inodes);
  1251. }
  1252. /*
  1253. * Update the fs overhead information
  1254. */
  1255. ext4_calculate_overhead(sb);
  1256. if (test_opt(sb, DEBUG))
  1257. printk(KERN_DEBUG "EXT4-fs: added group %u:"
  1258. "%llu blocks(%llu free %llu reserved)\n", flex_gd->count,
  1259. blocks_count, free_blocks, reserved_blocks);
  1260. }
  1261. /* Add a flex group to an fs. Ensure we handle all possible error conditions
  1262. * _before_ we start modifying the filesystem, because we cannot abort the
  1263. * transaction and not have it write the data to disk.
  1264. */
  1265. static int ext4_flex_group_add(struct super_block *sb,
  1266. struct inode *resize_inode,
  1267. struct ext4_new_flex_group_data *flex_gd)
  1268. {
  1269. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1270. struct ext4_super_block *es = sbi->s_es;
  1271. ext4_fsblk_t o_blocks_count;
  1272. ext4_grpblk_t last;
  1273. ext4_group_t group;
  1274. handle_t *handle;
  1275. unsigned reserved_gdb;
  1276. int err = 0, err2 = 0, credit;
  1277. BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags);
  1278. reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
  1279. o_blocks_count = ext4_blocks_count(es);
  1280. ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
  1281. BUG_ON(last);
  1282. err = setup_new_flex_group_blocks(sb, flex_gd);
  1283. if (err)
  1284. goto exit;
  1285. /*
  1286. * We will always be modifying at least the superblock and GDT
  1287. * blocks. If we are adding a group past the last current GDT block,
  1288. * we will also modify the inode and the dindirect block. If we
  1289. * are adding a group with superblock/GDT backups we will also
  1290. * modify each of the reserved GDT dindirect blocks.
  1291. */
  1292. credit = 3; /* sb, resize inode, resize inode dindirect */
  1293. /* GDT blocks */
  1294. credit += 1 + DIV_ROUND_UP(flex_gd->count, EXT4_DESC_PER_BLOCK(sb));
  1295. credit += reserved_gdb; /* Reserved GDT dindirect blocks */
  1296. handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit);
  1297. if (IS_ERR(handle)) {
  1298. err = PTR_ERR(handle);
  1299. goto exit;
  1300. }
  1301. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  1302. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1303. if (err)
  1304. goto exit_journal;
  1305. group = flex_gd->groups[0].group;
  1306. BUG_ON(group != sbi->s_groups_count);
  1307. err = ext4_add_new_descs(handle, sb, group,
  1308. resize_inode, flex_gd->count);
  1309. if (err)
  1310. goto exit_journal;
  1311. err = ext4_setup_new_descs(handle, sb, flex_gd);
  1312. if (err)
  1313. goto exit_journal;
  1314. ext4_update_super(sb, flex_gd);
  1315. err = ext4_handle_dirty_super(handle, sb);
  1316. exit_journal:
  1317. err2 = ext4_journal_stop(handle);
  1318. if (!err)
  1319. err = err2;
  1320. if (!err) {
  1321. int gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  1322. int gdb_num_end = ((group + flex_gd->count - 1) /
  1323. EXT4_DESC_PER_BLOCK(sb));
  1324. int meta_bg = ext4_has_feature_meta_bg(sb);
  1325. sector_t old_gdb = 0;
  1326. update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
  1327. sizeof(struct ext4_super_block), 0);
  1328. for (; gdb_num <= gdb_num_end; gdb_num++) {
  1329. struct buffer_head *gdb_bh;
  1330. gdb_bh = sbi->s_group_desc[gdb_num];
  1331. if (old_gdb == gdb_bh->b_blocknr)
  1332. continue;
  1333. update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data,
  1334. gdb_bh->b_size, meta_bg);
  1335. old_gdb = gdb_bh->b_blocknr;
  1336. }
  1337. }
  1338. exit:
  1339. return err;
  1340. }
  1341. static int ext4_setup_next_flex_gd(struct super_block *sb,
  1342. struct ext4_new_flex_group_data *flex_gd,
  1343. ext4_fsblk_t n_blocks_count,
  1344. unsigned long flexbg_size)
  1345. {
  1346. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1347. struct ext4_super_block *es = sbi->s_es;
  1348. struct ext4_new_group_data *group_data = flex_gd->groups;
  1349. ext4_fsblk_t o_blocks_count;
  1350. ext4_group_t n_group;
  1351. ext4_group_t group;
  1352. ext4_group_t last_group;
  1353. ext4_grpblk_t last;
  1354. ext4_grpblk_t clusters_per_group;
  1355. unsigned long i;
  1356. clusters_per_group = EXT4_CLUSTERS_PER_GROUP(sb);
  1357. o_blocks_count = ext4_blocks_count(es);
  1358. if (o_blocks_count == n_blocks_count)
  1359. return 0;
  1360. ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
  1361. BUG_ON(last);
  1362. ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last);
  1363. last_group = group | (flexbg_size - 1);
  1364. if (last_group > n_group)
  1365. last_group = n_group;
  1366. flex_gd->count = last_group - group + 1;
  1367. for (i = 0; i < flex_gd->count; i++) {
  1368. int overhead;
  1369. group_data[i].group = group + i;
  1370. group_data[i].blocks_count = EXT4_BLOCKS_PER_GROUP(sb);
  1371. overhead = ext4_group_overhead_blocks(sb, group + i);
  1372. group_data[i].mdata_blocks = overhead;
  1373. group_data[i].free_clusters_count = EXT4_CLUSTERS_PER_GROUP(sb);
  1374. if (ext4_has_group_desc_csum(sb)) {
  1375. flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT |
  1376. EXT4_BG_INODE_UNINIT;
  1377. if (!test_opt(sb, INIT_INODE_TABLE))
  1378. flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED;
  1379. } else
  1380. flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED;
  1381. }
  1382. if (last_group == n_group && ext4_has_group_desc_csum(sb))
  1383. /* We need to initialize block bitmap of last group. */
  1384. flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT;
  1385. if ((last_group == n_group) && (last != clusters_per_group - 1)) {
  1386. group_data[i - 1].blocks_count = EXT4_C2B(sbi, last + 1);
  1387. group_data[i - 1].free_clusters_count -= clusters_per_group -
  1388. last - 1;
  1389. }
  1390. return 1;
  1391. }
  1392. /* Add group descriptor data to an existing or new group descriptor block.
  1393. * Ensure we handle all possible error conditions _before_ we start modifying
  1394. * the filesystem, because we cannot abort the transaction and not have it
  1395. * write the data to disk.
  1396. *
  1397. * If we are on a GDT block boundary, we need to get the reserved GDT block.
  1398. * Otherwise, we may need to add backup GDT blocks for a sparse group.
  1399. *
  1400. * We only need to hold the superblock lock while we are actually adding
  1401. * in the new group's counts to the superblock. Prior to that we have
  1402. * not really "added" the group at all. We re-check that we are still
  1403. * adding in the last group in case things have changed since verifying.
  1404. */
  1405. int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
  1406. {
  1407. struct ext4_new_flex_group_data flex_gd;
  1408. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1409. struct ext4_super_block *es = sbi->s_es;
  1410. int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
  1411. le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
  1412. struct inode *inode = NULL;
  1413. int gdb_off;
  1414. int err;
  1415. __u16 bg_flags = 0;
  1416. gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
  1417. if (gdb_off == 0 && !ext4_has_feature_sparse_super(sb)) {
  1418. ext4_warning(sb, "Can't resize non-sparse filesystem further");
  1419. return -EPERM;
  1420. }
  1421. if (ext4_blocks_count(es) + input->blocks_count <
  1422. ext4_blocks_count(es)) {
  1423. ext4_warning(sb, "blocks_count overflow");
  1424. return -EINVAL;
  1425. }
  1426. if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
  1427. le32_to_cpu(es->s_inodes_count)) {
  1428. ext4_warning(sb, "inodes_count overflow");
  1429. return -EINVAL;
  1430. }
  1431. if (reserved_gdb || gdb_off == 0) {
  1432. if (ext4_has_feature_resize_inode(sb) ||
  1433. !le16_to_cpu(es->s_reserved_gdt_blocks)) {
  1434. ext4_warning(sb,
  1435. "No reserved GDT blocks, can't resize");
  1436. return -EPERM;
  1437. }
  1438. inode = ext4_iget(sb, EXT4_RESIZE_INO);
  1439. if (IS_ERR(inode)) {
  1440. ext4_warning(sb, "Error opening resize inode");
  1441. return PTR_ERR(inode);
  1442. }
  1443. }
  1444. err = verify_group_input(sb, input);
  1445. if (err)
  1446. goto out;
  1447. err = ext4_alloc_flex_bg_array(sb, input->group + 1);
  1448. if (err)
  1449. goto out;
  1450. err = ext4_mb_alloc_groupinfo(sb, input->group + 1);
  1451. if (err)
  1452. goto out;
  1453. flex_gd.count = 1;
  1454. flex_gd.groups = input;
  1455. flex_gd.bg_flags = &bg_flags;
  1456. err = ext4_flex_group_add(sb, inode, &flex_gd);
  1457. out:
  1458. iput(inode);
  1459. return err;
  1460. } /* ext4_group_add */
  1461. /*
  1462. * extend a group without checking assuming that checking has been done.
  1463. */
  1464. static int ext4_group_extend_no_check(struct super_block *sb,
  1465. ext4_fsblk_t o_blocks_count, ext4_grpblk_t add)
  1466. {
  1467. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  1468. handle_t *handle;
  1469. int err = 0, err2;
  1470. /* We will update the superblock, one block bitmap, and
  1471. * one group descriptor via ext4_group_add_blocks().
  1472. */
  1473. handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3);
  1474. if (IS_ERR(handle)) {
  1475. err = PTR_ERR(handle);
  1476. ext4_warning(sb, "error %d on journal start", err);
  1477. return err;
  1478. }
  1479. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  1480. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1481. if (err) {
  1482. ext4_warning(sb, "error %d on journal write access", err);
  1483. goto errout;
  1484. }
  1485. ext4_blocks_count_set(es, o_blocks_count + add);
  1486. ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add);
  1487. ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
  1488. o_blocks_count + add);
  1489. /* We add the blocks to the bitmap and set the group need init bit */
  1490. err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
  1491. if (err)
  1492. goto errout;
  1493. ext4_handle_dirty_super(handle, sb);
  1494. ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
  1495. o_blocks_count + add);
  1496. errout:
  1497. err2 = ext4_journal_stop(handle);
  1498. if (err2 && !err)
  1499. err = err2;
  1500. if (!err) {
  1501. if (test_opt(sb, DEBUG))
  1502. printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
  1503. "blocks\n", ext4_blocks_count(es));
  1504. update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr,
  1505. (char *)es, sizeof(struct ext4_super_block), 0);
  1506. }
  1507. return err;
  1508. }
  1509. /*
  1510. * Extend the filesystem to the new number of blocks specified. This entry
  1511. * point is only used to extend the current filesystem to the end of the last
  1512. * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
  1513. * for emergencies (because it has no dependencies on reserved blocks).
  1514. *
  1515. * If we _really_ wanted, we could use default values to call ext4_group_add()
  1516. * allow the "remount" trick to work for arbitrary resizing, assuming enough
  1517. * GDT blocks are reserved to grow to the desired size.
  1518. */
  1519. int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
  1520. ext4_fsblk_t n_blocks_count)
  1521. {
  1522. ext4_fsblk_t o_blocks_count;
  1523. ext4_grpblk_t last;
  1524. ext4_grpblk_t add;
  1525. struct buffer_head *bh;
  1526. int err;
  1527. ext4_group_t group;
  1528. o_blocks_count = ext4_blocks_count(es);
  1529. if (test_opt(sb, DEBUG))
  1530. ext4_msg(sb, KERN_DEBUG,
  1531. "extending last group from %llu to %llu blocks",
  1532. o_blocks_count, n_blocks_count);
  1533. if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
  1534. return 0;
  1535. if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
  1536. ext4_msg(sb, KERN_ERR,
  1537. "filesystem too large to resize to %llu blocks safely",
  1538. n_blocks_count);
  1539. if (sizeof(sector_t) < 8)
  1540. ext4_warning(sb, "CONFIG_LBDAF not enabled");
  1541. return -EINVAL;
  1542. }
  1543. if (n_blocks_count < o_blocks_count) {
  1544. ext4_warning(sb, "can't shrink FS - resize aborted");
  1545. return -EINVAL;
  1546. }
  1547. /* Handle the remaining blocks in the last group only. */
  1548. ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
  1549. if (last == 0) {
  1550. ext4_warning(sb, "need to use ext2online to resize further");
  1551. return -EPERM;
  1552. }
  1553. add = EXT4_BLOCKS_PER_GROUP(sb) - last;
  1554. if (o_blocks_count + add < o_blocks_count) {
  1555. ext4_warning(sb, "blocks_count overflow");
  1556. return -EINVAL;
  1557. }
  1558. if (o_blocks_count + add > n_blocks_count)
  1559. add = n_blocks_count - o_blocks_count;
  1560. if (o_blocks_count + add < n_blocks_count)
  1561. ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
  1562. o_blocks_count + add, add);
  1563. /* See if the device is actually as big as what was requested */
  1564. bh = sb_bread(sb, o_blocks_count + add - 1);
  1565. if (!bh) {
  1566. ext4_warning(sb, "can't read last block, resize aborted");
  1567. return -ENOSPC;
  1568. }
  1569. brelse(bh);
  1570. err = ext4_group_extend_no_check(sb, o_blocks_count, add);
  1571. return err;
  1572. } /* ext4_group_extend */
  1573. static int num_desc_blocks(struct super_block *sb, ext4_group_t groups)
  1574. {
  1575. return (groups + EXT4_DESC_PER_BLOCK(sb) - 1) / EXT4_DESC_PER_BLOCK(sb);
  1576. }
  1577. /*
  1578. * Release the resize inode and drop the resize_inode feature if there
  1579. * are no more reserved gdt blocks, and then convert the file system
  1580. * to enable meta_bg
  1581. */
  1582. static int ext4_convert_meta_bg(struct super_block *sb, struct inode *inode)
  1583. {
  1584. handle_t *handle;
  1585. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1586. struct ext4_super_block *es = sbi->s_es;
  1587. struct ext4_inode_info *ei = EXT4_I(inode);
  1588. ext4_fsblk_t nr;
  1589. int i, ret, err = 0;
  1590. int credits = 1;
  1591. ext4_msg(sb, KERN_INFO, "Converting file system to meta_bg");
  1592. if (inode) {
  1593. if (es->s_reserved_gdt_blocks) {
  1594. ext4_error(sb, "Unexpected non-zero "
  1595. "s_reserved_gdt_blocks");
  1596. return -EPERM;
  1597. }
  1598. /* Do a quick sanity check of the resize inode */
  1599. if (inode->i_blocks != 1 << (inode->i_blkbits -
  1600. (9 - sbi->s_cluster_bits)))
  1601. goto invalid_resize_inode;
  1602. for (i = 0; i < EXT4_N_BLOCKS; i++) {
  1603. if (i == EXT4_DIND_BLOCK) {
  1604. if (ei->i_data[i])
  1605. continue;
  1606. else
  1607. goto invalid_resize_inode;
  1608. }
  1609. if (ei->i_data[i])
  1610. goto invalid_resize_inode;
  1611. }
  1612. credits += 3; /* block bitmap, bg descriptor, resize inode */
  1613. }
  1614. handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits);
  1615. if (IS_ERR(handle))
  1616. return PTR_ERR(handle);
  1617. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  1618. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1619. if (err)
  1620. goto errout;
  1621. ext4_clear_feature_resize_inode(sb);
  1622. ext4_set_feature_meta_bg(sb);
  1623. sbi->s_es->s_first_meta_bg =
  1624. cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count));
  1625. err = ext4_handle_dirty_super(handle, sb);
  1626. if (err) {
  1627. ext4_std_error(sb, err);
  1628. goto errout;
  1629. }
  1630. if (inode) {
  1631. nr = le32_to_cpu(ei->i_data[EXT4_DIND_BLOCK]);
  1632. ext4_free_blocks(handle, inode, NULL, nr, 1,
  1633. EXT4_FREE_BLOCKS_METADATA |
  1634. EXT4_FREE_BLOCKS_FORGET);
  1635. ei->i_data[EXT4_DIND_BLOCK] = 0;
  1636. inode->i_blocks = 0;
  1637. err = ext4_mark_inode_dirty(handle, inode);
  1638. if (err)
  1639. ext4_std_error(sb, err);
  1640. }
  1641. errout:
  1642. ret = ext4_journal_stop(handle);
  1643. if (!err)
  1644. err = ret;
  1645. return ret;
  1646. invalid_resize_inode:
  1647. ext4_error(sb, "corrupted/inconsistent resize inode");
  1648. return -EINVAL;
  1649. }
  1650. /*
  1651. * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count
  1652. *
  1653. * @sb: super block of the fs to be resized
  1654. * @n_blocks_count: the number of blocks resides in the resized fs
  1655. */
  1656. int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
  1657. {
  1658. struct ext4_new_flex_group_data *flex_gd = NULL;
  1659. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1660. struct ext4_super_block *es = sbi->s_es;
  1661. struct buffer_head *bh;
  1662. struct inode *resize_inode = NULL;
  1663. ext4_grpblk_t add, offset;
  1664. unsigned long n_desc_blocks;
  1665. unsigned long o_desc_blocks;
  1666. ext4_group_t o_group;
  1667. ext4_group_t n_group;
  1668. ext4_fsblk_t o_blocks_count;
  1669. ext4_fsblk_t n_blocks_count_retry = 0;
  1670. unsigned long last_update_time = 0;
  1671. int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex;
  1672. int meta_bg;
  1673. /* See if the device is actually as big as what was requested */
  1674. bh = sb_bread(sb, n_blocks_count - 1);
  1675. if (!bh) {
  1676. ext4_warning(sb, "can't read last block, resize aborted");
  1677. return -ENOSPC;
  1678. }
  1679. brelse(bh);
  1680. retry:
  1681. o_blocks_count = ext4_blocks_count(es);
  1682. ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu "
  1683. "to %llu blocks", o_blocks_count, n_blocks_count);
  1684. if (n_blocks_count < o_blocks_count) {
  1685. /* On-line shrinking not supported */
  1686. ext4_warning(sb, "can't shrink FS - resize aborted");
  1687. return -EINVAL;
  1688. }
  1689. if (n_blocks_count == o_blocks_count)
  1690. /* Nothing need to do */
  1691. return 0;
  1692. n_group = ext4_get_group_number(sb, n_blocks_count - 1);
  1693. if (n_group >= (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) {
  1694. ext4_warning(sb, "resize would cause inodes_count overflow");
  1695. return -EINVAL;
  1696. }
  1697. ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset);
  1698. n_desc_blocks = num_desc_blocks(sb, n_group + 1);
  1699. o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count);
  1700. meta_bg = ext4_has_feature_meta_bg(sb);
  1701. if (ext4_has_feature_resize_inode(sb)) {
  1702. if (meta_bg) {
  1703. ext4_error(sb, "resize_inode and meta_bg enabled "
  1704. "simultaneously");
  1705. return -EINVAL;
  1706. }
  1707. if (n_desc_blocks > o_desc_blocks +
  1708. le16_to_cpu(es->s_reserved_gdt_blocks)) {
  1709. n_blocks_count_retry = n_blocks_count;
  1710. n_desc_blocks = o_desc_blocks +
  1711. le16_to_cpu(es->s_reserved_gdt_blocks);
  1712. n_group = n_desc_blocks * EXT4_DESC_PER_BLOCK(sb);
  1713. n_blocks_count = (ext4_fsblk_t)n_group *
  1714. EXT4_BLOCKS_PER_GROUP(sb);
  1715. n_group--; /* set to last group number */
  1716. }
  1717. if (!resize_inode)
  1718. resize_inode = ext4_iget(sb, EXT4_RESIZE_INO);
  1719. if (IS_ERR(resize_inode)) {
  1720. ext4_warning(sb, "Error opening resize inode");
  1721. return PTR_ERR(resize_inode);
  1722. }
  1723. }
  1724. if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) {
  1725. err = ext4_convert_meta_bg(sb, resize_inode);
  1726. if (err)
  1727. goto out;
  1728. if (resize_inode) {
  1729. iput(resize_inode);
  1730. resize_inode = NULL;
  1731. }
  1732. if (n_blocks_count_retry) {
  1733. n_blocks_count = n_blocks_count_retry;
  1734. n_blocks_count_retry = 0;
  1735. goto retry;
  1736. }
  1737. }
  1738. /*
  1739. * Make sure the last group has enough space so that it's
  1740. * guaranteed to have enough space for all metadata blocks
  1741. * that it might need to hold. (We might not need to store
  1742. * the inode table blocks in the last block group, but there
  1743. * will be cases where this might be needed.)
  1744. */
  1745. if ((ext4_group_first_block_no(sb, n_group) +
  1746. ext4_group_overhead_blocks(sb, n_group) + 2 +
  1747. sbi->s_itb_per_group + sbi->s_cluster_ratio) >= n_blocks_count) {
  1748. n_blocks_count = ext4_group_first_block_no(sb, n_group);
  1749. n_group--;
  1750. n_blocks_count_retry = 0;
  1751. if (resize_inode) {
  1752. iput(resize_inode);
  1753. resize_inode = NULL;
  1754. }
  1755. goto retry;
  1756. }
  1757. /* extend the last group */
  1758. if (n_group == o_group)
  1759. add = n_blocks_count - o_blocks_count;
  1760. else
  1761. add = EXT4_C2B(sbi, EXT4_CLUSTERS_PER_GROUP(sb) - (offset + 1));
  1762. if (add > 0) {
  1763. err = ext4_group_extend_no_check(sb, o_blocks_count, add);
  1764. if (err)
  1765. goto out;
  1766. }
  1767. if (ext4_blocks_count(es) == n_blocks_count)
  1768. goto out;
  1769. err = ext4_alloc_flex_bg_array(sb, n_group + 1);
  1770. if (err)
  1771. goto out;
  1772. err = ext4_mb_alloc_groupinfo(sb, n_group + 1);
  1773. if (err)
  1774. goto out;
  1775. flex_gd = alloc_flex_gd(flexbg_size);
  1776. if (flex_gd == NULL) {
  1777. err = -ENOMEM;
  1778. goto out;
  1779. }
  1780. /* Add flex groups. Note that a regular group is a
  1781. * flex group with 1 group.
  1782. */
  1783. while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count,
  1784. flexbg_size)) {
  1785. if (jiffies - last_update_time > HZ * 10) {
  1786. if (last_update_time)
  1787. ext4_msg(sb, KERN_INFO,
  1788. "resized to %llu blocks",
  1789. ext4_blocks_count(es));
  1790. last_update_time = jiffies;
  1791. }
  1792. if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0)
  1793. break;
  1794. err = ext4_flex_group_add(sb, resize_inode, flex_gd);
  1795. if (unlikely(err))
  1796. break;
  1797. }
  1798. if (!err && n_blocks_count_retry) {
  1799. n_blocks_count = n_blocks_count_retry;
  1800. n_blocks_count_retry = 0;
  1801. free_flex_gd(flex_gd);
  1802. flex_gd = NULL;
  1803. if (resize_inode) {
  1804. iput(resize_inode);
  1805. resize_inode = NULL;
  1806. }
  1807. goto retry;
  1808. }
  1809. out:
  1810. if (flex_gd)
  1811. free_flex_gd(flex_gd);
  1812. if (resize_inode != NULL)
  1813. iput(resize_inode);
  1814. ext4_msg(sb, KERN_INFO, "resized filesystem to %llu", n_blocks_count);
  1815. return err;
  1816. }