pass1.c revision 63003
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
35#if 0
36static const char sccsid[] = "@(#)pass1.c	8.6 (Berkeley) 4/28/95";
37#endif
38static const char rcsid[] =
39  "$FreeBSD: head/sbin/fsck_ffs/pass1.c 63003 2000-07-12 06:19:22Z mckusick $";
40#endif /* not lint */
41
42#include <sys/param.h>
43#include <sys/stat.h>
44
45#include <ufs/ufs/dinode.h>
46#include <ufs/ufs/dir.h>
47#include <ufs/ffs/fs.h>
48
49#include <err.h>
50#include <string.h>
51
52#include "fsck.h"
53
54static ufs_daddr_t badblk;
55static ufs_daddr_t dupblk;
56static ino_t lastino;		/* last inode in use */
57
58static void checkinode __P((ino_t inumber, struct inodesc *));
59
60void
61pass1()
62{
63	u_int8_t *cp;
64	ino_t inumber;
65	int c, i, cgd, inosused;
66	struct inostat *info;
67	struct inodesc idesc;
68
69	/*
70	 * Set file system reserved blocks in used block map.
71	 */
72	for (c = 0; c < sblock.fs_ncg; c++) {
73		cgd = cgdmin(&sblock, c);
74		if (c == 0) {
75			i = cgbase(&sblock, c);
76			cgd += howmany(sblock.fs_cssize, sblock.fs_fsize);
77		} else
78			i = cgsblock(&sblock, c);
79		for (; i < cgd; i++)
80			setbmap(i);
81	}
82	/*
83	 * Find all allocated blocks.
84	 */
85	memset(&idesc, 0, sizeof(struct inodesc));
86	idesc.id_func = pass1check;
87	n_files = n_blks = 0;
88	for (c = 0; c < sblock.fs_ncg; c++) {
89		inumber = c * sblock.fs_ipg;
90		setinodebuf(inumber);
91		inosused = sblock.fs_ipg;
92		/*
93		 * If we are using soft updates, then we can trust the
94		 * cylinder group inode allocation maps to tell us which
95		 * inodes are allocated. We will scan the used inode map
96		 * to find the inodes that are really in use, and then
97		 * read only those inodes in from disk.
98		 */
99		if (preen && usedsoftdep) {
100			getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
101			if (!cg_chkmagic(&cgrp))
102				pfatal("CG %d: BAD MAGIC NUMBER\n", c);
103			cp = &cg_inosused(&cgrp)[(sblock.fs_ipg - 1) / NBBY];
104			for ( ; inosused > 0; inosused -= NBBY, cp--) {
105				if (*cp == 0)
106					continue;
107				for (i = 1 << (NBBY - 1); i > 0; i >>= 1) {
108					if (*cp & i)
109						break;
110					inosused--;
111				}
112				break;
113			}
114			if (inosused < 0)
115				inosused = 0;
116		}
117		/*
118		 * Allocate inoinfo structures for the allocated inodes.
119		 */
120		inostathead[c].il_numalloced = inosused;
121		if (inosused == 0) {
122			inostathead[c].il_stat = 0;
123			continue;
124		}
125		info = calloc((unsigned)inosused, sizeof(struct inostat));
126		if (info == NULL)
127			pfatal("cannot alloc %u bytes for inoinfo\n",
128			    (unsigned)(sizeof(struct inostat) * inosused));
129		inostathead[c].il_stat = info;
130		/*
131		 * Scan the allocated inodes.
132		 */
133		for (i = 0; i < inosused; i++, inumber++) {
134			if (inumber < ROOTINO) {
135				(void)getnextinode(inumber);
136				continue;
137			}
138			checkinode(inumber, &idesc);
139		}
140		lastino += 1;
141		if (inosused < sblock.fs_ipg || inumber == lastino)
142			continue;
143		/*
144		 * If we were not able to determine in advance which inodes
145		 * were in use, then reduce the size of the inoinfo structure
146		 * to the size necessary to describe the inodes that we
147		 * really found.
148		 */
149		inosused = lastino - (c * sblock.fs_ipg);
150		if (inosused < 0)
151			inosused = 0;
152		inostathead[c].il_numalloced = inosused;
153		if (inosused == 0) {
154			free(inostathead[c].il_stat);
155			inostathead[c].il_stat = 0;
156			continue;
157		}
158		info = calloc((unsigned)inosused, sizeof(struct inostat));
159		if (info == NULL)
160			pfatal("cannot alloc %u bytes for inoinfo\n",
161			    (unsigned)(sizeof(struct inostat) * inosused));
162		memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
163		free(inostathead[c].il_stat);
164		inostathead[c].il_stat = info;
165	}
166	freeinodebuf();
167}
168
169static void
170checkinode(inumber, idesc)
171	ino_t inumber;
172	register struct inodesc *idesc;
173{
174	register struct dinode *dp;
175	struct zlncnt *zlnp;
176	int ndb, j;
177	mode_t mode;
178	char *symbuf;
179
180	dp = getnextinode(inumber);
181	mode = dp->di_mode & IFMT;
182	if (mode == 0) {
183		if (memcmp(dp->di_db, zino.di_db,
184			NDADDR * sizeof(ufs_daddr_t)) ||
185		    memcmp(dp->di_ib, zino.di_ib,
186			NIADDR * sizeof(ufs_daddr_t)) ||
187		    dp->di_mode || dp->di_size) {
188			pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber);
189			if (reply("CLEAR") == 1) {
190				dp = ginode(inumber);
191				clearinode(dp);
192				inodirty();
193			}
194		}
195		inoinfo(inumber)->ino_state = USTATE;
196		return;
197	}
198	lastino = inumber;
199	if (/* dp->di_size < 0 || */
200	    dp->di_size + sblock.fs_bsize - 1 < dp->di_size ||
201	    (mode == IFDIR && dp->di_size > MAXDIRSIZE)) {
202		if (debug)
203			printf("bad size %qu:", dp->di_size);
204		goto unknown;
205	}
206	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
207		dp = ginode(inumber);
208		dp->di_size = sblock.fs_fsize;
209		dp->di_mode = IFREG|0600;
210		inodirty();
211	}
212	if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
213	     mode == IFSOCK) && dp->di_size != 0) {
214		if (debug)
215			printf("bad special-file size %qu:", dp->di_size);
216		goto unknown;
217	}
218	ndb = howmany(dp->di_size, sblock.fs_bsize);
219	if (ndb < 0) {
220		if (debug)
221			printf("bad size %qu ndb %d:",
222				dp->di_size, ndb);
223		goto unknown;
224	}
225	if (mode == IFBLK || mode == IFCHR)
226		ndb++;
227	if (mode == IFLNK) {
228		if (doinglevel2 &&
229		    dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
230		    dp->di_blocks != 0) {
231			symbuf = alloca(secsize);
232			if (bread(fsreadfd, symbuf,
233			    fsbtodb(&sblock, dp->di_db[0]),
234			    (long)secsize) != 0)
235				errx(EEXIT, "cannot read symlink");
236			if (debug) {
237				symbuf[dp->di_size] = 0;
238				printf("convert symlink %lu(%s) of size %ld\n",
239				    (u_long)inumber, symbuf, (long)dp->di_size);
240			}
241			dp = ginode(inumber);
242			memmove(dp->di_shortlink, symbuf, (long)dp->di_size);
243			dp->di_blocks = 0;
244			inodirty();
245		}
246		/*
247		 * Fake ndb value so direct/indirect block checks below
248		 * will detect any garbage after symlink string.
249		 */
250		if (dp->di_size < sblock.fs_maxsymlinklen) {
251			ndb = howmany(dp->di_size, sizeof(ufs_daddr_t));
252			if (ndb > NDADDR) {
253				j = ndb - NDADDR;
254				for (ndb = 1; j > 1; j--)
255					ndb *= NINDIR(&sblock);
256				ndb += NDADDR;
257			}
258		}
259	}
260	for (j = ndb; j < NDADDR; j++)
261		if (dp->di_db[j] != 0) {
262			if (debug)
263				printf("bad direct addr: %ld\n",
264				    (long)dp->di_db[j]);
265			goto unknown;
266		}
267	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
268		ndb /= NINDIR(&sblock);
269	for (; j < NIADDR; j++)
270		if (dp->di_ib[j] != 0) {
271			if (debug)
272				printf("bad indirect addr: %ld\n",
273				    (long)dp->di_ib[j]);
274			goto unknown;
275		}
276	if (ftypeok(dp) == 0)
277		goto unknown;
278	n_files++;
279	inoinfo(inumber)->ino_linkcnt = dp->di_nlink;
280	if (dp->di_nlink <= 0) {
281		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
282		if (zlnp == NULL) {
283			pfatal("LINK COUNT TABLE OVERFLOW");
284			if (reply("CONTINUE") == 0) {
285				ckfini(0);
286				exit(EEXIT);
287			}
288		} else {
289			zlnp->zlncnt = inumber;
290			zlnp->next = zlnhead;
291			zlnhead = zlnp;
292		}
293	}
294	if (mode == IFDIR) {
295		if (dp->di_size == 0)
296			inoinfo(inumber)->ino_state = DCLEAR;
297		else
298			inoinfo(inumber)->ino_state = DSTATE;
299		cacheino(dp, inumber);
300		countdirs++;
301	} else
302		inoinfo(inumber)->ino_state = FSTATE;
303	inoinfo(inumber)->ino_type = IFTODT(mode);
304	if (doinglevel2 &&
305	    (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
306		dp = ginode(inumber);
307		dp->di_uid = dp->di_ouid;
308		dp->di_ouid = -1;
309		dp->di_gid = dp->di_ogid;
310		dp->di_ogid = -1;
311		inodirty();
312	}
313	badblk = dupblk = 0;
314	idesc->id_number = inumber;
315	if (dp->di_flags & SF_SNAPSHOT)
316		idesc->id_type = SNAP;
317	else
318		idesc->id_type = ADDR;
319	(void)ckinode(dp, idesc);
320	idesc->id_entryno *= btodb(sblock.fs_fsize);
321	if (dp->di_blocks != idesc->id_entryno) {
322		pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)",
323		    inumber, dp->di_blocks, idesc->id_entryno);
324		if (preen)
325			printf(" (CORRECTED)\n");
326		else if (reply("CORRECT") == 0)
327			return;
328		dp = ginode(inumber);
329		dp->di_blocks = idesc->id_entryno;
330		inodirty();
331	}
332	return;
333unknown:
334	pfatal("UNKNOWN FILE TYPE I=%lu", inumber);
335	inoinfo(inumber)->ino_state = FCLEAR;
336	if (reply("CLEAR") == 1) {
337		inoinfo(inumber)->ino_state = USTATE;
338		dp = ginode(inumber);
339		clearinode(dp);
340		inodirty();
341	}
342}
343
344int
345pass1check(idesc)
346	register struct inodesc *idesc;
347{
348	int res = KEEPON;
349	int anyout, nfrags;
350	ufs_daddr_t blkno = idesc->id_blkno;
351	register struct dups *dlp;
352	struct dups *new;
353
354	if (idesc->id_type == SNAP) {
355		if (blkno == BLK_NOCOPY)
356			return (KEEPON);
357		if (idesc->id_number == cursnapshot) {
358			if (blkno == blkstofrags(&sblock, idesc->id_lbn))
359				return (KEEPON);
360			if (blkno == BLK_SNAP) {
361				blkno = blkstofrags(&sblock, idesc->id_lbn);
362				idesc->id_entryno -= idesc->id_numfrags;
363			}
364		} else {
365			if (blkno == BLK_SNAP)
366				return (KEEPON);
367		}
368	}
369	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
370		blkerror(idesc->id_number, "BAD", blkno);
371		if (badblk++ >= MAXBAD) {
372			pwarn("EXCESSIVE BAD BLKS I=%lu",
373				idesc->id_number);
374			if (preen)
375				printf(" (SKIPPING)\n");
376			else if (reply("CONTINUE") == 0) {
377				ckfini(0);
378				exit(EEXIT);
379			}
380			return (STOP);
381		}
382	}
383	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
384		if (anyout && chkrange(blkno, 1)) {
385			res = SKIP;
386		} else if (!testbmap(blkno)) {
387			n_blks++;
388			setbmap(blkno);
389		} else {
390			blkerror(idesc->id_number, "DUP", blkno);
391			if (dupblk++ >= MAXDUP) {
392				pwarn("EXCESSIVE DUP BLKS I=%lu",
393					idesc->id_number);
394				if (preen)
395					printf(" (SKIPPING)\n");
396				else if (reply("CONTINUE") == 0) {
397					ckfini(0);
398					exit(EEXIT);
399				}
400				return (STOP);
401			}
402			new = (struct dups *)malloc(sizeof(struct dups));
403			if (new == NULL) {
404				pfatal("DUP TABLE OVERFLOW.");
405				if (reply("CONTINUE") == 0) {
406					ckfini(0);
407					exit(EEXIT);
408				}
409				return (STOP);
410			}
411			new->dup = blkno;
412			if (muldup == 0) {
413				duplist = muldup = new;
414				new->next = 0;
415			} else {
416				new->next = muldup->next;
417				muldup->next = new;
418			}
419			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
420				if (dlp->dup == blkno)
421					break;
422			if (dlp == muldup && dlp->dup != blkno)
423				muldup = new;
424		}
425		/*
426		 * count the number of blocks found in id_entryno
427		 */
428		idesc->id_entryno++;
429	}
430	return (res);
431}
432