1260076Sscottl/*
2260076Sscottl * Copyright (c) 1980, 1986, 1993
3260076Sscottl *	The Regents of the University of California.  All rights reserved.
4260076Sscottl *
5260076Sscottl * Redistribution and use in source and binary forms, with or without
6260076Sscottl * modification, are permitted provided that the following conditions
7260076Sscottl * are met:
8260076Sscottl * 1. Redistributions of source code must retain the above copyright
9260076Sscottl *    notice, this list of conditions and the following disclaimer.
10260076Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11260076Sscottl *    notice, this list of conditions and the following disclaimer in the
12260076Sscottl *    documentation and/or other materials provided with the distribution.
13260076Sscottl * 4. Neither the name of the University nor the names of its contributors
14260076Sscottl *    may be used to endorse or promote products derived from this software
15260076Sscottl *    without specific prior written permission.
16260076Sscottl *
17260076Sscottl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18260076Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19260076Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20260076Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21260076Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22260076Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23260076Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24260076Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25260076Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26260076Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27260076Sscottl * SUCH DAMAGE.
28260076Sscottl */
29260076Sscottl
30260076Sscottl#if 0
31260076Sscottl#ifndef lint
32260076Sscottlstatic const char copyright[] =
33260076Sscottl"@(#) Copyright (c) 1980, 1986, 1993\n\
34260076Sscottl	The Regents of the University of California.  All rights reserved.\n";
35260076Sscottl#endif /* not lint */
36260076Sscottl
37260076Sscottl#ifndef lint
38260076Sscottlstatic char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
39260076Sscottl#endif /* not lint */
40260076Sscottl#endif
41260076Sscottl#include <sys/cdefs.h>
42260076Sscottl__FBSDID("$FreeBSD$");
43260076Sscottl
44260076Sscottl#include <sys/param.h>
45260076Sscottl#include <ufs/ufs/dinode.h>
46260076Sscottl#include <ufs/ffs/fs.h>
47260076Sscottl#include <string.h>
48260076Sscottl#include "fsck.h"
49260076Sscottl
50260076Sscottllong readcnt[BT_NUMBUFTYPES];
51260076Sscottllong totalreadcnt[BT_NUMBUFTYPES];
52260076Sscottlstruct timespec readtime[BT_NUMBUFTYPES];
53260076Sscottlstruct timespec totalreadtime[BT_NUMBUFTYPES];
54260076Sscottlstruct timespec startprog;
55260076Sscottlstruct bufarea sblk;		/* file system superblock */
56260076Sscottlstruct bufarea *pdirbp;		/* current directory contents */
57260076Sscottlstruct bufarea *pbp;		/* current inode block */
58260076Sscottlino_t cursnapshot;
59260076Sscottllong numdirs, dirhash, listmax, inplast;
60260076Sscottllong countdirs;		/* number of directories we actually found */
61260076Sscottlint	adjrefcnt[MIBSIZE];	/* MIB command to adjust inode reference cnt */
62260076Sscottlint	adjblkcnt[MIBSIZE];	/* MIB command to adjust inode block count */
63260076Sscottlint	adjndir[MIBSIZE];	/* MIB command to adjust number of directories */
64260076Sscottlint	adjnbfree[MIBSIZE];	/* MIB command to adjust number of free blocks */
65260076Sscottlint	adjnifree[MIBSIZE];	/* MIB command to adjust number of free inodes */
66260076Sscottlint	adjnffree[MIBSIZE];	/* MIB command to adjust number of free frags */
67260076Sscottlint	adjnumclusters[MIBSIZE];	/* MIB command to adjust number of free clusters */
68260076Sscottlint	freefiles[MIBSIZE];	/* MIB command to free a set of files */
69260076Sscottlint	freedirs[MIBSIZE];	/* MIB command to free a set of directories */
70260076Sscottlint	freeblks[MIBSIZE];	/* MIB command to free a set of data blocks */
71260076Sscottlstruct	fsck_cmd cmd;		/* sysctl file system update commands */
72260076Sscottlchar	snapname[BUFSIZ];	/* when doing snapshots, the name of the file */
73260076Sscottlchar	*cdevname;		/* name of device being checked */
74260076Sscottllong	dev_bsize;		/* computed value of DEV_BSIZE */
75260076Sscottllong	secsize;		/* actual disk sector size */
76260076Sscottlu_int	real_dev_bsize;		/* actual disk sector size, not overriden */
77260076Sscottlchar	nflag;			/* assume a no response */
78260076Sscottlchar	yflag;			/* assume a yes response */
79260076Sscottlint	bkgrdflag;		/* use a snapshot to run on an active system */
80260076Sscottlint	bflag;			/* location of alternate super block */
81260076Sscottlint	debug;			/* output debugging info */
82260076Sscottlint	Eflag;			/* delete empty data blocks */
83260076Sscottlint	Zflag;			/* zero empty data blocks */
84260076Sscottlint	inoopt;			/* trim out unused inodes */
85260076Sscottlchar	ckclean;		/* only do work if not cleanly unmounted */
86260076Sscottlint	cvtlevel;		/* convert to newer file system format */
87260076Sscottlint	bkgrdcheck;		/* determine if background check is possible */
88260076Sscottlint	bkgrdsumadj;		/* whether the kernel have ability to adjust superblock summary */
89260076Sscottlchar	usedsoftdep;		/* just fix soft dependency inconsistencies */
90260076Sscottlchar	preen;			/* just fix normal inconsistencies */
91260076Sscottlchar	rerun;			/* rerun fsck. Only used in non-preen mode */
92260076Sscottlint	returntosingle;		/* 1 => return to single user mode on exit */
93260076Sscottlchar	resolved;		/* cleared if unresolved changes => not clean */
94260076Sscottlchar	havesb;			/* superblock has been read */
95260076Sscottlchar	skipclean;		/* skip clean file systems if preening */
96260076Sscottlint	fsmodified;		/* 1 => write done to file system */
97260076Sscottlint	fsreadfd;		/* file descriptor for reading file system */
98260076Sscottlint	fswritefd;		/* file descriptor for writing file system */
99260076Sscottlint	surrender;		/* Give up if reads fail */
100260076Sscottlint	wantrestart;		/* Restart fsck on early termination */
101260076Sscottlufs2_daddr_t maxfsblock;	/* number of blocks in the file system */
102260076Sscottlchar	*blockmap;		/* ptr to primary blk allocation map */
103260076Sscottlino_t	maxino;			/* number of inodes in file system */
104260076Sscottlino_t	lfdir;			/* lost & found directory inode number */
105260076Sscottlconst char *lfname;		/* lost & found directory name */
106260076Sscottlint	lfmode;			/* lost & found directory creation mode */
107260076Sscottlufs2_daddr_t n_blks;		/* number of blocks in use */
108260076Sscottlino_t n_files;			/* number of files in use */
109260076Sscottlvolatile sig_atomic_t	got_siginfo;	/* received a SIGINFO */
110260076Sscottlvolatile sig_atomic_t	got_sigalarm;	/* received a SIGALRM */
111260076Sscottlstruct	ufs1_dinode ufs1_zino;
112260076Sscottlstruct	ufs2_dinode ufs2_zino;
113260076Sscottl
114260076Sscottlvoid
115260076Sscottlfsckinit(void)
116260076Sscottl{
117260076Sscottl	bzero(readcnt, sizeof(long) * BT_NUMBUFTYPES);
118260076Sscottl	bzero(totalreadcnt, sizeof(long) * BT_NUMBUFTYPES);
119260076Sscottl	bzero(readtime, sizeof(struct timespec) * BT_NUMBUFTYPES);
120260076Sscottl	bzero(totalreadtime, sizeof(struct timespec) * BT_NUMBUFTYPES);
121260076Sscottl	bzero(&startprog, sizeof(struct timespec));;
122260076Sscottl	bzero(&sblk, sizeof(struct bufarea));
123260076Sscottl	pdirbp = NULL;
124260076Sscottl	pbp = NULL;
125260076Sscottl	cursnapshot = 0;
126260076Sscottl	numdirs = dirhash = listmax = inplast = 0;
127260076Sscottl	countdirs = 0;
128260076Sscottl	bzero(adjrefcnt, sizeof(int) * MIBSIZE);
129260076Sscottl	bzero(adjblkcnt, sizeof(int) * MIBSIZE);
130260076Sscottl	bzero(adjndir, sizeof(int) * MIBSIZE);
131260076Sscottl	bzero(adjnbfree, sizeof(int) * MIBSIZE);
132260076Sscottl	bzero(adjnifree, sizeof(int) * MIBSIZE);
133260076Sscottl	bzero(adjnffree, sizeof(int) * MIBSIZE);
134260076Sscottl	bzero(adjnumclusters, sizeof(int) * MIBSIZE);
135260076Sscottl	bzero(freefiles, sizeof(int) * MIBSIZE);
136260076Sscottl	bzero(freedirs, sizeof(int) * MIBSIZE);
137260076Sscottl	bzero(freeblks, sizeof(int) * MIBSIZE);
138260076Sscottl	bzero(&cmd, sizeof(struct fsck_cmd));
139260076Sscottl	bzero(snapname, sizeof(char) * BUFSIZ);
140260076Sscottl	cdevname = NULL;
141260076Sscottl	dev_bsize = 0;
142260076Sscottl	secsize = 0;
143260076Sscottl	real_dev_bsize = 0;
144260076Sscottl	bkgrdsumadj = 0;
145260076Sscottl	usedsoftdep = 0;
146260076Sscottl	rerun = 0;
147260076Sscottl	returntosingle = 0;
148260076Sscottl	resolved = 0;
149260076Sscottl	havesb = 0;
150260076Sscottl	fsmodified = 0;
151260076Sscottl	fsreadfd = 0;
152260076Sscottl	fswritefd = 0;
153260076Sscottl	maxfsblock = 0;
154260076Sscottl	blockmap = NULL;
155260076Sscottl	maxino = 0;
156260076Sscottl	lfdir = 0;
157260076Sscottl	lfname = "lost+found";
158260076Sscottl	lfmode = 0700;
159260076Sscottl	n_blks = 0;
160260076Sscottl	n_files = 0;
161260076Sscottl	got_siginfo = 0;
162260076Sscottl	got_sigalarm = 0;
163260076Sscottl	bzero(&ufs1_zino, sizeof(struct ufs1_dinode));
164260076Sscottl	bzero(&ufs2_zino, sizeof(struct ufs2_dinode));
165260076Sscottl}
166