xref: /freebsd/sbin/fsck_ffs/main.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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  * 3. 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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #if 0
31 #ifndef lint
32 static const char copyright[] =
33 "@(#) Copyright (c) 1980, 1986, 1993\n\
34 	The Regents of the University of California.  All rights reserved.\n";
35 #endif /* not lint */
36 
37 #ifndef lint
38 static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
39 #endif /* not lint */
40 #endif
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/param.h>
45 #include <sys/file.h>
46 #include <sys/mount.h>
47 #include <sys/resource.h>
48 #include <sys/stat.h>
49 #include <sys/sysctl.h>
50 #include <sys/uio.h>
51 #include <sys/disklabel.h>
52 
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ffs/fs.h>
55 
56 #include <err.h>
57 #include <errno.h>
58 #include <fstab.h>
59 #include <grp.h>
60 #include <inttypes.h>
61 #include <mntopts.h>
62 #include <paths.h>
63 #include <stdint.h>
64 #include <string.h>
65 #include <time.h>
66 
67 #include "fsck.h"
68 
69 int	restarts;
70 
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 *);
76 
77 int
78 main(int argc, char *argv[])
79 {
80 	int ch;
81 	struct rlimit rlimit;
82 	struct itimerval itimerval;
83 	int ret = 0;
84 
85 	sync();
86 	skipclean = 1;
87 	inoopt = 0;
88 	while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:npRrSyZ")) != -1) {
89 		switch (ch) {
90 		case 'b':
91 			skipclean = 0;
92 			bflag = argtoimax('b', "number", optarg, 10);
93 			printf("Alternate super block location: %jd\n", bflag);
94 			break;
95 
96 		case 'B':
97 			bkgrdflag = 1;
98 			break;
99 
100 		case 'c':
101 			skipclean = 0;
102 			cvtlevel = argtoimax('c', "conversion level", optarg,
103 			    10);
104 			if (cvtlevel < 3)
105 				errx(EEXIT, "cannot do level %d conversion",
106 				    cvtlevel);
107 			break;
108 
109 		case 'd':
110 			debug++;
111 			break;
112 
113 		case 'E':
114 			Eflag++;
115 			break;
116 
117 		case 'f':
118 			skipclean = 0;
119 			break;
120 
121 		case 'F':
122 			bkgrdcheck = 1;
123 			break;
124 
125 		case 'm':
126 			lfmode = argtoimax('m', "mode", optarg, 8);
127 			if (lfmode &~ 07777)
128 				errx(EEXIT, "bad mode to -m: %o", lfmode);
129 			printf("** lost+found creation mode %o\n", lfmode);
130 			break;
131 
132 		case 'n':
133 			nflag++;
134 			yflag = 0;
135 			break;
136 
137 		case 'p':
138 			preen++;
139 			/*FALLTHROUGH*/
140 
141 		case 'C':
142 			ckclean++;
143 			break;
144 
145 		case 'R':
146 			wantrestart = 1;
147 			break;
148 		case 'r':
149 			inoopt++;
150 			break;
151 
152 		case 'S':
153 			surrender = 1;
154 			break;
155 
156 		case 'y':
157 			yflag++;
158 			nflag = 0;
159 			break;
160 
161 		case 'Z':
162 			Zflag++;
163 			break;
164 
165 		default:
166 			usage();
167 		}
168 	}
169 	argc -= optind;
170 	argv += optind;
171 
172 	if (!argc)
173 		usage();
174 
175 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
176 		(void)signal(SIGINT, catch);
177 	if (ckclean)
178 		(void)signal(SIGQUIT, catchquit);
179 	signal(SIGINFO, infohandler);
180 	if (bkgrdflag) {
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);
187 	}
188 	/*
189 	 * Push up our allowed memory limit so we can cope
190 	 * with huge file systems.
191 	 */
192 	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
193 		rlimit.rlim_cur = rlimit.rlim_max;
194 		(void)setrlimit(RLIMIT_DATA, &rlimit);
195 	}
196 	while (argc > 0) {
197 		if (checkfilesys(*argv) == ERESTART)
198 			continue;
199 		argc--;
200 		argv++;
201 	}
202 
203 	if (returntosingle)
204 		ret = 2;
205 	exit(ret);
206 }
207 
208 static intmax_t
209 argtoimax(int flag, const char *req, const char *str, int base)
210 {
211 	char *cp;
212 	intmax_t ret;
213 
214 	ret = strtoimax(str, &cp, base);
215 	if (cp == str || *cp)
216 		errx(EEXIT, "-%c flag requires a %s", flag, req);
217 	return (ret);
218 }
219 
220 /*
221  * Check the specified file system.
222  */
223 /* ARGSUSED */
224 static int
225 checkfilesys(char *filesys)
226 {
227 	ufs2_daddr_t n_ffree, n_bfree;
228 	struct dups *dp;
229 	struct statfs *mntp;
230 	struct stat snapdir;
231 	struct group *grp;
232 	struct iovec *iov;
233 	char errmsg[255];
234 	int iovlen;
235 	int cylno;
236 	intmax_t blks, files;
237 	size_t size;
238 
239 	iov = NULL;
240 	iovlen = 0;
241 	errmsg[0] = '\0';
242 	fsutilinit();
243 	fsckinit();
244 
245 	cdevname = filesys;
246 	if (debug && ckclean)
247 		pwarn("starting\n");
248 	/*
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.
252 	 */
253 	mntp = getmntpt(filesys);
254 	if (mntp != NULL)
255 		filesys = mntp->f_mntfromname;
256 	else
257 		filesys = blockcheck(filesys);
258 	/*
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.
263 	 */
264 	sblock_init();
265 	if (bkgrdcheck) {
266 		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
267 			exit(3);	/* Cannot read superblock */
268 		close(fsreadfd);
269 		/* Earlier background failed or journaled */
270 		if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
271 			exit(4);
272 		if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
273 			exit(5);	/* Not running soft updates */
274 		size = MIBSIZE;
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 */
280 		exit(0);
281 	}
282 	if (ckclean && skipclean) {
283 		/*
284 		 * If file system is gjournaled, check it here.
285 		 */
286 		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
287 			exit(3);	/* Cannot read superblock */
288 		close(fsreadfd);
289 		if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
290 			//printf("GJournaled file system detected on %s.\n",
291 			//    filesys);
292 			if (sblock.fs_clean == 1) {
293 				pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
294 				exit(0);
295 			}
296 			if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
297 				gjournal_check(filesys);
298 				if (chkdoreload(mntp) == 0)
299 					exit(0);
300 				exit(4);
301 			} else {
302 				pfatal(
303 			    "UNEXPECTED INCONSISTENCY, CANNOT RUN FAST FSCK\n");
304 			}
305 		}
306 	}
307 	/*
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
313 	 */
314 	if (bkgrdflag) {
315 		if (mntp == NULL) {
316 			bkgrdflag = 0;
317 			pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
318 		} else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
319 			bkgrdflag = 0;
320 			pfatal(
321 			  "NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
322 		} else if ((mntp->f_flags & MNT_RDONLY) != 0) {
323 			bkgrdflag = 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)) {
328 					bkgrdflag = 0;
329 					pfatal(
330 			"UNEXPECTED INCONSISTENCY, CANNOT RUN IN BACKGROUND\n");
331 				}
332 				if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
333 				    skipclean && ckclean) {
334 					/*
335 					 * file system is clean;
336 					 * skip snapshot and report it clean
337 					 */
338 					pwarn(
339 					"FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
340 					goto clean;
341 				}
342 			}
343 			close(fsreadfd);
344 		}
345 		if (bkgrdflag) {
346 			snprintf(snapname, sizeof snapname, "%s/.snap",
347 			    mntp->f_mntonname);
348 			if (stat(snapname, &snapdir) < 0) {
349 				if (errno != ENOENT) {
350 					bkgrdflag = 0;
351 					pfatal(
352 	"CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
353 					    snapname, strerror(errno));
354 				} else if ((grp = getgrnam("operator")) == NULL ||
355 					   mkdir(snapname, 0770) < 0 ||
356 					   chown(snapname, -1, grp->gr_gid) < 0 ||
357 					   chmod(snapname, 0770) < 0) {
358 					bkgrdflag = 0;
359 					pfatal(
360 	"CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
361 					    snapname, strerror(errno));
362 				}
363 			} else if (!S_ISDIR(snapdir.st_mode)) {
364 				bkgrdflag = 0;
365 				pfatal(
366 			"%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
367 				    snapname);
368 			}
369 		}
370 		if (bkgrdflag) {
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,
375 			    (size_t)-1);
376 			build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
377 			    (size_t)-1);
378 			build_iovec(&iov, &iovlen, "errmsg", errmsg,
379 			    sizeof(errmsg));
380 			build_iovec(&iov, &iovlen, "update", NULL, 0);
381 			build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
382 
383 			while (nmount(iov, iovlen, mntp->f_flags) < 0) {
384 				if (errno == EEXIST && unlink(snapname) == 0)
385 					continue;
386 				bkgrdflag = 0;
387 				pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n",
388 				    snapname, strerror(errno), errmsg);
389 				break;
390 			}
391 			if (bkgrdflag != 0)
392 				filesys = snapname;
393 		}
394 	}
395 
396 	switch (setup(filesys)) {
397 	case 0:
398 		if (preen)
399 			pfatal("CAN'T CHECK FILE SYSTEM.");
400 		return (0);
401 	case -1:
402 	clean:
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);
409 		return (0);
410 	}
411 	/*
412 	 * Determine if we can and should do journal recovery.
413 	 */
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)
420 						exit(0);
421 					exit(4);
422 				}
423 			}
424 			printf("** Skipping journal, falling through to full fsck\n\n");
425 		}
426 		/*
427 		 * Write the superblock so we don't try to recover the
428 		 * journal on another pass.
429 		 */
430 		sblock.fs_mtime = time(NULL);
431 		sbdirty();
432 	}
433 
434 	/*
435 	 * Cleared if any questions answered no. Used to decide if
436 	 * the superblock should be marked clean.
437 	 */
438 	resolved = 1;
439 	/*
440 	 * 1: scan inodes tallying blocks used
441 	 */
442 	if (preen == 0) {
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");
447 	}
448 	clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
449 	pass1();
450 	IOstats("Pass1");
451 
452 	/*
453 	 * 1b: locate first references to duplicates, if any
454 	 */
455 	if (duplist) {
456 		if (preen || usedsoftdep)
457 			pfatal("INTERNAL ERROR: dups with %s%s%s",
458 			    preen ? "-p" : "",
459 			    (preen && usedsoftdep) ? " and " : "",
460 			    usedsoftdep ? "softupdates" : "");
461 		printf("** Phase 1b - Rescan For More DUPS\n");
462 		pass1b();
463 		IOstats("Pass1b");
464 	}
465 
466 	/*
467 	 * 2: traverse directories from root to mark all connected directories
468 	 */
469 	if (preen == 0)
470 		printf("** Phase 2 - Check Pathnames\n");
471 	pass2();
472 	IOstats("Pass2");
473 
474 	/*
475 	 * 3: scan inodes looking for disconnected directories
476 	 */
477 	if (preen == 0)
478 		printf("** Phase 3 - Check Connectivity\n");
479 	pass3();
480 	IOstats("Pass3");
481 
482 	/*
483 	 * 4: scan inodes looking for disconnected files; check reference counts
484 	 */
485 	if (preen == 0)
486 		printf("** Phase 4 - Check Reference Counts\n");
487 	pass4();
488 	IOstats("Pass4");
489 
490 	/*
491 	 * 5: check and repair resource counts in cylinder groups
492 	 */
493 	if (preen == 0)
494 		printf("** Phase 5 - Check Cyl groups\n");
495 	pass5();
496 	IOstats("Pass5");
497 
498 	/*
499 	 * print out summary statistics
500 	 */
501 	n_ffree = sblock.fs_cstotal.cs_nffree;
502 	n_bfree = sblock.fs_cstotal.cs_nbfree;
503 	files = maxino - UFS_ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
504 	blks = n_blks +
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);
513 	}
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);
520 	if (debug) {
521 		if (files < 0)
522 			printf("%jd inodes missing\n", -files);
523 		if (blks < 0)
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);
529 			printf("\n");
530 		}
531 	}
532 	duplist = (struct dups *)0;
533 	muldup = (struct dups *)0;
534 	inocleanup();
535 	if (fsmodified) {
536 		sblock.fs_time = time(NULL);
537 		sbdirty();
538 	}
539 	if (cvtlevel && sblk.b_dirty) {
540 		/*
541 		 * Write out the duplicate super blocks
542 		 */
543 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
544 			blwrite(fswritefd, (char *)&sblock,
545 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)),
546 			    SBLOCKSIZE);
547 	}
548 	if (rerun)
549 		resolved = 0;
550 	finalIOstats();
551 
552 	/*
553 	 * Check to see if the file system is mounted read-write.
554 	 */
555 	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
556 		resolved = 0;
557 	ckfini(resolved);
558 
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);
563 	inostathead = NULL;
564 	if (fsmodified && !preen)
565 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
566 	if (rerun) {
567 		if (wantrestart && (restarts++ < 10) &&
568 		    (preen || reply("RESTART")))
569 			return (ERESTART);
570 		printf("\n***** PLEASE RERUN FSCK *****\n");
571 	}
572 	if (chkdoreload(mntp) != 0) {
573 		if (!fsmodified)
574 			return (0);
575 		if (!preen)
576 			printf("\n***** REBOOT NOW *****\n");
577 		sync();
578 		return (4);
579 	}
580 	return (0);
581 }
582 
583 static int
584 chkdoreload(struct statfs *mntp)
585 {
586 	struct iovec *iov;
587 	int iovlen;
588 	char errmsg[255];
589 
590 	if (mntp == NULL)
591 		return (0);
592 
593 	iov = NULL;
594 	iovlen = 0;
595 	errmsg[0] = '\0';
596 	/*
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.
600 	 */
601 	if (mntp->f_flags & MNT_RDONLY) {
602 		build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
603 		build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
604 		    (size_t)-1);
605 		build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
606 		    (size_t)-1);
607 		build_iovec(&iov, &iovlen, "errmsg", errmsg,
608 		    sizeof(errmsg));
609 		build_iovec(&iov, &iovlen, "update", NULL, 0);
610 		build_iovec(&iov, &iovlen, "reload", NULL, 0);
611 		/*
612 		 * XX: We need the following line until we clean up
613 		 * nmount parsing of root mounts and NFS root mounts.
614 		 */
615 		build_iovec(&iov, &iovlen, "ro", NULL, 0);
616 		if (nmount(iov, iovlen, mntp->f_flags) == 0) {
617 			return (0);
618 		}
619 		pwarn("mount reload of '%s' failed: %s %s\n\n",
620 		    mntp->f_mntonname, strerror(errno), errmsg);
621 		return (1);
622 	}
623 	return (0);
624 }
625 
626 /*
627  * Get the mount point information for name.
628  */
629 static struct statfs *
630 getmntpt(const char *name)
631 {
632 	struct stat devstat, mntdevstat;
633 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
634 	char *ddevname;
635 	struct statfs *mntbuf, *statfsp;
636 	int i, mntsize, isdev;
637 
638 	if (stat(name, &devstat) != 0)
639 		return (NULL);
640 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
641 		isdev = 1;
642 	else
643 		isdev = 0;
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))
651 				continue;
652 			strcpy(device, _PATH_DEV);
653 			strcat(device, ddevname);
654 			strcpy(statfsp->f_mntfromname, device);
655 		}
656 		if (isdev == 0) {
657 			if (strcmp(name, statfsp->f_mntonname))
658 				continue;
659 			return (statfsp);
660 		}
661 		if (stat(ddevname, &mntdevstat) == 0 &&
662 		    mntdevstat.st_rdev == devstat.st_rdev)
663 			return (statfsp);
664 	}
665 	statfsp = NULL;
666 	return (statfsp);
667 }
668 
669 static void
670 usage(void)
671 {
672 	(void) fprintf(stderr,
673 "usage: %s [-BCdEFfnpRrSyZ] [-b block] [-c level] [-m mode] filesystem ...\n",
674 	    getprogname());
675 	exit(1);
676 }
677 
678 void
679 infohandler(int sig __unused)
680 {
681 	got_siginfo = 1;
682 }
683 
684 void
685 alarmhandler(int sig __unused)
686 {
687 	got_sigalarm = 1;
688 }
689