Home | History | Annotate | Line # | Download | only in fsck_ffs
pass5.c revision 1.38
      1 /*	$NetBSD: pass5.c,v 1.38 2003/09/19 08:35:15 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1986, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)pass5.c	8.9 (Berkeley) 4/28/95";
     36 #else
     37 __RCSID("$NetBSD: pass5.c,v 1.38 2003/09/19 08:35:15 itojun Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 #include <sys/time.h>
     43 
     44 #include <ufs/ufs/dinode.h>
     45 #include <ufs/ffs/fs.h>
     46 #include <ufs/ffs/ffs_extern.h>
     47 #include <ufs/ufs/ufs_bswap.h>
     48 
     49 #include <err.h>
     50 #include <string.h>
     51 #include <malloc.h>
     52 
     53 #include "fsutil.h"
     54 #include "fsck.h"
     55 #include "extern.h"
     56 
     57 void print_bmap __P((u_char *,u_int32_t));
     58 
     59 void
     60 pass5(void)
     61 {
     62 	int c, blk, frags, basesize, mapsize, cssize;
     63 	int inomapsize, blkmapsize;
     64 	struct fs *fs = sblock;
     65 	daddr_t dbase, dmax;
     66 	daddr_t d;
     67 	long i, j, k;
     68 	struct csum *cs;
     69 	struct csum_total cstotal;
     70 	struct inodesc idesc[4];
     71 	char buf[MAXBSIZE];
     72 	struct cg *newcg = (struct cg *)buf;
     73 	struct cg *cg = cgrp, *ncg;
     74 	struct inostat *info;
     75 	u_int32_t ncgsize;
     76 
     77 	inoinfo(WINO)->ino_state = USTATE;
     78 	memset(newcg, 0, (size_t)fs->fs_cgsize);
     79 	newcg->cg_niblk = fs->fs_ipg;
     80 	if (cvtlevel >= 3) {
     81 		if (fs->fs_maxcontig < 2 && fs->fs_contigsumsize > 0) {
     82 			if (preen)
     83 				pwarn("DELETING CLUSTERING MAPS\n");
     84 			if (preen || reply("DELETE CLUSTERING MAPS")) {
     85 				fs->fs_contigsumsize = 0;
     86 				doinglevel1 = 1;
     87 				sbdirty();
     88 			}
     89 		}
     90 		if (fs->fs_maxcontig > 1) {
     91 			char *doit = 0;
     92 
     93 			if (fs->fs_contigsumsize < 1) {
     94 				doit = "CREAT";
     95 			} else if (fs->fs_contigsumsize < fs->fs_maxcontig &&
     96 				   fs->fs_contigsumsize < FS_MAXCONTIG) {
     97 				doit = "EXPAND";
     98 			}
     99 			if (doit) {
    100 				i = fs->fs_contigsumsize;
    101 				fs->fs_contigsumsize =
    102 				    MIN(fs->fs_maxcontig, FS_MAXCONTIG);
    103 				if (CGSIZE(fs) > fs->fs_bsize) {
    104 					pwarn("CANNOT %s CLUSTER MAPS\n", doit);
    105 					fs->fs_contigsumsize = i;
    106 				} else if (preen ||
    107 				    reply("CREATE CLUSTER MAPS")) {
    108 					if (preen)
    109 						pwarn("%sING CLUSTER MAPS\n",
    110 						    doit);
    111 					ncgsize = fragroundup(fs, CGSIZE(fs));
    112 					ncg = realloc(cgrp, ncgsize);
    113 					if (ncg == NULL)
    114 						errx(EEXIT,
    115 						"cannot reallocate cg space");
    116 					cg = cgrp = ncg;
    117 					fs->fs_cgsize = ncgsize;
    118 					doinglevel1 = 1;
    119 					sbdirty();
    120 				}
    121 			}
    122 		}
    123 	}
    124 	basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_firstfield);
    125 	cssize = (u_char *)&cstotal.cs_spare[0] - (u_char *)&cstotal.cs_ndir;
    126 	if (is_ufs2) {
    127 		newcg->cg_iusedoff = basesize;
    128 	} else {
    129 		/*
    130 		 * We reserve the space for the old rotation summary
    131 		 * tables for the benefit of old kernels, but do not
    132 		 * maintain them in modern kernels. In time, they can
    133 		 * go away.
    134 		 */
    135 		newcg->cg_old_btotoff = basesize;
    136 		newcg->cg_old_boff = newcg->cg_old_btotoff +
    137 		    fs->fs_old_cpg * sizeof(int32_t);
    138 		newcg->cg_iusedoff = newcg->cg_old_boff +
    139 		    fs->fs_old_cpg * fs->fs_old_nrpos * sizeof(u_int16_t);
    140 		memset(&newcg->cg_space[0], 0, newcg->cg_iusedoff - basesize);
    141 	}
    142 	inomapsize = howmany(fs->fs_ipg, CHAR_BIT);
    143 	newcg->cg_freeoff = newcg->cg_iusedoff + inomapsize;
    144 	blkmapsize = howmany(fs->fs_fpg, CHAR_BIT);
    145 	newcg->cg_nextfreeoff = newcg->cg_freeoff + blkmapsize;
    146 	if (fs->fs_contigsumsize > 0) {
    147 		newcg->cg_clustersumoff = newcg->cg_nextfreeoff -
    148 		    sizeof(u_int32_t);
    149 		newcg->cg_clustersumoff =
    150 		    roundup(newcg->cg_clustersumoff, sizeof(u_int32_t));
    151 		newcg->cg_clusteroff = newcg->cg_clustersumoff +
    152 		    (fs->fs_contigsumsize + 1) * sizeof(u_int32_t);
    153 		newcg->cg_nextfreeoff = newcg->cg_clusteroff +
    154 		    howmany(fragstoblks(fs, fs->fs_fpg), CHAR_BIT);
    155 	}
    156 	newcg->cg_magic = CG_MAGIC;
    157 	mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
    158 	memset(&idesc[0], 0, sizeof idesc);
    159 	for (i = 0; i < 4; i++) {
    160 		idesc[i].id_type = ADDR;
    161 		if (!is_ufs2 && doinglevel2)
    162 			idesc[i].id_fix = FIX;
    163 	}
    164 	memset(&cstotal, 0, sizeof(struct csum_total));
    165 	dmax = blknum(fs, fs->fs_size + fs->fs_frag - 1);
    166 	for (d = fs->fs_size; d < dmax; d++)
    167 		setbmap(d);
    168 	for (c = 0; c < fs->fs_ncg; c++) {
    169 		if (got_siginfo) {
    170 			fprintf(stderr,
    171 			    "%s: phase 5: cyl group %d of %d (%d%%)\n",
    172 			    cdevname(), c, fs->fs_ncg,
    173 			    c * 100 / fs->fs_ncg);
    174 			got_siginfo = 0;
    175 		}
    176 		getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize);
    177 		memcpy(cg, cgblk.b_un.b_cg, fs->fs_cgsize);
    178 		if((doswap && !needswap) || (!doswap && needswap))
    179 			ffs_cg_swap(cgblk.b_un.b_cg, cg, sblock);
    180 		if (!cg_chkmagic(cg, 0))
    181 			pfatal("CG %d: PASS5: BAD MAGIC NUMBER\n", c);
    182 		if(doswap)
    183 			cgdirty();
    184 		/*
    185 		 * While we have the disk head where we want it,
    186 		 * write back the superblock to the spare at this
    187 		 * cylinder group.
    188 		 */
    189 		if ((cvtlevel && sblk.b_dirty) || doswap) {
    190 			bwrite(fswritefd, sblk.b_un.b_buf,
    191 			    fsbtodb(sblock, cgsblock(sblock, c)),
    192 			    sblock->fs_sbsize);
    193 		} else {
    194 			/*
    195 			 * Read in the current alternate superblock,
    196 			 * and compare it to the master.  If it's
    197 			 * wrong, fix it up.
    198 			 */
    199 			getblk(&asblk, cgsblock(sblock, c), sblock->fs_sbsize);
    200 			if (asblk.b_errs)
    201 				pfatal("CG %d: UNABLE TO READ ALTERNATE "
    202 				    "SUPERBLK\n", c);
    203 			else {
    204 				memmove(altsblock, asblk.b_un.b_fs,
    205 				    sblock->fs_sbsize);
    206 				if (needswap)
    207 					ffs_sb_swap(asblk.b_un.b_fs, altsblock);
    208 			}
    209 			if ((asblk.b_errs || cmpsblks(sblock, altsblock)) &&
    210 			     dofix(&idesc[3],
    211 				   "ALTERNATE SUPERBLK(S) ARE INCORRECT")) {
    212 				bwrite(fswritefd, sblk.b_un.b_buf,
    213 				    fsbtodb(sblock, cgsblock(sblock, c)),
    214 				    sblock->fs_sbsize);
    215 			}
    216 		}
    217 		dbase = cgbase(fs, c);
    218 		dmax = dbase + fs->fs_fpg;
    219 		if (dmax > fs->fs_size)
    220 			dmax = fs->fs_size;
    221 		newcg->cg_time = cg->cg_time;
    222 		newcg->cg_old_time = cg->cg_old_time;
    223 		newcg->cg_cgx = c;
    224 		newcg->cg_ndblk = dmax - dbase;
    225 		if (!is_ufs2) {
    226 			if (c == fs->fs_ncg - 1)
    227 				newcg->cg_old_ncyl = howmany(newcg->cg_ndblk,
    228 				    fs->fs_fpg / fs->fs_old_cpg);
    229 			else
    230 				newcg->cg_old_ncyl = fs->fs_old_cpg;
    231 			newcg->cg_old_niblk = fs->fs_ipg;
    232 			newcg->cg_niblk = 0;
    233 		}
    234 		if (fs->fs_contigsumsize > 0)
    235 			newcg->cg_nclusterblks = newcg->cg_ndblk / fs->fs_frag;
    236 		newcg->cg_cs.cs_ndir = 0;
    237 		newcg->cg_cs.cs_nffree = 0;
    238 		newcg->cg_cs.cs_nbfree = 0;
    239 		newcg->cg_cs.cs_nifree = fs->fs_ipg;
    240 		if (cg->cg_rotor >= 0 && cg->cg_rotor < newcg->cg_ndblk)
    241 			newcg->cg_rotor = cg->cg_rotor;
    242 		else
    243 			newcg->cg_rotor = 0;
    244 		if (cg->cg_frotor >= 0 && cg->cg_frotor < newcg->cg_ndblk)
    245 			newcg->cg_frotor = cg->cg_frotor;
    246 		else
    247 			newcg->cg_frotor = 0;
    248 		if (cg->cg_irotor >= 0 && cg->cg_irotor < fs->fs_ipg)
    249 			newcg->cg_irotor = cg->cg_irotor;
    250 		else
    251 			newcg->cg_irotor = 0;
    252 		if (!is_ufs2) {
    253 			newcg->cg_initediblk = 0;
    254 		} else {
    255 			if ((unsigned)cg->cg_initediblk > fs->fs_ipg)
    256 				newcg->cg_initediblk = fs->fs_ipg;
    257 			else
    258 				newcg->cg_initediblk = cg->cg_initediblk;
    259 		}
    260 		memset(&newcg->cg_frsum[0], 0, sizeof newcg->cg_frsum);
    261 		memset(cg_inosused(newcg, 0), 0, (size_t)(mapsize));
    262 		j = fs->fs_ipg * c;
    263 		for (i = 0; i < fs->fs_ipg; j++, i++) {
    264 			info = inoinfo(j);
    265 			switch (info->ino_state) {
    266 
    267 			case USTATE:
    268 				break;
    269 
    270 			case DSTATE:
    271 			case DCLEAR:
    272 			case DFOUND:
    273 				newcg->cg_cs.cs_ndir++;
    274 				/* fall through */
    275 
    276 			case FSTATE:
    277 			case FCLEAR:
    278 				newcg->cg_cs.cs_nifree--;
    279 				setbit(cg_inosused(newcg, 0), i);
    280 				break;
    281 
    282 			default:
    283 				if (j < ROOTINO)
    284 					break;
    285 				errx(EEXIT, "BAD STATE %d FOR INODE I=%ld",
    286 				    info->ino_state, (long)j);
    287 			}
    288 		}
    289 		if (c == 0)
    290 			for (i = 0; i < ROOTINO; i++) {
    291 				setbit(cg_inosused(newcg, 0), i);
    292 				newcg->cg_cs.cs_nifree--;
    293 			}
    294 		for (i = 0, d = dbase;
    295 		     d < dmax;
    296 		     d += fs->fs_frag, i += fs->fs_frag) {
    297 			frags = 0;
    298 			for (j = 0; j < fs->fs_frag; j++) {
    299 				if (testbmap(d + j))
    300 					continue;
    301 				setbit(cg_blksfree(newcg, 0), i + j);
    302 				frags++;
    303 			}
    304 			if (frags == fs->fs_frag) {
    305 				newcg->cg_cs.cs_nbfree++;
    306 				if (fs->fs_contigsumsize > 0)
    307 					setbit(cg_clustersfree(newcg, 0),
    308 					    fragstoblks(fs, i));
    309 			} else if (frags > 0) {
    310 				newcg->cg_cs.cs_nffree += frags;
    311 				blk = blkmap(fs, cg_blksfree(newcg, 0), i);
    312 				ffs_fragacct(fs, blk, newcg->cg_frsum, 1, 0);
    313 			}
    314 		}
    315 		if (fs->fs_contigsumsize > 0) {
    316 			int32_t *sump = cg_clustersum(newcg, 0);
    317 			u_char *mapp = cg_clustersfree(newcg, 0);
    318 			int map = *mapp++;
    319 			int bit = 1;
    320 			int run = 0;
    321 
    322 			for (i = 0; i < newcg->cg_nclusterblks; i++) {
    323 				if ((map & bit) != 0) {
    324 					run++;
    325 				} else if (run != 0) {
    326 					if (run > fs->fs_contigsumsize)
    327 						run = fs->fs_contigsumsize;
    328 					sump[run]++;
    329 					run = 0;
    330 				}
    331 				if ((i & (NBBY - 1)) != (NBBY - 1)) {
    332 					bit <<= 1;
    333 				} else {
    334 					map = *mapp++;
    335 					bit = 1;
    336 				}
    337 			}
    338 			if (run != 0) {
    339 				if (run > fs->fs_contigsumsize)
    340 					run = fs->fs_contigsumsize;
    341 				sump[run]++;
    342 			}
    343 		}
    344 		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
    345 		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
    346 		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
    347 		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
    348 		cs = &fs->fs_cs(fs, c);
    349 		if (memcmp(&newcg->cg_cs, cs, sizeof *cs) != 0) {
    350 			if (dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
    351 				memmove(cs, &newcg->cg_cs, sizeof *cs);
    352 				sbdirty();
    353 			} else
    354 				markclean = 0;
    355 		}
    356 		if (doinglevel1) {
    357 			memmove(cg, newcg, (size_t)fs->fs_cgsize);
    358 			cgdirty();
    359 			continue;
    360 		}
    361 		if (memcmp(newcg, cg, basesize) != 0) {
    362 		 	if (dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
    363 				memmove(cg, newcg, (size_t)basesize);
    364 				cgdirty();
    365 			} else
    366 				markclean = 0;
    367 		}
    368 		if (usedsoftdep) {
    369 			for (i = 0; i < inomapsize; i++) {
    370 				j = cg_inosused(newcg, 0)[i];
    371 				if ((cg_inosused(cg, 0)[i] & j) == j)
    372 					continue;
    373 				for (k = 0; k < NBBY; k++) {
    374 					if ((j & (1 << k)) == 0)
    375 						continue;
    376 					if (cg_inosused(cg, 0)[i] & (1 << k))
    377 						continue;
    378 					pwarn("ALLOCATED INODE %ld "
    379 					    "MARKED FREE\n",
    380 					    c * fs->fs_ipg + i * 8 + k);
    381 				}
    382 			}
    383 			for (i = 0; i < blkmapsize; i++) {
    384 				j = cg_blksfree(cg, 0)[i];
    385 				if ((cg_blksfree(newcg, 0)[i] & j) == j)
    386 					continue;
    387 				for (k = 0; k < NBBY; k++) {
    388 					if ((j & (1 << k)) == 0)
    389 						continue;
    390 					if (cg_inosused(cg, 0)[i] & (1 << k))
    391 						continue;
    392 					pwarn("ALLOCATED FRAG %ld "
    393 					    "MARKED FREE\n",
    394 					    c * fs->fs_fpg + i * 8 + k);
    395 				}
    396 			}
    397 		}
    398 		if (memcmp(cg_inosused(newcg, 0), cg_inosused(cg, 0), mapsize)
    399 		    != 0 && dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
    400 			memmove(cg_inosused(cg, 0), cg_inosused(newcg, 0),
    401 			    (size_t)mapsize);
    402                         cgdirty();
    403                 }
    404 	}
    405 	if (memcmp(&cstotal, &fs->fs_cstotal, cssize) != 0) {
    406 		if (dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
    407 			memmove(&fs->fs_cstotal, &cstotal, sizeof cstotal);
    408 			fs->fs_ronly = 0;
    409 			fs->fs_fmod = 0;
    410 			sbdirty();
    411 		} else
    412 			markclean = 0;
    413 	}
    414 }
    415 
    416 void
    417 print_bmap(map, size)
    418 	u_char *map;
    419 	u_int32_t size;
    420 {
    421 	int i, j;
    422 
    423 	i = 0;
    424 	while (i < size) {
    425 		printf("%u: ",i);
    426 		for (j = 0; j < 16; j++, i++)
    427 			printf("%2x ", (u_int)map[i] & 0xff);
    428 		printf("\n");
    429 	}
    430 }
    431