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"
39 #ifdef __linux__ /* Kludge for debugging */
40 #define RESIZE2FS_DEBUG
43 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size);
44 static errcode_t blocks_to_move(ext2_resize_t rfs);
45 static errcode_t block_mover(ext2_resize_t rfs);
46 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
47 static errcode_t inode_ref_fix(ext2_resize_t rfs);
48 static errcode_t move_itables(ext2_resize_t rfs);
49 static errcode_t fix_resize_inode(ext2_filsys fs);
50 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
53 * Some helper CPP macros
55 #define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap)
56 #define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap)
57 #define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table)
59 #define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i)))
60 #define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i)))
62 #define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \
63 ((blk) < (FS_INODE_TB((fs), (i)) + \
64 (fs)->inode_blocks_per_group)))
69 * This is the top-level routine which does the dirty deed....
71 errcode_t resize_fs(ext2_filsys fs, blk_t *new_size, int flags,
72 errcode_t (*progress)(ext2_resize_t rfs, int pass,
74 unsigned long max_val))
79 retval = ext2fs_read_bitmaps(fs);
84 * Create the data structure
86 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
89 memset(rfs, 0, sizeof(struct ext2_resize_struct));
94 rfs->progress = progress;
95 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
99 retval = adjust_superblock(rfs, *new_size);
103 *new_size = rfs->new_fs->super->s_blocks_count;
105 retval = blocks_to_move(rfs);
109 #ifdef RESIZE2FS_DEBUG
110 if (rfs->flags & RESIZE_DEBUG_BMOVE)
111 printf("Number of free blocks: %d/%d, Needed: %d\n",
112 rfs->old_fs->super->s_free_blocks_count,
113 rfs->new_fs->super->s_free_blocks_count,
117 retval = block_mover(rfs);
121 retval = inode_scan_and_fix(rfs);
125 retval = inode_ref_fix(rfs);
129 retval = move_itables(rfs);
133 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
137 retval = fix_resize_inode(rfs->new_fs);
141 retval = ext2fs_close(rfs->new_fs);
147 ext2fs_free(rfs->old_fs);
149 ext2fs_free_mem(&rfs->itable_buf);
150 ext2fs_free_mem(&rfs);
156 ext2fs_free(rfs->new_fs);
158 ext2fs_free_mem(&rfs->itable_buf);
159 ext2fs_free_mem(&rfs);
163 /* --------------------------------------------------------------------
165 * Resize processing, phase 1.
167 * In this phase we adjust the in-memory superblock information, and
168 * initialize any new parts of the inode table. The new parts of the
169 * inode table are created in virgin disk space, so we can abort here
170 * without any side effects.
171 * --------------------------------------------------------------------
175 * This routine adjusts the superblock and other data structures...
177 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
184 blk_t blk, group_block;
185 unsigned long i, j, old_desc_blocks;
186 int old_numblocks, numblocks, adjblocks;
187 unsigned int meta_bg, meta_bg_size;
189 unsigned long max_group;
192 fs->super->s_blocks_count = new_size;
193 ext2fs_mark_super_dirty(fs);
194 ext2fs_mark_bb_dirty(fs);
195 ext2fs_mark_ib_dirty(fs);
198 fs->group_desc_count = (fs->super->s_blocks_count -
199 fs->super->s_first_data_block +
200 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
201 / EXT2_BLOCKS_PER_GROUP(fs->super);
202 if (fs->group_desc_count == 0)
203 return EXT2_ET_TOOSMALL;
204 fs->desc_blocks = (fs->group_desc_count +
205 EXT2_DESC_PER_BLOCK(fs->super) - 1)
206 / EXT2_DESC_PER_BLOCK(fs->super);
209 * Overhead is the number of bookkeeping blocks per group. It
210 * includes the superblock backup, the group descriptor
211 * backups, the inode bitmap, the block bitmap, and the inode
214 overhead = (int) (2 + fs->inode_blocks_per_group);
216 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
217 overhead += 1 + fs->desc_blocks +
218 fs->super->s_reserved_gdt_blocks;
221 * See if the last group is big enough to support the
222 * necessary data structures. If not, we need to get rid of
225 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
226 fs->super->s_blocks_per_group;
227 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
228 return EXT2_ET_TOOSMALL;
229 if (rem && (rem < overhead+50)) {
230 fs->super->s_blocks_count -= rem;
234 * Adjust the number of inodes
236 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
237 fs->group_desc_count;
240 * Adjust the number of free blocks
242 blk = rfs->old_fs->super->s_blocks_count;
243 if (blk > fs->super->s_blocks_count)
244 fs->super->s_free_blocks_count -=
245 (blk - fs->super->s_blocks_count);
247 fs->super->s_free_blocks_count +=
248 (fs->super->s_blocks_count - blk);
251 * Adjust the number of reserved blocks
253 blk = rfs->old_fs->super->s_r_blocks_count * 100 /
254 rfs->old_fs->super->s_blocks_count;
255 fs->super->s_r_blocks_count = ((fs->super->s_blocks_count * blk)
259 * Adjust the bitmaps for size
261 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
262 fs->super->s_inodes_count,
264 if (retval) goto errout;
266 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
267 * fs->group_desc_count)) - 1 +
268 fs->super->s_first_data_block;
269 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
270 real_end, fs->block_map);
272 if (retval) goto errout;
275 * Reallocate the group descriptors as necessary.
277 if (rfs->old_fs->desc_blocks != fs->desc_blocks) {
278 retval = ext2fs_resize_mem(rfs->old_fs->desc_blocks *
280 fs->desc_blocks * fs->blocksize,
284 if (fs->desc_blocks > rfs->old_fs->desc_blocks)
285 memset((char *) fs->group_desc +
286 (rfs->old_fs->desc_blocks * fs->blocksize), 0,
287 (fs->desc_blocks - rfs->old_fs->desc_blocks) *
292 * Check to make sure there are enough inodes
294 if ((rfs->old_fs->super->s_inodes_count -
295 rfs->old_fs->super->s_free_inodes_count) >
296 rfs->new_fs->super->s_inodes_count) {
302 * If the resize_inode feature is set, and we are changing the
303 * number of descriptor blocks, then adjust
304 * s_reserved_gdt_blocks if possible to avoid needing to move
305 * the inode table either now or in the future.
307 if ((fs->super->s_feature_compat &
308 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
309 (rfs->old_fs->desc_blocks != fs->desc_blocks)) {
312 new = ((int) fs->super->s_reserved_gdt_blocks) +
313 (rfs->old_fs->desc_blocks - fs->desc_blocks);
316 if (new > fs->blocksize/4)
317 new = fs->blocksize/4;
318 fs->super->s_reserved_gdt_blocks = new;
320 fs->super->s_feature_compat &=
321 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
325 * If we are shrinking the number block groups, we're done and
328 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
333 * Fix the count of the last (old) block group
335 old_numblocks = (rfs->old_fs->super->s_blocks_count -
336 rfs->old_fs->super->s_first_data_block) %
337 rfs->old_fs->super->s_blocks_per_group;
339 old_numblocks = rfs->old_fs->super->s_blocks_per_group;
340 if (rfs->old_fs->group_desc_count == fs->group_desc_count) {
341 numblocks = (rfs->new_fs->super->s_blocks_count -
342 rfs->new_fs->super->s_first_data_block) %
343 rfs->new_fs->super->s_blocks_per_group;
345 numblocks = rfs->new_fs->super->s_blocks_per_group;
347 numblocks = rfs->new_fs->super->s_blocks_per_group;
348 i = rfs->old_fs->group_desc_count - 1;
349 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
352 * If the number of block groups is staying the same, we're
353 * done and can exit now. (If the number block groups is
354 * shrinking, we had exited earlier.)
356 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
361 * Initialize the new block group descriptors
363 retval = ext2fs_get_mem(fs->blocksize * fs->inode_blocks_per_group,
368 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
369 group_block = fs->super->s_first_data_block +
370 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
372 adj = rfs->old_fs->group_desc_count;
373 max_group = fs->group_desc_count - adj;
375 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
380 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
381 old_desc_blocks = fs->super->s_first_meta_bg;
383 old_desc_blocks = fs->desc_blocks +
384 fs->super->s_reserved_gdt_blocks;
385 for (i = rfs->old_fs->group_desc_count;
386 i < fs->group_desc_count; i++) {
387 memset(&fs->group_desc[i], 0,
388 sizeof(struct ext2_group_desc));
391 if (i == fs->group_desc_count-1) {
392 numblocks = (fs->super->s_blocks_count -
393 fs->super->s_first_data_block) %
394 fs->super->s_blocks_per_group;
396 numblocks = fs->super->s_blocks_per_group;
398 numblocks = fs->super->s_blocks_per_group;
400 has_super = ext2fs_bg_has_super(fs, i);
402 ext2fs_mark_block_bitmap(fs->block_map, group_block);
405 meta_bg_size = (fs->blocksize /
406 sizeof (struct ext2_group_desc));
407 meta_bg = i / meta_bg_size;
408 if (!(fs->super->s_feature_incompat &
409 EXT2_FEATURE_INCOMPAT_META_BG) ||
410 (meta_bg < fs->super->s_first_meta_bg)) {
412 for (j=0; j < old_desc_blocks; j++)
413 ext2fs_mark_block_bitmap(fs->block_map,
414 group_block + 1 + j);
415 adjblocks += old_desc_blocks;
420 if (((i % meta_bg_size) == 0) ||
421 ((i % meta_bg_size) == 1) ||
422 ((i % meta_bg_size) == (meta_bg_size-1)))
423 ext2fs_mark_block_bitmap(fs->block_map,
424 group_block + has_super);
427 adjblocks += 2 + fs->inode_blocks_per_group;
429 numblocks -= adjblocks;
430 fs->super->s_free_blocks_count -= adjblocks;
431 fs->super->s_free_inodes_count +=
432 fs->super->s_inodes_per_group;
433 fs->group_desc[i].bg_free_blocks_count = numblocks;
434 fs->group_desc[i].bg_free_inodes_count =
435 fs->super->s_inodes_per_group;
436 fs->group_desc[i].bg_used_dirs_count = 0;
438 retval = ext2fs_allocate_group_table(fs, i, 0);
439 if (retval) goto errout;
442 * Write out the new inode table
444 retval = io_channel_write_blk(fs->io,
445 fs->group_desc[i].bg_inode_table,
446 fs->inode_blocks_per_group,
448 if (retval) goto errout;
450 io_channel_flush(fs->io);
452 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
453 i - adj + 1, max_group);
457 group_block += fs->super->s_blocks_per_group;
459 io_channel_flush(fs->io);
466 /* --------------------------------------------------------------------
468 * Resize processing, phase 2.
470 * In this phase we adjust determine which blocks need to be moved, in
471 * blocks_to_move(). We then copy the blocks to their ultimate new
472 * destinations using block_mover(). Since we are copying blocks to
473 * their new locations, again during this pass we can abort without
475 * --------------------------------------------------------------------
479 * This helper function creates a block bitmap with all of the
480 * filesystem meta-data blocks.
482 static errcode_t mark_table_blocks(ext2_filsys fs,
483 ext2fs_block_bitmap bmap)
488 unsigned long meta_bg_size;
489 unsigned int old_desc_blocks;
491 meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
492 block = fs->super->s_first_data_block;
493 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
494 old_desc_blocks = fs->super->s_first_meta_bg;
496 old_desc_blocks = fs->desc_blocks +
497 fs->super->s_reserved_gdt_blocks;
498 for (i = 0; i < fs->group_desc_count; i++) {
499 ext2fs_reserve_super_and_bgd(fs, i, bmap);
502 * Mark the blocks used for the inode table
504 for (j = 0, b = fs->group_desc[i].bg_inode_table;
505 j < (unsigned int) fs->inode_blocks_per_group;
507 ext2fs_mark_block_bitmap(bmap, b);
510 * Mark block used for the block bitmap
512 ext2fs_mark_block_bitmap(bmap,
513 fs->group_desc[i].bg_block_bitmap);
516 * Mark block used for the inode bitmap
518 ext2fs_mark_block_bitmap(bmap,
519 fs->group_desc[i].bg_inode_bitmap);
520 block += fs->super->s_blocks_per_group;
526 * This function checks to see if a particular block (either a
527 * superblock or a block group descriptor) overlaps with an inode or
528 * block bitmap block, or with the inode table.
530 static void mark_fs_metablock(ext2_resize_t rfs,
531 ext2fs_block_bitmap meta_bmap,
532 int group, blk_t blk)
534 ext2_filsys fs = rfs->new_fs;
536 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
537 ext2fs_mark_block_bitmap(fs->block_map, blk);
540 * Check to see if we overlap with the inode or block bitmap,
541 * or the inode tables. If not, and the block is in use, then
542 * mark it as a block to be moved.
544 if (IS_BLOCK_BM(fs, group, blk)) {
545 FS_BLOCK_BM(fs, group) = 0;
546 rfs->needed_blocks++;
547 } else if (IS_INODE_BM(fs, group, blk)) {
548 FS_INODE_BM(fs, group) = 0;
549 rfs->needed_blocks++;
550 } else if (IS_INODE_TB(fs, group, blk)) {
551 FS_INODE_TB(fs, group) = 0;
552 rfs->needed_blocks++;
553 } else if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk) &&
554 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
555 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
556 rfs->needed_blocks++;
562 * This routine marks and unmarks reserved blocks in the new block
563 * bitmap. It also determines which blocks need to be moved and
564 * places this information into the move_blocks bitmap.
566 static errcode_t blocks_to_move(ext2_resize_t rfs)
569 dgrp_t i, max_groups;
570 blk_t blk, group_blk;
571 unsigned long old_blocks, new_blocks;
572 unsigned int meta_bg, meta_bg_size;
574 ext2_filsys fs, old_fs;
575 ext2fs_block_bitmap meta_bmap;
578 old_fs = rfs->old_fs;
579 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
582 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
583 &rfs->reserve_blocks);
587 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
592 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
597 retval = mark_table_blocks(old_fs, meta_bmap);
604 * If we're shrinking the filesystem, we need to move all of
605 * the blocks that don't fit any more
607 for (blk = fs->super->s_blocks_count;
608 blk < old_fs->super->s_blocks_count; blk++) {
609 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
610 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
611 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
612 rfs->needed_blocks++;
614 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
617 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
618 old_blocks = old_fs->super->s_first_meta_bg;
619 new_blocks = fs->super->s_first_meta_bg;
621 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
622 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
625 if (old_blocks == new_blocks) {
630 max_groups = fs->group_desc_count;
631 if (max_groups > old_fs->group_desc_count)
632 max_groups = old_fs->group_desc_count;
633 group_blk = old_fs->super->s_first_data_block;
635 * If we're reducing the number of descriptor blocks, this
636 * makes life easy. :-) We just have to mark some extra
639 if (old_blocks > new_blocks) {
640 for (i = 0; i < max_groups; i++) {
641 if (!ext2fs_bg_has_super(fs, i)) {
642 group_blk += fs->super->s_blocks_per_group;
645 for (blk = group_blk+1+new_blocks;
646 blk < group_blk+1+old_blocks; blk++) {
647 ext2fs_unmark_block_bitmap(fs->block_map,
649 rfs->needed_blocks--;
651 group_blk += fs->super->s_blocks_per_group;
657 * If we're increasing the number of descriptor blocks, life
658 * gets interesting....
660 meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
661 for (i = 0; i < max_groups; i++) {
662 has_super = ext2fs_bg_has_super(fs, i);
664 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
666 meta_bg = i / meta_bg_size;
667 if (!(fs->super->s_feature_incompat &
668 EXT2_FEATURE_INCOMPAT_META_BG) ||
669 (meta_bg < fs->super->s_first_meta_bg)) {
671 for (blk = group_blk+1;
672 blk < group_blk + 1 + new_blocks; blk++)
673 mark_fs_metablock(rfs, meta_bmap,
679 if (((i % meta_bg_size) == 0) ||
680 ((i % meta_bg_size) == 1) ||
681 ((i % meta_bg_size) == (meta_bg_size-1)))
682 mark_fs_metablock(rfs, meta_bmap, i,
683 group_blk + has_super);
686 if (fs->group_desc[i].bg_inode_table &&
687 fs->group_desc[i].bg_inode_bitmap &&
688 fs->group_desc[i].bg_block_bitmap)
692 * Reserve the existing meta blocks that we know
693 * aren't to be moved.
695 if (fs->group_desc[i].bg_block_bitmap)
696 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
697 fs->group_desc[i].bg_block_bitmap);
698 if (fs->group_desc[i].bg_inode_bitmap)
699 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
700 fs->group_desc[i].bg_inode_bitmap);
701 if (fs->group_desc[i].bg_inode_table)
702 for (blk = fs->group_desc[i].bg_inode_table, j=0;
703 j < fs->inode_blocks_per_group ; j++, blk++)
704 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
708 * Allocate the missing data structures
710 retval = ext2fs_allocate_group_table(fs, i,
711 rfs->reserve_blocks);
716 * For those structures that have changed, we need to
719 if (FS_BLOCK_BM(old_fs, i) !=
720 (blk = FS_BLOCK_BM(fs, i))) {
721 ext2fs_mark_block_bitmap(fs->block_map, blk);
722 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
723 !ext2fs_test_block_bitmap(meta_bmap, blk))
724 ext2fs_mark_block_bitmap(rfs->move_blocks,
727 if (FS_INODE_BM(old_fs, i) !=
728 (blk = FS_INODE_BM(fs, i))) {
729 ext2fs_mark_block_bitmap(fs->block_map, blk);
730 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
731 !ext2fs_test_block_bitmap(meta_bmap, blk))
732 ext2fs_mark_block_bitmap(rfs->move_blocks,
737 * The inode table, if we need to relocate it, is
738 * handled specially. We have to reserve the blocks
739 * for both the old and the new inode table, since we
740 * can't have the inode table be destroyed during the
741 * block relocation phase.
743 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
744 goto next_group; /* inode table not moved */
746 rfs->needed_blocks += fs->inode_blocks_per_group;
749 * Mark the new inode table as in use in the new block
750 * allocation bitmap, and move any blocks that might
753 for (blk = fs->group_desc[i].bg_inode_table, j=0;
754 j < fs->inode_blocks_per_group ; j++, blk++) {
755 ext2fs_mark_block_bitmap(fs->block_map, blk);
756 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
757 !ext2fs_test_block_bitmap(meta_bmap, blk))
758 ext2fs_mark_block_bitmap(rfs->move_blocks,
763 * Make sure the old inode table is reserved in the
764 * block reservation bitmap.
766 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
767 j < fs->inode_blocks_per_group ; j++, blk++)
768 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
771 group_blk += rfs->new_fs->super->s_blocks_per_group;
777 ext2fs_free_block_bitmap(meta_bmap);
783 * This helper function tries to allocate a new block. We try to
784 * avoid hitting the original group descriptor blocks at least at
785 * first, since we want to make it possible to recover from a badly
786 * aborted resize operation as much as possible.
788 * In the future, I may further modify this routine to balance out
789 * where we get the new blocks across the various block groups.
790 * Ideally we would allocate blocks that corresponded with the block
791 * group of the containing inode, and keep contiguous blocks
792 * together. However, this very difficult to do efficiently, since we
793 * don't have the necessary information up front.
797 #define DESPERATION 2
799 static void init_block_alloc(ext2_resize_t rfs)
801 rfs->alloc_state = AVOID_OLD;
802 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
804 /* HACK for testing */
805 if (rfs->new_fs->super->s_blocks_count >
806 rfs->old_fs->super->s_blocks_count)
807 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
811 static blk_t get_new_block(ext2_resize_t rfs)
813 ext2_filsys fs = rfs->new_fs;
816 if (rfs->new_blk >= fs->super->s_blocks_count) {
817 if (rfs->alloc_state == DESPERATION)
820 #ifdef RESIZE2FS_DEBUG
821 if (rfs->flags & RESIZE_DEBUG_BMOVE)
822 printf("Going into desperation mode "
823 "for block allocations\n");
825 rfs->alloc_state = DESPERATION;
826 rfs->new_blk = fs->super->s_first_data_block;
829 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
830 ext2fs_test_block_bitmap(rfs->reserve_blocks,
832 ((rfs->alloc_state == AVOID_OLD) &&
833 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
834 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
843 static errcode_t block_mover(ext2_resize_t rfs)
845 blk_t blk, old_blk, new_blk;
846 ext2_filsys fs = rfs->new_fs;
847 ext2_filsys old_fs = rfs->old_fs;
851 ext2_badblocks_list badblock_list = 0;
854 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
858 new_blk = fs->super->s_first_data_block;
859 if (!rfs->itable_buf) {
860 retval = ext2fs_get_mem(fs->blocksize *
861 fs->inode_blocks_per_group,
866 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
871 * The first step is to figure out where all of the blocks
875 init_block_alloc(rfs);
876 for (blk = old_fs->super->s_first_data_block;
877 blk < old_fs->super->s_blocks_count; blk++) {
878 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
880 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
882 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
883 ext2fs_badblocks_list_del(badblock_list, blk);
888 new_blk = get_new_block(rfs);
893 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
894 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
900 ext2fs_free_extent_table(rfs->bmap);
908 * Step two is to actually move the blocks
910 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
911 if (retval) goto errout;
914 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
920 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
921 if (retval) goto errout;
924 #ifdef RESIZE2FS_DEBUG
925 if (rfs->flags & RESIZE_DEBUG_BMOVE)
926 printf("Moving %d blocks %u->%u\n",
927 size, old_blk, new_blk);
931 if (c > fs->inode_blocks_per_group)
932 c = fs->inode_blocks_per_group;
933 retval = io_channel_read_blk(fs->io, old_blk, c,
935 if (retval) goto errout;
936 retval = io_channel_write_blk(fs->io, new_blk, c,
938 if (retval) goto errout;
944 io_channel_flush(fs->io);
945 retval = (rfs->progress)(rfs,
946 E2_RSZ_BLOCK_RELOC_PASS,
952 io_channel_flush(fs->io);
957 if (!retval && bb_modified)
958 retval = ext2fs_update_bb_inode(old_fs,
960 ext2fs_badblocks_list_free(badblock_list);
966 /* --------------------------------------------------------------------
968 * Resize processing, phase 3
970 * --------------------------------------------------------------------
974 struct process_block_struct {
977 struct ext2_inode * inode;
983 static int process_block(ext2_filsys fs, blk_t *block_nr,
984 e2_blkcnt_t blockcnt,
985 blk_t ref_block EXT2FS_ATTR((unused)),
986 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
988 struct process_block_struct *pb;
990 blk_t block, new_block;
993 pb = (struct process_block_struct *) priv_data;
996 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
998 *block_nr = new_block;
999 ret |= BLOCK_CHANGED;
1001 #ifdef RESIZE2FS_DEBUG
1002 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1003 printf("ino=%u, blockcnt=%lld, %u->%u\n",
1004 pb->ino, blockcnt, block, new_block);
1010 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
1011 block, (int) blockcnt);
1023 static errcode_t progress_callback(ext2_filsys fs,
1024 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1025 dgrp_t group, void * priv_data)
1027 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1031 * This check is to protect against old ext2 libraries. It
1032 * shouldn't be needed against new libraries.
1037 if (rfs->progress) {
1038 io_channel_flush(fs->io);
1039 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1040 group+1, fs->group_desc_count);
1048 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1050 struct process_block_struct pb;
1051 ext2_ino_t ino, new_inode;
1052 struct ext2_inode inode;
1053 ext2_inode_scan scan = NULL;
1056 char *block_buf = 0;
1057 ext2_ino_t start_to_move;
1058 blk_t orig_size, new_block;
1060 if ((rfs->old_fs->group_desc_count <=
1061 rfs->new_fs->group_desc_count) &&
1066 * Save the original size of the old filesystem, and
1067 * temporarily set the size to be the new size if the new size
1068 * is larger. We need to do this to avoid catching an error
1069 * by the block iterator routines
1071 orig_size = rfs->old_fs->super->s_blocks_count;
1072 if (orig_size < rfs->new_fs->super->s_blocks_count)
1073 rfs->old_fs->super->s_blocks_count =
1074 rfs->new_fs->super->s_blocks_count;
1076 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1077 if (retval) goto errout;
1079 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1080 if (retval) goto errout;
1081 retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3, &block_buf);
1082 if (retval) goto errout;
1084 start_to_move = (rfs->new_fs->group_desc_count *
1085 rfs->new_fs->super->s_inodes_per_group);
1087 if (rfs->progress) {
1088 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1089 0, rfs->old_fs->group_desc_count);
1093 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1097 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1099 * First, copy all of the inodes that need to be moved
1100 * elsewhere in the inode table
1103 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1104 if (retval) goto errout;
1108 if (inode.i_links_count == 0 && ino != EXT2_RESIZE_INO)
1109 continue; /* inode not in use */
1111 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
1114 if (inode.i_file_acl && rfs->bmap) {
1115 new_block = ext2fs_extent_translate(rfs->bmap,
1118 inode.i_file_acl = new_block;
1119 retval = ext2fs_write_inode(rfs->old_fs,
1121 if (retval) goto errout;
1125 if (ext2fs_inode_has_valid_blocks(&inode) &&
1126 (rfs->bmap || pb.is_dir)) {
1128 retval = ext2fs_block_iterate2(rfs->old_fs,
1130 process_block, &pb);
1139 if (ino <= start_to_move)
1140 continue; /* Don't need to move it. */
1146 if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
1150 if (new_inode > rfs->new_fs->super->s_inodes_count) {
1155 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1157 /* Get the new version of the inode */
1158 retval = ext2fs_read_inode(rfs->old_fs, ino, &inode);
1159 if (retval) goto errout;
1161 inode.i_ctime = time(0);
1162 retval = ext2fs_write_inode(rfs->old_fs, new_inode, &inode);
1163 if (retval) goto errout;
1165 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1166 if (LINUX_S_ISDIR(inode.i_mode))
1167 rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1169 #ifdef RESIZE2FS_DEBUG
1170 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1171 printf("Inode moved %u->%u\n", ino, new_inode);
1174 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1178 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1180 io_channel_flush(rfs->old_fs->io);
1183 rfs->old_fs->super->s_blocks_count = orig_size;
1185 ext2fs_free_extent_table(rfs->bmap);
1189 ext2fs_close_inode_scan(scan);
1191 ext2fs_free_mem(&block_buf);
1195 /* --------------------------------------------------------------------
1197 * Resize processing, phase 4.
1199 * --------------------------------------------------------------------
1205 unsigned long max_dirs;
1209 static int check_and_change_inodes(ext2_ino_t dir,
1210 int entry EXT2FS_ATTR((unused)),
1211 struct ext2_dir_entry *dirent, int offset,
1212 int blocksize EXT2FS_ATTR((unused)),
1213 char *buf EXT2FS_ATTR((unused)),
1216 struct istruct *is = (struct istruct *) priv_data;
1217 struct ext2_inode inode;
1218 ext2_ino_t new_inode;
1221 if (is->rfs->progress && offset == 0) {
1222 io_channel_flush(is->rfs->old_fs->io);
1223 is->err = (is->rfs->progress)(is->rfs,
1224 E2_RSZ_INODE_REF_UPD_PASS,
1225 ++is->num, is->max_dirs);
1227 return DIRENT_ABORT;
1233 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1237 #ifdef RESIZE2FS_DEBUG
1238 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1239 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1240 dir, dirent->name_len&0xFF, dirent->name,
1241 dirent->inode, new_inode);
1244 dirent->inode = new_inode;
1246 /* Update the directory mtime and ctime */
1247 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1249 inode.i_mtime = inode.i_ctime = time(0);
1250 ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1253 return DIRENT_CHANGED;
1256 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1265 * Now, we iterate over all of the directories to update the
1269 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1273 if (rfs->progress) {
1274 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1280 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1281 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1282 check_and_change_inodes, &is);
1291 ext2fs_free_extent_table(rfs->imap);
1297 /* --------------------------------------------------------------------
1299 * Resize processing, phase 5.
1301 * In this phase we actually move the inode table around, and then
1302 * update the summary statistics. This is scary, since aborting here
1303 * will potentially scramble the filesystem. (We are moving the
1304 * inode tables around in place, and so the potential for lost data,
1305 * or at the very least scrambling the mapping between filenames and
1306 * inode numbers is very high in case of a power failure here.)
1307 * --------------------------------------------------------------------
1312 * A very scary routine --- this one moves the inode table around!!!
1314 * After this you have to use the rfs->new_fs file handle to read and
1317 static errcode_t move_itables(ext2_resize_t rfs)
1319 int n, num, size, diff;
1320 dgrp_t i, max_groups;
1321 ext2_filsys fs = rfs->new_fs;
1323 blk_t old_blk, new_blk, blk;
1325 int j, to_move, moved;
1327 max_groups = fs->group_desc_count;
1328 if (max_groups > rfs->old_fs->group_desc_count)
1329 max_groups = rfs->old_fs->group_desc_count;
1331 size = fs->blocksize * fs->inode_blocks_per_group;
1332 if (!rfs->itable_buf) {
1333 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1339 * Figure out how many inode tables we need to move
1341 to_move = moved = 0;
1342 for (i=0; i < max_groups; i++)
1343 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1344 fs->group_desc[i].bg_inode_table)
1350 if (rfs->progress) {
1351 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1357 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1359 for (i=0; i < max_groups; i++) {
1360 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1361 new_blk = fs->group_desc[i].bg_inode_table;
1362 diff = new_blk - old_blk;
1364 #ifdef RESIZE2FS_DEBUG
1365 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1366 printf("Itable move group %d block %u->%u (diff %d)\n",
1367 i, old_blk, new_blk, diff);
1373 retval = io_channel_read_blk(fs->io, old_blk,
1374 fs->inode_blocks_per_group,
1379 * The end of the inode table segment often contains
1380 * all zeros, and we're often only moving the inode
1381 * table down a block or two. If so, we can optimize
1382 * things by not rewriting blocks that we know to be zero
1385 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1388 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1389 #ifdef RESIZE2FS_DEBUG
1390 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1391 printf("%d blocks of zeros...\n", n);
1393 num = fs->inode_blocks_per_group;
1397 retval = io_channel_write_blk(fs->io, new_blk,
1398 num, rfs->itable_buf);
1400 io_channel_write_blk(fs->io, old_blk,
1401 num, rfs->itable_buf);
1405 retval = io_channel_write_blk(fs->io,
1406 old_blk + fs->inode_blocks_per_group,
1407 diff, (rfs->itable_buf +
1408 (fs->inode_blocks_per_group - diff) *
1414 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1415 j < fs->inode_blocks_per_group ; j++, blk++)
1416 ext2fs_unmark_block_bitmap(fs->block_map, blk);
1418 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1419 ext2fs_mark_super_dirty(rfs->old_fs);
1420 ext2fs_flush(rfs->old_fs);
1422 if (rfs->progress) {
1423 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1429 mark_table_blocks(fs, fs->block_map);
1431 #ifdef RESIZE2FS_DEBUG
1432 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1433 printf("Inode table move finished.\n");
1442 * Fix the resize inode
1444 static errcode_t fix_resize_inode(ext2_filsys fs)
1446 struct ext2_inode inode;
1450 if (!(fs->super->s_feature_compat &
1451 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1454 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1455 if (retval) goto errout;
1457 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1458 if (retval) goto errout;
1460 inode.i_blocks = fs->blocksize/512;
1462 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1463 if (retval) goto errout;
1465 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1467 * Avoid zeroing out block #0; that's rude. This
1468 * should never happen anyway since the filesystem
1469 * should be fsck'ed and we assume it is consistent.
1472 _("Should never happen: resize inode corrupt!\n"));
1476 memset(block_buf, 0, fs->blocksize);
1478 retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1480 if (retval) goto errout;
1482 retval = ext2fs_create_resize_inode(fs);
1488 ext2fs_free_mem(&block_buf);
1493 * Finally, recalculate the summary information
1495 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1499 unsigned int group = 0;
1500 unsigned int count = 0;
1505 * First calculate the block statistics
1507 for (blk = fs->super->s_first_data_block;
1508 blk < fs->super->s_blocks_count; blk++) {
1509 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1514 if ((count == fs->super->s_blocks_per_group) ||
1515 (blk == fs->super->s_blocks_count-1)) {
1516 fs->group_desc[group++].bg_free_blocks_count =
1522 fs->super->s_free_blocks_count = total_free;
1525 * Next, calculate the inode statistics
1531 for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
1532 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1537 if ((count == fs->super->s_inodes_per_group) ||
1538 (ino == fs->super->s_inodes_count)) {
1539 fs->group_desc[group++].bg_free_inodes_count =
1545 fs->super->s_free_inodes_count = total_free;
1546 ext2fs_mark_super_dirty(fs);