2 * resize2fs.c --- ext2 main routine
4 * Copyright (C) 1997 Theodore Ts'o
11 #include "resize2fs.h"
14 * This routine adjusts the superblock and other data structures...
16 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
23 blk_t blk, group_block;
25 struct ext2_group_desc *new;
27 int old_numblocks, numblocks, adjblocks;
30 fs->super->s_blocks_count = new_size;
31 ext2fs_mark_super_dirty(fs);
32 ext2fs_mark_bb_dirty(fs);
33 ext2fs_mark_ib_dirty(fs);
36 fs->group_desc_count = (fs->super->s_blocks_count -
37 fs->super->s_first_data_block +
38 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
39 / EXT2_BLOCKS_PER_GROUP(fs->super);
40 if (fs->group_desc_count == 0)
41 return EXT2_ET_TOOSMALL;
42 fs->desc_blocks = (fs->group_desc_count +
43 EXT2_DESC_PER_BLOCK(fs->super) - 1)
44 / EXT2_DESC_PER_BLOCK(fs->super);
47 * Overhead is the number of bookkeeping blocks per group. It
48 * includes the superblock backup, the group descriptor
49 * backups, the inode bitmap, the block bitmap, and the inode
52 * XXX Not all block groups need the descriptor blocks, but
53 * being clever is tricky...
55 overhead = 3 + fs->desc_blocks + fs->inode_blocks_per_group;
58 * See if the last group is big enough to support the
59 * necessary data structures. If not, we need to get rid of
62 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
63 fs->super->s_blocks_per_group;
64 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
65 return EXT2_ET_TOOSMALL;
66 if (rem && (rem < overhead+50)) {
67 fs->super->s_blocks_count -= rem;
71 * Adjust the number of inodes
73 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
77 * Adjust the number of free blocks
79 blk = rfs->old_fs->super->s_blocks_count;
80 if (blk > fs->super->s_blocks_count)
81 fs->super->s_free_blocks_count -=
82 (blk - fs->super->s_blocks_count);
84 fs->super->s_free_blocks_count +=
85 (fs->super->s_blocks_count - blk);
88 * Adjust the bitmaps for size
90 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
91 fs->super->s_inodes_count,
96 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
97 * fs->group_desc_count)) - 1 +
98 fs->super->s_first_data_block;
99 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
100 real_end, fs->block_map);
106 * Reallocate the group descriptors as necessary.
108 if (rfs->old_fs->desc_blocks != fs->desc_blocks) {
109 new = realloc(fs->group_desc,
110 fs->desc_blocks * fs->blocksize);
113 fs->group_desc = new;
117 * Fix the count of the last (old) block group
119 if (rfs->old_fs->group_desc_count > fs->group_desc_count)
121 old_numblocks = (rfs->old_fs->super->s_blocks_count -
122 rfs->old_fs->super->s_first_data_block) %
123 rfs->old_fs->super->s_blocks_per_group;
125 old_numblocks = rfs->old_fs->super->s_blocks_per_group;
126 if (rfs->old_fs->group_desc_count == fs->group_desc_count) {
127 numblocks = (rfs->new_fs->super->s_blocks_count -
128 rfs->new_fs->super->s_first_data_block) %
129 rfs->new_fs->super->s_blocks_per_group;
131 numblocks = rfs->new_fs->super->s_blocks_per_group;
133 numblocks = rfs->new_fs->super->s_blocks_per_group;
134 i = rfs->old_fs->group_desc_count - 1;
135 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
138 * Initialize the new block group descriptors
140 if (rfs->old_fs->group_desc_count >= fs->group_desc_count)
142 buf = malloc(fs->blocksize);
145 memset(buf, 0, fs->blocksize);
146 group_block = fs->super->s_first_data_block +
147 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
148 for (i = rfs->old_fs->group_desc_count;
149 i < fs->group_desc_count; i++) {
150 memset(&fs->group_desc[i], 0,
151 sizeof(struct ext2_group_desc));
154 if (i == fs->group_desc_count-1) {
155 numblocks = (fs->super->s_blocks_count -
156 fs->super->s_first_data_block) %
157 fs->super->s_blocks_per_group;
159 numblocks = fs->super->s_blocks_per_group;
161 numblocks = fs->super->s_blocks_per_group;
163 if (ext2fs_bg_has_super(fs, i)) {
164 for (j=0; j < fs->desc_blocks+1; j++)
165 ext2fs_mark_block_bitmap(fs->block_map,
167 adjblocks = 1 + fs->desc_blocks;
169 adjblocks += 2 + fs->inode_blocks_per_group;
171 numblocks -= adjblocks;
172 fs->super->s_free_blocks_count -= adjblocks;
173 fs->super->s_free_inodes_count +=
174 fs->super->s_inodes_per_group;
175 fs->group_desc[i].bg_free_blocks_count = numblocks;
176 fs->group_desc[i].bg_free_inodes_count =
177 fs->super->s_inodes_per_group;
178 fs->group_desc[i].bg_used_dirs_count = 0;
180 retval = ext2fs_allocate_group_table(fs, i, 0);
184 for (blk=fs->group_desc[i].bg_inode_table, j=0;
185 j < fs->inode_blocks_per_group;
187 retval = io_channel_write_blk(fs->io, blk, 1, buf);
191 group_block += fs->super->s_blocks_per_group;
197 * This routine marks and unmarks reserved blocks in the new block
198 * bitmap. It also determines which blocks need to be moved and
199 * places this information into the move_blocks bitmap.
201 static errcode_t determine_relocations(ext2_resize_t rfs)
204 blk_t blk, group_blk;
205 unsigned long old_blocks, new_blocks;
207 ext2_filsys fs = rfs->new_fs;
209 retval = ext2fs_allocate_block_bitmap(rfs->old_fs,
210 "blocks to be moved",
211 &rfs->reserve_blocks);
216 * If we're shrinking the filesystem, we need to move all of
217 * the blocks that don't fit any more
219 for (blk = fs->super->s_blocks_count;
220 blk < rfs->old_fs->super->s_blocks_count; blk++) {
221 if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk))
222 rfs->needed_blocks++;
223 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
226 old_blocks = rfs->old_fs->desc_blocks;
227 new_blocks = fs->desc_blocks;
229 if (old_blocks == new_blocks)
232 group_blk = rfs->old_fs->super->s_first_data_block;
234 * If we're reducing the number of descriptor blocks, this
235 * makes life easy. :-) We just have to mark some extra
238 if (old_blocks > new_blocks) {
239 for (i = 0; i < fs->group_desc_count; i++) {
240 if (!ext2fs_bg_has_super(fs, i)) {
241 group_blk += fs->super->s_blocks_per_group;
244 for (blk = group_blk+1+old_blocks;
245 blk < group_blk+1+new_blocks; blk++)
246 ext2fs_unmark_block_bitmap(fs->block_map,
248 group_blk += fs->super->s_blocks_per_group;
253 * If we're increasing the number of descriptor blocks, life
254 * gets interesting....
256 for (i = 0; i < fs->group_desc_count; i++) {
257 if (!ext2fs_bg_has_super(fs, i))
260 for (blk = group_blk;
261 blk < group_blk + 1 + new_blocks; blk++) {
262 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
263 ext2fs_mark_block_bitmap(fs->block_map, blk);
266 * Check to see if we overlap with the inode
269 if (blk == fs->group_desc[i].bg_inode_bitmap)
270 fs->group_desc[i].bg_block_bitmap = 0;
271 if (blk == fs->group_desc[i].bg_inode_bitmap)
272 fs->group_desc[i].bg_inode_bitmap = 0;
275 * Check to see if we overlap with the inode
278 if (blk < fs->group_desc[i].bg_inode_table)
280 if (blk >= (fs->group_desc[i].bg_inode_table +
281 fs->inode_blocks_per_group))
283 fs->group_desc[i].bg_inode_table = 0;
284 blk = fs->group_desc[i].bg_inode_table +
285 fs->inode_blocks_per_group - 1;
287 if (fs->group_desc[i].bg_inode_table &&
288 fs->group_desc[i].bg_inode_bitmap &&
289 fs->group_desc[i].bg_block_bitmap)
293 * Allocate the missing bitmap and inode table
294 * structures, passing in rfs->reserve_blocks to
295 * prevent a conflict.
297 if (fs->group_desc[i].bg_block_bitmap)
298 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
299 fs->group_desc[i].bg_block_bitmap);
300 if (fs->group_desc[i].bg_inode_bitmap)
301 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
302 fs->group_desc[i].bg_inode_bitmap);
303 if (fs->group_desc[i].bg_inode_table)
304 for (blk = fs->group_desc[i].bg_inode_table, j=0;
305 j < fs->inode_blocks_per_group ; j++, blk++)
306 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
309 retval = ext2fs_allocate_group_table(fs, i,
310 rfs->reserve_blocks);
315 * Now make sure these blocks are reserved in the new
318 ext2fs_mark_block_bitmap(fs->block_map,
319 fs->group_desc[i].bg_block_bitmap);
320 ext2fs_mark_block_bitmap(fs->block_map,
321 fs->group_desc[i].bg_inode_bitmap);
323 for (blk = fs->group_desc[i].bg_inode_table, j=0;
324 j < fs->inode_blocks_per_group ; j++, blk++)
325 ext2fs_mark_block_bitmap(fs->block_map, blk);
328 * Mark the inode tables which will need to move, and
329 * restore the old inode table location (for now)
331 if (fs->group_desc[i].bg_inode_table !=
332 rfs->old_fs->group_desc[i].bg_inode_table) {
333 rfs->move_itable[i] = fs->group_desc[i].bg_inode_table;
334 fs->group_desc[i].bg_inode_table =
335 rfs->old_fs->group_desc[i].bg_inode_table;
339 group_blk += rfs->new_fs->super->s_blocks_per_group;
345 * This is the top-level routine which does the dirty deed....
347 errcode_t resize_fs(ext2_filsys fs, blk_t new_size)
352 retval = ext2fs_read_bitmaps(fs);
357 * Create the data structure
359 rfs = malloc(sizeof(struct ext2_resize_struct));
362 memset(rfs, 0, sizeof(struct ext2_resize_struct));
364 rfs->move_itable = malloc(sizeof(blk_t) * fs->group_desc_count);
365 if (!rfs->move_itable) {
369 memset(rfs->move_itable, 0, sizeof(blk_t) * fs->group_desc_count);
372 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
376 retval = adjust_superblock(rfs, new_size);
380 retval = determine_relocations(rfs);
384 printf("\nOld superblock:\n");
385 list_super(rfs->old_fs->super);
386 printf("\n\nNew superblock:\n");
387 list_super(rfs->new_fs->super);
390 retval = ext2fs_move_blocks(rfs->old_fs, rfs->reserve_blocks,
391 EXT2_BMOVE_GET_DBLIST);
393 retval = ext2fs_close(rfs->new_fs);
397 ext2fs_free(rfs->old_fs);
402 if (rfs->move_itable)
403 free(rfs->move_itable);
405 ext2fs_free(rfs->new_fs);