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