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