]>
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 | ||
5c87a5c5 | 284 | retval = quota_read_all_dquots(ctx->qctx, qf_ino, qtype, |
a0627aed | 285 | QREAD_USAGE | QREAD_LIMITS); |
7d79b40b DW |
286 | if (retval) |
287 | break; | |
288 | } | |
289 | if (retval) | |
290 | quota_release_context(&ctx->qctx); | |
291 | return retval; | |
292 | } | |
293 | ||
294 | /* Write all the quota info to disk. */ | |
295 | static errcode_t e2fsck_write_all_quotas(e2fsck_t ctx) | |
296 | { | |
297 | struct problem_context pctx; | |
298 | enum quota_type qtype; | |
299 | ||
300 | if (!ext2fs_has_feature_quota(ctx->fs->super)) | |
301 | return 0; | |
302 | ||
303 | clear_problem_context(&pctx); | |
304 | for (qtype = 0 ; qtype < MAXQUOTAS; qtype++) { | |
305 | pctx.num = qtype; | |
306 | pctx.errcode = quota_write_inode(ctx->qctx, 1 << qtype); | |
307 | if (pctx.errcode) { | |
308 | fix_problem(ctx, PR_6_WRITE_QUOTAS, &pctx); | |
309 | break; | |
310 | } | |
311 | } | |
312 | ||
313 | quota_release_context(&ctx->qctx); | |
314 | return pctx.errcode; | |
315 | } | |
316 | ||
d0c52ffb JK |
317 | static int release_orphan_inode(e2fsck_t ctx, ext2_ino_t *ino, char *block_buf) |
318 | { | |
319 | ext2_filsys fs = ctx->fs; | |
320 | struct problem_context pctx; | |
321 | struct ext2_inode_large inode; | |
322 | ext2_ino_t next_ino; | |
323 | ||
324 | e2fsck_read_inode_full(ctx, *ino, EXT2_INODE(&inode), | |
325 | sizeof(inode), "release_orphan_inode"); | |
326 | clear_problem_context(&pctx); | |
327 | pctx.ino = *ino; | |
328 | pctx.inode = EXT2_INODE(&inode); | |
329 | pctx.str = inode.i_links_count ? _("Truncating") : _("Clearing"); | |
330 | ||
331 | fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx); | |
332 | ||
333 | next_ino = inode.i_dtime; | |
334 | if (next_ino && | |
335 | ((next_ino < EXT2_FIRST_INODE(fs->super)) || | |
336 | (next_ino > fs->super->s_inodes_count))) { | |
337 | pctx.ino = next_ino; | |
338 | fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx); | |
339 | return 1; | |
340 | } | |
341 | ||
342 | if (release_inode_blocks(ctx, *ino, &inode, block_buf, &pctx)) | |
343 | return 1; | |
344 | ||
345 | if (!inode.i_links_count) { | |
346 | if (ctx->qctx) | |
347 | quota_data_inodes(ctx->qctx, &inode, *ino, -1); | |
348 | ext2fs_inode_alloc_stats2(fs, *ino, -1, | |
349 | LINUX_S_ISDIR(inode.i_mode)); | |
350 | ctx->free_inodes++; | |
351 | inode.i_dtime = ctx->now; | |
352 | } else { | |
353 | inode.i_dtime = 0; | |
354 | } | |
355 | e2fsck_write_inode_full(ctx, *ino, EXT2_INODE(&inode), | |
356 | sizeof(inode), "delete_file"); | |
357 | *ino = next_ino; | |
358 | return 0; | |
359 | } | |
360 | ||
361 | struct process_orphan_block_data { | |
362 | e2fsck_t ctx; | |
363 | char *buf; | |
364 | char *block_buf; | |
365 | e2_blkcnt_t blocks; | |
366 | int abort; | |
367 | int clear; | |
368 | errcode_t errcode; | |
369 | ext2_ino_t ino; | |
370 | __u32 generation; | |
371 | }; | |
372 | ||
373 | static int process_orphan_block(ext2_filsys fs, | |
374 | blk64_t *block_nr, | |
375 | e2_blkcnt_t blockcnt, | |
376 | blk64_t ref_blk EXT2FS_ATTR((unused)), | |
377 | int ref_offset EXT2FS_ATTR((unused)), | |
378 | void *priv_data) | |
379 | { | |
380 | struct process_orphan_block_data *pd; | |
381 | e2fsck_t ctx; | |
382 | struct problem_context pctx; | |
383 | blk64_t blk = *block_nr; | |
384 | struct ext4_orphan_block_tail *tail; | |
385 | int j; | |
386 | int inodes_per_ob; | |
387 | __u32 *bdata; | |
388 | ext2_ino_t ino; | |
389 | ||
390 | pd = priv_data; | |
391 | ctx = pd->ctx; | |
392 | clear_problem_context(&pctx); | |
393 | pctx.ino = fs->super->s_orphan_file_inum; | |
394 | pctx.blk = blockcnt; | |
395 | ||
396 | /* Orphan file must not have holes */ | |
397 | if (!blk) { | |
398 | if (blockcnt == pd->blocks) | |
399 | return BLOCK_ABORT; | |
400 | fix_problem(ctx, PR_0_ORPHAN_FILE_HOLE, &pctx); | |
401 | return_abort: | |
402 | pd->abort = 1; | |
403 | return BLOCK_ABORT; | |
404 | } | |
405 | inodes_per_ob = ext2fs_inodes_per_orphan_block(fs); | |
406 | pd->errcode = io_channel_read_blk64(fs->io, blk, 1, pd->buf); | |
407 | if (pd->errcode) | |
408 | goto return_abort; | |
409 | tail = ext2fs_orphan_block_tail(fs, pd->buf); | |
410 | if (ext2fs_le32_to_cpu(tail->ob_magic) != | |
411 | EXT4_ORPHAN_BLOCK_MAGIC) { | |
412 | fix_problem(ctx, PR_0_ORPHAN_FILE_BAD_MAGIC, &pctx); | |
413 | goto return_abort; | |
414 | } | |
415 | if (!ext2fs_orphan_file_block_csum_verify(fs, | |
416 | fs->super->s_orphan_file_inum, blk, pd->buf)) { | |
417 | fix_problem(ctx, PR_0_ORPHAN_FILE_BAD_CHECKSUM, &pctx); | |
418 | goto return_abort; | |
419 | } | |
420 | bdata = (__u32 *)pd->buf; | |
421 | for (j = 0; j < inodes_per_ob; j++) { | |
422 | if (!bdata[j]) | |
423 | continue; | |
424 | ino = ext2fs_le32_to_cpu(bdata[j]); | |
425 | if (release_orphan_inode(ctx, &ino, pd->block_buf)) | |
426 | goto return_abort; | |
427 | } | |
428 | return 0; | |
429 | } | |
430 | ||
431 | static int process_orphan_file(e2fsck_t ctx, char *block_buf) | |
432 | { | |
433 | ext2_filsys fs = ctx->fs; | |
434 | char *orphan_buf; | |
435 | struct process_orphan_block_data pd; | |
436 | int ret = 0; | |
437 | ext2_ino_t orphan_inum = fs->super->s_orphan_file_inum; | |
438 | struct ext2_inode orphan_inode; | |
439 | struct problem_context pctx; | |
440 | errcode_t retval; | |
441 | ||
442 | if (!ext2fs_has_feature_orphan_file(fs->super)) | |
443 | return 0; | |
444 | ||
445 | clear_problem_context(&pctx); | |
446 | pctx.ino = orphan_inum; | |
447 | ||
448 | orphan_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4, | |
449 | "orphan block buffer"); | |
450 | retval = ext2fs_read_inode(fs, orphan_inum, &orphan_inode); | |
451 | if (retval < 0) { | |
452 | com_err("process_orphan_file", retval, | |
453 | _("while reading inode %d"), orphan_inum); | |
454 | ret = 1; | |
455 | goto out; | |
456 | } | |
457 | if (EXT2_I_SIZE(&orphan_inode) & (fs->blocksize - 1)) { | |
458 | fix_problem(ctx, PR_0_ORPHAN_FILE_WRONG_SIZE, &pctx); | |
459 | ret = 1; | |
460 | goto out; | |
461 | } | |
462 | pd.buf = orphan_buf + 3 * fs->blocksize; | |
463 | pd.block_buf = block_buf; | |
464 | pd.blocks = EXT2_I_SIZE(&orphan_inode) / fs->blocksize; | |
465 | pd.ctx = ctx; | |
466 | pd.abort = 0; | |
467 | pd.errcode = 0; | |
468 | retval = ext2fs_block_iterate3(fs, orphan_inum, | |
469 | BLOCK_FLAG_DATA_ONLY | BLOCK_FLAG_HOLE, | |
470 | orphan_buf, process_orphan_block, &pd); | |
471 | if (retval) { | |
472 | com_err("process_orphan_block", retval, | |
473 | _("while calling ext2fs_block_iterate for inode %d"), | |
474 | orphan_inum); | |
475 | ret = 1; | |
476 | goto out; | |
477 | } | |
478 | if (pd.abort) { | |
479 | if (pd.errcode) { | |
480 | com_err("process_orphan_block", pd.errcode, | |
481 | _("while reading blocks of inode %d"), | |
482 | orphan_inum); | |
483 | } | |
484 | ret = 1; | |
485 | } | |
486 | out: | |
487 | ext2fs_free_mem(&orphan_buf); | |
488 | return ret; | |
489 | } | |
490 | ||
80bfaa3e TT |
491 | /* |
492 | * This function releases all of the orphan inodes. It returns 1 if | |
493 | * it hit some error, and 0 on success. | |
494 | */ | |
495 | static int release_orphan_inodes(e2fsck_t ctx) | |
496 | { | |
497 | ext2_filsys fs = ctx->fs; | |
d0c52ffb | 498 | ext2_ino_t ino; |
80bfaa3e TT |
499 | struct problem_context pctx; |
500 | char *block_buf; | |
501 | ||
d0c52ffb JK |
502 | if (fs->super->s_last_orphan == 0 && |
503 | !ext2fs_has_feature_orphan_present(fs->super)) | |
80bfaa3e TT |
504 | return 0; |
505 | ||
7d79b40b | 506 | clear_problem_context(&pctx); |
d0c52ffb JK |
507 | ino = fs->super->s_last_orphan; |
508 | pctx.ino = ino; | |
7d79b40b DW |
509 | pctx.errcode = e2fsck_read_all_quotas(ctx); |
510 | if (pctx.errcode) { | |
511 | fix_problem(ctx, PR_0_QUOTA_INIT_CTX, &pctx); | |
512 | return 1; | |
513 | } | |
514 | ||
25c63ba0 TT |
515 | /* |
516 | * Win or lose, we won't be using the head of the orphan inode | |
517 | * list again. | |
518 | */ | |
519 | fs->super->s_last_orphan = 0; | |
520 | ext2fs_mark_super_dirty(fs); | |
eb4ab510 TT |
521 | |
522 | /* | |
d0c52ffb JK |
523 | * If the filesystem contains errors, don't process the orphan list |
524 | * or orphan file, since neither can be trusted; and we're going to | |
525 | * be running a full e2fsck run anyway... We clear orphan file contents | |
526 | * after filesystem is checked to avoid clearing someone else's data. | |
eb4ab510 | 527 | */ |
7d79b40b DW |
528 | if (fs->super->s_state & EXT2_ERROR_FS) { |
529 | if (ctx->qctx) | |
530 | quota_release_context(&ctx->qctx); | |
eb4ab510 | 531 | return 0; |
7d79b40b | 532 | } |
efc6f628 | 533 | |
d0c52ffb JK |
534 | if (ino && ((ino < EXT2_FIRST_INODE(fs->super)) || |
535 | (ino > fs->super->s_inodes_count))) { | |
80bfaa3e | 536 | fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx); |
7d79b40b | 537 | goto err_qctx; |
80bfaa3e TT |
538 | } |
539 | ||
8394902e | 540 | block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4, |
9b565759 | 541 | "block iterate buffer"); |
80bfaa3e | 542 | e2fsck_read_bitmaps(ctx); |
efc6f628 | 543 | |
d0c52ffb | 544 | /* First process orphan list */ |
80bfaa3e | 545 | while (ino) { |
d0c52ffb | 546 | if (release_orphan_inode(ctx, &ino, block_buf)) |
7d79b40b | 547 | goto err_buf; |
d0c52ffb | 548 | } |
80bfaa3e | 549 | |
d0c52ffb JK |
550 | /* Next process orphan file */ |
551 | if (ext2fs_has_feature_orphan_present(fs->super) && | |
552 | !ext2fs_has_feature_orphan_file(fs->super)) | |
553 | goto err_buf; | |
554 | if (process_orphan_file(ctx, block_buf)) | |
555 | goto err_buf; | |
ecf1b776 | 556 | |
c4e3d3f3 | 557 | ext2fs_free_mem(&block_buf); |
7d79b40b DW |
558 | pctx.errcode = e2fsck_write_all_quotas(ctx); |
559 | if (pctx.errcode) | |
560 | goto err; | |
80bfaa3e | 561 | return 0; |
7d79b40b | 562 | err_buf: |
c4e3d3f3 | 563 | ext2fs_free_mem(&block_buf); |
7d79b40b DW |
564 | err_qctx: |
565 | if (ctx->qctx) | |
566 | quota_release_context(&ctx->qctx); | |
567 | err: | |
80bfaa3e TT |
568 | return 1; |
569 | } | |
570 | ||
d0c52ffb JK |
571 | static int reinit_orphan_block(ext2_filsys fs, |
572 | blk64_t *block_nr, | |
573 | e2_blkcnt_t blockcnt, | |
574 | blk64_t ref_blk EXT2FS_ATTR((unused)), | |
575 | int ref_offset EXT2FS_ATTR((unused)), | |
576 | void *priv_data) | |
577 | { | |
578 | struct process_orphan_block_data *pd; | |
579 | e2fsck_t ctx; | |
580 | blk64_t blk = *block_nr; | |
581 | struct problem_context pctx; | |
582 | ||
583 | pd = priv_data; | |
584 | ctx = pd->ctx; | |
585 | ||
586 | /* Orphan file must not have holes */ | |
587 | if (!blk) { | |
588 | if (blockcnt == pd->blocks) | |
589 | return BLOCK_ABORT; | |
590 | ||
591 | clear_problem_context(&pctx); | |
592 | pctx.ino = fs->super->s_orphan_file_inum; | |
593 | pctx.blk = blockcnt; | |
594 | fix_problem(ctx, PR_6_ORPHAN_FILE_HOLE, &pctx); | |
595 | return_abort: | |
596 | pd->abort = 1; | |
597 | return BLOCK_ABORT; | |
598 | } | |
599 | ||
600 | if (ext2fs_has_feature_metadata_csum(fs->super)) { | |
601 | struct ext4_orphan_block_tail *tail; | |
602 | ||
603 | tail = ext2fs_orphan_block_tail(fs, pd->buf); | |
604 | /* | |
605 | * Update checksum to match expected buffer contents with | |
606 | * appropriate block number. | |
607 | */ | |
608 | tail->ob_checksum = ext2fs_do_orphan_file_block_csum(fs, | |
609 | pd->ino, pd->generation, blk, pd->buf); | |
610 | } | |
611 | if (!pd->clear) { | |
612 | pd->errcode = io_channel_read_blk64(fs->io, blk, 1, | |
613 | pd->block_buf); | |
614 | /* Block is already cleanly initialized? */ | |
615 | if (!memcmp(pd->block_buf, pd->buf, fs->blocksize)) | |
616 | return 0; | |
617 | ||
618 | clear_problem_context(&pctx); | |
619 | pctx.ino = fs->super->s_orphan_file_inum; | |
620 | pctx.blk = blockcnt; | |
621 | if (!fix_problem(ctx, PR_6_ORPHAN_BLOCK_DIRTY, &pctx)) | |
622 | goto return_abort; | |
623 | pd->clear = 1; | |
624 | } | |
625 | pd->errcode = io_channel_write_blk64(fs->io, blk, 1, pd->buf); | |
626 | if (pd->errcode) | |
627 | goto return_abort; | |
628 | return 0; | |
629 | } | |
630 | ||
631 | /* | |
632 | * Check and clear orphan file. We just return non-zero if we hit some | |
633 | * inconsistency. Caller will truncate & recreate new orphan file. | |
634 | */ | |
635 | int check_init_orphan_file(e2fsck_t ctx) | |
636 | { | |
637 | ext2_filsys fs = ctx->fs; | |
638 | char *orphan_buf; | |
639 | struct process_orphan_block_data pd; | |
640 | struct ext4_orphan_block_tail *tail; | |
641 | ext2_ino_t orphan_inum = fs->super->s_orphan_file_inum; | |
642 | struct ext2_inode orphan_inode; | |
643 | int ret = 0; | |
644 | errcode_t retval; | |
645 | ||
646 | orphan_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 5, | |
647 | "orphan block buffer"); | |
648 | e2fsck_read_inode(ctx, orphan_inum, &orphan_inode, "orphan inode"); | |
649 | if (EXT2_I_SIZE(&orphan_inode) & (fs->blocksize - 1)) { | |
650 | struct problem_context pctx; | |
651 | ||
652 | clear_problem_context(&pctx); | |
653 | pctx.ino = orphan_inum; | |
654 | fix_problem(ctx, PR_6_ORPHAN_FILE_WRONG_SIZE, &pctx); | |
655 | ret = 1; | |
656 | goto out; | |
657 | } | |
658 | pd.buf = orphan_buf + 3 * fs->blocksize; | |
659 | pd.block_buf = orphan_buf + 4 * fs->blocksize; | |
660 | pd.blocks = EXT2_I_SIZE(&orphan_inode) / fs->blocksize; | |
661 | pd.ctx = ctx; | |
662 | pd.abort = 0; | |
663 | pd.clear = 0; | |
664 | pd.errcode = 0; | |
665 | pd.ino = orphan_inum; | |
666 | pd.generation = orphan_inode.i_generation; | |
667 | /* Initialize buffer to write */ | |
668 | memset(pd.buf, 0, fs->blocksize); | |
669 | tail = ext2fs_orphan_block_tail(fs, pd.buf); | |
670 | tail->ob_magic = ext2fs_cpu_to_le32(EXT4_ORPHAN_BLOCK_MAGIC); | |
671 | ||
672 | retval = ext2fs_block_iterate3(fs, orphan_inum, | |
673 | BLOCK_FLAG_DATA_ONLY | BLOCK_FLAG_HOLE, | |
674 | orphan_buf, reinit_orphan_block, &pd); | |
675 | if (retval) { | |
676 | com_err("reinit_orphan_block", retval, | |
677 | _("while calling ext2fs_block_iterate for inode %d"), | |
678 | orphan_inum); | |
679 | ret = 1; | |
680 | goto out; | |
681 | } | |
682 | if (pd.abort) { | |
683 | if (pd.errcode) { | |
684 | com_err("process_orphan_block", pd.errcode, | |
685 | _("while reading blocks of inode %d"), | |
686 | orphan_inum); | |
687 | } | |
688 | ret = 1; | |
689 | } | |
690 | ||
691 | /* We had to clear some blocks. Report it up. */ | |
692 | if (ret == 0 && pd.clear) | |
693 | ret = 2; | |
694 | out: | |
695 | ext2fs_free_mem(&orphan_buf); | |
696 | return ret; | |
697 | } | |
698 | ||
c3ffaf83 TT |
699 | /* |
700 | * Check the resize inode to make sure it is sane. We check both for | |
701 | * the case where on-line resizing is not enabled (in which case the | |
702 | * resize inode should be cleared) as well as the case where on-line | |
703 | * resizing is enabled. | |
704 | */ | |
69d0edfd | 705 | void check_resize_inode(e2fsck_t ctx) |
c3ffaf83 TT |
706 | { |
707 | ext2_filsys fs = ctx->fs; | |
708 | struct ext2_inode inode; | |
709 | struct problem_context pctx; | |
642935c0 TT |
710 | int i, gdt_off, ind_off; |
711 | dgrp_t j; | |
b849f712 | 712 | blk_t blk, pblk; |
027b0577 | 713 | blk_t expect; /* for resize inode, which is 32-bit only */ |
c3ffaf83 TT |
714 | __u32 *dind_buf = 0, *ind_buf; |
715 | errcode_t retval; | |
716 | ||
717 | clear_problem_context(&pctx); | |
c3ffaf83 | 718 | |
74848259 LC |
719 | if (ext2fs_has_feature_resize_inode(fs->super) && |
720 | ext2fs_has_feature_meta_bg(fs->super) && | |
721 | fix_problem(ctx, PR_0_DISABLE_RESIZE_INODE, &pctx)) { | |
722 | ext2fs_clear_feature_resize_inode(fs->super); | |
723 | fs->super->s_reserved_gdt_blocks = 0; | |
724 | ext2fs_mark_super_dirty(fs); | |
725 | } | |
726 | ||
efc6f628 TT |
727 | /* |
728 | * If the resize inode feature isn't set, then | |
ea774315 | 729 | * s_reserved_gdt_blocks must be zero. |
c3ffaf83 | 730 | */ |
86f3b6cf | 731 | if (!ext2fs_has_feature_resize_inode(fs->super)) { |
c3ffaf83 TT |
732 | if (fs->super->s_reserved_gdt_blocks) { |
733 | pctx.num = fs->super->s_reserved_gdt_blocks; | |
734 | if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS, | |
735 | &pctx)) { | |
736 | fs->super->s_reserved_gdt_blocks = 0; | |
737 | ext2fs_mark_super_dirty(fs); | |
738 | } | |
739 | } | |
ea774315 TT |
740 | } |
741 | ||
1f3ad14a | 742 | /* Read the resize inode */ |
ea774315 TT |
743 | pctx.ino = EXT2_RESIZE_INO; |
744 | retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode); | |
745 | if (retval) { | |
86f3b6cf | 746 | if (ext2fs_has_feature_resize_inode(fs->super)) |
ea774315 TT |
747 | ctx->flags |= E2F_FLAG_RESIZE_INODE; |
748 | return; | |
749 | } | |
750 | ||
efc6f628 TT |
751 | /* |
752 | * If the resize inode feature isn't set, check to make sure | |
ea774315 TT |
753 | * the resize inode is cleared; then we're done. |
754 | */ | |
86f3b6cf | 755 | if (!ext2fs_has_feature_resize_inode(fs->super)) { |
c3ffaf83 TT |
756 | for (i=0; i < EXT2_N_BLOCKS; i++) { |
757 | if (inode.i_block[i]) | |
758 | break; | |
759 | } | |
760 | if ((i < EXT2_N_BLOCKS) && | |
761 | fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) { | |
762 | memset(&inode, 0, sizeof(inode)); | |
763 | e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode, | |
764 | "clear_resize"); | |
765 | } | |
766 | return; | |
767 | } | |
ea774315 | 768 | |
efc6f628 | 769 | /* |
c3ffaf83 TT |
770 | * The resize inode feature is enabled; check to make sure the |
771 | * only block in use is the double indirect block | |
772 | */ | |
773 | blk = inode.i_block[EXT2_DIND_BLOCK]; | |
774 | for (i=0; i < EXT2_N_BLOCKS; i++) { | |
775 | if (i != EXT2_DIND_BLOCK && inode.i_block[i]) | |
776 | break; | |
777 | } | |
d4dc0a9e TT |
778 | if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count || |
779 | !(inode.i_mode & LINUX_S_IFREG) || | |
c3ffaf83 | 780 | (blk < fs->super->s_first_data_block || |
4efbac6f | 781 | blk >= ext2fs_blocks_count(fs->super))) { |
c3ffaf83 TT |
782 | resize_inode_invalid: |
783 | if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) { | |
784 | memset(&inode, 0, sizeof(inode)); | |
785 | e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode, | |
786 | "clear_resize"); | |
787 | ctx->flags |= E2F_FLAG_RESIZE_INODE; | |
788 | } | |
789 | if (!(ctx->options & E2F_OPT_READONLY)) { | |
790 | fs->super->s_state &= ~EXT2_VALID_FS; | |
791 | ext2fs_mark_super_dirty(fs); | |
792 | } | |
793 | goto cleanup; | |
794 | } | |
795 | dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2, | |
796 | "resize dind buffer"); | |
797 | ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize); | |
798 | ||
dc8ce346 | 799 | retval = ext2fs_read_ind_block(fs, blk, dind_buf); |
c3ffaf83 TT |
800 | if (retval) |
801 | goto resize_inode_invalid; | |
802 | ||
803 | gdt_off = fs->desc_blocks; | |
804 | pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks; | |
25567a7b TT |
805 | if (fs->blocksize == 1024 && fs->super->s_first_data_block == 0) |
806 | pblk++; /* Deal with 1024 blocksize bigalloc fs */ | |
efc6f628 | 807 | for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4; |
c3ffaf83 TT |
808 | i++, gdt_off++, pblk++) { |
809 | gdt_off %= fs->blocksize/4; | |
810 | if (dind_buf[gdt_off] != pblk) | |
811 | goto resize_inode_invalid; | |
dc8ce346 | 812 | retval = ext2fs_read_ind_block(fs, pblk, ind_buf); |
efc6f628 | 813 | if (retval) |
c3ffaf83 TT |
814 | goto resize_inode_invalid; |
815 | ind_off = 0; | |
816 | for (j = 1; j < fs->group_desc_count; j++) { | |
817 | if (!ext2fs_bg_has_super(fs, j)) | |
818 | continue; | |
1e33a8b4 | 819 | expect = pblk + EXT2_GROUPS_TO_BLOCKS(fs->super, j); |
c3ffaf83 TT |
820 | if (ind_buf[ind_off] != expect) |
821 | goto resize_inode_invalid; | |
822 | ind_off++; | |
823 | } | |
824 | } | |
825 | ||
826 | cleanup: | |
827 | if (dind_buf) | |
828 | ext2fs_free_mem(&dind_buf); | |
829 | ||
830 | } | |
80bfaa3e | 831 | |
f77704e4 TT |
832 | /* |
833 | * This function checks the dirhash signed/unsigned hint if necessary. | |
834 | */ | |
642935c0 | 835 | static void e2fsck_fix_dirhash_hint(e2fsck_t ctx) |
f77704e4 TT |
836 | { |
837 | struct ext2_super_block *sb = ctx->fs->super; | |
838 | struct problem_context pctx; | |
f77704e4 TT |
839 | char c; |
840 | ||
841 | if ((ctx->options & E2F_OPT_READONLY) || | |
86f3b6cf | 842 | !ext2fs_has_feature_dir_index(sb) || |
f77704e4 TT |
843 | (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH))) |
844 | return; | |
845 | ||
846 | c = (char) 255; | |
847 | ||
848 | clear_problem_context(&pctx); | |
849 | if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) { | |
850 | if (((int) c) == -1) { | |
851 | sb->s_flags |= EXT2_FLAGS_SIGNED_HASH; | |
852 | } else { | |
853 | sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH; | |
854 | } | |
855 | ext2fs_mark_super_dirty(ctx->fs); | |
856 | } | |
857 | } | |
858 | ||
859 | ||
1b6bf175 TT |
860 | void check_super_block(e2fsck_t ctx) |
861 | { | |
862 | ext2_filsys fs = ctx->fs; | |
6dc64392 | 863 | blk64_t first_block, last_block; |
5dd8f963 | 864 | struct ext2_super_block *sb = fs->super; |
68477355 | 865 | unsigned int ipg_max; |
26ea4899 | 866 | problem_t problem; |
6dc64392 | 867 | blk64_t blocks_per_group = fs->super->s_blocks_per_group; |
44fe08f1 | 868 | __u32 bpg_max, cpg_max; |
32cb8473 | 869 | __u64 blks_max; |
78cf0547 | 870 | int inodes_per_block; |
89db86d3 | 871 | int inode_size; |
ba5131f6 | 872 | int accept_time_fudge; |
177839e2 | 873 | int broken_system_clock; |
7f813ba3 | 874 | dgrp_t i; |
6dc64392 | 875 | blk64_t should_be; |
1b6bf175 | 876 | struct problem_context pctx; |
6dc64392 | 877 | blk64_t free_blocks = 0; |
382ed4a1 | 878 | ext2_ino_t free_inodes = 0; |
80875db5 | 879 | int csum_flag, clear_test_fs_flag; |
b21bf267 AD |
880 | |
881 | inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super); | |
882 | ipg_max = inodes_per_block * (blocks_per_group - 4); | |
883 | if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb)) | |
884 | ipg_max = EXT2_MAX_INODES_PER_GROUP(sb); | |
44fe08f1 TT |
885 | cpg_max = 8 * EXT2_BLOCK_SIZE(sb); |
886 | if (cpg_max > EXT2_MAX_CLUSTERS_PER_GROUP(sb)) | |
887 | cpg_max = EXT2_MAX_CLUSTERS_PER_GROUP(sb); | |
888 | bpg_max = 8 * EXT2_BLOCK_SIZE(sb) * EXT2FS_CLUSTER_RATIO(fs); | |
b21bf267 AD |
889 | if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb)) |
890 | bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb); | |
1b6bf175 | 891 | |
54dc7ca2 | 892 | ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx, |
f8188fff | 893 | sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap"); |
54dc7ca2 | 894 | ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx, |
f8188fff | 895 | sizeof(int) * fs->group_desc_count, "invalid_block_bitmap"); |
54dc7ca2 | 896 | ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx, |
f8188fff | 897 | sizeof(int) * fs->group_desc_count, "invalid_inode_table"); |
b21bf267 | 898 | |
32cb8473 TT |
899 | blks_max = (1ULL << 32) * EXT2_MAX_BLOCKS_PER_GROUP(fs->super); |
900 | if (ext2fs_has_feature_64bit(fs->super)) { | |
901 | if (blks_max > ((1ULL << 48) - 1)) | |
902 | blks_max = (1ULL << 48) - 1; | |
903 | } else { | |
904 | if (blks_max > ((1ULL << 32) - 1)) | |
905 | blks_max = (1ULL << 32) - 1; | |
906 | } | |
907 | ||
1b6bf175 TT |
908 | clear_problem_context(&pctx); |
909 | ||
910 | /* | |
911 | * Verify the super block constants... | |
912 | */ | |
0feae066 JK |
913 | if (!check_super_value(ctx, "inodes_count", sb->s_inodes_count, |
914 | MIN_CHECK, 1, 0)) | |
915 | return; | |
916 | if (!check_super_value64(ctx, "blocks_count", ext2fs_blocks_count(sb), | |
917 | MIN_CHECK | MAX_CHECK, 1, blks_max)) | |
918 | return; | |
919 | if (!check_super_value(ctx, "first_data_block", sb->s_first_data_block, | |
920 | MAX_CHECK, 0, ext2fs_blocks_count(sb))) | |
921 | return; | |
922 | if (!check_super_value(ctx, "log_block_size", sb->s_log_block_size, | |
923 | MIN_CHECK | MAX_CHECK, 0, | |
924 | EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE)) | |
925 | return; | |
926 | if (!check_super_value(ctx, "log_cluster_size", | |
927 | sb->s_log_cluster_size, | |
928 | MIN_CHECK | MAX_CHECK, sb->s_log_block_size, | |
929 | (EXT2_MAX_CLUSTER_LOG_SIZE - | |
930 | EXT2_MIN_CLUSTER_LOG_SIZE))) | |
931 | return; | |
932 | if (!check_super_value(ctx, "clusters_per_group", | |
933 | sb->s_clusters_per_group, | |
934 | MIN_CHECK | MAX_CHECK, 8, cpg_max)) | |
935 | return; | |
936 | if (!check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group, | |
937 | MIN_CHECK | MAX_CHECK, 8, bpg_max)) | |
938 | return; | |
939 | if (!check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group, | |
940 | MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max)) | |
941 | return; | |
942 | if (!check_super_value(ctx, "r_blocks_count", ext2fs_r_blocks_count(sb), | |
943 | MAX_CHECK, 0, ext2fs_blocks_count(sb) / 2)) | |
944 | return; | |
945 | if (!check_super_value(ctx, "reserved_gdt_blocks", | |
946 | sb->s_reserved_gdt_blocks, MAX_CHECK, 0, | |
947 | fs->blocksize / sizeof(__u32))) | |
948 | return; | |
949 | if (!check_super_value(ctx, "desc_size", | |
950 | sb->s_desc_size, MAX_CHECK | LOG2_CHECK, 0, | |
951 | EXT2_MAX_DESC_SIZE)) | |
952 | return; | |
d74eb7ef JK |
953 | |
954 | should_be = (__u64)sb->s_inodes_per_group * fs->group_desc_count; | |
955 | if (should_be > ~0U) { | |
956 | pctx.num = should_be; | |
957 | fix_problem(ctx, PR_0_INODE_COUNT_BIG, &pctx); | |
958 | ctx->flags |= E2F_FLAG_ABORT; | |
959 | return; | |
960 | } | |
961 | if (sb->s_inodes_count != should_be) { | |
962 | pctx.ino = sb->s_inodes_count; | |
963 | pctx.ino2 = should_be; | |
964 | if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) { | |
965 | sb->s_inodes_count = should_be; | |
966 | ext2fs_mark_super_dirty(fs); | |
867ffd47 TT |
967 | } else { |
968 | pctx.num = sb->s_inodes_count; | |
969 | pctx.str = "inodes_count"; | |
970 | fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx); | |
971 | ctx->flags |= E2F_FLAG_ABORT; | |
972 | return; | |
d74eb7ef JK |
973 | } |
974 | } | |
0feae066 JK |
975 | if (sb->s_rev_level > EXT2_GOOD_OLD_REV && |
976 | !check_super_value(ctx, "first_ino", sb->s_first_ino, | |
977 | MIN_CHECK | MAX_CHECK, | |
978 | EXT2_GOOD_OLD_FIRST_INO, sb->s_inodes_count)) | |
979 | return; | |
89db86d3 | 980 | inode_size = EXT2_INODE_SIZE(sb); |
0feae066 JK |
981 | if (!check_super_value(ctx, "inode_size", |
982 | inode_size, MIN_CHECK | MAX_CHECK | LOG2_CHECK, | |
983 | EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize)) | |
984 | return; | |
44fe08f1 TT |
985 | if (sb->s_blocks_per_group != (sb->s_clusters_per_group * |
986 | EXT2FS_CLUSTER_RATIO(fs))) { | |
987 | pctx.num = sb->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs); | |
988 | pctx.str = "block_size"; | |
989 | fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx); | |
0feae066 | 990 | ctx->flags |= E2F_FLAG_ABORT; |
44fe08f1 TT |
991 | return; |
992 | } | |
efc6f628 | 993 | |
d2af1bdd | 994 | if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) && |
4efbac6f VAH |
995 | (ctx->num_blocks < ext2fs_blocks_count(sb))) { |
996 | pctx.blk = ext2fs_blocks_count(sb); | |
d2af1bdd TT |
997 | pctx.blk2 = ctx->num_blocks; |
998 | if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) { | |
08b21301 TT |
999 | ctx->flags |= E2F_FLAG_ABORT; |
1000 | return; | |
1001 | } | |
1b6bf175 TT |
1002 | } |
1003 | ||
25567a7b TT |
1004 | should_be = (sb->s_log_block_size == 0 && |
1005 | EXT2FS_CLUSTER_RATIO(fs) == 1) ? 1 : 0; | |
5dd8f963 TT |
1006 | if (sb->s_first_data_block != should_be) { |
1007 | pctx.blk = sb->s_first_data_block; | |
1b6bf175 TT |
1008 | pctx.blk2 = should_be; |
1009 | fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx); | |
08b21301 TT |
1010 | ctx->flags |= E2F_FLAG_ABORT; |
1011 | return; | |
1b6bf175 TT |
1012 | } |
1013 | ||
047d5d77 TT |
1014 | if (EXT2_INODE_SIZE(sb) > EXT2_GOOD_OLD_INODE_SIZE) { |
1015 | unsigned min = | |
1016 | sizeof(((struct ext2_inode_large *) 0)->i_extra_isize) + | |
1017 | sizeof(((struct ext2_inode_large *) 0)->i_checksum_hi); | |
1018 | unsigned max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE; | |
1019 | pctx.num = sb->s_min_extra_isize; | |
1020 | if (sb->s_min_extra_isize && | |
1021 | (sb->s_min_extra_isize < min || | |
1022 | sb->s_min_extra_isize > max || | |
1023 | sb->s_min_extra_isize & 3) && | |
1024 | fix_problem(ctx, PR_0_BAD_MIN_EXTRA_ISIZE, &pctx)) { | |
1025 | sb->s_min_extra_isize = | |
1026 | (sizeof(struct ext2_inode_large) - | |
1027 | EXT2_GOOD_OLD_INODE_SIZE); | |
1028 | ext2fs_mark_super_dirty(fs); | |
1029 | } | |
1030 | pctx.num = sb->s_want_extra_isize; | |
1031 | if (sb->s_want_extra_isize && | |
1032 | (sb->s_want_extra_isize < min || | |
1033 | sb->s_want_extra_isize > max || | |
1034 | sb->s_want_extra_isize & 3) && | |
1035 | fix_problem(ctx, PR_0_BAD_WANT_EXTRA_ISIZE, &pctx)) { | |
1036 | sb->s_want_extra_isize = | |
1037 | (sizeof(struct ext2_inode_large) - | |
1038 | EXT2_GOOD_OLD_INODE_SIZE); | |
1039 | ext2fs_mark_super_dirty(fs); | |
1040 | } | |
1041 | } | |
1042 | ||
387e6384 | 1043 | /* Are metadata_csum and uninit_bg both set? */ |
86f3b6cf DW |
1044 | if (ext2fs_has_feature_metadata_csum(fs->super) && |
1045 | ext2fs_has_feature_gdt_csum(fs->super) && | |
387e6384 | 1046 | fix_problem(ctx, PR_0_META_AND_GDT_CSUM_SET, &pctx)) { |
86f3b6cf | 1047 | ext2fs_clear_feature_gdt_csum(fs->super); |
387e6384 DW |
1048 | ext2fs_mark_super_dirty(fs); |
1049 | for (i = 0; i < fs->group_desc_count; i++) | |
1050 | ext2fs_group_desc_csum_set(fs, i); | |
1051 | } | |
1052 | ||
17f2fbf7 DW |
1053 | /* We can't have ^metadata_csum,metadata_csum_seed */ |
1054 | if (!ext2fs_has_feature_metadata_csum(fs->super) && | |
1055 | ext2fs_has_feature_csum_seed(fs->super) && | |
1056 | fix_problem(ctx, PR_0_CSUM_SEED_WITHOUT_META_CSUM, &pctx)) { | |
1057 | ext2fs_clear_feature_csum_seed(fs->super); | |
1058 | fs->super->s_checksum_seed = 0; | |
1059 | ext2fs_mark_super_dirty(fs); | |
1060 | } | |
1061 | ||
27b2297d | 1062 | /* Is 64bit set and extents unset? */ |
86f3b6cf DW |
1063 | if (ext2fs_has_feature_64bit(fs->super) && |
1064 | !ext2fs_has_feature_extents(fs->super) && | |
27b2297d | 1065 | fix_problem(ctx, PR_0_64BIT_WITHOUT_EXTENTS, &pctx)) { |
86f3b6cf | 1066 | ext2fs_set_feature_extents(fs->super); |
27b2297d DW |
1067 | ext2fs_mark_super_dirty(fs); |
1068 | } | |
1069 | ||
e228d700 DW |
1070 | /* Did user ask us to convert files to extents? */ |
1071 | if (ctx->options & E2F_OPT_CONVERT_BMAP) { | |
86f3b6cf | 1072 | ext2fs_set_feature_extents(fs->super); |
e228d700 DW |
1073 | ext2fs_mark_super_dirty(fs); |
1074 | } | |
1075 | ||
86f3b6cf | 1076 | if (ext2fs_has_feature_meta_bg(fs->super) && |
7a4352dc TT |
1077 | (fs->super->s_first_meta_bg > fs->desc_blocks)) { |
1078 | pctx.group = fs->desc_blocks; | |
1079 | pctx.num = fs->super->s_first_meta_bg; | |
1080 | if (fix_problem(ctx, PR_0_FIRST_META_BG_TOO_BIG, &pctx)) { | |
86f3b6cf | 1081 | ext2fs_clear_feature_meta_bg(fs->super); |
7a4352dc TT |
1082 | fs->super->s_first_meta_bg = 0; |
1083 | ext2fs_mark_super_dirty(fs); | |
1084 | } | |
1085 | } | |
1086 | ||
1b6bf175 TT |
1087 | /* |
1088 | * Verify the group descriptors.... | |
1089 | */ | |
88b34b1f | 1090 | first_block = sb->s_first_data_block; |
4efbac6f | 1091 | last_block = ext2fs_blocks_count(sb)-1; |
1b6bf175 | 1092 | |
387e6384 | 1093 | csum_flag = ext2fs_has_group_desc_csum(fs); |
d7cca6b0 | 1094 | for (i = 0; i < fs->group_desc_count; i++) { |
1b6bf175 | 1095 | pctx.group = i; |
abf23439 | 1096 | |
86f3b6cf | 1097 | if (!ext2fs_has_feature_flex_bg(fs->super)) { |
b49f78fe TT |
1098 | first_block = ext2fs_group_first_block2(fs, i); |
1099 | last_block = ext2fs_group_last_block2(fs, i); | |
88b34b1f | 1100 | } |
bb1a46a4 | 1101 | |
d7cca6b0 VAH |
1102 | if ((ext2fs_block_bitmap_loc(fs, i) < first_block) || |
1103 | (ext2fs_block_bitmap_loc(fs, i) > last_block)) { | |
1104 | pctx.blk = ext2fs_block_bitmap_loc(fs, i); | |
24bfb443 | 1105 | if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx)) |
d7cca6b0 | 1106 | ext2fs_block_bitmap_loc_set(fs, i, 0); |
24bfb443 | 1107 | } |
d7cca6b0 | 1108 | if (ext2fs_block_bitmap_loc(fs, i) == 0) { |
24bfb443 TT |
1109 | ctx->invalid_block_bitmap_flag[i]++; |
1110 | ctx->invalid_bitmaps++; | |
1b6bf175 | 1111 | } |
d7cca6b0 VAH |
1112 | if ((ext2fs_inode_bitmap_loc(fs, i) < first_block) || |
1113 | (ext2fs_inode_bitmap_loc(fs, i) > last_block)) { | |
1114 | pctx.blk = ext2fs_inode_bitmap_loc(fs, i); | |
24bfb443 | 1115 | if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx)) |
d7cca6b0 | 1116 | ext2fs_inode_bitmap_loc_set(fs, i, 0); |
24bfb443 | 1117 | } |
d7cca6b0 | 1118 | if (ext2fs_inode_bitmap_loc(fs, i) == 0) { |
24bfb443 TT |
1119 | ctx->invalid_inode_bitmap_flag[i]++; |
1120 | ctx->invalid_bitmaps++; | |
1b6bf175 | 1121 | } |
d7cca6b0 VAH |
1122 | if ((ext2fs_inode_table_loc(fs, i) < first_block) || |
1123 | ((ext2fs_inode_table_loc(fs, i) + | |
bb1a46a4 | 1124 | fs->inode_blocks_per_group - 1) > last_block)) { |
d7cca6b0 | 1125 | pctx.blk = ext2fs_inode_table_loc(fs, i); |
24bfb443 | 1126 | if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx)) |
d7cca6b0 | 1127 | ext2fs_inode_table_loc_set(fs, i, 0); |
24bfb443 | 1128 | } |
d7cca6b0 | 1129 | if (ext2fs_inode_table_loc(fs, i) == 0) { |
24bfb443 TT |
1130 | ctx->invalid_inode_table_flag[i]++; |
1131 | ctx->invalid_bitmaps++; | |
1b6bf175 | 1132 | } |
d7cca6b0 VAH |
1133 | free_blocks += ext2fs_bg_free_blocks_count(fs, i); |
1134 | free_inodes += ext2fs_bg_free_inodes_count(fs, i); | |
550a4afa | 1135 | |
d7cca6b0 VAH |
1136 | if ((ext2fs_bg_free_blocks_count(fs, i) > sb->s_blocks_per_group) || |
1137 | (ext2fs_bg_free_inodes_count(fs, i) > sb->s_inodes_per_group) || | |
1138 | (ext2fs_bg_used_dirs_count(fs, i) > sb->s_inodes_per_group)) | |
550a4afa TT |
1139 | ext2fs_unmark_valid(fs); |
1140 | ||
0d5439c8 | 1141 | should_be = 0; |
49a7360b | 1142 | if (!ext2fs_group_desc_csum_verify(fs, i)) { |
b9a64a51 TT |
1143 | pctx.csum1 = ext2fs_bg_checksum(fs, i); |
1144 | pctx.csum2 = ext2fs_group_desc_csum(fs, i); | |
49a7360b | 1145 | if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) { |
e633b58a ES |
1146 | ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT); |
1147 | ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT); | |
d7cca6b0 | 1148 | ext2fs_bg_itable_unused_set(fs, i, 0); |
0d5439c8 | 1149 | should_be = 1; |
49a7360b JS |
1150 | } |
1151 | ext2fs_unmark_valid(fs); | |
1152 | } | |
1153 | ||
16b851cd | 1154 | if (!csum_flag && |
cd65a24e TT |
1155 | (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) || |
1156 | ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) || | |
d7cca6b0 | 1157 | ext2fs_bg_itable_unused(fs, i) != 0)) { |
49a7360b | 1158 | if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) { |
e633b58a ES |
1159 | ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT); |
1160 | ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT); | |
d7cca6b0 | 1161 | ext2fs_bg_itable_unused_set(fs, i, 0); |
0d5439c8 | 1162 | should_be = 1; |
49a7360b JS |
1163 | } |
1164 | ext2fs_unmark_valid(fs); | |
1165 | } | |
0d5439c8 AD |
1166 | |
1167 | if (i == fs->group_desc_count - 1 && | |
cd65a24e | 1168 | ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)) { |
0d5439c8 | 1169 | if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) { |
e633b58a | 1170 | ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT); |
0d5439c8 AD |
1171 | should_be = 1; |
1172 | } | |
1173 | ext2fs_unmark_valid(fs); | |
1174 | } | |
1175 | ||
49a7360b | 1176 | if (csum_flag && |
d7cca6b0 VAH |
1177 | (ext2fs_bg_itable_unused(fs, i) > ext2fs_bg_free_inodes_count(fs, i) || |
1178 | ext2fs_bg_itable_unused(fs, i) > sb->s_inodes_per_group)) { | |
1179 | pctx.blk = ext2fs_bg_itable_unused(fs, i); | |
0d5439c8 | 1180 | if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) { |
d7cca6b0 | 1181 | ext2fs_bg_itable_unused_set(fs, i, 0); |
0d5439c8 AD |
1182 | should_be = 1; |
1183 | } | |
49a7360b JS |
1184 | ext2fs_unmark_valid(fs); |
1185 | } | |
0d5439c8 AD |
1186 | |
1187 | if (should_be) | |
1188 | ext2fs_group_desc_csum_set(fs, i); | |
bee24f36 VAH |
1189 | /* If the user aborts e2fsck by typing ^C, stop right away */ |
1190 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) | |
1191 | return; | |
1b6bf175 | 1192 | } |
550a4afa | 1193 | |
fe75afbf | 1194 | ctx->free_blocks = EXT2FS_C2B(fs, free_blocks); |
a3efe484 | 1195 | ctx->free_inodes = free_inodes; |
efc6f628 | 1196 | |
4efbac6f | 1197 | if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) || |
550a4afa TT |
1198 | (sb->s_free_inodes_count > sb->s_inodes_count)) |
1199 | ext2fs_unmark_valid(fs); | |
1200 | ||
1201 | ||
1b6bf175 TT |
1202 | /* |
1203 | * If we have invalid bitmaps, set the error state of the | |
1204 | * filesystem. | |
1205 | */ | |
1206 | if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) { | |
550a4afa | 1207 | sb->s_state &= ~EXT2_VALID_FS; |
1b6bf175 TT |
1208 | ext2fs_mark_super_dirty(fs); |
1209 | } | |
1210 | ||
4ea0a110 | 1211 | clear_problem_context(&pctx); |
efc6f628 | 1212 | |
54be2ccc | 1213 | #ifndef EXT2_SKIP_UUID |
1b6bf175 TT |
1214 | /* |
1215 | * If the UUID field isn't assigned, assign it. | |
c22f3284 MM |
1216 | * Skip if checksums are enabled and the filesystem is mounted, |
1217 | * if the id changes under the kernel remounting rw may fail. | |
1b6bf175 | 1218 | */ |
c22f3284 | 1219 | if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid) && |
24b8916a | 1220 | !ext2fs_has_feature_metadata_csum(ctx->fs->super) && |
c22f3284 | 1221 | (!csum_flag || !(ctx->mount_flags & EXT2_MF_MOUNTED))) { |
1b6bf175 | 1222 | if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) { |
5dd8f963 | 1223 | uuid_generate(sb->s_uuid); |
4ffbe3af | 1224 | ext2fs_init_csum_seed(fs); |
86db36ab | 1225 | fs->flags |= EXT2_FLAG_DIRTY; |
299d7424 | 1226 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
1b6bf175 TT |
1227 | } |
1228 | } | |
54be2ccc | 1229 | #endif |
2a77a784 | 1230 | |
80875db5 TT |
1231 | /* |
1232 | * Check to see if we should disable the test_fs flag | |
1233 | */ | |
1234 | profile_get_boolean(ctx->profile, "options", | |
1235 | "clear_test_fs_flag", 0, 1, | |
1236 | &clear_test_fs_flag); | |
1237 | if (!(ctx->options & E2F_OPT_READONLY) && | |
1238 | clear_test_fs_flag && | |
1239 | (fs->super->s_flags & EXT2_FLAGS_TEST_FILESYS) && | |
1240 | (fs_proc_check("ext4") || check_for_modules("ext4"))) { | |
1241 | if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) { | |
1242 | fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS; | |
86db36ab | 1243 | fs->flags |= EXT2_FLAG_DIRTY; |
80875db5 TT |
1244 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
1245 | } | |
1246 | } | |
1247 | ||
4ea0a110 TT |
1248 | /* |
1249 | * For the Hurd, check to see if the filetype option is set, | |
1250 | * since it doesn't support it. | |
1251 | */ | |
80bfaa3e TT |
1252 | if (!(ctx->options & E2F_OPT_READONLY) && |
1253 | fs->super->s_creator_os == EXT2_OS_HURD && | |
86f3b6cf | 1254 | ext2fs_has_feature_filetype(fs->super)) { |
4ea0a110 | 1255 | if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) { |
86f3b6cf | 1256 | ext2fs_clear_feature_filetype(fs->super); |
4ea0a110 | 1257 | ext2fs_mark_super_dirty(fs); |
0cfce7f7 | 1258 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
4ea0a110 TT |
1259 | } |
1260 | } | |
80bfaa3e | 1261 | |
7b59f1ef TT |
1262 | /* |
1263 | * If we have any of the compatibility flags set, we need to have a | |
1264 | * revision 1 filesystem. Most kernels will not check the flags on | |
1265 | * a rev 0 filesystem and we may have corruption issues because of | |
1266 | * the incompatible changes to the filesystem. | |
1267 | */ | |
1268 | if (!(ctx->options & E2F_OPT_READONLY) && | |
1269 | fs->super->s_rev_level == EXT2_GOOD_OLD_REV && | |
1270 | (fs->super->s_feature_compat || | |
1271 | fs->super->s_feature_ro_compat || | |
1272 | fs->super->s_feature_incompat) && | |
1273 | fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) { | |
a917d1cc | 1274 | ext2fs_update_dynamic_rev(fs); |
7b59f1ef | 1275 | ext2fs_mark_super_dirty(fs); |
0cfce7f7 | 1276 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
7b59f1ef TT |
1277 | } |
1278 | ||
80bfaa3e TT |
1279 | /* |
1280 | * Clean up any orphan inodes, if present. | |
1281 | */ | |
1282 | if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) { | |
1283 | fs->super->s_state &= ~EXT2_VALID_FS; | |
7b59f1ef | 1284 | ext2fs_mark_super_dirty(fs); |
80bfaa3e TT |
1285 | } |
1286 | ||
60702c26 | 1287 | /* |
ba5131f6 TT |
1288 | * Unfortunately, due to Windows' unfortunate design decision |
1289 | * to configure the hardware clock to tick localtime, instead | |
1290 | * of the more proper and less error-prone UTC time, many | |
1291 | * users end up in the situation where the system clock is | |
1292 | * incorrectly set at the time when e2fsck is run. | |
1293 | * | |
1294 | * Historically this was usually due to some distributions | |
1295 | * having buggy init scripts and/or installers that didn't | |
1296 | * correctly detect this case and take appropriate | |
1297 | * countermeasures. However, it's still possible, despite the | |
1298 | * best efforts of init script and installer authors to not be | |
1299 | * able to detect this misconfiguration, usually due to a | |
1300 | * buggy or misconfigured virtualization manager or the | |
1301 | * installer not having access to a network time server during | |
1302 | * the installation process. So by default, we allow the | |
1303 | * superblock times to be fudged by up to 24 hours. This can | |
1304 | * be disabled by setting options.accept_time_fudge to the | |
1305 | * boolean value of false in e2fsck.conf. We also support | |
1306 | * options.buggy_init_scripts for backwards compatibility. | |
60702c26 | 1307 | */ |
ba5131f6 TT |
1308 | profile_get_boolean(ctx->profile, "options", "accept_time_fudge", |
1309 | 0, 1, &accept_time_fudge); | |
60702c26 | 1310 | profile_get_boolean(ctx->profile, "options", "buggy_init_scripts", |
ba5131f6 TT |
1311 | 0, accept_time_fudge, &accept_time_fudge); |
1312 | ctx->time_fudge = accept_time_fudge ? 86400 : 0; | |
60702c26 | 1313 | |
177839e2 TT |
1314 | profile_get_boolean(ctx->profile, "options", "broken_system_clock", |
1315 | 0, 0, &broken_system_clock); | |
1316 | ||
efc6f628 | 1317 | /* |
8dceb924 TT |
1318 | * Check to see if the superblock last mount time or last |
1319 | * write time is in the future. | |
1320 | */ | |
2c69c942 LC |
1321 | if (((ctx->options & E2F_OPT_FORCE) || fs->super->s_checkinterval) && |
1322 | !broken_system_clock && !(ctx->flags & E2F_FLAG_TIME_INSANE) && | |
ca8bc924 AD |
1323 | (ext2fs_get_tstamp(fs->super, s_mtime) > ctx->now)) { |
1324 | pctx.num = ext2fs_get_tstamp(fs->super, s_mtime); | |
26ea4899 | 1325 | problem = PR_0_FUTURE_SB_LAST_MOUNT; |
157b88c3 | 1326 | if ((time_t) pctx.num <= ctx->now + ctx->time_fudge) |
26ea4899 TT |
1327 | problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED; |
1328 | if (fix_problem(ctx, problem, &pctx)) { | |
ca8bc924 | 1329 | ext2fs_set_tstamp(fs->super, s_mtime, ctx->now); |
86db36ab | 1330 | fs->flags |= EXT2_FLAG_DIRTY; |
8dceb924 TT |
1331 | } |
1332 | } | |
2c69c942 LC |
1333 | if (((ctx->options & E2F_OPT_FORCE) || fs->super->s_checkinterval) && |
1334 | !broken_system_clock && !(ctx->flags & E2F_FLAG_TIME_INSANE) && | |
ca8bc924 AD |
1335 | (ext2fs_get_tstamp(fs->super, s_wtime) > ctx->now)) { |
1336 | pctx.num = ext2fs_get_tstamp(fs->super, s_wtime); | |
fe26a55a | 1337 | problem = PR_0_FUTURE_SB_LAST_WRITE; |
157b88c3 | 1338 | if ((time_t) pctx.num <= ctx->now + ctx->time_fudge) |
61a9d2b3 | 1339 | problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED; |
26ea4899 | 1340 | if (fix_problem(ctx, problem, &pctx)) { |
ca8bc924 | 1341 | ext2fs_set_tstamp(fs->super, s_wtime, ctx->now); |
86db36ab | 1342 | fs->flags |= EXT2_FLAG_DIRTY; |
8dceb924 TT |
1343 | } |
1344 | } | |
1345 | ||
d202f46d TT |
1346 | e2fsck_validate_quota_inodes(ctx); |
1347 | ||
773fd8a1 TT |
1348 | /* |
1349 | * Move the ext3 journal file, if necessary. | |
1350 | */ | |
1351 | e2fsck_move_ext3_journal(ctx); | |
b1c52b26 TT |
1352 | |
1353 | /* | |
1354 | * Fix journal hint, if necessary | |
1355 | */ | |
1356 | e2fsck_fix_ext3_journal_hint(ctx); | |
1357 | ||
f77704e4 TT |
1358 | /* |
1359 | * Add dirhash hint if necessary | |
1360 | */ | |
1361 | e2fsck_fix_dirhash_hint(ctx); | |
1362 | ||
624e4a64 AK |
1363 | /* |
1364 | * Hide quota inodes if necessary. | |
1365 | */ | |
1366 | e2fsck_hide_quota(ctx); | |
1367 | ||
1b6bf175 TT |
1368 | return; |
1369 | } | |
0c37f456 TT |
1370 | |
1371 | /* | |
1372 | * Check to see if we should backup the master sb to the backup super | |
a8cde73a | 1373 | * blocks. Returns non-zero if the sb should be backed up. |
0c37f456 | 1374 | */ |
a8cde73a TT |
1375 | |
1376 | /* | |
1377 | * A few flags are set on the fly by the kernel, but only in the | |
1378 | * primary superblock. This is actually a bad thing, and we should | |
1379 | * try to discourage it in the future. In particular, for the newer | |
1380 | * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and | |
1381 | * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the | |
d9ff474b ES |
1382 | * future. EXT3_FEATURE_INCOMPAT_RECOVER may also get set when |
1383 | * copying the primary superblock during online resize. | |
a8cde73a TT |
1384 | * |
1385 | * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but | |
1386 | * unfortunately, we shouldn't ignore it since if it's not set in the | |
1387 | * backup, the extended attributes in the filesystem will be stripped | |
1388 | * away. | |
1389 | */ | |
1390 | #define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \ | |
24a11cc3 TT |
1391 | EXT4_FEATURE_RO_COMPAT_DIR_NLINK| \ |
1392 | EXT4_FEATURE_RO_COMPAT_ORPHAN_PRESENT) | |
d9ff474b ES |
1393 | #define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS| \ |
1394 | EXT3_FEATURE_INCOMPAT_RECOVER) | |
a8cde73a | 1395 | |
0c37f456 TT |
1396 | int check_backup_super_block(e2fsck_t ctx) |
1397 | { | |
1398 | ext2_filsys fs = ctx->fs; | |
0c37f456 TT |
1399 | errcode_t retval; |
1400 | dgrp_t g; | |
6dc64392 | 1401 | blk64_t sb; |
0c37f456 | 1402 | int ret = 0; |
982dd30c TT |
1403 | char buf[SUPERBLOCK_SIZE]; |
1404 | struct ext2_super_block *backup_sb; | |
0c37f456 TT |
1405 | |
1406 | /* | |
1407 | * If we are already writing out the backup blocks, then we | |
1408 | * don't need to test. Also, if the filesystem is invalid, or | |
1409 | * the check was aborted or cancelled, we also don't want to | |
1410 | * do the backup. If the filesystem was opened read-only then | |
1411 | * we can't do the backup. | |
1412 | */ | |
1413 | if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) || | |
1414 | !ext2fs_test_valid(fs) || | |
1415 | (fs->super->s_state & EXT2_ERROR_FS) || | |
1416 | (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) || | |
1417 | (ctx->options & E2F_OPT_READONLY)) | |
1418 | return 0; | |
1419 | ||
1420 | for (g = 1; g < fs->group_desc_count; g++) { | |
1421 | if (!ext2fs_bg_has_super(fs, g)) | |
1422 | continue; | |
1423 | ||
027b0577 | 1424 | sb = ext2fs_group_first_block2(fs, g); |
0c37f456 | 1425 | |
982dd30c TT |
1426 | retval = io_channel_read_blk(fs->io, sb, -SUPERBLOCK_SIZE, |
1427 | buf); | |
1428 | if (retval) | |
1429 | continue; | |
1430 | backup_sb = (struct ext2_super_block *) buf; | |
1431 | #ifdef WORDS_BIGENDIAN | |
1432 | ext2fs_swap_super(backup_sb); | |
1433 | #endif | |
1434 | if ((backup_sb->s_magic != EXT2_SUPER_MAGIC) || | |
1435 | (backup_sb->s_rev_level > EXT2_LIB_CURRENT_REV) || | |
1436 | ((backup_sb->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) > | |
1437 | EXT2_MAX_BLOCK_LOG_SIZE) || | |
1438 | (EXT2_INODE_SIZE(backup_sb) < EXT2_GOOD_OLD_INODE_SIZE)) | |
0c37f456 | 1439 | continue; |
0c37f456 | 1440 | |
a8cde73a | 1441 | #define SUPER_INCOMPAT_DIFFERENT(x) \ |
982dd30c TT |
1442 | ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \ |
1443 | (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE)) | |
a8cde73a | 1444 | #define SUPER_RO_COMPAT_DIFFERENT(x) \ |
982dd30c TT |
1445 | ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \ |
1446 | (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE)) | |
a8cde73a | 1447 | #define SUPER_DIFFERENT(x) \ |
982dd30c | 1448 | (fs->super->x != backup_sb->x) |
a8cde73a | 1449 | |
0c37f456 | 1450 | if (SUPER_DIFFERENT(s_feature_compat) || |
a8cde73a TT |
1451 | SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) || |
1452 | SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) || | |
0c37f456 | 1453 | SUPER_DIFFERENT(s_blocks_count) || |
2fe2d408 | 1454 | SUPER_DIFFERENT(s_blocks_count_hi) || |
0c37f456 | 1455 | SUPER_DIFFERENT(s_inodes_count) || |
982dd30c | 1456 | memcmp(fs->super->s_uuid, backup_sb->s_uuid, |
0c37f456 TT |
1457 | sizeof(fs->super->s_uuid))) |
1458 | ret = 1; | |
0c37f456 TT |
1459 | break; |
1460 | } | |
1461 | return ret; | |
1462 | } |