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