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.
37 #include "resize2fs.h"
40 #ifdef __linux__ /* Kludge for debugging */
41 #define RESIZE2FS_DEBUG
44 static void fix_uninit_block_bitmaps(ext2_filsys fs);
45 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size);
46 static errcode_t blocks_to_move(ext2_resize_t rfs);
47 static errcode_t block_mover(ext2_resize_t rfs);
48 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
49 static errcode_t inode_ref_fix(ext2_resize_t rfs);
50 static errcode_t move_itables(ext2_resize_t rfs);
51 static errcode_t fix_resize_inode(ext2_filsys fs);
52 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
53 static errcode_t fix_sb_journal_backup(ext2_filsys fs);
54 static errcode_t mark_table_blocks(ext2_filsys fs,
55 ext2fs_block_bitmap bmap);
56 static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs);
57 static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
58 ext2fs_block_bitmap meta_bmap);
61 * Some helper CPP macros
63 #define IS_BLOCK_BM(fs, i, blk) ((blk) == ext2fs_block_bitmap_loc((fs),(i)))
64 #define IS_INODE_BM(fs, i, blk) ((blk) == ext2fs_inode_bitmap_loc((fs),(i)))
66 #define IS_INODE_TB(fs, i, blk) (((blk) >= ext2fs_inode_table_loc((fs), (i))) && \
67 ((blk) < (ext2fs_inode_table_loc((fs), (i)) + \
68 (fs)->inode_blocks_per_group)))
70 /* Some bigalloc helper macros which are more succint... */
71 #define B2C(x) EXT2FS_B2C(fs, (x))
72 #define C2B(x) EXT2FS_C2B(fs, (x))
73 #define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
74 #define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
75 #define LT_CLSTR(x, y) (B2C(x) < B2C(y))
76 #define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
77 #define GT_CLSTR(x, y) (B2C(x) > B2C(y))
79 static int lazy_itable_init;
82 * This is the top-level routine which does the dirty deed....
84 errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
85 errcode_t (*progress)(ext2_resize_t rfs, int pass,
87 unsigned long max_val))
91 struct resource_track rtrack, overall_track;
94 * Create the data structure
96 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
100 memset(rfs, 0, sizeof(struct ext2_resize_struct));
105 rfs->progress = progress;
107 init_resource_track(&overall_track, "overall resize2fs", fs->io);
108 init_resource_track(&rtrack, "read_bitmaps", fs->io);
109 retval = ext2fs_read_bitmaps(fs);
112 print_resource_track(rfs, &rtrack, fs->io);
114 fs->super->s_state |= EXT2_ERROR_FS;
115 ext2fs_mark_super_dirty(fs);
118 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io);
119 fix_uninit_block_bitmaps(fs);
120 print_resource_track(rfs, &rtrack, fs->io);
121 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
125 init_resource_track(&rtrack, "adjust_superblock", fs->io);
126 retval = adjust_superblock(rfs, *new_size);
129 print_resource_track(rfs, &rtrack, fs->io);
132 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io);
133 fix_uninit_block_bitmaps(rfs->new_fs);
134 print_resource_track(rfs, &rtrack, fs->io);
135 /* Clear the block bitmap uninit flag for the last block group */
136 ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
137 EXT2_BG_BLOCK_UNINIT);
139 *new_size = ext2fs_blocks_count(rfs->new_fs->super);
141 init_resource_track(&rtrack, "blocks_to_move", fs->io);
142 retval = blocks_to_move(rfs);
145 print_resource_track(rfs, &rtrack, fs->io);
147 #ifdef RESIZE2FS_DEBUG
148 if (rfs->flags & RESIZE_DEBUG_BMOVE)
149 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
150 ext2fs_free_blocks_count(rfs->old_fs->super),
151 ext2fs_free_blocks_count(rfs->new_fs->super),
155 init_resource_track(&rtrack, "block_mover", fs->io);
156 retval = block_mover(rfs);
159 print_resource_track(rfs, &rtrack, fs->io);
161 init_resource_track(&rtrack, "inode_scan_and_fix", fs->io);
162 retval = inode_scan_and_fix(rfs);
165 print_resource_track(rfs, &rtrack, fs->io);
167 init_resource_track(&rtrack, "inode_ref_fix", fs->io);
168 retval = inode_ref_fix(rfs);
171 print_resource_track(rfs, &rtrack, fs->io);
173 init_resource_track(&rtrack, "move_itables", fs->io);
174 retval = move_itables(rfs);
177 print_resource_track(rfs, &rtrack, fs->io);
179 init_resource_track(&rtrack, "calculate_summary_stats", fs->io);
180 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
183 print_resource_track(rfs, &rtrack, fs->io);
185 init_resource_track(&rtrack, "fix_resize_inode", fs->io);
186 retval = fix_resize_inode(rfs->new_fs);
189 print_resource_track(rfs, &rtrack, fs->io);
191 init_resource_track(&rtrack, "fix_sb_journal_backup", fs->io);
192 retval = fix_sb_journal_backup(rfs->new_fs);
195 print_resource_track(rfs, &rtrack, fs->io);
197 retval = clear_sparse_super2_last_group(rfs);
201 rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
202 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
204 print_resource_track(rfs, &overall_track, fs->io);
205 retval = ext2fs_close(rfs->new_fs);
211 ext2fs_free(rfs->old_fs);
214 ext2fs_free_mem(&rfs->itable_buf);
215 if (rfs->reserve_blocks)
216 ext2fs_free_block_bitmap(rfs->reserve_blocks);
217 if (rfs->move_blocks)
218 ext2fs_free_block_bitmap(rfs->move_blocks);
219 ext2fs_free_mem(&rfs);
225 ext2fs_free(rfs->new_fs);
229 ext2fs_free_mem(&rfs->itable_buf);
230 ext2fs_free_mem(&rfs);
235 * Clean up the bitmaps for unitialized bitmaps
237 static void fix_uninit_block_bitmaps(ext2_filsys fs)
243 if (!ext2fs_has_group_desc_csum(fs))
246 for (g=0; g < fs->group_desc_count; g++) {
247 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
250 blk = ext2fs_group_first_block2(fs, g);
251 lblk = ext2fs_group_last_block2(fs, g);
252 ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
255 ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
256 ext2fs_mark_block_bitmap2(fs->block_map,
257 ext2fs_block_bitmap_loc(fs, g));
258 ext2fs_mark_block_bitmap2(fs->block_map,
259 ext2fs_inode_bitmap_loc(fs, g));
260 for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
261 i < (unsigned int) fs->inode_blocks_per_group;
263 ext2fs_mark_block_bitmap2(fs->block_map, blk);
267 /* --------------------------------------------------------------------
269 * Resize processing, phase 1.
271 * In this phase we adjust the in-memory superblock information, and
272 * initialize any new parts of the inode table. The new parts of the
273 * inode table are created in virgin disk space, so we can abort here
274 * without any side effects.
275 * --------------------------------------------------------------------
279 * If the group descriptor's bitmap and inode table blocks are valid,
280 * release them in the new filesystem data structure, and mark them as
281 * reserved so the old inode table blocks don't get overwritten.
283 static errcode_t free_gdp_blocks(ext2_filsys fs,
284 ext2fs_block_bitmap reserve_blocks,
291 ext2fs_block_bitmap bg_map = NULL;
292 errcode_t retval = 0;
293 dgrp_t count = old_fs->group_desc_count - fs->group_desc_count;
295 /* If bigalloc, don't free metadata living in the same cluster */
296 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
297 retval = ext2fs_allocate_block_bitmap(fs, "bgdata", &bg_map);
301 retval = mark_table_blocks(fs, bg_map);
306 for (i = group; i < group + count; i++) {
307 blk = ext2fs_block_bitmap_loc(old_fs, i);
309 (blk < ext2fs_blocks_count(fs->super)) &&
310 !(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) {
311 ext2fs_block_alloc_stats2(fs, blk, -1);
312 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
315 blk = ext2fs_inode_bitmap_loc(old_fs, i);
317 (blk < ext2fs_blocks_count(fs->super)) &&
318 !(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) {
319 ext2fs_block_alloc_stats2(fs, blk, -1);
320 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
323 blk = ext2fs_inode_table_loc(old_fs, i);
325 j < fs->inode_blocks_per_group; j++, blk++) {
326 if (blk >= ext2fs_blocks_count(fs->super) ||
327 (bg_map && ext2fs_test_block_bitmap2(bg_map, blk)))
329 ext2fs_block_alloc_stats2(fs, blk, -1);
330 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
336 ext2fs_free_block_bitmap(bg_map);
341 * This routine is shared by the online and offline resize routines.
342 * All of the information which is adjusted in memory is done here.
344 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
345 ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
348 blk64_t overhead = 0;
350 blk64_t blk, group_block;
352 blk64_t old_numblocks, numblocks, adjblocks;
353 unsigned long i, j, old_desc_blocks;
354 unsigned int meta_bg, meta_bg_size;
355 int has_super, csum_flag;
356 unsigned long long new_inodes; /* u64 to check for overflow */
359 ext2fs_blocks_count_set(fs->super, new_size);
362 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
363 fs->super->s_first_data_block,
364 EXT2_BLOCKS_PER_GROUP(fs->super));
365 if (fs->group_desc_count == 0)
366 return EXT2_ET_TOOSMALL;
367 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
368 EXT2_DESC_PER_BLOCK(fs->super));
371 * Overhead is the number of bookkeeping blocks per group. It
372 * includes the superblock backup, the group descriptor
373 * backups, the inode bitmap, the block bitmap, and the inode
376 overhead = (int) (2 + fs->inode_blocks_per_group);
378 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
379 overhead += 1 + fs->desc_blocks +
380 fs->super->s_reserved_gdt_blocks;
383 * See if the last group is big enough to support the
384 * necessary data structures. If not, we need to get rid of
387 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
388 fs->super->s_blocks_per_group;
389 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
390 return EXT2_ET_TOOSMALL;
391 if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
392 ext2fs_blocks_count_set(fs->super,
393 ext2fs_blocks_count(fs->super) - rem);
397 * Adjust the number of inodes
399 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
400 if (new_inodes > ~0U) {
401 fprintf(stderr, _("inodes (%llu) must be less than %u"),
403 return EXT2_ET_TOO_MANY_INODES;
405 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
406 fs->group_desc_count;
409 * Adjust the number of free blocks
411 blk = ext2fs_blocks_count(old_fs->super);
412 if (blk > ext2fs_blocks_count(fs->super))
413 ext2fs_free_blocks_count_set(fs->super,
414 ext2fs_free_blocks_count(fs->super) -
415 (blk - ext2fs_blocks_count(fs->super)));
417 ext2fs_free_blocks_count_set(fs->super,
418 ext2fs_free_blocks_count(fs->super) +
419 (ext2fs_blocks_count(fs->super) - blk));
422 * Adjust the number of reserved blocks
424 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
425 ext2fs_blocks_count(old_fs->super);
426 ext2fs_r_blocks_count_set(fs->super,
427 (percent * ext2fs_blocks_count(fs->super) /
431 * Adjust the bitmaps for size
433 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
434 fs->super->s_inodes_count,
436 if (retval) goto errout;
438 real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) *
439 fs->group_desc_count)) - 1 +
440 fs->super->s_first_data_block;
441 retval = ext2fs_resize_block_bitmap2(new_size - 1,
442 real_end, fs->block_map);
443 if (retval) goto errout;
446 * If we are growing the file system, also grow the size of
447 * the reserve_blocks bitmap
449 if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) {
450 retval = ext2fs_resize_block_bitmap2(new_size - 1,
451 real_end, reserve_blocks);
452 if (retval) goto errout;
456 * Reallocate the group descriptors as necessary.
458 if (old_fs->desc_blocks != fs->desc_blocks) {
459 retval = ext2fs_resize_mem(old_fs->desc_blocks *
461 fs->desc_blocks * fs->blocksize,
465 if (fs->desc_blocks > old_fs->desc_blocks)
466 memset((char *) fs->group_desc +
467 (old_fs->desc_blocks * fs->blocksize), 0,
468 (fs->desc_blocks - old_fs->desc_blocks) *
473 * If the resize_inode feature is set, and we are changing the
474 * number of descriptor blocks, then adjust
475 * s_reserved_gdt_blocks if possible to avoid needing to move
476 * the inode table either now or in the future.
478 if ((fs->super->s_feature_compat &
479 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
480 (old_fs->desc_blocks != fs->desc_blocks)) {
483 new = ((int) fs->super->s_reserved_gdt_blocks) +
484 (old_fs->desc_blocks - fs->desc_blocks);
487 if (new > (int) fs->blocksize/4)
488 new = fs->blocksize/4;
489 fs->super->s_reserved_gdt_blocks = new;
493 * Update the location of the backup superblocks if the
494 * sparse_super2 feature is enabled.
496 if (fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) {
497 dgrp_t last_bg = fs->group_desc_count - 1;
498 dgrp_t old_last_bg = old_fs->group_desc_count - 1;
500 if (last_bg > old_last_bg) {
501 if (old_fs->group_desc_count == 1)
502 fs->super->s_backup_bgs[0] = 1;
503 if (old_fs->group_desc_count == 1 &&
504 fs->super->s_backup_bgs[0])
505 fs->super->s_backup_bgs[0] = last_bg;
506 else if (fs->super->s_backup_bgs[1])
507 fs->super->s_backup_bgs[1] = last_bg;
508 } else if (last_bg < old_last_bg) {
509 if (fs->super->s_backup_bgs[0] > last_bg)
510 fs->super->s_backup_bgs[0] = 0;
511 if (fs->super->s_backup_bgs[1] > last_bg)
512 fs->super->s_backup_bgs[1] = 0;
514 old_fs->super->s_backup_bgs[1] == old_last_bg)
515 fs->super->s_backup_bgs[1] = last_bg;
520 * If we are shrinking the number of block groups, we're done
523 if (old_fs->group_desc_count > fs->group_desc_count) {
525 * Check the block groups that we are chopping off
526 * and free any blocks associated with their metadata
528 retval = free_gdp_blocks(fs, reserve_blocks, old_fs,
529 fs->group_desc_count);
534 * Fix the count of the last (old) block group
536 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
537 old_fs->super->s_first_data_block) %
538 old_fs->super->s_blocks_per_group;
540 old_numblocks = old_fs->super->s_blocks_per_group;
541 if (old_fs->group_desc_count == fs->group_desc_count) {
542 numblocks = (ext2fs_blocks_count(fs->super) -
543 fs->super->s_first_data_block) %
544 fs->super->s_blocks_per_group;
546 numblocks = fs->super->s_blocks_per_group;
548 numblocks = fs->super->s_blocks_per_group;
549 i = old_fs->group_desc_count - 1;
550 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
551 ext2fs_group_desc_csum_set(fs, i);
554 * If the number of block groups is staying the same, we're
555 * done and can exit now. (If the number block groups is
556 * shrinking, we had exited earlier.)
558 if (old_fs->group_desc_count >= fs->group_desc_count) {
564 * Initialize the new block group descriptors
566 group_block = ext2fs_group_first_block2(fs,
567 old_fs->group_desc_count);
568 csum_flag = ext2fs_has_group_desc_csum(fs);
569 if (access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0)
570 lazy_itable_init = 1;
571 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
572 old_desc_blocks = fs->super->s_first_meta_bg;
574 old_desc_blocks = fs->desc_blocks +
575 fs->super->s_reserved_gdt_blocks;
578 * If we changed the number of block_group descriptor blocks,
579 * we need to make sure they are all marked as reserved in the
580 * file systems's block allocation map.
582 for (i = 0; i < old_fs->group_desc_count; i++)
583 ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
585 for (i = old_fs->group_desc_count;
586 i < fs->group_desc_count; i++) {
587 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
588 sizeof(struct ext2_group_desc));
591 ext2fs_bg_flags_zap(fs, i);
593 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
594 if (!lazy_itable_init)
595 ext2fs_bg_flags_set(fs, i,
596 EXT2_BG_INODE_ZEROED);
597 ext2fs_bg_itable_unused_set(fs, i,
598 fs->super->s_inodes_per_group);
601 numblocks = ext2fs_group_blocks_count(fs, i);
602 if ((i < fs->group_desc_count - 1) && csum_flag)
603 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
605 has_super = ext2fs_bg_has_super(fs, i);
607 ext2fs_block_alloc_stats2(fs, group_block, +1);
610 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
611 meta_bg = i / meta_bg_size;
612 if (!(fs->super->s_feature_incompat &
613 EXT2_FEATURE_INCOMPAT_META_BG) ||
614 (meta_bg < fs->super->s_first_meta_bg)) {
616 for (j=0; j < old_desc_blocks; j++)
617 ext2fs_block_alloc_stats2(fs,
618 group_block + 1 + j, +1);
619 adjblocks += old_desc_blocks;
624 if (((i % meta_bg_size) == 0) ||
625 ((i % meta_bg_size) == 1) ||
626 ((i % meta_bg_size) == (meta_bg_size-1)))
627 ext2fs_block_alloc_stats2(fs,
628 group_block + has_super, +1);
631 adjblocks += 2 + fs->inode_blocks_per_group;
633 numblocks -= adjblocks;
634 ext2fs_free_blocks_count_set(fs->super,
635 ext2fs_free_blocks_count(fs->super) - adjblocks);
636 fs->super->s_free_inodes_count +=
637 fs->super->s_inodes_per_group;
638 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
639 ext2fs_bg_free_inodes_count_set(fs, i,
640 fs->super->s_inodes_per_group);
641 ext2fs_bg_used_dirs_count_set(fs, i, 0);
642 ext2fs_group_desc_csum_set(fs, i);
644 retval = ext2fs_allocate_group_table(fs, i, 0);
645 if (retval) goto errout;
647 group_block += fs->super->s_blocks_per_group;
652 * Mark all of the metadata blocks as reserved so they won't
653 * get allocated by the call to ext2fs_allocate_group_table()
654 * in blocks_to_move(), where we allocate new blocks to
655 * replace those allocation bitmap and inode table blocks
656 * which have to get relocated to make space for an increased
657 * number of the block group descriptors.
660 mark_table_blocks(fs, reserve_blocks);
667 * This routine adjusts the superblock and other data structures, both
668 * in disk as well as in memory...
670 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
672 ext2_filsys fs = rfs->new_fs;
677 unsigned long max_group;
679 ext2fs_mark_super_dirty(fs);
680 ext2fs_mark_bb_dirty(fs);
681 ext2fs_mark_ib_dirty(fs);
683 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
684 &rfs->reserve_blocks);
688 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
693 * Check to make sure there are enough inodes
695 if ((rfs->old_fs->super->s_inodes_count -
696 rfs->old_fs->super->s_free_inodes_count) >
697 rfs->new_fs->super->s_inodes_count) {
703 * If we are shrinking the number block groups, we're done and
706 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
712 * If the number of block groups is staying the same, we're
713 * done and can exit now. (If the number block groups is
714 * shrinking, we had exited earlier.)
716 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
722 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
723 * supports lazy inode initialization, we can skip
724 * initializing the inode table.
726 if (lazy_itable_init && ext2fs_has_group_desc_csum(fs)) {
732 * Initialize the inode table
734 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
739 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
740 group_block = ext2fs_group_first_block2(fs,
741 rfs->old_fs->group_desc_count);
742 adj = rfs->old_fs->group_desc_count;
743 max_group = fs->group_desc_count - adj;
745 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
750 for (i = rfs->old_fs->group_desc_count;
751 i < fs->group_desc_count; i++) {
753 * Write out the new inode table
755 retval = io_channel_write_blk64(fs->io,
756 ext2fs_inode_table_loc(fs, i),
757 fs->inode_blocks_per_group,
759 if (retval) goto errout;
761 io_channel_flush(fs->io);
763 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
764 i - adj + 1, max_group);
768 group_block += fs->super->s_blocks_per_group;
770 io_channel_flush(fs->io);
777 /* --------------------------------------------------------------------
779 * Resize processing, phase 2.
781 * In this phase we adjust determine which blocks need to be moved, in
782 * blocks_to_move(). We then copy the blocks to their ultimate new
783 * destinations using block_mover(). Since we are copying blocks to
784 * their new locations, again during this pass we can abort without
786 * --------------------------------------------------------------------
790 * This helper function creates a block bitmap with all of the
791 * filesystem meta-data blocks.
793 static errcode_t mark_table_blocks(ext2_filsys fs,
794 ext2fs_block_bitmap bmap)
799 for (i = 0; i < fs->group_desc_count; i++) {
800 ext2fs_reserve_super_and_bgd(fs, i, bmap);
803 * Mark the blocks used for the inode table
805 blk = ext2fs_inode_table_loc(fs, i);
807 ext2fs_mark_block_bitmap_range2(bmap, blk,
808 fs->inode_blocks_per_group);
811 * Mark block used for the block bitmap
813 blk = ext2fs_block_bitmap_loc(fs, i);
815 ext2fs_mark_block_bitmap2(bmap, blk);
818 * Mark block used for the inode bitmap
820 blk = ext2fs_inode_bitmap_loc(fs, i);
822 ext2fs_mark_block_bitmap2(bmap, blk);
828 * This function checks to see if a particular block (either a
829 * superblock or a block group descriptor) overlaps with an inode or
830 * block bitmap block, or with the inode table.
832 static void mark_fs_metablock(ext2_resize_t rfs,
833 ext2fs_block_bitmap meta_bmap,
834 int group, blk64_t blk)
836 ext2_filsys fs = rfs->new_fs;
838 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
839 ext2fs_block_alloc_stats2(fs, blk, +1);
842 * Check to see if we overlap with the inode or block bitmap,
843 * or the inode tables. If not, and the block is in use, then
844 * mark it as a block to be moved.
846 if (IS_BLOCK_BM(fs, group, blk)) {
847 ext2fs_block_bitmap_loc_set(fs, group, 0);
848 rfs->needed_blocks++;
851 if (IS_INODE_BM(fs, group, blk)) {
852 ext2fs_inode_bitmap_loc_set(fs, group, 0);
853 rfs->needed_blocks++;
856 if (IS_INODE_TB(fs, group, blk)) {
857 ext2fs_inode_table_loc_set(fs, group, 0);
858 rfs->needed_blocks++;
861 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
864 for (i=0; i < rfs->old_fs->group_desc_count; i++) {
865 if (IS_BLOCK_BM(fs, i, blk)) {
866 ext2fs_block_bitmap_loc_set(fs, i, 0);
867 rfs->needed_blocks++;
870 if (IS_INODE_BM(fs, i, blk)) {
871 ext2fs_inode_bitmap_loc_set(fs, i, 0);
872 rfs->needed_blocks++;
875 if (IS_INODE_TB(fs, i, blk)) {
876 ext2fs_inode_table_loc_set(fs, i, 0);
877 rfs->needed_blocks++;
883 if (ext2fs_has_group_desc_csum(fs) &&
884 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
886 * If the block bitmap is uninitialized, which means
887 * nothing other than standard metadata in use.
890 } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
891 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
892 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
893 rfs->needed_blocks++;
899 * This routine marks and unmarks reserved blocks in the new block
900 * bitmap. It also determines which blocks need to be moved and
901 * places this information into the move_blocks bitmap.
903 static errcode_t blocks_to_move(ext2_resize_t rfs)
906 dgrp_t i, max_groups, g;
907 blk64_t blk, group_blk;
908 blk64_t old_blocks, new_blocks, group_end, cluster_freed;
910 unsigned int meta_bg, meta_bg_size;
912 ext2_filsys fs, old_fs;
913 ext2fs_block_bitmap meta_bmap, new_meta_bmap = NULL;
917 old_fs = rfs->old_fs;
918 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
921 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
926 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
931 retval = mark_table_blocks(old_fs, meta_bmap);
938 * If we're shrinking the filesystem, we need to move any
939 * group's metadata blocks (either allocation bitmaps or the
940 * inode table) which are beyond the end of the new
943 new_size = ext2fs_blocks_count(fs->super);
944 if (new_size < ext2fs_blocks_count(old_fs->super)) {
945 for (g = 0; g < fs->group_desc_count; g++) {
948 * ext2fs_allocate_group_table will re-allocate any
949 * metadata blocks whose location is set to zero.
951 if (ext2fs_block_bitmap_loc(fs, g) >= new_size) {
952 ext2fs_block_bitmap_loc_set(fs, g, 0);
955 if (ext2fs_inode_bitmap_loc(fs, g) >= new_size) {
956 ext2fs_inode_bitmap_loc_set(fs, g, 0);
959 if ((ext2fs_inode_table_loc(fs, g) +
960 fs->inode_blocks_per_group) > new_size) {
961 ext2fs_inode_table_loc_set(fs, g, 0);
966 retval = ext2fs_allocate_group_table(fs, g, 0);
974 * If we're shrinking the filesystem, we need to move all of
975 * the blocks that don't fit any more
977 for (blk = ext2fs_blocks_count(fs->super);
978 blk < ext2fs_blocks_count(old_fs->super); blk++) {
979 g = ext2fs_group_of_blk2(fs, blk);
980 if (ext2fs_has_group_desc_csum(fs) &&
981 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
983 * The block bitmap is uninitialized, so skip
984 * to the next block group.
986 blk = ext2fs_group_first_block2(fs, g+1) - 1;
989 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
990 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
991 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
992 rfs->needed_blocks++;
994 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
997 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
998 old_blocks = old_fs->super->s_first_meta_bg;
999 new_blocks = fs->super->s_first_meta_bg;
1001 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
1002 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
1005 retval = reserve_sparse_super2_last_group(rfs, meta_bmap);
1009 if (old_blocks == new_blocks) {
1014 max_groups = fs->group_desc_count;
1015 if (max_groups > old_fs->group_desc_count)
1016 max_groups = old_fs->group_desc_count;
1017 group_blk = old_fs->super->s_first_data_block;
1019 * If we're reducing the number of descriptor blocks, this
1020 * makes life easy. :-) We just have to mark some extra
1023 if (old_blocks > new_blocks) {
1024 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
1025 retval = ext2fs_allocate_block_bitmap(fs,
1026 _("new meta blocks"),
1031 retval = mark_table_blocks(fs, new_meta_bmap);
1036 for (i = 0; i < max_groups; i++) {
1037 if (!ext2fs_bg_has_super(fs, i)) {
1038 group_blk += fs->super->s_blocks_per_group;
1041 group_end = group_blk + 1 + old_blocks;
1042 for (blk = group_blk + 1 + new_blocks;
1044 if (new_meta_bmap == NULL ||
1045 !ext2fs_test_block_bitmap2(new_meta_bmap,
1048 EXT2FS_CLUSTER_RATIO(fs) -
1050 EXT2FS_CLUSTER_MASK(fs));
1051 if (cluster_freed > group_end - blk)
1052 cluster_freed = group_end - blk;
1053 ext2fs_block_alloc_stats2(fs, blk, -1);
1054 blk += EXT2FS_CLUSTER_RATIO(fs);
1055 rfs->needed_blocks -= cluster_freed;
1058 rfs->needed_blocks--;
1061 group_blk += fs->super->s_blocks_per_group;
1067 * If we're increasing the number of descriptor blocks, life
1068 * gets interesting....
1070 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
1071 flex_bg = fs->super->s_feature_incompat &
1072 EXT4_FEATURE_INCOMPAT_FLEX_BG;
1073 /* first reserve all of the existing fs meta blocks */
1074 for (i = 0; i < max_groups; i++) {
1075 has_super = ext2fs_bg_has_super(fs, i);
1077 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
1079 meta_bg = i / meta_bg_size;
1080 if (!(fs->super->s_feature_incompat &
1081 EXT2_FEATURE_INCOMPAT_META_BG) ||
1082 (meta_bg < fs->super->s_first_meta_bg)) {
1084 for (blk = group_blk+1;
1085 blk < group_blk + 1 + new_blocks; blk++)
1086 mark_fs_metablock(rfs, meta_bmap,
1092 if (((i % meta_bg_size) == 0) ||
1093 ((i % meta_bg_size) == 1) ||
1094 ((i % meta_bg_size) == (meta_bg_size-1)))
1095 mark_fs_metablock(rfs, meta_bmap, i,
1096 group_blk + has_super);
1100 * Reserve the existing meta blocks that we know
1101 * aren't to be moved.
1103 * For flex_bg file systems, in order to avoid
1104 * overwriting fs metadata (especially inode table
1105 * blocks) belonging to a different block group when
1106 * we are relocating the inode tables, we need to
1107 * reserve all existing fs metadata blocks.
1109 if (ext2fs_block_bitmap_loc(fs, i))
1110 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1111 ext2fs_block_bitmap_loc(fs, i));
1112 else if (flex_bg && i < old_fs->group_desc_count)
1113 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1114 ext2fs_block_bitmap_loc(old_fs, i));
1116 if (ext2fs_inode_bitmap_loc(fs, i))
1117 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1118 ext2fs_inode_bitmap_loc(fs, i));
1119 else if (flex_bg && i < old_fs->group_desc_count)
1120 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1121 ext2fs_inode_bitmap_loc(old_fs, i));
1123 if (ext2fs_inode_table_loc(fs, i))
1124 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1125 ext2fs_inode_table_loc(fs, i),
1126 fs->inode_blocks_per_group);
1127 else if (flex_bg && i < old_fs->group_desc_count)
1128 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1129 ext2fs_inode_table_loc(old_fs, i),
1130 old_fs->inode_blocks_per_group);
1132 group_blk += rfs->new_fs->super->s_blocks_per_group;
1135 /* Allocate the missing data structures */
1136 for (i = 0; i < max_groups; i++) {
1137 if (ext2fs_inode_table_loc(fs, i) &&
1138 ext2fs_inode_bitmap_loc(fs, i) &&
1139 ext2fs_block_bitmap_loc(fs, i))
1142 retval = ext2fs_allocate_group_table(fs, i,
1143 rfs->reserve_blocks);
1148 * For those structures that have changed, we need to
1151 if (ext2fs_block_bitmap_loc(old_fs, i) !=
1152 (blk = ext2fs_block_bitmap_loc(fs, i))) {
1153 ext2fs_block_alloc_stats2(fs, blk, +1);
1154 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1155 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1156 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1159 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
1160 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
1161 ext2fs_block_alloc_stats2(fs, blk, +1);
1162 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1163 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1164 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1169 * The inode table, if we need to relocate it, is
1170 * handled specially. We have to reserve the blocks
1171 * for both the old and the new inode table, since we
1172 * can't have the inode table be destroyed during the
1173 * block relocation phase.
1175 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
1176 continue; /* inode table not moved */
1178 rfs->needed_blocks += fs->inode_blocks_per_group;
1181 * Mark the new inode table as in use in the new block
1182 * allocation bitmap, and move any blocks that might
1185 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
1186 j < fs->inode_blocks_per_group ; j++, blk++) {
1187 ext2fs_block_alloc_stats2(fs, blk, +1);
1188 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1189 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1190 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1195 * Make sure the old inode table is reserved in the
1196 * block reservation bitmap.
1198 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1199 j < fs->inode_blocks_per_group ; j++, blk++)
1200 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1206 ext2fs_free_block_bitmap(new_meta_bmap);
1208 ext2fs_free_block_bitmap(meta_bmap);
1214 * This helper function tries to allocate a new block. We try to
1215 * avoid hitting the original group descriptor blocks at least at
1216 * first, since we want to make it possible to recover from a badly
1217 * aborted resize operation as much as possible.
1219 * In the future, I may further modify this routine to balance out
1220 * where we get the new blocks across the various block groups.
1221 * Ideally we would allocate blocks that corresponded with the block
1222 * group of the containing inode, and keep contiguous blocks
1223 * together. However, this very difficult to do efficiently, since we
1224 * don't have the necessary information up front.
1228 #define DESPERATION 2
1230 static void init_block_alloc(ext2_resize_t rfs)
1232 rfs->alloc_state = AVOID_OLD;
1233 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1235 /* HACK for testing */
1236 if (ext2fs_blocks_count(rfs->new_fs->super) >
1237 ext2fs_blocks_count(rfs->old_fs->super))
1238 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1242 static blk64_t get_new_block(ext2_resize_t rfs)
1244 ext2_filsys fs = rfs->new_fs;
1247 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1248 if (rfs->alloc_state == DESPERATION)
1251 #ifdef RESIZE2FS_DEBUG
1252 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1253 printf("Going into desperation mode "
1254 "for block allocations\n");
1256 rfs->alloc_state = DESPERATION;
1257 rfs->new_blk = fs->super->s_first_data_block;
1260 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1261 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1263 ((rfs->alloc_state == AVOID_OLD) &&
1264 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1265 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1270 return rfs->new_blk;
1274 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1277 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1280 blk = get_new_block(rfs);
1284 #ifdef RESIZE2FS_DEBUG
1285 if (rfs->flags & 0xF)
1286 printf("get_alloc_block allocating %llu\n", blk);
1289 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1290 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1291 *ret = (blk64_t) blk;
1295 static errcode_t block_mover(ext2_resize_t rfs)
1297 blk64_t blk, old_blk, new_blk;
1298 ext2_filsys fs = rfs->new_fs;
1299 ext2_filsys old_fs = rfs->old_fs;
1304 ext2_badblocks_list badblock_list = 0;
1305 int bb_modified = 0;
1307 fs->get_alloc_block = resize2fs_get_alloc_block;
1308 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1310 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1314 new_blk = fs->super->s_first_data_block;
1315 if (!rfs->itable_buf) {
1316 retval = ext2fs_get_array(fs->blocksize,
1317 fs->inode_blocks_per_group,
1322 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1327 * The first step is to figure out where all of the blocks
1330 to_move = moved = 0;
1331 init_block_alloc(rfs);
1332 for (blk = B2C(old_fs->super->s_first_data_block);
1333 blk < ext2fs_blocks_count(old_fs->super);
1334 blk += EXT2FS_CLUSTER_RATIO(fs)) {
1335 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1337 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1339 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1340 ext2fs_badblocks_list_del(badblock_list, blk);
1345 new_blk = get_new_block(rfs);
1350 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1351 ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk));
1357 ext2fs_free_extent_table(rfs->bmap);
1365 * Step two is to actually move the blocks
1367 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1368 if (retval) goto errout;
1370 if (rfs->progress) {
1371 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1377 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1378 if (retval) goto errout;
1381 old_blk = C2B(old_blk);
1382 new_blk = C2B(new_blk);
1384 #ifdef RESIZE2FS_DEBUG
1385 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1386 printf("Moving %llu blocks %llu->%llu\n",
1387 size, old_blk, new_blk);
1391 if (c > fs->inode_blocks_per_group)
1392 c = fs->inode_blocks_per_group;
1393 retval = io_channel_read_blk64(fs->io, old_blk, c,
1395 if (retval) goto errout;
1396 retval = io_channel_write_blk64(fs->io, new_blk, c,
1398 if (retval) goto errout;
1403 if (rfs->progress) {
1404 io_channel_flush(fs->io);
1405 retval = (rfs->progress)(rfs,
1406 E2_RSZ_BLOCK_RELOC_PASS,
1412 io_channel_flush(fs->io);
1416 if (badblock_list) {
1417 if (!retval && bb_modified)
1418 retval = ext2fs_update_bb_inode(old_fs,
1420 ext2fs_badblocks_list_free(badblock_list);
1426 /* --------------------------------------------------------------------
1428 * Resize processing, phase 3
1430 * --------------------------------------------------------------------
1435 * The extent translation table is stored in clusters so we need to
1436 * take special care when mapping a source block number to its
1437 * destination block number.
1439 static __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc)
1441 __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc)));
1444 new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1);
1448 struct process_block_struct {
1452 struct ext2_inode * inode;
1459 static int process_block(ext2_filsys fs, blk64_t *block_nr,
1460 e2_blkcnt_t blockcnt,
1461 blk64_t ref_block EXT2FS_ATTR((unused)),
1462 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1464 struct process_block_struct *pb;
1466 blk64_t block, new_block;
1469 pb = (struct process_block_struct *) priv_data;
1471 if (pb->rfs->bmap) {
1472 new_block = extent_translate(fs, pb->rfs->bmap, block);
1474 *block_nr = new_block;
1475 ret |= BLOCK_CHANGED;
1477 #ifdef RESIZE2FS_DEBUG
1478 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1479 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1480 pb->old_ino, blockcnt, block,
1488 * If we moved inodes and metadata_csum is enabled, we must force the
1489 * extent block to be rewritten with new checksum.
1491 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
1492 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
1494 pb->old_ino != pb->ino)
1495 ret |= BLOCK_CHANGED;
1498 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1499 block, (int) blockcnt);
1511 static errcode_t progress_callback(ext2_filsys fs,
1512 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1513 dgrp_t group, void * priv_data)
1515 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1519 * This check is to protect against old ext2 libraries. It
1520 * shouldn't be needed against new libraries.
1525 if (rfs->progress) {
1526 io_channel_flush(fs->io);
1527 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1528 group+1, fs->group_desc_count);
1536 static errcode_t migrate_ea_block(ext2_resize_t rfs, ext2_ino_t ino,
1537 struct ext2_inode *inode, int *changed)
1543 /* No EA block or no remapping? Quit early. */
1544 if (ext2fs_file_acl_block(rfs->old_fs, inode) == 0 && !rfs->bmap)
1546 new_block = extent_translate(rfs->old_fs, rfs->bmap,
1547 ext2fs_file_acl_block(rfs->old_fs, inode));
1551 /* Set the new ACL block */
1552 ext2fs_file_acl_block_set(rfs->old_fs, inode, new_block);
1554 /* Update checksum */
1555 if (EXT2_HAS_RO_COMPAT_FEATURE(rfs->new_fs->super,
1556 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1557 err = ext2fs_get_mem(rfs->old_fs->blocksize, &buf);
1560 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1561 err = ext2fs_read_ext_attr3(rfs->old_fs, new_block, buf, ino);
1562 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1565 err = ext2fs_write_ext_attr3(rfs->old_fs, new_block, buf, ino);
1572 ext2fs_free_mem(&buf);
1576 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1578 struct process_block_struct pb;
1579 ext2_ino_t ino, new_inode;
1580 struct ext2_inode *inode = NULL;
1581 ext2_inode_scan scan = NULL;
1583 char *block_buf = 0;
1584 ext2_ino_t start_to_move;
1588 if ((rfs->old_fs->group_desc_count <=
1589 rfs->new_fs->group_desc_count) &&
1594 * Save the original size of the old filesystem, and
1595 * temporarily set the size to be the new size if the new size
1596 * is larger. We need to do this to avoid catching an error
1597 * by the block iterator routines
1599 orig_size = ext2fs_blocks_count(rfs->old_fs->super);
1600 if (orig_size < ext2fs_blocks_count(rfs->new_fs->super))
1601 ext2fs_blocks_count_set(rfs->old_fs->super,
1602 ext2fs_blocks_count(rfs->new_fs->super));
1604 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1605 if (retval) goto errout;
1607 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1608 if (retval) goto errout;
1609 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1610 if (retval) goto errout;
1612 start_to_move = (rfs->new_fs->group_desc_count *
1613 rfs->new_fs->super->s_inodes_per_group);
1615 if (rfs->progress) {
1616 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1617 0, rfs->old_fs->group_desc_count);
1621 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1625 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1626 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1627 inode = malloc(inode_size);
1633 * First, copy all of the inodes that need to be moved
1634 * elsewhere in the inode table
1637 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1638 if (retval) goto errout;
1642 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1643 continue; /* inode not in use */
1645 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1648 /* Remap EA block */
1649 retval = migrate_ea_block(rfs, ino, inode, &pb.changed);
1654 if (ino <= start_to_move)
1655 goto remap_blocks; /* Don't need to move inode. */
1658 * Find a new inode. Now that extents and directory blocks
1659 * are tied to the inode number through the checksum, we must
1660 * set up the new inode before we start rewriting blocks.
1662 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1666 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1668 inode->i_ctime = time(0);
1669 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1675 #ifdef RESIZE2FS_DEBUG
1676 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1677 printf("Inode moved %u->%u\n", ino, new_inode);
1680 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1684 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1688 retval = ext2fs_write_inode_full(rfs->old_fs,
1695 * Update inodes to point to new blocks; schedule directory
1696 * blocks for inode remapping. Need to write out dir blocks
1697 * with new inode numbers if we have metadata_csum enabled.
1699 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
1700 (rfs->bmap || pb.is_dir)) {
1703 pb.has_extents = inode->i_flags & EXT4_EXTENTS_FL;
1704 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1705 retval = ext2fs_block_iterate3(rfs->old_fs,
1706 new_inode, 0, block_buf,
1707 process_block, &pb);
1708 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1717 io_channel_flush(rfs->old_fs->io);
1720 ext2fs_blocks_count_set(rfs->old_fs->super, orig_size);
1722 ext2fs_free_extent_table(rfs->bmap);
1726 ext2fs_close_inode_scan(scan);
1728 ext2fs_free_mem(&block_buf);
1733 /* --------------------------------------------------------------------
1735 * Resize processing, phase 4.
1737 * --------------------------------------------------------------------
1743 unsigned int max_dirs;
1747 static int check_and_change_inodes(ext2_ino_t dir,
1748 int entry EXT2FS_ATTR((unused)),
1749 struct ext2_dir_entry *dirent, int offset,
1750 int blocksize EXT2FS_ATTR((unused)),
1751 char *buf EXT2FS_ATTR((unused)),
1754 struct istruct *is = (struct istruct *) priv_data;
1755 struct ext2_inode inode;
1756 ext2_ino_t new_inode;
1760 if (is->rfs->progress && offset == 0) {
1761 io_channel_flush(is->rfs->old_fs->io);
1762 is->err = (is->rfs->progress)(is->rfs,
1763 E2_RSZ_INODE_REF_UPD_PASS,
1764 ++is->num, is->max_dirs);
1766 return DIRENT_ABORT;
1770 * If we have checksums enabled and the inode wasn't present in the
1771 * old fs, then we must rewrite all dir blocks with new checksums.
1773 if (EXT2_HAS_RO_COMPAT_FEATURE(is->rfs->old_fs->super,
1774 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
1775 !ext2fs_test_inode_bitmap2(is->rfs->old_fs->inode_map, dir))
1776 ret |= DIRENT_CHANGED;
1781 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1785 #ifdef RESIZE2FS_DEBUG
1786 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1787 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1788 dir, ext2fs_dirent_name_len(dirent), dirent->name,
1789 dirent->inode, new_inode);
1792 dirent->inode = new_inode;
1794 /* Update the directory mtime and ctime */
1795 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1797 inode.i_mtime = inode.i_ctime = time(0);
1798 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1800 return ret | DIRENT_ABORT;
1803 return ret | DIRENT_CHANGED;
1806 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1815 * Now, we iterate over all of the directories to update the
1819 is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
1823 if (rfs->progress) {
1824 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1830 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1831 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1832 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1833 check_and_change_inodes, &is);
1834 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1842 if (rfs->progress && (is.num < is.max_dirs))
1843 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1844 is.max_dirs, is.max_dirs);
1847 ext2fs_free_extent_table(rfs->imap);
1853 /* --------------------------------------------------------------------
1855 * Resize processing, phase 5.
1857 * In this phase we actually move the inode table around, and then
1858 * update the summary statistics. This is scary, since aborting here
1859 * will potentially scramble the filesystem. (We are moving the
1860 * inode tables around in place, and so the potential for lost data,
1861 * or at the very least scrambling the mapping between filenames and
1862 * inode numbers is very high in case of a power failure here.)
1863 * --------------------------------------------------------------------
1868 * A very scary routine --- this one moves the inode table around!!!
1870 * After this you have to use the rfs->new_fs file handle to read and
1873 static errcode_t move_itables(ext2_resize_t rfs)
1877 dgrp_t i, max_groups;
1878 ext2_filsys fs = rfs->new_fs;
1880 blk64_t old_blk, new_blk, blk, cluster_freed;
1882 int j, to_move, moved;
1883 ext2fs_block_bitmap new_bmap = NULL;
1885 max_groups = fs->group_desc_count;
1886 if (max_groups > rfs->old_fs->group_desc_count)
1887 max_groups = rfs->old_fs->group_desc_count;
1889 size = fs->blocksize * fs->inode_blocks_per_group;
1890 if (!rfs->itable_buf) {
1891 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1896 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
1897 retval = ext2fs_allocate_block_bitmap(fs, _("new meta blocks"),
1902 retval = mark_table_blocks(fs, new_bmap);
1908 * Figure out how many inode tables we need to move
1910 to_move = moved = 0;
1911 for (i=0; i < max_groups; i++)
1912 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
1913 ext2fs_inode_table_loc(fs, i))
1919 if (rfs->progress) {
1920 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1926 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1928 for (i=0; i < max_groups; i++) {
1929 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
1930 new_blk = ext2fs_inode_table_loc(fs, i);
1931 diff = new_blk - old_blk;
1933 #ifdef RESIZE2FS_DEBUG
1934 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1935 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
1936 i, old_blk, new_blk, diff);
1942 retval = io_channel_read_blk64(fs->io, old_blk,
1943 fs->inode_blocks_per_group,
1948 * The end of the inode table segment often contains
1949 * all zeros, and we're often only moving the inode
1950 * table down a block or two. If so, we can optimize
1951 * things by not rewriting blocks that we know to be zero
1954 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1957 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1958 #ifdef RESIZE2FS_DEBUG
1959 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1960 printf("%d blocks of zeros...\n", n);
1962 num = fs->inode_blocks_per_group;
1966 retval = io_channel_write_blk64(fs->io, new_blk,
1967 num, rfs->itable_buf);
1969 io_channel_write_blk64(fs->io, old_blk,
1970 num, rfs->itable_buf);
1974 retval = io_channel_write_blk64(fs->io,
1975 old_blk + fs->inode_blocks_per_group,
1976 diff, (rfs->itable_buf +
1977 (fs->inode_blocks_per_group - diff) *
1983 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1984 j < fs->inode_blocks_per_group;) {
1985 if (new_bmap == NULL ||
1986 !ext2fs_test_block_bitmap2(new_bmap, blk)) {
1987 ext2fs_block_alloc_stats2(fs, blk, -1);
1988 cluster_freed = EXT2FS_CLUSTER_RATIO(fs) -
1989 (blk & EXT2FS_CLUSTER_MASK(fs));
1990 blk += cluster_freed;
1998 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
1999 ext2fs_group_desc_csum_set(rfs->old_fs, i);
2000 ext2fs_mark_super_dirty(rfs->old_fs);
2001 ext2fs_flush(rfs->old_fs);
2003 if (rfs->progress) {
2004 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
2010 mark_table_blocks(fs, fs->block_map);
2012 #ifdef RESIZE2FS_DEBUG
2013 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
2014 printf("Inode table move finished.\n");
2020 ext2fs_free_block_bitmap(new_bmap);
2025 * This function is used when expanding a file system. It frees the
2026 * superblock and block group descriptor blocks from the block group
2027 * which is no longer the last block group.
2029 static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs)
2031 ext2_filsys fs = rfs->new_fs;
2032 ext2_filsys old_fs = rfs->old_fs;
2034 dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1;
2035 dgrp_t last_bg = fs->group_desc_count - 1;
2036 blk64_t sb, old_desc;
2039 if (!(fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2))
2042 if (last_bg <= old_last_bg)
2045 if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
2046 fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
2049 if (old_fs->super->s_backup_bgs[0] != old_last_bg &&
2050 old_fs->super->s_backup_bgs[1] != old_last_bg)
2053 if (fs->super->s_backup_bgs[0] == old_last_bg ||
2054 fs->super->s_backup_bgs[1] == old_last_bg)
2057 retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, old_last_bg,
2058 &sb, &old_desc, NULL, &num);
2063 ext2fs_unmark_block_bitmap2(fs->block_map, sb);
2065 ext2fs_unmark_block_bitmap_range2(fs->block_map, old_desc, num);
2070 * This function is used when shrinking a file system. We need to
2071 * utilize blocks from what will be the new last block group for the
2072 * backup superblock and block group descriptor blocks.
2073 * Unfortunately, those blocks may be used by other files or fs
2074 * metadata blocks. We need to mark them as being in use.
2076 static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
2077 ext2fs_block_bitmap meta_bmap)
2079 ext2_filsys fs = rfs->new_fs;
2080 ext2_filsys old_fs = rfs->old_fs;
2082 dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1;
2083 dgrp_t last_bg = fs->group_desc_count - 1;
2085 blk64_t blk, sb, old_desc;
2089 if (!(fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2))
2092 if (last_bg >= old_last_bg)
2095 if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
2096 fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
2099 if (fs->super->s_backup_bgs[0] != last_bg &&
2100 fs->super->s_backup_bgs[1] != last_bg)
2103 if (old_fs->super->s_backup_bgs[0] == last_bg ||
2104 old_fs->super->s_backup_bgs[1] == last_bg)
2107 retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, last_bg,
2108 &sb, &old_desc, NULL, &num);
2113 fputs(_("Should never happen! No sb in last super_sparse bg?\n"),
2117 if (old_desc != sb+1) {
2118 fputs(_("Should never happen! Unexpected old_desc in "
2119 "super_sparse bg?\n"),
2123 num = (old_desc) ? num : 1;
2125 /* Reserve the backup blocks */
2126 ext2fs_mark_block_bitmap_range2(fs->block_map, sb, num);
2128 for (g = 0; g < fs->group_desc_count; g++) {
2131 mb = ext2fs_block_bitmap_loc(fs, g);
2132 if ((mb >= sb) && (mb < sb + num)) {
2133 ext2fs_block_bitmap_loc_set(fs, g, 0);
2136 mb = ext2fs_inode_bitmap_loc(fs, g);
2137 if ((mb >= sb) && (mb < sb + num)) {
2138 ext2fs_inode_bitmap_loc_set(fs, g, 0);
2141 mb = ext2fs_inode_table_loc(fs, g);
2142 if ((mb < sb + num) &&
2143 (sb < mb + fs->inode_blocks_per_group)) {
2144 ext2fs_inode_table_loc_set(fs, g, 0);
2148 retval = ext2fs_allocate_group_table(fs, g, 0);
2154 for (blk = sb, i = 0; i < num; blk++, i++) {
2155 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
2156 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
2157 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
2158 rfs->needed_blocks++;
2160 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
2166 * Fix the resize inode
2168 static errcode_t fix_resize_inode(ext2_filsys fs)
2170 struct ext2_inode inode;
2172 char *block_buf = NULL;
2174 if (!(fs->super->s_feature_compat &
2175 EXT2_FEATURE_COMPAT_RESIZE_INODE))
2178 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
2179 if (retval) goto errout;
2181 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
2182 if (retval) goto errout;
2184 ext2fs_iblk_set(fs, &inode, 1);
2186 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
2187 if (retval) goto errout;
2189 if (!inode.i_block[EXT2_DIND_BLOCK]) {
2191 * Avoid zeroing out block #0; that's rude. This
2192 * should never happen anyway since the filesystem
2193 * should be fsck'ed and we assume it is consistent.
2195 fprintf(stderr, "%s",
2196 _("Should never happen: resize inode corrupt!\n"));
2200 memset(block_buf, 0, fs->blocksize);
2202 retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK],
2204 if (retval) goto errout;
2206 retval = ext2fs_create_resize_inode(fs);
2212 ext2fs_free_mem(&block_buf);
2217 * Finally, recalculate the summary information
2219 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
2223 unsigned int group = 0;
2224 unsigned int count = 0;
2225 blk64_t total_blocks_free = 0;
2226 int total_inodes_free = 0;
2229 blk64_t super_blk, old_desc_blk, new_desc_blk;
2230 int old_desc_blocks;
2233 * First calculate the block statistics
2235 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2236 ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
2238 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2239 old_desc_blocks = fs->super->s_first_meta_bg;
2241 old_desc_blocks = fs->desc_blocks +
2242 fs->super->s_reserved_gdt_blocks;
2243 for (blk = B2C(fs->super->s_first_data_block);
2244 blk < ext2fs_blocks_count(fs->super);
2245 blk += EXT2FS_CLUSTER_RATIO(fs)) {
2247 !(EQ_CLSTR(blk, super_blk) ||
2248 ((old_desc_blk && old_desc_blocks &&
2249 GE_CLSTR(blk, old_desc_blk) &&
2250 LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) ||
2251 ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) ||
2252 EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) ||
2253 EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) ||
2254 ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) &&
2255 LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group)
2256 + fs->inode_blocks_per_group))))) ||
2257 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
2259 total_blocks_free++;
2262 if ((count == fs->super->s_clusters_per_group) ||
2263 EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) {
2264 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
2265 ext2fs_group_desc_csum_set(fs, group);
2267 if (group >= fs->group_desc_count)
2271 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2272 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
2275 if (fs->super->s_feature_incompat &
2276 EXT2_FEATURE_INCOMPAT_META_BG)
2277 old_desc_blocks = fs->super->s_first_meta_bg;
2279 old_desc_blocks = fs->desc_blocks +
2280 fs->super->s_reserved_gdt_blocks;
2283 total_blocks_free = C2B(total_blocks_free);
2284 ext2fs_free_blocks_count_set(fs->super, total_blocks_free);
2287 * Next, calculate the inode statistics
2293 /* Protect loop from wrap-around if s_inodes_count maxed */
2294 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2295 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2297 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2299 total_inodes_free++;
2302 if ((count == fs->super->s_inodes_per_group) ||
2303 (ino == fs->super->s_inodes_count)) {
2304 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
2305 ext2fs_group_desc_csum_set(fs, group);
2307 if (group >= fs->group_desc_count)
2311 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2314 fs->super->s_free_inodes_count = total_inodes_free;
2315 ext2fs_mark_super_dirty(fs);
2320 * Journal may have been relocated; update the backup journal blocks
2321 * in the superblock.
2323 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
2326 struct ext2_inode inode;
2328 if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
2331 /* External journal? Nothing to do. */
2332 if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
2335 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
2338 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
2339 fs->super->s_jnl_blocks[15] = inode.i_size_high;
2340 fs->super->s_jnl_blocks[16] = inode.i_size;
2341 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
2342 ext2fs_mark_super_dirty(fs);
2346 static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
2347 int old_desc_blocks)
2349 blk64_t super_blk, old_desc_blk, new_desc_blk;
2352 /* inode table blocks plus allocation bitmaps */
2353 overhead = fs->inode_blocks_per_group + 2;
2355 ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
2356 &old_desc_blk, &new_desc_blk, 0);
2357 if ((grp == 0) || super_blk)
2360 overhead += old_desc_blocks;
2361 else if (new_desc_blk)
2368 * calcluate the minimum number of blocks the given fs can be resized to
2370 blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
2372 ext2_ino_t inode_count;
2374 blk64_t blks_needed, data_blocks;
2375 blk64_t grp, data_needed, last_start;
2376 blk64_t overhead = 0;
2377 int old_desc_blocks;
2378 int extra_groups = 0;
2379 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2382 * first figure out how many group descriptors we need to
2383 * handle the number of inodes we have
2385 inode_count = fs->super->s_inodes_count -
2386 fs->super->s_free_inodes_count;
2387 blks_needed = ext2fs_div_ceil(inode_count,
2388 fs->super->s_inodes_per_group) *
2389 EXT2_BLOCKS_PER_GROUP(fs->super);
2390 groups = ext2fs_div64_ceil(blks_needed,
2391 EXT2_BLOCKS_PER_GROUP(fs->super));
2392 #ifdef RESIZE2FS_DEBUG
2393 if (flags & RESIZE_DEBUG_MIN_CALC)
2394 printf("fs has %d inodes, %d groups required.\n",
2395 inode_count, groups);
2399 * number of old-style block group descriptor blocks
2401 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2402 old_desc_blocks = fs->super->s_first_meta_bg;
2404 old_desc_blocks = fs->desc_blocks +
2405 fs->super->s_reserved_gdt_blocks;
2407 /* calculate how many blocks are needed for data */
2408 data_needed = ext2fs_blocks_count(fs->super) -
2409 ext2fs_free_blocks_count(fs->super);
2411 for (grp = 0; grp < fs->group_desc_count; grp++)
2412 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
2413 #ifdef RESIZE2FS_DEBUG
2414 if (flags & RESIZE_DEBUG_MIN_CALC)
2415 printf("fs requires %llu data blocks.\n", data_needed);
2419 * For ext4 we need to allow for up to a flex_bg worth of
2420 * inode tables of slack space so the resize operation can be
2421 * guaranteed to finish.
2423 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2424 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
2425 data_needed += fs->inode_blocks_per_group * extra_groups;
2426 extra_groups = groups % flexbg_size;
2430 * figure out how many data blocks we have given the number of groups
2431 * we need for our inodes
2433 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
2435 for (grp = 0; grp < groups; grp++) {
2436 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
2439 * we want to keep track of how much data we can store in
2440 * the groups leading up to the last group so we can determine
2441 * how big the last group needs to be
2443 if (grp != (groups - 1))
2444 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
2447 data_blocks -= overhead;
2449 #ifdef RESIZE2FS_DEBUG
2450 if (flags & RESIZE_DEBUG_MIN_CALC)
2451 printf("With %d group(s), we have %llu blocks available.\n",
2452 groups, data_blocks);
2456 * if we need more group descriptors in order to accomodate our data
2457 * then we need to add them here
2459 while (data_needed > data_blocks) {
2460 blk64_t remainder = data_needed - data_blocks;
2463 /* figure out how many more groups we need for the data */
2464 extra_grps = ext2fs_div64_ceil(remainder,
2465 EXT2_BLOCKS_PER_GROUP(fs->super));
2467 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
2469 /* ok we have to account for the last group */
2470 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2471 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
2473 for (grp = groups; grp < groups+extra_grps; grp++) {
2474 overhead = calc_group_overhead(fs, grp,
2478 * again, we need to see how much data we cram into
2479 * all of the groups leading up to the last group
2481 if (grp != (groups + extra_grps - 1))
2482 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
2485 data_blocks -= overhead;
2488 groups += extra_grps;
2489 extra_groups += extra_grps;
2490 if (fs->super->s_feature_incompat
2491 & EXT4_FEATURE_INCOMPAT_FLEX_BG
2492 && extra_groups > flexbg_size) {
2494 * For ext4 we need to allow for up to a flex_bg worth
2495 * of inode tables of slack space so the resize
2496 * operation can be guaranteed to finish.
2498 extra_groups = flexbg_size -
2499 (groups & (flexbg_size - 1));
2500 data_needed += (fs->inode_blocks_per_group *
2502 extra_groups = groups % flexbg_size;
2504 #ifdef RESIZE2FS_DEBUG
2505 if (flags & RESIZE_DEBUG_MIN_CALC)
2506 printf("Added %d extra group(s), "
2507 "data_needed %llu, data_blocks %llu, "
2508 "last_start %llu\n",
2509 extra_grps, data_needed, data_blocks,
2514 /* now for the fun voodoo */
2515 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2516 #ifdef RESIZE2FS_DEBUG
2517 if (flags & RESIZE_DEBUG_MIN_CALC)
2518 printf("Last group's overhead is %llu\n", overhead);
2522 * if this is the case then the last group is going to have data in it
2523 * so we need to adjust the size of the last group accordingly
2525 if (last_start < data_needed) {
2526 blk64_t remainder = data_needed - last_start;
2528 #ifdef RESIZE2FS_DEBUG
2529 if (flags & RESIZE_DEBUG_MIN_CALC)
2530 printf("Need %llu data blocks in last group\n",
2534 * 50 is a magic number that mkfs/resize uses to see if its
2535 * even worth making/resizing the fs. basically you need to
2536 * have at least 50 blocks in addition to the blocks needed
2537 * for the metadata in the last group
2540 overhead += remainder;
2546 overhead += fs->super->s_first_data_block;
2547 #ifdef RESIZE2FS_DEBUG
2548 if (flags & RESIZE_DEBUG_MIN_CALC)
2549 printf("Final size of last group is %lld\n", overhead);
2553 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2554 * only do groups-1, and then add the number of blocks needed to
2555 * handle the group descriptor metadata+data that we need
2557 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2558 blks_needed += overhead;
2561 * Make sure blks_needed covers the end of the inode table in
2562 * the last block group.
2564 overhead = ext2fs_inode_table_loc(fs, groups-1) +
2565 fs->inode_blocks_per_group;
2566 if (blks_needed < overhead)
2567 blks_needed = overhead;
2569 #ifdef RESIZE2FS_DEBUG
2570 if (flags & RESIZE_DEBUG_MIN_CALC)
2571 printf("Estimated blocks needed: %llu\n", blks_needed);
2575 * If at this point we've already added up more "needed" than
2576 * the current size, just return current size as minimum.
2578 if (blks_needed >= ext2fs_blocks_count(fs->super))
2579 return ext2fs_blocks_count(fs->super);
2581 * We need to reserve a few extra blocks if extents are
2582 * enabled, in case we need to grow the extent tree. The more
2583 * we shrink the file system, the more space we need.
2585 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
2586 blk64_t safe_margin = (ext2fs_blocks_count(fs->super) -
2588 #ifdef RESIZE2FS_DEBUG
2589 if (flags & RESIZE_DEBUG_MIN_CALC)
2590 printf("Extents safety margin: %llu\n", safe_margin);
2592 blks_needed += safe_margin;