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