Whamcloud - gitweb
e2fsck: add support for checking the built-in quota files
[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  * helper function to release an inode
43  */
44 struct process_block_struct {
45         e2fsck_t        ctx;
46         char            *buf;
47         struct problem_context *pctx;
48         int             truncating;
49         int             truncate_offset;
50         e2_blkcnt_t     truncate_block;
51         int             truncated_blocks;
52         int             abort;
53         errcode_t       errcode;
54 };
55
56 static int release_inode_block(ext2_filsys fs,
57                                blk64_t  *block_nr,
58                                e2_blkcnt_t blockcnt,
59                                blk64_t  ref_blk EXT2FS_ATTR((unused)),
60                                int      ref_offset EXT2FS_ATTR((unused)),
61                                void *priv_data)
62 {
63         struct process_block_struct *pb;
64         e2fsck_t                ctx;
65         struct problem_context  *pctx;
66         blk64_t                 blk = *block_nr;
67         int                     retval = 0;
68
69         pb = (struct process_block_struct *) priv_data;
70         ctx = pb->ctx;
71         pctx = pb->pctx;
72
73         pctx->blk = blk;
74         pctx->blkcount = blockcnt;
75
76         if (HOLE_BLKADDR(blk))
77                 return 0;
78
79         if ((blk < fs->super->s_first_data_block) ||
80             (blk >= ext2fs_blocks_count(fs->super))) {
81                 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx);
82         return_abort:
83                 pb->abort = 1;
84                 return BLOCK_ABORT;
85         }
86
87         if (!ext2fs_test_block_bitmap2(fs->block_map, blk)) {
88                 fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx);
89                 goto return_abort;
90         }
91
92         /*
93          * If we are deleting an orphan, then we leave the fields alone.
94          * If we are truncating an orphan, then update the inode fields
95          * and clean up any partial block data.
96          */
97         if (pb->truncating) {
98                 /*
99                  * We only remove indirect blocks if they are
100                  * completely empty.
101                  */
102                 if (blockcnt < 0) {
103                         int     i, limit;
104                         blk_t   *bp;
105
106                         pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
107                                                         pb->buf);
108                         if (pb->errcode)
109                                 goto return_abort;
110
111                         limit = fs->blocksize >> 2;
112                         for (i = 0, bp = (blk_t *) pb->buf;
113                              i < limit;  i++, bp++)
114                                 if (*bp)
115                                         return 0;
116                 }
117                 /*
118                  * We don't remove direct blocks until we've reached
119                  * the truncation block.
120                  */
121                 if (blockcnt >= 0 && blockcnt < pb->truncate_block)
122                         return 0;
123                 /*
124                  * If part of the last block needs truncating, we do
125                  * it here.
126                  */
127                 if ((blockcnt == pb->truncate_block) && pb->truncate_offset) {
128                         pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
129                                                         pb->buf);
130                         if (pb->errcode)
131                                 goto return_abort;
132                         memset(pb->buf + pb->truncate_offset, 0,
133                                fs->blocksize - pb->truncate_offset);
134                         pb->errcode = io_channel_write_blk64(fs->io, blk, 1,
135                                                          pb->buf);
136                         if (pb->errcode)
137                                 goto return_abort;
138                 }
139                 pb->truncated_blocks++;
140                 *block_nr = 0;
141                 retval |= BLOCK_CHANGED;
142         }
143
144         ext2fs_block_alloc_stats2(fs, blk, -1);
145         return retval;
146 }
147
148 /*
149  * This function releases an inode.  Returns 1 if an inconsistency was
150  * found.  If the inode has a link count, then it is being truncated and
151  * not deleted.
152  */
153 static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
154                                 struct ext2_inode *inode, char *block_buf,
155                                 struct problem_context *pctx)
156 {
157         struct process_block_struct     pb;
158         ext2_filsys                     fs = ctx->fs;
159         errcode_t                       retval;
160         __u32                           count;
161
162         if (!ext2fs_inode_has_valid_blocks(inode))
163                 return 0;
164
165         pb.buf = block_buf + 3 * ctx->fs->blocksize;
166         pb.ctx = ctx;
167         pb.abort = 0;
168         pb.errcode = 0;
169         pb.pctx = pctx;
170         if (inode->i_links_count) {
171                 pb.truncating = 1;
172                 pb.truncate_block = (e2_blkcnt_t)
173                         ((EXT2_I_SIZE(inode) + fs->blocksize - 1) /
174                          fs->blocksize);
175                 pb.truncate_offset = inode->i_size % fs->blocksize;
176         } else {
177                 pb.truncating = 0;
178                 pb.truncate_block = 0;
179                 pb.truncate_offset = 0;
180         }
181         pb.truncated_blocks = 0;
182         retval = ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE,
183                                       block_buf, release_inode_block, &pb);
184         if (retval) {
185                 com_err("release_inode_blocks", retval,
186                         _("while calling ext2fs_block_iterate for inode %d"),
187                         ino);
188                 return 1;
189         }
190         if (pb.abort)
191                 return 1;
192
193         /* Refresh the inode since ext2fs_block_iterate may have changed it */
194         e2fsck_read_inode(ctx, ino, inode, "release_inode_blocks");
195
196         if (pb.truncated_blocks)
197                 ext2fs_iblk_sub_blocks(fs, inode, pb.truncated_blocks);
198
199         if (ext2fs_file_acl_block(inode)) {
200                 retval = ext2fs_adjust_ea_refcount2(fs, ext2fs_file_acl_block(inode),
201                                                    block_buf, -1, &count);
202                 if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) {
203                         retval = 0;
204                         count = 1;
205                 }
206                 if (retval) {
207                         com_err("release_inode_blocks", retval,
208                 _("while calling ext2fs_adjust_ea_refcount2 for inode %d"),
209                                 ino);
210                         return 1;
211                 }
212                 if (count == 0)
213                         ext2fs_block_alloc_stats2(fs,
214                                                  ext2fs_file_acl_block(inode),
215                                                  -1);
216                 ext2fs_file_acl_block_set(inode, 0);
217         }
218         return 0;
219 }
220
221 /*
222  * This function releases all of the orphan inodes.  It returns 1 if
223  * it hit some error, and 0 on success.
224  */
225 static int release_orphan_inodes(e2fsck_t ctx)
226 {
227         ext2_filsys fs = ctx->fs;
228         ext2_ino_t      ino, next_ino;
229         struct ext2_inode inode;
230         struct problem_context pctx;
231         char *block_buf;
232
233         if ((ino = fs->super->s_last_orphan) == 0)
234                 return 0;
235
236         /*
237          * Win or lose, we won't be using the head of the orphan inode
238          * list again.
239          */
240         fs->super->s_last_orphan = 0;
241         ext2fs_mark_super_dirty(fs);
242
243         /*
244          * If the filesystem contains errors, don't run the orphan
245          * list, since the orphan list can't be trusted; and we're
246          * going to be running a full e2fsck run anyway...
247          */
248         if (fs->super->s_state & EXT2_ERROR_FS)
249                 return 0;
250
251         if ((ino < EXT2_FIRST_INODE(fs->super)) ||
252             (ino > fs->super->s_inodes_count)) {
253                 clear_problem_context(&pctx);
254                 pctx.ino = ino;
255                 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
256                 return 1;
257         }
258
259         block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
260                                                     "block iterate buffer");
261         e2fsck_read_bitmaps(ctx);
262
263         while (ino) {
264                 e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes");
265                 clear_problem_context(&pctx);
266                 pctx.ino = ino;
267                 pctx.inode = &inode;
268                 pctx.str = inode.i_links_count ? _("Truncating") :
269                         _("Clearing");
270
271                 fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
272
273                 next_ino = inode.i_dtime;
274                 if (next_ino &&
275                     ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
276                      (next_ino > fs->super->s_inodes_count))) {
277                         pctx.ino = next_ino;
278                         fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
279                         goto return_abort;
280                 }
281
282                 if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
283                         goto return_abort;
284
285                 if (!inode.i_links_count) {
286                         ext2fs_inode_alloc_stats2(fs, ino, -1,
287                                                   LINUX_S_ISDIR(inode.i_mode));
288                         inode.i_dtime = ctx->now;
289                 } else {
290                         inode.i_dtime = 0;
291                 }
292                 e2fsck_write_inode(ctx, ino, &inode, "delete_file");
293                 ino = next_ino;
294         }
295         ext2fs_free_mem(&block_buf);
296         return 0;
297 return_abort:
298         ext2fs_free_mem(&block_buf);
299         return 1;
300 }
301
302 /*
303  * Check the resize inode to make sure it is sane.  We check both for
304  * the case where on-line resizing is not enabled (in which case the
305  * resize inode should be cleared) as well as the case where on-line
306  * resizing is enabled.
307  */
308 void check_resize_inode(e2fsck_t ctx)
309 {
310         ext2_filsys fs = ctx->fs;
311         struct ext2_inode inode;
312         struct problem_context  pctx;
313         int             i, gdt_off, ind_off;
314         dgrp_t          j;
315         blk64_t         blk, pblk, expect;
316         __u32           *dind_buf = 0, *ind_buf;
317         errcode_t       retval;
318
319         clear_problem_context(&pctx);
320
321         /*
322          * If the resize inode feature isn't set, then
323          * s_reserved_gdt_blocks must be zero.
324          */
325         if (!(fs->super->s_feature_compat &
326               EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
327                 if (fs->super->s_reserved_gdt_blocks) {
328                         pctx.num = fs->super->s_reserved_gdt_blocks;
329                         if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
330                                         &pctx)) {
331                                 fs->super->s_reserved_gdt_blocks = 0;
332                                 ext2fs_mark_super_dirty(fs);
333                         }
334                 }
335         }
336
337         /* Read the resize inode */
338         pctx.ino = EXT2_RESIZE_INO;
339         retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
340         if (retval) {
341                 if (fs->super->s_feature_compat &
342                     EXT2_FEATURE_COMPAT_RESIZE_INODE)
343                         ctx->flags |= E2F_FLAG_RESIZE_INODE;
344                 return;
345         }
346
347         /*
348          * If the resize inode feature isn't set, check to make sure
349          * the resize inode is cleared; then we're done.
350          */
351         if (!(fs->super->s_feature_compat &
352               EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
353                 for (i=0; i < EXT2_N_BLOCKS; i++) {
354                         if (inode.i_block[i])
355                                 break;
356                 }
357                 if ((i < EXT2_N_BLOCKS) &&
358                     fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
359                         memset(&inode, 0, sizeof(inode));
360                         e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
361                                            "clear_resize");
362                 }
363                 return;
364         }
365
366         /*
367          * The resize inode feature is enabled; check to make sure the
368          * only block in use is the double indirect block
369          */
370         blk = inode.i_block[EXT2_DIND_BLOCK];
371         for (i=0; i < EXT2_N_BLOCKS; i++) {
372                 if (i != EXT2_DIND_BLOCK && inode.i_block[i])
373                         break;
374         }
375         if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
376             !(inode.i_mode & LINUX_S_IFREG) ||
377             (blk < fs->super->s_first_data_block ||
378              blk >= ext2fs_blocks_count(fs->super))) {
379         resize_inode_invalid:
380                 if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
381                         memset(&inode, 0, sizeof(inode));
382                         e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
383                                            "clear_resize");
384                         ctx->flags |= E2F_FLAG_RESIZE_INODE;
385                 }
386                 if (!(ctx->options & E2F_OPT_READONLY)) {
387                         fs->super->s_state &= ~EXT2_VALID_FS;
388                         ext2fs_mark_super_dirty(fs);
389                 }
390                 goto cleanup;
391         }
392         dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
393                                                     "resize dind buffer");
394         ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
395
396         retval = ext2fs_read_ind_block(fs, blk, dind_buf);
397         if (retval)
398                 goto resize_inode_invalid;
399
400         gdt_off = fs->desc_blocks;
401         pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
402         if (fs->blocksize == 1024 && fs->super->s_first_data_block == 0)
403                 pblk++; /* Deal with 1024 blocksize bigalloc fs */
404         for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
405              i++, gdt_off++, pblk++) {
406                 gdt_off %= fs->blocksize/4;
407                 if (dind_buf[gdt_off] != pblk)
408                         goto resize_inode_invalid;
409                 retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
410                 if (retval)
411                         goto resize_inode_invalid;
412                 ind_off = 0;
413                 for (j = 1; j < fs->group_desc_count; j++) {
414                         if (!ext2fs_bg_has_super(fs, j))
415                                 continue;
416                         expect = pblk + (j * fs->super->s_blocks_per_group);
417                         if (ind_buf[ind_off] != expect)
418                                 goto resize_inode_invalid;
419                         ind_off++;
420                 }
421         }
422
423 cleanup:
424         if (dind_buf)
425                 ext2fs_free_mem(&dind_buf);
426
427  }
428
429 /*
430  * This function checks the dirhash signed/unsigned hint if necessary.
431  */
432 static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
433 {
434         struct ext2_super_block *sb = ctx->fs->super;
435         struct problem_context pctx;
436         char    c;
437
438         if ((ctx->options & E2F_OPT_READONLY) ||
439             !(sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
440             (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
441                 return;
442
443         c = (char) 255;
444
445         clear_problem_context(&pctx);
446         if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
447                 if (((int) c) == -1) {
448                         sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
449                 } else {
450                         sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
451                 }
452                 ext2fs_mark_super_dirty(ctx->fs);
453         }
454 }
455
456
457 void check_super_block(e2fsck_t ctx)
458 {
459         ext2_filsys fs = ctx->fs;
460         blk64_t first_block, last_block;
461         struct ext2_super_block *sb = fs->super;
462         problem_t       problem;
463         blk64_t blocks_per_group = fs->super->s_blocks_per_group;
464         __u32   bpg_max, cpg_max;
465         int     inodes_per_block;
466         int     ipg_max;
467         int     inode_size;
468         int     accept_time_fudge;
469         int     broken_system_clock;
470         dgrp_t  i;
471         blk64_t should_be;
472         struct problem_context  pctx;
473         blk64_t free_blocks = 0;
474         ino_t   free_inodes = 0;
475         int     csum_flag, clear_test_fs_flag;
476
477         inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
478         ipg_max = inodes_per_block * (blocks_per_group - 4);
479         if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
480                 ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
481         cpg_max = 8 * EXT2_BLOCK_SIZE(sb);
482         if (cpg_max > EXT2_MAX_CLUSTERS_PER_GROUP(sb))
483                 cpg_max = EXT2_MAX_CLUSTERS_PER_GROUP(sb);
484         bpg_max = 8 * EXT2_BLOCK_SIZE(sb) * EXT2FS_CLUSTER_RATIO(fs);
485         if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
486                 bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
487
488         ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
489                  sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
490         ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
491                  sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
492         ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
493                 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
494
495         clear_problem_context(&pctx);
496
497         /*
498          * Verify the super block constants...
499          */
500         check_super_value(ctx, "inodes_count", sb->s_inodes_count,
501                           MIN_CHECK, 1, 0);
502         check_super_value(ctx, "blocks_count", ext2fs_blocks_count(sb),
503                           MIN_CHECK, 1, 0);
504         check_super_value(ctx, "first_data_block", sb->s_first_data_block,
505                           MAX_CHECK, 0, ext2fs_blocks_count(sb));
506         check_super_value(ctx, "log_block_size", sb->s_log_block_size,
507                           MIN_CHECK | MAX_CHECK, 0,
508                           EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE);
509         check_super_value(ctx, "log_cluster_size",
510                           sb->s_log_cluster_size,
511                           MIN_CHECK | MAX_CHECK, sb->s_log_block_size,
512                           (EXT2_MAX_CLUSTER_LOG_SIZE -
513                            EXT2_MIN_CLUSTER_LOG_SIZE));
514         check_super_value(ctx, "clusters_per_group", sb->s_clusters_per_group,
515                           MIN_CHECK | MAX_CHECK, 8, cpg_max);
516         check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
517                           MIN_CHECK | MAX_CHECK, 8, bpg_max);
518         check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
519                           MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max);
520         check_super_value(ctx, "r_blocks_count", ext2fs_r_blocks_count(sb),
521                           MAX_CHECK, 0, ext2fs_blocks_count(sb) / 2);
522         check_super_value(ctx, "reserved_gdt_blocks",
523                           sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
524                           fs->blocksize/4);
525         if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
526                 check_super_value(ctx, "first_ino", sb->s_first_ino,
527                                   MIN_CHECK | MAX_CHECK,
528                                   EXT2_GOOD_OLD_FIRST_INO, sb->s_inodes_count);
529         inode_size = EXT2_INODE_SIZE(sb);
530         check_super_value(ctx, "inode_size",
531                           inode_size, MIN_CHECK | MAX_CHECK,
532                           EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
533         if (sb->s_blocks_per_group != (sb->s_clusters_per_group *
534                                        EXT2FS_CLUSTER_RATIO(fs))) {
535                 pctx.num = sb->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs);
536                 pctx.str = "block_size";
537                 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
538                 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
539                 return;
540         }
541         if (inode_size & (inode_size - 1)) {
542                 pctx.num = inode_size;
543                 pctx.str = "inode_size";
544                 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
545                 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
546                 return;
547         }
548
549         if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) &&
550             (ctx->num_blocks < ext2fs_blocks_count(sb))) {
551                 pctx.blk = ext2fs_blocks_count(sb);
552                 pctx.blk2 = ctx->num_blocks;
553                 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
554                         ctx->flags |= E2F_FLAG_ABORT;
555                         return;
556                 }
557         }
558
559         should_be = (sb->s_log_block_size == 0 &&
560                      EXT2FS_CLUSTER_RATIO(fs) == 1) ? 1 : 0;
561         if (sb->s_first_data_block != should_be) {
562                 pctx.blk = sb->s_first_data_block;
563                 pctx.blk2 = should_be;
564                 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
565                 ctx->flags |= E2F_FLAG_ABORT;
566                 return;
567         }
568
569         should_be = sb->s_inodes_per_group * fs->group_desc_count;
570         if (sb->s_inodes_count != should_be) {
571                 pctx.ino = sb->s_inodes_count;
572                 pctx.ino2 = should_be;
573                 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
574                         sb->s_inodes_count = should_be;
575                         ext2fs_mark_super_dirty(fs);
576                 }
577         }
578
579         /*
580          * Verify the group descriptors....
581          */
582         first_block = sb->s_first_data_block;
583         last_block = ext2fs_blocks_count(sb)-1;
584
585         csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
586                                                EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
587         for (i = 0; i < fs->group_desc_count; i++) {
588                 pctx.group = i;
589
590                 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
591                                                EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
592                         first_block = ext2fs_group_first_block2(fs, i);
593                         last_block = ext2fs_group_last_block2(fs, i);
594                 }
595
596                 if ((ext2fs_block_bitmap_loc(fs, i) < first_block) ||
597                     (ext2fs_block_bitmap_loc(fs, i) > last_block)) {
598                         pctx.blk = ext2fs_block_bitmap_loc(fs, i);
599                         if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
600                                 ext2fs_block_bitmap_loc_set(fs, i, 0);
601                 }
602                 if (ext2fs_block_bitmap_loc(fs, i) == 0) {
603                         ctx->invalid_block_bitmap_flag[i]++;
604                         ctx->invalid_bitmaps++;
605                 }
606                 if ((ext2fs_inode_bitmap_loc(fs, i) < first_block) ||
607                     (ext2fs_inode_bitmap_loc(fs, i) > last_block)) {
608                         pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
609                         if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
610                                 ext2fs_inode_bitmap_loc_set(fs, i, 0);
611                 }
612                 if (ext2fs_inode_bitmap_loc(fs, i) == 0) {
613                         ctx->invalid_inode_bitmap_flag[i]++;
614                         ctx->invalid_bitmaps++;
615                 }
616                 if ((ext2fs_inode_table_loc(fs, i) < first_block) ||
617                     ((ext2fs_inode_table_loc(fs, i) +
618                       fs->inode_blocks_per_group - 1) > last_block)) {
619                         pctx.blk = ext2fs_inode_table_loc(fs, i);
620                         if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
621                                 ext2fs_inode_table_loc_set(fs, i, 0);
622                 }
623                 if (ext2fs_inode_table_loc(fs, i) == 0) {
624                         ctx->invalid_inode_table_flag[i]++;
625                         ctx->invalid_bitmaps++;
626                 }
627                 free_blocks += ext2fs_bg_free_blocks_count(fs, i);
628                 free_inodes += ext2fs_bg_free_inodes_count(fs, i);
629
630                 if ((ext2fs_bg_free_blocks_count(fs, i) > sb->s_blocks_per_group) ||
631                     (ext2fs_bg_free_inodes_count(fs, i) > sb->s_inodes_per_group) ||
632                     (ext2fs_bg_used_dirs_count(fs, i) > sb->s_inodes_per_group))
633                         ext2fs_unmark_valid(fs);
634
635                 should_be = 0;
636                 if (!ext2fs_group_desc_csum_verify(fs, i)) {
637                         if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
638                                 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
639                                 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
640                                 ext2fs_bg_itable_unused_set(fs, i, 0);
641                                 should_be = 1;
642                         }
643                         ext2fs_unmark_valid(fs);
644                 }
645
646                 if (!csum_flag &&
647                     (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) ||
648                      ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) ||
649                      ext2fs_bg_itable_unused(fs, i) != 0)) {
650                         if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) {
651                                 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
652                                 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
653                                 ext2fs_bg_itable_unused_set(fs, i, 0);
654                                 should_be = 1;
655                         }
656                         ext2fs_unmark_valid(fs);
657                 }
658
659                 if (i == fs->group_desc_count - 1 &&
660                     ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)) {
661                         if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) {
662                                 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
663                                 should_be = 1;
664                         }
665                         ext2fs_unmark_valid(fs);
666                 }
667
668                 if (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) &&
669                     !ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT)) {
670                         if (fix_problem(ctx, PR_0_BB_UNINIT_IB_INIT, &pctx)) {
671                                 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
672                                 should_be = 1;
673                         }
674                         ext2fs_unmark_valid(fs);
675                 }
676
677                 if (csum_flag &&
678                     (ext2fs_bg_itable_unused(fs, i) > ext2fs_bg_free_inodes_count(fs, i) ||
679                      ext2fs_bg_itable_unused(fs, i) > sb->s_inodes_per_group)) {
680                         pctx.blk = ext2fs_bg_itable_unused(fs, i);
681                         if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) {
682                                 ext2fs_bg_itable_unused_set(fs, i, 0);
683                                 should_be = 1;
684                         }
685                         ext2fs_unmark_valid(fs);
686                 }
687
688                 if (should_be)
689                         ext2fs_group_desc_csum_set(fs, i);
690                 /* If the user aborts e2fsck by typing ^C, stop right away */
691                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
692                         return;
693         }
694
695         ctx->free_blocks = EXT2FS_C2B(fs, free_blocks);
696         ctx->free_inodes = free_inodes;
697
698         if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) ||
699             (sb->s_free_inodes_count > sb->s_inodes_count))
700                 ext2fs_unmark_valid(fs);
701
702
703         /*
704          * If we have invalid bitmaps, set the error state of the
705          * filesystem.
706          */
707         if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
708                 sb->s_state &= ~EXT2_VALID_FS;
709                 ext2fs_mark_super_dirty(fs);
710         }
711
712         clear_problem_context(&pctx);
713
714 #ifndef EXT2_SKIP_UUID
715         /*
716          * If the UUID field isn't assigned, assign it.
717          */
718         if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) {
719                 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
720                         uuid_generate(sb->s_uuid);
721                         ext2fs_mark_super_dirty(fs);
722                         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
723                 }
724         }
725 #endif
726
727         /*
728          * Check to see if we should disable the test_fs flag
729          */
730         profile_get_boolean(ctx->profile, "options",
731                             "clear_test_fs_flag", 0, 1,
732                             &clear_test_fs_flag);
733         if (!(ctx->options & E2F_OPT_READONLY) &&
734             clear_test_fs_flag &&
735             (fs->super->s_flags & EXT2_FLAGS_TEST_FILESYS) &&
736             (fs_proc_check("ext4") || check_for_modules("ext4"))) {
737                 if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) {
738                         fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
739                         ext2fs_mark_super_dirty(fs);
740                         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
741                 }
742         }
743                         
744         /*
745          * For the Hurd, check to see if the filetype option is set,
746          * since it doesn't support it.
747          */
748         if (!(ctx->options & E2F_OPT_READONLY) &&
749             fs->super->s_creator_os == EXT2_OS_HURD &&
750             (fs->super->s_feature_incompat &
751              EXT2_FEATURE_INCOMPAT_FILETYPE)) {
752                 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
753                         fs->super->s_feature_incompat &=
754                                 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
755                         ext2fs_mark_super_dirty(fs);
756                         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
757                 }
758         }
759
760         /*
761          * If we have any of the compatibility flags set, we need to have a
762          * revision 1 filesystem.  Most kernels will not check the flags on
763          * a rev 0 filesystem and we may have corruption issues because of
764          * the incompatible changes to the filesystem.
765          */
766         if (!(ctx->options & E2F_OPT_READONLY) &&
767             fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
768             (fs->super->s_feature_compat ||
769              fs->super->s_feature_ro_compat ||
770              fs->super->s_feature_incompat) &&
771             fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) {
772                 ext2fs_update_dynamic_rev(fs);
773                 ext2fs_mark_super_dirty(fs);
774                 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
775         }
776
777         /*
778          * Clean up any orphan inodes, if present.
779          */
780         if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
781                 fs->super->s_state &= ~EXT2_VALID_FS;
782                 ext2fs_mark_super_dirty(fs);
783         }
784
785         /*
786          * Unfortunately, due to Windows' unfortunate design decision
787          * to configure the hardware clock to tick localtime, instead
788          * of the more proper and less error-prone UTC time, many
789          * users end up in the situation where the system clock is
790          * incorrectly set at the time when e2fsck is run.
791          *
792          * Historically this was usually due to some distributions
793          * having buggy init scripts and/or installers that didn't
794          * correctly detect this case and take appropriate
795          * countermeasures.  However, it's still possible, despite the
796          * best efforts of init script and installer authors to not be
797          * able to detect this misconfiguration, usually due to a
798          * buggy or misconfigured virtualization manager or the
799          * installer not having access to a network time server during
800          * the installation process.  So by default, we allow the
801          * superblock times to be fudged by up to 24 hours.  This can
802          * be disabled by setting options.accept_time_fudge to the
803          * boolean value of false in e2fsck.conf.  We also support
804          * options.buggy_init_scripts for backwards compatibility.
805          */
806         profile_get_boolean(ctx->profile, "options", "accept_time_fudge",
807                             0, 1, &accept_time_fudge);
808         profile_get_boolean(ctx->profile, "options", "buggy_init_scripts",
809                             0, accept_time_fudge, &accept_time_fudge);
810         ctx->time_fudge = accept_time_fudge ? 86400 : 0;
811
812         profile_get_boolean(ctx->profile, "options", "broken_system_clock",
813                             0, 0, &broken_system_clock);
814
815         /*
816          * Check to see if the superblock last mount time or last
817          * write time is in the future.
818          */
819         if (!broken_system_clock &&
820             !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
821             fs->super->s_mtime > (__u32) ctx->now) {
822                 pctx.num = fs->super->s_mtime;
823                 problem = PR_0_FUTURE_SB_LAST_MOUNT;
824                 if (fs->super->s_mtime <= (__u32) ctx->now + ctx->time_fudge)
825                         problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED;
826                 if (fix_problem(ctx, problem, &pctx)) {
827                         fs->super->s_mtime = ctx->now;
828                         ext2fs_mark_super_dirty(fs);
829                 }
830         }
831         if (!broken_system_clock &&
832             !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
833             fs->super->s_wtime > (__u32) ctx->now) {
834                 pctx.num = fs->super->s_wtime;
835                 problem = PR_0_FUTURE_SB_LAST_WRITE;
836                 if (fs->super->s_wtime <= (__u32) ctx->now + ctx->time_fudge)
837                         problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED;
838                 if (fix_problem(ctx, problem, &pctx)) {
839                         fs->super->s_wtime = ctx->now;
840                         ext2fs_mark_super_dirty(fs);
841                 }
842         }
843
844         /*
845          * Move the ext3 journal file, if necessary.
846          */
847         e2fsck_move_ext3_journal(ctx);
848
849         /*
850          * Fix journal hint, if necessary
851          */
852         e2fsck_fix_ext3_journal_hint(ctx);
853
854         /*
855          * Add dirhash hint if necessary
856          */
857         e2fsck_fix_dirhash_hint(ctx);
858
859         /*
860          * Hide quota inodes if necessary.
861          */
862         e2fsck_hide_quota(ctx);
863
864         return;
865 }
866
867 /*
868  * Check to see if we should backup the master sb to the backup super
869  * blocks.  Returns non-zero if the sb should be backed up.
870  */
871
872 /*
873  * A few flags are set on the fly by the kernel, but only in the
874  * primary superblock.  This is actually a bad thing, and we should
875  * try to discourage it in the future.  In particular, for the newer
876  * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
877  * EXT3_FEATURE_INCOMPAT_EXTENTS.  So some of these may go away in the
878  * future.  EXT3_FEATURE_INCOMPAT_RECOVER may also get set when
879  * copying the primary superblock during online resize.
880  *
881  * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
882  * unfortunately, we shouldn't ignore it since if it's not set in the
883  * backup, the extended attributes in the filesystem will be stripped
884  * away.
885  */
886 #define FEATURE_RO_COMPAT_IGNORE        (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
887                                          EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
888 #define FEATURE_INCOMPAT_IGNORE         (EXT3_FEATURE_INCOMPAT_EXTENTS| \
889                                          EXT3_FEATURE_INCOMPAT_RECOVER)
890
891 int check_backup_super_block(e2fsck_t ctx)
892 {
893         ext2_filsys     fs = ctx->fs;
894         errcode_t       retval;
895         dgrp_t          g;
896         blk64_t         sb;
897         int             ret = 0;
898         char            buf[SUPERBLOCK_SIZE];
899         struct ext2_super_block *backup_sb;
900
901         /*
902          * If we are already writing out the backup blocks, then we
903          * don't need to test.  Also, if the filesystem is invalid, or
904          * the check was aborted or cancelled, we also don't want to
905          * do the backup.  If the filesystem was opened read-only then
906          * we can't do the backup.
907          */
908         if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) ||
909             !ext2fs_test_valid(fs) ||
910             (fs->super->s_state & EXT2_ERROR_FS) ||
911             (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) ||
912             (ctx->options & E2F_OPT_READONLY))
913                 return 0;
914
915         for (g = 1; g < fs->group_desc_count; g++) {
916                 if (!ext2fs_bg_has_super(fs, g))
917                         continue;
918
919                 sb = fs->super->s_first_data_block +
920                         (g * fs->super->s_blocks_per_group);
921
922                 retval = io_channel_read_blk(fs->io, sb, -SUPERBLOCK_SIZE,
923                                              buf);
924                 if (retval)
925                         continue;
926                 backup_sb = (struct ext2_super_block *) buf;
927 #ifdef WORDS_BIGENDIAN
928                 ext2fs_swap_super(backup_sb);
929 #endif
930                 if ((backup_sb->s_magic != EXT2_SUPER_MAGIC) ||
931                     (backup_sb->s_rev_level > EXT2_LIB_CURRENT_REV) ||
932                     ((backup_sb->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
933                      EXT2_MAX_BLOCK_LOG_SIZE) ||
934                     (EXT2_INODE_SIZE(backup_sb) < EXT2_GOOD_OLD_INODE_SIZE))
935                         continue;
936
937 #define SUPER_INCOMPAT_DIFFERENT(x)     \
938         ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) !=   \
939          (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE))
940 #define SUPER_RO_COMPAT_DIFFERENT(x)    \
941         ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) !=  \
942          (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE))
943 #define SUPER_DIFFERENT(x)              \
944         (fs->super->x != backup_sb->x)
945
946                 if (SUPER_DIFFERENT(s_feature_compat) ||
947                     SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) ||
948                     SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) ||
949                     SUPER_DIFFERENT(s_blocks_count) ||
950                     SUPER_DIFFERENT(s_inodes_count) ||
951                     memcmp(fs->super->s_uuid, backup_sb->s_uuid,
952                            sizeof(fs->super->s_uuid)))
953                         ret = 1;
954                 break;
955         }
956         return ret;
957 }