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