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