Home | History | Annotate | Line # | Download | only in mount
      1 /*	$NetBSD: mount.c,v 1.108 2025/07/01 17:55:05 kre Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1989, 1993, 1994
      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 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993, 1994\
     35  The Regents of the University of California.  All rights reserved.");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
     41 #else
     42 __RCSID("$NetBSD: mount.c,v 1.108 2025/07/01 17:55:05 kre Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/mount.h>
     48 #include <sys/wait.h>
     49 
     50 #include <fs/puffs/puffs_msgif.h>
     51 
     52 #include <err.h>
     53 #include <errno.h>
     54 #include <fstab.h>
     55 #include <pwd.h>
     56 #include <signal.h>
     57 #include <stdio.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 #include <unistd.h>
     61 #include <util.h>
     62 
     63 #define MOUNTNAMES
     64 #include <fcntl.h>
     65 #include <sys/disk.h>
     66 #include <sys/disklabel.h>
     67 #include <sys/ioctl.h>
     68 
     69 #include "pathnames.h"
     70 #include "mountprog.h"
     71 
     72 static int	debug, verbose, noise;
     73 
     74 static void	catopt(char **, const char *);
     75 static const char *
     76 		getfslab(const char *str);
     77 static struct statvfs *
     78 		getmntpt(const char *);
     79 static int 	getmntargs(struct statvfs *, char *, size_t);
     80 static int	hasopt(const char *, const char *);
     81 static void	mangle(char *, int *, const char ** volatile *, int *);
     82 static int	mountfs(const char *, const char *, const char *,
     83 		    int, const char *, const char *, int, char *, size_t);
     84 static void	prmount(struct statvfs *);
     85 __dead static void	usage(void);
     86 
     87 
     88 /* Map from mount options to printable formats. */
     89 static const struct opt {
     90 	int o_opt;
     91 	int o_silent;
     92 	const char *o_name;
     93 } optnames[] = {
     94 	__MNT_FLAGS
     95 };
     96 
     97 #define FLG_UPDATE	1
     98 #define FLG_RDONLY	2
     99 #define FLG_RDWRITE	4
    100 #define FLG_FORCE	8
    101 
    102 static const char ffs_fstype[] = "ffs";
    103 
    104 int
    105 main(int argc, char *argv[])
    106 {
    107 	const char *mntfromname, *mntonname, **vfslist, *vfstype;
    108 	struct fstab *fs;
    109 	struct statvfs *mntbuf;
    110 #if 0
    111 	FILE *mountdfp;
    112 #endif
    113 	int all, ch, forceall, i, init_flags, mntsize, rval;
    114 	char *options;
    115 	const char *mountopts, *fstypename;
    116 	char canonical_path_buf[MAXPATHLEN], buf[MAXPATHLEN];
    117 	char *canonical_path;
    118 
    119 	/* started as "mount" */
    120 	all = forceall = init_flags = 0;
    121 	options = NULL;
    122 	vfslist = NULL;
    123 	vfstype = ffs_fstype;
    124 	while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
    125 		switch (ch) {
    126 		case 'A':
    127 			all = forceall = 1;
    128 			break;
    129 		case 'a':
    130 			all = 1;
    131 			break;
    132 		case 'd':
    133 			debug = 1;
    134 			break;
    135 		case 'f':
    136 			init_flags |= FLG_FORCE;
    137 			break;
    138 		case 'o':
    139 			if (*optarg)
    140 				catopt(&options, optarg);
    141 			break;
    142 		case 'r':
    143 			init_flags |= FLG_RDONLY;
    144 			break;
    145 		case 't':
    146 			if (vfslist != NULL)
    147 				errx(EXIT_FAILURE,
    148 				    "Only one -t option may be specified.");
    149 			vfslist = makevfslist(optarg);
    150 			vfstype = optarg;
    151 			break;
    152 		case 'u':
    153 			init_flags |= FLG_UPDATE;
    154 			break;
    155 		case 'v':
    156 			verbose++;
    157 			break;
    158 		case 'w':
    159 			init_flags |= FLG_RDWRITE;
    160 			break;
    161 		case '?':
    162 		default:
    163 			usage();
    164 			/* NOTREACHED */
    165 		}
    166 	argc -= optind;
    167 	argv += optind;
    168 
    169 #define	BADTYPE(type)							\
    170 	(strcmp(type, FSTAB_RO) &&					\
    171 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    172 
    173 	noise = 0;
    174 	if (argc > 0 && verbose) {
    175 		noise = 1;
    176 		verbose--;
    177 	}
    178 
    179 	rval = 0;
    180 	switch (argc) {
    181 	case 0:
    182 		if (all) {
    183 			while ((fs = getfsent()) != NULL) {
    184 				if (BADTYPE(fs->fs_type))
    185 					continue;
    186 				if (checkvfsname(fs->fs_vfstype, vfslist))
    187 					continue;
    188 				if (hasopt(fs->fs_mntops, "noauto"))
    189 					continue;
    190 				if (strcmp(fs->fs_spec, "from_mount") == 0) {
    191 					if ((mntbuf = getmntpt(fs->fs_file))
    192 					    == NULL)
    193 						errx(EXIT_FAILURE,
    194 						    "Unknown file system %s",
    195 						    fs->fs_file);
    196 					mntfromname = mntbuf->f_mntfromname;
    197 				} else
    198 					mntfromname = fs->fs_spec;
    199 				mntfromname = getfsspecname(buf, sizeof(buf),
    200 				    mntfromname);
    201 				if (mntfromname == NULL)
    202 					err(EXIT_FAILURE, "%s", buf);
    203 				if (mountfs(fs->fs_vfstype, mntfromname,
    204 				    fs->fs_file, init_flags, options,
    205 				    fs->fs_mntops, !forceall, NULL, 0))
    206 					rval = 1;
    207 			}
    208 		} else {
    209 			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    210 				err(EXIT_FAILURE, "getmntinfo");
    211 			for (i = 0; i < mntsize; i++) {
    212 				if (checkvfsname(mntbuf[i].f_fstypename,
    213 				    vfslist))
    214 					continue;
    215 				prmount(&mntbuf[i]);
    216 			}
    217 		}
    218 		return rval;
    219 	case 1:
    220 		if (vfslist != NULL) {
    221 			usage();
    222 			/* NOTREACHED */
    223 		}
    224 
    225 		/*
    226 		 * Create a canonical version of the device or mount path
    227 		 * passed to us.  It's ok for this to fail.  It's also ok
    228 		 * for the result to be exactly the same as the original.
    229 		 */
    230 		canonical_path = realpath(*argv, canonical_path_buf);
    231 
    232 		if (init_flags & FLG_UPDATE) {
    233 			/*
    234 			 * Try looking up the canonical path first,
    235 			 * then try exactly what the user entered.
    236 			 */
    237 			if ((canonical_path == NULL ||
    238 			    (mntbuf = getmntpt(canonical_path)) == NULL) &&
    239 			    (mntbuf = getmntpt(*argv)) == NULL) {
    240 out:
    241 				errx(EXIT_FAILURE,
    242 				    "Unknown special file or file system `%s'",
    243 				    *argv);
    244 			}
    245 			mntfromname = mntbuf->f_mntfromname;
    246 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
    247 				if (strcmp(fs->fs_spec, "from_mount") != 0)
    248 					mntfromname = fs->fs_spec;
    249 				/* ignore the fstab file options.  */
    250 				fs->fs_mntops = NULL;
    251 			}
    252 			mntonname  = mntbuf->f_mntonname;
    253 			fstypename = mntbuf->f_fstypename;
    254 			mountopts  = NULL;
    255 		} else {
    256 			/*
    257 			 * Try looking up the canonical path first,
    258 			 * then try exactly what the user entered.
    259 			 */
    260 			if (canonical_path == NULL ||
    261 			    ((fs = getfsfile(canonical_path)) == NULL &&
    262 			     (fs = getfsspec(canonical_path)) == NULL))
    263 			{
    264 				if ((fs = getfsfile(*argv)) == NULL &&
    265 				    (fs = getfsspec(*argv)) == NULL) {
    266 					goto out;
    267 				}
    268 			}
    269 			if (BADTYPE(fs->fs_type))
    270 				errx(EXIT_FAILURE,
    271 				    "Unknown file system type for `%s'",
    272 				    *argv);
    273 			if (strcmp(fs->fs_spec, "from_mount") == 0) {
    274 				if ((canonical_path == NULL ||
    275 				    (mntbuf = getmntpt(canonical_path))
    276 				     == NULL) &&
    277 				    (mntbuf = getmntpt(*argv)) == NULL)
    278 					goto out;
    279 				mntfromname = mntbuf->f_mntfromname;
    280 			} else
    281 				mntfromname = fs->fs_spec;
    282 			mntonname   = fs->fs_file;
    283 			fstypename  = fs->fs_vfstype;
    284 			mountopts   = fs->fs_mntops;
    285 		}
    286 		mntfromname = getfsspecname(buf, sizeof(buf), mntfromname);
    287 		if (mntfromname == NULL)
    288 			err(EXIT_FAILURE, "%s", buf);
    289 		rval = mountfs(fstypename, mntfromname,
    290 		    mntonname, init_flags, options, mountopts, 0, NULL, 0);
    291 		break;
    292 	case 2:
    293 		/*
    294 		 * If -t flag has not been specified, and spec contains either
    295 		 * a ':' or a '@' then assume that an NFS filesystem is being
    296 		 * specified ala Sun.
    297 		 */
    298 		mntfromname = getfsspecname(buf, sizeof(buf), argv[0]);
    299 		if (mntfromname == NULL)
    300 			err(EXIT_FAILURE, "%s", buf);
    301 		if (vfslist == NULL) {
    302 			if (strpbrk(argv[0], ":@") != NULL) {
    303 				vfstype = "nfs";
    304 			} else {
    305 				vfstype = getfslab(mntfromname);
    306 				if (vfstype == NULL)
    307 					vfstype = ffs_fstype;
    308 			}
    309 		}
    310 		rval = mountfs(vfstype, mntfromname, argv[1], init_flags,
    311 		    options, NULL, 0, NULL, 0);
    312 		break;
    313 	default:
    314 		usage();
    315 	}
    316 
    317 #if 0	/* disabled because it interferes the service. */
    318 	/*
    319 	 * If the mount was successfully, and done by root, tell mountd the
    320 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
    321 	 */
    322 	if (rval == 0 && getuid() == 0 &&
    323 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    324 		int pid;
    325 
    326 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
    327 		    pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    328 			err(EXIT_FAILURE, "signal mountd");
    329 		(void)fclose(mountdfp);
    330 	}
    331 #endif
    332 
    333 	return rval;
    334 }
    335 
    336 int
    337 hasopt(const char *mntopts, const char *option)
    338 {
    339 	int negative, found;
    340 	char *opt, *optbuf;
    341 
    342 	if (option[0] == 'n' && option[1] == 'o') {
    343 		negative = 1;
    344 		option += 2;
    345 	} else
    346 		negative = 0;
    347 	optbuf = estrdup(mntopts);
    348 	found = 0;
    349 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    350 		if (opt[0] == 'n' && opt[1] == 'o') {
    351 			if (!strcasecmp(opt + 2, option))
    352 				found = negative;
    353 		} else if (!strcasecmp(opt, option))
    354 			found = !negative;
    355 	}
    356 	free(optbuf);
    357 	return found;
    358 }
    359 
    360 static int
    361 mountfs(const char *vfstype, const char *spec, const char *name,
    362     int flags, const char *options, const char *mntopts,
    363     int skipmounted, char *buf, size_t buflen)
    364 {
    365 	/* List of directories containing mount_xxx subcommands. */
    366 	static const char *edirs[] = {
    367 #ifdef RESCUEDIR
    368 		RESCUEDIR,
    369 #endif
    370 		_PATH_SBIN,
    371 		_PATH_USRSBIN,
    372 		NULL
    373 	};
    374 	const char ** volatile argv, **edir;
    375 	struct statvfs *sfp, sf;
    376 	pid_t pid;
    377 	int pfd[2];
    378 	int argc, numfs, i, status, maxargc;
    379 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
    380 	    mntpath[MAXPATHLEN];
    381 	volatile int getargs;
    382 
    383 	if (realpath(name, mntpath) == NULL) {
    384 		warn("realpath `%s'", name);
    385 		return 1;
    386 	}
    387 
    388 	name = mntpath;
    389 
    390 	optbuf = NULL;
    391 	if (mntopts)
    392 		catopt(&optbuf, mntopts);
    393 
    394 	if (options) {
    395 		catopt(&optbuf, options);
    396 		getargs = strstr(options, "getargs") != NULL;
    397 	} else
    398 		getargs = 0;
    399 
    400 	if (!mntopts && !options)
    401 		catopt(&optbuf, "rw");
    402 
    403 	if (getargs == 0 && strcmp(name, "/") == 0 && !hasopt(optbuf, "union"))
    404 		flags |= FLG_UPDATE;
    405 	else if (skipmounted) {
    406 		if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
    407 			warn("getmntinfo");
    408 			return 1;
    409 		}
    410 		for(i = 0; i < numfs; i++) {
    411 			const char *mountedtype = sfp[i].f_fstypename;
    412 			size_t cmplen = sizeof(sfp[i].f_fstypename);
    413 
    414 			/* remove "puffs|" from comparisons, if present */
    415 #define TYPESIZE (sizeof(PUFFS_TYPEPREFIX)-1)
    416 			if (strncmp(mountedtype,
    417 			    PUFFS_TYPEPREFIX, TYPESIZE) == 0) {
    418 				mountedtype += TYPESIZE;
    419 				cmplen -= TYPESIZE;
    420 			}
    421 
    422 			/*
    423 			 * XXX can't check f_mntfromname,
    424 			 * thanks to mfs, union, etc.
    425 			 */
    426 			if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
    427 			    strncmp(vfstype, mountedtype, cmplen) == 0) {
    428 				if (noise || verbose)
    429 					(void)printf("%s on %s type %.*s: "
    430 					    "%s\n",
    431 					    sfp[i].f_mntfromname,
    432 					    sfp[i].f_mntonname,
    433 					    (int)sizeof(sfp[i].f_fstypename),
    434 					    sfp[i].f_fstypename,
    435 					    "already mounted");
    436 				return 0;
    437 			}
    438 		}
    439 	}
    440 	if (flags & FLG_FORCE)
    441 		catopt(&optbuf, "force");
    442 	if (flags & FLG_RDONLY)
    443 		catopt(&optbuf, "ro");
    444 	/* make -w override -r */
    445 	if (flags & FLG_RDWRITE)
    446 		catopt(&optbuf, "rw");
    447 
    448 	if (flags & FLG_UPDATE) {
    449 		catopt(&optbuf, "update");
    450 		/* Figure out the fstype only if we defaulted to ffs */
    451 		if (vfstype == ffs_fstype && statvfs(name, &sf) != -1)
    452 			vfstype = sf.f_fstypename;
    453 	}
    454 
    455 	maxargc = 64;
    456 	argv = ecalloc(maxargc, sizeof(*argv));
    457 
    458 	if (getargs &&
    459 	    strncmp(vfstype, PUFFS_TYPEPREFIX, sizeof(PUFFS_TYPEPREFIX)-1) == 0)
    460 		(void)snprintf(execbase, sizeof(execbase), "mount_puffs");
    461 	else if (hasopt(optbuf, "rump"))
    462 		(void)snprintf(execbase, sizeof(execbase), "rump_%s", vfstype);
    463 	else
    464 		(void)snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
    465 	argc = 0;
    466 	argv[argc++] = execbase;
    467 	if (optbuf)
    468 		mangle(optbuf, &argc, &argv, &maxargc);
    469 	argv[argc++] = spec;
    470 	argv[argc++] = name;
    471 	argv[argc] = NULL;
    472 
    473 	if ((verbose && buf == NULL) || debug) {
    474 		(void)printf("exec:");
    475 		for (i = 0; i < argc; i++)
    476 			(void)printf(" %s", argv[i]);
    477 		(void)printf("\n");
    478 		if (debug) {
    479 			/*
    480 			 * Don't proceed to attempt to
    481 			 * print fs data, nothing has been
    482 			 * mounted, anything printed would
    483 			 * be for some other filesystem.
    484 			 */
    485 			return 0;
    486 		}
    487 	}
    488 
    489 	if (buf) {
    490 		if (pipe(pfd) == -1)
    491 			warn("Cannot create pipe");
    492 	}
    493 
    494 	switch (pid = fork()) {
    495 	case -1:				/* Error. */
    496 		warn("fork");
    497 		if (optbuf)
    498 			free(optbuf);
    499 		free(argv);
    500 		return 1;
    501 
    502 	case 0:					/* Child. */
    503 		if (debug)
    504 			_exit(0);
    505 
    506 		if (buf) {
    507 			(void)close(pfd[0]);
    508 			(void)close(STDOUT_FILENO);
    509 			if (dup2(pfd[1], STDOUT_FILENO) == -1)
    510 				warn("Cannot open fd to mount program");
    511 		}
    512 
    513 		/* Go find an executable. */
    514 		edir = edirs;
    515 		do {
    516 			(void)snprintf(execname,
    517 			    sizeof(execname), "%s/%s", *edir, execbase);
    518 			(void)execv(execname, __UNCONST(argv));
    519 			if (errno != ENOENT)
    520 				warn("exec %s for %s", execname, name);
    521 		} while (*++edir != NULL);
    522 
    523 		if (errno == ENOENT)
    524 			warn("exec %s for %s: %s", execbase, name, execbase);
    525 		_exit(1);
    526 		/* NOTREACHED */
    527 
    528 	default:				/* Parent. */
    529 		if (optbuf)
    530 			free(optbuf);
    531 		free(argv);
    532 
    533 		if (buf || getargs) {
    534 			char tbuf[1024], *ptr;
    535 			int nread;
    536 
    537 			if (buf == NULL) {
    538 				ptr = tbuf;
    539 				buflen = sizeof(tbuf) - 1;
    540 			} else {
    541 				ptr = buf;
    542 				buflen--;
    543 			}
    544 			(void)close(pfd[1]);
    545 			(void)signal(SIGPIPE, SIG_IGN);
    546 			while ((nread = read(pfd[0], ptr, buflen)) > 0) {
    547 				buflen -= nread;
    548 				ptr += nread;
    549 			}
    550 			*ptr = '\0';
    551 			if (buflen == 0) {
    552 				while (read(pfd[0], &nread, sizeof(nread)) > 0)
    553 					continue;
    554 			}
    555 			if (buf == NULL)
    556 				(void)fprintf(stdout, "%s", tbuf);
    557 		}
    558 
    559 		if (waitpid(pid, &status, 0) == -1) {
    560 			warn("waitpid");
    561 			return 1;
    562 		}
    563 
    564 		if (WIFEXITED(status)) {
    565 			if (WEXITSTATUS(status) != 0)
    566 				return WEXITSTATUS(status);
    567 		} else if (WIFSIGNALED(status)) {
    568 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
    569 			return 1;
    570 		}
    571 
    572 		if (buf == NULL) {
    573 			if (verbose || noise) {
    574 				if (statvfs(name, &sf) == -1) {
    575 					warn("statvfs %s", name);
    576 					return 1;
    577 				}
    578 				prmount(&sf);
    579 			}
    580 		}
    581 		break;
    582 	}
    583 
    584 	return 0;
    585 }
    586 
    587 static void
    588 prmount(struct statvfs *sfp)
    589 {
    590 	int flags;
    591 	const struct opt *o;
    592 	struct passwd *pw;
    593 	int f;
    594 
    595 	(void)printf("%s on %s type %.*s", sfp->f_mntfromname,
    596 	    sfp->f_mntonname, (int)sizeof(sfp->f_fstypename),
    597 	    sfp->f_fstypename);
    598 
    599 	flags = sfp->f_flag & MNT_VISFLAGMASK;
    600 	for (f = 0, o = optnames; flags && o <
    601 	    &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
    602 		if (flags & o->o_opt) {
    603 			if (!o->o_silent || verbose)
    604 				(void)printf("%s%s", !f++ ? " (" : ", ",
    605 				    o->o_name);
    606 			flags &= ~o->o_opt;
    607 		}
    608 	if (flags)
    609 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
    610 		    flags & (flags - 1) ? "s" : "", flags);
    611 	if (sfp->f_owner) {
    612 		(void)printf("%smounted by ", !f++ ? " (" : ", ");
    613 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
    614 			(void)printf("%s", pw->pw_name);
    615 		else
    616 			(void)printf("%d", sfp->f_owner);
    617 	}
    618 	if (verbose)
    619 		(void)printf("%sfsid: 0x%x/0x%x",
    620 		    !f++ ? " (" /* ) */: ", ",
    621 		    sfp->f_fsidx.__fsid_val[0], sfp->f_fsidx.__fsid_val[1]);
    622 
    623 	if (verbose) {
    624 		(void)printf("%s", !f++ ? " (" : ", ");
    625 		(void)printf("reads: sync %" PRIu64 " async %" PRIu64 "",
    626 		    sfp->f_syncreads, sfp->f_asyncreads);
    627 		(void)printf(", writes: sync %" PRIu64 " async %" PRIu64 "",
    628 		    sfp->f_syncwrites, sfp->f_asyncwrites);
    629 		if (verbose > 1) {
    630 			char buf[2048];
    631 
    632 			if (getmntargs(sfp, buf, sizeof(buf)))
    633 				printf(", [%s: %s]", sfp->f_fstypename, buf);
    634 		}
    635 		printf(")\n");
    636 	} else
    637 		(void)printf("%s", f ? ")\n" : "\n");
    638 }
    639 
    640 static int
    641 getmntargs(struct statvfs *sfs, char *buf, size_t buflen)
    642 {
    643 
    644 	if (mountfs(sfs->f_fstypename, sfs->f_mntfromname, sfs->f_mntonname, 0,
    645 	    "getargs", NULL, 0, buf, buflen))
    646 		return 0;
    647 	else {
    648 		if (*buf == '\0')
    649 			return 0;
    650 		if ((buf = strchr(buf, '\n')) != NULL)
    651 			*buf = '\0';
    652 		return 1;
    653 	}
    654 }
    655 
    656 static struct statvfs *
    657 getmntpt(const char *name)
    658 {
    659 	struct statvfs *mntbuf;
    660 	int i, mntsize;
    661 
    662 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    663 	for (i = 0; i < mntsize; i++)
    664 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    665 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    666 			return &mntbuf[i];
    667 	return NULL;
    668 }
    669 
    670 static void
    671 catopt(char **sp, const char *o)
    672 {
    673 	char *s, *n;
    674 
    675 	s = *sp;
    676 	if (s) {
    677 		easprintf(&n, "%s,%s", s, o);
    678 		free(s);
    679 		s = n;
    680 	} else
    681 		s = estrdup(o);
    682 	*sp = s;
    683 }
    684 
    685 static void
    686 mangle(char *options, int *argcp, const char ** volatile *argvp, int *maxargcp)
    687 {
    688 	char *p, *s;
    689 	int argc, maxargc;
    690 	const char **argv, **nargv;
    691 
    692 	argc = *argcp;
    693 	argv = *argvp;
    694 	maxargc = *maxargcp;
    695 
    696 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
    697 		/* Always leave space for one more argument and the NULL. */
    698 		if (argc >= maxargc - 4) {
    699 			nargv = erealloc(argv, (maxargc << 1) * sizeof(nargv));
    700 			argv = nargv;
    701 			maxargc <<= 1;
    702 		}
    703 		if (*p != '\0') {
    704 			if (*p == '-') {
    705 				argv[argc++] = p;
    706 				p = strchr(p, '=');
    707 				if (p) {
    708 					*p = '\0';
    709 					argv[argc++] = p+1;
    710 				}
    711 			} else {
    712 				argv[argc++] = "-o";
    713 				argv[argc++] = p;
    714 			}
    715 		}
    716 	}
    717 
    718 	*argcp = argc;
    719 	*argvp = argv;
    720 	*maxargcp = maxargc;
    721 }
    722 
    723 /* Deduce the filesystem type from the disk label. */
    724 static const char *
    725 getfslab(const char *str)
    726 {
    727 	static struct dkwedge_info dkw;
    728 	struct disklabel dl;
    729 	int fd;
    730 	int part;
    731 	const char *vfstype;
    732 	u_char fstype;
    733 	char buf[MAXPATHLEN + 1];
    734 	char *sp, *ep;
    735 
    736 	if ((fd = open(str, O_RDONLY)) == -1) {
    737 		/*
    738 		 * Iff we get EBUSY try the raw device. Since mount always uses
    739 		 * the block device we know we are never passed a raw device.
    740 		 */
    741 		if (errno != EBUSY)
    742 			err(EXIT_FAILURE, "cannot open `%s'", str);
    743 		strlcpy(buf, str, MAXPATHLEN);
    744 		if ((sp = strrchr(buf, '/')) != NULL)
    745 			++sp;
    746 		else
    747 			sp = buf;
    748 		for (ep = sp + strlen(sp) + 1;  ep > sp; ep--)
    749 			*ep = *(ep - 1);
    750 		*sp = 'r';
    751 
    752 		/* Silently fail here - mount call can display error */
    753 		if ((fd = open(buf, O_RDONLY)) == -1)
    754 			return NULL;
    755 	}
    756 
    757 	/* Check to see if this is a wedge. */
    758 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
    759 		/* Yup, this is easy. */
    760 		close(fd);
    761 		if (*dkw.dkw_ptype)
    762 			return dkw.dkw_ptype;
    763 		return NULL;
    764 	}
    765 
    766 	if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
    767 		(void) close(fd);
    768 		return NULL;
    769 	}
    770 
    771 	(void) close(fd);
    772 
    773 	part = str[strlen(str) - 1] - 'a';
    774 
    775 	if (part < 0 || part >= dl.d_npartitions)
    776 		return NULL;
    777 
    778 	/* Return NULL for unknown types - caller can fall back to ffs */
    779 	if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
    780 		vfstype = NULL;
    781 	else
    782 		vfstype = mountnames[fstype];
    783 
    784 	return vfstype;
    785 }
    786 
    787 static void
    788 usage(void)
    789 {
    790 
    791 	(void)fprintf(stderr, "Usage: %s [-Aadfruvw] [-t type]\n"
    792 	    "\t%s [-dfruvw] special | node\n"
    793 	    "\t%s [-dfruvw] [-o options] [-t type] special node\n",
    794 	    getprogname(), getprogname(), getprogname());
    795 	exit(1);
    796 }
    797