]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/super.c
Many files:
[thirdparty/e2fsprogs.git] / e2fsck / super.c
CommitLineData
1b6bf175
TT
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
1b6bf175
TT
12#ifdef HAVE_ERRNO_H
13#include <errno.h>
14#endif
1b6bf175 15
54be2ccc 16#ifndef EXT2_SKIP_UUID
1b6bf175 17#include "uuid/uuid.h"
54be2ccc 18#endif
1b6bf175
TT
19#include "e2fsck.h"
20#include "problem.h"
1b6bf175
TT
21
22#define MIN_CHECK 1
23#define MAX_CHECK 2
24
25static void check_super_value(e2fsck_t ctx, const char *descr,
26 unsigned long value, int flags,
27 unsigned long min, unsigned long max)
28{
29 struct problem_context pctx;
30
31 if (((flags & MIN_CHECK) && (value < min)) ||
32 ((flags & MAX_CHECK) && (value > max))) {
33 clear_problem_context(&pctx);
34 pctx.num = value;
35 pctx.str = descr;
36 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
08b21301 37 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
1b6bf175
TT
38 }
39}
40
4313932c
TT
41/*
42 * This routine may get stubbed out in special compilations of the
43 * e2fsck code..
44 */
45#ifndef EXT2_SPECIAL_DEVICE_SIZE
46errcode_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}
4313932c
TT
52#endif
53
1b6bf175
TT
54void check_super_block(e2fsck_t ctx)
55{
56 ext2_filsys fs = ctx->fs;
57 blk_t first_block, last_block;
58 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
59 blk_t blocks_per_group = fs->super->s_blocks_per_group;
60 int i;
61 blk_t should_be;
62 struct problem_context pctx;
63
54dc7ca2 64 ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
f8188fff 65 sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
54dc7ca2 66 ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
f8188fff 67 sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
54dc7ca2 68 ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
f8188fff 69 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
1b6bf175
TT
70
71 clear_problem_context(&pctx);
72
73 /*
74 * Verify the super block constants...
75 */
76 check_super_value(ctx, "inodes_count", s->s_inodes_count,
77 MIN_CHECK, 1, 0);
78 check_super_value(ctx, "blocks_count", s->s_blocks_count,
79 MIN_CHECK, 1, 0);
80 check_super_value(ctx, "first_data_block", s->s_first_data_block,
81 MAX_CHECK, 0, s->s_blocks_count);
82 check_super_value(ctx, "log_frag_size", s->s_log_frag_size,
83 MAX_CHECK, 0, 2);
84 check_super_value(ctx, "log_block_size", s->s_log_block_size,
85 MIN_CHECK | MAX_CHECK, s->s_log_frag_size,
86 2);
87 check_super_value(ctx, "frags_per_group", s->s_frags_per_group,
88 MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s));
89 check_super_value(ctx, "blocks_per_group", s->s_blocks_per_group,
90 MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s));
91 check_super_value(ctx, "inodes_per_group", s->s_inodes_per_group,
92 MIN_CHECK, 1, 0);
93 check_super_value(ctx, "r_blocks_count", s->s_r_blocks_count,
94 MAX_CHECK, 0, s->s_blocks_count);
95
f8188fff 96 if (!ctx->num_blocks) {
4313932c 97 pctx.errcode = e2fsck_get_device_size(ctx);
f8188fff
TT
98 if (pctx.errcode && pctx.errcode != EXT2_ET_UNIMPLEMENTED) {
99 fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
08b21301
TT
100 ctx->flags |= E2F_FLAG_ABORT;
101 return;
102 }
f8188fff
TT
103 if ((pctx.errcode != EXT2_ET_UNIMPLEMENTED) &&
104 (ctx->num_blocks < s->s_blocks_count)) {
105 pctx.blk = s->s_blocks_count;
106 pctx.blk2 = ctx->num_blocks;
107 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
108 ctx->flags |= E2F_FLAG_ABORT;
109 return;
110 }
111 }
1b6bf175
TT
112 }
113
114 if (s->s_log_block_size != s->s_log_frag_size) {
115 pctx.blk = EXT2_BLOCK_SIZE(s);
116 pctx.blk2 = EXT2_FRAG_SIZE(s);
117 fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx);
08b21301
TT
118 ctx->flags |= E2F_FLAG_ABORT;
119 return;
1b6bf175
TT
120 }
121
17dba281
TT
122 should_be = s->s_frags_per_group >>
123 (s->s_log_block_size - s->s_log_frag_size);
1b6bf175
TT
124 if (s->s_blocks_per_group != should_be) {
125 pctx.blk = s->s_blocks_per_group;
126 pctx.blk2 = should_be;
127 fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx);
08b21301
TT
128 ctx->flags |= E2F_FLAG_ABORT;
129 return;
1b6bf175
TT
130 }
131
132 should_be = (s->s_log_block_size == 0) ? 1 : 0;
133 if (s->s_first_data_block != should_be) {
134 pctx.blk = s->s_first_data_block;
135 pctx.blk2 = should_be;
136 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
08b21301
TT
137 ctx->flags |= E2F_FLAG_ABORT;
138 return;
1b6bf175
TT
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
54be2ccc 192#ifndef EXT2_SKIP_UUID
1b6bf175
TT
193 /*
194 * If the UUID field isn't assigned, assign it.
195 */
196 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(s->s_uuid)) {
197 clear_problem_context(&pctx);
198 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
199 uuid_generate(s->s_uuid);
200 ext2fs_mark_super_dirty(fs);
201 }
202 }
54be2ccc 203#endif
1b6bf175
TT
204 return;
205}
206