]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - resize/resize2fs.c
resize2fs: attempt to keep the # of inodes valid by removing the last bg
[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 *
055866d8 7 * Copyright (C) 1999, 2000 by Theodore 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);
895e8e33 52static errcode_t resize2fs_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);
65c6c3e0
TT
56static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs);
57static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
58 ext2fs_block_bitmap meta_bmap);
fe12931f
DW
59static errcode_t resize_group_descriptors(ext2_resize_t rfs, blk64_t new_size);
60static errcode_t move_bg_metadata(ext2_resize_t rfs);
61static errcode_t zero_high_bits_in_inodes(ext2_resize_t rfs);
a8519a2d
TT
62
63/*
539d3ae3 64 * Some helper functions to check if a block is in a metadata area
a8519a2d 65 */
539d3ae3
AD
66static inline int is_block_bm(ext2_filsys fs, unsigned int grp, blk64_t blk)
67{
68 return blk == ext2fs_block_bitmap_loc(fs, grp);
69}
a8519a2d 70
539d3ae3
AD
71static inline int is_inode_bm(ext2_filsys fs, unsigned int grp, blk64_t blk)
72{
73 return blk == ext2fs_inode_bitmap_loc(fs, grp);
74}
75
62f9bd0e 76static int is_inode_tb(ext2_filsys fs, unsigned int grp, blk64_t blk)
539d3ae3
AD
77{
78 return blk >= ext2fs_inode_table_loc(fs, grp) &&
79 blk < (ext2fs_inode_table_loc(fs, grp) +
80 fs->inode_blocks_per_group);
81}
a8519a2d 82
055866d8 83/* Some bigalloc helper macros which are more succinct... */
1773c87c
TT
84#define B2C(x) EXT2FS_B2C(fs, (x))
85#define C2B(x) EXT2FS_C2B(fs, (x))
86#define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
87#define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
88#define LT_CLSTR(x, y) (B2C(x) < B2C(y))
89#define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
90#define GT_CLSTR(x, y) (B2C(x) > B2C(y))
91
f404167d 92static int lazy_itable_init;
a8519a2d
TT
93
94/*
95 * This is the top-level routine which does the dirty deed....
96 */
8728d506 97errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
3346084b 98 errcode_t (*progress)(ext2_resize_t rfs, int pass,
86acdebd
TT
99 unsigned long cur,
100 unsigned long max_val))
a8519a2d
TT
101{
102 ext2_resize_t rfs;
103 errcode_t retval;
1eb31c48 104 struct resource_track rtrack, overall_track;
96cdb37e 105
a8519a2d
TT
106 /*
107 * Create the data structure
108 */
c4e3d3f3 109 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
a8519a2d
TT
110 if (retval)
111 return retval;
a8519a2d 112
1eb31c48 113 memset(rfs, 0, sizeof(struct ext2_resize_struct));
7f9c96ee 114 fs->priv_data = rfs;
a8519a2d
TT
115 rfs->old_fs = fs;
116 rfs->flags = flags;
117 rfs->itable_buf = 0;
118 rfs->progress = progress;
1eb31c48
TT
119
120 init_resource_track(&overall_track, "overall resize2fs", fs->io);
121 init_resource_track(&rtrack, "read_bitmaps", fs->io);
122 retval = ext2fs_read_bitmaps(fs);
123 if (retval)
124 goto errout;
125 print_resource_track(rfs, &rtrack, fs->io);
126
127 fs->super->s_state |= EXT2_ERROR_FS;
128 ext2fs_mark_super_dirty(fs);
129 ext2fs_flush(fs);
130
131 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io);
132 fix_uninit_block_bitmaps(fs);
133 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d
TT
134 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
135 if (retval)
136 goto errout;
137
fe12931f
DW
138 init_resource_track(&rtrack, "resize_group_descriptors", fs->io);
139 retval = resize_group_descriptors(rfs, *new_size);
140 if (retval)
141 goto errout;
142 print_resource_track(rfs, &rtrack, fs->io);
143
144 init_resource_track(&rtrack, "move_bg_metadata", fs->io);
145 retval = move_bg_metadata(rfs);
146 if (retval)
147 goto errout;
148 print_resource_track(rfs, &rtrack, fs->io);
149
150 init_resource_track(&rtrack, "zero_high_bits_in_metadata", fs->io);
151 retval = zero_high_bits_in_inodes(rfs);
152 if (retval)
153 goto errout;
154 print_resource_track(rfs, &rtrack, fs->io);
155
1eb31c48 156 init_resource_track(&rtrack, "adjust_superblock", fs->io);
116db1b5 157 retval = adjust_superblock(rfs, *new_size);
a8519a2d
TT
158 if (retval)
159 goto errout;
1eb31c48 160 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d 161
1eb31c48 162 init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io);
86acdebd 163 fix_uninit_block_bitmaps(rfs->new_fs);
1eb31c48 164 print_resource_track(rfs, &rtrack, fs->io);
86acdebd 165 /* Clear the block bitmap uninit flag for the last block group */
e633b58a 166 ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
732c8cd5 167 EXT2_BG_BLOCK_UNINIT);
86acdebd 168
4efbac6f 169 *new_size = ext2fs_blocks_count(rfs->new_fs->super);
116db1b5 170
1eb31c48 171 init_resource_track(&rtrack, "blocks_to_move", fs->io);
a8519a2d
TT
172 retval = blocks_to_move(rfs);
173 if (retval)
174 goto errout;
1eb31c48 175 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d
TT
176
177#ifdef RESIZE2FS_DEBUG
178 if (rfs->flags & RESIZE_DEBUG_BMOVE)
8728d506 179 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
33b9a60c
TT
180 (unsigned long long) ext2fs_free_blocks_count(rfs->old_fs->super),
181 (unsigned long long) ext2fs_free_blocks_count(rfs->new_fs->super),
182 (unsigned long long) rfs->needed_blocks);
a8519a2d 183#endif
efc6f628 184
1eb31c48 185 init_resource_track(&rtrack, "block_mover", fs->io);
a8519a2d
TT
186 retval = block_mover(rfs);
187 if (retval)
188 goto errout;
1eb31c48 189 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d 190
1eb31c48 191 init_resource_track(&rtrack, "inode_scan_and_fix", fs->io);
a8519a2d
TT
192 retval = inode_scan_and_fix(rfs);
193 if (retval)
194 goto errout;
1eb31c48 195 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d 196
1eb31c48 197 init_resource_track(&rtrack, "inode_ref_fix", fs->io);
a8519a2d
TT
198 retval = inode_ref_fix(rfs);
199 if (retval)
200 goto errout;
1eb31c48 201 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d 202
1eb31c48 203 init_resource_track(&rtrack, "move_itables", fs->io);
a8519a2d
TT
204 retval = move_itables(rfs);
205 if (retval)
206 goto errout;
1eb31c48 207 print_resource_track(rfs, &rtrack, fs->io);
a8519a2d 208
aaa1ae08
TT
209 retval = clear_sparse_super2_last_group(rfs);
210 if (retval)
211 goto errout;
212
1eb31c48 213 init_resource_track(&rtrack, "calculate_summary_stats", fs->io);
895e8e33 214 retval = resize2fs_calculate_summary_stats(rfs->new_fs);
64ad98ac
TT
215 if (retval)
216 goto errout;
1eb31c48 217 print_resource_track(rfs, &rtrack, fs->io);
efc6f628 218
1eb31c48 219 init_resource_track(&rtrack, "fix_resize_inode", fs->io);
9213a93b
TT
220 retval = fix_resize_inode(rfs->new_fs);
221 if (retval)
222 goto errout;
1eb31c48 223 print_resource_track(rfs, &rtrack, fs->io);
9213a93b 224
1eb31c48 225 init_resource_track(&rtrack, "fix_sb_journal_backup", fs->io);
8a6ede8b
ES
226 retval = fix_sb_journal_backup(rfs->new_fs);
227 if (retval)
228 goto errout;
1eb31c48 229 print_resource_track(rfs, &rtrack, fs->io);
8a6ede8b 230
1862ef72
DW
231 retval = ext2fs_set_gdt_csum(rfs->new_fs);
232 if (retval)
233 goto errout;
234
96cdb37e 235 rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
efc6f628 236 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1eb31c48
TT
237
238 print_resource_track(rfs, &overall_track, fs->io);
47fee2ef 239 retval = ext2fs_close_free(&rfs->new_fs);
a8519a2d
TT
240 if (retval)
241 goto errout;
242
243 rfs->flags = flags;
efc6f628 244
a8519a2d 245 ext2fs_free(rfs->old_fs);
585bca68 246 rfs->old_fs = NULL;
a8519a2d 247 if (rfs->itable_buf)
c4e3d3f3 248 ext2fs_free_mem(&rfs->itable_buf);
e2ca097f
TT
249 if (rfs->reserve_blocks)
250 ext2fs_free_block_bitmap(rfs->reserve_blocks);
251 if (rfs->move_blocks)
252 ext2fs_free_block_bitmap(rfs->move_blocks);
c4e3d3f3 253 ext2fs_free_mem(&rfs);
efc6f628 254
a8519a2d
TT
255 return 0;
256
257errout:
585bca68 258 if (rfs->new_fs) {
a8519a2d 259 ext2fs_free(rfs->new_fs);
585bca68
LC
260 rfs->new_fs = NULL;
261 }
a8519a2d 262 if (rfs->itable_buf)
c4e3d3f3
TT
263 ext2fs_free_mem(&rfs->itable_buf);
264 ext2fs_free_mem(&rfs);
a8519a2d
TT
265 return retval;
266}
267
fe12931f
DW
268/* Keep the size of the group descriptor region constant */
269static void adjust_reserved_gdt_blocks(ext2_filsys old_fs, ext2_filsys fs)
270{
21e37be6 271 if (ext2fs_has_feature_resize_inode(fs->super) &&
fe12931f
DW
272 (old_fs->desc_blocks != fs->desc_blocks)) {
273 int new;
274
275 new = ((int) fs->super->s_reserved_gdt_blocks) +
276 (old_fs->desc_blocks - fs->desc_blocks);
277 if (new < 0)
278 new = 0;
279 if (new > (int) fs->blocksize/4)
280 new = fs->blocksize/4;
281 fs->super->s_reserved_gdt_blocks = new;
282 }
283}
284
285/* Toggle 64bit mode */
286static errcode_t resize_group_descriptors(ext2_resize_t rfs, blk64_t new_size)
287{
288 void *o, *n, *new_group_desc;
289 dgrp_t i;
290 int copy_size;
291 errcode_t retval;
292
293 if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
294 return 0;
295
296 if (new_size != ext2fs_blocks_count(rfs->new_fs->super) ||
297 ext2fs_blocks_count(rfs->new_fs->super) >= (1ULL << 32) ||
298 (rfs->flags & RESIZE_DISABLE_64BIT &&
299 rfs->flags & RESIZE_ENABLE_64BIT))
300 return EXT2_ET_INVALID_ARGUMENT;
301
302 if (rfs->flags & RESIZE_DISABLE_64BIT) {
21e37be6 303 ext2fs_clear_feature_64bit(rfs->new_fs->super);
fe12931f
DW
304 rfs->new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE;
305 } else if (rfs->flags & RESIZE_ENABLE_64BIT) {
21e37be6 306 ext2fs_set_feature_64bit(rfs->new_fs->super);
fe12931f
DW
307 rfs->new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
308 }
309
310 if (EXT2_DESC_SIZE(rfs->old_fs->super) ==
311 EXT2_DESC_SIZE(rfs->new_fs->super))
312 return 0;
313
314 o = rfs->new_fs->group_desc;
315 rfs->new_fs->desc_blocks = ext2fs_div_ceil(
316 rfs->old_fs->group_desc_count,
317 EXT2_DESC_PER_BLOCK(rfs->new_fs->super));
318 retval = ext2fs_get_arrayzero(rfs->new_fs->desc_blocks,
319 rfs->old_fs->blocksize, &new_group_desc);
320 if (retval)
321 return retval;
322
323 n = new_group_desc;
324
325 if (EXT2_DESC_SIZE(rfs->old_fs->super) <=
326 EXT2_DESC_SIZE(rfs->new_fs->super))
327 copy_size = EXT2_DESC_SIZE(rfs->old_fs->super);
328 else
329 copy_size = EXT2_DESC_SIZE(rfs->new_fs->super);
330 for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
331 memcpy(n, o, copy_size);
62f9bd0e
TT
332 n = (char *)n + EXT2_DESC_SIZE(rfs->new_fs->super);
333 o = (char *)o + EXT2_DESC_SIZE(rfs->old_fs->super);
fe12931f
DW
334 }
335
336 ext2fs_free_mem(&rfs->new_fs->group_desc);
337 rfs->new_fs->group_desc = new_group_desc;
338
339 for (i = 0; i < rfs->old_fs->group_desc_count; i++)
340 ext2fs_group_desc_csum_set(rfs->new_fs, i);
341
342 adjust_reserved_gdt_blocks(rfs->old_fs, rfs->new_fs);
343
344 return 0;
345}
346
347/* Move bitmaps/inode tables out of the way. */
348static errcode_t move_bg_metadata(ext2_resize_t rfs)
349{
350 dgrp_t i;
351 blk64_t b, c, d, old_desc_blocks, new_desc_blocks, j;
352 ext2fs_block_bitmap old_map, new_map;
353 int old, new;
354 errcode_t retval;
355 int cluster_ratio;
356
357 if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
358 return 0;
359
360 retval = ext2fs_allocate_block_bitmap(rfs->old_fs, "oldfs", &old_map);
361 if (retval)
362 return retval;
363
364 retval = ext2fs_allocate_block_bitmap(rfs->new_fs, "newfs", &new_map);
365 if (retval)
366 goto out;
367
21e37be6 368 if (ext2fs_has_feature_meta_bg(rfs->old_fs->super)) {
fe12931f
DW
369 old_desc_blocks = rfs->old_fs->super->s_first_meta_bg;
370 new_desc_blocks = rfs->new_fs->super->s_first_meta_bg;
371 } else {
372 old_desc_blocks = rfs->old_fs->desc_blocks +
373 rfs->old_fs->super->s_reserved_gdt_blocks;
374 new_desc_blocks = rfs->new_fs->desc_blocks +
375 rfs->new_fs->super->s_reserved_gdt_blocks;
376 }
377
378 /* Construct bitmaps of super/descriptor blocks in old and new fs */
379 for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
380 retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, i, &b, &c, &d,
381 NULL);
382 if (retval)
383 goto out;
384 if (b)
385 ext2fs_mark_block_bitmap2(old_map, b);
386 for (j = 0; c != 0 && j < old_desc_blocks; j++)
387 ext2fs_mark_block_bitmap2(old_map, c + j);
388 if (d)
389 ext2fs_mark_block_bitmap2(old_map, d);
390
391 retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, i, &b, &c, &d,
392 NULL);
393 if (retval)
394 goto out;
395 if (b)
396 ext2fs_mark_block_bitmap2(new_map, b);
397 for (j = 0; c != 0 && j < new_desc_blocks; j++)
398 ext2fs_mark_block_bitmap2(new_map, c + j);
399 if (d)
400 ext2fs_mark_block_bitmap2(new_map, d);
401 }
402
403 cluster_ratio = EXT2FS_CLUSTER_RATIO(rfs->new_fs);
404
405 /* Find changes in block allocations for bg metadata */
406 for (b = EXT2FS_B2C(rfs->old_fs,
407 rfs->old_fs->super->s_first_data_block);
408 b < ext2fs_blocks_count(rfs->new_fs->super);
409 b += cluster_ratio) {
410 old = ext2fs_test_block_bitmap2(old_map, b);
411 new = ext2fs_test_block_bitmap2(new_map, b);
412
413 if (old && !new) {
414 /* mark old_map, unmark new_map */
415 if (cluster_ratio == 1)
416 ext2fs_unmark_block_bitmap2(
417 rfs->new_fs->block_map, b);
418 } else if (!old && new)
419 ; /* unmark old_map, mark new_map */
420 else {
421 ext2fs_unmark_block_bitmap2(old_map, b);
422 ext2fs_unmark_block_bitmap2(new_map, b);
423 }
424 }
425
426 /*
427 * new_map now shows blocks that have been newly allocated.
428 * old_map now shows blocks that have been newly freed.
429 */
430
431 /*
432 * Move any conflicting bitmaps and inode tables. Ensure that we
433 * don't try to free clusters associated with bitmaps or tables.
434 */
435 for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
436 b = ext2fs_block_bitmap_loc(rfs->new_fs, i);
437 if (ext2fs_test_block_bitmap2(new_map, b))
438 ext2fs_block_bitmap_loc_set(rfs->new_fs, i, 0);
439 else if (ext2fs_test_block_bitmap2(old_map, b))
440 ext2fs_unmark_block_bitmap2(old_map, b);
441
442 b = ext2fs_inode_bitmap_loc(rfs->new_fs, i);
443 if (ext2fs_test_block_bitmap2(new_map, b))
444 ext2fs_inode_bitmap_loc_set(rfs->new_fs, i, 0);
445 else if (ext2fs_test_block_bitmap2(old_map, b))
446 ext2fs_unmark_block_bitmap2(old_map, b);
447
448 c = ext2fs_inode_table_loc(rfs->new_fs, i);
449 for (b = 0;
450 b < rfs->new_fs->inode_blocks_per_group;
451 b++) {
452 if (ext2fs_test_block_bitmap2(new_map, b + c))
453 ext2fs_inode_table_loc_set(rfs->new_fs, i, 0);
454 else if (ext2fs_test_block_bitmap2(old_map, b + c))
455 ext2fs_unmark_block_bitmap2(old_map, b + c);
456 }
457 }
458
459 /* Free unused clusters */
460 for (b = 0;
461 cluster_ratio > 1 && b < ext2fs_blocks_count(rfs->new_fs->super);
462 b += cluster_ratio)
463 if (ext2fs_test_block_bitmap2(old_map, b))
464 ext2fs_unmark_block_bitmap2(rfs->new_fs->block_map, b);
465out:
466 if (old_map)
467 ext2fs_free_block_bitmap(old_map);
468 if (new_map)
469 ext2fs_free_block_bitmap(new_map);
470 return retval;
471}
472
473/* Zero out the high bits of extent fields */
474static errcode_t zero_high_bits_in_extents(ext2_filsys fs, ext2_ino_t ino,
475 struct ext2_inode *inode)
476{
477 ext2_extent_handle_t handle;
478 struct ext2fs_extent extent;
479 int op = EXT2_EXTENT_ROOT;
480 errcode_t errcode;
481
482 if (!(inode->i_flags & EXT4_EXTENTS_FL))
483 return 0;
484
485 errcode = ext2fs_extent_open(fs, ino, &handle);
486 if (errcode)
487 return errcode;
488
489 while (1) {
490 errcode = ext2fs_extent_get(handle, op, &extent);
491 if (errcode)
492 break;
493
494 op = EXT2_EXTENT_NEXT_SIB;
495
496 if (extent.e_pblk > (1ULL << 32)) {
497 extent.e_pblk &= (1ULL << 32) - 1;
498 errcode = ext2fs_extent_replace(handle, 0, &extent);
499 if (errcode)
500 break;
501 }
502 }
503
504 /* Ok if we run off the end */
505 if (errcode == EXT2_ET_EXTENT_NO_NEXT)
506 errcode = 0;
507 ext2fs_extent_free(handle);
508 return errcode;
509}
510
511/* Zero out the high bits of inodes. */
512static errcode_t zero_high_bits_in_inodes(ext2_resize_t rfs)
513{
514 ext2_filsys fs = rfs->old_fs;
515 int length = EXT2_INODE_SIZE(fs->super);
516 struct ext2_inode *inode = NULL;
517 ext2_inode_scan scan = NULL;
518 errcode_t retval;
519 ext2_ino_t ino;
520
521 if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
522 return 0;
523
81f965a0 524 if (fs->super->s_creator_os == EXT2_OS_HURD)
fe12931f
DW
525 return 0;
526
527 retval = ext2fs_open_inode_scan(fs, 0, &scan);
528 if (retval)
529 return retval;
530
531 retval = ext2fs_get_mem(length, &inode);
532 if (retval)
533 goto out;
534
535 do {
536 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
537 if (retval)
538 goto out;
539 if (!ino)
540 break;
541 if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino))
542 continue;
543
544 /*
545 * Here's how we deal with high block number fields:
546 *
547 * - i_size_high has been been written out with i_size_lo
548 * since the ext2 days, so no conversion is needed.
549 *
550 * - i_blocks_hi is guarded by both the huge_file feature and
551 * inode flags and has always been written out with
552 * i_blocks_lo if the feature is set. The field is only
553 * ever read if both feature and inode flag are set, so
554 * we don't need to zero it now.
555 *
556 * - i_file_acl_high can be uninitialized, so zero it if
557 * it isn't already.
558 */
559 if (inode->osd2.linux2.l_i_file_acl_high) {
560 inode->osd2.linux2.l_i_file_acl_high = 0;
561 retval = ext2fs_write_inode_full(fs, ino, inode,
562 length);
563 if (retval)
564 goto out;
565 }
566
567 retval = zero_high_bits_in_extents(fs, ino, inode);
568 if (retval)
569 goto out;
570 } while (ino);
571
572out:
573 if (inode)
574 ext2fs_free_mem(&inode);
575 if (scan)
576 ext2fs_close_inode_scan(scan);
577 return retval;
578}
579
86acdebd 580/*
055866d8 581 * Clean up the bitmaps for uninitialized bitmaps
86acdebd
TT
582 */
583static void fix_uninit_block_bitmaps(ext2_filsys fs)
584{
a0ba54ec 585 blk64_t blk, lblk;
86acdebd 586 dgrp_t g;
62f9bd0e 587 unsigned int i;
86acdebd 588
5b58dc23 589 if (!ext2fs_has_group_desc_csum(fs))
86acdebd
TT
590 return;
591
592 for (g=0; g < fs->group_desc_count; g++) {
cd65a24e 593 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
86acdebd
TT
594 continue;
595
5d3a88fb 596 blk = ext2fs_group_first_block2(fs, g);
a0ba54ec
TT
597 lblk = ext2fs_group_last_block2(fs, g);
598 ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
599 lblk - blk + 1);
600
601 ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
602 ext2fs_mark_block_bitmap2(fs->block_map,
603 ext2fs_block_bitmap_loc(fs, g));
604 ext2fs_mark_block_bitmap2(fs->block_map,
605 ext2fs_inode_bitmap_loc(fs, g));
606 for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
62f9bd0e 607 i < fs->inode_blocks_per_group;
a0ba54ec
TT
608 i++, blk++)
609 ext2fs_mark_block_bitmap2(fs->block_map, blk);
86acdebd
TT
610 }
611}
612
a8519a2d
TT
613/* --------------------------------------------------------------------
614 *
615 * Resize processing, phase 1.
616 *
617 * In this phase we adjust the in-memory superblock information, and
618 * initialize any new parts of the inode table. The new parts of the
619 * inode table are created in virgin disk space, so we can abort here
620 * without any side effects.
621 * --------------------------------------------------------------------
622 */
623
9227c5bb
TT
624/*
625 * If the group descriptor's bitmap and inode table blocks are valid,
c09043f1
TT
626 * release them in the new filesystem data structure, and mark them as
627 * reserved so the old inode table blocks don't get overwritten.
9227c5bb 628 */
01d6aa9d
DW
629static errcode_t free_gdp_blocks(ext2_filsys fs,
630 ext2fs_block_bitmap reserve_blocks,
631 ext2_filsys old_fs,
632 dgrp_t group)
9227c5bb 633{
62f9bd0e
TT
634 blk64_t blk;
635 unsigned int j;
636 dgrp_t i;
01d6aa9d 637 ext2fs_block_bitmap bg_map = NULL;
62f9bd0e
TT
638 errcode_t retval = 0;
639 dgrp_t count = old_fs->group_desc_count - fs->group_desc_count;
01d6aa9d
DW
640
641 /* If bigalloc, don't free metadata living in the same cluster */
642 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
643 retval = ext2fs_allocate_block_bitmap(fs, "bgdata", &bg_map);
644 if (retval)
645 goto out;
9227c5bb 646
01d6aa9d
DW
647 retval = mark_table_blocks(fs, bg_map);
648 if (retval)
649 goto out;
c09043f1 650 }
9227c5bb 651
01d6aa9d
DW
652 for (i = group; i < group + count; i++) {
653 blk = ext2fs_block_bitmap_loc(old_fs, i);
654 if (blk &&
655 (blk < ext2fs_blocks_count(fs->super)) &&
656 !(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) {
657 ext2fs_block_alloc_stats2(fs, blk, -1);
658 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
659 }
9227c5bb 660
01d6aa9d
DW
661 blk = ext2fs_inode_bitmap_loc(old_fs, i);
662 if (blk &&
663 (blk < ext2fs_blocks_count(fs->super)) &&
664 !(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) {
665 ext2fs_block_alloc_stats2(fs, blk, -1);
666 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
667 }
9227c5bb 668
01d6aa9d
DW
669 blk = ext2fs_inode_table_loc(old_fs, i);
670 for (j = 0;
671 j < fs->inode_blocks_per_group; j++, blk++) {
672 if (blk >= ext2fs_blocks_count(fs->super) ||
673 (bg_map && ext2fs_test_block_bitmap2(bg_map, blk)))
674 continue;
675 ext2fs_block_alloc_stats2(fs, blk, -1);
676 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
677 }
9227c5bb 678 }
01d6aa9d
DW
679
680out:
681 if (bg_map)
682 ext2fs_free_block_bitmap(bg_map);
683 return retval;
9227c5bb
TT
684}
685
24b2c7a7 686/*
bf69235a
TT
687 * This routine is shared by the online and offline resize routines.
688 * All of the information which is adjusted in memory is done here.
24b2c7a7 689 */
c09043f1 690errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
8728d506 691 ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
24b2c7a7 692{
24b2c7a7 693 errcode_t retval;
8728d506
VAH
694 blk64_t overhead = 0;
695 blk64_t rem;
696 blk64_t blk, group_block;
2696f250 697 blk64_t real_end;
5e27a274
TT
698 blk64_t old_numblocks, numblocks, adjblocks;
699 unsigned long i, j, old_desc_blocks;
54434927 700 unsigned int meta_bg, meta_bg_size;
aaa1ae08 701 int has_super, csum_flag, has_bg;
de8f3a76 702 unsigned long long new_inodes; /* u64 to check for overflow */
9ff8ece5 703 double percent;
bf69235a 704
4efbac6f 705 ext2fs_blocks_count_set(fs->super, new_size);
59037c53 706 fs->super->s_overhead_clusters = 0;
24b2c7a7
TT
707
708retry:
4efbac6f 709 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
69022e02
TT
710 fs->super->s_first_data_block,
711 EXT2_BLOCKS_PER_GROUP(fs->super));
24b2c7a7
TT
712 if (fs->group_desc_count == 0)
713 return EXT2_ET_TOOSMALL;
efc6f628 714 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
69022e02 715 EXT2_DESC_PER_BLOCK(fs->super));
24b2c7a7
TT
716
717 /*
718 * Overhead is the number of bookkeeping blocks per group. It
719 * includes the superblock backup, the group descriptor
720 * backups, the inode bitmap, the block bitmap, and the inode
721 * table.
24b2c7a7 722 */
9213a93b
TT
723 overhead = (int) (2 + fs->inode_blocks_per_group);
724
aaa1ae08
TT
725 has_bg = 0;
726 if (ext2fs_has_feature_sparse_super2(fs->super)) {
727 /*
728 * We have to do this manually since
729 * super->s_backup_bgs hasn't been set up yet.
730 */
731 if (fs->group_desc_count == 2)
732 has_bg = fs->super->s_backup_bgs[0] != 0;
733 else
734 has_bg = fs->super->s_backup_bgs[1] != 0;
735 } else
736 has_bg = ext2fs_bg_has_super(fs, fs->group_desc_count - 1);
737 if (has_bg)
efc6f628 738 overhead += 1 + fs->desc_blocks +
9213a93b
TT
739 fs->super->s_reserved_gdt_blocks;
740
24b2c7a7
TT
741 /*
742 * See if the last group is big enough to support the
743 * necessary data structures. If not, we need to get rid of
744 * it.
745 */
4efbac6f 746 rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
24b2c7a7
TT
747 fs->super->s_blocks_per_group;
748 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
749 return EXT2_ET_TOOSMALL;
515e555a 750 if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
4efbac6f
VAH
751 ext2fs_blocks_count_set(fs->super,
752 ext2fs_blocks_count(fs->super) - rem);
24b2c7a7
TT
753 goto retry;
754 }
755 /*
756 * Adjust the number of inodes
757 */
de8f3a76 758 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
f3358643 759 if (new_inodes > ~0U) {
623985ed
TT
760 new_inodes = (unsigned long long) fs->super->s_inodes_per_group * (fs->group_desc_count - 1);
761 if (new_inodes <= ~0U) {
762 unsigned long long new_blocks =
763 ((unsigned long long) fs->super->s_blocks_per_group *
764 (fs->group_desc_count - 1)) + fs->super->s_first_data_block;
765
766 ext2fs_blocks_count_set(fs->super, new_blocks);
767 goto retry;
768 }
bc5fb433 769 fprintf(stderr, _("inodes (%llu) must be less than %u\n"),
33b9a60c 770 (unsigned long long) new_inodes, ~0U);
f3358643
ES
771 return EXT2_ET_TOO_MANY_INODES;
772 }
24b2c7a7
TT
773 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
774 fs->group_desc_count;
775
776 /*
777 * Adjust the number of free blocks
778 */
4efbac6f
VAH
779 blk = ext2fs_blocks_count(old_fs->super);
780 if (blk > ext2fs_blocks_count(fs->super))
781 ext2fs_free_blocks_count_set(fs->super,
782 ext2fs_free_blocks_count(fs->super) -
783 (blk - ext2fs_blocks_count(fs->super)));
24b2c7a7 784 else
4efbac6f
VAH
785 ext2fs_free_blocks_count_set(fs->super,
786 ext2fs_free_blocks_count(fs->super) +
787 (ext2fs_blocks_count(fs->super) - blk));
24b2c7a7 788
c762c8e6
TT
789 /*
790 * Adjust the number of reserved blocks
791 */
4efbac6f
VAH
792 percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
793 ext2fs_blocks_count(old_fs->super);
794 ext2fs_r_blocks_count_set(fs->super,
795 (percent * ext2fs_blocks_count(fs->super) /
796 100.0));
c762c8e6 797
24b2c7a7
TT
798 /*
799 * Adjust the bitmaps for size
800 */
3346084b 801 retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
24b2c7a7
TT
802 fs->super->s_inodes_count,
803 fs->inode_map);
c762c8e6 804 if (retval) goto errout;
efc6f628 805
1e33a8b4 806 real_end = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count) - 1 +
2696f250 807 fs->super->s_first_data_block;
f026f1a3
TT
808 retval = ext2fs_resize_block_bitmap2(new_size - 1,
809 real_end, fs->block_map);
c762c8e6 810 if (retval) goto errout;
24b2c7a7 811
f026f1a3
TT
812 /*
813 * If we are growing the file system, also grow the size of
814 * the reserve_blocks bitmap
815 */
816 if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) {
817 retval = ext2fs_resize_block_bitmap2(new_size - 1,
818 real_end, reserve_blocks);
819 if (retval) goto errout;
820 }
821
24b2c7a7
TT
822 /*
823 * Reallocate the group descriptors as necessary.
824 */
fe12931f
DW
825 if (EXT2_DESC_SIZE(old_fs->super) == EXT2_DESC_SIZE(fs->super) &&
826 old_fs->desc_blocks != fs->desc_blocks) {
bf69235a 827 retval = ext2fs_resize_mem(old_fs->desc_blocks *
76f875da
TT
828 fs->blocksize,
829 fs->desc_blocks * fs->blocksize,
c4e3d3f3 830 &fs->group_desc);
ca8abba7 831 if (retval)
a8519a2d 832 goto errout;
efc6f628
TT
833 if (fs->desc_blocks > old_fs->desc_blocks)
834 memset((char *) fs->group_desc +
bf69235a
TT
835 (old_fs->desc_blocks * fs->blocksize), 0,
836 (fs->desc_blocks - old_fs->desc_blocks) *
2787276e 837 fs->blocksize);
24b2c7a7 838 }
1e1da29f 839
9213a93b
TT
840 /*
841 * If the resize_inode feature is set, and we are changing the
842 * number of descriptor blocks, then adjust
843 * s_reserved_gdt_blocks if possible to avoid needing to move
844 * the inode table either now or in the future.
fe12931f
DW
845 *
846 * Note: If we're converting to 64bit mode, we did this earlier.
9213a93b 847 */
fe12931f
DW
848 if (EXT2_DESC_SIZE(old_fs->super) == EXT2_DESC_SIZE(fs->super))
849 adjust_reserved_gdt_blocks(old_fs, fs);
9213a93b 850
21e37be6 851 if (ext2fs_has_feature_meta_bg(fs->super) &&
c82815e5 852 (fs->super->s_first_meta_bg > fs->desc_blocks)) {
21e37be6 853 ext2fs_clear_feature_meta_bg(fs->super);
c82815e5
TT
854 fs->super->s_first_meta_bg = 0;
855 }
856
65c6c3e0
TT
857 /*
858 * Update the location of the backup superblocks if the
859 * sparse_super2 feature is enabled.
860 */
21e37be6 861 if (ext2fs_has_feature_sparse_super2(fs->super)) {
65c6c3e0
TT
862 dgrp_t last_bg = fs->group_desc_count - 1;
863 dgrp_t old_last_bg = old_fs->group_desc_count - 1;
864
865 if (last_bg > old_last_bg) {
866 if (old_fs->group_desc_count == 1)
867 fs->super->s_backup_bgs[0] = 1;
aaa1ae08
TT
868 if ((old_fs->group_desc_count < 3 &&
869 fs->group_desc_count > 2) ||
870 fs->super->s_backup_bgs[1])
65c6c3e0
TT
871 fs->super->s_backup_bgs[1] = last_bg;
872 } else if (last_bg < old_last_bg) {
873 if (fs->super->s_backup_bgs[0] > last_bg)
874 fs->super->s_backup_bgs[0] = 0;
875 if (fs->super->s_backup_bgs[1] > last_bg)
876 fs->super->s_backup_bgs[1] = 0;
877 if (last_bg > 1 &&
878 old_fs->super->s_backup_bgs[1] == old_last_bg)
879 fs->super->s_backup_bgs[1] = last_bg;
880 }
881 }
882
a8519a2d 883 /*
c09043f1
TT
884 * If we are shrinking the number of block groups, we're done
885 * and can exit now.
1e1da29f 886 */
bf69235a 887 if (old_fs->group_desc_count > fs->group_desc_count) {
9227c5bb
TT
888 /*
889 * Check the block groups that we are chopping off
890 * and free any blocks associated with their metadata
891 */
01d6aa9d
DW
892 retval = free_gdp_blocks(fs, reserve_blocks, old_fs,
893 fs->group_desc_count);
c762c8e6
TT
894 goto errout;
895 }
bf69235a 896
a8519a2d
TT
897 /*
898 * Fix the count of the last (old) block group
899 */
4efbac6f 900 old_numblocks = (ext2fs_blocks_count(old_fs->super) -
bf69235a
TT
901 old_fs->super->s_first_data_block) %
902 old_fs->super->s_blocks_per_group;
1e1da29f 903 if (!old_numblocks)
bf69235a
TT
904 old_numblocks = old_fs->super->s_blocks_per_group;
905 if (old_fs->group_desc_count == fs->group_desc_count) {
4efbac6f 906 numblocks = (ext2fs_blocks_count(fs->super) -
bf69235a
TT
907 fs->super->s_first_data_block) %
908 fs->super->s_blocks_per_group;
1e1da29f 909 if (!numblocks)
bf69235a 910 numblocks = fs->super->s_blocks_per_group;
1e1da29f 911 } else
bf69235a
TT
912 numblocks = fs->super->s_blocks_per_group;
913 i = old_fs->group_desc_count - 1;
d7cca6b0 914 ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
236efede
JS
915 ext2fs_group_desc_csum_set(fs, i);
916
1e1da29f 917 /*
a8519a2d
TT
918 * If the number of block groups is staying the same, we're
919 * done and can exit now. (If the number block groups is
920 * shrinking, we had exited earlier.)
1e1da29f 921 */
bf69235a 922 if (old_fs->group_desc_count >= fs->group_desc_count) {
c762c8e6
TT
923 retval = 0;
924 goto errout;
925 }
bf69235a 926
a8519a2d
TT
927 /*
928 * Initialize the new block group descriptors
929 */
027b0577
TT
930 group_block = ext2fs_group_first_block2(fs,
931 old_fs->group_desc_count);
5b58dc23 932 csum_flag = ext2fs_has_group_desc_csum(fs);
74430052
TT
933 if (getenv("RESIZE2FS_FORCE_LAZY_ITABLE_INIT") ||
934 (!getenv("RESIZE2FS_FORCE_ITABLE_INIT") &&
935 access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0))
2d8c0c8a 936 lazy_itable_init = 1;
21e37be6 937 if (ext2fs_has_feature_meta_bg(fs->super))
76dd5e5c
TT
938 old_desc_blocks = fs->super->s_first_meta_bg;
939 else
efc6f628 940 old_desc_blocks = fs->desc_blocks +
9213a93b 941 fs->super->s_reserved_gdt_blocks;
f026f1a3
TT
942
943 /*
944 * If we changed the number of block_group descriptor blocks,
945 * we need to make sure they are all marked as reserved in the
055866d8 946 * filesystem's block allocation map.
f026f1a3
TT
947 */
948 for (i = 0; i < old_fs->group_desc_count; i++)
949 ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
950
bf69235a 951 for (i = old_fs->group_desc_count;
1e1da29f 952 i < fs->group_desc_count; i++) {
d7cca6b0 953 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
1e1da29f
TT
954 sizeof(struct ext2_group_desc));
955 adjblocks = 0;
956
e633b58a 957 ext2fs_bg_flags_zap(fs, i);
2d8c0c8a
TT
958 if (csum_flag) {
959 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
960 if (!lazy_itable_init)
961 ext2fs_bg_flags_set(fs, i,
962 EXT2_BG_INODE_ZEROED);
963 ext2fs_bg_itable_unused_set(fs, i,
964 fs->super->s_inodes_per_group);
965 }
98f45471
ES
966
967 numblocks = ext2fs_group_blocks_count(fs, i);
968 if ((i < fs->group_desc_count - 1) && csum_flag)
969 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
1e1da29f 970
76dd5e5c
TT
971 has_super = ext2fs_bg_has_super(fs, i);
972 if (has_super) {
48f23054 973 ext2fs_block_alloc_stats2(fs, group_block, +1);
76dd5e5c
TT
974 adjblocks++;
975 }
f2de1d38 976 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
76dd5e5c 977 meta_bg = i / meta_bg_size;
21e37be6 978 if (!ext2fs_has_feature_meta_bg(fs->super) ||
76dd5e5c
TT
979 (meta_bg < fs->super->s_first_meta_bg)) {
980 if (has_super) {
981 for (j=0; j < old_desc_blocks; j++)
48f23054 982 ext2fs_block_alloc_stats2(fs,
74128f8d 983 group_block + 1 + j, +1);
76dd5e5c
TT
984 adjblocks += old_desc_blocks;
985 }
986 } else {
987 if (has_super)
988 has_super = 1;
989 if (((i % meta_bg_size) == 0) ||
990 ((i % meta_bg_size) == 1) ||
991 ((i % meta_bg_size) == (meta_bg_size-1)))
48f23054 992 ext2fs_block_alloc_stats2(fs,
74128f8d 993 group_block + has_super, +1);
24b2c7a7 994 }
efc6f628 995
1e1da29f 996 adjblocks += 2 + fs->inode_blocks_per_group;
efc6f628 997
1e1da29f 998 numblocks -= adjblocks;
4efbac6f
VAH
999 ext2fs_free_blocks_count_set(fs->super,
1000 ext2fs_free_blocks_count(fs->super) - adjblocks);
1e1da29f
TT
1001 fs->super->s_free_inodes_count +=
1002 fs->super->s_inodes_per_group;
d7cca6b0
VAH
1003 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
1004 ext2fs_bg_free_inodes_count_set(fs, i,
1005 fs->super->s_inodes_per_group);
1006 ext2fs_bg_used_dirs_count_set(fs, i, 0);
236efede 1007 ext2fs_group_desc_csum_set(fs, i);
24b2c7a7 1008
1e1da29f 1009 retval = ext2fs_allocate_group_table(fs, i, 0);
c762c8e6 1010 if (retval) goto errout;
24b2c7a7 1011
bf69235a
TT
1012 group_block += fs->super->s_blocks_per_group;
1013 }
1014 retval = 0;
1015
f026f1a3
TT
1016 /*
1017 * Mark all of the metadata blocks as reserved so they won't
1018 * get allocated by the call to ext2fs_allocate_group_table()
1019 * in blocks_to_move(), where we allocate new blocks to
1020 * replace those allocation bitmap and inode table blocks
1021 * which have to get relocated to make space for an increased
1022 * number of the block group descriptors.
1023 */
1024 if (reserve_blocks)
1025 mark_table_blocks(fs, reserve_blocks);
1026
bf69235a
TT
1027errout:
1028 return (retval);
1029}
1030
1031/*
1032 * This routine adjusts the superblock and other data structures, both
1033 * in disk as well as in memory...
1034 */
8728d506 1035static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
bf69235a 1036{
65c6c3e0 1037 ext2_filsys fs = rfs->new_fs;
bf69235a
TT
1038 int adj = 0;
1039 errcode_t retval;
8728d506 1040 blk64_t group_block;
bf69235a
TT
1041 unsigned long i;
1042 unsigned long max_group;
efc6f628 1043
bf69235a
TT
1044 ext2fs_mark_super_dirty(fs);
1045 ext2fs_mark_bb_dirty(fs);
1046 ext2fs_mark_ib_dirty(fs);
1047
c09043f1
TT
1048 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
1049 &rfs->reserve_blocks);
1050 if (retval)
1051 return retval;
1052
1053 retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
bf69235a
TT
1054 if (retval)
1055 goto errout;
1056
1057 /*
1058 * Check to make sure there are enough inodes
1059 */
1060 if ((rfs->old_fs->super->s_inodes_count -
1061 rfs->old_fs->super->s_free_inodes_count) >
1062 rfs->new_fs->super->s_inodes_count) {
1063 retval = ENOSPC;
1064 goto errout;
1065 }
1066
1067 /*
1068 * If we are shrinking the number block groups, we're done and
1069 * can exit now.
1070 */
1071 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
1072 retval = 0;
1073 goto errout;
1074 }
1075
1076 /*
1077 * If the number of block groups is staying the same, we're
1078 * done and can exit now. (If the number block groups is
1079 * shrinking, we had exited earlier.)
1080 */
1081 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
1082 retval = 0;
1083 goto errout;
1084 }
1085
1086 /*
2d8c0c8a
TT
1087 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
1088 * supports lazy inode initialization, we can skip
1089 * initializing the inode table.
1090 */
22cde009 1091 if (lazy_itable_init && ext2fs_has_group_desc_csum(fs)) {
2d8c0c8a
TT
1092 retval = 0;
1093 goto errout;
1094 }
1095
1096 /*
1097 * Initialize the inode table
bf69235a 1098 */
e5aace90 1099 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
bf69235a
TT
1100 &rfs->itable_buf);
1101 if (retval)
1102 goto errout;
1103
1104 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
027b0577
TT
1105 group_block = ext2fs_group_first_block2(fs,
1106 rfs->old_fs->group_desc_count);
bf69235a
TT
1107 adj = rfs->old_fs->group_desc_count;
1108 max_group = fs->group_desc_count - adj;
1109 if (rfs->progress) {
1110 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
1111 0, max_group);
1112 if (retval)
1113 goto errout;
1114 }
1115 for (i = rfs->old_fs->group_desc_count;
1116 i < fs->group_desc_count; i++) {
05e112a1
TT
1117 /*
1118 * Write out the new inode table
1119 */
08c8e319
DW
1120 retval = ext2fs_zero_blocks2(fs, ext2fs_inode_table_loc(fs, i),
1121 fs->inode_blocks_per_group, NULL,
1122 NULL);
1123 if (retval)
1124 goto errout;
c762c8e6 1125
a8519a2d 1126 io_channel_flush(fs->io);
3b627e8d
TT
1127 if (rfs->progress) {
1128 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
1129 i - adj + 1, max_group);
1130 if (retval)
1131 goto errout;
1132 }
1e1da29f 1133 group_block += fs->super->s_blocks_per_group;
24b2c7a7 1134 }
c762c8e6
TT
1135 io_channel_flush(fs->io);
1136 retval = 0;
1137
1138errout:
c762c8e6
TT
1139 return retval;
1140}
1141
a8519a2d
TT
1142/* --------------------------------------------------------------------
1143 *
1144 * Resize processing, phase 2.
1145 *
1146 * In this phase we adjust determine which blocks need to be moved, in
1147 * blocks_to_move(). We then copy the blocks to their ultimate new
1148 * destinations using block_mover(). Since we are copying blocks to
1149 * their new locations, again during this pass we can abort without
1150 * any problems.
1151 * --------------------------------------------------------------------
1152 */
1153
c762c8e6
TT
1154/*
1155 * This helper function creates a block bitmap with all of the
1156 * filesystem meta-data blocks.
1157 */
1158static errcode_t mark_table_blocks(ext2_filsys fs,
64ad98ac 1159 ext2fs_block_bitmap bmap)
c762c8e6 1160{
54434927 1161 dgrp_t i;
f026f1a3 1162 blk64_t blk;
c762c8e6 1163
c762c8e6 1164 for (i = 0; i < fs->group_desc_count; i++) {
64ad98ac 1165 ext2fs_reserve_super_and_bgd(fs, i, bmap);
efc6f628 1166
c762c8e6
TT
1167 /*
1168 * Mark the blocks used for the inode table
1169 */
f026f1a3
TT
1170 blk = ext2fs_inode_table_loc(fs, i);
1171 if (blk)
1172 ext2fs_mark_block_bitmap_range2(bmap, blk,
1173 fs->inode_blocks_per_group);
efc6f628 1174
c762c8e6 1175 /*
efc6f628 1176 * Mark block used for the block bitmap
c762c8e6 1177 */
f026f1a3
TT
1178 blk = ext2fs_block_bitmap_loc(fs, i);
1179 if (blk)
1180 ext2fs_mark_block_bitmap2(bmap, blk);
64ad98ac 1181
c762c8e6 1182 /*
efc6f628 1183 * Mark block used for the inode bitmap
c762c8e6 1184 */
f026f1a3
TT
1185 blk = ext2fs_inode_bitmap_loc(fs, i);
1186 if (blk)
1187 ext2fs_mark_block_bitmap2(bmap, blk);
c762c8e6 1188 }
3042900a
TT
1189 /* Reserve the MMP block */
1190 if (ext2fs_has_feature_mmp(fs->super) &&
1191 fs->super->s_mmp_block > fs->super->s_first_data_block &&
1192 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1193 ext2fs_mark_block_bitmap2(bmap, fs->super->s_mmp_block);
1e1da29f 1194 return 0;
24b2c7a7
TT
1195}
1196
76dd5e5c
TT
1197/*
1198 * This function checks to see if a particular block (either a
1199 * superblock or a block group descriptor) overlaps with an inode or
1200 * block bitmap block, or with the inode table.
1201 */
1202static void mark_fs_metablock(ext2_resize_t rfs,
1203 ext2fs_block_bitmap meta_bmap,
8728d506 1204 int group, blk64_t blk)
76dd5e5c 1205{
539d3ae3 1206 ext2_filsys fs = rfs->new_fs;
efc6f628 1207
3346084b 1208 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
48f23054 1209 ext2fs_block_alloc_stats2(fs, blk, +1);
76dd5e5c
TT
1210
1211 /*
1212 * Check to see if we overlap with the inode or block bitmap,
1213 * or the inode tables. If not, and the block is in use, then
1214 * mark it as a block to be moved.
1215 */
539d3ae3 1216 if (is_block_bm(fs, group, blk)) {
d7cca6b0 1217 ext2fs_block_bitmap_loc_set(fs, group, 0);
76dd5e5c 1218 rfs->needed_blocks++;
ddcf1dbf
TT
1219 return;
1220 }
539d3ae3 1221 if (is_inode_bm(fs, group, blk)) {
d7cca6b0 1222 ext2fs_inode_bitmap_loc_set(fs, group, 0);
76dd5e5c 1223 rfs->needed_blocks++;
ddcf1dbf
TT
1224 return;
1225 }
539d3ae3 1226 if (is_inode_tb(fs, group, blk)) {
d7cca6b0 1227 ext2fs_inode_table_loc_set(fs, group, 0);
76dd5e5c 1228 rfs->needed_blocks++;
ddcf1dbf
TT
1229 return;
1230 }
21e37be6 1231 if (ext2fs_has_feature_flex_bg(fs->super)) {
ddcf1dbf
TT
1232 dgrp_t i;
1233
539d3ae3
AD
1234 for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
1235 if (is_block_bm(fs, i, blk)) {
ddcf1dbf
TT
1236 ext2fs_block_bitmap_loc_set(fs, i, 0);
1237 rfs->needed_blocks++;
1238 return;
1239 }
539d3ae3 1240 if (is_inode_bm(fs, i, blk)) {
ddcf1dbf
TT
1241 ext2fs_inode_bitmap_loc_set(fs, i, 0);
1242 rfs->needed_blocks++;
1243 return;
1244 }
539d3ae3 1245 if (is_inode_tb(fs, i, blk)) {
ddcf1dbf
TT
1246 ext2fs_inode_table_loc_set(fs, i, 0);
1247 rfs->needed_blocks++;
1248 return;
1249 }
1250 }
1251 }
b9b5e43e
TT
1252
1253 if (ext2fs_has_group_desc_csum(fs) &&
1254 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
86acdebd
TT
1255 /*
1256 * If the block bitmap is uninitialized, which means
1257 * nothing other than standard metadata in use.
1258 */
1259 return;
bb2349c5
TT
1260 } else if (blk < ext2fs_blocks_count(rfs->old_fs->super) &&
1261 ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
3346084b
VAH
1262 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
1263 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
76dd5e5c
TT
1264 rfs->needed_blocks++;
1265 }
1266}
1267
1268
24b2c7a7
TT
1269/*
1270 * This routine marks and unmarks reserved blocks in the new block
1271 * bitmap. It also determines which blocks need to be moved and
1272 * places this information into the move_blocks bitmap.
1273 */
c762c8e6 1274static errcode_t blocks_to_move(ext2_resize_t rfs)
24b2c7a7 1275{
62f9bd0e
TT
1276 unsigned int j;
1277 int has_super;
86acdebd 1278 dgrp_t i, max_groups, g;
8728d506 1279 blk64_t blk, group_blk;
118d3f0b 1280 blk64_t old_blocks, new_blocks, group_end, cluster_freed;
80391dcd 1281 blk64_t new_size;
54434927 1282 unsigned int meta_bg, meta_bg_size;
24b2c7a7 1283 errcode_t retval;
c762c8e6 1284 ext2_filsys fs, old_fs;
118d3f0b 1285 ext2fs_block_bitmap meta_bmap, new_meta_bmap = NULL;
4b04fb30 1286 int flex_bg;
24b2c7a7 1287
c762c8e6
TT
1288 fs = rfs->new_fs;
1289 old_fs = rfs->old_fs;
4efbac6f 1290 if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
c762c8e6 1291 fs = rfs->old_fs;
efc6f628 1292
a13575f4 1293 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
c762c8e6
TT
1294 &rfs->move_blocks);
1295 if (retval)
1296 return retval;
1297
efc6f628 1298 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
64ad98ac
TT
1299 &meta_bmap);
1300 if (retval)
1301 return retval;
efc6f628 1302
64ad98ac 1303 retval = mark_table_blocks(old_fs, meta_bmap);
c762c8e6
TT
1304 if (retval)
1305 return retval;
1306
1307 fs = rfs->new_fs;
efc6f628 1308
80391dcd 1309 /*
e9736a3b
TT
1310 * If we're shrinking the filesystem, we need to move any
1311 * group's metadata blocks (either allocation bitmaps or the
1312 * inode table) which are beyond the end of the new
1313 * filesystem.
80391dcd
ES
1314 */
1315 new_size = ext2fs_blocks_count(fs->super);
1316 if (new_size < ext2fs_blocks_count(old_fs->super)) {
1317 for (g = 0; g < fs->group_desc_count; g++) {
e9736a3b 1318 int realloc = 0;
80391dcd 1319 /*
e9736a3b
TT
1320 * ext2fs_allocate_group_table will re-allocate any
1321 * metadata blocks whose location is set to zero.
80391dcd
ES
1322 */
1323 if (ext2fs_block_bitmap_loc(fs, g) >= new_size) {
1324 ext2fs_block_bitmap_loc_set(fs, g, 0);
e9736a3b 1325 realloc = 1;
80391dcd
ES
1326 }
1327 if (ext2fs_inode_bitmap_loc(fs, g) >= new_size) {
1328 ext2fs_inode_bitmap_loc_set(fs, g, 0);
e9736a3b
TT
1329 realloc = 1;
1330 }
1331 if ((ext2fs_inode_table_loc(fs, g) +
1332 fs->inode_blocks_per_group) > new_size) {
1333 ext2fs_inode_table_loc_set(fs, g, 0);
1334 realloc = 1;
1335 }
1336
1337 if (realloc) {
80391dcd
ES
1338 retval = ext2fs_allocate_group_table(fs, g, 0);
1339 if (retval)
1340 return retval;
1341 }
1342 }
1343 }
1344
1e1da29f
TT
1345 /*
1346 * If we're shrinking the filesystem, we need to move all of
1347 * the blocks that don't fit any more
1348 */
4efbac6f
VAH
1349 for (blk = ext2fs_blocks_count(fs->super);
1350 blk < ext2fs_blocks_count(old_fs->super); blk++) {
6493f8e8 1351 g = ext2fs_group_of_blk2(fs, blk);
5b58dc23 1352 if (ext2fs_has_group_desc_csum(fs) &&
cd65a24e 1353 ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
86acdebd
TT
1354 /*
1355 * The block bitmap is uninitialized, so skip
1356 * to the next block group.
1357 */
5d3a88fb 1358 blk = ext2fs_group_first_block2(fs, g+1) - 1;
86acdebd
TT
1359 continue;
1360 }
3346084b
VAH
1361 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1362 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
1363 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
1e1da29f 1364 rfs->needed_blocks++;
c762c8e6 1365 }
3346084b 1366 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1e1da29f 1367 }
efc6f628 1368
21e37be6 1369 if (ext2fs_has_feature_meta_bg(old_fs->super))
76dd5e5c 1370 old_blocks = old_fs->super->s_first_meta_bg;
c82815e5
TT
1371 else
1372 old_blocks = old_fs->desc_blocks +
1373 old_fs->super->s_reserved_gdt_blocks;
21e37be6 1374 if (ext2fs_has_feature_meta_bg(fs->super))
76dd5e5c 1375 new_blocks = fs->super->s_first_meta_bg;
c82815e5 1376 else
9213a93b 1377 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
efc6f628 1378
65c6c3e0
TT
1379 retval = reserve_sparse_super2_last_group(rfs, meta_bmap);
1380 if (retval)
1381 goto errout;
1382
fe12931f
DW
1383 if (EXT2_DESC_SIZE(rfs->old_fs->super) ==
1384 EXT2_DESC_SIZE(rfs->new_fs->super) &&
1385 old_blocks == new_blocks) {
c762c8e6
TT
1386 retval = 0;
1387 goto errout;
1388 }
24b2c7a7 1389
1333fe93
TT
1390 max_groups = fs->group_desc_count;
1391 if (max_groups > old_fs->group_desc_count)
1392 max_groups = old_fs->group_desc_count;
c762c8e6 1393 group_blk = old_fs->super->s_first_data_block;
24b2c7a7
TT
1394 /*
1395 * If we're reducing the number of descriptor blocks, this
1396 * makes life easy. :-) We just have to mark some extra
1397 * blocks as free.
1398 */
1399 if (old_blocks > new_blocks) {
118d3f0b
DW
1400 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
1401 retval = ext2fs_allocate_block_bitmap(fs,
1402 _("new meta blocks"),
1403 &new_meta_bmap);
1404 if (retval)
1405 goto errout;
1406
1407 retval = mark_table_blocks(fs, new_meta_bmap);
1408 if (retval)
1409 goto errout;
1410 }
1411
1333fe93 1412 for (i = 0; i < max_groups; i++) {
8e98899b 1413 if (!ext2fs_bg_has_super(old_fs, i)) {
1e1da29f 1414 group_blk += fs->super->s_blocks_per_group;
24b2c7a7
TT
1415 continue;
1416 }
118d3f0b
DW
1417 group_end = group_blk + 1 + old_blocks;
1418 for (blk = group_blk + 1 + new_blocks;
1419 blk < group_end;) {
1420 if (new_meta_bmap == NULL ||
1421 !ext2fs_test_block_bitmap2(new_meta_bmap,
1422 blk)) {
1423 cluster_freed =
1424 EXT2FS_CLUSTER_RATIO(fs) -
1425 (blk &
1426 EXT2FS_CLUSTER_MASK(fs));
1427 if (cluster_freed > group_end - blk)
1428 cluster_freed = group_end - blk;
1429 ext2fs_block_alloc_stats2(fs, blk, -1);
1430 blk += EXT2FS_CLUSTER_RATIO(fs);
1431 rfs->needed_blocks -= cluster_freed;
1432 continue;
1433 }
052db4b7 1434 rfs->needed_blocks--;
118d3f0b 1435 blk++;
052db4b7 1436 }
1e1da29f 1437 group_blk += fs->super->s_blocks_per_group;
24b2c7a7 1438 }
c762c8e6
TT
1439 retval = 0;
1440 goto errout;
24b2c7a7
TT
1441 }
1442 /*
1443 * If we're increasing the number of descriptor blocks, life
efc6f628 1444 * gets interesting....
24b2c7a7 1445 */
f2de1d38 1446 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
21e37be6 1447 flex_bg = ext2fs_has_feature_flex_bg(fs->super);
2ebaeb35 1448 /* first reserve all of the existing fs meta blocks */
1333fe93 1449 for (i = 0; i < max_groups; i++) {
76dd5e5c
TT
1450 has_super = ext2fs_bg_has_super(fs, i);
1451 if (has_super)
1452 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
1453
1454 meta_bg = i / meta_bg_size;
21e37be6 1455 if (!ext2fs_has_feature_meta_bg(fs->super) ||
76dd5e5c 1456 (meta_bg < fs->super->s_first_meta_bg)) {
424cb7b6
TT
1457 if (has_super) {
1458 for (blk = group_blk+1;
1459 blk < group_blk + 1 + new_blocks; blk++)
efc6f628 1460 mark_fs_metablock(rfs, meta_bmap,
424cb7b6
TT
1461 i, blk);
1462 }
76dd5e5c
TT
1463 } else {
1464 if (has_super)
1465 has_super = 1;
1466 if (((i % meta_bg_size) == 0) ||
1467 ((i % meta_bg_size) == 1) ||
1468 ((i % meta_bg_size) == (meta_bg_size-1)))
1469 mark_fs_metablock(rfs, meta_bmap, i,
1470 group_blk + has_super);
24b2c7a7 1471 }
76dd5e5c 1472
1e1da29f 1473 /*
c762c8e6
TT
1474 * Reserve the existing meta blocks that we know
1475 * aren't to be moved.
4b04fb30
TT
1476 *
1477 * For flex_bg file systems, in order to avoid
1478 * overwriting fs metadata (especially inode table
1479 * blocks) belonging to a different block group when
1480 * we are relocating the inode tables, we need to
1481 * reserve all existing fs metadata blocks.
1e1da29f 1482 */
d7cca6b0 1483 if (ext2fs_block_bitmap_loc(fs, i))
3346084b 1484 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
d7cca6b0 1485 ext2fs_block_bitmap_loc(fs, i));
4b04fb30
TT
1486 else if (flex_bg && i < old_fs->group_desc_count)
1487 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1488 ext2fs_block_bitmap_loc(old_fs, i));
1489
d7cca6b0 1490 if (ext2fs_inode_bitmap_loc(fs, i))
3346084b 1491 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
d7cca6b0 1492 ext2fs_inode_bitmap_loc(fs, i));
4b04fb30
TT
1493 else if (flex_bg && i < old_fs->group_desc_count)
1494 ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1495 ext2fs_inode_bitmap_loc(old_fs, i));
1496
d7cca6b0 1497 if (ext2fs_inode_table_loc(fs, i))
a0ba54ec
TT
1498 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1499 ext2fs_inode_table_loc(fs, i),
1500 fs->inode_blocks_per_group);
4b04fb30 1501 else if (flex_bg && i < old_fs->group_desc_count)
a0ba54ec
TT
1502 ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1503 ext2fs_inode_table_loc(old_fs, i),
1504 old_fs->inode_blocks_per_group);
24b2c7a7 1505
2ebaeb35
TT
1506 group_blk += rfs->new_fs->super->s_blocks_per_group;
1507 }
1508
1509 /* Allocate the missing data structures */
1510 for (i = 0; i < max_groups; i++) {
1511 if (ext2fs_inode_table_loc(fs, i) &&
1512 ext2fs_inode_bitmap_loc(fs, i) &&
1513 ext2fs_block_bitmap_loc(fs, i))
1514 continue;
24b2c7a7 1515
1e1da29f
TT
1516 retval = ext2fs_allocate_group_table(fs, i,
1517 rfs->reserve_blocks);
1518 if (retval)
c762c8e6 1519 goto errout;
24b2c7a7 1520
1e1da29f 1521 /*
c762c8e6 1522 * For those structures that have changed, we need to
055866d8 1523 * do bookkeeping.
1e1da29f 1524 */
d7cca6b0
VAH
1525 if (ext2fs_block_bitmap_loc(old_fs, i) !=
1526 (blk = ext2fs_block_bitmap_loc(fs, i))) {
48f23054 1527 ext2fs_block_alloc_stats2(fs, blk, +1);
bb2349c5
TT
1528 if (blk < ext2fs_blocks_count(old_fs->super) &&
1529 ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
3346084b
VAH
1530 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1531 ext2fs_mark_block_bitmap2(rfs->move_blocks,
c762c8e6
TT
1532 blk);
1533 }
d7cca6b0
VAH
1534 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
1535 (blk = ext2fs_inode_bitmap_loc(fs, i))) {
48f23054 1536 ext2fs_block_alloc_stats2(fs, blk, +1);
bb2349c5
TT
1537 if (blk < ext2fs_blocks_count(old_fs->super) &&
1538 ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
3346084b
VAH
1539 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1540 ext2fs_mark_block_bitmap2(rfs->move_blocks,
c762c8e6
TT
1541 blk);
1542 }
24b2c7a7 1543
052db4b7
TT
1544 /*
1545 * The inode table, if we need to relocate it, is
1546 * handled specially. We have to reserve the blocks
1547 * for both the old and the new inode table, since we
1548 * can't have the inode table be destroyed during the
1549 * block relocation phase.
1550 */
d7cca6b0 1551 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
2ebaeb35 1552 continue; /* inode table not moved */
052db4b7 1553
c762c8e6 1554 rfs->needed_blocks += fs->inode_blocks_per_group;
052db4b7
TT
1555
1556 /*
1557 * Mark the new inode table as in use in the new block
efc6f628 1558 * allocation bitmap, and move any blocks that might
c762c8e6 1559 * be necessary.
052db4b7 1560 */
d7cca6b0 1561 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
c762c8e6 1562 j < fs->inode_blocks_per_group ; j++, blk++) {
48f23054 1563 ext2fs_block_alloc_stats2(fs, blk, +1);
bb2349c5
TT
1564 if (blk < ext2fs_blocks_count(old_fs->super) &&
1565 ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
3346084b
VAH
1566 !ext2fs_test_block_bitmap2(meta_bmap, blk))
1567 ext2fs_mark_block_bitmap2(rfs->move_blocks,
c762c8e6
TT
1568 blk);
1569 }
efc6f628 1570
1e1da29f 1571 /*
052db4b7
TT
1572 * Make sure the old inode table is reserved in the
1573 * block reservation bitmap.
1e1da29f 1574 */
d7cca6b0 1575 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
052db4b7 1576 j < fs->inode_blocks_per_group ; j++, blk++)
3346084b 1577 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1e1da29f 1578 }
c762c8e6
TT
1579 retval = 0;
1580
1581errout:
118d3f0b
DW
1582 if (new_meta_bmap)
1583 ext2fs_free_block_bitmap(new_meta_bmap);
c762c8e6
TT
1584 if (meta_bmap)
1585 ext2fs_free_block_bitmap(meta_bmap);
efc6f628 1586
c762c8e6 1587 return retval;
1e1da29f 1588}
24b2c7a7 1589
a8519a2d
TT
1590/*
1591 * This helper function tries to allocate a new block. We try to
1592 * avoid hitting the original group descriptor blocks at least at
1593 * first, since we want to make it possible to recover from a badly
1594 * aborted resize operation as much as possible.
1595 *
1596 * In the future, I may further modify this routine to balance out
1597 * where we get the new blocks across the various block groups.
1598 * Ideally we would allocate blocks that corresponded with the block
1599 * group of the containing inode, and keep contiguous blocks
1600 * together. However, this very difficult to do efficiently, since we
1601 * don't have the necessary information up front.
1602 */
1603
1604#define AVOID_OLD 1
1605#define DESPERATION 2
1606
1607static void init_block_alloc(ext2_resize_t rfs)
1608{
1609 rfs->alloc_state = AVOID_OLD;
1610 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
2bc4d4f7
TT
1611#if 0
1612 /* HACK for testing */
4efbac6f
VAH
1613 if (ext2fs_blocks_count(rfs->new_fs->super) >
1614 ext2fs_blocks_count(rfs->old_fs->super))
1615 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
2bc4d4f7 1616#endif
a8519a2d
TT
1617}
1618
8728d506 1619static blk64_t get_new_block(ext2_resize_t rfs)
a8519a2d
TT
1620{
1621 ext2_filsys fs = rfs->new_fs;
efc6f628 1622
a8519a2d 1623 while (1) {
4efbac6f 1624 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
a8519a2d
TT
1625 if (rfs->alloc_state == DESPERATION)
1626 return 0;
1627
1628#ifdef RESIZE2FS_DEBUG
1629 if (rfs->flags & RESIZE_DEBUG_BMOVE)
f35fd3d5
TT
1630 printf("Going into desperation mode "
1631 "for block allocations\n");
efc6f628 1632#endif
a8519a2d
TT
1633 rfs->alloc_state = DESPERATION;
1634 rfs->new_blk = fs->super->s_first_data_block;
1635 continue;
1636 }
3346084b
VAH
1637 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1638 ext2fs_test_block_bitmap2(rfs->reserve_blocks,
a8519a2d
TT
1639 rfs->new_blk) ||
1640 ((rfs->alloc_state == AVOID_OLD) &&
4efbac6f 1641 (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
3346084b 1642 ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
a8519a2d
TT
1643 rfs->new_blk))) {
1644 rfs->new_blk++;
1645 continue;
1646 }
1647 return rfs->new_blk;
1648 }
1649}
1650
25f291c9
TT
1651static errcode_t resize2fs_get_alloc_block(ext2_filsys fs,
1652 blk64_t goal EXT2FS_ATTR((unused)),
7f9c96ee
TT
1653 blk64_t *ret)
1654{
1655 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
8728d506 1656 blk64_t blk;
f3745728 1657 int group;
7f9c96ee
TT
1658
1659 blk = get_new_block(rfs);
1660 if (!blk)
1661 return ENOSPC;
1662
1663#ifdef RESIZE2FS_DEBUG
1664 if (rfs->flags & 0xF)
33b9a60c
TT
1665 printf("get_alloc_block allocating %llu\n",
1666 (unsigned long long) blk);
7f9c96ee
TT
1667#endif
1668
3346084b
VAH
1669 ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1670 ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
f3745728
ES
1671
1672 group = ext2fs_group_of_blk2(rfs->old_fs, blk);
1673 ext2fs_clear_block_uninit(rfs->old_fs, group);
1674 group = ext2fs_group_of_blk2(rfs->new_fs, blk);
1675 ext2fs_clear_block_uninit(rfs->new_fs, group);
1676
7f9c96ee
TT
1677 *ret = (blk64_t) blk;
1678 return 0;
1679}
1680
a8519a2d
TT
1681static errcode_t block_mover(ext2_resize_t rfs)
1682{
8728d506 1683 blk64_t blk, old_blk, new_blk;
a8519a2d
TT
1684 ext2_filsys fs = rfs->new_fs;
1685 ext2_filsys old_fs = rfs->old_fs;
1686 errcode_t retval;
62f9bd0e 1687 __u64 c, size;
a8519a2d 1688 int to_move, moved;
7d7bdd57
TT
1689 ext2_badblocks_list badblock_list = 0;
1690 int bb_modified = 0;
efc6f628 1691
7f9c96ee
TT
1692 fs->get_alloc_block = resize2fs_get_alloc_block;
1693 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1694
7d7bdd57
TT
1695 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1696 if (retval)
1697 return retval;
a8519a2d
TT
1698
1699 new_blk = fs->super->s_first_data_block;
1700 if (!rfs->itable_buf) {
e5aace90 1701 retval = ext2fs_get_array(fs->blocksize,
a8519a2d 1702 fs->inode_blocks_per_group,
c4e3d3f3 1703 &rfs->itable_buf);
a8519a2d
TT
1704 if (retval)
1705 return retval;
1706 }
1707 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1708 if (retval)
1709 return retval;
1710
1711 /*
1712 * The first step is to figure out where all of the blocks
1713 * will go.
1714 */
1715 to_move = moved = 0;
1716 init_block_alloc(rfs);
d1a1a583
TT
1717 for (blk = B2C(old_fs->super->s_first_data_block);
1718 blk < ext2fs_blocks_count(old_fs->super);
1719 blk += EXT2FS_CLUSTER_RATIO(fs)) {
3346084b 1720 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
a8519a2d 1721 continue;
3346084b 1722 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
a8519a2d 1723 continue;
7d7bdd57
TT
1724 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1725 ext2fs_badblocks_list_del(badblock_list, blk);
1726 bb_modified++;
1727 continue;
1728 }
a8519a2d
TT
1729
1730 new_blk = get_new_block(rfs);
1731 if (!new_blk) {
1732 retval = ENOSPC;
1733 goto errout;
1734 }
48f23054 1735 ext2fs_block_alloc_stats2(fs, new_blk, +1);
d1a1a583 1736 ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk));
a8519a2d
TT
1737 to_move++;
1738 }
efc6f628 1739
a8519a2d 1740 if (to_move == 0) {
cefbf487
TT
1741 if (rfs->bmap) {
1742 ext2fs_free_extent_table(rfs->bmap);
1743 rfs->bmap = 0;
1744 }
a8519a2d
TT
1745 retval = 0;
1746 goto errout;
1747 }
1748
1749 /*
1750 * Step two is to actually move the blocks
1751 */
1752 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1753 if (retval) goto errout;
1754
3b627e8d
TT
1755 if (rfs->progress) {
1756 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1757 0, to_move);
1758 if (retval)
1759 goto errout;
1760 }
a8519a2d
TT
1761 while (1) {
1762 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1763 if (retval) goto errout;
1764 if (!size)
1765 break;
d1a1a583
TT
1766 old_blk = C2B(old_blk);
1767 new_blk = C2B(new_blk);
1768 size = C2B(size);
a8519a2d
TT
1769#ifdef RESIZE2FS_DEBUG
1770 if (rfs->flags & RESIZE_DEBUG_BMOVE)
8728d506 1771 printf("Moving %llu blocks %llu->%llu\n",
33b9a60c
TT
1772 (unsigned long long) size,
1773 (unsigned long long) old_blk,
1774 (unsigned long long) new_blk);
a8519a2d
TT
1775#endif
1776 do {
1777 c = size;
1778 if (c > fs->inode_blocks_per_group)
1779 c = fs->inode_blocks_per_group;
24a117ab
VAH
1780 retval = io_channel_read_blk64(fs->io, old_blk, c,
1781 rfs->itable_buf);
a8519a2d 1782 if (retval) goto errout;
24a117ab
VAH
1783 retval = io_channel_write_blk64(fs->io, new_blk, c,
1784 rfs->itable_buf);
a8519a2d
TT
1785 if (retval) goto errout;
1786 size -= c;
1787 new_blk += c;
1788 old_blk += c;
1789 moved += c;
1790 if (rfs->progress) {
1791 io_channel_flush(fs->io);
3b627e8d
TT
1792 retval = (rfs->progress)(rfs,
1793 E2_RSZ_BLOCK_RELOC_PASS,
a8519a2d 1794 moved, to_move);
3b627e8d
TT
1795 if (retval)
1796 goto errout;
a8519a2d
TT
1797 }
1798 } while (size > 0);
1799 io_channel_flush(fs->io);
1800 }
1801
1802errout:
7d7bdd57
TT
1803 if (badblock_list) {
1804 if (!retval && bb_modified)
1805 retval = ext2fs_update_bb_inode(old_fs,
1806 badblock_list);
1807 ext2fs_badblocks_list_free(badblock_list);
1808 }
a8519a2d
TT
1809 return retval;
1810}
1811
1812
1813/* --------------------------------------------------------------------
1814 *
1815 * Resize processing, phase 3
1816 *
1817 * --------------------------------------------------------------------
1818 */
1819
1820
d1a1a583
TT
1821/*
1822 * The extent translation table is stored in clusters so we need to
1823 * take special care when mapping a source block number to its
1824 * destination block number.
1825 */
f404167d 1826static __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc)
d1a1a583
TT
1827{
1828 __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc)));
1829
1830 if (new_block != 0)
1831 new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1);
1832 return new_block;
1833}
1834
a8519a2d
TT
1835struct process_block_struct {
1836 ext2_resize_t rfs;
dfcdc32f 1837 ext2_ino_t ino;
fb47b94f 1838 ext2_ino_t old_ino;
a8519a2d
TT
1839 struct ext2_inode * inode;
1840 errcode_t error;
1841 int is_dir;
1842 int changed;
fb47b94f 1843 int has_extents;
a8519a2d
TT
1844};
1845
8728d506 1846static int process_block(ext2_filsys fs, blk64_t *block_nr,
efc6f628 1847 e2_blkcnt_t blockcnt,
8728d506 1848 blk64_t ref_block EXT2FS_ATTR((unused)),
54434927 1849 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
a8519a2d
TT
1850{
1851 struct process_block_struct *pb;
1852 errcode_t retval;
8728d506 1853 blk64_t block, new_block;
a8519a2d
TT
1854 int ret = 0;
1855
1856 pb = (struct process_block_struct *) priv_data;
1857 block = *block_nr;
1858 if (pb->rfs->bmap) {
d1a1a583 1859 new_block = extent_translate(fs, pb->rfs->bmap, block);
a8519a2d
TT
1860 if (new_block) {
1861 *block_nr = new_block;
1862 ret |= BLOCK_CHANGED;
1863 pb->changed = 1;
1864#ifdef RESIZE2FS_DEBUG
1865 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
8728d506 1866 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
33b9a60c
TT
1867 pb->old_ino, (long long) blockcnt,
1868 (unsigned long long) block,
1869 (unsigned long long) new_block);
a8519a2d
TT
1870#endif
1871 block = new_block;
1872 }
1873 }
fb47b94f 1874
a8519a2d 1875 if (pb->is_dir) {
8728d506
VAH
1876 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1877 block, (int) blockcnt);
a8519a2d
TT
1878 if (retval) {
1879 pb->error = retval;
1880 ret |= BLOCK_ABORT;
1881 }
1882 }
1883 return ret;
1884}
1885
1886/*
1887 * Progress callback
1888 */
efc6f628 1889static errcode_t progress_callback(ext2_filsys fs,
54434927 1890 ext2_inode_scan scan EXT2FS_ATTR((unused)),
a8519a2d
TT
1891 dgrp_t group, void * priv_data)
1892{
1893 ext2_resize_t rfs = (ext2_resize_t) priv_data;
3b627e8d 1894 errcode_t retval;
a8519a2d
TT
1895
1896 /*
f4b2a6db
TT
1897 * This check is to protect against old ext2 libraries. It
1898 * shouldn't be needed against new libraries.
a8519a2d 1899 */
f4b2a6db 1900 if ((group+1) == 0)
a8519a2d
TT
1901 return 0;
1902
1903 if (rfs->progress) {
1904 io_channel_flush(fs->io);
3b627e8d
TT
1905 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1906 group+1, fs->group_desc_count);
1907 if (retval)
1908 return retval;
a8519a2d 1909 }
efc6f628 1910
a8519a2d
TT
1911 return 0;
1912}
1913
fb47b94f
DW
1914static errcode_t migrate_ea_block(ext2_resize_t rfs, ext2_ino_t ino,
1915 struct ext2_inode *inode, int *changed)
1916{
180f376b 1917 char *buf = NULL;
fb47b94f
DW
1918 blk64_t new_block;
1919 errcode_t err = 0;
1920
1921 /* No EA block or no remapping? Quit early. */
3d6fc974 1922 if (ext2fs_file_acl_block(rfs->old_fs, inode) == 0 || !rfs->bmap)
fb47b94f
DW
1923 return 0;
1924 new_block = extent_translate(rfs->old_fs, rfs->bmap,
1925 ext2fs_file_acl_block(rfs->old_fs, inode));
1926 if (new_block == 0)
1927 return 0;
1928
1929 /* Set the new ACL block */
1930 ext2fs_file_acl_block_set(rfs->old_fs, inode, new_block);
1931
1932 /* Update checksum */
21e37be6 1933 if (ext2fs_has_feature_metadata_csum(rfs->new_fs->super)) {
fb47b94f
DW
1934 err = ext2fs_get_mem(rfs->old_fs->blocksize, &buf);
1935 if (err)
1936 return err;
1937 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1938 err = ext2fs_read_ext_attr3(rfs->old_fs, new_block, buf, ino);
1939 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1940 if (err)
1941 goto out;
1942 err = ext2fs_write_ext_attr3(rfs->old_fs, new_block, buf, ino);
1943 if (err)
1944 goto out;
1945 }
1946 *changed = 1;
1947
1948out:
1949 ext2fs_free_mem(&buf);
1950 return err;
1951}
1952
25f291c9
TT
1953static void quiet_com_err_proc(const char *whoami EXT2FS_ATTR((unused)),
1954 errcode_t code EXT2FS_ATTR((unused)),
1955 const char *fmt EXT2FS_ATTR((unused)),
1956 va_list args EXT2FS_ATTR((unused)))
538ef363
DW
1957{
1958}
1959
5906594b
TE
1960static int fix_ea_entries(ext2_extent imap, struct ext2_ext_attr_entry *entry,
1961 struct ext2_ext_attr_entry *end, ext2_ino_t last_ino)
1962{
1963 int modified = 0;
1964 ext2_ino_t new_ino;
5906594b
TE
1965
1966 while (entry < end && !EXT2_EXT_IS_LAST_ENTRY(entry)) {
1967 if (entry->e_value_inum > last_ino) {
1968 new_ino = ext2fs_extent_translate(imap,
1969 entry->e_value_inum);
1970 entry->e_value_inum = new_ino;
1971 modified = 1;
1972 }
1973 entry = EXT2_EXT_ATTR_NEXT(entry);
1974 }
1975 return modified;
1976}
1977
1978static int fix_ea_ibody_entries(ext2_extent imap,
1979 struct ext2_inode_large *inode, int inode_size,
1980 ext2_ino_t last_ino)
1981{
1982 struct ext2_ext_attr_entry *start, *end;
1983 __u32 *ea_magic;
1984
1985 if (inode->i_extra_isize == 0)
1986 return 0;
1987
1988 ea_magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
1989 inode->i_extra_isize);
1990 if (*ea_magic != EXT2_EXT_ATTR_MAGIC)
1991 return 0;
1992
1993 start = (struct ext2_ext_attr_entry *)(ea_magic + 1);
1994 end = (struct ext2_ext_attr_entry *)((char *)inode + inode_size);
1995
1996 return fix_ea_entries(imap, start, end, last_ino);
1997}
1998
1999static int fix_ea_block_entries(ext2_extent imap, char *block_buf,
2000 unsigned int blocksize, ext2_ino_t last_ino)
2001{
2002 struct ext2_ext_attr_header *header;
2003 struct ext2_ext_attr_entry *start, *end;
2004
2005 header = (struct ext2_ext_attr_header *)block_buf;
2006 start = (struct ext2_ext_attr_entry *)(header+1);
2007 end = (struct ext2_ext_attr_entry *)(block_buf + blocksize);
2008
2009 return fix_ea_entries(imap, start, end, last_ino);
2010}
2011
2012/* A simple LRU cache to check recently processed blocks. */
2013struct blk_cache {
2014 int cursor;
2015 blk64_t blks[4];
2016};
2017
2018#define BLK_IN_CACHE(b,c) ((b) == (c).blks[0] || (b) == (c).blks[1] || \
2019 (b) == (c).blks[2] || (b) == (c).blks[3])
2020#define BLK_ADD_CACHE(b,c) { \
2021 (c).blks[(c).cursor] = (b); \
2022 (c).cursor = ((c).cursor + 1) % 4; \
2023}
2024
2025static errcode_t fix_ea_inode_refs(ext2_resize_t rfs, struct ext2_inode *inode,
2026 char *block_buf, ext2_ino_t last_ino)
2027{
2028 ext2_filsys fs = rfs->new_fs;
2029 ext2_inode_scan scan = NULL;
2030 ext2_ino_t ino;
2031 int inode_size = EXT2_INODE_SIZE(fs->super);
2032 blk64_t blk;
2033 int modified;
ac3256fd 2034 struct blk_cache blk_cache;
5906594b
TE
2035 struct ext2_ext_attr_header *header;
2036 errcode_t retval;
2037
ac3256fd
TT
2038 memset(&blk_cache, 0, sizeof(blk_cache));
2039
5906594b
TE
2040 header = (struct ext2_ext_attr_header *)block_buf;
2041
2042 retval = ext2fs_open_inode_scan(fs, 0, &scan);
2043 if (retval)
2044 goto out;
2045
2046 while (1) {
2047 retval = ext2fs_get_next_inode_full(scan, &ino, inode,
2048 inode_size);
2049 if (retval)
2050 goto out;
2051 if (!ino)
2052 break;
2053
2054 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
2055 continue; /* inode not in use */
2056
2057 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE) {
2058 modified = fix_ea_ibody_entries(rfs->imap,
2059 (struct ext2_inode_large *)inode,
2060 inode_size, last_ino);
2061 if (modified) {
2062 retval = ext2fs_write_inode_full(fs, ino, inode,
2063 inode_size);
2064 if (retval)
2065 goto out;
2066 }
2067 }
2068
2069 blk = ext2fs_file_acl_block(fs, inode);
2070 if (blk && !BLK_IN_CACHE(blk, blk_cache)) {
2071 retval = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
2072 if (retval)
2073 goto out;
2074
2075 modified = fix_ea_block_entries(rfs->imap, block_buf,
2076 fs->blocksize,
2077 last_ino);
2078 if (modified) {
2079 retval = ext2fs_write_ext_attr3(fs, blk,
2080 block_buf, ino);
2081 if (retval)
2082 goto out;
2083 /*
2084 * If refcount is greater than 1, we might see
2085 * the same block referenced by other inodes
2086 * later.
2087 */
2088 if (header->h_refcount > 1)
2089 BLK_ADD_CACHE(blk, blk_cache);
2090 }
2091 }
2092 }
2093 retval = 0;
2094out:
2095 if (scan)
2096 ext2fs_close_inode_scan(scan);
2097 return retval;
2098
2099}
a8519a2d
TT
2100static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
2101{
2102 struct process_block_struct pb;
dfcdc32f 2103 ext2_ino_t ino, new_inode;
edfd9b0a 2104 struct ext2_inode *inode = NULL;
a8519a2d
TT
2105 ext2_inode_scan scan = NULL;
2106 errcode_t retval;
a8519a2d 2107 char *block_buf = 0;
dfcdc32f 2108 ext2_ino_t start_to_move;
4ef28824 2109 int inode_size;
5906594b 2110 int update_ea_inode_refs = 0;
efc6f628 2111
a8519a2d
TT
2112 if ((rfs->old_fs->group_desc_count <=
2113 rfs->new_fs->group_desc_count) &&
2114 !rfs->bmap)
2115 return 0;
2116
538ef363 2117 set_com_err_hook(quiet_com_err_proc);
2bc4d4f7 2118
a8519a2d
TT
2119 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
2120 if (retval) goto errout;
2121
2122 retval = ext2fs_init_dblist(rfs->old_fs, 0);
2123 if (retval) goto errout;
e5aace90 2124 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
a8519a2d
TT
2125 if (retval) goto errout;
2126
2127 start_to_move = (rfs->new_fs->group_desc_count *
2128 rfs->new_fs->super->s_inodes_per_group);
efc6f628 2129
3b627e8d
TT
2130 if (rfs->progress) {
2131 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
2132 0, rfs->old_fs->group_desc_count);
2133 if (retval)
2134 goto errout;
2135 }
a8519a2d
TT
2136 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
2137 pb.rfs = rfs;
edfd9b0a 2138 pb.inode = inode;
a8519a2d
TT
2139 pb.error = 0;
2140 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
4ef28824 2141 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
edfd9b0a
TT
2142 inode = malloc(inode_size);
2143 if (!inode) {
4ef28824
ES
2144 retval = ENOMEM;
2145 goto errout;
2146 }
a8519a2d
TT
2147 /*
2148 * First, copy all of the inodes that need to be moved
2149 * elsewhere in the inode table
2150 */
2151 while (1) {
edfd9b0a 2152 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
a8519a2d
TT
2153 if (retval) goto errout;
2154 if (!ino)
2155 break;
2156
edfd9b0a 2157 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
a8519a2d
TT
2158 continue; /* inode not in use */
2159
edfd9b0a 2160 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
a8519a2d
TT
2161 pb.changed = 0;
2162
fb47b94f
DW
2163 /* Remap EA block */
2164 retval = migrate_ea_block(rfs, ino, inode, &pb.changed);
2165 if (retval)
2166 goto errout;
a8519a2d 2167
fb47b94f 2168 new_inode = ino;
a8519a2d 2169 if (ino <= start_to_move)
fb47b94f 2170 goto remap_blocks; /* Don't need to move inode. */
a8519a2d
TT
2171
2172 /*
fb47b94f
DW
2173 * Find a new inode. Now that extents and directory blocks
2174 * are tied to the inode number through the checksum, we must
2175 * set up the new inode before we start rewriting blocks.
a8519a2d 2176 */
86acdebd
TT
2177 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
2178 if (retval)
2179 goto errout;
2180
74128f8d
TT
2181 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
2182 pb.is_dir);
5906594b
TE
2183 /*
2184 * i_ctime field in xattr inodes contain a portion of the ref
2185 * count, do not overwrite.
2186 */
2187 if (inode->i_flags & EXT4_EA_INODE_FL)
2188 update_ea_inode_refs = 1;
2189 else
2190 inode->i_ctime = time(0);
2191
4ef28824 2192 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
edfd9b0a 2193 inode, inode_size);
fb47b94f
DW
2194 if (retval)
2195 goto errout;
2196 pb.changed = 0;
a8519a2d 2197
a8519a2d
TT
2198#ifdef RESIZE2FS_DEBUG
2199 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
f35fd3d5 2200 printf("Inode moved %u->%u\n", ino, new_inode);
a8519a2d
TT
2201#endif
2202 if (!rfs->imap) {
2203 retval = ext2fs_create_extent_table(&rfs->imap, 0);
2204 if (retval)
2205 goto errout;
2206 }
2207 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
fb47b94f
DW
2208
2209remap_blocks:
2210 if (pb.changed)
2211 retval = ext2fs_write_inode_full(rfs->old_fs,
2212 new_inode,
2213 inode, inode_size);
2214 if (retval)
2215 goto errout;
2216
2217 /*
2218 * Update inodes to point to new blocks; schedule directory
2219 * blocks for inode remapping. Need to write out dir blocks
2220 * with new inode numbers if we have metadata_csum enabled.
2221 */
4b303813 2222 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
fb47b94f
DW
2223 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
2224 (rfs->bmap || pb.is_dir)) {
2225 pb.ino = new_inode;
2226 pb.old_ino = ino;
2227 pb.has_extents = inode->i_flags & EXT4_EXTENTS_FL;
fb47b94f
DW
2228 retval = ext2fs_block_iterate3(rfs->old_fs,
2229 new_inode, 0, block_buf,
2230 process_block, &pb);
fb47b94f
DW
2231 if (retval)
2232 goto errout;
2233 if (pb.error) {
2234 retval = pb.error;
2235 goto errout;
2236 }
d05c9c7a
DW
2237 } else if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
2238 (rfs->bmap || pb.is_dir)) {
2239 /* inline data dir; update it too */
2240 retval = ext2fs_add_dir_block2(rfs->old_fs->dblist,
2241 new_inode, 0, 0);
2242 if (retval)
2243 goto errout;
fb47b94f 2244 }
4b303813
TT
2245
2246 /* Fix up extent block checksums with the new inode number */
2247 if (ext2fs_has_feature_metadata_csum(rfs->old_fs->super) &&
2248 (inode->i_flags & EXT4_EXTENTS_FL)) {
ae9c0f36
TT
2249 retval = ext2fs_fix_extents_checksums(rfs->old_fs,
2250 new_inode, NULL);
4b303813
TT
2251 if (retval)
2252 goto errout;
2253 }
a8519a2d 2254 }
5906594b
TE
2255
2256 if (update_ea_inode_refs &&
2257 ext2fs_has_feature_ea_inode(rfs->new_fs->super)) {
2258 retval = fix_ea_inode_refs(rfs, inode, block_buf,
2259 start_to_move);
2260 if (retval)
2261 goto errout;
2262 }
a8519a2d
TT
2263 io_channel_flush(rfs->old_fs->io);
2264
2265errout:
538ef363 2266 reset_com_err_hook();
4b303813 2267 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
a8519a2d
TT
2268 if (rfs->bmap) {
2269 ext2fs_free_extent_table(rfs->bmap);
2270 rfs->bmap = 0;
2271 }
2272 if (scan)
2273 ext2fs_close_inode_scan(scan);
2274 if (block_buf)
c4e3d3f3 2275 ext2fs_free_mem(&block_buf);
45e338f5 2276 free(inode);
a8519a2d
TT
2277 return retval;
2278}
2279
2280/* --------------------------------------------------------------------
2281 *
2282 * Resize processing, phase 4.
2283 *
2284 * --------------------------------------------------------------------
2285 */
2286
2287struct istruct {
2288 ext2_resize_t rfs;
3b627e8d 2289 errcode_t err;
03fa6f8a
TT
2290 unsigned int max_dirs;
2291 unsigned int num;
a8519a2d
TT
2292};
2293
efc6f628 2294static int check_and_change_inodes(ext2_ino_t dir,
54434927 2295 int entry EXT2FS_ATTR((unused)),
a8519a2d 2296 struct ext2_dir_entry *dirent, int offset,
54434927 2297 int blocksize EXT2FS_ATTR((unused)),
efc6f628 2298 char *buf EXT2FS_ATTR((unused)),
54434927 2299 void *priv_data)
a8519a2d
TT
2300{
2301 struct istruct *is = (struct istruct *) priv_data;
085d2a83
TT
2302 struct ext2_inode inode;
2303 ext2_ino_t new_inode;
2304 errcode_t retval;
fb47b94f 2305 int ret = 0;
a8519a2d
TT
2306
2307 if (is->rfs->progress && offset == 0) {
2308 io_channel_flush(is->rfs->old_fs->io);
3b627e8d
TT
2309 is->err = (is->rfs->progress)(is->rfs,
2310 E2_RSZ_INODE_REF_UPD_PASS,
1333fe93 2311 ++is->num, is->max_dirs);
3b627e8d
TT
2312 if (is->err)
2313 return DIRENT_ABORT;
a8519a2d
TT
2314 }
2315
fb47b94f
DW
2316 /*
2317 * If we have checksums enabled and the inode wasn't present in the
2318 * old fs, then we must rewrite all dir blocks with new checksums.
2319 */
21e37be6 2320 if (ext2fs_has_feature_metadata_csum(is->rfs->old_fs->super) &&
fb47b94f
DW
2321 !ext2fs_test_inode_bitmap2(is->rfs->old_fs->inode_map, dir))
2322 ret |= DIRENT_CHANGED;
2323
a8519a2d 2324 if (!dirent->inode)
fb47b94f 2325 return ret;
a8519a2d
TT
2326
2327 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
2328
2329 if (!new_inode)
fb47b94f 2330 return ret;
a8519a2d
TT
2331#ifdef RESIZE2FS_DEBUG
2332 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
f35fd3d5 2333 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
70f4632b 2334 dir, ext2fs_dirent_name_len(dirent), dirent->name,
a8519a2d
TT
2335 dirent->inode, new_inode);
2336#endif
2337
2338 dirent->inode = new_inode;
2339
085d2a83
TT
2340 /* Update the directory mtime and ctime */
2341 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
2342 if (retval == 0) {
2343 inode.i_mtime = inode.i_ctime = time(0);
d2b2a488
BB
2344 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
2345 if (is->err)
fb47b94f 2346 return ret | DIRENT_ABORT;
085d2a83
TT
2347 }
2348
fb47b94f 2349 return ret | DIRENT_CHANGED;
a8519a2d
TT
2350}
2351
2352static errcode_t inode_ref_fix(ext2_resize_t rfs)
2353{
2354 errcode_t retval;
2355 struct istruct is;
efc6f628 2356
a8519a2d
TT
2357 if (!rfs->imap)
2358 return 0;
efc6f628 2359
a8519a2d
TT
2360 /*
2361 * Now, we iterate over all of the directories to update the
2362 * inode references
2363 */
2364 is.num = 0;
8728d506 2365 is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
a8519a2d 2366 is.rfs = rfs;
3b627e8d 2367 is.err = 0;
a8519a2d 2368
3b627e8d
TT
2369 if (rfs->progress) {
2370 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1333fe93 2371 0, is.max_dirs);
3b627e8d
TT
2372 if (retval)
2373 goto errout;
2374 }
efc6f628 2375
fb47b94f 2376 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
a8519a2d
TT
2377 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
2378 DIRENT_FLAG_INCLUDE_EMPTY, 0,
2379 check_and_change_inodes, &is);
fb47b94f 2380 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
3b627e8d
TT
2381 if (retval)
2382 goto errout;
2383 if (is.err) {
2384 retval = is.err;
2385 goto errout;
2386 }
a8519a2d 2387
88dbf828
TT
2388 if (rfs->progress && (is.num < is.max_dirs))
2389 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
2390 is.max_dirs, is.max_dirs);
2391
3b627e8d 2392errout:
a8519a2d
TT
2393 ext2fs_free_extent_table(rfs->imap);
2394 rfs->imap = 0;
2395 return retval;
2396}
2397
2398
2399/* --------------------------------------------------------------------
2400 *
2401 * Resize processing, phase 5.
2402 *
2403 * In this phase we actually move the inode table around, and then
2404 * update the summary statistics. This is scary, since aborting here
2405 * will potentially scramble the filesystem. (We are moving the
2406 * inode tables around in place, and so the potential for lost data,
2407 * or at the very least scrambling the mapping between filenames and
2408 * inode numbers is very high in case of a power failure here.)
2409 * --------------------------------------------------------------------
2410 */
2411
24b2c7a7 2412
052db4b7
TT
2413/*
2414 * A very scary routine --- this one moves the inode table around!!!
2415 *
2416 * After this you have to use the rfs->new_fs file handle to read and
2417 * write inodes.
2418 */
c762c8e6 2419static errcode_t move_itables(ext2_resize_t rfs)
052db4b7 2420{
8728d506
VAH
2421 int n, num, size;
2422 long long diff;
54434927 2423 dgrp_t i, max_groups;
052db4b7 2424 ext2_filsys fs = rfs->new_fs;
05e112a1 2425 char *cp;
118d3f0b 2426 blk64_t old_blk, new_blk, blk, cluster_freed;
a8519a2d 2427 errcode_t retval;
62f9bd0e
TT
2428 int to_move, moved;
2429 unsigned int j;
118d3f0b 2430 ext2fs_block_bitmap new_bmap = NULL;
052db4b7 2431
1333fe93
TT
2432 max_groups = fs->group_desc_count;
2433 if (max_groups > rfs->old_fs->group_desc_count)
2434 max_groups = rfs->old_fs->group_desc_count;
052db4b7 2435
05e112a1
TT
2436 size = fs->blocksize * fs->inode_blocks_per_group;
2437 if (!rfs->itable_buf) {
c4e3d3f3 2438 retval = ext2fs_get_mem(size, &rfs->itable_buf);
ca8abba7
TT
2439 if (retval)
2440 return retval;
05e112a1 2441 }
c762c8e6 2442
118d3f0b
DW
2443 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
2444 retval = ext2fs_allocate_block_bitmap(fs, _("new meta blocks"),
2445 &new_bmap);
2446 if (retval)
2447 return retval;
2448
2449 retval = mark_table_blocks(fs, new_bmap);
2450 if (retval)
2451 goto errout;
2452 }
2453
c762c8e6
TT
2454 /*
2455 * Figure out how many inode tables we need to move
2456 */
2457 to_move = moved = 0;
1333fe93 2458 for (i=0; i < max_groups; i++)
d7cca6b0
VAH
2459 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
2460 ext2fs_inode_table_loc(fs, i))
c762c8e6
TT
2461 to_move++;
2462
c916e524
DW
2463 if (to_move == 0) {
2464 retval = 0;
2465 goto errout;
2466 }
c762c8e6 2467
3b627e8d
TT
2468 if (rfs->progress) {
2469 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
2470 0, to_move);
2471 if (retval)
2472 goto errout;
2473 }
63b44fbe 2474
a8519a2d
TT
2475 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
2476
1333fe93 2477 for (i=0; i < max_groups; i++) {
d7cca6b0
VAH
2478 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
2479 new_blk = ext2fs_inode_table_loc(fs, i);
ca8abba7 2480 diff = new_blk - old_blk;
efc6f628 2481
80c0fc34 2482#ifdef RESIZE2FS_DEBUG
efc6f628 2483 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
8728d506 2484 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
33b9a60c
TT
2485 i, (unsigned long long) old_blk,
2486 (unsigned long long) new_blk, diff);
80c0fc34 2487#endif
efc6f628 2488
05e112a1 2489 if (!diff)
052db4b7 2490 continue;
cd84e9a3
TT
2491 if (diff < 0)
2492 diff = 0;
052db4b7 2493
24a117ab
VAH
2494 retval = io_channel_read_blk64(fs->io, old_blk,
2495 fs->inode_blocks_per_group,
2496 rfs->itable_buf);
efc6f628 2497 if (retval)
a8519a2d 2498 goto errout;
05e112a1
TT
2499 /*
2500 * The end of the inode table segment often contains
a8519a2d
TT
2501 * all zeros, and we're often only moving the inode
2502 * table down a block or two. If so, we can optimize
2503 * things by not rewriting blocks that we know to be zero
2504 * already.
05e112a1 2505 */
2787276e 2506 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
05e112a1
TT
2507 if (*cp)
2508 break;
2509 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
80c0fc34 2510#ifdef RESIZE2FS_DEBUG
efc6f628 2511 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
f35fd3d5 2512 printf("%d blocks of zeros...\n", n);
80c0fc34 2513#endif
05e112a1
TT
2514 num = fs->inode_blocks_per_group;
2515 if (n > diff)
2516 num -= n;
2517
24a117ab
VAH
2518 retval = io_channel_write_blk64(fs->io, new_blk,
2519 num, rfs->itable_buf);
052db4b7 2520 if (retval) {
24a117ab
VAH
2521 io_channel_write_blk64(fs->io, old_blk,
2522 num, rfs->itable_buf);
a8519a2d 2523 goto errout;
052db4b7 2524 }
05e112a1 2525 if (n > diff) {
24a117ab 2526 retval = io_channel_write_blk64(fs->io,
ca8abba7 2527 old_blk + fs->inode_blocks_per_group,
a8519a2d
TT
2528 diff, (rfs->itable_buf +
2529 (fs->inode_blocks_per_group - diff) *
2530 fs->blocksize));
05e112a1 2531 if (retval)
a8519a2d 2532 goto errout;
c762c8e6 2533 }
64ad98ac 2534
d7cca6b0 2535 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
118d3f0b
DW
2536 j < fs->inode_blocks_per_group;) {
2537 if (new_bmap == NULL ||
2538 !ext2fs_test_block_bitmap2(new_bmap, blk)) {
2539 ext2fs_block_alloc_stats2(fs, blk, -1);
2540 cluster_freed = EXT2FS_CLUSTER_RATIO(fs) -
2541 (blk & EXT2FS_CLUSTER_MASK(fs));
2542 blk += cluster_freed;
2543 j += cluster_freed;
2544 continue;
2545 }
2546 blk++;
2547 j++;
2548 }
64ad98ac 2549
d7cca6b0 2550 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
236efede 2551 ext2fs_group_desc_csum_set(rfs->old_fs, i);
a8519a2d 2552 ext2fs_mark_super_dirty(rfs->old_fs);
64ad98ac
TT
2553 ext2fs_flush(rfs->old_fs);
2554
a8519a2d 2555 if (rfs->progress) {
3b627e8d
TT
2556 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
2557 ++moved, to_move);
2558 if (retval)
2559 goto errout;
a8519a2d 2560 }
052db4b7 2561 }
64ad98ac 2562 mark_table_blocks(fs, fs->block_map);
a8519a2d 2563 ext2fs_flush(fs);
80c0fc34 2564#ifdef RESIZE2FS_DEBUG
efc6f628 2565 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
f35fd3d5 2566 printf("Inode table move finished.\n");
80c0fc34 2567#endif
118d3f0b 2568 retval = 0;
efc6f628 2569
a8519a2d 2570errout:
118d3f0b
DW
2571 if (new_bmap)
2572 ext2fs_free_block_bitmap(new_bmap);
052db4b7
TT
2573 return retval;
2574}
2575
65c6c3e0
TT
2576/*
2577 * This function is used when expanding a file system. It frees the
2578 * superblock and block group descriptor blocks from the block group
2579 * which is no longer the last block group.
2580 */
2581static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs)
2582{
2583 ext2_filsys fs = rfs->new_fs;
2584 ext2_filsys old_fs = rfs->old_fs;
2585 errcode_t retval;
2586 dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1;
2587 dgrp_t last_bg = fs->group_desc_count - 1;
2588 blk64_t sb, old_desc;
2589 blk_t num;
2590
21e37be6 2591 if (!ext2fs_has_feature_sparse_super2(fs->super))
65c6c3e0
TT
2592 return 0;
2593
2594 if (last_bg <= old_last_bg)
2595 return 0;
2596
2597 if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
2598 fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
2599 return 0;
2600
2601 if (old_fs->super->s_backup_bgs[0] != old_last_bg &&
2602 old_fs->super->s_backup_bgs[1] != old_last_bg)
2603 return 0;
2604
2605 if (fs->super->s_backup_bgs[0] == old_last_bg ||
2606 fs->super->s_backup_bgs[1] == old_last_bg)
2607 return 0;
2608
aaa1ae08
TT
2609 if (old_last_bg == 0)
2610 return 0;
2611
65c6c3e0
TT
2612 retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, old_last_bg,
2613 &sb, &old_desc, NULL, &num);
2614 if (retval)
2615 return retval;
2616
2617 if (sb)
2618 ext2fs_unmark_block_bitmap2(fs->block_map, sb);
2619 if (old_desc)
2620 ext2fs_unmark_block_bitmap_range2(fs->block_map, old_desc, num);
2621 return 0;
2622}
2623
2624/*
2625 * This function is used when shrinking a file system. We need to
2626 * utilize blocks from what will be the new last block group for the
2627 * backup superblock and block group descriptor blocks.
2628 * Unfortunately, those blocks may be used by other files or fs
2629 * metadata blocks. We need to mark them as being in use.
2630 */
2631static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
2632 ext2fs_block_bitmap meta_bmap)
2633{
2634 ext2_filsys fs = rfs->new_fs;
2635 ext2_filsys old_fs = rfs->old_fs;
2636 errcode_t retval;
2637 dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1;
2638 dgrp_t last_bg = fs->group_desc_count - 1;
2639 dgrp_t g;
2640 blk64_t blk, sb, old_desc;
2641 blk_t i, num;
2642 int realloc = 0;
2643
21e37be6 2644 if (!ext2fs_has_feature_sparse_super2(fs->super))
65c6c3e0
TT
2645 return 0;
2646
2647 if (last_bg >= old_last_bg)
2648 return 0;
2649
2650 if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
2651 fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
2652 return 0;
2653
2654 if (fs->super->s_backup_bgs[0] != last_bg &&
2655 fs->super->s_backup_bgs[1] != last_bg)
2656 return 0;
2657
2658 if (old_fs->super->s_backup_bgs[0] == last_bg ||
2659 old_fs->super->s_backup_bgs[1] == last_bg)
2660 return 0;
2661
2662 retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, last_bg,
2663 &sb, &old_desc, NULL, &num);
2664 if (retval)
2665 return retval;
2666
4495c5a0 2667 if (last_bg && !sb) {
65c6c3e0
TT
2668 fputs(_("Should never happen! No sb in last super_sparse bg?\n"),
2669 stderr);
2670 exit(1);
2671 }
1244cacc 2672 if (old_desc && old_desc != sb+1) {
65c6c3e0
TT
2673 fputs(_("Should never happen! Unexpected old_desc in "
2674 "super_sparse bg?\n"),
2675 stderr);
2676 exit(1);
2677 }
2678 num = (old_desc) ? num : 1;
2679
2680 /* Reserve the backup blocks */
2681 ext2fs_mark_block_bitmap_range2(fs->block_map, sb, num);
2682
2683 for (g = 0; g < fs->group_desc_count; g++) {
2684 blk64_t mb;
2685
2686 mb = ext2fs_block_bitmap_loc(fs, g);
2687 if ((mb >= sb) && (mb < sb + num)) {
2688 ext2fs_block_bitmap_loc_set(fs, g, 0);
2689 realloc = 1;
2690 }
2691 mb = ext2fs_inode_bitmap_loc(fs, g);
2692 if ((mb >= sb) && (mb < sb + num)) {
2693 ext2fs_inode_bitmap_loc_set(fs, g, 0);
2694 realloc = 1;
2695 }
2696 mb = ext2fs_inode_table_loc(fs, g);
2697 if ((mb < sb + num) &&
2698 (sb < mb + fs->inode_blocks_per_group)) {
2699 ext2fs_inode_table_loc_set(fs, g, 0);
2700 realloc = 1;
2701 }
2702 if (realloc) {
2703 retval = ext2fs_allocate_group_table(fs, g, 0);
2704 if (retval)
2705 return retval;
2706 }
2707 }
2708
2709 for (blk = sb, i = 0; i < num; blk++, i++) {
2710 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
2711 !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
2712 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
2713 rfs->needed_blocks++;
2714 }
2715 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
2716 }
2717 return 0;
2718}
2719
9213a93b 2720/*
efc6f628 2721 * Fix the resize inode
9213a93b
TT
2722 */
2723static errcode_t fix_resize_inode(ext2_filsys fs)
2724{
2725 struct ext2_inode inode;
2726 errcode_t retval;
9213a93b 2727
21e37be6 2728 if (!ext2fs_has_feature_resize_inode(fs->super))
9213a93b
TT
2729 return 0;
2730
9213a93b
TT
2731 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
2732 if (retval) goto errout;
2733
1ca1059f 2734 ext2fs_iblk_set(fs, &inode, 1);
9213a93b
TT
2735
2736 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
2737 if (retval) goto errout;
2738
2739 if (!inode.i_block[EXT2_DIND_BLOCK]) {
efc6f628 2740 /*
9213a93b
TT
2741 * Avoid zeroing out block #0; that's rude. This
2742 * should never happen anyway since the filesystem
2743 * should be fsck'ed and we assume it is consistent.
2744 */
45ff69ff 2745 fprintf(stderr, "%s",
f35fd3d5 2746 _("Should never happen: resize inode corrupt!\n"));
9213a93b
TT
2747 exit(1);
2748 }
2749
08c8e319
DW
2750 retval = ext2fs_zero_blocks2(fs, inode.i_block[EXT2_DIND_BLOCK], 1,
2751 NULL, NULL);
2752 if (retval)
2753 goto errout;
efc6f628 2754
9213a93b
TT
2755 retval = ext2fs_create_resize_inode(fs);
2756 if (retval)
2757 goto errout;
2758
2759errout:
9213a93b
TT
2760 return retval;
2761}
2762
052db4b7
TT
2763/*
2764 * Finally, recalculate the summary information
2765 */
895e8e33 2766static errcode_t resize2fs_calculate_summary_stats(ext2_filsys fs)
052db4b7 2767{
8728d506 2768 blk64_t blk;
dfcdc32f 2769 ext2_ino_t ino;
54434927
TT
2770 unsigned int group = 0;
2771 unsigned int count = 0;
2279fd78
TT
2772 blk64_t total_blocks_free = 0;
2773 int total_inodes_free = 0;
dfcdc32f 2774 int group_free = 0;
74128f8d 2775 int uninit = 0;
8728d506 2776 blk64_t super_blk, old_desc_blk, new_desc_blk;
74128f8d 2777 int old_desc_blocks;
052db4b7
TT
2778
2779 /*
2780 * First calculate the block statistics
2781 */
cd65a24e 2782 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
8728d506
VAH
2783 ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
2784 &new_desc_blk, 0);
21e37be6 2785 if (ext2fs_has_feature_meta_bg(fs->super))
74128f8d
TT
2786 old_desc_blocks = fs->super->s_first_meta_bg;
2787 else
2788 old_desc_blocks = fs->desc_blocks +
2789 fs->super->s_reserved_gdt_blocks;
1773c87c
TT
2790 for (blk = B2C(fs->super->s_first_data_block);
2791 blk < ext2fs_blocks_count(fs->super);
2792 blk += EXT2FS_CLUSTER_RATIO(fs)) {
74128f8d 2793 if ((uninit &&
1773c87c 2794 !(EQ_CLSTR(blk, super_blk) ||
74128f8d 2795 ((old_desc_blk && old_desc_blocks &&
1773c87c
TT
2796 GE_CLSTR(blk, old_desc_blk) &&
2797 LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) ||
2798 ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) ||
2799 EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) ||
2800 EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) ||
2801 ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) &&
2802 LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group)
2803 + fs->inode_blocks_per_group))))) ||
3346084b 2804 (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
052db4b7 2805 group_free++;
2279fd78 2806 total_blocks_free++;
052db4b7
TT
2807 }
2808 count++;
1773c87c
TT
2809 if ((count == fs->super->s_clusters_per_group) ||
2810 EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) {
d7cca6b0 2811 ext2fs_bg_free_blocks_count_set(fs, group, group_free);
236efede
JS
2812 ext2fs_group_desc_csum_set(fs, group);
2813 group++;
4828bbe9
TT
2814 if (group >= fs->group_desc_count)
2815 break;
052db4b7
TT
2816 count = 0;
2817 group_free = 0;
8728d506
VAH
2818 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2819 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
2820 &old_desc_blk,
2821 &new_desc_blk, 0);
21e37be6 2822 if (ext2fs_has_feature_meta_bg(fs->super))
74128f8d
TT
2823 old_desc_blocks = fs->super->s_first_meta_bg;
2824 else
2825 old_desc_blocks = fs->desc_blocks +
2826 fs->super->s_reserved_gdt_blocks;
052db4b7
TT
2827 }
2828 }
1773c87c 2829 total_blocks_free = C2B(total_blocks_free);
2279fd78 2830 ext2fs_free_blocks_count_set(fs->super, total_blocks_free);
efc6f628 2831
052db4b7
TT
2832 /*
2833 * Next, calculate the inode statistics
2834 */
2835 group_free = 0;
052db4b7
TT
2836 count = 0;
2837 group = 0;
5830d6be
ES
2838
2839 /* Protect loop from wrap-around if s_inodes_count maxed */
cd65a24e 2840 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
5830d6be 2841 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
74128f8d 2842 if (uninit ||
3346084b 2843 !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
052db4b7 2844 group_free++;
2279fd78 2845 total_inodes_free++;
052db4b7
TT
2846 }
2847 count++;
2848 if ((count == fs->super->s_inodes_per_group) ||
2849 (ino == fs->super->s_inodes_count)) {
d7cca6b0 2850 ext2fs_bg_free_inodes_count_set(fs, group, group_free);
236efede
JS
2851 ext2fs_group_desc_csum_set(fs, group);
2852 group++;
4828bbe9
TT
2853 if (group >= fs->group_desc_count)
2854 break;
052db4b7
TT
2855 count = 0;
2856 group_free = 0;
cd65a24e 2857 uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
052db4b7
TT
2858 }
2859 }
2279fd78 2860 fs->super->s_free_inodes_count = total_inodes_free;
052db4b7
TT
2861 ext2fs_mark_super_dirty(fs);
2862 return 0;
2863}
199ddaaa 2864
8a6ede8b
ES
2865/*
2866 * Journal may have been relocated; update the backup journal blocks
2867 * in the superblock.
2868 */
2869static errcode_t fix_sb_journal_backup(ext2_filsys fs)
2870{
2871 errcode_t retval;
2872 struct ext2_inode inode;
2873
21e37be6 2874 if (!ext2fs_has_feature_journal(fs->super))
8a6ede8b
ES
2875 return 0;
2876
d93d5bbf
ES
2877 /* External journal? Nothing to do. */
2878 if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
2879 return 0;
2880
8a6ede8b
ES
2881 retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
2882 if (retval)
2883 return retval;
2884 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
931b58e1 2885 fs->super->s_jnl_blocks[15] = inode.i_size_high;
8a6ede8b
ES
2886 fs->super->s_jnl_blocks[16] = inode.i_size;
2887 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
2888 ext2fs_mark_super_dirty(fs);
2889 return 0;
2890}
2891
7f820344
TT
2892static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
2893 int old_desc_blocks)
2894{
2895 blk64_t super_blk, old_desc_blk, new_desc_blk;
2896 int overhead;
2897
2898 /* inode table blocks plus allocation bitmaps */
2899 overhead = fs->inode_blocks_per_group + 2;
2900
2901 ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
2902 &old_desc_blk, &new_desc_blk, 0);
2903 if ((grp == 0) || super_blk)
2904 overhead++;
2905 if (old_desc_blk)
2906 overhead += old_desc_blocks;
2907 else if (new_desc_blk)
2908 overhead++;
2909 return overhead;
2910}
2911
2912
199ddaaa 2913/*
055866d8 2914 * calculate the minimum number of blocks the given fs can be resized to
199ddaaa 2915 */
e231f175 2916blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
199ddaaa 2917{
8728d506 2918 ext2_ino_t inode_count;
45a78b88 2919 dgrp_t groups, flex_groups;
e231f175 2920 blk64_t blks_needed, data_blocks;
8728d506
VAH
2921 blk64_t grp, data_needed, last_start;
2922 blk64_t overhead = 0;
7f820344 2923 int old_desc_blocks;
2884d208 2924 int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
199ddaaa
JB
2925
2926 /*
2927 * first figure out how many group descriptors we need to
2928 * handle the number of inodes we have
2929 */
2930 inode_count = fs->super->s_inodes_count -
2931 fs->super->s_free_inodes_count;
2932 blks_needed = ext2fs_div_ceil(inode_count,
2933 fs->super->s_inodes_per_group) *
1e33a8b4 2934 (blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super);
8728d506
VAH
2935 groups = ext2fs_div64_ceil(blks_needed,
2936 EXT2_BLOCKS_PER_GROUP(fs->super));
e231f175
TT
2937#ifdef RESIZE2FS_DEBUG
2938 if (flags & RESIZE_DEBUG_MIN_CALC)
2939 printf("fs has %d inodes, %d groups required.\n",
2940 inode_count, groups);
2941#endif
199ddaaa
JB
2942
2943 /*
7f820344 2944 * number of old-style block group descriptor blocks
199ddaaa 2945 */
21e37be6 2946 if (ext2fs_has_feature_meta_bg(fs->super))
7f820344
TT
2947 old_desc_blocks = fs->super->s_first_meta_bg;
2948 else
2949 old_desc_blocks = fs->desc_blocks +
2950 fs->super->s_reserved_gdt_blocks;
199ddaaa
JB
2951
2952 /* calculate how many blocks are needed for data */
ac94445f
JK
2953 data_needed = ext2fs_blocks_count(fs->super);
2954 for (grp = 0; grp < fs->group_desc_count; grp++) {
7f820344 2955 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
ac94445f
JK
2956 data_needed -= ext2fs_bg_free_blocks_count(fs, grp);
2957 }
e231f175
TT
2958#ifdef RESIZE2FS_DEBUG
2959 if (flags & RESIZE_DEBUG_MIN_CALC)
33b9a60c
TT
2960 printf("fs requires %llu data blocks.\n",
2961 (unsigned long long) data_needed);
e231f175 2962#endif
7f820344
TT
2963
2964 /*
2965 * For ext4 we need to allow for up to a flex_bg worth of
2966 * inode tables of slack space so the resize operation can be
2967 * guaranteed to finish.
2968 */
45a78b88 2969 flex_groups = groups;
21e37be6 2970 if (ext2fs_has_feature_flex_bg(fs->super)) {
45a78b88
TT
2971 dgrp_t remainder = groups & (flexbg_size - 1);
2972
2973 flex_groups += flexbg_size - remainder;
2974 if (flex_groups > fs->group_desc_count)
2975 flex_groups = fs->group_desc_count;
c09043f1
TT
2976 }
2977
199ddaaa
JB
2978 /*
2979 * figure out how many data blocks we have given the number of groups
2980 * we need for our inodes
2981 */
1e33a8b4 2982 data_blocks = EXT2_GROUPS_TO_BLOCKS(fs->super, groups);
199ddaaa 2983 last_start = 0;
45a78b88 2984 for (grp = 0; grp < flex_groups; grp++) {
7f820344 2985 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
199ddaaa
JB
2986
2987 /*
2988 * we want to keep track of how much data we can store in
2989 * the groups leading up to the last group so we can determine
2990 * how big the last group needs to be
2991 */
45a78b88 2992 if (grp < (groups - 1))
199ddaaa
JB
2993 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
2994 overhead;
2995
45a78b88
TT
2996 if (data_blocks > overhead)
2997 data_blocks -= overhead;
2998 else
2999 data_blocks = 0;
199ddaaa 3000 }
e231f175
TT
3001#ifdef RESIZE2FS_DEBUG
3002 if (flags & RESIZE_DEBUG_MIN_CALC)
3003 printf("With %d group(s), we have %llu blocks available.\n",
33b9a60c 3004 groups, (unsigned long long) data_blocks);
e231f175 3005#endif
199ddaaa
JB
3006
3007 /*
055866d8 3008 * if we need more group descriptors in order to accommodate our data
199ddaaa
JB
3009 * then we need to add them here
3010 */
45a78b88 3011 blks_needed = data_needed;
b4f30fcf
TT
3012 while (blks_needed > data_blocks) {
3013 blk64_t remainder = blks_needed - data_blocks;
e231f175 3014 dgrp_t extra_grps;
199ddaaa
JB
3015
3016 /* figure out how many more groups we need for the data */
8728d506
VAH
3017 extra_grps = ext2fs_div64_ceil(remainder,
3018 EXT2_BLOCKS_PER_GROUP(fs->super));
199ddaaa 3019
1e33a8b4 3020 data_blocks += EXT2_GROUPS_TO_BLOCKS(fs->super, extra_grps);
199ddaaa
JB
3021
3022 /* ok we have to account for the last group */
7f820344 3023 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
199ddaaa
JB
3024 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
3025
45a78b88
TT
3026 grp = flex_groups;
3027 groups += extra_grps;
21e37be6 3028 if (!ext2fs_has_feature_flex_bg(fs->super))
45a78b88
TT
3029 flex_groups = groups;
3030 else if (groups > flex_groups) {
3031 dgrp_t r = groups & (flexbg_size - 1);
3032
3033 flex_groups = groups + flexbg_size - r;
3034 if (flex_groups > fs->group_desc_count)
3035 flex_groups = fs->group_desc_count;
3036 }
3037
3038 for (; grp < flex_groups; grp++) {
7f820344
TT
3039 overhead = calc_group_overhead(fs, grp,
3040 old_desc_blocks);
199ddaaa
JB
3041
3042 /*
3043 * again, we need to see how much data we cram into
3044 * all of the groups leading up to the last group
3045 */
45a78b88 3046 if (grp < groups - 1)
199ddaaa
JB
3047 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
3048 - overhead;
3049
3050 data_blocks -= overhead;
3051 }
3052
e231f175
TT
3053#ifdef RESIZE2FS_DEBUG
3054 if (flags & RESIZE_DEBUG_MIN_CALC)
3055 printf("Added %d extra group(s), "
b4f30fcf 3056 "blks_needed %llu, data_blocks %llu, "
33b9a60c
TT
3057 "last_start %llu\n", extra_grps,
3058 (unsigned long long) blks_needed,
3059 (unsigned long long) data_blocks,
3060 (unsigned long long) last_start);
e231f175 3061#endif
199ddaaa
JB
3062 }
3063
3064 /* now for the fun voodoo */
45a78b88 3065 grp = groups - 1;
21e37be6 3066 if (ext2fs_has_feature_flex_bg(fs->super) &&
45a78b88
TT
3067 (grp & ~(flexbg_size - 1)) == 0)
3068 grp = grp & ~(flexbg_size - 1);
3069 overhead = 0;
3070 for (; grp < flex_groups; grp++)
3071 overhead += calc_group_overhead(fs, grp, old_desc_blocks);
3072
e231f175
TT
3073#ifdef RESIZE2FS_DEBUG
3074 if (flags & RESIZE_DEBUG_MIN_CALC)
33b9a60c
TT
3075 printf("Last group's overhead is %llu\n",
3076 (unsigned long long) overhead);
e231f175 3077#endif
199ddaaa
JB
3078
3079 /*
3080 * if this is the case then the last group is going to have data in it
3081 * so we need to adjust the size of the last group accordingly
3082 */
b4f30fcf
TT
3083 if (last_start < blks_needed) {
3084 blk64_t remainder = blks_needed - last_start;
199ddaaa 3085
e231f175
TT
3086#ifdef RESIZE2FS_DEBUG
3087 if (flags & RESIZE_DEBUG_MIN_CALC)
3088 printf("Need %llu data blocks in last group\n",
33b9a60c 3089 (unsigned long long) remainder);
e231f175 3090#endif
199ddaaa
JB
3091 /*
3092 * 50 is a magic number that mkfs/resize uses to see if its
3093 * even worth making/resizing the fs. basically you need to
3094 * have at least 50 blocks in addition to the blocks needed
3095 * for the metadata in the last group
3096 */
3097 if (remainder > 50)
3098 overhead += remainder;
3099 else
3100 overhead += 50;
3101 } else
3102 overhead += 50;
3103
ba8bfa1a 3104 overhead += fs->super->s_first_data_block;
e231f175
TT
3105#ifdef RESIZE2FS_DEBUG
3106 if (flags & RESIZE_DEBUG_MIN_CALC)
33b9a60c
TT
3107 printf("Final size of last group is %llu\n",
3108 (unsigned long long) overhead);
e231f175 3109#endif
199ddaaa 3110
45a78b88
TT
3111 /* Add extra slack for bigalloc file systems */
3112 if (EXT2FS_CLUSTER_RATIO(fs) > 1)
3113 overhead += EXT2FS_CLUSTER_RATIO(fs) * 2;
3114
199ddaaa 3115 /*
45a78b88
TT
3116 * since our last group doesn't have to be BLOCKS_PER_GROUP
3117 * large, we only do groups-1, and then add the number of
3118 * blocks needed to handle the group descriptor metadata+data
3119 * that we need
199ddaaa 3120 */
1e33a8b4 3121 blks_needed = EXT2_GROUPS_TO_BLOCKS(fs->super, groups - 1);
199ddaaa
JB
3122 blks_needed += overhead;
3123
2215293c
TT
3124 /*
3125 * Make sure blks_needed covers the end of the inode table in
3126 * the last block group.
3127 */
3128 overhead = ext2fs_inode_table_loc(fs, groups-1) +
3129 fs->inode_blocks_per_group;
3130 if (blks_needed < overhead)
3131 blks_needed = overhead;
3132
e231f175
TT
3133#ifdef RESIZE2FS_DEBUG
3134 if (flags & RESIZE_DEBUG_MIN_CALC)
33b9a60c
TT
3135 printf("Estimated blocks needed: %llu\n",
3136 (unsigned long long) blks_needed);
e231f175
TT
3137#endif
3138
69f7c80e
ES
3139 /*
3140 * If at this point we've already added up more "needed" than
3141 * the current size, just return current size as minimum.
3142 */
4efbac6f
VAH
3143 if (blks_needed >= ext2fs_blocks_count(fs->super))
3144 return ext2fs_blocks_count(fs->super);
793a04a0
TT
3145 /*
3146 * We need to reserve a few extra blocks if extents are
3147 * enabled, in case we need to grow the extent tree. The more
3148 * we shrink the file system, the more space we need.
b4f30fcf
TT
3149 *
3150 * The absolute worst case is every single data block is in
3151 * the part of the file system that needs to be evacuated,
3152 * with each data block needs to be in its own extent, and
3153 * with each inode needing at least one extent block.
793a04a0 3154 */
21e37be6 3155 if (ext2fs_has_feature_extents(fs->super)) {
e231f175
TT
3156 blk64_t safe_margin = (ext2fs_blocks_count(fs->super) -
3157 blks_needed)/500;
b4f30fcf
TT
3158 unsigned int exts_per_blk = (fs->blocksize /
3159 sizeof(struct ext3_extent)) - 1;
3160 blk64_t worst_case = ((data_needed + exts_per_blk - 1) /
3161 exts_per_blk);
3162
3163 if (worst_case < inode_count)
3164 worst_case = inode_count;
3165
3166 if (safe_margin > worst_case)
3167 safe_margin = worst_case;
3168
e231f175
TT
3169#ifdef RESIZE2FS_DEBUG
3170 if (flags & RESIZE_DEBUG_MIN_CALC)
33b9a60c
TT
3171 printf("Extents safety margin: %llu\n",
3172 (unsigned long long) safe_margin);
e231f175
TT
3173#endif
3174 blks_needed += safe_margin;
3175 }
793a04a0 3176
199ddaaa
JB
3177 return blks_needed;
3178}