xref: /netbsd-src/sbin/fsck_lfs/pass0.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /* $NetBSD: pass0.c,v 1.35 2013/06/08 02:16:03 dholland 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*-
32  * Copyright (c) 1980, 1986, 1993
33  *	The Regents of the University of California.  All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. Neither the name of the University nor the names of its contributors
44  *    may be used to endorse or promote products derived from this software
45  *    without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  */
59 
60 #include <sys/types.h>
61 #include <sys/param.h>
62 #include <sys/time.h>
63 #include <sys/buf.h>
64 #include <sys/mount.h>
65 
66 #define vnode uvnode
67 #include <ufs/lfs/lfs.h>
68 #include <ufs/lfs/lfs_inode.h>
69 #undef vnode
70 
71 #include <assert.h>
72 #include <err.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <util.h>
77 
78 #include "bufcache.h"
79 #include "vnode.h"
80 #include "lfs_user.h"
81 
82 #include "fsck.h"
83 #include "extern.h"
84 #include "fsutil.h"
85 
86 extern int fake_cleanseg;
87 
88 /*
89  * Pass 0.  Check the LFS partial segments for valid checksums, correcting
90  * if necessary.  Also check for valid offsets for inode and finfo blocks.
91  */
92 /*
93  * XXX more could be done here---consistency between inode-held blocks and
94  * finfo blocks, for one thing.
95  */
96 
97 #define dbshift (fs->lfs_bshift - fs->lfs_blktodb)
98 
99 void
100 pass0(void)
101 {
102 	daddr_t daddr;
103 	CLEANERINFO *cip;
104 	IFILE *ifp;
105 	struct ubuf *bp, *cbp;
106 	ino_t ino, plastino, nextino, lowfreeino, freehd;
107 	char *visited;
108 	int writeit = 0;
109 
110 	/*
111 	 * Check the inode free list for inuse inodes, and cycles.
112 	 * Make sure that all free inodes are in fact on the list.
113 	 */
114 	visited = ecalloc(maxino, sizeof(*visited));
115 	plastino = 0;
116 	lowfreeino = maxino;
117 	LFS_CLEANERINFO(cip, fs, cbp);
118 	freehd = ino = cip->free_head;
119 	brelse(cbp, 0);
120 
121 	while (ino) {
122 		if (lowfreeino > ino)
123 			lowfreeino = ino;
124 		if (ino >= maxino) {
125 			pwarn("OUT OF RANGE INO %llu ON FREE LIST\n",
126 			    (unsigned long long)ino);
127 			break;
128 		}
129 		if (visited[ino]) {
130 			pwarn("INO %llu ALREADY FOUND ON FREE LIST\n",
131 			    (unsigned long long)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] = 1;
141 		LFS_IENTRY(ifp, fs, ino, bp);
142 		nextino = ifp->if_nextfree;
143 		daddr = ifp->if_daddr;
144 		brelse(bp, 0);
145 		if (daddr) {
146 			pwarn("INO %llu WITH DADDR 0x%llx ON FREE LIST\n",
147 			    (unsigned long long)ino, (long long) daddr);
148 			if (preen || reply("FIX") == 1) {
149 				if (plastino == 0) {
150 					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 = maxino - 1; ino > ULFS_ROOTINO; --ino) {
170 		if (visited[ino])
171 			continue;
172 
173 		LFS_IENTRY(ifp, fs, ino, bp);
174 		if (ifp->if_daddr) {
175 			brelse(bp, 0);
176 			continue;
177 		}
178 		pwarn("INO %llu FREE BUT NOT ON FREE LIST\n",
179 		    (unsigned long long)ino);
180 		if (preen || reply("FIX") == 1) {
181 			assert(ino != freehd);
182 			ifp->if_nextfree = freehd;
183 			VOP_BWRITE(bp);
184 
185 			freehd = ino;
186 			sbdirty();
187 
188 			/* If freelist was empty, this is the tail */
189 			if (plastino == 0)
190 				plastino = ino;
191 		} else
192 			brelse(bp, 0);
193 	}
194 
195 	LFS_CLEANERINFO(cip, fs, cbp);
196 	if (cip->free_head != freehd) {
197 		/* They've already given us permission for this change */
198 		cip->free_head = freehd;
199 		writeit = 1;
200 	}
201 	if (freehd != fs->lfs_freehd) {
202 		pwarn("FREE LIST HEAD IN SUPERBLOCK SHOULD BE %d (WAS %d)\n",
203 			(int)fs->lfs_freehd, (int)freehd);
204 		if (preen || reply("FIX")) {
205 			fs->lfs_freehd = freehd;
206 			sbdirty();
207 		}
208 	}
209 	if (cip->free_tail != plastino) {
210 		pwarn("FREE LIST TAIL SHOULD BE %llu (WAS %llu)\n",
211 		    (unsigned long long)plastino,
212 		    (unsigned long long)cip->free_tail);
213 		if (preen || reply("FIX")) {
214 			cip->free_tail = plastino;
215 			writeit = 1;
216 		}
217 	}
218 
219 	if (writeit)
220 		LFS_SYNC_CLEANERINFO(cip, fs, cbp, writeit);
221 	else
222 		brelse(cbp, 0);
223 
224 	if (fs->lfs_freehd == 0) {
225 		pwarn("%sree list head is 0x0\n", preen ? "f" : "F");
226 		if (preen || reply("FIX"))
227 			extend_ifile(fs);
228 	}
229 }
230