Whamcloud - gitweb
main.c (main): If we are resizing a plain file which is smaller
[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                 retval = 0;
794                 goto errout;
795         }
796
797         /*
798          * Step two is to actually move the blocks
799          */
800         retval =  ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
801         if (retval) goto errout;
802
803         if (rfs->progress) {
804                 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
805                                          0, to_move);
806                 if (retval)
807                         goto errout;
808         }
809         while (1) {
810                 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
811                 if (retval) goto errout;
812                 if (!size)
813                         break;
814 #ifdef RESIZE2FS_DEBUG
815                 if (rfs->flags & RESIZE_DEBUG_BMOVE)
816                         printf(_("Moving %d blocks %u->%u\n"), size,
817                                old_blk, new_blk);
818 #endif
819                 do {
820                         c = size;
821                         if (c > fs->inode_blocks_per_group)
822                                 c = fs->inode_blocks_per_group;
823                         retval = io_channel_read_blk(fs->io, old_blk, c,
824                                                      rfs->itable_buf);
825                         if (retval) goto errout;
826                         retval = io_channel_write_blk(fs->io, new_blk, c,
827                                                       rfs->itable_buf);
828                         if (retval) goto errout;
829                         size -= c;
830                         new_blk += c;
831                         old_blk += c;
832                         moved += c;
833                         if (rfs->progress) {
834                                 io_channel_flush(fs->io);
835                                 retval = (rfs->progress)(rfs,
836                                                 E2_RSZ_BLOCK_RELOC_PASS,
837                                                 moved, to_move);
838                                 if (retval)
839                                         goto errout;
840                         }
841                 } while (size > 0);
842                 io_channel_flush(fs->io);
843         }
844
845 errout:
846         return retval;
847 }
848
849
850 /* --------------------------------------------------------------------
851  *
852  * Resize processing, phase 3
853  *
854  * --------------------------------------------------------------------
855  */
856
857
858 struct process_block_struct {
859         ext2_resize_t           rfs;
860         ext2_ino_t              ino;
861         struct ext2_inode *     inode;
862         errcode_t               error;
863         int                     is_dir;
864         int                     changed;
865 };
866
867 static int process_block(ext2_filsys fs, blk_t  *block_nr,
868                          e2_blkcnt_t blockcnt, blk_t ref_block,
869                          int ref_offset, void *priv_data)
870 {
871         struct process_block_struct *pb;
872         errcode_t       retval;
873         blk_t           block, new_block;
874         int             ret = 0;
875
876         pb = (struct process_block_struct *) priv_data;
877         block = *block_nr;
878         if (pb->rfs->bmap) {
879                 new_block = ext2fs_extent_translate(pb->rfs->bmap, block);
880                 if (new_block) {
881                         *block_nr = new_block;
882                         ret |= BLOCK_CHANGED;
883                         pb->changed = 1;
884 #ifdef RESIZE2FS_DEBUG
885                         if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
886                                 printf(_("ino=%u, blockcnt=%lld, %u->%u\n"), 
887                                        pb->ino, blockcnt, block, new_block);
888 #endif
889                         block = new_block;
890                 }
891         }
892         if (pb->is_dir) {
893                 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
894                                               block, (int) blockcnt);
895                 if (retval) {
896                         pb->error = retval;
897                         ret |= BLOCK_ABORT;
898                 }
899         }
900         return ret;
901 }
902
903 /*
904  * Progress callback
905  */
906 static errcode_t progress_callback(ext2_filsys fs, ext2_inode_scan scan,
907                                    dgrp_t group, void * priv_data)
908 {
909         ext2_resize_t rfs = (ext2_resize_t) priv_data;
910         errcode_t               retval;
911
912         /*
913          * This check is to protect against old ext2 libraries.  It
914          * shouldn't be needed against new libraries.
915          */
916         if ((group+1) == 0)
917                 return 0;
918
919         if (rfs->progress) {
920                 io_channel_flush(fs->io);
921                 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
922                                          group+1, fs->group_desc_count);
923                 if (retval)
924                         return retval;
925         }
926         
927         return 0;
928 }
929
930 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
931 {
932         struct process_block_struct     pb;
933         ext2_ino_t              ino, new_inode;
934         struct ext2_inode       inode;
935         ext2_inode_scan         scan = NULL;
936         errcode_t               retval;
937         int                     group;
938         char                    *block_buf = 0;
939         ext2_ino_t              start_to_move;
940         blk_t                   orig_size;
941         
942         if ((rfs->old_fs->group_desc_count <=
943              rfs->new_fs->group_desc_count) &&
944             !rfs->bmap)
945                 return 0;
946
947         /*
948          * Save the original size of the old filesystem, and
949          * temporarily set the size to be the new size if the new size
950          * is larger.  We need to do this to avoid catching an error
951          * by the block iterator routines
952          */
953         orig_size = rfs->old_fs->super->s_blocks_count;
954         if (orig_size < rfs->new_fs->super->s_blocks_count)
955                 rfs->old_fs->super->s_blocks_count =
956                         rfs->new_fs->super->s_blocks_count;
957
958         retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
959         if (retval) goto errout;
960
961         retval = ext2fs_init_dblist(rfs->old_fs, 0);
962         if (retval) goto errout;
963         retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3,
964                                 (void **) &block_buf);
965         if (retval) goto errout;
966
967         start_to_move = (rfs->new_fs->group_desc_count *
968                          rfs->new_fs->super->s_inodes_per_group);
969         
970         if (rfs->progress) {
971                 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
972                                          0, rfs->old_fs->group_desc_count);
973                 if (retval)
974                         goto errout;
975         }
976         ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
977         pb.rfs = rfs;
978         pb.inode = &inode;
979         pb.error = 0;
980         new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
981         /*
982          * First, copy all of the inodes that need to be moved
983          * elsewhere in the inode table
984          */
985         while (1) {
986                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
987                 if (retval) goto errout;
988                 if (!ino)
989                         break;
990
991                 if (inode.i_links_count == 0)
992                         continue; /* inode not in use */
993
994                 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
995                 pb.changed = 0;
996
997                 if (ext2fs_inode_has_valid_blocks(&inode) &&
998                     (rfs->bmap || pb.is_dir)) {
999                         pb.ino = ino;
1000                         retval = ext2fs_block_iterate2(rfs->old_fs,
1001                                                        ino, 0, block_buf,
1002                                                        process_block, &pb);
1003                         if (retval)
1004                                 goto errout;
1005                         if (pb.error) {
1006                                 retval = pb.error;
1007                                 goto errout;
1008                         }
1009                 }
1010
1011                 if (ino <= start_to_move)
1012                         continue; /* Don't need to move it. */
1013
1014                 /*
1015                  * Find a new inode
1016                  */
1017                 while (1) { 
1018                         if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map, 
1019                                                       new_inode))
1020                                 break;
1021                         new_inode++;
1022                         if (new_inode > rfs->new_fs->super->s_inodes_count) {
1023                                 retval = ENOSPC;
1024                                 goto errout;
1025                         }
1026                 }
1027                 ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode);
1028                 if (pb.changed) {
1029                         /* Get the new version of the inode */
1030                         retval = ext2fs_read_inode(rfs->old_fs, ino, &inode);
1031                         if (retval) goto errout;
1032                 }
1033                 retval = ext2fs_write_inode(rfs->old_fs, new_inode, &inode);
1034                 if (retval) goto errout;
1035
1036                 group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super);
1037                 if (LINUX_S_ISDIR(inode.i_mode))
1038                         rfs->new_fs->group_desc[group].bg_used_dirs_count++;
1039                 
1040 #ifdef RESIZE2FS_DEBUG
1041                 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1042                         printf(_("Inode moved %u->%u\n"), ino, new_inode);
1043 #endif
1044                 if (!rfs->imap) {
1045                         retval = ext2fs_create_extent_table(&rfs->imap, 0);
1046                         if (retval)
1047                                 goto errout;
1048                 }
1049                 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1050         }
1051         io_channel_flush(rfs->old_fs->io);
1052
1053 errout:
1054         rfs->old_fs->super->s_blocks_count = orig_size;
1055         if (rfs->bmap) {
1056                 ext2fs_free_extent_table(rfs->bmap);
1057                 rfs->bmap = 0;
1058         }
1059         if (scan)
1060                 ext2fs_close_inode_scan(scan);
1061         if (block_buf)
1062                 ext2fs_free_mem((void **) &block_buf);
1063         return retval;
1064 }
1065
1066 /* --------------------------------------------------------------------
1067  *
1068  * Resize processing, phase 4.
1069  *
1070  * --------------------------------------------------------------------
1071  */
1072
1073 struct istruct {
1074         ext2_resize_t rfs;
1075         errcode_t       err;
1076         unsigned long   max_dirs;
1077         int             num;
1078 };
1079
1080 static int check_and_change_inodes(ext2_ino_t dir, int entry,
1081                                    struct ext2_dir_entry *dirent, int offset,
1082                                    int  blocksize, char *buf, void *priv_data)
1083 {
1084         struct istruct *is = (struct istruct *) priv_data;
1085         ext2_ino_t      new_inode;
1086
1087         if (is->rfs->progress && offset == 0) {
1088                 io_channel_flush(is->rfs->old_fs->io);
1089                 is->err = (is->rfs->progress)(is->rfs,
1090                                               E2_RSZ_INODE_REF_UPD_PASS,
1091                                               ++is->num, is->max_dirs);
1092                 if (is->err)
1093                         return DIRENT_ABORT;
1094         }
1095
1096         if (!dirent->inode)
1097                 return 0;
1098
1099         new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1100
1101         if (!new_inode)
1102                 return 0;
1103 #ifdef RESIZE2FS_DEBUG
1104         if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1105                 printf(_("Inode translate (dir=%u, name=%.*s, %u->%u)\n"),
1106                        dir, dirent->name_len, dirent->name,
1107                        dirent->inode, new_inode);
1108 #endif
1109
1110         dirent->inode = new_inode;
1111
1112         return DIRENT_CHANGED;
1113 }
1114
1115 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1116 {
1117         errcode_t               retval;
1118         struct istruct          is;
1119         
1120         if (!rfs->imap)
1121                 return 0;
1122        
1123         /*
1124          * Now, we iterate over all of the directories to update the
1125          * inode references
1126          */
1127         is.num = 0;
1128         is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist);
1129         is.rfs = rfs;
1130         is.err = 0;
1131
1132         if (rfs->progress) {
1133                 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1134                                          0, is.max_dirs);
1135                 if (retval)
1136                         goto errout;
1137         }
1138         
1139         retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1140                                            DIRENT_FLAG_INCLUDE_EMPTY, 0,
1141                                            check_and_change_inodes, &is);
1142         if (retval)
1143                 goto errout;
1144         if (is.err) {
1145                 retval = is.err;
1146                 goto errout;
1147         }
1148
1149 errout:
1150         ext2fs_free_extent_table(rfs->imap);
1151         rfs->imap = 0;
1152         return retval;
1153 }
1154
1155
1156 /* --------------------------------------------------------------------
1157  *
1158  * Resize processing, phase 5.
1159  *
1160  * In this phase we actually move the inode table around, and then
1161  * update the summary statistics.  This is scary, since aborting here
1162  * will potentially scramble the filesystem.  (We are moving the
1163  * inode tables around in place, and so the potential for lost data,
1164  * or at the very least scrambling the mapping between filenames and
1165  * inode numbers is very high in case of a power failure here.)
1166  * --------------------------------------------------------------------
1167  */
1168
1169
1170 /*
1171  * A very scary routine --- this one moves the inode table around!!!
1172  *
1173  * After this you have to use the rfs->new_fs file handle to read and
1174  * write inodes.
1175  */
1176 static errcode_t move_itables(ext2_resize_t rfs)
1177 {
1178         int             i, n, num, max_groups, size, diff;
1179         ext2_filsys     fs = rfs->new_fs;
1180         char            *cp;
1181         blk_t           old_blk, new_blk;
1182         errcode_t       retval;
1183         int             to_move, moved;
1184
1185         max_groups = fs->group_desc_count;
1186         if (max_groups > rfs->old_fs->group_desc_count)
1187                 max_groups = rfs->old_fs->group_desc_count;
1188
1189         size = fs->blocksize * fs->inode_blocks_per_group;
1190         if (!rfs->itable_buf) {
1191                 retval = ext2fs_get_mem(size, (void **) &rfs->itable_buf);
1192                 if (retval)
1193                         return retval;
1194         }
1195
1196         /*
1197          * Figure out how many inode tables we need to move
1198          */
1199         to_move = moved = 0;
1200         for (i=0; i < max_groups; i++)
1201                 if (rfs->old_fs->group_desc[i].bg_inode_table !=
1202                     fs->group_desc[i].bg_inode_table)
1203                         to_move++;
1204
1205         if (to_move == 0)
1206                 return 0;
1207
1208         if (rfs->progress) {
1209                 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1210                                        0, to_move);
1211                 if (retval)
1212                         goto errout;
1213         }
1214
1215         rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1216
1217         for (i=0; i < max_groups; i++) {
1218                 old_blk = rfs->old_fs->group_desc[i].bg_inode_table;
1219                 new_blk = fs->group_desc[i].bg_inode_table;
1220                 diff = new_blk - old_blk;
1221                 
1222 #ifdef RESIZE2FS_DEBUG
1223                 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) 
1224                         printf(_("Itable move group %d block "
1225                                "%u->%u (diff %d)\n"), 
1226                                i, old_blk, new_blk, diff);
1227 #endif
1228                 
1229                 if (!diff)
1230                         continue;
1231
1232                 retval = io_channel_read_blk(fs->io, old_blk,
1233                                              fs->inode_blocks_per_group,
1234                                              rfs->itable_buf);
1235                 if (retval) 
1236                         goto errout;
1237                 /*
1238                  * The end of the inode table segment often contains
1239                  * all zeros, and we're often only moving the inode
1240                  * table down a block or two.  If so, we can optimize
1241                  * things by not rewriting blocks that we know to be zero
1242                  * already.
1243                  */
1244                 for (cp = rfs->itable_buf+size, n=0; n < size; n++, cp--)
1245                         if (*cp)
1246                                 break;
1247                 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1248 #ifdef RESIZE2FS_DEBUG
1249                 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) 
1250                         printf(_("%d blocks of zeros...\n"), n);
1251 #endif
1252                 num = fs->inode_blocks_per_group;
1253                 if (n > diff)
1254                         num -= n;
1255
1256                 retval = io_channel_write_blk(fs->io, new_blk,
1257                                               num, rfs->itable_buf);
1258                 if (retval) {
1259                         io_channel_write_blk(fs->io, old_blk,
1260                                              num, rfs->itable_buf);
1261                         goto errout;
1262                 }
1263                 if (n > diff) {
1264                         retval = io_channel_write_blk(fs->io,
1265                               old_blk + fs->inode_blocks_per_group,
1266                               diff, (rfs->itable_buf +
1267                                      (fs->inode_blocks_per_group - diff) *
1268                                      fs->blocksize));
1269                         if (retval)
1270                                 goto errout;
1271                 }
1272                 rfs->old_fs->group_desc[i].bg_inode_table = new_blk;
1273                 ext2fs_mark_super_dirty(rfs->old_fs);
1274                 if (rfs->progress) {
1275                         ext2fs_flush(rfs->old_fs);
1276                         retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1277                                                ++moved, to_move);
1278                         if (retval)
1279                                 goto errout;
1280                 }
1281         }
1282         ext2fs_flush(fs);
1283 #ifdef RESIZE2FS_DEBUG
1284         if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) 
1285                 printf(_("Inode table move finished.\n"));
1286 #endif
1287         return 0;
1288         
1289 errout:
1290         return retval;
1291 }
1292
1293 /*
1294  * Finally, recalculate the summary information
1295  */
1296 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1297 {
1298         blk_t           blk;
1299         ext2_ino_t      ino;
1300         int             group = 0;
1301         int             count = 0;
1302         int             total_free = 0;
1303         int             group_free = 0;
1304
1305         /*
1306          * First calculate the block statistics
1307          */
1308         for (blk = fs->super->s_first_data_block;
1309              blk < fs->super->s_blocks_count; blk++) {
1310                 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1311                         group_free++;
1312                         total_free++;
1313                 }
1314                 count++;
1315                 if ((count == fs->super->s_blocks_per_group) ||
1316                     (blk == fs->super->s_blocks_count-1)) {
1317                         fs->group_desc[group++].bg_free_blocks_count =
1318                                 group_free;
1319                         count = 0;
1320                         group_free = 0;
1321                 }
1322         }
1323         fs->super->s_free_blocks_count = total_free;
1324         
1325         /*
1326          * Next, calculate the inode statistics
1327          */
1328         group_free = 0;
1329         total_free = 0;
1330         count = 0;
1331         group = 0;
1332         for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
1333                 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1334                         group_free++;
1335                         total_free++;
1336                 }
1337                 count++;
1338                 if ((count == fs->super->s_inodes_per_group) ||
1339                     (ino == fs->super->s_inodes_count)) {
1340                         fs->group_desc[group++].bg_free_inodes_count =
1341                                 group_free;
1342                         count = 0;
1343                         group_free = 0;
1344                 }
1345         }
1346         fs->super->s_free_inodes_count = total_free;
1347         ext2fs_mark_super_dirty(fs);
1348         return 0;
1349 }