Home | History | Annotate | Line # | Download | only in fsck_lfs
pass0.c revision 1.14
      1 /* $NetBSD: pass0.c,v 1.14 2003/03/28 08:09:53 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. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  */
     70 
     71 #include <sys/types.h>
     72 #include <sys/param.h>
     73 #include <sys/time.h>
     74 #include <sys/buf.h>
     75 #include <sys/mount.h>
     76 
     77 #include <ufs/ufs/inode.h>
     78 #include <ufs/ufs/dir.h>
     79 #define vnode uvnode
     80 #include <ufs/lfs/lfs.h>
     81 #undef vnode
     82 
     83 #include <stdio.h>
     84 #include <stdlib.h>
     85 #include <string.h>
     86 
     87 #include "bufcache.h"
     88 #include "vnode.h"
     89 #include "lfs.h"
     90 
     91 #include "fsck.h"
     92 #include "extern.h"
     93 #include "fsutil.h"
     94 
     95 extern int fake_cleanseg;
     96 
     97 /*
     98  * Pass 0.  Check the LFS partial segments for valid checksums, correcting
     99  * if necessary.  Also check for valid offsets for inode and finfo blocks.
    100  */
    101 /*
    102  * XXX more could be done here---consistency between inode-held blocks and
    103  * finfo blocks, for one thing.
    104  */
    105 
    106 #define dbshift (fs->lfs_bshift - fs->lfs_blktodb)
    107 
    108 void
    109 pass0()
    110 {
    111 	daddr_t daddr;
    112 	IFILE *ifp;
    113 	struct ubuf *bp;
    114 	ino_t ino, plastino, nextino, *visited;
    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 	memset(visited, 0, maxino * sizeof(ino_t));
    122 
    123 	plastino = 0;
    124 	ino = fs->lfs_freehd;
    125 	while (ino) {
    126 		if (ino >= maxino) {
    127 			printf("! Ino %d out of range (last was %d)\n", ino,
    128 			    plastino);
    129 			break;
    130 		}
    131 		if (visited[ino]) {
    132 			pwarn("! Ino %d already found on the free list!\n",
    133 			    ino);
    134 			if (preen || reply("FIX") == 1) {
    135 				/* plastino can't be zero */
    136 				LFS_IENTRY(ifp, fs, plastino, bp);
    137 				ifp->if_nextfree = 0;
    138 				VOP_BWRITE(bp);
    139 			}
    140 			break;
    141 		}
    142 		++visited[ino];
    143 		LFS_IENTRY(ifp, fs, ino, bp);
    144 		nextino = ifp->if_nextfree;
    145 		daddr = ifp->if_daddr;
    146 		brelse(bp);
    147 		if (daddr) {
    148 			pwarn("! Ino %d with daddr 0x%llx is on the free list!\n",
    149 			    ino, (long long) daddr);
    150 			if (preen || reply("FIX") == 1) {
    151 				if (plastino == 0) {
    152 					fs->lfs_freehd = nextino;
    153 					sbdirty();
    154 				} else {
    155 					LFS_IENTRY(ifp, fs, plastino, bp);
    156 					ifp->if_nextfree = nextino;
    157 					VOP_BWRITE(bp);
    158 				}
    159 				ino = nextino;
    160 				continue;
    161 			}
    162 		}
    163 		plastino = ino;
    164 		ino = nextino;
    165 	}
    166 	/*
    167 	 * Make sure all free inodes were found on the list
    168 	 */
    169 	for (ino = ROOTINO + 1; ino < maxino; ++ino) {
    170 		if (visited[ino])
    171 			continue;
    172 
    173 		LFS_IENTRY(ifp, fs, ino, bp);
    174 		if (ifp->if_daddr) {
    175 			brelse(bp);
    176 			continue;
    177 		}
    178 		pwarn("! Ino %d free, but not on the free list\n", ino);
    179 		if (preen || reply("FIX") == 1) {
    180 			ifp->if_nextfree = fs->lfs_freehd;
    181 			fs->lfs_freehd = ino;
    182 			sbdirty();
    183 			VOP_BWRITE(bp);
    184 		} else
    185 			brelse(bp);
    186 	}
    187 }
    188