main.c revision 23675
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 4123675Speterstatic const char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95"; 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> 4823675Speter 491558Srgrimes#include <ufs/ufs/dinode.h> 5023675Speter#include <ufs/ufs/ufsmount.h> 511558Srgrimes#include <ufs/ffs/fs.h> 5223675Speter 5323675Speter#include <ctype.h> 5423675Speter#include <err.h> 551558Srgrimes#include <fstab.h> 561558Srgrimes#include <stdlib.h> 577585Sbde#include <unistd.h> 581558Srgrimes#include <string.h> 591558Srgrimes#include <stdio.h> 6023675Speter 611558Srgrimes#include "fsck.h" 621558Srgrimes 6323675Speterint returntosingle; 6423675Speter 6523675Speterstatic int argtoi __P((int flag, char *req, char *str, int base)); 6623675Speterstatic int docheck __P((struct fstab *fsp)); 6723675Speterstatic int checkfilesys __P((char *filesys, char *mntpt, long auxdata, 6823675Speter int child)); 6923675Speterint main __P((int argc, char *argv[])); 7023675Speter 717585Sbdeint 721558Srgrimesmain(argc, argv) 731558Srgrimes int argc; 741558Srgrimes char *argv[]; 751558Srgrimes{ 761558Srgrimes int ch; 771558Srgrimes int ret, maxrun = 0; 787585Sbde extern char *optarg; 791558Srgrimes extern int optind; 801558Srgrimes 811558Srgrimes sync(); 822153Sdg while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != EOF) { 831558Srgrimes switch (ch) { 841558Srgrimes case 'p': 851558Srgrimes preen++; 861558Srgrimes break; 871558Srgrimes 881558Srgrimes case 'b': 891558Srgrimes bflag = argtoi('b', "number", optarg, 10); 901558Srgrimes printf("Alternate super block location: %d\n", bflag); 911558Srgrimes break; 921558Srgrimes 931558Srgrimes case 'c': 941558Srgrimes cvtlevel = argtoi('c', "conversion level", optarg, 10); 951558Srgrimes break; 968871Srgrimes 971558Srgrimes case 'd': 981558Srgrimes debug++; 991558Srgrimes break; 1001558Srgrimes 1012153Sdg case 'f': 1022153Sdg fflag++; 1032153Sdg break; 1042153Sdg 1051558Srgrimes case 'l': 1061558Srgrimes maxrun = argtoi('l', "number", optarg, 10); 1071558Srgrimes break; 1081558Srgrimes 1091558Srgrimes case 'm': 1101558Srgrimes lfmode = argtoi('m', "mode", optarg, 8); 1111558Srgrimes if (lfmode &~ 07777) 11223675Speter errx(EEXIT, "bad mode to -m: %o", lfmode); 1131558Srgrimes printf("** lost+found creation mode %o\n", lfmode); 1141558Srgrimes break; 1151558Srgrimes 1161558Srgrimes case 'n': 1171558Srgrimes case 'N': 1181558Srgrimes nflag++; 1191558Srgrimes yflag = 0; 1201558Srgrimes break; 1211558Srgrimes 1221558Srgrimes case 'y': 1231558Srgrimes case 'Y': 1241558Srgrimes yflag++; 1251558Srgrimes nflag = 0; 1261558Srgrimes break; 1271558Srgrimes 1281558Srgrimes default: 12923675Speter errx(EEXIT, "%c option?", ch); 1301558Srgrimes } 1311558Srgrimes } 1321558Srgrimes argc -= optind; 1331558Srgrimes argv += optind; 1341558Srgrimes if (signal(SIGINT, SIG_IGN) != SIG_IGN) 1351558Srgrimes (void)signal(SIGINT, catch); 1361558Srgrimes if (preen) 1371558Srgrimes (void)signal(SIGQUIT, catchquit); 1381558Srgrimes if (argc) { 1391558Srgrimes while (argc-- > 0) 1401558Srgrimes (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0); 1411558Srgrimes exit(0); 1421558Srgrimes } 1431558Srgrimes ret = checkfstab(preen, maxrun, docheck, checkfilesys); 1441558Srgrimes if (returntosingle) 1451558Srgrimes exit(2); 1461558Srgrimes exit(ret); 1471558Srgrimes} 1481558Srgrimes 14923675Speterstatic int 1501558Srgrimesargtoi(flag, req, str, base) 1511558Srgrimes int flag; 1521558Srgrimes char *req, *str; 1531558Srgrimes int base; 1541558Srgrimes{ 1551558Srgrimes char *cp; 1561558Srgrimes int ret; 1571558Srgrimes 1581558Srgrimes ret = (int)strtol(str, &cp, base); 1591558Srgrimes if (cp == str || *cp) 16023675Speter errx(EEXIT, "-%c flag requires a %s", flag, req); 1611558Srgrimes return (ret); 1621558Srgrimes} 1631558Srgrimes 1641558Srgrimes/* 1651558Srgrimes * Determine whether a filesystem should be checked. 1661558Srgrimes */ 16723675Speterstatic int 1681558Srgrimesdocheck(fsp) 1691558Srgrimes register struct fstab *fsp; 1701558Srgrimes{ 1711558Srgrimes 1721558Srgrimes if (strcmp(fsp->fs_vfstype, "ufs") || 1731558Srgrimes (strcmp(fsp->fs_type, FSTAB_RW) && 1741558Srgrimes strcmp(fsp->fs_type, FSTAB_RO)) || 1751558Srgrimes fsp->fs_passno == 0) 1761558Srgrimes return (0); 1771558Srgrimes return (1); 1781558Srgrimes} 1791558Srgrimes 1801558Srgrimes/* 1811558Srgrimes * Check the specified filesystem. 1821558Srgrimes */ 1831558Srgrimes/* ARGSUSED */ 18423675Speterstatic int 1851558Srgrimescheckfilesys(filesys, mntpt, auxdata, child) 1861558Srgrimes char *filesys, *mntpt; 1871558Srgrimes long auxdata; 1887585Sbde int child; 1891558Srgrimes{ 19023675Speter ufs_daddr_t n_ffree, n_bfree; 1911558Srgrimes struct dups *dp; 1921558Srgrimes struct zlncnt *zlnp; 19323675Speter int cylno, flags; 1941558Srgrimes 1951558Srgrimes if (preen && child) 1961558Srgrimes (void)signal(SIGQUIT, voidquit); 1971558Srgrimes cdevname = filesys; 1981558Srgrimes if (debug && preen) 1991558Srgrimes pwarn("starting\n"); 20023675Speter switch (setup(filesys)) { 20123675Speter case 0: 2021558Srgrimes if (preen) 2031558Srgrimes pfatal("CAN'T CHECK FILE SYSTEM."); 20423675Speter /* fall through */ 20523675Speter case -1: 2061558Srgrimes return (0); 2071558Srgrimes } 2082153Sdg 2092153Sdg if (preen && sblock.fs_clean && !fflag) { 2102153Sdg pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree + 2112153Sdg sblock.fs_frag * sblock.fs_cstotal.cs_nbfree); 2122153Sdg printf("(%ld frags, %ld blocks, %.1f%% fragmentation)\n", 2132153Sdg sblock.fs_cstotal.cs_nffree, 2142153Sdg sblock.fs_cstotal.cs_nbfree, 2152153Sdg (float)(sblock.fs_cstotal.cs_nffree * 100) / 2162153Sdg sblock.fs_dsize); 2172153Sdg return(0); 2182153Sdg } 2192153Sdg 2201558Srgrimes /* 2211558Srgrimes * 1: scan inodes tallying blocks used 2221558Srgrimes */ 2231558Srgrimes if (preen == 0) { 2241558Srgrimes printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 2251558Srgrimes if (hotroot) 2261558Srgrimes printf("** Root file system\n"); 2271558Srgrimes printf("** Phase 1 - Check Blocks and Sizes\n"); 2281558Srgrimes } 2291558Srgrimes pass1(); 2301558Srgrimes 2311558Srgrimes /* 2321558Srgrimes * 1b: locate first references to duplicates, if any 2331558Srgrimes */ 2341558Srgrimes if (duplist) { 2351558Srgrimes if (preen) 2361558Srgrimes pfatal("INTERNAL ERROR: dups with -p"); 2371558Srgrimes printf("** Phase 1b - Rescan For More DUPS\n"); 2381558Srgrimes pass1b(); 2391558Srgrimes } 2401558Srgrimes 2411558Srgrimes /* 2421558Srgrimes * 2: traverse directories from root to mark all connected directories 2431558Srgrimes */ 2441558Srgrimes if (preen == 0) 2451558Srgrimes printf("** Phase 2 - Check Pathnames\n"); 2461558Srgrimes pass2(); 2471558Srgrimes 2481558Srgrimes /* 2491558Srgrimes * 3: scan inodes looking for disconnected directories 2501558Srgrimes */ 2511558Srgrimes if (preen == 0) 2521558Srgrimes printf("** Phase 3 - Check Connectivity\n"); 2531558Srgrimes pass3(); 2541558Srgrimes 2551558Srgrimes /* 2561558Srgrimes * 4: scan inodes looking for disconnected files; check reference counts 2571558Srgrimes */ 2581558Srgrimes if (preen == 0) 2591558Srgrimes printf("** Phase 4 - Check Reference Counts\n"); 2601558Srgrimes pass4(); 2611558Srgrimes 2621558Srgrimes /* 2631558Srgrimes * 5: check and repair resource counts in cylinder groups 2641558Srgrimes */ 2651558Srgrimes if (preen == 0) 2661558Srgrimes printf("** Phase 5 - Check Cyl groups\n"); 2671558Srgrimes pass5(); 2681558Srgrimes 2691558Srgrimes /* 2701558Srgrimes * print out summary statistics 2711558Srgrimes */ 2721558Srgrimes n_ffree = sblock.fs_cstotal.cs_nffree; 2731558Srgrimes n_bfree = sblock.fs_cstotal.cs_nbfree; 2741558Srgrimes pwarn("%ld files, %ld used, %ld free ", 2751558Srgrimes n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree); 2767585Sbde printf("(%ld frags, %ld blocks, %ld.%ld%% fragmentation)\n", 2771558Srgrimes n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize, 2781558Srgrimes ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10); 2791558Srgrimes if (debug && 2801558Srgrimes (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree)) 2811558Srgrimes printf("%ld files missing\n", n_files); 2821558Srgrimes if (debug) { 2831558Srgrimes n_blks += sblock.fs_ncg * 2841558Srgrimes (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 2851558Srgrimes n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 2861558Srgrimes n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 2871558Srgrimes if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree)) 2881558Srgrimes printf("%ld blocks missing\n", n_blks); 2891558Srgrimes if (duplist != NULL) { 2901558Srgrimes printf("The following duplicate blocks remain:"); 2911558Srgrimes for (dp = duplist; dp; dp = dp->next) 2921558Srgrimes printf(" %ld,", dp->dup); 2931558Srgrimes printf("\n"); 2941558Srgrimes } 2951558Srgrimes if (zlnhead != NULL) { 2961558Srgrimes printf("The following zero link count inodes remain:"); 2971558Srgrimes for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 2981558Srgrimes printf(" %lu,", zlnp->zlncnt); 2991558Srgrimes printf("\n"); 3001558Srgrimes } 3011558Srgrimes } 3021558Srgrimes zlnhead = (struct zlncnt *)0; 3031558Srgrimes duplist = (struct dups *)0; 3041558Srgrimes muldup = (struct dups *)0; 3051558Srgrimes inocleanup(); 3062179Sdg if (fsmodified) { 3071558Srgrimes (void)time(&sblock.fs_time); 3081558Srgrimes sbdirty(); 3091558Srgrimes } 3101558Srgrimes if (cvtlevel && sblk.b_dirty) { 3118871Srgrimes /* 3121558Srgrimes * Write out the duplicate super blocks 3131558Srgrimes */ 3141558Srgrimes for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 3151558Srgrimes bwrite(fswritefd, (char *)&sblock, 3161558Srgrimes fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE); 3171558Srgrimes } 31823675Speter if (!hotroot) { 31923675Speter ckfini(1); 32023675Speter } else { 32123675Speter struct statfs stfs_buf; 32223675Speter /* 32323675Speter * Check to see if root is mounted read-write. 32423675Speter */ 32523675Speter if (statfs("/", &stfs_buf) == 0) 32623675Speter flags = stfs_buf.f_flags; 32723675Speter else 32823675Speter flags = 0; 32923675Speter ckfini(flags & MNT_RDONLY); 33023675Speter } 3311558Srgrimes free(blockmap); 3321558Srgrimes free(statemap); 3331558Srgrimes free((char *)lncntp); 3341558Srgrimes if (!fsmodified) 3351558Srgrimes return (0); 3361558Srgrimes if (!preen) 3371558Srgrimes printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 33818808Sguido if (rerun) 33918808Sguido printf("\n***** PLEASE RERUN FSCK *****\n"); 3401558Srgrimes if (hotroot) { 34123675Speter struct ufs_args args; 34223675Speter int ret; 3431558Srgrimes /* 3441558Srgrimes * We modified the root. Do a mount update on 3451558Srgrimes * it, unless it is read-write, so we can continue. 3461558Srgrimes */ 34723675Speter if (flags & MNT_RDONLY) { 34823675Speter args.fspec = 0; 34923675Speter args.export.ex_flags = 0; 35023675Speter args.export.ex_root = 0; 35123675Speter flags |= MNT_UPDATE | MNT_RELOAD; 35223675Speter ret = mount("ufs", "/", flags, &args); 35323675Speter if (ret == 0) 35423675Speter return (0); 3551558Srgrimes } 3561558Srgrimes if (!preen) 3571558Srgrimes printf("\n***** REBOOT NOW *****\n"); 3581558Srgrimes sync(); 3591558Srgrimes return (4); 3601558Srgrimes } 3611558Srgrimes return (0); 3621558Srgrimes} 363