main.c revision 41474
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 4141474Sjulianstatic 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> 461558Srgrimes#include <sys/mount.h> 4740918Smjacob#include <sys/resource.h> 4823675Speter 491558Srgrimes#include <ufs/ufs/dinode.h> 5023675Speter#include <ufs/ufs/ufsmount.h> 511558Srgrimes#include <ufs/ffs/fs.h> 5223675Speter 5323675Speter#include <err.h> 541558Srgrimes#include <fstab.h> 5541474Sjulian#include <string.h> 5623675Speter 571558Srgrimes#include "fsck.h" 581558Srgrimes 5923675Speterstatic int argtoi __P((int flag, char *req, char *str, int base)); 6023675Speterstatic int docheck __P((struct fstab *fsp)); 6123675Speterstatic int checkfilesys __P((char *filesys, char *mntpt, long auxdata, 6223675Speter int child)); 6323675Speterint main __P((int argc, char *argv[])); 6423675Speter 657585Sbdeint 661558Srgrimesmain(argc, argv) 671558Srgrimes int argc; 681558Srgrimes char *argv[]; 691558Srgrimes{ 701558Srgrimes int ch; 711558Srgrimes int ret, maxrun = 0; 7241474Sjulian struct rlimit rlimit; 731558Srgrimes 741558Srgrimes sync(); 7523795Sbde while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != -1) { 761558Srgrimes switch (ch) { 771558Srgrimes case 'p': 781558Srgrimes preen++; 791558Srgrimes break; 801558Srgrimes 811558Srgrimes case 'b': 821558Srgrimes bflag = argtoi('b', "number", optarg, 10); 831558Srgrimes printf("Alternate super block location: %d\n", bflag); 841558Srgrimes break; 851558Srgrimes 861558Srgrimes case 'c': 871558Srgrimes cvtlevel = argtoi('c', "conversion level", optarg, 10); 881558Srgrimes break; 898871Srgrimes 901558Srgrimes case 'd': 911558Srgrimes debug++; 921558Srgrimes break; 931558Srgrimes 942153Sdg case 'f': 952153Sdg fflag++; 962153Sdg break; 972153Sdg 981558Srgrimes case 'l': 991558Srgrimes maxrun = argtoi('l', "number", optarg, 10); 1001558Srgrimes break; 1011558Srgrimes 1021558Srgrimes case 'm': 1031558Srgrimes lfmode = argtoi('m', "mode", optarg, 8); 1041558Srgrimes if (lfmode &~ 07777) 10523675Speter errx(EEXIT, "bad mode to -m: %o", lfmode); 1061558Srgrimes printf("** lost+found creation mode %o\n", lfmode); 1071558Srgrimes break; 1081558Srgrimes 1091558Srgrimes case 'n': 1101558Srgrimes case 'N': 1111558Srgrimes nflag++; 1121558Srgrimes yflag = 0; 1131558Srgrimes break; 1141558Srgrimes 1151558Srgrimes case 'y': 1161558Srgrimes case 'Y': 1171558Srgrimes yflag++; 1181558Srgrimes nflag = 0; 1191558Srgrimes break; 1201558Srgrimes 1211558Srgrimes default: 12223675Speter errx(EEXIT, "%c option?", ch); 1231558Srgrimes } 1241558Srgrimes } 1251558Srgrimes argc -= optind; 1261558Srgrimes argv += optind; 1271558Srgrimes if (signal(SIGINT, SIG_IGN) != SIG_IGN) 1281558Srgrimes (void)signal(SIGINT, catch); 1291558Srgrimes if (preen) 1301558Srgrimes (void)signal(SIGQUIT, catchquit); 13141474Sjulian /* 13241474Sjulian * Push up our allowed memory limit so we can cope 13341474Sjulian * with huge filesystems. 13441474Sjulian */ 13541474Sjulian if (getrlimit(RLIMIT_DATA, &rlimit) == 0) { 13641474Sjulian rlimit.rlim_cur = rlimit.rlim_max; 13741474Sjulian (void)setrlimit(RLIMIT_DATA, &rlimit); 13841474Sjulian } 1391558Srgrimes if (argc) { 14041474Sjulian while (argc-- > 0) { 14141474Sjulian char *path = blockcheck(*argv); 14241474Sjulian 14341474Sjulian if (path == NULL) 14441474Sjulian pfatal("Can't check %s\n", *argv); 14541474Sjulian else 14641474Sjulian (void)checkfilesys(path, 0, 0L, 0); 14741474Sjulian ++argv; 14841474Sjulian } 1491558Srgrimes exit(0); 1501558Srgrimes } 1511558Srgrimes ret = checkfstab(preen, maxrun, docheck, checkfilesys); 1521558Srgrimes if (returntosingle) 1531558Srgrimes exit(2); 1541558Srgrimes exit(ret); 1551558Srgrimes} 1561558Srgrimes 15723675Speterstatic int 1581558Srgrimesargtoi(flag, req, str, base) 1591558Srgrimes int flag; 1601558Srgrimes char *req, *str; 1611558Srgrimes int base; 1621558Srgrimes{ 1631558Srgrimes char *cp; 1641558Srgrimes int ret; 1651558Srgrimes 1661558Srgrimes ret = (int)strtol(str, &cp, base); 1671558Srgrimes if (cp == str || *cp) 16823675Speter errx(EEXIT, "-%c flag requires a %s", flag, req); 1691558Srgrimes return (ret); 1701558Srgrimes} 1711558Srgrimes 1721558Srgrimes/* 1731558Srgrimes * Determine whether a filesystem should be checked. 1741558Srgrimes */ 17523675Speterstatic int 1761558Srgrimesdocheck(fsp) 1771558Srgrimes register struct fstab *fsp; 1781558Srgrimes{ 1791558Srgrimes 1801558Srgrimes if (strcmp(fsp->fs_vfstype, "ufs") || 1811558Srgrimes (strcmp(fsp->fs_type, FSTAB_RW) && 1821558Srgrimes strcmp(fsp->fs_type, FSTAB_RO)) || 1831558Srgrimes fsp->fs_passno == 0) 1841558Srgrimes return (0); 1851558Srgrimes return (1); 1861558Srgrimes} 1871558Srgrimes 1881558Srgrimes/* 1891558Srgrimes * Check the specified filesystem. 1901558Srgrimes */ 1911558Srgrimes/* ARGSUSED */ 19223675Speterstatic int 1931558Srgrimescheckfilesys(filesys, mntpt, auxdata, child) 1941558Srgrimes char *filesys, *mntpt; 1951558Srgrimes long auxdata; 1967585Sbde int child; 1971558Srgrimes{ 19823675Speter ufs_daddr_t n_ffree, n_bfree; 1991558Srgrimes struct dups *dp; 2001558Srgrimes struct zlncnt *zlnp; 20123675Speter int cylno, flags; 2021558Srgrimes 2031558Srgrimes if (preen && child) 2041558Srgrimes (void)signal(SIGQUIT, voidquit); 2051558Srgrimes cdevname = filesys; 2061558Srgrimes if (debug && preen) 2071558Srgrimes pwarn("starting\n"); 20823675Speter switch (setup(filesys)) { 20923675Speter case 0: 2101558Srgrimes if (preen) 2111558Srgrimes pfatal("CAN'T CHECK FILE SYSTEM."); 21241474Sjulian /* fall through */ 21323675Speter case -1: 21431904Sbde return (0); 2152153Sdg } 2161558Srgrimes /* 21734266Sjulian * Cleared if any questions answered no. Used to decide if 21834266Sjulian * the superblock should be marked clean. 21934266Sjulian */ 22034266Sjulian resolved = 1; 22134266Sjulian /* 2221558Srgrimes * 1: scan inodes tallying blocks used 2231558Srgrimes */ 2241558Srgrimes if (preen == 0) { 2251558Srgrimes printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 2261558Srgrimes if (hotroot) 2271558Srgrimes printf("** Root file system\n"); 2281558Srgrimes printf("** Phase 1 - Check Blocks and Sizes\n"); 2291558Srgrimes } 2301558Srgrimes pass1(); 2311558Srgrimes 2321558Srgrimes /* 2331558Srgrimes * 1b: locate first references to duplicates, if any 2341558Srgrimes */ 2351558Srgrimes if (duplist) { 23634266Sjulian if (preen || usedsoftdep) 2371558Srgrimes pfatal("INTERNAL ERROR: dups with -p"); 2381558Srgrimes printf("** Phase 1b - Rescan For More DUPS\n"); 2391558Srgrimes pass1b(); 2401558Srgrimes } 2411558Srgrimes 2421558Srgrimes /* 2431558Srgrimes * 2: traverse directories from root to mark all connected directories 2441558Srgrimes */ 2451558Srgrimes if (preen == 0) 2461558Srgrimes printf("** Phase 2 - Check Pathnames\n"); 2471558Srgrimes pass2(); 2481558Srgrimes 2491558Srgrimes /* 2501558Srgrimes * 3: scan inodes looking for disconnected directories 2511558Srgrimes */ 2521558Srgrimes if (preen == 0) 2531558Srgrimes printf("** Phase 3 - Check Connectivity\n"); 2541558Srgrimes pass3(); 2551558Srgrimes 2561558Srgrimes /* 2571558Srgrimes * 4: scan inodes looking for disconnected files; check reference counts 2581558Srgrimes */ 2591558Srgrimes if (preen == 0) 2601558Srgrimes printf("** Phase 4 - Check Reference Counts\n"); 2611558Srgrimes pass4(); 2621558Srgrimes 2631558Srgrimes /* 2641558Srgrimes * 5: check and repair resource counts in cylinder groups 2651558Srgrimes */ 2661558Srgrimes if (preen == 0) 2671558Srgrimes printf("** Phase 5 - Check Cyl groups\n"); 2681558Srgrimes pass5(); 2691558Srgrimes 2701558Srgrimes /* 2711558Srgrimes * print out summary statistics 2721558Srgrimes */ 2731558Srgrimes n_ffree = sblock.fs_cstotal.cs_nffree; 2741558Srgrimes n_bfree = sblock.fs_cstotal.cs_nbfree; 2751558Srgrimes pwarn("%ld files, %ld used, %ld free ", 2761558Srgrimes n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree); 27731904Sbde printf("(%d frags, %d blocks, %.1f%% fragmentation)\n", 27831904Sbde n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize); 2791558Srgrimes if (debug && 2801558Srgrimes (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree)) 28131904Sbde printf("%d 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)) 28831904Sbde printf("%d blocks missing\n", n_blks); 2891558Srgrimes if (duplist != NULL) { 2901558Srgrimes printf("The following duplicate blocks remain:"); 2911558Srgrimes for (dp = duplist; dp; dp = dp->next) 29231904Sbde printf(" %d,", 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) 29831904Sbde printf(" %u,", 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) { 30741474Sjulian sblock.fs_time = time(NULL); 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 } 31834266Sjulian if (rerun) 31934266Sjulian resolved = 0; 32034266Sjulian flags = 0; 32134266Sjulian if (hotroot) { 32223675Speter struct statfs stfs_buf; 32323675Speter /* 32423675Speter * Check to see if root is mounted read-write. 32523675Speter */ 32623675Speter if (statfs("/", &stfs_buf) == 0) 32723675Speter flags = stfs_buf.f_flags; 32834266Sjulian if ((flags & MNT_RDONLY) == 0) 32934266Sjulian resolved = 0; 33023675Speter } 33134266Sjulian ckfini(resolved); 33241474Sjulian 33341474Sjulian for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 33441474Sjulian if (inostathead[cylno].il_stat != NULL) 33541474Sjulian free((char *)inostathead[cylno].il_stat); 33641474Sjulian free((char *)inostathead); 33741474Sjulian inostathead = NULL; 33841474Sjulian if (fsmodified && !preen) 3391558Srgrimes printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 34018808Sguido if (rerun) 34118808Sguido printf("\n***** PLEASE RERUN FSCK *****\n"); 3421558Srgrimes if (hotroot) { 34323675Speter struct ufs_args args; 34423675Speter int ret; 3451558Srgrimes /* 3461558Srgrimes * We modified the root. Do a mount update on 3471558Srgrimes * it, unless it is read-write, so we can continue. 3481558Srgrimes */ 34923675Speter if (flags & MNT_RDONLY) { 35023675Speter args.fspec = 0; 35123675Speter args.export.ex_flags = 0; 35223675Speter args.export.ex_root = 0; 35323675Speter flags |= MNT_UPDATE | MNT_RELOAD; 35423675Speter ret = mount("ufs", "/", flags, &args); 35523675Speter if (ret == 0) 35623675Speter return (0); 3571558Srgrimes } 35841474Sjulian if (!fsmodified) 35941474Sjulian return (0); 3601558Srgrimes if (!preen) 3611558Srgrimes printf("\n***** REBOOT NOW *****\n"); 3621558Srgrimes sync(); 3631558Srgrimes return (4); 3641558Srgrimes } 3651558Srgrimes return (0); 3661558Srgrimes} 367