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