Home | History | Annotate | Line # | Download | only in fsck_lfs
pass0.c revision 1.28
      1 /* $NetBSD: pass0.c,v 1.28 2006/07/18 23:37:13 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 <assert.h>
     80 #include <err.h>
     81 #include <stdio.h>
     82 #include <stdlib.h>
     83 #include <string.h>
     84 
     85 #include "bufcache.h"
     86 #include "vnode.h"
     87 #include "lfs_user.h"
     88 
     89 #include "fsck.h"
     90 #include "extern.h"
     91 #include "fsutil.h"
     92 
     93 extern int fake_cleanseg;
     94 
     95 /*
     96  * Pass 0.  Check the LFS partial segments for valid checksums, correcting
     97  * if necessary.  Also check for valid offsets for inode and finfo blocks.
     98  */
     99 /*
    100  * XXX more could be done here---consistency between inode-held blocks and
    101  * finfo blocks, for one thing.
    102  */
    103 
    104 #define dbshift (fs->lfs_bshift - fs->lfs_blktodb)
    105 
    106 void
    107 pass0(void)
    108 {
    109 	daddr_t daddr;
    110 	CLEANERINFO *cip;
    111 	IFILE *ifp;
    112 	struct ubuf *bp, *cbp;
    113 	ino_t ino, plastino, nextino, lowfreeino, freehd;
    114 	char *visited;
    115 	int writeit = 0;
    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 = (char *) malloc(maxino * sizeof(*visited));
    122 	if (visited == NULL)
    123 		err(1, NULL);
    124 	memset(visited, 0, maxino * sizeof(*visited));
    125 
    126 	plastino = 0;
    127 	lowfreeino = maxino;
    128 	LFS_CLEANERINFO(cip, fs, cbp);
    129 	freehd = ino = cip->free_head;
    130 	brelse(cbp);
    131 
    132 	while (ino) {
    133 		if (lowfreeino > ino)
    134 			lowfreeino = ino;
    135 		if (ino >= maxino) {
    136 			pwarn("OUT OF RANGE INO %llu ON FREE LIST\n",
    137 			    (unsigned long long)ino);
    138 			break;
    139 		}
    140 		if (visited[ino]) {
    141 			pwarn("INO %llu ALREADY FOUND ON FREE LIST\n",
    142 			    (unsigned long long)ino);
    143 			if (preen || reply("FIX") == 1) {
    144 				/* plastino can't be zero */
    145 				LFS_IENTRY(ifp, fs, plastino, bp);
    146 				ifp->if_nextfree = 0;
    147 				VOP_BWRITE(bp);
    148 			}
    149 			break;
    150 		}
    151 		visited[ino] = 1;
    152 		LFS_IENTRY(ifp, fs, ino, bp);
    153 		nextino = ifp->if_nextfree;
    154 		daddr = ifp->if_daddr;
    155 		brelse(bp);
    156 		if (daddr) {
    157 			pwarn("INO %llu WITH DADDR 0x%llx ON FREE LIST\n",
    158 			    (unsigned long long)ino, (long long) daddr);
    159 			if (preen || reply("FIX") == 1) {
    160 				if (plastino == 0) {
    161 					freehd = nextino;
    162 					sbdirty();
    163 				} else {
    164 					LFS_IENTRY(ifp, fs, plastino, bp);
    165 					ifp->if_nextfree = nextino;
    166 					VOP_BWRITE(bp);
    167 				}
    168 				ino = nextino;
    169 				continue;
    170 			}
    171 		}
    172 		plastino = ino;
    173 		ino = nextino;
    174 	}
    175 
    176 
    177 	/*
    178 	 * Make sure all free inodes were found on the list
    179 	 */
    180 	for (ino = maxino - 1; ino > ROOTINO; --ino) {
    181 		if (visited[ino])
    182 			continue;
    183 
    184 		LFS_IENTRY(ifp, fs, ino, bp);
    185 		if (ifp->if_daddr) {
    186 			brelse(bp);
    187 			continue;
    188 		}
    189 		pwarn("INO %llu FREE BUT NOT ON FREE LIST\n",
    190 		    (unsigned long long)ino);
    191 		if (preen || reply("FIX") == 1) {
    192 			assert(ino != freehd);
    193 			ifp->if_nextfree = freehd;
    194 			VOP_BWRITE(bp);
    195 
    196 			freehd = ino;
    197 			sbdirty();
    198 
    199 			/* If freelist was empty, this is the tail */
    200 			if (plastino == 0)
    201 				plastino = ino;
    202 		} else
    203 			brelse(bp);
    204 	}
    205 
    206 	LFS_CLEANERINFO(cip, fs, cbp);
    207 	if (cip->free_head != freehd) {
    208 		/* They've already given us permission for this change */
    209 		cip->free_head = freehd;
    210 		writeit = 1;
    211 	}
    212 	if (freehd != fs->lfs_freehd) {
    213 		pwarn("FREE LIST HEAD IN SUPERBLOCK SHOULD BE %d (WAS %d)\n",
    214 			(int)fs->lfs_freehd, (int)freehd);
    215 		if (preen || reply("FIX")) {
    216 			fs->lfs_freehd = freehd;
    217 			sbdirty();
    218 		}
    219 	}
    220 	if (cip->free_tail != plastino) {
    221 		pwarn("FREE LIST TAIL SHOULD BE %llu (WAS %llu)\n",
    222 		    (unsigned long long)plastino,
    223 		    (unsigned long long)cip->free_tail);
    224 		if (preen || reply("FIX")) {
    225 			cip->free_tail = plastino;
    226 			writeit = 1;
    227 		}
    228 	}
    229 
    230 	if (writeit)
    231 		LFS_SYNC_CLEANERINFO(cip, fs, cbp, writeit);
    232 	else
    233 		brelse(cbp);
    234 
    235 	if (fs->lfs_freehd == 0) {
    236 		pwarn("%sree list head is 0x0\n", preen ? "f" : "F");
    237 		if (preen || reply("FIX"))
    238 			extend_ifile(fs);
    239 	}
    240 }
    241