pass1.c revision 1.2 1 /*
2 * Copyright (c) 1980, 1986 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char sccsid[] = "@(#)pass1.c 5.16 (Berkeley) 3/19/91";
36 static char rcsid[] = "$Id: pass1.c,v 1.2 1993/03/22 08:04:00 cgd Exp $";
37 #endif /* not lint */
38
39 #include <sys/param.h>
40 #include <ufs/dinode.h>
41 #include <ufs/fs.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include "fsck.h"
45
46 static daddr_t badblk;
47 static daddr_t dupblk;
48 int pass1check();
49 struct dinode *getnextinode();
50
51 pass1()
52 {
53 register int c, i, j;
54 register struct dinode *dp;
55 struct zlncnt *zlnp;
56 int ndb, cgd;
57 struct inodesc idesc;
58 ino_t inumber;
59
60 /*
61 * Set file system reserved blocks in used block map.
62 */
63 for (c = 0; c < sblock.fs_ncg; c++) {
64 cgd = cgdmin(&sblock, c);
65 if (c == 0) {
66 i = cgbase(&sblock, c);
67 cgd += howmany(sblock.fs_cssize, sblock.fs_fsize);
68 } else
69 i = cgsblock(&sblock, c);
70 for (; i < cgd; i++)
71 setbmap(i);
72 }
73 /*
74 * Find all allocated blocks.
75 */
76 bzero((char *)&idesc, sizeof(struct inodesc));
77 idesc.id_type = ADDR;
78 idesc.id_func = pass1check;
79 inumber = 0;
80 n_files = n_blks = 0;
81 resetinodebuf();
82 for (c = 0; c < sblock.fs_ncg; c++) {
83 for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
84 if (inumber < ROOTINO)
85 continue;
86 dp = getnextinode(inumber);
87 if ((dp->di_mode & IFMT) == 0) {
88 if (bcmp((char *)dp->di_db, (char *)zino.di_db,
89 NDADDR * sizeof(daddr_t)) ||
90 bcmp((char *)dp->di_ib, (char *)zino.di_ib,
91 NIADDR * sizeof(daddr_t)) ||
92 dp->di_mode || dp->di_size) {
93 pfatal("PARTIALLY ALLOCATED INODE I=%lu",
94 inumber);
95 if (reply("CLEAR") == 1) {
96 dp = ginode(inumber);
97 clearinode(dp);
98 inodirty();
99 }
100 }
101 statemap[inumber] = USTATE;
102 continue;
103 }
104 lastino = inumber;
105 if (/* dp->di_size < 0 || */
106 dp->di_size + sblock.fs_bsize - 1 < dp->di_size) {
107 if (debug)
108 printf("bad size %lu:", dp->di_size);
109 goto unknown;
110 }
111 if (!preen && (dp->di_mode & IFMT) == IFMT &&
112 reply("HOLD BAD BLOCK") == 1) {
113 dp = ginode(inumber);
114 dp->di_size = sblock.fs_fsize;
115 dp->di_mode = IFREG|0600;
116 inodirty();
117 }
118 ndb = howmany(dp->di_size, sblock.fs_bsize);
119 if (ndb < 0) {
120 if (debug)
121 printf("bad size %lu ndb %d:",
122 dp->di_size, ndb);
123 goto unknown;
124 }
125 if ((dp->di_mode & IFMT) == IFBLK ||
126 (dp->di_mode & IFMT) == IFCHR)
127 ndb++;
128 for (j = ndb; j < NDADDR; j++)
129 if (dp->di_db[j] != 0) {
130 if (debug)
131 printf("bad direct addr: %ld\n",
132 dp->di_db[j]);
133 goto unknown;
134 }
135 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
136 ndb /= NINDIR(&sblock);
137 for (; j < NIADDR; j++)
138 if (dp->di_ib[j] != 0) {
139 if (debug)
140 printf("bad indirect addr: %ld\n",
141 dp->di_ib[j]);
142 goto unknown;
143 }
144 if (ftypeok(dp) == 0)
145 goto unknown;
146 n_files++;
147 lncntp[inumber] = dp->di_nlink;
148 if (dp->di_nlink <= 0) {
149 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
150 if (zlnp == NULL) {
151 pfatal("LINK COUNT TABLE OVERFLOW");
152 if (reply("CONTINUE") == 0)
153 errexit("");
154 } else {
155 zlnp->zlncnt = inumber;
156 zlnp->next = zlnhead;
157 zlnhead = zlnp;
158 }
159 }
160 if ((dp->di_mode & IFMT) == IFDIR) {
161 if (dp->di_size == 0)
162 statemap[inumber] = DCLEAR;
163 else
164 statemap[inumber] = DSTATE;
165 cacheino(dp, inumber);
166 } else
167 statemap[inumber] = FSTATE;
168 badblk = dupblk = 0;
169 idesc.id_number = inumber;
170 (void)ckinode(dp, &idesc);
171 idesc.id_entryno *= btodb(sblock.fs_fsize);
172 if (dp->di_blocks != idesc.id_entryno) {
173 pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)",
174 inumber, dp->di_blocks, idesc.id_entryno);
175 if (preen)
176 printf(" (CORRECTED)\n");
177 else if (reply("CORRECT") == 0)
178 continue;
179 dp = ginode(inumber);
180 dp->di_blocks = idesc.id_entryno;
181 inodirty();
182 }
183 continue;
184 unknown:
185 pfatal("UNKNOWN FILE TYPE I=%lu", inumber);
186 statemap[inumber] = FCLEAR;
187 if (reply("CLEAR") == 1) {
188 statemap[inumber] = USTATE;
189 dp = ginode(inumber);
190 clearinode(dp);
191 inodirty();
192 }
193 }
194 }
195 freeinodebuf();
196 }
197
198 pass1check(idesc)
199 register struct inodesc *idesc;
200 {
201 int res = KEEPON;
202 int anyout, nfrags;
203 daddr_t blkno = idesc->id_blkno;
204 register struct dups *dlp;
205 struct dups *new;
206
207 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
208 blkerror(idesc->id_number, "BAD", blkno);
209 if (badblk++ >= MAXBAD) {
210 pwarn("EXCESSIVE BAD BLKS I=%lu",
211 idesc->id_number);
212 if (preen)
213 printf(" (SKIPPING)\n");
214 else if (reply("CONTINUE") == 0)
215 errexit("");
216 return (STOP);
217 }
218 }
219 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
220 if (anyout && chkrange(blkno, 1)) {
221 res = SKIP;
222 } else if (!testbmap(blkno)) {
223 n_blks++;
224 setbmap(blkno);
225 } else {
226 blkerror(idesc->id_number, "DUP", blkno);
227 if (dupblk++ >= MAXDUP) {
228 pwarn("EXCESSIVE DUP BLKS I=%lu",
229 idesc->id_number);
230 if (preen)
231 printf(" (SKIPPING)\n");
232 else if (reply("CONTINUE") == 0)
233 errexit("");
234 return (STOP);
235 }
236 new = (struct dups *)malloc(sizeof(struct dups));
237 if (new == NULL) {
238 pfatal("DUP TABLE OVERFLOW.");
239 if (reply("CONTINUE") == 0)
240 errexit("");
241 return (STOP);
242 }
243 new->dup = blkno;
244 if (muldup == 0) {
245 duplist = muldup = new;
246 new->next = 0;
247 } else {
248 new->next = muldup->next;
249 muldup->next = new;
250 }
251 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
252 if (dlp->dup == blkno)
253 break;
254 if (dlp == muldup && dlp->dup != blkno)
255 muldup = new;
256 }
257 /*
258 * count the number of blocks found in id_entryno
259 */
260 idesc->id_entryno++;
261 }
262 return (res);
263 }
264