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