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 #include <sys/param.h>
48 #include <sys/mount.h>
49 #include <sys/resource.h>
51 #include <sys/sysctl.h>
53 #include <sys/disklabel.h>
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ffs/fs.h>
73 static void usage(void) __dead2;
74 static intmax_t argtoimax(int flag, const char *req, const char *str, int base);
75 static int checkfilesys(char *filesys);
76 static int chkdoreload(struct statfs *mntp);
77 static struct statfs *getmntpt(const char *);
80 main(int argc, char *argv[])
84 struct itimerval itimerval;
90 while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:npRrSyZ")) != -1) {
94 bflag = argtoimax('b', "number", optarg, 10);
95 printf("Alternate super block location: %jd\n", bflag);
104 cvtlevel = argtoimax('c', "conversion level", optarg,
107 errx(EEXIT, "cannot do level %d conversion",
128 lfmode = argtoimax('m', "mode", optarg, 8);
130 errx(EEXIT, "bad mode to -m: %o", lfmode);
131 printf("** lost+found creation mode %o\n", lfmode);
177 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
178 (void)signal(SIGINT, catch);
180 (void)signal(SIGQUIT, catchquit);
181 signal(SIGINFO, infohandler);
183 signal(SIGALRM, alarmhandler);
184 itimerval.it_interval.tv_sec = 5;
185 itimerval.it_interval.tv_usec = 0;
186 itimerval.it_value.tv_sec = 5;
187 itimerval.it_value.tv_usec = 0;
188 setitimer(ITIMER_REAL, &itimerval, NULL);
191 * Push up our allowed memory limit so we can cope
192 * with huge file systems.
194 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
195 rlimit.rlim_cur = rlimit.rlim_max;
196 (void)setrlimit(RLIMIT_DATA, &rlimit);
199 if (checkfilesys(*argv) == ERESTART)
211 argtoimax(int flag, const char *req, const char *str, int base)
216 ret = strtoimax(str, &cp, base);
217 if (cp == str || *cp)
218 errx(EEXIT, "-%c flag requires a %s", flag, req);
223 * Check the specified file system.
227 checkfilesys(char *filesys)
229 ufs2_daddr_t n_ffree, n_bfree;
239 intmax_t blks, files;
249 if (debug && ckclean)
252 * Make best effort to get the disk name. Check first to see
253 * if it is listed among the mounted file systems. Failing that
254 * check to see if it is listed in /etc/fstab.
256 mntp = getmntpt(filesys);
258 filesys = mntp->f_mntfromname;
260 filesys = blockcheck(filesys);
262 * If -F flag specified, check to see whether a background check
263 * is possible and needed. If possible and needed, exit with
264 * status zero. Otherwise exit with status non-zero. A non-zero
265 * exit status will cause a foreground check to be run.
269 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
270 exit(3); /* Cannot read superblock */
272 /* Earlier background failed or journaled */
273 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
275 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
276 exit(5); /* Not running soft updates */
278 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
279 exit(6); /* Lacks kernel support */
280 if ((mntp == NULL && sblock.fs_clean == 1) ||
281 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
282 exit(7); /* Filesystem clean, report it now */
285 if (ckclean && skipclean) {
287 * If file system is gjournaled, check it here.
289 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
290 exit(3); /* Cannot read superblock */
292 if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
293 //printf("GJournaled file system detected on %s.\n",
295 if (sblock.fs_clean == 1) {
296 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
299 if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
300 gjournal_check(filesys);
301 if (chkdoreload(mntp) == 0)
306 "UNEXPECTED INCONSISTENCY, CANNOT RUN FAST FSCK\n");
311 * If we are to do a background check:
312 * Get the mount point information of the file system
313 * create snapshot file
314 * return created snapshot file
315 * if not found, clear bkgrdflag and proceed with normal fsck
320 pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
321 } else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
324 "NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
325 } else if ((mntp->f_flags & MNT_RDONLY) != 0) {
327 pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
328 } else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
329 if (readsb(0) != 0) {
330 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) {
333 "UNEXPECTED INCONSISTENCY, CANNOT RUN IN BACKGROUND\n");
335 if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
336 skipclean && ckclean) {
338 * file system is clean;
339 * skip snapshot and report it clean
342 "FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
349 snprintf(snapname, sizeof snapname, "%s/.snap",
351 if (stat(snapname, &snapdir) < 0) {
352 if (errno != ENOENT) {
355 "CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
356 snapname, strerror(errno));
357 } else if ((grp = getgrnam("operator")) == NULL ||
358 mkdir(snapname, 0770) < 0 ||
359 chown(snapname, -1, grp->gr_gid) < 0 ||
360 chmod(snapname, 0770) < 0) {
363 "CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
364 snapname, strerror(errno));
366 } else if (!S_ISDIR(snapdir.st_mode)) {
369 "%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
374 snprintf(snapname, sizeof snapname,
375 "%s/.snap/fsck_snapshot", mntp->f_mntonname);
376 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
377 build_iovec(&iov, &iovlen, "from", snapname,
379 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
381 build_iovec(&iov, &iovlen, "errmsg", errmsg,
383 build_iovec(&iov, &iovlen, "update", NULL, 0);
384 build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
386 while (nmount(iov, iovlen, mntp->f_flags) < 0) {
387 if (errno == EEXIST && unlink(snapname) == 0)
390 pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n",
391 snapname, strerror(errno), errmsg);
399 switch (setup(filesys)) {
402 pfatal("CAN'T CHECK FILE SYSTEM.");
406 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
407 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
408 printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
409 (intmax_t)sblock.fs_cstotal.cs_nffree,
410 (intmax_t)sblock.fs_cstotal.cs_nbfree,
411 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
415 * Determine if we can and should do journal recovery.
417 if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
418 if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
419 if (preen || reply("USE JOURNAL")) {
420 if (suj_check(filesys) == 0) {
421 printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
422 if (chkdoreload(mntp) == 0)
427 printf("** Skipping journal, falling through to full fsck\n\n");
430 * Write the superblock so we don't try to recover the
431 * journal on another pass. If this is the only change
432 * to the filesystem, we do not want it to be called
435 sblock.fs_mtime = time(NULL);
437 ofsmodified = fsmodified;
438 flush(fswritefd, &sblk);
439 fsmodified = ofsmodified;
443 * Cleared if any questions answered no. Used to decide if
444 * the superblock should be marked clean.
448 * 1: scan inodes tallying blocks used
451 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
452 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
453 printf("** Root file system\n");
454 printf("** Phase 1 - Check Blocks and Sizes\n");
456 clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
461 * 1b: locate first references to duplicates, if any
464 if (preen || usedsoftdep)
465 pfatal("INTERNAL ERROR: dups with %s%s%s",
467 (preen && usedsoftdep) ? " and " : "",
468 usedsoftdep ? "softupdates" : "");
469 printf("** Phase 1b - Rescan For More DUPS\n");
475 * 2: traverse directories from root to mark all connected directories
478 printf("** Phase 2 - Check Pathnames\n");
483 * 3: scan inodes looking for disconnected directories
486 printf("** Phase 3 - Check Connectivity\n");
491 * 4: scan inodes looking for disconnected files; check reference counts
494 printf("** Phase 4 - Check Reference Counts\n");
499 * 5: check and repair resource counts in cylinder groups
502 printf("** Phase 5 - Check Cyl groups\n");
507 * print out summary statistics
509 n_ffree = sblock.fs_cstotal.cs_nffree;
510 n_bfree = sblock.fs_cstotal.cs_nbfree;
511 files = maxino - UFS_ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
513 sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
514 blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
515 blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
516 blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
517 if (bkgrdflag && (files > 0 || blks > 0)) {
518 countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
519 pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n",
520 countdirs, files - countdirs, blks);
522 pwarn("%ld files, %jd used, %ju free ",
523 (long)n_files, (intmax_t)n_blks,
524 (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
525 printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
526 (uintmax_t)n_ffree, (uintmax_t)n_bfree,
527 n_ffree * 100.0 / sblock.fs_dsize);
530 printf("%jd inodes missing\n", -files);
532 printf("%jd blocks missing\n", -blks);
533 if (duplist != NULL) {
534 printf("The following duplicate blocks remain:");
535 for (dp = duplist; dp; dp = dp->next)
536 printf(" %jd,", (intmax_t)dp->dup);
540 duplist = (struct dups *)0;
541 muldup = (struct dups *)0;
544 sblock.fs_time = time(NULL);
547 if (cvtlevel && sblk.b_dirty) {
549 * Write out the duplicate super blocks
551 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
552 blwrite(fswritefd, (char *)&sblock,
553 fsbtodb(&sblock, cgsblock(&sblock, cylno)),
561 * Check to see if the file system is mounted read-write.
563 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
567 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
568 if (inostathead[cylno].il_stat != NULL)
569 free((char *)inostathead[cylno].il_stat);
570 free((char *)inostathead);
572 if (fsmodified && !preen)
573 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
575 if (wantrestart && (restarts++ < 10) &&
576 (preen || reply("RESTART")))
578 printf("\n***** PLEASE RERUN FSCK *****\n");
580 if (chkdoreload(mntp) != 0) {
584 printf("\n***** REBOOT NOW *****\n");
592 chkdoreload(struct statfs *mntp)
605 * We modified a mounted file system. Do a mount update on
606 * it unless it is read-write, so we can continue using it
607 * as safely as possible.
609 if (mntp->f_flags & MNT_RDONLY) {
610 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
611 build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
613 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
615 build_iovec(&iov, &iovlen, "errmsg", errmsg,
617 build_iovec(&iov, &iovlen, "update", NULL, 0);
618 build_iovec(&iov, &iovlen, "reload", NULL, 0);
620 * XX: We need the following line until we clean up
621 * nmount parsing of root mounts and NFS root mounts.
623 build_iovec(&iov, &iovlen, "ro", NULL, 0);
624 if (nmount(iov, iovlen, mntp->f_flags) == 0) {
627 pwarn("mount reload of '%s' failed: %s %s\n\n",
628 mntp->f_mntonname, strerror(errno), errmsg);
635 * Get the mount point information for name.
637 static struct statfs *
638 getmntpt(const char *name)
640 struct stat devstat, mntdevstat;
641 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
643 struct statfs *mntbuf, *statfsp;
644 int i, mntsize, isdev;
646 if (stat(name, &devstat) != 0)
648 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
652 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
653 for (i = 0; i < mntsize; i++) {
654 statfsp = &mntbuf[i];
655 ddevname = statfsp->f_mntfromname;
656 if (*ddevname != '/') {
657 if (strlen(_PATH_DEV) + strlen(ddevname) + 1 >
658 sizeof(statfsp->f_mntfromname))
660 strcpy(device, _PATH_DEV);
661 strcat(device, ddevname);
662 strcpy(statfsp->f_mntfromname, device);
665 if (strcmp(name, statfsp->f_mntonname))
669 if (stat(ddevname, &mntdevstat) == 0 &&
670 mntdevstat.st_rdev == devstat.st_rdev)
680 (void) fprintf(stderr,
681 "usage: %s [-BCdEFfnpRrSyZ] [-b block] [-c level] [-m mode] filesystem ...\n",
687 infohandler(int sig __unused)
693 alarmhandler(int sig __unused)