2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1986, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #define IN_RTLD /* So we pickup the P_OSREL defines */
47 #include <sys/param.h>
49 #include <sys/mount.h>
50 #include <sys/resource.h>
52 #include <sys/sysctl.h>
54 #include <sys/disklabel.h>
56 #include <ufs/ufs/dinode.h>
57 #include <ufs/ffs/fs.h>
75 static void usage(void) __dead2;
76 static intmax_t argtoimax(int flag, const char *req, const char *str, int base);
77 static int checkfilesys(char *filesys);
78 static int chkdoreload(struct statfs *mntp);
79 static struct statfs *getmntpt(const char *);
82 main(int argc, char *argv[])
86 struct itimerval itimerval;
93 while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:npRrSyZz")) != -1) {
97 bflag = argtoimax('b', "number", optarg, 10);
98 printf("Alternate super block location: %jd\n", bflag);
107 cvtlevel = argtoimax('c', "conversion level", optarg,
110 errx(EEXIT, "cannot do level %d conversion",
131 lfmode = argtoimax('m', "mode", optarg, 8);
133 errx(EEXIT, "bad mode to -m: %o", lfmode);
134 printf("** lost+found creation mode %o\n", lfmode);
184 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
185 (void)signal(SIGINT, catch);
187 (void)signal(SIGQUIT, catchquit);
188 signal(SIGINFO, infohandler);
190 signal(SIGALRM, alarmhandler);
191 itimerval.it_interval.tv_sec = 5;
192 itimerval.it_interval.tv_usec = 0;
193 itimerval.it_value.tv_sec = 5;
194 itimerval.it_value.tv_usec = 0;
195 setitimer(ITIMER_REAL, &itimerval, NULL);
198 * Push up our allowed memory limit so we can cope
199 * with huge file systems.
201 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
202 rlimit.rlim_cur = rlimit.rlim_max;
203 (void)setrlimit(RLIMIT_DATA, &rlimit);
206 if ((fsret = checkfilesys(*argv)) == ERESTART)
219 argtoimax(int flag, const char *req, const char *str, int base)
224 ret = strtoimax(str, &cp, base);
225 if (cp == str || *cp)
226 errx(EEXIT, "-%c flag requires a %s", flag, req);
231 * Check the specified file system.
235 checkfilesys(char *filesys)
237 ufs2_daddr_t n_ffree, n_bfree;
247 intmax_t blks, files;
257 if (debug && ckclean)
260 * Make best effort to get the disk name. Check first to see
261 * if it is listed among the mounted file systems. Failing that
262 * check to see if it is listed in /etc/fstab.
264 mntp = getmntpt(filesys);
266 filesys = mntp->f_mntfromname;
268 filesys = blockcheck(filesys);
270 * If -F flag specified, check to see whether a background check
271 * is possible and needed. If possible and needed, exit with
272 * status zero. Otherwise exit with status non-zero. A non-zero
273 * exit status will cause a foreground check to be run.
277 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
278 exit(3); /* Cannot read superblock */
280 /* Earlier background failed or journaled */
281 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
283 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
284 exit(5); /* Not running soft updates */
286 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
287 exit(6); /* Lacks kernel support */
288 if ((mntp == NULL && sblock.fs_clean == 1) ||
289 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
290 exit(7); /* Filesystem clean, report it now */
293 if (ckclean && skipclean) {
295 * If file system is gjournaled, check it here.
297 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
298 exit(3); /* Cannot read superblock */
300 if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
301 //printf("GJournaled file system detected on %s.\n",
303 if (sblock.fs_clean == 1) {
304 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
307 if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
308 gjournal_check(filesys);
309 if (chkdoreload(mntp) == 0)
314 "UNEXPECTED INCONSISTENCY, CANNOT RUN FAST FSCK\n");
319 * If we are to do a background check:
320 * Get the mount point information of the file system
321 * create snapshot file
322 * return created snapshot file
323 * if not found, clear bkgrdflag and proceed with normal fsck
328 pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
329 } else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
332 "NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
333 } else if ((mntp->f_flags & MNT_RDONLY) != 0) {
335 pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
336 } else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
337 if (readsb(0) != 0) {
338 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) {
341 "UNEXPECTED INCONSISTENCY, CANNOT RUN IN BACKGROUND\n");
343 if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
344 skipclean && ckclean) {
346 * file system is clean;
347 * skip snapshot and report it clean
350 "FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
357 snprintf(snapname, sizeof snapname, "%s/.snap",
359 if (stat(snapname, &snapdir) < 0) {
360 if (errno != ENOENT) {
363 "CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
364 snapname, strerror(errno));
365 } else if ((grp = getgrnam("operator")) == NULL ||
366 mkdir(snapname, 0770) < 0 ||
367 chown(snapname, -1, grp->gr_gid) < 0 ||
368 chmod(snapname, 0770) < 0) {
371 "CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
372 snapname, strerror(errno));
374 } else if (!S_ISDIR(snapdir.st_mode)) {
377 "%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
382 snprintf(snapname, sizeof snapname,
383 "%s/.snap/fsck_snapshot", mntp->f_mntonname);
384 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
385 build_iovec(&iov, &iovlen, "from", snapname,
387 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
389 build_iovec(&iov, &iovlen, "errmsg", errmsg,
391 build_iovec(&iov, &iovlen, "update", NULL, 0);
392 build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
394 while (nmount(iov, iovlen, mntp->f_flags) < 0) {
395 if (errno == EEXIST && unlink(snapname) == 0)
398 pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n",
399 snapname, strerror(errno), errmsg);
407 switch (setup(filesys)) {
410 pfatal("CAN'T CHECK FILE SYSTEM.");
414 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
415 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
416 printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
417 (intmax_t)sblock.fs_cstotal.cs_nffree,
418 (intmax_t)sblock.fs_cstotal.cs_nbfree,
419 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
423 * Determine if we can and should do journal recovery.
425 if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
426 if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
427 if (suj_check(filesys) == 0) {
428 printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
429 if (chkdoreload(mntp) == 0)
433 printf("** Skipping journal, falling through to full fsck\n\n");
436 * Write the superblock so we don't try to recover the
437 * journal on another pass. If this is the only change
438 * to the filesystem, we do not want it to be called
441 sblock.fs_mtime = time(NULL);
443 ofsmodified = fsmodified;
444 flush(fswritefd, &sblk);
445 fsmodified = ofsmodified;
448 * If the filesystem was run on an old kernel that did not
449 * support check hashes, clear the check-hash flags so that
450 * we do not try to verify them.
452 if ((sblock.fs_flags & FS_METACKHASH) == 0)
453 sblock.fs_metackhash = 0;
455 * If we are running on a kernel that can provide check hashes
456 * that are not yet enabled for the filesystem and we are
457 * running manually without the -y flag, offer to add any
458 * supported check hashes that are not already enabled.
461 if (preen == 0 && yflag == 0 && sblock.fs_magic != FS_UFS1_MAGIC &&
462 fswritefd != -1 && getosreldate() >= P_OSREL_CK_CYLGRP) {
463 if ((sblock.fs_metackhash & CK_CYLGRP) == 0 &&
464 reply("ADD CYLINDER GROUP CHECK-HASH PROTECTION") != 0) {
465 ckhashadd |= CK_CYLGRP;
466 sblock.fs_metackhash |= CK_CYLGRP;
468 if ((sblock.fs_metackhash & CK_SUPERBLOCK) == 0 &&
469 getosreldate() >= P_OSREL_CK_SUPERBLOCK &&
470 reply("ADD SUPERBLOCK CHECK-HASH PROTECTION") != 0) {
471 ckhashadd |= CK_SUPERBLOCK;
472 sblock.fs_metackhash |= CK_SUPERBLOCK;
474 if ((sblock.fs_metackhash & CK_INODE) == 0 &&
475 getosreldate() >= P_OSREL_CK_INODE &&
476 reply("ADD INODE CHECK-HASH PROTECTION") != 0) {
477 ckhashadd |= CK_INODE;
478 sblock.fs_metackhash |= CK_INODE;
481 if ((sblock.fs_metackhash & CK_INDIR) == 0 &&
482 getosreldate() >= P_OSREL_CK_INDIR &&
483 reply("ADD INDIRECT BLOCK CHECK-HASH PROTECTION") != 0) {
484 ckhashadd |= CK_INDIR;
485 sblock.fs_metackhash |= CK_INDIR;
487 if ((sblock.fs_metackhash & CK_DIR) == 0 &&
488 getosreldate() >= P_OSREL_CK_DIR &&
489 reply("ADD DIRECTORY CHECK-HASH PROTECTION") != 0) {
491 sblock.fs_metackhash |= CK_DIR;
494 if (ckhashadd != 0) {
495 sblock.fs_flags |= FS_METACKHASH;
500 * Cleared if any questions answered no. Used to decide if
501 * the superblock should be marked clean.
505 * 1: scan inodes tallying blocks used
508 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
509 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
510 printf("** Root file system\n");
511 printf("** Phase 1 - Check Blocks and Sizes\n");
513 clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
518 * 1b: locate first references to duplicates, if any
521 if (preen || usedsoftdep)
522 pfatal("INTERNAL ERROR: dups with %s%s%s",
524 (preen && usedsoftdep) ? " and " : "",
525 usedsoftdep ? "softupdates" : "");
526 printf("** Phase 1b - Rescan For More DUPS\n");
532 * 2: traverse directories from root to mark all connected directories
535 printf("** Phase 2 - Check Pathnames\n");
540 * 3: scan inodes looking for disconnected directories
543 printf("** Phase 3 - Check Connectivity\n");
548 * 4: scan inodes looking for disconnected files; check reference counts
551 printf("** Phase 4 - Check Reference Counts\n");
556 * 5: check and repair resource counts in cylinder groups
559 printf("** Phase 5 - Check Cyl groups\n");
564 * print out summary statistics
566 n_ffree = sblock.fs_cstotal.cs_nffree;
567 n_bfree = sblock.fs_cstotal.cs_nbfree;
568 files = maxino - UFS_ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
570 sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
571 blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
572 blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
573 blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
574 if (bkgrdflag && (files > 0 || blks > 0)) {
575 countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
576 pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n",
577 countdirs, files - countdirs, blks);
579 pwarn("%ld files, %jd used, %ju free ",
580 (long)n_files, (intmax_t)n_blks,
581 (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
582 printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
583 (uintmax_t)n_ffree, (uintmax_t)n_bfree,
584 n_ffree * 100.0 / sblock.fs_dsize);
587 printf("%jd inodes missing\n", -files);
589 printf("%jd blocks missing\n", -blks);
590 if (duplist != NULL) {
591 printf("The following duplicate blocks remain:");
592 for (dp = duplist; dp; dp = dp->next)
593 printf(" %jd,", (intmax_t)dp->dup);
597 duplist = (struct dups *)0;
598 muldup = (struct dups *)0;
601 sblock.fs_time = time(NULL);
604 if (cvtlevel && sblk.b_dirty) {
606 * Write out the duplicate super blocks
608 if (sbput(fswritefd, &sblock, sblock.fs_ncg) == 0)
616 * Check to see if the file system is mounted read-write.
618 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
622 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
623 if (inostathead[cylno].il_stat != NULL)
624 free((char *)inostathead[cylno].il_stat);
625 free((char *)inostathead);
627 if (fsmodified && !preen)
628 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
630 if (wantrestart && (restarts++ < 10) &&
631 (preen || reply("RESTART")))
633 printf("\n***** PLEASE RERUN FSCK *****\n");
635 if (chkdoreload(mntp) != 0) {
639 printf("\n***** REBOOT NOW *****\n");
643 return (rerun ? ERERUN : 0);
647 chkdoreload(struct statfs *mntp)
660 * We modified a mounted file system. Do a mount update on
661 * it unless it is read-write, so we can continue using it
662 * as safely as possible.
664 if (mntp->f_flags & MNT_RDONLY) {
665 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
666 build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
668 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
670 build_iovec(&iov, &iovlen, "errmsg", errmsg,
672 build_iovec(&iov, &iovlen, "update", NULL, 0);
673 build_iovec(&iov, &iovlen, "reload", NULL, 0);
675 * XX: We need the following line until we clean up
676 * nmount parsing of root mounts and NFS root mounts.
678 build_iovec(&iov, &iovlen, "ro", NULL, 0);
679 if (nmount(iov, iovlen, mntp->f_flags) == 0) {
682 pwarn("mount reload of '%s' failed: %s %s\n\n",
683 mntp->f_mntonname, strerror(errno), errmsg);
690 * Get the mount point information for name.
692 static struct statfs *
693 getmntpt(const char *name)
695 struct stat devstat, mntdevstat;
696 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
698 struct statfs *mntbuf, *statfsp;
699 int i, mntsize, isdev;
701 if (stat(name, &devstat) != 0)
703 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
707 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
708 for (i = 0; i < mntsize; i++) {
709 statfsp = &mntbuf[i];
710 ddevname = statfsp->f_mntfromname;
711 if (*ddevname != '/') {
712 if (strlen(_PATH_DEV) + strlen(ddevname) + 1 >
713 sizeof(statfsp->f_mntfromname))
715 strcpy(device, _PATH_DEV);
716 strcat(device, ddevname);
717 strcpy(statfsp->f_mntfromname, device);
720 if (strcmp(name, statfsp->f_mntonname))
724 if (stat(ddevname, &mntdevstat) == 0 &&
725 mntdevstat.st_rdev == devstat.st_rdev)
735 (void) fprintf(stderr,
736 "usage: %s [-BCdEFfnpRrSyZ] [-b block] [-c level] [-m mode] filesystem ...\n",
742 infohandler(int sig __unused)
748 alarmhandler(int sig __unused)