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