pass0.c revision 1.26 1 1.26 perseant /* $NetBSD: pass0.c,v 1.26 2006/03/17 19:24:08 perseant Exp $ */
2 1.3 kleink
3 1.14 perseant /*-
4 1.14 perseant * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 1.14 perseant * All rights reserved.
6 1.14 perseant *
7 1.14 perseant * This code is derived from software contributed to The NetBSD Foundation
8 1.14 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 1.14 perseant *
10 1.14 perseant * Redistribution and use in source and binary forms, with or without
11 1.14 perseant * modification, are permitted provided that the following conditions
12 1.14 perseant * are met:
13 1.14 perseant * 1. Redistributions of source code must retain the above copyright
14 1.14 perseant * notice, this list of conditions and the following disclaimer.
15 1.14 perseant * 2. Redistributions in binary form must reproduce the above copyright
16 1.14 perseant * notice, this list of conditions and the following disclaimer in the
17 1.14 perseant * documentation and/or other materials provided with the distribution.
18 1.14 perseant * 3. All advertising materials mentioning features or use of this software
19 1.14 perseant * must display the following acknowledgement:
20 1.14 perseant * This product includes software developed by the NetBSD
21 1.14 perseant * Foundation, Inc. and its contributors.
22 1.14 perseant * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.14 perseant * contributors may be used to endorse or promote products derived
24 1.14 perseant * from this software without specific prior written permission.
25 1.14 perseant *
26 1.14 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.14 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.14 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.14 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.14 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.14 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.14 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.14 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.14 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.14 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.14 perseant * POSSIBILITY OF SUCH DAMAGE.
37 1.14 perseant */
38 1.14 perseant /*-
39 1.1 perseant * Copyright (c) 1980, 1986, 1993
40 1.1 perseant * The Regents of the University of California. All rights reserved.
41 1.1 perseant *
42 1.1 perseant * Redistribution and use in source and binary forms, with or without
43 1.1 perseant * modification, are permitted provided that the following conditions
44 1.1 perseant * are met:
45 1.1 perseant * 1. Redistributions of source code must retain the above copyright
46 1.1 perseant * notice, this list of conditions and the following disclaimer.
47 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
48 1.1 perseant * notice, this list of conditions and the following disclaimer in the
49 1.1 perseant * documentation and/or other materials provided with the distribution.
50 1.16 agc * 3. Neither the name of the University nor the names of its contributors
51 1.1 perseant * may be used to endorse or promote products derived from this software
52 1.1 perseant * without specific prior written permission.
53 1.1 perseant *
54 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 1.1 perseant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 1.1 perseant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 1.1 perseant * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 1.1 perseant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 1.1 perseant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 1.1 perseant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 1.1 perseant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 1.1 perseant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 1.1 perseant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 1.1 perseant * SUCH DAMAGE.
65 1.1 perseant */
66 1.1 perseant
67 1.14 perseant #include <sys/types.h>
68 1.1 perseant #include <sys/param.h>
69 1.1 perseant #include <sys/time.h>
70 1.14 perseant #include <sys/buf.h>
71 1.14 perseant #include <sys/mount.h>
72 1.14 perseant
73 1.14 perseant #include <ufs/ufs/inode.h>
74 1.1 perseant #include <ufs/ufs/dir.h>
75 1.14 perseant #define vnode uvnode
76 1.1 perseant #include <ufs/lfs/lfs.h>
77 1.14 perseant #undef vnode
78 1.1 perseant
79 1.26 perseant #include <err.h>
80 1.1 perseant #include <stdio.h>
81 1.1 perseant #include <stdlib.h>
82 1.1 perseant #include <string.h>
83 1.1 perseant
84 1.14 perseant #include "bufcache.h"
85 1.14 perseant #include "vnode.h"
86 1.24 christos #include "lfs_user.h"
87 1.14 perseant
88 1.1 perseant #include "fsck.h"
89 1.1 perseant #include "extern.h"
90 1.1 perseant #include "fsutil.h"
91 1.1 perseant
92 1.14 perseant extern int fake_cleanseg;
93 1.1 perseant
94 1.1 perseant /*
95 1.1 perseant * Pass 0. Check the LFS partial segments for valid checksums, correcting
96 1.1 perseant * if necessary. Also check for valid offsets for inode and finfo blocks.
97 1.1 perseant */
98 1.5 perseant /*
99 1.5 perseant * XXX more could be done here---consistency between inode-held blocks and
100 1.5 perseant * finfo blocks, for one thing.
101 1.5 perseant */
102 1.1 perseant
103 1.14 perseant #define dbshift (fs->lfs_bshift - fs->lfs_blktodb)
104 1.1 perseant
105 1.6 perseant void
106 1.17 xtraeme pass0(void)
107 1.1 perseant {
108 1.14 perseant daddr_t daddr;
109 1.15 perseant CLEANERINFO *cip;
110 1.14 perseant IFILE *ifp;
111 1.14 perseant struct ubuf *bp;
112 1.19 perseant ino_t ino, plastino, nextino, *visited, lowfreeino;
113 1.15 perseant int writeit = 0;
114 1.19 perseant int count;
115 1.19 perseant long long totaldist;
116 1.5 perseant
117 1.5 perseant /*
118 1.6 perseant * Check the inode free list for inuse inodes, and cycles.
119 1.6 perseant * Make sure that all free inodes are in fact on the list.
120 1.5 perseant */
121 1.14 perseant visited = (ino_t *) malloc(maxino * sizeof(ino_t));
122 1.25 rumble if (visited == NULL)
123 1.25 rumble err(1, NULL);
124 1.7 perseant memset(visited, 0, maxino * sizeof(ino_t));
125 1.6 perseant
126 1.19 perseant #ifdef BAD
127 1.19 perseant /*
128 1.19 perseant * Scramble the free list, to trigger the optimizer below.
129 1.19 perseant * You don't want this unless you are debugging fsck_lfs itself.
130 1.19 perseant */
131 1.19 perseant if (!preen && reply("SCRAMBLE FREE LIST") == 1) {
132 1.19 perseant ino_t topino, botino, tail;
133 1.19 perseant botino = 0;
134 1.19 perseant topino = maxino;
135 1.19 perseant while (botino < topino) {
136 1.19 perseant for (--topino; botino < topino; --topino) {
137 1.19 perseant LFS_IENTRY(ifp, fs, topino, bp);
138 1.19 perseant if (ifp->if_daddr == 0)
139 1.19 perseant break;
140 1.19 perseant brelse(bp);
141 1.19 perseant bp = NULL;
142 1.19 perseant }
143 1.19 perseant if (topino == botino)
144 1.19 perseant break;
145 1.19 perseant if (botino > 0) {
146 1.19 perseant ifp->if_nextfree = botino;
147 1.19 perseant } else {
148 1.19 perseant ifp->if_nextfree = 0x0;
149 1.19 perseant tail = topino;
150 1.19 perseant }
151 1.19 perseant VOP_BWRITE(bp);
152 1.19 perseant ino = topino;
153 1.19 perseant
154 1.19 perseant for (++botino; botino < topino; ++botino) {
155 1.19 perseant LFS_IENTRY(ifp, fs, botino, bp);
156 1.19 perseant if (ifp->if_daddr == 0)
157 1.19 perseant break;
158 1.19 perseant brelse(bp);
159 1.19 perseant bp = NULL;
160 1.19 perseant }
161 1.19 perseant if (topino == botino)
162 1.19 perseant break;
163 1.19 perseant ifp->if_nextfree = topino;
164 1.19 perseant VOP_BWRITE(bp);
165 1.19 perseant ino = botino;
166 1.19 perseant }
167 1.19 perseant LFS_CLEANERINFO(cip, fs, bp);
168 1.19 perseant cip->free_head = fs->lfs_freehd = ino;
169 1.19 perseant cip->free_tail = tail;
170 1.19 perseant LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
171 1.19 perseant }
172 1.19 perseant #endif /* BAD */
173 1.19 perseant
174 1.19 perseant count = 0;
175 1.10 lukem plastino = 0;
176 1.19 perseant totaldist = 0;
177 1.19 perseant lowfreeino = maxino;
178 1.14 perseant ino = fs->lfs_freehd;
179 1.5 perseant while (ino) {
180 1.19 perseant if (lowfreeino > ino)
181 1.19 perseant lowfreeino = ino;
182 1.19 perseant if (plastino > 0) {
183 1.19 perseant totaldist += abs(ino - plastino);
184 1.19 perseant ++count;
185 1.19 perseant }
186 1.7 perseant if (ino >= maxino) {
187 1.22 christos printf("! Ino %llu out of range (last was %llu)\n",
188 1.22 christos (unsigned long long)ino,
189 1.22 christos (unsigned long long)plastino);
190 1.7 perseant break;
191 1.7 perseant }
192 1.6 perseant if (visited[ino]) {
193 1.22 christos pwarn("! Ino %llu already found on the free list!\n",
194 1.22 christos (unsigned long long)ino);
195 1.6 perseant if (preen || reply("FIX") == 1) {
196 1.10 lukem /* plastino can't be zero */
197 1.14 perseant LFS_IENTRY(ifp, fs, plastino, bp);
198 1.6 perseant ifp->if_nextfree = 0;
199 1.14 perseant VOP_BWRITE(bp);
200 1.6 perseant }
201 1.6 perseant break;
202 1.6 perseant }
203 1.6 perseant ++visited[ino];
204 1.14 perseant LFS_IENTRY(ifp, fs, ino, bp);
205 1.5 perseant nextino = ifp->if_nextfree;
206 1.5 perseant daddr = ifp->if_daddr;
207 1.14 perseant brelse(bp);
208 1.5 perseant if (daddr) {
209 1.22 christos pwarn("! Ino %llu with daddr 0x%llx is on the "
210 1.22 christos "free list!\n",
211 1.22 christos (unsigned long long)ino, (long long) daddr);
212 1.5 perseant if (preen || reply("FIX") == 1) {
213 1.10 lukem if (plastino == 0) {
214 1.14 perseant fs->lfs_freehd = nextino;
215 1.5 perseant sbdirty();
216 1.5 perseant } else {
217 1.14 perseant LFS_IENTRY(ifp, fs, plastino, bp);
218 1.5 perseant ifp->if_nextfree = nextino;
219 1.14 perseant VOP_BWRITE(bp);
220 1.5 perseant }
221 1.5 perseant ino = nextino;
222 1.5 perseant continue;
223 1.5 perseant }
224 1.5 perseant }
225 1.10 lukem plastino = ino;
226 1.5 perseant ino = nextino;
227 1.6 perseant }
228 1.15 perseant
229 1.15 perseant
230 1.6 perseant /*
231 1.6 perseant * Make sure all free inodes were found on the list
232 1.6 perseant */
233 1.14 perseant for (ino = ROOTINO + 1; ino < maxino; ++ino) {
234 1.6 perseant if (visited[ino])
235 1.6 perseant continue;
236 1.6 perseant
237 1.14 perseant LFS_IENTRY(ifp, fs, ino, bp);
238 1.6 perseant if (ifp->if_daddr) {
239 1.14 perseant brelse(bp);
240 1.6 perseant continue;
241 1.6 perseant }
242 1.22 christos pwarn("! Ino %llu free, but not on the free list\n",
243 1.22 christos (unsigned long long)ino);
244 1.6 perseant if (preen || reply("FIX") == 1) {
245 1.14 perseant ifp->if_nextfree = fs->lfs_freehd;
246 1.14 perseant fs->lfs_freehd = ino;
247 1.6 perseant sbdirty();
248 1.14 perseant VOP_BWRITE(bp);
249 1.14 perseant } else
250 1.14 perseant brelse(bp);
251 1.5 perseant }
252 1.15 perseant
253 1.15 perseant LFS_CLEANERINFO(cip, fs, bp);
254 1.15 perseant if (cip->free_head != fs->lfs_freehd) {
255 1.15 perseant pwarn("! Free list head should be %d (was %d)\n",
256 1.15 perseant fs->lfs_freehd, cip->free_head);
257 1.15 perseant if (preen || reply("FIX")) {
258 1.15 perseant cip->free_head = fs->lfs_freehd;
259 1.15 perseant writeit = 1;
260 1.15 perseant }
261 1.15 perseant }
262 1.15 perseant if (cip->free_tail != plastino) {
263 1.22 christos pwarn("! Free list tail should be %llu (was %d)\n",
264 1.22 christos (unsigned long long)plastino, cip->free_tail);
265 1.15 perseant if (preen || reply("FIX")) {
266 1.15 perseant cip->free_tail = plastino;
267 1.15 perseant writeit = 1;
268 1.15 perseant }
269 1.15 perseant }
270 1.18 perseant if (writeit)
271 1.18 perseant LFS_SYNC_CLEANERINFO(cip, fs, bp, writeit);
272 1.18 perseant else
273 1.18 perseant brelse(bp);
274 1.19 perseant
275 1.20 perseant if (fs->lfs_freehd == 0) {
276 1.20 perseant pwarn("%sree list head is 0x0\n", preen ? "f" : "F");
277 1.21 perseant if (preen || reply("FIX")) {
278 1.21 perseant extend_ifile(fs);
279 1.21 perseant reset_maxino(((VTOI(fs->lfs_ivnode)->i_ffs1_size >>
280 1.21 perseant fs->lfs_bsize) -
281 1.21 perseant fs->lfs_segtabsz - fs->lfs_cleansz) *
282 1.21 perseant fs->lfs_ifpb);
283 1.21 perseant }
284 1.20 perseant }
285 1.20 perseant
286 1.19 perseant /*
287 1.19 perseant * Check the distance between sequential free list entries.
288 1.19 perseant * An ideally ordered free list will have the sum of these
289 1.19 perseant * distances <= the number of inodes in the inode list.
290 1.19 perseant * If the observed distance is too high, reorder the list.
291 1.19 perseant * Strictly speaking, this is not an error, but it optimizes the
292 1.19 perseant * speed in creation of files and should help a tiny bit with
293 1.19 perseant * cleaner thrash as well.
294 1.19 perseant */
295 1.19 perseant if (totaldist > 4 * maxino) {
296 1.23 kent pwarn("%sotal inode list traversal length %" PRIu64 "x list length%s\n",
297 1.19 perseant (preen ? "t" : "T"), totaldist/maxino,
298 1.19 perseant (preen ? ", optimizing" : ""));
299 1.19 perseant if (preen || reply("OPTIMIZE") == 1) {
300 1.19 perseant plastino = lowfreeino;
301 1.19 perseant for (ino = lowfreeino + 1; ino < maxino; ino++) {
302 1.19 perseant LFS_IENTRY(ifp, fs, ino, bp);
303 1.19 perseant daddr = ifp->if_daddr;
304 1.19 perseant brelse(bp);
305 1.19 perseant if (daddr == 0) {
306 1.19 perseant LFS_IENTRY(ifp, fs, plastino, bp);
307 1.19 perseant ifp->if_nextfree = ino;
308 1.19 perseant VOP_BWRITE(bp);
309 1.19 perseant plastino = ino;
310 1.19 perseant }
311 1.19 perseant }
312 1.19 perseant LFS_CLEANERINFO(cip, fs, bp);
313 1.19 perseant cip->free_head = fs->lfs_freehd = lowfreeino;
314 1.19 perseant cip->free_tail = plastino;
315 1.19 perseant LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
316 1.19 perseant }
317 1.19 perseant }
318 1.1 perseant }
319