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 (!getenv("RESIZE2FS_FORCE_ITABLE_INIT") &&
908 access("/sys/fs/ext4/features/lazy_itable_init", F_OK
) == 0)
909 lazy_itable_init
= 1;
910 if (fs
->super
->s_feature_incompat
& EXT2_FEATURE_INCOMPAT_META_BG
)
911 old_desc_blocks
= fs
->super
->s_first_meta_bg
;
913 old_desc_blocks
= fs
->desc_blocks
+
914 fs
->super
->s_reserved_gdt_blocks
;
917 * If we changed the number of block_group descriptor blocks,
918 * we need to make sure they are all marked as reserved in the
919 * file systems's block allocation map.
921 for (i
= 0; i
< old_fs
->group_desc_count
; i
++)
922 ext2fs_reserve_super_and_bgd(fs
, i
, fs
->block_map
);
924 for (i
= old_fs
->group_desc_count
;
925 i
< fs
->group_desc_count
; i
++) {
926 memset(ext2fs_group_desc(fs
, fs
->group_desc
, i
), 0,
927 sizeof(struct ext2_group_desc
));
930 ext2fs_bg_flags_zap(fs
, i
);
932 ext2fs_bg_flags_set(fs
, i
, EXT2_BG_INODE_UNINIT
);
933 if (!lazy_itable_init
)
934 ext2fs_bg_flags_set(fs
, i
,
935 EXT2_BG_INODE_ZEROED
);
936 ext2fs_bg_itable_unused_set(fs
, i
,
937 fs
->super
->s_inodes_per_group
);
940 numblocks
= ext2fs_group_blocks_count(fs
, i
);
941 if ((i
< fs
->group_desc_count
- 1) && csum_flag
)
942 ext2fs_bg_flags_set(fs
, i
, EXT2_BG_BLOCK_UNINIT
);
944 has_super
= ext2fs_bg_has_super(fs
, i
);
946 ext2fs_block_alloc_stats2(fs
, group_block
, +1);
949 meta_bg_size
= EXT2_DESC_PER_BLOCK(fs
->super
);
950 meta_bg
= i
/ meta_bg_size
;
951 if (!(fs
->super
->s_feature_incompat
&
952 EXT2_FEATURE_INCOMPAT_META_BG
) ||
953 (meta_bg
< fs
->super
->s_first_meta_bg
)) {
955 for (j
=0; j
< old_desc_blocks
; j
++)
956 ext2fs_block_alloc_stats2(fs
,
957 group_block
+ 1 + j
, +1);
958 adjblocks
+= old_desc_blocks
;
963 if (((i
% meta_bg_size
) == 0) ||
964 ((i
% meta_bg_size
) == 1) ||
965 ((i
% meta_bg_size
) == (meta_bg_size
-1)))
966 ext2fs_block_alloc_stats2(fs
,
967 group_block
+ has_super
, +1);
970 adjblocks
+= 2 + fs
->inode_blocks_per_group
;
972 numblocks
-= adjblocks
;
973 ext2fs_free_blocks_count_set(fs
->super
,
974 ext2fs_free_blocks_count(fs
->super
) - adjblocks
);
975 fs
->super
->s_free_inodes_count
+=
976 fs
->super
->s_inodes_per_group
;
977 ext2fs_bg_free_blocks_count_set(fs
, i
, numblocks
);
978 ext2fs_bg_free_inodes_count_set(fs
, i
,
979 fs
->super
->s_inodes_per_group
);
980 ext2fs_bg_used_dirs_count_set(fs
, i
, 0);
981 ext2fs_group_desc_csum_set(fs
, i
);
983 retval
= ext2fs_allocate_group_table(fs
, i
, 0);
984 if (retval
) goto errout
;
986 group_block
+= fs
->super
->s_blocks_per_group
;
991 * Mark all of the metadata blocks as reserved so they won't
992 * get allocated by the call to ext2fs_allocate_group_table()
993 * in blocks_to_move(), where we allocate new blocks to
994 * replace those allocation bitmap and inode table blocks
995 * which have to get relocated to make space for an increased
996 * number of the block group descriptors.
999 mark_table_blocks(fs
, reserve_blocks
);
1006 * This routine adjusts the superblock and other data structures, both
1007 * in disk as well as in memory...
1009 static errcode_t
adjust_superblock(ext2_resize_t rfs
, blk64_t new_size
)
1011 ext2_filsys fs
= rfs
->new_fs
;
1014 blk64_t group_block
;
1016 unsigned long max_group
;
1018 ext2fs_mark_super_dirty(fs
);
1019 ext2fs_mark_bb_dirty(fs
);
1020 ext2fs_mark_ib_dirty(fs
);
1022 retval
= ext2fs_allocate_block_bitmap(fs
, _("reserved blocks"),
1023 &rfs
->reserve_blocks
);
1027 retval
= adjust_fs_info(fs
, rfs
->old_fs
, rfs
->reserve_blocks
, new_size
);
1032 * Check to make sure there are enough inodes
1034 if ((rfs
->old_fs
->super
->s_inodes_count
-
1035 rfs
->old_fs
->super
->s_free_inodes_count
) >
1036 rfs
->new_fs
->super
->s_inodes_count
) {
1042 * If we are shrinking the number block groups, we're done and
1045 if (rfs
->old_fs
->group_desc_count
> fs
->group_desc_count
) {
1051 * If the number of block groups is staying the same, we're
1052 * done and can exit now. (If the number block groups is
1053 * shrinking, we had exited earlier.)
1055 if (rfs
->old_fs
->group_desc_count
>= fs
->group_desc_count
) {
1061 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
1062 * supports lazy inode initialization, we can skip
1063 * initializing the inode table.
1065 if (lazy_itable_init
&& ext2fs_has_group_desc_csum(fs
)) {
1071 * Initialize the inode table
1073 retval
= ext2fs_get_array(fs
->blocksize
, fs
->inode_blocks_per_group
,
1078 memset(rfs
->itable_buf
, 0, fs
->blocksize
* fs
->inode_blocks_per_group
);
1079 group_block
= ext2fs_group_first_block2(fs
,
1080 rfs
->old_fs
->group_desc_count
);
1081 adj
= rfs
->old_fs
->group_desc_count
;
1082 max_group
= fs
->group_desc_count
- adj
;
1083 if (rfs
->progress
) {
1084 retval
= rfs
->progress(rfs
, E2_RSZ_EXTEND_ITABLE_PASS
,
1089 for (i
= rfs
->old_fs
->group_desc_count
;
1090 i
< fs
->group_desc_count
; i
++) {
1092 * Write out the new inode table
1094 retval
= ext2fs_zero_blocks2(fs
, ext2fs_inode_table_loc(fs
, i
),
1095 fs
->inode_blocks_per_group
, NULL
,
1100 io_channel_flush(fs
->io
);
1101 if (rfs
->progress
) {
1102 retval
= rfs
->progress(rfs
, E2_RSZ_EXTEND_ITABLE_PASS
,
1103 i
- adj
+ 1, max_group
);
1107 group_block
+= fs
->super
->s_blocks_per_group
;
1109 io_channel_flush(fs
->io
);
1116 /* --------------------------------------------------------------------
1118 * Resize processing, phase 2.
1120 * In this phase we adjust determine which blocks need to be moved, in
1121 * blocks_to_move(). We then copy the blocks to their ultimate new
1122 * destinations using block_mover(). Since we are copying blocks to
1123 * their new locations, again during this pass we can abort without
1125 * --------------------------------------------------------------------
1129 * This helper function creates a block bitmap with all of the
1130 * filesystem meta-data blocks.
1132 static errcode_t
mark_table_blocks(ext2_filsys fs
,
1133 ext2fs_block_bitmap bmap
)
1138 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
1139 ext2fs_reserve_super_and_bgd(fs
, i
, bmap
);
1142 * Mark the blocks used for the inode table
1144 blk
= ext2fs_inode_table_loc(fs
, i
);
1146 ext2fs_mark_block_bitmap_range2(bmap
, blk
,
1147 fs
->inode_blocks_per_group
);
1150 * Mark block used for the block bitmap
1152 blk
= ext2fs_block_bitmap_loc(fs
, i
);
1154 ext2fs_mark_block_bitmap2(bmap
, blk
);
1157 * Mark block used for the inode bitmap
1159 blk
= ext2fs_inode_bitmap_loc(fs
, i
);
1161 ext2fs_mark_block_bitmap2(bmap
, blk
);
1167 * This function checks to see if a particular block (either a
1168 * superblock or a block group descriptor) overlaps with an inode or
1169 * block bitmap block, or with the inode table.
1171 static void mark_fs_metablock(ext2_resize_t rfs
,
1172 ext2fs_block_bitmap meta_bmap
,
1173 int group
, blk64_t blk
)
1175 ext2_filsys fs
= rfs
->new_fs
;
1177 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
, blk
);
1178 ext2fs_block_alloc_stats2(fs
, blk
, +1);
1181 * Check to see if we overlap with the inode or block bitmap,
1182 * or the inode tables. If not, and the block is in use, then
1183 * mark it as a block to be moved.
1185 if (IS_BLOCK_BM(fs
, group
, blk
)) {
1186 ext2fs_block_bitmap_loc_set(fs
, group
, 0);
1187 rfs
->needed_blocks
++;
1190 if (IS_INODE_BM(fs
, group
, blk
)) {
1191 ext2fs_inode_bitmap_loc_set(fs
, group
, 0);
1192 rfs
->needed_blocks
++;
1195 if (IS_INODE_TB(fs
, group
, blk
)) {
1196 ext2fs_inode_table_loc_set(fs
, group
, 0);
1197 rfs
->needed_blocks
++;
1200 if (fs
->super
->s_feature_incompat
& EXT4_FEATURE_INCOMPAT_FLEX_BG
) {
1203 for (i
=0; i
< rfs
->old_fs
->group_desc_count
; i
++) {
1204 if (IS_BLOCK_BM(fs
, i
, blk
)) {
1205 ext2fs_block_bitmap_loc_set(fs
, i
, 0);
1206 rfs
->needed_blocks
++;
1209 if (IS_INODE_BM(fs
, i
, blk
)) {
1210 ext2fs_inode_bitmap_loc_set(fs
, i
, 0);
1211 rfs
->needed_blocks
++;
1214 if (IS_INODE_TB(fs
, i
, blk
)) {
1215 ext2fs_inode_table_loc_set(fs
, i
, 0);
1216 rfs
->needed_blocks
++;
1222 if (ext2fs_has_group_desc_csum(fs
) &&
1223 (ext2fs_bg_flags_test(fs
, group
, EXT2_BG_BLOCK_UNINIT
))) {
1225 * If the block bitmap is uninitialized, which means
1226 * nothing other than standard metadata in use.
1229 } else if (ext2fs_test_block_bitmap2(rfs
->old_fs
->block_map
, blk
) &&
1230 !ext2fs_test_block_bitmap2(meta_bmap
, blk
)) {
1231 ext2fs_mark_block_bitmap2(rfs
->move_blocks
, blk
);
1232 rfs
->needed_blocks
++;
1238 * This routine marks and unmarks reserved blocks in the new block
1239 * bitmap. It also determines which blocks need to be moved and
1240 * places this information into the move_blocks bitmap.
1242 static errcode_t
blocks_to_move(ext2_resize_t rfs
)
1245 dgrp_t i
, max_groups
, g
;
1246 blk64_t blk
, group_blk
;
1247 blk64_t old_blocks
, new_blocks
, group_end
, cluster_freed
;
1249 unsigned int meta_bg
, meta_bg_size
;
1251 ext2_filsys fs
, old_fs
;
1252 ext2fs_block_bitmap meta_bmap
, new_meta_bmap
= NULL
;
1256 old_fs
= rfs
->old_fs
;
1257 if (ext2fs_blocks_count(old_fs
->super
) > ext2fs_blocks_count(fs
->super
))
1260 retval
= ext2fs_allocate_block_bitmap(fs
, _("blocks to be moved"),
1265 retval
= ext2fs_allocate_block_bitmap(fs
, _("meta-data blocks"),
1270 retval
= mark_table_blocks(old_fs
, meta_bmap
);
1277 * If we're shrinking the filesystem, we need to move any
1278 * group's metadata blocks (either allocation bitmaps or the
1279 * inode table) which are beyond the end of the new
1282 new_size
= ext2fs_blocks_count(fs
->super
);
1283 if (new_size
< ext2fs_blocks_count(old_fs
->super
)) {
1284 for (g
= 0; g
< fs
->group_desc_count
; g
++) {
1287 * ext2fs_allocate_group_table will re-allocate any
1288 * metadata blocks whose location is set to zero.
1290 if (ext2fs_block_bitmap_loc(fs
, g
) >= new_size
) {
1291 ext2fs_block_bitmap_loc_set(fs
, g
, 0);
1294 if (ext2fs_inode_bitmap_loc(fs
, g
) >= new_size
) {
1295 ext2fs_inode_bitmap_loc_set(fs
, g
, 0);
1298 if ((ext2fs_inode_table_loc(fs
, g
) +
1299 fs
->inode_blocks_per_group
) > new_size
) {
1300 ext2fs_inode_table_loc_set(fs
, g
, 0);
1305 retval
= ext2fs_allocate_group_table(fs
, g
, 0);
1313 * If we're shrinking the filesystem, we need to move all of
1314 * the blocks that don't fit any more
1316 for (blk
= ext2fs_blocks_count(fs
->super
);
1317 blk
< ext2fs_blocks_count(old_fs
->super
); blk
++) {
1318 g
= ext2fs_group_of_blk2(fs
, blk
);
1319 if (ext2fs_has_group_desc_csum(fs
) &&
1320 ext2fs_bg_flags_test(old_fs
, g
, EXT2_BG_BLOCK_UNINIT
)) {
1322 * The block bitmap is uninitialized, so skip
1323 * to the next block group.
1325 blk
= ext2fs_group_first_block2(fs
, g
+1) - 1;
1328 if (ext2fs_test_block_bitmap2(old_fs
->block_map
, blk
) &&
1329 !ext2fs_test_block_bitmap2(meta_bmap
, blk
)) {
1330 ext2fs_mark_block_bitmap2(rfs
->move_blocks
, blk
);
1331 rfs
->needed_blocks
++;
1333 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
, blk
);
1336 if (old_fs
->super
->s_feature_incompat
& EXT2_FEATURE_INCOMPAT_META_BG
)
1337 old_blocks
= old_fs
->super
->s_first_meta_bg
;
1339 old_blocks
= old_fs
->desc_blocks
+
1340 old_fs
->super
->s_reserved_gdt_blocks
;
1341 if (fs
->super
->s_feature_incompat
& EXT2_FEATURE_INCOMPAT_META_BG
)
1342 new_blocks
= fs
->super
->s_first_meta_bg
;
1344 new_blocks
= fs
->desc_blocks
+ fs
->super
->s_reserved_gdt_blocks
;
1346 retval
= reserve_sparse_super2_last_group(rfs
, meta_bmap
);
1350 if (EXT2_DESC_SIZE(rfs
->old_fs
->super
) ==
1351 EXT2_DESC_SIZE(rfs
->new_fs
->super
) &&
1352 old_blocks
== new_blocks
) {
1357 max_groups
= fs
->group_desc_count
;
1358 if (max_groups
> old_fs
->group_desc_count
)
1359 max_groups
= old_fs
->group_desc_count
;
1360 group_blk
= old_fs
->super
->s_first_data_block
;
1362 * If we're reducing the number of descriptor blocks, this
1363 * makes life easy. :-) We just have to mark some extra
1366 if (old_blocks
> new_blocks
) {
1367 if (EXT2FS_CLUSTER_RATIO(fs
) > 1) {
1368 retval
= ext2fs_allocate_block_bitmap(fs
,
1369 _("new meta blocks"),
1374 retval
= mark_table_blocks(fs
, new_meta_bmap
);
1379 for (i
= 0; i
< max_groups
; i
++) {
1380 if (!ext2fs_bg_has_super(old_fs
, i
)) {
1381 group_blk
+= fs
->super
->s_blocks_per_group
;
1384 group_end
= group_blk
+ 1 + old_blocks
;
1385 for (blk
= group_blk
+ 1 + new_blocks
;
1387 if (new_meta_bmap
== NULL
||
1388 !ext2fs_test_block_bitmap2(new_meta_bmap
,
1391 EXT2FS_CLUSTER_RATIO(fs
) -
1393 EXT2FS_CLUSTER_MASK(fs
));
1394 if (cluster_freed
> group_end
- blk
)
1395 cluster_freed
= group_end
- blk
;
1396 ext2fs_block_alloc_stats2(fs
, blk
, -1);
1397 blk
+= EXT2FS_CLUSTER_RATIO(fs
);
1398 rfs
->needed_blocks
-= cluster_freed
;
1401 rfs
->needed_blocks
--;
1404 group_blk
+= fs
->super
->s_blocks_per_group
;
1410 * If we're increasing the number of descriptor blocks, life
1411 * gets interesting....
1413 meta_bg_size
= EXT2_DESC_PER_BLOCK(fs
->super
);
1414 flex_bg
= fs
->super
->s_feature_incompat
&
1415 EXT4_FEATURE_INCOMPAT_FLEX_BG
;
1416 /* first reserve all of the existing fs meta blocks */
1417 for (i
= 0; i
< max_groups
; i
++) {
1418 has_super
= ext2fs_bg_has_super(fs
, i
);
1420 mark_fs_metablock(rfs
, meta_bmap
, i
, group_blk
);
1422 meta_bg
= i
/ meta_bg_size
;
1423 if (!(fs
->super
->s_feature_incompat
&
1424 EXT2_FEATURE_INCOMPAT_META_BG
) ||
1425 (meta_bg
< fs
->super
->s_first_meta_bg
)) {
1427 for (blk
= group_blk
+1;
1428 blk
< group_blk
+ 1 + new_blocks
; blk
++)
1429 mark_fs_metablock(rfs
, meta_bmap
,
1435 if (((i
% meta_bg_size
) == 0) ||
1436 ((i
% meta_bg_size
) == 1) ||
1437 ((i
% meta_bg_size
) == (meta_bg_size
-1)))
1438 mark_fs_metablock(rfs
, meta_bmap
, i
,
1439 group_blk
+ has_super
);
1443 * Reserve the existing meta blocks that we know
1444 * aren't to be moved.
1446 * For flex_bg file systems, in order to avoid
1447 * overwriting fs metadata (especially inode table
1448 * blocks) belonging to a different block group when
1449 * we are relocating the inode tables, we need to
1450 * reserve all existing fs metadata blocks.
1452 if (ext2fs_block_bitmap_loc(fs
, i
))
1453 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
,
1454 ext2fs_block_bitmap_loc(fs
, i
));
1455 else if (flex_bg
&& i
< old_fs
->group_desc_count
)
1456 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
,
1457 ext2fs_block_bitmap_loc(old_fs
, i
));
1459 if (ext2fs_inode_bitmap_loc(fs
, i
))
1460 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
,
1461 ext2fs_inode_bitmap_loc(fs
, i
));
1462 else if (flex_bg
&& i
< old_fs
->group_desc_count
)
1463 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
,
1464 ext2fs_inode_bitmap_loc(old_fs
, i
));
1466 if (ext2fs_inode_table_loc(fs
, i
))
1467 ext2fs_mark_block_bitmap_range2(rfs
->reserve_blocks
,
1468 ext2fs_inode_table_loc(fs
, i
),
1469 fs
->inode_blocks_per_group
);
1470 else if (flex_bg
&& i
< old_fs
->group_desc_count
)
1471 ext2fs_mark_block_bitmap_range2(rfs
->reserve_blocks
,
1472 ext2fs_inode_table_loc(old_fs
, i
),
1473 old_fs
->inode_blocks_per_group
);
1475 group_blk
+= rfs
->new_fs
->super
->s_blocks_per_group
;
1478 /* Allocate the missing data structures */
1479 for (i
= 0; i
< max_groups
; i
++) {
1480 if (ext2fs_inode_table_loc(fs
, i
) &&
1481 ext2fs_inode_bitmap_loc(fs
, i
) &&
1482 ext2fs_block_bitmap_loc(fs
, i
))
1485 retval
= ext2fs_allocate_group_table(fs
, i
,
1486 rfs
->reserve_blocks
);
1491 * For those structures that have changed, we need to
1494 if (ext2fs_block_bitmap_loc(old_fs
, i
) !=
1495 (blk
= ext2fs_block_bitmap_loc(fs
, i
))) {
1496 ext2fs_block_alloc_stats2(fs
, blk
, +1);
1497 if (ext2fs_test_block_bitmap2(old_fs
->block_map
, blk
) &&
1498 !ext2fs_test_block_bitmap2(meta_bmap
, blk
))
1499 ext2fs_mark_block_bitmap2(rfs
->move_blocks
,
1502 if (ext2fs_inode_bitmap_loc(old_fs
, i
) !=
1503 (blk
= ext2fs_inode_bitmap_loc(fs
, i
))) {
1504 ext2fs_block_alloc_stats2(fs
, blk
, +1);
1505 if (ext2fs_test_block_bitmap2(old_fs
->block_map
, blk
) &&
1506 !ext2fs_test_block_bitmap2(meta_bmap
, blk
))
1507 ext2fs_mark_block_bitmap2(rfs
->move_blocks
,
1512 * The inode table, if we need to relocate it, is
1513 * handled specially. We have to reserve the blocks
1514 * for both the old and the new inode table, since we
1515 * can't have the inode table be destroyed during the
1516 * block relocation phase.
1518 if (ext2fs_inode_table_loc(fs
, i
) == ext2fs_inode_table_loc(old_fs
, i
))
1519 continue; /* inode table not moved */
1521 rfs
->needed_blocks
+= fs
->inode_blocks_per_group
;
1524 * Mark the new inode table as in use in the new block
1525 * allocation bitmap, and move any blocks that might
1528 for (blk
= ext2fs_inode_table_loc(fs
, i
), j
=0;
1529 j
< fs
->inode_blocks_per_group
; j
++, blk
++) {
1530 ext2fs_block_alloc_stats2(fs
, blk
, +1);
1531 if (ext2fs_test_block_bitmap2(old_fs
->block_map
, blk
) &&
1532 !ext2fs_test_block_bitmap2(meta_bmap
, blk
))
1533 ext2fs_mark_block_bitmap2(rfs
->move_blocks
,
1538 * Make sure the old inode table is reserved in the
1539 * block reservation bitmap.
1541 for (blk
= ext2fs_inode_table_loc(rfs
->old_fs
, i
), j
=0;
1542 j
< fs
->inode_blocks_per_group
; j
++, blk
++)
1543 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
, blk
);
1549 ext2fs_free_block_bitmap(new_meta_bmap
);
1551 ext2fs_free_block_bitmap(meta_bmap
);
1557 * This helper function tries to allocate a new block. We try to
1558 * avoid hitting the original group descriptor blocks at least at
1559 * first, since we want to make it possible to recover from a badly
1560 * aborted resize operation as much as possible.
1562 * In the future, I may further modify this routine to balance out
1563 * where we get the new blocks across the various block groups.
1564 * Ideally we would allocate blocks that corresponded with the block
1565 * group of the containing inode, and keep contiguous blocks
1566 * together. However, this very difficult to do efficiently, since we
1567 * don't have the necessary information up front.
1571 #define DESPERATION 2
1573 static void init_block_alloc(ext2_resize_t rfs
)
1575 rfs
->alloc_state
= AVOID_OLD
;
1576 rfs
->new_blk
= rfs
->new_fs
->super
->s_first_data_block
;
1578 /* HACK for testing */
1579 if (ext2fs_blocks_count(rfs
->new_fs
->super
) >
1580 ext2fs_blocks_count(rfs
->old_fs
->super
))
1581 rfs
->new_blk
= ext2fs_blocks_count(rfs
->old_fs
->super
);
1585 static blk64_t
get_new_block(ext2_resize_t rfs
)
1587 ext2_filsys fs
= rfs
->new_fs
;
1590 if (rfs
->new_blk
>= ext2fs_blocks_count(fs
->super
)) {
1591 if (rfs
->alloc_state
== DESPERATION
)
1594 #ifdef RESIZE2FS_DEBUG
1595 if (rfs
->flags
& RESIZE_DEBUG_BMOVE
)
1596 printf("Going into desperation mode "
1597 "for block allocations\n");
1599 rfs
->alloc_state
= DESPERATION
;
1600 rfs
->new_blk
= fs
->super
->s_first_data_block
;
1603 if (ext2fs_test_block_bitmap2(fs
->block_map
, rfs
->new_blk
) ||
1604 ext2fs_test_block_bitmap2(rfs
->reserve_blocks
,
1606 ((rfs
->alloc_state
== AVOID_OLD
) &&
1607 (rfs
->new_blk
< ext2fs_blocks_count(rfs
->old_fs
->super
)) &&
1608 ext2fs_test_block_bitmap2(rfs
->old_fs
->block_map
,
1613 return rfs
->new_blk
;
1617 static errcode_t
resize2fs_get_alloc_block(ext2_filsys fs
, blk64_t goal
,
1620 ext2_resize_t rfs
= (ext2_resize_t
) fs
->priv_data
;
1623 blk
= get_new_block(rfs
);
1627 #ifdef RESIZE2FS_DEBUG
1628 if (rfs
->flags
& 0xF)
1629 printf("get_alloc_block allocating %llu\n", blk
);
1632 ext2fs_mark_block_bitmap2(rfs
->old_fs
->block_map
, blk
);
1633 ext2fs_mark_block_bitmap2(rfs
->new_fs
->block_map
, blk
);
1634 *ret
= (blk64_t
) blk
;
1638 static errcode_t
block_mover(ext2_resize_t rfs
)
1640 blk64_t blk
, old_blk
, new_blk
;
1641 ext2_filsys fs
= rfs
->new_fs
;
1642 ext2_filsys old_fs
= rfs
->old_fs
;
1647 ext2_badblocks_list badblock_list
= 0;
1648 int bb_modified
= 0;
1650 fs
->get_alloc_block
= resize2fs_get_alloc_block
;
1651 old_fs
->get_alloc_block
= resize2fs_get_alloc_block
;
1653 retval
= ext2fs_read_bb_inode(old_fs
, &badblock_list
);
1657 new_blk
= fs
->super
->s_first_data_block
;
1658 if (!rfs
->itable_buf
) {
1659 retval
= ext2fs_get_array(fs
->blocksize
,
1660 fs
->inode_blocks_per_group
,
1665 retval
= ext2fs_create_extent_table(&rfs
->bmap
, 0);
1670 * The first step is to figure out where all of the blocks
1673 to_move
= moved
= 0;
1674 init_block_alloc(rfs
);
1675 for (blk
= B2C(old_fs
->super
->s_first_data_block
);
1676 blk
< ext2fs_blocks_count(old_fs
->super
);
1677 blk
+= EXT2FS_CLUSTER_RATIO(fs
)) {
1678 if (!ext2fs_test_block_bitmap2(old_fs
->block_map
, blk
))
1680 if (!ext2fs_test_block_bitmap2(rfs
->move_blocks
, blk
))
1682 if (ext2fs_badblocks_list_test(badblock_list
, blk
)) {
1683 ext2fs_badblocks_list_del(badblock_list
, blk
);
1688 new_blk
= get_new_block(rfs
);
1693 ext2fs_block_alloc_stats2(fs
, new_blk
, +1);
1694 ext2fs_add_extent_entry(rfs
->bmap
, B2C(blk
), B2C(new_blk
));
1700 ext2fs_free_extent_table(rfs
->bmap
);
1708 * Step two is to actually move the blocks
1710 retval
= ext2fs_iterate_extent(rfs
->bmap
, 0, 0, 0);
1711 if (retval
) goto errout
;
1713 if (rfs
->progress
) {
1714 retval
= (rfs
->progress
)(rfs
, E2_RSZ_BLOCK_RELOC_PASS
,
1720 retval
= ext2fs_iterate_extent(rfs
->bmap
, &old_blk
, &new_blk
, &size
);
1721 if (retval
) goto errout
;
1724 old_blk
= C2B(old_blk
);
1725 new_blk
= C2B(new_blk
);
1727 #ifdef RESIZE2FS_DEBUG
1728 if (rfs
->flags
& RESIZE_DEBUG_BMOVE
)
1729 printf("Moving %llu blocks %llu->%llu\n",
1730 size
, old_blk
, new_blk
);
1734 if (c
> fs
->inode_blocks_per_group
)
1735 c
= fs
->inode_blocks_per_group
;
1736 retval
= io_channel_read_blk64(fs
->io
, old_blk
, c
,
1738 if (retval
) goto errout
;
1739 retval
= io_channel_write_blk64(fs
->io
, new_blk
, c
,
1741 if (retval
) goto errout
;
1746 if (rfs
->progress
) {
1747 io_channel_flush(fs
->io
);
1748 retval
= (rfs
->progress
)(rfs
,
1749 E2_RSZ_BLOCK_RELOC_PASS
,
1755 io_channel_flush(fs
->io
);
1759 if (badblock_list
) {
1760 if (!retval
&& bb_modified
)
1761 retval
= ext2fs_update_bb_inode(old_fs
,
1763 ext2fs_badblocks_list_free(badblock_list
);
1769 /* --------------------------------------------------------------------
1771 * Resize processing, phase 3
1773 * --------------------------------------------------------------------
1778 * The extent translation table is stored in clusters so we need to
1779 * take special care when mapping a source block number to its
1780 * destination block number.
1782 static __u64
extent_translate(ext2_filsys fs
, ext2_extent extent
, __u64 old_loc
)
1784 __u64 new_block
= C2B(ext2fs_extent_translate(extent
, B2C(old_loc
)));
1787 new_block
+= old_loc
& (EXT2FS_CLUSTER_RATIO(fs
) - 1);
1791 struct process_block_struct
{
1795 struct ext2_inode
* inode
;
1802 static int process_block(ext2_filsys fs
, blk64_t
*block_nr
,
1803 e2_blkcnt_t blockcnt
,
1804 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1805 int ref_offset
EXT2FS_ATTR((unused
)), void *priv_data
)
1807 struct process_block_struct
*pb
;
1809 blk64_t block
, new_block
;
1812 pb
= (struct process_block_struct
*) priv_data
;
1814 if (pb
->rfs
->bmap
) {
1815 new_block
= extent_translate(fs
, pb
->rfs
->bmap
, block
);
1817 *block_nr
= new_block
;
1818 ret
|= BLOCK_CHANGED
;
1820 #ifdef RESIZE2FS_DEBUG
1821 if (pb
->rfs
->flags
& RESIZE_DEBUG_BMOVE
)
1822 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1823 pb
->old_ino
, blockcnt
, block
,
1831 retval
= ext2fs_add_dir_block2(fs
->dblist
, pb
->ino
,
1832 block
, (int) blockcnt
);
1844 static errcode_t
progress_callback(ext2_filsys fs
,
1845 ext2_inode_scan scan
EXT2FS_ATTR((unused
)),
1846 dgrp_t group
, void * priv_data
)
1848 ext2_resize_t rfs
= (ext2_resize_t
) priv_data
;
1852 * This check is to protect against old ext2 libraries. It
1853 * shouldn't be needed against new libraries.
1858 if (rfs
->progress
) {
1859 io_channel_flush(fs
->io
);
1860 retval
= (rfs
->progress
)(rfs
, E2_RSZ_INODE_SCAN_PASS
,
1861 group
+1, fs
->group_desc_count
);
1869 static errcode_t
migrate_ea_block(ext2_resize_t rfs
, ext2_ino_t ino
,
1870 struct ext2_inode
*inode
, int *changed
)
1876 /* No EA block or no remapping? Quit early. */
1877 if (ext2fs_file_acl_block(rfs
->old_fs
, inode
) == 0 && !rfs
->bmap
)
1879 new_block
= extent_translate(rfs
->old_fs
, rfs
->bmap
,
1880 ext2fs_file_acl_block(rfs
->old_fs
, inode
));
1884 /* Set the new ACL block */
1885 ext2fs_file_acl_block_set(rfs
->old_fs
, inode
, new_block
);
1887 /* Update checksum */
1888 if (EXT2_HAS_RO_COMPAT_FEATURE(rfs
->new_fs
->super
,
1889 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
)) {
1890 err
= ext2fs_get_mem(rfs
->old_fs
->blocksize
, &buf
);
1893 rfs
->old_fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1894 err
= ext2fs_read_ext_attr3(rfs
->old_fs
, new_block
, buf
, ino
);
1895 rfs
->old_fs
->flags
&= ~EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1898 err
= ext2fs_write_ext_attr3(rfs
->old_fs
, new_block
, buf
, ino
);
1905 ext2fs_free_mem(&buf
);
1909 /* Rewrite extents */
1910 static errcode_t
rewrite_extents(ext2_filsys fs
, ext2_ino_t ino
)
1912 ext2_extent_handle_t handle
;
1913 struct ext2fs_extent extent
;
1915 struct ext2_extent_info info
;
1917 errcode
= ext2fs_extent_open(fs
, ino
, &handle
);
1921 errcode
= ext2fs_extent_get(handle
, EXT2_EXTENT_ROOT
, &extent
);
1926 errcode
= ext2fs_extent_get_info(handle
, &info
);
1931 * If this is the first extent in an extent block that we
1932 * haven't visited, rewrite the extent to force the ETB
1933 * checksum to be rewritten.
1935 if (info
.curr_entry
== 1 && info
.curr_level
!= 0 &&
1936 !(extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)) {
1937 errcode
= ext2fs_extent_replace(handle
, 0, &extent
);
1942 /* Skip to the end of a block of leaf nodes */
1943 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
) {
1944 errcode
= ext2fs_extent_get(handle
,
1945 EXT2_EXTENT_LAST_SIB
,
1951 errcode
= ext2fs_extent_get(handle
, EXT2_EXTENT_NEXT
, &extent
);
1952 } while (errcode
== 0);
1955 /* Ok if we run off the end */
1956 if (errcode
== EXT2_ET_EXTENT_NO_NEXT
)
1958 ext2fs_extent_free(handle
);
1962 static void quiet_com_err_proc(const char *whoami
, errcode_t code
,
1963 const char *fmt
, va_list args
)
1967 static errcode_t
inode_scan_and_fix(ext2_resize_t rfs
)
1969 struct process_block_struct pb
;
1970 ext2_ino_t ino
, new_inode
;
1971 struct ext2_inode
*inode
= NULL
;
1972 ext2_inode_scan scan
= NULL
;
1974 char *block_buf
= 0;
1975 ext2_ino_t start_to_move
;
1978 if ((rfs
->old_fs
->group_desc_count
<=
1979 rfs
->new_fs
->group_desc_count
) &&
1983 set_com_err_hook(quiet_com_err_proc
);
1985 retval
= ext2fs_open_inode_scan(rfs
->old_fs
, 0, &scan
);
1986 if (retval
) goto errout
;
1988 retval
= ext2fs_init_dblist(rfs
->old_fs
, 0);
1989 if (retval
) goto errout
;
1990 retval
= ext2fs_get_array(rfs
->old_fs
->blocksize
, 3, &block_buf
);
1991 if (retval
) goto errout
;
1993 start_to_move
= (rfs
->new_fs
->group_desc_count
*
1994 rfs
->new_fs
->super
->s_inodes_per_group
);
1996 if (rfs
->progress
) {
1997 retval
= (rfs
->progress
)(rfs
, E2_RSZ_INODE_SCAN_PASS
,
1998 0, rfs
->old_fs
->group_desc_count
);
2002 ext2fs_set_inode_callback(scan
, progress_callback
, (void *) rfs
);
2006 new_inode
= EXT2_FIRST_INODE(rfs
->new_fs
->super
);
2007 inode_size
= EXT2_INODE_SIZE(rfs
->new_fs
->super
);
2008 inode
= malloc(inode_size
);
2014 * First, copy all of the inodes that need to be moved
2015 * elsewhere in the inode table
2018 retval
= ext2fs_get_next_inode_full(scan
, &ino
, inode
, inode_size
);
2019 if (retval
) goto errout
;
2023 if (inode
->i_links_count
== 0 && ino
!= EXT2_RESIZE_INO
)
2024 continue; /* inode not in use */
2026 pb
.is_dir
= LINUX_S_ISDIR(inode
->i_mode
);
2029 /* Remap EA block */
2030 retval
= migrate_ea_block(rfs
, ino
, inode
, &pb
.changed
);
2035 if (ino
<= start_to_move
)
2036 goto remap_blocks
; /* Don't need to move inode. */
2039 * Find a new inode. Now that extents and directory blocks
2040 * are tied to the inode number through the checksum, we must
2041 * set up the new inode before we start rewriting blocks.
2043 retval
= ext2fs_new_inode(rfs
->new_fs
, 0, 0, 0, &new_inode
);
2047 ext2fs_inode_alloc_stats2(rfs
->new_fs
, new_inode
, +1,
2049 inode
->i_ctime
= time(0);
2050 retval
= ext2fs_write_inode_full(rfs
->old_fs
, new_inode
,
2056 #ifdef RESIZE2FS_DEBUG
2057 if (rfs
->flags
& RESIZE_DEBUG_INODEMAP
)
2058 printf("Inode moved %u->%u\n", ino
, new_inode
);
2061 retval
= ext2fs_create_extent_table(&rfs
->imap
, 0);
2065 ext2fs_add_extent_entry(rfs
->imap
, ino
, new_inode
);
2069 retval
= ext2fs_write_inode_full(rfs
->old_fs
,
2075 /* Rewrite extent block checksums with new inode number */
2076 if (EXT2_HAS_RO_COMPAT_FEATURE(rfs
->old_fs
->super
,
2077 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
) &&
2078 (inode
->i_flags
& EXT4_EXTENTS_FL
)) {
2079 rfs
->old_fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2080 retval
= rewrite_extents(rfs
->old_fs
, new_inode
);
2081 rfs
->old_fs
->flags
&= ~EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2087 * Update inodes to point to new blocks; schedule directory
2088 * blocks for inode remapping. Need to write out dir blocks
2089 * with new inode numbers if we have metadata_csum enabled.
2091 if (ext2fs_inode_has_valid_blocks2(rfs
->old_fs
, inode
) &&
2092 (rfs
->bmap
|| pb
.is_dir
)) {
2095 pb
.has_extents
= inode
->i_flags
& EXT4_EXTENTS_FL
;
2096 rfs
->old_fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2097 retval
= ext2fs_block_iterate3(rfs
->old_fs
,
2098 new_inode
, 0, block_buf
,
2099 process_block
, &pb
);
2100 rfs
->old_fs
->flags
&= ~EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2107 } else if ((inode
->i_flags
& EXT4_INLINE_DATA_FL
) &&
2108 (rfs
->bmap
|| pb
.is_dir
)) {
2109 /* inline data dir; update it too */
2110 retval
= ext2fs_add_dir_block2(rfs
->old_fs
->dblist
,
2116 io_channel_flush(rfs
->old_fs
->io
);
2119 reset_com_err_hook();
2121 ext2fs_free_extent_table(rfs
->bmap
);
2125 ext2fs_close_inode_scan(scan
);
2127 ext2fs_free_mem(&block_buf
);
2132 /* --------------------------------------------------------------------
2134 * Resize processing, phase 4.
2136 * --------------------------------------------------------------------
2142 unsigned int max_dirs
;
2146 static int check_and_change_inodes(ext2_ino_t dir
,
2147 int entry
EXT2FS_ATTR((unused
)),
2148 struct ext2_dir_entry
*dirent
, int offset
,
2149 int blocksize
EXT2FS_ATTR((unused
)),
2150 char *buf
EXT2FS_ATTR((unused
)),
2153 struct istruct
*is
= (struct istruct
*) priv_data
;
2154 struct ext2_inode inode
;
2155 ext2_ino_t new_inode
;
2159 if (is
->rfs
->progress
&& offset
== 0) {
2160 io_channel_flush(is
->rfs
->old_fs
->io
);
2161 is
->err
= (is
->rfs
->progress
)(is
->rfs
,
2162 E2_RSZ_INODE_REF_UPD_PASS
,
2163 ++is
->num
, is
->max_dirs
);
2165 return DIRENT_ABORT
;
2169 * If we have checksums enabled and the inode wasn't present in the
2170 * old fs, then we must rewrite all dir blocks with new checksums.
2172 if (EXT2_HAS_RO_COMPAT_FEATURE(is
->rfs
->old_fs
->super
,
2173 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
) &&
2174 !ext2fs_test_inode_bitmap2(is
->rfs
->old_fs
->inode_map
, dir
))
2175 ret
|= DIRENT_CHANGED
;
2180 new_inode
= ext2fs_extent_translate(is
->rfs
->imap
, dirent
->inode
);
2184 #ifdef RESIZE2FS_DEBUG
2185 if (is
->rfs
->flags
& RESIZE_DEBUG_INODEMAP
)
2186 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
2187 dir
, ext2fs_dirent_name_len(dirent
), dirent
->name
,
2188 dirent
->inode
, new_inode
);
2191 dirent
->inode
= new_inode
;
2193 /* Update the directory mtime and ctime */
2194 retval
= ext2fs_read_inode(is
->rfs
->old_fs
, dir
, &inode
);
2196 inode
.i_mtime
= inode
.i_ctime
= time(0);
2197 is
->err
= ext2fs_write_inode(is
->rfs
->old_fs
, dir
, &inode
);
2199 return ret
| DIRENT_ABORT
;
2202 return ret
| DIRENT_CHANGED
;
2205 static errcode_t
inode_ref_fix(ext2_resize_t rfs
)
2214 * Now, we iterate over all of the directories to update the
2218 is
.max_dirs
= ext2fs_dblist_count2(rfs
->old_fs
->dblist
);
2222 if (rfs
->progress
) {
2223 retval
= (rfs
->progress
)(rfs
, E2_RSZ_INODE_REF_UPD_PASS
,
2229 rfs
->old_fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2230 retval
= ext2fs_dblist_dir_iterate(rfs
->old_fs
->dblist
,
2231 DIRENT_FLAG_INCLUDE_EMPTY
, 0,
2232 check_and_change_inodes
, &is
);
2233 rfs
->old_fs
->flags
&= ~EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2241 if (rfs
->progress
&& (is
.num
< is
.max_dirs
))
2242 (rfs
->progress
)(rfs
, E2_RSZ_INODE_REF_UPD_PASS
,
2243 is
.max_dirs
, is
.max_dirs
);
2246 ext2fs_free_extent_table(rfs
->imap
);
2252 /* --------------------------------------------------------------------
2254 * Resize processing, phase 5.
2256 * In this phase we actually move the inode table around, and then
2257 * update the summary statistics. This is scary, since aborting here
2258 * will potentially scramble the filesystem. (We are moving the
2259 * inode tables around in place, and so the potential for lost data,
2260 * or at the very least scrambling the mapping between filenames and
2261 * inode numbers is very high in case of a power failure here.)
2262 * --------------------------------------------------------------------
2267 * A very scary routine --- this one moves the inode table around!!!
2269 * After this you have to use the rfs->new_fs file handle to read and
2272 static errcode_t
move_itables(ext2_resize_t rfs
)
2276 dgrp_t i
, max_groups
;
2277 ext2_filsys fs
= rfs
->new_fs
;
2279 blk64_t old_blk
, new_blk
, blk
, cluster_freed
;
2281 int j
, to_move
, moved
;
2282 ext2fs_block_bitmap new_bmap
= NULL
;
2284 max_groups
= fs
->group_desc_count
;
2285 if (max_groups
> rfs
->old_fs
->group_desc_count
)
2286 max_groups
= rfs
->old_fs
->group_desc_count
;
2288 size
= fs
->blocksize
* fs
->inode_blocks_per_group
;
2289 if (!rfs
->itable_buf
) {
2290 retval
= ext2fs_get_mem(size
, &rfs
->itable_buf
);
2295 if (EXT2FS_CLUSTER_RATIO(fs
) > 1) {
2296 retval
= ext2fs_allocate_block_bitmap(fs
, _("new meta blocks"),
2301 retval
= mark_table_blocks(fs
, new_bmap
);
2307 * Figure out how many inode tables we need to move
2309 to_move
= moved
= 0;
2310 for (i
=0; i
< max_groups
; i
++)
2311 if (ext2fs_inode_table_loc(rfs
->old_fs
, i
) !=
2312 ext2fs_inode_table_loc(fs
, i
))
2320 if (rfs
->progress
) {
2321 retval
= rfs
->progress(rfs
, E2_RSZ_MOVE_ITABLE_PASS
,
2327 rfs
->old_fs
->flags
|= EXT2_FLAG_MASTER_SB_ONLY
;
2329 for (i
=0; i
< max_groups
; i
++) {
2330 old_blk
= ext2fs_inode_table_loc(rfs
->old_fs
, i
);
2331 new_blk
= ext2fs_inode_table_loc(fs
, i
);
2332 diff
= new_blk
- old_blk
;
2334 #ifdef RESIZE2FS_DEBUG
2335 if (rfs
->flags
& RESIZE_DEBUG_ITABLEMOVE
)
2336 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
2337 i
, old_blk
, new_blk
, diff
);
2345 retval
= io_channel_read_blk64(fs
->io
, old_blk
,
2346 fs
->inode_blocks_per_group
,
2351 * The end of the inode table segment often contains
2352 * all zeros, and we're often only moving the inode
2353 * table down a block or two. If so, we can optimize
2354 * things by not rewriting blocks that we know to be zero
2357 for (cp
= rfs
->itable_buf
+size
-1, n
=0; n
< size
; n
++, cp
--)
2360 n
= n
>> EXT2_BLOCK_SIZE_BITS(fs
->super
);
2361 #ifdef RESIZE2FS_DEBUG
2362 if (rfs
->flags
& RESIZE_DEBUG_ITABLEMOVE
)
2363 printf("%d blocks of zeros...\n", n
);
2365 num
= fs
->inode_blocks_per_group
;
2369 retval
= io_channel_write_blk64(fs
->io
, new_blk
,
2370 num
, rfs
->itable_buf
);
2372 io_channel_write_blk64(fs
->io
, old_blk
,
2373 num
, rfs
->itable_buf
);
2377 retval
= io_channel_write_blk64(fs
->io
,
2378 old_blk
+ fs
->inode_blocks_per_group
,
2379 diff
, (rfs
->itable_buf
+
2380 (fs
->inode_blocks_per_group
- diff
) *
2386 for (blk
= ext2fs_inode_table_loc(rfs
->old_fs
, i
), j
=0;
2387 j
< fs
->inode_blocks_per_group
;) {
2388 if (new_bmap
== NULL
||
2389 !ext2fs_test_block_bitmap2(new_bmap
, blk
)) {
2390 ext2fs_block_alloc_stats2(fs
, blk
, -1);
2391 cluster_freed
= EXT2FS_CLUSTER_RATIO(fs
) -
2392 (blk
& EXT2FS_CLUSTER_MASK(fs
));
2393 blk
+= cluster_freed
;
2401 ext2fs_inode_table_loc_set(rfs
->old_fs
, i
, new_blk
);
2402 ext2fs_group_desc_csum_set(rfs
->old_fs
, i
);
2403 ext2fs_mark_super_dirty(rfs
->old_fs
);
2404 ext2fs_flush(rfs
->old_fs
);
2406 if (rfs
->progress
) {
2407 retval
= rfs
->progress(rfs
, E2_RSZ_MOVE_ITABLE_PASS
,
2413 mark_table_blocks(fs
, fs
->block_map
);
2415 #ifdef RESIZE2FS_DEBUG
2416 if (rfs
->flags
& RESIZE_DEBUG_ITABLEMOVE
)
2417 printf("Inode table move finished.\n");
2423 ext2fs_free_block_bitmap(new_bmap
);
2428 * This function is used when expanding a file system. It frees the
2429 * superblock and block group descriptor blocks from the block group
2430 * which is no longer the last block group.
2432 static errcode_t
clear_sparse_super2_last_group(ext2_resize_t rfs
)
2434 ext2_filsys fs
= rfs
->new_fs
;
2435 ext2_filsys old_fs
= rfs
->old_fs
;
2437 dgrp_t old_last_bg
= rfs
->old_fs
->group_desc_count
- 1;
2438 dgrp_t last_bg
= fs
->group_desc_count
- 1;
2439 blk64_t sb
, old_desc
;
2442 if (!(fs
->super
->s_feature_compat
& EXT4_FEATURE_COMPAT_SPARSE_SUPER2
))
2445 if (last_bg
<= old_last_bg
)
2448 if (fs
->super
->s_backup_bgs
[0] == old_fs
->super
->s_backup_bgs
[0] &&
2449 fs
->super
->s_backup_bgs
[1] == old_fs
->super
->s_backup_bgs
[1])
2452 if (old_fs
->super
->s_backup_bgs
[0] != old_last_bg
&&
2453 old_fs
->super
->s_backup_bgs
[1] != old_last_bg
)
2456 if (fs
->super
->s_backup_bgs
[0] == old_last_bg
||
2457 fs
->super
->s_backup_bgs
[1] == old_last_bg
)
2460 retval
= ext2fs_super_and_bgd_loc2(rfs
->old_fs
, old_last_bg
,
2461 &sb
, &old_desc
, NULL
, &num
);
2466 ext2fs_unmark_block_bitmap2(fs
->block_map
, sb
);
2468 ext2fs_unmark_block_bitmap_range2(fs
->block_map
, old_desc
, num
);
2473 * This function is used when shrinking a file system. We need to
2474 * utilize blocks from what will be the new last block group for the
2475 * backup superblock and block group descriptor blocks.
2476 * Unfortunately, those blocks may be used by other files or fs
2477 * metadata blocks. We need to mark them as being in use.
2479 static errcode_t
reserve_sparse_super2_last_group(ext2_resize_t rfs
,
2480 ext2fs_block_bitmap meta_bmap
)
2482 ext2_filsys fs
= rfs
->new_fs
;
2483 ext2_filsys old_fs
= rfs
->old_fs
;
2485 dgrp_t old_last_bg
= rfs
->old_fs
->group_desc_count
- 1;
2486 dgrp_t last_bg
= fs
->group_desc_count
- 1;
2488 blk64_t blk
, sb
, old_desc
;
2492 if (!(fs
->super
->s_feature_compat
& EXT4_FEATURE_COMPAT_SPARSE_SUPER2
))
2495 if (last_bg
>= old_last_bg
)
2498 if (fs
->super
->s_backup_bgs
[0] == old_fs
->super
->s_backup_bgs
[0] &&
2499 fs
->super
->s_backup_bgs
[1] == old_fs
->super
->s_backup_bgs
[1])
2502 if (fs
->super
->s_backup_bgs
[0] != last_bg
&&
2503 fs
->super
->s_backup_bgs
[1] != last_bg
)
2506 if (old_fs
->super
->s_backup_bgs
[0] == last_bg
||
2507 old_fs
->super
->s_backup_bgs
[1] == last_bg
)
2510 retval
= ext2fs_super_and_bgd_loc2(rfs
->new_fs
, last_bg
,
2511 &sb
, &old_desc
, NULL
, &num
);
2515 if (last_bg
&& !sb
) {
2516 fputs(_("Should never happen! No sb in last super_sparse bg?\n"),
2520 if (old_desc
&& old_desc
!= sb
+1) {
2521 fputs(_("Should never happen! Unexpected old_desc in "
2522 "super_sparse bg?\n"),
2526 num
= (old_desc
) ? num
: 1;
2528 /* Reserve the backup blocks */
2529 ext2fs_mark_block_bitmap_range2(fs
->block_map
, sb
, num
);
2531 for (g
= 0; g
< fs
->group_desc_count
; g
++) {
2534 mb
= ext2fs_block_bitmap_loc(fs
, g
);
2535 if ((mb
>= sb
) && (mb
< sb
+ num
)) {
2536 ext2fs_block_bitmap_loc_set(fs
, g
, 0);
2539 mb
= ext2fs_inode_bitmap_loc(fs
, g
);
2540 if ((mb
>= sb
) && (mb
< sb
+ num
)) {
2541 ext2fs_inode_bitmap_loc_set(fs
, g
, 0);
2544 mb
= ext2fs_inode_table_loc(fs
, g
);
2545 if ((mb
< sb
+ num
) &&
2546 (sb
< mb
+ fs
->inode_blocks_per_group
)) {
2547 ext2fs_inode_table_loc_set(fs
, g
, 0);
2551 retval
= ext2fs_allocate_group_table(fs
, g
, 0);
2557 for (blk
= sb
, i
= 0; i
< num
; blk
++, i
++) {
2558 if (ext2fs_test_block_bitmap2(old_fs
->block_map
, blk
) &&
2559 !ext2fs_test_block_bitmap2(meta_bmap
, blk
)) {
2560 ext2fs_mark_block_bitmap2(rfs
->move_blocks
, blk
);
2561 rfs
->needed_blocks
++;
2563 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
, blk
);
2569 * Fix the resize inode
2571 static errcode_t
fix_resize_inode(ext2_filsys fs
)
2573 struct ext2_inode inode
;
2576 if (!(fs
->super
->s_feature_compat
&
2577 EXT2_FEATURE_COMPAT_RESIZE_INODE
))
2580 retval
= ext2fs_read_inode(fs
, EXT2_RESIZE_INO
, &inode
);
2581 if (retval
) goto errout
;
2583 ext2fs_iblk_set(fs
, &inode
, 1);
2585 retval
= ext2fs_write_inode(fs
, EXT2_RESIZE_INO
, &inode
);
2586 if (retval
) goto errout
;
2588 if (!inode
.i_block
[EXT2_DIND_BLOCK
]) {
2590 * Avoid zeroing out block #0; that's rude. This
2591 * should never happen anyway since the filesystem
2592 * should be fsck'ed and we assume it is consistent.
2594 fprintf(stderr
, "%s",
2595 _("Should never happen: resize inode corrupt!\n"));
2599 retval
= ext2fs_zero_blocks2(fs
, inode
.i_block
[EXT2_DIND_BLOCK
], 1,
2604 retval
= ext2fs_create_resize_inode(fs
);
2613 * Finally, recalculate the summary information
2615 static errcode_t
ext2fs_calculate_summary_stats(ext2_filsys fs
)
2619 unsigned int group
= 0;
2620 unsigned int count
= 0;
2621 blk64_t total_blocks_free
= 0;
2622 int total_inodes_free
= 0;
2625 blk64_t super_blk
, old_desc_blk
, new_desc_blk
;
2626 int old_desc_blocks
;
2629 * First calculate the block statistics
2631 uninit
= ext2fs_bg_flags_test(fs
, group
, EXT2_BG_BLOCK_UNINIT
);
2632 ext2fs_super_and_bgd_loc2(fs
, group
, &super_blk
, &old_desc_blk
,
2634 if (fs
->super
->s_feature_incompat
& EXT2_FEATURE_INCOMPAT_META_BG
)
2635 old_desc_blocks
= fs
->super
->s_first_meta_bg
;
2637 old_desc_blocks
= fs
->desc_blocks
+
2638 fs
->super
->s_reserved_gdt_blocks
;
2639 for (blk
= B2C(fs
->super
->s_first_data_block
);
2640 blk
< ext2fs_blocks_count(fs
->super
);
2641 blk
+= EXT2FS_CLUSTER_RATIO(fs
)) {
2643 !(EQ_CLSTR(blk
, super_blk
) ||
2644 ((old_desc_blk
&& old_desc_blocks
&&
2645 GE_CLSTR(blk
, old_desc_blk
) &&
2646 LT_CLSTR(blk
, old_desc_blk
+ old_desc_blocks
))) ||
2647 ((new_desc_blk
&& EQ_CLSTR(blk
, new_desc_blk
))) ||
2648 EQ_CLSTR(blk
, ext2fs_block_bitmap_loc(fs
, group
)) ||
2649 EQ_CLSTR(blk
, ext2fs_inode_bitmap_loc(fs
, group
)) ||
2650 ((GE_CLSTR(blk
, ext2fs_inode_table_loc(fs
, group
)) &&
2651 LT_CLSTR(blk
, ext2fs_inode_table_loc(fs
, group
)
2652 + fs
->inode_blocks_per_group
))))) ||
2653 (!ext2fs_fast_test_block_bitmap2(fs
->block_map
, blk
))) {
2655 total_blocks_free
++;
2658 if ((count
== fs
->super
->s_clusters_per_group
) ||
2659 EQ_CLSTR(blk
, ext2fs_blocks_count(fs
->super
)-1)) {
2660 ext2fs_bg_free_blocks_count_set(fs
, group
, group_free
);
2661 ext2fs_group_desc_csum_set(fs
, group
);
2663 if (group
>= fs
->group_desc_count
)
2667 uninit
= ext2fs_bg_flags_test(fs
, group
, EXT2_BG_BLOCK_UNINIT
);
2668 ext2fs_super_and_bgd_loc2(fs
, group
, &super_blk
,
2671 if (fs
->super
->s_feature_incompat
&
2672 EXT2_FEATURE_INCOMPAT_META_BG
)
2673 old_desc_blocks
= fs
->super
->s_first_meta_bg
;
2675 old_desc_blocks
= fs
->desc_blocks
+
2676 fs
->super
->s_reserved_gdt_blocks
;
2679 total_blocks_free
= C2B(total_blocks_free
);
2680 ext2fs_free_blocks_count_set(fs
->super
, total_blocks_free
);
2683 * Next, calculate the inode statistics
2689 /* Protect loop from wrap-around if s_inodes_count maxed */
2690 uninit
= ext2fs_bg_flags_test(fs
, group
, EXT2_BG_INODE_UNINIT
);
2691 for (ino
= 1; ino
<= fs
->super
->s_inodes_count
&& ino
> 0; ino
++) {
2693 !ext2fs_fast_test_inode_bitmap2(fs
->inode_map
, ino
)) {
2695 total_inodes_free
++;
2698 if ((count
== fs
->super
->s_inodes_per_group
) ||
2699 (ino
== fs
->super
->s_inodes_count
)) {
2700 ext2fs_bg_free_inodes_count_set(fs
, group
, group_free
);
2701 ext2fs_group_desc_csum_set(fs
, group
);
2703 if (group
>= fs
->group_desc_count
)
2707 uninit
= ext2fs_bg_flags_test(fs
, group
, EXT2_BG_INODE_UNINIT
);
2710 fs
->super
->s_free_inodes_count
= total_inodes_free
;
2711 ext2fs_mark_super_dirty(fs
);
2716 * Journal may have been relocated; update the backup journal blocks
2717 * in the superblock.
2719 static errcode_t
fix_sb_journal_backup(ext2_filsys fs
)
2722 struct ext2_inode inode
;
2724 if (!(fs
->super
->s_feature_compat
& EXT3_FEATURE_COMPAT_HAS_JOURNAL
))
2727 /* External journal? Nothing to do. */
2728 if (fs
->super
->s_journal_dev
&& !fs
->super
->s_journal_inum
)
2731 retval
= ext2fs_read_inode(fs
, fs
->super
->s_journal_inum
, &inode
);
2734 memcpy(fs
->super
->s_jnl_blocks
, inode
.i_block
, EXT2_N_BLOCKS
*4);
2735 fs
->super
->s_jnl_blocks
[15] = inode
.i_size_high
;
2736 fs
->super
->s_jnl_blocks
[16] = inode
.i_size
;
2737 fs
->super
->s_jnl_backup_type
= EXT3_JNL_BACKUP_BLOCKS
;
2738 ext2fs_mark_super_dirty(fs
);
2742 static int calc_group_overhead(ext2_filsys fs
, blk64_t grp
,
2743 int old_desc_blocks
)
2745 blk64_t super_blk
, old_desc_blk
, new_desc_blk
;
2748 /* inode table blocks plus allocation bitmaps */
2749 overhead
= fs
->inode_blocks_per_group
+ 2;
2751 ext2fs_super_and_bgd_loc2(fs
, grp
, &super_blk
,
2752 &old_desc_blk
, &new_desc_blk
, 0);
2753 if ((grp
== 0) || super_blk
)
2756 overhead
+= old_desc_blocks
;
2757 else if (new_desc_blk
)
2764 * calcluate the minimum number of blocks the given fs can be resized to
2766 blk64_t
calculate_minimum_resize_size(ext2_filsys fs
, int flags
)
2768 ext2_ino_t inode_count
;
2769 dgrp_t groups
, flex_groups
;
2770 blk64_t blks_needed
, data_blocks
;
2771 blk64_t grp
, data_needed
, last_start
;
2772 blk64_t overhead
= 0;
2773 int old_desc_blocks
;
2774 int flexbg_size
= 1 << fs
->super
->s_log_groups_per_flex
;
2777 * first figure out how many group descriptors we need to
2778 * handle the number of inodes we have
2780 inode_count
= fs
->super
->s_inodes_count
-
2781 fs
->super
->s_free_inodes_count
;
2782 blks_needed
= ext2fs_div_ceil(inode_count
,
2783 fs
->super
->s_inodes_per_group
) *
2784 (blk64_t
) EXT2_BLOCKS_PER_GROUP(fs
->super
);
2785 groups
= ext2fs_div64_ceil(blks_needed
,
2786 EXT2_BLOCKS_PER_GROUP(fs
->super
));
2787 #ifdef RESIZE2FS_DEBUG
2788 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2789 printf("fs has %d inodes, %d groups required.\n",
2790 inode_count
, groups
);
2794 * number of old-style block group descriptor blocks
2796 if (fs
->super
->s_feature_incompat
& EXT2_FEATURE_INCOMPAT_META_BG
)
2797 old_desc_blocks
= fs
->super
->s_first_meta_bg
;
2799 old_desc_blocks
= fs
->desc_blocks
+
2800 fs
->super
->s_reserved_gdt_blocks
;
2802 /* calculate how many blocks are needed for data */
2803 data_needed
= ext2fs_blocks_count(fs
->super
) -
2804 ext2fs_free_blocks_count(fs
->super
);
2806 for (grp
= 0; grp
< fs
->group_desc_count
; grp
++)
2807 data_needed
-= calc_group_overhead(fs
, grp
, old_desc_blocks
);
2808 #ifdef RESIZE2FS_DEBUG
2809 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2810 printf("fs requires %llu data blocks.\n", data_needed
);
2814 * For ext4 we need to allow for up to a flex_bg worth of
2815 * inode tables of slack space so the resize operation can be
2816 * guaranteed to finish.
2818 flex_groups
= groups
;
2819 if (fs
->super
->s_feature_incompat
& EXT4_FEATURE_INCOMPAT_FLEX_BG
) {
2820 dgrp_t remainder
= groups
& (flexbg_size
- 1);
2822 flex_groups
+= flexbg_size
- remainder
;
2823 if (flex_groups
> fs
->group_desc_count
)
2824 flex_groups
= fs
->group_desc_count
;
2828 * figure out how many data blocks we have given the number of groups
2829 * we need for our inodes
2831 data_blocks
= EXT2_GROUPS_TO_BLOCKS(fs
->super
, groups
);
2833 for (grp
= 0; grp
< flex_groups
; grp
++) {
2834 overhead
= calc_group_overhead(fs
, grp
, old_desc_blocks
);
2837 * we want to keep track of how much data we can store in
2838 * the groups leading up to the last group so we can determine
2839 * how big the last group needs to be
2841 if (grp
< (groups
- 1))
2842 last_start
+= EXT2_BLOCKS_PER_GROUP(fs
->super
) -
2845 if (data_blocks
> overhead
)
2846 data_blocks
-= overhead
;
2850 #ifdef RESIZE2FS_DEBUG
2851 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2852 printf("With %d group(s), we have %llu blocks available.\n",
2853 groups
, data_blocks
);
2857 * if we need more group descriptors in order to accomodate our data
2858 * then we need to add them here
2860 blks_needed
= data_needed
;
2861 while (blks_needed
> data_blocks
) {
2862 blk64_t remainder
= blks_needed
- data_blocks
;
2865 /* figure out how many more groups we need for the data */
2866 extra_grps
= ext2fs_div64_ceil(remainder
,
2867 EXT2_BLOCKS_PER_GROUP(fs
->super
));
2869 data_blocks
+= EXT2_GROUPS_TO_BLOCKS(fs
->super
, extra_grps
);
2871 /* ok we have to account for the last group */
2872 overhead
= calc_group_overhead(fs
, groups
-1, old_desc_blocks
);
2873 last_start
+= EXT2_BLOCKS_PER_GROUP(fs
->super
) - overhead
;
2876 groups
+= extra_grps
;
2877 if (!(fs
->super
->s_feature_incompat
&
2878 EXT4_FEATURE_INCOMPAT_FLEX_BG
))
2879 flex_groups
= groups
;
2880 else if (groups
> flex_groups
) {
2881 dgrp_t r
= groups
& (flexbg_size
- 1);
2883 flex_groups
= groups
+ flexbg_size
- r
;
2884 if (flex_groups
> fs
->group_desc_count
)
2885 flex_groups
= fs
->group_desc_count
;
2888 for (; grp
< flex_groups
; grp
++) {
2889 overhead
= calc_group_overhead(fs
, grp
,
2893 * again, we need to see how much data we cram into
2894 * all of the groups leading up to the last group
2896 if (grp
< groups
- 1)
2897 last_start
+= EXT2_BLOCKS_PER_GROUP(fs
->super
)
2900 data_blocks
-= overhead
;
2903 #ifdef RESIZE2FS_DEBUG
2904 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2905 printf("Added %d extra group(s), "
2906 "blks_needed %llu, data_blocks %llu, "
2907 "last_start %llu\n", extra_grps
, blks_needed
,
2908 data_blocks
, last_start
);
2912 /* now for the fun voodoo */
2914 if ((fs
->super
->s_feature_incompat
& EXT4_FEATURE_INCOMPAT_FLEX_BG
) &&
2915 (grp
& ~(flexbg_size
- 1)) == 0)
2916 grp
= grp
& ~(flexbg_size
- 1);
2918 for (; grp
< flex_groups
; grp
++)
2919 overhead
+= calc_group_overhead(fs
, grp
, old_desc_blocks
);
2921 #ifdef RESIZE2FS_DEBUG
2922 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2923 printf("Last group's overhead is %llu\n", overhead
);
2927 * if this is the case then the last group is going to have data in it
2928 * so we need to adjust the size of the last group accordingly
2930 if (last_start
< blks_needed
) {
2931 blk64_t remainder
= blks_needed
- last_start
;
2933 #ifdef RESIZE2FS_DEBUG
2934 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2935 printf("Need %llu data blocks in last group\n",
2939 * 50 is a magic number that mkfs/resize uses to see if its
2940 * even worth making/resizing the fs. basically you need to
2941 * have at least 50 blocks in addition to the blocks needed
2942 * for the metadata in the last group
2945 overhead
+= remainder
;
2951 overhead
+= fs
->super
->s_first_data_block
;
2952 #ifdef RESIZE2FS_DEBUG
2953 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2954 printf("Final size of last group is %lld\n", overhead
);
2957 /* Add extra slack for bigalloc file systems */
2958 if (EXT2FS_CLUSTER_RATIO(fs
) > 1)
2959 overhead
+= EXT2FS_CLUSTER_RATIO(fs
) * 2;
2962 * since our last group doesn't have to be BLOCKS_PER_GROUP
2963 * large, we only do groups-1, and then add the number of
2964 * blocks needed to handle the group descriptor metadata+data
2967 blks_needed
= EXT2_GROUPS_TO_BLOCKS(fs
->super
, groups
- 1);
2968 blks_needed
+= overhead
;
2971 * Make sure blks_needed covers the end of the inode table in
2972 * the last block group.
2974 overhead
= ext2fs_inode_table_loc(fs
, groups
-1) +
2975 fs
->inode_blocks_per_group
;
2976 if (blks_needed
< overhead
)
2977 blks_needed
= overhead
;
2979 #ifdef RESIZE2FS_DEBUG
2980 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2981 printf("Estimated blocks needed: %llu\n", blks_needed
);
2985 * If at this point we've already added up more "needed" than
2986 * the current size, just return current size as minimum.
2988 if (blks_needed
>= ext2fs_blocks_count(fs
->super
))
2989 return ext2fs_blocks_count(fs
->super
);
2991 * We need to reserve a few extra blocks if extents are
2992 * enabled, in case we need to grow the extent tree. The more
2993 * we shrink the file system, the more space we need.
2995 * The absolute worst case is every single data block is in
2996 * the part of the file system that needs to be evacuated,
2997 * with each data block needs to be in its own extent, and
2998 * with each inode needing at least one extent block.
3000 if (fs
->super
->s_feature_incompat
& EXT3_FEATURE_INCOMPAT_EXTENTS
) {
3001 blk64_t safe_margin
= (ext2fs_blocks_count(fs
->super
) -
3003 unsigned int exts_per_blk
= (fs
->blocksize
/
3004 sizeof(struct ext3_extent
)) - 1;
3005 blk64_t worst_case
= ((data_needed
+ exts_per_blk
- 1) /
3008 if (worst_case
< inode_count
)
3009 worst_case
= inode_count
;
3011 if (safe_margin
> worst_case
)
3012 safe_margin
= worst_case
;
3014 #ifdef RESIZE2FS_DEBUG
3015 if (flags
& RESIZE_DEBUG_MIN_CALC
)
3016 printf("Extents safety margin: %llu\n", safe_margin
);
3018 blks_needed
+= safe_margin
;