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