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