resize.c 50 KB

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