]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - resize/resize2fs.c
Makefile.in (check): Use LD_LIBRARY_PATH to run test programs.
[thirdparty/e2fsprogs.git] / resize / resize2fs.c
CommitLineData
24b2c7a7
TT
1/*
2 * resize2fs.c --- ext2 main routine
3 *
0cee8a5c
TT
4 * Copyright (C) 1997, 1998 by Theodore Ts'o and
5 * PowerQuest, Inc.
6 *
7 * Copyright (C) 1999, 2000 by Theosore Ts'o
24b2c7a7
TT
8 *
9 * %Begin-Header%
0cee8a5c
TT
10 * This file may be redistributed under the terms of the GNU Public
11 * License.
24b2c7a7
TT
12 * %End-Header%
13 */
14
05e112a1
TT
15/*
16 * Resizing a filesystem consists of the following phases:
17 *
a8519a2d 18 * 1. Adjust superblock and write out new parts of the inode
05e112a1 19 * table
a8519a2d
TT
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
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.
05e112a1 34 */
a8519a2d 35
24b2c7a7
TT
36#include "resize2fs.h"
37
a8519a2d
TT
38#ifdef linux /* Kludge for debugging */
39#define RESIZE2FS_DEBUG
40#endif
41
42static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size);
43static errcode_t blocks_to_move(ext2_resize_t rfs);
44static errcode_t block_mover(ext2_resize_t rfs);
45static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
46static errcode_t inode_ref_fix(ext2_resize_t rfs);
47static errcode_t move_itables(ext2_resize_t rfs);
48static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
49
50/*
51 * Some helper CPP macros
52 */
53#define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap)
54#define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap)
55#define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table)
56
57#define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i)))
58#define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i)))
59
60#define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \
61 ((blk) < (FS_INODE_TB((fs), (i)) + \
62 (fs)->inode_blocks_per_group)))
63
64
65
66/*
67 * This is the top-level routine which does the dirty deed....
68 */
69errcode_t resize_fs(ext2_filsys fs, blk_t new_size, int flags,
3b627e8d 70 errcode_t (*progress)(ext2_resize_t rfs, int pass,
a8519a2d 71 unsigned long cur,
1333fe93 72 unsigned long max_val))
a8519a2d
TT
73{
74 ext2_resize_t rfs;
75 errcode_t retval;
76
77 retval = ext2fs_read_bitmaps(fs);
78 if (retval)
79 return retval;
80
81 /*
82 * Create the data structure
83 */
84 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct),
85 (void **) &rfs);
86 if (retval)
87 return retval;
88 memset(rfs, 0, sizeof(struct ext2_resize_struct));
89
90 rfs->old_fs = fs;
91 rfs->flags = flags;
92 rfs->itable_buf = 0;
93 rfs->progress = progress;
94 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
95 if (retval)
96 goto errout;
97
98 retval = adjust_superblock(rfs, new_size);
99 if (retval)
100 goto errout;
101
102 retval = blocks_to_move(rfs);
103 if (retval)
104 goto errout;
105
106#ifdef RESIZE2FS_DEBUG
107 if (rfs->flags & RESIZE_DEBUG_BMOVE)
a13575f4 108 printf(_("Number of free blocks: %d/%d, Needed: %d\n"),
a8519a2d
TT
109 rfs->old_fs->super->s_free_blocks_count,
110 rfs->new_fs->super->s_free_blocks_count,
111 rfs->needed_blocks);
112#endif
113
114 retval = block_mover(rfs);
115 if (retval)
116 goto errout;
117
118 retval = inode_scan_and_fix(rfs);
119 if (retval)
120 goto errout;
121
122 retval = inode_ref_fix(rfs);
123 if (retval)
124 goto errout;
125
126 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
127 if (retval)
128 goto errout;
129
130 retval = move_itables(rfs);
131 if (retval)
132 goto errout;
133
134 retval = ext2fs_close(rfs->new_fs);
135 if (retval)
136 goto errout;
137
138 rfs->flags = flags;
139
140 ext2fs_free(rfs->old_fs);
141 if (rfs->itable_buf)
142 ext2fs_free_mem((void **) &rfs->itable_buf);
143 ext2fs_free_mem((void **) &rfs);
144
145 return 0;
146
147errout:
148 if (rfs->new_fs)
149 ext2fs_free(rfs->new_fs);
150 if (rfs->itable_buf)
151 ext2fs_free_mem((void **) &rfs->itable_buf);
152 ext2fs_free_mem((void **) &rfs);
153 return retval;
154}
155
156/* --------------------------------------------------------------------
157 *
158 * Resize processing, phase 1.
159 *
160 * In this phase we adjust the in-memory superblock information, and
161 * initialize any new parts of the inode table. The new parts of the
162 * inode table are created in virgin disk space, so we can abort here
163 * without any side effects.
164 * --------------------------------------------------------------------
165 */
166
24b2c7a7
TT
167/*
168 * This routine adjusts the superblock and other data structures...
169 */
170static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
171{
172 ext2_filsys fs;
173 int overhead = 0;
c762c8e6 174 int rem, adj = 0;
24b2c7a7 175 errcode_t retval;
dfcdc32f 176 ext2_ino_t real_end;
24b2c7a7 177 blk_t blk, group_block;
1e1da29f 178 unsigned long i, j;
1e1da29f 179 int old_numblocks, numblocks, adjblocks;
63b44fbe 180 unsigned long max_group;
24b2c7a7
TT
181
182 fs = rfs->new_fs;
183 fs->super->s_blocks_count = new_size;
1e1da29f
TT
184 ext2fs_mark_super_dirty(fs);
185 ext2fs_mark_bb_dirty(fs);
186 ext2fs_mark_ib_dirty(fs);
24b2c7a7
TT
187
188retry:
189 fs->group_desc_count = (fs->super->s_blocks_count -
190 fs->super->s_first_data_block +
191 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
192 / EXT2_BLOCKS_PER_GROUP(fs->super);
193 if (fs->group_desc_count == 0)
194 return EXT2_ET_TOOSMALL;
195 fs->desc_blocks = (fs->group_desc_count +
196 EXT2_DESC_PER_BLOCK(fs->super) - 1)
197 / EXT2_DESC_PER_BLOCK(fs->super);
198
199 /*
200 * Overhead is the number of bookkeeping blocks per group. It
201 * includes the superblock backup, the group descriptor
202 * backups, the inode bitmap, the block bitmap, and the inode
203 * table.
204 *
205 * XXX Not all block groups need the descriptor blocks, but
206 * being clever is tricky...
207 */
208 overhead = 3 + fs->desc_blocks + fs->inode_blocks_per_group;
209
210 /*
211 * See if the last group is big enough to support the
212 * necessary data structures. If not, we need to get rid of
213 * it.
214 */
215 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
216 fs->super->s_blocks_per_group;
217 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
218 return EXT2_ET_TOOSMALL;
219 if (rem && (rem < overhead+50)) {
220 fs->super->s_blocks_count -= rem;
221 goto retry;
222 }
223 /*
224 * Adjust the number of inodes
225 */
226 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
227 fs->group_desc_count;
228
229 /*
230 * Adjust the number of free blocks
231 */
232 blk = rfs->old_fs->super->s_blocks_count;
233 if (blk > fs->super->s_blocks_count)
234 fs->super->s_free_blocks_count -=
235 (blk - fs->super->s_blocks_count);
236 else
237 fs->super->s_free_blocks_count +=
238 (fs->super->s_blocks_count - blk);
239
c762c8e6
TT
240 /*
241 * Adjust the number of reserved blocks
242 */
243 blk = rfs->old_fs->super->s_r_blocks_count * 100 /
244 rfs->old_fs->super->s_blocks_count;
245 fs->super->s_r_blocks_count = ((fs->super->s_blocks_count * blk)
246 / 100);
247
24b2c7a7
TT
248 /*
249 * Adjust the bitmaps for size
250 */
251 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
252 fs->super->s_inodes_count,
253 fs->inode_map);
c762c8e6 254 if (retval) goto errout;
24b2c7a7
TT
255
256 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
257 * fs->group_desc_count)) - 1 +
258 fs->super->s_first_data_block;
259 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
260 real_end, fs->block_map);
261
c762c8e6 262 if (retval) goto errout;
24b2c7a7
TT
263
264 /*
265 * Reallocate the group descriptors as necessary.
266 */
267 if (rfs->old_fs->desc_blocks != fs->desc_blocks) {
76f875da
TT
268 retval = ext2fs_resize_mem(rfs->old_fs->desc_blocks *
269 fs->blocksize,
270 fs->desc_blocks * fs->blocksize,
ca8abba7
TT
271 (void **) &fs->group_desc);
272 if (retval)
a8519a2d 273 goto errout;
24b2c7a7 274 }
1e1da29f
TT
275
276 /*
a8519a2d
TT
277 * Check to make sure there are enough inodes
278 */
279 if ((rfs->old_fs->super->s_inodes_count -
280 rfs->old_fs->super->s_free_inodes_count) >
281 rfs->new_fs->super->s_inodes_count) {
282 retval = ENOSPC;
283 goto errout;
284 }
285
286 /*
287 * If we are shrinking the number block groups, we're done and
288 * can exit now.
1e1da29f 289 */
c762c8e6
TT
290 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
291 retval = 0;
292 goto errout;
293 }
a8519a2d
TT
294 /*
295 * Fix the count of the last (old) block group
296 */
1e1da29f
TT
297 old_numblocks = (rfs->old_fs->super->s_blocks_count -
298 rfs->old_fs->super->s_first_data_block) %
299 rfs->old_fs->super->s_blocks_per_group;
300 if (!old_numblocks)
301 old_numblocks = rfs->old_fs->super->s_blocks_per_group;
302 if (rfs->old_fs->group_desc_count == fs->group_desc_count) {
303 numblocks = (rfs->new_fs->super->s_blocks_count -
304 rfs->new_fs->super->s_first_data_block) %
305 rfs->new_fs->super->s_blocks_per_group;
306 if (!numblocks)
307 numblocks = rfs->new_fs->super->s_blocks_per_group;
308 } else
309 numblocks = rfs->new_fs->super->s_blocks_per_group;
310 i = rfs->old_fs->group_desc_count - 1;
311 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
312
313 /*
a8519a2d
TT
314 * If the number of block groups is staying the same, we're
315 * done and can exit now. (If the number block groups is
316 * shrinking, we had exited earlier.)
1e1da29f 317 */
c762c8e6
TT
318 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
319 retval = 0;
320 goto errout;
321 }
a8519a2d
TT
322 /*
323 * Initialize the new block group descriptors
324 */
ca8abba7
TT
325 retval = ext2fs_get_mem(fs->blocksize * fs->inode_blocks_per_group,
326 (void **) &rfs->itable_buf);
327 if (retval)
c762c8e6 328 goto errout;
ca8abba7 329
05e112a1 330 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
1e1da29f
TT
331 group_block = fs->super->s_first_data_block +
332 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
c762c8e6 333
63b44fbe
TT
334 adj = rfs->old_fs->group_desc_count;
335 max_group = fs->group_desc_count - adj;
3b627e8d
TT
336 if (rfs->progress) {
337 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
338 0, max_group);
339 if (retval)
340 goto errout;
341 }
1e1da29f
TT
342 for (i = rfs->old_fs->group_desc_count;
343 i < fs->group_desc_count; i++) {
344 memset(&fs->group_desc[i], 0,
345 sizeof(struct ext2_group_desc));
346 adjblocks = 0;
347
348 if (i == fs->group_desc_count-1) {
349 numblocks = (fs->super->s_blocks_count -
350 fs->super->s_first_data_block) %
351 fs->super->s_blocks_per_group;
352 if (!numblocks)
353 numblocks = fs->super->s_blocks_per_group;
354 } else
355 numblocks = fs->super->s_blocks_per_group;
356
357 if (ext2fs_bg_has_super(fs, i)) {
358 for (j=0; j < fs->desc_blocks+1; j++)
359 ext2fs_mark_block_bitmap(fs->block_map,
360 group_block + j);
361 adjblocks = 1 + fs->desc_blocks;
24b2c7a7 362 }
1e1da29f
TT
363 adjblocks += 2 + fs->inode_blocks_per_group;
364
365 numblocks -= adjblocks;
366 fs->super->s_free_blocks_count -= adjblocks;
367 fs->super->s_free_inodes_count +=
368 fs->super->s_inodes_per_group;
369 fs->group_desc[i].bg_free_blocks_count = numblocks;
370 fs->group_desc[i].bg_free_inodes_count =
371 fs->super->s_inodes_per_group;
372 fs->group_desc[i].bg_used_dirs_count = 0;
24b2c7a7 373
1e1da29f 374 retval = ext2fs_allocate_group_table(fs, i, 0);
c762c8e6 375 if (retval) goto errout;
24b2c7a7 376
05e112a1
TT
377 /*
378 * Write out the new inode table
379 */
380 retval = io_channel_write_blk(fs->io,
381 fs->group_desc[i].bg_inode_table,
382 fs->inode_blocks_per_group,
383 rfs->itable_buf);
c762c8e6
TT
384 if (retval) goto errout;
385
a8519a2d 386 io_channel_flush(fs->io);
3b627e8d
TT
387 if (rfs->progress) {
388 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
389 i - adj + 1, max_group);
390 if (retval)
391 goto errout;
392 }
1e1da29f 393 group_block += fs->super->s_blocks_per_group;
24b2c7a7 394 }
c762c8e6
TT
395 io_channel_flush(fs->io);
396 retval = 0;
397
398errout:
c762c8e6
TT
399 return retval;
400}
401
a8519a2d
TT
402/* --------------------------------------------------------------------
403 *
404 * Resize processing, phase 2.
405 *
406 * In this phase we adjust determine which blocks need to be moved, in
407 * blocks_to_move(). We then copy the blocks to their ultimate new
408 * destinations using block_mover(). Since we are copying blocks to
409 * their new locations, again during this pass we can abort without
410 * any problems.
411 * --------------------------------------------------------------------
412 */
413
c762c8e6
TT
414/*
415 * This helper function creates a block bitmap with all of the
416 * filesystem meta-data blocks.
417 */
418static errcode_t mark_table_blocks(ext2_filsys fs,
419 ext2fs_block_bitmap *ret_bmap)
420{
421 blk_t block, b;
422 int i,j;
423 ext2fs_block_bitmap bmap;
424 errcode_t retval;
425
a13575f4
TT
426 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
427 &bmap);
c762c8e6
TT
428 if (retval)
429 return retval;
430
431 block = fs->super->s_first_data_block;
432 for (i = 0; i < fs->group_desc_count; i++) {
433 if (ext2fs_bg_has_super(fs, i)) {
434 /*
435 * Mark this group's copy of the superblock
436 */
437 ext2fs_mark_block_bitmap(bmap, block);
438
439 /*
440 * Mark this group's copy of the descriptors
441 */
442 for (j = 0; j < fs->desc_blocks; j++)
443 ext2fs_mark_block_bitmap(bmap, block + j + 1);
444 }
445
446 /*
447 * Mark the blocks used for the inode table
448 */
449 for (j = 0, b = fs->group_desc[i].bg_inode_table;
450 j < fs->inode_blocks_per_group;
451 j++, b++)
452 ext2fs_mark_block_bitmap(bmap, b);
453
454 /*
455 * Mark block used for the block bitmap
456 */
457 ext2fs_mark_block_bitmap(bmap,
458 fs->group_desc[i].bg_block_bitmap);
459 /*
460 * Mark block used for the inode bitmap
461 */
462 ext2fs_mark_block_bitmap(bmap,
463 fs->group_desc[i].bg_inode_bitmap);
464 block += fs->super->s_blocks_per_group;
465 }
466 *ret_bmap = bmap;
1e1da29f 467 return 0;
24b2c7a7
TT
468}
469
24b2c7a7
TT
470/*
471 * This routine marks and unmarks reserved blocks in the new block
472 * bitmap. It also determines which blocks need to be moved and
473 * places this information into the move_blocks bitmap.
474 */
c762c8e6 475static errcode_t blocks_to_move(ext2_resize_t rfs)
24b2c7a7 476{
1333fe93 477 int i, j, max_groups;
24b2c7a7
TT
478 blk_t blk, group_blk;
479 unsigned long old_blocks, new_blocks;
480 errcode_t retval;
c762c8e6
TT
481 ext2_filsys fs, old_fs;
482 ext2fs_block_bitmap meta_bmap;
24b2c7a7 483
c762c8e6
TT
484 fs = rfs->new_fs;
485 old_fs = rfs->old_fs;
486 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
487 fs = rfs->old_fs;
488
a13575f4 489 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
1e1da29f 490 &rfs->reserve_blocks);
24b2c7a7
TT
491 if (retval)
492 return retval;
1e1da29f 493
a13575f4 494 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
c762c8e6
TT
495 &rfs->move_blocks);
496 if (retval)
497 return retval;
498
bce49798 499 retval = mark_table_blocks(old_fs, &meta_bmap);
c762c8e6
TT
500 if (retval)
501 return retval;
502
503 fs = rfs->new_fs;
504
1e1da29f
TT
505 /*
506 * If we're shrinking the filesystem, we need to move all of
507 * the blocks that don't fit any more
508 */
509 for (blk = fs->super->s_blocks_count;
c762c8e6
TT
510 blk < old_fs->super->s_blocks_count; blk++) {
511 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
512 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
513 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
1e1da29f 514 rfs->needed_blocks++;
c762c8e6 515 }
1e1da29f
TT
516 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
517 }
24b2c7a7 518
c762c8e6 519 old_blocks = old_fs->desc_blocks;
1e1da29f
TT
520 new_blocks = fs->desc_blocks;
521
c762c8e6
TT
522 if (old_blocks == new_blocks) {
523 retval = 0;
524 goto errout;
525 }
24b2c7a7 526
1333fe93
TT
527 max_groups = fs->group_desc_count;
528 if (max_groups > old_fs->group_desc_count)
529 max_groups = old_fs->group_desc_count;
c762c8e6 530 group_blk = old_fs->super->s_first_data_block;
24b2c7a7
TT
531 /*
532 * If we're reducing the number of descriptor blocks, this
533 * makes life easy. :-) We just have to mark some extra
534 * blocks as free.
535 */
536 if (old_blocks > new_blocks) {
1333fe93 537 for (i = 0; i < max_groups; i++) {
1e1da29f
TT
538 if (!ext2fs_bg_has_super(fs, i)) {
539 group_blk += fs->super->s_blocks_per_group;
24b2c7a7
TT
540 continue;
541 }
c762c8e6
TT
542 for (blk = group_blk+1+new_blocks;
543 blk < group_blk+1+old_blocks; blk++) {
1e1da29f 544 ext2fs_unmark_block_bitmap(fs->block_map,
24b2c7a7 545 blk);
052db4b7
TT
546 rfs->needed_blocks--;
547 }
1e1da29f 548 group_blk += fs->super->s_blocks_per_group;
24b2c7a7 549 }
c762c8e6
TT
550 retval = 0;
551 goto errout;
24b2c7a7
TT
552 }
553 /*
554 * If we're increasing the number of descriptor blocks, life
1e1da29f 555 * gets interesting....
24b2c7a7 556 */
1333fe93 557 for (i = 0; i < max_groups; i++) {
1e1da29f
TT
558 if (!ext2fs_bg_has_super(fs, i))
559 goto next_group;
560
561 for (blk = group_blk;
562 blk < group_blk + 1 + new_blocks; blk++) {
563 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
564 ext2fs_mark_block_bitmap(fs->block_map, blk);
565
566 /*
567 * Check to see if we overlap with the inode
c762c8e6
TT
568 * or block bitmap, or the inode tables. If
569 * not, and the block is in use, then mark it
570 * as a block to be moved.
1e1da29f 571 */
c762c8e6
TT
572 if (IS_BLOCK_BM(fs, i, blk)) {
573 FS_BLOCK_BM(fs, i) = 0;
052db4b7 574 rfs->needed_blocks++;
c762c8e6
TT
575 } else if (IS_INODE_BM(fs, i, blk)) {
576 FS_INODE_BM(fs, i) = 0;
577 rfs->needed_blocks++;
578 } else if (IS_INODE_TB(fs, i, blk)) {
579 FS_INODE_TB(fs, i) = 0;
580 rfs->needed_blocks++;
581 } else if (ext2fs_test_block_bitmap(old_fs->block_map,
582 blk) &&
583 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
584 ext2fs_mark_block_bitmap(rfs->move_blocks,
585 blk);
052db4b7
TT
586 rfs->needed_blocks++;
587 }
24b2c7a7 588 }
1e1da29f
TT
589 if (fs->group_desc[i].bg_inode_table &&
590 fs->group_desc[i].bg_inode_bitmap &&
591 fs->group_desc[i].bg_block_bitmap)
592 goto next_group;
24b2c7a7 593
1e1da29f 594 /*
c762c8e6
TT
595 * Reserve the existing meta blocks that we know
596 * aren't to be moved.
1e1da29f
TT
597 */
598 if (fs->group_desc[i].bg_block_bitmap)
599 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
600 fs->group_desc[i].bg_block_bitmap);
601 if (fs->group_desc[i].bg_inode_bitmap)
602 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
603 fs->group_desc[i].bg_inode_bitmap);
604 if (fs->group_desc[i].bg_inode_table)
605 for (blk = fs->group_desc[i].bg_inode_table, j=0;
606 j < fs->inode_blocks_per_group ; j++, blk++)
607 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
608 blk);
24b2c7a7 609
c762c8e6
TT
610 /*
611 * Allocate the missing data structures
612 */
1e1da29f
TT
613 retval = ext2fs_allocate_group_table(fs, i,
614 rfs->reserve_blocks);
615 if (retval)
c762c8e6 616 goto errout;
24b2c7a7 617
1e1da29f 618 /*
c762c8e6
TT
619 * For those structures that have changed, we need to
620 * do bookkeepping.
1e1da29f 621 */
c762c8e6
TT
622 if (FS_BLOCK_BM(old_fs, i) !=
623 (blk = FS_BLOCK_BM(fs, i))) {
624 ext2fs_mark_block_bitmap(fs->block_map, blk);
625 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
626 !ext2fs_test_block_bitmap(meta_bmap, blk))
627 ext2fs_mark_block_bitmap(rfs->move_blocks,
628 blk);
629 }
630 if (FS_INODE_BM(old_fs, i) !=
631 (blk = FS_INODE_BM(fs, i))) {
632 ext2fs_mark_block_bitmap(fs->block_map, blk);
633 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
634 !ext2fs_test_block_bitmap(meta_bmap, blk))
635 ext2fs_mark_block_bitmap(rfs->move_blocks,
636 blk);
637 }
24b2c7a7 638
052db4b7
TT
639 /*
640 * The inode table, if we need to relocate it, is
641 * handled specially. We have to reserve the blocks
642 * for both the old and the new inode table, since we
643 * can't have the inode table be destroyed during the
644 * block relocation phase.
645 */
c762c8e6 646 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
052db4b7
TT
647 goto next_group; /* inode table not moved */
648
c762c8e6 649 rfs->needed_blocks += fs->inode_blocks_per_group;
052db4b7
TT
650
651 /*
652 * Mark the new inode table as in use in the new block
c762c8e6
TT
653 * allocation bitmap, and move any blocks that might
654 * be necessary.
052db4b7 655 */
1e1da29f 656 for (blk = fs->group_desc[i].bg_inode_table, j=0;
c762c8e6 657 j < fs->inode_blocks_per_group ; j++, blk++) {
1e1da29f 658 ext2fs_mark_block_bitmap(fs->block_map, blk);
c762c8e6
TT
659 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
660 !ext2fs_test_block_bitmap(meta_bmap, blk))
661 ext2fs_mark_block_bitmap(rfs->move_blocks,
662 blk);
663 }
664
1e1da29f 665 /*
052db4b7
TT
666 * Make sure the old inode table is reserved in the
667 * block reservation bitmap.
1e1da29f 668 */
052db4b7
TT
669 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
670 j < fs->inode_blocks_per_group ; j++, blk++)
671 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
1e1da29f
TT
672
673 next_group:
674 group_blk += rfs->new_fs->super->s_blocks_per_group;
675 }
c762c8e6
TT
676 retval = 0;
677
678errout:
679 if (meta_bmap)
680 ext2fs_free_block_bitmap(meta_bmap);
681
682 return retval;
1e1da29f 683}
24b2c7a7 684
a8519a2d
TT
685/*
686 * This helper function tries to allocate a new block. We try to
687 * avoid hitting the original group descriptor blocks at least at
688 * first, since we want to make it possible to recover from a badly
689 * aborted resize operation as much as possible.
690 *
691 * In the future, I may further modify this routine to balance out
692 * where we get the new blocks across the various block groups.
693 * Ideally we would allocate blocks that corresponded with the block
694 * group of the containing inode, and keep contiguous blocks
695 * together. However, this very difficult to do efficiently, since we
696 * don't have the necessary information up front.
697 */
698
699#define AVOID_OLD 1
700#define DESPERATION 2
701
702static void init_block_alloc(ext2_resize_t rfs)
703{
704 rfs->alloc_state = AVOID_OLD;
705 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
2bc4d4f7
TT
706#if 0
707 /* HACK for testing */
708 if (rfs->new_fs->super->s_blocks_count >
709 rfs->old_fs->super->s_blocks_count)
710 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
711#endif
a8519a2d
TT
712}
713
714static blk_t get_new_block(ext2_resize_t rfs)
715{
716 ext2_filsys fs = rfs->new_fs;
717
718 while (1) {
719 if (rfs->new_blk >= fs->super->s_blocks_count) {
720 if (rfs->alloc_state == DESPERATION)
721 return 0;
722
723#ifdef RESIZE2FS_DEBUG
724 if (rfs->flags & RESIZE_DEBUG_BMOVE)
a13575f4
TT
725 printf(_("Going into desperation "
726 "mode for block allocations\n"));
a8519a2d
TT
727#endif
728 rfs->alloc_state = DESPERATION;
729 rfs->new_blk = fs->super->s_first_data_block;
730 continue;
731 }
732 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
733 ext2fs_test_block_bitmap(rfs->reserve_blocks,
734 rfs->new_blk) ||
735 ((rfs->alloc_state == AVOID_OLD) &&
bce49798 736 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
a8519a2d
TT
737 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
738 rfs->new_blk))) {
739 rfs->new_blk++;
740 continue;
741 }
742 return rfs->new_blk;
743 }
744}
745
746static errcode_t block_mover(ext2_resize_t rfs)
747{
748 blk_t blk, old_blk, new_blk;
749 ext2_filsys fs = rfs->new_fs;
750 ext2_filsys old_fs = rfs->old_fs;
751 errcode_t retval;
752 int size, c;
753 int to_move, moved;
754
755 new_blk = fs->super->s_first_data_block;
756 if (!rfs->itable_buf) {
757 retval = ext2fs_get_mem(fs->blocksize *
758 fs->inode_blocks_per_group,
759 (void **) &rfs->itable_buf);
760 if (retval)
761 return retval;
762 }
763 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
764 if (retval)
765 return retval;
766
767 /*
768 * The first step is to figure out where all of the blocks
769 * will go.
770 */
771 to_move = moved = 0;
772 init_block_alloc(rfs);
773 for (blk = old_fs->super->s_first_data_block;
774 blk < old_fs->super->s_blocks_count; blk++) {
775 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
776 continue;
777 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
778 continue;
779
780 new_blk = get_new_block(rfs);
781 if (!new_blk) {
782 retval = ENOSPC;
783 goto errout;
784 }
785 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
786 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
787 to_move++;
788 }
789
790 if (to_move == 0) {
791 retval = 0;
792 goto errout;
793 }
794
795 /*
796 * Step two is to actually move the blocks
797 */
798 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
799 if (retval) goto errout;
800
3b627e8d
TT
801 if (rfs->progress) {
802 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
803 0, to_move);
804 if (retval)
805 goto errout;
806 }
a8519a2d
TT
807 while (1) {
808 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
809 if (retval) goto errout;
810 if (!size)
811 break;
812#ifdef RESIZE2FS_DEBUG
813 if (rfs->flags & RESIZE_DEBUG_BMOVE)
a13575f4 814 printf(_("Moving %d blocks %u->%u\n"), size,
a8519a2d
TT
815 old_blk, new_blk);
816#endif
817 do {
818 c = size;
819 if (c > fs->inode_blocks_per_group)
820 c = fs->inode_blocks_per_group;
821 retval = io_channel_read_blk(fs->io, old_blk, c,
822 rfs->itable_buf);
823 if (retval) goto errout;
824 retval = io_channel_write_blk(fs->io, new_blk, c,
825 rfs->itable_buf);
826 if (retval) goto errout;
827 size -= c;
828 new_blk += c;
829 old_blk += c;
830 moved += c;
831 if (rfs->progress) {
832 io_channel_flush(fs->io);
3b627e8d
TT
833 retval = (rfs->progress)(rfs,
834 E2_RSZ_BLOCK_RELOC_PASS,
a8519a2d 835 moved, to_move);
3b627e8d
TT
836 if (retval)
837 goto errout;
a8519a2d
TT
838 }
839 } while (size > 0);
840 io_channel_flush(fs->io);
841 }
842
843errout:
844 return retval;
845}
846
847
848/* --------------------------------------------------------------------
849 *
850 * Resize processing, phase 3
851 *
852 * --------------------------------------------------------------------
853 */
854
855
856struct process_block_struct {
857 ext2_resize_t rfs;
dfcdc32f 858 ext2_ino_t ino;
a8519a2d
TT
859 struct ext2_inode * inode;
860 errcode_t error;
861 int is_dir;
862 int changed;
863};
864
865static int process_block(ext2_filsys fs, blk_t *block_nr,
08430759 866 e2_blkcnt_t blockcnt, blk_t ref_block,
a8519a2d
TT
867 int ref_offset, void *priv_data)
868{
869 struct process_block_struct *pb;
870 errcode_t retval;
871 blk_t block, new_block;
872 int ret = 0;
873
874 pb = (struct process_block_struct *) priv_data;
875 block = *block_nr;
876 if (pb->rfs->bmap) {
877 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
878 if (new_block) {
879 *block_nr = new_block;
880 ret |= BLOCK_CHANGED;
881 pb->changed = 1;
882#ifdef RESIZE2FS_DEBUG
883 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
a13575f4 884 printf(_("ino=%ld, blockcnt=%ld, %u->%u\n"),
a8519a2d
TT
885 pb->ino, blockcnt, block, new_block);
886#endif
887 block = new_block;
888 }
889 }
890 if (pb->is_dir) {
891 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
101c84f2 892 block, (int) blockcnt);
a8519a2d
TT
893 if (retval) {
894 pb->error = retval;
895 ret |= BLOCK_ABORT;
896 }
897 }
898 return ret;
899}
900
901/*
902 * Progress callback
903 */
904static errcode_t progress_callback(ext2_filsys fs, ext2_inode_scan scan,
905 dgrp_t group, void * priv_data)
906{
907 ext2_resize_t rfs = (ext2_resize_t) priv_data;
3b627e8d 908 errcode_t retval;
a8519a2d
TT
909
910 /*
f4b2a6db
TT
911 * This check is to protect against old ext2 libraries. It
912 * shouldn't be needed against new libraries.
a8519a2d 913 */
f4b2a6db 914 if ((group+1) == 0)
a8519a2d
TT
915 return 0;
916
917 if (rfs->progress) {
918 io_channel_flush(fs->io);
3b627e8d
TT
919 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
920 group+1, fs->group_desc_count);
921 if (retval)
922 return retval;
a8519a2d
TT
923 }
924
925 return 0;
926}
927
928static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
929{
930 struct process_block_struct pb;
dfcdc32f 931 ext2_ino_t ino, new_inode;
a8519a2d
TT
932 struct ext2_inode inode;
933 ext2_inode_scan scan = NULL;
934 errcode_t retval;
935 int group;
936 char *block_buf = 0;
dfcdc32f 937 ext2_ino_t start_to_move;
2bc4d4f7 938 blk_t orig_size;
a8519a2d
TT
939
940 if ((rfs->old_fs->group_desc_count <=
941 rfs->new_fs->group_desc_count) &&
942 !rfs->bmap)
943 return 0;
944
2bc4d4f7
TT
945 /*
946 * Save the original size of the old filesystem, and
947 * temporarily set the size to be the new size if the new size
948 * is larger. We need to do this to avoid catching an error
949 * by the block iterator routines
950 */
951 orig_size = rfs->old_fs->super->s_blocks_count;
952 if (orig_size < rfs->new_fs->super->s_blocks_count)
953 rfs->old_fs->super->s_blocks_count =
954 rfs->new_fs->super->s_blocks_count;
955
a8519a2d
TT
956 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
957 if (retval) goto errout;
958
959 retval = ext2fs_init_dblist(rfs->old_fs, 0);
960 if (retval) goto errout;
961 retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3,
962 (void **) &block_buf);
963 if (retval) goto errout;
964
965 start_to_move = (rfs->new_fs->group_desc_count *
966 rfs->new_fs->super->s_inodes_per_group);
967
3b627e8d
TT
968 if (rfs->progress) {
969 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
970 0, rfs->old_fs->group_desc_count);
971 if (retval)
972 goto errout;
973 }
a8519a2d
TT
974 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
975 pb.rfs = rfs;
976 pb.inode = &inode;
977 pb.error = 0;
978 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
979 /*
980 * First, copy all of the inodes that need to be moved
981 * elsewhere in the inode table
982 */
983 while (1) {
984 retval = ext2fs_get_next_inode(scan, &ino, &inode);
985 if (retval) goto errout;
986 if (!ino)
987 break;
988
989 if (inode.i_links_count == 0)
990 continue; /* inode not in use */
991
992 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
993 pb.changed = 0;
994
995 if (ext2fs_inode_has_valid_blocks(&inode) &&
996 (rfs->bmap || pb.is_dir)) {
997 pb.ino = ino;
998 retval = ext2fs_block_iterate2(rfs->old_fs,
999 ino, 0, block_buf,
1000 process_block, &pb);
1001 if (retval)
1002 goto errout;
1003 if (pb.error) {
1004 retval = pb.error;
1005 goto errout;
1006 }
1007 }
1008
1009 if (ino <= start_to_move)
1010 continue; /* Don't need to move it. */
1011
1012 /*
1013 * Find a new inode
1014 */
1015 while (1) {
1016 if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
1017 new_inode))
1018 break;
1019 new_inode++;
1020 if (new_inode > rfs->new_fs->super->s_inodes_count) {
1021 retval = ENOSPC;
1022 goto errout;
1023 }
1024 }
1025 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1026 if (pb.changed) {
1027 /* Get the new version of the inode */
1028 retval = ext2fs_read_inode(rfs->old_fs, ino, &inode);
1029 if (retval) goto errout;
1030 }
1031 retval = ext2fs_write_inode(rfs->old_fs, new_inode, &inode);
1032 if (retval) goto errout;
1033
1034 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1035 if (LINUX_S_ISDIR(inode.i_mode))
1036 rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1037
1038#ifdef RESIZE2FS_DEBUG
1039 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
a13575f4 1040 printf(_("Inode moved %ld->%ld\n"), ino, new_inode);
a8519a2d
TT
1041#endif
1042 if (!rfs->imap) {
1043 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1044 if (retval)
1045 goto errout;
1046 }
1047 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1048 }
1049 io_channel_flush(rfs->old_fs->io);
1050
1051errout:
2bc4d4f7 1052 rfs->old_fs->super->s_blocks_count = orig_size;
a8519a2d
TT
1053 if (rfs->bmap) {
1054 ext2fs_free_extent_table(rfs->bmap);
1055 rfs->bmap = 0;
1056 }
1057 if (scan)
1058 ext2fs_close_inode_scan(scan);
1059 if (block_buf)
1060 ext2fs_free_mem((void **) &block_buf);
1061 return retval;
1062}
1063
1064/* --------------------------------------------------------------------
1065 *
1066 * Resize processing, phase 4.
1067 *
1068 * --------------------------------------------------------------------
1069 */
1070
1071struct istruct {
1072 ext2_resize_t rfs;
3b627e8d 1073 errcode_t err;
1333fe93 1074 unsigned long max_dirs;
a8519a2d
TT
1075 int num;
1076};
1077
dfcdc32f 1078static int check_and_change_inodes(ext2_ino_t dir, int entry,
a8519a2d
TT
1079 struct ext2_dir_entry *dirent, int offset,
1080 int blocksize, char *buf, void *priv_data)
1081{
1082 struct istruct *is = (struct istruct *) priv_data;
dfcdc32f 1083 ext2_ino_t new_inode;
a8519a2d
TT
1084
1085 if (is->rfs->progress && offset == 0) {
1086 io_channel_flush(is->rfs->old_fs->io);
3b627e8d
TT
1087 is->err = (is->rfs->progress)(is->rfs,
1088 E2_RSZ_INODE_REF_UPD_PASS,
1333fe93 1089 ++is->num, is->max_dirs);
3b627e8d
TT
1090 if (is->err)
1091 return DIRENT_ABORT;
a8519a2d
TT
1092 }
1093
1094 if (!dirent->inode)
1095 return 0;
1096
1097 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1098
1099 if (!new_inode)
1100 return 0;
1101#ifdef RESIZE2FS_DEBUG
1102 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
a13575f4 1103 printf(_("Inode translate (dir=%ld, name=%.*s, %u->%ld)\n"),
a8519a2d
TT
1104 dir, dirent->name_len, dirent->name,
1105 dirent->inode, new_inode);
1106#endif
1107
1108 dirent->inode = new_inode;
1109
1110 return DIRENT_CHANGED;
1111}
1112
1113static errcode_t inode_ref_fix(ext2_resize_t rfs)
1114{
1115 errcode_t retval;
1116 struct istruct is;
1117
1118 if (!rfs->imap)
1119 return 0;
1120
1121 /*
1122 * Now, we iterate over all of the directories to update the
1123 * inode references
1124 */
1125 is.num = 0;
1333fe93 1126 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
a8519a2d 1127 is.rfs = rfs;
3b627e8d 1128 is.err = 0;
a8519a2d 1129
3b627e8d
TT
1130 if (rfs->progress) {
1131 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1333fe93 1132 0, is.max_dirs);
3b627e8d
TT
1133 if (retval)
1134 goto errout;
1135 }
a8519a2d
TT
1136
1137 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1138 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1139 check_and_change_inodes, &is);
3b627e8d
TT
1140 if (retval)
1141 goto errout;
1142 if (is.err) {
1143 retval = is.err;
1144 goto errout;
1145 }
a8519a2d 1146
3b627e8d 1147errout:
a8519a2d
TT
1148 ext2fs_free_extent_table(rfs->imap);
1149 rfs->imap = 0;
1150 return retval;
1151}
1152
1153
1154/* --------------------------------------------------------------------
1155 *
1156 * Resize processing, phase 5.
1157 *
1158 * In this phase we actually move the inode table around, and then
1159 * update the summary statistics. This is scary, since aborting here
1160 * will potentially scramble the filesystem. (We are moving the
1161 * inode tables around in place, and so the potential for lost data,
1162 * or at the very least scrambling the mapping between filenames and
1163 * inode numbers is very high in case of a power failure here.)
1164 * --------------------------------------------------------------------
1165 */
1166
24b2c7a7 1167
052db4b7
TT
1168/*
1169 * A very scary routine --- this one moves the inode table around!!!
1170 *
1171 * After this you have to use the rfs->new_fs file handle to read and
1172 * write inodes.
1173 */
c762c8e6 1174static errcode_t move_itables(ext2_resize_t rfs)
052db4b7 1175{
1333fe93 1176 int i, n, num, max_groups, size, diff;
052db4b7 1177 ext2_filsys fs = rfs->new_fs;
05e112a1 1178 char *cp;
ca8abba7 1179 blk_t old_blk, new_blk;
a8519a2d 1180 errcode_t retval;
c762c8e6 1181 int to_move, moved;
052db4b7 1182
1333fe93
TT
1183 max_groups = fs->group_desc_count;
1184 if (max_groups > rfs->old_fs->group_desc_count)
1185 max_groups = rfs->old_fs->group_desc_count;
052db4b7 1186
05e112a1
TT
1187 size = fs->blocksize * fs->inode_blocks_per_group;
1188 if (!rfs->itable_buf) {
ca8abba7
TT
1189 retval = ext2fs_get_mem(size, (void **) &rfs->itable_buf);
1190 if (retval)
1191 return retval;
05e112a1 1192 }
c762c8e6
TT
1193
1194 /*
1195 * Figure out how many inode tables we need to move
1196 */
1197 to_move = moved = 0;
1333fe93 1198 for (i=0; i < max_groups; i++)
c762c8e6
TT
1199 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1200 fs->group_desc[i].bg_inode_table)
1201 to_move++;
1202
1203 if (to_move == 0)
1204 return 0;
1205
3b627e8d
TT
1206 if (rfs->progress) {
1207 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1208 0, to_move);
1209 if (retval)
1210 goto errout;
1211 }
63b44fbe 1212
a8519a2d
TT
1213 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1214
1333fe93 1215 for (i=0; i < max_groups; i++) {
ca8abba7
TT
1216 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1217 new_blk = fs->group_desc[i].bg_inode_table;
1218 diff = new_blk - old_blk;
052db4b7 1219
80c0fc34 1220#ifdef RESIZE2FS_DEBUG
05e112a1 1221 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
a13575f4
TT
1222 printf(_("Itable move group %d block "
1223 "%u->%u (diff %d)\n"),
ca8abba7 1224 i, old_blk, new_blk, diff);
80c0fc34 1225#endif
052db4b7 1226
05e112a1 1227 if (!diff)
052db4b7
TT
1228 continue;
1229
ca8abba7 1230 retval = io_channel_read_blk(fs->io, old_blk,
05e112a1
TT
1231 fs->inode_blocks_per_group,
1232 rfs->itable_buf);
052db4b7 1233 if (retval)
a8519a2d 1234 goto errout;
05e112a1
TT
1235 /*
1236 * The end of the inode table segment often contains
a8519a2d
TT
1237 * all zeros, and we're often only moving the inode
1238 * table down a block or two. If so, we can optimize
1239 * things by not rewriting blocks that we know to be zero
1240 * already.
05e112a1
TT
1241 */
1242 for (cp = rfs->itable_buf+size, n=0; n < size; n++, cp--)
1243 if (*cp)
1244 break;
1245 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
80c0fc34 1246#ifdef RESIZE2FS_DEBUG
05e112a1 1247 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
a13575f4 1248 printf(_("%d blocks of zeros...\n"), n);
80c0fc34 1249#endif
05e112a1
TT
1250 num = fs->inode_blocks_per_group;
1251 if (n > diff)
1252 num -= n;
1253
ca8abba7 1254 retval = io_channel_write_blk(fs->io, new_blk,
05e112a1 1255 num, rfs->itable_buf);
052db4b7 1256 if (retval) {
ca8abba7 1257 io_channel_write_blk(fs->io, old_blk,
05e112a1 1258 num, rfs->itable_buf);
a8519a2d 1259 goto errout;
052db4b7 1260 }
05e112a1
TT
1261 if (n > diff) {
1262 retval = io_channel_write_blk(fs->io,
ca8abba7 1263 old_blk + fs->inode_blocks_per_group,
a8519a2d
TT
1264 diff, (rfs->itable_buf +
1265 (fs->inode_blocks_per_group - diff) *
1266 fs->blocksize));
05e112a1 1267 if (retval)
a8519a2d 1268 goto errout;
c762c8e6 1269 }
a8519a2d
TT
1270 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1271 ext2fs_mark_super_dirty(rfs->old_fs);
1272 if (rfs->progress) {
1273 ext2fs_flush(rfs->old_fs);
3b627e8d
TT
1274 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1275 ++moved, to_move);
1276 if (retval)
1277 goto errout;
a8519a2d 1278 }
052db4b7 1279 }
a8519a2d 1280 ext2fs_flush(fs);
80c0fc34 1281#ifdef RESIZE2FS_DEBUG
05e112a1 1282 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
a13575f4 1283 printf(_("Inode table move finished.\n"));
80c0fc34 1284#endif
052db4b7
TT
1285 return 0;
1286
a8519a2d 1287errout:
052db4b7
TT
1288 return retval;
1289}
1290
1291/*
1292 * Finally, recalculate the summary information
1293 */
1294static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1295{
dfcdc32f
TT
1296 blk_t blk;
1297 ext2_ino_t ino;
1298 int group = 0;
1299 int count = 0;
1300 int total_free = 0;
1301 int group_free = 0;
052db4b7
TT
1302
1303 /*
1304 * First calculate the block statistics
1305 */
1306 for (blk = fs->super->s_first_data_block;
1307 blk < fs->super->s_blocks_count; blk++) {
1308 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1309 group_free++;
1310 total_free++;
1311 }
1312 count++;
1313 if ((count == fs->super->s_blocks_per_group) ||
1314 (blk == fs->super->s_blocks_count-1)) {
1315 fs->group_desc[group++].bg_free_blocks_count =
1316 group_free;
1317 count = 0;
1318 group_free = 0;
1319 }
1320 }
1321 fs->super->s_free_blocks_count = total_free;
1322
1323 /*
1324 * Next, calculate the inode statistics
1325 */
1326 group_free = 0;
1327 total_free = 0;
1328 count = 0;
1329 group = 0;
1330 for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
1331 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1332 group_free++;
1333 total_free++;
1334 }
1335 count++;
1336 if ((count == fs->super->s_inodes_per_group) ||
1337 (ino == fs->super->s_inodes_count)) {
1338 fs->group_desc[group++].bg_free_inodes_count =
1339 group_free;
1340 count = 0;
1341 group_free = 0;
1342 }
1343 }
1344 fs->super->s_free_inodes_count = total_free;
1345 ext2fs_mark_super_dirty(fs);
1346 return 0;
1347}