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