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