]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - resize/resize2fs.c
Make dumpe2fs uninit block group aware
[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 36#include "resize2fs.h"
b969b1b8 37#include <time.h>
24b2c7a7 38
546a1ff1 39#ifdef __linux__ /* Kludge for debugging */
a8519a2d
TT
40#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);
9213a93b 49static errcode_t fix_resize_inode(ext2_filsys fs);
a8519a2d
TT
50static 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
199ddaaa
JB
66#define META_OVERHEAD(fs) (2 + (fs)->inode_blocks_per_group)
67#define SUPER_OVERHEAD(fs) (1 + (fs)->desc_blocks +\
68 (fs)->super->s_reserved_gdt_blocks)
a8519a2d
TT
69
70/*
71 * This is the top-level routine which does the dirty deed....
72 */
116db1b5 73errcode_t resize_fs(ext2_filsys fs, blk_t *new_size, int flags,
3b627e8d 74 errcode_t (*progress)(ext2_resize_t rfs, int pass,
a8519a2d 75 unsigned long cur,
1333fe93 76 unsigned long max_val))
a8519a2d
TT
77{
78 ext2_resize_t rfs;
79 errcode_t retval;
80
81 retval = ext2fs_read_bitmaps(fs);
82 if (retval)
83 return retval;
84
85 /*
86 * Create the data structure
87 */
c4e3d3f3 88 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
a8519a2d
TT
89 if (retval)
90 return retval;
91 memset(rfs, 0, sizeof(struct ext2_resize_struct));
92
93 rfs->old_fs = fs;
94 rfs->flags = flags;
95 rfs->itable_buf = 0;
96 rfs->progress = progress;
97 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
98 if (retval)
99 goto errout;
100
116db1b5 101 retval = adjust_superblock(rfs, *new_size);
a8519a2d
TT
102 if (retval)
103 goto errout;
104
116db1b5
TT
105 *new_size = rfs->new_fs->super->s_blocks_count;
106
a8519a2d
TT
107 retval = blocks_to_move(rfs);
108 if (retval)
109 goto errout;
110
111#ifdef RESIZE2FS_DEBUG
112 if (rfs->flags & RESIZE_DEBUG_BMOVE)
8deb80a5 113 printf("Number of free blocks: %u/%u, Needed: %d\n",
a8519a2d
TT
114 rfs->old_fs->super->s_free_blocks_count,
115 rfs->new_fs->super->s_free_blocks_count,
116 rfs->needed_blocks);
117#endif
118
119 retval = block_mover(rfs);
120 if (retval)
121 goto errout;
122
123 retval = inode_scan_and_fix(rfs);
124 if (retval)
125 goto errout;
126
127 retval = inode_ref_fix(rfs);
128 if (retval)
129 goto errout;
130
a8519a2d
TT
131 retval = move_itables(rfs);
132 if (retval)
133 goto errout;
134
64ad98ac
TT
135 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
136 if (retval)
137 goto errout;
138
9213a93b
TT
139 retval = fix_resize_inode(rfs->new_fs);
140 if (retval)
141 goto errout;
142
058ad1c7 143 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
a8519a2d
TT
144 retval = ext2fs_close(rfs->new_fs);
145 if (retval)
146 goto errout;
147
148 rfs->flags = flags;
149
150 ext2fs_free(rfs->old_fs);
151 if (rfs->itable_buf)
c4e3d3f3
TT
152 ext2fs_free_mem(&rfs->itable_buf);
153 ext2fs_free_mem(&rfs);
a8519a2d
TT
154
155 return 0;
156
157errout:
158 if (rfs->new_fs)
159 ext2fs_free(rfs->new_fs);
160 if (rfs->itable_buf)
c4e3d3f3
TT
161 ext2fs_free_mem(&rfs->itable_buf);
162 ext2fs_free_mem(&rfs);
a8519a2d
TT
163 return retval;
164}
165
166/* --------------------------------------------------------------------
167 *
168 * Resize processing, phase 1.
169 *
170 * In this phase we adjust the in-memory superblock information, and
171 * initialize any new parts of the inode table. The new parts of the
172 * inode table are created in virgin disk space, so we can abort here
173 * without any side effects.
174 * --------------------------------------------------------------------
175 */
176
24b2c7a7 177/*
bf69235a
TT
178 * This routine is shared by the online and offline resize routines.
179 * All of the information which is adjusted in memory is done here.
24b2c7a7 180 */
bf69235a 181errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs, blk_t new_size)
24b2c7a7 182{
24b2c7a7 183 errcode_t retval;
bf69235a
TT
184 int overhead = 0;
185 int rem;
24b2c7a7 186 blk_t blk, group_block;
bf69235a
TT
187 ext2_ino_t real_end;
188 int adj, old_numblocks, numblocks, adjblocks;
189 unsigned long i, j, old_desc_blocks, max_group;
54434927
TT
190 unsigned int meta_bg, meta_bg_size;
191 int has_super;
de8f3a76 192 unsigned long long new_inodes; /* u64 to check for overflow */
bf69235a 193
24b2c7a7
TT
194 fs->super->s_blocks_count = new_size;
195
196retry:
69022e02
TT
197 fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
198 fs->super->s_first_data_block,
199 EXT2_BLOCKS_PER_GROUP(fs->super));
24b2c7a7
TT
200 if (fs->group_desc_count == 0)
201 return EXT2_ET_TOOSMALL;
69022e02
TT
202 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
203 EXT2_DESC_PER_BLOCK(fs->super));
24b2c7a7
TT
204
205 /*
206 * Overhead is the number of bookkeeping blocks per group. It
207 * includes the superblock backup, the group descriptor
208 * backups, the inode bitmap, the block bitmap, and the inode
209 * table.
24b2c7a7 210 */
9213a93b
TT
211 overhead = (int) (2 + fs->inode_blocks_per_group);
212
213 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
214 overhead += 1 + fs->desc_blocks +
215 fs->super->s_reserved_gdt_blocks;
216
24b2c7a7
TT
217 /*
218 * See if the last group is big enough to support the
219 * necessary data structures. If not, we need to get rid of
220 * it.
221 */
222 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
223 fs->super->s_blocks_per_group;
224 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
225 return EXT2_ET_TOOSMALL;
226 if (rem && (rem < overhead+50)) {
227 fs->super->s_blocks_count -= rem;
228 goto retry;
229 }
230 /*
231 * Adjust the number of inodes
232 */
de8f3a76 233 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
f3358643
ES
234 if (new_inodes > ~0U) {
235 fprintf(stderr, _("inodes (%llu) must be less than %u"),
236 new_inodes, ~0U);
237 return EXT2_ET_TOO_MANY_INODES;
238 }
24b2c7a7
TT
239 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
240 fs->group_desc_count;
241
242 /*
243 * Adjust the number of free blocks
244 */
bf69235a 245 blk = old_fs->super->s_blocks_count;
24b2c7a7
TT
246 if (blk > fs->super->s_blocks_count)
247 fs->super->s_free_blocks_count -=
248 (blk - fs->super->s_blocks_count);
249 else
250 fs->super->s_free_blocks_count +=
251 (fs->super->s_blocks_count - blk);
252
c762c8e6
TT
253 /*
254 * Adjust the number of reserved blocks
255 */
d1b4b85c 256 blk = (__u64)old_fs->super->s_r_blocks_count * 100 /
bf69235a 257 old_fs->super->s_blocks_count;
a8862d9e
TT
258 fs->super->s_r_blocks_count = e2p_percent(blk,
259 fs->super->s_blocks_count);
c762c8e6 260
24b2c7a7
TT
261 /*
262 * Adjust the bitmaps for size
263 */
264 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
265 fs->super->s_inodes_count,
266 fs->inode_map);
c762c8e6 267 if (retval) goto errout;
24b2c7a7
TT
268
269 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
270 * fs->group_desc_count)) - 1 +
271 fs->super->s_first_data_block;
272 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
273 real_end, fs->block_map);
274
c762c8e6 275 if (retval) goto errout;
24b2c7a7
TT
276
277 /*
278 * Reallocate the group descriptors as necessary.
279 */
bf69235a
TT
280 if (old_fs->desc_blocks != fs->desc_blocks) {
281 retval = ext2fs_resize_mem(old_fs->desc_blocks *
76f875da
TT
282 fs->blocksize,
283 fs->desc_blocks * fs->blocksize,
c4e3d3f3 284 &fs->group_desc);
ca8abba7 285 if (retval)
a8519a2d 286 goto errout;
bf69235a 287 if (fs->desc_blocks > old_fs->desc_blocks)
2787276e 288 memset((char *) fs->group_desc +
bf69235a
TT
289 (old_fs->desc_blocks * fs->blocksize), 0,
290 (fs->desc_blocks - old_fs->desc_blocks) *
2787276e 291 fs->blocksize);
24b2c7a7 292 }
1e1da29f 293
9213a93b
TT
294 /*
295 * If the resize_inode feature is set, and we are changing the
296 * number of descriptor blocks, then adjust
297 * s_reserved_gdt_blocks if possible to avoid needing to move
298 * the inode table either now or in the future.
299 */
300 if ((fs->super->s_feature_compat &
301 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
bf69235a 302 (old_fs->desc_blocks != fs->desc_blocks)) {
9213a93b
TT
303 int new;
304
305 new = ((int) fs->super->s_reserved_gdt_blocks) +
bf69235a 306 (old_fs->desc_blocks - fs->desc_blocks);
9213a93b
TT
307 if (new < 0)
308 new = 0;
de8f3a76 309 if (new > (int) fs->blocksize/4)
9213a93b
TT
310 new = fs->blocksize/4;
311 fs->super->s_reserved_gdt_blocks = new;
312 if (new == 0)
313 fs->super->s_feature_compat &=
314 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
315 }
316
a8519a2d
TT
317 /*
318 * If we are shrinking the number block groups, we're done and
319 * can exit now.
1e1da29f 320 */
bf69235a 321 if (old_fs->group_desc_count > fs->group_desc_count) {
c762c8e6
TT
322 retval = 0;
323 goto errout;
324 }
bf69235a 325
a8519a2d
TT
326 /*
327 * Fix the count of the last (old) block group
328 */
bf69235a
TT
329 old_numblocks = (old_fs->super->s_blocks_count -
330 old_fs->super->s_first_data_block) %
331 old_fs->super->s_blocks_per_group;
1e1da29f 332 if (!old_numblocks)
bf69235a
TT
333 old_numblocks = old_fs->super->s_blocks_per_group;
334 if (old_fs->group_desc_count == fs->group_desc_count) {
335 numblocks = (fs->super->s_blocks_count -
336 fs->super->s_first_data_block) %
337 fs->super->s_blocks_per_group;
1e1da29f 338 if (!numblocks)
bf69235a 339 numblocks = fs->super->s_blocks_per_group;
1e1da29f 340 } else
bf69235a
TT
341 numblocks = fs->super->s_blocks_per_group;
342 i = old_fs->group_desc_count - 1;
1e1da29f
TT
343 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
344
345 /*
a8519a2d
TT
346 * If the number of block groups is staying the same, we're
347 * done and can exit now. (If the number block groups is
348 * shrinking, we had exited earlier.)
1e1da29f 349 */
bf69235a 350 if (old_fs->group_desc_count >= fs->group_desc_count) {
c762c8e6
TT
351 retval = 0;
352 goto errout;
353 }
bf69235a 354
a8519a2d
TT
355 /*
356 * Initialize the new block group descriptors
357 */
1e1da29f 358 group_block = fs->super->s_first_data_block +
bf69235a 359 old_fs->group_desc_count * fs->super->s_blocks_per_group;
c762c8e6 360
bf69235a 361 adj = old_fs->group_desc_count;
63b44fbe 362 max_group = fs->group_desc_count - adj;
76dd5e5c
TT
363 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
364 old_desc_blocks = fs->super->s_first_meta_bg;
365 else
9213a93b
TT
366 old_desc_blocks = fs->desc_blocks +
367 fs->super->s_reserved_gdt_blocks;
bf69235a 368 for (i = old_fs->group_desc_count;
1e1da29f
TT
369 i < fs->group_desc_count; i++) {
370 memset(&fs->group_desc[i], 0,
371 sizeof(struct ext2_group_desc));
372 adjblocks = 0;
373
374 if (i == fs->group_desc_count-1) {
375 numblocks = (fs->super->s_blocks_count -
376 fs->super->s_first_data_block) %
377 fs->super->s_blocks_per_group;
378 if (!numblocks)
379 numblocks = fs->super->s_blocks_per_group;
380 } else
381 numblocks = fs->super->s_blocks_per_group;
382
76dd5e5c
TT
383 has_super = ext2fs_bg_has_super(fs, i);
384 if (has_super) {
385 ext2fs_mark_block_bitmap(fs->block_map, group_block);
386 adjblocks++;
387 }
f2de1d38 388 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
76dd5e5c
TT
389 meta_bg = i / meta_bg_size;
390 if (!(fs->super->s_feature_incompat &
391 EXT2_FEATURE_INCOMPAT_META_BG) ||
392 (meta_bg < fs->super->s_first_meta_bg)) {
393 if (has_super) {
394 for (j=0; j < old_desc_blocks; j++)
395 ext2fs_mark_block_bitmap(fs->block_map,
396 group_block + 1 + j);
397 adjblocks += old_desc_blocks;
398 }
399 } else {
400 if (has_super)
401 has_super = 1;
402 if (((i % meta_bg_size) == 0) ||
403 ((i % meta_bg_size) == 1) ||
404 ((i % meta_bg_size) == (meta_bg_size-1)))
1e1da29f 405 ext2fs_mark_block_bitmap(fs->block_map,
76dd5e5c 406 group_block + has_super);
24b2c7a7 407 }
76dd5e5c 408
1e1da29f
TT
409 adjblocks += 2 + fs->inode_blocks_per_group;
410
411 numblocks -= adjblocks;
412 fs->super->s_free_blocks_count -= adjblocks;
413 fs->super->s_free_inodes_count +=
414 fs->super->s_inodes_per_group;
415 fs->group_desc[i].bg_free_blocks_count = numblocks;
416 fs->group_desc[i].bg_free_inodes_count =
417 fs->super->s_inodes_per_group;
418 fs->group_desc[i].bg_used_dirs_count = 0;
24b2c7a7 419
1e1da29f 420 retval = ext2fs_allocate_group_table(fs, i, 0);
c762c8e6 421 if (retval) goto errout;
24b2c7a7 422
bf69235a
TT
423 group_block += fs->super->s_blocks_per_group;
424 }
425 retval = 0;
426
427errout:
428 return (retval);
429}
430
431/*
432 * This routine adjusts the superblock and other data structures, both
433 * in disk as well as in memory...
434 */
435static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
436{
437 ext2_filsys fs;
438 int adj = 0;
439 errcode_t retval;
440 blk_t group_block;
441 unsigned long i;
442 unsigned long max_group;
443
444 fs = rfs->new_fs;
445 ext2fs_mark_super_dirty(fs);
446 ext2fs_mark_bb_dirty(fs);
447 ext2fs_mark_ib_dirty(fs);
448
449 retval = adjust_fs_info(fs, rfs->old_fs, new_size);
450 if (retval)
451 goto errout;
452
453 /*
454 * Check to make sure there are enough inodes
455 */
456 if ((rfs->old_fs->super->s_inodes_count -
457 rfs->old_fs->super->s_free_inodes_count) >
458 rfs->new_fs->super->s_inodes_count) {
459 retval = ENOSPC;
460 goto errout;
461 }
462
463 /*
464 * If we are shrinking the number block groups, we're done and
465 * can exit now.
466 */
467 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
468 retval = 0;
469 goto errout;
470 }
471
472 /*
473 * If the number of block groups is staying the same, we're
474 * done and can exit now. (If the number block groups is
475 * shrinking, we had exited earlier.)
476 */
477 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
478 retval = 0;
479 goto errout;
480 }
481
482 /*
483 * Initialize the new block group descriptors
484 */
e5aace90 485 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
bf69235a
TT
486 &rfs->itable_buf);
487 if (retval)
488 goto errout;
489
490 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
491 group_block = fs->super->s_first_data_block +
492 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
493
494 adj = rfs->old_fs->group_desc_count;
495 max_group = fs->group_desc_count - adj;
496 if (rfs->progress) {
497 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
498 0, max_group);
499 if (retval)
500 goto errout;
501 }
502 for (i = rfs->old_fs->group_desc_count;
503 i < fs->group_desc_count; i++) {
05e112a1
TT
504 /*
505 * Write out the new inode table
506 */
507 retval = io_channel_write_blk(fs->io,
508 fs->group_desc[i].bg_inode_table,
509 fs->inode_blocks_per_group,
510 rfs->itable_buf);
c762c8e6
TT
511 if (retval) goto errout;
512
a8519a2d 513 io_channel_flush(fs->io);
3b627e8d
TT
514 if (rfs->progress) {
515 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
516 i - adj + 1, max_group);
517 if (retval)
518 goto errout;
519 }
1e1da29f 520 group_block += fs->super->s_blocks_per_group;
24b2c7a7 521 }
c762c8e6
TT
522 io_channel_flush(fs->io);
523 retval = 0;
524
525errout:
c762c8e6
TT
526 return retval;
527}
528
a8519a2d
TT
529/* --------------------------------------------------------------------
530 *
531 * Resize processing, phase 2.
532 *
533 * In this phase we adjust determine which blocks need to be moved, in
534 * blocks_to_move(). We then copy the blocks to their ultimate new
535 * destinations using block_mover(). Since we are copying blocks to
536 * their new locations, again during this pass we can abort without
537 * any problems.
538 * --------------------------------------------------------------------
539 */
540
c762c8e6
TT
541/*
542 * This helper function creates a block bitmap with all of the
543 * filesystem meta-data blocks.
544 */
545static errcode_t mark_table_blocks(ext2_filsys fs,
64ad98ac 546 ext2fs_block_bitmap bmap)
c762c8e6 547{
62c6d140 548 blk_t b;
54434927
TT
549 unsigned int j;
550 dgrp_t i;
68963d5a 551 unsigned long meta_bg_size;
54434927 552 unsigned int old_desc_blocks;
c762c8e6 553
f2de1d38 554 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
76dd5e5c
TT
555 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
556 old_desc_blocks = fs->super->s_first_meta_bg;
557 else
9213a93b
TT
558 old_desc_blocks = fs->desc_blocks +
559 fs->super->s_reserved_gdt_blocks;
c762c8e6 560 for (i = 0; i < fs->group_desc_count; i++) {
64ad98ac 561 ext2fs_reserve_super_and_bgd(fs, i, bmap);
76dd5e5c 562
c762c8e6
TT
563 /*
564 * Mark the blocks used for the inode table
565 */
566 for (j = 0, b = fs->group_desc[i].bg_inode_table;
54434927 567 j < (unsigned int) fs->inode_blocks_per_group;
c762c8e6
TT
568 j++, b++)
569 ext2fs_mark_block_bitmap(bmap, b);
570
571 /*
572 * Mark block used for the block bitmap
573 */
574 ext2fs_mark_block_bitmap(bmap,
575 fs->group_desc[i].bg_block_bitmap);
64ad98ac 576
c762c8e6
TT
577 /*
578 * Mark block used for the inode bitmap
579 */
580 ext2fs_mark_block_bitmap(bmap,
581 fs->group_desc[i].bg_inode_bitmap);
c762c8e6 582 }
1e1da29f 583 return 0;
24b2c7a7
TT
584}
585
76dd5e5c
TT
586/*
587 * This function checks to see if a particular block (either a
588 * superblock or a block group descriptor) overlaps with an inode or
589 * block bitmap block, or with the inode table.
590 */
591static void mark_fs_metablock(ext2_resize_t rfs,
592 ext2fs_block_bitmap meta_bmap,
593 int group, blk_t blk)
594{
595 ext2_filsys fs = rfs->new_fs;
596
597 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
598 ext2fs_mark_block_bitmap(fs->block_map, blk);
599
600 /*
601 * Check to see if we overlap with the inode or block bitmap,
602 * or the inode tables. If not, and the block is in use, then
603 * mark it as a block to be moved.
604 */
605 if (IS_BLOCK_BM(fs, group, blk)) {
606 FS_BLOCK_BM(fs, group) = 0;
607 rfs->needed_blocks++;
608 } else if (IS_INODE_BM(fs, group, blk)) {
609 FS_INODE_BM(fs, group) = 0;
610 rfs->needed_blocks++;
611 } else if (IS_INODE_TB(fs, group, blk)) {
612 FS_INODE_TB(fs, group) = 0;
613 rfs->needed_blocks++;
614 } else if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk) &&
615 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
616 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
617 rfs->needed_blocks++;
618 }
619}
620
621
24b2c7a7
TT
622/*
623 * This routine marks and unmarks reserved blocks in the new block
624 * bitmap. It also determines which blocks need to be moved and
625 * places this information into the move_blocks bitmap.
626 */
c762c8e6 627static errcode_t blocks_to_move(ext2_resize_t rfs)
24b2c7a7 628{
54434927
TT
629 int j, has_super;
630 dgrp_t i, max_groups;
631 blk_t blk, group_blk;
632 unsigned long old_blocks, new_blocks;
633 unsigned int meta_bg, meta_bg_size;
24b2c7a7 634 errcode_t retval;
c762c8e6
TT
635 ext2_filsys fs, old_fs;
636 ext2fs_block_bitmap meta_bmap;
24b2c7a7 637
c762c8e6
TT
638 fs = rfs->new_fs;
639 old_fs = rfs->old_fs;
640 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
641 fs = rfs->old_fs;
642
a13575f4 643 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
1e1da29f 644 &rfs->reserve_blocks);
24b2c7a7
TT
645 if (retval)
646 return retval;
1e1da29f 647
a13575f4 648 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
c762c8e6
TT
649 &rfs->move_blocks);
650 if (retval)
651 return retval;
652
64ad98ac
TT
653 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
654 &meta_bmap);
655 if (retval)
656 return retval;
657
658 retval = mark_table_blocks(old_fs, meta_bmap);
c762c8e6
TT
659 if (retval)
660 return retval;
661
662 fs = rfs->new_fs;
663
1e1da29f
TT
664 /*
665 * If we're shrinking the filesystem, we need to move all of
666 * the blocks that don't fit any more
667 */
668 for (blk = fs->super->s_blocks_count;
c762c8e6
TT
669 blk < old_fs->super->s_blocks_count; blk++) {
670 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
671 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
672 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
1e1da29f 673 rfs->needed_blocks++;
c762c8e6 674 }
1e1da29f
TT
675 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
676 }
24b2c7a7 677
76dd5e5c
TT
678 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
679 old_blocks = old_fs->super->s_first_meta_bg;
680 new_blocks = fs->super->s_first_meta_bg;
681 } else {
9213a93b
TT
682 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
683 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
76dd5e5c
TT
684 }
685
c762c8e6
TT
686 if (old_blocks == new_blocks) {
687 retval = 0;
688 goto errout;
689 }
24b2c7a7 690
1333fe93
TT
691 max_groups = fs->group_desc_count;
692 if (max_groups > old_fs->group_desc_count)
693 max_groups = old_fs->group_desc_count;
c762c8e6 694 group_blk = old_fs->super->s_first_data_block;
24b2c7a7
TT
695 /*
696 * If we're reducing the number of descriptor blocks, this
697 * makes life easy. :-) We just have to mark some extra
698 * blocks as free.
699 */
700 if (old_blocks > new_blocks) {
1333fe93 701 for (i = 0; i < max_groups; i++) {
1e1da29f
TT
702 if (!ext2fs_bg_has_super(fs, i)) {
703 group_blk += fs->super->s_blocks_per_group;
24b2c7a7
TT
704 continue;
705 }
c762c8e6
TT
706 for (blk = group_blk+1+new_blocks;
707 blk < group_blk+1+old_blocks; blk++) {
1e1da29f 708 ext2fs_unmark_block_bitmap(fs->block_map,
24b2c7a7 709 blk);
052db4b7
TT
710 rfs->needed_blocks--;
711 }
1e1da29f 712 group_blk += fs->super->s_blocks_per_group;
24b2c7a7 713 }
c762c8e6
TT
714 retval = 0;
715 goto errout;
24b2c7a7
TT
716 }
717 /*
718 * If we're increasing the number of descriptor blocks, life
1e1da29f 719 * gets interesting....
24b2c7a7 720 */
f2de1d38 721 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
1333fe93 722 for (i = 0; i < max_groups; i++) {
76dd5e5c
TT
723 has_super = ext2fs_bg_has_super(fs, i);
724 if (has_super)
725 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
726
727 meta_bg = i / meta_bg_size;
728 if (!(fs->super->s_feature_incompat &
729 EXT2_FEATURE_INCOMPAT_META_BG) ||
730 (meta_bg < fs->super->s_first_meta_bg)) {
424cb7b6
TT
731 if (has_super) {
732 for (blk = group_blk+1;
733 blk < group_blk + 1 + new_blocks; blk++)
734 mark_fs_metablock(rfs, meta_bmap,
735 i, blk);
736 }
76dd5e5c
TT
737 } else {
738 if (has_super)
739 has_super = 1;
740 if (((i % meta_bg_size) == 0) ||
741 ((i % meta_bg_size) == 1) ||
742 ((i % meta_bg_size) == (meta_bg_size-1)))
743 mark_fs_metablock(rfs, meta_bmap, i,
744 group_blk + has_super);
24b2c7a7 745 }
76dd5e5c 746
1e1da29f
TT
747 if (fs->group_desc[i].bg_inode_table &&
748 fs->group_desc[i].bg_inode_bitmap &&
749 fs->group_desc[i].bg_block_bitmap)
750 goto next_group;
24b2c7a7 751
1e1da29f 752 /*
c762c8e6
TT
753 * Reserve the existing meta blocks that we know
754 * aren't to be moved.
1e1da29f
TT
755 */
756 if (fs->group_desc[i].bg_block_bitmap)
757 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
758 fs->group_desc[i].bg_block_bitmap);
759 if (fs->group_desc[i].bg_inode_bitmap)
760 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
761 fs->group_desc[i].bg_inode_bitmap);
762 if (fs->group_desc[i].bg_inode_table)
763 for (blk = 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,
766 blk);
24b2c7a7 767
c762c8e6
TT
768 /*
769 * Allocate the missing data structures
770 */
1e1da29f
TT
771 retval = ext2fs_allocate_group_table(fs, i,
772 rfs->reserve_blocks);
773 if (retval)
c762c8e6 774 goto errout;
24b2c7a7 775
1e1da29f 776 /*
c762c8e6
TT
777 * For those structures that have changed, we need to
778 * do bookkeepping.
1e1da29f 779 */
c762c8e6
TT
780 if (FS_BLOCK_BM(old_fs, i) !=
781 (blk = FS_BLOCK_BM(fs, i))) {
782 ext2fs_mark_block_bitmap(fs->block_map, blk);
783 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
784 !ext2fs_test_block_bitmap(meta_bmap, blk))
785 ext2fs_mark_block_bitmap(rfs->move_blocks,
786 blk);
787 }
788 if (FS_INODE_BM(old_fs, i) !=
789 (blk = FS_INODE_BM(fs, i))) {
790 ext2fs_mark_block_bitmap(fs->block_map, blk);
791 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
792 !ext2fs_test_block_bitmap(meta_bmap, blk))
793 ext2fs_mark_block_bitmap(rfs->move_blocks,
794 blk);
795 }
24b2c7a7 796
052db4b7
TT
797 /*
798 * The inode table, if we need to relocate it, is
799 * handled specially. We have to reserve the blocks
800 * for both the old and the new inode table, since we
801 * can't have the inode table be destroyed during the
802 * block relocation phase.
803 */
c762c8e6 804 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
052db4b7
TT
805 goto next_group; /* inode table not moved */
806
c762c8e6 807 rfs->needed_blocks += fs->inode_blocks_per_group;
052db4b7
TT
808
809 /*
810 * Mark the new inode table as in use in the new block
c762c8e6
TT
811 * allocation bitmap, and move any blocks that might
812 * be necessary.
052db4b7 813 */
1e1da29f 814 for (blk = fs->group_desc[i].bg_inode_table, j=0;
c762c8e6 815 j < fs->inode_blocks_per_group ; j++, blk++) {
1e1da29f 816 ext2fs_mark_block_bitmap(fs->block_map, blk);
c762c8e6
TT
817 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
818 !ext2fs_test_block_bitmap(meta_bmap, blk))
819 ext2fs_mark_block_bitmap(rfs->move_blocks,
820 blk);
821 }
822
1e1da29f 823 /*
052db4b7
TT
824 * Make sure the old inode table is reserved in the
825 * block reservation bitmap.
1e1da29f 826 */
052db4b7
TT
827 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
828 j < fs->inode_blocks_per_group ; j++, blk++)
829 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
1e1da29f
TT
830
831 next_group:
832 group_blk += rfs->new_fs->super->s_blocks_per_group;
833 }
c762c8e6
TT
834 retval = 0;
835
836errout:
837 if (meta_bmap)
838 ext2fs_free_block_bitmap(meta_bmap);
839
840 return retval;
1e1da29f 841}
24b2c7a7 842
a8519a2d
TT
843/*
844 * This helper function tries to allocate a new block. We try to
845 * avoid hitting the original group descriptor blocks at least at
846 * first, since we want to make it possible to recover from a badly
847 * aborted resize operation as much as possible.
848 *
849 * In the future, I may further modify this routine to balance out
850 * where we get the new blocks across the various block groups.
851 * Ideally we would allocate blocks that corresponded with the block
852 * group of the containing inode, and keep contiguous blocks
853 * together. However, this very difficult to do efficiently, since we
854 * don't have the necessary information up front.
855 */
856
857#define AVOID_OLD 1
858#define DESPERATION 2
859
860static void init_block_alloc(ext2_resize_t rfs)
861{
862 rfs->alloc_state = AVOID_OLD;
863 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
2bc4d4f7
TT
864#if 0
865 /* HACK for testing */
866 if (rfs->new_fs->super->s_blocks_count >
867 rfs->old_fs->super->s_blocks_count)
868 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
869#endif
a8519a2d
TT
870}
871
872static blk_t get_new_block(ext2_resize_t rfs)
873{
874 ext2_filsys fs = rfs->new_fs;
875
876 while (1) {
877 if (rfs->new_blk >= fs->super->s_blocks_count) {
878 if (rfs->alloc_state == DESPERATION)
879 return 0;
880
881#ifdef RESIZE2FS_DEBUG
882 if (rfs->flags & RESIZE_DEBUG_BMOVE)
f35fd3d5
TT
883 printf("Going into desperation mode "
884 "for block allocations\n");
a8519a2d
TT
885#endif
886 rfs->alloc_state = DESPERATION;
887 rfs->new_blk = fs->super->s_first_data_block;
888 continue;
889 }
890 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
891 ext2fs_test_block_bitmap(rfs->reserve_blocks,
892 rfs->new_blk) ||
893 ((rfs->alloc_state == AVOID_OLD) &&
bce49798 894 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
a8519a2d
TT
895 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
896 rfs->new_blk))) {
897 rfs->new_blk++;
898 continue;
899 }
900 return rfs->new_blk;
901 }
902}
903
904static errcode_t block_mover(ext2_resize_t rfs)
905{
906 blk_t blk, old_blk, new_blk;
907 ext2_filsys fs = rfs->new_fs;
908 ext2_filsys old_fs = rfs->old_fs;
909 errcode_t retval;
910 int size, c;
911 int to_move, moved;
7d7bdd57
TT
912 ext2_badblocks_list badblock_list = 0;
913 int bb_modified = 0;
914
915 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
916 if (retval)
917 return retval;
a8519a2d
TT
918
919 new_blk = fs->super->s_first_data_block;
920 if (!rfs->itable_buf) {
e5aace90 921 retval = ext2fs_get_array(fs->blocksize,
a8519a2d 922 fs->inode_blocks_per_group,
c4e3d3f3 923 &rfs->itable_buf);
a8519a2d
TT
924 if (retval)
925 return retval;
926 }
927 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
928 if (retval)
929 return retval;
930
931 /*
932 * The first step is to figure out where all of the blocks
933 * will go.
934 */
935 to_move = moved = 0;
936 init_block_alloc(rfs);
937 for (blk = old_fs->super->s_first_data_block;
938 blk < old_fs->super->s_blocks_count; blk++) {
939 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
940 continue;
941 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
942 continue;
7d7bdd57
TT
943 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
944 ext2fs_badblocks_list_del(badblock_list, blk);
945 bb_modified++;
946 continue;
947 }
a8519a2d
TT
948
949 new_blk = get_new_block(rfs);
950 if (!new_blk) {
951 retval = ENOSPC;
952 goto errout;
953 }
954 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
955 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
956 to_move++;
957 }
958
959 if (to_move == 0) {
cefbf487
TT
960 if (rfs->bmap) {
961 ext2fs_free_extent_table(rfs->bmap);
962 rfs->bmap = 0;
963 }
a8519a2d
TT
964 retval = 0;
965 goto errout;
966 }
967
968 /*
969 * Step two is to actually move the blocks
970 */
971 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
972 if (retval) goto errout;
973
3b627e8d
TT
974 if (rfs->progress) {
975 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
976 0, to_move);
977 if (retval)
978 goto errout;
979 }
a8519a2d
TT
980 while (1) {
981 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
982 if (retval) goto errout;
983 if (!size)
984 break;
985#ifdef RESIZE2FS_DEBUG
986 if (rfs->flags & RESIZE_DEBUG_BMOVE)
f35fd3d5
TT
987 printf("Moving %d blocks %u->%u\n",
988 size, old_blk, new_blk);
a8519a2d
TT
989#endif
990 do {
991 c = size;
992 if (c > fs->inode_blocks_per_group)
993 c = fs->inode_blocks_per_group;
994 retval = io_channel_read_blk(fs->io, old_blk, c,
995 rfs->itable_buf);
996 if (retval) goto errout;
997 retval = io_channel_write_blk(fs->io, new_blk, c,
998 rfs->itable_buf);
999 if (retval) goto errout;
1000 size -= c;
1001 new_blk += c;
1002 old_blk += c;
1003 moved += c;
1004 if (rfs->progress) {
1005 io_channel_flush(fs->io);
3b627e8d
TT
1006 retval = (rfs->progress)(rfs,
1007 E2_RSZ_BLOCK_RELOC_PASS,
a8519a2d 1008 moved, to_move);
3b627e8d
TT
1009 if (retval)
1010 goto errout;
a8519a2d
TT
1011 }
1012 } while (size > 0);
1013 io_channel_flush(fs->io);
1014 }
1015
1016errout:
7d7bdd57
TT
1017 if (badblock_list) {
1018 if (!retval && bb_modified)
1019 retval = ext2fs_update_bb_inode(old_fs,
1020 badblock_list);
1021 ext2fs_badblocks_list_free(badblock_list);
1022 }
a8519a2d
TT
1023 return retval;
1024}
1025
1026
1027/* --------------------------------------------------------------------
1028 *
1029 * Resize processing, phase 3
1030 *
1031 * --------------------------------------------------------------------
1032 */
1033
1034
1035struct process_block_struct {
1036 ext2_resize_t rfs;
dfcdc32f 1037 ext2_ino_t ino;
a8519a2d
TT
1038 struct ext2_inode * inode;
1039 errcode_t error;
1040 int is_dir;
1041 int changed;
1042};
1043
1044static int process_block(ext2_filsys fs, blk_t *block_nr,
54434927
TT
1045 e2_blkcnt_t blockcnt,
1046 blk_t ref_block EXT2FS_ATTR((unused)),
1047 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
a8519a2d
TT
1048{
1049 struct process_block_struct *pb;
1050 errcode_t retval;
1051 blk_t block, new_block;
1052 int ret = 0;
1053
1054 pb = (struct process_block_struct *) priv_data;
1055 block = *block_nr;
1056 if (pb->rfs->bmap) {
1057 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1058 if (new_block) {
1059 *block_nr = new_block;
1060 ret |= BLOCK_CHANGED;
1061 pb->changed = 1;
1062#ifdef RESIZE2FS_DEBUG
1063 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
f35fd3d5 1064 printf("ino=%u, blockcnt=%lld, %u->%u\n",
a8519a2d
TT
1065 pb->ino, blockcnt, block, new_block);
1066#endif
1067 block = new_block;
1068 }
1069 }
1070 if (pb->is_dir) {
1071 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
101c84f2 1072 block, (int) blockcnt);
a8519a2d
TT
1073 if (retval) {
1074 pb->error = retval;
1075 ret |= BLOCK_ABORT;
1076 }
1077 }
1078 return ret;
1079}
1080
1081/*
1082 * Progress callback
1083 */
54434927
TT
1084static errcode_t progress_callback(ext2_filsys fs,
1085 ext2_inode_scan scan EXT2FS_ATTR((unused)),
a8519a2d
TT
1086 dgrp_t group, void * priv_data)
1087{
1088 ext2_resize_t rfs = (ext2_resize_t) priv_data;
3b627e8d 1089 errcode_t retval;
a8519a2d
TT
1090
1091 /*
f4b2a6db
TT
1092 * This check is to protect against old ext2 libraries. It
1093 * shouldn't be needed against new libraries.
a8519a2d 1094 */
f4b2a6db 1095 if ((group+1) == 0)
a8519a2d
TT
1096 return 0;
1097
1098 if (rfs->progress) {
1099 io_channel_flush(fs->io);
3b627e8d
TT
1100 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1101 group+1, fs->group_desc_count);
1102 if (retval)
1103 return retval;
a8519a2d
TT
1104 }
1105
1106 return 0;
1107}
1108
1109static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1110{
1111 struct process_block_struct pb;
dfcdc32f 1112 ext2_ino_t ino, new_inode;
edfd9b0a 1113 struct ext2_inode *inode = NULL;
a8519a2d
TT
1114 ext2_inode_scan scan = NULL;
1115 errcode_t retval;
1116 int group;
1117 char *block_buf = 0;
dfcdc32f 1118 ext2_ino_t start_to_move;
0ccd488a 1119 blk_t orig_size, new_block;
4ef28824 1120 int inode_size;
a8519a2d
TT
1121
1122 if ((rfs->old_fs->group_desc_count <=
1123 rfs->new_fs->group_desc_count) &&
1124 !rfs->bmap)
1125 return 0;
1126
2bc4d4f7
TT
1127 /*
1128 * Save the original size of the old filesystem, and
1129 * temporarily set the size to be the new size if the new size
1130 * is larger. We need to do this to avoid catching an error
1131 * by the block iterator routines
1132 */
1133 orig_size = rfs->old_fs->super->s_blocks_count;
1134 if (orig_size < rfs->new_fs->super->s_blocks_count)
1135 rfs->old_fs->super->s_blocks_count =
1136 rfs->new_fs->super->s_blocks_count;
1137
a8519a2d
TT
1138 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1139 if (retval) goto errout;
1140
1141 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1142 if (retval) goto errout;
e5aace90 1143 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
a8519a2d
TT
1144 if (retval) goto errout;
1145
1146 start_to_move = (rfs->new_fs->group_desc_count *
1147 rfs->new_fs->super->s_inodes_per_group);
1148
3b627e8d
TT
1149 if (rfs->progress) {
1150 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1151 0, rfs->old_fs->group_desc_count);
1152 if (retval)
1153 goto errout;
1154 }
a8519a2d
TT
1155 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1156 pb.rfs = rfs;
edfd9b0a 1157 pb.inode = inode;
a8519a2d
TT
1158 pb.error = 0;
1159 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
4ef28824 1160 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
edfd9b0a
TT
1161 inode = malloc(inode_size);
1162 if (!inode) {
4ef28824
ES
1163 retval = ENOMEM;
1164 goto errout;
1165 }
a8519a2d
TT
1166 /*
1167 * First, copy all of the inodes that need to be moved
1168 * elsewhere in the inode table
1169 */
1170 while (1) {
edfd9b0a 1171 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
a8519a2d
TT
1172 if (retval) goto errout;
1173 if (!ino)
1174 break;
1175
edfd9b0a 1176 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
a8519a2d
TT
1177 continue; /* inode not in use */
1178
edfd9b0a 1179 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
a8519a2d
TT
1180 pb.changed = 0;
1181
edfd9b0a 1182 if (inode->i_file_acl && rfs->bmap) {
0ccd488a 1183 new_block = ext2fs_extent_translate(rfs->bmap,
edfd9b0a 1184 inode->i_file_acl);
ed909bbe 1185 if (new_block) {
edfd9b0a
TT
1186 inode->i_file_acl = new_block;
1187 retval = ext2fs_write_inode_full(rfs->old_fs,
1188 ino, inode, inode_size);
ed909bbe
TT
1189 if (retval) goto errout;
1190 }
1191 }
1192
edfd9b0a 1193 if (ext2fs_inode_has_valid_blocks(inode) &&
a8519a2d
TT
1194 (rfs->bmap || pb.is_dir)) {
1195 pb.ino = ino;
1196 retval = ext2fs_block_iterate2(rfs->old_fs,
1197 ino, 0, block_buf,
1198 process_block, &pb);
1199 if (retval)
1200 goto errout;
1201 if (pb.error) {
1202 retval = pb.error;
1203 goto errout;
1204 }
1205 }
1206
1207 if (ino <= start_to_move)
1208 continue; /* Don't need to move it. */
1209
1210 /*
1211 * Find a new inode
1212 */
1213 while (1) {
1214 if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map,
1215 new_inode))
1216 break;
1217 new_inode++;
1218 if (new_inode > rfs->new_fs->super->s_inodes_count) {
1219 retval = ENOSPC;
1220 goto errout;
1221 }
1222 }
1223 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1224 if (pb.changed) {
1225 /* Get the new version of the inode */
4ef28824 1226 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
edfd9b0a 1227 inode, inode_size);
a8519a2d
TT
1228 if (retval) goto errout;
1229 }
edfd9b0a 1230 inode->i_ctime = time(0);
4ef28824 1231 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
edfd9b0a 1232 inode, inode_size);
a8519a2d
TT
1233 if (retval) goto errout;
1234
1235 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
edfd9b0a 1236 if (LINUX_S_ISDIR(inode->i_mode))
a8519a2d
TT
1237 rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1238
1239#ifdef RESIZE2FS_DEBUG
1240 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
f35fd3d5 1241 printf("Inode moved %u->%u\n", ino, new_inode);
a8519a2d
TT
1242#endif
1243 if (!rfs->imap) {
1244 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1245 if (retval)
1246 goto errout;
1247 }
1248 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1249 }
1250 io_channel_flush(rfs->old_fs->io);
1251
1252errout:
2bc4d4f7 1253 rfs->old_fs->super->s_blocks_count = orig_size;
a8519a2d
TT
1254 if (rfs->bmap) {
1255 ext2fs_free_extent_table(rfs->bmap);
1256 rfs->bmap = 0;
1257 }
1258 if (scan)
1259 ext2fs_close_inode_scan(scan);
1260 if (block_buf)
c4e3d3f3 1261 ext2fs_free_mem(&block_buf);
edfd9b0a
TT
1262 if (inode)
1263 free(inode);
a8519a2d
TT
1264 return retval;
1265}
1266
1267/* --------------------------------------------------------------------
1268 *
1269 * Resize processing, phase 4.
1270 *
1271 * --------------------------------------------------------------------
1272 */
1273
1274struct istruct {
1275 ext2_resize_t rfs;
3b627e8d 1276 errcode_t err;
1333fe93 1277 unsigned long max_dirs;
a8519a2d
TT
1278 int num;
1279};
1280
54434927
TT
1281static int check_and_change_inodes(ext2_ino_t dir,
1282 int entry EXT2FS_ATTR((unused)),
a8519a2d 1283 struct ext2_dir_entry *dirent, int offset,
54434927
TT
1284 int blocksize EXT2FS_ATTR((unused)),
1285 char *buf EXT2FS_ATTR((unused)),
1286 void *priv_data)
a8519a2d
TT
1287{
1288 struct istruct *is = (struct istruct *) priv_data;
085d2a83
TT
1289 struct ext2_inode inode;
1290 ext2_ino_t new_inode;
1291 errcode_t retval;
a8519a2d
TT
1292
1293 if (is->rfs->progress && offset == 0) {
1294 io_channel_flush(is->rfs->old_fs->io);
3b627e8d
TT
1295 is->err = (is->rfs->progress)(is->rfs,
1296 E2_RSZ_INODE_REF_UPD_PASS,
1333fe93 1297 ++is->num, is->max_dirs);
3b627e8d
TT
1298 if (is->err)
1299 return DIRENT_ABORT;
a8519a2d
TT
1300 }
1301
1302 if (!dirent->inode)
1303 return 0;
1304
1305 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1306
1307 if (!new_inode)
1308 return 0;
1309#ifdef RESIZE2FS_DEBUG
1310 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
f35fd3d5 1311 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
06191693 1312 dir, dirent->name_len&0xFF, dirent->name,
a8519a2d
TT
1313 dirent->inode, new_inode);
1314#endif
1315
1316 dirent->inode = new_inode;
1317
085d2a83
TT
1318 /* Update the directory mtime and ctime */
1319 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1320 if (retval == 0) {
1321 inode.i_mtime = inode.i_ctime = time(0);
d2b2a488
BB
1322 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1323 if (is->err)
1324 return DIRENT_ABORT;
085d2a83
TT
1325 }
1326
a8519a2d
TT
1327 return DIRENT_CHANGED;
1328}
1329
1330static errcode_t inode_ref_fix(ext2_resize_t rfs)
1331{
1332 errcode_t retval;
1333 struct istruct is;
1334
1335 if (!rfs->imap)
1336 return 0;
1337
1338 /*
1339 * Now, we iterate over all of the directories to update the
1340 * inode references
1341 */
1342 is.num = 0;
1333fe93 1343 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
a8519a2d 1344 is.rfs = rfs;
3b627e8d 1345 is.err = 0;
a8519a2d 1346
3b627e8d
TT
1347 if (rfs->progress) {
1348 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1333fe93 1349 0, is.max_dirs);
3b627e8d
TT
1350 if (retval)
1351 goto errout;
1352 }
a8519a2d
TT
1353
1354 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1355 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1356 check_and_change_inodes, &is);
3b627e8d
TT
1357 if (retval)
1358 goto errout;
1359 if (is.err) {
1360 retval = is.err;
1361 goto errout;
1362 }
a8519a2d 1363
3b627e8d 1364errout:
a8519a2d
TT
1365 ext2fs_free_extent_table(rfs->imap);
1366 rfs->imap = 0;
1367 return retval;
1368}
1369
1370
1371/* --------------------------------------------------------------------
1372 *
1373 * Resize processing, phase 5.
1374 *
1375 * In this phase we actually move the inode table around, and then
1376 * update the summary statistics. This is scary, since aborting here
1377 * will potentially scramble the filesystem. (We are moving the
1378 * inode tables around in place, and so the potential for lost data,
1379 * or at the very least scrambling the mapping between filenames and
1380 * inode numbers is very high in case of a power failure here.)
1381 * --------------------------------------------------------------------
1382 */
1383
24b2c7a7 1384
052db4b7
TT
1385/*
1386 * A very scary routine --- this one moves the inode table around!!!
1387 *
1388 * After this you have to use the rfs->new_fs file handle to read and
1389 * write inodes.
1390 */
c762c8e6 1391static errcode_t move_itables(ext2_resize_t rfs)
052db4b7 1392{
54434927
TT
1393 int n, num, size, diff;
1394 dgrp_t i, max_groups;
052db4b7 1395 ext2_filsys fs = rfs->new_fs;
05e112a1 1396 char *cp;
64ad98ac 1397 blk_t old_blk, new_blk, blk;
a8519a2d 1398 errcode_t retval;
64ad98ac 1399 int j, to_move, moved;
052db4b7 1400
1333fe93
TT
1401 max_groups = fs->group_desc_count;
1402 if (max_groups > rfs->old_fs->group_desc_count)
1403 max_groups = rfs->old_fs->group_desc_count;
052db4b7 1404
05e112a1
TT
1405 size = fs->blocksize * fs->inode_blocks_per_group;
1406 if (!rfs->itable_buf) {
c4e3d3f3 1407 retval = ext2fs_get_mem(size, &rfs->itable_buf);
ca8abba7
TT
1408 if (retval)
1409 return retval;
05e112a1 1410 }
c762c8e6
TT
1411
1412 /*
1413 * Figure out how many inode tables we need to move
1414 */
1415 to_move = moved = 0;
1333fe93 1416 for (i=0; i < max_groups; i++)
c762c8e6
TT
1417 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1418 fs->group_desc[i].bg_inode_table)
1419 to_move++;
1420
1421 if (to_move == 0)
1422 return 0;
1423
3b627e8d
TT
1424 if (rfs->progress) {
1425 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1426 0, to_move);
1427 if (retval)
1428 goto errout;
1429 }
63b44fbe 1430
a8519a2d
TT
1431 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1432
1333fe93 1433 for (i=0; i < max_groups; i++) {
ca8abba7
TT
1434 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1435 new_blk = fs->group_desc[i].bg_inode_table;
1436 diff = new_blk - old_blk;
052db4b7 1437
80c0fc34 1438#ifdef RESIZE2FS_DEBUG
05e112a1 1439 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
f35fd3d5 1440 printf("Itable move group %d block %u->%u (diff %d)\n",
ca8abba7 1441 i, old_blk, new_blk, diff);
80c0fc34 1442#endif
052db4b7 1443
05e112a1 1444 if (!diff)
052db4b7
TT
1445 continue;
1446
ca8abba7 1447 retval = io_channel_read_blk(fs->io, old_blk,
05e112a1
TT
1448 fs->inode_blocks_per_group,
1449 rfs->itable_buf);
052db4b7 1450 if (retval)
a8519a2d 1451 goto errout;
05e112a1
TT
1452 /*
1453 * The end of the inode table segment often contains
a8519a2d
TT
1454 * all zeros, and we're often only moving the inode
1455 * table down a block or two. If so, we can optimize
1456 * things by not rewriting blocks that we know to be zero
1457 * already.
05e112a1 1458 */
2787276e 1459 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
05e112a1
TT
1460 if (*cp)
1461 break;
1462 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
80c0fc34 1463#ifdef RESIZE2FS_DEBUG
05e112a1 1464 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
f35fd3d5 1465 printf("%d blocks of zeros...\n", n);
80c0fc34 1466#endif
05e112a1
TT
1467 num = fs->inode_blocks_per_group;
1468 if (n > diff)
1469 num -= n;
1470
ca8abba7 1471 retval = io_channel_write_blk(fs->io, new_blk,
05e112a1 1472 num, rfs->itable_buf);
052db4b7 1473 if (retval) {
ca8abba7 1474 io_channel_write_blk(fs->io, old_blk,
05e112a1 1475 num, rfs->itable_buf);
a8519a2d 1476 goto errout;
052db4b7 1477 }
05e112a1
TT
1478 if (n > diff) {
1479 retval = io_channel_write_blk(fs->io,
ca8abba7 1480 old_blk + fs->inode_blocks_per_group,
a8519a2d
TT
1481 diff, (rfs->itable_buf +
1482 (fs->inode_blocks_per_group - diff) *
1483 fs->blocksize));
05e112a1 1484 if (retval)
a8519a2d 1485 goto errout;
c762c8e6 1486 }
64ad98ac
TT
1487
1488 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1489 j < fs->inode_blocks_per_group ; j++, blk++)
1490 ext2fs_unmark_block_bitmap(fs->block_map, blk);
1491
a8519a2d
TT
1492 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1493 ext2fs_mark_super_dirty(rfs->old_fs);
64ad98ac
TT
1494 ext2fs_flush(rfs->old_fs);
1495
a8519a2d 1496 if (rfs->progress) {
3b627e8d
TT
1497 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1498 ++moved, to_move);
1499 if (retval)
1500 goto errout;
a8519a2d 1501 }
052db4b7 1502 }
64ad98ac 1503 mark_table_blocks(fs, fs->block_map);
a8519a2d 1504 ext2fs_flush(fs);
80c0fc34 1505#ifdef RESIZE2FS_DEBUG
05e112a1 1506 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
f35fd3d5 1507 printf("Inode table move finished.\n");
80c0fc34 1508#endif
052db4b7
TT
1509 return 0;
1510
a8519a2d 1511errout:
052db4b7
TT
1512 return retval;
1513}
1514
9213a93b
TT
1515/*
1516 * Fix the resize inode
1517 */
1518static errcode_t fix_resize_inode(ext2_filsys fs)
1519{
1520 struct ext2_inode inode;
1521 errcode_t retval;
1522 char * block_buf;
1523
1524 if (!(fs->super->s_feature_compat &
1525 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1526 return 0;
1527
1528 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1529 if (retval) goto errout;
1530
1531 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1532 if (retval) goto errout;
1533
1534 inode.i_blocks = fs->blocksize/512;
1535
1536 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1537 if (retval) goto errout;
1538
1539 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1540 /*
1541 * Avoid zeroing out block #0; that's rude. This
1542 * should never happen anyway since the filesystem
1543 * should be fsck'ed and we assume it is consistent.
1544 */
1545 fprintf(stderr,
f35fd3d5 1546 _("Should never happen: resize inode corrupt!\n"));
9213a93b
TT
1547 exit(1);
1548 }
1549
1550 memset(block_buf, 0, fs->blocksize);
1551
1552 retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1553 1, block_buf);
1554 if (retval) goto errout;
1555
1556 retval = ext2fs_create_resize_inode(fs);
1557 if (retval)
1558 goto errout;
1559
1560errout:
1561 if (block_buf)
1562 ext2fs_free_mem(&block_buf);
1563 return retval;
1564}
1565
052db4b7
TT
1566/*
1567 * Finally, recalculate the summary information
1568 */
1569static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1570{
dfcdc32f
TT
1571 blk_t blk;
1572 ext2_ino_t ino;
54434927
TT
1573 unsigned int group = 0;
1574 unsigned int count = 0;
dfcdc32f
TT
1575 int total_free = 0;
1576 int group_free = 0;
052db4b7
TT
1577
1578 /*
1579 * First calculate the block statistics
1580 */
1581 for (blk = fs->super->s_first_data_block;
1582 blk < fs->super->s_blocks_count; blk++) {
1583 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1584 group_free++;
1585 total_free++;
1586 }
1587 count++;
1588 if ((count == fs->super->s_blocks_per_group) ||
1589 (blk == fs->super->s_blocks_count-1)) {
1590 fs->group_desc[group++].bg_free_blocks_count =
1591 group_free;
1592 count = 0;
1593 group_free = 0;
1594 }
1595 }
1596 fs->super->s_free_blocks_count = total_free;
1597
1598 /*
1599 * Next, calculate the inode statistics
1600 */
1601 group_free = 0;
1602 total_free = 0;
1603 count = 0;
1604 group = 0;
5830d6be
ES
1605
1606 /* Protect loop from wrap-around if s_inodes_count maxed */
1607 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
052db4b7
TT
1608 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1609 group_free++;
1610 total_free++;
1611 }
1612 count++;
1613 if ((count == fs->super->s_inodes_per_group) ||
1614 (ino == fs->super->s_inodes_count)) {
1615 fs->group_desc[group++].bg_free_inodes_count =
1616 group_free;
1617 count = 0;
1618 group_free = 0;
1619 }
1620 }
1621 fs->super->s_free_inodes_count = total_free;
1622 ext2fs_mark_super_dirty(fs);
1623 return 0;
1624}
199ddaaa
JB
1625
1626/*
1627 * calcluate the minimum number of blocks the given fs can be resized to
1628 */
1629blk_t calculate_minimum_resize_size(ext2_filsys fs)
1630{
1631 blk_t inode_count, blks_needed, groups, blk, data_blocks;
1632 blk_t grp, data_needed, last_start;
1633 int overhead = 0, old_group = -1, num_of_superblocks = 0;
1634
1635 /*
1636 * first figure out how many group descriptors we need to
1637 * handle the number of inodes we have
1638 */
1639 inode_count = fs->super->s_inodes_count -
1640 fs->super->s_free_inodes_count;
1641 blks_needed = ext2fs_div_ceil(inode_count,
1642 fs->super->s_inodes_per_group) *
1643 EXT2_BLOCKS_PER_GROUP(fs->super);
1644 groups = ext2fs_div_ceil(blks_needed,
1645 EXT2_BLOCKS_PER_GROUP(fs->super));
1646
1647 /*
1648 * we need to figure out how many backup superblocks we have so we can
1649 * account for that in the metadata
1650 */
1651 for (grp = 0; grp < fs->group_desc_count; grp++) {
1652 if (ext2fs_bg_has_super(fs, grp))
1653 num_of_superblocks++;
1654 }
1655
1656 /* calculate how many blocks are needed for data */
1657 data_needed = fs->super->s_blocks_count -
1658 fs->super->s_free_blocks_count;
1659 data_needed -= SUPER_OVERHEAD(fs) * num_of_superblocks;
1660 data_needed -= META_OVERHEAD(fs) * fs->group_desc_count;
1661
1662 /*
1663 * figure out how many data blocks we have given the number of groups
1664 * we need for our inodes
1665 */
1666 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
1667 last_start = 0;
1668 for (grp = 0; grp < groups; grp++) {
1669 overhead = META_OVERHEAD(fs);
1670
1671 if (ext2fs_bg_has_super(fs, grp))
1672 overhead += SUPER_OVERHEAD(fs);
1673
1674 /*
1675 * we want to keep track of how much data we can store in
1676 * the groups leading up to the last group so we can determine
1677 * how big the last group needs to be
1678 */
1679 if (grp != (groups - 1))
1680 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
1681 overhead;
1682
1683 data_blocks -= overhead;
1684 }
1685
1686 /*
1687 * if we need more group descriptors in order to accomodate our data
1688 * then we need to add them here
1689 */
1690 while (data_needed > data_blocks) {
1691 blk_t remainder = data_needed - data_blocks;
1692 blk_t extra_grps;
1693
1694 /* figure out how many more groups we need for the data */
1695 extra_grps = ext2fs_div_ceil(remainder,
1696 EXT2_BLOCKS_PER_GROUP(fs->super));
1697
1698 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
1699
1700 /* ok we have to account for the last group */
1701 overhead = META_OVERHEAD(fs);
1702 if (ext2fs_bg_has_super(fs, groups-1))
1703 overhead += SUPER_OVERHEAD(fs);
1704 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
1705
1706 for (grp = groups; grp < groups+extra_grps; grp++) {
1707 overhead = META_OVERHEAD(fs);
1708 if (ext2fs_bg_has_super(fs, grp))
1709 overhead += SUPER_OVERHEAD(fs);
1710
1711 /*
1712 * again, we need to see how much data we cram into
1713 * all of the groups leading up to the last group
1714 */
1715 if (grp != (groups + extra_grps - 1))
1716 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
1717 - overhead;
1718
1719 data_blocks -= overhead;
1720 }
1721
1722 groups += extra_grps;
1723 }
1724
1725 /* now for the fun voodoo */
1726 overhead = META_OVERHEAD(fs);
1727
1728 /*
1729 * if this is the case then the last group is going to have data in it
1730 * so we need to adjust the size of the last group accordingly
1731 */
1732 if (last_start < data_needed) {
1733 blk_t remainder = data_needed - last_start;
1734
1735 /*
1736 * 50 is a magic number that mkfs/resize uses to see if its
1737 * even worth making/resizing the fs. basically you need to
1738 * have at least 50 blocks in addition to the blocks needed
1739 * for the metadata in the last group
1740 */
1741 if (remainder > 50)
1742 overhead += remainder;
1743 else
1744 overhead += 50;
1745 } else
1746 overhead += 50;
1747
1748 if (ext2fs_bg_has_super(fs, groups-1))
1749 overhead += SUPER_OVERHEAD(fs);
1750
1751 /*
1752 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
1753 * only do groups-1, and then add the number of blocks needed to
1754 * handle the group descriptor metadata+data that we need
1755 */
1756 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
1757 blks_needed += overhead;
1758
1759 return blks_needed;
1760}