]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/super.c
Add comments documenting ext2fs_[reserve_]super_and_bgd_loc()
[thirdparty/e2fsprogs.git] / e2fsck / super.c
CommitLineData
1b6bf175
TT
1/*
2 * e2fsck.c - superblock checks
3 *
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
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) ||
80 (blk >= fs->super->s_blocks_count)) {
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
87 if (!ext2fs_test_block_bitmap(fs->block_map, blk)) {
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;
8394902e
TT
105
106 pb->errcode = io_channel_read_blk(fs->io, blk, 1,
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) {
128 pb->errcode = io_channel_read_blk(fs->io, blk, 1,
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);
134 pb->errcode = io_channel_write_blk(fs->io, blk, 1,
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
TT
142 }
143
0684a4f3 144 ext2fs_block_alloc_stats(fs, blk, -1);
8394902e 145 return retval;
80bfaa3e
TT
146}
147
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;
54434927 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
0684a4f3
TT
200 if (inode->i_file_acl) {
201 retval = ext2fs_adjust_ea_refcount(fs, inode->i_file_acl,
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)
214 ext2fs_block_alloc_stats(fs, inode->i_file_acl, -1);
215 inode->i_file_acl = 0;
216 }
80bfaa3e
TT
217 return 0;
218}
219
220/*
221 * This function releases all of the orphan inodes. It returns 1 if
222 * it hit some error, and 0 on success.
223 */
224static int release_orphan_inodes(e2fsck_t ctx)
225{
226 ext2_filsys fs = ctx->fs;
86c627ec 227 ext2_ino_t ino, next_ino;
80bfaa3e
TT
228 struct ext2_inode inode;
229 struct problem_context pctx;
230 char *block_buf;
231
232 if ((ino = fs->super->s_last_orphan) == 0)
233 return 0;
234
25c63ba0
TT
235 /*
236 * Win or lose, we won't be using the head of the orphan inode
237 * list again.
238 */
239 fs->super->s_last_orphan = 0;
240 ext2fs_mark_super_dirty(fs);
eb4ab510
TT
241
242 /*
243 * If the filesystem contains errors, don't run the orphan
244 * list, since the orphan list can't be trusted; and we're
245 * going to be running a full e2fsck run anyway...
246 */
247 if (fs->super->s_state & EXT2_ERROR_FS)
248 return 0;
25c63ba0 249
80bfaa3e
TT
250 if ((ino < EXT2_FIRST_INODE(fs->super)) ||
251 (ino > fs->super->s_inodes_count)) {
252 clear_problem_context(&pctx);
53ef44c4 253 pctx.ino = ino;
80bfaa3e
TT
254 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
255 return 1;
256 }
257
8394902e 258 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
9b565759 259 "block iterate buffer");
80bfaa3e
TT
260 e2fsck_read_bitmaps(ctx);
261
262 while (ino) {
8394902e 263 e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes");
80bfaa3e 264 clear_problem_context(&pctx);
ecf1b776 265 pctx.ino = ino;
80bfaa3e 266 pctx.inode = &inode;
f0b8c87d
TT
267 pctx.str = inode.i_links_count ? _("Truncating") :
268 _("Clearing");
80bfaa3e 269
8394902e 270 fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
ecf1b776 271
80bfaa3e
TT
272 next_ino = inode.i_dtime;
273 if (next_ino &&
99a2cc96
TT
274 ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
275 (next_ino > fs->super->s_inodes_count))) {
276 pctx.ino = next_ino;
80bfaa3e 277 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
53ef44c4 278 goto return_abort;
80bfaa3e
TT
279 }
280
8394902e 281 if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
53ef44c4 282 goto return_abort;
ecf1b776 283
8394902e 284 if (!inode.i_links_count) {
0684a4f3
TT
285 ext2fs_inode_alloc_stats2(fs, ino, -1,
286 LINUX_S_ISDIR(inode.i_mode));
1f3ad14a 287 inode.i_dtime = ctx->now;
eb16f861
ST
288 } else {
289 inode.i_dtime = 0;
8394902e
TT
290 }
291 e2fsck_write_inode(ctx, ino, &inode, "delete_file");
80bfaa3e
TT
292 ino = next_ino;
293 }
c4e3d3f3 294 ext2fs_free_mem(&block_buf);
80bfaa3e 295 return 0;
53ef44c4 296return_abort:
c4e3d3f3 297 ext2fs_free_mem(&block_buf);
80bfaa3e
TT
298 return 1;
299}
300
c3ffaf83
TT
301/*
302 * Check the resize inode to make sure it is sane. We check both for
303 * the case where on-line resizing is not enabled (in which case the
304 * resize inode should be cleared) as well as the case where on-line
305 * resizing is enabled.
306 */
642935c0 307static void check_resize_inode(e2fsck_t ctx)
c3ffaf83
TT
308{
309 ext2_filsys fs = ctx->fs;
310 struct ext2_inode inode;
311 struct problem_context pctx;
642935c0
TT
312 int i, gdt_off, ind_off;
313 dgrp_t j;
c3ffaf83
TT
314 blk_t blk, pblk, expect;
315 __u32 *dind_buf = 0, *ind_buf;
316 errcode_t retval;
317
318 clear_problem_context(&pctx);
c3ffaf83
TT
319
320 /*
ea774315
TT
321 * If the resize inode feature isn't set, then
322 * s_reserved_gdt_blocks must be zero.
c3ffaf83
TT
323 */
324 if (!(fs->super->s_feature_compat &
325 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
326 if (fs->super->s_reserved_gdt_blocks) {
327 pctx.num = fs->super->s_reserved_gdt_blocks;
328 if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
329 &pctx)) {
330 fs->super->s_reserved_gdt_blocks = 0;
331 ext2fs_mark_super_dirty(fs);
332 }
333 }
ea774315
TT
334 }
335
1f3ad14a 336 /* Read the resize inode */
ea774315
TT
337 pctx.ino = EXT2_RESIZE_INO;
338 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
339 if (retval) {
340 if (fs->super->s_feature_compat &
341 EXT2_FEATURE_COMPAT_RESIZE_INODE)
342 ctx->flags |= E2F_FLAG_RESIZE_INODE;
343 return;
344 }
345
346 /*
347 * If the resize inode feature isn't set, check to make sure
348 * the resize inode is cleared; then we're done.
349 */
350 if (!(fs->super->s_feature_compat &
351 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
c3ffaf83
TT
352 for (i=0; i < EXT2_N_BLOCKS; i++) {
353 if (inode.i_block[i])
354 break;
355 }
356 if ((i < EXT2_N_BLOCKS) &&
357 fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
358 memset(&inode, 0, sizeof(inode));
359 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
360 "clear_resize");
361 }
362 return;
363 }
ea774315 364
c3ffaf83
TT
365 /*
366 * The resize inode feature is enabled; check to make sure the
367 * only block in use is the double indirect block
368 */
369 blk = inode.i_block[EXT2_DIND_BLOCK];
370 for (i=0; i < EXT2_N_BLOCKS; i++) {
371 if (i != EXT2_DIND_BLOCK && inode.i_block[i])
372 break;
373 }
d4dc0a9e
TT
374 if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
375 !(inode.i_mode & LINUX_S_IFREG) ||
c3ffaf83
TT
376 (blk < fs->super->s_first_data_block ||
377 blk >= fs->super->s_blocks_count)) {
378 resize_inode_invalid:
379 if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
380 memset(&inode, 0, sizeof(inode));
381 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
382 "clear_resize");
383 ctx->flags |= E2F_FLAG_RESIZE_INODE;
384 }
385 if (!(ctx->options & E2F_OPT_READONLY)) {
386 fs->super->s_state &= ~EXT2_VALID_FS;
387 ext2fs_mark_super_dirty(fs);
388 }
389 goto cleanup;
390 }
391 dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
392 "resize dind buffer");
393 ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
394
dc8ce346 395 retval = ext2fs_read_ind_block(fs, blk, dind_buf);
c3ffaf83
TT
396 if (retval)
397 goto resize_inode_invalid;
398
399 gdt_off = fs->desc_blocks;
400 pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
401 for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
402 i++, gdt_off++, pblk++) {
403 gdt_off %= fs->blocksize/4;
404 if (dind_buf[gdt_off] != pblk)
405 goto resize_inode_invalid;
dc8ce346 406 retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
c3ffaf83
TT
407 if (retval)
408 goto resize_inode_invalid;
409 ind_off = 0;
410 for (j = 1; j < fs->group_desc_count; j++) {
411 if (!ext2fs_bg_has_super(fs, j))
412 continue;
413 expect = pblk + (j * fs->super->s_blocks_per_group);
414 if (ind_buf[ind_off] != expect)
415 goto resize_inode_invalid;
416 ind_off++;
417 }
418 }
419
420cleanup:
421 if (dind_buf)
422 ext2fs_free_mem(&dind_buf);
423
424 }
80bfaa3e 425
f77704e4
TT
426/*
427 * This function checks the dirhash signed/unsigned hint if necessary.
428 */
642935c0 429static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
f77704e4
TT
430{
431 struct ext2_super_block *sb = ctx->fs->super;
432 struct problem_context pctx;
f77704e4
TT
433 char c;
434
435 if ((ctx->options & E2F_OPT_READONLY) ||
436 !(sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
437 (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
438 return;
439
440 c = (char) 255;
441
442 clear_problem_context(&pctx);
443 if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
444 if (((int) c) == -1) {
445 sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
446 } else {
447 sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
448 }
449 ext2fs_mark_super_dirty(ctx->fs);
450 }
451}
452
453
1b6bf175
TT
454void check_super_block(e2fsck_t ctx)
455{
456 ext2_filsys fs = ctx->fs;
457 blk_t first_block, last_block;
5dd8f963 458 struct ext2_super_block *sb = fs->super;
550a4afa 459 struct ext2_group_desc *gd;
1b6bf175 460 blk_t blocks_per_group = fs->super->s_blocks_per_group;
b21bf267 461 blk_t bpg_max;
78cf0547 462 int inodes_per_block;
b21bf267 463 int ipg_max;
89db86d3 464 int inode_size;
60702c26 465 int buggy_init_scripts;
7f813ba3 466 dgrp_t i;
1b6bf175
TT
467 blk_t should_be;
468 struct problem_context pctx;
5d38ef1d
VC
469 blk_t free_blocks = 0;
470 ino_t free_inodes = 0;
49a7360b 471 int lazy_flag, csum_flag;
b21bf267
AD
472
473 inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
474 ipg_max = inodes_per_block * (blocks_per_group - 4);
475 if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
476 ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
477 bpg_max = 8 * EXT2_BLOCK_SIZE(sb);
478 if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
479 bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
1b6bf175 480
54dc7ca2 481 ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
f8188fff 482 sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
54dc7ca2 483 ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
f8188fff 484 sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
54dc7ca2 485 ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
f8188fff 486 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
b21bf267 487
1b6bf175
TT
488 clear_problem_context(&pctx);
489
490 /*
491 * Verify the super block constants...
492 */
5dd8f963 493 check_super_value(ctx, "inodes_count", sb->s_inodes_count,
1b6bf175 494 MIN_CHECK, 1, 0);
5dd8f963 495 check_super_value(ctx, "blocks_count", sb->s_blocks_count,
1b6bf175 496 MIN_CHECK, 1, 0);
5dd8f963
TT
497 check_super_value(ctx, "first_data_block", sb->s_first_data_block,
498 MAX_CHECK, 0, sb->s_blocks_count);
5dd8f963 499 check_super_value(ctx, "log_block_size", sb->s_log_block_size,
31e29a12
TT
500 MIN_CHECK | MAX_CHECK, 0,
501 EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE);
932a489c
AD
502 check_super_value(ctx, "log_frag_size", sb->s_log_frag_size,
503 MIN_CHECK | MAX_CHECK, 0, sb->s_log_block_size);
5dd8f963 504 check_super_value(ctx, "frags_per_group", sb->s_frags_per_group,
ef059870 505 MIN_CHECK | MAX_CHECK, sb->s_blocks_per_group,
b21bf267 506 bpg_max);
5dd8f963 507 check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
b21bf267 508 MIN_CHECK | MAX_CHECK, 8, bpg_max);
5dd8f963 509 check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
b21bf267 510 MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max);
5dd8f963 511 check_super_value(ctx, "r_blocks_count", sb->s_r_blocks_count,
22ba4c1d 512 MAX_CHECK, 0, sb->s_blocks_count / 2);
c3ffaf83
TT
513 check_super_value(ctx, "reserved_gdt_blocks",
514 sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
515 fs->blocksize/4);
89db86d3
TT
516 inode_size = EXT2_INODE_SIZE(sb);
517 check_super_value(ctx, "inode_size",
518 inode_size, MIN_CHECK | MAX_CHECK,
519 EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
520 if (inode_size & (inode_size - 1)) {
521 pctx.num = inode_size;
522 pctx.str = "inode_size";
523 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
524 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
525 return;
526 }
527
d2af1bdd
TT
528 if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) &&
529 (ctx->num_blocks < sb->s_blocks_count)) {
530 pctx.blk = sb->s_blocks_count;
531 pctx.blk2 = ctx->num_blocks;
532 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
08b21301
TT
533 ctx->flags |= E2F_FLAG_ABORT;
534 return;
535 }
1b6bf175
TT
536 }
537
54434927 538 if (sb->s_log_block_size != (__u32) sb->s_log_frag_size) {
5dd8f963
TT
539 pctx.blk = EXT2_BLOCK_SIZE(sb);
540 pctx.blk2 = EXT2_FRAG_SIZE(sb);
1b6bf175 541 fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx);
08b21301
TT
542 ctx->flags |= E2F_FLAG_ABORT;
543 return;
1b6bf175
TT
544 }
545
5dd8f963
TT
546 should_be = sb->s_frags_per_group >>
547 (sb->s_log_block_size - sb->s_log_frag_size);
548 if (sb->s_blocks_per_group != should_be) {
549 pctx.blk = sb->s_blocks_per_group;
1b6bf175
TT
550 pctx.blk2 = should_be;
551 fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx);
08b21301
TT
552 ctx->flags |= E2F_FLAG_ABORT;
553 return;
1b6bf175
TT
554 }
555
5dd8f963
TT
556 should_be = (sb->s_log_block_size == 0) ? 1 : 0;
557 if (sb->s_first_data_block != should_be) {
558 pctx.blk = sb->s_first_data_block;
1b6bf175
TT
559 pctx.blk2 = should_be;
560 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
08b21301
TT
561 ctx->flags |= E2F_FLAG_ABORT;
562 return;
1b6bf175
TT
563 }
564
5dd8f963
TT
565 should_be = sb->s_inodes_per_group * fs->group_desc_count;
566 if (sb->s_inodes_count != should_be) {
567 pctx.ino = sb->s_inodes_count;
d4b0ce03
TT
568 pctx.ino2 = should_be;
569 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
5dd8f963 570 sb->s_inodes_count = should_be;
d4b0ce03
TT
571 ext2fs_mark_super_dirty(fs);
572 }
573 }
574
1b6bf175
TT
575 /*
576 * Verify the group descriptors....
577 */
88b34b1f
JS
578 first_block = sb->s_first_data_block;
579 last_block = sb->s_blocks_count-1;
1b6bf175 580
49a7360b
JS
581 lazy_flag = EXT2_HAS_COMPAT_FEATURE(fs->super,
582 EXT2_FEATURE_COMPAT_LAZY_BG);
583 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
584 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
550a4afa 585 for (i = 0, gd=fs->group_desc; i < fs->group_desc_count; i++, gd++) {
1b6bf175 586 pctx.group = i;
abf23439 587
88b34b1f
JS
588 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
589 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
590 first_block = ext2fs_group_first_block(fs, i);
591 last_block = ext2fs_group_last_block(fs, i);
592 }
bb1a46a4 593
550a4afa 594 if ((gd->bg_block_bitmap < first_block) ||
bb1a46a4 595 (gd->bg_block_bitmap > last_block)) {
550a4afa 596 pctx.blk = gd->bg_block_bitmap;
24bfb443 597 if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
550a4afa 598 gd->bg_block_bitmap = 0;
24bfb443 599 }
550a4afa 600 if (gd->bg_block_bitmap == 0) {
24bfb443
TT
601 ctx->invalid_block_bitmap_flag[i]++;
602 ctx->invalid_bitmaps++;
1b6bf175 603 }
550a4afa 604 if ((gd->bg_inode_bitmap < first_block) ||
bb1a46a4 605 (gd->bg_inode_bitmap > last_block)) {
550a4afa 606 pctx.blk = gd->bg_inode_bitmap;
24bfb443 607 if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
550a4afa 608 gd->bg_inode_bitmap = 0;
24bfb443 609 }
550a4afa 610 if (gd->bg_inode_bitmap == 0) {
24bfb443
TT
611 ctx->invalid_inode_bitmap_flag[i]++;
612 ctx->invalid_bitmaps++;
1b6bf175 613 }
550a4afa
TT
614 if ((gd->bg_inode_table < first_block) ||
615 ((gd->bg_inode_table +
bb1a46a4 616 fs->inode_blocks_per_group - 1) > last_block)) {
550a4afa 617 pctx.blk = gd->bg_inode_table;
24bfb443 618 if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
550a4afa 619 gd->bg_inode_table = 0;
24bfb443 620 }
550a4afa 621 if (gd->bg_inode_table == 0) {
24bfb443
TT
622 ctx->invalid_inode_table_flag[i]++;
623 ctx->invalid_bitmaps++;
1b6bf175 624 }
550a4afa
TT
625 free_blocks += gd->bg_free_blocks_count;
626 free_inodes += gd->bg_free_inodes_count;
550a4afa
TT
627
628 if ((gd->bg_free_blocks_count > sb->s_blocks_per_group) ||
629 (gd->bg_free_inodes_count > sb->s_inodes_per_group) ||
630 (gd->bg_used_dirs_count > sb->s_inodes_per_group))
631 ext2fs_unmark_valid(fs);
632
0d5439c8 633 should_be = 0;
49a7360b
JS
634 if (!ext2fs_group_desc_csum_verify(fs, i)) {
635 if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
636 gd->bg_flags &= ~(EXT2_BG_BLOCK_UNINIT |
637 EXT2_BG_INODE_UNINIT);
638 gd->bg_itable_unused = 0;
0d5439c8 639 should_be = 1;
49a7360b
JS
640 }
641 ext2fs_unmark_valid(fs);
642 }
643
644 if (!lazy_flag && !csum_flag &&
645 (gd->bg_flags &(EXT2_BG_BLOCK_UNINIT|EXT2_BG_INODE_UNINIT)||
646 gd->bg_itable_unused != 0)){
647 if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) {
648 gd->bg_flags &= ~(EXT2_BG_BLOCK_UNINIT |
649 EXT2_BG_INODE_UNINIT);
650 gd->bg_itable_unused = 0;
0d5439c8 651 should_be = 1;
49a7360b
JS
652 }
653 ext2fs_unmark_valid(fs);
654 }
0d5439c8
AD
655
656 if (i == fs->group_desc_count - 1 &&
657 gd->bg_flags & EXT2_BG_BLOCK_UNINIT) {
658 if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) {
659 gd->bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
660 should_be = 1;
661 }
662 ext2fs_unmark_valid(fs);
663 }
664
49a7360b
JS
665 if (gd->bg_flags & EXT2_BG_BLOCK_UNINIT &&
666 !(gd->bg_flags & EXT2_BG_INODE_UNINIT)) {
0d5439c8 667 if (fix_problem(ctx, PR_0_BB_UNINIT_IB_INIT, &pctx)) {
49a7360b 668 gd->bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
0d5439c8
AD
669 should_be = 1;
670 }
49a7360b
JS
671 ext2fs_unmark_valid(fs);
672 }
0d5439c8 673
49a7360b
JS
674 if (csum_flag &&
675 (gd->bg_itable_unused > gd->bg_free_inodes_count ||
676 gd->bg_itable_unused > sb->s_inodes_per_group)) {
677 pctx.blk = gd->bg_itable_unused;
0d5439c8 678 if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) {
49a7360b 679 gd->bg_itable_unused = 0;
0d5439c8
AD
680 should_be = 1;
681 }
49a7360b
JS
682 ext2fs_unmark_valid(fs);
683 }
0d5439c8
AD
684
685 if (should_be)
686 ext2fs_group_desc_csum_set(fs, i);
1b6bf175 687 }
550a4afa
TT
688
689 /*
690 * Update the global counts from the block group counts. This
691 * is needed for an experimental patch which eliminates
692 * locking the entire filesystem when allocating blocks or
693 * inodes; if the filesystem is not unmounted cleanly, the
694 * global counts may not be accurate.
695 */
696 if ((free_blocks != sb->s_free_blocks_count) ||
697 (free_inodes != sb->s_free_inodes_count)) {
698 if (ctx->options & E2F_OPT_READONLY)
699 ext2fs_unmark_valid(fs);
700 else {
701 sb->s_free_blocks_count = free_blocks;
702 sb->s_free_inodes_count = free_inodes;
703 ext2fs_mark_super_dirty(fs);
704 }
705 }
706
707 if ((sb->s_free_blocks_count > sb->s_blocks_count) ||
708 (sb->s_free_inodes_count > sb->s_inodes_count))
709 ext2fs_unmark_valid(fs);
710
711
1b6bf175
TT
712 /*
713 * If we have invalid bitmaps, set the error state of the
714 * filesystem.
715 */
716 if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
550a4afa 717 sb->s_state &= ~EXT2_VALID_FS;
1b6bf175
TT
718 ext2fs_mark_super_dirty(fs);
719 }
720
4ea0a110
TT
721 clear_problem_context(&pctx);
722
54be2ccc 723#ifndef EXT2_SKIP_UUID
1b6bf175
TT
724 /*
725 * If the UUID field isn't assigned, assign it.
726 */
5dd8f963 727 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) {
1b6bf175 728 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
5dd8f963 729 uuid_generate(sb->s_uuid);
1b6bf175 730 ext2fs_mark_super_dirty(fs);
299d7424 731 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1b6bf175
TT
732 }
733 }
54be2ccc 734#endif
2a77a784 735
4ea0a110
TT
736 /*
737 * For the Hurd, check to see if the filetype option is set,
738 * since it doesn't support it.
739 */
80bfaa3e
TT
740 if (!(ctx->options & E2F_OPT_READONLY) &&
741 fs->super->s_creator_os == EXT2_OS_HURD &&
4ea0a110
TT
742 (fs->super->s_feature_incompat &
743 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
744 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
745 fs->super->s_feature_incompat &=
746 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
747 ext2fs_mark_super_dirty(fs);
0cfce7f7 748 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
4ea0a110
TT
749 }
750 }
80bfaa3e 751
7b59f1ef
TT
752 /*
753 * If we have any of the compatibility flags set, we need to have a
754 * revision 1 filesystem. Most kernels will not check the flags on
755 * a rev 0 filesystem and we may have corruption issues because of
756 * the incompatible changes to the filesystem.
757 */
758 if (!(ctx->options & E2F_OPT_READONLY) &&
759 fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
760 (fs->super->s_feature_compat ||
761 fs->super->s_feature_ro_compat ||
762 fs->super->s_feature_incompat) &&
763 fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) {
a917d1cc 764 ext2fs_update_dynamic_rev(fs);
7b59f1ef 765 ext2fs_mark_super_dirty(fs);
0cfce7f7 766 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
7b59f1ef
TT
767 }
768
c3ffaf83 769 check_resize_inode(ctx);
e75cfc5d 770
80bfaa3e
TT
771 /*
772 * Clean up any orphan inodes, if present.
773 */
774 if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
775 fs->super->s_state &= ~EXT2_VALID_FS;
7b59f1ef 776 ext2fs_mark_super_dirty(fs);
80bfaa3e
TT
777 }
778
60702c26
TT
779 /*
780 * Some buggy distributions (such as Ubuntu) have init scripts
781 * and/or installers which fail to correctly set the system
782 * clock before running e2fsck and/or formatting the
783 * filesystem initially. Normally this happens because the
784 * hardware clock is ticking localtime, instead of the more
785 * proper and less error-prone UTC time. So while the kernel
786 * is booting, the system time (which in Linux systems always
787 * ticks in UTC time) is set from the hardware clock, but
788 * since the hardware clock is ticking localtime, the system
789 * time is incorrect. Unfortunately, some buggy distributions
790 * do not correct this before running e2fsck. If this option
791 * is set to a boolean value of true, we attempt to work
792 * around this situation by allowing the superblock last write
793 * time, last mount time, and last check time to be in the
794 * future by up to 24 hours.
795 */
796 profile_get_boolean(ctx->profile, "options", "buggy_init_scripts",
797 0, 0, &buggy_init_scripts);
798 ctx->time_fudge = buggy_init_scripts ? 86400 : 0;
799
8dceb924
TT
800 /*
801 * Check to see if the superblock last mount time or last
802 * write time is in the future.
803 */
60702c26 804 if (fs->super->s_mtime > (__u32) ctx->now + ctx->time_fudge) {
8dceb924
TT
805 pctx.num = fs->super->s_mtime;
806 if (fix_problem(ctx, PR_0_FUTURE_SB_LAST_MOUNT, &pctx)) {
807 fs->super->s_mtime = ctx->now;
808 ext2fs_mark_super_dirty(fs);
809 }
810 }
60702c26 811 if (fs->super->s_wtime > (__u32) ctx->now + ctx->time_fudge) {
8dceb924
TT
812 pctx.num = fs->super->s_wtime;
813 if (fix_problem(ctx, PR_0_FUTURE_SB_LAST_WRITE, &pctx)) {
814 fs->super->s_wtime = ctx->now;
815 ext2fs_mark_super_dirty(fs);
816 }
817 }
818
773fd8a1
TT
819 /*
820 * Move the ext3 journal file, if necessary.
821 */
822 e2fsck_move_ext3_journal(ctx);
b1c52b26
TT
823
824 /*
825 * Fix journal hint, if necessary
826 */
827 e2fsck_fix_ext3_journal_hint(ctx);
828
f77704e4
TT
829 /*
830 * Add dirhash hint if necessary
831 */
832 e2fsck_fix_dirhash_hint(ctx);
833
1b6bf175
TT
834 return;
835}
0c37f456
TT
836
837/*
838 * Check to see if we should backup the master sb to the backup super
a8cde73a 839 * blocks. Returns non-zero if the sb should be backed up.
0c37f456 840 */
a8cde73a
TT
841
842/*
843 * A few flags are set on the fly by the kernel, but only in the
844 * primary superblock. This is actually a bad thing, and we should
845 * try to discourage it in the future. In particular, for the newer
846 * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
847 * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the
848 * future.
849 *
850 * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
851 * unfortunately, we shouldn't ignore it since if it's not set in the
852 * backup, the extended attributes in the filesystem will be stripped
853 * away.
854 */
855#define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
856 EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
857#define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS)
858
0c37f456
TT
859int check_backup_super_block(e2fsck_t ctx)
860{
861 ext2_filsys fs = ctx->fs;
862 ext2_filsys tfs = 0;
0c37f456
TT
863 errcode_t retval;
864 dgrp_t g;
865 blk_t sb;
866 int ret = 0;
867
868 /*
869 * If we are already writing out the backup blocks, then we
870 * don't need to test. Also, if the filesystem is invalid, or
871 * the check was aborted or cancelled, we also don't want to
872 * do the backup. If the filesystem was opened read-only then
873 * we can't do the backup.
874 */
875 if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) ||
876 !ext2fs_test_valid(fs) ||
877 (fs->super->s_state & EXT2_ERROR_FS) ||
878 (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) ||
879 (ctx->options & E2F_OPT_READONLY))
880 return 0;
881
882 for (g = 1; g < fs->group_desc_count; g++) {
883 if (!ext2fs_bg_has_super(fs, g))
884 continue;
885
886 sb = fs->super->s_first_data_block +
887 (g * fs->super->s_blocks_per_group);
888
889 retval = ext2fs_open(ctx->filesystem_name, 0,
890 sb, fs->blocksize,
891 fs->io->manager, &tfs);
892 if (retval) {
893 tfs = 0;
894 continue;
895 }
896
a8cde73a
TT
897#define SUPER_INCOMPAT_DIFFERENT(x) \
898 (( fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \
899 (tfs->super->x & ~FEATURE_INCOMPAT_IGNORE))
900#define SUPER_RO_COMPAT_DIFFERENT(x) \
901 (( fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \
902 (tfs->super->x & ~FEATURE_RO_COMPAT_IGNORE))
903#define SUPER_DIFFERENT(x) \
904 (fs->super->x != tfs->super->x)
905
0c37f456 906 if (SUPER_DIFFERENT(s_feature_compat) ||
a8cde73a
TT
907 SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) ||
908 SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) ||
0c37f456
TT
909 SUPER_DIFFERENT(s_blocks_count) ||
910 SUPER_DIFFERENT(s_inodes_count) ||
911 memcmp(fs->super->s_uuid, tfs->super->s_uuid,
912 sizeof(fs->super->s_uuid)))
913 ret = 1;
914 ext2fs_close(tfs);
915 break;
916 }
917 return ret;
918}