pass0.c revision 1.26 1 /* $NetBSD: pass0.c,v 1.26 2006/03/17 19:24:08 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. 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 <err.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <string.h>
83
84 #include "bufcache.h"
85 #include "vnode.h"
86 #include "lfs_user.h"
87
88 #include "fsck.h"
89 #include "extern.h"
90 #include "fsutil.h"
91
92 extern int fake_cleanseg;
93
94 /*
95 * Pass 0. Check the LFS partial segments for valid checksums, correcting
96 * if necessary. Also check for valid offsets for inode and finfo blocks.
97 */
98 /*
99 * XXX more could be done here---consistency between inode-held blocks and
100 * finfo blocks, for one thing.
101 */
102
103 #define dbshift (fs->lfs_bshift - fs->lfs_blktodb)
104
105 void
106 pass0(void)
107 {
108 daddr_t daddr;
109 CLEANERINFO *cip;
110 IFILE *ifp;
111 struct ubuf *bp;
112 ino_t ino, plastino, nextino, *visited, lowfreeino;
113 int writeit = 0;
114 int count;
115 long long totaldist;
116
117 /*
118 * Check the inode free list for inuse inodes, and cycles.
119 * Make sure that all free inodes are in fact on the list.
120 */
121 visited = (ino_t *) malloc(maxino * sizeof(ino_t));
122 if (visited == NULL)
123 err(1, NULL);
124 memset(visited, 0, maxino * sizeof(ino_t));
125
126 #ifdef BAD
127 /*
128 * Scramble the free list, to trigger the optimizer below.
129 * You don't want this unless you are debugging fsck_lfs itself.
130 */
131 if (!preen && reply("SCRAMBLE FREE LIST") == 1) {
132 ino_t topino, botino, tail;
133 botino = 0;
134 topino = maxino;
135 while (botino < topino) {
136 for (--topino; botino < topino; --topino) {
137 LFS_IENTRY(ifp, fs, topino, bp);
138 if (ifp->if_daddr == 0)
139 break;
140 brelse(bp);
141 bp = NULL;
142 }
143 if (topino == botino)
144 break;
145 if (botino > 0) {
146 ifp->if_nextfree = botino;
147 } else {
148 ifp->if_nextfree = 0x0;
149 tail = topino;
150 }
151 VOP_BWRITE(bp);
152 ino = topino;
153
154 for (++botino; botino < topino; ++botino) {
155 LFS_IENTRY(ifp, fs, botino, bp);
156 if (ifp->if_daddr == 0)
157 break;
158 brelse(bp);
159 bp = NULL;
160 }
161 if (topino == botino)
162 break;
163 ifp->if_nextfree = topino;
164 VOP_BWRITE(bp);
165 ino = botino;
166 }
167 LFS_CLEANERINFO(cip, fs, bp);
168 cip->free_head = fs->lfs_freehd = ino;
169 cip->free_tail = tail;
170 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
171 }
172 #endif /* BAD */
173
174 count = 0;
175 plastino = 0;
176 totaldist = 0;
177 lowfreeino = maxino;
178 ino = fs->lfs_freehd;
179 while (ino) {
180 if (lowfreeino > ino)
181 lowfreeino = ino;
182 if (plastino > 0) {
183 totaldist += abs(ino - plastino);
184 ++count;
185 }
186 if (ino >= maxino) {
187 printf("! Ino %llu out of range (last was %llu)\n",
188 (unsigned long long)ino,
189 (unsigned long long)plastino);
190 break;
191 }
192 if (visited[ino]) {
193 pwarn("! Ino %llu already found on the free list!\n",
194 (unsigned long long)ino);
195 if (preen || reply("FIX") == 1) {
196 /* plastino can't be zero */
197 LFS_IENTRY(ifp, fs, plastino, bp);
198 ifp->if_nextfree = 0;
199 VOP_BWRITE(bp);
200 }
201 break;
202 }
203 ++visited[ino];
204 LFS_IENTRY(ifp, fs, ino, bp);
205 nextino = ifp->if_nextfree;
206 daddr = ifp->if_daddr;
207 brelse(bp);
208 if (daddr) {
209 pwarn("! Ino %llu with daddr 0x%llx is on the "
210 "free list!\n",
211 (unsigned long long)ino, (long long) daddr);
212 if (preen || reply("FIX") == 1) {
213 if (plastino == 0) {
214 fs->lfs_freehd = nextino;
215 sbdirty();
216 } else {
217 LFS_IENTRY(ifp, fs, plastino, bp);
218 ifp->if_nextfree = nextino;
219 VOP_BWRITE(bp);
220 }
221 ino = nextino;
222 continue;
223 }
224 }
225 plastino = ino;
226 ino = nextino;
227 }
228
229
230 /*
231 * Make sure all free inodes were found on the list
232 */
233 for (ino = ROOTINO + 1; ino < maxino; ++ino) {
234 if (visited[ino])
235 continue;
236
237 LFS_IENTRY(ifp, fs, ino, bp);
238 if (ifp->if_daddr) {
239 brelse(bp);
240 continue;
241 }
242 pwarn("! Ino %llu free, but not on the free list\n",
243 (unsigned long long)ino);
244 if (preen || reply("FIX") == 1) {
245 ifp->if_nextfree = fs->lfs_freehd;
246 fs->lfs_freehd = ino;
247 sbdirty();
248 VOP_BWRITE(bp);
249 } else
250 brelse(bp);
251 }
252
253 LFS_CLEANERINFO(cip, fs, bp);
254 if (cip->free_head != fs->lfs_freehd) {
255 pwarn("! Free list head should be %d (was %d)\n",
256 fs->lfs_freehd, cip->free_head);
257 if (preen || reply("FIX")) {
258 cip->free_head = fs->lfs_freehd;
259 writeit = 1;
260 }
261 }
262 if (cip->free_tail != plastino) {
263 pwarn("! Free list tail should be %llu (was %d)\n",
264 (unsigned long long)plastino, cip->free_tail);
265 if (preen || reply("FIX")) {
266 cip->free_tail = plastino;
267 writeit = 1;
268 }
269 }
270 if (writeit)
271 LFS_SYNC_CLEANERINFO(cip, fs, bp, writeit);
272 else
273 brelse(bp);
274
275 if (fs->lfs_freehd == 0) {
276 pwarn("%sree list head is 0x0\n", preen ? "f" : "F");
277 if (preen || reply("FIX")) {
278 extend_ifile(fs);
279 reset_maxino(((VTOI(fs->lfs_ivnode)->i_ffs1_size >>
280 fs->lfs_bsize) -
281 fs->lfs_segtabsz - fs->lfs_cleansz) *
282 fs->lfs_ifpb);
283 }
284 }
285
286 /*
287 * Check the distance between sequential free list entries.
288 * An ideally ordered free list will have the sum of these
289 * distances <= the number of inodes in the inode list.
290 * If the observed distance is too high, reorder the list.
291 * Strictly speaking, this is not an error, but it optimizes the
292 * speed in creation of files and should help a tiny bit with
293 * cleaner thrash as well.
294 */
295 if (totaldist > 4 * maxino) {
296 pwarn("%sotal inode list traversal length %" PRIu64 "x list length%s\n",
297 (preen ? "t" : "T"), totaldist/maxino,
298 (preen ? ", optimizing" : ""));
299 if (preen || reply("OPTIMIZE") == 1) {
300 plastino = lowfreeino;
301 for (ino = lowfreeino + 1; ino < maxino; ino++) {
302 LFS_IENTRY(ifp, fs, ino, bp);
303 daddr = ifp->if_daddr;
304 brelse(bp);
305 if (daddr == 0) {
306 LFS_IENTRY(ifp, fs, plastino, bp);
307 ifp->if_nextfree = ino;
308 VOP_BWRITE(bp);
309 plastino = ino;
310 }
311 }
312 LFS_CLEANERINFO(cip, fs, bp);
313 cip->free_head = fs->lfs_freehd = lowfreeino;
314 cip->free_tail = plastino;
315 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
316 }
317 }
318 }
319