pass1.c revision 136281
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static const char sccsid[] = "@(#)pass1.c	8.6 (Berkeley) 4/28/95";
33#endif /* not lint */
34#endif
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/sbin/fsck_ffs/pass1.c 136281 2004-10-08 20:44:47Z truckman $");
37
38#include <sys/param.h>
39#include <sys/stat.h>
40#include <sys/sysctl.h>
41
42#include <ufs/ufs/dinode.h>
43#include <ufs/ufs/dir.h>
44#include <ufs/ffs/fs.h>
45
46#include <err.h>
47#include <limits.h>
48#include <stdint.h>
49#include <string.h>
50
51#include "fsck.h"
52
53static ufs2_daddr_t badblk;
54static ufs2_daddr_t dupblk;
55static ino_t lastino;		/* last inode in use */
56
57static void checkinode(ino_t inumber, struct inodesc *);
58
59void
60pass1(void)
61{
62	struct inostat *info;
63	struct inodesc idesc;
64	ino_t inumber, inosused;
65	ufs2_daddr_t i, cgd;
66	u_int8_t *cp;
67	int c;
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		} else
77			i = cgsblock(&sblock, c);
78		for (; i < cgd; i++)
79			setbmap(i);
80	}
81	i = sblock.fs_csaddr;
82	cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
83	for (; i < cgd; i++)
84		setbmap(i);
85
86	/*
87	 * Find all allocated blocks.
88	 */
89	memset(&idesc, 0, sizeof(struct inodesc));
90	idesc.id_func = pass1check;
91	n_files = n_blks = 0;
92	for (c = 0; c < sblock.fs_ncg; c++) {
93		inumber = c * sblock.fs_ipg;
94		setinodebuf(inumber);
95		getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
96		if (sblock.fs_magic == FS_UFS2_MAGIC)
97			inosused = cgrp.cg_initediblk;
98		else
99			inosused = sblock.fs_ipg;
100		if (got_siginfo) {
101			printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
102			    cdevname, c, sblock.fs_ncg,
103			    c * 100 / sblock.fs_ncg);
104			got_siginfo = 0;
105		}
106		if (got_sigalarm) {
107			setproctitle("%s p1 %d%%", cdevname,
108			     c * 100 / sblock.fs_ncg);
109			got_sigalarm = 0;
110		}
111		/*
112		 * If we are using soft updates, then we can trust the
113		 * cylinder group inode allocation maps to tell us which
114		 * inodes are allocated. We will scan the used inode map
115		 * to find the inodes that are really in use, and then
116		 * read only those inodes in from disk.
117		 */
118		if (preen && usedsoftdep) {
119			if (!cg_chkmagic(&cgrp))
120				pfatal("CG %d: BAD MAGIC NUMBER\n", c);
121			cp = &cg_inosused(&cgrp)[(inosused - 1) / CHAR_BIT];
122			for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
123				if (*cp == 0)
124					continue;
125				for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
126					if (*cp & i)
127						break;
128					inosused--;
129				}
130				break;
131			}
132			if (inosused < 0)
133				inosused = 0;
134		}
135		/*
136		 * Allocate inoinfo structures for the allocated inodes.
137		 */
138		inostathead[c].il_numalloced = inosused;
139		if (inosused == 0) {
140			inostathead[c].il_stat = 0;
141			continue;
142		}
143		info = calloc((unsigned)inosused, sizeof(struct inostat));
144		if (info == NULL)
145			errx(EEXIT, "cannot alloc %u bytes for inoinfo",
146			    (unsigned)(sizeof(struct inostat) * inosused));
147		inostathead[c].il_stat = info;
148		/*
149		 * Scan the allocated inodes.
150		 */
151		for (i = 0; i < inosused; i++, inumber++) {
152			if (inumber < ROOTINO) {
153				(void)getnextinode(inumber);
154				continue;
155			}
156			checkinode(inumber, &idesc);
157		}
158		lastino += 1;
159		if (inosused < sblock.fs_ipg || inumber == lastino)
160			continue;
161		/*
162		 * If we were not able to determine in advance which inodes
163		 * were in use, then reduce the size of the inoinfo structure
164		 * to the size necessary to describe the inodes that we
165		 * really found.
166		 */
167		if (lastino < (c * sblock.fs_ipg))
168			inosused = 0;
169		else
170			inosused = lastino - (c * sblock.fs_ipg);
171		inostathead[c].il_numalloced = inosused;
172		if (inosused == 0) {
173			free(inostathead[c].il_stat);
174			inostathead[c].il_stat = 0;
175			continue;
176		}
177		info = calloc((unsigned)inosused, sizeof(struct inostat));
178		if (info == NULL)
179			errx(EEXIT, "cannot alloc %u bytes for inoinfo",
180			    (unsigned)(sizeof(struct inostat) * inosused));
181		memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
182		free(inostathead[c].il_stat);
183		inostathead[c].il_stat = info;
184	}
185	freeinodebuf();
186}
187
188static void
189checkinode(ino_t inumber, struct inodesc *idesc)
190{
191	union dinode *dp;
192	off_t kernmaxfilesize;
193	ufs2_daddr_t ndb;
194	mode_t mode;
195	int j, ret, offset;
196
197	dp = getnextinode(inumber);
198	mode = DIP(dp, di_mode) & IFMT;
199	if (mode == 0) {
200		if ((sblock.fs_magic == FS_UFS1_MAGIC &&
201		     (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
202			NDADDR * sizeof(ufs1_daddr_t)) ||
203		      memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
204			NIADDR * sizeof(ufs1_daddr_t)) ||
205		      dp->dp1.di_mode || dp->dp1.di_size)) ||
206		    (sblock.fs_magic == FS_UFS2_MAGIC &&
207		     (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
208			NDADDR * sizeof(ufs2_daddr_t)) ||
209		      memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
210			NIADDR * sizeof(ufs2_daddr_t)) ||
211		      dp->dp2.di_mode || dp->dp2.di_size))) {
212			pfatal("PARTIALLY ALLOCATED INODE I=%lu",
213			    (u_long)inumber);
214			if (reply("CLEAR") == 1) {
215				dp = ginode(inumber);
216				clearinode(dp);
217				inodirty();
218			}
219		}
220		inoinfo(inumber)->ino_state = USTATE;
221		return;
222	}
223	lastino = inumber;
224	/* This should match the file size limit in ffs_mountfs(). */
225	if (sblock.fs_magic == FS_UFS1_MAGIC)
226		kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
227	else
228		kernmaxfilesize = sblock.fs_maxfilesize;
229	if (DIP(dp, di_size) > kernmaxfilesize ||
230	    DIP(dp, di_size) > sblock.fs_maxfilesize ||
231	    (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
232		if (debug)
233			printf("bad size %ju:", (uintmax_t)DIP(dp, di_size));
234		goto unknown;
235	}
236	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
237		dp = ginode(inumber);
238		DIP_SET(dp, di_size, sblock.fs_fsize);
239		DIP_SET(dp, di_mode, IFREG|0600);
240		inodirty();
241	}
242	if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
243	     mode == IFSOCK) && DIP(dp, di_size) != 0) {
244		if (debug)
245			printf("bad special-file size %ju:",
246			    (uintmax_t)DIP(dp, di_size));
247		goto unknown;
248	}
249	if ((mode == IFBLK || mode == IFCHR) &&
250	    (dev_t)DIP(dp, di_rdev) == NODEV) {
251		if (debug)
252			printf("bad special-file rdev NODEV:");
253		goto unknown;
254	}
255	ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
256	if (ndb < 0) {
257		if (debug)
258			printf("bad size %ju ndb %ju:",
259				(uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
260		goto unknown;
261	}
262	if (mode == IFBLK || mode == IFCHR)
263		ndb++;
264	if (mode == IFLNK) {
265		/*
266		 * Fake ndb value so direct/indirect block checks below
267		 * will detect any garbage after symlink string.
268		 */
269		if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
270			if (sblock.fs_magic == FS_UFS1_MAGIC)
271				ndb = howmany(DIP(dp, di_size),
272				    sizeof(ufs1_daddr_t));
273			else
274				ndb = howmany(DIP(dp, di_size),
275				    sizeof(ufs2_daddr_t));
276			if (ndb > NDADDR) {
277				j = ndb - NDADDR;
278				for (ndb = 1; j > 1; j--)
279					ndb *= NINDIR(&sblock);
280				ndb += NDADDR;
281			}
282		}
283	}
284	for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
285		if (DIP(dp, di_db[j]) != 0) {
286			if (debug)
287				printf("bad direct addr[%d]: %ju\n", j,
288				    (uintmax_t)DIP(dp, di_db[j]));
289			goto unknown;
290		}
291	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
292		ndb /= NINDIR(&sblock);
293	for (; j < NIADDR; j++)
294		if (DIP(dp, di_ib[j]) != 0) {
295			if (debug)
296				printf("bad indirect addr: %ju\n",
297				    (uintmax_t)DIP(dp, di_ib[j]));
298			goto unknown;
299		}
300	if (ftypeok(dp) == 0)
301		goto unknown;
302	n_files++;
303	inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
304	if (mode == IFDIR) {
305		if (DIP(dp, di_size) == 0)
306			inoinfo(inumber)->ino_state = DCLEAR;
307		else if (DIP(dp, di_nlink) <= 0)
308			inoinfo(inumber)->ino_state = DZLINK;
309		else
310			inoinfo(inumber)->ino_state = DSTATE;
311		cacheino(dp, inumber);
312		countdirs++;
313	} else if (DIP(dp, di_nlink) <= 0)
314		inoinfo(inumber)->ino_state = FZLINK;
315	else
316		inoinfo(inumber)->ino_state = FSTATE;
317	inoinfo(inumber)->ino_type = IFTODT(mode);
318	badblk = dupblk = 0;
319	idesc->id_number = inumber;
320	if (DIP(dp, di_flags) & SF_SNAPSHOT)
321		idesc->id_type = SNAP;
322	else
323		idesc->id_type = ADDR;
324	(void)ckinode(dp, idesc);
325	if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
326		idesc->id_type = ADDR;
327		ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
328		for (j = 0; j < NXADDR; j++) {
329			if (--ndb == 0 &&
330			    (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
331				idesc->id_numfrags = numfrags(&sblock,
332				    fragroundup(&sblock, offset));
333			else
334				idesc->id_numfrags = sblock.fs_frag;
335			if (dp->dp2.di_extb[j] == 0)
336				continue;
337			idesc->id_blkno = dp->dp2.di_extb[j];
338			ret = (*idesc->id_func)(idesc);
339			if (ret & STOP)
340				break;
341		}
342	}
343	if (sblock.fs_magic == FS_UFS2_MAGIC)
344		eascan(idesc, &dp->dp2);
345	idesc->id_entryno *= btodb(sblock.fs_fsize);
346	if (DIP(dp, di_blocks) != idesc->id_entryno) {
347		pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
348		    (u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
349		    (uintmax_t)idesc->id_entryno);
350		if (preen)
351			printf(" (CORRECTED)\n");
352		else if (reply("CORRECT") == 0)
353			return;
354		if (bkgrdflag == 0) {
355			dp = ginode(inumber);
356			DIP_SET(dp, di_blocks, idesc->id_entryno);
357			inodirty();
358		} else {
359			cmd.value = idesc->id_number;
360			cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
361			if (debug)
362				printf("adjblkcnt ino %ju amount %lld\n",
363				    (uintmax_t)cmd.value, (long long)cmd.size);
364			if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
365			    &cmd, sizeof cmd) == -1)
366				rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
367		}
368	}
369	return;
370unknown:
371	pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
372	inoinfo(inumber)->ino_state = FCLEAR;
373	if (reply("CLEAR") == 1) {
374		inoinfo(inumber)->ino_state = USTATE;
375		dp = ginode(inumber);
376		clearinode(dp);
377		inodirty();
378	}
379}
380
381int
382pass1check(struct inodesc *idesc)
383{
384	int res = KEEPON;
385	int anyout, nfrags;
386	ufs2_daddr_t blkno = idesc->id_blkno;
387	struct dups *dlp;
388	struct dups *new;
389
390	if (idesc->id_type == SNAP) {
391		if (blkno == BLK_NOCOPY)
392			return (KEEPON);
393		if (idesc->id_number == cursnapshot) {
394			if (blkno == blkstofrags(&sblock, idesc->id_lbn))
395				return (KEEPON);
396			if (blkno == BLK_SNAP) {
397				blkno = blkstofrags(&sblock, idesc->id_lbn);
398				idesc->id_entryno -= idesc->id_numfrags;
399			}
400		} else {
401			if (blkno == BLK_SNAP)
402				return (KEEPON);
403		}
404	}
405	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
406		blkerror(idesc->id_number, "BAD", blkno);
407		if (badblk++ >= MAXBAD) {
408			pwarn("EXCESSIVE BAD BLKS I=%lu",
409			    (u_long)idesc->id_number);
410			if (preen)
411				printf(" (SKIPPING)\n");
412			else if (reply("CONTINUE") == 0) {
413				ckfini(0);
414				exit(EEXIT);
415			}
416			return (STOP);
417		}
418	}
419	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
420		if (anyout && chkrange(blkno, 1)) {
421			res = SKIP;
422		} else if (!testbmap(blkno)) {
423			n_blks++;
424			setbmap(blkno);
425		} else {
426			blkerror(idesc->id_number, "DUP", blkno);
427			if (dupblk++ >= MAXDUP) {
428				pwarn("EXCESSIVE DUP BLKS I=%lu",
429					(u_long)idesc->id_number);
430				if (preen)
431					printf(" (SKIPPING)\n");
432				else if (reply("CONTINUE") == 0) {
433					ckfini(0);
434					exit(EEXIT);
435				}
436				return (STOP);
437			}
438			new = (struct dups *)malloc(sizeof(struct dups));
439			if (new == NULL) {
440				pfatal("DUP TABLE OVERFLOW.");
441				if (reply("CONTINUE") == 0) {
442					ckfini(0);
443					exit(EEXIT);
444				}
445				return (STOP);
446			}
447			new->dup = blkno;
448			if (muldup == 0) {
449				duplist = muldup = new;
450				new->next = 0;
451			} else {
452				new->next = muldup->next;
453				muldup->next = new;
454			}
455			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
456				if (dlp->dup == blkno)
457					break;
458			if (dlp == muldup && dlp->dup != blkno)
459				muldup = new;
460		}
461		/*
462		 * count the number of blocks found in id_entryno
463		 */
464		idesc->id_entryno++;
465	}
466	return (res);
467}
468