2 * resize2fs.c --- ext2 main routine
4 * Copyright (C) 1997, 1998 by Theodore Ts'o and
7 * Copyright (C) 1999, 2000 by Theosore Ts'o
10 * This file may be redistributed under the terms of the GNU Public
16 * Resizing a filesystem consists of the following phases:
18 * 1. Adjust superblock and write out new parts of the inode
20 * 2. Determine blocks which need to be relocated, and copy the
21 * contents of blocks from their old locations to the new ones.
22 * 3. Scan the inode table, doing the following:
23 * a. If blocks have been moved, update the block
24 * pointers in the inodes and indirect blocks to
25 * point at the new block locations.
26 * b. If parts of the inode table need to be evacuated,
27 * copy inodes from their old locations to their
29 * c. If (b) needs to be done, note which blocks contain
30 * directory information, since we will need to
31 * update the directory information.
32 * 4. Update the directory blocks with the new inode locations.
33 * 5. Move the inode tables, if necessary.
36 #include "resize2fs.h"
39 #ifdef __linux__ /* Kludge for debugging */
40 #define RESIZE2FS_DEBUG
43 static void fix_uninit_block_bitmaps(ext2_filsys fs);
44 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size);
45 static errcode_t blocks_to_move(ext2_resize_t rfs);
46 static errcode_t block_mover(ext2_resize_t rfs);
47 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
48 static errcode_t inode_ref_fix(ext2_resize_t rfs);
49 static errcode_t move_itables(ext2_resize_t rfs);
50 static errcode_t fix_resize_inode(ext2_filsys fs);
51 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
52 static errcode_t fix_sb_journal_backup(ext2_filsys fs);
55 * Some helper CPP macros
57 #define IS_BLOCK_BM(fs, i, blk) ((blk) == ext2fs_block_bitmap_loc((fs),(i)))
58 #define IS_INODE_BM(fs, i, blk) ((blk) == ext2fs_inode_bitmap_loc((fs),(i)))
60 #define IS_INODE_TB(fs, i, blk) (((blk) >= ext2fs_inode_table_loc((fs), (i))) && \
61 ((blk) < (ext2fs_inode_table_loc((fs), (i)) + \
62 (fs)->inode_blocks_per_group)))
64 #define META_OVERHEAD(fs) (2 + (fs)->inode_blocks_per_group)
65 #define SUPER_OVERHEAD(fs) (1 + (fs)->desc_blocks +\
66 (fs)->super->s_reserved_gdt_blocks)
69 * This is the top-level routine which does the dirty deed....
71 errcode_t resize_fs(ext2_filsys fs, blk_t *new_size, int flags,
72 errcode_t (*progress)(ext2_resize_t rfs, int pass,
74 unsigned long max_val))
79 retval = ext2fs_read_bitmaps(fs);
83 fs->super->s_state |= EXT2_ERROR_FS;
84 ext2fs_mark_super_dirty(fs);
88 * Create the data structure
90 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
93 memset(rfs, 0, sizeof(struct ext2_resize_struct));
95 fix_uninit_block_bitmaps(fs);
100 rfs->progress = progress;
101 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
105 retval = adjust_superblock(rfs, *new_size);
109 fix_uninit_block_bitmaps(rfs->new_fs);
110 /* Clear the block bitmap uninit flag for the last block group */
111 ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
112 EXT2_BG_BLOCK_UNINIT);
114 *new_size = ext2fs_blocks_count(rfs->new_fs->super);
116 retval = blocks_to_move(rfs);
120 #ifdef RESIZE2FS_DEBUG
121 if (rfs->flags & RESIZE_DEBUG_BMOVE)
122 printf("Number of free blocks: %llu/%llu, Needed: %d\n",
123 ext2fs_free_blocks_count(rfs->old_fs->super),
124 ext2fs_free_blocks_count(rfs->new_fs->super),
128 retval = block_mover(rfs);
132 retval = inode_scan_and_fix(rfs);
136 retval = inode_ref_fix(rfs);
140 retval = move_itables(rfs);
144 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
148 retval = fix_resize_inode(rfs->new_fs);
152 retval = fix_sb_journal_backup(rfs->new_fs);
156 rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
157 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
158 retval = ext2fs_close(rfs->new_fs);
164 ext2fs_free(rfs->old_fs);
166 ext2fs_free_mem(&rfs->itable_buf);
167 if (rfs->reserve_blocks)
168 ext2fs_free_block_bitmap(rfs->reserve_blocks);
169 if (rfs->move_blocks)
170 ext2fs_free_block_bitmap(rfs->move_blocks);
171 ext2fs_free_mem(&rfs);
177 ext2fs_free(rfs->new_fs);
179 ext2fs_free_mem(&rfs->itable_buf);
180 ext2fs_free_mem(&rfs);
185 * Clean up the bitmaps for unitialized bitmaps
187 static void fix_uninit_block_bitmaps(ext2_filsys fs)
189 blk_t i, blk, super_blk, old_desc_blk, new_desc_blk;
193 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
194 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
197 for (g=0; g < fs->group_desc_count; g++) {
198 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
201 blk = (g * fs->super->s_blocks_per_group) +
202 fs->super->s_first_data_block;
204 ext2fs_super_and_bgd_loc(fs, g, &super_blk,
205 &old_desc_blk, &new_desc_blk, 0);
207 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
208 old_desc_blocks = fs->super->s_first_meta_bg;
210 old_desc_blocks = fs->desc_blocks +
211 fs->super->s_reserved_gdt_blocks;
213 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
214 if (blk >= ext2fs_blocks_count(fs->super))
216 if ((blk == super_blk) ||
217 (old_desc_blk && old_desc_blocks &&
218 (blk >= old_desc_blk) &&
219 (blk < old_desc_blk + old_desc_blocks)) ||
220 (new_desc_blk && (blk == new_desc_blk)) ||
221 (blk == ext2fs_block_bitmap_loc(fs, g)) ||
222 (blk == ext2fs_inode_bitmap_loc(fs, g)) ||
223 (blk >= ext2fs_inode_table_loc(fs, g) &&
224 (blk < ext2fs_inode_table_loc(fs, g)
225 + fs->inode_blocks_per_group)))
226 ext2fs_fast_mark_block_bitmap2(fs->block_map, blk);
228 ext2fs_fast_unmark_block_bitmap2(fs->block_map, blk);
233 /* --------------------------------------------------------------------
235 * Resize processing, phase 1.
237 * In this phase we adjust the in-memory superblock information, and
238 * initialize any new parts of the inode table. The new parts of the
239 * inode table are created in virgin disk space, so we can abort here
240 * without any side effects.
241 * --------------------------------------------------------------------
245 * If the group descriptor's bitmap and inode table blocks are valid,
246 * release them in the new filesystem data structure, and mark them as
247 * reserved so the old inode table blocks don't get overwritten.
249 static void free_gdp_blocks(ext2_filsys fs,
250 ext2fs_block_bitmap reserve_blocks,
251 struct ext2_group_desc *gdp)
256 if (gdp->bg_block_bitmap &&
257 (gdp->bg_block_bitmap < ext2fs_blocks_count(fs->super))) {
258 ext2fs_block_alloc_stats(fs, gdp->bg_block_bitmap, -1);
259 ext2fs_mark_block_bitmap2(reserve_blocks,
260 gdp->bg_block_bitmap);
263 if (gdp->bg_inode_bitmap &&
264 (gdp->bg_inode_bitmap < ext2fs_blocks_count(fs->super))) {
265 ext2fs_block_alloc_stats(fs, gdp->bg_inode_bitmap, -1);
266 ext2fs_mark_block_bitmap2(reserve_blocks,
267 gdp->bg_inode_bitmap);
270 if (gdp->bg_inode_table == 0 ||
271 (gdp->bg_inode_table >= ext2fs_blocks_count(fs->super)))
274 for (blk = gdp->bg_inode_table, j = 0;
275 j < fs->inode_blocks_per_group; j++, blk++) {
276 if (blk >= ext2fs_blocks_count(fs->super))
278 ext2fs_block_alloc_stats(fs, blk, -1);
279 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
284 * This routine is shared by the online and offline resize routines.
285 * All of the information which is adjusted in memory is done here.
287 * The reserve_blocks parameter is only needed when shrinking the
290 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
291 ext2fs_block_bitmap reserve_blocks, blk_t new_size)
296 blk_t blk, group_block;
298 int adj, old_numblocks, numblocks, adjblocks;
299 unsigned long i, j, old_desc_blocks, max_group;
300 unsigned int meta_bg, meta_bg_size;
301 int has_super, csum_flag;
302 unsigned long long new_inodes; /* u64 to check for overflow */
305 ext2fs_blocks_count_set(fs->super, new_size);
308 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
309 fs->super->s_first_data_block,
310 EXT2_BLOCKS_PER_GROUP(fs->super));
311 if (fs->group_desc_count == 0)
312 return EXT2_ET_TOOSMALL;
313 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
314 EXT2_DESC_PER_BLOCK(fs->super));
317 * Overhead is the number of bookkeeping blocks per group. It
318 * includes the superblock backup, the group descriptor
319 * backups, the inode bitmap, the block bitmap, and the inode
322 overhead = (int) (2 + fs->inode_blocks_per_group);
324 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
325 overhead += 1 + fs->desc_blocks +
326 fs->super->s_reserved_gdt_blocks;
329 * See if the last group is big enough to support the
330 * necessary data structures. If not, we need to get rid of
333 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
334 fs->super->s_blocks_per_group;
335 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
336 return EXT2_ET_TOOSMALL;
337 if (rem && (rem < overhead+50)) {
338 ext2fs_blocks_count_set(fs->super,
339 ext2fs_blocks_count(fs->super) - rem);
343 * Adjust the number of inodes
345 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
346 if (new_inodes > ~0U) {
347 fprintf(stderr, _("inodes (%llu) must be less than %u"),
349 return EXT2_ET_TOO_MANY_INODES;
351 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
352 fs->group_desc_count;
355 * Adjust the number of free blocks
357 blk = ext2fs_blocks_count(old_fs->super);
358 if (blk > ext2fs_blocks_count(fs->super))
359 ext2fs_free_blocks_count_set(fs->super,
360 ext2fs_free_blocks_count(fs->super) -
361 (blk - ext2fs_blocks_count(fs->super)));
363 ext2fs_free_blocks_count_set(fs->super,
364 ext2fs_free_blocks_count(fs->super) +
365 (ext2fs_blocks_count(fs->super) - blk));
368 * Adjust the number of reserved blocks
370 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
371 ext2fs_blocks_count(old_fs->super);
372 ext2fs_r_blocks_count_set(fs->super,
373 (percent * ext2fs_blocks_count(fs->super) /
377 * Adjust the bitmaps for size
379 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
380 fs->super->s_inodes_count,
382 if (retval) goto errout;
384 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
385 * fs->group_desc_count)) - 1 +
386 fs->super->s_first_data_block;
387 retval = ext2fs_resize_block_bitmap2(ext2fs_blocks_count(fs->super)-1,
388 real_end, fs->block_map);
390 if (retval) goto errout;
393 * Reallocate the group descriptors as necessary.
395 if (old_fs->desc_blocks != fs->desc_blocks) {
396 retval = ext2fs_resize_mem(old_fs->desc_blocks *
398 fs->desc_blocks * fs->blocksize,
402 if (fs->desc_blocks > old_fs->desc_blocks)
403 memset((char *) fs->group_desc +
404 (old_fs->desc_blocks * fs->blocksize), 0,
405 (fs->desc_blocks - old_fs->desc_blocks) *
410 * If the resize_inode feature is set, and we are changing the
411 * number of descriptor blocks, then adjust
412 * s_reserved_gdt_blocks if possible to avoid needing to move
413 * the inode table either now or in the future.
415 if ((fs->super->s_feature_compat &
416 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
417 (old_fs->desc_blocks != fs->desc_blocks)) {
420 new = ((int) fs->super->s_reserved_gdt_blocks) +
421 (old_fs->desc_blocks - fs->desc_blocks);
424 if (new > (int) fs->blocksize/4)
425 new = fs->blocksize/4;
426 fs->super->s_reserved_gdt_blocks = new;
430 * If we are shrinking the number of block groups, we're done
433 if (old_fs->group_desc_count > fs->group_desc_count) {
435 * Check the block groups that we are chopping off
436 * and free any blocks associated with their metadata
438 for (i = fs->group_desc_count;
439 i < old_fs->group_desc_count; i++) {
440 free_gdp_blocks(fs, reserve_blocks,
441 ext2fs_group_desc(old_fs,
442 old_fs->group_desc, i));
449 * Fix the count of the last (old) block group
451 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
452 old_fs->super->s_first_data_block) %
453 old_fs->super->s_blocks_per_group;
455 old_numblocks = old_fs->super->s_blocks_per_group;
456 if (old_fs->group_desc_count == fs->group_desc_count) {
457 numblocks = (ext2fs_blocks_count(fs->super) -
458 fs->super->s_first_data_block) %
459 fs->super->s_blocks_per_group;
461 numblocks = fs->super->s_blocks_per_group;
463 numblocks = fs->super->s_blocks_per_group;
464 i = old_fs->group_desc_count - 1;
465 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
466 ext2fs_group_desc_csum_set(fs, i);
469 * If the number of block groups is staying the same, we're
470 * done and can exit now. (If the number block groups is
471 * shrinking, we had exited earlier.)
473 if (old_fs->group_desc_count >= fs->group_desc_count) {
479 * Initialize the new block group descriptors
481 group_block = fs->super->s_first_data_block +
482 old_fs->group_desc_count * fs->super->s_blocks_per_group;
484 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
485 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
486 adj = old_fs->group_desc_count;
487 max_group = fs->group_desc_count - adj;
488 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
489 old_desc_blocks = fs->super->s_first_meta_bg;
491 old_desc_blocks = fs->desc_blocks +
492 fs->super->s_reserved_gdt_blocks;
493 for (i = old_fs->group_desc_count;
494 i < fs->group_desc_count; i++) {
495 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
496 sizeof(struct ext2_group_desc));
499 ext2fs_bg_flags_zap(fs, i);
501 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT | EXT2_BG_INODE_ZEROED);
502 if (i == fs->group_desc_count-1) {
503 numblocks = (ext2fs_blocks_count(fs->super) -
504 fs->super->s_first_data_block) %
505 fs->super->s_blocks_per_group;
507 numblocks = fs->super->s_blocks_per_group;
509 numblocks = fs->super->s_blocks_per_group;
511 ext2fs_bg_flags_set(fs, i,
512 EXT2_BG_BLOCK_UNINIT);
515 has_super = ext2fs_bg_has_super(fs, i);
517 ext2fs_block_alloc_stats2(fs, group_block, +1);
520 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
521 meta_bg = i / meta_bg_size;
522 if (!(fs->super->s_feature_incompat &
523 EXT2_FEATURE_INCOMPAT_META_BG) ||
524 (meta_bg < fs->super->s_first_meta_bg)) {
526 for (j=0; j < old_desc_blocks; j++)
527 ext2fs_block_alloc_stats2(fs,
528 group_block + 1 + j, +1);
529 adjblocks += old_desc_blocks;
534 if (((i % meta_bg_size) == 0) ||
535 ((i % meta_bg_size) == 1) ||
536 ((i % meta_bg_size) == (meta_bg_size-1)))
537 ext2fs_block_alloc_stats2(fs,
538 group_block + has_super, +1);
541 adjblocks += 2 + fs->inode_blocks_per_group;
543 numblocks -= adjblocks;
544 ext2fs_free_blocks_count_set(fs->super,
545 ext2fs_free_blocks_count(fs->super) - adjblocks);
546 fs->super->s_free_inodes_count +=
547 fs->super->s_inodes_per_group;
548 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
549 ext2fs_bg_free_inodes_count_set(fs, i,
550 fs->super->s_inodes_per_group);
551 ext2fs_bg_used_dirs_count_set(fs, i, 0);
552 ext2fs_group_desc_csum_set(fs, i);
554 retval = ext2fs_allocate_group_table(fs, i, 0);
555 if (retval) goto errout;
557 group_block += fs->super->s_blocks_per_group;
566 * This routine adjusts the superblock and other data structures, both
567 * in disk as well as in memory...
569 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
576 unsigned long max_group;
579 ext2fs_mark_super_dirty(fs);
580 ext2fs_mark_bb_dirty(fs);
581 ext2fs_mark_ib_dirty(fs);
583 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
584 &rfs->reserve_blocks);
588 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
593 * Check to make sure there are enough inodes
595 if ((rfs->old_fs->super->s_inodes_count -
596 rfs->old_fs->super->s_free_inodes_count) >
597 rfs->new_fs->super->s_inodes_count) {
603 * If we are shrinking the number block groups, we're done and
606 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
612 * If the number of block groups is staying the same, we're
613 * done and can exit now. (If the number block groups is
614 * shrinking, we had exited earlier.)
616 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
622 * Initialize the new block group descriptors
624 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
629 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
630 group_block = fs->super->s_first_data_block +
631 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
633 adj = rfs->old_fs->group_desc_count;
634 max_group = fs->group_desc_count - adj;
636 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
641 for (i = rfs->old_fs->group_desc_count;
642 i < fs->group_desc_count; i++) {
644 * Write out the new inode table
646 retval = io_channel_write_blk64(fs->io,
647 ext2fs_inode_table_loc(fs, i),
648 fs->inode_blocks_per_group,
650 if (retval) goto errout;
652 io_channel_flush(fs->io);
654 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
655 i - adj + 1, max_group);
659 group_block += fs->super->s_blocks_per_group;
661 io_channel_flush(fs->io);
668 /* --------------------------------------------------------------------
670 * Resize processing, phase 2.
672 * In this phase we adjust determine which blocks need to be moved, in
673 * blocks_to_move(). We then copy the blocks to their ultimate new
674 * destinations using block_mover(). Since we are copying blocks to
675 * their new locations, again during this pass we can abort without
677 * --------------------------------------------------------------------
681 * This helper function creates a block bitmap with all of the
682 * filesystem meta-data blocks.
684 static errcode_t mark_table_blocks(ext2_filsys fs,
685 ext2fs_block_bitmap bmap)
690 unsigned long meta_bg_size;
691 unsigned int old_desc_blocks;
693 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
694 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
695 old_desc_blocks = fs->super->s_first_meta_bg;
697 old_desc_blocks = fs->desc_blocks +
698 fs->super->s_reserved_gdt_blocks;
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, blk_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++;
747 } else if (IS_INODE_BM(fs, group, blk)) {
748 ext2fs_inode_bitmap_loc_set(fs, group, 0);
749 rfs->needed_blocks++;
750 } else if (IS_INODE_TB(fs, group, blk)) {
751 ext2fs_inode_table_loc_set(fs, group, 0);
752 rfs->needed_blocks++;
753 } else if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
754 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
755 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
757 * If the block bitmap is uninitialized, which means
758 * nothing other than standard metadata in use.
761 } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
762 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
763 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
764 rfs->needed_blocks++;
770 * This routine marks and unmarks reserved blocks in the new block
771 * bitmap. It also determines which blocks need to be moved and
772 * places this information into the move_blocks bitmap.
774 static errcode_t blocks_to_move(ext2_resize_t rfs)
777 dgrp_t i, max_groups, g;
778 blk_t blk, group_blk;
779 unsigned long old_blocks, new_blocks;
780 unsigned int meta_bg, meta_bg_size;
782 ext2_filsys fs, old_fs;
783 ext2fs_block_bitmap meta_bmap;
784 __u32 save_incompat_flag;
787 old_fs = rfs->old_fs;
788 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
791 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
796 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
801 retval = mark_table_blocks(old_fs, meta_bmap);
808 * If we're shrinking the filesystem, we need to move all of
809 * the blocks that don't fit any more
811 for (blk = ext2fs_blocks_count(fs->super);
812 blk < ext2fs_blocks_count(old_fs->super); blk++) {
813 g = ext2fs_group_of_blk2(fs, blk);
814 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
815 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
816 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
818 * The block bitmap is uninitialized, so skip
819 * to the next block group.
821 blk = ((g+1) * fs->super->s_blocks_per_group) +
822 fs->super->s_first_data_block - 1;
825 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
826 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
827 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
828 rfs->needed_blocks++;
830 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
833 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
834 old_blocks = old_fs->super->s_first_meta_bg;
835 new_blocks = fs->super->s_first_meta_bg;
837 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
838 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
841 if (old_blocks == new_blocks) {
846 max_groups = fs->group_desc_count;
847 if (max_groups > old_fs->group_desc_count)
848 max_groups = old_fs->group_desc_count;
849 group_blk = old_fs->super->s_first_data_block;
851 * If we're reducing the number of descriptor blocks, this
852 * makes life easy. :-) We just have to mark some extra
855 if (old_blocks > new_blocks) {
856 for (i = 0; i < max_groups; i++) {
857 if (!ext2fs_bg_has_super(fs, i)) {
858 group_blk += fs->super->s_blocks_per_group;
861 for (blk = group_blk+1+new_blocks;
862 blk < group_blk+1+old_blocks; blk++) {
863 ext2fs_block_alloc_stats2(fs, blk, -1);
864 rfs->needed_blocks--;
866 group_blk += fs->super->s_blocks_per_group;
872 * If we're increasing the number of descriptor blocks, life
873 * gets interesting....
875 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
876 for (i = 0; i < max_groups; i++) {
877 has_super = ext2fs_bg_has_super(fs, i);
879 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
881 meta_bg = i / meta_bg_size;
882 if (!(fs->super->s_feature_incompat &
883 EXT2_FEATURE_INCOMPAT_META_BG) ||
884 (meta_bg < fs->super->s_first_meta_bg)) {
886 for (blk = group_blk+1;
887 blk < group_blk + 1 + new_blocks; blk++)
888 mark_fs_metablock(rfs, meta_bmap,
894 if (((i % meta_bg_size) == 0) ||
895 ((i % meta_bg_size) == 1) ||
896 ((i % meta_bg_size) == (meta_bg_size-1)))
897 mark_fs_metablock(rfs, meta_bmap, i,
898 group_blk + has_super);
901 if (ext2fs_inode_table_loc(fs, i) &&
902 ext2fs_inode_bitmap_loc(fs, i) &&
903 ext2fs_block_bitmap_loc(fs, i))
907 * Reserve the existing meta blocks that we know
908 * aren't to be moved.
910 if (ext2fs_block_bitmap_loc(fs, i))
911 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
912 ext2fs_block_bitmap_loc(fs, i));
913 if (ext2fs_inode_bitmap_loc(fs, i))
914 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
915 ext2fs_inode_bitmap_loc(fs, i));
916 if (ext2fs_inode_table_loc(fs, i))
917 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
918 j < fs->inode_blocks_per_group ; j++, blk++)
919 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
923 * Allocate the missing data structures
925 * XXX We have a problem with FLEX_BG and off-line
926 * resizing where we are growing the size of the
927 * filesystem. ext2fs_allocate_group_table() will try
928 * to reserve the inode table in the desired flex_bg
929 * location. However, passing rfs->reserve_blocks
930 * doesn't work since it only has reserved the blocks
931 * that will be used in the new block group -- and
932 * with flex_bg, we can and will allocate the tables
933 * outside of the block group. And we can't pass in
934 * the fs->block_map because it doesn't handle
935 * overlapping inode table movements right. So for
936 * now, we temporarily disable flex_bg to force
937 * ext2fs_allocate_group_tables() to allocate the bg
938 * metadata in side the block group, and the restore
939 * it afterwards. Ugly, until we can fix this up
942 save_incompat_flag = fs->super->s_feature_incompat;
943 fs->super->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_FLEX_BG;
944 retval = ext2fs_allocate_group_table(fs, i,
945 rfs->reserve_blocks);
946 fs->super->s_feature_incompat = save_incompat_flag;
951 * For those structures that have changed, we need to
954 if (ext2fs_block_bitmap_loc(old_fs, i) !=
955 (blk = ext2fs_block_bitmap_loc(fs, i))) {
956 ext2fs_block_alloc_stats2(fs, blk, +1);
957 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
958 !ext2fs_test_block_bitmap2(meta_bmap, blk))
959 ext2fs_mark_block_bitmap2(rfs->move_blocks,
962 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
963 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
964 ext2fs_block_alloc_stats2(fs, blk, +1);
965 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
966 !ext2fs_test_block_bitmap2(meta_bmap, blk))
967 ext2fs_mark_block_bitmap2(rfs->move_blocks,
972 * The inode table, if we need to relocate it, is
973 * handled specially. We have to reserve the blocks
974 * for both the old and the new inode table, since we
975 * can't have the inode table be destroyed during the
976 * block relocation phase.
978 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
979 goto next_group; /* inode table not moved */
981 rfs->needed_blocks += fs->inode_blocks_per_group;
984 * Mark the new inode table as in use in the new block
985 * allocation bitmap, and move any blocks that might
988 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
989 j < fs->inode_blocks_per_group ; j++, blk++) {
990 ext2fs_block_alloc_stats2(fs, blk, +1);
991 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
992 !ext2fs_test_block_bitmap2(meta_bmap, blk))
993 ext2fs_mark_block_bitmap2(rfs->move_blocks,
998 * Make sure the old inode table is reserved in the
999 * block reservation bitmap.
1001 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1002 j < fs->inode_blocks_per_group ; j++, blk++)
1003 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1006 group_blk += rfs->new_fs->super->s_blocks_per_group;
1012 ext2fs_free_block_bitmap(meta_bmap);
1018 * This helper function tries to allocate a new block. We try to
1019 * avoid hitting the original group descriptor blocks at least at
1020 * first, since we want to make it possible to recover from a badly
1021 * aborted resize operation as much as possible.
1023 * In the future, I may further modify this routine to balance out
1024 * where we get the new blocks across the various block groups.
1025 * Ideally we would allocate blocks that corresponded with the block
1026 * group of the containing inode, and keep contiguous blocks
1027 * together. However, this very difficult to do efficiently, since we
1028 * don't have the necessary information up front.
1032 #define DESPERATION 2
1034 static void init_block_alloc(ext2_resize_t rfs)
1036 rfs->alloc_state = AVOID_OLD;
1037 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1039 /* HACK for testing */
1040 if (ext2fs_blocks_count(rfs->new_fs->super) >
1041 ext2fs_blocks_count(rfs->old_fs->super))
1042 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1046 static blk_t get_new_block(ext2_resize_t rfs)
1048 ext2_filsys fs = rfs->new_fs;
1051 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1052 if (rfs->alloc_state == DESPERATION)
1055 #ifdef RESIZE2FS_DEBUG
1056 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1057 printf("Going into desperation mode "
1058 "for block allocations\n");
1060 rfs->alloc_state = DESPERATION;
1061 rfs->new_blk = fs->super->s_first_data_block;
1064 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1065 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1067 ((rfs->alloc_state == AVOID_OLD) &&
1068 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1069 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1074 return rfs->new_blk;
1078 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1081 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1084 blk = get_new_block(rfs);
1088 #ifdef RESIZE2FS_DEBUG
1089 if (rfs->flags & 0xF)
1090 printf("get_alloc_block allocating %u\n", blk);
1093 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1094 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1095 *ret = (blk64_t) blk;
1099 static errcode_t block_mover(ext2_resize_t rfs)
1101 blk_t blk, old_blk, new_blk;
1102 ext2_filsys fs = rfs->new_fs;
1103 ext2_filsys old_fs = rfs->old_fs;
1107 ext2_badblocks_list badblock_list = 0;
1108 int bb_modified = 0;
1110 fs->get_alloc_block = resize2fs_get_alloc_block;
1111 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1113 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1117 new_blk = fs->super->s_first_data_block;
1118 if (!rfs->itable_buf) {
1119 retval = ext2fs_get_array(fs->blocksize,
1120 fs->inode_blocks_per_group,
1125 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1130 * The first step is to figure out where all of the blocks
1133 to_move = moved = 0;
1134 init_block_alloc(rfs);
1135 for (blk = old_fs->super->s_first_data_block;
1136 blk < ext2fs_blocks_count(old_fs->super); blk++) {
1137 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1139 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1141 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1142 ext2fs_badblocks_list_del(badblock_list, blk);
1147 new_blk = get_new_block(rfs);
1152 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1153 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
1159 ext2fs_free_extent_table(rfs->bmap);
1167 * Step two is to actually move the blocks
1169 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1170 if (retval) goto errout;
1172 if (rfs->progress) {
1173 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1179 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1180 if (retval) goto errout;
1183 #ifdef RESIZE2FS_DEBUG
1184 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1185 printf("Moving %d blocks %u->%u\n",
1186 size, old_blk, new_blk);
1190 if (c > fs->inode_blocks_per_group)
1191 c = fs->inode_blocks_per_group;
1192 retval = io_channel_read_blk64(fs->io, old_blk, c,
1194 if (retval) goto errout;
1195 retval = io_channel_write_blk64(fs->io, new_blk, c,
1197 if (retval) goto errout;
1202 if (rfs->progress) {
1203 io_channel_flush(fs->io);
1204 retval = (rfs->progress)(rfs,
1205 E2_RSZ_BLOCK_RELOC_PASS,
1211 io_channel_flush(fs->io);
1215 if (badblock_list) {
1216 if (!retval && bb_modified)
1217 retval = ext2fs_update_bb_inode(old_fs,
1219 ext2fs_badblocks_list_free(badblock_list);
1225 /* --------------------------------------------------------------------
1227 * Resize processing, phase 3
1229 * --------------------------------------------------------------------
1233 struct process_block_struct {
1236 struct ext2_inode * inode;
1242 static int process_block(ext2_filsys fs, blk_t *block_nr,
1243 e2_blkcnt_t blockcnt,
1244 blk_t ref_block EXT2FS_ATTR((unused)),
1245 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1247 struct process_block_struct *pb;
1249 blk_t block, new_block;
1252 pb = (struct process_block_struct *) priv_data;
1254 if (pb->rfs->bmap) {
1255 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1257 *block_nr = new_block;
1258 ret |= BLOCK_CHANGED;
1260 #ifdef RESIZE2FS_DEBUG
1261 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1262 printf("ino=%u, blockcnt=%lld, %u->%u\n",
1263 pb->ino, blockcnt, block, new_block);
1269 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
1270 block, (int) blockcnt);
1282 static errcode_t progress_callback(ext2_filsys fs,
1283 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1284 dgrp_t group, void * priv_data)
1286 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1290 * This check is to protect against old ext2 libraries. It
1291 * shouldn't be needed against new libraries.
1296 if (rfs->progress) {
1297 io_channel_flush(fs->io);
1298 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1299 group+1, fs->group_desc_count);
1307 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1309 struct process_block_struct pb;
1310 ext2_ino_t ino, new_inode;
1311 struct ext2_inode *inode = NULL;
1312 ext2_inode_scan scan = NULL;
1314 char *block_buf = 0;
1315 ext2_ino_t start_to_move;
1316 blk_t orig_size, new_block;
1319 if ((rfs->old_fs->group_desc_count <=
1320 rfs->new_fs->group_desc_count) &&
1325 * Save the original size of the old filesystem, and
1326 * temporarily set the size to be the new size if the new size
1327 * is larger. We need to do this to avoid catching an error
1328 * by the block iterator routines
1330 orig_size = ext2fs_blocks_count(rfs->old_fs->super);
1331 if (orig_size < ext2fs_blocks_count(rfs->new_fs->super))
1332 ext2fs_blocks_count_set(rfs->old_fs->super,
1333 ext2fs_blocks_count(rfs->new_fs->super));
1335 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1336 if (retval) goto errout;
1338 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1339 if (retval) goto errout;
1340 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1341 if (retval) goto errout;
1343 start_to_move = (rfs->new_fs->group_desc_count *
1344 rfs->new_fs->super->s_inodes_per_group);
1346 if (rfs->progress) {
1347 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1348 0, rfs->old_fs->group_desc_count);
1352 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1356 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1357 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1358 inode = malloc(inode_size);
1364 * First, copy all of the inodes that need to be moved
1365 * elsewhere in the inode table
1368 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1369 if (retval) goto errout;
1373 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1374 continue; /* inode not in use */
1376 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1379 if (ext2fs_file_acl_block(inode) && rfs->bmap) {
1380 new_block = ext2fs_extent_translate(rfs->bmap,
1381 ext2fs_file_acl_block(inode));
1383 ext2fs_file_acl_block_set(inode, new_block);
1384 retval = ext2fs_write_inode_full(rfs->old_fs,
1385 ino, inode, inode_size);
1386 if (retval) goto errout;
1390 if (ext2fs_inode_has_valid_blocks(inode) &&
1391 (rfs->bmap || pb.is_dir)) {
1393 retval = ext2fs_block_iterate2(rfs->old_fs,
1395 process_block, &pb);
1404 if (ino <= start_to_move)
1405 continue; /* Don't need to move it. */
1410 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1414 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1417 /* Get the new version of the inode */
1418 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1420 if (retval) goto errout;
1422 inode->i_ctime = time(0);
1423 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1425 if (retval) goto errout;
1427 #ifdef RESIZE2FS_DEBUG
1428 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1429 printf("Inode moved %u->%u\n", ino, new_inode);
1432 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1436 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1438 io_channel_flush(rfs->old_fs->io);
1441 ext2fs_blocks_count_set(rfs->old_fs->super, orig_size);
1443 ext2fs_free_extent_table(rfs->bmap);
1447 ext2fs_close_inode_scan(scan);
1449 ext2fs_free_mem(&block_buf);
1454 /* --------------------------------------------------------------------
1456 * Resize processing, phase 4.
1458 * --------------------------------------------------------------------
1464 unsigned int max_dirs;
1468 static int check_and_change_inodes(ext2_ino_t dir,
1469 int entry EXT2FS_ATTR((unused)),
1470 struct ext2_dir_entry *dirent, int offset,
1471 int blocksize EXT2FS_ATTR((unused)),
1472 char *buf EXT2FS_ATTR((unused)),
1475 struct istruct *is = (struct istruct *) priv_data;
1476 struct ext2_inode inode;
1477 ext2_ino_t new_inode;
1480 if (is->rfs->progress && offset == 0) {
1481 io_channel_flush(is->rfs->old_fs->io);
1482 is->err = (is->rfs->progress)(is->rfs,
1483 E2_RSZ_INODE_REF_UPD_PASS,
1484 ++is->num, is->max_dirs);
1486 return DIRENT_ABORT;
1492 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1496 #ifdef RESIZE2FS_DEBUG
1497 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1498 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1499 dir, dirent->name_len&0xFF, dirent->name,
1500 dirent->inode, new_inode);
1503 dirent->inode = new_inode;
1505 /* Update the directory mtime and ctime */
1506 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1508 inode.i_mtime = inode.i_ctime = time(0);
1509 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1511 return DIRENT_ABORT;
1514 return DIRENT_CHANGED;
1517 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1526 * Now, we iterate over all of the directories to update the
1530 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1534 if (rfs->progress) {
1535 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1541 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1542 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1543 check_and_change_inodes, &is);
1551 if (rfs->progress && (is.num < is.max_dirs))
1552 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1553 is.max_dirs, is.max_dirs);
1556 ext2fs_free_extent_table(rfs->imap);
1562 /* --------------------------------------------------------------------
1564 * Resize processing, phase 5.
1566 * In this phase we actually move the inode table around, and then
1567 * update the summary statistics. This is scary, since aborting here
1568 * will potentially scramble the filesystem. (We are moving the
1569 * inode tables around in place, and so the potential for lost data,
1570 * or at the very least scrambling the mapping between filenames and
1571 * inode numbers is very high in case of a power failure here.)
1572 * --------------------------------------------------------------------
1577 * A very scary routine --- this one moves the inode table around!!!
1579 * After this you have to use the rfs->new_fs file handle to read and
1582 static errcode_t move_itables(ext2_resize_t rfs)
1584 int n, num, size, diff;
1585 dgrp_t i, max_groups;
1586 ext2_filsys fs = rfs->new_fs;
1588 blk_t old_blk, new_blk, blk;
1590 int j, to_move, moved;
1592 max_groups = fs->group_desc_count;
1593 if (max_groups > rfs->old_fs->group_desc_count)
1594 max_groups = rfs->old_fs->group_desc_count;
1596 size = fs->blocksize * fs->inode_blocks_per_group;
1597 if (!rfs->itable_buf) {
1598 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1604 * Figure out how many inode tables we need to move
1606 to_move = moved = 0;
1607 for (i=0; i < max_groups; i++)
1608 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
1609 ext2fs_inode_table_loc(fs, i))
1615 if (rfs->progress) {
1616 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1622 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1624 for (i=0; i < max_groups; i++) {
1625 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
1626 new_blk = ext2fs_inode_table_loc(fs, i);
1627 diff = new_blk - old_blk;
1629 #ifdef RESIZE2FS_DEBUG
1630 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1631 printf("Itable move group %d block %u->%u (diff %d)\n",
1632 i, old_blk, new_blk, diff);
1638 retval = io_channel_read_blk64(fs->io, old_blk,
1639 fs->inode_blocks_per_group,
1644 * The end of the inode table segment often contains
1645 * all zeros, and we're often only moving the inode
1646 * table down a block or two. If so, we can optimize
1647 * things by not rewriting blocks that we know to be zero
1650 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1653 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1654 #ifdef RESIZE2FS_DEBUG
1655 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1656 printf("%d blocks of zeros...\n", n);
1658 num = fs->inode_blocks_per_group;
1662 retval = io_channel_write_blk64(fs->io, new_blk,
1663 num, rfs->itable_buf);
1665 io_channel_write_blk64(fs->io, old_blk,
1666 num, rfs->itable_buf);
1670 retval = io_channel_write_blk64(fs->io,
1671 old_blk + fs->inode_blocks_per_group,
1672 diff, (rfs->itable_buf +
1673 (fs->inode_blocks_per_group - diff) *
1679 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1680 j < fs->inode_blocks_per_group ; j++, blk++)
1681 ext2fs_block_alloc_stats2(fs, blk, -1);
1683 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
1684 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1685 ext2fs_mark_super_dirty(rfs->old_fs);
1686 ext2fs_flush(rfs->old_fs);
1688 if (rfs->progress) {
1689 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1695 mark_table_blocks(fs, fs->block_map);
1697 #ifdef RESIZE2FS_DEBUG
1698 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1699 printf("Inode table move finished.\n");
1708 * Fix the resize inode
1710 static errcode_t fix_resize_inode(ext2_filsys fs)
1712 struct ext2_inode inode;
1717 if (!(fs->super->s_feature_compat &
1718 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1721 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1722 if (retval) goto errout;
1724 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1725 if (retval) goto errout;
1727 if (fs->super->s_reserved_gdt_blocks == 0) {
1728 fs->super->s_feature_compat &=
1729 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1730 ext2fs_mark_super_dirty(fs);
1732 if ((blk = inode.i_block[EXT2_DIND_BLOCK]) != 0)
1733 ext2fs_block_alloc_stats2(fs, blk, -1);
1735 memset(&inode, 0, sizeof(inode));
1737 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1741 ext2fs_iblk_set(fs, &inode, 1);
1743 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1744 if (retval) goto errout;
1746 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1748 * Avoid zeroing out block #0; that's rude. This
1749 * should never happen anyway since the filesystem
1750 * should be fsck'ed and we assume it is consistent.
1753 _("Should never happen: resize inode corrupt!\n"));
1757 memset(block_buf, 0, fs->blocksize);
1759 retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1761 if (retval) goto errout;
1763 retval = ext2fs_create_resize_inode(fs);
1769 ext2fs_free_mem(&block_buf);
1774 * Finally, recalculate the summary information
1776 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1780 unsigned int group = 0;
1781 unsigned int count = 0;
1785 blk_t super_blk, old_desc_blk, new_desc_blk;
1786 int old_desc_blocks;
1789 * First calculate the block statistics
1791 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1792 ext2fs_super_and_bgd_loc(fs, group, &super_blk, &old_desc_blk,
1794 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1795 old_desc_blocks = fs->super->s_first_meta_bg;
1797 old_desc_blocks = fs->desc_blocks +
1798 fs->super->s_reserved_gdt_blocks;
1799 for (blk = fs->super->s_first_data_block;
1800 blk < ext2fs_blocks_count(fs->super); blk++) {
1802 !((blk == super_blk) ||
1803 ((old_desc_blk && old_desc_blocks &&
1804 (blk >= old_desc_blk) &&
1805 (blk < old_desc_blk + old_desc_blocks))) ||
1806 ((new_desc_blk && (blk == new_desc_blk))) ||
1807 (blk == ext2fs_block_bitmap_loc(fs, group)) ||
1808 (blk == ext2fs_inode_bitmap_loc(fs, group)) ||
1809 ((blk >= ext2fs_inode_table_loc(fs, group) &&
1810 (blk < ext2fs_inode_table_loc(fs, group)
1811 + fs->inode_blocks_per_group))))) ||
1812 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
1817 if ((count == fs->super->s_blocks_per_group) ||
1818 (blk == ext2fs_blocks_count(fs->super)-1)) {
1819 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
1820 ext2fs_group_desc_csum_set(fs, group);
1824 uninit = (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)
1826 ext2fs_super_and_bgd_loc(fs, group, &super_blk,
1829 if (fs->super->s_feature_incompat &
1830 EXT2_FEATURE_INCOMPAT_META_BG)
1831 old_desc_blocks = fs->super->s_first_meta_bg;
1833 old_desc_blocks = fs->desc_blocks +
1834 fs->super->s_reserved_gdt_blocks;
1837 ext2fs_free_blocks_count_set(fs->super, total_free);
1840 * Next, calculate the inode statistics
1847 /* Protect loop from wrap-around if s_inodes_count maxed */
1848 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
1849 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1851 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1856 if ((count == fs->super->s_inodes_per_group) ||
1857 (ino == fs->super->s_inodes_count)) {
1858 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
1859 ext2fs_group_desc_csum_set(fs, group);
1863 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
1866 fs->super->s_free_inodes_count = total_free;
1867 ext2fs_mark_super_dirty(fs);
1872 * Journal may have been relocated; update the backup journal blocks
1873 * in the superblock.
1875 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
1878 struct ext2_inode inode;
1880 if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1883 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
1886 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
1887 fs->super->s_jnl_blocks[16] = inode.i_size;
1888 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
1889 ext2fs_mark_super_dirty(fs);
1894 * calcluate the minimum number of blocks the given fs can be resized to
1896 blk_t calculate_minimum_resize_size(ext2_filsys fs)
1898 blk_t inode_count, blks_needed, groups, data_blocks;
1899 blk_t grp, data_needed, last_start;
1900 int overhead = 0, num_of_superblocks = 0;
1901 int extra_groups = 0;
1902 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1905 * first figure out how many group descriptors we need to
1906 * handle the number of inodes we have
1908 inode_count = fs->super->s_inodes_count -
1909 fs->super->s_free_inodes_count;
1910 blks_needed = ext2fs_div_ceil(inode_count,
1911 fs->super->s_inodes_per_group) *
1912 EXT2_BLOCKS_PER_GROUP(fs->super);
1913 groups = ext2fs_div_ceil(blks_needed,
1914 EXT2_BLOCKS_PER_GROUP(fs->super));
1917 * we need to figure out how many backup superblocks we have so we can
1918 * account for that in the metadata
1920 for (grp = 0; grp < fs->group_desc_count; grp++) {
1921 if (ext2fs_bg_has_super(fs, grp))
1922 num_of_superblocks++;
1925 /* calculate how many blocks are needed for data */
1926 data_needed = ext2fs_blocks_count(fs->super) -
1927 ext2fs_free_blocks_count(fs->super);
1928 data_needed -= SUPER_OVERHEAD(fs) * num_of_superblocks;
1929 data_needed -= META_OVERHEAD(fs) * fs->group_desc_count;
1931 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1933 * For ext4 we need to allow for up to a flex_bg worth
1934 * of inode tables of slack space so the resize
1935 * operation can be guaranteed to finish.
1937 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
1938 data_needed += META_OVERHEAD(fs) * extra_groups;
1939 extra_groups = groups % flexbg_size;
1943 * figure out how many data blocks we have given the number of groups
1944 * we need for our inodes
1946 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
1948 for (grp = 0; grp < groups; grp++) {
1949 overhead = META_OVERHEAD(fs);
1951 if (ext2fs_bg_has_super(fs, grp))
1952 overhead += SUPER_OVERHEAD(fs);
1955 * we want to keep track of how much data we can store in
1956 * the groups leading up to the last group so we can determine
1957 * how big the last group needs to be
1959 if (grp != (groups - 1))
1960 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
1963 data_blocks -= overhead;
1967 * if we need more group descriptors in order to accomodate our data
1968 * then we need to add them here
1970 while (data_needed > data_blocks) {
1971 blk_t remainder = data_needed - data_blocks;
1974 /* figure out how many more groups we need for the data */
1975 extra_grps = ext2fs_div_ceil(remainder,
1976 EXT2_BLOCKS_PER_GROUP(fs->super));
1978 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
1980 /* ok we have to account for the last group */
1981 overhead = META_OVERHEAD(fs);
1982 if (ext2fs_bg_has_super(fs, groups-1))
1983 overhead += SUPER_OVERHEAD(fs);
1984 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
1986 for (grp = groups; grp < groups+extra_grps; grp++) {
1987 overhead = META_OVERHEAD(fs);
1988 if (ext2fs_bg_has_super(fs, grp))
1989 overhead += SUPER_OVERHEAD(fs);
1992 * again, we need to see how much data we cram into
1993 * all of the groups leading up to the last group
1995 if (grp != (groups + extra_grps - 1))
1996 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
1999 data_blocks -= overhead;
2002 groups += extra_grps;
2003 extra_groups += extra_grps;
2004 if (fs->super->s_feature_incompat
2005 & EXT4_FEATURE_INCOMPAT_FLEX_BG
2006 && extra_groups > flexbg_size) {
2008 * For ext4 we need to allow for up to a flex_bg worth
2009 * of inode tables of slack space so the resize
2010 * operation can be guaranteed to finish.
2012 extra_groups = flexbg_size -
2013 (groups & (flexbg_size - 1));
2014 data_needed += META_OVERHEAD(fs) * extra_groups;
2015 extra_groups = groups % flexbg_size;
2019 /* now for the fun voodoo */
2020 overhead = META_OVERHEAD(fs);
2023 * if this is the case then the last group is going to have data in it
2024 * so we need to adjust the size of the last group accordingly
2026 if (last_start < data_needed) {
2027 blk_t remainder = data_needed - last_start;
2030 * 50 is a magic number that mkfs/resize uses to see if its
2031 * even worth making/resizing the fs. basically you need to
2032 * have at least 50 blocks in addition to the blocks needed
2033 * for the metadata in the last group
2036 overhead += remainder;
2042 if (ext2fs_bg_has_super(fs, groups-1))
2043 overhead += SUPER_OVERHEAD(fs);
2046 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2047 * only do groups-1, and then add the number of blocks needed to
2048 * handle the group descriptor metadata+data that we need
2050 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2051 blks_needed += overhead;
2054 * If at this point we've already added up more "needed" than
2055 * the current size, just return current size as minimum.
2057 if (blks_needed >= ext2fs_blocks_count(fs->super))
2058 return ext2fs_blocks_count(fs->super);
2060 * We need to reserve a few extra blocks if extents are
2061 * enabled, in case we need to grow the extent tree. The more
2062 * we shrink the file system, the more space we need.
2064 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
2065 blks_needed += (ext2fs_blocks_count(fs->super) -