Whamcloud - gitweb
Many 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  * This routine may get stubbed out in special compilations of the
43  * e2fsck code..
44  */
45 #ifndef EXT2_SPECIAL_DEVICE_SIZE
46 errcode_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 }
52 #endif
53
54 void 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     inodes_per_block;
61         dgrp_t  i;
62         blk_t   should_be;
63         struct problem_context  pctx;
64         
65         inodes_per_block = (EXT2_INODE_SIZE(fs->super) + 
66                             EXT2_BLOCK_SIZE(fs->super) - 1) /
67                                     EXT2_BLOCK_SIZE(fs->super);
68
69         ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
70                  sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
71         ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
72                  sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
73         ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
74                 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
75                 
76         clear_problem_context(&pctx);
77
78         /*
79          * Verify the super block constants...
80          */
81         check_super_value(ctx, "inodes_count", s->s_inodes_count,
82                           MIN_CHECK, 1, 0);
83         check_super_value(ctx, "blocks_count", s->s_blocks_count,
84                           MIN_CHECK, 1, 0);
85         check_super_value(ctx, "first_data_block", s->s_first_data_block,
86                           MAX_CHECK, 0, s->s_blocks_count);
87         check_super_value(ctx, "log_frag_size", s->s_log_frag_size,
88                           MAX_CHECK, 0, 2);
89         check_super_value(ctx, "log_block_size", s->s_log_block_size,
90                           MIN_CHECK | MAX_CHECK, s->s_log_frag_size,
91                           2);
92         check_super_value(ctx, "frags_per_group", s->s_frags_per_group,
93                           MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s));
94         check_super_value(ctx, "blocks_per_group", s->s_blocks_per_group,
95                           MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s));
96         check_super_value(ctx, "inodes_per_group", s->s_inodes_per_group,
97                           MIN_CHECK | MAX_CHECK, 1,
98                           inodes_per_block * blocks_per_group);
99         check_super_value(ctx, "r_blocks_count", s->s_r_blocks_count,
100                           MAX_CHECK, 0, s->s_blocks_count);
101
102         if (!ctx->num_blocks) {
103                 pctx.errcode = e2fsck_get_device_size(ctx);
104                 if (pctx.errcode && pctx.errcode != EXT2_ET_UNIMPLEMENTED) {
105                         fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
106                         ctx->flags |= E2F_FLAG_ABORT;
107                         return;
108                 }
109                 if ((pctx.errcode != EXT2_ET_UNIMPLEMENTED) &&
110                     (ctx->num_blocks < s->s_blocks_count)) {
111                         pctx.blk = s->s_blocks_count;
112                         pctx.blk2 = ctx->num_blocks;
113                         if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
114                                 ctx->flags |= E2F_FLAG_ABORT;
115                                 return;
116                         }
117                 }
118         }
119
120         if (s->s_log_block_size != s->s_log_frag_size) {
121                 pctx.blk = EXT2_BLOCK_SIZE(s);
122                 pctx.blk2 = EXT2_FRAG_SIZE(s);
123                 fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx);
124                 ctx->flags |= E2F_FLAG_ABORT;
125                 return;
126         }
127
128         should_be = s->s_frags_per_group >>
129                 (s->s_log_block_size - s->s_log_frag_size);             
130         if (s->s_blocks_per_group != should_be) {
131                 pctx.blk = s->s_blocks_per_group;
132                 pctx.blk2 = should_be;
133                 fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx);
134                 ctx->flags |= E2F_FLAG_ABORT;
135                 return;
136         }
137
138         should_be = (s->s_log_block_size == 0) ? 1 : 0;
139         if (s->s_first_data_block != should_be) {
140                 pctx.blk = s->s_first_data_block;
141                 pctx.blk2 = should_be;
142                 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
143                 ctx->flags |= E2F_FLAG_ABORT;
144                 return;
145         }
146
147         should_be = s->s_inodes_per_group * fs->group_desc_count;
148         if (s->s_inodes_count != should_be) {
149                 pctx.ino = s->s_inodes_count;
150                 pctx.ino2 = should_be;
151                 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
152                         s->s_inodes_count = should_be;
153                         ext2fs_mark_super_dirty(fs);
154                 }
155         }
156
157         /*
158          * Verify the group descriptors....
159          */
160         first_block =  fs->super->s_first_data_block;
161         last_block = first_block + blocks_per_group;
162
163         for (i = 0; i < fs->group_desc_count; i++) {
164                 pctx.group = i;
165                 
166                 if (i == fs->group_desc_count - 1)
167                         last_block = fs->super->s_blocks_count;
168                 if ((fs->group_desc[i].bg_block_bitmap < first_block) ||
169                     (fs->group_desc[i].bg_block_bitmap >= last_block)) {
170                         pctx.blk = fs->group_desc[i].bg_block_bitmap;
171                         if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx)) {
172                                 fs->group_desc[i].bg_block_bitmap = 0;
173                                 ctx->invalid_block_bitmap_flag[i]++;
174                                 ctx->invalid_bitmaps++;
175                         }
176                 }
177                 if ((fs->group_desc[i].bg_inode_bitmap < first_block) ||
178                     (fs->group_desc[i].bg_inode_bitmap >= last_block)) {
179                         pctx.blk = fs->group_desc[i].bg_inode_bitmap;
180                         if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx)) {
181                                 fs->group_desc[i].bg_inode_bitmap = 0;
182                                 ctx->invalid_inode_bitmap_flag[i]++;
183                                 ctx->invalid_bitmaps++;
184                         }
185                 }
186                 if ((fs->group_desc[i].bg_inode_table < first_block) ||
187                     ((fs->group_desc[i].bg_inode_table +
188                       fs->inode_blocks_per_group - 1) >= last_block)) {
189                         pctx.blk = fs->group_desc[i].bg_inode_table;
190                         if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx)) {
191                                 fs->group_desc[i].bg_inode_table = 0;
192                                 ctx->invalid_inode_table_flag[i]++;
193                                 ctx->invalid_bitmaps++;
194                         }
195                 }
196                 first_block += fs->super->s_blocks_per_group;
197                 last_block += fs->super->s_blocks_per_group;
198         }
199         /*
200          * If we have invalid bitmaps, set the error state of the
201          * filesystem.
202          */
203         if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
204                 fs->super->s_state &= ~EXT2_VALID_FS;
205                 ext2fs_mark_super_dirty(fs);
206         }
207
208 #ifndef EXT2_SKIP_UUID
209         /*
210          * If the UUID field isn't assigned, assign it.
211          */
212         if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(s->s_uuid)) {
213                 clear_problem_context(&pctx);
214                 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
215                         uuid_generate(s->s_uuid);
216                         ext2fs_mark_super_dirty(fs);
217                 }
218         }
219 #endif
220         return;
221 }
222