]> 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,
7f813ba3 27 unsigned long min_val, unsigned long max_val)
1b6bf175
TT
28{
29 struct problem_context pctx;
30
7f813ba3
TT
31 if (((flags & MIN_CHECK) && (value < min_val)) ||
32 ((flags & MAX_CHECK) && (value > max_val))) {
1b6bf175
TT
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
80bfaa3e
TT
54/*
55 * helper function to release an inode
56 */
57struct process_block_struct {
58 ino_t ino;
59 e2fsck_t ctx;
60 struct problem_context *pctx;
61 int abort;
62};
63
64static int release_inode_block(ext2_filsys fs,
65 blk_t *block_nr,
66 int blockcnt,
67 void *priv_data)
68{
69 struct process_block_struct *pb;
70 e2fsck_t ctx;
71 struct problem_context *pctx;
72 blk_t blk = *block_nr;
73
74 pb = (struct process_block_struct *) priv_data;
75 ctx = pb->ctx;
76 pctx = pb->pctx;
77
78 pctx->blk = blk;
79 pctx->blkcount = blockcnt;
80
81 if (HOLE_BLKADDR(blk))
82 return 0;
83
84 if ((blk < fs->super->s_first_data_block) ||
85 (blk >= fs->super->s_blocks_count)) {
86 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx);
87 pb->abort = 1;
88 return BLOCK_ABORT;
89 }
90
91 if (!ext2fs_test_block_bitmap(fs->block_map, blk)) {
92 fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx);
93 pb->abort = 1;
94 return BLOCK_ABORT;
95 }
96
97 ext2fs_unmark_block_bitmap(fs->block_map, blk);
98
99 return 0;
100}
101
102/*
103 * This function releases an inode. Returns 1 if an inconsistency was
104 * found.
105 */
106static int release_inode_blocks(e2fsck_t ctx, ino_t ino, char* block_buf,
107 struct problem_context *pctx)
108{
109 ext2_filsys fs = ctx->fs;
110 errcode_t retval;
111 struct process_block_struct pb;
112
113 pb.ino = ino;
114 pb.ctx = ctx;
115 pb.abort = 0;
116 pb.pctx = pctx;
117 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
118 release_inode_block, &pb);
119 if (retval) {
120 com_err("delete_file", retval,
121 _("while calling ext2fs_block_iterate for inode %d"),
122 ino);
123 return 1;
124 }
125 if (pb.abort)
126 return 1;
127
128 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
129 ext2fs_mark_ib_dirty(fs);
130 ext2fs_mark_bb_dirty(fs);
131 return 0;
132}
133
134/*
135 * This function releases all of the orphan inodes. It returns 1 if
136 * it hit some error, and 0 on success.
137 */
138static int release_orphan_inodes(e2fsck_t ctx)
139{
140 ext2_filsys fs = ctx->fs;
141 ino_t ino, next_ino;
142 struct ext2_inode inode;
143 struct problem_context pctx;
144 char *block_buf;
145
146 if ((ino = fs->super->s_last_orphan) == 0)
147 return 0;
148
149 if ((ino < EXT2_FIRST_INODE(fs->super)) ||
150 (ino > fs->super->s_inodes_count)) {
151 clear_problem_context(&pctx);
152 pctx.ino;
153 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
154 return 1;
155 }
156
157 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
158 "block interate buffer");
159 e2fsck_read_bitmaps(ctx);
160
161 while (ino) {
162#ifdef JFS_DEBUG
163 printf("Clearing orphan inode %d\n", ino);
164#endif
165
166 e2fsck_read_inode(ctx, ino, &inode, "delete_file");
167 clear_problem_context(&pctx);
168 pctx.ino;
169 pctx.inode = &inode;
170
171 if (inode.i_links_count) {
172 fix_problem(ctx, PR_0_ORPHAN_INODE_INUSE, &pctx);
173 goto abort;
174 }
175 next_ino = inode.i_dtime;
176 if (next_ino &&
177 ((ino < EXT2_FIRST_INODE(fs->super)) ||
178 (ino > fs->super->s_inodes_count))) {
179 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
180 goto abort;
181 }
182
183 if (release_inode_blocks(ctx, ino, block_buf, &pctx))
184 goto abort;
185
186 inode.i_dtime = time(0);
187 e2fsck_write_inode(ctx, ino, &inode, "delete_file");
188 ino = next_ino;
189 }
190 return 0;
191abort:
192 ext2fs_free_mem((void **) &block_buf);
193 return 1;
194}
195
196
1b6bf175
TT
197void check_super_block(e2fsck_t ctx)
198{
199 ext2_filsys fs = ctx->fs;
200 blk_t first_block, last_block;
201 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
202 blk_t blocks_per_group = fs->super->s_blocks_per_group;
78cf0547 203 int inodes_per_block;
7f813ba3 204 dgrp_t i;
1b6bf175
TT
205 blk_t should_be;
206 struct problem_context pctx;
78cf0547
TT
207
208 inodes_per_block = (EXT2_INODE_SIZE(fs->super) +
209 EXT2_BLOCK_SIZE(fs->super) - 1) /
210 EXT2_BLOCK_SIZE(fs->super);
1b6bf175 211
54dc7ca2 212 ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
f8188fff 213 sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
54dc7ca2 214 ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
f8188fff 215 sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
54dc7ca2 216 ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
f8188fff 217 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
1b6bf175
TT
218
219 clear_problem_context(&pctx);
220
221 /*
222 * Verify the super block constants...
223 */
224 check_super_value(ctx, "inodes_count", s->s_inodes_count,
225 MIN_CHECK, 1, 0);
226 check_super_value(ctx, "blocks_count", s->s_blocks_count,
227 MIN_CHECK, 1, 0);
228 check_super_value(ctx, "first_data_block", s->s_first_data_block,
229 MAX_CHECK, 0, s->s_blocks_count);
230 check_super_value(ctx, "log_frag_size", s->s_log_frag_size,
231 MAX_CHECK, 0, 2);
232 check_super_value(ctx, "log_block_size", s->s_log_block_size,
233 MIN_CHECK | MAX_CHECK, s->s_log_frag_size,
234 2);
235 check_super_value(ctx, "frags_per_group", s->s_frags_per_group,
236 MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s));
237 check_super_value(ctx, "blocks_per_group", s->s_blocks_per_group,
238 MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s));
239 check_super_value(ctx, "inodes_per_group", s->s_inodes_per_group,
78cf0547
TT
240 MIN_CHECK | MAX_CHECK, 1,
241 inodes_per_block * blocks_per_group);
1b6bf175
TT
242 check_super_value(ctx, "r_blocks_count", s->s_r_blocks_count,
243 MAX_CHECK, 0, s->s_blocks_count);
244
f8188fff 245 if (!ctx->num_blocks) {
4313932c 246 pctx.errcode = e2fsck_get_device_size(ctx);
f8188fff
TT
247 if (pctx.errcode && pctx.errcode != EXT2_ET_UNIMPLEMENTED) {
248 fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
08b21301
TT
249 ctx->flags |= E2F_FLAG_ABORT;
250 return;
251 }
f8188fff
TT
252 if ((pctx.errcode != EXT2_ET_UNIMPLEMENTED) &&
253 (ctx->num_blocks < s->s_blocks_count)) {
254 pctx.blk = s->s_blocks_count;
255 pctx.blk2 = ctx->num_blocks;
256 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
257 ctx->flags |= E2F_FLAG_ABORT;
258 return;
259 }
260 }
1b6bf175
TT
261 }
262
263 if (s->s_log_block_size != s->s_log_frag_size) {
264 pctx.blk = EXT2_BLOCK_SIZE(s);
265 pctx.blk2 = EXT2_FRAG_SIZE(s);
266 fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx);
08b21301
TT
267 ctx->flags |= E2F_FLAG_ABORT;
268 return;
1b6bf175
TT
269 }
270
17dba281
TT
271 should_be = s->s_frags_per_group >>
272 (s->s_log_block_size - s->s_log_frag_size);
1b6bf175
TT
273 if (s->s_blocks_per_group != should_be) {
274 pctx.blk = s->s_blocks_per_group;
275 pctx.blk2 = should_be;
276 fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx);
08b21301
TT
277 ctx->flags |= E2F_FLAG_ABORT;
278 return;
1b6bf175
TT
279 }
280
281 should_be = (s->s_log_block_size == 0) ? 1 : 0;
282 if (s->s_first_data_block != should_be) {
283 pctx.blk = s->s_first_data_block;
284 pctx.blk2 = should_be;
285 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
08b21301
TT
286 ctx->flags |= E2F_FLAG_ABORT;
287 return;
1b6bf175
TT
288 }
289
d4b0ce03
TT
290 should_be = s->s_inodes_per_group * fs->group_desc_count;
291 if (s->s_inodes_count != should_be) {
292 pctx.ino = s->s_inodes_count;
293 pctx.ino2 = should_be;
294 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
295 s->s_inodes_count = should_be;
296 ext2fs_mark_super_dirty(fs);
297 }
298 }
299
1b6bf175
TT
300 /*
301 * Verify the group descriptors....
302 */
303 first_block = fs->super->s_first_data_block;
304 last_block = first_block + blocks_per_group;
305
306 for (i = 0; i < fs->group_desc_count; i++) {
307 pctx.group = i;
308
309 if (i == fs->group_desc_count - 1)
310 last_block = fs->super->s_blocks_count;
311 if ((fs->group_desc[i].bg_block_bitmap < first_block) ||
312 (fs->group_desc[i].bg_block_bitmap >= last_block)) {
313 pctx.blk = fs->group_desc[i].bg_block_bitmap;
314 if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx)) {
315 fs->group_desc[i].bg_block_bitmap = 0;
316 ctx->invalid_block_bitmap_flag[i]++;
317 ctx->invalid_bitmaps++;
318 }
319 }
320 if ((fs->group_desc[i].bg_inode_bitmap < first_block) ||
321 (fs->group_desc[i].bg_inode_bitmap >= last_block)) {
322 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
323 if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx)) {
324 fs->group_desc[i].bg_inode_bitmap = 0;
325 ctx->invalid_inode_bitmap_flag[i]++;
326 ctx->invalid_bitmaps++;
327 }
328 }
329 if ((fs->group_desc[i].bg_inode_table < first_block) ||
330 ((fs->group_desc[i].bg_inode_table +
331 fs->inode_blocks_per_group - 1) >= last_block)) {
332 pctx.blk = fs->group_desc[i].bg_inode_table;
333 if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx)) {
334 fs->group_desc[i].bg_inode_table = 0;
335 ctx->invalid_inode_table_flag[i]++;
336 ctx->invalid_bitmaps++;
337 }
338 }
339 first_block += fs->super->s_blocks_per_group;
340 last_block += fs->super->s_blocks_per_group;
341 }
342 /*
343 * If we have invalid bitmaps, set the error state of the
344 * filesystem.
345 */
346 if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
347 fs->super->s_state &= ~EXT2_VALID_FS;
348 ext2fs_mark_super_dirty(fs);
349 }
350
4ea0a110
TT
351 clear_problem_context(&pctx);
352
54be2ccc 353#ifndef EXT2_SKIP_UUID
1b6bf175
TT
354 /*
355 * If the UUID field isn't assigned, assign it.
356 */
357 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(s->s_uuid)) {
1b6bf175
TT
358 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
359 uuid_generate(s->s_uuid);
360 ext2fs_mark_super_dirty(fs);
361 }
362 }
54be2ccc 363#endif
4ea0a110
TT
364
365 /*
366 * For the Hurd, check to see if the filetype option is set,
367 * since it doesn't support it.
368 */
80bfaa3e
TT
369 if (!(ctx->options & E2F_OPT_READONLY) &&
370 fs->super->s_creator_os == EXT2_OS_HURD &&
4ea0a110
TT
371 (fs->super->s_feature_incompat &
372 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
373 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
374 fs->super->s_feature_incompat &=
375 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
376 ext2fs_mark_super_dirty(fs);
377
378 }
379 }
80bfaa3e
TT
380
381 /*
382 * Clean up any orphan inodes, if present.
383 */
384 if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
385 fs->super->s_state &= ~EXT2_VALID_FS;
386 ext2fs_mark_super_dirty(ctx->fs);
387 }
388
1b6bf175
TT
389 return;
390}
80bfaa3e
TT
391
392