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