2 * resize2fs.c --- ext2 main routine
4 * Copyright (C) 1997, 1998 by Theodore Ts'o and
7 * Copyright (C) 1999, 2000 by Theodore 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 resize2fs_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 functions to check if a block is in a metadata area
66 static inline int is_block_bm(ext2_filsys fs, unsigned int grp, blk64_t blk)
68 return blk == ext2fs_block_bitmap_loc(fs, grp);
71 static inline int is_inode_bm(ext2_filsys fs, unsigned int grp, blk64_t blk)
73 return blk == ext2fs_inode_bitmap_loc(fs, grp);
76 static int is_inode_tb(ext2_filsys fs, unsigned int grp, blk64_t blk)
78 return blk >= ext2fs_inode_table_loc(fs, grp) &&
79 blk < (ext2fs_inode_table_loc(fs, grp) +
80 fs->inode_blocks_per_group);
83 /* Some bigalloc helper macros which are more succinct... */
84 #define B2C(x) EXT2FS_B2C(fs, (x))
85 #define C2B(x) EXT2FS_C2B(fs, (x))
86 #define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
87 #define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
88 #define LT_CLSTR(x, y) (B2C(x) < B2C(y))
89 #define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
90 #define GT_CLSTR(x, y) (B2C(x) > B2C(y))
92 static int lazy_itable_init;
95 * This is the top-level routine which does the dirty deed....
97 errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
98 errcode_t (*progress)(ext2_resize_t rfs, int pass,
100 unsigned long max_val))
104 struct resource_track rtrack, overall_track;
107 * Create the data structure
109 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
113 memset(rfs, 0, sizeof(struct ext2_resize_struct));
118 rfs->progress = progress;
120 init_resource_track(&overall_track, "overall resize2fs", fs->io);
121 init_resource_track(&rtrack, "read_bitmaps", fs->io);
122 retval = ext2fs_read_bitmaps(fs);
125 print_resource_track(rfs, &rtrack, fs->io);
127 fs->super->s_state |= EXT2_ERROR_FS;
128 ext2fs_mark_super_dirty(fs);
131 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io);
132 fix_uninit_block_bitmaps(fs);
133 print_resource_track(rfs, &rtrack, fs->io);
134 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
138 init_resource_track(&rtrack, "resize_group_descriptors", fs->io);
139 retval = resize_group_descriptors(rfs, *new_size);
142 print_resource_track(rfs, &rtrack, fs->io);
144 init_resource_track(&rtrack, "move_bg_metadata", fs->io);
145 retval = move_bg_metadata(rfs);
148 print_resource_track(rfs, &rtrack, fs->io);
150 init_resource_track(&rtrack, "zero_high_bits_in_metadata", fs->io);
151 retval = zero_high_bits_in_inodes(rfs);
154 print_resource_track(rfs, &rtrack, fs->io);
156 init_resource_track(&rtrack, "adjust_superblock", fs->io);
157 retval = adjust_superblock(rfs, *new_size);
160 print_resource_track(rfs, &rtrack, fs->io);
162 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io);
163 fix_uninit_block_bitmaps(rfs->new_fs);
164 print_resource_track(rfs, &rtrack, fs->io);
165 /* Clear the block bitmap uninit flag for the last block group */
166 ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
167 EXT2_BG_BLOCK_UNINIT);
169 *new_size = ext2fs_blocks_count(rfs->new_fs->super);
171 init_resource_track(&rtrack, "blocks_to_move", fs->io);
172 retval = blocks_to_move(rfs);
175 print_resource_track(rfs, &rtrack, fs->io);
177 #ifdef RESIZE2FS_DEBUG
178 if (rfs->flags & RESIZE_DEBUG_BMOVE)
179 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
180 (unsigned long long) ext2fs_free_blocks_count(rfs->old_fs->super),
181 (unsigned long long) ext2fs_free_blocks_count(rfs->new_fs->super),
182 (unsigned long long) rfs->needed_blocks);
185 init_resource_track(&rtrack, "block_mover", fs->io);
186 retval = block_mover(rfs);
189 print_resource_track(rfs, &rtrack, fs->io);
191 init_resource_track(&rtrack, "inode_scan_and_fix", fs->io);
192 retval = inode_scan_and_fix(rfs);
195 print_resource_track(rfs, &rtrack, fs->io);
197 init_resource_track(&rtrack, "inode_ref_fix", fs->io);
198 retval = inode_ref_fix(rfs);
201 print_resource_track(rfs, &rtrack, fs->io);
203 init_resource_track(&rtrack, "move_itables", fs->io);
204 retval = move_itables(rfs);
207 print_resource_track(rfs, &rtrack, fs->io);
209 retval = clear_sparse_super2_last_group(rfs);
213 init_resource_track(&rtrack, "calculate_summary_stats", fs->io);
214 retval = resize2fs_calculate_summary_stats(rfs->new_fs);
217 print_resource_track(rfs, &rtrack, fs->io);
219 init_resource_track(&rtrack, "fix_resize_inode", fs->io);
220 retval = fix_resize_inode(rfs->new_fs);
223 print_resource_track(rfs, &rtrack, fs->io);
225 init_resource_track(&rtrack, "fix_sb_journal_backup", fs->io);
226 retval = fix_sb_journal_backup(rfs->new_fs);
229 print_resource_track(rfs, &rtrack, fs->io);
231 retval = ext2fs_set_gdt_csum(rfs->new_fs);
235 rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
236 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
238 print_resource_track(rfs, &overall_track, fs->io);
239 retval = ext2fs_close_free(&rfs->new_fs);
245 ext2fs_free(rfs->old_fs);
248 ext2fs_free_mem(&rfs->itable_buf);
249 if (rfs->reserve_blocks)
250 ext2fs_free_block_bitmap(rfs->reserve_blocks);
251 if (rfs->move_blocks)
252 ext2fs_free_block_bitmap(rfs->move_blocks);
253 ext2fs_free_mem(&rfs);
259 ext2fs_free(rfs->new_fs);
263 ext2fs_free_mem(&rfs->itable_buf);
264 ext2fs_free_mem(&rfs);
268 /* Keep the size of the group descriptor region constant */
269 static void adjust_reserved_gdt_blocks(ext2_filsys old_fs, ext2_filsys fs)
271 if (ext2fs_has_feature_resize_inode(fs->super) &&
272 (old_fs->desc_blocks != fs->desc_blocks)) {
275 new = ((int) fs->super->s_reserved_gdt_blocks) +
276 (old_fs->desc_blocks - fs->desc_blocks);
279 if (new > (int) fs->blocksize/4)
280 new = fs->blocksize/4;
281 fs->super->s_reserved_gdt_blocks = new;
285 /* Toggle 64bit mode */
286 static errcode_t resize_group_descriptors(ext2_resize_t rfs, blk64_t new_size)
288 void *o, *n, *new_group_desc;
293 if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
296 if (new_size != ext2fs_blocks_count(rfs->new_fs->super) ||
297 ext2fs_blocks_count(rfs->new_fs->super) >= (1ULL << 32) ||
298 (rfs->flags & RESIZE_DISABLE_64BIT &&
299 rfs->flags & RESIZE_ENABLE_64BIT))
300 return EXT2_ET_INVALID_ARGUMENT;
302 if (rfs->flags & RESIZE_DISABLE_64BIT) {
303 ext2fs_clear_feature_64bit(rfs->new_fs->super);
304 rfs->new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE;
305 } else if (rfs->flags & RESIZE_ENABLE_64BIT) {
306 ext2fs_set_feature_64bit(rfs->new_fs->super);
307 rfs->new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
310 if (EXT2_DESC_SIZE(rfs->old_fs->super) ==
311 EXT2_DESC_SIZE(rfs->new_fs->super))
314 o = rfs->new_fs->group_desc;
315 rfs->new_fs->desc_blocks = ext2fs_div_ceil(
316 rfs->old_fs->group_desc_count,
317 EXT2_DESC_PER_BLOCK(rfs->new_fs->super));
318 retval = ext2fs_get_arrayzero(rfs->new_fs->desc_blocks,
319 rfs->old_fs->blocksize, &new_group_desc);
325 if (EXT2_DESC_SIZE(rfs->old_fs->super) <=
326 EXT2_DESC_SIZE(rfs->new_fs->super))
327 copy_size = EXT2_DESC_SIZE(rfs->old_fs->super);
329 copy_size = EXT2_DESC_SIZE(rfs->new_fs->super);
330 for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
331 memcpy(n, o, copy_size);
332 n = (char *)n + EXT2_DESC_SIZE(rfs->new_fs->super);
333 o = (char *)o + EXT2_DESC_SIZE(rfs->old_fs->super);
336 ext2fs_free_mem(&rfs->new_fs->group_desc);
337 rfs->new_fs->group_desc = new_group_desc;
339 for (i = 0; i < rfs->old_fs->group_desc_count; i++)
340 ext2fs_group_desc_csum_set(rfs->new_fs, i);
342 adjust_reserved_gdt_blocks(rfs->old_fs, rfs->new_fs);
347 /* Move bitmaps/inode tables out of the way. */
348 static errcode_t move_bg_metadata(ext2_resize_t rfs)
351 blk64_t b, c, d, old_desc_blocks, new_desc_blocks, j;
352 ext2fs_block_bitmap old_map, new_map;
357 if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
360 retval = ext2fs_allocate_block_bitmap(rfs->old_fs, "oldfs", &old_map);
364 retval = ext2fs_allocate_block_bitmap(rfs->new_fs, "newfs", &new_map);
368 if (ext2fs_has_feature_meta_bg(rfs->old_fs->super)) {
369 old_desc_blocks = rfs->old_fs->super->s_first_meta_bg;
370 new_desc_blocks = rfs->new_fs->super->s_first_meta_bg;
372 old_desc_blocks = rfs->old_fs->desc_blocks +
373 rfs->old_fs->super->s_reserved_gdt_blocks;
374 new_desc_blocks = rfs->new_fs->desc_blocks +
375 rfs->new_fs->super->s_reserved_gdt_blocks;
378 /* Construct bitmaps of super/descriptor blocks in old and new fs */
379 for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
380 retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, i, &b, &c, &d,
385 ext2fs_mark_block_bitmap2(old_map, b);
386 for (j = 0; c != 0 && j < old_desc_blocks; j++)
387 ext2fs_mark_block_bitmap2(old_map, c + j);
389 ext2fs_mark_block_bitmap2(old_map, d);
391 retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, i, &b, &c, &d,
396 ext2fs_mark_block_bitmap2(new_map, b);
397 for (j = 0; c != 0 && j < new_desc_blocks; j++)
398 ext2fs_mark_block_bitmap2(new_map, c + j);
400 ext2fs_mark_block_bitmap2(new_map, d);
403 cluster_ratio = EXT2FS_CLUSTER_RATIO(rfs->new_fs);
405 /* Find changes in block allocations for bg metadata */
406 for (b = EXT2FS_B2C(rfs->old_fs,
407 rfs->old_fs->super->s_first_data_block);
408 b < ext2fs_blocks_count(rfs->new_fs->super);
409 b += cluster_ratio) {
410 old = ext2fs_test_block_bitmap2(old_map, b);
411 new = ext2fs_test_block_bitmap2(new_map, b);
414 /* mark old_map, unmark new_map */
415 if (cluster_ratio == 1)
416 ext2fs_unmark_block_bitmap2(
417 rfs->new_fs->block_map, b);
418 } else if (!old && new)
419 ; /* unmark old_map, mark new_map */
421 ext2fs_unmark_block_bitmap2(old_map, b);
422 ext2fs_unmark_block_bitmap2(new_map, b);
427 * new_map now shows blocks that have been newly allocated.
428 * old_map now shows blocks that have been newly freed.
432 * Move any conflicting bitmaps and inode tables. Ensure that we
433 * don't try to free clusters associated with bitmaps or tables.
435 for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
436 b = ext2fs_block_bitmap_loc(rfs->new_fs, i);
437 if (ext2fs_test_block_bitmap2(new_map, b))
438 ext2fs_block_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 b = ext2fs_inode_bitmap_loc(rfs->new_fs, i);
443 if (ext2fs_test_block_bitmap2(new_map, b))
444 ext2fs_inode_bitmap_loc_set(rfs->new_fs, i, 0);
445 else if (ext2fs_test_block_bitmap2(old_map, b))
446 ext2fs_unmark_block_bitmap2(old_map, b);
448 c = ext2fs_inode_table_loc(rfs->new_fs, i);
450 b < rfs->new_fs->inode_blocks_per_group;
452 if (ext2fs_test_block_bitmap2(new_map, b + c))
453 ext2fs_inode_table_loc_set(rfs->new_fs, i, 0);
454 else if (ext2fs_test_block_bitmap2(old_map, b + c))
455 ext2fs_unmark_block_bitmap2(old_map, b + c);
459 /* Free unused clusters */
461 cluster_ratio > 1 && b < ext2fs_blocks_count(rfs->new_fs->super);
463 if (ext2fs_test_block_bitmap2(old_map, b))
464 ext2fs_unmark_block_bitmap2(rfs->new_fs->block_map, b);
467 ext2fs_free_block_bitmap(old_map);
469 ext2fs_free_block_bitmap(new_map);
473 /* Zero out the high bits of extent fields */
474 static errcode_t zero_high_bits_in_extents(ext2_filsys fs, ext2_ino_t ino,
475 struct ext2_inode *inode)
477 ext2_extent_handle_t handle;
478 struct ext2fs_extent extent;
479 int op = EXT2_EXTENT_ROOT;
482 if (!(inode->i_flags & EXT4_EXTENTS_FL))
485 errcode = ext2fs_extent_open(fs, ino, &handle);
490 errcode = ext2fs_extent_get(handle, op, &extent);
494 op = EXT2_EXTENT_NEXT_SIB;
496 if (extent.e_pblk > (1ULL << 32)) {
497 extent.e_pblk &= (1ULL << 32) - 1;
498 errcode = ext2fs_extent_replace(handle, 0, &extent);
504 /* Ok if we run off the end */
505 if (errcode == EXT2_ET_EXTENT_NO_NEXT)
507 ext2fs_extent_free(handle);
511 /* Zero out the high bits of inodes. */
512 static errcode_t zero_high_bits_in_inodes(ext2_resize_t rfs)
514 ext2_filsys fs = rfs->old_fs;
515 int length = EXT2_INODE_SIZE(fs->super);
516 struct ext2_inode *inode = NULL;
517 ext2_inode_scan scan = NULL;
521 if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
524 if (fs->super->s_creator_os == EXT2_OS_HURD)
527 retval = ext2fs_open_inode_scan(fs, 0, &scan);
531 retval = ext2fs_get_mem(length, &inode);
536 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
541 if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino))
545 * Here's how we deal with high block number fields:
547 * - i_size_high has been been written out with i_size_lo
548 * since the ext2 days, so no conversion is needed.
550 * - i_blocks_hi is guarded by both the huge_file feature and
551 * inode flags and has always been written out with
552 * i_blocks_lo if the feature is set. The field is only
553 * ever read if both feature and inode flag are set, so
554 * we don't need to zero it now.
556 * - i_file_acl_high can be uninitialized, so zero it if
559 if (inode->osd2.linux2.l_i_file_acl_high) {
560 inode->osd2.linux2.l_i_file_acl_high = 0;
561 retval = ext2fs_write_inode_full(fs, ino, inode,
567 retval = zero_high_bits_in_extents(fs, ino, inode);
574 ext2fs_free_mem(&inode);
576 ext2fs_close_inode_scan(scan);
581 * Clean up the bitmaps for uninitialized bitmaps
583 static void fix_uninit_block_bitmaps(ext2_filsys fs)
589 if (!ext2fs_has_group_desc_csum(fs))
592 for (g=0; g < fs->group_desc_count; g++) {
593 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
596 blk = ext2fs_group_first_block2(fs, g);
597 lblk = ext2fs_group_last_block2(fs, g);
598 ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
601 ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
602 ext2fs_mark_block_bitmap2(fs->block_map,
603 ext2fs_block_bitmap_loc(fs, g));
604 ext2fs_mark_block_bitmap2(fs->block_map,
605 ext2fs_inode_bitmap_loc(fs, g));
606 for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
607 i < fs->inode_blocks_per_group;
609 ext2fs_mark_block_bitmap2(fs->block_map, blk);
613 /* --------------------------------------------------------------------
615 * Resize processing, phase 1.
617 * In this phase we adjust the in-memory superblock information, and
618 * initialize any new parts of the inode table. The new parts of the
619 * inode table are created in virgin disk space, so we can abort here
620 * without any side effects.
621 * --------------------------------------------------------------------
625 * If the group descriptor's bitmap and inode table blocks are valid,
626 * release them in the new filesystem data structure, and mark them as
627 * reserved so the old inode table blocks don't get overwritten.
629 static errcode_t free_gdp_blocks(ext2_filsys fs,
630 ext2fs_block_bitmap reserve_blocks,
637 ext2fs_block_bitmap bg_map = NULL;
638 errcode_t retval = 0;
639 dgrp_t count = old_fs->group_desc_count - fs->group_desc_count;
641 /* If bigalloc, don't free metadata living in the same cluster */
642 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
643 retval = ext2fs_allocate_block_bitmap(fs, "bgdata", &bg_map);
647 retval = mark_table_blocks(fs, bg_map);
652 for (i = group; i < group + count; i++) {
653 blk = ext2fs_block_bitmap_loc(old_fs, i);
655 (blk < ext2fs_blocks_count(fs->super)) &&
656 !(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) {
657 ext2fs_block_alloc_stats2(fs, blk, -1);
658 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
661 blk = ext2fs_inode_bitmap_loc(old_fs, i);
663 (blk < ext2fs_blocks_count(fs->super)) &&
664 !(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) {
665 ext2fs_block_alloc_stats2(fs, blk, -1);
666 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
669 blk = ext2fs_inode_table_loc(old_fs, i);
671 j < fs->inode_blocks_per_group; j++, blk++) {
672 if (blk >= ext2fs_blocks_count(fs->super) ||
673 (bg_map && ext2fs_test_block_bitmap2(bg_map, blk)))
675 ext2fs_block_alloc_stats2(fs, blk, -1);
676 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
682 ext2fs_free_block_bitmap(bg_map);
687 * This routine is shared by the online and offline resize routines.
688 * All of the information which is adjusted in memory is done here.
690 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
691 ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
694 blk64_t overhead = 0;
696 blk64_t blk, group_block;
698 blk64_t old_numblocks, numblocks, adjblocks;
699 unsigned long i, j, old_desc_blocks;
700 unsigned int meta_bg, meta_bg_size;
701 int has_super, csum_flag, has_bg;
702 unsigned long long new_inodes; /* u64 to check for overflow */
705 ext2fs_blocks_count_set(fs->super, new_size);
706 fs->super->s_overhead_clusters = 0;
709 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
710 fs->super->s_first_data_block,
711 EXT2_BLOCKS_PER_GROUP(fs->super));
712 if (fs->group_desc_count == 0)
713 return EXT2_ET_TOOSMALL;
714 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
715 EXT2_DESC_PER_BLOCK(fs->super));
718 * Overhead is the number of bookkeeping blocks per group. It
719 * includes the superblock backup, the group descriptor
720 * backups, the inode bitmap, the block bitmap, and the inode
723 overhead = (int) (2 + fs->inode_blocks_per_group);
726 if (ext2fs_has_feature_sparse_super2(fs->super)) {
728 * We have to do this manually since
729 * super->s_backup_bgs hasn't been set up yet.
731 if (fs->group_desc_count == 2)
732 has_bg = fs->super->s_backup_bgs[0] != 0;
734 has_bg = fs->super->s_backup_bgs[1] != 0;
736 has_bg = ext2fs_bg_has_super(fs, fs->group_desc_count - 1);
738 overhead += 1 + fs->desc_blocks +
739 fs->super->s_reserved_gdt_blocks;
742 * See if the last group is big enough to support the
743 * necessary data structures. If not, we need to get rid of
746 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
747 fs->super->s_blocks_per_group;
748 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
749 return EXT2_ET_TOOSMALL;
750 if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
751 ext2fs_blocks_count_set(fs->super,
752 ext2fs_blocks_count(fs->super) - rem);
756 * Adjust the number of inodes
758 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
759 if (new_inodes > ~0U) {
760 fprintf(stderr, _("inodes (%llu) must be less than %u\n"),
761 (unsigned long long) new_inodes, ~0U);
762 return EXT2_ET_TOO_MANY_INODES;
764 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
765 fs->group_desc_count;
768 * Adjust the number of free blocks
770 blk = ext2fs_blocks_count(old_fs->super);
771 if (blk > ext2fs_blocks_count(fs->super))
772 ext2fs_free_blocks_count_set(fs->super,
773 ext2fs_free_blocks_count(fs->super) -
774 (blk - ext2fs_blocks_count(fs->super)));
776 ext2fs_free_blocks_count_set(fs->super,
777 ext2fs_free_blocks_count(fs->super) +
778 (ext2fs_blocks_count(fs->super) - blk));
781 * Adjust the number of reserved blocks
783 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
784 ext2fs_blocks_count(old_fs->super);
785 ext2fs_r_blocks_count_set(fs->super,
786 (percent * ext2fs_blocks_count(fs->super) /
790 * Adjust the bitmaps for size
792 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
793 fs->super->s_inodes_count,
795 if (retval) goto errout;
797 real_end = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count) - 1 +
798 fs->super->s_first_data_block;
799 retval = ext2fs_resize_block_bitmap2(new_size - 1,
800 real_end, fs->block_map);
801 if (retval) goto errout;
804 * If we are growing the file system, also grow the size of
805 * the reserve_blocks bitmap
807 if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) {
808 retval = ext2fs_resize_block_bitmap2(new_size - 1,
809 real_end, reserve_blocks);
810 if (retval) goto errout;
814 * Reallocate the group descriptors as necessary.
816 if (EXT2_DESC_SIZE(old_fs->super) == EXT2_DESC_SIZE(fs->super) &&
817 old_fs->desc_blocks != fs->desc_blocks) {
818 retval = ext2fs_resize_mem(old_fs->desc_blocks *
820 fs->desc_blocks * fs->blocksize,
824 if (fs->desc_blocks > old_fs->desc_blocks)
825 memset((char *) fs->group_desc +
826 (old_fs->desc_blocks * fs->blocksize), 0,
827 (fs->desc_blocks - old_fs->desc_blocks) *
832 * If the resize_inode feature is set, and we are changing the
833 * number of descriptor blocks, then adjust
834 * s_reserved_gdt_blocks if possible to avoid needing to move
835 * the inode table either now or in the future.
837 * Note: If we're converting to 64bit mode, we did this earlier.
839 if (EXT2_DESC_SIZE(old_fs->super) == EXT2_DESC_SIZE(fs->super))
840 adjust_reserved_gdt_blocks(old_fs, fs);
842 if (ext2fs_has_feature_meta_bg(fs->super) &&
843 (fs->super->s_first_meta_bg > fs->desc_blocks)) {
844 ext2fs_clear_feature_meta_bg(fs->super);
845 fs->super->s_first_meta_bg = 0;
849 * Update the location of the backup superblocks if the
850 * sparse_super2 feature is enabled.
852 if (ext2fs_has_feature_sparse_super2(fs->super)) {
853 dgrp_t last_bg = fs->group_desc_count - 1;
854 dgrp_t old_last_bg = old_fs->group_desc_count - 1;
856 if (last_bg > old_last_bg) {
857 if (old_fs->group_desc_count == 1)
858 fs->super->s_backup_bgs[0] = 1;
859 if ((old_fs->group_desc_count < 3 &&
860 fs->group_desc_count > 2) ||
861 fs->super->s_backup_bgs[1])
862 fs->super->s_backup_bgs[1] = last_bg;
863 } else if (last_bg < old_last_bg) {
864 if (fs->super->s_backup_bgs[0] > last_bg)
865 fs->super->s_backup_bgs[0] = 0;
866 if (fs->super->s_backup_bgs[1] > last_bg)
867 fs->super->s_backup_bgs[1] = 0;
869 old_fs->super->s_backup_bgs[1] == old_last_bg)
870 fs->super->s_backup_bgs[1] = last_bg;
875 * If we are shrinking the number of block groups, we're done
878 if (old_fs->group_desc_count > fs->group_desc_count) {
880 * Check the block groups that we are chopping off
881 * and free any blocks associated with their metadata
883 retval = free_gdp_blocks(fs, reserve_blocks, old_fs,
884 fs->group_desc_count);
889 * Fix the count of the last (old) block group
891 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
892 old_fs->super->s_first_data_block) %
893 old_fs->super->s_blocks_per_group;
895 old_numblocks = old_fs->super->s_blocks_per_group;
896 if (old_fs->group_desc_count == fs->group_desc_count) {
897 numblocks = (ext2fs_blocks_count(fs->super) -
898 fs->super->s_first_data_block) %
899 fs->super->s_blocks_per_group;
901 numblocks = fs->super->s_blocks_per_group;
903 numblocks = fs->super->s_blocks_per_group;
904 i = old_fs->group_desc_count - 1;
905 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
906 ext2fs_group_desc_csum_set(fs, i);
909 * If the number of block groups is staying the same, we're
910 * done and can exit now. (If the number block groups is
911 * shrinking, we had exited earlier.)
913 if (old_fs->group_desc_count >= fs->group_desc_count) {
919 * Initialize the new block group descriptors
921 group_block = ext2fs_group_first_block2(fs,
922 old_fs->group_desc_count);
923 csum_flag = ext2fs_has_group_desc_csum(fs);
924 if (getenv("RESIZE2FS_FORCE_LAZY_ITABLE_INIT") ||
925 (!getenv("RESIZE2FS_FORCE_ITABLE_INIT") &&
926 access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0))
927 lazy_itable_init = 1;
928 if (ext2fs_has_feature_meta_bg(fs->super))
929 old_desc_blocks = fs->super->s_first_meta_bg;
931 old_desc_blocks = fs->desc_blocks +
932 fs->super->s_reserved_gdt_blocks;
935 * If we changed the number of block_group descriptor blocks,
936 * we need to make sure they are all marked as reserved in the
937 * filesystem's block allocation map.
939 for (i = 0; i < old_fs->group_desc_count; i++)
940 ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
942 for (i = old_fs->group_desc_count;
943 i < fs->group_desc_count; i++) {
944 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
945 sizeof(struct ext2_group_desc));
948 ext2fs_bg_flags_zap(fs, i);
950 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
951 if (!lazy_itable_init)
952 ext2fs_bg_flags_set(fs, i,
953 EXT2_BG_INODE_ZEROED);
954 ext2fs_bg_itable_unused_set(fs, i,
955 fs->super->s_inodes_per_group);
958 numblocks = ext2fs_group_blocks_count(fs, i);
959 if ((i < fs->group_desc_count - 1) && csum_flag)
960 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
962 has_super = ext2fs_bg_has_super(fs, i);
964 ext2fs_block_alloc_stats2(fs, group_block, +1);
967 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
968 meta_bg = i / meta_bg_size;
969 if (!ext2fs_has_feature_meta_bg(fs->super) ||
970 (meta_bg < fs->super->s_first_meta_bg)) {
972 for (j=0; j < old_desc_blocks; j++)
973 ext2fs_block_alloc_stats2(fs,
974 group_block + 1 + j, +1);
975 adjblocks += old_desc_blocks;
980 if (((i % meta_bg_size) == 0) ||
981 ((i % meta_bg_size) == 1) ||
982 ((i % meta_bg_size) == (meta_bg_size-1)))
983 ext2fs_block_alloc_stats2(fs,
984 group_block + has_super, +1);
987 adjblocks += 2 + fs->inode_blocks_per_group;
989 numblocks -= adjblocks;
990 ext2fs_free_blocks_count_set(fs->super,
991 ext2fs_free_blocks_count(fs->super) - adjblocks);
992 fs->super->s_free_inodes_count +=
993 fs->super->s_inodes_per_group;
994 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
995 ext2fs_bg_free_inodes_count_set(fs, i,
996 fs->super->s_inodes_per_group);
997 ext2fs_bg_used_dirs_count_set(fs, i, 0);
998 ext2fs_group_desc_csum_set(fs, i);
1000 retval = ext2fs_allocate_group_table(fs, i, 0);
1001 if (retval) goto errout;
1003 group_block += fs->super->s_blocks_per_group;
1008 * Mark all of the metadata blocks as reserved so they won't
1009 * get allocated by the call to ext2fs_allocate_group_table()
1010 * in blocks_to_move(), where we allocate new blocks to
1011 * replace those allocation bitmap and inode table blocks
1012 * which have to get relocated to make space for an increased
1013 * number of the block group descriptors.
1016 mark_table_blocks(fs, reserve_blocks);
1023 * This routine adjusts the superblock and other data structures, both
1024 * in disk as well as in memory...
1026 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
1028 ext2_filsys fs = rfs->new_fs;
1031 blk64_t group_block;
1033 unsigned long max_group;
1035 ext2fs_mark_super_dirty(fs);
1036 ext2fs_mark_bb_dirty(fs);
1037 ext2fs_mark_ib_dirty(fs);
1039 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
1040 &rfs->reserve_blocks);
1044 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
1049 * Check to make sure there are enough inodes
1051 if ((rfs->old_fs->super->s_inodes_count -
1052 rfs->old_fs->super->s_free_inodes_count) >
1053 rfs->new_fs->super->s_inodes_count) {
1059 * If we are shrinking the number block groups, we're done and
1062 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
1068 * If the number of block groups is staying the same, we're
1069 * done and can exit now. (If the number block groups is
1070 * shrinking, we had exited earlier.)
1072 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
1078 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
1079 * supports lazy inode initialization, we can skip
1080 * initializing the inode table.
1082 if (lazy_itable_init && ext2fs_has_group_desc_csum(fs)) {
1088 * Initialize the inode table
1090 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
1095 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
1096 group_block = ext2fs_group_first_block2(fs,
1097 rfs->old_fs->group_desc_count);
1098 adj = rfs->old_fs->group_desc_count;
1099 max_group = fs->group_desc_count - adj;
1100 if (rfs->progress) {
1101 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
1106 for (i = rfs->old_fs->group_desc_count;
1107 i < fs->group_desc_count; i++) {
1109 * Write out the new inode table
1111 retval = ext2fs_zero_blocks2(fs, ext2fs_inode_table_loc(fs, i),
1112 fs->inode_blocks_per_group, NULL,
1117 io_channel_flush(fs->io);
1118 if (rfs->progress) {
1119 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
1120 i - adj + 1, max_group);
1124 group_block += fs->super->s_blocks_per_group;
1126 io_channel_flush(fs->io);
1133 /* --------------------------------------------------------------------
1135 * Resize processing, phase 2.
1137 * In this phase we adjust determine which blocks need to be moved, in
1138 * blocks_to_move(). We then copy the blocks to their ultimate new
1139 * destinations using block_mover(). Since we are copying blocks to
1140 * their new locations, again during this pass we can abort without
1142 * --------------------------------------------------------------------
1146 * This helper function creates a block bitmap with all of the
1147 * filesystem meta-data blocks.
1149 static errcode_t mark_table_blocks(ext2_filsys fs,
1150 ext2fs_block_bitmap bmap)
1155 for (i = 0; i < fs->group_desc_count; i++) {
1156 ext2fs_reserve_super_and_bgd(fs, i, bmap);
1159 * Mark the blocks used for the inode table
1161 blk = ext2fs_inode_table_loc(fs, i);
1163 ext2fs_mark_block_bitmap_range2(bmap, blk,
1164 fs->inode_blocks_per_group);
1167 * Mark block used for the block bitmap
1169 blk = ext2fs_block_bitmap_loc(fs, i);
1171 ext2fs_mark_block_bitmap2(bmap, blk);
1174 * Mark block used for the inode bitmap
1176 blk = ext2fs_inode_bitmap_loc(fs, i);
1178 ext2fs_mark_block_bitmap2(bmap, blk);
1184 * This function checks to see if a particular block (either a
1185 * superblock or a block group descriptor) overlaps with an inode or
1186 * block bitmap block, or with the inode table.
1188 static void mark_fs_metablock(ext2_resize_t rfs,
1189 ext2fs_block_bitmap meta_bmap,
1190 int group, blk64_t blk)
1192 ext2_filsys fs = rfs->new_fs;
1194 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1195 ext2fs_block_alloc_stats2(fs, blk, +1);
1198 * Check to see if we overlap with the inode or block bitmap,
1199 * or the inode tables. If not, and the block is in use, then
1200 * mark it as a block to be moved.
1202 if (is_block_bm(fs, group, blk)) {
1203 ext2fs_block_bitmap_loc_set(fs, group, 0);
1204 rfs->needed_blocks++;
1207 if (is_inode_bm(fs, group, blk)) {
1208 ext2fs_inode_bitmap_loc_set(fs, group, 0);
1209 rfs->needed_blocks++;
1212 if (is_inode_tb(fs, group, blk)) {
1213 ext2fs_inode_table_loc_set(fs, group, 0);
1214 rfs->needed_blocks++;
1217 if (ext2fs_has_feature_flex_bg(fs->super)) {
1220 for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
1221 if (is_block_bm(fs, i, blk)) {
1222 ext2fs_block_bitmap_loc_set(fs, i, 0);
1223 rfs->needed_blocks++;
1226 if (is_inode_bm(fs, i, blk)) {
1227 ext2fs_inode_bitmap_loc_set(fs, i, 0);
1228 rfs->needed_blocks++;
1231 if (is_inode_tb(fs, i, blk)) {
1232 ext2fs_inode_table_loc_set(fs, i, 0);
1233 rfs->needed_blocks++;
1239 if (ext2fs_has_group_desc_csum(fs) &&
1240 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
1242 * If the block bitmap is uninitialized, which means
1243 * nothing other than standard metadata in use.
1246 } else if (blk < ext2fs_blocks_count(rfs->old_fs->super) &&
1247 ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
1248 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
1249 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
1250 rfs->needed_blocks++;
1256 * This routine marks and unmarks reserved blocks in the new block
1257 * bitmap. It also determines which blocks need to be moved and
1258 * places this information into the move_blocks bitmap.
1260 static errcode_t blocks_to_move(ext2_resize_t rfs)
1264 dgrp_t i, max_groups, g;
1265 blk64_t blk, group_blk;
1266 blk64_t old_blocks, new_blocks, group_end, cluster_freed;
1268 unsigned int meta_bg, meta_bg_size;
1270 ext2_filsys fs, old_fs;
1271 ext2fs_block_bitmap meta_bmap, new_meta_bmap = NULL;
1275 old_fs = rfs->old_fs;
1276 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
1279 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1284 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
1289 retval = mark_table_blocks(old_fs, meta_bmap);
1296 * If we're shrinking the filesystem, we need to move any
1297 * group's metadata blocks (either allocation bitmaps or the
1298 * inode table) which are beyond the end of the new
1301 new_size = ext2fs_blocks_count(fs->super);
1302 if (new_size < ext2fs_blocks_count(old_fs->super)) {
1303 for (g = 0; g < fs->group_desc_count; g++) {
1306 * ext2fs_allocate_group_table will re-allocate any
1307 * metadata blocks whose location is set to zero.
1309 if (ext2fs_block_bitmap_loc(fs, g) >= new_size) {
1310 ext2fs_block_bitmap_loc_set(fs, g, 0);
1313 if (ext2fs_inode_bitmap_loc(fs, g) >= new_size) {
1314 ext2fs_inode_bitmap_loc_set(fs, g, 0);
1317 if ((ext2fs_inode_table_loc(fs, g) +
1318 fs->inode_blocks_per_group) > new_size) {
1319 ext2fs_inode_table_loc_set(fs, g, 0);
1324 retval = ext2fs_allocate_group_table(fs, g, 0);
1332 * If we're shrinking the filesystem, we need to move all of
1333 * the blocks that don't fit any more
1335 for (blk = ext2fs_blocks_count(fs->super);
1336 blk < ext2fs_blocks_count(old_fs->super); blk++) {
1337 g = ext2fs_group_of_blk2(fs, blk);
1338 if (ext2fs_has_group_desc_csum(fs) &&
1339 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
1341 * The block bitmap is uninitialized, so skip
1342 * to the next block group.
1344 blk = ext2fs_group_first_block2(fs, g+1) - 1;
1347 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1348 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
1349 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
1350 rfs->needed_blocks++;
1352 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1355 if (ext2fs_has_feature_meta_bg(old_fs->super))
1356 old_blocks = old_fs->super->s_first_meta_bg;
1358 old_blocks = old_fs->desc_blocks +
1359 old_fs->super->s_reserved_gdt_blocks;
1360 if (ext2fs_has_feature_meta_bg(fs->super))
1361 new_blocks = fs->super->s_first_meta_bg;
1363 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
1365 retval = reserve_sparse_super2_last_group(rfs, meta_bmap);
1369 if (EXT2_DESC_SIZE(rfs->old_fs->super) ==
1370 EXT2_DESC_SIZE(rfs->new_fs->super) &&
1371 old_blocks == new_blocks) {
1376 max_groups = fs->group_desc_count;
1377 if (max_groups > old_fs->group_desc_count)
1378 max_groups = old_fs->group_desc_count;
1379 group_blk = old_fs->super->s_first_data_block;
1381 * If we're reducing the number of descriptor blocks, this
1382 * makes life easy. :-) We just have to mark some extra
1385 if (old_blocks > new_blocks) {
1386 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
1387 retval = ext2fs_allocate_block_bitmap(fs,
1388 _("new meta blocks"),
1393 retval = mark_table_blocks(fs, new_meta_bmap);
1398 for (i = 0; i < max_groups; i++) {
1399 if (!ext2fs_bg_has_super(old_fs, i)) {
1400 group_blk += fs->super->s_blocks_per_group;
1403 group_end = group_blk + 1 + old_blocks;
1404 for (blk = group_blk + 1 + new_blocks;
1406 if (new_meta_bmap == NULL ||
1407 !ext2fs_test_block_bitmap2(new_meta_bmap,
1410 EXT2FS_CLUSTER_RATIO(fs) -
1412 EXT2FS_CLUSTER_MASK(fs));
1413 if (cluster_freed > group_end - blk)
1414 cluster_freed = group_end - blk;
1415 ext2fs_block_alloc_stats2(fs, blk, -1);
1416 blk += EXT2FS_CLUSTER_RATIO(fs);
1417 rfs->needed_blocks -= cluster_freed;
1420 rfs->needed_blocks--;
1423 group_blk += fs->super->s_blocks_per_group;
1429 * If we're increasing the number of descriptor blocks, life
1430 * gets interesting....
1432 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
1433 flex_bg = ext2fs_has_feature_flex_bg(fs->super);
1434 /* first reserve all of the existing fs meta blocks */
1435 for (i = 0; i < max_groups; i++) {
1436 has_super = ext2fs_bg_has_super(fs, i);
1438 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
1440 meta_bg = i / meta_bg_size;
1441 if (!ext2fs_has_feature_meta_bg(fs->super) ||
1442 (meta_bg < fs->super->s_first_meta_bg)) {
1444 for (blk = group_blk+1;
1445 blk < group_blk + 1 + new_blocks; blk++)
1446 mark_fs_metablock(rfs, meta_bmap,
1452 if (((i % meta_bg_size) == 0) ||
1453 ((i % meta_bg_size) == 1) ||
1454 ((i % meta_bg_size) == (meta_bg_size-1)))
1455 mark_fs_metablock(rfs, meta_bmap, i,
1456 group_blk + has_super);
1460 * Reserve the existing meta blocks that we know
1461 * aren't to be moved.
1463 * For flex_bg file systems, in order to avoid
1464 * overwriting fs metadata (especially inode table
1465 * blocks) belonging to a different block group when
1466 * we are relocating the inode tables, we need to
1467 * reserve all existing fs metadata blocks.
1469 if (ext2fs_block_bitmap_loc(fs, i))
1470 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1471 ext2fs_block_bitmap_loc(fs, i));
1472 else if (flex_bg && i < old_fs->group_desc_count)
1473 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1474 ext2fs_block_bitmap_loc(old_fs, i));
1476 if (ext2fs_inode_bitmap_loc(fs, i))
1477 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1478 ext2fs_inode_bitmap_loc(fs, i));
1479 else if (flex_bg && i < old_fs->group_desc_count)
1480 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1481 ext2fs_inode_bitmap_loc(old_fs, i));
1483 if (ext2fs_inode_table_loc(fs, i))
1484 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1485 ext2fs_inode_table_loc(fs, i),
1486 fs->inode_blocks_per_group);
1487 else if (flex_bg && i < old_fs->group_desc_count)
1488 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1489 ext2fs_inode_table_loc(old_fs, i),
1490 old_fs->inode_blocks_per_group);
1492 group_blk += rfs->new_fs->super->s_blocks_per_group;
1495 /* Allocate the missing data structures */
1496 for (i = 0; i < max_groups; i++) {
1497 if (ext2fs_inode_table_loc(fs, i) &&
1498 ext2fs_inode_bitmap_loc(fs, i) &&
1499 ext2fs_block_bitmap_loc(fs, i))
1502 retval = ext2fs_allocate_group_table(fs, i,
1503 rfs->reserve_blocks);
1508 * For those structures that have changed, we need to
1511 if (ext2fs_block_bitmap_loc(old_fs, i) !=
1512 (blk = ext2fs_block_bitmap_loc(fs, i))) {
1513 ext2fs_block_alloc_stats2(fs, blk, +1);
1514 if (blk < ext2fs_blocks_count(old_fs->super) &&
1515 ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1516 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1517 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1520 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
1521 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
1522 ext2fs_block_alloc_stats2(fs, blk, +1);
1523 if (blk < ext2fs_blocks_count(old_fs->super) &&
1524 ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1525 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1526 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1531 * The inode table, if we need to relocate it, is
1532 * handled specially. We have to reserve the blocks
1533 * for both the old and the new inode table, since we
1534 * can't have the inode table be destroyed during the
1535 * block relocation phase.
1537 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
1538 continue; /* inode table not moved */
1540 rfs->needed_blocks += fs->inode_blocks_per_group;
1543 * Mark the new inode table as in use in the new block
1544 * allocation bitmap, and move any blocks that might
1547 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
1548 j < fs->inode_blocks_per_group ; j++, blk++) {
1549 ext2fs_block_alloc_stats2(fs, blk, +1);
1550 if (blk < ext2fs_blocks_count(old_fs->super) &&
1551 ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1552 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1553 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1558 * Make sure the old inode table is reserved in the
1559 * block reservation bitmap.
1561 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1562 j < fs->inode_blocks_per_group ; j++, blk++)
1563 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1569 ext2fs_free_block_bitmap(new_meta_bmap);
1571 ext2fs_free_block_bitmap(meta_bmap);
1577 * This helper function tries to allocate a new block. We try to
1578 * avoid hitting the original group descriptor blocks at least at
1579 * first, since we want to make it possible to recover from a badly
1580 * aborted resize operation as much as possible.
1582 * In the future, I may further modify this routine to balance out
1583 * where we get the new blocks across the various block groups.
1584 * Ideally we would allocate blocks that corresponded with the block
1585 * group of the containing inode, and keep contiguous blocks
1586 * together. However, this very difficult to do efficiently, since we
1587 * don't have the necessary information up front.
1591 #define DESPERATION 2
1593 static void init_block_alloc(ext2_resize_t rfs)
1595 rfs->alloc_state = AVOID_OLD;
1596 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1598 /* HACK for testing */
1599 if (ext2fs_blocks_count(rfs->new_fs->super) >
1600 ext2fs_blocks_count(rfs->old_fs->super))
1601 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1605 static blk64_t get_new_block(ext2_resize_t rfs)
1607 ext2_filsys fs = rfs->new_fs;
1610 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1611 if (rfs->alloc_state == DESPERATION)
1614 #ifdef RESIZE2FS_DEBUG
1615 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1616 printf("Going into desperation mode "
1617 "for block allocations\n");
1619 rfs->alloc_state = DESPERATION;
1620 rfs->new_blk = fs->super->s_first_data_block;
1623 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1624 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1626 ((rfs->alloc_state == AVOID_OLD) &&
1627 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1628 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1633 return rfs->new_blk;
1637 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs,
1638 blk64_t goal EXT2FS_ATTR((unused)),
1641 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1645 blk = get_new_block(rfs);
1649 #ifdef RESIZE2FS_DEBUG
1650 if (rfs->flags & 0xF)
1651 printf("get_alloc_block allocating %llu\n",
1652 (unsigned long long) blk);
1655 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1656 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1658 group = ext2fs_group_of_blk2(rfs->old_fs, blk);
1659 ext2fs_clear_block_uninit(rfs->old_fs, group);
1660 group = ext2fs_group_of_blk2(rfs->new_fs, blk);
1661 ext2fs_clear_block_uninit(rfs->new_fs, group);
1663 *ret = (blk64_t) blk;
1667 static errcode_t block_mover(ext2_resize_t rfs)
1669 blk64_t blk, old_blk, new_blk;
1670 ext2_filsys fs = rfs->new_fs;
1671 ext2_filsys old_fs = rfs->old_fs;
1675 ext2_badblocks_list badblock_list = 0;
1676 int bb_modified = 0;
1678 fs->get_alloc_block = resize2fs_get_alloc_block;
1679 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1681 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1685 new_blk = fs->super->s_first_data_block;
1686 if (!rfs->itable_buf) {
1687 retval = ext2fs_get_array(fs->blocksize,
1688 fs->inode_blocks_per_group,
1693 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1698 * The first step is to figure out where all of the blocks
1701 to_move = moved = 0;
1702 init_block_alloc(rfs);
1703 for (blk = B2C(old_fs->super->s_first_data_block);
1704 blk < ext2fs_blocks_count(old_fs->super);
1705 blk += EXT2FS_CLUSTER_RATIO(fs)) {
1706 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1708 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1710 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1711 ext2fs_badblocks_list_del(badblock_list, blk);
1716 new_blk = get_new_block(rfs);
1721 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1722 ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk));
1728 ext2fs_free_extent_table(rfs->bmap);
1736 * Step two is to actually move the blocks
1738 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1739 if (retval) goto errout;
1741 if (rfs->progress) {
1742 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1748 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1749 if (retval) goto errout;
1752 old_blk = C2B(old_blk);
1753 new_blk = C2B(new_blk);
1755 #ifdef RESIZE2FS_DEBUG
1756 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1757 printf("Moving %llu blocks %llu->%llu\n",
1758 (unsigned long long) size,
1759 (unsigned long long) old_blk,
1760 (unsigned long long) new_blk);
1764 if (c > fs->inode_blocks_per_group)
1765 c = fs->inode_blocks_per_group;
1766 retval = io_channel_read_blk64(fs->io, old_blk, c,
1768 if (retval) goto errout;
1769 retval = io_channel_write_blk64(fs->io, new_blk, c,
1771 if (retval) goto errout;
1776 if (rfs->progress) {
1777 io_channel_flush(fs->io);
1778 retval = (rfs->progress)(rfs,
1779 E2_RSZ_BLOCK_RELOC_PASS,
1785 io_channel_flush(fs->io);
1789 if (badblock_list) {
1790 if (!retval && bb_modified)
1791 retval = ext2fs_update_bb_inode(old_fs,
1793 ext2fs_badblocks_list_free(badblock_list);
1799 /* --------------------------------------------------------------------
1801 * Resize processing, phase 3
1803 * --------------------------------------------------------------------
1808 * The extent translation table is stored in clusters so we need to
1809 * take special care when mapping a source block number to its
1810 * destination block number.
1812 static __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc)
1814 __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc)));
1817 new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1);
1821 struct process_block_struct {
1825 struct ext2_inode * inode;
1832 static int process_block(ext2_filsys fs, blk64_t *block_nr,
1833 e2_blkcnt_t blockcnt,
1834 blk64_t ref_block EXT2FS_ATTR((unused)),
1835 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1837 struct process_block_struct *pb;
1839 blk64_t block, new_block;
1842 pb = (struct process_block_struct *) priv_data;
1844 if (pb->rfs->bmap) {
1845 new_block = extent_translate(fs, pb->rfs->bmap, block);
1847 *block_nr = new_block;
1848 ret |= BLOCK_CHANGED;
1850 #ifdef RESIZE2FS_DEBUG
1851 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1852 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1853 pb->old_ino, (long long) blockcnt,
1854 (unsigned long long) block,
1855 (unsigned long long) new_block);
1862 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1863 block, (int) blockcnt);
1875 static errcode_t progress_callback(ext2_filsys fs,
1876 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1877 dgrp_t group, void * priv_data)
1879 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1883 * This check is to protect against old ext2 libraries. It
1884 * shouldn't be needed against new libraries.
1889 if (rfs->progress) {
1890 io_channel_flush(fs->io);
1891 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1892 group+1, fs->group_desc_count);
1900 static errcode_t migrate_ea_block(ext2_resize_t rfs, ext2_ino_t ino,
1901 struct ext2_inode *inode, int *changed)
1907 /* No EA block or no remapping? Quit early. */
1908 if (ext2fs_file_acl_block(rfs->old_fs, inode) == 0 || !rfs->bmap)
1910 new_block = extent_translate(rfs->old_fs, rfs->bmap,
1911 ext2fs_file_acl_block(rfs->old_fs, inode));
1915 /* Set the new ACL block */
1916 ext2fs_file_acl_block_set(rfs->old_fs, inode, new_block);
1918 /* Update checksum */
1919 if (ext2fs_has_feature_metadata_csum(rfs->new_fs->super)) {
1920 err = ext2fs_get_mem(rfs->old_fs->blocksize, &buf);
1923 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1924 err = ext2fs_read_ext_attr3(rfs->old_fs, new_block, buf, ino);
1925 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1928 err = ext2fs_write_ext_attr3(rfs->old_fs, new_block, buf, ino);
1935 ext2fs_free_mem(&buf);
1939 static void quiet_com_err_proc(const char *whoami EXT2FS_ATTR((unused)),
1940 errcode_t code EXT2FS_ATTR((unused)),
1941 const char *fmt EXT2FS_ATTR((unused)),
1942 va_list args EXT2FS_ATTR((unused)))
1946 static int fix_ea_entries(ext2_extent imap, struct ext2_ext_attr_entry *entry,
1947 struct ext2_ext_attr_entry *end, ext2_ino_t last_ino)
1952 while (entry < end && !EXT2_EXT_IS_LAST_ENTRY(entry)) {
1953 if (entry->e_value_inum > last_ino) {
1954 new_ino = ext2fs_extent_translate(imap,
1955 entry->e_value_inum);
1956 entry->e_value_inum = new_ino;
1959 entry = EXT2_EXT_ATTR_NEXT(entry);
1964 static int fix_ea_ibody_entries(ext2_extent imap,
1965 struct ext2_inode_large *inode, int inode_size,
1966 ext2_ino_t last_ino)
1968 struct ext2_ext_attr_entry *start, *end;
1971 if (inode->i_extra_isize == 0)
1974 ea_magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
1975 inode->i_extra_isize);
1976 if (*ea_magic != EXT2_EXT_ATTR_MAGIC)
1979 start = (struct ext2_ext_attr_entry *)(ea_magic + 1);
1980 end = (struct ext2_ext_attr_entry *)((char *)inode + inode_size);
1982 return fix_ea_entries(imap, start, end, last_ino);
1985 static int fix_ea_block_entries(ext2_extent imap, char *block_buf,
1986 unsigned int blocksize, ext2_ino_t last_ino)
1988 struct ext2_ext_attr_header *header;
1989 struct ext2_ext_attr_entry *start, *end;
1991 header = (struct ext2_ext_attr_header *)block_buf;
1992 start = (struct ext2_ext_attr_entry *)(header+1);
1993 end = (struct ext2_ext_attr_entry *)(block_buf + blocksize);
1995 return fix_ea_entries(imap, start, end, last_ino);
1998 /* A simple LRU cache to check recently processed blocks. */
2004 #define BLK_IN_CACHE(b,c) ((b) == (c).blks[0] || (b) == (c).blks[1] || \
2005 (b) == (c).blks[2] || (b) == (c).blks[3])
2006 #define BLK_ADD_CACHE(b,c) { \
2007 (c).blks[(c).cursor] = (b); \
2008 (c).cursor = ((c).cursor + 1) % 4; \
2011 static errcode_t fix_ea_inode_refs(ext2_resize_t rfs, struct ext2_inode *inode,
2012 char *block_buf, ext2_ino_t last_ino)
2014 ext2_filsys fs = rfs->new_fs;
2015 ext2_inode_scan scan = NULL;
2017 int inode_size = EXT2_INODE_SIZE(fs->super);
2020 struct blk_cache blk_cache;
2021 struct ext2_ext_attr_header *header;
2024 memset(&blk_cache, 0, sizeof(blk_cache));
2026 header = (struct ext2_ext_attr_header *)block_buf;
2028 retval = ext2fs_open_inode_scan(fs, 0, &scan);
2033 retval = ext2fs_get_next_inode_full(scan, &ino, inode,
2040 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
2041 continue; /* inode not in use */
2043 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE) {
2044 modified = fix_ea_ibody_entries(rfs->imap,
2045 (struct ext2_inode_large *)inode,
2046 inode_size, last_ino);
2048 retval = ext2fs_write_inode_full(fs, ino, inode,
2055 blk = ext2fs_file_acl_block(fs, inode);
2056 if (blk && !BLK_IN_CACHE(blk, blk_cache)) {
2057 retval = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
2061 modified = fix_ea_block_entries(rfs->imap, block_buf,
2065 retval = ext2fs_write_ext_attr3(fs, blk,
2070 * If refcount is greater than 1, we might see
2071 * the same block referenced by other inodes
2074 if (header->h_refcount > 1)
2075 BLK_ADD_CACHE(blk, blk_cache);
2082 ext2fs_close_inode_scan(scan);
2086 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
2088 struct process_block_struct pb;
2089 ext2_ino_t ino, new_inode;
2090 struct ext2_inode *inode = NULL;
2091 ext2_inode_scan scan = NULL;
2093 char *block_buf = 0;
2094 ext2_ino_t start_to_move;
2096 int update_ea_inode_refs = 0;
2098 if ((rfs->old_fs->group_desc_count <=
2099 rfs->new_fs->group_desc_count) &&
2103 set_com_err_hook(quiet_com_err_proc);
2105 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
2106 if (retval) goto errout;
2108 retval = ext2fs_init_dblist(rfs->old_fs, 0);
2109 if (retval) goto errout;
2110 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
2111 if (retval) goto errout;
2113 start_to_move = (rfs->new_fs->group_desc_count *
2114 rfs->new_fs->super->s_inodes_per_group);
2116 if (rfs->progress) {
2117 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
2118 0, rfs->old_fs->group_desc_count);
2122 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
2126 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
2127 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
2128 inode = malloc(inode_size);
2134 * First, copy all of the inodes that need to be moved
2135 * elsewhere in the inode table
2138 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
2139 if (retval) goto errout;
2143 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
2144 continue; /* inode not in use */
2146 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2149 /* Remap EA block */
2150 retval = migrate_ea_block(rfs, ino, inode, &pb.changed);
2155 if (ino <= start_to_move)
2156 goto remap_blocks; /* Don't need to move inode. */
2159 * Find a new inode. Now that extents and directory blocks
2160 * are tied to the inode number through the checksum, we must
2161 * set up the new inode before we start rewriting blocks.
2163 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
2167 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
2170 * i_ctime field in xattr inodes contain a portion of the ref
2171 * count, do not overwrite.
2173 if (inode->i_flags & EXT4_EA_INODE_FL)
2174 update_ea_inode_refs = 1;
2176 inode->i_ctime = time(0);
2178 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
2184 #ifdef RESIZE2FS_DEBUG
2185 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
2186 printf("Inode moved %u->%u\n", ino, new_inode);
2189 retval = ext2fs_create_extent_table(&rfs->imap, 0);
2193 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
2197 retval = ext2fs_write_inode_full(rfs->old_fs,
2204 * Update inodes to point to new blocks; schedule directory
2205 * blocks for inode remapping. Need to write out dir blocks
2206 * with new inode numbers if we have metadata_csum enabled.
2208 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2209 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
2210 (rfs->bmap || pb.is_dir)) {
2213 pb.has_extents = inode->i_flags & EXT4_EXTENTS_FL;
2214 retval = ext2fs_block_iterate3(rfs->old_fs,
2215 new_inode, 0, block_buf,
2216 process_block, &pb);
2223 } else if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
2224 (rfs->bmap || pb.is_dir)) {
2225 /* inline data dir; update it too */
2226 retval = ext2fs_add_dir_block2(rfs->old_fs->dblist,
2232 /* Fix up extent block checksums with the new inode number */
2233 if (ext2fs_has_feature_metadata_csum(rfs->old_fs->super) &&
2234 (inode->i_flags & EXT4_EXTENTS_FL)) {
2235 retval = ext2fs_fix_extents_checksums(rfs->old_fs,
2242 if (update_ea_inode_refs &&
2243 ext2fs_has_feature_ea_inode(rfs->new_fs->super)) {
2244 retval = fix_ea_inode_refs(rfs, inode, block_buf,
2249 io_channel_flush(rfs->old_fs->io);
2252 reset_com_err_hook();
2253 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
2255 ext2fs_free_extent_table(rfs->bmap);
2259 ext2fs_close_inode_scan(scan);
2261 ext2fs_free_mem(&block_buf);
2266 /* --------------------------------------------------------------------
2268 * Resize processing, phase 4.
2270 * --------------------------------------------------------------------
2276 unsigned int max_dirs;
2280 static int check_and_change_inodes(ext2_ino_t dir,
2281 int entry EXT2FS_ATTR((unused)),
2282 struct ext2_dir_entry *dirent, int offset,
2283 int blocksize EXT2FS_ATTR((unused)),
2284 char *buf EXT2FS_ATTR((unused)),
2287 struct istruct *is = (struct istruct *) priv_data;
2288 struct ext2_inode inode;
2289 ext2_ino_t new_inode;
2293 if (is->rfs->progress && offset == 0) {
2294 io_channel_flush(is->rfs->old_fs->io);
2295 is->err = (is->rfs->progress)(is->rfs,
2296 E2_RSZ_INODE_REF_UPD_PASS,
2297 ++is->num, is->max_dirs);
2299 return DIRENT_ABORT;
2303 * If we have checksums enabled and the inode wasn't present in the
2304 * old fs, then we must rewrite all dir blocks with new checksums.
2306 if (ext2fs_has_feature_metadata_csum(is->rfs->old_fs->super) &&
2307 !ext2fs_test_inode_bitmap2(is->rfs->old_fs->inode_map, dir))
2308 ret |= DIRENT_CHANGED;
2313 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
2317 #ifdef RESIZE2FS_DEBUG
2318 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
2319 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
2320 dir, ext2fs_dirent_name_len(dirent), dirent->name,
2321 dirent->inode, new_inode);
2324 dirent->inode = new_inode;
2326 /* Update the directory mtime and ctime */
2327 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
2329 inode.i_mtime = inode.i_ctime = time(0);
2330 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
2332 return ret | DIRENT_ABORT;
2335 return ret | DIRENT_CHANGED;
2338 static errcode_t inode_ref_fix(ext2_resize_t rfs)
2347 * Now, we iterate over all of the directories to update the
2351 is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
2355 if (rfs->progress) {
2356 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
2362 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2363 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
2364 DIRENT_FLAG_INCLUDE_EMPTY, 0,
2365 check_and_change_inodes, &is);
2366 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
2374 if (rfs->progress && (is.num < is.max_dirs))
2375 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
2376 is.max_dirs, is.max_dirs);
2379 ext2fs_free_extent_table(rfs->imap);
2385 /* --------------------------------------------------------------------
2387 * Resize processing, phase 5.
2389 * In this phase we actually move the inode table around, and then
2390 * update the summary statistics. This is scary, since aborting here
2391 * will potentially scramble the filesystem. (We are moving the
2392 * inode tables around in place, and so the potential for lost data,
2393 * or at the very least scrambling the mapping between filenames and
2394 * inode numbers is very high in case of a power failure here.)
2395 * --------------------------------------------------------------------
2400 * A very scary routine --- this one moves the inode table around!!!
2402 * After this you have to use the rfs->new_fs file handle to read and
2405 static errcode_t move_itables(ext2_resize_t rfs)
2409 dgrp_t i, max_groups;
2410 ext2_filsys fs = rfs->new_fs;
2412 blk64_t old_blk, new_blk, blk, cluster_freed;
2416 ext2fs_block_bitmap new_bmap = NULL;
2418 max_groups = fs->group_desc_count;
2419 if (max_groups > rfs->old_fs->group_desc_count)
2420 max_groups = rfs->old_fs->group_desc_count;
2422 size = fs->blocksize * fs->inode_blocks_per_group;
2423 if (!rfs->itable_buf) {
2424 retval = ext2fs_get_mem(size, &rfs->itable_buf);
2429 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
2430 retval = ext2fs_allocate_block_bitmap(fs, _("new meta blocks"),
2435 retval = mark_table_blocks(fs, new_bmap);
2441 * Figure out how many inode tables we need to move
2443 to_move = moved = 0;
2444 for (i=0; i < max_groups; i++)
2445 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
2446 ext2fs_inode_table_loc(fs, i))
2454 if (rfs->progress) {
2455 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
2461 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
2463 for (i=0; i < max_groups; i++) {
2464 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
2465 new_blk = ext2fs_inode_table_loc(fs, i);
2466 diff = new_blk - old_blk;
2468 #ifdef RESIZE2FS_DEBUG
2469 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
2470 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
2471 i, (unsigned long long) old_blk,
2472 (unsigned long long) new_blk, diff);
2480 retval = io_channel_read_blk64(fs->io, old_blk,
2481 fs->inode_blocks_per_group,
2486 * The end of the inode table segment often contains
2487 * all zeros, and we're often only moving the inode
2488 * table down a block or two. If so, we can optimize
2489 * things by not rewriting blocks that we know to be zero
2492 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
2495 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
2496 #ifdef RESIZE2FS_DEBUG
2497 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
2498 printf("%d blocks of zeros...\n", n);
2500 num = fs->inode_blocks_per_group;
2504 retval = io_channel_write_blk64(fs->io, new_blk,
2505 num, rfs->itable_buf);
2507 io_channel_write_blk64(fs->io, old_blk,
2508 num, rfs->itable_buf);
2512 retval = io_channel_write_blk64(fs->io,
2513 old_blk + fs->inode_blocks_per_group,
2514 diff, (rfs->itable_buf +
2515 (fs->inode_blocks_per_group - diff) *
2521 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
2522 j < fs->inode_blocks_per_group;) {
2523 if (new_bmap == NULL ||
2524 !ext2fs_test_block_bitmap2(new_bmap, blk)) {
2525 ext2fs_block_alloc_stats2(fs, blk, -1);
2526 cluster_freed = EXT2FS_CLUSTER_RATIO(fs) -
2527 (blk & EXT2FS_CLUSTER_MASK(fs));
2528 blk += cluster_freed;
2536 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
2537 ext2fs_group_desc_csum_set(rfs->old_fs, i);
2538 ext2fs_mark_super_dirty(rfs->old_fs);
2539 ext2fs_flush(rfs->old_fs);
2541 if (rfs->progress) {
2542 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
2548 mark_table_blocks(fs, fs->block_map);
2550 #ifdef RESIZE2FS_DEBUG
2551 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
2552 printf("Inode table move finished.\n");
2558 ext2fs_free_block_bitmap(new_bmap);
2563 * This function is used when expanding a file system. It frees the
2564 * superblock and block group descriptor blocks from the block group
2565 * which is no longer the last block group.
2567 static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs)
2569 ext2_filsys fs = rfs->new_fs;
2570 ext2_filsys old_fs = rfs->old_fs;
2572 dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1;
2573 dgrp_t last_bg = fs->group_desc_count - 1;
2574 blk64_t sb, old_desc;
2577 if (!ext2fs_has_feature_sparse_super2(fs->super))
2580 if (last_bg <= old_last_bg)
2583 if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
2584 fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
2587 if (old_fs->super->s_backup_bgs[0] != old_last_bg &&
2588 old_fs->super->s_backup_bgs[1] != old_last_bg)
2591 if (fs->super->s_backup_bgs[0] == old_last_bg ||
2592 fs->super->s_backup_bgs[1] == old_last_bg)
2595 if (old_last_bg == 0)
2598 retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, old_last_bg,
2599 &sb, &old_desc, NULL, &num);
2604 ext2fs_unmark_block_bitmap2(fs->block_map, sb);
2606 ext2fs_unmark_block_bitmap_range2(fs->block_map, old_desc, num);
2611 * This function is used when shrinking a file system. We need to
2612 * utilize blocks from what will be the new last block group for the
2613 * backup superblock and block group descriptor blocks.
2614 * Unfortunately, those blocks may be used by other files or fs
2615 * metadata blocks. We need to mark them as being in use.
2617 static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
2618 ext2fs_block_bitmap meta_bmap)
2620 ext2_filsys fs = rfs->new_fs;
2621 ext2_filsys old_fs = rfs->old_fs;
2623 dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1;
2624 dgrp_t last_bg = fs->group_desc_count - 1;
2626 blk64_t blk, sb, old_desc;
2630 if (!ext2fs_has_feature_sparse_super2(fs->super))
2633 if (last_bg >= old_last_bg)
2636 if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
2637 fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
2640 if (fs->super->s_backup_bgs[0] != last_bg &&
2641 fs->super->s_backup_bgs[1] != last_bg)
2644 if (old_fs->super->s_backup_bgs[0] == last_bg ||
2645 old_fs->super->s_backup_bgs[1] == last_bg)
2648 retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, last_bg,
2649 &sb, &old_desc, NULL, &num);
2653 if (last_bg && !sb) {
2654 fputs(_("Should never happen! No sb in last super_sparse bg?\n"),
2658 if (old_desc && old_desc != sb+1) {
2659 fputs(_("Should never happen! Unexpected old_desc in "
2660 "super_sparse bg?\n"),
2664 num = (old_desc) ? num : 1;
2666 /* Reserve the backup blocks */
2667 ext2fs_mark_block_bitmap_range2(fs->block_map, sb, num);
2669 for (g = 0; g < fs->group_desc_count; g++) {
2672 mb = ext2fs_block_bitmap_loc(fs, g);
2673 if ((mb >= sb) && (mb < sb + num)) {
2674 ext2fs_block_bitmap_loc_set(fs, g, 0);
2677 mb = ext2fs_inode_bitmap_loc(fs, g);
2678 if ((mb >= sb) && (mb < sb + num)) {
2679 ext2fs_inode_bitmap_loc_set(fs, g, 0);
2682 mb = ext2fs_inode_table_loc(fs, g);
2683 if ((mb < sb + num) &&
2684 (sb < mb + fs->inode_blocks_per_group)) {
2685 ext2fs_inode_table_loc_set(fs, g, 0);
2689 retval = ext2fs_allocate_group_table(fs, g, 0);
2695 for (blk = sb, i = 0; i < num; blk++, i++) {
2696 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
2697 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
2698 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
2699 rfs->needed_blocks++;
2701 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
2707 * Fix the resize inode
2709 static errcode_t fix_resize_inode(ext2_filsys fs)
2711 struct ext2_inode inode;
2714 if (!ext2fs_has_feature_resize_inode(fs->super))
2717 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
2718 if (retval) goto errout;
2720 ext2fs_iblk_set(fs, &inode, 1);
2722 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
2723 if (retval) goto errout;
2725 if (!inode.i_block[EXT2_DIND_BLOCK]) {
2727 * Avoid zeroing out block #0; that's rude. This
2728 * should never happen anyway since the filesystem
2729 * should be fsck'ed and we assume it is consistent.
2731 fprintf(stderr, "%s",
2732 _("Should never happen: resize inode corrupt!\n"));
2736 retval = ext2fs_zero_blocks2(fs, inode.i_block[EXT2_DIND_BLOCK], 1,
2741 retval = ext2fs_create_resize_inode(fs);
2750 * Finally, recalculate the summary information
2752 static errcode_t resize2fs_calculate_summary_stats(ext2_filsys fs)
2756 unsigned int group = 0;
2757 unsigned int count = 0;
2758 blk64_t total_blocks_free = 0;
2759 int total_inodes_free = 0;
2762 blk64_t super_blk, old_desc_blk, new_desc_blk;
2763 int old_desc_blocks;
2766 * First calculate the block statistics
2768 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2769 ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
2771 if (ext2fs_has_feature_meta_bg(fs->super))
2772 old_desc_blocks = fs->super->s_first_meta_bg;
2774 old_desc_blocks = fs->desc_blocks +
2775 fs->super->s_reserved_gdt_blocks;
2776 for (blk = B2C(fs->super->s_first_data_block);
2777 blk < ext2fs_blocks_count(fs->super);
2778 blk += EXT2FS_CLUSTER_RATIO(fs)) {
2780 !(EQ_CLSTR(blk, super_blk) ||
2781 ((old_desc_blk && old_desc_blocks &&
2782 GE_CLSTR(blk, old_desc_blk) &&
2783 LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) ||
2784 ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) ||
2785 EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) ||
2786 EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) ||
2787 ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) &&
2788 LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group)
2789 + fs->inode_blocks_per_group))))) ||
2790 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
2792 total_blocks_free++;
2795 if ((count == fs->super->s_clusters_per_group) ||
2796 EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) {
2797 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
2798 ext2fs_group_desc_csum_set(fs, group);
2800 if (group >= fs->group_desc_count)
2804 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2805 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
2808 if (ext2fs_has_feature_meta_bg(fs->super))
2809 old_desc_blocks = fs->super->s_first_meta_bg;
2811 old_desc_blocks = fs->desc_blocks +
2812 fs->super->s_reserved_gdt_blocks;
2815 total_blocks_free = C2B(total_blocks_free);
2816 ext2fs_free_blocks_count_set(fs->super, total_blocks_free);
2819 * Next, calculate the inode statistics
2825 /* Protect loop from wrap-around if s_inodes_count maxed */
2826 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2827 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2829 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2831 total_inodes_free++;
2834 if ((count == fs->super->s_inodes_per_group) ||
2835 (ino == fs->super->s_inodes_count)) {
2836 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
2837 ext2fs_group_desc_csum_set(fs, group);
2839 if (group >= fs->group_desc_count)
2843 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2846 fs->super->s_free_inodes_count = total_inodes_free;
2847 ext2fs_mark_super_dirty(fs);
2852 * Journal may have been relocated; update the backup journal blocks
2853 * in the superblock.
2855 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
2858 struct ext2_inode inode;
2860 if (!ext2fs_has_feature_journal(fs->super))
2863 /* External journal? Nothing to do. */
2864 if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
2867 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
2870 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
2871 fs->super->s_jnl_blocks[15] = inode.i_size_high;
2872 fs->super->s_jnl_blocks[16] = inode.i_size;
2873 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
2874 ext2fs_mark_super_dirty(fs);
2878 static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
2879 int old_desc_blocks)
2881 blk64_t super_blk, old_desc_blk, new_desc_blk;
2884 /* inode table blocks plus allocation bitmaps */
2885 overhead = fs->inode_blocks_per_group + 2;
2887 ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
2888 &old_desc_blk, &new_desc_blk, 0);
2889 if ((grp == 0) || super_blk)
2892 overhead += old_desc_blocks;
2893 else if (new_desc_blk)
2900 * calculate the minimum number of blocks the given fs can be resized to
2902 blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
2904 ext2_ino_t inode_count;
2905 dgrp_t groups, flex_groups;
2906 blk64_t blks_needed, data_blocks;
2907 blk64_t grp, data_needed, last_start;
2908 blk64_t overhead = 0;
2909 int old_desc_blocks;
2910 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2913 * first figure out how many group descriptors we need to
2914 * handle the number of inodes we have
2916 inode_count = fs->super->s_inodes_count -
2917 fs->super->s_free_inodes_count;
2918 blks_needed = ext2fs_div_ceil(inode_count,
2919 fs->super->s_inodes_per_group) *
2920 (blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super);
2921 groups = ext2fs_div64_ceil(blks_needed,
2922 EXT2_BLOCKS_PER_GROUP(fs->super));
2923 #ifdef RESIZE2FS_DEBUG
2924 if (flags & RESIZE_DEBUG_MIN_CALC)
2925 printf("fs has %d inodes, %d groups required.\n",
2926 inode_count, groups);
2930 * number of old-style block group descriptor blocks
2932 if (ext2fs_has_feature_meta_bg(fs->super))
2933 old_desc_blocks = fs->super->s_first_meta_bg;
2935 old_desc_blocks = fs->desc_blocks +
2936 fs->super->s_reserved_gdt_blocks;
2938 /* calculate how many blocks are needed for data */
2939 data_needed = ext2fs_blocks_count(fs->super);
2940 for (grp = 0; grp < fs->group_desc_count; grp++) {
2941 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
2942 data_needed -= ext2fs_bg_free_blocks_count(fs, grp);
2944 #ifdef RESIZE2FS_DEBUG
2945 if (flags & RESIZE_DEBUG_MIN_CALC)
2946 printf("fs requires %llu data blocks.\n",
2947 (unsigned long long) data_needed);
2951 * For ext4 we need to allow for up to a flex_bg worth of
2952 * inode tables of slack space so the resize operation can be
2953 * guaranteed to finish.
2955 flex_groups = groups;
2956 if (ext2fs_has_feature_flex_bg(fs->super)) {
2957 dgrp_t remainder = groups & (flexbg_size - 1);
2959 flex_groups += flexbg_size - remainder;
2960 if (flex_groups > fs->group_desc_count)
2961 flex_groups = fs->group_desc_count;
2965 * figure out how many data blocks we have given the number of groups
2966 * we need for our inodes
2968 data_blocks = EXT2_GROUPS_TO_BLOCKS(fs->super, groups);
2970 for (grp = 0; grp < flex_groups; grp++) {
2971 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
2974 * we want to keep track of how much data we can store in
2975 * the groups leading up to the last group so we can determine
2976 * how big the last group needs to be
2978 if (grp < (groups - 1))
2979 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
2982 if (data_blocks > overhead)
2983 data_blocks -= overhead;
2987 #ifdef RESIZE2FS_DEBUG
2988 if (flags & RESIZE_DEBUG_MIN_CALC)
2989 printf("With %d group(s), we have %llu blocks available.\n",
2990 groups, (unsigned long long) data_blocks);
2994 * if we need more group descriptors in order to accommodate our data
2995 * then we need to add them here
2997 blks_needed = data_needed;
2998 while (blks_needed > data_blocks) {
2999 blk64_t remainder = blks_needed - data_blocks;
3002 /* figure out how many more groups we need for the data */
3003 extra_grps = ext2fs_div64_ceil(remainder,
3004 EXT2_BLOCKS_PER_GROUP(fs->super));
3006 data_blocks += EXT2_GROUPS_TO_BLOCKS(fs->super, extra_grps);
3008 /* ok we have to account for the last group */
3009 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
3010 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
3013 groups += extra_grps;
3014 if (!ext2fs_has_feature_flex_bg(fs->super))
3015 flex_groups = groups;
3016 else if (groups > flex_groups) {
3017 dgrp_t r = groups & (flexbg_size - 1);
3019 flex_groups = groups + flexbg_size - r;
3020 if (flex_groups > fs->group_desc_count)
3021 flex_groups = fs->group_desc_count;
3024 for (; grp < flex_groups; grp++) {
3025 overhead = calc_group_overhead(fs, grp,
3029 * again, we need to see how much data we cram into
3030 * all of the groups leading up to the last group
3032 if (grp < groups - 1)
3033 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
3036 data_blocks -= overhead;
3039 #ifdef RESIZE2FS_DEBUG
3040 if (flags & RESIZE_DEBUG_MIN_CALC)
3041 printf("Added %d extra group(s), "
3042 "blks_needed %llu, data_blocks %llu, "
3043 "last_start %llu\n", extra_grps,
3044 (unsigned long long) blks_needed,
3045 (unsigned long long) data_blocks,
3046 (unsigned long long) last_start);
3050 /* now for the fun voodoo */
3052 if (ext2fs_has_feature_flex_bg(fs->super) &&
3053 (grp & ~(flexbg_size - 1)) == 0)
3054 grp = grp & ~(flexbg_size - 1);
3056 for (; grp < flex_groups; grp++)
3057 overhead += calc_group_overhead(fs, grp, old_desc_blocks);
3059 #ifdef RESIZE2FS_DEBUG
3060 if (flags & RESIZE_DEBUG_MIN_CALC)
3061 printf("Last group's overhead is %llu\n",
3062 (unsigned long long) overhead);
3066 * if this is the case then the last group is going to have data in it
3067 * so we need to adjust the size of the last group accordingly
3069 if (last_start < blks_needed) {
3070 blk64_t remainder = blks_needed - last_start;
3072 #ifdef RESIZE2FS_DEBUG
3073 if (flags & RESIZE_DEBUG_MIN_CALC)
3074 printf("Need %llu data blocks in last group\n",
3075 (unsigned long long) remainder);
3078 * 50 is a magic number that mkfs/resize uses to see if its
3079 * even worth making/resizing the fs. basically you need to
3080 * have at least 50 blocks in addition to the blocks needed
3081 * for the metadata in the last group
3084 overhead += remainder;
3090 overhead += fs->super->s_first_data_block;
3091 #ifdef RESIZE2FS_DEBUG
3092 if (flags & RESIZE_DEBUG_MIN_CALC)
3093 printf("Final size of last group is %llu\n",
3094 (unsigned long long) overhead);
3097 /* Add extra slack for bigalloc file systems */
3098 if (EXT2FS_CLUSTER_RATIO(fs) > 1)
3099 overhead += EXT2FS_CLUSTER_RATIO(fs) * 2;
3102 * since our last group doesn't have to be BLOCKS_PER_GROUP
3103 * large, we only do groups-1, and then add the number of
3104 * blocks needed to handle the group descriptor metadata+data
3107 blks_needed = EXT2_GROUPS_TO_BLOCKS(fs->super, groups - 1);
3108 blks_needed += overhead;
3111 * Make sure blks_needed covers the end of the inode table in
3112 * the last block group.
3114 overhead = ext2fs_inode_table_loc(fs, groups-1) +
3115 fs->inode_blocks_per_group;
3116 if (blks_needed < overhead)
3117 blks_needed = overhead;
3119 #ifdef RESIZE2FS_DEBUG
3120 if (flags & RESIZE_DEBUG_MIN_CALC)
3121 printf("Estimated blocks needed: %llu\n",
3122 (unsigned long long) blks_needed);
3126 * If at this point we've already added up more "needed" than
3127 * the current size, just return current size as minimum.
3129 if (blks_needed >= ext2fs_blocks_count(fs->super))
3130 return ext2fs_blocks_count(fs->super);
3132 * We need to reserve a few extra blocks if extents are
3133 * enabled, in case we need to grow the extent tree. The more
3134 * we shrink the file system, the more space we need.
3136 * The absolute worst case is every single data block is in
3137 * the part of the file system that needs to be evacuated,
3138 * with each data block needs to be in its own extent, and
3139 * with each inode needing at least one extent block.
3141 if (ext2fs_has_feature_extents(fs->super)) {
3142 blk64_t safe_margin = (ext2fs_blocks_count(fs->super) -
3144 unsigned int exts_per_blk = (fs->blocksize /
3145 sizeof(struct ext3_extent)) - 1;
3146 blk64_t worst_case = ((data_needed + exts_per_blk - 1) /
3149 if (worst_case < inode_count)
3150 worst_case = inode_count;
3152 if (safe_margin > worst_case)
3153 safe_margin = worst_case;
3155 #ifdef RESIZE2FS_DEBUG
3156 if (flags & RESIZE_DEBUG_MIN_CALC)
3157 printf("Extents safety margin: %llu\n",
3158 (unsigned long long) safe_margin);
3160 blks_needed += safe_margin;