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