1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1980, 1986, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#if 0
33#ifndef lint
34static const char copyright[] =
35"@(#) Copyright (c) 1980, 1986, 1993\n\
36	The Regents of the University of California.  All rights reserved.\n";
37#endif /* not lint */
38
39#ifndef lint
40static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
41#endif /* not lint */
42#endif
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD$");
45
46#include <sys/param.h>
47#include <ufs/ufs/dinode.h>
48#include <ufs/ffs/fs.h>
49#include <string.h>
50#include "fsck.h"
51
52long readcnt[BT_NUMBUFTYPES];
53long totalreadcnt[BT_NUMBUFTYPES];
54struct timespec readtime[BT_NUMBUFTYPES];
55struct timespec totalreadtime[BT_NUMBUFTYPES];
56struct timespec startprog;
57struct bufarea sblk;		/* file system superblock */
58struct bufarea *pdirbp;		/* current directory contents */
59struct bufarea *pbp;		/* current inode block */
60ino_t cursnapshot;
61long  dirhash, inplast;
62unsigned long  numdirs, listmax;
63long countdirs;		/* number of directories we actually found */
64int	adjrefcnt[MIBSIZE];	/* MIB command to adjust inode reference cnt */
65int	adjblkcnt[MIBSIZE];	/* MIB command to adjust inode block count */
66int	setsize[MIBSIZE];	/* MIB command to set inode size */
67int	adjndir[MIBSIZE];	/* MIB command to adjust number of directories */
68int	adjnbfree[MIBSIZE];	/* MIB command to adjust number of free blocks */
69int	adjnifree[MIBSIZE];	/* MIB command to adjust number of free inodes */
70int	adjnffree[MIBSIZE];	/* MIB command to adjust number of free frags */
71int	adjnumclusters[MIBSIZE];	/* MIB command to adjust number of free clusters */
72int	freefiles[MIBSIZE];	/* MIB command to free a set of files */
73int	freedirs[MIBSIZE];	/* MIB command to free a set of directories */
74int	freeblks[MIBSIZE];	/* MIB command to free a set of data blocks */
75struct	fsck_cmd cmd;		/* sysctl file system update commands */
76char	snapname[BUFSIZ];	/* when doing snapshots, the name of the file */
77char	*cdevname;		/* name of device being checked */
78long	dev_bsize;		/* computed value of DEV_BSIZE */
79long	secsize;		/* actual disk sector size */
80u_int	real_dev_bsize;		/* actual disk sector size, not overridden */
81char	nflag;			/* assume a no response */
82char	yflag;			/* assume a yes response */
83int	bkgrdflag;		/* use a snapshot to run on an active system */
84off_t	bflag;			/* location of alternate super block */
85int	debug;			/* output debugging info */
86int	Eflag;			/* delete empty data blocks */
87int	Zflag;			/* zero empty data blocks */
88int	zflag;			/* zero unused directory space */
89int	inoopt;			/* trim out unused inodes */
90char	ckclean;		/* only do work if not cleanly unmounted */
91int	cvtlevel;		/* convert to newer file system format */
92int	ckhashadd;		/* check hashes to be added */
93int	bkgrdcheck;		/* determine if background check is possible */
94int	bkgrdsumadj;		/* whether the kernel have ability to adjust superblock summary */
95char	usedsoftdep;		/* just fix soft dependency inconsistencies */
96char	preen;			/* just fix normal inconsistencies */
97char	rerun;			/* rerun fsck. Only used in non-preen mode */
98int	returntosingle;		/* 1 => return to single user mode on exit */
99char	resolved;		/* cleared if unresolved changes => not clean */
100char	havesb;			/* superblock has been read */
101char	skipclean;		/* skip clean file systems if preening */
102int	fsmodified;		/* 1 => write done to file system */
103int	fsreadfd;		/* file descriptor for reading file system */
104int	fswritefd;		/* file descriptor for writing file system */
105int	surrender;		/* Give up if reads fail */
106int	wantrestart;		/* Restart fsck on early termination */
107ufs2_daddr_t maxfsblock;	/* number of blocks in the file system */
108char	*blockmap;		/* ptr to primary blk allocation map */
109ino_t	maxino;			/* number of inodes in file system */
110ino_t	lfdir;			/* lost & found directory inode number */
111const char *lfname;		/* lost & found directory name */
112int	lfmode;			/* lost & found directory creation mode */
113ufs2_daddr_t n_blks;		/* number of blocks in use */
114ino_t n_files;			/* number of files in use */
115volatile sig_atomic_t	got_siginfo;	/* received a SIGINFO */
116volatile sig_atomic_t	got_sigalarm;	/* received a SIGALRM */
117struct	ufs1_dinode ufs1_zino;
118struct	ufs2_dinode ufs2_zino;
119
120struct dups *duplist;
121struct dups *muldup;
122struct inostatlist *inostathead;
123
124void
125fsckinit(void)
126{
127	bzero(readcnt, sizeof(long) * BT_NUMBUFTYPES);
128	bzero(totalreadcnt, sizeof(long) * BT_NUMBUFTYPES);
129	bzero(readtime, sizeof(struct timespec) * BT_NUMBUFTYPES);
130	bzero(totalreadtime, sizeof(struct timespec) * BT_NUMBUFTYPES);
131	bzero(&startprog, sizeof(struct timespec));
132	bzero(&sblk, sizeof(struct bufarea));
133	pdirbp = NULL;
134	pbp = NULL;
135	cursnapshot = 0;
136	listmax = numdirs = dirhash = inplast = 0;
137	countdirs = 0;
138	bzero(adjrefcnt, sizeof(int) * MIBSIZE);
139	bzero(adjblkcnt, sizeof(int) * MIBSIZE);
140	bzero(setsize, sizeof(int) * MIBSIZE);
141	bzero(adjndir, sizeof(int) * MIBSIZE);
142	bzero(adjnbfree, sizeof(int) * MIBSIZE);
143	bzero(adjnifree, sizeof(int) * MIBSIZE);
144	bzero(adjnffree, sizeof(int) * MIBSIZE);
145	bzero(adjnumclusters, sizeof(int) * MIBSIZE);
146	bzero(freefiles, sizeof(int) * MIBSIZE);
147	bzero(freedirs, sizeof(int) * MIBSIZE);
148	bzero(freeblks, sizeof(int) * MIBSIZE);
149	bzero(&cmd, sizeof(struct fsck_cmd));
150	bzero(snapname, sizeof(char) * BUFSIZ);
151	cdevname = NULL;
152	dev_bsize = 0;
153	secsize = 0;
154	real_dev_bsize = 0;
155	bkgrdsumadj = 0;
156	usedsoftdep = 0;
157	rerun = 0;
158	returntosingle = 0;
159	resolved = 0;
160	havesb = 0;
161	fsmodified = 0;
162	fsreadfd = 0;
163	fswritefd = 0;
164	maxfsblock = 0;
165	blockmap = NULL;
166	maxino = 0;
167	lfdir = 0;
168	lfname = "lost+found";
169	lfmode = 0700;
170	n_blks = 0;
171	n_files = 0;
172	got_siginfo = 0;
173	got_sigalarm = 0;
174	bzero(&ufs1_zino, sizeof(struct ufs1_dinode));
175	bzero(&ufs2_zino, sizeof(struct ufs2_dinode));
176}
177