resize.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  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(-ENOMEM);
  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 = EXT4_BLOCKS_PER_GROUP(sb) - (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 -ENOMEM;
  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. if (!ext4_bg_has_super(sb, group))
  397. goto handle_itb;
  398. /* Copy all of the GDT blocks into the backup in this group */
  399. for (j = 0, block = start + 1; j < gdblocks; j++, block++) {
  400. struct buffer_head *gdb;
  401. ext4_debug("update backup group %#04llx\n", block);
  402. err = extend_or_restart_transaction(handle, 1);
  403. if (err)
  404. goto out;
  405. gdb = sb_getblk(sb, block);
  406. if (!gdb) {
  407. err = -ENOMEM;
  408. goto out;
  409. }
  410. err = ext4_journal_get_write_access(handle, gdb);
  411. if (err) {
  412. brelse(gdb);
  413. goto out;
  414. }
  415. memcpy(gdb->b_data, sbi->s_group_desc[j]->b_data,
  416. gdb->b_size);
  417. set_buffer_uptodate(gdb);
  418. err = ext4_handle_dirty_metadata(handle, NULL, gdb);
  419. if (unlikely(err)) {
  420. brelse(gdb);
  421. goto out;
  422. }
  423. brelse(gdb);
  424. }
  425. /* Zero out all of the reserved backup group descriptor
  426. * table blocks
  427. */
  428. if (ext4_bg_has_super(sb, group)) {
  429. err = sb_issue_zeroout(sb, gdblocks + start + 1,
  430. reserved_gdb, GFP_NOFS);
  431. if (err)
  432. goto out;
  433. }
  434. handle_itb:
  435. /* Initialize group tables of the grop @group */
  436. if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED))
  437. goto handle_bb;
  438. /* Zero out all of the inode table blocks */
  439. block = group_data[i].inode_table;
  440. ext4_debug("clear inode table blocks %#04llx -> %#04lx\n",
  441. block, sbi->s_itb_per_group);
  442. err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group,
  443. GFP_NOFS);
  444. if (err)
  445. goto out;
  446. handle_bb:
  447. if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT)
  448. goto handle_ib;
  449. /* Initialize block bitmap of the @group */
  450. block = group_data[i].block_bitmap;
  451. err = extend_or_restart_transaction(handle, 1);
  452. if (err)
  453. goto out;
  454. bh = bclean(handle, sb, block);
  455. if (IS_ERR(bh)) {
  456. err = PTR_ERR(bh);
  457. bh = NULL;
  458. goto out;
  459. }
  460. if (ext4_bg_has_super(sb, group)) {
  461. ext4_debug("mark backup superblock %#04llx (+0)\n",
  462. start);
  463. ext4_set_bits(bh->b_data, 0, gdblocks + reserved_gdb +
  464. 1);
  465. }
  466. ext4_mark_bitmap_end(group_data[i].blocks_count,
  467. sb->s_blocksize * 8, bh->b_data);
  468. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  469. if (err)
  470. goto out;
  471. brelse(bh);
  472. handle_ib:
  473. if (bg_flags[i] & EXT4_BG_INODE_UNINIT)
  474. continue;
  475. /* Initialize inode bitmap of the @group */
  476. block = group_data[i].inode_bitmap;
  477. err = extend_or_restart_transaction(handle, 1);
  478. if (err)
  479. goto out;
  480. /* Mark unused entries in inode bitmap used */
  481. bh = bclean(handle, sb, block);
  482. if (IS_ERR(bh)) {
  483. err = PTR_ERR(bh);
  484. bh = NULL;
  485. goto out;
  486. }
  487. ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
  488. sb->s_blocksize * 8, bh->b_data);
  489. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  490. if (err)
  491. goto out;
  492. brelse(bh);
  493. }
  494. bh = NULL;
  495. /* Mark group tables in block bitmap */
  496. for (j = 0; j < GROUP_TABLE_COUNT; j++) {
  497. count = group_table_count[j];
  498. start = (&group_data[0].block_bitmap)[j];
  499. block = start;
  500. for (i = 1; i < flex_gd->count; i++) {
  501. block += group_table_count[j];
  502. if (block == (&group_data[i].block_bitmap)[j]) {
  503. count += group_table_count[j];
  504. continue;
  505. }
  506. err = set_flexbg_block_bitmap(sb, handle,
  507. flex_gd, start, count);
  508. if (err)
  509. goto out;
  510. count = group_table_count[j];
  511. start = group_data[i].block_bitmap;
  512. block = start;
  513. }
  514. if (count) {
  515. err = set_flexbg_block_bitmap(sb, handle,
  516. flex_gd, start, count);
  517. if (err)
  518. goto out;
  519. }
  520. }
  521. out:
  522. brelse(bh);
  523. err2 = ext4_journal_stop(handle);
  524. if (err2 && !err)
  525. err = err2;
  526. return err;
  527. }
  528. /*
  529. * Iterate through the groups which hold BACKUP superblock/GDT copies in an
  530. * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
  531. * calling this for the first time. In a sparse filesystem it will be the
  532. * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
  533. * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
  534. */
  535. static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
  536. unsigned *five, unsigned *seven)
  537. {
  538. unsigned *min = three;
  539. int mult = 3;
  540. unsigned ret;
  541. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  542. EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
  543. ret = *min;
  544. *min += 1;
  545. return ret;
  546. }
  547. if (*five < *min) {
  548. min = five;
  549. mult = 5;
  550. }
  551. if (*seven < *min) {
  552. min = seven;
  553. mult = 7;
  554. }
  555. ret = *min;
  556. *min *= mult;
  557. return ret;
  558. }
  559. /*
  560. * Check that all of the backup GDT blocks are held in the primary GDT block.
  561. * It is assumed that they are stored in group order. Returns the number of
  562. * groups in current filesystem that have BACKUPS, or -ve error code.
  563. */
  564. static int verify_reserved_gdb(struct super_block *sb,
  565. ext4_group_t end,
  566. struct buffer_head *primary)
  567. {
  568. const ext4_fsblk_t blk = primary->b_blocknr;
  569. unsigned three = 1;
  570. unsigned five = 5;
  571. unsigned seven = 7;
  572. unsigned grp;
  573. __le32 *p = (__le32 *)primary->b_data;
  574. int gdbackups = 0;
  575. while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
  576. if (le32_to_cpu(*p++) !=
  577. grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
  578. ext4_warning(sb, "reserved GDT %llu"
  579. " missing grp %d (%llu)",
  580. blk, grp,
  581. grp *
  582. (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
  583. blk);
  584. return -EINVAL;
  585. }
  586. if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
  587. return -EFBIG;
  588. }
  589. return gdbackups;
  590. }
  591. /*
  592. * Called when we need to bring a reserved group descriptor table block into
  593. * use from the resize inode. The primary copy of the new GDT block currently
  594. * is an indirect block (under the double indirect block in the resize inode).
  595. * The new backup GDT blocks will be stored as leaf blocks in this indirect
  596. * block, in group order. Even though we know all the block numbers we need,
  597. * we check to ensure that the resize inode has actually reserved these blocks.
  598. *
  599. * Don't need to update the block bitmaps because the blocks are still in use.
  600. *
  601. * We get all of the error cases out of the way, so that we are sure to not
  602. * fail once we start modifying the data on disk, because JBD has no rollback.
  603. */
  604. static int add_new_gdb(handle_t *handle, struct inode *inode,
  605. ext4_group_t group)
  606. {
  607. struct super_block *sb = inode->i_sb;
  608. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  609. unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  610. ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
  611. struct buffer_head **o_group_desc, **n_group_desc;
  612. struct buffer_head *dind;
  613. struct buffer_head *gdb_bh;
  614. int gdbackups;
  615. struct ext4_iloc iloc;
  616. __le32 *data;
  617. int err;
  618. if (test_opt(sb, DEBUG))
  619. printk(KERN_DEBUG
  620. "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
  621. gdb_num);
  622. /*
  623. * If we are not using the primary superblock/GDT copy don't resize,
  624. * because the user tools have no way of handling this. Probably a
  625. * bad time to do it anyways.
  626. */
  627. if (EXT4_SB(sb)->s_sbh->b_blocknr !=
  628. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
  629. ext4_warning(sb, "won't resize using backup superblock at %llu",
  630. (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
  631. return -EPERM;
  632. }
  633. gdb_bh = sb_bread(sb, gdblock);
  634. if (!gdb_bh)
  635. return -EIO;
  636. gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
  637. if (gdbackups < 0) {
  638. err = gdbackups;
  639. goto exit_bh;
  640. }
  641. data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
  642. dind = sb_bread(sb, le32_to_cpu(*data));
  643. if (!dind) {
  644. err = -EIO;
  645. goto exit_bh;
  646. }
  647. data = (__le32 *)dind->b_data;
  648. if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
  649. ext4_warning(sb, "new group %u GDT block %llu not reserved",
  650. group, gdblock);
  651. err = -EINVAL;
  652. goto exit_dind;
  653. }
  654. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  655. if (unlikely(err))
  656. goto exit_dind;
  657. err = ext4_journal_get_write_access(handle, gdb_bh);
  658. if (unlikely(err))
  659. goto exit_sbh;
  660. err = ext4_journal_get_write_access(handle, dind);
  661. if (unlikely(err))
  662. ext4_std_error(sb, err);
  663. /* ext4_reserve_inode_write() gets a reference on the iloc */
  664. err = ext4_reserve_inode_write(handle, inode, &iloc);
  665. if (unlikely(err))
  666. goto exit_dindj;
  667. n_group_desc = ext4_kvmalloc((gdb_num + 1) *
  668. sizeof(struct buffer_head *),
  669. GFP_NOFS);
  670. if (!n_group_desc) {
  671. err = -ENOMEM;
  672. ext4_warning(sb, "not enough memory for %lu groups",
  673. gdb_num + 1);
  674. goto exit_inode;
  675. }
  676. /*
  677. * Finally, we have all of the possible failures behind us...
  678. *
  679. * Remove new GDT block from inode double-indirect block and clear out
  680. * the new GDT block for use (which also "frees" the backup GDT blocks
  681. * from the reserved inode). We don't need to change the bitmaps for
  682. * these blocks, because they are marked as in-use from being in the
  683. * reserved inode, and will become GDT blocks (primary and backup).
  684. */
  685. data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
  686. err = ext4_handle_dirty_metadata(handle, NULL, dind);
  687. if (unlikely(err)) {
  688. ext4_std_error(sb, err);
  689. goto exit_inode;
  690. }
  691. inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
  692. ext4_mark_iloc_dirty(handle, inode, &iloc);
  693. memset(gdb_bh->b_data, 0, sb->s_blocksize);
  694. err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
  695. if (unlikely(err)) {
  696. ext4_std_error(sb, err);
  697. goto exit_inode;
  698. }
  699. brelse(dind);
  700. o_group_desc = EXT4_SB(sb)->s_group_desc;
  701. memcpy(n_group_desc, o_group_desc,
  702. EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
  703. n_group_desc[gdb_num] = gdb_bh;
  704. EXT4_SB(sb)->s_group_desc = n_group_desc;
  705. EXT4_SB(sb)->s_gdb_count++;
  706. ext4_kvfree(o_group_desc);
  707. le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
  708. err = ext4_handle_dirty_super_now(handle, sb);
  709. if (err)
  710. ext4_std_error(sb, err);
  711. return err;
  712. exit_inode:
  713. ext4_kvfree(n_group_desc);
  714. /* ext4_handle_release_buffer(handle, iloc.bh); */
  715. brelse(iloc.bh);
  716. exit_dindj:
  717. /* ext4_handle_release_buffer(handle, dind); */
  718. exit_sbh:
  719. /* ext4_handle_release_buffer(handle, EXT4_SB(sb)->s_sbh); */
  720. exit_dind:
  721. brelse(dind);
  722. exit_bh:
  723. brelse(gdb_bh);
  724. ext4_debug("leaving with error %d\n", err);
  725. return err;
  726. }
  727. /*
  728. * Called when we are adding a new group which has a backup copy of each of
  729. * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
  730. * We need to add these reserved backup GDT blocks to the resize inode, so
  731. * that they are kept for future resizing and not allocated to files.
  732. *
  733. * Each reserved backup GDT block will go into a different indirect block.
  734. * The indirect blocks are actually the primary reserved GDT blocks,
  735. * so we know in advance what their block numbers are. We only get the
  736. * double-indirect block to verify it is pointing to the primary reserved
  737. * GDT blocks so we don't overwrite a data block by accident. The reserved
  738. * backup GDT blocks are stored in their reserved primary GDT block.
  739. */
  740. static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
  741. ext4_group_t group)
  742. {
  743. struct super_block *sb = inode->i_sb;
  744. int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
  745. struct buffer_head **primary;
  746. struct buffer_head *dind;
  747. struct ext4_iloc iloc;
  748. ext4_fsblk_t blk;
  749. __le32 *data, *end;
  750. int gdbackups = 0;
  751. int res, i;
  752. int err;
  753. primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
  754. if (!primary)
  755. return -ENOMEM;
  756. data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
  757. dind = sb_bread(sb, le32_to_cpu(*data));
  758. if (!dind) {
  759. err = -EIO;
  760. goto exit_free;
  761. }
  762. blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count;
  763. data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count %
  764. EXT4_ADDR_PER_BLOCK(sb));
  765. end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb);
  766. /* Get each reserved primary GDT block and verify it holds backups */
  767. for (res = 0; res < reserved_gdb; res++, blk++) {
  768. if (le32_to_cpu(*data) != blk) {
  769. ext4_warning(sb, "reserved block %llu"
  770. " not at offset %ld",
  771. blk,
  772. (long)(data - (__le32 *)dind->b_data));
  773. err = -EINVAL;
  774. goto exit_bh;
  775. }
  776. primary[res] = sb_bread(sb, blk);
  777. if (!primary[res]) {
  778. err = -EIO;
  779. goto exit_bh;
  780. }
  781. gdbackups = verify_reserved_gdb(sb, group, primary[res]);
  782. if (gdbackups < 0) {
  783. brelse(primary[res]);
  784. err = gdbackups;
  785. goto exit_bh;
  786. }
  787. if (++data >= end)
  788. data = (__le32 *)dind->b_data;
  789. }
  790. for (i = 0; i < reserved_gdb; i++) {
  791. if ((err = ext4_journal_get_write_access(handle, primary[i]))) {
  792. /*
  793. int j;
  794. for (j = 0; j < i; j++)
  795. ext4_handle_release_buffer(handle, primary[j]);
  796. */
  797. goto exit_bh;
  798. }
  799. }
  800. if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
  801. goto exit_bh;
  802. /*
  803. * Finally we can add each of the reserved backup GDT blocks from
  804. * the new group to its reserved primary GDT block.
  805. */
  806. blk = group * EXT4_BLOCKS_PER_GROUP(sb);
  807. for (i = 0; i < reserved_gdb; i++) {
  808. int err2;
  809. data = (__le32 *)primary[i]->b_data;
  810. /* printk("reserving backup %lu[%u] = %lu\n",
  811. primary[i]->b_blocknr, gdbackups,
  812. blk + primary[i]->b_blocknr); */
  813. data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
  814. err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]);
  815. if (!err)
  816. err = err2;
  817. }
  818. inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
  819. ext4_mark_iloc_dirty(handle, inode, &iloc);
  820. exit_bh:
  821. while (--res >= 0)
  822. brelse(primary[res]);
  823. brelse(dind);
  824. exit_free:
  825. kfree(primary);
  826. return err;
  827. }
  828. /*
  829. * Update the backup copies of the ext4 metadata. These don't need to be part
  830. * of the main resize transaction, because e2fsck will re-write them if there
  831. * is a problem (basically only OOM will cause a problem). However, we
  832. * _should_ update the backups if possible, in case the primary gets trashed
  833. * for some reason and we need to run e2fsck from a backup superblock. The
  834. * important part is that the new block and inode counts are in the backup
  835. * superblocks, and the location of the new group metadata in the GDT backups.
  836. *
  837. * We do not need take the s_resize_lock for this, because these
  838. * blocks are not otherwise touched by the filesystem code when it is
  839. * mounted. We don't need to worry about last changing from
  840. * sbi->s_groups_count, because the worst that can happen is that we
  841. * do not copy the full number of backups at this time. The resize
  842. * which changed s_groups_count will backup again.
  843. */
  844. static void update_backups(struct super_block *sb,
  845. int blk_off, char *data, int size)
  846. {
  847. struct ext4_sb_info *sbi = EXT4_SB(sb);
  848. const ext4_group_t last = sbi->s_groups_count;
  849. const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
  850. unsigned three = 1;
  851. unsigned five = 5;
  852. unsigned seven = 7;
  853. ext4_group_t group;
  854. int rest = sb->s_blocksize - size;
  855. handle_t *handle;
  856. int err = 0, err2;
  857. handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
  858. if (IS_ERR(handle)) {
  859. group = 1;
  860. err = PTR_ERR(handle);
  861. goto exit_err;
  862. }
  863. ext4_superblock_csum_set(sb, (struct ext4_super_block *)data);
  864. while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
  865. struct buffer_head *bh;
  866. /* Out of journal space, and can't get more - abort - so sad */
  867. if (ext4_handle_valid(handle) &&
  868. handle->h_buffer_credits == 0 &&
  869. ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
  870. (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
  871. break;
  872. bh = sb_getblk(sb, ((ext4_fsblk_t)group) * bpg + blk_off);
  873. if (!bh) {
  874. err = -ENOMEM;
  875. break;
  876. }
  877. ext4_debug("update metadata backup %#04lx\n",
  878. (unsigned long)bh->b_blocknr);
  879. if ((err = ext4_journal_get_write_access(handle, bh)))
  880. break;
  881. lock_buffer(bh);
  882. memcpy(bh->b_data, data, size);
  883. if (rest)
  884. memset(bh->b_data + size, 0, rest);
  885. set_buffer_uptodate(bh);
  886. unlock_buffer(bh);
  887. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  888. if (unlikely(err))
  889. ext4_std_error(sb, err);
  890. brelse(bh);
  891. }
  892. if ((err2 = ext4_journal_stop(handle)) && !err)
  893. err = err2;
  894. /*
  895. * Ugh! Need to have e2fsck write the backup copies. It is too
  896. * late to revert the resize, we shouldn't fail just because of
  897. * the backup copies (they are only needed in case of corruption).
  898. *
  899. * However, if we got here we have a journal problem too, so we
  900. * can't really start a transaction to mark the superblock.
  901. * Chicken out and just set the flag on the hope it will be written
  902. * to disk, and if not - we will simply wait until next fsck.
  903. */
  904. exit_err:
  905. if (err) {
  906. ext4_warning(sb, "can't update backup for group %u (err %d), "
  907. "forcing fsck on next reboot", group, err);
  908. sbi->s_mount_state &= ~EXT4_VALID_FS;
  909. sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
  910. mark_buffer_dirty(sbi->s_sbh);
  911. }
  912. }
  913. /*
  914. * ext4_add_new_descs() adds @count group descriptor of groups
  915. * starting at @group
  916. *
  917. * @handle: journal handle
  918. * @sb: super block
  919. * @group: the group no. of the first group desc to be added
  920. * @resize_inode: the resize inode
  921. * @count: number of group descriptors to be added
  922. */
  923. static int ext4_add_new_descs(handle_t *handle, struct super_block *sb,
  924. ext4_group_t group, struct inode *resize_inode,
  925. ext4_group_t count)
  926. {
  927. struct ext4_sb_info *sbi = EXT4_SB(sb);
  928. struct ext4_super_block *es = sbi->s_es;
  929. struct buffer_head *gdb_bh;
  930. int i, gdb_off, gdb_num, err = 0;
  931. for (i = 0; i < count; i++, group++) {
  932. int reserved_gdb = ext4_bg_has_super(sb, group) ?
  933. le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
  934. gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
  935. gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  936. /*
  937. * We will only either add reserved group blocks to a backup group
  938. * or remove reserved blocks for the first group in a new group block.
  939. * Doing both would be mean more complex code, and sane people don't
  940. * use non-sparse filesystems anymore. This is already checked above.
  941. */
  942. if (gdb_off) {
  943. gdb_bh = sbi->s_group_desc[gdb_num];
  944. err = ext4_journal_get_write_access(handle, gdb_bh);
  945. if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group))
  946. err = reserve_backup_gdb(handle, resize_inode, group);
  947. } else
  948. err = add_new_gdb(handle, resize_inode, group);
  949. if (err)
  950. break;
  951. }
  952. return err;
  953. }
  954. static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block)
  955. {
  956. struct buffer_head *bh = sb_getblk(sb, block);
  957. if (!bh)
  958. return NULL;
  959. if (bitmap_uptodate(bh))
  960. return bh;
  961. lock_buffer(bh);
  962. if (bh_submit_read(bh) < 0) {
  963. unlock_buffer(bh);
  964. brelse(bh);
  965. return NULL;
  966. }
  967. unlock_buffer(bh);
  968. return bh;
  969. }
  970. static int ext4_set_bitmap_checksums(struct super_block *sb,
  971. ext4_group_t group,
  972. struct ext4_group_desc *gdp,
  973. struct ext4_new_group_data *group_data)
  974. {
  975. struct buffer_head *bh;
  976. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  977. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  978. return 0;
  979. bh = ext4_get_bitmap(sb, group_data->inode_bitmap);
  980. if (!bh)
  981. return -EIO;
  982. ext4_inode_bitmap_csum_set(sb, group, gdp, bh,
  983. EXT4_INODES_PER_GROUP(sb) / 8);
  984. brelse(bh);
  985. return 0;
  986. }
  987. /*
  988. * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg
  989. */
  990. static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb,
  991. struct ext4_new_flex_group_data *flex_gd)
  992. {
  993. struct ext4_new_group_data *group_data = flex_gd->groups;
  994. struct ext4_group_desc *gdp;
  995. struct ext4_sb_info *sbi = EXT4_SB(sb);
  996. struct buffer_head *gdb_bh;
  997. ext4_group_t group;
  998. __u16 *bg_flags = flex_gd->bg_flags;
  999. int i, gdb_off, gdb_num, err = 0;
  1000. for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) {
  1001. group = group_data->group;
  1002. gdb_off = group % EXT4_DESC_PER_BLOCK(sb);
  1003. gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  1004. /*
  1005. * get_write_access() has been called on gdb_bh by ext4_add_new_desc().
  1006. */
  1007. gdb_bh = sbi->s_group_desc[gdb_num];
  1008. /* Update group descriptor block for new group */
  1009. gdp = (struct ext4_group_desc *)((char *)gdb_bh->b_data +
  1010. gdb_off * EXT4_DESC_SIZE(sb));
  1011. memset(gdp, 0, EXT4_DESC_SIZE(sb));
  1012. ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap);
  1013. ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap);
  1014. err = ext4_set_bitmap_checksums(sb, group, gdp, group_data);
  1015. if (err) {
  1016. ext4_std_error(sb, err);
  1017. break;
  1018. }
  1019. ext4_inode_table_set(sb, gdp, group_data->inode_table);
  1020. ext4_free_group_clusters_set(sb, gdp,
  1021. EXT4_NUM_B2C(sbi, group_data->free_blocks_count));
  1022. ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb));
  1023. gdp->bg_flags = cpu_to_le16(*bg_flags);
  1024. gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
  1025. err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh);
  1026. if (unlikely(err)) {
  1027. ext4_std_error(sb, err);
  1028. break;
  1029. }
  1030. /*
  1031. * We can allocate memory for mb_alloc based on the new group
  1032. * descriptor
  1033. */
  1034. err = ext4_mb_add_groupinfo(sb, group, gdp);
  1035. if (err)
  1036. break;
  1037. }
  1038. return err;
  1039. }
  1040. /*
  1041. * ext4_update_super() updates the super block so that the newly added
  1042. * groups can be seen by the filesystem.
  1043. *
  1044. * @sb: super block
  1045. * @flex_gd: new added groups
  1046. */
  1047. static void ext4_update_super(struct super_block *sb,
  1048. struct ext4_new_flex_group_data *flex_gd)
  1049. {
  1050. ext4_fsblk_t blocks_count = 0;
  1051. ext4_fsblk_t free_blocks = 0;
  1052. ext4_fsblk_t reserved_blocks = 0;
  1053. struct ext4_new_group_data *group_data = flex_gd->groups;
  1054. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1055. struct ext4_super_block *es = sbi->s_es;
  1056. int i;
  1057. BUG_ON(flex_gd->count == 0 || group_data == NULL);
  1058. /*
  1059. * Make the new blocks and inodes valid next. We do this before
  1060. * increasing the group count so that once the group is enabled,
  1061. * all of its blocks and inodes are already valid.
  1062. *
  1063. * We always allocate group-by-group, then block-by-block or
  1064. * inode-by-inode within a group, so enabling these
  1065. * blocks/inodes before the group is live won't actually let us
  1066. * allocate the new space yet.
  1067. */
  1068. for (i = 0; i < flex_gd->count; i++) {
  1069. blocks_count += group_data[i].blocks_count;
  1070. free_blocks += group_data[i].free_blocks_count;
  1071. }
  1072. reserved_blocks = ext4_r_blocks_count(es) * 100;
  1073. do_div(reserved_blocks, ext4_blocks_count(es));
  1074. reserved_blocks *= blocks_count;
  1075. do_div(reserved_blocks, 100);
  1076. ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count);
  1077. ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks);
  1078. le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) *
  1079. flex_gd->count);
  1080. le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) *
  1081. flex_gd->count);
  1082. /*
  1083. * We need to protect s_groups_count against other CPUs seeing
  1084. * inconsistent state in the superblock.
  1085. *
  1086. * The precise rules we use are:
  1087. *
  1088. * * Writers must perform a smp_wmb() after updating all
  1089. * dependent data and before modifying the groups count
  1090. *
  1091. * * Readers must perform an smp_rmb() after reading the groups
  1092. * count and before reading any dependent data.
  1093. *
  1094. * NB. These rules can be relaxed when checking the group count
  1095. * while freeing data, as we can only allocate from a block
  1096. * group after serialising against the group count, and we can
  1097. * only then free after serialising in turn against that
  1098. * allocation.
  1099. */
  1100. smp_wmb();
  1101. /* Update the global fs size fields */
  1102. sbi->s_groups_count += flex_gd->count;
  1103. sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
  1104. (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
  1105. /* Update the reserved block counts only once the new group is
  1106. * active. */
  1107. ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
  1108. reserved_blocks);
  1109. /* Update the free space counts */
  1110. percpu_counter_add(&sbi->s_freeclusters_counter,
  1111. EXT4_NUM_B2C(sbi, free_blocks));
  1112. percpu_counter_add(&sbi->s_freeinodes_counter,
  1113. EXT4_INODES_PER_GROUP(sb) * flex_gd->count);
  1114. if (EXT4_HAS_INCOMPAT_FEATURE(sb,
  1115. EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
  1116. sbi->s_log_groups_per_flex) {
  1117. ext4_group_t flex_group;
  1118. flex_group = ext4_flex_group(sbi, group_data[0].group);
  1119. atomic64_add(EXT4_NUM_B2C(sbi, free_blocks),
  1120. &sbi->s_flex_groups[flex_group].free_clusters);
  1121. atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count,
  1122. &sbi->s_flex_groups[flex_group].free_inodes);
  1123. }
  1124. /*
  1125. * Update the fs overhead information
  1126. */
  1127. ext4_calculate_overhead(sb);
  1128. if (test_opt(sb, DEBUG))
  1129. printk(KERN_DEBUG "EXT4-fs: added group %u:"
  1130. "%llu blocks(%llu free %llu reserved)\n", flex_gd->count,
  1131. blocks_count, free_blocks, reserved_blocks);
  1132. }
  1133. /* Add a flex group to an fs. Ensure we handle all possible error conditions
  1134. * _before_ we start modifying the filesystem, because we cannot abort the
  1135. * transaction and not have it write the data to disk.
  1136. */
  1137. static int ext4_flex_group_add(struct super_block *sb,
  1138. struct inode *resize_inode,
  1139. struct ext4_new_flex_group_data *flex_gd)
  1140. {
  1141. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1142. struct ext4_super_block *es = sbi->s_es;
  1143. ext4_fsblk_t o_blocks_count;
  1144. ext4_grpblk_t last;
  1145. ext4_group_t group;
  1146. handle_t *handle;
  1147. unsigned reserved_gdb;
  1148. int err = 0, err2 = 0, credit;
  1149. BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags);
  1150. reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks);
  1151. o_blocks_count = ext4_blocks_count(es);
  1152. ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
  1153. BUG_ON(last);
  1154. err = setup_new_flex_group_blocks(sb, flex_gd);
  1155. if (err)
  1156. goto exit;
  1157. /*
  1158. * We will always be modifying at least the superblock and GDT
  1159. * block. If we are adding a group past the last current GDT block,
  1160. * we will also modify the inode and the dindirect block. If we
  1161. * are adding a group with superblock/GDT backups we will also
  1162. * modify each of the reserved GDT dindirect blocks.
  1163. */
  1164. credit = flex_gd->count * 4 + reserved_gdb;
  1165. handle = ext4_journal_start_sb(sb, credit);
  1166. if (IS_ERR(handle)) {
  1167. err = PTR_ERR(handle);
  1168. goto exit;
  1169. }
  1170. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1171. if (err)
  1172. goto exit_journal;
  1173. group = flex_gd->groups[0].group;
  1174. BUG_ON(group != EXT4_SB(sb)->s_groups_count);
  1175. err = ext4_add_new_descs(handle, sb, group,
  1176. resize_inode, flex_gd->count);
  1177. if (err)
  1178. goto exit_journal;
  1179. err = ext4_setup_new_descs(handle, sb, flex_gd);
  1180. if (err)
  1181. goto exit_journal;
  1182. ext4_update_super(sb, flex_gd);
  1183. err = ext4_handle_dirty_super(handle, sb);
  1184. exit_journal:
  1185. err2 = ext4_journal_stop(handle);
  1186. if (!err)
  1187. err = err2;
  1188. if (!err) {
  1189. int gdb_num = group / EXT4_DESC_PER_BLOCK(sb);
  1190. int gdb_num_end = ((group + flex_gd->count - 1) /
  1191. EXT4_DESC_PER_BLOCK(sb));
  1192. update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
  1193. sizeof(struct ext4_super_block));
  1194. for (; gdb_num <= gdb_num_end; gdb_num++) {
  1195. struct buffer_head *gdb_bh;
  1196. gdb_bh = sbi->s_group_desc[gdb_num];
  1197. update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data,
  1198. gdb_bh->b_size);
  1199. }
  1200. }
  1201. exit:
  1202. return err;
  1203. }
  1204. static int ext4_setup_next_flex_gd(struct super_block *sb,
  1205. struct ext4_new_flex_group_data *flex_gd,
  1206. ext4_fsblk_t n_blocks_count,
  1207. unsigned long flexbg_size)
  1208. {
  1209. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  1210. struct ext4_new_group_data *group_data = flex_gd->groups;
  1211. ext4_fsblk_t o_blocks_count;
  1212. ext4_group_t n_group;
  1213. ext4_group_t group;
  1214. ext4_group_t last_group;
  1215. ext4_grpblk_t last;
  1216. ext4_grpblk_t blocks_per_group;
  1217. unsigned long i;
  1218. blocks_per_group = EXT4_BLOCKS_PER_GROUP(sb);
  1219. o_blocks_count = ext4_blocks_count(es);
  1220. if (o_blocks_count == n_blocks_count)
  1221. return 0;
  1222. ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
  1223. BUG_ON(last);
  1224. ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last);
  1225. last_group = group | (flexbg_size - 1);
  1226. if (last_group > n_group)
  1227. last_group = n_group;
  1228. flex_gd->count = last_group - group + 1;
  1229. for (i = 0; i < flex_gd->count; i++) {
  1230. int overhead;
  1231. group_data[i].group = group + i;
  1232. group_data[i].blocks_count = blocks_per_group;
  1233. overhead = ext4_bg_has_super(sb, group + i) ?
  1234. (1 + ext4_bg_num_gdb(sb, group + i) +
  1235. le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
  1236. group_data[i].free_blocks_count = blocks_per_group - overhead;
  1237. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  1238. EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
  1239. flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT |
  1240. EXT4_BG_INODE_UNINIT;
  1241. else
  1242. flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED;
  1243. }
  1244. if (last_group == n_group &&
  1245. EXT4_HAS_RO_COMPAT_FEATURE(sb,
  1246. EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
  1247. /* We need to initialize block bitmap of last group. */
  1248. flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT;
  1249. if ((last_group == n_group) && (last != blocks_per_group - 1)) {
  1250. group_data[i - 1].blocks_count = last + 1;
  1251. group_data[i - 1].free_blocks_count -= blocks_per_group-
  1252. last - 1;
  1253. }
  1254. return 1;
  1255. }
  1256. /* Add group descriptor data to an existing or new group descriptor block.
  1257. * Ensure we handle all possible error conditions _before_ we start modifying
  1258. * the filesystem, because we cannot abort the transaction and not have it
  1259. * write the data to disk.
  1260. *
  1261. * If we are on a GDT block boundary, we need to get the reserved GDT block.
  1262. * Otherwise, we may need to add backup GDT blocks for a sparse group.
  1263. *
  1264. * We only need to hold the superblock lock while we are actually adding
  1265. * in the new group's counts to the superblock. Prior to that we have
  1266. * not really "added" the group at all. We re-check that we are still
  1267. * adding in the last group in case things have changed since verifying.
  1268. */
  1269. int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
  1270. {
  1271. struct ext4_new_flex_group_data flex_gd;
  1272. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1273. struct ext4_super_block *es = sbi->s_es;
  1274. int reserved_gdb = ext4_bg_has_super(sb, input->group) ?
  1275. le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
  1276. struct inode *inode = NULL;
  1277. int gdb_off, gdb_num;
  1278. int err;
  1279. __u16 bg_flags = 0;
  1280. gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
  1281. gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
  1282. if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
  1283. EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
  1284. ext4_warning(sb, "Can't resize non-sparse filesystem further");
  1285. return -EPERM;
  1286. }
  1287. if (ext4_blocks_count(es) + input->blocks_count <
  1288. ext4_blocks_count(es)) {
  1289. ext4_warning(sb, "blocks_count overflow");
  1290. return -EINVAL;
  1291. }
  1292. if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
  1293. le32_to_cpu(es->s_inodes_count)) {
  1294. ext4_warning(sb, "inodes_count overflow");
  1295. return -EINVAL;
  1296. }
  1297. if (reserved_gdb || gdb_off == 0) {
  1298. if (!EXT4_HAS_COMPAT_FEATURE(sb,
  1299. EXT4_FEATURE_COMPAT_RESIZE_INODE)
  1300. || !le16_to_cpu(es->s_reserved_gdt_blocks)) {
  1301. ext4_warning(sb,
  1302. "No reserved GDT blocks, can't resize");
  1303. return -EPERM;
  1304. }
  1305. inode = ext4_iget(sb, EXT4_RESIZE_INO);
  1306. if (IS_ERR(inode)) {
  1307. ext4_warning(sb, "Error opening resize inode");
  1308. return PTR_ERR(inode);
  1309. }
  1310. }
  1311. err = verify_group_input(sb, input);
  1312. if (err)
  1313. goto out;
  1314. flex_gd.count = 1;
  1315. flex_gd.groups = input;
  1316. flex_gd.bg_flags = &bg_flags;
  1317. err = ext4_flex_group_add(sb, inode, &flex_gd);
  1318. out:
  1319. iput(inode);
  1320. return err;
  1321. } /* ext4_group_add */
  1322. /*
  1323. * extend a group without checking assuming that checking has been done.
  1324. */
  1325. static int ext4_group_extend_no_check(struct super_block *sb,
  1326. ext4_fsblk_t o_blocks_count, ext4_grpblk_t add)
  1327. {
  1328. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  1329. handle_t *handle;
  1330. int err = 0, err2;
  1331. /* We will update the superblock, one block bitmap, and
  1332. * one group descriptor via ext4_group_add_blocks().
  1333. */
  1334. handle = ext4_journal_start_sb(sb, 3);
  1335. if (IS_ERR(handle)) {
  1336. err = PTR_ERR(handle);
  1337. ext4_warning(sb, "error %d on journal start", err);
  1338. return err;
  1339. }
  1340. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1341. if (err) {
  1342. ext4_warning(sb, "error %d on journal write access", err);
  1343. goto errout;
  1344. }
  1345. ext4_blocks_count_set(es, o_blocks_count + add);
  1346. ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add);
  1347. ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
  1348. o_blocks_count + add);
  1349. /* We add the blocks to the bitmap and set the group need init bit */
  1350. err = ext4_group_add_blocks(handle, sb, o_blocks_count, add);
  1351. if (err)
  1352. goto errout;
  1353. ext4_handle_dirty_super(handle, sb);
  1354. ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
  1355. o_blocks_count + add);
  1356. errout:
  1357. err2 = ext4_journal_stop(handle);
  1358. if (err2 && !err)
  1359. err = err2;
  1360. if (!err) {
  1361. if (test_opt(sb, DEBUG))
  1362. printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
  1363. "blocks\n", ext4_blocks_count(es));
  1364. update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
  1365. sizeof(struct ext4_super_block));
  1366. }
  1367. return err;
  1368. }
  1369. /*
  1370. * Extend the filesystem to the new number of blocks specified. This entry
  1371. * point is only used to extend the current filesystem to the end of the last
  1372. * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
  1373. * for emergencies (because it has no dependencies on reserved blocks).
  1374. *
  1375. * If we _really_ wanted, we could use default values to call ext4_group_add()
  1376. * allow the "remount" trick to work for arbitrary resizing, assuming enough
  1377. * GDT blocks are reserved to grow to the desired size.
  1378. */
  1379. int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
  1380. ext4_fsblk_t n_blocks_count)
  1381. {
  1382. ext4_fsblk_t o_blocks_count;
  1383. ext4_grpblk_t last;
  1384. ext4_grpblk_t add;
  1385. struct buffer_head *bh;
  1386. int err;
  1387. ext4_group_t group;
  1388. o_blocks_count = ext4_blocks_count(es);
  1389. if (test_opt(sb, DEBUG))
  1390. ext4_msg(sb, KERN_DEBUG,
  1391. "extending last group from %llu to %llu blocks",
  1392. o_blocks_count, n_blocks_count);
  1393. if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
  1394. return 0;
  1395. if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
  1396. ext4_msg(sb, KERN_ERR,
  1397. "filesystem too large to resize to %llu blocks safely",
  1398. n_blocks_count);
  1399. if (sizeof(sector_t) < 8)
  1400. ext4_warning(sb, "CONFIG_LBDAF not enabled");
  1401. return -EINVAL;
  1402. }
  1403. if (n_blocks_count < o_blocks_count) {
  1404. ext4_warning(sb, "can't shrink FS - resize aborted");
  1405. return -EINVAL;
  1406. }
  1407. /* Handle the remaining blocks in the last group only. */
  1408. ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last);
  1409. if (last == 0) {
  1410. ext4_warning(sb, "need to use ext2online to resize further");
  1411. return -EPERM;
  1412. }
  1413. add = EXT4_BLOCKS_PER_GROUP(sb) - last;
  1414. if (o_blocks_count + add < o_blocks_count) {
  1415. ext4_warning(sb, "blocks_count overflow");
  1416. return -EINVAL;
  1417. }
  1418. if (o_blocks_count + add > n_blocks_count)
  1419. add = n_blocks_count - o_blocks_count;
  1420. if (o_blocks_count + add < n_blocks_count)
  1421. ext4_warning(sb, "will only finish group (%llu blocks, %u new)",
  1422. o_blocks_count + add, add);
  1423. /* See if the device is actually as big as what was requested */
  1424. bh = sb_bread(sb, o_blocks_count + add - 1);
  1425. if (!bh) {
  1426. ext4_warning(sb, "can't read last block, resize aborted");
  1427. return -ENOSPC;
  1428. }
  1429. brelse(bh);
  1430. err = ext4_group_extend_no_check(sb, o_blocks_count, add);
  1431. return err;
  1432. } /* ext4_group_extend */
  1433. /*
  1434. * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count
  1435. *
  1436. * @sb: super block of the fs to be resized
  1437. * @n_blocks_count: the number of blocks resides in the resized fs
  1438. */
  1439. int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count)
  1440. {
  1441. struct ext4_new_flex_group_data *flex_gd = NULL;
  1442. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1443. struct ext4_super_block *es = sbi->s_es;
  1444. struct buffer_head *bh;
  1445. struct inode *resize_inode;
  1446. ext4_fsblk_t o_blocks_count;
  1447. ext4_group_t o_group;
  1448. ext4_group_t n_group;
  1449. ext4_grpblk_t offset, add;
  1450. unsigned long n_desc_blocks;
  1451. unsigned long o_desc_blocks;
  1452. unsigned long desc_blocks;
  1453. int err = 0, flexbg_size = 1;
  1454. o_blocks_count = ext4_blocks_count(es);
  1455. if (test_opt(sb, DEBUG))
  1456. ext4_msg(sb, KERN_DEBUG, "resizing filesystem from %llu "
  1457. "to %llu blocks", o_blocks_count, n_blocks_count);
  1458. if (n_blocks_count < o_blocks_count) {
  1459. /* On-line shrinking not supported */
  1460. ext4_warning(sb, "can't shrink FS - resize aborted");
  1461. return -EINVAL;
  1462. }
  1463. if (n_blocks_count == o_blocks_count)
  1464. /* Nothing need to do */
  1465. return 0;
  1466. ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &offset);
  1467. if (n_group > (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) {
  1468. ext4_warning(sb, "resize would cause inodes_count overflow");
  1469. return -EINVAL;
  1470. }
  1471. ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset);
  1472. n_desc_blocks = (n_group + EXT4_DESC_PER_BLOCK(sb)) /
  1473. EXT4_DESC_PER_BLOCK(sb);
  1474. o_desc_blocks = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
  1475. EXT4_DESC_PER_BLOCK(sb);
  1476. desc_blocks = n_desc_blocks - o_desc_blocks;
  1477. if (desc_blocks &&
  1478. (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_RESIZE_INODE) ||
  1479. le16_to_cpu(es->s_reserved_gdt_blocks) < desc_blocks)) {
  1480. ext4_warning(sb, "No reserved GDT blocks, can't resize");
  1481. return -EPERM;
  1482. }
  1483. resize_inode = ext4_iget(sb, EXT4_RESIZE_INO);
  1484. if (IS_ERR(resize_inode)) {
  1485. ext4_warning(sb, "Error opening resize inode");
  1486. return PTR_ERR(resize_inode);
  1487. }
  1488. /* See if the device is actually as big as what was requested */
  1489. bh = sb_bread(sb, n_blocks_count - 1);
  1490. if (!bh) {
  1491. ext4_warning(sb, "can't read last block, resize aborted");
  1492. return -ENOSPC;
  1493. }
  1494. brelse(bh);
  1495. /* extend the last group */
  1496. if (n_group == o_group)
  1497. add = n_blocks_count - o_blocks_count;
  1498. else
  1499. add = EXT4_BLOCKS_PER_GROUP(sb) - (offset + 1);
  1500. if (add > 0) {
  1501. err = ext4_group_extend_no_check(sb, o_blocks_count, add);
  1502. if (err)
  1503. goto out;
  1504. }
  1505. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
  1506. es->s_log_groups_per_flex)
  1507. flexbg_size = 1 << es->s_log_groups_per_flex;
  1508. o_blocks_count = ext4_blocks_count(es);
  1509. if (o_blocks_count == n_blocks_count)
  1510. goto out;
  1511. flex_gd = alloc_flex_gd(flexbg_size);
  1512. if (flex_gd == NULL) {
  1513. err = -ENOMEM;
  1514. goto out;
  1515. }
  1516. /* Add flex groups. Note that a regular group is a
  1517. * flex group with 1 group.
  1518. */
  1519. while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count,
  1520. flexbg_size)) {
  1521. if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0)
  1522. break;
  1523. err = ext4_flex_group_add(sb, resize_inode, flex_gd);
  1524. if (unlikely(err))
  1525. break;
  1526. }
  1527. out:
  1528. if (flex_gd)
  1529. free_flex_gd(flex_gd);
  1530. iput(resize_inode);
  1531. if (test_opt(sb, DEBUG))
  1532. ext4_msg(sb, KERN_DEBUG, "resized filesystem from %llu "
  1533. "upto %llu blocks", o_blocks_count, n_blocks_count);
  1534. return err;
  1535. }