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>
70 static void usage(void) __dead2;
71 static int argtoi(int flag, const char *req, const char *str, int base);
72 static int checkfilesys(char *filesys);
73 static int chkdoreload(struct statfs *mntp);
74 static struct statfs *getmntpt(const char *);
77 main(int argc, char *argv[])
81 struct itimerval itimerval;
87 while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:npRrSyZ")) != -1) {
91 bflag = argtoi('b', "number", optarg, 10);
92 printf("Alternate super block location: %d\n", bflag);
101 cvtlevel = argtoi('c', "conversion level", optarg, 10);
103 errx(EEXIT, "cannot do level %d conversion",
124 lfmode = argtoi('m', "mode", optarg, 8);
126 errx(EEXIT, "bad mode to -m: %o", lfmode);
127 printf("** lost+found creation mode %o\n", lfmode);
173 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
174 (void)signal(SIGINT, catch);
176 (void)signal(SIGQUIT, catchquit);
177 signal(SIGINFO, infohandler);
179 signal(SIGALRM, alarmhandler);
180 itimerval.it_interval.tv_sec = 5;
181 itimerval.it_interval.tv_usec = 0;
182 itimerval.it_value.tv_sec = 5;
183 itimerval.it_value.tv_usec = 0;
184 setitimer(ITIMER_REAL, &itimerval, NULL);
187 * Push up our allowed memory limit so we can cope
188 * with huge file systems.
190 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
191 rlimit.rlim_cur = rlimit.rlim_max;
192 (void)setrlimit(RLIMIT_DATA, &rlimit);
195 if (checkfilesys(*argv) == ERESTART)
207 argtoi(int flag, const char *req, const char *str, int base)
212 ret = (int)strtol(str, &cp, base);
213 if (cp == str || *cp)
214 errx(EEXIT, "-%c flag requires a %s", flag, req);
219 * Check the specified file system.
223 checkfilesys(char *filesys)
225 ufs2_daddr_t n_ffree, n_bfree;
234 intmax_t blks, files;
244 if (debug && ckclean)
247 * Make best effort to get the disk name. Check first to see
248 * if it is listed among the mounted file systems. Failing that
249 * check to see if it is listed in /etc/fstab.
251 mntp = getmntpt(filesys);
253 filesys = mntp->f_mntfromname;
255 filesys = blockcheck(filesys);
257 * If -F flag specified, check to see whether a background check
258 * is possible and needed. If possible and needed, exit with
259 * status zero. Otherwise exit with status non-zero. A non-zero
260 * exit status will cause a foreground check to be run.
264 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
265 exit(3); /* Cannot read superblock */
267 /* Earlier background failed or journaled */
268 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
270 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
271 exit(5); /* Not running soft updates */
273 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
274 exit(6); /* Lacks kernel support */
275 if ((mntp == NULL && sblock.fs_clean == 1) ||
276 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
277 exit(7); /* Filesystem clean, report it now */
280 if (ckclean && skipclean) {
282 * If file system is gjournaled, check it here.
284 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
285 exit(3); /* Cannot read superblock */
287 if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
288 //printf("GJournaled file system detected on %s.\n",
290 if (sblock.fs_clean == 1) {
291 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
294 if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
295 gjournal_check(filesys);
296 if (chkdoreload(mntp) == 0)
301 "UNEXPECTED INCONSISTENCY, CANNOT RUN FAST FSCK\n");
306 * If we are to do a background check:
307 * Get the mount point information of the file system
308 * create snapshot file
309 * return created snapshot file
310 * if not found, clear bkgrdflag and proceed with normal fsck
315 pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
316 } else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
319 "NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
320 } else if ((mntp->f_flags & MNT_RDONLY) != 0) {
322 pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
323 } else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
324 if (readsb(0) != 0) {
325 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) {
328 "UNEXPECTED INCONSISTENCY, CANNOT RUN IN BACKGROUND\n");
330 if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
331 skipclean && ckclean) {
333 * file system is clean;
334 * skip snapshot and report it clean
337 "FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
344 snprintf(snapname, sizeof snapname, "%s/.snap",
346 if (stat(snapname, &snapdir) < 0) {
347 if (errno != ENOENT) {
350 "CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
351 snapname, strerror(errno));
352 } else if ((grp = getgrnam("operator")) == 0 ||
353 mkdir(snapname, 0770) < 0 ||
354 chown(snapname, -1, grp->gr_gid) < 0 ||
355 chmod(snapname, 0770) < 0) {
358 "CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
359 snapname, strerror(errno));
361 } else if (!S_ISDIR(snapdir.st_mode)) {
364 "%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
369 snprintf(snapname, sizeof snapname,
370 "%s/.snap/fsck_snapshot", mntp->f_mntonname);
371 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
372 build_iovec(&iov, &iovlen, "from", snapname,
374 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
376 build_iovec(&iov, &iovlen, "errmsg", errmsg,
378 build_iovec(&iov, &iovlen, "update", NULL, 0);
379 build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
381 while (nmount(iov, iovlen, mntp->f_flags) < 0) {
382 if (errno == EEXIST && unlink(snapname) == 0)
385 pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n",
386 snapname, strerror(errno), errmsg);
394 switch (setup(filesys)) {
397 pfatal("CAN'T CHECK FILE SYSTEM.");
401 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
402 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
403 printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
404 (intmax_t)sblock.fs_cstotal.cs_nffree,
405 (intmax_t)sblock.fs_cstotal.cs_nbfree,
406 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
410 * Determine if we can and should do journal recovery.
412 if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
413 if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
414 if (preen || reply("USE JOURNAL")) {
415 if (suj_check(filesys) == 0) {
416 printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
417 if (chkdoreload(mntp) == 0)
422 printf("** Skipping journal, falling through to full fsck\n\n");
425 * Write the superblock so we don't try to recover the
426 * journal on another pass.
428 sblock.fs_mtime = time(NULL);
433 * Cleared if any questions answered no. Used to decide if
434 * the superblock should be marked clean.
438 * 1: scan inodes tallying blocks used
441 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
442 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
443 printf("** Root file system\n");
444 printf("** Phase 1 - Check Blocks and Sizes\n");
446 clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
451 * 1b: locate first references to duplicates, if any
454 if (preen || usedsoftdep)
455 pfatal("INTERNAL ERROR: dups with %s%s%s",
457 (preen && usedsoftdep) ? " and " : "",
458 usedsoftdep ? "softupdates" : "");
459 printf("** Phase 1b - Rescan For More DUPS\n");
465 * 2: traverse directories from root to mark all connected directories
468 printf("** Phase 2 - Check Pathnames\n");
473 * 3: scan inodes looking for disconnected directories
476 printf("** Phase 3 - Check Connectivity\n");
481 * 4: scan inodes looking for disconnected files; check reference counts
484 printf("** Phase 4 - Check Reference Counts\n");
489 * 5: check and repair resource counts in cylinder groups
492 printf("** Phase 5 - Check Cyl groups\n");
497 * print out summary statistics
499 n_ffree = sblock.fs_cstotal.cs_nffree;
500 n_bfree = sblock.fs_cstotal.cs_nbfree;
501 files = maxino - 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_dirty) {
539 * Write out the duplicate super blocks
541 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
542 blwrite(fswritefd, (char *)&sblock,
543 fsbtodb(&sblock, cgsblock(&sblock, cylno)),
551 * Check to see if the file system is mounted read-write.
553 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
557 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
558 if (inostathead[cylno].il_stat != NULL)
559 free((char *)inostathead[cylno].il_stat);
560 free((char *)inostathead);
562 if (fsmodified && !preen)
563 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
565 if (wantrestart && (restarts++ < 10) &&
566 (preen || reply("RESTART")))
568 printf("\n***** PLEASE RERUN FSCK *****\n");
570 if (chkdoreload(mntp) != 0) {
574 printf("\n***** REBOOT NOW *****\n");
582 chkdoreload(struct statfs *mntp)
595 * We modified a mounted file system. Do a mount update on
596 * it unless it is read-write, so we can continue using it
597 * as safely as possible.
599 if (mntp->f_flags & MNT_RDONLY) {
600 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
601 build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
603 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
605 build_iovec(&iov, &iovlen, "errmsg", errmsg,
607 build_iovec(&iov, &iovlen, "update", NULL, 0);
608 build_iovec(&iov, &iovlen, "reload", NULL, 0);
610 * XX: We need the following line until we clean up
611 * nmount parsing of root mounts and NFS root mounts.
613 build_iovec(&iov, &iovlen, "ro", NULL, 0);
614 if (nmount(iov, iovlen, mntp->f_flags) == 0) {
617 pwarn("mount reload of '%s' failed: %s %s\n\n",
618 mntp->f_mntonname, strerror(errno), errmsg);
625 * Get the mount point information for name.
627 static struct statfs *
628 getmntpt(const char *name)
630 struct stat devstat, mntdevstat;
631 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
633 struct statfs *mntbuf, *statfsp;
634 int i, mntsize, isdev;
636 if (stat(name, &devstat) != 0)
638 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
642 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
643 for (i = 0; i < mntsize; i++) {
644 statfsp = &mntbuf[i];
645 ddevname = statfsp->f_mntfromname;
646 if (*ddevname != '/') {
647 strcpy(device, _PATH_DEV);
648 strcat(device, ddevname);
649 strcpy(statfsp->f_mntfromname, device);
652 if (strcmp(name, statfsp->f_mntonname))
656 if (stat(ddevname, &mntdevstat) == 0 &&
657 mntdevstat.st_rdev == devstat.st_rdev)
667 (void) fprintf(stderr,
668 "usage: %s [-BEFfnpry] [-b block] [-c level] [-m mode] filesystem ...\n",
674 infohandler(int sig __unused)
680 alarmhandler(int sig __unused)