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