2 * resize2fs.c --- ext2 main routine
4 * Copyright (C) 1997, 1998 by Theodore Ts'o and
7 * Copyright (C) 1999, 2000 by Theosore Ts'o
10 * This file may be redistributed under the terms of the GNU Public
16 * Resizing a filesystem consists of the following phases:
18 * 1. Adjust superblock and write out new parts of the inode
20 * 2. Determine blocks which need to be relocated, and copy the
21 * contents of blocks from their old locations to the new ones.
22 * 3. Scan the inode table, doing the following:
23 * a. If blocks have been moved, update the block
24 * pointers in the inodes and indirect blocks to
25 * point at the new block locations.
26 * b. If parts of the inode table need to be evacuated,
27 * copy inodes from their old locations to their
29 * c. If (b) needs to be done, note which blocks contain
30 * directory information, since we will need to
31 * update the directory information.
32 * 4. Update the directory blocks with the new inode locations.
33 * 5. Move the inode tables, if necessary.
36 #include "resize2fs.h"
38 #ifdef __linux__ /* Kludge for debugging */
39 #define RESIZE2FS_DEBUG
42 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size);
43 static errcode_t blocks_to_move(ext2_resize_t rfs);
44 static errcode_t block_mover(ext2_resize_t rfs);
45 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
46 static errcode_t inode_ref_fix(ext2_resize_t rfs);
47 static errcode_t move_itables(ext2_resize_t rfs);
48 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
51 * Some helper CPP macros
53 #define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap)
54 #define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap)
55 #define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table)
57 #define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i)))
58 #define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i)))
60 #define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \
61 ((blk) < (FS_INODE_TB((fs), (i)) + \
62 (fs)->inode_blocks_per_group)))
67 * This is the top-level routine which does the dirty deed....
69 errcode_t resize_fs(ext2_filsys fs, blk_t new_size, int flags,
70 errcode_t (*progress)(ext2_resize_t rfs, int pass,
72 unsigned long max_val))
77 retval = ext2fs_read_bitmaps(fs);
82 * Create the data structure
84 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct),
88 memset(rfs, 0, sizeof(struct ext2_resize_struct));
93 rfs->progress = progress;
94 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
98 retval = adjust_superblock(rfs, new_size);
102 retval = blocks_to_move(rfs);
106 #ifdef RESIZE2FS_DEBUG
107 if (rfs->flags & RESIZE_DEBUG_BMOVE)
108 printf(_("Number of free blocks: %d/%d, Needed: %d\n"),
109 rfs->old_fs->super->s_free_blocks_count,
110 rfs->new_fs->super->s_free_blocks_count,
114 retval = block_mover(rfs);
118 retval = inode_scan_and_fix(rfs);
122 retval = inode_ref_fix(rfs);
126 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
130 retval = move_itables(rfs);
134 retval = ext2fs_close(rfs->new_fs);
140 ext2fs_free(rfs->old_fs);
142 ext2fs_free_mem((void **) &rfs->itable_buf);
143 ext2fs_free_mem((void **) &rfs);
149 ext2fs_free(rfs->new_fs);
151 ext2fs_free_mem((void **) &rfs->itable_buf);
152 ext2fs_free_mem((void **) &rfs);
156 /* --------------------------------------------------------------------
158 * Resize processing, phase 1.
160 * In this phase we adjust the in-memory superblock information, and
161 * initialize any new parts of the inode table. The new parts of the
162 * inode table are created in virgin disk space, so we can abort here
163 * without any side effects.
164 * --------------------------------------------------------------------
168 * This routine adjusts the superblock and other data structures...
170 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
177 blk_t blk, group_block;
179 int old_numblocks, numblocks, adjblocks;
180 unsigned long max_group;
183 fs->super->s_blocks_count = new_size;
184 ext2fs_mark_super_dirty(fs);
185 ext2fs_mark_bb_dirty(fs);
186 ext2fs_mark_ib_dirty(fs);
189 fs->group_desc_count = (fs->super->s_blocks_count -
190 fs->super->s_first_data_block +
191 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
192 / EXT2_BLOCKS_PER_GROUP(fs->super);
193 if (fs->group_desc_count == 0)
194 return EXT2_ET_TOOSMALL;
195 fs->desc_blocks = (fs->group_desc_count +
196 EXT2_DESC_PER_BLOCK(fs->super) - 1)
197 / EXT2_DESC_PER_BLOCK(fs->super);
200 * Overhead is the number of bookkeeping blocks per group. It
201 * includes the superblock backup, the group descriptor
202 * backups, the inode bitmap, the block bitmap, and the inode
205 * XXX Not all block groups need the descriptor blocks, but
206 * being clever is tricky...
208 overhead = 3 + fs->desc_blocks + fs->inode_blocks_per_group;
211 * See if the last group is big enough to support the
212 * necessary data structures. If not, we need to get rid of
215 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
216 fs->super->s_blocks_per_group;
217 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
218 return EXT2_ET_TOOSMALL;
219 if (rem && (rem < overhead+50)) {
220 fs->super->s_blocks_count -= rem;
224 * Adjust the number of inodes
226 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
227 fs->group_desc_count;
230 * Adjust the number of free blocks
232 blk = rfs->old_fs->super->s_blocks_count;
233 if (blk > fs->super->s_blocks_count)
234 fs->super->s_free_blocks_count -=
235 (blk - fs->super->s_blocks_count);
237 fs->super->s_free_blocks_count +=
238 (fs->super->s_blocks_count - blk);
241 * Adjust the number of reserved blocks
243 blk = rfs->old_fs->super->s_r_blocks_count * 100 /
244 rfs->old_fs->super->s_blocks_count;
245 fs->super->s_r_blocks_count = ((fs->super->s_blocks_count * blk)
249 * Adjust the bitmaps for size
251 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
252 fs->super->s_inodes_count,
254 if (retval) goto errout;
256 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
257 * fs->group_desc_count)) - 1 +
258 fs->super->s_first_data_block;
259 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
260 real_end, fs->block_map);
262 if (retval) goto errout;
265 * Reallocate the group descriptors as necessary.
267 if (rfs->old_fs->desc_blocks != fs->desc_blocks) {
268 retval = ext2fs_resize_mem(rfs->old_fs->desc_blocks *
270 fs->desc_blocks * fs->blocksize,
271 (void **) &fs->group_desc);
277 * Check to make sure there are enough inodes
279 if ((rfs->old_fs->super->s_inodes_count -
280 rfs->old_fs->super->s_free_inodes_count) >
281 rfs->new_fs->super->s_inodes_count) {
287 * If we are shrinking the number block groups, we're done and
290 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
295 * Fix the count of the last (old) block group
297 old_numblocks = (rfs->old_fs->super->s_blocks_count -
298 rfs->old_fs->super->s_first_data_block) %
299 rfs->old_fs->super->s_blocks_per_group;
301 old_numblocks = rfs->old_fs->super->s_blocks_per_group;
302 if (rfs->old_fs->group_desc_count == fs->group_desc_count) {
303 numblocks = (rfs->new_fs->super->s_blocks_count -
304 rfs->new_fs->super->s_first_data_block) %
305 rfs->new_fs->super->s_blocks_per_group;
307 numblocks = rfs->new_fs->super->s_blocks_per_group;
309 numblocks = rfs->new_fs->super->s_blocks_per_group;
310 i = rfs->old_fs->group_desc_count - 1;
311 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
314 * If the number of block groups is staying the same, we're
315 * done and can exit now. (If the number block groups is
316 * shrinking, we had exited earlier.)
318 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
323 * Initialize the new block group descriptors
325 retval = ext2fs_get_mem(fs->blocksize * fs->inode_blocks_per_group,
326 (void **) &rfs->itable_buf);
330 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
331 group_block = fs->super->s_first_data_block +
332 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
334 adj = rfs->old_fs->group_desc_count;
335 max_group = fs->group_desc_count - adj;
337 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
342 for (i = rfs->old_fs->group_desc_count;
343 i < fs->group_desc_count; i++) {
344 memset(&fs->group_desc[i], 0,
345 sizeof(struct ext2_group_desc));
348 if (i == fs->group_desc_count-1) {
349 numblocks = (fs->super->s_blocks_count -
350 fs->super->s_first_data_block) %
351 fs->super->s_blocks_per_group;
353 numblocks = fs->super->s_blocks_per_group;
355 numblocks = fs->super->s_blocks_per_group;
357 if (ext2fs_bg_has_super(fs, i)) {
358 for (j=0; j < fs->desc_blocks+1; j++)
359 ext2fs_mark_block_bitmap(fs->block_map,
361 adjblocks = 1 + fs->desc_blocks;
363 adjblocks += 2 + fs->inode_blocks_per_group;
365 numblocks -= adjblocks;
366 fs->super->s_free_blocks_count -= adjblocks;
367 fs->super->s_free_inodes_count +=
368 fs->super->s_inodes_per_group;
369 fs->group_desc[i].bg_free_blocks_count = numblocks;
370 fs->group_desc[i].bg_free_inodes_count =
371 fs->super->s_inodes_per_group;
372 fs->group_desc[i].bg_used_dirs_count = 0;
374 retval = ext2fs_allocate_group_table(fs, i, 0);
375 if (retval) goto errout;
378 * Write out the new inode table
380 retval = io_channel_write_blk(fs->io,
381 fs->group_desc[i].bg_inode_table,
382 fs->inode_blocks_per_group,
384 if (retval) goto errout;
386 io_channel_flush(fs->io);
388 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
389 i - adj + 1, max_group);
393 group_block += fs->super->s_blocks_per_group;
395 io_channel_flush(fs->io);
402 /* --------------------------------------------------------------------
404 * Resize processing, phase 2.
406 * In this phase we adjust determine which blocks need to be moved, in
407 * blocks_to_move(). We then copy the blocks to their ultimate new
408 * destinations using block_mover(). Since we are copying blocks to
409 * their new locations, again during this pass we can abort without
411 * --------------------------------------------------------------------
415 * This helper function creates a block bitmap with all of the
416 * filesystem meta-data blocks.
418 static errcode_t mark_table_blocks(ext2_filsys fs,
419 ext2fs_block_bitmap *ret_bmap)
423 ext2fs_block_bitmap bmap;
426 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
431 block = fs->super->s_first_data_block;
432 for (i = 0; i < fs->group_desc_count; i++) {
433 if (ext2fs_bg_has_super(fs, i)) {
435 * Mark this group's copy of the superblock
437 ext2fs_mark_block_bitmap(bmap, block);
440 * Mark this group's copy of the descriptors
442 for (j = 0; j < fs->desc_blocks; j++)
443 ext2fs_mark_block_bitmap(bmap, block + j + 1);
447 * Mark the blocks used for the inode table
449 for (j = 0, b = fs->group_desc[i].bg_inode_table;
450 j < fs->inode_blocks_per_group;
452 ext2fs_mark_block_bitmap(bmap, b);
455 * Mark block used for the block bitmap
457 ext2fs_mark_block_bitmap(bmap,
458 fs->group_desc[i].bg_block_bitmap);
460 * Mark block used for the inode bitmap
462 ext2fs_mark_block_bitmap(bmap,
463 fs->group_desc[i].bg_inode_bitmap);
464 block += fs->super->s_blocks_per_group;
471 * This routine marks and unmarks reserved blocks in the new block
472 * bitmap. It also determines which blocks need to be moved and
473 * places this information into the move_blocks bitmap.
475 static errcode_t blocks_to_move(ext2_resize_t rfs)
477 int i, j, max_groups;
478 blk_t blk, group_blk;
479 unsigned long old_blocks, new_blocks;
481 ext2_filsys fs, old_fs;
482 ext2fs_block_bitmap meta_bmap;
485 old_fs = rfs->old_fs;
486 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
489 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
490 &rfs->reserve_blocks);
494 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
499 retval = mark_table_blocks(old_fs, &meta_bmap);
506 * If we're shrinking the filesystem, we need to move all of
507 * the blocks that don't fit any more
509 for (blk = fs->super->s_blocks_count;
510 blk < old_fs->super->s_blocks_count; blk++) {
511 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
512 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
513 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
514 rfs->needed_blocks++;
516 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
519 old_blocks = old_fs->desc_blocks;
520 new_blocks = fs->desc_blocks;
522 if (old_blocks == new_blocks) {
527 max_groups = fs->group_desc_count;
528 if (max_groups > old_fs->group_desc_count)
529 max_groups = old_fs->group_desc_count;
530 group_blk = old_fs->super->s_first_data_block;
532 * If we're reducing the number of descriptor blocks, this
533 * makes life easy. :-) We just have to mark some extra
536 if (old_blocks > new_blocks) {
537 for (i = 0; i < max_groups; i++) {
538 if (!ext2fs_bg_has_super(fs, i)) {
539 group_blk += fs->super->s_blocks_per_group;
542 for (blk = group_blk+1+new_blocks;
543 blk < group_blk+1+old_blocks; blk++) {
544 ext2fs_unmark_block_bitmap(fs->block_map,
546 rfs->needed_blocks--;
548 group_blk += fs->super->s_blocks_per_group;
554 * If we're increasing the number of descriptor blocks, life
555 * gets interesting....
557 for (i = 0; i < max_groups; i++) {
558 if (!ext2fs_bg_has_super(fs, i))
561 for (blk = group_blk;
562 blk < group_blk + 1 + new_blocks; blk++) {
563 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
564 ext2fs_mark_block_bitmap(fs->block_map, blk);
567 * Check to see if we overlap with the inode
568 * or block bitmap, or the inode tables. If
569 * not, and the block is in use, then mark it
570 * as a block to be moved.
572 if (IS_BLOCK_BM(fs, i, blk)) {
573 FS_BLOCK_BM(fs, i) = 0;
574 rfs->needed_blocks++;
575 } else if (IS_INODE_BM(fs, i, blk)) {
576 FS_INODE_BM(fs, i) = 0;
577 rfs->needed_blocks++;
578 } else if (IS_INODE_TB(fs, i, blk)) {
579 FS_INODE_TB(fs, i) = 0;
580 rfs->needed_blocks++;
581 } else if (ext2fs_test_block_bitmap(old_fs->block_map,
583 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
584 ext2fs_mark_block_bitmap(rfs->move_blocks,
586 rfs->needed_blocks++;
589 if (fs->group_desc[i].bg_inode_table &&
590 fs->group_desc[i].bg_inode_bitmap &&
591 fs->group_desc[i].bg_block_bitmap)
595 * Reserve the existing meta blocks that we know
596 * aren't to be moved.
598 if (fs->group_desc[i].bg_block_bitmap)
599 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
600 fs->group_desc[i].bg_block_bitmap);
601 if (fs->group_desc[i].bg_inode_bitmap)
602 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
603 fs->group_desc[i].bg_inode_bitmap);
604 if (fs->group_desc[i].bg_inode_table)
605 for (blk = fs->group_desc[i].bg_inode_table, j=0;
606 j < fs->inode_blocks_per_group ; j++, blk++)
607 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
611 * Allocate the missing data structures
613 retval = ext2fs_allocate_group_table(fs, i,
614 rfs->reserve_blocks);
619 * For those structures that have changed, we need to
622 if (FS_BLOCK_BM(old_fs, i) !=
623 (blk = FS_BLOCK_BM(fs, i))) {
624 ext2fs_mark_block_bitmap(fs->block_map, blk);
625 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
626 !ext2fs_test_block_bitmap(meta_bmap, blk))
627 ext2fs_mark_block_bitmap(rfs->move_blocks,
630 if (FS_INODE_BM(old_fs, i) !=
631 (blk = FS_INODE_BM(fs, i))) {
632 ext2fs_mark_block_bitmap(fs->block_map, blk);
633 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
634 !ext2fs_test_block_bitmap(meta_bmap, blk))
635 ext2fs_mark_block_bitmap(rfs->move_blocks,
640 * The inode table, if we need to relocate it, is
641 * handled specially. We have to reserve the blocks
642 * for both the old and the new inode table, since we
643 * can't have the inode table be destroyed during the
644 * block relocation phase.
646 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
647 goto next_group; /* inode table not moved */
649 rfs->needed_blocks += fs->inode_blocks_per_group;
652 * Mark the new inode table as in use in the new block
653 * allocation bitmap, and move any blocks that might
656 for (blk = fs->group_desc[i].bg_inode_table, j=0;
657 j < fs->inode_blocks_per_group ; j++, blk++) {
658 ext2fs_mark_block_bitmap(fs->block_map, blk);
659 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
660 !ext2fs_test_block_bitmap(meta_bmap, blk))
661 ext2fs_mark_block_bitmap(rfs->move_blocks,
666 * Make sure the old inode table is reserved in the
667 * block reservation bitmap.
669 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
670 j < fs->inode_blocks_per_group ; j++, blk++)
671 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
674 group_blk += rfs->new_fs->super->s_blocks_per_group;
680 ext2fs_free_block_bitmap(meta_bmap);
686 * This helper function tries to allocate a new block. We try to
687 * avoid hitting the original group descriptor blocks at least at
688 * first, since we want to make it possible to recover from a badly
689 * aborted resize operation as much as possible.
691 * In the future, I may further modify this routine to balance out
692 * where we get the new blocks across the various block groups.
693 * Ideally we would allocate blocks that corresponded with the block
694 * group of the containing inode, and keep contiguous blocks
695 * together. However, this very difficult to do efficiently, since we
696 * don't have the necessary information up front.
700 #define DESPERATION 2
702 static void init_block_alloc(ext2_resize_t rfs)
704 rfs->alloc_state = AVOID_OLD;
705 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
707 /* HACK for testing */
708 if (rfs->new_fs->super->s_blocks_count >
709 rfs->old_fs->super->s_blocks_count)
710 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
714 static blk_t get_new_block(ext2_resize_t rfs)
716 ext2_filsys fs = rfs->new_fs;
719 if (rfs->new_blk >= fs->super->s_blocks_count) {
720 if (rfs->alloc_state == DESPERATION)
723 #ifdef RESIZE2FS_DEBUG
724 if (rfs->flags & RESIZE_DEBUG_BMOVE)
725 printf(_("Going into desperation "
726 "mode for block allocations\n"));
728 rfs->alloc_state = DESPERATION;
729 rfs->new_blk = fs->super->s_first_data_block;
732 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
733 ext2fs_test_block_bitmap(rfs->reserve_blocks,
735 ((rfs->alloc_state == AVOID_OLD) &&
736 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
737 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
746 static errcode_t block_mover(ext2_resize_t rfs)
748 blk_t blk, old_blk, new_blk;
749 ext2_filsys fs = rfs->new_fs;
750 ext2_filsys old_fs = rfs->old_fs;
755 new_blk = fs->super->s_first_data_block;
756 if (!rfs->itable_buf) {
757 retval = ext2fs_get_mem(fs->blocksize *
758 fs->inode_blocks_per_group,
759 (void **) &rfs->itable_buf);
763 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
768 * The first step is to figure out where all of the blocks
772 init_block_alloc(rfs);
773 for (blk = old_fs->super->s_first_data_block;
774 blk < old_fs->super->s_blocks_count; blk++) {
775 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
777 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
780 new_blk = get_new_block(rfs);
785 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
786 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
796 * Step two is to actually move the blocks
798 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
799 if (retval) goto errout;
802 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
808 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
809 if (retval) goto errout;
812 #ifdef RESIZE2FS_DEBUG
813 if (rfs->flags & RESIZE_DEBUG_BMOVE)
814 printf(_("Moving %d blocks %u->%u\n"), size,
819 if (c > fs->inode_blocks_per_group)
820 c = fs->inode_blocks_per_group;
821 retval = io_channel_read_blk(fs->io, old_blk, c,
823 if (retval) goto errout;
824 retval = io_channel_write_blk(fs->io, new_blk, c,
826 if (retval) goto errout;
832 io_channel_flush(fs->io);
833 retval = (rfs->progress)(rfs,
834 E2_RSZ_BLOCK_RELOC_PASS,
840 io_channel_flush(fs->io);
848 /* --------------------------------------------------------------------
850 * Resize processing, phase 3
852 * --------------------------------------------------------------------
856 struct process_block_struct {
859 struct ext2_inode * inode;
865 static int process_block(ext2_filsys fs, blk_t *block_nr,
866 e2_blkcnt_t blockcnt, blk_t ref_block,
867 int ref_offset, void *priv_data)
869 struct process_block_struct *pb;
871 blk_t block, new_block;
874 pb = (struct process_block_struct *) priv_data;
877 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
879 *block_nr = new_block;
880 ret |= BLOCK_CHANGED;
882 #ifdef RESIZE2FS_DEBUG
883 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
884 printf(_("ino=%u, blockcnt=%lld, %u->%u\n"),
885 pb->ino, blockcnt, block, new_block);
891 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
892 block, (int) blockcnt);
904 static errcode_t progress_callback(ext2_filsys fs, ext2_inode_scan scan,
905 dgrp_t group, void * priv_data)
907 ext2_resize_t rfs = (ext2_resize_t) priv_data;
911 * This check is to protect against old ext2 libraries. It
912 * shouldn't be needed against new libraries.
918 io_channel_flush(fs->io);
919 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
920 group+1, fs->group_desc_count);
928 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
930 struct process_block_struct pb;
931 ext2_ino_t ino, new_inode;
932 struct ext2_inode inode;
933 ext2_inode_scan scan = NULL;
937 ext2_ino_t start_to_move;
940 if ((rfs->old_fs->group_desc_count <=
941 rfs->new_fs->group_desc_count) &&
946 * Save the original size of the old filesystem, and
947 * temporarily set the size to be the new size if the new size
948 * is larger. We need to do this to avoid catching an error
949 * by the block iterator routines
951 orig_size = rfs->old_fs->super->s_blocks_count;
952 if (orig_size < rfs->new_fs->super->s_blocks_count)
953 rfs->old_fs->super->s_blocks_count =
954 rfs->new_fs->super->s_blocks_count;
956 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
957 if (retval) goto errout;
959 retval = ext2fs_init_dblist(rfs->old_fs, 0);
960 if (retval) goto errout;
961 retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3,
962 (void **) &block_buf);
963 if (retval) goto errout;
965 start_to_move = (rfs->new_fs->group_desc_count *
966 rfs->new_fs->super->s_inodes_per_group);
969 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
970 0, rfs->old_fs->group_desc_count);
974 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
978 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
980 * First, copy all of the inodes that need to be moved
981 * elsewhere in the inode table
984 retval = ext2fs_get_next_inode(scan, &ino, &inode);
985 if (retval) goto errout;
989 if (inode.i_links_count == 0)
990 continue; /* inode not in use */
992 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
995 if (ext2fs_inode_has_valid_blocks(&inode) &&
996 (rfs->bmap || pb.is_dir)) {
998 retval = ext2fs_block_iterate2(rfs->old_fs,
1000 process_block, &pb);
1009 if (ino <= start_to_move)
1010 continue; /* Don't need to move it. */
1016 if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
1020 if (new_inode > rfs->new_fs->super->s_inodes_count) {
1025 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1027 /* Get the new version of the inode */
1028 retval = ext2fs_read_inode(rfs->old_fs, ino, &inode);
1029 if (retval) goto errout;
1031 retval = ext2fs_write_inode(rfs->old_fs, new_inode, &inode);
1032 if (retval) goto errout;
1034 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1035 if (LINUX_S_ISDIR(inode.i_mode))
1036 rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1038 #ifdef RESIZE2FS_DEBUG
1039 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1040 printf(_("Inode moved %u->%u\n"), ino, new_inode);
1043 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1047 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1049 io_channel_flush(rfs->old_fs->io);
1052 rfs->old_fs->super->s_blocks_count = orig_size;
1054 ext2fs_free_extent_table(rfs->bmap);
1058 ext2fs_close_inode_scan(scan);
1060 ext2fs_free_mem((void **) &block_buf);
1064 /* --------------------------------------------------------------------
1066 * Resize processing, phase 4.
1068 * --------------------------------------------------------------------
1074 unsigned long max_dirs;
1078 static int check_and_change_inodes(ext2_ino_t dir, int entry,
1079 struct ext2_dir_entry *dirent, int offset,
1080 int blocksize, char *buf, void *priv_data)
1082 struct istruct *is = (struct istruct *) priv_data;
1083 ext2_ino_t new_inode;
1085 if (is->rfs->progress && offset == 0) {
1086 io_channel_flush(is->rfs->old_fs->io);
1087 is->err = (is->rfs->progress)(is->rfs,
1088 E2_RSZ_INODE_REF_UPD_PASS,
1089 ++is->num, is->max_dirs);
1091 return DIRENT_ABORT;
1097 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1101 #ifdef RESIZE2FS_DEBUG
1102 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1103 printf(_("Inode translate (dir=%u, name=%.*s, %u->%u)\n"),
1104 dir, dirent->name_len, dirent->name,
1105 dirent->inode, new_inode);
1108 dirent->inode = new_inode;
1110 return DIRENT_CHANGED;
1113 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1122 * Now, we iterate over all of the directories to update the
1126 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1130 if (rfs->progress) {
1131 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1137 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1138 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1139 check_and_change_inodes, &is);
1148 ext2fs_free_extent_table(rfs->imap);
1154 /* --------------------------------------------------------------------
1156 * Resize processing, phase 5.
1158 * In this phase we actually move the inode table around, and then
1159 * update the summary statistics. This is scary, since aborting here
1160 * will potentially scramble the filesystem. (We are moving the
1161 * inode tables around in place, and so the potential for lost data,
1162 * or at the very least scrambling the mapping between filenames and
1163 * inode numbers is very high in case of a power failure here.)
1164 * --------------------------------------------------------------------
1169 * A very scary routine --- this one moves the inode table around!!!
1171 * After this you have to use the rfs->new_fs file handle to read and
1174 static errcode_t move_itables(ext2_resize_t rfs)
1176 int i, n, num, max_groups, size, diff;
1177 ext2_filsys fs = rfs->new_fs;
1179 blk_t old_blk, new_blk;
1183 max_groups = fs->group_desc_count;
1184 if (max_groups > rfs->old_fs->group_desc_count)
1185 max_groups = rfs->old_fs->group_desc_count;
1187 size = fs->blocksize * fs->inode_blocks_per_group;
1188 if (!rfs->itable_buf) {
1189 retval = ext2fs_get_mem(size, (void **) &rfs->itable_buf);
1195 * Figure out how many inode tables we need to move
1197 to_move = moved = 0;
1198 for (i=0; i < max_groups; i++)
1199 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1200 fs->group_desc[i].bg_inode_table)
1206 if (rfs->progress) {
1207 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1213 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1215 for (i=0; i < max_groups; i++) {
1216 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1217 new_blk = fs->group_desc[i].bg_inode_table;
1218 diff = new_blk - old_blk;
1220 #ifdef RESIZE2FS_DEBUG
1221 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1222 printf(_("Itable move group %d block "
1223 "%u->%u (diff %d)\n"),
1224 i, old_blk, new_blk, diff);
1230 retval = io_channel_read_blk(fs->io, old_blk,
1231 fs->inode_blocks_per_group,
1236 * The end of the inode table segment often contains
1237 * all zeros, and we're often only moving the inode
1238 * table down a block or two. If so, we can optimize
1239 * things by not rewriting blocks that we know to be zero
1242 for (cp = rfs->itable_buf+size, n=0; n < size; n++, cp--)
1245 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1246 #ifdef RESIZE2FS_DEBUG
1247 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1248 printf(_("%d blocks of zeros...\n"), n);
1250 num = fs->inode_blocks_per_group;
1254 retval = io_channel_write_blk(fs->io, new_blk,
1255 num, rfs->itable_buf);
1257 io_channel_write_blk(fs->io, old_blk,
1258 num, rfs->itable_buf);
1262 retval = io_channel_write_blk(fs->io,
1263 old_blk + fs->inode_blocks_per_group,
1264 diff, (rfs->itable_buf +
1265 (fs->inode_blocks_per_group - diff) *
1270 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1271 ext2fs_mark_super_dirty(rfs->old_fs);
1272 if (rfs->progress) {
1273 ext2fs_flush(rfs->old_fs);
1274 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1281 #ifdef RESIZE2FS_DEBUG
1282 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1283 printf(_("Inode table move finished.\n"));
1292 * Finally, recalculate the summary information
1294 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1304 * First calculate the block statistics
1306 for (blk = fs->super->s_first_data_block;
1307 blk < fs->super->s_blocks_count; blk++) {
1308 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1313 if ((count == fs->super->s_blocks_per_group) ||
1314 (blk == fs->super->s_blocks_count-1)) {
1315 fs->group_desc[group++].bg_free_blocks_count =
1321 fs->super->s_free_blocks_count = total_free;
1324 * Next, calculate the inode statistics
1330 for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
1331 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1336 if ((count == fs->super->s_inodes_per_group) ||
1337 (ino == fs->super->s_inodes_count)) {
1338 fs->group_desc[group++].bg_free_inodes_count =
1344 fs->super->s_free_inodes_count = total_free;
1345 ext2fs_mark_super_dirty(fs);