pass1.c revision 1.18 1 /* $NetBSD: pass1.c,v 1.18 1997/09/16 08:37:06 mrg 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)pass1.c 8.1 (Berkeley) 6/5/93";
40 #else
41 __RCSID("$NetBSD: pass1.c,v 1.18 1997/09/16 08:37:06 mrg Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <ufs/ufs/dinode.h>
48 #include <ufs/ufs/dir.h>
49 #include <ufs/ffs/fs.h>
50
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include "fsck.h"
56 #include "extern.h"
57 #include "fsutil.h"
58
59 static daddr_t badblk;
60 static daddr_t dupblk;
61 static void checkinode __P((ino_t, struct inodesc *));
62
63 void
64 pass1()
65 {
66 ino_t inumber;
67 int c, i, cgd;
68 struct inodesc idesc;
69
70 /*
71 * Set file system reserved blocks in used block map.
72 */
73 for (c = 0; c < sblock.fs_ncg; c++) {
74 cgd = cgdmin(&sblock, c);
75 if (c == 0)
76 i = cgbase(&sblock, c);
77 else
78 i = cgsblock(&sblock, c);
79 for (; i < cgd; i++)
80 setbmap(i);
81 }
82 i = sblock.fs_csaddr;
83 cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
84 for (; i < cgd; i++)
85 setbmap(i);
86 /*
87 * Find all allocated blocks.
88 */
89 memset(&idesc, 0, sizeof(struct inodesc));
90 idesc.id_type = ADDR;
91 idesc.id_func = pass1check;
92 inumber = 0;
93 n_files = n_blks = 0;
94 resetinodebuf();
95 for (c = 0; c < sblock.fs_ncg; c++) {
96 for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
97 if (inumber < ROOTINO)
98 continue;
99 checkinode(inumber, &idesc);
100 }
101 }
102 freeinodebuf();
103 }
104
105 static void
106 checkinode(inumber, idesc)
107 ino_t inumber;
108 struct inodesc *idesc;
109 {
110 struct dinode *dp;
111 struct zlncnt *zlnp;
112 int ndb, j;
113 mode_t mode;
114 char *symbuf;
115
116 dp = getnextinode(inumber);
117 mode = dp->di_mode & IFMT;
118 if (mode == 0) {
119 if (memcmp(dp->di_db, zino.di_db, NDADDR * sizeof(daddr_t)) ||
120 memcmp(dp->di_ib, zino.di_ib, NIADDR * sizeof(daddr_t)) ||
121 dp->di_mode || dp->di_size) {
122 pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
123 if (reply("CLEAR") == 1) {
124 dp = ginode(inumber);
125 clearinode(dp);
126 inodirty();
127 }
128 }
129 statemap[inumber] = USTATE;
130 return;
131 }
132 lastino = inumber;
133 if (/* dp->di_size < 0 || */
134 dp->di_size + sblock.fs_bsize - 1 < dp->di_size) {
135 if (debug)
136 printf("bad size %qu:",(unsigned long long)dp->di_size);
137 goto unknown;
138 }
139 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
140 dp = ginode(inumber);
141 dp->di_size = sblock.fs_fsize;
142 dp->di_mode = IFREG|0600;
143 inodirty();
144 }
145 ndb = howmany(dp->di_size, sblock.fs_bsize);
146 if (ndb < 0) {
147 if (debug)
148 printf("bad size %qu ndb %d:",
149 (unsigned long long)dp->di_size, ndb);
150 goto unknown;
151 }
152 if (mode == IFBLK || mode == IFCHR)
153 ndb++;
154 if (mode == IFLNK) {
155 /*
156 * Note that the old fastlink format always had di_blocks set
157 * to 0. Other than that we no longer use the `spare' field
158 * (which is now the extended uid) for sanity checking, the
159 * new format is the same as the old. We simply ignore the
160 * conversion altogether. - mycroft, 19MAY1994
161 */
162 if (doinglevel2 &&
163 dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
164 dp->di_blocks != 0) {
165 symbuf = alloca(secsize);
166 if (bread(fsreadfd, symbuf,
167 fsbtodb(&sblock, dp->di_db[0]),
168 (long)secsize) != 0)
169 errexit("cannot read symlink");
170 if (debug) {
171 symbuf[dp->di_size] = 0;
172 printf("convert symlink %u(%s) of size %qd\n",
173 inumber, symbuf,
174 (unsigned long long)dp->di_size);
175 }
176 dp = ginode(inumber);
177 memcpy(dp->di_shortlink, symbuf, (long)dp->di_size);
178 dp->di_blocks = 0;
179 inodirty();
180 }
181 /*
182 * Fake ndb value so direct/indirect block checks below
183 * will detect any garbage after symlink string.
184 */
185 if (dp->di_size < sblock.fs_maxsymlinklen ||
186 (sblock.fs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
187 ndb = howmany(dp->di_size, sizeof(daddr_t));
188 if (ndb > NDADDR) {
189 j = ndb - NDADDR;
190 for (ndb = 1; j > 1; j--)
191 ndb *= NINDIR(&sblock);
192 ndb += NDADDR;
193 }
194 }
195 }
196 for (j = ndb; j < NDADDR; j++)
197 if (dp->di_db[j] != 0) {
198 if (debug)
199 printf("bad direct addr: %d\n", dp->di_db[j]);
200 goto unknown;
201 }
202 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
203 ndb /= NINDIR(&sblock);
204 for (; j < NIADDR; j++)
205 if (dp->di_ib[j] != 0) {
206 if (debug)
207 printf("bad indirect addr: %d\n",
208 dp->di_ib[j]);
209 goto unknown;
210 }
211 if (ftypeok(dp) == 0)
212 goto unknown;
213 n_files++;
214 lncntp[inumber] = dp->di_nlink;
215 if (dp->di_nlink <= 0) {
216 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
217 if (zlnp == NULL) {
218 pfatal("LINK COUNT TABLE OVERFLOW");
219 if (reply("CONTINUE") == 0)
220 errexit("%s", "");
221 } else {
222 zlnp->zlncnt = inumber;
223 zlnp->next = zlnhead;
224 zlnhead = zlnp;
225 }
226 }
227 if (mode == IFDIR) {
228 if (dp->di_size == 0)
229 statemap[inumber] = DCLEAR;
230 else
231 statemap[inumber] = DSTATE;
232 cacheino(dp, inumber);
233 } else
234 statemap[inumber] = FSTATE;
235 typemap[inumber] = IFTODT(mode);
236 if (doinglevel2 &&
237 (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
238 dp = ginode(inumber);
239 dp->di_uid = dp->di_ouid;
240 dp->di_ouid = -1;
241 dp->di_gid = dp->di_ogid;
242 dp->di_ogid = -1;
243 inodirty();
244 }
245 badblk = dupblk = 0;
246 idesc->id_number = inumber;
247 (void)ckinode(dp, idesc);
248 idesc->id_entryno *= btodb(sblock.fs_fsize);
249 if (dp->di_blocks != idesc->id_entryno) {
250 pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
251 inumber, dp->di_blocks, idesc->id_entryno);
252 if (preen)
253 printf(" (CORRECTED)\n");
254 else if (reply("CORRECT") == 0)
255 return;
256 dp = ginode(inumber);
257 dp->di_blocks = idesc->id_entryno;
258 inodirty();
259 }
260 return;
261 unknown:
262 pfatal("UNKNOWN FILE TYPE I=%u", inumber);
263 statemap[inumber] = FCLEAR;
264 if (reply("CLEAR") == 1) {
265 statemap[inumber] = USTATE;
266 dp = ginode(inumber);
267 clearinode(dp);
268 inodirty();
269 }
270 }
271
272 int
273 pass1check(idesc)
274 struct inodesc *idesc;
275 {
276 int res = KEEPON;
277 int anyout, nfrags;
278 daddr_t blkno = idesc->id_blkno;
279 struct dups *dlp;
280 struct dups *new;
281
282 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
283 blkerror(idesc->id_number, "BAD", blkno);
284 if (badblk++ >= MAXBAD) {
285 pwarn("EXCESSIVE BAD BLKS I=%u",
286 idesc->id_number);
287 if (preen)
288 printf(" (SKIPPING)\n");
289 else if (reply("CONTINUE") == 0)
290 errexit("%s", "");
291 return (STOP);
292 }
293 }
294 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
295 if (anyout && chkrange(blkno, 1)) {
296 res = SKIP;
297 } else if (!testbmap(blkno)) {
298 n_blks++;
299 setbmap(blkno);
300 } else {
301 blkerror(idesc->id_number, "DUP", blkno);
302 if (dupblk++ >= MAXDUP) {
303 pwarn("EXCESSIVE DUP BLKS I=%u",
304 idesc->id_number);
305 if (preen)
306 printf(" (SKIPPING)\n");
307 else if (reply("CONTINUE") == 0)
308 errexit("%s", "");
309 return (STOP);
310 }
311 new = (struct dups *)malloc(sizeof(struct dups));
312 if (new == NULL) {
313 pfatal("DUP TABLE OVERFLOW.");
314 if (reply("CONTINUE") == 0)
315 errexit("%s", "");
316 return (STOP);
317 }
318 new->dup = blkno;
319 if (muldup == 0) {
320 duplist = muldup = new;
321 new->next = 0;
322 } else {
323 new->next = muldup->next;
324 muldup->next = new;
325 }
326 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
327 if (dlp->dup == blkno)
328 break;
329 if (dlp == muldup && dlp->dup != blkno)
330 muldup = new;
331 }
332 /*
333 * count the number of blocks found in id_entryno
334 */
335 idesc->id_entryno++;
336 }
337 return (res);
338 }
339