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