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