Home | History | Annotate | Line # | Download | only in fsck_lfs
pass0.c revision 1.25
      1 /* $NetBSD: pass0.c,v 1.25 2006/03/17 15:53:46 rumble Exp $	 */
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*-
     39  * Copyright (c) 1980, 1986, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  */
     66 
     67 #include <sys/types.h>
     68 #include <sys/param.h>
     69 #include <sys/time.h>
     70 #include <sys/buf.h>
     71 #include <sys/mount.h>
     72 
     73 #include <ufs/ufs/inode.h>
     74 #include <ufs/ufs/dir.h>
     75 #define vnode uvnode
     76 #include <ufs/lfs/lfs.h>
     77 #undef vnode
     78 
     79 #include <stdio.h>
     80 #include <stdlib.h>
     81 #include <string.h>
     82 
     83 #include "bufcache.h"
     84 #include "vnode.h"
     85 #include "lfs_user.h"
     86 
     87 #include "fsck.h"
     88 #include "extern.h"
     89 #include "fsutil.h"
     90 
     91 extern int fake_cleanseg;
     92 
     93 /*
     94  * Pass 0.  Check the LFS partial segments for valid checksums, correcting
     95  * if necessary.  Also check for valid offsets for inode and finfo blocks.
     96  */
     97 /*
     98  * XXX more could be done here---consistency between inode-held blocks and
     99  * finfo blocks, for one thing.
    100  */
    101 
    102 #define dbshift (fs->lfs_bshift - fs->lfs_blktodb)
    103 
    104 void
    105 pass0(void)
    106 {
    107 	daddr_t daddr;
    108 	CLEANERINFO *cip;
    109 	IFILE *ifp;
    110 	struct ubuf *bp;
    111 	ino_t ino, plastino, nextino, *visited, lowfreeino;
    112 	int writeit = 0;
    113 	int count;
    114 	long long totaldist;
    115 
    116 	/*
    117          * Check the inode free list for inuse inodes, and cycles.
    118 	 * Make sure that all free inodes are in fact on the list.
    119          */
    120 	visited = (ino_t *) malloc(maxino * sizeof(ino_t));
    121 	if (visited == NULL)
    122 		err(1, NULL);
    123 	memset(visited, 0, maxino * sizeof(ino_t));
    124 
    125 #ifdef BAD
    126 	/*
    127 	 * Scramble the free list, to trigger the optimizer below.
    128 	 * You don't want this unless you are debugging fsck_lfs itself.
    129 	 */
    130 	if (!preen && reply("SCRAMBLE FREE LIST") == 1) {
    131 		ino_t topino, botino, tail;
    132 		botino = 0;
    133 		topino = maxino;
    134 		while (botino < topino) {
    135 			for (--topino; botino < topino; --topino) {
    136 				LFS_IENTRY(ifp, fs, topino, bp);
    137 				if (ifp->if_daddr == 0)
    138 					break;
    139 				brelse(bp);
    140 				bp = NULL;
    141 			}
    142 			if (topino == botino)
    143 				break;
    144 			if (botino > 0) {
    145 				ifp->if_nextfree = botino;
    146 			} else {
    147 				ifp->if_nextfree = 0x0;
    148 				tail = topino;
    149 			}
    150 			VOP_BWRITE(bp);
    151 			ino = topino;
    152 
    153 			for (++botino; botino < topino; ++botino) {
    154 				LFS_IENTRY(ifp, fs, botino, bp);
    155 				if (ifp->if_daddr == 0)
    156 					break;
    157 				brelse(bp);
    158 				bp = NULL;
    159 			}
    160 			if (topino == botino)
    161 				break;
    162 			ifp->if_nextfree = topino;
    163 			VOP_BWRITE(bp);
    164 			ino = botino;
    165 		}
    166 		LFS_CLEANERINFO(cip, fs, bp);
    167 		cip->free_head = fs->lfs_freehd = ino;
    168 		cip->free_tail = tail;
    169 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
    170 	}
    171 #endif /* BAD */
    172 
    173 	count = 0;
    174 	plastino = 0;
    175 	totaldist = 0;
    176 	lowfreeino = maxino;
    177 	ino = fs->lfs_freehd;
    178 	while (ino) {
    179 		if (lowfreeino > ino)
    180 			lowfreeino = ino;
    181 		if (plastino > 0) {
    182 			totaldist += abs(ino - plastino);
    183 			++count;
    184 		}
    185 		if (ino >= maxino) {
    186 			printf("! Ino %llu out of range (last was %llu)\n",
    187 			    (unsigned long long)ino,
    188 			    (unsigned long long)plastino);
    189 			break;
    190 		}
    191 		if (visited[ino]) {
    192 			pwarn("! Ino %llu already found on the free list!\n",
    193 			    (unsigned long long)ino);
    194 			if (preen || reply("FIX") == 1) {
    195 				/* plastino can't be zero */
    196 				LFS_IENTRY(ifp, fs, plastino, bp);
    197 				ifp->if_nextfree = 0;
    198 				VOP_BWRITE(bp);
    199 			}
    200 			break;
    201 		}
    202 		++visited[ino];
    203 		LFS_IENTRY(ifp, fs, ino, bp);
    204 		nextino = ifp->if_nextfree;
    205 		daddr = ifp->if_daddr;
    206 		brelse(bp);
    207 		if (daddr) {
    208 			pwarn("! Ino %llu with daddr 0x%llx is on the "
    209 			    "free list!\n",
    210 			    (unsigned long long)ino, (long long) daddr);
    211 			if (preen || reply("FIX") == 1) {
    212 				if (plastino == 0) {
    213 					fs->lfs_freehd = nextino;
    214 					sbdirty();
    215 				} else {
    216 					LFS_IENTRY(ifp, fs, plastino, bp);
    217 					ifp->if_nextfree = nextino;
    218 					VOP_BWRITE(bp);
    219 				}
    220 				ino = nextino;
    221 				continue;
    222 			}
    223 		}
    224 		plastino = ino;
    225 		ino = nextino;
    226 	}
    227 
    228 
    229 	/*
    230 	 * Make sure all free inodes were found on the list
    231 	 */
    232 	for (ino = ROOTINO + 1; ino < maxino; ++ino) {
    233 		if (visited[ino])
    234 			continue;
    235 
    236 		LFS_IENTRY(ifp, fs, ino, bp);
    237 		if (ifp->if_daddr) {
    238 			brelse(bp);
    239 			continue;
    240 		}
    241 		pwarn("! Ino %llu free, but not on the free list\n",
    242 		    (unsigned long long)ino);
    243 		if (preen || reply("FIX") == 1) {
    244 			ifp->if_nextfree = fs->lfs_freehd;
    245 			fs->lfs_freehd = ino;
    246 			sbdirty();
    247 			VOP_BWRITE(bp);
    248 		} else
    249 			brelse(bp);
    250 	}
    251 
    252 	LFS_CLEANERINFO(cip, fs, bp);
    253 	if (cip->free_head != fs->lfs_freehd) {
    254 		pwarn("! Free list head should be %d (was %d)\n",
    255 			fs->lfs_freehd, cip->free_head);
    256 		if (preen || reply("FIX")) {
    257 			cip->free_head = fs->lfs_freehd;
    258 			writeit = 1;
    259 		}
    260 	}
    261 	if (cip->free_tail != plastino) {
    262 		pwarn("! Free list tail should be %llu (was %d)\n",
    263 		    (unsigned long long)plastino, cip->free_tail);
    264 		if (preen || reply("FIX")) {
    265 			cip->free_tail = plastino;
    266 			writeit = 1;
    267 		}
    268 	}
    269 	if (writeit)
    270 		LFS_SYNC_CLEANERINFO(cip, fs, bp, writeit);
    271 	else
    272 		brelse(bp);
    273 
    274 	if (fs->lfs_freehd == 0) {
    275 		pwarn("%sree list head is 0x0\n", preen ? "f" : "F");
    276 		if (preen || reply("FIX")) {
    277 			extend_ifile(fs);
    278 			reset_maxino(((VTOI(fs->lfs_ivnode)->i_ffs1_size >>
    279 				       fs->lfs_bsize) -
    280 				      fs->lfs_segtabsz - fs->lfs_cleansz) *
    281 				     fs->lfs_ifpb);
    282 		}
    283 	}
    284 
    285 	/*
    286 	 * Check the distance between sequential free list entries.
    287 	 * An ideally ordered free list will have the sum of these
    288 	 * distances <= the number of inodes in the inode list.
    289 	 * If the observed distance is too high, reorder the list.
    290 	 * Strictly speaking, this is not an error, but it optimizes the
    291 	 * speed in creation of files and should help a tiny bit with
    292 	 * cleaner thrash as well.
    293 	 */
    294 	if (totaldist > 4 * maxino) {
    295 		pwarn("%sotal inode list traversal length %" PRIu64 "x list length%s\n",
    296 		      (preen ? "t" : "T"), totaldist/maxino,
    297 		      (preen ? ", optimizing" : ""));
    298 		if (preen || reply("OPTIMIZE") == 1) {
    299 			plastino = lowfreeino;
    300 			for (ino = lowfreeino + 1; ino < maxino; ino++) {
    301 				LFS_IENTRY(ifp, fs, ino, bp);
    302 				daddr = ifp->if_daddr;
    303 				brelse(bp);
    304 				if (daddr == 0) {
    305 					LFS_IENTRY(ifp, fs, plastino, bp);
    306 					ifp->if_nextfree = ino;
    307 					VOP_BWRITE(bp);
    308 					plastino = ino;
    309 				}
    310 			}
    311 			LFS_CLEANERINFO(cip, fs, bp);
    312 			cip->free_head = fs->lfs_freehd = lowfreeino;
    313 			cip->free_tail = plastino;
    314 			LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
    315 		}
    316 	}
    317 }
    318