2 * resize2fs.c --- ext2 main routine
4 * Copyright (C) 1997 Theodore Ts'o
12 * Resizing a filesystem consists of the following phases:
14 * 1. Adjust superblock and write out new parts of the inode
16 * 2. Determine blocks which need to be relocated, and copy the
17 * contents of blocks from their old locations to the new ones.
18 * 3. Scan the inode table, doing the following:
19 * a. If blocks have been moved, update the block
20 * pointers in the inodes and indirect blocks to
21 * point at the new block locations.
22 * b. If parts of the inode table need to be evacuated,
23 * copy inodes from their old locations to their
25 * c. If (b) needs to be done, note which blocks contain
26 * directory information, since we will need to
27 * update the directory information.
28 * 4. Update the directory blocks with the new inode locations.
29 * 5. Move the inode tables, if necessary.
32 #include "resize2fs.h"
34 #ifdef linux /* Kludge for debugging */
35 #define RESIZE2FS_DEBUG
38 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size);
39 static errcode_t blocks_to_move(ext2_resize_t rfs);
40 static errcode_t block_mover(ext2_resize_t rfs);
41 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
42 static errcode_t inode_ref_fix(ext2_resize_t rfs);
43 static errcode_t move_itables(ext2_resize_t rfs);
44 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
47 * Some helper CPP macros
49 #define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap)
50 #define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap)
51 #define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table)
53 #define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i)))
54 #define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i)))
56 #define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \
57 ((blk) < (FS_INODE_TB((fs), (i)) + \
58 (fs)->inode_blocks_per_group)))
63 * This is the top-level routine which does the dirty deed....
65 errcode_t resize_fs(ext2_filsys fs, blk_t new_size, int flags,
66 void (*progress)(ext2_resize_t rfs, int pass,
73 retval = ext2fs_read_bitmaps(fs);
78 * Create the data structure
80 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct),
84 memset(rfs, 0, sizeof(struct ext2_resize_struct));
89 rfs->progress = progress;
90 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
94 retval = adjust_superblock(rfs, new_size);
98 retval = blocks_to_move(rfs);
102 #ifdef RESIZE2FS_DEBUG
103 if (rfs->flags & RESIZE_DEBUG_BMOVE)
104 printf("Number of free blocks: %d/%d, Needed: %d\n",
105 rfs->old_fs->super->s_free_blocks_count,
106 rfs->new_fs->super->s_free_blocks_count,
110 retval = block_mover(rfs);
114 retval = inode_scan_and_fix(rfs);
118 retval = inode_ref_fix(rfs);
122 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
126 retval = move_itables(rfs);
130 retval = ext2fs_close(rfs->new_fs);
136 ext2fs_free(rfs->old_fs);
138 ext2fs_free_mem((void **) &rfs->itable_buf);
139 ext2fs_free_mem((void **) &rfs);
145 ext2fs_free(rfs->new_fs);
147 ext2fs_free_mem((void **) &rfs->itable_buf);
148 ext2fs_free_mem((void **) &rfs);
152 /* --------------------------------------------------------------------
154 * Resize processing, phase 1.
156 * In this phase we adjust the in-memory superblock information, and
157 * initialize any new parts of the inode table. The new parts of the
158 * inode table are created in virgin disk space, so we can abort here
159 * without any side effects.
160 * --------------------------------------------------------------------
164 * This routine adjusts the superblock and other data structures...
166 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
173 blk_t blk, group_block;
175 int old_numblocks, numblocks, adjblocks;
176 unsigned long max_group;
179 fs->super->s_blocks_count = new_size;
180 ext2fs_mark_super_dirty(fs);
181 ext2fs_mark_bb_dirty(fs);
182 ext2fs_mark_ib_dirty(fs);
185 fs->group_desc_count = (fs->super->s_blocks_count -
186 fs->super->s_first_data_block +
187 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
188 / EXT2_BLOCKS_PER_GROUP(fs->super);
189 if (fs->group_desc_count == 0)
190 return EXT2_ET_TOOSMALL;
191 fs->desc_blocks = (fs->group_desc_count +
192 EXT2_DESC_PER_BLOCK(fs->super) - 1)
193 / EXT2_DESC_PER_BLOCK(fs->super);
196 * Overhead is the number of bookkeeping blocks per group. It
197 * includes the superblock backup, the group descriptor
198 * backups, the inode bitmap, the block bitmap, and the inode
201 * XXX Not all block groups need the descriptor blocks, but
202 * being clever is tricky...
204 overhead = 3 + fs->desc_blocks + fs->inode_blocks_per_group;
207 * See if the last group is big enough to support the
208 * necessary data structures. If not, we need to get rid of
211 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
212 fs->super->s_blocks_per_group;
213 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
214 return EXT2_ET_TOOSMALL;
215 if (rem && (rem < overhead+50)) {
216 fs->super->s_blocks_count -= rem;
220 * Adjust the number of inodes
222 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
223 fs->group_desc_count;
226 * Adjust the number of free blocks
228 blk = rfs->old_fs->super->s_blocks_count;
229 if (blk > fs->super->s_blocks_count)
230 fs->super->s_free_blocks_count -=
231 (blk - fs->super->s_blocks_count);
233 fs->super->s_free_blocks_count +=
234 (fs->super->s_blocks_count - blk);
237 * Adjust the number of reserved blocks
239 blk = rfs->old_fs->super->s_r_blocks_count * 100 /
240 rfs->old_fs->super->s_blocks_count;
241 fs->super->s_r_blocks_count = ((fs->super->s_blocks_count * blk)
245 * Adjust the bitmaps for size
247 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
248 fs->super->s_inodes_count,
250 if (retval) goto errout;
252 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
253 * fs->group_desc_count)) - 1 +
254 fs->super->s_first_data_block;
255 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
256 real_end, fs->block_map);
258 if (retval) goto errout;
261 * Reallocate the group descriptors as necessary.
263 if (rfs->old_fs->desc_blocks != fs->desc_blocks) {
264 retval = ext2fs_resize_mem(fs->desc_blocks * fs->blocksize,
265 (void **) &fs->group_desc);
271 * Check to make sure there are enough inodes
273 if ((rfs->old_fs->super->s_inodes_count -
274 rfs->old_fs->super->s_free_inodes_count) >
275 rfs->new_fs->super->s_inodes_count) {
281 * If we are shrinking the number block groups, we're done and
284 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
289 * Fix the count of the last (old) block group
291 old_numblocks = (rfs->old_fs->super->s_blocks_count -
292 rfs->old_fs->super->s_first_data_block) %
293 rfs->old_fs->super->s_blocks_per_group;
295 old_numblocks = rfs->old_fs->super->s_blocks_per_group;
296 if (rfs->old_fs->group_desc_count == fs->group_desc_count) {
297 numblocks = (rfs->new_fs->super->s_blocks_count -
298 rfs->new_fs->super->s_first_data_block) %
299 rfs->new_fs->super->s_blocks_per_group;
301 numblocks = rfs->new_fs->super->s_blocks_per_group;
303 numblocks = rfs->new_fs->super->s_blocks_per_group;
304 i = rfs->old_fs->group_desc_count - 1;
305 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
308 * If the number of block groups is staying the same, we're
309 * done and can exit now. (If the number block groups is
310 * shrinking, we had exited earlier.)
312 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
317 * Initialize the new block group descriptors
319 retval = ext2fs_get_mem(fs->blocksize * fs->inode_blocks_per_group,
320 (void **) &rfs->itable_buf);
324 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
325 group_block = fs->super->s_first_data_block +
326 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
328 adj = rfs->old_fs->group_desc_count;
329 max_group = fs->group_desc_count - adj;
331 rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
333 for (i = rfs->old_fs->group_desc_count;
334 i < fs->group_desc_count; i++) {
335 memset(&fs->group_desc[i], 0,
336 sizeof(struct ext2_group_desc));
339 if (i == fs->group_desc_count-1) {
340 numblocks = (fs->super->s_blocks_count -
341 fs->super->s_first_data_block) %
342 fs->super->s_blocks_per_group;
344 numblocks = fs->super->s_blocks_per_group;
346 numblocks = fs->super->s_blocks_per_group;
348 if (ext2fs_bg_has_super(fs, i)) {
349 for (j=0; j < fs->desc_blocks+1; j++)
350 ext2fs_mark_block_bitmap(fs->block_map,
352 adjblocks = 1 + fs->desc_blocks;
354 adjblocks += 2 + fs->inode_blocks_per_group;
356 numblocks -= adjblocks;
357 fs->super->s_free_blocks_count -= adjblocks;
358 fs->super->s_free_inodes_count +=
359 fs->super->s_inodes_per_group;
360 fs->group_desc[i].bg_free_blocks_count = numblocks;
361 fs->group_desc[i].bg_free_inodes_count =
362 fs->super->s_inodes_per_group;
363 fs->group_desc[i].bg_used_dirs_count = 0;
365 retval = ext2fs_allocate_group_table(fs, i, 0);
366 if (retval) goto errout;
369 * Write out the new inode table
371 retval = io_channel_write_blk(fs->io,
372 fs->group_desc[i].bg_inode_table,
373 fs->inode_blocks_per_group,
375 if (retval) goto errout;
377 io_channel_flush(fs->io);
379 rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
380 i - adj + 1, max_group);
382 group_block += fs->super->s_blocks_per_group;
384 io_channel_flush(fs->io);
391 /* --------------------------------------------------------------------
393 * Resize processing, phase 2.
395 * In this phase we adjust determine which blocks need to be moved, in
396 * blocks_to_move(). We then copy the blocks to their ultimate new
397 * destinations using block_mover(). Since we are copying blocks to
398 * their new locations, again during this pass we can abort without
400 * --------------------------------------------------------------------
404 * This helper function creates a block bitmap with all of the
405 * filesystem meta-data blocks.
407 static errcode_t mark_table_blocks(ext2_filsys fs,
408 ext2fs_block_bitmap *ret_bmap)
412 ext2fs_block_bitmap bmap;
415 retval = ext2fs_allocate_block_bitmap(fs, "meta-data blocks", &bmap);
419 block = fs->super->s_first_data_block;
420 for (i = 0; i < fs->group_desc_count; i++) {
421 if (ext2fs_bg_has_super(fs, i)) {
423 * Mark this group's copy of the superblock
425 ext2fs_mark_block_bitmap(bmap, block);
428 * Mark this group's copy of the descriptors
430 for (j = 0; j < fs->desc_blocks; j++)
431 ext2fs_mark_block_bitmap(bmap, block + j + 1);
435 * Mark the blocks used for the inode table
437 for (j = 0, b = fs->group_desc[i].bg_inode_table;
438 j < fs->inode_blocks_per_group;
440 ext2fs_mark_block_bitmap(bmap, b);
443 * Mark block used for the block bitmap
445 ext2fs_mark_block_bitmap(bmap,
446 fs->group_desc[i].bg_block_bitmap);
448 * Mark block used for the inode bitmap
450 ext2fs_mark_block_bitmap(bmap,
451 fs->group_desc[i].bg_inode_bitmap);
452 block += fs->super->s_blocks_per_group;
459 * This routine marks and unmarks reserved blocks in the new block
460 * bitmap. It also determines which blocks need to be moved and
461 * places this information into the move_blocks bitmap.
463 static errcode_t blocks_to_move(ext2_resize_t rfs)
466 blk_t blk, group_blk;
467 unsigned long old_blocks, new_blocks;
469 ext2_filsys fs, old_fs;
470 ext2fs_block_bitmap meta_bmap;
473 old_fs = rfs->old_fs;
474 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
477 retval = ext2fs_allocate_block_bitmap(fs, "reserved blocks",
478 &rfs->reserve_blocks);
482 retval = ext2fs_allocate_block_bitmap(fs, "blocks to be moved",
487 retval = mark_table_blocks(fs, &meta_bmap);
494 * If we're shrinking the filesystem, we need to move all of
495 * the blocks that don't fit any more
497 for (blk = fs->super->s_blocks_count;
498 blk < old_fs->super->s_blocks_count; blk++) {
499 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
500 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
501 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
502 rfs->needed_blocks++;
504 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
507 old_blocks = old_fs->desc_blocks;
508 new_blocks = fs->desc_blocks;
510 if (old_blocks == new_blocks) {
515 max = fs->group_desc_count;
516 if (max > old_fs->group_desc_count)
517 max = old_fs->group_desc_count;
518 group_blk = old_fs->super->s_first_data_block;
520 * If we're reducing the number of descriptor blocks, this
521 * makes life easy. :-) We just have to mark some extra
524 if (old_blocks > new_blocks) {
525 for (i = 0; i < max; i++) {
526 if (!ext2fs_bg_has_super(fs, i)) {
527 group_blk += fs->super->s_blocks_per_group;
530 for (blk = group_blk+1+new_blocks;
531 blk < group_blk+1+old_blocks; blk++) {
532 ext2fs_unmark_block_bitmap(fs->block_map,
534 rfs->needed_blocks--;
536 group_blk += fs->super->s_blocks_per_group;
542 * If we're increasing the number of descriptor blocks, life
543 * gets interesting....
545 for (i = 0; i < max; i++) {
546 if (!ext2fs_bg_has_super(fs, i))
549 for (blk = group_blk;
550 blk < group_blk + 1 + new_blocks; blk++) {
551 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
552 ext2fs_mark_block_bitmap(fs->block_map, blk);
555 * Check to see if we overlap with the inode
556 * or block bitmap, or the inode tables. If
557 * not, and the block is in use, then mark it
558 * as a block to be moved.
560 if (IS_BLOCK_BM(fs, i, blk)) {
561 FS_BLOCK_BM(fs, i) = 0;
562 rfs->needed_blocks++;
563 } else if (IS_INODE_BM(fs, i, blk)) {
564 FS_INODE_BM(fs, i) = 0;
565 rfs->needed_blocks++;
566 } else if (IS_INODE_TB(fs, i, blk)) {
567 FS_INODE_TB(fs, i) = 0;
568 rfs->needed_blocks++;
569 } else if (ext2fs_test_block_bitmap(old_fs->block_map,
571 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
572 ext2fs_mark_block_bitmap(rfs->move_blocks,
574 rfs->needed_blocks++;
577 if (fs->group_desc[i].bg_inode_table &&
578 fs->group_desc[i].bg_inode_bitmap &&
579 fs->group_desc[i].bg_block_bitmap)
583 * Reserve the existing meta blocks that we know
584 * aren't to be moved.
586 if (fs->group_desc[i].bg_block_bitmap)
587 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
588 fs->group_desc[i].bg_block_bitmap);
589 if (fs->group_desc[i].bg_inode_bitmap)
590 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
591 fs->group_desc[i].bg_inode_bitmap);
592 if (fs->group_desc[i].bg_inode_table)
593 for (blk = fs->group_desc[i].bg_inode_table, j=0;
594 j < fs->inode_blocks_per_group ; j++, blk++)
595 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
599 * Allocate the missing data structures
601 retval = ext2fs_allocate_group_table(fs, i,
602 rfs->reserve_blocks);
607 * For those structures that have changed, we need to
610 if (FS_BLOCK_BM(old_fs, i) !=
611 (blk = FS_BLOCK_BM(fs, i))) {
612 ext2fs_mark_block_bitmap(fs->block_map, blk);
613 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
614 !ext2fs_test_block_bitmap(meta_bmap, blk))
615 ext2fs_mark_block_bitmap(rfs->move_blocks,
618 if (FS_INODE_BM(old_fs, i) !=
619 (blk = FS_INODE_BM(fs, i))) {
620 ext2fs_mark_block_bitmap(fs->block_map, blk);
621 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
622 !ext2fs_test_block_bitmap(meta_bmap, blk))
623 ext2fs_mark_block_bitmap(rfs->move_blocks,
628 * The inode table, if we need to relocate it, is
629 * handled specially. We have to reserve the blocks
630 * for both the old and the new inode table, since we
631 * can't have the inode table be destroyed during the
632 * block relocation phase.
634 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
635 goto next_group; /* inode table not moved */
637 rfs->needed_blocks += fs->inode_blocks_per_group;
640 * Mark the new inode table as in use in the new block
641 * allocation bitmap, and move any blocks that might
644 for (blk = fs->group_desc[i].bg_inode_table, j=0;
645 j < fs->inode_blocks_per_group ; j++, blk++) {
646 ext2fs_mark_block_bitmap(fs->block_map, blk);
647 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
648 !ext2fs_test_block_bitmap(meta_bmap, blk))
649 ext2fs_mark_block_bitmap(rfs->move_blocks,
654 * Make sure the old inode table is reserved in the
655 * block reservation bitmap.
657 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
658 j < fs->inode_blocks_per_group ; j++, blk++)
659 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
662 group_blk += rfs->new_fs->super->s_blocks_per_group;
668 ext2fs_free_block_bitmap(meta_bmap);
674 * This helper function tries to allocate a new block. We try to
675 * avoid hitting the original group descriptor blocks at least at
676 * first, since we want to make it possible to recover from a badly
677 * aborted resize operation as much as possible.
679 * In the future, I may further modify this routine to balance out
680 * where we get the new blocks across the various block groups.
681 * Ideally we would allocate blocks that corresponded with the block
682 * group of the containing inode, and keep contiguous blocks
683 * together. However, this very difficult to do efficiently, since we
684 * don't have the necessary information up front.
688 #define DESPERATION 2
690 static void init_block_alloc(ext2_resize_t rfs)
692 rfs->alloc_state = AVOID_OLD;
693 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
696 static blk_t get_new_block(ext2_resize_t rfs)
698 ext2_filsys fs = rfs->new_fs;
701 if (rfs->new_blk >= fs->super->s_blocks_count) {
702 if (rfs->alloc_state == DESPERATION)
705 #ifdef RESIZE2FS_DEBUG
706 if (rfs->flags & RESIZE_DEBUG_BMOVE)
707 printf("Going into desperation "
708 "mode for block allocations\n");
710 rfs->alloc_state = DESPERATION;
711 rfs->new_blk = fs->super->s_first_data_block;
714 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
715 ext2fs_test_block_bitmap(rfs->reserve_blocks,
717 ((rfs->alloc_state == AVOID_OLD) &&
718 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
727 static errcode_t block_mover(ext2_resize_t rfs)
729 blk_t blk, old_blk, new_blk;
730 ext2_filsys fs = rfs->new_fs;
731 ext2_filsys old_fs = rfs->old_fs;
736 new_blk = fs->super->s_first_data_block;
737 if (!rfs->itable_buf) {
738 retval = ext2fs_get_mem(fs->blocksize *
739 fs->inode_blocks_per_group,
740 (void **) &rfs->itable_buf);
744 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
749 * The first step is to figure out where all of the blocks
753 init_block_alloc(rfs);
754 for (blk = old_fs->super->s_first_data_block;
755 blk < old_fs->super->s_blocks_count; blk++) {
756 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
758 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
761 new_blk = get_new_block(rfs);
766 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
767 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
777 * Step two is to actually move the blocks
779 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
780 if (retval) goto errout;
783 (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS, 0, to_move);
786 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
787 if (retval) goto errout;
790 #ifdef RESIZE2FS_DEBUG
791 if (rfs->flags & RESIZE_DEBUG_BMOVE)
792 printf("Moving %d blocks %u->%u\n", size,
797 if (c > fs->inode_blocks_per_group)
798 c = fs->inode_blocks_per_group;
799 retval = io_channel_read_blk(fs->io, old_blk, c,
801 if (retval) goto errout;
802 retval = io_channel_write_blk(fs->io, new_blk, c,
804 if (retval) goto errout;
810 io_channel_flush(fs->io);
811 (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
815 io_channel_flush(fs->io);
823 /* --------------------------------------------------------------------
825 * Resize processing, phase 3
827 * --------------------------------------------------------------------
831 struct process_block_struct {
834 struct ext2_inode * inode;
840 static int process_block(ext2_filsys fs, blk_t *block_nr,
841 int blockcnt, blk_t ref_block,
842 int ref_offset, void *priv_data)
844 struct process_block_struct *pb;
846 blk_t block, new_block;
849 pb = (struct process_block_struct *) priv_data;
852 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
854 *block_nr = new_block;
855 ret |= BLOCK_CHANGED;
857 #ifdef RESIZE2FS_DEBUG
858 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
859 printf("ino=%ld, blockcnt=%d, %u->%u\n",
860 pb->ino, blockcnt, block, new_block);
866 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
879 static errcode_t progress_callback(ext2_filsys fs, ext2_inode_scan scan,
880 dgrp_t group, void * priv_data)
882 ext2_resize_t rfs = (ext2_resize_t) priv_data;
885 * This check is to protect against old ext2 libraries. It
886 * shouldn't be needed against new libraries.
892 io_channel_flush(fs->io);
893 (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
894 group+1, fs->group_desc_count);
900 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
902 struct process_block_struct pb;
903 ino_t ino, new_inode;
904 struct ext2_inode inode;
905 ext2_inode_scan scan = NULL;
911 if ((rfs->old_fs->group_desc_count <=
912 rfs->new_fs->group_desc_count) &&
916 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
917 if (retval) goto errout;
919 retval = ext2fs_init_dblist(rfs->old_fs, 0);
920 if (retval) goto errout;
921 retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3,
922 (void **) &block_buf);
923 if (retval) goto errout;
925 start_to_move = (rfs->new_fs->group_desc_count *
926 rfs->new_fs->super->s_inodes_per_group);
929 (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
930 0, rfs->old_fs->group_desc_count);
932 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
936 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
938 * First, copy all of the inodes that need to be moved
939 * elsewhere in the inode table
942 retval = ext2fs_get_next_inode(scan, &ino, &inode);
943 if (retval) goto errout;
947 if (inode.i_links_count == 0)
948 continue; /* inode not in use */
950 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
953 if (ext2fs_inode_has_valid_blocks(&inode) &&
954 (rfs->bmap || pb.is_dir)) {
956 retval = ext2fs_block_iterate2(rfs->old_fs,
967 if (ino <= start_to_move)
968 continue; /* Don't need to move it. */
974 if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
978 if (new_inode > rfs->new_fs->super->s_inodes_count) {
983 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
985 /* Get the new version of the inode */
986 retval = ext2fs_read_inode(rfs->old_fs, ino, &inode);
987 if (retval) goto errout;
989 retval = ext2fs_write_inode(rfs->old_fs, new_inode, &inode);
990 if (retval) goto errout;
992 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
993 if (LINUX_S_ISDIR(inode.i_mode))
994 rfs->new_fs->group_desc[group].bg_used_dirs_count++;
996 #ifdef RESIZE2FS_DEBUG
997 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
998 printf("Inode moved %ld->%ld\n", ino, new_inode);
1001 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1005 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1007 io_channel_flush(rfs->old_fs->io);
1011 ext2fs_free_extent_table(rfs->bmap);
1015 ext2fs_close_inode_scan(scan);
1017 ext2fs_free_mem((void **) &block_buf);
1021 /* --------------------------------------------------------------------
1023 * Resize processing, phase 4.
1025 * --------------------------------------------------------------------
1034 static int check_and_change_inodes(ino_t dir, int entry,
1035 struct ext2_dir_entry *dirent, int offset,
1036 int blocksize, char *buf, void *priv_data)
1038 struct istruct *is = (struct istruct *) priv_data;
1041 if (is->rfs->progress && offset == 0) {
1042 io_channel_flush(is->rfs->old_fs->io);
1043 (is->rfs->progress)(is->rfs, E2_RSZ_INODE_REF_UPD_PASS,
1044 ++is->num, is->max);
1050 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1054 #ifdef RESIZE2FS_DEBUG
1055 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1056 printf("Inode translate (dir=%ld, name=%.*s, %u->%ld)\n",
1057 dir, dirent->name_len, dirent->name,
1058 dirent->inode, new_inode);
1061 dirent->inode = new_inode;
1063 return DIRENT_CHANGED;
1066 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1075 * Now, we iterate over all of the directories to update the
1079 is.max = ext2fs_dblist_count(rfs->old_fs->dblist);
1083 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1086 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1087 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1088 check_and_change_inodes, &is);
1090 ext2fs_free_extent_table(rfs->imap);
1096 /* --------------------------------------------------------------------
1098 * Resize processing, phase 5.
1100 * In this phase we actually move the inode table around, and then
1101 * update the summary statistics. This is scary, since aborting here
1102 * will potentially scramble the filesystem. (We are moving the
1103 * inode tables around in place, and so the potential for lost data,
1104 * or at the very least scrambling the mapping between filenames and
1105 * inode numbers is very high in case of a power failure here.)
1106 * --------------------------------------------------------------------
1111 * A very scary routine --- this one moves the inode table around!!!
1113 * After this you have to use the rfs->new_fs file handle to read and
1116 static errcode_t move_itables(ext2_resize_t rfs)
1118 int i, n, num, max, size, diff;
1119 ext2_filsys fs = rfs->new_fs;
1121 blk_t old_blk, new_blk;
1125 max = fs->group_desc_count;
1126 if (max > rfs->old_fs->group_desc_count)
1127 max = rfs->old_fs->group_desc_count;
1129 size = fs->blocksize * fs->inode_blocks_per_group;
1130 if (!rfs->itable_buf) {
1131 retval = ext2fs_get_mem(size, (void **) &rfs->itable_buf);
1137 * Figure out how many inode tables we need to move
1139 to_move = moved = 0;
1140 for (i=0; i < max; i++)
1141 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1142 fs->group_desc[i].bg_inode_table)
1149 rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS, 0, to_move);
1151 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1153 for (i=0; i < max; i++) {
1154 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1155 new_blk = fs->group_desc[i].bg_inode_table;
1156 diff = new_blk - old_blk;
1158 #ifdef RESIZE2FS_DEBUG
1159 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1160 printf("Itable move group %d block "
1161 "%u->%u (diff %d)\n",
1162 i, old_blk, new_blk, diff);
1168 retval = io_channel_read_blk(fs->io, old_blk,
1169 fs->inode_blocks_per_group,
1174 * The end of the inode table segment often contains
1175 * all zeros, and we're often only moving the inode
1176 * table down a block or two. If so, we can optimize
1177 * things by not rewriting blocks that we know to be zero
1180 for (cp = rfs->itable_buf+size, n=0; n < size; n++, cp--)
1183 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1184 #ifdef RESIZE2FS_DEBUG
1185 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1186 printf("%d blocks of zeros...\n", n);
1188 num = fs->inode_blocks_per_group;
1192 retval = io_channel_write_blk(fs->io, new_blk,
1193 num, rfs->itable_buf);
1195 io_channel_write_blk(fs->io, old_blk,
1196 num, rfs->itable_buf);
1200 retval = io_channel_write_blk(fs->io,
1201 old_blk + fs->inode_blocks_per_group,
1202 diff, (rfs->itable_buf +
1203 (fs->inode_blocks_per_group - diff) *
1208 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1209 ext2fs_mark_super_dirty(rfs->old_fs);
1210 if (rfs->progress) {
1211 ext2fs_flush(rfs->old_fs);
1212 rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1217 #ifdef RESIZE2FS_DEBUG
1218 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1219 printf("Inode table move finished.\n");
1228 * Finally, recalculate the summary information
1230 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1240 * First calculate the block statistics
1242 for (blk = fs->super->s_first_data_block;
1243 blk < fs->super->s_blocks_count; blk++) {
1244 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1249 if ((count == fs->super->s_blocks_per_group) ||
1250 (blk == fs->super->s_blocks_count-1)) {
1251 fs->group_desc[group++].bg_free_blocks_count =
1257 fs->super->s_free_blocks_count = total_free;
1260 * Next, calculate the inode statistics
1266 for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
1267 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1272 if ((count == fs->super->s_inodes_per_group) ||
1273 (ino == fs->super->s_inodes_count)) {
1274 fs->group_desc[group++].bg_free_inodes_count =
1280 fs->super->s_free_inodes_count = total_free;
1281 ext2fs_mark_super_dirty(fs);