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 setup_bkgrdchk(struct statfs *mntp, int sbrdfailed, char **filesys);
79 static int chkdoreload(struct statfs *mntp);
80 static struct statfs *getmntpt(const char *);
83 main(int argc, char *argv[])
87 struct itimerval itimerval;
94 while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:npRrSyZz")) != -1) {
98 bflag = argtoimax('b', "number", optarg, 10);
99 printf("Alternate super block location: %jd\n", bflag);
108 cvtlevel = argtoimax('c', "conversion level", optarg,
111 errx(EEXIT, "cannot do level %d conversion",
132 lfmode = argtoimax('m', "mode", optarg, 8);
134 errx(EEXIT, "bad mode to -m: %o", lfmode);
135 printf("** lost+found creation mode %o\n", lfmode);
185 if (bkgrdflag && cvtlevel > 0) {
186 pfatal("CANNOT CONVERT A SNAPSHOT\n");
190 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
191 (void)signal(SIGINT, catch);
193 (void)signal(SIGQUIT, catchquit);
194 signal(SIGINFO, infohandler);
196 signal(SIGALRM, alarmhandler);
197 itimerval.it_interval.tv_sec = 5;
198 itimerval.it_interval.tv_usec = 0;
199 itimerval.it_value.tv_sec = 5;
200 itimerval.it_value.tv_usec = 0;
201 setitimer(ITIMER_REAL, &itimerval, NULL);
204 * Push up our allowed memory limit so we can cope
205 * with huge file systems.
207 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
208 rlimit.rlim_cur = rlimit.rlim_max;
209 (void)setrlimit(RLIMIT_DATA, &rlimit);
212 if ((fsret = checkfilesys(*argv)) == ERESTART)
225 argtoimax(int flag, const char *req, const char *str, int base)
230 ret = strtoimax(str, &cp, base);
231 if (cp == str || *cp)
232 errx(EEXIT, "-%c flag requires a %s", flag, req);
237 * Check the specified file system.
241 checkfilesys(char *filesys)
243 ufs2_daddr_t n_ffree, n_bfree;
246 intmax_t blks, files;
248 int sbreadfailed, ofsmodified;
254 if (debug && ckclean)
257 * Make best effort to get the disk name. Check first to see
258 * if it is listed among the mounted file systems. Failing that
259 * check to see if it is listed in /etc/fstab.
261 mntp = getmntpt(filesys);
263 filesys = mntp->f_mntfromname;
265 filesys = blockcheck(filesys);
267 * If -F flag specified, check to see whether a background check
268 * is possible and needed. If possible and needed, exit with
269 * status zero. Otherwise exit with status non-zero. A non-zero
270 * exit status will cause a foreground check to be run.
274 if (openfilesys(filesys) == 0 || readsb(0) == 0)
278 exit(3); /* Cannot read superblock */
279 /* Earlier background failed or journaled */
280 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
282 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
283 exit(5); /* Not running soft updates */
285 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
286 exit(6); /* Lacks kernel support */
287 if ((mntp == NULL && sblock.fs_clean == 1) ||
288 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
289 exit(7); /* Filesystem clean, report it now */
292 if (ckclean && skipclean) {
294 * If file system is gjournaled, check it here.
297 exit(3); /* Cannot read superblock */
298 if (bkgrdflag == 0 &&
299 (nflag || (fswritefd = open(filesys, O_WRONLY)) < 0)) {
302 pfatal("NO WRITE ACCESS");
303 printf(" (NO WRITE)");
305 if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
306 if (sblock.fs_clean == 1) {
307 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
310 if ((sblock.fs_flags &
311 (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
313 gjournal_check(filesys);
314 if (chkdoreload(mntp) == 0)
318 pfatal("FULL FSCK NEEDED, CANNOT RUN FAST "
326 switch (setup_bkgrdchk(mntp, sbreadfailed, &filesys)) {
327 case -1: /* filesystem clean */
329 case 0: /* cannot do background, give up */
331 case 1: /* doing background check, preen rules apply */
337 switch (setup(filesys)) {
340 pfatal("CAN'T CHECK FILE SYSTEM.");
344 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
345 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
346 printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
347 (intmax_t)sblock.fs_cstotal.cs_nffree,
348 (intmax_t)sblock.fs_cstotal.cs_nbfree,
349 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
353 * Determine if we can and should do journal recovery.
355 if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
356 if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
358 if (suj_check(filesys) == 0) {
359 printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
360 if (chkdoreload(mntp) == 0)
365 printf("** Skipping journal, falling through to full fsck\n\n");
367 if (fswritefd != -1) {
369 * Write the superblock so we don't try to recover the
370 * journal on another pass. If this is the only change
371 * to the filesystem, we do not want it to be called
374 sblock.fs_mtime = time(NULL);
376 ofsmodified = fsmodified;
377 flush(fswritefd, &sblk);
378 fsmodified = ofsmodified;
382 * If the filesystem was run on an old kernel that did not
383 * support check hashes, clear the check-hash flags so that
384 * we do not try to verify them.
386 if ((sblock.fs_flags & FS_METACKHASH) == 0)
387 sblock.fs_metackhash = 0;
389 * If we are running on a kernel that can provide check hashes
390 * that are not yet enabled for the filesystem and we are
391 * running manually without the -y flag, offer to add any
392 * supported check hashes that are not already enabled.
395 if (preen == 0 && yflag == 0 && sblock.fs_magic != FS_UFS1_MAGIC &&
396 fswritefd != -1 && getosreldate() >= P_OSREL_CK_CYLGRP) {
397 if ((sblock.fs_metackhash & CK_CYLGRP) == 0 &&
398 reply("ADD CYLINDER GROUP CHECK-HASH PROTECTION") != 0) {
399 ckhashadd |= CK_CYLGRP;
400 sblock.fs_metackhash |= CK_CYLGRP;
402 if ((sblock.fs_metackhash & CK_SUPERBLOCK) == 0 &&
403 getosreldate() >= P_OSREL_CK_SUPERBLOCK &&
404 reply("ADD SUPERBLOCK CHECK-HASH PROTECTION") != 0) {
405 ckhashadd |= CK_SUPERBLOCK;
406 sblock.fs_metackhash |= CK_SUPERBLOCK;
408 if ((sblock.fs_metackhash & CK_INODE) == 0 &&
409 getosreldate() >= P_OSREL_CK_INODE &&
410 reply("ADD INODE CHECK-HASH PROTECTION") != 0) {
411 ckhashadd |= CK_INODE;
412 sblock.fs_metackhash |= CK_INODE;
415 if ((sblock.fs_metackhash & CK_INDIR) == 0 &&
416 getosreldate() >= P_OSREL_CK_INDIR &&
417 reply("ADD INDIRECT BLOCK CHECK-HASH PROTECTION") != 0) {
418 ckhashadd |= CK_INDIR;
419 sblock.fs_metackhash |= CK_INDIR;
421 if ((sblock.fs_metackhash & CK_DIR) == 0 &&
422 getosreldate() >= P_OSREL_CK_DIR &&
423 reply("ADD DIRECTORY CHECK-HASH PROTECTION") != 0) {
425 sblock.fs_metackhash |= CK_DIR;
428 if (ckhashadd != 0) {
429 sblock.fs_flags |= FS_METACKHASH;
434 * Cleared if any questions answered no. Used to decide if
435 * the superblock should be marked clean.
439 * 1: scan inodes tallying blocks used
442 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
443 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
444 printf("** Root file system\n");
445 printf("** Phase 1 - Check Blocks and Sizes\n");
447 clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
452 * 1b: locate first references to duplicates, if any
455 if (preen || usedsoftdep)
456 pfatal("INTERNAL ERROR: DUPS WITH %s%s%s",
458 (preen && usedsoftdep) ? " AND " : "",
459 usedsoftdep ? "SOFTUPDATES" : "");
460 printf("** Phase 1b - Rescan For More DUPS\n");
466 * 2: traverse directories from root to mark all connected directories
469 printf("** Phase 2 - Check Pathnames\n");
474 * 3: scan inodes looking for disconnected directories
477 printf("** Phase 3 - Check Connectivity\n");
482 * 4: scan inodes looking for disconnected files; check reference counts
485 printf("** Phase 4 - Check Reference Counts\n");
490 * 5: check and repair resource counts in cylinder groups
493 printf("** Phase 5 - Check Cyl groups\n");
498 * print out summary statistics
500 n_ffree = sblock.fs_cstotal.cs_nffree;
501 n_bfree = sblock.fs_cstotal.cs_nbfree;
502 files = maxino - UFS_ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
504 sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
505 blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
506 blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
507 blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
508 if (bkgrdflag && (files > 0 || blks > 0)) {
509 countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
510 pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n",
511 countdirs, files - countdirs, blks);
513 pwarn("%ld files, %jd used, %ju free ",
514 (long)n_files, (intmax_t)n_blks,
515 (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
516 printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
517 (uintmax_t)n_ffree, (uintmax_t)n_bfree,
518 n_ffree * 100.0 / sblock.fs_dsize);
521 printf("%jd inodes missing\n", -files);
523 printf("%jd blocks missing\n", -blks);
524 if (duplist != NULL) {
525 printf("The following duplicate blocks remain:");
526 for (dp = duplist; dp; dp = dp->next)
527 printf(" %jd,", (intmax_t)dp->dup);
531 duplist = (struct dups *)0;
532 muldup = (struct dups *)0;
535 sblock.fs_time = time(NULL);
538 if (cvtlevel && (sblk.b_flags & B_DIRTY) != 0) {
540 * Write out the duplicate super blocks
542 if (sbput(fswritefd, &sblock, sblock.fs_ncg) == 0)
549 * Check to see if the file system is mounted read-write.
551 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
555 if (fsmodified && !preen)
556 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
558 if (wantrestart && (restarts++ < 10) &&
559 (preen || reply("RESTART")))
561 printf("\n***** PLEASE RERUN FSCK *****\n");
563 if (chkdoreload(mntp) != 0) {
567 printf("\n***** REBOOT NOW *****\n");
571 return (rerun ? ERERUN : 0);
575 * If we are to do a background check:
576 * Get the mount point information of the file system
577 * If already clean, return -1
578 * Check that kernel supports background fsck
579 * Find or create the snapshot directory
580 * Create the snapshot file
582 * If anything fails print reason and return 0 which exits
585 setup_bkgrdchk(struct statfs *mntp, int sbreadfailed, char **filesys)
594 /* Get the mount point information of the file system */
596 pwarn("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
599 if ((mntp->f_flags & MNT_RDONLY) != 0) {
600 pwarn("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
603 if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
604 pwarn("NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
608 pwarn("SUPERBLOCK READ FAILED, CANNOT RUN IN BACKGROUND\n");
611 if ((sblock.fs_flags & FS_NEEDSFSCK) != 0) {
612 pwarn("FULL FSCK NEEDED, CANNOT RUN IN BACKGROUND\n");
615 if ((sblock.fs_flags & FS_SUJ) != 0) {
616 pwarn("JOURNALED FILESYSTEM, CANNOT RUN IN BACKGROUND\n");
619 if (skipclean && ckclean &&
620 (sblock.fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK)) == 0) {
622 * file system is clean;
623 * skip snapshot and report it clean
625 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
628 /* Check that kernel supports background fsck */
630 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
631 sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
632 sysctlnametomib("vfs.ffs.setsize", setsize, &size) < 0 ||
633 sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
634 sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
635 sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
636 pwarn("KERNEL LACKS BACKGROUND FSCK SUPPORT\n");
640 * When kernel lacks runtime bgfsck superblock summary
641 * adjustment functionality, it does not mean we can not
642 * continue, as old kernels will recompute the summary at
643 * mount time. However, it will be an unexpected softupdates
644 * inconsistency if it turns out that the summary is still
645 * incorrect. Set a flag so subsequent operation can know this.
648 if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 ||
649 sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 ||
650 sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 ||
651 sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 ||
652 sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters,
655 pwarn("KERNEL LACKS RUNTIME SUPERBLOCK SUMMARY ADJUSTMENT "
658 /* Find or create the snapshot directory */
659 snprintf(snapname, sizeof snapname, "%s/.snap",
661 if (stat(snapname, &snapdir) < 0) {
662 if (errno != ENOENT) {
663 pwarn("CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT "
664 "RUN IN BACKGROUND\n", snapname, strerror(errno));
667 if ((grp = getgrnam("operator")) == NULL ||
668 mkdir(snapname, 0770) < 0 ||
669 chown(snapname, -1, grp->gr_gid) < 0 ||
670 chmod(snapname, 0770) < 0) {
671 pwarn("CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, "
672 "CANNOT RUN IN BACKGROUND\n", snapname,
676 } else if (!S_ISDIR(snapdir.st_mode)) {
677 pwarn("%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
681 /* Create the snapshot file */
685 snprintf(snapname, sizeof snapname, "%s/.snap/fsck_snapshot",
687 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
688 build_iovec(&iov, &iovlen, "from", snapname, (size_t)-1);
689 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname, (size_t)-1);
690 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
691 build_iovec(&iov, &iovlen, "update", NULL, 0);
692 build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
693 /* Create snapshot, removing old snapshot if it exists */
694 while (nmount(iov, iovlen, mntp->f_flags) < 0) {
695 if (errno == EEXIST && unlink(snapname) == 0)
697 pwarn("CANNOT CREATE SNAPSHOT %s: %s %s\n", snapname,
698 strerror(errno), errmsg);
702 if (openfilesys(snapname) == 0) {
704 pwarn("CANNOT OPEN SNAPSHOT %s: %s, CANNOT RUN IN "
705 "BACKGROUND\n", snapname, strerror(errno));
712 cmd.version = FFS_CMD_VERSION;
713 cmd.handle = fsreadfd;
718 chkdoreload(struct statfs *mntp)
731 * We modified a mounted file system. Do a mount update on
732 * it unless it is read-write, so we can continue using it
733 * as safely as possible.
735 if (mntp->f_flags & MNT_RDONLY) {
736 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
737 build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
739 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
741 build_iovec(&iov, &iovlen, "errmsg", errmsg,
743 build_iovec(&iov, &iovlen, "update", NULL, 0);
744 build_iovec(&iov, &iovlen, "reload", NULL, 0);
746 * XX: We need the following line until we clean up
747 * nmount parsing of root mounts and NFS root mounts.
749 build_iovec(&iov, &iovlen, "ro", NULL, 0);
750 if (nmount(iov, iovlen, mntp->f_flags) == 0) {
753 pwarn("mount reload of '%s' failed: %s %s\n\n",
754 mntp->f_mntonname, strerror(errno), errmsg);
761 * Get the mount point information for name.
763 static struct statfs *
764 getmntpt(const char *name)
766 struct stat devstat, mntdevstat;
767 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
769 struct statfs *mntbuf, *statfsp;
770 int i, mntsize, isdev;
772 if (stat(name, &devstat) != 0)
774 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
778 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
779 for (i = 0; i < mntsize; i++) {
780 statfsp = &mntbuf[i];
781 ddevname = statfsp->f_mntfromname;
782 if (*ddevname != '/') {
783 if (strlen(_PATH_DEV) + strlen(ddevname) + 1 >
784 sizeof(statfsp->f_mntfromname))
786 strcpy(device, _PATH_DEV);
787 strcat(device, ddevname);
788 strcpy(statfsp->f_mntfromname, device);
791 if (strcmp(name, statfsp->f_mntonname))
795 if (stat(ddevname, &mntdevstat) == 0 &&
796 mntdevstat.st_rdev == devstat.st_rdev)
806 (void) fprintf(stderr,
807 "usage: %s [-BCdEFfnpRrSyZ] [-b block] [-c level] [-m mode] filesystem ...\n",
813 infohandler(int sig __unused)
819 alarmhandler(int sig __unused)