pass0.c revision 1.15 1 1.15 perseant /* $NetBSD: pass0.c,v 1.15 2003/03/31 19:56:59 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.1 perseant * 3. All advertising materials mentioning features or use of this software
51 1.1 perseant * must display the following acknowledgement:
52 1.1 perseant * This product includes software developed by the University of
53 1.1 perseant * California, Berkeley and its contributors.
54 1.1 perseant * 4. Neither the name of the University nor the names of its contributors
55 1.1 perseant * may be used to endorse or promote products derived from this software
56 1.1 perseant * without specific prior written permission.
57 1.1 perseant *
58 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 1.1 perseant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 1.1 perseant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 1.1 perseant * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 1.1 perseant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 1.1 perseant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 1.1 perseant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 1.1 perseant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 1.1 perseant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 1.1 perseant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 1.1 perseant * SUCH DAMAGE.
69 1.1 perseant */
70 1.1 perseant
71 1.14 perseant #include <sys/types.h>
72 1.1 perseant #include <sys/param.h>
73 1.1 perseant #include <sys/time.h>
74 1.14 perseant #include <sys/buf.h>
75 1.14 perseant #include <sys/mount.h>
76 1.14 perseant
77 1.14 perseant #include <ufs/ufs/inode.h>
78 1.1 perseant #include <ufs/ufs/dir.h>
79 1.14 perseant #define vnode uvnode
80 1.1 perseant #include <ufs/lfs/lfs.h>
81 1.14 perseant #undef vnode
82 1.1 perseant
83 1.1 perseant #include <stdio.h>
84 1.1 perseant #include <stdlib.h>
85 1.1 perseant #include <string.h>
86 1.1 perseant
87 1.14 perseant #include "bufcache.h"
88 1.14 perseant #include "vnode.h"
89 1.14 perseant #include "lfs.h"
90 1.14 perseant
91 1.1 perseant #include "fsck.h"
92 1.1 perseant #include "extern.h"
93 1.1 perseant #include "fsutil.h"
94 1.1 perseant
95 1.14 perseant extern int fake_cleanseg;
96 1.1 perseant
97 1.1 perseant /*
98 1.1 perseant * Pass 0. Check the LFS partial segments for valid checksums, correcting
99 1.1 perseant * if necessary. Also check for valid offsets for inode and finfo blocks.
100 1.1 perseant */
101 1.5 perseant /*
102 1.5 perseant * XXX more could be done here---consistency between inode-held blocks and
103 1.5 perseant * finfo blocks, for one thing.
104 1.5 perseant */
105 1.1 perseant
106 1.14 perseant #define dbshift (fs->lfs_bshift - fs->lfs_blktodb)
107 1.1 perseant
108 1.6 perseant void
109 1.6 perseant pass0()
110 1.1 perseant {
111 1.14 perseant daddr_t daddr;
112 1.15 perseant CLEANERINFO *cip;
113 1.14 perseant IFILE *ifp;
114 1.14 perseant struct ubuf *bp;
115 1.14 perseant ino_t ino, plastino, nextino, *visited;
116 1.15 perseant int writeit = 0;
117 1.5 perseant
118 1.5 perseant /*
119 1.6 perseant * Check the inode free list for inuse inodes, and cycles.
120 1.6 perseant * Make sure that all free inodes are in fact on the list.
121 1.5 perseant */
122 1.14 perseant visited = (ino_t *) malloc(maxino * sizeof(ino_t));
123 1.7 perseant memset(visited, 0, maxino * sizeof(ino_t));
124 1.6 perseant
125 1.10 lukem plastino = 0;
126 1.14 perseant ino = fs->lfs_freehd;
127 1.5 perseant while (ino) {
128 1.7 perseant if (ino >= maxino) {
129 1.7 perseant printf("! Ino %d out of range (last was %d)\n", ino,
130 1.14 perseant plastino);
131 1.7 perseant break;
132 1.7 perseant }
133 1.6 perseant if (visited[ino]) {
134 1.6 perseant pwarn("! Ino %d already found on the free list!\n",
135 1.14 perseant ino);
136 1.6 perseant if (preen || reply("FIX") == 1) {
137 1.10 lukem /* plastino can't be zero */
138 1.14 perseant LFS_IENTRY(ifp, fs, plastino, bp);
139 1.6 perseant ifp->if_nextfree = 0;
140 1.14 perseant VOP_BWRITE(bp);
141 1.6 perseant }
142 1.6 perseant break;
143 1.6 perseant }
144 1.6 perseant ++visited[ino];
145 1.14 perseant LFS_IENTRY(ifp, fs, ino, bp);
146 1.5 perseant nextino = ifp->if_nextfree;
147 1.5 perseant daddr = ifp->if_daddr;
148 1.14 perseant brelse(bp);
149 1.5 perseant if (daddr) {
150 1.12 fvdl pwarn("! Ino %d with daddr 0x%llx is on the free list!\n",
151 1.14 perseant ino, (long long) daddr);
152 1.5 perseant if (preen || reply("FIX") == 1) {
153 1.10 lukem if (plastino == 0) {
154 1.14 perseant fs->lfs_freehd = nextino;
155 1.5 perseant sbdirty();
156 1.5 perseant } else {
157 1.14 perseant LFS_IENTRY(ifp, fs, plastino, bp);
158 1.5 perseant ifp->if_nextfree = nextino;
159 1.14 perseant VOP_BWRITE(bp);
160 1.5 perseant }
161 1.5 perseant ino = nextino;
162 1.5 perseant continue;
163 1.5 perseant }
164 1.5 perseant }
165 1.10 lukem plastino = ino;
166 1.5 perseant ino = nextino;
167 1.6 perseant }
168 1.15 perseant
169 1.15 perseant
170 1.6 perseant /*
171 1.6 perseant * Make sure all free inodes were found on the list
172 1.6 perseant */
173 1.14 perseant for (ino = ROOTINO + 1; ino < maxino; ++ino) {
174 1.6 perseant if (visited[ino])
175 1.6 perseant continue;
176 1.6 perseant
177 1.14 perseant LFS_IENTRY(ifp, fs, ino, bp);
178 1.6 perseant if (ifp->if_daddr) {
179 1.14 perseant brelse(bp);
180 1.6 perseant continue;
181 1.6 perseant }
182 1.6 perseant pwarn("! Ino %d free, but not on the free list\n", ino);
183 1.6 perseant if (preen || reply("FIX") == 1) {
184 1.14 perseant ifp->if_nextfree = fs->lfs_freehd;
185 1.14 perseant fs->lfs_freehd = ino;
186 1.6 perseant sbdirty();
187 1.14 perseant VOP_BWRITE(bp);
188 1.14 perseant } else
189 1.14 perseant brelse(bp);
190 1.5 perseant }
191 1.15 perseant
192 1.15 perseant LFS_CLEANERINFO(cip, fs, bp);
193 1.15 perseant if (cip->free_head != fs->lfs_freehd) {
194 1.15 perseant pwarn("! Free list head should be %d (was %d)\n",
195 1.15 perseant fs->lfs_freehd, cip->free_head);
196 1.15 perseant if (preen || reply("FIX")) {
197 1.15 perseant cip->free_head = fs->lfs_freehd;
198 1.15 perseant writeit = 1;
199 1.15 perseant }
200 1.15 perseant }
201 1.15 perseant if (cip->free_tail != plastino) {
202 1.15 perseant pwarn("! Free list tail should be %d (was %d)\n", plastino,
203 1.15 perseant cip->free_tail);
204 1.15 perseant if (preen || reply("FIX")) {
205 1.15 perseant cip->free_tail = plastino;
206 1.15 perseant writeit = 1;
207 1.15 perseant }
208 1.15 perseant }
209 1.15 perseant LFS_SYNC_CLEANERINFO(cip, fs, bp, writeit);
210 1.1 perseant }
211