1*84d9c625SLionel Sambuc /* $NetBSD: pass5.c,v 1.20 2012/08/26 09:33:18 dholland Exp $ */
294715d8eSBen Gras
394715d8eSBen Gras /*
494715d8eSBen Gras * Copyright (c) 1980, 1986, 1993
594715d8eSBen Gras * The Regents of the University of California. All rights reserved.
694715d8eSBen Gras *
794715d8eSBen Gras * Redistribution and use in source and binary forms, with or without
894715d8eSBen Gras * modification, are permitted provided that the following conditions
994715d8eSBen Gras * are met:
1094715d8eSBen Gras * 1. Redistributions of source code must retain the above copyright
1194715d8eSBen Gras * notice, this list of conditions and the following disclaimer.
1294715d8eSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
1394715d8eSBen Gras * notice, this list of conditions and the following disclaimer in the
1494715d8eSBen Gras * documentation and/or other materials provided with the distribution.
1594715d8eSBen Gras * 3. Neither the name of the University nor the names of its contributors
1694715d8eSBen Gras * may be used to endorse or promote products derived from this software
1794715d8eSBen Gras * without specific prior written permission.
1894715d8eSBen Gras *
1994715d8eSBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2094715d8eSBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2194715d8eSBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2294715d8eSBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2394715d8eSBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2494715d8eSBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2594715d8eSBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2694715d8eSBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2794715d8eSBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2894715d8eSBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2994715d8eSBen Gras * SUCH DAMAGE.
3094715d8eSBen Gras */
3194715d8eSBen Gras
3294715d8eSBen Gras /*
3394715d8eSBen Gras * Copyright (c) 1997 Manuel Bouyer.
3494715d8eSBen Gras *
3594715d8eSBen Gras * Redistribution and use in source and binary forms, with or without
3694715d8eSBen Gras * modification, are permitted provided that the following conditions
3794715d8eSBen Gras * are met:
3894715d8eSBen Gras * 1. Redistributions of source code must retain the above copyright
3994715d8eSBen Gras * notice, this list of conditions and the following disclaimer.
4094715d8eSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
4194715d8eSBen Gras * notice, this list of conditions and the following disclaimer in the
4294715d8eSBen Gras * documentation and/or other materials provided with the distribution.
4394715d8eSBen Gras *
4494715d8eSBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4594715d8eSBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4694715d8eSBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4794715d8eSBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
4894715d8eSBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4994715d8eSBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5094715d8eSBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5194715d8eSBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5294715d8eSBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
5394715d8eSBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5494715d8eSBen Gras */
5594715d8eSBen Gras
5694715d8eSBen Gras #include <sys/cdefs.h>
5794715d8eSBen Gras #ifndef lint
5894715d8eSBen Gras #if 0
5994715d8eSBen Gras static char sccsid[] = "@(#)pass5.c 8.6 (Berkeley) 11/30/94";
6094715d8eSBen Gras #else
61*84d9c625SLionel Sambuc __RCSID("$NetBSD: pass5.c,v 1.20 2012/08/26 09:33:18 dholland Exp $");
6294715d8eSBen Gras #endif
6394715d8eSBen Gras #endif /* not lint */
6494715d8eSBen Gras
6594715d8eSBen Gras #include <sys/param.h>
6694715d8eSBen Gras #include <sys/time.h>
6794715d8eSBen Gras #include <ufs/ufs/dinode.h>
6894715d8eSBen Gras #include <ufs/ext2fs/ext2fs_dinode.h>
6994715d8eSBen Gras #include <ufs/ext2fs/ext2fs.h>
7094715d8eSBen Gras #include <inttypes.h>
7194715d8eSBen Gras #include <string.h>
72*84d9c625SLionel Sambuc #include <stdlib.h>
7394715d8eSBen Gras #include <stdio.h>
7494715d8eSBen Gras
7594715d8eSBen Gras #include "fsutil.h"
7694715d8eSBen Gras #include "fsck.h"
7794715d8eSBen Gras #include "extern.h"
7894715d8eSBen Gras
7994715d8eSBen Gras
8094715d8eSBen Gras static void print_bmap(char *, uint32_t);
8194715d8eSBen Gras
8294715d8eSBen Gras void
pass5(void)8394715d8eSBen Gras pass5(void)
8494715d8eSBen Gras {
8594715d8eSBen Gras int c;
8694715d8eSBen Gras struct m_ext2fs *fs = &sblock;
8794715d8eSBen Gras daddr_t dbase, dmax;
8894715d8eSBen Gras daddr_t d;
8994715d8eSBen Gras uint32_t i, j;
9094715d8eSBen Gras struct inodesc idesc[3];
9194715d8eSBen Gras struct bufarea *ino_bitmap = NULL, *blk_bitmap = NULL;
9294715d8eSBen Gras char *ibmap, *bbmap;
9394715d8eSBen Gras u_int32_t cs_ndir, cs_nbfree, cs_nifree;
9494715d8eSBen Gras char msg[255];
9594715d8eSBen Gras
9694715d8eSBen Gras cs_ndir = 0;
9794715d8eSBen Gras cs_nbfree = 0;
9894715d8eSBen Gras cs_nifree = 0;
9994715d8eSBen Gras
10094715d8eSBen Gras ibmap = malloc(fs->e2fs_bsize);
10194715d8eSBen Gras bbmap = malloc(fs->e2fs_bsize);
10294715d8eSBen Gras if (ibmap == NULL || bbmap == NULL) {
10394715d8eSBen Gras errexit("out of memory");
10494715d8eSBen Gras }
10594715d8eSBen Gras
10694715d8eSBen Gras for (c = 0; c < fs->e2fs_ncg; c++) {
10794715d8eSBen Gras u_int32_t nbfree = 0;
10894715d8eSBen Gras u_int32_t nifree = 0;
10994715d8eSBen Gras u_int32_t ndirs = 0;
11094715d8eSBen Gras
11194715d8eSBen Gras nbfree = 0;
11294715d8eSBen Gras nifree = fs->e2fs.e2fs_ipg;
11394715d8eSBen Gras ndirs = 0;
11494715d8eSBen Gras
11594715d8eSBen Gras if (blk_bitmap == NULL) {
11694715d8eSBen Gras blk_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
11794715d8eSBen Gras fs->e2fs_bsize);
11894715d8eSBen Gras } else {
11994715d8eSBen Gras getblk(blk_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
12094715d8eSBen Gras fs->e2fs_bsize);
12194715d8eSBen Gras }
12294715d8eSBen Gras if (ino_bitmap == NULL) {
12394715d8eSBen Gras ino_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
12494715d8eSBen Gras fs->e2fs_bsize);
12594715d8eSBen Gras } else {
12694715d8eSBen Gras getblk(ino_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
12794715d8eSBen Gras fs->e2fs_bsize);
12894715d8eSBen Gras }
12994715d8eSBen Gras memset(bbmap, 0, fs->e2fs_bsize);
13094715d8eSBen Gras memset(ibmap, 0, fs->e2fs_bsize);
13194715d8eSBen Gras memset(&idesc[0], 0, sizeof idesc);
13294715d8eSBen Gras for (i = 0; i < 3; i++) {
13394715d8eSBen Gras idesc[i].id_type = ADDR;
13494715d8eSBen Gras }
13594715d8eSBen Gras
13694715d8eSBen Gras j = fs->e2fs.e2fs_ipg * c + 1;
13794715d8eSBen Gras
13894715d8eSBen Gras for (i = 0; i < fs->e2fs.e2fs_ipg; j++, i++) {
13994715d8eSBen Gras if ((j < EXT2_FIRSTINO) && (j != EXT2_ROOTINO)) {
14094715d8eSBen Gras setbit(ibmap, i);
14194715d8eSBen Gras nifree--;
14294715d8eSBen Gras continue;
14394715d8eSBen Gras }
14494715d8eSBen Gras if (j > fs->e2fs.e2fs_icount) {
14594715d8eSBen Gras setbit(ibmap, i);
14694715d8eSBen Gras continue;
14794715d8eSBen Gras }
14894715d8eSBen Gras switch (statemap[j]) {
14994715d8eSBen Gras
15094715d8eSBen Gras case USTATE:
15194715d8eSBen Gras break;
15294715d8eSBen Gras
15394715d8eSBen Gras case DSTATE:
15494715d8eSBen Gras case DCLEAR:
15594715d8eSBen Gras case DFOUND:
15694715d8eSBen Gras ndirs++;
15794715d8eSBen Gras /* fall through */
15894715d8eSBen Gras
15994715d8eSBen Gras case FSTATE:
16094715d8eSBen Gras case FCLEAR:
16194715d8eSBen Gras nifree--;
16294715d8eSBen Gras setbit(ibmap, i);
16394715d8eSBen Gras break;
16494715d8eSBen Gras
16594715d8eSBen Gras default:
16694715d8eSBen Gras errexit("BAD STATE %d FOR INODE I=%"PRIu32,
16794715d8eSBen Gras statemap[j], j);
16894715d8eSBen Gras }
16994715d8eSBen Gras }
17094715d8eSBen Gras
17194715d8eSBen Gras /* fill in unused par of the inode map */
17294715d8eSBen Gras for (i = fs->e2fs.e2fs_ipg / NBBY; i < (uint32_t)fs->e2fs_bsize; i++)
17394715d8eSBen Gras ibmap[i] = 0xff;
17494715d8eSBen Gras
17594715d8eSBen Gras dbase = c * sblock.e2fs.e2fs_bpg +
17694715d8eSBen Gras sblock.e2fs.e2fs_first_dblock;
17794715d8eSBen Gras dmax = (c+1) * sblock.e2fs.e2fs_bpg +
17894715d8eSBen Gras sblock.e2fs.e2fs_first_dblock;
17994715d8eSBen Gras
18094715d8eSBen Gras for (i = 0, d = dbase;
18194715d8eSBen Gras d < dmax;
18294715d8eSBen Gras d ++, i ++) {
18394715d8eSBen Gras if (testbmap(d) || d >= sblock.e2fs.e2fs_bcount) {
18494715d8eSBen Gras setbit(bbmap, i);
18594715d8eSBen Gras continue;
18694715d8eSBen Gras } else {
18794715d8eSBen Gras nbfree++;
18894715d8eSBen Gras }
18994715d8eSBen Gras
19094715d8eSBen Gras }
19194715d8eSBen Gras cs_nbfree += nbfree;
19294715d8eSBen Gras cs_nifree += nifree;
19394715d8eSBen Gras cs_ndir += ndirs;
19494715d8eSBen Gras
19594715d8eSBen Gras if (debug && (fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
19694715d8eSBen Gras fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
19794715d8eSBen Gras fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs)) {
19894715d8eSBen Gras printf("summary info for cg %d is %d, %d, %d,"
19994715d8eSBen Gras "should be %d, %d, %d\n", c,
20094715d8eSBen Gras fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree),
20194715d8eSBen Gras fs2h16(fs->e2fs_gd[c].ext2bgd_nifree),
20294715d8eSBen Gras fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs),
20394715d8eSBen Gras nbfree,
20494715d8eSBen Gras nifree,
20594715d8eSBen Gras ndirs);
20694715d8eSBen Gras }
20794715d8eSBen Gras (void)snprintf(msg, sizeof(msg),
20894715d8eSBen Gras "SUMMARY INFORMATIONS WRONG FOR CG #%d", c);
20994715d8eSBen Gras if ((fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
21094715d8eSBen Gras fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
21194715d8eSBen Gras fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs) &&
21294715d8eSBen Gras dofix(&idesc[0], msg)) {
21394715d8eSBen Gras fs->e2fs_gd[c].ext2bgd_nbfree = h2fs16(nbfree);
21494715d8eSBen Gras fs->e2fs_gd[c].ext2bgd_nifree = h2fs16(nifree);
21594715d8eSBen Gras fs->e2fs_gd[c].ext2bgd_ndirs = h2fs16(ndirs);
21694715d8eSBen Gras sbdirty();
21794715d8eSBen Gras }
21894715d8eSBen Gras
21994715d8eSBen Gras if (debug && memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize)) {
22094715d8eSBen Gras printf("blk_bitmap:\n");
22194715d8eSBen Gras print_bmap(blk_bitmap->b_un.b_buf, fs->e2fs_bsize);
22294715d8eSBen Gras printf("bbmap:\n");
22394715d8eSBen Gras print_bmap(bbmap, fs->e2fs_bsize);
22494715d8eSBen Gras }
22594715d8eSBen Gras
22694715d8eSBen Gras (void)snprintf(msg, sizeof(msg),
22794715d8eSBen Gras "BLK(S) MISSING IN BIT MAPS #%d", c);
22894715d8eSBen Gras if (memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize) &&
22994715d8eSBen Gras dofix(&idesc[1], msg)) {
23094715d8eSBen Gras memcpy(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize);
23194715d8eSBen Gras dirty(blk_bitmap);
23294715d8eSBen Gras }
23394715d8eSBen Gras if (debug && memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize)) {
23494715d8eSBen Gras printf("ino_bitmap:\n");
23594715d8eSBen Gras print_bmap(ino_bitmap->b_un.b_buf, fs->e2fs_bsize);
23694715d8eSBen Gras printf("ibmap:\n");
23794715d8eSBen Gras print_bmap(ibmap, fs->e2fs_bsize);
23894715d8eSBen Gras }
23994715d8eSBen Gras (void)snprintf(msg, sizeof(msg),
24094715d8eSBen Gras "INODE(S) MISSING IN BIT MAPS #%d", c);
24194715d8eSBen Gras if (memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize) &&
24294715d8eSBen Gras dofix(&idesc[1], msg)) {
24394715d8eSBen Gras memcpy(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize);
24494715d8eSBen Gras dirty(ino_bitmap);
24594715d8eSBen Gras }
24694715d8eSBen Gras
24794715d8eSBen Gras }
24894715d8eSBen Gras if (debug && (fs->e2fs.e2fs_fbcount != cs_nbfree ||
24994715d8eSBen Gras fs->e2fs.e2fs_ficount != cs_nifree)) {
25094715d8eSBen Gras printf("summary info bad in superblock: %d, %d should be %d, %d\n",
25194715d8eSBen Gras fs->e2fs.e2fs_fbcount, fs->e2fs.e2fs_ficount,
25294715d8eSBen Gras cs_nbfree, cs_nifree);
25394715d8eSBen Gras }
25494715d8eSBen Gras if ((fs->e2fs.e2fs_fbcount != cs_nbfree ||
25594715d8eSBen Gras fs->e2fs.e2fs_ficount != cs_nifree)
25694715d8eSBen Gras && dofix(&idesc[0], "SUPERBLK SUMMARY INFORMATION BAD")) {
25794715d8eSBen Gras fs->e2fs.e2fs_fbcount = cs_nbfree;
25894715d8eSBen Gras fs->e2fs.e2fs_ficount = cs_nifree;
25994715d8eSBen Gras sbdirty();
26094715d8eSBen Gras }
26194715d8eSBen Gras free(ibmap);
26294715d8eSBen Gras free(bbmap);
26394715d8eSBen Gras }
26494715d8eSBen Gras
26594715d8eSBen Gras static void
print_bmap(char * map,uint32_t size)26694715d8eSBen Gras print_bmap(char *map, uint32_t size)
26794715d8eSBen Gras {
26894715d8eSBen Gras uint32_t i, j;
26994715d8eSBen Gras
27094715d8eSBen Gras i = 0;
27194715d8eSBen Gras while (i < size) {
27294715d8eSBen Gras printf("%04x: ",i);
27394715d8eSBen Gras for (j = 0; j < 16; j++, i++)
27494715d8eSBen Gras printf("%02x ", (unsigned char)map[i] & 0xff);
27594715d8eSBen Gras printf("\n");
27694715d8eSBen Gras }
27794715d8eSBen Gras }
278