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