1 /* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 #if 0 36 static const char sccsid[] = "@(#)pass5.c 8.9 (Berkeley) 4/28/95"; 37 #endif 38 static const char rcsid[] = 39 "$FreeBSD$"; 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 44 #include <ufs/ufs/dinode.h> 45 #include <ufs/ffs/fs.h> 46 47 #include <err.h> 48 #include <string.h> 49 50 #include "fsck.h" 51 52 void 53 pass5() 54 { 55 int c, blk, frags, basesize, sumsize, mapsize, savednrpos = 0; 56 int inomapsize, blkmapsize; 57 struct fs *fs = &sblock; 58 struct cg *cg = &cgrp; 59 ufs_daddr_t dbase, dmax; 60 ufs_daddr_t d; 61 long i, j, k; 62 struct csum *cs; 63 struct csum cstotal; 64 struct inodesc idesc[3]; 65 char buf[MAXBSIZE]; 66 register struct cg *newcg = (struct cg *)buf; 67 struct ocg *ocg = (struct ocg *)buf; 68 69 inoinfo(WINO)->ino_state = USTATE; 70 memset(newcg, 0, (size_t)fs->fs_cgsize); 71 newcg->cg_niblk = fs->fs_ipg; 72 if (cvtlevel >= 3) { 73 if (fs->fs_maxcontig < 2 && fs->fs_contigsumsize > 0) { 74 if (preen) 75 pwarn("DELETING CLUSTERING MAPS\n"); 76 if (preen || reply("DELETE CLUSTERING MAPS")) { 77 fs->fs_contigsumsize = 0; 78 doinglevel1 = 1; 79 sbdirty(); 80 } 81 } 82 if (fs->fs_maxcontig > 1) { 83 char *doit = 0; 84 85 if (fs->fs_contigsumsize < 1) { 86 doit = "CREAT"; 87 } else if (fs->fs_contigsumsize < fs->fs_maxcontig && 88 fs->fs_contigsumsize < FS_MAXCONTIG) { 89 doit = "EXPAND"; 90 } 91 if (doit) { 92 i = fs->fs_contigsumsize; 93 fs->fs_contigsumsize = 94 MIN(fs->fs_maxcontig, FS_MAXCONTIG); 95 if (CGSIZE(fs) > fs->fs_bsize) { 96 pwarn("CANNOT %s CLUSTER MAPS\n", doit); 97 fs->fs_contigsumsize = i; 98 } else if (preen || 99 reply("CREATE CLUSTER MAPS")) { 100 if (preen) 101 pwarn("%sING CLUSTER MAPS\n", 102 doit); 103 fs->fs_cgsize = 104 fragroundup(fs, CGSIZE(fs)); 105 doinglevel1 = 1; 106 sbdirty(); 107 } 108 } 109 } 110 } 111 switch ((int)fs->fs_postblformat) { 112 113 case FS_42POSTBLFMT: 114 basesize = (char *)(&ocg->cg_btot[0]) - 115 (char *)(&ocg->cg_firstfield); 116 sumsize = &ocg->cg_iused[0] - (u_int8_t *)(&ocg->cg_btot[0]); 117 mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] - 118 (u_char *)&ocg->cg_iused[0]; 119 blkmapsize = howmany(fs->fs_fpg, NBBY); 120 inomapsize = &ocg->cg_free[0] - (u_char *)&ocg->cg_iused[0]; 121 ocg->cg_magic = CG_MAGIC; 122 savednrpos = fs->fs_nrpos; 123 fs->fs_nrpos = 8; 124 break; 125 126 case FS_DYNAMICPOSTBLFMT: 127 newcg->cg_btotoff = 128 &newcg->cg_space[0] - (u_char *)(&newcg->cg_firstfield); 129 newcg->cg_boff = 130 newcg->cg_btotoff + fs->fs_cpg * sizeof(int32_t); 131 newcg->cg_iusedoff = newcg->cg_boff + 132 fs->fs_cpg * fs->fs_nrpos * sizeof(u_int16_t); 133 newcg->cg_freeoff = 134 newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY); 135 inomapsize = newcg->cg_freeoff - newcg->cg_iusedoff; 136 newcg->cg_nextfreeoff = newcg->cg_freeoff + 137 howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs), NBBY); 138 blkmapsize = newcg->cg_nextfreeoff - newcg->cg_freeoff; 139 if (fs->fs_contigsumsize > 0) { 140 newcg->cg_clustersumoff = newcg->cg_nextfreeoff - 141 sizeof(u_int32_t); 142 newcg->cg_clustersumoff = 143 roundup(newcg->cg_clustersumoff, sizeof(u_int32_t)); 144 newcg->cg_clusteroff = newcg->cg_clustersumoff + 145 (fs->fs_contigsumsize + 1) * sizeof(u_int32_t); 146 newcg->cg_nextfreeoff = newcg->cg_clusteroff + 147 howmany(fs->fs_cpg * fs->fs_spc / NSPB(fs), NBBY); 148 } 149 newcg->cg_magic = CG_MAGIC; 150 basesize = &newcg->cg_space[0] - 151 (u_char *)(&newcg->cg_firstfield); 152 sumsize = newcg->cg_iusedoff - newcg->cg_btotoff; 153 mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff; 154 break; 155 156 default: 157 inomapsize = blkmapsize = sumsize = 0; /* keep lint happy */ 158 errx(EEXIT, "UNKNOWN ROTATIONAL TABLE FORMAT %d", 159 fs->fs_postblformat); 160 } 161 memset(&idesc[0], 0, sizeof idesc); 162 for (i = 0; i < 3; i++) { 163 idesc[i].id_type = ADDR; 164 if (doinglevel2) 165 idesc[i].id_fix = FIX; 166 } 167 memset(&cstotal, 0, sizeof(struct csum)); 168 j = blknum(fs, fs->fs_size + fs->fs_frag - 1); 169 for (i = fs->fs_size; i < j; i++) 170 setbmap(i); 171 for (c = 0; c < fs->fs_ncg; c++) { 172 getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize); 173 if (!cg_chkmagic(cg)) 174 pfatal("CG %d: BAD MAGIC NUMBER\n", c); 175 dbase = cgbase(fs, c); 176 dmax = dbase + fs->fs_fpg; 177 if (dmax > fs->fs_size) 178 dmax = fs->fs_size; 179 newcg->cg_time = cg->cg_time; 180 newcg->cg_cgx = c; 181 if (c == fs->fs_ncg - 1) 182 newcg->cg_ncyl = fs->fs_ncyl % fs->fs_cpg; 183 else 184 newcg->cg_ncyl = fs->fs_cpg; 185 newcg->cg_ndblk = dmax - dbase; 186 if (fs->fs_contigsumsize > 0) 187 newcg->cg_nclusterblks = newcg->cg_ndblk / fs->fs_frag; 188 newcg->cg_cs.cs_ndir = 0; 189 newcg->cg_cs.cs_nffree = 0; 190 newcg->cg_cs.cs_nbfree = 0; 191 newcg->cg_cs.cs_nifree = fs->fs_ipg; 192 if (cg->cg_rotor < newcg->cg_ndblk) 193 newcg->cg_rotor = cg->cg_rotor; 194 else 195 newcg->cg_rotor = 0; 196 if (cg->cg_frotor < newcg->cg_ndblk) 197 newcg->cg_frotor = cg->cg_frotor; 198 else 199 newcg->cg_frotor = 0; 200 if (cg->cg_irotor < newcg->cg_niblk) 201 newcg->cg_irotor = cg->cg_irotor; 202 else 203 newcg->cg_irotor = 0; 204 memset(&newcg->cg_frsum[0], 0, sizeof newcg->cg_frsum); 205 memset(&cg_blktot(newcg)[0], 0, 206 (size_t)(sumsize + mapsize)); 207 if (fs->fs_postblformat == FS_42POSTBLFMT) 208 ocg->cg_magic = CG_MAGIC; 209 j = fs->fs_ipg * c; 210 for (i = 0; i < inostathead[c].il_numalloced; j++, i++) { 211 switch (inoinfo(j)->ino_state) { 212 213 case USTATE: 214 break; 215 216 case DSTATE: 217 case DCLEAR: 218 case DFOUND: 219 newcg->cg_cs.cs_ndir++; 220 /* fall through */ 221 222 case FSTATE: 223 case FCLEAR: 224 newcg->cg_cs.cs_nifree--; 225 setbit(cg_inosused(newcg), i); 226 break; 227 228 default: 229 if (j < ROOTINO) 230 break; 231 errx(EEXIT, "BAD STATE %d FOR INODE I=%ld", 232 inoinfo(j)->ino_state, j); 233 } 234 } 235 if (c == 0) 236 for (i = 0; i < ROOTINO; i++) { 237 setbit(cg_inosused(newcg), i); 238 newcg->cg_cs.cs_nifree--; 239 } 240 for (i = 0, d = dbase; 241 d < dmax; 242 d += fs->fs_frag, i += fs->fs_frag) { 243 frags = 0; 244 for (j = 0; j < fs->fs_frag; j++) { 245 if (testbmap(d + j)) 246 continue; 247 setbit(cg_blksfree(newcg), i + j); 248 frags++; 249 } 250 if (frags == fs->fs_frag) { 251 newcg->cg_cs.cs_nbfree++; 252 j = cbtocylno(fs, i); 253 cg_blktot(newcg)[j]++; 254 cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++; 255 if (fs->fs_contigsumsize > 0) 256 setbit(cg_clustersfree(newcg), 257 i / fs->fs_frag); 258 } else if (frags > 0) { 259 newcg->cg_cs.cs_nffree += frags; 260 blk = blkmap(fs, cg_blksfree(newcg), i); 261 ffs_fragacct(fs, blk, newcg->cg_frsum, 1); 262 } 263 } 264 if (fs->fs_contigsumsize > 0) { 265 int32_t *sump = cg_clustersum(newcg); 266 u_char *mapp = cg_clustersfree(newcg); 267 int map = *mapp++; 268 int bit = 1; 269 int run = 0; 270 271 for (i = 0; i < newcg->cg_nclusterblks; i++) { 272 if ((map & bit) != 0) { 273 run++; 274 } else if (run != 0) { 275 if (run > fs->fs_contigsumsize) 276 run = fs->fs_contigsumsize; 277 sump[run]++; 278 run = 0; 279 } 280 if ((i & (NBBY - 1)) != (NBBY - 1)) { 281 bit <<= 1; 282 } else { 283 map = *mapp++; 284 bit = 1; 285 } 286 } 287 if (run != 0) { 288 if (run > fs->fs_contigsumsize) 289 run = fs->fs_contigsumsize; 290 sump[run]++; 291 } 292 } 293 cstotal.cs_nffree += newcg->cg_cs.cs_nffree; 294 cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree; 295 cstotal.cs_nifree += newcg->cg_cs.cs_nifree; 296 cstotal.cs_ndir += newcg->cg_cs.cs_ndir; 297 cs = &fs->fs_cs(fs, c); 298 if (memcmp(&newcg->cg_cs, cs, sizeof *cs) != 0 && 299 dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { 300 memmove(cs, &newcg->cg_cs, sizeof *cs); 301 sbdirty(); 302 } 303 if (doinglevel1) { 304 memmove(cg, newcg, (size_t)fs->fs_cgsize); 305 cgdirty(); 306 continue; 307 } 308 if ((memcmp(newcg, cg, basesize) != 0 || 309 memcmp(&cg_blktot(newcg)[0], 310 &cg_blktot(cg)[0], sumsize) != 0) && 311 dofix(&idesc[2], "SUMMARY INFORMATION BAD")) { 312 memmove(cg, newcg, (size_t)basesize); 313 memmove(&cg_blktot(cg)[0], 314 &cg_blktot(newcg)[0], (size_t)sumsize); 315 cgdirty(); 316 } 317 if (usedsoftdep) { 318 for (i = 0; i < inomapsize; i++) { 319 j = cg_inosused(newcg)[i]; 320 if ((cg_inosused(cg)[i] & j) == j) 321 continue; 322 for (k = 0; k < NBBY; k++) { 323 if ((j & (1 << k)) == 0) 324 continue; 325 if (cg_inosused(cg)[i] & (1 << k)) 326 continue; 327 pwarn("ALLOCATED INODE %d MARKED FREE\n", 328 c * fs->fs_ipg + i * NBBY + k); 329 } 330 } 331 for (i = 0; i < blkmapsize; i++) { 332 j = cg_blksfree(cg)[i]; 333 if ((cg_blksfree(newcg)[i] & j) == j) 334 continue; 335 for (k = 0; k < NBBY; k++) { 336 if ((j & (1 << k)) == 0) 337 continue; 338 if (cg_blksfree(newcg)[i] & (1 << k)) 339 continue; 340 pwarn("ALLOCATED FRAG %d MARKED FREE\n", 341 c * fs->fs_fpg + i * NBBY + k); 342 } 343 } 344 } 345 if (memcmp(cg_inosused(newcg), cg_inosused(cg), mapsize) != 0 && 346 dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) { 347 memmove(cg_inosused(cg), cg_inosused(newcg), 348 (size_t)mapsize); 349 cgdirty(); 350 } 351 } 352 if (fs->fs_postblformat == FS_42POSTBLFMT) 353 fs->fs_nrpos = savednrpos; 354 if (memcmp(&cstotal, &fs->fs_cstotal, sizeof *cs) != 0 355 && dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { 356 memmove(&fs->fs_cstotal, &cstotal, sizeof *cs); 357 fs->fs_ronly = 0; 358 fs->fs_fmod = 0; 359 sbdirty(); 360 } 361 } 362