main.c revision 34266
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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1980, 1986, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95"; 43#endif 44static const char rcsid[] = 45 "$Id: main.c,v 1.12 1997/12/20 22:24:32 bde Exp $"; 46#endif /* not lint */ 47 48#include <sys/param.h> 49#include <sys/time.h> 50#include <sys/mount.h> 51 52#include <ufs/ufs/dinode.h> 53#include <ufs/ufs/ufsmount.h> 54#include <ufs/ffs/fs.h> 55 56#include <ctype.h> 57#include <err.h> 58#include <fstab.h> 59#include <string.h> 60 61#include "fsck.h" 62 63int returntosingle; 64 65static int argtoi __P((int flag, char *req, char *str, int base)); 66static int docheck __P((struct fstab *fsp)); 67static int checkfilesys __P((char *filesys, char *mntpt, long auxdata, 68 int child)); 69int main __P((int argc, char *argv[])); 70 71int 72main(argc, argv) 73 int argc; 74 char *argv[]; 75{ 76 int ch; 77 int ret, maxrun = 0; 78 79 sync(); 80 while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != -1) { 81 switch (ch) { 82 case 'p': 83 preen++; 84 break; 85 86 case 'b': 87 bflag = argtoi('b', "number", optarg, 10); 88 printf("Alternate super block location: %d\n", bflag); 89 break; 90 91 case 'c': 92 cvtlevel = argtoi('c', "conversion level", optarg, 10); 93 break; 94 95 case 'd': 96 debug++; 97 break; 98 99 case 'f': 100 fflag++; 101 break; 102 103 case 'l': 104 maxrun = argtoi('l', "number", optarg, 10); 105 break; 106 107 case 'm': 108 lfmode = argtoi('m', "mode", optarg, 8); 109 if (lfmode &~ 07777) 110 errx(EEXIT, "bad mode to -m: %o", lfmode); 111 printf("** lost+found creation mode %o\n", lfmode); 112 break; 113 114 case 'n': 115 case 'N': 116 nflag++; 117 yflag = 0; 118 break; 119 120 case 'y': 121 case 'Y': 122 yflag++; 123 nflag = 0; 124 break; 125 126 default: 127 errx(EEXIT, "%c option?", ch); 128 } 129 } 130 argc -= optind; 131 argv += optind; 132 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 133 (void)signal(SIGINT, catch); 134 if (preen) 135 (void)signal(SIGQUIT, catchquit); 136 if (argc) { 137 while (argc-- > 0) 138 (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0); 139 exit(0); 140 } 141 ret = checkfstab(preen, maxrun, docheck, checkfilesys); 142 if (returntosingle) 143 exit(2); 144 exit(ret); 145} 146 147static int 148argtoi(flag, req, str, base) 149 int flag; 150 char *req, *str; 151 int base; 152{ 153 char *cp; 154 int ret; 155 156 ret = (int)strtol(str, &cp, base); 157 if (cp == str || *cp) 158 errx(EEXIT, "-%c flag requires a %s", flag, req); 159 return (ret); 160} 161 162/* 163 * Determine whether a filesystem should be checked. 164 */ 165static int 166docheck(fsp) 167 register struct fstab *fsp; 168{ 169 170 if (strcmp(fsp->fs_vfstype, "ufs") || 171 (strcmp(fsp->fs_type, FSTAB_RW) && 172 strcmp(fsp->fs_type, FSTAB_RO)) || 173 fsp->fs_passno == 0) 174 return (0); 175 return (1); 176} 177 178/* 179 * Check the specified filesystem. 180 */ 181/* ARGSUSED */ 182static int 183checkfilesys(filesys, mntpt, auxdata, child) 184 char *filesys, *mntpt; 185 long auxdata; 186 int child; 187{ 188 ufs_daddr_t n_ffree, n_bfree; 189 struct dups *dp; 190 struct zlncnt *zlnp; 191 int cylno, flags; 192 193 if (preen && child) 194 (void)signal(SIGQUIT, voidquit); 195 cdevname = filesys; 196 if (debug && preen) 197 pwarn("starting\n"); 198 switch (setup(filesys)) { 199 case 0: 200 if (preen) 201 pfatal("CAN'T CHECK FILE SYSTEM."); 202 return (0); 203 case -1: 204 pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree + 205 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree); 206 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n", 207 sblock.fs_cstotal.cs_nffree, sblock.fs_cstotal.cs_nbfree, 208 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize); 209 return (0); 210 } 211 212 /* 213 * Cleared if any questions answered no. Used to decide if 214 * the superblock should be marked clean. 215 */ 216 resolved = 1; 217 /* 218 * 1: scan inodes tallying blocks used 219 */ 220 if (preen == 0) { 221 printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 222 if (hotroot) 223 printf("** Root file system\n"); 224 printf("** Phase 1 - Check Blocks and Sizes\n"); 225 } 226 pass1(); 227 228 /* 229 * 1b: locate first references to duplicates, if any 230 */ 231 if (duplist) { 232 if (preen || usedsoftdep) 233 pfatal("INTERNAL ERROR: dups with -p"); 234 printf("** Phase 1b - Rescan For More DUPS\n"); 235 pass1b(); 236 } 237 238 /* 239 * 2: traverse directories from root to mark all connected directories 240 */ 241 if (preen == 0) 242 printf("** Phase 2 - Check Pathnames\n"); 243 pass2(); 244 245 /* 246 * 3: scan inodes looking for disconnected directories 247 */ 248 if (preen == 0) 249 printf("** Phase 3 - Check Connectivity\n"); 250 pass3(); 251 252 /* 253 * 4: scan inodes looking for disconnected files; check reference counts 254 */ 255 if (preen == 0) 256 printf("** Phase 4 - Check Reference Counts\n"); 257 pass4(); 258 259 /* 260 * 5: check and repair resource counts in cylinder groups 261 */ 262 if (preen == 0) 263 printf("** Phase 5 - Check Cyl groups\n"); 264 pass5(); 265 266 /* 267 * print out summary statistics 268 */ 269 n_ffree = sblock.fs_cstotal.cs_nffree; 270 n_bfree = sblock.fs_cstotal.cs_nbfree; 271 pwarn("%ld files, %ld used, %ld free ", 272 n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree); 273 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n", 274 n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize); 275 if (debug && 276 (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree)) 277 printf("%d files missing\n", n_files); 278 if (debug) { 279 n_blks += sblock.fs_ncg * 280 (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 281 n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 282 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 283 if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree)) 284 printf("%d blocks missing\n", n_blks); 285 if (duplist != NULL) { 286 printf("The following duplicate blocks remain:"); 287 for (dp = duplist; dp; dp = dp->next) 288 printf(" %d,", dp->dup); 289 printf("\n"); 290 } 291 if (zlnhead != NULL) { 292 printf("The following zero link count inodes remain:"); 293 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 294 printf(" %u,", zlnp->zlncnt); 295 printf("\n"); 296 } 297 } 298 zlnhead = (struct zlncnt *)0; 299 duplist = (struct dups *)0; 300 muldup = (struct dups *)0; 301 inocleanup(); 302 if (fsmodified) { 303 (void)time(&sblock.fs_time); 304 sbdirty(); 305 } 306 if (cvtlevel && sblk.b_dirty) { 307 /* 308 * Write out the duplicate super blocks 309 */ 310 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 311 bwrite(fswritefd, (char *)&sblock, 312 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE); 313 } 314 if (rerun) 315 resolved = 0; 316 flags = 0; 317 if (hotroot) { 318 struct statfs stfs_buf; 319 /* 320 * Check to see if root is mounted read-write. 321 */ 322 if (statfs("/", &stfs_buf) == 0) 323 flags = stfs_buf.f_flags; 324 if ((flags & MNT_RDONLY) == 0) 325 resolved = 0; 326 } 327 ckfini(resolved); 328 free(blockmap); 329 free(statemap); 330 free((char *)lncntp); 331 if (!fsmodified) 332 return (0); 333 if (!preen) 334 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 335 if (rerun) 336 printf("\n***** PLEASE RERUN FSCK *****\n"); 337 if (hotroot) { 338 struct ufs_args args; 339 int ret; 340 /* 341 * We modified the root. Do a mount update on 342 * it, unless it is read-write, so we can continue. 343 */ 344 if (flags & MNT_RDONLY) { 345 args.fspec = 0; 346 args.export.ex_flags = 0; 347 args.export.ex_root = 0; 348 flags |= MNT_UPDATE | MNT_RELOAD; 349 ret = mount("ufs", "/", flags, &args); 350 if (ret == 0) 351 return (0); 352 } 353 if (!preen) 354 printf("\n***** REBOOT NOW *****\n"); 355 sync(); 356 return (4); 357 } 358 return (0); 359} 360