Home | History | Annotate | Line # | Download | only in fsck_lfs
pass0.c revision 1.20
      1 /* $NetBSD: pass0.c,v 1.20 2005/04/11 23:19:24 perseant 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.h"
     86 
     87 #include "fsck.h"
     88 #include "extern.h"
     89 #include "fsutil.h"
     90 
     91 extern int fake_cleanseg;
     92 int extend_ifile(void);
     93 
     94 /*
     95  * Pass 0.  Check the LFS partial segments for valid checksums, correcting
     96  * if necessary.  Also check for valid offsets for inode and finfo blocks.
     97  */
     98 /*
     99  * XXX more could be done here---consistency between inode-held blocks and
    100  * finfo blocks, for one thing.
    101  */
    102 
    103 #define dbshift (fs->lfs_bshift - fs->lfs_blktodb)
    104 
    105 void
    106 pass0(void)
    107 {
    108 	daddr_t daddr;
    109 	CLEANERINFO *cip;
    110 	IFILE *ifp;
    111 	struct ubuf *bp;
    112 	ino_t ino, plastino, nextino, *visited, lowfreeino;
    113 	int writeit = 0;
    114 	int count;
    115 	long long totaldist;
    116 
    117 	/*
    118          * Check the inode free list for inuse inodes, and cycles.
    119 	 * Make sure that all free inodes are in fact on the list.
    120          */
    121 	visited = (ino_t *) malloc(maxino * sizeof(ino_t));
    122 	memset(visited, 0, maxino * sizeof(ino_t));
    123 
    124 #ifdef BAD
    125 	/*
    126 	 * Scramble the free list, to trigger the optimizer below.
    127 	 * You don't want this unless you are debugging fsck_lfs itself.
    128 	 */
    129 	if (!preen && reply("SCRAMBLE FREE LIST") == 1) {
    130 		ino_t topino, botino, tail;
    131 		botino = 0;
    132 		topino = maxino;
    133 		while (botino < topino) {
    134 			for (--topino; botino < topino; --topino) {
    135 				LFS_IENTRY(ifp, fs, topino, bp);
    136 				if (ifp->if_daddr == 0)
    137 					break;
    138 				brelse(bp);
    139 				bp = NULL;
    140 			}
    141 			if (topino == botino)
    142 				break;
    143 			if (botino > 0) {
    144 				ifp->if_nextfree = botino;
    145 			} else {
    146 				ifp->if_nextfree = 0x0;
    147 				tail = topino;
    148 			}
    149 			VOP_BWRITE(bp);
    150 			ino = topino;
    151 
    152 			for (++botino; botino < topino; ++botino) {
    153 				LFS_IENTRY(ifp, fs, botino, bp);
    154 				if (ifp->if_daddr == 0)
    155 					break;
    156 				brelse(bp);
    157 				bp = NULL;
    158 			}
    159 			if (topino == botino)
    160 				break;
    161 			ifp->if_nextfree = topino;
    162 			VOP_BWRITE(bp);
    163 			ino = botino;
    164 		}
    165 		LFS_CLEANERINFO(cip, fs, bp);
    166 		cip->free_head = fs->lfs_freehd = ino;
    167 		cip->free_tail = tail;
    168 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
    169 	}
    170 #endif /* BAD */
    171 
    172 	count = 0;
    173 	plastino = 0;
    174 	totaldist = 0;
    175 	lowfreeino = maxino;
    176 	ino = fs->lfs_freehd;
    177 	while (ino) {
    178 		if (lowfreeino > ino)
    179 			lowfreeino = ino;
    180 		if (plastino > 0) {
    181 			totaldist += abs(ino - plastino);
    182 			++count;
    183 		}
    184 		if (ino >= maxino) {
    185 			printf("! Ino %d out of range (last was %d)\n", ino,
    186 			    plastino);
    187 			break;
    188 		}
    189 		if (visited[ino]) {
    190 			pwarn("! Ino %d already found on the free list!\n",
    191 			    ino);
    192 			if (preen || reply("FIX") == 1) {
    193 				/* plastino can't be zero */
    194 				LFS_IENTRY(ifp, fs, plastino, bp);
    195 				ifp->if_nextfree = 0;
    196 				VOP_BWRITE(bp);
    197 			}
    198 			break;
    199 		}
    200 		++visited[ino];
    201 		LFS_IENTRY(ifp, fs, ino, bp);
    202 		nextino = ifp->if_nextfree;
    203 		daddr = ifp->if_daddr;
    204 		brelse(bp);
    205 		if (daddr) {
    206 			pwarn("! Ino %d with daddr 0x%llx is on the free list!\n",
    207 			    ino, (long long) daddr);
    208 			if (preen || reply("FIX") == 1) {
    209 				if (plastino == 0) {
    210 					fs->lfs_freehd = nextino;
    211 					sbdirty();
    212 				} else {
    213 					LFS_IENTRY(ifp, fs, plastino, bp);
    214 					ifp->if_nextfree = nextino;
    215 					VOP_BWRITE(bp);
    216 				}
    217 				ino = nextino;
    218 				continue;
    219 			}
    220 		}
    221 		plastino = ino;
    222 		ino = nextino;
    223 	}
    224 
    225 
    226 	/*
    227 	 * Make sure all free inodes were found on the list
    228 	 */
    229 	for (ino = ROOTINO + 1; ino < maxino; ++ino) {
    230 		if (visited[ino])
    231 			continue;
    232 
    233 		LFS_IENTRY(ifp, fs, ino, bp);
    234 		if (ifp->if_daddr) {
    235 			brelse(bp);
    236 			continue;
    237 		}
    238 		pwarn("! Ino %d free, but not on the free list\n", ino);
    239 		if (preen || reply("FIX") == 1) {
    240 			ifp->if_nextfree = fs->lfs_freehd;
    241 			fs->lfs_freehd = ino;
    242 			sbdirty();
    243 			VOP_BWRITE(bp);
    244 		} else
    245 			brelse(bp);
    246 	}
    247 
    248 	LFS_CLEANERINFO(cip, fs, bp);
    249 	if (cip->free_head != fs->lfs_freehd) {
    250 		pwarn("! Free list head should be %d (was %d)\n",
    251 			fs->lfs_freehd, cip->free_head);
    252 		if (preen || reply("FIX")) {
    253 			cip->free_head = fs->lfs_freehd;
    254 			writeit = 1;
    255 		}
    256 	}
    257 	if (cip->free_tail != plastino) {
    258 		pwarn("! Free list tail should be %d (was %d)\n", plastino,
    259 			cip->free_tail);
    260 		if (preen || reply("FIX")) {
    261 			cip->free_tail = plastino;
    262 			writeit = 1;
    263 		}
    264 	}
    265 	if (writeit)
    266 		LFS_SYNC_CLEANERINFO(cip, fs, bp, writeit);
    267 	else
    268 		brelse(bp);
    269 
    270 	if (fs->lfs_freehd == 0) {
    271 		pwarn("%sree list head is 0x0\n", preen ? "f" : "F");
    272 		if (preen || reply("FIX"))
    273 			extend_ifile();
    274 	}
    275 
    276 	/*
    277 	 * Check the distance between sequential free list entries.
    278 	 * An ideally ordered free list will have the sum of these
    279 	 * distances <= the number of inodes in the inode list.
    280 	 * If the observed distance is too high, reorder the list.
    281 	 * Strictly speaking, this is not an error, but it optimizes the
    282 	 * speed in creation of files and should help a tiny bit with
    283 	 * cleaner thrash as well.
    284 	 */
    285 	if (totaldist > 4 * maxino) {
    286 		pwarn("%sotal inode list traversal length %lldx list length%s\n",
    287 		      (preen ? "t" : "T"), totaldist/maxino,
    288 		      (preen ? ", optimizing" : ""));
    289 		if (preen || reply("OPTIMIZE") == 1) {
    290 			plastino = lowfreeino;
    291 			for (ino = lowfreeino + 1; ino < maxino; ino++) {
    292 				LFS_IENTRY(ifp, fs, ino, bp);
    293 				daddr = ifp->if_daddr;
    294 				brelse(bp);
    295 				if (daddr == 0) {
    296 					LFS_IENTRY(ifp, fs, plastino, bp);
    297 					ifp->if_nextfree = ino;
    298 					VOP_BWRITE(bp);
    299 					plastino = ino;
    300 				}
    301 			}
    302 			LFS_CLEANERINFO(cip, fs, bp);
    303 			cip->free_head = fs->lfs_freehd = lowfreeino;
    304 			cip->free_tail = plastino;
    305 			LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
    306 		}
    307 	}
    308 }
    309