]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - e2fsck/super.c
Many files:
[thirdparty/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 <stdio.h>
13 #ifdef HAVE_STDLIB_H
14 #include <stdlib.h>
15 #endif
16 #include <string.h>
17 #include <fcntl.h>
18 #include <ctype.h>
19 #include <termios.h>
20 #include <time.h>
21 #ifdef HAVE_GETOPT_H
22 #include <getopt.h>
23 #endif
24 #include <unistd.h>
25 #ifdef HAVE_ERRNO_H
26 #include <errno.h>
27 #endif
28 #ifdef HAVE_MNTENT_H
29 #include <mntent.h>
30 #endif
31 #include <sys/ioctl.h>
32 #include <malloc.h>
33
34 #include "uuid/uuid.h"
35 #include "e2fsck.h"
36 #include "problem.h"
37 #include "../version.h"
38
39 #define MIN_CHECK 1
40 #define MAX_CHECK 2
41
42 static void check_super_value(e2fsck_t ctx, const char *descr,
43 unsigned long value, int flags,
44 unsigned long min, unsigned long max)
45 {
46 struct problem_context pctx;
47
48 if (((flags & MIN_CHECK) && (value < min)) ||
49 ((flags & MAX_CHECK) && (value > max))) {
50 clear_problem_context(&pctx);
51 pctx.num = value;
52 pctx.str = descr;
53 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
54 fatal_error(0); /* never get here! */
55 }
56 }
57
58 void check_super_block(e2fsck_t ctx)
59 {
60 ext2_filsys fs = ctx->fs;
61 blk_t first_block, last_block;
62 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
63 blk_t blocks_per_group = fs->super->s_blocks_per_group;
64 int i;
65 blk_t should_be;
66 struct problem_context pctx;
67
68 ctx->invalid_inode_bitmap_flag = allocate_memory(sizeof(int) *
69 fs->group_desc_count,
70 "invalid_inode_bitmap");
71 ctx->invalid_block_bitmap_flag = allocate_memory(sizeof(int) *
72 fs->group_desc_count,
73 "invalid_block_bitmap");
74 ctx->invalid_inode_table_flag = allocate_memory(sizeof(int) *
75 fs->group_desc_count,
76 "invalid_inode_table");
77
78 clear_problem_context(&pctx);
79
80 /*
81 * Verify the super block constants...
82 */
83 check_super_value(ctx, "inodes_count", s->s_inodes_count,
84 MIN_CHECK, 1, 0);
85 check_super_value(ctx, "blocks_count", s->s_blocks_count,
86 MIN_CHECK, 1, 0);
87 check_super_value(ctx, "first_data_block", s->s_first_data_block,
88 MAX_CHECK, 0, s->s_blocks_count);
89 check_super_value(ctx, "log_frag_size", s->s_log_frag_size,
90 MAX_CHECK, 0, 2);
91 check_super_value(ctx, "log_block_size", s->s_log_block_size,
92 MIN_CHECK | MAX_CHECK, s->s_log_frag_size,
93 2);
94 check_super_value(ctx, "frags_per_group", s->s_frags_per_group,
95 MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s));
96 check_super_value(ctx, "blocks_per_group", s->s_blocks_per_group,
97 MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s));
98 check_super_value(ctx, "inodes_per_group", s->s_inodes_per_group,
99 MIN_CHECK, 1, 0);
100 check_super_value(ctx, "r_blocks_count", s->s_r_blocks_count,
101 MAX_CHECK, 0, s->s_blocks_count);
102
103 pctx.errcode = ext2fs_get_device_size(ctx->filesystem_name,
104 EXT2_BLOCK_SIZE(s),
105 &should_be);
106 if (pctx.errcode) {
107 fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
108 fatal_error(0);
109 }
110 if (should_be < s->s_blocks_count) {
111 pctx.blk = s->s_blocks_count;
112 pctx.blk2 = should_be;
113 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx))
114 fatal_error(0);
115 }
116
117 if (s->s_log_block_size != s->s_log_frag_size) {
118 pctx.blk = EXT2_BLOCK_SIZE(s);
119 pctx.blk2 = EXT2_FRAG_SIZE(s);
120 fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx);
121 fatal_error(0);
122 }
123
124 should_be = s->s_frags_per_group /
125 (s->s_log_block_size - s->s_log_frag_size + 1);
126 if (s->s_blocks_per_group != should_be) {
127 pctx.blk = s->s_blocks_per_group;
128 pctx.blk2 = should_be;
129 fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx);
130 fatal_error(0);
131 }
132
133 should_be = (s->s_log_block_size == 0) ? 1 : 0;
134 if (s->s_first_data_block != should_be) {
135 pctx.blk = s->s_first_data_block;
136 pctx.blk2 = should_be;
137 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
138 fatal_error(0);
139 }
140
141 /*
142 * Verify the group descriptors....
143 */
144 first_block = fs->super->s_first_data_block;
145 last_block = first_block + blocks_per_group;
146
147 for (i = 0; i < fs->group_desc_count; i++) {
148 pctx.group = i;
149
150 if (i == fs->group_desc_count - 1)
151 last_block = fs->super->s_blocks_count;
152 if ((fs->group_desc[i].bg_block_bitmap < first_block) ||
153 (fs->group_desc[i].bg_block_bitmap >= last_block)) {
154 pctx.blk = fs->group_desc[i].bg_block_bitmap;
155 if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx)) {
156 fs->group_desc[i].bg_block_bitmap = 0;
157 ctx->invalid_block_bitmap_flag[i]++;
158 ctx->invalid_bitmaps++;
159 }
160 }
161 if ((fs->group_desc[i].bg_inode_bitmap < first_block) ||
162 (fs->group_desc[i].bg_inode_bitmap >= last_block)) {
163 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
164 if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx)) {
165 fs->group_desc[i].bg_inode_bitmap = 0;
166 ctx->invalid_inode_bitmap_flag[i]++;
167 ctx->invalid_bitmaps++;
168 }
169 }
170 if ((fs->group_desc[i].bg_inode_table < first_block) ||
171 ((fs->group_desc[i].bg_inode_table +
172 fs->inode_blocks_per_group - 1) >= last_block)) {
173 pctx.blk = fs->group_desc[i].bg_inode_table;
174 if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx)) {
175 fs->group_desc[i].bg_inode_table = 0;
176 ctx->invalid_inode_table_flag[i]++;
177 ctx->invalid_bitmaps++;
178 }
179 }
180 first_block += fs->super->s_blocks_per_group;
181 last_block += fs->super->s_blocks_per_group;
182 }
183 /*
184 * If we have invalid bitmaps, set the error state of the
185 * filesystem.
186 */
187 if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
188 fs->super->s_state &= ~EXT2_VALID_FS;
189 ext2fs_mark_super_dirty(fs);
190 }
191
192 /*
193 * If the UUID field isn't assigned, assign it.
194 */
195 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(s->s_uuid)) {
196 clear_problem_context(&pctx);
197 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
198 uuid_generate(s->s_uuid);
199 ext2fs_mark_super_dirty(fs);
200 }
201 }
202 return;
203 }
204