2 * resize2fs.c --- ext2 main routine
4 * Copyright (C) 1997, 1998 by Theodore Ts'o and
7 * Copyright (C) 1999, 2000 by Theosore Ts'o
10 * This file may be redistributed under the terms of the GNU Public
16 * Resizing a filesystem consists of the following phases:
18 * 1. Adjust superblock and write out new parts of the inode
20 * 2. Determine blocks which need to be relocated, and copy the
21 * contents of blocks from their old locations to the new ones.
22 * 3. Scan the inode table, doing the following:
23 * a. If blocks have been moved, update the block
24 * pointers in the inodes and indirect blocks to
25 * point at the new block locations.
26 * b. If parts of the inode table need to be evacuated,
27 * copy inodes from their old locations to their
29 * c. If (b) needs to be done, note which blocks contain
30 * directory information, since we will need to
31 * update the directory information.
32 * 4. Update the directory blocks with the new inode locations.
33 * 5. Move the inode tables, if necessary.
36 #include "resize2fs.h"
39 #ifdef __linux__ /* Kludge for debugging */
40 #define RESIZE2FS_DEBUG
43 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size);
44 static errcode_t blocks_to_move(ext2_resize_t rfs);
45 static errcode_t block_mover(ext2_resize_t rfs);
46 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
47 static errcode_t inode_ref_fix(ext2_resize_t rfs);
48 static errcode_t move_itables(ext2_resize_t rfs);
49 static errcode_t fix_resize_inode(ext2_filsys fs);
50 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
53 * Some helper CPP macros
55 #define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap)
56 #define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap)
57 #define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table)
59 #define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i)))
60 #define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i)))
62 #define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \
63 ((blk) < (FS_INODE_TB((fs), (i)) + \
64 (fs)->inode_blocks_per_group)))
66 #define META_OVERHEAD(fs) (2 + (fs)->inode_blocks_per_group)
67 #define SUPER_OVERHEAD(fs) (1 + (fs)->desc_blocks +\
68 (fs)->super->s_reserved_gdt_blocks)
71 * This is the top-level routine which does the dirty deed....
73 errcode_t resize_fs(ext2_filsys fs, blk_t *new_size, int flags,
74 errcode_t (*progress)(ext2_resize_t rfs, int pass,
76 unsigned long max_val))
81 retval = ext2fs_read_bitmaps(fs);
86 * Create the data structure
88 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
91 memset(rfs, 0, sizeof(struct ext2_resize_struct));
96 rfs->progress = progress;
97 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
101 retval = adjust_superblock(rfs, *new_size);
105 *new_size = rfs->new_fs->super->s_blocks_count;
107 retval = blocks_to_move(rfs);
111 #ifdef RESIZE2FS_DEBUG
112 if (rfs->flags & RESIZE_DEBUG_BMOVE)
113 printf("Number of free blocks: %u/%u, Needed: %d\n",
114 rfs->old_fs->super->s_free_blocks_count,
115 rfs->new_fs->super->s_free_blocks_count,
119 retval = block_mover(rfs);
123 retval = inode_scan_and_fix(rfs);
127 retval = inode_ref_fix(rfs);
131 retval = move_itables(rfs);
135 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
139 retval = fix_resize_inode(rfs->new_fs);
143 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
144 retval = ext2fs_close(rfs->new_fs);
150 ext2fs_free(rfs->old_fs);
152 ext2fs_free_mem(&rfs->itable_buf);
153 ext2fs_free_mem(&rfs);
159 ext2fs_free(rfs->new_fs);
161 ext2fs_free_mem(&rfs->itable_buf);
162 ext2fs_free_mem(&rfs);
166 /* --------------------------------------------------------------------
168 * Resize processing, phase 1.
170 * In this phase we adjust the in-memory superblock information, and
171 * initialize any new parts of the inode table. The new parts of the
172 * inode table are created in virgin disk space, so we can abort here
173 * without any side effects.
174 * --------------------------------------------------------------------
178 * This routine is shared by the online and offline resize routines.
179 * All of the information which is adjusted in memory is done here.
181 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs, blk_t new_size)
186 blk_t blk, group_block;
188 int adj, old_numblocks, numblocks, adjblocks;
189 unsigned long i, j, old_desc_blocks, max_group;
190 unsigned int meta_bg, meta_bg_size;
192 unsigned long long new_inodes; /* u64 to check for overflow */
194 fs->super->s_blocks_count = new_size;
197 fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
198 fs->super->s_first_data_block,
199 EXT2_BLOCKS_PER_GROUP(fs->super));
200 if (fs->group_desc_count == 0)
201 return EXT2_ET_TOOSMALL;
202 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
203 EXT2_DESC_PER_BLOCK(fs->super));
206 * Overhead is the number of bookkeeping blocks per group. It
207 * includes the superblock backup, the group descriptor
208 * backups, the inode bitmap, the block bitmap, and the inode
211 overhead = (int) (2 + fs->inode_blocks_per_group);
213 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
214 overhead += 1 + fs->desc_blocks +
215 fs->super->s_reserved_gdt_blocks;
218 * See if the last group is big enough to support the
219 * necessary data structures. If not, we need to get rid of
222 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
223 fs->super->s_blocks_per_group;
224 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
225 return EXT2_ET_TOOSMALL;
226 if (rem && (rem < overhead+50)) {
227 fs->super->s_blocks_count -= rem;
231 * Adjust the number of inodes
233 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
234 if (new_inodes > ~0U) {
235 fprintf(stderr, _("inodes (%llu) must be less than %u"),
237 return EXT2_ET_TOO_MANY_INODES;
239 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
240 fs->group_desc_count;
243 * Adjust the number of free blocks
245 blk = old_fs->super->s_blocks_count;
246 if (blk > fs->super->s_blocks_count)
247 fs->super->s_free_blocks_count -=
248 (blk - fs->super->s_blocks_count);
250 fs->super->s_free_blocks_count +=
251 (fs->super->s_blocks_count - blk);
254 * Adjust the number of reserved blocks
256 blk = (__u64)old_fs->super->s_r_blocks_count * 100 /
257 old_fs->super->s_blocks_count;
258 fs->super->s_r_blocks_count = e2p_percent(blk,
259 fs->super->s_blocks_count);
262 * Adjust the bitmaps for size
264 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
265 fs->super->s_inodes_count,
267 if (retval) goto errout;
269 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
270 * fs->group_desc_count)) - 1 +
271 fs->super->s_first_data_block;
272 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
273 real_end, fs->block_map);
275 if (retval) goto errout;
278 * Reallocate the group descriptors as necessary.
280 if (old_fs->desc_blocks != fs->desc_blocks) {
281 retval = ext2fs_resize_mem(old_fs->desc_blocks *
283 fs->desc_blocks * fs->blocksize,
287 if (fs->desc_blocks > old_fs->desc_blocks)
288 memset((char *) fs->group_desc +
289 (old_fs->desc_blocks * fs->blocksize), 0,
290 (fs->desc_blocks - old_fs->desc_blocks) *
295 * If the resize_inode feature is set, and we are changing the
296 * number of descriptor blocks, then adjust
297 * s_reserved_gdt_blocks if possible to avoid needing to move
298 * the inode table either now or in the future.
300 if ((fs->super->s_feature_compat &
301 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
302 (old_fs->desc_blocks != fs->desc_blocks)) {
305 new = ((int) fs->super->s_reserved_gdt_blocks) +
306 (old_fs->desc_blocks - fs->desc_blocks);
309 if (new > (int) fs->blocksize/4)
310 new = fs->blocksize/4;
311 fs->super->s_reserved_gdt_blocks = new;
313 fs->super->s_feature_compat &=
314 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
318 * If we are shrinking the number block groups, we're done and
321 if (old_fs->group_desc_count > fs->group_desc_count) {
327 * Fix the count of the last (old) block group
329 old_numblocks = (old_fs->super->s_blocks_count -
330 old_fs->super->s_first_data_block) %
331 old_fs->super->s_blocks_per_group;
333 old_numblocks = old_fs->super->s_blocks_per_group;
334 if (old_fs->group_desc_count == fs->group_desc_count) {
335 numblocks = (fs->super->s_blocks_count -
336 fs->super->s_first_data_block) %
337 fs->super->s_blocks_per_group;
339 numblocks = fs->super->s_blocks_per_group;
341 numblocks = fs->super->s_blocks_per_group;
342 i = old_fs->group_desc_count - 1;
343 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
344 ext2fs_group_desc_csum_set(fs, i);
347 * If the number of block groups is staying the same, we're
348 * done and can exit now. (If the number block groups is
349 * shrinking, we had exited earlier.)
351 if (old_fs->group_desc_count >= fs->group_desc_count) {
357 * Initialize the new block group descriptors
359 group_block = fs->super->s_first_data_block +
360 old_fs->group_desc_count * fs->super->s_blocks_per_group;
362 adj = old_fs->group_desc_count;
363 max_group = fs->group_desc_count - adj;
364 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
365 old_desc_blocks = fs->super->s_first_meta_bg;
367 old_desc_blocks = fs->desc_blocks +
368 fs->super->s_reserved_gdt_blocks;
369 for (i = old_fs->group_desc_count;
370 i < fs->group_desc_count; i++) {
371 memset(&fs->group_desc[i], 0,
372 sizeof(struct ext2_group_desc));
375 if (i == fs->group_desc_count-1) {
376 numblocks = (fs->super->s_blocks_count -
377 fs->super->s_first_data_block) %
378 fs->super->s_blocks_per_group;
380 numblocks = fs->super->s_blocks_per_group;
382 numblocks = fs->super->s_blocks_per_group;
384 has_super = ext2fs_bg_has_super(fs, i);
386 ext2fs_mark_block_bitmap(fs->block_map, group_block);
389 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
390 meta_bg = i / meta_bg_size;
391 if (!(fs->super->s_feature_incompat &
392 EXT2_FEATURE_INCOMPAT_META_BG) ||
393 (meta_bg < fs->super->s_first_meta_bg)) {
395 for (j=0; j < old_desc_blocks; j++)
396 ext2fs_mark_block_bitmap(fs->block_map,
397 group_block + 1 + j);
398 adjblocks += old_desc_blocks;
403 if (((i % meta_bg_size) == 0) ||
404 ((i % meta_bg_size) == 1) ||
405 ((i % meta_bg_size) == (meta_bg_size-1)))
406 ext2fs_mark_block_bitmap(fs->block_map,
407 group_block + has_super);
410 adjblocks += 2 + fs->inode_blocks_per_group;
412 numblocks -= adjblocks;
413 fs->super->s_free_blocks_count -= adjblocks;
414 fs->super->s_free_inodes_count +=
415 fs->super->s_inodes_per_group;
416 fs->group_desc[i].bg_free_blocks_count = numblocks;
417 fs->group_desc[i].bg_free_inodes_count =
418 fs->super->s_inodes_per_group;
419 fs->group_desc[i].bg_used_dirs_count = 0;
420 ext2fs_group_desc_csum_set(fs, i);
422 retval = ext2fs_allocate_group_table(fs, i, 0);
423 if (retval) goto errout;
425 group_block += fs->super->s_blocks_per_group;
434 * This routine adjusts the superblock and other data structures, both
435 * in disk as well as in memory...
437 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
444 unsigned long max_group;
447 ext2fs_mark_super_dirty(fs);
448 ext2fs_mark_bb_dirty(fs);
449 ext2fs_mark_ib_dirty(fs);
451 retval = adjust_fs_info(fs, rfs->old_fs, new_size);
456 * Check to make sure there are enough inodes
458 if ((rfs->old_fs->super->s_inodes_count -
459 rfs->old_fs->super->s_free_inodes_count) >
460 rfs->new_fs->super->s_inodes_count) {
466 * If we are shrinking the number block groups, we're done and
469 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
475 * If the number of block groups is staying the same, we're
476 * done and can exit now. (If the number block groups is
477 * shrinking, we had exited earlier.)
479 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
485 * Initialize the new block group descriptors
487 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
492 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
493 group_block = fs->super->s_first_data_block +
494 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
496 adj = rfs->old_fs->group_desc_count;
497 max_group = fs->group_desc_count - adj;
499 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
504 for (i = rfs->old_fs->group_desc_count;
505 i < fs->group_desc_count; i++) {
507 * Write out the new inode table
509 retval = io_channel_write_blk(fs->io,
510 fs->group_desc[i].bg_inode_table,
511 fs->inode_blocks_per_group,
513 if (retval) goto errout;
515 io_channel_flush(fs->io);
517 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
518 i - adj + 1, max_group);
522 group_block += fs->super->s_blocks_per_group;
524 io_channel_flush(fs->io);
531 /* --------------------------------------------------------------------
533 * Resize processing, phase 2.
535 * In this phase we adjust determine which blocks need to be moved, in
536 * blocks_to_move(). We then copy the blocks to their ultimate new
537 * destinations using block_mover(). Since we are copying blocks to
538 * their new locations, again during this pass we can abort without
540 * --------------------------------------------------------------------
544 * This helper function creates a block bitmap with all of the
545 * filesystem meta-data blocks.
547 static errcode_t mark_table_blocks(ext2_filsys fs,
548 ext2fs_block_bitmap bmap)
553 unsigned long meta_bg_size;
554 unsigned int old_desc_blocks;
556 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
557 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
558 old_desc_blocks = fs->super->s_first_meta_bg;
560 old_desc_blocks = fs->desc_blocks +
561 fs->super->s_reserved_gdt_blocks;
562 for (i = 0; i < fs->group_desc_count; i++) {
563 ext2fs_reserve_super_and_bgd(fs, i, bmap);
566 * Mark the blocks used for the inode table
568 for (j = 0, b = fs->group_desc[i].bg_inode_table;
569 j < (unsigned int) fs->inode_blocks_per_group;
571 ext2fs_mark_block_bitmap(bmap, b);
574 * Mark block used for the block bitmap
576 ext2fs_mark_block_bitmap(bmap,
577 fs->group_desc[i].bg_block_bitmap);
580 * Mark block used for the inode bitmap
582 ext2fs_mark_block_bitmap(bmap,
583 fs->group_desc[i].bg_inode_bitmap);
589 * This function checks to see if a particular block (either a
590 * superblock or a block group descriptor) overlaps with an inode or
591 * block bitmap block, or with the inode table.
593 static void mark_fs_metablock(ext2_resize_t rfs,
594 ext2fs_block_bitmap meta_bmap,
595 int group, blk_t blk)
597 ext2_filsys fs = rfs->new_fs;
599 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
600 ext2fs_mark_block_bitmap(fs->block_map, blk);
603 * Check to see if we overlap with the inode or block bitmap,
604 * or the inode tables. If not, and the block is in use, then
605 * mark it as a block to be moved.
607 if (IS_BLOCK_BM(fs, group, blk)) {
608 FS_BLOCK_BM(fs, group) = 0;
609 rfs->needed_blocks++;
610 } else if (IS_INODE_BM(fs, group, blk)) {
611 FS_INODE_BM(fs, group) = 0;
612 rfs->needed_blocks++;
613 } else if (IS_INODE_TB(fs, group, blk)) {
614 FS_INODE_TB(fs, group) = 0;
615 rfs->needed_blocks++;
616 } else if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk) &&
617 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
618 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
619 rfs->needed_blocks++;
625 * This routine marks and unmarks reserved blocks in the new block
626 * bitmap. It also determines which blocks need to be moved and
627 * places this information into the move_blocks bitmap.
629 static errcode_t blocks_to_move(ext2_resize_t rfs)
632 dgrp_t i, max_groups;
633 blk_t blk, group_blk;
634 unsigned long old_blocks, new_blocks;
635 unsigned int meta_bg, meta_bg_size;
637 ext2_filsys fs, old_fs;
638 ext2fs_block_bitmap meta_bmap;
641 old_fs = rfs->old_fs;
642 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
645 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
646 &rfs->reserve_blocks);
650 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
655 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
660 retval = mark_table_blocks(old_fs, meta_bmap);
667 * If we're shrinking the filesystem, we need to move all of
668 * the blocks that don't fit any more
670 for (blk = fs->super->s_blocks_count;
671 blk < old_fs->super->s_blocks_count; blk++) {
672 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
673 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
674 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
675 rfs->needed_blocks++;
677 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
680 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
681 old_blocks = old_fs->super->s_first_meta_bg;
682 new_blocks = fs->super->s_first_meta_bg;
684 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
685 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
688 if (old_blocks == new_blocks) {
693 max_groups = fs->group_desc_count;
694 if (max_groups > old_fs->group_desc_count)
695 max_groups = old_fs->group_desc_count;
696 group_blk = old_fs->super->s_first_data_block;
698 * If we're reducing the number of descriptor blocks, this
699 * makes life easy. :-) We just have to mark some extra
702 if (old_blocks > new_blocks) {
703 for (i = 0; i < max_groups; i++) {
704 if (!ext2fs_bg_has_super(fs, i)) {
705 group_blk += fs->super->s_blocks_per_group;
708 for (blk = group_blk+1+new_blocks;
709 blk < group_blk+1+old_blocks; blk++) {
710 ext2fs_unmark_block_bitmap(fs->block_map,
712 rfs->needed_blocks--;
714 group_blk += fs->super->s_blocks_per_group;
720 * If we're increasing the number of descriptor blocks, life
721 * gets interesting....
723 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
724 for (i = 0; i < max_groups; i++) {
725 has_super = ext2fs_bg_has_super(fs, i);
727 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
729 meta_bg = i / meta_bg_size;
730 if (!(fs->super->s_feature_incompat &
731 EXT2_FEATURE_INCOMPAT_META_BG) ||
732 (meta_bg < fs->super->s_first_meta_bg)) {
734 for (blk = group_blk+1;
735 blk < group_blk + 1 + new_blocks; blk++)
736 mark_fs_metablock(rfs, meta_bmap,
742 if (((i % meta_bg_size) == 0) ||
743 ((i % meta_bg_size) == 1) ||
744 ((i % meta_bg_size) == (meta_bg_size-1)))
745 mark_fs_metablock(rfs, meta_bmap, i,
746 group_blk + has_super);
749 if (fs->group_desc[i].bg_inode_table &&
750 fs->group_desc[i].bg_inode_bitmap &&
751 fs->group_desc[i].bg_block_bitmap)
755 * Reserve the existing meta blocks that we know
756 * aren't to be moved.
758 if (fs->group_desc[i].bg_block_bitmap)
759 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
760 fs->group_desc[i].bg_block_bitmap);
761 if (fs->group_desc[i].bg_inode_bitmap)
762 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
763 fs->group_desc[i].bg_inode_bitmap);
764 if (fs->group_desc[i].bg_inode_table)
765 for (blk = fs->group_desc[i].bg_inode_table, j=0;
766 j < fs->inode_blocks_per_group ; j++, blk++)
767 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
771 * Allocate the missing data structures
773 retval = ext2fs_allocate_group_table(fs, i,
774 rfs->reserve_blocks);
779 * For those structures that have changed, we need to
782 if (FS_BLOCK_BM(old_fs, i) !=
783 (blk = FS_BLOCK_BM(fs, i))) {
784 ext2fs_mark_block_bitmap(fs->block_map, blk);
785 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
786 !ext2fs_test_block_bitmap(meta_bmap, blk))
787 ext2fs_mark_block_bitmap(rfs->move_blocks,
790 if (FS_INODE_BM(old_fs, i) !=
791 (blk = FS_INODE_BM(fs, i))) {
792 ext2fs_mark_block_bitmap(fs->block_map, blk);
793 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
794 !ext2fs_test_block_bitmap(meta_bmap, blk))
795 ext2fs_mark_block_bitmap(rfs->move_blocks,
800 * The inode table, if we need to relocate it, is
801 * handled specially. We have to reserve the blocks
802 * for both the old and the new inode table, since we
803 * can't have the inode table be destroyed during the
804 * block relocation phase.
806 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
807 goto next_group; /* inode table not moved */
809 rfs->needed_blocks += fs->inode_blocks_per_group;
812 * Mark the new inode table as in use in the new block
813 * allocation bitmap, and move any blocks that might
816 for (blk = fs->group_desc[i].bg_inode_table, j=0;
817 j < fs->inode_blocks_per_group ; j++, blk++) {
818 ext2fs_mark_block_bitmap(fs->block_map, blk);
819 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
820 !ext2fs_test_block_bitmap(meta_bmap, blk))
821 ext2fs_mark_block_bitmap(rfs->move_blocks,
826 * Make sure the old inode table is reserved in the
827 * block reservation bitmap.
829 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
830 j < fs->inode_blocks_per_group ; j++, blk++)
831 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
834 group_blk += rfs->new_fs->super->s_blocks_per_group;
840 ext2fs_free_block_bitmap(meta_bmap);
846 * This helper function tries to allocate a new block. We try to
847 * avoid hitting the original group descriptor blocks at least at
848 * first, since we want to make it possible to recover from a badly
849 * aborted resize operation as much as possible.
851 * In the future, I may further modify this routine to balance out
852 * where we get the new blocks across the various block groups.
853 * Ideally we would allocate blocks that corresponded with the block
854 * group of the containing inode, and keep contiguous blocks
855 * together. However, this very difficult to do efficiently, since we
856 * don't have the necessary information up front.
860 #define DESPERATION 2
862 static void init_block_alloc(ext2_resize_t rfs)
864 rfs->alloc_state = AVOID_OLD;
865 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
867 /* HACK for testing */
868 if (rfs->new_fs->super->s_blocks_count >
869 rfs->old_fs->super->s_blocks_count)
870 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
874 static blk_t get_new_block(ext2_resize_t rfs)
876 ext2_filsys fs = rfs->new_fs;
879 if (rfs->new_blk >= fs->super->s_blocks_count) {
880 if (rfs->alloc_state == DESPERATION)
883 #ifdef RESIZE2FS_DEBUG
884 if (rfs->flags & RESIZE_DEBUG_BMOVE)
885 printf("Going into desperation mode "
886 "for block allocations\n");
888 rfs->alloc_state = DESPERATION;
889 rfs->new_blk = fs->super->s_first_data_block;
892 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
893 ext2fs_test_block_bitmap(rfs->reserve_blocks,
895 ((rfs->alloc_state == AVOID_OLD) &&
896 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
897 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
906 static errcode_t block_mover(ext2_resize_t rfs)
908 blk_t blk, old_blk, new_blk;
909 ext2_filsys fs = rfs->new_fs;
910 ext2_filsys old_fs = rfs->old_fs;
914 ext2_badblocks_list badblock_list = 0;
917 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
921 new_blk = fs->super->s_first_data_block;
922 if (!rfs->itable_buf) {
923 retval = ext2fs_get_array(fs->blocksize,
924 fs->inode_blocks_per_group,
929 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
934 * The first step is to figure out where all of the blocks
938 init_block_alloc(rfs);
939 for (blk = old_fs->super->s_first_data_block;
940 blk < old_fs->super->s_blocks_count; blk++) {
941 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
943 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
945 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
946 ext2fs_badblocks_list_del(badblock_list, blk);
951 new_blk = get_new_block(rfs);
956 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
957 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
963 ext2fs_free_extent_table(rfs->bmap);
971 * Step two is to actually move the blocks
973 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
974 if (retval) goto errout;
977 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
983 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
984 if (retval) goto errout;
987 #ifdef RESIZE2FS_DEBUG
988 if (rfs->flags & RESIZE_DEBUG_BMOVE)
989 printf("Moving %d blocks %u->%u\n",
990 size, old_blk, new_blk);
994 if (c > fs->inode_blocks_per_group)
995 c = fs->inode_blocks_per_group;
996 retval = io_channel_read_blk(fs->io, old_blk, c,
998 if (retval) goto errout;
999 retval = io_channel_write_blk(fs->io, new_blk, c,
1001 if (retval) goto errout;
1006 if (rfs->progress) {
1007 io_channel_flush(fs->io);
1008 retval = (rfs->progress)(rfs,
1009 E2_RSZ_BLOCK_RELOC_PASS,
1015 io_channel_flush(fs->io);
1019 if (badblock_list) {
1020 if (!retval && bb_modified)
1021 retval = ext2fs_update_bb_inode(old_fs,
1023 ext2fs_badblocks_list_free(badblock_list);
1029 /* --------------------------------------------------------------------
1031 * Resize processing, phase 3
1033 * --------------------------------------------------------------------
1037 struct process_block_struct {
1040 struct ext2_inode * inode;
1046 static int process_block(ext2_filsys fs, blk_t *block_nr,
1047 e2_blkcnt_t blockcnt,
1048 blk_t ref_block EXT2FS_ATTR((unused)),
1049 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1051 struct process_block_struct *pb;
1053 blk_t block, new_block;
1056 pb = (struct process_block_struct *) priv_data;
1058 if (pb->rfs->bmap) {
1059 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1061 *block_nr = new_block;
1062 ret |= BLOCK_CHANGED;
1064 #ifdef RESIZE2FS_DEBUG
1065 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1066 printf("ino=%u, blockcnt=%lld, %u->%u\n",
1067 pb->ino, blockcnt, block, new_block);
1073 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
1074 block, (int) blockcnt);
1086 static errcode_t progress_callback(ext2_filsys fs,
1087 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1088 dgrp_t group, void * priv_data)
1090 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1094 * This check is to protect against old ext2 libraries. It
1095 * shouldn't be needed against new libraries.
1100 if (rfs->progress) {
1101 io_channel_flush(fs->io);
1102 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1103 group+1, fs->group_desc_count);
1111 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1113 struct process_block_struct pb;
1114 ext2_ino_t ino, new_inode;
1115 struct ext2_inode *inode = NULL;
1116 ext2_inode_scan scan = NULL;
1119 char *block_buf = 0;
1120 ext2_ino_t start_to_move;
1121 blk_t orig_size, new_block;
1124 if ((rfs->old_fs->group_desc_count <=
1125 rfs->new_fs->group_desc_count) &&
1130 * Save the original size of the old filesystem, and
1131 * temporarily set the size to be the new size if the new size
1132 * is larger. We need to do this to avoid catching an error
1133 * by the block iterator routines
1135 orig_size = rfs->old_fs->super->s_blocks_count;
1136 if (orig_size < rfs->new_fs->super->s_blocks_count)
1137 rfs->old_fs->super->s_blocks_count =
1138 rfs->new_fs->super->s_blocks_count;
1140 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1141 if (retval) goto errout;
1143 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1144 if (retval) goto errout;
1145 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1146 if (retval) goto errout;
1148 start_to_move = (rfs->new_fs->group_desc_count *
1149 rfs->new_fs->super->s_inodes_per_group);
1151 if (rfs->progress) {
1152 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1153 0, rfs->old_fs->group_desc_count);
1157 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1161 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1162 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1163 inode = malloc(inode_size);
1169 * First, copy all of the inodes that need to be moved
1170 * elsewhere in the inode table
1173 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1174 if (retval) goto errout;
1178 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1179 continue; /* inode not in use */
1181 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1184 if (inode->i_file_acl && rfs->bmap) {
1185 new_block = ext2fs_extent_translate(rfs->bmap,
1188 inode->i_file_acl = new_block;
1189 retval = ext2fs_write_inode_full(rfs->old_fs,
1190 ino, inode, inode_size);
1191 if (retval) goto errout;
1195 if (ext2fs_inode_has_valid_blocks(inode) &&
1196 (rfs->bmap || pb.is_dir)) {
1198 retval = ext2fs_block_iterate2(rfs->old_fs,
1200 process_block, &pb);
1209 if (ino <= start_to_move)
1210 continue; /* Don't need to move it. */
1216 if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
1220 if (new_inode > rfs->new_fs->super->s_inodes_count) {
1225 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1227 /* Get the new version of the inode */
1228 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1230 if (retval) goto errout;
1232 inode->i_ctime = time(0);
1233 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1235 if (retval) goto errout;
1237 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1238 if (LINUX_S_ISDIR(inode->i_mode)) {
1239 rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1240 ext2fs_group_desc_csum_set(rfs->new_fs, group);
1243 #ifdef RESIZE2FS_DEBUG
1244 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1245 printf("Inode moved %u->%u\n", ino, new_inode);
1248 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1252 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1254 io_channel_flush(rfs->old_fs->io);
1257 rfs->old_fs->super->s_blocks_count = orig_size;
1259 ext2fs_free_extent_table(rfs->bmap);
1263 ext2fs_close_inode_scan(scan);
1265 ext2fs_free_mem(&block_buf);
1271 /* --------------------------------------------------------------------
1273 * Resize processing, phase 4.
1275 * --------------------------------------------------------------------
1281 unsigned long max_dirs;
1285 static int check_and_change_inodes(ext2_ino_t dir,
1286 int entry EXT2FS_ATTR((unused)),
1287 struct ext2_dir_entry *dirent, int offset,
1288 int blocksize EXT2FS_ATTR((unused)),
1289 char *buf EXT2FS_ATTR((unused)),
1292 struct istruct *is = (struct istruct *) priv_data;
1293 struct ext2_inode inode;
1294 ext2_ino_t new_inode;
1297 if (is->rfs->progress && offset == 0) {
1298 io_channel_flush(is->rfs->old_fs->io);
1299 is->err = (is->rfs->progress)(is->rfs,
1300 E2_RSZ_INODE_REF_UPD_PASS,
1301 ++is->num, is->max_dirs);
1303 return DIRENT_ABORT;
1309 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1313 #ifdef RESIZE2FS_DEBUG
1314 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1315 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1316 dir, dirent->name_len&0xFF, dirent->name,
1317 dirent->inode, new_inode);
1320 dirent->inode = new_inode;
1322 /* Update the directory mtime and ctime */
1323 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1325 inode.i_mtime = inode.i_ctime = time(0);
1326 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1328 return DIRENT_ABORT;
1331 return DIRENT_CHANGED;
1334 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1343 * Now, we iterate over all of the directories to update the
1347 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1351 if (rfs->progress) {
1352 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1358 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1359 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1360 check_and_change_inodes, &is);
1369 ext2fs_free_extent_table(rfs->imap);
1375 /* --------------------------------------------------------------------
1377 * Resize processing, phase 5.
1379 * In this phase we actually move the inode table around, and then
1380 * update the summary statistics. This is scary, since aborting here
1381 * will potentially scramble the filesystem. (We are moving the
1382 * inode tables around in place, and so the potential for lost data,
1383 * or at the very least scrambling the mapping between filenames and
1384 * inode numbers is very high in case of a power failure here.)
1385 * --------------------------------------------------------------------
1390 * A very scary routine --- this one moves the inode table around!!!
1392 * After this you have to use the rfs->new_fs file handle to read and
1395 static errcode_t move_itables(ext2_resize_t rfs)
1397 int n, num, size, diff;
1398 dgrp_t i, max_groups;
1399 ext2_filsys fs = rfs->new_fs;
1401 blk_t old_blk, new_blk, blk;
1403 int j, to_move, moved;
1405 max_groups = fs->group_desc_count;
1406 if (max_groups > rfs->old_fs->group_desc_count)
1407 max_groups = rfs->old_fs->group_desc_count;
1409 size = fs->blocksize * fs->inode_blocks_per_group;
1410 if (!rfs->itable_buf) {
1411 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1417 * Figure out how many inode tables we need to move
1419 to_move = moved = 0;
1420 for (i=0; i < max_groups; i++)
1421 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1422 fs->group_desc[i].bg_inode_table)
1428 if (rfs->progress) {
1429 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1435 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1437 for (i=0; i < max_groups; i++) {
1438 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1439 new_blk = fs->group_desc[i].bg_inode_table;
1440 diff = new_blk - old_blk;
1442 #ifdef RESIZE2FS_DEBUG
1443 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1444 printf("Itable move group %d block %u->%u (diff %d)\n",
1445 i, old_blk, new_blk, diff);
1451 retval = io_channel_read_blk(fs->io, old_blk,
1452 fs->inode_blocks_per_group,
1457 * The end of the inode table segment often contains
1458 * all zeros, and we're often only moving the inode
1459 * table down a block or two. If so, we can optimize
1460 * things by not rewriting blocks that we know to be zero
1463 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1466 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1467 #ifdef RESIZE2FS_DEBUG
1468 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1469 printf("%d blocks of zeros...\n", n);
1471 num = fs->inode_blocks_per_group;
1475 retval = io_channel_write_blk(fs->io, new_blk,
1476 num, rfs->itable_buf);
1478 io_channel_write_blk(fs->io, old_blk,
1479 num, rfs->itable_buf);
1483 retval = io_channel_write_blk(fs->io,
1484 old_blk + fs->inode_blocks_per_group,
1485 diff, (rfs->itable_buf +
1486 (fs->inode_blocks_per_group - diff) *
1492 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1493 j < fs->inode_blocks_per_group ; j++, blk++)
1494 ext2fs_unmark_block_bitmap(fs->block_map, blk);
1496 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1497 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1498 ext2fs_mark_super_dirty(rfs->old_fs);
1499 ext2fs_flush(rfs->old_fs);
1501 if (rfs->progress) {
1502 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1508 mark_table_blocks(fs, fs->block_map);
1510 #ifdef RESIZE2FS_DEBUG
1511 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1512 printf("Inode table move finished.\n");
1521 * Fix the resize inode
1523 static errcode_t fix_resize_inode(ext2_filsys fs)
1525 struct ext2_inode inode;
1529 if (!(fs->super->s_feature_compat &
1530 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1533 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1534 if (retval) goto errout;
1536 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1537 if (retval) goto errout;
1539 ext2fs_iblk_set(fs, &inode, 1);
1541 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1542 if (retval) goto errout;
1544 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1546 * Avoid zeroing out block #0; that's rude. This
1547 * should never happen anyway since the filesystem
1548 * should be fsck'ed and we assume it is consistent.
1551 _("Should never happen: resize inode corrupt!\n"));
1555 memset(block_buf, 0, fs->blocksize);
1557 retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1559 if (retval) goto errout;
1561 retval = ext2fs_create_resize_inode(fs);
1567 ext2fs_free_mem(&block_buf);
1572 * Finally, recalculate the summary information
1574 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1578 unsigned int group = 0;
1579 unsigned int count = 0;
1584 * First calculate the block statistics
1586 for (blk = fs->super->s_first_data_block;
1587 blk < fs->super->s_blocks_count; blk++) {
1588 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1593 if ((count == fs->super->s_blocks_per_group) ||
1594 (blk == fs->super->s_blocks_count-1)) {
1595 fs->group_desc[group].bg_free_blocks_count =
1597 ext2fs_group_desc_csum_set(fs, group);
1603 fs->super->s_free_blocks_count = total_free;
1606 * Next, calculate the inode statistics
1613 /* Protect loop from wrap-around if s_inodes_count maxed */
1614 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1615 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1620 if ((count == fs->super->s_inodes_per_group) ||
1621 (ino == fs->super->s_inodes_count)) {
1622 fs->group_desc[group].bg_free_inodes_count =
1624 ext2fs_group_desc_csum_set(fs, group);
1630 fs->super->s_free_inodes_count = total_free;
1631 ext2fs_mark_super_dirty(fs);
1636 * calcluate the minimum number of blocks the given fs can be resized to
1638 blk_t calculate_minimum_resize_size(ext2_filsys fs)
1640 blk_t inode_count, blks_needed, groups, data_blocks;
1641 blk_t grp, data_needed, last_start;
1642 int overhead = 0, num_of_superblocks = 0;
1645 * first figure out how many group descriptors we need to
1646 * handle the number of inodes we have
1648 inode_count = fs->super->s_inodes_count -
1649 fs->super->s_free_inodes_count;
1650 blks_needed = ext2fs_div_ceil(inode_count,
1651 fs->super->s_inodes_per_group) *
1652 EXT2_BLOCKS_PER_GROUP(fs->super);
1653 groups = ext2fs_div_ceil(blks_needed,
1654 EXT2_BLOCKS_PER_GROUP(fs->super));
1657 * we need to figure out how many backup superblocks we have so we can
1658 * account for that in the metadata
1660 for (grp = 0; grp < fs->group_desc_count; grp++) {
1661 if (ext2fs_bg_has_super(fs, grp))
1662 num_of_superblocks++;
1665 /* calculate how many blocks are needed for data */
1666 data_needed = fs->super->s_blocks_count -
1667 fs->super->s_free_blocks_count;
1668 data_needed -= SUPER_OVERHEAD(fs) * num_of_superblocks;
1669 data_needed -= META_OVERHEAD(fs) * fs->group_desc_count;
1672 * figure out how many data blocks we have given the number of groups
1673 * we need for our inodes
1675 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
1677 for (grp = 0; grp < groups; grp++) {
1678 overhead = META_OVERHEAD(fs);
1680 if (ext2fs_bg_has_super(fs, grp))
1681 overhead += SUPER_OVERHEAD(fs);
1684 * we want to keep track of how much data we can store in
1685 * the groups leading up to the last group so we can determine
1686 * how big the last group needs to be
1688 if (grp != (groups - 1))
1689 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
1692 data_blocks -= overhead;
1696 * if we need more group descriptors in order to accomodate our data
1697 * then we need to add them here
1699 while (data_needed > data_blocks) {
1700 blk_t remainder = data_needed - data_blocks;
1703 /* figure out how many more groups we need for the data */
1704 extra_grps = ext2fs_div_ceil(remainder,
1705 EXT2_BLOCKS_PER_GROUP(fs->super));
1707 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
1709 /* ok we have to account for the last group */
1710 overhead = META_OVERHEAD(fs);
1711 if (ext2fs_bg_has_super(fs, groups-1))
1712 overhead += SUPER_OVERHEAD(fs);
1713 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
1715 for (grp = groups; grp < groups+extra_grps; grp++) {
1716 overhead = META_OVERHEAD(fs);
1717 if (ext2fs_bg_has_super(fs, grp))
1718 overhead += SUPER_OVERHEAD(fs);
1721 * again, we need to see how much data we cram into
1722 * all of the groups leading up to the last group
1724 if (grp != (groups + extra_grps - 1))
1725 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
1728 data_blocks -= overhead;
1731 groups += extra_grps;
1734 /* now for the fun voodoo */
1735 overhead = META_OVERHEAD(fs);
1738 * if this is the case then the last group is going to have data in it
1739 * so we need to adjust the size of the last group accordingly
1741 if (last_start < data_needed) {
1742 blk_t remainder = data_needed - last_start;
1745 * 50 is a magic number that mkfs/resize uses to see if its
1746 * even worth making/resizing the fs. basically you need to
1747 * have at least 50 blocks in addition to the blocks needed
1748 * for the metadata in the last group
1751 overhead += remainder;
1757 if (ext2fs_bg_has_super(fs, groups-1))
1758 overhead += SUPER_OVERHEAD(fs);
1761 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
1762 * only do groups-1, and then add the number of blocks needed to
1763 * handle the group descriptor metadata+data that we need
1765 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
1766 blks_needed += overhead;