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 *new_size = rfs->new_fs->super->s_blocks_count;
104 retval = blocks_to_move(rfs);
108 #ifdef RESIZE2FS_DEBUG
109 if (rfs->flags & RESIZE_DEBUG_BMOVE)
110 printf(_("Number of free blocks: %d/%d, Needed: %d\n"),
111 rfs->old_fs->super->s_free_blocks_count,
112 rfs->new_fs->super->s_free_blocks_count,
116 retval = block_mover(rfs);
120 retval = inode_scan_and_fix(rfs);
124 retval = inode_ref_fix(rfs);
128 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
132 retval = move_itables(rfs);
136 retval = ext2fs_close(rfs->new_fs);
142 ext2fs_free(rfs->old_fs);
144 ext2fs_free_mem((void **) &rfs->itable_buf);
145 ext2fs_free_mem((void **) &rfs);
151 ext2fs_free(rfs->new_fs);
153 ext2fs_free_mem((void **) &rfs->itable_buf);
154 ext2fs_free_mem((void **) &rfs);
158 /* --------------------------------------------------------------------
160 * Resize processing, phase 1.
162 * In this phase we adjust the in-memory superblock information, and
163 * initialize any new parts of the inode table. The new parts of the
164 * inode table are created in virgin disk space, so we can abort here
165 * without any side effects.
166 * --------------------------------------------------------------------
170 * This routine adjusts the superblock and other data structures...
172 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
179 blk_t blk, group_block;
181 int old_numblocks, numblocks, adjblocks;
182 unsigned long max_group;
185 fs->super->s_blocks_count = new_size;
186 ext2fs_mark_super_dirty(fs);
187 ext2fs_mark_bb_dirty(fs);
188 ext2fs_mark_ib_dirty(fs);
191 fs->group_desc_count = (fs->super->s_blocks_count -
192 fs->super->s_first_data_block +
193 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
194 / EXT2_BLOCKS_PER_GROUP(fs->super);
195 if (fs->group_desc_count == 0)
196 return EXT2_ET_TOOSMALL;
197 fs->desc_blocks = (fs->group_desc_count +
198 EXT2_DESC_PER_BLOCK(fs->super) - 1)
199 / EXT2_DESC_PER_BLOCK(fs->super);
202 * Overhead is the number of bookkeeping blocks per group. It
203 * includes the superblock backup, the group descriptor
204 * backups, the inode bitmap, the block bitmap, and the inode
207 * XXX Not all block groups need the descriptor blocks, but
208 * being clever is tricky...
210 overhead = 3 + fs->desc_blocks + fs->inode_blocks_per_group;
213 * See if the last group is big enough to support the
214 * necessary data structures. If not, we need to get rid of
217 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
218 fs->super->s_blocks_per_group;
219 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
220 return EXT2_ET_TOOSMALL;
221 if (rem && (rem < overhead+50)) {
222 fs->super->s_blocks_count -= rem;
226 * Adjust the number of inodes
228 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
229 fs->group_desc_count;
232 * Adjust the number of free blocks
234 blk = rfs->old_fs->super->s_blocks_count;
235 if (blk > fs->super->s_blocks_count)
236 fs->super->s_free_blocks_count -=
237 (blk - fs->super->s_blocks_count);
239 fs->super->s_free_blocks_count +=
240 (fs->super->s_blocks_count - blk);
243 * Adjust the number of reserved blocks
245 blk = rfs->old_fs->super->s_r_blocks_count * 100 /
246 rfs->old_fs->super->s_blocks_count;
247 fs->super->s_r_blocks_count = ((fs->super->s_blocks_count * blk)
251 * Adjust the bitmaps for size
253 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
254 fs->super->s_inodes_count,
256 if (retval) goto errout;
258 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
259 * fs->group_desc_count)) - 1 +
260 fs->super->s_first_data_block;
261 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
262 real_end, fs->block_map);
264 if (retval) goto errout;
267 * Reallocate the group descriptors as necessary.
269 if (rfs->old_fs->desc_blocks != fs->desc_blocks) {
270 retval = ext2fs_resize_mem(rfs->old_fs->desc_blocks *
272 fs->desc_blocks * fs->blocksize,
273 (void **) &fs->group_desc);
279 * Check to make sure there are enough inodes
281 if ((rfs->old_fs->super->s_inodes_count -
282 rfs->old_fs->super->s_free_inodes_count) >
283 rfs->new_fs->super->s_inodes_count) {
289 * If we are shrinking the number block groups, we're done and
292 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
297 * Fix the count of the last (old) block group
299 old_numblocks = (rfs->old_fs->super->s_blocks_count -
300 rfs->old_fs->super->s_first_data_block) %
301 rfs->old_fs->super->s_blocks_per_group;
303 old_numblocks = rfs->old_fs->super->s_blocks_per_group;
304 if (rfs->old_fs->group_desc_count == fs->group_desc_count) {
305 numblocks = (rfs->new_fs->super->s_blocks_count -
306 rfs->new_fs->super->s_first_data_block) %
307 rfs->new_fs->super->s_blocks_per_group;
309 numblocks = rfs->new_fs->super->s_blocks_per_group;
311 numblocks = rfs->new_fs->super->s_blocks_per_group;
312 i = rfs->old_fs->group_desc_count - 1;
313 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
316 * If the number of block groups is staying the same, we're
317 * done and can exit now. (If the number block groups is
318 * shrinking, we had exited earlier.)
320 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
325 * Initialize the new block group descriptors
327 retval = ext2fs_get_mem(fs->blocksize * fs->inode_blocks_per_group,
328 (void **) &rfs->itable_buf);
332 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
333 group_block = fs->super->s_first_data_block +
334 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
336 adj = rfs->old_fs->group_desc_count;
337 max_group = fs->group_desc_count - adj;
339 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
344 for (i = rfs->old_fs->group_desc_count;
345 i < fs->group_desc_count; i++) {
346 memset(&fs->group_desc[i], 0,
347 sizeof(struct ext2_group_desc));
350 if (i == fs->group_desc_count-1) {
351 numblocks = (fs->super->s_blocks_count -
352 fs->super->s_first_data_block) %
353 fs->super->s_blocks_per_group;
355 numblocks = fs->super->s_blocks_per_group;
357 numblocks = fs->super->s_blocks_per_group;
359 if (ext2fs_bg_has_super(fs, i)) {
360 for (j=0; j < fs->desc_blocks+1; j++)
361 ext2fs_mark_block_bitmap(fs->block_map,
363 adjblocks = 1 + fs->desc_blocks;
365 adjblocks += 2 + fs->inode_blocks_per_group;
367 numblocks -= adjblocks;
368 fs->super->s_free_blocks_count -= adjblocks;
369 fs->super->s_free_inodes_count +=
370 fs->super->s_inodes_per_group;
371 fs->group_desc[i].bg_free_blocks_count = numblocks;
372 fs->group_desc[i].bg_free_inodes_count =
373 fs->super->s_inodes_per_group;
374 fs->group_desc[i].bg_used_dirs_count = 0;
376 retval = ext2fs_allocate_group_table(fs, i, 0);
377 if (retval) goto errout;
380 * Write out the new inode table
382 retval = io_channel_write_blk(fs->io,
383 fs->group_desc[i].bg_inode_table,
384 fs->inode_blocks_per_group,
386 if (retval) goto errout;
388 io_channel_flush(fs->io);
390 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
391 i - adj + 1, max_group);
395 group_block += fs->super->s_blocks_per_group;
397 io_channel_flush(fs->io);
404 /* --------------------------------------------------------------------
406 * Resize processing, phase 2.
408 * In this phase we adjust determine which blocks need to be moved, in
409 * blocks_to_move(). We then copy the blocks to their ultimate new
410 * destinations using block_mover(). Since we are copying blocks to
411 * their new locations, again during this pass we can abort without
413 * --------------------------------------------------------------------
417 * This helper function creates a block bitmap with all of the
418 * filesystem meta-data blocks.
420 static errcode_t mark_table_blocks(ext2_filsys fs,
421 ext2fs_block_bitmap *ret_bmap)
425 ext2fs_block_bitmap bmap;
428 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
433 block = fs->super->s_first_data_block;
434 for (i = 0; i < fs->group_desc_count; i++) {
435 if (ext2fs_bg_has_super(fs, i)) {
437 * Mark this group's copy of the superblock
439 ext2fs_mark_block_bitmap(bmap, block);
442 * Mark this group's copy of the descriptors
444 for (j = 0; j < fs->desc_blocks; j++)
445 ext2fs_mark_block_bitmap(bmap, block + j + 1);
449 * Mark the blocks used for the inode table
451 for (j = 0, b = fs->group_desc[i].bg_inode_table;
452 j < fs->inode_blocks_per_group;
454 ext2fs_mark_block_bitmap(bmap, b);
457 * Mark block used for the block bitmap
459 ext2fs_mark_block_bitmap(bmap,
460 fs->group_desc[i].bg_block_bitmap);
462 * Mark block used for the inode bitmap
464 ext2fs_mark_block_bitmap(bmap,
465 fs->group_desc[i].bg_inode_bitmap);
466 block += fs->super->s_blocks_per_group;
473 * This routine marks and unmarks reserved blocks in the new block
474 * bitmap. It also determines which blocks need to be moved and
475 * places this information into the move_blocks bitmap.
477 static errcode_t blocks_to_move(ext2_resize_t rfs)
479 int i, j, max_groups;
480 blk_t blk, group_blk;
481 unsigned long old_blocks, new_blocks;
483 ext2_filsys fs, old_fs;
484 ext2fs_block_bitmap meta_bmap;
487 old_fs = rfs->old_fs;
488 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
491 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
492 &rfs->reserve_blocks);
496 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
501 retval = mark_table_blocks(old_fs, &meta_bmap);
508 * If we're shrinking the filesystem, we need to move all of
509 * the blocks that don't fit any more
511 for (blk = fs->super->s_blocks_count;
512 blk < old_fs->super->s_blocks_count; blk++) {
513 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
514 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
515 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
516 rfs->needed_blocks++;
518 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
521 old_blocks = old_fs->desc_blocks;
522 new_blocks = fs->desc_blocks;
524 if (old_blocks == new_blocks) {
529 max_groups = fs->group_desc_count;
530 if (max_groups > old_fs->group_desc_count)
531 max_groups = old_fs->group_desc_count;
532 group_blk = old_fs->super->s_first_data_block;
534 * If we're reducing the number of descriptor blocks, this
535 * makes life easy. :-) We just have to mark some extra
538 if (old_blocks > new_blocks) {
539 for (i = 0; i < max_groups; i++) {
540 if (!ext2fs_bg_has_super(fs, i)) {
541 group_blk += fs->super->s_blocks_per_group;
544 for (blk = group_blk+1+new_blocks;
545 blk < group_blk+1+old_blocks; blk++) {
546 ext2fs_unmark_block_bitmap(fs->block_map,
548 rfs->needed_blocks--;
550 group_blk += fs->super->s_blocks_per_group;
556 * If we're increasing the number of descriptor blocks, life
557 * gets interesting....
559 for (i = 0; i < max_groups; i++) {
560 if (!ext2fs_bg_has_super(fs, i))
563 for (blk = group_blk;
564 blk < group_blk + 1 + new_blocks; blk++) {
565 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
566 ext2fs_mark_block_bitmap(fs->block_map, blk);
569 * Check to see if we overlap with the inode
570 * or block bitmap, or the inode tables. If
571 * not, and the block is in use, then mark it
572 * as a block to be moved.
574 if (IS_BLOCK_BM(fs, i, blk)) {
575 FS_BLOCK_BM(fs, i) = 0;
576 rfs->needed_blocks++;
577 } else if (IS_INODE_BM(fs, i, blk)) {
578 FS_INODE_BM(fs, i) = 0;
579 rfs->needed_blocks++;
580 } else if (IS_INODE_TB(fs, i, blk)) {
581 FS_INODE_TB(fs, i) = 0;
582 rfs->needed_blocks++;
583 } else if (ext2fs_test_block_bitmap(old_fs->block_map,
585 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
586 ext2fs_mark_block_bitmap(rfs->move_blocks,
588 rfs->needed_blocks++;
591 if (fs->group_desc[i].bg_inode_table &&
592 fs->group_desc[i].bg_inode_bitmap &&
593 fs->group_desc[i].bg_block_bitmap)
597 * Reserve the existing meta blocks that we know
598 * aren't to be moved.
600 if (fs->group_desc[i].bg_block_bitmap)
601 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
602 fs->group_desc[i].bg_block_bitmap);
603 if (fs->group_desc[i].bg_inode_bitmap)
604 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
605 fs->group_desc[i].bg_inode_bitmap);
606 if (fs->group_desc[i].bg_inode_table)
607 for (blk = fs->group_desc[i].bg_inode_table, j=0;
608 j < fs->inode_blocks_per_group ; j++, blk++)
609 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
613 * Allocate the missing data structures
615 retval = ext2fs_allocate_group_table(fs, i,
616 rfs->reserve_blocks);
621 * For those structures that have changed, we need to
624 if (FS_BLOCK_BM(old_fs, i) !=
625 (blk = FS_BLOCK_BM(fs, i))) {
626 ext2fs_mark_block_bitmap(fs->block_map, blk);
627 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
628 !ext2fs_test_block_bitmap(meta_bmap, blk))
629 ext2fs_mark_block_bitmap(rfs->move_blocks,
632 if (FS_INODE_BM(old_fs, i) !=
633 (blk = FS_INODE_BM(fs, i))) {
634 ext2fs_mark_block_bitmap(fs->block_map, blk);
635 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
636 !ext2fs_test_block_bitmap(meta_bmap, blk))
637 ext2fs_mark_block_bitmap(rfs->move_blocks,
642 * The inode table, if we need to relocate it, is
643 * handled specially. We have to reserve the blocks
644 * for both the old and the new inode table, since we
645 * can't have the inode table be destroyed during the
646 * block relocation phase.
648 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
649 goto next_group; /* inode table not moved */
651 rfs->needed_blocks += fs->inode_blocks_per_group;
654 * Mark the new inode table as in use in the new block
655 * allocation bitmap, and move any blocks that might
658 for (blk = fs->group_desc[i].bg_inode_table, j=0;
659 j < fs->inode_blocks_per_group ; j++, blk++) {
660 ext2fs_mark_block_bitmap(fs->block_map, blk);
661 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
662 !ext2fs_test_block_bitmap(meta_bmap, blk))
663 ext2fs_mark_block_bitmap(rfs->move_blocks,
668 * Make sure the old inode table is reserved in the
669 * block reservation bitmap.
671 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
672 j < fs->inode_blocks_per_group ; j++, blk++)
673 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
676 group_blk += rfs->new_fs->super->s_blocks_per_group;
682 ext2fs_free_block_bitmap(meta_bmap);
688 * This helper function tries to allocate a new block. We try to
689 * avoid hitting the original group descriptor blocks at least at
690 * first, since we want to make it possible to recover from a badly
691 * aborted resize operation as much as possible.
693 * In the future, I may further modify this routine to balance out
694 * where we get the new blocks across the various block groups.
695 * Ideally we would allocate blocks that corresponded with the block
696 * group of the containing inode, and keep contiguous blocks
697 * together. However, this very difficult to do efficiently, since we
698 * don't have the necessary information up front.
702 #define DESPERATION 2
704 static void init_block_alloc(ext2_resize_t rfs)
706 rfs->alloc_state = AVOID_OLD;
707 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
709 /* HACK for testing */
710 if (rfs->new_fs->super->s_blocks_count >
711 rfs->old_fs->super->s_blocks_count)
712 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
716 static blk_t get_new_block(ext2_resize_t rfs)
718 ext2_filsys fs = rfs->new_fs;
721 if (rfs->new_blk >= fs->super->s_blocks_count) {
722 if (rfs->alloc_state == DESPERATION)
725 #ifdef RESIZE2FS_DEBUG
726 if (rfs->flags & RESIZE_DEBUG_BMOVE)
727 printf(_("Going into desperation "
728 "mode for block allocations\n"));
730 rfs->alloc_state = DESPERATION;
731 rfs->new_blk = fs->super->s_first_data_block;
734 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
735 ext2fs_test_block_bitmap(rfs->reserve_blocks,
737 ((rfs->alloc_state == AVOID_OLD) &&
738 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
739 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
748 static errcode_t block_mover(ext2_resize_t rfs)
750 blk_t blk, old_blk, new_blk;
751 ext2_filsys fs = rfs->new_fs;
752 ext2_filsys old_fs = rfs->old_fs;
757 new_blk = fs->super->s_first_data_block;
758 if (!rfs->itable_buf) {
759 retval = ext2fs_get_mem(fs->blocksize *
760 fs->inode_blocks_per_group,
761 (void **) &rfs->itable_buf);
765 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
770 * The first step is to figure out where all of the blocks
774 init_block_alloc(rfs);
775 for (blk = old_fs->super->s_first_data_block;
776 blk < old_fs->super->s_blocks_count; blk++) {
777 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
779 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
782 new_blk = get_new_block(rfs);
787 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
788 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
798 * Step two is to actually move the blocks
800 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
801 if (retval) goto errout;
804 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
810 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
811 if (retval) goto errout;
814 #ifdef RESIZE2FS_DEBUG
815 if (rfs->flags & RESIZE_DEBUG_BMOVE)
816 printf(_("Moving %d blocks %u->%u\n"), size,
821 if (c > fs->inode_blocks_per_group)
822 c = fs->inode_blocks_per_group;
823 retval = io_channel_read_blk(fs->io, old_blk, c,
825 if (retval) goto errout;
826 retval = io_channel_write_blk(fs->io, new_blk, c,
828 if (retval) goto errout;
834 io_channel_flush(fs->io);
835 retval = (rfs->progress)(rfs,
836 E2_RSZ_BLOCK_RELOC_PASS,
842 io_channel_flush(fs->io);
850 /* --------------------------------------------------------------------
852 * Resize processing, phase 3
854 * --------------------------------------------------------------------
858 struct process_block_struct {
861 struct ext2_inode * inode;
867 static int process_block(ext2_filsys fs, blk_t *block_nr,
868 e2_blkcnt_t blockcnt, blk_t ref_block,
869 int ref_offset, void *priv_data)
871 struct process_block_struct *pb;
873 blk_t block, new_block;
876 pb = (struct process_block_struct *) priv_data;
879 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
881 *block_nr = new_block;
882 ret |= BLOCK_CHANGED;
884 #ifdef RESIZE2FS_DEBUG
885 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
886 printf(_("ino=%u, blockcnt=%lld, %u->%u\n"),
887 pb->ino, blockcnt, block, new_block);
893 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
894 block, (int) blockcnt);
906 static errcode_t progress_callback(ext2_filsys fs, ext2_inode_scan scan,
907 dgrp_t group, void * priv_data)
909 ext2_resize_t rfs = (ext2_resize_t) priv_data;
913 * This check is to protect against old ext2 libraries. It
914 * shouldn't be needed against new libraries.
920 io_channel_flush(fs->io);
921 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
922 group+1, fs->group_desc_count);
930 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
932 struct process_block_struct pb;
933 ext2_ino_t ino, new_inode;
934 struct ext2_inode inode;
935 ext2_inode_scan scan = NULL;
939 ext2_ino_t start_to_move;
942 if ((rfs->old_fs->group_desc_count <=
943 rfs->new_fs->group_desc_count) &&
948 * Save the original size of the old filesystem, and
949 * temporarily set the size to be the new size if the new size
950 * is larger. We need to do this to avoid catching an error
951 * by the block iterator routines
953 orig_size = rfs->old_fs->super->s_blocks_count;
954 if (orig_size < rfs->new_fs->super->s_blocks_count)
955 rfs->old_fs->super->s_blocks_count =
956 rfs->new_fs->super->s_blocks_count;
958 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
959 if (retval) goto errout;
961 retval = ext2fs_init_dblist(rfs->old_fs, 0);
962 if (retval) goto errout;
963 retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3,
964 (void **) &block_buf);
965 if (retval) goto errout;
967 start_to_move = (rfs->new_fs->group_desc_count *
968 rfs->new_fs->super->s_inodes_per_group);
971 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
972 0, rfs->old_fs->group_desc_count);
976 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
980 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
982 * First, copy all of the inodes that need to be moved
983 * elsewhere in the inode table
986 retval = ext2fs_get_next_inode(scan, &ino, &inode);
987 if (retval) goto errout;
991 if (inode.i_links_count == 0)
992 continue; /* inode not in use */
994 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
997 if (ext2fs_inode_has_valid_blocks(&inode) &&
998 (rfs->bmap || pb.is_dir)) {
1000 retval = ext2fs_block_iterate2(rfs->old_fs,
1002 process_block, &pb);
1011 if (ino <= start_to_move)
1012 continue; /* Don't need to move it. */
1018 if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
1022 if (new_inode > rfs->new_fs->super->s_inodes_count) {
1027 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1029 /* Get the new version of the inode */
1030 retval = ext2fs_read_inode(rfs->old_fs, ino, &inode);
1031 if (retval) goto errout;
1033 retval = ext2fs_write_inode(rfs->old_fs, new_inode, &inode);
1034 if (retval) goto errout;
1036 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1037 if (LINUX_S_ISDIR(inode.i_mode))
1038 rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1040 #ifdef RESIZE2FS_DEBUG
1041 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1042 printf(_("Inode moved %u->%u\n"), ino, new_inode);
1045 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1049 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1051 io_channel_flush(rfs->old_fs->io);
1054 rfs->old_fs->super->s_blocks_count = orig_size;
1056 ext2fs_free_extent_table(rfs->bmap);
1060 ext2fs_close_inode_scan(scan);
1062 ext2fs_free_mem((void **) &block_buf);
1066 /* --------------------------------------------------------------------
1068 * Resize processing, phase 4.
1070 * --------------------------------------------------------------------
1076 unsigned long max_dirs;
1080 static int check_and_change_inodes(ext2_ino_t dir, int entry,
1081 struct ext2_dir_entry *dirent, int offset,
1082 int blocksize, char *buf, void *priv_data)
1084 struct istruct *is = (struct istruct *) priv_data;
1085 ext2_ino_t new_inode;
1087 if (is->rfs->progress && offset == 0) {
1088 io_channel_flush(is->rfs->old_fs->io);
1089 is->err = (is->rfs->progress)(is->rfs,
1090 E2_RSZ_INODE_REF_UPD_PASS,
1091 ++is->num, is->max_dirs);
1093 return DIRENT_ABORT;
1099 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1103 #ifdef RESIZE2FS_DEBUG
1104 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1105 printf(_("Inode translate (dir=%u, name=%.*s, %u->%u)\n"),
1106 dir, dirent->name_len, dirent->name,
1107 dirent->inode, new_inode);
1110 dirent->inode = new_inode;
1112 return DIRENT_CHANGED;
1115 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1124 * Now, we iterate over all of the directories to update the
1128 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1132 if (rfs->progress) {
1133 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1139 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1140 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1141 check_and_change_inodes, &is);
1150 ext2fs_free_extent_table(rfs->imap);
1156 /* --------------------------------------------------------------------
1158 * Resize processing, phase 5.
1160 * In this phase we actually move the inode table around, and then
1161 * update the summary statistics. This is scary, since aborting here
1162 * will potentially scramble the filesystem. (We are moving the
1163 * inode tables around in place, and so the potential for lost data,
1164 * or at the very least scrambling the mapping between filenames and
1165 * inode numbers is very high in case of a power failure here.)
1166 * --------------------------------------------------------------------
1171 * A very scary routine --- this one moves the inode table around!!!
1173 * After this you have to use the rfs->new_fs file handle to read and
1176 static errcode_t move_itables(ext2_resize_t rfs)
1178 int i, n, num, max_groups, size, diff;
1179 ext2_filsys fs = rfs->new_fs;
1181 blk_t old_blk, new_blk;
1185 max_groups = fs->group_desc_count;
1186 if (max_groups > rfs->old_fs->group_desc_count)
1187 max_groups = rfs->old_fs->group_desc_count;
1189 size = fs->blocksize * fs->inode_blocks_per_group;
1190 if (!rfs->itable_buf) {
1191 retval = ext2fs_get_mem(size, (void **) &rfs->itable_buf);
1197 * Figure out how many inode tables we need to move
1199 to_move = moved = 0;
1200 for (i=0; i < max_groups; i++)
1201 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1202 fs->group_desc[i].bg_inode_table)
1208 if (rfs->progress) {
1209 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1215 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1217 for (i=0; i < max_groups; i++) {
1218 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1219 new_blk = fs->group_desc[i].bg_inode_table;
1220 diff = new_blk - old_blk;
1222 #ifdef RESIZE2FS_DEBUG
1223 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1224 printf(_("Itable move group %d block "
1225 "%u->%u (diff %d)\n"),
1226 i, old_blk, new_blk, diff);
1232 retval = io_channel_read_blk(fs->io, old_blk,
1233 fs->inode_blocks_per_group,
1238 * The end of the inode table segment often contains
1239 * all zeros, and we're often only moving the inode
1240 * table down a block or two. If so, we can optimize
1241 * things by not rewriting blocks that we know to be zero
1244 for (cp = rfs->itable_buf+size, n=0; n < size; n++, cp--)
1247 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1248 #ifdef RESIZE2FS_DEBUG
1249 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1250 printf(_("%d blocks of zeros...\n"), n);
1252 num = fs->inode_blocks_per_group;
1256 retval = io_channel_write_blk(fs->io, new_blk,
1257 num, rfs->itable_buf);
1259 io_channel_write_blk(fs->io, old_blk,
1260 num, rfs->itable_buf);
1264 retval = io_channel_write_blk(fs->io,
1265 old_blk + fs->inode_blocks_per_group,
1266 diff, (rfs->itable_buf +
1267 (fs->inode_blocks_per_group - diff) *
1272 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1273 ext2fs_mark_super_dirty(rfs->old_fs);
1274 if (rfs->progress) {
1275 ext2fs_flush(rfs->old_fs);
1276 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1283 #ifdef RESIZE2FS_DEBUG
1284 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1285 printf(_("Inode table move finished.\n"));
1294 * Finally, recalculate the summary information
1296 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1306 * First calculate the block statistics
1308 for (blk = fs->super->s_first_data_block;
1309 blk < fs->super->s_blocks_count; blk++) {
1310 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1315 if ((count == fs->super->s_blocks_per_group) ||
1316 (blk == fs->super->s_blocks_count-1)) {
1317 fs->group_desc[group++].bg_free_blocks_count =
1323 fs->super->s_free_blocks_count = total_free;
1326 * Next, calculate the inode statistics
1332 for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
1333 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1338 if ((count == fs->super->s_inodes_per_group) ||
1339 (ino == fs->super->s_inodes_count)) {
1340 fs->group_desc[group++].bg_free_inodes_count =
1346 fs->super->s_free_inodes_count = total_free;
1347 ext2fs_mark_super_dirty(fs);