Home | History | Annotate | Line # | Download | only in newfs
newfs.c revision 1.41
      1 /*	$NetBSD: newfs.c,v 1.41 2000/12/01 12:48:09 simonb Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)newfs.c	8.13 (Berkeley) 5/1/95";
     45 #else
     46 __RCSID("$NetBSD: newfs.c,v 1.41 2000/12/01 12:48:09 simonb Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 /*
     51  * newfs: friendly front end to mkfs
     52  */
     53 #include <sys/param.h>
     54 #include <sys/stat.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/disklabel.h>
     57 #include <sys/file.h>
     58 #include <sys/mount.h>
     59 #include <sys/sysctl.h>
     60 #include <sys/wait.h>
     61 
     62 #include <ufs/ufs/dir.h>
     63 #include <ufs/ufs/dinode.h>
     64 #include <ufs/ufs/ufsmount.h>
     65 #include <ufs/ffs/fs.h>
     66 
     67 #include <ctype.h>
     68 #include <disktab.h>
     69 #include <errno.h>
     70 #include <paths.h>
     71 #include <stdio.h>
     72 #include <stdlib.h>
     73 #include <string.h>
     74 #include <syslog.h>
     75 #include <unistd.h>
     76 #include <signal.h>
     77 #include <err.h>
     78 #include <util.h>
     79 
     80 #include "mntopts.h"
     81 #include "dkcksum.h"
     82 #include "extern.h"
     83 
     84 struct mntopt mopts[] = {
     85 	MOPT_STDOPTS,
     86 	MOPT_ASYNC,
     87 	MOPT_UPDATE,
     88 	MOPT_NOATIME,
     89 	{ NULL },
     90 };
     91 
     92 static struct disklabel *getdisklabel(char *, int);
     93 static void rewritelabel(char *, int, struct disklabel *);
     94 static void usage(void);
     95 int main(int, char *[]);
     96 
     97 #define	COMPAT			/* allow non-labeled disks */
     98 
     99 /*
    100  * The following two constants set the default block and fragment sizes.
    101  * Both constants must be a power of 2 and meet the following constraints:
    102  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
    103  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
    104  *	DESBLKSIZE / DESFRAGSIZE <= 8
    105  */
    106 #define	DFL_FRAGSIZE	1024
    107 #define	DFL_BLKSIZE	8192
    108 
    109 /*
    110  * Cylinder groups may have up to many cylinders. The actual
    111  * number used depends upon how much information can be stored
    112  * on a single cylinder. The default is to use 16 cylinders
    113  * per group.
    114  */
    115 #define	DESCPG		16	/* desired fs_cpg */
    116 
    117 /*
    118  * ROTDELAY gives the minimum number of milliseconds to initiate
    119  * another disk transfer on the same cylinder. It is used in
    120  * determining the rotationally optimal layout for disk blocks
    121  * within a file; the default of fs_rotdelay is 0ms.
    122  */
    123 #define ROTDELAY	0
    124 
    125 /*
    126  * MAXBLKPG determines the maximum number of data blocks which are
    127  * placed in a single cylinder group. The default is one indirect
    128  * block worth of data blocks.
    129  */
    130 #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
    131 
    132 /*
    133  * Each file system has a number of inodes statically allocated.
    134  * We allocate one inode slot per NFPI fragments, expecting this
    135  * to be far more than we will ever need.
    136  */
    137 #define	NFPI		4
    138 
    139 /*
    140  * For each cylinder we keep track of the availability of blocks at different
    141  * rotational positions, so that we can lay out the data to be picked
    142  * up with minimum rotational latency.  NRPOS is the default number of
    143  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
    144  * of our summary information is 2ms for a typical 3600 rpm drive.  Caching
    145  * and zoning pretty much defeats rotational optimization, so we now use a
    146  * default of 1.
    147  */
    148 #define	NRPOS		1	/* number distinct rotational positions */
    149 
    150 
    151 int	mfs;			/* run as the memory based filesystem */
    152 int	Nflag;			/* run without writing file system */
    153 int	Oflag;			/* format as an 4.3BSD file system */
    154 int	fssize;			/* file system size */
    155 int	ntracks;		/* # tracks/cylinder */
    156 int	nsectors;		/* # sectors/track */
    157 int	nphyssectors;		/* # sectors/track including spares */
    158 int	secpercyl;		/* sectors per cylinder */
    159 int	trackspares = -1;	/* spare sectors per track */
    160 int	cylspares = -1;		/* spare sectors per cylinder */
    161 int	sectorsize;		/* bytes/sector */
    162 int	rpm;			/* revolutions/minute of drive */
    163 int	interleave;		/* hardware sector interleave */
    164 int	trackskew = -1;		/* sector 0 skew, per track */
    165 int	headswitch;		/* head switch time, usec */
    166 int	trackseek;		/* track-to-track seek, usec */
    167 int	fsize = 0;		/* fragment size */
    168 int	bsize = 0;		/* block size */
    169 int	cpg = DESCPG;		/* cylinders/cylinder group */
    170 int	cpgflg;			/* cylinders/cylinder group flag was given */
    171 int	minfree = MINFREE;	/* free space threshold */
    172 int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
    173 int	density;		/* number of bytes per inode */
    174 int	maxcontig = 0;		/* max contiguous blocks to allocate */
    175 int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
    176 int	maxbpg;			/* maximum blocks per file in a cyl group */
    177 int	nrpos = NRPOS;		/* # of distinguished rotational positions */
    178 int	bbsize = BBSIZE;	/* boot block size */
    179 int	sbsize = SBSIZE;	/* superblock size */
    180 int	mntflags = MNT_ASYNC;	/* flags to be passed to mount */
    181 u_long	memleft;		/* virtual memory available */
    182 caddr_t	membase;		/* start address of memory based filesystem */
    183 int needswap =0;		/* Filesystem not in native byte order */
    184 #ifdef COMPAT
    185 char	*disktype;
    186 int	unlabeled;
    187 #endif
    188 
    189 char	device[MAXPATHLEN];
    190 extern char *__progname;
    191 
    192 int
    193 main(int argc, char *argv[])
    194 {
    195 	struct partition *pp;
    196 	struct disklabel *lp;
    197 	struct disklabel mfsfakelabel;
    198 	struct partition oldpartition;
    199 	struct stat st;
    200 	struct statfs *mp;
    201 	int ch, fsi = 0, fso, len, maxpartitions, n;
    202 	char *cp = NULL, *endp, *opstring, *s1, *s2, *special;
    203 #ifdef MFS
    204 	char mountfromname[100];
    205 	pid_t pid, res;
    206 	struct statfs sf;
    207 	int status;
    208 #endif
    209 
    210 	if (strstr(__progname, "mfs")) {
    211 		mfs = 1;
    212 		Nflag++;
    213 	}
    214 
    215 	maxpartitions = getmaxpartitions();
    216 	if (maxpartitions > 26)
    217 		errx(1, "insane maxpartitions value %d", maxpartitions);
    218 
    219 	opstring = mfs ?
    220 	    "NT:a:b:c:d:e:f:i:m:o:s:" :
    221 	    "B:NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
    222 	while ((ch = getopt(argc, argv, opstring)) != -1)
    223 		switch (ch) {
    224 		case 'B':
    225 			if (strcmp(optarg, "be") == 0) {
    226 #if BYTE_ORDER == LITTLE_ENDIAN
    227 				needswap = 1;
    228 #endif
    229 			} else if (strcmp(optarg, "le") == 0) {
    230 #if BYTE_ORDER == BIG_ENDIAN
    231 				needswap = 1;
    232 #endif
    233 			} else
    234 				usage();
    235 			break;
    236 		case 'N':
    237 			Nflag = 1;
    238 			break;
    239 		case 'O':
    240 			Oflag = 1;
    241 			break;
    242 		case 'S':
    243 			if ((sectorsize = atoi(optarg)) <= 0)
    244 				errx(1, "%s: bad sector size", optarg);
    245 			break;
    246 #ifdef COMPAT
    247 		case 'T':
    248 			disktype = optarg;
    249 			break;
    250 #endif
    251 		case 'a':
    252 			if ((maxcontig = atoi(optarg)) <= 0)
    253 				errx(1, "%s: bad maximum contiguous blocks",
    254 				    optarg);
    255 			break;
    256 		case 'b':
    257 			if ((bsize = atoi(optarg)) < MINBSIZE)
    258 				errx(1, "%s: bad block size", optarg);
    259 			break;
    260 		case 'c':
    261 			if ((cpg = atoi(optarg)) <= 0)
    262 				errx(1, "%s: bad cylinders/group", optarg);
    263 			cpgflg++;
    264 			break;
    265 		case 'd':
    266 			if ((rotdelay = atoi(optarg)) < 0)
    267 				errx(1, "%s: bad rotational delay", optarg);
    268 			break;
    269 		case 'e':
    270 			if ((maxbpg = atoi(optarg)) <= 0)
    271 		errx(1, "%s: bad blocks per file in a cylinder group",
    272 				    optarg);
    273 			break;
    274 		case 'f':
    275 			if ((fsize = atoi(optarg)) <= 0)
    276 				errx(1, "%s: bad fragment size", optarg);
    277 			break;
    278 		case 'i':
    279 			if ((density = atoi(optarg)) <= 0)
    280 				errx(1, "%s: bad bytes per inode", optarg);
    281 			break;
    282 		case 'k':
    283 			if ((trackskew = atoi(optarg)) < 0)
    284 				errx(1, "%s: bad track skew", optarg);
    285 			break;
    286 		case 'l':
    287 			if ((interleave = atoi(optarg)) <= 0)
    288 				errx(1, "%s: bad interleave", optarg);
    289 			break;
    290 		case 'm':
    291 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
    292 				errx(1, "%s: bad free space %%", optarg);
    293 			break;
    294 		case 'n':
    295 			if ((nrpos = atoi(optarg)) <= 0)
    296 				errx(1, "%s: bad rotational layout count",
    297 				    optarg);
    298 			break;
    299 		case 'o':
    300 			if (mfs)
    301 				getmntopts(optarg, mopts, &mntflags, 0);
    302 			else {
    303 				if (strcmp(optarg, "space") == 0)
    304 					opt = FS_OPTSPACE;
    305 				else if (strcmp(optarg, "time") == 0)
    306 					opt = FS_OPTTIME;
    307 				else
    308 				    errx(1, "%s %s",
    309 					"unknown optimization preference: ",
    310 					"use `space' or `time'.");
    311 			}
    312 			break;
    313 		case 'p':
    314 			if ((trackspares = atoi(optarg)) < 0)
    315 				errx(1, "%s: bad spare sectors per track",
    316 				    optarg);
    317 			break;
    318 		case 'r':
    319 			if ((rpm = atoi(optarg)) <= 0)
    320 				errx(1, "%s: bad revolutions/minute", optarg);
    321 			break;
    322 		case 's':
    323 			fssize = (int)strtol(optarg, &endp, 10);
    324 			if (*endp) {
    325 				if (mfs) {	/* Only do 'm' for mfs */
    326 					if (*endp == 'm' || *endp == 'M')
    327 						fssize *= 1024 * 1024 / 512;
    328 					else
    329 						fssize = -1;	/* error */
    330 				}
    331 				else
    332 					fssize = -1;	/* error */
    333 			}
    334 			if (fssize <= 0)
    335 				errx(1, "%s: bad file system size", optarg);
    336 			break;
    337 		case 't':
    338 			if ((ntracks = atoi(optarg)) <= 0)
    339 				errx(1, "%s: bad total tracks", optarg);
    340 			break;
    341 		case 'u':
    342 			if ((nsectors = atoi(optarg)) <= 0)
    343 				errx(1, "%s: bad sectors/track", optarg);
    344 			break;
    345 		case 'x':
    346 			if ((cylspares = atoi(optarg)) < 0)
    347 				errx(1, "%s: bad spare sectors per cylinder",
    348 				    optarg);
    349 			break;
    350 		case '?':
    351 		default:
    352 			usage();
    353 		}
    354 	argc -= optind;
    355 	argv += optind;
    356 
    357 	if (argc != 2 && (mfs || argc != 1))
    358 		usage();
    359 
    360 	special = argv[0];
    361 	if (mfs && !strcmp(special, "swap")) {
    362 		/*
    363 		 * it's an MFS, mounted on "swap."  fake up a label.
    364 		 * XXX XXX XXX
    365 		 */
    366 		fso = -1;	/* XXX; normally done below. */
    367 
    368 		memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
    369 		mfsfakelabel.d_secsize = 512;
    370 		mfsfakelabel.d_nsectors = 64;
    371 		mfsfakelabel.d_ntracks = 16;
    372 		mfsfakelabel.d_ncylinders = 16;
    373 		mfsfakelabel.d_secpercyl = 1024;
    374 		mfsfakelabel.d_secperunit = 16384;
    375 		mfsfakelabel.d_rpm = 3600;
    376 		mfsfakelabel.d_interleave = 1;
    377 		mfsfakelabel.d_npartitions = 1;
    378 		mfsfakelabel.d_partitions[0].p_size = 16384;
    379 		mfsfakelabel.d_partitions[0].p_fsize = 1024;
    380 		mfsfakelabel.d_partitions[0].p_frag = 8;
    381 		mfsfakelabel.d_partitions[0].p_cpg = 16;
    382 
    383 		lp = &mfsfakelabel;
    384 		pp = &mfsfakelabel.d_partitions[0];
    385 
    386 		goto havelabel;
    387 	}
    388 	cp = strrchr(special, '/');
    389 	if (cp == 0) {
    390 		/*
    391 		 * No path prefix; try /dev/r%s then /dev/%s.
    392 		 */
    393 		(void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
    394 		    special);
    395 		if (stat(device, &st) == -1)
    396 			(void)snprintf(device, sizeof(device), "%s%s",
    397 			    _PATH_DEV, special);
    398 		special = device;
    399 	}
    400 	if (Nflag) {
    401 		fso = -1;
    402 	} else {
    403 		fso = open(special, O_WRONLY);
    404 		if (fso < 0)
    405 			err(1, "%s: open", special);
    406 
    407 		/* Bail if target special is mounted */
    408 		n = getmntinfo(&mp, MNT_NOWAIT);
    409 		if (n == 0)
    410 			err(1, "%s: getmntinfo", special);
    411 
    412 		len = sizeof(_PATH_DEV) - 1;
    413 		s1 = special;
    414 		if (strncmp(_PATH_DEV, s1, len) == 0)
    415 			s1 += len;
    416 
    417 		while (--n >= 0) {
    418 			s2 = mp->f_mntfromname;
    419 			if (strncmp(_PATH_DEV, s2, len) == 0) {
    420 				s2 += len - 1;
    421 				*s2 = 'r';
    422 			}
    423 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
    424 				errx(1, "%s is mounted on %s",
    425 				    special, mp->f_mntonname);
    426 			++mp;
    427 		}
    428 	}
    429 	if (mfs && disktype != NULL) {
    430 		lp = (struct disklabel *)getdiskbyname(disktype);
    431 		if (lp == NULL)
    432 			errx(1, "%s: unknown disk type", disktype);
    433 		pp = &lp->d_partitions[1];
    434 	} else {
    435 		fsi = open(special, O_RDONLY);
    436 		if (fsi < 0)
    437 			err(1, "%s: open", special);
    438 		if (fstat(fsi, &st) < 0)
    439 			err(1, "%s: fstat", special);
    440 		if (!S_ISCHR(st.st_mode) && !mfs)
    441 			warnx("%s: not a character-special device", special);
    442 		cp = strchr(argv[0], '\0') - 1;
    443 		if (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
    444 		    && !isdigit(*cp)))
    445 			errx(1, "can't figure out file system partition");
    446 #ifdef COMPAT
    447 		if (!mfs && disktype == NULL)
    448 			disktype = argv[1];
    449 #endif
    450 		lp = getdisklabel(special, fsi);
    451 		if (isdigit(*cp))
    452 			pp = &lp->d_partitions[0];
    453 		else
    454 			pp = &lp->d_partitions[*cp - 'a'];
    455 		if (pp->p_size == 0)
    456 			errx(1, "`%c' partition is unavailable", *cp);
    457 		if (pp->p_fstype == FS_BOOT)
    458 			errx(1, "`%c' partition overlaps boot program", *cp);
    459 	}
    460 havelabel:
    461 	if (fssize == 0)
    462 		fssize = pp->p_size;
    463 	if (fssize > pp->p_size && !mfs)
    464 		errx(1, "maximum file system size on the `%c' partition is %d",
    465 		    *cp, pp->p_size);
    466 	if (rpm == 0) {
    467 		rpm = lp->d_rpm;
    468 		if (rpm <= 0)
    469 			rpm = 3600;
    470 	}
    471 	if (ntracks == 0) {
    472 		ntracks = lp->d_ntracks;
    473 		if (ntracks <= 0)
    474 			errx(1, "no default #tracks");
    475 	}
    476 	if (nsectors == 0) {
    477 		nsectors = lp->d_nsectors;
    478 		if (nsectors <= 0)
    479 			errx(1, "no default #sectors/track");
    480 	}
    481 	if (sectorsize == 0) {
    482 		sectorsize = lp->d_secsize;
    483 		if (sectorsize <= 0)
    484 			errx(1, "no default sector size");
    485 	}
    486 	if (trackskew == -1) {
    487 		trackskew = lp->d_trackskew;
    488 		if (trackskew < 0)
    489 			trackskew = 0;
    490 	}
    491 	if (interleave == 0) {
    492 		interleave = lp->d_interleave;
    493 		if (interleave <= 0)
    494 			interleave = 1;
    495 	}
    496 	if (fsize == 0) {
    497 		fsize = pp->p_fsize;
    498 		if (fsize <= 0)
    499 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
    500 	}
    501 	if (bsize == 0) {
    502 		bsize = pp->p_frag * pp->p_fsize;
    503 		if (bsize <= 0)
    504 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
    505 	}
    506 	if (cpgflg == 0) {
    507 		if (pp->p_cpg != 0)
    508 			cpg = pp->p_cpg;
    509 	}
    510 	/*
    511 	 * Maxcontig sets the default for the maximum number of blocks
    512 	 * that may be allocated sequentially. With filesystem clustering
    513 	 * it is possible to allocate contiguous blocks up to the maximum
    514 	 * transfer size permitted by the controller or buffering.
    515 	 */
    516 	if (maxcontig == 0)
    517 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
    518 	if (density == 0)
    519 		density = NFPI * fsize;
    520 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
    521 		warnx("%s %s %d%%", "Warning: changing optimization to space",
    522 		    "because minfree is less than", MINFREE);
    523 		opt = FS_OPTSPACE;
    524 	}
    525 	if (trackspares == -1) {
    526 		trackspares = lp->d_sparespertrack;
    527 		if (trackspares < 0)
    528 			trackspares = 0;
    529 	}
    530 	nphyssectors = nsectors + trackspares;
    531 	if (cylspares == -1) {
    532 		cylspares = lp->d_sparespercyl;
    533 		if (cylspares < 0)
    534 			cylspares = 0;
    535 	}
    536 	secpercyl = nsectors * ntracks - cylspares;
    537 	if (secpercyl != lp->d_secpercyl)
    538 		warnx("%s (%d) %s (%u)\n",
    539 			"Warning: calculated sectors per cylinder", secpercyl,
    540 			"disagrees with disk label", lp->d_secpercyl);
    541 	if (maxbpg == 0)
    542 		maxbpg = MAXBLKPG(bsize);
    543 	headswitch = lp->d_headswitch;
    544 	trackseek = lp->d_trkseek;
    545 #ifdef notdef /* label may be 0 if faked up by kernel */
    546 	bbsize = lp->d_bbsize;
    547 	sbsize = lp->d_sbsize;
    548 #endif
    549 	oldpartition = *pp;
    550 	mkfs(pp, special, fsi, fso);
    551 	if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)))
    552 		rewritelabel(special, fso, lp);
    553 	if (!Nflag)
    554 		close(fso);
    555 	close(fsi);
    556 #ifdef MFS
    557 	if (mfs) {
    558 		struct mfs_args args;
    559 
    560 		switch (pid = fork()) {
    561 		case -1:
    562 			perror("mfs");
    563 			exit(10);
    564 		case 0:
    565 			(void)snprintf(mountfromname, sizeof(mountfromname),
    566 			    "mfs:%d", getpid());
    567 			break;
    568 		default:
    569 			(void)snprintf(mountfromname, sizeof(mountfromname),
    570 			    "mfs:%d", pid);
    571 			for (;;) {
    572 				/*
    573 				 * spin until the mount succeeds
    574 				 * or the child exits
    575 				 */
    576 				usleep(1);
    577 
    578 				/*
    579 				 * XXX Here is a race condition: another process
    580 				 * can mount a filesystem which hides our
    581 				 * ramdisk before we see the success.
    582 				 */
    583 				if (statfs(argv[1], &sf) < 0)
    584 					err(88, "statfs %s", argv[1]);
    585 				if (!strcmp(sf.f_mntfromname, mountfromname) &&
    586 				    !strncmp(sf.f_mntonname, argv[1],
    587 					     MNAMELEN) &&
    588 				    !strcmp(sf.f_fstypename, "mfs"))
    589 					exit(0);
    590 
    591 				res = waitpid(pid, &status, WNOHANG);
    592 				if (res == -1)
    593 					err(11, "waitpid");
    594 				if (res != pid)
    595 					continue;
    596 				if (WIFEXITED(status)) {
    597 					if (WEXITSTATUS(status) == 0)
    598 						exit(0);
    599 					errx(1, "%s: mount: %s", argv[1],
    600 					     strerror(WEXITSTATUS(status)));
    601 				} else
    602 					errx(11, "abnormal termination");
    603 			}
    604 			/* NOTREACHED */
    605 		}
    606 
    607 		(void) setsid();
    608 		(void) close(0);
    609 		(void) close(1);
    610 		(void) close(2);
    611 		(void) chdir("/");
    612 
    613 		args.fspec = mountfromname;
    614 		args.export.ex_root = -2;
    615 		if (mntflags & MNT_RDONLY)
    616 			args.export.ex_flags = MNT_EXRDONLY;
    617 		else
    618 			args.export.ex_flags = 0;
    619 		args.base = membase;
    620 		args.size = fssize * sectorsize;
    621 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
    622 			exit(errno); /* parent prints message */
    623 	}
    624 #endif
    625 	exit(0);
    626 }
    627 
    628 #ifdef COMPAT
    629 const char lmsg[] = "%s: can't read disk label; disk type must be specified";
    630 #else
    631 const char lmsg[] = "%s: can't read disk label";
    632 #endif
    633 
    634 static struct disklabel *
    635 getdisklabel(char *s, volatile int fd)
    636 /* XXX why is fs volatile?! */
    637 {
    638 	static struct disklabel lab;
    639 
    640 	if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
    641 #ifdef COMPAT
    642 		if (disktype) {
    643 			struct disklabel *lp;
    644 
    645 			unlabeled++;
    646 			lp = getdiskbyname(disktype);
    647 			if (lp == NULL)
    648 				errx(1, "%s: unknown disk type", disktype);
    649 			return (lp);
    650 		}
    651 #endif
    652 		warn("ioctl (GDINFO)");
    653 		errx(1, lmsg, s);
    654 	}
    655 	return (&lab);
    656 }
    657 
    658 static void
    659 rewritelabel(char *s, volatile int fd, struct disklabel *lp)
    660 /* XXX why is fd volatile?! */
    661 {
    662 #ifdef COMPAT
    663 	if (unlabeled)
    664 		return;
    665 #endif
    666 	lp->d_checksum = 0;
    667 	lp->d_checksum = dkcksum(lp);
    668 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    669 		warn("ioctl (WDINFO)");
    670 		errx(1, "%s: can't rewrite disk label", s);
    671 	}
    672 #if __vax__
    673 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    674 		int i;
    675 		int cfd;
    676 		daddr_t alt;
    677 		char specname[64];
    678 		char blk[1024];
    679 		char *cp;
    680 
    681 		/*
    682 		 * Make name for 'c' partition.
    683 		 */
    684 		strcpy(specname, s);
    685 		cp = specname + strlen(specname) - 1;
    686 		if (!isdigit(*cp))
    687 			*cp = 'c';
    688 		cfd = open(specname, O_WRONLY);
    689 		if (cfd < 0)
    690 			err(1, "%s: open", specname);
    691 		memset(blk, 0, sizeof(blk));
    692 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
    693 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    694 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    695 			off_t offset;
    696 
    697 			offset = alt + i;
    698 			offset *= lp->d_secsize;
    699 			if (lseek(cfd, offset, SEEK_SET) == -1)
    700 				err(1, "lseek to badsector area: ");
    701 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    702 				warn("alternate label %d write", i/2);
    703 		}
    704 		close(cfd);
    705 	}
    706 #endif
    707 }
    708 
    709 static void
    710 usage(void)
    711 {
    712 	if (mfs) {
    713 		fprintf(stderr,
    714 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
    715 			__progname);
    716 	} else
    717 		fprintf(stderr,
    718 		    "usage: %s [ -fsoptions ] special-device%s\n",
    719 		    __progname,
    720 #ifdef COMPAT
    721 		    " [device-type]");
    722 #else
    723 		    "");
    724 #endif
    725 	fprintf(stderr, "where fsoptions are:\n");
    726 	fprintf(stderr, "\t-B byte order (`be' or `le')\n");
    727 	fprintf(stderr,
    728 	    "\t-N do not create file system, just print out parameters\n");
    729 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
    730 	fprintf(stderr, "\t-S sector size\n");
    731 #ifdef COMPAT
    732 	fprintf(stderr, "\t-T disktype\n");
    733 #endif
    734 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    735 	fprintf(stderr, "\t-b block size\n");
    736 	fprintf(stderr, "\t-c cylinders/group\n");
    737 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    738 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    739 	fprintf(stderr, "\t-f frag size\n");
    740 	fprintf(stderr, "\t-i number of bytes per inode\n");
    741 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
    742 	fprintf(stderr, "\t-l hardware sector interleave\n");
    743 	fprintf(stderr, "\t-m minimum free space %%\n");
    744 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
    745 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    746 	fprintf(stderr, "\t-p spare sectors per track\n");
    747 	fprintf(stderr, "\t-s file system size (sectors)\n");
    748 	fprintf(stderr, "\t-r revolutions/minute\n");
    749 	fprintf(stderr, "\t-t tracks/cylinder\n");
    750 	fprintf(stderr, "\t-u sectors/track\n");
    751 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
    752 	exit(1);
    753 }
    754