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)))
65 /* Some bigalloc helper macros which are more succint... */
66 #define B2C(x) EXT2FS_B2C(fs, (x))
67 #define C2B(x) EXT2FS_C2B(fs, (x))
68 #define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
69 #define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
70 #define LT_CLSTR(x, y) (B2C(x) < B2C(y))
71 #define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
72 #define GT_CLSTR(x, y) (B2C(x) > B2C(y))
77 * This is the top-level routine which does the dirty deed....
79 errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
80 errcode_t (*progress)(ext2_resize_t rfs, int pass,
82 unsigned long max_val))
86 struct resource_track rtrack, overall_track;
89 * Create the data structure
91 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
95 memset(rfs, 0, sizeof(struct ext2_resize_struct));
100 rfs->progress = progress;
102 init_resource_track(&overall_track, "overall resize2fs", fs->io);
103 init_resource_track(&rtrack, "read_bitmaps", fs->io);
104 retval = ext2fs_read_bitmaps(fs);
107 print_resource_track(rfs, &rtrack, fs->io);
109 fs->super->s_state |= EXT2_ERROR_FS;
110 ext2fs_mark_super_dirty(fs);
113 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io);
114 fix_uninit_block_bitmaps(fs);
115 print_resource_track(rfs, &rtrack, fs->io);
116 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
120 init_resource_track(&rtrack, "adjust_superblock", fs->io);
121 retval = adjust_superblock(rfs, *new_size);
124 print_resource_track(rfs, &rtrack, fs->io);
127 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io);
128 fix_uninit_block_bitmaps(rfs->new_fs);
129 print_resource_track(rfs, &rtrack, fs->io);
130 /* Clear the block bitmap uninit flag for the last block group */
131 ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
132 EXT2_BG_BLOCK_UNINIT);
134 *new_size = ext2fs_blocks_count(rfs->new_fs->super);
136 init_resource_track(&rtrack, "blocks_to_move", fs->io);
137 retval = blocks_to_move(rfs);
140 print_resource_track(rfs, &rtrack, fs->io);
142 #ifdef RESIZE2FS_DEBUG
143 if (rfs->flags & RESIZE_DEBUG_BMOVE)
144 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
145 ext2fs_free_blocks_count(rfs->old_fs->super),
146 ext2fs_free_blocks_count(rfs->new_fs->super),
150 init_resource_track(&rtrack, "block_mover", fs->io);
151 retval = block_mover(rfs);
154 print_resource_track(rfs, &rtrack, fs->io);
156 init_resource_track(&rtrack, "inode_scan_and_fix", fs->io);
157 retval = inode_scan_and_fix(rfs);
160 print_resource_track(rfs, &rtrack, fs->io);
162 init_resource_track(&rtrack, "inode_ref_fix", fs->io);
163 retval = inode_ref_fix(rfs);
166 print_resource_track(rfs, &rtrack, fs->io);
168 init_resource_track(&rtrack, "move_itables", fs->io);
169 retval = move_itables(rfs);
172 print_resource_track(rfs, &rtrack, fs->io);
174 init_resource_track(&rtrack, "calculate_summary_stats", fs->io);
175 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
178 print_resource_track(rfs, &rtrack, fs->io);
180 init_resource_track(&rtrack, "fix_resize_inode", fs->io);
181 retval = fix_resize_inode(rfs->new_fs);
184 print_resource_track(rfs, &rtrack, fs->io);
186 init_resource_track(&rtrack, "fix_sb_journal_backup", fs->io);
187 retval = fix_sb_journal_backup(rfs->new_fs);
190 print_resource_track(rfs, &rtrack, fs->io);
192 rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
193 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
195 print_resource_track(rfs, &overall_track, fs->io);
196 retval = ext2fs_close(rfs->new_fs);
202 ext2fs_free(rfs->old_fs);
204 ext2fs_free_mem(&rfs->itable_buf);
205 if (rfs->reserve_blocks)
206 ext2fs_free_block_bitmap(rfs->reserve_blocks);
207 if (rfs->move_blocks)
208 ext2fs_free_block_bitmap(rfs->move_blocks);
209 ext2fs_free_mem(&rfs);
215 ext2fs_free(rfs->new_fs);
217 ext2fs_free_mem(&rfs->itable_buf);
218 ext2fs_free_mem(&rfs);
223 * Clean up the bitmaps for unitialized bitmaps
225 static void fix_uninit_block_bitmaps(ext2_filsys fs)
231 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
232 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
235 for (g=0; g < fs->group_desc_count; g++) {
236 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
239 blk = ext2fs_group_first_block2(fs, g);
240 lblk = ext2fs_group_last_block2(fs, g);
241 ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
244 ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
245 ext2fs_mark_block_bitmap2(fs->block_map,
246 ext2fs_block_bitmap_loc(fs, g));
247 ext2fs_mark_block_bitmap2(fs->block_map,
248 ext2fs_inode_bitmap_loc(fs, g));
249 for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
250 i < (unsigned int) fs->inode_blocks_per_group;
252 ext2fs_mark_block_bitmap2(fs->block_map, blk);
256 /* --------------------------------------------------------------------
258 * Resize processing, phase 1.
260 * In this phase we adjust the in-memory superblock information, and
261 * initialize any new parts of the inode table. The new parts of the
262 * inode table are created in virgin disk space, so we can abort here
263 * without any side effects.
264 * --------------------------------------------------------------------
268 * If the group descriptor's bitmap and inode table blocks are valid,
269 * release them in the new filesystem data structure, and mark them as
270 * reserved so the old inode table blocks don't get overwritten.
272 static void free_gdp_blocks(ext2_filsys fs,
273 ext2fs_block_bitmap reserve_blocks,
274 struct ext2_group_desc *gdp)
279 if (gdp->bg_block_bitmap &&
280 (gdp->bg_block_bitmap < ext2fs_blocks_count(fs->super))) {
281 ext2fs_block_alloc_stats(fs, gdp->bg_block_bitmap, -1);
282 ext2fs_mark_block_bitmap2(reserve_blocks,
283 gdp->bg_block_bitmap);
286 if (gdp->bg_inode_bitmap &&
287 (gdp->bg_inode_bitmap < ext2fs_blocks_count(fs->super))) {
288 ext2fs_block_alloc_stats(fs, gdp->bg_inode_bitmap, -1);
289 ext2fs_mark_block_bitmap2(reserve_blocks,
290 gdp->bg_inode_bitmap);
293 if (gdp->bg_inode_table == 0 ||
294 (gdp->bg_inode_table >= ext2fs_blocks_count(fs->super)))
297 for (blk = gdp->bg_inode_table, j = 0;
298 j < fs->inode_blocks_per_group; j++, blk++) {
299 if (blk >= ext2fs_blocks_count(fs->super))
301 ext2fs_block_alloc_stats(fs, blk, -1);
302 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
307 * This routine is shared by the online and offline resize routines.
308 * All of the information which is adjusted in memory is done here.
310 * The reserve_blocks parameter is only needed when shrinking the
313 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
314 ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
317 blk64_t overhead = 0;
319 blk64_t blk, group_block;
321 blk64_t old_numblocks, numblocks, adjblocks;
322 unsigned long i, j, old_desc_blocks;
323 unsigned int meta_bg, meta_bg_size;
324 int has_super, csum_flag;
325 unsigned long long new_inodes; /* u64 to check for overflow */
328 ext2fs_blocks_count_set(fs->super, new_size);
331 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
332 fs->super->s_first_data_block,
333 EXT2_BLOCKS_PER_GROUP(fs->super));
334 if (fs->group_desc_count == 0)
335 return EXT2_ET_TOOSMALL;
336 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
337 EXT2_DESC_PER_BLOCK(fs->super));
340 * Overhead is the number of bookkeeping blocks per group. It
341 * includes the superblock backup, the group descriptor
342 * backups, the inode bitmap, the block bitmap, and the inode
345 overhead = (int) (2 + fs->inode_blocks_per_group);
347 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
348 overhead += 1 + fs->desc_blocks +
349 fs->super->s_reserved_gdt_blocks;
352 * See if the last group is big enough to support the
353 * necessary data structures. If not, we need to get rid of
356 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
357 fs->super->s_blocks_per_group;
358 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
359 return EXT2_ET_TOOSMALL;
360 if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
361 ext2fs_blocks_count_set(fs->super,
362 ext2fs_blocks_count(fs->super) - rem);
366 * Adjust the number of inodes
368 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
369 if (new_inodes > ~0U) {
370 fprintf(stderr, _("inodes (%llu) must be less than %u"),
372 return EXT2_ET_TOO_MANY_INODES;
374 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
375 fs->group_desc_count;
378 * Adjust the number of free blocks
380 blk = ext2fs_blocks_count(old_fs->super);
381 if (blk > ext2fs_blocks_count(fs->super))
382 ext2fs_free_blocks_count_set(fs->super,
383 ext2fs_free_blocks_count(fs->super) -
384 (blk - ext2fs_blocks_count(fs->super)));
386 ext2fs_free_blocks_count_set(fs->super,
387 ext2fs_free_blocks_count(fs->super) +
388 (ext2fs_blocks_count(fs->super) - blk));
391 * Adjust the number of reserved blocks
393 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
394 ext2fs_blocks_count(old_fs->super);
395 ext2fs_r_blocks_count_set(fs->super,
396 (percent * ext2fs_blocks_count(fs->super) /
400 * Adjust the bitmaps for size
402 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
403 fs->super->s_inodes_count,
405 if (retval) goto errout;
407 real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) *
408 fs->group_desc_count)) - 1 +
409 fs->super->s_first_data_block;
410 retval = ext2fs_resize_block_bitmap2(ext2fs_blocks_count(fs->super)-1,
411 real_end, fs->block_map);
413 if (retval) goto errout;
416 * Reallocate the group descriptors as necessary.
418 if (old_fs->desc_blocks != fs->desc_blocks) {
419 retval = ext2fs_resize_mem(old_fs->desc_blocks *
421 fs->desc_blocks * fs->blocksize,
425 if (fs->desc_blocks > old_fs->desc_blocks)
426 memset((char *) fs->group_desc +
427 (old_fs->desc_blocks * fs->blocksize), 0,
428 (fs->desc_blocks - old_fs->desc_blocks) *
433 * If the resize_inode feature is set, and we are changing the
434 * number of descriptor blocks, then adjust
435 * s_reserved_gdt_blocks if possible to avoid needing to move
436 * the inode table either now or in the future.
438 if ((fs->super->s_feature_compat &
439 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
440 (old_fs->desc_blocks != fs->desc_blocks)) {
443 new = ((int) fs->super->s_reserved_gdt_blocks) +
444 (old_fs->desc_blocks - fs->desc_blocks);
447 if (new > (int) fs->blocksize/4)
448 new = fs->blocksize/4;
449 fs->super->s_reserved_gdt_blocks = new;
453 * If we are shrinking the number of block groups, we're done
456 if (old_fs->group_desc_count > fs->group_desc_count) {
458 * Check the block groups that we are chopping off
459 * and free any blocks associated with their metadata
461 for (i = fs->group_desc_count;
462 i < old_fs->group_desc_count; i++) {
463 free_gdp_blocks(fs, reserve_blocks,
464 ext2fs_group_desc(old_fs,
465 old_fs->group_desc, i));
472 * Fix the count of the last (old) block group
474 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
475 old_fs->super->s_first_data_block) %
476 old_fs->super->s_blocks_per_group;
478 old_numblocks = old_fs->super->s_blocks_per_group;
479 if (old_fs->group_desc_count == fs->group_desc_count) {
480 numblocks = (ext2fs_blocks_count(fs->super) -
481 fs->super->s_first_data_block) %
482 fs->super->s_blocks_per_group;
484 numblocks = fs->super->s_blocks_per_group;
486 numblocks = fs->super->s_blocks_per_group;
487 i = old_fs->group_desc_count - 1;
488 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
489 ext2fs_group_desc_csum_set(fs, i);
492 * If the number of block groups is staying the same, we're
493 * done and can exit now. (If the number block groups is
494 * shrinking, we had exited earlier.)
496 if (old_fs->group_desc_count >= fs->group_desc_count) {
502 * Initialize the new block group descriptors
504 group_block = ext2fs_group_first_block2(fs,
505 old_fs->group_desc_count);
506 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
507 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
508 if (access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0)
509 lazy_itable_init = 1;
510 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
511 old_desc_blocks = fs->super->s_first_meta_bg;
513 old_desc_blocks = fs->desc_blocks +
514 fs->super->s_reserved_gdt_blocks;
515 for (i = old_fs->group_desc_count;
516 i < fs->group_desc_count; i++) {
517 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
518 sizeof(struct ext2_group_desc));
521 ext2fs_bg_flags_zap(fs, i);
523 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
524 if (!lazy_itable_init)
525 ext2fs_bg_flags_set(fs, i,
526 EXT2_BG_INODE_ZEROED);
527 ext2fs_bg_itable_unused_set(fs, i,
528 fs->super->s_inodes_per_group);
531 numblocks = ext2fs_group_blocks_count(fs, i);
532 if ((i < fs->group_desc_count - 1) && csum_flag)
533 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
535 has_super = ext2fs_bg_has_super(fs, i);
537 ext2fs_block_alloc_stats2(fs, group_block, +1);
540 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
541 meta_bg = i / meta_bg_size;
542 if (!(fs->super->s_feature_incompat &
543 EXT2_FEATURE_INCOMPAT_META_BG) ||
544 (meta_bg < fs->super->s_first_meta_bg)) {
546 for (j=0; j < old_desc_blocks; j++)
547 ext2fs_block_alloc_stats2(fs,
548 group_block + 1 + j, +1);
549 adjblocks += old_desc_blocks;
554 if (((i % meta_bg_size) == 0) ||
555 ((i % meta_bg_size) == 1) ||
556 ((i % meta_bg_size) == (meta_bg_size-1)))
557 ext2fs_block_alloc_stats2(fs,
558 group_block + has_super, +1);
561 adjblocks += 2 + fs->inode_blocks_per_group;
563 numblocks -= adjblocks;
564 ext2fs_free_blocks_count_set(fs->super,
565 ext2fs_free_blocks_count(fs->super) - adjblocks);
566 fs->super->s_free_inodes_count +=
567 fs->super->s_inodes_per_group;
568 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
569 ext2fs_bg_free_inodes_count_set(fs, i,
570 fs->super->s_inodes_per_group);
571 ext2fs_bg_used_dirs_count_set(fs, i, 0);
572 ext2fs_group_desc_csum_set(fs, i);
574 retval = ext2fs_allocate_group_table(fs, i, 0);
575 if (retval) goto errout;
577 group_block += fs->super->s_blocks_per_group;
586 * This routine adjusts the superblock and other data structures, both
587 * in disk as well as in memory...
589 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
596 unsigned long max_group;
599 ext2fs_mark_super_dirty(fs);
600 ext2fs_mark_bb_dirty(fs);
601 ext2fs_mark_ib_dirty(fs);
603 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
604 &rfs->reserve_blocks);
608 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
613 * Check to make sure there are enough inodes
615 if ((rfs->old_fs->super->s_inodes_count -
616 rfs->old_fs->super->s_free_inodes_count) >
617 rfs->new_fs->super->s_inodes_count) {
623 * If we are shrinking the number block groups, we're done and
626 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
632 * If the number of block groups is staying the same, we're
633 * done and can exit now. (If the number block groups is
634 * shrinking, we had exited earlier.)
636 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
642 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
643 * supports lazy inode initialization, we can skip
644 * initializing the inode table.
646 if (lazy_itable_init &&
647 EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
648 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
654 * Initialize the inode table
656 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
661 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
662 group_block = ext2fs_group_first_block2(fs,
663 rfs->old_fs->group_desc_count);
664 adj = rfs->old_fs->group_desc_count;
665 max_group = fs->group_desc_count - adj;
667 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
672 for (i = rfs->old_fs->group_desc_count;
673 i < fs->group_desc_count; i++) {
675 * Write out the new inode table
677 retval = io_channel_write_blk64(fs->io,
678 ext2fs_inode_table_loc(fs, i),
679 fs->inode_blocks_per_group,
681 if (retval) goto errout;
683 io_channel_flush(fs->io);
685 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
686 i - adj + 1, max_group);
690 group_block += fs->super->s_blocks_per_group;
692 io_channel_flush(fs->io);
699 /* --------------------------------------------------------------------
701 * Resize processing, phase 2.
703 * In this phase we adjust determine which blocks need to be moved, in
704 * blocks_to_move(). We then copy the blocks to their ultimate new
705 * destinations using block_mover(). Since we are copying blocks to
706 * their new locations, again during this pass we can abort without
708 * --------------------------------------------------------------------
712 * This helper function creates a block bitmap with all of the
713 * filesystem meta-data blocks.
715 static errcode_t mark_table_blocks(ext2_filsys fs,
716 ext2fs_block_bitmap bmap)
720 for (i = 0; i < fs->group_desc_count; i++) {
721 ext2fs_reserve_super_and_bgd(fs, i, bmap);
724 * Mark the blocks used for the inode table
726 ext2fs_mark_block_bitmap_range2(bmap,
727 ext2fs_inode_table_loc(fs, i),
728 fs->inode_blocks_per_group);
731 * Mark block used for the block bitmap
733 ext2fs_mark_block_bitmap2(bmap,
734 ext2fs_block_bitmap_loc(fs, i));
737 * Mark block used for the inode bitmap
739 ext2fs_mark_block_bitmap2(bmap,
740 ext2fs_inode_bitmap_loc(fs, i));
746 * This function checks to see if a particular block (either a
747 * superblock or a block group descriptor) overlaps with an inode or
748 * block bitmap block, or with the inode table.
750 static void mark_fs_metablock(ext2_resize_t rfs,
751 ext2fs_block_bitmap meta_bmap,
752 int group, blk64_t blk)
754 ext2_filsys fs = rfs->new_fs;
756 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
757 ext2fs_block_alloc_stats2(fs, blk, +1);
760 * Check to see if we overlap with the inode or block bitmap,
761 * or the inode tables. If not, and the block is in use, then
762 * mark it as a block to be moved.
764 if (IS_BLOCK_BM(fs, group, blk)) {
765 ext2fs_block_bitmap_loc_set(fs, group, 0);
766 rfs->needed_blocks++;
769 if (IS_INODE_BM(fs, group, blk)) {
770 ext2fs_inode_bitmap_loc_set(fs, group, 0);
771 rfs->needed_blocks++;
774 if (IS_INODE_TB(fs, group, blk)) {
775 ext2fs_inode_table_loc_set(fs, group, 0);
776 rfs->needed_blocks++;
779 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
782 for (i=0; i < rfs->old_fs->group_desc_count; i++) {
783 if (IS_BLOCK_BM(fs, i, blk)) {
784 ext2fs_block_bitmap_loc_set(fs, i, 0);
785 rfs->needed_blocks++;
788 if (IS_INODE_BM(fs, i, blk)) {
789 ext2fs_inode_bitmap_loc_set(fs, i, 0);
790 rfs->needed_blocks++;
793 if (IS_INODE_TB(fs, i, blk)) {
794 ext2fs_inode_table_loc_set(fs, i, 0);
795 rfs->needed_blocks++;
800 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
801 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
802 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
804 * If the block bitmap is uninitialized, which means
805 * nothing other than standard metadata in use.
808 } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
809 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
810 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
811 rfs->needed_blocks++;
817 * This routine marks and unmarks reserved blocks in the new block
818 * bitmap. It also determines which blocks need to be moved and
819 * places this information into the move_blocks bitmap.
821 static errcode_t blocks_to_move(ext2_resize_t rfs)
824 dgrp_t i, max_groups, g;
825 blk64_t blk, group_blk;
826 blk64_t old_blocks, new_blocks;
827 unsigned int meta_bg, meta_bg_size;
829 ext2_filsys fs, old_fs;
830 ext2fs_block_bitmap meta_bmap;
834 old_fs = rfs->old_fs;
835 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
838 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
843 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
848 retval = mark_table_blocks(old_fs, meta_bmap);
855 * If we're shrinking the filesystem, we need to move all of
856 * the blocks that don't fit any more
858 for (blk = ext2fs_blocks_count(fs->super);
859 blk < ext2fs_blocks_count(old_fs->super); blk++) {
860 g = ext2fs_group_of_blk2(fs, blk);
861 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
862 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
863 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
865 * The block bitmap is uninitialized, so skip
866 * to the next block group.
868 blk = ext2fs_group_first_block2(fs, g+1) - 1;
871 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
872 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
873 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
874 rfs->needed_blocks++;
876 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
879 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
880 old_blocks = old_fs->super->s_first_meta_bg;
881 new_blocks = fs->super->s_first_meta_bg;
883 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
884 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
887 if (old_blocks == new_blocks) {
892 max_groups = fs->group_desc_count;
893 if (max_groups > old_fs->group_desc_count)
894 max_groups = old_fs->group_desc_count;
895 group_blk = old_fs->super->s_first_data_block;
897 * If we're reducing the number of descriptor blocks, this
898 * makes life easy. :-) We just have to mark some extra
901 if (old_blocks > new_blocks) {
902 for (i = 0; i < max_groups; i++) {
903 if (!ext2fs_bg_has_super(fs, i)) {
904 group_blk += fs->super->s_blocks_per_group;
907 for (blk = group_blk+1+new_blocks;
908 blk < group_blk+1+old_blocks; blk++) {
909 ext2fs_block_alloc_stats2(fs, blk, -1);
910 rfs->needed_blocks--;
912 group_blk += fs->super->s_blocks_per_group;
918 * If we're increasing the number of descriptor blocks, life
919 * gets interesting....
921 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
922 flex_bg = fs->super->s_feature_incompat &
923 EXT4_FEATURE_INCOMPAT_FLEX_BG;
924 /* first reserve all of the existing fs meta blocks */
925 for (i = 0; i < max_groups; i++) {
926 has_super = ext2fs_bg_has_super(fs, i);
928 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
930 meta_bg = i / meta_bg_size;
931 if (!(fs->super->s_feature_incompat &
932 EXT2_FEATURE_INCOMPAT_META_BG) ||
933 (meta_bg < fs->super->s_first_meta_bg)) {
935 for (blk = group_blk+1;
936 blk < group_blk + 1 + new_blocks; blk++)
937 mark_fs_metablock(rfs, meta_bmap,
943 if (((i % meta_bg_size) == 0) ||
944 ((i % meta_bg_size) == 1) ||
945 ((i % meta_bg_size) == (meta_bg_size-1)))
946 mark_fs_metablock(rfs, meta_bmap, i,
947 group_blk + has_super);
951 * Reserve the existing meta blocks that we know
952 * aren't to be moved.
954 * For flex_bg file systems, in order to avoid
955 * overwriting fs metadata (especially inode table
956 * blocks) belonging to a different block group when
957 * we are relocating the inode tables, we need to
958 * reserve all existing fs metadata blocks.
960 if (ext2fs_block_bitmap_loc(fs, i))
961 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
962 ext2fs_block_bitmap_loc(fs, i));
963 else if (flex_bg && i < old_fs->group_desc_count)
964 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
965 ext2fs_block_bitmap_loc(old_fs, i));
967 if (ext2fs_inode_bitmap_loc(fs, i))
968 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
969 ext2fs_inode_bitmap_loc(fs, i));
970 else if (flex_bg && i < old_fs->group_desc_count)
971 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
972 ext2fs_inode_bitmap_loc(old_fs, i));
974 if (ext2fs_inode_table_loc(fs, i))
975 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
976 ext2fs_inode_table_loc(fs, i),
977 fs->inode_blocks_per_group);
978 else if (flex_bg && i < old_fs->group_desc_count)
979 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
980 ext2fs_inode_table_loc(old_fs, i),
981 old_fs->inode_blocks_per_group);
983 group_blk += rfs->new_fs->super->s_blocks_per_group;
986 /* Allocate the missing data structures */
987 for (i = 0; i < max_groups; i++) {
988 if (ext2fs_inode_table_loc(fs, i) &&
989 ext2fs_inode_bitmap_loc(fs, i) &&
990 ext2fs_block_bitmap_loc(fs, i))
993 retval = ext2fs_allocate_group_table(fs, i,
994 rfs->reserve_blocks);
999 * For those structures that have changed, we need to
1002 if (ext2fs_block_bitmap_loc(old_fs, i) !=
1003 (blk = ext2fs_block_bitmap_loc(fs, i))) {
1004 ext2fs_block_alloc_stats2(fs, blk, +1);
1005 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1006 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1007 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1010 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
1011 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
1012 ext2fs_block_alloc_stats2(fs, blk, +1);
1013 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1014 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1015 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1020 * The inode table, if we need to relocate it, is
1021 * handled specially. We have to reserve the blocks
1022 * for both the old and the new inode table, since we
1023 * can't have the inode table be destroyed during the
1024 * block relocation phase.
1026 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
1027 continue; /* inode table not moved */
1029 rfs->needed_blocks += fs->inode_blocks_per_group;
1032 * Mark the new inode table as in use in the new block
1033 * allocation bitmap, and move any blocks that might
1036 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
1037 j < fs->inode_blocks_per_group ; j++, blk++) {
1038 ext2fs_block_alloc_stats2(fs, blk, +1);
1039 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1040 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1041 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1046 * Make sure the old inode table is reserved in the
1047 * block reservation bitmap.
1049 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1050 j < fs->inode_blocks_per_group ; j++, blk++)
1051 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1057 ext2fs_free_block_bitmap(meta_bmap);
1063 * This helper function tries to allocate a new block. We try to
1064 * avoid hitting the original group descriptor blocks at least at
1065 * first, since we want to make it possible to recover from a badly
1066 * aborted resize operation as much as possible.
1068 * In the future, I may further modify this routine to balance out
1069 * where we get the new blocks across the various block groups.
1070 * Ideally we would allocate blocks that corresponded with the block
1071 * group of the containing inode, and keep contiguous blocks
1072 * together. However, this very difficult to do efficiently, since we
1073 * don't have the necessary information up front.
1077 #define DESPERATION 2
1079 static void init_block_alloc(ext2_resize_t rfs)
1081 rfs->alloc_state = AVOID_OLD;
1082 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1084 /* HACK for testing */
1085 if (ext2fs_blocks_count(rfs->new_fs->super) >
1086 ext2fs_blocks_count(rfs->old_fs->super))
1087 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1091 static blk64_t get_new_block(ext2_resize_t rfs)
1093 ext2_filsys fs = rfs->new_fs;
1096 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1097 if (rfs->alloc_state == DESPERATION)
1100 #ifdef RESIZE2FS_DEBUG
1101 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1102 printf("Going into desperation mode "
1103 "for block allocations\n");
1105 rfs->alloc_state = DESPERATION;
1106 rfs->new_blk = fs->super->s_first_data_block;
1109 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1110 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1112 ((rfs->alloc_state == AVOID_OLD) &&
1113 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1114 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1119 return rfs->new_blk;
1123 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1126 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1129 blk = get_new_block(rfs);
1133 #ifdef RESIZE2FS_DEBUG
1134 if (rfs->flags & 0xF)
1135 printf("get_alloc_block allocating %llu\n", blk);
1138 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1139 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1140 *ret = (blk64_t) blk;
1144 static errcode_t block_mover(ext2_resize_t rfs)
1146 blk64_t blk, old_blk, new_blk;
1147 ext2_filsys fs = rfs->new_fs;
1148 ext2_filsys old_fs = rfs->old_fs;
1153 ext2_badblocks_list badblock_list = 0;
1154 int bb_modified = 0;
1156 fs->get_alloc_block = resize2fs_get_alloc_block;
1157 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1159 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1163 new_blk = fs->super->s_first_data_block;
1164 if (!rfs->itable_buf) {
1165 retval = ext2fs_get_array(fs->blocksize,
1166 fs->inode_blocks_per_group,
1171 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1176 * The first step is to figure out where all of the blocks
1179 to_move = moved = 0;
1180 init_block_alloc(rfs);
1181 for (blk = B2C(old_fs->super->s_first_data_block);
1182 blk < ext2fs_blocks_count(old_fs->super);
1183 blk += EXT2FS_CLUSTER_RATIO(fs)) {
1184 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1186 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1188 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1189 ext2fs_badblocks_list_del(badblock_list, blk);
1194 new_blk = get_new_block(rfs);
1199 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1200 ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk));
1206 ext2fs_free_extent_table(rfs->bmap);
1214 * Step two is to actually move the blocks
1216 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1217 if (retval) goto errout;
1219 if (rfs->progress) {
1220 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1226 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1227 if (retval) goto errout;
1230 old_blk = C2B(old_blk);
1231 new_blk = C2B(new_blk);
1233 #ifdef RESIZE2FS_DEBUG
1234 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1235 printf("Moving %llu blocks %llu->%llu\n",
1236 size, old_blk, new_blk);
1240 if (c > fs->inode_blocks_per_group)
1241 c = fs->inode_blocks_per_group;
1242 retval = io_channel_read_blk64(fs->io, old_blk, c,
1244 if (retval) goto errout;
1245 retval = io_channel_write_blk64(fs->io, new_blk, c,
1247 if (retval) goto errout;
1252 if (rfs->progress) {
1253 io_channel_flush(fs->io);
1254 retval = (rfs->progress)(rfs,
1255 E2_RSZ_BLOCK_RELOC_PASS,
1261 io_channel_flush(fs->io);
1265 if (badblock_list) {
1266 if (!retval && bb_modified)
1267 retval = ext2fs_update_bb_inode(old_fs,
1269 ext2fs_badblocks_list_free(badblock_list);
1275 /* --------------------------------------------------------------------
1277 * Resize processing, phase 3
1279 * --------------------------------------------------------------------
1284 * The extent translation table is stored in clusters so we need to
1285 * take special care when mapping a source block number to its
1286 * destination block number.
1288 __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc)
1290 __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc)));
1293 new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1);
1297 struct process_block_struct {
1300 struct ext2_inode * inode;
1306 static int process_block(ext2_filsys fs, blk64_t *block_nr,
1307 e2_blkcnt_t blockcnt,
1308 blk64_t ref_block EXT2FS_ATTR((unused)),
1309 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1311 struct process_block_struct *pb;
1313 blk64_t block, new_block;
1316 pb = (struct process_block_struct *) priv_data;
1318 if (pb->rfs->bmap) {
1319 new_block = extent_translate(fs, pb->rfs->bmap, block);
1321 *block_nr = new_block;
1322 ret |= BLOCK_CHANGED;
1324 #ifdef RESIZE2FS_DEBUG
1325 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1326 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1327 pb->ino, blockcnt, block, new_block);
1333 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1334 block, (int) blockcnt);
1346 static errcode_t progress_callback(ext2_filsys fs,
1347 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1348 dgrp_t group, void * priv_data)
1350 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1354 * This check is to protect against old ext2 libraries. It
1355 * shouldn't be needed against new libraries.
1360 if (rfs->progress) {
1361 io_channel_flush(fs->io);
1362 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1363 group+1, fs->group_desc_count);
1371 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1373 struct process_block_struct pb;
1374 ext2_ino_t ino, new_inode;
1375 struct ext2_inode *inode = NULL;
1376 ext2_inode_scan scan = NULL;
1378 char *block_buf = 0;
1379 ext2_ino_t start_to_move;
1384 if ((rfs->old_fs->group_desc_count <=
1385 rfs->new_fs->group_desc_count) &&
1390 * Save the original size of the old filesystem, and
1391 * temporarily set the size to be the new size if the new size
1392 * is larger. We need to do this to avoid catching an error
1393 * by the block iterator routines
1395 orig_size = ext2fs_blocks_count(rfs->old_fs->super);
1396 if (orig_size < ext2fs_blocks_count(rfs->new_fs->super))
1397 ext2fs_blocks_count_set(rfs->old_fs->super,
1398 ext2fs_blocks_count(rfs->new_fs->super));
1400 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1401 if (retval) goto errout;
1403 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1404 if (retval) goto errout;
1405 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1406 if (retval) goto errout;
1408 start_to_move = (rfs->new_fs->group_desc_count *
1409 rfs->new_fs->super->s_inodes_per_group);
1411 if (rfs->progress) {
1412 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1413 0, rfs->old_fs->group_desc_count);
1417 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1421 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1422 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1423 inode = malloc(inode_size);
1429 * First, copy all of the inodes that need to be moved
1430 * elsewhere in the inode table
1433 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1434 if (retval) goto errout;
1438 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1439 continue; /* inode not in use */
1441 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1444 if (ext2fs_file_acl_block(rfs->old_fs, inode) && rfs->bmap) {
1445 new_block = extent_translate(rfs->old_fs, rfs->bmap,
1446 ext2fs_file_acl_block(rfs->old_fs, inode));
1448 ext2fs_file_acl_block_set(rfs->old_fs, inode,
1450 retval = ext2fs_write_inode_full(rfs->old_fs,
1451 ino, inode, inode_size);
1452 if (retval) goto errout;
1456 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
1457 (rfs->bmap || pb.is_dir)) {
1459 retval = ext2fs_block_iterate3(rfs->old_fs,
1461 process_block, &pb);
1470 if (ino <= start_to_move)
1471 continue; /* Don't need to move it. */
1476 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1480 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1483 /* Get the new version of the inode */
1484 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1486 if (retval) goto errout;
1488 inode->i_ctime = time(0);
1489 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1491 if (retval) goto errout;
1493 #ifdef RESIZE2FS_DEBUG
1494 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1495 printf("Inode moved %u->%u\n", ino, new_inode);
1498 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1502 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1504 io_channel_flush(rfs->old_fs->io);
1507 ext2fs_blocks_count_set(rfs->old_fs->super, orig_size);
1509 ext2fs_free_extent_table(rfs->bmap);
1513 ext2fs_close_inode_scan(scan);
1515 ext2fs_free_mem(&block_buf);
1520 /* --------------------------------------------------------------------
1522 * Resize processing, phase 4.
1524 * --------------------------------------------------------------------
1530 unsigned int max_dirs;
1534 static int check_and_change_inodes(ext2_ino_t dir,
1535 int entry EXT2FS_ATTR((unused)),
1536 struct ext2_dir_entry *dirent, int offset,
1537 int blocksize EXT2FS_ATTR((unused)),
1538 char *buf EXT2FS_ATTR((unused)),
1541 struct istruct *is = (struct istruct *) priv_data;
1542 struct ext2_inode inode;
1543 ext2_ino_t new_inode;
1546 if (is->rfs->progress && offset == 0) {
1547 io_channel_flush(is->rfs->old_fs->io);
1548 is->err = (is->rfs->progress)(is->rfs,
1549 E2_RSZ_INODE_REF_UPD_PASS,
1550 ++is->num, is->max_dirs);
1552 return DIRENT_ABORT;
1558 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1562 #ifdef RESIZE2FS_DEBUG
1563 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1564 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1565 dir, dirent->name_len&0xFF, dirent->name,
1566 dirent->inode, new_inode);
1569 dirent->inode = new_inode;
1571 /* Update the directory mtime and ctime */
1572 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1574 inode.i_mtime = inode.i_ctime = time(0);
1575 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1577 return DIRENT_ABORT;
1580 return DIRENT_CHANGED;
1583 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1592 * Now, we iterate over all of the directories to update the
1596 is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
1600 if (rfs->progress) {
1601 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1607 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1608 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1609 check_and_change_inodes, &is);
1617 if (rfs->progress && (is.num < is.max_dirs))
1618 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1619 is.max_dirs, is.max_dirs);
1622 ext2fs_free_extent_table(rfs->imap);
1628 /* --------------------------------------------------------------------
1630 * Resize processing, phase 5.
1632 * In this phase we actually move the inode table around, and then
1633 * update the summary statistics. This is scary, since aborting here
1634 * will potentially scramble the filesystem. (We are moving the
1635 * inode tables around in place, and so the potential for lost data,
1636 * or at the very least scrambling the mapping between filenames and
1637 * inode numbers is very high in case of a power failure here.)
1638 * --------------------------------------------------------------------
1643 * A very scary routine --- this one moves the inode table around!!!
1645 * After this you have to use the rfs->new_fs file handle to read and
1648 static errcode_t move_itables(ext2_resize_t rfs)
1652 dgrp_t i, max_groups;
1653 ext2_filsys fs = rfs->new_fs;
1655 blk64_t old_blk, new_blk, blk;
1657 int j, to_move, moved;
1659 max_groups = fs->group_desc_count;
1660 if (max_groups > rfs->old_fs->group_desc_count)
1661 max_groups = rfs->old_fs->group_desc_count;
1663 size = fs->blocksize * fs->inode_blocks_per_group;
1664 if (!rfs->itable_buf) {
1665 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1671 * Figure out how many inode tables we need to move
1673 to_move = moved = 0;
1674 for (i=0; i < max_groups; i++)
1675 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
1676 ext2fs_inode_table_loc(fs, i))
1682 if (rfs->progress) {
1683 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1689 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1691 for (i=0; i < max_groups; i++) {
1692 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
1693 new_blk = ext2fs_inode_table_loc(fs, i);
1694 diff = new_blk - old_blk;
1696 #ifdef RESIZE2FS_DEBUG
1697 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1698 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
1699 i, old_blk, new_blk, diff);
1705 retval = io_channel_read_blk64(fs->io, old_blk,
1706 fs->inode_blocks_per_group,
1711 * The end of the inode table segment often contains
1712 * all zeros, and we're often only moving the inode
1713 * table down a block or two. If so, we can optimize
1714 * things by not rewriting blocks that we know to be zero
1717 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1720 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1721 #ifdef RESIZE2FS_DEBUG
1722 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1723 printf("%d blocks of zeros...\n", n);
1725 num = fs->inode_blocks_per_group;
1729 retval = io_channel_write_blk64(fs->io, new_blk,
1730 num, rfs->itable_buf);
1732 io_channel_write_blk64(fs->io, old_blk,
1733 num, rfs->itable_buf);
1737 retval = io_channel_write_blk64(fs->io,
1738 old_blk + fs->inode_blocks_per_group,
1739 diff, (rfs->itable_buf +
1740 (fs->inode_blocks_per_group - diff) *
1746 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1747 j < fs->inode_blocks_per_group ; j++, blk++)
1748 ext2fs_block_alloc_stats2(fs, blk, -1);
1750 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
1751 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1752 ext2fs_mark_super_dirty(rfs->old_fs);
1753 ext2fs_flush(rfs->old_fs);
1755 if (rfs->progress) {
1756 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1762 mark_table_blocks(fs, fs->block_map);
1764 #ifdef RESIZE2FS_DEBUG
1765 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1766 printf("Inode table move finished.\n");
1775 * Fix the resize inode
1777 static errcode_t fix_resize_inode(ext2_filsys fs)
1779 struct ext2_inode inode;
1781 char *block_buf = NULL;
1783 if (!(fs->super->s_feature_compat &
1784 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1787 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1788 if (retval) goto errout;
1790 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1791 if (retval) goto errout;
1793 ext2fs_iblk_set(fs, &inode, 1);
1795 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1796 if (retval) goto errout;
1798 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1800 * Avoid zeroing out block #0; that's rude. This
1801 * should never happen anyway since the filesystem
1802 * should be fsck'ed and we assume it is consistent.
1805 _("Should never happen: resize inode corrupt!\n"));
1809 memset(block_buf, 0, fs->blocksize);
1811 retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1813 if (retval) goto errout;
1815 retval = ext2fs_create_resize_inode(fs);
1821 ext2fs_free_mem(&block_buf);
1826 * Finally, recalculate the summary information
1828 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1832 unsigned int group = 0;
1833 unsigned int count = 0;
1834 blk64_t total_blocks_free = 0;
1835 int total_inodes_free = 0;
1838 blk64_t super_blk, old_desc_blk, new_desc_blk;
1839 int old_desc_blocks;
1842 * First calculate the block statistics
1844 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1845 ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
1847 if (fs->super->s_feature_incompat & 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;
1852 for (blk = B2C(fs->super->s_first_data_block);
1853 blk < ext2fs_blocks_count(fs->super);
1854 blk += EXT2FS_CLUSTER_RATIO(fs)) {
1856 !(EQ_CLSTR(blk, super_blk) ||
1857 ((old_desc_blk && old_desc_blocks &&
1858 GE_CLSTR(blk, old_desc_blk) &&
1859 LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) ||
1860 ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) ||
1861 EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) ||
1862 EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) ||
1863 ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) &&
1864 LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group)
1865 + fs->inode_blocks_per_group))))) ||
1866 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
1868 total_blocks_free++;
1871 if ((count == fs->super->s_clusters_per_group) ||
1872 EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) {
1873 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
1874 ext2fs_group_desc_csum_set(fs, group);
1876 if (group >= fs->group_desc_count)
1880 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1881 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
1884 if (fs->super->s_feature_incompat &
1885 EXT2_FEATURE_INCOMPAT_META_BG)
1886 old_desc_blocks = fs->super->s_first_meta_bg;
1888 old_desc_blocks = fs->desc_blocks +
1889 fs->super->s_reserved_gdt_blocks;
1892 total_blocks_free = C2B(total_blocks_free);
1893 ext2fs_free_blocks_count_set(fs->super, total_blocks_free);
1896 * Next, calculate the inode statistics
1902 /* Protect loop from wrap-around if s_inodes_count maxed */
1903 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
1904 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1906 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1908 total_inodes_free++;
1911 if ((count == fs->super->s_inodes_per_group) ||
1912 (ino == fs->super->s_inodes_count)) {
1913 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
1914 ext2fs_group_desc_csum_set(fs, group);
1916 if (group >= fs->group_desc_count)
1920 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
1923 fs->super->s_free_inodes_count = total_inodes_free;
1924 ext2fs_mark_super_dirty(fs);
1929 * Journal may have been relocated; update the backup journal blocks
1930 * in the superblock.
1932 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
1935 struct ext2_inode inode;
1937 if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1940 /* External journal? Nothing to do. */
1941 if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
1944 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
1947 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
1948 fs->super->s_jnl_blocks[15] = inode.i_size_high;
1949 fs->super->s_jnl_blocks[16] = inode.i_size;
1950 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
1951 ext2fs_mark_super_dirty(fs);
1955 static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
1956 int old_desc_blocks)
1958 blk64_t super_blk, old_desc_blk, new_desc_blk;
1961 /* inode table blocks plus allocation bitmaps */
1962 overhead = fs->inode_blocks_per_group + 2;
1964 ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
1965 &old_desc_blk, &new_desc_blk, 0);
1966 if ((grp == 0) || super_blk)
1969 overhead += old_desc_blocks;
1970 else if (new_desc_blk)
1977 * calcluate the minimum number of blocks the given fs can be resized to
1979 blk64_t calculate_minimum_resize_size(ext2_filsys fs)
1981 ext2_ino_t inode_count;
1982 blk64_t blks_needed, groups, data_blocks;
1983 blk64_t grp, data_needed, last_start;
1984 blk64_t overhead = 0;
1985 int old_desc_blocks;
1986 int extra_groups = 0;
1987 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1990 * first figure out how many group descriptors we need to
1991 * handle the number of inodes we have
1993 inode_count = fs->super->s_inodes_count -
1994 fs->super->s_free_inodes_count;
1995 blks_needed = ext2fs_div_ceil(inode_count,
1996 fs->super->s_inodes_per_group) *
1997 EXT2_BLOCKS_PER_GROUP(fs->super);
1998 groups = ext2fs_div64_ceil(blks_needed,
1999 EXT2_BLOCKS_PER_GROUP(fs->super));
2002 * number of old-style block group descriptor blocks
2004 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2005 old_desc_blocks = fs->super->s_first_meta_bg;
2007 old_desc_blocks = fs->desc_blocks +
2008 fs->super->s_reserved_gdt_blocks;
2010 /* calculate how many blocks are needed for data */
2011 data_needed = ext2fs_blocks_count(fs->super) -
2012 ext2fs_free_blocks_count(fs->super);
2014 for (grp = 0; grp < fs->group_desc_count; grp++)
2015 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
2018 * For ext4 we need to allow for up to a flex_bg worth of
2019 * inode tables of slack space so the resize operation can be
2020 * guaranteed to finish.
2022 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2023 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
2024 data_needed += fs->inode_blocks_per_group * extra_groups;
2025 extra_groups = groups % flexbg_size;
2029 * figure out how many data blocks we have given the number of groups
2030 * we need for our inodes
2032 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
2034 for (grp = 0; grp < groups; grp++) {
2035 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
2038 * we want to keep track of how much data we can store in
2039 * the groups leading up to the last group so we can determine
2040 * how big the last group needs to be
2042 if (grp != (groups - 1))
2043 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
2046 data_blocks -= overhead;
2050 * if we need more group descriptors in order to accomodate our data
2051 * then we need to add them here
2053 while (data_needed > data_blocks) {
2054 blk64_t remainder = data_needed - data_blocks;
2057 /* figure out how many more groups we need for the data */
2058 extra_grps = ext2fs_div64_ceil(remainder,
2059 EXT2_BLOCKS_PER_GROUP(fs->super));
2061 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
2063 /* ok we have to account for the last group */
2064 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2065 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
2067 for (grp = groups; grp < groups+extra_grps; grp++) {
2068 overhead = calc_group_overhead(fs, grp,
2072 * again, we need to see how much data we cram into
2073 * all of the groups leading up to the last group
2075 if (grp != (groups + extra_grps - 1))
2076 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
2079 data_blocks -= overhead;
2082 groups += extra_grps;
2083 extra_groups += extra_grps;
2084 if (fs->super->s_feature_incompat
2085 & EXT4_FEATURE_INCOMPAT_FLEX_BG
2086 && extra_groups > flexbg_size) {
2088 * For ext4 we need to allow for up to a flex_bg worth
2089 * of inode tables of slack space so the resize
2090 * operation can be guaranteed to finish.
2092 extra_groups = flexbg_size -
2093 (groups & (flexbg_size - 1));
2094 data_needed += (fs->inode_blocks_per_group *
2096 extra_groups = groups % flexbg_size;
2100 /* now for the fun voodoo */
2101 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2104 * if this is the case then the last group is going to have data in it
2105 * so we need to adjust the size of the last group accordingly
2107 if (last_start < data_needed) {
2108 blk64_t remainder = data_needed - last_start;
2111 * 50 is a magic number that mkfs/resize uses to see if its
2112 * even worth making/resizing the fs. basically you need to
2113 * have at least 50 blocks in addition to the blocks needed
2114 * for the metadata in the last group
2117 overhead += remainder;
2123 overhead += fs->super->s_first_data_block;
2126 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2127 * only do groups-1, and then add the number of blocks needed to
2128 * handle the group descriptor metadata+data that we need
2130 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2131 blks_needed += overhead;
2134 * If at this point we've already added up more "needed" than
2135 * the current size, just return current size as minimum.
2137 if (blks_needed >= ext2fs_blocks_count(fs->super))
2138 return ext2fs_blocks_count(fs->super);
2140 * We need to reserve a few extra blocks if extents are
2141 * enabled, in case we need to grow the extent tree. The more
2142 * we shrink the file system, the more space we need.
2144 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
2145 blks_needed += (ext2fs_blocks_count(fs->super) -