xref: /netbsd-src/sbin/fsck_lfs/pass0.c (revision bf1e9b32e27832f0c493206710fb8b58a980838a)
1 /* $NetBSD: pass0.c,v 1.21 2005/06/08 19:09:55 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@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(void)
106 {
107 	daddr_t daddr;
108 	CLEANERINFO *cip;
109 	IFILE *ifp;
110 	struct ubuf *bp;
111 	ino_t ino, plastino, nextino, *visited, lowfreeino;
112 	int writeit = 0;
113 	int count;
114 	long long totaldist;
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 #ifdef BAD
124 	/*
125 	 * Scramble the free list, to trigger the optimizer below.
126 	 * You don't want this unless you are debugging fsck_lfs itself.
127 	 */
128 	if (!preen && reply("SCRAMBLE FREE LIST") == 1) {
129 		ino_t topino, botino, tail;
130 		botino = 0;
131 		topino = maxino;
132 		while (botino < topino) {
133 			for (--topino; botino < topino; --topino) {
134 				LFS_IENTRY(ifp, fs, topino, bp);
135 				if (ifp->if_daddr == 0)
136 					break;
137 				brelse(bp);
138 				bp = NULL;
139 			}
140 			if (topino == botino)
141 				break;
142 			if (botino > 0) {
143 				ifp->if_nextfree = botino;
144 			} else {
145 				ifp->if_nextfree = 0x0;
146 				tail = topino;
147 			}
148 			VOP_BWRITE(bp);
149 			ino = topino;
150 
151 			for (++botino; botino < topino; ++botino) {
152 				LFS_IENTRY(ifp, fs, botino, bp);
153 				if (ifp->if_daddr == 0)
154 					break;
155 				brelse(bp);
156 				bp = NULL;
157 			}
158 			if (topino == botino)
159 				break;
160 			ifp->if_nextfree = topino;
161 			VOP_BWRITE(bp);
162 			ino = botino;
163 		}
164 		LFS_CLEANERINFO(cip, fs, bp);
165 		cip->free_head = fs->lfs_freehd = ino;
166 		cip->free_tail = tail;
167 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
168 	}
169 #endif /* BAD */
170 
171 	count = 0;
172 	plastino = 0;
173 	totaldist = 0;
174 	lowfreeino = maxino;
175 	ino = fs->lfs_freehd;
176 	while (ino) {
177 		if (lowfreeino > ino)
178 			lowfreeino = ino;
179 		if (plastino > 0) {
180 			totaldist += abs(ino - plastino);
181 			++count;
182 		}
183 		if (ino >= maxino) {
184 			printf("! Ino %d out of range (last was %d)\n", ino,
185 			    plastino);
186 			break;
187 		}
188 		if (visited[ino]) {
189 			pwarn("! Ino %d already found on the free list!\n",
190 			    ino);
191 			if (preen || reply("FIX") == 1) {
192 				/* plastino can't be zero */
193 				LFS_IENTRY(ifp, fs, plastino, bp);
194 				ifp->if_nextfree = 0;
195 				VOP_BWRITE(bp);
196 			}
197 			break;
198 		}
199 		++visited[ino];
200 		LFS_IENTRY(ifp, fs, ino, bp);
201 		nextino = ifp->if_nextfree;
202 		daddr = ifp->if_daddr;
203 		brelse(bp);
204 		if (daddr) {
205 			pwarn("! Ino %d with daddr 0x%llx is on the free list!\n",
206 			    ino, (long long) daddr);
207 			if (preen || reply("FIX") == 1) {
208 				if (plastino == 0) {
209 					fs->lfs_freehd = nextino;
210 					sbdirty();
211 				} else {
212 					LFS_IENTRY(ifp, fs, plastino, bp);
213 					ifp->if_nextfree = nextino;
214 					VOP_BWRITE(bp);
215 				}
216 				ino = nextino;
217 				continue;
218 			}
219 		}
220 		plastino = ino;
221 		ino = nextino;
222 	}
223 
224 
225 	/*
226 	 * Make sure all free inodes were found on the list
227 	 */
228 	for (ino = ROOTINO + 1; ino < maxino; ++ino) {
229 		if (visited[ino])
230 			continue;
231 
232 		LFS_IENTRY(ifp, fs, ino, bp);
233 		if (ifp->if_daddr) {
234 			brelse(bp);
235 			continue;
236 		}
237 		pwarn("! Ino %d free, but not on the free list\n", ino);
238 		if (preen || reply("FIX") == 1) {
239 			ifp->if_nextfree = fs->lfs_freehd;
240 			fs->lfs_freehd = ino;
241 			sbdirty();
242 			VOP_BWRITE(bp);
243 		} else
244 			brelse(bp);
245 	}
246 
247 	LFS_CLEANERINFO(cip, fs, bp);
248 	if (cip->free_head != fs->lfs_freehd) {
249 		pwarn("! Free list head should be %d (was %d)\n",
250 			fs->lfs_freehd, cip->free_head);
251 		if (preen || reply("FIX")) {
252 			cip->free_head = fs->lfs_freehd;
253 			writeit = 1;
254 		}
255 	}
256 	if (cip->free_tail != plastino) {
257 		pwarn("! Free list tail should be %d (was %d)\n", plastino,
258 			cip->free_tail);
259 		if (preen || reply("FIX")) {
260 			cip->free_tail = plastino;
261 			writeit = 1;
262 		}
263 	}
264 	if (writeit)
265 		LFS_SYNC_CLEANERINFO(cip, fs, bp, writeit);
266 	else
267 		brelse(bp);
268 
269 	if (fs->lfs_freehd == 0) {
270 		pwarn("%sree list head is 0x0\n", preen ? "f" : "F");
271 		if (preen || reply("FIX")) {
272 			extend_ifile(fs);
273 			reset_maxino(((VTOI(fs->lfs_ivnode)->i_ffs1_size >>
274 				       fs->lfs_bsize) -
275 				      fs->lfs_segtabsz - fs->lfs_cleansz) *
276 				     fs->lfs_ifpb);
277 		}
278 	}
279 
280 	/*
281 	 * Check the distance between sequential free list entries.
282 	 * An ideally ordered free list will have the sum of these
283 	 * distances <= the number of inodes in the inode list.
284 	 * If the observed distance is too high, reorder the list.
285 	 * Strictly speaking, this is not an error, but it optimizes the
286 	 * speed in creation of files and should help a tiny bit with
287 	 * cleaner thrash as well.
288 	 */
289 	if (totaldist > 4 * maxino) {
290 		pwarn("%sotal inode list traversal length %lldx list length%s\n",
291 		      (preen ? "t" : "T"), totaldist/maxino,
292 		      (preen ? ", optimizing" : ""));
293 		if (preen || reply("OPTIMIZE") == 1) {
294 			plastino = lowfreeino;
295 			for (ino = lowfreeino + 1; ino < maxino; ino++) {
296 				LFS_IENTRY(ifp, fs, ino, bp);
297 				daddr = ifp->if_daddr;
298 				brelse(bp);
299 				if (daddr == 0) {
300 					LFS_IENTRY(ifp, fs, plastino, bp);
301 					ifp->if_nextfree = ino;
302 					VOP_BWRITE(bp);
303 					plastino = ino;
304 				}
305 			}
306 			LFS_CLEANERINFO(cip, fs, bp);
307 			cip->free_head = fs->lfs_freehd = lowfreeino;
308 			cip->free_tail = plastino;
309 			LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
310 		}
311 	}
312 }
313