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