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 if (rfs->reserve_blocks)
162 ext2fs_free_block_bitmap(rfs->reserve_blocks);
163 if (rfs->move_blocks)
164 ext2fs_free_block_bitmap(rfs->move_blocks);
165 ext2fs_free_mem(&rfs);
171 ext2fs_free(rfs->new_fs);
173 ext2fs_free_mem(&rfs->itable_buf);
174 ext2fs_free_mem(&rfs);
179 * Clean up the bitmaps for unitialized bitmaps
181 static void fix_uninit_block_bitmaps(ext2_filsys fs)
183 blk_t i, blk, super_blk, old_desc_blk, new_desc_blk;
187 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
188 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
191 for (g=0; g < fs->group_desc_count; g++) {
192 if (!(fs->group_desc[g].bg_flags & EXT2_BG_BLOCK_UNINIT))
195 blk = (g * fs->super->s_blocks_per_group) +
196 fs->super->s_first_data_block;
198 ext2fs_super_and_bgd_loc(fs, g, &super_blk,
199 &old_desc_blk, &new_desc_blk, 0);
201 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
202 old_desc_blocks = fs->super->s_first_meta_bg;
204 old_desc_blocks = fs->desc_blocks +
205 fs->super->s_reserved_gdt_blocks;
207 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
208 if (blk >= fs->super->s_blocks_count)
210 if ((blk == super_blk) ||
211 (old_desc_blk && old_desc_blocks &&
212 (blk >= old_desc_blk) &&
213 (blk < old_desc_blk + old_desc_blocks)) ||
214 (new_desc_blk && (blk == new_desc_blk)) ||
215 (blk == fs->group_desc[g].bg_block_bitmap) ||
216 (blk == fs->group_desc[g].bg_inode_bitmap) ||
217 (blk >= fs->group_desc[g].bg_inode_table &&
218 (blk < fs->group_desc[g].bg_inode_table
219 + fs->inode_blocks_per_group)))
220 ext2fs_fast_mark_block_bitmap(fs->block_map, blk);
222 ext2fs_fast_unmark_block_bitmap(fs->block_map, blk);
227 /* --------------------------------------------------------------------
229 * Resize processing, phase 1.
231 * In this phase we adjust the in-memory superblock information, and
232 * initialize any new parts of the inode table. The new parts of the
233 * inode table are created in virgin disk space, so we can abort here
234 * without any side effects.
235 * --------------------------------------------------------------------
239 * If the group descriptor's bitmap and inode table blocks are valid,
240 * release them in the new filesystem data structure, and mark them as
241 * reserved so the old inode table blocks don't get overwritten.
243 static void free_gdp_blocks(ext2_filsys fs,
244 ext2fs_block_bitmap reserve_blocks,
245 struct ext2_group_desc *gdp)
250 if (gdp->bg_block_bitmap &&
251 (gdp->bg_block_bitmap < fs->super->s_blocks_count)) {
252 ext2fs_block_alloc_stats(fs, gdp->bg_block_bitmap, -1);
253 ext2fs_mark_block_bitmap(reserve_blocks,
254 gdp->bg_block_bitmap);
257 if (gdp->bg_inode_bitmap &&
258 (gdp->bg_inode_bitmap < fs->super->s_blocks_count)) {
259 ext2fs_block_alloc_stats(fs, gdp->bg_inode_bitmap, -1);
260 ext2fs_mark_block_bitmap(reserve_blocks,
261 gdp->bg_inode_bitmap);
264 if (gdp->bg_inode_table == 0 ||
265 (gdp->bg_inode_table >= fs->super->s_blocks_count))
268 for (blk = gdp->bg_inode_table, j = 0;
269 j < fs->inode_blocks_per_group; j++, blk++) {
270 if (blk >= fs->super->s_blocks_count)
272 ext2fs_block_alloc_stats(fs, blk, -1);
273 ext2fs_mark_block_bitmap(reserve_blocks, blk);
278 * This routine is shared by the online and offline resize routines.
279 * All of the information which is adjusted in memory is done here.
281 * The reserve_blocks parameter is only needed when shrinking the
284 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
285 ext2fs_block_bitmap reserve_blocks, blk_t new_size)
290 blk_t blk, group_block;
292 int adj, old_numblocks, numblocks, adjblocks;
293 unsigned long i, j, old_desc_blocks, max_group;
294 unsigned int meta_bg, meta_bg_size;
295 int has_super, csum_flag;
296 unsigned long long new_inodes; /* u64 to check for overflow */
299 fs->super->s_blocks_count = new_size;
302 fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
303 fs->super->s_first_data_block,
304 EXT2_BLOCKS_PER_GROUP(fs->super));
305 if (fs->group_desc_count == 0)
306 return EXT2_ET_TOOSMALL;
307 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
308 EXT2_DESC_PER_BLOCK(fs->super));
311 * Overhead is the number of bookkeeping blocks per group. It
312 * includes the superblock backup, the group descriptor
313 * backups, the inode bitmap, the block bitmap, and the inode
316 overhead = (int) (2 + fs->inode_blocks_per_group);
318 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
319 overhead += 1 + fs->desc_blocks +
320 fs->super->s_reserved_gdt_blocks;
323 * See if the last group is big enough to support the
324 * necessary data structures. If not, we need to get rid of
327 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
328 fs->super->s_blocks_per_group;
329 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
330 return EXT2_ET_TOOSMALL;
331 if (rem && (rem < overhead+50)) {
332 fs->super->s_blocks_count -= rem;
336 * Adjust the number of inodes
338 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
339 if (new_inodes > ~0U) {
340 fprintf(stderr, _("inodes (%llu) must be less than %u"),
342 return EXT2_ET_TOO_MANY_INODES;
344 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
345 fs->group_desc_count;
348 * Adjust the number of free blocks
350 blk = old_fs->super->s_blocks_count;
351 if (blk > fs->super->s_blocks_count)
352 fs->super->s_free_blocks_count -=
353 (blk - fs->super->s_blocks_count);
355 fs->super->s_free_blocks_count +=
356 (fs->super->s_blocks_count - blk);
359 * Adjust the number of reserved blocks
361 percent = (old_fs->super->s_r_blocks_count * 100.0) /
362 old_fs->super->s_blocks_count;
363 fs->super->s_r_blocks_count = (unsigned int) (percent *
364 fs->super->s_blocks_count / 100.0);
367 * Adjust the bitmaps for size
369 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
370 fs->super->s_inodes_count,
372 if (retval) goto errout;
374 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
375 * fs->group_desc_count)) - 1 +
376 fs->super->s_first_data_block;
377 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
378 real_end, fs->block_map);
380 if (retval) goto errout;
383 * Reallocate the group descriptors as necessary.
385 if (old_fs->desc_blocks != fs->desc_blocks) {
386 retval = ext2fs_resize_mem(old_fs->desc_blocks *
388 fs->desc_blocks * fs->blocksize,
392 if (fs->desc_blocks > old_fs->desc_blocks)
393 memset((char *) fs->group_desc +
394 (old_fs->desc_blocks * fs->blocksize), 0,
395 (fs->desc_blocks - old_fs->desc_blocks) *
400 * If the resize_inode feature is set, and we are changing the
401 * number of descriptor blocks, then adjust
402 * s_reserved_gdt_blocks if possible to avoid needing to move
403 * the inode table either now or in the future.
405 if ((fs->super->s_feature_compat &
406 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
407 (old_fs->desc_blocks != fs->desc_blocks)) {
410 new = ((int) fs->super->s_reserved_gdt_blocks) +
411 (old_fs->desc_blocks - fs->desc_blocks);
414 if (new > (int) fs->blocksize/4)
415 new = fs->blocksize/4;
416 fs->super->s_reserved_gdt_blocks = new;
420 * If we are shrinking the number of block groups, we're done
423 if (old_fs->group_desc_count > fs->group_desc_count) {
425 * Check the block groups that we are chopping off
426 * and free any blocks associated with their metadata
428 for (i = fs->group_desc_count;
429 i < old_fs->group_desc_count; i++) {
430 free_gdp_blocks(fs, reserve_blocks,
431 &old_fs->group_desc[i]);
438 * Fix the count of the last (old) block group
440 old_numblocks = (old_fs->super->s_blocks_count -
441 old_fs->super->s_first_data_block) %
442 old_fs->super->s_blocks_per_group;
444 old_numblocks = old_fs->super->s_blocks_per_group;
445 if (old_fs->group_desc_count == fs->group_desc_count) {
446 numblocks = (fs->super->s_blocks_count -
447 fs->super->s_first_data_block) %
448 fs->super->s_blocks_per_group;
450 numblocks = fs->super->s_blocks_per_group;
452 numblocks = fs->super->s_blocks_per_group;
453 i = old_fs->group_desc_count - 1;
454 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
455 ext2fs_group_desc_csum_set(fs, i);
458 * If the number of block groups is staying the same, we're
459 * done and can exit now. (If the number block groups is
460 * shrinking, we had exited earlier.)
462 if (old_fs->group_desc_count >= fs->group_desc_count) {
468 * Initialize the new block group descriptors
470 group_block = fs->super->s_first_data_block +
471 old_fs->group_desc_count * fs->super->s_blocks_per_group;
473 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
474 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
475 adj = old_fs->group_desc_count;
476 max_group = fs->group_desc_count - adj;
477 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
478 old_desc_blocks = fs->super->s_first_meta_bg;
480 old_desc_blocks = fs->desc_blocks +
481 fs->super->s_reserved_gdt_blocks;
482 for (i = old_fs->group_desc_count;
483 i < fs->group_desc_count; i++) {
484 memset(&fs->group_desc[i], 0,
485 sizeof(struct ext2_group_desc));
488 fs->group_desc[i].bg_flags = 0;
490 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_UNINIT |
491 EXT2_BG_INODE_ZEROED;
492 if (i == fs->group_desc_count-1) {
493 numblocks = (fs->super->s_blocks_count -
494 fs->super->s_first_data_block) %
495 fs->super->s_blocks_per_group;
497 numblocks = fs->super->s_blocks_per_group;
499 numblocks = fs->super->s_blocks_per_group;
501 fs->group_desc[i].bg_flags |=
502 EXT2_BG_BLOCK_UNINIT;
505 has_super = ext2fs_bg_has_super(fs, i);
507 ext2fs_block_alloc_stats(fs, group_block, +1);
510 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
511 meta_bg = i / meta_bg_size;
512 if (!(fs->super->s_feature_incompat &
513 EXT2_FEATURE_INCOMPAT_META_BG) ||
514 (meta_bg < fs->super->s_first_meta_bg)) {
516 for (j=0; j < old_desc_blocks; j++)
517 ext2fs_block_alloc_stats(fs,
518 group_block + 1 + j, +1);
519 adjblocks += old_desc_blocks;
524 if (((i % meta_bg_size) == 0) ||
525 ((i % meta_bg_size) == 1) ||
526 ((i % meta_bg_size) == (meta_bg_size-1)))
527 ext2fs_block_alloc_stats(fs,
528 group_block + has_super, +1);
531 adjblocks += 2 + fs->inode_blocks_per_group;
533 numblocks -= adjblocks;
534 fs->super->s_free_blocks_count -= adjblocks;
535 fs->super->s_free_inodes_count +=
536 fs->super->s_inodes_per_group;
537 fs->group_desc[i].bg_free_blocks_count = numblocks;
538 fs->group_desc[i].bg_free_inodes_count =
539 fs->super->s_inodes_per_group;
540 fs->group_desc[i].bg_used_dirs_count = 0;
541 ext2fs_group_desc_csum_set(fs, i);
543 retval = ext2fs_allocate_group_table(fs, i, 0);
544 if (retval) goto errout;
546 group_block += fs->super->s_blocks_per_group;
555 * This routine adjusts the superblock and other data structures, both
556 * in disk as well as in memory...
558 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
565 unsigned long max_group;
568 ext2fs_mark_super_dirty(fs);
569 ext2fs_mark_bb_dirty(fs);
570 ext2fs_mark_ib_dirty(fs);
572 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
573 &rfs->reserve_blocks);
577 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
582 * Check to make sure there are enough inodes
584 if ((rfs->old_fs->super->s_inodes_count -
585 rfs->old_fs->super->s_free_inodes_count) >
586 rfs->new_fs->super->s_inodes_count) {
592 * If we are shrinking the number block groups, we're done and
595 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
601 * If the number of block groups is staying the same, we're
602 * done and can exit now. (If the number block groups is
603 * shrinking, we had exited earlier.)
605 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
611 * Initialize the new block group descriptors
613 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
618 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
619 group_block = fs->super->s_first_data_block +
620 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
622 adj = rfs->old_fs->group_desc_count;
623 max_group = fs->group_desc_count - adj;
625 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
630 for (i = rfs->old_fs->group_desc_count;
631 i < fs->group_desc_count; i++) {
633 * Write out the new inode table
635 retval = io_channel_write_blk(fs->io,
636 fs->group_desc[i].bg_inode_table,
637 fs->inode_blocks_per_group,
639 if (retval) goto errout;
641 io_channel_flush(fs->io);
643 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
644 i - adj + 1, max_group);
648 group_block += fs->super->s_blocks_per_group;
650 io_channel_flush(fs->io);
657 /* --------------------------------------------------------------------
659 * Resize processing, phase 2.
661 * In this phase we adjust determine which blocks need to be moved, in
662 * blocks_to_move(). We then copy the blocks to their ultimate new
663 * destinations using block_mover(). Since we are copying blocks to
664 * their new locations, again during this pass we can abort without
666 * --------------------------------------------------------------------
670 * This helper function creates a block bitmap with all of the
671 * filesystem meta-data blocks.
673 static errcode_t mark_table_blocks(ext2_filsys fs,
674 ext2fs_block_bitmap bmap)
679 unsigned long meta_bg_size;
680 unsigned int old_desc_blocks;
682 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
683 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
684 old_desc_blocks = fs->super->s_first_meta_bg;
686 old_desc_blocks = fs->desc_blocks +
687 fs->super->s_reserved_gdt_blocks;
688 for (i = 0; i < fs->group_desc_count; i++) {
689 ext2fs_reserve_super_and_bgd(fs, i, bmap);
692 * Mark the blocks used for the inode table
694 for (j = 0, b = fs->group_desc[i].bg_inode_table;
695 j < (unsigned int) fs->inode_blocks_per_group;
697 ext2fs_mark_block_bitmap(bmap, b);
700 * Mark block used for the block bitmap
702 ext2fs_mark_block_bitmap(bmap,
703 fs->group_desc[i].bg_block_bitmap);
706 * Mark block used for the inode bitmap
708 ext2fs_mark_block_bitmap(bmap,
709 fs->group_desc[i].bg_inode_bitmap);
715 * This function checks to see if a particular block (either a
716 * superblock or a block group descriptor) overlaps with an inode or
717 * block bitmap block, or with the inode table.
719 static void mark_fs_metablock(ext2_resize_t rfs,
720 ext2fs_block_bitmap meta_bmap,
721 int group, blk_t blk)
723 ext2_filsys fs = rfs->new_fs;
725 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
726 ext2fs_block_alloc_stats(fs, blk, +1);
729 * Check to see if we overlap with the inode or block bitmap,
730 * or the inode tables. If not, and the block is in use, then
731 * mark it as a block to be moved.
733 if (IS_BLOCK_BM(fs, group, blk)) {
734 FS_BLOCK_BM(fs, group) = 0;
735 rfs->needed_blocks++;
736 } else if (IS_INODE_BM(fs, group, blk)) {
737 FS_INODE_BM(fs, group) = 0;
738 rfs->needed_blocks++;
739 } else if (IS_INODE_TB(fs, group, blk)) {
740 FS_INODE_TB(fs, group) = 0;
741 rfs->needed_blocks++;
742 } else if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
743 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
744 (fs->group_desc[group].bg_flags & EXT2_BG_BLOCK_UNINIT)) {
746 * If the block bitmap is uninitialized, which means
747 * nothing other than standard metadata in use.
750 } else if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk) &&
751 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
752 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
753 rfs->needed_blocks++;
759 * This routine marks and unmarks reserved blocks in the new block
760 * bitmap. It also determines which blocks need to be moved and
761 * places this information into the move_blocks bitmap.
763 static errcode_t blocks_to_move(ext2_resize_t rfs)
766 dgrp_t i, max_groups, g;
767 blk_t blk, group_blk;
768 unsigned long old_blocks, new_blocks;
769 unsigned int meta_bg, meta_bg_size;
771 ext2_filsys fs, old_fs;
772 ext2fs_block_bitmap meta_bmap;
773 __u32 save_incompat_flag;
776 old_fs = rfs->old_fs;
777 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
780 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
785 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
790 retval = mark_table_blocks(old_fs, meta_bmap);
797 * If we're shrinking the filesystem, we need to move all of
798 * the blocks that don't fit any more
800 for (blk = fs->super->s_blocks_count;
801 blk < old_fs->super->s_blocks_count; blk++) {
802 g = ext2fs_group_of_blk(fs, blk);
803 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
804 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
805 (old_fs->group_desc[g].bg_flags & EXT2_BG_BLOCK_UNINIT)) {
807 * The block bitmap is uninitialized, so skip
808 * to the next block group.
810 blk = ((g+1) * fs->super->s_blocks_per_group) +
811 fs->super->s_first_data_block - 1;
814 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
815 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
816 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
817 rfs->needed_blocks++;
819 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
822 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
823 old_blocks = old_fs->super->s_first_meta_bg;
824 new_blocks = fs->super->s_first_meta_bg;
826 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
827 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
830 if (old_blocks == new_blocks) {
835 max_groups = fs->group_desc_count;
836 if (max_groups > old_fs->group_desc_count)
837 max_groups = old_fs->group_desc_count;
838 group_blk = old_fs->super->s_first_data_block;
840 * If we're reducing the number of descriptor blocks, this
841 * makes life easy. :-) We just have to mark some extra
844 if (old_blocks > new_blocks) {
845 for (i = 0; i < max_groups; i++) {
846 if (!ext2fs_bg_has_super(fs, i)) {
847 group_blk += fs->super->s_blocks_per_group;
850 for (blk = group_blk+1+new_blocks;
851 blk < group_blk+1+old_blocks; blk++) {
852 ext2fs_block_alloc_stats(fs, blk, -1);
853 rfs->needed_blocks--;
855 group_blk += fs->super->s_blocks_per_group;
861 * If we're increasing the number of descriptor blocks, life
862 * gets interesting....
864 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
865 for (i = 0; i < max_groups; i++) {
866 has_super = ext2fs_bg_has_super(fs, i);
868 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
870 meta_bg = i / meta_bg_size;
871 if (!(fs->super->s_feature_incompat &
872 EXT2_FEATURE_INCOMPAT_META_BG) ||
873 (meta_bg < fs->super->s_first_meta_bg)) {
875 for (blk = group_blk+1;
876 blk < group_blk + 1 + new_blocks; blk++)
877 mark_fs_metablock(rfs, meta_bmap,
883 if (((i % meta_bg_size) == 0) ||
884 ((i % meta_bg_size) == 1) ||
885 ((i % meta_bg_size) == (meta_bg_size-1)))
886 mark_fs_metablock(rfs, meta_bmap, i,
887 group_blk + has_super);
890 if (fs->group_desc[i].bg_inode_table &&
891 fs->group_desc[i].bg_inode_bitmap &&
892 fs->group_desc[i].bg_block_bitmap)
896 * Reserve the existing meta blocks that we know
897 * aren't to be moved.
899 if (fs->group_desc[i].bg_block_bitmap)
900 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
901 fs->group_desc[i].bg_block_bitmap);
902 if (fs->group_desc[i].bg_inode_bitmap)
903 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
904 fs->group_desc[i].bg_inode_bitmap);
905 if (fs->group_desc[i].bg_inode_table)
906 for (blk = fs->group_desc[i].bg_inode_table, j=0;
907 j < fs->inode_blocks_per_group ; j++, blk++)
908 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
912 * Allocate the missing data structures
914 * XXX We have a problem with FLEX_BG and off-line
915 * resizing where we are growing the size of the
916 * filesystem. ext2fs_allocate_group_table() will try
917 * to reserve the inode table in the desired flex_bg
918 * location. However, passing rfs->reserve_blocks
919 * doesn't work since it only has reserved the blocks
920 * that will be used in the new block group -- and
921 * with flex_bg, we can and will allocate the tables
922 * outside of the block group. And we can't pass in
923 * the fs->block_map because it doesn't handle
924 * overlapping inode table movements right. So for
925 * now, we temporarily disable flex_bg to force
926 * ext2fs_allocate_group_tables() to allocate the bg
927 * metadata in side the block group, and the restore
928 * it afterwards. Ugly, until we can fix this up
931 save_incompat_flag = fs->super->s_feature_incompat;
932 fs->super->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_FLEX_BG;
933 retval = ext2fs_allocate_group_table(fs, i,
934 rfs->reserve_blocks);
935 fs->super->s_feature_incompat = save_incompat_flag;
940 * For those structures that have changed, we need to
943 if (FS_BLOCK_BM(old_fs, i) !=
944 (blk = FS_BLOCK_BM(fs, i))) {
945 ext2fs_block_alloc_stats(fs, blk, +1);
946 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
947 !ext2fs_test_block_bitmap(meta_bmap, blk))
948 ext2fs_mark_block_bitmap(rfs->move_blocks,
951 if (FS_INODE_BM(old_fs, i) !=
952 (blk = FS_INODE_BM(fs, i))) {
953 ext2fs_block_alloc_stats(fs, blk, +1);
954 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
955 !ext2fs_test_block_bitmap(meta_bmap, blk))
956 ext2fs_mark_block_bitmap(rfs->move_blocks,
961 * The inode table, if we need to relocate it, is
962 * handled specially. We have to reserve the blocks
963 * for both the old and the new inode table, since we
964 * can't have the inode table be destroyed during the
965 * block relocation phase.
967 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
968 goto next_group; /* inode table not moved */
970 rfs->needed_blocks += fs->inode_blocks_per_group;
973 * Mark the new inode table as in use in the new block
974 * allocation bitmap, and move any blocks that might
977 for (blk = fs->group_desc[i].bg_inode_table, j=0;
978 j < fs->inode_blocks_per_group ; j++, blk++) {
979 ext2fs_block_alloc_stats(fs, blk, +1);
980 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
981 !ext2fs_test_block_bitmap(meta_bmap, blk))
982 ext2fs_mark_block_bitmap(rfs->move_blocks,
987 * Make sure the old inode table is reserved in the
988 * block reservation bitmap.
990 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
991 j < fs->inode_blocks_per_group ; j++, blk++)
992 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
995 group_blk += rfs->new_fs->super->s_blocks_per_group;
1001 ext2fs_free_block_bitmap(meta_bmap);
1007 * This helper function tries to allocate a new block. We try to
1008 * avoid hitting the original group descriptor blocks at least at
1009 * first, since we want to make it possible to recover from a badly
1010 * aborted resize operation as much as possible.
1012 * In the future, I may further modify this routine to balance out
1013 * where we get the new blocks across the various block groups.
1014 * Ideally we would allocate blocks that corresponded with the block
1015 * group of the containing inode, and keep contiguous blocks
1016 * together. However, this very difficult to do efficiently, since we
1017 * don't have the necessary information up front.
1021 #define DESPERATION 2
1023 static void init_block_alloc(ext2_resize_t rfs)
1025 rfs->alloc_state = AVOID_OLD;
1026 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1028 /* HACK for testing */
1029 if (rfs->new_fs->super->s_blocks_count >
1030 rfs->old_fs->super->s_blocks_count)
1031 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
1035 static blk_t get_new_block(ext2_resize_t rfs)
1037 ext2_filsys fs = rfs->new_fs;
1040 if (rfs->new_blk >= fs->super->s_blocks_count) {
1041 if (rfs->alloc_state == DESPERATION)
1044 #ifdef RESIZE2FS_DEBUG
1045 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1046 printf("Going into desperation mode "
1047 "for block allocations\n");
1049 rfs->alloc_state = DESPERATION;
1050 rfs->new_blk = fs->super->s_first_data_block;
1053 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
1054 ext2fs_test_block_bitmap(rfs->reserve_blocks,
1056 ((rfs->alloc_state == AVOID_OLD) &&
1057 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
1058 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
1063 return rfs->new_blk;
1067 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1070 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1073 blk = get_new_block(rfs);
1077 #ifdef RESIZE2FS_DEBUG
1078 if (rfs->flags & 0xF)
1079 printf("get_alloc_block allocating %u\n", blk);
1082 ext2fs_mark_block_bitmap(rfs->old_fs->block_map, blk);
1083 ext2fs_mark_block_bitmap(rfs->new_fs->block_map, blk);
1084 *ret = (blk64_t) blk;
1088 static errcode_t block_mover(ext2_resize_t rfs)
1090 blk_t blk, old_blk, new_blk;
1091 ext2_filsys fs = rfs->new_fs;
1092 ext2_filsys old_fs = rfs->old_fs;
1096 ext2_badblocks_list badblock_list = 0;
1097 int bb_modified = 0;
1099 fs->get_alloc_block = resize2fs_get_alloc_block;
1100 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1102 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1106 new_blk = fs->super->s_first_data_block;
1107 if (!rfs->itable_buf) {
1108 retval = ext2fs_get_array(fs->blocksize,
1109 fs->inode_blocks_per_group,
1114 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1119 * The first step is to figure out where all of the blocks
1122 to_move = moved = 0;
1123 init_block_alloc(rfs);
1124 for (blk = old_fs->super->s_first_data_block;
1125 blk < old_fs->super->s_blocks_count; blk++) {
1126 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
1128 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
1130 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1131 ext2fs_badblocks_list_del(badblock_list, blk);
1136 new_blk = get_new_block(rfs);
1141 ext2fs_block_alloc_stats(fs, new_blk, +1);
1142 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
1148 ext2fs_free_extent_table(rfs->bmap);
1156 * Step two is to actually move the blocks
1158 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1159 if (retval) goto errout;
1161 if (rfs->progress) {
1162 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1168 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1169 if (retval) goto errout;
1172 #ifdef RESIZE2FS_DEBUG
1173 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1174 printf("Moving %d blocks %u->%u\n",
1175 size, old_blk, new_blk);
1179 if (c > fs->inode_blocks_per_group)
1180 c = fs->inode_blocks_per_group;
1181 retval = io_channel_read_blk(fs->io, old_blk, c,
1183 if (retval) goto errout;
1184 retval = io_channel_write_blk(fs->io, new_blk, c,
1186 if (retval) goto errout;
1191 if (rfs->progress) {
1192 io_channel_flush(fs->io);
1193 retval = (rfs->progress)(rfs,
1194 E2_RSZ_BLOCK_RELOC_PASS,
1200 io_channel_flush(fs->io);
1204 if (badblock_list) {
1205 if (!retval && bb_modified)
1206 retval = ext2fs_update_bb_inode(old_fs,
1208 ext2fs_badblocks_list_free(badblock_list);
1214 /* --------------------------------------------------------------------
1216 * Resize processing, phase 3
1218 * --------------------------------------------------------------------
1222 struct process_block_struct {
1225 struct ext2_inode * inode;
1231 static int process_block(ext2_filsys fs, blk_t *block_nr,
1232 e2_blkcnt_t blockcnt,
1233 blk_t ref_block EXT2FS_ATTR((unused)),
1234 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1236 struct process_block_struct *pb;
1238 blk_t block, new_block;
1241 pb = (struct process_block_struct *) priv_data;
1243 if (pb->rfs->bmap) {
1244 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1246 *block_nr = new_block;
1247 ret |= BLOCK_CHANGED;
1249 #ifdef RESIZE2FS_DEBUG
1250 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1251 printf("ino=%u, blockcnt=%lld, %u->%u\n",
1252 pb->ino, blockcnt, block, new_block);
1258 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
1259 block, (int) blockcnt);
1271 static errcode_t progress_callback(ext2_filsys fs,
1272 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1273 dgrp_t group, void * priv_data)
1275 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1279 * This check is to protect against old ext2 libraries. It
1280 * shouldn't be needed against new libraries.
1285 if (rfs->progress) {
1286 io_channel_flush(fs->io);
1287 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1288 group+1, fs->group_desc_count);
1296 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1298 struct process_block_struct pb;
1299 ext2_ino_t ino, new_inode;
1300 struct ext2_inode *inode = NULL;
1301 ext2_inode_scan scan = NULL;
1303 char *block_buf = 0;
1304 ext2_ino_t start_to_move;
1305 blk_t orig_size, new_block;
1308 if ((rfs->old_fs->group_desc_count <=
1309 rfs->new_fs->group_desc_count) &&
1314 * Save the original size of the old filesystem, and
1315 * temporarily set the size to be the new size if the new size
1316 * is larger. We need to do this to avoid catching an error
1317 * by the block iterator routines
1319 orig_size = rfs->old_fs->super->s_blocks_count;
1320 if (orig_size < rfs->new_fs->super->s_blocks_count)
1321 rfs->old_fs->super->s_blocks_count =
1322 rfs->new_fs->super->s_blocks_count;
1324 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1325 if (retval) goto errout;
1327 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1328 if (retval) goto errout;
1329 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1330 if (retval) goto errout;
1332 start_to_move = (rfs->new_fs->group_desc_count *
1333 rfs->new_fs->super->s_inodes_per_group);
1335 if (rfs->progress) {
1336 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1337 0, rfs->old_fs->group_desc_count);
1341 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1345 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1346 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1347 inode = malloc(inode_size);
1353 * First, copy all of the inodes that need to be moved
1354 * elsewhere in the inode table
1357 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1358 if (retval) goto errout;
1362 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1363 continue; /* inode not in use */
1365 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1368 if (inode->i_file_acl && rfs->bmap) {
1369 new_block = ext2fs_extent_translate(rfs->bmap,
1372 inode->i_file_acl = new_block;
1373 retval = ext2fs_write_inode_full(rfs->old_fs,
1374 ino, inode, inode_size);
1375 if (retval) goto errout;
1379 if (ext2fs_inode_has_valid_blocks(inode) &&
1380 (rfs->bmap || pb.is_dir)) {
1382 retval = ext2fs_block_iterate2(rfs->old_fs,
1384 process_block, &pb);
1393 if (ino <= start_to_move)
1394 continue; /* Don't need to move it. */
1399 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1403 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1406 /* Get the new version of the inode */
1407 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1409 if (retval) goto errout;
1411 inode->i_ctime = time(0);
1412 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1414 if (retval) goto errout;
1416 #ifdef RESIZE2FS_DEBUG
1417 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1418 printf("Inode moved %u->%u\n", ino, new_inode);
1421 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1425 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1427 io_channel_flush(rfs->old_fs->io);
1430 rfs->old_fs->super->s_blocks_count = orig_size;
1432 ext2fs_free_extent_table(rfs->bmap);
1436 ext2fs_close_inode_scan(scan);
1438 ext2fs_free_mem(&block_buf);
1443 /* --------------------------------------------------------------------
1445 * Resize processing, phase 4.
1447 * --------------------------------------------------------------------
1453 unsigned int max_dirs;
1457 static int check_and_change_inodes(ext2_ino_t dir,
1458 int entry EXT2FS_ATTR((unused)),
1459 struct ext2_dir_entry *dirent, int offset,
1460 int blocksize EXT2FS_ATTR((unused)),
1461 char *buf EXT2FS_ATTR((unused)),
1464 struct istruct *is = (struct istruct *) priv_data;
1465 struct ext2_inode inode;
1466 ext2_ino_t new_inode;
1469 if (is->rfs->progress && offset == 0) {
1470 io_channel_flush(is->rfs->old_fs->io);
1471 is->err = (is->rfs->progress)(is->rfs,
1472 E2_RSZ_INODE_REF_UPD_PASS,
1473 ++is->num, is->max_dirs);
1475 return DIRENT_ABORT;
1481 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1485 #ifdef RESIZE2FS_DEBUG
1486 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1487 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1488 dir, dirent->name_len&0xFF, dirent->name,
1489 dirent->inode, new_inode);
1492 dirent->inode = new_inode;
1494 /* Update the directory mtime and ctime */
1495 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1497 inode.i_mtime = inode.i_ctime = time(0);
1498 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1500 return DIRENT_ABORT;
1503 return DIRENT_CHANGED;
1506 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1515 * Now, we iterate over all of the directories to update the
1519 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1523 if (rfs->progress) {
1524 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1530 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1531 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1532 check_and_change_inodes, &is);
1540 if (rfs->progress && (is.num < is.max_dirs))
1541 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1542 is.max_dirs, is.max_dirs);
1545 ext2fs_free_extent_table(rfs->imap);
1551 /* --------------------------------------------------------------------
1553 * Resize processing, phase 5.
1555 * In this phase we actually move the inode table around, and then
1556 * update the summary statistics. This is scary, since aborting here
1557 * will potentially scramble the filesystem. (We are moving the
1558 * inode tables around in place, and so the potential for lost data,
1559 * or at the very least scrambling the mapping between filenames and
1560 * inode numbers is very high in case of a power failure here.)
1561 * --------------------------------------------------------------------
1566 * A very scary routine --- this one moves the inode table around!!!
1568 * After this you have to use the rfs->new_fs file handle to read and
1571 static errcode_t move_itables(ext2_resize_t rfs)
1573 int n, num, size, diff;
1574 dgrp_t i, max_groups;
1575 ext2_filsys fs = rfs->new_fs;
1577 blk_t old_blk, new_blk, blk;
1579 int j, to_move, moved;
1581 max_groups = fs->group_desc_count;
1582 if (max_groups > rfs->old_fs->group_desc_count)
1583 max_groups = rfs->old_fs->group_desc_count;
1585 size = fs->blocksize * fs->inode_blocks_per_group;
1586 if (!rfs->itable_buf) {
1587 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1593 * Figure out how many inode tables we need to move
1595 to_move = moved = 0;
1596 for (i=0; i < max_groups; i++)
1597 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1598 fs->group_desc[i].bg_inode_table)
1604 if (rfs->progress) {
1605 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1611 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1613 for (i=0; i < max_groups; i++) {
1614 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1615 new_blk = fs->group_desc[i].bg_inode_table;
1616 diff = new_blk - old_blk;
1618 #ifdef RESIZE2FS_DEBUG
1619 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1620 printf("Itable move group %d block %u->%u (diff %d)\n",
1621 i, old_blk, new_blk, diff);
1627 retval = io_channel_read_blk(fs->io, old_blk,
1628 fs->inode_blocks_per_group,
1633 * The end of the inode table segment often contains
1634 * all zeros, and we're often only moving the inode
1635 * table down a block or two. If so, we can optimize
1636 * things by not rewriting blocks that we know to be zero
1639 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1642 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1643 #ifdef RESIZE2FS_DEBUG
1644 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1645 printf("%d blocks of zeros...\n", n);
1647 num = fs->inode_blocks_per_group;
1651 retval = io_channel_write_blk(fs->io, new_blk,
1652 num, rfs->itable_buf);
1654 io_channel_write_blk(fs->io, old_blk,
1655 num, rfs->itable_buf);
1659 retval = io_channel_write_blk(fs->io,
1660 old_blk + fs->inode_blocks_per_group,
1661 diff, (rfs->itable_buf +
1662 (fs->inode_blocks_per_group - diff) *
1668 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1669 j < fs->inode_blocks_per_group ; j++, blk++)
1670 ext2fs_block_alloc_stats(fs, blk, -1);
1672 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1673 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1674 ext2fs_mark_super_dirty(rfs->old_fs);
1675 ext2fs_flush(rfs->old_fs);
1677 if (rfs->progress) {
1678 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1684 mark_table_blocks(fs, fs->block_map);
1686 #ifdef RESIZE2FS_DEBUG
1687 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1688 printf("Inode table move finished.\n");
1697 * Fix the resize inode
1699 static errcode_t fix_resize_inode(ext2_filsys fs)
1701 struct ext2_inode inode;
1706 if (!(fs->super->s_feature_compat &
1707 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1710 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1711 if (retval) goto errout;
1713 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1714 if (retval) goto errout;
1716 if (fs->super->s_reserved_gdt_blocks == 0) {
1717 fs->super->s_feature_compat &=
1718 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1719 ext2fs_mark_super_dirty(fs);
1721 if ((blk = inode.i_block[EXT2_DIND_BLOCK]) != 0)
1722 ext2fs_block_alloc_stats(fs, blk, -1);
1724 memset(&inode, 0, sizeof(inode));
1726 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1730 ext2fs_iblk_set(fs, &inode, 1);
1732 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1733 if (retval) goto errout;
1735 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1737 * Avoid zeroing out block #0; that's rude. This
1738 * should never happen anyway since the filesystem
1739 * should be fsck'ed and we assume it is consistent.
1742 _("Should never happen: resize inode corrupt!\n"));
1746 memset(block_buf, 0, fs->blocksize);
1748 retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1750 if (retval) goto errout;
1752 retval = ext2fs_create_resize_inode(fs);
1758 ext2fs_free_mem(&block_buf);
1763 * Finally, recalculate the summary information
1765 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1769 unsigned int group = 0;
1770 unsigned int count = 0;
1774 blk_t super_blk, old_desc_blk, new_desc_blk;
1775 int old_desc_blocks;
1778 * First calculate the block statistics
1780 uninit = fs->group_desc[group].bg_flags & EXT2_BG_BLOCK_UNINIT;
1781 ext2fs_super_and_bgd_loc(fs, group, &super_blk, &old_desc_blk,
1783 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1784 old_desc_blocks = fs->super->s_first_meta_bg;
1786 old_desc_blocks = fs->desc_blocks +
1787 fs->super->s_reserved_gdt_blocks;
1788 for (blk = fs->super->s_first_data_block;
1789 blk < fs->super->s_blocks_count; blk++) {
1791 !((blk == super_blk) ||
1792 ((old_desc_blk && old_desc_blocks &&
1793 (blk >= old_desc_blk) &&
1794 (blk < old_desc_blk + old_desc_blocks))) ||
1795 ((new_desc_blk && (blk == new_desc_blk))) ||
1796 (blk == fs->group_desc[group].bg_block_bitmap) ||
1797 (blk == fs->group_desc[group].bg_inode_bitmap) ||
1798 ((blk >= fs->group_desc[group].bg_inode_table &&
1799 (blk < fs->group_desc[group].bg_inode_table
1800 + fs->inode_blocks_per_group))))) ||
1801 (!ext2fs_fast_test_block_bitmap(fs->block_map, blk))) {
1806 if ((count == fs->super->s_blocks_per_group) ||
1807 (blk == fs->super->s_blocks_count-1)) {
1808 fs->group_desc[group].bg_free_blocks_count =
1810 ext2fs_group_desc_csum_set(fs, group);
1814 uninit = (fs->group_desc[group].bg_flags &
1815 EXT2_BG_BLOCK_UNINIT);
1816 ext2fs_super_and_bgd_loc(fs, group, &super_blk,
1819 if (fs->super->s_feature_incompat &
1820 EXT2_FEATURE_INCOMPAT_META_BG)
1821 old_desc_blocks = fs->super->s_first_meta_bg;
1823 old_desc_blocks = fs->desc_blocks +
1824 fs->super->s_reserved_gdt_blocks;
1827 fs->super->s_free_blocks_count = total_free;
1830 * Next, calculate the inode statistics
1837 /* Protect loop from wrap-around if s_inodes_count maxed */
1838 uninit = fs->group_desc[group].bg_flags & EXT2_BG_INODE_UNINIT;
1839 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1841 !ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1846 if ((count == fs->super->s_inodes_per_group) ||
1847 (ino == fs->super->s_inodes_count)) {
1848 fs->group_desc[group].bg_free_inodes_count =
1850 ext2fs_group_desc_csum_set(fs, group);
1854 uninit = (fs->group_desc[group].bg_flags &
1855 EXT2_BG_INODE_UNINIT);
1858 fs->super->s_free_inodes_count = total_free;
1859 ext2fs_mark_super_dirty(fs);
1864 * calcluate the minimum number of blocks the given fs can be resized to
1866 blk_t calculate_minimum_resize_size(ext2_filsys fs)
1868 blk_t inode_count, blks_needed, groups, data_blocks;
1869 blk_t grp, data_needed, last_start;
1870 int overhead = 0, num_of_superblocks = 0;
1873 * first figure out how many group descriptors we need to
1874 * handle the number of inodes we have
1876 inode_count = fs->super->s_inodes_count -
1877 fs->super->s_free_inodes_count;
1878 blks_needed = ext2fs_div_ceil(inode_count,
1879 fs->super->s_inodes_per_group) *
1880 EXT2_BLOCKS_PER_GROUP(fs->super);
1881 groups = ext2fs_div_ceil(blks_needed,
1882 EXT2_BLOCKS_PER_GROUP(fs->super));
1885 * we need to figure out how many backup superblocks we have so we can
1886 * account for that in the metadata
1888 for (grp = 0; grp < fs->group_desc_count; grp++) {
1889 if (ext2fs_bg_has_super(fs, grp))
1890 num_of_superblocks++;
1893 /* calculate how many blocks are needed for data */
1894 data_needed = fs->super->s_blocks_count -
1895 fs->super->s_free_blocks_count;
1896 data_needed -= SUPER_OVERHEAD(fs) * num_of_superblocks;
1897 data_needed -= META_OVERHEAD(fs) * fs->group_desc_count;
1899 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1901 * For ext4 we need to allow for up to a flex_bg worth
1902 * of inode tables of slack space so the resize
1903 * operation can be guaranteed to finish.
1905 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1908 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
1909 data_needed += META_OVERHEAD(fs) * extra_groups;
1913 * figure out how many data blocks we have given the number of groups
1914 * we need for our inodes
1916 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
1918 for (grp = 0; grp < groups; grp++) {
1919 overhead = META_OVERHEAD(fs);
1921 if (ext2fs_bg_has_super(fs, grp))
1922 overhead += SUPER_OVERHEAD(fs);
1925 * we want to keep track of how much data we can store in
1926 * the groups leading up to the last group so we can determine
1927 * how big the last group needs to be
1929 if (grp != (groups - 1))
1930 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
1933 data_blocks -= overhead;
1937 * if we need more group descriptors in order to accomodate our data
1938 * then we need to add them here
1940 while (data_needed > data_blocks) {
1941 blk_t remainder = data_needed - data_blocks;
1944 /* figure out how many more groups we need for the data */
1945 extra_grps = ext2fs_div_ceil(remainder,
1946 EXT2_BLOCKS_PER_GROUP(fs->super));
1948 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
1950 /* ok we have to account for the last group */
1951 overhead = META_OVERHEAD(fs);
1952 if (ext2fs_bg_has_super(fs, groups-1))
1953 overhead += SUPER_OVERHEAD(fs);
1954 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
1956 for (grp = groups; grp < groups+extra_grps; grp++) {
1957 overhead = META_OVERHEAD(fs);
1958 if (ext2fs_bg_has_super(fs, grp))
1959 overhead += SUPER_OVERHEAD(fs);
1962 * again, we need to see how much data we cram into
1963 * all of the groups leading up to the last group
1965 if (grp != (groups + extra_grps - 1))
1966 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
1969 data_blocks -= overhead;
1972 groups += extra_grps;
1975 /* now for the fun voodoo */
1976 overhead = META_OVERHEAD(fs);
1979 * if this is the case then the last group is going to have data in it
1980 * so we need to adjust the size of the last group accordingly
1982 if (last_start < data_needed) {
1983 blk_t remainder = data_needed - last_start;
1986 * 50 is a magic number that mkfs/resize uses to see if its
1987 * even worth making/resizing the fs. basically you need to
1988 * have at least 50 blocks in addition to the blocks needed
1989 * for the metadata in the last group
1992 overhead += remainder;
1998 if (ext2fs_bg_has_super(fs, groups-1))
1999 overhead += SUPER_OVERHEAD(fs);
2002 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2003 * only do groups-1, and then add the number of blocks needed to
2004 * handle the group descriptor metadata+data that we need
2006 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2007 blks_needed += overhead;
2010 * If at this point we've already added up more "needed" than
2011 * the current size, just return current size as minimum.
2013 if (blks_needed >= fs->super->s_blocks_count)
2014 return fs->super->s_blocks_count;
2016 * We need to reserve a few extra blocks if extents are
2017 * enabled, in case we need to grow the extent tree. The more
2018 * we shrink the file system, the more space we need.
2020 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
2021 blks_needed += (fs->super->s_blocks_count - blks_needed)/500;