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