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