OSDN Git Service

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