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);
56 * Some helper CPP macros
58 #define IS_BLOCK_BM(fs, i, blk) ((blk) == ext2fs_block_bitmap_loc((fs),(i)))
59 #define IS_INODE_BM(fs, i, blk) ((blk) == ext2fs_inode_bitmap_loc((fs),(i)))
61 #define IS_INODE_TB(fs, i, blk) (((blk) >= ext2fs_inode_table_loc((fs), (i))) && \
62 ((blk) < (ext2fs_inode_table_loc((fs), (i)) + \
63 (fs)->inode_blocks_per_group)))
68 * This is the top-level routine which does the dirty deed....
70 errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
71 errcode_t (*progress)(ext2_resize_t rfs, int pass,
73 unsigned long max_val))
78 retval = ext2fs_read_bitmaps(fs);
82 fs->super->s_state |= EXT2_ERROR_FS;
83 ext2fs_mark_super_dirty(fs);
87 * Create the data structure
89 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
92 memset(rfs, 0, sizeof(struct ext2_resize_struct));
94 fix_uninit_block_bitmaps(fs);
99 rfs->progress = progress;
100 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
104 retval = adjust_superblock(rfs, *new_size);
108 fix_uninit_block_bitmaps(rfs->new_fs);
109 /* Clear the block bitmap uninit flag for the last block group */
110 ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
111 EXT2_BG_BLOCK_UNINIT);
113 *new_size = ext2fs_blocks_count(rfs->new_fs->super);
115 retval = blocks_to_move(rfs);
119 #ifdef RESIZE2FS_DEBUG
120 if (rfs->flags & RESIZE_DEBUG_BMOVE)
121 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
122 ext2fs_free_blocks_count(rfs->old_fs->super),
123 ext2fs_free_blocks_count(rfs->new_fs->super),
127 retval = block_mover(rfs);
131 retval = inode_scan_and_fix(rfs);
135 retval = inode_ref_fix(rfs);
139 retval = move_itables(rfs);
143 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
147 retval = fix_resize_inode(rfs->new_fs);
151 retval = fix_sb_journal_backup(rfs->new_fs);
155 rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
156 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
157 retval = ext2fs_close(rfs->new_fs);
163 ext2fs_free(rfs->old_fs);
165 ext2fs_free_mem(&rfs->itable_buf);
166 if (rfs->reserve_blocks)
167 ext2fs_free_block_bitmap(rfs->reserve_blocks);
168 if (rfs->move_blocks)
169 ext2fs_free_block_bitmap(rfs->move_blocks);
170 ext2fs_free_mem(&rfs);
176 ext2fs_free(rfs->new_fs);
178 ext2fs_free_mem(&rfs->itable_buf);
179 ext2fs_free_mem(&rfs);
184 * Clean up the bitmaps for unitialized bitmaps
186 static void fix_uninit_block_bitmaps(ext2_filsys fs)
188 blk64_t i, blk, super_blk, old_desc_blk, new_desc_blk;
192 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
193 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
196 for (g=0; g < fs->group_desc_count; g++) {
197 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
200 blk = ext2fs_group_first_block2(fs, g);
202 ext2fs_super_and_bgd_loc2(fs, g, &super_blk,
203 &old_desc_blk, &new_desc_blk, 0);
205 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
206 old_desc_blocks = fs->super->s_first_meta_bg;
208 old_desc_blocks = fs->desc_blocks +
209 fs->super->s_reserved_gdt_blocks;
211 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
212 if (blk >= ext2fs_blocks_count(fs->super))
214 if ((blk == super_blk) ||
215 (old_desc_blk && old_desc_blocks &&
216 (blk >= old_desc_blk) &&
217 (blk < old_desc_blk + old_desc_blocks)) ||
218 (new_desc_blk && (blk == new_desc_blk)) ||
219 (blk == ext2fs_block_bitmap_loc(fs, g)) ||
220 (blk == ext2fs_inode_bitmap_loc(fs, g)) ||
221 (blk >= ext2fs_inode_table_loc(fs, g) &&
222 (blk < ext2fs_inode_table_loc(fs, g)
223 + fs->inode_blocks_per_group)))
224 ext2fs_fast_mark_block_bitmap2(fs->block_map, blk);
226 ext2fs_fast_unmark_block_bitmap2(fs->block_map, blk);
231 /* --------------------------------------------------------------------
233 * Resize processing, phase 1.
235 * In this phase we adjust the in-memory superblock information, and
236 * initialize any new parts of the inode table. The new parts of the
237 * inode table are created in virgin disk space, so we can abort here
238 * without any side effects.
239 * --------------------------------------------------------------------
243 * If the group descriptor's bitmap and inode table blocks are valid,
244 * release them in the new filesystem data structure, and mark them as
245 * reserved so the old inode table blocks don't get overwritten.
247 static void free_gdp_blocks(ext2_filsys fs,
248 ext2fs_block_bitmap reserve_blocks,
249 struct ext2_group_desc *gdp)
254 if (gdp->bg_block_bitmap &&
255 (gdp->bg_block_bitmap < ext2fs_blocks_count(fs->super))) {
256 ext2fs_block_alloc_stats(fs, gdp->bg_block_bitmap, -1);
257 ext2fs_mark_block_bitmap2(reserve_blocks,
258 gdp->bg_block_bitmap);
261 if (gdp->bg_inode_bitmap &&
262 (gdp->bg_inode_bitmap < ext2fs_blocks_count(fs->super))) {
263 ext2fs_block_alloc_stats(fs, gdp->bg_inode_bitmap, -1);
264 ext2fs_mark_block_bitmap2(reserve_blocks,
265 gdp->bg_inode_bitmap);
268 if (gdp->bg_inode_table == 0 ||
269 (gdp->bg_inode_table >= ext2fs_blocks_count(fs->super)))
272 for (blk = gdp->bg_inode_table, j = 0;
273 j < fs->inode_blocks_per_group; j++, blk++) {
274 if (blk >= ext2fs_blocks_count(fs->super))
276 ext2fs_block_alloc_stats(fs, blk, -1);
277 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
282 * This routine is shared by the online and offline resize routines.
283 * All of the information which is adjusted in memory is done here.
285 * The reserve_blocks parameter is only needed when shrinking the
288 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
289 ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
292 blk64_t overhead = 0;
294 blk64_t blk, group_block;
296 blk64_t old_numblocks, numblocks, adjblocks;
297 unsigned long i, j, old_desc_blocks;
298 unsigned int meta_bg, meta_bg_size;
299 int has_super, csum_flag;
300 unsigned long long new_inodes; /* u64 to check for overflow */
303 ext2fs_blocks_count_set(fs->super, new_size);
306 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
307 fs->super->s_first_data_block,
308 EXT2_BLOCKS_PER_GROUP(fs->super));
309 if (fs->group_desc_count == 0)
310 return EXT2_ET_TOOSMALL;
311 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
312 EXT2_DESC_PER_BLOCK(fs->super));
315 * Overhead is the number of bookkeeping blocks per group. It
316 * includes the superblock backup, the group descriptor
317 * backups, the inode bitmap, the block bitmap, and the inode
320 overhead = (int) (2 + fs->inode_blocks_per_group);
322 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
323 overhead += 1 + fs->desc_blocks +
324 fs->super->s_reserved_gdt_blocks;
327 * See if the last group is big enough to support the
328 * necessary data structures. If not, we need to get rid of
331 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
332 fs->super->s_blocks_per_group;
333 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
334 return EXT2_ET_TOOSMALL;
335 if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
336 ext2fs_blocks_count_set(fs->super,
337 ext2fs_blocks_count(fs->super) - rem);
341 * Adjust the number of inodes
343 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
344 if (new_inodes > ~0U) {
345 fprintf(stderr, _("inodes (%llu) must be less than %u"),
347 return EXT2_ET_TOO_MANY_INODES;
349 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
350 fs->group_desc_count;
353 * Adjust the number of free blocks
355 blk = ext2fs_blocks_count(old_fs->super);
356 if (blk > ext2fs_blocks_count(fs->super))
357 ext2fs_free_blocks_count_set(fs->super,
358 ext2fs_free_blocks_count(fs->super) -
359 (blk - ext2fs_blocks_count(fs->super)));
361 ext2fs_free_blocks_count_set(fs->super,
362 ext2fs_free_blocks_count(fs->super) +
363 (ext2fs_blocks_count(fs->super) - blk));
366 * Adjust the number of reserved blocks
368 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
369 ext2fs_blocks_count(old_fs->super);
370 ext2fs_r_blocks_count_set(fs->super,
371 (percent * ext2fs_blocks_count(fs->super) /
375 * Adjust the bitmaps for size
377 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
378 fs->super->s_inodes_count,
380 if (retval) goto errout;
382 real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) *
383 fs->group_desc_count)) - 1 +
384 fs->super->s_first_data_block;
385 retval = ext2fs_resize_block_bitmap2(ext2fs_blocks_count(fs->super)-1,
386 real_end, fs->block_map);
388 if (retval) goto errout;
391 * Reallocate the group descriptors as necessary.
393 if (old_fs->desc_blocks != fs->desc_blocks) {
394 retval = ext2fs_resize_mem(old_fs->desc_blocks *
396 fs->desc_blocks * fs->blocksize,
400 if (fs->desc_blocks > old_fs->desc_blocks)
401 memset((char *) fs->group_desc +
402 (old_fs->desc_blocks * fs->blocksize), 0,
403 (fs->desc_blocks - old_fs->desc_blocks) *
408 * If the resize_inode feature is set, and we are changing the
409 * number of descriptor blocks, then adjust
410 * s_reserved_gdt_blocks if possible to avoid needing to move
411 * the inode table either now or in the future.
413 if ((fs->super->s_feature_compat &
414 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
415 (old_fs->desc_blocks != fs->desc_blocks)) {
418 new = ((int) fs->super->s_reserved_gdt_blocks) +
419 (old_fs->desc_blocks - fs->desc_blocks);
422 if (new > (int) fs->blocksize/4)
423 new = fs->blocksize/4;
424 fs->super->s_reserved_gdt_blocks = new;
428 * If we are shrinking the number of block groups, we're done
431 if (old_fs->group_desc_count > fs->group_desc_count) {
433 * Check the block groups that we are chopping off
434 * and free any blocks associated with their metadata
436 for (i = fs->group_desc_count;
437 i < old_fs->group_desc_count; i++) {
438 free_gdp_blocks(fs, reserve_blocks,
439 ext2fs_group_desc(old_fs,
440 old_fs->group_desc, i));
447 * Fix the count of the last (old) block group
449 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
450 old_fs->super->s_first_data_block) %
451 old_fs->super->s_blocks_per_group;
453 old_numblocks = old_fs->super->s_blocks_per_group;
454 if (old_fs->group_desc_count == fs->group_desc_count) {
455 numblocks = (ext2fs_blocks_count(fs->super) -
456 fs->super->s_first_data_block) %
457 fs->super->s_blocks_per_group;
459 numblocks = fs->super->s_blocks_per_group;
461 numblocks = fs->super->s_blocks_per_group;
462 i = old_fs->group_desc_count - 1;
463 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
464 ext2fs_group_desc_csum_set(fs, i);
467 * If the number of block groups is staying the same, we're
468 * done and can exit now. (If the number block groups is
469 * shrinking, we had exited earlier.)
471 if (old_fs->group_desc_count >= fs->group_desc_count) {
477 * Initialize the new block group descriptors
479 group_block = fs->super->s_first_data_block +
480 old_fs->group_desc_count * fs->super->s_blocks_per_group;
482 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
483 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
484 if (access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0)
485 lazy_itable_init = 1;
486 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
487 old_desc_blocks = fs->super->s_first_meta_bg;
489 old_desc_blocks = fs->desc_blocks +
490 fs->super->s_reserved_gdt_blocks;
491 for (i = old_fs->group_desc_count;
492 i < fs->group_desc_count; i++) {
493 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
494 sizeof(struct ext2_group_desc));
497 ext2fs_bg_flags_zap(fs, i);
499 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
500 if (!lazy_itable_init)
501 ext2fs_bg_flags_set(fs, i,
502 EXT2_BG_INODE_ZEROED);
503 ext2fs_bg_itable_unused_set(fs, i,
504 fs->super->s_inodes_per_group);
507 numblocks = ext2fs_group_blocks_count(fs, i);
508 if ((i < fs->group_desc_count - 1) && csum_flag)
509 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
511 has_super = ext2fs_bg_has_super(fs, i);
513 ext2fs_block_alloc_stats2(fs, group_block, +1);
516 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
517 meta_bg = i / meta_bg_size;
518 if (!(fs->super->s_feature_incompat &
519 EXT2_FEATURE_INCOMPAT_META_BG) ||
520 (meta_bg < fs->super->s_first_meta_bg)) {
522 for (j=0; j < old_desc_blocks; j++)
523 ext2fs_block_alloc_stats2(fs,
524 group_block + 1 + j, +1);
525 adjblocks += old_desc_blocks;
530 if (((i % meta_bg_size) == 0) ||
531 ((i % meta_bg_size) == 1) ||
532 ((i % meta_bg_size) == (meta_bg_size-1)))
533 ext2fs_block_alloc_stats2(fs,
534 group_block + has_super, +1);
537 adjblocks += 2 + fs->inode_blocks_per_group;
539 numblocks -= adjblocks;
540 ext2fs_free_blocks_count_set(fs->super,
541 ext2fs_free_blocks_count(fs->super) - adjblocks);
542 fs->super->s_free_inodes_count +=
543 fs->super->s_inodes_per_group;
544 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
545 ext2fs_bg_free_inodes_count_set(fs, i,
546 fs->super->s_inodes_per_group);
547 ext2fs_bg_used_dirs_count_set(fs, i, 0);
548 ext2fs_group_desc_csum_set(fs, i);
550 retval = ext2fs_allocate_group_table(fs, i, 0);
551 if (retval) goto errout;
553 group_block += fs->super->s_blocks_per_group;
562 * This routine adjusts the superblock and other data structures, both
563 * in disk as well as in memory...
565 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
572 unsigned long max_group;
575 ext2fs_mark_super_dirty(fs);
576 ext2fs_mark_bb_dirty(fs);
577 ext2fs_mark_ib_dirty(fs);
579 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
580 &rfs->reserve_blocks);
584 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
589 * Check to make sure there are enough inodes
591 if ((rfs->old_fs->super->s_inodes_count -
592 rfs->old_fs->super->s_free_inodes_count) >
593 rfs->new_fs->super->s_inodes_count) {
599 * If we are shrinking the number block groups, we're done and
602 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
608 * If the number of block groups is staying the same, we're
609 * done and can exit now. (If the number block groups is
610 * shrinking, we had exited earlier.)
612 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
618 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
619 * supports lazy inode initialization, we can skip
620 * initializing the inode table.
622 if (lazy_itable_init &&
623 EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
624 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
630 * Initialize the inode table
632 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
637 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
638 group_block = fs->super->s_first_data_block +
639 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
641 adj = rfs->old_fs->group_desc_count;
642 max_group = fs->group_desc_count - adj;
644 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
649 for (i = rfs->old_fs->group_desc_count;
650 i < fs->group_desc_count; i++) {
652 * Write out the new inode table
654 retval = io_channel_write_blk64(fs->io,
655 ext2fs_inode_table_loc(fs, i),
656 fs->inode_blocks_per_group,
658 if (retval) goto errout;
660 io_channel_flush(fs->io);
662 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
663 i - adj + 1, max_group);
667 group_block += fs->super->s_blocks_per_group;
669 io_channel_flush(fs->io);
676 /* --------------------------------------------------------------------
678 * Resize processing, phase 2.
680 * In this phase we adjust determine which blocks need to be moved, in
681 * blocks_to_move(). We then copy the blocks to their ultimate new
682 * destinations using block_mover(). Since we are copying blocks to
683 * their new locations, again during this pass we can abort without
685 * --------------------------------------------------------------------
689 * This helper function creates a block bitmap with all of the
690 * filesystem meta-data blocks.
692 static errcode_t mark_table_blocks(ext2_filsys fs,
693 ext2fs_block_bitmap bmap)
699 for (i = 0; i < fs->group_desc_count; i++) {
700 ext2fs_reserve_super_and_bgd(fs, i, bmap);
703 * Mark the blocks used for the inode table
705 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
706 j < (unsigned int) fs->inode_blocks_per_group;
708 ext2fs_mark_block_bitmap2(bmap, b);
711 * Mark block used for the block bitmap
713 ext2fs_mark_block_bitmap2(bmap,
714 ext2fs_block_bitmap_loc(fs, i));
717 * Mark block used for the inode bitmap
719 ext2fs_mark_block_bitmap2(bmap,
720 ext2fs_inode_bitmap_loc(fs, i));
726 * This function checks to see if a particular block (either a
727 * superblock or a block group descriptor) overlaps with an inode or
728 * block bitmap block, or with the inode table.
730 static void mark_fs_metablock(ext2_resize_t rfs,
731 ext2fs_block_bitmap meta_bmap,
732 int group, blk64_t blk)
734 ext2_filsys fs = rfs->new_fs;
736 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
737 ext2fs_block_alloc_stats2(fs, blk, +1);
740 * Check to see if we overlap with the inode or block bitmap,
741 * or the inode tables. If not, and the block is in use, then
742 * mark it as a block to be moved.
744 if (IS_BLOCK_BM(fs, group, blk)) {
745 ext2fs_block_bitmap_loc_set(fs, group, 0);
746 rfs->needed_blocks++;
749 if (IS_INODE_BM(fs, group, blk)) {
750 ext2fs_inode_bitmap_loc_set(fs, group, 0);
751 rfs->needed_blocks++;
754 if (IS_INODE_TB(fs, group, blk)) {
755 ext2fs_inode_table_loc_set(fs, group, 0);
756 rfs->needed_blocks++;
759 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
762 for (i=0; i < rfs->old_fs->group_desc_count; i++) {
763 if (IS_BLOCK_BM(fs, i, blk)) {
764 ext2fs_block_bitmap_loc_set(fs, i, 0);
765 rfs->needed_blocks++;
768 if (IS_INODE_BM(fs, i, blk)) {
769 ext2fs_inode_bitmap_loc_set(fs, i, 0);
770 rfs->needed_blocks++;
773 if (IS_INODE_TB(fs, i, blk)) {
774 ext2fs_inode_table_loc_set(fs, i, 0);
775 rfs->needed_blocks++;
780 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
781 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
782 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
784 * If the block bitmap is uninitialized, which means
785 * nothing other than standard metadata in use.
788 } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
789 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
790 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
791 rfs->needed_blocks++;
797 * This routine marks and unmarks reserved blocks in the new block
798 * bitmap. It also determines which blocks need to be moved and
799 * places this information into the move_blocks bitmap.
801 static errcode_t blocks_to_move(ext2_resize_t rfs)
804 dgrp_t i, max_groups, g;
805 blk64_t blk, group_blk;
806 blk64_t old_blocks, new_blocks;
807 unsigned int meta_bg, meta_bg_size;
809 ext2_filsys fs, old_fs;
810 ext2fs_block_bitmap meta_bmap;
814 old_fs = rfs->old_fs;
815 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
818 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
823 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
828 retval = mark_table_blocks(old_fs, meta_bmap);
835 * If we're shrinking the filesystem, we need to move all of
836 * the blocks that don't fit any more
838 for (blk = ext2fs_blocks_count(fs->super);
839 blk < ext2fs_blocks_count(old_fs->super); blk++) {
840 g = ext2fs_group_of_blk2(fs, blk);
841 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
842 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
843 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
845 * The block bitmap is uninitialized, so skip
846 * to the next block group.
848 blk = ext2fs_group_first_block2(fs, g+1) - 1;
851 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
852 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
853 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
854 rfs->needed_blocks++;
856 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
859 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
860 old_blocks = old_fs->super->s_first_meta_bg;
861 new_blocks = fs->super->s_first_meta_bg;
863 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
864 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
867 if (old_blocks == new_blocks) {
872 max_groups = fs->group_desc_count;
873 if (max_groups > old_fs->group_desc_count)
874 max_groups = old_fs->group_desc_count;
875 group_blk = old_fs->super->s_first_data_block;
877 * If we're reducing the number of descriptor blocks, this
878 * makes life easy. :-) We just have to mark some extra
881 if (old_blocks > new_blocks) {
882 for (i = 0; i < max_groups; i++) {
883 if (!ext2fs_bg_has_super(fs, i)) {
884 group_blk += fs->super->s_blocks_per_group;
887 for (blk = group_blk+1+new_blocks;
888 blk < group_blk+1+old_blocks; blk++) {
889 ext2fs_block_alloc_stats2(fs, blk, -1);
890 rfs->needed_blocks--;
892 group_blk += fs->super->s_blocks_per_group;
898 * If we're increasing the number of descriptor blocks, life
899 * gets interesting....
901 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
902 flex_bg = fs->super->s_feature_incompat &
903 EXT4_FEATURE_INCOMPAT_FLEX_BG;
904 /* first reserve all of the existing fs meta blocks */
905 for (i = 0; i < max_groups; i++) {
906 has_super = ext2fs_bg_has_super(fs, i);
908 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
910 meta_bg = i / meta_bg_size;
911 if (!(fs->super->s_feature_incompat &
912 EXT2_FEATURE_INCOMPAT_META_BG) ||
913 (meta_bg < fs->super->s_first_meta_bg)) {
915 for (blk = group_blk+1;
916 blk < group_blk + 1 + new_blocks; blk++)
917 mark_fs_metablock(rfs, meta_bmap,
923 if (((i % meta_bg_size) == 0) ||
924 ((i % meta_bg_size) == 1) ||
925 ((i % meta_bg_size) == (meta_bg_size-1)))
926 mark_fs_metablock(rfs, meta_bmap, i,
927 group_blk + has_super);
931 * Reserve the existing meta blocks that we know
932 * aren't to be moved.
934 * For flex_bg file systems, in order to avoid
935 * overwriting fs metadata (especially inode table
936 * blocks) belonging to a different block group when
937 * we are relocating the inode tables, we need to
938 * reserve all existing fs metadata blocks.
940 if (ext2fs_block_bitmap_loc(fs, i))
941 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
942 ext2fs_block_bitmap_loc(fs, i));
943 else if (flex_bg && i < old_fs->group_desc_count)
944 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
945 ext2fs_block_bitmap_loc(old_fs, i));
947 if (ext2fs_inode_bitmap_loc(fs, i))
948 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
949 ext2fs_inode_bitmap_loc(fs, i));
950 else if (flex_bg && i < old_fs->group_desc_count)
951 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
952 ext2fs_inode_bitmap_loc(old_fs, i));
954 if (ext2fs_inode_table_loc(fs, i))
955 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
956 j < fs->inode_blocks_per_group ; j++, blk++)
957 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
959 else if (flex_bg && i < old_fs->group_desc_count)
960 for (blk = ext2fs_inode_table_loc(old_fs, i), j=0;
961 j < old_fs->inode_blocks_per_group ; j++, blk++)
962 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
965 group_blk += rfs->new_fs->super->s_blocks_per_group;
968 /* Allocate the missing data structures */
969 for (i = 0; i < max_groups; i++) {
970 if (ext2fs_inode_table_loc(fs, i) &&
971 ext2fs_inode_bitmap_loc(fs, i) &&
972 ext2fs_block_bitmap_loc(fs, i))
975 retval = ext2fs_allocate_group_table(fs, i,
976 rfs->reserve_blocks);
981 * For those structures that have changed, we need to
984 if (ext2fs_block_bitmap_loc(old_fs, i) !=
985 (blk = ext2fs_block_bitmap_loc(fs, i))) {
986 ext2fs_block_alloc_stats2(fs, blk, +1);
987 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
988 !ext2fs_test_block_bitmap2(meta_bmap, blk))
989 ext2fs_mark_block_bitmap2(rfs->move_blocks,
992 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
993 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
994 ext2fs_block_alloc_stats2(fs, blk, +1);
995 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
996 !ext2fs_test_block_bitmap2(meta_bmap, blk))
997 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1002 * The inode table, if we need to relocate it, is
1003 * handled specially. We have to reserve the blocks
1004 * for both the old and the new inode table, since we
1005 * can't have the inode table be destroyed during the
1006 * block relocation phase.
1008 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
1009 continue; /* inode table not moved */
1011 rfs->needed_blocks += fs->inode_blocks_per_group;
1014 * Mark the new inode table as in use in the new block
1015 * allocation bitmap, and move any blocks that might
1018 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
1019 j < fs->inode_blocks_per_group ; j++, blk++) {
1020 ext2fs_block_alloc_stats2(fs, blk, +1);
1021 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1022 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1023 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1028 * Make sure the old inode table is reserved in the
1029 * block reservation bitmap.
1031 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1032 j < fs->inode_blocks_per_group ; j++, blk++)
1033 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1039 ext2fs_free_block_bitmap(meta_bmap);
1045 * This helper function tries to allocate a new block. We try to
1046 * avoid hitting the original group descriptor blocks at least at
1047 * first, since we want to make it possible to recover from a badly
1048 * aborted resize operation as much as possible.
1050 * In the future, I may further modify this routine to balance out
1051 * where we get the new blocks across the various block groups.
1052 * Ideally we would allocate blocks that corresponded with the block
1053 * group of the containing inode, and keep contiguous blocks
1054 * together. However, this very difficult to do efficiently, since we
1055 * don't have the necessary information up front.
1059 #define DESPERATION 2
1061 static void init_block_alloc(ext2_resize_t rfs)
1063 rfs->alloc_state = AVOID_OLD;
1064 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1066 /* HACK for testing */
1067 if (ext2fs_blocks_count(rfs->new_fs->super) >
1068 ext2fs_blocks_count(rfs->old_fs->super))
1069 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1073 static blk64_t get_new_block(ext2_resize_t rfs)
1075 ext2_filsys fs = rfs->new_fs;
1078 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1079 if (rfs->alloc_state == DESPERATION)
1082 #ifdef RESIZE2FS_DEBUG
1083 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1084 printf("Going into desperation mode "
1085 "for block allocations\n");
1087 rfs->alloc_state = DESPERATION;
1088 rfs->new_blk = fs->super->s_first_data_block;
1091 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1092 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1094 ((rfs->alloc_state == AVOID_OLD) &&
1095 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1096 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1101 return rfs->new_blk;
1105 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1108 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1111 blk = get_new_block(rfs);
1115 #ifdef RESIZE2FS_DEBUG
1116 if (rfs->flags & 0xF)
1117 printf("get_alloc_block allocating %llu\n", blk);
1120 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1121 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1122 *ret = (blk64_t) blk;
1126 static errcode_t block_mover(ext2_resize_t rfs)
1128 blk64_t blk, old_blk, new_blk;
1129 ext2_filsys fs = rfs->new_fs;
1130 ext2_filsys old_fs = rfs->old_fs;
1135 ext2_badblocks_list badblock_list = 0;
1136 int bb_modified = 0;
1138 fs->get_alloc_block = resize2fs_get_alloc_block;
1139 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1141 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1145 new_blk = fs->super->s_first_data_block;
1146 if (!rfs->itable_buf) {
1147 retval = ext2fs_get_array(fs->blocksize,
1148 fs->inode_blocks_per_group,
1153 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1158 * The first step is to figure out where all of the blocks
1161 to_move = moved = 0;
1162 init_block_alloc(rfs);
1163 for (blk = old_fs->super->s_first_data_block;
1164 blk < ext2fs_blocks_count(old_fs->super); blk++) {
1165 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1167 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1169 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1170 ext2fs_badblocks_list_del(badblock_list, blk);
1175 new_blk = get_new_block(rfs);
1180 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1181 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
1187 ext2fs_free_extent_table(rfs->bmap);
1195 * Step two is to actually move the blocks
1197 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1198 if (retval) goto errout;
1200 if (rfs->progress) {
1201 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1207 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1208 if (retval) goto errout;
1211 #ifdef RESIZE2FS_DEBUG
1212 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1213 printf("Moving %llu blocks %llu->%llu\n",
1214 size, old_blk, new_blk);
1218 if (c > fs->inode_blocks_per_group)
1219 c = fs->inode_blocks_per_group;
1220 retval = io_channel_read_blk64(fs->io, old_blk, c,
1222 if (retval) goto errout;
1223 retval = io_channel_write_blk64(fs->io, new_blk, c,
1225 if (retval) goto errout;
1230 if (rfs->progress) {
1231 io_channel_flush(fs->io);
1232 retval = (rfs->progress)(rfs,
1233 E2_RSZ_BLOCK_RELOC_PASS,
1239 io_channel_flush(fs->io);
1243 if (badblock_list) {
1244 if (!retval && bb_modified)
1245 retval = ext2fs_update_bb_inode(old_fs,
1247 ext2fs_badblocks_list_free(badblock_list);
1253 /* --------------------------------------------------------------------
1255 * Resize processing, phase 3
1257 * --------------------------------------------------------------------
1261 struct process_block_struct {
1264 struct ext2_inode * inode;
1270 static int process_block(ext2_filsys fs, blk64_t *block_nr,
1271 e2_blkcnt_t blockcnt,
1272 blk64_t ref_block EXT2FS_ATTR((unused)),
1273 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1275 struct process_block_struct *pb;
1277 blk64_t block, new_block;
1280 pb = (struct process_block_struct *) priv_data;
1282 if (pb->rfs->bmap) {
1283 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1285 *block_nr = new_block;
1286 ret |= BLOCK_CHANGED;
1288 #ifdef RESIZE2FS_DEBUG
1289 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1290 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1291 pb->ino, blockcnt, block, new_block);
1297 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1298 block, (int) blockcnt);
1310 static errcode_t progress_callback(ext2_filsys fs,
1311 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1312 dgrp_t group, void * priv_data)
1314 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1318 * This check is to protect against old ext2 libraries. It
1319 * shouldn't be needed against new libraries.
1324 if (rfs->progress) {
1325 io_channel_flush(fs->io);
1326 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1327 group+1, fs->group_desc_count);
1335 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1337 struct process_block_struct pb;
1338 ext2_ino_t ino, new_inode;
1339 struct ext2_inode *inode = NULL;
1340 ext2_inode_scan scan = NULL;
1342 char *block_buf = 0;
1343 ext2_ino_t start_to_move;
1348 if ((rfs->old_fs->group_desc_count <=
1349 rfs->new_fs->group_desc_count) &&
1354 * Save the original size of the old filesystem, and
1355 * temporarily set the size to be the new size if the new size
1356 * is larger. We need to do this to avoid catching an error
1357 * by the block iterator routines
1359 orig_size = ext2fs_blocks_count(rfs->old_fs->super);
1360 if (orig_size < ext2fs_blocks_count(rfs->new_fs->super))
1361 ext2fs_blocks_count_set(rfs->old_fs->super,
1362 ext2fs_blocks_count(rfs->new_fs->super));
1364 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1365 if (retval) goto errout;
1367 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1368 if (retval) goto errout;
1369 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1370 if (retval) goto errout;
1372 start_to_move = (rfs->new_fs->group_desc_count *
1373 rfs->new_fs->super->s_inodes_per_group);
1375 if (rfs->progress) {
1376 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1377 0, rfs->old_fs->group_desc_count);
1381 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1385 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1386 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1387 inode = malloc(inode_size);
1393 * First, copy all of the inodes that need to be moved
1394 * elsewhere in the inode table
1397 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1398 if (retval) goto errout;
1402 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1403 continue; /* inode not in use */
1405 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1408 if (ext2fs_file_acl_block(rfs->old_fs, inode) && rfs->bmap) {
1409 new_block = ext2fs_extent_translate(rfs->bmap,
1410 ext2fs_file_acl_block(rfs->old_fs, inode));
1412 ext2fs_file_acl_block_set(rfs->old_fs, inode,
1414 retval = ext2fs_write_inode_full(rfs->old_fs,
1415 ino, inode, inode_size);
1416 if (retval) goto errout;
1420 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
1421 (rfs->bmap || pb.is_dir)) {
1423 retval = ext2fs_block_iterate3(rfs->old_fs,
1425 process_block, &pb);
1434 if (ino <= start_to_move)
1435 continue; /* Don't need to move it. */
1440 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1444 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1447 /* Get the new version of the inode */
1448 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1450 if (retval) goto errout;
1452 inode->i_ctime = time(0);
1453 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1455 if (retval) goto errout;
1457 #ifdef RESIZE2FS_DEBUG
1458 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1459 printf("Inode moved %u->%u\n", ino, new_inode);
1462 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1466 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1468 io_channel_flush(rfs->old_fs->io);
1471 ext2fs_blocks_count_set(rfs->old_fs->super, orig_size);
1473 ext2fs_free_extent_table(rfs->bmap);
1477 ext2fs_close_inode_scan(scan);
1479 ext2fs_free_mem(&block_buf);
1484 /* --------------------------------------------------------------------
1486 * Resize processing, phase 4.
1488 * --------------------------------------------------------------------
1494 unsigned int max_dirs;
1498 static int check_and_change_inodes(ext2_ino_t dir,
1499 int entry EXT2FS_ATTR((unused)),
1500 struct ext2_dir_entry *dirent, int offset,
1501 int blocksize EXT2FS_ATTR((unused)),
1502 char *buf EXT2FS_ATTR((unused)),
1505 struct istruct *is = (struct istruct *) priv_data;
1506 struct ext2_inode inode;
1507 ext2_ino_t new_inode;
1510 if (is->rfs->progress && offset == 0) {
1511 io_channel_flush(is->rfs->old_fs->io);
1512 is->err = (is->rfs->progress)(is->rfs,
1513 E2_RSZ_INODE_REF_UPD_PASS,
1514 ++is->num, is->max_dirs);
1516 return DIRENT_ABORT;
1522 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1526 #ifdef RESIZE2FS_DEBUG
1527 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1528 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1529 dir, dirent->name_len&0xFF, dirent->name,
1530 dirent->inode, new_inode);
1533 dirent->inode = new_inode;
1535 /* Update the directory mtime and ctime */
1536 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1538 inode.i_mtime = inode.i_ctime = time(0);
1539 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1541 return DIRENT_ABORT;
1544 return DIRENT_CHANGED;
1547 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1556 * Now, we iterate over all of the directories to update the
1560 is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
1564 if (rfs->progress) {
1565 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1571 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1572 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1573 check_and_change_inodes, &is);
1581 if (rfs->progress && (is.num < is.max_dirs))
1582 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1583 is.max_dirs, is.max_dirs);
1586 ext2fs_free_extent_table(rfs->imap);
1592 /* --------------------------------------------------------------------
1594 * Resize processing, phase 5.
1596 * In this phase we actually move the inode table around, and then
1597 * update the summary statistics. This is scary, since aborting here
1598 * will potentially scramble the filesystem. (We are moving the
1599 * inode tables around in place, and so the potential for lost data,
1600 * or at the very least scrambling the mapping between filenames and
1601 * inode numbers is very high in case of a power failure here.)
1602 * --------------------------------------------------------------------
1607 * A very scary routine --- this one moves the inode table around!!!
1609 * After this you have to use the rfs->new_fs file handle to read and
1612 static errcode_t move_itables(ext2_resize_t rfs)
1616 dgrp_t i, max_groups;
1617 ext2_filsys fs = rfs->new_fs;
1619 blk64_t old_blk, new_blk, blk;
1621 int j, to_move, moved;
1623 max_groups = fs->group_desc_count;
1624 if (max_groups > rfs->old_fs->group_desc_count)
1625 max_groups = rfs->old_fs->group_desc_count;
1627 size = fs->blocksize * fs->inode_blocks_per_group;
1628 if (!rfs->itable_buf) {
1629 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1635 * Figure out how many inode tables we need to move
1637 to_move = moved = 0;
1638 for (i=0; i < max_groups; i++)
1639 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
1640 ext2fs_inode_table_loc(fs, i))
1646 if (rfs->progress) {
1647 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1653 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1655 for (i=0; i < max_groups; i++) {
1656 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
1657 new_blk = ext2fs_inode_table_loc(fs, i);
1658 diff = new_blk - old_blk;
1660 #ifdef RESIZE2FS_DEBUG
1661 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1662 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
1663 i, old_blk, new_blk, diff);
1669 retval = io_channel_read_blk64(fs->io, old_blk,
1670 fs->inode_blocks_per_group,
1675 * The end of the inode table segment often contains
1676 * all zeros, and we're often only moving the inode
1677 * table down a block or two. If so, we can optimize
1678 * things by not rewriting blocks that we know to be zero
1681 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1684 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1685 #ifdef RESIZE2FS_DEBUG
1686 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1687 printf("%d blocks of zeros...\n", n);
1689 num = fs->inode_blocks_per_group;
1693 retval = io_channel_write_blk64(fs->io, new_blk,
1694 num, rfs->itable_buf);
1696 io_channel_write_blk64(fs->io, old_blk,
1697 num, rfs->itable_buf);
1701 retval = io_channel_write_blk64(fs->io,
1702 old_blk + fs->inode_blocks_per_group,
1703 diff, (rfs->itable_buf +
1704 (fs->inode_blocks_per_group - diff) *
1710 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1711 j < fs->inode_blocks_per_group ; j++, blk++)
1712 ext2fs_block_alloc_stats2(fs, blk, -1);
1714 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
1715 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1716 ext2fs_mark_super_dirty(rfs->old_fs);
1717 ext2fs_flush(rfs->old_fs);
1719 if (rfs->progress) {
1720 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1726 mark_table_blocks(fs, fs->block_map);
1728 #ifdef RESIZE2FS_DEBUG
1729 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1730 printf("Inode table move finished.\n");
1739 * Fix the resize inode
1741 static errcode_t fix_resize_inode(ext2_filsys fs)
1743 struct ext2_inode inode;
1745 char *block_buf = NULL;
1747 if (!(fs->super->s_feature_compat &
1748 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1751 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1752 if (retval) goto errout;
1754 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1755 if (retval) goto errout;
1757 ext2fs_iblk_set(fs, &inode, 1);
1759 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1760 if (retval) goto errout;
1762 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1764 * Avoid zeroing out block #0; that's rude. This
1765 * should never happen anyway since the filesystem
1766 * should be fsck'ed and we assume it is consistent.
1769 _("Should never happen: resize inode corrupt!\n"));
1773 memset(block_buf, 0, fs->blocksize);
1775 retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1777 if (retval) goto errout;
1779 retval = ext2fs_create_resize_inode(fs);
1785 ext2fs_free_mem(&block_buf);
1790 * Finally, recalculate the summary information
1792 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1796 unsigned int group = 0;
1797 unsigned int count = 0;
1801 blk64_t super_blk, old_desc_blk, new_desc_blk;
1802 int old_desc_blocks;
1805 * First calculate the block statistics
1807 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1808 ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
1810 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1811 old_desc_blocks = fs->super->s_first_meta_bg;
1813 old_desc_blocks = fs->desc_blocks +
1814 fs->super->s_reserved_gdt_blocks;
1815 for (blk = fs->super->s_first_data_block;
1816 blk < ext2fs_blocks_count(fs->super); blk++) {
1818 !((blk == super_blk) ||
1819 ((old_desc_blk && old_desc_blocks &&
1820 (blk >= old_desc_blk) &&
1821 (blk < old_desc_blk + old_desc_blocks))) ||
1822 ((new_desc_blk && (blk == new_desc_blk))) ||
1823 (blk == ext2fs_block_bitmap_loc(fs, group)) ||
1824 (blk == ext2fs_inode_bitmap_loc(fs, group)) ||
1825 ((blk >= ext2fs_inode_table_loc(fs, group) &&
1826 (blk < ext2fs_inode_table_loc(fs, group)
1827 + fs->inode_blocks_per_group))))) ||
1828 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
1833 if ((count == fs->super->s_blocks_per_group) ||
1834 (blk == ext2fs_blocks_count(fs->super)-1)) {
1835 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
1836 ext2fs_group_desc_csum_set(fs, group);
1838 if (group >= fs->group_desc_count)
1842 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1843 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
1846 if (fs->super->s_feature_incompat &
1847 EXT2_FEATURE_INCOMPAT_META_BG)
1848 old_desc_blocks = fs->super->s_first_meta_bg;
1850 old_desc_blocks = fs->desc_blocks +
1851 fs->super->s_reserved_gdt_blocks;
1854 ext2fs_free_blocks_count_set(fs->super, total_free);
1857 * Next, calculate the inode statistics
1864 /* Protect loop from wrap-around if s_inodes_count maxed */
1865 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
1866 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1868 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1873 if ((count == fs->super->s_inodes_per_group) ||
1874 (ino == fs->super->s_inodes_count)) {
1875 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
1876 ext2fs_group_desc_csum_set(fs, group);
1878 if (group >= fs->group_desc_count)
1882 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
1885 fs->super->s_free_inodes_count = total_free;
1886 ext2fs_mark_super_dirty(fs);
1891 * Journal may have been relocated; update the backup journal blocks
1892 * in the superblock.
1894 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
1897 struct ext2_inode inode;
1899 if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1902 /* External journal? Nothing to do. */
1903 if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
1906 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
1909 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
1910 fs->super->s_jnl_blocks[15] = inode.i_size_high;
1911 fs->super->s_jnl_blocks[16] = inode.i_size;
1912 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
1913 ext2fs_mark_super_dirty(fs);
1917 static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
1918 int old_desc_blocks)
1920 blk64_t super_blk, old_desc_blk, new_desc_blk;
1923 /* inode table blocks plus allocation bitmaps */
1924 overhead = fs->inode_blocks_per_group + 2;
1926 ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
1927 &old_desc_blk, &new_desc_blk, 0);
1928 if ((grp == 0) || super_blk)
1931 overhead += old_desc_blocks;
1932 else if (new_desc_blk)
1939 * calcluate the minimum number of blocks the given fs can be resized to
1941 blk64_t calculate_minimum_resize_size(ext2_filsys fs)
1943 ext2_ino_t inode_count;
1944 blk64_t blks_needed, groups, data_blocks;
1945 blk64_t grp, data_needed, last_start;
1946 blk64_t overhead = 0;
1947 int old_desc_blocks;
1948 int extra_groups = 0;
1949 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1952 * first figure out how many group descriptors we need to
1953 * handle the number of inodes we have
1955 inode_count = fs->super->s_inodes_count -
1956 fs->super->s_free_inodes_count;
1957 blks_needed = ext2fs_div_ceil(inode_count,
1958 fs->super->s_inodes_per_group) *
1959 EXT2_BLOCKS_PER_GROUP(fs->super);
1960 groups = ext2fs_div64_ceil(blks_needed,
1961 EXT2_BLOCKS_PER_GROUP(fs->super));
1964 * number of old-style block group descriptor blocks
1966 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1967 old_desc_blocks = fs->super->s_first_meta_bg;
1969 old_desc_blocks = fs->desc_blocks +
1970 fs->super->s_reserved_gdt_blocks;
1972 /* calculate how many blocks are needed for data */
1973 data_needed = ext2fs_blocks_count(fs->super) -
1974 ext2fs_free_blocks_count(fs->super);
1976 for (grp = 0; grp < fs->group_desc_count; grp++)
1977 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
1980 * For ext4 we need to allow for up to a flex_bg worth of
1981 * inode tables of slack space so the resize operation can be
1982 * guaranteed to finish.
1984 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1985 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
1986 data_needed += fs->inode_blocks_per_group * extra_groups;
1987 extra_groups = groups % flexbg_size;
1991 * figure out how many data blocks we have given the number of groups
1992 * we need for our inodes
1994 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
1996 for (grp = 0; grp < groups; grp++) {
1997 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
2000 * we want to keep track of how much data we can store in
2001 * the groups leading up to the last group so we can determine
2002 * how big the last group needs to be
2004 if (grp != (groups - 1))
2005 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
2008 data_blocks -= overhead;
2012 * if we need more group descriptors in order to accomodate our data
2013 * then we need to add them here
2015 while (data_needed > data_blocks) {
2016 blk64_t remainder = data_needed - data_blocks;
2019 /* figure out how many more groups we need for the data */
2020 extra_grps = ext2fs_div64_ceil(remainder,
2021 EXT2_BLOCKS_PER_GROUP(fs->super));
2023 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
2025 /* ok we have to account for the last group */
2026 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2027 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
2029 for (grp = groups; grp < groups+extra_grps; grp++) {
2030 overhead = calc_group_overhead(fs, grp,
2034 * again, we need to see how much data we cram into
2035 * all of the groups leading up to the last group
2037 if (grp != (groups + extra_grps - 1))
2038 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
2041 data_blocks -= overhead;
2044 groups += extra_grps;
2045 extra_groups += extra_grps;
2046 if (fs->super->s_feature_incompat
2047 & EXT4_FEATURE_INCOMPAT_FLEX_BG
2048 && extra_groups > flexbg_size) {
2050 * For ext4 we need to allow for up to a flex_bg worth
2051 * of inode tables of slack space so the resize
2052 * operation can be guaranteed to finish.
2054 extra_groups = flexbg_size -
2055 (groups & (flexbg_size - 1));
2056 data_needed += (fs->inode_blocks_per_group *
2058 extra_groups = groups % flexbg_size;
2062 /* now for the fun voodoo */
2063 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2066 * if this is the case then the last group is going to have data in it
2067 * so we need to adjust the size of the last group accordingly
2069 if (last_start < data_needed) {
2070 blk64_t remainder = data_needed - last_start;
2073 * 50 is a magic number that mkfs/resize uses to see if its
2074 * even worth making/resizing the fs. basically you need to
2075 * have at least 50 blocks in addition to the blocks needed
2076 * for the metadata in the last group
2079 overhead += remainder;
2085 overhead += fs->super->s_first_data_block;
2088 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2089 * only do groups-1, and then add the number of blocks needed to
2090 * handle the group descriptor metadata+data that we need
2092 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2093 blks_needed += overhead;
2096 * If at this point we've already added up more "needed" than
2097 * the current size, just return current size as minimum.
2099 if (blks_needed >= ext2fs_blocks_count(fs->super))
2100 return ext2fs_blocks_count(fs->super);
2102 * We need to reserve a few extra blocks if extents are
2103 * enabled, in case we need to grow the extent tree. The more
2104 * we shrink the file system, the more space we need.
2106 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
2107 blks_needed += (ext2fs_blocks_count(fs->super) -