pass1.c revision 1.14 1 1.14 perseant /* $NetBSD: pass1.c,v 1.14 2003/03/28 08:09:54 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.14 perseant #include <sys/mount.h>
39 1.14 perseant #include <sys/buf.h>
40 1.14 perseant
41 1.14 perseant #include <ufs/ufs/inode.h>
42 1.1 perseant #include <ufs/ufs/dir.h>
43 1.14 perseant #define vnode uvnode
44 1.1 perseant #include <ufs/lfs/lfs.h>
45 1.14 perseant #undef vnode
46 1.1 perseant
47 1.14 perseant #include <err.h>
48 1.1 perseant #include <stdio.h>
49 1.1 perseant #include <stdlib.h>
50 1.1 perseant #include <string.h>
51 1.1 perseant
52 1.1 perseant #include <signal.h>
53 1.1 perseant
54 1.14 perseant #include "bufcache.h"
55 1.14 perseant #include "vnode.h"
56 1.14 perseant #include "lfs.h"
57 1.14 perseant
58 1.1 perseant #include "fsck.h"
59 1.1 perseant #include "extern.h"
60 1.1 perseant #include "fsutil.h"
61 1.1 perseant
62 1.14 perseant SEGUSE *seg_table;
63 1.14 perseant extern ufs_daddr_t *din_table;
64 1.1 perseant
65 1.14 perseant ufs_daddr_t badblk;
66 1.14 perseant static ufs_daddr_t dupblk;
67 1.14 perseant static int i_d_cmp(const void *, const void *);
68 1.1 perseant
69 1.5 perseant struct ino_daddr {
70 1.14 perseant ino_t ino;
71 1.14 perseant ufs_daddr_t daddr;
72 1.5 perseant };
73 1.5 perseant
74 1.5 perseant static int
75 1.5 perseant i_d_cmp(const void *va, const void *vb)
76 1.5 perseant {
77 1.5 perseant struct ino_daddr *a, *b;
78 1.5 perseant
79 1.14 perseant a = *((struct ino_daddr **) va);
80 1.14 perseant b = *((struct ino_daddr **) vb);
81 1.5 perseant
82 1.5 perseant if (a->daddr == b->daddr) {
83 1.5 perseant return (a->ino - b->ino);
84 1.5 perseant }
85 1.5 perseant if (a->daddr > b->daddr) {
86 1.5 perseant return 1;
87 1.5 perseant }
88 1.5 perseant return -1;
89 1.5 perseant }
90 1.5 perseant
91 1.1 perseant void
92 1.1 perseant pass1()
93 1.1 perseant {
94 1.14 perseant ino_t inumber;
95 1.14 perseant int i;
96 1.14 perseant struct inodesc idesc;
97 1.14 perseant struct dinode *tinode;
98 1.14 perseant struct ifile *ifp;
99 1.14 perseant struct ubuf *bp;
100 1.5 perseant struct ino_daddr **dins;
101 1.1 perseant
102 1.1 perseant /*
103 1.1 perseant * Find all allocated blocks, initialize numdirs.
104 1.1 perseant */
105 1.1 perseant memset(&idesc, 0, sizeof(struct inodesc));
106 1.1 perseant idesc.id_type = ADDR;
107 1.1 perseant idesc.id_func = pass1check;
108 1.1 perseant idesc.id_lblkno = 0;
109 1.1 perseant inumber = 0;
110 1.1 perseant n_files = n_blks = 0;
111 1.1 perseant
112 1.5 perseant if (debug)
113 1.14 perseant printf("creating sorted inode address table...\n");
114 1.5 perseant /* Sort by daddr */
115 1.14 perseant dins = (struct ino_daddr **) malloc(maxino * sizeof(*dins));
116 1.8 perseant for (i = 0; i < maxino; i++) {
117 1.5 perseant dins[i] = malloc(sizeof(**dins));
118 1.5 perseant dins[i]->ino = i;
119 1.14 perseant if (i == fs->lfs_ifile)
120 1.14 perseant dins[i]->daddr = fs->lfs_idaddr;
121 1.14 perseant else {
122 1.14 perseant LFS_IENTRY(ifp, fs, i, bp);
123 1.14 perseant dins[i]->daddr = ifp->if_daddr;
124 1.14 perseant brelse(bp);
125 1.14 perseant }
126 1.5 perseant }
127 1.8 perseant qsort(dins, maxino, sizeof(*dins), i_d_cmp);
128 1.5 perseant
129 1.5 perseant /* find a value for numdirs, fill in din_table */
130 1.5 perseant if (debug)
131 1.5 perseant printf("counting dirs...\n");
132 1.6 perseant numdirs = 0;
133 1.8 perseant for (i = 0; i < maxino; i++) {
134 1.5 perseant inumber = dins[i]->ino;
135 1.6 perseant if (inumber == 0 || dins[i]->daddr == 0)
136 1.5 perseant continue;
137 1.14 perseant tinode = ginode(inumber);
138 1.6 perseant if (tinode && (tinode->di_mode & IFMT) == IFDIR)
139 1.5 perseant numdirs++;
140 1.1 perseant }
141 1.1 perseant
142 1.1 perseant /* from setup.c */
143 1.6 perseant inplast = 0;
144 1.6 perseant listmax = numdirs + 10;
145 1.14 perseant inpsort = (struct inoinfo **) calloc((unsigned) listmax,
146 1.14 perseant sizeof(struct inoinfo *));
147 1.14 perseant inphead = (struct inoinfo **) calloc((unsigned) numdirs,
148 1.14 perseant sizeof(struct inoinfo *));
149 1.6 perseant if (inpsort == NULL || inphead == NULL) {
150 1.6 perseant printf("cannot alloc %lu bytes for inphead\n",
151 1.14 perseant (unsigned long) numdirs * sizeof(struct inoinfo *));
152 1.1 perseant exit(1);
153 1.6 perseant }
154 1.5 perseant if (debug)
155 1.5 perseant printf("counting blocks...\n");
156 1.14 perseant
157 1.8 perseant for (i = 0; i < maxino; i++) {
158 1.5 perseant inumber = dins[i]->ino;
159 1.7 perseant if (inumber == 0 || dins[i]->daddr == 0) {
160 1.7 perseant statemap[inumber] = USTATE;
161 1.5 perseant continue;
162 1.7 perseant }
163 1.14 perseant if (dins[i]->daddr != LFS_UNUSED_DADDR) {
164 1.5 perseant checkinode(inumber, &idesc);
165 1.5 perseant } else {
166 1.5 perseant statemap[inumber] = USTATE;
167 1.5 perseant }
168 1.5 perseant free(dins[i]);
169 1.6 perseant }
170 1.5 perseant free(dins);
171 1.1 perseant }
172 1.1 perseant
173 1.14 perseant void
174 1.6 perseant checkinode(ino_t inumber, struct inodesc * idesc)
175 1.1 perseant {
176 1.14 perseant struct dinode *dp;
177 1.14 perseant struct uvnode *vp;
178 1.14 perseant struct zlncnt *zlnp;
179 1.14 perseant int ndb, j;
180 1.14 perseant mode_t mode;
181 1.1 perseant
182 1.14 perseant vp = vget(fs, inumber);
183 1.14 perseant dp = VTOD(vp);
184 1.1 perseant
185 1.6 perseant if (dp == NULL) {
186 1.6 perseant /* pwarn("Could not find inode %ld\n",(long)inumber); */
187 1.6 perseant statemap[inumber] = USTATE;
188 1.6 perseant return;
189 1.6 perseant }
190 1.1 perseant mode = dp->di_mode & IFMT;
191 1.1 perseant
192 1.6 perseant /* XXX - LFS doesn't have this particular problem (?) */
193 1.1 perseant if (mode == 0) {
194 1.14 perseant if (memcmp(dp->di_db, zino.di_db, NDADDR * sizeof(ufs_daddr_t)) ||
195 1.14 perseant memcmp(dp->di_ib, zino.di_ib, NIADDR * sizeof(ufs_daddr_t)) ||
196 1.1 perseant dp->di_mode || dp->di_size) {
197 1.1 perseant pwarn("mode=o%o, ifmt=o%o\n", dp->di_mode, mode);
198 1.1 perseant pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
199 1.1 perseant if (reply("CLEAR") == 1) {
200 1.14 perseant vp = vget(fs, inumber);
201 1.14 perseant dp = VTOD(vp);
202 1.1 perseant clearinode(dp);
203 1.14 perseant inodirty(VTOI(vp));
204 1.1 perseant }
205 1.1 perseant }
206 1.1 perseant statemap[inumber] = USTATE;
207 1.1 perseant return;
208 1.1 perseant }
209 1.1 perseant lastino = inumber;
210 1.14 perseant if (dp->di_size < 0 || dp->di_size + fs->lfs_bsize - 1 < dp->di_size) {
211 1.1 perseant if (debug)
212 1.9 lukem printf("bad size %llu:",
213 1.14 perseant (unsigned long long) dp->di_size);
214 1.1 perseant goto unknown;
215 1.1 perseant }
216 1.1 perseant if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
217 1.14 perseant vp = vget(fs, inumber);
218 1.14 perseant dp = VTOD(vp);
219 1.14 perseant dp->di_size = fs->lfs_fsize;
220 1.6 perseant dp->di_mode = IFREG | 0600;
221 1.14 perseant inodirty(VTOI(vp));
222 1.1 perseant }
223 1.14 perseant ndb = howmany(dp->di_size, fs->lfs_bsize);
224 1.1 perseant if (ndb < 0) {
225 1.1 perseant if (debug)
226 1.9 lukem printf("bad size %llu ndb %d:",
227 1.14 perseant (unsigned long long) dp->di_size, ndb);
228 1.1 perseant goto unknown;
229 1.1 perseant }
230 1.1 perseant if (mode == IFBLK || mode == IFCHR)
231 1.1 perseant ndb++;
232 1.1 perseant if (mode == IFLNK) {
233 1.1 perseant /*
234 1.1 perseant * Fake ndb value so direct/indirect block checks below
235 1.1 perseant * will detect any garbage after symlink string.
236 1.1 perseant */
237 1.14 perseant if (dp->di_size < fs->lfs_maxsymlinklen ||
238 1.14 perseant (fs->lfs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
239 1.14 perseant ndb = howmany(dp->di_size, sizeof(ufs_daddr_t));
240 1.1 perseant if (ndb > NDADDR) {
241 1.1 perseant j = ndb - NDADDR;
242 1.1 perseant for (ndb = 1; j > 1; j--)
243 1.14 perseant ndb *= NINDIR(fs);
244 1.1 perseant ndb += NDADDR;
245 1.1 perseant }
246 1.1 perseant }
247 1.1 perseant }
248 1.1 perseant for (j = ndb; j < NDADDR; j++)
249 1.1 perseant if (dp->di_db[j] != 0) {
250 1.1 perseant if (debug)
251 1.1 perseant printf("bad direct addr: %d\n", dp->di_db[j]);
252 1.1 perseant goto unknown;
253 1.1 perseant }
254 1.1 perseant for (j = 0, ndb -= NDADDR; ndb > 0; j++)
255 1.14 perseant ndb /= NINDIR(fs);
256 1.1 perseant for (; j < NIADDR; j++)
257 1.1 perseant if (dp->di_ib[j] != 0) {
258 1.1 perseant if (debug)
259 1.1 perseant printf("bad indirect addr: %d\n",
260 1.14 perseant dp->di_ib[j]);
261 1.5 perseant /* goto unknown; */
262 1.1 perseant }
263 1.1 perseant if (ftypeok(dp) == 0)
264 1.1 perseant goto unknown;
265 1.1 perseant n_files++;
266 1.1 perseant lncntp[inumber] = dp->di_nlink;
267 1.1 perseant if (dp->di_nlink <= 0) {
268 1.14 perseant zlnp = (struct zlncnt *) malloc(sizeof *zlnp);
269 1.1 perseant if (zlnp == NULL) {
270 1.1 perseant pfatal("LINK COUNT TABLE OVERFLOW");
271 1.1 perseant if (reply("CONTINUE") == 0)
272 1.14 perseant err(8, "%s", "");
273 1.1 perseant } else {
274 1.1 perseant zlnp->zlncnt = inumber;
275 1.1 perseant zlnp->next = zlnhead;
276 1.1 perseant zlnhead = zlnp;
277 1.1 perseant }
278 1.1 perseant }
279 1.1 perseant if (mode == IFDIR) {
280 1.1 perseant if (dp->di_size == 0)
281 1.1 perseant statemap[inumber] = DCLEAR;
282 1.1 perseant else
283 1.1 perseant statemap[inumber] = DSTATE;
284 1.1 perseant cacheino(dp, inumber);
285 1.1 perseant } else
286 1.1 perseant statemap[inumber] = FSTATE;
287 1.1 perseant typemap[inumber] = IFTODT(mode);
288 1.1 perseant badblk = dupblk = 0;
289 1.1 perseant idesc->id_number = inumber;
290 1.14 perseant (void) ckinode(VTOD(vp), idesc);
291 1.1 perseant if (dp->di_blocks != idesc->id_entryno) {
292 1.1 perseant pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
293 1.14 perseant inumber, dp->di_blocks, idesc->id_entryno);
294 1.1 perseant if (preen)
295 1.1 perseant printf(" (CORRECTED)\n");
296 1.1 perseant else if (reply("CORRECT") == 0)
297 1.1 perseant return;
298 1.14 perseant VTOI(vp)->i_ffs_blocks = idesc->id_entryno;
299 1.14 perseant inodirty(VTOI(vp));
300 1.1 perseant }
301 1.1 perseant return;
302 1.1 perseant unknown:
303 1.1 perseant pfatal("UNKNOWN FILE TYPE I=%u", inumber);
304 1.1 perseant statemap[inumber] = FCLEAR;
305 1.1 perseant if (reply("CLEAR") == 1) {
306 1.1 perseant statemap[inumber] = USTATE;
307 1.14 perseant vp = vget(fs, inumber);
308 1.14 perseant dp = VTOD(vp);
309 1.1 perseant clearinode(dp);
310 1.14 perseant inodirty(VTOI(vp));
311 1.1 perseant }
312 1.1 perseant }
313 1.1 perseant
314 1.1 perseant int
315 1.14 perseant pass1check(struct inodesc *idesc)
316 1.1 perseant {
317 1.14 perseant int res = KEEPON;
318 1.14 perseant int anyout, ndblks;
319 1.14 perseant daddr_t blkno = idesc->id_blkno;
320 1.1 perseant register struct dups *dlp;
321 1.14 perseant struct dups *new;
322 1.1 perseant
323 1.14 perseant if ((anyout = chkrange(blkno, fragstofsb(fs, idesc->id_numfrags))) != 0) {
324 1.1 perseant blkerror(idesc->id_number, "BAD", blkno);
325 1.1 perseant if (badblk++ >= MAXBAD) {
326 1.1 perseant pwarn("EXCESSIVE BAD BLKS I=%u",
327 1.14 perseant idesc->id_number);
328 1.1 perseant if (preen)
329 1.1 perseant printf(" (SKIPPING)\n");
330 1.1 perseant else if (reply("CONTINUE") == 0)
331 1.14 perseant err(8, "%s", "");
332 1.1 perseant return (STOP);
333 1.1 perseant }
334 1.8 perseant } else if (!testbmap(blkno)) {
335 1.14 perseant seg_table[dtosn(fs, blkno)].su_nbytes += idesc->id_numfrags * fs->lfs_fsize;
336 1.1 perseant }
337 1.14 perseant for (ndblks = fragstofsb(fs, idesc->id_numfrags); ndblks > 0; blkno++, ndblks--) {
338 1.1 perseant if (anyout && chkrange(blkno, 1)) {
339 1.1 perseant res = SKIP;
340 1.1 perseant } else if (!testbmap(blkno)) {
341 1.1 perseant n_blks++;
342 1.1 perseant #ifndef VERBOSE_BLOCKMAP
343 1.1 perseant setbmap(blkno);
344 1.1 perseant #else
345 1.6 perseant setbmap(blkno, idesc->id_number);
346 1.1 perseant #endif
347 1.1 perseant } else {
348 1.1 perseant blkerror(idesc->id_number, "DUP", blkno);
349 1.1 perseant #ifdef VERBOSE_BLOCKMAP
350 1.6 perseant pwarn("(lbn %d: Holder is %d)\n", idesc->id_lblkno,
351 1.14 perseant testbmap(blkno));
352 1.1 perseant #endif
353 1.1 perseant if (dupblk++ >= MAXDUP) {
354 1.1 perseant pwarn("EXCESSIVE DUP BLKS I=%u",
355 1.14 perseant idesc->id_number);
356 1.1 perseant if (preen)
357 1.1 perseant printf(" (SKIPPING)\n");
358 1.1 perseant else if (reply("CONTINUE") == 0)
359 1.14 perseant err(8, "%s", "");
360 1.1 perseant return (STOP);
361 1.1 perseant }
362 1.14 perseant new = (struct dups *) malloc(sizeof(struct dups));
363 1.1 perseant if (new == NULL) {
364 1.1 perseant pfatal("DUP TABLE OVERFLOW.");
365 1.1 perseant if (reply("CONTINUE") == 0)
366 1.14 perseant err(8, "%s", "");
367 1.1 perseant return (STOP);
368 1.1 perseant }
369 1.1 perseant new->dup = blkno;
370 1.1 perseant if (muldup == 0) {
371 1.1 perseant duplist = muldup = new;
372 1.1 perseant new->next = 0;
373 1.1 perseant } else {
374 1.1 perseant new->next = muldup->next;
375 1.1 perseant muldup->next = new;
376 1.1 perseant }
377 1.1 perseant for (dlp = duplist; dlp != muldup; dlp = dlp->next)
378 1.1 perseant if (dlp->dup == blkno)
379 1.1 perseant break;
380 1.1 perseant if (dlp == muldup && dlp->dup != blkno)
381 1.1 perseant muldup = new;
382 1.1 perseant }
383 1.1 perseant /*
384 1.1 perseant * count the number of blocks found in id_entryno
385 1.1 perseant */
386 1.6 perseant idesc->id_entryno++;
387 1.1 perseant }
388 1.1 perseant return (res);
389 1.1 perseant }
390