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>
68 static void usage(void) __dead2;
69 static int argtoi(int flag, const char *req, const char *str, int base);
70 static int checkfilesys(char *filesys);
71 static int chkdoreload(struct statfs *mntp);
72 static struct statfs *getmntpt(const char *);
75 main(int argc, char *argv[])
79 struct itimerval itimerval;
85 while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:nprSyZ")) != -1) {
89 bflag = argtoi('b', "number", optarg, 10);
90 printf("Alternate super block location: %d\n", bflag);
99 cvtlevel = argtoi('c', "conversion level", optarg, 10);
101 errx(EEXIT, "cannot do level %d conversion",
122 lfmode = argtoi('m', "mode", optarg, 8);
124 errx(EEXIT, "bad mode to -m: %o", lfmode);
125 printf("** lost+found creation mode %o\n", lfmode);
168 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
169 (void)signal(SIGINT, catch);
171 (void)signal(SIGQUIT, catchquit);
172 signal(SIGINFO, infohandler);
174 signal(SIGALRM, alarmhandler);
175 itimerval.it_interval.tv_sec = 5;
176 itimerval.it_interval.tv_usec = 0;
177 itimerval.it_value.tv_sec = 5;
178 itimerval.it_value.tv_usec = 0;
179 setitimer(ITIMER_REAL, &itimerval, NULL);
182 * Push up our allowed memory limit so we can cope
183 * with huge file systems.
185 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
186 rlimit.rlim_cur = rlimit.rlim_max;
187 (void)setrlimit(RLIMIT_DATA, &rlimit);
190 (void)checkfilesys(*argv++);
198 argtoi(int flag, const char *req, const char *str, int base)
203 ret = (int)strtol(str, &cp, base);
204 if (cp == str || *cp)
205 errx(EEXIT, "-%c flag requires a %s", flag, req);
210 * Check the specified file system.
214 checkfilesys(char *filesys)
216 ufs2_daddr_t n_ffree, n_bfree;
225 intmax_t blks, files;
233 if (debug && ckclean)
236 * Make best effort to get the disk name. Check first to see
237 * if it is listed among the mounted file systems. Failing that
238 * check to see if it is listed in /etc/fstab.
240 mntp = getmntpt(filesys);
242 filesys = mntp->f_mntfromname;
244 filesys = blockcheck(filesys);
246 * If -F flag specified, check to see whether a background check
247 * is possible and needed. If possible and needed, exit with
248 * status zero. Otherwise exit with status non-zero. A non-zero
249 * exit status will cause a foreground check to be run.
253 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
254 exit(3); /* Cannot read superblock */
256 /* Earlier background failed or journaled */
257 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
259 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
260 exit(5); /* Not running soft updates */
262 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
263 exit(6); /* Lacks kernel support */
264 if ((mntp == NULL && sblock.fs_clean == 1) ||
265 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
266 exit(7); /* Filesystem clean, report it now */
269 if (ckclean && skipclean) {
271 * If file system is gjournaled, check it here.
273 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
274 exit(3); /* Cannot read superblock */
276 if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
277 //printf("GJournaled file system detected on %s.\n",
279 if (sblock.fs_clean == 1) {
280 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
283 if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
284 gjournal_check(filesys);
285 if (chkdoreload(mntp) == 0)
290 "UNEXPECTED INCONSISTENCY, CANNOT RUN FAST FSCK\n");
295 * If we are to do a background check:
296 * Get the mount point information of the file system
297 * create snapshot file
298 * return created snapshot file
299 * if not found, clear bkgrdflag and proceed with normal fsck
304 pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
305 } else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
308 "NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
309 } else if ((mntp->f_flags & MNT_RDONLY) != 0) {
311 pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
312 } else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
313 if (readsb(0) != 0) {
314 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) {
317 "UNEXPECTED INCONSISTENCY, CANNOT RUN IN BACKGROUND\n");
319 if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
320 skipclean && ckclean) {
322 * file system is clean;
323 * skip snapshot and report it clean
326 "FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
333 snprintf(snapname, sizeof snapname, "%s/.snap",
335 if (stat(snapname, &snapdir) < 0) {
336 if (errno != ENOENT) {
339 "CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
340 snapname, strerror(errno));
341 } else if ((grp = getgrnam("operator")) == 0 ||
342 mkdir(snapname, 0770) < 0 ||
343 chown(snapname, -1, grp->gr_gid) < 0 ||
344 chmod(snapname, 0770) < 0) {
347 "CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
348 snapname, strerror(errno));
350 } else if (!S_ISDIR(snapdir.st_mode)) {
353 "%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
358 snprintf(snapname, sizeof snapname,
359 "%s/.snap/fsck_snapshot", mntp->f_mntonname);
360 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
361 build_iovec(&iov, &iovlen, "from", snapname,
363 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
365 build_iovec(&iov, &iovlen, "errmsg", errmsg,
367 build_iovec(&iov, &iovlen, "update", NULL, 0);
368 build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
370 while (nmount(iov, iovlen, mntp->f_flags) < 0) {
371 if (errno == EEXIST && unlink(snapname) == 0)
374 pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n",
375 snapname, strerror(errno), errmsg);
383 switch (setup(filesys)) {
386 pfatal("CAN'T CHECK FILE SYSTEM.");
390 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
391 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
392 printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
393 (intmax_t)sblock.fs_cstotal.cs_nffree,
394 (intmax_t)sblock.fs_cstotal.cs_nbfree,
395 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
399 * Determine if we can and should do journal recovery.
401 if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
402 if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
403 if (preen || reply("USE JOURNAL")) {
404 if (suj_check(filesys) == 0) {
405 printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
406 if (chkdoreload(mntp) == 0)
411 printf("** Skipping journal, falling through to full fsck\n\n");
414 * Write the superblock so we don't try to recover the
415 * journal on another pass.
417 sblock.fs_mtime = time(NULL);
422 * Cleared if any questions answered no. Used to decide if
423 * the superblock should be marked clean.
427 * 1: scan inodes tallying blocks used
430 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
431 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
432 printf("** Root file system\n");
433 printf("** Phase 1 - Check Blocks and Sizes\n");
435 clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
440 * 1b: locate first references to duplicates, if any
443 if (preen || usedsoftdep)
444 pfatal("INTERNAL ERROR: dups with %s%s%s",
446 (preen && usedsoftdep) ? " and " : "",
447 usedsoftdep ? "softupdates" : "");
448 printf("** Phase 1b - Rescan For More DUPS\n");
454 * 2: traverse directories from root to mark all connected directories
457 printf("** Phase 2 - Check Pathnames\n");
462 * 3: scan inodes looking for disconnected directories
465 printf("** Phase 3 - Check Connectivity\n");
470 * 4: scan inodes looking for disconnected files; check reference counts
473 printf("** Phase 4 - Check Reference Counts\n");
478 * 5: check and repair resource counts in cylinder groups
481 printf("** Phase 5 - Check Cyl groups\n");
486 * print out summary statistics
488 n_ffree = sblock.fs_cstotal.cs_nffree;
489 n_bfree = sblock.fs_cstotal.cs_nbfree;
490 files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
492 sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
493 blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
494 blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
495 blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
496 if (bkgrdflag && (files > 0 || blks > 0)) {
497 countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
498 pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n",
499 countdirs, files - countdirs, blks);
501 pwarn("%ld files, %jd used, %ju free ",
502 (long)n_files, (intmax_t)n_blks,
503 (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
504 printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
505 (uintmax_t)n_ffree, (uintmax_t)n_bfree,
506 n_ffree * 100.0 / sblock.fs_dsize);
509 printf("%jd inodes missing\n", -files);
511 printf("%jd blocks missing\n", -blks);
512 if (duplist != NULL) {
513 printf("The following duplicate blocks remain:");
514 for (dp = duplist; dp; dp = dp->next)
515 printf(" %jd,", (intmax_t)dp->dup);
519 duplist = (struct dups *)0;
520 muldup = (struct dups *)0;
523 sblock.fs_time = time(NULL);
526 if (cvtlevel && sblk.b_dirty) {
528 * Write out the duplicate super blocks
530 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
531 blwrite(fswritefd, (char *)&sblock,
532 fsbtodb(&sblock, cgsblock(&sblock, cylno)),
540 * Check to see if the file system is mounted read-write.
542 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
546 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
547 if (inostathead[cylno].il_stat != NULL)
548 free((char *)inostathead[cylno].il_stat);
549 free((char *)inostathead);
551 if (fsmodified && !preen)
552 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
554 printf("\n***** PLEASE RERUN FSCK *****\n");
555 if (chkdoreload(mntp) != 0) {
559 printf("\n***** REBOOT NOW *****\n");
567 chkdoreload(struct statfs *mntp)
580 * We modified a mounted file system. Do a mount update on
581 * it unless it is read-write, so we can continue using it
582 * as safely as possible.
584 if (mntp->f_flags & MNT_RDONLY) {
585 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
586 build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
588 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
590 build_iovec(&iov, &iovlen, "errmsg", errmsg,
592 build_iovec(&iov, &iovlen, "update", NULL, 0);
593 build_iovec(&iov, &iovlen, "reload", NULL, 0);
595 * XX: We need the following line until we clean up
596 * nmount parsing of root mounts and NFS root mounts.
598 build_iovec(&iov, &iovlen, "ro", NULL, 0);
599 if (nmount(iov, iovlen, mntp->f_flags) == 0) {
602 pwarn("mount reload of '%s' failed: %s %s\n\n",
603 mntp->f_mntonname, strerror(errno), errmsg);
610 * Get the mount point information for name.
612 static struct statfs *
613 getmntpt(const char *name)
615 struct stat devstat, mntdevstat;
616 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
618 struct statfs *mntbuf, *statfsp;
619 int i, mntsize, isdev;
621 if (stat(name, &devstat) != 0)
623 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
627 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
628 for (i = 0; i < mntsize; i++) {
629 statfsp = &mntbuf[i];
630 ddevname = statfsp->f_mntfromname;
631 if (*ddevname != '/') {
632 strcpy(device, _PATH_DEV);
633 strcat(device, ddevname);
634 strcpy(statfsp->f_mntfromname, device);
637 if (strcmp(name, statfsp->f_mntonname))
641 if (stat(ddevname, &mntdevstat) == 0 &&
642 mntdevstat.st_rdev == devstat.st_rdev)
652 (void) fprintf(stderr,
653 "usage: %s [-BEFfnpry] [-b block] [-c level] [-m mode] filesystem ...\n",