1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
/*
* pass5.c --- check block and inode bitmaps against on-disk bitmaps
*
* Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*
*/
#include "e2fsck.h"
#include "problem.h"
static void check_block_bitmaps(e2fsck_t ctx);
static void check_inode_bitmaps(e2fsck_t ctx);
static void check_inode_end(e2fsck_t ctx);
static void check_block_end(e2fsck_t ctx);
void e2fsck_pass5(e2fsck_t ctx)
{
#ifdef RESOURCE_TRACK
struct resource_track rtrack;
#endif
struct problem_context pctx;
#ifdef MTRACE
mtrace_print("Pass 5");
#endif
#ifdef RESOURCE_TRACK
init_resource_track(&rtrack);
#endif
clear_problem_context(&pctx);
if (!(ctx->options & E2F_OPT_PREEN))
fix_problem(ctx, PR_5_PASS_HEADER, &pctx);
if (ctx->progress)
if ((ctx->progress)(ctx, 5, 0, 3))
return;
e2fsck_read_bitmaps(ctx);
if (ctx->progress)
if ((ctx->progress)(ctx, 5, 2, 3))
return;
check_block_bitmaps(ctx);
if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
return;
check_inode_bitmaps(ctx);
if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
return;
check_inode_end(ctx);
if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
return;
check_block_end(ctx);
if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
return;
if (ctx->progress)
if ((ctx->progress)(ctx, 5, 3, 3))
return;
ext2fs_free_inode_bitmap(ctx->inode_used_map);
ctx->inode_used_map = 0;
ext2fs_free_inode_bitmap(ctx->inode_dir_map);
ctx->inode_dir_map = 0;
ext2fs_free_block_bitmap(ctx->block_found_map);
ctx->block_found_map = 0;
#ifdef RESOURCE_TRACK
if (ctx->options & E2F_OPT_TIME2)
print_resource_track("Pass 5", &rtrack);
#endif
}
static void check_block_bitmaps(e2fsck_t ctx)
{
ext2_filsys fs = ctx->fs;
blk_t i;
int *free_array;
int group = 0;
int blocks = 0;
int free_blocks = 0;
int group_free = 0;
int actual, bitmap;
struct problem_context pctx;
int problem, fixit, had_problem;
errcode_t retval;
clear_problem_context(&pctx);
free_array = (int *) e2fsck_allocate_memory(ctx,
fs->group_desc_count * sizeof(int), "free block count array");
if ((fs->super->s_first_data_block <
ext2fs_get_block_bitmap_start(ctx->block_found_map)) ||
(fs->super->s_blocks_count-1 >
ext2fs_get_block_bitmap_end(ctx->block_found_map))) {
pctx.num = 1;
pctx.blk = fs->super->s_first_data_block;
pctx.blk2 = fs->super->s_blocks_count -1;
pctx.ino = ext2fs_get_block_bitmap_start(ctx->block_found_map);
pctx.ino2 = ext2fs_get_block_bitmap_end(ctx->block_found_map);
fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
ctx->flags |= E2F_FLAG_ABORT; /* fatal */
return;
}
if ((fs->super->s_first_data_block <
ext2fs_get_block_bitmap_start(fs->block_map)) ||
(fs->super->s_blocks_count-1 >
ext2fs_get_block_bitmap_end(fs->block_map))) {
pctx.num = 2;
pctx.blk = fs->super->s_first_data_block;
pctx.blk2 = fs->super->s_blocks_count -1;
pctx.ino = ext2fs_get_block_bitmap_start(fs->block_map);
pctx.ino2 = ext2fs_get_block_bitmap_end(fs->block_map);
fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
ctx->flags |= E2F_FLAG_ABORT; /* fatal */
return;
}
redo_counts:
had_problem = 0;
for (i = fs->super->s_first_data_block;
i < fs->super->s_blocks_count;
i++) {
actual = ext2fs_fast_test_block_bitmap(ctx->block_found_map, i);
bitmap = ext2fs_fast_test_block_bitmap(fs->block_map, i);
if (actual == bitmap)
goto do_counts;
if (!actual && bitmap) {
/*
* Block not used, but marked in use in the bitmap.
*/
problem = PR_5_UNUSED_BLOCK;
} else {
/*
* Block used, but not marked in use in the bitmap.
*/
problem = PR_5_BLOCK_USED;
}
pctx.blk = i;
fix_problem(ctx, problem, &pctx);
had_problem++;
do_counts:
if (!bitmap) {
group_free++;
free_blocks++;
}
blocks ++;
if ((blocks == fs->super->s_blocks_per_group) ||
(i == fs->super->s_blocks_count-1)) {
free_array[group] = group_free;
group ++;
blocks = 0;
group_free = 0;
}
}
if (had_problem)
fixit = end_problem_latch(ctx, PR_LATCH_BBITMAP);
else
fixit = -1;
if (fixit == 1) {
ext2fs_free_block_bitmap(fs->block_map);
retval = ext2fs_copy_bitmap(ctx->block_found_map,
&fs->block_map);
/* XXX check retval --- should never fail! */
ext2fs_set_bitmap_padding(fs->block_map);
ext2fs_mark_bb_dirty(fs);
/* Redo the counts */
blocks = 0; free_blocks = 0; group_free = 0; group = 0;
memset(free_array, 0, fs->group_desc_count * sizeof(int));
goto redo_counts;
} else if (fixit == 0)
ext2fs_unmark_valid(fs);
for (i = 0; i < fs->group_desc_count; i++) {
if (free_array[i] != fs->group_desc[i].bg_free_blocks_count) {
pctx.group = i;
pctx.blk = fs->group_desc[i].bg_free_blocks_count;
pctx.blk2 = free_array[i];
if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT_GROUP,
&pctx)) {
fs->group_desc[i].bg_free_blocks_count =
free_array[i];
ext2fs_mark_super_dirty(fs);
} else
ext2fs_unmark_valid(fs);
}
}
if (free_blocks != fs->super->s_free_blocks_count) {
pctx.group = 0;
pctx.blk = fs->super->s_free_blocks_count;
pctx.blk2 = free_blocks;
if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT, &pctx)) {
fs->super->s_free_blocks_count = free_blocks;
ext2fs_mark_super_dirty(fs);
} else
ext2fs_unmark_valid(fs);
}
ext2fs_free_mem((void **) &free_array);
}
static void check_inode_bitmaps(e2fsck_t ctx)
{
ext2_filsys fs = ctx->fs;
ino_t i;
int free_inodes = 0;
int group_free = 0;
int dirs_count = 0;
int group = 0;
int inodes = 0;
int *free_array;
int *dir_array;
int actual, bitmap;
errcode_t retval;
struct problem_context pctx;
int problem, fixit, had_problem;
clear_problem_context(&pctx);
free_array = (int *) e2fsck_allocate_memory(ctx,
fs->group_desc_count * sizeof(int), "free inode count array");
dir_array = (int *) e2fsck_allocate_memory(ctx,
fs->group_desc_count * sizeof(int), "directory count array");
if ((1 < ext2fs_get_inode_bitmap_start(ctx->inode_used_map)) ||
(fs->super->s_inodes_count >
ext2fs_get_inode_bitmap_end(ctx->inode_used_map))) {
pctx.num = 3;
pctx.blk = 1;
pctx.blk2 = fs->super->s_inodes_count;
pctx.ino = ext2fs_get_inode_bitmap_start(ctx->inode_used_map);
pctx.ino2 = ext2fs_get_inode_bitmap_end(ctx->inode_used_map);
fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
ctx->flags |= E2F_FLAG_ABORT; /* fatal */
return;
}
if ((1 < ext2fs_get_inode_bitmap_start(fs->inode_map)) ||
(fs->super->s_inodes_count >
ext2fs_get_inode_bitmap_end(fs->inode_map))) {
pctx.num = 4;
pctx.blk = 1;
pctx.blk2 = fs->super->s_inodes_count;
pctx.ino = ext2fs_get_inode_bitmap_start(fs->inode_map);
pctx.ino2 = ext2fs_get_inode_bitmap_end(fs->inode_map);
fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
ctx->flags |= E2F_FLAG_ABORT; /* fatal */
return;
}
redo_counts:
had_problem = 0;
for (i = 1; i <= fs->super->s_inodes_count; i++) {
actual = ext2fs_fast_test_inode_bitmap(ctx->inode_used_map, i);
bitmap = ext2fs_fast_test_inode_bitmap(fs->inode_map, i);
if (actual == bitmap)
goto do_counts;
if (!actual && bitmap) {
/*
* Inode wasn't used, but marked in bitmap
*/
problem = PR_5_UNUSED_INODE;
} else /* if (actual && !bitmap) */ {
/*
* Inode used, but not in bitmap
*/
problem = PR_5_INODE_USED;
}
pctx.ino = i;
fix_problem(ctx, problem, &pctx);
had_problem++;
do_counts:
if (!bitmap) {
group_free++;
free_inodes++;
} else {
if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, i))
dirs_count++;
}
inodes++;
if ((inodes == fs->super->s_inodes_per_group) ||
(i == fs->super->s_inodes_count)) {
free_array[group] = group_free;
dir_array[group] = dirs_count;
group ++;
inodes = 0;
group_free = 0;
dirs_count = 0;
}
}
if (had_problem)
fixit = end_problem_latch(ctx, PR_LATCH_IBITMAP);
else
fixit = -1;
if (fixit == 1) {
ext2fs_free_inode_bitmap(fs->inode_map);
retval = ext2fs_copy_bitmap(ctx->inode_used_map,
&fs->inode_map);
/* XXX check retval --- should never fail! */
ext2fs_set_bitmap_padding(fs->inode_map);
ext2fs_mark_ib_dirty(fs);
/* redo counts */
inodes = 0; free_inodes = 0; group_free = 0;
dirs_count = 0; group = 0;
memset(free_array, 0, fs->group_desc_count * sizeof(int));
memset(dir_array, 0, fs->group_desc_count * sizeof(int));
goto redo_counts;
} else if (fixit == 0)
ext2fs_unmark_valid(fs);
for (i = 0; i < fs->group_desc_count; i++) {
if (free_array[i] != fs->group_desc[i].bg_free_inodes_count) {
pctx.group = i;
pctx.ino = fs->group_desc[i].bg_free_inodes_count;
pctx.ino2 = free_array[i];
if (fix_problem(ctx, PR_5_FREE_INODE_COUNT_GROUP,
&pctx)) {
fs->group_desc[i].bg_free_inodes_count =
free_array[i];
ext2fs_mark_super_dirty(fs);
} else
ext2fs_unmark_valid(fs);
}
if (dir_array[i] != fs->group_desc[i].bg_used_dirs_count) {
pctx.group = i;
pctx.ino = fs->group_desc[i].bg_used_dirs_count;
pctx.ino2 = dir_array[i];
if (fix_problem(ctx, PR_5_FREE_DIR_COUNT_GROUP,
&pctx)) {
fs->group_desc[i].bg_used_dirs_count =
dir_array[i];
ext2fs_mark_super_dirty(fs);
} else
ext2fs_unmark_valid(fs);
}
}
if (free_inodes != fs->super->s_free_inodes_count) {
pctx.group = -1;
pctx.ino = fs->super->s_free_inodes_count;
pctx.ino2 = free_inodes;
if (fix_problem(ctx, PR_5_FREE_INODE_COUNT, &pctx)) {
fs->super->s_free_inodes_count = free_inodes;
ext2fs_mark_super_dirty(fs);
} else
ext2fs_unmark_valid(fs);
}
ext2fs_free_mem((void **) &free_array);
ext2fs_free_mem((void **) &dir_array);
}
static void check_inode_end(e2fsck_t ctx)
{
ext2_filsys fs = ctx->fs;
ino_t end, save_inodes_count, i;
struct problem_context pctx;
clear_problem_context(&pctx);
end = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map, end,
&save_inodes_count);
if (pctx.errcode) {
pctx.num = 1;
fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
ctx->flags |= E2F_FLAG_ABORT; /* fatal */
return;
}
if (save_inodes_count == end)
return;
for (i = save_inodes_count + 1; i <= end; i++) {
if (!ext2fs_test_inode_bitmap(fs->inode_map, i)) {
if (fix_problem(ctx, PR_5_INODE_BMAP_PADDING, &pctx)) {
for (i = save_inodes_count + 1; i <= end; i++)
ext2fs_mark_inode_bitmap(fs->inode_map,
i);
ext2fs_mark_ib_dirty(fs);
} else
ext2fs_unmark_valid(fs);
break;
}
}
pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map,
save_inodes_count, 0);
if (pctx.errcode) {
pctx.num = 2;
fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
ctx->flags |= E2F_FLAG_ABORT; /* fatal */
return;
}
}
static void check_block_end(e2fsck_t ctx)
{
ext2_filsys fs = ctx->fs;
blk_t end, save_blocks_count, i;
struct problem_context pctx;
clear_problem_context(&pctx);
end = fs->block_map->start +
(EXT2_BLOCKS_PER_GROUP(fs->super) * fs->group_desc_count) - 1;
pctx.errcode = ext2fs_fudge_block_bitmap_end(fs->block_map, end,
&save_blocks_count);
if (pctx.errcode) {
pctx.num = 3;
fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
ctx->flags |= E2F_FLAG_ABORT; /* fatal */
return;
}
if (save_blocks_count == end)
return;
for (i = save_blocks_count + 1; i <= end; i++) {
if (!ext2fs_test_block_bitmap(fs->block_map, i)) {
if (fix_problem(ctx, PR_5_BLOCK_BMAP_PADDING, &pctx)) {
for (i = save_blocks_count + 1; i < end; i++)
ext2fs_mark_block_bitmap(fs->block_map,
i);
ext2fs_mark_bb_dirty(fs);
} else
ext2fs_unmark_valid(fs);
break;
}
}
pctx.errcode = ext2fs_fudge_block_bitmap_end(fs->block_map,
save_blocks_count, 0);
if (pctx.errcode) {
pctx.num = 4;
fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
ctx->flags |= E2F_FLAG_ABORT; /* fatal */
return;
}
}
|