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