pass1.c revision 2603
1/*
2 * Copyright (c) 1980, 1986, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)pass1.c	8.1 (Berkeley) 6/5/93";
36#endif /* not lint */
37
38#include <sys/param.h>
39#include <sys/time.h>
40#include <ufs/ufs/dinode.h>
41#include <ufs/ufs/dir.h>
42#include <ufs/ffs/fs.h>
43#include <stdlib.h>
44#include <string.h>
45#include "fsck.h"
46
47static daddr_t badblk;
48static daddr_t dupblk;
49int pass1check();
50struct dinode *getnextinode();
51
52pass1()
53{
54	ino_t inumber;
55	int c, i, cgd;
56	struct inodesc idesc;
57
58	/*
59	 * Set file system reserved blocks in used block map.
60	 */
61	for (c = 0; c < sblock.fs_ncg; c++) {
62		cgd = cgdmin(&sblock, c);
63		if (c == 0) {
64			i = cgbase(&sblock, c);
65			cgd += howmany(sblock.fs_cssize, sblock.fs_fsize);
66		} else
67			i = cgsblock(&sblock, c);
68		for (; i < cgd; i++)
69			setbmap(i);
70	}
71	/*
72	 * Find all allocated blocks.
73	 */
74	bzero((char *)&idesc, sizeof(struct inodesc));
75	idesc.id_type = ADDR;
76	idesc.id_func = pass1check;
77	inumber = 0;
78	n_files = n_blks = 0;
79	resetinodebuf();
80	for (c = 0; c < sblock.fs_ncg; c++) {
81		for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
82			if (inumber < ROOTINO)
83				continue;
84			checkinode(inumber, &idesc);
85		}
86	}
87	freeinodebuf();
88}
89
90checkinode(inumber, idesc)
91	ino_t inumber;
92	register struct inodesc *idesc;
93{
94	register struct dinode *dp;
95	struct zlncnt *zlnp;
96	int ndb, j;
97	mode_t mode;
98	char *symbuf;
99
100	dp = getnextinode(inumber);
101	mode = dp->di_mode & IFMT;
102	if (mode == 0) {
103		if (bcmp((char *)dp->di_db, (char *)zino.di_db,
104			NDADDR * sizeof(daddr_t)) ||
105		    bcmp((char *)dp->di_ib, (char *)zino.di_ib,
106			NIADDR * sizeof(daddr_t)) ||
107		    dp->di_mode || dp->di_size) {
108			pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber);
109			if (reply("CLEAR") == 1) {
110				dp = ginode(inumber);
111				clearinode(dp);
112				inodirty();
113			}
114		}
115		statemap[inumber] = USTATE;
116		return;
117	}
118	lastino = inumber;
119	if (/* dp->di_size < 0 || */
120	    dp->di_size + sblock.fs_bsize - 1 < dp->di_size) {
121		if (debug)
122			printf("bad size %qu:", dp->di_size);
123		goto unknown;
124	}
125	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
126		dp = ginode(inumber);
127		dp->di_size = sblock.fs_fsize;
128		dp->di_mode = IFREG|0600;
129		inodirty();
130	}
131	ndb = howmany(dp->di_size, sblock.fs_bsize);
132	if (ndb < 0) {
133		if (debug)
134			printf("bad size %qu ndb %d:",
135				dp->di_size, ndb);
136		goto unknown;
137	}
138	if (mode == IFBLK || mode == IFCHR)
139		ndb++;
140	if (mode == IFLNK) {
141		if (doinglevel2 &&
142		    dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
143		    dp->di_blocks != 0) {
144			symbuf = alloca(secsize);
145			if (bread(fsreadfd, symbuf,
146			    fsbtodb(&sblock, dp->di_db[0]),
147			    (long)secsize) != 0)
148				errexit("cannot read symlink");
149			if (debug) {
150				symbuf[dp->di_size] = 0;
151				printf("convert symlink %d(%s) of size %d\n",
152					inumber, symbuf, (long)dp->di_size);
153			}
154			dp = ginode(inumber);
155			bcopy(symbuf, (caddr_t)dp->di_shortlink,
156			    (long)dp->di_size);
157			dp->di_blocks = 0;
158			inodirty();
159		}
160		/*
161		 * Fake ndb value so direct/indirect block checks below
162		 * will detect any garbage after symlink string.
163		 */
164		if ((dp->di_size < sblock.fs_maxsymlinklen) || dp->di_blocks == 0) {
165			ndb = howmany(dp->di_size, sizeof(daddr_t));
166			if (ndb > NDADDR) {
167				j = ndb - NDADDR;
168				for (ndb = 1; j > 1; j--)
169					ndb *= NINDIR(&sblock);
170				ndb += NDADDR;
171			}
172		}
173	}
174	for (j = ndb; j < NDADDR; j++)
175		if (dp->di_db[j] != 0) {
176			if (debug)
177				printf("bad direct addr: %ld\n", dp->di_db[j]);
178			goto unknown;
179		}
180	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
181		ndb /= NINDIR(&sblock);
182	for (; j < NIADDR; j++)
183		if (dp->di_ib[j] != 0) {
184			if (debug)
185				printf("bad indirect addr: %ld\n",
186					dp->di_ib[j]);
187			goto unknown;
188		}
189	if (ftypeok(dp) == 0)
190		goto unknown;
191	n_files++;
192	lncntp[inumber] = dp->di_nlink;
193	if (dp->di_nlink <= 0) {
194		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
195		if (zlnp == NULL) {
196			pfatal("LINK COUNT TABLE OVERFLOW");
197			if (reply("CONTINUE") == 0)
198				errexit("");
199		} else {
200			zlnp->zlncnt = inumber;
201			zlnp->next = zlnhead;
202			zlnhead = zlnp;
203		}
204	}
205	if (mode == IFDIR) {
206		if (dp->di_size == 0)
207			statemap[inumber] = DCLEAR;
208		else
209			statemap[inumber] = DSTATE;
210		cacheino(dp, inumber);
211	} else
212		statemap[inumber] = FSTATE;
213	typemap[inumber] = IFTODT(mode);
214	if (doinglevel2 &&
215	    (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
216		dp = ginode(inumber);
217		dp->di_uid = dp->di_ouid;
218		dp->di_ouid = -1;
219		dp->di_gid = dp->di_ogid;
220		dp->di_ogid = -1;
221		inodirty();
222	}
223	badblk = dupblk = 0;
224	idesc->id_number = inumber;
225	(void)ckinode(dp, idesc);
226	idesc->id_entryno *= btodb(sblock.fs_fsize);
227	if (dp->di_blocks != idesc->id_entryno) {
228		pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)",
229		    inumber, dp->di_blocks, idesc->id_entryno);
230		if (preen)
231			printf(" (CORRECTED)\n");
232		else if (reply("CORRECT") == 0)
233			return;
234		dp = ginode(inumber);
235		dp->di_blocks = idesc->id_entryno;
236		inodirty();
237	}
238	return;
239unknown:
240	pfatal("UNKNOWN FILE TYPE I=%lu", inumber);
241	statemap[inumber] = FCLEAR;
242	if (reply("CLEAR") == 1) {
243		statemap[inumber] = USTATE;
244		dp = ginode(inumber);
245		clearinode(dp);
246		inodirty();
247	}
248}
249
250pass1check(idesc)
251	register struct inodesc *idesc;
252{
253	int res = KEEPON;
254	int anyout, nfrags;
255	daddr_t blkno = idesc->id_blkno;
256	register struct dups *dlp;
257	struct dups *new;
258
259	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
260		blkerror(idesc->id_number, "BAD", blkno);
261		if (badblk++ >= MAXBAD) {
262			pwarn("EXCESSIVE BAD BLKS I=%lu",
263				idesc->id_number);
264			if (preen)
265				printf(" (SKIPPING)\n");
266			else if (reply("CONTINUE") == 0)
267				errexit("");
268			return (STOP);
269		}
270	}
271	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
272		if (anyout && chkrange(blkno, 1)) {
273			res = SKIP;
274		} else if (!testbmap(blkno)) {
275			n_blks++;
276			setbmap(blkno);
277		} else {
278			blkerror(idesc->id_number, "DUP", blkno);
279			if (dupblk++ >= MAXDUP) {
280				pwarn("EXCESSIVE DUP BLKS I=%lu",
281					idesc->id_number);
282				if (preen)
283					printf(" (SKIPPING)\n");
284				else if (reply("CONTINUE") == 0)
285					errexit("");
286				return (STOP);
287			}
288			new = (struct dups *)malloc(sizeof(struct dups));
289			if (new == NULL) {
290				pfatal("DUP TABLE OVERFLOW.");
291				if (reply("CONTINUE") == 0)
292					errexit("");
293				return (STOP);
294			}
295			new->dup = blkno;
296			if (muldup == 0) {
297				duplist = muldup = new;
298				new->next = 0;
299			} else {
300				new->next = muldup->next;
301				muldup->next = new;
302			}
303			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
304				if (dlp->dup == blkno)
305					break;
306			if (dlp == muldup && dlp->dup != blkno)
307				muldup = new;
308		}
309		/*
310		 * count the number of blocks found in id_entryno
311		 */
312		idesc->id_entryno++;
313	}
314	return (res);
315}
316