xref: /csrg-svn/sbin/newfs/newfs.c (revision 45836)
121159Sdist /*
239049Smckusick  * Copyright (c) 1983, 1989 The Regents of the University of California.
339049Smckusick  * All rights reserved.
439049Smckusick  *
542704Sbostic  * %sccs.include.redist.c%
621159Sdist  */
721159Sdist 
810762Ssam #ifndef lint
9*45836Skarels static char sccsid[] = "@(#)newfs.c	6.24 (Berkeley) 12/18/90";
1039049Smckusick #endif /* not lint */
1139049Smckusick 
1239049Smckusick #ifndef lint
1321159Sdist char copyright[] =
1439049Smckusick "@(#) Copyright (c) 1983, 1989 Regents of the University of California.\n\
1521159Sdist  All rights reserved.\n";
1639049Smckusick #endif /* not lint */
1710762Ssam 
1810762Ssam /*
1911069Ssam  * newfs: friendly front end to mkfs
2010762Ssam  */
2110762Ssam #include <sys/param.h>
2210762Ssam #include <sys/stat.h>
2338380Smckusick #include <ufs/fs.h>
2438380Smckusick #include <ufs/dir.h>
2530380Smckusick #include <sys/ioctl.h>
2630380Smckusick #include <sys/disklabel.h>
2730380Smckusick #include <sys/file.h>
2839049Smckusick #include <sys/mount.h>
2910762Ssam 
3044970Sbostic #include <errno.h>
3144970Sbostic #include <stdarg.h>
3210762Ssam #include <stdio.h>
3330380Smckusick #include <ctype.h>
3444970Sbostic #include <string.h>
3544970Sbostic #include <stdlib.h>
3637951Sbostic #include <paths.h>
3710762Ssam 
3844970Sbostic #define	COMPAT			/* allow non-labeled disks */
3931680Skarels 
4030380Smckusick /*
4130380Smckusick  * The following two constants set the default block and fragment sizes.
4230380Smckusick  * Both constants must be a power of 2 and meet the following constraints:
4330380Smckusick  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
4430380Smckusick  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
4530380Smckusick  *	DESBLKSIZE / DESFRAGSIZE <= 8
4630380Smckusick  */
4730380Smckusick #define	DFL_FRAGSIZE	1024
4830380Smckusick #define	DFL_BLKSIZE	8192
4911183Ssam 
5030380Smckusick /*
5134137Smckusick  * Cylinder groups may have up to many cylinders. The actual
5230380Smckusick  * number used depends upon how much information can be stored
5334137Smckusick  * on a single cylinder. The default is to use 16 cylinders
5430380Smckusick  * per group.
5530380Smckusick  */
5630380Smckusick #define	DESCPG		16	/* desired fs_cpg */
5730380Smckusick 
5830380Smckusick /*
5930380Smckusick  * MINFREE gives the minimum acceptable percentage of file system
6030380Smckusick  * blocks which may be free. If the freelist drops below this level
6130380Smckusick  * only the superuser may continue to allocate blocks. This may
6230380Smckusick  * be set to 0 if no reserve of free blocks is deemed necessary,
6330380Smckusick  * however throughput drops by fifty percent if the file system
6430380Smckusick  * is run at between 90% and 100% full; thus the default value of
6530380Smckusick  * fs_minfree is 10%. With 10% free space, fragmentation is not a
6630380Smckusick  * problem, so we choose to optimize for time.
6730380Smckusick  */
6830380Smckusick #define MINFREE		10
6930380Smckusick #define DEFAULTOPT	FS_OPTTIME
7030380Smckusick 
7130380Smckusick /*
7230380Smckusick  * ROTDELAY gives the minimum number of milliseconds to initiate
7330380Smckusick  * another disk transfer on the same cylinder. It is used in
7430380Smckusick  * determining the rotationally optimal layout for disk blocks
7530380Smckusick  * within a file; the default of fs_rotdelay is 4ms.
7630380Smckusick  */
7730380Smckusick #define ROTDELAY	4
7830380Smckusick 
7930380Smckusick /*
8030380Smckusick  * MAXCONTIG sets the default for the maximum number of blocks
8130380Smckusick  * that may be allocated sequentially. Since UNIX drivers are
8230380Smckusick  * not capable of scheduling multi-block transfers, this defaults
8330380Smckusick  * to 1 (ie no contiguous blocks are allocated).
8430380Smckusick  */
8530380Smckusick #define MAXCONTIG	1
8630380Smckusick 
8730380Smckusick /*
8832309Smckusick  * MAXBLKPG determines the maximum number of data blocks which are
8932309Smckusick  * placed in a single cylinder group. The default is one indirect
9032309Smckusick  * block worth of data blocks.
9132309Smckusick  */
9232309Smckusick #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
9332309Smckusick 
9432309Smckusick /*
9530380Smckusick  * Each file system has a number of inodes statically allocated.
9639049Smckusick  * We allocate one inode slot per NFPI fragments, expecting this
9730380Smckusick  * to be far more than we will ever need.
9830380Smckusick  */
9939049Smckusick #define	NFPI		4
10030380Smckusick 
10134137Smckusick /*
10234137Smckusick  * For each cylinder we keep track of the availability of blocks at different
10334137Smckusick  * rotational positions, so that we can lay out the data to be picked
10434137Smckusick  * up with minimum rotational latency.  NRPOS is the default number of
10534137Smckusick  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
10634137Smckusick  * of our summary information is 2ms for a typical 3600 rpm drive.
10734137Smckusick  */
10834137Smckusick #define	NRPOS		8	/* number distinct rotational positions */
10934137Smckusick 
11034137Smckusick 
11139325Smckusick int	mfs;			/* run as the memory based filesystem */
11230380Smckusick int	Nflag;			/* run without writing file system */
11310762Ssam int	fssize;			/* file system size */
11410762Ssam int	ntracks;		/* # tracks/cylinder */
11510762Ssam int	nsectors;		/* # sectors/track */
11630386Smckusick int	nphyssectors;		/* # sectors/track including spares */
11730380Smckusick int	secpercyl;		/* sectors per cylinder */
11830386Smckusick int	trackspares = -1;	/* spare sectors per track */
11930386Smckusick int	cylspares = -1;		/* spare sectors per cylinder */
12010762Ssam int	sectorsize;		/* bytes/sector */
12130691Skarels #ifdef tahoe
12230691Skarels int	realsectorsize;		/* bytes/sector in hardware */
12330691Skarels #endif
12411069Ssam int	rpm;			/* revolutions/minute of drive */
12530386Smckusick int	interleave;		/* hardware sector interleave */
12630386Smckusick int	trackskew = -1;		/* sector 0 skew, per track */
12730386Smckusick int	headswitch;		/* head switch time, usec */
12830386Smckusick int	trackseek;		/* track-to-track seek, usec */
12930862Skarels int	fsize = 0;		/* fragment size */
13030862Skarels int	bsize = 0;		/* block size */
13130380Smckusick int	cpg = DESCPG;		/* cylinders/cylinder group */
13232119Smckusick int	cpgflg;			/* cylinders/cylinder group flag was given */
13330380Smckusick int	minfree = MINFREE;	/* free space threshold */
13430380Smckusick int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
13539049Smckusick int	density;		/* number of bytes per inode */
13630380Smckusick int	maxcontig = MAXCONTIG;	/* max contiguous blocks to allocate */
13730380Smckusick int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
13832309Smckusick int	maxbpg;			/* maximum blocks per file in a cyl group */
13934137Smckusick int	nrpos = NRPOS;		/* # of distinguished rotational positions */
14030691Skarels int	bbsize = BBSIZE;	/* boot block size */
14130691Skarels int	sbsize = SBSIZE;	/* superblock size */
14239325Smckusick int	mntflags;		/* flags to be passed to mount */
14339049Smckusick u_long	memleft;		/* virtual memory available */
14439049Smckusick caddr_t	membase;		/* start address of memory based filesystem */
14531680Skarels #ifdef COMPAT
14643314Smckusick char	*disktype;
14744970Sbostic int	unlabeled;
14831680Skarels #endif
14910762Ssam 
15010762Ssam char	device[MAXPATHLEN];
15139049Smckusick char	*progname;
15210762Ssam 
15310762Ssam main(argc, argv)
15414064Smckusick 	int argc;
15510762Ssam 	char *argv[];
15610762Ssam {
15744970Sbostic 	extern char *optarg;
15844970Sbostic 	extern int optind;
15944970Sbostic 	register int ch;
16010762Ssam 	register struct partition *pp;
16130380Smckusick 	register struct disklabel *lp;
16230380Smckusick 	struct disklabel *getdisklabel();
16330380Smckusick 	struct partition oldpartition;
16439049Smckusick 	struct mfs_args args;
16510762Ssam 	struct stat st;
16630380Smckusick 	int fsi, fso;
16744970Sbostic 	char *cp, *special, *opstring, buf[BUFSIZ];
16810762Ssam 
16944970Sbostic 	if (progname = rindex(*argv, '/'))
17044970Sbostic 		++progname;
17144970Sbostic 	else
17239049Smckusick 		progname = *argv;
17344970Sbostic 
17439325Smckusick 	if (!strcmp(progname, "mfs")) {
17544970Sbostic 		mfs = 1;
17639049Smckusick 		Nflag++;
17739049Smckusick 	}
17810762Ssam 
17944970Sbostic 	opstring = "F:NS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
18044970Sbostic 	if (!mfs)
18144970Sbostic 		opstring += 2;		/* -F is mfs only */
18239325Smckusick 
18344970Sbostic 	while ((ch = getopt(argc, argv, opstring)) != EOF)
18444970Sbostic 		switch(ch) {
18544970Sbostic 		case 'F':
18644970Sbostic 			if ((mntflags = atoi(optarg)) == 0)
18744970Sbostic 				fatal("%s: bad mount flags", optarg);
18844970Sbostic 			break;
18944970Sbostic 		case 'N':
19044970Sbostic 			Nflag++;
19144970Sbostic 			break;
19244970Sbostic 		case 'S':
19344970Sbostic 			if ((sectorsize = atoi(optarg)) <= 0)
19444970Sbostic 				fatal("%s: bad sector size", optarg);
19544970Sbostic 			break;
19643314Smckusick #ifdef COMPAT
19744970Sbostic 		case 'T':
19844970Sbostic 			disktype = optarg;
19944970Sbostic 			break;
20043314Smckusick #endif
20144970Sbostic 		case 'a':
20244970Sbostic 			if ((maxcontig = atoi(optarg)) <= 0)
20344970Sbostic 				fatal("%s: bad max contiguous blocks\n",
20444970Sbostic 				    optarg);
20544970Sbostic 			break;
20644970Sbostic 		case 'b':
20744970Sbostic 			if ((bsize = atoi(optarg)) < MINBSIZE)
20844970Sbostic 				fatal("%s: bad block size", optarg);
20944970Sbostic 			break;
21044970Sbostic 		case 'c':
21144970Sbostic 			if ((cpg = atoi(optarg)) <= 0)
21244970Sbostic 				fatal("%s: bad cylinders/group", optarg);
21344970Sbostic 			cpgflg++;
21444970Sbostic 			break;
21544970Sbostic 		case 'd':
21644970Sbostic 			if ((rotdelay = atoi(optarg)) < 0)
21744970Sbostic 				fatal("%s: bad rotational delay\n", optarg);
21844970Sbostic 			break;
21944970Sbostic 		case 'e':
22044970Sbostic 			if ((maxbpg = atoi(optarg)) <= 0)
22144970Sbostic 				fatal("%s: bad blocks per file in a cyl group\n",
22244970Sbostic 				    optarg);
22344970Sbostic 			break;
22444970Sbostic 		case 'f':
22544970Sbostic 			if ((fsize = atoi(optarg)) <= 0)
22644970Sbostic 				fatal("%s: bad frag size", optarg);
22744970Sbostic 			break;
22844970Sbostic 		case 'i':
22944970Sbostic 			if ((density = atoi(optarg)) <= 0)
23044970Sbostic 				fatal("%s: bad bytes per inode\n", optarg);
23144970Sbostic 			break;
23244970Sbostic 		case 'k':
23344970Sbostic 			if ((trackskew = atoi(optarg)) < 0)
23444970Sbostic 				fatal("%s: bad track skew", optarg);
23544970Sbostic 			break;
23644970Sbostic 		case 'l':
23744970Sbostic 			if ((interleave = atoi(optarg)) <= 0)
23844970Sbostic 				fatal("%s: bad interleave", optarg);
23944970Sbostic 			break;
24044970Sbostic 		case 'm':
24144970Sbostic 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
24244970Sbostic 				fatal("%s: bad free space %%\n", optarg);
24344970Sbostic 			break;
24444970Sbostic 		case 'n':
24544970Sbostic 			if ((nrpos = atoi(optarg)) <= 0)
24644970Sbostic 				fatal("%s: bad rotational layout count\n",
24744970Sbostic 				    optarg);
24844970Sbostic 			break;
24944970Sbostic 		case 'o':
25044970Sbostic 			if (strcmp(optarg, "space") == 0)
25144970Sbostic 				opt = FS_OPTSPACE;
25244970Sbostic 			else if (strcmp(optarg, "time") == 0)
25344970Sbostic 				opt = FS_OPTTIME;
25444970Sbostic 			else
25544970Sbostic 				fatal("%s: bad optimization preference %s",
25644970Sbostic 				    optarg, "(options are `space' or `time')");
25744970Sbostic 			break;
25844970Sbostic 		case 'p':
25944970Sbostic 			if ((trackspares = atoi(optarg)) < 0)
26044970Sbostic 				fatal("%s: bad spare sectors per track",
26144970Sbostic 				    optarg);
26244970Sbostic 			break;
26344970Sbostic 		case 'r':
26444970Sbostic 			if ((rpm = atoi(optarg)) <= 0)
26544970Sbostic 				fatal("%s: bad revs/minute\n", optarg);
26644970Sbostic 			break;
26744970Sbostic 		case 's':
26844970Sbostic 			if ((fssize = atoi(optarg)) <= 0)
26944970Sbostic 				fatal("%s: bad file system size", optarg);
27044970Sbostic 			break;
27144970Sbostic 		case 't':
27244970Sbostic 			if ((ntracks = atoi(optarg)) <= 0)
27344970Sbostic 				fatal("%s: bad total tracks", optarg);
27444970Sbostic 			break;
27544970Sbostic 		case 'u':
27644970Sbostic 			if ((nsectors = atoi(optarg)) <= 0)
27744970Sbostic 				fatal("%s: bad sectors/track", optarg);
27844970Sbostic 			break;
27944970Sbostic 		case 'x':
28044970Sbostic 			if ((cylspares = atoi(optarg)) < 0)
28144970Sbostic 				fatal("%s: bad spare sectors per cylinder",
28244970Sbostic 				    optarg);
28344970Sbostic 			break;
28444970Sbostic 		case '?':
28544970Sbostic 		default:
28644970Sbostic 			usage();
28744970Sbostic 		}
28844970Sbostic 	argc -= optind;
28944970Sbostic 	argv += optind;
29043314Smckusick 
29144970Sbostic 	if (argc != 2 && (mfs || argc != 1))
29244970Sbostic 		usage();
29310762Ssam 
29410762Ssam 	special = argv[0];
29514064Smckusick 	cp = rindex(special, '/');
296*45836Skarels 	if (cp == 0) {
297*45836Skarels 		/*
298*45836Skarels 		 * No path prefix; try /dev/r%s then /dev/%s.
299*45836Skarels 		 */
300*45836Skarels 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
301*45836Skarels 		if (stat(device, &st) == -1)
302*45836Skarels 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
303*45836Skarels 		special = device;
304*45836Skarels 	}
30530380Smckusick 	if (!Nflag) {
30630380Smckusick 		fso = open(special, O_WRONLY);
30744970Sbostic 		if (fso < 0)
30844970Sbostic 			fatal("%s: %s", special, strerror(errno));
30930380Smckusick 	} else
31030380Smckusick 		fso = -1;
31130380Smckusick 	fsi = open(special, O_RDONLY);
31244970Sbostic 	if (fsi < 0)
31344970Sbostic 		fatal("%s: %s", special, strerror(errno));
31444970Sbostic 	if (fstat(fsi, &st) < 0)
31544970Sbostic 		fatal("%s: %s", special, strerror(errno));
31614064Smckusick 	if ((st.st_mode & S_IFMT) != S_IFCHR)
317*45836Skarels 		printf("%s: not a character device\n", special);
31810762Ssam 	cp = index(argv[0], '\0') - 1;
31930380Smckusick 	if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
32010762Ssam 		fatal("%s: can't figure out file system partition", argv[0]);
32131680Skarels #ifdef COMPAT
32243314Smckusick 	if (!mfs && disktype == NULL)
32343314Smckusick 		disktype = argv[1];
32443314Smckusick #endif
32530380Smckusick 	lp = getdisklabel(special, fsi);
32630380Smckusick 	if (isdigit(*cp))
32730380Smckusick 		pp = &lp->d_partitions[0];
32830380Smckusick 	else
32930380Smckusick 		pp = &lp->d_partitions[*cp - 'a'];
33030380Smckusick 	if (pp->p_size == 0)
33130380Smckusick 		fatal("%s: `%c' partition is unavailable", argv[0], *cp);
33230380Smckusick 	if (fssize == 0)
33310762Ssam 		fssize = pp->p_size;
33439325Smckusick 	if (fssize > pp->p_size && !mfs)
33530380Smckusick 	       fatal("%s: maximum file system size on the `%c' partition is %d",
33630380Smckusick 			argv[0], *cp, pp->p_size);
33730380Smckusick 	if (rpm == 0) {
33830380Smckusick 		rpm = lp->d_rpm;
33930380Smckusick 		if (rpm <= 0)
34030743Skarels 			rpm = 3600;
34110762Ssam 	}
34230380Smckusick 	if (ntracks == 0) {
34330380Smckusick 		ntracks = lp->d_ntracks;
34430380Smckusick 		if (ntracks <= 0)
34530743Skarels 			fatal("%s: no default #tracks", argv[0]);
34630380Smckusick 	}
34710762Ssam 	if (nsectors == 0) {
34830380Smckusick 		nsectors = lp->d_nsectors;
34930380Smckusick 		if (nsectors <= 0)
35030743Skarels 			fatal("%s: no default #sectors/track", argv[0]);
35110762Ssam 	}
35210762Ssam 	if (sectorsize == 0) {
35330380Smckusick 		sectorsize = lp->d_secsize;
35430380Smckusick 		if (sectorsize <= 0)
35530743Skarels 			fatal("%s: no default sector size", argv[0]);
35610762Ssam 	}
35730386Smckusick 	if (trackskew == -1) {
35830386Smckusick 		trackskew = lp->d_trackskew;
35930386Smckusick 		if (trackskew < 0)
36030743Skarels 			trackskew = 0;
36130386Smckusick 	}
36230386Smckusick 	if (interleave == 0) {
36330386Smckusick 		interleave = lp->d_interleave;
36430386Smckusick 		if (interleave <= 0)
36530743Skarels 			interleave = 1;
36630386Smckusick 	}
36710762Ssam 	if (fsize == 0) {
36810762Ssam 		fsize = pp->p_fsize;
36930380Smckusick 		if (fsize <= 0)
37030380Smckusick 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
37110762Ssam 	}
37230380Smckusick 	if (bsize == 0) {
37330380Smckusick 		bsize = pp->p_frag * pp->p_fsize;
37430380Smckusick 		if (bsize <= 0)
37530380Smckusick 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
37611069Ssam 	}
37739049Smckusick 	if (density == 0)
37839049Smckusick 		density = NFPI * fsize;
37924702Smckusick 	if (minfree < 10 && opt != FS_OPTSPACE) {
38030380Smckusick 		fprintf(stderr, "Warning: changing optimization to space ");
38130380Smckusick 		fprintf(stderr, "because minfree is less than 10%%\n");
38224702Smckusick 		opt = FS_OPTSPACE;
38324702Smckusick 	}
38430386Smckusick 	if (trackspares == -1) {
38530386Smckusick 		trackspares = lp->d_sparespertrack;
38630386Smckusick 		if (trackspares < 0)
38730743Skarels 			trackspares = 0;
38830386Smckusick 	}
38930386Smckusick 	nphyssectors = nsectors + trackspares;
39030386Smckusick 	if (cylspares == -1) {
39130386Smckusick 		cylspares = lp->d_sparespercyl;
39230386Smckusick 		if (cylspares < 0)
39330743Skarels 			cylspares = 0;
39430386Smckusick 	}
39530386Smckusick 	secpercyl = nsectors * ntracks - cylspares;
39630380Smckusick 	if (secpercyl != lp->d_secpercyl)
39744970Sbostic 		fprintf(stderr, "%s (%d) %s (%lu)\n",
39830380Smckusick 			"Warning: calculated sectors per cylinder", secpercyl,
39930380Smckusick 			"disagrees with disk label", lp->d_secpercyl);
40032321Smckusick 	if (maxbpg == 0)
40132321Smckusick 		maxbpg = MAXBLKPG(bsize);
40230386Smckusick 	headswitch = lp->d_headswitch;
40330386Smckusick 	trackseek = lp->d_trkseek;
404*45836Skarels #ifdef notdef /* label may be 0 if faked up by kernel */
40530691Skarels 	bbsize = lp->d_bbsize;
40630691Skarels 	sbsize = lp->d_sbsize;
407*45836Skarels #endif
40830380Smckusick 	oldpartition = *pp;
40930691Skarels #ifdef tahoe
41030691Skarels 	realsectorsize = sectorsize;
41130862Skarels 	if (sectorsize != DEV_BSIZE) {		/* XXX */
41230691Skarels 		int secperblk = DEV_BSIZE / sectorsize;
41330691Skarels 
41430691Skarels 		sectorsize = DEV_BSIZE;
41530691Skarels 		nsectors /= secperblk;
41630691Skarels 		nphyssectors /= secperblk;
41730691Skarels 		secpercyl /= secperblk;
41830691Skarels 		fssize /= secperblk;
41930691Skarels 		pp->p_size /= secperblk;
42030691Skarels 	}
42130691Skarels #endif
42230380Smckusick 	mkfs(pp, special, fsi, fso);
42330691Skarels #ifdef tahoe
42430691Skarels 	if (realsectorsize != DEV_BSIZE)
42530691Skarels 		pp->p_size *= DEV_BSIZE / realsectorsize;
42630691Skarels #endif
42730380Smckusick 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
42830380Smckusick 		rewritelabel(special, fso, lp);
42939049Smckusick 	if (!Nflag)
43039049Smckusick 		close(fso);
43139049Smckusick 	close(fsi);
43239325Smckusick 	if (mfs) {
43339325Smckusick 		sprintf(buf, "mfs:%d", getpid());
43439049Smckusick 		args.name = buf;
43539049Smckusick 		args.base = membase;
43639049Smckusick 		args.size = fssize * sectorsize;
43744970Sbostic 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
43844970Sbostic 			fatal("%s: %s", argv[1], strerror(errno));
43939049Smckusick 	}
44030380Smckusick 	exit(0);
44130380Smckusick }
44230380Smckusick 
44331680Skarels #ifdef COMPAT
44443314Smckusick char lmsg[] = "%s: can't read disk label; disk type must be specified";
44543314Smckusick #else
44643314Smckusick char lmsg[] = "%s: can't read disk label";
44743314Smckusick #endif
44831680Skarels 
44931680Skarels struct disklabel *
45030380Smckusick getdisklabel(s, fd)
45130380Smckusick 	char *s;
45231680Skarels 	int fd;
45330380Smckusick {
45430380Smckusick 	static struct disklabel lab;
45530380Smckusick 
45630380Smckusick 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
45743314Smckusick #ifdef COMPAT
45843314Smckusick 		if (disktype) {
459*45836Skarels 			struct disklabel *lp, *getdiskbyname();
46043314Smckusick 
46144970Sbostic 			unlabeled++;
462*45836Skarels 			lp = getdiskbyname(disktype);
463*45836Skarels 			if (lp == NULL)
464*45836Skarels 				fatal("%s: unknown disk type", disktype);
465*45836Skarels 			return (lp);
46643314Smckusick 		}
46743314Smckusick #endif
46844970Sbostic 		(void)fprintf(stderr,
46944970Sbostic 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
47043314Smckusick 		fatal(lmsg, s);
47110762Ssam 	}
47230380Smckusick 	return (&lab);
47330380Smckusick }
47410762Ssam 
47530380Smckusick rewritelabel(s, fd, lp)
47630380Smckusick 	char *s;
47730380Smckusick 	int fd;
47830380Smckusick 	register struct disklabel *lp;
47930380Smckusick {
48031680Skarels #ifdef COMPAT
48144970Sbostic 	if (unlabeled)
48231680Skarels 		return;
48331680Skarels #endif
48430380Smckusick 	lp->d_checksum = 0;
48530380Smckusick 	lp->d_checksum = dkcksum(lp);
48630380Smckusick 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
48744970Sbostic 		(void)fprintf(stderr,
48844970Sbostic 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
48930380Smckusick 		fatal("%s: can't rewrite disk label", s);
49010762Ssam 	}
49130446Skarels #if vax
49230446Skarels 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
49330446Skarels 		register i;
49430691Skarels 		int cfd;
49530446Skarels 		daddr_t alt;
49630691Skarels 		char specname[64];
49730691Skarels 		char blk[1024];
49831045Ssam 		char *cp;
49930446Skarels 
50030691Skarels 		/*
50130691Skarels 		 * Make name for 'c' partition.
50230691Skarels 		 */
50330691Skarels 		strcpy(specname, s);
50430691Skarels 		cp = specname + strlen(specname) - 1;
50530691Skarels 		if (!isdigit(*cp))
50630691Skarels 			*cp = 'c';
50730691Skarels 		cfd = open(specname, O_WRONLY);
50844970Sbostic 		if (cfd < 0)
50944970Sbostic 			fatal("%s: %s", specname, strerror(errno));
51030691Skarels 		bzero(blk, sizeof(blk));
51130691Skarels 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
51230446Skarels 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
51330446Skarels 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
514*45836Skarels 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
515*45836Skarels 			    L_SET) == -1)
51644970Sbostic 				fatal("lseek to badsector area: %s",
51744970Sbostic 				    strerror(errno));
51844970Sbostic 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
51944970Sbostic 				fprintf(stderr,
52044970Sbostic 				    "%s: alternate label %d write: %s\n",
52144970Sbostic 				    progname, i/2, strerror(errno));
52230380Smckusick 		}
52339049Smckusick 		close(cfd);
52430380Smckusick 	}
52530446Skarels #endif
52610762Ssam }
52710762Ssam 
52810762Ssam /*VARARGS*/
52944970Sbostic fatal(fmt)
53010762Ssam 	char *fmt;
53110762Ssam {
53244970Sbostic 	va_list ap;
53310762Ssam 
53439049Smckusick 	fprintf(stderr, "%s: ", progname);
53544970Sbostic 	va_start(ap, fmt);
53644970Sbostic 	(void)vfprintf(stderr, fmt, ap);
53744970Sbostic 	va_end(ap);
53810762Ssam 	putc('\n', stderr);
53944970Sbostic 	exit(1);
54010762Ssam }
54144970Sbostic 
54244970Sbostic usage()
54344970Sbostic {
54444970Sbostic 	if (mfs) {
54544970Sbostic 		fprintf(stderr,
54644970Sbostic 		    "usage: mfs [ -fsoptions ] special-device mount-point\n");
54744970Sbostic 	} else
54844970Sbostic 		fprintf(stderr,
54944970Sbostic 		    "usage: newfs [ -fsoptions ] special-device%s\n",
55044970Sbostic #ifdef COMPAT
55144970Sbostic 		    " [device-type]");
55244970Sbostic #else
55344970Sbostic 		    "");
55444970Sbostic #endif
55544970Sbostic 	fprintf(stderr, "where fsoptions are:\n");
55644970Sbostic 	fprintf(stderr,
55744970Sbostic 	    "\t-N do not create file system, just print out parameters\n");
55844970Sbostic 	fprintf(stderr, "\t-S sector size\n");
55944970Sbostic #ifdef COMPAT
56044970Sbostic 	fprintf(stderr, "\t-T disktype\n");
56144970Sbostic #endif
56244970Sbostic 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
56344970Sbostic 	fprintf(stderr, "\t-b block size\n");
56444970Sbostic 	fprintf(stderr, "\t-c cylinders/group\n");
56544970Sbostic 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
56644970Sbostic 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
56744970Sbostic 	fprintf(stderr, "\t-f frag size\n");
56844970Sbostic 	fprintf(stderr, "\t-i number of bytes per inode\n");
56944970Sbostic 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
57044970Sbostic 	fprintf(stderr, "\t-l hardware sector interleave\n");
57144970Sbostic 	fprintf(stderr, "\t-m minimum free space %%\n");
57244970Sbostic 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
57344970Sbostic 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
57444970Sbostic 	fprintf(stderr, "\t-p spare sectors per track\n");
57544970Sbostic 	fprintf(stderr, "\t-s file system size (sectors)\n");
57644970Sbostic 	fprintf(stderr, "\t-r revolutions/minute\n");
57744970Sbostic 	fprintf(stderr, "\t-t tracks/cylinder\n");
57844970Sbostic 	fprintf(stderr, "\t-u sectors/track\n");
57944970Sbostic 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
58044970Sbostic 	exit(1);
58144970Sbostic }
582