Home | History | Annotate | Line # | Download | only in newfs
newfs.c revision 1.37
      1 /*	$NetBSD: newfs.c,v 1.37 1999/01/19 19:34:50 tron 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.37 1999/01/19 19:34:50 tron 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 __P((char *, int));
     93 static void rewritelabel __P((char *, int, struct disklabel *));
     94 static void usage __P((void));
     95 int	main __P((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(argc, argv)
    194 	int argc;
    195 	char *argv[];
    196 {
    197 	int ch;
    198 	struct partition *pp;
    199 	struct disklabel *lp;
    200 	struct disklabel mfsfakelabel;
    201 	struct partition oldpartition;
    202 	struct stat st;
    203 	struct statfs *mp;
    204 	int fsi = 0, fso, len, n, maxpartitions;
    205 	char *cp = NULL, *s1, *s2, *special, *opstring;
    206 #ifdef MFS
    207 	char mountfromname[100];
    208 	pid_t pid, res;
    209 	struct statfs sf;
    210 	int status;
    211 #endif
    212 
    213 	if (strstr(__progname, "mfs")) {
    214 		mfs = 1;
    215 		Nflag++;
    216 	}
    217 
    218 	maxpartitions = getmaxpartitions();
    219 	if (maxpartitions > 26)
    220 		errx(1, "insane maxpartitions value %d", maxpartitions);
    221 
    222 	opstring = mfs ?
    223 	    "NT:a:b:c:d:e:f:i:m:o:s:" :
    224 	    "B:NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
    225 	while ((ch = getopt(argc, argv, opstring)) != -1)
    226 		switch (ch) {
    227 		case 'B':
    228 			if (strcmp(optarg, "be") == 0) {
    229 #if BYTE_ORDER == LITTLE_ENDIAN
    230 				needswap = 1;
    231 #endif
    232 			} else if (strcmp(optarg, "le") == 0) {
    233 #if BYTE_ORDER == BIG_ENDIAN
    234 				needswap = 1;
    235 #endif
    236 			} else
    237 				usage();
    238 			break;
    239 		case 'N':
    240 			Nflag = 1;
    241 			break;
    242 		case 'O':
    243 			Oflag = 1;
    244 			break;
    245 		case 'S':
    246 			if ((sectorsize = atoi(optarg)) <= 0)
    247 				errx(1, "%s: bad sector size", optarg);
    248 			break;
    249 #ifdef COMPAT
    250 		case 'T':
    251 			disktype = optarg;
    252 			break;
    253 #endif
    254 		case 'a':
    255 			if ((maxcontig = atoi(optarg)) <= 0)
    256 				errx(1, "%s: bad maximum contiguous blocks",
    257 				    optarg);
    258 			break;
    259 		case 'b':
    260 			if ((bsize = atoi(optarg)) < MINBSIZE)
    261 				errx(1, "%s: bad block size", optarg);
    262 			break;
    263 		case 'c':
    264 			if ((cpg = atoi(optarg)) <= 0)
    265 				errx(1, "%s: bad cylinders/group", optarg);
    266 			cpgflg++;
    267 			break;
    268 		case 'd':
    269 			if ((rotdelay = atoi(optarg)) < 0)
    270 				errx(1, "%s: bad rotational delay", optarg);
    271 			break;
    272 		case 'e':
    273 			if ((maxbpg = atoi(optarg)) <= 0)
    274 		errx(1, "%s: bad blocks per file in a cylinder group",
    275 				    optarg);
    276 			break;
    277 		case 'f':
    278 			if ((fsize = atoi(optarg)) <= 0)
    279 				errx(1, "%s: bad fragment size", optarg);
    280 			break;
    281 		case 'i':
    282 			if ((density = atoi(optarg)) <= 0)
    283 				errx(1, "%s: bad bytes per inode", optarg);
    284 			break;
    285 		case 'k':
    286 			if ((trackskew = atoi(optarg)) < 0)
    287 				errx(1, "%s: bad track skew", optarg);
    288 			break;
    289 		case 'l':
    290 			if ((interleave = atoi(optarg)) <= 0)
    291 				errx(1, "%s: bad interleave", optarg);
    292 			break;
    293 		case 'm':
    294 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
    295 				errx(1, "%s: bad free space %%", optarg);
    296 			break;
    297 		case 'n':
    298 			if ((nrpos = atoi(optarg)) <= 0)
    299 				errx(1, "%s: bad rotational layout count",
    300 				    optarg);
    301 			break;
    302 		case 'o':
    303 			if (mfs)
    304 				getmntopts(optarg, mopts, &mntflags, 0);
    305 			else {
    306 				if (strcmp(optarg, "space") == 0)
    307 					opt = FS_OPTSPACE;
    308 				else if (strcmp(optarg, "time") == 0)
    309 					opt = FS_OPTTIME;
    310 				else
    311 				    errx(1, "%s %s",
    312 					"unknown optimization preference: ",
    313 					"use `space' or `time'.");
    314 			}
    315 			break;
    316 		case 'p':
    317 			if ((trackspares = atoi(optarg)) < 0)
    318 				errx(1, "%s: bad spare sectors per track",
    319 				    optarg);
    320 			break;
    321 		case 'r':
    322 			if ((rpm = atoi(optarg)) <= 0)
    323 				errx(1, "%s: bad revolutions/minute", optarg);
    324 			break;
    325 		case 's':
    326 			if ((fssize = atoi(optarg)) <= 0)
    327 				errx(1, "%s: bad file system size", optarg);
    328 			break;
    329 		case 't':
    330 			if ((ntracks = atoi(optarg)) <= 0)
    331 				errx(1, "%s: bad total tracks", optarg);
    332 			break;
    333 		case 'u':
    334 			if ((nsectors = atoi(optarg)) <= 0)
    335 				errx(1, "%s: bad sectors/track", optarg);
    336 			break;
    337 		case 'x':
    338 			if ((cylspares = atoi(optarg)) < 0)
    339 				errx(1, "%s: bad spare sectors per cylinder",
    340 				    optarg);
    341 			break;
    342 		case '?':
    343 		default:
    344 			usage();
    345 		}
    346 	argc -= optind;
    347 	argv += optind;
    348 
    349 	if (argc != 2 && (mfs || argc != 1))
    350 		usage();
    351 
    352 	special = argv[0];
    353 	if (mfs && !strcmp(special, "swap")) {
    354 		/*
    355 		 * it's an MFS, mounted on "swap."  fake up a label.
    356 		 * XXX XXX XXX
    357 		 */
    358 		fso = -1;	/* XXX; normally done below. */
    359 
    360 		memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
    361 		mfsfakelabel.d_secsize = 512;
    362 		mfsfakelabel.d_nsectors = 64;
    363 		mfsfakelabel.d_ntracks = 16;
    364 		mfsfakelabel.d_ncylinders = 16;
    365 		mfsfakelabel.d_secpercyl = 1024;
    366 		mfsfakelabel.d_secperunit = 16384;
    367 		mfsfakelabel.d_rpm = 3600;
    368 		mfsfakelabel.d_interleave = 1;
    369 		mfsfakelabel.d_npartitions = 1;
    370 		mfsfakelabel.d_partitions[0].p_size = 16384;
    371 		mfsfakelabel.d_partitions[0].p_fsize = 1024;
    372 		mfsfakelabel.d_partitions[0].p_frag = 8;
    373 		mfsfakelabel.d_partitions[0].p_cpg = 16;
    374 
    375 		lp = &mfsfakelabel;
    376 		pp = &mfsfakelabel.d_partitions[0];
    377 
    378 		goto havelabel;
    379 	}
    380 	cp = strrchr(special, '/');
    381 	if (cp == 0) {
    382 		/*
    383 		 * No path prefix; try /dev/r%s then /dev/%s.
    384 		 */
    385 		(void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
    386 		    special);
    387 		if (stat(device, &st) == -1)
    388 			(void)snprintf(device, sizeof(device), "%s%s",
    389 			    _PATH_DEV, special);
    390 		special = device;
    391 	}
    392 	if (Nflag) {
    393 		fso = -1;
    394 	} else {
    395 		fso = open(special, O_WRONLY);
    396 		if (fso < 0)
    397 			err(1, "%s: open", special);
    398 
    399 		/* Bail if target special is mounted */
    400 		n = getmntinfo(&mp, MNT_NOWAIT);
    401 		if (n == 0)
    402 			err(1, "%s: getmntinfo", special);
    403 
    404 		len = sizeof(_PATH_DEV) - 1;
    405 		s1 = special;
    406 		if (strncmp(_PATH_DEV, s1, len) == 0)
    407 			s1 += len;
    408 
    409 		while (--n >= 0) {
    410 			s2 = mp->f_mntfromname;
    411 			if (strncmp(_PATH_DEV, s2, len) == 0) {
    412 				s2 += len - 1;
    413 				*s2 = 'r';
    414 			}
    415 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
    416 				errx(1, "%s is mounted on %s",
    417 				    special, mp->f_mntonname);
    418 			++mp;
    419 		}
    420 	}
    421 	if (mfs && disktype != NULL) {
    422 		lp = (struct disklabel *)getdiskbyname(disktype);
    423 		if (lp == NULL)
    424 			errx(1, "%s: unknown disk type", disktype);
    425 		pp = &lp->d_partitions[1];
    426 	} else {
    427 		fsi = open(special, O_RDONLY);
    428 		if (fsi < 0)
    429 			err(1, "%s: open", special);
    430 		if (fstat(fsi, &st) < 0)
    431 			err(1, "%s: fstat", special);
    432 		if (!S_ISCHR(st.st_mode) && !mfs)
    433 			warnx("%s: not a character-special device", special);
    434 		cp = strchr(argv[0], '\0') - 1;
    435 		if (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
    436 		    && !isdigit(*cp)))
    437 			errx(1, "can't figure out file system partition");
    438 #ifdef COMPAT
    439 		if (!mfs && disktype == NULL)
    440 			disktype = argv[1];
    441 #endif
    442 		lp = getdisklabel(special, fsi);
    443 		if (isdigit(*cp))
    444 			pp = &lp->d_partitions[0];
    445 		else
    446 			pp = &lp->d_partitions[*cp - 'a'];
    447 		if (pp->p_size == 0)
    448 			errx(1, "`%c' partition is unavailable", *cp);
    449 		if (pp->p_fstype == FS_BOOT)
    450 			errx(1, "`%c' partition overlaps boot program", *cp);
    451 	}
    452 havelabel:
    453 	if (fssize == 0)
    454 		fssize = pp->p_size;
    455 	if (fssize > pp->p_size && !mfs)
    456 		errx(1, "maximum file system size on the `%c' partition is %d",
    457 		    *cp, pp->p_size);
    458 	if (rpm == 0) {
    459 		rpm = lp->d_rpm;
    460 		if (rpm <= 0)
    461 			rpm = 3600;
    462 	}
    463 	if (ntracks == 0) {
    464 		ntracks = lp->d_ntracks;
    465 		if (ntracks <= 0)
    466 			errx(1, "no default #tracks");
    467 	}
    468 	if (nsectors == 0) {
    469 		nsectors = lp->d_nsectors;
    470 		if (nsectors <= 0)
    471 			errx(1, "no default #sectors/track");
    472 	}
    473 	if (sectorsize == 0) {
    474 		sectorsize = lp->d_secsize;
    475 		if (sectorsize <= 0)
    476 			errx(1, "no default sector size");
    477 	}
    478 	if (trackskew == -1) {
    479 		trackskew = lp->d_trackskew;
    480 		if (trackskew < 0)
    481 			trackskew = 0;
    482 	}
    483 	if (interleave == 0) {
    484 		interleave = lp->d_interleave;
    485 		if (interleave <= 0)
    486 			interleave = 1;
    487 	}
    488 	if (fsize == 0) {
    489 		fsize = pp->p_fsize;
    490 		if (fsize <= 0)
    491 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
    492 	}
    493 	if (bsize == 0) {
    494 		bsize = pp->p_frag * pp->p_fsize;
    495 		if (bsize <= 0)
    496 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
    497 	}
    498 	/*
    499 	 * Maxcontig sets the default for the maximum number of blocks
    500 	 * that may be allocated sequentially. With filesystem clustering
    501 	 * it is possible to allocate contiguous blocks up to the maximum
    502 	 * transfer size permitted by the controller or buffering.
    503 	 */
    504 	if (maxcontig == 0)
    505 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
    506 	if (density == 0)
    507 		density = NFPI * fsize;
    508 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
    509 		warnx("%s %s %d%%", "Warning: changing optimization to space",
    510 		    "because minfree is less than", MINFREE);
    511 		opt = FS_OPTSPACE;
    512 	}
    513 	if (trackspares == -1) {
    514 		trackspares = lp->d_sparespertrack;
    515 		if (trackspares < 0)
    516 			trackspares = 0;
    517 	}
    518 	nphyssectors = nsectors + trackspares;
    519 	if (cylspares == -1) {
    520 		cylspares = lp->d_sparespercyl;
    521 		if (cylspares < 0)
    522 			cylspares = 0;
    523 	}
    524 	secpercyl = nsectors * ntracks - cylspares;
    525 	if (secpercyl != lp->d_secpercyl)
    526 		warnx("%s (%d) %s (%u)\n",
    527 			"Warning: calculated sectors per cylinder", secpercyl,
    528 			"disagrees with disk label", lp->d_secpercyl);
    529 	if (maxbpg == 0)
    530 		maxbpg = MAXBLKPG(bsize);
    531 	headswitch = lp->d_headswitch;
    532 	trackseek = lp->d_trkseek;
    533 #ifdef notdef /* label may be 0 if faked up by kernel */
    534 	bbsize = lp->d_bbsize;
    535 	sbsize = lp->d_sbsize;
    536 #endif
    537 	oldpartition = *pp;
    538 	mkfs(pp, special, fsi, fso);
    539 	if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)))
    540 		rewritelabel(special, fso, lp);
    541 	if (!Nflag)
    542 		close(fso);
    543 	close(fsi);
    544 #ifdef MFS
    545 	if (mfs) {
    546 		struct mfs_args args;
    547 
    548 		switch (pid = fork()) {
    549 		case -1:
    550 			perror("mfs");
    551 			exit(10);
    552 		case 0:
    553 			(void)snprintf(mountfromname, sizeof(mountfromname),
    554 			    "mfs:%d", getpid());
    555 			break;
    556 		default:
    557 			(void)snprintf(mountfromname, sizeof(mountfromname),
    558 			    "mfs:%d", pid);
    559 			for (;;) {
    560 				/*
    561 				 * spin until the mount succeeds
    562 				 * or the child exits
    563 				 */
    564 				usleep(1);
    565 
    566 				/*
    567 				 * XXX Here is a race condition: another process
    568 				 * can mount a filesystem which hides our
    569 				 * ramdisk before we see the success.
    570 				 */
    571 				if (statfs(argv[1], &sf) < 0)
    572 					err(88, "statfs %s", argv[1]);
    573 				if (!strcmp(sf.f_mntfromname, mountfromname) &&
    574 				    !strncmp(sf.f_mntonname, argv[1],
    575 					     MNAMELEN) &&
    576 				    !strcmp(sf.f_fstypename, "mfs"))
    577 					exit(0);
    578 
    579 				res = waitpid(pid, &status, WNOHANG);
    580 				if (res == -1)
    581 					err(11, "waitpid");
    582 				if (res != pid)
    583 					continue;
    584 				if (WIFEXITED(status)) {
    585 					if (WEXITSTATUS(status) == 0)
    586 						exit(0);
    587 					errx(1, "%s: mount: %s", argv[1],
    588 					     strerror(WEXITSTATUS(status)));
    589 				} else
    590 					errx(11, "abnormal termination");
    591 			}
    592 			/* NOTREACHED */
    593 		}
    594 
    595 		(void) setsid();
    596 		(void) close(0);
    597 		(void) close(1);
    598 		(void) close(2);
    599 		(void) chdir("/");
    600 
    601 		args.fspec = mountfromname;
    602 		args.export.ex_root = -2;
    603 		if (mntflags & MNT_RDONLY)
    604 			args.export.ex_flags = MNT_EXRDONLY;
    605 		else
    606 			args.export.ex_flags = 0;
    607 		args.base = membase;
    608 		args.size = fssize * sectorsize;
    609 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
    610 			exit(errno); /* parent prints message */
    611 	}
    612 #endif
    613 	exit(0);
    614 }
    615 
    616 #ifdef COMPAT
    617 char lmsg[] = "%s: can't read disk label; disk type must be specified";
    618 #else
    619 char lmsg[] = "%s: can't read disk label";
    620 #endif
    621 
    622 static struct disklabel *
    623 getdisklabel(s, fd)
    624 	char *s;
    625 	volatile int fd;
    626 {
    627 	static struct disklabel lab;
    628 
    629 	if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
    630 #ifdef COMPAT
    631 		if (disktype) {
    632 			struct disklabel *lp;
    633 
    634 			unlabeled++;
    635 			lp = getdiskbyname(disktype);
    636 			if (lp == NULL)
    637 				errx(1, "%s: unknown disk type", disktype);
    638 			return (lp);
    639 		}
    640 #endif
    641 		warn("ioctl (GDINFO)");
    642 		errx(1, lmsg, s);
    643 	}
    644 	return (&lab);
    645 }
    646 
    647 static void
    648 rewritelabel(s, fd, lp)
    649 	char *s;
    650 	volatile int fd;
    651 	struct disklabel *lp;
    652 {
    653 #ifdef COMPAT
    654 	if (unlabeled)
    655 		return;
    656 #endif
    657 	lp->d_checksum = 0;
    658 	lp->d_checksum = dkcksum(lp);
    659 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    660 		warn("ioctl (WDINFO)");
    661 		errx(1, "%s: can't rewrite disk label", s);
    662 	}
    663 #if __vax__
    664 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    665 		int i;
    666 		int cfd;
    667 		daddr_t alt;
    668 		char specname[64];
    669 		char blk[1024];
    670 		char *cp;
    671 
    672 		/*
    673 		 * Make name for 'c' partition.
    674 		 */
    675 		strcpy(specname, s);
    676 		cp = specname + strlen(specname) - 1;
    677 		if (!isdigit(*cp))
    678 			*cp = 'c';
    679 		cfd = open(specname, O_WRONLY);
    680 		if (cfd < 0)
    681 			err(1, "%s: open", specname);
    682 		memset(blk, 0, sizeof(blk));
    683 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
    684 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    685 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    686 			off_t offset;
    687 
    688 			offset = alt + i;
    689 			offset *= lp->d_secsize;
    690 			if (lseek(cfd, offset, SEEK_SET) == -1)
    691 				err(1, "lseek to badsector area: ");
    692 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    693 				warn("alternate label %d write", i/2);
    694 		}
    695 		close(cfd);
    696 	}
    697 #endif
    698 }
    699 
    700 static void
    701 usage()
    702 {
    703 	if (mfs) {
    704 		fprintf(stderr,
    705 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
    706 			__progname);
    707 	} else
    708 		fprintf(stderr,
    709 		    "usage: %s [ -fsoptions ] special-device%s\n",
    710 		    __progname,
    711 #ifdef COMPAT
    712 		    " [device-type]");
    713 #else
    714 		    "");
    715 #endif
    716 	fprintf(stderr, "where fsoptions are:\n");
    717 	fprintf(stderr, "\t-B byte order (`be' or `le')\n");
    718 	fprintf(stderr,
    719 	    "\t-N do not create file system, just print out parameters\n");
    720 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
    721 	fprintf(stderr, "\t-S sector size\n");
    722 #ifdef COMPAT
    723 	fprintf(stderr, "\t-T disktype\n");
    724 #endif
    725 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    726 	fprintf(stderr, "\t-b block size\n");
    727 	fprintf(stderr, "\t-c cylinders/group\n");
    728 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    729 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    730 	fprintf(stderr, "\t-f frag size\n");
    731 	fprintf(stderr, "\t-i number of bytes per inode\n");
    732 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
    733 	fprintf(stderr, "\t-l hardware sector interleave\n");
    734 	fprintf(stderr, "\t-m minimum free space %%\n");
    735 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
    736 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    737 	fprintf(stderr, "\t-p spare sectors per track\n");
    738 	fprintf(stderr, "\t-s file system size (sectors)\n");
    739 	fprintf(stderr, "\t-r revolutions/minute\n");
    740 	fprintf(stderr, "\t-t tracks/cylinder\n");
    741 	fprintf(stderr, "\t-u sectors/track\n");
    742 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
    743 	exit(1);
    744 }
    745