resize.c 51 KB

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