Home | History | Annotate | Line # | Download | only in newfs
newfs.c revision 1.11
      1 /*
      2  * Copyright (c) 1983, 1989, 1993, 1994
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char copyright[] =
     36 "@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
     37 	The Regents of the University of California.  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)newfs.c	8.8 (Berkeley) 4/18/94";*/
     42 static char *rcsid = "$Id: newfs.c,v 1.11 1994/09/23 14:27:42 mycroft Exp $";
     43 #endif /* not lint */
     44 
     45 /*
     46  * newfs: friendly front end to mkfs
     47  */
     48 #include <sys/param.h>
     49 #include <sys/stat.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/disklabel.h>
     52 #include <sys/file.h>
     53 #include <sys/mount.h>
     54 
     55 #include <ufs/ufs/dir.h>
     56 #include <ufs/ffs/fs.h>
     57 
     58 #include <ctype.h>
     59 #include <errno.h>
     60 #include <paths.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <syslog.h>
     65 #include <unistd.h>
     66 
     67 #if __STDC__
     68 #include <stdarg.h>
     69 #else
     70 #include <varargs.h>
     71 #endif
     72 
     73 #include "mntopts.h"
     74 
     75 struct mntopt mopts[] = {
     76 	MOPT_STDOPTS,
     77 	MOPT_ASYNC,
     78 	{ NULL },
     79 };
     80 
     81 #if __STDC__
     82 void	fatal(const char *fmt, ...);
     83 #else
     84 void	fatal();
     85 #endif
     86 
     87 #define	COMPAT			/* allow non-labeled disks */
     88 
     89 /*
     90  * The following two constants set the default block and fragment sizes.
     91  * Both constants must be a power of 2 and meet the following constraints:
     92  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
     93  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
     94  *	DESBLKSIZE / DESFRAGSIZE <= 8
     95  */
     96 #define	DFL_FRAGSIZE	1024
     97 #define	DFL_BLKSIZE	8192
     98 
     99 /*
    100  * Cylinder groups may have up to many cylinders. The actual
    101  * number used depends upon how much information can be stored
    102  * on a single cylinder. The default is to use 16 cylinders
    103  * per group.
    104  */
    105 #define	DESCPG		16	/* desired fs_cpg */
    106 
    107 /*
    108  * ROTDELAY gives the minimum number of milliseconds to initiate
    109  * another disk transfer on the same cylinder. It is used in
    110  * determining the rotationally optimal layout for disk blocks
    111  * within a file; the default of fs_rotdelay is 4ms.
    112  */
    113 #define ROTDELAY	4
    114 
    115 /*
    116  * MAXBLKPG determines the maximum number of data blocks which are
    117  * placed in a single cylinder group. The default is one indirect
    118  * block worth of data blocks.
    119  */
    120 #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
    121 
    122 /*
    123  * Each file system has a number of inodes statically allocated.
    124  * We allocate one inode slot per NFPI fragments, expecting this
    125  * to be far more than we will ever need.
    126  */
    127 #define	NFPI		4
    128 
    129 /*
    130  * For each cylinder we keep track of the availability of blocks at different
    131  * rotational positions, so that we can lay out the data to be picked
    132  * up with minimum rotational latency.  NRPOS is the default number of
    133  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
    134  * of our summary information is 2ms for a typical 3600 rpm drive.
    135  */
    136 #define	NRPOS		8	/* number distinct rotational positions */
    137 
    138 
    139 int	mfs;			/* run as the memory based filesystem */
    140 int	Nflag;			/* run without writing file system */
    141 int	Oflag;			/* format as an 4.3BSD file system */
    142 int	fssize;			/* file system size */
    143 int	ntracks;		/* # tracks/cylinder */
    144 int	nsectors;		/* # sectors/track */
    145 int	nphyssectors;		/* # sectors/track including spares */
    146 int	secpercyl;		/* sectors per cylinder */
    147 int	trackspares = -1;	/* spare sectors per track */
    148 int	cylspares = -1;		/* spare sectors per cylinder */
    149 int	sectorsize;		/* bytes/sector */
    150 #ifdef tahoe
    151 int	realsectorsize;		/* bytes/sector in hardware */
    152 #endif
    153 int	rpm;			/* revolutions/minute of drive */
    154 int	interleave;		/* hardware sector interleave */
    155 int	trackskew = -1;		/* sector 0 skew, per track */
    156 int	headswitch;		/* head switch time, usec */
    157 int	trackseek;		/* track-to-track seek, usec */
    158 int	fsize = 0;		/* fragment size */
    159 int	bsize = 0;		/* block size */
    160 int	cpg = DESCPG;		/* cylinders/cylinder group */
    161 int	cpgflg;			/* cylinders/cylinder group flag was given */
    162 int	minfree = MINFREE;	/* free space threshold */
    163 int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
    164 int	density;		/* number of bytes per inode */
    165 int	maxcontig = 0;		/* max contiguous blocks to allocate */
    166 int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
    167 int	maxbpg;			/* maximum blocks per file in a cyl group */
    168 int	nrpos = NRPOS;		/* # of distinguished rotational positions */
    169 int	bbsize = BBSIZE;	/* boot block size */
    170 int	sbsize = SBSIZE;	/* superblock size */
    171 int	mntflags = MNT_ASYNC;	/* flags to be passed to mount */
    172 u_long	memleft;		/* virtual memory available */
    173 caddr_t	membase;		/* start address of memory based filesystem */
    174 #ifdef COMPAT
    175 char	*disktype;
    176 int	unlabeled;
    177 #endif
    178 
    179 char	device[MAXPATHLEN];
    180 char	*progname;
    181 
    182 int
    183 main(argc, argv)
    184 	int argc;
    185 	char *argv[];
    186 {
    187 	extern char *optarg;
    188 	extern int optind;
    189 	register int ch;
    190 	register struct partition *pp;
    191 	register struct disklabel *lp;
    192 	struct disklabel *getdisklabel();
    193 	struct partition oldpartition;
    194 	struct stat st;
    195 	struct statfs *mp;
    196 	int fsi, fso, len, n;
    197 	char *cp, *s1, *s2, *special, *opstring, buf[BUFSIZ];
    198 
    199 	if (progname = strrchr(*argv, '/'))
    200 		++progname;
    201 	else
    202 		progname = *argv;
    203 
    204 	if (strstr(progname, "mfs")) {
    205 		mfs = 1;
    206 		Nflag++;
    207 	}
    208 
    209 	opstring = mfs ?
    210 	    "NT:a:b:c:d:e:f:i:m:o:s:" :
    211 	    "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
    212 	while ((ch = getopt(argc, argv, opstring)) != EOF)
    213 		switch (ch) {
    214 		case 'N':
    215 			Nflag = 1;
    216 			break;
    217 		case 'O':
    218 			Oflag = 1;
    219 			break;
    220 		case 'S':
    221 			if ((sectorsize = atoi(optarg)) <= 0)
    222 				fatal("%s: bad sector size", optarg);
    223 			break;
    224 #ifdef COMPAT
    225 		case 'T':
    226 			disktype = optarg;
    227 			break;
    228 #endif
    229 		case 'a':
    230 			if ((maxcontig = atoi(optarg)) <= 0)
    231 				fatal("%s: bad maximum contiguous blocks\n",
    232 				    optarg);
    233 			break;
    234 		case 'b':
    235 			if ((bsize = atoi(optarg)) < MINBSIZE)
    236 				fatal("%s: bad block size", optarg);
    237 			break;
    238 		case 'c':
    239 			if ((cpg = atoi(optarg)) <= 0)
    240 				fatal("%s: bad cylinders/group", optarg);
    241 			cpgflg++;
    242 			break;
    243 		case 'd':
    244 			if ((rotdelay = atoi(optarg)) < 0)
    245 				fatal("%s: bad rotational delay\n", optarg);
    246 			break;
    247 		case 'e':
    248 			if ((maxbpg = atoi(optarg)) <= 0)
    249 		fatal("%s: bad blocks per file in a cylinder group\n",
    250 				    optarg);
    251 			break;
    252 		case 'f':
    253 			if ((fsize = atoi(optarg)) <= 0)
    254 				fatal("%s: bad fragment size", optarg);
    255 			break;
    256 		case 'i':
    257 			if ((density = atoi(optarg)) <= 0)
    258 				fatal("%s: bad bytes per inode\n", optarg);
    259 			break;
    260 		case 'k':
    261 			if ((trackskew = atoi(optarg)) < 0)
    262 				fatal("%s: bad track skew", optarg);
    263 			break;
    264 		case 'l':
    265 			if ((interleave = atoi(optarg)) <= 0)
    266 				fatal("%s: bad interleave", optarg);
    267 			break;
    268 		case 'm':
    269 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
    270 				fatal("%s: bad free space %%\n", optarg);
    271 			break;
    272 		case 'n':
    273 			if ((nrpos = atoi(optarg)) <= 0)
    274 				fatal("%s: bad rotational layout count\n",
    275 				    optarg);
    276 			break;
    277 		case 'o':
    278 			if (mfs)
    279 				getmntopts(optarg, mopts, &mntflags);
    280 			else {
    281 				if (strcmp(optarg, "space") == 0)
    282 					opt = FS_OPTSPACE;
    283 				else if (strcmp(optarg, "time") == 0)
    284 					opt = FS_OPTTIME;
    285 				else
    286 	fatal("%s: unknown optimization preference: use `space' or `time'.");
    287 			}
    288 			break;
    289 		case 'p':
    290 			if ((trackspares = atoi(optarg)) < 0)
    291 				fatal("%s: bad spare sectors per track",
    292 				    optarg);
    293 			break;
    294 		case 'r':
    295 			if ((rpm = atoi(optarg)) <= 0)
    296 				fatal("%s: bad revolutions/minute\n", optarg);
    297 			break;
    298 		case 's':
    299 			if ((fssize = atoi(optarg)) <= 0)
    300 				fatal("%s: bad file system size", optarg);
    301 			break;
    302 		case 't':
    303 			if ((ntracks = atoi(optarg)) <= 0)
    304 				fatal("%s: bad total tracks", optarg);
    305 			break;
    306 		case 'u':
    307 			if ((nsectors = atoi(optarg)) <= 0)
    308 				fatal("%s: bad sectors/track", optarg);
    309 			break;
    310 		case 'x':
    311 			if ((cylspares = atoi(optarg)) < 0)
    312 				fatal("%s: bad spare sectors per cylinder",
    313 				    optarg);
    314 			break;
    315 		case '?':
    316 		default:
    317 			usage();
    318 		}
    319 	argc -= optind;
    320 	argv += optind;
    321 
    322 	if (argc != 2 && (mfs || argc != 1))
    323 		usage();
    324 
    325 	special = argv[0];
    326 	cp = strrchr(special, '/');
    327 	if (cp == 0) {
    328 		/*
    329 		 * No path prefix; try /dev/r%s then /dev/%s.
    330 		 */
    331 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
    332 		if (stat(device, &st) == -1)
    333 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
    334 		special = device;
    335 	}
    336 	if (Nflag) {
    337 		fso = -1;
    338 	} else {
    339 		fso = open(special, O_WRONLY);
    340 		if (fso < 0)
    341 			fatal("%s: %s", special, strerror(errno));
    342 
    343 		/* Bail if target special is mounted */
    344 		n = getmntinfo(&mp, MNT_NOWAIT);
    345 		if (n == 0)
    346 			fatal("%s: getmntinfo: %s", special, strerror(errno));
    347 
    348 		len = sizeof(_PATH_DEV) - 1;
    349 		s1 = special;
    350 		if (strncmp(_PATH_DEV, s1, len) == 0)
    351 			s1 += len;
    352 
    353 		while (--n >= 0) {
    354 			s2 = mp->f_mntfromname;
    355 			if (strncmp(_PATH_DEV, s2, len) == 0) {
    356 				s2 += len - 1;
    357 				*s2 = 'r';
    358 			}
    359 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
    360 				fatal("%s is mounted on %s",
    361 				    special, mp->f_mntonname);
    362 			++mp;
    363 		}
    364 	}
    365 	if (mfs && disktype != NULL) {
    366 		lp = (struct disklabel *)getdiskbyname(disktype);
    367 		if (lp == NULL)
    368 			fatal("%s: unknown disk type", disktype);
    369 		pp = &lp->d_partitions[1];
    370 	} else {
    371 		fsi = open(special, O_RDONLY);
    372 		if (fsi < 0)
    373 			fatal("%s: %s", special, strerror(errno));
    374 		if (fstat(fsi, &st) < 0)
    375 			fatal("%s: %s", special, strerror(errno));
    376 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
    377 			printf("%s: %s: not a character-special device\n",
    378 			    progname, special);
    379 		cp = strchr(argv[0], '\0') - 1;
    380 		if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
    381 			fatal("%s: can't figure out file system partition",
    382 			    argv[0]);
    383 #ifdef COMPAT
    384 		if (!mfs && disktype == NULL)
    385 			disktype = argv[1];
    386 #endif
    387 		lp = getdisklabel(special, fsi);
    388 		if (isdigit(*cp))
    389 			pp = &lp->d_partitions[0];
    390 		else
    391 			pp = &lp->d_partitions[*cp - 'a'];
    392 		if (pp->p_size == 0)
    393 			fatal("%s: `%c' partition is unavailable",
    394 			    argv[0], *cp);
    395 		if (pp->p_fstype == FS_BOOT)
    396 			fatal("%s: `%c' partition overlaps boot program",
    397 			      argv[0], *cp);
    398 	}
    399 	if (fssize == 0)
    400 		fssize = pp->p_size;
    401 	if (fssize > pp->p_size && !mfs)
    402 	       fatal("%s: maximum file system size on the `%c' partition is %d",
    403 			argv[0], *cp, pp->p_size);
    404 	if (rpm == 0) {
    405 		rpm = lp->d_rpm;
    406 		if (rpm <= 0)
    407 			rpm = 3600;
    408 	}
    409 	if (ntracks == 0) {
    410 		ntracks = lp->d_ntracks;
    411 		if (ntracks <= 0)
    412 			fatal("%s: no default #tracks", argv[0]);
    413 	}
    414 	if (nsectors == 0) {
    415 		nsectors = lp->d_nsectors;
    416 		if (nsectors <= 0)
    417 			fatal("%s: no default #sectors/track", argv[0]);
    418 	}
    419 	if (sectorsize == 0) {
    420 		sectorsize = lp->d_secsize;
    421 		if (sectorsize <= 0)
    422 			fatal("%s: no default sector size", argv[0]);
    423 	}
    424 	if (trackskew == -1) {
    425 		trackskew = lp->d_trackskew;
    426 		if (trackskew < 0)
    427 			trackskew = 0;
    428 	}
    429 	if (interleave == 0) {
    430 		interleave = lp->d_interleave;
    431 		if (interleave <= 0)
    432 			interleave = 1;
    433 	}
    434 	if (fsize == 0) {
    435 		fsize = pp->p_fsize;
    436 		if (fsize <= 0)
    437 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
    438 	}
    439 	if (bsize == 0) {
    440 		bsize = pp->p_frag * pp->p_fsize;
    441 		if (bsize <= 0)
    442 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
    443 	}
    444 	/*
    445 	 * Maxcontig sets the default for the maximum number of blocks
    446 	 * that may be allocated sequentially. With filesystem clustering
    447 	 * it is possible to allocate contiguous blocks up to the maximum
    448 	 * transfer size permitted by the controller or buffering.
    449 	 */
    450 	if (maxcontig == 0)
    451 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize - 1);
    452 	if (density == 0)
    453 		density = NFPI * fsize;
    454 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
    455 		fprintf(stderr, "Warning: changing optimization to space ");
    456 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
    457 		opt = FS_OPTSPACE;
    458 	}
    459 	if (trackspares == -1) {
    460 		trackspares = lp->d_sparespertrack;
    461 		if (trackspares < 0)
    462 			trackspares = 0;
    463 	}
    464 	nphyssectors = nsectors + trackspares;
    465 	if (cylspares == -1) {
    466 		cylspares = lp->d_sparespercyl;
    467 		if (cylspares < 0)
    468 			cylspares = 0;
    469 	}
    470 	secpercyl = nsectors * ntracks - cylspares;
    471 	if (secpercyl != lp->d_secpercyl)
    472 		fprintf(stderr, "%s (%d) %s (%lu)\n",
    473 			"Warning: calculated sectors per cylinder", secpercyl,
    474 			"disagrees with disk label", lp->d_secpercyl);
    475 	if (maxbpg == 0)
    476 		maxbpg = MAXBLKPG(bsize);
    477 	headswitch = lp->d_headswitch;
    478 	trackseek = lp->d_trkseek;
    479 #ifdef notdef /* label may be 0 if faked up by kernel */
    480 	bbsize = lp->d_bbsize;
    481 	sbsize = lp->d_sbsize;
    482 #endif
    483 	oldpartition = *pp;
    484 #ifdef tahoe
    485 	realsectorsize = sectorsize;
    486 	if (sectorsize != DEV_BSIZE) {		/* XXX */
    487 		int secperblk = DEV_BSIZE / sectorsize;
    488 
    489 		sectorsize = DEV_BSIZE;
    490 		nsectors /= secperblk;
    491 		nphyssectors /= secperblk;
    492 		secpercyl /= secperblk;
    493 		fssize /= secperblk;
    494 		pp->p_size /= secperblk;
    495 	}
    496 #endif
    497 	mkfs(pp, special, fsi, fso);
    498 #ifdef tahoe
    499 	if (realsectorsize != DEV_BSIZE)
    500 		pp->p_size *= DEV_BSIZE / realsectorsize;
    501 #endif
    502 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
    503 		rewritelabel(special, fso, lp);
    504 	if (!Nflag)
    505 		close(fso);
    506 	close(fsi);
    507 #ifdef MFS
    508 	if (mfs) {
    509 		struct mfs_args args;
    510 
    511 		sprintf(buf, "mfs:%d", getpid());
    512 		args.fspec = buf;
    513 		args.export.ex_root = -2;
    514 		if (mntflags & MNT_RDONLY)
    515 			args.export.ex_flags = MNT_EXRDONLY;
    516 		else
    517 			args.export.ex_flags = 0;
    518 		args.base = membase;
    519 		args.size = fssize * sectorsize;
    520 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
    521 			fatal("%s: %s", argv[1], strerror(errno));
    522 	}
    523 #endif
    524 	exit(0);
    525 }
    526 
    527 #ifdef COMPAT
    528 char lmsg[] = "%s: can't read disk label; disk type must be specified";
    529 #else
    530 char lmsg[] = "%s: can't read disk label";
    531 #endif
    532 
    533 struct disklabel *
    534 getdisklabel(s, fd)
    535 	char *s;
    536 	int fd;
    537 {
    538 	static struct disklabel lab;
    539 
    540 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    541 #ifdef COMPAT
    542 		if (disktype) {
    543 			struct disklabel *lp, *getdiskbyname();
    544 
    545 			unlabeled++;
    546 			lp = getdiskbyname(disktype);
    547 			if (lp == NULL)
    548 				fatal("%s: unknown disk type", disktype);
    549 			return (lp);
    550 		}
    551 #endif
    552 		warn("ioctl (GDINFO)");
    553 		fatal(lmsg, s);
    554 	}
    555 	return (&lab);
    556 }
    557 
    558 rewritelabel(s, fd, lp)
    559 	char *s;
    560 	int fd;
    561 	register struct disklabel *lp;
    562 {
    563 #ifdef COMPAT
    564 	if (unlabeled)
    565 		return;
    566 #endif
    567 	lp->d_checksum = 0;
    568 	lp->d_checksum = dkcksum(lp);
    569 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    570 		warn("ioctl (WDINFO)");
    571 		fatal("%s: can't rewrite disk label", s);
    572 	}
    573 #if vax
    574 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    575 		register i;
    576 		int cfd;
    577 		daddr_t alt;
    578 		char specname[64];
    579 		char blk[1024];
    580 		char *cp;
    581 
    582 		/*
    583 		 * Make name for 'c' partition.
    584 		 */
    585 		strcpy(specname, s);
    586 		cp = specname + strlen(specname) - 1;
    587 		if (!isdigit(*cp))
    588 			*cp = 'c';
    589 		cfd = open(specname, O_WRONLY);
    590 		if (cfd < 0)
    591 			fatal("%s: %s", specname, strerror(errno));
    592 		memset(blk, 0, sizeof(blk));
    593 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
    594 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    595 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    596 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
    597 			    L_SET) == -1)
    598 				fatal("lseek to badsector area: %s",
    599 				    strerror(errno));
    600 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    601 				warn("alternate label %d write", i/2);
    602 		}
    603 		close(cfd);
    604 	}
    605 #endif
    606 }
    607 
    608 /*VARARGS*/
    609 void
    610 #if __STDC__
    611 fatal(const char *fmt, ...)
    612 #else
    613 fatal(fmt, va_alist)
    614 	char *fmt;
    615 	va_dcl
    616 #endif
    617 {
    618 	va_list ap;
    619 
    620 #if __STDC__
    621 	va_start(ap, fmt);
    622 #else
    623 	va_start(ap);
    624 #endif
    625 	if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
    626 		openlog(progname, LOG_CONS, LOG_DAEMON);
    627 		vsyslog(LOG_ERR, fmt, ap);
    628 		closelog();
    629 	} else {
    630 		vwarnx(fmt, ap);
    631 	}
    632 	va_end(ap);
    633 	exit(1);
    634 	/*NOTREACHED*/
    635 }
    636 
    637 usage()
    638 {
    639 	if (mfs) {
    640 		fprintf(stderr,
    641 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
    642 			progname);
    643 	} else
    644 		fprintf(stderr,
    645 		    "usage: %s [ -fsoptions ] special-device%s\n",
    646 		    progname,
    647 #ifdef COMPAT
    648 		    " [device-type]");
    649 #else
    650 		    "");
    651 #endif
    652 	fprintf(stderr, "where fsoptions are:\n");
    653 	fprintf(stderr,
    654 	    "\t-N do not create file system, just print out parameters\n");
    655 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
    656 	fprintf(stderr, "\t-S sector size\n");
    657 #ifdef COMPAT
    658 	fprintf(stderr, "\t-T disktype\n");
    659 #endif
    660 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    661 	fprintf(stderr, "\t-b block size\n");
    662 	fprintf(stderr, "\t-c cylinders/group\n");
    663 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    664 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    665 	fprintf(stderr, "\t-f frag size\n");
    666 	fprintf(stderr, "\t-i number of bytes per inode\n");
    667 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
    668 	fprintf(stderr, "\t-l hardware sector interleave\n");
    669 	fprintf(stderr, "\t-m minimum free space %%\n");
    670 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
    671 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    672 	fprintf(stderr, "\t-p spare sectors per track\n");
    673 	fprintf(stderr, "\t-s file system size (sectors)\n");
    674 	fprintf(stderr, "\t-r revolutions/minute\n");
    675 	fprintf(stderr, "\t-t tracks/cylinder\n");
    676 	fprintf(stderr, "\t-u sectors/track\n");
    677 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
    678 	exit(1);
    679 }
    680