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