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 void free_gdp_blocks(ext2_filsys fs,
284 ext2fs_block_bitmap reserve_blocks,
291 blk = ext2fs_block_bitmap_loc(old_fs, group);
293 (blk < ext2fs_blocks_count(fs->super))) {
294 ext2fs_block_alloc_stats2(fs, blk, -1);
295 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
298 blk = ext2fs_inode_bitmap_loc(old_fs, group);
300 (blk < ext2fs_blocks_count(fs->super))) {
301 ext2fs_block_alloc_stats2(fs, blk, -1);
302 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
305 blk = ext2fs_inode_table_loc(old_fs, group);
307 (blk >= ext2fs_blocks_count(fs->super)))
311 j < fs->inode_blocks_per_group; j++, blk++) {
312 if (blk >= ext2fs_blocks_count(fs->super))
314 ext2fs_block_alloc_stats2(fs, blk, -1);
315 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
320 * This routine is shared by the online and offline resize routines.
321 * All of the information which is adjusted in memory is done here.
323 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
324 ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
327 blk64_t overhead = 0;
329 blk64_t blk, group_block;
331 blk64_t old_numblocks, numblocks, adjblocks;
332 unsigned long i, j, old_desc_blocks;
333 unsigned int meta_bg, meta_bg_size;
334 int has_super, csum_flag;
335 unsigned long long new_inodes; /* u64 to check for overflow */
338 ext2fs_blocks_count_set(fs->super, new_size);
341 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
342 fs->super->s_first_data_block,
343 EXT2_BLOCKS_PER_GROUP(fs->super));
344 if (fs->group_desc_count == 0)
345 return EXT2_ET_TOOSMALL;
346 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
347 EXT2_DESC_PER_BLOCK(fs->super));
350 * Overhead is the number of bookkeeping blocks per group. It
351 * includes the superblock backup, the group descriptor
352 * backups, the inode bitmap, the block bitmap, and the inode
355 overhead = (int) (2 + fs->inode_blocks_per_group);
357 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
358 overhead += 1 + fs->desc_blocks +
359 fs->super->s_reserved_gdt_blocks;
362 * See if the last group is big enough to support the
363 * necessary data structures. If not, we need to get rid of
366 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
367 fs->super->s_blocks_per_group;
368 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
369 return EXT2_ET_TOOSMALL;
370 if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
371 ext2fs_blocks_count_set(fs->super,
372 ext2fs_blocks_count(fs->super) - rem);
376 * Adjust the number of inodes
378 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
379 if (new_inodes > ~0U) {
380 fprintf(stderr, _("inodes (%llu) must be less than %u"),
382 return EXT2_ET_TOO_MANY_INODES;
384 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
385 fs->group_desc_count;
388 * Adjust the number of free blocks
390 blk = ext2fs_blocks_count(old_fs->super);
391 if (blk > ext2fs_blocks_count(fs->super))
392 ext2fs_free_blocks_count_set(fs->super,
393 ext2fs_free_blocks_count(fs->super) -
394 (blk - ext2fs_blocks_count(fs->super)));
396 ext2fs_free_blocks_count_set(fs->super,
397 ext2fs_free_blocks_count(fs->super) +
398 (ext2fs_blocks_count(fs->super) - blk));
401 * Adjust the number of reserved blocks
403 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
404 ext2fs_blocks_count(old_fs->super);
405 ext2fs_r_blocks_count_set(fs->super,
406 (percent * ext2fs_blocks_count(fs->super) /
410 * Adjust the bitmaps for size
412 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
413 fs->super->s_inodes_count,
415 if (retval) goto errout;
417 real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) *
418 fs->group_desc_count)) - 1 +
419 fs->super->s_first_data_block;
420 retval = ext2fs_resize_block_bitmap2(new_size - 1,
421 real_end, fs->block_map);
422 if (retval) goto errout;
425 * If we are growing the file system, also grow the size of
426 * the reserve_blocks bitmap
428 if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) {
429 retval = ext2fs_resize_block_bitmap2(new_size - 1,
430 real_end, reserve_blocks);
431 if (retval) goto errout;
435 * Reallocate the group descriptors as necessary.
437 if (old_fs->desc_blocks != fs->desc_blocks) {
438 retval = ext2fs_resize_mem(old_fs->desc_blocks *
440 fs->desc_blocks * fs->blocksize,
444 if (fs->desc_blocks > old_fs->desc_blocks)
445 memset((char *) fs->group_desc +
446 (old_fs->desc_blocks * fs->blocksize), 0,
447 (fs->desc_blocks - old_fs->desc_blocks) *
452 * If the resize_inode feature is set, and we are changing the
453 * number of descriptor blocks, then adjust
454 * s_reserved_gdt_blocks if possible to avoid needing to move
455 * the inode table either now or in the future.
457 if ((fs->super->s_feature_compat &
458 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
459 (old_fs->desc_blocks != fs->desc_blocks)) {
462 new = ((int) fs->super->s_reserved_gdt_blocks) +
463 (old_fs->desc_blocks - fs->desc_blocks);
466 if (new > (int) fs->blocksize/4)
467 new = fs->blocksize/4;
468 fs->super->s_reserved_gdt_blocks = new;
472 * Update the location of the backup superblocks if the
473 * sparse_super2 feature is enabled.
475 if (fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) {
476 dgrp_t last_bg = fs->group_desc_count - 1;
477 dgrp_t old_last_bg = old_fs->group_desc_count - 1;
479 if (last_bg > old_last_bg) {
480 if (old_fs->group_desc_count == 1)
481 fs->super->s_backup_bgs[0] = 1;
482 if (old_fs->group_desc_count == 1 &&
483 fs->super->s_backup_bgs[0])
484 fs->super->s_backup_bgs[0] = last_bg;
485 else if (fs->super->s_backup_bgs[1])
486 fs->super->s_backup_bgs[1] = last_bg;
487 } else if (last_bg < old_last_bg) {
488 if (fs->super->s_backup_bgs[0] > last_bg)
489 fs->super->s_backup_bgs[0] = 0;
490 if (fs->super->s_backup_bgs[1] > last_bg)
491 fs->super->s_backup_bgs[1] = 0;
493 old_fs->super->s_backup_bgs[1] == old_last_bg)
494 fs->super->s_backup_bgs[1] = last_bg;
499 * If we are shrinking the number of block groups, we're done
502 if (old_fs->group_desc_count > fs->group_desc_count) {
504 * Check the block groups that we are chopping off
505 * and free any blocks associated with their metadata
507 for (i = fs->group_desc_count;
508 i < old_fs->group_desc_count; i++)
509 free_gdp_blocks(fs, reserve_blocks, old_fs, i);
515 * Fix the count of the last (old) block group
517 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
518 old_fs->super->s_first_data_block) %
519 old_fs->super->s_blocks_per_group;
521 old_numblocks = old_fs->super->s_blocks_per_group;
522 if (old_fs->group_desc_count == fs->group_desc_count) {
523 numblocks = (ext2fs_blocks_count(fs->super) -
524 fs->super->s_first_data_block) %
525 fs->super->s_blocks_per_group;
527 numblocks = fs->super->s_blocks_per_group;
529 numblocks = fs->super->s_blocks_per_group;
530 i = old_fs->group_desc_count - 1;
531 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
532 ext2fs_group_desc_csum_set(fs, i);
535 * If the number of block groups is staying the same, we're
536 * done and can exit now. (If the number block groups is
537 * shrinking, we had exited earlier.)
539 if (old_fs->group_desc_count >= fs->group_desc_count) {
545 * Initialize the new block group descriptors
547 group_block = ext2fs_group_first_block2(fs,
548 old_fs->group_desc_count);
549 csum_flag = ext2fs_has_group_desc_csum(fs);
550 if (access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0)
551 lazy_itable_init = 1;
552 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
553 old_desc_blocks = fs->super->s_first_meta_bg;
555 old_desc_blocks = fs->desc_blocks +
556 fs->super->s_reserved_gdt_blocks;
559 * If we changed the number of block_group descriptor blocks,
560 * we need to make sure they are all marked as reserved in the
561 * file systems's block allocation map.
563 for (i = 0; i < old_fs->group_desc_count; i++)
564 ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
566 for (i = old_fs->group_desc_count;
567 i < fs->group_desc_count; i++) {
568 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
569 sizeof(struct ext2_group_desc));
572 ext2fs_bg_flags_zap(fs, i);
574 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
575 if (!lazy_itable_init)
576 ext2fs_bg_flags_set(fs, i,
577 EXT2_BG_INODE_ZEROED);
578 ext2fs_bg_itable_unused_set(fs, i,
579 fs->super->s_inodes_per_group);
582 numblocks = ext2fs_group_blocks_count(fs, i);
583 if ((i < fs->group_desc_count - 1) && csum_flag)
584 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
586 has_super = ext2fs_bg_has_super(fs, i);
588 ext2fs_block_alloc_stats2(fs, group_block, +1);
591 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
592 meta_bg = i / meta_bg_size;
593 if (!(fs->super->s_feature_incompat &
594 EXT2_FEATURE_INCOMPAT_META_BG) ||
595 (meta_bg < fs->super->s_first_meta_bg)) {
597 for (j=0; j < old_desc_blocks; j++)
598 ext2fs_block_alloc_stats2(fs,
599 group_block + 1 + j, +1);
600 adjblocks += old_desc_blocks;
605 if (((i % meta_bg_size) == 0) ||
606 ((i % meta_bg_size) == 1) ||
607 ((i % meta_bg_size) == (meta_bg_size-1)))
608 ext2fs_block_alloc_stats2(fs,
609 group_block + has_super, +1);
612 adjblocks += 2 + fs->inode_blocks_per_group;
614 numblocks -= adjblocks;
615 ext2fs_free_blocks_count_set(fs->super,
616 ext2fs_free_blocks_count(fs->super) - adjblocks);
617 fs->super->s_free_inodes_count +=
618 fs->super->s_inodes_per_group;
619 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
620 ext2fs_bg_free_inodes_count_set(fs, i,
621 fs->super->s_inodes_per_group);
622 ext2fs_bg_used_dirs_count_set(fs, i, 0);
623 ext2fs_group_desc_csum_set(fs, i);
625 retval = ext2fs_allocate_group_table(fs, i, 0);
626 if (retval) goto errout;
628 group_block += fs->super->s_blocks_per_group;
633 * Mark all of the metadata blocks as reserved so they won't
634 * get allocated by the call to ext2fs_allocate_group_table()
635 * in blocks_to_move(), where we allocate new blocks to
636 * replace those allocation bitmap and inode table blocks
637 * which have to get relocated to make space for an increased
638 * number of the block group descriptors.
641 mark_table_blocks(fs, reserve_blocks);
648 * This routine adjusts the superblock and other data structures, both
649 * in disk as well as in memory...
651 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
653 ext2_filsys fs = rfs->new_fs;
658 unsigned long max_group;
660 ext2fs_mark_super_dirty(fs);
661 ext2fs_mark_bb_dirty(fs);
662 ext2fs_mark_ib_dirty(fs);
664 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
665 &rfs->reserve_blocks);
669 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
674 * Check to make sure there are enough inodes
676 if ((rfs->old_fs->super->s_inodes_count -
677 rfs->old_fs->super->s_free_inodes_count) >
678 rfs->new_fs->super->s_inodes_count) {
684 * If we are shrinking the number block groups, we're done and
687 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
693 * If the number of block groups is staying the same, we're
694 * done and can exit now. (If the number block groups is
695 * shrinking, we had exited earlier.)
697 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
703 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
704 * supports lazy inode initialization, we can skip
705 * initializing the inode table.
707 if (lazy_itable_init && ext2fs_has_group_desc_csum(fs)) {
713 * Initialize the inode table
715 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
720 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
721 group_block = ext2fs_group_first_block2(fs,
722 rfs->old_fs->group_desc_count);
723 adj = rfs->old_fs->group_desc_count;
724 max_group = fs->group_desc_count - adj;
726 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
731 for (i = rfs->old_fs->group_desc_count;
732 i < fs->group_desc_count; i++) {
734 * Write out the new inode table
736 retval = io_channel_write_blk64(fs->io,
737 ext2fs_inode_table_loc(fs, i),
738 fs->inode_blocks_per_group,
740 if (retval) goto errout;
742 io_channel_flush(fs->io);
744 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
745 i - adj + 1, max_group);
749 group_block += fs->super->s_blocks_per_group;
751 io_channel_flush(fs->io);
758 /* --------------------------------------------------------------------
760 * Resize processing, phase 2.
762 * In this phase we adjust determine which blocks need to be moved, in
763 * blocks_to_move(). We then copy the blocks to their ultimate new
764 * destinations using block_mover(). Since we are copying blocks to
765 * their new locations, again during this pass we can abort without
767 * --------------------------------------------------------------------
771 * This helper function creates a block bitmap with all of the
772 * filesystem meta-data blocks.
774 static errcode_t mark_table_blocks(ext2_filsys fs,
775 ext2fs_block_bitmap bmap)
780 for (i = 0; i < fs->group_desc_count; i++) {
781 ext2fs_reserve_super_and_bgd(fs, i, bmap);
784 * Mark the blocks used for the inode table
786 blk = ext2fs_inode_table_loc(fs, i);
788 ext2fs_mark_block_bitmap_range2(bmap, blk,
789 fs->inode_blocks_per_group);
792 * Mark block used for the block bitmap
794 blk = ext2fs_block_bitmap_loc(fs, i);
796 ext2fs_mark_block_bitmap2(bmap, blk);
799 * Mark block used for the inode bitmap
801 blk = ext2fs_inode_bitmap_loc(fs, i);
803 ext2fs_mark_block_bitmap2(bmap, blk);
809 * This function checks to see if a particular block (either a
810 * superblock or a block group descriptor) overlaps with an inode or
811 * block bitmap block, or with the inode table.
813 static void mark_fs_metablock(ext2_resize_t rfs,
814 ext2fs_block_bitmap meta_bmap,
815 int group, blk64_t blk)
817 ext2_filsys fs = rfs->new_fs;
819 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
820 ext2fs_block_alloc_stats2(fs, blk, +1);
823 * Check to see if we overlap with the inode or block bitmap,
824 * or the inode tables. If not, and the block is in use, then
825 * mark it as a block to be moved.
827 if (IS_BLOCK_BM(fs, group, blk)) {
828 ext2fs_block_bitmap_loc_set(fs, group, 0);
829 rfs->needed_blocks++;
832 if (IS_INODE_BM(fs, group, blk)) {
833 ext2fs_inode_bitmap_loc_set(fs, group, 0);
834 rfs->needed_blocks++;
837 if (IS_INODE_TB(fs, group, blk)) {
838 ext2fs_inode_table_loc_set(fs, group, 0);
839 rfs->needed_blocks++;
842 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
845 for (i=0; i < rfs->old_fs->group_desc_count; i++) {
846 if (IS_BLOCK_BM(fs, i, blk)) {
847 ext2fs_block_bitmap_loc_set(fs, i, 0);
848 rfs->needed_blocks++;
851 if (IS_INODE_BM(fs, i, blk)) {
852 ext2fs_inode_bitmap_loc_set(fs, i, 0);
853 rfs->needed_blocks++;
856 if (IS_INODE_TB(fs, i, blk)) {
857 ext2fs_inode_table_loc_set(fs, i, 0);
858 rfs->needed_blocks++;
864 if (ext2fs_has_group_desc_csum(fs) &&
865 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
867 * If the block bitmap is uninitialized, which means
868 * nothing other than standard metadata in use.
871 } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
872 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
873 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
874 rfs->needed_blocks++;
880 * This routine marks and unmarks reserved blocks in the new block
881 * bitmap. It also determines which blocks need to be moved and
882 * places this information into the move_blocks bitmap.
884 static errcode_t blocks_to_move(ext2_resize_t rfs)
887 dgrp_t i, max_groups, g;
888 blk64_t blk, group_blk;
889 blk64_t old_blocks, new_blocks;
891 unsigned int meta_bg, meta_bg_size;
893 ext2_filsys fs, old_fs;
894 ext2fs_block_bitmap meta_bmap;
898 old_fs = rfs->old_fs;
899 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
902 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
907 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
912 retval = mark_table_blocks(old_fs, meta_bmap);
919 * If we're shrinking the filesystem, we need to move any
920 * group's metadata blocks (either allocation bitmaps or the
921 * inode table) which are beyond the end of the new
924 new_size = ext2fs_blocks_count(fs->super);
925 if (new_size < ext2fs_blocks_count(old_fs->super)) {
926 for (g = 0; g < fs->group_desc_count; g++) {
929 * ext2fs_allocate_group_table will re-allocate any
930 * metadata blocks whose location is set to zero.
932 if (ext2fs_block_bitmap_loc(fs, g) >= new_size) {
933 ext2fs_block_bitmap_loc_set(fs, g, 0);
936 if (ext2fs_inode_bitmap_loc(fs, g) >= new_size) {
937 ext2fs_inode_bitmap_loc_set(fs, g, 0);
940 if ((ext2fs_inode_table_loc(fs, g) +
941 fs->inode_blocks_per_group) > new_size) {
942 ext2fs_inode_table_loc_set(fs, g, 0);
947 retval = ext2fs_allocate_group_table(fs, g, 0);
955 * If we're shrinking the filesystem, we need to move all of
956 * the blocks that don't fit any more
958 for (blk = ext2fs_blocks_count(fs->super);
959 blk < ext2fs_blocks_count(old_fs->super); blk++) {
960 g = ext2fs_group_of_blk2(fs, blk);
961 if (ext2fs_has_group_desc_csum(fs) &&
962 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
964 * The block bitmap is uninitialized, so skip
965 * to the next block group.
967 blk = ext2fs_group_first_block2(fs, g+1) - 1;
970 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
971 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
972 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
973 rfs->needed_blocks++;
975 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
978 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
979 old_blocks = old_fs->super->s_first_meta_bg;
980 new_blocks = fs->super->s_first_meta_bg;
982 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
983 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
986 retval = reserve_sparse_super2_last_group(rfs, meta_bmap);
990 if (old_blocks == new_blocks) {
995 max_groups = fs->group_desc_count;
996 if (max_groups > old_fs->group_desc_count)
997 max_groups = old_fs->group_desc_count;
998 group_blk = old_fs->super->s_first_data_block;
1000 * If we're reducing the number of descriptor blocks, this
1001 * makes life easy. :-) We just have to mark some extra
1004 if (old_blocks > new_blocks) {
1005 for (i = 0; i < max_groups; i++) {
1006 if (!ext2fs_bg_has_super(fs, i)) {
1007 group_blk += fs->super->s_blocks_per_group;
1010 for (blk = group_blk+1+new_blocks;
1011 blk < group_blk+1+old_blocks; blk++) {
1012 ext2fs_block_alloc_stats2(fs, blk, -1);
1013 rfs->needed_blocks--;
1015 group_blk += fs->super->s_blocks_per_group;
1021 * If we're increasing the number of descriptor blocks, life
1022 * gets interesting....
1024 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
1025 flex_bg = fs->super->s_feature_incompat &
1026 EXT4_FEATURE_INCOMPAT_FLEX_BG;
1027 /* first reserve all of the existing fs meta blocks */
1028 for (i = 0; i < max_groups; i++) {
1029 has_super = ext2fs_bg_has_super(fs, i);
1031 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
1033 meta_bg = i / meta_bg_size;
1034 if (!(fs->super->s_feature_incompat &
1035 EXT2_FEATURE_INCOMPAT_META_BG) ||
1036 (meta_bg < fs->super->s_first_meta_bg)) {
1038 for (blk = group_blk+1;
1039 blk < group_blk + 1 + new_blocks; blk++)
1040 mark_fs_metablock(rfs, meta_bmap,
1046 if (((i % meta_bg_size) == 0) ||
1047 ((i % meta_bg_size) == 1) ||
1048 ((i % meta_bg_size) == (meta_bg_size-1)))
1049 mark_fs_metablock(rfs, meta_bmap, i,
1050 group_blk + has_super);
1054 * Reserve the existing meta blocks that we know
1055 * aren't to be moved.
1057 * For flex_bg file systems, in order to avoid
1058 * overwriting fs metadata (especially inode table
1059 * blocks) belonging to a different block group when
1060 * we are relocating the inode tables, we need to
1061 * reserve all existing fs metadata blocks.
1063 if (ext2fs_block_bitmap_loc(fs, i))
1064 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1065 ext2fs_block_bitmap_loc(fs, i));
1066 else if (flex_bg && i < old_fs->group_desc_count)
1067 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1068 ext2fs_block_bitmap_loc(old_fs, i));
1070 if (ext2fs_inode_bitmap_loc(fs, i))
1071 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1072 ext2fs_inode_bitmap_loc(fs, i));
1073 else if (flex_bg && i < old_fs->group_desc_count)
1074 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1075 ext2fs_inode_bitmap_loc(old_fs, i));
1077 if (ext2fs_inode_table_loc(fs, i))
1078 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1079 ext2fs_inode_table_loc(fs, i),
1080 fs->inode_blocks_per_group);
1081 else if (flex_bg && i < old_fs->group_desc_count)
1082 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1083 ext2fs_inode_table_loc(old_fs, i),
1084 old_fs->inode_blocks_per_group);
1086 group_blk += rfs->new_fs->super->s_blocks_per_group;
1089 /* Allocate the missing data structures */
1090 for (i = 0; i < max_groups; i++) {
1091 if (ext2fs_inode_table_loc(fs, i) &&
1092 ext2fs_inode_bitmap_loc(fs, i) &&
1093 ext2fs_block_bitmap_loc(fs, i))
1096 retval = ext2fs_allocate_group_table(fs, i,
1097 rfs->reserve_blocks);
1102 * For those structures that have changed, we need to
1105 if (ext2fs_block_bitmap_loc(old_fs, i) !=
1106 (blk = ext2fs_block_bitmap_loc(fs, i))) {
1107 ext2fs_block_alloc_stats2(fs, blk, +1);
1108 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1109 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1110 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1113 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
1114 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
1115 ext2fs_block_alloc_stats2(fs, blk, +1);
1116 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1117 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1118 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1123 * The inode table, if we need to relocate it, is
1124 * handled specially. We have to reserve the blocks
1125 * for both the old and the new inode table, since we
1126 * can't have the inode table be destroyed during the
1127 * block relocation phase.
1129 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
1130 continue; /* inode table not moved */
1132 rfs->needed_blocks += fs->inode_blocks_per_group;
1135 * Mark the new inode table as in use in the new block
1136 * allocation bitmap, and move any blocks that might
1139 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
1140 j < fs->inode_blocks_per_group ; j++, blk++) {
1141 ext2fs_block_alloc_stats2(fs, blk, +1);
1142 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1143 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1144 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1149 * Make sure the old inode table is reserved in the
1150 * block reservation bitmap.
1152 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1153 j < fs->inode_blocks_per_group ; j++, blk++)
1154 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1160 ext2fs_free_block_bitmap(meta_bmap);
1166 * This helper function tries to allocate a new block. We try to
1167 * avoid hitting the original group descriptor blocks at least at
1168 * first, since we want to make it possible to recover from a badly
1169 * aborted resize operation as much as possible.
1171 * In the future, I may further modify this routine to balance out
1172 * where we get the new blocks across the various block groups.
1173 * Ideally we would allocate blocks that corresponded with the block
1174 * group of the containing inode, and keep contiguous blocks
1175 * together. However, this very difficult to do efficiently, since we
1176 * don't have the necessary information up front.
1180 #define DESPERATION 2
1182 static void init_block_alloc(ext2_resize_t rfs)
1184 rfs->alloc_state = AVOID_OLD;
1185 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1187 /* HACK for testing */
1188 if (ext2fs_blocks_count(rfs->new_fs->super) >
1189 ext2fs_blocks_count(rfs->old_fs->super))
1190 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1194 static blk64_t get_new_block(ext2_resize_t rfs)
1196 ext2_filsys fs = rfs->new_fs;
1199 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1200 if (rfs->alloc_state == DESPERATION)
1203 #ifdef RESIZE2FS_DEBUG
1204 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1205 printf("Going into desperation mode "
1206 "for block allocations\n");
1208 rfs->alloc_state = DESPERATION;
1209 rfs->new_blk = fs->super->s_first_data_block;
1212 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1213 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1215 ((rfs->alloc_state == AVOID_OLD) &&
1216 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1217 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1222 return rfs->new_blk;
1226 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1229 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1232 blk = get_new_block(rfs);
1236 #ifdef RESIZE2FS_DEBUG
1237 if (rfs->flags & 0xF)
1238 printf("get_alloc_block allocating %llu\n", blk);
1241 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1242 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1243 *ret = (blk64_t) blk;
1247 static errcode_t block_mover(ext2_resize_t rfs)
1249 blk64_t blk, old_blk, new_blk;
1250 ext2_filsys fs = rfs->new_fs;
1251 ext2_filsys old_fs = rfs->old_fs;
1256 ext2_badblocks_list badblock_list = 0;
1257 int bb_modified = 0;
1259 fs->get_alloc_block = resize2fs_get_alloc_block;
1260 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1262 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1266 new_blk = fs->super->s_first_data_block;
1267 if (!rfs->itable_buf) {
1268 retval = ext2fs_get_array(fs->blocksize,
1269 fs->inode_blocks_per_group,
1274 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1279 * The first step is to figure out where all of the blocks
1282 to_move = moved = 0;
1283 init_block_alloc(rfs);
1284 for (blk = B2C(old_fs->super->s_first_data_block);
1285 blk < ext2fs_blocks_count(old_fs->super);
1286 blk += EXT2FS_CLUSTER_RATIO(fs)) {
1287 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1289 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1291 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1292 ext2fs_badblocks_list_del(badblock_list, blk);
1297 new_blk = get_new_block(rfs);
1302 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1303 ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk));
1309 ext2fs_free_extent_table(rfs->bmap);
1317 * Step two is to actually move the blocks
1319 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1320 if (retval) goto errout;
1322 if (rfs->progress) {
1323 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1329 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1330 if (retval) goto errout;
1333 old_blk = C2B(old_blk);
1334 new_blk = C2B(new_blk);
1336 #ifdef RESIZE2FS_DEBUG
1337 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1338 printf("Moving %llu blocks %llu->%llu\n",
1339 size, old_blk, new_blk);
1343 if (c > fs->inode_blocks_per_group)
1344 c = fs->inode_blocks_per_group;
1345 retval = io_channel_read_blk64(fs->io, old_blk, c,
1347 if (retval) goto errout;
1348 retval = io_channel_write_blk64(fs->io, new_blk, c,
1350 if (retval) goto errout;
1355 if (rfs->progress) {
1356 io_channel_flush(fs->io);
1357 retval = (rfs->progress)(rfs,
1358 E2_RSZ_BLOCK_RELOC_PASS,
1364 io_channel_flush(fs->io);
1368 if (badblock_list) {
1369 if (!retval && bb_modified)
1370 retval = ext2fs_update_bb_inode(old_fs,
1372 ext2fs_badblocks_list_free(badblock_list);
1378 /* --------------------------------------------------------------------
1380 * Resize processing, phase 3
1382 * --------------------------------------------------------------------
1387 * The extent translation table is stored in clusters so we need to
1388 * take special care when mapping a source block number to its
1389 * destination block number.
1391 static __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc)
1393 __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc)));
1396 new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1);
1400 struct process_block_struct {
1404 struct ext2_inode * inode;
1411 static int process_block(ext2_filsys fs, blk64_t *block_nr,
1412 e2_blkcnt_t blockcnt,
1413 blk64_t ref_block EXT2FS_ATTR((unused)),
1414 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1416 struct process_block_struct *pb;
1418 blk64_t block, new_block;
1421 pb = (struct process_block_struct *) priv_data;
1423 if (pb->rfs->bmap) {
1424 new_block = extent_translate(fs, pb->rfs->bmap, block);
1426 *block_nr = new_block;
1427 ret |= BLOCK_CHANGED;
1429 #ifdef RESIZE2FS_DEBUG
1430 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1431 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1432 pb->old_ino, blockcnt, block,
1440 * If we moved inodes and metadata_csum is enabled, we must force the
1441 * extent block to be rewritten with new checksum.
1443 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
1444 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
1446 pb->old_ino != pb->ino)
1447 ret |= BLOCK_CHANGED;
1450 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1451 block, (int) blockcnt);
1463 static errcode_t progress_callback(ext2_filsys fs,
1464 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1465 dgrp_t group, void * priv_data)
1467 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1471 * This check is to protect against old ext2 libraries. It
1472 * shouldn't be needed against new libraries.
1477 if (rfs->progress) {
1478 io_channel_flush(fs->io);
1479 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1480 group+1, fs->group_desc_count);
1488 static errcode_t migrate_ea_block(ext2_resize_t rfs, ext2_ino_t ino,
1489 struct ext2_inode *inode, int *changed)
1495 /* No EA block or no remapping? Quit early. */
1496 if (ext2fs_file_acl_block(rfs->old_fs, inode) == 0 && !rfs->bmap)
1498 new_block = extent_translate(rfs->old_fs, rfs->bmap,
1499 ext2fs_file_acl_block(rfs->old_fs, inode));
1503 /* Set the new ACL block */
1504 ext2fs_file_acl_block_set(rfs->old_fs, inode, new_block);
1506 /* Update checksum */
1507 if (EXT2_HAS_RO_COMPAT_FEATURE(rfs->new_fs->super,
1508 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1509 err = ext2fs_get_mem(rfs->old_fs->blocksize, &buf);
1512 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1513 err = ext2fs_read_ext_attr3(rfs->old_fs, new_block, buf, ino);
1514 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1517 err = ext2fs_write_ext_attr3(rfs->old_fs, new_block, buf, ino);
1524 ext2fs_free_mem(&buf);
1528 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1530 struct process_block_struct pb;
1531 ext2_ino_t ino, new_inode;
1532 struct ext2_inode *inode = NULL;
1533 ext2_inode_scan scan = NULL;
1535 char *block_buf = 0;
1536 ext2_ino_t start_to_move;
1540 if ((rfs->old_fs->group_desc_count <=
1541 rfs->new_fs->group_desc_count) &&
1546 * Save the original size of the old filesystem, and
1547 * temporarily set the size to be the new size if the new size
1548 * is larger. We need to do this to avoid catching an error
1549 * by the block iterator routines
1551 orig_size = ext2fs_blocks_count(rfs->old_fs->super);
1552 if (orig_size < ext2fs_blocks_count(rfs->new_fs->super))
1553 ext2fs_blocks_count_set(rfs->old_fs->super,
1554 ext2fs_blocks_count(rfs->new_fs->super));
1556 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1557 if (retval) goto errout;
1559 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1560 if (retval) goto errout;
1561 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1562 if (retval) goto errout;
1564 start_to_move = (rfs->new_fs->group_desc_count *
1565 rfs->new_fs->super->s_inodes_per_group);
1567 if (rfs->progress) {
1568 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1569 0, rfs->old_fs->group_desc_count);
1573 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1577 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1578 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1579 inode = malloc(inode_size);
1585 * First, copy all of the inodes that need to be moved
1586 * elsewhere in the inode table
1589 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1590 if (retval) goto errout;
1594 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1595 continue; /* inode not in use */
1597 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1600 /* Remap EA block */
1601 retval = migrate_ea_block(rfs, ino, inode, &pb.changed);
1606 if (ino <= start_to_move)
1607 goto remap_blocks; /* Don't need to move inode. */
1610 * Find a new inode. Now that extents and directory blocks
1611 * are tied to the inode number through the checksum, we must
1612 * set up the new inode before we start rewriting blocks.
1614 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1618 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1620 inode->i_ctime = time(0);
1621 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1627 #ifdef RESIZE2FS_DEBUG
1628 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1629 printf("Inode moved %u->%u\n", ino, new_inode);
1632 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1636 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1640 retval = ext2fs_write_inode_full(rfs->old_fs,
1647 * Update inodes to point to new blocks; schedule directory
1648 * blocks for inode remapping. Need to write out dir blocks
1649 * with new inode numbers if we have metadata_csum enabled.
1651 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
1652 (rfs->bmap || pb.is_dir)) {
1655 pb.has_extents = inode->i_flags & EXT4_EXTENTS_FL;
1656 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1657 retval = ext2fs_block_iterate3(rfs->old_fs,
1658 new_inode, 0, block_buf,
1659 process_block, &pb);
1660 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1669 io_channel_flush(rfs->old_fs->io);
1672 ext2fs_blocks_count_set(rfs->old_fs->super, orig_size);
1674 ext2fs_free_extent_table(rfs->bmap);
1678 ext2fs_close_inode_scan(scan);
1680 ext2fs_free_mem(&block_buf);
1685 /* --------------------------------------------------------------------
1687 * Resize processing, phase 4.
1689 * --------------------------------------------------------------------
1695 unsigned int max_dirs;
1699 static int check_and_change_inodes(ext2_ino_t dir,
1700 int entry EXT2FS_ATTR((unused)),
1701 struct ext2_dir_entry *dirent, int offset,
1702 int blocksize EXT2FS_ATTR((unused)),
1703 char *buf EXT2FS_ATTR((unused)),
1706 struct istruct *is = (struct istruct *) priv_data;
1707 struct ext2_inode inode;
1708 ext2_ino_t new_inode;
1712 if (is->rfs->progress && offset == 0) {
1713 io_channel_flush(is->rfs->old_fs->io);
1714 is->err = (is->rfs->progress)(is->rfs,
1715 E2_RSZ_INODE_REF_UPD_PASS,
1716 ++is->num, is->max_dirs);
1718 return DIRENT_ABORT;
1722 * If we have checksums enabled and the inode wasn't present in the
1723 * old fs, then we must rewrite all dir blocks with new checksums.
1725 if (EXT2_HAS_RO_COMPAT_FEATURE(is->rfs->old_fs->super,
1726 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
1727 !ext2fs_test_inode_bitmap2(is->rfs->old_fs->inode_map, dir))
1728 ret |= DIRENT_CHANGED;
1733 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1737 #ifdef RESIZE2FS_DEBUG
1738 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1739 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1740 dir, ext2fs_dirent_name_len(dirent), dirent->name,
1741 dirent->inode, new_inode);
1744 dirent->inode = new_inode;
1746 /* Update the directory mtime and ctime */
1747 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1749 inode.i_mtime = inode.i_ctime = time(0);
1750 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1752 return ret | DIRENT_ABORT;
1755 return ret | DIRENT_CHANGED;
1758 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1767 * Now, we iterate over all of the directories to update the
1771 is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
1775 if (rfs->progress) {
1776 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1782 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1783 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1784 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1785 check_and_change_inodes, &is);
1786 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1794 if (rfs->progress && (is.num < is.max_dirs))
1795 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1796 is.max_dirs, is.max_dirs);
1799 ext2fs_free_extent_table(rfs->imap);
1805 /* --------------------------------------------------------------------
1807 * Resize processing, phase 5.
1809 * In this phase we actually move the inode table around, and then
1810 * update the summary statistics. This is scary, since aborting here
1811 * will potentially scramble the filesystem. (We are moving the
1812 * inode tables around in place, and so the potential for lost data,
1813 * or at the very least scrambling the mapping between filenames and
1814 * inode numbers is very high in case of a power failure here.)
1815 * --------------------------------------------------------------------
1820 * A very scary routine --- this one moves the inode table around!!!
1822 * After this you have to use the rfs->new_fs file handle to read and
1825 static errcode_t move_itables(ext2_resize_t rfs)
1829 dgrp_t i, max_groups;
1830 ext2_filsys fs = rfs->new_fs;
1832 blk64_t old_blk, new_blk, blk;
1834 int j, to_move, moved;
1836 max_groups = fs->group_desc_count;
1837 if (max_groups > rfs->old_fs->group_desc_count)
1838 max_groups = rfs->old_fs->group_desc_count;
1840 size = fs->blocksize * fs->inode_blocks_per_group;
1841 if (!rfs->itable_buf) {
1842 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1848 * Figure out how many inode tables we need to move
1850 to_move = moved = 0;
1851 for (i=0; i < max_groups; i++)
1852 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
1853 ext2fs_inode_table_loc(fs, i))
1859 if (rfs->progress) {
1860 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1866 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1868 for (i=0; i < max_groups; i++) {
1869 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
1870 new_blk = ext2fs_inode_table_loc(fs, i);
1871 diff = new_blk - old_blk;
1873 #ifdef RESIZE2FS_DEBUG
1874 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1875 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
1876 i, old_blk, new_blk, diff);
1882 retval = io_channel_read_blk64(fs->io, old_blk,
1883 fs->inode_blocks_per_group,
1888 * The end of the inode table segment often contains
1889 * all zeros, and we're often only moving the inode
1890 * table down a block or two. If so, we can optimize
1891 * things by not rewriting blocks that we know to be zero
1894 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1897 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1898 #ifdef RESIZE2FS_DEBUG
1899 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1900 printf("%d blocks of zeros...\n", n);
1902 num = fs->inode_blocks_per_group;
1906 retval = io_channel_write_blk64(fs->io, new_blk,
1907 num, rfs->itable_buf);
1909 io_channel_write_blk64(fs->io, old_blk,
1910 num, rfs->itable_buf);
1914 retval = io_channel_write_blk64(fs->io,
1915 old_blk + fs->inode_blocks_per_group,
1916 diff, (rfs->itable_buf +
1917 (fs->inode_blocks_per_group - diff) *
1923 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1924 j < fs->inode_blocks_per_group ; j++, blk++)
1925 ext2fs_block_alloc_stats2(fs, blk, -1);
1927 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
1928 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1929 ext2fs_mark_super_dirty(rfs->old_fs);
1930 ext2fs_flush(rfs->old_fs);
1932 if (rfs->progress) {
1933 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1939 mark_table_blocks(fs, fs->block_map);
1941 #ifdef RESIZE2FS_DEBUG
1942 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1943 printf("Inode table move finished.\n");
1952 * This function is used when expanding a file system. It frees the
1953 * superblock and block group descriptor blocks from the block group
1954 * which is no longer the last block group.
1956 static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs)
1958 ext2_filsys fs = rfs->new_fs;
1959 ext2_filsys old_fs = rfs->old_fs;
1961 dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1;
1962 dgrp_t last_bg = fs->group_desc_count - 1;
1963 blk64_t sb, old_desc;
1966 if (!(fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2))
1969 if (last_bg <= old_last_bg)
1972 if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
1973 fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
1976 if (old_fs->super->s_backup_bgs[0] != old_last_bg &&
1977 old_fs->super->s_backup_bgs[1] != old_last_bg)
1980 if (fs->super->s_backup_bgs[0] == old_last_bg ||
1981 fs->super->s_backup_bgs[1] == old_last_bg)
1984 retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, old_last_bg,
1985 &sb, &old_desc, NULL, &num);
1990 ext2fs_unmark_block_bitmap2(fs->block_map, sb);
1992 ext2fs_unmark_block_bitmap_range2(fs->block_map, old_desc, num);
1997 * This function is used when shrinking a file system. We need to
1998 * utilize blocks from what will be the new last block group for the
1999 * backup superblock and block group descriptor blocks.
2000 * Unfortunately, those blocks may be used by other files or fs
2001 * metadata blocks. We need to mark them as being in use.
2003 static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
2004 ext2fs_block_bitmap meta_bmap)
2006 ext2_filsys fs = rfs->new_fs;
2007 ext2_filsys old_fs = rfs->old_fs;
2009 dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1;
2010 dgrp_t last_bg = fs->group_desc_count - 1;
2012 blk64_t blk, sb, old_desc;
2016 if (!(fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2))
2019 if (last_bg >= old_last_bg)
2022 if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
2023 fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
2026 if (fs->super->s_backup_bgs[0] != last_bg &&
2027 fs->super->s_backup_bgs[1] != last_bg)
2030 if (old_fs->super->s_backup_bgs[0] == last_bg ||
2031 old_fs->super->s_backup_bgs[1] == last_bg)
2034 retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, last_bg,
2035 &sb, &old_desc, NULL, &num);
2040 fputs(_("Should never happen! No sb in last super_sparse bg?\n"),
2044 if (old_desc != sb+1) {
2045 fputs(_("Should never happen! Unexpected old_desc in "
2046 "super_sparse bg?\n"),
2050 num = (old_desc) ? num : 1;
2052 /* Reserve the backup blocks */
2053 ext2fs_mark_block_bitmap_range2(fs->block_map, sb, num);
2055 for (g = 0; g < fs->group_desc_count; g++) {
2058 mb = ext2fs_block_bitmap_loc(fs, g);
2059 if ((mb >= sb) && (mb < sb + num)) {
2060 ext2fs_block_bitmap_loc_set(fs, g, 0);
2063 mb = ext2fs_inode_bitmap_loc(fs, g);
2064 if ((mb >= sb) && (mb < sb + num)) {
2065 ext2fs_inode_bitmap_loc_set(fs, g, 0);
2068 mb = ext2fs_inode_table_loc(fs, g);
2069 if ((mb < sb + num) &&
2070 (sb < mb + fs->inode_blocks_per_group)) {
2071 ext2fs_inode_table_loc_set(fs, g, 0);
2075 retval = ext2fs_allocate_group_table(fs, g, 0);
2081 for (blk = sb, i = 0; i < num; blk++, i++) {
2082 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
2083 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
2084 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
2085 rfs->needed_blocks++;
2087 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
2093 * Fix the resize inode
2095 static errcode_t fix_resize_inode(ext2_filsys fs)
2097 struct ext2_inode inode;
2099 char *block_buf = NULL;
2101 if (!(fs->super->s_feature_compat &
2102 EXT2_FEATURE_COMPAT_RESIZE_INODE))
2105 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
2106 if (retval) goto errout;
2108 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
2109 if (retval) goto errout;
2111 ext2fs_iblk_set(fs, &inode, 1);
2113 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
2114 if (retval) goto errout;
2116 if (!inode.i_block[EXT2_DIND_BLOCK]) {
2118 * Avoid zeroing out block #0; that's rude. This
2119 * should never happen anyway since the filesystem
2120 * should be fsck'ed and we assume it is consistent.
2122 fprintf(stderr, "%s",
2123 _("Should never happen: resize inode corrupt!\n"));
2127 memset(block_buf, 0, fs->blocksize);
2129 retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK],
2131 if (retval) goto errout;
2133 retval = ext2fs_create_resize_inode(fs);
2139 ext2fs_free_mem(&block_buf);
2144 * Finally, recalculate the summary information
2146 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
2150 unsigned int group = 0;
2151 unsigned int count = 0;
2152 blk64_t total_blocks_free = 0;
2153 int total_inodes_free = 0;
2156 blk64_t super_blk, old_desc_blk, new_desc_blk;
2157 int old_desc_blocks;
2160 * First calculate the block statistics
2162 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2163 ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
2165 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2166 old_desc_blocks = fs->super->s_first_meta_bg;
2168 old_desc_blocks = fs->desc_blocks +
2169 fs->super->s_reserved_gdt_blocks;
2170 for (blk = B2C(fs->super->s_first_data_block);
2171 blk < ext2fs_blocks_count(fs->super);
2172 blk += EXT2FS_CLUSTER_RATIO(fs)) {
2174 !(EQ_CLSTR(blk, super_blk) ||
2175 ((old_desc_blk && old_desc_blocks &&
2176 GE_CLSTR(blk, old_desc_blk) &&
2177 LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) ||
2178 ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) ||
2179 EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) ||
2180 EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) ||
2181 ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) &&
2182 LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group)
2183 + fs->inode_blocks_per_group))))) ||
2184 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
2186 total_blocks_free++;
2189 if ((count == fs->super->s_clusters_per_group) ||
2190 EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) {
2191 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
2192 ext2fs_group_desc_csum_set(fs, group);
2194 if (group >= fs->group_desc_count)
2198 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2199 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
2202 if (fs->super->s_feature_incompat &
2203 EXT2_FEATURE_INCOMPAT_META_BG)
2204 old_desc_blocks = fs->super->s_first_meta_bg;
2206 old_desc_blocks = fs->desc_blocks +
2207 fs->super->s_reserved_gdt_blocks;
2210 total_blocks_free = C2B(total_blocks_free);
2211 ext2fs_free_blocks_count_set(fs->super, total_blocks_free);
2214 * Next, calculate the inode statistics
2220 /* Protect loop from wrap-around if s_inodes_count maxed */
2221 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2222 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2224 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2226 total_inodes_free++;
2229 if ((count == fs->super->s_inodes_per_group) ||
2230 (ino == fs->super->s_inodes_count)) {
2231 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
2232 ext2fs_group_desc_csum_set(fs, group);
2234 if (group >= fs->group_desc_count)
2238 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2241 fs->super->s_free_inodes_count = total_inodes_free;
2242 ext2fs_mark_super_dirty(fs);
2247 * Journal may have been relocated; update the backup journal blocks
2248 * in the superblock.
2250 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
2253 struct ext2_inode inode;
2255 if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
2258 /* External journal? Nothing to do. */
2259 if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
2262 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
2265 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
2266 fs->super->s_jnl_blocks[15] = inode.i_size_high;
2267 fs->super->s_jnl_blocks[16] = inode.i_size;
2268 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
2269 ext2fs_mark_super_dirty(fs);
2273 static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
2274 int old_desc_blocks)
2276 blk64_t super_blk, old_desc_blk, new_desc_blk;
2279 /* inode table blocks plus allocation bitmaps */
2280 overhead = fs->inode_blocks_per_group + 2;
2282 ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
2283 &old_desc_blk, &new_desc_blk, 0);
2284 if ((grp == 0) || super_blk)
2287 overhead += old_desc_blocks;
2288 else if (new_desc_blk)
2295 * calcluate the minimum number of blocks the given fs can be resized to
2297 blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
2299 ext2_ino_t inode_count;
2301 blk64_t blks_needed, data_blocks;
2302 blk64_t grp, data_needed, last_start;
2303 blk64_t overhead = 0;
2304 int old_desc_blocks;
2305 int extra_groups = 0;
2306 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2309 * first figure out how many group descriptors we need to
2310 * handle the number of inodes we have
2312 inode_count = fs->super->s_inodes_count -
2313 fs->super->s_free_inodes_count;
2314 blks_needed = ext2fs_div_ceil(inode_count,
2315 fs->super->s_inodes_per_group) *
2316 EXT2_BLOCKS_PER_GROUP(fs->super);
2317 groups = ext2fs_div64_ceil(blks_needed,
2318 EXT2_BLOCKS_PER_GROUP(fs->super));
2319 #ifdef RESIZE2FS_DEBUG
2320 if (flags & RESIZE_DEBUG_MIN_CALC)
2321 printf("fs has %d inodes, %d groups required.\n",
2322 inode_count, groups);
2326 * number of old-style block group descriptor blocks
2328 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2329 old_desc_blocks = fs->super->s_first_meta_bg;
2331 old_desc_blocks = fs->desc_blocks +
2332 fs->super->s_reserved_gdt_blocks;
2334 /* calculate how many blocks are needed for data */
2335 data_needed = ext2fs_blocks_count(fs->super) -
2336 ext2fs_free_blocks_count(fs->super);
2338 for (grp = 0; grp < fs->group_desc_count; grp++)
2339 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
2340 #ifdef RESIZE2FS_DEBUG
2341 if (flags & RESIZE_DEBUG_MIN_CALC)
2342 printf("fs requires %llu data blocks.\n", data_needed);
2346 * For ext4 we need to allow for up to a flex_bg worth of
2347 * inode tables of slack space so the resize operation can be
2348 * guaranteed to finish.
2350 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2351 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
2352 data_needed += fs->inode_blocks_per_group * extra_groups;
2353 extra_groups = groups % flexbg_size;
2357 * figure out how many data blocks we have given the number of groups
2358 * we need for our inodes
2360 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
2362 for (grp = 0; grp < groups; grp++) {
2363 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
2366 * we want to keep track of how much data we can store in
2367 * the groups leading up to the last group so we can determine
2368 * how big the last group needs to be
2370 if (grp != (groups - 1))
2371 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
2374 data_blocks -= overhead;
2376 #ifdef RESIZE2FS_DEBUG
2377 if (flags & RESIZE_DEBUG_MIN_CALC)
2378 printf("With %d group(s), we have %llu blocks available.\n",
2379 groups, data_blocks);
2383 * if we need more group descriptors in order to accomodate our data
2384 * then we need to add them here
2386 while (data_needed > data_blocks) {
2387 blk64_t remainder = data_needed - data_blocks;
2390 /* figure out how many more groups we need for the data */
2391 extra_grps = ext2fs_div64_ceil(remainder,
2392 EXT2_BLOCKS_PER_GROUP(fs->super));
2394 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
2396 /* ok we have to account for the last group */
2397 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2398 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
2400 for (grp = groups; grp < groups+extra_grps; grp++) {
2401 overhead = calc_group_overhead(fs, grp,
2405 * again, we need to see how much data we cram into
2406 * all of the groups leading up to the last group
2408 if (grp != (groups + extra_grps - 1))
2409 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
2412 data_blocks -= overhead;
2415 groups += extra_grps;
2416 extra_groups += extra_grps;
2417 if (fs->super->s_feature_incompat
2418 & EXT4_FEATURE_INCOMPAT_FLEX_BG
2419 && extra_groups > flexbg_size) {
2421 * For ext4 we need to allow for up to a flex_bg worth
2422 * of inode tables of slack space so the resize
2423 * operation can be guaranteed to finish.
2425 extra_groups = flexbg_size -
2426 (groups & (flexbg_size - 1));
2427 data_needed += (fs->inode_blocks_per_group *
2429 extra_groups = groups % flexbg_size;
2431 #ifdef RESIZE2FS_DEBUG
2432 if (flags & RESIZE_DEBUG_MIN_CALC)
2433 printf("Added %d extra group(s), "
2434 "data_needed %llu, data_blocks %llu, "
2435 "last_start %llu\n",
2436 extra_grps, data_needed, data_blocks,
2441 /* now for the fun voodoo */
2442 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2443 #ifdef RESIZE2FS_DEBUG
2444 if (flags & RESIZE_DEBUG_MIN_CALC)
2445 printf("Last group's overhead is %llu\n", overhead);
2449 * if this is the case then the last group is going to have data in it
2450 * so we need to adjust the size of the last group accordingly
2452 if (last_start < data_needed) {
2453 blk64_t remainder = data_needed - last_start;
2455 #ifdef RESIZE2FS_DEBUG
2456 if (flags & RESIZE_DEBUG_MIN_CALC)
2457 printf("Need %llu data blocks in last group\n",
2461 * 50 is a magic number that mkfs/resize uses to see if its
2462 * even worth making/resizing the fs. basically you need to
2463 * have at least 50 blocks in addition to the blocks needed
2464 * for the metadata in the last group
2467 overhead += remainder;
2473 overhead += fs->super->s_first_data_block;
2474 #ifdef RESIZE2FS_DEBUG
2475 if (flags & RESIZE_DEBUG_MIN_CALC)
2476 printf("Final size of last group is %lld\n", overhead);
2480 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2481 * only do groups-1, and then add the number of blocks needed to
2482 * handle the group descriptor metadata+data that we need
2484 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2485 blks_needed += overhead;
2488 * Make sure blks_needed covers the end of the inode table in
2489 * the last block group.
2491 overhead = ext2fs_inode_table_loc(fs, groups-1) +
2492 fs->inode_blocks_per_group;
2493 if (blks_needed < overhead)
2494 blks_needed = overhead;
2496 #ifdef RESIZE2FS_DEBUG
2497 if (flags & RESIZE_DEBUG_MIN_CALC)
2498 printf("Estimated blocks needed: %llu\n", blks_needed);
2502 * If at this point we've already added up more "needed" than
2503 * the current size, just return current size as minimum.
2505 if (blks_needed >= ext2fs_blocks_count(fs->super))
2506 return ext2fs_blocks_count(fs->super);
2508 * We need to reserve a few extra blocks if extents are
2509 * enabled, in case we need to grow the extent tree. The more
2510 * we shrink the file system, the more space we need.
2512 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
2513 blk64_t safe_margin = (ext2fs_blocks_count(fs->super) -
2515 #ifdef RESIZE2FS_DEBUG
2516 if (flags & RESIZE_DEBUG_MIN_CALC)
2517 printf("Extents safety margin: %llu\n", safe_margin);
2519 blks_needed += safe_margin;