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