Home | History | Annotate | Line # | Download | only in fsck_ext2fs
pass5.c revision 1.5
      1 /*	$NetBSD: pass5.c,v 1.5 1998/03/01 02:20:25 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Manuel Bouyer.
      5  * Copyright (c) 1980, 1986, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)pass5.c	8.6 (Berkeley) 11/30/94";
     41 #else
     42 __RCSID("$NetBSD: pass5.c,v 1.5 1998/03/01 02:20:25 fvdl Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/time.h>
     48 #include <ufs/ufs/dinode.h>
     49 #include <ufs/ext2fs/ext2fs_dinode.h>
     50 #include <ufs/ext2fs/ext2fs.h>
     51 #include <ufs/ext2fs/ext2fs_extern.h>
     52 #include <string.h>
     53 #include <malloc.h>
     54 #include <stdio.h>
     55 
     56 #include "fsutil.h"
     57 #include "fsck.h"
     58 #include "extern.h"
     59 
     60 
     61 void print_bmap __P((u_char *,u_int32_t));
     62 
     63 void
     64 pass5()
     65 {
     66 	int c;
     67 	struct m_ext2fs *fs = &sblock;
     68 	daddr_t dbase, dmax;
     69 	daddr_t d;
     70 	long i, j;
     71 	struct inodesc idesc[3];
     72 	struct bufarea *ino_bitmap = NULL, *blk_bitmap = NULL;
     73 	char *ibmap, *bbmap;
     74 	u_int32_t cs_ndir, cs_nbfree, cs_nifree;
     75 	char msg[255];
     76 
     77 	cs_ndir = 0;
     78 	cs_nbfree = 0;
     79 	cs_nifree = 0;
     80 
     81 	ibmap = malloc(fs->e2fs_bsize);
     82 	bbmap = malloc(fs->e2fs_bsize);
     83 	if (ibmap == NULL || bbmap == NULL) {
     84 		errexit("out of memory\n");
     85 	}
     86 
     87 	for (c = 0; c < fs->e2fs_ncg; c++) {
     88 		u_int32_t nbfree = 0;
     89 		u_int32_t nifree = 0;
     90 		u_int32_t ndirs = 0;
     91 
     92 		nbfree = 0;
     93 		nifree = fs->e2fs.e2fs_ipg;
     94 		ndirs = 0;
     95 
     96 		if (blk_bitmap == NULL) {
     97 			blk_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
     98 				fs->e2fs_bsize);
     99 		} else {
    100 			getblk(blk_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
    101 				fs->e2fs_bsize);
    102 		}
    103 		if (ino_bitmap == NULL) {
    104 			ino_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
    105 				fs->e2fs_bsize);
    106 		} else {
    107 			getblk(ino_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
    108 				fs->e2fs_bsize);
    109 		}
    110 		memset(bbmap, 0, fs->e2fs_bsize);
    111 		memset(ibmap, 0, fs->e2fs_bsize);
    112 		memset(&idesc[0], 0, sizeof idesc);
    113 		for (i = 0; i < 3; i++) {
    114 			idesc[i].id_type = ADDR;
    115 		}
    116 
    117 		j = fs->e2fs.e2fs_ipg * c + 1;
    118 
    119 		for (i = 0; i < fs->e2fs.e2fs_ipg; j++, i++) {
    120 			if ((j < EXT2_FIRSTINO) && (j != EXT2_ROOTINO)) {
    121 				setbit(ibmap, i);
    122 				nifree--;
    123 				continue;
    124 			}
    125 			if (j > fs->e2fs.e2fs_icount) {
    126 				setbit(ibmap, i);
    127 				continue;
    128 			}
    129 			switch (statemap[j]) {
    130 
    131 			case USTATE:
    132 				break;
    133 
    134 			case DSTATE:
    135 			case DCLEAR:
    136 			case DFOUND:
    137 				ndirs++;
    138 				/* fall through */
    139 
    140 			case FSTATE:
    141 			case FCLEAR:
    142 				nifree--;
    143 				setbit(ibmap, i);
    144 				break;
    145 
    146 			default:
    147 				errexit("BAD STATE %d FOR INODE I=%ld\n",
    148 				    statemap[j], j);
    149 			}
    150 		}
    151 
    152 		/* fill in unused par of the inode map */
    153 		for (i = fs->e2fs.e2fs_ipg / NBBY; i < fs->e2fs_bsize; i++)
    154 			ibmap[i] = 0xff;
    155 
    156 		dbase = c * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock +
    157 				cgoverhead;
    158 		dmax = (c+1) * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock;
    159 
    160 		for (i = 0; i < cgoverhead; i++)
    161 			setbit(bbmap, i); /* blks allocated to fs metadata */
    162 		for (i = cgoverhead, d = dbase;
    163 		     d < dmax;
    164 		     d ++, i ++) {
    165 			if (testbmap(d) || d >= sblock.e2fs.e2fs_bcount) {
    166 				setbit(bbmap, i);
    167 				continue;
    168 			} else {
    169 				nbfree++;
    170 			}
    171 
    172 		}
    173 		cs_nbfree += nbfree;
    174 		cs_nifree += nifree;
    175 		cs_ndir += ndirs;
    176 
    177 		if (debug && (fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
    178 					  fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
    179 					  fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs)) {
    180 			printf("summary info for cg %d is %d, %d, %d,"
    181 					"should be %d, %d, %d\n", c,
    182 					fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree),
    183 					fs2h16(fs->e2fs_gd[c].ext2bgd_nifree),
    184 					fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs),
    185 					nbfree,
    186 					nifree,
    187 					ndirs);
    188 		}
    189 		snprintf(msg, 255, "SUMMARY INFORMATIONS WRONG FOR CG #%d", c);
    190 		if ((fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
    191 			fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
    192 			fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs) &&
    193 			dofix(&idesc[0], msg)) {
    194 			fs->e2fs_gd[c].ext2bgd_nbfree = h2fs16(nbfree);
    195 			fs->e2fs_gd[c].ext2bgd_nifree = h2fs16(nifree);
    196 			fs->e2fs_gd[c].ext2bgd_ndirs = h2fs16(ndirs);
    197 			sbdirty();
    198 		}
    199 
    200 		if (debug && memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize)) {
    201 			printf("blk_bitmap:\n");
    202 			print_bmap(blk_bitmap->b_un.b_buf, fs->e2fs_bsize);
    203 			printf("bbmap:\n");
    204 			print_bmap(bbmap, fs->e2fs_bsize);
    205 		}
    206 
    207 		snprintf(msg, 255, "BLK(S) MISSING IN BIT MAPS #%d", c);
    208 		if (memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize) &&
    209 			dofix(&idesc[1], msg)) {
    210 			memcpy(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize);
    211 			dirty(blk_bitmap);
    212 		}
    213 		if (debug && memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize)) {
    214 			printf("ino_bitmap:\n");
    215 			print_bmap(ino_bitmap->b_un.b_buf, fs->e2fs_bsize);
    216 			printf("ibmap:\n");
    217 			print_bmap(ibmap, fs->e2fs_bsize);
    218 		}
    219 		snprintf(msg, 255, "INODE(S) MISSING IN BIT MAPS #%d", c);
    220 		if (memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize) &&
    221 			dofix(&idesc[1], msg)) {
    222 			memcpy(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize);
    223 			dirty(ino_bitmap);
    224 		}
    225 
    226 	}
    227 	if (debug && (fs->e2fs.e2fs_fbcount != cs_nbfree ||
    228 		fs->e2fs.e2fs_ficount != cs_nifree)) {
    229 		printf("summary info bad in superblock: %d, %d should be %d, %d\n",
    230 		fs->e2fs.e2fs_fbcount, fs->e2fs.e2fs_ficount,
    231 		cs_nbfree, cs_nifree);
    232 	}
    233 	if ((fs->e2fs.e2fs_fbcount != cs_nbfree ||
    234 		fs->e2fs.e2fs_ficount != cs_nifree)
    235 	    && dofix(&idesc[0], "SUPERBLK SUMMARY INFORMATION BAD")) {
    236 		fs->e2fs.e2fs_fbcount = cs_nbfree;
    237 		fs->e2fs.e2fs_ficount = cs_nifree;
    238 		sbdirty();
    239 	}
    240 }
    241 
    242 void
    243 print_bmap(map, size)
    244 	u_char *map;
    245 	u_int32_t size;
    246 {
    247 	int i, j;
    248 
    249 	i = 0;
    250 	while (i < size) {
    251 		printf("%u: ",i);
    252 		for (j = 0; j < 16; j++, i++)
    253 			printf("%2x ", (u_int)map[i] & 0xff);
    254 		printf("\n");
    255 	}
    256 }
    257