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 = ext2fs_div_ceil(fs->super->s_blocks_count -
194 fs->super->s_first_data_block,
195 EXT2_BLOCKS_PER_GROUP(fs->super));
196 if (fs->group_desc_count == 0)
197 return EXT2_ET_TOOSMALL;
198 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
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 overhead = (int) (2 + fs->inode_blocks_per_group);
209 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
210 overhead += 1 + fs->desc_blocks +
211 fs->super->s_reserved_gdt_blocks;
214 * See if the last group is big enough to support the
215 * necessary data structures. If not, we need to get rid of
218 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
219 fs->super->s_blocks_per_group;
220 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
221 return EXT2_ET_TOOSMALL;
222 if (rem && (rem < overhead+50)) {
223 fs->super->s_blocks_count -= rem;
227 * Adjust the number of inodes
229 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
230 fs->group_desc_count;
233 * Adjust the number of free blocks
235 blk = old_fs->super->s_blocks_count;
236 if (blk > fs->super->s_blocks_count)
237 fs->super->s_free_blocks_count -=
238 (blk - fs->super->s_blocks_count);
240 fs->super->s_free_blocks_count +=
241 (fs->super->s_blocks_count - blk);
244 * Adjust the number of reserved blocks
246 blk = old_fs->super->s_r_blocks_count * 100 /
247 old_fs->super->s_blocks_count;
248 fs->super->s_r_blocks_count = ((fs->super->s_blocks_count * blk)
252 * Adjust the bitmaps for size
254 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
255 fs->super->s_inodes_count,
257 if (retval) goto errout;
259 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
260 * fs->group_desc_count)) - 1 +
261 fs->super->s_first_data_block;
262 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
263 real_end, fs->block_map);
265 if (retval) goto errout;
268 * Reallocate the group descriptors as necessary.
270 if (old_fs->desc_blocks != fs->desc_blocks) {
271 retval = ext2fs_resize_mem(old_fs->desc_blocks *
273 fs->desc_blocks * fs->blocksize,
277 if (fs->desc_blocks > old_fs->desc_blocks)
278 memset((char *) fs->group_desc +
279 (old_fs->desc_blocks * fs->blocksize), 0,
280 (fs->desc_blocks - old_fs->desc_blocks) *
285 * If the resize_inode feature is set, and we are changing the
286 * number of descriptor blocks, then adjust
287 * s_reserved_gdt_blocks if possible to avoid needing to move
288 * the inode table either now or in the future.
290 if ((fs->super->s_feature_compat &
291 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
292 (old_fs->desc_blocks != fs->desc_blocks)) {
295 new = ((int) fs->super->s_reserved_gdt_blocks) +
296 (old_fs->desc_blocks - fs->desc_blocks);
299 if (new > fs->blocksize/4)
300 new = fs->blocksize/4;
301 fs->super->s_reserved_gdt_blocks = new;
303 fs->super->s_feature_compat &=
304 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
308 * If we are shrinking the number block groups, we're done and
311 if (old_fs->group_desc_count > fs->group_desc_count) {
317 * Fix the count of the last (old) block group
319 old_numblocks = (old_fs->super->s_blocks_count -
320 old_fs->super->s_first_data_block) %
321 old_fs->super->s_blocks_per_group;
323 old_numblocks = old_fs->super->s_blocks_per_group;
324 if (old_fs->group_desc_count == fs->group_desc_count) {
325 numblocks = (fs->super->s_blocks_count -
326 fs->super->s_first_data_block) %
327 fs->super->s_blocks_per_group;
329 numblocks = fs->super->s_blocks_per_group;
331 numblocks = fs->super->s_blocks_per_group;
332 i = old_fs->group_desc_count - 1;
333 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
336 * If the number of block groups is staying the same, we're
337 * done and can exit now. (If the number block groups is
338 * shrinking, we had exited earlier.)
340 if (old_fs->group_desc_count >= fs->group_desc_count) {
346 * Initialize the new block group descriptors
348 group_block = fs->super->s_first_data_block +
349 old_fs->group_desc_count * fs->super->s_blocks_per_group;
351 adj = old_fs->group_desc_count;
352 max_group = fs->group_desc_count - adj;
353 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
354 old_desc_blocks = fs->super->s_first_meta_bg;
356 old_desc_blocks = fs->desc_blocks +
357 fs->super->s_reserved_gdt_blocks;
358 for (i = old_fs->group_desc_count;
359 i < fs->group_desc_count; i++) {
360 memset(&fs->group_desc[i], 0,
361 sizeof(struct ext2_group_desc));
364 if (i == fs->group_desc_count-1) {
365 numblocks = (fs->super->s_blocks_count -
366 fs->super->s_first_data_block) %
367 fs->super->s_blocks_per_group;
369 numblocks = fs->super->s_blocks_per_group;
371 numblocks = fs->super->s_blocks_per_group;
373 has_super = ext2fs_bg_has_super(fs, i);
375 ext2fs_mark_block_bitmap(fs->block_map, group_block);
378 meta_bg_size = (fs->blocksize /
379 sizeof (struct ext2_group_desc));
380 meta_bg = i / meta_bg_size;
381 if (!(fs->super->s_feature_incompat &
382 EXT2_FEATURE_INCOMPAT_META_BG) ||
383 (meta_bg < fs->super->s_first_meta_bg)) {
385 for (j=0; j < old_desc_blocks; j++)
386 ext2fs_mark_block_bitmap(fs->block_map,
387 group_block + 1 + j);
388 adjblocks += old_desc_blocks;
393 if (((i % meta_bg_size) == 0) ||
394 ((i % meta_bg_size) == 1) ||
395 ((i % meta_bg_size) == (meta_bg_size-1)))
396 ext2fs_mark_block_bitmap(fs->block_map,
397 group_block + has_super);
400 adjblocks += 2 + fs->inode_blocks_per_group;
402 numblocks -= adjblocks;
403 fs->super->s_free_blocks_count -= adjblocks;
404 fs->super->s_free_inodes_count +=
405 fs->super->s_inodes_per_group;
406 fs->group_desc[i].bg_free_blocks_count = numblocks;
407 fs->group_desc[i].bg_free_inodes_count =
408 fs->super->s_inodes_per_group;
409 fs->group_desc[i].bg_used_dirs_count = 0;
411 retval = ext2fs_allocate_group_table(fs, i, 0);
412 if (retval) goto errout;
414 group_block += fs->super->s_blocks_per_group;
423 * This routine adjusts the superblock and other data structures, both
424 * in disk as well as in memory...
426 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
433 unsigned long max_group;
436 ext2fs_mark_super_dirty(fs);
437 ext2fs_mark_bb_dirty(fs);
438 ext2fs_mark_ib_dirty(fs);
440 retval = adjust_fs_info(fs, rfs->old_fs, new_size);
445 * Check to make sure there are enough inodes
447 if ((rfs->old_fs->super->s_inodes_count -
448 rfs->old_fs->super->s_free_inodes_count) >
449 rfs->new_fs->super->s_inodes_count) {
455 * If we are shrinking the number block groups, we're done and
458 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
464 * If the number of block groups is staying the same, we're
465 * done and can exit now. (If the number block groups is
466 * shrinking, we had exited earlier.)
468 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
474 * Initialize the new block group descriptors
476 retval = ext2fs_get_mem(fs->blocksize * fs->inode_blocks_per_group,
481 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
482 group_block = fs->super->s_first_data_block +
483 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
485 adj = rfs->old_fs->group_desc_count;
486 max_group = fs->group_desc_count - adj;
488 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
493 for (i = rfs->old_fs->group_desc_count;
494 i < fs->group_desc_count; i++) {
496 * Write out the new inode table
498 retval = io_channel_write_blk(fs->io,
499 fs->group_desc[i].bg_inode_table,
500 fs->inode_blocks_per_group,
502 if (retval) goto errout;
504 io_channel_flush(fs->io);
506 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
507 i - adj + 1, max_group);
511 group_block += fs->super->s_blocks_per_group;
513 io_channel_flush(fs->io);
520 /* --------------------------------------------------------------------
522 * Resize processing, phase 2.
524 * In this phase we adjust determine which blocks need to be moved, in
525 * blocks_to_move(). We then copy the blocks to their ultimate new
526 * destinations using block_mover(). Since we are copying blocks to
527 * their new locations, again during this pass we can abort without
529 * --------------------------------------------------------------------
533 * This helper function creates a block bitmap with all of the
534 * filesystem meta-data blocks.
536 static errcode_t mark_table_blocks(ext2_filsys fs,
537 ext2fs_block_bitmap bmap)
542 unsigned long meta_bg_size;
543 unsigned int old_desc_blocks;
545 meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
546 block = fs->super->s_first_data_block;
547 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
548 old_desc_blocks = fs->super->s_first_meta_bg;
550 old_desc_blocks = fs->desc_blocks +
551 fs->super->s_reserved_gdt_blocks;
552 for (i = 0; i < fs->group_desc_count; i++) {
553 ext2fs_reserve_super_and_bgd(fs, i, bmap);
556 * Mark the blocks used for the inode table
558 for (j = 0, b = fs->group_desc[i].bg_inode_table;
559 j < (unsigned int) fs->inode_blocks_per_group;
561 ext2fs_mark_block_bitmap(bmap, b);
564 * Mark block used for the block bitmap
566 ext2fs_mark_block_bitmap(bmap,
567 fs->group_desc[i].bg_block_bitmap);
570 * Mark block used for the inode bitmap
572 ext2fs_mark_block_bitmap(bmap,
573 fs->group_desc[i].bg_inode_bitmap);
574 block += fs->super->s_blocks_per_group;
580 * This function checks to see if a particular block (either a
581 * superblock or a block group descriptor) overlaps with an inode or
582 * block bitmap block, or with the inode table.
584 static void mark_fs_metablock(ext2_resize_t rfs,
585 ext2fs_block_bitmap meta_bmap,
586 int group, blk_t blk)
588 ext2_filsys fs = rfs->new_fs;
590 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
591 ext2fs_mark_block_bitmap(fs->block_map, blk);
594 * Check to see if we overlap with the inode or block bitmap,
595 * or the inode tables. If not, and the block is in use, then
596 * mark it as a block to be moved.
598 if (IS_BLOCK_BM(fs, group, blk)) {
599 FS_BLOCK_BM(fs, group) = 0;
600 rfs->needed_blocks++;
601 } else if (IS_INODE_BM(fs, group, blk)) {
602 FS_INODE_BM(fs, group) = 0;
603 rfs->needed_blocks++;
604 } else if (IS_INODE_TB(fs, group, blk)) {
605 FS_INODE_TB(fs, group) = 0;
606 rfs->needed_blocks++;
607 } else if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk) &&
608 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
609 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
610 rfs->needed_blocks++;
616 * This routine marks and unmarks reserved blocks in the new block
617 * bitmap. It also determines which blocks need to be moved and
618 * places this information into the move_blocks bitmap.
620 static errcode_t blocks_to_move(ext2_resize_t rfs)
623 dgrp_t i, max_groups;
624 blk_t blk, group_blk;
625 unsigned long old_blocks, new_blocks;
626 unsigned int meta_bg, meta_bg_size;
628 ext2_filsys fs, old_fs;
629 ext2fs_block_bitmap meta_bmap;
632 old_fs = rfs->old_fs;
633 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
636 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
637 &rfs->reserve_blocks);
641 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
646 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
651 retval = mark_table_blocks(old_fs, meta_bmap);
658 * If we're shrinking the filesystem, we need to move all of
659 * the blocks that don't fit any more
661 for (blk = fs->super->s_blocks_count;
662 blk < old_fs->super->s_blocks_count; blk++) {
663 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
664 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
665 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
666 rfs->needed_blocks++;
668 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
671 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
672 old_blocks = old_fs->super->s_first_meta_bg;
673 new_blocks = fs->super->s_first_meta_bg;
675 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
676 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
679 if (old_blocks == new_blocks) {
684 max_groups = fs->group_desc_count;
685 if (max_groups > old_fs->group_desc_count)
686 max_groups = old_fs->group_desc_count;
687 group_blk = old_fs->super->s_first_data_block;
689 * If we're reducing the number of descriptor blocks, this
690 * makes life easy. :-) We just have to mark some extra
693 if (old_blocks > new_blocks) {
694 for (i = 0; i < max_groups; i++) {
695 if (!ext2fs_bg_has_super(fs, i)) {
696 group_blk += fs->super->s_blocks_per_group;
699 for (blk = group_blk+1+new_blocks;
700 blk < group_blk+1+old_blocks; blk++) {
701 ext2fs_unmark_block_bitmap(fs->block_map,
703 rfs->needed_blocks--;
705 group_blk += fs->super->s_blocks_per_group;
711 * If we're increasing the number of descriptor blocks, life
712 * gets interesting....
714 meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
715 for (i = 0; i < max_groups; i++) {
716 has_super = ext2fs_bg_has_super(fs, i);
718 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
720 meta_bg = i / meta_bg_size;
721 if (!(fs->super->s_feature_incompat &
722 EXT2_FEATURE_INCOMPAT_META_BG) ||
723 (meta_bg < fs->super->s_first_meta_bg)) {
725 for (blk = group_blk+1;
726 blk < group_blk + 1 + new_blocks; blk++)
727 mark_fs_metablock(rfs, meta_bmap,
733 if (((i % meta_bg_size) == 0) ||
734 ((i % meta_bg_size) == 1) ||
735 ((i % meta_bg_size) == (meta_bg_size-1)))
736 mark_fs_metablock(rfs, meta_bmap, i,
737 group_blk + has_super);
740 if (fs->group_desc[i].bg_inode_table &&
741 fs->group_desc[i].bg_inode_bitmap &&
742 fs->group_desc[i].bg_block_bitmap)
746 * Reserve the existing meta blocks that we know
747 * aren't to be moved.
749 if (fs->group_desc[i].bg_block_bitmap)
750 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
751 fs->group_desc[i].bg_block_bitmap);
752 if (fs->group_desc[i].bg_inode_bitmap)
753 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
754 fs->group_desc[i].bg_inode_bitmap);
755 if (fs->group_desc[i].bg_inode_table)
756 for (blk = fs->group_desc[i].bg_inode_table, j=0;
757 j < fs->inode_blocks_per_group ; j++, blk++)
758 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
762 * Allocate the missing data structures
764 retval = ext2fs_allocate_group_table(fs, i,
765 rfs->reserve_blocks);
770 * For those structures that have changed, we need to
773 if (FS_BLOCK_BM(old_fs, i) !=
774 (blk = FS_BLOCK_BM(fs, i))) {
775 ext2fs_mark_block_bitmap(fs->block_map, blk);
776 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
777 !ext2fs_test_block_bitmap(meta_bmap, blk))
778 ext2fs_mark_block_bitmap(rfs->move_blocks,
781 if (FS_INODE_BM(old_fs, i) !=
782 (blk = FS_INODE_BM(fs, i))) {
783 ext2fs_mark_block_bitmap(fs->block_map, blk);
784 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
785 !ext2fs_test_block_bitmap(meta_bmap, blk))
786 ext2fs_mark_block_bitmap(rfs->move_blocks,
791 * The inode table, if we need to relocate it, is
792 * handled specially. We have to reserve the blocks
793 * for both the old and the new inode table, since we
794 * can't have the inode table be destroyed during the
795 * block relocation phase.
797 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
798 goto next_group; /* inode table not moved */
800 rfs->needed_blocks += fs->inode_blocks_per_group;
803 * Mark the new inode table as in use in the new block
804 * allocation bitmap, and move any blocks that might
807 for (blk = fs->group_desc[i].bg_inode_table, j=0;
808 j < fs->inode_blocks_per_group ; j++, blk++) {
809 ext2fs_mark_block_bitmap(fs->block_map, blk);
810 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
811 !ext2fs_test_block_bitmap(meta_bmap, blk))
812 ext2fs_mark_block_bitmap(rfs->move_blocks,
817 * Make sure the old inode table is reserved in the
818 * block reservation bitmap.
820 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
821 j < fs->inode_blocks_per_group ; j++, blk++)
822 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
825 group_blk += rfs->new_fs->super->s_blocks_per_group;
831 ext2fs_free_block_bitmap(meta_bmap);
837 * This helper function tries to allocate a new block. We try to
838 * avoid hitting the original group descriptor blocks at least at
839 * first, since we want to make it possible to recover from a badly
840 * aborted resize operation as much as possible.
842 * In the future, I may further modify this routine to balance out
843 * where we get the new blocks across the various block groups.
844 * Ideally we would allocate blocks that corresponded with the block
845 * group of the containing inode, and keep contiguous blocks
846 * together. However, this very difficult to do efficiently, since we
847 * don't have the necessary information up front.
851 #define DESPERATION 2
853 static void init_block_alloc(ext2_resize_t rfs)
855 rfs->alloc_state = AVOID_OLD;
856 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
858 /* HACK for testing */
859 if (rfs->new_fs->super->s_blocks_count >
860 rfs->old_fs->super->s_blocks_count)
861 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
865 static blk_t get_new_block(ext2_resize_t rfs)
867 ext2_filsys fs = rfs->new_fs;
870 if (rfs->new_blk >= fs->super->s_blocks_count) {
871 if (rfs->alloc_state == DESPERATION)
874 #ifdef RESIZE2FS_DEBUG
875 if (rfs->flags & RESIZE_DEBUG_BMOVE)
876 printf("Going into desperation mode "
877 "for block allocations\n");
879 rfs->alloc_state = DESPERATION;
880 rfs->new_blk = fs->super->s_first_data_block;
883 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
884 ext2fs_test_block_bitmap(rfs->reserve_blocks,
886 ((rfs->alloc_state == AVOID_OLD) &&
887 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
888 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
897 static errcode_t block_mover(ext2_resize_t rfs)
899 blk_t blk, old_blk, new_blk;
900 ext2_filsys fs = rfs->new_fs;
901 ext2_filsys old_fs = rfs->old_fs;
905 ext2_badblocks_list badblock_list = 0;
908 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
912 new_blk = fs->super->s_first_data_block;
913 if (!rfs->itable_buf) {
914 retval = ext2fs_get_mem(fs->blocksize *
915 fs->inode_blocks_per_group,
920 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
925 * The first step is to figure out where all of the blocks
929 init_block_alloc(rfs);
930 for (blk = old_fs->super->s_first_data_block;
931 blk < old_fs->super->s_blocks_count; blk++) {
932 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
934 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
936 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
937 ext2fs_badblocks_list_del(badblock_list, blk);
942 new_blk = get_new_block(rfs);
947 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
948 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
954 ext2fs_free_extent_table(rfs->bmap);
962 * Step two is to actually move the blocks
964 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
965 if (retval) goto errout;
968 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
974 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
975 if (retval) goto errout;
978 #ifdef RESIZE2FS_DEBUG
979 if (rfs->flags & RESIZE_DEBUG_BMOVE)
980 printf("Moving %d blocks %u->%u\n",
981 size, old_blk, new_blk);
985 if (c > fs->inode_blocks_per_group)
986 c = fs->inode_blocks_per_group;
987 retval = io_channel_read_blk(fs->io, old_blk, c,
989 if (retval) goto errout;
990 retval = io_channel_write_blk(fs->io, new_blk, c,
992 if (retval) goto errout;
998 io_channel_flush(fs->io);
999 retval = (rfs->progress)(rfs,
1000 E2_RSZ_BLOCK_RELOC_PASS,
1006 io_channel_flush(fs->io);
1010 if (badblock_list) {
1011 if (!retval && bb_modified)
1012 retval = ext2fs_update_bb_inode(old_fs,
1014 ext2fs_badblocks_list_free(badblock_list);
1020 /* --------------------------------------------------------------------
1022 * Resize processing, phase 3
1024 * --------------------------------------------------------------------
1028 struct process_block_struct {
1031 struct ext2_inode * inode;
1037 static int process_block(ext2_filsys fs, blk_t *block_nr,
1038 e2_blkcnt_t blockcnt,
1039 blk_t ref_block EXT2FS_ATTR((unused)),
1040 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1042 struct process_block_struct *pb;
1044 blk_t block, new_block;
1047 pb = (struct process_block_struct *) priv_data;
1049 if (pb->rfs->bmap) {
1050 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1052 *block_nr = new_block;
1053 ret |= BLOCK_CHANGED;
1055 #ifdef RESIZE2FS_DEBUG
1056 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1057 printf("ino=%u, blockcnt=%lld, %u->%u\n",
1058 pb->ino, blockcnt, block, new_block);
1064 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
1065 block, (int) blockcnt);
1077 static errcode_t progress_callback(ext2_filsys fs,
1078 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1079 dgrp_t group, void * priv_data)
1081 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1085 * This check is to protect against old ext2 libraries. It
1086 * shouldn't be needed against new libraries.
1091 if (rfs->progress) {
1092 io_channel_flush(fs->io);
1093 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1094 group+1, fs->group_desc_count);
1102 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1104 struct process_block_struct pb;
1105 ext2_ino_t ino, new_inode;
1106 struct ext2_inode inode;
1107 ext2_inode_scan scan = NULL;
1110 char *block_buf = 0;
1111 ext2_ino_t start_to_move;
1112 blk_t orig_size, new_block;
1114 if ((rfs->old_fs->group_desc_count <=
1115 rfs->new_fs->group_desc_count) &&
1120 * Save the original size of the old filesystem, and
1121 * temporarily set the size to be the new size if the new size
1122 * is larger. We need to do this to avoid catching an error
1123 * by the block iterator routines
1125 orig_size = rfs->old_fs->super->s_blocks_count;
1126 if (orig_size < rfs->new_fs->super->s_blocks_count)
1127 rfs->old_fs->super->s_blocks_count =
1128 rfs->new_fs->super->s_blocks_count;
1130 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1131 if (retval) goto errout;
1133 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1134 if (retval) goto errout;
1135 retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3, &block_buf);
1136 if (retval) goto errout;
1138 start_to_move = (rfs->new_fs->group_desc_count *
1139 rfs->new_fs->super->s_inodes_per_group);
1141 if (rfs->progress) {
1142 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1143 0, rfs->old_fs->group_desc_count);
1147 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1151 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1153 * First, copy all of the inodes that need to be moved
1154 * elsewhere in the inode table
1157 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1158 if (retval) goto errout;
1162 if (inode.i_links_count == 0 && ino != EXT2_RESIZE_INO)
1163 continue; /* inode not in use */
1165 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
1168 if (inode.i_file_acl && rfs->bmap) {
1169 new_block = ext2fs_extent_translate(rfs->bmap,
1172 inode.i_file_acl = new_block;
1173 retval = ext2fs_write_inode(rfs->old_fs,
1175 if (retval) goto errout;
1179 if (ext2fs_inode_has_valid_blocks(&inode) &&
1180 (rfs->bmap || pb.is_dir)) {
1182 retval = ext2fs_block_iterate2(rfs->old_fs,
1184 process_block, &pb);
1193 if (ino <= start_to_move)
1194 continue; /* Don't need to move it. */
1200 if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
1204 if (new_inode > rfs->new_fs->super->s_inodes_count) {
1209 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1211 /* Get the new version of the inode */
1212 retval = ext2fs_read_inode(rfs->old_fs, ino, &inode);
1213 if (retval) goto errout;
1215 inode.i_ctime = time(0);
1216 retval = ext2fs_write_inode(rfs->old_fs, new_inode, &inode);
1217 if (retval) goto errout;
1219 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1220 if (LINUX_S_ISDIR(inode.i_mode))
1221 rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1223 #ifdef RESIZE2FS_DEBUG
1224 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1225 printf("Inode moved %u->%u\n", ino, new_inode);
1228 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1232 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1234 io_channel_flush(rfs->old_fs->io);
1237 rfs->old_fs->super->s_blocks_count = orig_size;
1239 ext2fs_free_extent_table(rfs->bmap);
1243 ext2fs_close_inode_scan(scan);
1245 ext2fs_free_mem(&block_buf);
1249 /* --------------------------------------------------------------------
1251 * Resize processing, phase 4.
1253 * --------------------------------------------------------------------
1259 unsigned long max_dirs;
1263 static int check_and_change_inodes(ext2_ino_t dir,
1264 int entry EXT2FS_ATTR((unused)),
1265 struct ext2_dir_entry *dirent, int offset,
1266 int blocksize EXT2FS_ATTR((unused)),
1267 char *buf EXT2FS_ATTR((unused)),
1270 struct istruct *is = (struct istruct *) priv_data;
1271 struct ext2_inode inode;
1272 ext2_ino_t new_inode;
1275 if (is->rfs->progress && offset == 0) {
1276 io_channel_flush(is->rfs->old_fs->io);
1277 is->err = (is->rfs->progress)(is->rfs,
1278 E2_RSZ_INODE_REF_UPD_PASS,
1279 ++is->num, is->max_dirs);
1281 return DIRENT_ABORT;
1287 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1291 #ifdef RESIZE2FS_DEBUG
1292 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1293 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1294 dir, dirent->name_len&0xFF, dirent->name,
1295 dirent->inode, new_inode);
1298 dirent->inode = new_inode;
1300 /* Update the directory mtime and ctime */
1301 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1303 inode.i_mtime = inode.i_ctime = time(0);
1304 ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1307 return DIRENT_CHANGED;
1310 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1319 * Now, we iterate over all of the directories to update the
1323 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1327 if (rfs->progress) {
1328 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1334 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1335 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1336 check_and_change_inodes, &is);
1345 ext2fs_free_extent_table(rfs->imap);
1351 /* --------------------------------------------------------------------
1353 * Resize processing, phase 5.
1355 * In this phase we actually move the inode table around, and then
1356 * update the summary statistics. This is scary, since aborting here
1357 * will potentially scramble the filesystem. (We are moving the
1358 * inode tables around in place, and so the potential for lost data,
1359 * or at the very least scrambling the mapping between filenames and
1360 * inode numbers is very high in case of a power failure here.)
1361 * --------------------------------------------------------------------
1366 * A very scary routine --- this one moves the inode table around!!!
1368 * After this you have to use the rfs->new_fs file handle to read and
1371 static errcode_t move_itables(ext2_resize_t rfs)
1373 int n, num, size, diff;
1374 dgrp_t i, max_groups;
1375 ext2_filsys fs = rfs->new_fs;
1377 blk_t old_blk, new_blk, blk;
1379 int j, to_move, moved;
1381 max_groups = fs->group_desc_count;
1382 if (max_groups > rfs->old_fs->group_desc_count)
1383 max_groups = rfs->old_fs->group_desc_count;
1385 size = fs->blocksize * fs->inode_blocks_per_group;
1386 if (!rfs->itable_buf) {
1387 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1393 * Figure out how many inode tables we need to move
1395 to_move = moved = 0;
1396 for (i=0; i < max_groups; i++)
1397 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1398 fs->group_desc[i].bg_inode_table)
1404 if (rfs->progress) {
1405 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1411 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1413 for (i=0; i < max_groups; i++) {
1414 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1415 new_blk = fs->group_desc[i].bg_inode_table;
1416 diff = new_blk - old_blk;
1418 #ifdef RESIZE2FS_DEBUG
1419 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1420 printf("Itable move group %d block %u->%u (diff %d)\n",
1421 i, old_blk, new_blk, diff);
1427 retval = io_channel_read_blk(fs->io, old_blk,
1428 fs->inode_blocks_per_group,
1433 * The end of the inode table segment often contains
1434 * all zeros, and we're often only moving the inode
1435 * table down a block or two. If so, we can optimize
1436 * things by not rewriting blocks that we know to be zero
1439 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1442 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1443 #ifdef RESIZE2FS_DEBUG
1444 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1445 printf("%d blocks of zeros...\n", n);
1447 num = fs->inode_blocks_per_group;
1451 retval = io_channel_write_blk(fs->io, new_blk,
1452 num, rfs->itable_buf);
1454 io_channel_write_blk(fs->io, old_blk,
1455 num, rfs->itable_buf);
1459 retval = io_channel_write_blk(fs->io,
1460 old_blk + fs->inode_blocks_per_group,
1461 diff, (rfs->itable_buf +
1462 (fs->inode_blocks_per_group - diff) *
1468 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1469 j < fs->inode_blocks_per_group ; j++, blk++)
1470 ext2fs_unmark_block_bitmap(fs->block_map, blk);
1472 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1473 ext2fs_mark_super_dirty(rfs->old_fs);
1474 ext2fs_flush(rfs->old_fs);
1476 if (rfs->progress) {
1477 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1483 mark_table_blocks(fs, fs->block_map);
1485 #ifdef RESIZE2FS_DEBUG
1486 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1487 printf("Inode table move finished.\n");
1496 * Fix the resize inode
1498 static errcode_t fix_resize_inode(ext2_filsys fs)
1500 struct ext2_inode inode;
1504 if (!(fs->super->s_feature_compat &
1505 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1508 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1509 if (retval) goto errout;
1511 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1512 if (retval) goto errout;
1514 inode.i_blocks = fs->blocksize/512;
1516 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1517 if (retval) goto errout;
1519 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1521 * Avoid zeroing out block #0; that's rude. This
1522 * should never happen anyway since the filesystem
1523 * should be fsck'ed and we assume it is consistent.
1526 _("Should never happen: resize inode corrupt!\n"));
1530 memset(block_buf, 0, fs->blocksize);
1532 retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1534 if (retval) goto errout;
1536 retval = ext2fs_create_resize_inode(fs);
1542 ext2fs_free_mem(&block_buf);
1547 * Finally, recalculate the summary information
1549 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1553 unsigned int group = 0;
1554 unsigned int count = 0;
1559 * First calculate the block statistics
1561 for (blk = fs->super->s_first_data_block;
1562 blk < fs->super->s_blocks_count; blk++) {
1563 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1568 if ((count == fs->super->s_blocks_per_group) ||
1569 (blk == fs->super->s_blocks_count-1)) {
1570 fs->group_desc[group++].bg_free_blocks_count =
1576 fs->super->s_free_blocks_count = total_free;
1579 * Next, calculate the inode statistics
1586 /* Protect loop from wrap-around if s_inodes_count maxed */
1587 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; 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);