2 * resize2fs.c --- ext2 main routine
4 * Copyright (C) 1997, 1998 by Theodore Ts'o and
7 * Copyright (C) 1999, 2000 by Theosore Ts'o
10 * This file may be redistributed under the terms of the GNU Public
16 * Resizing a filesystem consists of the following phases:
18 * 1. Adjust superblock and write out new parts of the inode
20 * 2. Determine blocks which need to be relocated, and copy the
21 * contents of blocks from their old locations to the new ones.
22 * 3. Scan the inode table, doing the following:
23 * a. If blocks have been moved, update the block
24 * pointers in the inodes and indirect blocks to
25 * point at the new block locations.
26 * b. If parts of the inode table need to be evacuated,
27 * copy inodes from their old locations to their
29 * c. If (b) needs to be done, note which blocks contain
30 * directory information, since we will need to
31 * update the directory information.
32 * 4. Update the directory blocks with the new inode locations.
33 * 5. Move the inode tables, if necessary.
37 #include "resize2fs.h"
40 #ifdef __linux__ /* Kludge for debugging */
41 #define RESIZE2FS_DEBUG
44 static void fix_uninit_block_bitmaps(ext2_filsys fs);
45 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size);
46 static errcode_t blocks_to_move(ext2_resize_t rfs);
47 static errcode_t block_mover(ext2_resize_t rfs);
48 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
49 static errcode_t inode_ref_fix(ext2_resize_t rfs);
50 static errcode_t move_itables(ext2_resize_t rfs);
51 static errcode_t fix_resize_inode(ext2_filsys fs);
52 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
53 static errcode_t fix_sb_journal_backup(ext2_filsys fs);
54 static errcode_t mark_table_blocks(ext2_filsys fs,
55 ext2fs_block_bitmap bmap);
58 * Some helper CPP macros
60 #define IS_BLOCK_BM(fs, i, blk) ((blk) == ext2fs_block_bitmap_loc((fs),(i)))
61 #define IS_INODE_BM(fs, i, blk) ((blk) == ext2fs_inode_bitmap_loc((fs),(i)))
63 #define IS_INODE_TB(fs, i, blk) (((blk) >= ext2fs_inode_table_loc((fs), (i))) && \
64 ((blk) < (ext2fs_inode_table_loc((fs), (i)) + \
65 (fs)->inode_blocks_per_group)))
67 /* Some bigalloc helper macros which are more succint... */
68 #define B2C(x) EXT2FS_B2C(fs, (x))
69 #define C2B(x) EXT2FS_C2B(fs, (x))
70 #define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
71 #define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
72 #define LT_CLSTR(x, y) (B2C(x) < B2C(y))
73 #define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
74 #define GT_CLSTR(x, y) (B2C(x) > B2C(y))
79 * This is the top-level routine which does the dirty deed....
81 errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
82 errcode_t (*progress)(ext2_resize_t rfs, int pass,
84 unsigned long max_val))
88 struct resource_track rtrack, overall_track;
91 * Create the data structure
93 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
97 memset(rfs, 0, sizeof(struct ext2_resize_struct));
102 rfs->progress = progress;
104 init_resource_track(&overall_track, "overall resize2fs", fs->io);
105 init_resource_track(&rtrack, "read_bitmaps", fs->io);
106 retval = ext2fs_read_bitmaps(fs);
109 print_resource_track(rfs, &rtrack, fs->io);
111 fs->super->s_state |= EXT2_ERROR_FS;
112 ext2fs_mark_super_dirty(fs);
115 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io);
116 fix_uninit_block_bitmaps(fs);
117 print_resource_track(rfs, &rtrack, fs->io);
118 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
122 init_resource_track(&rtrack, "adjust_superblock", fs->io);
123 retval = adjust_superblock(rfs, *new_size);
126 print_resource_track(rfs, &rtrack, fs->io);
129 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io);
130 fix_uninit_block_bitmaps(rfs->new_fs);
131 print_resource_track(rfs, &rtrack, fs->io);
132 /* Clear the block bitmap uninit flag for the last block group */
133 ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
134 EXT2_BG_BLOCK_UNINIT);
136 *new_size = ext2fs_blocks_count(rfs->new_fs->super);
138 init_resource_track(&rtrack, "blocks_to_move", fs->io);
139 retval = blocks_to_move(rfs);
142 print_resource_track(rfs, &rtrack, fs->io);
144 #ifdef RESIZE2FS_DEBUG
145 if (rfs->flags & RESIZE_DEBUG_BMOVE)
146 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
147 ext2fs_free_blocks_count(rfs->old_fs->super),
148 ext2fs_free_blocks_count(rfs->new_fs->super),
152 init_resource_track(&rtrack, "block_mover", fs->io);
153 retval = block_mover(rfs);
156 print_resource_track(rfs, &rtrack, fs->io);
158 init_resource_track(&rtrack, "inode_scan_and_fix", fs->io);
159 retval = inode_scan_and_fix(rfs);
162 print_resource_track(rfs, &rtrack, fs->io);
164 init_resource_track(&rtrack, "inode_ref_fix", fs->io);
165 retval = inode_ref_fix(rfs);
168 print_resource_track(rfs, &rtrack, fs->io);
170 init_resource_track(&rtrack, "move_itables", fs->io);
171 retval = move_itables(rfs);
174 print_resource_track(rfs, &rtrack, fs->io);
176 init_resource_track(&rtrack, "calculate_summary_stats", fs->io);
177 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
180 print_resource_track(rfs, &rtrack, fs->io);
182 init_resource_track(&rtrack, "fix_resize_inode", fs->io);
183 retval = fix_resize_inode(rfs->new_fs);
186 print_resource_track(rfs, &rtrack, fs->io);
188 init_resource_track(&rtrack, "fix_sb_journal_backup", fs->io);
189 retval = fix_sb_journal_backup(rfs->new_fs);
192 print_resource_track(rfs, &rtrack, fs->io);
194 rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
195 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
197 print_resource_track(rfs, &overall_track, fs->io);
198 retval = ext2fs_close(rfs->new_fs);
204 ext2fs_free(rfs->old_fs);
206 ext2fs_free_mem(&rfs->itable_buf);
207 if (rfs->reserve_blocks)
208 ext2fs_free_block_bitmap(rfs->reserve_blocks);
209 if (rfs->move_blocks)
210 ext2fs_free_block_bitmap(rfs->move_blocks);
211 ext2fs_free_mem(&rfs);
217 ext2fs_free(rfs->new_fs);
219 ext2fs_free_mem(&rfs->itable_buf);
220 ext2fs_free_mem(&rfs);
225 * Clean up the bitmaps for unitialized bitmaps
227 static void fix_uninit_block_bitmaps(ext2_filsys fs)
233 if (!ext2fs_has_group_desc_csum(fs))
236 for (g=0; g < fs->group_desc_count; g++) {
237 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
240 blk = ext2fs_group_first_block2(fs, g);
241 lblk = ext2fs_group_last_block2(fs, g);
242 ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
245 ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
246 ext2fs_mark_block_bitmap2(fs->block_map,
247 ext2fs_block_bitmap_loc(fs, g));
248 ext2fs_mark_block_bitmap2(fs->block_map,
249 ext2fs_inode_bitmap_loc(fs, g));
250 for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
251 i < (unsigned int) fs->inode_blocks_per_group;
253 ext2fs_mark_block_bitmap2(fs->block_map, blk);
257 /* --------------------------------------------------------------------
259 * Resize processing, phase 1.
261 * In this phase we adjust the in-memory superblock information, and
262 * initialize any new parts of the inode table. The new parts of the
263 * inode table are created in virgin disk space, so we can abort here
264 * without any side effects.
265 * --------------------------------------------------------------------
269 * If the group descriptor's bitmap and inode table blocks are valid,
270 * release them in the new filesystem data structure, and mark them as
271 * reserved so the old inode table blocks don't get overwritten.
273 static void free_gdp_blocks(ext2_filsys fs,
274 ext2fs_block_bitmap reserve_blocks,
275 struct ext2_group_desc *gdp)
280 if (gdp->bg_block_bitmap &&
281 (gdp->bg_block_bitmap < ext2fs_blocks_count(fs->super))) {
282 ext2fs_block_alloc_stats(fs, gdp->bg_block_bitmap, -1);
283 ext2fs_mark_block_bitmap2(reserve_blocks,
284 gdp->bg_block_bitmap);
287 if (gdp->bg_inode_bitmap &&
288 (gdp->bg_inode_bitmap < ext2fs_blocks_count(fs->super))) {
289 ext2fs_block_alloc_stats(fs, gdp->bg_inode_bitmap, -1);
290 ext2fs_mark_block_bitmap2(reserve_blocks,
291 gdp->bg_inode_bitmap);
294 if (gdp->bg_inode_table == 0 ||
295 (gdp->bg_inode_table >= ext2fs_blocks_count(fs->super)))
298 for (blk = gdp->bg_inode_table, j = 0;
299 j < fs->inode_blocks_per_group; j++, blk++) {
300 if (blk >= ext2fs_blocks_count(fs->super))
302 ext2fs_block_alloc_stats(fs, blk, -1);
303 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
308 * This routine is shared by the online and offline resize routines.
309 * All of the information which is adjusted in memory is done here.
311 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
312 ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
315 blk64_t overhead = 0;
317 blk64_t blk, group_block;
319 blk64_t old_numblocks, numblocks, adjblocks;
320 unsigned long i, j, old_desc_blocks;
321 unsigned int meta_bg, meta_bg_size;
322 int has_super, csum_flag;
323 unsigned long long new_inodes; /* u64 to check for overflow */
326 ext2fs_blocks_count_set(fs->super, new_size);
329 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
330 fs->super->s_first_data_block,
331 EXT2_BLOCKS_PER_GROUP(fs->super));
332 if (fs->group_desc_count == 0)
333 return EXT2_ET_TOOSMALL;
334 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
335 EXT2_DESC_PER_BLOCK(fs->super));
338 * Overhead is the number of bookkeeping blocks per group. It
339 * includes the superblock backup, the group descriptor
340 * backups, the inode bitmap, the block bitmap, and the inode
343 overhead = (int) (2 + fs->inode_blocks_per_group);
345 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
346 overhead += 1 + fs->desc_blocks +
347 fs->super->s_reserved_gdt_blocks;
350 * See if the last group is big enough to support the
351 * necessary data structures. If not, we need to get rid of
354 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
355 fs->super->s_blocks_per_group;
356 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
357 return EXT2_ET_TOOSMALL;
358 if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
359 ext2fs_blocks_count_set(fs->super,
360 ext2fs_blocks_count(fs->super) - rem);
364 * Adjust the number of inodes
366 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
367 if (new_inodes > ~0U) {
368 fprintf(stderr, _("inodes (%llu) must be less than %u"),
370 return EXT2_ET_TOO_MANY_INODES;
372 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
373 fs->group_desc_count;
376 * Adjust the number of free blocks
378 blk = ext2fs_blocks_count(old_fs->super);
379 if (blk > ext2fs_blocks_count(fs->super))
380 ext2fs_free_blocks_count_set(fs->super,
381 ext2fs_free_blocks_count(fs->super) -
382 (blk - ext2fs_blocks_count(fs->super)));
384 ext2fs_free_blocks_count_set(fs->super,
385 ext2fs_free_blocks_count(fs->super) +
386 (ext2fs_blocks_count(fs->super) - blk));
389 * Adjust the number of reserved blocks
391 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
392 ext2fs_blocks_count(old_fs->super);
393 ext2fs_r_blocks_count_set(fs->super,
394 (percent * ext2fs_blocks_count(fs->super) /
398 * Adjust the bitmaps for size
400 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
401 fs->super->s_inodes_count,
403 if (retval) goto errout;
405 real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) *
406 fs->group_desc_count)) - 1 +
407 fs->super->s_first_data_block;
408 retval = ext2fs_resize_block_bitmap2(new_size - 1,
409 real_end, fs->block_map);
410 if (retval) goto errout;
413 * If we are growing the file system, also grow the size of
414 * the reserve_blocks bitmap
416 if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) {
417 retval = ext2fs_resize_block_bitmap2(new_size - 1,
418 real_end, reserve_blocks);
419 if (retval) goto errout;
423 * Reallocate the group descriptors as necessary.
425 if (old_fs->desc_blocks != fs->desc_blocks) {
426 retval = ext2fs_resize_mem(old_fs->desc_blocks *
428 fs->desc_blocks * fs->blocksize,
432 if (fs->desc_blocks > old_fs->desc_blocks)
433 memset((char *) fs->group_desc +
434 (old_fs->desc_blocks * fs->blocksize), 0,
435 (fs->desc_blocks - old_fs->desc_blocks) *
440 * If the resize_inode feature is set, and we are changing the
441 * number of descriptor blocks, then adjust
442 * s_reserved_gdt_blocks if possible to avoid needing to move
443 * the inode table either now or in the future.
445 if ((fs->super->s_feature_compat &
446 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
447 (old_fs->desc_blocks != fs->desc_blocks)) {
450 new = ((int) fs->super->s_reserved_gdt_blocks) +
451 (old_fs->desc_blocks - fs->desc_blocks);
454 if (new > (int) fs->blocksize/4)
455 new = fs->blocksize/4;
456 fs->super->s_reserved_gdt_blocks = new;
460 * If we are shrinking the number of block groups, we're done
463 if (old_fs->group_desc_count > fs->group_desc_count) {
465 * Check the block groups that we are chopping off
466 * and free any blocks associated with their metadata
468 for (i = fs->group_desc_count;
469 i < old_fs->group_desc_count; i++) {
470 free_gdp_blocks(fs, reserve_blocks,
471 ext2fs_group_desc(old_fs,
472 old_fs->group_desc, i));
479 * Fix the count of the last (old) block group
481 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
482 old_fs->super->s_first_data_block) %
483 old_fs->super->s_blocks_per_group;
485 old_numblocks = old_fs->super->s_blocks_per_group;
486 if (old_fs->group_desc_count == fs->group_desc_count) {
487 numblocks = (ext2fs_blocks_count(fs->super) -
488 fs->super->s_first_data_block) %
489 fs->super->s_blocks_per_group;
491 numblocks = fs->super->s_blocks_per_group;
493 numblocks = fs->super->s_blocks_per_group;
494 i = old_fs->group_desc_count - 1;
495 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
496 ext2fs_group_desc_csum_set(fs, i);
499 * If the number of block groups is staying the same, we're
500 * done and can exit now. (If the number block groups is
501 * shrinking, we had exited earlier.)
503 if (old_fs->group_desc_count >= fs->group_desc_count) {
509 * Initialize the new block group descriptors
511 group_block = ext2fs_group_first_block2(fs,
512 old_fs->group_desc_count);
513 csum_flag = ext2fs_has_group_desc_csum(fs);
514 if (access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0)
515 lazy_itable_init = 1;
516 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
517 old_desc_blocks = fs->super->s_first_meta_bg;
519 old_desc_blocks = fs->desc_blocks +
520 fs->super->s_reserved_gdt_blocks;
523 * If we changed the number of block_group descriptor blocks,
524 * we need to make sure they are all marked as reserved in the
525 * file systems's block allocation map.
527 for (i = 0; i < old_fs->group_desc_count; i++)
528 ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
530 for (i = old_fs->group_desc_count;
531 i < fs->group_desc_count; i++) {
532 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
533 sizeof(struct ext2_group_desc));
536 ext2fs_bg_flags_zap(fs, i);
538 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
539 if (!lazy_itable_init)
540 ext2fs_bg_flags_set(fs, i,
541 EXT2_BG_INODE_ZEROED);
542 ext2fs_bg_itable_unused_set(fs, i,
543 fs->super->s_inodes_per_group);
546 numblocks = ext2fs_group_blocks_count(fs, i);
547 if ((i < fs->group_desc_count - 1) && csum_flag)
548 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
550 has_super = ext2fs_bg_has_super(fs, i);
552 ext2fs_block_alloc_stats2(fs, group_block, +1);
555 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
556 meta_bg = i / meta_bg_size;
557 if (!(fs->super->s_feature_incompat &
558 EXT2_FEATURE_INCOMPAT_META_BG) ||
559 (meta_bg < fs->super->s_first_meta_bg)) {
561 for (j=0; j < old_desc_blocks; j++)
562 ext2fs_block_alloc_stats2(fs,
563 group_block + 1 + j, +1);
564 adjblocks += old_desc_blocks;
569 if (((i % meta_bg_size) == 0) ||
570 ((i % meta_bg_size) == 1) ||
571 ((i % meta_bg_size) == (meta_bg_size-1)))
572 ext2fs_block_alloc_stats2(fs,
573 group_block + has_super, +1);
576 adjblocks += 2 + fs->inode_blocks_per_group;
578 numblocks -= adjblocks;
579 ext2fs_free_blocks_count_set(fs->super,
580 ext2fs_free_blocks_count(fs->super) - adjblocks);
581 fs->super->s_free_inodes_count +=
582 fs->super->s_inodes_per_group;
583 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
584 ext2fs_bg_free_inodes_count_set(fs, i,
585 fs->super->s_inodes_per_group);
586 ext2fs_bg_used_dirs_count_set(fs, i, 0);
587 ext2fs_group_desc_csum_set(fs, i);
589 retval = ext2fs_allocate_group_table(fs, i, 0);
590 if (retval) goto errout;
592 group_block += fs->super->s_blocks_per_group;
597 * Mark all of the metadata blocks as reserved so they won't
598 * get allocated by the call to ext2fs_allocate_group_table()
599 * in blocks_to_move(), where we allocate new blocks to
600 * replace those allocation bitmap and inode table blocks
601 * which have to get relocated to make space for an increased
602 * number of the block group descriptors.
605 mark_table_blocks(fs, reserve_blocks);
612 * This routine adjusts the superblock and other data structures, both
613 * in disk as well as in memory...
615 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
622 unsigned long max_group;
625 ext2fs_mark_super_dirty(fs);
626 ext2fs_mark_bb_dirty(fs);
627 ext2fs_mark_ib_dirty(fs);
629 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
630 &rfs->reserve_blocks);
634 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
639 * Check to make sure there are enough inodes
641 if ((rfs->old_fs->super->s_inodes_count -
642 rfs->old_fs->super->s_free_inodes_count) >
643 rfs->new_fs->super->s_inodes_count) {
649 * If we are shrinking the number block groups, we're done and
652 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
658 * If the number of block groups is staying the same, we're
659 * done and can exit now. (If the number block groups is
660 * shrinking, we had exited earlier.)
662 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
668 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
669 * supports lazy inode initialization, we can skip
670 * initializing the inode table.
672 if (lazy_itable_init &&
673 EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
674 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
680 * Initialize the inode table
682 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
687 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
688 group_block = ext2fs_group_first_block2(fs,
689 rfs->old_fs->group_desc_count);
690 adj = rfs->old_fs->group_desc_count;
691 max_group = fs->group_desc_count - adj;
693 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
698 for (i = rfs->old_fs->group_desc_count;
699 i < fs->group_desc_count; i++) {
701 * Write out the new inode table
703 retval = io_channel_write_blk64(fs->io,
704 ext2fs_inode_table_loc(fs, i),
705 fs->inode_blocks_per_group,
707 if (retval) goto errout;
709 io_channel_flush(fs->io);
711 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
712 i - adj + 1, max_group);
716 group_block += fs->super->s_blocks_per_group;
718 io_channel_flush(fs->io);
725 /* --------------------------------------------------------------------
727 * Resize processing, phase 2.
729 * In this phase we adjust determine which blocks need to be moved, in
730 * blocks_to_move(). We then copy the blocks to their ultimate new
731 * destinations using block_mover(). Since we are copying blocks to
732 * their new locations, again during this pass we can abort without
734 * --------------------------------------------------------------------
738 * This helper function creates a block bitmap with all of the
739 * filesystem meta-data blocks.
741 static errcode_t mark_table_blocks(ext2_filsys fs,
742 ext2fs_block_bitmap bmap)
747 for (i = 0; i < fs->group_desc_count; i++) {
748 ext2fs_reserve_super_and_bgd(fs, i, bmap);
751 * Mark the blocks used for the inode table
753 blk = ext2fs_inode_table_loc(fs, i);
755 ext2fs_mark_block_bitmap_range2(bmap, blk,
756 fs->inode_blocks_per_group);
759 * Mark block used for the block bitmap
761 blk = ext2fs_block_bitmap_loc(fs, i);
763 ext2fs_mark_block_bitmap2(bmap, blk);
766 * Mark block used for the inode bitmap
768 blk = ext2fs_inode_bitmap_loc(fs, i);
770 ext2fs_mark_block_bitmap2(bmap, blk);
776 * This function checks to see if a particular block (either a
777 * superblock or a block group descriptor) overlaps with an inode or
778 * block bitmap block, or with the inode table.
780 static void mark_fs_metablock(ext2_resize_t rfs,
781 ext2fs_block_bitmap meta_bmap,
782 int group, blk64_t blk)
784 ext2_filsys fs = rfs->new_fs;
786 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
787 ext2fs_block_alloc_stats2(fs, blk, +1);
790 * Check to see if we overlap with the inode or block bitmap,
791 * or the inode tables. If not, and the block is in use, then
792 * mark it as a block to be moved.
794 if (IS_BLOCK_BM(fs, group, blk)) {
795 ext2fs_block_bitmap_loc_set(fs, group, 0);
796 rfs->needed_blocks++;
799 if (IS_INODE_BM(fs, group, blk)) {
800 ext2fs_inode_bitmap_loc_set(fs, group, 0);
801 rfs->needed_blocks++;
804 if (IS_INODE_TB(fs, group, blk)) {
805 ext2fs_inode_table_loc_set(fs, group, 0);
806 rfs->needed_blocks++;
809 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
812 for (i=0; i < rfs->old_fs->group_desc_count; i++) {
813 if (IS_BLOCK_BM(fs, i, blk)) {
814 ext2fs_block_bitmap_loc_set(fs, i, 0);
815 rfs->needed_blocks++;
818 if (IS_INODE_BM(fs, i, blk)) {
819 ext2fs_inode_bitmap_loc_set(fs, i, 0);
820 rfs->needed_blocks++;
823 if (IS_INODE_TB(fs, i, blk)) {
824 ext2fs_inode_table_loc_set(fs, i, 0);
825 rfs->needed_blocks++;
831 if (ext2fs_has_group_desc_csum(fs) &&
832 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
834 * If the block bitmap is uninitialized, which means
835 * nothing other than standard metadata in use.
838 } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
839 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
840 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
841 rfs->needed_blocks++;
847 * This routine marks and unmarks reserved blocks in the new block
848 * bitmap. It also determines which blocks need to be moved and
849 * places this information into the move_blocks bitmap.
851 static errcode_t blocks_to_move(ext2_resize_t rfs)
854 dgrp_t i, max_groups, g;
855 blk64_t blk, group_blk;
856 blk64_t old_blocks, new_blocks;
857 unsigned int meta_bg, meta_bg_size;
859 ext2_filsys fs, old_fs;
860 ext2fs_block_bitmap meta_bmap;
864 old_fs = rfs->old_fs;
865 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
868 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
873 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
878 retval = mark_table_blocks(old_fs, meta_bmap);
885 * If we're shrinking the filesystem, we need to move all of
886 * the blocks that don't fit any more
888 for (blk = ext2fs_blocks_count(fs->super);
889 blk < ext2fs_blocks_count(old_fs->super); blk++) {
890 g = ext2fs_group_of_blk2(fs, blk);
891 if (ext2fs_has_group_desc_csum(fs) &&
892 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
894 * The block bitmap is uninitialized, so skip
895 * to the next block group.
897 blk = ext2fs_group_first_block2(fs, g+1) - 1;
900 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
901 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
902 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
903 rfs->needed_blocks++;
905 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
908 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
909 old_blocks = old_fs->super->s_first_meta_bg;
910 new_blocks = fs->super->s_first_meta_bg;
912 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
913 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
916 if (old_blocks == new_blocks) {
921 max_groups = fs->group_desc_count;
922 if (max_groups > old_fs->group_desc_count)
923 max_groups = old_fs->group_desc_count;
924 group_blk = old_fs->super->s_first_data_block;
926 * If we're reducing the number of descriptor blocks, this
927 * makes life easy. :-) We just have to mark some extra
930 if (old_blocks > new_blocks) {
931 for (i = 0; i < max_groups; i++) {
932 if (!ext2fs_bg_has_super(fs, i)) {
933 group_blk += fs->super->s_blocks_per_group;
936 for (blk = group_blk+1+new_blocks;
937 blk < group_blk+1+old_blocks; blk++) {
938 ext2fs_block_alloc_stats2(fs, blk, -1);
939 rfs->needed_blocks--;
941 group_blk += fs->super->s_blocks_per_group;
947 * If we're increasing the number of descriptor blocks, life
948 * gets interesting....
950 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
951 flex_bg = fs->super->s_feature_incompat &
952 EXT4_FEATURE_INCOMPAT_FLEX_BG;
953 /* first reserve all of the existing fs meta blocks */
954 for (i = 0; i < max_groups; i++) {
955 has_super = ext2fs_bg_has_super(fs, i);
957 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
959 meta_bg = i / meta_bg_size;
960 if (!(fs->super->s_feature_incompat &
961 EXT2_FEATURE_INCOMPAT_META_BG) ||
962 (meta_bg < fs->super->s_first_meta_bg)) {
964 for (blk = group_blk+1;
965 blk < group_blk + 1 + new_blocks; blk++)
966 mark_fs_metablock(rfs, meta_bmap,
972 if (((i % meta_bg_size) == 0) ||
973 ((i % meta_bg_size) == 1) ||
974 ((i % meta_bg_size) == (meta_bg_size-1)))
975 mark_fs_metablock(rfs, meta_bmap, i,
976 group_blk + has_super);
980 * Reserve the existing meta blocks that we know
981 * aren't to be moved.
983 * For flex_bg file systems, in order to avoid
984 * overwriting fs metadata (especially inode table
985 * blocks) belonging to a different block group when
986 * we are relocating the inode tables, we need to
987 * reserve all existing fs metadata blocks.
989 if (ext2fs_block_bitmap_loc(fs, i))
990 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
991 ext2fs_block_bitmap_loc(fs, i));
992 else if (flex_bg && i < old_fs->group_desc_count)
993 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
994 ext2fs_block_bitmap_loc(old_fs, i));
996 if (ext2fs_inode_bitmap_loc(fs, i))
997 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
998 ext2fs_inode_bitmap_loc(fs, i));
999 else if (flex_bg && i < old_fs->group_desc_count)
1000 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1001 ext2fs_inode_bitmap_loc(old_fs, i));
1003 if (ext2fs_inode_table_loc(fs, i))
1004 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1005 ext2fs_inode_table_loc(fs, i),
1006 fs->inode_blocks_per_group);
1007 else if (flex_bg && i < old_fs->group_desc_count)
1008 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1009 ext2fs_inode_table_loc(old_fs, i),
1010 old_fs->inode_blocks_per_group);
1012 group_blk += rfs->new_fs->super->s_blocks_per_group;
1015 /* Allocate the missing data structures */
1016 for (i = 0; i < max_groups; i++) {
1017 if (ext2fs_inode_table_loc(fs, i) &&
1018 ext2fs_inode_bitmap_loc(fs, i) &&
1019 ext2fs_block_bitmap_loc(fs, i))
1022 retval = ext2fs_allocate_group_table(fs, i,
1023 rfs->reserve_blocks);
1028 * For those structures that have changed, we need to
1031 if (ext2fs_block_bitmap_loc(old_fs, i) !=
1032 (blk = ext2fs_block_bitmap_loc(fs, i))) {
1033 ext2fs_block_alloc_stats2(fs, blk, +1);
1034 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1035 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1036 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1039 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
1040 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
1041 ext2fs_block_alloc_stats2(fs, blk, +1);
1042 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1043 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1044 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1049 * The inode table, if we need to relocate it, is
1050 * handled specially. We have to reserve the blocks
1051 * for both the old and the new inode table, since we
1052 * can't have the inode table be destroyed during the
1053 * block relocation phase.
1055 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
1056 continue; /* inode table not moved */
1058 rfs->needed_blocks += fs->inode_blocks_per_group;
1061 * Mark the new inode table as in use in the new block
1062 * allocation bitmap, and move any blocks that might
1065 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
1066 j < fs->inode_blocks_per_group ; j++, blk++) {
1067 ext2fs_block_alloc_stats2(fs, blk, +1);
1068 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1069 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1070 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1075 * Make sure the old inode table is reserved in the
1076 * block reservation bitmap.
1078 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1079 j < fs->inode_blocks_per_group ; j++, blk++)
1080 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1086 ext2fs_free_block_bitmap(meta_bmap);
1092 * This helper function tries to allocate a new block. We try to
1093 * avoid hitting the original group descriptor blocks at least at
1094 * first, since we want to make it possible to recover from a badly
1095 * aborted resize operation as much as possible.
1097 * In the future, I may further modify this routine to balance out
1098 * where we get the new blocks across the various block groups.
1099 * Ideally we would allocate blocks that corresponded with the block
1100 * group of the containing inode, and keep contiguous blocks
1101 * together. However, this very difficult to do efficiently, since we
1102 * don't have the necessary information up front.
1106 #define DESPERATION 2
1108 static void init_block_alloc(ext2_resize_t rfs)
1110 rfs->alloc_state = AVOID_OLD;
1111 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1113 /* HACK for testing */
1114 if (ext2fs_blocks_count(rfs->new_fs->super) >
1115 ext2fs_blocks_count(rfs->old_fs->super))
1116 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1120 static blk64_t get_new_block(ext2_resize_t rfs)
1122 ext2_filsys fs = rfs->new_fs;
1125 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1126 if (rfs->alloc_state == DESPERATION)
1129 #ifdef RESIZE2FS_DEBUG
1130 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1131 printf("Going into desperation mode "
1132 "for block allocations\n");
1134 rfs->alloc_state = DESPERATION;
1135 rfs->new_blk = fs->super->s_first_data_block;
1138 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1139 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1141 ((rfs->alloc_state == AVOID_OLD) &&
1142 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1143 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1148 return rfs->new_blk;
1152 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1155 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1158 blk = get_new_block(rfs);
1162 #ifdef RESIZE2FS_DEBUG
1163 if (rfs->flags & 0xF)
1164 printf("get_alloc_block allocating %llu\n", blk);
1167 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1168 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1169 *ret = (blk64_t) blk;
1173 static errcode_t block_mover(ext2_resize_t rfs)
1175 blk64_t blk, old_blk, new_blk;
1176 ext2_filsys fs = rfs->new_fs;
1177 ext2_filsys old_fs = rfs->old_fs;
1182 ext2_badblocks_list badblock_list = 0;
1183 int bb_modified = 0;
1185 fs->get_alloc_block = resize2fs_get_alloc_block;
1186 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1188 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1192 new_blk = fs->super->s_first_data_block;
1193 if (!rfs->itable_buf) {
1194 retval = ext2fs_get_array(fs->blocksize,
1195 fs->inode_blocks_per_group,
1200 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1205 * The first step is to figure out where all of the blocks
1208 to_move = moved = 0;
1209 init_block_alloc(rfs);
1210 for (blk = B2C(old_fs->super->s_first_data_block);
1211 blk < ext2fs_blocks_count(old_fs->super);
1212 blk += EXT2FS_CLUSTER_RATIO(fs)) {
1213 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1215 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1217 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1218 ext2fs_badblocks_list_del(badblock_list, blk);
1223 new_blk = get_new_block(rfs);
1228 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1229 ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk));
1235 ext2fs_free_extent_table(rfs->bmap);
1243 * Step two is to actually move the blocks
1245 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1246 if (retval) goto errout;
1248 if (rfs->progress) {
1249 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1255 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1256 if (retval) goto errout;
1259 old_blk = C2B(old_blk);
1260 new_blk = C2B(new_blk);
1262 #ifdef RESIZE2FS_DEBUG
1263 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1264 printf("Moving %llu blocks %llu->%llu\n",
1265 size, old_blk, new_blk);
1269 if (c > fs->inode_blocks_per_group)
1270 c = fs->inode_blocks_per_group;
1271 retval = io_channel_read_blk64(fs->io, old_blk, c,
1273 if (retval) goto errout;
1274 retval = io_channel_write_blk64(fs->io, new_blk, c,
1276 if (retval) goto errout;
1281 if (rfs->progress) {
1282 io_channel_flush(fs->io);
1283 retval = (rfs->progress)(rfs,
1284 E2_RSZ_BLOCK_RELOC_PASS,
1290 io_channel_flush(fs->io);
1294 if (badblock_list) {
1295 if (!retval && bb_modified)
1296 retval = ext2fs_update_bb_inode(old_fs,
1298 ext2fs_badblocks_list_free(badblock_list);
1304 /* --------------------------------------------------------------------
1306 * Resize processing, phase 3
1308 * --------------------------------------------------------------------
1313 * The extent translation table is stored in clusters so we need to
1314 * take special care when mapping a source block number to its
1315 * destination block number.
1317 __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc)
1319 __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc)));
1322 new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1);
1326 struct process_block_struct {
1329 struct ext2_inode * inode;
1335 static int process_block(ext2_filsys fs, blk64_t *block_nr,
1336 e2_blkcnt_t blockcnt,
1337 blk64_t ref_block EXT2FS_ATTR((unused)),
1338 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1340 struct process_block_struct *pb;
1342 blk64_t block, new_block;
1345 pb = (struct process_block_struct *) priv_data;
1347 if (pb->rfs->bmap) {
1348 new_block = extent_translate(fs, pb->rfs->bmap, block);
1350 *block_nr = new_block;
1351 ret |= BLOCK_CHANGED;
1353 #ifdef RESIZE2FS_DEBUG
1354 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1355 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1356 pb->ino, blockcnt, block, new_block);
1362 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1363 block, (int) blockcnt);
1375 static errcode_t progress_callback(ext2_filsys fs,
1376 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1377 dgrp_t group, void * priv_data)
1379 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1383 * This check is to protect against old ext2 libraries. It
1384 * shouldn't be needed against new libraries.
1389 if (rfs->progress) {
1390 io_channel_flush(fs->io);
1391 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1392 group+1, fs->group_desc_count);
1400 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1402 struct process_block_struct pb;
1403 ext2_ino_t ino, new_inode;
1404 struct ext2_inode *inode = NULL;
1405 ext2_inode_scan scan = NULL;
1407 char *block_buf = 0;
1408 ext2_ino_t start_to_move;
1413 if ((rfs->old_fs->group_desc_count <=
1414 rfs->new_fs->group_desc_count) &&
1419 * Save the original size of the old filesystem, and
1420 * temporarily set the size to be the new size if the new size
1421 * is larger. We need to do this to avoid catching an error
1422 * by the block iterator routines
1424 orig_size = ext2fs_blocks_count(rfs->old_fs->super);
1425 if (orig_size < ext2fs_blocks_count(rfs->new_fs->super))
1426 ext2fs_blocks_count_set(rfs->old_fs->super,
1427 ext2fs_blocks_count(rfs->new_fs->super));
1429 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1430 if (retval) goto errout;
1432 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1433 if (retval) goto errout;
1434 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1435 if (retval) goto errout;
1437 start_to_move = (rfs->new_fs->group_desc_count *
1438 rfs->new_fs->super->s_inodes_per_group);
1440 if (rfs->progress) {
1441 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1442 0, rfs->old_fs->group_desc_count);
1446 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1450 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1451 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1452 inode = malloc(inode_size);
1458 * First, copy all of the inodes that need to be moved
1459 * elsewhere in the inode table
1462 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1463 if (retval) goto errout;
1467 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1468 continue; /* inode not in use */
1470 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1473 if (ext2fs_file_acl_block(rfs->old_fs, inode) && rfs->bmap) {
1474 new_block = extent_translate(rfs->old_fs, rfs->bmap,
1475 ext2fs_file_acl_block(rfs->old_fs, inode));
1477 ext2fs_file_acl_block_set(rfs->old_fs, inode,
1479 retval = ext2fs_write_inode_full(rfs->old_fs,
1480 ino, inode, inode_size);
1481 if (retval) goto errout;
1485 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
1486 (rfs->bmap || pb.is_dir)) {
1488 retval = ext2fs_block_iterate3(rfs->old_fs,
1490 process_block, &pb);
1499 if (ino <= start_to_move)
1500 continue; /* Don't need to move it. */
1505 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1509 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1512 /* Get the new version of the inode */
1513 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1515 if (retval) goto errout;
1517 inode->i_ctime = time(0);
1518 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1520 if (retval) goto errout;
1522 #ifdef RESIZE2FS_DEBUG
1523 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1524 printf("Inode moved %u->%u\n", ino, new_inode);
1527 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1531 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1533 io_channel_flush(rfs->old_fs->io);
1536 ext2fs_blocks_count_set(rfs->old_fs->super, orig_size);
1538 ext2fs_free_extent_table(rfs->bmap);
1542 ext2fs_close_inode_scan(scan);
1544 ext2fs_free_mem(&block_buf);
1549 /* --------------------------------------------------------------------
1551 * Resize processing, phase 4.
1553 * --------------------------------------------------------------------
1559 unsigned int max_dirs;
1563 static int check_and_change_inodes(ext2_ino_t dir,
1564 int entry EXT2FS_ATTR((unused)),
1565 struct ext2_dir_entry *dirent, int offset,
1566 int blocksize EXT2FS_ATTR((unused)),
1567 char *buf EXT2FS_ATTR((unused)),
1570 struct istruct *is = (struct istruct *) priv_data;
1571 struct ext2_inode inode;
1572 ext2_ino_t new_inode;
1575 if (is->rfs->progress && offset == 0) {
1576 io_channel_flush(is->rfs->old_fs->io);
1577 is->err = (is->rfs->progress)(is->rfs,
1578 E2_RSZ_INODE_REF_UPD_PASS,
1579 ++is->num, is->max_dirs);
1581 return DIRENT_ABORT;
1587 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1591 #ifdef RESIZE2FS_DEBUG
1592 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1593 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1594 dir, dirent->name_len&0xFF, dirent->name,
1595 dirent->inode, new_inode);
1598 dirent->inode = new_inode;
1600 /* Update the directory mtime and ctime */
1601 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1603 inode.i_mtime = inode.i_ctime = time(0);
1604 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1606 return DIRENT_ABORT;
1609 return DIRENT_CHANGED;
1612 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1621 * Now, we iterate over all of the directories to update the
1625 is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
1629 if (rfs->progress) {
1630 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1636 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1637 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1638 check_and_change_inodes, &is);
1646 if (rfs->progress && (is.num < is.max_dirs))
1647 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1648 is.max_dirs, is.max_dirs);
1651 ext2fs_free_extent_table(rfs->imap);
1657 /* --------------------------------------------------------------------
1659 * Resize processing, phase 5.
1661 * In this phase we actually move the inode table around, and then
1662 * update the summary statistics. This is scary, since aborting here
1663 * will potentially scramble the filesystem. (We are moving the
1664 * inode tables around in place, and so the potential for lost data,
1665 * or at the very least scrambling the mapping between filenames and
1666 * inode numbers is very high in case of a power failure here.)
1667 * --------------------------------------------------------------------
1672 * A very scary routine --- this one moves the inode table around!!!
1674 * After this you have to use the rfs->new_fs file handle to read and
1677 static errcode_t move_itables(ext2_resize_t rfs)
1681 dgrp_t i, max_groups;
1682 ext2_filsys fs = rfs->new_fs;
1684 blk64_t old_blk, new_blk, blk;
1686 int j, to_move, moved;
1688 max_groups = fs->group_desc_count;
1689 if (max_groups > rfs->old_fs->group_desc_count)
1690 max_groups = rfs->old_fs->group_desc_count;
1692 size = fs->blocksize * fs->inode_blocks_per_group;
1693 if (!rfs->itable_buf) {
1694 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1700 * Figure out how many inode tables we need to move
1702 to_move = moved = 0;
1703 for (i=0; i < max_groups; i++)
1704 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
1705 ext2fs_inode_table_loc(fs, i))
1711 if (rfs->progress) {
1712 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1718 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1720 for (i=0; i < max_groups; i++) {
1721 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
1722 new_blk = ext2fs_inode_table_loc(fs, i);
1723 diff = new_blk - old_blk;
1725 #ifdef RESIZE2FS_DEBUG
1726 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1727 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
1728 i, old_blk, new_blk, diff);
1734 retval = io_channel_read_blk64(fs->io, old_blk,
1735 fs->inode_blocks_per_group,
1740 * The end of the inode table segment often contains
1741 * all zeros, and we're often only moving the inode
1742 * table down a block or two. If so, we can optimize
1743 * things by not rewriting blocks that we know to be zero
1746 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1749 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1750 #ifdef RESIZE2FS_DEBUG
1751 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1752 printf("%d blocks of zeros...\n", n);
1754 num = fs->inode_blocks_per_group;
1758 retval = io_channel_write_blk64(fs->io, new_blk,
1759 num, rfs->itable_buf);
1761 io_channel_write_blk64(fs->io, old_blk,
1762 num, rfs->itable_buf);
1766 retval = io_channel_write_blk64(fs->io,
1767 old_blk + fs->inode_blocks_per_group,
1768 diff, (rfs->itable_buf +
1769 (fs->inode_blocks_per_group - diff) *
1775 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1776 j < fs->inode_blocks_per_group ; j++, blk++)
1777 ext2fs_block_alloc_stats2(fs, blk, -1);
1779 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
1780 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1781 ext2fs_mark_super_dirty(rfs->old_fs);
1782 ext2fs_flush(rfs->old_fs);
1784 if (rfs->progress) {
1785 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1791 mark_table_blocks(fs, fs->block_map);
1793 #ifdef RESIZE2FS_DEBUG
1794 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1795 printf("Inode table move finished.\n");
1804 * Fix the resize inode
1806 static errcode_t fix_resize_inode(ext2_filsys fs)
1808 struct ext2_inode inode;
1810 char *block_buf = NULL;
1812 if (!(fs->super->s_feature_compat &
1813 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1816 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1817 if (retval) goto errout;
1819 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1820 if (retval) goto errout;
1822 ext2fs_iblk_set(fs, &inode, 1);
1824 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1825 if (retval) goto errout;
1827 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1829 * Avoid zeroing out block #0; that's rude. This
1830 * should never happen anyway since the filesystem
1831 * should be fsck'ed and we assume it is consistent.
1834 _("Should never happen: resize inode corrupt!\n"));
1838 memset(block_buf, 0, fs->blocksize);
1840 retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1842 if (retval) goto errout;
1844 retval = ext2fs_create_resize_inode(fs);
1850 ext2fs_free_mem(&block_buf);
1855 * Finally, recalculate the summary information
1857 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1861 unsigned int group = 0;
1862 unsigned int count = 0;
1863 blk64_t total_blocks_free = 0;
1864 int total_inodes_free = 0;
1867 blk64_t super_blk, old_desc_blk, new_desc_blk;
1868 int old_desc_blocks;
1871 * First calculate the block statistics
1873 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1874 ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
1876 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1877 old_desc_blocks = fs->super->s_first_meta_bg;
1879 old_desc_blocks = fs->desc_blocks +
1880 fs->super->s_reserved_gdt_blocks;
1881 for (blk = B2C(fs->super->s_first_data_block);
1882 blk < ext2fs_blocks_count(fs->super);
1883 blk += EXT2FS_CLUSTER_RATIO(fs)) {
1885 !(EQ_CLSTR(blk, super_blk) ||
1886 ((old_desc_blk && old_desc_blocks &&
1887 GE_CLSTR(blk, old_desc_blk) &&
1888 LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) ||
1889 ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) ||
1890 EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) ||
1891 EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) ||
1892 ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) &&
1893 LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group)
1894 + fs->inode_blocks_per_group))))) ||
1895 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
1897 total_blocks_free++;
1900 if ((count == fs->super->s_clusters_per_group) ||
1901 EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) {
1902 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
1903 ext2fs_group_desc_csum_set(fs, group);
1905 if (group >= fs->group_desc_count)
1909 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1910 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
1913 if (fs->super->s_feature_incompat &
1914 EXT2_FEATURE_INCOMPAT_META_BG)
1915 old_desc_blocks = fs->super->s_first_meta_bg;
1917 old_desc_blocks = fs->desc_blocks +
1918 fs->super->s_reserved_gdt_blocks;
1921 total_blocks_free = C2B(total_blocks_free);
1922 ext2fs_free_blocks_count_set(fs->super, total_blocks_free);
1925 * Next, calculate the inode statistics
1931 /* Protect loop from wrap-around if s_inodes_count maxed */
1932 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
1933 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1935 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1937 total_inodes_free++;
1940 if ((count == fs->super->s_inodes_per_group) ||
1941 (ino == fs->super->s_inodes_count)) {
1942 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
1943 ext2fs_group_desc_csum_set(fs, group);
1945 if (group >= fs->group_desc_count)
1949 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
1952 fs->super->s_free_inodes_count = total_inodes_free;
1953 ext2fs_mark_super_dirty(fs);
1958 * Journal may have been relocated; update the backup journal blocks
1959 * in the superblock.
1961 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
1964 struct ext2_inode inode;
1966 if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1969 /* External journal? Nothing to do. */
1970 if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
1973 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
1976 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
1977 fs->super->s_jnl_blocks[15] = inode.i_size_high;
1978 fs->super->s_jnl_blocks[16] = inode.i_size;
1979 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
1980 ext2fs_mark_super_dirty(fs);
1984 static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
1985 int old_desc_blocks)
1987 blk64_t super_blk, old_desc_blk, new_desc_blk;
1990 /* inode table blocks plus allocation bitmaps */
1991 overhead = fs->inode_blocks_per_group + 2;
1993 ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
1994 &old_desc_blk, &new_desc_blk, 0);
1995 if ((grp == 0) || super_blk)
1998 overhead += old_desc_blocks;
1999 else if (new_desc_blk)
2006 * calcluate the minimum number of blocks the given fs can be resized to
2008 blk64_t calculate_minimum_resize_size(ext2_filsys fs)
2010 ext2_ino_t inode_count;
2011 blk64_t blks_needed, groups, data_blocks;
2012 blk64_t grp, data_needed, last_start;
2013 blk64_t overhead = 0;
2014 int old_desc_blocks;
2015 int extra_groups = 0;
2016 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2019 * first figure out how many group descriptors we need to
2020 * handle the number of inodes we have
2022 inode_count = fs->super->s_inodes_count -
2023 fs->super->s_free_inodes_count;
2024 blks_needed = ext2fs_div_ceil(inode_count,
2025 fs->super->s_inodes_per_group) *
2026 EXT2_BLOCKS_PER_GROUP(fs->super);
2027 groups = ext2fs_div64_ceil(blks_needed,
2028 EXT2_BLOCKS_PER_GROUP(fs->super));
2031 * number of old-style block group descriptor blocks
2033 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2034 old_desc_blocks = fs->super->s_first_meta_bg;
2036 old_desc_blocks = fs->desc_blocks +
2037 fs->super->s_reserved_gdt_blocks;
2039 /* calculate how many blocks are needed for data */
2040 data_needed = ext2fs_blocks_count(fs->super) -
2041 ext2fs_free_blocks_count(fs->super);
2043 for (grp = 0; grp < fs->group_desc_count; grp++)
2044 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
2047 * For ext4 we need to allow for up to a flex_bg worth of
2048 * inode tables of slack space so the resize operation can be
2049 * guaranteed to finish.
2051 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2052 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
2053 data_needed += fs->inode_blocks_per_group * extra_groups;
2054 extra_groups = groups % flexbg_size;
2058 * figure out how many data blocks we have given the number of groups
2059 * we need for our inodes
2061 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
2063 for (grp = 0; grp < groups; grp++) {
2064 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
2067 * we want to keep track of how much data we can store in
2068 * the groups leading up to the last group so we can determine
2069 * how big the last group needs to be
2071 if (grp != (groups - 1))
2072 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
2075 data_blocks -= overhead;
2079 * if we need more group descriptors in order to accomodate our data
2080 * then we need to add them here
2082 while (data_needed > data_blocks) {
2083 blk64_t remainder = data_needed - data_blocks;
2086 /* figure out how many more groups we need for the data */
2087 extra_grps = ext2fs_div64_ceil(remainder,
2088 EXT2_BLOCKS_PER_GROUP(fs->super));
2090 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
2092 /* ok we have to account for the last group */
2093 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2094 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
2096 for (grp = groups; grp < groups+extra_grps; grp++) {
2097 overhead = calc_group_overhead(fs, grp,
2101 * again, we need to see how much data we cram into
2102 * all of the groups leading up to the last group
2104 if (grp != (groups + extra_grps - 1))
2105 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
2108 data_blocks -= overhead;
2111 groups += extra_grps;
2112 extra_groups += extra_grps;
2113 if (fs->super->s_feature_incompat
2114 & EXT4_FEATURE_INCOMPAT_FLEX_BG
2115 && extra_groups > flexbg_size) {
2117 * For ext4 we need to allow for up to a flex_bg worth
2118 * of inode tables of slack space so the resize
2119 * operation can be guaranteed to finish.
2121 extra_groups = flexbg_size -
2122 (groups & (flexbg_size - 1));
2123 data_needed += (fs->inode_blocks_per_group *
2125 extra_groups = groups % flexbg_size;
2129 /* now for the fun voodoo */
2130 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2133 * if this is the case then the last group is going to have data in it
2134 * so we need to adjust the size of the last group accordingly
2136 if (last_start < data_needed) {
2137 blk64_t remainder = data_needed - last_start;
2140 * 50 is a magic number that mkfs/resize uses to see if its
2141 * even worth making/resizing the fs. basically you need to
2142 * have at least 50 blocks in addition to the blocks needed
2143 * for the metadata in the last group
2146 overhead += remainder;
2152 overhead += fs->super->s_first_data_block;
2155 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2156 * only do groups-1, and then add the number of blocks needed to
2157 * handle the group descriptor metadata+data that we need
2159 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2160 blks_needed += overhead;
2163 * If at this point we've already added up more "needed" than
2164 * the current size, just return current size as minimum.
2166 if (blks_needed >= ext2fs_blocks_count(fs->super))
2167 return ext2fs_blocks_count(fs->super);
2169 * We need to reserve a few extra blocks if extents are
2170 * enabled, in case we need to grow the extent tree. The more
2171 * we shrink the file system, the more space we need.
2173 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
2174 blks_needed += (ext2fs_blocks_count(fs->super) -