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 void fix_uninit_block_bitmaps(ext2_filsys fs);
44 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size);
45 static errcode_t blocks_to_move(ext2_resize_t rfs);
46 static errcode_t block_mover(ext2_resize_t rfs);
47 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
48 static errcode_t inode_ref_fix(ext2_resize_t rfs);
49 static errcode_t move_itables(ext2_resize_t rfs);
50 static errcode_t fix_resize_inode(ext2_filsys fs);
51 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
52 static errcode_t fix_sb_journal_backup(ext2_filsys fs);
55 * Some helper CPP macros
57 #define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap)
58 #define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap)
59 #define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table)
61 #define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i)))
62 #define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i)))
64 #define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \
65 ((blk) < (FS_INODE_TB((fs), (i)) + \
66 (fs)->inode_blocks_per_group)))
68 #define META_OVERHEAD(fs) (2 + (fs)->inode_blocks_per_group)
69 #define SUPER_OVERHEAD(fs) (1 + (fs)->desc_blocks +\
70 (fs)->super->s_reserved_gdt_blocks)
73 * This is the top-level routine which does the dirty deed....
75 errcode_t resize_fs(ext2_filsys fs, blk_t *new_size, int flags,
76 errcode_t (*progress)(ext2_resize_t rfs, int pass,
78 unsigned long max_val))
83 retval = ext2fs_read_bitmaps(fs);
87 fs->super->s_state |= EXT2_ERROR_FS;
88 ext2fs_mark_super_dirty(fs);
92 * Create the data structure
94 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
97 memset(rfs, 0, sizeof(struct ext2_resize_struct));
99 fix_uninit_block_bitmaps(fs);
104 rfs->progress = progress;
105 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
109 retval = adjust_superblock(rfs, *new_size);
113 fix_uninit_block_bitmaps(rfs->new_fs);
114 /* Clear the block bitmap uninit flag for the last block group */
115 ext2fs_bg_flag_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
116 EXT2_BG_BLOCK_UNINIT);
118 *new_size = rfs->new_fs->super->s_blocks_count;
120 retval = blocks_to_move(rfs);
124 #ifdef RESIZE2FS_DEBUG
125 if (rfs->flags & RESIZE_DEBUG_BMOVE)
126 printf("Number of free blocks: %u/%u, Needed: %d\n",
127 rfs->old_fs->super->s_free_blocks_count,
128 rfs->new_fs->super->s_free_blocks_count,
132 retval = block_mover(rfs);
136 retval = inode_scan_and_fix(rfs);
140 retval = inode_ref_fix(rfs);
144 retval = move_itables(rfs);
148 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
152 retval = fix_resize_inode(rfs->new_fs);
156 retval = fix_sb_journal_backup(rfs->new_fs);
160 rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
161 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
162 retval = ext2fs_close(rfs->new_fs);
168 ext2fs_free(rfs->old_fs);
170 ext2fs_free_mem(&rfs->itable_buf);
171 if (rfs->reserve_blocks)
172 ext2fs_free_block_bitmap(rfs->reserve_blocks);
173 if (rfs->move_blocks)
174 ext2fs_free_block_bitmap(rfs->move_blocks);
175 ext2fs_free_mem(&rfs);
181 ext2fs_free(rfs->new_fs);
183 ext2fs_free_mem(&rfs->itable_buf);
184 ext2fs_free_mem(&rfs);
189 * Clean up the bitmaps for unitialized bitmaps
191 static void fix_uninit_block_bitmaps(ext2_filsys fs)
193 blk_t i, blk, super_blk, old_desc_blk, new_desc_blk;
197 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
198 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
201 for (g=0; g < fs->group_desc_count; g++) {
202 if (!(ext2fs_bg_flag_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
205 blk = (g * fs->super->s_blocks_per_group) +
206 fs->super->s_first_data_block;
208 ext2fs_super_and_bgd_loc(fs, g, &super_blk,
209 &old_desc_blk, &new_desc_blk, 0);
211 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
212 old_desc_blocks = fs->super->s_first_meta_bg;
214 old_desc_blocks = fs->desc_blocks +
215 fs->super->s_reserved_gdt_blocks;
217 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
218 if (blk >= fs->super->s_blocks_count)
220 if ((blk == super_blk) ||
221 (old_desc_blk && old_desc_blocks &&
222 (blk >= old_desc_blk) &&
223 (blk < old_desc_blk + old_desc_blocks)) ||
224 (new_desc_blk && (blk == new_desc_blk)) ||
225 (blk == fs->group_desc[g].bg_block_bitmap) ||
226 (blk == fs->group_desc[g].bg_inode_bitmap) ||
227 (blk >= fs->group_desc[g].bg_inode_table &&
228 (blk < fs->group_desc[g].bg_inode_table
229 + fs->inode_blocks_per_group)))
230 ext2fs_fast_mark_block_bitmap2(fs->block_map, blk);
232 ext2fs_fast_unmark_block_bitmap2(fs->block_map, blk);
237 /* --------------------------------------------------------------------
239 * Resize processing, phase 1.
241 * In this phase we adjust the in-memory superblock information, and
242 * initialize any new parts of the inode table. The new parts of the
243 * inode table are created in virgin disk space, so we can abort here
244 * without any side effects.
245 * --------------------------------------------------------------------
249 * If the group descriptor's bitmap and inode table blocks are valid,
250 * release them in the new filesystem data structure, and mark them as
251 * reserved so the old inode table blocks don't get overwritten.
253 static void free_gdp_blocks(ext2_filsys fs,
254 ext2fs_block_bitmap reserve_blocks,
255 struct ext2_group_desc *gdp)
260 if (gdp->bg_block_bitmap &&
261 (gdp->bg_block_bitmap < fs->super->s_blocks_count)) {
262 ext2fs_block_alloc_stats(fs, gdp->bg_block_bitmap, -1);
263 ext2fs_mark_block_bitmap2(reserve_blocks,
264 gdp->bg_block_bitmap);
267 if (gdp->bg_inode_bitmap &&
268 (gdp->bg_inode_bitmap < fs->super->s_blocks_count)) {
269 ext2fs_block_alloc_stats(fs, gdp->bg_inode_bitmap, -1);
270 ext2fs_mark_block_bitmap2(reserve_blocks,
271 gdp->bg_inode_bitmap);
274 if (gdp->bg_inode_table == 0 ||
275 (gdp->bg_inode_table >= fs->super->s_blocks_count))
278 for (blk = gdp->bg_inode_table, j = 0;
279 j < fs->inode_blocks_per_group; j++, blk++) {
280 if (blk >= fs->super->s_blocks_count)
282 ext2fs_block_alloc_stats(fs, blk, -1);
283 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
288 * This routine is shared by the online and offline resize routines.
289 * All of the information which is adjusted in memory is done here.
291 * The reserve_blocks parameter is only needed when shrinking the
294 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
295 ext2fs_block_bitmap reserve_blocks, blk_t new_size)
300 blk_t blk, group_block;
302 int adj, old_numblocks, numblocks, adjblocks;
303 unsigned long i, j, old_desc_blocks, max_group;
304 unsigned int meta_bg, meta_bg_size;
305 int has_super, csum_flag;
306 unsigned long long new_inodes; /* u64 to check for overflow */
309 fs->super->s_blocks_count = new_size;
312 fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
313 fs->super->s_first_data_block,
314 EXT2_BLOCKS_PER_GROUP(fs->super));
315 if (fs->group_desc_count == 0)
316 return EXT2_ET_TOOSMALL;
317 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
318 EXT2_DESC_PER_BLOCK(fs->super));
321 * Overhead is the number of bookkeeping blocks per group. It
322 * includes the superblock backup, the group descriptor
323 * backups, the inode bitmap, the block bitmap, and the inode
326 overhead = (int) (2 + fs->inode_blocks_per_group);
328 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
329 overhead += 1 + fs->desc_blocks +
330 fs->super->s_reserved_gdt_blocks;
333 * See if the last group is big enough to support the
334 * necessary data structures. If not, we need to get rid of
337 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
338 fs->super->s_blocks_per_group;
339 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
340 return EXT2_ET_TOOSMALL;
341 if (rem && (rem < overhead+50)) {
342 fs->super->s_blocks_count -= rem;
346 * Adjust the number of inodes
348 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
349 if (new_inodes > ~0U) {
350 fprintf(stderr, _("inodes (%llu) must be less than %u"),
352 return EXT2_ET_TOO_MANY_INODES;
354 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
355 fs->group_desc_count;
358 * Adjust the number of free blocks
360 blk = old_fs->super->s_blocks_count;
361 if (blk > fs->super->s_blocks_count)
362 fs->super->s_free_blocks_count -=
363 (blk - fs->super->s_blocks_count);
365 fs->super->s_free_blocks_count +=
366 (fs->super->s_blocks_count - blk);
369 * Adjust the number of reserved blocks
371 percent = (old_fs->super->s_r_blocks_count * 100.0) /
372 old_fs->super->s_blocks_count;
373 fs->super->s_r_blocks_count = (unsigned int) (percent *
374 fs->super->s_blocks_count / 100.0);
377 * Adjust the bitmaps for size
379 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
380 fs->super->s_inodes_count,
382 if (retval) goto errout;
384 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
385 * fs->group_desc_count)) - 1 +
386 fs->super->s_first_data_block;
387 retval = ext2fs_resize_block_bitmap2(fs->super->s_blocks_count-1,
388 real_end, fs->block_map);
390 if (retval) goto errout;
393 * Reallocate the group descriptors as necessary.
395 if (old_fs->desc_blocks != fs->desc_blocks) {
396 retval = ext2fs_resize_mem(old_fs->desc_blocks *
398 fs->desc_blocks * fs->blocksize,
402 if (fs->desc_blocks > old_fs->desc_blocks)
403 memset((char *) fs->group_desc +
404 (old_fs->desc_blocks * fs->blocksize), 0,
405 (fs->desc_blocks - old_fs->desc_blocks) *
410 * If the resize_inode feature is set, and we are changing the
411 * number of descriptor blocks, then adjust
412 * s_reserved_gdt_blocks if possible to avoid needing to move
413 * the inode table either now or in the future.
415 if ((fs->super->s_feature_compat &
416 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
417 (old_fs->desc_blocks != fs->desc_blocks)) {
420 new = ((int) fs->super->s_reserved_gdt_blocks) +
421 (old_fs->desc_blocks - fs->desc_blocks);
424 if (new > (int) fs->blocksize/4)
425 new = fs->blocksize/4;
426 fs->super->s_reserved_gdt_blocks = new;
430 * If we are shrinking the number of block groups, we're done
433 if (old_fs->group_desc_count > fs->group_desc_count) {
435 * Check the block groups that we are chopping off
436 * and free any blocks associated with their metadata
438 for (i = fs->group_desc_count;
439 i < old_fs->group_desc_count; i++) {
440 free_gdp_blocks(fs, reserve_blocks,
441 &old_fs->group_desc[i]);
448 * Fix the count of the last (old) block group
450 old_numblocks = (old_fs->super->s_blocks_count -
451 old_fs->super->s_first_data_block) %
452 old_fs->super->s_blocks_per_group;
454 old_numblocks = old_fs->super->s_blocks_per_group;
455 if (old_fs->group_desc_count == fs->group_desc_count) {
456 numblocks = (fs->super->s_blocks_count -
457 fs->super->s_first_data_block) %
458 fs->super->s_blocks_per_group;
460 numblocks = fs->super->s_blocks_per_group;
462 numblocks = fs->super->s_blocks_per_group;
463 i = old_fs->group_desc_count - 1;
464 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
465 ext2fs_group_desc_csum_set(fs, i);
468 * If the number of block groups is staying the same, we're
469 * done and can exit now. (If the number block groups is
470 * shrinking, we had exited earlier.)
472 if (old_fs->group_desc_count >= fs->group_desc_count) {
478 * Initialize the new block group descriptors
480 group_block = fs->super->s_first_data_block +
481 old_fs->group_desc_count * fs->super->s_blocks_per_group;
483 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
484 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
485 adj = old_fs->group_desc_count;
486 max_group = fs->group_desc_count - adj;
487 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
488 old_desc_blocks = fs->super->s_first_meta_bg;
490 old_desc_blocks = fs->desc_blocks +
491 fs->super->s_reserved_gdt_blocks;
492 for (i = old_fs->group_desc_count;
493 i < fs->group_desc_count; i++) {
494 memset(&fs->group_desc[i], 0,
495 sizeof(struct ext2_group_desc));
498 ext2fs_bg_flags_clear(fs, i, 0);
500 ext2fs_bg_flag_set(fs, i, EXT2_BG_INODE_UNINIT | EXT2_BG_INODE_ZEROED)
502 if (i == fs->group_desc_count-1) {
503 numblocks = (fs->super->s_blocks_count -
504 fs->super->s_first_data_block) %
505 fs->super->s_blocks_per_group;
507 numblocks = fs->super->s_blocks_per_group;
509 numblocks = fs->super->s_blocks_per_group;
511 ext2fs_bg_flag_set(fs, i, EXT2_BG_BLOCK_UNINIT)
515 has_super = ext2fs_bg_has_super(fs, i);
517 ext2fs_block_alloc_stats(fs, group_block, +1);
520 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
521 meta_bg = i / meta_bg_size;
522 if (!(fs->super->s_feature_incompat &
523 EXT2_FEATURE_INCOMPAT_META_BG) ||
524 (meta_bg < fs->super->s_first_meta_bg)) {
526 for (j=0; j < old_desc_blocks; j++)
527 ext2fs_block_alloc_stats(fs,
528 group_block + 1 + j, +1);
529 adjblocks += old_desc_blocks;
534 if (((i % meta_bg_size) == 0) ||
535 ((i % meta_bg_size) == 1) ||
536 ((i % meta_bg_size) == (meta_bg_size-1)))
537 ext2fs_block_alloc_stats(fs,
538 group_block + has_super, +1);
541 adjblocks += 2 + fs->inode_blocks_per_group;
543 numblocks -= adjblocks;
544 fs->super->s_free_blocks_count -= adjblocks;
545 fs->super->s_free_inodes_count +=
546 fs->super->s_inodes_per_group;
547 fs->group_desc[i].bg_free_blocks_count = numblocks;
548 fs->group_desc[i].bg_free_inodes_count =
549 fs->super->s_inodes_per_group;
550 fs->group_desc[i].bg_used_dirs_count = 0;
551 ext2fs_group_desc_csum_set(fs, i);
553 retval = ext2fs_allocate_group_table(fs, i, 0);
554 if (retval) goto errout;
556 group_block += fs->super->s_blocks_per_group;
565 * This routine adjusts the superblock and other data structures, both
566 * in disk as well as in memory...
568 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
575 unsigned long max_group;
578 ext2fs_mark_super_dirty(fs);
579 ext2fs_mark_bb_dirty(fs);
580 ext2fs_mark_ib_dirty(fs);
582 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
583 &rfs->reserve_blocks);
587 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
592 * Check to make sure there are enough inodes
594 if ((rfs->old_fs->super->s_inodes_count -
595 rfs->old_fs->super->s_free_inodes_count) >
596 rfs->new_fs->super->s_inodes_count) {
602 * If we are shrinking the number block groups, we're done and
605 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
611 * If the number of block groups is staying the same, we're
612 * done and can exit now. (If the number block groups is
613 * shrinking, we had exited earlier.)
615 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
621 * Initialize the new block group descriptors
623 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
628 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
629 group_block = fs->super->s_first_data_block +
630 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
632 adj = rfs->old_fs->group_desc_count;
633 max_group = fs->group_desc_count - adj;
635 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
640 for (i = rfs->old_fs->group_desc_count;
641 i < fs->group_desc_count; i++) {
643 * Write out the new inode table
645 retval = io_channel_write_blk64(fs->io,
646 fs->group_desc[i].bg_inode_table,
647 fs->inode_blocks_per_group,
649 if (retval) goto errout;
651 io_channel_flush(fs->io);
653 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
654 i - adj + 1, max_group);
658 group_block += fs->super->s_blocks_per_group;
660 io_channel_flush(fs->io);
667 /* --------------------------------------------------------------------
669 * Resize processing, phase 2.
671 * In this phase we adjust determine which blocks need to be moved, in
672 * blocks_to_move(). We then copy the blocks to their ultimate new
673 * destinations using block_mover(). Since we are copying blocks to
674 * their new locations, again during this pass we can abort without
676 * --------------------------------------------------------------------
680 * This helper function creates a block bitmap with all of the
681 * filesystem meta-data blocks.
683 static errcode_t mark_table_blocks(ext2_filsys fs,
684 ext2fs_block_bitmap bmap)
689 unsigned long meta_bg_size;
690 unsigned int old_desc_blocks;
692 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
693 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
694 old_desc_blocks = fs->super->s_first_meta_bg;
696 old_desc_blocks = fs->desc_blocks +
697 fs->super->s_reserved_gdt_blocks;
698 for (i = 0; i < fs->group_desc_count; i++) {
699 ext2fs_reserve_super_and_bgd(fs, i, bmap);
702 * Mark the blocks used for the inode table
704 for (j = 0, b = fs->group_desc[i].bg_inode_table;
705 j < (unsigned int) fs->inode_blocks_per_group;
707 ext2fs_mark_block_bitmap2(bmap, b);
710 * Mark block used for the block bitmap
712 ext2fs_mark_block_bitmap2(bmap,
713 fs->group_desc[i].bg_block_bitmap);
716 * Mark block used for the inode bitmap
718 ext2fs_mark_block_bitmap2(bmap,
719 fs->group_desc[i].bg_inode_bitmap);
725 * This function checks to see if a particular block (either a
726 * superblock or a block group descriptor) overlaps with an inode or
727 * block bitmap block, or with the inode table.
729 static void mark_fs_metablock(ext2_resize_t rfs,
730 ext2fs_block_bitmap meta_bmap,
731 int group, blk_t blk)
733 ext2_filsys fs = rfs->new_fs;
735 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
736 ext2fs_block_alloc_stats(fs, blk, +1);
739 * Check to see if we overlap with the inode or block bitmap,
740 * or the inode tables. If not, and the block is in use, then
741 * mark it as a block to be moved.
743 if (IS_BLOCK_BM(fs, group, blk)) {
744 FS_BLOCK_BM(fs, group) = 0;
745 rfs->needed_blocks++;
746 } else if (IS_INODE_BM(fs, group, blk)) {
747 FS_INODE_BM(fs, group) = 0;
748 rfs->needed_blocks++;
749 } else if (IS_INODE_TB(fs, group, blk)) {
750 FS_INODE_TB(fs, group) = 0;
751 rfs->needed_blocks++;
752 } else if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
753 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
754 (ext2fs_bg_flag_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
756 * If the block bitmap is uninitialized, which means
757 * nothing other than standard metadata in use.
760 } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
761 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
762 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
763 rfs->needed_blocks++;
769 * This routine marks and unmarks reserved blocks in the new block
770 * bitmap. It also determines which blocks need to be moved and
771 * places this information into the move_blocks bitmap.
773 static errcode_t blocks_to_move(ext2_resize_t rfs)
776 dgrp_t i, max_groups, g;
777 blk_t blk, group_blk;
778 unsigned long old_blocks, new_blocks;
779 unsigned int meta_bg, meta_bg_size;
781 ext2_filsys fs, old_fs;
782 ext2fs_block_bitmap meta_bmap;
783 __u32 save_incompat_flag;
786 old_fs = rfs->old_fs;
787 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
790 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
795 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
800 retval = mark_table_blocks(old_fs, meta_bmap);
807 * If we're shrinking the filesystem, we need to move all of
808 * the blocks that don't fit any more
810 for (blk = fs->super->s_blocks_count;
811 blk < old_fs->super->s_blocks_count; blk++) {
812 g = ext2fs_group_of_blk(fs, blk);
813 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
814 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
815 ext2fs_bg_flag_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
817 * The block bitmap is uninitialized, so skip
818 * to the next block group.
820 blk = ((g+1) * fs->super->s_blocks_per_group) +
821 fs->super->s_first_data_block - 1;
824 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
825 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
826 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
827 rfs->needed_blocks++;
829 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
832 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
833 old_blocks = old_fs->super->s_first_meta_bg;
834 new_blocks = fs->super->s_first_meta_bg;
836 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
837 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
840 if (old_blocks == new_blocks) {
845 max_groups = fs->group_desc_count;
846 if (max_groups > old_fs->group_desc_count)
847 max_groups = old_fs->group_desc_count;
848 group_blk = old_fs->super->s_first_data_block;
850 * If we're reducing the number of descriptor blocks, this
851 * makes life easy. :-) We just have to mark some extra
854 if (old_blocks > new_blocks) {
855 for (i = 0; i < max_groups; i++) {
856 if (!ext2fs_bg_has_super(fs, i)) {
857 group_blk += fs->super->s_blocks_per_group;
860 for (blk = group_blk+1+new_blocks;
861 blk < group_blk+1+old_blocks; blk++) {
862 ext2fs_block_alloc_stats(fs, blk, -1);
863 rfs->needed_blocks--;
865 group_blk += fs->super->s_blocks_per_group;
871 * If we're increasing the number of descriptor blocks, life
872 * gets interesting....
874 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
875 for (i = 0; i < max_groups; i++) {
876 has_super = ext2fs_bg_has_super(fs, i);
878 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
880 meta_bg = i / meta_bg_size;
881 if (!(fs->super->s_feature_incompat &
882 EXT2_FEATURE_INCOMPAT_META_BG) ||
883 (meta_bg < fs->super->s_first_meta_bg)) {
885 for (blk = group_blk+1;
886 blk < group_blk + 1 + new_blocks; blk++)
887 mark_fs_metablock(rfs, meta_bmap,
893 if (((i % meta_bg_size) == 0) ||
894 ((i % meta_bg_size) == 1) ||
895 ((i % meta_bg_size) == (meta_bg_size-1)))
896 mark_fs_metablock(rfs, meta_bmap, i,
897 group_blk + has_super);
900 if (fs->group_desc[i].bg_inode_table &&
901 fs->group_desc[i].bg_inode_bitmap &&
902 fs->group_desc[i].bg_block_bitmap)
906 * Reserve the existing meta blocks that we know
907 * aren't to be moved.
909 if (fs->group_desc[i].bg_block_bitmap)
910 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
911 fs->group_desc[i].bg_block_bitmap);
912 if (fs->group_desc[i].bg_inode_bitmap)
913 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
914 fs->group_desc[i].bg_inode_bitmap);
915 if (fs->group_desc[i].bg_inode_table)
916 for (blk = fs->group_desc[i].bg_inode_table, j=0;
917 j < fs->inode_blocks_per_group ; j++, blk++)
918 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
922 * Allocate the missing data structures
924 * XXX We have a problem with FLEX_BG and off-line
925 * resizing where we are growing the size of the
926 * filesystem. ext2fs_allocate_group_table() will try
927 * to reserve the inode table in the desired flex_bg
928 * location. However, passing rfs->reserve_blocks
929 * doesn't work since it only has reserved the blocks
930 * that will be used in the new block group -- and
931 * with flex_bg, we can and will allocate the tables
932 * outside of the block group. And we can't pass in
933 * the fs->block_map because it doesn't handle
934 * overlapping inode table movements right. So for
935 * now, we temporarily disable flex_bg to force
936 * ext2fs_allocate_group_tables() to allocate the bg
937 * metadata in side the block group, and the restore
938 * it afterwards. Ugly, until we can fix this up
941 save_incompat_flag = fs->super->s_feature_incompat;
942 fs->super->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_FLEX_BG;
943 retval = ext2fs_allocate_group_table(fs, i,
944 rfs->reserve_blocks);
945 fs->super->s_feature_incompat = save_incompat_flag;
950 * For those structures that have changed, we need to
953 if (FS_BLOCK_BM(old_fs, i) !=
954 (blk = FS_BLOCK_BM(fs, i))) {
955 ext2fs_block_alloc_stats(fs, blk, +1);
956 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
957 !ext2fs_test_block_bitmap2(meta_bmap, blk))
958 ext2fs_mark_block_bitmap2(rfs->move_blocks,
961 if (FS_INODE_BM(old_fs, i) !=
962 (blk = FS_INODE_BM(fs, i))) {
963 ext2fs_block_alloc_stats(fs, blk, +1);
964 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
965 !ext2fs_test_block_bitmap2(meta_bmap, blk))
966 ext2fs_mark_block_bitmap2(rfs->move_blocks,
971 * The inode table, if we need to relocate it, is
972 * handled specially. We have to reserve the blocks
973 * for both the old and the new inode table, since we
974 * can't have the inode table be destroyed during the
975 * block relocation phase.
977 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
978 goto next_group; /* inode table not moved */
980 rfs->needed_blocks += fs->inode_blocks_per_group;
983 * Mark the new inode table as in use in the new block
984 * allocation bitmap, and move any blocks that might
987 for (blk = fs->group_desc[i].bg_inode_table, j=0;
988 j < fs->inode_blocks_per_group ; j++, blk++) {
989 ext2fs_block_alloc_stats(fs, blk, +1);
990 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
991 !ext2fs_test_block_bitmap2(meta_bmap, blk))
992 ext2fs_mark_block_bitmap2(rfs->move_blocks,
997 * Make sure the old inode table is reserved in the
998 * block reservation bitmap.
1000 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1001 j < fs->inode_blocks_per_group ; j++, blk++)
1002 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1005 group_blk += rfs->new_fs->super->s_blocks_per_group;
1011 ext2fs_free_block_bitmap(meta_bmap);
1017 * This helper function tries to allocate a new block. We try to
1018 * avoid hitting the original group descriptor blocks at least at
1019 * first, since we want to make it possible to recover from a badly
1020 * aborted resize operation as much as possible.
1022 * In the future, I may further modify this routine to balance out
1023 * where we get the new blocks across the various block groups.
1024 * Ideally we would allocate blocks that corresponded with the block
1025 * group of the containing inode, and keep contiguous blocks
1026 * together. However, this very difficult to do efficiently, since we
1027 * don't have the necessary information up front.
1031 #define DESPERATION 2
1033 static void init_block_alloc(ext2_resize_t rfs)
1035 rfs->alloc_state = AVOID_OLD;
1036 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1038 /* HACK for testing */
1039 if (rfs->new_fs->super->s_blocks_count >
1040 rfs->old_fs->super->s_blocks_count)
1041 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
1045 static blk_t get_new_block(ext2_resize_t rfs)
1047 ext2_filsys fs = rfs->new_fs;
1050 if (rfs->new_blk >= fs->super->s_blocks_count) {
1051 if (rfs->alloc_state == DESPERATION)
1054 #ifdef RESIZE2FS_DEBUG
1055 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1056 printf("Going into desperation mode "
1057 "for block allocations\n");
1059 rfs->alloc_state = DESPERATION;
1060 rfs->new_blk = fs->super->s_first_data_block;
1063 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1064 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1066 ((rfs->alloc_state == AVOID_OLD) &&
1067 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
1068 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1073 return rfs->new_blk;
1077 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1080 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1083 blk = get_new_block(rfs);
1087 #ifdef RESIZE2FS_DEBUG
1088 if (rfs->flags & 0xF)
1089 printf("get_alloc_block allocating %u\n", blk);
1092 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1093 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1094 *ret = (blk64_t) blk;
1098 static errcode_t block_mover(ext2_resize_t rfs)
1100 blk_t blk, old_blk, new_blk;
1101 ext2_filsys fs = rfs->new_fs;
1102 ext2_filsys old_fs = rfs->old_fs;
1106 ext2_badblocks_list badblock_list = 0;
1107 int bb_modified = 0;
1109 fs->get_alloc_block = resize2fs_get_alloc_block;
1110 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1112 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1116 new_blk = fs->super->s_first_data_block;
1117 if (!rfs->itable_buf) {
1118 retval = ext2fs_get_array(fs->blocksize,
1119 fs->inode_blocks_per_group,
1124 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1129 * The first step is to figure out where all of the blocks
1132 to_move = moved = 0;
1133 init_block_alloc(rfs);
1134 for (blk = old_fs->super->s_first_data_block;
1135 blk < old_fs->super->s_blocks_count; blk++) {
1136 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1138 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1140 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1141 ext2fs_badblocks_list_del(badblock_list, blk);
1146 new_blk = get_new_block(rfs);
1151 ext2fs_block_alloc_stats(fs, new_blk, +1);
1152 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
1158 ext2fs_free_extent_table(rfs->bmap);
1166 * Step two is to actually move the blocks
1168 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1169 if (retval) goto errout;
1171 if (rfs->progress) {
1172 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1178 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1179 if (retval) goto errout;
1182 #ifdef RESIZE2FS_DEBUG
1183 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1184 printf("Moving %d blocks %u->%u\n",
1185 size, old_blk, new_blk);
1189 if (c > fs->inode_blocks_per_group)
1190 c = fs->inode_blocks_per_group;
1191 retval = io_channel_read_blk64(fs->io, old_blk, c,
1193 if (retval) goto errout;
1194 retval = io_channel_write_blk64(fs->io, new_blk, c,
1196 if (retval) goto errout;
1201 if (rfs->progress) {
1202 io_channel_flush(fs->io);
1203 retval = (rfs->progress)(rfs,
1204 E2_RSZ_BLOCK_RELOC_PASS,
1210 io_channel_flush(fs->io);
1214 if (badblock_list) {
1215 if (!retval && bb_modified)
1216 retval = ext2fs_update_bb_inode(old_fs,
1218 ext2fs_badblocks_list_free(badblock_list);
1224 /* --------------------------------------------------------------------
1226 * Resize processing, phase 3
1228 * --------------------------------------------------------------------
1232 struct process_block_struct {
1235 struct ext2_inode * inode;
1241 static int process_block(ext2_filsys fs, blk_t *block_nr,
1242 e2_blkcnt_t blockcnt,
1243 blk_t ref_block EXT2FS_ATTR((unused)),
1244 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1246 struct process_block_struct *pb;
1248 blk_t block, new_block;
1251 pb = (struct process_block_struct *) priv_data;
1253 if (pb->rfs->bmap) {
1254 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1256 *block_nr = new_block;
1257 ret |= BLOCK_CHANGED;
1259 #ifdef RESIZE2FS_DEBUG
1260 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1261 printf("ino=%u, blockcnt=%lld, %u->%u\n",
1262 pb->ino, blockcnt, block, new_block);
1268 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
1269 block, (int) blockcnt);
1281 static errcode_t progress_callback(ext2_filsys fs,
1282 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1283 dgrp_t group, void * priv_data)
1285 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1289 * This check is to protect against old ext2 libraries. It
1290 * shouldn't be needed against new libraries.
1295 if (rfs->progress) {
1296 io_channel_flush(fs->io);
1297 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1298 group+1, fs->group_desc_count);
1306 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1308 struct process_block_struct pb;
1309 ext2_ino_t ino, new_inode;
1310 struct ext2_inode *inode = NULL;
1311 ext2_inode_scan scan = NULL;
1313 char *block_buf = 0;
1314 ext2_ino_t start_to_move;
1315 blk_t orig_size, new_block;
1318 if ((rfs->old_fs->group_desc_count <=
1319 rfs->new_fs->group_desc_count) &&
1324 * Save the original size of the old filesystem, and
1325 * temporarily set the size to be the new size if the new size
1326 * is larger. We need to do this to avoid catching an error
1327 * by the block iterator routines
1329 orig_size = rfs->old_fs->super->s_blocks_count;
1330 if (orig_size < rfs->new_fs->super->s_blocks_count)
1331 rfs->old_fs->super->s_blocks_count =
1332 rfs->new_fs->super->s_blocks_count;
1334 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1335 if (retval) goto errout;
1337 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1338 if (retval) goto errout;
1339 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1340 if (retval) goto errout;
1342 start_to_move = (rfs->new_fs->group_desc_count *
1343 rfs->new_fs->super->s_inodes_per_group);
1345 if (rfs->progress) {
1346 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1347 0, rfs->old_fs->group_desc_count);
1351 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1355 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1356 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1357 inode = malloc(inode_size);
1363 * First, copy all of the inodes that need to be moved
1364 * elsewhere in the inode table
1367 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1368 if (retval) goto errout;
1372 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1373 continue; /* inode not in use */
1375 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1378 if (inode->i_file_acl && rfs->bmap) {
1379 new_block = ext2fs_extent_translate(rfs->bmap,
1382 inode->i_file_acl = new_block;
1383 retval = ext2fs_write_inode_full(rfs->old_fs,
1384 ino, inode, inode_size);
1385 if (retval) goto errout;
1389 if (ext2fs_inode_has_valid_blocks(inode) &&
1390 (rfs->bmap || pb.is_dir)) {
1392 retval = ext2fs_block_iterate2(rfs->old_fs,
1394 process_block, &pb);
1403 if (ino <= start_to_move)
1404 continue; /* Don't need to move it. */
1409 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1413 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1416 /* Get the new version of the inode */
1417 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1419 if (retval) goto errout;
1421 inode->i_ctime = time(0);
1422 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1424 if (retval) goto errout;
1426 #ifdef RESIZE2FS_DEBUG
1427 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1428 printf("Inode moved %u->%u\n", ino, new_inode);
1431 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1435 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1437 io_channel_flush(rfs->old_fs->io);
1440 rfs->old_fs->super->s_blocks_count = orig_size;
1442 ext2fs_free_extent_table(rfs->bmap);
1446 ext2fs_close_inode_scan(scan);
1448 ext2fs_free_mem(&block_buf);
1453 /* --------------------------------------------------------------------
1455 * Resize processing, phase 4.
1457 * --------------------------------------------------------------------
1463 unsigned int max_dirs;
1467 static int check_and_change_inodes(ext2_ino_t dir,
1468 int entry EXT2FS_ATTR((unused)),
1469 struct ext2_dir_entry *dirent, int offset,
1470 int blocksize EXT2FS_ATTR((unused)),
1471 char *buf EXT2FS_ATTR((unused)),
1474 struct istruct *is = (struct istruct *) priv_data;
1475 struct ext2_inode inode;
1476 ext2_ino_t new_inode;
1479 if (is->rfs->progress && offset == 0) {
1480 io_channel_flush(is->rfs->old_fs->io);
1481 is->err = (is->rfs->progress)(is->rfs,
1482 E2_RSZ_INODE_REF_UPD_PASS,
1483 ++is->num, is->max_dirs);
1485 return DIRENT_ABORT;
1491 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1495 #ifdef RESIZE2FS_DEBUG
1496 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1497 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1498 dir, dirent->name_len&0xFF, dirent->name,
1499 dirent->inode, new_inode);
1502 dirent->inode = new_inode;
1504 /* Update the directory mtime and ctime */
1505 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1507 inode.i_mtime = inode.i_ctime = time(0);
1508 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1510 return DIRENT_ABORT;
1513 return DIRENT_CHANGED;
1516 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1525 * Now, we iterate over all of the directories to update the
1529 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1533 if (rfs->progress) {
1534 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1540 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1541 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1542 check_and_change_inodes, &is);
1550 if (rfs->progress && (is.num < is.max_dirs))
1551 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1552 is.max_dirs, is.max_dirs);
1555 ext2fs_free_extent_table(rfs->imap);
1561 /* --------------------------------------------------------------------
1563 * Resize processing, phase 5.
1565 * In this phase we actually move the inode table around, and then
1566 * update the summary statistics. This is scary, since aborting here
1567 * will potentially scramble the filesystem. (We are moving the
1568 * inode tables around in place, and so the potential for lost data,
1569 * or at the very least scrambling the mapping between filenames and
1570 * inode numbers is very high in case of a power failure here.)
1571 * --------------------------------------------------------------------
1576 * A very scary routine --- this one moves the inode table around!!!
1578 * After this you have to use the rfs->new_fs file handle to read and
1581 static errcode_t move_itables(ext2_resize_t rfs)
1583 int n, num, size, diff;
1584 dgrp_t i, max_groups;
1585 ext2_filsys fs = rfs->new_fs;
1587 blk_t old_blk, new_blk, blk;
1589 int j, to_move, moved;
1591 max_groups = fs->group_desc_count;
1592 if (max_groups > rfs->old_fs->group_desc_count)
1593 max_groups = rfs->old_fs->group_desc_count;
1595 size = fs->blocksize * fs->inode_blocks_per_group;
1596 if (!rfs->itable_buf) {
1597 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1603 * Figure out how many inode tables we need to move
1605 to_move = moved = 0;
1606 for (i=0; i < max_groups; i++)
1607 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1608 fs->group_desc[i].bg_inode_table)
1614 if (rfs->progress) {
1615 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1621 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1623 for (i=0; i < max_groups; i++) {
1624 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1625 new_blk = fs->group_desc[i].bg_inode_table;
1626 diff = new_blk - old_blk;
1628 #ifdef RESIZE2FS_DEBUG
1629 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1630 printf("Itable move group %d block %u->%u (diff %d)\n",
1631 i, old_blk, new_blk, diff);
1637 retval = io_channel_read_blk64(fs->io, old_blk,
1638 fs->inode_blocks_per_group,
1643 * The end of the inode table segment often contains
1644 * all zeros, and we're often only moving the inode
1645 * table down a block or two. If so, we can optimize
1646 * things by not rewriting blocks that we know to be zero
1649 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1652 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1653 #ifdef RESIZE2FS_DEBUG
1654 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1655 printf("%d blocks of zeros...\n", n);
1657 num = fs->inode_blocks_per_group;
1661 retval = io_channel_write_blk64(fs->io, new_blk,
1662 num, rfs->itable_buf);
1664 io_channel_write_blk64(fs->io, old_blk,
1665 num, rfs->itable_buf);
1669 retval = io_channel_write_blk64(fs->io,
1670 old_blk + fs->inode_blocks_per_group,
1671 diff, (rfs->itable_buf +
1672 (fs->inode_blocks_per_group - diff) *
1678 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1679 j < fs->inode_blocks_per_group ; j++, blk++)
1680 ext2fs_block_alloc_stats(fs, blk, -1);
1682 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1683 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1684 ext2fs_mark_super_dirty(rfs->old_fs);
1685 ext2fs_flush(rfs->old_fs);
1687 if (rfs->progress) {
1688 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1694 mark_table_blocks(fs, fs->block_map);
1696 #ifdef RESIZE2FS_DEBUG
1697 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1698 printf("Inode table move finished.\n");
1707 * Fix the resize inode
1709 static errcode_t fix_resize_inode(ext2_filsys fs)
1711 struct ext2_inode inode;
1716 if (!(fs->super->s_feature_compat &
1717 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1720 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1721 if (retval) goto errout;
1723 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1724 if (retval) goto errout;
1726 if (fs->super->s_reserved_gdt_blocks == 0) {
1727 fs->super->s_feature_compat &=
1728 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1729 ext2fs_mark_super_dirty(fs);
1731 if ((blk = inode.i_block[EXT2_DIND_BLOCK]) != 0)
1732 ext2fs_block_alloc_stats(fs, blk, -1);
1734 memset(&inode, 0, sizeof(inode));
1736 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1740 ext2fs_iblk_set(fs, &inode, 1);
1742 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1743 if (retval) goto errout;
1745 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1747 * Avoid zeroing out block #0; that's rude. This
1748 * should never happen anyway since the filesystem
1749 * should be fsck'ed and we assume it is consistent.
1752 _("Should never happen: resize inode corrupt!\n"));
1756 memset(block_buf, 0, fs->blocksize);
1758 retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1760 if (retval) goto errout;
1762 retval = ext2fs_create_resize_inode(fs);
1768 ext2fs_free_mem(&block_buf);
1773 * Finally, recalculate the summary information
1775 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1779 unsigned int group = 0;
1780 unsigned int count = 0;
1784 blk_t super_blk, old_desc_blk, new_desc_blk;
1785 int old_desc_blocks;
1788 * First calculate the block statistics
1790 uninit = ext2fs_bg_flag_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1791 ext2fs_super_and_bgd_loc(fs, group, &super_blk, &old_desc_blk,
1793 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1794 old_desc_blocks = fs->super->s_first_meta_bg;
1796 old_desc_blocks = fs->desc_blocks +
1797 fs->super->s_reserved_gdt_blocks;
1798 for (blk = fs->super->s_first_data_block;
1799 blk < fs->super->s_blocks_count; blk++) {
1801 !((blk == super_blk) ||
1802 ((old_desc_blk && old_desc_blocks &&
1803 (blk >= old_desc_blk) &&
1804 (blk < old_desc_blk + old_desc_blocks))) ||
1805 ((new_desc_blk && (blk == new_desc_blk))) ||
1806 (blk == fs->group_desc[group].bg_block_bitmap) ||
1807 (blk == fs->group_desc[group].bg_inode_bitmap) ||
1808 ((blk >= fs->group_desc[group].bg_inode_table &&
1809 (blk < fs->group_desc[group].bg_inode_table
1810 + fs->inode_blocks_per_group))))) ||
1811 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
1816 if ((count == fs->super->s_blocks_per_group) ||
1817 (blk == fs->super->s_blocks_count-1)) {
1818 fs->group_desc[group].bg_free_blocks_count =
1820 ext2fs_group_desc_csum_set(fs, group);
1824 uninit = (ext2fs_bg_flag_test(fs, group, EXT2_BG_BLOCK_UNINIT)
1826 ext2fs_super_and_bgd_loc(fs, group, &super_blk,
1829 if (fs->super->s_feature_incompat &
1830 EXT2_FEATURE_INCOMPAT_META_BG)
1831 old_desc_blocks = fs->super->s_first_meta_bg;
1833 old_desc_blocks = fs->desc_blocks +
1834 fs->super->s_reserved_gdt_blocks;
1837 fs->super->s_free_blocks_count = total_free;
1840 * Next, calculate the inode statistics
1847 /* Protect loop from wrap-around if s_inodes_count maxed */
1848 uninit = ext2fs_bg_flag_test(fs, group, EXT2_BG_INODE_UNINIT);
1849 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1851 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1856 if ((count == fs->super->s_inodes_per_group) ||
1857 (ino == fs->super->s_inodes_count)) {
1858 fs->group_desc[group].bg_free_inodes_count =
1860 ext2fs_group_desc_csum_set(fs, group);
1864 uninit = (ext2fs_bg_flag_test(fs, group, EXT2_BG_INODE_UNINIT)
1868 fs->super->s_free_inodes_count = total_free;
1869 ext2fs_mark_super_dirty(fs);
1874 * Journal may have been relocated; update the backup journal blocks
1875 * in the superblock.
1877 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
1880 struct ext2_inode inode;
1882 if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1885 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
1888 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
1889 fs->super->s_jnl_blocks[16] = inode.i_size;
1890 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
1891 ext2fs_mark_super_dirty(fs);
1896 * calcluate the minimum number of blocks the given fs can be resized to
1898 blk_t calculate_minimum_resize_size(ext2_filsys fs)
1900 blk_t inode_count, blks_needed, groups, data_blocks;
1901 blk_t grp, data_needed, last_start;
1902 int overhead = 0, num_of_superblocks = 0;
1903 int extra_groups = 0;
1904 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1907 * first figure out how many group descriptors we need to
1908 * handle the number of inodes we have
1910 inode_count = fs->super->s_inodes_count -
1911 fs->super->s_free_inodes_count;
1912 blks_needed = ext2fs_div_ceil(inode_count,
1913 fs->super->s_inodes_per_group) *
1914 EXT2_BLOCKS_PER_GROUP(fs->super);
1915 groups = ext2fs_div_ceil(blks_needed,
1916 EXT2_BLOCKS_PER_GROUP(fs->super));
1919 * we need to figure out how many backup superblocks we have so we can
1920 * account for that in the metadata
1922 for (grp = 0; grp < fs->group_desc_count; grp++) {
1923 if (ext2fs_bg_has_super(fs, grp))
1924 num_of_superblocks++;
1927 /* calculate how many blocks are needed for data */
1928 data_needed = fs->super->s_blocks_count -
1929 fs->super->s_free_blocks_count;
1930 data_needed -= SUPER_OVERHEAD(fs) * num_of_superblocks;
1931 data_needed -= META_OVERHEAD(fs) * fs->group_desc_count;
1933 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1935 * For ext4 we need to allow for up to a flex_bg worth
1936 * of inode tables of slack space so the resize
1937 * operation can be guaranteed to finish.
1939 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
1940 data_needed += META_OVERHEAD(fs) * extra_groups;
1941 extra_groups = groups % flexbg_size;
1945 * figure out how many data blocks we have given the number of groups
1946 * we need for our inodes
1948 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
1950 for (grp = 0; grp < groups; grp++) {
1951 overhead = META_OVERHEAD(fs);
1953 if (ext2fs_bg_has_super(fs, grp))
1954 overhead += SUPER_OVERHEAD(fs);
1957 * we want to keep track of how much data we can store in
1958 * the groups leading up to the last group so we can determine
1959 * how big the last group needs to be
1961 if (grp != (groups - 1))
1962 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
1965 data_blocks -= overhead;
1969 * if we need more group descriptors in order to accomodate our data
1970 * then we need to add them here
1972 while (data_needed > data_blocks) {
1973 blk_t remainder = data_needed - data_blocks;
1976 /* figure out how many more groups we need for the data */
1977 extra_grps = ext2fs_div_ceil(remainder,
1978 EXT2_BLOCKS_PER_GROUP(fs->super));
1980 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
1982 /* ok we have to account for the last group */
1983 overhead = META_OVERHEAD(fs);
1984 if (ext2fs_bg_has_super(fs, groups-1))
1985 overhead += SUPER_OVERHEAD(fs);
1986 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
1988 for (grp = groups; grp < groups+extra_grps; grp++) {
1989 overhead = META_OVERHEAD(fs);
1990 if (ext2fs_bg_has_super(fs, grp))
1991 overhead += SUPER_OVERHEAD(fs);
1994 * again, we need to see how much data we cram into
1995 * all of the groups leading up to the last group
1997 if (grp != (groups + extra_grps - 1))
1998 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
2001 data_blocks -= overhead;
2004 groups += extra_grps;
2005 extra_groups += extra_grps;
2006 if (fs->super->s_feature_incompat
2007 & EXT4_FEATURE_INCOMPAT_FLEX_BG
2008 && extra_groups > flexbg_size) {
2010 * For ext4 we need to allow for up to a flex_bg worth
2011 * of inode tables of slack space so the resize
2012 * operation can be guaranteed to finish.
2014 extra_groups = flexbg_size -
2015 (groups & (flexbg_size - 1));
2016 data_needed += META_OVERHEAD(fs) * extra_groups;
2017 extra_groups = groups % flexbg_size;
2021 /* now for the fun voodoo */
2022 overhead = META_OVERHEAD(fs);
2025 * if this is the case then the last group is going to have data in it
2026 * so we need to adjust the size of the last group accordingly
2028 if (last_start < data_needed) {
2029 blk_t remainder = data_needed - last_start;
2032 * 50 is a magic number that mkfs/resize uses to see if its
2033 * even worth making/resizing the fs. basically you need to
2034 * have at least 50 blocks in addition to the blocks needed
2035 * for the metadata in the last group
2038 overhead += remainder;
2044 if (ext2fs_bg_has_super(fs, groups-1))
2045 overhead += SUPER_OVERHEAD(fs);
2048 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2049 * only do groups-1, and then add the number of blocks needed to
2050 * handle the group descriptor metadata+data that we need
2052 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2053 blks_needed += overhead;
2056 * If at this point we've already added up more "needed" than
2057 * the current size, just return current size as minimum.
2059 if (blks_needed >= fs->super->s_blocks_count)
2060 return fs->super->s_blocks_count;
2062 * We need to reserve a few extra blocks if extents are
2063 * enabled, in case we need to grow the extent tree. The more
2064 * we shrink the file system, the more space we need.
2066 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
2067 blks_needed += (fs->super->s_blocks_count - blks_needed)/500;