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.
36 #include "resize2fs.h"
39 #ifdef __linux__ /* Kludge for debugging */
40 #define RESIZE2FS_DEBUG
43 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size);
44 static errcode_t blocks_to_move(ext2_resize_t rfs);
45 static errcode_t block_mover(ext2_resize_t rfs);
46 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
47 static errcode_t inode_ref_fix(ext2_resize_t rfs);
48 static errcode_t move_itables(ext2_resize_t rfs);
49 static errcode_t fix_resize_inode(ext2_filsys fs);
50 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
53 * Some helper CPP macros
55 #define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap)
56 #define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap)
57 #define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table)
59 #define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i)))
60 #define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i)))
62 #define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \
63 ((blk) < (FS_INODE_TB((fs), (i)) + \
64 (fs)->inode_blocks_per_group)))
69 * This is the top-level routine which does the dirty deed....
71 errcode_t resize_fs(ext2_filsys fs, blk_t *new_size, int flags,
72 errcode_t (*progress)(ext2_resize_t rfs, int pass,
74 unsigned long max_val))
79 retval = ext2fs_read_bitmaps(fs);
84 * Create the data structure
86 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
89 memset(rfs, 0, sizeof(struct ext2_resize_struct));
94 rfs->progress = progress;
95 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
99 retval = adjust_superblock(rfs, *new_size);
103 *new_size = rfs->new_fs->super->s_blocks_count;
105 retval = blocks_to_move(rfs);
109 #ifdef RESIZE2FS_DEBUG
110 if (rfs->flags & RESIZE_DEBUG_BMOVE)
111 printf("Number of free blocks: %u/%u, Needed: %d\n",
112 rfs->old_fs->super->s_free_blocks_count,
113 rfs->new_fs->super->s_free_blocks_count,
117 retval = block_mover(rfs);
121 retval = inode_scan_and_fix(rfs);
125 retval = inode_ref_fix(rfs);
129 retval = move_itables(rfs);
133 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
137 retval = fix_resize_inode(rfs->new_fs);
141 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
142 retval = ext2fs_close(rfs->new_fs);
148 ext2fs_free(rfs->old_fs);
150 ext2fs_free_mem(&rfs->itable_buf);
151 ext2fs_free_mem(&rfs);
157 ext2fs_free(rfs->new_fs);
159 ext2fs_free_mem(&rfs->itable_buf);
160 ext2fs_free_mem(&rfs);
164 /* --------------------------------------------------------------------
166 * Resize processing, phase 1.
168 * In this phase we adjust the in-memory superblock information, and
169 * initialize any new parts of the inode table. The new parts of the
170 * inode table are created in virgin disk space, so we can abort here
171 * without any side effects.
172 * --------------------------------------------------------------------
176 * This routine is shared by the online and offline resize routines.
177 * All of the information which is adjusted in memory is done here.
179 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs, blk_t new_size)
184 blk_t blk, group_block;
186 int adj, old_numblocks, numblocks, adjblocks;
187 unsigned long i, j, old_desc_blocks, max_group;
188 unsigned int meta_bg, meta_bg_size;
190 unsigned long long new_inodes; /* u64 to check for overflow */
192 fs->super->s_blocks_count = new_size;
195 fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
196 fs->super->s_first_data_block,
197 EXT2_BLOCKS_PER_GROUP(fs->super));
198 if (fs->group_desc_count == 0)
199 return EXT2_ET_TOOSMALL;
200 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
201 EXT2_DESC_PER_BLOCK(fs->super));
204 * Overhead is the number of bookkeeping blocks per group. It
205 * includes the superblock backup, the group descriptor
206 * backups, the inode bitmap, the block bitmap, and the inode
209 overhead = (int) (2 + fs->inode_blocks_per_group);
211 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
212 overhead += 1 + fs->desc_blocks +
213 fs->super->s_reserved_gdt_blocks;
216 * See if the last group is big enough to support the
217 * necessary data structures. If not, we need to get rid of
220 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
221 fs->super->s_blocks_per_group;
222 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
223 return EXT2_ET_TOOSMALL;
224 if (rem && (rem < overhead+50)) {
225 fs->super->s_blocks_count -= rem;
229 * Adjust the number of inodes
231 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
232 if (new_inodes > ~0U) {
233 fprintf(stderr, _("inodes (%llu) must be less than %u"),
235 return EXT2_ET_TOO_MANY_INODES;
237 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
238 fs->group_desc_count;
241 * Adjust the number of free blocks
243 blk = old_fs->super->s_blocks_count;
244 if (blk > fs->super->s_blocks_count)
245 fs->super->s_free_blocks_count -=
246 (blk - fs->super->s_blocks_count);
248 fs->super->s_free_blocks_count +=
249 (fs->super->s_blocks_count - blk);
252 * Adjust the number of reserved blocks
254 blk = (__u64)old_fs->super->s_r_blocks_count * 100 /
255 old_fs->super->s_blocks_count;
256 fs->super->s_r_blocks_count = e2p_percent(blk,
257 fs->super->s_blocks_count);
260 * Adjust the bitmaps for size
262 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
263 fs->super->s_inodes_count,
265 if (retval) goto errout;
267 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
268 * fs->group_desc_count)) - 1 +
269 fs->super->s_first_data_block;
270 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
271 real_end, fs->block_map);
273 if (retval) goto errout;
276 * Reallocate the group descriptors as necessary.
278 if (old_fs->desc_blocks != fs->desc_blocks) {
279 retval = ext2fs_resize_mem(old_fs->desc_blocks *
281 fs->desc_blocks * fs->blocksize,
285 if (fs->desc_blocks > old_fs->desc_blocks)
286 memset((char *) fs->group_desc +
287 (old_fs->desc_blocks * fs->blocksize), 0,
288 (fs->desc_blocks - old_fs->desc_blocks) *
293 * If the resize_inode feature is set, and we are changing the
294 * number of descriptor blocks, then adjust
295 * s_reserved_gdt_blocks if possible to avoid needing to move
296 * the inode table either now or in the future.
298 if ((fs->super->s_feature_compat &
299 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
300 (old_fs->desc_blocks != fs->desc_blocks)) {
303 new = ((int) fs->super->s_reserved_gdt_blocks) +
304 (old_fs->desc_blocks - fs->desc_blocks);
307 if (new > (int) fs->blocksize/4)
308 new = fs->blocksize/4;
309 fs->super->s_reserved_gdt_blocks = new;
311 fs->super->s_feature_compat &=
312 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
316 * If we are shrinking the number block groups, we're done and
319 if (old_fs->group_desc_count > fs->group_desc_count) {
325 * Fix the count of the last (old) block group
327 old_numblocks = (old_fs->super->s_blocks_count -
328 old_fs->super->s_first_data_block) %
329 old_fs->super->s_blocks_per_group;
331 old_numblocks = old_fs->super->s_blocks_per_group;
332 if (old_fs->group_desc_count == fs->group_desc_count) {
333 numblocks = (fs->super->s_blocks_count -
334 fs->super->s_first_data_block) %
335 fs->super->s_blocks_per_group;
337 numblocks = fs->super->s_blocks_per_group;
339 numblocks = fs->super->s_blocks_per_group;
340 i = old_fs->group_desc_count - 1;
341 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
344 * If the number of block groups is staying the same, we're
345 * done and can exit now. (If the number block groups is
346 * shrinking, we had exited earlier.)
348 if (old_fs->group_desc_count >= fs->group_desc_count) {
354 * Initialize the new block group descriptors
356 group_block = fs->super->s_first_data_block +
357 old_fs->group_desc_count * fs->super->s_blocks_per_group;
359 adj = old_fs->group_desc_count;
360 max_group = fs->group_desc_count - adj;
361 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
362 old_desc_blocks = fs->super->s_first_meta_bg;
364 old_desc_blocks = fs->desc_blocks +
365 fs->super->s_reserved_gdt_blocks;
366 for (i = old_fs->group_desc_count;
367 i < fs->group_desc_count; i++) {
368 memset(&fs->group_desc[i], 0,
369 sizeof(struct ext2_group_desc));
372 if (i == fs->group_desc_count-1) {
373 numblocks = (fs->super->s_blocks_count -
374 fs->super->s_first_data_block) %
375 fs->super->s_blocks_per_group;
377 numblocks = fs->super->s_blocks_per_group;
379 numblocks = fs->super->s_blocks_per_group;
381 has_super = ext2fs_bg_has_super(fs, i);
383 ext2fs_mark_block_bitmap(fs->block_map, group_block);
386 meta_bg_size = (fs->blocksize /
387 sizeof (struct ext2_group_desc));
388 meta_bg = i / meta_bg_size;
389 if (!(fs->super->s_feature_incompat &
390 EXT2_FEATURE_INCOMPAT_META_BG) ||
391 (meta_bg < fs->super->s_first_meta_bg)) {
393 for (j=0; j < old_desc_blocks; j++)
394 ext2fs_mark_block_bitmap(fs->block_map,
395 group_block + 1 + j);
396 adjblocks += old_desc_blocks;
401 if (((i % meta_bg_size) == 0) ||
402 ((i % meta_bg_size) == 1) ||
403 ((i % meta_bg_size) == (meta_bg_size-1)))
404 ext2fs_mark_block_bitmap(fs->block_map,
405 group_block + has_super);
408 adjblocks += 2 + fs->inode_blocks_per_group;
410 numblocks -= adjblocks;
411 fs->super->s_free_blocks_count -= adjblocks;
412 fs->super->s_free_inodes_count +=
413 fs->super->s_inodes_per_group;
414 fs->group_desc[i].bg_free_blocks_count = numblocks;
415 fs->group_desc[i].bg_free_inodes_count =
416 fs->super->s_inodes_per_group;
417 fs->group_desc[i].bg_used_dirs_count = 0;
419 retval = ext2fs_allocate_group_table(fs, i, 0);
420 if (retval) goto errout;
422 group_block += fs->super->s_blocks_per_group;
431 * This routine adjusts the superblock and other data structures, both
432 * in disk as well as in memory...
434 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
441 unsigned long max_group;
444 ext2fs_mark_super_dirty(fs);
445 ext2fs_mark_bb_dirty(fs);
446 ext2fs_mark_ib_dirty(fs);
448 retval = adjust_fs_info(fs, rfs->old_fs, new_size);
453 * Check to make sure there are enough inodes
455 if ((rfs->old_fs->super->s_inodes_count -
456 rfs->old_fs->super->s_free_inodes_count) >
457 rfs->new_fs->super->s_inodes_count) {
463 * If we are shrinking the number block groups, we're done and
466 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
472 * If the number of block groups is staying the same, we're
473 * done and can exit now. (If the number block groups is
474 * shrinking, we had exited earlier.)
476 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
482 * Initialize the new block group descriptors
484 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
489 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
490 group_block = fs->super->s_first_data_block +
491 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
493 adj = rfs->old_fs->group_desc_count;
494 max_group = fs->group_desc_count - adj;
496 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
501 for (i = rfs->old_fs->group_desc_count;
502 i < fs->group_desc_count; i++) {
504 * Write out the new inode table
506 retval = io_channel_write_blk(fs->io,
507 fs->group_desc[i].bg_inode_table,
508 fs->inode_blocks_per_group,
510 if (retval) goto errout;
512 io_channel_flush(fs->io);
514 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
515 i - adj + 1, max_group);
519 group_block += fs->super->s_blocks_per_group;
521 io_channel_flush(fs->io);
528 /* --------------------------------------------------------------------
530 * Resize processing, phase 2.
532 * In this phase we adjust determine which blocks need to be moved, in
533 * blocks_to_move(). We then copy the blocks to their ultimate new
534 * destinations using block_mover(). Since we are copying blocks to
535 * their new locations, again during this pass we can abort without
537 * --------------------------------------------------------------------
541 * This helper function creates a block bitmap with all of the
542 * filesystem meta-data blocks.
544 static errcode_t mark_table_blocks(ext2_filsys fs,
545 ext2fs_block_bitmap bmap)
550 unsigned long meta_bg_size;
551 unsigned int old_desc_blocks;
553 meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
554 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
555 old_desc_blocks = fs->super->s_first_meta_bg;
557 old_desc_blocks = fs->desc_blocks +
558 fs->super->s_reserved_gdt_blocks;
559 for (i = 0; i < fs->group_desc_count; i++) {
560 ext2fs_reserve_super_and_bgd(fs, i, bmap);
563 * Mark the blocks used for the inode table
565 for (j = 0, b = fs->group_desc[i].bg_inode_table;
566 j < (unsigned int) fs->inode_blocks_per_group;
568 ext2fs_mark_block_bitmap(bmap, b);
571 * Mark block used for the block bitmap
573 ext2fs_mark_block_bitmap(bmap,
574 fs->group_desc[i].bg_block_bitmap);
577 * Mark block used for the inode bitmap
579 ext2fs_mark_block_bitmap(bmap,
580 fs->group_desc[i].bg_inode_bitmap);
586 * This function checks to see if a particular block (either a
587 * superblock or a block group descriptor) overlaps with an inode or
588 * block bitmap block, or with the inode table.
590 static void mark_fs_metablock(ext2_resize_t rfs,
591 ext2fs_block_bitmap meta_bmap,
592 int group, blk_t blk)
594 ext2_filsys fs = rfs->new_fs;
596 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
597 ext2fs_mark_block_bitmap(fs->block_map, blk);
600 * Check to see if we overlap with the inode or block bitmap,
601 * or the inode tables. If not, and the block is in use, then
602 * mark it as a block to be moved.
604 if (IS_BLOCK_BM(fs, group, blk)) {
605 FS_BLOCK_BM(fs, group) = 0;
606 rfs->needed_blocks++;
607 } else if (IS_INODE_BM(fs, group, blk)) {
608 FS_INODE_BM(fs, group) = 0;
609 rfs->needed_blocks++;
610 } else if (IS_INODE_TB(fs, group, blk)) {
611 FS_INODE_TB(fs, group) = 0;
612 rfs->needed_blocks++;
613 } else if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk) &&
614 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
615 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
616 rfs->needed_blocks++;
622 * This routine marks and unmarks reserved blocks in the new block
623 * bitmap. It also determines which blocks need to be moved and
624 * places this information into the move_blocks bitmap.
626 static errcode_t blocks_to_move(ext2_resize_t rfs)
629 dgrp_t i, max_groups;
630 blk_t blk, group_blk;
631 unsigned long old_blocks, new_blocks;
632 unsigned int meta_bg, meta_bg_size;
634 ext2_filsys fs, old_fs;
635 ext2fs_block_bitmap meta_bmap;
638 old_fs = rfs->old_fs;
639 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
642 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
643 &rfs->reserve_blocks);
647 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
652 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
657 retval = mark_table_blocks(old_fs, meta_bmap);
664 * If we're shrinking the filesystem, we need to move all of
665 * the blocks that don't fit any more
667 for (blk = fs->super->s_blocks_count;
668 blk < old_fs->super->s_blocks_count; blk++) {
669 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
670 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
671 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
672 rfs->needed_blocks++;
674 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
677 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
678 old_blocks = old_fs->super->s_first_meta_bg;
679 new_blocks = fs->super->s_first_meta_bg;
681 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
682 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
685 if (old_blocks == new_blocks) {
690 max_groups = fs->group_desc_count;
691 if (max_groups > old_fs->group_desc_count)
692 max_groups = old_fs->group_desc_count;
693 group_blk = old_fs->super->s_first_data_block;
695 * If we're reducing the number of descriptor blocks, this
696 * makes life easy. :-) We just have to mark some extra
699 if (old_blocks > new_blocks) {
700 for (i = 0; i < max_groups; i++) {
701 if (!ext2fs_bg_has_super(fs, i)) {
702 group_blk += fs->super->s_blocks_per_group;
705 for (blk = group_blk+1+new_blocks;
706 blk < group_blk+1+old_blocks; blk++) {
707 ext2fs_unmark_block_bitmap(fs->block_map,
709 rfs->needed_blocks--;
711 group_blk += fs->super->s_blocks_per_group;
717 * If we're increasing the number of descriptor blocks, life
718 * gets interesting....
720 meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
721 for (i = 0; i < max_groups; i++) {
722 has_super = ext2fs_bg_has_super(fs, i);
724 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
726 meta_bg = i / meta_bg_size;
727 if (!(fs->super->s_feature_incompat &
728 EXT2_FEATURE_INCOMPAT_META_BG) ||
729 (meta_bg < fs->super->s_first_meta_bg)) {
731 for (blk = group_blk+1;
732 blk < group_blk + 1 + new_blocks; blk++)
733 mark_fs_metablock(rfs, meta_bmap,
739 if (((i % meta_bg_size) == 0) ||
740 ((i % meta_bg_size) == 1) ||
741 ((i % meta_bg_size) == (meta_bg_size-1)))
742 mark_fs_metablock(rfs, meta_bmap, i,
743 group_blk + has_super);
746 if (fs->group_desc[i].bg_inode_table &&
747 fs->group_desc[i].bg_inode_bitmap &&
748 fs->group_desc[i].bg_block_bitmap)
752 * Reserve the existing meta blocks that we know
753 * aren't to be moved.
755 if (fs->group_desc[i].bg_block_bitmap)
756 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
757 fs->group_desc[i].bg_block_bitmap);
758 if (fs->group_desc[i].bg_inode_bitmap)
759 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
760 fs->group_desc[i].bg_inode_bitmap);
761 if (fs->group_desc[i].bg_inode_table)
762 for (blk = fs->group_desc[i].bg_inode_table, j=0;
763 j < fs->inode_blocks_per_group ; j++, blk++)
764 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
768 * Allocate the missing data structures
770 retval = ext2fs_allocate_group_table(fs, i,
771 rfs->reserve_blocks);
776 * For those structures that have changed, we need to
779 if (FS_BLOCK_BM(old_fs, i) !=
780 (blk = FS_BLOCK_BM(fs, i))) {
781 ext2fs_mark_block_bitmap(fs->block_map, blk);
782 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
783 !ext2fs_test_block_bitmap(meta_bmap, blk))
784 ext2fs_mark_block_bitmap(rfs->move_blocks,
787 if (FS_INODE_BM(old_fs, i) !=
788 (blk = FS_INODE_BM(fs, i))) {
789 ext2fs_mark_block_bitmap(fs->block_map, blk);
790 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
791 !ext2fs_test_block_bitmap(meta_bmap, blk))
792 ext2fs_mark_block_bitmap(rfs->move_blocks,
797 * The inode table, if we need to relocate it, is
798 * handled specially. We have to reserve the blocks
799 * for both the old and the new inode table, since we
800 * can't have the inode table be destroyed during the
801 * block relocation phase.
803 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
804 goto next_group; /* inode table not moved */
806 rfs->needed_blocks += fs->inode_blocks_per_group;
809 * Mark the new inode table as in use in the new block
810 * allocation bitmap, and move any blocks that might
813 for (blk = fs->group_desc[i].bg_inode_table, j=0;
814 j < fs->inode_blocks_per_group ; j++, blk++) {
815 ext2fs_mark_block_bitmap(fs->block_map, blk);
816 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
817 !ext2fs_test_block_bitmap(meta_bmap, blk))
818 ext2fs_mark_block_bitmap(rfs->move_blocks,
823 * Make sure the old inode table is reserved in the
824 * block reservation bitmap.
826 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
827 j < fs->inode_blocks_per_group ; j++, blk++)
828 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
831 group_blk += rfs->new_fs->super->s_blocks_per_group;
837 ext2fs_free_block_bitmap(meta_bmap);
843 * This helper function tries to allocate a new block. We try to
844 * avoid hitting the original group descriptor blocks at least at
845 * first, since we want to make it possible to recover from a badly
846 * aborted resize operation as much as possible.
848 * In the future, I may further modify this routine to balance out
849 * where we get the new blocks across the various block groups.
850 * Ideally we would allocate blocks that corresponded with the block
851 * group of the containing inode, and keep contiguous blocks
852 * together. However, this very difficult to do efficiently, since we
853 * don't have the necessary information up front.
857 #define DESPERATION 2
859 static void init_block_alloc(ext2_resize_t rfs)
861 rfs->alloc_state = AVOID_OLD;
862 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
864 /* HACK for testing */
865 if (rfs->new_fs->super->s_blocks_count >
866 rfs->old_fs->super->s_blocks_count)
867 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
871 static blk_t get_new_block(ext2_resize_t rfs)
873 ext2_filsys fs = rfs->new_fs;
876 if (rfs->new_blk >= fs->super->s_blocks_count) {
877 if (rfs->alloc_state == DESPERATION)
880 #ifdef RESIZE2FS_DEBUG
881 if (rfs->flags & RESIZE_DEBUG_BMOVE)
882 printf("Going into desperation mode "
883 "for block allocations\n");
885 rfs->alloc_state = DESPERATION;
886 rfs->new_blk = fs->super->s_first_data_block;
889 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
890 ext2fs_test_block_bitmap(rfs->reserve_blocks,
892 ((rfs->alloc_state == AVOID_OLD) &&
893 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
894 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
903 static errcode_t block_mover(ext2_resize_t rfs)
905 blk_t blk, old_blk, new_blk;
906 ext2_filsys fs = rfs->new_fs;
907 ext2_filsys old_fs = rfs->old_fs;
911 ext2_badblocks_list badblock_list = 0;
914 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
918 new_blk = fs->super->s_first_data_block;
919 if (!rfs->itable_buf) {
920 retval = ext2fs_get_array(fs->blocksize,
921 fs->inode_blocks_per_group,
926 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
931 * The first step is to figure out where all of the blocks
935 init_block_alloc(rfs);
936 for (blk = old_fs->super->s_first_data_block;
937 blk < old_fs->super->s_blocks_count; blk++) {
938 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
940 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
942 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
943 ext2fs_badblocks_list_del(badblock_list, blk);
948 new_blk = get_new_block(rfs);
953 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
954 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
960 ext2fs_free_extent_table(rfs->bmap);
968 * Step two is to actually move the blocks
970 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
971 if (retval) goto errout;
974 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
980 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
981 if (retval) goto errout;
984 #ifdef RESIZE2FS_DEBUG
985 if (rfs->flags & RESIZE_DEBUG_BMOVE)
986 printf("Moving %d blocks %u->%u\n",
987 size, old_blk, new_blk);
991 if (c > fs->inode_blocks_per_group)
992 c = fs->inode_blocks_per_group;
993 retval = io_channel_read_blk(fs->io, old_blk, c,
995 if (retval) goto errout;
996 retval = io_channel_write_blk(fs->io, new_blk, c,
998 if (retval) goto errout;
1003 if (rfs->progress) {
1004 io_channel_flush(fs->io);
1005 retval = (rfs->progress)(rfs,
1006 E2_RSZ_BLOCK_RELOC_PASS,
1012 io_channel_flush(fs->io);
1016 if (badblock_list) {
1017 if (!retval && bb_modified)
1018 retval = ext2fs_update_bb_inode(old_fs,
1020 ext2fs_badblocks_list_free(badblock_list);
1026 /* --------------------------------------------------------------------
1028 * Resize processing, phase 3
1030 * --------------------------------------------------------------------
1034 struct process_block_struct {
1037 struct ext2_inode * inode;
1043 static int process_block(ext2_filsys fs, blk_t *block_nr,
1044 e2_blkcnt_t blockcnt,
1045 blk_t ref_block EXT2FS_ATTR((unused)),
1046 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1048 struct process_block_struct *pb;
1050 blk_t block, new_block;
1053 pb = (struct process_block_struct *) priv_data;
1055 if (pb->rfs->bmap) {
1056 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1058 *block_nr = new_block;
1059 ret |= BLOCK_CHANGED;
1061 #ifdef RESIZE2FS_DEBUG
1062 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1063 printf("ino=%u, blockcnt=%lld, %u->%u\n",
1064 pb->ino, blockcnt, block, new_block);
1070 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
1071 block, (int) blockcnt);
1083 static errcode_t progress_callback(ext2_filsys fs,
1084 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1085 dgrp_t group, void * priv_data)
1087 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1091 * This check is to protect against old ext2 libraries. It
1092 * shouldn't be needed against new libraries.
1097 if (rfs->progress) {
1098 io_channel_flush(fs->io);
1099 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1100 group+1, fs->group_desc_count);
1108 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1110 struct process_block_struct pb;
1111 ext2_ino_t ino, new_inode;
1112 struct ext2_inode inode, *buf = NULL;
1113 struct ext2_inode_large *large_inode;
1114 ext2_inode_scan scan = NULL;
1117 char *block_buf = 0;
1118 ext2_ino_t start_to_move;
1119 blk_t orig_size, new_block;
1122 if ((rfs->old_fs->group_desc_count <=
1123 rfs->new_fs->group_desc_count) &&
1128 * Save the original size of the old filesystem, and
1129 * temporarily set the size to be the new size if the new size
1130 * is larger. We need to do this to avoid catching an error
1131 * by the block iterator routines
1133 orig_size = rfs->old_fs->super->s_blocks_count;
1134 if (orig_size < rfs->new_fs->super->s_blocks_count)
1135 rfs->old_fs->super->s_blocks_count =
1136 rfs->new_fs->super->s_blocks_count;
1138 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1139 if (retval) goto errout;
1141 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1142 if (retval) goto errout;
1143 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1144 if (retval) goto errout;
1146 start_to_move = (rfs->new_fs->group_desc_count *
1147 rfs->new_fs->super->s_inodes_per_group);
1149 if (rfs->progress) {
1150 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1151 0, rfs->old_fs->group_desc_count);
1155 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1159 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1160 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1161 buf = malloc(inode_size);
1167 * First, copy all of the inodes that need to be moved
1168 * elsewhere in the inode table
1171 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1172 if (retval) goto errout;
1176 if (inode.i_links_count == 0 && ino != EXT2_RESIZE_INO)
1177 continue; /* inode not in use */
1179 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
1182 if (inode.i_file_acl && rfs->bmap) {
1183 new_block = ext2fs_extent_translate(rfs->bmap,
1186 inode.i_file_acl = new_block;
1187 retval = ext2fs_write_inode(rfs->old_fs,
1189 if (retval) goto errout;
1193 if (ext2fs_inode_has_valid_blocks(&inode) &&
1194 (rfs->bmap || pb.is_dir)) {
1196 retval = ext2fs_block_iterate2(rfs->old_fs,
1198 process_block, &pb);
1207 if (ino <= start_to_move)
1208 continue; /* Don't need to move it. */
1214 if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
1218 if (new_inode > rfs->new_fs->super->s_inodes_count) {
1223 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1224 memcpy(buf, &inode, sizeof(struct ext2_inode));
1225 large_inode = (struct ext2_inode_large *)buf;
1226 large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
1227 EXT2_GOOD_OLD_INODE_SIZE;
1229 /* Get the new version of the inode */
1230 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1232 if (retval) goto errout;
1234 inode.i_ctime = time(0);
1235 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1237 if (retval) goto errout;
1239 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1240 if (LINUX_S_ISDIR(inode.i_mode))
1241 rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1243 #ifdef RESIZE2FS_DEBUG
1244 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1245 printf("Inode moved %u->%u\n", ino, new_inode);
1248 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1252 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1254 io_channel_flush(rfs->old_fs->io);
1257 rfs->old_fs->super->s_blocks_count = orig_size;
1259 ext2fs_free_extent_table(rfs->bmap);
1263 ext2fs_close_inode_scan(scan);
1265 ext2fs_free_mem(&block_buf);
1271 /* --------------------------------------------------------------------
1273 * Resize processing, phase 4.
1275 * --------------------------------------------------------------------
1281 unsigned long max_dirs;
1285 static int check_and_change_inodes(ext2_ino_t dir,
1286 int entry EXT2FS_ATTR((unused)),
1287 struct ext2_dir_entry *dirent, int offset,
1288 int blocksize EXT2FS_ATTR((unused)),
1289 char *buf EXT2FS_ATTR((unused)),
1292 struct istruct *is = (struct istruct *) priv_data;
1293 struct ext2_inode inode;
1294 ext2_ino_t new_inode;
1297 if (is->rfs->progress && offset == 0) {
1298 io_channel_flush(is->rfs->old_fs->io);
1299 is->err = (is->rfs->progress)(is->rfs,
1300 E2_RSZ_INODE_REF_UPD_PASS,
1301 ++is->num, is->max_dirs);
1303 return DIRENT_ABORT;
1309 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1313 #ifdef RESIZE2FS_DEBUG
1314 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1315 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1316 dir, dirent->name_len&0xFF, dirent->name,
1317 dirent->inode, new_inode);
1320 dirent->inode = new_inode;
1322 /* Update the directory mtime and ctime */
1323 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1325 inode.i_mtime = inode.i_ctime = time(0);
1326 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1328 return DIRENT_ABORT;
1331 return DIRENT_CHANGED;
1334 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1343 * Now, we iterate over all of the directories to update the
1347 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1351 if (rfs->progress) {
1352 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1358 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1359 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1360 check_and_change_inodes, &is);
1369 ext2fs_free_extent_table(rfs->imap);
1375 /* --------------------------------------------------------------------
1377 * Resize processing, phase 5.
1379 * In this phase we actually move the inode table around, and then
1380 * update the summary statistics. This is scary, since aborting here
1381 * will potentially scramble the filesystem. (We are moving the
1382 * inode tables around in place, and so the potential for lost data,
1383 * or at the very least scrambling the mapping between filenames and
1384 * inode numbers is very high in case of a power failure here.)
1385 * --------------------------------------------------------------------
1390 * A very scary routine --- this one moves the inode table around!!!
1392 * After this you have to use the rfs->new_fs file handle to read and
1395 static errcode_t move_itables(ext2_resize_t rfs)
1397 int n, num, size, diff;
1398 dgrp_t i, max_groups;
1399 ext2_filsys fs = rfs->new_fs;
1401 blk_t old_blk, new_blk, blk;
1403 int j, to_move, moved;
1405 max_groups = fs->group_desc_count;
1406 if (max_groups > rfs->old_fs->group_desc_count)
1407 max_groups = rfs->old_fs->group_desc_count;
1409 size = fs->blocksize * fs->inode_blocks_per_group;
1410 if (!rfs->itable_buf) {
1411 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1417 * Figure out how many inode tables we need to move
1419 to_move = moved = 0;
1420 for (i=0; i < max_groups; i++)
1421 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1422 fs->group_desc[i].bg_inode_table)
1428 if (rfs->progress) {
1429 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1435 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1437 for (i=0; i < max_groups; i++) {
1438 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1439 new_blk = fs->group_desc[i].bg_inode_table;
1440 diff = new_blk - old_blk;
1442 #ifdef RESIZE2FS_DEBUG
1443 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1444 printf("Itable move group %d block %u->%u (diff %d)\n",
1445 i, old_blk, new_blk, diff);
1451 retval = io_channel_read_blk(fs->io, old_blk,
1452 fs->inode_blocks_per_group,
1457 * The end of the inode table segment often contains
1458 * all zeros, and we're often only moving the inode
1459 * table down a block or two. If so, we can optimize
1460 * things by not rewriting blocks that we know to be zero
1463 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1466 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1467 #ifdef RESIZE2FS_DEBUG
1468 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1469 printf("%d blocks of zeros...\n", n);
1471 num = fs->inode_blocks_per_group;
1475 retval = io_channel_write_blk(fs->io, new_blk,
1476 num, rfs->itable_buf);
1478 io_channel_write_blk(fs->io, old_blk,
1479 num, rfs->itable_buf);
1483 retval = io_channel_write_blk(fs->io,
1484 old_blk + fs->inode_blocks_per_group,
1485 diff, (rfs->itable_buf +
1486 (fs->inode_blocks_per_group - diff) *
1492 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1493 j < fs->inode_blocks_per_group ; j++, blk++)
1494 ext2fs_unmark_block_bitmap(fs->block_map, blk);
1496 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1497 ext2fs_mark_super_dirty(rfs->old_fs);
1498 ext2fs_flush(rfs->old_fs);
1500 if (rfs->progress) {
1501 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1507 mark_table_blocks(fs, fs->block_map);
1509 #ifdef RESIZE2FS_DEBUG
1510 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1511 printf("Inode table move finished.\n");
1520 * Fix the resize inode
1522 static errcode_t fix_resize_inode(ext2_filsys fs)
1524 struct ext2_inode inode;
1528 if (!(fs->super->s_feature_compat &
1529 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1532 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1533 if (retval) goto errout;
1535 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1536 if (retval) goto errout;
1538 inode.i_blocks = fs->blocksize/512;
1540 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1541 if (retval) goto errout;
1543 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1545 * Avoid zeroing out block #0; that's rude. This
1546 * should never happen anyway since the filesystem
1547 * should be fsck'ed and we assume it is consistent.
1550 _("Should never happen: resize inode corrupt!\n"));
1554 memset(block_buf, 0, fs->blocksize);
1556 retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1558 if (retval) goto errout;
1560 retval = ext2fs_create_resize_inode(fs);
1566 ext2fs_free_mem(&block_buf);
1571 * Finally, recalculate the summary information
1573 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1577 unsigned int group = 0;
1578 unsigned int count = 0;
1583 * First calculate the block statistics
1585 for (blk = fs->super->s_first_data_block;
1586 blk < fs->super->s_blocks_count; blk++) {
1587 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1592 if ((count == fs->super->s_blocks_per_group) ||
1593 (blk == fs->super->s_blocks_count-1)) {
1594 fs->group_desc[group++].bg_free_blocks_count =
1600 fs->super->s_free_blocks_count = total_free;
1603 * Next, calculate the inode statistics
1610 /* Protect loop from wrap-around if s_inodes_count maxed */
1611 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1612 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1617 if ((count == fs->super->s_inodes_per_group) ||
1618 (ino == fs->super->s_inodes_count)) {
1619 fs->group_desc[group++].bg_free_inodes_count =
1625 fs->super->s_free_inodes_count = total_free;
1626 ext2fs_mark_super_dirty(fs);