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#include <sys/param.h>
33#include <ufs/ufs/dinode.h>
34#include <ufs/ffs/fs.h>
35#include <string.h>
36#include "fsck.h"
37
38long readcnt[BT_NUMBUFTYPES];
39long totalreadcnt[BT_NUMBUFTYPES];
40struct timespec readtime[BT_NUMBUFTYPES];
41struct timespec totalreadtime[BT_NUMBUFTYPES];
42struct timespec startprog;
43struct bufarea sblk;		/* file system superblock */
44struct bufarea *pdirbp;		/* current directory contents */
45ino_t cursnapshot;
46long  dirhash, inplast;
47unsigned long  numdirs, listmax;
48long countdirs;		/* number of directories we actually found */
49int	adjrefcnt[MIBSIZE];	/* MIB cmd to adjust inode reference cnt */
50int	adjblkcnt[MIBSIZE];	/* MIB cmd to adjust inode block count */
51int	setsize[MIBSIZE];	/* MIB cmd to set inode size */
52int	adjndir[MIBSIZE];	/* MIB cmd to adjust number of directories */
53int	adjnbfree[MIBSIZE];	/* MIB cmd to adjust number of free blocks */
54int	adjnifree[MIBSIZE];	/* MIB cmd to adjust number of free inodes */
55int	adjnffree[MIBSIZE];	/* MIB cmd to adjust number of free frags */
56int	adjnumclusters[MIBSIZE]; /* MIB cmd to adjust number of free clusters */
57int	adjdepth[MIBSIZE];	/* MIB cmd to adjust directory depth count */
58int	freefiles[MIBSIZE];	/* MIB cmd to free a set of files */
59int	freedirs[MIBSIZE];	/* MIB cmd to free a set of directories */
60int	freeblks[MIBSIZE];	/* MIB cmd to free a set of data blocks */
61struct	fsck_cmd cmd;		/* sysctl file system update commands */
62char	*cdevname;		/* name of device being checked */
63long	dev_bsize;		/* computed value of DEV_BSIZE */
64long	secsize;		/* actual disk sector size */
65u_int	real_dev_bsize;		/* actual disk sector size, not overridden */
66char	nflag;			/* assume a no response */
67char	yflag;			/* assume a yes response */
68int	bkgrdflag;		/* use a snapshot to run on an active system */
69off_t	bflag;			/* location of alternate super block */
70int	debug;			/* output debugging info */
71int	Eflag;			/* delete empty data blocks */
72int	Zflag;			/* zero empty data blocks */
73int	zflag;			/* zero unused directory space */
74int	inoopt;			/* trim out unused inodes */
75char	ckclean;		/* only do work if not cleanly unmounted */
76int	cvtlevel;		/* convert to newer file system format */
77int	ckhashadd;		/* check hashes to be added */
78int	bkgrdcheck;		/* determine if background check is possible */
79int	bkgrdsumadj;		/* kernel able to adjust superblock summary */
80char	usedsoftdep;		/* just fix soft dependency inconsistencies */
81char	preen;			/* just fix normal inconsistencies */
82char	rerun;			/* rerun fsck. Only used in non-preen mode */
83int	returntosingle;		/* 1 => return to single user mode on exit */
84char	resolved;		/* cleared if unresolved changes => not clean */
85char	havesb;			/* superblock has been read */
86char	skipclean;		/* skip clean file systems if preening */
87int	fsmodified;		/* 1 => write done to file system */
88int	fsreadfd;		/* file descriptor for reading file system */
89int	fswritefd;		/* file descriptor for writing file system */
90int	surrender;		/* Give up if reads fail */
91int	wantrestart;		/* Restart fsck on early termination */
92ufs2_daddr_t maxfsblock;	/* number of blocks in the file system */
93char	*blockmap;		/* ptr to primary blk allocation map */
94ino_t	maxino;			/* number of inodes in file system */
95ino_t	lfdir;			/* lost & found directory inode number */
96const char *lfname;		/* lost & found directory name */
97int	lfmode;			/* lost & found directory creation mode */
98ufs2_daddr_t n_blks;		/* number of blocks in use */
99int	cgheader_corrupt;	/* one or more CG headers are corrupt */
100ino_t n_files;			/* number of files in use */
101volatile sig_atomic_t	got_siginfo;	/* received a SIGINFO */
102volatile sig_atomic_t	got_sigalarm;	/* received a SIGALRM */
103union dinode zino;
104
105struct dups *duplist;
106struct dups *muldup;
107struct inostatlist *inostathead;
108
109void
110fsckinit(void)
111{
112	bzero(readcnt, sizeof(long) * BT_NUMBUFTYPES);
113	bzero(totalreadcnt, sizeof(long) * BT_NUMBUFTYPES);
114	bzero(readtime, sizeof(struct timespec) * BT_NUMBUFTYPES);
115	bzero(totalreadtime, sizeof(struct timespec) * BT_NUMBUFTYPES);
116	bzero(&startprog, sizeof(struct timespec));
117	bzero(&sblk, sizeof(struct bufarea));
118	cursnapshot = 0;
119	listmax = numdirs = dirhash = inplast = 0;
120	countdirs = 0;
121	bzero(adjrefcnt, sizeof(int) * MIBSIZE);
122	bzero(adjblkcnt, sizeof(int) * MIBSIZE);
123	bzero(setsize, sizeof(int) * MIBSIZE);
124	bzero(adjndir, sizeof(int) * MIBSIZE);
125	bzero(adjnbfree, sizeof(int) * MIBSIZE);
126	bzero(adjnifree, sizeof(int) * MIBSIZE);
127	bzero(adjnffree, sizeof(int) * MIBSIZE);
128	bzero(adjnumclusters, sizeof(int) * MIBSIZE);
129	bzero(adjdepth, sizeof(int) * MIBSIZE);
130	bzero(freefiles, sizeof(int) * MIBSIZE);
131	bzero(freedirs, sizeof(int) * MIBSIZE);
132	bzero(freeblks, sizeof(int) * MIBSIZE);
133	bzero(&cmd, sizeof(struct fsck_cmd));
134	cdevname = NULL;
135	dev_bsize = 0;
136	secsize = 0;
137	real_dev_bsize = 0;
138	bkgrdsumadj = 0;
139	usedsoftdep = 0;
140	rerun = 0;
141	returntosingle = 0;
142	resolved = 0;
143	havesb = 0;
144	fsmodified = 0;
145	fsreadfd = -1;
146	fswritefd = -1;
147	maxfsblock = 0;
148	maxino = 0;
149	lfdir = 0;
150	lfname = "lost+found";
151	lfmode = 0700;
152	n_blks = 0;
153	n_files = 0;
154	cgheader_corrupt = 0;
155	got_siginfo = 0;
156	got_sigalarm = 0;
157	bzero(&zino.dp1, sizeof(struct ufs1_dinode));
158	bzero(&zino.dp2, sizeof(struct ufs2_dinode));
159}
160