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.
37 #include "resize2fs.h"
40 #ifdef __linux__ /* Kludge for debugging */
41 #define RESIZE2FS_DEBUG
44 static void fix_uninit_block_bitmaps(ext2_filsys fs);
45 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size);
46 static errcode_t blocks_to_move(ext2_resize_t rfs);
47 static errcode_t block_mover(ext2_resize_t rfs);
48 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
49 static errcode_t inode_ref_fix(ext2_resize_t rfs);
50 static errcode_t move_itables(ext2_resize_t rfs);
51 static errcode_t fix_resize_inode(ext2_filsys fs);
52 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
53 static errcode_t fix_sb_journal_backup(ext2_filsys fs);
56 * Some helper CPP macros
58 #define IS_BLOCK_BM(fs, i, blk) ((blk) == ext2fs_block_bitmap_loc((fs),(i)))
59 #define IS_INODE_BM(fs, i, blk) ((blk) == ext2fs_inode_bitmap_loc((fs),(i)))
61 #define IS_INODE_TB(fs, i, blk) (((blk) >= ext2fs_inode_table_loc((fs), (i))) && \
62 ((blk) < (ext2fs_inode_table_loc((fs), (i)) + \
63 (fs)->inode_blocks_per_group)))
65 #define META_OVERHEAD(fs) (2 + (fs)->inode_blocks_per_group)
66 #define SUPER_OVERHEAD(fs) (1 + (fs)->desc_blocks +\
67 (fs)->super->s_reserved_gdt_blocks)
70 * This is the top-level routine which does the dirty deed....
72 errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
73 errcode_t (*progress)(ext2_resize_t rfs, int pass,
75 unsigned long max_val))
80 retval = ext2fs_read_bitmaps(fs);
84 fs->super->s_state |= EXT2_ERROR_FS;
85 ext2fs_mark_super_dirty(fs);
89 * Create the data structure
91 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
94 memset(rfs, 0, sizeof(struct ext2_resize_struct));
96 fix_uninit_block_bitmaps(fs);
101 rfs->progress = progress;
102 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
106 retval = adjust_superblock(rfs, *new_size);
110 fix_uninit_block_bitmaps(rfs->new_fs);
111 /* Clear the block bitmap uninit flag for the last block group */
112 ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
113 EXT2_BG_BLOCK_UNINIT);
115 *new_size = ext2fs_blocks_count(rfs->new_fs->super);
117 retval = blocks_to_move(rfs);
121 #ifdef RESIZE2FS_DEBUG
122 if (rfs->flags & RESIZE_DEBUG_BMOVE)
123 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
124 ext2fs_free_blocks_count(rfs->old_fs->super),
125 ext2fs_free_blocks_count(rfs->new_fs->super),
129 retval = block_mover(rfs);
133 retval = inode_scan_and_fix(rfs);
137 retval = inode_ref_fix(rfs);
141 retval = move_itables(rfs);
145 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
149 retval = fix_resize_inode(rfs->new_fs);
153 retval = fix_sb_journal_backup(rfs->new_fs);
157 rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
158 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
159 retval = ext2fs_close(rfs->new_fs);
165 ext2fs_free(rfs->old_fs);
167 ext2fs_free_mem(&rfs->itable_buf);
168 if (rfs->reserve_blocks)
169 ext2fs_free_block_bitmap(rfs->reserve_blocks);
170 if (rfs->move_blocks)
171 ext2fs_free_block_bitmap(rfs->move_blocks);
172 ext2fs_free_mem(&rfs);
178 ext2fs_free(rfs->new_fs);
180 ext2fs_free_mem(&rfs->itable_buf);
181 ext2fs_free_mem(&rfs);
186 * Clean up the bitmaps for unitialized bitmaps
188 static void fix_uninit_block_bitmaps(ext2_filsys fs)
190 blk64_t i, blk, super_blk, old_desc_blk, new_desc_blk;
194 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
195 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
198 for (g=0; g < fs->group_desc_count; g++) {
199 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
202 blk = (g * fs->super->s_blocks_per_group) +
203 fs->super->s_first_data_block;
205 ext2fs_super_and_bgd_loc2(fs, g, &super_blk,
206 &old_desc_blk, &new_desc_blk, 0);
208 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
209 old_desc_blocks = fs->super->s_first_meta_bg;
211 old_desc_blocks = fs->desc_blocks +
212 fs->super->s_reserved_gdt_blocks;
214 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
215 if (blk >= ext2fs_blocks_count(fs->super))
217 if ((blk == super_blk) ||
218 (old_desc_blk && old_desc_blocks &&
219 (blk >= old_desc_blk) &&
220 (blk < old_desc_blk + old_desc_blocks)) ||
221 (new_desc_blk && (blk == new_desc_blk)) ||
222 (blk == ext2fs_block_bitmap_loc(fs, g)) ||
223 (blk == ext2fs_inode_bitmap_loc(fs, g)) ||
224 (blk >= ext2fs_inode_table_loc(fs, g) &&
225 (blk < ext2fs_inode_table_loc(fs, g)
226 + fs->inode_blocks_per_group)))
227 ext2fs_fast_mark_block_bitmap2(fs->block_map, blk);
229 ext2fs_fast_unmark_block_bitmap2(fs->block_map, blk);
234 /* --------------------------------------------------------------------
236 * Resize processing, phase 1.
238 * In this phase we adjust the in-memory superblock information, and
239 * initialize any new parts of the inode table. The new parts of the
240 * inode table are created in virgin disk space, so we can abort here
241 * without any side effects.
242 * --------------------------------------------------------------------
246 * If the group descriptor's bitmap and inode table blocks are valid,
247 * release them in the new filesystem data structure, and mark them as
248 * reserved so the old inode table blocks don't get overwritten.
250 static void free_gdp_blocks(ext2_filsys fs,
251 ext2fs_block_bitmap reserve_blocks,
252 struct ext2_group_desc *gdp)
257 if (gdp->bg_block_bitmap &&
258 (gdp->bg_block_bitmap < ext2fs_blocks_count(fs->super))) {
259 ext2fs_block_alloc_stats(fs, gdp->bg_block_bitmap, -1);
260 ext2fs_mark_block_bitmap2(reserve_blocks,
261 gdp->bg_block_bitmap);
264 if (gdp->bg_inode_bitmap &&
265 (gdp->bg_inode_bitmap < ext2fs_blocks_count(fs->super))) {
266 ext2fs_block_alloc_stats(fs, gdp->bg_inode_bitmap, -1);
267 ext2fs_mark_block_bitmap2(reserve_blocks,
268 gdp->bg_inode_bitmap);
271 if (gdp->bg_inode_table == 0 ||
272 (gdp->bg_inode_table >= ext2fs_blocks_count(fs->super)))
275 for (blk = gdp->bg_inode_table, j = 0;
276 j < fs->inode_blocks_per_group; j++, blk++) {
277 if (blk >= ext2fs_blocks_count(fs->super))
279 ext2fs_block_alloc_stats(fs, blk, -1);
280 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
285 * This routine is shared by the online and offline resize routines.
286 * All of the information which is adjusted in memory is done here.
288 * The reserve_blocks parameter is only needed when shrinking the
291 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
292 ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
295 blk64_t overhead = 0;
297 blk64_t blk, group_block;
299 blk64_t adj, old_numblocks, numblocks, adjblocks;
300 unsigned long i, j, old_desc_blocks, max_group;
301 unsigned int meta_bg, meta_bg_size;
302 int has_super, csum_flag;
303 unsigned long long new_inodes; /* u64 to check for overflow */
306 ext2fs_blocks_count_set(fs->super, new_size);
309 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
310 fs->super->s_first_data_block,
311 EXT2_BLOCKS_PER_GROUP(fs->super));
312 if (fs->group_desc_count == 0)
313 return EXT2_ET_TOOSMALL;
314 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
315 EXT2_DESC_PER_BLOCK(fs->super));
318 * Overhead is the number of bookkeeping blocks per group. It
319 * includes the superblock backup, the group descriptor
320 * backups, the inode bitmap, the block bitmap, and the inode
323 overhead = (int) (2 + fs->inode_blocks_per_group);
325 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
326 overhead += 1 + fs->desc_blocks +
327 fs->super->s_reserved_gdt_blocks;
330 * See if the last group is big enough to support the
331 * necessary data structures. If not, we need to get rid of
334 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
335 fs->super->s_blocks_per_group;
336 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
337 return EXT2_ET_TOOSMALL;
338 if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
339 ext2fs_blocks_count_set(fs->super,
340 ext2fs_blocks_count(fs->super) - rem);
344 * Adjust the number of inodes
346 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
347 if (new_inodes > ~0U) {
348 fprintf(stderr, _("inodes (%llu) must be less than %u"),
350 return EXT2_ET_TOO_MANY_INODES;
352 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
353 fs->group_desc_count;
356 * Adjust the number of free blocks
358 blk = ext2fs_blocks_count(old_fs->super);
359 if (blk > ext2fs_blocks_count(fs->super))
360 ext2fs_free_blocks_count_set(fs->super,
361 ext2fs_free_blocks_count(fs->super) -
362 (blk - ext2fs_blocks_count(fs->super)));
364 ext2fs_free_blocks_count_set(fs->super,
365 ext2fs_free_blocks_count(fs->super) +
366 (ext2fs_blocks_count(fs->super) - blk));
369 * Adjust the number of reserved blocks
371 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
372 ext2fs_blocks_count(old_fs->super);
373 ext2fs_r_blocks_count_set(fs->super,
374 (percent * ext2fs_blocks_count(fs->super) /
378 * Adjust the bitmaps for size
380 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
381 fs->super->s_inodes_count,
383 if (retval) goto errout;
385 real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) *
386 fs->group_desc_count)) - 1 +
387 fs->super->s_first_data_block;
388 retval = ext2fs_resize_block_bitmap2(ext2fs_blocks_count(fs->super)-1,
389 real_end, fs->block_map);
391 if (retval) goto errout;
394 * Reallocate the group descriptors as necessary.
396 if (old_fs->desc_blocks != fs->desc_blocks) {
397 retval = ext2fs_resize_mem(old_fs->desc_blocks *
399 fs->desc_blocks * fs->blocksize,
403 if (fs->desc_blocks > old_fs->desc_blocks)
404 memset((char *) fs->group_desc +
405 (old_fs->desc_blocks * fs->blocksize), 0,
406 (fs->desc_blocks - old_fs->desc_blocks) *
411 * If the resize_inode feature is set, and we are changing the
412 * number of descriptor blocks, then adjust
413 * s_reserved_gdt_blocks if possible to avoid needing to move
414 * the inode table either now or in the future.
416 if ((fs->super->s_feature_compat &
417 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
418 (old_fs->desc_blocks != fs->desc_blocks)) {
421 new = ((int) fs->super->s_reserved_gdt_blocks) +
422 (old_fs->desc_blocks - fs->desc_blocks);
425 if (new > (int) fs->blocksize/4)
426 new = fs->blocksize/4;
427 fs->super->s_reserved_gdt_blocks = new;
431 * If we are shrinking the number of block groups, we're done
434 if (old_fs->group_desc_count > fs->group_desc_count) {
436 * Check the block groups that we are chopping off
437 * and free any blocks associated with their metadata
439 for (i = fs->group_desc_count;
440 i < old_fs->group_desc_count; i++) {
441 free_gdp_blocks(fs, reserve_blocks,
442 ext2fs_group_desc(old_fs,
443 old_fs->group_desc, i));
450 * Fix the count of the last (old) block group
452 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
453 old_fs->super->s_first_data_block) %
454 old_fs->super->s_blocks_per_group;
456 old_numblocks = old_fs->super->s_blocks_per_group;
457 if (old_fs->group_desc_count == fs->group_desc_count) {
458 numblocks = (ext2fs_blocks_count(fs->super) -
459 fs->super->s_first_data_block) %
460 fs->super->s_blocks_per_group;
462 numblocks = fs->super->s_blocks_per_group;
464 numblocks = fs->super->s_blocks_per_group;
465 i = old_fs->group_desc_count - 1;
466 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
467 ext2fs_group_desc_csum_set(fs, i);
470 * If the number of block groups is staying the same, we're
471 * done and can exit now. (If the number block groups is
472 * shrinking, we had exited earlier.)
474 if (old_fs->group_desc_count >= fs->group_desc_count) {
480 * Initialize the new block group descriptors
482 group_block = fs->super->s_first_data_block +
483 old_fs->group_desc_count * fs->super->s_blocks_per_group;
485 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
486 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
487 adj = old_fs->group_desc_count;
488 max_group = fs->group_desc_count - adj;
489 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
490 old_desc_blocks = fs->super->s_first_meta_bg;
492 old_desc_blocks = fs->desc_blocks +
493 fs->super->s_reserved_gdt_blocks;
494 for (i = old_fs->group_desc_count;
495 i < fs->group_desc_count; i++) {
496 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
497 sizeof(struct ext2_group_desc));
500 ext2fs_bg_flags_zap(fs, i);
502 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT | EXT2_BG_INODE_ZEROED);
504 numblocks = ext2fs_group_blocks_count(fs, i);
505 if ((i < fs->group_desc_count - 1) && csum_flag)
506 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
508 has_super = ext2fs_bg_has_super(fs, i);
510 ext2fs_block_alloc_stats2(fs, group_block, +1);
513 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
514 meta_bg = i / meta_bg_size;
515 if (!(fs->super->s_feature_incompat &
516 EXT2_FEATURE_INCOMPAT_META_BG) ||
517 (meta_bg < fs->super->s_first_meta_bg)) {
519 for (j=0; j < old_desc_blocks; j++)
520 ext2fs_block_alloc_stats2(fs,
521 group_block + 1 + j, +1);
522 adjblocks += old_desc_blocks;
527 if (((i % meta_bg_size) == 0) ||
528 ((i % meta_bg_size) == 1) ||
529 ((i % meta_bg_size) == (meta_bg_size-1)))
530 ext2fs_block_alloc_stats2(fs,
531 group_block + has_super, +1);
534 adjblocks += 2 + fs->inode_blocks_per_group;
536 numblocks -= adjblocks;
537 ext2fs_free_blocks_count_set(fs->super,
538 ext2fs_free_blocks_count(fs->super) - adjblocks);
539 fs->super->s_free_inodes_count +=
540 fs->super->s_inodes_per_group;
541 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
542 ext2fs_bg_free_inodes_count_set(fs, i,
543 fs->super->s_inodes_per_group);
544 ext2fs_bg_used_dirs_count_set(fs, i, 0);
545 ext2fs_group_desc_csum_set(fs, i);
547 retval = ext2fs_allocate_group_table(fs, i, 0);
548 if (retval) goto errout;
550 group_block += fs->super->s_blocks_per_group;
559 * This routine adjusts the superblock and other data structures, both
560 * in disk as well as in memory...
562 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
569 unsigned long max_group;
572 ext2fs_mark_super_dirty(fs);
573 ext2fs_mark_bb_dirty(fs);
574 ext2fs_mark_ib_dirty(fs);
576 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
577 &rfs->reserve_blocks);
581 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
586 * Check to make sure there are enough inodes
588 if ((rfs->old_fs->super->s_inodes_count -
589 rfs->old_fs->super->s_free_inodes_count) >
590 rfs->new_fs->super->s_inodes_count) {
596 * If we are shrinking the number block groups, we're done and
599 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
605 * If the number of block groups is staying the same, we're
606 * done and can exit now. (If the number block groups is
607 * shrinking, we had exited earlier.)
609 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
615 * Initialize the new block group descriptors
617 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
622 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
623 group_block = fs->super->s_first_data_block +
624 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
626 adj = rfs->old_fs->group_desc_count;
627 max_group = fs->group_desc_count - adj;
629 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
634 for (i = rfs->old_fs->group_desc_count;
635 i < fs->group_desc_count; i++) {
637 * Write out the new inode table
639 retval = io_channel_write_blk64(fs->io,
640 ext2fs_inode_table_loc(fs, i),
641 fs->inode_blocks_per_group,
643 if (retval) goto errout;
645 io_channel_flush(fs->io);
647 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
648 i - adj + 1, max_group);
652 group_block += fs->super->s_blocks_per_group;
654 io_channel_flush(fs->io);
661 /* --------------------------------------------------------------------
663 * Resize processing, phase 2.
665 * In this phase we adjust determine which blocks need to be moved, in
666 * blocks_to_move(). We then copy the blocks to their ultimate new
667 * destinations using block_mover(). Since we are copying blocks to
668 * their new locations, again during this pass we can abort without
670 * --------------------------------------------------------------------
674 * This helper function creates a block bitmap with all of the
675 * filesystem meta-data blocks.
677 static errcode_t mark_table_blocks(ext2_filsys fs,
678 ext2fs_block_bitmap bmap)
683 unsigned long meta_bg_size;
684 unsigned int old_desc_blocks;
686 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
687 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
688 old_desc_blocks = fs->super->s_first_meta_bg;
690 old_desc_blocks = fs->desc_blocks +
691 fs->super->s_reserved_gdt_blocks;
692 for (i = 0; i < fs->group_desc_count; i++) {
693 ext2fs_reserve_super_and_bgd(fs, i, bmap);
696 * Mark the blocks used for the inode table
698 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
699 j < (unsigned int) fs->inode_blocks_per_group;
701 ext2fs_mark_block_bitmap2(bmap, b);
704 * Mark block used for the block bitmap
706 ext2fs_mark_block_bitmap2(bmap,
707 ext2fs_block_bitmap_loc(fs, i));
710 * Mark block used for the inode bitmap
712 ext2fs_mark_block_bitmap2(bmap,
713 ext2fs_inode_bitmap_loc(fs, i));
719 * This function checks to see if a particular block (either a
720 * superblock or a block group descriptor) overlaps with an inode or
721 * block bitmap block, or with the inode table.
723 static void mark_fs_metablock(ext2_resize_t rfs,
724 ext2fs_block_bitmap meta_bmap,
725 int group, blk64_t blk)
727 ext2_filsys fs = rfs->new_fs;
729 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
730 ext2fs_block_alloc_stats2(fs, blk, +1);
733 * Check to see if we overlap with the inode or block bitmap,
734 * or the inode tables. If not, and the block is in use, then
735 * mark it as a block to be moved.
737 if (IS_BLOCK_BM(fs, group, blk)) {
738 ext2fs_block_bitmap_loc_set(fs, group, 0);
739 rfs->needed_blocks++;
740 } else if (IS_INODE_BM(fs, group, blk)) {
741 ext2fs_inode_bitmap_loc_set(fs, group, 0);
742 rfs->needed_blocks++;
743 } else if (IS_INODE_TB(fs, group, blk)) {
744 ext2fs_inode_table_loc_set(fs, group, 0);
745 rfs->needed_blocks++;
746 } else if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
747 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
748 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
750 * If the block bitmap is uninitialized, which means
751 * nothing other than standard metadata in use.
754 } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
755 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
756 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
757 rfs->needed_blocks++;
763 * This routine marks and unmarks reserved blocks in the new block
764 * bitmap. It also determines which blocks need to be moved and
765 * places this information into the move_blocks bitmap.
767 static errcode_t blocks_to_move(ext2_resize_t rfs)
770 dgrp_t i, max_groups, g;
771 blk64_t blk, group_blk;
772 blk64_t old_blocks, new_blocks;
773 unsigned int meta_bg, meta_bg_size;
775 ext2_filsys fs, old_fs;
776 ext2fs_block_bitmap meta_bmap;
777 __u32 save_incompat_flag;
780 old_fs = rfs->old_fs;
781 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
784 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
789 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
794 retval = mark_table_blocks(old_fs, meta_bmap);
801 * If we're shrinking the filesystem, we need to move all of
802 * the blocks that don't fit any more
804 for (blk = ext2fs_blocks_count(fs->super);
805 blk < ext2fs_blocks_count(old_fs->super); blk++) {
806 g = ext2fs_group_of_blk2(fs, blk);
807 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
808 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
809 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
811 * The block bitmap is uninitialized, so skip
812 * to the next block group.
814 blk = ((g+1) * fs->super->s_blocks_per_group) +
815 fs->super->s_first_data_block - 1;
818 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
819 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
820 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
821 rfs->needed_blocks++;
823 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
826 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
827 old_blocks = old_fs->super->s_first_meta_bg;
828 new_blocks = fs->super->s_first_meta_bg;
830 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
831 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
834 if (old_blocks == new_blocks) {
839 max_groups = fs->group_desc_count;
840 if (max_groups > old_fs->group_desc_count)
841 max_groups = old_fs->group_desc_count;
842 group_blk = old_fs->super->s_first_data_block;
844 * If we're reducing the number of descriptor blocks, this
845 * makes life easy. :-) We just have to mark some extra
848 if (old_blocks > new_blocks) {
849 for (i = 0; i < max_groups; i++) {
850 if (!ext2fs_bg_has_super(fs, i)) {
851 group_blk += fs->super->s_blocks_per_group;
854 for (blk = group_blk+1+new_blocks;
855 blk < group_blk+1+old_blocks; blk++) {
856 ext2fs_block_alloc_stats2(fs, blk, -1);
857 rfs->needed_blocks--;
859 group_blk += fs->super->s_blocks_per_group;
865 * If we're increasing the number of descriptor blocks, life
866 * gets interesting....
868 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
869 for (i = 0; i < max_groups; i++) {
870 has_super = ext2fs_bg_has_super(fs, i);
872 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
874 meta_bg = i / meta_bg_size;
875 if (!(fs->super->s_feature_incompat &
876 EXT2_FEATURE_INCOMPAT_META_BG) ||
877 (meta_bg < fs->super->s_first_meta_bg)) {
879 for (blk = group_blk+1;
880 blk < group_blk + 1 + new_blocks; blk++)
881 mark_fs_metablock(rfs, meta_bmap,
887 if (((i % meta_bg_size) == 0) ||
888 ((i % meta_bg_size) == 1) ||
889 ((i % meta_bg_size) == (meta_bg_size-1)))
890 mark_fs_metablock(rfs, meta_bmap, i,
891 group_blk + has_super);
894 if (ext2fs_inode_table_loc(fs, i) &&
895 ext2fs_inode_bitmap_loc(fs, i) &&
896 ext2fs_block_bitmap_loc(fs, i))
900 * Reserve the existing meta blocks that we know
901 * aren't to be moved.
903 if (ext2fs_block_bitmap_loc(fs, i))
904 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
905 ext2fs_block_bitmap_loc(fs, i));
906 if (ext2fs_inode_bitmap_loc(fs, i))
907 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
908 ext2fs_inode_bitmap_loc(fs, i));
909 if (ext2fs_inode_table_loc(fs, i))
910 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
911 j < fs->inode_blocks_per_group ; j++, blk++)
912 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
916 * Allocate the missing data structures
918 * XXX We have a problem with FLEX_BG and off-line
919 * resizing where we are growing the size of the
920 * filesystem. ext2fs_allocate_group_table() will try
921 * to reserve the inode table in the desired flex_bg
922 * location. However, passing rfs->reserve_blocks
923 * doesn't work since it only has reserved the blocks
924 * that will be used in the new block group -- and
925 * with flex_bg, we can and will allocate the tables
926 * outside of the block group. And we can't pass in
927 * the fs->block_map because it doesn't handle
928 * overlapping inode table movements right. So for
929 * now, we temporarily disable flex_bg to force
930 * ext2fs_allocate_group_tables() to allocate the bg
931 * metadata in side the block group, and the restore
932 * it afterwards. Ugly, until we can fix this up
935 save_incompat_flag = fs->super->s_feature_incompat;
936 fs->super->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_FLEX_BG;
937 retval = ext2fs_allocate_group_table(fs, i,
938 rfs->reserve_blocks);
939 fs->super->s_feature_incompat = save_incompat_flag;
944 * For those structures that have changed, we need to
947 if (ext2fs_block_bitmap_loc(old_fs, i) !=
948 (blk = ext2fs_block_bitmap_loc(fs, i))) {
949 ext2fs_block_alloc_stats2(fs, blk, +1);
950 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
951 !ext2fs_test_block_bitmap2(meta_bmap, blk))
952 ext2fs_mark_block_bitmap2(rfs->move_blocks,
955 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
956 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
957 ext2fs_block_alloc_stats2(fs, blk, +1);
958 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
959 !ext2fs_test_block_bitmap2(meta_bmap, blk))
960 ext2fs_mark_block_bitmap2(rfs->move_blocks,
965 * The inode table, if we need to relocate it, is
966 * handled specially. We have to reserve the blocks
967 * for both the old and the new inode table, since we
968 * can't have the inode table be destroyed during the
969 * block relocation phase.
971 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
972 goto next_group; /* inode table not moved */
974 rfs->needed_blocks += fs->inode_blocks_per_group;
977 * Mark the new inode table as in use in the new block
978 * allocation bitmap, and move any blocks that might
981 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
982 j < fs->inode_blocks_per_group ; j++, blk++) {
983 ext2fs_block_alloc_stats2(fs, blk, +1);
984 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
985 !ext2fs_test_block_bitmap2(meta_bmap, blk))
986 ext2fs_mark_block_bitmap2(rfs->move_blocks,
991 * Make sure the old inode table is reserved in the
992 * block reservation bitmap.
994 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
995 j < fs->inode_blocks_per_group ; j++, blk++)
996 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
999 group_blk += rfs->new_fs->super->s_blocks_per_group;
1005 ext2fs_free_block_bitmap(meta_bmap);
1011 * This helper function tries to allocate a new block. We try to
1012 * avoid hitting the original group descriptor blocks at least at
1013 * first, since we want to make it possible to recover from a badly
1014 * aborted resize operation as much as possible.
1016 * In the future, I may further modify this routine to balance out
1017 * where we get the new blocks across the various block groups.
1018 * Ideally we would allocate blocks that corresponded with the block
1019 * group of the containing inode, and keep contiguous blocks
1020 * together. However, this very difficult to do efficiently, since we
1021 * don't have the necessary information up front.
1025 #define DESPERATION 2
1027 static void init_block_alloc(ext2_resize_t rfs)
1029 rfs->alloc_state = AVOID_OLD;
1030 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1032 /* HACK for testing */
1033 if (ext2fs_blocks_count(rfs->new_fs->super) >
1034 ext2fs_blocks_count(rfs->old_fs->super))
1035 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1039 static blk64_t get_new_block(ext2_resize_t rfs)
1041 ext2_filsys fs = rfs->new_fs;
1044 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1045 if (rfs->alloc_state == DESPERATION)
1048 #ifdef RESIZE2FS_DEBUG
1049 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1050 printf("Going into desperation mode "
1051 "for block allocations\n");
1053 rfs->alloc_state = DESPERATION;
1054 rfs->new_blk = fs->super->s_first_data_block;
1057 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1058 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1060 ((rfs->alloc_state == AVOID_OLD) &&
1061 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1062 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1067 return rfs->new_blk;
1071 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1074 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1077 blk = get_new_block(rfs);
1081 #ifdef RESIZE2FS_DEBUG
1082 if (rfs->flags & 0xF)
1083 printf("get_alloc_block allocating %llu\n", blk);
1086 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1087 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1088 *ret = (blk64_t) blk;
1092 static errcode_t block_mover(ext2_resize_t rfs)
1094 blk64_t blk, old_blk, new_blk;
1095 ext2_filsys fs = rfs->new_fs;
1096 ext2_filsys old_fs = rfs->old_fs;
1101 ext2_badblocks_list badblock_list = 0;
1102 int bb_modified = 0;
1104 fs->get_alloc_block = resize2fs_get_alloc_block;
1105 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1107 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1111 new_blk = fs->super->s_first_data_block;
1112 if (!rfs->itable_buf) {
1113 retval = ext2fs_get_array(fs->blocksize,
1114 fs->inode_blocks_per_group,
1119 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1124 * The first step is to figure out where all of the blocks
1127 to_move = moved = 0;
1128 init_block_alloc(rfs);
1129 for (blk = old_fs->super->s_first_data_block;
1130 blk < ext2fs_blocks_count(old_fs->super); blk++) {
1131 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1133 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1135 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1136 ext2fs_badblocks_list_del(badblock_list, blk);
1141 new_blk = get_new_block(rfs);
1146 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1147 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
1153 ext2fs_free_extent_table(rfs->bmap);
1161 * Step two is to actually move the blocks
1163 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1164 if (retval) goto errout;
1166 if (rfs->progress) {
1167 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1173 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1174 if (retval) goto errout;
1177 #ifdef RESIZE2FS_DEBUG
1178 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1179 printf("Moving %llu blocks %llu->%llu\n",
1180 size, old_blk, new_blk);
1184 if (c > fs->inode_blocks_per_group)
1185 c = fs->inode_blocks_per_group;
1186 retval = io_channel_read_blk64(fs->io, old_blk, c,
1188 if (retval) goto errout;
1189 retval = io_channel_write_blk64(fs->io, new_blk, c,
1191 if (retval) goto errout;
1196 if (rfs->progress) {
1197 io_channel_flush(fs->io);
1198 retval = (rfs->progress)(rfs,
1199 E2_RSZ_BLOCK_RELOC_PASS,
1205 io_channel_flush(fs->io);
1209 if (badblock_list) {
1210 if (!retval && bb_modified)
1211 retval = ext2fs_update_bb_inode(old_fs,
1213 ext2fs_badblocks_list_free(badblock_list);
1219 /* --------------------------------------------------------------------
1221 * Resize processing, phase 3
1223 * --------------------------------------------------------------------
1227 struct process_block_struct {
1230 struct ext2_inode * inode;
1236 static int process_block(ext2_filsys fs, blk64_t *block_nr,
1237 e2_blkcnt_t blockcnt,
1238 blk64_t ref_block EXT2FS_ATTR((unused)),
1239 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1241 struct process_block_struct *pb;
1243 blk64_t block, new_block;
1246 pb = (struct process_block_struct *) priv_data;
1248 if (pb->rfs->bmap) {
1249 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1251 *block_nr = new_block;
1252 ret |= BLOCK_CHANGED;
1254 #ifdef RESIZE2FS_DEBUG
1255 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1256 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1257 pb->ino, blockcnt, block, new_block);
1263 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1264 block, (int) blockcnt);
1276 static errcode_t progress_callback(ext2_filsys fs,
1277 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1278 dgrp_t group, void * priv_data)
1280 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1284 * This check is to protect against old ext2 libraries. It
1285 * shouldn't be needed against new libraries.
1290 if (rfs->progress) {
1291 io_channel_flush(fs->io);
1292 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1293 group+1, fs->group_desc_count);
1301 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1303 struct process_block_struct pb;
1304 ext2_ino_t ino, new_inode;
1305 struct ext2_inode *inode = NULL;
1306 ext2_inode_scan scan = NULL;
1308 char *block_buf = 0;
1309 ext2_ino_t start_to_move;
1314 if ((rfs->old_fs->group_desc_count <=
1315 rfs->new_fs->group_desc_count) &&
1320 * Save the original size of the old filesystem, and
1321 * temporarily set the size to be the new size if the new size
1322 * is larger. We need to do this to avoid catching an error
1323 * by the block iterator routines
1325 orig_size = ext2fs_blocks_count(rfs->old_fs->super);
1326 if (orig_size < ext2fs_blocks_count(rfs->new_fs->super))
1327 ext2fs_blocks_count_set(rfs->old_fs->super,
1328 ext2fs_blocks_count(rfs->new_fs->super));
1330 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1331 if (retval) goto errout;
1333 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1334 if (retval) goto errout;
1335 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1336 if (retval) goto errout;
1338 start_to_move = (rfs->new_fs->group_desc_count *
1339 rfs->new_fs->super->s_inodes_per_group);
1341 if (rfs->progress) {
1342 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1343 0, rfs->old_fs->group_desc_count);
1347 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1351 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1352 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1353 inode = malloc(inode_size);
1359 * First, copy all of the inodes that need to be moved
1360 * elsewhere in the inode table
1363 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1364 if (retval) goto errout;
1368 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1369 continue; /* inode not in use */
1371 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1374 if (ext2fs_file_acl_block(inode) && rfs->bmap) {
1375 new_block = ext2fs_extent_translate(rfs->bmap,
1376 ext2fs_file_acl_block(inode));
1378 ext2fs_file_acl_block_set(inode, new_block);
1379 retval = ext2fs_write_inode_full(rfs->old_fs,
1380 ino, inode, inode_size);
1381 if (retval) goto errout;
1385 if (ext2fs_inode_has_valid_blocks(inode) &&
1386 (rfs->bmap || pb.is_dir)) {
1388 retval = ext2fs_block_iterate3(rfs->old_fs,
1390 process_block, &pb);
1399 if (ino <= start_to_move)
1400 continue; /* Don't need to move it. */
1405 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1409 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1412 /* Get the new version of the inode */
1413 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1415 if (retval) goto errout;
1417 inode->i_ctime = time(0);
1418 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1420 if (retval) goto errout;
1422 #ifdef RESIZE2FS_DEBUG
1423 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1424 printf("Inode moved %u->%u\n", ino, new_inode);
1427 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1431 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1433 io_channel_flush(rfs->old_fs->io);
1436 ext2fs_blocks_count_set(rfs->old_fs->super, orig_size);
1438 ext2fs_free_extent_table(rfs->bmap);
1442 ext2fs_close_inode_scan(scan);
1444 ext2fs_free_mem(&block_buf);
1449 /* --------------------------------------------------------------------
1451 * Resize processing, phase 4.
1453 * --------------------------------------------------------------------
1459 unsigned int max_dirs;
1463 static int check_and_change_inodes(ext2_ino_t dir,
1464 int entry EXT2FS_ATTR((unused)),
1465 struct ext2_dir_entry *dirent, int offset,
1466 int blocksize EXT2FS_ATTR((unused)),
1467 char *buf EXT2FS_ATTR((unused)),
1470 struct istruct *is = (struct istruct *) priv_data;
1471 struct ext2_inode inode;
1472 ext2_ino_t new_inode;
1475 if (is->rfs->progress && offset == 0) {
1476 io_channel_flush(is->rfs->old_fs->io);
1477 is->err = (is->rfs->progress)(is->rfs,
1478 E2_RSZ_INODE_REF_UPD_PASS,
1479 ++is->num, is->max_dirs);
1481 return DIRENT_ABORT;
1487 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1491 #ifdef RESIZE2FS_DEBUG
1492 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1493 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1494 dir, dirent->name_len&0xFF, dirent->name,
1495 dirent->inode, new_inode);
1498 dirent->inode = new_inode;
1500 /* Update the directory mtime and ctime */
1501 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1503 inode.i_mtime = inode.i_ctime = time(0);
1504 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1506 return DIRENT_ABORT;
1509 return DIRENT_CHANGED;
1512 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1521 * Now, we iterate over all of the directories to update the
1525 is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
1529 if (rfs->progress) {
1530 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1536 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1537 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1538 check_and_change_inodes, &is);
1546 if (rfs->progress && (is.num < is.max_dirs))
1547 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1548 is.max_dirs, is.max_dirs);
1551 ext2fs_free_extent_table(rfs->imap);
1557 /* --------------------------------------------------------------------
1559 * Resize processing, phase 5.
1561 * In this phase we actually move the inode table around, and then
1562 * update the summary statistics. This is scary, since aborting here
1563 * will potentially scramble the filesystem. (We are moving the
1564 * inode tables around in place, and so the potential for lost data,
1565 * or at the very least scrambling the mapping between filenames and
1566 * inode numbers is very high in case of a power failure here.)
1567 * --------------------------------------------------------------------
1572 * A very scary routine --- this one moves the inode table around!!!
1574 * After this you have to use the rfs->new_fs file handle to read and
1577 static errcode_t move_itables(ext2_resize_t rfs)
1581 dgrp_t i, max_groups;
1582 ext2_filsys fs = rfs->new_fs;
1584 blk64_t old_blk, new_blk, blk;
1586 int j, to_move, moved;
1588 max_groups = fs->group_desc_count;
1589 if (max_groups > rfs->old_fs->group_desc_count)
1590 max_groups = rfs->old_fs->group_desc_count;
1592 size = fs->blocksize * fs->inode_blocks_per_group;
1593 if (!rfs->itable_buf) {
1594 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1600 * Figure out how many inode tables we need to move
1602 to_move = moved = 0;
1603 for (i=0; i < max_groups; i++)
1604 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
1605 ext2fs_inode_table_loc(fs, i))
1611 if (rfs->progress) {
1612 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1618 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1620 for (i=0; i < max_groups; i++) {
1621 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
1622 new_blk = ext2fs_inode_table_loc(fs, i);
1623 diff = new_blk - old_blk;
1625 #ifdef RESIZE2FS_DEBUG
1626 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1627 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
1628 i, old_blk, new_blk, diff);
1634 retval = io_channel_read_blk64(fs->io, old_blk,
1635 fs->inode_blocks_per_group,
1640 * The end of the inode table segment often contains
1641 * all zeros, and we're often only moving the inode
1642 * table down a block or two. If so, we can optimize
1643 * things by not rewriting blocks that we know to be zero
1646 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1649 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1650 #ifdef RESIZE2FS_DEBUG
1651 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1652 printf("%d blocks of zeros...\n", n);
1654 num = fs->inode_blocks_per_group;
1658 retval = io_channel_write_blk64(fs->io, new_blk,
1659 num, rfs->itable_buf);
1661 io_channel_write_blk64(fs->io, old_blk,
1662 num, rfs->itable_buf);
1666 retval = io_channel_write_blk64(fs->io,
1667 old_blk + fs->inode_blocks_per_group,
1668 diff, (rfs->itable_buf +
1669 (fs->inode_blocks_per_group - diff) *
1675 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1676 j < fs->inode_blocks_per_group ; j++, blk++)
1677 ext2fs_block_alloc_stats2(fs, blk, -1);
1679 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
1680 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1681 ext2fs_mark_super_dirty(rfs->old_fs);
1682 ext2fs_flush(rfs->old_fs);
1684 if (rfs->progress) {
1685 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1691 mark_table_blocks(fs, fs->block_map);
1693 #ifdef RESIZE2FS_DEBUG
1694 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1695 printf("Inode table move finished.\n");
1704 * Fix the resize inode
1706 static errcode_t fix_resize_inode(ext2_filsys fs)
1708 struct ext2_inode inode;
1712 if (!(fs->super->s_feature_compat &
1713 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1716 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1717 if (retval) goto errout;
1719 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1720 if (retval) goto errout;
1722 ext2fs_iblk_set(fs, &inode, 1);
1724 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1725 if (retval) goto errout;
1727 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1729 * Avoid zeroing out block #0; that's rude. This
1730 * should never happen anyway since the filesystem
1731 * should be fsck'ed and we assume it is consistent.
1734 _("Should never happen: resize inode corrupt!\n"));
1738 memset(block_buf, 0, fs->blocksize);
1740 retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1742 if (retval) goto errout;
1744 retval = ext2fs_create_resize_inode(fs);
1750 ext2fs_free_mem(&block_buf);
1755 * Finally, recalculate the summary information
1757 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1761 unsigned int group = 0;
1762 unsigned int count = 0;
1766 blk64_t super_blk, old_desc_blk, new_desc_blk;
1767 int old_desc_blocks;
1770 * First calculate the block statistics
1772 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1773 ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
1775 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1776 old_desc_blocks = fs->super->s_first_meta_bg;
1778 old_desc_blocks = fs->desc_blocks +
1779 fs->super->s_reserved_gdt_blocks;
1780 for (blk = fs->super->s_first_data_block;
1781 blk < ext2fs_blocks_count(fs->super); blk++) {
1783 !((blk == super_blk) ||
1784 ((old_desc_blk && old_desc_blocks &&
1785 (blk >= old_desc_blk) &&
1786 (blk < old_desc_blk + old_desc_blocks))) ||
1787 ((new_desc_blk && (blk == new_desc_blk))) ||
1788 (blk == ext2fs_block_bitmap_loc(fs, group)) ||
1789 (blk == ext2fs_inode_bitmap_loc(fs, group)) ||
1790 ((blk >= ext2fs_inode_table_loc(fs, group) &&
1791 (blk < ext2fs_inode_table_loc(fs, group)
1792 + fs->inode_blocks_per_group))))) ||
1793 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
1798 if ((count == fs->super->s_blocks_per_group) ||
1799 (blk == ext2fs_blocks_count(fs->super)-1)) {
1800 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
1801 ext2fs_group_desc_csum_set(fs, group);
1803 if (group >= fs->group_desc_count)
1807 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1808 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
1811 if (fs->super->s_feature_incompat &
1812 EXT2_FEATURE_INCOMPAT_META_BG)
1813 old_desc_blocks = fs->super->s_first_meta_bg;
1815 old_desc_blocks = fs->desc_blocks +
1816 fs->super->s_reserved_gdt_blocks;
1819 ext2fs_free_blocks_count_set(fs->super, total_free);
1822 * Next, calculate the inode statistics
1829 /* Protect loop from wrap-around if s_inodes_count maxed */
1830 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
1831 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1833 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1838 if ((count == fs->super->s_inodes_per_group) ||
1839 (ino == fs->super->s_inodes_count)) {
1840 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
1841 ext2fs_group_desc_csum_set(fs, group);
1843 if (group >= fs->group_desc_count)
1847 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
1850 fs->super->s_free_inodes_count = total_free;
1851 ext2fs_mark_super_dirty(fs);
1856 * Journal may have been relocated; update the backup journal blocks
1857 * in the superblock.
1859 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
1862 struct ext2_inode inode;
1864 if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1867 /* External journal? Nothing to do. */
1868 if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
1871 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
1874 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
1875 fs->super->s_jnl_blocks[15] = inode.i_size_high;
1876 fs->super->s_jnl_blocks[16] = inode.i_size;
1877 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
1878 ext2fs_mark_super_dirty(fs);
1883 * calcluate the minimum number of blocks the given fs can be resized to
1885 blk64_t calculate_minimum_resize_size(ext2_filsys fs)
1887 ext2_ino_t inode_count;
1888 blk64_t blks_needed, groups, data_blocks;
1889 blk64_t grp, data_needed, last_start;
1890 blk64_t overhead = 0;
1891 int num_of_superblocks = 0;
1892 int extra_groups = 0;
1893 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1896 * first figure out how many group descriptors we need to
1897 * handle the number of inodes we have
1899 inode_count = fs->super->s_inodes_count -
1900 fs->super->s_free_inodes_count;
1901 blks_needed = ext2fs_div_ceil(inode_count,
1902 fs->super->s_inodes_per_group) *
1903 EXT2_BLOCKS_PER_GROUP(fs->super);
1904 groups = ext2fs_div64_ceil(blks_needed,
1905 EXT2_BLOCKS_PER_GROUP(fs->super));
1908 * we need to figure out how many backup superblocks we have so we can
1909 * account for that in the metadata
1911 for (grp = 0; grp < fs->group_desc_count; grp++) {
1912 if (ext2fs_bg_has_super(fs, grp))
1913 num_of_superblocks++;
1916 /* calculate how many blocks are needed for data */
1917 data_needed = ext2fs_blocks_count(fs->super) -
1918 ext2fs_free_blocks_count(fs->super);
1919 data_needed -= SUPER_OVERHEAD(fs) * num_of_superblocks;
1920 data_needed -= META_OVERHEAD(fs) * fs->group_desc_count;
1922 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1924 * For ext4 we need to allow for up to a flex_bg worth
1925 * of inode tables of slack space so the resize
1926 * operation can be guaranteed to finish.
1928 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
1929 data_needed += META_OVERHEAD(fs) * extra_groups;
1930 extra_groups = groups % flexbg_size;
1934 * figure out how many data blocks we have given the number of groups
1935 * we need for our inodes
1937 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
1939 for (grp = 0; grp < groups; grp++) {
1940 overhead = META_OVERHEAD(fs);
1942 if (ext2fs_bg_has_super(fs, grp))
1943 overhead += SUPER_OVERHEAD(fs);
1946 * we want to keep track of how much data we can store in
1947 * the groups leading up to the last group so we can determine
1948 * how big the last group needs to be
1950 if (grp != (groups - 1))
1951 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
1954 data_blocks -= overhead;
1958 * if we need more group descriptors in order to accomodate our data
1959 * then we need to add them here
1961 while (data_needed > data_blocks) {
1962 blk64_t remainder = data_needed - data_blocks;
1965 /* figure out how many more groups we need for the data */
1966 extra_grps = ext2fs_div64_ceil(remainder,
1967 EXT2_BLOCKS_PER_GROUP(fs->super));
1969 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
1971 /* ok we have to account for the last group */
1972 overhead = META_OVERHEAD(fs);
1973 if (ext2fs_bg_has_super(fs, groups-1))
1974 overhead += SUPER_OVERHEAD(fs);
1975 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
1977 for (grp = groups; grp < groups+extra_grps; grp++) {
1978 overhead = META_OVERHEAD(fs);
1979 if (ext2fs_bg_has_super(fs, grp))
1980 overhead += SUPER_OVERHEAD(fs);
1983 * again, we need to see how much data we cram into
1984 * all of the groups leading up to the last group
1986 if (grp != (groups + extra_grps - 1))
1987 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
1990 data_blocks -= overhead;
1993 groups += extra_grps;
1994 extra_groups += extra_grps;
1995 if (fs->super->s_feature_incompat
1996 & EXT4_FEATURE_INCOMPAT_FLEX_BG
1997 && extra_groups > flexbg_size) {
1999 * For ext4 we need to allow for up to a flex_bg worth
2000 * of inode tables of slack space so the resize
2001 * operation can be guaranteed to finish.
2003 extra_groups = flexbg_size -
2004 (groups & (flexbg_size - 1));
2005 data_needed += META_OVERHEAD(fs) * extra_groups;
2006 extra_groups = groups % flexbg_size;
2010 /* now for the fun voodoo */
2011 overhead = META_OVERHEAD(fs);
2014 * if this is the case then the last group is going to have data in it
2015 * so we need to adjust the size of the last group accordingly
2017 if (last_start < data_needed) {
2018 blk64_t remainder = data_needed - last_start;
2021 * 50 is a magic number that mkfs/resize uses to see if its
2022 * even worth making/resizing the fs. basically you need to
2023 * have at least 50 blocks in addition to the blocks needed
2024 * for the metadata in the last group
2027 overhead += remainder;
2033 if (ext2fs_bg_has_super(fs, groups-1))
2034 overhead += SUPER_OVERHEAD(fs);
2037 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2038 * only do groups-1, and then add the number of blocks needed to
2039 * handle the group descriptor metadata+data that we need
2041 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2042 blks_needed += overhead;
2045 * If at this point we've already added up more "needed" than
2046 * the current size, just return current size as minimum.
2048 if (blks_needed >= ext2fs_blocks_count(fs->super))
2049 return ext2fs_blocks_count(fs->super);
2051 * We need to reserve a few extra blocks if extents are
2052 * enabled, in case we need to grow the extent tree. The more
2053 * we shrink the file system, the more space we need.
2055 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
2056 blks_needed += (ext2fs_blocks_count(fs->super) -