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