xref: /freebsd/sbin/fsck_ffs/main.c (revision 8a0a413e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1986, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
41 #endif /* not lint */
42 #endif
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include <sys/param.h>
47 #include <sys/file.h>
48 #include <sys/mount.h>
49 #include <sys/resource.h>
50 #include <sys/stat.h>
51 #include <sys/sysctl.h>
52 #include <sys/uio.h>
53 #include <sys/disklabel.h>
54 
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ffs/fs.h>
57 
58 #include <err.h>
59 #include <errno.h>
60 #include <fstab.h>
61 #include <grp.h>
62 #include <inttypes.h>
63 #include <mntopts.h>
64 #include <paths.h>
65 #include <stdint.h>
66 #include <string.h>
67 #include <time.h>
68 
69 #include "fsck.h"
70 
71 int	restarts;
72 
73 static void usage(void) __dead2;
74 static intmax_t argtoimax(int flag, const char *req, const char *str, int base);
75 static int checkfilesys(char *filesys);
76 static int chkdoreload(struct statfs *mntp);
77 static struct statfs *getmntpt(const char *);
78 
79 int
80 main(int argc, char *argv[])
81 {
82 	int ch;
83 	struct rlimit rlimit;
84 	struct itimerval itimerval;
85 	int ret = 0;
86 
87 	sync();
88 	skipclean = 1;
89 	inoopt = 0;
90 	while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:npRrSyZ")) != -1) {
91 		switch (ch) {
92 		case 'b':
93 			skipclean = 0;
94 			bflag = argtoimax('b', "number", optarg, 10);
95 			printf("Alternate super block location: %jd\n", bflag);
96 			break;
97 
98 		case 'B':
99 			bkgrdflag = 1;
100 			break;
101 
102 		case 'c':
103 			skipclean = 0;
104 			cvtlevel = argtoimax('c', "conversion level", optarg,
105 			    10);
106 			if (cvtlevel < 3)
107 				errx(EEXIT, "cannot do level %d conversion",
108 				    cvtlevel);
109 			break;
110 
111 		case 'd':
112 			debug++;
113 			break;
114 
115 		case 'E':
116 			Eflag++;
117 			break;
118 
119 		case 'f':
120 			skipclean = 0;
121 			break;
122 
123 		case 'F':
124 			bkgrdcheck = 1;
125 			break;
126 
127 		case 'm':
128 			lfmode = argtoimax('m', "mode", optarg, 8);
129 			if (lfmode &~ 07777)
130 				errx(EEXIT, "bad mode to -m: %o", lfmode);
131 			printf("** lost+found creation mode %o\n", lfmode);
132 			break;
133 
134 		case 'n':
135 			nflag++;
136 			yflag = 0;
137 			break;
138 
139 		case 'p':
140 			preen++;
141 			/*FALLTHROUGH*/
142 
143 		case 'C':
144 			ckclean++;
145 			break;
146 
147 		case 'R':
148 			wantrestart = 1;
149 			break;
150 		case 'r':
151 			inoopt++;
152 			break;
153 
154 		case 'S':
155 			surrender = 1;
156 			break;
157 
158 		case 'y':
159 			yflag++;
160 			nflag = 0;
161 			break;
162 
163 		case 'Z':
164 			Zflag++;
165 			break;
166 
167 		default:
168 			usage();
169 		}
170 	}
171 	argc -= optind;
172 	argv += optind;
173 
174 	if (!argc)
175 		usage();
176 
177 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
178 		(void)signal(SIGINT, catch);
179 	if (ckclean)
180 		(void)signal(SIGQUIT, catchquit);
181 	signal(SIGINFO, infohandler);
182 	if (bkgrdflag) {
183 		signal(SIGALRM, alarmhandler);
184 		itimerval.it_interval.tv_sec = 5;
185 		itimerval.it_interval.tv_usec = 0;
186 		itimerval.it_value.tv_sec = 5;
187 		itimerval.it_value.tv_usec = 0;
188 		setitimer(ITIMER_REAL, &itimerval, NULL);
189 	}
190 	/*
191 	 * Push up our allowed memory limit so we can cope
192 	 * with huge file systems.
193 	 */
194 	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
195 		rlimit.rlim_cur = rlimit.rlim_max;
196 		(void)setrlimit(RLIMIT_DATA, &rlimit);
197 	}
198 	while (argc > 0) {
199 		if (checkfilesys(*argv) == ERESTART)
200 			continue;
201 		argc--;
202 		argv++;
203 	}
204 
205 	if (returntosingle)
206 		ret = 2;
207 	exit(ret);
208 }
209 
210 static intmax_t
211 argtoimax(int flag, const char *req, const char *str, int base)
212 {
213 	char *cp;
214 	intmax_t ret;
215 
216 	ret = strtoimax(str, &cp, base);
217 	if (cp == str || *cp)
218 		errx(EEXIT, "-%c flag requires a %s", flag, req);
219 	return (ret);
220 }
221 
222 /*
223  * Check the specified file system.
224  */
225 /* ARGSUSED */
226 static int
227 checkfilesys(char *filesys)
228 {
229 	ufs2_daddr_t n_ffree, n_bfree;
230 	struct dups *dp;
231 	struct statfs *mntp;
232 	struct stat snapdir;
233 	struct group *grp;
234 	struct iovec *iov;
235 	char errmsg[255];
236 	int ofsmodified;
237 	int iovlen;
238 	int cylno;
239 	intmax_t blks, files;
240 	size_t size;
241 
242 	iov = NULL;
243 	iovlen = 0;
244 	errmsg[0] = '\0';
245 	fsutilinit();
246 	fsckinit();
247 
248 	cdevname = filesys;
249 	if (debug && ckclean)
250 		pwarn("starting\n");
251 	/*
252 	 * Make best effort to get the disk name. Check first to see
253 	 * if it is listed among the mounted file systems. Failing that
254 	 * check to see if it is listed in /etc/fstab.
255 	 */
256 	mntp = getmntpt(filesys);
257 	if (mntp != NULL)
258 		filesys = mntp->f_mntfromname;
259 	else
260 		filesys = blockcheck(filesys);
261 	/*
262 	 * If -F flag specified, check to see whether a background check
263 	 * is possible and needed. If possible and needed, exit with
264 	 * status zero. Otherwise exit with status non-zero. A non-zero
265 	 * exit status will cause a foreground check to be run.
266 	 */
267 	sblock_init();
268 	if (bkgrdcheck) {
269 		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
270 			exit(3);	/* Cannot read superblock */
271 		close(fsreadfd);
272 		/* Earlier background failed or journaled */
273 		if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
274 			exit(4);
275 		if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
276 			exit(5);	/* Not running soft updates */
277 		size = MIBSIZE;
278 		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
279 			exit(6);	/* Lacks kernel support */
280 		if ((mntp == NULL && sblock.fs_clean == 1) ||
281 		    (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
282 			exit(7);	/* Filesystem clean, report it now */
283 		exit(0);
284 	}
285 	if (ckclean && skipclean) {
286 		/*
287 		 * If file system is gjournaled, check it here.
288 		 */
289 		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
290 			exit(3);	/* Cannot read superblock */
291 		close(fsreadfd);
292 		if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
293 			//printf("GJournaled file system detected on %s.\n",
294 			//    filesys);
295 			if (sblock.fs_clean == 1) {
296 				pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
297 				exit(0);
298 			}
299 			if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
300 				gjournal_check(filesys);
301 				if (chkdoreload(mntp) == 0)
302 					exit(0);
303 				exit(4);
304 			} else {
305 				pfatal(
306 			    "UNEXPECTED INCONSISTENCY, CANNOT RUN FAST FSCK\n");
307 			}
308 		}
309 	}
310 	/*
311 	 * If we are to do a background check:
312 	 *	Get the mount point information of the file system
313 	 *	create snapshot file
314 	 *	return created snapshot file
315 	 *	if not found, clear bkgrdflag and proceed with normal fsck
316 	 */
317 	if (bkgrdflag) {
318 		if (mntp == NULL) {
319 			bkgrdflag = 0;
320 			pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
321 		} else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
322 			bkgrdflag = 0;
323 			pfatal(
324 			  "NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
325 		} else if ((mntp->f_flags & MNT_RDONLY) != 0) {
326 			bkgrdflag = 0;
327 			pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
328 		} else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
329 			if (readsb(0) != 0) {
330 				if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) {
331 					bkgrdflag = 0;
332 					pfatal(
333 			"UNEXPECTED INCONSISTENCY, CANNOT RUN IN BACKGROUND\n");
334 				}
335 				if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
336 				    skipclean && ckclean) {
337 					/*
338 					 * file system is clean;
339 					 * skip snapshot and report it clean
340 					 */
341 					pwarn(
342 					"FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
343 					goto clean;
344 				}
345 			}
346 			close(fsreadfd);
347 		}
348 		if (bkgrdflag) {
349 			snprintf(snapname, sizeof snapname, "%s/.snap",
350 			    mntp->f_mntonname);
351 			if (stat(snapname, &snapdir) < 0) {
352 				if (errno != ENOENT) {
353 					bkgrdflag = 0;
354 					pfatal(
355 	"CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
356 					    snapname, strerror(errno));
357 				} else if ((grp = getgrnam("operator")) == NULL ||
358 					   mkdir(snapname, 0770) < 0 ||
359 					   chown(snapname, -1, grp->gr_gid) < 0 ||
360 					   chmod(snapname, 0770) < 0) {
361 					bkgrdflag = 0;
362 					pfatal(
363 	"CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
364 					    snapname, strerror(errno));
365 				}
366 			} else if (!S_ISDIR(snapdir.st_mode)) {
367 				bkgrdflag = 0;
368 				pfatal(
369 			"%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
370 				    snapname);
371 			}
372 		}
373 		if (bkgrdflag) {
374 			snprintf(snapname, sizeof snapname,
375 			    "%s/.snap/fsck_snapshot", mntp->f_mntonname);
376 			build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
377 			build_iovec(&iov, &iovlen, "from", snapname,
378 			    (size_t)-1);
379 			build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
380 			    (size_t)-1);
381 			build_iovec(&iov, &iovlen, "errmsg", errmsg,
382 			    sizeof(errmsg));
383 			build_iovec(&iov, &iovlen, "update", NULL, 0);
384 			build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
385 
386 			while (nmount(iov, iovlen, mntp->f_flags) < 0) {
387 				if (errno == EEXIST && unlink(snapname) == 0)
388 					continue;
389 				bkgrdflag = 0;
390 				pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n",
391 				    snapname, strerror(errno), errmsg);
392 				break;
393 			}
394 			if (bkgrdflag != 0)
395 				filesys = snapname;
396 		}
397 	}
398 
399 	switch (setup(filesys)) {
400 	case 0:
401 		if (preen)
402 			pfatal("CAN'T CHECK FILE SYSTEM.");
403 		return (0);
404 	case -1:
405 	clean:
406 		pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
407 		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
408 		printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
409 		    (intmax_t)sblock.fs_cstotal.cs_nffree,
410 		    (intmax_t)sblock.fs_cstotal.cs_nbfree,
411 		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
412 		return (0);
413 	}
414 	/*
415 	 * Determine if we can and should do journal recovery.
416 	 */
417 	if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
418 		if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
419 			if (preen || reply("USE JOURNAL")) {
420 				if (suj_check(filesys) == 0) {
421 					printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
422 					if (chkdoreload(mntp) == 0)
423 						exit(0);
424 					exit(4);
425 				}
426 			}
427 			printf("** Skipping journal, falling through to full fsck\n\n");
428 		}
429 		/*
430 		 * Write the superblock so we don't try to recover the
431 		 * journal on another pass. If this is the only change
432 		 * to the filesystem, we do not want it to be called
433 		 * out as modified.
434 		 */
435 		sblock.fs_mtime = time(NULL);
436 		sbdirty();
437 		ofsmodified = fsmodified;
438 		flush(fswritefd, &sblk);
439 		fsmodified = ofsmodified;
440 	}
441 
442 	/*
443 	 * Cleared if any questions answered no. Used to decide if
444 	 * the superblock should be marked clean.
445 	 */
446 	resolved = 1;
447 	/*
448 	 * 1: scan inodes tallying blocks used
449 	 */
450 	if (preen == 0) {
451 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
452 		if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
453 			printf("** Root file system\n");
454 		printf("** Phase 1 - Check Blocks and Sizes\n");
455 	}
456 	clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
457 	pass1();
458 	IOstats("Pass1");
459 
460 	/*
461 	 * 1b: locate first references to duplicates, if any
462 	 */
463 	if (duplist) {
464 		if (preen || usedsoftdep)
465 			pfatal("INTERNAL ERROR: dups with %s%s%s",
466 			    preen ? "-p" : "",
467 			    (preen && usedsoftdep) ? " and " : "",
468 			    usedsoftdep ? "softupdates" : "");
469 		printf("** Phase 1b - Rescan For More DUPS\n");
470 		pass1b();
471 		IOstats("Pass1b");
472 	}
473 
474 	/*
475 	 * 2: traverse directories from root to mark all connected directories
476 	 */
477 	if (preen == 0)
478 		printf("** Phase 2 - Check Pathnames\n");
479 	pass2();
480 	IOstats("Pass2");
481 
482 	/*
483 	 * 3: scan inodes looking for disconnected directories
484 	 */
485 	if (preen == 0)
486 		printf("** Phase 3 - Check Connectivity\n");
487 	pass3();
488 	IOstats("Pass3");
489 
490 	/*
491 	 * 4: scan inodes looking for disconnected files; check reference counts
492 	 */
493 	if (preen == 0)
494 		printf("** Phase 4 - Check Reference Counts\n");
495 	pass4();
496 	IOstats("Pass4");
497 
498 	/*
499 	 * 5: check and repair resource counts in cylinder groups
500 	 */
501 	if (preen == 0)
502 		printf("** Phase 5 - Check Cyl groups\n");
503 	pass5();
504 	IOstats("Pass5");
505 
506 	/*
507 	 * print out summary statistics
508 	 */
509 	n_ffree = sblock.fs_cstotal.cs_nffree;
510 	n_bfree = sblock.fs_cstotal.cs_nbfree;
511 	files = maxino - UFS_ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
512 	blks = n_blks +
513 	    sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
514 	blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
515 	blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
516 	blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
517 	if (bkgrdflag && (files > 0 || blks > 0)) {
518 		countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
519 		pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n",
520 		    countdirs, files - countdirs, blks);
521 	}
522 	pwarn("%ld files, %jd used, %ju free ",
523 	    (long)n_files, (intmax_t)n_blks,
524 	    (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
525 	printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
526 	    (uintmax_t)n_ffree, (uintmax_t)n_bfree,
527 	    n_ffree * 100.0 / sblock.fs_dsize);
528 	if (debug) {
529 		if (files < 0)
530 			printf("%jd inodes missing\n", -files);
531 		if (blks < 0)
532 			printf("%jd blocks missing\n", -blks);
533 		if (duplist != NULL) {
534 			printf("The following duplicate blocks remain:");
535 			for (dp = duplist; dp; dp = dp->next)
536 				printf(" %jd,", (intmax_t)dp->dup);
537 			printf("\n");
538 		}
539 	}
540 	duplist = (struct dups *)0;
541 	muldup = (struct dups *)0;
542 	inocleanup();
543 	if (fsmodified) {
544 		sblock.fs_time = time(NULL);
545 		sbdirty();
546 	}
547 	if (cvtlevel && sblk.b_dirty) {
548 		/*
549 		 * Write out the duplicate super blocks
550 		 */
551 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
552 			blwrite(fswritefd, (char *)&sblock,
553 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)),
554 			    SBLOCKSIZE);
555 	}
556 	if (rerun)
557 		resolved = 0;
558 	finalIOstats();
559 
560 	/*
561 	 * Check to see if the file system is mounted read-write.
562 	 */
563 	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
564 		resolved = 0;
565 	ckfini(resolved);
566 
567 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
568 		if (inostathead[cylno].il_stat != NULL)
569 			free((char *)inostathead[cylno].il_stat);
570 	free((char *)inostathead);
571 	inostathead = NULL;
572 	if (fsmodified && !preen)
573 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
574 	if (rerun) {
575 		if (wantrestart && (restarts++ < 10) &&
576 		    (preen || reply("RESTART")))
577 			return (ERESTART);
578 		printf("\n***** PLEASE RERUN FSCK *****\n");
579 	}
580 	if (chkdoreload(mntp) != 0) {
581 		if (!fsmodified)
582 			return (0);
583 		if (!preen)
584 			printf("\n***** REBOOT NOW *****\n");
585 		sync();
586 		return (4);
587 	}
588 	return (0);
589 }
590 
591 static int
592 chkdoreload(struct statfs *mntp)
593 {
594 	struct iovec *iov;
595 	int iovlen;
596 	char errmsg[255];
597 
598 	if (mntp == NULL)
599 		return (0);
600 
601 	iov = NULL;
602 	iovlen = 0;
603 	errmsg[0] = '\0';
604 	/*
605 	 * We modified a mounted file system.  Do a mount update on
606 	 * it unless it is read-write, so we can continue using it
607 	 * as safely as possible.
608 	 */
609 	if (mntp->f_flags & MNT_RDONLY) {
610 		build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
611 		build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
612 		    (size_t)-1);
613 		build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
614 		    (size_t)-1);
615 		build_iovec(&iov, &iovlen, "errmsg", errmsg,
616 		    sizeof(errmsg));
617 		build_iovec(&iov, &iovlen, "update", NULL, 0);
618 		build_iovec(&iov, &iovlen, "reload", NULL, 0);
619 		/*
620 		 * XX: We need the following line until we clean up
621 		 * nmount parsing of root mounts and NFS root mounts.
622 		 */
623 		build_iovec(&iov, &iovlen, "ro", NULL, 0);
624 		if (nmount(iov, iovlen, mntp->f_flags) == 0) {
625 			return (0);
626 		}
627 		pwarn("mount reload of '%s' failed: %s %s\n\n",
628 		    mntp->f_mntonname, strerror(errno), errmsg);
629 		return (1);
630 	}
631 	return (0);
632 }
633 
634 /*
635  * Get the mount point information for name.
636  */
637 static struct statfs *
638 getmntpt(const char *name)
639 {
640 	struct stat devstat, mntdevstat;
641 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
642 	char *ddevname;
643 	struct statfs *mntbuf, *statfsp;
644 	int i, mntsize, isdev;
645 
646 	if (stat(name, &devstat) != 0)
647 		return (NULL);
648 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
649 		isdev = 1;
650 	else
651 		isdev = 0;
652 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
653 	for (i = 0; i < mntsize; i++) {
654 		statfsp = &mntbuf[i];
655 		ddevname = statfsp->f_mntfromname;
656 		if (*ddevname != '/') {
657 			if (strlen(_PATH_DEV) + strlen(ddevname) + 1 >
658 			    sizeof(statfsp->f_mntfromname))
659 				continue;
660 			strcpy(device, _PATH_DEV);
661 			strcat(device, ddevname);
662 			strcpy(statfsp->f_mntfromname, device);
663 		}
664 		if (isdev == 0) {
665 			if (strcmp(name, statfsp->f_mntonname))
666 				continue;
667 			return (statfsp);
668 		}
669 		if (stat(ddevname, &mntdevstat) == 0 &&
670 		    mntdevstat.st_rdev == devstat.st_rdev)
671 			return (statfsp);
672 	}
673 	statfsp = NULL;
674 	return (statfsp);
675 }
676 
677 static void
678 usage(void)
679 {
680 	(void) fprintf(stderr,
681 "usage: %s [-BCdEFfnpRrSyZ] [-b block] [-c level] [-m mode] filesystem ...\n",
682 	    getprogname());
683 	exit(1);
684 }
685 
686 void
687 infohandler(int sig __unused)
688 {
689 	got_siginfo = 1;
690 }
691 
692 void
693 alarmhandler(int sig __unused)
694 {
695 	got_sigalarm = 1;
696 }
697