1 /* 2 * Copyright (c) 1980, 1986 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)pass5.c 5.17 (Berkeley) 07/02/92"; 10 #endif /* not lint */ 11 12 #include <sys/param.h> 13 #include <sys/time.h> 14 #include <ufs/ufs/dinode.h> 15 #include <ufs/ffs/fs.h> 16 #include <string.h> 17 #include "fsck.h" 18 19 pass5() 20 { 21 int c, blk, frags, basesize, sumsize, mapsize, savednrpos; 22 register struct fs *fs = &sblock; 23 register struct cg *cg = &cgrp; 24 daddr_t dbase, dmax; 25 register daddr_t d; 26 register long i, j; 27 struct csum *cs; 28 time_t now; 29 struct csum cstotal; 30 struct inodesc idesc[3]; 31 char buf[MAXBSIZE]; 32 register struct cg *newcg = (struct cg *)buf; 33 struct ocg *ocg = (struct ocg *)buf; 34 35 bzero((char *)newcg, (size_t)fs->fs_cgsize); 36 newcg->cg_niblk = fs->fs_ipg; 37 switch ((int)fs->fs_postblformat) { 38 39 case FS_42POSTBLFMT: 40 basesize = (char *)(&ocg->cg_btot[0]) - (char *)(&ocg->cg_link); 41 sumsize = &ocg->cg_iused[0] - (char *)(&ocg->cg_btot[0]); 42 mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] - 43 (u_char *)&ocg->cg_iused[0]; 44 ocg->cg_magic = CG_MAGIC; 45 savednrpos = fs->fs_nrpos; 46 fs->fs_nrpos = 8; 47 break; 48 49 case FS_DYNAMICPOSTBLFMT: 50 newcg->cg_btotoff = 51 &newcg->cg_space[0] - (u_char *)(&newcg->cg_link); 52 newcg->cg_boff = 53 newcg->cg_btotoff + fs->fs_cpg * sizeof(long); 54 newcg->cg_iusedoff = newcg->cg_boff + 55 fs->fs_cpg * fs->fs_nrpos * sizeof(short); 56 newcg->cg_freeoff = 57 newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY); 58 newcg->cg_nextfreeoff = newcg->cg_freeoff + 59 howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs), 60 NBBY); 61 newcg->cg_magic = CG_MAGIC; 62 basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_link); 63 sumsize = newcg->cg_iusedoff - newcg->cg_btotoff; 64 mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff; 65 break; 66 67 default: 68 errexit("UNKNOWN ROTATIONAL TABLE FORMAT %d\n", 69 fs->fs_postblformat); 70 } 71 bzero((char *)&idesc[0], sizeof idesc); 72 for (i = 0; i < 3; i++) { 73 idesc[i].id_type = ADDR; 74 if (doinglevel2) 75 idesc[i].id_fix = FIX; 76 } 77 bzero((char *)&cstotal, sizeof(struct csum)); 78 (void)time(&now); 79 j = blknum(fs, fs->fs_size + fs->fs_frag - 1); 80 for (i = fs->fs_size; i < j; i++) 81 setbmap(i); 82 for (c = 0; c < fs->fs_ncg; c++) { 83 getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize); 84 if (!cg_chkmagic(cg)) 85 pfatal("CG %d: BAD MAGIC NUMBER\n", c); 86 dbase = cgbase(fs, c); 87 dmax = dbase + fs->fs_fpg; 88 if (dmax > fs->fs_size) 89 dmax = fs->fs_size; 90 if (now > cg->cg_time) 91 newcg->cg_time = cg->cg_time; 92 else 93 newcg->cg_time = now; 94 newcg->cg_cgx = c; 95 if (c == fs->fs_ncg - 1) 96 newcg->cg_ncyl = fs->fs_ncyl % fs->fs_cpg; 97 else 98 newcg->cg_ncyl = fs->fs_cpg; 99 newcg->cg_ndblk = dmax - dbase; 100 newcg->cg_cs.cs_ndir = 0; 101 newcg->cg_cs.cs_nffree = 0; 102 newcg->cg_cs.cs_nbfree = 0; 103 newcg->cg_cs.cs_nifree = fs->fs_ipg; 104 if (cg->cg_rotor < newcg->cg_ndblk) 105 newcg->cg_rotor = cg->cg_rotor; 106 else 107 newcg->cg_rotor = 0; 108 if (cg->cg_frotor < newcg->cg_ndblk) 109 newcg->cg_frotor = cg->cg_frotor; 110 else 111 newcg->cg_frotor = 0; 112 if (cg->cg_irotor < newcg->cg_niblk) 113 newcg->cg_irotor = cg->cg_irotor; 114 else 115 newcg->cg_irotor = 0; 116 bzero((char *)&newcg->cg_frsum[0], sizeof newcg->cg_frsum); 117 bzero((char *)&cg_blktot(newcg)[0], 118 (size_t)(sumsize + mapsize)); 119 if (fs->fs_postblformat == FS_42POSTBLFMT) 120 ocg->cg_magic = CG_MAGIC; 121 j = fs->fs_ipg * c; 122 for (i = 0; i < fs->fs_ipg; j++, i++) { 123 switch (statemap[j]) { 124 125 case USTATE: 126 break; 127 128 case DSTATE: 129 case DCLEAR: 130 case DFOUND: 131 newcg->cg_cs.cs_ndir++; 132 /* fall through */ 133 134 case FSTATE: 135 case FCLEAR: 136 newcg->cg_cs.cs_nifree--; 137 setbit(cg_inosused(newcg), i); 138 break; 139 140 default: 141 if (j < ROOTINO) 142 break; 143 errexit("BAD STATE %d FOR INODE I=%d", 144 statemap[j], j); 145 } 146 } 147 if (c == 0) 148 for (i = 0; i < ROOTINO; i++) { 149 setbit(cg_inosused(newcg), i); 150 newcg->cg_cs.cs_nifree--; 151 } 152 for (i = 0, d = dbase; 153 d < dmax; 154 d += fs->fs_frag, i += fs->fs_frag) { 155 frags = 0; 156 for (j = 0; j < fs->fs_frag; j++) { 157 if (testbmap(d + j)) 158 continue; 159 setbit(cg_blksfree(newcg), i + j); 160 frags++; 161 } 162 if (frags == fs->fs_frag) { 163 newcg->cg_cs.cs_nbfree++; 164 j = cbtocylno(fs, i); 165 cg_blktot(newcg)[j]++; 166 cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++; 167 } else if (frags > 0) { 168 newcg->cg_cs.cs_nffree += frags; 169 blk = blkmap(fs, cg_blksfree(newcg), i); 170 ffs_fragacct(fs, blk, newcg->cg_frsum, 1); 171 } 172 } 173 cstotal.cs_nffree += newcg->cg_cs.cs_nffree; 174 cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree; 175 cstotal.cs_nifree += newcg->cg_cs.cs_nifree; 176 cstotal.cs_ndir += newcg->cg_cs.cs_ndir; 177 cs = &fs->fs_cs(fs, c); 178 if (bcmp((char *)&newcg->cg_cs, (char *)cs, sizeof *cs) != 0 && 179 dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { 180 bcopy((char *)&newcg->cg_cs, (char *)cs, sizeof *cs); 181 sbdirty(); 182 } 183 if (doinglevel1) { 184 bcopy((char *)newcg, (char *)cg, (size_t)fs->fs_cgsize); 185 cgdirty(); 186 continue; 187 } 188 if (bcmp(cg_inosused(newcg), 189 cg_inosused(cg), mapsize) != 0 && 190 dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) { 191 bcopy(cg_inosused(newcg), cg_inosused(cg), 192 (size_t)mapsize); 193 cgdirty(); 194 } 195 if ((bcmp((char *)newcg, (char *)cg, basesize) != 0 || 196 bcmp((char *)&cg_blktot(newcg)[0], 197 (char *)&cg_blktot(cg)[0], sumsize) != 0) && 198 dofix(&idesc[2], "SUMMARY INFORMATION BAD")) { 199 bcopy((char *)newcg, (char *)cg, (size_t)basesize); 200 bcopy((char *)&cg_blktot(newcg)[0], 201 (char *)&cg_blktot(cg)[0], (size_t)sumsize); 202 cgdirty(); 203 } 204 } 205 if (fs->fs_postblformat == FS_42POSTBLFMT) 206 fs->fs_nrpos = savednrpos; 207 if (bcmp((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs) != 0 208 && dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { 209 bcopy((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs); 210 fs->fs_ronly = 0; 211 fs->fs_fmod = 0; 212 sbdirty(); 213 } 214 } 215