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