pass5.c revision 23799
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1980, 1986, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 3523675Speterstatic const char sccsid[] = "@(#)pass5.c 8.9 (Berkeley) 4/28/95"; 361558Srgrimes#endif /* not lint */ 371558Srgrimes 381558Srgrimes#include <sys/param.h> 391558Srgrimes#include <sys/time.h> 4023675Speter 411558Srgrimes#include <ufs/ufs/dinode.h> 421558Srgrimes#include <ufs/ffs/fs.h> 4323799Sbde 4423675Speter#include <err.h> 451558Srgrimes#include <string.h> 4623675Speter 471558Srgrimes#include "fsck.h" 481558Srgrimes 497585Sbdevoid 501558Srgrimespass5() 511558Srgrimes{ 5223675Speter int c, blk, frags, basesize, sumsize, mapsize, savednrpos; 5323675Speter struct fs *fs = &sblock; 5423675Speter struct cg *cg = &cgrp; 5523675Speter ufs_daddr_t dbase, dmax; 5623675Speter ufs_daddr_t d; 5723675Speter long i, j; 581558Srgrimes struct csum *cs; 591558Srgrimes struct csum cstotal; 601558Srgrimes struct inodesc idesc[3]; 611558Srgrimes char buf[MAXBSIZE]; 621558Srgrimes register struct cg *newcg = (struct cg *)buf; 631558Srgrimes struct ocg *ocg = (struct ocg *)buf; 641558Srgrimes 6523675Speter statemap[WINO] = USTATE; 6623675Speter memset(newcg, 0, (size_t)fs->fs_cgsize); 671558Srgrimes newcg->cg_niblk = fs->fs_ipg; 6823675Speter if (cvtlevel >= 3) { 691558Srgrimes if (fs->fs_maxcontig < 2 && fs->fs_contigsumsize > 0) { 701558Srgrimes if (preen) 711558Srgrimes pwarn("DELETING CLUSTERING MAPS\n"); 721558Srgrimes if (preen || reply("DELETE CLUSTERING MAPS")) { 731558Srgrimes fs->fs_contigsumsize = 0; 741558Srgrimes doinglevel1 = 1; 751558Srgrimes sbdirty(); 761558Srgrimes } 771558Srgrimes } 781558Srgrimes if (fs->fs_maxcontig > 1) { 791558Srgrimes char *doit = 0; 801558Srgrimes 811558Srgrimes if (fs->fs_contigsumsize < 1) { 821558Srgrimes doit = "CREAT"; 831558Srgrimes } else if (fs->fs_contigsumsize < fs->fs_maxcontig && 841558Srgrimes fs->fs_contigsumsize < FS_MAXCONTIG) { 851558Srgrimes doit = "EXPAND"; 861558Srgrimes } 871558Srgrimes if (doit) { 881558Srgrimes i = fs->fs_contigsumsize; 891558Srgrimes fs->fs_contigsumsize = 901558Srgrimes MIN(fs->fs_maxcontig, FS_MAXCONTIG); 911558Srgrimes if (CGSIZE(fs) > fs->fs_bsize) { 921558Srgrimes pwarn("CANNOT %s CLUSTER MAPS\n", doit); 931558Srgrimes fs->fs_contigsumsize = i; 941558Srgrimes } else if (preen || 951558Srgrimes reply("CREATE CLUSTER MAPS")) { 961558Srgrimes if (preen) 971558Srgrimes pwarn("%sING CLUSTER MAPS\n", 981558Srgrimes doit); 991558Srgrimes fs->fs_cgsize = 1001558Srgrimes fragroundup(fs, CGSIZE(fs)); 1011558Srgrimes doinglevel1 = 1; 1021558Srgrimes sbdirty(); 1031558Srgrimes } 1041558Srgrimes } 1051558Srgrimes } 1061558Srgrimes } 1071558Srgrimes switch ((int)fs->fs_postblformat) { 1081558Srgrimes 1091558Srgrimes case FS_42POSTBLFMT: 11023675Speter basesize = (char *)(&ocg->cg_btot[0]) - 11123675Speter (char *)(&ocg->cg_firstfield); 11223675Speter sumsize = &ocg->cg_iused[0] - (u_int8_t *)(&ocg->cg_btot[0]); 1131558Srgrimes mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] - 1141558Srgrimes (u_char *)&ocg->cg_iused[0]; 1151558Srgrimes ocg->cg_magic = CG_MAGIC; 1161558Srgrimes savednrpos = fs->fs_nrpos; 1171558Srgrimes fs->fs_nrpos = 8; 1181558Srgrimes break; 1191558Srgrimes 1201558Srgrimes case FS_DYNAMICPOSTBLFMT: 1211558Srgrimes newcg->cg_btotoff = 12223675Speter &newcg->cg_space[0] - (u_char *)(&newcg->cg_firstfield); 1231558Srgrimes newcg->cg_boff = 1241558Srgrimes newcg->cg_btotoff + fs->fs_cpg * sizeof(long); 1258871Srgrimes newcg->cg_iusedoff = newcg->cg_boff + 1261558Srgrimes fs->fs_cpg * fs->fs_nrpos * sizeof(short); 1271558Srgrimes newcg->cg_freeoff = 1281558Srgrimes newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY); 1291558Srgrimes if (fs->fs_contigsumsize <= 0) { 1301558Srgrimes newcg->cg_nextfreeoff = newcg->cg_freeoff + 1311558Srgrimes howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs), NBBY); 1321558Srgrimes } else { 1331558Srgrimes newcg->cg_clustersumoff = newcg->cg_freeoff + 1341558Srgrimes howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs), NBBY) - 1351558Srgrimes sizeof(long); 1361558Srgrimes newcg->cg_clustersumoff = 1371558Srgrimes roundup(newcg->cg_clustersumoff, sizeof(long)); 1381558Srgrimes newcg->cg_clusteroff = newcg->cg_clustersumoff + 1391558Srgrimes (fs->fs_contigsumsize + 1) * sizeof(long); 1401558Srgrimes newcg->cg_nextfreeoff = newcg->cg_clusteroff + 1411558Srgrimes howmany(fs->fs_cpg * fs->fs_spc / NSPB(fs), NBBY); 1421558Srgrimes } 1431558Srgrimes newcg->cg_magic = CG_MAGIC; 14423675Speter basesize = &newcg->cg_space[0] - 14523675Speter (u_char *)(&newcg->cg_firstfield); 1461558Srgrimes sumsize = newcg->cg_iusedoff - newcg->cg_btotoff; 1471558Srgrimes mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff; 1481558Srgrimes break; 1491558Srgrimes 1501558Srgrimes default: 15123675Speter sumsize = 0; /* keep lint happy */ 15223675Speter errx(EEXIT, "UNKNOWN ROTATIONAL TABLE FORMAT %d", 1531558Srgrimes fs->fs_postblformat); 1541558Srgrimes } 15523675Speter memset(&idesc[0], 0, sizeof idesc); 1561558Srgrimes for (i = 0; i < 3; i++) { 1571558Srgrimes idesc[i].id_type = ADDR; 1581558Srgrimes if (doinglevel2) 1591558Srgrimes idesc[i].id_fix = FIX; 1601558Srgrimes } 16123675Speter memset(&cstotal, 0, sizeof(struct csum)); 1621558Srgrimes j = blknum(fs, fs->fs_size + fs->fs_frag - 1); 1631558Srgrimes for (i = fs->fs_size; i < j; i++) 1641558Srgrimes setbmap(i); 1651558Srgrimes for (c = 0; c < fs->fs_ncg; c++) { 1661558Srgrimes getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize); 1671558Srgrimes if (!cg_chkmagic(cg)) 1681558Srgrimes pfatal("CG %d: BAD MAGIC NUMBER\n", c); 1691558Srgrimes dbase = cgbase(fs, c); 1701558Srgrimes dmax = dbase + fs->fs_fpg; 1711558Srgrimes if (dmax > fs->fs_size) 1721558Srgrimes dmax = fs->fs_size; 1731558Srgrimes newcg->cg_time = cg->cg_time; 1741558Srgrimes newcg->cg_cgx = c; 1751558Srgrimes if (c == fs->fs_ncg - 1) 1761558Srgrimes newcg->cg_ncyl = fs->fs_ncyl % fs->fs_cpg; 1771558Srgrimes else 1781558Srgrimes newcg->cg_ncyl = fs->fs_cpg; 1791558Srgrimes newcg->cg_ndblk = dmax - dbase; 1801558Srgrimes if (fs->fs_contigsumsize > 0) 1811558Srgrimes newcg->cg_nclusterblks = newcg->cg_ndblk / fs->fs_frag; 1821558Srgrimes newcg->cg_cs.cs_ndir = 0; 1831558Srgrimes newcg->cg_cs.cs_nffree = 0; 1841558Srgrimes newcg->cg_cs.cs_nbfree = 0; 1851558Srgrimes newcg->cg_cs.cs_nifree = fs->fs_ipg; 1861558Srgrimes if (cg->cg_rotor < newcg->cg_ndblk) 1871558Srgrimes newcg->cg_rotor = cg->cg_rotor; 1881558Srgrimes else 1891558Srgrimes newcg->cg_rotor = 0; 1901558Srgrimes if (cg->cg_frotor < newcg->cg_ndblk) 1911558Srgrimes newcg->cg_frotor = cg->cg_frotor; 1921558Srgrimes else 1931558Srgrimes newcg->cg_frotor = 0; 1941558Srgrimes if (cg->cg_irotor < newcg->cg_niblk) 1951558Srgrimes newcg->cg_irotor = cg->cg_irotor; 1961558Srgrimes else 1971558Srgrimes newcg->cg_irotor = 0; 19823675Speter memset(&newcg->cg_frsum[0], 0, sizeof newcg->cg_frsum); 19923675Speter memset(&cg_blktot(newcg)[0], 0, 2001558Srgrimes (size_t)(sumsize + mapsize)); 2011558Srgrimes if (fs->fs_postblformat == FS_42POSTBLFMT) 2021558Srgrimes ocg->cg_magic = CG_MAGIC; 2031558Srgrimes j = fs->fs_ipg * c; 2041558Srgrimes for (i = 0; i < fs->fs_ipg; j++, i++) { 2051558Srgrimes switch (statemap[j]) { 2061558Srgrimes 2071558Srgrimes case USTATE: 2081558Srgrimes break; 2091558Srgrimes 2101558Srgrimes case DSTATE: 2111558Srgrimes case DCLEAR: 2121558Srgrimes case DFOUND: 2131558Srgrimes newcg->cg_cs.cs_ndir++; 2141558Srgrimes /* fall through */ 2151558Srgrimes 2161558Srgrimes case FSTATE: 2171558Srgrimes case FCLEAR: 2181558Srgrimes newcg->cg_cs.cs_nifree--; 2191558Srgrimes setbit(cg_inosused(newcg), i); 2201558Srgrimes break; 2211558Srgrimes 2221558Srgrimes default: 2231558Srgrimes if (j < ROOTINO) 2241558Srgrimes break; 22523675Speter errx(EEXIT, "BAD STATE %d FOR INODE I=%d", 2261558Srgrimes statemap[j], j); 2271558Srgrimes } 2281558Srgrimes } 2291558Srgrimes if (c == 0) 2301558Srgrimes for (i = 0; i < ROOTINO; i++) { 2311558Srgrimes setbit(cg_inosused(newcg), i); 2321558Srgrimes newcg->cg_cs.cs_nifree--; 2331558Srgrimes } 2341558Srgrimes for (i = 0, d = dbase; 2351558Srgrimes d < dmax; 2361558Srgrimes d += fs->fs_frag, i += fs->fs_frag) { 2371558Srgrimes frags = 0; 2381558Srgrimes for (j = 0; j < fs->fs_frag; j++) { 2391558Srgrimes if (testbmap(d + j)) 2401558Srgrimes continue; 2411558Srgrimes setbit(cg_blksfree(newcg), i + j); 2421558Srgrimes frags++; 2431558Srgrimes } 2441558Srgrimes if (frags == fs->fs_frag) { 2451558Srgrimes newcg->cg_cs.cs_nbfree++; 2461558Srgrimes j = cbtocylno(fs, i); 2471558Srgrimes cg_blktot(newcg)[j]++; 2481558Srgrimes cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++; 2491558Srgrimes if (fs->fs_contigsumsize > 0) 2501558Srgrimes setbit(cg_clustersfree(newcg), 2511558Srgrimes i / fs->fs_frag); 2521558Srgrimes } else if (frags > 0) { 2531558Srgrimes newcg->cg_cs.cs_nffree += frags; 2541558Srgrimes blk = blkmap(fs, cg_blksfree(newcg), i); 2551558Srgrimes ffs_fragacct(fs, blk, newcg->cg_frsum, 1); 2561558Srgrimes } 2571558Srgrimes } 2581558Srgrimes if (fs->fs_contigsumsize > 0) { 25923675Speter int32_t *sump = cg_clustersum(newcg); 2601558Srgrimes u_char *mapp = cg_clustersfree(newcg); 2611558Srgrimes int map = *mapp++; 2621558Srgrimes int bit = 1; 2631558Srgrimes int run = 0; 2641558Srgrimes 2651558Srgrimes for (i = 0; i < newcg->cg_nclusterblks; i++) { 2661558Srgrimes if ((map & bit) != 0) { 2671558Srgrimes run++; 2681558Srgrimes } else if (run != 0) { 2691558Srgrimes if (run > fs->fs_contigsumsize) 2701558Srgrimes run = fs->fs_contigsumsize; 2711558Srgrimes sump[run]++; 2721558Srgrimes run = 0; 2731558Srgrimes } 2741558Srgrimes if ((i & (NBBY - 1)) != (NBBY - 1)) { 2751558Srgrimes bit <<= 1; 2761558Srgrimes } else { 2771558Srgrimes map = *mapp++; 2781558Srgrimes bit = 1; 2791558Srgrimes } 2801558Srgrimes } 2811558Srgrimes if (run != 0) { 2821558Srgrimes if (run > fs->fs_contigsumsize) 2831558Srgrimes run = fs->fs_contigsumsize; 2841558Srgrimes sump[run]++; 2851558Srgrimes } 2861558Srgrimes } 2871558Srgrimes cstotal.cs_nffree += newcg->cg_cs.cs_nffree; 2881558Srgrimes cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree; 2891558Srgrimes cstotal.cs_nifree += newcg->cg_cs.cs_nifree; 2901558Srgrimes cstotal.cs_ndir += newcg->cg_cs.cs_ndir; 2911558Srgrimes cs = &fs->fs_cs(fs, c); 29223675Speter if (memcmp(&newcg->cg_cs, cs, sizeof *cs) != 0 && 2931558Srgrimes dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { 29423675Speter memmove(cs, &newcg->cg_cs, sizeof *cs); 2951558Srgrimes sbdirty(); 2961558Srgrimes } 2971558Srgrimes if (doinglevel1) { 29823675Speter memmove(cg, newcg, (size_t)fs->fs_cgsize); 2991558Srgrimes cgdirty(); 3001558Srgrimes continue; 3011558Srgrimes } 30223675Speter if (memcmp(cg_inosused(newcg), 3031558Srgrimes cg_inosused(cg), mapsize) != 0 && 3041558Srgrimes dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) { 30523675Speter memmove(cg_inosused(cg), cg_inosused(newcg), 3061558Srgrimes (size_t)mapsize); 3071558Srgrimes cgdirty(); 3081558Srgrimes } 30923675Speter if ((memcmp(newcg, cg, basesize) != 0 || 31023675Speter memcmp(&cg_blktot(newcg)[0], 31123675Speter &cg_blktot(cg)[0], sumsize) != 0) && 3121558Srgrimes dofix(&idesc[2], "SUMMARY INFORMATION BAD")) { 31323675Speter memmove(cg, newcg, (size_t)basesize); 31423675Speter memmove(&cg_blktot(cg)[0], 31523675Speter &cg_blktot(newcg)[0], (size_t)sumsize); 3161558Srgrimes cgdirty(); 3171558Srgrimes } 3181558Srgrimes } 3191558Srgrimes if (fs->fs_postblformat == FS_42POSTBLFMT) 3201558Srgrimes fs->fs_nrpos = savednrpos; 32123675Speter if (memcmp(&cstotal, &fs->fs_cstotal, sizeof *cs) != 0 3221558Srgrimes && dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { 32323675Speter memmove(&fs->fs_cstotal, &cstotal, sizeof *cs); 3241558Srgrimes fs->fs_ronly = 0; 3251558Srgrimes sbdirty(); 3261558Srgrimes } 32719702Sjulian if (fs->fs_fmod != 0) { 32819702Sjulian pwarn("MODIFIED FLAG SET IN SUPERBLOCK"); 32919702Sjulian if (preen) 33019702Sjulian printf(" (FIXED)\n"); 33119702Sjulian if (preen || reply("FIX") == 1) { 33219702Sjulian fs->fs_fmod = 0; 33319702Sjulian sbdirty(); 33419702Sjulian } 33519702Sjulian } 3362179Sdg if (fs->fs_clean == 0) { 3376405Sdg pwarn("CLEAN FLAG NOT SET IN SUPERBLOCK"); 3382179Sdg if (preen) 3392179Sdg printf(" (FIXED)\n"); 3402179Sdg if (preen || reply("FIX") == 1) { 3412179Sdg fs->fs_clean = 1; 3422179Sdg sbdirty(); 3432179Sdg } 3442179Sdg } 3451558Srgrimes} 346