main.c revision 8871
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 <ctype.h> 551558Srgrimes#include <stdio.h> 561558Srgrimes#include "fsck.h" 577585Sbdestatic int argtoi __P((int flag, char *req, char *str, int base)); 587585Sbdestatic int docheck __P((struct fstab *fsp)); 597585Sbdestatic int checkfilesys __P((char *filesys, char *mntpt, long auxdata, 607585Sbde int child)); 611558Srgrimes 627585Sbdeint 631558Srgrimesmain(argc, argv) 641558Srgrimes int argc; 651558Srgrimes char *argv[]; 661558Srgrimes{ 671558Srgrimes int ch; 681558Srgrimes int ret, maxrun = 0; 697585Sbde extern char *optarg; 701558Srgrimes extern int optind; 711558Srgrimes 721558Srgrimes sync(); 732153Sdg while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != EOF) { 741558Srgrimes switch (ch) { 751558Srgrimes case 'p': 761558Srgrimes preen++; 771558Srgrimes break; 781558Srgrimes 791558Srgrimes case 'b': 801558Srgrimes bflag = argtoi('b', "number", optarg, 10); 811558Srgrimes printf("Alternate super block location: %d\n", bflag); 821558Srgrimes break; 831558Srgrimes 841558Srgrimes case 'c': 851558Srgrimes cvtlevel = argtoi('c', "conversion level", optarg, 10); 861558Srgrimes break; 878871Srgrimes 881558Srgrimes case 'd': 891558Srgrimes debug++; 901558Srgrimes break; 911558Srgrimes 922153Sdg case 'f': 932153Sdg fflag++; 942153Sdg break; 952153Sdg 961558Srgrimes case 'l': 971558Srgrimes maxrun = argtoi('l', "number", optarg, 10); 981558Srgrimes break; 991558Srgrimes 1001558Srgrimes case 'm': 1011558Srgrimes lfmode = argtoi('m', "mode", optarg, 8); 1021558Srgrimes if (lfmode &~ 07777) 1031558Srgrimes errexit("bad mode to -m: %o\n", lfmode); 1041558Srgrimes printf("** lost+found creation mode %o\n", lfmode); 1051558Srgrimes break; 1061558Srgrimes 1071558Srgrimes case 'n': 1081558Srgrimes case 'N': 1091558Srgrimes nflag++; 1101558Srgrimes yflag = 0; 1111558Srgrimes break; 1121558Srgrimes 1131558Srgrimes case 'y': 1141558Srgrimes case 'Y': 1151558Srgrimes yflag++; 1161558Srgrimes nflag = 0; 1171558Srgrimes break; 1181558Srgrimes 1191558Srgrimes default: 1201558Srgrimes errexit("%c option?\n", ch); 1211558Srgrimes } 1221558Srgrimes } 1231558Srgrimes argc -= optind; 1241558Srgrimes argv += optind; 1251558Srgrimes if (signal(SIGINT, SIG_IGN) != SIG_IGN) 1261558Srgrimes (void)signal(SIGINT, catch); 1271558Srgrimes if (preen) 1281558Srgrimes (void)signal(SIGQUIT, catchquit); 1291558Srgrimes if (argc) { 1301558Srgrimes while (argc-- > 0) 1311558Srgrimes (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0); 1321558Srgrimes exit(0); 1331558Srgrimes } 1341558Srgrimes ret = checkfstab(preen, maxrun, docheck, checkfilesys); 1351558Srgrimes if (returntosingle) 1361558Srgrimes exit(2); 1371558Srgrimes exit(ret); 1381558Srgrimes} 1391558Srgrimes 1407585Sbdeint 1411558Srgrimesargtoi(flag, req, str, base) 1421558Srgrimes int flag; 1431558Srgrimes char *req, *str; 1441558Srgrimes int base; 1451558Srgrimes{ 1461558Srgrimes char *cp; 1471558Srgrimes int ret; 1481558Srgrimes 1491558Srgrimes ret = (int)strtol(str, &cp, base); 1501558Srgrimes if (cp == str || *cp) 1511558Srgrimes errexit("-%c flag requires a %s\n", flag, req); 1521558Srgrimes return (ret); 1531558Srgrimes} 1541558Srgrimes 1551558Srgrimes/* 1561558Srgrimes * Determine whether a filesystem should be checked. 1571558Srgrimes */ 1587585Sbdeint 1591558Srgrimesdocheck(fsp) 1601558Srgrimes register struct fstab *fsp; 1611558Srgrimes{ 1621558Srgrimes 1631558Srgrimes if (strcmp(fsp->fs_vfstype, "ufs") || 1641558Srgrimes (strcmp(fsp->fs_type, FSTAB_RW) && 1651558Srgrimes strcmp(fsp->fs_type, FSTAB_RO)) || 1661558Srgrimes fsp->fs_passno == 0) 1671558Srgrimes return (0); 1681558Srgrimes return (1); 1691558Srgrimes} 1701558Srgrimes 1711558Srgrimes/* 1721558Srgrimes * Check the specified filesystem. 1731558Srgrimes */ 1741558Srgrimes/* ARGSUSED */ 1757585Sbdeint 1761558Srgrimescheckfilesys(filesys, mntpt, auxdata, child) 1771558Srgrimes char *filesys, *mntpt; 1781558Srgrimes long auxdata; 1797585Sbde int child; 1801558Srgrimes{ 1811558Srgrimes daddr_t n_ffree, n_bfree; 1821558Srgrimes struct dups *dp; 1831558Srgrimes struct zlncnt *zlnp; 1841558Srgrimes int cylno; 1851558Srgrimes 1861558Srgrimes if (preen && child) 1871558Srgrimes (void)signal(SIGQUIT, voidquit); 1881558Srgrimes cdevname = filesys; 1891558Srgrimes if (debug && preen) 1901558Srgrimes pwarn("starting\n"); 1911558Srgrimes if (setup(filesys) == 0) { 1921558Srgrimes if (preen) 1931558Srgrimes pfatal("CAN'T CHECK FILE SYSTEM."); 1941558Srgrimes return (0); 1951558Srgrimes } 1962153Sdg 1972153Sdg if (preen && sblock.fs_clean && !fflag) { 1982153Sdg pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree + 1992153Sdg sblock.fs_frag * sblock.fs_cstotal.cs_nbfree); 2002153Sdg printf("(%ld frags, %ld blocks, %.1f%% fragmentation)\n", 2012153Sdg sblock.fs_cstotal.cs_nffree, 2022153Sdg sblock.fs_cstotal.cs_nbfree, 2032153Sdg (float)(sblock.fs_cstotal.cs_nffree * 100) / 2042153Sdg sblock.fs_dsize); 2052153Sdg return(0); 2062153Sdg } 2072153Sdg 2081558Srgrimes /* 2091558Srgrimes * 1: scan inodes tallying blocks used 2101558Srgrimes */ 2111558Srgrimes if (preen == 0) { 2121558Srgrimes printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 2131558Srgrimes if (hotroot) 2141558Srgrimes printf("** Root file system\n"); 2151558Srgrimes printf("** Phase 1 - Check Blocks and Sizes\n"); 2161558Srgrimes } 2171558Srgrimes pass1(); 2181558Srgrimes 2191558Srgrimes /* 2201558Srgrimes * 1b: locate first references to duplicates, if any 2211558Srgrimes */ 2221558Srgrimes if (duplist) { 2231558Srgrimes if (preen) 2241558Srgrimes pfatal("INTERNAL ERROR: dups with -p"); 2251558Srgrimes printf("** Phase 1b - Rescan For More DUPS\n"); 2261558Srgrimes pass1b(); 2271558Srgrimes } 2281558Srgrimes 2291558Srgrimes /* 2301558Srgrimes * 2: traverse directories from root to mark all connected directories 2311558Srgrimes */ 2321558Srgrimes if (preen == 0) 2331558Srgrimes printf("** Phase 2 - Check Pathnames\n"); 2341558Srgrimes pass2(); 2351558Srgrimes 2361558Srgrimes /* 2371558Srgrimes * 3: scan inodes looking for disconnected directories 2381558Srgrimes */ 2391558Srgrimes if (preen == 0) 2401558Srgrimes printf("** Phase 3 - Check Connectivity\n"); 2411558Srgrimes pass3(); 2421558Srgrimes 2431558Srgrimes /* 2441558Srgrimes * 4: scan inodes looking for disconnected files; check reference counts 2451558Srgrimes */ 2461558Srgrimes if (preen == 0) 2471558Srgrimes printf("** Phase 4 - Check Reference Counts\n"); 2481558Srgrimes pass4(); 2491558Srgrimes 2501558Srgrimes /* 2511558Srgrimes * 5: check and repair resource counts in cylinder groups 2521558Srgrimes */ 2531558Srgrimes if (preen == 0) 2541558Srgrimes printf("** Phase 5 - Check Cyl groups\n"); 2551558Srgrimes pass5(); 2561558Srgrimes 2571558Srgrimes /* 2581558Srgrimes * print out summary statistics 2591558Srgrimes */ 2601558Srgrimes n_ffree = sblock.fs_cstotal.cs_nffree; 2611558Srgrimes n_bfree = sblock.fs_cstotal.cs_nbfree; 2621558Srgrimes pwarn("%ld files, %ld used, %ld free ", 2631558Srgrimes n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree); 2647585Sbde printf("(%ld frags, %ld blocks, %ld.%ld%% fragmentation)\n", 2651558Srgrimes n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize, 2661558Srgrimes ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10); 2671558Srgrimes if (debug && 2681558Srgrimes (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree)) 2691558Srgrimes printf("%ld files missing\n", n_files); 2701558Srgrimes if (debug) { 2711558Srgrimes n_blks += sblock.fs_ncg * 2721558Srgrimes (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 2731558Srgrimes n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 2741558Srgrimes n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 2751558Srgrimes if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree)) 2761558Srgrimes printf("%ld blocks missing\n", n_blks); 2771558Srgrimes if (duplist != NULL) { 2781558Srgrimes printf("The following duplicate blocks remain:"); 2791558Srgrimes for (dp = duplist; dp; dp = dp->next) 2801558Srgrimes printf(" %ld,", dp->dup); 2811558Srgrimes printf("\n"); 2821558Srgrimes } 2831558Srgrimes if (zlnhead != NULL) { 2841558Srgrimes printf("The following zero link count inodes remain:"); 2851558Srgrimes for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 2861558Srgrimes printf(" %lu,", zlnp->zlncnt); 2871558Srgrimes printf("\n"); 2881558Srgrimes } 2891558Srgrimes } 2901558Srgrimes zlnhead = (struct zlncnt *)0; 2911558Srgrimes duplist = (struct dups *)0; 2921558Srgrimes muldup = (struct dups *)0; 2931558Srgrimes inocleanup(); 2942179Sdg if (fsmodified) { 2951558Srgrimes (void)time(&sblock.fs_time); 2961558Srgrimes sbdirty(); 2971558Srgrimes } 2981558Srgrimes if (cvtlevel && sblk.b_dirty) { 2998871Srgrimes /* 3001558Srgrimes * Write out the duplicate super blocks 3011558Srgrimes */ 3021558Srgrimes for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 3031558Srgrimes bwrite(fswritefd, (char *)&sblock, 3041558Srgrimes fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE); 3051558Srgrimes } 3061558Srgrimes ckfini(); 3071558Srgrimes free(blockmap); 3081558Srgrimes free(statemap); 3091558Srgrimes free((char *)lncntp); 3101558Srgrimes if (!fsmodified) 3111558Srgrimes return (0); 3121558Srgrimes if (!preen) 3131558Srgrimes printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 3141558Srgrimes if (hotroot) { 3151558Srgrimes struct statfs stfs_buf; 3161558Srgrimes /* 3171558Srgrimes * We modified the root. Do a mount update on 3181558Srgrimes * it, unless it is read-write, so we can continue. 3191558Srgrimes */ 3201558Srgrimes if (statfs("/", &stfs_buf) == 0) { 3211558Srgrimes long flags = stfs_buf.f_flags; 3221558Srgrimes struct ufs_args args; 3231558Srgrimes int ret; 3241558Srgrimes 3251558Srgrimes if (flags & MNT_RDONLY) { 3261558Srgrimes args.fspec = 0; 3271558Srgrimes args.export.ex_flags = 0; 3281558Srgrimes args.export.ex_root = 0; 3291558Srgrimes flags |= MNT_UPDATE | MNT_RELOAD; 3301558Srgrimes ret = mount(MOUNT_UFS, "/", flags, &args); 3311558Srgrimes if (ret == 0) 3321558Srgrimes return(0); 3331558Srgrimes } 3341558Srgrimes } 3351558Srgrimes if (!preen) 3361558Srgrimes printf("\n***** REBOOT NOW *****\n"); 3371558Srgrimes sync(); 3381558Srgrimes return (4); 3391558Srgrimes } 3401558Srgrimes return (0); 3411558Srgrimes} 342