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