Home | History | Annotate | Line # | Download | only in fsck
fsck.c revision 1.38
      1  1.38  christos /*	$NetBSD: fsck.c,v 1.38 2005/01/13 22:56:09 christos Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*
      4  1.33  christos  * Copyright (c) 1996 Christos Zoulas. All rights reserved.
      5   1.1  christos  * Copyright (c) 1980, 1989, 1993, 1994
      6   1.1  christos  *	The Regents of the University of California.  All rights reserved.
      7   1.1  christos  *
      8   1.1  christos  * Redistribution and use in source and binary forms, with or without
      9   1.1  christos  * modification, are permitted provided that the following conditions
     10   1.1  christos  * are met:
     11   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15   1.1  christos  *    documentation and/or other materials provided with the distribution.
     16  1.30       agc  * 3. Neither the name of the University nor the names of its contributors
     17  1.30       agc  *    may be used to endorse or promote products derived from this software
     18  1.30       agc  *    without specific prior written permission.
     19  1.30       agc  *
     20  1.30       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  1.30       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  1.30       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.30       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  1.30       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.30       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  1.30       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.30       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  1.30       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  1.30       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  1.30       agc  * SUCH DAMAGE.
     31  1.30       agc  *
     32  1.30       agc  * From: @(#)mount.c	8.19 (Berkeley) 4/19/94
     33  1.30       agc  * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
     34  1.30       agc  *
     35  1.30       agc  */
     36  1.30       agc 
     37  1.12     lukem #include <sys/cdefs.h>
     38  1.12     lukem #ifndef lint
     39  1.38  christos __RCSID("$NetBSD: fsck.c,v 1.38 2005/01/13 22:56:09 christos Exp $");
     40  1.12     lukem #endif /* not lint */
     41   1.1  christos 
     42   1.1  christos #include <sys/param.h>
     43   1.1  christos #include <sys/mount.h>
     44   1.1  christos #include <sys/queue.h>
     45   1.1  christos #include <sys/wait.h>
     46  1.20  christos #define FSTYPENAMES
     47  1.20  christos #define FSCKNAMES
     48  1.36   thorpej #include <sys/disk.h>
     49   1.8  christos #include <sys/disklabel.h>
     50   1.8  christos #include <sys/ioctl.h>
     51   1.1  christos 
     52   1.1  christos #include <err.h>
     53   1.1  christos #include <errno.h>
     54   1.1  christos #include <fstab.h>
     55   1.8  christos #include <fcntl.h>
     56  1.21       abs #include <paths.h>
     57   1.1  christos #include <signal.h>
     58   1.1  christos #include <stdio.h>
     59   1.1  christos #include <stdlib.h>
     60   1.1  christos #include <string.h>
     61   1.1  christos #include <unistd.h>
     62   1.1  christos 
     63   1.1  christos #include "pathnames.h"
     64   1.4  christos #include "fsutil.h"
     65   1.1  christos 
     66   1.2  christos static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
     67   1.1  christos 
     68   1.2  christos TAILQ_HEAD(fstypelist, entry) opthead, selhead;
     69   1.1  christos 
     70   1.1  christos struct entry {
     71   1.1  christos 	char *type;
     72   1.2  christos 	char *options;
     73   1.1  christos 	TAILQ_ENTRY(entry) entries;
     74   1.1  christos };
     75   1.1  christos 
     76   1.4  christos static int maxrun = 0;
     77   1.2  christos static char *options = NULL;
     78   1.4  christos static int flags = 0;
     79   1.2  christos 
     80  1.25     lukem int main(int, char *[]);
     81   1.1  christos 
     82  1.25     lukem static int checkfs(const char *, const char *, const char *, void *, pid_t *);
     83  1.25     lukem static int selected(const char *);
     84  1.25     lukem static void addoption(char *);
     85  1.25     lukem static const char *getoptions(const char *);
     86  1.25     lukem static void addentry(struct fstypelist *, const char *, const char *);
     87  1.25     lukem static void maketypelist(char *);
     88  1.25     lukem static void catopt(char **, const char *);
     89  1.25     lukem static void mangle(char *, int *, const char ***, int *);
     90  1.25     lukem static const char *getfslab(const char *);
     91  1.25     lukem static void usage(void);
     92  1.25     lukem static void *isok(struct fstab *);
     93   1.1  christos 
     94   1.1  christos int
     95  1.25     lukem main(int argc, char *argv[])
     96   1.1  christos {
     97   1.1  christos 	struct fstab *fs;
     98   1.1  christos 	int i, rval = 0;
     99  1.17   mycroft 	const char *vfstype = NULL;
    100   1.7  christos 	char globopt[3];
    101   1.7  christos 
    102   1.7  christos 	globopt[0] = '-';
    103   1.7  christos 	globopt[2] = '\0';
    104   1.1  christos 
    105   1.2  christos 	TAILQ_INIT(&selhead);
    106   1.2  christos 	TAILQ_INIT(&opthead);
    107   1.2  christos 
    108  1.31       dsl 	while ((i = getopt(argc, argv, "dfl:npqT:t:vy")) != -1) {
    109   1.1  christos 		switch (i) {
    110   1.1  christos 		case 'd':
    111   1.4  christos 			flags |= CHECK_DEBUG;
    112  1.31       dsl 			continue;
    113  1.31       dsl 
    114  1.31       dsl 		case 'f':
    115  1.31       dsl 			flags |= CHECK_FORCE;
    116   1.1  christos 			break;
    117   1.1  christos 
    118  1.31       dsl 		case 'n':
    119  1.38  christos 			flags |= CHECK_NOFIX;
    120   1.1  christos 			break;
    121   1.1  christos 
    122   1.7  christos 		case 'p':
    123  1.31       dsl 			flags |= CHECK_PREEN;
    124  1.31       dsl 			break;
    125  1.31       dsl 
    126  1.37  christos 		case 'P':
    127  1.37  christos 			flags |= CHECK_PROGRESS;
    128  1.37  christos 			break;
    129  1.37  christos 
    130  1.31       dsl 		case 'q':
    131   1.2  christos 			break;
    132   1.2  christos 
    133   1.2  christos 		case 'l':
    134   1.2  christos 			maxrun = atoi(optarg);
    135  1.31       dsl 			continue;
    136   1.1  christos 
    137   1.2  christos 		case 'T':
    138   1.1  christos 			if (*optarg)
    139   1.2  christos 				addoption(optarg);
    140  1.31       dsl 			continue;
    141   1.1  christos 
    142   1.1  christos 		case 't':
    143  1.24     lukem 			if (TAILQ_FIRST(&selhead) != NULL)
    144   1.1  christos 				errx(1, "only one -t option may be specified.");
    145   1.2  christos 
    146   1.1  christos 			maketypelist(optarg);
    147   1.1  christos 			vfstype = optarg;
    148  1.31       dsl 			continue;
    149  1.31       dsl 
    150  1.31       dsl 		case 'v':
    151  1.31       dsl 			flags |= CHECK_VERBOSE;
    152  1.31       dsl 			continue;
    153  1.31       dsl 
    154  1.31       dsl 		case 'y':
    155   1.1  christos 			break;
    156   1.1  christos 
    157   1.1  christos 		case '?':
    158   1.1  christos 		default:
    159   1.1  christos 			usage();
    160   1.1  christos 			/* NOTREACHED */
    161   1.1  christos 		}
    162   1.1  christos 
    163  1.31       dsl 		/* Pass option to fsck_xxxfs */
    164  1.31       dsl 		globopt[1] = i;
    165  1.31       dsl 		catopt(&options, globopt);
    166  1.31       dsl 	}
    167  1.31       dsl 
    168  1.37  christos 	/* Don't do progress meters if we're debugging. */
    169  1.37  christos 	if (flags & CHECK_DEBUG)
    170  1.37  christos 		flags &= ~CHECK_PROGRESS;
    171  1.37  christos 
    172  1.37  christos 	/*
    173  1.37  christos 	 * If progress meters are being used, force max parallel to 1
    174  1.37  christos 	 * so the progress meter outputs don't interfere with one another.
    175  1.37  christos 	 */
    176  1.37  christos 	if (flags & CHECK_PROGRESS)
    177  1.37  christos 		maxrun = 1;
    178  1.37  christos 
    179   1.1  christos 	argc -= optind;
    180   1.1  christos 	argv += optind;
    181   1.1  christos 
    182   1.2  christos 	if (argc == 0)
    183   1.4  christos 		return checkfstab(flags, maxrun, isok, checkfs);
    184   1.2  christos 
    185   1.1  christos #define	BADTYPE(type)							\
    186   1.1  christos 	(strcmp(type, FSTAB_RO) &&					\
    187   1.1  christos 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    188   1.1  christos 
    189   1.1  christos 
    190   1.1  christos 	for (; argc--; argv++) {
    191  1.21       abs 		const char *spec, *type, *cp;
    192  1.21       abs 		char	device[MAXPATHLEN];
    193   1.1  christos 
    194  1.21       abs 		spec = *argv;
    195  1.21       abs 		cp = strrchr(spec, '/');
    196  1.21       abs 		if (cp == 0) {
    197  1.21       abs 			(void)snprintf(device, sizeof(device), "%s%s",
    198  1.21       abs 				_PATH_DEV, spec);
    199  1.21       abs 			spec = device;
    200  1.21       abs 		}
    201  1.21       abs 		if ((fs = getfsfile(spec)) == NULL &&
    202  1.21       abs 		    (fs = getfsspec(spec)) == NULL) {
    203   1.1  christos 			if (vfstype == NULL)
    204  1.21       abs 				vfstype = getfslab(spec);
    205   1.1  christos 			type = vfstype;
    206   1.1  christos 		}
    207   1.1  christos 		else {
    208   1.1  christos 			spec = fs->fs_spec;
    209   1.1  christos 			type = fs->fs_vfstype;
    210   1.1  christos 			if (BADTYPE(fs->fs_type))
    211   1.1  christos 				errx(1, "%s has unknown file system type.",
    212  1.21       abs 				    spec);
    213   1.1  christos 		}
    214   1.1  christos 
    215   1.6  christos 		rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL);
    216   1.1  christos 	}
    217   1.1  christos 
    218   1.1  christos 	return rval;
    219   1.1  christos }
    220   1.1  christos 
    221   1.2  christos 
    222   1.4  christos static void *
    223  1.25     lukem isok(struct fstab *fs)
    224   1.2  christos {
    225  1.25     lukem 
    226   1.2  christos 	if (fs->fs_passno == 0)
    227   1.4  christos 		return NULL;
    228   1.2  christos 
    229   1.2  christos 	if (BADTYPE(fs->fs_type))
    230   1.4  christos 		return NULL;
    231   1.2  christos 
    232   1.2  christos 	if (!selected(fs->fs_vfstype))
    233   1.4  christos 		return NULL;
    234   1.2  christos 
    235   1.4  christos 	return fs;
    236   1.2  christos }
    237   1.2  christos 
    238   1.2  christos 
    239   1.1  christos static int
    240  1.25     lukem checkfs(const char *vfstype, const char *spec, const char *mntpt, void *auxarg,
    241  1.25     lukem     pid_t *pidp)
    242   1.1  christos {
    243   1.1  christos 	/* List of directories containing fsck_xxx subcommands. */
    244   1.1  christos 	static const char *edirs[] = {
    245  1.35  christos #ifdef RESCUEDIR
    246  1.35  christos 		RESCUEDIR,
    247  1.29     lukem #endif
    248   1.1  christos 		_PATH_SBIN,
    249   1.1  christos 		_PATH_USRSBIN,
    250   1.1  christos 		NULL
    251   1.1  christos 	};
    252   1.7  christos 	const char **argv, **edir;
    253   1.1  christos 	pid_t pid;
    254  1.15   mycroft 	int argc, i, status, maxargc;
    255  1.15   mycroft 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
    256   1.2  christos 	const char *extra = getoptions(vfstype);
    257   1.1  christos 
    258   1.1  christos #ifdef __GNUC__
    259   1.1  christos 	/* Avoid vfork clobbering */
    260   1.1  christos 	(void) &optbuf;
    261   1.8  christos 	(void) &vfstype;
    262   1.1  christos #endif
    263   1.1  christos 
    264  1.15   mycroft 	if (!strcmp(vfstype, "ufs"))
    265   1.3       cgd 		vfstype = MOUNT_UFS;
    266   1.3       cgd 
    267  1.16   mycroft 	optbuf = NULL;
    268  1.16   mycroft 	if (options)
    269  1.16   mycroft 		catopt(&optbuf, options);
    270  1.16   mycroft 	if (extra)
    271  1.16   mycroft 		catopt(&optbuf, extra);
    272   1.2  christos 
    273  1.15   mycroft 	maxargc = 64;
    274  1.15   mycroft 	argv = emalloc(sizeof(char *) * maxargc);
    275  1.15   mycroft 
    276  1.15   mycroft 	(void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
    277  1.15   mycroft 	argc = 0;
    278  1.15   mycroft 	argv[argc++] = execbase;
    279   1.2  christos 	if (optbuf)
    280   1.7  christos 		mangle(optbuf, &argc, &argv, &maxargc);
    281   1.2  christos 	argv[argc++] = spec;
    282   1.7  christos 	argv[argc] = NULL;
    283   1.1  christos 
    284   1.4  christos 	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
    285   1.8  christos 		(void)printf("start %s %swait", mntpt,
    286   1.8  christos 			pidp ? "no" : "");
    287   1.8  christos 		for (i = 0; i < argc; i++)
    288   1.1  christos 			(void)printf(" %s", argv[i]);
    289   1.1  christos 		(void)printf("\n");
    290   1.1  christos 	}
    291   1.1  christos 
    292   1.1  christos 	switch (pid = vfork()) {
    293   1.1  christos 	case -1:				/* Error. */
    294   1.1  christos 		warn("vfork");
    295   1.1  christos 		if (optbuf)
    296   1.1  christos 			free(optbuf);
    297   1.1  christos 		return (1);
    298   1.1  christos 
    299   1.1  christos 	case 0:					/* Child. */
    300  1.26     lukem 		if ((flags & CHECK_FORCE) == 0) {
    301  1.34  christos 			struct statvfs	sfs;
    302  1.26     lukem 
    303  1.26     lukem 				/*
    304  1.26     lukem 				 * if mntpt is a mountpoint of a mounted file
    305  1.26     lukem 				 * system and it's mounted read-write, skip it
    306  1.26     lukem 				 * unless -f is given.
    307  1.26     lukem 				 */
    308  1.34  christos 			if ((statvfs(mntpt, &sfs) == 0) &&
    309  1.26     lukem 			    (strcmp(mntpt, sfs.f_mntonname) == 0) &&
    310  1.34  christos 			    ((sfs.f_flag & MNT_RDONLY) == 0)) {
    311  1.26     lukem 				printf(
    312  1.26     lukem 		"%s: file system is mounted read-write on %s; not checking\n",
    313  1.26     lukem 				    spec, mntpt);
    314  1.26     lukem 				if ((flags & CHECK_PREEN) && auxarg != NULL)
    315  1.26     lukem 					_exit(0);	/* fsck -p */
    316  1.26     lukem 				else
    317  1.26     lukem 					_exit(1);	/* fsck [[-p] ...] */
    318  1.26     lukem 			}
    319  1.26     lukem 		}
    320  1.26     lukem 
    321   1.4  christos 		if (flags & CHECK_DEBUG)
    322   1.2  christos 			_exit(0);
    323   1.2  christos 
    324   1.1  christos 		/* Go find an executable. */
    325   1.1  christos 		edir = edirs;
    326   1.1  christos 		do {
    327   1.1  christos 			(void)snprintf(execname,
    328   1.8  christos 			    sizeof(execname), "%s/%s", *edir, execbase);
    329   1.1  christos 			execv(execname, (char * const *)argv);
    330  1.19      ross 			if (errno != ENOENT) {
    331   1.1  christos 				if (spec)
    332   1.1  christos 					warn("exec %s for %s", execname, spec);
    333   1.1  christos 				else
    334   1.1  christos 					warn("exec %s", execname);
    335  1.19      ross 			}
    336   1.1  christos 		} while (*++edir != NULL);
    337   1.1  christos 
    338  1.19      ross 		if (errno == ENOENT) {
    339   1.1  christos 			if (spec)
    340   1.1  christos 				warn("exec %s for %s", execname, spec);
    341   1.1  christos 			else
    342   1.1  christos 				warn("exec %s", execname);
    343  1.19      ross 		}
    344   1.8  christos 		_exit(1);
    345   1.1  christos 		/* NOTREACHED */
    346   1.1  christos 
    347   1.1  christos 	default:				/* Parent. */
    348   1.1  christos 		if (optbuf)
    349   1.1  christos 			free(optbuf);
    350   1.1  christos 
    351   1.2  christos 		if (pidp) {
    352   1.2  christos 			*pidp = pid;
    353   1.2  christos 			return 0;
    354   1.2  christos 		}
    355   1.2  christos 
    356   1.1  christos 		if (waitpid(pid, &status, 0) < 0) {
    357   1.1  christos 			warn("waitpid");
    358   1.1  christos 			return (1);
    359   1.1  christos 		}
    360   1.1  christos 
    361   1.1  christos 		if (WIFEXITED(status)) {
    362   1.1  christos 			if (WEXITSTATUS(status) != 0)
    363   1.1  christos 				return (WEXITSTATUS(status));
    364   1.1  christos 		}
    365   1.1  christos 		else if (WIFSIGNALED(status)) {
    366   1.1  christos 			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
    367   1.1  christos 			return (1);
    368   1.1  christos 		}
    369   1.1  christos 		break;
    370   1.1  christos 	}
    371   1.1  christos 
    372   1.1  christos 	return (0);
    373   1.1  christos }
    374   1.1  christos 
    375   1.1  christos 
    376   1.1  christos static int
    377  1.25     lukem selected(const char *type)
    378   1.1  christos {
    379   1.1  christos 	struct entry *e;
    380   1.1  christos 
    381   1.1  christos 	/* If no type specified, it's always selected. */
    382  1.24     lukem 	TAILQ_FOREACH(e, &selhead, entries)
    383   1.1  christos 		if (!strncmp(e->type, type, MFSNAMELEN))
    384   1.1  christos 			return which == IN_LIST ? 1 : 0;
    385   1.1  christos 
    386   1.1  christos 	return which == IN_LIST ? 0 : 1;
    387   1.1  christos }
    388   1.1  christos 
    389   1.1  christos 
    390   1.2  christos static const char *
    391  1.25     lukem getoptions(const char *type)
    392   1.1  christos {
    393   1.1  christos 	struct entry *e;
    394   1.1  christos 
    395  1.24     lukem 	TAILQ_FOREACH(e, &opthead, entries)
    396   1.2  christos 		if (!strncmp(e->type, type, MFSNAMELEN))
    397   1.2  christos 			return e->options;
    398   1.2  christos 	return "";
    399   1.1  christos }
    400   1.1  christos 
    401   1.1  christos 
    402   1.1  christos static void
    403  1.25     lukem addoption(char *optstr)
    404   1.1  christos {
    405   1.2  christos 	char *newoptions;
    406   1.1  christos 	struct entry *e;
    407   1.1  christos 
    408   1.2  christos 	if ((newoptions = strchr(optstr, ':')) == NULL)
    409   1.2  christos 		errx(1, "Invalid option string");
    410   1.2  christos 
    411   1.2  christos 	*newoptions++ = '\0';
    412   1.2  christos 
    413  1.24     lukem 	TAILQ_FOREACH(e, &opthead, entries)
    414   1.2  christos 		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
    415  1.16   mycroft 			catopt(&e->options, newoptions);
    416   1.1  christos 			return;
    417   1.1  christos 		}
    418   1.2  christos 	addentry(&opthead, optstr, newoptions);
    419   1.1  christos }
    420   1.1  christos 
    421   1.1  christos 
    422   1.1  christos static void
    423  1.25     lukem addentry(struct fstypelist *list, const char *type, const char *opts)
    424   1.1  christos {
    425   1.2  christos 	struct entry *e;
    426   1.2  christos 
    427   1.2  christos 	e = emalloc(sizeof(struct entry));
    428   1.2  christos 	e->type = estrdup(type);
    429   1.2  christos 	e->options = estrdup(opts);
    430   1.2  christos 	TAILQ_INSERT_TAIL(list, e, entries);
    431   1.1  christos }
    432   1.1  christos 
    433   1.1  christos 
    434   1.1  christos static void
    435  1.25     lukem maketypelist(char *fslist)
    436   1.1  christos {
    437   1.1  christos 	char *ptr;
    438   1.1  christos 
    439   1.1  christos 	if ((fslist == NULL) || (fslist[0] == '\0'))
    440   1.1  christos 		errx(1, "empty type list");
    441   1.1  christos 
    442   1.1  christos 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    443   1.1  christos 		fslist += 2;
    444   1.1  christos 		which = NOT_IN_LIST;
    445   1.1  christos 	}
    446   1.1  christos 	else
    447   1.1  christos 		which = IN_LIST;
    448   1.1  christos 
    449   1.1  christos 	while ((ptr = strsep(&fslist, ",")) != NULL)
    450   1.2  christos 		addentry(&selhead, ptr, "");
    451   1.1  christos 
    452   1.1  christos }
    453   1.1  christos 
    454   1.1  christos 
    455  1.16   mycroft static void
    456  1.25     lukem catopt(char **sp, const char *o)
    457   1.1  christos {
    458  1.16   mycroft 	char *s;
    459  1.16   mycroft 	size_t i, j;
    460   1.1  christos 
    461  1.16   mycroft 	s = *sp;
    462  1.16   mycroft 	if (s) {
    463  1.16   mycroft 		i = strlen(s);
    464  1.16   mycroft 		j = i + 1 + strlen(o) + 1;
    465  1.16   mycroft 		s = erealloc(s, j);
    466  1.16   mycroft 		(void)snprintf(s + i, j, ",%s", o);
    467  1.15   mycroft 	} else
    468  1.16   mycroft 		s = estrdup(o);
    469  1.16   mycroft 	*sp = s;
    470   1.1  christos }
    471   1.1  christos 
    472   1.1  christos 
    473   1.1  christos static void
    474  1.27     lukem mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp)
    475   1.1  christos {
    476   1.1  christos 	char *p, *s;
    477  1.15   mycroft 	int argc, maxargc;
    478  1.15   mycroft 	const char **argv;
    479   1.1  christos 
    480   1.1  christos 	argc = *argcp;
    481  1.15   mycroft 	argv = *argvp;
    482   1.7  christos 	maxargc = *maxargcp;
    483   1.7  christos 
    484  1.27     lukem 	for (s = opts; (p = strsep(&s, ",")) != NULL;) {
    485  1.15   mycroft 		/* Always leave space for one more argument and the NULL. */
    486   1.7  christos 		if (argc >= maxargc - 3) {
    487  1.15   mycroft 			maxargc <<= 1;
    488   1.7  christos 			argv = erealloc(argv, maxargc * sizeof(char *));
    489   1.7  christos 		}
    490  1.19      ross 		if (*p != '\0')  {
    491   1.1  christos 			if (*p == '-') {
    492   1.1  christos 				argv[argc++] = p;
    493   1.1  christos 				p = strchr(p, '=');
    494   1.1  christos 				if (p) {
    495   1.1  christos 					*p = '\0';
    496   1.1  christos 					argv[argc++] = p+1;
    497   1.1  christos 				}
    498  1.15   mycroft 			} else {
    499   1.1  christos 				argv[argc++] = "-o";
    500   1.1  christos 				argv[argc++] = p;
    501   1.1  christos 			}
    502  1.19      ross 		}
    503   1.7  christos 	}
    504   1.1  christos 
    505   1.1  christos 	*argcp = argc;
    506   1.7  christos 	*argvp = argv;
    507   1.7  christos 	*maxargcp = maxargc;
    508   1.1  christos }
    509   1.8  christos 
    510  1.17   mycroft const static char *
    511  1.25     lukem getfslab(const char *str)
    512   1.8  christos {
    513  1.36   thorpej 	static struct dkwedge_info dkw;
    514   1.8  christos 	struct disklabel dl;
    515   1.8  christos 	int fd;
    516  1.17   mycroft 	char p;
    517  1.17   mycroft 	const char *vfstype;
    518   1.8  christos 	u_char t;
    519   1.8  christos 
    520  1.26     lukem 	/* deduce the file system type from the disk label */
    521   1.8  christos 	if ((fd = open(str, O_RDONLY)) == -1)
    522   1.8  christos 		err(1, "cannot open `%s'", str);
    523   1.8  christos 
    524  1.36   thorpej 	/* First check to see if it's a wedge. */
    525  1.36   thorpej 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
    526  1.36   thorpej 		/* Yup, this is easy. */
    527  1.36   thorpej 		(void) close(fd);
    528  1.36   thorpej 		return (dkw.dkw_ptype);
    529  1.36   thorpej 	}
    530  1.36   thorpej 
    531   1.8  christos 	if (ioctl(fd, DIOCGDINFO, &dl) == -1)
    532   1.8  christos 		err(1, "cannot get disklabel for `%s'", str);
    533   1.8  christos 
    534   1.8  christos 	(void) close(fd);
    535   1.8  christos 
    536   1.8  christos 	p = str[strlen(str) - 1];
    537   1.8  christos 
    538   1.8  christos 	if ((p - 'a') >= dl.d_npartitions)
    539   1.8  christos 		errx(1, "partition `%s' is not defined on disk", str);
    540   1.8  christos 
    541  1.13    bouyer 	if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
    542   1.8  christos 		errx(1, "partition `%s' is not of a legal vfstype",
    543  1.11     mikel 		    str);
    544   1.8  christos 
    545   1.8  christos 	if ((vfstype = fscknames[t]) == NULL)
    546   1.8  christos 		errx(1, "vfstype `%s' on partition `%s' is not supported",
    547   1.8  christos 		    fstypenames[t], str);
    548   1.8  christos 
    549   1.8  christos 	return vfstype;
    550   1.8  christos }
    551   1.1  christos 
    552   1.1  christos 
    553   1.1  christos static void
    554  1.25     lukem usage(void)
    555   1.1  christos {
    556   1.2  christos 	static const char common[] =
    557  1.31       dsl 	    "[-dfnpqvy] [-T fstype:fsoptions] [-t fstype]";
    558   1.1  christos 
    559  1.32      jmmv 	(void)fprintf(stderr, "usage: %s %s [special|node]...\n",
    560  1.23       cgd 	    getprogname(), common);
    561   1.1  christos 	exit(1);
    562   1.1  christos }
    563