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