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