Whamcloud - gitweb
Prevent i_dtime from being mistaken for an inode number post-2038 wraparound
[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 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
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
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  */
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. */
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. */
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 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                 ext2fs_set_dtime(fs, EXT2_INODE(&inode));
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
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;
498         ext2_ino_t ino;
499         struct problem_context pctx;
500         char *block_buf;
501
502         if (fs->super->s_last_orphan == 0 &&
503             !ext2fs_has_feature_orphan_present(fs->super))
504                 return 0;
505
506         clear_problem_context(&pctx);
507         ino = fs->super->s_last_orphan;
508         pctx.ino = ino;
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
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);
521
522         /*
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.
527          */
528         if (fs->super->s_state & EXT2_ERROR_FS) {
529                 if (ctx->qctx)
530                         quota_release_context(&ctx->qctx);
531                 return 0;
532         }
533
534         if (ino && ((ino < EXT2_FIRST_INODE(fs->super)) ||
535             (ino > fs->super->s_inodes_count))) {
536                 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
537                 goto err_qctx;
538         }
539
540         block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
541                                                     "block iterate buffer");
542         e2fsck_read_bitmaps(ctx);
543
544         /* First process orphan list */
545         while (ino) {
546                 if (release_orphan_inode(ctx, &ino, block_buf))
547                         goto err_buf;
548         }
549
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;
556
557         ext2fs_free_mem(&block_buf);
558         pctx.errcode = e2fsck_write_all_quotas(ctx);
559         if (pctx.errcode)
560                 goto err;
561         return 0;
562 err_buf:
563         ext2fs_free_mem(&block_buf);
564 err_qctx:
565         if (ctx->qctx)
566                 quota_release_context(&ctx->qctx);
567 err:
568         return 1;
569 }
570
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
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  */
705 void check_resize_inode(e2fsck_t ctx)
706 {
707         ext2_filsys fs = ctx->fs;
708         struct ext2_inode inode;
709         struct problem_context  pctx;
710         int             i, gdt_off, ind_off;
711         dgrp_t          j;
712         blk_t           blk, pblk;
713         blk_t           expect; /* for resize inode, which is 32-bit only */
714         __u32           *dind_buf = 0, *ind_buf;
715         errcode_t       retval;
716
717         clear_problem_context(&pctx);
718
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
727         /*
728          * If the resize inode feature isn't set, then
729          * s_reserved_gdt_blocks must be zero.
730          */
731         if (!ext2fs_has_feature_resize_inode(fs->super)) {
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                 }
740         }
741
742         /* Read the resize inode */
743         pctx.ino = EXT2_RESIZE_INO;
744         retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
745         if (retval) {
746                 if (ext2fs_has_feature_resize_inode(fs->super))
747                         ctx->flags |= E2F_FLAG_RESIZE_INODE;
748                 return;
749         }
750
751         /*
752          * If the resize inode feature isn't set, check to make sure
753          * the resize inode is cleared; then we're done.
754          */
755         if (!ext2fs_has_feature_resize_inode(fs->super)) {
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         }
768
769         /*
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         }
778         if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
779             !(inode.i_mode & LINUX_S_IFREG) ||
780             (blk < fs->super->s_first_data_block ||
781              blk >= ext2fs_blocks_count(fs->super))) {
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
799         retval = ext2fs_read_ind_block(fs, blk, dind_buf);
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;
805         if (fs->blocksize == 1024 && fs->super->s_first_data_block == 0)
806                 pblk++; /* Deal with 1024 blocksize bigalloc fs */
807         for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
808              i++, gdt_off++, pblk++) {
809                 gdt_off %= fs->blocksize/4;
810                 if (dind_buf[gdt_off] != pblk)
811                         goto resize_inode_invalid;
812                 retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
813                 if (retval)
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;
819                         expect = pblk + EXT2_GROUPS_TO_BLOCKS(fs->super, j);
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  }
831
832 /*
833  * This function checks the dirhash signed/unsigned hint if necessary.
834  */
835 static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
836 {
837         struct ext2_super_block *sb = ctx->fs->super;
838         struct problem_context pctx;
839         char    c;
840
841         if ((ctx->options & E2F_OPT_READONLY) ||
842             !ext2fs_has_feature_dir_index(sb) ||
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
860 void check_super_block(e2fsck_t ctx)
861 {
862         ext2_filsys fs = ctx->fs;
863         blk64_t first_block, last_block;
864         struct ext2_super_block *sb = fs->super;
865         unsigned int    ipg_max;
866         problem_t       problem;
867         blk64_t blocks_per_group = fs->super->s_blocks_per_group;
868         __u32   bpg_max, cpg_max;
869         __u64   blks_max;
870         int     inodes_per_block;
871         int     inode_size;
872         int     accept_time_fudge;
873         int     broken_system_clock;
874         dgrp_t  i;
875         blk64_t should_be;
876         struct problem_context  pctx;
877         blk64_t free_blocks = 0;
878         ext2_ino_t free_inodes = 0;
879         int     csum_flag, clear_test_fs_flag;
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);
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);
889         if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
890                 bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
891
892         ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
893                  sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
894         ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
895                  sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
896         ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
897                 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
898
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
908         clear_problem_context(&pctx);
909
910         /*
911          * Verify the super block constants...
912          */
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;
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);
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;
973                 }
974         }
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;
980         inode_size = EXT2_INODE_SIZE(sb);
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;
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);
990                 ctx->flags |= E2F_FLAG_ABORT;
991                 return;
992         }
993
994         if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) &&
995             (ctx->num_blocks < ext2fs_blocks_count(sb))) {
996                 pctx.blk = ext2fs_blocks_count(sb);
997                 pctx.blk2 = ctx->num_blocks;
998                 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
999                         ctx->flags |= E2F_FLAG_ABORT;
1000                         return;
1001                 }
1002         }
1003
1004         should_be = (sb->s_log_block_size == 0 &&
1005                      EXT2FS_CLUSTER_RATIO(fs) == 1) ? 1 : 0;
1006         if (sb->s_first_data_block != should_be) {
1007                 pctx.blk = sb->s_first_data_block;
1008                 pctx.blk2 = should_be;
1009                 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
1010                 ctx->flags |= E2F_FLAG_ABORT;
1011                 return;
1012         }
1013
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                     
1043         /* Are metadata_csum and uninit_bg both set? */
1044         if (ext2fs_has_feature_metadata_csum(fs->super) &&
1045             ext2fs_has_feature_gdt_csum(fs->super) &&
1046             fix_problem(ctx, PR_0_META_AND_GDT_CSUM_SET, &pctx)) {
1047                 ext2fs_clear_feature_gdt_csum(fs->super);
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
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
1062         /* Is 64bit set and extents unset? */
1063         if (ext2fs_has_feature_64bit(fs->super) &&
1064             !ext2fs_has_feature_extents(fs->super) &&
1065             fix_problem(ctx, PR_0_64BIT_WITHOUT_EXTENTS, &pctx)) {
1066                 ext2fs_set_feature_extents(fs->super);
1067                 ext2fs_mark_super_dirty(fs);
1068         }
1069
1070         /* Did user ask us to convert files to extents? */
1071         if (ctx->options & E2F_OPT_CONVERT_BMAP) {
1072                 ext2fs_set_feature_extents(fs->super);
1073                 ext2fs_mark_super_dirty(fs);
1074         }
1075
1076         if (ext2fs_has_feature_meta_bg(fs->super) &&
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)) {
1081                         ext2fs_clear_feature_meta_bg(fs->super);
1082                         fs->super->s_first_meta_bg = 0;
1083                         ext2fs_mark_super_dirty(fs);
1084                 }
1085         }
1086
1087         /*
1088          * Verify the group descriptors....
1089          */
1090         first_block = sb->s_first_data_block;
1091         last_block = ext2fs_blocks_count(sb)-1;
1092
1093         csum_flag = ext2fs_has_group_desc_csum(fs);
1094         for (i = 0; i < fs->group_desc_count; i++) {
1095                 pctx.group = i;
1096
1097                 if (!ext2fs_has_feature_flex_bg(fs->super)) {
1098                         first_block = ext2fs_group_first_block2(fs, i);
1099                         last_block = ext2fs_group_last_block2(fs, i);
1100                 }
1101
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);
1105                         if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
1106                                 ext2fs_block_bitmap_loc_set(fs, i, 0);
1107                 }
1108                 if (ext2fs_block_bitmap_loc(fs, i) == 0) {
1109                         ctx->invalid_block_bitmap_flag[i]++;
1110                         ctx->invalid_bitmaps++;
1111                 }
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);
1115                         if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
1116                                 ext2fs_inode_bitmap_loc_set(fs, i, 0);
1117                 }
1118                 if (ext2fs_inode_bitmap_loc(fs, i) == 0) {
1119                         ctx->invalid_inode_bitmap_flag[i]++;
1120                         ctx->invalid_bitmaps++;
1121                 }
1122                 if ((ext2fs_inode_table_loc(fs, i) < first_block) ||
1123                     ((ext2fs_inode_table_loc(fs, i) +
1124                       fs->inode_blocks_per_group - 1) > last_block)) {
1125                         pctx.blk = ext2fs_inode_table_loc(fs, i);
1126                         if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
1127                                 ext2fs_inode_table_loc_set(fs, i, 0);
1128                 }
1129                 if (ext2fs_inode_table_loc(fs, i) == 0) {
1130                         ctx->invalid_inode_table_flag[i]++;
1131                         ctx->invalid_bitmaps++;
1132                 }
1133                 free_blocks += ext2fs_bg_free_blocks_count(fs, i);
1134                 free_inodes += ext2fs_bg_free_inodes_count(fs, i);
1135
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))
1139                         ext2fs_unmark_valid(fs);
1140
1141                 should_be = 0;
1142                 if (!ext2fs_group_desc_csum_verify(fs, i)) {
1143                         pctx.csum1 = ext2fs_bg_checksum(fs, i);
1144                         pctx.csum2 = ext2fs_group_desc_csum(fs, i);
1145                         if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
1146                                 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
1147                                 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
1148                                 ext2fs_bg_itable_unused_set(fs, i, 0);
1149                                 should_be = 1;
1150                         }
1151                         ext2fs_unmark_valid(fs);
1152                 }
1153
1154                 if (!csum_flag &&
1155                     (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) ||
1156                      ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) ||
1157                      ext2fs_bg_itable_unused(fs, i) != 0)) {
1158                         if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) {
1159                                 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
1160                                 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
1161                                 ext2fs_bg_itable_unused_set(fs, i, 0);
1162                                 should_be = 1;
1163                         }
1164                         ext2fs_unmark_valid(fs);
1165                 }
1166
1167                 if (i == fs->group_desc_count - 1 &&
1168                     ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)) {
1169                         if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) {
1170                                 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
1171                                 should_be = 1;
1172                         }
1173                         ext2fs_unmark_valid(fs);
1174                 }
1175
1176                 if (csum_flag &&
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);
1180                         if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) {
1181                                 ext2fs_bg_itable_unused_set(fs, i, 0);
1182                                 should_be = 1;
1183                         }
1184                         ext2fs_unmark_valid(fs);
1185                 }
1186
1187                 if (should_be)
1188                         ext2fs_group_desc_csum_set(fs, i);
1189                 /* If the user aborts e2fsck by typing ^C, stop right away */
1190                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1191                         return;
1192         }
1193
1194         ctx->free_blocks = EXT2FS_C2B(fs, free_blocks);
1195         ctx->free_inodes = free_inodes;
1196
1197         if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) ||
1198             (sb->s_free_inodes_count > sb->s_inodes_count))
1199                 ext2fs_unmark_valid(fs);
1200
1201
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)) {
1207                 sb->s_state &= ~EXT2_VALID_FS;
1208                 ext2fs_mark_super_dirty(fs);
1209         }
1210
1211         clear_problem_context(&pctx);
1212
1213 #ifndef EXT2_SKIP_UUID
1214         /*
1215          * If the UUID field isn't assigned, assign it.
1216          * Skip if checksums are enabled and the filesystem is mounted,
1217          * if the id changes under the kernel remounting rw may fail.
1218          */
1219         if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid) &&
1220             !ext2fs_has_feature_metadata_csum(ctx->fs->super) &&
1221             (!csum_flag || !(ctx->mount_flags & EXT2_MF_MOUNTED))) {
1222                 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
1223                         uuid_generate(sb->s_uuid);
1224                         ext2fs_init_csum_seed(fs);
1225                         fs->flags |= EXT2_FLAG_DIRTY;
1226                         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1227                 }
1228         }
1229 #endif
1230
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;
1243                         fs->flags |= EXT2_FLAG_DIRTY;
1244                         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1245                 }
1246         }
1247                         
1248         /*
1249          * For the Hurd, check to see if the filetype option is set,
1250          * since it doesn't support it.
1251          */
1252         if (!(ctx->options & E2F_OPT_READONLY) &&
1253             fs->super->s_creator_os == EXT2_OS_HURD &&
1254             ext2fs_has_feature_filetype(fs->super)) {
1255                 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
1256                         ext2fs_clear_feature_filetype(fs->super);
1257                         ext2fs_mark_super_dirty(fs);
1258                         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1259                 }
1260         }
1261
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)) {
1274                 ext2fs_update_dynamic_rev(fs);
1275                 ext2fs_mark_super_dirty(fs);
1276                 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1277         }
1278
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;
1284                 ext2fs_mark_super_dirty(fs);
1285         }
1286
1287         /*
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.
1307          */
1308         profile_get_boolean(ctx->profile, "options", "accept_time_fudge",
1309                             0, 1, &accept_time_fudge);
1310         profile_get_boolean(ctx->profile, "options", "buggy_init_scripts",
1311                             0, accept_time_fudge, &accept_time_fudge);
1312         ctx->time_fudge = accept_time_fudge ? 86400 : 0;
1313
1314         profile_get_boolean(ctx->profile, "options", "broken_system_clock",
1315                             0, 0, &broken_system_clock);
1316
1317         /*
1318          * Check to see if the superblock last mount time or last
1319          * write time is in the future.
1320          */
1321         if (((ctx->options & E2F_OPT_FORCE) || fs->super->s_checkinterval) &&
1322             !broken_system_clock && !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
1323             (ext2fs_get_tstamp(fs->super, s_mtime) > ctx->now)) {
1324                 pctx.num = ext2fs_get_tstamp(fs->super, s_mtime);
1325                 problem = PR_0_FUTURE_SB_LAST_MOUNT;
1326                 if ((time_t) pctx.num <= ctx->now + ctx->time_fudge)
1327                         problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED;
1328                 if (fix_problem(ctx, problem, &pctx)) {
1329                         ext2fs_set_tstamp(fs->super, s_mtime, ctx->now);
1330                         fs->flags |= EXT2_FLAG_DIRTY;
1331                 }
1332         }
1333         if (((ctx->options & E2F_OPT_FORCE) || fs->super->s_checkinterval) &&
1334             !broken_system_clock && !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
1335             (ext2fs_get_tstamp(fs->super, s_wtime) > ctx->now)) {
1336                 pctx.num = ext2fs_get_tstamp(fs->super, s_wtime);
1337                 problem = PR_0_FUTURE_SB_LAST_WRITE;
1338                 if ((time_t) pctx.num <= ctx->now + ctx->time_fudge)
1339                         problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED;
1340                 if (fix_problem(ctx, problem, &pctx)) {
1341                         ext2fs_set_tstamp(fs->super, s_wtime, ctx->now);
1342                         fs->flags |= EXT2_FLAG_DIRTY;
1343                 }
1344         }
1345
1346         e2fsck_validate_quota_inodes(ctx);
1347
1348         /*
1349          * Move the ext3 journal file, if necessary.
1350          */
1351         e2fsck_move_ext3_journal(ctx);
1352
1353         /*
1354          * Fix journal hint, if necessary
1355          */
1356         e2fsck_fix_ext3_journal_hint(ctx);
1357
1358         /*
1359          * Add dirhash hint if necessary
1360          */
1361         e2fsck_fix_dirhash_hint(ctx);
1362
1363         /*
1364          * Hide quota inodes if necessary.
1365          */
1366         e2fsck_hide_quota(ctx);
1367
1368         return;
1369 }
1370
1371 /*
1372  * Check to see if we should backup the master sb to the backup super
1373  * blocks.  Returns non-zero if the sb should be backed up.
1374  */
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
1382  * future.  EXT3_FEATURE_INCOMPAT_RECOVER may also get set when
1383  * copying the primary superblock during online resize.
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| \
1391                                          EXT4_FEATURE_RO_COMPAT_DIR_NLINK| \
1392                                          EXT4_FEATURE_RO_COMPAT_ORPHAN_PRESENT)
1393 #define FEATURE_INCOMPAT_IGNORE         (EXT3_FEATURE_INCOMPAT_EXTENTS| \
1394                                          EXT3_FEATURE_INCOMPAT_RECOVER)
1395
1396 int check_backup_super_block(e2fsck_t ctx)
1397 {
1398         ext2_filsys     fs = ctx->fs;
1399         errcode_t       retval;
1400         dgrp_t          g;
1401         blk64_t         sb;
1402         int             ret = 0;
1403         char            buf[SUPERBLOCK_SIZE];
1404         struct ext2_super_block *backup_sb;
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
1424                 sb = ext2fs_group_first_block2(fs, g);
1425
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))
1439                         continue;
1440
1441 #define SUPER_INCOMPAT_DIFFERENT(x)     \
1442         ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) !=   \
1443          (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE))
1444 #define SUPER_RO_COMPAT_DIFFERENT(x)    \
1445         ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) !=  \
1446          (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE))
1447 #define SUPER_DIFFERENT(x)              \
1448         (fs->super->x != backup_sb->x)
1449
1450                 if (SUPER_DIFFERENT(s_feature_compat) ||
1451                     SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) ||
1452                     SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) ||
1453                     SUPER_DIFFERENT(s_blocks_count) ||
1454                     SUPER_DIFFERENT(s_blocks_count_hi) ||
1455                     SUPER_DIFFERENT(s_inodes_count) ||
1456                     memcmp(fs->super->s_uuid, backup_sb->s_uuid,
1457                            sizeof(fs->super->s_uuid)))
1458                         ret = 1;
1459                 break;
1460         }
1461         return ret;
1462 }