pass0.c revision 1.34 1 /* $NetBSD: pass0.c,v 1.34 2013/06/06 00:54:49 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 (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 *
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 #define _SYS_VNODE_H_ /* XXX */
68 #include <ufs/lfs/ulfs_inode.h>
69 #include <ufs/lfs/ulfs_dir.h>
70 #include <ufs/lfs/lfs.h>
71 #undef vnode
72
73 #include <assert.h>
74 #include <err.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <util.h>
79
80 #include "bufcache.h"
81 #include "vnode.h"
82 #include "lfs_user.h"
83
84 #include "fsck.h"
85 #include "extern.h"
86 #include "fsutil.h"
87
88 extern int fake_cleanseg;
89
90 /*
91 * Pass 0. Check the LFS partial segments for valid checksums, correcting
92 * if necessary. Also check for valid offsets for inode and finfo blocks.
93 */
94 /*
95 * XXX more could be done here---consistency between inode-held blocks and
96 * finfo blocks, for one thing.
97 */
98
99 #define dbshift (fs->lfs_bshift - fs->lfs_blktodb)
100
101 void
102 pass0(void)
103 {
104 daddr_t daddr;
105 CLEANERINFO *cip;
106 IFILE *ifp;
107 struct ubuf *bp, *cbp;
108 ino_t ino, plastino, nextino, lowfreeino, freehd;
109 char *visited;
110 int writeit = 0;
111
112 /*
113 * Check the inode free list for inuse inodes, and cycles.
114 * Make sure that all free inodes are in fact on the list.
115 */
116 visited = ecalloc(maxino, sizeof(*visited));
117 plastino = 0;
118 lowfreeino = maxino;
119 LFS_CLEANERINFO(cip, fs, cbp);
120 freehd = ino = cip->free_head;
121 brelse(cbp, 0);
122
123 while (ino) {
124 if (lowfreeino > ino)
125 lowfreeino = ino;
126 if (ino >= maxino) {
127 pwarn("OUT OF RANGE INO %llu ON FREE LIST\n",
128 (unsigned long long)ino);
129 break;
130 }
131 if (visited[ino]) {
132 pwarn("INO %llu ALREADY FOUND ON FREE LIST\n",
133 (unsigned long long)ino);
134 if (preen || reply("FIX") == 1) {
135 /* plastino can't be zero */
136 LFS_IENTRY(ifp, fs, plastino, bp);
137 ifp->if_nextfree = 0;
138 VOP_BWRITE(bp);
139 }
140 break;
141 }
142 visited[ino] = 1;
143 LFS_IENTRY(ifp, fs, ino, bp);
144 nextino = ifp->if_nextfree;
145 daddr = ifp->if_daddr;
146 brelse(bp, 0);
147 if (daddr) {
148 pwarn("INO %llu WITH DADDR 0x%llx ON FREE LIST\n",
149 (unsigned long long)ino, (long long) daddr);
150 if (preen || reply("FIX") == 1) {
151 if (plastino == 0) {
152 freehd = nextino;
153 sbdirty();
154 } else {
155 LFS_IENTRY(ifp, fs, plastino, bp);
156 ifp->if_nextfree = nextino;
157 VOP_BWRITE(bp);
158 }
159 ino = nextino;
160 continue;
161 }
162 }
163 plastino = ino;
164 ino = nextino;
165 }
166
167
168 /*
169 * Make sure all free inodes were found on the list
170 */
171 for (ino = maxino - 1; ino > ULFS_ROOTINO; --ino) {
172 if (visited[ino])
173 continue;
174
175 LFS_IENTRY(ifp, fs, ino, bp);
176 if (ifp->if_daddr) {
177 brelse(bp, 0);
178 continue;
179 }
180 pwarn("INO %llu FREE BUT NOT ON FREE LIST\n",
181 (unsigned long long)ino);
182 if (preen || reply("FIX") == 1) {
183 assert(ino != freehd);
184 ifp->if_nextfree = freehd;
185 VOP_BWRITE(bp);
186
187 freehd = ino;
188 sbdirty();
189
190 /* If freelist was empty, this is the tail */
191 if (plastino == 0)
192 plastino = ino;
193 } else
194 brelse(bp, 0);
195 }
196
197 LFS_CLEANERINFO(cip, fs, cbp);
198 if (cip->free_head != freehd) {
199 /* They've already given us permission for this change */
200 cip->free_head = freehd;
201 writeit = 1;
202 }
203 if (freehd != fs->lfs_freehd) {
204 pwarn("FREE LIST HEAD IN SUPERBLOCK SHOULD BE %d (WAS %d)\n",
205 (int)fs->lfs_freehd, (int)freehd);
206 if (preen || reply("FIX")) {
207 fs->lfs_freehd = freehd;
208 sbdirty();
209 }
210 }
211 if (cip->free_tail != plastino) {
212 pwarn("FREE LIST TAIL SHOULD BE %llu (WAS %llu)\n",
213 (unsigned long long)plastino,
214 (unsigned long long)cip->free_tail);
215 if (preen || reply("FIX")) {
216 cip->free_tail = plastino;
217 writeit = 1;
218 }
219 }
220
221 if (writeit)
222 LFS_SYNC_CLEANERINFO(cip, fs, cbp, writeit);
223 else
224 brelse(cbp, 0);
225
226 if (fs->lfs_freehd == 0) {
227 pwarn("%sree list head is 0x0\n", preen ? "f" : "F");
228 if (preen || reply("FIX"))
229 extend_ifile(fs);
230 }
231 }
232