]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - resize/resize2fs.c
Remove trailing whitespace for the entire source tree
[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 "resize2fs.h"
37 #include <time.h>
38
39 #ifdef __linux__ /* Kludge for debugging */
40 #define RESIZE2FS_DEBUG
41 #endif
42
43 static void fix_uninit_block_bitmaps(ext2_filsys fs);
44 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size);
45 static errcode_t blocks_to_move(ext2_resize_t rfs);
46 static errcode_t block_mover(ext2_resize_t rfs);
47 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
48 static errcode_t inode_ref_fix(ext2_resize_t rfs);
49 static errcode_t move_itables(ext2_resize_t rfs);
50 static errcode_t fix_resize_inode(ext2_filsys fs);
51 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
52
53 /*
54 * Some helper CPP macros
55 */
56 #define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap)
57 #define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap)
58 #define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table)
59
60 #define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i)))
61 #define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i)))
62
63 #define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \
64 ((blk) < (FS_INODE_TB((fs), (i)) + \
65 (fs)->inode_blocks_per_group)))
66
67 #define META_OVERHEAD(fs) (2 + (fs)->inode_blocks_per_group)
68 #define SUPER_OVERHEAD(fs) (1 + (fs)->desc_blocks +\
69 (fs)->super->s_reserved_gdt_blocks)
70
71 /*
72 * This is the top-level routine which does the dirty deed....
73 */
74 errcode_t resize_fs(ext2_filsys fs, blk_t *new_size, int flags,
75 errcode_t (*progress)(ext2_resize_t rfs, int pass,
76 unsigned long cur,
77 unsigned long max_val))
78 {
79 ext2_resize_t rfs;
80 errcode_t retval;
81
82 retval = ext2fs_read_bitmaps(fs);
83 if (retval)
84 return retval;
85
86 /*
87 * Create the data structure
88 */
89 retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
90 if (retval)
91 return retval;
92 memset(rfs, 0, sizeof(struct ext2_resize_struct));
93
94 fix_uninit_block_bitmaps(fs);
95 fs->priv_data = rfs;
96 rfs->old_fs = fs;
97 rfs->flags = flags;
98 rfs->itable_buf = 0;
99 rfs->progress = progress;
100 retval = ext2fs_dup_handle(fs, &rfs->new_fs);
101 if (retval)
102 goto errout;
103
104 retval = adjust_superblock(rfs, *new_size);
105 if (retval)
106 goto errout;
107
108 fix_uninit_block_bitmaps(rfs->new_fs);
109 /* Clear the block bitmap uninit flag for the last block group */
110 rfs->new_fs->group_desc[rfs->new_fs->group_desc_count-1].bg_flags &=
111 ~EXT2_BG_BLOCK_UNINIT;
112
113 *new_size = rfs->new_fs->super->s_blocks_count;
114
115 retval = blocks_to_move(rfs);
116 if (retval)
117 goto errout;
118
119 #ifdef RESIZE2FS_DEBUG
120 if (rfs->flags & RESIZE_DEBUG_BMOVE)
121 printf("Number of free blocks: %u/%u, Needed: %d\n",
122 rfs->old_fs->super->s_free_blocks_count,
123 rfs->new_fs->super->s_free_blocks_count,
124 rfs->needed_blocks);
125 #endif
126
127 retval = block_mover(rfs);
128 if (retval)
129 goto errout;
130
131 retval = inode_scan_and_fix(rfs);
132 if (retval)
133 goto errout;
134
135 retval = inode_ref_fix(rfs);
136 if (retval)
137 goto errout;
138
139 retval = move_itables(rfs);
140 if (retval)
141 goto errout;
142
143 retval = ext2fs_calculate_summary_stats(rfs->new_fs);
144 if (retval)
145 goto errout;
146
147 retval = fix_resize_inode(rfs->new_fs);
148 if (retval)
149 goto errout;
150
151 rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
152 retval = ext2fs_close(rfs->new_fs);
153 if (retval)
154 goto errout;
155
156 rfs->flags = flags;
157
158 ext2fs_free(rfs->old_fs);
159 if (rfs->itable_buf)
160 ext2fs_free_mem(&rfs->itable_buf);
161 ext2fs_free_mem(&rfs);
162
163 return 0;
164
165 errout:
166 if (rfs->new_fs)
167 ext2fs_free(rfs->new_fs);
168 if (rfs->itable_buf)
169 ext2fs_free_mem(&rfs->itable_buf);
170 ext2fs_free_mem(&rfs);
171 return retval;
172 }
173
174 /*
175 * Clean up the bitmaps for unitialized bitmaps
176 */
177 static void fix_uninit_block_bitmaps(ext2_filsys fs)
178 {
179 int i;
180 blk_t blk, super_blk, old_desc_blk, new_desc_blk;
181 int old_desc_blocks;
182 dgrp_t g;
183
184 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
185 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
186 return;
187
188 for (g=0; g < fs->group_desc_count; g++) {
189 if (!(fs->group_desc[g].bg_flags & EXT2_BG_BLOCK_UNINIT))
190 continue;
191
192 blk = (g * fs->super->s_blocks_per_group) +
193 fs->super->s_first_data_block;
194
195 ext2fs_super_and_bgd_loc(fs, g, &super_blk,
196 &old_desc_blk, &new_desc_blk, 0);
197
198 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
199 old_desc_blocks = fs->super->s_first_meta_bg;
200 else
201 old_desc_blocks = fs->desc_blocks +
202 fs->super->s_reserved_gdt_blocks;
203
204 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
205 if (blk >= fs->super->s_blocks_count)
206 break;
207 if ((blk == super_blk) ||
208 (old_desc_blk && old_desc_blocks &&
209 (blk >= old_desc_blk) &&
210 (blk < old_desc_blk + old_desc_blocks)) ||
211 (new_desc_blk && (blk == new_desc_blk)) ||
212 (blk == fs->group_desc[g].bg_block_bitmap) ||
213 (blk == fs->group_desc[g].bg_inode_bitmap) ||
214 (blk >= fs->group_desc[g].bg_inode_table &&
215 (blk < fs->group_desc[g].bg_inode_table
216 + fs->inode_blocks_per_group)))
217 ext2fs_fast_mark_block_bitmap(fs->block_map, blk);
218 else
219 ext2fs_fast_unmark_block_bitmap(fs->block_map, blk);
220 }
221 }
222 }
223
224 /* --------------------------------------------------------------------
225 *
226 * Resize processing, phase 1.
227 *
228 * In this phase we adjust the in-memory superblock information, and
229 * initialize any new parts of the inode table. The new parts of the
230 * inode table are created in virgin disk space, so we can abort here
231 * without any side effects.
232 * --------------------------------------------------------------------
233 */
234
235 /*
236 * This routine is shared by the online and offline resize routines.
237 * All of the information which is adjusted in memory is done here.
238 */
239 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs, blk_t new_size)
240 {
241 errcode_t retval;
242 int overhead = 0;
243 int rem;
244 blk_t blk, group_block;
245 ext2_ino_t real_end;
246 int adj, old_numblocks, numblocks, adjblocks;
247 unsigned long i, j, old_desc_blocks, max_group;
248 unsigned int meta_bg, meta_bg_size;
249 int has_super, csum_flag;
250 unsigned long long new_inodes; /* u64 to check for overflow */
251 double percent;
252
253 fs->super->s_blocks_count = new_size;
254
255 retry:
256 fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
257 fs->super->s_first_data_block,
258 EXT2_BLOCKS_PER_GROUP(fs->super));
259 if (fs->group_desc_count == 0)
260 return EXT2_ET_TOOSMALL;
261 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
262 EXT2_DESC_PER_BLOCK(fs->super));
263
264 /*
265 * Overhead is the number of bookkeeping blocks per group. It
266 * includes the superblock backup, the group descriptor
267 * backups, the inode bitmap, the block bitmap, and the inode
268 * table.
269 */
270 overhead = (int) (2 + fs->inode_blocks_per_group);
271
272 if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
273 overhead += 1 + fs->desc_blocks +
274 fs->super->s_reserved_gdt_blocks;
275
276 /*
277 * See if the last group is big enough to support the
278 * necessary data structures. If not, we need to get rid of
279 * it.
280 */
281 rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
282 fs->super->s_blocks_per_group;
283 if ((fs->group_desc_count == 1) && rem && (rem < overhead))
284 return EXT2_ET_TOOSMALL;
285 if (rem && (rem < overhead+50)) {
286 fs->super->s_blocks_count -= rem;
287 goto retry;
288 }
289 /*
290 * Adjust the number of inodes
291 */
292 new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
293 if (new_inodes > ~0U) {
294 fprintf(stderr, _("inodes (%llu) must be less than %u"),
295 new_inodes, ~0U);
296 return EXT2_ET_TOO_MANY_INODES;
297 }
298 fs->super->s_inodes_count = fs->super->s_inodes_per_group *
299 fs->group_desc_count;
300
301 /*
302 * Adjust the number of free blocks
303 */
304 blk = old_fs->super->s_blocks_count;
305 if (blk > fs->super->s_blocks_count)
306 fs->super->s_free_blocks_count -=
307 (blk - fs->super->s_blocks_count);
308 else
309 fs->super->s_free_blocks_count +=
310 (fs->super->s_blocks_count - blk);
311
312 /*
313 * Adjust the number of reserved blocks
314 */
315 percent = (old_fs->super->s_r_blocks_count * 100.0) /
316 old_fs->super->s_blocks_count;
317 fs->super->s_r_blocks_count = (unsigned int) (percent *
318 fs->super->s_blocks_count / 100.0);
319
320 /*
321 * Adjust the bitmaps for size
322 */
323 retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
324 fs->super->s_inodes_count,
325 fs->inode_map);
326 if (retval) goto errout;
327
328 real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
329 * fs->group_desc_count)) - 1 +
330 fs->super->s_first_data_block;
331 retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
332 real_end, fs->block_map);
333
334 if (retval) goto errout;
335
336 /*
337 * Reallocate the group descriptors as necessary.
338 */
339 if (old_fs->desc_blocks != fs->desc_blocks) {
340 retval = ext2fs_resize_mem(old_fs->desc_blocks *
341 fs->blocksize,
342 fs->desc_blocks * fs->blocksize,
343 &fs->group_desc);
344 if (retval)
345 goto errout;
346 if (fs->desc_blocks > old_fs->desc_blocks)
347 memset((char *) fs->group_desc +
348 (old_fs->desc_blocks * fs->blocksize), 0,
349 (fs->desc_blocks - old_fs->desc_blocks) *
350 fs->blocksize);
351 }
352
353 /*
354 * If the resize_inode feature is set, and we are changing the
355 * number of descriptor blocks, then adjust
356 * s_reserved_gdt_blocks if possible to avoid needing to move
357 * the inode table either now or in the future.
358 */
359 if ((fs->super->s_feature_compat &
360 EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
361 (old_fs->desc_blocks != fs->desc_blocks)) {
362 int new;
363
364 new = ((int) fs->super->s_reserved_gdt_blocks) +
365 (old_fs->desc_blocks - fs->desc_blocks);
366 if (new < 0)
367 new = 0;
368 if (new > (int) fs->blocksize/4)
369 new = fs->blocksize/4;
370 fs->super->s_reserved_gdt_blocks = new;
371 }
372
373 /*
374 * If we are shrinking the number block groups, we're done and
375 * can exit now.
376 */
377 if (old_fs->group_desc_count > fs->group_desc_count) {
378 retval = 0;
379 goto errout;
380 }
381
382 /*
383 * Fix the count of the last (old) block group
384 */
385 old_numblocks = (old_fs->super->s_blocks_count -
386 old_fs->super->s_first_data_block) %
387 old_fs->super->s_blocks_per_group;
388 if (!old_numblocks)
389 old_numblocks = old_fs->super->s_blocks_per_group;
390 if (old_fs->group_desc_count == fs->group_desc_count) {
391 numblocks = (fs->super->s_blocks_count -
392 fs->super->s_first_data_block) %
393 fs->super->s_blocks_per_group;
394 if (!numblocks)
395 numblocks = fs->super->s_blocks_per_group;
396 } else
397 numblocks = fs->super->s_blocks_per_group;
398 i = old_fs->group_desc_count - 1;
399 fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
400 ext2fs_group_desc_csum_set(fs, i);
401
402 /*
403 * If the number of block groups is staying the same, we're
404 * done and can exit now. (If the number block groups is
405 * shrinking, we had exited earlier.)
406 */
407 if (old_fs->group_desc_count >= fs->group_desc_count) {
408 retval = 0;
409 goto errout;
410 }
411
412 /*
413 * Initialize the new block group descriptors
414 */
415 group_block = fs->super->s_first_data_block +
416 old_fs->group_desc_count * fs->super->s_blocks_per_group;
417
418 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
419 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
420 adj = old_fs->group_desc_count;
421 max_group = fs->group_desc_count - adj;
422 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
423 old_desc_blocks = fs->super->s_first_meta_bg;
424 else
425 old_desc_blocks = fs->desc_blocks +
426 fs->super->s_reserved_gdt_blocks;
427 for (i = old_fs->group_desc_count;
428 i < fs->group_desc_count; i++) {
429 memset(&fs->group_desc[i], 0,
430 sizeof(struct ext2_group_desc));
431 adjblocks = 0;
432
433 fs->group_desc[i].bg_flags = 0;
434 if (csum_flag)
435 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_UNINIT |
436 EXT2_BG_INODE_ZEROED;
437 if (i == fs->group_desc_count-1) {
438 numblocks = (fs->super->s_blocks_count -
439 fs->super->s_first_data_block) %
440 fs->super->s_blocks_per_group;
441 if (!numblocks)
442 numblocks = fs->super->s_blocks_per_group;
443 } else {
444 numblocks = fs->super->s_blocks_per_group;
445 if (csum_flag)
446 fs->group_desc[i].bg_flags |=
447 EXT2_BG_BLOCK_UNINIT;
448 }
449
450 has_super = ext2fs_bg_has_super(fs, i);
451 if (has_super) {
452 ext2fs_block_alloc_stats(fs, group_block, +1);
453 adjblocks++;
454 }
455 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
456 meta_bg = i / meta_bg_size;
457 if (!(fs->super->s_feature_incompat &
458 EXT2_FEATURE_INCOMPAT_META_BG) ||
459 (meta_bg < fs->super->s_first_meta_bg)) {
460 if (has_super) {
461 for (j=0; j < old_desc_blocks; j++)
462 ext2fs_block_alloc_stats(fs,
463 group_block + 1 + j, +1);
464 adjblocks += old_desc_blocks;
465 }
466 } else {
467 if (has_super)
468 has_super = 1;
469 if (((i % meta_bg_size) == 0) ||
470 ((i % meta_bg_size) == 1) ||
471 ((i % meta_bg_size) == (meta_bg_size-1)))
472 ext2fs_block_alloc_stats(fs,
473 group_block + has_super, +1);
474 }
475
476 adjblocks += 2 + fs->inode_blocks_per_group;
477
478 numblocks -= adjblocks;
479 fs->super->s_free_blocks_count -= adjblocks;
480 fs->super->s_free_inodes_count +=
481 fs->super->s_inodes_per_group;
482 fs->group_desc[i].bg_free_blocks_count = numblocks;
483 fs->group_desc[i].bg_free_inodes_count =
484 fs->super->s_inodes_per_group;
485 fs->group_desc[i].bg_used_dirs_count = 0;
486 ext2fs_group_desc_csum_set(fs, i);
487
488 retval = ext2fs_allocate_group_table(fs, i, 0);
489 if (retval) goto errout;
490
491 group_block += fs->super->s_blocks_per_group;
492 }
493 retval = 0;
494
495 errout:
496 return (retval);
497 }
498
499 /*
500 * This routine adjusts the superblock and other data structures, both
501 * in disk as well as in memory...
502 */
503 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
504 {
505 ext2_filsys fs;
506 int adj = 0;
507 errcode_t retval;
508 blk_t group_block;
509 unsigned long i;
510 unsigned long max_group;
511
512 fs = rfs->new_fs;
513 ext2fs_mark_super_dirty(fs);
514 ext2fs_mark_bb_dirty(fs);
515 ext2fs_mark_ib_dirty(fs);
516
517 retval = adjust_fs_info(fs, rfs->old_fs, new_size);
518 if (retval)
519 goto errout;
520
521 /*
522 * Check to make sure there are enough inodes
523 */
524 if ((rfs->old_fs->super->s_inodes_count -
525 rfs->old_fs->super->s_free_inodes_count) >
526 rfs->new_fs->super->s_inodes_count) {
527 retval = ENOSPC;
528 goto errout;
529 }
530
531 /*
532 * If we are shrinking the number block groups, we're done and
533 * can exit now.
534 */
535 if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
536 retval = 0;
537 goto errout;
538 }
539
540 /*
541 * If the number of block groups is staying the same, we're
542 * done and can exit now. (If the number block groups is
543 * shrinking, we had exited earlier.)
544 */
545 if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
546 retval = 0;
547 goto errout;
548 }
549
550 /*
551 * Initialize the new block group descriptors
552 */
553 retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
554 &rfs->itable_buf);
555 if (retval)
556 goto errout;
557
558 memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
559 group_block = fs->super->s_first_data_block +
560 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
561
562 adj = rfs->old_fs->group_desc_count;
563 max_group = fs->group_desc_count - adj;
564 if (rfs->progress) {
565 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
566 0, max_group);
567 if (retval)
568 goto errout;
569 }
570 for (i = rfs->old_fs->group_desc_count;
571 i < fs->group_desc_count; i++) {
572 /*
573 * Write out the new inode table
574 */
575 retval = io_channel_write_blk(fs->io,
576 fs->group_desc[i].bg_inode_table,
577 fs->inode_blocks_per_group,
578 rfs->itable_buf);
579 if (retval) goto errout;
580
581 io_channel_flush(fs->io);
582 if (rfs->progress) {
583 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
584 i - adj + 1, max_group);
585 if (retval)
586 goto errout;
587 }
588 group_block += fs->super->s_blocks_per_group;
589 }
590 io_channel_flush(fs->io);
591 retval = 0;
592
593 errout:
594 return retval;
595 }
596
597 /* --------------------------------------------------------------------
598 *
599 * Resize processing, phase 2.
600 *
601 * In this phase we adjust determine which blocks need to be moved, in
602 * blocks_to_move(). We then copy the blocks to their ultimate new
603 * destinations using block_mover(). Since we are copying blocks to
604 * their new locations, again during this pass we can abort without
605 * any problems.
606 * --------------------------------------------------------------------
607 */
608
609 /*
610 * This helper function creates a block bitmap with all of the
611 * filesystem meta-data blocks.
612 */
613 static errcode_t mark_table_blocks(ext2_filsys fs,
614 ext2fs_block_bitmap bmap)
615 {
616 blk_t b;
617 unsigned int j;
618 dgrp_t i;
619 unsigned long meta_bg_size;
620 unsigned int old_desc_blocks;
621
622 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
623 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
624 old_desc_blocks = fs->super->s_first_meta_bg;
625 else
626 old_desc_blocks = fs->desc_blocks +
627 fs->super->s_reserved_gdt_blocks;
628 for (i = 0; i < fs->group_desc_count; i++) {
629 ext2fs_reserve_super_and_bgd(fs, i, bmap);
630
631 /*
632 * Mark the blocks used for the inode table
633 */
634 for (j = 0, b = fs->group_desc[i].bg_inode_table;
635 j < (unsigned int) fs->inode_blocks_per_group;
636 j++, b++)
637 ext2fs_mark_block_bitmap(bmap, b);
638
639 /*
640 * Mark block used for the block bitmap
641 */
642 ext2fs_mark_block_bitmap(bmap,
643 fs->group_desc[i].bg_block_bitmap);
644
645 /*
646 * Mark block used for the inode bitmap
647 */
648 ext2fs_mark_block_bitmap(bmap,
649 fs->group_desc[i].bg_inode_bitmap);
650 }
651 return 0;
652 }
653
654 /*
655 * This function checks to see if a particular block (either a
656 * superblock or a block group descriptor) overlaps with an inode or
657 * block bitmap block, or with the inode table.
658 */
659 static void mark_fs_metablock(ext2_resize_t rfs,
660 ext2fs_block_bitmap meta_bmap,
661 int group, blk_t blk)
662 {
663 ext2_filsys fs = rfs->new_fs;
664
665 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
666 ext2fs_block_alloc_stats(fs, blk, +1);
667
668 /*
669 * Check to see if we overlap with the inode or block bitmap,
670 * or the inode tables. If not, and the block is in use, then
671 * mark it as a block to be moved.
672 */
673 if (IS_BLOCK_BM(fs, group, blk)) {
674 FS_BLOCK_BM(fs, group) = 0;
675 rfs->needed_blocks++;
676 } else if (IS_INODE_BM(fs, group, blk)) {
677 FS_INODE_BM(fs, group) = 0;
678 rfs->needed_blocks++;
679 } else if (IS_INODE_TB(fs, group, blk)) {
680 FS_INODE_TB(fs, group) = 0;
681 rfs->needed_blocks++;
682 } else if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
683 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
684 (fs->group_desc[group].bg_flags & EXT2_BG_BLOCK_UNINIT)) {
685 /*
686 * If the block bitmap is uninitialized, which means
687 * nothing other than standard metadata in use.
688 */
689 return;
690 } else if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk) &&
691 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
692 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
693 rfs->needed_blocks++;
694 }
695 }
696
697
698 /*
699 * This routine marks and unmarks reserved blocks in the new block
700 * bitmap. It also determines which blocks need to be moved and
701 * places this information into the move_blocks bitmap.
702 */
703 static errcode_t blocks_to_move(ext2_resize_t rfs)
704 {
705 int j, has_super;
706 dgrp_t i, max_groups, g;
707 blk_t blk, group_blk;
708 unsigned long old_blocks, new_blocks;
709 unsigned int meta_bg, meta_bg_size;
710 errcode_t retval;
711 ext2_filsys fs, old_fs;
712 ext2fs_block_bitmap meta_bmap;
713
714 fs = rfs->new_fs;
715 old_fs = rfs->old_fs;
716 if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
717 fs = rfs->old_fs;
718
719 retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
720 &rfs->reserve_blocks);
721 if (retval)
722 return retval;
723
724 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
725 &rfs->move_blocks);
726 if (retval)
727 return retval;
728
729 retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
730 &meta_bmap);
731 if (retval)
732 return retval;
733
734 retval = mark_table_blocks(old_fs, meta_bmap);
735 if (retval)
736 return retval;
737
738 fs = rfs->new_fs;
739
740 /*
741 * If we're shrinking the filesystem, we need to move all of
742 * the blocks that don't fit any more
743 */
744 for (blk = fs->super->s_blocks_count;
745 blk < old_fs->super->s_blocks_count; blk++) {
746 g = ext2fs_group_of_blk(fs, blk);
747 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
748 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
749 (fs->group_desc[g].bg_flags & EXT2_BG_BLOCK_UNINIT)) {
750 /*
751 * The block bitmap is uninitialized, so skip
752 * to the next block group.
753 */
754 blk = ((g+1) * fs->super->s_blocks_per_group) +
755 fs->super->s_first_data_block;
756 continue;
757 }
758 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
759 !ext2fs_test_block_bitmap(meta_bmap, blk)) {
760 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
761 rfs->needed_blocks++;
762 }
763 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
764 }
765
766 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
767 old_blocks = old_fs->super->s_first_meta_bg;
768 new_blocks = fs->super->s_first_meta_bg;
769 } else {
770 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
771 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
772 }
773
774 if (old_blocks == new_blocks) {
775 retval = 0;
776 goto errout;
777 }
778
779 max_groups = fs->group_desc_count;
780 if (max_groups > old_fs->group_desc_count)
781 max_groups = old_fs->group_desc_count;
782 group_blk = old_fs->super->s_first_data_block;
783 /*
784 * If we're reducing the number of descriptor blocks, this
785 * makes life easy. :-) We just have to mark some extra
786 * blocks as free.
787 */
788 if (old_blocks > new_blocks) {
789 for (i = 0; i < max_groups; i++) {
790 if (!ext2fs_bg_has_super(fs, i)) {
791 group_blk += fs->super->s_blocks_per_group;
792 continue;
793 }
794 for (blk = group_blk+1+new_blocks;
795 blk < group_blk+1+old_blocks; blk++) {
796 ext2fs_block_alloc_stats(fs, blk, -1);
797 rfs->needed_blocks--;
798 }
799 group_blk += fs->super->s_blocks_per_group;
800 }
801 retval = 0;
802 goto errout;
803 }
804 /*
805 * If we're increasing the number of descriptor blocks, life
806 * gets interesting....
807 */
808 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
809 for (i = 0; i < max_groups; i++) {
810 has_super = ext2fs_bg_has_super(fs, i);
811 if (has_super)
812 mark_fs_metablock(rfs, meta_bmap, i, group_blk);
813
814 meta_bg = i / meta_bg_size;
815 if (!(fs->super->s_feature_incompat &
816 EXT2_FEATURE_INCOMPAT_META_BG) ||
817 (meta_bg < fs->super->s_first_meta_bg)) {
818 if (has_super) {
819 for (blk = group_blk+1;
820 blk < group_blk + 1 + new_blocks; blk++)
821 mark_fs_metablock(rfs, meta_bmap,
822 i, blk);
823 }
824 } else {
825 if (has_super)
826 has_super = 1;
827 if (((i % meta_bg_size) == 0) ||
828 ((i % meta_bg_size) == 1) ||
829 ((i % meta_bg_size) == (meta_bg_size-1)))
830 mark_fs_metablock(rfs, meta_bmap, i,
831 group_blk + has_super);
832 }
833
834 if (fs->group_desc[i].bg_inode_table &&
835 fs->group_desc[i].bg_inode_bitmap &&
836 fs->group_desc[i].bg_block_bitmap)
837 goto next_group;
838
839 /*
840 * Reserve the existing meta blocks that we know
841 * aren't to be moved.
842 */
843 if (fs->group_desc[i].bg_block_bitmap)
844 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
845 fs->group_desc[i].bg_block_bitmap);
846 if (fs->group_desc[i].bg_inode_bitmap)
847 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
848 fs->group_desc[i].bg_inode_bitmap);
849 if (fs->group_desc[i].bg_inode_table)
850 for (blk = fs->group_desc[i].bg_inode_table, j=0;
851 j < fs->inode_blocks_per_group ; j++, blk++)
852 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
853 blk);
854
855 /*
856 * Allocate the missing data structures
857 */
858 retval = ext2fs_allocate_group_table(fs, i,
859 rfs->reserve_blocks);
860 if (retval)
861 goto errout;
862
863 /*
864 * For those structures that have changed, we need to
865 * do bookkeepping.
866 */
867 if (FS_BLOCK_BM(old_fs, i) !=
868 (blk = FS_BLOCK_BM(fs, i))) {
869 ext2fs_block_alloc_stats(fs, blk, +1);
870 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
871 !ext2fs_test_block_bitmap(meta_bmap, blk))
872 ext2fs_mark_block_bitmap(rfs->move_blocks,
873 blk);
874 }
875 if (FS_INODE_BM(old_fs, i) !=
876 (blk = FS_INODE_BM(fs, i))) {
877 ext2fs_block_alloc_stats(fs, blk, +1);
878 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
879 !ext2fs_test_block_bitmap(meta_bmap, blk))
880 ext2fs_mark_block_bitmap(rfs->move_blocks,
881 blk);
882 }
883
884 /*
885 * The inode table, if we need to relocate it, is
886 * handled specially. We have to reserve the blocks
887 * for both the old and the new inode table, since we
888 * can't have the inode table be destroyed during the
889 * block relocation phase.
890 */
891 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
892 goto next_group; /* inode table not moved */
893
894 rfs->needed_blocks += fs->inode_blocks_per_group;
895
896 /*
897 * Mark the new inode table as in use in the new block
898 * allocation bitmap, and move any blocks that might
899 * be necessary.
900 */
901 for (blk = fs->group_desc[i].bg_inode_table, j=0;
902 j < fs->inode_blocks_per_group ; j++, blk++) {
903 ext2fs_block_alloc_stats(fs, blk, +1);
904 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
905 !ext2fs_test_block_bitmap(meta_bmap, blk))
906 ext2fs_mark_block_bitmap(rfs->move_blocks,
907 blk);
908 }
909
910 /*
911 * Make sure the old inode table is reserved in the
912 * block reservation bitmap.
913 */
914 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
915 j < fs->inode_blocks_per_group ; j++, blk++)
916 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
917
918 next_group:
919 group_blk += rfs->new_fs->super->s_blocks_per_group;
920 }
921 retval = 0;
922
923 errout:
924 if (meta_bmap)
925 ext2fs_free_block_bitmap(meta_bmap);
926
927 return retval;
928 }
929
930 /*
931 * This helper function tries to allocate a new block. We try to
932 * avoid hitting the original group descriptor blocks at least at
933 * first, since we want to make it possible to recover from a badly
934 * aborted resize operation as much as possible.
935 *
936 * In the future, I may further modify this routine to balance out
937 * where we get the new blocks across the various block groups.
938 * Ideally we would allocate blocks that corresponded with the block
939 * group of the containing inode, and keep contiguous blocks
940 * together. However, this very difficult to do efficiently, since we
941 * don't have the necessary information up front.
942 */
943
944 #define AVOID_OLD 1
945 #define DESPERATION 2
946
947 static void init_block_alloc(ext2_resize_t rfs)
948 {
949 rfs->alloc_state = AVOID_OLD;
950 rfs->new_blk = rfs->new_fs->super->s_first_data_block;
951 #if 0
952 /* HACK for testing */
953 if (rfs->new_fs->super->s_blocks_count >
954 rfs->old_fs->super->s_blocks_count)
955 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
956 #endif
957 }
958
959 static blk_t get_new_block(ext2_resize_t rfs)
960 {
961 ext2_filsys fs = rfs->new_fs;
962
963 while (1) {
964 if (rfs->new_blk >= fs->super->s_blocks_count) {
965 if (rfs->alloc_state == DESPERATION)
966 return 0;
967
968 #ifdef RESIZE2FS_DEBUG
969 if (rfs->flags & RESIZE_DEBUG_BMOVE)
970 printf("Going into desperation mode "
971 "for block allocations\n");
972 #endif
973 rfs->alloc_state = DESPERATION;
974 rfs->new_blk = fs->super->s_first_data_block;
975 continue;
976 }
977 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
978 ext2fs_test_block_bitmap(rfs->reserve_blocks,
979 rfs->new_blk) ||
980 ((rfs->alloc_state == AVOID_OLD) &&
981 (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
982 ext2fs_test_block_bitmap(rfs->old_fs->block_map,
983 rfs->new_blk))) {
984 rfs->new_blk++;
985 continue;
986 }
987 return rfs->new_blk;
988 }
989 }
990
991 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
992 blk64_t *ret)
993 {
994 ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
995 blk_t blk;
996
997 blk = get_new_block(rfs);
998 if (!blk)
999 return ENOSPC;
1000
1001 #ifdef RESIZE2FS_DEBUG
1002 if (rfs->flags & 0xF)
1003 printf("get_alloc_block allocating %u\n", blk);
1004 #endif
1005
1006 ext2fs_mark_block_bitmap(rfs->old_fs->block_map, blk);
1007 ext2fs_mark_block_bitmap(rfs->new_fs->block_map, blk);
1008 *ret = (blk64_t) blk;
1009 return 0;
1010 }
1011
1012 static errcode_t block_mover(ext2_resize_t rfs)
1013 {
1014 blk_t blk, old_blk, new_blk;
1015 ext2_filsys fs = rfs->new_fs;
1016 ext2_filsys old_fs = rfs->old_fs;
1017 errcode_t retval;
1018 int size, c;
1019 int to_move, moved;
1020 ext2_badblocks_list badblock_list = 0;
1021 int bb_modified = 0;
1022
1023 fs->get_alloc_block = resize2fs_get_alloc_block;
1024 old_fs->get_alloc_block = resize2fs_get_alloc_block;
1025
1026 retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1027 if (retval)
1028 return retval;
1029
1030 new_blk = fs->super->s_first_data_block;
1031 if (!rfs->itable_buf) {
1032 retval = ext2fs_get_array(fs->blocksize,
1033 fs->inode_blocks_per_group,
1034 &rfs->itable_buf);
1035 if (retval)
1036 return retval;
1037 }
1038 retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1039 if (retval)
1040 return retval;
1041
1042 /*
1043 * The first step is to figure out where all of the blocks
1044 * will go.
1045 */
1046 to_move = moved = 0;
1047 init_block_alloc(rfs);
1048 for (blk = old_fs->super->s_first_data_block;
1049 blk < old_fs->super->s_blocks_count; blk++) {
1050 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
1051 continue;
1052 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
1053 continue;
1054 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1055 ext2fs_badblocks_list_del(badblock_list, blk);
1056 bb_modified++;
1057 continue;
1058 }
1059
1060 new_blk = get_new_block(rfs);
1061 if (!new_blk) {
1062 retval = ENOSPC;
1063 goto errout;
1064 }
1065 ext2fs_block_alloc_stats(fs, new_blk, +1);
1066 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
1067 to_move++;
1068 }
1069
1070 if (to_move == 0) {
1071 if (rfs->bmap) {
1072 ext2fs_free_extent_table(rfs->bmap);
1073 rfs->bmap = 0;
1074 }
1075 retval = 0;
1076 goto errout;
1077 }
1078
1079 /*
1080 * Step two is to actually move the blocks
1081 */
1082 retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1083 if (retval) goto errout;
1084
1085 if (rfs->progress) {
1086 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1087 0, to_move);
1088 if (retval)
1089 goto errout;
1090 }
1091 while (1) {
1092 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1093 if (retval) goto errout;
1094 if (!size)
1095 break;
1096 #ifdef RESIZE2FS_DEBUG
1097 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1098 printf("Moving %d blocks %u->%u\n",
1099 size, old_blk, new_blk);
1100 #endif
1101 do {
1102 c = size;
1103 if (c > fs->inode_blocks_per_group)
1104 c = fs->inode_blocks_per_group;
1105 retval = io_channel_read_blk(fs->io, old_blk, c,
1106 rfs->itable_buf);
1107 if (retval) goto errout;
1108 retval = io_channel_write_blk(fs->io, new_blk, c,
1109 rfs->itable_buf);
1110 if (retval) goto errout;
1111 size -= c;
1112 new_blk += c;
1113 old_blk += c;
1114 moved += c;
1115 if (rfs->progress) {
1116 io_channel_flush(fs->io);
1117 retval = (rfs->progress)(rfs,
1118 E2_RSZ_BLOCK_RELOC_PASS,
1119 moved, to_move);
1120 if (retval)
1121 goto errout;
1122 }
1123 } while (size > 0);
1124 io_channel_flush(fs->io);
1125 }
1126
1127 errout:
1128 if (badblock_list) {
1129 if (!retval && bb_modified)
1130 retval = ext2fs_update_bb_inode(old_fs,
1131 badblock_list);
1132 ext2fs_badblocks_list_free(badblock_list);
1133 }
1134 return retval;
1135 }
1136
1137
1138 /* --------------------------------------------------------------------
1139 *
1140 * Resize processing, phase 3
1141 *
1142 * --------------------------------------------------------------------
1143 */
1144
1145
1146 struct process_block_struct {
1147 ext2_resize_t rfs;
1148 ext2_ino_t ino;
1149 struct ext2_inode * inode;
1150 errcode_t error;
1151 int is_dir;
1152 int changed;
1153 };
1154
1155 static int process_block(ext2_filsys fs, blk_t *block_nr,
1156 e2_blkcnt_t blockcnt,
1157 blk_t ref_block EXT2FS_ATTR((unused)),
1158 int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1159 {
1160 struct process_block_struct *pb;
1161 errcode_t retval;
1162 blk_t block, new_block;
1163 int ret = 0;
1164
1165 pb = (struct process_block_struct *) priv_data;
1166 block = *block_nr;
1167 if (pb->rfs->bmap) {
1168 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
1169 if (new_block) {
1170 *block_nr = new_block;
1171 ret |= BLOCK_CHANGED;
1172 pb->changed = 1;
1173 #ifdef RESIZE2FS_DEBUG
1174 if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1175 printf("ino=%u, blockcnt=%lld, %u->%u\n",
1176 pb->ino, blockcnt, block, new_block);
1177 #endif
1178 block = new_block;
1179 }
1180 }
1181 if (pb->is_dir) {
1182 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
1183 block, (int) blockcnt);
1184 if (retval) {
1185 pb->error = retval;
1186 ret |= BLOCK_ABORT;
1187 }
1188 }
1189 return ret;
1190 }
1191
1192 /*
1193 * Progress callback
1194 */
1195 static errcode_t progress_callback(ext2_filsys fs,
1196 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1197 dgrp_t group, void * priv_data)
1198 {
1199 ext2_resize_t rfs = (ext2_resize_t) priv_data;
1200 errcode_t retval;
1201
1202 /*
1203 * This check is to protect against old ext2 libraries. It
1204 * shouldn't be needed against new libraries.
1205 */
1206 if ((group+1) == 0)
1207 return 0;
1208
1209 if (rfs->progress) {
1210 io_channel_flush(fs->io);
1211 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1212 group+1, fs->group_desc_count);
1213 if (retval)
1214 return retval;
1215 }
1216
1217 return 0;
1218 }
1219
1220 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1221 {
1222 struct process_block_struct pb;
1223 ext2_ino_t ino, new_inode;
1224 struct ext2_inode *inode = NULL;
1225 ext2_inode_scan scan = NULL;
1226 errcode_t retval;
1227 char *block_buf = 0;
1228 ext2_ino_t start_to_move;
1229 blk_t orig_size, new_block;
1230 int inode_size;
1231
1232 if ((rfs->old_fs->group_desc_count <=
1233 rfs->new_fs->group_desc_count) &&
1234 !rfs->bmap)
1235 return 0;
1236
1237 /*
1238 * Save the original size of the old filesystem, and
1239 * temporarily set the size to be the new size if the new size
1240 * is larger. We need to do this to avoid catching an error
1241 * by the block iterator routines
1242 */
1243 orig_size = rfs->old_fs->super->s_blocks_count;
1244 if (orig_size < rfs->new_fs->super->s_blocks_count)
1245 rfs->old_fs->super->s_blocks_count =
1246 rfs->new_fs->super->s_blocks_count;
1247
1248 retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1249 if (retval) goto errout;
1250
1251 retval = ext2fs_init_dblist(rfs->old_fs, 0);
1252 if (retval) goto errout;
1253 retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1254 if (retval) goto errout;
1255
1256 start_to_move = (rfs->new_fs->group_desc_count *
1257 rfs->new_fs->super->s_inodes_per_group);
1258
1259 if (rfs->progress) {
1260 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1261 0, rfs->old_fs->group_desc_count);
1262 if (retval)
1263 goto errout;
1264 }
1265 ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1266 pb.rfs = rfs;
1267 pb.inode = inode;
1268 pb.error = 0;
1269 new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1270 inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1271 inode = malloc(inode_size);
1272 if (!inode) {
1273 retval = ENOMEM;
1274 goto errout;
1275 }
1276 /*
1277 * First, copy all of the inodes that need to be moved
1278 * elsewhere in the inode table
1279 */
1280 while (1) {
1281 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1282 if (retval) goto errout;
1283 if (!ino)
1284 break;
1285
1286 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1287 continue; /* inode not in use */
1288
1289 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1290 pb.changed = 0;
1291
1292 if (inode->i_file_acl && rfs->bmap) {
1293 new_block = ext2fs_extent_translate(rfs->bmap,
1294 inode->i_file_acl);
1295 if (new_block) {
1296 inode->i_file_acl = new_block;
1297 retval = ext2fs_write_inode_full(rfs->old_fs,
1298 ino, inode, inode_size);
1299 if (retval) goto errout;
1300 }
1301 }
1302
1303 if (ext2fs_inode_has_valid_blocks(inode) &&
1304 (rfs->bmap || pb.is_dir)) {
1305 pb.ino = ino;
1306 retval = ext2fs_block_iterate2(rfs->old_fs,
1307 ino, 0, block_buf,
1308 process_block, &pb);
1309 if (retval)
1310 goto errout;
1311 if (pb.error) {
1312 retval = pb.error;
1313 goto errout;
1314 }
1315 }
1316
1317 if (ino <= start_to_move)
1318 continue; /* Don't need to move it. */
1319
1320 /*
1321 * Find a new inode
1322 */
1323 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1324 if (retval)
1325 goto errout;
1326
1327 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1328 pb.is_dir);
1329 if (pb.changed) {
1330 /* Get the new version of the inode */
1331 retval = ext2fs_read_inode_full(rfs->old_fs, ino,
1332 inode, inode_size);
1333 if (retval) goto errout;
1334 }
1335 inode->i_ctime = time(0);
1336 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1337 inode, inode_size);
1338 if (retval) goto errout;
1339
1340 #ifdef RESIZE2FS_DEBUG
1341 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1342 printf("Inode moved %u->%u\n", ino, new_inode);
1343 #endif
1344 if (!rfs->imap) {
1345 retval = ext2fs_create_extent_table(&rfs->imap, 0);
1346 if (retval)
1347 goto errout;
1348 }
1349 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1350 }
1351 io_channel_flush(rfs->old_fs->io);
1352
1353 errout:
1354 rfs->old_fs->super->s_blocks_count = orig_size;
1355 if (rfs->bmap) {
1356 ext2fs_free_extent_table(rfs->bmap);
1357 rfs->bmap = 0;
1358 }
1359 if (scan)
1360 ext2fs_close_inode_scan(scan);
1361 if (block_buf)
1362 ext2fs_free_mem(&block_buf);
1363 if (inode)
1364 free(inode);
1365 return retval;
1366 }
1367
1368 /* --------------------------------------------------------------------
1369 *
1370 * Resize processing, phase 4.
1371 *
1372 * --------------------------------------------------------------------
1373 */
1374
1375 struct istruct {
1376 ext2_resize_t rfs;
1377 errcode_t err;
1378 unsigned long max_dirs;
1379 int num;
1380 };
1381
1382 static int check_and_change_inodes(ext2_ino_t dir,
1383 int entry EXT2FS_ATTR((unused)),
1384 struct ext2_dir_entry *dirent, int offset,
1385 int blocksize EXT2FS_ATTR((unused)),
1386 char *buf EXT2FS_ATTR((unused)),
1387 void *priv_data)
1388 {
1389 struct istruct *is = (struct istruct *) priv_data;
1390 struct ext2_inode inode;
1391 ext2_ino_t new_inode;
1392 errcode_t retval;
1393
1394 if (is->rfs->progress && offset == 0) {
1395 io_channel_flush(is->rfs->old_fs->io);
1396 is->err = (is->rfs->progress)(is->rfs,
1397 E2_RSZ_INODE_REF_UPD_PASS,
1398 ++is->num, is->max_dirs);
1399 if (is->err)
1400 return DIRENT_ABORT;
1401 }
1402
1403 if (!dirent->inode)
1404 return 0;
1405
1406 new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1407
1408 if (!new_inode)
1409 return 0;
1410 #ifdef RESIZE2FS_DEBUG
1411 if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1412 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1413 dir, dirent->name_len&0xFF, dirent->name,
1414 dirent->inode, new_inode);
1415 #endif
1416
1417 dirent->inode = new_inode;
1418
1419 /* Update the directory mtime and ctime */
1420 retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1421 if (retval == 0) {
1422 inode.i_mtime = inode.i_ctime = time(0);
1423 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1424 if (is->err)
1425 return DIRENT_ABORT;
1426 }
1427
1428 return DIRENT_CHANGED;
1429 }
1430
1431 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1432 {
1433 errcode_t retval;
1434 struct istruct is;
1435
1436 if (!rfs->imap)
1437 return 0;
1438
1439 /*
1440 * Now, we iterate over all of the directories to update the
1441 * inode references
1442 */
1443 is.num = 0;
1444 is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1445 is.rfs = rfs;
1446 is.err = 0;
1447
1448 if (rfs->progress) {
1449 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1450 0, is.max_dirs);
1451 if (retval)
1452 goto errout;
1453 }
1454
1455 retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1456 DIRENT_FLAG_INCLUDE_EMPTY, 0,
1457 check_and_change_inodes, &is);
1458 if (retval)
1459 goto errout;
1460 if (is.err) {
1461 retval = is.err;
1462 goto errout;
1463 }
1464
1465 if (rfs->progress && (is.num < is.max_dirs))
1466 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1467 is.max_dirs, is.max_dirs);
1468
1469 errout:
1470 ext2fs_free_extent_table(rfs->imap);
1471 rfs->imap = 0;
1472 return retval;
1473 }
1474
1475
1476 /* --------------------------------------------------------------------
1477 *
1478 * Resize processing, phase 5.
1479 *
1480 * In this phase we actually move the inode table around, and then
1481 * update the summary statistics. This is scary, since aborting here
1482 * will potentially scramble the filesystem. (We are moving the
1483 * inode tables around in place, and so the potential for lost data,
1484 * or at the very least scrambling the mapping between filenames and
1485 * inode numbers is very high in case of a power failure here.)
1486 * --------------------------------------------------------------------
1487 */
1488
1489
1490 /*
1491 * A very scary routine --- this one moves the inode table around!!!
1492 *
1493 * After this you have to use the rfs->new_fs file handle to read and
1494 * write inodes.
1495 */
1496 static errcode_t move_itables(ext2_resize_t rfs)
1497 {
1498 int n, num, size, diff;
1499 dgrp_t i, max_groups;
1500 ext2_filsys fs = rfs->new_fs;
1501 char *cp;
1502 blk_t old_blk, new_blk, blk;
1503 errcode_t retval;
1504 int j, to_move, moved;
1505
1506 max_groups = fs->group_desc_count;
1507 if (max_groups > rfs->old_fs->group_desc_count)
1508 max_groups = rfs->old_fs->group_desc_count;
1509
1510 size = fs->blocksize * fs->inode_blocks_per_group;
1511 if (!rfs->itable_buf) {
1512 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1513 if (retval)
1514 return retval;
1515 }
1516
1517 /*
1518 * Figure out how many inode tables we need to move
1519 */
1520 to_move = moved = 0;
1521 for (i=0; i < max_groups; i++)
1522 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1523 fs->group_desc[i].bg_inode_table)
1524 to_move++;
1525
1526 if (to_move == 0)
1527 return 0;
1528
1529 if (rfs->progress) {
1530 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1531 0, to_move);
1532 if (retval)
1533 goto errout;
1534 }
1535
1536 rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1537
1538 for (i=0; i < max_groups; i++) {
1539 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1540 new_blk = fs->group_desc[i].bg_inode_table;
1541 diff = new_blk - old_blk;
1542
1543 #ifdef RESIZE2FS_DEBUG
1544 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1545 printf("Itable move group %d block %u->%u (diff %d)\n",
1546 i, old_blk, new_blk, diff);
1547 #endif
1548
1549 if (!diff)
1550 continue;
1551
1552 retval = io_channel_read_blk(fs->io, old_blk,
1553 fs->inode_blocks_per_group,
1554 rfs->itable_buf);
1555 if (retval)
1556 goto errout;
1557 /*
1558 * The end of the inode table segment often contains
1559 * all zeros, and we're often only moving the inode
1560 * table down a block or two. If so, we can optimize
1561 * things by not rewriting blocks that we know to be zero
1562 * already.
1563 */
1564 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1565 if (*cp)
1566 break;
1567 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1568 #ifdef RESIZE2FS_DEBUG
1569 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1570 printf("%d blocks of zeros...\n", n);
1571 #endif
1572 num = fs->inode_blocks_per_group;
1573 if (n > diff)
1574 num -= n;
1575
1576 retval = io_channel_write_blk(fs->io, new_blk,
1577 num, rfs->itable_buf);
1578 if (retval) {
1579 io_channel_write_blk(fs->io, old_blk,
1580 num, rfs->itable_buf);
1581 goto errout;
1582 }
1583 if (n > diff) {
1584 retval = io_channel_write_blk(fs->io,
1585 old_blk + fs->inode_blocks_per_group,
1586 diff, (rfs->itable_buf +
1587 (fs->inode_blocks_per_group - diff) *
1588 fs->blocksize));
1589 if (retval)
1590 goto errout;
1591 }
1592
1593 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1594 j < fs->inode_blocks_per_group ; j++, blk++)
1595 ext2fs_block_alloc_stats(fs, blk, -1);
1596
1597 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1598 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1599 ext2fs_mark_super_dirty(rfs->old_fs);
1600 ext2fs_flush(rfs->old_fs);
1601
1602 if (rfs->progress) {
1603 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1604 ++moved, to_move);
1605 if (retval)
1606 goto errout;
1607 }
1608 }
1609 mark_table_blocks(fs, fs->block_map);
1610 ext2fs_flush(fs);
1611 #ifdef RESIZE2FS_DEBUG
1612 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1613 printf("Inode table move finished.\n");
1614 #endif
1615 return 0;
1616
1617 errout:
1618 return retval;
1619 }
1620
1621 /*
1622 * Fix the resize inode
1623 */
1624 static errcode_t fix_resize_inode(ext2_filsys fs)
1625 {
1626 struct ext2_inode inode;
1627 errcode_t retval;
1628 char * block_buf;
1629 blk_t blk;
1630
1631 if (!(fs->super->s_feature_compat &
1632 EXT2_FEATURE_COMPAT_RESIZE_INODE))
1633 return 0;
1634
1635 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1636 if (retval) goto errout;
1637
1638 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1639 if (retval) goto errout;
1640
1641 if (fs->super->s_reserved_gdt_blocks == 0) {
1642 fs->super->s_feature_compat &=
1643 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1644 ext2fs_mark_super_dirty(fs);
1645
1646 if ((blk = inode.i_block[EXT2_DIND_BLOCK]) != 0)
1647 ext2fs_block_alloc_stats(fs, blk, -1);
1648
1649 memset(&inode, 0, sizeof(inode));
1650
1651 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1652 goto errout;
1653 }
1654
1655 ext2fs_iblk_set(fs, &inode, 1);
1656
1657 retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1658 if (retval) goto errout;
1659
1660 if (!inode.i_block[EXT2_DIND_BLOCK]) {
1661 /*
1662 * Avoid zeroing out block #0; that's rude. This
1663 * should never happen anyway since the filesystem
1664 * should be fsck'ed and we assume it is consistent.
1665 */
1666 fprintf(stderr,
1667 _("Should never happen: resize inode corrupt!\n"));
1668 exit(1);
1669 }
1670
1671 memset(block_buf, 0, fs->blocksize);
1672
1673 retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1674 1, block_buf);
1675 if (retval) goto errout;
1676
1677 retval = ext2fs_create_resize_inode(fs);
1678 if (retval)
1679 goto errout;
1680
1681 errout:
1682 if (block_buf)
1683 ext2fs_free_mem(&block_buf);
1684 return retval;
1685 }
1686
1687 /*
1688 * Finally, recalculate the summary information
1689 */
1690 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1691 {
1692 blk_t blk;
1693 ext2_ino_t ino;
1694 unsigned int group = 0;
1695 unsigned int count = 0;
1696 int total_free = 0;
1697 int group_free = 0;
1698 int uninit = 0;
1699 blk_t super_blk, old_desc_blk, new_desc_blk;
1700 int old_desc_blocks;
1701
1702 /*
1703 * First calculate the block statistics
1704 */
1705 uninit = fs->group_desc[group].bg_flags & EXT2_BG_BLOCK_UNINIT;
1706 ext2fs_super_and_bgd_loc(fs, group, &super_blk, &old_desc_blk,
1707 &new_desc_blk, 0);
1708 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1709 old_desc_blocks = fs->super->s_first_meta_bg;
1710 else
1711 old_desc_blocks = fs->desc_blocks +
1712 fs->super->s_reserved_gdt_blocks;
1713 for (blk = fs->super->s_first_data_block;
1714 blk < fs->super->s_blocks_count; blk++) {
1715 if ((uninit &&
1716 !((blk == super_blk) ||
1717 ((old_desc_blk && old_desc_blocks &&
1718 (blk >= old_desc_blk) &&
1719 (blk < old_desc_blk + old_desc_blocks))) ||
1720 ((new_desc_blk && (blk == new_desc_blk))) ||
1721 (blk == fs->group_desc[group].bg_block_bitmap) ||
1722 (blk == fs->group_desc[group].bg_inode_bitmap) ||
1723 ((blk >= fs->group_desc[group].bg_inode_table &&
1724 (blk < fs->group_desc[group].bg_inode_table
1725 + fs->inode_blocks_per_group))))) ||
1726 (!ext2fs_fast_test_block_bitmap(fs->block_map, blk))) {
1727 group_free++;
1728 total_free++;
1729 }
1730 count++;
1731 if ((count == fs->super->s_blocks_per_group) ||
1732 (blk == fs->super->s_blocks_count-1)) {
1733 fs->group_desc[group].bg_free_blocks_count =
1734 group_free;
1735 ext2fs_group_desc_csum_set(fs, group);
1736 group++;
1737 count = 0;
1738 group_free = 0;
1739 uninit = (fs->group_desc[group].bg_flags &
1740 EXT2_BG_BLOCK_UNINIT);
1741 ext2fs_super_and_bgd_loc(fs, group, &super_blk,
1742 &old_desc_blk,
1743 &new_desc_blk, 0);
1744 if (fs->super->s_feature_incompat &
1745 EXT2_FEATURE_INCOMPAT_META_BG)
1746 old_desc_blocks = fs->super->s_first_meta_bg;
1747 else
1748 old_desc_blocks = fs->desc_blocks +
1749 fs->super->s_reserved_gdt_blocks;
1750 }
1751 }
1752 fs->super->s_free_blocks_count = total_free;
1753
1754 /*
1755 * Next, calculate the inode statistics
1756 */
1757 group_free = 0;
1758 total_free = 0;
1759 count = 0;
1760 group = 0;
1761
1762 /* Protect loop from wrap-around if s_inodes_count maxed */
1763 uninit = fs->group_desc[group].bg_flags & EXT2_BG_INODE_UNINIT;
1764 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1765 if (uninit ||
1766 !ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1767 group_free++;
1768 total_free++;
1769 }
1770 count++;
1771 if ((count == fs->super->s_inodes_per_group) ||
1772 (ino == fs->super->s_inodes_count)) {
1773 fs->group_desc[group].bg_free_inodes_count =
1774 group_free;
1775 ext2fs_group_desc_csum_set(fs, group);
1776 group++;
1777 count = 0;
1778 group_free = 0;
1779 uninit = (fs->group_desc[group].bg_flags &
1780 EXT2_BG_INODE_UNINIT);
1781 }
1782 }
1783 fs->super->s_free_inodes_count = total_free;
1784 ext2fs_mark_super_dirty(fs);
1785 return 0;
1786 }
1787
1788 /*
1789 * calcluate the minimum number of blocks the given fs can be resized to
1790 */
1791 blk_t calculate_minimum_resize_size(ext2_filsys fs)
1792 {
1793 blk_t inode_count, blks_needed, groups, data_blocks;
1794 blk_t grp, data_needed, last_start;
1795 int overhead = 0, num_of_superblocks = 0;
1796
1797 /*
1798 * first figure out how many group descriptors we need to
1799 * handle the number of inodes we have
1800 */
1801 inode_count = fs->super->s_inodes_count -
1802 fs->super->s_free_inodes_count;
1803 blks_needed = ext2fs_div_ceil(inode_count,
1804 fs->super->s_inodes_per_group) *
1805 EXT2_BLOCKS_PER_GROUP(fs->super);
1806 groups = ext2fs_div_ceil(blks_needed,
1807 EXT2_BLOCKS_PER_GROUP(fs->super));
1808
1809 /*
1810 * we need to figure out how many backup superblocks we have so we can
1811 * account for that in the metadata
1812 */
1813 for (grp = 0; grp < fs->group_desc_count; grp++) {
1814 if (ext2fs_bg_has_super(fs, grp))
1815 num_of_superblocks++;
1816 }
1817
1818 /* calculate how many blocks are needed for data */
1819 data_needed = fs->super->s_blocks_count -
1820 fs->super->s_free_blocks_count;
1821 data_needed -= SUPER_OVERHEAD(fs) * num_of_superblocks;
1822 data_needed -= META_OVERHEAD(fs) * fs->group_desc_count;
1823
1824 /*
1825 * figure out how many data blocks we have given the number of groups
1826 * we need for our inodes
1827 */
1828 data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
1829 last_start = 0;
1830 for (grp = 0; grp < groups; grp++) {
1831 overhead = META_OVERHEAD(fs);
1832
1833 if (ext2fs_bg_has_super(fs, grp))
1834 overhead += SUPER_OVERHEAD(fs);
1835
1836 /*
1837 * we want to keep track of how much data we can store in
1838 * the groups leading up to the last group so we can determine
1839 * how big the last group needs to be
1840 */
1841 if (grp != (groups - 1))
1842 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
1843 overhead;
1844
1845 data_blocks -= overhead;
1846 }
1847
1848 /*
1849 * if we need more group descriptors in order to accomodate our data
1850 * then we need to add them here
1851 */
1852 while (data_needed > data_blocks) {
1853 blk_t remainder = data_needed - data_blocks;
1854 blk_t extra_grps;
1855
1856 /* figure out how many more groups we need for the data */
1857 extra_grps = ext2fs_div_ceil(remainder,
1858 EXT2_BLOCKS_PER_GROUP(fs->super));
1859
1860 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
1861
1862 /* ok we have to account for the last group */
1863 overhead = META_OVERHEAD(fs);
1864 if (ext2fs_bg_has_super(fs, groups-1))
1865 overhead += SUPER_OVERHEAD(fs);
1866 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
1867
1868 for (grp = groups; grp < groups+extra_grps; grp++) {
1869 overhead = META_OVERHEAD(fs);
1870 if (ext2fs_bg_has_super(fs, grp))
1871 overhead += SUPER_OVERHEAD(fs);
1872
1873 /*
1874 * again, we need to see how much data we cram into
1875 * all of the groups leading up to the last group
1876 */
1877 if (grp != (groups + extra_grps - 1))
1878 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
1879 - overhead;
1880
1881 data_blocks -= overhead;
1882 }
1883
1884 groups += extra_grps;
1885 }
1886
1887 /* now for the fun voodoo */
1888 overhead = META_OVERHEAD(fs);
1889
1890 /*
1891 * if this is the case then the last group is going to have data in it
1892 * so we need to adjust the size of the last group accordingly
1893 */
1894 if (last_start < data_needed) {
1895 blk_t remainder = data_needed - last_start;
1896
1897 /*
1898 * 50 is a magic number that mkfs/resize uses to see if its
1899 * even worth making/resizing the fs. basically you need to
1900 * have at least 50 blocks in addition to the blocks needed
1901 * for the metadata in the last group
1902 */
1903 if (remainder > 50)
1904 overhead += remainder;
1905 else
1906 overhead += 50;
1907 } else
1908 overhead += 50;
1909
1910 if (ext2fs_bg_has_super(fs, groups-1))
1911 overhead += SUPER_OVERHEAD(fs);
1912
1913 /*
1914 * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
1915 * only do groups-1, and then add the number of blocks needed to
1916 * handle the group descriptor metadata+data that we need
1917 */
1918 blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
1919 blks_needed += overhead;
1920
1921 return blks_needed;
1922 }