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 34c69284caSDavid E. O'Brien #if 0 358fae3551SRodney W. Grimes #ifndef lint 3631f4ab50SBruce Evans static const char copyright[] = 378fae3551SRodney W. Grimes "@(#) Copyright (c) 1980, 1986, 1993\n\ 388fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 398fae3551SRodney W. Grimes #endif /* not lint */ 408fae3551SRodney W. Grimes 418fae3551SRodney W. Grimes #ifndef lint 426b100474SJulian Elischer static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95"; 438fae3551SRodney W. Grimes #endif /* not lint */ 44c69284caSDavid E. O'Brien #endif 458e2e167cSMark Murray #include <sys/cdefs.h> 468e2e167cSMark Murray __FBSDID("$FreeBSD$"); 478e2e167cSMark Murray 488fae3551SRodney W. Grimes #include <sys/param.h> 49571b29eeSPeter Wemm #include <sys/stat.h> 5038375c40SKirk McKusick #include <sys/file.h> 518fae3551SRodney W. Grimes #include <sys/time.h> 528fae3551SRodney W. Grimes #include <sys/mount.h> 5333966081SMatt Jacob #include <sys/resource.h> 54bf58d635SIan Dowse #include <sys/sysctl.h> 551c85e6a3SKirk McKusick #include <sys/disklabel.h> 56780a5c1eSPeter Wemm 578fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h> 58780a5c1eSPeter Wemm #include <ufs/ufs/ufsmount.h> 598fae3551SRodney W. Grimes #include <ufs/ffs/fs.h> 60780a5c1eSPeter Wemm 61780a5c1eSPeter Wemm #include <err.h> 62d0e1503bSPeter Wemm #include <errno.h> 638fae3551SRodney W. Grimes #include <fstab.h> 64ff76fc7fSKirk McKusick #include <grp.h> 65d0e1503bSPeter Wemm #include <paths.h> 6684fc0d7eSMaxime Henrion #include <stdint.h> 67bf58d635SIan Dowse #include <string.h> 68780a5c1eSPeter Wemm 698fae3551SRodney W. Grimes #include "fsck.h" 70780a5c1eSPeter Wemm 71b70cd7eeSWarner Losh static void usage(void) __dead2; 72599304a4SPoul-Henning Kamp static int argtoi(int flag, const char *req, const char *str, int base); 73b70cd7eeSWarner Losh static int checkfilesys(char *filesys); 74b70cd7eeSWarner Losh static struct statfs *getmntpt(const char *); 758fae3551SRodney W. Grimes 7631f4ab50SBruce Evans int 77b70cd7eeSWarner Losh main(int argc, char *argv[]) 788fae3551SRodney W. Grimes { 798fae3551SRodney W. Grimes int ch; 80d33e92f9SJulian Elischer struct rlimit rlimit; 811660ae87SScott Long struct itimerval itimerval; 829ea6f4f0SAdrian Chadd int ret = 0; 838fae3551SRodney W. Grimes 848fae3551SRodney W. Grimes sync(); 859ea6f4f0SAdrian Chadd skipclean = 1; 8615fca934SKirk McKusick while ((ch = getopt(argc, argv, "b:Bc:dfFm:npy")) != -1) { 878fae3551SRodney W. Grimes switch (ch) { 888fae3551SRodney W. Grimes case 'b': 899ea6f4f0SAdrian Chadd skipclean = 0; 908fae3551SRodney W. Grimes bflag = argtoi('b', "number", optarg, 10); 918fae3551SRodney W. Grimes printf("Alternate super block location: %d\n", bflag); 928fae3551SRodney W. Grimes break; 938fae3551SRodney W. Grimes 947578c6abSKirk McKusick case 'B': 957578c6abSKirk McKusick bkgrdflag = 1; 967578c6abSKirk McKusick break; 977578c6abSKirk McKusick 988fae3551SRodney W. Grimes case 'c': 999ea6f4f0SAdrian Chadd skipclean = 0; 1008fae3551SRodney W. Grimes cvtlevel = argtoi('c', "conversion level", optarg, 10); 1011c85e6a3SKirk McKusick if (cvtlevel < 3) 1021c85e6a3SKirk McKusick errx(EEXIT, "cannot do level %d conversion", 1031c85e6a3SKirk McKusick cvtlevel); 1048fae3551SRodney W. Grimes break; 1058fae3551SRodney W. Grimes 1068fae3551SRodney W. Grimes case 'd': 1078fae3551SRodney W. Grimes debug++; 1088fae3551SRodney W. Grimes break; 1098fae3551SRodney W. Grimes 11041cee58cSDavid Greenman case 'f': 1119ea6f4f0SAdrian Chadd skipclean = 0; 1128fae3551SRodney W. Grimes break; 1138fae3551SRodney W. Grimes 11415fca934SKirk McKusick case 'F': 11515fca934SKirk McKusick bkgrdcheck = 1; 11615fca934SKirk McKusick break; 11715fca934SKirk McKusick 1188fae3551SRodney W. Grimes case 'm': 1198fae3551SRodney W. Grimes lfmode = argtoi('m', "mode", optarg, 8); 1208fae3551SRodney W. Grimes if (lfmode &~ 07777) 121780a5c1eSPeter Wemm errx(EEXIT, "bad mode to -m: %o", lfmode); 1228fae3551SRodney W. Grimes printf("** lost+found creation mode %o\n", lfmode); 1238fae3551SRodney W. Grimes break; 1248fae3551SRodney W. Grimes 1258fae3551SRodney W. Grimes case 'n': 1268fae3551SRodney W. Grimes nflag++; 1278fae3551SRodney W. Grimes yflag = 0; 1288fae3551SRodney W. Grimes break; 1298fae3551SRodney W. Grimes 1309ea6f4f0SAdrian Chadd case 'p': 1319ea6f4f0SAdrian Chadd preen++; 1329ea6f4f0SAdrian Chadd break; 1339ea6f4f0SAdrian Chadd 1348fae3551SRodney W. Grimes case 'y': 1358fae3551SRodney W. Grimes yflag++; 1368fae3551SRodney W. Grimes nflag = 0; 1378fae3551SRodney W. Grimes break; 1388fae3551SRodney W. Grimes 1398fae3551SRodney W. Grimes default: 1409ea6f4f0SAdrian Chadd usage(); 1418fae3551SRodney W. Grimes } 1428fae3551SRodney W. Grimes } 1438fae3551SRodney W. Grimes argc -= optind; 1448fae3551SRodney W. Grimes argv += optind; 1459ea6f4f0SAdrian Chadd 1469ea6f4f0SAdrian Chadd if (!argc) 1479ea6f4f0SAdrian Chadd usage(); 1489ea6f4f0SAdrian Chadd 1498fae3551SRodney W. Grimes if (signal(SIGINT, SIG_IGN) != SIG_IGN) 1508fae3551SRodney W. Grimes (void)signal(SIGINT, catch); 1518fae3551SRodney W. Grimes if (preen) 1528fae3551SRodney W. Grimes (void)signal(SIGQUIT, catchquit); 1536db798caSIan Dowse signal(SIGINFO, infohandler); 1541660ae87SScott Long if (bkgrdflag) { 1551660ae87SScott Long signal(SIGALRM, alarmhandler); 1561660ae87SScott Long itimerval.it_interval.tv_sec = 5; 1571660ae87SScott Long itimerval.it_interval.tv_usec = 0; 1581660ae87SScott Long itimerval.it_value.tv_sec = 5; 1591660ae87SScott Long itimerval.it_value.tv_usec = 0; 1601660ae87SScott Long setitimer(ITIMER_REAL, &itimerval, NULL); 1611660ae87SScott Long } 162d33e92f9SJulian Elischer /* 163d33e92f9SJulian Elischer * Push up our allowed memory limit so we can cope 164d33e92f9SJulian Elischer * with huge file systems. 165d33e92f9SJulian Elischer */ 166d33e92f9SJulian Elischer if (getrlimit(RLIMIT_DATA, &rlimit) == 0) { 167d33e92f9SJulian Elischer rlimit.rlim_cur = rlimit.rlim_max; 168d33e92f9SJulian Elischer (void)setrlimit(RLIMIT_DATA, &rlimit); 169d33e92f9SJulian Elischer } 1709ea6f4f0SAdrian Chadd while (argc-- > 0) 171bf58d635SIan Dowse (void)checkfilesys(*argv++); 172d33e92f9SJulian Elischer 1738fae3551SRodney W. Grimes if (returntosingle) 1749ea6f4f0SAdrian Chadd ret = 2; 1758fae3551SRodney W. Grimes exit(ret); 1768fae3551SRodney W. Grimes } 1778fae3551SRodney W. Grimes 178780a5c1eSPeter Wemm static int 179599304a4SPoul-Henning Kamp argtoi(int flag, const char *req, const char *str, int base) 1808fae3551SRodney W. Grimes { 1818fae3551SRodney W. Grimes char *cp; 1828fae3551SRodney W. Grimes int ret; 1838fae3551SRodney W. Grimes 1848fae3551SRodney W. Grimes ret = (int)strtol(str, &cp, base); 1858fae3551SRodney W. Grimes if (cp == str || *cp) 186780a5c1eSPeter Wemm errx(EEXIT, "-%c flag requires a %s", flag, req); 1878fae3551SRodney W. Grimes return (ret); 1888fae3551SRodney W. Grimes } 1898fae3551SRodney W. Grimes 1908fae3551SRodney W. Grimes /* 1918fae3551SRodney W. Grimes * Check the specified file system. 1928fae3551SRodney W. Grimes */ 1938fae3551SRodney W. Grimes /* ARGSUSED */ 194780a5c1eSPeter Wemm static int 195b70cd7eeSWarner Losh checkfilesys(char *filesys) 1968fae3551SRodney W. Grimes { 1971c85e6a3SKirk McKusick ufs2_daddr_t n_ffree, n_bfree; 1987578c6abSKirk McKusick struct ufs_args args; 1998fae3551SRodney W. Grimes struct dups *dp; 2007578c6abSKirk McKusick struct statfs *mntp; 201d0e1503bSPeter Wemm struct zlncnt *zlnp; 202ff76fc7fSKirk McKusick struct stat snapdir; 203ff76fc7fSKirk McKusick struct group *grp; 2041c85e6a3SKirk McKusick ufs2_daddr_t blks; 20584fc0d7eSMaxime Henrion int cylno, ret; 2061c85e6a3SKirk McKusick ino_t files; 20784fc0d7eSMaxime Henrion size_t size; 2088fae3551SRodney W. Grimes 2098fae3551SRodney W. Grimes cdevname = filesys; 2108fae3551SRodney W. Grimes if (debug && preen) 2118fae3551SRodney W. Grimes pwarn("starting\n"); 21215fca934SKirk McKusick /* 21315fca934SKirk McKusick * Make best effort to get the disk name. Check first to see 21415fca934SKirk McKusick * if it is listed among the mounted file systems. Failing that 21515fca934SKirk McKusick * check to see if it is listed in /etc/fstab. 21615fca934SKirk McKusick */ 21715fca934SKirk McKusick mntp = getmntpt(filesys); 21815fca934SKirk McKusick if (mntp != NULL) 21915fca934SKirk McKusick filesys = mntp->f_mntfromname; 22015fca934SKirk McKusick else 22115fca934SKirk McKusick filesys = blockcheck(filesys); 22215fca934SKirk McKusick /* 22315fca934SKirk McKusick * If -F flag specified, check to see whether a background check 22415fca934SKirk McKusick * is possible and needed. If possible and needed, exit with 22515fca934SKirk McKusick * status zero. Otherwise exit with status non-zero. A non-zero 22615fca934SKirk McKusick * exit status will cause a foreground check to be run. 22715fca934SKirk McKusick */ 22838375c40SKirk McKusick sblock_init(); 22915fca934SKirk McKusick if (bkgrdcheck) { 23015fca934SKirk McKusick if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0) 23115fca934SKirk McKusick exit(3); /* Cannot read superblock */ 23215fca934SKirk McKusick close(fsreadfd); 23315fca934SKirk McKusick if (sblock.fs_flags & FS_NEEDSFSCK) 23415fca934SKirk McKusick exit(4); /* Earlier background failed */ 23515fca934SKirk McKusick if ((sblock.fs_flags & FS_DOSOFTDEP) == 0) 23615fca934SKirk McKusick exit(5); /* Not running soft updates */ 23715fca934SKirk McKusick size = MIBSIZE; 23815fca934SKirk McKusick if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0) 23915fca934SKirk McKusick exit(6); /* Lacks kernel support */ 24015fca934SKirk McKusick if ((mntp == NULL && sblock.fs_clean == 1) || 24115fca934SKirk McKusick (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0)) 24215fca934SKirk McKusick exit(7); /* Filesystem clean, report it now */ 24315fca934SKirk McKusick exit(0); 24415fca934SKirk McKusick } 2457578c6abSKirk McKusick /* 2467578c6abSKirk McKusick * If we are to do a background check: 2477578c6abSKirk McKusick * Get the mount point information of the file system 2487578c6abSKirk McKusick * create snapshot file 2497578c6abSKirk McKusick * return created snapshot file 2507578c6abSKirk McKusick * if not found, clear bkgrdflag and proceed with normal fsck 2517578c6abSKirk McKusick */ 2527578c6abSKirk McKusick if (bkgrdflag) { 2537578c6abSKirk McKusick if (mntp == NULL) { 2547578c6abSKirk McKusick bkgrdflag = 0; 25538375c40SKirk McKusick pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n"); 2567578c6abSKirk McKusick } else if ((mntp->f_flags & MNT_SOFTDEP) == 0) { 2577578c6abSKirk McKusick bkgrdflag = 0; 25838375c40SKirk McKusick pfatal("NOT USING SOFT UPDATES, %s\n", 25938375c40SKirk McKusick "CANNOT RUN IN BACKGROUND"); 2607578c6abSKirk McKusick } else if ((mntp->f_flags & MNT_RDONLY) != 0) { 2617578c6abSKirk McKusick bkgrdflag = 0; 26238375c40SKirk McKusick pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n"); 26338375c40SKirk McKusick } else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) { 26438375c40SKirk McKusick if (readsb(0) != 0) { 26538375c40SKirk McKusick if (sblock.fs_flags & FS_NEEDSFSCK) { 26638375c40SKirk McKusick bkgrdflag = 0; 26738375c40SKirk McKusick pfatal("UNEXPECTED INCONSISTENCY, %s\n", 26838375c40SKirk McKusick "CANNOT RUN IN BACKGROUND\n"); 26938375c40SKirk McKusick } 27038375c40SKirk McKusick if ((sblock.fs_flags & FS_UNCLEAN) == 0 && 27138375c40SKirk McKusick skipclean && preen) { 27238375c40SKirk McKusick /* 27338375c40SKirk McKusick * file system is clean; 27438375c40SKirk McKusick * skip snapshot and report it clean 27538375c40SKirk McKusick */ 27638375c40SKirk McKusick pwarn("FILE SYSTEM CLEAN; %s\n", 27738375c40SKirk McKusick "SKIPPING CHECKS"); 27838375c40SKirk McKusick goto clean; 27938375c40SKirk McKusick } 28038375c40SKirk McKusick } 28138375c40SKirk McKusick close(fsreadfd); 28238375c40SKirk McKusick } 28338375c40SKirk McKusick if (bkgrdflag) { 284ff76fc7fSKirk McKusick snprintf(snapname, sizeof snapname, "%s/.snap", 2857578c6abSKirk McKusick mntp->f_mntonname); 286ff76fc7fSKirk McKusick if (stat(snapname, &snapdir) < 0) { 287ff76fc7fSKirk McKusick if (errno != ENOENT) { 288ff76fc7fSKirk McKusick bkgrdflag = 0; 289ff76fc7fSKirk McKusick pfatal("CANNOT FIND %s %s: %s, %s\n", 290ff76fc7fSKirk McKusick "SNAPSHOT DIRECTORY", 291ff76fc7fSKirk McKusick snapname, strerror(errno), 292ff76fc7fSKirk McKusick "CANNOT RUN IN BACKGROUND"); 293ff76fc7fSKirk McKusick } else if ((grp = getgrnam("operator")) == 0 || 294ff76fc7fSKirk McKusick mkdir(snapname, 0770) < 0 || 295ff76fc7fSKirk McKusick chown(snapname, -1, grp->gr_gid) < 0 || 296ff76fc7fSKirk McKusick chmod(snapname, 0770) < 0) { 297ff76fc7fSKirk McKusick bkgrdflag = 0; 298ff76fc7fSKirk McKusick pfatal("CANNOT CREATE %s %s: %s, %s\n", 299ff76fc7fSKirk McKusick "SNAPSHOT DIRECTORY", 300ff76fc7fSKirk McKusick snapname, strerror(errno), 301ff76fc7fSKirk McKusick "CANNOT RUN IN BACKGROUND"); 302ff76fc7fSKirk McKusick } 303ff76fc7fSKirk McKusick } else if (!S_ISDIR(snapdir.st_mode)) { 304ff76fc7fSKirk McKusick bkgrdflag = 0; 305ff76fc7fSKirk McKusick pfatal("%s IS NOT A DIRECTORY, %s\n", snapname, 306ff76fc7fSKirk McKusick "CANNOT RUN IN BACKGROUND"); 307ff76fc7fSKirk McKusick } 308ff76fc7fSKirk McKusick } 309ff76fc7fSKirk McKusick if (bkgrdflag) { 310ff76fc7fSKirk McKusick snprintf(snapname, sizeof snapname, 311ff76fc7fSKirk McKusick "%s/.snap/fsck_snapshot", mntp->f_mntonname); 3127578c6abSKirk McKusick args.fspec = snapname; 3137578c6abSKirk McKusick while (mount("ffs", mntp->f_mntonname, 3147578c6abSKirk McKusick mntp->f_flags | MNT_UPDATE | MNT_SNAPSHOT, 3157578c6abSKirk McKusick &args) < 0) { 3167578c6abSKirk McKusick if (errno == EEXIST && unlink(snapname) == 0) 3177578c6abSKirk McKusick continue; 3187578c6abSKirk McKusick bkgrdflag = 0; 31938375c40SKirk McKusick pfatal("CANNOT CREATE SNAPSHOT %s: %s\n", 3207578c6abSKirk McKusick snapname, strerror(errno)); 3217578c6abSKirk McKusick break; 3227578c6abSKirk McKusick } 3237578c6abSKirk McKusick if (bkgrdflag != 0) 3247578c6abSKirk McKusick filesys = snapname; 3257578c6abSKirk McKusick } 3267578c6abSKirk McKusick } 3277578c6abSKirk McKusick 328780a5c1eSPeter Wemm switch (setup(filesys)) { 329780a5c1eSPeter Wemm case 0: 3308fae3551SRodney W. Grimes if (preen) 3318fae3551SRodney W. Grimes pfatal("CAN'T CHECK FILE SYSTEM."); 3326b100474SJulian Elischer return (0); 333780a5c1eSPeter Wemm case -1: 33438375c40SKirk McKusick clean: 335bf58d635SIan Dowse pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree + 336bf58d635SIan Dowse sblock.fs_frag * sblock.fs_cstotal.cs_nbfree)); 337599304a4SPoul-Henning Kamp printf("(%lld frags, %lld blocks, %.1f%% fragmentation)\n", 338599304a4SPoul-Henning Kamp (long long)sblock.fs_cstotal.cs_nffree, 339599304a4SPoul-Henning Kamp (long long)sblock.fs_cstotal.cs_nbfree, 3406b100474SJulian Elischer sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize); 34141cee58cSDavid Greenman return (0); 34241cee58cSDavid Greenman } 3436b100474SJulian Elischer 3448fae3551SRodney W. Grimes /* 345b1897c19SJulian Elischer * Cleared if any questions answered no. Used to decide if 346b1897c19SJulian Elischer * the superblock should be marked clean. 347b1897c19SJulian Elischer */ 348b1897c19SJulian Elischer resolved = 1; 349b1897c19SJulian Elischer /* 3508fae3551SRodney W. Grimes * 1: scan inodes tallying blocks used 3518fae3551SRodney W. Grimes */ 3528fae3551SRodney W. Grimes if (preen == 0) { 3538fae3551SRodney W. Grimes printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 3547578c6abSKirk McKusick if (mntp != NULL && mntp->f_flags & MNT_ROOTFS) 3558fae3551SRodney W. Grimes printf("** Root file system\n"); 3568fae3551SRodney W. Grimes printf("** Phase 1 - Check Blocks and Sizes\n"); 3578fae3551SRodney W. Grimes } 3588fae3551SRodney W. Grimes pass1(); 3598fae3551SRodney W. Grimes 3608fae3551SRodney W. Grimes /* 3618fae3551SRodney W. Grimes * 1b: locate first references to duplicates, if any 3628fae3551SRodney W. Grimes */ 3638fae3551SRodney W. Grimes if (duplist) { 364b1897c19SJulian Elischer if (preen || usedsoftdep) 3658fae3551SRodney W. Grimes pfatal("INTERNAL ERROR: dups with -p"); 3668fae3551SRodney W. Grimes printf("** Phase 1b - Rescan For More DUPS\n"); 3678fae3551SRodney W. Grimes pass1b(); 3688fae3551SRodney W. Grimes } 3698fae3551SRodney W. Grimes 3708fae3551SRodney W. Grimes /* 3718fae3551SRodney W. Grimes * 2: traverse directories from root to mark all connected directories 3728fae3551SRodney W. Grimes */ 3738fae3551SRodney W. Grimes if (preen == 0) 3748fae3551SRodney W. Grimes printf("** Phase 2 - Check Pathnames\n"); 3758fae3551SRodney W. Grimes pass2(); 3768fae3551SRodney W. Grimes 3778fae3551SRodney W. Grimes /* 3788fae3551SRodney W. Grimes * 3: scan inodes looking for disconnected directories 3798fae3551SRodney W. Grimes */ 3808fae3551SRodney W. Grimes if (preen == 0) 3818fae3551SRodney W. Grimes printf("** Phase 3 - Check Connectivity\n"); 3828fae3551SRodney W. Grimes pass3(); 3838fae3551SRodney W. Grimes 3848fae3551SRodney W. Grimes /* 3858fae3551SRodney W. Grimes * 4: scan inodes looking for disconnected files; check reference counts 3868fae3551SRodney W. Grimes */ 3878fae3551SRodney W. Grimes if (preen == 0) 3888fae3551SRodney W. Grimes printf("** Phase 4 - Check Reference Counts\n"); 3898fae3551SRodney W. Grimes pass4(); 3908fae3551SRodney W. Grimes 3918fae3551SRodney W. Grimes /* 3928fae3551SRodney W. Grimes * 5: check and repair resource counts in cylinder groups 3938fae3551SRodney W. Grimes */ 3948fae3551SRodney W. Grimes if (preen == 0) 3958fae3551SRodney W. Grimes printf("** Phase 5 - Check Cyl groups\n"); 3968fae3551SRodney W. Grimes pass5(); 3978fae3551SRodney W. Grimes 3988fae3551SRodney W. Grimes /* 3998fae3551SRodney W. Grimes * print out summary statistics 4008fae3551SRodney W. Grimes */ 4018fae3551SRodney W. Grimes n_ffree = sblock.fs_cstotal.cs_nffree; 4028fae3551SRodney W. Grimes n_bfree = sblock.fs_cstotal.cs_nbfree; 4037578c6abSKirk McKusick files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files; 4047578c6abSKirk McKusick blks = n_blks + 4057578c6abSKirk McKusick sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 4067578c6abSKirk McKusick blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 4077578c6abSKirk McKusick blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 4087578c6abSKirk McKusick blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks; 4097578c6abSKirk McKusick if (bkgrdflag && (files > 0 || blks > 0)) { 4107578c6abSKirk McKusick countdirs = sblock.fs_cstotal.cs_ndir - countdirs; 411599304a4SPoul-Henning Kamp pwarn("Reclaimed: %ld directories, %ld files, %lld fragments\n", 412599304a4SPoul-Henning Kamp countdirs, (long)files - countdirs, (long long)blks); 4137578c6abSKirk McKusick } 41484fc0d7eSMaxime Henrion pwarn("%ld files, %jd used, %ju free ", 41584fc0d7eSMaxime Henrion (long)n_files, (intmax_t)n_blks, 41684fc0d7eSMaxime Henrion (uintmax_t)n_ffree + sblock.fs_frag * n_bfree); 41784fc0d7eSMaxime Henrion printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n", 41884fc0d7eSMaxime Henrion (uintmax_t)n_ffree, (uintmax_t)n_bfree, 41984fc0d7eSMaxime Henrion n_ffree * 100.0 / sblock.fs_dsize); 4208fae3551SRodney W. Grimes if (debug) { 4217578c6abSKirk McKusick if (files < 0) 4227578c6abSKirk McKusick printf("%d inodes missing\n", -files); 4237578c6abSKirk McKusick if (blks < 0) 424599304a4SPoul-Henning Kamp printf("%lld blocks missing\n", -(long long)blks); 4258fae3551SRodney W. Grimes if (duplist != NULL) { 4268fae3551SRodney W. Grimes printf("The following duplicate blocks remain:"); 4278fae3551SRodney W. Grimes for (dp = duplist; dp; dp = dp->next) 428599304a4SPoul-Henning Kamp printf(" %lld,", (long long)dp->dup); 4298fae3551SRodney W. Grimes printf("\n"); 4308fae3551SRodney W. Grimes } 4318fae3551SRodney W. Grimes if (zlnhead != NULL) { 4328fae3551SRodney W. Grimes printf("The following zero link count inodes remain:"); 4338fae3551SRodney W. Grimes for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) 434ccc3fadfSBruce Evans printf(" %u,", zlnp->zlncnt); 4358fae3551SRodney W. Grimes printf("\n"); 4368fae3551SRodney W. Grimes } 4378fae3551SRodney W. Grimes } 4388fae3551SRodney W. Grimes zlnhead = (struct zlncnt *)0; 4398fae3551SRodney W. Grimes duplist = (struct dups *)0; 4408fae3551SRodney W. Grimes muldup = (struct dups *)0; 4418fae3551SRodney W. Grimes inocleanup(); 442802cd8e6SDavid Greenman if (fsmodified) { 443d33e92f9SJulian Elischer sblock.fs_time = time(NULL); 4448fae3551SRodney W. Grimes sbdirty(); 4458fae3551SRodney W. Grimes } 4468fae3551SRodney W. Grimes if (cvtlevel && sblk.b_dirty) { 4478fae3551SRodney W. Grimes /* 4488fae3551SRodney W. Grimes * Write out the duplicate super blocks 4498fae3551SRodney W. Grimes */ 4508fae3551SRodney W. Grimes for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 4518fae3551SRodney W. Grimes bwrite(fswritefd, (char *)&sblock, 4521c85e6a3SKirk McKusick fsbtodb(&sblock, cgsblock(&sblock, cylno)), 4531c85e6a3SKirk McKusick SBLOCKSIZE); 4548fae3551SRodney W. Grimes } 455b1897c19SJulian Elischer if (rerun) 456b1897c19SJulian Elischer resolved = 0; 457571b29eeSPeter Wemm 458d0e1503bSPeter Wemm /* 459aa5344b7SPeter Wemm * Check to see if the file system is mounted read-write. 460d0e1503bSPeter Wemm */ 4617578c6abSKirk McKusick if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0) 462b1897c19SJulian Elischer resolved = 0; 463b1897c19SJulian Elischer ckfini(resolved); 464d33e92f9SJulian Elischer 465d33e92f9SJulian Elischer for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 466d33e92f9SJulian Elischer if (inostathead[cylno].il_stat != NULL) 467d33e92f9SJulian Elischer free((char *)inostathead[cylno].il_stat); 468d33e92f9SJulian Elischer free((char *)inostathead); 469d33e92f9SJulian Elischer inostathead = NULL; 470d33e92f9SJulian Elischer if (fsmodified && !preen) 4718fae3551SRodney W. Grimes printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 47247ceb636SGuido van Rooij if (rerun) 47347ceb636SGuido van Rooij printf("\n***** PLEASE RERUN FSCK *****\n"); 4747578c6abSKirk McKusick if (mntp != NULL) { 475d0e1503bSPeter Wemm /* 476d0e1503bSPeter Wemm * We modified a mounted file system. Do a mount update on 477d0e1503bSPeter Wemm * it unless it is read-write, so we can continue using it 478d0e1503bSPeter Wemm * as safely as possible. 479d0e1503bSPeter Wemm */ 4807578c6abSKirk McKusick if (mntp->f_flags & MNT_RDONLY) { 4818fae3551SRodney W. Grimes args.fspec = 0; 4828fae3551SRodney W. Grimes args.export.ex_flags = 0; 4838fae3551SRodney W. Grimes args.export.ex_root = 0; 4847578c6abSKirk McKusick ret = mount("ufs", mntp->f_mntonname, 4857578c6abSKirk McKusick mntp->f_flags | MNT_UPDATE | MNT_RELOAD, &args); 4868fae3551SRodney W. Grimes if (ret == 0) 487d0e1503bSPeter Wemm return (0); 488d0e1503bSPeter Wemm pwarn("mount reload of '%s' failed: %s\n\n", 4897578c6abSKirk McKusick mntp->f_mntonname, strerror(errno)); 490d0e1503bSPeter Wemm } 491d33e92f9SJulian Elischer if (!fsmodified) 492d33e92f9SJulian Elischer return (0); 4938fae3551SRodney W. Grimes if (!preen) 4948fae3551SRodney W. Grimes printf("\n***** REBOOT NOW *****\n"); 4958fae3551SRodney W. Grimes sync(); 4968fae3551SRodney W. Grimes return (4); 4978fae3551SRodney W. Grimes } 4988fae3551SRodney W. Grimes return (0); 4998fae3551SRodney W. Grimes } 500571b29eeSPeter Wemm 501571b29eeSPeter Wemm /* 50215fca934SKirk McKusick * Get the mount point information for name. 503571b29eeSPeter Wemm */ 504571b29eeSPeter Wemm static struct statfs * 505b70cd7eeSWarner Losh getmntpt(const char *name) 506571b29eeSPeter Wemm { 507571b29eeSPeter Wemm struct stat devstat, mntdevstat; 508d0e1503bSPeter Wemm char device[sizeof(_PATH_DEV) - 1 + MNAMELEN]; 509599304a4SPoul-Henning Kamp char *ddevname; 51015fca934SKirk McKusick struct statfs *mntbuf, *statfsp; 51115fca934SKirk McKusick int i, mntsize, isdev; 512571b29eeSPeter Wemm 51315fca934SKirk McKusick if (stat(name, &devstat) != 0) 514571b29eeSPeter Wemm return (NULL); 51515fca934SKirk McKusick if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)) 51615fca934SKirk McKusick isdev = 1; 51715fca934SKirk McKusick else 51815fca934SKirk McKusick isdev = 0; 519571b29eeSPeter Wemm mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 520571b29eeSPeter Wemm for (i = 0; i < mntsize; i++) { 52115fca934SKirk McKusick statfsp = &mntbuf[i]; 522599304a4SPoul-Henning Kamp ddevname = statfsp->f_mntfromname; 523599304a4SPoul-Henning Kamp if (*ddevname != '/') { 524d0e1503bSPeter Wemm strcpy(device, _PATH_DEV); 525599304a4SPoul-Henning Kamp strcat(device, ddevname); 52615fca934SKirk McKusick strcpy(statfsp->f_mntfromname, device); 52715fca934SKirk McKusick } 52815fca934SKirk McKusick if (isdev == 0) { 52915fca934SKirk McKusick if (strcmp(name, statfsp->f_mntonname)) 53015fca934SKirk McKusick continue; 53115fca934SKirk McKusick return (statfsp); 532d0e1503bSPeter Wemm } 533599304a4SPoul-Henning Kamp if (stat(ddevname, &mntdevstat) == 0 && 534d0e1503bSPeter Wemm mntdevstat.st_rdev == devstat.st_rdev) 53515fca934SKirk McKusick return (statfsp); 536571b29eeSPeter Wemm } 53715fca934SKirk McKusick statfsp = NULL; 53815fca934SKirk McKusick return (statfsp); 539571b29eeSPeter Wemm } 5409ea6f4f0SAdrian Chadd 5419ea6f4f0SAdrian Chadd static void 542b70cd7eeSWarner Losh usage(void) 5439ea6f4f0SAdrian Chadd { 5449ea6f4f0SAdrian Chadd (void) fprintf(stderr, 545d3974088SDag-Erling Smørgrav "usage: %s [-BFpfny] [-b block] [-c level] [-m mode] " 5469ea6f4f0SAdrian Chadd "file system ...\n", 5478e2e167cSMark Murray getprogname()); 5489ea6f4f0SAdrian Chadd exit(1); 5499ea6f4f0SAdrian Chadd } 550