Whamcloud - gitweb
Fix memory leaks from using the tdb library
[tools/e2fsprogs.git] / e2fsck / super.c
1 /*
2  * e2fsck.c - superblock checks
3  * 
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #ifdef HAVE_ERRNO_H
13 #include <errno.h>
14 #endif
15
16 #ifndef EXT2_SKIP_UUID
17 #include "uuid/uuid.h"
18 #endif
19 #include "e2fsck.h"
20 #include "problem.h"
21
22 #define MIN_CHECK 1
23 #define MAX_CHECK 2
24
25 static void check_super_value(e2fsck_t ctx, const char *descr,
26                               unsigned long value, int flags,
27                               unsigned long min_val, unsigned long max_val)
28 {
29         struct          problem_context pctx;
30
31         if (((flags & MIN_CHECK) && (value < min_val)) ||
32             ((flags & MAX_CHECK) && (value > max_val))) {
33                 clear_problem_context(&pctx);
34                 pctx.num = value;
35                 pctx.str = descr;
36                 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
37                 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
38         }
39 }
40
41 /*
42  * This routine may get stubbed out in special compilations of the
43  * e2fsck code..
44  */
45 #ifndef EXT2_SPECIAL_DEVICE_SIZE
46 errcode_t e2fsck_get_device_size(e2fsck_t ctx)
47 {
48         return (ext2fs_get_device_size(ctx->filesystem_name,
49                                        EXT2_BLOCK_SIZE(ctx->fs->super),
50                                        &ctx->num_blocks));
51 }
52 #endif
53
54 /*
55  * helper function to release an inode
56  */
57 struct process_block_struct {
58         e2fsck_t        ctx;
59         char            *buf;
60         struct problem_context *pctx;
61         int             truncating;
62         int             truncate_offset;
63         e2_blkcnt_t     truncate_block;
64         int             truncated_blocks;
65         int             abort;
66         errcode_t       errcode;
67 };
68
69 static int release_inode_block(ext2_filsys fs,
70                                blk_t    *block_nr,
71                                e2_blkcnt_t blockcnt,
72                                blk_t    ref_blk EXT2FS_ATTR((unused)),
73                                int      ref_offset EXT2FS_ATTR((unused)),
74                                void *priv_data)
75 {
76         struct process_block_struct *pb;
77         e2fsck_t                ctx;
78         struct problem_context  *pctx;
79         blk_t                   blk = *block_nr;
80         int                     retval = 0;
81
82         pb = (struct process_block_struct *) priv_data;
83         ctx = pb->ctx;
84         pctx = pb->pctx;
85
86         pctx->blk = blk;
87         pctx->blkcount = blockcnt;
88
89         if (HOLE_BLKADDR(blk))
90                 return 0;
91
92         if ((blk < fs->super->s_first_data_block) ||
93             (blk >= fs->super->s_blocks_count)) {
94                 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx);
95         return_abort:
96                 pb->abort = 1;
97                 return BLOCK_ABORT;
98         }
99
100         if (!ext2fs_test_block_bitmap(fs->block_map, blk)) {
101                 fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx);
102                 goto return_abort;
103         }
104
105         /*
106          * If we are deleting an orphan, then we leave the fields alone.
107          * If we are truncating an orphan, then update the inode fields
108          * and clean up any partial block data.
109          */
110         if (pb->truncating) {
111                 /*
112                  * We only remove indirect blocks if they are
113                  * completely empty.
114                  */
115                 if (blockcnt < 0) {
116                         int     i, limit;
117                         blk_t   *bp;
118                         
119                         pb->errcode = io_channel_read_blk(fs->io, blk, 1,
120                                                         pb->buf);
121                         if (pb->errcode)
122                                 goto return_abort;
123
124                         limit = fs->blocksize >> 2;
125                         for (i = 0, bp = (blk_t *) pb->buf;
126                              i < limit;  i++, bp++)
127                                 if (*bp)
128                                         return 0;
129                 }
130                 /*
131                  * We don't remove direct blocks until we've reached
132                  * the truncation block.
133                  */
134                 if (blockcnt >= 0 && blockcnt < pb->truncate_block)
135                         return 0;
136                 /*
137                  * If part of the last block needs truncating, we do
138                  * it here.
139                  */
140                 if ((blockcnt == pb->truncate_block) && pb->truncate_offset) {
141                         pb->errcode = io_channel_read_blk(fs->io, blk, 1,
142                                                         pb->buf);
143                         if (pb->errcode)
144                                 goto return_abort;
145                         memset(pb->buf + pb->truncate_offset, 0,
146                                fs->blocksize - pb->truncate_offset);
147                         pb->errcode = io_channel_write_blk(fs->io, blk, 1,
148                                                          pb->buf);
149                         if (pb->errcode)
150                                 goto return_abort;
151                 }
152                 pb->truncated_blocks++;
153                 *block_nr = 0;
154                 retval |= BLOCK_CHANGED;
155         }
156         
157         ext2fs_block_alloc_stats(fs, blk, -1);
158         return retval;
159 }
160                 
161 /*
162  * This function releases an inode.  Returns 1 if an inconsistency was
163  * found.  If the inode has a link count, then it is being truncated and
164  * not deleted.
165  */
166 static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
167                                 struct ext2_inode *inode, char *block_buf,
168                                 struct problem_context *pctx)
169 {
170         struct process_block_struct     pb;
171         ext2_filsys                     fs = ctx->fs;
172         errcode_t                       retval;
173         __u32                           count;
174
175         if (!ext2fs_inode_has_valid_blocks(inode))
176                 return 0;
177
178         pb.buf = block_buf + 3 * ctx->fs->blocksize;
179         pb.ctx = ctx;
180         pb.abort = 0;
181         pb.errcode = 0;
182         pb.pctx = pctx;
183         if (inode->i_links_count) {
184                 pb.truncating = 1;
185                 pb.truncate_block = (e2_blkcnt_t)
186                         ((((long long)inode->i_size_high << 32) +
187                           inode->i_size + fs->blocksize - 1) /
188                          fs->blocksize);
189                 pb.truncate_offset = inode->i_size % fs->blocksize;
190         } else {
191                 pb.truncating = 0;
192                 pb.truncate_block = 0;
193                 pb.truncate_offset = 0;
194         }
195         pb.truncated_blocks = 0;
196         retval = ext2fs_block_iterate2(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE, 
197                                       block_buf, release_inode_block, &pb);
198         if (retval) {
199                 com_err("release_inode_blocks", retval,
200                         _("while calling ext2fs_block_iterate for inode %d"),
201                         ino);
202                 return 1;
203         }
204         if (pb.abort)
205                 return 1;
206
207         /* Refresh the inode since ext2fs_block_iterate may have changed it */
208         e2fsck_read_inode(ctx, ino, inode, "release_inode_blocks");
209
210         if (pb.truncated_blocks)
211                 inode->i_blocks -= pb.truncated_blocks *
212                         (fs->blocksize / 512);
213
214         if (inode->i_file_acl) {
215                 retval = ext2fs_adjust_ea_refcount(fs, inode->i_file_acl,
216                                                    block_buf, -1, &count);
217                 if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) {
218                         retval = 0;
219                         count = 1;
220                 }
221                 if (retval) {
222                         com_err("release_inode_blocks", retval,
223                 _("while calling ext2fs_adjust_ea_refcount for inode %d"),
224                                 ino);
225                         return 1;
226                 }
227                 if (count == 0)
228                         ext2fs_block_alloc_stats(fs, inode->i_file_acl, -1);
229                 inode->i_file_acl = 0;
230         }
231         return 0;
232 }
233
234 /*
235  * This function releases all of the orphan inodes.  It returns 1 if
236  * it hit some error, and 0 on success.
237  */
238 static int release_orphan_inodes(e2fsck_t ctx)
239 {
240         ext2_filsys fs = ctx->fs;
241         ext2_ino_t      ino, next_ino;
242         struct ext2_inode inode;
243         struct problem_context pctx;
244         char *block_buf;
245
246         if ((ino = fs->super->s_last_orphan) == 0)
247                 return 0;
248
249         /*
250          * Win or lose, we won't be using the head of the orphan inode
251          * list again.
252          */
253         fs->super->s_last_orphan = 0;
254         ext2fs_mark_super_dirty(fs);
255
256         /*
257          * If the filesystem contains errors, don't run the orphan
258          * list, since the orphan list can't be trusted; and we're
259          * going to be running a full e2fsck run anyway...
260          */
261         if (fs->super->s_state & EXT2_ERROR_FS)
262                 return 0;
263         
264         if ((ino < EXT2_FIRST_INODE(fs->super)) ||
265             (ino > fs->super->s_inodes_count)) {
266                 clear_problem_context(&pctx);
267                 pctx.ino = ino;
268                 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
269                 return 1;
270         }
271
272         block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
273                                                     "block iterate buffer");
274         e2fsck_read_bitmaps(ctx);
275         
276         while (ino) {
277                 e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes");
278                 clear_problem_context(&pctx);
279                 pctx.ino = ino;
280                 pctx.inode = &inode;
281                 pctx.str = inode.i_links_count ? _("Truncating") :
282                         _("Clearing");
283
284                 fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
285
286                 next_ino = inode.i_dtime;
287                 if (next_ino &&
288                     ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
289                      (next_ino > fs->super->s_inodes_count))) {
290                         pctx.ino = next_ino;
291                         fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
292                         goto return_abort;
293                 }
294
295                 if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
296                         goto return_abort;
297
298                 if (!inode.i_links_count) {
299                         ext2fs_inode_alloc_stats2(fs, ino, -1,
300                                                   LINUX_S_ISDIR(inode.i_mode));
301                         inode.i_dtime = ctx->now;
302                 } else {
303                         inode.i_dtime = 0;
304                 }
305                 e2fsck_write_inode(ctx, ino, &inode, "delete_file");
306                 ino = next_ino;
307         }
308         ext2fs_free_mem(&block_buf);
309         return 0;
310 return_abort:
311         ext2fs_free_mem(&block_buf);
312         return 1;
313 }
314
315 /*
316  * Check the resize inode to make sure it is sane.  We check both for
317  * the case where on-line resizing is not enabled (in which case the
318  * resize inode should be cleared) as well as the case where on-line
319  * resizing is enabled.
320  */
321 static void check_resize_inode(e2fsck_t ctx)
322 {
323         ext2_filsys fs = ctx->fs;
324         struct ext2_inode inode;
325         struct problem_context  pctx;
326         int             i, gdt_off, ind_off;
327         dgrp_t          j;
328         blk_t           blk, pblk, expect;
329         __u32           *dind_buf = 0, *ind_buf;
330         errcode_t       retval;
331
332         clear_problem_context(&pctx);
333
334         /* 
335          * If the resize inode feature isn't set, then 
336          * s_reserved_gdt_blocks must be zero.
337          */
338         if (!(fs->super->s_feature_compat & 
339               EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
340                 if (fs->super->s_reserved_gdt_blocks) {
341                         pctx.num = fs->super->s_reserved_gdt_blocks;
342                         if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
343                                         &pctx)) {
344                                 fs->super->s_reserved_gdt_blocks = 0;
345                                 ext2fs_mark_super_dirty(fs);
346                         }
347                 }
348         }
349
350         /* Read the resize inode */
351         pctx.ino = EXT2_RESIZE_INO;
352         retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
353         if (retval) {
354                 if (fs->super->s_feature_compat & 
355                     EXT2_FEATURE_COMPAT_RESIZE_INODE)
356                         ctx->flags |= E2F_FLAG_RESIZE_INODE;
357                 return;
358         }
359
360         /* 
361          * If the resize inode feature isn't set, check to make sure 
362          * the resize inode is cleared; then we're done.
363          */
364         if (!(fs->super->s_feature_compat & 
365               EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
366                 for (i=0; i < EXT2_N_BLOCKS; i++) {
367                         if (inode.i_block[i])
368                                 break;
369                 }
370                 if ((i < EXT2_N_BLOCKS) &&
371                     fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
372                         memset(&inode, 0, sizeof(inode));
373                         e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
374                                            "clear_resize");
375                 }
376                 return;
377         }
378
379         /* 
380          * The resize inode feature is enabled; check to make sure the
381          * only block in use is the double indirect block
382          */
383         blk = inode.i_block[EXT2_DIND_BLOCK];
384         for (i=0; i < EXT2_N_BLOCKS; i++) {
385                 if (i != EXT2_DIND_BLOCK && inode.i_block[i])
386                         break;
387         }
388         if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
389             !(inode.i_mode & LINUX_S_IFREG) ||
390             (blk < fs->super->s_first_data_block ||
391              blk >= fs->super->s_blocks_count)) {
392         resize_inode_invalid:
393                 if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
394                         memset(&inode, 0, sizeof(inode));
395                         e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
396                                            "clear_resize");
397                         ctx->flags |= E2F_FLAG_RESIZE_INODE;
398                 }
399                 if (!(ctx->options & E2F_OPT_READONLY)) {
400                         fs->super->s_state &= ~EXT2_VALID_FS;
401                         ext2fs_mark_super_dirty(fs);
402                 }
403                 goto cleanup;
404         }
405         dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
406                                                     "resize dind buffer");
407         ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
408
409         retval = ext2fs_read_ind_block(fs, blk, dind_buf);
410         if (retval)
411                 goto resize_inode_invalid;
412
413         gdt_off = fs->desc_blocks;
414         pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
415         for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4; 
416              i++, gdt_off++, pblk++) {
417                 gdt_off %= fs->blocksize/4;
418                 if (dind_buf[gdt_off] != pblk)
419                         goto resize_inode_invalid;
420                 retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
421                 if (retval) 
422                         goto resize_inode_invalid;
423                 ind_off = 0;
424                 for (j = 1; j < fs->group_desc_count; j++) {
425                         if (!ext2fs_bg_has_super(fs, j))
426                                 continue;
427                         expect = pblk + (j * fs->super->s_blocks_per_group);
428                         if (ind_buf[ind_off] != expect)
429                                 goto resize_inode_invalid;
430                         ind_off++;
431                 }
432         }
433
434 cleanup:
435         if (dind_buf)
436                 ext2fs_free_mem(&dind_buf);
437
438  }
439
440 /*
441  * This function checks the dirhash signed/unsigned hint if necessary.
442  */
443 static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
444 {
445         struct ext2_super_block *sb = ctx->fs->super;
446         struct problem_context pctx;
447         char    c;
448
449         if ((ctx->options & E2F_OPT_READONLY) ||
450             !(sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
451             (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
452                 return;
453
454         c = (char) 255;
455
456         clear_problem_context(&pctx);
457         if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
458                 if (((int) c) == -1) {
459                         sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
460                 } else {
461                         sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
462                 }
463                 ext2fs_mark_super_dirty(ctx->fs);
464         }
465 }
466
467
468 void check_super_block(e2fsck_t ctx)
469 {
470         ext2_filsys fs = ctx->fs;
471         blk_t   first_block, last_block;
472         struct ext2_super_block *sb = fs->super;
473         struct ext2_group_desc *gd;
474         blk_t   blocks_per_group = fs->super->s_blocks_per_group;
475         blk_t   bpg_max;
476         int     inodes_per_block;
477         int     ipg_max;
478         int     inode_size;
479         dgrp_t  i;
480         blk_t   should_be;
481         struct problem_context  pctx;
482         __u32   free_blocks = 0, free_inodes = 0;
483
484         inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
485         ipg_max = inodes_per_block * (blocks_per_group - 4);
486         if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
487                 ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
488         bpg_max = 8 * EXT2_BLOCK_SIZE(sb);
489         if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
490                 bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
491
492         ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
493                  sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
494         ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
495                  sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
496         ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
497                 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
498
499         clear_problem_context(&pctx);
500
501         /*
502          * Verify the super block constants...
503          */
504         check_super_value(ctx, "inodes_count", sb->s_inodes_count,
505                           MIN_CHECK, 1, 0);
506         check_super_value(ctx, "blocks_count", sb->s_blocks_count,
507                           MIN_CHECK, 1, 0);
508         check_super_value(ctx, "first_data_block", sb->s_first_data_block,
509                           MAX_CHECK, 0, sb->s_blocks_count);
510         check_super_value(ctx, "log_block_size", sb->s_log_block_size,
511                           MIN_CHECK | MAX_CHECK, 0,
512                           EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE);
513         check_super_value(ctx, "log_frag_size", sb->s_log_frag_size,
514                           MIN_CHECK | MAX_CHECK, 0, sb->s_log_block_size);
515         check_super_value(ctx, "frags_per_group", sb->s_frags_per_group,
516                           MIN_CHECK | MAX_CHECK, sb->s_blocks_per_group,
517                           bpg_max);
518         check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
519                           MIN_CHECK | MAX_CHECK, 8, bpg_max);
520         check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
521                           MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max);
522         check_super_value(ctx, "r_blocks_count", sb->s_r_blocks_count,
523                           MAX_CHECK, 0, sb->s_blocks_count / 2);
524         check_super_value(ctx, "reserved_gdt_blocks", 
525                           sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
526                           fs->blocksize/4);
527         inode_size = EXT2_INODE_SIZE(sb);
528         check_super_value(ctx, "inode_size",
529                           inode_size, MIN_CHECK | MAX_CHECK,
530                           EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
531         if (inode_size & (inode_size - 1)) {
532                 pctx.num = inode_size;
533                 pctx.str = "inode_size";
534                 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
535                 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
536                 return;
537         }
538                 
539         if (!ctx->num_blocks) {
540                 pctx.errcode = e2fsck_get_device_size(ctx);
541                 if (pctx.errcode && pctx.errcode != EXT2_ET_UNIMPLEMENTED) {
542                         fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
543                         ctx->flags |= E2F_FLAG_ABORT;
544                         return;
545                 }
546                 if ((pctx.errcode != EXT2_ET_UNIMPLEMENTED) &&
547                     (ctx->num_blocks < sb->s_blocks_count)) {
548                         pctx.blk = sb->s_blocks_count;
549                         pctx.blk2 = ctx->num_blocks;
550                         if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
551                                 ctx->flags |= E2F_FLAG_ABORT;
552                                 return;
553                         }
554                 }
555         }
556
557         if (sb->s_log_block_size != (__u32) sb->s_log_frag_size) {
558                 pctx.blk = EXT2_BLOCK_SIZE(sb);
559                 pctx.blk2 = EXT2_FRAG_SIZE(sb);
560                 fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx);
561                 ctx->flags |= E2F_FLAG_ABORT;
562                 return;
563         }
564
565         should_be = sb->s_frags_per_group >>
566                 (sb->s_log_block_size - sb->s_log_frag_size);           
567         if (sb->s_blocks_per_group != should_be) {
568                 pctx.blk = sb->s_blocks_per_group;
569                 pctx.blk2 = should_be;
570                 fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx);
571                 ctx->flags |= E2F_FLAG_ABORT;
572                 return;
573         }
574
575         should_be = (sb->s_log_block_size == 0) ? 1 : 0;
576         if (sb->s_first_data_block != should_be) {
577                 pctx.blk = sb->s_first_data_block;
578                 pctx.blk2 = should_be;
579                 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
580                 ctx->flags |= E2F_FLAG_ABORT;
581                 return;
582         }
583
584         should_be = sb->s_inodes_per_group * fs->group_desc_count;
585         if (sb->s_inodes_count != should_be) {
586                 pctx.ino = sb->s_inodes_count;
587                 pctx.ino2 = should_be;
588                 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
589                         sb->s_inodes_count = should_be;
590                         ext2fs_mark_super_dirty(fs);
591                 }
592         }
593
594         /*
595          * Verify the group descriptors....
596          */
597         first_block =  sb->s_first_data_block;
598
599         for (i = 0, gd=fs->group_desc; i < fs->group_desc_count; i++, gd++) {
600                 pctx.group = i;
601
602                 first_block = ext2fs_group_first_block(fs, i);
603                 last_block = ext2fs_group_last_block(fs, i);
604
605                 if ((gd->bg_block_bitmap < first_block) ||
606                     (gd->bg_block_bitmap > last_block)) {
607                         pctx.blk = gd->bg_block_bitmap;
608                         if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
609                                 gd->bg_block_bitmap = 0;
610                 }
611                 if (gd->bg_block_bitmap == 0) {
612                         ctx->invalid_block_bitmap_flag[i]++;
613                         ctx->invalid_bitmaps++;
614                 }
615                 if ((gd->bg_inode_bitmap < first_block) ||
616                     (gd->bg_inode_bitmap > last_block)) {
617                         pctx.blk = gd->bg_inode_bitmap;
618                         if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
619                                 gd->bg_inode_bitmap = 0;
620                 }
621                 if (gd->bg_inode_bitmap == 0) {
622                         ctx->invalid_inode_bitmap_flag[i]++;
623                         ctx->invalid_bitmaps++;
624                 }
625                 if ((gd->bg_inode_table < first_block) ||
626                     ((gd->bg_inode_table +
627                       fs->inode_blocks_per_group - 1) > last_block)) {
628                         pctx.blk = gd->bg_inode_table;
629                         if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
630                                 gd->bg_inode_table = 0;
631                 }
632                 if (gd->bg_inode_table == 0) {
633                         ctx->invalid_inode_table_flag[i]++;
634                         ctx->invalid_bitmaps++;
635                 }
636                 free_blocks += gd->bg_free_blocks_count;
637                 free_inodes += gd->bg_free_inodes_count;
638
639                 if ((gd->bg_free_blocks_count > sb->s_blocks_per_group) ||
640                     (gd->bg_free_inodes_count > sb->s_inodes_per_group) ||
641                     (gd->bg_used_dirs_count > sb->s_inodes_per_group))
642                         ext2fs_unmark_valid(fs);
643
644         }
645
646         /*
647          * Update the global counts from the block group counts.  This
648          * is needed for an experimental patch which eliminates
649          * locking the entire filesystem when allocating blocks or
650          * inodes; if the filesystem is not unmounted cleanly, the
651          * global counts may not be accurate.
652          */
653         if ((free_blocks != sb->s_free_blocks_count) ||
654             (free_inodes != sb->s_free_inodes_count)) {
655                 if (ctx->options & E2F_OPT_READONLY)
656                         ext2fs_unmark_valid(fs);
657                 else {
658                         sb->s_free_blocks_count = free_blocks;
659                         sb->s_free_inodes_count = free_inodes;
660                         ext2fs_mark_super_dirty(fs);
661                 }
662         }
663         
664         if ((sb->s_free_blocks_count > sb->s_blocks_count) ||
665             (sb->s_free_inodes_count > sb->s_inodes_count))
666                 ext2fs_unmark_valid(fs);
667
668
669         /*
670          * If we have invalid bitmaps, set the error state of the
671          * filesystem.
672          */
673         if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
674                 sb->s_state &= ~EXT2_VALID_FS;
675                 ext2fs_mark_super_dirty(fs);
676         }
677
678         clear_problem_context(&pctx);
679         
680 #ifndef EXT2_SKIP_UUID
681         /*
682          * If the UUID field isn't assigned, assign it.
683          */
684         if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) {
685                 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
686                         uuid_generate(sb->s_uuid);
687                         ext2fs_mark_super_dirty(fs);
688                         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
689                 }
690         }
691 #endif
692
693         /*
694          * For the Hurd, check to see if the filetype option is set,
695          * since it doesn't support it.
696          */
697         if (!(ctx->options & E2F_OPT_READONLY) &&
698             fs->super->s_creator_os == EXT2_OS_HURD &&
699             (fs->super->s_feature_incompat &
700              EXT2_FEATURE_INCOMPAT_FILETYPE)) {
701                 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
702                         fs->super->s_feature_incompat &=
703                                 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
704                         ext2fs_mark_super_dirty(fs);
705
706                 }
707         }
708
709         /*
710          * If we have any of the compatibility flags set, we need to have a
711          * revision 1 filesystem.  Most kernels will not check the flags on
712          * a rev 0 filesystem and we may have corruption issues because of
713          * the incompatible changes to the filesystem.
714          */
715         if (!(ctx->options & E2F_OPT_READONLY) &&
716             fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
717             (fs->super->s_feature_compat ||
718              fs->super->s_feature_ro_compat ||
719              fs->super->s_feature_incompat) &&
720             fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) {
721                 ext2fs_update_dynamic_rev(fs);
722                 ext2fs_mark_super_dirty(fs);
723         }
724
725         check_resize_inode(ctx);
726
727         /*
728          * Clean up any orphan inodes, if present.
729          */
730         if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
731                 fs->super->s_state &= ~EXT2_VALID_FS;
732                 ext2fs_mark_super_dirty(fs);
733         }
734
735         /* 
736          * Check to see if the superblock last mount time or last
737          * write time is in the future.
738          */
739         if (fs->super->s_mtime > (__u32) ctx->now) {
740                 pctx.num = fs->super->s_mtime;
741                 if (fix_problem(ctx, PR_0_FUTURE_SB_LAST_MOUNT, &pctx)) {
742                         fs->super->s_mtime = ctx->now;
743                         ext2fs_mark_super_dirty(fs);
744                 }
745         }
746         if (fs->super->s_wtime > (__u32) ctx->now) {
747                 pctx.num = fs->super->s_wtime;
748                 if (fix_problem(ctx, PR_0_FUTURE_SB_LAST_WRITE, &pctx)) {
749                         fs->super->s_wtime = ctx->now;
750                         ext2fs_mark_super_dirty(fs);
751                 }
752         }
753
754         /*
755          * Move the ext3 journal file, if necessary.
756          */
757         e2fsck_move_ext3_journal(ctx);
758
759         /*
760          * Fix journal hint, if necessary
761          */
762         e2fsck_fix_ext3_journal_hint(ctx);
763
764         /*
765          * Add dirhash hint if necessary
766          */
767         e2fsck_fix_dirhash_hint(ctx);
768
769         return;
770 }