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);
81 main(int argc, char *argv[])
85 struct itimerval itimerval;
92 while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:npRrSyZz")) != -1) {
96 bflag = argtoimax('b', "number", optarg, 10);
97 printf("Alternate super block location: %jd\n", bflag);
106 cvtlevel = argtoimax('c', "conversion level", optarg,
109 errx(EEXIT, "cannot do level %d conversion",
130 lfmode = argtoimax('m', "mode", optarg, 8);
132 errx(EEXIT, "bad mode to -m: %o", lfmode);
133 printf("** lost+found creation mode %o\n", lfmode);
183 if (bkgrdflag && cvtlevel > 0) {
184 pfatal("CANNOT CONVERT A SNAPSHOT\n");
188 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
189 (void)signal(SIGINT, catch);
191 (void)signal(SIGQUIT, catchquit);
192 signal(SIGINFO, infohandler);
194 signal(SIGALRM, alarmhandler);
195 itimerval.it_interval.tv_sec = 5;
196 itimerval.it_interval.tv_usec = 0;
197 itimerval.it_value.tv_sec = 5;
198 itimerval.it_value.tv_usec = 0;
199 setitimer(ITIMER_REAL, &itimerval, NULL);
202 * Push up our allowed memory limit so we can cope
203 * with huge file systems.
205 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
206 rlimit.rlim_cur = rlimit.rlim_max;
207 (void)setrlimit(RLIMIT_DATA, &rlimit);
210 if ((fsret = checkfilesys(*argv)) == ERESTART)
223 argtoimax(int flag, const char *req, const char *str, int base)
228 ret = strtoimax(str, &cp, base);
229 if (cp == str || *cp)
230 errx(EEXIT, "-%c flag requires a %s", flag, req);
235 * Check the specified file system.
239 checkfilesys(char *filesys)
241 ufs2_daddr_t n_ffree, n_bfree;
244 intmax_t blks, files;
246 int sbreadfailed, ofsmodified;
252 if (debug && ckclean)
255 * Make best effort to get the disk name. Check first to see
256 * if it is listed among the mounted file systems. Failing that
257 * check to see if it is listed in /etc/fstab.
259 mntp = getmntpoint(filesys);
261 filesys = mntp->f_mntfromname;
263 filesys = blockcheck(filesys);
265 * If -F flag specified, check to see whether a background check
266 * is possible and needed. If possible and needed, exit with
267 * status zero. Otherwise exit with status non-zero. A non-zero
268 * exit status will cause a foreground check to be run.
272 if (openfilesys(filesys) == 0 || readsb() == 0)
276 exit(3); /* Cannot read superblock */
277 /* Earlier background failed or journaled */
278 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
280 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
281 exit(5); /* Not running soft updates */
283 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
284 exit(6); /* Lacks kernel support */
285 if ((mntp == NULL && sblock.fs_clean == 1) ||
286 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
287 exit(7); /* Filesystem clean, report it now */
290 if (ckclean && skipclean) {
292 * If file system is gjournaled, check it here.
295 exit(3); /* Cannot read superblock */
296 if (bkgrdflag == 0 &&
297 (nflag || (fswritefd = open(filesys, O_WRONLY)) < 0)) {
300 pfatal("NO WRITE ACCESS");
301 printf(" (NO WRITE)");
303 if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
304 if (sblock.fs_clean == 1) {
305 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
308 if ((sblock.fs_flags &
309 (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
311 gjournal_check(filesys);
312 if (chkdoreload(mntp, pwarn) == 0)
316 pfatal("FULL FSCK NEEDED, CANNOT RUN FAST "
324 switch (setup_bkgrdchk(mntp, sbreadfailed, &filesys)) {
325 case -1: /* filesystem clean */
327 case 0: /* cannot do background, give up */
329 case 1: /* doing background check, preen rules apply */
335 switch (setup(filesys)) {
338 pfatal("CAN'T CHECK FILE SYSTEM.");
342 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
343 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
344 printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
345 (intmax_t)sblock.fs_cstotal.cs_nffree,
346 (intmax_t)sblock.fs_cstotal.cs_nbfree,
347 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
351 * Determine if we can and should do journal recovery.
353 if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
354 if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
356 if (suj_check(filesys) == 0) {
357 printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
358 if (chkdoreload(mntp, pwarn) == 0)
363 printf("** Skipping journal, falling through to full fsck\n\n");
365 if (fswritefd != -1) {
367 * Write the superblock so we don't try to recover the
368 * journal on another pass. If this is the only change
369 * to the filesystem, we do not want it to be called
372 sblock.fs_mtime = time(NULL);
374 ofsmodified = fsmodified;
375 flush(fswritefd, &sblk);
376 fsmodified = ofsmodified;
380 * If the filesystem was run on an old kernel that did not
381 * support check hashes, clear the check-hash flags so that
382 * we do not try to verify them.
384 if ((sblock.fs_flags & FS_METACKHASH) == 0)
385 sblock.fs_metackhash = 0;
387 * If we are running on a kernel that can provide check hashes
388 * that are not yet enabled for the filesystem and we are
389 * running manually without the -y flag, offer to add any
390 * supported check hashes that are not already enabled.
393 if (preen == 0 && yflag == 0 && sblock.fs_magic != FS_UFS1_MAGIC &&
394 fswritefd != -1 && getosreldate() >= P_OSREL_CK_CYLGRP) {
395 if ((sblock.fs_metackhash & CK_CYLGRP) == 0 &&
396 reply("ADD CYLINDER GROUP CHECK-HASH PROTECTION") != 0) {
397 ckhashadd |= CK_CYLGRP;
398 sblock.fs_metackhash |= CK_CYLGRP;
400 if ((sblock.fs_metackhash & CK_SUPERBLOCK) == 0 &&
401 getosreldate() >= P_OSREL_CK_SUPERBLOCK &&
402 reply("ADD SUPERBLOCK CHECK-HASH PROTECTION") != 0) {
403 ckhashadd |= CK_SUPERBLOCK;
404 sblock.fs_metackhash |= CK_SUPERBLOCK;
406 if ((sblock.fs_metackhash & CK_INODE) == 0 &&
407 getosreldate() >= P_OSREL_CK_INODE &&
408 reply("ADD INODE CHECK-HASH PROTECTION") != 0) {
409 ckhashadd |= CK_INODE;
410 sblock.fs_metackhash |= CK_INODE;
413 if ((sblock.fs_metackhash & CK_INDIR) == 0 &&
414 getosreldate() >= P_OSREL_CK_INDIR &&
415 reply("ADD INDIRECT BLOCK CHECK-HASH PROTECTION") != 0) {
416 ckhashadd |= CK_INDIR;
417 sblock.fs_metackhash |= CK_INDIR;
419 if ((sblock.fs_metackhash & CK_DIR) == 0 &&
420 getosreldate() >= P_OSREL_CK_DIR &&
421 reply("ADD DIRECTORY CHECK-HASH PROTECTION") != 0) {
423 sblock.fs_metackhash |= CK_DIR;
426 if (ckhashadd != 0) {
427 sblock.fs_flags |= FS_METACKHASH;
432 * Cleared if any questions answered no. Used to decide if
433 * the superblock should be marked clean.
437 * 1: scan inodes tallying blocks used
440 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
441 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
442 printf("** Root file system\n");
443 printf("** Phase 1 - Check Blocks and Sizes\n");
445 clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
450 * 1b: locate first references to duplicates, if any
453 if (preen || usedsoftdep)
454 pfatal("INTERNAL ERROR: DUPS WITH %s%s%s",
456 (preen && usedsoftdep) ? " AND " : "",
457 usedsoftdep ? "SOFTUPDATES" : "");
458 printf("** Phase 1b - Rescan For More DUPS\n");
464 * 2: traverse directories from root to mark all connected directories
467 printf("** Phase 2 - Check Pathnames\n");
472 * 3: scan inodes looking for disconnected directories
475 printf("** Phase 3 - Check Connectivity\n");
480 * 4: scan inodes looking for disconnected files; check reference counts
483 printf("** Phase 4 - Check Reference Counts\n");
488 * 5: check and repair resource counts in cylinder groups
491 printf("** Phase 5 - Check Cyl groups\n");
492 snapflush(std_checkblkavail);
497 * print out summary statistics
499 n_ffree = sblock.fs_cstotal.cs_nffree;
500 n_bfree = sblock.fs_cstotal.cs_nbfree;
501 files = maxino - UFS_ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
503 sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
504 blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
505 blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
506 blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
507 if (bkgrdflag && (files > 0 || blks > 0)) {
508 countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
509 pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n",
510 countdirs, files - countdirs, blks);
512 pwarn("%ld files, %jd used, %ju free ",
513 (long)n_files, (intmax_t)n_blks,
514 (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
515 printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
516 (uintmax_t)n_ffree, (uintmax_t)n_bfree,
517 n_ffree * 100.0 / sblock.fs_dsize);
520 printf("%jd inodes missing\n", -files);
522 printf("%jd blocks missing\n", -blks);
523 if (duplist != NULL) {
524 printf("The following duplicate blocks remain:");
525 for (dp = duplist; dp; dp = dp->next)
526 printf(" %jd,", (intmax_t)dp->dup);
530 duplist = (struct dups *)0;
531 muldup = (struct dups *)0;
534 sblock.fs_time = time(NULL);
537 if (cvtlevel && (sblk.b_flags & B_DIRTY) != 0) {
539 * Write out the duplicate super blocks
541 if (sbput(fswritefd, &sblock, sblock.fs_ncg) == 0)
548 * Check to see if the file system is mounted read-write.
550 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
554 if (fsmodified && !preen)
555 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
557 if (wantrestart && (restarts++ < 10) &&
558 (preen || reply("RESTART")))
560 printf("\n***** PLEASE RERUN FSCK *****\n");
562 if (chkdoreload(mntp, pwarn) != 0) {
566 printf("\n***** REBOOT NOW *****\n");
570 return (rerun ? ERERUN : 0);
574 * If we are to do a background check:
575 * Get the mount point information of the file system
576 * If already clean, return -1
577 * Check that kernel supports background fsck
578 * Find or create the snapshot directory
579 * Create the snapshot file
581 * If anything fails print reason and return 0 which exits
584 setup_bkgrdchk(struct statfs *mntp, int sbreadfailed, char **filesys)
593 /* Get the mount point information of the file system */
595 pwarn("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
598 if ((mntp->f_flags & MNT_RDONLY) != 0) {
599 pwarn("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
602 if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
603 pwarn("NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
607 pwarn("SUPERBLOCK READ FAILED, CANNOT RUN IN BACKGROUND\n");
610 if ((sblock.fs_flags & FS_NEEDSFSCK) != 0) {
611 pwarn("FULL FSCK NEEDED, CANNOT RUN IN BACKGROUND\n");
614 if ((sblock.fs_flags & FS_SUJ) != 0) {
615 pwarn("JOURNALED FILESYSTEM, CANNOT RUN IN BACKGROUND\n");
618 if (skipclean && ckclean &&
619 (sblock.fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK)) == 0) {
621 * file system is clean;
622 * skip snapshot and report it clean
624 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
627 /* Check that kernel supports background fsck */
629 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
630 sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
631 sysctlnametomib("vfs.ffs.setsize", setsize, &size) < 0 ||
632 sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
633 sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
634 sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
635 pwarn("KERNEL LACKS BACKGROUND FSCK SUPPORT\n");
639 * When kernel lacks runtime bgfsck superblock summary
640 * adjustment functionality, it does not mean we can not
641 * continue, as old kernels will recompute the summary at
642 * mount time. However, it will be an unexpected softupdates
643 * inconsistency if it turns out that the summary is still
644 * incorrect. Set a flag so subsequent operation can know this.
647 if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 ||
648 sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 ||
649 sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 ||
650 sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 ||
651 sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters,
654 pwarn("KERNEL LACKS RUNTIME SUPERBLOCK SUMMARY ADJUSTMENT "
657 /* Find or create the snapshot directory */
658 snprintf(snapname, sizeof snapname, "%s/.snap",
660 if (stat(snapname, &snapdir) < 0) {
661 if (errno != ENOENT) {
662 pwarn("CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT "
663 "RUN IN BACKGROUND\n", snapname, strerror(errno));
666 if ((grp = getgrnam("operator")) == NULL ||
667 mkdir(snapname, 0770) < 0 ||
668 chown(snapname, -1, grp->gr_gid) < 0 ||
669 chmod(snapname, 0770) < 0) {
670 pwarn("CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, "
671 "CANNOT RUN IN BACKGROUND\n", snapname,
675 } else if (!S_ISDIR(snapdir.st_mode)) {
676 pwarn("%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
680 /* Create the snapshot file */
684 snprintf(snapname, sizeof snapname, "%s/.snap/fsck_snapshot",
686 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
687 build_iovec(&iov, &iovlen, "from", snapname, (size_t)-1);
688 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname, (size_t)-1);
689 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
690 build_iovec(&iov, &iovlen, "update", NULL, 0);
691 build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
692 /* Create snapshot, removing old snapshot if it exists */
693 while (nmount(iov, iovlen, mntp->f_flags) < 0) {
694 if (errno == EEXIST && unlink(snapname) == 0)
696 pwarn("CANNOT CREATE SNAPSHOT %s: %s %s\n", snapname,
697 strerror(errno), errmsg);
701 if (openfilesys(snapname) == 0) {
703 pwarn("CANNOT OPEN SNAPSHOT %s: %s, CANNOT RUN IN "
704 "BACKGROUND\n", snapname, strerror(errno));
711 cmd.version = FFS_CMD_VERSION;
712 cmd.handle = fsreadfd;
719 (void) fprintf(stderr,
720 "usage: %s [-BCdEFfnpRrSyZ] [-b block] [-c level] [-m mode] filesystem ...\n",
726 infohandler(int sig __unused)
732 alarmhandler(int sig __unused)