Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sbin / fsck_lfs / pass0.c
blob4791cd1b23ec572c9cb295fa61536fae21477620
1 /* $NetBSD: pass0.c,v 1.30 2007/10/08 21:39:49 ad Exp $ */
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant@hhhh.org>.
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.
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.
31 /*-
32 * Copyright (c) 1980, 1986, 1993
33 * The Regents of the University of California. All rights reserved.
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.
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.
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>
66 #include <ufs/ufs/inode.h>
67 #include <ufs/ufs/dir.h>
68 #define vnode uvnode
69 #include <ufs/lfs/lfs.h>
70 #undef vnode
72 #include <assert.h>
73 #include <err.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <util.h>
79 #include "bufcache.h"
80 #include "vnode.h"
81 #include "lfs_user.h"
83 #include "fsck.h"
84 #include "extern.h"
85 #include "fsutil.h"
87 extern int fake_cleanseg;
90 * Pass 0. Check the LFS partial segments for valid checksums, correcting
91 * if necessary. Also check for valid offsets for inode and finfo blocks.
94 * XXX more could be done here---consistency between inode-held blocks and
95 * finfo blocks, for one thing.
98 #define dbshift (fs->lfs_bshift - fs->lfs_blktodb)
100 void
101 pass0(void)
103 daddr_t daddr;
104 CLEANERINFO *cip;
105 IFILE *ifp;
106 struct ubuf *bp, *cbp;
107 ino_t ino, plastino, nextino, lowfreeino, freehd;
108 char *visited;
109 int writeit = 0;
112 * Check the inode free list for inuse inodes, and cycles.
113 * Make sure that all free inodes are in fact on the list.
115 visited = ecalloc(maxino, sizeof(*visited));
116 plastino = 0;
117 lowfreeino = maxino;
118 LFS_CLEANERINFO(cip, fs, cbp);
119 freehd = ino = cip->free_head;
120 brelse(cbp, 0);
122 while (ino) {
123 if (lowfreeino > ino)
124 lowfreeino = ino;
125 if (ino >= maxino) {
126 pwarn("OUT OF RANGE INO %llu ON FREE LIST\n",
127 (unsigned long long)ino);
128 break;
130 if (visited[ino]) {
131 pwarn("INO %llu ALREADY FOUND ON FREE LIST\n",
132 (unsigned long long)ino);
133 if (preen || reply("FIX") == 1) {
134 /* plastino can't be zero */
135 LFS_IENTRY(ifp, fs, plastino, bp);
136 ifp->if_nextfree = 0;
137 VOP_BWRITE(bp);
139 break;
141 visited[ino] = 1;
142 LFS_IENTRY(ifp, fs, ino, bp);
143 nextino = ifp->if_nextfree;
144 daddr = ifp->if_daddr;
145 brelse(bp, 0);
146 if (daddr) {
147 pwarn("INO %llu WITH DADDR 0x%llx ON FREE LIST\n",
148 (unsigned long long)ino, (long long) daddr);
149 if (preen || reply("FIX") == 1) {
150 if (plastino == 0) {
151 freehd = nextino;
152 sbdirty();
153 } else {
154 LFS_IENTRY(ifp, fs, plastino, bp);
155 ifp->if_nextfree = nextino;
156 VOP_BWRITE(bp);
158 ino = nextino;
159 continue;
162 plastino = ino;
163 ino = nextino;
168 * Make sure all free inodes were found on the list
170 for (ino = maxino - 1; ino > ROOTINO; --ino) {
171 if (visited[ino])
172 continue;
174 LFS_IENTRY(ifp, fs, ino, bp);
175 if (ifp->if_daddr) {
176 brelse(bp, 0);
177 continue;
179 pwarn("INO %llu FREE BUT NOT ON FREE LIST\n",
180 (unsigned long long)ino);
181 if (preen || reply("FIX") == 1) {
182 assert(ino != freehd);
183 ifp->if_nextfree = freehd;
184 VOP_BWRITE(bp);
186 freehd = ino;
187 sbdirty();
189 /* If freelist was empty, this is the tail */
190 if (plastino == 0)
191 plastino = ino;
192 } else
193 brelse(bp, 0);
196 LFS_CLEANERINFO(cip, fs, cbp);
197 if (cip->free_head != freehd) {
198 /* They've already given us permission for this change */
199 cip->free_head = freehd;
200 writeit = 1;
202 if (freehd != fs->lfs_freehd) {
203 pwarn("FREE LIST HEAD IN SUPERBLOCK SHOULD BE %d (WAS %d)\n",
204 (int)fs->lfs_freehd, (int)freehd);
205 if (preen || reply("FIX")) {
206 fs->lfs_freehd = freehd;
207 sbdirty();
210 if (cip->free_tail != plastino) {
211 pwarn("FREE LIST TAIL SHOULD BE %llu (WAS %llu)\n",
212 (unsigned long long)plastino,
213 (unsigned long long)cip->free_tail);
214 if (preen || reply("FIX")) {
215 cip->free_tail = plastino;
216 writeit = 1;
220 if (writeit)
221 LFS_SYNC_CLEANERINFO(cip, fs, cbp, writeit);
222 else
223 brelse(cbp, 0);
225 if (fs->lfs_freehd == 0) {
226 pwarn("%sree list head is 0x0\n", preen ? "f" : "F");
227 if (preen || reply("FIX"))
228 extend_ifile(fs);