pass1.c revision 1.5 1 /* $NetBSD: pass1.c,v 1.5 2000/05/16 04:55:59 perseant 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/param.h>
37 #include <sys/time.h>
38 #include <ufs/ufs/dinode.h>
39 #include <ufs/ufs/dir.h>
40 #include <sys/mount.h>
41 #include <ufs/lfs/lfs.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include <signal.h>
48
49 #include "fsck.h"
50 #include "extern.h"
51 #include "fsutil.h"
52
53 SEGUSE *seg_table;
54 extern daddr_t *din_table;
55
56 static daddr_t badblk;
57 static daddr_t dupblk;
58 static void checkinode __P((ino_t, struct inodesc *));
59 static int i_d_cmp(const void *, const void *);
60
61 static void bmapcheck(void)
62 {
63 int i;
64
65 if(testbmap(0))
66 raise(1);
67 for(i=0;i<maxfsblock;i++)
68 if(testbmap(i) > maxino)
69 raise(1);
70 }
71
72 struct ino_daddr {
73 ino_t ino;
74 daddr_t daddr;
75 };
76
77 static int
78 i_d_cmp(const void *va, const void *vb)
79 {
80 struct ino_daddr *a, *b;
81
82 a = *((struct ino_daddr **)va);
83 b = *((struct ino_daddr **)vb);
84
85 if (a->daddr == b->daddr) {
86 return (a->ino - b->ino);
87 }
88 if (a->daddr > b->daddr) {
89 return 1;
90 }
91 return -1;
92 }
93
94 void
95 pass1()
96 {
97 ino_t inumber;
98 int i, total_segments;
99 struct inodesc idesc;
100 struct dinode *idinode, *tinode;
101 struct ifile *ifp;
102 CLEANERINFO *cp;
103 struct bufarea *bp;
104 struct ino_daddr **dins;
105
106 idinode = lfs_difind(&sblock,sblock.lfs_ifile,&ifblock);
107
108 /*
109 * We now have the ifile's inode block in core. Read out the
110 * number of segments.
111 */
112 #if 1
113 if(pbp != 0)
114 pbp->b_flags &= ~B_INUSE;
115 pbp = getddblk(idinode->di_db[0], sblock.lfs_bsize);
116
117 cp = (CLEANERINFO *)(pbp->b_un.b_buf);
118 #endif
119 total_segments = sblock.lfs_size / sblock.lfs_bsize;
120
121 /*
122 * Find all allocated blocks, initialize numdirs.
123 */
124 memset(&idesc, 0, sizeof(struct inodesc));
125 idesc.id_type = ADDR;
126 idesc.id_func = pass1check;
127 idesc.id_lblkno = 0;
128 inumber = 0;
129 n_files = n_blks = 0;
130
131 if (debug)
132 printf("creating inode address table...\n");
133 /* Sort by daddr */
134 dins = (struct ino_daddr **)malloc((maxino+1) * sizeof(*dins));
135 for(i=0;i<=maxino;i++) {
136 dins[i] = malloc(sizeof(**dins));
137 dins[i]->ino = i;
138 dins[i]->daddr = lfs_ino_daddr(i);
139 }
140 qsort(dins, maxino+1, sizeof(*dins), i_d_cmp);
141
142 /* find a value for numdirs, fill in din_table */
143 if (debug)
144 printf("counting dirs...\n");
145 numdirs=0;
146 for(i=0; i <= maxino; i++) {
147 inumber = dins[i]->ino;
148 if(inumber == 0 || dins[i]->daddr == 0)
149 continue;
150 tinode = lfs_ginode(inumber);
151 if(tinode && (tinode->di_mode & IFMT)==IFDIR)
152 numdirs++;
153 }
154
155 /* from setup.c */
156 inplast = 0;
157 listmax = numdirs + 10;
158 inpsort = (struct inoinfo **)calloc((unsigned)listmax,
159 sizeof(struct inoinfo *));
160 inphead = (struct inoinfo **)calloc((unsigned)numdirs,
161 sizeof(struct inoinfo *));
162 if (inpsort == NULL || inphead == NULL) {
163 printf("cannot alloc %lu bytes for inphead\n",
164 (unsigned long)numdirs * sizeof(struct inoinfo *));
165 exit(1);
166 }
167
168 /* resetinodebuf(); */
169 if (debug)
170 printf("counting blocks...\n");
171 for(i=0; i <= maxino; i++) {
172 inumber = dins[i]->ino;
173 if(inumber == 0 || dins[i]->daddr == 0)
174 continue;
175 ifp = lfs_ientry(inumber,&bp);
176 if(ifp && ifp->if_daddr != LFS_UNUSED_DADDR) {
177 bp->b_flags &= ~B_INUSE;
178 checkinode(inumber, &idesc);
179 } else {
180 bp->b_flags &= ~B_INUSE;
181 statemap[inumber] = USTATE;
182 }
183 free(dins[i]);
184 }
185 free(dins);
186
187 bmapcheck();
188 /* freeinodebuf(); */
189 }
190
191 static void
192 checkinode(inumber, idesc)
193 ino_t inumber;
194 register struct inodesc *idesc;
195 {
196 register struct dinode *dp;
197 struct zlncnt *zlnp;
198 int ndb, j;
199 mode_t mode;
200 char *symbuf;
201
202 /* dp = getnextinode(inumber); */
203 dp = lfs_ginode(inumber);
204
205 if(dp==NULL) {
206 /* pwarn("Could not find inode %ld\n",(long)inumber); */
207 statemap[inumber]=USTATE;
208 return;
209 }
210
211 mode = dp->di_mode & IFMT;
212
213 /* XXX - LFS doesn't have this particular problem (?) */
214 if (mode == 0) {
215 if (memcmp(dp->di_db, zino.di_db, NDADDR * sizeof(daddr_t)) ||
216 memcmp(dp->di_ib, zino.di_ib, NIADDR * sizeof(daddr_t)) ||
217 dp->di_mode || dp->di_size) {
218 pwarn("mode=o%o, ifmt=o%o\n", dp->di_mode, mode);
219 pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
220 if (reply("CLEAR") == 1) {
221 dp = ginode(inumber);
222 clearinode(dp);
223 inodirty();
224 }
225 }
226 statemap[inumber] = USTATE;
227 return;
228 }
229 lastino = inumber;
230 if (/* dp->di_size < 0 || */
231 dp->di_size + sblock.lfs_bsize - 1 < dp->di_size) {
232 if (debug)
233 printf("bad size %qu:", (unsigned long long)dp->di_size);
234 goto unknown;
235 }
236 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
237 dp = ginode(inumber);
238 dp->di_size = sblock.lfs_fsize;
239 dp->di_mode = IFREG|0600;
240 inodirty();
241 }
242 ndb = howmany(dp->di_size, sblock.lfs_bsize);
243 if (ndb < 0) {
244 if (debug)
245 printf("bad size %qu ndb %d:",
246 (unsigned long long)dp->di_size, ndb);
247 goto unknown;
248 }
249 if (mode == IFBLK || mode == IFCHR)
250 ndb++;
251 if (mode == IFLNK) {
252 /*
253 * Note that the old fastlink format always had di_blocks set
254 * to 0. Other than that we no longer use the `spare' field
255 * (which is now the extended uid) for sanity checking, the
256 * new format is the same as the old. We simply ignore the
257 * conversion altogether. - mycroft, 19MAY1994
258 */
259 if (doinglevel2 &&
260 dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
261 dp->di_blocks != 0) {
262 symbuf = alloca(secsize);
263 if (bread(fsreadfd, symbuf,
264 fsbtodb(&sblock, dp->di_db[0]),
265 (long)secsize) != 0)
266 errexit("cannot read symlink");
267 if (debug) {
268 symbuf[dp->di_size] = 0;
269 printf("convert symlink %d(%s) of size %qd\n",
270 inumber, symbuf, (long long)dp->di_size);
271 }
272 dp = ginode(inumber);
273 memcpy(dp->di_shortlink, symbuf, (long)dp->di_size);
274 dp->di_blocks = 0;
275 inodirty();
276 }
277 /*
278 * Fake ndb value so direct/indirect block checks below
279 * will detect any garbage after symlink string.
280 */
281 if (dp->di_size < sblock.lfs_maxsymlinklen ||
282 (sblock.lfs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
283 ndb = howmany(dp->di_size, sizeof(daddr_t));
284 if (ndb > NDADDR) {
285 j = ndb - NDADDR;
286 for (ndb = 1; j > 1; j--)
287 ndb *= NINDIR(&sblock);
288 ndb += NDADDR;
289 }
290 }
291 }
292 for (j = ndb; j < NDADDR; j++)
293 if (dp->di_db[j] != 0) {
294 if (debug)
295 printf("bad direct addr: %d\n", dp->di_db[j]);
296 goto unknown;
297 }
298 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
299 ndb /= NINDIR(&sblock);
300 for (; j < NIADDR; j++)
301 if (dp->di_ib[j] != 0) {
302 if (debug)
303 printf("bad indirect addr: %d\n",
304 dp->di_ib[j]);
305 /* goto unknown; */
306 }
307 if (ftypeok(dp) == 0)
308 goto unknown;
309 n_files++;
310 lncntp[inumber] = dp->di_nlink;
311 if (dp->di_nlink <= 0) {
312 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
313 if (zlnp == NULL) {
314 pfatal("LINK COUNT TABLE OVERFLOW");
315 if (reply("CONTINUE") == 0)
316 errexit("%s", "");
317 } else {
318 zlnp->zlncnt = inumber;
319 zlnp->next = zlnhead;
320 zlnhead = zlnp;
321 }
322 }
323 if (mode == IFDIR) {
324 if (dp->di_size == 0)
325 statemap[inumber] = DCLEAR;
326 else
327 statemap[inumber] = DSTATE;
328 cacheino(dp, inumber);
329 } else
330 statemap[inumber] = FSTATE;
331 typemap[inumber] = IFTODT(mode);
332 #if 0 /* FFS */
333 if (doinglevel2 &&
334 (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
335 dp = ginode(inumber);
336 dp->di_uid = dp->di_ouid;
337 dp->di_ouid = -1;
338 dp->di_gid = dp->di_ogid;
339 dp->di_ogid = -1;
340 inodirty();
341 }
342 #endif
343 badblk = dupblk = 0;
344 idesc->id_number = inumber;
345 (void)ckinode(dp, idesc);
346 idesc->id_entryno *= btodb(sblock.lfs_fsize);
347 if (dp->di_blocks != idesc->id_entryno) {
348 pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
349 inumber, dp->di_blocks, idesc->id_entryno);
350 if (preen)
351 printf(" (CORRECTED)\n");
352 else if (reply("CORRECT") == 0)
353 return;
354 dp = ginode(inumber);
355 dp->di_blocks = idesc->id_entryno;
356 inodirty();
357 }
358 return;
359 unknown:
360 pfatal("UNKNOWN FILE TYPE I=%u", inumber);
361 statemap[inumber] = FCLEAR;
362 if (reply("CLEAR") == 1) {
363 statemap[inumber] = USTATE;
364 dp = ginode(inumber);
365 clearinode(dp);
366 inodirty();
367 }
368 }
369
370 int
371 pass1check(idesc)
372 register struct inodesc *idesc;
373 {
374 int res = KEEPON;
375 int anyout, nfrags;
376 daddr_t blkno = idesc->id_blkno;
377 register struct dups *dlp;
378 struct dups *new;
379
380 if (!testbmap(blkno)) {
381 seg_table[datosn(&sblock,blkno)].su_nbytes += idesc->id_numfrags * sblock.lfs_fsize;
382 }
383
384 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
385 blkerror(idesc->id_number, "BAD", blkno);
386 if (badblk++ >= MAXBAD) {
387 pwarn("EXCESSIVE BAD BLKS I=%u",
388 idesc->id_number);
389 if (preen)
390 printf(" (SKIPPING)\n");
391 else if (reply("CONTINUE") == 0)
392 errexit("%s", "");
393 return (STOP);
394 }
395 }
396 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
397 if (anyout && chkrange(blkno, 1)) {
398 res = SKIP;
399 } else if (!testbmap(blkno)) {
400 n_blks++;
401 #ifndef VERBOSE_BLOCKMAP
402 setbmap(blkno);
403 #else
404 setbmap(blkno,idesc->id_number);
405 #endif
406 } else {
407 blkerror(idesc->id_number, "DUP", blkno);
408 #ifdef VERBOSE_BLOCKMAP
409 pwarn("(lbn %d: Holder is %d)\n", idesc->id_lblkno,
410 testbmap(blkno));
411 #endif
412 if (dupblk++ >= MAXDUP) {
413 pwarn("EXCESSIVE DUP BLKS I=%u",
414 idesc->id_number);
415 if (preen)
416 printf(" (SKIPPING)\n");
417 else if (reply("CONTINUE") == 0)
418 errexit("%s", "");
419 return (STOP);
420 }
421 new = (struct dups *)malloc(sizeof(struct dups));
422 if (new == NULL) {
423 pfatal("DUP TABLE OVERFLOW.");
424 if (reply("CONTINUE") == 0)
425 errexit("%s", "");
426 return (STOP);
427 }
428 new->dup = blkno;
429 if (muldup == 0) {
430 duplist = muldup = new;
431 new->next = 0;
432 } else {
433 new->next = muldup->next;
434 muldup->next = new;
435 }
436 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
437 if (dlp->dup == blkno)
438 break;
439 if (dlp == muldup && dlp->dup != blkno)
440 muldup = new;
441 }
442 /*
443 * count the number of blocks found in id_entryno
444 */
445 idesc->id_entryno ++;
446 }
447 return (res);
448 }
449