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