xref: /netbsd-src/sbin/fsck_lfs/pass0.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /* $NetBSD: pass0.c,v 1.16 2003/08/07 10:04:23 agc 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@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 
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()
106 {
107 	daddr_t daddr;
108 	CLEANERINFO *cip;
109 	IFILE *ifp;
110 	struct ubuf *bp;
111 	ino_t ino, plastino, nextino, *visited;
112 	int writeit = 0;
113 
114 	/*
115          * Check the inode free list for inuse inodes, and cycles.
116 	 * Make sure that all free inodes are in fact on the list.
117          */
118 	visited = (ino_t *) malloc(maxino * sizeof(ino_t));
119 	memset(visited, 0, maxino * sizeof(ino_t));
120 
121 	plastino = 0;
122 	ino = fs->lfs_freehd;
123 	while (ino) {
124 		if (ino >= maxino) {
125 			printf("! Ino %d out of range (last was %d)\n", ino,
126 			    plastino);
127 			break;
128 		}
129 		if (visited[ino]) {
130 			pwarn("! Ino %d already found on the free list!\n",
131 			    ino);
132 			if (preen || reply("FIX") == 1) {
133 				/* plastino can't be zero */
134 				LFS_IENTRY(ifp, fs, plastino, bp);
135 				ifp->if_nextfree = 0;
136 				VOP_BWRITE(bp);
137 			}
138 			break;
139 		}
140 		++visited[ino];
141 		LFS_IENTRY(ifp, fs, ino, bp);
142 		nextino = ifp->if_nextfree;
143 		daddr = ifp->if_daddr;
144 		brelse(bp);
145 		if (daddr) {
146 			pwarn("! Ino %d with daddr 0x%llx is on the free list!\n",
147 			    ino, (long long) daddr);
148 			if (preen || reply("FIX") == 1) {
149 				if (plastino == 0) {
150 					fs->lfs_freehd = nextino;
151 					sbdirty();
152 				} else {
153 					LFS_IENTRY(ifp, fs, plastino, bp);
154 					ifp->if_nextfree = nextino;
155 					VOP_BWRITE(bp);
156 				}
157 				ino = nextino;
158 				continue;
159 			}
160 		}
161 		plastino = ino;
162 		ino = nextino;
163 	}
164 
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 	LFS_CLEANERINFO(cip, fs, bp);
189 	if (cip->free_head != fs->lfs_freehd) {
190 		pwarn("! Free list head should be %d (was %d)\n",
191 			fs->lfs_freehd, cip->free_head);
192 		if (preen || reply("FIX")) {
193 			cip->free_head = fs->lfs_freehd;
194 			writeit = 1;
195 		}
196 	}
197 	if (cip->free_tail != plastino) {
198 		pwarn("! Free list tail should be %d (was %d)\n", plastino,
199 			cip->free_tail);
200 		if (preen || reply("FIX")) {
201 			cip->free_tail = plastino;
202 			writeit = 1;
203 		}
204 	}
205 	LFS_SYNC_CLEANERINFO(cip, fs, bp, writeit);
206 }
207