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