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);
794 ext2fs_free_extent_table(rfs->bmap);
802 * Step two is to actually move the blocks
804 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
805 if (retval) goto errout;
808 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
814 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
815 if (retval) goto errout;
818 #ifdef RESIZE2FS_DEBUG
819 if (rfs->flags & RESIZE_DEBUG_BMOVE)
820 printf(_("Moving %d blocks %u->%u\n"), size,
825 if (c > fs->inode_blocks_per_group)
826 c = fs->inode_blocks_per_group;
827 retval = io_channel_read_blk(fs->io, old_blk, c,
829 if (retval) goto errout;
830 retval = io_channel_write_blk(fs->io, new_blk, c,
832 if (retval) goto errout;
838 io_channel_flush(fs->io);
839 retval = (rfs->progress)(rfs,
840 E2_RSZ_BLOCK_RELOC_PASS,
846 io_channel_flush(fs->io);
854 /* --------------------------------------------------------------------
856 * Resize processing, phase 3
858 * --------------------------------------------------------------------
862 struct process_block_struct {
865 struct ext2_inode * inode;
871 static int process_block(ext2_filsys fs, blk_t *block_nr,
872 e2_blkcnt_t blockcnt, blk_t ref_block,
873 int ref_offset, void *priv_data)
875 struct process_block_struct *pb;
877 blk_t block, new_block;
880 pb = (struct process_block_struct *) priv_data;
883 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
885 *block_nr = new_block;
886 ret |= BLOCK_CHANGED;
888 #ifdef RESIZE2FS_DEBUG
889 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
890 printf(_("ino=%u, blockcnt=%lld, %u->%u\n"),
891 pb->ino, blockcnt, block, new_block);
897 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
898 block, (int) blockcnt);
910 static errcode_t progress_callback(ext2_filsys fs, ext2_inode_scan scan,
911 dgrp_t group, void * priv_data)
913 ext2_resize_t rfs = (ext2_resize_t) priv_data;
917 * This check is to protect against old ext2 libraries. It
918 * shouldn't be needed against new libraries.
924 io_channel_flush(fs->io);
925 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
926 group+1, fs->group_desc_count);
934 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
936 struct process_block_struct pb;
937 ext2_ino_t ino, new_inode;
938 struct ext2_inode inode;
939 ext2_inode_scan scan = NULL;
943 ext2_ino_t start_to_move;
946 if ((rfs->old_fs->group_desc_count <=
947 rfs->new_fs->group_desc_count) &&
952 * Save the original size of the old filesystem, and
953 * temporarily set the size to be the new size if the new size
954 * is larger. We need to do this to avoid catching an error
955 * by the block iterator routines
957 orig_size = rfs->old_fs->super->s_blocks_count;
958 if (orig_size < rfs->new_fs->super->s_blocks_count)
959 rfs->old_fs->super->s_blocks_count =
960 rfs->new_fs->super->s_blocks_count;
962 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
963 if (retval) goto errout;
965 retval = ext2fs_init_dblist(rfs->old_fs, 0);
966 if (retval) goto errout;
967 retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3,
968 (void **) &block_buf);
969 if (retval) goto errout;
971 start_to_move = (rfs->new_fs->group_desc_count *
972 rfs->new_fs->super->s_inodes_per_group);
975 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
976 0, rfs->old_fs->group_desc_count);
980 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
984 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
986 * First, copy all of the inodes that need to be moved
987 * elsewhere in the inode table
990 retval = ext2fs_get_next_inode(scan, &ino, &inode);
991 if (retval) goto errout;
995 if (inode.i_links_count == 0)
996 continue; /* inode not in use */
998 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
1001 if (ext2fs_inode_has_valid_blocks(&inode) &&
1002 (rfs->bmap || pb.is_dir)) {
1004 retval = ext2fs_block_iterate2(rfs->old_fs,
1006 process_block, &pb);
1015 if (ino <= start_to_move)
1016 continue; /* Don't need to move it. */
1022 if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
1026 if (new_inode > rfs->new_fs->super->s_inodes_count) {
1031 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1033 /* Get the new version of the inode */
1034 retval = ext2fs_read_inode(rfs->old_fs, ino, &inode);
1035 if (retval) goto errout;
1037 retval = ext2fs_write_inode(rfs->old_fs, new_inode, &inode);
1038 if (retval) goto errout;
1040 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1041 if (LINUX_S_ISDIR(inode.i_mode))
1042 rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1044 #ifdef RESIZE2FS_DEBUG
1045 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1046 printf(_("Inode moved %u->%u\n"), ino, new_inode);
1049 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1053 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1055 io_channel_flush(rfs->old_fs->io);
1058 rfs->old_fs->super->s_blocks_count = orig_size;
1060 ext2fs_free_extent_table(rfs->bmap);
1064 ext2fs_close_inode_scan(scan);
1066 ext2fs_free_mem((void **) &block_buf);
1070 /* --------------------------------------------------------------------
1072 * Resize processing, phase 4.
1074 * --------------------------------------------------------------------
1080 unsigned long max_dirs;
1084 static int check_and_change_inodes(ext2_ino_t dir, int entry,
1085 struct ext2_dir_entry *dirent, int offset,
1086 int blocksize, char *buf, void *priv_data)
1088 struct istruct *is = (struct istruct *) priv_data;
1089 ext2_ino_t new_inode;
1091 if (is->rfs->progress && offset == 0) {
1092 io_channel_flush(is->rfs->old_fs->io);
1093 is->err = (is->rfs->progress)(is->rfs,
1094 E2_RSZ_INODE_REF_UPD_PASS,
1095 ++is->num, is->max_dirs);
1097 return DIRENT_ABORT;
1103 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1107 #ifdef RESIZE2FS_DEBUG
1108 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1109 printf(_("Inode translate (dir=%u, name=%.*s, %u->%u)\n"),
1110 dir, dirent->name_len, dirent->name,
1111 dirent->inode, new_inode);
1114 dirent->inode = new_inode;
1116 return DIRENT_CHANGED;
1119 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1128 * Now, we iterate over all of the directories to update the
1132 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1136 if (rfs->progress) {
1137 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1143 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1144 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1145 check_and_change_inodes, &is);
1154 ext2fs_free_extent_table(rfs->imap);
1160 /* --------------------------------------------------------------------
1162 * Resize processing, phase 5.
1164 * In this phase we actually move the inode table around, and then
1165 * update the summary statistics. This is scary, since aborting here
1166 * will potentially scramble the filesystem. (We are moving the
1167 * inode tables around in place, and so the potential for lost data,
1168 * or at the very least scrambling the mapping between filenames and
1169 * inode numbers is very high in case of a power failure here.)
1170 * --------------------------------------------------------------------
1175 * A very scary routine --- this one moves the inode table around!!!
1177 * After this you have to use the rfs->new_fs file handle to read and
1180 static errcode_t move_itables(ext2_resize_t rfs)
1182 int i, n, num, max_groups, size, diff;
1183 ext2_filsys fs = rfs->new_fs;
1185 blk_t old_blk, new_blk;
1189 max_groups = fs->group_desc_count;
1190 if (max_groups > rfs->old_fs->group_desc_count)
1191 max_groups = rfs->old_fs->group_desc_count;
1193 size = fs->blocksize * fs->inode_blocks_per_group;
1194 if (!rfs->itable_buf) {
1195 retval = ext2fs_get_mem(size, (void **) &rfs->itable_buf);
1201 * Figure out how many inode tables we need to move
1203 to_move = moved = 0;
1204 for (i=0; i < max_groups; i++)
1205 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1206 fs->group_desc[i].bg_inode_table)
1212 if (rfs->progress) {
1213 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1219 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1221 for (i=0; i < max_groups; i++) {
1222 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1223 new_blk = fs->group_desc[i].bg_inode_table;
1224 diff = new_blk - old_blk;
1226 #ifdef RESIZE2FS_DEBUG
1227 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1228 printf(_("Itable move group %d block "
1229 "%u->%u (diff %d)\n"),
1230 i, old_blk, new_blk, diff);
1236 retval = io_channel_read_blk(fs->io, old_blk,
1237 fs->inode_blocks_per_group,
1242 * The end of the inode table segment often contains
1243 * all zeros, and we're often only moving the inode
1244 * table down a block or two. If so, we can optimize
1245 * things by not rewriting blocks that we know to be zero
1248 for (cp = rfs->itable_buf+size, n=0; n < size; n++, cp--)
1251 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1252 #ifdef RESIZE2FS_DEBUG
1253 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1254 printf(_("%d blocks of zeros...\n"), n);
1256 num = fs->inode_blocks_per_group;
1260 retval = io_channel_write_blk(fs->io, new_blk,
1261 num, rfs->itable_buf);
1263 io_channel_write_blk(fs->io, old_blk,
1264 num, rfs->itable_buf);
1268 retval = io_channel_write_blk(fs->io,
1269 old_blk + fs->inode_blocks_per_group,
1270 diff, (rfs->itable_buf +
1271 (fs->inode_blocks_per_group - diff) *
1276 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1277 ext2fs_mark_super_dirty(rfs->old_fs);
1278 if (rfs->progress) {
1279 ext2fs_flush(rfs->old_fs);
1280 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1287 #ifdef RESIZE2FS_DEBUG
1288 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1289 printf(_("Inode table move finished.\n"));
1298 * Finally, recalculate the summary information
1300 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1310 * First calculate the block statistics
1312 for (blk = fs->super->s_first_data_block;
1313 blk < fs->super->s_blocks_count; blk++) {
1314 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1319 if ((count == fs->super->s_blocks_per_group) ||
1320 (blk == fs->super->s_blocks_count-1)) {
1321 fs->group_desc[group++].bg_free_blocks_count =
1327 fs->super->s_free_blocks_count = total_free;
1330 * Next, calculate the inode statistics
1336 for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
1337 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1342 if ((count == fs->super->s_inodes_per_group) ||
1343 (ino == fs->super->s_inodes_count)) {
1344 fs->group_desc[group++].bg_free_inodes_count =
1350 fs->super->s_free_inodes_count = total_free;
1351 ext2fs_mark_super_dirty(fs);