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