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