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