main.c revision 18808
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1980, 1986, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 357585Sbdestatic const char copyright[] = 361558Srgrimes"@(#) Copyright (c) 1980, 1986, 1993\n\ 371558Srgrimes The Regents of the University of California. All rights reserved.\n"; 381558Srgrimes#endif /* not lint */ 391558Srgrimes 401558Srgrimes#ifndef lint 417585Sbdestatic const char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/23/94"; 421558Srgrimes#endif /* not lint */ 431558Srgrimes 441558Srgrimes#include <sys/param.h> 451558Srgrimes#include <sys/time.h> 462153Sdg#include <sys/proc.h> 471558Srgrimes#include <sys/mount.h> 481558Srgrimes#include <ufs/ufs/dinode.h> 491558Srgrimes#include <ufs/ffs/fs.h> 501558Srgrimes#include <fstab.h> 511558Srgrimes#include <stdlib.h> 527585Sbde#include <unistd.h> 531558Srgrimes#include <string.h> 541558Srgrimes#include <stdio.h> 551558Srgrimes#include "fsck.h" 567585Sbdestatic int argtoi __P((int flag, char *req, char *str, int base)); 577585Sbdestatic int docheck __P((struct fstab *fsp)); 587585Sbdestatic int checkfilesys __P((char *filesys, char *mntpt, long auxdata, 597585Sbde int child)); 601558Srgrimes 617585Sbdeint 621558Srgrimesmain(argc, argv) 631558Srgrimes int argc; 641558Srgrimes char *argv[]; 651558Srgrimes{ 661558Srgrimes int ch; 671558Srgrimes int ret, maxrun = 0; 687585Sbde extern char *optarg; 691558Srgrimes extern int optind; 701558Srgrimes 711558Srgrimes sync(); 722153Sdg while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != EOF) { 731558Srgrimes switch (ch) { 741558Srgrimes case 'p': 751558Srgrimes preen++; 761558Srgrimes break; 771558Srgrimes 781558Srgrimes case 'b': 791558Srgrimes bflag = argtoi('b', "number", optarg, 10); 801558Srgrimes printf("Alternate super block location: %d\n", bflag); 811558Srgrimes break; 821558Srgrimes 831558Srgrimes case 'c': 841558Srgrimes cvtlevel = argtoi('c', "conversion level", optarg, 10); 851558Srgrimes break; 868871Srgrimes 871558Srgrimes case 'd': 881558Srgrimes debug++; 891558Srgrimes break; 901558Srgrimes 912153Sdg case 'f': 922153Sdg fflag++; 932153Sdg break; 942153Sdg 951558Srgrimes case 'l': 961558Srgrimes maxrun = argtoi('l', "number", optarg, 10); 971558Srgrimes break; 981558Srgrimes 991558Srgrimes case 'm': 1001558Srgrimes lfmode = argtoi('m', "mode", optarg, 8); 1011558Srgrimes if (lfmode &~ 07777) 1021558Srgrimes errexit("bad mode to -m: %o\n", lfmode); 1031558Srgrimes printf("** lost+found creation mode %o\n", lfmode); 1041558Srgrimes break; 1051558Srgrimes 1061558Srgrimes case 'n': 1071558Srgrimes case 'N': 1081558Srgrimes nflag++; 1091558Srgrimes yflag = 0; 1101558Srgrimes break; 1111558Srgrimes 1121558Srgrimes case 'y': 1131558Srgrimes case 'Y': 1141558Srgrimes yflag++; 1151558Srgrimes nflag = 0; 1161558Srgrimes break; 1171558Srgrimes 1181558Srgrimes default: 1191558Srgrimes errexit("%c option?\n", ch); 1201558Srgrimes } 1211558Srgrimes } 1221558Srgrimes argc -= optind; 1231558Srgrimes argv += optind; 1241558Srgrimes if (signal(SIGINT, SIG_IGN) != SIG_IGN) 1251558Srgrimes (void)signal(SIGINT, catch); 1261558Srgrimes if (preen) 1271558Srgrimes (void)signal(SIGQUIT, catchquit); 1281558Srgrimes if (argc) { 1291558Srgrimes while (argc-- > 0) 1301558Srgrimes (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0); 1311558Srgrimes exit(0); 1321558Srgrimes } 1331558Srgrimes ret = checkfstab(preen, maxrun, docheck, checkfilesys); 1341558Srgrimes if (returntosingle) 1351558Srgrimes exit(2); 1361558Srgrimes exit(ret); 1371558Srgrimes} 1381558Srgrimes 1397585Sbdeint 1401558Srgrimesargtoi(flag, req, str, base) 1411558Srgrimes int flag; 1421558Srgrimes char *req, *str; 1431558Srgrimes int base; 1441558Srgrimes{ 1451558Srgrimes char *cp; 1461558Srgrimes int ret; 1471558Srgrimes 1481558Srgrimes ret = (int)strtol(str, &cp, base); 1491558Srgrimes if (cp == str || *cp) 1501558Srgrimes errexit("-%c flag requires a %s\n", flag, req); 1511558Srgrimes return (ret); 1521558Srgrimes} 1531558Srgrimes 1541558Srgrimes/* 1551558Srgrimes * Determine whether a filesystem should be checked. 1561558Srgrimes */ 1577585Sbdeint 1581558Srgrimesdocheck(fsp) 1591558Srgrimes register struct fstab *fsp; 1601558Srgrimes{ 1611558Srgrimes 1621558Srgrimes if (strcmp(fsp->fs_vfstype, "ufs") || 1631558Srgrimes (strcmp(fsp->fs_type, FSTAB_RW) && 1641558Srgrimes strcmp(fsp->fs_type, FSTAB_RO)) || 1651558Srgrimes fsp->fs_passno == 0) 1661558Srgrimes return (0); 1671558Srgrimes return (1); 1681558Srgrimes} 1691558Srgrimes 1701558Srgrimes/* 1711558Srgrimes * Check the specified filesystem. 1721558Srgrimes */ 1731558Srgrimes/* ARGSUSED */ 1747585Sbdeint 1751558Srgrimescheckfilesys(filesys, mntpt, auxdata, child) 1761558Srgrimes char *filesys, *mntpt; 1771558Srgrimes long auxdata; 1787585Sbde int child; 1791558Srgrimes{ 1801558Srgrimes daddr_t n_ffree, n_bfree; 1811558Srgrimes struct dups *dp; 1821558Srgrimes struct zlncnt *zlnp; 1831558Srgrimes int cylno; 1841558Srgrimes 1851558Srgrimes if (preen && child) 1861558Srgrimes (void)signal(SIGQUIT, voidquit); 1871558Srgrimes cdevname = filesys; 1881558Srgrimes if (debug && preen) 1891558Srgrimes pwarn("starting\n"); 1901558Srgrimes if (setup(filesys) == 0) { 1911558Srgrimes if (preen) 1921558Srgrimes pfatal("CAN'T CHECK FILE SYSTEM."); 1931558Srgrimes return (0); 1941558Srgrimes } 1952153Sdg 1962153Sdg if (preen && sblock.fs_clean && !fflag) { 1972153Sdg pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree + 1982153Sdg sblock.fs_frag * sblock.fs_cstotal.cs_nbfree); 1992153Sdg printf("(%ld frags, %ld blocks, %.1f%% fragmentation)\n", 2002153Sdg sblock.fs_cstotal.cs_nffree, 2012153Sdg sblock.fs_cstotal.cs_nbfree, 2022153Sdg (float)(sblock.fs_cstotal.cs_nffree * 100) / 2032153Sdg sblock.fs_dsize); 2042153Sdg return(0); 2052153Sdg } 2062153Sdg 2071558Srgrimes /* 2081558Srgrimes * 1: scan inodes tallying blocks used 2091558Srgrimes */ 2101558Srgrimes if (preen == 0) { 2111558Srgrimes printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 2121558Srgrimes if (hotroot) 2131558Srgrimes printf("** Root file system\n"); 2141558Srgrimes printf("** Phase 1 - Check Blocks and Sizes\n"); 2151558Srgrimes } 2161558Srgrimes pass1(); 2171558Srgrimes 2181558Srgrimes /* 2191558Srgrimes * 1b: locate first references to duplicates, if any 2201558Srgrimes */ 2211558Srgrimes if (duplist) { 2221558Srgrimes if (preen) 2231558Srgrimes pfatal("INTERNAL ERROR: dups with -p"); 2241558Srgrimes printf("** Phase 1b - Rescan For More DUPS\n"); 2251558Srgrimes pass1b(); 2261558Srgrimes } 2271558Srgrimes 2281558Srgrimes /* 2291558Srgrimes * 2: traverse directories from root to mark all connected directories 2301558Srgrimes */ 2311558Srgrimes if (preen == 0) 2321558Srgrimes printf("** Phase 2 - Check Pathnames\n"); 2331558Srgrimes pass2(); 2341558Srgrimes 2351558Srgrimes /* 2361558Srgrimes * 3: scan inodes looking for disconnected directories 2371558Srgrimes */ 2381558Srgrimes if (preen == 0) 2391558Srgrimes printf("** Phase 3 - Check Connectivity\n"); 2401558Srgrimes pass3(); 2411558Srgrimes 2421558Srgrimes /* 2431558Srgrimes * 4: scan inodes looking for disconnected files; check reference counts 2441558Srgrimes */ 2451558Srgrimes if (preen == 0) 2461558Srgrimes printf("** Phase 4 - Check Reference Counts\n"); 2471558Srgrimes pass4(); 2481558Srgrimes 2491558Srgrimes /* 2501558Srgrimes * 5: check and repair resource counts in cylinder groups 2511558Srgrimes */ 2521558Srgrimes if (preen == 0) 2531558Srgrimes printf("** Phase 5 - Check Cyl groups\n"); 2541558Srgrimes pass5(); 2551558Srgrimes 2561558Srgrimes /* 2571558Srgrimes * print out summary statistics 2581558Srgrimes */ 2591558Srgrimes n_ffree = sblock.fs_cstotal.cs_nffree; 2601558Srgrimes n_bfree = sblock.fs_cstotal.cs_nbfree; 2611558Srgrimes pwarn("%ld files, %ld used, %ld free ", 2621558Srgrimes n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree); 2637585Sbde printf("(%ld frags, %ld blocks, %ld.%ld%% fragmentation)\n", 2641558Srgrimes n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize, 2651558Srgrimes ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10); 2661558Srgrimes if (debug && 2671558Srgrimes (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree)) 2681558Srgrimes printf("%ld files missing\n", n_files); 2691558Srgrimes if (debug) { 2701558Srgrimes n_blks += sblock.fs_ncg * 2711558Srgrimes (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 2721558Srgrimes n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 2731558Srgrimes n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 2741558Srgrimes if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree)) 2751558Srgrimes printf("%ld blocks missing\n", n_blks); 2761558Srgrimes if (duplist != NULL) { 2771558Srgrimes printf("The following duplicate blocks remain:"); 2781558Srgrimes for (dp = duplist; dp; dp = dp->next) 2791558Srgrimes printf(" %ld,", dp->dup); 2801558Srgrimes printf("\n"); 2811558Srgrimes } 2821558Srgrimes if (zlnhead != NULL) { 2831558Srgrimes printf("The following zero link count inodes remain:"); 2841558Srgrimes for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 2851558Srgrimes printf(" %lu,", zlnp->zlncnt); 2861558Srgrimes printf("\n"); 2871558Srgrimes } 2881558Srgrimes } 2891558Srgrimes zlnhead = (struct zlncnt *)0; 2901558Srgrimes duplist = (struct dups *)0; 2911558Srgrimes muldup = (struct dups *)0; 2921558Srgrimes inocleanup(); 2932179Sdg if (fsmodified) { 2941558Srgrimes (void)time(&sblock.fs_time); 2951558Srgrimes sbdirty(); 2961558Srgrimes } 2971558Srgrimes if (cvtlevel && sblk.b_dirty) { 2988871Srgrimes /* 2991558Srgrimes * Write out the duplicate super blocks 3001558Srgrimes */ 3011558Srgrimes for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 3021558Srgrimes bwrite(fswritefd, (char *)&sblock, 3031558Srgrimes fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE); 3041558Srgrimes } 3051558Srgrimes ckfini(); 3061558Srgrimes free(blockmap); 3071558Srgrimes free(statemap); 3081558Srgrimes free((char *)lncntp); 3091558Srgrimes if (!fsmodified) 3101558Srgrimes return (0); 3111558Srgrimes if (!preen) 3121558Srgrimes printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 31318808Sguido if (rerun) 31418808Sguido printf("\n***** PLEASE RERUN FSCK *****\n"); 3151558Srgrimes if (hotroot) { 3161558Srgrimes struct statfs stfs_buf; 3171558Srgrimes /* 3181558Srgrimes * We modified the root. Do a mount update on 3191558Srgrimes * it, unless it is read-write, so we can continue. 3201558Srgrimes */ 3211558Srgrimes if (statfs("/", &stfs_buf) == 0) { 3221558Srgrimes long flags = stfs_buf.f_flags; 3231558Srgrimes struct ufs_args args; 3241558Srgrimes int ret; 3251558Srgrimes 3261558Srgrimes if (flags & MNT_RDONLY) { 3271558Srgrimes args.fspec = 0; 3281558Srgrimes args.export.ex_flags = 0; 3291558Srgrimes args.export.ex_root = 0; 3301558Srgrimes flags |= MNT_UPDATE | MNT_RELOAD; 3311558Srgrimes ret = mount(MOUNT_UFS, "/", flags, &args); 3321558Srgrimes if (ret == 0) 3331558Srgrimes return(0); 3341558Srgrimes } 3351558Srgrimes } 3361558Srgrimes if (!preen) 3371558Srgrimes printf("\n***** REBOOT NOW *****\n"); 3381558Srgrimes sync(); 3391558Srgrimes return (4); 3401558Srgrimes } 3411558Srgrimes return (0); 3421558Srgrimes} 343