resize.c 50 KB

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