18fae3551SRodney W. Grimes /* 28fae3551SRodney W. Grimes * Copyright (c) 1980, 1986, 1993 38fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 48fae3551SRodney W. Grimes * 58fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 68fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 78fae3551SRodney W. Grimes * are met: 88fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 98fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 108fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 118fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 128fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 138fae3551SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 148fae3551SRodney W. Grimes * must display the following acknowledgement: 158fae3551SRodney W. Grimes * This product includes software developed by the University of 168fae3551SRodney W. Grimes * California, Berkeley and its contributors. 178fae3551SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 188fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 198fae3551SRodney W. Grimes * without specific prior written permission. 208fae3551SRodney W. Grimes * 218fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 228fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 238fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 248fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 258fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 268fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 278fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 288fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 298fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 308fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 318fae3551SRodney W. Grimes * SUCH DAMAGE. 328fae3551SRodney W. Grimes */ 338fae3551SRodney W. Grimes 348fae3551SRodney W. Grimes #ifndef lint 3531f4ab50SBruce Evans static const char copyright[] = 368fae3551SRodney W. Grimes "@(#) Copyright (c) 1980, 1986, 1993\n\ 378fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 388fae3551SRodney W. Grimes #endif /* not lint */ 398fae3551SRodney W. Grimes 408fae3551SRodney W. Grimes #ifndef lint 41ccc3fadfSBruce Evans #if 0 42ccc3fadfSBruce Evans static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95"; 43ccc3fadfSBruce Evans #endif 44ccc3fadfSBruce Evans static const char rcsid[] = 4533966081SMatt Jacob "$Id: main.c,v 1.14 1998/06/15 07:07:16 charnier Exp $"; 468fae3551SRodney W. Grimes #endif /* not lint */ 478fae3551SRodney W. Grimes 488fae3551SRodney W. Grimes #include <sys/param.h> 498fae3551SRodney W. Grimes #include <sys/time.h> 508fae3551SRodney W. Grimes #include <sys/mount.h> 5133966081SMatt Jacob #include <sys/resource.h> 52780a5c1eSPeter Wemm 538fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h> 54780a5c1eSPeter Wemm #include <ufs/ufs/ufsmount.h> 558fae3551SRodney W. Grimes #include <ufs/ffs/fs.h> 56780a5c1eSPeter Wemm 57780a5c1eSPeter Wemm #include <err.h> 588fae3551SRodney W. Grimes #include <fstab.h> 59780a5c1eSPeter Wemm 608fae3551SRodney W. Grimes #include "fsck.h" 61780a5c1eSPeter Wemm 62780a5c1eSPeter Wemm int returntosingle; 63780a5c1eSPeter Wemm 6431f4ab50SBruce Evans static int argtoi __P((int flag, char *req, char *str, int base)); 6531f4ab50SBruce Evans static int docheck __P((struct fstab *fsp)); 6631f4ab50SBruce Evans static int checkfilesys __P((char *filesys, char *mntpt, long auxdata, 6731f4ab50SBruce Evans int child)); 68780a5c1eSPeter Wemm int main __P((int argc, char *argv[])); 698fae3551SRodney W. Grimes 7031f4ab50SBruce Evans int 718fae3551SRodney W. Grimes main(argc, argv) 728fae3551SRodney W. Grimes int argc; 738fae3551SRodney W. Grimes char *argv[]; 748fae3551SRodney W. Grimes { 758fae3551SRodney W. Grimes int ch; 768fae3551SRodney W. Grimes int ret, maxrun = 0; 7733966081SMatt Jacob struct rlimit rlim; 788fae3551SRodney W. Grimes 7933966081SMatt Jacob if (getrlimit(RLIMIT_DATA, &rlim) == 0) { 8033966081SMatt Jacob rlim.rlim_cur = rlim.rlim_max; 8133966081SMatt Jacob (void) setrlimit(RLIMIT_DATA, &rlim); 8233966081SMatt Jacob } 838fae3551SRodney W. Grimes sync(); 84473e3a9aSBruce Evans while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != -1) { 858fae3551SRodney W. Grimes switch (ch) { 868fae3551SRodney W. Grimes case 'p': 878fae3551SRodney W. Grimes preen++; 888fae3551SRodney W. Grimes break; 898fae3551SRodney W. Grimes 908fae3551SRodney W. Grimes case 'b': 918fae3551SRodney W. Grimes bflag = argtoi('b', "number", optarg, 10); 928fae3551SRodney W. Grimes printf("Alternate super block location: %d\n", bflag); 938fae3551SRodney W. Grimes break; 948fae3551SRodney W. Grimes 958fae3551SRodney W. Grimes case 'c': 968fae3551SRodney W. Grimes cvtlevel = argtoi('c', "conversion level", optarg, 10); 978fae3551SRodney W. Grimes break; 988fae3551SRodney W. Grimes 998fae3551SRodney W. Grimes case 'd': 1008fae3551SRodney W. Grimes debug++; 1018fae3551SRodney W. Grimes break; 1028fae3551SRodney W. Grimes 10341cee58cSDavid Greenman case 'f': 10441cee58cSDavid Greenman fflag++; 10541cee58cSDavid Greenman break; 10641cee58cSDavid Greenman 1078fae3551SRodney W. Grimes case 'l': 1088fae3551SRodney W. Grimes maxrun = argtoi('l', "number", optarg, 10); 1098fae3551SRodney W. Grimes break; 1108fae3551SRodney W. Grimes 1118fae3551SRodney W. Grimes case 'm': 1128fae3551SRodney W. Grimes lfmode = argtoi('m', "mode", optarg, 8); 1138fae3551SRodney W. Grimes if (lfmode &~ 07777) 114780a5c1eSPeter Wemm errx(EEXIT, "bad mode to -m: %o", lfmode); 1158fae3551SRodney W. Grimes printf("** lost+found creation mode %o\n", lfmode); 1168fae3551SRodney W. Grimes break; 1178fae3551SRodney W. Grimes 1188fae3551SRodney W. Grimes case 'n': 1198fae3551SRodney W. Grimes case 'N': 1208fae3551SRodney W. Grimes nflag++; 1218fae3551SRodney W. Grimes yflag = 0; 1228fae3551SRodney W. Grimes break; 1238fae3551SRodney W. Grimes 1248fae3551SRodney W. Grimes case 'y': 1258fae3551SRodney W. Grimes case 'Y': 1268fae3551SRodney W. Grimes yflag++; 1278fae3551SRodney W. Grimes nflag = 0; 1288fae3551SRodney W. Grimes break; 1298fae3551SRodney W. Grimes 1308fae3551SRodney W. Grimes default: 131780a5c1eSPeter Wemm errx(EEXIT, "%c option?", ch); 1328fae3551SRodney W. Grimes } 1338fae3551SRodney W. Grimes } 1348fae3551SRodney W. Grimes argc -= optind; 1358fae3551SRodney W. Grimes argv += optind; 1368fae3551SRodney W. Grimes if (signal(SIGINT, SIG_IGN) != SIG_IGN) 1378fae3551SRodney W. Grimes (void)signal(SIGINT, catch); 1388fae3551SRodney W. Grimes if (preen) 1398fae3551SRodney W. Grimes (void)signal(SIGQUIT, catchquit); 1408fae3551SRodney W. Grimes if (argc) { 1418fae3551SRodney W. Grimes while (argc-- > 0) 1428fae3551SRodney W. Grimes (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0); 1438fae3551SRodney W. Grimes exit(0); 1448fae3551SRodney W. Grimes } 1458fae3551SRodney W. Grimes ret = checkfstab(preen, maxrun, docheck, checkfilesys); 1468fae3551SRodney W. Grimes if (returntosingle) 1478fae3551SRodney W. Grimes exit(2); 1488fae3551SRodney W. Grimes exit(ret); 1498fae3551SRodney W. Grimes } 1508fae3551SRodney W. Grimes 151780a5c1eSPeter Wemm static int 1528fae3551SRodney W. Grimes argtoi(flag, req, str, base) 1538fae3551SRodney W. Grimes int flag; 1548fae3551SRodney W. Grimes char *req, *str; 1558fae3551SRodney W. Grimes int base; 1568fae3551SRodney W. Grimes { 1578fae3551SRodney W. Grimes char *cp; 1588fae3551SRodney W. Grimes int ret; 1598fae3551SRodney W. Grimes 1608fae3551SRodney W. Grimes ret = (int)strtol(str, &cp, base); 1618fae3551SRodney W. Grimes if (cp == str || *cp) 162780a5c1eSPeter Wemm errx(EEXIT, "-%c flag requires a %s", flag, req); 1638fae3551SRodney W. Grimes return (ret); 1648fae3551SRodney W. Grimes } 1658fae3551SRodney W. Grimes 1668fae3551SRodney W. Grimes /* 1678fae3551SRodney W. Grimes * Determine whether a filesystem should be checked. 1688fae3551SRodney W. Grimes */ 169780a5c1eSPeter Wemm static int 1708fae3551SRodney W. Grimes docheck(fsp) 1718fae3551SRodney W. Grimes register struct fstab *fsp; 1728fae3551SRodney W. Grimes { 1738fae3551SRodney W. Grimes 1748fae3551SRodney W. Grimes if (strcmp(fsp->fs_vfstype, "ufs") || 1758fae3551SRodney W. Grimes (strcmp(fsp->fs_type, FSTAB_RW) && 1768fae3551SRodney W. Grimes strcmp(fsp->fs_type, FSTAB_RO)) || 1778fae3551SRodney W. Grimes fsp->fs_passno == 0) 1788fae3551SRodney W. Grimes return (0); 1798fae3551SRodney W. Grimes return (1); 1808fae3551SRodney W. Grimes } 1818fae3551SRodney W. Grimes 1828fae3551SRodney W. Grimes /* 1838fae3551SRodney W. Grimes * Check the specified filesystem. 1848fae3551SRodney W. Grimes */ 1858fae3551SRodney W. Grimes /* ARGSUSED */ 186780a5c1eSPeter Wemm static int 1878fae3551SRodney W. Grimes checkfilesys(filesys, mntpt, auxdata, child) 1888fae3551SRodney W. Grimes char *filesys, *mntpt; 1898fae3551SRodney W. Grimes long auxdata; 19031f4ab50SBruce Evans int child; 1918fae3551SRodney W. Grimes { 192780a5c1eSPeter Wemm ufs_daddr_t n_ffree, n_bfree; 1938fae3551SRodney W. Grimes struct dups *dp; 1948fae3551SRodney W. Grimes struct zlncnt *zlnp; 195780a5c1eSPeter Wemm int cylno, flags; 1968fae3551SRodney W. Grimes 1978fae3551SRodney W. Grimes if (preen && child) 1988fae3551SRodney W. Grimes (void)signal(SIGQUIT, voidquit); 1998fae3551SRodney W. Grimes cdevname = filesys; 2008fae3551SRodney W. Grimes if (debug && preen) 2018fae3551SRodney W. Grimes pwarn("starting\n"); 202780a5c1eSPeter Wemm switch (setup(filesys)) { 203780a5c1eSPeter Wemm case 0: 2048fae3551SRodney W. Grimes if (preen) 2058fae3551SRodney W. Grimes pfatal("CAN'T CHECK FILE SYSTEM."); 2062d187af5SBruce Evans return (0); 207780a5c1eSPeter Wemm case -1: 20841cee58cSDavid Greenman pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree + 20941cee58cSDavid Greenman sblock.fs_frag * sblock.fs_cstotal.cs_nbfree); 210ccc3fadfSBruce Evans printf("(%d frags, %d blocks, %.1f%% fragmentation)\n", 211ccc3fadfSBruce Evans sblock.fs_cstotal.cs_nffree, sblock.fs_cstotal.cs_nbfree, 212ccc3fadfSBruce Evans sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize); 21341cee58cSDavid Greenman return (0); 21441cee58cSDavid Greenman } 21541cee58cSDavid Greenman 2168fae3551SRodney W. Grimes /* 217b1897c19SJulian Elischer * Cleared if any questions answered no. Used to decide if 218b1897c19SJulian Elischer * the superblock should be marked clean. 219b1897c19SJulian Elischer */ 220b1897c19SJulian Elischer resolved = 1; 221b1897c19SJulian Elischer /* 2228fae3551SRodney W. Grimes * 1: scan inodes tallying blocks used 2238fae3551SRodney W. Grimes */ 2248fae3551SRodney W. Grimes if (preen == 0) { 2258fae3551SRodney W. Grimes printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 2268fae3551SRodney W. Grimes if (hotroot) 2278fae3551SRodney W. Grimes printf("** Root file system\n"); 2288fae3551SRodney W. Grimes printf("** Phase 1 - Check Blocks and Sizes\n"); 2298fae3551SRodney W. Grimes } 2308fae3551SRodney W. Grimes pass1(); 2318fae3551SRodney W. Grimes 2328fae3551SRodney W. Grimes /* 2338fae3551SRodney W. Grimes * 1b: locate first references to duplicates, if any 2348fae3551SRodney W. Grimes */ 2358fae3551SRodney W. Grimes if (duplist) { 236b1897c19SJulian Elischer if (preen || usedsoftdep) 2378fae3551SRodney W. Grimes pfatal("INTERNAL ERROR: dups with -p"); 2388fae3551SRodney W. Grimes printf("** Phase 1b - Rescan For More DUPS\n"); 2398fae3551SRodney W. Grimes pass1b(); 2408fae3551SRodney W. Grimes } 2418fae3551SRodney W. Grimes 2428fae3551SRodney W. Grimes /* 2438fae3551SRodney W. Grimes * 2: traverse directories from root to mark all connected directories 2448fae3551SRodney W. Grimes */ 2458fae3551SRodney W. Grimes if (preen == 0) 2468fae3551SRodney W. Grimes printf("** Phase 2 - Check Pathnames\n"); 2478fae3551SRodney W. Grimes pass2(); 2488fae3551SRodney W. Grimes 2498fae3551SRodney W. Grimes /* 2508fae3551SRodney W. Grimes * 3: scan inodes looking for disconnected directories 2518fae3551SRodney W. Grimes */ 2528fae3551SRodney W. Grimes if (preen == 0) 2538fae3551SRodney W. Grimes printf("** Phase 3 - Check Connectivity\n"); 2548fae3551SRodney W. Grimes pass3(); 2558fae3551SRodney W. Grimes 2568fae3551SRodney W. Grimes /* 2578fae3551SRodney W. Grimes * 4: scan inodes looking for disconnected files; check reference counts 2588fae3551SRodney W. Grimes */ 2598fae3551SRodney W. Grimes if (preen == 0) 2608fae3551SRodney W. Grimes printf("** Phase 4 - Check Reference Counts\n"); 2618fae3551SRodney W. Grimes pass4(); 2628fae3551SRodney W. Grimes 2638fae3551SRodney W. Grimes /* 2648fae3551SRodney W. Grimes * 5: check and repair resource counts in cylinder groups 2658fae3551SRodney W. Grimes */ 2668fae3551SRodney W. Grimes if (preen == 0) 2678fae3551SRodney W. Grimes printf("** Phase 5 - Check Cyl groups\n"); 2688fae3551SRodney W. Grimes pass5(); 2698fae3551SRodney W. Grimes 2708fae3551SRodney W. Grimes /* 2718fae3551SRodney W. Grimes * print out summary statistics 2728fae3551SRodney W. Grimes */ 2738fae3551SRodney W. Grimes n_ffree = sblock.fs_cstotal.cs_nffree; 2748fae3551SRodney W. Grimes n_bfree = sblock.fs_cstotal.cs_nbfree; 2758fae3551SRodney W. Grimes pwarn("%ld files, %ld used, %ld free ", 2768fae3551SRodney W. Grimes n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree); 277ccc3fadfSBruce Evans printf("(%d frags, %d blocks, %.1f%% fragmentation)\n", 278ccc3fadfSBruce Evans n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize); 2798fae3551SRodney W. Grimes if (debug && 2808fae3551SRodney W. Grimes (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree)) 281ccc3fadfSBruce Evans printf("%d files missing\n", n_files); 2828fae3551SRodney W. Grimes if (debug) { 2838fae3551SRodney W. Grimes n_blks += sblock.fs_ncg * 2848fae3551SRodney W. Grimes (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 2858fae3551SRodney W. Grimes n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 2868fae3551SRodney W. Grimes n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 2878fae3551SRodney W. Grimes if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree)) 288ccc3fadfSBruce Evans printf("%d blocks missing\n", n_blks); 2898fae3551SRodney W. Grimes if (duplist != NULL) { 2908fae3551SRodney W. Grimes printf("The following duplicate blocks remain:"); 2918fae3551SRodney W. Grimes for (dp = duplist; dp; dp = dp->next) 292ccc3fadfSBruce Evans printf(" %d,", dp->dup); 2938fae3551SRodney W. Grimes printf("\n"); 2948fae3551SRodney W. Grimes } 2958fae3551SRodney W. Grimes if (zlnhead != NULL) { 2968fae3551SRodney W. Grimes printf("The following zero link count inodes remain:"); 2978fae3551SRodney W. Grimes for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 298ccc3fadfSBruce Evans printf(" %u,", zlnp->zlncnt); 2998fae3551SRodney W. Grimes printf("\n"); 3008fae3551SRodney W. Grimes } 3018fae3551SRodney W. Grimes } 3028fae3551SRodney W. Grimes zlnhead = (struct zlncnt *)0; 3038fae3551SRodney W. Grimes duplist = (struct dups *)0; 3048fae3551SRodney W. Grimes muldup = (struct dups *)0; 3058fae3551SRodney W. Grimes inocleanup(); 306802cd8e6SDavid Greenman if (fsmodified) { 3078fae3551SRodney W. Grimes (void)time(&sblock.fs_time); 3088fae3551SRodney W. Grimes sbdirty(); 3098fae3551SRodney W. Grimes } 3108fae3551SRodney W. Grimes if (cvtlevel && sblk.b_dirty) { 3118fae3551SRodney W. Grimes /* 3128fae3551SRodney W. Grimes * Write out the duplicate super blocks 3138fae3551SRodney W. Grimes */ 3148fae3551SRodney W. Grimes for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 3158fae3551SRodney W. Grimes bwrite(fswritefd, (char *)&sblock, 3168fae3551SRodney W. Grimes fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE); 3178fae3551SRodney W. Grimes } 318b1897c19SJulian Elischer if (rerun) 319b1897c19SJulian Elischer resolved = 0; 320b1897c19SJulian Elischer flags = 0; 321b1897c19SJulian Elischer if (hotroot) { 322780a5c1eSPeter Wemm struct statfs stfs_buf; 323780a5c1eSPeter Wemm /* 324780a5c1eSPeter Wemm * Check to see if root is mounted read-write. 325780a5c1eSPeter Wemm */ 326780a5c1eSPeter Wemm if (statfs("/", &stfs_buf) == 0) 327780a5c1eSPeter Wemm flags = stfs_buf.f_flags; 328b1897c19SJulian Elischer if ((flags & MNT_RDONLY) == 0) 329b1897c19SJulian Elischer resolved = 0; 330780a5c1eSPeter Wemm } 331b1897c19SJulian Elischer ckfini(resolved); 3328fae3551SRodney W. Grimes free(blockmap); 3338fae3551SRodney W. Grimes free(statemap); 3348fae3551SRodney W. Grimes free((char *)lncntp); 3358fae3551SRodney W. Grimes if (!fsmodified) 3368fae3551SRodney W. Grimes return (0); 3378fae3551SRodney W. Grimes if (!preen) 3388fae3551SRodney W. Grimes printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 33947ceb636SGuido van Rooij if (rerun) 34047ceb636SGuido van Rooij printf("\n***** PLEASE RERUN FSCK *****\n"); 3418fae3551SRodney W. Grimes if (hotroot) { 342780a5c1eSPeter Wemm struct ufs_args args; 343780a5c1eSPeter Wemm int ret; 3448fae3551SRodney W. Grimes /* 3458fae3551SRodney W. Grimes * We modified the root. Do a mount update on 3468fae3551SRodney W. Grimes * it, unless it is read-write, so we can continue. 3478fae3551SRodney W. Grimes */ 3488fae3551SRodney W. Grimes if (flags & MNT_RDONLY) { 3498fae3551SRodney W. Grimes args.fspec = 0; 3508fae3551SRodney W. Grimes args.export.ex_flags = 0; 3518fae3551SRodney W. Grimes args.export.ex_root = 0; 3528fae3551SRodney W. Grimes flags |= MNT_UPDATE | MNT_RELOAD; 353780a5c1eSPeter Wemm ret = mount("ufs", "/", flags, &args); 3548fae3551SRodney W. Grimes if (ret == 0) 3558fae3551SRodney W. Grimes return (0); 3568fae3551SRodney W. Grimes } 3578fae3551SRodney W. Grimes if (!preen) 3588fae3551SRodney W. Grimes printf("\n***** REBOOT NOW *****\n"); 3598fae3551SRodney W. Grimes sync(); 3608fae3551SRodney W. Grimes return (4); 3618fae3551SRodney W. Grimes } 3628fae3551SRodney W. Grimes return (0); 3638fae3551SRodney W. Grimes } 364