]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - resize/resize2fs.c
Clean up sparse warnings
[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
efc6f628 8 *
24b2c7a7 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
d1154eb4 36#include "config.h"
24b2c7a7 37#include "resize2fs.h"
b969b1b8 38#include <time.h>
24b2c7a7 39
546a1ff1 40#ifdef __linux__ /* Kludge for debugging */
a8519a2d
TT
41#define RESIZE2FS_DEBUG
42#endif
43
86acdebd 44static void fix_uninit_block_bitmaps(ext2_filsys fs);
8728d506 45static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size);
a8519a2d
TT
46static errcode_t blocks_to_move(ext2_resize_t rfs);
47static errcode_t block_mover(ext2_resize_t rfs);
48static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
49static errcode_t inode_ref_fix(ext2_resize_t rfs);
50static errcode_t move_itables(ext2_resize_t rfs);
9213a93b 51static errcode_t fix_resize_inode(ext2_filsys fs);
a8519a2d 52static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
8a6ede8b 53static errcode_t fix_sb_journal_backup(ext2_filsys fs);
f026f1a3
TT
54static errcode_t mark_table_blocks(ext2_filsys fs,
55 ext2fs_block_bitmap bmap);
a8519a2d
TT
56
57/*
58 * Some helper CPP macros
59 */
d7cca6b0
VAH
60#define IS_BLOCK_BM(fs, i, blk) ((blk) == ext2fs_block_bitmap_loc((fs),(i)))
61#define IS_INODE_BM(fs, i, blk) ((blk) == ext2fs_inode_bitmap_loc((fs),(i)))
a8519a2d 62
d7cca6b0
VAH
63#define IS_INODE_TB(fs, i, blk) (((blk) >= ext2fs_inode_table_loc((fs), (i))) && \
64 ((blk) < (ext2fs_inode_table_loc((fs), (i)) + \
a8519a2d
TT
65 (fs)->inode_blocks_per_group)))
66
1773c87c
TT
67/* Some bigalloc helper macros which are more succint... */
68#define B2C(x) EXT2FS_B2C(fs, (x))
69#define C2B(x) EXT2FS_C2B(fs, (x))
70#define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
71#define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
72#define LT_CLSTR(x, y) (B2C(x) < B2C(y))
73#define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
74#define GT_CLSTR(x, y) (B2C(x) > B2C(y))
75
f404167d 76static int lazy_itable_init;
2d8c0c8a 77
a8519a2d
TT
78/*
79 * This is the top-level routine which does the dirty deed....
80 */
8728d506 81errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
3346084b 82 errcode_t (*progress)(ext2_resize_t rfs, int pass,
86acdebd
TT
83 unsigned long cur,
84 unsigned long max_val))
a8519a2d
TT
85{
86 ext2_resize_t rfs;
87 errcode_t retval;
1eb31c48 88 struct resource_track rtrack, overall_track;
96cdb37e 89
a8519a2d
TT
90 /*
91 * Create the data structure
92 */
c4e3d3f3 93 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
a8519a2d
TT
94 if (retval)
95 return retval;
a8519a2d 96
1eb31c48 97 memset(rfs, 0, sizeof(struct ext2_resize_struct));
7f9c96ee 98 fs->priv_data = rfs;
a8519a2d
TT
99 rfs->old_fs = fs;
100 rfs->flags = flags;
101 rfs->itable_buf = 0;
102 rfs->progress = progress;
1eb31c48
TT
103
104 init_resource_track(&overall_track, "overall resize2fs", fs->io);
105 init_resource_track(&rtrack, "read_bitmaps", fs->io);
106 retval = ext2fs_read_bitmaps(fs);
107 if (retval)
108 goto errout;
109 print_resource_track(rfs, &rtrack, fs->io);
110
111 fs->super->s_state |= EXT2_ERROR_FS;
112 ext2fs_mark_super_dirty(fs);
113 ext2fs_flush(fs);
114
115 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io);
116 fix_uninit_block_bitmaps(fs);
117 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d
TT
118 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
119 if (retval)
120 goto errout;
121
1eb31c48 122 init_resource_track(&rtrack, "adjust_superblock", fs->io);
116db1b5 123 retval = adjust_superblock(rfs, *new_size);
a8519a2d
TT
124 if (retval)
125 goto errout;
1eb31c48 126 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d 127
1eb31c48
TT
128
129 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io);
86acdebd 130 fix_uninit_block_bitmaps(rfs->new_fs);
1eb31c48 131 print_resource_track(rfs, &rtrack, fs->io);
86acdebd 132 /* Clear the block bitmap uninit flag for the last block group */
e633b58a 133 ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
732c8cd5 134 EXT2_BG_BLOCK_UNINIT);
86acdebd 135
4efbac6f 136 *new_size = ext2fs_blocks_count(rfs->new_fs->super);
116db1b5 137
1eb31c48 138 init_resource_track(&rtrack, "blocks_to_move", fs->io);
a8519a2d
TT
139 retval = blocks_to_move(rfs);
140 if (retval)
141 goto errout;
1eb31c48 142 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d
TT
143
144#ifdef RESIZE2FS_DEBUG
145 if (rfs->flags & RESIZE_DEBUG_BMOVE)
8728d506 146 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
4efbac6f
VAH
147 ext2fs_free_blocks_count(rfs->old_fs->super),
148 ext2fs_free_blocks_count(rfs->new_fs->super),
a8519a2d
TT
149 rfs->needed_blocks);
150#endif
efc6f628 151
1eb31c48 152 init_resource_track(&rtrack, "block_mover", fs->io);
a8519a2d
TT
153 retval = block_mover(rfs);
154 if (retval)
155 goto errout;
1eb31c48 156 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d 157
1eb31c48 158 init_resource_track(&rtrack, "inode_scan_and_fix", fs->io);
a8519a2d
TT
159 retval = inode_scan_and_fix(rfs);
160 if (retval)
161 goto errout;
1eb31c48 162 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d 163
1eb31c48 164 init_resource_track(&rtrack, "inode_ref_fix", fs->io);
a8519a2d
TT
165 retval = inode_ref_fix(rfs);
166 if (retval)
167 goto errout;
1eb31c48 168 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d 169
1eb31c48 170 init_resource_track(&rtrack, "move_itables", fs->io);
a8519a2d
TT
171 retval = move_itables(rfs);
172 if (retval)
173 goto errout;
1eb31c48 174 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d 175
1eb31c48 176 init_resource_track(&rtrack, "calculate_summary_stats", fs->io);
64ad98ac
TT
177 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
178 if (retval)
179 goto errout;
1eb31c48 180 print_resource_track(rfs, &rtrack, fs->io);
efc6f628 181
1eb31c48 182 init_resource_track(&rtrack, "fix_resize_inode", fs->io);
9213a93b
TT
183 retval = fix_resize_inode(rfs->new_fs);
184 if (retval)
185 goto errout;
1eb31c48 186 print_resource_track(rfs, &rtrack, fs->io);
9213a93b 187
1eb31c48 188 init_resource_track(&rtrack, "fix_sb_journal_backup", fs->io);
8a6ede8b
ES
189 retval = fix_sb_journal_backup(rfs->new_fs);
190 if (retval)
191 goto errout;
1eb31c48 192 print_resource_track(rfs, &rtrack, fs->io);
8a6ede8b 193
96cdb37e 194 rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
efc6f628 195 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1eb31c48
TT
196
197 print_resource_track(rfs, &overall_track, fs->io);
a8519a2d
TT
198 retval = ext2fs_close(rfs->new_fs);
199 if (retval)
200 goto errout;
201
202 rfs->flags = flags;
efc6f628 203
a8519a2d
TT
204 ext2fs_free(rfs->old_fs);
205 if (rfs->itable_buf)
c4e3d3f3 206 ext2fs_free_mem(&rfs->itable_buf);
e2ca097f
TT
207 if (rfs->reserve_blocks)
208 ext2fs_free_block_bitmap(rfs->reserve_blocks);
209 if (rfs->move_blocks)
210 ext2fs_free_block_bitmap(rfs->move_blocks);
c4e3d3f3 211 ext2fs_free_mem(&rfs);
efc6f628 212
a8519a2d
TT
213 return 0;
214
215errout:
216 if (rfs->new_fs)
217 ext2fs_free(rfs->new_fs);
218 if (rfs->itable_buf)
c4e3d3f3
TT
219 ext2fs_free_mem(&rfs->itable_buf);
220 ext2fs_free_mem(&rfs);
a8519a2d
TT
221 return retval;
222}
223
86acdebd
TT
224/*
225 * Clean up the bitmaps for unitialized bitmaps
226 */
227static void fix_uninit_block_bitmaps(ext2_filsys fs)
228{
a0ba54ec 229 blk64_t blk, lblk;
86acdebd 230 dgrp_t g;
a0ba54ec 231 int i;
86acdebd
TT
232
233 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
234 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
235 return;
236
237 for (g=0; g < fs->group_desc_count; g++) {
cd65a24e 238 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
86acdebd
TT
239 continue;
240
5d3a88fb 241 blk = ext2fs_group_first_block2(fs, g);
a0ba54ec
TT
242 lblk = ext2fs_group_last_block2(fs, g);
243 ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
244 lblk - blk + 1);
245
246 ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
247 ext2fs_mark_block_bitmap2(fs->block_map,
248 ext2fs_block_bitmap_loc(fs, g));
249 ext2fs_mark_block_bitmap2(fs->block_map,
250 ext2fs_inode_bitmap_loc(fs, g));
251 for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
252 i < (unsigned int) fs->inode_blocks_per_group;
253 i++, blk++)
254 ext2fs_mark_block_bitmap2(fs->block_map, blk);
86acdebd
TT
255 }
256}
257
a8519a2d
TT
258/* --------------------------------------------------------------------
259 *
260 * Resize processing, phase 1.
261 *
262 * In this phase we adjust the in-memory superblock information, and
263 * initialize any new parts of the inode table. The new parts of the
264 * inode table are created in virgin disk space, so we can abort here
265 * without any side effects.
266 * --------------------------------------------------------------------
267 */
268
9227c5bb
TT
269/*
270 * If the group descriptor's bitmap and inode table blocks are valid,
c09043f1
TT
271 * release them in the new filesystem data structure, and mark them as
272 * reserved so the old inode table blocks don't get overwritten.
9227c5bb 273 */
c09043f1
TT
274static void free_gdp_blocks(ext2_filsys fs,
275 ext2fs_block_bitmap reserve_blocks,
0851517d
DW
276 ext2_filsys old_fs,
277 dgrp_t group)
9227c5bb 278{
0851517d 279 blk64_t blk;
9227c5bb
TT
280 int j;
281
0851517d
DW
282 blk = ext2fs_block_bitmap_loc(old_fs, group);
283 if (blk &&
284 (blk < ext2fs_blocks_count(fs->super))) {
285 ext2fs_block_alloc_stats2(fs, blk, -1);
286 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
c09043f1 287 }
9227c5bb 288
0851517d
DW
289 blk = ext2fs_inode_bitmap_loc(old_fs, group);
290 if (blk &&
291 (blk < ext2fs_blocks_count(fs->super))) {
292 ext2fs_block_alloc_stats2(fs, blk, -1);
293 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
c09043f1 294 }
9227c5bb 295
0851517d
DW
296 blk = ext2fs_inode_table_loc(old_fs, group);
297 if (blk == 0 ||
298 (blk >= ext2fs_blocks_count(fs->super)))
9227c5bb
TT
299 return;
300
0851517d 301 for (j = 0;
9227c5bb 302 j < fs->inode_blocks_per_group; j++, blk++) {
4efbac6f 303 if (blk >= ext2fs_blocks_count(fs->super))
9227c5bb 304 break;
0851517d 305 ext2fs_block_alloc_stats2(fs, blk, -1);
3346084b 306 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
9227c5bb
TT
307 }
308}
309
24b2c7a7 310/*
bf69235a
TT
311 * This routine is shared by the online and offline resize routines.
312 * All of the information which is adjusted in memory is done here.
24b2c7a7 313 */
c09043f1 314errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
8728d506 315 ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
24b2c7a7 316{
24b2c7a7 317 errcode_t retval;
8728d506
VAH
318 blk64_t overhead = 0;
319 blk64_t rem;
320 blk64_t blk, group_block;
2696f250 321 blk64_t real_end;
5e27a274
TT
322 blk64_t old_numblocks, numblocks, adjblocks;
323 unsigned long i, j, old_desc_blocks;
54434927 324 unsigned int meta_bg, meta_bg_size;
74128f8d 325 int has_super, csum_flag;
de8f3a76 326 unsigned long long new_inodes; /* u64 to check for overflow */
9ff8ece5 327 double percent;
bf69235a 328
4efbac6f 329 ext2fs_blocks_count_set(fs->super, new_size);
24b2c7a7
TT
330
331retry:
4efbac6f 332 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
69022e02
TT
333 fs->super->s_first_data_block,
334 EXT2_BLOCKS_PER_GROUP(fs->super));
24b2c7a7
TT
335 if (fs->group_desc_count == 0)
336 return EXT2_ET_TOOSMALL;
efc6f628 337 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
69022e02 338 EXT2_DESC_PER_BLOCK(fs->super));
24b2c7a7
TT
339
340 /*
341 * Overhead is the number of bookkeeping blocks per group. It
342 * includes the superblock backup, the group descriptor
343 * backups, the inode bitmap, the block bitmap, and the inode
344 * table.
24b2c7a7 345 */
9213a93b
TT
346 overhead = (int) (2 + fs->inode_blocks_per_group);
347
348 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
efc6f628 349 overhead += 1 + fs->desc_blocks +
9213a93b
TT
350 fs->super->s_reserved_gdt_blocks;
351
24b2c7a7
TT
352 /*
353 * See if the last group is big enough to support the
354 * necessary data structures. If not, we need to get rid of
355 * it.
356 */
4efbac6f 357 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
24b2c7a7
TT
358 fs->super->s_blocks_per_group;
359 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
360 return EXT2_ET_TOOSMALL;
515e555a 361 if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
4efbac6f
VAH
362 ext2fs_blocks_count_set(fs->super,
363 ext2fs_blocks_count(fs->super) - rem);
24b2c7a7
TT
364 goto retry;
365 }
366 /*
367 * Adjust the number of inodes
368 */
de8f3a76 369 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
f3358643
ES
370 if (new_inodes > ~0U) {
371 fprintf(stderr, _("inodes (%llu) must be less than %u"),
372 new_inodes, ~0U);
373 return EXT2_ET_TOO_MANY_INODES;
374 }
24b2c7a7
TT
375 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
376 fs->group_desc_count;
377
378 /*
379 * Adjust the number of free blocks
380 */
4efbac6f
VAH
381 blk = ext2fs_blocks_count(old_fs->super);
382 if (blk > ext2fs_blocks_count(fs->super))
383 ext2fs_free_blocks_count_set(fs->super,
384 ext2fs_free_blocks_count(fs->super) -
385 (blk - ext2fs_blocks_count(fs->super)));
24b2c7a7 386 else
4efbac6f
VAH
387 ext2fs_free_blocks_count_set(fs->super,
388 ext2fs_free_blocks_count(fs->super) +
389 (ext2fs_blocks_count(fs->super) - blk));
24b2c7a7 390
c762c8e6
TT
391 /*
392 * Adjust the number of reserved blocks
393 */
4efbac6f
VAH
394 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
395 ext2fs_blocks_count(old_fs->super);
396 ext2fs_r_blocks_count_set(fs->super,
397 (percent * ext2fs_blocks_count(fs->super) /
398 100.0));
c762c8e6 399
24b2c7a7
TT
400 /*
401 * Adjust the bitmaps for size
402 */
3346084b 403 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
24b2c7a7
TT
404 fs->super->s_inodes_count,
405 fs->inode_map);
c762c8e6 406 if (retval) goto errout;
efc6f628 407
2696f250
TT
408 real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) *
409 fs->group_desc_count)) - 1 +
410 fs->super->s_first_data_block;
f026f1a3
TT
411 retval = ext2fs_resize_block_bitmap2(new_size - 1,
412 real_end, fs->block_map);
c762c8e6 413 if (retval) goto errout;
24b2c7a7 414
f026f1a3
TT
415 /*
416 * If we are growing the file system, also grow the size of
417 * the reserve_blocks bitmap
418 */
419 if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) {
420 retval = ext2fs_resize_block_bitmap2(new_size - 1,
421 real_end, reserve_blocks);
422 if (retval) goto errout;
423 }
424
24b2c7a7
TT
425 /*
426 * Reallocate the group descriptors as necessary.
427 */
bf69235a
TT
428 if (old_fs->desc_blocks != fs->desc_blocks) {
429 retval = ext2fs_resize_mem(old_fs->desc_blocks *
76f875da
TT
430 fs->blocksize,
431 fs->desc_blocks * fs->blocksize,
c4e3d3f3 432 &fs->group_desc);
ca8abba7 433 if (retval)
a8519a2d 434 goto errout;
efc6f628
TT
435 if (fs->desc_blocks > old_fs->desc_blocks)
436 memset((char *) fs->group_desc +
bf69235a
TT
437 (old_fs->desc_blocks * fs->blocksize), 0,
438 (fs->desc_blocks - old_fs->desc_blocks) *
2787276e 439 fs->blocksize);
24b2c7a7 440 }
1e1da29f 441
9213a93b
TT
442 /*
443 * If the resize_inode feature is set, and we are changing the
444 * number of descriptor blocks, then adjust
445 * s_reserved_gdt_blocks if possible to avoid needing to move
446 * the inode table either now or in the future.
447 */
efc6f628 448 if ((fs->super->s_feature_compat &
9213a93b 449 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
bf69235a 450 (old_fs->desc_blocks != fs->desc_blocks)) {
9213a93b
TT
451 int new;
452
efc6f628 453 new = ((int) fs->super->s_reserved_gdt_blocks) +
bf69235a 454 (old_fs->desc_blocks - fs->desc_blocks);
9213a93b
TT
455 if (new < 0)
456 new = 0;
de8f3a76 457 if (new > (int) fs->blocksize/4)
9213a93b
TT
458 new = fs->blocksize/4;
459 fs->super->s_reserved_gdt_blocks = new;
9213a93b
TT
460 }
461
a8519a2d 462 /*
c09043f1
TT
463 * If we are shrinking the number of block groups, we're done
464 * and can exit now.
1e1da29f 465 */
bf69235a 466 if (old_fs->group_desc_count > fs->group_desc_count) {
9227c5bb
TT
467 /*
468 * Check the block groups that we are chopping off
469 * and free any blocks associated with their metadata
470 */
471 for (i = fs->group_desc_count;
0851517d
DW
472 i < old_fs->group_desc_count; i++)
473 free_gdp_blocks(fs, reserve_blocks, old_fs, i);
c762c8e6
TT
474 retval = 0;
475 goto errout;
476 }
bf69235a 477
a8519a2d
TT
478 /*
479 * Fix the count of the last (old) block group
480 */
4efbac6f 481 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
bf69235a
TT
482 old_fs->super->s_first_data_block) %
483 old_fs->super->s_blocks_per_group;
1e1da29f 484 if (!old_numblocks)
bf69235a
TT
485 old_numblocks = old_fs->super->s_blocks_per_group;
486 if (old_fs->group_desc_count == fs->group_desc_count) {
4efbac6f 487 numblocks = (ext2fs_blocks_count(fs->super) -
bf69235a
TT
488 fs->super->s_first_data_block) %
489 fs->super->s_blocks_per_group;
1e1da29f 490 if (!numblocks)
bf69235a 491 numblocks = fs->super->s_blocks_per_group;
1e1da29f 492 } else
bf69235a
TT
493 numblocks = fs->super->s_blocks_per_group;
494 i = old_fs->group_desc_count - 1;
d7cca6b0 495 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
236efede
JS
496 ext2fs_group_desc_csum_set(fs, i);
497
1e1da29f 498 /*
a8519a2d
TT
499 * If the number of block groups is staying the same, we're
500 * done and can exit now. (If the number block groups is
501 * shrinking, we had exited earlier.)
1e1da29f 502 */
bf69235a 503 if (old_fs->group_desc_count >= fs->group_desc_count) {
c762c8e6
TT
504 retval = 0;
505 goto errout;
506 }
bf69235a 507
a8519a2d
TT
508 /*
509 * Initialize the new block group descriptors
510 */
027b0577
TT
511 group_block = ext2fs_group_first_block2(fs,
512 old_fs->group_desc_count);
74128f8d
TT
513 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
514 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
2d8c0c8a
TT
515 if (access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0)
516 lazy_itable_init = 1;
76dd5e5c
TT
517 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
518 old_desc_blocks = fs->super->s_first_meta_bg;
519 else
efc6f628 520 old_desc_blocks = fs->desc_blocks +
9213a93b 521 fs->super->s_reserved_gdt_blocks;
f026f1a3
TT
522
523 /*
524 * If we changed the number of block_group descriptor blocks,
525 * we need to make sure they are all marked as reserved in the
526 * file systems's block allocation map.
527 */
528 for (i = 0; i < old_fs->group_desc_count; i++)
529 ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
530
bf69235a 531 for (i = old_fs->group_desc_count;
1e1da29f 532 i < fs->group_desc_count; i++) {
d7cca6b0 533 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
1e1da29f
TT
534 sizeof(struct ext2_group_desc));
535 adjblocks = 0;
536
e633b58a 537 ext2fs_bg_flags_zap(fs, i);
2d8c0c8a
TT
538 if (csum_flag) {
539 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
540 if (!lazy_itable_init)
541 ext2fs_bg_flags_set(fs, i,
542 EXT2_BG_INODE_ZEROED);
543 ext2fs_bg_itable_unused_set(fs, i,
544 fs->super->s_inodes_per_group);
545 }
98f45471
ES
546
547 numblocks = ext2fs_group_blocks_count(fs, i);
548 if ((i < fs->group_desc_count - 1) && csum_flag)
549 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
1e1da29f 550
76dd5e5c
TT
551 has_super = ext2fs_bg_has_super(fs, i);
552 if (has_super) {
48f23054 553 ext2fs_block_alloc_stats2(fs, group_block, +1);
76dd5e5c
TT
554 adjblocks++;
555 }
f2de1d38 556 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
76dd5e5c
TT
557 meta_bg = i / meta_bg_size;
558 if (!(fs->super->s_feature_incompat &
559 EXT2_FEATURE_INCOMPAT_META_BG) ||
560 (meta_bg < fs->super->s_first_meta_bg)) {
561 if (has_super) {
562 for (j=0; j < old_desc_blocks; j++)
48f23054 563 ext2fs_block_alloc_stats2(fs,
74128f8d 564 group_block + 1 + j, +1);
76dd5e5c
TT
565 adjblocks += old_desc_blocks;
566 }
567 } else {
568 if (has_super)
569 has_super = 1;
570 if (((i % meta_bg_size) == 0) ||
571 ((i % meta_bg_size) == 1) ||
572 ((i % meta_bg_size) == (meta_bg_size-1)))
48f23054 573 ext2fs_block_alloc_stats2(fs,
74128f8d 574 group_block + has_super, +1);
24b2c7a7 575 }
efc6f628 576
1e1da29f 577 adjblocks += 2 + fs->inode_blocks_per_group;
efc6f628 578
1e1da29f 579 numblocks -= adjblocks;
4efbac6f
VAH
580 ext2fs_free_blocks_count_set(fs->super,
581 ext2fs_free_blocks_count(fs->super) - adjblocks);
1e1da29f
TT
582 fs->super->s_free_inodes_count +=
583 fs->super->s_inodes_per_group;
d7cca6b0
VAH
584 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
585 ext2fs_bg_free_inodes_count_set(fs, i,
586 fs->super->s_inodes_per_group);
587 ext2fs_bg_used_dirs_count_set(fs, i, 0);
236efede 588 ext2fs_group_desc_csum_set(fs, i);
24b2c7a7 589
1e1da29f 590 retval = ext2fs_allocate_group_table(fs, i, 0);
c762c8e6 591 if (retval) goto errout;
24b2c7a7 592
bf69235a
TT
593 group_block += fs->super->s_blocks_per_group;
594 }
595 retval = 0;
596
f026f1a3
TT
597 /*
598 * Mark all of the metadata blocks as reserved so they won't
599 * get allocated by the call to ext2fs_allocate_group_table()
600 * in blocks_to_move(), where we allocate new blocks to
601 * replace those allocation bitmap and inode table blocks
602 * which have to get relocated to make space for an increased
603 * number of the block group descriptors.
604 */
605 if (reserve_blocks)
606 mark_table_blocks(fs, reserve_blocks);
607
bf69235a
TT
608errout:
609 return (retval);
610}
611
612/*
613 * This routine adjusts the superblock and other data structures, both
614 * in disk as well as in memory...
615 */
8728d506 616static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
bf69235a
TT
617{
618 ext2_filsys fs;
619 int adj = 0;
620 errcode_t retval;
8728d506 621 blk64_t group_block;
bf69235a
TT
622 unsigned long i;
623 unsigned long max_group;
efc6f628 624
bf69235a
TT
625 fs = rfs->new_fs;
626 ext2fs_mark_super_dirty(fs);
627 ext2fs_mark_bb_dirty(fs);
628 ext2fs_mark_ib_dirty(fs);
629
c09043f1
TT
630 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
631 &rfs->reserve_blocks);
632 if (retval)
633 return retval;
634
635 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
bf69235a
TT
636 if (retval)
637 goto errout;
638
639 /*
640 * Check to make sure there are enough inodes
641 */
642 if ((rfs->old_fs->super->s_inodes_count -
643 rfs->old_fs->super->s_free_inodes_count) >
644 rfs->new_fs->super->s_inodes_count) {
645 retval = ENOSPC;
646 goto errout;
647 }
648
649 /*
650 * If we are shrinking the number block groups, we're done and
651 * can exit now.
652 */
653 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
654 retval = 0;
655 goto errout;
656 }
657
658 /*
659 * If the number of block groups is staying the same, we're
660 * done and can exit now. (If the number block groups is
661 * shrinking, we had exited earlier.)
662 */
663 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
664 retval = 0;
665 goto errout;
666 }
667
668 /*
2d8c0c8a
TT
669 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
670 * supports lazy inode initialization, we can skip
671 * initializing the inode table.
672 */
673 if (lazy_itable_init &&
674 EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
675 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
676 retval = 0;
677 goto errout;
678 }
679
680 /*
681 * Initialize the inode table
bf69235a 682 */
e5aace90 683 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
bf69235a
TT
684 &rfs->itable_buf);
685 if (retval)
686 goto errout;
687
688 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
027b0577
TT
689 group_block = ext2fs_group_first_block2(fs,
690 rfs->old_fs->group_desc_count);
bf69235a
TT
691 adj = rfs->old_fs->group_desc_count;
692 max_group = fs->group_desc_count - adj;
693 if (rfs->progress) {
694 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
695 0, max_group);
696 if (retval)
697 goto errout;
698 }
699 for (i = rfs->old_fs->group_desc_count;
700 i < fs->group_desc_count; i++) {
05e112a1
TT
701 /*
702 * Write out the new inode table
703 */
24a117ab 704 retval = io_channel_write_blk64(fs->io,
d7cca6b0 705 ext2fs_inode_table_loc(fs, i),
24a117ab
VAH
706 fs->inode_blocks_per_group,
707 rfs->itable_buf);
c762c8e6
TT
708 if (retval) goto errout;
709
a8519a2d 710 io_channel_flush(fs->io);
3b627e8d
TT
711 if (rfs->progress) {
712 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
713 i - adj + 1, max_group);
714 if (retval)
715 goto errout;
716 }
1e1da29f 717 group_block += fs->super->s_blocks_per_group;
24b2c7a7 718 }
c762c8e6
TT
719 io_channel_flush(fs->io);
720 retval = 0;
721
722errout:
c762c8e6
TT
723 return retval;
724}
725
a8519a2d
TT
726/* --------------------------------------------------------------------
727 *
728 * Resize processing, phase 2.
729 *
730 * In this phase we adjust determine which blocks need to be moved, in
731 * blocks_to_move(). We then copy the blocks to their ultimate new
732 * destinations using block_mover(). Since we are copying blocks to
733 * their new locations, again during this pass we can abort without
734 * any problems.
735 * --------------------------------------------------------------------
736 */
737
c762c8e6
TT
738/*
739 * This helper function creates a block bitmap with all of the
740 * filesystem meta-data blocks.
741 */
742static errcode_t mark_table_blocks(ext2_filsys fs,
64ad98ac 743 ext2fs_block_bitmap bmap)
c762c8e6 744{
54434927 745 dgrp_t i;
f026f1a3 746 blk64_t blk;
c762c8e6 747
c762c8e6 748 for (i = 0; i < fs->group_desc_count; i++) {
64ad98ac 749 ext2fs_reserve_super_and_bgd(fs, i, bmap);
efc6f628 750
c762c8e6
TT
751 /*
752 * Mark the blocks used for the inode table
753 */
f026f1a3
TT
754 blk = ext2fs_inode_table_loc(fs, i);
755 if (blk)
756 ext2fs_mark_block_bitmap_range2(bmap, blk,
757 fs->inode_blocks_per_group);
efc6f628 758
c762c8e6 759 /*
efc6f628 760 * Mark block used for the block bitmap
c762c8e6 761 */
f026f1a3
TT
762 blk = ext2fs_block_bitmap_loc(fs, i);
763 if (blk)
764 ext2fs_mark_block_bitmap2(bmap, blk);
64ad98ac 765
c762c8e6 766 /*
efc6f628 767 * Mark block used for the inode bitmap
c762c8e6 768 */
f026f1a3
TT
769 blk = ext2fs_inode_bitmap_loc(fs, i);
770 if (blk)
771 ext2fs_mark_block_bitmap2(bmap, blk);
c762c8e6 772 }
1e1da29f 773 return 0;
24b2c7a7
TT
774}
775
76dd5e5c
TT
776/*
777 * This function checks to see if a particular block (either a
778 * superblock or a block group descriptor) overlaps with an inode or
779 * block bitmap block, or with the inode table.
780 */
781static void mark_fs_metablock(ext2_resize_t rfs,
782 ext2fs_block_bitmap meta_bmap,
8728d506 783 int group, blk64_t blk)
76dd5e5c
TT
784{
785 ext2_filsys fs = rfs->new_fs;
efc6f628 786
3346084b 787 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
48f23054 788 ext2fs_block_alloc_stats2(fs, blk, +1);
76dd5e5c
TT
789
790 /*
791 * Check to see if we overlap with the inode or block bitmap,
792 * or the inode tables. If not, and the block is in use, then
793 * mark it as a block to be moved.
794 */
795 if (IS_BLOCK_BM(fs, group, blk)) {
d7cca6b0 796 ext2fs_block_bitmap_loc_set(fs, group, 0);
76dd5e5c 797 rfs->needed_blocks++;
ddcf1dbf
TT
798 return;
799 }
800 if (IS_INODE_BM(fs, group, blk)) {
d7cca6b0 801 ext2fs_inode_bitmap_loc_set(fs, group, 0);
76dd5e5c 802 rfs->needed_blocks++;
ddcf1dbf
TT
803 return;
804 }
805 if (IS_INODE_TB(fs, group, blk)) {
d7cca6b0 806 ext2fs_inode_table_loc_set(fs, group, 0);
76dd5e5c 807 rfs->needed_blocks++;
ddcf1dbf
TT
808 return;
809 }
810 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
811 dgrp_t i;
812
813 for (i=0; i < rfs->old_fs->group_desc_count; i++) {
814 if (IS_BLOCK_BM(fs, i, blk)) {
815 ext2fs_block_bitmap_loc_set(fs, i, 0);
816 rfs->needed_blocks++;
817 return;
818 }
819 if (IS_INODE_BM(fs, i, blk)) {
820 ext2fs_inode_bitmap_loc_set(fs, i, 0);
821 rfs->needed_blocks++;
822 return;
823 }
824 if (IS_INODE_TB(fs, i, blk)) {
825 ext2fs_inode_table_loc_set(fs, i, 0);
826 rfs->needed_blocks++;
827 return;
828 }
829 }
830 }
831 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
832 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
cd65a24e 833 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
86acdebd
TT
834 /*
835 * If the block bitmap is uninitialized, which means
836 * nothing other than standard metadata in use.
837 */
838 return;
3346084b
VAH
839 } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
840 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
841 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
76dd5e5c
TT
842 rfs->needed_blocks++;
843 }
844}
845
846
24b2c7a7
TT
847/*
848 * This routine marks and unmarks reserved blocks in the new block
849 * bitmap. It also determines which blocks need to be moved and
850 * places this information into the move_blocks bitmap.
851 */
c762c8e6 852static errcode_t blocks_to_move(ext2_resize_t rfs)
24b2c7a7 853{
54434927 854 int j, has_super;
86acdebd 855 dgrp_t i, max_groups, g;
8728d506
VAH
856 blk64_t blk, group_blk;
857 blk64_t old_blocks, new_blocks;
80391dcd 858 blk64_t new_size;
54434927 859 unsigned int meta_bg, meta_bg_size;
24b2c7a7 860 errcode_t retval;
c762c8e6
TT
861 ext2_filsys fs, old_fs;
862 ext2fs_block_bitmap meta_bmap;
4b04fb30 863 int flex_bg;
24b2c7a7 864
c762c8e6
TT
865 fs = rfs->new_fs;
866 old_fs = rfs->old_fs;
4efbac6f 867 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
c762c8e6 868 fs = rfs->old_fs;
efc6f628 869
a13575f4 870 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
c762c8e6
TT
871 &rfs->move_blocks);
872 if (retval)
873 return retval;
874
efc6f628 875 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
64ad98ac
TT
876 &meta_bmap);
877 if (retval)
878 return retval;
efc6f628 879
64ad98ac 880 retval = mark_table_blocks(old_fs, meta_bmap);
c762c8e6
TT
881 if (retval)
882 return retval;
883
884 fs = rfs->new_fs;
efc6f628 885
80391dcd 886 /*
e9736a3b
TT
887 * If we're shrinking the filesystem, we need to move any
888 * group's metadata blocks (either allocation bitmaps or the
889 * inode table) which are beyond the end of the new
890 * filesystem.
80391dcd
ES
891 */
892 new_size = ext2fs_blocks_count(fs->super);
893 if (new_size < ext2fs_blocks_count(old_fs->super)) {
894 for (g = 0; g < fs->group_desc_count; g++) {
e9736a3b 895 int realloc = 0;
80391dcd 896 /*
e9736a3b
TT
897 * ext2fs_allocate_group_table will re-allocate any
898 * metadata blocks whose location is set to zero.
80391dcd
ES
899 */
900 if (ext2fs_block_bitmap_loc(fs, g) >= new_size) {
901 ext2fs_block_bitmap_loc_set(fs, g, 0);
e9736a3b 902 realloc = 1;
80391dcd
ES
903 }
904 if (ext2fs_inode_bitmap_loc(fs, g) >= new_size) {
905 ext2fs_inode_bitmap_loc_set(fs, g, 0);
e9736a3b
TT
906 realloc = 1;
907 }
908 if ((ext2fs_inode_table_loc(fs, g) +
909 fs->inode_blocks_per_group) > new_size) {
910 ext2fs_inode_table_loc_set(fs, g, 0);
911 realloc = 1;
912 }
913
914 if (realloc) {
80391dcd
ES
915 retval = ext2fs_allocate_group_table(fs, g, 0);
916 if (retval)
917 return retval;
918 }
919 }
920 }
921
1e1da29f
TT
922 /*
923 * If we're shrinking the filesystem, we need to move all of
924 * the blocks that don't fit any more
925 */
4efbac6f
VAH
926 for (blk = ext2fs_blocks_count(fs->super);
927 blk < ext2fs_blocks_count(old_fs->super); blk++) {
6493f8e8 928 g = ext2fs_group_of_blk2(fs, blk);
86acdebd
TT
929 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
930 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
cd65a24e 931 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
86acdebd
TT
932 /*
933 * The block bitmap is uninitialized, so skip
934 * to the next block group.
935 */
5d3a88fb 936 blk = ext2fs_group_first_block2(fs, g+1) - 1;
86acdebd
TT
937 continue;
938 }
3346084b
VAH
939 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
940 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
941 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
1e1da29f 942 rfs->needed_blocks++;
c762c8e6 943 }
3346084b 944 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1e1da29f 945 }
efc6f628 946
76dd5e5c
TT
947 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
948 old_blocks = old_fs->super->s_first_meta_bg;
949 new_blocks = fs->super->s_first_meta_bg;
950 } else {
9213a93b
TT
951 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
952 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
76dd5e5c 953 }
efc6f628 954
c762c8e6
TT
955 if (old_blocks == new_blocks) {
956 retval = 0;
957 goto errout;
958 }
24b2c7a7 959
1333fe93
TT
960 max_groups = fs->group_desc_count;
961 if (max_groups > old_fs->group_desc_count)
962 max_groups = old_fs->group_desc_count;
c762c8e6 963 group_blk = old_fs->super->s_first_data_block;
24b2c7a7
TT
964 /*
965 * If we're reducing the number of descriptor blocks, this
966 * makes life easy. :-) We just have to mark some extra
967 * blocks as free.
968 */
969 if (old_blocks > new_blocks) {
1333fe93 970 for (i = 0; i < max_groups; i++) {
1e1da29f
TT
971 if (!ext2fs_bg_has_super(fs, i)) {
972 group_blk += fs->super->s_blocks_per_group;
24b2c7a7
TT
973 continue;
974 }
c762c8e6
TT
975 for (blk = group_blk+1+new_blocks;
976 blk < group_blk+1+old_blocks; blk++) {
48f23054 977 ext2fs_block_alloc_stats2(fs, blk, -1);
052db4b7
TT
978 rfs->needed_blocks--;
979 }
1e1da29f 980 group_blk += fs->super->s_blocks_per_group;
24b2c7a7 981 }
c762c8e6
TT
982 retval = 0;
983 goto errout;
24b2c7a7
TT
984 }
985 /*
986 * If we're increasing the number of descriptor blocks, life
efc6f628 987 * gets interesting....
24b2c7a7 988 */
f2de1d38 989 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
4b04fb30
TT
990 flex_bg = fs->super->s_feature_incompat &
991 EXT4_FEATURE_INCOMPAT_FLEX_BG;
2ebaeb35 992 /* first reserve all of the existing fs meta blocks */
1333fe93 993 for (i = 0; i < max_groups; i++) {
76dd5e5c
TT
994 has_super = ext2fs_bg_has_super(fs, i);
995 if (has_super)
996 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
997
998 meta_bg = i / meta_bg_size;
999 if (!(fs->super->s_feature_incompat &
1000 EXT2_FEATURE_INCOMPAT_META_BG) ||
1001 (meta_bg < fs->super->s_first_meta_bg)) {
424cb7b6
TT
1002 if (has_super) {
1003 for (blk = group_blk+1;
1004 blk < group_blk + 1 + new_blocks; blk++)
efc6f628 1005 mark_fs_metablock(rfs, meta_bmap,
424cb7b6
TT
1006 i, blk);
1007 }
76dd5e5c
TT
1008 } else {
1009 if (has_super)
1010 has_super = 1;
1011 if (((i % meta_bg_size) == 0) ||
1012 ((i % meta_bg_size) == 1) ||
1013 ((i % meta_bg_size) == (meta_bg_size-1)))
1014 mark_fs_metablock(rfs, meta_bmap, i,
1015 group_blk + has_super);
24b2c7a7 1016 }
76dd5e5c 1017
1e1da29f 1018 /*
c762c8e6
TT
1019 * Reserve the existing meta blocks that we know
1020 * aren't to be moved.
4b04fb30
TT
1021 *
1022 * For flex_bg file systems, in order to avoid
1023 * overwriting fs metadata (especially inode table
1024 * blocks) belonging to a different block group when
1025 * we are relocating the inode tables, we need to
1026 * reserve all existing fs metadata blocks.
1e1da29f 1027 */
d7cca6b0 1028 if (ext2fs_block_bitmap_loc(fs, i))
3346084b 1029 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
d7cca6b0 1030 ext2fs_block_bitmap_loc(fs, i));
4b04fb30
TT
1031 else if (flex_bg && i < old_fs->group_desc_count)
1032 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1033 ext2fs_block_bitmap_loc(old_fs, i));
1034
d7cca6b0 1035 if (ext2fs_inode_bitmap_loc(fs, i))
3346084b 1036 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
d7cca6b0 1037 ext2fs_inode_bitmap_loc(fs, i));
4b04fb30
TT
1038 else if (flex_bg && i < old_fs->group_desc_count)
1039 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1040 ext2fs_inode_bitmap_loc(old_fs, i));
1041
d7cca6b0 1042 if (ext2fs_inode_table_loc(fs, i))
a0ba54ec
TT
1043 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1044 ext2fs_inode_table_loc(fs, i),
1045 fs->inode_blocks_per_group);
4b04fb30 1046 else if (flex_bg && i < old_fs->group_desc_count)
a0ba54ec
TT
1047 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1048 ext2fs_inode_table_loc(old_fs, i),
1049 old_fs->inode_blocks_per_group);
24b2c7a7 1050
2ebaeb35
TT
1051 group_blk += rfs->new_fs->super->s_blocks_per_group;
1052 }
1053
1054 /* Allocate the missing data structures */
1055 for (i = 0; i < max_groups; i++) {
1056 if (ext2fs_inode_table_loc(fs, i) &&
1057 ext2fs_inode_bitmap_loc(fs, i) &&
1058 ext2fs_block_bitmap_loc(fs, i))
1059 continue;
1060
1e1da29f
TT
1061 retval = ext2fs_allocate_group_table(fs, i,
1062 rfs->reserve_blocks);
1063 if (retval)
c762c8e6 1064 goto errout;
24b2c7a7 1065
1e1da29f 1066 /*
c762c8e6
TT
1067 * For those structures that have changed, we need to
1068 * do bookkeepping.
1e1da29f 1069 */
d7cca6b0
VAH
1070 if (ext2fs_block_bitmap_loc(old_fs, i) !=
1071 (blk = ext2fs_block_bitmap_loc(fs, i))) {
48f23054 1072 ext2fs_block_alloc_stats2(fs, blk, +1);
3346084b
VAH
1073 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1074 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1075 ext2fs_mark_block_bitmap2(rfs->move_blocks,
c762c8e6
TT
1076 blk);
1077 }
d7cca6b0
VAH
1078 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
1079 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
48f23054 1080 ext2fs_block_alloc_stats2(fs, blk, +1);
3346084b
VAH
1081 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1082 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1083 ext2fs_mark_block_bitmap2(rfs->move_blocks,
c762c8e6
TT
1084 blk);
1085 }
24b2c7a7 1086
052db4b7
TT
1087 /*
1088 * The inode table, if we need to relocate it, is
1089 * handled specially. We have to reserve the blocks
1090 * for both the old and the new inode table, since we
1091 * can't have the inode table be destroyed during the
1092 * block relocation phase.
1093 */
d7cca6b0 1094 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
2ebaeb35 1095 continue; /* inode table not moved */
052db4b7 1096
c762c8e6 1097 rfs->needed_blocks += fs->inode_blocks_per_group;
052db4b7
TT
1098
1099 /*
1100 * Mark the new inode table as in use in the new block
efc6f628 1101 * allocation bitmap, and move any blocks that might
c762c8e6 1102 * be necessary.
052db4b7 1103 */
d7cca6b0 1104 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
c762c8e6 1105 j < fs->inode_blocks_per_group ; j++, blk++) {
48f23054 1106 ext2fs_block_alloc_stats2(fs, blk, +1);
3346084b
VAH
1107 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1108 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1109 ext2fs_mark_block_bitmap2(rfs->move_blocks,
c762c8e6
TT
1110 blk);
1111 }
efc6f628 1112
1e1da29f 1113 /*
052db4b7
TT
1114 * Make sure the old inode table is reserved in the
1115 * block reservation bitmap.
1e1da29f 1116 */
d7cca6b0 1117 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
052db4b7 1118 j < fs->inode_blocks_per_group ; j++, blk++)
3346084b 1119 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1e1da29f 1120 }
c762c8e6
TT
1121 retval = 0;
1122
1123errout:
1124 if (meta_bmap)
1125 ext2fs_free_block_bitmap(meta_bmap);
efc6f628 1126
c762c8e6 1127 return retval;
1e1da29f 1128}
24b2c7a7 1129
a8519a2d
TT
1130/*
1131 * This helper function tries to allocate a new block. We try to
1132 * avoid hitting the original group descriptor blocks at least at
1133 * first, since we want to make it possible to recover from a badly
1134 * aborted resize operation as much as possible.
1135 *
1136 * In the future, I may further modify this routine to balance out
1137 * where we get the new blocks across the various block groups.
1138 * Ideally we would allocate blocks that corresponded with the block
1139 * group of the containing inode, and keep contiguous blocks
1140 * together. However, this very difficult to do efficiently, since we
1141 * don't have the necessary information up front.
1142 */
1143
1144#define AVOID_OLD 1
1145#define DESPERATION 2
1146
1147static void init_block_alloc(ext2_resize_t rfs)
1148{
1149 rfs->alloc_state = AVOID_OLD;
1150 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
2bc4d4f7
TT
1151#if 0
1152 /* HACK for testing */
4efbac6f
VAH
1153 if (ext2fs_blocks_count(rfs->new_fs->super) >
1154 ext2fs_blocks_count(rfs->old_fs->super))
1155 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
2bc4d4f7 1156#endif
a8519a2d
TT
1157}
1158
8728d506 1159static blk64_t get_new_block(ext2_resize_t rfs)
a8519a2d
TT
1160{
1161 ext2_filsys fs = rfs->new_fs;
efc6f628 1162
a8519a2d 1163 while (1) {
4efbac6f 1164 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
a8519a2d
TT
1165 if (rfs->alloc_state == DESPERATION)
1166 return 0;
1167
1168#ifdef RESIZE2FS_DEBUG
1169 if (rfs->flags & RESIZE_DEBUG_BMOVE)
f35fd3d5
TT
1170 printf("Going into desperation mode "
1171 "for block allocations\n");
efc6f628 1172#endif
a8519a2d
TT
1173 rfs->alloc_state = DESPERATION;
1174 rfs->new_blk = fs->super->s_first_data_block;
1175 continue;
1176 }
3346084b
VAH
1177 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1178 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
a8519a2d
TT
1179 rfs->new_blk) ||
1180 ((rfs->alloc_state == AVOID_OLD) &&
4efbac6f 1181 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
3346084b 1182 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
a8519a2d
TT
1183 rfs->new_blk))) {
1184 rfs->new_blk++;
1185 continue;
1186 }
1187 return rfs->new_blk;
1188 }
1189}
1190
7f9c96ee
TT
1191static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1192 blk64_t *ret)
1193{
1194 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
8728d506 1195 blk64_t blk;
7f9c96ee
TT
1196
1197 blk = get_new_block(rfs);
1198 if (!blk)
1199 return ENOSPC;
1200
1201#ifdef RESIZE2FS_DEBUG
1202 if (rfs->flags & 0xF)
8728d506 1203 printf("get_alloc_block allocating %llu\n", blk);
7f9c96ee
TT
1204#endif
1205
3346084b
VAH
1206 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1207 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
7f9c96ee
TT
1208 *ret = (blk64_t) blk;
1209 return 0;
1210}
1211
a8519a2d
TT
1212static errcode_t block_mover(ext2_resize_t rfs)
1213{
8728d506 1214 blk64_t blk, old_blk, new_blk;
a8519a2d
TT
1215 ext2_filsys fs = rfs->new_fs;
1216 ext2_filsys old_fs = rfs->old_fs;
1217 errcode_t retval;
8728d506
VAH
1218 __u64 size;
1219 int c;
a8519a2d 1220 int to_move, moved;
7d7bdd57
TT
1221 ext2_badblocks_list badblock_list = 0;
1222 int bb_modified = 0;
efc6f628 1223
7f9c96ee
TT
1224 fs->get_alloc_block = resize2fs_get_alloc_block;
1225 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1226
7d7bdd57
TT
1227 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1228 if (retval)
1229 return retval;
a8519a2d
TT
1230
1231 new_blk = fs->super->s_first_data_block;
1232 if (!rfs->itable_buf) {
e5aace90 1233 retval = ext2fs_get_array(fs->blocksize,
a8519a2d 1234 fs->inode_blocks_per_group,
c4e3d3f3 1235 &rfs->itable_buf);
a8519a2d
TT
1236 if (retval)
1237 return retval;
1238 }
1239 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1240 if (retval)
1241 return retval;
1242
1243 /*
1244 * The first step is to figure out where all of the blocks
1245 * will go.
1246 */
1247 to_move = moved = 0;
1248 init_block_alloc(rfs);
d1a1a583
TT
1249 for (blk = B2C(old_fs->super->s_first_data_block);
1250 blk < ext2fs_blocks_count(old_fs->super);
1251 blk += EXT2FS_CLUSTER_RATIO(fs)) {
3346084b 1252 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
a8519a2d 1253 continue;
3346084b 1254 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
a8519a2d 1255 continue;
7d7bdd57
TT
1256 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1257 ext2fs_badblocks_list_del(badblock_list, blk);
1258 bb_modified++;
1259 continue;
1260 }
a8519a2d
TT
1261
1262 new_blk = get_new_block(rfs);
1263 if (!new_blk) {
1264 retval = ENOSPC;
1265 goto errout;
1266 }
48f23054 1267 ext2fs_block_alloc_stats2(fs, new_blk, +1);
d1a1a583 1268 ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk));
a8519a2d
TT
1269 to_move++;
1270 }
efc6f628 1271
a8519a2d 1272 if (to_move == 0) {
cefbf487
TT
1273 if (rfs->bmap) {
1274 ext2fs_free_extent_table(rfs->bmap);
1275 rfs->bmap = 0;
1276 }
a8519a2d
TT
1277 retval = 0;
1278 goto errout;
1279 }
1280
1281 /*
1282 * Step two is to actually move the blocks
1283 */
1284 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1285 if (retval) goto errout;
1286
3b627e8d
TT
1287 if (rfs->progress) {
1288 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1289 0, to_move);
1290 if (retval)
1291 goto errout;
1292 }
a8519a2d
TT
1293 while (1) {
1294 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1295 if (retval) goto errout;
1296 if (!size)
1297 break;
d1a1a583
TT
1298 old_blk = C2B(old_blk);
1299 new_blk = C2B(new_blk);
1300 size = C2B(size);
a8519a2d
TT
1301#ifdef RESIZE2FS_DEBUG
1302 if (rfs->flags & RESIZE_DEBUG_BMOVE)
8728d506 1303 printf("Moving %llu blocks %llu->%llu\n",
f35fd3d5 1304 size, old_blk, new_blk);
a8519a2d
TT
1305#endif
1306 do {
1307 c = size;
1308 if (c > fs->inode_blocks_per_group)
1309 c = fs->inode_blocks_per_group;
24a117ab
VAH
1310 retval = io_channel_read_blk64(fs->io, old_blk, c,
1311 rfs->itable_buf);
a8519a2d 1312 if (retval) goto errout;
24a117ab
VAH
1313 retval = io_channel_write_blk64(fs->io, new_blk, c,
1314 rfs->itable_buf);
a8519a2d
TT
1315 if (retval) goto errout;
1316 size -= c;
1317 new_blk += c;
1318 old_blk += c;
1319 moved += c;
1320 if (rfs->progress) {
1321 io_channel_flush(fs->io);
3b627e8d
TT
1322 retval = (rfs->progress)(rfs,
1323 E2_RSZ_BLOCK_RELOC_PASS,
a8519a2d 1324 moved, to_move);
3b627e8d
TT
1325 if (retval)
1326 goto errout;
a8519a2d
TT
1327 }
1328 } while (size > 0);
1329 io_channel_flush(fs->io);
1330 }
1331
1332errout:
7d7bdd57
TT
1333 if (badblock_list) {
1334 if (!retval && bb_modified)
1335 retval = ext2fs_update_bb_inode(old_fs,
1336 badblock_list);
1337 ext2fs_badblocks_list_free(badblock_list);
1338 }
a8519a2d
TT
1339 return retval;
1340}
1341
1342
1343/* --------------------------------------------------------------------
1344 *
1345 * Resize processing, phase 3
1346 *
1347 * --------------------------------------------------------------------
1348 */
1349
1350
d1a1a583
TT
1351/*
1352 * The extent translation table is stored in clusters so we need to
1353 * take special care when mapping a source block number to its
1354 * destination block number.
1355 */
f404167d 1356static __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc)
d1a1a583
TT
1357{
1358 __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc)));
1359
1360 if (new_block != 0)
1361 new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1);
1362 return new_block;
1363}
1364
a8519a2d
TT
1365struct process_block_struct {
1366 ext2_resize_t rfs;
dfcdc32f 1367 ext2_ino_t ino;
a8519a2d
TT
1368 struct ext2_inode * inode;
1369 errcode_t error;
1370 int is_dir;
1371 int changed;
1372};
1373
8728d506 1374static int process_block(ext2_filsys fs, blk64_t *block_nr,
efc6f628 1375 e2_blkcnt_t blockcnt,
8728d506 1376 blk64_t ref_block EXT2FS_ATTR((unused)),
54434927 1377 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
a8519a2d
TT
1378{
1379 struct process_block_struct *pb;
1380 errcode_t retval;
8728d506 1381 blk64_t block, new_block;
a8519a2d
TT
1382 int ret = 0;
1383
1384 pb = (struct process_block_struct *) priv_data;
1385 block = *block_nr;
1386 if (pb->rfs->bmap) {
d1a1a583 1387 new_block = extent_translate(fs, pb->rfs->bmap, block);
a8519a2d
TT
1388 if (new_block) {
1389 *block_nr = new_block;
1390 ret |= BLOCK_CHANGED;
1391 pb->changed = 1;
1392#ifdef RESIZE2FS_DEBUG
1393 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
8728d506 1394 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
a8519a2d
TT
1395 pb->ino, blockcnt, block, new_block);
1396#endif
1397 block = new_block;
1398 }
1399 }
1400 if (pb->is_dir) {
8728d506
VAH
1401 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1402 block, (int) blockcnt);
a8519a2d
TT
1403 if (retval) {
1404 pb->error = retval;
1405 ret |= BLOCK_ABORT;
1406 }
1407 }
1408 return ret;
1409}
1410
1411/*
1412 * Progress callback
1413 */
efc6f628 1414static errcode_t progress_callback(ext2_filsys fs,
54434927 1415 ext2_inode_scan scan EXT2FS_ATTR((unused)),
a8519a2d
TT
1416 dgrp_t group, void * priv_data)
1417{
1418 ext2_resize_t rfs = (ext2_resize_t) priv_data;
3b627e8d 1419 errcode_t retval;
a8519a2d
TT
1420
1421 /*
f4b2a6db
TT
1422 * This check is to protect against old ext2 libraries. It
1423 * shouldn't be needed against new libraries.
a8519a2d 1424 */
f4b2a6db 1425 if ((group+1) == 0)
a8519a2d
TT
1426 return 0;
1427
1428 if (rfs->progress) {
1429 io_channel_flush(fs->io);
3b627e8d
TT
1430 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1431 group+1, fs->group_desc_count);
1432 if (retval)
1433 return retval;
a8519a2d 1434 }
efc6f628 1435
a8519a2d
TT
1436 return 0;
1437}
1438
1439static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1440{
1441 struct process_block_struct pb;
dfcdc32f 1442 ext2_ino_t ino, new_inode;
edfd9b0a 1443 struct ext2_inode *inode = NULL;
a8519a2d
TT
1444 ext2_inode_scan scan = NULL;
1445 errcode_t retval;
a8519a2d 1446 char *block_buf = 0;
dfcdc32f 1447 ext2_ino_t start_to_move;
8728d506
VAH
1448 blk64_t orig_size;
1449 blk64_t new_block;
4ef28824 1450 int inode_size;
efc6f628 1451
a8519a2d
TT
1452 if ((rfs->old_fs->group_desc_count <=
1453 rfs->new_fs->group_desc_count) &&
1454 !rfs->bmap)
1455 return 0;
1456
2bc4d4f7
TT
1457 /*
1458 * Save the original size of the old filesystem, and
1459 * temporarily set the size to be the new size if the new size
1460 * is larger. We need to do this to avoid catching an error
1461 * by the block iterator routines
1462 */
4efbac6f
VAH
1463 orig_size = ext2fs_blocks_count(rfs->old_fs->super);
1464 if (orig_size < ext2fs_blocks_count(rfs->new_fs->super))
1465 ext2fs_blocks_count_set(rfs->old_fs->super,
1466 ext2fs_blocks_count(rfs->new_fs->super));
2bc4d4f7 1467
a8519a2d
TT
1468 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1469 if (retval) goto errout;
1470
1471 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1472 if (retval) goto errout;
e5aace90 1473 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
a8519a2d
TT
1474 if (retval) goto errout;
1475
1476 start_to_move = (rfs->new_fs->group_desc_count *
1477 rfs->new_fs->super->s_inodes_per_group);
efc6f628 1478
3b627e8d
TT
1479 if (rfs->progress) {
1480 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1481 0, rfs->old_fs->group_desc_count);
1482 if (retval)
1483 goto errout;
1484 }
a8519a2d
TT
1485 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1486 pb.rfs = rfs;
edfd9b0a 1487 pb.inode = inode;
a8519a2d
TT
1488 pb.error = 0;
1489 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
4ef28824 1490 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
edfd9b0a
TT
1491 inode = malloc(inode_size);
1492 if (!inode) {
4ef28824
ES
1493 retval = ENOMEM;
1494 goto errout;
1495 }
a8519a2d
TT
1496 /*
1497 * First, copy all of the inodes that need to be moved
1498 * elsewhere in the inode table
1499 */
1500 while (1) {
edfd9b0a 1501 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
a8519a2d
TT
1502 if (retval) goto errout;
1503 if (!ino)
1504 break;
1505
edfd9b0a 1506 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
a8519a2d
TT
1507 continue; /* inode not in use */
1508
edfd9b0a 1509 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
a8519a2d
TT
1510 pb.changed = 0;
1511
0c80c44b 1512 if (ext2fs_file_acl_block(rfs->old_fs, inode) && rfs->bmap) {
d1a1a583 1513 new_block = extent_translate(rfs->old_fs, rfs->bmap,
0c80c44b 1514 ext2fs_file_acl_block(rfs->old_fs, inode));
ed909bbe 1515 if (new_block) {
0c80c44b
TT
1516 ext2fs_file_acl_block_set(rfs->old_fs, inode,
1517 new_block);
efc6f628 1518 retval = ext2fs_write_inode_full(rfs->old_fs,
edfd9b0a 1519 ino, inode, inode_size);
ed909bbe
TT
1520 if (retval) goto errout;
1521 }
1522 }
efc6f628 1523
0c80c44b 1524 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
a8519a2d
TT
1525 (rfs->bmap || pb.is_dir)) {
1526 pb.ino = ino;
8728d506 1527 retval = ext2fs_block_iterate3(rfs->old_fs,
a8519a2d
TT
1528 ino, 0, block_buf,
1529 process_block, &pb);
1530 if (retval)
1531 goto errout;
1532 if (pb.error) {
1533 retval = pb.error;
1534 goto errout;
1535 }
1536 }
1537
1538 if (ino <= start_to_move)
1539 continue; /* Don't need to move it. */
1540
1541 /*
1542 * Find a new inode
1543 */
86acdebd
TT
1544 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1545 if (retval)
1546 goto errout;
1547
74128f8d
TT
1548 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1549 pb.is_dir);
a8519a2d
TT
1550 if (pb.changed) {
1551 /* Get the new version of the inode */
4ef28824 1552 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
edfd9b0a 1553 inode, inode_size);
a8519a2d
TT
1554 if (retval) goto errout;
1555 }
edfd9b0a 1556 inode->i_ctime = time(0);
4ef28824 1557 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
edfd9b0a 1558 inode, inode_size);
a8519a2d
TT
1559 if (retval) goto errout;
1560
a8519a2d
TT
1561#ifdef RESIZE2FS_DEBUG
1562 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
f35fd3d5 1563 printf("Inode moved %u->%u\n", ino, new_inode);
a8519a2d
TT
1564#endif
1565 if (!rfs->imap) {
1566 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1567 if (retval)
1568 goto errout;
1569 }
1570 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1571 }
1572 io_channel_flush(rfs->old_fs->io);
1573
1574errout:
4efbac6f 1575 ext2fs_blocks_count_set(rfs->old_fs->super, orig_size);
a8519a2d
TT
1576 if (rfs->bmap) {
1577 ext2fs_free_extent_table(rfs->bmap);
1578 rfs->bmap = 0;
1579 }
1580 if (scan)
1581 ext2fs_close_inode_scan(scan);
1582 if (block_buf)
c4e3d3f3 1583 ext2fs_free_mem(&block_buf);
45e338f5 1584 free(inode);
a8519a2d
TT
1585 return retval;
1586}
1587
1588/* --------------------------------------------------------------------
1589 *
1590 * Resize processing, phase 4.
1591 *
1592 * --------------------------------------------------------------------
1593 */
1594
1595struct istruct {
1596 ext2_resize_t rfs;
3b627e8d 1597 errcode_t err;
03fa6f8a
TT
1598 unsigned int max_dirs;
1599 unsigned int num;
a8519a2d
TT
1600};
1601
efc6f628 1602static int check_and_change_inodes(ext2_ino_t dir,
54434927 1603 int entry EXT2FS_ATTR((unused)),
a8519a2d 1604 struct ext2_dir_entry *dirent, int offset,
54434927 1605 int blocksize EXT2FS_ATTR((unused)),
efc6f628 1606 char *buf EXT2FS_ATTR((unused)),
54434927 1607 void *priv_data)
a8519a2d
TT
1608{
1609 struct istruct *is = (struct istruct *) priv_data;
085d2a83
TT
1610 struct ext2_inode inode;
1611 ext2_ino_t new_inode;
1612 errcode_t retval;
a8519a2d
TT
1613
1614 if (is->rfs->progress && offset == 0) {
1615 io_channel_flush(is->rfs->old_fs->io);
3b627e8d
TT
1616 is->err = (is->rfs->progress)(is->rfs,
1617 E2_RSZ_INODE_REF_UPD_PASS,
1333fe93 1618 ++is->num, is->max_dirs);
3b627e8d
TT
1619 if (is->err)
1620 return DIRENT_ABORT;
a8519a2d
TT
1621 }
1622
1623 if (!dirent->inode)
1624 return 0;
1625
1626 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1627
1628 if (!new_inode)
1629 return 0;
1630#ifdef RESIZE2FS_DEBUG
1631 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
f35fd3d5 1632 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
06191693 1633 dir, dirent->name_len&0xFF, dirent->name,
a8519a2d
TT
1634 dirent->inode, new_inode);
1635#endif
1636
1637 dirent->inode = new_inode;
1638
085d2a83
TT
1639 /* Update the directory mtime and ctime */
1640 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1641 if (retval == 0) {
1642 inode.i_mtime = inode.i_ctime = time(0);
d2b2a488
BB
1643 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1644 if (is->err)
1645 return DIRENT_ABORT;
085d2a83
TT
1646 }
1647
a8519a2d
TT
1648 return DIRENT_CHANGED;
1649}
1650
1651static errcode_t inode_ref_fix(ext2_resize_t rfs)
1652{
1653 errcode_t retval;
1654 struct istruct is;
efc6f628 1655
a8519a2d
TT
1656 if (!rfs->imap)
1657 return 0;
efc6f628 1658
a8519a2d
TT
1659 /*
1660 * Now, we iterate over all of the directories to update the
1661 * inode references
1662 */
1663 is.num = 0;
8728d506 1664 is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
a8519a2d 1665 is.rfs = rfs;
3b627e8d 1666 is.err = 0;
a8519a2d 1667
3b627e8d
TT
1668 if (rfs->progress) {
1669 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1333fe93 1670 0, is.max_dirs);
3b627e8d
TT
1671 if (retval)
1672 goto errout;
1673 }
efc6f628 1674
a8519a2d
TT
1675 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1676 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1677 check_and_change_inodes, &is);
3b627e8d
TT
1678 if (retval)
1679 goto errout;
1680 if (is.err) {
1681 retval = is.err;
1682 goto errout;
1683 }
a8519a2d 1684
88dbf828
TT
1685 if (rfs->progress && (is.num < is.max_dirs))
1686 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1687 is.max_dirs, is.max_dirs);
1688
3b627e8d 1689errout:
a8519a2d
TT
1690 ext2fs_free_extent_table(rfs->imap);
1691 rfs->imap = 0;
1692 return retval;
1693}
1694
1695
1696/* --------------------------------------------------------------------
1697 *
1698 * Resize processing, phase 5.
1699 *
1700 * In this phase we actually move the inode table around, and then
1701 * update the summary statistics. This is scary, since aborting here
1702 * will potentially scramble the filesystem. (We are moving the
1703 * inode tables around in place, and so the potential for lost data,
1704 * or at the very least scrambling the mapping between filenames and
1705 * inode numbers is very high in case of a power failure here.)
1706 * --------------------------------------------------------------------
1707 */
1708
24b2c7a7 1709
052db4b7
TT
1710/*
1711 * A very scary routine --- this one moves the inode table around!!!
1712 *
1713 * After this you have to use the rfs->new_fs file handle to read and
1714 * write inodes.
1715 */
c762c8e6 1716static errcode_t move_itables(ext2_resize_t rfs)
052db4b7 1717{
8728d506
VAH
1718 int n, num, size;
1719 long long diff;
54434927 1720 dgrp_t i, max_groups;
052db4b7 1721 ext2_filsys fs = rfs->new_fs;
05e112a1 1722 char *cp;
8728d506 1723 blk64_t old_blk, new_blk, blk;
a8519a2d 1724 errcode_t retval;
64ad98ac 1725 int j, to_move, moved;
052db4b7 1726
1333fe93
TT
1727 max_groups = fs->group_desc_count;
1728 if (max_groups > rfs->old_fs->group_desc_count)
1729 max_groups = rfs->old_fs->group_desc_count;
052db4b7 1730
05e112a1
TT
1731 size = fs->blocksize * fs->inode_blocks_per_group;
1732 if (!rfs->itable_buf) {
c4e3d3f3 1733 retval = ext2fs_get_mem(size, &rfs->itable_buf);
ca8abba7
TT
1734 if (retval)
1735 return retval;
05e112a1 1736 }
c762c8e6
TT
1737
1738 /*
1739 * Figure out how many inode tables we need to move
1740 */
1741 to_move = moved = 0;
1333fe93 1742 for (i=0; i < max_groups; i++)
d7cca6b0
VAH
1743 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
1744 ext2fs_inode_table_loc(fs, i))
c762c8e6
TT
1745 to_move++;
1746
1747 if (to_move == 0)
1748 return 0;
1749
3b627e8d
TT
1750 if (rfs->progress) {
1751 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1752 0, to_move);
1753 if (retval)
1754 goto errout;
1755 }
63b44fbe 1756
a8519a2d
TT
1757 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1758
1333fe93 1759 for (i=0; i < max_groups; i++) {
d7cca6b0
VAH
1760 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
1761 new_blk = ext2fs_inode_table_loc(fs, i);
ca8abba7 1762 diff = new_blk - old_blk;
efc6f628 1763
80c0fc34 1764#ifdef RESIZE2FS_DEBUG
efc6f628 1765 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
8728d506 1766 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
ca8abba7 1767 i, old_blk, new_blk, diff);
80c0fc34 1768#endif
efc6f628 1769
05e112a1 1770 if (!diff)
052db4b7
TT
1771 continue;
1772
24a117ab
VAH
1773 retval = io_channel_read_blk64(fs->io, old_blk,
1774 fs->inode_blocks_per_group,
1775 rfs->itable_buf);
efc6f628 1776 if (retval)
a8519a2d 1777 goto errout;
05e112a1
TT
1778 /*
1779 * The end of the inode table segment often contains
a8519a2d
TT
1780 * all zeros, and we're often only moving the inode
1781 * table down a block or two. If so, we can optimize
1782 * things by not rewriting blocks that we know to be zero
1783 * already.
05e112a1 1784 */
2787276e 1785 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
05e112a1
TT
1786 if (*cp)
1787 break;
1788 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
80c0fc34 1789#ifdef RESIZE2FS_DEBUG
efc6f628 1790 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
f35fd3d5 1791 printf("%d blocks of zeros...\n", n);
80c0fc34 1792#endif
05e112a1
TT
1793 num = fs->inode_blocks_per_group;
1794 if (n > diff)
1795 num -= n;
1796
24a117ab
VAH
1797 retval = io_channel_write_blk64(fs->io, new_blk,
1798 num, rfs->itable_buf);
052db4b7 1799 if (retval) {
24a117ab
VAH
1800 io_channel_write_blk64(fs->io, old_blk,
1801 num, rfs->itable_buf);
a8519a2d 1802 goto errout;
052db4b7 1803 }
05e112a1 1804 if (n > diff) {
24a117ab 1805 retval = io_channel_write_blk64(fs->io,
ca8abba7 1806 old_blk + fs->inode_blocks_per_group,
a8519a2d
TT
1807 diff, (rfs->itable_buf +
1808 (fs->inode_blocks_per_group - diff) *
1809 fs->blocksize));
05e112a1 1810 if (retval)
a8519a2d 1811 goto errout;
c762c8e6 1812 }
64ad98ac 1813
d7cca6b0 1814 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
64ad98ac 1815 j < fs->inode_blocks_per_group ; j++, blk++)
48f23054 1816 ext2fs_block_alloc_stats2(fs, blk, -1);
64ad98ac 1817
d7cca6b0 1818 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
236efede 1819 ext2fs_group_desc_csum_set(rfs->old_fs, i);
a8519a2d 1820 ext2fs_mark_super_dirty(rfs->old_fs);
64ad98ac
TT
1821 ext2fs_flush(rfs->old_fs);
1822
a8519a2d 1823 if (rfs->progress) {
3b627e8d
TT
1824 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1825 ++moved, to_move);
1826 if (retval)
1827 goto errout;
a8519a2d 1828 }
052db4b7 1829 }
64ad98ac 1830 mark_table_blocks(fs, fs->block_map);
a8519a2d 1831 ext2fs_flush(fs);
80c0fc34 1832#ifdef RESIZE2FS_DEBUG
efc6f628 1833 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
f35fd3d5 1834 printf("Inode table move finished.\n");
80c0fc34 1835#endif
052db4b7 1836 return 0;
efc6f628 1837
a8519a2d 1838errout:
052db4b7
TT
1839 return retval;
1840}
1841
9213a93b 1842/*
efc6f628 1843 * Fix the resize inode
9213a93b
TT
1844 */
1845static errcode_t fix_resize_inode(ext2_filsys fs)
1846{
1847 struct ext2_inode inode;
1848 errcode_t retval;
5e27a274 1849 char *block_buf = NULL;
9213a93b 1850
efc6f628 1851 if (!(fs->super->s_feature_compat &
9213a93b
TT
1852 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1853 return 0;
1854
1855 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1856 if (retval) goto errout;
1857
1858 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1859 if (retval) goto errout;
1860
1ca1059f 1861 ext2fs_iblk_set(fs, &inode, 1);
9213a93b
TT
1862
1863 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1864 if (retval) goto errout;
1865
1866 if (!inode.i_block[EXT2_DIND_BLOCK]) {
efc6f628 1867 /*
9213a93b
TT
1868 * Avoid zeroing out block #0; that's rude. This
1869 * should never happen anyway since the filesystem
1870 * should be fsck'ed and we assume it is consistent.
1871 */
45ff69ff 1872 fprintf(stderr, "%s",
f35fd3d5 1873 _("Should never happen: resize inode corrupt!\n"));
9213a93b
TT
1874 exit(1);
1875 }
1876
1877 memset(block_buf, 0, fs->blocksize);
1878
24a117ab
VAH
1879 retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1880 1, block_buf);
9213a93b 1881 if (retval) goto errout;
efc6f628 1882
9213a93b
TT
1883 retval = ext2fs_create_resize_inode(fs);
1884 if (retval)
1885 goto errout;
1886
1887errout:
1888 if (block_buf)
1889 ext2fs_free_mem(&block_buf);
1890 return retval;
1891}
1892
052db4b7
TT
1893/*
1894 * Finally, recalculate the summary information
1895 */
1896static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1897{
8728d506 1898 blk64_t blk;
dfcdc32f 1899 ext2_ino_t ino;
54434927
TT
1900 unsigned int group = 0;
1901 unsigned int count = 0;
2279fd78
TT
1902 blk64_t total_blocks_free = 0;
1903 int total_inodes_free = 0;
dfcdc32f 1904 int group_free = 0;
74128f8d 1905 int uninit = 0;
8728d506 1906 blk64_t super_blk, old_desc_blk, new_desc_blk;
74128f8d 1907 int old_desc_blocks;
052db4b7
TT
1908
1909 /*
1910 * First calculate the block statistics
1911 */
cd65a24e 1912 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
8728d506
VAH
1913 ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
1914 &new_desc_blk, 0);
74128f8d
TT
1915 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1916 old_desc_blocks = fs->super->s_first_meta_bg;
1917 else
1918 old_desc_blocks = fs->desc_blocks +
1919 fs->super->s_reserved_gdt_blocks;
1773c87c
TT
1920 for (blk = B2C(fs->super->s_first_data_block);
1921 blk < ext2fs_blocks_count(fs->super);
1922 blk += EXT2FS_CLUSTER_RATIO(fs)) {
74128f8d 1923 if ((uninit &&
1773c87c 1924 !(EQ_CLSTR(blk, super_blk) ||
74128f8d 1925 ((old_desc_blk && old_desc_blocks &&
1773c87c
TT
1926 GE_CLSTR(blk, old_desc_blk) &&
1927 LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) ||
1928 ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) ||
1929 EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) ||
1930 EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) ||
1931 ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) &&
1932 LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group)
1933 + fs->inode_blocks_per_group))))) ||
3346084b 1934 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
052db4b7 1935 group_free++;
2279fd78 1936 total_blocks_free++;
052db4b7
TT
1937 }
1938 count++;
1773c87c
TT
1939 if ((count == fs->super->s_clusters_per_group) ||
1940 EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) {
d7cca6b0 1941 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
236efede
JS
1942 ext2fs_group_desc_csum_set(fs, group);
1943 group++;
4828bbe9
TT
1944 if (group >= fs->group_desc_count)
1945 break;
052db4b7
TT
1946 count = 0;
1947 group_free = 0;
8728d506
VAH
1948 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1949 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
1950 &old_desc_blk,
1951 &new_desc_blk, 0);
74128f8d
TT
1952 if (fs->super->s_feature_incompat &
1953 EXT2_FEATURE_INCOMPAT_META_BG)
1954 old_desc_blocks = fs->super->s_first_meta_bg;
1955 else
1956 old_desc_blocks = fs->desc_blocks +
1957 fs->super->s_reserved_gdt_blocks;
052db4b7
TT
1958 }
1959 }
1773c87c 1960 total_blocks_free = C2B(total_blocks_free);
2279fd78 1961 ext2fs_free_blocks_count_set(fs->super, total_blocks_free);
efc6f628 1962
052db4b7
TT
1963 /*
1964 * Next, calculate the inode statistics
1965 */
1966 group_free = 0;
052db4b7
TT
1967 count = 0;
1968 group = 0;
5830d6be
ES
1969
1970 /* Protect loop from wrap-around if s_inodes_count maxed */
cd65a24e 1971 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
5830d6be 1972 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
74128f8d 1973 if (uninit ||
3346084b 1974 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
052db4b7 1975 group_free++;
2279fd78 1976 total_inodes_free++;
052db4b7
TT
1977 }
1978 count++;
1979 if ((count == fs->super->s_inodes_per_group) ||
1980 (ino == fs->super->s_inodes_count)) {
d7cca6b0 1981 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
236efede
JS
1982 ext2fs_group_desc_csum_set(fs, group);
1983 group++;
4828bbe9
TT
1984 if (group >= fs->group_desc_count)
1985 break;
052db4b7
TT
1986 count = 0;
1987 group_free = 0;
cd65a24e 1988 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
052db4b7
TT
1989 }
1990 }
2279fd78 1991 fs->super->s_free_inodes_count = total_inodes_free;
052db4b7
TT
1992 ext2fs_mark_super_dirty(fs);
1993 return 0;
1994}
199ddaaa 1995
8a6ede8b
ES
1996/*
1997 * Journal may have been relocated; update the backup journal blocks
1998 * in the superblock.
1999 */
2000static errcode_t fix_sb_journal_backup(ext2_filsys fs)
2001{
2002 errcode_t retval;
2003 struct ext2_inode inode;
2004
2005 if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
2006 return 0;
2007
d93d5bbf
ES
2008 /* External journal? Nothing to do. */
2009 if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
2010 return 0;
2011
8a6ede8b
ES
2012 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
2013 if (retval)
2014 return retval;
2015 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
931b58e1 2016 fs->super->s_jnl_blocks[15] = inode.i_size_high;
8a6ede8b
ES
2017 fs->super->s_jnl_blocks[16] = inode.i_size;
2018 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
2019 ext2fs_mark_super_dirty(fs);
2020 return 0;
2021}
2022
7f820344
TT
2023static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
2024 int old_desc_blocks)
2025{
2026 blk64_t super_blk, old_desc_blk, new_desc_blk;
2027 int overhead;
2028
2029 /* inode table blocks plus allocation bitmaps */
2030 overhead = fs->inode_blocks_per_group + 2;
2031
2032 ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
2033 &old_desc_blk, &new_desc_blk, 0);
2034 if ((grp == 0) || super_blk)
2035 overhead++;
2036 if (old_desc_blk)
2037 overhead += old_desc_blocks;
2038 else if (new_desc_blk)
2039 overhead++;
2040 return overhead;
2041}
2042
2043
199ddaaa
JB
2044/*
2045 * calcluate the minimum number of blocks the given fs can be resized to
2046 */
e231f175 2047blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
199ddaaa 2048{
8728d506 2049 ext2_ino_t inode_count;
e231f175
TT
2050 dgrp_t groups;
2051 blk64_t blks_needed, data_blocks;
8728d506
VAH
2052 blk64_t grp, data_needed, last_start;
2053 blk64_t overhead = 0;
7f820344 2054 int old_desc_blocks;
2884d208
PT
2055 int extra_groups = 0;
2056 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
199ddaaa
JB
2057
2058 /*
2059 * first figure out how many group descriptors we need to
2060 * handle the number of inodes we have
2061 */
2062 inode_count = fs->super->s_inodes_count -
2063 fs->super->s_free_inodes_count;
2064 blks_needed = ext2fs_div_ceil(inode_count,
2065 fs->super->s_inodes_per_group) *
2066 EXT2_BLOCKS_PER_GROUP(fs->super);
8728d506
VAH
2067 groups = ext2fs_div64_ceil(blks_needed,
2068 EXT2_BLOCKS_PER_GROUP(fs->super));
e231f175
TT
2069#ifdef RESIZE2FS_DEBUG
2070 if (flags & RESIZE_DEBUG_MIN_CALC)
2071 printf("fs has %d inodes, %d groups required.\n",
2072 inode_count, groups);
2073#endif
199ddaaa
JB
2074
2075 /*
7f820344 2076 * number of old-style block group descriptor blocks
199ddaaa 2077 */
7f820344
TT
2078 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2079 old_desc_blocks = fs->super->s_first_meta_bg;
2080 else
2081 old_desc_blocks = fs->desc_blocks +
2082 fs->super->s_reserved_gdt_blocks;
199ddaaa
JB
2083
2084 /* calculate how many blocks are needed for data */
4efbac6f
VAH
2085 data_needed = ext2fs_blocks_count(fs->super) -
2086 ext2fs_free_blocks_count(fs->super);
199ddaaa 2087
7f820344
TT
2088 for (grp = 0; grp < fs->group_desc_count; grp++)
2089 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
e231f175
TT
2090#ifdef RESIZE2FS_DEBUG
2091 if (flags & RESIZE_DEBUG_MIN_CALC)
2092 printf("fs requires %llu data blocks.\n", data_needed);
2093#endif
7f820344
TT
2094
2095 /*
2096 * For ext4 we need to allow for up to a flex_bg worth of
2097 * inode tables of slack space so the resize operation can be
2098 * guaranteed to finish.
2099 */
c09043f1 2100 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
c09043f1 2101 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
7f820344 2102 data_needed += fs->inode_blocks_per_group * extra_groups;
2884d208 2103 extra_groups = groups % flexbg_size;
c09043f1
TT
2104 }
2105
199ddaaa
JB
2106 /*
2107 * figure out how many data blocks we have given the number of groups
2108 * we need for our inodes
2109 */
2110 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
2111 last_start = 0;
2112 for (grp = 0; grp < groups; grp++) {
7f820344 2113 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
199ddaaa
JB
2114
2115 /*
2116 * we want to keep track of how much data we can store in
2117 * the groups leading up to the last group so we can determine
2118 * how big the last group needs to be
2119 */
2120 if (grp != (groups - 1))
2121 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
2122 overhead;
2123
2124 data_blocks -= overhead;
2125 }
e231f175
TT
2126#ifdef RESIZE2FS_DEBUG
2127 if (flags & RESIZE_DEBUG_MIN_CALC)
2128 printf("With %d group(s), we have %llu blocks available.\n",
2129 groups, data_blocks);
2130#endif
199ddaaa
JB
2131
2132 /*
2133 * if we need more group descriptors in order to accomodate our data
2134 * then we need to add them here
2135 */
2136 while (data_needed > data_blocks) {
8728d506 2137 blk64_t remainder = data_needed - data_blocks;
e231f175 2138 dgrp_t extra_grps;
199ddaaa
JB
2139
2140 /* figure out how many more groups we need for the data */
8728d506
VAH
2141 extra_grps = ext2fs_div64_ceil(remainder,
2142 EXT2_BLOCKS_PER_GROUP(fs->super));
199ddaaa
JB
2143
2144 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
2145
2146 /* ok we have to account for the last group */
7f820344 2147 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
199ddaaa
JB
2148 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
2149
2150 for (grp = groups; grp < groups+extra_grps; grp++) {
7f820344
TT
2151 overhead = calc_group_overhead(fs, grp,
2152 old_desc_blocks);
199ddaaa
JB
2153
2154 /*
2155 * again, we need to see how much data we cram into
2156 * all of the groups leading up to the last group
2157 */
2158 if (grp != (groups + extra_grps - 1))
2159 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
2160 - overhead;
2161
2162 data_blocks -= overhead;
2163 }
2164
2165 groups += extra_grps;
2884d208
PT
2166 extra_groups += extra_grps;
2167 if (fs->super->s_feature_incompat
2168 & EXT4_FEATURE_INCOMPAT_FLEX_BG
2169 && extra_groups > flexbg_size) {
2170 /*
2171 * For ext4 we need to allow for up to a flex_bg worth
2172 * of inode tables of slack space so the resize
2173 * operation can be guaranteed to finish.
2174 */
2175 extra_groups = flexbg_size -
2176 (groups & (flexbg_size - 1));
7f820344
TT
2177 data_needed += (fs->inode_blocks_per_group *
2178 extra_groups);
2884d208
PT
2179 extra_groups = groups % flexbg_size;
2180 }
e231f175
TT
2181#ifdef RESIZE2FS_DEBUG
2182 if (flags & RESIZE_DEBUG_MIN_CALC)
2183 printf("Added %d extra group(s), "
2184 "data_needed %llu, data_blocks %llu, "
2185 "last_start %llu\n",
2186 extra_grps, data_needed, data_blocks,
2187 last_start);
2188#endif
199ddaaa
JB
2189 }
2190
2191 /* now for the fun voodoo */
7f820344 2192 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
e231f175
TT
2193#ifdef RESIZE2FS_DEBUG
2194 if (flags & RESIZE_DEBUG_MIN_CALC)
2195 printf("Last group's overhead is %llu\n", overhead);
2196#endif
199ddaaa
JB
2197
2198 /*
2199 * if this is the case then the last group is going to have data in it
2200 * so we need to adjust the size of the last group accordingly
2201 */
2202 if (last_start < data_needed) {
8728d506 2203 blk64_t remainder = data_needed - last_start;
199ddaaa 2204
e231f175
TT
2205#ifdef RESIZE2FS_DEBUG
2206 if (flags & RESIZE_DEBUG_MIN_CALC)
2207 printf("Need %llu data blocks in last group\n",
2208 remainder);
2209#endif
199ddaaa
JB
2210 /*
2211 * 50 is a magic number that mkfs/resize uses to see if its
2212 * even worth making/resizing the fs. basically you need to
2213 * have at least 50 blocks in addition to the blocks needed
2214 * for the metadata in the last group
2215 */
2216 if (remainder > 50)
2217 overhead += remainder;
2218 else
2219 overhead += 50;
2220 } else
2221 overhead += 50;
2222
ba8bfa1a 2223 overhead += fs->super->s_first_data_block;
e231f175
TT
2224#ifdef RESIZE2FS_DEBUG
2225 if (flags & RESIZE_DEBUG_MIN_CALC)
2226 printf("Final size of last group is %lld\n", overhead);
2227#endif
199ddaaa
JB
2228
2229 /*
2230 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2231 * only do groups-1, and then add the number of blocks needed to
2232 * handle the group descriptor metadata+data that we need
2233 */
2234 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2235 blks_needed += overhead;
2236
2215293c
TT
2237 /*
2238 * Make sure blks_needed covers the end of the inode table in
2239 * the last block group.
2240 */
2241 overhead = ext2fs_inode_table_loc(fs, groups-1) +
2242 fs->inode_blocks_per_group;
2243 if (blks_needed < overhead)
2244 blks_needed = overhead;
2245
e231f175
TT
2246#ifdef RESIZE2FS_DEBUG
2247 if (flags & RESIZE_DEBUG_MIN_CALC)
2248 printf("Estimated blocks needed: %llu\n", blks_needed);
2249#endif
2250
69f7c80e
ES
2251 /*
2252 * If at this point we've already added up more "needed" than
2253 * the current size, just return current size as minimum.
2254 */
4efbac6f
VAH
2255 if (blks_needed >= ext2fs_blocks_count(fs->super))
2256 return ext2fs_blocks_count(fs->super);
793a04a0
TT
2257 /*
2258 * We need to reserve a few extra blocks if extents are
2259 * enabled, in case we need to grow the extent tree. The more
2260 * we shrink the file system, the more space we need.
2261 */
e231f175
TT
2262 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
2263 blk64_t safe_margin = (ext2fs_blocks_count(fs->super) -
2264 blks_needed)/500;
2265#ifdef RESIZE2FS_DEBUG
2266 if (flags & RESIZE_DEBUG_MIN_CALC)
2267 printf("Extents safety margin: %llu\n", safe_margin);
2268#endif
2269 blks_needed += safe_margin;
2270 }
793a04a0 2271
199ddaaa
JB
2272 return blks_needed;
2273}