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: %u/%u, 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 is shared by the online and offline resize routines.
176 * All of the information which is adjusted in memory is done here.
178 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs, blk_t new_size)
183 blk_t blk, group_block;
185 int adj, old_numblocks, numblocks, adjblocks;
186 unsigned long i, j, old_desc_blocks, max_group;
187 unsigned int meta_bg, meta_bg_size;
190 fs->super->s_blocks_count = new_size;
193 fs->group_desc_count = (fs->super->s_blocks_count -
194 fs->super->s_first_data_block +
195 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
196 / EXT2_BLOCKS_PER_GROUP(fs->super);
197 if (fs->group_desc_count == 0)
198 return EXT2_ET_TOOSMALL;
199 fs->desc_blocks = (fs->group_desc_count +
200 EXT2_DESC_PER_BLOCK(fs->super) - 1)
201 / EXT2_DESC_PER_BLOCK(fs->super);
204 * Overhead is the number of bookkeeping blocks per group. It
205 * includes the superblock backup, the group descriptor
206 * backups, the inode bitmap, the block bitmap, and the inode
209 overhead = (int) (2 + fs->inode_blocks_per_group);
211 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
212 overhead += 1 + fs->desc_blocks +
213 fs->super->s_reserved_gdt_blocks;
216 * See if the last group is big enough to support the
217 * necessary data structures. If not, we need to get rid of
220 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
221 fs->super->s_blocks_per_group;
222 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
223 return EXT2_ET_TOOSMALL;
224 if (rem && (rem < overhead+50)) {
225 fs->super->s_blocks_count -= rem;
229 * Adjust the number of inodes
231 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
232 fs->group_desc_count;
235 * Adjust the number of free blocks
237 blk = old_fs->super->s_blocks_count;
238 if (blk > fs->super->s_blocks_count)
239 fs->super->s_free_blocks_count -=
240 (blk - fs->super->s_blocks_count);
242 fs->super->s_free_blocks_count +=
243 (fs->super->s_blocks_count - blk);
246 * Adjust the number of reserved blocks
248 blk = old_fs->super->s_r_blocks_count * 100 /
249 old_fs->super->s_blocks_count;
250 fs->super->s_r_blocks_count = ((fs->super->s_blocks_count * blk)
254 * Adjust the bitmaps for size
256 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
257 fs->super->s_inodes_count,
259 if (retval) goto errout;
261 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
262 * fs->group_desc_count)) - 1 +
263 fs->super->s_first_data_block;
264 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
265 real_end, fs->block_map);
267 if (retval) goto errout;
270 * Reallocate the group descriptors as necessary.
272 if (old_fs->desc_blocks != fs->desc_blocks) {
273 retval = ext2fs_resize_mem(old_fs->desc_blocks *
275 fs->desc_blocks * fs->blocksize,
279 if (fs->desc_blocks > old_fs->desc_blocks)
280 memset((char *) fs->group_desc +
281 (old_fs->desc_blocks * fs->blocksize), 0,
282 (fs->desc_blocks - old_fs->desc_blocks) *
287 * If the resize_inode feature is set, and we are changing the
288 * number of descriptor blocks, then adjust
289 * s_reserved_gdt_blocks if possible to avoid needing to move
290 * the inode table either now or in the future.
292 if ((fs->super->s_feature_compat &
293 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
294 (old_fs->desc_blocks != fs->desc_blocks)) {
297 new = ((int) fs->super->s_reserved_gdt_blocks) +
298 (old_fs->desc_blocks - fs->desc_blocks);
301 if (new > fs->blocksize/4)
302 new = fs->blocksize/4;
303 fs->super->s_reserved_gdt_blocks = new;
305 fs->super->s_feature_compat &=
306 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
310 * If we are shrinking the number block groups, we're done and
313 if (old_fs->group_desc_count > fs->group_desc_count) {
319 * Fix the count of the last (old) block group
321 old_numblocks = (old_fs->super->s_blocks_count -
322 old_fs->super->s_first_data_block) %
323 old_fs->super->s_blocks_per_group;
325 old_numblocks = old_fs->super->s_blocks_per_group;
326 if (old_fs->group_desc_count == fs->group_desc_count) {
327 numblocks = (fs->super->s_blocks_count -
328 fs->super->s_first_data_block) %
329 fs->super->s_blocks_per_group;
331 numblocks = fs->super->s_blocks_per_group;
333 numblocks = fs->super->s_blocks_per_group;
334 i = old_fs->group_desc_count - 1;
335 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
338 * If the number of block groups is staying the same, we're
339 * done and can exit now. (If the number block groups is
340 * shrinking, we had exited earlier.)
342 if (old_fs->group_desc_count >= fs->group_desc_count) {
348 * Initialize the new block group descriptors
350 group_block = fs->super->s_first_data_block +
351 old_fs->group_desc_count * fs->super->s_blocks_per_group;
353 adj = old_fs->group_desc_count;
354 max_group = fs->group_desc_count - adj;
355 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
356 old_desc_blocks = fs->super->s_first_meta_bg;
358 old_desc_blocks = fs->desc_blocks +
359 fs->super->s_reserved_gdt_blocks;
360 for (i = old_fs->group_desc_count;
361 i < fs->group_desc_count; i++) {
362 memset(&fs->group_desc[i], 0,
363 sizeof(struct ext2_group_desc));
366 if (i == fs->group_desc_count-1) {
367 numblocks = (fs->super->s_blocks_count -
368 fs->super->s_first_data_block) %
369 fs->super->s_blocks_per_group;
371 numblocks = fs->super->s_blocks_per_group;
373 numblocks = fs->super->s_blocks_per_group;
375 has_super = ext2fs_bg_has_super(fs, i);
377 ext2fs_mark_block_bitmap(fs->block_map, group_block);
380 meta_bg_size = (fs->blocksize /
381 sizeof (struct ext2_group_desc));
382 meta_bg = i / meta_bg_size;
383 if (!(fs->super->s_feature_incompat &
384 EXT2_FEATURE_INCOMPAT_META_BG) ||
385 (meta_bg < fs->super->s_first_meta_bg)) {
387 for (j=0; j < old_desc_blocks; j++)
388 ext2fs_mark_block_bitmap(fs->block_map,
389 group_block + 1 + j);
390 adjblocks += old_desc_blocks;
395 if (((i % meta_bg_size) == 0) ||
396 ((i % meta_bg_size) == 1) ||
397 ((i % meta_bg_size) == (meta_bg_size-1)))
398 ext2fs_mark_block_bitmap(fs->block_map,
399 group_block + has_super);
402 adjblocks += 2 + fs->inode_blocks_per_group;
404 numblocks -= adjblocks;
405 fs->super->s_free_blocks_count -= adjblocks;
406 fs->super->s_free_inodes_count +=
407 fs->super->s_inodes_per_group;
408 fs->group_desc[i].bg_free_blocks_count = numblocks;
409 fs->group_desc[i].bg_free_inodes_count =
410 fs->super->s_inodes_per_group;
411 fs->group_desc[i].bg_used_dirs_count = 0;
413 retval = ext2fs_allocate_group_table(fs, i, 0);
414 if (retval) goto errout;
416 group_block += fs->super->s_blocks_per_group;
425 * This routine adjusts the superblock and other data structures, both
426 * in disk as well as in memory...
428 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
435 unsigned long max_group;
438 ext2fs_mark_super_dirty(fs);
439 ext2fs_mark_bb_dirty(fs);
440 ext2fs_mark_ib_dirty(fs);
442 retval = adjust_fs_info(fs, rfs->old_fs, new_size);
447 * Check to make sure there are enough inodes
449 if ((rfs->old_fs->super->s_inodes_count -
450 rfs->old_fs->super->s_free_inodes_count) >
451 rfs->new_fs->super->s_inodes_count) {
457 * If we are shrinking the number block groups, we're done and
460 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
466 * If the number of block groups is staying the same, we're
467 * done and can exit now. (If the number block groups is
468 * shrinking, we had exited earlier.)
470 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
476 * Initialize the new block group descriptors
478 retval = ext2fs_get_mem(fs->blocksize * fs->inode_blocks_per_group,
483 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
484 group_block = fs->super->s_first_data_block +
485 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
487 adj = rfs->old_fs->group_desc_count;
488 max_group = fs->group_desc_count - adj;
490 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
495 for (i = rfs->old_fs->group_desc_count;
496 i < fs->group_desc_count; i++) {
498 * Write out the new inode table
500 retval = io_channel_write_blk(fs->io,
501 fs->group_desc[i].bg_inode_table,
502 fs->inode_blocks_per_group,
504 if (retval) goto errout;
506 io_channel_flush(fs->io);
508 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
509 i - adj + 1, max_group);
513 group_block += fs->super->s_blocks_per_group;
515 io_channel_flush(fs->io);
522 /* --------------------------------------------------------------------
524 * Resize processing, phase 2.
526 * In this phase we adjust determine which blocks need to be moved, in
527 * blocks_to_move(). We then copy the blocks to their ultimate new
528 * destinations using block_mover(). Since we are copying blocks to
529 * their new locations, again during this pass we can abort without
531 * --------------------------------------------------------------------
535 * This helper function creates a block bitmap with all of the
536 * filesystem meta-data blocks.
538 static errcode_t mark_table_blocks(ext2_filsys fs,
539 ext2fs_block_bitmap bmap)
544 unsigned long meta_bg_size;
545 unsigned int old_desc_blocks;
547 meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
548 block = fs->super->s_first_data_block;
549 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
550 old_desc_blocks = fs->super->s_first_meta_bg;
552 old_desc_blocks = fs->desc_blocks +
553 fs->super->s_reserved_gdt_blocks;
554 for (i = 0; i < fs->group_desc_count; i++) {
555 ext2fs_reserve_super_and_bgd(fs, i, bmap);
558 * Mark the blocks used for the inode table
560 for (j = 0, b = fs->group_desc[i].bg_inode_table;
561 j < (unsigned int) fs->inode_blocks_per_group;
563 ext2fs_mark_block_bitmap(bmap, b);
566 * Mark block used for the block bitmap
568 ext2fs_mark_block_bitmap(bmap,
569 fs->group_desc[i].bg_block_bitmap);
572 * Mark block used for the inode bitmap
574 ext2fs_mark_block_bitmap(bmap,
575 fs->group_desc[i].bg_inode_bitmap);
576 block += fs->super->s_blocks_per_group;
582 * This function checks to see if a particular block (either a
583 * superblock or a block group descriptor) overlaps with an inode or
584 * block bitmap block, or with the inode table.
586 static void mark_fs_metablock(ext2_resize_t rfs,
587 ext2fs_block_bitmap meta_bmap,
588 int group, blk_t blk)
590 ext2_filsys fs = rfs->new_fs;
592 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
593 ext2fs_mark_block_bitmap(fs->block_map, blk);
596 * Check to see if we overlap with the inode or block bitmap,
597 * or the inode tables. If not, and the block is in use, then
598 * mark it as a block to be moved.
600 if (IS_BLOCK_BM(fs, group, blk)) {
601 FS_BLOCK_BM(fs, group) = 0;
602 rfs->needed_blocks++;
603 } else if (IS_INODE_BM(fs, group, blk)) {
604 FS_INODE_BM(fs, group) = 0;
605 rfs->needed_blocks++;
606 } else if (IS_INODE_TB(fs, group, blk)) {
607 FS_INODE_TB(fs, group) = 0;
608 rfs->needed_blocks++;
609 } else if (ext2fs_test_block_bitmap(rfs->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++;
618 * This routine marks and unmarks reserved blocks in the new block
619 * bitmap. It also determines which blocks need to be moved and
620 * places this information into the move_blocks bitmap.
622 static errcode_t blocks_to_move(ext2_resize_t rfs)
625 dgrp_t i, max_groups;
626 blk_t blk, group_blk;
627 unsigned long old_blocks, new_blocks;
628 unsigned int meta_bg, meta_bg_size;
630 ext2_filsys fs, old_fs;
631 ext2fs_block_bitmap meta_bmap;
634 old_fs = rfs->old_fs;
635 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
638 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
639 &rfs->reserve_blocks);
643 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
648 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
653 retval = mark_table_blocks(old_fs, meta_bmap);
660 * If we're shrinking the filesystem, we need to move all of
661 * the blocks that don't fit any more
663 for (blk = fs->super->s_blocks_count;
664 blk < old_fs->super->s_blocks_count; blk++) {
665 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
666 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
667 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
668 rfs->needed_blocks++;
670 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
673 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
674 old_blocks = old_fs->super->s_first_meta_bg;
675 new_blocks = fs->super->s_first_meta_bg;
677 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
678 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
681 if (old_blocks == new_blocks) {
686 max_groups = fs->group_desc_count;
687 if (max_groups > old_fs->group_desc_count)
688 max_groups = old_fs->group_desc_count;
689 group_blk = old_fs->super->s_first_data_block;
691 * If we're reducing the number of descriptor blocks, this
692 * makes life easy. :-) We just have to mark some extra
695 if (old_blocks > new_blocks) {
696 for (i = 0; i < max_groups; i++) {
697 if (!ext2fs_bg_has_super(fs, i)) {
698 group_blk += fs->super->s_blocks_per_group;
701 for (blk = group_blk+1+new_blocks;
702 blk < group_blk+1+old_blocks; blk++) {
703 ext2fs_unmark_block_bitmap(fs->block_map,
705 rfs->needed_blocks--;
707 group_blk += fs->super->s_blocks_per_group;
713 * If we're increasing the number of descriptor blocks, life
714 * gets interesting....
716 meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
717 for (i = 0; i < max_groups; i++) {
718 has_super = ext2fs_bg_has_super(fs, i);
720 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
722 meta_bg = i / meta_bg_size;
723 if (!(fs->super->s_feature_incompat &
724 EXT2_FEATURE_INCOMPAT_META_BG) ||
725 (meta_bg < fs->super->s_first_meta_bg)) {
727 for (blk = group_blk+1;
728 blk < group_blk + 1 + new_blocks; blk++)
729 mark_fs_metablock(rfs, meta_bmap,
735 if (((i % meta_bg_size) == 0) ||
736 ((i % meta_bg_size) == 1) ||
737 ((i % meta_bg_size) == (meta_bg_size-1)))
738 mark_fs_metablock(rfs, meta_bmap, i,
739 group_blk + has_super);
742 if (fs->group_desc[i].bg_inode_table &&
743 fs->group_desc[i].bg_inode_bitmap &&
744 fs->group_desc[i].bg_block_bitmap)
748 * Reserve the existing meta blocks that we know
749 * aren't to be moved.
751 if (fs->group_desc[i].bg_block_bitmap)
752 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
753 fs->group_desc[i].bg_block_bitmap);
754 if (fs->group_desc[i].bg_inode_bitmap)
755 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
756 fs->group_desc[i].bg_inode_bitmap);
757 if (fs->group_desc[i].bg_inode_table)
758 for (blk = fs->group_desc[i].bg_inode_table, j=0;
759 j < fs->inode_blocks_per_group ; j++, blk++)
760 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
764 * Allocate the missing data structures
766 retval = ext2fs_allocate_group_table(fs, i,
767 rfs->reserve_blocks);
772 * For those structures that have changed, we need to
775 if (FS_BLOCK_BM(old_fs, i) !=
776 (blk = FS_BLOCK_BM(fs, i))) {
777 ext2fs_mark_block_bitmap(fs->block_map, blk);
778 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
779 !ext2fs_test_block_bitmap(meta_bmap, blk))
780 ext2fs_mark_block_bitmap(rfs->move_blocks,
783 if (FS_INODE_BM(old_fs, i) !=
784 (blk = FS_INODE_BM(fs, i))) {
785 ext2fs_mark_block_bitmap(fs->block_map, blk);
786 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
787 !ext2fs_test_block_bitmap(meta_bmap, blk))
788 ext2fs_mark_block_bitmap(rfs->move_blocks,
793 * The inode table, if we need to relocate it, is
794 * handled specially. We have to reserve the blocks
795 * for both the old and the new inode table, since we
796 * can't have the inode table be destroyed during the
797 * block relocation phase.
799 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
800 goto next_group; /* inode table not moved */
802 rfs->needed_blocks += fs->inode_blocks_per_group;
805 * Mark the new inode table as in use in the new block
806 * allocation bitmap, and move any blocks that might
809 for (blk = fs->group_desc[i].bg_inode_table, j=0;
810 j < fs->inode_blocks_per_group ; j++, blk++) {
811 ext2fs_mark_block_bitmap(fs->block_map, blk);
812 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
813 !ext2fs_test_block_bitmap(meta_bmap, blk))
814 ext2fs_mark_block_bitmap(rfs->move_blocks,
819 * Make sure the old inode table is reserved in the
820 * block reservation bitmap.
822 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
823 j < fs->inode_blocks_per_group ; j++, blk++)
824 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
827 group_blk += rfs->new_fs->super->s_blocks_per_group;
833 ext2fs_free_block_bitmap(meta_bmap);
839 * This helper function tries to allocate a new block. We try to
840 * avoid hitting the original group descriptor blocks at least at
841 * first, since we want to make it possible to recover from a badly
842 * aborted resize operation as much as possible.
844 * In the future, I may further modify this routine to balance out
845 * where we get the new blocks across the various block groups.
846 * Ideally we would allocate blocks that corresponded with the block
847 * group of the containing inode, and keep contiguous blocks
848 * together. However, this very difficult to do efficiently, since we
849 * don't have the necessary information up front.
853 #define DESPERATION 2
855 static void init_block_alloc(ext2_resize_t rfs)
857 rfs->alloc_state = AVOID_OLD;
858 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
860 /* HACK for testing */
861 if (rfs->new_fs->super->s_blocks_count >
862 rfs->old_fs->super->s_blocks_count)
863 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
867 static blk_t get_new_block(ext2_resize_t rfs)
869 ext2_filsys fs = rfs->new_fs;
872 if (rfs->new_blk >= fs->super->s_blocks_count) {
873 if (rfs->alloc_state == DESPERATION)
876 #ifdef RESIZE2FS_DEBUG
877 if (rfs->flags & RESIZE_DEBUG_BMOVE)
878 printf("Going into desperation mode "
879 "for block allocations\n");
881 rfs->alloc_state = DESPERATION;
882 rfs->new_blk = fs->super->s_first_data_block;
885 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
886 ext2fs_test_block_bitmap(rfs->reserve_blocks,
888 ((rfs->alloc_state == AVOID_OLD) &&
889 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
890 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
899 static errcode_t block_mover(ext2_resize_t rfs)
901 blk_t blk, old_blk, new_blk;
902 ext2_filsys fs = rfs->new_fs;
903 ext2_filsys old_fs = rfs->old_fs;
907 ext2_badblocks_list badblock_list = 0;
910 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
914 new_blk = fs->super->s_first_data_block;
915 if (!rfs->itable_buf) {
916 retval = ext2fs_get_mem(fs->blocksize *
917 fs->inode_blocks_per_group,
922 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
927 * The first step is to figure out where all of the blocks
931 init_block_alloc(rfs);
932 for (blk = old_fs->super->s_first_data_block;
933 blk < old_fs->super->s_blocks_count; blk++) {
934 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
936 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
938 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
939 ext2fs_badblocks_list_del(badblock_list, blk);
944 new_blk = get_new_block(rfs);
949 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
950 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
956 ext2fs_free_extent_table(rfs->bmap);
964 * Step two is to actually move the blocks
966 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
967 if (retval) goto errout;
970 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
976 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
977 if (retval) goto errout;
980 #ifdef RESIZE2FS_DEBUG
981 if (rfs->flags & RESIZE_DEBUG_BMOVE)
982 printf("Moving %d blocks %u->%u\n",
983 size, old_blk, new_blk);
987 if (c > fs->inode_blocks_per_group)
988 c = fs->inode_blocks_per_group;
989 retval = io_channel_read_blk(fs->io, old_blk, c,
991 if (retval) goto errout;
992 retval = io_channel_write_blk(fs->io, new_blk, c,
994 if (retval) goto errout;
1000 io_channel_flush(fs->io);
1001 retval = (rfs->progress)(rfs,
1002 E2_RSZ_BLOCK_RELOC_PASS,
1008 io_channel_flush(fs->io);
1012 if (badblock_list) {
1013 if (!retval && bb_modified)
1014 retval = ext2fs_update_bb_inode(old_fs,
1016 ext2fs_badblocks_list_free(badblock_list);
1022 /* --------------------------------------------------------------------
1024 * Resize processing, phase 3
1026 * --------------------------------------------------------------------
1030 struct process_block_struct {
1033 struct ext2_inode * inode;
1039 static int process_block(ext2_filsys fs, blk_t *block_nr,
1040 e2_blkcnt_t blockcnt,
1041 blk_t ref_block EXT2FS_ATTR((unused)),
1042 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1044 struct process_block_struct *pb;
1046 blk_t block, new_block;
1049 pb = (struct process_block_struct *) priv_data;
1051 if (pb->rfs->bmap) {
1052 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1054 *block_nr = new_block;
1055 ret |= BLOCK_CHANGED;
1057 #ifdef RESIZE2FS_DEBUG
1058 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1059 printf("ino=%u, blockcnt=%lld, %u->%u\n",
1060 pb->ino, blockcnt, block, new_block);
1066 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
1067 block, (int) blockcnt);
1079 static errcode_t progress_callback(ext2_filsys fs,
1080 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1081 dgrp_t group, void * priv_data)
1083 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1087 * This check is to protect against old ext2 libraries. It
1088 * shouldn't be needed against new libraries.
1093 if (rfs->progress) {
1094 io_channel_flush(fs->io);
1095 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1096 group+1, fs->group_desc_count);
1104 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1106 struct process_block_struct pb;
1107 ext2_ino_t ino, new_inode;
1108 struct ext2_inode inode;
1109 ext2_inode_scan scan = NULL;
1112 char *block_buf = 0;
1113 ext2_ino_t start_to_move;
1114 blk_t orig_size, new_block;
1116 if ((rfs->old_fs->group_desc_count <=
1117 rfs->new_fs->group_desc_count) &&
1122 * Save the original size of the old filesystem, and
1123 * temporarily set the size to be the new size if the new size
1124 * is larger. We need to do this to avoid catching an error
1125 * by the block iterator routines
1127 orig_size = rfs->old_fs->super->s_blocks_count;
1128 if (orig_size < rfs->new_fs->super->s_blocks_count)
1129 rfs->old_fs->super->s_blocks_count =
1130 rfs->new_fs->super->s_blocks_count;
1132 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1133 if (retval) goto errout;
1135 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1136 if (retval) goto errout;
1137 retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3, &block_buf);
1138 if (retval) goto errout;
1140 start_to_move = (rfs->new_fs->group_desc_count *
1141 rfs->new_fs->super->s_inodes_per_group);
1143 if (rfs->progress) {
1144 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1145 0, rfs->old_fs->group_desc_count);
1149 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1153 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1155 * First, copy all of the inodes that need to be moved
1156 * elsewhere in the inode table
1159 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1160 if (retval) goto errout;
1164 if (inode.i_links_count == 0 && ino != EXT2_RESIZE_INO)
1165 continue; /* inode not in use */
1167 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
1170 if (inode.i_file_acl && rfs->bmap) {
1171 new_block = ext2fs_extent_translate(rfs->bmap,
1174 inode.i_file_acl = new_block;
1175 retval = ext2fs_write_inode(rfs->old_fs,
1177 if (retval) goto errout;
1181 if (ext2fs_inode_has_valid_blocks(&inode) &&
1182 (rfs->bmap || pb.is_dir)) {
1184 retval = ext2fs_block_iterate2(rfs->old_fs,
1186 process_block, &pb);
1195 if (ino <= start_to_move)
1196 continue; /* Don't need to move it. */
1202 if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
1206 if (new_inode > rfs->new_fs->super->s_inodes_count) {
1211 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1213 /* Get the new version of the inode */
1214 retval = ext2fs_read_inode(rfs->old_fs, ino, &inode);
1215 if (retval) goto errout;
1217 inode.i_ctime = time(0);
1218 retval = ext2fs_write_inode(rfs->old_fs, new_inode, &inode);
1219 if (retval) goto errout;
1221 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1222 if (LINUX_S_ISDIR(inode.i_mode))
1223 rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1225 #ifdef RESIZE2FS_DEBUG
1226 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1227 printf("Inode moved %u->%u\n", ino, new_inode);
1230 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1234 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1236 io_channel_flush(rfs->old_fs->io);
1239 rfs->old_fs->super->s_blocks_count = orig_size;
1241 ext2fs_free_extent_table(rfs->bmap);
1245 ext2fs_close_inode_scan(scan);
1247 ext2fs_free_mem(&block_buf);
1251 /* --------------------------------------------------------------------
1253 * Resize processing, phase 4.
1255 * --------------------------------------------------------------------
1261 unsigned long max_dirs;
1265 static int check_and_change_inodes(ext2_ino_t dir,
1266 int entry EXT2FS_ATTR((unused)),
1267 struct ext2_dir_entry *dirent, int offset,
1268 int blocksize EXT2FS_ATTR((unused)),
1269 char *buf EXT2FS_ATTR((unused)),
1272 struct istruct *is = (struct istruct *) priv_data;
1273 struct ext2_inode inode;
1274 ext2_ino_t new_inode;
1277 if (is->rfs->progress && offset == 0) {
1278 io_channel_flush(is->rfs->old_fs->io);
1279 is->err = (is->rfs->progress)(is->rfs,
1280 E2_RSZ_INODE_REF_UPD_PASS,
1281 ++is->num, is->max_dirs);
1283 return DIRENT_ABORT;
1289 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1293 #ifdef RESIZE2FS_DEBUG
1294 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1295 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1296 dir, dirent->name_len&0xFF, dirent->name,
1297 dirent->inode, new_inode);
1300 dirent->inode = new_inode;
1302 /* Update the directory mtime and ctime */
1303 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1305 inode.i_mtime = inode.i_ctime = time(0);
1306 ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1309 return DIRENT_CHANGED;
1312 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1321 * Now, we iterate over all of the directories to update the
1325 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1329 if (rfs->progress) {
1330 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1336 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1337 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1338 check_and_change_inodes, &is);
1347 ext2fs_free_extent_table(rfs->imap);
1353 /* --------------------------------------------------------------------
1355 * Resize processing, phase 5.
1357 * In this phase we actually move the inode table around, and then
1358 * update the summary statistics. This is scary, since aborting here
1359 * will potentially scramble the filesystem. (We are moving the
1360 * inode tables around in place, and so the potential for lost data,
1361 * or at the very least scrambling the mapping between filenames and
1362 * inode numbers is very high in case of a power failure here.)
1363 * --------------------------------------------------------------------
1368 * A very scary routine --- this one moves the inode table around!!!
1370 * After this you have to use the rfs->new_fs file handle to read and
1373 static errcode_t move_itables(ext2_resize_t rfs)
1375 int n, num, size, diff;
1376 dgrp_t i, max_groups;
1377 ext2_filsys fs = rfs->new_fs;
1379 blk_t old_blk, new_blk, blk;
1381 int j, to_move, moved;
1383 max_groups = fs->group_desc_count;
1384 if (max_groups > rfs->old_fs->group_desc_count)
1385 max_groups = rfs->old_fs->group_desc_count;
1387 size = fs->blocksize * fs->inode_blocks_per_group;
1388 if (!rfs->itable_buf) {
1389 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1395 * Figure out how many inode tables we need to move
1397 to_move = moved = 0;
1398 for (i=0; i < max_groups; i++)
1399 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1400 fs->group_desc[i].bg_inode_table)
1406 if (rfs->progress) {
1407 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1413 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1415 for (i=0; i < max_groups; i++) {
1416 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1417 new_blk = fs->group_desc[i].bg_inode_table;
1418 diff = new_blk - old_blk;
1420 #ifdef RESIZE2FS_DEBUG
1421 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1422 printf("Itable move group %d block %u->%u (diff %d)\n",
1423 i, old_blk, new_blk, diff);
1429 retval = io_channel_read_blk(fs->io, old_blk,
1430 fs->inode_blocks_per_group,
1435 * The end of the inode table segment often contains
1436 * all zeros, and we're often only moving the inode
1437 * table down a block or two. If so, we can optimize
1438 * things by not rewriting blocks that we know to be zero
1441 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1444 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1445 #ifdef RESIZE2FS_DEBUG
1446 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1447 printf("%d blocks of zeros...\n", n);
1449 num = fs->inode_blocks_per_group;
1453 retval = io_channel_write_blk(fs->io, new_blk,
1454 num, rfs->itable_buf);
1456 io_channel_write_blk(fs->io, old_blk,
1457 num, rfs->itable_buf);
1461 retval = io_channel_write_blk(fs->io,
1462 old_blk + fs->inode_blocks_per_group,
1463 diff, (rfs->itable_buf +
1464 (fs->inode_blocks_per_group - diff) *
1470 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1471 j < fs->inode_blocks_per_group ; j++, blk++)
1472 ext2fs_unmark_block_bitmap(fs->block_map, blk);
1474 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1475 ext2fs_mark_super_dirty(rfs->old_fs);
1476 ext2fs_flush(rfs->old_fs);
1478 if (rfs->progress) {
1479 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1485 mark_table_blocks(fs, fs->block_map);
1487 #ifdef RESIZE2FS_DEBUG
1488 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1489 printf("Inode table move finished.\n");
1498 * Fix the resize inode
1500 static errcode_t fix_resize_inode(ext2_filsys fs)
1502 struct ext2_inode inode;
1506 if (!(fs->super->s_feature_compat &
1507 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1510 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1511 if (retval) goto errout;
1513 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1514 if (retval) goto errout;
1516 inode.i_blocks = fs->blocksize/512;
1518 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1519 if (retval) goto errout;
1521 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1523 * Avoid zeroing out block #0; that's rude. This
1524 * should never happen anyway since the filesystem
1525 * should be fsck'ed and we assume it is consistent.
1528 _("Should never happen: resize inode corrupt!\n"));
1532 memset(block_buf, 0, fs->blocksize);
1534 retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1536 if (retval) goto errout;
1538 retval = ext2fs_create_resize_inode(fs);
1544 ext2fs_free_mem(&block_buf);
1549 * Finally, recalculate the summary information
1551 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1555 unsigned int group = 0;
1556 unsigned int count = 0;
1561 * First calculate the block statistics
1563 for (blk = fs->super->s_first_data_block;
1564 blk < fs->super->s_blocks_count; blk++) {
1565 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1570 if ((count == fs->super->s_blocks_per_group) ||
1571 (blk == fs->super->s_blocks_count-1)) {
1572 fs->group_desc[group++].bg_free_blocks_count =
1578 fs->super->s_free_blocks_count = total_free;
1581 * Next, calculate the inode statistics
1587 for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
1588 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1593 if ((count == fs->super->s_inodes_per_group) ||
1594 (ino == fs->super->s_inodes_count)) {
1595 fs->group_desc[group++].bg_free_inodes_count =
1601 fs->super->s_free_inodes_count = total_free;
1602 ext2fs_mark_super_dirty(fs);