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