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);
54 static errcode_t mark_table_blocks(ext2_filsys fs,
55 ext2fs_block_bitmap bmap);
58 * Some helper CPP macros
60 #define IS_BLOCK_BM(fs, i, blk) ((blk) == ext2fs_block_bitmap_loc((fs),(i)))
61 #define IS_INODE_BM(fs, i, blk) ((blk) == ext2fs_inode_bitmap_loc((fs),(i)))
63 #define IS_INODE_TB(fs, i, blk) (((blk) >= ext2fs_inode_table_loc((fs), (i))) && \
64 ((blk) < (ext2fs_inode_table_loc((fs), (i)) + \
65 (fs)->inode_blocks_per_group)))
67 /* Some bigalloc helper macros which are more succint... */
68 #define B2C(x) EXT2FS_B2C(fs, (x))
69 #define C2B(x) EXT2FS_C2B(fs, (x))
70 #define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
71 #define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
72 #define LT_CLSTR(x, y) (B2C(x) < B2C(y))
73 #define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
74 #define GT_CLSTR(x, y) (B2C(x) > B2C(y))
79 * This is the top-level routine which does the dirty deed....
81 errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
82 errcode_t (*progress)(ext2_resize_t rfs, int pass,
84 unsigned long max_val))
88 struct resource_track rtrack, overall_track;
91 * Create the data structure
93 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
97 memset(rfs, 0, sizeof(struct ext2_resize_struct));
102 rfs->progress = progress;
104 init_resource_track(&overall_track, "overall resize2fs", fs->io);
105 init_resource_track(&rtrack, "read_bitmaps", fs->io);
106 retval = ext2fs_read_bitmaps(fs);
109 print_resource_track(rfs, &rtrack, fs->io);
111 fs->super->s_state |= EXT2_ERROR_FS;
112 ext2fs_mark_super_dirty(fs);
115 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io);
116 fix_uninit_block_bitmaps(fs);
117 print_resource_track(rfs, &rtrack, fs->io);
118 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
122 init_resource_track(&rtrack, "adjust_superblock", fs->io);
123 retval = adjust_superblock(rfs, *new_size);
126 print_resource_track(rfs, &rtrack, fs->io);
129 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io);
130 fix_uninit_block_bitmaps(rfs->new_fs);
131 print_resource_track(rfs, &rtrack, fs->io);
132 /* Clear the block bitmap uninit flag for the last block group */
133 ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
134 EXT2_BG_BLOCK_UNINIT);
136 *new_size = ext2fs_blocks_count(rfs->new_fs->super);
138 init_resource_track(&rtrack, "blocks_to_move", fs->io);
139 retval = blocks_to_move(rfs);
142 print_resource_track(rfs, &rtrack, fs->io);
144 #ifdef RESIZE2FS_DEBUG
145 if (rfs->flags & RESIZE_DEBUG_BMOVE)
146 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
147 ext2fs_free_blocks_count(rfs->old_fs->super),
148 ext2fs_free_blocks_count(rfs->new_fs->super),
152 init_resource_track(&rtrack, "block_mover", fs->io);
153 retval = block_mover(rfs);
156 print_resource_track(rfs, &rtrack, fs->io);
158 init_resource_track(&rtrack, "inode_scan_and_fix", fs->io);
159 retval = inode_scan_and_fix(rfs);
162 print_resource_track(rfs, &rtrack, fs->io);
164 init_resource_track(&rtrack, "inode_ref_fix", fs->io);
165 retval = inode_ref_fix(rfs);
168 print_resource_track(rfs, &rtrack, fs->io);
170 init_resource_track(&rtrack, "move_itables", fs->io);
171 retval = move_itables(rfs);
174 print_resource_track(rfs, &rtrack, fs->io);
176 init_resource_track(&rtrack, "calculate_summary_stats", fs->io);
177 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
180 print_resource_track(rfs, &rtrack, fs->io);
182 init_resource_track(&rtrack, "fix_resize_inode", fs->io);
183 retval = fix_resize_inode(rfs->new_fs);
186 print_resource_track(rfs, &rtrack, fs->io);
188 init_resource_track(&rtrack, "fix_sb_journal_backup", fs->io);
189 retval = fix_sb_journal_backup(rfs->new_fs);
192 print_resource_track(rfs, &rtrack, fs->io);
194 rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
195 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
197 print_resource_track(rfs, &overall_track, fs->io);
198 retval = ext2fs_close(rfs->new_fs);
204 ext2fs_free(rfs->old_fs);
206 ext2fs_free_mem(&rfs->itable_buf);
207 if (rfs->reserve_blocks)
208 ext2fs_free_block_bitmap(rfs->reserve_blocks);
209 if (rfs->move_blocks)
210 ext2fs_free_block_bitmap(rfs->move_blocks);
211 ext2fs_free_mem(&rfs);
217 ext2fs_free(rfs->new_fs);
219 ext2fs_free_mem(&rfs->itable_buf);
220 ext2fs_free_mem(&rfs);
225 * Clean up the bitmaps for unitialized bitmaps
227 static void fix_uninit_block_bitmaps(ext2_filsys fs)
233 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
234 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
237 for (g=0; g < fs->group_desc_count; g++) {
238 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
241 blk = ext2fs_group_first_block2(fs, g);
242 lblk = ext2fs_group_last_block2(fs, g);
243 ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
246 ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
247 ext2fs_mark_block_bitmap2(fs->block_map,
248 ext2fs_block_bitmap_loc(fs, g));
249 ext2fs_mark_block_bitmap2(fs->block_map,
250 ext2fs_inode_bitmap_loc(fs, g));
251 for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
252 i < (unsigned int) fs->inode_blocks_per_group;
254 ext2fs_mark_block_bitmap2(fs->block_map, blk);
258 /* --------------------------------------------------------------------
260 * Resize processing, phase 1.
262 * In this phase we adjust the in-memory superblock information, and
263 * initialize any new parts of the inode table. The new parts of the
264 * inode table are created in virgin disk space, so we can abort here
265 * without any side effects.
266 * --------------------------------------------------------------------
270 * If the group descriptor's bitmap and inode table blocks are valid,
271 * release them in the new filesystem data structure, and mark them as
272 * reserved so the old inode table blocks don't get overwritten.
274 static void free_gdp_blocks(ext2_filsys fs,
275 ext2fs_block_bitmap reserve_blocks,
276 struct ext2_group_desc *gdp)
281 if (gdp->bg_block_bitmap &&
282 (gdp->bg_block_bitmap < ext2fs_blocks_count(fs->super))) {
283 ext2fs_block_alloc_stats(fs, gdp->bg_block_bitmap, -1);
284 ext2fs_mark_block_bitmap2(reserve_blocks,
285 gdp->bg_block_bitmap);
288 if (gdp->bg_inode_bitmap &&
289 (gdp->bg_inode_bitmap < ext2fs_blocks_count(fs->super))) {
290 ext2fs_block_alloc_stats(fs, gdp->bg_inode_bitmap, -1);
291 ext2fs_mark_block_bitmap2(reserve_blocks,
292 gdp->bg_inode_bitmap);
295 if (gdp->bg_inode_table == 0 ||
296 (gdp->bg_inode_table >= ext2fs_blocks_count(fs->super)))
299 for (blk = gdp->bg_inode_table, j = 0;
300 j < fs->inode_blocks_per_group; j++, blk++) {
301 if (blk >= ext2fs_blocks_count(fs->super))
303 ext2fs_block_alloc_stats(fs, blk, -1);
304 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
309 * This routine is shared by the online and offline resize routines.
310 * All of the information which is adjusted in memory is done here.
312 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
313 ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
316 blk64_t overhead = 0;
318 blk64_t blk, group_block;
320 blk64_t old_numblocks, numblocks, adjblocks;
321 unsigned long i, j, old_desc_blocks;
322 unsigned int meta_bg, meta_bg_size;
323 int has_super, csum_flag;
324 unsigned long long new_inodes; /* u64 to check for overflow */
327 ext2fs_blocks_count_set(fs->super, new_size);
330 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
331 fs->super->s_first_data_block,
332 EXT2_BLOCKS_PER_GROUP(fs->super));
333 if (fs->group_desc_count == 0)
334 return EXT2_ET_TOOSMALL;
335 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
336 EXT2_DESC_PER_BLOCK(fs->super));
339 * Overhead is the number of bookkeeping blocks per group. It
340 * includes the superblock backup, the group descriptor
341 * backups, the inode bitmap, the block bitmap, and the inode
344 overhead = (int) (2 + fs->inode_blocks_per_group);
346 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
347 overhead += 1 + fs->desc_blocks +
348 fs->super->s_reserved_gdt_blocks;
351 * See if the last group is big enough to support the
352 * necessary data structures. If not, we need to get rid of
355 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
356 fs->super->s_blocks_per_group;
357 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
358 return EXT2_ET_TOOSMALL;
359 if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
360 ext2fs_blocks_count_set(fs->super,
361 ext2fs_blocks_count(fs->super) - rem);
365 * Adjust the number of inodes
367 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
368 if (new_inodes > ~0U) {
369 fprintf(stderr, _("inodes (%llu) must be less than %u"),
371 return EXT2_ET_TOO_MANY_INODES;
373 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
374 fs->group_desc_count;
377 * Adjust the number of free blocks
379 blk = ext2fs_blocks_count(old_fs->super);
380 if (blk > ext2fs_blocks_count(fs->super))
381 ext2fs_free_blocks_count_set(fs->super,
382 ext2fs_free_blocks_count(fs->super) -
383 (blk - ext2fs_blocks_count(fs->super)));
385 ext2fs_free_blocks_count_set(fs->super,
386 ext2fs_free_blocks_count(fs->super) +
387 (ext2fs_blocks_count(fs->super) - blk));
390 * Adjust the number of reserved blocks
392 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
393 ext2fs_blocks_count(old_fs->super);
394 ext2fs_r_blocks_count_set(fs->super,
395 (percent * ext2fs_blocks_count(fs->super) /
399 * Adjust the bitmaps for size
401 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
402 fs->super->s_inodes_count,
404 if (retval) goto errout;
406 real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) *
407 fs->group_desc_count)) - 1 +
408 fs->super->s_first_data_block;
409 retval = ext2fs_resize_block_bitmap2(new_size - 1,
410 real_end, fs->block_map);
411 if (retval) goto errout;
414 * If we are growing the file system, also grow the size of
415 * the reserve_blocks bitmap
417 if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) {
418 retval = ext2fs_resize_block_bitmap2(new_size - 1,
419 real_end, reserve_blocks);
420 if (retval) goto errout;
424 * Reallocate the group descriptors as necessary.
426 if (old_fs->desc_blocks != fs->desc_blocks) {
427 retval = ext2fs_resize_mem(old_fs->desc_blocks *
429 fs->desc_blocks * fs->blocksize,
433 if (fs->desc_blocks > old_fs->desc_blocks)
434 memset((char *) fs->group_desc +
435 (old_fs->desc_blocks * fs->blocksize), 0,
436 (fs->desc_blocks - old_fs->desc_blocks) *
441 * If the resize_inode feature is set, and we are changing the
442 * number of descriptor blocks, then adjust
443 * s_reserved_gdt_blocks if possible to avoid needing to move
444 * the inode table either now or in the future.
446 if ((fs->super->s_feature_compat &
447 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
448 (old_fs->desc_blocks != fs->desc_blocks)) {
451 new = ((int) fs->super->s_reserved_gdt_blocks) +
452 (old_fs->desc_blocks - fs->desc_blocks);
455 if (new > (int) fs->blocksize/4)
456 new = fs->blocksize/4;
457 fs->super->s_reserved_gdt_blocks = new;
461 * If we are shrinking the number of block groups, we're done
464 if (old_fs->group_desc_count > fs->group_desc_count) {
466 * Check the block groups that we are chopping off
467 * and free any blocks associated with their metadata
469 for (i = fs->group_desc_count;
470 i < old_fs->group_desc_count; i++) {
471 free_gdp_blocks(fs, reserve_blocks,
472 ext2fs_group_desc(old_fs,
473 old_fs->group_desc, i));
480 * Fix the count of the last (old) block group
482 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
483 old_fs->super->s_first_data_block) %
484 old_fs->super->s_blocks_per_group;
486 old_numblocks = old_fs->super->s_blocks_per_group;
487 if (old_fs->group_desc_count == fs->group_desc_count) {
488 numblocks = (ext2fs_blocks_count(fs->super) -
489 fs->super->s_first_data_block) %
490 fs->super->s_blocks_per_group;
492 numblocks = fs->super->s_blocks_per_group;
494 numblocks = fs->super->s_blocks_per_group;
495 i = old_fs->group_desc_count - 1;
496 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
497 ext2fs_group_desc_csum_set(fs, i);
500 * If the number of block groups is staying the same, we're
501 * done and can exit now. (If the number block groups is
502 * shrinking, we had exited earlier.)
504 if (old_fs->group_desc_count >= fs->group_desc_count) {
510 * Initialize the new block group descriptors
512 group_block = ext2fs_group_first_block2(fs,
513 old_fs->group_desc_count);
514 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
515 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
516 if (access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0)
517 lazy_itable_init = 1;
518 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
519 old_desc_blocks = fs->super->s_first_meta_bg;
521 old_desc_blocks = fs->desc_blocks +
522 fs->super->s_reserved_gdt_blocks;
525 * If we changed the number of block_group descriptor blocks,
526 * we need to make sure they are all marked as reserved in the
527 * file systems's block allocation map.
529 for (i = 0; i < old_fs->group_desc_count; i++)
530 ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
532 for (i = old_fs->group_desc_count;
533 i < fs->group_desc_count; i++) {
534 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
535 sizeof(struct ext2_group_desc));
538 ext2fs_bg_flags_zap(fs, i);
540 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
541 if (!lazy_itable_init)
542 ext2fs_bg_flags_set(fs, i,
543 EXT2_BG_INODE_ZEROED);
544 ext2fs_bg_itable_unused_set(fs, i,
545 fs->super->s_inodes_per_group);
548 numblocks = ext2fs_group_blocks_count(fs, i);
549 if ((i < fs->group_desc_count - 1) && csum_flag)
550 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
552 has_super = ext2fs_bg_has_super(fs, i);
554 ext2fs_block_alloc_stats2(fs, group_block, +1);
557 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
558 meta_bg = i / meta_bg_size;
559 if (!(fs->super->s_feature_incompat &
560 EXT2_FEATURE_INCOMPAT_META_BG) ||
561 (meta_bg < fs->super->s_first_meta_bg)) {
563 for (j=0; j < old_desc_blocks; j++)
564 ext2fs_block_alloc_stats2(fs,
565 group_block + 1 + j, +1);
566 adjblocks += old_desc_blocks;
571 if (((i % meta_bg_size) == 0) ||
572 ((i % meta_bg_size) == 1) ||
573 ((i % meta_bg_size) == (meta_bg_size-1)))
574 ext2fs_block_alloc_stats2(fs,
575 group_block + has_super, +1);
578 adjblocks += 2 + fs->inode_blocks_per_group;
580 numblocks -= adjblocks;
581 ext2fs_free_blocks_count_set(fs->super,
582 ext2fs_free_blocks_count(fs->super) - adjblocks);
583 fs->super->s_free_inodes_count +=
584 fs->super->s_inodes_per_group;
585 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
586 ext2fs_bg_free_inodes_count_set(fs, i,
587 fs->super->s_inodes_per_group);
588 ext2fs_bg_used_dirs_count_set(fs, i, 0);
589 ext2fs_group_desc_csum_set(fs, i);
591 retval = ext2fs_allocate_group_table(fs, i, 0);
592 if (retval) goto errout;
594 group_block += fs->super->s_blocks_per_group;
599 * Mark all of the metadata blocks as reserved so they won't
600 * get allocated by the call to ext2fs_allocate_group_table()
601 * in blocks_to_move(), where we allocate new blocks to
602 * replace those allocation bitmap and inode table blocks
603 * which have to get relocated to make space for an increased
604 * number of the block group descriptors.
607 mark_table_blocks(fs, reserve_blocks);
614 * This routine adjusts the superblock and other data structures, both
615 * in disk as well as in memory...
617 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
624 unsigned long max_group;
627 ext2fs_mark_super_dirty(fs);
628 ext2fs_mark_bb_dirty(fs);
629 ext2fs_mark_ib_dirty(fs);
631 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
632 &rfs->reserve_blocks);
636 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
641 * Check to make sure there are enough inodes
643 if ((rfs->old_fs->super->s_inodes_count -
644 rfs->old_fs->super->s_free_inodes_count) >
645 rfs->new_fs->super->s_inodes_count) {
651 * If we are shrinking the number block groups, we're done and
654 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
660 * If the number of block groups is staying the same, we're
661 * done and can exit now. (If the number block groups is
662 * shrinking, we had exited earlier.)
664 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
670 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
671 * supports lazy inode initialization, we can skip
672 * initializing the inode table.
674 if (lazy_itable_init &&
675 EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
676 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
682 * Initialize the inode table
684 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
689 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
690 group_block = ext2fs_group_first_block2(fs,
691 rfs->old_fs->group_desc_count);
692 adj = rfs->old_fs->group_desc_count;
693 max_group = fs->group_desc_count - adj;
695 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
700 for (i = rfs->old_fs->group_desc_count;
701 i < fs->group_desc_count; i++) {
703 * Write out the new inode table
705 retval = io_channel_write_blk64(fs->io,
706 ext2fs_inode_table_loc(fs, i),
707 fs->inode_blocks_per_group,
709 if (retval) goto errout;
711 io_channel_flush(fs->io);
713 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
714 i - adj + 1, max_group);
718 group_block += fs->super->s_blocks_per_group;
720 io_channel_flush(fs->io);
727 /* --------------------------------------------------------------------
729 * Resize processing, phase 2.
731 * In this phase we adjust determine which blocks need to be moved, in
732 * blocks_to_move(). We then copy the blocks to their ultimate new
733 * destinations using block_mover(). Since we are copying blocks to
734 * their new locations, again during this pass we can abort without
736 * --------------------------------------------------------------------
740 * This helper function creates a block bitmap with all of the
741 * filesystem meta-data blocks.
743 static errcode_t mark_table_blocks(ext2_filsys fs,
744 ext2fs_block_bitmap bmap)
749 for (i = 0; i < fs->group_desc_count; i++) {
750 ext2fs_reserve_super_and_bgd(fs, i, bmap);
753 * Mark the blocks used for the inode table
755 blk = ext2fs_inode_table_loc(fs, i);
757 ext2fs_mark_block_bitmap_range2(bmap, blk,
758 fs->inode_blocks_per_group);
761 * Mark block used for the block bitmap
763 blk = ext2fs_block_bitmap_loc(fs, i);
765 ext2fs_mark_block_bitmap2(bmap, blk);
768 * Mark block used for the inode bitmap
770 blk = ext2fs_inode_bitmap_loc(fs, i);
772 ext2fs_mark_block_bitmap2(bmap, blk);
778 * This function checks to see if a particular block (either a
779 * superblock or a block group descriptor) overlaps with an inode or
780 * block bitmap block, or with the inode table.
782 static void mark_fs_metablock(ext2_resize_t rfs,
783 ext2fs_block_bitmap meta_bmap,
784 int group, blk64_t blk)
786 ext2_filsys fs = rfs->new_fs;
788 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
789 ext2fs_block_alloc_stats2(fs, blk, +1);
792 * Check to see if we overlap with the inode or block bitmap,
793 * or the inode tables. If not, and the block is in use, then
794 * mark it as a block to be moved.
796 if (IS_BLOCK_BM(fs, group, blk)) {
797 ext2fs_block_bitmap_loc_set(fs, group, 0);
798 rfs->needed_blocks++;
801 if (IS_INODE_BM(fs, group, blk)) {
802 ext2fs_inode_bitmap_loc_set(fs, group, 0);
803 rfs->needed_blocks++;
806 if (IS_INODE_TB(fs, group, blk)) {
807 ext2fs_inode_table_loc_set(fs, group, 0);
808 rfs->needed_blocks++;
811 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
814 for (i=0; i < rfs->old_fs->group_desc_count; i++) {
815 if (IS_BLOCK_BM(fs, i, blk)) {
816 ext2fs_block_bitmap_loc_set(fs, i, 0);
817 rfs->needed_blocks++;
820 if (IS_INODE_BM(fs, i, blk)) {
821 ext2fs_inode_bitmap_loc_set(fs, i, 0);
822 rfs->needed_blocks++;
825 if (IS_INODE_TB(fs, i, blk)) {
826 ext2fs_inode_table_loc_set(fs, i, 0);
827 rfs->needed_blocks++;
832 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
833 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
834 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
836 * If the block bitmap is uninitialized, which means
837 * nothing other than standard metadata in use.
840 } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
841 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
842 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
843 rfs->needed_blocks++;
849 * This routine marks and unmarks reserved blocks in the new block
850 * bitmap. It also determines which blocks need to be moved and
851 * places this information into the move_blocks bitmap.
853 static errcode_t blocks_to_move(ext2_resize_t rfs)
856 dgrp_t i, max_groups, g;
857 blk64_t blk, group_blk;
858 blk64_t old_blocks, new_blocks;
860 unsigned int meta_bg, meta_bg_size;
862 ext2_filsys fs, old_fs;
863 ext2fs_block_bitmap meta_bmap;
867 old_fs = rfs->old_fs;
868 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
871 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
876 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
881 retval = mark_table_blocks(old_fs, meta_bmap);
888 * If we're shrinking the filesystem, we need to move any group's
889 * bitmaps which are beyond the end of the new filesystem.
891 new_size = ext2fs_blocks_count(fs->super);
892 if (new_size < ext2fs_blocks_count(old_fs->super)) {
893 for (g = 0; g < fs->group_desc_count; g++) {
895 * ext2fs_allocate_group_table re-allocates bitmaps
896 * which are set to block 0.
898 if (ext2fs_block_bitmap_loc(fs, g) >= new_size) {
899 ext2fs_block_bitmap_loc_set(fs, g, 0);
900 retval = ext2fs_allocate_group_table(fs, g, 0);
904 if (ext2fs_inode_bitmap_loc(fs, g) >= new_size) {
905 ext2fs_inode_bitmap_loc_set(fs, g, 0);
906 retval = ext2fs_allocate_group_table(fs, g, 0);
914 * If we're shrinking the filesystem, we need to move all of
915 * the blocks that don't fit any more
917 for (blk = ext2fs_blocks_count(fs->super);
918 blk < ext2fs_blocks_count(old_fs->super); blk++) {
919 g = ext2fs_group_of_blk2(fs, blk);
920 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
921 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
922 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
924 * The block bitmap is uninitialized, so skip
925 * to the next block group.
927 blk = ext2fs_group_first_block2(fs, g+1) - 1;
930 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
931 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
932 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
933 rfs->needed_blocks++;
935 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
938 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
939 old_blocks = old_fs->super->s_first_meta_bg;
940 new_blocks = fs->super->s_first_meta_bg;
942 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
943 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
946 if (old_blocks == new_blocks) {
951 max_groups = fs->group_desc_count;
952 if (max_groups > old_fs->group_desc_count)
953 max_groups = old_fs->group_desc_count;
954 group_blk = old_fs->super->s_first_data_block;
956 * If we're reducing the number of descriptor blocks, this
957 * makes life easy. :-) We just have to mark some extra
960 if (old_blocks > new_blocks) {
961 for (i = 0; i < max_groups; i++) {
962 if (!ext2fs_bg_has_super(fs, i)) {
963 group_blk += fs->super->s_blocks_per_group;
966 for (blk = group_blk+1+new_blocks;
967 blk < group_blk+1+old_blocks; blk++) {
968 ext2fs_block_alloc_stats2(fs, blk, -1);
969 rfs->needed_blocks--;
971 group_blk += fs->super->s_blocks_per_group;
977 * If we're increasing the number of descriptor blocks, life
978 * gets interesting....
980 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
981 flex_bg = fs->super->s_feature_incompat &
982 EXT4_FEATURE_INCOMPAT_FLEX_BG;
983 /* first reserve all of the existing fs meta blocks */
984 for (i = 0; i < max_groups; i++) {
985 has_super = ext2fs_bg_has_super(fs, i);
987 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
989 meta_bg = i / meta_bg_size;
990 if (!(fs->super->s_feature_incompat &
991 EXT2_FEATURE_INCOMPAT_META_BG) ||
992 (meta_bg < fs->super->s_first_meta_bg)) {
994 for (blk = group_blk+1;
995 blk < group_blk + 1 + new_blocks; blk++)
996 mark_fs_metablock(rfs, meta_bmap,
1002 if (((i % meta_bg_size) == 0) ||
1003 ((i % meta_bg_size) == 1) ||
1004 ((i % meta_bg_size) == (meta_bg_size-1)))
1005 mark_fs_metablock(rfs, meta_bmap, i,
1006 group_blk + has_super);
1010 * Reserve the existing meta blocks that we know
1011 * aren't to be moved.
1013 * For flex_bg file systems, in order to avoid
1014 * overwriting fs metadata (especially inode table
1015 * blocks) belonging to a different block group when
1016 * we are relocating the inode tables, we need to
1017 * reserve all existing fs metadata blocks.
1019 if (ext2fs_block_bitmap_loc(fs, i))
1020 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1021 ext2fs_block_bitmap_loc(fs, i));
1022 else if (flex_bg && i < old_fs->group_desc_count)
1023 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1024 ext2fs_block_bitmap_loc(old_fs, i));
1026 if (ext2fs_inode_bitmap_loc(fs, i))
1027 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1028 ext2fs_inode_bitmap_loc(fs, i));
1029 else if (flex_bg && i < old_fs->group_desc_count)
1030 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1031 ext2fs_inode_bitmap_loc(old_fs, i));
1033 if (ext2fs_inode_table_loc(fs, i))
1034 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1035 ext2fs_inode_table_loc(fs, i),
1036 fs->inode_blocks_per_group);
1037 else if (flex_bg && i < old_fs->group_desc_count)
1038 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1039 ext2fs_inode_table_loc(old_fs, i),
1040 old_fs->inode_blocks_per_group);
1042 group_blk += rfs->new_fs->super->s_blocks_per_group;
1045 /* Allocate the missing data structures */
1046 for (i = 0; i < max_groups; i++) {
1047 if (ext2fs_inode_table_loc(fs, i) &&
1048 ext2fs_inode_bitmap_loc(fs, i) &&
1049 ext2fs_block_bitmap_loc(fs, i))
1052 retval = ext2fs_allocate_group_table(fs, i,
1053 rfs->reserve_blocks);
1058 * For those structures that have changed, we need to
1061 if (ext2fs_block_bitmap_loc(old_fs, i) !=
1062 (blk = ext2fs_block_bitmap_loc(fs, i))) {
1063 ext2fs_block_alloc_stats2(fs, blk, +1);
1064 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1065 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1066 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1069 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
1070 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
1071 ext2fs_block_alloc_stats2(fs, blk, +1);
1072 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1073 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1074 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1079 * The inode table, if we need to relocate it, is
1080 * handled specially. We have to reserve the blocks
1081 * for both the old and the new inode table, since we
1082 * can't have the inode table be destroyed during the
1083 * block relocation phase.
1085 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
1086 continue; /* inode table not moved */
1088 rfs->needed_blocks += fs->inode_blocks_per_group;
1091 * Mark the new inode table as in use in the new block
1092 * allocation bitmap, and move any blocks that might
1095 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
1096 j < fs->inode_blocks_per_group ; j++, blk++) {
1097 ext2fs_block_alloc_stats2(fs, blk, +1);
1098 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1099 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1100 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1105 * Make sure the old inode table is reserved in the
1106 * block reservation bitmap.
1108 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1109 j < fs->inode_blocks_per_group ; j++, blk++)
1110 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1116 ext2fs_free_block_bitmap(meta_bmap);
1122 * This helper function tries to allocate a new block. We try to
1123 * avoid hitting the original group descriptor blocks at least at
1124 * first, since we want to make it possible to recover from a badly
1125 * aborted resize operation as much as possible.
1127 * In the future, I may further modify this routine to balance out
1128 * where we get the new blocks across the various block groups.
1129 * Ideally we would allocate blocks that corresponded with the block
1130 * group of the containing inode, and keep contiguous blocks
1131 * together. However, this very difficult to do efficiently, since we
1132 * don't have the necessary information up front.
1136 #define DESPERATION 2
1138 static void init_block_alloc(ext2_resize_t rfs)
1140 rfs->alloc_state = AVOID_OLD;
1141 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1143 /* HACK for testing */
1144 if (ext2fs_blocks_count(rfs->new_fs->super) >
1145 ext2fs_blocks_count(rfs->old_fs->super))
1146 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1150 static blk64_t get_new_block(ext2_resize_t rfs)
1152 ext2_filsys fs = rfs->new_fs;
1155 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1156 if (rfs->alloc_state == DESPERATION)
1159 #ifdef RESIZE2FS_DEBUG
1160 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1161 printf("Going into desperation mode "
1162 "for block allocations\n");
1164 rfs->alloc_state = DESPERATION;
1165 rfs->new_blk = fs->super->s_first_data_block;
1168 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1169 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1171 ((rfs->alloc_state == AVOID_OLD) &&
1172 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1173 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1178 return rfs->new_blk;
1182 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1185 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1188 blk = get_new_block(rfs);
1192 #ifdef RESIZE2FS_DEBUG
1193 if (rfs->flags & 0xF)
1194 printf("get_alloc_block allocating %llu\n", blk);
1197 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1198 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1199 *ret = (blk64_t) blk;
1203 static errcode_t block_mover(ext2_resize_t rfs)
1205 blk64_t blk, old_blk, new_blk;
1206 ext2_filsys fs = rfs->new_fs;
1207 ext2_filsys old_fs = rfs->old_fs;
1212 ext2_badblocks_list badblock_list = 0;
1213 int bb_modified = 0;
1215 fs->get_alloc_block = resize2fs_get_alloc_block;
1216 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1218 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1222 new_blk = fs->super->s_first_data_block;
1223 if (!rfs->itable_buf) {
1224 retval = ext2fs_get_array(fs->blocksize,
1225 fs->inode_blocks_per_group,
1230 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1235 * The first step is to figure out where all of the blocks
1238 to_move = moved = 0;
1239 init_block_alloc(rfs);
1240 for (blk = B2C(old_fs->super->s_first_data_block);
1241 blk < ext2fs_blocks_count(old_fs->super);
1242 blk += EXT2FS_CLUSTER_RATIO(fs)) {
1243 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1245 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1247 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1248 ext2fs_badblocks_list_del(badblock_list, blk);
1253 new_blk = get_new_block(rfs);
1258 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1259 ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk));
1265 ext2fs_free_extent_table(rfs->bmap);
1273 * Step two is to actually move the blocks
1275 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1276 if (retval) goto errout;
1278 if (rfs->progress) {
1279 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1285 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1286 if (retval) goto errout;
1289 old_blk = C2B(old_blk);
1290 new_blk = C2B(new_blk);
1292 #ifdef RESIZE2FS_DEBUG
1293 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1294 printf("Moving %llu blocks %llu->%llu\n",
1295 size, old_blk, new_blk);
1299 if (c > fs->inode_blocks_per_group)
1300 c = fs->inode_blocks_per_group;
1301 retval = io_channel_read_blk64(fs->io, old_blk, c,
1303 if (retval) goto errout;
1304 retval = io_channel_write_blk64(fs->io, new_blk, c,
1306 if (retval) goto errout;
1311 if (rfs->progress) {
1312 io_channel_flush(fs->io);
1313 retval = (rfs->progress)(rfs,
1314 E2_RSZ_BLOCK_RELOC_PASS,
1320 io_channel_flush(fs->io);
1324 if (badblock_list) {
1325 if (!retval && bb_modified)
1326 retval = ext2fs_update_bb_inode(old_fs,
1328 ext2fs_badblocks_list_free(badblock_list);
1334 /* --------------------------------------------------------------------
1336 * Resize processing, phase 3
1338 * --------------------------------------------------------------------
1343 * The extent translation table is stored in clusters so we need to
1344 * take special care when mapping a source block number to its
1345 * destination block number.
1347 __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc)
1349 __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc)));
1352 new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1);
1356 struct process_block_struct {
1359 struct ext2_inode * inode;
1365 static int process_block(ext2_filsys fs, blk64_t *block_nr,
1366 e2_blkcnt_t blockcnt,
1367 blk64_t ref_block EXT2FS_ATTR((unused)),
1368 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1370 struct process_block_struct *pb;
1372 blk64_t block, new_block;
1375 pb = (struct process_block_struct *) priv_data;
1377 if (pb->rfs->bmap) {
1378 new_block = extent_translate(fs, pb->rfs->bmap, block);
1380 *block_nr = new_block;
1381 ret |= BLOCK_CHANGED;
1383 #ifdef RESIZE2FS_DEBUG
1384 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1385 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1386 pb->ino, blockcnt, block, new_block);
1392 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1393 block, (int) blockcnt);
1405 static errcode_t progress_callback(ext2_filsys fs,
1406 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1407 dgrp_t group, void * priv_data)
1409 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1413 * This check is to protect against old ext2 libraries. It
1414 * shouldn't be needed against new libraries.
1419 if (rfs->progress) {
1420 io_channel_flush(fs->io);
1421 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1422 group+1, fs->group_desc_count);
1430 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1432 struct process_block_struct pb;
1433 ext2_ino_t ino, new_inode;
1434 struct ext2_inode *inode = NULL;
1435 ext2_inode_scan scan = NULL;
1437 char *block_buf = 0;
1438 ext2_ino_t start_to_move;
1443 if ((rfs->old_fs->group_desc_count <=
1444 rfs->new_fs->group_desc_count) &&
1449 * Save the original size of the old filesystem, and
1450 * temporarily set the size to be the new size if the new size
1451 * is larger. We need to do this to avoid catching an error
1452 * by the block iterator routines
1454 orig_size = ext2fs_blocks_count(rfs->old_fs->super);
1455 if (orig_size < ext2fs_blocks_count(rfs->new_fs->super))
1456 ext2fs_blocks_count_set(rfs->old_fs->super,
1457 ext2fs_blocks_count(rfs->new_fs->super));
1459 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1460 if (retval) goto errout;
1462 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1463 if (retval) goto errout;
1464 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1465 if (retval) goto errout;
1467 start_to_move = (rfs->new_fs->group_desc_count *
1468 rfs->new_fs->super->s_inodes_per_group);
1470 if (rfs->progress) {
1471 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1472 0, rfs->old_fs->group_desc_count);
1476 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1480 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1481 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1482 inode = malloc(inode_size);
1488 * First, copy all of the inodes that need to be moved
1489 * elsewhere in the inode table
1492 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1493 if (retval) goto errout;
1497 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1498 continue; /* inode not in use */
1500 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1503 if (ext2fs_file_acl_block(rfs->old_fs, inode) && rfs->bmap) {
1504 new_block = extent_translate(rfs->old_fs, rfs->bmap,
1505 ext2fs_file_acl_block(rfs->old_fs, inode));
1507 ext2fs_file_acl_block_set(rfs->old_fs, inode,
1509 retval = ext2fs_write_inode_full(rfs->old_fs,
1510 ino, inode, inode_size);
1511 if (retval) goto errout;
1515 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
1516 (rfs->bmap || pb.is_dir)) {
1518 retval = ext2fs_block_iterate3(rfs->old_fs,
1520 process_block, &pb);
1529 if (ino <= start_to_move)
1530 continue; /* Don't need to move it. */
1535 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1539 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1542 /* Get the new version of the inode */
1543 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1545 if (retval) goto errout;
1547 inode->i_ctime = time(0);
1548 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1550 if (retval) goto errout;
1552 #ifdef RESIZE2FS_DEBUG
1553 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1554 printf("Inode moved %u->%u\n", ino, new_inode);
1557 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1561 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1563 io_channel_flush(rfs->old_fs->io);
1566 ext2fs_blocks_count_set(rfs->old_fs->super, orig_size);
1568 ext2fs_free_extent_table(rfs->bmap);
1572 ext2fs_close_inode_scan(scan);
1574 ext2fs_free_mem(&block_buf);
1579 /* --------------------------------------------------------------------
1581 * Resize processing, phase 4.
1583 * --------------------------------------------------------------------
1589 unsigned int max_dirs;
1593 static int check_and_change_inodes(ext2_ino_t dir,
1594 int entry EXT2FS_ATTR((unused)),
1595 struct ext2_dir_entry *dirent, int offset,
1596 int blocksize EXT2FS_ATTR((unused)),
1597 char *buf EXT2FS_ATTR((unused)),
1600 struct istruct *is = (struct istruct *) priv_data;
1601 struct ext2_inode inode;
1602 ext2_ino_t new_inode;
1605 if (is->rfs->progress && offset == 0) {
1606 io_channel_flush(is->rfs->old_fs->io);
1607 is->err = (is->rfs->progress)(is->rfs,
1608 E2_RSZ_INODE_REF_UPD_PASS,
1609 ++is->num, is->max_dirs);
1611 return DIRENT_ABORT;
1617 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1621 #ifdef RESIZE2FS_DEBUG
1622 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1623 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1624 dir, dirent->name_len&0xFF, dirent->name,
1625 dirent->inode, new_inode);
1628 dirent->inode = new_inode;
1630 /* Update the directory mtime and ctime */
1631 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1633 inode.i_mtime = inode.i_ctime = time(0);
1634 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1636 return DIRENT_ABORT;
1639 return DIRENT_CHANGED;
1642 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1651 * Now, we iterate over all of the directories to update the
1655 is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
1659 if (rfs->progress) {
1660 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1666 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1667 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1668 check_and_change_inodes, &is);
1676 if (rfs->progress && (is.num < is.max_dirs))
1677 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1678 is.max_dirs, is.max_dirs);
1681 ext2fs_free_extent_table(rfs->imap);
1687 /* --------------------------------------------------------------------
1689 * Resize processing, phase 5.
1691 * In this phase we actually move the inode table around, and then
1692 * update the summary statistics. This is scary, since aborting here
1693 * will potentially scramble the filesystem. (We are moving the
1694 * inode tables around in place, and so the potential for lost data,
1695 * or at the very least scrambling the mapping between filenames and
1696 * inode numbers is very high in case of a power failure here.)
1697 * --------------------------------------------------------------------
1702 * A very scary routine --- this one moves the inode table around!!!
1704 * After this you have to use the rfs->new_fs file handle to read and
1707 static errcode_t move_itables(ext2_resize_t rfs)
1711 dgrp_t i, max_groups;
1712 ext2_filsys fs = rfs->new_fs;
1714 blk64_t old_blk, new_blk, blk;
1716 int j, to_move, moved;
1718 max_groups = fs->group_desc_count;
1719 if (max_groups > rfs->old_fs->group_desc_count)
1720 max_groups = rfs->old_fs->group_desc_count;
1722 size = fs->blocksize * fs->inode_blocks_per_group;
1723 if (!rfs->itable_buf) {
1724 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1730 * Figure out how many inode tables we need to move
1732 to_move = moved = 0;
1733 for (i=0; i < max_groups; i++)
1734 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
1735 ext2fs_inode_table_loc(fs, i))
1741 if (rfs->progress) {
1742 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1748 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1750 for (i=0; i < max_groups; i++) {
1751 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
1752 new_blk = ext2fs_inode_table_loc(fs, i);
1753 diff = new_blk - old_blk;
1755 #ifdef RESIZE2FS_DEBUG
1756 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1757 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
1758 i, old_blk, new_blk, diff);
1764 retval = io_channel_read_blk64(fs->io, old_blk,
1765 fs->inode_blocks_per_group,
1770 * The end of the inode table segment often contains
1771 * all zeros, and we're often only moving the inode
1772 * table down a block or two. If so, we can optimize
1773 * things by not rewriting blocks that we know to be zero
1776 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1779 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1780 #ifdef RESIZE2FS_DEBUG
1781 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1782 printf("%d blocks of zeros...\n", n);
1784 num = fs->inode_blocks_per_group;
1788 retval = io_channel_write_blk64(fs->io, new_blk,
1789 num, rfs->itable_buf);
1791 io_channel_write_blk64(fs->io, old_blk,
1792 num, rfs->itable_buf);
1796 retval = io_channel_write_blk64(fs->io,
1797 old_blk + fs->inode_blocks_per_group,
1798 diff, (rfs->itable_buf +
1799 (fs->inode_blocks_per_group - diff) *
1805 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1806 j < fs->inode_blocks_per_group ; j++, blk++)
1807 ext2fs_block_alloc_stats2(fs, blk, -1);
1809 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
1810 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1811 ext2fs_mark_super_dirty(rfs->old_fs);
1812 ext2fs_flush(rfs->old_fs);
1814 if (rfs->progress) {
1815 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1821 mark_table_blocks(fs, fs->block_map);
1823 #ifdef RESIZE2FS_DEBUG
1824 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1825 printf("Inode table move finished.\n");
1834 * Fix the resize inode
1836 static errcode_t fix_resize_inode(ext2_filsys fs)
1838 struct ext2_inode inode;
1840 char *block_buf = NULL;
1842 if (!(fs->super->s_feature_compat &
1843 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1846 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1847 if (retval) goto errout;
1849 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1850 if (retval) goto errout;
1852 ext2fs_iblk_set(fs, &inode, 1);
1854 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1855 if (retval) goto errout;
1857 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1859 * Avoid zeroing out block #0; that's rude. This
1860 * should never happen anyway since the filesystem
1861 * should be fsck'ed and we assume it is consistent.
1864 _("Should never happen: resize inode corrupt!\n"));
1868 memset(block_buf, 0, fs->blocksize);
1870 retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1872 if (retval) goto errout;
1874 retval = ext2fs_create_resize_inode(fs);
1880 ext2fs_free_mem(&block_buf);
1885 * Finally, recalculate the summary information
1887 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1891 unsigned int group = 0;
1892 unsigned int count = 0;
1893 blk64_t total_blocks_free = 0;
1894 int total_inodes_free = 0;
1897 blk64_t super_blk, old_desc_blk, new_desc_blk;
1898 int old_desc_blocks;
1901 * First calculate the block statistics
1903 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1904 ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
1906 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1907 old_desc_blocks = fs->super->s_first_meta_bg;
1909 old_desc_blocks = fs->desc_blocks +
1910 fs->super->s_reserved_gdt_blocks;
1911 for (blk = B2C(fs->super->s_first_data_block);
1912 blk < ext2fs_blocks_count(fs->super);
1913 blk += EXT2FS_CLUSTER_RATIO(fs)) {
1915 !(EQ_CLSTR(blk, super_blk) ||
1916 ((old_desc_blk && old_desc_blocks &&
1917 GE_CLSTR(blk, old_desc_blk) &&
1918 LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) ||
1919 ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) ||
1920 EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) ||
1921 EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) ||
1922 ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) &&
1923 LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group)
1924 + fs->inode_blocks_per_group))))) ||
1925 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
1927 total_blocks_free++;
1930 if ((count == fs->super->s_clusters_per_group) ||
1931 EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) {
1932 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
1933 ext2fs_group_desc_csum_set(fs, group);
1935 if (group >= fs->group_desc_count)
1939 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1940 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
1943 if (fs->super->s_feature_incompat &
1944 EXT2_FEATURE_INCOMPAT_META_BG)
1945 old_desc_blocks = fs->super->s_first_meta_bg;
1947 old_desc_blocks = fs->desc_blocks +
1948 fs->super->s_reserved_gdt_blocks;
1951 total_blocks_free = C2B(total_blocks_free);
1952 ext2fs_free_blocks_count_set(fs->super, total_blocks_free);
1955 * Next, calculate the inode statistics
1961 /* Protect loop from wrap-around if s_inodes_count maxed */
1962 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
1963 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1965 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1967 total_inodes_free++;
1970 if ((count == fs->super->s_inodes_per_group) ||
1971 (ino == fs->super->s_inodes_count)) {
1972 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
1973 ext2fs_group_desc_csum_set(fs, group);
1975 if (group >= fs->group_desc_count)
1979 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
1982 fs->super->s_free_inodes_count = total_inodes_free;
1983 ext2fs_mark_super_dirty(fs);
1988 * Journal may have been relocated; update the backup journal blocks
1989 * in the superblock.
1991 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
1994 struct ext2_inode inode;
1996 if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1999 /* External journal? Nothing to do. */
2000 if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
2003 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
2006 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
2007 fs->super->s_jnl_blocks[15] = inode.i_size_high;
2008 fs->super->s_jnl_blocks[16] = inode.i_size;
2009 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
2010 ext2fs_mark_super_dirty(fs);
2014 static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
2015 int old_desc_blocks)
2017 blk64_t super_blk, old_desc_blk, new_desc_blk;
2020 /* inode table blocks plus allocation bitmaps */
2021 overhead = fs->inode_blocks_per_group + 2;
2023 ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
2024 &old_desc_blk, &new_desc_blk, 0);
2025 if ((grp == 0) || super_blk)
2028 overhead += old_desc_blocks;
2029 else if (new_desc_blk)
2036 * calcluate the minimum number of blocks the given fs can be resized to
2038 blk64_t calculate_minimum_resize_size(ext2_filsys fs)
2040 ext2_ino_t inode_count;
2041 blk64_t blks_needed, groups, data_blocks;
2042 blk64_t grp, data_needed, last_start;
2043 blk64_t overhead = 0;
2044 int old_desc_blocks;
2045 int extra_groups = 0;
2046 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2049 * first figure out how many group descriptors we need to
2050 * handle the number of inodes we have
2052 inode_count = fs->super->s_inodes_count -
2053 fs->super->s_free_inodes_count;
2054 blks_needed = ext2fs_div_ceil(inode_count,
2055 fs->super->s_inodes_per_group) *
2056 EXT2_BLOCKS_PER_GROUP(fs->super);
2057 groups = ext2fs_div64_ceil(blks_needed,
2058 EXT2_BLOCKS_PER_GROUP(fs->super));
2061 * number of old-style block group descriptor blocks
2063 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2064 old_desc_blocks = fs->super->s_first_meta_bg;
2066 old_desc_blocks = fs->desc_blocks +
2067 fs->super->s_reserved_gdt_blocks;
2069 /* calculate how many blocks are needed for data */
2070 data_needed = ext2fs_blocks_count(fs->super) -
2071 ext2fs_free_blocks_count(fs->super);
2073 for (grp = 0; grp < fs->group_desc_count; grp++)
2074 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
2077 * For ext4 we need to allow for up to a flex_bg worth of
2078 * inode tables of slack space so the resize operation can be
2079 * guaranteed to finish.
2081 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2082 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
2083 data_needed += fs->inode_blocks_per_group * extra_groups;
2084 extra_groups = groups % flexbg_size;
2088 * figure out how many data blocks we have given the number of groups
2089 * we need for our inodes
2091 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
2093 for (grp = 0; grp < groups; grp++) {
2094 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
2097 * we want to keep track of how much data we can store in
2098 * the groups leading up to the last group so we can determine
2099 * how big the last group needs to be
2101 if (grp != (groups - 1))
2102 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
2105 data_blocks -= overhead;
2109 * if we need more group descriptors in order to accomodate our data
2110 * then we need to add them here
2112 while (data_needed > data_blocks) {
2113 blk64_t remainder = data_needed - data_blocks;
2116 /* figure out how many more groups we need for the data */
2117 extra_grps = ext2fs_div64_ceil(remainder,
2118 EXT2_BLOCKS_PER_GROUP(fs->super));
2120 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
2122 /* ok we have to account for the last group */
2123 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2124 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
2126 for (grp = groups; grp < groups+extra_grps; grp++) {
2127 overhead = calc_group_overhead(fs, grp,
2131 * again, we need to see how much data we cram into
2132 * all of the groups leading up to the last group
2134 if (grp != (groups + extra_grps - 1))
2135 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
2138 data_blocks -= overhead;
2141 groups += extra_grps;
2142 extra_groups += extra_grps;
2143 if (fs->super->s_feature_incompat
2144 & EXT4_FEATURE_INCOMPAT_FLEX_BG
2145 && extra_groups > flexbg_size) {
2147 * For ext4 we need to allow for up to a flex_bg worth
2148 * of inode tables of slack space so the resize
2149 * operation can be guaranteed to finish.
2151 extra_groups = flexbg_size -
2152 (groups & (flexbg_size - 1));
2153 data_needed += (fs->inode_blocks_per_group *
2155 extra_groups = groups % flexbg_size;
2159 /* now for the fun voodoo */
2160 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2163 * if this is the case then the last group is going to have data in it
2164 * so we need to adjust the size of the last group accordingly
2166 if (last_start < data_needed) {
2167 blk64_t remainder = data_needed - last_start;
2170 * 50 is a magic number that mkfs/resize uses to see if its
2171 * even worth making/resizing the fs. basically you need to
2172 * have at least 50 blocks in addition to the blocks needed
2173 * for the metadata in the last group
2176 overhead += remainder;
2182 overhead += fs->super->s_first_data_block;
2185 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2186 * only do groups-1, and then add the number of blocks needed to
2187 * handle the group descriptor metadata+data that we need
2189 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2190 blks_needed += overhead;
2193 * If at this point we've already added up more "needed" than
2194 * the current size, just return current size as minimum.
2196 if (blks_needed >= ext2fs_blocks_count(fs->super))
2197 return ext2fs_blocks_count(fs->super);
2199 * We need to reserve a few extra blocks if extents are
2200 * enabled, in case we need to grow the extent tree. The more
2201 * we shrink the file system, the more space we need.
2203 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
2204 blks_needed += (ext2fs_blocks_count(fs->super) -