pass1.c revision 1.16 1 /* $NetBSD: pass1.c,v 1.16 1996/09/27 22:45:15 christos Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)pass1.c 8.1 (Berkeley) 6/5/93";
39 #else
40 static char rcsid[] = "$NetBSD: pass1.c,v 1.16 1996/09/27 22:45:15 christos Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/time.h>
46 #include <ufs/ufs/dinode.h>
47 #include <ufs/ufs/dir.h>
48 #include <ufs/ffs/fs.h>
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "fsck.h"
55 #include "extern.h"
56 #include "fsutil.h"
57
58 static daddr_t badblk;
59 static daddr_t dupblk;
60 static void checkinode __P((ino_t, struct inodesc *));
61
62 void
63 pass1()
64 {
65 ino_t inumber;
66 int c, i, cgd;
67 struct inodesc idesc;
68
69 /*
70 * Set file system reserved blocks in used block map.
71 */
72 for (c = 0; c < sblock.fs_ncg; c++) {
73 cgd = cgdmin(&sblock, c);
74 if (c == 0)
75 i = cgbase(&sblock, c);
76 else
77 i = cgsblock(&sblock, c);
78 for (; i < cgd; i++)
79 setbmap(i);
80 }
81 i = sblock.fs_csaddr;
82 cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
83 for (; i < cgd; i++)
84 setbmap(i);
85 /*
86 * Find all allocated blocks.
87 */
88 memset(&idesc, 0, sizeof(struct inodesc));
89 idesc.id_type = ADDR;
90 idesc.id_func = pass1check;
91 inumber = 0;
92 n_files = n_blks = 0;
93 resetinodebuf();
94 for (c = 0; c < sblock.fs_ncg; c++) {
95 for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
96 if (inumber < ROOTINO)
97 continue;
98 checkinode(inumber, &idesc);
99 }
100 }
101 freeinodebuf();
102 }
103
104 static void
105 checkinode(inumber, idesc)
106 ino_t inumber;
107 register struct inodesc *idesc;
108 {
109 register struct dinode *dp;
110 struct zlncnt *zlnp;
111 int ndb, j;
112 mode_t mode;
113 char *symbuf;
114
115 dp = getnextinode(inumber);
116 mode = dp->di_mode & IFMT;
117 if (mode == 0) {
118 if (memcmp(dp->di_db, zino.di_db, NDADDR * sizeof(daddr_t)) ||
119 memcmp(dp->di_ib, zino.di_ib, NIADDR * sizeof(daddr_t)) ||
120 dp->di_mode || dp->di_size) {
121 pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
122 if (reply("CLEAR") == 1) {
123 dp = ginode(inumber);
124 clearinode(dp);
125 inodirty();
126 }
127 }
128 statemap[inumber] = USTATE;
129 return;
130 }
131 lastino = inumber;
132 if (/* dp->di_size < 0 || */
133 dp->di_size + sblock.fs_bsize - 1 < dp->di_size) {
134 if (debug)
135 printf("bad size %qu:", dp->di_size);
136 goto unknown;
137 }
138 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
139 dp = ginode(inumber);
140 dp->di_size = sblock.fs_fsize;
141 dp->di_mode = IFREG|0600;
142 inodirty();
143 }
144 ndb = howmany(dp->di_size, sblock.fs_bsize);
145 if (ndb < 0) {
146 if (debug)
147 printf("bad size %qu ndb %d:",
148 dp->di_size, ndb);
149 goto unknown;
150 }
151 if (mode == IFBLK || mode == IFCHR)
152 ndb++;
153 if (mode == IFLNK) {
154 /*
155 * Note that the old fastlink format always had di_blocks set
156 * to 0. Other than that we no longer use the `spare' field
157 * (which is now the extended uid) for sanity checking, the
158 * new format is the same as the old. We simply ignore the
159 * conversion altogether. - mycroft, 19MAY1994
160 */
161 if (doinglevel2 &&
162 dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
163 dp->di_blocks != 0) {
164 symbuf = alloca(secsize);
165 if (bread(fsreadfd, symbuf,
166 fsbtodb(&sblock, dp->di_db[0]),
167 (long)secsize) != 0)
168 errexit("cannot read symlink");
169 if (debug) {
170 symbuf[dp->di_size] = 0;
171 printf("convert symlink %d(%s) of size %qd\n",
172 inumber, symbuf, dp->di_size);
173 }
174 dp = ginode(inumber);
175 memcpy(dp->di_shortlink, symbuf, (long)dp->di_size);
176 dp->di_blocks = 0;
177 inodirty();
178 }
179 /*
180 * Fake ndb value so direct/indirect block checks below
181 * will detect any garbage after symlink string.
182 */
183 if (dp->di_size < sblock.fs_maxsymlinklen ||
184 (sblock.fs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
185 ndb = howmany(dp->di_size, sizeof(daddr_t));
186 if (ndb > NDADDR) {
187 j = ndb - NDADDR;
188 for (ndb = 1; j > 1; j--)
189 ndb *= NINDIR(&sblock);
190 ndb += NDADDR;
191 }
192 }
193 }
194 for (j = ndb; j < NDADDR; j++)
195 if (dp->di_db[j] != 0) {
196 if (debug)
197 printf("bad direct addr: %d\n", dp->di_db[j]);
198 goto unknown;
199 }
200 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
201 ndb /= NINDIR(&sblock);
202 for (; j < NIADDR; j++)
203 if (dp->di_ib[j] != 0) {
204 if (debug)
205 printf("bad indirect addr: %d\n",
206 dp->di_ib[j]);
207 goto unknown;
208 }
209 if (ftypeok(dp) == 0)
210 goto unknown;
211 n_files++;
212 lncntp[inumber] = dp->di_nlink;
213 if (dp->di_nlink <= 0) {
214 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
215 if (zlnp == NULL) {
216 pfatal("LINK COUNT TABLE OVERFLOW");
217 if (reply("CONTINUE") == 0)
218 errexit("%s", "");
219 } else {
220 zlnp->zlncnt = inumber;
221 zlnp->next = zlnhead;
222 zlnhead = zlnp;
223 }
224 }
225 if (mode == IFDIR) {
226 if (dp->di_size == 0)
227 statemap[inumber] = DCLEAR;
228 else
229 statemap[inumber] = DSTATE;
230 cacheino(dp, inumber);
231 } else
232 statemap[inumber] = FSTATE;
233 typemap[inumber] = IFTODT(mode);
234 if (doinglevel2 &&
235 (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
236 dp = ginode(inumber);
237 dp->di_uid = dp->di_ouid;
238 dp->di_ouid = -1;
239 dp->di_gid = dp->di_ogid;
240 dp->di_ogid = -1;
241 inodirty();
242 }
243 badblk = dupblk = 0;
244 idesc->id_number = inumber;
245 (void)ckinode(dp, idesc);
246 idesc->id_entryno *= btodb(sblock.fs_fsize);
247 if (dp->di_blocks != idesc->id_entryno) {
248 pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
249 inumber, dp->di_blocks, idesc->id_entryno);
250 if (preen)
251 printf(" (CORRECTED)\n");
252 else if (reply("CORRECT") == 0)
253 return;
254 dp = ginode(inumber);
255 dp->di_blocks = idesc->id_entryno;
256 inodirty();
257 }
258 return;
259 unknown:
260 pfatal("UNKNOWN FILE TYPE I=%u", inumber);
261 statemap[inumber] = FCLEAR;
262 if (reply("CLEAR") == 1) {
263 statemap[inumber] = USTATE;
264 dp = ginode(inumber);
265 clearinode(dp);
266 inodirty();
267 }
268 }
269
270 int
271 pass1check(idesc)
272 register struct inodesc *idesc;
273 {
274 int res = KEEPON;
275 int anyout, nfrags;
276 daddr_t blkno = idesc->id_blkno;
277 register struct dups *dlp;
278 struct dups *new;
279
280 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
281 blkerror(idesc->id_number, "BAD", blkno);
282 if (badblk++ >= MAXBAD) {
283 pwarn("EXCESSIVE BAD BLKS I=%u",
284 idesc->id_number);
285 if (preen)
286 printf(" (SKIPPING)\n");
287 else if (reply("CONTINUE") == 0)
288 errexit("%s", "");
289 return (STOP);
290 }
291 }
292 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
293 if (anyout && chkrange(blkno, 1)) {
294 res = SKIP;
295 } else if (!testbmap(blkno)) {
296 n_blks++;
297 setbmap(blkno);
298 } else {
299 blkerror(idesc->id_number, "DUP", blkno);
300 if (dupblk++ >= MAXDUP) {
301 pwarn("EXCESSIVE DUP BLKS I=%u",
302 idesc->id_number);
303 if (preen)
304 printf(" (SKIPPING)\n");
305 else if (reply("CONTINUE") == 0)
306 errexit("%s", "");
307 return (STOP);
308 }
309 new = (struct dups *)malloc(sizeof(struct dups));
310 if (new == NULL) {
311 pfatal("DUP TABLE OVERFLOW.");
312 if (reply("CONTINUE") == 0)
313 errexit("%s", "");
314 return (STOP);
315 }
316 new->dup = blkno;
317 if (muldup == 0) {
318 duplist = muldup = new;
319 new->next = 0;
320 } else {
321 new->next = muldup->next;
322 muldup->next = new;
323 }
324 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
325 if (dlp->dup == blkno)
326 break;
327 if (dlp == muldup && dlp->dup != blkno)
328 muldup = new;
329 }
330 /*
331 * count the number of blocks found in id_entryno
332 */
333 idesc->id_entryno++;
334 }
335 return (res);
336 }
337