2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1980, 1986, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
46 #include <sys/mount.h>
47 #include <sys/resource.h>
49 #include <sys/sysctl.h>
51 #include <sys/disklabel.h>
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ffs/fs.h>
71 static void usage(void) __dead2;
72 static intmax_t argtoimax(int flag, const char *req, const char *str, int base);
73 static int checkfilesys(char *filesys);
74 static int chkdoreload(struct statfs *mntp);
75 static struct statfs *getmntpt(const char *);
78 main(int argc, char *argv[])
82 struct itimerval itimerval;
88 while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:npRrSyZ")) != -1) {
92 bflag = argtoimax('b', "number", optarg, 10);
93 printf("Alternate super block location: %jd\n", bflag);
102 cvtlevel = argtoimax('c', "conversion level", optarg,
105 errx(EEXIT, "cannot do level %d conversion",
126 lfmode = argtoimax('m', "mode", optarg, 8);
128 errx(EEXIT, "bad mode to -m: %o", lfmode);
129 printf("** lost+found creation mode %o\n", lfmode);
175 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
176 (void)signal(SIGINT, catch);
178 (void)signal(SIGQUIT, catchquit);
179 signal(SIGINFO, infohandler);
181 signal(SIGALRM, alarmhandler);
182 itimerval.it_interval.tv_sec = 5;
183 itimerval.it_interval.tv_usec = 0;
184 itimerval.it_value.tv_sec = 5;
185 itimerval.it_value.tv_usec = 0;
186 setitimer(ITIMER_REAL, &itimerval, NULL);
189 * Push up our allowed memory limit so we can cope
190 * with huge file systems.
192 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
193 rlimit.rlim_cur = rlimit.rlim_max;
194 (void)setrlimit(RLIMIT_DATA, &rlimit);
197 if (checkfilesys(*argv) == ERESTART)
209 argtoimax(int flag, const char *req, const char *str, int base)
214 ret = strtoimax(str, &cp, base);
215 if (cp == str || *cp)
216 errx(EEXIT, "-%c flag requires a %s", flag, req);
221 * Check the specified file system.
225 checkfilesys(char *filesys)
227 ufs2_daddr_t n_ffree, n_bfree;
236 intmax_t blks, files;
246 if (debug && ckclean)
249 * Make best effort to get the disk name. Check first to see
250 * if it is listed among the mounted file systems. Failing that
251 * check to see if it is listed in /etc/fstab.
253 mntp = getmntpt(filesys);
255 filesys = mntp->f_mntfromname;
257 filesys = blockcheck(filesys);
259 * If -F flag specified, check to see whether a background check
260 * is possible and needed. If possible and needed, exit with
261 * status zero. Otherwise exit with status non-zero. A non-zero
262 * exit status will cause a foreground check to be run.
266 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
267 exit(3); /* Cannot read superblock */
269 /* Earlier background failed or journaled */
270 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
272 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
273 exit(5); /* Not running soft updates */
275 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
276 exit(6); /* Lacks kernel support */
277 if ((mntp == NULL && sblock.fs_clean == 1) ||
278 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
279 exit(7); /* Filesystem clean, report it now */
282 if (ckclean && skipclean) {
284 * If file system is gjournaled, check it here.
286 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
287 exit(3); /* Cannot read superblock */
289 if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
290 //printf("GJournaled file system detected on %s.\n",
292 if (sblock.fs_clean == 1) {
293 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
296 if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
297 gjournal_check(filesys);
298 if (chkdoreload(mntp) == 0)
303 "UNEXPECTED INCONSISTENCY, CANNOT RUN FAST FSCK\n");
308 * If we are to do a background check:
309 * Get the mount point information of the file system
310 * create snapshot file
311 * return created snapshot file
312 * if not found, clear bkgrdflag and proceed with normal fsck
317 pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
318 } else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
321 "NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
322 } else if ((mntp->f_flags & MNT_RDONLY) != 0) {
324 pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
325 } else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
326 if (readsb(0) != 0) {
327 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) {
330 "UNEXPECTED INCONSISTENCY, CANNOT RUN IN BACKGROUND\n");
332 if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
333 skipclean && ckclean) {
335 * file system is clean;
336 * skip snapshot and report it clean
339 "FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
346 snprintf(snapname, sizeof snapname, "%s/.snap",
348 if (stat(snapname, &snapdir) < 0) {
349 if (errno != ENOENT) {
352 "CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
353 snapname, strerror(errno));
354 } else if ((grp = getgrnam("operator")) == 0 ||
355 mkdir(snapname, 0770) < 0 ||
356 chown(snapname, -1, grp->gr_gid) < 0 ||
357 chmod(snapname, 0770) < 0) {
360 "CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
361 snapname, strerror(errno));
363 } else if (!S_ISDIR(snapdir.st_mode)) {
366 "%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
371 snprintf(snapname, sizeof snapname,
372 "%s/.snap/fsck_snapshot", mntp->f_mntonname);
373 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
374 build_iovec(&iov, &iovlen, "from", snapname,
376 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
378 build_iovec(&iov, &iovlen, "errmsg", errmsg,
380 build_iovec(&iov, &iovlen, "update", NULL, 0);
381 build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
383 while (nmount(iov, iovlen, mntp->f_flags) < 0) {
384 if (errno == EEXIST && unlink(snapname) == 0)
387 pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n",
388 snapname, strerror(errno), errmsg);
396 switch (setup(filesys)) {
399 pfatal("CAN'T CHECK FILE SYSTEM.");
403 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
404 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
405 printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
406 (intmax_t)sblock.fs_cstotal.cs_nffree,
407 (intmax_t)sblock.fs_cstotal.cs_nbfree,
408 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
412 * Determine if we can and should do journal recovery.
414 if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
415 if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
416 if (preen || reply("USE JOURNAL")) {
417 if (suj_check(filesys) == 0) {
418 printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
419 if (chkdoreload(mntp) == 0)
424 printf("** Skipping journal, falling through to full fsck\n\n");
427 * Write the superblock so we don't try to recover the
428 * journal on another pass.
430 sblock.fs_mtime = time(NULL);
435 * Cleared if any questions answered no. Used to decide if
436 * the superblock should be marked clean.
440 * 1: scan inodes tallying blocks used
443 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
444 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
445 printf("** Root file system\n");
446 printf("** Phase 1 - Check Blocks and Sizes\n");
448 clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
453 * 1b: locate first references to duplicates, if any
456 if (preen || usedsoftdep)
457 pfatal("INTERNAL ERROR: dups with %s%s%s",
459 (preen && usedsoftdep) ? " and " : "",
460 usedsoftdep ? "softupdates" : "");
461 printf("** Phase 1b - Rescan For More DUPS\n");
467 * 2: traverse directories from root to mark all connected directories
470 printf("** Phase 2 - Check Pathnames\n");
475 * 3: scan inodes looking for disconnected directories
478 printf("** Phase 3 - Check Connectivity\n");
483 * 4: scan inodes looking for disconnected files; check reference counts
486 printf("** Phase 4 - Check Reference Counts\n");
491 * 5: check and repair resource counts in cylinder groups
494 printf("** Phase 5 - Check Cyl groups\n");
499 * print out summary statistics
501 n_ffree = sblock.fs_cstotal.cs_nffree;
502 n_bfree = sblock.fs_cstotal.cs_nbfree;
503 files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
505 sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
506 blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
507 blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
508 blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
509 if (bkgrdflag && (files > 0 || blks > 0)) {
510 countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
511 pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n",
512 countdirs, files - countdirs, blks);
514 pwarn("%ld files, %jd used, %ju free ",
515 (long)n_files, (intmax_t)n_blks,
516 (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
517 printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
518 (uintmax_t)n_ffree, (uintmax_t)n_bfree,
519 n_ffree * 100.0 / sblock.fs_dsize);
522 printf("%jd inodes missing\n", -files);
524 printf("%jd blocks missing\n", -blks);
525 if (duplist != NULL) {
526 printf("The following duplicate blocks remain:");
527 for (dp = duplist; dp; dp = dp->next)
528 printf(" %jd,", (intmax_t)dp->dup);
532 duplist = (struct dups *)0;
533 muldup = (struct dups *)0;
536 sblock.fs_time = time(NULL);
539 if (cvtlevel && sblk.b_dirty) {
541 * Write out the duplicate super blocks
543 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
544 blwrite(fswritefd, (char *)&sblock,
545 fsbtodb(&sblock, cgsblock(&sblock, cylno)),
553 * Check to see if the file system is mounted read-write.
555 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
559 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
560 if (inostathead[cylno].il_stat != NULL)
561 free((char *)inostathead[cylno].il_stat);
562 free((char *)inostathead);
564 if (fsmodified && !preen)
565 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
567 if (wantrestart && (restarts++ < 10) &&
568 (preen || reply("RESTART")))
570 printf("\n***** PLEASE RERUN FSCK *****\n");
572 if (chkdoreload(mntp) != 0) {
576 printf("\n***** REBOOT NOW *****\n");
584 chkdoreload(struct statfs *mntp)
597 * We modified a mounted file system. Do a mount update on
598 * it unless it is read-write, so we can continue using it
599 * as safely as possible.
601 if (mntp->f_flags & MNT_RDONLY) {
602 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
603 build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
605 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
607 build_iovec(&iov, &iovlen, "errmsg", errmsg,
609 build_iovec(&iov, &iovlen, "update", NULL, 0);
610 build_iovec(&iov, &iovlen, "reload", NULL, 0);
612 * XX: We need the following line until we clean up
613 * nmount parsing of root mounts and NFS root mounts.
615 build_iovec(&iov, &iovlen, "ro", NULL, 0);
616 if (nmount(iov, iovlen, mntp->f_flags) == 0) {
619 pwarn("mount reload of '%s' failed: %s %s\n\n",
620 mntp->f_mntonname, strerror(errno), errmsg);
627 * Get the mount point information for name.
629 static struct statfs *
630 getmntpt(const char *name)
632 struct stat devstat, mntdevstat;
633 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
635 struct statfs *mntbuf, *statfsp;
636 int i, mntsize, isdev;
638 if (stat(name, &devstat) != 0)
640 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
644 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
645 for (i = 0; i < mntsize; i++) {
646 statfsp = &mntbuf[i];
647 ddevname = statfsp->f_mntfromname;
648 if (*ddevname != '/') {
649 if (strlen(_PATH_DEV) + strlen(ddevname) + 1 >
650 sizeof(statfsp->f_mntfromname))
652 strcpy(device, _PATH_DEV);
653 strcat(device, ddevname);
654 strcpy(statfsp->f_mntfromname, device);
657 if (strcmp(name, statfsp->f_mntonname))
661 if (stat(ddevname, &mntdevstat) == 0 &&
662 mntdevstat.st_rdev == devstat.st_rdev)
672 (void) fprintf(stderr,
673 "usage: %s [-BEFfnpry] [-b block] [-c level] [-m mode] filesystem ...\n",
679 infohandler(int sig __unused)
685 alarmhandler(int sig __unused)