Home | History | Annotate | Line # | Download | only in newfs
newfs.c revision 1.22
      1 /*	$NetBSD: newfs.c,v 1.22 1996/10/23 22:46:17 cgd 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 #ifndef lint
     37 static char copyright[] =
     38 "@(#) 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.8 (Berkeley) 4/18/94";
     45 #else
     46 static char rcsid[] = "$NetBSD: newfs.c,v 1.22 1996/10/23 22:46:17 cgd 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 
     61 #include <ufs/ufs/dir.h>
     62 #include <ufs/ffs/fs.h>
     63 
     64 #include <ctype.h>
     65 #include <errno.h>
     66 #include <paths.h>
     67 #include <stdio.h>
     68 #include <stdlib.h>
     69 #include <string.h>
     70 #include <syslog.h>
     71 #include <unistd.h>
     72 #include <util.h>
     73 
     74 #if __STDC__
     75 #include <stdarg.h>
     76 #else
     77 #include <varargs.h>
     78 #endif
     79 
     80 #include "mntopts.h"
     81 
     82 struct mntopt mopts[] = {
     83 	MOPT_STDOPTS,
     84 	MOPT_ASYNC,
     85 	MOPT_UPDATE,
     86 	{ NULL },
     87 };
     88 
     89 #if __STDC__
     90 void	fatal(const char *fmt, ...);
     91 #else
     92 void	fatal();
     93 #endif
     94 
     95 #define	COMPAT			/* allow non-labeled disks */
     96 
     97 /*
     98  * The following two constants set the default block and fragment sizes.
     99  * Both constants must be a power of 2 and meet the following constraints:
    100  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
    101  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
    102  *	DESBLKSIZE / DESFRAGSIZE <= 8
    103  */
    104 #define	DFL_FRAGSIZE	1024
    105 #define	DFL_BLKSIZE	8192
    106 
    107 /*
    108  * Cylinder groups may have up to many cylinders. The actual
    109  * number used depends upon how much information can be stored
    110  * on a single cylinder. The default is to use 16 cylinders
    111  * per group.
    112  */
    113 #define	DESCPG		16	/* desired fs_cpg */
    114 
    115 /*
    116  * ROTDELAY gives the minimum number of milliseconds to initiate
    117  * another disk transfer on the same cylinder. It is used in
    118  * determining the rotationally optimal layout for disk blocks
    119  * within a file; the default of fs_rotdelay is 0ms.
    120  */
    121 #define ROTDELAY	0
    122 
    123 /*
    124  * MAXBLKPG determines the maximum number of data blocks which are
    125  * placed in a single cylinder group. The default is one indirect
    126  * block worth of data blocks.
    127  */
    128 #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
    129 
    130 /*
    131  * Each file system has a number of inodes statically allocated.
    132  * We allocate one inode slot per NFPI fragments, expecting this
    133  * to be far more than we will ever need.
    134  */
    135 #define	NFPI		4
    136 
    137 /*
    138  * For each cylinder we keep track of the availability of blocks at different
    139  * rotational positions, so that we can lay out the data to be picked
    140  * up with minimum rotational latency.  NRPOS is the default number of
    141  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
    142  * of our summary information is 2ms for a typical 3600 rpm drive.  Caching
    143  * and zoning pretty much defeats rotational optimization, so we now use a
    144  * default of 1.
    145  */
    146 #define	NRPOS		1	/* number distinct rotational positions */
    147 
    148 
    149 int	mfs;			/* run as the memory based filesystem */
    150 int	Nflag;			/* run without writing file system */
    151 int	Oflag;			/* format as an 4.3BSD file system */
    152 int	fssize;			/* file system size */
    153 int	ntracks;		/* # tracks/cylinder */
    154 int	nsectors;		/* # sectors/track */
    155 int	nphyssectors;		/* # sectors/track including spares */
    156 int	secpercyl;		/* sectors per cylinder */
    157 int	trackspares = -1;	/* spare sectors per track */
    158 int	cylspares = -1;		/* spare sectors per cylinder */
    159 int	sectorsize;		/* bytes/sector */
    160 int	rpm;			/* revolutions/minute of drive */
    161 int	interleave;		/* hardware sector interleave */
    162 int	trackskew = -1;		/* sector 0 skew, per track */
    163 int	headswitch;		/* head switch time, usec */
    164 int	trackseek;		/* track-to-track seek, usec */
    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 = 8;		/* 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 #ifdef COMPAT
    182 char	*disktype;
    183 int	unlabeled;
    184 #endif
    185 
    186 char	device[MAXPATHLEN];
    187 char	*progname;
    188 
    189 int
    190 main(argc, argv)
    191 	int argc;
    192 	char *argv[];
    193 {
    194 	extern char *optarg;
    195 	extern int optind;
    196 	register int ch;
    197 	register struct partition *pp;
    198 	register struct disklabel *lp;
    199 	struct disklabel mfsfakelabel;
    200 	struct disklabel *getdisklabel();
    201 	struct partition oldpartition;
    202 	struct stat st;
    203 	struct statfs *mp;
    204 	int fsi, fso, len, n, maxpartitions;
    205 	char *cp, *s1, *s2, *special, *opstring, buf[BUFSIZ];
    206 
    207 	if (progname = strrchr(*argv, '/'))
    208 		++progname;
    209 	else
    210 		progname = *argv;
    211 
    212 	if (strstr(progname, "mfs")) {
    213 		mfs = 1;
    214 		Nflag++;
    215 	}
    216 
    217 	maxpartitions = getmaxpartitions();
    218 	if (maxpartitions > 26)
    219 		fatal("insane maxpartitions value %d", maxpartitions);
    220 
    221 	opstring = mfs ?
    222 	    "NT:a:b:c:d:e:f:i:m:o:s:" :
    223 	    "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
    224 	while ((ch = getopt(argc, argv, opstring)) != EOF)
    225 		switch (ch) {
    226 		case 'N':
    227 			Nflag = 1;
    228 			break;
    229 		case 'O':
    230 			Oflag = 1;
    231 			break;
    232 		case 'S':
    233 			if ((sectorsize = atoi(optarg)) <= 0)
    234 				fatal("%s: bad sector size", optarg);
    235 			break;
    236 #ifdef COMPAT
    237 		case 'T':
    238 			disktype = optarg;
    239 			break;
    240 #endif
    241 		case 'a':
    242 			if ((maxcontig = atoi(optarg)) <= 0)
    243 				fatal("%s: bad maximum contiguous blocks\n",
    244 				    optarg);
    245 			break;
    246 		case 'b':
    247 			if ((bsize = atoi(optarg)) < MINBSIZE)
    248 				fatal("%s: bad block size", optarg);
    249 			break;
    250 		case 'c':
    251 			if ((cpg = atoi(optarg)) <= 0)
    252 				fatal("%s: bad cylinders/group", optarg);
    253 			cpgflg++;
    254 			break;
    255 		case 'd':
    256 			if ((rotdelay = atoi(optarg)) < 0)
    257 				fatal("%s: bad rotational delay\n", optarg);
    258 			break;
    259 		case 'e':
    260 			if ((maxbpg = atoi(optarg)) <= 0)
    261 		fatal("%s: bad blocks per file in a cylinder group\n",
    262 				    optarg);
    263 			break;
    264 		case 'f':
    265 			if ((fsize = atoi(optarg)) <= 0)
    266 				fatal("%s: bad fragment size", optarg);
    267 			break;
    268 		case 'i':
    269 			if ((density = atoi(optarg)) <= 0)
    270 				fatal("%s: bad bytes per inode\n", optarg);
    271 			break;
    272 		case 'k':
    273 			if ((trackskew = atoi(optarg)) < 0)
    274 				fatal("%s: bad track skew", optarg);
    275 			break;
    276 		case 'l':
    277 			if ((interleave = atoi(optarg)) <= 0)
    278 				fatal("%s: bad interleave", optarg);
    279 			break;
    280 		case 'm':
    281 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
    282 				fatal("%s: bad free space %%\n", optarg);
    283 			break;
    284 		case 'n':
    285 			if ((nrpos = atoi(optarg)) <= 0)
    286 				fatal("%s: bad rotational layout count\n",
    287 				    optarg);
    288 			break;
    289 		case 'o':
    290 			if (mfs)
    291 				getmntopts(optarg, mopts, &mntflags);
    292 			else {
    293 				if (strcmp(optarg, "space") == 0)
    294 					opt = FS_OPTSPACE;
    295 				else if (strcmp(optarg, "time") == 0)
    296 					opt = FS_OPTTIME;
    297 				else
    298 	fatal("%s: unknown optimization preference: use `space' or `time'.");
    299 			}
    300 			break;
    301 		case 'p':
    302 			if ((trackspares = atoi(optarg)) < 0)
    303 				fatal("%s: bad spare sectors per track",
    304 				    optarg);
    305 			break;
    306 		case 'r':
    307 			if ((rpm = atoi(optarg)) <= 0)
    308 				fatal("%s: bad revolutions/minute\n", optarg);
    309 			break;
    310 		case 's':
    311 			if ((fssize = atoi(optarg)) <= 0)
    312 				fatal("%s: bad file system size", optarg);
    313 			break;
    314 		case 't':
    315 			if ((ntracks = atoi(optarg)) <= 0)
    316 				fatal("%s: bad total tracks", optarg);
    317 			break;
    318 		case 'u':
    319 			if ((nsectors = atoi(optarg)) <= 0)
    320 				fatal("%s: bad sectors/track", optarg);
    321 			break;
    322 		case 'x':
    323 			if ((cylspares = atoi(optarg)) < 0)
    324 				fatal("%s: bad spare sectors per cylinder",
    325 				    optarg);
    326 			break;
    327 		case '?':
    328 		default:
    329 			usage();
    330 		}
    331 	argc -= optind;
    332 	argv += optind;
    333 
    334 	if (argc != 2 && (mfs || argc != 1))
    335 		usage();
    336 
    337 	special = argv[0];
    338 	if (mfs && !strcmp(special, "swap")) {
    339 		/*
    340 		 * it's an MFS, mounted on "swap."  fake up a label.
    341 		 * XXX XXX XXX
    342 		 */
    343 		fso = -1;	/* XXX; normally done below. */
    344 
    345 		memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
    346 		mfsfakelabel.d_secsize = 512;
    347 		mfsfakelabel.d_nsectors = 64;
    348 		mfsfakelabel.d_ntracks = 16;
    349 		mfsfakelabel.d_ncylinders = 16;
    350 		mfsfakelabel.d_secpercyl = 1024;
    351 		mfsfakelabel.d_secperunit = 16384;
    352 		mfsfakelabel.d_rpm = 3600;
    353 		mfsfakelabel.d_interleave = 1;
    354 		mfsfakelabel.d_npartitions = 1;
    355 		mfsfakelabel.d_partitions[0].p_size = 16384;
    356 		mfsfakelabel.d_partitions[0].p_fsize = 1024;
    357 		mfsfakelabel.d_partitions[0].p_frag = 8;
    358 		mfsfakelabel.d_partitions[0].p_cpg = 16;
    359 
    360 		lp = &mfsfakelabel;
    361 		pp = &mfsfakelabel.d_partitions[0];
    362 
    363 		goto havelabel;
    364 	}
    365 	cp = strrchr(special, '/');
    366 	if (cp == 0) {
    367 		/*
    368 		 * No path prefix; try /dev/r%s then /dev/%s.
    369 		 */
    370 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
    371 		if (stat(device, &st) == -1)
    372 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
    373 		special = device;
    374 	}
    375 	if (Nflag) {
    376 		fso = -1;
    377 	} else {
    378 		fso = open(special, O_WRONLY);
    379 		if (fso < 0)
    380 			fatal("%s: %s", special, strerror(errno));
    381 
    382 		/* Bail if target special is mounted */
    383 		n = getmntinfo(&mp, MNT_NOWAIT);
    384 		if (n == 0)
    385 			fatal("%s: getmntinfo: %s", special, strerror(errno));
    386 
    387 		len = sizeof(_PATH_DEV) - 1;
    388 		s1 = special;
    389 		if (strncmp(_PATH_DEV, s1, len) == 0)
    390 			s1 += len;
    391 
    392 		while (--n >= 0) {
    393 			s2 = mp->f_mntfromname;
    394 			if (strncmp(_PATH_DEV, s2, len) == 0) {
    395 				s2 += len - 1;
    396 				*s2 = 'r';
    397 			}
    398 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
    399 				fatal("%s is mounted on %s",
    400 				    special, mp->f_mntonname);
    401 			++mp;
    402 		}
    403 	}
    404 	if (mfs && disktype != NULL) {
    405 		lp = (struct disklabel *)getdiskbyname(disktype);
    406 		if (lp == NULL)
    407 			fatal("%s: unknown disk type", disktype);
    408 		pp = &lp->d_partitions[1];
    409 	} else {
    410 		fsi = open(special, O_RDONLY);
    411 		if (fsi < 0)
    412 			fatal("%s: %s", special, strerror(errno));
    413 		if (fstat(fsi, &st) < 0)
    414 			fatal("%s: %s", special, strerror(errno));
    415 		if (!S_ISCHR(st.st_mode) && !mfs)
    416 			printf("%s: %s: not a character-special device\n",
    417 			    progname, special);
    418 		cp = strchr(argv[0], '\0') - 1;
    419 		if (cp == 0 || (*cp < 'a' || *cp > ('a' + maxpartitions - 1))
    420 		    && !isdigit(*cp))
    421 			fatal("%s: can't figure out file system partition",
    422 			    argv[0]);
    423 #ifdef COMPAT
    424 		if (!mfs && disktype == NULL)
    425 			disktype = argv[1];
    426 #endif
    427 		lp = getdisklabel(special, fsi);
    428 		if (isdigit(*cp))
    429 			pp = &lp->d_partitions[0];
    430 		else
    431 			pp = &lp->d_partitions[*cp - 'a'];
    432 		if (pp->p_size == 0)
    433 			fatal("%s: `%c' partition is unavailable",
    434 			    argv[0], *cp);
    435 		if (pp->p_fstype == FS_BOOT)
    436 			fatal("%s: `%c' partition overlaps boot program",
    437 			      argv[0], *cp);
    438 	}
    439 havelabel:
    440 	if (fssize == 0)
    441 		fssize = pp->p_size;
    442 	if (fssize > pp->p_size && !mfs)
    443 	       fatal("%s: maximum file system size on the `%c' partition is %d",
    444 			argv[0], *cp, pp->p_size);
    445 	if (rpm == 0) {
    446 		rpm = lp->d_rpm;
    447 		if (rpm <= 0)
    448 			rpm = 3600;
    449 	}
    450 	if (ntracks == 0) {
    451 		ntracks = lp->d_ntracks;
    452 		if (ntracks <= 0)
    453 			fatal("%s: no default #tracks", argv[0]);
    454 	}
    455 	if (nsectors == 0) {
    456 		nsectors = lp->d_nsectors;
    457 		if (nsectors <= 0)
    458 			fatal("%s: no default #sectors/track", argv[0]);
    459 	}
    460 	if (sectorsize == 0) {
    461 		sectorsize = lp->d_secsize;
    462 		if (sectorsize <= 0)
    463 			fatal("%s: no default sector size", argv[0]);
    464 	}
    465 	if (trackskew == -1) {
    466 		trackskew = lp->d_trackskew;
    467 		if (trackskew < 0)
    468 			trackskew = 0;
    469 	}
    470 	if (interleave == 0) {
    471 		interleave = lp->d_interleave;
    472 		if (interleave <= 0)
    473 			interleave = 1;
    474 	}
    475 	if (fsize == 0) {
    476 		fsize = pp->p_fsize;
    477 		if (fsize <= 0)
    478 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
    479 	}
    480 	if (bsize == 0) {
    481 		bsize = pp->p_frag * pp->p_fsize;
    482 		if (bsize <= 0)
    483 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
    484 	}
    485 	/*
    486 	 * Maxcontig sets the default for the maximum number of blocks
    487 	 * that may be allocated sequentially. With filesystem clustering
    488 	 * it is possible to allocate contiguous blocks up to the maximum
    489 	 * transfer size permitted by the controller or buffering.
    490 	 */
    491 	if (maxcontig == 0)
    492 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize - 1);
    493 	if (density == 0)
    494 		density = NFPI * fsize;
    495 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
    496 		fprintf(stderr, "Warning: changing optimization to space ");
    497 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
    498 		opt = FS_OPTSPACE;
    499 	}
    500 	if (trackspares == -1) {
    501 		trackspares = lp->d_sparespertrack;
    502 		if (trackspares < 0)
    503 			trackspares = 0;
    504 	}
    505 	nphyssectors = nsectors + trackspares;
    506 	if (cylspares == -1) {
    507 		cylspares = lp->d_sparespercyl;
    508 		if (cylspares < 0)
    509 			cylspares = 0;
    510 	}
    511 	secpercyl = nsectors * ntracks - cylspares;
    512 	if (secpercyl != lp->d_secpercyl)
    513 		fprintf(stderr, "%s (%d) %s (%lu)\n",
    514 			"Warning: calculated sectors per cylinder", secpercyl,
    515 			"disagrees with disk label", lp->d_secpercyl);
    516 	if (maxbpg == 0)
    517 		maxbpg = MAXBLKPG(bsize);
    518 	headswitch = lp->d_headswitch;
    519 	trackseek = lp->d_trkseek;
    520 #ifdef notdef /* label may be 0 if faked up by kernel */
    521 	bbsize = lp->d_bbsize;
    522 	sbsize = lp->d_sbsize;
    523 #endif
    524 	oldpartition = *pp;
    525 	mkfs(pp, special, fsi, fso);
    526 	if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)))
    527 		rewritelabel(special, fso, lp);
    528 	if (!Nflag)
    529 		close(fso);
    530 	close(fsi);
    531 #ifdef MFS
    532 	if (mfs) {
    533 		struct mfs_args args;
    534 
    535 		sprintf(buf, "mfs:%d", getpid());
    536 		args.fspec = buf;
    537 		args.export.ex_root = -2;
    538 		if (mntflags & MNT_RDONLY)
    539 			args.export.ex_flags = MNT_EXRDONLY;
    540 		else
    541 			args.export.ex_flags = 0;
    542 		args.base = membase;
    543 		args.size = fssize * sectorsize;
    544 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
    545 			fatal("%s: %s", argv[1], strerror(errno));
    546 	}
    547 #endif
    548 	exit(0);
    549 }
    550 
    551 #ifdef COMPAT
    552 char lmsg[] = "%s: can't read disk label; disk type must be specified";
    553 #else
    554 char lmsg[] = "%s: can't read disk label";
    555 #endif
    556 
    557 struct disklabel *
    558 getdisklabel(s, fd)
    559 	char *s;
    560 	int fd;
    561 {
    562 	static struct disklabel lab;
    563 
    564 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    565 #ifdef COMPAT
    566 		if (disktype) {
    567 			struct disklabel *lp, *getdiskbyname();
    568 
    569 			unlabeled++;
    570 			lp = getdiskbyname(disktype);
    571 			if (lp == NULL)
    572 				fatal("%s: unknown disk type", disktype);
    573 			return (lp);
    574 		}
    575 #endif
    576 		warn("ioctl (GDINFO)");
    577 		fatal(lmsg, s);
    578 	}
    579 	return (&lab);
    580 }
    581 
    582 rewritelabel(s, fd, lp)
    583 	char *s;
    584 	int fd;
    585 	register struct disklabel *lp;
    586 {
    587 #ifdef COMPAT
    588 	if (unlabeled)
    589 		return;
    590 #endif
    591 	lp->d_checksum = 0;
    592 	lp->d_checksum = dkcksum(lp);
    593 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    594 		warn("ioctl (WDINFO)");
    595 		fatal("%s: can't rewrite disk label", s);
    596 	}
    597 #if vax
    598 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    599 		register i;
    600 		int cfd;
    601 		daddr_t alt;
    602 		char specname[64];
    603 		char blk[1024];
    604 		char *cp;
    605 
    606 		/*
    607 		 * Make name for 'c' partition.
    608 		 */
    609 		strcpy(specname, s);
    610 		cp = specname + strlen(specname) - 1;
    611 		if (!isdigit(*cp))
    612 			*cp = 'c';
    613 		cfd = open(specname, O_WRONLY);
    614 		if (cfd < 0)
    615 			fatal("%s: %s", specname, strerror(errno));
    616 		memset(blk, 0, sizeof(blk));
    617 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
    618 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    619 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    620 			off_t offset;
    621 
    622 			offset = alt + i;
    623 			offset *= lp->d_secsize;
    624 			if (lseek(cfd, offset, SEEK_SET) == -1)
    625 				fatal("lseek to badsector area: %s",
    626 				    strerror(errno));
    627 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    628 				warn("alternate label %d write", i/2);
    629 		}
    630 		close(cfd);
    631 	}
    632 #endif
    633 }
    634 
    635 /*VARARGS*/
    636 void
    637 #if __STDC__
    638 fatal(const char *fmt, ...)
    639 #else
    640 fatal(fmt, va_alist)
    641 	char *fmt;
    642 	va_dcl
    643 #endif
    644 {
    645 	va_list ap;
    646 
    647 #if __STDC__
    648 	va_start(ap, fmt);
    649 #else
    650 	va_start(ap);
    651 #endif
    652 	if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
    653 		openlog(progname, LOG_CONS, LOG_DAEMON);
    654 		vsyslog(LOG_ERR, fmt, ap);
    655 		closelog();
    656 	} else {
    657 		vwarnx(fmt, ap);
    658 	}
    659 	va_end(ap);
    660 	exit(1);
    661 	/*NOTREACHED*/
    662 }
    663 
    664 usage()
    665 {
    666 	if (mfs) {
    667 		fprintf(stderr,
    668 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
    669 			progname);
    670 	} else
    671 		fprintf(stderr,
    672 		    "usage: %s [ -fsoptions ] special-device%s\n",
    673 		    progname,
    674 #ifdef COMPAT
    675 		    " [device-type]");
    676 #else
    677 		    "");
    678 #endif
    679 	fprintf(stderr, "where fsoptions are:\n");
    680 	fprintf(stderr,
    681 	    "\t-N do not create file system, just print out parameters\n");
    682 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
    683 	fprintf(stderr, "\t-S sector size\n");
    684 #ifdef COMPAT
    685 	fprintf(stderr, "\t-T disktype\n");
    686 #endif
    687 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    688 	fprintf(stderr, "\t-b block size\n");
    689 	fprintf(stderr, "\t-c cylinders/group\n");
    690 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    691 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    692 	fprintf(stderr, "\t-f frag size\n");
    693 	fprintf(stderr, "\t-i number of bytes per inode\n");
    694 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
    695 	fprintf(stderr, "\t-l hardware sector interleave\n");
    696 	fprintf(stderr, "\t-m minimum free space %%\n");
    697 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
    698 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    699 	fprintf(stderr, "\t-p spare sectors per track\n");
    700 	fprintf(stderr, "\t-s file system size (sectors)\n");
    701 	fprintf(stderr, "\t-r revolutions/minute\n");
    702 	fprintf(stderr, "\t-t tracks/cylinder\n");
    703 	fprintf(stderr, "\t-u sectors/track\n");
    704 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
    705 	exit(1);
    706 }
    707