Home | History | Annotate | Line # | Download | only in newfs
newfs.c revision 1.47
      1 /*	$NetBSD: newfs.c,v 1.47 2001/09/06 02:16:01 lukem 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.47 2001/09/06 02:16:01 lukem 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 int strsuftoi(const char *, const char *, int, int);
     95 static void usage(void);
     96 int main(int, char *[]);
     97 
     98 #define	COMPAT			/* allow non-labeled disks */
     99 
    100 /*
    101  * The following two constants set the default block and fragment sizes.
    102  * Both constants must be a power of 2 and meet the following constraints:
    103  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
    104  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
    105  *	DESBLKSIZE / DESFRAGSIZE <= 8
    106  */
    107 #define	DFL_FRAGSIZE	1024
    108 #define	DFL_BLKSIZE	8192
    109 
    110 /*
    111  * Default sector size.
    112  */
    113 #define DFL_SECSIZE	512
    114 
    115 /*
    116  * Cylinder groups may have up to many cylinders. The actual
    117  * number used depends upon how much information can be stored
    118  * on a single cylinder. The default is to use 16 cylinders
    119  * per group.
    120  */
    121 #define	DESCPG		16	/* desired fs_cpg */
    122 
    123 /*
    124  * ROTDELAY gives the minimum number of milliseconds to initiate
    125  * another disk transfer on the same cylinder. It is used in
    126  * determining the rotationally optimal layout for disk blocks
    127  * within a file; the default of fs_rotdelay is 0ms.
    128  */
    129 #define ROTDELAY	0
    130 
    131 /*
    132  * MAXBLKPG determines the maximum number of data blocks which are
    133  * placed in a single cylinder group. The default is one indirect
    134  * block worth of data blocks.
    135  */
    136 #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
    137 
    138 /*
    139  * Each file system has a number of inodes statically allocated.
    140  * We allocate one inode slot per NFPI fragments, expecting this
    141  * to be far more than we will ever need.
    142  */
    143 #define	NFPI		4
    144 
    145 /*
    146  * For each cylinder we keep track of the availability of blocks at different
    147  * rotational positions, so that we can lay out the data to be picked
    148  * up with minimum rotational latency.  NRPOS is the default number of
    149  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
    150  * of our summary information is 2ms for a typical 3600 rpm drive.  Caching
    151  * and zoning pretty much defeats rotational optimization, so we now use a
    152  * default of 1.
    153  */
    154 #define	NRPOS		1	/* number distinct rotational positions */
    155 
    156 
    157 int	mfs;			/* run as the memory based filesystem */
    158 int	Nflag;			/* run without writing file system */
    159 int	Oflag;			/* format as an 4.3BSD file system */
    160 int	fssize;			/* file system size */
    161 int	ntracks;		/* # tracks/cylinder */
    162 int	nsectors;		/* # sectors/track */
    163 int	nphyssectors;		/* # sectors/track including spares */
    164 int	secpercyl;		/* sectors per cylinder */
    165 int	trackspares = -1;	/* spare sectors per track */
    166 int	cylspares = -1;		/* spare sectors per cylinder */
    167 int	sectorsize;		/* bytes/sector */
    168 int	rpm;			/* revolutions/minute of drive */
    169 int	interleave;		/* hardware sector interleave */
    170 int	trackskew = -1;		/* sector 0 skew, per track */
    171 int	fsize = 0;		/* fragment size */
    172 int	bsize = 0;		/* block size */
    173 int	cpg = DESCPG;		/* cylinders/cylinder group */
    174 int	cpgflg;			/* cylinders/cylinder group flag was given */
    175 int	minfree = MINFREE;	/* free space threshold */
    176 int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
    177 int	density;		/* number of bytes per inode */
    178 int	maxcontig = 0;		/* max contiguous blocks to allocate */
    179 int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
    180 int	maxbpg;			/* maximum blocks per file in a cyl group */
    181 int	nrpos = NRPOS;		/* # of distinguished rotational positions */
    182 int	avgfilesize = AVFILESIZ;/* expected average file size */
    183 int	avgfpdir = AFPDIR;	/* expected number of files per directory */
    184 int	bbsize = BBSIZE;	/* boot block size */
    185 int	sbsize = SBSIZE;	/* superblock size */
    186 int	mntflags = MNT_ASYNC;	/* flags to be passed to mount */
    187 u_long	memleft;		/* virtual memory available */
    188 caddr_t	membase;		/* start address of memory based filesystem */
    189 int	needswap;		/* Filesystem not in native byte order */
    190 #ifdef COMPAT
    191 char	*disktype;
    192 int	unlabeled;
    193 #endif
    194 
    195 char	device[MAXPATHLEN];
    196 
    197 int
    198 main(int argc, char *argv[])
    199 {
    200 	struct partition *pp;
    201 	struct disklabel *lp;
    202 	struct disklabel mfsfakelabel;
    203 	struct partition oldpartition;
    204 	struct stat st;
    205 	struct statfs *mp;
    206 	int ch, fsi, fso, len, maxpartitions, n, Fflag, Zflag;
    207 	char *cp, *endp, *s1, *s2, *special;
    208 	const char *opstring;
    209 	long long llsize;
    210 #ifdef MFS
    211 	char mountfromname[100];
    212 	pid_t pid, res;
    213 	struct statfs sf;
    214 	int status;
    215 #endif
    216 
    217 	cp = NULL;
    218 	fsi = fso = -1;
    219 	Fflag = Zflag = 0;
    220 	if (strstr(getprogname(), "mfs")) {
    221 		mfs = 1;
    222 		Nflag++;
    223 	}
    224 
    225 	maxpartitions = getmaxpartitions();
    226 	if (maxpartitions > 26)
    227 		errx(1, "insane maxpartitions value %d", maxpartitions);
    228 
    229 	opstring = mfs ?
    230 	    "NT:a:b:c:d:e:f:g:h:i:m:o:s:" :
    231 	    "B:FNOS:T:Za:b:c:d:e:f:g:h:i:k:l:m:n:o:p:r:s:t:u:x:";
    232 	while ((ch = getopt(argc, argv, opstring)) != -1)
    233 		switch (ch) {
    234 		case 'B':
    235 			if (strcmp(optarg, "be") == 0) {
    236 #if BYTE_ORDER == LITTLE_ENDIAN
    237 				needswap = 1;
    238 #endif
    239 			} else if (strcmp(optarg, "le") == 0) {
    240 #if BYTE_ORDER == BIG_ENDIAN
    241 				needswap = 1;
    242 #endif
    243 			} else
    244 				usage();
    245 			break;
    246 		case 'F':
    247 			Fflag = 1;
    248 			break;
    249 		case 'N':
    250 			Nflag = 1;
    251 			break;
    252 		case 'O':
    253 			Oflag = 1;
    254 			break;
    255 		case 'S':
    256 			sectorsize = strsuftoi("sector size",
    257 			    optarg, 1, INT_MAX);
    258 			break;
    259 #ifdef COMPAT
    260 		case 'T':
    261 			disktype = optarg;
    262 			break;
    263 #endif
    264 		case 'Z':
    265 			Zflag = 1;
    266 			break;
    267 		case 'a':
    268 			maxcontig = strsuftoi("maximum contiguous blocks",
    269 			    optarg, 1, INT_MAX);
    270 			break;
    271 		case 'b':
    272 			bsize = strsuftoi("block size",
    273 			    optarg, MINBSIZE, INT_MAX);
    274 			break;
    275 		case 'c':
    276 			cpg = strsuftoi("cylinders per group",
    277 			    optarg, 1, INT_MAX);
    278 			cpgflg++;
    279 			break;
    280 		case 'd':
    281 			rotdelay = strsuftoi("rotational delay",
    282 			    optarg, 0, INT_MAX);
    283 			break;
    284 		case 'e':
    285 			maxbpg = strsuftoi(
    286 			    "blocks per file in a cylinder group",
    287 			    optarg, 1, INT_MAX);
    288 			break;
    289 		case 'f':
    290 			fsize = strsuftoi("fragment size",
    291 			    optarg, 1, INT_MAX);
    292 			break;
    293 		case 'g':
    294 			avgfilesize = strsuftoi("average file size",
    295 			    optarg, 1, INT_MAX);
    296 			break;
    297 		case 'h':
    298 			avgfpdir = strsuftoi("expected files per directory",
    299 			    optarg, 1, INT_MAX);
    300 			break;
    301 		case 'i':
    302 			density = strsuftoi("bytes per inode",
    303 			    optarg, 1, INT_MAX);
    304 			break;
    305 		case 'k':
    306 			trackskew = strsuftoi("track skew",
    307 			    optarg, 0, INT_MAX);
    308 			break;
    309 		case 'l':
    310 			interleave = strsuftoi("interleave",
    311 			    optarg, 1, INT_MAX);
    312 			break;
    313 		case 'm':
    314 			minfree = strsuftoi("free space %",
    315 			    optarg, 0, 99);
    316 			break;
    317 		case 'n':
    318 			nrpos = strsuftoi("rotational layout count",
    319 			    optarg, 1, INT_MAX);
    320 			break;
    321 		case 'o':
    322 			if (mfs)
    323 				getmntopts(optarg, mopts, &mntflags, 0);
    324 			else {
    325 				if (strcmp(optarg, "space") == 0)
    326 					opt = FS_OPTSPACE;
    327 				else if (strcmp(optarg, "time") == 0)
    328 					opt = FS_OPTTIME;
    329 				else
    330 				    errx(1, "%s %s",
    331 					"unknown optimization preference: ",
    332 					"use `space' or `time'.");
    333 			}
    334 			break;
    335 		case 'p':
    336 			trackspares = strsuftoi("spare sectors per track",
    337 			    optarg, 0, INT_MAX);
    338 			break;
    339 		case 'r':
    340 			rpm = strsuftoi("revolutions per minute",
    341 			    optarg, 1, INT_MAX);
    342 			break;
    343 		case 's':
    344 			llsize = strtoll(optarg, &endp, 10);
    345 			if (endp[0] != '\0' && endp[1] != '\0')
    346 				llsize = -1;
    347 			else {
    348 				int	ssiz;
    349 
    350 				ssiz = (sectorsize ? sectorsize : DFL_SECSIZE);
    351 				switch (tolower((unsigned char)endp[0])) {
    352 				case 'b':
    353 					llsize /= ssiz;
    354 					break;
    355 				case 'k':
    356 					llsize *= 1024 / ssiz;
    357 					break;
    358 				case 'm':
    359 					llsize *= 1024 * 1024 / ssiz;
    360 					break;
    361 				case 'g':
    362 					llsize *= 1024 * 1024 * 1024 / ssiz;
    363 					break;
    364 				case '\0':
    365 				case 's':
    366 					break;
    367 				default:
    368 					llsize = -1;
    369 				}
    370 			}
    371 			if (llsize > INT_MAX)
    372 				errx(1, "file system size `%s' is too large.",
    373 				    optarg);
    374 			if (llsize <= 0)
    375 				errx(1,
    376 			    "`%s' is not a valid number for file system size.",
    377 				    optarg);
    378 			fssize = (int)llsize;
    379 			break;
    380 		case 't':
    381 			ntracks = strsuftoi("total tracks",
    382 			    optarg, 1, INT_MAX);
    383 			break;
    384 		case 'u':
    385 			nsectors = strsuftoi("sectors per track",
    386 			    optarg, 1, INT_MAX);
    387 			break;
    388 		case 'x':
    389 			cylspares = strsuftoi("spare sectors per cylinder",
    390 			    optarg, 0, INT_MAX);
    391 			break;
    392 		case '?':
    393 		default:
    394 			usage();
    395 		}
    396 	argc -= optind;
    397 	argv += optind;
    398 
    399 	if (argc != 2 && (mfs || argc != 1))
    400 		usage();
    401 
    402 	special = argv[0];
    403 	if ((mfs && !strcmp(special, "swap")) || Fflag) {
    404 		/*
    405 		 * it's an MFS mounted on "swap" or a file system image;
    406 		 * fake up a label.		XXX
    407 		 */
    408 		if (!sectorsize)
    409 			sectorsize = DFL_SECSIZE;
    410 
    411 		if (Fflag && (stat(special, &st) != -1 && !S_ISREG(st.st_mode)))
    412 			errx(1, "%s is not a regular file", special);
    413 		if (Fflag && !Nflag) {	/* creating image in a regular file */
    414 			if (fssize == 0)
    415 				errx(1, "need to specify size when using -F");
    416 			fso = open(special, O_RDWR | O_CREAT | O_TRUNC, 0777);
    417 			if (fso == -1)
    418 				err(1, "can't open file %s", special);
    419 			if ((fsi = dup(fso)) == -1)
    420 				err(1, "can't dup(2) image fd");
    421 			if (ftruncate(fso, (off_t)fssize * sectorsize) == -1)
    422 				err(1, "can't resize %s to %d",
    423 				    special, fssize);
    424 
    425 			if (Zflag) {	/* pre-zero the file */
    426 				char	*buf;
    427 				int	bufsize, i;
    428 				off_t	bufrem;
    429 				struct statfs sfs;
    430 
    431 				if (fstatfs(fso, &sfs) == -1) {
    432 					warn("can't fstatfs `%s'", special);
    433 					bufsize = 8192;
    434 				} else
    435 					bufsize = sfs.f_iosize;
    436 
    437 				if ((buf = calloc(1, bufsize)) == NULL)
    438 					err(1, "can't malloc buffer of %d",
    439 					bufsize);
    440 				bufrem = fssize * sectorsize;
    441 				printf(
    442     "Creating file system image in `%s', size %lld bytes, in %d byte chunks.\n",
    443 				    special, (long long)bufrem, bufsize);
    444 				while (bufrem > 0) {
    445 					i = write(fso, buf,
    446 					    MIN(bufsize, bufrem));
    447 					if (i == -1)
    448 						err(1, "writing image");
    449 					bufrem -= i;
    450 				}
    451 			}
    452 
    453 		}
    454 
    455 		memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
    456 		mfsfakelabel.d_secsize = sectorsize;
    457 		mfsfakelabel.d_nsectors = 64;	/* these 3 add up to 16MB */
    458 		mfsfakelabel.d_ntracks = 16;
    459 		mfsfakelabel.d_ncylinders = 16;
    460 		mfsfakelabel.d_secpercyl =
    461 		    mfsfakelabel.d_nsectors * mfsfakelabel.d_ntracks;
    462 		mfsfakelabel.d_secperunit =
    463 		    mfsfakelabel.d_ncylinders * mfsfakelabel.d_secpercyl;
    464 		mfsfakelabel.d_rpm = 3600;
    465 		mfsfakelabel.d_interleave = 1;
    466 		mfsfakelabel.d_npartitions = 1;
    467 		mfsfakelabel.d_partitions[0].p_size = mfsfakelabel.d_secperunit;
    468 		mfsfakelabel.d_partitions[0].p_fsize = 1024;
    469 		mfsfakelabel.d_partitions[0].p_frag = 8;
    470 		mfsfakelabel.d_partitions[0].p_cpg = 16;
    471 
    472 		lp = &mfsfakelabel;
    473 		pp = &mfsfakelabel.d_partitions[0];
    474 
    475 		goto havelabel;
    476 	}
    477 	cp = strrchr(special, '/');
    478 	if (cp == NULL) {
    479 		/*
    480 		 * No path prefix; try /dev/r%s then /dev/%s.
    481 		 */
    482 		(void)snprintf(device, sizeof(device), "%sr%s",
    483 		    _PATH_DEV, special);
    484 		if (stat(device, &st) == -1)
    485 			(void)snprintf(device, sizeof(device), "%s%s",
    486 			    _PATH_DEV, special);
    487 		special = device;
    488 	}
    489 	if (Nflag) {
    490 		fso = -1;
    491 	} else {
    492 		fso = open(special, O_WRONLY);
    493 		if (fso < 0)
    494 			err(1, "%s: open", special);
    495 
    496 		/* Bail if target special is mounted */
    497 		n = getmntinfo(&mp, MNT_NOWAIT);
    498 		if (n == 0)
    499 			err(1, "%s: getmntinfo", special);
    500 
    501 		len = sizeof(_PATH_DEV) - 1;
    502 		s1 = special;
    503 		if (strncmp(_PATH_DEV, s1, len) == 0)
    504 			s1 += len;
    505 
    506 		while (--n >= 0) {
    507 			s2 = mp->f_mntfromname;
    508 			if (strncmp(_PATH_DEV, s2, len) == 0) {
    509 				s2 += len - 1;
    510 				*s2 = 'r';
    511 			}
    512 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
    513 				errx(1, "%s is mounted on %s",
    514 				    special, mp->f_mntonname);
    515 			++mp;
    516 		}
    517 	}
    518 	if (mfs && disktype != NULL) {
    519 		lp = (struct disklabel *)getdiskbyname(disktype);
    520 		if (lp == NULL)
    521 			errx(1, "%s: unknown disk type", disktype);
    522 		pp = &lp->d_partitions[1];
    523 	} else {
    524 		fsi = open(special, O_RDONLY);
    525 		if (fsi < 0)
    526 			err(1, "%s: open", special);
    527 		if (fstat(fsi, &st) < 0)
    528 			err(1, "%s: fstat", special);
    529 		if (!S_ISCHR(st.st_mode) && !mfs)
    530 			warnx("%s: not a character-special device", special);
    531 		cp = strchr(argv[0], '\0') - 1;
    532 		if (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
    533 		    && !isdigit(*cp)))
    534 			errx(1, "can't figure out file system partition");
    535 #ifdef COMPAT
    536 		if (!mfs && disktype == NULL)
    537 			disktype = argv[1];
    538 #endif
    539 		lp = getdisklabel(special, fsi);
    540 		if (isdigit(*cp))
    541 			pp = &lp->d_partitions[0];
    542 		else
    543 			pp = &lp->d_partitions[*cp - 'a'];
    544 		if (pp->p_size == 0)
    545 			errx(1, "`%c' partition is unavailable", *cp);
    546 		if (pp->p_fstype == FS_BOOT)
    547 			errx(1, "`%c' partition overlaps boot program", *cp);
    548 	}
    549 
    550  havelabel:
    551 	if (fssize == 0)
    552 		fssize = pp->p_size;
    553 	if (fssize > pp->p_size && !mfs && !Fflag)
    554 		errx(1, "maximum file system size on the `%c' partition is %d",
    555 		    *cp, pp->p_size);
    556 	if (rpm == 0) {
    557 		rpm = lp->d_rpm;
    558 		if (rpm <= 0)
    559 			rpm = 3600;
    560 	}
    561 	if (ntracks == 0) {
    562 		ntracks = lp->d_ntracks;
    563 		if (ntracks <= 0)
    564 			errx(1, "no default #tracks");
    565 	}
    566 	if (nsectors == 0) {
    567 		nsectors = lp->d_nsectors;
    568 		if (nsectors <= 0)
    569 			errx(1, "no default #sectors/track");
    570 	}
    571 	if (sectorsize == 0) {
    572 		sectorsize = lp->d_secsize;
    573 		if (sectorsize <= 0)
    574 			errx(1, "no default sector size");
    575 	}
    576 	if (trackskew == -1) {
    577 		trackskew = lp->d_trackskew;
    578 		if (trackskew < 0)
    579 			trackskew = 0;
    580 	}
    581 	if (interleave == 0) {
    582 		interleave = lp->d_interleave;
    583 		if (interleave <= 0)
    584 			interleave = 1;
    585 	}
    586 	if (fsize == 0) {
    587 		fsize = pp->p_fsize;
    588 		if (fsize <= 0)
    589 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
    590 	}
    591 	if (bsize == 0) {
    592 		bsize = pp->p_frag * pp->p_fsize;
    593 		if (bsize <= 0)
    594 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
    595 	}
    596 	if (cpgflg == 0) {
    597 		if (pp->p_cpg != 0)
    598 			cpg = pp->p_cpg;
    599 	}
    600 	/*
    601 	 * Maxcontig sets the default for the maximum number of blocks
    602 	 * that may be allocated sequentially. With filesystem clustering
    603 	 * it is possible to allocate contiguous blocks up to the maximum
    604 	 * transfer size permitted by the controller or buffering.
    605 	 */
    606 	if (maxcontig == 0)
    607 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
    608 	if (density == 0)
    609 		density = NFPI * fsize;
    610 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
    611 		warnx("%s %s %d%%", "Warning: changing optimization to space",
    612 		    "because minfree is less than", MINFREE);
    613 		opt = FS_OPTSPACE;
    614 	}
    615 	if (trackspares == -1) {
    616 		trackspares = lp->d_sparespertrack;
    617 		if (trackspares < 0)
    618 			trackspares = 0;
    619 	}
    620 	nphyssectors = nsectors + trackspares;
    621 	if (cylspares == -1) {
    622 		cylspares = lp->d_sparespercyl;
    623 		if (cylspares < 0)
    624 			cylspares = 0;
    625 	}
    626 	secpercyl = nsectors * ntracks - cylspares;
    627 	if (secpercyl != lp->d_secpercyl)
    628 		warnx("%s (%d) %s (%u)\n",
    629 			"Warning: calculated sectors per cylinder", secpercyl,
    630 			"disagrees with disk label", lp->d_secpercyl);
    631 	if (maxbpg == 0)
    632 		maxbpg = MAXBLKPG(bsize);
    633 #ifdef notdef /* label may be 0 if faked up by kernel */
    634 	bbsize = lp->d_bbsize;
    635 	sbsize = lp->d_sbsize;
    636 #endif
    637 	oldpartition = *pp;
    638 	mkfs(pp, special, fsi, fso);
    639 	if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)) && !Fflag)
    640 		rewritelabel(special, fso, lp);
    641 	if (!Nflag)
    642 		close(fso);
    643 	close(fsi);
    644 #ifdef MFS
    645 	if (mfs) {
    646 		struct mfs_args args;
    647 
    648 		switch (pid = fork()) {
    649 		case -1:
    650 			perror("mfs");
    651 			exit(10);
    652 		case 0:
    653 			(void)snprintf(mountfromname, sizeof(mountfromname),
    654 			    "mfs:%d", getpid());
    655 			break;
    656 		default:
    657 			(void)snprintf(mountfromname, sizeof(mountfromname),
    658 			    "mfs:%d", pid);
    659 			for (;;) {
    660 				/*
    661 				 * spin until the mount succeeds
    662 				 * or the child exits
    663 				 */
    664 				usleep(1);
    665 
    666 				/*
    667 				 * XXX Here is a race condition: another process
    668 				 * can mount a filesystem which hides our
    669 				 * ramdisk before we see the success.
    670 				 */
    671 				if (statfs(argv[1], &sf) < 0)
    672 					err(88, "statfs %s", argv[1]);
    673 				if (!strcmp(sf.f_mntfromname, mountfromname) &&
    674 				    !strncmp(sf.f_mntonname, argv[1],
    675 					     MNAMELEN) &&
    676 				    !strcmp(sf.f_fstypename, "mfs"))
    677 					exit(0);
    678 
    679 				res = waitpid(pid, &status, WNOHANG);
    680 				if (res == -1)
    681 					err(11, "waitpid");
    682 				if (res != pid)
    683 					continue;
    684 				if (WIFEXITED(status)) {
    685 					if (WEXITSTATUS(status) == 0)
    686 						exit(0);
    687 					errx(1, "%s: mount: %s", argv[1],
    688 					     strerror(WEXITSTATUS(status)));
    689 				} else
    690 					errx(11, "abnormal termination");
    691 			}
    692 			/* NOTREACHED */
    693 		}
    694 
    695 		(void) setsid();
    696 		(void) close(0);
    697 		(void) close(1);
    698 		(void) close(2);
    699 		(void) chdir("/");
    700 
    701 		args.fspec = mountfromname;
    702 		args.export.ex_root = -2;
    703 		if (mntflags & MNT_RDONLY)
    704 			args.export.ex_flags = MNT_EXRDONLY;
    705 		else
    706 			args.export.ex_flags = 0;
    707 		args.base = membase;
    708 		args.size = fssize * sectorsize;
    709 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
    710 			exit(errno); /* parent prints message */
    711 	}
    712 #endif
    713 	exit(0);
    714 }
    715 
    716 #ifdef COMPAT
    717 const char lmsg[] = "%s: can't read disk label; disk type must be specified";
    718 #else
    719 const char lmsg[] = "%s: can't read disk label";
    720 #endif
    721 
    722 static struct disklabel *
    723 getdisklabel(char *s, volatile int fd)
    724 /* XXX why is fs volatile?! */
    725 {
    726 	static struct disklabel lab;
    727 
    728 	if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
    729 #ifdef COMPAT
    730 		if (disktype) {
    731 			struct disklabel *lp;
    732 
    733 			unlabeled++;
    734 			lp = getdiskbyname(disktype);
    735 			if (lp == NULL)
    736 				errx(1, "%s: unknown disk type", disktype);
    737 			return (lp);
    738 		}
    739 #endif
    740 		warn("ioctl (GDINFO)");
    741 		errx(1, lmsg, s);
    742 	}
    743 	return (&lab);
    744 }
    745 
    746 static void
    747 rewritelabel(char *s, volatile int fd, struct disklabel *lp)
    748 /* XXX why is fd volatile?! */
    749 {
    750 #ifdef COMPAT
    751 	if (unlabeled)
    752 		return;
    753 #endif
    754 	lp->d_checksum = 0;
    755 	lp->d_checksum = dkcksum(lp);
    756 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    757 		warn("ioctl (WDINFO)");
    758 		errx(1, "%s: can't rewrite disk label", s);
    759 	}
    760 #if __vax__
    761 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    762 		int i;
    763 		int cfd;
    764 		daddr_t alt;
    765 		char specname[64];
    766 		char blk[1024];
    767 		char *cp;
    768 
    769 		/*
    770 		 * Make name for 'c' partition.
    771 		 */
    772 		strcpy(specname, s);
    773 		cp = specname + strlen(specname) - 1;
    774 		if (!isdigit(*cp))
    775 			*cp = 'c';
    776 		cfd = open(specname, O_WRONLY);
    777 		if (cfd < 0)
    778 			err(1, "%s: open", specname);
    779 		memset(blk, 0, sizeof(blk));
    780 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
    781 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    782 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    783 			off_t offset;
    784 
    785 			offset = alt + i;
    786 			offset *= lp->d_secsize;
    787 			if (lseek(cfd, offset, SEEK_SET) == -1)
    788 				err(1, "lseek to badsector area: ");
    789 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    790 				warn("alternate label %d write", i/2);
    791 		}
    792 		close(cfd);
    793 	}
    794 #endif
    795 }
    796 
    797 static int
    798 strsuftoi(const char *desc, const char *arg, int min, int max)
    799 {
    800 	long long result;
    801 	char	*ep;
    802 
    803 	errno = 0;
    804 	result = strtoll(arg, &ep, 10);
    805 	if (ep[0] != '\0' && ep[1] != '\0')
    806 		errx(1, "%s `%s' is not a valid number.", desc, arg);
    807 	switch (tolower((unsigned char)ep[0])) {
    808 	case '\0':
    809 	case 'b':
    810 		break;
    811 	case 'k':
    812 		result <<= 10;
    813 		break;
    814 	case 'm':
    815 		result <<= 20;
    816 		break;
    817 	case 'g':
    818 		result <<= 30;
    819 		break;
    820 	default:
    821 		errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
    822 	}
    823 	if (result < min)
    824 		errx(1, "%s `%s' (%lld) is less than minimum (%d).",
    825 		    desc, arg, result, min);
    826 	if (result > max)
    827 		errx(1, "%s `%s' (%lld) is greater than maximum (%d).",
    828 		    desc, arg, result, max);
    829 	return ((int)result);
    830 }
    831 
    832 static void
    833 usage(void)
    834 {
    835 
    836 	if (mfs) {
    837 		fprintf(stderr,
    838 		    "usage: %s [ fsoptions ] special-device mount-point\n",
    839 			getprogname());
    840 	} else
    841 		fprintf(stderr,
    842 		    "usage: %s [ fsoptions ] special-device%s\n",
    843 		    getprogname(),
    844 #ifdef COMPAT
    845 		    " [device-type]");
    846 #else
    847 		    "");
    848 #endif
    849 	fprintf(stderr, "where fsoptions are:\n");
    850 	if (!mfs) {
    851 		fprintf(stderr,
    852 			"\t-B byteorder\tbyte order (`be' or `le')\n");
    853 		fprintf(stderr,
    854 			"\t-F\t\tcreate file system image in regular file\n");
    855 	}
    856 	fprintf(stderr, "\t-N\t\tdo not create file system, "
    857 			"just print out parameters\n");
    858 	if (!mfs) {
    859 		fprintf(stderr,
    860 			"\t-O\t\tcreate a 4.3BSD format filesystem\n");
    861 		fprintf(stderr,
    862 			"\t-S secsize\tsector size\n");
    863 	}
    864 #ifdef COMPAT
    865 	fprintf(stderr, "\t-T disktype\tdisk type\n");
    866 #endif
    867 	if (!mfs)
    868 		fprintf(stderr,
    869 			"\t-Z\t\tpre-zero the image file (with -F)\n");
    870 	fprintf(stderr, "\t-a maxcontig\tmaximum contiguous blocks\n");
    871 	fprintf(stderr, "\t-b bsize\tblock size\n");
    872 	fprintf(stderr, "\t-c cpg\t\tcylinders/group\n");
    873 	fprintf(stderr, "\t-d rotdelay\trotational delay between "
    874 			"contiguous blocks\n");
    875 	fprintf(stderr, "\t-e maxbpg\tmaximum blocks per file "
    876 			"in a cylinder group\n");
    877 	fprintf(stderr, "\t-f fsize\tfragment size\n");
    878 	fprintf(stderr, "\t-g average file size\n");
    879 	fprintf(stderr, "\t-h average files per directory\n");
    880 	fprintf(stderr, "\t-i density\tnumber of bytes per inode\n");
    881 	if (!mfs) {
    882 		fprintf(stderr,
    883 			"\t-k trackskew\tsector 0 skew, per track\n");
    884 		fprintf(stderr,
    885 			"\t-l interleave\thardware sector interleave\n");
    886 	}
    887 	fprintf(stderr, "\t-m minfree\tminimum free space %%\n");
    888 	if (!mfs)
    889 		fprintf(stderr,
    890 			"\t-n nrpos\tnumber of distinguished "
    891 			"rotational positions\n");
    892 	fprintf(stderr, "\t-o optim\toptimization preference "
    893 			"(`space' or `time')\n");
    894 	if (!mfs)
    895 		fprintf(stderr,
    896 			"\t-p trackspares\tspare sectors per track\n");
    897 	fprintf(stderr, "\t-s fssize\tfile system size (sectors)\n");
    898 	if (!mfs) {
    899 		fprintf(stderr,
    900 			"\t-r rpm\t\trevolutions/minute\n");
    901 		fprintf(stderr,
    902 			"\t-t ntracks\ttracks/cylinder\n");
    903 		fprintf(stderr,
    904 			"\t-u nsectors\tsectors/track\n");
    905 		fprintf(stderr,
    906 			"\t-x cylspares\tspare sectors per cylinder\n");
    907 	}
    908 	exit(1);
    909 }
    910