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