18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
4443b5077SScott Long * Copyright (c) 1980, 1986, 1993
5443b5077SScott Long * The Regents of the University of California. All rights reserved.
6443b5077SScott Long *
7443b5077SScott Long * Redistribution and use in source and binary forms, with or without
8443b5077SScott Long * modification, are permitted provided that the following conditions
9443b5077SScott Long * are met:
10443b5077SScott Long * 1. Redistributions of source code must retain the above copyright
11443b5077SScott Long * notice, this list of conditions and the following disclaimer.
12443b5077SScott Long * 2. Redistributions in binary form must reproduce the above copyright
13443b5077SScott Long * notice, this list of conditions and the following disclaimer in the
14443b5077SScott Long * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
16443b5077SScott Long * may be used to endorse or promote products derived from this software
17443b5077SScott Long * without specific prior written permission.
18443b5077SScott Long *
19443b5077SScott Long * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20443b5077SScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21443b5077SScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22443b5077SScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23443b5077SScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24443b5077SScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25443b5077SScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26443b5077SScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27443b5077SScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28443b5077SScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29443b5077SScott Long * SUCH DAMAGE.
30443b5077SScott Long */
31443b5077SScott Long
32443b5077SScott Long #include <sys/param.h>
33443b5077SScott Long #include <ufs/ufs/dinode.h>
34443b5077SScott Long #include <ufs/ffs/fs.h>
35443b5077SScott Long #include <string.h>
36443b5077SScott Long #include "fsck.h"
37443b5077SScott Long
38443b5077SScott Long long readcnt[BT_NUMBUFTYPES];
39443b5077SScott Long long totalreadcnt[BT_NUMBUFTYPES];
40443b5077SScott Long struct timespec readtime[BT_NUMBUFTYPES];
41443b5077SScott Long struct timespec totalreadtime[BT_NUMBUFTYPES];
42443b5077SScott Long struct timespec startprog;
43443b5077SScott Long struct bufarea sblk; /* file system superblock */
44443b5077SScott Long struct bufarea *pdirbp; /* current directory contents */
45443b5077SScott Long ino_t cursnapshot;
46f6717697SPedro F. Giffuni long dirhash, inplast;
47f6717697SPedro F. Giffuni unsigned long numdirs, listmax;
48443b5077SScott Long long countdirs; /* number of directories we actually found */
49*239597e0SKirk McKusick int adjrefcnt[MIBSIZE]; /* MIB cmd to adjust inode reference cnt */
50*239597e0SKirk McKusick int adjblkcnt[MIBSIZE]; /* MIB cmd to adjust inode block count */
51*239597e0SKirk McKusick int setsize[MIBSIZE]; /* MIB cmd to set inode size */
52*239597e0SKirk McKusick int adjndir[MIBSIZE]; /* MIB cmd to adjust number of directories */
53*239597e0SKirk McKusick int adjnbfree[MIBSIZE]; /* MIB cmd to adjust number of free blocks */
54*239597e0SKirk McKusick int adjnifree[MIBSIZE]; /* MIB cmd to adjust number of free inodes */
55*239597e0SKirk McKusick int adjnffree[MIBSIZE]; /* MIB cmd to adjust number of free frags */
56*239597e0SKirk McKusick int adjnumclusters[MIBSIZE]; /* MIB cmd to adjust number of free clusters */
57e4a905d1SKirk McKusick int adjdepth[MIBSIZE]; /* MIB cmd to adjust directory depth count */
58*239597e0SKirk McKusick int freefiles[MIBSIZE]; /* MIB cmd to free a set of files */
59*239597e0SKirk McKusick int freedirs[MIBSIZE]; /* MIB cmd to free a set of directories */
60*239597e0SKirk McKusick int freeblks[MIBSIZE]; /* MIB cmd to free a set of data blocks */
61443b5077SScott Long struct fsck_cmd cmd; /* sysctl file system update commands */
62443b5077SScott Long char *cdevname; /* name of device being checked */
63443b5077SScott Long long dev_bsize; /* computed value of DEV_BSIZE */
64443b5077SScott Long long secsize; /* actual disk sector size */
65463a577bSEitan Adler u_int real_dev_bsize; /* actual disk sector size, not overridden */
66443b5077SScott Long char nflag; /* assume a no response */
67443b5077SScott Long char yflag; /* assume a yes response */
68443b5077SScott Long int bkgrdflag; /* use a snapshot to run on an active system */
69dffce215SKirk McKusick off_t bflag; /* location of alternate super block */
70443b5077SScott Long int debug; /* output debugging info */
71443b5077SScott Long int Eflag; /* delete empty data blocks */
72443b5077SScott Long int Zflag; /* zero empty data blocks */
730061238fSKirk McKusick int zflag; /* zero unused directory space */
74443b5077SScott Long int inoopt; /* trim out unused inodes */
75443b5077SScott Long char ckclean; /* only do work if not cleanly unmounted */
76443b5077SScott Long int cvtlevel; /* convert to newer file system format */
7731461aa2SKirk McKusick int ckhashadd; /* check hashes to be added */
78443b5077SScott Long int bkgrdcheck; /* determine if background check is possible */
79*239597e0SKirk McKusick int bkgrdsumadj; /* kernel able to adjust superblock summary */
80443b5077SScott Long char usedsoftdep; /* just fix soft dependency inconsistencies */
81443b5077SScott Long char preen; /* just fix normal inconsistencies */
82443b5077SScott Long char rerun; /* rerun fsck. Only used in non-preen mode */
83443b5077SScott Long int returntosingle; /* 1 => return to single user mode on exit */
84443b5077SScott Long char resolved; /* cleared if unresolved changes => not clean */
85443b5077SScott Long char havesb; /* superblock has been read */
86443b5077SScott Long char skipclean; /* skip clean file systems if preening */
87443b5077SScott Long int fsmodified; /* 1 => write done to file system */
88443b5077SScott Long int fsreadfd; /* file descriptor for reading file system */
89443b5077SScott Long int fswritefd; /* file descriptor for writing file system */
90443b5077SScott Long int surrender; /* Give up if reads fail */
91443b5077SScott Long int wantrestart; /* Restart fsck on early termination */
92443b5077SScott Long ufs2_daddr_t maxfsblock; /* number of blocks in the file system */
93443b5077SScott Long char *blockmap; /* ptr to primary blk allocation map */
94443b5077SScott Long ino_t maxino; /* number of inodes in file system */
95443b5077SScott Long ino_t lfdir; /* lost & found directory inode number */
96443b5077SScott Long const char *lfname; /* lost & found directory name */
97443b5077SScott Long int lfmode; /* lost & found directory creation mode */
98443b5077SScott Long ufs2_daddr_t n_blks; /* number of blocks in use */
99da86e7a2SKirk McKusick int cgheader_corrupt; /* one or more CG headers are corrupt */
100443b5077SScott Long ino_t n_files; /* number of files in use */
101443b5077SScott Long volatile sig_atomic_t got_siginfo; /* received a SIGINFO */
102443b5077SScott Long volatile sig_atomic_t got_sigalarm; /* received a SIGALRM */
1035cc52631SKirk McKusick union dinode zino;
104443b5077SScott Long
105c3e9752eSKyle Evans struct dups *duplist;
106c3e9752eSKyle Evans struct dups *muldup;
107c3e9752eSKyle Evans struct inostatlist *inostathead;
108c3e9752eSKyle Evans
109443b5077SScott Long void
fsckinit(void)110443b5077SScott Long fsckinit(void)
111443b5077SScott Long {
112443b5077SScott Long bzero(readcnt, sizeof(long) * BT_NUMBUFTYPES);
113443b5077SScott Long bzero(totalreadcnt, sizeof(long) * BT_NUMBUFTYPES);
114443b5077SScott Long bzero(readtime, sizeof(struct timespec) * BT_NUMBUFTYPES);
115443b5077SScott Long bzero(totalreadtime, sizeof(struct timespec) * BT_NUMBUFTYPES);
11680c7cc1cSPedro F. Giffuni bzero(&startprog, sizeof(struct timespec));
117443b5077SScott Long bzero(&sblk, sizeof(struct bufarea));
118443b5077SScott Long cursnapshot = 0;
119f6717697SPedro F. Giffuni listmax = numdirs = dirhash = inplast = 0;
120443b5077SScott Long countdirs = 0;
121443b5077SScott Long bzero(adjrefcnt, sizeof(int) * MIBSIZE);
122443b5077SScott Long bzero(adjblkcnt, sizeof(int) * MIBSIZE);
123ac4b20a0SKirk McKusick bzero(setsize, sizeof(int) * MIBSIZE);
124443b5077SScott Long bzero(adjndir, sizeof(int) * MIBSIZE);
125443b5077SScott Long bzero(adjnbfree, sizeof(int) * MIBSIZE);
126443b5077SScott Long bzero(adjnifree, sizeof(int) * MIBSIZE);
127443b5077SScott Long bzero(adjnffree, sizeof(int) * MIBSIZE);
128443b5077SScott Long bzero(adjnumclusters, sizeof(int) * MIBSIZE);
129e4a905d1SKirk McKusick bzero(adjdepth, sizeof(int) * MIBSIZE);
130443b5077SScott Long bzero(freefiles, sizeof(int) * MIBSIZE);
131443b5077SScott Long bzero(freedirs, sizeof(int) * MIBSIZE);
132443b5077SScott Long bzero(freeblks, sizeof(int) * MIBSIZE);
133443b5077SScott Long bzero(&cmd, sizeof(struct fsck_cmd));
134443b5077SScott Long cdevname = NULL;
135443b5077SScott Long dev_bsize = 0;
136443b5077SScott Long secsize = 0;
137443b5077SScott Long real_dev_bsize = 0;
138443b5077SScott Long bkgrdsumadj = 0;
139443b5077SScott Long usedsoftdep = 0;
140443b5077SScott Long rerun = 0;
141443b5077SScott Long returntosingle = 0;
142443b5077SScott Long resolved = 0;
143443b5077SScott Long havesb = 0;
144443b5077SScott Long fsmodified = 0;
145c0bfa109SKirk McKusick fsreadfd = -1;
146c0bfa109SKirk McKusick fswritefd = -1;
147443b5077SScott Long maxfsblock = 0;
148443b5077SScott Long maxino = 0;
149443b5077SScott Long lfdir = 0;
150443b5077SScott Long lfname = "lost+found";
151443b5077SScott Long lfmode = 0700;
152443b5077SScott Long n_blks = 0;
153443b5077SScott Long n_files = 0;
154da86e7a2SKirk McKusick cgheader_corrupt = 0;
155443b5077SScott Long got_siginfo = 0;
156443b5077SScott Long got_sigalarm = 0;
1575cc52631SKirk McKusick bzero(&zino.dp1, sizeof(struct ufs1_dinode));
1585cc52631SKirk McKusick bzero(&zino.dp2, sizeof(struct ufs2_dinode));
159443b5077SScott Long }
160