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