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