Home | History | Annotate | Line # | Download | only in fsck_ffs
pass5.c revision 1.12
      1   1.1      cgd /*
      2   1.6  mycroft  * Copyright (c) 1980, 1986, 1993
      3   1.6  mycroft  *	The Regents of the University of California.  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.12  mycroft /*static char sccsid[] = "from: @(#)pass5.c	8.6 (Berkeley) 11/30/94";*/
     36  1.12  mycroft static char *rcsid = "$Id: pass5.c,v 1.12 1994/12/28 00:03:54 mycroft Exp $";
     37   1.1      cgd #endif /* not lint */
     38   1.1      cgd 
     39   1.1      cgd #include <sys/param.h>
     40   1.5      cgd #include <sys/time.h>
     41   1.6  mycroft #include <ufs/ufs/dinode.h>
     42   1.6  mycroft #include <ufs/ffs/fs.h>
     43   1.1      cgd #include <string.h>
     44   1.1      cgd #include "fsck.h"
     45  1.10      cgd #include "extern.h"
     46   1.1      cgd 
     47  1.10      cgd void
     48   1.1      cgd pass5()
     49   1.1      cgd {
     50   1.1      cgd 	int c, blk, frags, basesize, sumsize, mapsize, savednrpos;
     51   1.1      cgd 	register struct fs *fs = &sblock;
     52   1.1      cgd 	register struct cg *cg = &cgrp;
     53   1.1      cgd 	daddr_t dbase, dmax;
     54   1.1      cgd 	register daddr_t d;
     55   1.1      cgd 	register long i, j;
     56   1.1      cgd 	struct csum *cs;
     57   1.1      cgd 	struct csum cstotal;
     58   1.1      cgd 	struct inodesc idesc[3];
     59   1.1      cgd 	char buf[MAXBSIZE];
     60   1.1      cgd 	register struct cg *newcg = (struct cg *)buf;
     61   1.1      cgd 	struct ocg *ocg = (struct ocg *)buf;
     62   1.1      cgd 
     63  1.12  mycroft 	statemap[WINO] = USTATE;
     64   1.7  mycroft 	memset(newcg, 0, (size_t)fs->fs_cgsize);
     65   1.1      cgd 	newcg->cg_niblk = fs->fs_ipg;
     66  1.12  mycroft 	if (cvtlevel >= 3) {
     67   1.6  mycroft 		if (fs->fs_maxcontig < 2 && fs->fs_contigsumsize > 0) {
     68   1.6  mycroft 			if (preen)
     69   1.6  mycroft 				pwarn("DELETING CLUSTERING MAPS\n");
     70   1.6  mycroft 			if (preen || reply("DELETE CLUSTERING MAPS")) {
     71   1.6  mycroft 				fs->fs_contigsumsize = 0;
     72   1.6  mycroft 				doinglevel1 = 1;
     73   1.6  mycroft 				sbdirty();
     74   1.6  mycroft 			}
     75   1.6  mycroft 		}
     76   1.6  mycroft 		if (fs->fs_maxcontig > 1) {
     77   1.6  mycroft 			char *doit = 0;
     78   1.6  mycroft 
     79   1.6  mycroft 			if (fs->fs_contigsumsize < 1) {
     80   1.6  mycroft 				doit = "CREAT";
     81   1.6  mycroft 			} else if (fs->fs_contigsumsize < fs->fs_maxcontig &&
     82   1.6  mycroft 				   fs->fs_contigsumsize < FS_MAXCONTIG) {
     83   1.6  mycroft 				doit = "EXPAND";
     84   1.6  mycroft 			}
     85   1.6  mycroft 			if (doit) {
     86   1.6  mycroft 				i = fs->fs_contigsumsize;
     87   1.6  mycroft 				fs->fs_contigsumsize =
     88   1.6  mycroft 				    MIN(fs->fs_maxcontig, FS_MAXCONTIG);
     89   1.6  mycroft 				if (CGSIZE(fs) > fs->fs_bsize) {
     90   1.6  mycroft 					pwarn("CANNOT %s CLUSTER MAPS\n", doit);
     91   1.6  mycroft 					fs->fs_contigsumsize = i;
     92   1.6  mycroft 				} else if (preen ||
     93   1.6  mycroft 				    reply("CREATE CLUSTER MAPS")) {
     94   1.6  mycroft 					if (preen)
     95   1.6  mycroft 						pwarn("%sING CLUSTER MAPS\n",
     96   1.6  mycroft 						    doit);
     97   1.6  mycroft 					fs->fs_cgsize =
     98   1.6  mycroft 					    fragroundup(fs, CGSIZE(fs));
     99   1.6  mycroft 					doinglevel1 = 1;
    100   1.6  mycroft 					sbdirty();
    101   1.6  mycroft 				}
    102   1.6  mycroft 			}
    103   1.6  mycroft 		}
    104   1.6  mycroft 	}
    105   1.1      cgd 	switch ((int)fs->fs_postblformat) {
    106   1.1      cgd 
    107   1.1      cgd 	case FS_42POSTBLFMT:
    108  1.11      cgd 		basesize = (char *)(&ocg->cg_btot[0]) -
    109  1.11      cgd 		    (char *)(&ocg->cg_firstfield);
    110  1.12  mycroft 		sumsize = &ocg->cg_iused[0] - (u_int8_t *)(&ocg->cg_btot[0]);
    111   1.1      cgd 		mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] -
    112   1.1      cgd 			(u_char *)&ocg->cg_iused[0];
    113   1.1      cgd 		ocg->cg_magic = CG_MAGIC;
    114   1.1      cgd 		savednrpos = fs->fs_nrpos;
    115   1.1      cgd 		fs->fs_nrpos = 8;
    116   1.1      cgd 		break;
    117   1.1      cgd 
    118   1.1      cgd 	case FS_DYNAMICPOSTBLFMT:
    119   1.1      cgd 		newcg->cg_btotoff =
    120  1.11      cgd 		     &newcg->cg_space[0] - (u_char *)(&newcg->cg_firstfield);
    121   1.1      cgd 		newcg->cg_boff =
    122   1.6  mycroft 		    newcg->cg_btotoff + fs->fs_cpg * sizeof(long);
    123   1.1      cgd 		newcg->cg_iusedoff = newcg->cg_boff +
    124   1.6  mycroft 		    fs->fs_cpg * fs->fs_nrpos * sizeof(short);
    125   1.1      cgd 		newcg->cg_freeoff =
    126   1.6  mycroft 		    newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY);
    127   1.6  mycroft 		if (fs->fs_contigsumsize <= 0) {
    128   1.6  mycroft 			newcg->cg_nextfreeoff = newcg->cg_freeoff +
    129   1.6  mycroft 			    howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs), NBBY);
    130   1.6  mycroft 		} else {
    131   1.6  mycroft 			newcg->cg_clustersumoff = newcg->cg_freeoff +
    132   1.6  mycroft 			    howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs), NBBY) -
    133   1.6  mycroft 			    sizeof(long);
    134   1.6  mycroft 			newcg->cg_clustersumoff =
    135   1.6  mycroft 			    roundup(newcg->cg_clustersumoff, sizeof(long));
    136   1.6  mycroft 			newcg->cg_clusteroff = newcg->cg_clustersumoff +
    137   1.6  mycroft 			    (fs->fs_contigsumsize + 1) * sizeof(long);
    138   1.6  mycroft 			newcg->cg_nextfreeoff = newcg->cg_clusteroff +
    139   1.6  mycroft 			    howmany(fs->fs_cpg * fs->fs_spc / NSPB(fs), NBBY);
    140   1.6  mycroft 		}
    141   1.1      cgd 		newcg->cg_magic = CG_MAGIC;
    142  1.11      cgd 		basesize = &newcg->cg_space[0] -
    143  1.11      cgd 		    (u_char *)(&newcg->cg_firstfield);
    144   1.1      cgd 		sumsize = newcg->cg_iusedoff - newcg->cg_btotoff;
    145   1.1      cgd 		mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
    146   1.1      cgd 		break;
    147   1.1      cgd 
    148   1.1      cgd 	default:
    149   1.1      cgd 		errexit("UNKNOWN ROTATIONAL TABLE FORMAT %d\n",
    150   1.1      cgd 			fs->fs_postblformat);
    151   1.1      cgd 	}
    152   1.7  mycroft 	memset(&idesc[0], 0, sizeof idesc);
    153   1.6  mycroft 	for (i = 0; i < 3; i++) {
    154   1.1      cgd 		idesc[i].id_type = ADDR;
    155   1.6  mycroft 		if (doinglevel2)
    156   1.6  mycroft 			idesc[i].id_fix = FIX;
    157   1.6  mycroft 	}
    158   1.7  mycroft 	memset(&cstotal, 0, sizeof(struct csum));
    159   1.1      cgd 	j = blknum(fs, fs->fs_size + fs->fs_frag - 1);
    160   1.1      cgd 	for (i = fs->fs_size; i < j; i++)
    161   1.1      cgd 		setbmap(i);
    162   1.1      cgd 	for (c = 0; c < fs->fs_ncg; c++) {
    163   1.1      cgd 		getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize);
    164   1.1      cgd 		if (!cg_chkmagic(cg))
    165   1.1      cgd 			pfatal("CG %d: BAD MAGIC NUMBER\n", c);
    166   1.1      cgd 		dbase = cgbase(fs, c);
    167   1.1      cgd 		dmax = dbase + fs->fs_fpg;
    168   1.1      cgd 		if (dmax > fs->fs_size)
    169   1.1      cgd 			dmax = fs->fs_size;
    170   1.6  mycroft 		newcg->cg_time = cg->cg_time;
    171   1.1      cgd 		newcg->cg_cgx = c;
    172   1.1      cgd 		if (c == fs->fs_ncg - 1)
    173   1.1      cgd 			newcg->cg_ncyl = fs->fs_ncyl % fs->fs_cpg;
    174   1.1      cgd 		else
    175   1.1      cgd 			newcg->cg_ncyl = fs->fs_cpg;
    176   1.1      cgd 		newcg->cg_ndblk = dmax - dbase;
    177   1.6  mycroft 		if (fs->fs_contigsumsize > 0)
    178   1.6  mycroft 			newcg->cg_nclusterblks = newcg->cg_ndblk / fs->fs_frag;
    179   1.1      cgd 		newcg->cg_cs.cs_ndir = 0;
    180   1.1      cgd 		newcg->cg_cs.cs_nffree = 0;
    181   1.1      cgd 		newcg->cg_cs.cs_nbfree = 0;
    182   1.1      cgd 		newcg->cg_cs.cs_nifree = fs->fs_ipg;
    183   1.1      cgd 		if (cg->cg_rotor < newcg->cg_ndblk)
    184   1.1      cgd 			newcg->cg_rotor = cg->cg_rotor;
    185   1.1      cgd 		else
    186   1.1      cgd 			newcg->cg_rotor = 0;
    187   1.1      cgd 		if (cg->cg_frotor < newcg->cg_ndblk)
    188   1.1      cgd 			newcg->cg_frotor = cg->cg_frotor;
    189   1.1      cgd 		else
    190   1.1      cgd 			newcg->cg_frotor = 0;
    191   1.1      cgd 		if (cg->cg_irotor < newcg->cg_niblk)
    192   1.1      cgd 			newcg->cg_irotor = cg->cg_irotor;
    193   1.1      cgd 		else
    194   1.1      cgd 			newcg->cg_irotor = 0;
    195   1.7  mycroft 		memset(&newcg->cg_frsum[0], 0, sizeof newcg->cg_frsum);
    196   1.7  mycroft 		memset(&cg_blktot(newcg)[0], 0,
    197   1.1      cgd 		      (size_t)(sumsize + mapsize));
    198   1.1      cgd 		if (fs->fs_postblformat == FS_42POSTBLFMT)
    199   1.1      cgd 			ocg->cg_magic = CG_MAGIC;
    200   1.1      cgd 		j = fs->fs_ipg * c;
    201   1.1      cgd 		for (i = 0; i < fs->fs_ipg; j++, i++) {
    202   1.1      cgd 			switch (statemap[j]) {
    203   1.1      cgd 
    204   1.1      cgd 			case USTATE:
    205   1.1      cgd 				break;
    206   1.1      cgd 
    207   1.1      cgd 			case DSTATE:
    208   1.1      cgd 			case DCLEAR:
    209   1.1      cgd 			case DFOUND:
    210   1.1      cgd 				newcg->cg_cs.cs_ndir++;
    211   1.1      cgd 				/* fall through */
    212   1.1      cgd 
    213   1.1      cgd 			case FSTATE:
    214   1.1      cgd 			case FCLEAR:
    215   1.1      cgd 				newcg->cg_cs.cs_nifree--;
    216   1.1      cgd 				setbit(cg_inosused(newcg), i);
    217   1.1      cgd 				break;
    218   1.1      cgd 
    219   1.1      cgd 			default:
    220   1.1      cgd 				if (j < ROOTINO)
    221   1.1      cgd 					break;
    222   1.1      cgd 				errexit("BAD STATE %d FOR INODE I=%d",
    223   1.1      cgd 				    statemap[j], j);
    224   1.1      cgd 			}
    225   1.1      cgd 		}
    226   1.1      cgd 		if (c == 0)
    227   1.1      cgd 			for (i = 0; i < ROOTINO; i++) {
    228   1.1      cgd 				setbit(cg_inosused(newcg), i);
    229   1.1      cgd 				newcg->cg_cs.cs_nifree--;
    230   1.1      cgd 			}
    231   1.1      cgd 		for (i = 0, d = dbase;
    232   1.1      cgd 		     d < dmax;
    233   1.1      cgd 		     d += fs->fs_frag, i += fs->fs_frag) {
    234   1.1      cgd 			frags = 0;
    235   1.1      cgd 			for (j = 0; j < fs->fs_frag; j++) {
    236   1.1      cgd 				if (testbmap(d + j))
    237   1.1      cgd 					continue;
    238   1.1      cgd 				setbit(cg_blksfree(newcg), i + j);
    239   1.1      cgd 				frags++;
    240   1.1      cgd 			}
    241   1.1      cgd 			if (frags == fs->fs_frag) {
    242   1.1      cgd 				newcg->cg_cs.cs_nbfree++;
    243   1.1      cgd 				j = cbtocylno(fs, i);
    244   1.1      cgd 				cg_blktot(newcg)[j]++;
    245   1.1      cgd 				cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++;
    246   1.6  mycroft 				if (fs->fs_contigsumsize > 0)
    247   1.6  mycroft 					setbit(cg_clustersfree(newcg),
    248   1.6  mycroft 					    i / fs->fs_frag);
    249   1.1      cgd 			} else if (frags > 0) {
    250   1.1      cgd 				newcg->cg_cs.cs_nffree += frags;
    251   1.1      cgd 				blk = blkmap(fs, cg_blksfree(newcg), i);
    252   1.6  mycroft 				ffs_fragacct(fs, blk, newcg->cg_frsum, 1);
    253   1.6  mycroft 			}
    254   1.6  mycroft 		}
    255   1.6  mycroft 		if (fs->fs_contigsumsize > 0) {
    256   1.9  mycroft 			int32_t *sump = cg_clustersum(newcg);
    257   1.6  mycroft 			u_char *mapp = cg_clustersfree(newcg);
    258   1.6  mycroft 			int map = *mapp++;
    259   1.6  mycroft 			int bit = 1;
    260   1.6  mycroft 			int run = 0;
    261   1.6  mycroft 
    262   1.6  mycroft 			for (i = 0; i < newcg->cg_nclusterblks; i++) {
    263   1.6  mycroft 				if ((map & bit) != 0) {
    264   1.6  mycroft 					run++;
    265   1.6  mycroft 				} else if (run != 0) {
    266   1.6  mycroft 					if (run > fs->fs_contigsumsize)
    267   1.6  mycroft 						run = fs->fs_contigsumsize;
    268   1.6  mycroft 					sump[run]++;
    269   1.6  mycroft 					run = 0;
    270   1.6  mycroft 				}
    271   1.6  mycroft 				if ((i & (NBBY - 1)) != (NBBY - 1)) {
    272   1.6  mycroft 					bit <<= 1;
    273   1.6  mycroft 				} else {
    274   1.6  mycroft 					map = *mapp++;
    275   1.6  mycroft 					bit = 1;
    276   1.6  mycroft 				}
    277   1.6  mycroft 			}
    278   1.6  mycroft 			if (run != 0) {
    279   1.6  mycroft 				if (run > fs->fs_contigsumsize)
    280   1.6  mycroft 					run = fs->fs_contigsumsize;
    281   1.6  mycroft 				sump[run]++;
    282   1.1      cgd 			}
    283   1.1      cgd 		}
    284   1.1      cgd 		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
    285   1.1      cgd 		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
    286   1.1      cgd 		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
    287   1.1      cgd 		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
    288   1.1      cgd 		cs = &fs->fs_cs(fs, c);
    289   1.8  mycroft 		if (memcmp(&newcg->cg_cs, cs, sizeof *cs) != 0 &&
    290   1.1      cgd 		    dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
    291   1.7  mycroft 			memcpy(cs, &newcg->cg_cs, sizeof *cs);
    292   1.1      cgd 			sbdirty();
    293   1.1      cgd 		}
    294   1.6  mycroft 		if (doinglevel1) {
    295   1.7  mycroft 			memcpy(cg, newcg, (size_t)fs->fs_cgsize);
    296   1.1      cgd 			cgdirty();
    297   1.1      cgd 			continue;
    298   1.1      cgd 		}
    299   1.8  mycroft 		if (memcmp(cg_inosused(newcg),
    300   1.8  mycroft 			   cg_inosused(cg), mapsize) != 0 &&
    301   1.1      cgd 		    dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
    302   1.7  mycroft 			memcpy(cg_inosused(cg), cg_inosused(newcg),
    303   1.1      cgd 			      (size_t)mapsize);
    304   1.1      cgd 			cgdirty();
    305   1.1      cgd 		}
    306   1.8  mycroft 		if ((memcmp(newcg, cg, basesize) != 0 ||
    307   1.8  mycroft 		     memcmp(&cg_blktot(newcg)[0],
    308   1.8  mycroft 			    &cg_blktot(cg)[0], sumsize) != 0) &&
    309   1.1      cgd 		    dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
    310   1.7  mycroft 			memcpy(cg, newcg, (size_t)basesize);
    311   1.7  mycroft 			memcpy(&cg_blktot(cg)[0],
    312   1.7  mycroft 			       &cg_blktot(newcg)[0], (size_t)sumsize);
    313   1.1      cgd 			cgdirty();
    314   1.1      cgd 		}
    315   1.1      cgd 	}
    316   1.1      cgd 	if (fs->fs_postblformat == FS_42POSTBLFMT)
    317   1.1      cgd 		fs->fs_nrpos = savednrpos;
    318   1.8  mycroft 	if (memcmp(&cstotal, &fs->fs_cstotal, sizeof *cs) != 0
    319   1.1      cgd 	    && dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
    320   1.7  mycroft 		memcpy(&fs->fs_cstotal, &cstotal, sizeof *cs);
    321   1.1      cgd 		fs->fs_ronly = 0;
    322   1.1      cgd 		fs->fs_fmod = 0;
    323   1.1      cgd 		sbdirty();
    324   1.1      cgd 	}
    325   1.1      cgd }
    326