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