Home | History | Annotate | Line # | Download | only in fsck_ffs
pass5.c revision 1.4
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1980, 1986 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.4  mycroft /*static char sccsid[] = "from: @(#)pass5.c	5.13 (Berkeley) 7/20/90";*/
     36  1.4  mycroft static char rcsid[] = "$Id: pass5.c,v 1.4 1993/08/01 18:27:15 mycroft Exp $";
     37  1.1      cgd #endif /* not lint */
     38  1.1      cgd 
     39  1.1      cgd #include <sys/param.h>
     40  1.1      cgd #include <ufs/dinode.h>
     41  1.1      cgd #include <ufs/fs.h>
     42  1.1      cgd #include <string.h>
     43  1.1      cgd #include "fsck.h"
     44  1.1      cgd 
     45  1.1      cgd pass5()
     46  1.1      cgd {
     47  1.1      cgd 	int c, blk, frags, basesize, sumsize, mapsize, savednrpos;
     48  1.1      cgd 	register struct fs *fs = &sblock;
     49  1.1      cgd 	register struct cg *cg = &cgrp;
     50  1.1      cgd 	daddr_t dbase, dmax;
     51  1.1      cgd 	register daddr_t d;
     52  1.1      cgd 	register long i, j;
     53  1.1      cgd 	struct csum *cs;
     54  1.1      cgd 	time_t now;
     55  1.1      cgd 	struct csum cstotal;
     56  1.1      cgd 	struct inodesc idesc[3];
     57  1.1      cgd 	char buf[MAXBSIZE];
     58  1.1      cgd 	register struct cg *newcg = (struct cg *)buf;
     59  1.1      cgd 	struct ocg *ocg = (struct ocg *)buf;
     60  1.1      cgd 
     61  1.1      cgd 	bzero((char *)newcg, (size_t)fs->fs_cgsize);
     62  1.1      cgd 	newcg->cg_niblk = fs->fs_ipg;
     63  1.1      cgd 	switch ((int)fs->fs_postblformat) {
     64  1.1      cgd 
     65  1.1      cgd 	case FS_42POSTBLFMT:
     66  1.1      cgd 		basesize = (char *)(&ocg->cg_btot[0]) - (char *)(&ocg->cg_link);
     67  1.1      cgd 		sumsize = &ocg->cg_iused[0] - (char *)(&ocg->cg_btot[0]);
     68  1.1      cgd 		mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] -
     69  1.1      cgd 			(u_char *)&ocg->cg_iused[0];
     70  1.1      cgd 		ocg->cg_magic = CG_MAGIC;
     71  1.1      cgd 		savednrpos = fs->fs_nrpos;
     72  1.1      cgd 		fs->fs_nrpos = 8;
     73  1.1      cgd 		break;
     74  1.1      cgd 
     75  1.1      cgd 	case FS_DYNAMICPOSTBLFMT:
     76  1.1      cgd 		newcg->cg_btotoff =
     77  1.1      cgd 		 	&newcg->cg_space[0] - (u_char *)(&newcg->cg_link);
     78  1.1      cgd 		newcg->cg_boff =
     79  1.1      cgd 			newcg->cg_btotoff + fs->fs_cpg * sizeof(long);
     80  1.1      cgd 		newcg->cg_iusedoff = newcg->cg_boff +
     81  1.1      cgd 			fs->fs_cpg * fs->fs_nrpos * sizeof(short);
     82  1.1      cgd 		newcg->cg_freeoff =
     83  1.1      cgd 			newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY);
     84  1.1      cgd 		newcg->cg_nextfreeoff = newcg->cg_freeoff +
     85  1.1      cgd 			howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs),
     86  1.1      cgd 				NBBY);
     87  1.1      cgd 		newcg->cg_magic = CG_MAGIC;
     88  1.1      cgd 		basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_link);
     89  1.1      cgd 		sumsize = newcg->cg_iusedoff - newcg->cg_btotoff;
     90  1.1      cgd 		mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
     91  1.1      cgd 		break;
     92  1.1      cgd 
     93  1.1      cgd 	default:
     94  1.1      cgd 		errexit("UNKNOWN ROTATIONAL TABLE FORMAT %d\n",
     95  1.1      cgd 			fs->fs_postblformat);
     96  1.1      cgd 	}
     97  1.1      cgd 	bzero((char *)&idesc[0], sizeof idesc);
     98  1.1      cgd 	for (i = 0; i < 3; i++)
     99  1.1      cgd 		idesc[i].id_type = ADDR;
    100  1.1      cgd 	bzero((char *)&cstotal, sizeof(struct csum));
    101  1.1      cgd 	(void)time(&now);
    102  1.1      cgd 	j = blknum(fs, fs->fs_size + fs->fs_frag - 1);
    103  1.1      cgd 	for (i = fs->fs_size; i < j; i++)
    104  1.1      cgd 		setbmap(i);
    105  1.1      cgd 	for (c = 0; c < fs->fs_ncg; c++) {
    106  1.1      cgd 		getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize);
    107  1.1      cgd 		if (!cg_chkmagic(cg))
    108  1.1      cgd 			pfatal("CG %d: BAD MAGIC NUMBER\n", c);
    109  1.1      cgd 		dbase = cgbase(fs, c);
    110  1.1      cgd 		dmax = dbase + fs->fs_fpg;
    111  1.1      cgd 		if (dmax > fs->fs_size)
    112  1.1      cgd 			dmax = fs->fs_size;
    113  1.1      cgd 		if (now > cg->cg_time)
    114  1.1      cgd 			newcg->cg_time = cg->cg_time;
    115  1.1      cgd 		else
    116  1.1      cgd 			newcg->cg_time = now;
    117  1.1      cgd 		newcg->cg_cgx = c;
    118  1.1      cgd 		if (c == fs->fs_ncg - 1)
    119  1.1      cgd 			newcg->cg_ncyl = fs->fs_ncyl % fs->fs_cpg;
    120  1.1      cgd 		else
    121  1.1      cgd 			newcg->cg_ncyl = fs->fs_cpg;
    122  1.1      cgd 		newcg->cg_ndblk = dmax - dbase;
    123  1.1      cgd 		newcg->cg_cs.cs_ndir = 0;
    124  1.1      cgd 		newcg->cg_cs.cs_nffree = 0;
    125  1.1      cgd 		newcg->cg_cs.cs_nbfree = 0;
    126  1.1      cgd 		newcg->cg_cs.cs_nifree = fs->fs_ipg;
    127  1.1      cgd 		if (cg->cg_rotor < newcg->cg_ndblk)
    128  1.1      cgd 			newcg->cg_rotor = cg->cg_rotor;
    129  1.1      cgd 		else
    130  1.1      cgd 			newcg->cg_rotor = 0;
    131  1.1      cgd 		if (cg->cg_frotor < newcg->cg_ndblk)
    132  1.1      cgd 			newcg->cg_frotor = cg->cg_frotor;
    133  1.1      cgd 		else
    134  1.1      cgd 			newcg->cg_frotor = 0;
    135  1.1      cgd 		if (cg->cg_irotor < newcg->cg_niblk)
    136  1.1      cgd 			newcg->cg_irotor = cg->cg_irotor;
    137  1.1      cgd 		else
    138  1.1      cgd 			newcg->cg_irotor = 0;
    139  1.1      cgd 		bzero((char *)&newcg->cg_frsum[0], sizeof newcg->cg_frsum);
    140  1.1      cgd 		bzero((char *)&cg_blktot(newcg)[0],
    141  1.1      cgd 		      (size_t)(sumsize + mapsize));
    142  1.1      cgd 		if (fs->fs_postblformat == FS_42POSTBLFMT)
    143  1.1      cgd 			ocg->cg_magic = CG_MAGIC;
    144  1.1      cgd 		j = fs->fs_ipg * c;
    145  1.1      cgd 		for (i = 0; i < fs->fs_ipg; j++, i++) {
    146  1.1      cgd 			switch (statemap[j]) {
    147  1.1      cgd 
    148  1.1      cgd 			case USTATE:
    149  1.1      cgd 				break;
    150  1.1      cgd 
    151  1.1      cgd 			case DSTATE:
    152  1.1      cgd 			case DCLEAR:
    153  1.1      cgd 			case DFOUND:
    154  1.1      cgd 				newcg->cg_cs.cs_ndir++;
    155  1.1      cgd 				/* fall through */
    156  1.1      cgd 
    157  1.1      cgd 			case FSTATE:
    158  1.1      cgd 			case FCLEAR:
    159  1.1      cgd 				newcg->cg_cs.cs_nifree--;
    160  1.1      cgd 				setbit(cg_inosused(newcg), i);
    161  1.1      cgd 				break;
    162  1.1      cgd 
    163  1.1      cgd 			default:
    164  1.1      cgd 				if (j < ROOTINO)
    165  1.1      cgd 					break;
    166  1.1      cgd 				errexit("BAD STATE %d FOR INODE I=%d",
    167  1.1      cgd 				    statemap[j], j);
    168  1.1      cgd 			}
    169  1.1      cgd 		}
    170  1.1      cgd 		if (c == 0)
    171  1.1      cgd 			for (i = 0; i < ROOTINO; i++) {
    172  1.1      cgd 				setbit(cg_inosused(newcg), i);
    173  1.1      cgd 				newcg->cg_cs.cs_nifree--;
    174  1.1      cgd 			}
    175  1.1      cgd 		for (i = 0, d = dbase;
    176  1.1      cgd 		     d < dmax;
    177  1.1      cgd 		     d += fs->fs_frag, i += fs->fs_frag) {
    178  1.1      cgd 			frags = 0;
    179  1.1      cgd 			for (j = 0; j < fs->fs_frag; j++) {
    180  1.1      cgd 				if (testbmap(d + j))
    181  1.1      cgd 					continue;
    182  1.1      cgd 				setbit(cg_blksfree(newcg), i + j);
    183  1.1      cgd 				frags++;
    184  1.1      cgd 			}
    185  1.1      cgd 			if (frags == fs->fs_frag) {
    186  1.1      cgd 				newcg->cg_cs.cs_nbfree++;
    187  1.1      cgd 				j = cbtocylno(fs, i);
    188  1.1      cgd 				cg_blktot(newcg)[j]++;
    189  1.1      cgd 				cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++;
    190  1.1      cgd 			} else if (frags > 0) {
    191  1.1      cgd 				newcg->cg_cs.cs_nffree += frags;
    192  1.1      cgd 				blk = blkmap(fs, cg_blksfree(newcg), i);
    193  1.1      cgd 				fragacct(fs, blk, newcg->cg_frsum, 1);
    194  1.1      cgd 			}
    195  1.1      cgd 		}
    196  1.1      cgd 		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
    197  1.1      cgd 		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
    198  1.1      cgd 		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
    199  1.1      cgd 		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
    200  1.1      cgd 		cs = &fs->fs_cs(fs, c);
    201  1.1      cgd 		if (bcmp((char *)&newcg->cg_cs, (char *)cs, sizeof *cs) != 0 &&
    202  1.1      cgd 		    dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
    203  1.1      cgd 			bcopy((char *)&newcg->cg_cs, (char *)cs, sizeof *cs);
    204  1.1      cgd 			sbdirty();
    205  1.1      cgd 		}
    206  1.1      cgd 		if (cvtflag) {
    207  1.1      cgd 			bcopy((char *)newcg, (char *)cg, (size_t)fs->fs_cgsize);
    208  1.1      cgd 			cgdirty();
    209  1.1      cgd 			continue;
    210  1.1      cgd 		}
    211  1.1      cgd 		if (bcmp(cg_inosused(newcg),
    212  1.1      cgd 			 cg_inosused(cg), mapsize) != 0 &&
    213  1.1      cgd 		    dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
    214  1.1      cgd 			bcopy(cg_inosused(newcg), cg_inosused(cg),
    215  1.1      cgd 			      (size_t)mapsize);
    216  1.1      cgd 			cgdirty();
    217  1.1      cgd 		}
    218  1.1      cgd 		if ((bcmp((char *)newcg, (char *)cg, basesize) != 0 ||
    219  1.1      cgd 		     bcmp((char *)&cg_blktot(newcg)[0],
    220  1.1      cgd 			  (char *)&cg_blktot(cg)[0], sumsize) != 0) &&
    221  1.1      cgd 		    dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
    222  1.1      cgd 			bcopy((char *)newcg, (char *)cg, (size_t)basesize);
    223  1.1      cgd 			bcopy((char *)&cg_blktot(newcg)[0],
    224  1.1      cgd 			      (char *)&cg_blktot(cg)[0], (size_t)sumsize);
    225  1.1      cgd 			cgdirty();
    226  1.1      cgd 		}
    227  1.1      cgd 	}
    228  1.1      cgd 	if (fs->fs_postblformat == FS_42POSTBLFMT)
    229  1.1      cgd 		fs->fs_nrpos = savednrpos;
    230  1.1      cgd 	if (bcmp((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs) != 0
    231  1.1      cgd 	    && dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
    232  1.1      cgd 		bcopy((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs);
    233  1.1      cgd 		fs->fs_ronly = 0;
    234  1.1      cgd 		fs->fs_fmod = 0;
    235  1.1      cgd 		sbdirty();
    236  1.1      cgd 	}
    237  1.1      cgd }
    238