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