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>
48 #include <sys/mount.h>
49 #include <sys/resource.h>
50 #include <sys/sysctl.h>
51 #include <sys/disklabel.h>
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ufs/ufsmount.h>
55 #include <ufs/ffs/fs.h>
67 static void usage(void) __dead2;
68 static int argtoi(int flag, const char *req, const char *str, int base);
69 static int checkfilesys(char *filesys);
70 static struct statfs *getmntpt(const char *);
73 main(int argc, char *argv[])
77 struct itimerval itimerval;
82 while ((ch = getopt(argc, argv, "b:Bc:dfFm:npy")) != -1) {
86 bflag = argtoi('b', "number", optarg, 10);
87 printf("Alternate super block location: %d\n", bflag);
96 cvtlevel = argtoi('c', "conversion level", optarg, 10);
98 errx(EEXIT, "cannot do level %d conversion",
115 lfmode = argtoi('m', "mode", optarg, 8);
117 errx(EEXIT, "bad mode to -m: %o", lfmode);
118 printf("** lost+found creation mode %o\n", lfmode);
145 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
146 (void)signal(SIGINT, catch);
148 (void)signal(SIGQUIT, catchquit);
149 signal(SIGINFO, infohandler);
151 signal(SIGALRM, alarmhandler);
152 itimerval.it_interval.tv_sec = 5;
153 itimerval.it_interval.tv_usec = 0;
154 itimerval.it_value.tv_sec = 5;
155 itimerval.it_value.tv_usec = 0;
156 setitimer(ITIMER_REAL, &itimerval, NULL);
159 * Push up our allowed memory limit so we can cope
160 * with huge file systems.
162 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
163 rlimit.rlim_cur = rlimit.rlim_max;
164 (void)setrlimit(RLIMIT_DATA, &rlimit);
167 (void)checkfilesys(*argv++);
175 argtoi(int flag, const char *req, const char *str, int base)
180 ret = (int)strtol(str, &cp, base);
181 if (cp == str || *cp)
182 errx(EEXIT, "-%c flag requires a %s", flag, req);
187 * Check the specified file system.
191 checkfilesys(char *filesys)
193 ufs2_daddr_t n_ffree, n_bfree;
194 struct ufs_args args;
208 * Make best effort to get the disk name. Check first to see
209 * if it is listed among the mounted file systems. Failing that
210 * check to see if it is listed in /etc/fstab.
212 mntp = getmntpt(filesys);
214 filesys = mntp->f_mntfromname;
216 filesys = blockcheck(filesys);
218 * If -F flag specified, check to see whether a background check
219 * is possible and needed. If possible and needed, exit with
220 * status zero. Otherwise exit with status non-zero. A non-zero
221 * exit status will cause a foreground check to be run.
225 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
226 exit(3); /* Cannot read superblock */
228 if (sblock.fs_flags & FS_NEEDSFSCK)
229 exit(4); /* Earlier background failed */
230 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
231 exit(5); /* Not running soft updates */
233 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
234 exit(6); /* Lacks kernel support */
235 if ((mntp == NULL && sblock.fs_clean == 1) ||
236 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
237 exit(7); /* Filesystem clean, report it now */
240 if (preen && skipclean) {
242 * If file system is gjournaled, check it here.
244 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
245 exit(3); /* Cannot read superblock */
247 if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
248 //printf("GJournaled file system detected on %s.\n",
250 if (sblock.fs_clean == 1) {
251 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
254 if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
255 gjournal_check(filesys);
258 pfatal("UNEXPECTED INCONSISTENCY, %s\n",
259 "CANNOT RUN FAST FSCK\n");
264 * If we are to do a background check:
265 * Get the mount point information of the file system
266 * create snapshot file
267 * return created snapshot file
268 * if not found, clear bkgrdflag and proceed with normal fsck
273 pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
274 } else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
276 pfatal("NOT USING SOFT UPDATES, %s\n",
277 "CANNOT RUN IN BACKGROUND");
278 } else if ((mntp->f_flags & MNT_RDONLY) != 0) {
280 pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
281 } else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
282 if (readsb(0) != 0) {
283 if (sblock.fs_flags & FS_NEEDSFSCK) {
285 pfatal("UNEXPECTED INCONSISTENCY, %s\n",
286 "CANNOT RUN IN BACKGROUND\n");
288 if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
289 skipclean && preen) {
291 * file system is clean;
292 * skip snapshot and report it clean
294 pwarn("FILE SYSTEM CLEAN; %s\n",
302 snprintf(snapname, sizeof snapname, "%s/.snap",
304 if (stat(snapname, &snapdir) < 0) {
305 if (errno != ENOENT) {
307 pfatal("CANNOT FIND %s %s: %s, %s\n",
308 "SNAPSHOT DIRECTORY",
309 snapname, strerror(errno),
310 "CANNOT RUN IN BACKGROUND");
311 } else if ((grp = getgrnam("operator")) == 0 ||
312 mkdir(snapname, 0770) < 0 ||
313 chown(snapname, -1, grp->gr_gid) < 0 ||
314 chmod(snapname, 0770) < 0) {
316 pfatal("CANNOT CREATE %s %s: %s, %s\n",
317 "SNAPSHOT DIRECTORY",
318 snapname, strerror(errno),
319 "CANNOT RUN IN BACKGROUND");
321 } else if (!S_ISDIR(snapdir.st_mode)) {
323 pfatal("%s IS NOT A DIRECTORY, %s\n", snapname,
324 "CANNOT RUN IN BACKGROUND");
328 snprintf(snapname, sizeof snapname,
329 "%s/.snap/fsck_snapshot", mntp->f_mntonname);
330 memset(&args, 0, sizeof args);
331 args.fspec = snapname;
332 while (mount("ffs", mntp->f_mntonname,
333 mntp->f_flags | MNT_UPDATE | MNT_SNAPSHOT,
335 if (errno == EEXIST && unlink(snapname) == 0)
338 pfatal("CANNOT CREATE SNAPSHOT %s: %s\n",
339 snapname, strerror(errno));
347 switch (setup(filesys)) {
350 pfatal("CAN'T CHECK FILE SYSTEM.");
354 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
355 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
356 printf("(%lld frags, %lld blocks, %.1f%% fragmentation)\n",
357 (long long)sblock.fs_cstotal.cs_nffree,
358 (long long)sblock.fs_cstotal.cs_nbfree,
359 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
364 * Cleared if any questions answered no. Used to decide if
365 * the superblock should be marked clean.
369 * 1: scan inodes tallying blocks used
372 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
373 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
374 printf("** Root file system\n");
375 printf("** Phase 1 - Check Blocks and Sizes\n");
380 * 1b: locate first references to duplicates, if any
383 if (preen || usedsoftdep)
384 pfatal("INTERNAL ERROR: dups with -p");
385 printf("** Phase 1b - Rescan For More DUPS\n");
390 * 2: traverse directories from root to mark all connected directories
393 printf("** Phase 2 - Check Pathnames\n");
397 * 3: scan inodes looking for disconnected directories
400 printf("** Phase 3 - Check Connectivity\n");
404 * 4: scan inodes looking for disconnected files; check reference counts
407 printf("** Phase 4 - Check Reference Counts\n");
411 * 5: check and repair resource counts in cylinder groups
414 printf("** Phase 5 - Check Cyl groups\n");
418 * print out summary statistics
420 n_ffree = sblock.fs_cstotal.cs_nffree;
421 n_bfree = sblock.fs_cstotal.cs_nbfree;
422 files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
424 sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
425 blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
426 blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
427 blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
428 if (bkgrdflag && (files > 0 || blks > 0)) {
429 countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
430 pwarn("Reclaimed: %ld directories, %ld files, %lld fragments\n",
431 countdirs, (long)files - countdirs, (long long)blks);
433 pwarn("%ld files, %jd used, %ju free ",
434 (long)n_files, (intmax_t)n_blks,
435 (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
436 printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
437 (uintmax_t)n_ffree, (uintmax_t)n_bfree,
438 n_ffree * 100.0 / sblock.fs_dsize);
441 printf("%d inodes missing\n", -files);
443 printf("%lld blocks missing\n", -(long long)blks);
444 if (duplist != NULL) {
445 printf("The following duplicate blocks remain:");
446 for (dp = duplist; dp; dp = dp->next)
447 printf(" %lld,", (long long)dp->dup);
451 duplist = (struct dups *)0;
452 muldup = (struct dups *)0;
455 sblock.fs_time = time(NULL);
458 if (cvtlevel && sblk.b_dirty) {
460 * Write out the duplicate super blocks
462 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
463 blwrite(fswritefd, (char *)&sblock,
464 fsbtodb(&sblock, cgsblock(&sblock, cylno)),
471 * Check to see if the file system is mounted read-write.
473 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
477 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
478 if (inostathead[cylno].il_stat != NULL)
479 free((char *)inostathead[cylno].il_stat);
480 free((char *)inostathead);
482 if (fsmodified && !preen)
483 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
485 printf("\n***** PLEASE RERUN FSCK *****\n");
488 * We modified a mounted file system. Do a mount update on
489 * it unless it is read-write, so we can continue using it
490 * as safely as possible.
492 if (mntp->f_flags & MNT_RDONLY) {
494 args.export.ex_flags = 0;
495 args.export.ex_root = 0;
496 ret = mount("ufs", mntp->f_mntonname,
497 mntp->f_flags | MNT_UPDATE | MNT_RELOAD, &args);
500 pwarn("mount reload of '%s' failed: %s\n\n",
501 mntp->f_mntonname, strerror(errno));
506 printf("\n***** REBOOT NOW *****\n");
514 * Get the mount point information for name.
516 static struct statfs *
517 getmntpt(const char *name)
519 struct stat devstat, mntdevstat;
520 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
522 struct statfs *mntbuf, *statfsp;
523 int i, mntsize, isdev;
525 if (stat(name, &devstat) != 0)
527 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
531 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
532 for (i = 0; i < mntsize; i++) {
533 statfsp = &mntbuf[i];
534 ddevname = statfsp->f_mntfromname;
535 if (*ddevname != '/') {
536 strcpy(device, _PATH_DEV);
537 strcat(device, ddevname);
538 strcpy(statfsp->f_mntfromname, device);
541 if (strcmp(name, statfsp->f_mntonname))
545 if (stat(ddevname, &mntdevstat) == 0 &&
546 mntdevstat.st_rdev == devstat.st_rdev)
556 (void) fprintf(stderr,
557 "usage: %s [-BFpfny] [-b block] [-c level] [-m mode] "