xref: /freebsd/sbin/fsck_ffs/main.c (revision a2aef24aa3c8458e4036735dd6928b4ef77294e5)
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 ofsmodified;
235 	int iovlen;
236 	int cylno;
237 	intmax_t blks, files;
238 	size_t size;
239 
240 	iov = NULL;
241 	iovlen = 0;
242 	errmsg[0] = '\0';
243 	fsutilinit();
244 	fsckinit();
245 
246 	cdevname = filesys;
247 	if (debug && ckclean)
248 		pwarn("starting\n");
249 	/*
250 	 * Make best effort to get the disk name. Check first to see
251 	 * if it is listed among the mounted file systems. Failing that
252 	 * check to see if it is listed in /etc/fstab.
253 	 */
254 	mntp = getmntpt(filesys);
255 	if (mntp != NULL)
256 		filesys = mntp->f_mntfromname;
257 	else
258 		filesys = blockcheck(filesys);
259 	/*
260 	 * If -F flag specified, check to see whether a background check
261 	 * is possible and needed. If possible and needed, exit with
262 	 * status zero. Otherwise exit with status non-zero. A non-zero
263 	 * exit status will cause a foreground check to be run.
264 	 */
265 	sblock_init();
266 	if (bkgrdcheck) {
267 		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
268 			exit(3);	/* Cannot read superblock */
269 		close(fsreadfd);
270 		/* Earlier background failed or journaled */
271 		if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
272 			exit(4);
273 		if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
274 			exit(5);	/* Not running soft updates */
275 		size = MIBSIZE;
276 		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
277 			exit(6);	/* Lacks kernel support */
278 		if ((mntp == NULL && sblock.fs_clean == 1) ||
279 		    (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
280 			exit(7);	/* Filesystem clean, report it now */
281 		exit(0);
282 	}
283 	if (ckclean && skipclean) {
284 		/*
285 		 * If file system is gjournaled, check it here.
286 		 */
287 		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
288 			exit(3);	/* Cannot read superblock */
289 		close(fsreadfd);
290 		if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
291 			//printf("GJournaled file system detected on %s.\n",
292 			//    filesys);
293 			if (sblock.fs_clean == 1) {
294 				pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
295 				exit(0);
296 			}
297 			if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
298 				gjournal_check(filesys);
299 				if (chkdoreload(mntp) == 0)
300 					exit(0);
301 				exit(4);
302 			} else {
303 				pfatal(
304 			    "UNEXPECTED INCONSISTENCY, CANNOT RUN FAST FSCK\n");
305 			}
306 		}
307 	}
308 	/*
309 	 * If we are to do a background check:
310 	 *	Get the mount point information of the file system
311 	 *	create snapshot file
312 	 *	return created snapshot file
313 	 *	if not found, clear bkgrdflag and proceed with normal fsck
314 	 */
315 	if (bkgrdflag) {
316 		if (mntp == NULL) {
317 			bkgrdflag = 0;
318 			pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
319 		} else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
320 			bkgrdflag = 0;
321 			pfatal(
322 			  "NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
323 		} else if ((mntp->f_flags & MNT_RDONLY) != 0) {
324 			bkgrdflag = 0;
325 			pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
326 		} else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
327 			if (readsb(0) != 0) {
328 				if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) {
329 					bkgrdflag = 0;
330 					pfatal(
331 			"UNEXPECTED INCONSISTENCY, CANNOT RUN IN BACKGROUND\n");
332 				}
333 				if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
334 				    skipclean && ckclean) {
335 					/*
336 					 * file system is clean;
337 					 * skip snapshot and report it clean
338 					 */
339 					pwarn(
340 					"FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
341 					goto clean;
342 				}
343 			}
344 			close(fsreadfd);
345 		}
346 		if (bkgrdflag) {
347 			snprintf(snapname, sizeof snapname, "%s/.snap",
348 			    mntp->f_mntonname);
349 			if (stat(snapname, &snapdir) < 0) {
350 				if (errno != ENOENT) {
351 					bkgrdflag = 0;
352 					pfatal(
353 	"CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
354 					    snapname, strerror(errno));
355 				} else if ((grp = getgrnam("operator")) == NULL ||
356 					   mkdir(snapname, 0770) < 0 ||
357 					   chown(snapname, -1, grp->gr_gid) < 0 ||
358 					   chmod(snapname, 0770) < 0) {
359 					bkgrdflag = 0;
360 					pfatal(
361 	"CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
362 					    snapname, strerror(errno));
363 				}
364 			} else if (!S_ISDIR(snapdir.st_mode)) {
365 				bkgrdflag = 0;
366 				pfatal(
367 			"%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
368 				    snapname);
369 			}
370 		}
371 		if (bkgrdflag) {
372 			snprintf(snapname, sizeof snapname,
373 			    "%s/.snap/fsck_snapshot", mntp->f_mntonname);
374 			build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
375 			build_iovec(&iov, &iovlen, "from", snapname,
376 			    (size_t)-1);
377 			build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
378 			    (size_t)-1);
379 			build_iovec(&iov, &iovlen, "errmsg", errmsg,
380 			    sizeof(errmsg));
381 			build_iovec(&iov, &iovlen, "update", NULL, 0);
382 			build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
383 
384 			while (nmount(iov, iovlen, mntp->f_flags) < 0) {
385 				if (errno == EEXIST && unlink(snapname) == 0)
386 					continue;
387 				bkgrdflag = 0;
388 				pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n",
389 				    snapname, strerror(errno), errmsg);
390 				break;
391 			}
392 			if (bkgrdflag != 0)
393 				filesys = snapname;
394 		}
395 	}
396 
397 	switch (setup(filesys)) {
398 	case 0:
399 		if (preen)
400 			pfatal("CAN'T CHECK FILE SYSTEM.");
401 		return (0);
402 	case -1:
403 	clean:
404 		pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
405 		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
406 		printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
407 		    (intmax_t)sblock.fs_cstotal.cs_nffree,
408 		    (intmax_t)sblock.fs_cstotal.cs_nbfree,
409 		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
410 		return (0);
411 	}
412 	/*
413 	 * Determine if we can and should do journal recovery.
414 	 */
415 	if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
416 		if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
417 			if (preen || reply("USE JOURNAL")) {
418 				if (suj_check(filesys) == 0) {
419 					printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
420 					if (chkdoreload(mntp) == 0)
421 						exit(0);
422 					exit(4);
423 				}
424 			}
425 			printf("** Skipping journal, falling through to full fsck\n\n");
426 		}
427 		/*
428 		 * Write the superblock so we don't try to recover the
429 		 * journal on another pass. If this is the only change
430 		 * to the filesystem, we do not want it to be called
431 		 * out as modified.
432 		 */
433 		sblock.fs_mtime = time(NULL);
434 		sbdirty();
435 		ofsmodified = fsmodified;
436 		flush(fswritefd, &sblk);
437 		fsmodified = ofsmodified;
438 	}
439 
440 	/*
441 	 * Cleared if any questions answered no. Used to decide if
442 	 * the superblock should be marked clean.
443 	 */
444 	resolved = 1;
445 	/*
446 	 * 1: scan inodes tallying blocks used
447 	 */
448 	if (preen == 0) {
449 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
450 		if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
451 			printf("** Root file system\n");
452 		printf("** Phase 1 - Check Blocks and Sizes\n");
453 	}
454 	clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
455 	pass1();
456 	IOstats("Pass1");
457 
458 	/*
459 	 * 1b: locate first references to duplicates, if any
460 	 */
461 	if (duplist) {
462 		if (preen || usedsoftdep)
463 			pfatal("INTERNAL ERROR: dups with %s%s%s",
464 			    preen ? "-p" : "",
465 			    (preen && usedsoftdep) ? " and " : "",
466 			    usedsoftdep ? "softupdates" : "");
467 		printf("** Phase 1b - Rescan For More DUPS\n");
468 		pass1b();
469 		IOstats("Pass1b");
470 	}
471 
472 	/*
473 	 * 2: traverse directories from root to mark all connected directories
474 	 */
475 	if (preen == 0)
476 		printf("** Phase 2 - Check Pathnames\n");
477 	pass2();
478 	IOstats("Pass2");
479 
480 	/*
481 	 * 3: scan inodes looking for disconnected directories
482 	 */
483 	if (preen == 0)
484 		printf("** Phase 3 - Check Connectivity\n");
485 	pass3();
486 	IOstats("Pass3");
487 
488 	/*
489 	 * 4: scan inodes looking for disconnected files; check reference counts
490 	 */
491 	if (preen == 0)
492 		printf("** Phase 4 - Check Reference Counts\n");
493 	pass4();
494 	IOstats("Pass4");
495 
496 	/*
497 	 * 5: check and repair resource counts in cylinder groups
498 	 */
499 	if (preen == 0)
500 		printf("** Phase 5 - Check Cyl groups\n");
501 	pass5();
502 	IOstats("Pass5");
503 
504 	/*
505 	 * print out summary statistics
506 	 */
507 	n_ffree = sblock.fs_cstotal.cs_nffree;
508 	n_bfree = sblock.fs_cstotal.cs_nbfree;
509 	files = maxino - UFS_ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
510 	blks = n_blks +
511 	    sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
512 	blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
513 	blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
514 	blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
515 	if (bkgrdflag && (files > 0 || blks > 0)) {
516 		countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
517 		pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n",
518 		    countdirs, files - countdirs, blks);
519 	}
520 	pwarn("%ld files, %jd used, %ju free ",
521 	    (long)n_files, (intmax_t)n_blks,
522 	    (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
523 	printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
524 	    (uintmax_t)n_ffree, (uintmax_t)n_bfree,
525 	    n_ffree * 100.0 / sblock.fs_dsize);
526 	if (debug) {
527 		if (files < 0)
528 			printf("%jd inodes missing\n", -files);
529 		if (blks < 0)
530 			printf("%jd blocks missing\n", -blks);
531 		if (duplist != NULL) {
532 			printf("The following duplicate blocks remain:");
533 			for (dp = duplist; dp; dp = dp->next)
534 				printf(" %jd,", (intmax_t)dp->dup);
535 			printf("\n");
536 		}
537 	}
538 	duplist = (struct dups *)0;
539 	muldup = (struct dups *)0;
540 	inocleanup();
541 	if (fsmodified) {
542 		sblock.fs_time = time(NULL);
543 		sbdirty();
544 	}
545 	if (cvtlevel && sblk.b_dirty) {
546 		/*
547 		 * Write out the duplicate super blocks
548 		 */
549 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
550 			blwrite(fswritefd, (char *)&sblock,
551 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)),
552 			    SBLOCKSIZE);
553 	}
554 	if (rerun)
555 		resolved = 0;
556 	finalIOstats();
557 
558 	/*
559 	 * Check to see if the file system is mounted read-write.
560 	 */
561 	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
562 		resolved = 0;
563 	ckfini(resolved);
564 
565 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
566 		if (inostathead[cylno].il_stat != NULL)
567 			free((char *)inostathead[cylno].il_stat);
568 	free((char *)inostathead);
569 	inostathead = NULL;
570 	if (fsmodified && !preen)
571 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
572 	if (rerun) {
573 		if (wantrestart && (restarts++ < 10) &&
574 		    (preen || reply("RESTART")))
575 			return (ERESTART);
576 		printf("\n***** PLEASE RERUN FSCK *****\n");
577 	}
578 	if (chkdoreload(mntp) != 0) {
579 		if (!fsmodified)
580 			return (0);
581 		if (!preen)
582 			printf("\n***** REBOOT NOW *****\n");
583 		sync();
584 		return (4);
585 	}
586 	return (0);
587 }
588 
589 static int
590 chkdoreload(struct statfs *mntp)
591 {
592 	struct iovec *iov;
593 	int iovlen;
594 	char errmsg[255];
595 
596 	if (mntp == NULL)
597 		return (0);
598 
599 	iov = NULL;
600 	iovlen = 0;
601 	errmsg[0] = '\0';
602 	/*
603 	 * We modified a mounted file system.  Do a mount update on
604 	 * it unless it is read-write, so we can continue using it
605 	 * as safely as possible.
606 	 */
607 	if (mntp->f_flags & MNT_RDONLY) {
608 		build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
609 		build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
610 		    (size_t)-1);
611 		build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
612 		    (size_t)-1);
613 		build_iovec(&iov, &iovlen, "errmsg", errmsg,
614 		    sizeof(errmsg));
615 		build_iovec(&iov, &iovlen, "update", NULL, 0);
616 		build_iovec(&iov, &iovlen, "reload", NULL, 0);
617 		/*
618 		 * XX: We need the following line until we clean up
619 		 * nmount parsing of root mounts and NFS root mounts.
620 		 */
621 		build_iovec(&iov, &iovlen, "ro", NULL, 0);
622 		if (nmount(iov, iovlen, mntp->f_flags) == 0) {
623 			return (0);
624 		}
625 		pwarn("mount reload of '%s' failed: %s %s\n\n",
626 		    mntp->f_mntonname, strerror(errno), errmsg);
627 		return (1);
628 	}
629 	return (0);
630 }
631 
632 /*
633  * Get the mount point information for name.
634  */
635 static struct statfs *
636 getmntpt(const char *name)
637 {
638 	struct stat devstat, mntdevstat;
639 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
640 	char *ddevname;
641 	struct statfs *mntbuf, *statfsp;
642 	int i, mntsize, isdev;
643 
644 	if (stat(name, &devstat) != 0)
645 		return (NULL);
646 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
647 		isdev = 1;
648 	else
649 		isdev = 0;
650 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
651 	for (i = 0; i < mntsize; i++) {
652 		statfsp = &mntbuf[i];
653 		ddevname = statfsp->f_mntfromname;
654 		if (*ddevname != '/') {
655 			if (strlen(_PATH_DEV) + strlen(ddevname) + 1 >
656 			    sizeof(statfsp->f_mntfromname))
657 				continue;
658 			strcpy(device, _PATH_DEV);
659 			strcat(device, ddevname);
660 			strcpy(statfsp->f_mntfromname, device);
661 		}
662 		if (isdev == 0) {
663 			if (strcmp(name, statfsp->f_mntonname))
664 				continue;
665 			return (statfsp);
666 		}
667 		if (stat(ddevname, &mntdevstat) == 0 &&
668 		    mntdevstat.st_rdev == devstat.st_rdev)
669 			return (statfsp);
670 	}
671 	statfsp = NULL;
672 	return (statfsp);
673 }
674 
675 static void
676 usage(void)
677 {
678 	(void) fprintf(stderr,
679 "usage: %s [-BCdEFfnpRrSyZ] [-b block] [-c level] [-m mode] filesystem ...\n",
680 	    getprogname());
681 	exit(1);
682 }
683 
684 void
685 infohandler(int sig __unused)
686 {
687 	got_siginfo = 1;
688 }
689 
690 void
691 alarmhandler(int sig __unused)
692 {
693 	got_sigalarm = 1;
694 }
695