Home | History | Annotate | Line # | Download | only in newfs
newfs.c revision 1.35
      1 /*	$NetBSD: newfs.c,v 1.35 1998/10/20 01:51:17 matt 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.35 1998/10/20 01:51:17 matt 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)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
    385 		    special);
    386 		if (stat(device, &st) == -1)
    387 			(void)snprintf(device, sizeof(device), "%s%s",
    388 			    _PATH_DEV, special);
    389 		special = device;
    390 	}
    391 	if (Nflag) {
    392 		fso = -1;
    393 	} else {
    394 		fso = open(special, O_WRONLY);
    395 		if (fso < 0)
    396 			err(1, "%s: open", special);
    397 
    398 		/* Bail if target special is mounted */
    399 		n = getmntinfo(&mp, MNT_NOWAIT);
    400 		if (n == 0)
    401 			err(1, "%s: getmntinfo", special);
    402 
    403 		len = sizeof(_PATH_DEV) - 1;
    404 		s1 = special;
    405 		if (strncmp(_PATH_DEV, s1, len) == 0)
    406 			s1 += len;
    407 
    408 		while (--n >= 0) {
    409 			s2 = mp->f_mntfromname;
    410 			if (strncmp(_PATH_DEV, s2, len) == 0) {
    411 				s2 += len - 1;
    412 				*s2 = 'r';
    413 			}
    414 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
    415 				errx(1, "%s is mounted on %s",
    416 				    special, mp->f_mntonname);
    417 			++mp;
    418 		}
    419 	}
    420 	if (mfs && disktype != NULL) {
    421 		lp = (struct disklabel *)getdiskbyname(disktype);
    422 		if (lp == NULL)
    423 			errx(1, "%s: unknown disk type", disktype);
    424 		pp = &lp->d_partitions[1];
    425 	} else {
    426 		fsi = open(special, O_RDONLY);
    427 		if (fsi < 0)
    428 			err(1, "%s: open", special);
    429 		if (fstat(fsi, &st) < 0)
    430 			err(1, "%s: fstat", special);
    431 		if (!S_ISCHR(st.st_mode) && !mfs)
    432 			warnx("%s: not a character-special device", special);
    433 		cp = strchr(argv[0], '\0') - 1;
    434 		if (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
    435 		    && !isdigit(*cp)))
    436 			errx(1, "can't figure out file system partition");
    437 #ifdef COMPAT
    438 		if (!mfs && disktype == NULL)
    439 			disktype = argv[1];
    440 #endif
    441 		lp = getdisklabel(special, fsi);
    442 		if (isdigit(*cp))
    443 			pp = &lp->d_partitions[0];
    444 		else
    445 			pp = &lp->d_partitions[*cp - 'a'];
    446 		if (pp->p_size == 0)
    447 			errx(1, "`%c' partition is unavailable", *cp);
    448 		if (pp->p_fstype == FS_BOOT)
    449 			errx(1, "`%c' partition overlaps boot program", *cp);
    450 	}
    451 havelabel:
    452 	if (fssize == 0)
    453 		fssize = pp->p_size;
    454 	if (fssize > pp->p_size && !mfs)
    455 		errx(1, "maximum file system size on the `%c' partition is %d",
    456 		    *cp, pp->p_size);
    457 	if (rpm == 0) {
    458 		rpm = lp->d_rpm;
    459 		if (rpm <= 0)
    460 			rpm = 3600;
    461 	}
    462 	if (ntracks == 0) {
    463 		ntracks = lp->d_ntracks;
    464 		if (ntracks <= 0)
    465 			errx(1, "no default #tracks");
    466 	}
    467 	if (nsectors == 0) {
    468 		nsectors = lp->d_nsectors;
    469 		if (nsectors <= 0)
    470 			errx(1, "no default #sectors/track");
    471 	}
    472 	if (sectorsize == 0) {
    473 		sectorsize = lp->d_secsize;
    474 		if (sectorsize <= 0)
    475 			errx(1, "no default sector size");
    476 	}
    477 	if (trackskew == -1) {
    478 		trackskew = lp->d_trackskew;
    479 		if (trackskew < 0)
    480 			trackskew = 0;
    481 	}
    482 	if (interleave == 0) {
    483 		interleave = lp->d_interleave;
    484 		if (interleave <= 0)
    485 			interleave = 1;
    486 	}
    487 	if (fsize == 0) {
    488 		fsize = pp->p_fsize;
    489 		if (fsize <= 0)
    490 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
    491 	}
    492 	if (bsize == 0) {
    493 		bsize = pp->p_frag * pp->p_fsize;
    494 		if (bsize <= 0)
    495 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
    496 	}
    497 	/*
    498 	 * Maxcontig sets the default for the maximum number of blocks
    499 	 * that may be allocated sequentially. With filesystem clustering
    500 	 * it is possible to allocate contiguous blocks up to the maximum
    501 	 * transfer size permitted by the controller or buffering.
    502 	 */
    503 	if (maxcontig == 0)
    504 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
    505 	if (density == 0)
    506 		density = NFPI * fsize;
    507 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
    508 		warnx("%s %s %d%%", "Warning: changing optimization to space",
    509 		    "because minfree is less than", MINFREE);
    510 		opt = FS_OPTSPACE;
    511 	}
    512 	if (trackspares == -1) {
    513 		trackspares = lp->d_sparespertrack;
    514 		if (trackspares < 0)
    515 			trackspares = 0;
    516 	}
    517 	nphyssectors = nsectors + trackspares;
    518 	if (cylspares == -1) {
    519 		cylspares = lp->d_sparespercyl;
    520 		if (cylspares < 0)
    521 			cylspares = 0;
    522 	}
    523 	secpercyl = nsectors * ntracks - cylspares;
    524 	if (secpercyl != lp->d_secpercyl)
    525 		warnx("%s (%d) %s (%u)\n",
    526 			"Warning: calculated sectors per cylinder", secpercyl,
    527 			"disagrees with disk label", lp->d_secpercyl);
    528 	if (maxbpg == 0)
    529 		maxbpg = MAXBLKPG(bsize);
    530 	headswitch = lp->d_headswitch;
    531 	trackseek = lp->d_trkseek;
    532 #ifdef notdef /* label may be 0 if faked up by kernel */
    533 	bbsize = lp->d_bbsize;
    534 	sbsize = lp->d_sbsize;
    535 #endif
    536 	oldpartition = *pp;
    537 	mkfs(pp, special, fsi, fso);
    538 	if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)))
    539 		rewritelabel(special, fso, lp);
    540 	if (!Nflag)
    541 		close(fso);
    542 	close(fsi);
    543 #ifdef MFS
    544 	if (mfs) {
    545 		struct mfs_args args;
    546 
    547 		switch (pid = fork()) {
    548 		case -1:
    549 			perror("mfs");
    550 			exit(10);
    551 		case 0:
    552 			(void)snprintf(mountfromname, sizeof(mountfromname),
    553 			    "mfs:%d", getpid());
    554 			break;
    555 		default:
    556 			(void)snprintf(mountfromname, sizeof(mountfromname),
    557 			    "mfs:%d", pid);
    558 			for (;;) {
    559 				/*
    560 				 * spin until the mount succeeds
    561 				 * or the child exits
    562 				 */
    563 				usleep(1);
    564 
    565 				/*
    566 				 * XXX Here is a race condition: another process
    567 				 * can mount a filesystem which hides our
    568 				 * ramdisk before we see the success.
    569 				 */
    570 				if (statfs(argv[1], &sf) < 0)
    571 					err(88, "statfs %s", argv[1]);
    572 				if (!strcmp(sf.f_mntfromname, mountfromname) &&
    573 				    !strncmp(sf.f_mntonname, argv[1],
    574 					     MNAMELEN) &&
    575 				    !strcmp(sf.f_fstypename, "mfs"))
    576 					exit(0);
    577 
    578 				res = waitpid(pid, &status, WNOHANG);
    579 				if (res == -1)
    580 					err(11, "waitpid");
    581 				if (res != pid)
    582 					continue;
    583 				if (WIFEXITED(status)) {
    584 					if (WEXITSTATUS(status) == 0)
    585 						exit(0);
    586 					errx(1, "%s: mount: %s", argv[1],
    587 					     strerror(WEXITSTATUS(status)));
    588 				} else
    589 					errx(11, "abnormal termination");
    590 			}
    591 			/* NOTREACHED */
    592 		}
    593 
    594 		(void) setsid();
    595 		(void) close(0);
    596 		(void) close(1);
    597 		(void) close(2);
    598 		(void) chdir("/");
    599 
    600 		args.fspec = mountfromname;
    601 		args.export.ex_root = -2;
    602 		if (mntflags & MNT_RDONLY)
    603 			args.export.ex_flags = MNT_EXRDONLY;
    604 		else
    605 			args.export.ex_flags = 0;
    606 		args.base = membase;
    607 		args.size = fssize * sectorsize;
    608 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
    609 			exit(errno); /* parent prints message */
    610 	}
    611 #endif
    612 	exit(0);
    613 }
    614 
    615 #ifdef COMPAT
    616 char lmsg[] = "%s: can't read disk label; disk type must be specified";
    617 #else
    618 char lmsg[] = "%s: can't read disk label";
    619 #endif
    620 
    621 static struct disklabel *
    622 getdisklabel(s, fd)
    623 	char *s;
    624 	volatile int fd;
    625 {
    626 	static struct disklabel lab;
    627 
    628 	if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
    629 #ifdef COMPAT
    630 		if (disktype) {
    631 			struct disklabel *lp;
    632 
    633 			unlabeled++;
    634 			lp = getdiskbyname(disktype);
    635 			if (lp == NULL)
    636 				errx(1, "%s: unknown disk type", disktype);
    637 			return (lp);
    638 		}
    639 #endif
    640 		warn("ioctl (GDINFO)");
    641 		errx(1, lmsg, s);
    642 	}
    643 	return (&lab);
    644 }
    645 
    646 static void
    647 rewritelabel(s, fd, lp)
    648 	char *s;
    649 	volatile int fd;
    650 	struct disklabel *lp;
    651 {
    652 #ifdef COMPAT
    653 	if (unlabeled)
    654 		return;
    655 #endif
    656 	lp->d_checksum = 0;
    657 	lp->d_checksum = dkcksum(lp);
    658 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    659 		warn("ioctl (WDINFO)");
    660 		errx(1, "%s: can't rewrite disk label", s);
    661 	}
    662 #if __vax__
    663 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    664 		int i;
    665 		int cfd;
    666 		daddr_t alt;
    667 		char specname[64];
    668 		char blk[1024];
    669 		char *cp;
    670 
    671 		/*
    672 		 * Make name for 'c' partition.
    673 		 */
    674 		strcpy(specname, s);
    675 		cp = specname + strlen(specname) - 1;
    676 		if (!isdigit(*cp))
    677 			*cp = 'c';
    678 		cfd = open(specname, O_WRONLY);
    679 		if (cfd < 0)
    680 			err(1, "%s: open", specname);
    681 		memset(blk, 0, sizeof(blk));
    682 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
    683 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    684 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    685 			off_t offset;
    686 
    687 			offset = alt + i;
    688 			offset *= lp->d_secsize;
    689 			if (lseek(cfd, offset, SEEK_SET) == -1)
    690 				err(1, "lseek to badsector area: ");
    691 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    692 				warn("alternate label %d write", i/2);
    693 		}
    694 		close(cfd);
    695 	}
    696 #endif
    697 }
    698 
    699 static void
    700 usage()
    701 {
    702 	if (mfs) {
    703 		fprintf(stderr,
    704 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
    705 			__progname);
    706 	} else
    707 		fprintf(stderr,
    708 		    "usage: %s [ -fsoptions ] special-device%s\n",
    709 		    __progname,
    710 #ifdef COMPAT
    711 		    " [device-type]");
    712 #else
    713 		    "");
    714 #endif
    715 	fprintf(stderr, "where fsoptions are:\n");
    716 	fprintf(stderr, "\t-B byte order (`be' or `le')\n");
    717 	fprintf(stderr,
    718 	    "\t-N do not create file system, just print out parameters\n");
    719 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
    720 	fprintf(stderr, "\t-S sector size\n");
    721 #ifdef COMPAT
    722 	fprintf(stderr, "\t-T disktype\n");
    723 #endif
    724 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    725 	fprintf(stderr, "\t-b block size\n");
    726 	fprintf(stderr, "\t-c cylinders/group\n");
    727 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    728 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    729 	fprintf(stderr, "\t-f frag size\n");
    730 	fprintf(stderr, "\t-i number of bytes per inode\n");
    731 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
    732 	fprintf(stderr, "\t-l hardware sector interleave\n");
    733 	fprintf(stderr, "\t-m minimum free space %%\n");
    734 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
    735 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    736 	fprintf(stderr, "\t-p spare sectors per track\n");
    737 	fprintf(stderr, "\t-s file system size (sectors)\n");
    738 	fprintf(stderr, "\t-r revolutions/minute\n");
    739 	fprintf(stderr, "\t-t tracks/cylinder\n");
    740 	fprintf(stderr, "\t-u sectors/track\n");
    741 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
    742 	exit(1);
    743 }
    744