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