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);
56 static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs);
57 static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
58 ext2fs_block_bitmap meta_bmap);
59 static errcode_t resize_group_descriptors(ext2_resize_t rfs, blk64_t new_size);
60 static errcode_t move_bg_metadata(ext2_resize_t rfs);
61 static errcode_t zero_high_bits_in_inodes(ext2_resize_t rfs);
64 * Some helper CPP macros
66 #define IS_BLOCK_BM(fs, i, blk) ((blk) == ext2fs_block_bitmap_loc((fs),(i)))
67 #define IS_INODE_BM(fs, i, blk) ((blk) == ext2fs_inode_bitmap_loc((fs),(i)))
69 #define IS_INODE_TB(fs, i, blk) (((blk) >= ext2fs_inode_table_loc((fs), (i))) && \
70 ((blk) < (ext2fs_inode_table_loc((fs), (i)) + \
71 (fs)->inode_blocks_per_group)))
73 /* Some bigalloc helper macros which are more succint... */
74 #define B2C(x) EXT2FS_B2C(fs, (x))
75 #define C2B(x) EXT2FS_C2B(fs, (x))
76 #define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
77 #define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
78 #define LT_CLSTR(x, y) (B2C(x) < B2C(y))
79 #define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
80 #define GT_CLSTR(x, y) (B2C(x) > B2C(y))
82 static int lazy_itable_init;
85 * This is the top-level routine which does the dirty deed....
87 errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
88 errcode_t (*progress)(ext2_resize_t rfs, int pass,
90 unsigned long max_val))
94 struct resource_track rtrack, overall_track;
97 * Create the data structure
99 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
103 memset(rfs, 0, sizeof(struct ext2_resize_struct));
108 rfs->progress = progress;
110 init_resource_track(&overall_track, "overall resize2fs", fs->io);
111 init_resource_track(&rtrack, "read_bitmaps", fs->io);
112 retval = ext2fs_read_bitmaps(fs);
115 print_resource_track(rfs, &rtrack, fs->io);
117 fs->super->s_state |= EXT2_ERROR_FS;
118 ext2fs_mark_super_dirty(fs);
121 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io);
122 fix_uninit_block_bitmaps(fs);
123 print_resource_track(rfs, &rtrack, fs->io);
124 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
128 init_resource_track(&rtrack, "resize_group_descriptors", fs->io);
129 retval = resize_group_descriptors(rfs, *new_size);
132 print_resource_track(rfs, &rtrack, fs->io);
134 init_resource_track(&rtrack, "move_bg_metadata", fs->io);
135 retval = move_bg_metadata(rfs);
138 print_resource_track(rfs, &rtrack, fs->io);
140 init_resource_track(&rtrack, "zero_high_bits_in_metadata", fs->io);
141 retval = zero_high_bits_in_inodes(rfs);
144 print_resource_track(rfs, &rtrack, fs->io);
146 init_resource_track(&rtrack, "adjust_superblock", fs->io);
147 retval = adjust_superblock(rfs, *new_size);
150 print_resource_track(rfs, &rtrack, fs->io);
152 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io);
153 fix_uninit_block_bitmaps(rfs->new_fs);
154 print_resource_track(rfs, &rtrack, fs->io);
155 /* Clear the block bitmap uninit flag for the last block group */
156 ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
157 EXT2_BG_BLOCK_UNINIT);
159 *new_size = ext2fs_blocks_count(rfs->new_fs->super);
161 init_resource_track(&rtrack, "blocks_to_move", fs->io);
162 retval = blocks_to_move(rfs);
165 print_resource_track(rfs, &rtrack, fs->io);
167 #ifdef RESIZE2FS_DEBUG
168 if (rfs->flags & RESIZE_DEBUG_BMOVE)
169 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
170 ext2fs_free_blocks_count(rfs->old_fs->super),
171 ext2fs_free_blocks_count(rfs->new_fs->super),
175 init_resource_track(&rtrack, "block_mover", fs->io);
176 retval = block_mover(rfs);
179 print_resource_track(rfs, &rtrack, fs->io);
181 init_resource_track(&rtrack, "inode_scan_and_fix", fs->io);
182 retval = inode_scan_and_fix(rfs);
185 print_resource_track(rfs, &rtrack, fs->io);
187 init_resource_track(&rtrack, "inode_ref_fix", fs->io);
188 retval = inode_ref_fix(rfs);
191 print_resource_track(rfs, &rtrack, fs->io);
193 init_resource_track(&rtrack, "move_itables", fs->io);
194 retval = move_itables(rfs);
197 print_resource_track(rfs, &rtrack, fs->io);
199 init_resource_track(&rtrack, "calculate_summary_stats", fs->io);
200 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
203 print_resource_track(rfs, &rtrack, fs->io);
205 init_resource_track(&rtrack, "fix_resize_inode", fs->io);
206 retval = fix_resize_inode(rfs->new_fs);
209 print_resource_track(rfs, &rtrack, fs->io);
211 init_resource_track(&rtrack, "fix_sb_journal_backup", fs->io);
212 retval = fix_sb_journal_backup(rfs->new_fs);
215 print_resource_track(rfs, &rtrack, fs->io);
217 retval = clear_sparse_super2_last_group(rfs);
221 retval = ext2fs_set_gdt_csum(rfs->new_fs);
225 rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
226 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
228 print_resource_track(rfs, &overall_track, fs->io);
229 retval = ext2fs_close_free(&rfs->new_fs);
235 ext2fs_free(rfs->old_fs);
238 ext2fs_free_mem(&rfs->itable_buf);
239 if (rfs->reserve_blocks)
240 ext2fs_free_block_bitmap(rfs->reserve_blocks);
241 if (rfs->move_blocks)
242 ext2fs_free_block_bitmap(rfs->move_blocks);
243 ext2fs_free_mem(&rfs);
249 ext2fs_free(rfs->new_fs);
253 ext2fs_free_mem(&rfs->itable_buf);
254 ext2fs_free_mem(&rfs);
258 /* Keep the size of the group descriptor region constant */
259 static void adjust_reserved_gdt_blocks(ext2_filsys old_fs, ext2_filsys fs)
261 if ((fs->super->s_feature_compat &
262 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
263 (old_fs->desc_blocks != fs->desc_blocks)) {
266 new = ((int) fs->super->s_reserved_gdt_blocks) +
267 (old_fs->desc_blocks - fs->desc_blocks);
270 if (new > (int) fs->blocksize/4)
271 new = fs->blocksize/4;
272 fs->super->s_reserved_gdt_blocks = new;
276 /* Toggle 64bit mode */
277 static errcode_t resize_group_descriptors(ext2_resize_t rfs, blk64_t new_size)
279 void *o, *n, *new_group_desc;
284 if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
287 if (new_size != ext2fs_blocks_count(rfs->new_fs->super) ||
288 ext2fs_blocks_count(rfs->new_fs->super) >= (1ULL << 32) ||
289 (rfs->flags & RESIZE_DISABLE_64BIT &&
290 rfs->flags & RESIZE_ENABLE_64BIT))
291 return EXT2_ET_INVALID_ARGUMENT;
293 if (rfs->flags & RESIZE_DISABLE_64BIT) {
294 rfs->new_fs->super->s_feature_incompat &=
295 ~EXT4_FEATURE_INCOMPAT_64BIT;
296 rfs->new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE;
297 } else if (rfs->flags & RESIZE_ENABLE_64BIT) {
298 rfs->new_fs->super->s_feature_incompat |=
299 EXT4_FEATURE_INCOMPAT_64BIT;
300 rfs->new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
303 if (EXT2_DESC_SIZE(rfs->old_fs->super) ==
304 EXT2_DESC_SIZE(rfs->new_fs->super))
307 o = rfs->new_fs->group_desc;
308 rfs->new_fs->desc_blocks = ext2fs_div_ceil(
309 rfs->old_fs->group_desc_count,
310 EXT2_DESC_PER_BLOCK(rfs->new_fs->super));
311 retval = ext2fs_get_arrayzero(rfs->new_fs->desc_blocks,
312 rfs->old_fs->blocksize, &new_group_desc);
318 if (EXT2_DESC_SIZE(rfs->old_fs->super) <=
319 EXT2_DESC_SIZE(rfs->new_fs->super))
320 copy_size = EXT2_DESC_SIZE(rfs->old_fs->super);
322 copy_size = EXT2_DESC_SIZE(rfs->new_fs->super);
323 for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
324 memcpy(n, o, copy_size);
325 n += EXT2_DESC_SIZE(rfs->new_fs->super);
326 o += EXT2_DESC_SIZE(rfs->old_fs->super);
329 ext2fs_free_mem(&rfs->new_fs->group_desc);
330 rfs->new_fs->group_desc = new_group_desc;
332 for (i = 0; i < rfs->old_fs->group_desc_count; i++)
333 ext2fs_group_desc_csum_set(rfs->new_fs, i);
335 adjust_reserved_gdt_blocks(rfs->old_fs, rfs->new_fs);
340 /* Move bitmaps/inode tables out of the way. */
341 static errcode_t move_bg_metadata(ext2_resize_t rfs)
344 blk64_t b, c, d, old_desc_blocks, new_desc_blocks, j;
345 ext2fs_block_bitmap old_map, new_map;
350 if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
353 retval = ext2fs_allocate_block_bitmap(rfs->old_fs, "oldfs", &old_map);
357 retval = ext2fs_allocate_block_bitmap(rfs->new_fs, "newfs", &new_map);
361 if (EXT2_HAS_INCOMPAT_FEATURE(rfs->old_fs->super,
362 EXT2_FEATURE_INCOMPAT_META_BG)) {
363 old_desc_blocks = rfs->old_fs->super->s_first_meta_bg;
364 new_desc_blocks = rfs->new_fs->super->s_first_meta_bg;
366 old_desc_blocks = rfs->old_fs->desc_blocks +
367 rfs->old_fs->super->s_reserved_gdt_blocks;
368 new_desc_blocks = rfs->new_fs->desc_blocks +
369 rfs->new_fs->super->s_reserved_gdt_blocks;
372 /* Construct bitmaps of super/descriptor blocks in old and new fs */
373 for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
374 retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, i, &b, &c, &d,
379 ext2fs_mark_block_bitmap2(old_map, b);
380 for (j = 0; c != 0 && j < old_desc_blocks; j++)
381 ext2fs_mark_block_bitmap2(old_map, c + j);
383 ext2fs_mark_block_bitmap2(old_map, d);
385 retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, i, &b, &c, &d,
390 ext2fs_mark_block_bitmap2(new_map, b);
391 for (j = 0; c != 0 && j < new_desc_blocks; j++)
392 ext2fs_mark_block_bitmap2(new_map, c + j);
394 ext2fs_mark_block_bitmap2(new_map, d);
397 cluster_ratio = EXT2FS_CLUSTER_RATIO(rfs->new_fs);
399 /* Find changes in block allocations for bg metadata */
400 for (b = EXT2FS_B2C(rfs->old_fs,
401 rfs->old_fs->super->s_first_data_block);
402 b < ext2fs_blocks_count(rfs->new_fs->super);
403 b += cluster_ratio) {
404 old = ext2fs_test_block_bitmap2(old_map, b);
405 new = ext2fs_test_block_bitmap2(new_map, b);
408 /* mark old_map, unmark new_map */
409 if (cluster_ratio == 1)
410 ext2fs_unmark_block_bitmap2(
411 rfs->new_fs->block_map, b);
412 } else if (!old && new)
413 ; /* unmark old_map, mark new_map */
415 ext2fs_unmark_block_bitmap2(old_map, b);
416 ext2fs_unmark_block_bitmap2(new_map, b);
421 * new_map now shows blocks that have been newly allocated.
422 * old_map now shows blocks that have been newly freed.
426 * Move any conflicting bitmaps and inode tables. Ensure that we
427 * don't try to free clusters associated with bitmaps or tables.
429 for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
430 b = ext2fs_block_bitmap_loc(rfs->new_fs, i);
431 if (ext2fs_test_block_bitmap2(new_map, b))
432 ext2fs_block_bitmap_loc_set(rfs->new_fs, i, 0);
433 else if (ext2fs_test_block_bitmap2(old_map, b))
434 ext2fs_unmark_block_bitmap2(old_map, b);
436 b = ext2fs_inode_bitmap_loc(rfs->new_fs, i);
437 if (ext2fs_test_block_bitmap2(new_map, b))
438 ext2fs_inode_bitmap_loc_set(rfs->new_fs, i, 0);
439 else if (ext2fs_test_block_bitmap2(old_map, b))
440 ext2fs_unmark_block_bitmap2(old_map, b);
442 c = ext2fs_inode_table_loc(rfs->new_fs, i);
444 b < rfs->new_fs->inode_blocks_per_group;
446 if (ext2fs_test_block_bitmap2(new_map, b + c))
447 ext2fs_inode_table_loc_set(rfs->new_fs, i, 0);
448 else if (ext2fs_test_block_bitmap2(old_map, b + c))
449 ext2fs_unmark_block_bitmap2(old_map, b + c);
453 /* Free unused clusters */
455 cluster_ratio > 1 && b < ext2fs_blocks_count(rfs->new_fs->super);
457 if (ext2fs_test_block_bitmap2(old_map, b))
458 ext2fs_unmark_block_bitmap2(rfs->new_fs->block_map, b);
461 ext2fs_free_block_bitmap(old_map);
463 ext2fs_free_block_bitmap(new_map);
467 /* Zero out the high bits of extent fields */
468 static errcode_t zero_high_bits_in_extents(ext2_filsys fs, ext2_ino_t ino,
469 struct ext2_inode *inode)
471 ext2_extent_handle_t handle;
472 struct ext2fs_extent extent;
473 int op = EXT2_EXTENT_ROOT;
476 if (!(inode->i_flags & EXT4_EXTENTS_FL))
479 errcode = ext2fs_extent_open(fs, ino, &handle);
484 errcode = ext2fs_extent_get(handle, op, &extent);
488 op = EXT2_EXTENT_NEXT_SIB;
490 if (extent.e_pblk > (1ULL << 32)) {
491 extent.e_pblk &= (1ULL << 32) - 1;
492 errcode = ext2fs_extent_replace(handle, 0, &extent);
498 /* Ok if we run off the end */
499 if (errcode == EXT2_ET_EXTENT_NO_NEXT)
501 ext2fs_extent_free(handle);
505 /* Zero out the high bits of inodes. */
506 static errcode_t zero_high_bits_in_inodes(ext2_resize_t rfs)
508 ext2_filsys fs = rfs->old_fs;
509 int length = EXT2_INODE_SIZE(fs->super);
510 struct ext2_inode *inode = NULL;
511 ext2_inode_scan scan = NULL;
515 if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
518 if (fs->super->s_creator_os != EXT2_OS_LINUX)
521 retval = ext2fs_open_inode_scan(fs, 0, &scan);
525 retval = ext2fs_get_mem(length, &inode);
530 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
535 if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino))
539 * Here's how we deal with high block number fields:
541 * - i_size_high has been been written out with i_size_lo
542 * since the ext2 days, so no conversion is needed.
544 * - i_blocks_hi is guarded by both the huge_file feature and
545 * inode flags and has always been written out with
546 * i_blocks_lo if the feature is set. The field is only
547 * ever read if both feature and inode flag are set, so
548 * we don't need to zero it now.
550 * - i_file_acl_high can be uninitialized, so zero it if
553 if (inode->osd2.linux2.l_i_file_acl_high) {
554 inode->osd2.linux2.l_i_file_acl_high = 0;
555 retval = ext2fs_write_inode_full(fs, ino, inode,
561 retval = zero_high_bits_in_extents(fs, ino, inode);
568 ext2fs_free_mem(&inode);
570 ext2fs_close_inode_scan(scan);
575 * Clean up the bitmaps for unitialized bitmaps
577 static void fix_uninit_block_bitmaps(ext2_filsys fs)
583 if (!ext2fs_has_group_desc_csum(fs))
586 for (g=0; g < fs->group_desc_count; g++) {
587 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
590 blk = ext2fs_group_first_block2(fs, g);
591 lblk = ext2fs_group_last_block2(fs, g);
592 ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
595 ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
596 ext2fs_mark_block_bitmap2(fs->block_map,
597 ext2fs_block_bitmap_loc(fs, g));
598 ext2fs_mark_block_bitmap2(fs->block_map,
599 ext2fs_inode_bitmap_loc(fs, g));
600 for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
601 i < (unsigned int) fs->inode_blocks_per_group;
603 ext2fs_mark_block_bitmap2(fs->block_map, blk);
607 /* --------------------------------------------------------------------
609 * Resize processing, phase 1.
611 * In this phase we adjust the in-memory superblock information, and
612 * initialize any new parts of the inode table. The new parts of the
613 * inode table are created in virgin disk space, so we can abort here
614 * without any side effects.
615 * --------------------------------------------------------------------
619 * If the group descriptor's bitmap and inode table blocks are valid,
620 * release them in the new filesystem data structure, and mark them as
621 * reserved so the old inode table blocks don't get overwritten.
623 static errcode_t free_gdp_blocks(ext2_filsys fs,
624 ext2fs_block_bitmap reserve_blocks,
631 ext2fs_block_bitmap bg_map = NULL;
632 errcode_t retval = 0;
633 dgrp_t count = old_fs->group_desc_count - fs->group_desc_count;
635 /* If bigalloc, don't free metadata living in the same cluster */
636 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
637 retval = ext2fs_allocate_block_bitmap(fs, "bgdata", &bg_map);
641 retval = mark_table_blocks(fs, bg_map);
646 for (i = group; i < group + count; i++) {
647 blk = ext2fs_block_bitmap_loc(old_fs, i);
649 (blk < ext2fs_blocks_count(fs->super)) &&
650 !(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) {
651 ext2fs_block_alloc_stats2(fs, blk, -1);
652 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
655 blk = ext2fs_inode_bitmap_loc(old_fs, i);
657 (blk < ext2fs_blocks_count(fs->super)) &&
658 !(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) {
659 ext2fs_block_alloc_stats2(fs, blk, -1);
660 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
663 blk = ext2fs_inode_table_loc(old_fs, i);
665 j < fs->inode_blocks_per_group; j++, blk++) {
666 if (blk >= ext2fs_blocks_count(fs->super) ||
667 (bg_map && ext2fs_test_block_bitmap2(bg_map, blk)))
669 ext2fs_block_alloc_stats2(fs, blk, -1);
670 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
676 ext2fs_free_block_bitmap(bg_map);
681 * This routine is shared by the online and offline resize routines.
682 * All of the information which is adjusted in memory is done here.
684 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
685 ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
688 blk64_t overhead = 0;
690 blk64_t blk, group_block;
692 blk64_t old_numblocks, numblocks, adjblocks;
693 unsigned long i, j, old_desc_blocks;
694 unsigned int meta_bg, meta_bg_size;
695 int has_super, csum_flag;
696 unsigned long long new_inodes; /* u64 to check for overflow */
699 ext2fs_blocks_count_set(fs->super, new_size);
702 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
703 fs->super->s_first_data_block,
704 EXT2_BLOCKS_PER_GROUP(fs->super));
705 if (fs->group_desc_count == 0)
706 return EXT2_ET_TOOSMALL;
707 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
708 EXT2_DESC_PER_BLOCK(fs->super));
711 * Overhead is the number of bookkeeping blocks per group. It
712 * includes the superblock backup, the group descriptor
713 * backups, the inode bitmap, the block bitmap, and the inode
716 overhead = (int) (2 + fs->inode_blocks_per_group);
718 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
719 overhead += 1 + fs->desc_blocks +
720 fs->super->s_reserved_gdt_blocks;
723 * See if the last group is big enough to support the
724 * necessary data structures. If not, we need to get rid of
727 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
728 fs->super->s_blocks_per_group;
729 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
730 return EXT2_ET_TOOSMALL;
731 if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
732 ext2fs_blocks_count_set(fs->super,
733 ext2fs_blocks_count(fs->super) - rem);
737 * Adjust the number of inodes
739 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
740 if (new_inodes > ~0U) {
741 fprintf(stderr, _("inodes (%llu) must be less than %u"),
743 return EXT2_ET_TOO_MANY_INODES;
745 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
746 fs->group_desc_count;
749 * Adjust the number of free blocks
751 blk = ext2fs_blocks_count(old_fs->super);
752 if (blk > ext2fs_blocks_count(fs->super))
753 ext2fs_free_blocks_count_set(fs->super,
754 ext2fs_free_blocks_count(fs->super) -
755 (blk - ext2fs_blocks_count(fs->super)));
757 ext2fs_free_blocks_count_set(fs->super,
758 ext2fs_free_blocks_count(fs->super) +
759 (ext2fs_blocks_count(fs->super) - blk));
762 * Adjust the number of reserved blocks
764 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
765 ext2fs_blocks_count(old_fs->super);
766 ext2fs_r_blocks_count_set(fs->super,
767 (percent * ext2fs_blocks_count(fs->super) /
771 * Adjust the bitmaps for size
773 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
774 fs->super->s_inodes_count,
776 if (retval) goto errout;
778 real_end = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count) - 1 +
779 fs->super->s_first_data_block;
780 retval = ext2fs_resize_block_bitmap2(new_size - 1,
781 real_end, fs->block_map);
782 if (retval) goto errout;
785 * If we are growing the file system, also grow the size of
786 * the reserve_blocks bitmap
788 if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) {
789 retval = ext2fs_resize_block_bitmap2(new_size - 1,
790 real_end, reserve_blocks);
791 if (retval) goto errout;
795 * Reallocate the group descriptors as necessary.
797 if (EXT2_DESC_SIZE(old_fs->super) == EXT2_DESC_SIZE(fs->super) &&
798 old_fs->desc_blocks != fs->desc_blocks) {
799 retval = ext2fs_resize_mem(old_fs->desc_blocks *
801 fs->desc_blocks * fs->blocksize,
805 if (fs->desc_blocks > old_fs->desc_blocks)
806 memset((char *) fs->group_desc +
807 (old_fs->desc_blocks * fs->blocksize), 0,
808 (fs->desc_blocks - old_fs->desc_blocks) *
813 * If the resize_inode feature is set, and we are changing the
814 * number of descriptor blocks, then adjust
815 * s_reserved_gdt_blocks if possible to avoid needing to move
816 * the inode table either now or in the future.
818 * Note: If we're converting to 64bit mode, we did this earlier.
820 if (EXT2_DESC_SIZE(old_fs->super) == EXT2_DESC_SIZE(fs->super))
821 adjust_reserved_gdt_blocks(old_fs, fs);
823 if ((fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
824 (fs->super->s_first_meta_bg > fs->desc_blocks)) {
825 fs->super->s_feature_incompat &=
826 ~EXT2_FEATURE_INCOMPAT_META_BG;
827 fs->super->s_first_meta_bg = 0;
831 * Update the location of the backup superblocks if the
832 * sparse_super2 feature is enabled.
834 if (fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) {
835 dgrp_t last_bg = fs->group_desc_count - 1;
836 dgrp_t old_last_bg = old_fs->group_desc_count - 1;
838 if (last_bg > old_last_bg) {
839 if (old_fs->group_desc_count == 1)
840 fs->super->s_backup_bgs[0] = 1;
841 if (old_fs->group_desc_count == 1 &&
842 fs->super->s_backup_bgs[0])
843 fs->super->s_backup_bgs[0] = last_bg;
844 else if (fs->super->s_backup_bgs[1])
845 fs->super->s_backup_bgs[1] = last_bg;
846 } else if (last_bg < old_last_bg) {
847 if (fs->super->s_backup_bgs[0] > last_bg)
848 fs->super->s_backup_bgs[0] = 0;
849 if (fs->super->s_backup_bgs[1] > last_bg)
850 fs->super->s_backup_bgs[1] = 0;
852 old_fs->super->s_backup_bgs[1] == old_last_bg)
853 fs->super->s_backup_bgs[1] = last_bg;
858 * If we are shrinking the number of block groups, we're done
861 if (old_fs->group_desc_count > fs->group_desc_count) {
863 * Check the block groups that we are chopping off
864 * and free any blocks associated with their metadata
866 retval = free_gdp_blocks(fs, reserve_blocks, old_fs,
867 fs->group_desc_count);
872 * Fix the count of the last (old) block group
874 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
875 old_fs->super->s_first_data_block) %
876 old_fs->super->s_blocks_per_group;
878 old_numblocks = old_fs->super->s_blocks_per_group;
879 if (old_fs->group_desc_count == fs->group_desc_count) {
880 numblocks = (ext2fs_blocks_count(fs->super) -
881 fs->super->s_first_data_block) %
882 fs->super->s_blocks_per_group;
884 numblocks = fs->super->s_blocks_per_group;
886 numblocks = fs->super->s_blocks_per_group;
887 i = old_fs->group_desc_count - 1;
888 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
889 ext2fs_group_desc_csum_set(fs, i);
892 * If the number of block groups is staying the same, we're
893 * done and can exit now. (If the number block groups is
894 * shrinking, we had exited earlier.)
896 if (old_fs->group_desc_count >= fs->group_desc_count) {
902 * Initialize the new block group descriptors
904 group_block = ext2fs_group_first_block2(fs,
905 old_fs->group_desc_count);
906 csum_flag = ext2fs_has_group_desc_csum(fs);
907 if (access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0)
908 lazy_itable_init = 1;
909 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
910 old_desc_blocks = fs->super->s_first_meta_bg;
912 old_desc_blocks = fs->desc_blocks +
913 fs->super->s_reserved_gdt_blocks;
916 * If we changed the number of block_group descriptor blocks,
917 * we need to make sure they are all marked as reserved in the
918 * file systems's block allocation map.
920 for (i = 0; i < old_fs->group_desc_count; i++)
921 ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
923 for (i = old_fs->group_desc_count;
924 i < fs->group_desc_count; i++) {
925 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
926 sizeof(struct ext2_group_desc));
929 ext2fs_bg_flags_zap(fs, i);
931 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
932 if (!lazy_itable_init)
933 ext2fs_bg_flags_set(fs, i,
934 EXT2_BG_INODE_ZEROED);
935 ext2fs_bg_itable_unused_set(fs, i,
936 fs->super->s_inodes_per_group);
939 numblocks = ext2fs_group_blocks_count(fs, i);
940 if ((i < fs->group_desc_count - 1) && csum_flag)
941 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
943 has_super = ext2fs_bg_has_super(fs, i);
945 ext2fs_block_alloc_stats2(fs, group_block, +1);
948 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
949 meta_bg = i / meta_bg_size;
950 if (!(fs->super->s_feature_incompat &
951 EXT2_FEATURE_INCOMPAT_META_BG) ||
952 (meta_bg < fs->super->s_first_meta_bg)) {
954 for (j=0; j < old_desc_blocks; j++)
955 ext2fs_block_alloc_stats2(fs,
956 group_block + 1 + j, +1);
957 adjblocks += old_desc_blocks;
962 if (((i % meta_bg_size) == 0) ||
963 ((i % meta_bg_size) == 1) ||
964 ((i % meta_bg_size) == (meta_bg_size-1)))
965 ext2fs_block_alloc_stats2(fs,
966 group_block + has_super, +1);
969 adjblocks += 2 + fs->inode_blocks_per_group;
971 numblocks -= adjblocks;
972 ext2fs_free_blocks_count_set(fs->super,
973 ext2fs_free_blocks_count(fs->super) - adjblocks);
974 fs->super->s_free_inodes_count +=
975 fs->super->s_inodes_per_group;
976 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
977 ext2fs_bg_free_inodes_count_set(fs, i,
978 fs->super->s_inodes_per_group);
979 ext2fs_bg_used_dirs_count_set(fs, i, 0);
980 ext2fs_group_desc_csum_set(fs, i);
982 retval = ext2fs_allocate_group_table(fs, i, 0);
983 if (retval) goto errout;
985 group_block += fs->super->s_blocks_per_group;
990 * Mark all of the metadata blocks as reserved so they won't
991 * get allocated by the call to ext2fs_allocate_group_table()
992 * in blocks_to_move(), where we allocate new blocks to
993 * replace those allocation bitmap and inode table blocks
994 * which have to get relocated to make space for an increased
995 * number of the block group descriptors.
998 mark_table_blocks(fs, reserve_blocks);
1005 * This routine adjusts the superblock and other data structures, both
1006 * in disk as well as in memory...
1008 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
1010 ext2_filsys fs = rfs->new_fs;
1013 blk64_t group_block;
1015 unsigned long max_group;
1017 ext2fs_mark_super_dirty(fs);
1018 ext2fs_mark_bb_dirty(fs);
1019 ext2fs_mark_ib_dirty(fs);
1021 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
1022 &rfs->reserve_blocks);
1026 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
1031 * Check to make sure there are enough inodes
1033 if ((rfs->old_fs->super->s_inodes_count -
1034 rfs->old_fs->super->s_free_inodes_count) >
1035 rfs->new_fs->super->s_inodes_count) {
1041 * If we are shrinking the number block groups, we're done and
1044 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
1050 * If the number of block groups is staying the same, we're
1051 * done and can exit now. (If the number block groups is
1052 * shrinking, we had exited earlier.)
1054 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
1060 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
1061 * supports lazy inode initialization, we can skip
1062 * initializing the inode table.
1064 if (lazy_itable_init && ext2fs_has_group_desc_csum(fs)) {
1070 * Initialize the inode table
1072 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
1077 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
1078 group_block = ext2fs_group_first_block2(fs,
1079 rfs->old_fs->group_desc_count);
1080 adj = rfs->old_fs->group_desc_count;
1081 max_group = fs->group_desc_count - adj;
1082 if (rfs->progress) {
1083 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
1088 for (i = rfs->old_fs->group_desc_count;
1089 i < fs->group_desc_count; i++) {
1091 * Write out the new inode table
1093 retval = ext2fs_zero_blocks2(fs, ext2fs_inode_table_loc(fs, i),
1094 fs->inode_blocks_per_group, NULL,
1099 io_channel_flush(fs->io);
1100 if (rfs->progress) {
1101 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
1102 i - adj + 1, max_group);
1106 group_block += fs->super->s_blocks_per_group;
1108 io_channel_flush(fs->io);
1115 /* --------------------------------------------------------------------
1117 * Resize processing, phase 2.
1119 * In this phase we adjust determine which blocks need to be moved, in
1120 * blocks_to_move(). We then copy the blocks to their ultimate new
1121 * destinations using block_mover(). Since we are copying blocks to
1122 * their new locations, again during this pass we can abort without
1124 * --------------------------------------------------------------------
1128 * This helper function creates a block bitmap with all of the
1129 * filesystem meta-data blocks.
1131 static errcode_t mark_table_blocks(ext2_filsys fs,
1132 ext2fs_block_bitmap bmap)
1137 for (i = 0; i < fs->group_desc_count; i++) {
1138 ext2fs_reserve_super_and_bgd(fs, i, bmap);
1141 * Mark the blocks used for the inode table
1143 blk = ext2fs_inode_table_loc(fs, i);
1145 ext2fs_mark_block_bitmap_range2(bmap, blk,
1146 fs->inode_blocks_per_group);
1149 * Mark block used for the block bitmap
1151 blk = ext2fs_block_bitmap_loc(fs, i);
1153 ext2fs_mark_block_bitmap2(bmap, blk);
1156 * Mark block used for the inode bitmap
1158 blk = ext2fs_inode_bitmap_loc(fs, i);
1160 ext2fs_mark_block_bitmap2(bmap, blk);
1166 * This function checks to see if a particular block (either a
1167 * superblock or a block group descriptor) overlaps with an inode or
1168 * block bitmap block, or with the inode table.
1170 static void mark_fs_metablock(ext2_resize_t rfs,
1171 ext2fs_block_bitmap meta_bmap,
1172 int group, blk64_t blk)
1174 ext2_filsys fs = rfs->new_fs;
1176 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1177 ext2fs_block_alloc_stats2(fs, blk, +1);
1180 * Check to see if we overlap with the inode or block bitmap,
1181 * or the inode tables. If not, and the block is in use, then
1182 * mark it as a block to be moved.
1184 if (IS_BLOCK_BM(fs, group, blk)) {
1185 ext2fs_block_bitmap_loc_set(fs, group, 0);
1186 rfs->needed_blocks++;
1189 if (IS_INODE_BM(fs, group, blk)) {
1190 ext2fs_inode_bitmap_loc_set(fs, group, 0);
1191 rfs->needed_blocks++;
1194 if (IS_INODE_TB(fs, group, blk)) {
1195 ext2fs_inode_table_loc_set(fs, group, 0);
1196 rfs->needed_blocks++;
1199 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1202 for (i=0; i < rfs->old_fs->group_desc_count; i++) {
1203 if (IS_BLOCK_BM(fs, i, blk)) {
1204 ext2fs_block_bitmap_loc_set(fs, i, 0);
1205 rfs->needed_blocks++;
1208 if (IS_INODE_BM(fs, i, blk)) {
1209 ext2fs_inode_bitmap_loc_set(fs, i, 0);
1210 rfs->needed_blocks++;
1213 if (IS_INODE_TB(fs, i, blk)) {
1214 ext2fs_inode_table_loc_set(fs, i, 0);
1215 rfs->needed_blocks++;
1221 if (ext2fs_has_group_desc_csum(fs) &&
1222 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
1224 * If the block bitmap is uninitialized, which means
1225 * nothing other than standard metadata in use.
1228 } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
1229 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
1230 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
1231 rfs->needed_blocks++;
1237 * This routine marks and unmarks reserved blocks in the new block
1238 * bitmap. It also determines which blocks need to be moved and
1239 * places this information into the move_blocks bitmap.
1241 static errcode_t blocks_to_move(ext2_resize_t rfs)
1244 dgrp_t i, max_groups, g;
1245 blk64_t blk, group_blk;
1246 blk64_t old_blocks, new_blocks, group_end, cluster_freed;
1248 unsigned int meta_bg, meta_bg_size;
1250 ext2_filsys fs, old_fs;
1251 ext2fs_block_bitmap meta_bmap, new_meta_bmap = NULL;
1255 old_fs = rfs->old_fs;
1256 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
1259 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1264 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
1269 retval = mark_table_blocks(old_fs, meta_bmap);
1276 * If we're shrinking the filesystem, we need to move any
1277 * group's metadata blocks (either allocation bitmaps or the
1278 * inode table) which are beyond the end of the new
1281 new_size = ext2fs_blocks_count(fs->super);
1282 if (new_size < ext2fs_blocks_count(old_fs->super)) {
1283 for (g = 0; g < fs->group_desc_count; g++) {
1286 * ext2fs_allocate_group_table will re-allocate any
1287 * metadata blocks whose location is set to zero.
1289 if (ext2fs_block_bitmap_loc(fs, g) >= new_size) {
1290 ext2fs_block_bitmap_loc_set(fs, g, 0);
1293 if (ext2fs_inode_bitmap_loc(fs, g) >= new_size) {
1294 ext2fs_inode_bitmap_loc_set(fs, g, 0);
1297 if ((ext2fs_inode_table_loc(fs, g) +
1298 fs->inode_blocks_per_group) > new_size) {
1299 ext2fs_inode_table_loc_set(fs, g, 0);
1304 retval = ext2fs_allocate_group_table(fs, g, 0);
1312 * If we're shrinking the filesystem, we need to move all of
1313 * the blocks that don't fit any more
1315 for (blk = ext2fs_blocks_count(fs->super);
1316 blk < ext2fs_blocks_count(old_fs->super); blk++) {
1317 g = ext2fs_group_of_blk2(fs, blk);
1318 if (ext2fs_has_group_desc_csum(fs) &&
1319 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
1321 * The block bitmap is uninitialized, so skip
1322 * to the next block group.
1324 blk = ext2fs_group_first_block2(fs, g+1) - 1;
1327 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1328 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
1329 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
1330 rfs->needed_blocks++;
1332 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1335 if (old_fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1336 old_blocks = old_fs->super->s_first_meta_bg;
1338 old_blocks = old_fs->desc_blocks +
1339 old_fs->super->s_reserved_gdt_blocks;
1340 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1341 new_blocks = fs->super->s_first_meta_bg;
1343 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
1345 retval = reserve_sparse_super2_last_group(rfs, meta_bmap);
1349 if (EXT2_DESC_SIZE(rfs->old_fs->super) ==
1350 EXT2_DESC_SIZE(rfs->new_fs->super) &&
1351 old_blocks == new_blocks) {
1356 max_groups = fs->group_desc_count;
1357 if (max_groups > old_fs->group_desc_count)
1358 max_groups = old_fs->group_desc_count;
1359 group_blk = old_fs->super->s_first_data_block;
1361 * If we're reducing the number of descriptor blocks, this
1362 * makes life easy. :-) We just have to mark some extra
1365 if (old_blocks > new_blocks) {
1366 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
1367 retval = ext2fs_allocate_block_bitmap(fs,
1368 _("new meta blocks"),
1373 retval = mark_table_blocks(fs, new_meta_bmap);
1378 for (i = 0; i < max_groups; i++) {
1379 if (!ext2fs_bg_has_super(old_fs, i)) {
1380 group_blk += fs->super->s_blocks_per_group;
1383 group_end = group_blk + 1 + old_blocks;
1384 for (blk = group_blk + 1 + new_blocks;
1386 if (new_meta_bmap == NULL ||
1387 !ext2fs_test_block_bitmap2(new_meta_bmap,
1390 EXT2FS_CLUSTER_RATIO(fs) -
1392 EXT2FS_CLUSTER_MASK(fs));
1393 if (cluster_freed > group_end - blk)
1394 cluster_freed = group_end - blk;
1395 ext2fs_block_alloc_stats2(fs, blk, -1);
1396 blk += EXT2FS_CLUSTER_RATIO(fs);
1397 rfs->needed_blocks -= cluster_freed;
1400 rfs->needed_blocks--;
1403 group_blk += fs->super->s_blocks_per_group;
1409 * If we're increasing the number of descriptor blocks, life
1410 * gets interesting....
1412 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
1413 flex_bg = fs->super->s_feature_incompat &
1414 EXT4_FEATURE_INCOMPAT_FLEX_BG;
1415 /* first reserve all of the existing fs meta blocks */
1416 for (i = 0; i < max_groups; i++) {
1417 has_super = ext2fs_bg_has_super(fs, i);
1419 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
1421 meta_bg = i / meta_bg_size;
1422 if (!(fs->super->s_feature_incompat &
1423 EXT2_FEATURE_INCOMPAT_META_BG) ||
1424 (meta_bg < fs->super->s_first_meta_bg)) {
1426 for (blk = group_blk+1;
1427 blk < group_blk + 1 + new_blocks; blk++)
1428 mark_fs_metablock(rfs, meta_bmap,
1434 if (((i % meta_bg_size) == 0) ||
1435 ((i % meta_bg_size) == 1) ||
1436 ((i % meta_bg_size) == (meta_bg_size-1)))
1437 mark_fs_metablock(rfs, meta_bmap, i,
1438 group_blk + has_super);
1442 * Reserve the existing meta blocks that we know
1443 * aren't to be moved.
1445 * For flex_bg file systems, in order to avoid
1446 * overwriting fs metadata (especially inode table
1447 * blocks) belonging to a different block group when
1448 * we are relocating the inode tables, we need to
1449 * reserve all existing fs metadata blocks.
1451 if (ext2fs_block_bitmap_loc(fs, i))
1452 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1453 ext2fs_block_bitmap_loc(fs, i));
1454 else if (flex_bg && i < old_fs->group_desc_count)
1455 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1456 ext2fs_block_bitmap_loc(old_fs, i));
1458 if (ext2fs_inode_bitmap_loc(fs, i))
1459 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1460 ext2fs_inode_bitmap_loc(fs, i));
1461 else if (flex_bg && i < old_fs->group_desc_count)
1462 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1463 ext2fs_inode_bitmap_loc(old_fs, i));
1465 if (ext2fs_inode_table_loc(fs, i))
1466 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1467 ext2fs_inode_table_loc(fs, i),
1468 fs->inode_blocks_per_group);
1469 else if (flex_bg && i < old_fs->group_desc_count)
1470 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1471 ext2fs_inode_table_loc(old_fs, i),
1472 old_fs->inode_blocks_per_group);
1474 group_blk += rfs->new_fs->super->s_blocks_per_group;
1477 /* Allocate the missing data structures */
1478 for (i = 0; i < max_groups; i++) {
1479 if (ext2fs_inode_table_loc(fs, i) &&
1480 ext2fs_inode_bitmap_loc(fs, i) &&
1481 ext2fs_block_bitmap_loc(fs, i))
1484 retval = ext2fs_allocate_group_table(fs, i,
1485 rfs->reserve_blocks);
1490 * For those structures that have changed, we need to
1493 if (ext2fs_block_bitmap_loc(old_fs, i) !=
1494 (blk = ext2fs_block_bitmap_loc(fs, i))) {
1495 ext2fs_block_alloc_stats2(fs, blk, +1);
1496 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1497 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1498 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1501 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
1502 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
1503 ext2fs_block_alloc_stats2(fs, blk, +1);
1504 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1505 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1506 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1511 * The inode table, if we need to relocate it, is
1512 * handled specially. We have to reserve the blocks
1513 * for both the old and the new inode table, since we
1514 * can't have the inode table be destroyed during the
1515 * block relocation phase.
1517 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
1518 continue; /* inode table not moved */
1520 rfs->needed_blocks += fs->inode_blocks_per_group;
1523 * Mark the new inode table as in use in the new block
1524 * allocation bitmap, and move any blocks that might
1527 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
1528 j < fs->inode_blocks_per_group ; j++, blk++) {
1529 ext2fs_block_alloc_stats2(fs, blk, +1);
1530 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1531 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1532 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1537 * Make sure the old inode table is reserved in the
1538 * block reservation bitmap.
1540 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1541 j < fs->inode_blocks_per_group ; j++, blk++)
1542 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1548 ext2fs_free_block_bitmap(new_meta_bmap);
1550 ext2fs_free_block_bitmap(meta_bmap);
1556 * This helper function tries to allocate a new block. We try to
1557 * avoid hitting the original group descriptor blocks at least at
1558 * first, since we want to make it possible to recover from a badly
1559 * aborted resize operation as much as possible.
1561 * In the future, I may further modify this routine to balance out
1562 * where we get the new blocks across the various block groups.
1563 * Ideally we would allocate blocks that corresponded with the block
1564 * group of the containing inode, and keep contiguous blocks
1565 * together. However, this very difficult to do efficiently, since we
1566 * don't have the necessary information up front.
1570 #define DESPERATION 2
1572 static void init_block_alloc(ext2_resize_t rfs)
1574 rfs->alloc_state = AVOID_OLD;
1575 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1577 /* HACK for testing */
1578 if (ext2fs_blocks_count(rfs->new_fs->super) >
1579 ext2fs_blocks_count(rfs->old_fs->super))
1580 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1584 static blk64_t get_new_block(ext2_resize_t rfs)
1586 ext2_filsys fs = rfs->new_fs;
1589 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1590 if (rfs->alloc_state == DESPERATION)
1593 #ifdef RESIZE2FS_DEBUG
1594 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1595 printf("Going into desperation mode "
1596 "for block allocations\n");
1598 rfs->alloc_state = DESPERATION;
1599 rfs->new_blk = fs->super->s_first_data_block;
1602 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1603 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1605 ((rfs->alloc_state == AVOID_OLD) &&
1606 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1607 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1612 return rfs->new_blk;
1616 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1619 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1622 blk = get_new_block(rfs);
1626 #ifdef RESIZE2FS_DEBUG
1627 if (rfs->flags & 0xF)
1628 printf("get_alloc_block allocating %llu\n", blk);
1631 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1632 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1633 *ret = (blk64_t) blk;
1637 static errcode_t block_mover(ext2_resize_t rfs)
1639 blk64_t blk, old_blk, new_blk;
1640 ext2_filsys fs = rfs->new_fs;
1641 ext2_filsys old_fs = rfs->old_fs;
1646 ext2_badblocks_list badblock_list = 0;
1647 int bb_modified = 0;
1649 fs->get_alloc_block = resize2fs_get_alloc_block;
1650 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1652 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1656 new_blk = fs->super->s_first_data_block;
1657 if (!rfs->itable_buf) {
1658 retval = ext2fs_get_array(fs->blocksize,
1659 fs->inode_blocks_per_group,
1664 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1669 * The first step is to figure out where all of the blocks
1672 to_move = moved = 0;
1673 init_block_alloc(rfs);
1674 for (blk = B2C(old_fs->super->s_first_data_block);
1675 blk < ext2fs_blocks_count(old_fs->super);
1676 blk += EXT2FS_CLUSTER_RATIO(fs)) {
1677 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1679 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1681 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1682 ext2fs_badblocks_list_del(badblock_list, blk);
1687 new_blk = get_new_block(rfs);
1692 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1693 ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk));
1699 ext2fs_free_extent_table(rfs->bmap);
1707 * Step two is to actually move the blocks
1709 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1710 if (retval) goto errout;
1712 if (rfs->progress) {
1713 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1719 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1720 if (retval) goto errout;
1723 old_blk = C2B(old_blk);
1724 new_blk = C2B(new_blk);
1726 #ifdef RESIZE2FS_DEBUG
1727 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1728 printf("Moving %llu blocks %llu->%llu\n",
1729 size, old_blk, new_blk);
1733 if (c > fs->inode_blocks_per_group)
1734 c = fs->inode_blocks_per_group;
1735 retval = io_channel_read_blk64(fs->io, old_blk, c,
1737 if (retval) goto errout;
1738 retval = io_channel_write_blk64(fs->io, new_blk, c,
1740 if (retval) goto errout;
1745 if (rfs->progress) {
1746 io_channel_flush(fs->io);
1747 retval = (rfs->progress)(rfs,
1748 E2_RSZ_BLOCK_RELOC_PASS,
1754 io_channel_flush(fs->io);
1758 if (badblock_list) {
1759 if (!retval && bb_modified)
1760 retval = ext2fs_update_bb_inode(old_fs,
1762 ext2fs_badblocks_list_free(badblock_list);
1768 /* --------------------------------------------------------------------
1770 * Resize processing, phase 3
1772 * --------------------------------------------------------------------
1777 * The extent translation table is stored in clusters so we need to
1778 * take special care when mapping a source block number to its
1779 * destination block number.
1781 static __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc)
1783 __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc)));
1786 new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1);
1790 struct process_block_struct {
1794 struct ext2_inode * inode;
1801 static int process_block(ext2_filsys fs, blk64_t *block_nr,
1802 e2_blkcnt_t blockcnt,
1803 blk64_t ref_block EXT2FS_ATTR((unused)),
1804 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1806 struct process_block_struct *pb;
1808 blk64_t block, new_block;
1811 pb = (struct process_block_struct *) priv_data;
1813 if (pb->rfs->bmap) {
1814 new_block = extent_translate(fs, pb->rfs->bmap, block);
1816 *block_nr = new_block;
1817 ret |= BLOCK_CHANGED;
1819 #ifdef RESIZE2FS_DEBUG
1820 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1821 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1822 pb->old_ino, blockcnt, block,
1830 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1831 block, (int) blockcnt);
1843 static errcode_t progress_callback(ext2_filsys fs,
1844 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1845 dgrp_t group, void * priv_data)
1847 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1851 * This check is to protect against old ext2 libraries. It
1852 * shouldn't be needed against new libraries.
1857 if (rfs->progress) {
1858 io_channel_flush(fs->io);
1859 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1860 group+1, fs->group_desc_count);
1868 static errcode_t migrate_ea_block(ext2_resize_t rfs, ext2_ino_t ino,
1869 struct ext2_inode *inode, int *changed)
1875 /* No EA block or no remapping? Quit early. */
1876 if (ext2fs_file_acl_block(rfs->old_fs, inode) == 0 && !rfs->bmap)
1878 new_block = extent_translate(rfs->old_fs, rfs->bmap,
1879 ext2fs_file_acl_block(rfs->old_fs, inode));
1883 /* Set the new ACL block */
1884 ext2fs_file_acl_block_set(rfs->old_fs, inode, new_block);
1886 /* Update checksum */
1887 if (EXT2_HAS_RO_COMPAT_FEATURE(rfs->new_fs->super,
1888 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1889 err = ext2fs_get_mem(rfs->old_fs->blocksize, &buf);
1892 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1893 err = ext2fs_read_ext_attr3(rfs->old_fs, new_block, buf, ino);
1894 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1897 err = ext2fs_write_ext_attr3(rfs->old_fs, new_block, buf, ino);
1904 ext2fs_free_mem(&buf);
1908 /* Rewrite extents */
1909 static errcode_t rewrite_extents(ext2_filsys fs, ext2_ino_t ino)
1911 ext2_extent_handle_t handle;
1912 struct ext2fs_extent extent;
1914 struct ext2_extent_info info;
1916 errcode = ext2fs_extent_open(fs, ino, &handle);
1920 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
1925 errcode = ext2fs_extent_get_info(handle, &info);
1930 * If this is the first extent in an extent block that we
1931 * haven't visited, rewrite the extent to force the ETB
1932 * checksum to be rewritten.
1934 if (info.curr_entry == 1 && info.curr_level != 0 &&
1935 !(extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)) {
1936 errcode = ext2fs_extent_replace(handle, 0, &extent);
1941 /* Skip to the end of a block of leaf nodes */
1942 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
1943 errcode = ext2fs_extent_get(handle,
1944 EXT2_EXTENT_LAST_SIB,
1950 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT, &extent);
1951 } while (errcode == 0);
1954 /* Ok if we run off the end */
1955 if (errcode == EXT2_ET_EXTENT_NO_NEXT)
1957 ext2fs_extent_free(handle);
1961 static void quiet_com_err_proc(const char *whoami, errcode_t code,
1962 const char *fmt, va_list args)
1966 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1968 struct process_block_struct pb;
1969 ext2_ino_t ino, new_inode;
1970 struct ext2_inode *inode = NULL;
1971 ext2_inode_scan scan = NULL;
1973 char *block_buf = 0;
1974 ext2_ino_t start_to_move;
1977 if ((rfs->old_fs->group_desc_count <=
1978 rfs->new_fs->group_desc_count) &&
1982 set_com_err_hook(quiet_com_err_proc);
1984 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1985 if (retval) goto errout;
1987 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1988 if (retval) goto errout;
1989 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1990 if (retval) goto errout;
1992 start_to_move = (rfs->new_fs->group_desc_count *
1993 rfs->new_fs->super->s_inodes_per_group);
1995 if (rfs->progress) {
1996 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1997 0, rfs->old_fs->group_desc_count);
2001 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
2005 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
2006 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
2007 inode = malloc(inode_size);
2013 * First, copy all of the inodes that need to be moved
2014 * elsewhere in the inode table
2017 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
2018 if (retval) goto errout;
2022 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
2023 continue; /* inode not in use */
2025 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2028 /* Remap EA block */
2029 retval = migrate_ea_block(rfs, ino, inode, &pb.changed);
2034 if (ino <= start_to_move)
2035 goto remap_blocks; /* Don't need to move inode. */
2038 * Find a new inode. Now that extents and directory blocks
2039 * are tied to the inode number through the checksum, we must
2040 * set up the new inode before we start rewriting blocks.
2042 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
2046 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
2048 inode->i_ctime = time(0);
2049 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
2055 #ifdef RESIZE2FS_DEBUG
2056 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
2057 printf("Inode moved %u->%u\n", ino, new_inode);
2060 retval = ext2fs_create_extent_table(&rfs->imap, 0);
2064 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
2068 retval = ext2fs_write_inode_full(rfs->old_fs,
2074 /* Rewrite extent block checksums with new inode number */
2075 if (EXT2_HAS_RO_COMPAT_FEATURE(rfs->old_fs->super,
2076 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
2077 (inode->i_flags & EXT4_EXTENTS_FL)) {
2078 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2079 retval = rewrite_extents(rfs->old_fs, new_inode);
2080 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
2086 * Update inodes to point to new blocks; schedule directory
2087 * blocks for inode remapping. Need to write out dir blocks
2088 * with new inode numbers if we have metadata_csum enabled.
2090 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
2091 (rfs->bmap || pb.is_dir)) {
2094 pb.has_extents = inode->i_flags & EXT4_EXTENTS_FL;
2095 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2096 retval = ext2fs_block_iterate3(rfs->old_fs,
2097 new_inode, 0, block_buf,
2098 process_block, &pb);
2099 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
2106 } else if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
2107 (rfs->bmap || pb.is_dir)) {
2108 /* inline data dir; update it too */
2109 retval = ext2fs_add_dir_block2(rfs->old_fs->dblist,
2115 io_channel_flush(rfs->old_fs->io);
2118 reset_com_err_hook();
2120 ext2fs_free_extent_table(rfs->bmap);
2124 ext2fs_close_inode_scan(scan);
2126 ext2fs_free_mem(&block_buf);
2131 /* --------------------------------------------------------------------
2133 * Resize processing, phase 4.
2135 * --------------------------------------------------------------------
2141 unsigned int max_dirs;
2145 static int check_and_change_inodes(ext2_ino_t dir,
2146 int entry EXT2FS_ATTR((unused)),
2147 struct ext2_dir_entry *dirent, int offset,
2148 int blocksize EXT2FS_ATTR((unused)),
2149 char *buf EXT2FS_ATTR((unused)),
2152 struct istruct *is = (struct istruct *) priv_data;
2153 struct ext2_inode inode;
2154 ext2_ino_t new_inode;
2158 if (is->rfs->progress && offset == 0) {
2159 io_channel_flush(is->rfs->old_fs->io);
2160 is->err = (is->rfs->progress)(is->rfs,
2161 E2_RSZ_INODE_REF_UPD_PASS,
2162 ++is->num, is->max_dirs);
2164 return DIRENT_ABORT;
2168 * If we have checksums enabled and the inode wasn't present in the
2169 * old fs, then we must rewrite all dir blocks with new checksums.
2171 if (EXT2_HAS_RO_COMPAT_FEATURE(is->rfs->old_fs->super,
2172 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
2173 !ext2fs_test_inode_bitmap2(is->rfs->old_fs->inode_map, dir))
2174 ret |= DIRENT_CHANGED;
2179 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
2183 #ifdef RESIZE2FS_DEBUG
2184 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
2185 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
2186 dir, ext2fs_dirent_name_len(dirent), dirent->name,
2187 dirent->inode, new_inode);
2190 dirent->inode = new_inode;
2192 /* Update the directory mtime and ctime */
2193 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
2195 inode.i_mtime = inode.i_ctime = time(0);
2196 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
2198 return ret | DIRENT_ABORT;
2201 return ret | DIRENT_CHANGED;
2204 static errcode_t inode_ref_fix(ext2_resize_t rfs)
2213 * Now, we iterate over all of the directories to update the
2217 is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
2221 if (rfs->progress) {
2222 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
2228 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2229 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
2230 DIRENT_FLAG_INCLUDE_EMPTY, 0,
2231 check_and_change_inodes, &is);
2232 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
2240 if (rfs->progress && (is.num < is.max_dirs))
2241 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
2242 is.max_dirs, is.max_dirs);
2245 ext2fs_free_extent_table(rfs->imap);
2251 /* --------------------------------------------------------------------
2253 * Resize processing, phase 5.
2255 * In this phase we actually move the inode table around, and then
2256 * update the summary statistics. This is scary, since aborting here
2257 * will potentially scramble the filesystem. (We are moving the
2258 * inode tables around in place, and so the potential for lost data,
2259 * or at the very least scrambling the mapping between filenames and
2260 * inode numbers is very high in case of a power failure here.)
2261 * --------------------------------------------------------------------
2266 * A very scary routine --- this one moves the inode table around!!!
2268 * After this you have to use the rfs->new_fs file handle to read and
2271 static errcode_t move_itables(ext2_resize_t rfs)
2275 dgrp_t i, max_groups;
2276 ext2_filsys fs = rfs->new_fs;
2278 blk64_t old_blk, new_blk, blk, cluster_freed;
2280 int j, to_move, moved;
2281 ext2fs_block_bitmap new_bmap = NULL;
2283 max_groups = fs->group_desc_count;
2284 if (max_groups > rfs->old_fs->group_desc_count)
2285 max_groups = rfs->old_fs->group_desc_count;
2287 size = fs->blocksize * fs->inode_blocks_per_group;
2288 if (!rfs->itable_buf) {
2289 retval = ext2fs_get_mem(size, &rfs->itable_buf);
2294 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
2295 retval = ext2fs_allocate_block_bitmap(fs, _("new meta blocks"),
2300 retval = mark_table_blocks(fs, new_bmap);
2306 * Figure out how many inode tables we need to move
2308 to_move = moved = 0;
2309 for (i=0; i < max_groups; i++)
2310 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
2311 ext2fs_inode_table_loc(fs, i))
2319 if (rfs->progress) {
2320 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
2326 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
2328 for (i=0; i < max_groups; i++) {
2329 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
2330 new_blk = ext2fs_inode_table_loc(fs, i);
2331 diff = new_blk - old_blk;
2333 #ifdef RESIZE2FS_DEBUG
2334 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
2335 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
2336 i, old_blk, new_blk, diff);
2344 retval = io_channel_read_blk64(fs->io, old_blk,
2345 fs->inode_blocks_per_group,
2350 * The end of the inode table segment often contains
2351 * all zeros, and we're often only moving the inode
2352 * table down a block or two. If so, we can optimize
2353 * things by not rewriting blocks that we know to be zero
2356 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
2359 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
2360 #ifdef RESIZE2FS_DEBUG
2361 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
2362 printf("%d blocks of zeros...\n", n);
2364 num = fs->inode_blocks_per_group;
2368 retval = io_channel_write_blk64(fs->io, new_blk,
2369 num, rfs->itable_buf);
2371 io_channel_write_blk64(fs->io, old_blk,
2372 num, rfs->itable_buf);
2376 retval = io_channel_write_blk64(fs->io,
2377 old_blk + fs->inode_blocks_per_group,
2378 diff, (rfs->itable_buf +
2379 (fs->inode_blocks_per_group - diff) *
2385 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
2386 j < fs->inode_blocks_per_group;) {
2387 if (new_bmap == NULL ||
2388 !ext2fs_test_block_bitmap2(new_bmap, blk)) {
2389 ext2fs_block_alloc_stats2(fs, blk, -1);
2390 cluster_freed = EXT2FS_CLUSTER_RATIO(fs) -
2391 (blk & EXT2FS_CLUSTER_MASK(fs));
2392 blk += cluster_freed;
2400 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
2401 ext2fs_group_desc_csum_set(rfs->old_fs, i);
2402 ext2fs_mark_super_dirty(rfs->old_fs);
2403 ext2fs_flush(rfs->old_fs);
2405 if (rfs->progress) {
2406 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
2412 mark_table_blocks(fs, fs->block_map);
2414 #ifdef RESIZE2FS_DEBUG
2415 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
2416 printf("Inode table move finished.\n");
2422 ext2fs_free_block_bitmap(new_bmap);
2427 * This function is used when expanding a file system. It frees the
2428 * superblock and block group descriptor blocks from the block group
2429 * which is no longer the last block group.
2431 static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs)
2433 ext2_filsys fs = rfs->new_fs;
2434 ext2_filsys old_fs = rfs->old_fs;
2436 dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1;
2437 dgrp_t last_bg = fs->group_desc_count - 1;
2438 blk64_t sb, old_desc;
2441 if (!(fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2))
2444 if (last_bg <= old_last_bg)
2447 if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
2448 fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
2451 if (old_fs->super->s_backup_bgs[0] != old_last_bg &&
2452 old_fs->super->s_backup_bgs[1] != old_last_bg)
2455 if (fs->super->s_backup_bgs[0] == old_last_bg ||
2456 fs->super->s_backup_bgs[1] == old_last_bg)
2459 retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, old_last_bg,
2460 &sb, &old_desc, NULL, &num);
2465 ext2fs_unmark_block_bitmap2(fs->block_map, sb);
2467 ext2fs_unmark_block_bitmap_range2(fs->block_map, old_desc, num);
2472 * This function is used when shrinking a file system. We need to
2473 * utilize blocks from what will be the new last block group for the
2474 * backup superblock and block group descriptor blocks.
2475 * Unfortunately, those blocks may be used by other files or fs
2476 * metadata blocks. We need to mark them as being in use.
2478 static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
2479 ext2fs_block_bitmap meta_bmap)
2481 ext2_filsys fs = rfs->new_fs;
2482 ext2_filsys old_fs = rfs->old_fs;
2484 dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1;
2485 dgrp_t last_bg = fs->group_desc_count - 1;
2487 blk64_t blk, sb, old_desc;
2491 if (!(fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2))
2494 if (last_bg >= old_last_bg)
2497 if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
2498 fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
2501 if (fs->super->s_backup_bgs[0] != last_bg &&
2502 fs->super->s_backup_bgs[1] != last_bg)
2505 if (old_fs->super->s_backup_bgs[0] == last_bg ||
2506 old_fs->super->s_backup_bgs[1] == last_bg)
2509 retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, last_bg,
2510 &sb, &old_desc, NULL, &num);
2514 if (last_bg && !sb) {
2515 fputs(_("Should never happen! No sb in last super_sparse bg?\n"),
2519 if (old_desc && old_desc != sb+1) {
2520 fputs(_("Should never happen! Unexpected old_desc in "
2521 "super_sparse bg?\n"),
2525 num = (old_desc) ? num : 1;
2527 /* Reserve the backup blocks */
2528 ext2fs_mark_block_bitmap_range2(fs->block_map, sb, num);
2530 for (g = 0; g < fs->group_desc_count; g++) {
2533 mb = ext2fs_block_bitmap_loc(fs, g);
2534 if ((mb >= sb) && (mb < sb + num)) {
2535 ext2fs_block_bitmap_loc_set(fs, g, 0);
2538 mb = ext2fs_inode_bitmap_loc(fs, g);
2539 if ((mb >= sb) && (mb < sb + num)) {
2540 ext2fs_inode_bitmap_loc_set(fs, g, 0);
2543 mb = ext2fs_inode_table_loc(fs, g);
2544 if ((mb < sb + num) &&
2545 (sb < mb + fs->inode_blocks_per_group)) {
2546 ext2fs_inode_table_loc_set(fs, g, 0);
2550 retval = ext2fs_allocate_group_table(fs, g, 0);
2556 for (blk = sb, i = 0; i < num; blk++, i++) {
2557 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
2558 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
2559 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
2560 rfs->needed_blocks++;
2562 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
2568 * Fix the resize inode
2570 static errcode_t fix_resize_inode(ext2_filsys fs)
2572 struct ext2_inode inode;
2575 if (!(fs->super->s_feature_compat &
2576 EXT2_FEATURE_COMPAT_RESIZE_INODE))
2579 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
2580 if (retval) goto errout;
2582 ext2fs_iblk_set(fs, &inode, 1);
2584 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
2585 if (retval) goto errout;
2587 if (!inode.i_block[EXT2_DIND_BLOCK]) {
2589 * Avoid zeroing out block #0; that's rude. This
2590 * should never happen anyway since the filesystem
2591 * should be fsck'ed and we assume it is consistent.
2593 fprintf(stderr, "%s",
2594 _("Should never happen: resize inode corrupt!\n"));
2598 retval = ext2fs_zero_blocks2(fs, inode.i_block[EXT2_DIND_BLOCK], 1,
2603 retval = ext2fs_create_resize_inode(fs);
2612 * Finally, recalculate the summary information
2614 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
2618 unsigned int group = 0;
2619 unsigned int count = 0;
2620 blk64_t total_blocks_free = 0;
2621 int total_inodes_free = 0;
2624 blk64_t super_blk, old_desc_blk, new_desc_blk;
2625 int old_desc_blocks;
2628 * First calculate the block statistics
2630 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2631 ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
2633 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2634 old_desc_blocks = fs->super->s_first_meta_bg;
2636 old_desc_blocks = fs->desc_blocks +
2637 fs->super->s_reserved_gdt_blocks;
2638 for (blk = B2C(fs->super->s_first_data_block);
2639 blk < ext2fs_blocks_count(fs->super);
2640 blk += EXT2FS_CLUSTER_RATIO(fs)) {
2642 !(EQ_CLSTR(blk, super_blk) ||
2643 ((old_desc_blk && old_desc_blocks &&
2644 GE_CLSTR(blk, old_desc_blk) &&
2645 LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) ||
2646 ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) ||
2647 EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) ||
2648 EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) ||
2649 ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) &&
2650 LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group)
2651 + fs->inode_blocks_per_group))))) ||
2652 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
2654 total_blocks_free++;
2657 if ((count == fs->super->s_clusters_per_group) ||
2658 EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) {
2659 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
2660 ext2fs_group_desc_csum_set(fs, group);
2662 if (group >= fs->group_desc_count)
2666 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2667 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
2670 if (fs->super->s_feature_incompat &
2671 EXT2_FEATURE_INCOMPAT_META_BG)
2672 old_desc_blocks = fs->super->s_first_meta_bg;
2674 old_desc_blocks = fs->desc_blocks +
2675 fs->super->s_reserved_gdt_blocks;
2678 total_blocks_free = C2B(total_blocks_free);
2679 ext2fs_free_blocks_count_set(fs->super, total_blocks_free);
2682 * Next, calculate the inode statistics
2688 /* Protect loop from wrap-around if s_inodes_count maxed */
2689 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2690 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2692 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2694 total_inodes_free++;
2697 if ((count == fs->super->s_inodes_per_group) ||
2698 (ino == fs->super->s_inodes_count)) {
2699 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
2700 ext2fs_group_desc_csum_set(fs, group);
2702 if (group >= fs->group_desc_count)
2706 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2709 fs->super->s_free_inodes_count = total_inodes_free;
2710 ext2fs_mark_super_dirty(fs);
2715 * Journal may have been relocated; update the backup journal blocks
2716 * in the superblock.
2718 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
2721 struct ext2_inode inode;
2723 if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
2726 /* External journal? Nothing to do. */
2727 if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
2730 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
2733 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
2734 fs->super->s_jnl_blocks[15] = inode.i_size_high;
2735 fs->super->s_jnl_blocks[16] = inode.i_size;
2736 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
2737 ext2fs_mark_super_dirty(fs);
2741 static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
2742 int old_desc_blocks)
2744 blk64_t super_blk, old_desc_blk, new_desc_blk;
2747 /* inode table blocks plus allocation bitmaps */
2748 overhead = fs->inode_blocks_per_group + 2;
2750 ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
2751 &old_desc_blk, &new_desc_blk, 0);
2752 if ((grp == 0) || super_blk)
2755 overhead += old_desc_blocks;
2756 else if (new_desc_blk)
2763 * calcluate the minimum number of blocks the given fs can be resized to
2765 blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
2767 ext2_ino_t inode_count;
2768 dgrp_t groups, flex_groups;
2769 blk64_t blks_needed, data_blocks;
2770 blk64_t grp, data_needed, last_start;
2771 blk64_t overhead = 0;
2772 int old_desc_blocks;
2773 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2776 * first figure out how many group descriptors we need to
2777 * handle the number of inodes we have
2779 inode_count = fs->super->s_inodes_count -
2780 fs->super->s_free_inodes_count;
2781 blks_needed = ext2fs_div_ceil(inode_count,
2782 fs->super->s_inodes_per_group) *
2783 (blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super);
2784 groups = ext2fs_div64_ceil(blks_needed,
2785 EXT2_BLOCKS_PER_GROUP(fs->super));
2786 #ifdef RESIZE2FS_DEBUG
2787 if (flags & RESIZE_DEBUG_MIN_CALC)
2788 printf("fs has %d inodes, %d groups required.\n",
2789 inode_count, groups);
2793 * number of old-style block group descriptor blocks
2795 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2796 old_desc_blocks = fs->super->s_first_meta_bg;
2798 old_desc_blocks = fs->desc_blocks +
2799 fs->super->s_reserved_gdt_blocks;
2801 /* calculate how many blocks are needed for data */
2802 data_needed = ext2fs_blocks_count(fs->super) -
2803 ext2fs_free_blocks_count(fs->super);
2805 for (grp = 0; grp < fs->group_desc_count; grp++)
2806 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
2807 #ifdef RESIZE2FS_DEBUG
2808 if (flags & RESIZE_DEBUG_MIN_CALC)
2809 printf("fs requires %llu data blocks.\n", data_needed);
2813 * For ext4 we need to allow for up to a flex_bg worth of
2814 * inode tables of slack space so the resize operation can be
2815 * guaranteed to finish.
2817 flex_groups = groups;
2818 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2819 dgrp_t remainder = groups & (flexbg_size - 1);
2821 flex_groups += flexbg_size - remainder;
2822 if (flex_groups > fs->group_desc_count)
2823 flex_groups = fs->group_desc_count;
2827 * figure out how many data blocks we have given the number of groups
2828 * we need for our inodes
2830 data_blocks = EXT2_GROUPS_TO_BLOCKS(fs->super, groups);
2832 for (grp = 0; grp < flex_groups; grp++) {
2833 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
2836 * we want to keep track of how much data we can store in
2837 * the groups leading up to the last group so we can determine
2838 * how big the last group needs to be
2840 if (grp < (groups - 1))
2841 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
2844 if (data_blocks > overhead)
2845 data_blocks -= overhead;
2849 #ifdef RESIZE2FS_DEBUG
2850 if (flags & RESIZE_DEBUG_MIN_CALC)
2851 printf("With %d group(s), we have %llu blocks available.\n",
2852 groups, data_blocks);
2856 * if we need more group descriptors in order to accomodate our data
2857 * then we need to add them here
2859 blks_needed = data_needed;
2860 while (blks_needed > data_blocks) {
2861 blk64_t remainder = blks_needed - data_blocks;
2864 /* figure out how many more groups we need for the data */
2865 extra_grps = ext2fs_div64_ceil(remainder,
2866 EXT2_BLOCKS_PER_GROUP(fs->super));
2868 data_blocks += EXT2_GROUPS_TO_BLOCKS(fs->super, extra_grps);
2870 /* ok we have to account for the last group */
2871 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2872 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
2875 groups += extra_grps;
2876 if (!(fs->super->s_feature_incompat &
2877 EXT4_FEATURE_INCOMPAT_FLEX_BG))
2878 flex_groups = groups;
2879 else if (groups > flex_groups) {
2880 dgrp_t r = groups & (flexbg_size - 1);
2882 flex_groups = groups + flexbg_size - r;
2883 if (flex_groups > fs->group_desc_count)
2884 flex_groups = fs->group_desc_count;
2887 for (; grp < flex_groups; grp++) {
2888 overhead = calc_group_overhead(fs, grp,
2892 * again, we need to see how much data we cram into
2893 * all of the groups leading up to the last group
2895 if (grp < groups - 1)
2896 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
2899 data_blocks -= overhead;
2902 #ifdef RESIZE2FS_DEBUG
2903 if (flags & RESIZE_DEBUG_MIN_CALC)
2904 printf("Added %d extra group(s), "
2905 "blks_needed %llu, data_blocks %llu, "
2906 "last_start %llu\n", extra_grps, blks_needed,
2907 data_blocks, last_start);
2911 /* now for the fun voodoo */
2913 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
2914 (grp & ~(flexbg_size - 1)) == 0)
2915 grp = grp & ~(flexbg_size - 1);
2917 for (; grp < flex_groups; grp++)
2918 overhead += calc_group_overhead(fs, grp, old_desc_blocks);
2920 #ifdef RESIZE2FS_DEBUG
2921 if (flags & RESIZE_DEBUG_MIN_CALC)
2922 printf("Last group's overhead is %llu\n", overhead);
2926 * if this is the case then the last group is going to have data in it
2927 * so we need to adjust the size of the last group accordingly
2929 if (last_start < blks_needed) {
2930 blk64_t remainder = blks_needed - last_start;
2932 #ifdef RESIZE2FS_DEBUG
2933 if (flags & RESIZE_DEBUG_MIN_CALC)
2934 printf("Need %llu data blocks in last group\n",
2938 * 50 is a magic number that mkfs/resize uses to see if its
2939 * even worth making/resizing the fs. basically you need to
2940 * have at least 50 blocks in addition to the blocks needed
2941 * for the metadata in the last group
2944 overhead += remainder;
2950 overhead += fs->super->s_first_data_block;
2951 #ifdef RESIZE2FS_DEBUG
2952 if (flags & RESIZE_DEBUG_MIN_CALC)
2953 printf("Final size of last group is %lld\n", overhead);
2956 /* Add extra slack for bigalloc file systems */
2957 if (EXT2FS_CLUSTER_RATIO(fs) > 1)
2958 overhead += EXT2FS_CLUSTER_RATIO(fs) * 2;
2961 * since our last group doesn't have to be BLOCKS_PER_GROUP
2962 * large, we only do groups-1, and then add the number of
2963 * blocks needed to handle the group descriptor metadata+data
2966 blks_needed = EXT2_GROUPS_TO_BLOCKS(fs->super, groups - 1);
2967 blks_needed += overhead;
2970 * Make sure blks_needed covers the end of the inode table in
2971 * the last block group.
2973 overhead = ext2fs_inode_table_loc(fs, groups-1) +
2974 fs->inode_blocks_per_group;
2975 if (blks_needed < overhead)
2976 blks_needed = overhead;
2978 #ifdef RESIZE2FS_DEBUG
2979 if (flags & RESIZE_DEBUG_MIN_CALC)
2980 printf("Estimated blocks needed: %llu\n", blks_needed);
2984 * If at this point we've already added up more "needed" than
2985 * the current size, just return current size as minimum.
2987 if (blks_needed >= ext2fs_blocks_count(fs->super))
2988 return ext2fs_blocks_count(fs->super);
2990 * We need to reserve a few extra blocks if extents are
2991 * enabled, in case we need to grow the extent tree. The more
2992 * we shrink the file system, the more space we need.
2994 * The absolute worst case is every single data block is in
2995 * the part of the file system that needs to be evacuated,
2996 * with each data block needs to be in its own extent, and
2997 * with each inode needing at least one extent block.
2999 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
3000 blk64_t safe_margin = (ext2fs_blocks_count(fs->super) -
3002 unsigned int exts_per_blk = (fs->blocksize /
3003 sizeof(struct ext3_extent)) - 1;
3004 blk64_t worst_case = ((data_needed + exts_per_blk - 1) /
3007 if (worst_case < inode_count)
3008 worst_case = inode_count;
3010 if (safe_margin > worst_case)
3011 safe_margin = worst_case;
3013 #ifdef RESIZE2FS_DEBUG
3014 if (flags & RESIZE_DEBUG_MIN_CALC)
3015 printf("Extents safety margin: %llu\n", safe_margin);
3017 blks_needed += safe_margin;