blob: 9356d067486aefaed136ba1a274ccad81074beb9 [file] [log] [blame]
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +00001/*
2 * resize2fs.c --- ext2 main routine
3 *
Theodore Ts'o0cee8a52000-04-06 21:38:34 +00004 * Copyright (C) 1997, 1998 by Theodore Ts'o and
5 * PowerQuest, Inc.
6 *
7 * Copyright (C) 1999, 2000 by Theosore Ts'o
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +00008 *
9 * %Begin-Header%
Theodore Ts'o0cee8a52000-04-06 21:38:34 +000010 * This file may be redistributed under the terms of the GNU Public
11 * License.
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000012 * %End-Header%
13 */
14
Theodore Ts'o05e112a1997-06-14 07:28:44 +000015/*
16 * Resizing a filesystem consists of the following phases:
17 *
Theodore Ts'oa8519a21998-02-16 22:16:20 +000018 * 1. Adjust superblock and write out new parts of the inode
Theodore Ts'o05e112a1997-06-14 07:28:44 +000019 * table
Theodore Ts'oa8519a21998-02-16 22:16:20 +000020 * 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
28 * new ones.
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.
Theodore Ts'o05e112a1997-06-14 07:28:44 +000034 */
Theodore Ts'oa8519a21998-02-16 22:16:20 +000035
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000036#include "resize2fs.h"
Matthias Andreeb969b1b2003-12-28 13:04:35 +010037#include <time.h>
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000038
Theodore Ts'o546a1ff2002-03-07 23:52:56 -050039#ifdef __linux__ /* Kludge for debugging */
Theodore Ts'oa8519a21998-02-16 22:16:20 +000040#define RESIZE2FS_DEBUG
41#endif
42
43static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size);
44static errcode_t blocks_to_move(ext2_resize_t rfs);
45static errcode_t block_mover(ext2_resize_t rfs);
46static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
47static errcode_t inode_ref_fix(ext2_resize_t rfs);
48static errcode_t move_itables(ext2_resize_t rfs);
Theodore Ts'o9213a932004-12-24 01:34:29 -050049static errcode_t fix_resize_inode(ext2_filsys fs);
Theodore Ts'oa8519a21998-02-16 22:16:20 +000050static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
51
52/*
53 * Some helper CPP macros
54 */
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)
58
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)))
61
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)))
65
66
67
68/*
69 * This is the top-level routine which does the dirty deed....
70 */
Theodore Ts'o116db1b2002-04-01 01:28:30 -050071errcode_t resize_fs(ext2_filsys fs, blk_t *new_size, int flags,
Theodore Ts'o3b627e81998-02-24 20:24:49 +000072 errcode_t (*progress)(ext2_resize_t rfs, int pass,
Theodore Ts'oa8519a21998-02-16 22:16:20 +000073 unsigned long cur,
Theodore Ts'o1333fe91998-09-03 00:26:49 +000074 unsigned long max_val))
Theodore Ts'oa8519a21998-02-16 22:16:20 +000075{
76 ext2_resize_t rfs;
77 errcode_t retval;
78
79 retval = ext2fs_read_bitmaps(fs);
80 if (retval)
81 return retval;
82
83 /*
84 * Create the data structure
85 */
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040086 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
Theodore Ts'oa8519a21998-02-16 22:16:20 +000087 if (retval)
88 return retval;
89 memset(rfs, 0, sizeof(struct ext2_resize_struct));
90
91 rfs->old_fs = fs;
92 rfs->flags = flags;
93 rfs->itable_buf = 0;
94 rfs->progress = progress;
95 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
96 if (retval)
97 goto errout;
98
Theodore Ts'o116db1b2002-04-01 01:28:30 -050099 retval = adjust_superblock(rfs, *new_size);
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000100 if (retval)
101 goto errout;
102
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500103 *new_size = rfs->new_fs->super->s_blocks_count;
104
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000105 retval = blocks_to_move(rfs);
106 if (retval)
107 goto errout;
108
109#ifdef RESIZE2FS_DEBUG
110 if (rfs->flags & RESIZE_DEBUG_BMOVE)
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000111 printf(_("Number of free blocks: %d/%d, Needed: %d\n"),
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000112 rfs->old_fs->super->s_free_blocks_count,
113 rfs->new_fs->super->s_free_blocks_count,
114 rfs->needed_blocks);
115#endif
116
117 retval = block_mover(rfs);
118 if (retval)
119 goto errout;
120
121 retval = inode_scan_and_fix(rfs);
122 if (retval)
123 goto errout;
124
125 retval = inode_ref_fix(rfs);
126 if (retval)
127 goto errout;
128
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000129 retval = move_itables(rfs);
130 if (retval)
131 goto errout;
132
Theodore Ts'o64ad98a2005-01-26 10:03:56 -0500133 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
134 if (retval)
135 goto errout;
136
Theodore Ts'o9213a932004-12-24 01:34:29 -0500137 retval = fix_resize_inode(rfs->new_fs);
138 if (retval)
139 goto errout;
140
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000141 retval = ext2fs_close(rfs->new_fs);
142 if (retval)
143 goto errout;
144
145 rfs->flags = flags;
146
147 ext2fs_free(rfs->old_fs);
148 if (rfs->itable_buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400149 ext2fs_free_mem(&rfs->itable_buf);
150 ext2fs_free_mem(&rfs);
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000151
152 return 0;
153
154errout:
155 if (rfs->new_fs)
156 ext2fs_free(rfs->new_fs);
157 if (rfs->itable_buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400158 ext2fs_free_mem(&rfs->itable_buf);
159 ext2fs_free_mem(&rfs);
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000160 return retval;
161}
162
163/* --------------------------------------------------------------------
164 *
165 * Resize processing, phase 1.
166 *
167 * In this phase we adjust the in-memory superblock information, and
168 * initialize any new parts of the inode table. The new parts of the
169 * inode table are created in virgin disk space, so we can abort here
170 * without any side effects.
171 * --------------------------------------------------------------------
172 */
173
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000174/*
175 * This routine adjusts the superblock and other data structures...
176 */
177static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
178{
179 ext2_filsys fs;
180 int overhead = 0;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000181 int rem, adj = 0;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000182 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000183 ext2_ino_t real_end;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000184 blk_t blk, group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500185 unsigned long i, j, old_desc_blocks;
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000186 int old_numblocks, numblocks, adjblocks;
Theodore Ts'o54434922003-12-07 01:28:50 -0500187 unsigned int meta_bg, meta_bg_size;
188 int has_super;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +0000189 unsigned long max_group;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000190
191 fs = rfs->new_fs;
192 fs->super->s_blocks_count = new_size;
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000193 ext2fs_mark_super_dirty(fs);
194 ext2fs_mark_bb_dirty(fs);
195 ext2fs_mark_ib_dirty(fs);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000196
197retry:
198 fs->group_desc_count = (fs->super->s_blocks_count -
199 fs->super->s_first_data_block +
200 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
201 / EXT2_BLOCKS_PER_GROUP(fs->super);
202 if (fs->group_desc_count == 0)
203 return EXT2_ET_TOOSMALL;
204 fs->desc_blocks = (fs->group_desc_count +
205 EXT2_DESC_PER_BLOCK(fs->super) - 1)
206 / EXT2_DESC_PER_BLOCK(fs->super);
207
208 /*
209 * Overhead is the number of bookkeeping blocks per group. It
210 * includes the superblock backup, the group descriptor
211 * backups, the inode bitmap, the block bitmap, and the inode
212 * table.
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000213 */
Theodore Ts'o9213a932004-12-24 01:34:29 -0500214 overhead = (int) (2 + fs->inode_blocks_per_group);
215
216 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
217 overhead += 1 + fs->desc_blocks +
218 fs->super->s_reserved_gdt_blocks;
219
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000220 /*
221 * See if the last group is big enough to support the
222 * necessary data structures. If not, we need to get rid of
223 * it.
224 */
225 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
226 fs->super->s_blocks_per_group;
227 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
228 return EXT2_ET_TOOSMALL;
229 if (rem && (rem < overhead+50)) {
230 fs->super->s_blocks_count -= rem;
231 goto retry;
232 }
233 /*
234 * Adjust the number of inodes
235 */
236 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
237 fs->group_desc_count;
238
239 /*
240 * Adjust the number of free blocks
241 */
242 blk = rfs->old_fs->super->s_blocks_count;
243 if (blk > fs->super->s_blocks_count)
244 fs->super->s_free_blocks_count -=
245 (blk - fs->super->s_blocks_count);
246 else
247 fs->super->s_free_blocks_count +=
248 (fs->super->s_blocks_count - blk);
249
250 /*
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000251 * Adjust the number of reserved blocks
252 */
253 blk = rfs->old_fs->super->s_r_blocks_count * 100 /
254 rfs->old_fs->super->s_blocks_count;
255 fs->super->s_r_blocks_count = ((fs->super->s_blocks_count * blk)
256 / 100);
257
258 /*
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000259 * Adjust the bitmaps for size
260 */
261 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
262 fs->super->s_inodes_count,
263 fs->inode_map);
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000264 if (retval) goto errout;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000265
266 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
267 * fs->group_desc_count)) - 1 +
268 fs->super->s_first_data_block;
269 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
270 real_end, fs->block_map);
271
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000272 if (retval) goto errout;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000273
274 /*
275 * Reallocate the group descriptors as necessary.
276 */
277 if (rfs->old_fs->desc_blocks != fs->desc_blocks) {
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000278 retval = ext2fs_resize_mem(rfs->old_fs->desc_blocks *
279 fs->blocksize,
280 fs->desc_blocks * fs->blocksize,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400281 &fs->group_desc);
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000282 if (retval)
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000283 goto errout;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000284 }
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000285
286 /*
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000287 * Check to make sure there are enough inodes
288 */
289 if ((rfs->old_fs->super->s_inodes_count -
290 rfs->old_fs->super->s_free_inodes_count) >
291 rfs->new_fs->super->s_inodes_count) {
292 retval = ENOSPC;
293 goto errout;
294 }
295
296 /*
Theodore Ts'o9213a932004-12-24 01:34:29 -0500297 * If the resize_inode feature is set, and we are changing the
298 * number of descriptor blocks, then adjust
299 * s_reserved_gdt_blocks if possible to avoid needing to move
300 * the inode table either now or in the future.
301 */
302 if ((fs->super->s_feature_compat &
303 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
304 (rfs->old_fs->desc_blocks != fs->desc_blocks)) {
305 int new;
306
307 new = ((int) fs->super->s_reserved_gdt_blocks) +
308 (rfs->old_fs->desc_blocks - fs->desc_blocks);
309 if (new < 0)
310 new = 0;
311 if (new > fs->blocksize/4)
312 new = fs->blocksize/4;
313 fs->super->s_reserved_gdt_blocks = new;
314 if (new == 0)
315 fs->super->s_feature_compat &=
316 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
317 }
318
319 /*
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000320 * If we are shrinking the number block groups, we're done and
321 * can exit now.
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000322 */
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000323 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
324 retval = 0;
325 goto errout;
326 }
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000327 /*
328 * Fix the count of the last (old) block group
329 */
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000330 old_numblocks = (rfs->old_fs->super->s_blocks_count -
331 rfs->old_fs->super->s_first_data_block) %
332 rfs->old_fs->super->s_blocks_per_group;
333 if (!old_numblocks)
334 old_numblocks = rfs->old_fs->super->s_blocks_per_group;
335 if (rfs->old_fs->group_desc_count == fs->group_desc_count) {
336 numblocks = (rfs->new_fs->super->s_blocks_count -
337 rfs->new_fs->super->s_first_data_block) %
338 rfs->new_fs->super->s_blocks_per_group;
339 if (!numblocks)
340 numblocks = rfs->new_fs->super->s_blocks_per_group;
341 } else
342 numblocks = rfs->new_fs->super->s_blocks_per_group;
343 i = rfs->old_fs->group_desc_count - 1;
344 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
345
346 /*
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000347 * If the number of block groups is staying the same, we're
348 * done and can exit now. (If the number block groups is
349 * shrinking, we had exited earlier.)
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000350 */
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000351 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
352 retval = 0;
353 goto errout;
354 }
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000355 /*
356 * Initialize the new block group descriptors
357 */
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000358 retval = ext2fs_get_mem(fs->blocksize * fs->inode_blocks_per_group,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400359 &rfs->itable_buf);
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000360 if (retval)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000361 goto errout;
Theodore Ts'oca8abba1998-01-19 14:55:24 +0000362
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000363 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000364 group_block = fs->super->s_first_data_block +
365 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000366
Theodore Ts'o63b44fb1998-02-13 22:58:18 +0000367 adj = rfs->old_fs->group_desc_count;
368 max_group = fs->group_desc_count - adj;
Theodore Ts'o3b627e81998-02-24 20:24:49 +0000369 if (rfs->progress) {
370 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
371 0, max_group);
372 if (retval)
373 goto errout;
374 }
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500375 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
376 old_desc_blocks = fs->super->s_first_meta_bg;
377 else
Theodore Ts'o9213a932004-12-24 01:34:29 -0500378 old_desc_blocks = fs->desc_blocks +
379 fs->super->s_reserved_gdt_blocks;
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000380 for (i = rfs->old_fs->group_desc_count;
381 i < fs->group_desc_count; i++) {
382 memset(&fs->group_desc[i], 0,
383 sizeof(struct ext2_group_desc));
384 adjblocks = 0;
385
386 if (i == fs->group_desc_count-1) {
387 numblocks = (fs->super->s_blocks_count -
388 fs->super->s_first_data_block) %
389 fs->super->s_blocks_per_group;
390 if (!numblocks)
391 numblocks = fs->super->s_blocks_per_group;
392 } else
393 numblocks = fs->super->s_blocks_per_group;
394
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500395 has_super = ext2fs_bg_has_super(fs, i);
396 if (has_super) {
397 ext2fs_mark_block_bitmap(fs->block_map, group_block);
398 adjblocks++;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000399 }
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500400 meta_bg_size = (fs->blocksize /
401 sizeof (struct ext2_group_desc));
402 meta_bg = i / meta_bg_size;
403 if (!(fs->super->s_feature_incompat &
404 EXT2_FEATURE_INCOMPAT_META_BG) ||
405 (meta_bg < fs->super->s_first_meta_bg)) {
406 if (has_super) {
407 for (j=0; j < old_desc_blocks; j++)
408 ext2fs_mark_block_bitmap(fs->block_map,
409 group_block + 1 + j);
410 adjblocks += old_desc_blocks;
411 }
412 } else {
413 if (has_super)
414 has_super = 1;
415 if (((i % meta_bg_size) == 0) ||
416 ((i % meta_bg_size) == 1) ||
417 ((i % meta_bg_size) == (meta_bg_size-1)))
418 ext2fs_mark_block_bitmap(fs->block_map,
419 group_block + has_super);
420 }
421
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000422 adjblocks += 2 + fs->inode_blocks_per_group;
423
424 numblocks -= adjblocks;
425 fs->super->s_free_blocks_count -= adjblocks;
426 fs->super->s_free_inodes_count +=
427 fs->super->s_inodes_per_group;
428 fs->group_desc[i].bg_free_blocks_count = numblocks;
429 fs->group_desc[i].bg_free_inodes_count =
430 fs->super->s_inodes_per_group;
431 fs->group_desc[i].bg_used_dirs_count = 0;
432
433 retval = ext2fs_allocate_group_table(fs, i, 0);
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000434 if (retval) goto errout;
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000435
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000436 /*
437 * Write out the new inode table
438 */
439 retval = io_channel_write_blk(fs->io,
440 fs->group_desc[i].bg_inode_table,
441 fs->inode_blocks_per_group,
442 rfs->itable_buf);
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000443 if (retval) goto errout;
444
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000445 io_channel_flush(fs->io);
Theodore Ts'o3b627e81998-02-24 20:24:49 +0000446 if (rfs->progress) {
447 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
448 i - adj + 1, max_group);
449 if (retval)
450 goto errout;
451 }
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000452 group_block += fs->super->s_blocks_per_group;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000453 }
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000454 io_channel_flush(fs->io);
455 retval = 0;
456
457errout:
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000458 return retval;
459}
460
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000461/* --------------------------------------------------------------------
462 *
463 * Resize processing, phase 2.
464 *
465 * In this phase we adjust determine which blocks need to be moved, in
466 * blocks_to_move(). We then copy the blocks to their ultimate new
467 * destinations using block_mover(). Since we are copying blocks to
468 * their new locations, again during this pass we can abort without
469 * any problems.
470 * --------------------------------------------------------------------
471 */
472
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000473/*
474 * This helper function creates a block bitmap with all of the
475 * filesystem meta-data blocks.
476 */
477static errcode_t mark_table_blocks(ext2_filsys fs,
Theodore Ts'o64ad98a2005-01-26 10:03:56 -0500478 ext2fs_block_bitmap bmap)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000479{
480 blk_t block, b;
Theodore Ts'o54434922003-12-07 01:28:50 -0500481 unsigned int j;
482 dgrp_t i;
483 unsigned long meta_bg, meta_bg_size;
484 int has_super;
485 unsigned int old_desc_blocks;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000486 errcode_t retval;
487
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500488 meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000489 block = fs->super->s_first_data_block;
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500490 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
491 old_desc_blocks = fs->super->s_first_meta_bg;
492 else
Theodore Ts'o9213a932004-12-24 01:34:29 -0500493 old_desc_blocks = fs->desc_blocks +
494 fs->super->s_reserved_gdt_blocks;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000495 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o64ad98a2005-01-26 10:03:56 -0500496 ext2fs_reserve_super_and_bgd(fs, i, bmap);
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500497
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000498 /*
499 * Mark the blocks used for the inode table
500 */
501 for (j = 0, b = fs->group_desc[i].bg_inode_table;
Theodore Ts'o54434922003-12-07 01:28:50 -0500502 j < (unsigned int) fs->inode_blocks_per_group;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000503 j++, b++)
504 ext2fs_mark_block_bitmap(bmap, b);
505
506 /*
507 * Mark block used for the block bitmap
508 */
509 ext2fs_mark_block_bitmap(bmap,
510 fs->group_desc[i].bg_block_bitmap);
Theodore Ts'o64ad98a2005-01-26 10:03:56 -0500511
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000512 /*
513 * Mark block used for the inode bitmap
514 */
515 ext2fs_mark_block_bitmap(bmap,
516 fs->group_desc[i].bg_inode_bitmap);
517 block += fs->super->s_blocks_per_group;
518 }
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000519 return 0;
520}
521
522/*
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500523 * This function checks to see if a particular block (either a
524 * superblock or a block group descriptor) overlaps with an inode or
525 * block bitmap block, or with the inode table.
526 */
527static void mark_fs_metablock(ext2_resize_t rfs,
528 ext2fs_block_bitmap meta_bmap,
529 int group, blk_t blk)
530{
531 ext2_filsys fs = rfs->new_fs;
532
533 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
534 ext2fs_mark_block_bitmap(fs->block_map, blk);
535
536 /*
537 * Check to see if we overlap with the inode or block bitmap,
538 * or the inode tables. If not, and the block is in use, then
539 * mark it as a block to be moved.
540 */
541 if (IS_BLOCK_BM(fs, group, blk)) {
542 FS_BLOCK_BM(fs, group) = 0;
543 rfs->needed_blocks++;
544 } else if (IS_INODE_BM(fs, group, blk)) {
545 FS_INODE_BM(fs, group) = 0;
546 rfs->needed_blocks++;
547 } else if (IS_INODE_TB(fs, group, blk)) {
548 FS_INODE_TB(fs, group) = 0;
549 rfs->needed_blocks++;
550 } else if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk) &&
551 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
552 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
553 rfs->needed_blocks++;
554 }
555}
556
557
558/*
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000559 * This routine marks and unmarks reserved blocks in the new block
560 * bitmap. It also determines which blocks need to be moved and
561 * places this information into the move_blocks bitmap.
562 */
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000563static errcode_t blocks_to_move(ext2_resize_t rfs)
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000564{
Theodore Ts'o54434922003-12-07 01:28:50 -0500565 int j, has_super;
566 dgrp_t i, max_groups;
567 blk_t blk, group_blk;
568 unsigned long old_blocks, new_blocks;
569 unsigned int meta_bg, meta_bg_size;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000570 errcode_t retval;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000571 ext2_filsys fs, old_fs;
572 ext2fs_block_bitmap meta_bmap;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000573
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000574 fs = rfs->new_fs;
575 old_fs = rfs->old_fs;
576 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
577 fs = rfs->old_fs;
578
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000579 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000580 &rfs->reserve_blocks);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000581 if (retval)
582 return retval;
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000583
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000584 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000585 &rfs->move_blocks);
586 if (retval)
587 return retval;
588
Theodore Ts'o64ad98a2005-01-26 10:03:56 -0500589 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
590 &meta_bmap);
591 if (retval)
592 return retval;
593
594 retval = mark_table_blocks(old_fs, meta_bmap);
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000595 if (retval)
596 return retval;
597
598 fs = rfs->new_fs;
599
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000600 /*
601 * If we're shrinking the filesystem, we need to move all of
602 * the blocks that don't fit any more
603 */
604 for (blk = fs->super->s_blocks_count;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000605 blk < old_fs->super->s_blocks_count; blk++) {
606 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
607 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
608 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000609 rfs->needed_blocks++;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000610 }
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000611 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
612 }
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000613
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500614 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
615 old_blocks = old_fs->super->s_first_meta_bg;
616 new_blocks = fs->super->s_first_meta_bg;
617 } else {
Theodore Ts'o9213a932004-12-24 01:34:29 -0500618 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
619 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500620 }
621
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000622 if (old_blocks == new_blocks) {
623 retval = 0;
624 goto errout;
625 }
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000626
Theodore Ts'o1333fe91998-09-03 00:26:49 +0000627 max_groups = fs->group_desc_count;
628 if (max_groups > old_fs->group_desc_count)
629 max_groups = old_fs->group_desc_count;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000630 group_blk = old_fs->super->s_first_data_block;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000631 /*
632 * If we're reducing the number of descriptor blocks, this
633 * makes life easy. :-) We just have to mark some extra
634 * blocks as free.
635 */
636 if (old_blocks > new_blocks) {
Theodore Ts'o1333fe91998-09-03 00:26:49 +0000637 for (i = 0; i < max_groups; i++) {
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000638 if (!ext2fs_bg_has_super(fs, i)) {
639 group_blk += fs->super->s_blocks_per_group;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000640 continue;
641 }
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000642 for (blk = group_blk+1+new_blocks;
643 blk < group_blk+1+old_blocks; blk++) {
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000644 ext2fs_unmark_block_bitmap(fs->block_map,
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000645 blk);
Theodore Ts'o052db4b1997-06-12 07:14:32 +0000646 rfs->needed_blocks--;
647 }
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000648 group_blk += fs->super->s_blocks_per_group;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000649 }
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000650 retval = 0;
651 goto errout;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000652 }
653 /*
654 * If we're increasing the number of descriptor blocks, life
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000655 * gets interesting....
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000656 */
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500657 meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
Theodore Ts'o1333fe91998-09-03 00:26:49 +0000658 for (i = 0; i < max_groups; i++) {
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500659 has_super = ext2fs_bg_has_super(fs, i);
660 if (has_super)
661 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000662
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500663 meta_bg = i / meta_bg_size;
664 if (!(fs->super->s_feature_incompat &
665 EXT2_FEATURE_INCOMPAT_META_BG) ||
666 (meta_bg < fs->super->s_first_meta_bg)) {
Theodore Ts'o424cb7b2003-03-06 12:22:52 -0500667 if (has_super) {
668 for (blk = group_blk+1;
669 blk < group_blk + 1 + new_blocks; blk++)
670 mark_fs_metablock(rfs, meta_bmap,
671 i, blk);
672 }
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500673 } else {
674 if (has_super)
675 has_super = 1;
676 if (((i % meta_bg_size) == 0) ||
677 ((i % meta_bg_size) == 1) ||
678 ((i % meta_bg_size) == (meta_bg_size-1)))
679 mark_fs_metablock(rfs, meta_bmap, i,
680 group_blk + has_super);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000681 }
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500682
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000683 if (fs->group_desc[i].bg_inode_table &&
684 fs->group_desc[i].bg_inode_bitmap &&
685 fs->group_desc[i].bg_block_bitmap)
686 goto next_group;
687
688 /*
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000689 * Reserve the existing meta blocks that we know
690 * aren't to be moved.
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000691 */
692 if (fs->group_desc[i].bg_block_bitmap)
693 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
694 fs->group_desc[i].bg_block_bitmap);
695 if (fs->group_desc[i].bg_inode_bitmap)
696 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
697 fs->group_desc[i].bg_inode_bitmap);
698 if (fs->group_desc[i].bg_inode_table)
699 for (blk = fs->group_desc[i].bg_inode_table, j=0;
700 j < fs->inode_blocks_per_group ; j++, blk++)
701 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
702 blk);
703
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000704 /*
705 * Allocate the missing data structures
706 */
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000707 retval = ext2fs_allocate_group_table(fs, i,
708 rfs->reserve_blocks);
709 if (retval)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000710 goto errout;
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000711
712 /*
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000713 * For those structures that have changed, we need to
714 * do bookkeepping.
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000715 */
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000716 if (FS_BLOCK_BM(old_fs, i) !=
717 (blk = FS_BLOCK_BM(fs, i))) {
718 ext2fs_mark_block_bitmap(fs->block_map, blk);
719 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
720 !ext2fs_test_block_bitmap(meta_bmap, blk))
721 ext2fs_mark_block_bitmap(rfs->move_blocks,
722 blk);
723 }
724 if (FS_INODE_BM(old_fs, i) !=
725 (blk = FS_INODE_BM(fs, i))) {
726 ext2fs_mark_block_bitmap(fs->block_map, blk);
727 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
728 !ext2fs_test_block_bitmap(meta_bmap, blk))
729 ext2fs_mark_block_bitmap(rfs->move_blocks,
730 blk);
731 }
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000732
Theodore Ts'o052db4b1997-06-12 07:14:32 +0000733 /*
734 * The inode table, if we need to relocate it, is
735 * handled specially. We have to reserve the blocks
736 * for both the old and the new inode table, since we
737 * can't have the inode table be destroyed during the
738 * block relocation phase.
739 */
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000740 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
Theodore Ts'o052db4b1997-06-12 07:14:32 +0000741 goto next_group; /* inode table not moved */
742
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000743 rfs->needed_blocks += fs->inode_blocks_per_group;
Theodore Ts'o052db4b1997-06-12 07:14:32 +0000744
745 /*
746 * Mark the new inode table as in use in the new block
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000747 * allocation bitmap, and move any blocks that might
748 * be necessary.
Theodore Ts'o052db4b1997-06-12 07:14:32 +0000749 */
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000750 for (blk = fs->group_desc[i].bg_inode_table, j=0;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000751 j < fs->inode_blocks_per_group ; j++, blk++) {
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000752 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000753 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
754 !ext2fs_test_block_bitmap(meta_bmap, blk))
755 ext2fs_mark_block_bitmap(rfs->move_blocks,
756 blk);
757 }
758
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000759 /*
Theodore Ts'o052db4b1997-06-12 07:14:32 +0000760 * Make sure the old inode table is reserved in the
761 * block reservation bitmap.
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000762 */
Theodore Ts'o052db4b1997-06-12 07:14:32 +0000763 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
764 j < fs->inode_blocks_per_group ; j++, blk++)
765 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000766
767 next_group:
768 group_blk += rfs->new_fs->super->s_blocks_per_group;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000769 }
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000770 retval = 0;
771
772errout:
773 if (meta_bmap)
774 ext2fs_free_block_bitmap(meta_bmap);
775
776 return retval;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000777}
778
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000779/*
780 * This helper function tries to allocate a new block. We try to
781 * avoid hitting the original group descriptor blocks at least at
782 * first, since we want to make it possible to recover from a badly
783 * aborted resize operation as much as possible.
784 *
785 * In the future, I may further modify this routine to balance out
786 * where we get the new blocks across the various block groups.
787 * Ideally we would allocate blocks that corresponded with the block
788 * group of the containing inode, and keep contiguous blocks
789 * together. However, this very difficult to do efficiently, since we
790 * don't have the necessary information up front.
791 */
792
793#define AVOID_OLD 1
794#define DESPERATION 2
795
796static void init_block_alloc(ext2_resize_t rfs)
797{
798 rfs->alloc_state = AVOID_OLD;
799 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
Theodore Ts'o2bc4d4f1998-03-21 03:27:48 +0000800#if 0
801 /* HACK for testing */
802 if (rfs->new_fs->super->s_blocks_count >
803 rfs->old_fs->super->s_blocks_count)
804 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
805#endif
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000806}
807
808static blk_t get_new_block(ext2_resize_t rfs)
809{
810 ext2_filsys fs = rfs->new_fs;
811
812 while (1) {
813 if (rfs->new_blk >= fs->super->s_blocks_count) {
814 if (rfs->alloc_state == DESPERATION)
815 return 0;
816
817#ifdef RESIZE2FS_DEBUG
818 if (rfs->flags & RESIZE_DEBUG_BMOVE)
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000819 printf(_("Going into desperation "
820 "mode for block allocations\n"));
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000821#endif
822 rfs->alloc_state = DESPERATION;
823 rfs->new_blk = fs->super->s_first_data_block;
824 continue;
825 }
826 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
827 ext2fs_test_block_bitmap(rfs->reserve_blocks,
828 rfs->new_blk) ||
829 ((rfs->alloc_state == AVOID_OLD) &&
Theodore Ts'obce49791998-03-07 23:24:01 +0000830 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000831 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
832 rfs->new_blk))) {
833 rfs->new_blk++;
834 continue;
835 }
836 return rfs->new_blk;
837 }
838}
839
840static errcode_t block_mover(ext2_resize_t rfs)
841{
842 blk_t blk, old_blk, new_blk;
843 ext2_filsys fs = rfs->new_fs;
844 ext2_filsys old_fs = rfs->old_fs;
845 errcode_t retval;
846 int size, c;
847 int to_move, moved;
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400848 ext2_badblocks_list badblock_list = 0;
849 int bb_modified = 0;
850
851 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
852 if (retval)
853 return retval;
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000854
855 new_blk = fs->super->s_first_data_block;
856 if (!rfs->itable_buf) {
857 retval = ext2fs_get_mem(fs->blocksize *
858 fs->inode_blocks_per_group,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400859 &rfs->itable_buf);
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000860 if (retval)
861 return retval;
862 }
863 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
864 if (retval)
865 return retval;
866
867 /*
868 * The first step is to figure out where all of the blocks
869 * will go.
870 */
871 to_move = moved = 0;
872 init_block_alloc(rfs);
873 for (blk = old_fs->super->s_first_data_block;
874 blk < old_fs->super->s_blocks_count; blk++) {
875 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
876 continue;
877 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
878 continue;
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400879 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
880 ext2fs_badblocks_list_del(badblock_list, blk);
881 bb_modified++;
882 continue;
883 }
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000884
885 new_blk = get_new_block(rfs);
886 if (!new_blk) {
887 retval = ENOSPC;
888 goto errout;
889 }
890 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
891 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
892 to_move++;
893 }
894
895 if (to_move == 0) {
Theodore Ts'ocefbf482002-07-26 01:56:22 -0400896 if (rfs->bmap) {
897 ext2fs_free_extent_table(rfs->bmap);
898 rfs->bmap = 0;
899 }
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000900 retval = 0;
901 goto errout;
902 }
903
904 /*
905 * Step two is to actually move the blocks
906 */
907 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
908 if (retval) goto errout;
909
Theodore Ts'o3b627e81998-02-24 20:24:49 +0000910 if (rfs->progress) {
911 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
912 0, to_move);
913 if (retval)
914 goto errout;
915 }
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000916 while (1) {
917 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
918 if (retval) goto errout;
919 if (!size)
920 break;
921#ifdef RESIZE2FS_DEBUG
922 if (rfs->flags & RESIZE_DEBUG_BMOVE)
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000923 printf(_("Moving %d blocks %u->%u\n"), size,
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000924 old_blk, new_blk);
925#endif
926 do {
927 c = size;
928 if (c > fs->inode_blocks_per_group)
929 c = fs->inode_blocks_per_group;
930 retval = io_channel_read_blk(fs->io, old_blk, c,
931 rfs->itable_buf);
932 if (retval) goto errout;
933 retval = io_channel_write_blk(fs->io, new_blk, c,
934 rfs->itable_buf);
935 if (retval) goto errout;
936 size -= c;
937 new_blk += c;
938 old_blk += c;
939 moved += c;
940 if (rfs->progress) {
941 io_channel_flush(fs->io);
Theodore Ts'o3b627e81998-02-24 20:24:49 +0000942 retval = (rfs->progress)(rfs,
943 E2_RSZ_BLOCK_RELOC_PASS,
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000944 moved, to_move);
Theodore Ts'o3b627e81998-02-24 20:24:49 +0000945 if (retval)
946 goto errout;
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000947 }
948 } while (size > 0);
949 io_channel_flush(fs->io);
950 }
951
952errout:
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400953 if (badblock_list) {
954 if (!retval && bb_modified)
955 retval = ext2fs_update_bb_inode(old_fs,
956 badblock_list);
957 ext2fs_badblocks_list_free(badblock_list);
958 }
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000959 return retval;
960}
961
962
963/* --------------------------------------------------------------------
964 *
965 * Resize processing, phase 3
966 *
967 * --------------------------------------------------------------------
968 */
969
970
971struct process_block_struct {
972 ext2_resize_t rfs;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000973 ext2_ino_t ino;
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000974 struct ext2_inode * inode;
975 errcode_t error;
976 int is_dir;
977 int changed;
978};
979
980static int process_block(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'o54434922003-12-07 01:28:50 -0500981 e2_blkcnt_t blockcnt,
982 blk_t ref_block EXT2FS_ATTR((unused)),
983 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000984{
985 struct process_block_struct *pb;
986 errcode_t retval;
987 blk_t block, new_block;
988 int ret = 0;
989
990 pb = (struct process_block_struct *) priv_data;
991 block = *block_nr;
992 if (pb->rfs->bmap) {
993 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
994 if (new_block) {
995 *block_nr = new_block;
996 ret |= BLOCK_CHANGED;
997 pb->changed = 1;
998#ifdef RESIZE2FS_DEBUG
999 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
Theodore Ts'o546a1ff2002-03-07 23:52:56 -05001000 printf(_("ino=%u, blockcnt=%lld, %u->%u\n"),
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001001 pb->ino, blockcnt, block, new_block);
1002#endif
1003 block = new_block;
1004 }
1005 }
1006 if (pb->is_dir) {
1007 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
Theodore Ts'o101c84f1998-03-24 16:27:11 +00001008 block, (int) blockcnt);
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001009 if (retval) {
1010 pb->error = retval;
1011 ret |= BLOCK_ABORT;
1012 }
1013 }
1014 return ret;
1015}
1016
1017/*
1018 * Progress callback
1019 */
Theodore Ts'o54434922003-12-07 01:28:50 -05001020static errcode_t progress_callback(ext2_filsys fs,
1021 ext2_inode_scan scan EXT2FS_ATTR((unused)),
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001022 dgrp_t group, void * priv_data)
1023{
1024 ext2_resize_t rfs = (ext2_resize_t) priv_data;
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001025 errcode_t retval;
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001026
1027 /*
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +00001028 * This check is to protect against old ext2 libraries. It
1029 * shouldn't be needed against new libraries.
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001030 */
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +00001031 if ((group+1) == 0)
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001032 return 0;
1033
1034 if (rfs->progress) {
1035 io_channel_flush(fs->io);
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001036 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1037 group+1, fs->group_desc_count);
1038 if (retval)
1039 return retval;
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001040 }
1041
1042 return 0;
1043}
1044
1045static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1046{
1047 struct process_block_struct pb;
Theodore Ts'odfcdc322001-01-11 15:38:00 +00001048 ext2_ino_t ino, new_inode;
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001049 struct ext2_inode inode;
1050 ext2_inode_scan scan = NULL;
1051 errcode_t retval;
1052 int group;
1053 char *block_buf = 0;
Theodore Ts'odfcdc322001-01-11 15:38:00 +00001054 ext2_ino_t start_to_move;
Theodore Ts'o0ccd4882002-08-16 17:07:06 -04001055 blk_t orig_size, new_block;
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001056
1057 if ((rfs->old_fs->group_desc_count <=
1058 rfs->new_fs->group_desc_count) &&
1059 !rfs->bmap)
1060 return 0;
1061
Theodore Ts'o2bc4d4f1998-03-21 03:27:48 +00001062 /*
1063 * Save the original size of the old filesystem, and
1064 * temporarily set the size to be the new size if the new size
1065 * is larger. We need to do this to avoid catching an error
1066 * by the block iterator routines
1067 */
1068 orig_size = rfs->old_fs->super->s_blocks_count;
1069 if (orig_size < rfs->new_fs->super->s_blocks_count)
1070 rfs->old_fs->super->s_blocks_count =
1071 rfs->new_fs->super->s_blocks_count;
1072
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001073 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1074 if (retval) goto errout;
1075
1076 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1077 if (retval) goto errout;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -04001078 retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3, &block_buf);
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001079 if (retval) goto errout;
1080
1081 start_to_move = (rfs->new_fs->group_desc_count *
1082 rfs->new_fs->super->s_inodes_per_group);
1083
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001084 if (rfs->progress) {
1085 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1086 0, rfs->old_fs->group_desc_count);
1087 if (retval)
1088 goto errout;
1089 }
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001090 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1091 pb.rfs = rfs;
1092 pb.inode = &inode;
1093 pb.error = 0;
1094 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1095 /*
1096 * First, copy all of the inodes that need to be moved
1097 * elsewhere in the inode table
1098 */
1099 while (1) {
1100 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1101 if (retval) goto errout;
1102 if (!ino)
1103 break;
1104
Theodore Ts'o9213a932004-12-24 01:34:29 -05001105 if (inode.i_links_count == 0 && ino != EXT2_RESIZE_INO)
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001106 continue; /* inode not in use */
1107
1108 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
1109 pb.changed = 0;
1110
Theodore Ts'o0ccd4882002-08-16 17:07:06 -04001111 if (inode.i_file_acl && rfs->bmap) {
1112 new_block = ext2fs_extent_translate(rfs->bmap,
Theodore Ts'oed909bb2002-08-16 17:03:59 -04001113 inode.i_file_acl);
1114 if (new_block) {
1115 inode.i_file_acl = new_block;
1116 retval = ext2fs_write_inode(rfs->old_fs,
1117 ino, &inode);
1118 if (retval) goto errout;
1119 }
1120 }
1121
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001122 if (ext2fs_inode_has_valid_blocks(&inode) &&
1123 (rfs->bmap || pb.is_dir)) {
1124 pb.ino = ino;
1125 retval = ext2fs_block_iterate2(rfs->old_fs,
1126 ino, 0, block_buf,
1127 process_block, &pb);
1128 if (retval)
1129 goto errout;
1130 if (pb.error) {
1131 retval = pb.error;
1132 goto errout;
1133 }
1134 }
1135
1136 if (ino <= start_to_move)
1137 continue; /* Don't need to move it. */
1138
1139 /*
1140 * Find a new inode
1141 */
1142 while (1) {
1143 if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
1144 new_inode))
1145 break;
1146 new_inode++;
1147 if (new_inode > rfs->new_fs->super->s_inodes_count) {
1148 retval = ENOSPC;
1149 goto errout;
1150 }
1151 }
1152 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1153 if (pb.changed) {
1154 /* Get the new version of the inode */
1155 retval = ext2fs_read_inode(rfs->old_fs, ino, &inode);
1156 if (retval) goto errout;
1157 }
Theodore Ts'o085d2a82002-10-31 19:56:56 -05001158 inode.i_ctime = time(0);
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001159 retval = ext2fs_write_inode(rfs->old_fs, new_inode, &inode);
1160 if (retval) goto errout;
1161
1162 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1163 if (LINUX_S_ISDIR(inode.i_mode))
1164 rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1165
1166#ifdef RESIZE2FS_DEBUG
1167 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
Theodore Ts'o546a1ff2002-03-07 23:52:56 -05001168 printf(_("Inode moved %u->%u\n"), ino, new_inode);
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001169#endif
1170 if (!rfs->imap) {
1171 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1172 if (retval)
1173 goto errout;
1174 }
1175 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1176 }
1177 io_channel_flush(rfs->old_fs->io);
1178
1179errout:
Theodore Ts'o2bc4d4f1998-03-21 03:27:48 +00001180 rfs->old_fs->super->s_blocks_count = orig_size;
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001181 if (rfs->bmap) {
1182 ext2fs_free_extent_table(rfs->bmap);
1183 rfs->bmap = 0;
1184 }
1185 if (scan)
1186 ext2fs_close_inode_scan(scan);
1187 if (block_buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -04001188 ext2fs_free_mem(&block_buf);
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001189 return retval;
1190}
1191
1192/* --------------------------------------------------------------------
1193 *
1194 * Resize processing, phase 4.
1195 *
1196 * --------------------------------------------------------------------
1197 */
1198
1199struct istruct {
1200 ext2_resize_t rfs;
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001201 errcode_t err;
Theodore Ts'o1333fe91998-09-03 00:26:49 +00001202 unsigned long max_dirs;
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001203 int num;
1204};
1205
Theodore Ts'o54434922003-12-07 01:28:50 -05001206static int check_and_change_inodes(ext2_ino_t dir,
1207 int entry EXT2FS_ATTR((unused)),
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001208 struct ext2_dir_entry *dirent, int offset,
Theodore Ts'o54434922003-12-07 01:28:50 -05001209 int blocksize EXT2FS_ATTR((unused)),
1210 char *buf EXT2FS_ATTR((unused)),
1211 void *priv_data)
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001212{
1213 struct istruct *is = (struct istruct *) priv_data;
Theodore Ts'o085d2a82002-10-31 19:56:56 -05001214 struct ext2_inode inode;
1215 ext2_ino_t new_inode;
1216 errcode_t retval;
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001217
1218 if (is->rfs->progress && offset == 0) {
1219 io_channel_flush(is->rfs->old_fs->io);
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001220 is->err = (is->rfs->progress)(is->rfs,
1221 E2_RSZ_INODE_REF_UPD_PASS,
Theodore Ts'o1333fe91998-09-03 00:26:49 +00001222 ++is->num, is->max_dirs);
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001223 if (is->err)
1224 return DIRENT_ABORT;
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001225 }
1226
1227 if (!dirent->inode)
1228 return 0;
1229
1230 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1231
1232 if (!new_inode)
1233 return 0;
1234#ifdef RESIZE2FS_DEBUG
1235 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
Theodore Ts'o546a1ff2002-03-07 23:52:56 -05001236 printf(_("Inode translate (dir=%u, name=%.*s, %u->%u)\n"),
Theodore Ts'o06191692004-09-17 17:10:17 -04001237 dir, dirent->name_len&0xFF, dirent->name,
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001238 dirent->inode, new_inode);
1239#endif
1240
1241 dirent->inode = new_inode;
1242
Theodore Ts'o085d2a82002-10-31 19:56:56 -05001243 /* Update the directory mtime and ctime */
1244 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1245 if (retval == 0) {
1246 inode.i_mtime = inode.i_ctime = time(0);
1247 ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1248 }
1249
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001250 return DIRENT_CHANGED;
1251}
1252
1253static errcode_t inode_ref_fix(ext2_resize_t rfs)
1254{
1255 errcode_t retval;
1256 struct istruct is;
1257
1258 if (!rfs->imap)
1259 return 0;
1260
1261 /*
1262 * Now, we iterate over all of the directories to update the
1263 * inode references
1264 */
1265 is.num = 0;
Theodore Ts'o1333fe91998-09-03 00:26:49 +00001266 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001267 is.rfs = rfs;
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001268 is.err = 0;
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001269
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001270 if (rfs->progress) {
1271 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
Theodore Ts'o1333fe91998-09-03 00:26:49 +00001272 0, is.max_dirs);
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001273 if (retval)
1274 goto errout;
1275 }
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001276
1277 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1278 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1279 check_and_change_inodes, &is);
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001280 if (retval)
1281 goto errout;
1282 if (is.err) {
1283 retval = is.err;
1284 goto errout;
1285 }
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001286
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001287errout:
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001288 ext2fs_free_extent_table(rfs->imap);
1289 rfs->imap = 0;
1290 return retval;
1291}
1292
1293
1294/* --------------------------------------------------------------------
1295 *
1296 * Resize processing, phase 5.
1297 *
1298 * In this phase we actually move the inode table around, and then
1299 * update the summary statistics. This is scary, since aborting here
1300 * will potentially scramble the filesystem. (We are moving the
1301 * inode tables around in place, and so the potential for lost data,
1302 * or at the very least scrambling the mapping between filenames and
1303 * inode numbers is very high in case of a power failure here.)
1304 * --------------------------------------------------------------------
1305 */
1306
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +00001307
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +00001308/*
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001309 * A very scary routine --- this one moves the inode table around!!!
1310 *
1311 * After this you have to use the rfs->new_fs file handle to read and
1312 * write inodes.
1313 */
Theodore Ts'oc762c8e1997-06-17 03:52:12 +00001314static errcode_t move_itables(ext2_resize_t rfs)
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001315{
Theodore Ts'o54434922003-12-07 01:28:50 -05001316 int n, num, size, diff;
1317 dgrp_t i, max_groups;
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001318 ext2_filsys fs = rfs->new_fs;
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001319 char *cp;
Theodore Ts'o64ad98a2005-01-26 10:03:56 -05001320 blk_t old_blk, new_blk, blk;
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001321 errcode_t retval;
Theodore Ts'o64ad98a2005-01-26 10:03:56 -05001322 int j, to_move, moved;
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001323
Theodore Ts'o1333fe91998-09-03 00:26:49 +00001324 max_groups = fs->group_desc_count;
1325 if (max_groups > rfs->old_fs->group_desc_count)
1326 max_groups = rfs->old_fs->group_desc_count;
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001327
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001328 size = fs->blocksize * fs->inode_blocks_per_group;
1329 if (!rfs->itable_buf) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -04001330 retval = ext2fs_get_mem(size, &rfs->itable_buf);
Theodore Ts'oca8abba1998-01-19 14:55:24 +00001331 if (retval)
1332 return retval;
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001333 }
Theodore Ts'oc762c8e1997-06-17 03:52:12 +00001334
1335 /*
1336 * Figure out how many inode tables we need to move
1337 */
1338 to_move = moved = 0;
Theodore Ts'o1333fe91998-09-03 00:26:49 +00001339 for (i=0; i < max_groups; i++)
Theodore Ts'oc762c8e1997-06-17 03:52:12 +00001340 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1341 fs->group_desc[i].bg_inode_table)
1342 to_move++;
1343
1344 if (to_move == 0)
1345 return 0;
1346
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001347 if (rfs->progress) {
1348 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1349 0, to_move);
1350 if (retval)
1351 goto errout;
1352 }
Theodore Ts'o63b44fb1998-02-13 22:58:18 +00001353
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001354 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1355
Theodore Ts'o1333fe91998-09-03 00:26:49 +00001356 for (i=0; i < max_groups; i++) {
Theodore Ts'oca8abba1998-01-19 14:55:24 +00001357 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1358 new_blk = fs->group_desc[i].bg_inode_table;
1359 diff = new_blk - old_blk;
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001360
Theodore Ts'o80c0fc31997-11-03 19:46:49 +00001361#ifdef RESIZE2FS_DEBUG
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001362 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
Theodore Ts'oa13575f2000-06-12 22:06:16 +00001363 printf(_("Itable move group %d block "
1364 "%u->%u (diff %d)\n"),
Theodore Ts'oca8abba1998-01-19 14:55:24 +00001365 i, old_blk, new_blk, diff);
Theodore Ts'o80c0fc31997-11-03 19:46:49 +00001366#endif
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001367
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001368 if (!diff)
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001369 continue;
1370
Theodore Ts'oca8abba1998-01-19 14:55:24 +00001371 retval = io_channel_read_blk(fs->io, old_blk,
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001372 fs->inode_blocks_per_group,
1373 rfs->itable_buf);
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001374 if (retval)
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001375 goto errout;
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001376 /*
1377 * The end of the inode table segment often contains
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001378 * all zeros, and we're often only moving the inode
1379 * table down a block or two. If so, we can optimize
1380 * things by not rewriting blocks that we know to be zero
1381 * already.
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001382 */
1383 for (cp = rfs->itable_buf+size, n=0; n < size; n++, cp--)
1384 if (*cp)
1385 break;
1386 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
Theodore Ts'o80c0fc31997-11-03 19:46:49 +00001387#ifdef RESIZE2FS_DEBUG
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001388 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
Theodore Ts'oa13575f2000-06-12 22:06:16 +00001389 printf(_("%d blocks of zeros...\n"), n);
Theodore Ts'o80c0fc31997-11-03 19:46:49 +00001390#endif
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001391 num = fs->inode_blocks_per_group;
1392 if (n > diff)
1393 num -= n;
1394
Theodore Ts'oca8abba1998-01-19 14:55:24 +00001395 retval = io_channel_write_blk(fs->io, new_blk,
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001396 num, rfs->itable_buf);
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001397 if (retval) {
Theodore Ts'oca8abba1998-01-19 14:55:24 +00001398 io_channel_write_blk(fs->io, old_blk,
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001399 num, rfs->itable_buf);
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001400 goto errout;
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001401 }
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001402 if (n > diff) {
1403 retval = io_channel_write_blk(fs->io,
Theodore Ts'oca8abba1998-01-19 14:55:24 +00001404 old_blk + fs->inode_blocks_per_group,
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001405 diff, (rfs->itable_buf +
1406 (fs->inode_blocks_per_group - diff) *
1407 fs->blocksize));
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001408 if (retval)
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001409 goto errout;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +00001410 }
Theodore Ts'o64ad98a2005-01-26 10:03:56 -05001411
1412 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1413 j < fs->inode_blocks_per_group ; j++, blk++)
1414 ext2fs_unmark_block_bitmap(fs->block_map, blk);
1415
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001416 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1417 ext2fs_mark_super_dirty(rfs->old_fs);
Theodore Ts'o64ad98a2005-01-26 10:03:56 -05001418 ext2fs_flush(rfs->old_fs);
1419
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001420 if (rfs->progress) {
Theodore Ts'o3b627e81998-02-24 20:24:49 +00001421 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1422 ++moved, to_move);
1423 if (retval)
1424 goto errout;
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001425 }
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001426 }
Theodore Ts'o64ad98a2005-01-26 10:03:56 -05001427 mark_table_blocks(fs, fs->block_map);
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001428 ext2fs_flush(fs);
Theodore Ts'o80c0fc31997-11-03 19:46:49 +00001429#ifdef RESIZE2FS_DEBUG
Theodore Ts'o05e112a1997-06-14 07:28:44 +00001430 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
Theodore Ts'oa13575f2000-06-12 22:06:16 +00001431 printf(_("Inode table move finished.\n"));
Theodore Ts'o80c0fc31997-11-03 19:46:49 +00001432#endif
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001433 return 0;
1434
Theodore Ts'oa8519a21998-02-16 22:16:20 +00001435errout:
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001436 return retval;
1437}
1438
1439/*
Theodore Ts'o9213a932004-12-24 01:34:29 -05001440 * Fix the resize inode
1441 */
1442static errcode_t fix_resize_inode(ext2_filsys fs)
1443{
1444 struct ext2_inode inode;
1445 errcode_t retval;
1446 char * block_buf;
1447
1448 if (!(fs->super->s_feature_compat &
1449 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1450 return 0;
1451
1452 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1453 if (retval) goto errout;
1454
1455 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1456 if (retval) goto errout;
1457
1458 inode.i_blocks = fs->blocksize/512;
1459
1460 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1461 if (retval) goto errout;
1462
1463 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1464 /*
1465 * Avoid zeroing out block #0; that's rude. This
1466 * should never happen anyway since the filesystem
1467 * should be fsck'ed and we assume it is consistent.
1468 */
1469 fprintf(stderr,
1470 _("Should never happen resize inode corrupt!\n"));
1471 exit(1);
1472 }
1473
1474 memset(block_buf, 0, fs->blocksize);
1475
1476 retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1477 1, block_buf);
1478 if (retval) goto errout;
1479
1480 retval = ext2fs_create_resize_inode(fs);
1481 if (retval)
1482 goto errout;
1483
1484errout:
1485 if (block_buf)
1486 ext2fs_free_mem(&block_buf);
1487 return retval;
1488}
1489
1490/*
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001491 * Finally, recalculate the summary information
1492 */
1493static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1494{
Theodore Ts'odfcdc322001-01-11 15:38:00 +00001495 blk_t blk;
1496 ext2_ino_t ino;
Theodore Ts'o54434922003-12-07 01:28:50 -05001497 unsigned int group = 0;
1498 unsigned int count = 0;
Theodore Ts'odfcdc322001-01-11 15:38:00 +00001499 int total_free = 0;
1500 int group_free = 0;
Theodore Ts'o052db4b1997-06-12 07:14:32 +00001501
1502 /*
1503 * First calculate the block statistics
1504 */
1505 for (blk = fs->super->s_first_data_block;
1506 blk < fs->super->s_blocks_count; blk++) {
1507 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1508 group_free++;
1509 total_free++;
1510 }
1511 count++;
1512 if ((count == fs->super->s_blocks_per_group) ||
1513 (blk == fs->super->s_blocks_count-1)) {
1514 fs->group_desc[group++].bg_free_blocks_count =
1515 group_free;
1516 count = 0;
1517 group_free = 0;
1518 }
1519 }
1520 fs->super->s_free_blocks_count = total_free;
1521
1522 /*
1523 * Next, calculate the inode statistics
1524 */
1525 group_free = 0;
1526 total_free = 0;
1527 count = 0;
1528 group = 0;
1529 for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
1530 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1531 group_free++;
1532 total_free++;
1533 }
1534 count++;
1535 if ((count == fs->super->s_inodes_per_group) ||
1536 (ino == fs->super->s_inodes_count)) {
1537 fs->group_desc[group++].bg_free_inodes_count =
1538 group_free;
1539 count = 0;
1540 group_free = 0;
1541 }
1542 }
1543 fs->super->s_free_inodes_count = total_free;
1544 ext2fs_mark_super_dirty(fs);
1545 return 0;
1546}