Whamcloud - gitweb
Clean up gcc -Wall complaints in resize2fs
[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 errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size);
44 static errcode_t blocks_to_move(ext2_resize_t rfs);
45 static errcode_t block_mover(ext2_resize_t rfs);
46 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
47 static errcode_t inode_ref_fix(ext2_resize_t rfs);
48 static errcode_t move_itables(ext2_resize_t rfs);
49 static errcode_t fix_resize_inode(ext2_filsys fs);
50 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
51
52 /*
53  * Some helper CPP macros
54  */
55 #define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap)
56 #define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap)
57 #define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table)
58
59 #define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i)))
60 #define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i)))
61
62 #define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \
63                                  ((blk) < (FS_INODE_TB((fs), (i)) + \
64                                            (fs)->inode_blocks_per_group)))
65
66
67
68 /*
69  * This is the top-level routine which does the dirty deed....
70  */
71 errcode_t resize_fs(ext2_filsys fs, blk_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         /*
84          * Create the data structure
85          */
86         retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
87         if (retval)
88                 return retval;
89         memset(rfs, 0, sizeof(struct ext2_resize_struct));
90
91         rfs->old_fs = fs;
92         rfs->flags = flags;
93         rfs->itable_buf  = 0;
94         rfs->progress = progress;
95         retval = ext2fs_dup_handle(fs, &rfs->new_fs);
96         if (retval)
97                 goto errout;
98
99         retval = adjust_superblock(rfs, *new_size);
100         if (retval)
101                 goto errout;
102
103         *new_size = rfs->new_fs->super->s_blocks_count;
104
105         retval = blocks_to_move(rfs);
106         if (retval)
107                 goto errout;
108
109 #ifdef RESIZE2FS_DEBUG
110         if (rfs->flags & RESIZE_DEBUG_BMOVE)
111                 printf("Number of free blocks: %d/%d, Needed: %d\n",
112                        rfs->old_fs->super->s_free_blocks_count,
113                        rfs->new_fs->super->s_free_blocks_count,
114                        rfs->needed_blocks);
115 #endif
116         
117         retval = block_mover(rfs);
118         if (retval)
119                 goto errout;
120
121         retval = inode_scan_and_fix(rfs);
122         if (retval)
123                 goto errout;
124
125         retval = inode_ref_fix(rfs);
126         if (retval)
127                 goto errout;
128
129         retval = move_itables(rfs);
130         if (retval)
131                 goto errout;
132
133         retval = ext2fs_calculate_summary_stats(rfs->new_fs);
134         if (retval)
135                 goto errout;
136         
137         retval = fix_resize_inode(rfs->new_fs);
138         if (retval)
139                 goto errout;
140
141         retval = ext2fs_close(rfs->new_fs);
142         if (retval)
143                 goto errout;
144
145         rfs->flags = flags;
146         
147         ext2fs_free(rfs->old_fs);
148         if (rfs->itable_buf)
149                 ext2fs_free_mem(&rfs->itable_buf);
150         ext2fs_free_mem(&rfs);
151         
152         return 0;
153
154 errout:
155         if (rfs->new_fs)
156                 ext2fs_free(rfs->new_fs);
157         if (rfs->itable_buf)
158                 ext2fs_free_mem(&rfs->itable_buf);
159         ext2fs_free_mem(&rfs);
160         return retval;
161 }
162
163 /* --------------------------------------------------------------------
164  *
165  * Resize processing, phase 1.
166  *
167  * In this phase we adjust the in-memory superblock information, and
168  * initialize any new parts of the inode table.  The new parts of the
169  * inode table are created in virgin disk space, so we can abort here
170  * without any side effects.
171  * --------------------------------------------------------------------
172  */
173
174 /*
175  * This routine adjusts the superblock and other data structures...
176  */
177 static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
178 {
179         ext2_filsys fs;
180         int             overhead = 0;
181         int             rem, adj = 0;
182         errcode_t       retval;
183         ext2_ino_t      real_end;
184         blk_t           blk, group_block;
185         unsigned long   i, j, old_desc_blocks;
186         int             old_numblocks, numblocks, adjblocks;
187         unsigned int    meta_bg, meta_bg_size;
188         int             has_super;
189         unsigned long   max_group;
190         
191         fs = rfs->new_fs;
192         fs->super->s_blocks_count = new_size;
193         ext2fs_mark_super_dirty(fs);
194         ext2fs_mark_bb_dirty(fs);
195         ext2fs_mark_ib_dirty(fs);
196
197 retry:
198         fs->group_desc_count = (fs->super->s_blocks_count -
199                                 fs->super->s_first_data_block +
200                                 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
201                 / EXT2_BLOCKS_PER_GROUP(fs->super);
202         if (fs->group_desc_count == 0)
203                 return EXT2_ET_TOOSMALL;
204         fs->desc_blocks = (fs->group_desc_count +
205                            EXT2_DESC_PER_BLOCK(fs->super) - 1)
206                 / EXT2_DESC_PER_BLOCK(fs->super);
207
208         /*
209          * Overhead is the number of bookkeeping blocks per group.  It
210          * includes the superblock backup, the group descriptor
211          * backups, the inode bitmap, the block bitmap, and the inode
212          * table.
213          */
214         overhead = (int) (2 + fs->inode_blocks_per_group);
215
216         if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
217                 overhead += 1 + fs->desc_blocks + 
218                         fs->super->s_reserved_gdt_blocks;
219
220         /*
221          * See if the last group is big enough to support the
222          * necessary data structures.  If not, we need to get rid of
223          * it.
224          */
225         rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) %
226                 fs->super->s_blocks_per_group;
227         if ((fs->group_desc_count == 1) && rem && (rem < overhead))
228                 return EXT2_ET_TOOSMALL;
229         if (rem && (rem < overhead+50)) {
230                 fs->super->s_blocks_count -= rem;
231                 goto retry;
232         }
233         /*
234          * Adjust the number of inodes
235          */
236         fs->super->s_inodes_count = fs->super->s_inodes_per_group *
237                 fs->group_desc_count;
238
239         /*
240          * Adjust the number of free blocks
241          */
242         blk = rfs->old_fs->super->s_blocks_count;
243         if (blk > fs->super->s_blocks_count)
244                 fs->super->s_free_blocks_count -=
245                         (blk - fs->super->s_blocks_count);
246         else
247                 fs->super->s_free_blocks_count +=
248                         (fs->super->s_blocks_count - blk);
249
250         /*
251          * Adjust the number of reserved blocks
252          */
253         blk = rfs->old_fs->super->s_r_blocks_count * 100 /
254                 rfs->old_fs->super->s_blocks_count;
255         fs->super->s_r_blocks_count = ((fs->super->s_blocks_count * blk)
256                                        / 100);
257
258         /*
259          * Adjust the bitmaps for size
260          */
261         retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count,
262                                             fs->super->s_inodes_count,
263                                             fs->inode_map);
264         if (retval) goto errout;
265         
266         real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super)
267                      * fs->group_desc_count)) - 1 +
268                              fs->super->s_first_data_block;
269         retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1,
270                                             real_end, fs->block_map);
271
272         if (retval) goto errout;
273
274         /*
275          * Reallocate the group descriptors as necessary.
276          */
277         if (rfs->old_fs->desc_blocks != fs->desc_blocks) {
278                 retval = ext2fs_resize_mem(rfs->old_fs->desc_blocks *
279                                            fs->blocksize,
280                                            fs->desc_blocks * fs->blocksize,
281                                            &fs->group_desc);
282                 if (retval)
283                         goto errout;
284                 if (fs->desc_blocks > rfs->old_fs->desc_blocks) 
285                         memset((char *) fs->group_desc + 
286                                (rfs->old_fs->desc_blocks * fs->blocksize), 0,
287                                (fs->desc_blocks - rfs->old_fs->desc_blocks) *
288                                fs->blocksize);
289         }
290
291         /*
292          * Check to make sure there are enough inodes
293          */
294         if ((rfs->old_fs->super->s_inodes_count -
295              rfs->old_fs->super->s_free_inodes_count) >
296             rfs->new_fs->super->s_inodes_count) {
297                 retval = ENOSPC;
298                 goto errout;
299         }
300
301         /*
302          * If the resize_inode feature is set, and we are changing the
303          * number of descriptor blocks, then adjust
304          * s_reserved_gdt_blocks if possible to avoid needing to move
305          * the inode table either now or in the future.
306          */
307         if ((fs->super->s_feature_compat & 
308              EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
309             (rfs->old_fs->desc_blocks != fs->desc_blocks)) {
310                 int new;
311
312                 new = ((int) fs->super->s_reserved_gdt_blocks) + 
313                         (rfs->old_fs->desc_blocks - fs->desc_blocks);
314                 if (new < 0)
315                         new = 0;
316                 if (new > fs->blocksize/4)
317                         new = fs->blocksize/4;
318                 fs->super->s_reserved_gdt_blocks = new;
319                 if (new == 0)
320                         fs->super->s_feature_compat &= 
321                                 ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
322         }
323
324         /*
325          * If we are shrinking the number block groups, we're done and
326          * can exit now.
327          */
328         if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
329                 retval = 0;
330                 goto errout;
331         }
332         /*
333          * Fix the count of the last (old) block group
334          */
335         old_numblocks = (rfs->old_fs->super->s_blocks_count -
336                          rfs->old_fs->super->s_first_data_block) %
337                                  rfs->old_fs->super->s_blocks_per_group;
338         if (!old_numblocks)
339                 old_numblocks = rfs->old_fs->super->s_blocks_per_group;
340         if (rfs->old_fs->group_desc_count == fs->group_desc_count) {
341                 numblocks = (rfs->new_fs->super->s_blocks_count -
342                              rfs->new_fs->super->s_first_data_block) %
343                                      rfs->new_fs->super->s_blocks_per_group;
344                 if (!numblocks)
345                         numblocks = rfs->new_fs->super->s_blocks_per_group;
346         } else
347                 numblocks = rfs->new_fs->super->s_blocks_per_group;
348         i = rfs->old_fs->group_desc_count - 1;
349         fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks);
350                 
351         /*
352          * If the number of block groups is staying the same, we're
353          * done and can exit now.  (If the number block groups is
354          * shrinking, we had exited earlier.)
355          */
356         if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
357                 retval = 0;
358                 goto errout;
359         }
360         /*
361          * Initialize the new block group descriptors
362          */
363         retval = ext2fs_get_mem(fs->blocksize * fs->inode_blocks_per_group,
364                                 &rfs->itable_buf);
365         if (retval)
366                 goto errout;
367
368         memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
369         group_block = fs->super->s_first_data_block +
370                 rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group;
371
372         adj = rfs->old_fs->group_desc_count;
373         max_group = fs->group_desc_count - adj;
374         if (rfs->progress) {
375                 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
376                                        0, max_group);
377                 if (retval)
378                         goto errout;
379         }
380         if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
381                 old_desc_blocks = fs->super->s_first_meta_bg;
382         else
383                 old_desc_blocks = fs->desc_blocks + 
384                         fs->super->s_reserved_gdt_blocks;
385         for (i = rfs->old_fs->group_desc_count;
386              i < fs->group_desc_count; i++) {
387                 memset(&fs->group_desc[i], 0,
388                        sizeof(struct ext2_group_desc));
389                 adjblocks = 0;
390
391                 if (i == fs->group_desc_count-1) {
392                         numblocks = (fs->super->s_blocks_count -
393                                      fs->super->s_first_data_block) %
394                                              fs->super->s_blocks_per_group;
395                         if (!numblocks)
396                                 numblocks = fs->super->s_blocks_per_group;
397                 } else
398                         numblocks = fs->super->s_blocks_per_group;
399
400                 has_super = ext2fs_bg_has_super(fs, i);
401                 if (has_super) {
402                         ext2fs_mark_block_bitmap(fs->block_map, group_block);
403                         adjblocks++;
404                 }
405                 meta_bg_size = (fs->blocksize /
406                                 sizeof (struct ext2_group_desc));
407                 meta_bg = i / meta_bg_size;
408                 if (!(fs->super->s_feature_incompat &
409                       EXT2_FEATURE_INCOMPAT_META_BG) ||
410                     (meta_bg < fs->super->s_first_meta_bg)) {
411                         if (has_super) {
412                                 for (j=0; j < old_desc_blocks; j++)
413                                         ext2fs_mark_block_bitmap(fs->block_map,
414                                                          group_block + 1 + j);
415                                 adjblocks += old_desc_blocks;
416                         }
417                 } else {
418                         if (has_super)
419                                 has_super = 1;
420                         if (((i % meta_bg_size) == 0) ||
421                             ((i % meta_bg_size) == 1) ||
422                             ((i % meta_bg_size) == (meta_bg_size-1)))
423                                 ext2fs_mark_block_bitmap(fs->block_map,
424                                                  group_block + has_super);
425                 }
426                 
427                 adjblocks += 2 + fs->inode_blocks_per_group;
428                 
429                 numblocks -= adjblocks;
430                 fs->super->s_free_blocks_count -= adjblocks;
431                 fs->super->s_free_inodes_count +=
432                         fs->super->s_inodes_per_group;
433                 fs->group_desc[i].bg_free_blocks_count = numblocks;
434                 fs->group_desc[i].bg_free_inodes_count =
435                         fs->super->s_inodes_per_group;
436                 fs->group_desc[i].bg_used_dirs_count = 0;
437
438                 retval = ext2fs_allocate_group_table(fs, i, 0);
439                 if (retval) goto errout;
440
441                 /*
442                  * Write out the new inode table
443                  */
444                 retval = io_channel_write_blk(fs->io,
445                                               fs->group_desc[i].bg_inode_table,
446                                               fs->inode_blocks_per_group,
447                                               rfs->itable_buf);
448                 if (retval) goto errout;
449
450                 io_channel_flush(fs->io);
451                 if (rfs->progress) {
452                         retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
453                                                i - adj + 1, max_group);
454                         if (retval)
455                                 goto errout;
456                 }
457                 group_block += fs->super->s_blocks_per_group;
458         }
459         io_channel_flush(fs->io);
460         retval = 0;
461
462 errout:
463         return retval;
464 }
465
466 /* --------------------------------------------------------------------
467  *
468  * Resize processing, phase 2.
469  *
470  * In this phase we adjust determine which blocks need to be moved, in
471  * blocks_to_move().  We then copy the blocks to their ultimate new
472  * destinations using block_mover().  Since we are copying blocks to
473  * their new locations, again during this pass we can abort without
474  * any problems.
475  * --------------------------------------------------------------------
476  */
477
478 /*
479  * This helper function creates a block bitmap with all of the
480  * filesystem meta-data blocks.
481  */
482 static errcode_t mark_table_blocks(ext2_filsys fs,
483                                    ext2fs_block_bitmap bmap)
484 {
485         blk_t                   block, b;
486         unsigned int            j;
487         dgrp_t                  i;
488         unsigned long           meta_bg_size;
489         unsigned int            old_desc_blocks;
490
491         meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
492         block = fs->super->s_first_data_block;
493         if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
494                 old_desc_blocks = fs->super->s_first_meta_bg;
495         else
496                 old_desc_blocks = fs->desc_blocks + 
497                         fs->super->s_reserved_gdt_blocks;
498         for (i = 0; i < fs->group_desc_count; i++) {
499                 ext2fs_reserve_super_and_bgd(fs, i, bmap);
500         
501                 /*
502                  * Mark the blocks used for the inode table
503                  */
504                 for (j = 0, b = fs->group_desc[i].bg_inode_table;
505                      j < (unsigned int) fs->inode_blocks_per_group;
506                      j++, b++)
507                         ext2fs_mark_block_bitmap(bmap, b);
508                             
509                 /*
510                  * Mark block used for the block bitmap 
511                  */
512                 ext2fs_mark_block_bitmap(bmap,
513                                          fs->group_desc[i].bg_block_bitmap);
514
515                 /*
516                  * Mark block used for the inode bitmap 
517                  */
518                 ext2fs_mark_block_bitmap(bmap,
519                                          fs->group_desc[i].bg_inode_bitmap);
520                 block += fs->super->s_blocks_per_group;
521         }
522         return 0;
523 }
524
525 /*
526  * This function checks to see if a particular block (either a
527  * superblock or a block group descriptor) overlaps with an inode or
528  * block bitmap block, or with the inode table.
529  */
530 static void mark_fs_metablock(ext2_resize_t rfs,
531                               ext2fs_block_bitmap meta_bmap,
532                               int group, blk_t blk)
533 {
534         ext2_filsys     fs = rfs->new_fs;
535         
536         ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
537         ext2fs_mark_block_bitmap(fs->block_map, blk);
538
539         /*
540          * Check to see if we overlap with the inode or block bitmap,
541          * or the inode tables.  If not, and the block is in use, then
542          * mark it as a block to be moved.
543          */
544         if (IS_BLOCK_BM(fs, group, blk)) {
545                 FS_BLOCK_BM(fs, group) = 0;
546                 rfs->needed_blocks++;
547         } else if (IS_INODE_BM(fs, group, blk)) {
548                 FS_INODE_BM(fs, group) = 0;
549                 rfs->needed_blocks++;
550         } else if (IS_INODE_TB(fs, group, blk)) {
551                 FS_INODE_TB(fs, group) = 0;
552                 rfs->needed_blocks++;
553         } else if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk) &&
554                    !ext2fs_test_block_bitmap(meta_bmap, blk)) {
555                 ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
556                 rfs->needed_blocks++;
557         }
558 }
559
560
561 /*
562  * This routine marks and unmarks reserved blocks in the new block
563  * bitmap.  It also determines which blocks need to be moved and
564  * places this information into the move_blocks bitmap.
565  */
566 static errcode_t blocks_to_move(ext2_resize_t rfs)
567 {
568         int             j, has_super;
569         dgrp_t          i, max_groups;
570         blk_t           blk, group_blk;
571         unsigned long   old_blocks, new_blocks;
572         unsigned int    meta_bg, meta_bg_size;
573         errcode_t       retval;
574         ext2_filsys     fs, old_fs;
575         ext2fs_block_bitmap     meta_bmap;
576
577         fs = rfs->new_fs;
578         old_fs = rfs->old_fs;
579         if (old_fs->super->s_blocks_count > fs->super->s_blocks_count)
580                 fs = rfs->old_fs;
581         
582         retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
583                                               &rfs->reserve_blocks);
584         if (retval)
585                 return retval;
586
587         retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
588                                               &rfs->move_blocks);
589         if (retval)
590                 return retval;
591
592         retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"), 
593                                               &meta_bmap);
594         if (retval)
595                 return retval;
596         
597         retval = mark_table_blocks(old_fs, meta_bmap);
598         if (retval)
599                 return retval;
600
601         fs = rfs->new_fs;
602         
603         /*
604          * If we're shrinking the filesystem, we need to move all of
605          * the blocks that don't fit any more
606          */
607         for (blk = fs->super->s_blocks_count;
608              blk < old_fs->super->s_blocks_count; blk++) {
609                 if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
610                     !ext2fs_test_block_bitmap(meta_bmap, blk)) {
611                         ext2fs_mark_block_bitmap(rfs->move_blocks, blk);
612                         rfs->needed_blocks++;
613                 }
614                 ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
615         }
616         
617         if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
618                 old_blocks = old_fs->super->s_first_meta_bg;
619                 new_blocks = fs->super->s_first_meta_bg;
620         } else {
621                 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
622                 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
623         }
624         
625         if (old_blocks == new_blocks) {
626                 retval = 0;
627                 goto errout;
628         }
629
630         max_groups = fs->group_desc_count;
631         if (max_groups > old_fs->group_desc_count)
632                 max_groups = old_fs->group_desc_count;
633         group_blk = old_fs->super->s_first_data_block;
634         /*
635          * If we're reducing the number of descriptor blocks, this
636          * makes life easy.  :-)   We just have to mark some extra
637          * blocks as free.
638          */
639         if (old_blocks > new_blocks) {
640                 for (i = 0; i < max_groups; i++) {
641                         if (!ext2fs_bg_has_super(fs, i)) {
642                                 group_blk += fs->super->s_blocks_per_group;
643                                 continue;
644                         }
645                         for (blk = group_blk+1+new_blocks;
646                              blk < group_blk+1+old_blocks; blk++) {
647                                 ext2fs_unmark_block_bitmap(fs->block_map,
648                                                            blk);
649                                 rfs->needed_blocks--;
650                         }
651                         group_blk += fs->super->s_blocks_per_group;
652                 }
653                 retval = 0;
654                 goto errout;
655         }
656         /*
657          * If we're increasing the number of descriptor blocks, life
658          * gets interesting....  
659          */
660         meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
661         for (i = 0; i < max_groups; i++) {
662                 has_super = ext2fs_bg_has_super(fs, i);
663                 if (has_super)
664                         mark_fs_metablock(rfs, meta_bmap, i, group_blk);
665
666                 meta_bg = i / meta_bg_size;
667                 if (!(fs->super->s_feature_incompat &
668                       EXT2_FEATURE_INCOMPAT_META_BG) ||
669                     (meta_bg < fs->super->s_first_meta_bg)) {
670                         if (has_super) {
671                                 for (blk = group_blk+1;
672                                      blk < group_blk + 1 + new_blocks; blk++)
673                                         mark_fs_metablock(rfs, meta_bmap, 
674                                                           i, blk);
675                         }
676                 } else {
677                         if (has_super)
678                                 has_super = 1;
679                         if (((i % meta_bg_size) == 0) ||
680                             ((i % meta_bg_size) == 1) ||
681                             ((i % meta_bg_size) == (meta_bg_size-1)))
682                                 mark_fs_metablock(rfs, meta_bmap, i,
683                                                   group_blk + has_super);
684                 }
685
686                 if (fs->group_desc[i].bg_inode_table &&
687                     fs->group_desc[i].bg_inode_bitmap &&
688                     fs->group_desc[i].bg_block_bitmap)
689                         goto next_group;
690
691                 /*
692                  * Reserve the existing meta blocks that we know
693                  * aren't to be moved.
694                  */
695                 if (fs->group_desc[i].bg_block_bitmap)
696                         ext2fs_mark_block_bitmap(rfs->reserve_blocks,
697                                  fs->group_desc[i].bg_block_bitmap);
698                 if (fs->group_desc[i].bg_inode_bitmap)
699                         ext2fs_mark_block_bitmap(rfs->reserve_blocks,
700                                  fs->group_desc[i].bg_inode_bitmap);
701                 if (fs->group_desc[i].bg_inode_table)
702                         for (blk = fs->group_desc[i].bg_inode_table, j=0;
703                              j < fs->inode_blocks_per_group ; j++, blk++)
704                                 ext2fs_mark_block_bitmap(rfs->reserve_blocks,
705                                                          blk);
706
707                 /*
708                  * Allocate the missing data structures
709                  */
710                 retval = ext2fs_allocate_group_table(fs, i,
711                                                      rfs->reserve_blocks);
712                 if (retval)
713                         goto errout;
714
715                 /*
716                  * For those structures that have changed, we need to
717                  * do bookkeepping.
718                  */
719                 if (FS_BLOCK_BM(old_fs, i) !=
720                     (blk = FS_BLOCK_BM(fs, i))) {
721                         ext2fs_mark_block_bitmap(fs->block_map, blk);
722                         if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
723                             !ext2fs_test_block_bitmap(meta_bmap, blk))
724                                 ext2fs_mark_block_bitmap(rfs->move_blocks,
725                                                          blk);
726                 }
727                 if (FS_INODE_BM(old_fs, i) !=
728                     (blk = FS_INODE_BM(fs, i))) {
729                         ext2fs_mark_block_bitmap(fs->block_map, blk);
730                         if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
731                             !ext2fs_test_block_bitmap(meta_bmap, blk))
732                                 ext2fs_mark_block_bitmap(rfs->move_blocks,
733                                                          blk);
734                 }
735
736                 /*
737                  * The inode table, if we need to relocate it, is
738                  * handled specially.  We have to reserve the blocks
739                  * for both the old and the new inode table, since we
740                  * can't have the inode table be destroyed during the
741                  * block relocation phase.
742                  */
743                 if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i))
744                         goto next_group; /* inode table not moved */
745
746                 rfs->needed_blocks += fs->inode_blocks_per_group;
747
748                 /*
749                  * Mark the new inode table as in use in the new block
750                  * allocation bitmap, and move any blocks that might 
751                  * be necessary.
752                  */
753                 for (blk = fs->group_desc[i].bg_inode_table, j=0;
754                      j < fs->inode_blocks_per_group ; j++, blk++) {
755                         ext2fs_mark_block_bitmap(fs->block_map, blk);
756                         if (ext2fs_test_block_bitmap(old_fs->block_map, blk) &&
757                             !ext2fs_test_block_bitmap(meta_bmap, blk))
758                                 ext2fs_mark_block_bitmap(rfs->move_blocks,
759                                                          blk);
760                 }
761                 
762                 /*
763                  * Make sure the old inode table is reserved in the
764                  * block reservation bitmap.
765                  */
766                 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
767                      j < fs->inode_blocks_per_group ; j++, blk++)
768                         ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk);
769                 
770         next_group:
771                 group_blk += rfs->new_fs->super->s_blocks_per_group;
772         }
773         retval = 0;
774
775 errout:
776         if (meta_bmap)
777                 ext2fs_free_block_bitmap(meta_bmap);
778         
779         return retval;
780 }
781
782 /*
783  * This helper function tries to allocate a new block.  We try to
784  * avoid hitting the original group descriptor blocks at least at
785  * first, since we want to make it possible to recover from a badly
786  * aborted resize operation as much as possible.
787  *
788  * In the future, I may further modify this routine to balance out
789  * where we get the new blocks across the various block groups.
790  * Ideally we would allocate blocks that corresponded with the block
791  * group of the containing inode, and keep contiguous blocks
792  * together.  However, this very difficult to do efficiently, since we
793  * don't have the necessary information up front.
794  */
795
796 #define AVOID_OLD       1
797 #define DESPERATION     2
798
799 static void init_block_alloc(ext2_resize_t rfs)
800 {
801         rfs->alloc_state = AVOID_OLD;
802         rfs->new_blk = rfs->new_fs->super->s_first_data_block;
803 #if 0
804         /* HACK for testing */
805         if (rfs->new_fs->super->s_blocks_count >
806             rfs->old_fs->super->s_blocks_count)
807                 rfs->new_blk = rfs->old_fs->super->s_blocks_count;
808 #endif
809 }
810
811 static blk_t get_new_block(ext2_resize_t rfs)
812 {
813         ext2_filsys     fs = rfs->new_fs;
814         
815         while (1) {
816                 if (rfs->new_blk >= fs->super->s_blocks_count) {
817                         if (rfs->alloc_state == DESPERATION)
818                                 return 0;
819
820 #ifdef RESIZE2FS_DEBUG
821                         if (rfs->flags & RESIZE_DEBUG_BMOVE)
822                                 printf("Going into desperation mode "
823                                        "for block allocations\n");
824 #endif                  
825                         rfs->alloc_state = DESPERATION;
826                         rfs->new_blk = fs->super->s_first_data_block;
827                         continue;
828                 }
829                 if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) ||
830                     ext2fs_test_block_bitmap(rfs->reserve_blocks,
831                                              rfs->new_blk) ||
832                     ((rfs->alloc_state == AVOID_OLD) &&
833                      (rfs->new_blk < rfs->old_fs->super->s_blocks_count) &&
834                      ext2fs_test_block_bitmap(rfs->old_fs->block_map,
835                                               rfs->new_blk))) {
836                         rfs->new_blk++;
837                         continue;
838                 }
839                 return rfs->new_blk;
840         }
841 }
842
843 static errcode_t block_mover(ext2_resize_t rfs)
844 {
845         blk_t                   blk, old_blk, new_blk;
846         ext2_filsys             fs = rfs->new_fs;
847         ext2_filsys             old_fs = rfs->old_fs;
848         errcode_t               retval;
849         int                     size, c;
850         int                     to_move, moved;
851         ext2_badblocks_list     badblock_list = 0;
852         int                     bb_modified = 0;
853         
854         retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
855         if (retval)
856                 return retval;
857
858         new_blk = fs->super->s_first_data_block;
859         if (!rfs->itable_buf) {
860                 retval = ext2fs_get_mem(fs->blocksize *
861                                         fs->inode_blocks_per_group,
862                                         &rfs->itable_buf);
863                 if (retval)
864                         return retval;
865         }
866         retval = ext2fs_create_extent_table(&rfs->bmap, 0);
867         if (retval)
868                 return retval;
869
870         /*
871          * The first step is to figure out where all of the blocks
872          * will go.
873          */
874         to_move = moved = 0;
875         init_block_alloc(rfs);
876         for (blk = old_fs->super->s_first_data_block;
877              blk < old_fs->super->s_blocks_count; blk++) {
878                 if (!ext2fs_test_block_bitmap(old_fs->block_map, blk))
879                         continue;
880                 if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk))
881                         continue;
882                 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
883                         ext2fs_badblocks_list_del(badblock_list, blk);
884                         bb_modified++;
885                         continue;
886                 }
887
888                 new_blk = get_new_block(rfs);
889                 if (!new_blk) {
890                         retval = ENOSPC;
891                         goto errout;
892                 }
893                 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
894                 ext2fs_add_extent_entry(rfs->bmap, blk, new_blk);
895                 to_move++;
896         }
897         
898         if (to_move == 0) {
899                 if (rfs->bmap) {
900                         ext2fs_free_extent_table(rfs->bmap);
901                         rfs->bmap = 0;
902                 }
903                 retval = 0;
904                 goto errout;
905         }
906
907         /*
908          * Step two is to actually move the blocks
909          */
910         retval =  ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
911         if (retval) goto errout;
912
913         if (rfs->progress) {
914                 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
915                                          0, to_move);
916                 if (retval)
917                         goto errout;
918         }
919         while (1) {
920                 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
921                 if (retval) goto errout;
922                 if (!size)
923                         break;
924 #ifdef RESIZE2FS_DEBUG
925                 if (rfs->flags & RESIZE_DEBUG_BMOVE)
926                         printf("Moving %d blocks %u->%u\n",
927                                size, old_blk, new_blk);
928 #endif
929                 do {
930                         c = size;
931                         if (c > fs->inode_blocks_per_group)
932                                 c = fs->inode_blocks_per_group;
933                         retval = io_channel_read_blk(fs->io, old_blk, c,
934                                                      rfs->itable_buf);
935                         if (retval) goto errout;
936                         retval = io_channel_write_blk(fs->io, new_blk, c,
937                                                       rfs->itable_buf);
938                         if (retval) goto errout;
939                         size -= c;
940                         new_blk += c;
941                         old_blk += c;
942                         moved += c;
943                         if (rfs->progress) {
944                                 io_channel_flush(fs->io);
945                                 retval = (rfs->progress)(rfs,
946                                                 E2_RSZ_BLOCK_RELOC_PASS,
947                                                 moved, to_move);
948                                 if (retval)
949                                         goto errout;
950                         }
951                 } while (size > 0);
952                 io_channel_flush(fs->io);
953         }
954
955 errout:
956         if (badblock_list) {
957                 if (!retval && bb_modified)
958                         retval = ext2fs_update_bb_inode(old_fs,
959                                                         badblock_list);
960                 ext2fs_badblocks_list_free(badblock_list);
961         }
962         return retval;
963 }
964
965
966 /* --------------------------------------------------------------------
967  *
968  * Resize processing, phase 3
969  *
970  * --------------------------------------------------------------------
971  */
972
973
974 struct process_block_struct {
975         ext2_resize_t           rfs;
976         ext2_ino_t              ino;
977         struct ext2_inode *     inode;
978         errcode_t               error;
979         int                     is_dir;
980         int                     changed;
981 };
982
983 static int process_block(ext2_filsys fs, blk_t  *block_nr,
984                          e2_blkcnt_t blockcnt, 
985                          blk_t ref_block EXT2FS_ATTR((unused)),
986                          int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
987 {
988         struct process_block_struct *pb;
989         errcode_t       retval;
990         blk_t           block, new_block;
991         int             ret = 0;
992
993         pb = (struct process_block_struct *) priv_data;
994         block = *block_nr;
995         if (pb->rfs->bmap) {
996                 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
997                 if (new_block) {
998                         *block_nr = new_block;
999                         ret |= BLOCK_CHANGED;
1000                         pb->changed = 1;
1001 #ifdef RESIZE2FS_DEBUG
1002                         if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1003                                 printf("ino=%u, blockcnt=%lld, %u->%u\n", 
1004                                        pb->ino, blockcnt, block, new_block);
1005 #endif
1006                         block = new_block;
1007                 }
1008         }
1009         if (pb->is_dir) {
1010                 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
1011                                               block, (int) blockcnt);
1012                 if (retval) {
1013                         pb->error = retval;
1014                         ret |= BLOCK_ABORT;
1015                 }
1016         }
1017         return ret;
1018 }
1019
1020 /*
1021  * Progress callback
1022  */
1023 static errcode_t progress_callback(ext2_filsys fs, 
1024                                    ext2_inode_scan scan EXT2FS_ATTR((unused)),
1025                                    dgrp_t group, void * priv_data)
1026 {
1027         ext2_resize_t rfs = (ext2_resize_t) priv_data;
1028         errcode_t               retval;
1029
1030         /*
1031          * This check is to protect against old ext2 libraries.  It
1032          * shouldn't be needed against new libraries.
1033          */
1034         if ((group+1) == 0)
1035                 return 0;
1036
1037         if (rfs->progress) {
1038                 io_channel_flush(fs->io);
1039                 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1040                                          group+1, fs->group_desc_count);
1041                 if (retval)
1042                         return retval;
1043         }
1044         
1045         return 0;
1046 }
1047
1048 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1049 {
1050         struct process_block_struct     pb;
1051         ext2_ino_t              ino, new_inode;
1052         struct ext2_inode       inode;
1053         ext2_inode_scan         scan = NULL;
1054         errcode_t               retval;
1055         int                     group;
1056         char                    *block_buf = 0;
1057         ext2_ino_t              start_to_move;
1058         blk_t                   orig_size, new_block;
1059         
1060         if ((rfs->old_fs->group_desc_count <=
1061              rfs->new_fs->group_desc_count) &&
1062             !rfs->bmap)
1063                 return 0;
1064
1065         /*
1066          * Save the original size of the old filesystem, and
1067          * temporarily set the size to be the new size if the new size
1068          * is larger.  We need to do this to avoid catching an error
1069          * by the block iterator routines
1070          */
1071         orig_size = rfs->old_fs->super->s_blocks_count;
1072         if (orig_size < rfs->new_fs->super->s_blocks_count)
1073                 rfs->old_fs->super->s_blocks_count =
1074                         rfs->new_fs->super->s_blocks_count;
1075
1076         retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1077         if (retval) goto errout;
1078
1079         retval = ext2fs_init_dblist(rfs->old_fs, 0);
1080         if (retval) goto errout;
1081         retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3, &block_buf);
1082         if (retval) goto errout;
1083
1084         start_to_move = (rfs->new_fs->group_desc_count *
1085                          rfs->new_fs->super->s_inodes_per_group);
1086         
1087         if (rfs->progress) {
1088                 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1089                                          0, rfs->old_fs->group_desc_count);
1090                 if (retval)
1091                         goto errout;
1092         }
1093         ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1094         pb.rfs = rfs;
1095         pb.inode = &inode;
1096         pb.error = 0;
1097         new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1098         /*
1099          * First, copy all of the inodes that need to be moved
1100          * elsewhere in the inode table
1101          */
1102         while (1) {
1103                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1104                 if (retval) goto errout;
1105                 if (!ino)
1106                         break;
1107
1108                 if (inode.i_links_count == 0 && ino != EXT2_RESIZE_INO)
1109                         continue; /* inode not in use */
1110
1111                 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
1112                 pb.changed = 0;
1113
1114                 if (inode.i_file_acl && rfs->bmap) {
1115                         new_block = ext2fs_extent_translate(rfs->bmap, 
1116                                                             inode.i_file_acl);
1117                         if (new_block) {
1118                                 inode.i_file_acl = new_block;
1119                                 retval = ext2fs_write_inode(rfs->old_fs, 
1120                                                             ino, &inode);
1121                                 if (retval) goto errout;
1122                         }
1123                 }
1124                 
1125                 if (ext2fs_inode_has_valid_blocks(&inode) &&
1126                     (rfs->bmap || pb.is_dir)) {
1127                         pb.ino = ino;
1128                         retval = ext2fs_block_iterate2(rfs->old_fs,
1129                                                        ino, 0, block_buf,
1130                                                        process_block, &pb);
1131                         if (retval)
1132                                 goto errout;
1133                         if (pb.error) {
1134                                 retval = pb.error;
1135                                 goto errout;
1136                         }
1137                 }
1138
1139                 if (ino <= start_to_move)
1140                         continue; /* Don't need to move it. */
1141
1142                 /*
1143                  * Find a new inode
1144                  */
1145                 while (1) { 
1146                         if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map, 
1147                                                       new_inode))
1148                                 break;
1149                         new_inode++;
1150                         if (new_inode > rfs->new_fs->super->s_inodes_count) {
1151                                 retval = ENOSPC;
1152                                 goto errout;
1153                         }
1154                 }
1155                 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1156                 if (pb.changed) {
1157                         /* Get the new version of the inode */
1158                         retval = ext2fs_read_inode(rfs->old_fs, ino, &inode);
1159                         if (retval) goto errout;
1160                 }
1161                 inode.i_ctime = time(0);
1162                 retval = ext2fs_write_inode(rfs->old_fs, new_inode, &inode);
1163                 if (retval) goto errout;
1164
1165                 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1166                 if (LINUX_S_ISDIR(inode.i_mode))
1167                         rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1168                 
1169 #ifdef RESIZE2FS_DEBUG
1170                 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1171                         printf("Inode moved %u->%u\n", ino, new_inode);
1172 #endif
1173                 if (!rfs->imap) {
1174                         retval = ext2fs_create_extent_table(&rfs->imap, 0);
1175                         if (retval)
1176                                 goto errout;
1177                 }
1178                 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1179         }
1180         io_channel_flush(rfs->old_fs->io);
1181
1182 errout:
1183         rfs->old_fs->super->s_blocks_count = orig_size;
1184         if (rfs->bmap) {
1185                 ext2fs_free_extent_table(rfs->bmap);
1186                 rfs->bmap = 0;
1187         }
1188         if (scan)
1189                 ext2fs_close_inode_scan(scan);
1190         if (block_buf)
1191                 ext2fs_free_mem(&block_buf);
1192         return retval;
1193 }
1194
1195 /* --------------------------------------------------------------------
1196  *
1197  * Resize processing, phase 4.
1198  *
1199  * --------------------------------------------------------------------
1200  */
1201
1202 struct istruct {
1203         ext2_resize_t rfs;
1204         errcode_t       err;
1205         unsigned long   max_dirs;
1206         int             num;
1207 };
1208
1209 static int check_and_change_inodes(ext2_ino_t dir, 
1210                                    int entry EXT2FS_ATTR((unused)),
1211                                    struct ext2_dir_entry *dirent, int offset,
1212                                    int  blocksize EXT2FS_ATTR((unused)),
1213                                    char *buf EXT2FS_ATTR((unused)), 
1214                                    void *priv_data)
1215 {
1216         struct istruct *is = (struct istruct *) priv_data;
1217         struct ext2_inode       inode;
1218         ext2_ino_t              new_inode;
1219         errcode_t               retval;
1220
1221         if (is->rfs->progress && offset == 0) {
1222                 io_channel_flush(is->rfs->old_fs->io);
1223                 is->err = (is->rfs->progress)(is->rfs,
1224                                               E2_RSZ_INODE_REF_UPD_PASS,
1225                                               ++is->num, is->max_dirs);
1226                 if (is->err)
1227                         return DIRENT_ABORT;
1228         }
1229
1230         if (!dirent->inode)
1231                 return 0;
1232
1233         new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1234
1235         if (!new_inode)
1236                 return 0;
1237 #ifdef RESIZE2FS_DEBUG
1238         if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1239                 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1240                        dir, dirent->name_len&0xFF, dirent->name,
1241                        dirent->inode, new_inode);
1242 #endif
1243
1244         dirent->inode = new_inode;
1245
1246         /* Update the directory mtime and ctime */
1247         retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1248         if (retval == 0) {
1249                 inode.i_mtime = inode.i_ctime = time(0);
1250                 ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1251         }
1252
1253         return DIRENT_CHANGED;
1254 }
1255
1256 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1257 {
1258         errcode_t               retval;
1259         struct istruct          is;
1260         
1261         if (!rfs->imap)
1262                 return 0;
1263        
1264         /*
1265          * Now, we iterate over all of the directories to update the
1266          * inode references
1267          */
1268         is.num = 0;
1269         is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1270         is.rfs = rfs;
1271         is.err = 0;
1272
1273         if (rfs->progress) {
1274                 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1275                                          0, is.max_dirs);
1276                 if (retval)
1277                         goto errout;
1278         }
1279         
1280         retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1281                                            DIRENT_FLAG_INCLUDE_EMPTY, 0,
1282                                            check_and_change_inodes, &is);
1283         if (retval)
1284                 goto errout;
1285         if (is.err) {
1286                 retval = is.err;
1287                 goto errout;
1288         }
1289
1290 errout:
1291         ext2fs_free_extent_table(rfs->imap);
1292         rfs->imap = 0;
1293         return retval;
1294 }
1295
1296
1297 /* --------------------------------------------------------------------
1298  *
1299  * Resize processing, phase 5.
1300  *
1301  * In this phase we actually move the inode table around, and then
1302  * update the summary statistics.  This is scary, since aborting here
1303  * will potentially scramble the filesystem.  (We are moving the
1304  * inode tables around in place, and so the potential for lost data,
1305  * or at the very least scrambling the mapping between filenames and
1306  * inode numbers is very high in case of a power failure here.)
1307  * --------------------------------------------------------------------
1308  */
1309
1310
1311 /*
1312  * A very scary routine --- this one moves the inode table around!!!
1313  *
1314  * After this you have to use the rfs->new_fs file handle to read and
1315  * write inodes.
1316  */
1317 static errcode_t move_itables(ext2_resize_t rfs)
1318 {
1319         int             n, num, size, diff;
1320         dgrp_t          i, max_groups;
1321         ext2_filsys     fs = rfs->new_fs;
1322         char            *cp;
1323         blk_t           old_blk, new_blk, blk;
1324         errcode_t       retval;
1325         int             j, to_move, moved;
1326
1327         max_groups = fs->group_desc_count;
1328         if (max_groups > rfs->old_fs->group_desc_count)
1329                 max_groups = rfs->old_fs->group_desc_count;
1330
1331         size = fs->blocksize * fs->inode_blocks_per_group;
1332         if (!rfs->itable_buf) {
1333                 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1334                 if (retval)
1335                         return retval;
1336         }
1337
1338         /*
1339          * Figure out how many inode tables we need to move
1340          */
1341         to_move = moved = 0;
1342         for (i=0; i < max_groups; i++)
1343                 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1344                     fs->group_desc[i].bg_inode_table)
1345                         to_move++;
1346
1347         if (to_move == 0)
1348                 return 0;
1349
1350         if (rfs->progress) {
1351                 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1352                                        0, to_move);
1353                 if (retval)
1354                         goto errout;
1355         }
1356
1357         rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1358
1359         for (i=0; i < max_groups; i++) {
1360                 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1361                 new_blk = fs->group_desc[i].bg_inode_table;
1362                 diff = new_blk - old_blk;
1363                 
1364 #ifdef RESIZE2FS_DEBUG
1365                 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) 
1366                         printf("Itable move group %d block %u->%u (diff %d)\n",
1367                                i, old_blk, new_blk, diff);
1368 #endif
1369                 
1370                 if (!diff)
1371                         continue;
1372
1373                 retval = io_channel_read_blk(fs->io, old_blk,
1374                                              fs->inode_blocks_per_group,
1375                                              rfs->itable_buf);
1376                 if (retval) 
1377                         goto errout;
1378                 /*
1379                  * The end of the inode table segment often contains
1380                  * all zeros, and we're often only moving the inode
1381                  * table down a block or two.  If so, we can optimize
1382                  * things by not rewriting blocks that we know to be zero
1383                  * already.
1384                  */
1385                 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1386                         if (*cp)
1387                                 break;
1388                 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1389 #ifdef RESIZE2FS_DEBUG
1390                 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) 
1391                         printf("%d blocks of zeros...\n", n);
1392 #endif
1393                 num = fs->inode_blocks_per_group;
1394                 if (n > diff)
1395                         num -= n;
1396
1397                 retval = io_channel_write_blk(fs->io, new_blk,
1398                                               num, rfs->itable_buf);
1399                 if (retval) {
1400                         io_channel_write_blk(fs->io, old_blk,
1401                                              num, rfs->itable_buf);
1402                         goto errout;
1403                 }
1404                 if (n > diff) {
1405                         retval = io_channel_write_blk(fs->io,
1406                               old_blk + fs->inode_blocks_per_group,
1407                               diff, (rfs->itable_buf +
1408                                      (fs->inode_blocks_per_group - diff) *
1409                                      fs->blocksize));
1410                         if (retval)
1411                                 goto errout;
1412                 }
1413
1414                 for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0;
1415                      j < fs->inode_blocks_per_group ; j++, blk++)
1416                         ext2fs_unmark_block_bitmap(fs->block_map, blk);
1417
1418                 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1419                 ext2fs_mark_super_dirty(rfs->old_fs);
1420                 ext2fs_flush(rfs->old_fs);
1421
1422                 if (rfs->progress) {
1423                         retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1424                                                ++moved, to_move);
1425                         if (retval)
1426                                 goto errout;
1427                 }
1428         }
1429         mark_table_blocks(fs, fs->block_map);
1430         ext2fs_flush(fs);
1431 #ifdef RESIZE2FS_DEBUG
1432         if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) 
1433                 printf("Inode table move finished.\n");
1434 #endif
1435         return 0;
1436         
1437 errout:
1438         return retval;
1439 }
1440
1441 /*
1442  * Fix the resize inode 
1443  */
1444 static errcode_t fix_resize_inode(ext2_filsys fs)
1445 {
1446         struct ext2_inode       inode;
1447         errcode_t               retval;
1448         char *                  block_buf;
1449
1450         if (!(fs->super->s_feature_compat & 
1451               EXT2_FEATURE_COMPAT_RESIZE_INODE))
1452                 return 0;
1453
1454         retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1455         if (retval) goto errout;
1456
1457         retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1458         if (retval) goto errout;
1459
1460         inode.i_blocks = fs->blocksize/512;
1461
1462         retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1463         if (retval) goto errout;
1464
1465         if (!inode.i_block[EXT2_DIND_BLOCK]) {
1466                 /* 
1467                  * Avoid zeroing out block #0; that's rude.  This
1468                  * should never happen anyway since the filesystem
1469                  * should be fsck'ed and we assume it is consistent.
1470                  */
1471                 fprintf(stderr, 
1472                         _("Should never happen: resize inode corrupt!\n"));
1473                 exit(1);
1474         }
1475
1476         memset(block_buf, 0, fs->blocksize);
1477
1478         retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1479                                       1, block_buf);
1480         if (retval) goto errout;
1481         
1482         retval = ext2fs_create_resize_inode(fs);
1483         if (retval)
1484                 goto errout;
1485
1486 errout:
1487         if (block_buf)
1488                 ext2fs_free_mem(&block_buf);
1489         return retval;
1490 }
1491
1492 /*
1493  * Finally, recalculate the summary information
1494  */
1495 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1496 {
1497         blk_t           blk;
1498         ext2_ino_t      ino;
1499         unsigned int    group = 0;
1500         unsigned int    count = 0;
1501         int             total_free = 0;
1502         int             group_free = 0;
1503
1504         /*
1505          * First calculate the block statistics
1506          */
1507         for (blk = fs->super->s_first_data_block;
1508              blk < fs->super->s_blocks_count; blk++) {
1509                 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1510                         group_free++;
1511                         total_free++;
1512                 }
1513                 count++;
1514                 if ((count == fs->super->s_blocks_per_group) ||
1515                     (blk == fs->super->s_blocks_count-1)) {
1516                         fs->group_desc[group++].bg_free_blocks_count =
1517                                 group_free;
1518                         count = 0;
1519                         group_free = 0;
1520                 }
1521         }
1522         fs->super->s_free_blocks_count = total_free;
1523         
1524         /*
1525          * Next, calculate the inode statistics
1526          */
1527         group_free = 0;
1528         total_free = 0;
1529         count = 0;
1530         group = 0;
1531         for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
1532                 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1533                         group_free++;
1534                         total_free++;
1535                 }
1536                 count++;
1537                 if ((count == fs->super->s_inodes_per_group) ||
1538                     (ino == fs->super->s_inodes_count)) {
1539                         fs->group_desc[group++].bg_free_inodes_count =
1540                                 group_free;
1541                         count = 0;
1542                         group_free = 0;
1543                 }
1544         }
1545         fs->super->s_free_inodes_count = total_free;
1546         ext2fs_mark_super_dirty(fs);
1547         return 0;
1548 }