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