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 (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
244 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
247 for (g=0; g < fs->group_desc_count; g++) {
248 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
251 blk = ext2fs_group_first_block2(fs, g);
252 lblk = ext2fs_group_last_block2(fs, g);
253 ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
256 ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
257 ext2fs_mark_block_bitmap2(fs->block_map,
258 ext2fs_block_bitmap_loc(fs, g));
259 ext2fs_mark_block_bitmap2(fs->block_map,
260 ext2fs_inode_bitmap_loc(fs, g));
261 for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
262 i < (unsigned int) fs->inode_blocks_per_group;
264 ext2fs_mark_block_bitmap2(fs->block_map, blk);
268 /* --------------------------------------------------------------------
270 * Resize processing, phase 1.
272 * In this phase we adjust the in-memory superblock information, and
273 * initialize any new parts of the inode table. The new parts of the
274 * inode table are created in virgin disk space, so we can abort here
275 * without any side effects.
276 * --------------------------------------------------------------------
280 * If the group descriptor's bitmap and inode table blocks are valid,
281 * release them in the new filesystem data structure, and mark them as
282 * reserved so the old inode table blocks don't get overwritten.
284 static void free_gdp_blocks(ext2_filsys fs,
285 ext2fs_block_bitmap reserve_blocks,
292 blk = ext2fs_block_bitmap_loc(old_fs, group);
294 (blk < ext2fs_blocks_count(fs->super))) {
295 ext2fs_block_alloc_stats2(fs, blk, -1);
296 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
299 blk = ext2fs_inode_bitmap_loc(old_fs, group);
301 (blk < ext2fs_blocks_count(fs->super))) {
302 ext2fs_block_alloc_stats2(fs, blk, -1);
303 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
306 blk = ext2fs_inode_table_loc(old_fs, group);
308 (blk >= ext2fs_blocks_count(fs->super)))
312 j < fs->inode_blocks_per_group; j++, blk++) {
313 if (blk >= ext2fs_blocks_count(fs->super))
315 ext2fs_block_alloc_stats2(fs, blk, -1);
316 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
321 * This routine is shared by the online and offline resize routines.
322 * All of the information which is adjusted in memory is done here.
324 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
325 ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
328 blk64_t overhead = 0;
330 blk64_t blk, group_block;
332 blk64_t old_numblocks, numblocks, adjblocks;
333 unsigned long i, j, old_desc_blocks;
334 unsigned int meta_bg, meta_bg_size;
335 int has_super, csum_flag;
336 unsigned long long new_inodes; /* u64 to check for overflow */
339 ext2fs_blocks_count_set(fs->super, new_size);
342 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
343 fs->super->s_first_data_block,
344 EXT2_BLOCKS_PER_GROUP(fs->super));
345 if (fs->group_desc_count == 0)
346 return EXT2_ET_TOOSMALL;
347 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
348 EXT2_DESC_PER_BLOCK(fs->super));
351 * Overhead is the number of bookkeeping blocks per group. It
352 * includes the superblock backup, the group descriptor
353 * backups, the inode bitmap, the block bitmap, and the inode
356 overhead = (int) (2 + fs->inode_blocks_per_group);
358 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
359 overhead += 1 + fs->desc_blocks +
360 fs->super->s_reserved_gdt_blocks;
363 * See if the last group is big enough to support the
364 * necessary data structures. If not, we need to get rid of
367 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
368 fs->super->s_blocks_per_group;
369 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
370 return EXT2_ET_TOOSMALL;
371 if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
372 ext2fs_blocks_count_set(fs->super,
373 ext2fs_blocks_count(fs->super) - rem);
377 * Adjust the number of inodes
379 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
380 if (new_inodes > ~0U) {
381 fprintf(stderr, _("inodes (%llu) must be less than %u"),
383 return EXT2_ET_TOO_MANY_INODES;
385 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
386 fs->group_desc_count;
389 * Adjust the number of free blocks
391 blk = ext2fs_blocks_count(old_fs->super);
392 if (blk > ext2fs_blocks_count(fs->super))
393 ext2fs_free_blocks_count_set(fs->super,
394 ext2fs_free_blocks_count(fs->super) -
395 (blk - ext2fs_blocks_count(fs->super)));
397 ext2fs_free_blocks_count_set(fs->super,
398 ext2fs_free_blocks_count(fs->super) +
399 (ext2fs_blocks_count(fs->super) - blk));
402 * Adjust the number of reserved blocks
404 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
405 ext2fs_blocks_count(old_fs->super);
406 ext2fs_r_blocks_count_set(fs->super,
407 (percent * ext2fs_blocks_count(fs->super) /
411 * Adjust the bitmaps for size
413 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
414 fs->super->s_inodes_count,
416 if (retval) goto errout;
418 real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) *
419 fs->group_desc_count)) - 1 +
420 fs->super->s_first_data_block;
421 retval = ext2fs_resize_block_bitmap2(new_size - 1,
422 real_end, fs->block_map);
423 if (retval) goto errout;
426 * If we are growing the file system, also grow the size of
427 * the reserve_blocks bitmap
429 if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) {
430 retval = ext2fs_resize_block_bitmap2(new_size - 1,
431 real_end, reserve_blocks);
432 if (retval) goto errout;
436 * Reallocate the group descriptors as necessary.
438 if (old_fs->desc_blocks != fs->desc_blocks) {
439 retval = ext2fs_resize_mem(old_fs->desc_blocks *
441 fs->desc_blocks * fs->blocksize,
445 if (fs->desc_blocks > old_fs->desc_blocks)
446 memset((char *) fs->group_desc +
447 (old_fs->desc_blocks * fs->blocksize), 0,
448 (fs->desc_blocks - old_fs->desc_blocks) *
453 * If the resize_inode feature is set, and we are changing the
454 * number of descriptor blocks, then adjust
455 * s_reserved_gdt_blocks if possible to avoid needing to move
456 * the inode table either now or in the future.
458 if ((fs->super->s_feature_compat &
459 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
460 (old_fs->desc_blocks != fs->desc_blocks)) {
463 new = ((int) fs->super->s_reserved_gdt_blocks) +
464 (old_fs->desc_blocks - fs->desc_blocks);
467 if (new > (int) fs->blocksize/4)
468 new = fs->blocksize/4;
469 fs->super->s_reserved_gdt_blocks = new;
473 * Update the location of the backup superblocks if the
474 * sparse_super2 feature is enabled.
476 if (fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) {
477 dgrp_t last_bg = fs->group_desc_count - 1;
478 dgrp_t old_last_bg = old_fs->group_desc_count - 1;
480 if (last_bg > old_last_bg) {
481 if (old_fs->group_desc_count == 1)
482 fs->super->s_backup_bgs[0] = 1;
483 if (old_fs->group_desc_count == 1 &&
484 fs->super->s_backup_bgs[0])
485 fs->super->s_backup_bgs[0] = last_bg;
486 else if (fs->super->s_backup_bgs[1])
487 fs->super->s_backup_bgs[1] = last_bg;
488 } else if (last_bg < old_last_bg) {
489 if (fs->super->s_backup_bgs[0] > last_bg)
490 fs->super->s_backup_bgs[0] = 0;
491 if (fs->super->s_backup_bgs[1] > last_bg)
492 fs->super->s_backup_bgs[1] = 0;
494 old_fs->super->s_backup_bgs[1] == old_last_bg)
495 fs->super->s_backup_bgs[1] = last_bg;
500 * If we are shrinking the number of block groups, we're done
503 if (old_fs->group_desc_count > fs->group_desc_count) {
505 * Check the block groups that we are chopping off
506 * and free any blocks associated with their metadata
508 for (i = fs->group_desc_count;
509 i < old_fs->group_desc_count; i++)
510 free_gdp_blocks(fs, reserve_blocks, old_fs, i);
516 * Fix the count of the last (old) block group
518 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
519 old_fs->super->s_first_data_block) %
520 old_fs->super->s_blocks_per_group;
522 old_numblocks = old_fs->super->s_blocks_per_group;
523 if (old_fs->group_desc_count == fs->group_desc_count) {
524 numblocks = (ext2fs_blocks_count(fs->super) -
525 fs->super->s_first_data_block) %
526 fs->super->s_blocks_per_group;
528 numblocks = fs->super->s_blocks_per_group;
530 numblocks = fs->super->s_blocks_per_group;
531 i = old_fs->group_desc_count - 1;
532 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
533 ext2fs_group_desc_csum_set(fs, i);
536 * If the number of block groups is staying the same, we're
537 * done and can exit now. (If the number block groups is
538 * shrinking, we had exited earlier.)
540 if (old_fs->group_desc_count >= fs->group_desc_count) {
546 * Initialize the new block group descriptors
548 group_block = ext2fs_group_first_block2(fs,
549 old_fs->group_desc_count);
550 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
551 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
552 if (access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0)
553 lazy_itable_init = 1;
554 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
555 old_desc_blocks = fs->super->s_first_meta_bg;
557 old_desc_blocks = fs->desc_blocks +
558 fs->super->s_reserved_gdt_blocks;
561 * If we changed the number of block_group descriptor blocks,
562 * we need to make sure they are all marked as reserved in the
563 * file systems's block allocation map.
565 for (i = 0; i < old_fs->group_desc_count; i++)
566 ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
568 for (i = old_fs->group_desc_count;
569 i < fs->group_desc_count; i++) {
570 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
571 sizeof(struct ext2_group_desc));
574 ext2fs_bg_flags_zap(fs, i);
576 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
577 if (!lazy_itable_init)
578 ext2fs_bg_flags_set(fs, i,
579 EXT2_BG_INODE_ZEROED);
580 ext2fs_bg_itable_unused_set(fs, i,
581 fs->super->s_inodes_per_group);
584 numblocks = ext2fs_group_blocks_count(fs, i);
585 if ((i < fs->group_desc_count - 1) && csum_flag)
586 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
588 has_super = ext2fs_bg_has_super(fs, i);
590 ext2fs_block_alloc_stats2(fs, group_block, +1);
593 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
594 meta_bg = i / meta_bg_size;
595 if (!(fs->super->s_feature_incompat &
596 EXT2_FEATURE_INCOMPAT_META_BG) ||
597 (meta_bg < fs->super->s_first_meta_bg)) {
599 for (j=0; j < old_desc_blocks; j++)
600 ext2fs_block_alloc_stats2(fs,
601 group_block + 1 + j, +1);
602 adjblocks += old_desc_blocks;
607 if (((i % meta_bg_size) == 0) ||
608 ((i % meta_bg_size) == 1) ||
609 ((i % meta_bg_size) == (meta_bg_size-1)))
610 ext2fs_block_alloc_stats2(fs,
611 group_block + has_super, +1);
614 adjblocks += 2 + fs->inode_blocks_per_group;
616 numblocks -= adjblocks;
617 ext2fs_free_blocks_count_set(fs->super,
618 ext2fs_free_blocks_count(fs->super) - adjblocks);
619 fs->super->s_free_inodes_count +=
620 fs->super->s_inodes_per_group;
621 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
622 ext2fs_bg_free_inodes_count_set(fs, i,
623 fs->super->s_inodes_per_group);
624 ext2fs_bg_used_dirs_count_set(fs, i, 0);
625 ext2fs_group_desc_csum_set(fs, i);
627 retval = ext2fs_allocate_group_table(fs, i, 0);
628 if (retval) goto errout;
630 group_block += fs->super->s_blocks_per_group;
635 * Mark all of the metadata blocks as reserved so they won't
636 * get allocated by the call to ext2fs_allocate_group_table()
637 * in blocks_to_move(), where we allocate new blocks to
638 * replace those allocation bitmap and inode table blocks
639 * which have to get relocated to make space for an increased
640 * number of the block group descriptors.
643 mark_table_blocks(fs, reserve_blocks);
650 * This routine adjusts the superblock and other data structures, both
651 * in disk as well as in memory...
653 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
655 ext2_filsys fs = rfs->new_fs;
660 unsigned long max_group;
662 ext2fs_mark_super_dirty(fs);
663 ext2fs_mark_bb_dirty(fs);
664 ext2fs_mark_ib_dirty(fs);
666 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
667 &rfs->reserve_blocks);
671 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
676 * Check to make sure there are enough inodes
678 if ((rfs->old_fs->super->s_inodes_count -
679 rfs->old_fs->super->s_free_inodes_count) >
680 rfs->new_fs->super->s_inodes_count) {
686 * If we are shrinking the number block groups, we're done and
689 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
695 * If the number of block groups is staying the same, we're
696 * done and can exit now. (If the number block groups is
697 * shrinking, we had exited earlier.)
699 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
705 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
706 * supports lazy inode initialization, we can skip
707 * initializing the inode table.
709 if (lazy_itable_init &&
710 EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
711 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
717 * Initialize the inode table
719 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
724 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
725 group_block = ext2fs_group_first_block2(fs,
726 rfs->old_fs->group_desc_count);
727 adj = rfs->old_fs->group_desc_count;
728 max_group = fs->group_desc_count - adj;
730 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
735 for (i = rfs->old_fs->group_desc_count;
736 i < fs->group_desc_count; i++) {
738 * Write out the new inode table
740 retval = io_channel_write_blk64(fs->io,
741 ext2fs_inode_table_loc(fs, i),
742 fs->inode_blocks_per_group,
744 if (retval) goto errout;
746 io_channel_flush(fs->io);
748 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
749 i - adj + 1, max_group);
753 group_block += fs->super->s_blocks_per_group;
755 io_channel_flush(fs->io);
762 /* --------------------------------------------------------------------
764 * Resize processing, phase 2.
766 * In this phase we adjust determine which blocks need to be moved, in
767 * blocks_to_move(). We then copy the blocks to their ultimate new
768 * destinations using block_mover(). Since we are copying blocks to
769 * their new locations, again during this pass we can abort without
771 * --------------------------------------------------------------------
775 * This helper function creates a block bitmap with all of the
776 * filesystem meta-data blocks.
778 static errcode_t mark_table_blocks(ext2_filsys fs,
779 ext2fs_block_bitmap bmap)
784 for (i = 0; i < fs->group_desc_count; i++) {
785 ext2fs_reserve_super_and_bgd(fs, i, bmap);
788 * Mark the blocks used for the inode table
790 blk = ext2fs_inode_table_loc(fs, i);
792 ext2fs_mark_block_bitmap_range2(bmap, blk,
793 fs->inode_blocks_per_group);
796 * Mark block used for the block bitmap
798 blk = ext2fs_block_bitmap_loc(fs, i);
800 ext2fs_mark_block_bitmap2(bmap, blk);
803 * Mark block used for the inode bitmap
805 blk = ext2fs_inode_bitmap_loc(fs, i);
807 ext2fs_mark_block_bitmap2(bmap, blk);
813 * This function checks to see if a particular block (either a
814 * superblock or a block group descriptor) overlaps with an inode or
815 * block bitmap block, or with the inode table.
817 static void mark_fs_metablock(ext2_resize_t rfs,
818 ext2fs_block_bitmap meta_bmap,
819 int group, blk64_t blk)
821 ext2_filsys fs = rfs->new_fs;
823 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
824 ext2fs_block_alloc_stats2(fs, blk, +1);
827 * Check to see if we overlap with the inode or block bitmap,
828 * or the inode tables. If not, and the block is in use, then
829 * mark it as a block to be moved.
831 if (IS_BLOCK_BM(fs, group, blk)) {
832 ext2fs_block_bitmap_loc_set(fs, group, 0);
833 rfs->needed_blocks++;
836 if (IS_INODE_BM(fs, group, blk)) {
837 ext2fs_inode_bitmap_loc_set(fs, group, 0);
838 rfs->needed_blocks++;
841 if (IS_INODE_TB(fs, group, blk)) {
842 ext2fs_inode_table_loc_set(fs, group, 0);
843 rfs->needed_blocks++;
846 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
849 for (i=0; i < rfs->old_fs->group_desc_count; i++) {
850 if (IS_BLOCK_BM(fs, i, blk)) {
851 ext2fs_block_bitmap_loc_set(fs, i, 0);
852 rfs->needed_blocks++;
855 if (IS_INODE_BM(fs, i, blk)) {
856 ext2fs_inode_bitmap_loc_set(fs, i, 0);
857 rfs->needed_blocks++;
860 if (IS_INODE_TB(fs, i, blk)) {
861 ext2fs_inode_table_loc_set(fs, i, 0);
862 rfs->needed_blocks++;
867 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
868 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
869 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
871 * If the block bitmap is uninitialized, which means
872 * nothing other than standard metadata in use.
875 } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
876 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
877 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
878 rfs->needed_blocks++;
884 * This routine marks and unmarks reserved blocks in the new block
885 * bitmap. It also determines which blocks need to be moved and
886 * places this information into the move_blocks bitmap.
888 static errcode_t blocks_to_move(ext2_resize_t rfs)
891 dgrp_t i, max_groups, g;
892 blk64_t blk, group_blk;
893 blk64_t old_blocks, new_blocks;
895 unsigned int meta_bg, meta_bg_size;
897 ext2_filsys fs, old_fs;
898 ext2fs_block_bitmap meta_bmap;
902 old_fs = rfs->old_fs;
903 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
906 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
911 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
916 retval = mark_table_blocks(old_fs, meta_bmap);
923 * If we're shrinking the filesystem, we need to move any
924 * group's metadata blocks (either allocation bitmaps or the
925 * inode table) which are beyond the end of the new
928 new_size = ext2fs_blocks_count(fs->super);
929 if (new_size < ext2fs_blocks_count(old_fs->super)) {
930 for (g = 0; g < fs->group_desc_count; g++) {
933 * ext2fs_allocate_group_table will re-allocate any
934 * metadata blocks whose location is set to zero.
936 if (ext2fs_block_bitmap_loc(fs, g) >= new_size) {
937 ext2fs_block_bitmap_loc_set(fs, g, 0);
940 if (ext2fs_inode_bitmap_loc(fs, g) >= new_size) {
941 ext2fs_inode_bitmap_loc_set(fs, g, 0);
944 if ((ext2fs_inode_table_loc(fs, g) +
945 fs->inode_blocks_per_group) > new_size) {
946 ext2fs_inode_table_loc_set(fs, g, 0);
951 retval = ext2fs_allocate_group_table(fs, g, 0);
959 * If we're shrinking the filesystem, we need to move all of
960 * the blocks that don't fit any more
962 for (blk = ext2fs_blocks_count(fs->super);
963 blk < ext2fs_blocks_count(old_fs->super); blk++) {
964 g = ext2fs_group_of_blk2(fs, blk);
965 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
966 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
967 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
969 * The block bitmap is uninitialized, so skip
970 * to the next block group.
972 blk = ext2fs_group_first_block2(fs, g+1) - 1;
975 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
976 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
977 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
978 rfs->needed_blocks++;
980 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
983 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
984 old_blocks = old_fs->super->s_first_meta_bg;
985 new_blocks = fs->super->s_first_meta_bg;
987 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
988 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
991 retval = reserve_sparse_super2_last_group(rfs, meta_bmap);
995 if (old_blocks == new_blocks) {
1000 max_groups = fs->group_desc_count;
1001 if (max_groups > old_fs->group_desc_count)
1002 max_groups = old_fs->group_desc_count;
1003 group_blk = old_fs->super->s_first_data_block;
1005 * If we're reducing the number of descriptor blocks, this
1006 * makes life easy. :-) We just have to mark some extra
1009 if (old_blocks > new_blocks) {
1010 for (i = 0; i < max_groups; i++) {
1011 if (!ext2fs_bg_has_super(fs, i)) {
1012 group_blk += fs->super->s_blocks_per_group;
1015 for (blk = group_blk+1+new_blocks;
1016 blk < group_blk+1+old_blocks; blk++) {
1017 ext2fs_block_alloc_stats2(fs, blk, -1);
1018 rfs->needed_blocks--;
1020 group_blk += fs->super->s_blocks_per_group;
1026 * If we're increasing the number of descriptor blocks, life
1027 * gets interesting....
1029 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
1030 flex_bg = fs->super->s_feature_incompat &
1031 EXT4_FEATURE_INCOMPAT_FLEX_BG;
1032 /* first reserve all of the existing fs meta blocks */
1033 for (i = 0; i < max_groups; i++) {
1034 has_super = ext2fs_bg_has_super(fs, i);
1036 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
1038 meta_bg = i / meta_bg_size;
1039 if (!(fs->super->s_feature_incompat &
1040 EXT2_FEATURE_INCOMPAT_META_BG) ||
1041 (meta_bg < fs->super->s_first_meta_bg)) {
1043 for (blk = group_blk+1;
1044 blk < group_blk + 1 + new_blocks; blk++)
1045 mark_fs_metablock(rfs, meta_bmap,
1051 if (((i % meta_bg_size) == 0) ||
1052 ((i % meta_bg_size) == 1) ||
1053 ((i % meta_bg_size) == (meta_bg_size-1)))
1054 mark_fs_metablock(rfs, meta_bmap, i,
1055 group_blk + has_super);
1059 * Reserve the existing meta blocks that we know
1060 * aren't to be moved.
1062 * For flex_bg file systems, in order to avoid
1063 * overwriting fs metadata (especially inode table
1064 * blocks) belonging to a different block group when
1065 * we are relocating the inode tables, we need to
1066 * reserve all existing fs metadata blocks.
1068 if (ext2fs_block_bitmap_loc(fs, i))
1069 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1070 ext2fs_block_bitmap_loc(fs, i));
1071 else if (flex_bg && i < old_fs->group_desc_count)
1072 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1073 ext2fs_block_bitmap_loc(old_fs, i));
1075 if (ext2fs_inode_bitmap_loc(fs, i))
1076 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1077 ext2fs_inode_bitmap_loc(fs, i));
1078 else if (flex_bg && i < old_fs->group_desc_count)
1079 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1080 ext2fs_inode_bitmap_loc(old_fs, i));
1082 if (ext2fs_inode_table_loc(fs, i))
1083 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1084 ext2fs_inode_table_loc(fs, i),
1085 fs->inode_blocks_per_group);
1086 else if (flex_bg && i < old_fs->group_desc_count)
1087 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1088 ext2fs_inode_table_loc(old_fs, i),
1089 old_fs->inode_blocks_per_group);
1091 group_blk += rfs->new_fs->super->s_blocks_per_group;
1094 /* Allocate the missing data structures */
1095 for (i = 0; i < max_groups; i++) {
1096 if (ext2fs_inode_table_loc(fs, i) &&
1097 ext2fs_inode_bitmap_loc(fs, i) &&
1098 ext2fs_block_bitmap_loc(fs, i))
1101 retval = ext2fs_allocate_group_table(fs, i,
1102 rfs->reserve_blocks);
1107 * For those structures that have changed, we need to
1110 if (ext2fs_block_bitmap_loc(old_fs, i) !=
1111 (blk = ext2fs_block_bitmap_loc(fs, i))) {
1112 ext2fs_block_alloc_stats2(fs, blk, +1);
1113 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1114 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1115 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1118 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
1119 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
1120 ext2fs_block_alloc_stats2(fs, blk, +1);
1121 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1122 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1123 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1128 * The inode table, if we need to relocate it, is
1129 * handled specially. We have to reserve the blocks
1130 * for both the old and the new inode table, since we
1131 * can't have the inode table be destroyed during the
1132 * block relocation phase.
1134 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
1135 continue; /* inode table not moved */
1137 rfs->needed_blocks += fs->inode_blocks_per_group;
1140 * Mark the new inode table as in use in the new block
1141 * allocation bitmap, and move any blocks that might
1144 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
1145 j < fs->inode_blocks_per_group ; j++, blk++) {
1146 ext2fs_block_alloc_stats2(fs, blk, +1);
1147 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1148 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1149 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1154 * Make sure the old inode table is reserved in the
1155 * block reservation bitmap.
1157 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1158 j < fs->inode_blocks_per_group ; j++, blk++)
1159 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1165 ext2fs_free_block_bitmap(meta_bmap);
1171 * This helper function tries to allocate a new block. We try to
1172 * avoid hitting the original group descriptor blocks at least at
1173 * first, since we want to make it possible to recover from a badly
1174 * aborted resize operation as much as possible.
1176 * In the future, I may further modify this routine to balance out
1177 * where we get the new blocks across the various block groups.
1178 * Ideally we would allocate blocks that corresponded with the block
1179 * group of the containing inode, and keep contiguous blocks
1180 * together. However, this very difficult to do efficiently, since we
1181 * don't have the necessary information up front.
1185 #define DESPERATION 2
1187 static void init_block_alloc(ext2_resize_t rfs)
1189 rfs->alloc_state = AVOID_OLD;
1190 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1192 /* HACK for testing */
1193 if (ext2fs_blocks_count(rfs->new_fs->super) >
1194 ext2fs_blocks_count(rfs->old_fs->super))
1195 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1199 static blk64_t get_new_block(ext2_resize_t rfs)
1201 ext2_filsys fs = rfs->new_fs;
1204 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1205 if (rfs->alloc_state == DESPERATION)
1208 #ifdef RESIZE2FS_DEBUG
1209 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1210 printf("Going into desperation mode "
1211 "for block allocations\n");
1213 rfs->alloc_state = DESPERATION;
1214 rfs->new_blk = fs->super->s_first_data_block;
1217 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1218 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1220 ((rfs->alloc_state == AVOID_OLD) &&
1221 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1222 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1227 return rfs->new_blk;
1231 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1234 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1237 blk = get_new_block(rfs);
1241 #ifdef RESIZE2FS_DEBUG
1242 if (rfs->flags & 0xF)
1243 printf("get_alloc_block allocating %llu\n", blk);
1246 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1247 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1248 *ret = (blk64_t) blk;
1252 static errcode_t block_mover(ext2_resize_t rfs)
1254 blk64_t blk, old_blk, new_blk;
1255 ext2_filsys fs = rfs->new_fs;
1256 ext2_filsys old_fs = rfs->old_fs;
1261 ext2_badblocks_list badblock_list = 0;
1262 int bb_modified = 0;
1264 fs->get_alloc_block = resize2fs_get_alloc_block;
1265 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1267 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1271 new_blk = fs->super->s_first_data_block;
1272 if (!rfs->itable_buf) {
1273 retval = ext2fs_get_array(fs->blocksize,
1274 fs->inode_blocks_per_group,
1279 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1284 * The first step is to figure out where all of the blocks
1287 to_move = moved = 0;
1288 init_block_alloc(rfs);
1289 for (blk = B2C(old_fs->super->s_first_data_block);
1290 blk < ext2fs_blocks_count(old_fs->super);
1291 blk += EXT2FS_CLUSTER_RATIO(fs)) {
1292 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1294 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1296 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1297 ext2fs_badblocks_list_del(badblock_list, blk);
1302 new_blk = get_new_block(rfs);
1307 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1308 ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk));
1314 ext2fs_free_extent_table(rfs->bmap);
1322 * Step two is to actually move the blocks
1324 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1325 if (retval) goto errout;
1327 if (rfs->progress) {
1328 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1334 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1335 if (retval) goto errout;
1338 old_blk = C2B(old_blk);
1339 new_blk = C2B(new_blk);
1341 #ifdef RESIZE2FS_DEBUG
1342 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1343 printf("Moving %llu blocks %llu->%llu\n",
1344 size, old_blk, new_blk);
1348 if (c > fs->inode_blocks_per_group)
1349 c = fs->inode_blocks_per_group;
1350 retval = io_channel_read_blk64(fs->io, old_blk, c,
1352 if (retval) goto errout;
1353 retval = io_channel_write_blk64(fs->io, new_blk, c,
1355 if (retval) goto errout;
1360 if (rfs->progress) {
1361 io_channel_flush(fs->io);
1362 retval = (rfs->progress)(rfs,
1363 E2_RSZ_BLOCK_RELOC_PASS,
1369 io_channel_flush(fs->io);
1373 if (badblock_list) {
1374 if (!retval && bb_modified)
1375 retval = ext2fs_update_bb_inode(old_fs,
1377 ext2fs_badblocks_list_free(badblock_list);
1383 /* --------------------------------------------------------------------
1385 * Resize processing, phase 3
1387 * --------------------------------------------------------------------
1392 * The extent translation table is stored in clusters so we need to
1393 * take special care when mapping a source block number to its
1394 * destination block number.
1396 static __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc)
1398 __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc)));
1401 new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1);
1405 struct process_block_struct {
1408 struct ext2_inode * inode;
1414 static int process_block(ext2_filsys fs, blk64_t *block_nr,
1415 e2_blkcnt_t blockcnt,
1416 blk64_t ref_block EXT2FS_ATTR((unused)),
1417 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1419 struct process_block_struct *pb;
1421 blk64_t block, new_block;
1424 pb = (struct process_block_struct *) priv_data;
1426 if (pb->rfs->bmap) {
1427 new_block = extent_translate(fs, pb->rfs->bmap, block);
1429 *block_nr = new_block;
1430 ret |= BLOCK_CHANGED;
1432 #ifdef RESIZE2FS_DEBUG
1433 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1434 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1435 pb->ino, blockcnt, block, new_block);
1441 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1442 block, (int) blockcnt);
1454 static errcode_t progress_callback(ext2_filsys fs,
1455 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1456 dgrp_t group, void * priv_data)
1458 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1462 * This check is to protect against old ext2 libraries. It
1463 * shouldn't be needed against new libraries.
1468 if (rfs->progress) {
1469 io_channel_flush(fs->io);
1470 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1471 group+1, fs->group_desc_count);
1479 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1481 struct process_block_struct pb;
1482 ext2_ino_t ino, new_inode;
1483 struct ext2_inode *inode = NULL;
1484 ext2_inode_scan scan = NULL;
1486 char *block_buf = 0;
1487 ext2_ino_t start_to_move;
1492 if ((rfs->old_fs->group_desc_count <=
1493 rfs->new_fs->group_desc_count) &&
1498 * Save the original size of the old filesystem, and
1499 * temporarily set the size to be the new size if the new size
1500 * is larger. We need to do this to avoid catching an error
1501 * by the block iterator routines
1503 orig_size = ext2fs_blocks_count(rfs->old_fs->super);
1504 if (orig_size < ext2fs_blocks_count(rfs->new_fs->super))
1505 ext2fs_blocks_count_set(rfs->old_fs->super,
1506 ext2fs_blocks_count(rfs->new_fs->super));
1508 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1509 if (retval) goto errout;
1511 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1512 if (retval) goto errout;
1513 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1514 if (retval) goto errout;
1516 start_to_move = (rfs->new_fs->group_desc_count *
1517 rfs->new_fs->super->s_inodes_per_group);
1519 if (rfs->progress) {
1520 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1521 0, rfs->old_fs->group_desc_count);
1525 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1529 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1530 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1531 inode = malloc(inode_size);
1537 * First, copy all of the inodes that need to be moved
1538 * elsewhere in the inode table
1541 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1542 if (retval) goto errout;
1546 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1547 continue; /* inode not in use */
1549 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1552 if (ext2fs_file_acl_block(rfs->old_fs, inode) && rfs->bmap) {
1553 new_block = extent_translate(rfs->old_fs, rfs->bmap,
1554 ext2fs_file_acl_block(rfs->old_fs, inode));
1556 ext2fs_file_acl_block_set(rfs->old_fs, inode,
1558 retval = ext2fs_write_inode_full(rfs->old_fs,
1559 ino, inode, inode_size);
1560 if (retval) goto errout;
1564 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
1565 (rfs->bmap || pb.is_dir)) {
1567 retval = ext2fs_block_iterate3(rfs->old_fs,
1569 process_block, &pb);
1578 if (ino <= start_to_move)
1579 continue; /* Don't need to move it. */
1584 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1588 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1591 /* Get the new version of the inode */
1592 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1594 if (retval) goto errout;
1596 inode->i_ctime = time(0);
1597 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1599 if (retval) goto errout;
1601 #ifdef RESIZE2FS_DEBUG
1602 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1603 printf("Inode moved %u->%u\n", ino, new_inode);
1606 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1610 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1612 io_channel_flush(rfs->old_fs->io);
1615 ext2fs_blocks_count_set(rfs->old_fs->super, orig_size);
1617 ext2fs_free_extent_table(rfs->bmap);
1621 ext2fs_close_inode_scan(scan);
1623 ext2fs_free_mem(&block_buf);
1628 /* --------------------------------------------------------------------
1630 * Resize processing, phase 4.
1632 * --------------------------------------------------------------------
1638 unsigned int max_dirs;
1642 static int check_and_change_inodes(ext2_ino_t dir,
1643 int entry EXT2FS_ATTR((unused)),
1644 struct ext2_dir_entry *dirent, int offset,
1645 int blocksize EXT2FS_ATTR((unused)),
1646 char *buf EXT2FS_ATTR((unused)),
1649 struct istruct *is = (struct istruct *) priv_data;
1650 struct ext2_inode inode;
1651 ext2_ino_t new_inode;
1654 if (is->rfs->progress && offset == 0) {
1655 io_channel_flush(is->rfs->old_fs->io);
1656 is->err = (is->rfs->progress)(is->rfs,
1657 E2_RSZ_INODE_REF_UPD_PASS,
1658 ++is->num, is->max_dirs);
1660 return DIRENT_ABORT;
1666 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1670 #ifdef RESIZE2FS_DEBUG
1671 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1672 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1673 dir, dirent->name_len&0xFF, dirent->name,
1674 dirent->inode, new_inode);
1677 dirent->inode = new_inode;
1679 /* Update the directory mtime and ctime */
1680 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1682 inode.i_mtime = inode.i_ctime = time(0);
1683 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1685 return DIRENT_ABORT;
1688 return DIRENT_CHANGED;
1691 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1700 * Now, we iterate over all of the directories to update the
1704 is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
1708 if (rfs->progress) {
1709 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1715 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1716 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1717 check_and_change_inodes, &is);
1725 if (rfs->progress && (is.num < is.max_dirs))
1726 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1727 is.max_dirs, is.max_dirs);
1730 ext2fs_free_extent_table(rfs->imap);
1736 /* --------------------------------------------------------------------
1738 * Resize processing, phase 5.
1740 * In this phase we actually move the inode table around, and then
1741 * update the summary statistics. This is scary, since aborting here
1742 * will potentially scramble the filesystem. (We are moving the
1743 * inode tables around in place, and so the potential for lost data,
1744 * or at the very least scrambling the mapping between filenames and
1745 * inode numbers is very high in case of a power failure here.)
1746 * --------------------------------------------------------------------
1751 * A very scary routine --- this one moves the inode table around!!!
1753 * After this you have to use the rfs->new_fs file handle to read and
1756 static errcode_t move_itables(ext2_resize_t rfs)
1760 dgrp_t i, max_groups;
1761 ext2_filsys fs = rfs->new_fs;
1763 blk64_t old_blk, new_blk, blk;
1765 int j, to_move, moved;
1767 max_groups = fs->group_desc_count;
1768 if (max_groups > rfs->old_fs->group_desc_count)
1769 max_groups = rfs->old_fs->group_desc_count;
1771 size = fs->blocksize * fs->inode_blocks_per_group;
1772 if (!rfs->itable_buf) {
1773 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1779 * Figure out how many inode tables we need to move
1781 to_move = moved = 0;
1782 for (i=0; i < max_groups; i++)
1783 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
1784 ext2fs_inode_table_loc(fs, i))
1790 if (rfs->progress) {
1791 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1797 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1799 for (i=0; i < max_groups; i++) {
1800 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
1801 new_blk = ext2fs_inode_table_loc(fs, i);
1802 diff = new_blk - old_blk;
1804 #ifdef RESIZE2FS_DEBUG
1805 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1806 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
1807 i, old_blk, new_blk, diff);
1813 retval = io_channel_read_blk64(fs->io, old_blk,
1814 fs->inode_blocks_per_group,
1819 * The end of the inode table segment often contains
1820 * all zeros, and we're often only moving the inode
1821 * table down a block or two. If so, we can optimize
1822 * things by not rewriting blocks that we know to be zero
1825 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1828 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1829 #ifdef RESIZE2FS_DEBUG
1830 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1831 printf("%d blocks of zeros...\n", n);
1833 num = fs->inode_blocks_per_group;
1837 retval = io_channel_write_blk64(fs->io, new_blk,
1838 num, rfs->itable_buf);
1840 io_channel_write_blk64(fs->io, old_blk,
1841 num, rfs->itable_buf);
1845 retval = io_channel_write_blk64(fs->io,
1846 old_blk + fs->inode_blocks_per_group,
1847 diff, (rfs->itable_buf +
1848 (fs->inode_blocks_per_group - diff) *
1854 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1855 j < fs->inode_blocks_per_group ; j++, blk++)
1856 ext2fs_block_alloc_stats2(fs, blk, -1);
1858 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
1859 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1860 ext2fs_mark_super_dirty(rfs->old_fs);
1861 ext2fs_flush(rfs->old_fs);
1863 if (rfs->progress) {
1864 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1870 mark_table_blocks(fs, fs->block_map);
1872 #ifdef RESIZE2FS_DEBUG
1873 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1874 printf("Inode table move finished.\n");
1883 * This function is used when expanding a file system. It frees the
1884 * superblock and block group descriptor blocks from the block group
1885 * which is no longer the last block group.
1887 static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs)
1889 ext2_filsys fs = rfs->new_fs;
1890 ext2_filsys old_fs = rfs->old_fs;
1892 dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1;
1893 dgrp_t last_bg = fs->group_desc_count - 1;
1894 blk64_t sb, old_desc;
1897 if (!(fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2))
1900 if (last_bg <= old_last_bg)
1903 if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
1904 fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
1907 if (old_fs->super->s_backup_bgs[0] != old_last_bg &&
1908 old_fs->super->s_backup_bgs[1] != old_last_bg)
1911 if (fs->super->s_backup_bgs[0] == old_last_bg ||
1912 fs->super->s_backup_bgs[1] == old_last_bg)
1915 retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, old_last_bg,
1916 &sb, &old_desc, NULL, &num);
1921 ext2fs_unmark_block_bitmap2(fs->block_map, sb);
1923 ext2fs_unmark_block_bitmap_range2(fs->block_map, old_desc, num);
1928 * This function is used when shrinking a file system. We need to
1929 * utilize blocks from what will be the new last block group for the
1930 * backup superblock and block group descriptor blocks.
1931 * Unfortunately, those blocks may be used by other files or fs
1932 * metadata blocks. We need to mark them as being in use.
1934 static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
1935 ext2fs_block_bitmap meta_bmap)
1937 ext2_filsys fs = rfs->new_fs;
1938 ext2_filsys old_fs = rfs->old_fs;
1940 dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1;
1941 dgrp_t last_bg = fs->group_desc_count - 1;
1943 blk64_t blk, sb, old_desc;
1947 if (!(fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2))
1950 if (last_bg >= old_last_bg)
1953 if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
1954 fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
1957 if (fs->super->s_backup_bgs[0] != last_bg &&
1958 fs->super->s_backup_bgs[1] != last_bg)
1961 if (old_fs->super->s_backup_bgs[0] == last_bg ||
1962 old_fs->super->s_backup_bgs[1] == last_bg)
1965 retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, last_bg,
1966 &sb, &old_desc, NULL, &num);
1971 fputs(_("Should never happen! No sb in last super_sparse bg?\n"),
1975 if (old_desc != sb+1) {
1976 fputs(_("Should never happen! Unexpected old_desc in "
1977 "super_sparse bg?\n"),
1981 num = (old_desc) ? num : 1;
1983 /* Reserve the backup blocks */
1984 ext2fs_mark_block_bitmap_range2(fs->block_map, sb, num);
1986 for (g = 0; g < fs->group_desc_count; g++) {
1989 mb = ext2fs_block_bitmap_loc(fs, g);
1990 if ((mb >= sb) && (mb < sb + num)) {
1991 ext2fs_block_bitmap_loc_set(fs, g, 0);
1994 mb = ext2fs_inode_bitmap_loc(fs, g);
1995 if ((mb >= sb) && (mb < sb + num)) {
1996 ext2fs_inode_bitmap_loc_set(fs, g, 0);
1999 mb = ext2fs_inode_table_loc(fs, g);
2000 if ((mb < sb + num) &&
2001 (sb < mb + fs->inode_blocks_per_group)) {
2002 ext2fs_inode_table_loc_set(fs, g, 0);
2006 retval = ext2fs_allocate_group_table(fs, g, 0);
2012 for (blk = sb, i = 0; i < num; blk++, i++) {
2013 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
2014 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
2015 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
2016 rfs->needed_blocks++;
2018 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
2024 * Fix the resize inode
2026 static errcode_t fix_resize_inode(ext2_filsys fs)
2028 struct ext2_inode inode;
2030 char *block_buf = NULL;
2032 if (!(fs->super->s_feature_compat &
2033 EXT2_FEATURE_COMPAT_RESIZE_INODE))
2036 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
2037 if (retval) goto errout;
2039 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
2040 if (retval) goto errout;
2042 ext2fs_iblk_set(fs, &inode, 1);
2044 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
2045 if (retval) goto errout;
2047 if (!inode.i_block[EXT2_DIND_BLOCK]) {
2049 * Avoid zeroing out block #0; that's rude. This
2050 * should never happen anyway since the filesystem
2051 * should be fsck'ed and we assume it is consistent.
2053 fprintf(stderr, "%s",
2054 _("Should never happen: resize inode corrupt!\n"));
2058 memset(block_buf, 0, fs->blocksize);
2060 retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK],
2062 if (retval) goto errout;
2064 retval = ext2fs_create_resize_inode(fs);
2070 ext2fs_free_mem(&block_buf);
2075 * Finally, recalculate the summary information
2077 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
2081 unsigned int group = 0;
2082 unsigned int count = 0;
2083 blk64_t total_blocks_free = 0;
2084 int total_inodes_free = 0;
2087 blk64_t super_blk, old_desc_blk, new_desc_blk;
2088 int old_desc_blocks;
2091 * First calculate the block statistics
2093 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2094 ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
2096 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2097 old_desc_blocks = fs->super->s_first_meta_bg;
2099 old_desc_blocks = fs->desc_blocks +
2100 fs->super->s_reserved_gdt_blocks;
2101 for (blk = B2C(fs->super->s_first_data_block);
2102 blk < ext2fs_blocks_count(fs->super);
2103 blk += EXT2FS_CLUSTER_RATIO(fs)) {
2105 !(EQ_CLSTR(blk, super_blk) ||
2106 ((old_desc_blk && old_desc_blocks &&
2107 GE_CLSTR(blk, old_desc_blk) &&
2108 LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) ||
2109 ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) ||
2110 EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) ||
2111 EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) ||
2112 ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) &&
2113 LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group)
2114 + fs->inode_blocks_per_group))))) ||
2115 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
2117 total_blocks_free++;
2120 if ((count == fs->super->s_clusters_per_group) ||
2121 EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) {
2122 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
2123 ext2fs_group_desc_csum_set(fs, group);
2125 if (group >= fs->group_desc_count)
2129 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2130 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
2133 if (fs->super->s_feature_incompat &
2134 EXT2_FEATURE_INCOMPAT_META_BG)
2135 old_desc_blocks = fs->super->s_first_meta_bg;
2137 old_desc_blocks = fs->desc_blocks +
2138 fs->super->s_reserved_gdt_blocks;
2141 total_blocks_free = C2B(total_blocks_free);
2142 ext2fs_free_blocks_count_set(fs->super, total_blocks_free);
2145 * Next, calculate the inode statistics
2151 /* Protect loop from wrap-around if s_inodes_count maxed */
2152 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2153 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2155 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2157 total_inodes_free++;
2160 if ((count == fs->super->s_inodes_per_group) ||
2161 (ino == fs->super->s_inodes_count)) {
2162 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
2163 ext2fs_group_desc_csum_set(fs, group);
2165 if (group >= fs->group_desc_count)
2169 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2172 fs->super->s_free_inodes_count = total_inodes_free;
2173 ext2fs_mark_super_dirty(fs);
2178 * Journal may have been relocated; update the backup journal blocks
2179 * in the superblock.
2181 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
2184 struct ext2_inode inode;
2186 if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
2189 /* External journal? Nothing to do. */
2190 if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
2193 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
2196 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
2197 fs->super->s_jnl_blocks[15] = inode.i_size_high;
2198 fs->super->s_jnl_blocks[16] = inode.i_size;
2199 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
2200 ext2fs_mark_super_dirty(fs);
2204 static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
2205 int old_desc_blocks)
2207 blk64_t super_blk, old_desc_blk, new_desc_blk;
2210 /* inode table blocks plus allocation bitmaps */
2211 overhead = fs->inode_blocks_per_group + 2;
2213 ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
2214 &old_desc_blk, &new_desc_blk, 0);
2215 if ((grp == 0) || super_blk)
2218 overhead += old_desc_blocks;
2219 else if (new_desc_blk)
2226 * calcluate the minimum number of blocks the given fs can be resized to
2228 blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
2230 ext2_ino_t inode_count;
2232 blk64_t blks_needed, data_blocks;
2233 blk64_t grp, data_needed, last_start;
2234 blk64_t overhead = 0;
2235 int old_desc_blocks;
2236 int extra_groups = 0;
2237 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2240 * first figure out how many group descriptors we need to
2241 * handle the number of inodes we have
2243 inode_count = fs->super->s_inodes_count -
2244 fs->super->s_free_inodes_count;
2245 blks_needed = ext2fs_div_ceil(inode_count,
2246 fs->super->s_inodes_per_group) *
2247 EXT2_BLOCKS_PER_GROUP(fs->super);
2248 groups = ext2fs_div64_ceil(blks_needed,
2249 EXT2_BLOCKS_PER_GROUP(fs->super));
2250 #ifdef RESIZE2FS_DEBUG
2251 if (flags & RESIZE_DEBUG_MIN_CALC)
2252 printf("fs has %d inodes, %d groups required.\n",
2253 inode_count, groups);
2257 * number of old-style block group descriptor blocks
2259 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2260 old_desc_blocks = fs->super->s_first_meta_bg;
2262 old_desc_blocks = fs->desc_blocks +
2263 fs->super->s_reserved_gdt_blocks;
2265 /* calculate how many blocks are needed for data */
2266 data_needed = ext2fs_blocks_count(fs->super) -
2267 ext2fs_free_blocks_count(fs->super);
2269 for (grp = 0; grp < fs->group_desc_count; grp++)
2270 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
2271 #ifdef RESIZE2FS_DEBUG
2272 if (flags & RESIZE_DEBUG_MIN_CALC)
2273 printf("fs requires %llu data blocks.\n", data_needed);
2277 * For ext4 we need to allow for up to a flex_bg worth of
2278 * inode tables of slack space so the resize operation can be
2279 * guaranteed to finish.
2281 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2282 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
2283 data_needed += fs->inode_blocks_per_group * extra_groups;
2284 extra_groups = groups % flexbg_size;
2288 * figure out how many data blocks we have given the number of groups
2289 * we need for our inodes
2291 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
2293 for (grp = 0; grp < groups; grp++) {
2294 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
2297 * we want to keep track of how much data we can store in
2298 * the groups leading up to the last group so we can determine
2299 * how big the last group needs to be
2301 if (grp != (groups - 1))
2302 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
2305 data_blocks -= overhead;
2307 #ifdef RESIZE2FS_DEBUG
2308 if (flags & RESIZE_DEBUG_MIN_CALC)
2309 printf("With %d group(s), we have %llu blocks available.\n",
2310 groups, data_blocks);
2314 * if we need more group descriptors in order to accomodate our data
2315 * then we need to add them here
2317 while (data_needed > data_blocks) {
2318 blk64_t remainder = data_needed - data_blocks;
2321 /* figure out how many more groups we need for the data */
2322 extra_grps = ext2fs_div64_ceil(remainder,
2323 EXT2_BLOCKS_PER_GROUP(fs->super));
2325 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
2327 /* ok we have to account for the last group */
2328 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2329 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
2331 for (grp = groups; grp < groups+extra_grps; grp++) {
2332 overhead = calc_group_overhead(fs, grp,
2336 * again, we need to see how much data we cram into
2337 * all of the groups leading up to the last group
2339 if (grp != (groups + extra_grps - 1))
2340 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
2343 data_blocks -= overhead;
2346 groups += extra_grps;
2347 extra_groups += extra_grps;
2348 if (fs->super->s_feature_incompat
2349 & EXT4_FEATURE_INCOMPAT_FLEX_BG
2350 && extra_groups > flexbg_size) {
2352 * For ext4 we need to allow for up to a flex_bg worth
2353 * of inode tables of slack space so the resize
2354 * operation can be guaranteed to finish.
2356 extra_groups = flexbg_size -
2357 (groups & (flexbg_size - 1));
2358 data_needed += (fs->inode_blocks_per_group *
2360 extra_groups = groups % flexbg_size;
2362 #ifdef RESIZE2FS_DEBUG
2363 if (flags & RESIZE_DEBUG_MIN_CALC)
2364 printf("Added %d extra group(s), "
2365 "data_needed %llu, data_blocks %llu, "
2366 "last_start %llu\n",
2367 extra_grps, data_needed, data_blocks,
2372 /* now for the fun voodoo */
2373 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2374 #ifdef RESIZE2FS_DEBUG
2375 if (flags & RESIZE_DEBUG_MIN_CALC)
2376 printf("Last group's overhead is %llu\n", overhead);
2380 * if this is the case then the last group is going to have data in it
2381 * so we need to adjust the size of the last group accordingly
2383 if (last_start < data_needed) {
2384 blk64_t remainder = data_needed - last_start;
2386 #ifdef RESIZE2FS_DEBUG
2387 if (flags & RESIZE_DEBUG_MIN_CALC)
2388 printf("Need %llu data blocks in last group\n",
2392 * 50 is a magic number that mkfs/resize uses to see if its
2393 * even worth making/resizing the fs. basically you need to
2394 * have at least 50 blocks in addition to the blocks needed
2395 * for the metadata in the last group
2398 overhead += remainder;
2404 overhead += fs->super->s_first_data_block;
2405 #ifdef RESIZE2FS_DEBUG
2406 if (flags & RESIZE_DEBUG_MIN_CALC)
2407 printf("Final size of last group is %lld\n", overhead);
2411 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2412 * only do groups-1, and then add the number of blocks needed to
2413 * handle the group descriptor metadata+data that we need
2415 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2416 blks_needed += overhead;
2419 * Make sure blks_needed covers the end of the inode table in
2420 * the last block group.
2422 overhead = ext2fs_inode_table_loc(fs, groups-1) +
2423 fs->inode_blocks_per_group;
2424 if (blks_needed < overhead)
2425 blks_needed = overhead;
2427 #ifdef RESIZE2FS_DEBUG
2428 if (flags & RESIZE_DEBUG_MIN_CALC)
2429 printf("Estimated blocks needed: %llu\n", blks_needed);
2433 * If at this point we've already added up more "needed" than
2434 * the current size, just return current size as minimum.
2436 if (blks_needed >= ext2fs_blocks_count(fs->super))
2437 return ext2fs_blocks_count(fs->super);
2439 * We need to reserve a few extra blocks if extents are
2440 * enabled, in case we need to grow the extent tree. The more
2441 * we shrink the file system, the more space we need.
2443 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
2444 blk64_t safe_margin = (ext2fs_blocks_count(fs->super) -
2446 #ifdef RESIZE2FS_DEBUG
2447 if (flags & RESIZE_DEBUG_MIN_CALC)
2448 printf("Extents safety margin: %llu\n", safe_margin);
2450 blks_needed += safe_margin;