]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/super.c
libext2fs: clean up ext2fs_bg_flags_ interfaces
[thirdparty/e2fsprogs.git] / e2fsck / super.c
CommitLineData
1b6bf175
TT
1/*
2 * e2fsck.c - superblock checks
efc6f628 3 *
1b6bf175
TT
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
80bfaa3e
TT
41/*
42 * helper function to release an inode
43 */
44struct process_block_struct {
8394902e
TT
45 e2fsck_t ctx;
46 char *buf;
80bfaa3e 47 struct problem_context *pctx;
8394902e
TT
48 int truncating;
49 int truncate_offset;
54434927 50 e2_blkcnt_t truncate_block;
8394902e
TT
51 int truncated_blocks;
52 int abort;
53 errcode_t errcode;
80bfaa3e
TT
54};
55
56static int release_inode_block(ext2_filsys fs,
57 blk_t *block_nr,
54434927
TT
58 e2_blkcnt_t blockcnt,
59 blk_t ref_blk EXT2FS_ATTR((unused)),
60 int ref_offset EXT2FS_ATTR((unused)),
80bfaa3e
TT
61 void *priv_data)
62{
63 struct process_block_struct *pb;
8394902e
TT
64 e2fsck_t ctx;
65 struct problem_context *pctx;
66 blk_t blk = *block_nr;
67 int retval = 0;
80bfaa3e
TT
68
69 pb = (struct process_block_struct *) priv_data;
70 ctx = pb->ctx;
71 pctx = pb->pctx;
72
73 pctx->blk = blk;
74 pctx->blkcount = blockcnt;
75
76 if (HOLE_BLKADDR(blk))
77 return 0;
78
79 if ((blk < fs->super->s_first_data_block) ||
4efbac6f 80 (blk >= ext2fs_blocks_count(fs->super))) {
80bfaa3e 81 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx);
53ef44c4 82 return_abort:
80bfaa3e
TT
83 pb->abort = 1;
84 return BLOCK_ABORT;
85 }
86
c5d2f50d 87 if (!ext2fs_test_block_bitmap2(fs->block_map, blk)) {
80bfaa3e 88 fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx);
53ef44c4 89 goto return_abort;
8394902e
TT
90 }
91
92 /*
93 * If we are deleting an orphan, then we leave the fields alone.
94 * If we are truncating an orphan, then update the inode fields
95 * and clean up any partial block data.
96 */
97 if (pb->truncating) {
98 /*
99 * We only remove indirect blocks if they are
100 * completely empty.
101 */
102 if (blockcnt < 0) {
103 int i, limit;
53ef44c4 104 blk_t *bp;
efc6f628 105
24a117ab 106 pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
8394902e
TT
107 pb->buf);
108 if (pb->errcode)
53ef44c4 109 goto return_abort;
8394902e
TT
110
111 limit = fs->blocksize >> 2;
53ef44c4
TT
112 for (i = 0, bp = (blk_t *) pb->buf;
113 i < limit; i++, bp++)
114 if (*bp)
8394902e
TT
115 return 0;
116 }
117 /*
118 * We don't remove direct blocks until we've reached
119 * the truncation block.
120 */
121 if (blockcnt >= 0 && blockcnt < pb->truncate_block)
122 return 0;
123 /*
124 * If part of the last block needs truncating, we do
125 * it here.
126 */
127 if ((blockcnt == pb->truncate_block) && pb->truncate_offset) {
24a117ab 128 pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
8394902e
TT
129 pb->buf);
130 if (pb->errcode)
53ef44c4 131 goto return_abort;
8394902e
TT
132 memset(pb->buf + pb->truncate_offset, 0,
133 fs->blocksize - pb->truncate_offset);
24a117ab 134 pb->errcode = io_channel_write_blk64(fs->io, blk, 1,
8394902e
TT
135 pb->buf);
136 if (pb->errcode)
53ef44c4 137 goto return_abort;
8394902e
TT
138 }
139 pb->truncated_blocks++;
140 *block_nr = 0;
141 retval |= BLOCK_CHANGED;
80bfaa3e 142 }
efc6f628 143
0684a4f3 144 ext2fs_block_alloc_stats(fs, blk, -1);
8394902e 145 return retval;
80bfaa3e 146}
efc6f628 147
80bfaa3e
TT
148/*
149 * This function releases an inode. Returns 1 if an inconsistency was
8394902e
TT
150 * found. If the inode has a link count, then it is being truncated and
151 * not deleted.
80bfaa3e 152 */
86c627ec 153static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
721edd0e 154 struct ext2_inode *inode, char *block_buf,
80bfaa3e
TT
155 struct problem_context *pctx)
156{
0684a4f3 157 struct process_block_struct pb;
8394902e 158 ext2_filsys fs = ctx->fs;
80bfaa3e 159 errcode_t retval;
0684a4f3 160 __u32 count;
80bfaa3e 161
42475e28
TT
162 if (!ext2fs_inode_has_valid_blocks(inode))
163 return 0;
164
8394902e 165 pb.buf = block_buf + 3 * ctx->fs->blocksize;
80bfaa3e
TT
166 pb.ctx = ctx;
167 pb.abort = 0;
8394902e 168 pb.errcode = 0;
80bfaa3e 169 pb.pctx = pctx;
8394902e
TT
170 if (inode->i_links_count) {
171 pb.truncating = 1;
54434927 172 pb.truncate_block = (e2_blkcnt_t)
8394902e
TT
173 ((((long long)inode->i_size_high << 32) +
174 inode->i_size + fs->blocksize - 1) /
175 fs->blocksize);
176 pb.truncate_offset = inode->i_size % fs->blocksize;
177 } else {
178 pb.truncating = 0;
179 pb.truncate_block = 0;
180 pb.truncate_offset = 0;
181 }
182 pb.truncated_blocks = 0;
efc6f628 183 retval = ext2fs_block_iterate2(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE,
8394902e 184 block_buf, release_inode_block, &pb);
80bfaa3e 185 if (retval) {
8394902e 186 com_err("release_inode_blocks", retval,
80bfaa3e
TT
187 _("while calling ext2fs_block_iterate for inode %d"),
188 ino);
189 return 1;
190 }
191 if (pb.abort)
192 return 1;
193
8394902e
TT
194 /* Refresh the inode since ext2fs_block_iterate may have changed it */
195 e2fsck_read_inode(ctx, ino, inode, "release_inode_blocks");
196
197 if (pb.truncated_blocks)
1ca1059f 198 ext2fs_iblk_sub_blocks(fs, inode, pb.truncated_blocks);
8394902e 199
a63745e8
VAH
200 if (ext2fs_file_acl_block(inode)) {
201 retval = ext2fs_adjust_ea_refcount(fs, ext2fs_file_acl_block(inode),
0684a4f3
TT
202 block_buf, -1, &count);
203 if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) {
204 retval = 0;
205 count = 1;
206 }
207 if (retval) {
208 com_err("release_inode_blocks", retval,
a3e025c7 209 _("while calling ext2fs_adjust_ea_refcount for inode %d"),
0684a4f3
TT
210 ino);
211 return 1;
212 }
213 if (count == 0)
a63745e8
VAH
214 ext2fs_block_alloc_stats(fs,
215 ext2fs_file_acl_block(inode),
216 -1);
217 ext2fs_file_acl_block_set(inode, 0);
0684a4f3 218 }
80bfaa3e
TT
219 return 0;
220}
221
222/*
223 * This function releases all of the orphan inodes. It returns 1 if
224 * it hit some error, and 0 on success.
225 */
226static int release_orphan_inodes(e2fsck_t ctx)
227{
228 ext2_filsys fs = ctx->fs;
86c627ec 229 ext2_ino_t ino, next_ino;
80bfaa3e
TT
230 struct ext2_inode inode;
231 struct problem_context pctx;
232 char *block_buf;
233
234 if ((ino = fs->super->s_last_orphan) == 0)
235 return 0;
236
25c63ba0
TT
237 /*
238 * Win or lose, we won't be using the head of the orphan inode
239 * list again.
240 */
241 fs->super->s_last_orphan = 0;
242 ext2fs_mark_super_dirty(fs);
eb4ab510
TT
243
244 /*
245 * If the filesystem contains errors, don't run the orphan
246 * list, since the orphan list can't be trusted; and we're
247 * going to be running a full e2fsck run anyway...
248 */
249 if (fs->super->s_state & EXT2_ERROR_FS)
250 return 0;
efc6f628 251
80bfaa3e
TT
252 if ((ino < EXT2_FIRST_INODE(fs->super)) ||
253 (ino > fs->super->s_inodes_count)) {
254 clear_problem_context(&pctx);
53ef44c4 255 pctx.ino = ino;
80bfaa3e
TT
256 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
257 return 1;
258 }
259
8394902e 260 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
9b565759 261 "block iterate buffer");
80bfaa3e 262 e2fsck_read_bitmaps(ctx);
efc6f628 263
80bfaa3e 264 while (ino) {
8394902e 265 e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes");
80bfaa3e 266 clear_problem_context(&pctx);
ecf1b776 267 pctx.ino = ino;
80bfaa3e 268 pctx.inode = &inode;
f0b8c87d
TT
269 pctx.str = inode.i_links_count ? _("Truncating") :
270 _("Clearing");
80bfaa3e 271
8394902e 272 fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
ecf1b776 273
80bfaa3e
TT
274 next_ino = inode.i_dtime;
275 if (next_ino &&
99a2cc96
TT
276 ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
277 (next_ino > fs->super->s_inodes_count))) {
278 pctx.ino = next_ino;
80bfaa3e 279 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
53ef44c4 280 goto return_abort;
80bfaa3e
TT
281 }
282
8394902e 283 if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
53ef44c4 284 goto return_abort;
ecf1b776 285
8394902e 286 if (!inode.i_links_count) {
0684a4f3
TT
287 ext2fs_inode_alloc_stats2(fs, ino, -1,
288 LINUX_S_ISDIR(inode.i_mode));
1f3ad14a 289 inode.i_dtime = ctx->now;
eb16f861
ST
290 } else {
291 inode.i_dtime = 0;
8394902e
TT
292 }
293 e2fsck_write_inode(ctx, ino, &inode, "delete_file");
80bfaa3e
TT
294 ino = next_ino;
295 }
c4e3d3f3 296 ext2fs_free_mem(&block_buf);
80bfaa3e 297 return 0;
53ef44c4 298return_abort:
c4e3d3f3 299 ext2fs_free_mem(&block_buf);
80bfaa3e
TT
300 return 1;
301}
302
c3ffaf83
TT
303/*
304 * Check the resize inode to make sure it is sane. We check both for
305 * the case where on-line resizing is not enabled (in which case the
306 * resize inode should be cleared) as well as the case where on-line
307 * resizing is enabled.
308 */
69d0edfd 309void check_resize_inode(e2fsck_t ctx)
c3ffaf83
TT
310{
311 ext2_filsys fs = ctx->fs;
312 struct ext2_inode inode;
313 struct problem_context pctx;
642935c0
TT
314 int i, gdt_off, ind_off;
315 dgrp_t j;
c3ffaf83
TT
316 blk_t blk, pblk, expect;
317 __u32 *dind_buf = 0, *ind_buf;
318 errcode_t retval;
319
320 clear_problem_context(&pctx);
c3ffaf83 321
efc6f628
TT
322 /*
323 * If the resize inode feature isn't set, then
ea774315 324 * s_reserved_gdt_blocks must be zero.
c3ffaf83 325 */
efc6f628 326 if (!(fs->super->s_feature_compat &
c3ffaf83
TT
327 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
328 if (fs->super->s_reserved_gdt_blocks) {
329 pctx.num = fs->super->s_reserved_gdt_blocks;
330 if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
331 &pctx)) {
332 fs->super->s_reserved_gdt_blocks = 0;
333 ext2fs_mark_super_dirty(fs);
334 }
335 }
ea774315
TT
336 }
337
1f3ad14a 338 /* Read the resize inode */
ea774315
TT
339 pctx.ino = EXT2_RESIZE_INO;
340 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
341 if (retval) {
efc6f628 342 if (fs->super->s_feature_compat &
ea774315
TT
343 EXT2_FEATURE_COMPAT_RESIZE_INODE)
344 ctx->flags |= E2F_FLAG_RESIZE_INODE;
345 return;
346 }
347
efc6f628
TT
348 /*
349 * If the resize inode feature isn't set, check to make sure
ea774315
TT
350 * the resize inode is cleared; then we're done.
351 */
efc6f628 352 if (!(fs->super->s_feature_compat &
ea774315 353 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
c3ffaf83
TT
354 for (i=0; i < EXT2_N_BLOCKS; i++) {
355 if (inode.i_block[i])
356 break;
357 }
358 if ((i < EXT2_N_BLOCKS) &&
359 fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
360 memset(&inode, 0, sizeof(inode));
361 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
362 "clear_resize");
363 }
364 return;
365 }
ea774315 366
efc6f628 367 /*
c3ffaf83
TT
368 * The resize inode feature is enabled; check to make sure the
369 * only block in use is the double indirect block
370 */
371 blk = inode.i_block[EXT2_DIND_BLOCK];
372 for (i=0; i < EXT2_N_BLOCKS; i++) {
373 if (i != EXT2_DIND_BLOCK && inode.i_block[i])
374 break;
375 }
d4dc0a9e
TT
376 if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
377 !(inode.i_mode & LINUX_S_IFREG) ||
c3ffaf83 378 (blk < fs->super->s_first_data_block ||
4efbac6f 379 blk >= ext2fs_blocks_count(fs->super))) {
c3ffaf83
TT
380 resize_inode_invalid:
381 if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
382 memset(&inode, 0, sizeof(inode));
383 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
384 "clear_resize");
385 ctx->flags |= E2F_FLAG_RESIZE_INODE;
386 }
387 if (!(ctx->options & E2F_OPT_READONLY)) {
388 fs->super->s_state &= ~EXT2_VALID_FS;
389 ext2fs_mark_super_dirty(fs);
390 }
391 goto cleanup;
392 }
393 dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
394 "resize dind buffer");
395 ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
396
dc8ce346 397 retval = ext2fs_read_ind_block(fs, blk, dind_buf);
c3ffaf83
TT
398 if (retval)
399 goto resize_inode_invalid;
400
401 gdt_off = fs->desc_blocks;
402 pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
efc6f628 403 for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
c3ffaf83
TT
404 i++, gdt_off++, pblk++) {
405 gdt_off %= fs->blocksize/4;
406 if (dind_buf[gdt_off] != pblk)
407 goto resize_inode_invalid;
dc8ce346 408 retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
efc6f628 409 if (retval)
c3ffaf83
TT
410 goto resize_inode_invalid;
411 ind_off = 0;
412 for (j = 1; j < fs->group_desc_count; j++) {
413 if (!ext2fs_bg_has_super(fs, j))
414 continue;
415 expect = pblk + (j * fs->super->s_blocks_per_group);
416 if (ind_buf[ind_off] != expect)
417 goto resize_inode_invalid;
418 ind_off++;
419 }
420 }
421
422cleanup:
423 if (dind_buf)
424 ext2fs_free_mem(&dind_buf);
425
426 }
80bfaa3e 427
f77704e4
TT
428/*
429 * This function checks the dirhash signed/unsigned hint if necessary.
430 */
642935c0 431static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
f77704e4
TT
432{
433 struct ext2_super_block *sb = ctx->fs->super;
434 struct problem_context pctx;
f77704e4
TT
435 char c;
436
437 if ((ctx->options & E2F_OPT_READONLY) ||
438 !(sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
439 (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
440 return;
441
442 c = (char) 255;
443
444 clear_problem_context(&pctx);
445 if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
446 if (((int) c) == -1) {
447 sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
448 } else {
449 sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
450 }
451 ext2fs_mark_super_dirty(ctx->fs);
452 }
453}
454
455
1b6bf175
TT
456void check_super_block(e2fsck_t ctx)
457{
458 ext2_filsys fs = ctx->fs;
459 blk_t first_block, last_block;
5dd8f963 460 struct ext2_super_block *sb = fs->super;
550a4afa 461 struct ext2_group_desc *gd;
26ea4899 462 problem_t problem;
1b6bf175 463 blk_t blocks_per_group = fs->super->s_blocks_per_group;
b21bf267 464 blk_t bpg_max;
78cf0547 465 int inodes_per_block;
b21bf267 466 int ipg_max;
89db86d3 467 int inode_size;
ba5131f6 468 int accept_time_fudge;
7f813ba3 469 dgrp_t i;
1b6bf175
TT
470 blk_t should_be;
471 struct problem_context pctx;
5d38ef1d
VC
472 blk_t free_blocks = 0;
473 ino_t free_inodes = 0;
80875db5 474 int csum_flag, clear_test_fs_flag;
b21bf267
AD
475
476 inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
477 ipg_max = inodes_per_block * (blocks_per_group - 4);
478 if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
479 ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
480 bpg_max = 8 * EXT2_BLOCK_SIZE(sb);
481 if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
482 bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
1b6bf175 483
54dc7ca2 484 ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
f8188fff 485 sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
54dc7ca2 486 ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
f8188fff 487 sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
54dc7ca2 488 ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
f8188fff 489 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
b21bf267 490
1b6bf175
TT
491 clear_problem_context(&pctx);
492
493 /*
494 * Verify the super block constants...
495 */
5dd8f963 496 check_super_value(ctx, "inodes_count", sb->s_inodes_count,
1b6bf175 497 MIN_CHECK, 1, 0);
4efbac6f 498 check_super_value(ctx, "blocks_count", ext2fs_blocks_count(sb),
1b6bf175 499 MIN_CHECK, 1, 0);
5dd8f963 500 check_super_value(ctx, "first_data_block", sb->s_first_data_block,
4efbac6f 501 MAX_CHECK, 0, ext2fs_blocks_count(sb));
5dd8f963 502 check_super_value(ctx, "log_block_size", sb->s_log_block_size,
31e29a12
TT
503 MIN_CHECK | MAX_CHECK, 0,
504 EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE);
932a489c
AD
505 check_super_value(ctx, "log_frag_size", sb->s_log_frag_size,
506 MIN_CHECK | MAX_CHECK, 0, sb->s_log_block_size);
5dd8f963 507 check_super_value(ctx, "frags_per_group", sb->s_frags_per_group,
ef059870 508 MIN_CHECK | MAX_CHECK, sb->s_blocks_per_group,
b21bf267 509 bpg_max);
5dd8f963 510 check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
b21bf267 511 MIN_CHECK | MAX_CHECK, 8, bpg_max);
5dd8f963 512 check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
b21bf267 513 MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max);
4efbac6f
VAH
514 check_super_value(ctx, "r_blocks_count", ext2fs_r_blocks_count(sb),
515 MAX_CHECK, 0, ext2fs_blocks_count(sb) / 2);
efc6f628 516 check_super_value(ctx, "reserved_gdt_blocks",
c3ffaf83
TT
517 sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
518 fs->blocksize/4);
1f790a7c
TT
519 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
520 check_super_value(ctx, "first_ino", sb->s_first_ino,
521 MIN_CHECK | MAX_CHECK,
522 EXT2_GOOD_OLD_FIRST_INO, sb->s_inodes_count);
89db86d3
TT
523 inode_size = EXT2_INODE_SIZE(sb);
524 check_super_value(ctx, "inode_size",
525 inode_size, MIN_CHECK | MAX_CHECK,
526 EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
527 if (inode_size & (inode_size - 1)) {
528 pctx.num = inode_size;
529 pctx.str = "inode_size";
530 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
531 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
532 return;
533 }
efc6f628 534
d2af1bdd 535 if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) &&
4efbac6f
VAH
536 (ctx->num_blocks < ext2fs_blocks_count(sb))) {
537 pctx.blk = ext2fs_blocks_count(sb);
d2af1bdd
TT
538 pctx.blk2 = ctx->num_blocks;
539 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
08b21301
TT
540 ctx->flags |= E2F_FLAG_ABORT;
541 return;
542 }
1b6bf175
TT
543 }
544
54434927 545 if (sb->s_log_block_size != (__u32) sb->s_log_frag_size) {
5dd8f963
TT
546 pctx.blk = EXT2_BLOCK_SIZE(sb);
547 pctx.blk2 = EXT2_FRAG_SIZE(sb);
1b6bf175 548 fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx);
08b21301
TT
549 ctx->flags |= E2F_FLAG_ABORT;
550 return;
1b6bf175
TT
551 }
552
5dd8f963 553 should_be = sb->s_frags_per_group >>
efc6f628 554 (sb->s_log_block_size - sb->s_log_frag_size);
5dd8f963
TT
555 if (sb->s_blocks_per_group != should_be) {
556 pctx.blk = sb->s_blocks_per_group;
1b6bf175
TT
557 pctx.blk2 = should_be;
558 fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx);
08b21301
TT
559 ctx->flags |= E2F_FLAG_ABORT;
560 return;
1b6bf175
TT
561 }
562
5dd8f963
TT
563 should_be = (sb->s_log_block_size == 0) ? 1 : 0;
564 if (sb->s_first_data_block != should_be) {
565 pctx.blk = sb->s_first_data_block;
1b6bf175
TT
566 pctx.blk2 = should_be;
567 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
08b21301
TT
568 ctx->flags |= E2F_FLAG_ABORT;
569 return;
1b6bf175
TT
570 }
571
5dd8f963
TT
572 should_be = sb->s_inodes_per_group * fs->group_desc_count;
573 if (sb->s_inodes_count != should_be) {
574 pctx.ino = sb->s_inodes_count;
d4b0ce03
TT
575 pctx.ino2 = should_be;
576 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
5dd8f963 577 sb->s_inodes_count = should_be;
d4b0ce03
TT
578 ext2fs_mark_super_dirty(fs);
579 }
580 }
581
1b6bf175
TT
582 /*
583 * Verify the group descriptors....
584 */
88b34b1f 585 first_block = sb->s_first_data_block;
4efbac6f 586 last_block = ext2fs_blocks_count(sb)-1;
1b6bf175 587
49a7360b
JS
588 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
589 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
550a4afa 590 for (i = 0, gd=fs->group_desc; i < fs->group_desc_count; i++, gd++) {
1b6bf175 591 pctx.group = i;
abf23439 592
88b34b1f
JS
593 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
594 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
595 first_block = ext2fs_group_first_block(fs, i);
596 last_block = ext2fs_group_last_block(fs, i);
597 }
bb1a46a4 598
550a4afa 599 if ((gd->bg_block_bitmap < first_block) ||
bb1a46a4 600 (gd->bg_block_bitmap > last_block)) {
550a4afa 601 pctx.blk = gd->bg_block_bitmap;
24bfb443 602 if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
550a4afa 603 gd->bg_block_bitmap = 0;
24bfb443 604 }
550a4afa 605 if (gd->bg_block_bitmap == 0) {
24bfb443
TT
606 ctx->invalid_block_bitmap_flag[i]++;
607 ctx->invalid_bitmaps++;
1b6bf175 608 }
550a4afa 609 if ((gd->bg_inode_bitmap < first_block) ||
bb1a46a4 610 (gd->bg_inode_bitmap > last_block)) {
550a4afa 611 pctx.blk = gd->bg_inode_bitmap;
24bfb443 612 if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
550a4afa 613 gd->bg_inode_bitmap = 0;
24bfb443 614 }
550a4afa 615 if (gd->bg_inode_bitmap == 0) {
24bfb443
TT
616 ctx->invalid_inode_bitmap_flag[i]++;
617 ctx->invalid_bitmaps++;
1b6bf175 618 }
550a4afa
TT
619 if ((gd->bg_inode_table < first_block) ||
620 ((gd->bg_inode_table +
bb1a46a4 621 fs->inode_blocks_per_group - 1) > last_block)) {
550a4afa 622 pctx.blk = gd->bg_inode_table;
24bfb443 623 if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
550a4afa 624 gd->bg_inode_table = 0;
24bfb443 625 }
550a4afa 626 if (gd->bg_inode_table == 0) {
24bfb443
TT
627 ctx->invalid_inode_table_flag[i]++;
628 ctx->invalid_bitmaps++;
1b6bf175 629 }
550a4afa
TT
630 free_blocks += gd->bg_free_blocks_count;
631 free_inodes += gd->bg_free_inodes_count;
550a4afa
TT
632
633 if ((gd->bg_free_blocks_count > sb->s_blocks_per_group) ||
634 (gd->bg_free_inodes_count > sb->s_inodes_per_group) ||
635 (gd->bg_used_dirs_count > sb->s_inodes_per_group))
636 ext2fs_unmark_valid(fs);
637
0d5439c8 638 should_be = 0;
49a7360b
JS
639 if (!ext2fs_group_desc_csum_verify(fs, i)) {
640 if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
e633b58a
ES
641 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
642 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
49a7360b 643 gd->bg_itable_unused = 0;
0d5439c8 644 should_be = 1;
49a7360b
JS
645 }
646 ext2fs_unmark_valid(fs);
647 }
648
16b851cd 649 if (!csum_flag &&
732c8cd5
TT
650 (ext2fs_bg_flag_test(fs, i, EXT2_BG_BLOCK_UNINIT) ||
651 ext2fs_bg_flag_test(fs, i, EXT2_BG_INODE_UNINIT) ||
49a7360b
JS
652 gd->bg_itable_unused != 0)){
653 if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) {
e633b58a
ES
654 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
655 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
49a7360b 656 gd->bg_itable_unused = 0;
0d5439c8 657 should_be = 1;
49a7360b
JS
658 }
659 ext2fs_unmark_valid(fs);
660 }
0d5439c8
AD
661
662 if (i == fs->group_desc_count - 1 &&
732c8cd5 663 ext2fs_bg_flag_test(fs, i, EXT2_BG_BLOCK_UNINIT)) {
0d5439c8 664 if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) {
e633b58a 665 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
0d5439c8
AD
666 should_be = 1;
667 }
668 ext2fs_unmark_valid(fs);
669 }
670
732c8cd5
TT
671 if (ext2fs_bg_flag_test(fs, i, EXT2_BG_BLOCK_UNINIT) &&
672 !ext2fs_bg_flag_test(fs, i, EXT2_BG_INODE_UNINIT)) {
0d5439c8 673 if (fix_problem(ctx, PR_0_BB_UNINIT_IB_INIT, &pctx)) {
e633b58a 674 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
0d5439c8
AD
675 should_be = 1;
676 }
49a7360b
JS
677 ext2fs_unmark_valid(fs);
678 }
0d5439c8 679
49a7360b
JS
680 if (csum_flag &&
681 (gd->bg_itable_unused > gd->bg_free_inodes_count ||
682 gd->bg_itable_unused > sb->s_inodes_per_group)) {
683 pctx.blk = gd->bg_itable_unused;
0d5439c8 684 if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) {
49a7360b 685 gd->bg_itable_unused = 0;
0d5439c8
AD
686 should_be = 1;
687 }
49a7360b
JS
688 ext2fs_unmark_valid(fs);
689 }
0d5439c8
AD
690
691 if (should_be)
692 ext2fs_group_desc_csum_set(fs, i);
bee24f36
VAH
693 /* If the user aborts e2fsck by typing ^C, stop right away */
694 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
695 return;
1b6bf175 696 }
550a4afa
TT
697
698 /*
699 * Update the global counts from the block group counts. This
700 * is needed for an experimental patch which eliminates
701 * locking the entire filesystem when allocating blocks or
702 * inodes; if the filesystem is not unmounted cleanly, the
703 * global counts may not be accurate.
704 */
4efbac6f 705 if ((free_blocks != ext2fs_free_blocks_count(sb)) ||
550a4afa
TT
706 (free_inodes != sb->s_free_inodes_count)) {
707 if (ctx->options & E2F_OPT_READONLY)
708 ext2fs_unmark_valid(fs);
709 else {
4efbac6f 710 ext2fs_free_blocks_count_set(sb, free_blocks);
550a4afa
TT
711 sb->s_free_inodes_count = free_inodes;
712 ext2fs_mark_super_dirty(fs);
713 }
714 }
efc6f628 715
4efbac6f 716 if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) ||
550a4afa
TT
717 (sb->s_free_inodes_count > sb->s_inodes_count))
718 ext2fs_unmark_valid(fs);
719
720
1b6bf175
TT
721 /*
722 * If we have invalid bitmaps, set the error state of the
723 * filesystem.
724 */
725 if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
550a4afa 726 sb->s_state &= ~EXT2_VALID_FS;
1b6bf175
TT
727 ext2fs_mark_super_dirty(fs);
728 }
729
4ea0a110 730 clear_problem_context(&pctx);
efc6f628 731
54be2ccc 732#ifndef EXT2_SKIP_UUID
1b6bf175
TT
733 /*
734 * If the UUID field isn't assigned, assign it.
735 */
5dd8f963 736 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) {
1b6bf175 737 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
5dd8f963 738 uuid_generate(sb->s_uuid);
1b6bf175 739 ext2fs_mark_super_dirty(fs);
299d7424 740 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1b6bf175
TT
741 }
742 }
54be2ccc 743#endif
2a77a784 744
80875db5
TT
745 /*
746 * Check to see if we should disable the test_fs flag
747 */
748 profile_get_boolean(ctx->profile, "options",
749 "clear_test_fs_flag", 0, 1,
750 &clear_test_fs_flag);
751 if (!(ctx->options & E2F_OPT_READONLY) &&
752 clear_test_fs_flag &&
753 (fs->super->s_flags & EXT2_FLAGS_TEST_FILESYS) &&
754 (fs_proc_check("ext4") || check_for_modules("ext4"))) {
755 if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) {
756 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
757 ext2fs_mark_super_dirty(fs);
758 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
759 }
760 }
761
4ea0a110
TT
762 /*
763 * For the Hurd, check to see if the filetype option is set,
764 * since it doesn't support it.
765 */
80bfaa3e
TT
766 if (!(ctx->options & E2F_OPT_READONLY) &&
767 fs->super->s_creator_os == EXT2_OS_HURD &&
4ea0a110
TT
768 (fs->super->s_feature_incompat &
769 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
770 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
771 fs->super->s_feature_incompat &=
772 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
773 ext2fs_mark_super_dirty(fs);
0cfce7f7 774 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
4ea0a110
TT
775 }
776 }
80bfaa3e 777
7b59f1ef
TT
778 /*
779 * If we have any of the compatibility flags set, we need to have a
780 * revision 1 filesystem. Most kernels will not check the flags on
781 * a rev 0 filesystem and we may have corruption issues because of
782 * the incompatible changes to the filesystem.
783 */
784 if (!(ctx->options & E2F_OPT_READONLY) &&
785 fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
786 (fs->super->s_feature_compat ||
787 fs->super->s_feature_ro_compat ||
788 fs->super->s_feature_incompat) &&
789 fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) {
a917d1cc 790 ext2fs_update_dynamic_rev(fs);
7b59f1ef 791 ext2fs_mark_super_dirty(fs);
0cfce7f7 792 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
7b59f1ef
TT
793 }
794
80bfaa3e
TT
795 /*
796 * Clean up any orphan inodes, if present.
797 */
798 if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
799 fs->super->s_state &= ~EXT2_VALID_FS;
7b59f1ef 800 ext2fs_mark_super_dirty(fs);
80bfaa3e
TT
801 }
802
60702c26 803 /*
ba5131f6
TT
804 * Unfortunately, due to Windows' unfortunate design decision
805 * to configure the hardware clock to tick localtime, instead
806 * of the more proper and less error-prone UTC time, many
807 * users end up in the situation where the system clock is
808 * incorrectly set at the time when e2fsck is run.
809 *
810 * Historically this was usually due to some distributions
811 * having buggy init scripts and/or installers that didn't
812 * correctly detect this case and take appropriate
813 * countermeasures. However, it's still possible, despite the
814 * best efforts of init script and installer authors to not be
815 * able to detect this misconfiguration, usually due to a
816 * buggy or misconfigured virtualization manager or the
817 * installer not having access to a network time server during
818 * the installation process. So by default, we allow the
819 * superblock times to be fudged by up to 24 hours. This can
820 * be disabled by setting options.accept_time_fudge to the
821 * boolean value of false in e2fsck.conf. We also support
822 * options.buggy_init_scripts for backwards compatibility.
60702c26 823 */
ba5131f6
TT
824 profile_get_boolean(ctx->profile, "options", "accept_time_fudge",
825 0, 1, &accept_time_fudge);
60702c26 826 profile_get_boolean(ctx->profile, "options", "buggy_init_scripts",
ba5131f6
TT
827 0, accept_time_fudge, &accept_time_fudge);
828 ctx->time_fudge = accept_time_fudge ? 86400 : 0;
60702c26 829
efc6f628 830 /*
8dceb924
TT
831 * Check to see if the superblock last mount time or last
832 * write time is in the future.
833 */
26ea4899 834 if (fs->super->s_mtime > (__u32) ctx->now) {
8dceb924 835 pctx.num = fs->super->s_mtime;
26ea4899
TT
836 problem = PR_0_FUTURE_SB_LAST_MOUNT;
837 if (fs->super->s_mtime <= (__u32) ctx->now + ctx->time_fudge)
838 problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED;
839 if (fix_problem(ctx, problem, &pctx)) {
8dceb924
TT
840 fs->super->s_mtime = ctx->now;
841 ext2fs_mark_super_dirty(fs);
842 }
843 }
26ea4899 844 if (fs->super->s_wtime > (__u32) ctx->now) {
8dceb924 845 pctx.num = fs->super->s_wtime;
fe26a55a 846 problem = PR_0_FUTURE_SB_LAST_WRITE;
26ea4899 847 if (fs->super->s_wtime <= (__u32) ctx->now + ctx->time_fudge)
61a9d2b3 848 problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED;
26ea4899 849 if (fix_problem(ctx, problem, &pctx)) {
8dceb924
TT
850 fs->super->s_wtime = ctx->now;
851 ext2fs_mark_super_dirty(fs);
852 }
853 }
854
773fd8a1
TT
855 /*
856 * Move the ext3 journal file, if necessary.
857 */
858 e2fsck_move_ext3_journal(ctx);
b1c52b26
TT
859
860 /*
861 * Fix journal hint, if necessary
862 */
863 e2fsck_fix_ext3_journal_hint(ctx);
864
f77704e4
TT
865 /*
866 * Add dirhash hint if necessary
867 */
868 e2fsck_fix_dirhash_hint(ctx);
869
1b6bf175
TT
870 return;
871}
0c37f456
TT
872
873/*
874 * Check to see if we should backup the master sb to the backup super
a8cde73a 875 * blocks. Returns non-zero if the sb should be backed up.
0c37f456 876 */
a8cde73a
TT
877
878/*
879 * A few flags are set on the fly by the kernel, but only in the
880 * primary superblock. This is actually a bad thing, and we should
881 * try to discourage it in the future. In particular, for the newer
882 * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
883 * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the
d9ff474b
ES
884 * future. EXT3_FEATURE_INCOMPAT_RECOVER may also get set when
885 * copying the primary superblock during online resize.
a8cde73a
TT
886 *
887 * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
888 * unfortunately, we shouldn't ignore it since if it's not set in the
889 * backup, the extended attributes in the filesystem will be stripped
890 * away.
891 */
892#define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
893 EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
d9ff474b
ES
894#define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS| \
895 EXT3_FEATURE_INCOMPAT_RECOVER)
a8cde73a 896
0c37f456
TT
897int check_backup_super_block(e2fsck_t ctx)
898{
899 ext2_filsys fs = ctx->fs;
0c37f456
TT
900 errcode_t retval;
901 dgrp_t g;
902 blk_t sb;
903 int ret = 0;
982dd30c
TT
904 char buf[SUPERBLOCK_SIZE];
905 struct ext2_super_block *backup_sb;
0c37f456
TT
906
907 /*
908 * If we are already writing out the backup blocks, then we
909 * don't need to test. Also, if the filesystem is invalid, or
910 * the check was aborted or cancelled, we also don't want to
911 * do the backup. If the filesystem was opened read-only then
912 * we can't do the backup.
913 */
914 if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) ||
915 !ext2fs_test_valid(fs) ||
916 (fs->super->s_state & EXT2_ERROR_FS) ||
917 (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) ||
918 (ctx->options & E2F_OPT_READONLY))
919 return 0;
920
921 for (g = 1; g < fs->group_desc_count; g++) {
922 if (!ext2fs_bg_has_super(fs, g))
923 continue;
924
925 sb = fs->super->s_first_data_block +
926 (g * fs->super->s_blocks_per_group);
927
982dd30c
TT
928 retval = io_channel_read_blk(fs->io, sb, -SUPERBLOCK_SIZE,
929 buf);
930 if (retval)
931 continue;
932 backup_sb = (struct ext2_super_block *) buf;
933#ifdef WORDS_BIGENDIAN
934 ext2fs_swap_super(backup_sb);
935#endif
936 if ((backup_sb->s_magic != EXT2_SUPER_MAGIC) ||
937 (backup_sb->s_rev_level > EXT2_LIB_CURRENT_REV) ||
938 ((backup_sb->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
939 EXT2_MAX_BLOCK_LOG_SIZE) ||
940 (EXT2_INODE_SIZE(backup_sb) < EXT2_GOOD_OLD_INODE_SIZE))
0c37f456 941 continue;
0c37f456 942
a8cde73a 943#define SUPER_INCOMPAT_DIFFERENT(x) \
982dd30c
TT
944 ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \
945 (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE))
a8cde73a 946#define SUPER_RO_COMPAT_DIFFERENT(x) \
982dd30c
TT
947 ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \
948 (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE))
a8cde73a 949#define SUPER_DIFFERENT(x) \
982dd30c 950 (fs->super->x != backup_sb->x)
a8cde73a 951
0c37f456 952 if (SUPER_DIFFERENT(s_feature_compat) ||
a8cde73a
TT
953 SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) ||
954 SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) ||
0c37f456
TT
955 SUPER_DIFFERENT(s_blocks_count) ||
956 SUPER_DIFFERENT(s_inodes_count) ||
982dd30c 957 memcmp(fs->super->s_uuid, backup_sb->s_uuid,
0c37f456
TT
958 sizeof(fs->super->s_uuid)))
959 ret = 1;
0c37f456
TT
960 break;
961 }
962 return ret;
963}