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