Home | History | Annotate | Line # | Download | only in newfs
newfs.c revision 1.14
      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.14 1994/12/18 05:09:44 cgd 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 0ms.
    112  */
    113 #define ROTDELAY	0
    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.  Caching
    135  * and zoning pretty much defeats rotational optimization, so we now use a
    136  * default of 1.
    137  */
    138 #define	NRPOS		1	/* number distinct rotational positions */
    139 
    140 
    141 int	mfs;			/* run as the memory based filesystem */
    142 int	Nflag;			/* run without writing file system */
    143 int	Oflag;			/* format as an 4.3BSD file system */
    144 int	fssize;			/* file system size */
    145 int	ntracks;		/* # tracks/cylinder */
    146 int	nsectors;		/* # sectors/track */
    147 int	nphyssectors;		/* # sectors/track including spares */
    148 int	secpercyl;		/* sectors per cylinder */
    149 int	trackspares = -1;	/* spare sectors per track */
    150 int	cylspares = -1;		/* spare sectors per cylinder */
    151 int	sectorsize;		/* bytes/sector */
    152 #ifdef tahoe
    153 int	realsectorsize;		/* bytes/sector in hardware */
    154 #endif
    155 int	rpm;			/* revolutions/minute of drive */
    156 int	interleave;		/* hardware sector interleave */
    157 int	trackskew = -1;		/* sector 0 skew, per track */
    158 int	headswitch;		/* head switch time, usec */
    159 int	trackseek;		/* track-to-track seek, usec */
    160 int	fsize = 0;		/* fragment size */
    161 int	bsize = 0;		/* block size */
    162 int	cpg = DESCPG;		/* cylinders/cylinder group */
    163 int	cpgflg;			/* cylinders/cylinder group flag was given */
    164 int	minfree = MINFREE;	/* free space threshold */
    165 int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
    166 int	density;		/* number of bytes per inode */
    167 int	maxcontig = 8;		/* max contiguous blocks to allocate */
    168 int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
    169 int	maxbpg;			/* maximum blocks per file in a cyl group */
    170 int	nrpos = NRPOS;		/* # of distinguished rotational positions */
    171 int	bbsize = BBSIZE;	/* boot block size */
    172 int	sbsize = SBSIZE;	/* superblock size */
    173 int	mntflags = MNT_ASYNC;	/* flags to be passed to mount */
    174 u_long	memleft;		/* virtual memory available */
    175 caddr_t	membase;		/* start address of memory based filesystem */
    176 #ifdef COMPAT
    177 char	*disktype;
    178 int	unlabeled;
    179 #endif
    180 
    181 char	device[MAXPATHLEN];
    182 char	*progname;
    183 
    184 int
    185 main(argc, argv)
    186 	int argc;
    187 	char *argv[];
    188 {
    189 	extern char *optarg;
    190 	extern int optind;
    191 	register int ch;
    192 	register struct partition *pp;
    193 	register struct disklabel *lp;
    194 	struct disklabel mfsfakelabel;
    195 	struct disklabel *getdisklabel();
    196 	struct partition oldpartition;
    197 	struct stat st;
    198 	struct statfs *mp;
    199 	int fsi, fso, len, n;
    200 	char *cp, *s1, *s2, *special, *opstring, buf[BUFSIZ];
    201 
    202 	if (progname = strrchr(*argv, '/'))
    203 		++progname;
    204 	else
    205 		progname = *argv;
    206 
    207 	if (strstr(progname, "mfs")) {
    208 		mfs = 1;
    209 		Nflag++;
    210 	}
    211 
    212 	opstring = mfs ?
    213 	    "NT:a:b:c:d:e:f:i:m:o:s:" :
    214 	    "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
    215 	while ((ch = getopt(argc, argv, opstring)) != EOF)
    216 		switch (ch) {
    217 		case 'N':
    218 			Nflag = 1;
    219 			break;
    220 		case 'O':
    221 			Oflag = 1;
    222 			break;
    223 		case 'S':
    224 			if ((sectorsize = atoi(optarg)) <= 0)
    225 				fatal("%s: bad sector size", optarg);
    226 			break;
    227 #ifdef COMPAT
    228 		case 'T':
    229 			disktype = optarg;
    230 			break;
    231 #endif
    232 		case 'a':
    233 			if ((maxcontig = atoi(optarg)) <= 0)
    234 				fatal("%s: bad maximum contiguous blocks\n",
    235 				    optarg);
    236 			break;
    237 		case 'b':
    238 			if ((bsize = atoi(optarg)) < MINBSIZE)
    239 				fatal("%s: bad block size", optarg);
    240 			break;
    241 		case 'c':
    242 			if ((cpg = atoi(optarg)) <= 0)
    243 				fatal("%s: bad cylinders/group", optarg);
    244 			cpgflg++;
    245 			break;
    246 		case 'd':
    247 			if ((rotdelay = atoi(optarg)) < 0)
    248 				fatal("%s: bad rotational delay\n", optarg);
    249 			break;
    250 		case 'e':
    251 			if ((maxbpg = atoi(optarg)) <= 0)
    252 		fatal("%s: bad blocks per file in a cylinder group\n",
    253 				    optarg);
    254 			break;
    255 		case 'f':
    256 			if ((fsize = atoi(optarg)) <= 0)
    257 				fatal("%s: bad fragment size", optarg);
    258 			break;
    259 		case 'i':
    260 			if ((density = atoi(optarg)) <= 0)
    261 				fatal("%s: bad bytes per inode\n", optarg);
    262 			break;
    263 		case 'k':
    264 			if ((trackskew = atoi(optarg)) < 0)
    265 				fatal("%s: bad track skew", optarg);
    266 			break;
    267 		case 'l':
    268 			if ((interleave = atoi(optarg)) <= 0)
    269 				fatal("%s: bad interleave", optarg);
    270 			break;
    271 		case 'm':
    272 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
    273 				fatal("%s: bad free space %%\n", optarg);
    274 			break;
    275 		case 'n':
    276 			if ((nrpos = atoi(optarg)) <= 0)
    277 				fatal("%s: bad rotational layout count\n",
    278 				    optarg);
    279 			break;
    280 		case 'o':
    281 			if (mfs)
    282 				getmntopts(optarg, mopts, &mntflags);
    283 			else {
    284 				if (strcmp(optarg, "space") == 0)
    285 					opt = FS_OPTSPACE;
    286 				else if (strcmp(optarg, "time") == 0)
    287 					opt = FS_OPTTIME;
    288 				else
    289 	fatal("%s: unknown optimization preference: use `space' or `time'.");
    290 			}
    291 			break;
    292 		case 'p':
    293 			if ((trackspares = atoi(optarg)) < 0)
    294 				fatal("%s: bad spare sectors per track",
    295 				    optarg);
    296 			break;
    297 		case 'r':
    298 			if ((rpm = atoi(optarg)) <= 0)
    299 				fatal("%s: bad revolutions/minute\n", optarg);
    300 			break;
    301 		case 's':
    302 			if ((fssize = atoi(optarg)) <= 0)
    303 				fatal("%s: bad file system size", optarg);
    304 			break;
    305 		case 't':
    306 			if ((ntracks = atoi(optarg)) <= 0)
    307 				fatal("%s: bad total tracks", optarg);
    308 			break;
    309 		case 'u':
    310 			if ((nsectors = atoi(optarg)) <= 0)
    311 				fatal("%s: bad sectors/track", optarg);
    312 			break;
    313 		case 'x':
    314 			if ((cylspares = atoi(optarg)) < 0)
    315 				fatal("%s: bad spare sectors per cylinder",
    316 				    optarg);
    317 			break;
    318 		case '?':
    319 		default:
    320 			usage();
    321 		}
    322 	argc -= optind;
    323 	argv += optind;
    324 
    325 	if (argc != 2 && (mfs || argc != 1))
    326 		usage();
    327 
    328 	special = argv[0];
    329 	if (mfs && !strcmp(special, "swap")) {
    330 		/*
    331 		 * it's an MFS, mounted on "swap."  fake up a label.
    332 		 * XXX XXX XXX
    333 		 */
    334 		fso = -1;	/* XXX; normally done below. */
    335 
    336 		memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
    337 		mfsfakelabel.d_secsize = 512;
    338 		mfsfakelabel.d_nsectors = 64;
    339 		mfsfakelabel.d_ntracks = 16;
    340 		mfsfakelabel.d_ncylinders = 16;
    341 		mfsfakelabel.d_secpercyl = 1024;
    342 		mfsfakelabel.d_secperunit = 16384;
    343 		mfsfakelabel.d_rpm = 3600;
    344 		mfsfakelabel.d_interleave = 1;
    345 		mfsfakelabel.d_npartitions = 1;
    346 		mfsfakelabel.d_partitions[0].p_size = 16384;
    347 		mfsfakelabel.d_partitions[0].p_fsize = 1024;
    348 		mfsfakelabel.d_partitions[0].p_frag = 8;
    349 		mfsfakelabel.d_partitions[0].p_cpg = 16;
    350 
    351 		lp = &mfsfakelabel;
    352 		pp = &mfsfakelabel.d_partitions[0];
    353 
    354 		goto havelabel;
    355 	}
    356 	cp = strrchr(special, '/');
    357 	if (cp == 0) {
    358 		/*
    359 		 * No path prefix; try /dev/r%s then /dev/%s.
    360 		 */
    361 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
    362 		if (stat(device, &st) == -1)
    363 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
    364 		special = device;
    365 	}
    366 	if (Nflag) {
    367 		fso = -1;
    368 	} else {
    369 		fso = open(special, O_WRONLY);
    370 		if (fso < 0)
    371 			fatal("%s: %s", special, strerror(errno));
    372 
    373 		/* Bail if target special is mounted */
    374 		n = getmntinfo(&mp, MNT_NOWAIT);
    375 		if (n == 0)
    376 			fatal("%s: getmntinfo: %s", special, strerror(errno));
    377 
    378 		len = sizeof(_PATH_DEV) - 1;
    379 		s1 = special;
    380 		if (strncmp(_PATH_DEV, s1, len) == 0)
    381 			s1 += len;
    382 
    383 		while (--n >= 0) {
    384 			s2 = mp->f_mntfromname;
    385 			if (strncmp(_PATH_DEV, s2, len) == 0) {
    386 				s2 += len - 1;
    387 				*s2 = 'r';
    388 			}
    389 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
    390 				fatal("%s is mounted on %s",
    391 				    special, mp->f_mntonname);
    392 			++mp;
    393 		}
    394 	}
    395 	if (mfs && disktype != NULL) {
    396 		lp = (struct disklabel *)getdiskbyname(disktype);
    397 		if (lp == NULL)
    398 			fatal("%s: unknown disk type", disktype);
    399 		pp = &lp->d_partitions[1];
    400 	} else {
    401 		fsi = open(special, O_RDONLY);
    402 		if (fsi < 0)
    403 			fatal("%s: %s", special, strerror(errno));
    404 		if (fstat(fsi, &st) < 0)
    405 			fatal("%s: %s", special, strerror(errno));
    406 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
    407 			printf("%s: %s: not a character-special device\n",
    408 			    progname, special);
    409 		cp = strchr(argv[0], '\0') - 1;
    410 		if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
    411 			fatal("%s: can't figure out file system partition",
    412 			    argv[0]);
    413 #ifdef COMPAT
    414 		if (!mfs && disktype == NULL)
    415 			disktype = argv[1];
    416 #endif
    417 		lp = getdisklabel(special, fsi);
    418 		if (isdigit(*cp))
    419 			pp = &lp->d_partitions[0];
    420 		else
    421 			pp = &lp->d_partitions[*cp - 'a'];
    422 		if (pp->p_size == 0)
    423 			fatal("%s: `%c' partition is unavailable",
    424 			    argv[0], *cp);
    425 		if (pp->p_fstype == FS_BOOT)
    426 			fatal("%s: `%c' partition overlaps boot program",
    427 			      argv[0], *cp);
    428 	}
    429 havelabel:
    430 	if (fssize == 0)
    431 		fssize = pp->p_size;
    432 	if (fssize > pp->p_size && !mfs)
    433 	       fatal("%s: maximum file system size on the `%c' partition is %d",
    434 			argv[0], *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 			fatal("%s: no default #tracks", argv[0]);
    444 	}
    445 	if (nsectors == 0) {
    446 		nsectors = lp->d_nsectors;
    447 		if (nsectors <= 0)
    448 			fatal("%s: no default #sectors/track", argv[0]);
    449 	}
    450 	if (sectorsize == 0) {
    451 		sectorsize = lp->d_secsize;
    452 		if (sectorsize <= 0)
    453 			fatal("%s: no default sector size", argv[0]);
    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 - 1);
    483 	if (density == 0)
    484 		density = NFPI * fsize;
    485 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
    486 		fprintf(stderr, "Warning: changing optimization to space ");
    487 		fprintf(stderr, "because minfree is less than %d%%\n", 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 		fprintf(stderr, "%s (%d) %s (%lu)\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 #ifdef tahoe
    516 	realsectorsize = sectorsize;
    517 	if (sectorsize != DEV_BSIZE) {		/* XXX */
    518 		int secperblk = DEV_BSIZE / sectorsize;
    519 
    520 		sectorsize = DEV_BSIZE;
    521 		nsectors /= secperblk;
    522 		nphyssectors /= secperblk;
    523 		secpercyl /= secperblk;
    524 		fssize /= secperblk;
    525 		pp->p_size /= secperblk;
    526 	}
    527 #endif
    528 	mkfs(pp, special, fsi, fso);
    529 #ifdef tahoe
    530 	if (realsectorsize != DEV_BSIZE)
    531 		pp->p_size *= DEV_BSIZE / realsectorsize;
    532 #endif
    533 	if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)))
    534 		rewritelabel(special, fso, lp);
    535 	if (!Nflag)
    536 		close(fso);
    537 	close(fsi);
    538 #ifdef MFS
    539 	if (mfs) {
    540 		struct mfs_args args;
    541 
    542 		sprintf(buf, "mfs:%d", getpid());
    543 		args.fspec = buf;
    544 		args.export.ex_root = -2;
    545 		if (mntflags & MNT_RDONLY)
    546 			args.export.ex_flags = MNT_EXRDONLY;
    547 		else
    548 			args.export.ex_flags = 0;
    549 		args.base = membase;
    550 		args.size = fssize * sectorsize;
    551 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
    552 			fatal("%s: %s", argv[1], strerror(errno));
    553 	}
    554 #endif
    555 	exit(0);
    556 }
    557 
    558 #ifdef COMPAT
    559 char lmsg[] = "%s: can't read disk label; disk type must be specified";
    560 #else
    561 char lmsg[] = "%s: can't read disk label";
    562 #endif
    563 
    564 struct disklabel *
    565 getdisklabel(s, fd)
    566 	char *s;
    567 	int fd;
    568 {
    569 	static struct disklabel lab;
    570 
    571 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    572 #ifdef COMPAT
    573 		if (disktype) {
    574 			struct disklabel *lp, *getdiskbyname();
    575 
    576 			unlabeled++;
    577 			lp = getdiskbyname(disktype);
    578 			if (lp == NULL)
    579 				fatal("%s: unknown disk type", disktype);
    580 			return (lp);
    581 		}
    582 #endif
    583 		warn("ioctl (GDINFO)");
    584 		fatal(lmsg, s);
    585 	}
    586 	return (&lab);
    587 }
    588 
    589 rewritelabel(s, fd, lp)
    590 	char *s;
    591 	int fd;
    592 	register struct disklabel *lp;
    593 {
    594 #ifdef COMPAT
    595 	if (unlabeled)
    596 		return;
    597 #endif
    598 	lp->d_checksum = 0;
    599 	lp->d_checksum = dkcksum(lp);
    600 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    601 		warn("ioctl (WDINFO)");
    602 		fatal("%s: can't rewrite disk label", s);
    603 	}
    604 #if vax
    605 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    606 		register i;
    607 		int cfd;
    608 		daddr_t alt;
    609 		char specname[64];
    610 		char blk[1024];
    611 		char *cp;
    612 
    613 		/*
    614 		 * Make name for 'c' partition.
    615 		 */
    616 		strcpy(specname, s);
    617 		cp = specname + strlen(specname) - 1;
    618 		if (!isdigit(*cp))
    619 			*cp = 'c';
    620 		cfd = open(specname, O_WRONLY);
    621 		if (cfd < 0)
    622 			fatal("%s: %s", specname, strerror(errno));
    623 		memset(blk, 0, sizeof(blk));
    624 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
    625 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    626 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    627 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
    628 			    L_SET) == -1)
    629 				fatal("lseek to badsector area: %s",
    630 				    strerror(errno));
    631 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    632 				warn("alternate label %d write", i/2);
    633 		}
    634 		close(cfd);
    635 	}
    636 #endif
    637 }
    638 
    639 /*VARARGS*/
    640 void
    641 #if __STDC__
    642 fatal(const char *fmt, ...)
    643 #else
    644 fatal(fmt, va_alist)
    645 	char *fmt;
    646 	va_dcl
    647 #endif
    648 {
    649 	va_list ap;
    650 
    651 #if __STDC__
    652 	va_start(ap, fmt);
    653 #else
    654 	va_start(ap);
    655 #endif
    656 	if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
    657 		openlog(progname, LOG_CONS, LOG_DAEMON);
    658 		vsyslog(LOG_ERR, fmt, ap);
    659 		closelog();
    660 	} else {
    661 		vwarnx(fmt, ap);
    662 	}
    663 	va_end(ap);
    664 	exit(1);
    665 	/*NOTREACHED*/
    666 }
    667 
    668 usage()
    669 {
    670 	if (mfs) {
    671 		fprintf(stderr,
    672 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
    673 			progname);
    674 	} else
    675 		fprintf(stderr,
    676 		    "usage: %s [ -fsoptions ] special-device%s\n",
    677 		    progname,
    678 #ifdef COMPAT
    679 		    " [device-type]");
    680 #else
    681 		    "");
    682 #endif
    683 	fprintf(stderr, "where fsoptions are:\n");
    684 	fprintf(stderr,
    685 	    "\t-N do not create file system, just print out parameters\n");
    686 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
    687 	fprintf(stderr, "\t-S sector size\n");
    688 #ifdef COMPAT
    689 	fprintf(stderr, "\t-T disktype\n");
    690 #endif
    691 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    692 	fprintf(stderr, "\t-b block size\n");
    693 	fprintf(stderr, "\t-c cylinders/group\n");
    694 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    695 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    696 	fprintf(stderr, "\t-f frag size\n");
    697 	fprintf(stderr, "\t-i number of bytes per inode\n");
    698 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
    699 	fprintf(stderr, "\t-l hardware sector interleave\n");
    700 	fprintf(stderr, "\t-m minimum free space %%\n");
    701 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
    702 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    703 	fprintf(stderr, "\t-p spare sectors per track\n");
    704 	fprintf(stderr, "\t-s file system size (sectors)\n");
    705 	fprintf(stderr, "\t-r revolutions/minute\n");
    706 	fprintf(stderr, "\t-t tracks/cylinder\n");
    707 	fprintf(stderr, "\t-u sectors/track\n");
    708 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
    709 	exit(1);
    710 }
    711