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);
54 * Some helper CPP macros
56 #define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap)
57 #define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap)
58 #define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table)
60 #define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i)))
61 #define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i)))
63 #define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \
64 ((blk) < (FS_INODE_TB((fs), (i)) + \
65 (fs)->inode_blocks_per_group)))
67 #define META_OVERHEAD(fs) (2 + (fs)->inode_blocks_per_group)
68 #define SUPER_OVERHEAD(fs) (1 + (fs)->desc_blocks +\
69 (fs)->super->s_reserved_gdt_blocks)
72 * This is the top-level routine which does the dirty deed....
74 errcode_t resize_fs(ext2_filsys fs, blk_t *new_size, int flags,
75 errcode_t (*progress)(ext2_resize_t rfs, int pass,
77 unsigned long max_val))
82 retval = ext2fs_read_bitmaps(fs);
87 * Create the data structure
89 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
92 memset(rfs, 0, sizeof(struct ext2_resize_struct));
94 fix_uninit_block_bitmaps(fs);
99 rfs->progress = progress;
100 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
104 retval = adjust_superblock(rfs, *new_size);
108 fix_uninit_block_bitmaps(rfs->new_fs);
109 /* Clear the block bitmap uninit flag for the last block group */
110 rfs->new_fs->group_desc[rfs->new_fs->group_desc_count-1].bg_flags &=
111 ~EXT2_BG_BLOCK_UNINIT;
113 *new_size = rfs->new_fs->super->s_blocks_count;
115 retval = blocks_to_move(rfs);
119 #ifdef RESIZE2FS_DEBUG
120 if (rfs->flags & RESIZE_DEBUG_BMOVE)
121 printf("Number of free blocks: %u/%u, Needed: %d\n",
122 rfs->old_fs->super->s_free_blocks_count,
123 rfs->new_fs->super->s_free_blocks_count,
127 retval = block_mover(rfs);
131 retval = inode_scan_and_fix(rfs);
135 retval = inode_ref_fix(rfs);
139 retval = move_itables(rfs);
143 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
147 retval = fix_resize_inode(rfs->new_fs);
151 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
152 retval = ext2fs_close(rfs->new_fs);
158 ext2fs_free(rfs->old_fs);
160 ext2fs_free_mem(&rfs->itable_buf);
161 ext2fs_free_mem(&rfs);
167 ext2fs_free(rfs->new_fs);
169 ext2fs_free_mem(&rfs->itable_buf);
170 ext2fs_free_mem(&rfs);
175 * Clean up the bitmaps for unitialized bitmaps
177 static void fix_uninit_block_bitmaps(ext2_filsys fs)
179 blk_t i, blk, super_blk, old_desc_blk, new_desc_blk;
183 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
184 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
187 for (g=0; g < fs->group_desc_count; g++) {
188 if (!(fs->group_desc[g].bg_flags & EXT2_BG_BLOCK_UNINIT))
191 blk = (g * fs->super->s_blocks_per_group) +
192 fs->super->s_first_data_block;
194 ext2fs_super_and_bgd_loc(fs, g, &super_blk,
195 &old_desc_blk, &new_desc_blk, 0);
197 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
198 old_desc_blocks = fs->super->s_first_meta_bg;
200 old_desc_blocks = fs->desc_blocks +
201 fs->super->s_reserved_gdt_blocks;
203 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
204 if (blk >= fs->super->s_blocks_count)
206 if ((blk == super_blk) ||
207 (old_desc_blk && old_desc_blocks &&
208 (blk >= old_desc_blk) &&
209 (blk < old_desc_blk + old_desc_blocks)) ||
210 (new_desc_blk && (blk == new_desc_blk)) ||
211 (blk == fs->group_desc[g].bg_block_bitmap) ||
212 (blk == fs->group_desc[g].bg_inode_bitmap) ||
213 (blk >= fs->group_desc[g].bg_inode_table &&
214 (blk < fs->group_desc[g].bg_inode_table
215 + fs->inode_blocks_per_group)))
216 ext2fs_fast_mark_block_bitmap(fs->block_map, blk);
218 ext2fs_fast_unmark_block_bitmap(fs->block_map, blk);
223 /* --------------------------------------------------------------------
225 * Resize processing, phase 1.
227 * In this phase we adjust the in-memory superblock information, and
228 * initialize any new parts of the inode table. The new parts of the
229 * inode table are created in virgin disk space, so we can abort here
230 * without any side effects.
231 * --------------------------------------------------------------------
235 * If the group descriptor's bitmap and inode table blocks are valid,
236 * release them in the new filesystem data structure, and mark them as
237 * reserved so the old inode table blocks don't get overwritten.
239 static void free_gdp_blocks(ext2_filsys fs,
240 ext2fs_block_bitmap reserve_blocks,
241 struct ext2_group_desc *gdp)
246 if (gdp->bg_block_bitmap &&
247 (gdp->bg_block_bitmap < fs->super->s_blocks_count)) {
248 ext2fs_block_alloc_stats(fs, gdp->bg_block_bitmap, -1);
249 ext2fs_mark_block_bitmap(reserve_blocks,
250 gdp->bg_block_bitmap);
253 if (gdp->bg_inode_bitmap &&
254 (gdp->bg_inode_bitmap < fs->super->s_blocks_count)) {
255 ext2fs_block_alloc_stats(fs, gdp->bg_inode_bitmap, -1);
256 ext2fs_mark_block_bitmap(reserve_blocks,
257 gdp->bg_inode_bitmap);
260 if (gdp->bg_inode_table == 0 ||
261 (gdp->bg_inode_table >= fs->super->s_blocks_count))
264 for (blk = gdp->bg_inode_table, j = 0;
265 j < fs->inode_blocks_per_group; j++, blk++) {
266 if (blk >= fs->super->s_blocks_count)
268 ext2fs_block_alloc_stats(fs, blk, -1);
269 ext2fs_mark_block_bitmap(reserve_blocks, blk);
274 * This routine is shared by the online and offline resize routines.
275 * All of the information which is adjusted in memory is done here.
277 * The reserve_blocks parameter is only needed when shrinking the
280 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
281 ext2fs_block_bitmap reserve_blocks, blk_t new_size)
286 blk_t blk, group_block;
288 int adj, old_numblocks, numblocks, adjblocks;
289 unsigned long i, j, old_desc_blocks, max_group;
290 unsigned int meta_bg, meta_bg_size;
291 int has_super, csum_flag;
292 unsigned long long new_inodes; /* u64 to check for overflow */
295 fs->super->s_blocks_count = new_size;
298 fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
299 fs->super->s_first_data_block,
300 EXT2_BLOCKS_PER_GROUP(fs->super));
301 if (fs->group_desc_count == 0)
302 return EXT2_ET_TOOSMALL;
303 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
304 EXT2_DESC_PER_BLOCK(fs->super));
307 * Overhead is the number of bookkeeping blocks per group. It
308 * includes the superblock backup, the group descriptor
309 * backups, the inode bitmap, the block bitmap, and the inode
312 overhead = (int) (2 + fs->inode_blocks_per_group);
314 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
315 overhead += 1 + fs->desc_blocks +
316 fs->super->s_reserved_gdt_blocks;
319 * See if the last group is big enough to support the
320 * necessary data structures. If not, we need to get rid of
323 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
324 fs->super->s_blocks_per_group;
325 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
326 return EXT2_ET_TOOSMALL;
327 if (rem && (rem < overhead+50)) {
328 fs->super->s_blocks_count -= rem;
332 * Adjust the number of inodes
334 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
335 if (new_inodes > ~0U) {
336 fprintf(stderr, _("inodes (%llu) must be less than %u"),
338 return EXT2_ET_TOO_MANY_INODES;
340 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
341 fs->group_desc_count;
344 * Adjust the number of free blocks
346 blk = old_fs->super->s_blocks_count;
347 if (blk > fs->super->s_blocks_count)
348 fs->super->s_free_blocks_count -=
349 (blk - fs->super->s_blocks_count);
351 fs->super->s_free_blocks_count +=
352 (fs->super->s_blocks_count - blk);
355 * Adjust the number of reserved blocks
357 percent = (old_fs->super->s_r_blocks_count * 100.0) /
358 old_fs->super->s_blocks_count;
359 fs->super->s_r_blocks_count = (unsigned int) (percent *
360 fs->super->s_blocks_count / 100.0);
363 * Adjust the bitmaps for size
365 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
366 fs->super->s_inodes_count,
368 if (retval) goto errout;
370 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
371 * fs->group_desc_count)) - 1 +
372 fs->super->s_first_data_block;
373 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
374 real_end, fs->block_map);
376 if (retval) goto errout;
379 * Reallocate the group descriptors as necessary.
381 if (old_fs->desc_blocks != fs->desc_blocks) {
382 retval = ext2fs_resize_mem(old_fs->desc_blocks *
384 fs->desc_blocks * fs->blocksize,
388 if (fs->desc_blocks > old_fs->desc_blocks)
389 memset((char *) fs->group_desc +
390 (old_fs->desc_blocks * fs->blocksize), 0,
391 (fs->desc_blocks - old_fs->desc_blocks) *
396 * If the resize_inode feature is set, and we are changing the
397 * number of descriptor blocks, then adjust
398 * s_reserved_gdt_blocks if possible to avoid needing to move
399 * the inode table either now or in the future.
401 if ((fs->super->s_feature_compat &
402 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
403 (old_fs->desc_blocks != fs->desc_blocks)) {
406 new = ((int) fs->super->s_reserved_gdt_blocks) +
407 (old_fs->desc_blocks - fs->desc_blocks);
410 if (new > (int) fs->blocksize/4)
411 new = fs->blocksize/4;
412 fs->super->s_reserved_gdt_blocks = new;
416 * If we are shrinking the number of block groups, we're done
419 if (old_fs->group_desc_count > fs->group_desc_count) {
421 * Check the block groups that we are chopping off
422 * and free any blocks associated with their metadata
424 for (i = fs->group_desc_count;
425 i < old_fs->group_desc_count; i++) {
426 free_gdp_blocks(fs, reserve_blocks,
427 &old_fs->group_desc[i]);
434 * Fix the count of the last (old) block group
436 old_numblocks = (old_fs->super->s_blocks_count -
437 old_fs->super->s_first_data_block) %
438 old_fs->super->s_blocks_per_group;
440 old_numblocks = old_fs->super->s_blocks_per_group;
441 if (old_fs->group_desc_count == fs->group_desc_count) {
442 numblocks = (fs->super->s_blocks_count -
443 fs->super->s_first_data_block) %
444 fs->super->s_blocks_per_group;
446 numblocks = fs->super->s_blocks_per_group;
448 numblocks = fs->super->s_blocks_per_group;
449 i = old_fs->group_desc_count - 1;
450 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
451 ext2fs_group_desc_csum_set(fs, i);
454 * If the number of block groups is staying the same, we're
455 * done and can exit now. (If the number block groups is
456 * shrinking, we had exited earlier.)
458 if (old_fs->group_desc_count >= fs->group_desc_count) {
464 * Initialize the new block group descriptors
466 group_block = fs->super->s_first_data_block +
467 old_fs->group_desc_count * fs->super->s_blocks_per_group;
469 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
470 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
471 adj = old_fs->group_desc_count;
472 max_group = fs->group_desc_count - adj;
473 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
474 old_desc_blocks = fs->super->s_first_meta_bg;
476 old_desc_blocks = fs->desc_blocks +
477 fs->super->s_reserved_gdt_blocks;
478 for (i = old_fs->group_desc_count;
479 i < fs->group_desc_count; i++) {
480 memset(&fs->group_desc[i], 0,
481 sizeof(struct ext2_group_desc));
484 fs->group_desc[i].bg_flags = 0;
486 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_UNINIT |
487 EXT2_BG_INODE_ZEROED;
488 if (i == fs->group_desc_count-1) {
489 numblocks = (fs->super->s_blocks_count -
490 fs->super->s_first_data_block) %
491 fs->super->s_blocks_per_group;
493 numblocks = fs->super->s_blocks_per_group;
495 numblocks = fs->super->s_blocks_per_group;
497 fs->group_desc[i].bg_flags |=
498 EXT2_BG_BLOCK_UNINIT;
501 has_super = ext2fs_bg_has_super(fs, i);
503 ext2fs_block_alloc_stats(fs, group_block, +1);
506 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
507 meta_bg = i / meta_bg_size;
508 if (!(fs->super->s_feature_incompat &
509 EXT2_FEATURE_INCOMPAT_META_BG) ||
510 (meta_bg < fs->super->s_first_meta_bg)) {
512 for (j=0; j < old_desc_blocks; j++)
513 ext2fs_block_alloc_stats(fs,
514 group_block + 1 + j, +1);
515 adjblocks += old_desc_blocks;
520 if (((i % meta_bg_size) == 0) ||
521 ((i % meta_bg_size) == 1) ||
522 ((i % meta_bg_size) == (meta_bg_size-1)))
523 ext2fs_block_alloc_stats(fs,
524 group_block + has_super, +1);
527 adjblocks += 2 + fs->inode_blocks_per_group;
529 numblocks -= adjblocks;
530 fs->super->s_free_blocks_count -= adjblocks;
531 fs->super->s_free_inodes_count +=
532 fs->super->s_inodes_per_group;
533 fs->group_desc[i].bg_free_blocks_count = numblocks;
534 fs->group_desc[i].bg_free_inodes_count =
535 fs->super->s_inodes_per_group;
536 fs->group_desc[i].bg_used_dirs_count = 0;
537 ext2fs_group_desc_csum_set(fs, i);
539 retval = ext2fs_allocate_group_table(fs, i, 0);
540 if (retval) goto errout;
542 group_block += fs->super->s_blocks_per_group;
551 * This routine adjusts the superblock and other data structures, both
552 * in disk as well as in memory...
554 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
561 unsigned long max_group;
564 ext2fs_mark_super_dirty(fs);
565 ext2fs_mark_bb_dirty(fs);
566 ext2fs_mark_ib_dirty(fs);
568 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
569 &rfs->reserve_blocks);
573 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
578 * Check to make sure there are enough inodes
580 if ((rfs->old_fs->super->s_inodes_count -
581 rfs->old_fs->super->s_free_inodes_count) >
582 rfs->new_fs->super->s_inodes_count) {
588 * If we are shrinking the number block groups, we're done and
591 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
597 * If the number of block groups is staying the same, we're
598 * done and can exit now. (If the number block groups is
599 * shrinking, we had exited earlier.)
601 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
607 * Initialize the new block group descriptors
609 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
614 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
615 group_block = fs->super->s_first_data_block +
616 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
618 adj = rfs->old_fs->group_desc_count;
619 max_group = fs->group_desc_count - adj;
621 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
626 for (i = rfs->old_fs->group_desc_count;
627 i < fs->group_desc_count; i++) {
629 * Write out the new inode table
631 retval = io_channel_write_blk(fs->io,
632 fs->group_desc[i].bg_inode_table,
633 fs->inode_blocks_per_group,
635 if (retval) goto errout;
637 io_channel_flush(fs->io);
639 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
640 i - adj + 1, max_group);
644 group_block += fs->super->s_blocks_per_group;
646 io_channel_flush(fs->io);
653 /* --------------------------------------------------------------------
655 * Resize processing, phase 2.
657 * In this phase we adjust determine which blocks need to be moved, in
658 * blocks_to_move(). We then copy the blocks to their ultimate new
659 * destinations using block_mover(). Since we are copying blocks to
660 * their new locations, again during this pass we can abort without
662 * --------------------------------------------------------------------
666 * This helper function creates a block bitmap with all of the
667 * filesystem meta-data blocks.
669 static errcode_t mark_table_blocks(ext2_filsys fs,
670 ext2fs_block_bitmap bmap)
675 unsigned long meta_bg_size;
676 unsigned int old_desc_blocks;
678 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
679 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
680 old_desc_blocks = fs->super->s_first_meta_bg;
682 old_desc_blocks = fs->desc_blocks +
683 fs->super->s_reserved_gdt_blocks;
684 for (i = 0; i < fs->group_desc_count; i++) {
685 ext2fs_reserve_super_and_bgd(fs, i, bmap);
688 * Mark the blocks used for the inode table
690 for (j = 0, b = fs->group_desc[i].bg_inode_table;
691 j < (unsigned int) fs->inode_blocks_per_group;
693 ext2fs_mark_block_bitmap(bmap, b);
696 * Mark block used for the block bitmap
698 ext2fs_mark_block_bitmap(bmap,
699 fs->group_desc[i].bg_block_bitmap);
702 * Mark block used for the inode bitmap
704 ext2fs_mark_block_bitmap(bmap,
705 fs->group_desc[i].bg_inode_bitmap);
711 * This function checks to see if a particular block (either a
712 * superblock or a block group descriptor) overlaps with an inode or
713 * block bitmap block, or with the inode table.
715 static void mark_fs_metablock(ext2_resize_t rfs,
716 ext2fs_block_bitmap meta_bmap,
717 int group, blk_t blk)
719 ext2_filsys fs = rfs->new_fs;
721 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
722 ext2fs_block_alloc_stats(fs, blk, +1);
725 * Check to see if we overlap with the inode or block bitmap,
726 * or the inode tables. If not, and the block is in use, then
727 * mark it as a block to be moved.
729 if (IS_BLOCK_BM(fs, group, blk)) {
730 FS_BLOCK_BM(fs, group) = 0;
731 rfs->needed_blocks++;
732 } else if (IS_INODE_BM(fs, group, blk)) {
733 FS_INODE_BM(fs, group) = 0;
734 rfs->needed_blocks++;
735 } else if (IS_INODE_TB(fs, group, blk)) {
736 FS_INODE_TB(fs, group) = 0;
737 rfs->needed_blocks++;
738 } else if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
739 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
740 (fs->group_desc[group].bg_flags & EXT2_BG_BLOCK_UNINIT)) {
742 * If the block bitmap is uninitialized, which means
743 * nothing other than standard metadata in use.
746 } else if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk) &&
747 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
748 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
749 rfs->needed_blocks++;
755 * This routine marks and unmarks reserved blocks in the new block
756 * bitmap. It also determines which blocks need to be moved and
757 * places this information into the move_blocks bitmap.
759 static errcode_t blocks_to_move(ext2_resize_t rfs)
762 dgrp_t i, max_groups, g;
763 blk_t blk, group_blk;
764 unsigned long old_blocks, new_blocks;
765 unsigned int meta_bg, meta_bg_size;
767 ext2_filsys fs, old_fs;
768 ext2fs_block_bitmap meta_bmap;
769 __u32 save_incompat_flag;
772 old_fs = rfs->old_fs;
773 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
776 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
781 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
786 retval = mark_table_blocks(old_fs, meta_bmap);
793 * If we're shrinking the filesystem, we need to move all of
794 * the blocks that don't fit any more
796 for (blk = fs->super->s_blocks_count;
797 blk < old_fs->super->s_blocks_count; blk++) {
798 g = ext2fs_group_of_blk(fs, blk);
799 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
800 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
801 (old_fs->group_desc[g].bg_flags & EXT2_BG_BLOCK_UNINIT)) {
803 * The block bitmap is uninitialized, so skip
804 * to the next block group.
806 blk = ((g+1) * fs->super->s_blocks_per_group) +
807 fs->super->s_first_data_block - 1;
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, blk);
813 rfs->needed_blocks++;
815 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
818 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
819 old_blocks = old_fs->super->s_first_meta_bg;
820 new_blocks = fs->super->s_first_meta_bg;
822 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
823 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
826 if (old_blocks == new_blocks) {
831 max_groups = fs->group_desc_count;
832 if (max_groups > old_fs->group_desc_count)
833 max_groups = old_fs->group_desc_count;
834 group_blk = old_fs->super->s_first_data_block;
836 * If we're reducing the number of descriptor blocks, this
837 * makes life easy. :-) We just have to mark some extra
840 if (old_blocks > new_blocks) {
841 for (i = 0; i < max_groups; i++) {
842 if (!ext2fs_bg_has_super(fs, i)) {
843 group_blk += fs->super->s_blocks_per_group;
846 for (blk = group_blk+1+new_blocks;
847 blk < group_blk+1+old_blocks; blk++) {
848 ext2fs_block_alloc_stats(fs, blk, -1);
849 rfs->needed_blocks--;
851 group_blk += fs->super->s_blocks_per_group;
857 * If we're increasing the number of descriptor blocks, life
858 * gets interesting....
860 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
861 for (i = 0; i < max_groups; i++) {
862 has_super = ext2fs_bg_has_super(fs, i);
864 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
866 meta_bg = i / meta_bg_size;
867 if (!(fs->super->s_feature_incompat &
868 EXT2_FEATURE_INCOMPAT_META_BG) ||
869 (meta_bg < fs->super->s_first_meta_bg)) {
871 for (blk = group_blk+1;
872 blk < group_blk + 1 + new_blocks; blk++)
873 mark_fs_metablock(rfs, meta_bmap,
879 if (((i % meta_bg_size) == 0) ||
880 ((i % meta_bg_size) == 1) ||
881 ((i % meta_bg_size) == (meta_bg_size-1)))
882 mark_fs_metablock(rfs, meta_bmap, i,
883 group_blk + has_super);
886 if (fs->group_desc[i].bg_inode_table &&
887 fs->group_desc[i].bg_inode_bitmap &&
888 fs->group_desc[i].bg_block_bitmap)
892 * Reserve the existing meta blocks that we know
893 * aren't to be moved.
895 if (fs->group_desc[i].bg_block_bitmap)
896 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
897 fs->group_desc[i].bg_block_bitmap);
898 if (fs->group_desc[i].bg_inode_bitmap)
899 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
900 fs->group_desc[i].bg_inode_bitmap);
901 if (fs->group_desc[i].bg_inode_table)
902 for (blk = fs->group_desc[i].bg_inode_table, j=0;
903 j < fs->inode_blocks_per_group ; j++, blk++)
904 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
908 * Allocate the missing data structures
910 * XXX We have a problem with FLEX_BG and off-line
911 * resizing where we are growing the size of the
912 * filesystem. ext2fs_allocate_group_table() will try
913 * to reserve the inode table in the desired flex_bg
914 * location. However, passing rfs->reserve_blocks
915 * doesn't work since it only has reserved the blocks
916 * that will be used in the new block group -- and
917 * with flex_bg, we can and will allocate the tables
918 * outside of the block group. And we can't pass in
919 * the fs->block_map because it doesn't handle
920 * overlapping inode table movements right. So for
921 * now, we temporarily disable flex_bg to force
922 * ext2fs_allocate_group_tables() to allocate the bg
923 * metadata in side the block group, and the restore
924 * it afterwards. Ugly, until we can fix this up
927 save_incompat_flag = fs->super->s_feature_incompat;
928 fs->super->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_FLEX_BG;
929 retval = ext2fs_allocate_group_table(fs, i,
930 rfs->reserve_blocks);
931 fs->super->s_feature_incompat = save_incompat_flag;
936 * For those structures that have changed, we need to
939 if (FS_BLOCK_BM(old_fs, i) !=
940 (blk = FS_BLOCK_BM(fs, i))) {
941 ext2fs_block_alloc_stats(fs, blk, +1);
942 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
943 !ext2fs_test_block_bitmap(meta_bmap, blk))
944 ext2fs_mark_block_bitmap(rfs->move_blocks,
947 if (FS_INODE_BM(old_fs, i) !=
948 (blk = FS_INODE_BM(fs, i))) {
949 ext2fs_block_alloc_stats(fs, blk, +1);
950 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
951 !ext2fs_test_block_bitmap(meta_bmap, blk))
952 ext2fs_mark_block_bitmap(rfs->move_blocks,
957 * The inode table, if we need to relocate it, is
958 * handled specially. We have to reserve the blocks
959 * for both the old and the new inode table, since we
960 * can't have the inode table be destroyed during the
961 * block relocation phase.
963 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
964 goto next_group; /* inode table not moved */
966 rfs->needed_blocks += fs->inode_blocks_per_group;
969 * Mark the new inode table as in use in the new block
970 * allocation bitmap, and move any blocks that might
973 for (blk = fs->group_desc[i].bg_inode_table, j=0;
974 j < fs->inode_blocks_per_group ; j++, blk++) {
975 ext2fs_block_alloc_stats(fs, blk, +1);
976 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
977 !ext2fs_test_block_bitmap(meta_bmap, blk))
978 ext2fs_mark_block_bitmap(rfs->move_blocks,
983 * Make sure the old inode table is reserved in the
984 * block reservation bitmap.
986 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
987 j < fs->inode_blocks_per_group ; j++, blk++)
988 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
991 group_blk += rfs->new_fs->super->s_blocks_per_group;
997 ext2fs_free_block_bitmap(meta_bmap);
1003 * This helper function tries to allocate a new block. We try to
1004 * avoid hitting the original group descriptor blocks at least at
1005 * first, since we want to make it possible to recover from a badly
1006 * aborted resize operation as much as possible.
1008 * In the future, I may further modify this routine to balance out
1009 * where we get the new blocks across the various block groups.
1010 * Ideally we would allocate blocks that corresponded with the block
1011 * group of the containing inode, and keep contiguous blocks
1012 * together. However, this very difficult to do efficiently, since we
1013 * don't have the necessary information up front.
1017 #define DESPERATION 2
1019 static void init_block_alloc(ext2_resize_t rfs)
1021 rfs->alloc_state = AVOID_OLD;
1022 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1024 /* HACK for testing */
1025 if (rfs->new_fs->super->s_blocks_count >
1026 rfs->old_fs->super->s_blocks_count)
1027 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
1031 static blk_t get_new_block(ext2_resize_t rfs)
1033 ext2_filsys fs = rfs->new_fs;
1036 if (rfs->new_blk >= fs->super->s_blocks_count) {
1037 if (rfs->alloc_state == DESPERATION)
1040 #ifdef RESIZE2FS_DEBUG
1041 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1042 printf("Going into desperation mode "
1043 "for block allocations\n");
1045 rfs->alloc_state = DESPERATION;
1046 rfs->new_blk = fs->super->s_first_data_block;
1049 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
1050 ext2fs_test_block_bitmap(rfs->reserve_blocks,
1052 ((rfs->alloc_state == AVOID_OLD) &&
1053 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
1054 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
1059 return rfs->new_blk;
1063 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1066 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1069 blk = get_new_block(rfs);
1073 #ifdef RESIZE2FS_DEBUG
1074 if (rfs->flags & 0xF)
1075 printf("get_alloc_block allocating %u\n", blk);
1078 ext2fs_mark_block_bitmap(rfs->old_fs->block_map, blk);
1079 ext2fs_mark_block_bitmap(rfs->new_fs->block_map, blk);
1080 *ret = (blk64_t) blk;
1084 static errcode_t block_mover(ext2_resize_t rfs)
1086 blk_t blk, old_blk, new_blk;
1087 ext2_filsys fs = rfs->new_fs;
1088 ext2_filsys old_fs = rfs->old_fs;
1092 ext2_badblocks_list badblock_list = 0;
1093 int bb_modified = 0;
1095 fs->get_alloc_block = resize2fs_get_alloc_block;
1096 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1098 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1102 new_blk = fs->super->s_first_data_block;
1103 if (!rfs->itable_buf) {
1104 retval = ext2fs_get_array(fs->blocksize,
1105 fs->inode_blocks_per_group,
1110 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1115 * The first step is to figure out where all of the blocks
1118 to_move = moved = 0;
1119 init_block_alloc(rfs);
1120 for (blk = old_fs->super->s_first_data_block;
1121 blk < old_fs->super->s_blocks_count; blk++) {
1122 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
1124 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
1126 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1127 ext2fs_badblocks_list_del(badblock_list, blk);
1132 new_blk = get_new_block(rfs);
1137 ext2fs_block_alloc_stats(fs, new_blk, +1);
1138 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
1144 ext2fs_free_extent_table(rfs->bmap);
1152 * Step two is to actually move the blocks
1154 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1155 if (retval) goto errout;
1157 if (rfs->progress) {
1158 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1164 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1165 if (retval) goto errout;
1168 #ifdef RESIZE2FS_DEBUG
1169 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1170 printf("Moving %d blocks %u->%u\n",
1171 size, old_blk, new_blk);
1175 if (c > fs->inode_blocks_per_group)
1176 c = fs->inode_blocks_per_group;
1177 retval = io_channel_read_blk(fs->io, old_blk, c,
1179 if (retval) goto errout;
1180 retval = io_channel_write_blk(fs->io, new_blk, c,
1182 if (retval) goto errout;
1187 if (rfs->progress) {
1188 io_channel_flush(fs->io);
1189 retval = (rfs->progress)(rfs,
1190 E2_RSZ_BLOCK_RELOC_PASS,
1196 io_channel_flush(fs->io);
1200 if (badblock_list) {
1201 if (!retval && bb_modified)
1202 retval = ext2fs_update_bb_inode(old_fs,
1204 ext2fs_badblocks_list_free(badblock_list);
1210 /* --------------------------------------------------------------------
1212 * Resize processing, phase 3
1214 * --------------------------------------------------------------------
1218 struct process_block_struct {
1221 struct ext2_inode * inode;
1227 static int process_block(ext2_filsys fs, blk_t *block_nr,
1228 e2_blkcnt_t blockcnt,
1229 blk_t ref_block EXT2FS_ATTR((unused)),
1230 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1232 struct process_block_struct *pb;
1234 blk_t block, new_block;
1237 pb = (struct process_block_struct *) priv_data;
1239 if (pb->rfs->bmap) {
1240 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1242 *block_nr = new_block;
1243 ret |= BLOCK_CHANGED;
1245 #ifdef RESIZE2FS_DEBUG
1246 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1247 printf("ino=%u, blockcnt=%lld, %u->%u\n",
1248 pb->ino, blockcnt, block, new_block);
1254 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
1255 block, (int) blockcnt);
1267 static errcode_t progress_callback(ext2_filsys fs,
1268 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1269 dgrp_t group, void * priv_data)
1271 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1275 * This check is to protect against old ext2 libraries. It
1276 * shouldn't be needed against new libraries.
1281 if (rfs->progress) {
1282 io_channel_flush(fs->io);
1283 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1284 group+1, fs->group_desc_count);
1292 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1294 struct process_block_struct pb;
1295 ext2_ino_t ino, new_inode;
1296 struct ext2_inode *inode = NULL;
1297 ext2_inode_scan scan = NULL;
1299 char *block_buf = 0;
1300 ext2_ino_t start_to_move;
1301 blk_t orig_size, new_block;
1304 if ((rfs->old_fs->group_desc_count <=
1305 rfs->new_fs->group_desc_count) &&
1310 * Save the original size of the old filesystem, and
1311 * temporarily set the size to be the new size if the new size
1312 * is larger. We need to do this to avoid catching an error
1313 * by the block iterator routines
1315 orig_size = rfs->old_fs->super->s_blocks_count;
1316 if (orig_size < rfs->new_fs->super->s_blocks_count)
1317 rfs->old_fs->super->s_blocks_count =
1318 rfs->new_fs->super->s_blocks_count;
1320 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1321 if (retval) goto errout;
1323 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1324 if (retval) goto errout;
1325 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1326 if (retval) goto errout;
1328 start_to_move = (rfs->new_fs->group_desc_count *
1329 rfs->new_fs->super->s_inodes_per_group);
1331 if (rfs->progress) {
1332 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1333 0, rfs->old_fs->group_desc_count);
1337 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1341 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1342 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1343 inode = malloc(inode_size);
1349 * First, copy all of the inodes that need to be moved
1350 * elsewhere in the inode table
1353 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1354 if (retval) goto errout;
1358 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1359 continue; /* inode not in use */
1361 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1364 if (inode->i_file_acl && rfs->bmap) {
1365 new_block = ext2fs_extent_translate(rfs->bmap,
1368 inode->i_file_acl = new_block;
1369 retval = ext2fs_write_inode_full(rfs->old_fs,
1370 ino, inode, inode_size);
1371 if (retval) goto errout;
1375 if (ext2fs_inode_has_valid_blocks(inode) &&
1376 (rfs->bmap || pb.is_dir)) {
1378 retval = ext2fs_block_iterate2(rfs->old_fs,
1380 process_block, &pb);
1389 if (ino <= start_to_move)
1390 continue; /* Don't need to move it. */
1395 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1399 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1402 /* Get the new version of the inode */
1403 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1405 if (retval) goto errout;
1407 inode->i_ctime = time(0);
1408 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1410 if (retval) goto errout;
1412 #ifdef RESIZE2FS_DEBUG
1413 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1414 printf("Inode moved %u->%u\n", ino, new_inode);
1417 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1421 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1423 io_channel_flush(rfs->old_fs->io);
1426 rfs->old_fs->super->s_blocks_count = orig_size;
1428 ext2fs_free_extent_table(rfs->bmap);
1432 ext2fs_close_inode_scan(scan);
1434 ext2fs_free_mem(&block_buf);
1439 /* --------------------------------------------------------------------
1441 * Resize processing, phase 4.
1443 * --------------------------------------------------------------------
1449 unsigned int max_dirs;
1453 static int check_and_change_inodes(ext2_ino_t dir,
1454 int entry EXT2FS_ATTR((unused)),
1455 struct ext2_dir_entry *dirent, int offset,
1456 int blocksize EXT2FS_ATTR((unused)),
1457 char *buf EXT2FS_ATTR((unused)),
1460 struct istruct *is = (struct istruct *) priv_data;
1461 struct ext2_inode inode;
1462 ext2_ino_t new_inode;
1465 if (is->rfs->progress && offset == 0) {
1466 io_channel_flush(is->rfs->old_fs->io);
1467 is->err = (is->rfs->progress)(is->rfs,
1468 E2_RSZ_INODE_REF_UPD_PASS,
1469 ++is->num, is->max_dirs);
1471 return DIRENT_ABORT;
1477 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1481 #ifdef RESIZE2FS_DEBUG
1482 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1483 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1484 dir, dirent->name_len&0xFF, dirent->name,
1485 dirent->inode, new_inode);
1488 dirent->inode = new_inode;
1490 /* Update the directory mtime and ctime */
1491 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1493 inode.i_mtime = inode.i_ctime = time(0);
1494 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1496 return DIRENT_ABORT;
1499 return DIRENT_CHANGED;
1502 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1511 * Now, we iterate over all of the directories to update the
1515 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1519 if (rfs->progress) {
1520 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1526 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1527 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1528 check_and_change_inodes, &is);
1536 if (rfs->progress && (is.num < is.max_dirs))
1537 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1538 is.max_dirs, is.max_dirs);
1541 ext2fs_free_extent_table(rfs->imap);
1547 /* --------------------------------------------------------------------
1549 * Resize processing, phase 5.
1551 * In this phase we actually move the inode table around, and then
1552 * update the summary statistics. This is scary, since aborting here
1553 * will potentially scramble the filesystem. (We are moving the
1554 * inode tables around in place, and so the potential for lost data,
1555 * or at the very least scrambling the mapping between filenames and
1556 * inode numbers is very high in case of a power failure here.)
1557 * --------------------------------------------------------------------
1562 * A very scary routine --- this one moves the inode table around!!!
1564 * After this you have to use the rfs->new_fs file handle to read and
1567 static errcode_t move_itables(ext2_resize_t rfs)
1569 int n, num, size, diff;
1570 dgrp_t i, max_groups;
1571 ext2_filsys fs = rfs->new_fs;
1573 blk_t old_blk, new_blk, blk;
1575 int j, to_move, moved;
1577 max_groups = fs->group_desc_count;
1578 if (max_groups > rfs->old_fs->group_desc_count)
1579 max_groups = rfs->old_fs->group_desc_count;
1581 size = fs->blocksize * fs->inode_blocks_per_group;
1582 if (!rfs->itable_buf) {
1583 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1589 * Figure out how many inode tables we need to move
1591 to_move = moved = 0;
1592 for (i=0; i < max_groups; i++)
1593 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1594 fs->group_desc[i].bg_inode_table)
1600 if (rfs->progress) {
1601 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1607 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1609 for (i=0; i < max_groups; i++) {
1610 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1611 new_blk = fs->group_desc[i].bg_inode_table;
1612 diff = new_blk - old_blk;
1614 #ifdef RESIZE2FS_DEBUG
1615 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1616 printf("Itable move group %d block %u->%u (diff %d)\n",
1617 i, old_blk, new_blk, diff);
1623 retval = io_channel_read_blk(fs->io, old_blk,
1624 fs->inode_blocks_per_group,
1629 * The end of the inode table segment often contains
1630 * all zeros, and we're often only moving the inode
1631 * table down a block or two. If so, we can optimize
1632 * things by not rewriting blocks that we know to be zero
1635 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1638 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1639 #ifdef RESIZE2FS_DEBUG
1640 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1641 printf("%d blocks of zeros...\n", n);
1643 num = fs->inode_blocks_per_group;
1647 retval = io_channel_write_blk(fs->io, new_blk,
1648 num, rfs->itable_buf);
1650 io_channel_write_blk(fs->io, old_blk,
1651 num, rfs->itable_buf);
1655 retval = io_channel_write_blk(fs->io,
1656 old_blk + fs->inode_blocks_per_group,
1657 diff, (rfs->itable_buf +
1658 (fs->inode_blocks_per_group - diff) *
1664 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1665 j < fs->inode_blocks_per_group ; j++, blk++)
1666 ext2fs_block_alloc_stats(fs, blk, -1);
1668 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1669 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1670 ext2fs_mark_super_dirty(rfs->old_fs);
1671 ext2fs_flush(rfs->old_fs);
1673 if (rfs->progress) {
1674 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1680 mark_table_blocks(fs, fs->block_map);
1682 #ifdef RESIZE2FS_DEBUG
1683 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1684 printf("Inode table move finished.\n");
1693 * Fix the resize inode
1695 static errcode_t fix_resize_inode(ext2_filsys fs)
1697 struct ext2_inode inode;
1702 if (!(fs->super->s_feature_compat &
1703 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1706 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1707 if (retval) goto errout;
1709 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1710 if (retval) goto errout;
1712 if (fs->super->s_reserved_gdt_blocks == 0) {
1713 fs->super->s_feature_compat &=
1714 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1715 ext2fs_mark_super_dirty(fs);
1717 if ((blk = inode.i_block[EXT2_DIND_BLOCK]) != 0)
1718 ext2fs_block_alloc_stats(fs, blk, -1);
1720 memset(&inode, 0, sizeof(inode));
1722 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1726 ext2fs_iblk_set(fs, &inode, 1);
1728 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1729 if (retval) goto errout;
1731 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1733 * Avoid zeroing out block #0; that's rude. This
1734 * should never happen anyway since the filesystem
1735 * should be fsck'ed and we assume it is consistent.
1738 _("Should never happen: resize inode corrupt!\n"));
1742 memset(block_buf, 0, fs->blocksize);
1744 retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1746 if (retval) goto errout;
1748 retval = ext2fs_create_resize_inode(fs);
1754 ext2fs_free_mem(&block_buf);
1759 * Finally, recalculate the summary information
1761 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1765 unsigned int group = 0;
1766 unsigned int count = 0;
1770 blk_t super_blk, old_desc_blk, new_desc_blk;
1771 int old_desc_blocks;
1774 * First calculate the block statistics
1776 uninit = fs->group_desc[group].bg_flags & EXT2_BG_BLOCK_UNINIT;
1777 ext2fs_super_and_bgd_loc(fs, group, &super_blk, &old_desc_blk,
1779 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1780 old_desc_blocks = fs->super->s_first_meta_bg;
1782 old_desc_blocks = fs->desc_blocks +
1783 fs->super->s_reserved_gdt_blocks;
1784 for (blk = fs->super->s_first_data_block;
1785 blk < fs->super->s_blocks_count; blk++) {
1787 !((blk == super_blk) ||
1788 ((old_desc_blk && old_desc_blocks &&
1789 (blk >= old_desc_blk) &&
1790 (blk < old_desc_blk + old_desc_blocks))) ||
1791 ((new_desc_blk && (blk == new_desc_blk))) ||
1792 (blk == fs->group_desc[group].bg_block_bitmap) ||
1793 (blk == fs->group_desc[group].bg_inode_bitmap) ||
1794 ((blk >= fs->group_desc[group].bg_inode_table &&
1795 (blk < fs->group_desc[group].bg_inode_table
1796 + fs->inode_blocks_per_group))))) ||
1797 (!ext2fs_fast_test_block_bitmap(fs->block_map, blk))) {
1802 if ((count == fs->super->s_blocks_per_group) ||
1803 (blk == fs->super->s_blocks_count-1)) {
1804 fs->group_desc[group].bg_free_blocks_count =
1806 ext2fs_group_desc_csum_set(fs, group);
1810 uninit = (fs->group_desc[group].bg_flags &
1811 EXT2_BG_BLOCK_UNINIT);
1812 ext2fs_super_and_bgd_loc(fs, group, &super_blk,
1815 if (fs->super->s_feature_incompat &
1816 EXT2_FEATURE_INCOMPAT_META_BG)
1817 old_desc_blocks = fs->super->s_first_meta_bg;
1819 old_desc_blocks = fs->desc_blocks +
1820 fs->super->s_reserved_gdt_blocks;
1823 fs->super->s_free_blocks_count = total_free;
1826 * Next, calculate the inode statistics
1833 /* Protect loop from wrap-around if s_inodes_count maxed */
1834 uninit = fs->group_desc[group].bg_flags & EXT2_BG_INODE_UNINIT;
1835 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1837 !ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1842 if ((count == fs->super->s_inodes_per_group) ||
1843 (ino == fs->super->s_inodes_count)) {
1844 fs->group_desc[group].bg_free_inodes_count =
1846 ext2fs_group_desc_csum_set(fs, group);
1850 uninit = (fs->group_desc[group].bg_flags &
1851 EXT2_BG_INODE_UNINIT);
1854 fs->super->s_free_inodes_count = total_free;
1855 ext2fs_mark_super_dirty(fs);
1860 * calcluate the minimum number of blocks the given fs can be resized to
1862 blk_t calculate_minimum_resize_size(ext2_filsys fs)
1864 blk_t inode_count, blks_needed, groups, data_blocks;
1865 blk_t grp, data_needed, last_start;
1866 int overhead = 0, num_of_superblocks = 0;
1869 * first figure out how many group descriptors we need to
1870 * handle the number of inodes we have
1872 inode_count = fs->super->s_inodes_count -
1873 fs->super->s_free_inodes_count;
1874 blks_needed = ext2fs_div_ceil(inode_count,
1875 fs->super->s_inodes_per_group) *
1876 EXT2_BLOCKS_PER_GROUP(fs->super);
1877 groups = ext2fs_div_ceil(blks_needed,
1878 EXT2_BLOCKS_PER_GROUP(fs->super));
1881 * we need to figure out how many backup superblocks we have so we can
1882 * account for that in the metadata
1884 for (grp = 0; grp < fs->group_desc_count; grp++) {
1885 if (ext2fs_bg_has_super(fs, grp))
1886 num_of_superblocks++;
1889 /* calculate how many blocks are needed for data */
1890 data_needed = fs->super->s_blocks_count -
1891 fs->super->s_free_blocks_count;
1892 data_needed -= SUPER_OVERHEAD(fs) * num_of_superblocks;
1893 data_needed -= META_OVERHEAD(fs) * fs->group_desc_count;
1895 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1897 * For ext4 we need to allow for up to a flex_bg worth
1898 * of inode tables of slack space so the resize
1899 * operation can be guaranteed to finish.
1901 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1904 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
1905 data_needed += META_OVERHEAD(fs) * extra_groups;
1909 * figure out how many data blocks we have given the number of groups
1910 * we need for our inodes
1912 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
1914 for (grp = 0; grp < groups; grp++) {
1915 overhead = META_OVERHEAD(fs);
1917 if (ext2fs_bg_has_super(fs, grp))
1918 overhead += SUPER_OVERHEAD(fs);
1921 * we want to keep track of how much data we can store in
1922 * the groups leading up to the last group so we can determine
1923 * how big the last group needs to be
1925 if (grp != (groups - 1))
1926 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
1929 data_blocks -= overhead;
1933 * if we need more group descriptors in order to accomodate our data
1934 * then we need to add them here
1936 while (data_needed > data_blocks) {
1937 blk_t remainder = data_needed - data_blocks;
1940 /* figure out how many more groups we need for the data */
1941 extra_grps = ext2fs_div_ceil(remainder,
1942 EXT2_BLOCKS_PER_GROUP(fs->super));
1944 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
1946 /* ok we have to account for the last group */
1947 overhead = META_OVERHEAD(fs);
1948 if (ext2fs_bg_has_super(fs, groups-1))
1949 overhead += SUPER_OVERHEAD(fs);
1950 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
1952 for (grp = groups; grp < groups+extra_grps; grp++) {
1953 overhead = META_OVERHEAD(fs);
1954 if (ext2fs_bg_has_super(fs, grp))
1955 overhead += SUPER_OVERHEAD(fs);
1958 * again, we need to see how much data we cram into
1959 * all of the groups leading up to the last group
1961 if (grp != (groups + extra_grps - 1))
1962 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
1965 data_blocks -= overhead;
1968 groups += extra_grps;
1971 /* now for the fun voodoo */
1972 overhead = META_OVERHEAD(fs);
1975 * if this is the case then the last group is going to have data in it
1976 * so we need to adjust the size of the last group accordingly
1978 if (last_start < data_needed) {
1979 blk_t remainder = data_needed - last_start;
1982 * 50 is a magic number that mkfs/resize uses to see if its
1983 * even worth making/resizing the fs. basically you need to
1984 * have at least 50 blocks in addition to the blocks needed
1985 * for the metadata in the last group
1988 overhead += remainder;
1994 if (ext2fs_bg_has_super(fs, groups-1))
1995 overhead += SUPER_OVERHEAD(fs);
1998 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
1999 * only do groups-1, and then add the number of blocks needed to
2000 * handle the group descriptor metadata+data that we need
2002 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2003 blks_needed += overhead;
2006 * We need to reserve a few extra blocks if extents are
2007 * enabled, in case we need to grow the extent tree. The more
2008 * we shrink the file system, the more space we need.
2010 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
2011 blks_needed += (fs->super->s_blocks_count - blks_needed)/500;