pass1.c revision 1.9 1 1.9 lukem /* $NetBSD: pass1.c,v 1.9 2001/01/05 02:02:58 lukem Exp $ */
2 1.1 perseant
3 1.1 perseant /*
4 1.1 perseant * Copyright (c) 1980, 1986, 1993
5 1.1 perseant * The Regents of the University of California. All rights reserved.
6 1.1 perseant *
7 1.1 perseant * Redistribution and use in source and binary forms, with or without
8 1.1 perseant * modification, are permitted provided that the following conditions
9 1.1 perseant * are met:
10 1.1 perseant * 1. Redistributions of source code must retain the above copyright
11 1.1 perseant * notice, this list of conditions and the following disclaimer.
12 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 perseant * notice, this list of conditions and the following disclaimer in the
14 1.1 perseant * documentation and/or other materials provided with the distribution.
15 1.1 perseant * 3. All advertising materials mentioning features or use of this software
16 1.1 perseant * must display the following acknowledgement:
17 1.1 perseant * This product includes software developed by the University of
18 1.1 perseant * California, Berkeley and its contributors.
19 1.1 perseant * 4. Neither the name of the University nor the names of its contributors
20 1.1 perseant * may be used to endorse or promote products derived from this software
21 1.1 perseant * without specific prior written permission.
22 1.1 perseant *
23 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 perseant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 perseant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 perseant * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 perseant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 perseant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 perseant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 perseant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 perseant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 perseant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 perseant * SUCH DAMAGE.
34 1.1 perseant */
35 1.1 perseant
36 1.1 perseant #include <sys/param.h>
37 1.1 perseant #include <sys/time.h>
38 1.1 perseant #include <ufs/ufs/dinode.h>
39 1.1 perseant #include <ufs/ufs/dir.h>
40 1.1 perseant #include <sys/mount.h>
41 1.1 perseant #include <ufs/lfs/lfs.h>
42 1.1 perseant
43 1.1 perseant #include <stdio.h>
44 1.1 perseant #include <stdlib.h>
45 1.1 perseant #include <string.h>
46 1.1 perseant
47 1.1 perseant #include <signal.h>
48 1.1 perseant
49 1.1 perseant #include "fsck.h"
50 1.1 perseant #include "extern.h"
51 1.1 perseant #include "fsutil.h"
52 1.1 perseant
53 1.6 perseant SEGUSE *seg_table;
54 1.5 perseant extern daddr_t *din_table;
55 1.1 perseant
56 1.6 perseant static daddr_t badblk;
57 1.6 perseant static daddr_t dupblk;
58 1.6 perseant static void checkinode(ino_t, struct inodesc *);
59 1.6 perseant static int i_d_cmp(const void *, const void *);
60 1.1 perseant
61 1.5 perseant struct ino_daddr {
62 1.6 perseant ino_t ino;
63 1.6 perseant daddr_t daddr;
64 1.5 perseant };
65 1.5 perseant
66 1.5 perseant static int
67 1.5 perseant i_d_cmp(const void *va, const void *vb)
68 1.5 perseant {
69 1.5 perseant struct ino_daddr *a, *b;
70 1.5 perseant
71 1.5 perseant a = *((struct ino_daddr **)va);
72 1.5 perseant b = *((struct ino_daddr **)vb);
73 1.5 perseant
74 1.5 perseant if (a->daddr == b->daddr) {
75 1.5 perseant return (a->ino - b->ino);
76 1.5 perseant }
77 1.5 perseant if (a->daddr > b->daddr) {
78 1.5 perseant return 1;
79 1.5 perseant }
80 1.5 perseant return -1;
81 1.5 perseant }
82 1.5 perseant
83 1.1 perseant void
84 1.1 perseant pass1()
85 1.1 perseant {
86 1.6 perseant ino_t inumber;
87 1.6 perseant int i, total_segments;
88 1.6 perseant struct inodesc idesc;
89 1.6 perseant struct dinode *idinode, *tinode;
90 1.6 perseant struct ifile *ifp;
91 1.6 perseant CLEANERINFO *cp;
92 1.5 perseant struct bufarea *bp;
93 1.5 perseant struct ino_daddr **dins;
94 1.1 perseant
95 1.6 perseant idinode = lfs_difind(&sblock, sblock.lfs_ifile, &ifblock);
96 1.1 perseant
97 1.6 perseant /*
98 1.1 perseant * We now have the ifile's inode block in core. Read out the
99 1.1 perseant * number of segments.
100 1.1 perseant */
101 1.1 perseant #if 1
102 1.6 perseant if (pbp != 0)
103 1.6 perseant pbp->b_flags &= ~B_INUSE;
104 1.6 perseant pbp = getddblk(idinode->di_db[0], sblock.lfs_bsize);
105 1.1 perseant
106 1.6 perseant cp = (CLEANERINFO *)(pbp->b_un.b_buf);
107 1.1 perseant #endif
108 1.6 perseant total_segments = sblock.lfs_size / sblock.lfs_bsize;
109 1.1 perseant
110 1.1 perseant /*
111 1.1 perseant * Find all allocated blocks, initialize numdirs.
112 1.1 perseant */
113 1.1 perseant memset(&idesc, 0, sizeof(struct inodesc));
114 1.1 perseant idesc.id_type = ADDR;
115 1.1 perseant idesc.id_func = pass1check;
116 1.1 perseant idesc.id_lblkno = 0;
117 1.1 perseant inumber = 0;
118 1.1 perseant n_files = n_blks = 0;
119 1.1 perseant
120 1.5 perseant if (debug)
121 1.5 perseant printf("creating inode address table...\n");
122 1.5 perseant /* Sort by daddr */
123 1.8 perseant dins = (struct ino_daddr **)malloc(maxino * sizeof(*dins));
124 1.8 perseant for (i = 0; i < maxino; i++) {
125 1.5 perseant dins[i] = malloc(sizeof(**dins));
126 1.5 perseant dins[i]->ino = i;
127 1.5 perseant dins[i]->daddr = lfs_ino_daddr(i);
128 1.5 perseant }
129 1.8 perseant qsort(dins, maxino, sizeof(*dins), i_d_cmp);
130 1.5 perseant
131 1.5 perseant /* find a value for numdirs, fill in din_table */
132 1.5 perseant if (debug)
133 1.5 perseant printf("counting dirs...\n");
134 1.6 perseant numdirs = 0;
135 1.8 perseant for (i = 0; i < maxino; i++) {
136 1.5 perseant inumber = dins[i]->ino;
137 1.6 perseant if (inumber == 0 || dins[i]->daddr == 0)
138 1.5 perseant continue;
139 1.5 perseant tinode = lfs_ginode(inumber);
140 1.6 perseant if (tinode && (tinode->di_mode & IFMT) == IFDIR)
141 1.5 perseant numdirs++;
142 1.1 perseant }
143 1.1 perseant
144 1.1 perseant /* from setup.c */
145 1.6 perseant inplast = 0;
146 1.6 perseant listmax = numdirs + 10;
147 1.6 perseant inpsort = (struct inoinfo **)calloc((unsigned) listmax,
148 1.6 perseant sizeof(struct inoinfo *));
149 1.6 perseant inphead = (struct inoinfo **)calloc((unsigned) numdirs,
150 1.6 perseant sizeof(struct inoinfo *));
151 1.6 perseant if (inpsort == NULL || inphead == NULL) {
152 1.6 perseant printf("cannot alloc %lu bytes for inphead\n",
153 1.6 perseant (unsigned long)numdirs * sizeof(struct inoinfo *));
154 1.1 perseant exit(1);
155 1.6 perseant }
156 1.1 perseant /* resetinodebuf(); */
157 1.5 perseant if (debug)
158 1.5 perseant printf("counting blocks...\n");
159 1.8 perseant for (i = 0; i < maxino; i++) {
160 1.5 perseant inumber = dins[i]->ino;
161 1.7 perseant if (inumber == 0 || dins[i]->daddr == 0) {
162 1.7 perseant statemap[inumber] = USTATE;
163 1.5 perseant continue;
164 1.7 perseant }
165 1.6 perseant ifp = lfs_ientry(inumber, &bp);
166 1.6 perseant if (ifp && ifp->if_daddr != LFS_UNUSED_DADDR) {
167 1.5 perseant bp->b_flags &= ~B_INUSE;
168 1.5 perseant checkinode(inumber, &idesc);
169 1.5 perseant } else {
170 1.5 perseant bp->b_flags &= ~B_INUSE;
171 1.5 perseant statemap[inumber] = USTATE;
172 1.5 perseant }
173 1.5 perseant free(dins[i]);
174 1.6 perseant }
175 1.5 perseant free(dins);
176 1.1 perseant
177 1.1 perseant /* freeinodebuf(); */
178 1.1 perseant }
179 1.1 perseant
180 1.1 perseant static void
181 1.6 perseant checkinode(ino_t inumber, struct inodesc * idesc)
182 1.1 perseant {
183 1.1 perseant register struct dinode *dp;
184 1.6 perseant struct zlncnt *zlnp;
185 1.6 perseant int ndb, j;
186 1.6 perseant mode_t mode;
187 1.6 perseant char *symbuf;
188 1.1 perseant
189 1.1 perseant /* dp = getnextinode(inumber); */
190 1.6 perseant dp = lfs_ginode(inumber);
191 1.1 perseant
192 1.6 perseant if (dp == NULL) {
193 1.6 perseant /* pwarn("Could not find inode %ld\n",(long)inumber); */
194 1.6 perseant statemap[inumber] = USTATE;
195 1.6 perseant return;
196 1.6 perseant }
197 1.1 perseant mode = dp->di_mode & IFMT;
198 1.1 perseant
199 1.6 perseant /* XXX - LFS doesn't have this particular problem (?) */
200 1.1 perseant if (mode == 0) {
201 1.1 perseant if (memcmp(dp->di_db, zino.di_db, NDADDR * sizeof(daddr_t)) ||
202 1.6 perseant memcmp(dp->di_ib, zino.di_ib, NIADDR * sizeof(daddr_t)) ||
203 1.1 perseant dp->di_mode || dp->di_size) {
204 1.1 perseant pwarn("mode=o%o, ifmt=o%o\n", dp->di_mode, mode);
205 1.1 perseant pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
206 1.1 perseant if (reply("CLEAR") == 1) {
207 1.1 perseant dp = ginode(inumber);
208 1.1 perseant clearinode(dp);
209 1.1 perseant inodirty();
210 1.1 perseant }
211 1.1 perseant }
212 1.1 perseant statemap[inumber] = USTATE;
213 1.1 perseant return;
214 1.1 perseant }
215 1.1 perseant lastino = inumber;
216 1.6 perseant if ( /* dp->di_size < 0 || */
217 1.1 perseant dp->di_size + sblock.lfs_bsize - 1 < dp->di_size) {
218 1.1 perseant if (debug)
219 1.9 lukem printf("bad size %llu:",
220 1.9 lukem (unsigned long long)dp->di_size);
221 1.1 perseant goto unknown;
222 1.1 perseant }
223 1.1 perseant if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
224 1.1 perseant dp = ginode(inumber);
225 1.1 perseant dp->di_size = sblock.lfs_fsize;
226 1.6 perseant dp->di_mode = IFREG | 0600;
227 1.1 perseant inodirty();
228 1.1 perseant }
229 1.1 perseant ndb = howmany(dp->di_size, sblock.lfs_bsize);
230 1.1 perseant if (ndb < 0) {
231 1.1 perseant if (debug)
232 1.9 lukem printf("bad size %llu ndb %d:",
233 1.6 perseant (unsigned long long)dp->di_size, ndb);
234 1.1 perseant goto unknown;
235 1.1 perseant }
236 1.1 perseant if (mode == IFBLK || mode == IFCHR)
237 1.1 perseant ndb++;
238 1.1 perseant if (mode == IFLNK) {
239 1.1 perseant /*
240 1.1 perseant * Note that the old fastlink format always had di_blocks set
241 1.1 perseant * to 0. Other than that we no longer use the `spare' field
242 1.6 perseant * (which is now the extended uid)for sanity checking, the
243 1.1 perseant * new format is the same as the old. We simply ignore the
244 1.1 perseant * conversion altogether. - mycroft, 19MAY1994
245 1.1 perseant */
246 1.1 perseant if (doinglevel2 &&
247 1.1 perseant dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
248 1.1 perseant dp->di_blocks != 0) {
249 1.1 perseant symbuf = alloca(secsize);
250 1.1 perseant if (bread(fsreadfd, symbuf,
251 1.6 perseant fsbtodb(&sblock, dp->di_db[0]),
252 1.6 perseant (long)secsize) != 0)
253 1.1 perseant errexit("cannot read symlink");
254 1.1 perseant if (debug) {
255 1.1 perseant symbuf[dp->di_size] = 0;
256 1.9 lukem printf("convert symlink %d(%s)of size %lld\n",
257 1.6 perseant inumber, symbuf, (long long)dp->di_size);
258 1.1 perseant }
259 1.1 perseant dp = ginode(inumber);
260 1.1 perseant memcpy(dp->di_shortlink, symbuf, (long)dp->di_size);
261 1.1 perseant dp->di_blocks = 0;
262 1.1 perseant inodirty();
263 1.1 perseant }
264 1.1 perseant /*
265 1.1 perseant * Fake ndb value so direct/indirect block checks below
266 1.1 perseant * will detect any garbage after symlink string.
267 1.1 perseant */
268 1.1 perseant if (dp->di_size < sblock.lfs_maxsymlinklen ||
269 1.1 perseant (sblock.lfs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
270 1.1 perseant ndb = howmany(dp->di_size, sizeof(daddr_t));
271 1.1 perseant if (ndb > NDADDR) {
272 1.1 perseant j = ndb - NDADDR;
273 1.1 perseant for (ndb = 1; j > 1; j--)
274 1.1 perseant ndb *= NINDIR(&sblock);
275 1.1 perseant ndb += NDADDR;
276 1.1 perseant }
277 1.1 perseant }
278 1.1 perseant }
279 1.1 perseant for (j = ndb; j < NDADDR; j++)
280 1.1 perseant if (dp->di_db[j] != 0) {
281 1.1 perseant if (debug)
282 1.1 perseant printf("bad direct addr: %d\n", dp->di_db[j]);
283 1.1 perseant goto unknown;
284 1.1 perseant }
285 1.1 perseant for (j = 0, ndb -= NDADDR; ndb > 0; j++)
286 1.1 perseant ndb /= NINDIR(&sblock);
287 1.1 perseant for (; j < NIADDR; j++)
288 1.1 perseant if (dp->di_ib[j] != 0) {
289 1.1 perseant if (debug)
290 1.1 perseant printf("bad indirect addr: %d\n",
291 1.6 perseant dp->di_ib[j]);
292 1.5 perseant /* goto unknown; */
293 1.1 perseant }
294 1.1 perseant if (ftypeok(dp) == 0)
295 1.1 perseant goto unknown;
296 1.1 perseant n_files++;
297 1.1 perseant lncntp[inumber] = dp->di_nlink;
298 1.1 perseant if (dp->di_nlink <= 0) {
299 1.1 perseant zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
300 1.1 perseant if (zlnp == NULL) {
301 1.1 perseant pfatal("LINK COUNT TABLE OVERFLOW");
302 1.1 perseant if (reply("CONTINUE") == 0)
303 1.1 perseant errexit("%s", "");
304 1.1 perseant } else {
305 1.1 perseant zlnp->zlncnt = inumber;
306 1.1 perseant zlnp->next = zlnhead;
307 1.1 perseant zlnhead = zlnp;
308 1.1 perseant }
309 1.1 perseant }
310 1.1 perseant if (mode == IFDIR) {
311 1.1 perseant if (dp->di_size == 0)
312 1.1 perseant statemap[inumber] = DCLEAR;
313 1.1 perseant else
314 1.1 perseant statemap[inumber] = DSTATE;
315 1.1 perseant cacheino(dp, inumber);
316 1.1 perseant } else
317 1.1 perseant statemap[inumber] = FSTATE;
318 1.1 perseant typemap[inumber] = IFTODT(mode);
319 1.6 perseant #if 0 /* FFS */
320 1.1 perseant if (doinglevel2 &&
321 1.6 perseant (dp->di_ouid != (u_short) - 1 || dp->di_ogid != (u_short) - 1)) {
322 1.1 perseant dp = ginode(inumber);
323 1.1 perseant dp->di_uid = dp->di_ouid;
324 1.1 perseant dp->di_ouid = -1;
325 1.1 perseant dp->di_gid = dp->di_ogid;
326 1.1 perseant dp->di_ogid = -1;
327 1.1 perseant inodirty();
328 1.1 perseant }
329 1.1 perseant #endif
330 1.1 perseant badblk = dupblk = 0;
331 1.1 perseant idesc->id_number = inumber;
332 1.1 perseant (void)ckinode(dp, idesc);
333 1.1 perseant idesc->id_entryno *= btodb(sblock.lfs_fsize);
334 1.1 perseant if (dp->di_blocks != idesc->id_entryno) {
335 1.1 perseant pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
336 1.6 perseant inumber, dp->di_blocks, idesc->id_entryno);
337 1.1 perseant if (preen)
338 1.1 perseant printf(" (CORRECTED)\n");
339 1.1 perseant else if (reply("CORRECT") == 0)
340 1.1 perseant return;
341 1.1 perseant dp = ginode(inumber);
342 1.1 perseant dp->di_blocks = idesc->id_entryno;
343 1.1 perseant inodirty();
344 1.1 perseant }
345 1.1 perseant return;
346 1.1 perseant unknown:
347 1.1 perseant pfatal("UNKNOWN FILE TYPE I=%u", inumber);
348 1.1 perseant statemap[inumber] = FCLEAR;
349 1.1 perseant if (reply("CLEAR") == 1) {
350 1.1 perseant statemap[inumber] = USTATE;
351 1.1 perseant dp = ginode(inumber);
352 1.1 perseant clearinode(dp);
353 1.1 perseant inodirty();
354 1.1 perseant }
355 1.1 perseant }
356 1.1 perseant
357 1.1 perseant int
358 1.6 perseant pass1check(struct inodesc * idesc)
359 1.1 perseant {
360 1.6 perseant int res = KEEPON;
361 1.6 perseant int anyout, nfrags;
362 1.6 perseant daddr_t blkno = idesc->id_blkno;
363 1.1 perseant register struct dups *dlp;
364 1.6 perseant struct dups *new;
365 1.1 perseant
366 1.1 perseant if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
367 1.1 perseant blkerror(idesc->id_number, "BAD", blkno);
368 1.1 perseant if (badblk++ >= MAXBAD) {
369 1.1 perseant pwarn("EXCESSIVE BAD BLKS I=%u",
370 1.6 perseant idesc->id_number);
371 1.1 perseant if (preen)
372 1.1 perseant printf(" (SKIPPING)\n");
373 1.1 perseant else if (reply("CONTINUE") == 0)
374 1.1 perseant errexit("%s", "");
375 1.1 perseant return (STOP);
376 1.1 perseant }
377 1.8 perseant } else if (!testbmap(blkno)) {
378 1.8 perseant seg_table[datosn(&sblock, blkno)].su_nbytes += idesc->id_numfrags * sblock.lfs_fsize;
379 1.1 perseant }
380 1.1 perseant for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
381 1.1 perseant if (anyout && chkrange(blkno, 1)) {
382 1.1 perseant res = SKIP;
383 1.1 perseant } else if (!testbmap(blkno)) {
384 1.1 perseant n_blks++;
385 1.1 perseant #ifndef VERBOSE_BLOCKMAP
386 1.1 perseant setbmap(blkno);
387 1.1 perseant #else
388 1.6 perseant setbmap(blkno, idesc->id_number);
389 1.1 perseant #endif
390 1.1 perseant } else {
391 1.1 perseant blkerror(idesc->id_number, "DUP", blkno);
392 1.1 perseant #ifdef VERBOSE_BLOCKMAP
393 1.6 perseant pwarn("(lbn %d: Holder is %d)\n", idesc->id_lblkno,
394 1.1 perseant testbmap(blkno));
395 1.1 perseant #endif
396 1.1 perseant if (dupblk++ >= MAXDUP) {
397 1.1 perseant pwarn("EXCESSIVE DUP BLKS I=%u",
398 1.6 perseant idesc->id_number);
399 1.1 perseant if (preen)
400 1.1 perseant printf(" (SKIPPING)\n");
401 1.1 perseant else if (reply("CONTINUE") == 0)
402 1.1 perseant errexit("%s", "");
403 1.1 perseant return (STOP);
404 1.1 perseant }
405 1.1 perseant new = (struct dups *)malloc(sizeof(struct dups));
406 1.1 perseant if (new == NULL) {
407 1.1 perseant pfatal("DUP TABLE OVERFLOW.");
408 1.1 perseant if (reply("CONTINUE") == 0)
409 1.1 perseant errexit("%s", "");
410 1.1 perseant return (STOP);
411 1.1 perseant }
412 1.1 perseant new->dup = blkno;
413 1.1 perseant if (muldup == 0) {
414 1.1 perseant duplist = muldup = new;
415 1.1 perseant new->next = 0;
416 1.1 perseant } else {
417 1.1 perseant new->next = muldup->next;
418 1.1 perseant muldup->next = new;
419 1.1 perseant }
420 1.1 perseant for (dlp = duplist; dlp != muldup; dlp = dlp->next)
421 1.1 perseant if (dlp->dup == blkno)
422 1.1 perseant break;
423 1.1 perseant if (dlp == muldup && dlp->dup != blkno)
424 1.1 perseant muldup = new;
425 1.1 perseant }
426 1.1 perseant /*
427 1.1 perseant * count the number of blocks found in id_entryno
428 1.1 perseant */
429 1.6 perseant idesc->id_entryno++;
430 1.1 perseant }
431 1.1 perseant return (res);
432 1.1 perseant }
433