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