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