xref: /csrg-svn/sbin/newfs/newfs.c (revision 55561)
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*55561Smckusick static char sccsid[] = "@(#)newfs.c	6.32 (Berkeley) 07/23/92";
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>
2330380Smckusick #include <sys/ioctl.h>
2430380Smckusick #include <sys/disklabel.h>
2530380Smckusick #include <sys/file.h>
2639049Smckusick #include <sys/mount.h>
2751533Sbostic #include <ufs/ufs/dir.h>
2851533Sbostic #include <ufs/ffs/fs.h>
2910762Ssam 
3047870Skarels #include <errno.h>
3150787Storek #if __STDC__
3247870Skarels #include <stdarg.h>
3350787Storek #else
3450787Storek #include <varargs.h>
3550787Storek #endif
3653915Smckusick #include <unistd.h>
3710762Ssam #include <stdio.h>
3830380Smckusick #include <ctype.h>
3947870Skarels #include <string.h>
4047870Skarels #include <stdlib.h>
4137951Sbostic #include <paths.h>
4210762Ssam 
4350787Storek #if __STDC__
4450787Storek void	fatal(const char *fmt, ...);
4550787Storek #else
4650787Storek void	fatal();
4750787Storek #endif
4850787Storek 
4947870Skarels #define	COMPAT			/* allow non-labeled disks */
5031680Skarels 
5130380Smckusick /*
5230380Smckusick  * The following two constants set the default block and fragment sizes.
5330380Smckusick  * Both constants must be a power of 2 and meet the following constraints:
5430380Smckusick  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
5530380Smckusick  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
5630380Smckusick  *	DESBLKSIZE / DESFRAGSIZE <= 8
5730380Smckusick  */
5830380Smckusick #define	DFL_FRAGSIZE	1024
5930380Smckusick #define	DFL_BLKSIZE	8192
6011183Ssam 
6130380Smckusick /*
6234137Smckusick  * Cylinder groups may have up to many cylinders. The actual
6330380Smckusick  * number used depends upon how much information can be stored
6434137Smckusick  * on a single cylinder. The default is to use 16 cylinders
6530380Smckusick  * per group.
6630380Smckusick  */
6730380Smckusick #define	DESCPG		16	/* desired fs_cpg */
6830380Smckusick 
6930380Smckusick /*
7030380Smckusick  * MINFREE gives the minimum acceptable percentage of file system
7130380Smckusick  * blocks which may be free. If the freelist drops below this level
7230380Smckusick  * only the superuser may continue to allocate blocks. This may
7330380Smckusick  * be set to 0 if no reserve of free blocks is deemed necessary,
7430380Smckusick  * however throughput drops by fifty percent if the file system
7530380Smckusick  * is run at between 90% and 100% full; thus the default value of
7630380Smckusick  * fs_minfree is 10%. With 10% free space, fragmentation is not a
7730380Smckusick  * problem, so we choose to optimize for time.
7830380Smckusick  */
7930380Smckusick #define MINFREE		10
8030380Smckusick #define DEFAULTOPT	FS_OPTTIME
8130380Smckusick 
8230380Smckusick /*
8330380Smckusick  * ROTDELAY gives the minimum number of milliseconds to initiate
8430380Smckusick  * another disk transfer on the same cylinder. It is used in
8530380Smckusick  * determining the rotationally optimal layout for disk blocks
8630380Smckusick  * within a file; the default of fs_rotdelay is 4ms.
8730380Smckusick  */
8830380Smckusick #define ROTDELAY	4
8930380Smckusick 
9030380Smckusick /*
9130380Smckusick  * MAXCONTIG sets the default for the maximum number of blocks
9230380Smckusick  * that may be allocated sequentially. Since UNIX drivers are
9330380Smckusick  * not capable of scheduling multi-block transfers, this defaults
9430380Smckusick  * to 1 (ie no contiguous blocks are allocated).
9530380Smckusick  */
9630380Smckusick #define MAXCONTIG	1
9730380Smckusick 
9830380Smckusick /*
9932309Smckusick  * MAXBLKPG determines the maximum number of data blocks which are
10032309Smckusick  * placed in a single cylinder group. The default is one indirect
10132309Smckusick  * block worth of data blocks.
10232309Smckusick  */
10332309Smckusick #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
10432309Smckusick 
10532309Smckusick /*
10630380Smckusick  * Each file system has a number of inodes statically allocated.
10739049Smckusick  * We allocate one inode slot per NFPI fragments, expecting this
10830380Smckusick  * to be far more than we will ever need.
10930380Smckusick  */
11039049Smckusick #define	NFPI		4
11130380Smckusick 
11234137Smckusick /*
11334137Smckusick  * For each cylinder we keep track of the availability of blocks at different
11434137Smckusick  * rotational positions, so that we can lay out the data to be picked
11534137Smckusick  * up with minimum rotational latency.  NRPOS is the default number of
11634137Smckusick  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
11734137Smckusick  * of our summary information is 2ms for a typical 3600 rpm drive.
11834137Smckusick  */
11934137Smckusick #define	NRPOS		8	/* number distinct rotational positions */
12034137Smckusick 
12134137Smckusick 
12239325Smckusick int	mfs;			/* run as the memory based filesystem */
12330380Smckusick int	Nflag;			/* run without writing file system */
12410762Ssam int	fssize;			/* file system size */
12510762Ssam int	ntracks;		/* # tracks/cylinder */
12610762Ssam int	nsectors;		/* # sectors/track */
12730386Smckusick int	nphyssectors;		/* # sectors/track including spares */
12830380Smckusick int	secpercyl;		/* sectors per cylinder */
12930386Smckusick int	trackspares = -1;	/* spare sectors per track */
13030386Smckusick int	cylspares = -1;		/* spare sectors per cylinder */
13110762Ssam int	sectorsize;		/* bytes/sector */
13230691Skarels #ifdef tahoe
13330691Skarels int	realsectorsize;		/* bytes/sector in hardware */
13430691Skarels #endif
13511069Ssam int	rpm;			/* revolutions/minute of drive */
13630386Smckusick int	interleave;		/* hardware sector interleave */
13730386Smckusick int	trackskew = -1;		/* sector 0 skew, per track */
13830386Smckusick int	headswitch;		/* head switch time, usec */
13930386Smckusick int	trackseek;		/* track-to-track seek, usec */
14030862Skarels int	fsize = 0;		/* fragment size */
14130862Skarels int	bsize = 0;		/* block size */
14230380Smckusick int	cpg = DESCPG;		/* cylinders/cylinder group */
14332119Smckusick int	cpgflg;			/* cylinders/cylinder group flag was given */
14430380Smckusick int	minfree = MINFREE;	/* free space threshold */
14530380Smckusick int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
14639049Smckusick int	density;		/* number of bytes per inode */
14730380Smckusick int	maxcontig = MAXCONTIG;	/* max contiguous blocks to allocate */
14830380Smckusick int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
14932309Smckusick int	maxbpg;			/* maximum blocks per file in a cyl group */
15034137Smckusick int	nrpos = NRPOS;		/* # of distinguished rotational positions */
15130691Skarels int	bbsize = BBSIZE;	/* boot block size */
15230691Skarels int	sbsize = SBSIZE;	/* superblock size */
15339325Smckusick int	mntflags;		/* flags to be passed to mount */
15439049Smckusick u_long	memleft;		/* virtual memory available */
15539049Smckusick caddr_t	membase;		/* start address of memory based filesystem */
15631680Skarels #ifdef COMPAT
15743314Smckusick char	*disktype;
15847870Skarels int	unlabeled;
15931680Skarels #endif
16010762Ssam 
16110762Ssam char	device[MAXPATHLEN];
16239049Smckusick char	*progname;
16310762Ssam 
16410762Ssam main(argc, argv)
16514064Smckusick 	int argc;
16610762Ssam 	char *argv[];
16710762Ssam {
16847870Skarels 	extern char *optarg;
16947870Skarels 	extern int optind;
17047870Skarels 	register int ch;
17110762Ssam 	register struct partition *pp;
17230380Smckusick 	register struct disklabel *lp;
17330380Smckusick 	struct disklabel *getdisklabel();
17430380Smckusick 	struct partition oldpartition;
17510762Ssam 	struct stat st;
17630380Smckusick 	int fsi, fso;
17747870Skarels 	char *cp, *special, *opstring, buf[BUFSIZ];
17853709Smckusick 	struct statfs *mp;
17953709Smckusick 	char *s1, *s2;
18053709Smckusick 	int len, n;
18110762Ssam 
18247870Skarels 	if (progname = rindex(*argv, '/'))
18347870Skarels 		++progname;
18447870Skarels 	else
18539049Smckusick 		progname = *argv;
18647870Skarels 
18750402Smckusick 	if (strstr(progname, "mfs")) {
18847870Skarels 		mfs = 1;
18939049Smckusick 		Nflag++;
19039049Smckusick 	}
19110762Ssam 
19247870Skarels 	opstring = "F:NS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
19347870Skarels 	if (!mfs)
19447870Skarels 		opstring += 2;		/* -F is mfs only */
19539325Smckusick 
19647870Skarels 	while ((ch = getopt(argc, argv, opstring)) != EOF)
19747870Skarels 		switch(ch) {
19847870Skarels 		case 'F':
19947870Skarels 			if ((mntflags = atoi(optarg)) == 0)
20047870Skarels 				fatal("%s: bad mount flags", optarg);
20147870Skarels 			break;
20247870Skarels 		case 'N':
20347870Skarels 			Nflag++;
20447870Skarels 			break;
20547870Skarels 		case 'S':
20647870Skarels 			if ((sectorsize = atoi(optarg)) <= 0)
20747870Skarels 				fatal("%s: bad sector size", optarg);
20847870Skarels 			break;
20943314Smckusick #ifdef COMPAT
21047870Skarels 		case 'T':
21147870Skarels 			disktype = optarg;
21247870Skarels 			break;
21343314Smckusick #endif
21447870Skarels 		case 'a':
21547870Skarels 			if ((maxcontig = atoi(optarg)) <= 0)
21647870Skarels 				fatal("%s: bad max contiguous blocks\n",
21747870Skarels 				    optarg);
21847870Skarels 			break;
21947870Skarels 		case 'b':
22047870Skarels 			if ((bsize = atoi(optarg)) < MINBSIZE)
22147870Skarels 				fatal("%s: bad block size", optarg);
22247870Skarels 			break;
22347870Skarels 		case 'c':
22447870Skarels 			if ((cpg = atoi(optarg)) <= 0)
22547870Skarels 				fatal("%s: bad cylinders/group", optarg);
22647870Skarels 			cpgflg++;
22747870Skarels 			break;
22847870Skarels 		case 'd':
22947870Skarels 			if ((rotdelay = atoi(optarg)) < 0)
23047870Skarels 				fatal("%s: bad rotational delay\n", optarg);
23147870Skarels 			break;
23247870Skarels 		case 'e':
23347870Skarels 			if ((maxbpg = atoi(optarg)) <= 0)
23447870Skarels 				fatal("%s: bad blocks per file in a cyl group\n",
23547870Skarels 				    optarg);
23647870Skarels 			break;
23747870Skarels 		case 'f':
23847870Skarels 			if ((fsize = atoi(optarg)) <= 0)
23947870Skarels 				fatal("%s: bad frag size", optarg);
24047870Skarels 			break;
24147870Skarels 		case 'i':
24247870Skarels 			if ((density = atoi(optarg)) <= 0)
24347870Skarels 				fatal("%s: bad bytes per inode\n", optarg);
24447870Skarels 			break;
24547870Skarels 		case 'k':
24647870Skarels 			if ((trackskew = atoi(optarg)) < 0)
24747870Skarels 				fatal("%s: bad track skew", optarg);
24847870Skarels 			break;
24947870Skarels 		case 'l':
25047870Skarels 			if ((interleave = atoi(optarg)) <= 0)
25147870Skarels 				fatal("%s: bad interleave", optarg);
25247870Skarels 			break;
25347870Skarels 		case 'm':
25447870Skarels 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
25547870Skarels 				fatal("%s: bad free space %%\n", optarg);
25647870Skarels 			break;
25747870Skarels 		case 'n':
25847870Skarels 			if ((nrpos = atoi(optarg)) <= 0)
25947870Skarels 				fatal("%s: bad rotational layout count\n",
26047870Skarels 				    optarg);
26147870Skarels 			break;
26247870Skarels 		case 'o':
26347870Skarels 			if (strcmp(optarg, "space") == 0)
26447870Skarels 				opt = FS_OPTSPACE;
26547870Skarels 			else if (strcmp(optarg, "time") == 0)
26647870Skarels 				opt = FS_OPTTIME;
26747870Skarels 			else
26847870Skarels 				fatal("%s: bad optimization preference %s",
26947870Skarels 				    optarg, "(options are `space' or `time')");
27047870Skarels 			break;
27147870Skarels 		case 'p':
27247870Skarels 			if ((trackspares = atoi(optarg)) < 0)
27347870Skarels 				fatal("%s: bad spare sectors per track",
27447870Skarels 				    optarg);
27547870Skarels 			break;
27647870Skarels 		case 'r':
27747870Skarels 			if ((rpm = atoi(optarg)) <= 0)
27847870Skarels 				fatal("%s: bad revs/minute\n", optarg);
27947870Skarels 			break;
28047870Skarels 		case 's':
28147870Skarels 			if ((fssize = atoi(optarg)) <= 0)
28247870Skarels 				fatal("%s: bad file system size", optarg);
28347870Skarels 			break;
28447870Skarels 		case 't':
28547870Skarels 			if ((ntracks = atoi(optarg)) <= 0)
28647870Skarels 				fatal("%s: bad total tracks", optarg);
28747870Skarels 			break;
28847870Skarels 		case 'u':
28947870Skarels 			if ((nsectors = atoi(optarg)) <= 0)
29047870Skarels 				fatal("%s: bad sectors/track", optarg);
29147870Skarels 			break;
29247870Skarels 		case 'x':
29347870Skarels 			if ((cylspares = atoi(optarg)) < 0)
29447870Skarels 				fatal("%s: bad spare sectors per cylinder",
29547870Skarels 				    optarg);
29647870Skarels 			break;
29747870Skarels 		case '?':
29847870Skarels 		default:
29947870Skarels 			usage();
30047870Skarels 		}
30147870Skarels 	argc -= optind;
30247870Skarels 	argv += optind;
30343314Smckusick 
30447870Skarels 	if (argc != 2 && (mfs || argc != 1))
30547870Skarels 		usage();
30610762Ssam 
30710762Ssam 	special = argv[0];
30814064Smckusick 	cp = rindex(special, '/');
30947870Skarels 	if (cp == 0) {
31047870Skarels 		/*
31147870Skarels 		 * No path prefix; try /dev/r%s then /dev/%s.
31247870Skarels 		 */
31347870Skarels 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
31447870Skarels 		if (stat(device, &st) == -1)
31547870Skarels 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
31647870Skarels 		special = device;
31747870Skarels 	}
31853709Smckusick 	if (Nflag) {
31953709Smckusick 		fso = -1;
32053709Smckusick 	} else {
32130380Smckusick 		fso = open(special, O_WRONLY);
32247870Skarels 		if (fso < 0)
32347870Skarels 			fatal("%s: %s", special, strerror(errno));
32453709Smckusick 
32553709Smckusick 		/* Bail if target special is mounted */
32653709Smckusick 		n = getmntinfo(&mp, MNT_NOWAIT);
32753709Smckusick 		if (n == 0)
32853709Smckusick 			fatal("%s: getmntinfo: %s", special, strerror(errno));
32953709Smckusick 
33053709Smckusick 		len = sizeof(_PATH_DEV) - 1;
33153709Smckusick 		s1 = special;
33253709Smckusick 		if (strncmp(_PATH_DEV, s1, len) == 0)
33353709Smckusick 			s1 += len;
33453709Smckusick 
33553709Smckusick 		while (--n >= 0) {
33653709Smckusick 			s2 = mp->f_mntfromname;
33753709Smckusick 			if (strncmp(_PATH_DEV, s2, len) == 0) {
33853709Smckusick 				s2 += len - 1;
33953709Smckusick 				*s2 = 'r';
34053709Smckusick 			}
34153709Smckusick 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
34253709Smckusick 				fatal("%s is mounted on %s",
34353709Smckusick 				    special, mp->f_mntonname);
34453709Smckusick 			++mp;
34553709Smckusick 		}
34653709Smckusick 	}
347*55561Smckusick 	if (mfs && disktype != NULL) {
348*55561Smckusick 		lp = (struct disklabel *)getdiskbyname(disktype);
349*55561Smckusick 		if (lp == NULL)
350*55561Smckusick 			fatal("%s: unknown disk type", disktype);
351*55561Smckusick 		pp = &lp->d_partitions[1];
352*55561Smckusick 	} else {
353*55561Smckusick 		fsi = open(special, O_RDONLY);
354*55561Smckusick 		if (fsi < 0)
355*55561Smckusick 			fatal("%s: %s", special, strerror(errno));
356*55561Smckusick 		if (fstat(fsi, &st) < 0)
357*55561Smckusick 			fatal("%s: %s", special, strerror(errno));
358*55561Smckusick 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
359*55561Smckusick 			printf("%s: %s: not a character-special device\n",
360*55561Smckusick 			    progname, special);
361*55561Smckusick 		cp = index(argv[0], '\0') - 1;
362*55561Smckusick 		if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
363*55561Smckusick 			fatal("%s: can't figure out file system partition",
364*55561Smckusick 			    argv[0]);
36531680Skarels #ifdef COMPAT
366*55561Smckusick 		if (!mfs && disktype == NULL)
367*55561Smckusick 			disktype = argv[1];
36843314Smckusick #endif
369*55561Smckusick 		lp = getdisklabel(special, fsi);
370*55561Smckusick 		if (isdigit(*cp))
371*55561Smckusick 			pp = &lp->d_partitions[0];
372*55561Smckusick 		else
373*55561Smckusick 			pp = &lp->d_partitions[*cp - 'a'];
374*55561Smckusick 		if (pp->p_size == 0)
375*55561Smckusick 			fatal("%s: `%c' partition is unavailable",
376*55561Smckusick 			    argv[0], *cp);
377*55561Smckusick 	}
37830380Smckusick 	if (fssize == 0)
37910762Ssam 		fssize = pp->p_size;
38039325Smckusick 	if (fssize > pp->p_size && !mfs)
38130380Smckusick 	       fatal("%s: maximum file system size on the `%c' partition is %d",
38230380Smckusick 			argv[0], *cp, pp->p_size);
38330380Smckusick 	if (rpm == 0) {
38430380Smckusick 		rpm = lp->d_rpm;
38530380Smckusick 		if (rpm <= 0)
38630743Skarels 			rpm = 3600;
38710762Ssam 	}
38830380Smckusick 	if (ntracks == 0) {
38930380Smckusick 		ntracks = lp->d_ntracks;
39030380Smckusick 		if (ntracks <= 0)
39130743Skarels 			fatal("%s: no default #tracks", argv[0]);
39230380Smckusick 	}
39310762Ssam 	if (nsectors == 0) {
39430380Smckusick 		nsectors = lp->d_nsectors;
39530380Smckusick 		if (nsectors <= 0)
39630743Skarels 			fatal("%s: no default #sectors/track", argv[0]);
39710762Ssam 	}
39810762Ssam 	if (sectorsize == 0) {
39930380Smckusick 		sectorsize = lp->d_secsize;
40030380Smckusick 		if (sectorsize <= 0)
40130743Skarels 			fatal("%s: no default sector size", argv[0]);
40210762Ssam 	}
40330386Smckusick 	if (trackskew == -1) {
40430386Smckusick 		trackskew = lp->d_trackskew;
40530386Smckusick 		if (trackskew < 0)
40630743Skarels 			trackskew = 0;
40730386Smckusick 	}
40830386Smckusick 	if (interleave == 0) {
40930386Smckusick 		interleave = lp->d_interleave;
41030386Smckusick 		if (interleave <= 0)
41130743Skarels 			interleave = 1;
41230386Smckusick 	}
41310762Ssam 	if (fsize == 0) {
41410762Ssam 		fsize = pp->p_fsize;
41530380Smckusick 		if (fsize <= 0)
41630380Smckusick 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
41710762Ssam 	}
41830380Smckusick 	if (bsize == 0) {
41930380Smckusick 		bsize = pp->p_frag * pp->p_fsize;
42030380Smckusick 		if (bsize <= 0)
42130380Smckusick 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
42211069Ssam 	}
42339049Smckusick 	if (density == 0)
42439049Smckusick 		density = NFPI * fsize;
42524702Smckusick 	if (minfree < 10 && opt != FS_OPTSPACE) {
42630380Smckusick 		fprintf(stderr, "Warning: changing optimization to space ");
42730380Smckusick 		fprintf(stderr, "because minfree is less than 10%%\n");
42824702Smckusick 		opt = FS_OPTSPACE;
42924702Smckusick 	}
43030386Smckusick 	if (trackspares == -1) {
43130386Smckusick 		trackspares = lp->d_sparespertrack;
43230386Smckusick 		if (trackspares < 0)
43330743Skarels 			trackspares = 0;
43430386Smckusick 	}
43530386Smckusick 	nphyssectors = nsectors + trackspares;
43630386Smckusick 	if (cylspares == -1) {
43730386Smckusick 		cylspares = lp->d_sparespercyl;
43830386Smckusick 		if (cylspares < 0)
43930743Skarels 			cylspares = 0;
44030386Smckusick 	}
44130386Smckusick 	secpercyl = nsectors * ntracks - cylspares;
44230380Smckusick 	if (secpercyl != lp->d_secpercyl)
44347870Skarels 		fprintf(stderr, "%s (%d) %s (%lu)\n",
44430380Smckusick 			"Warning: calculated sectors per cylinder", secpercyl,
44530380Smckusick 			"disagrees with disk label", lp->d_secpercyl);
44632321Smckusick 	if (maxbpg == 0)
44732321Smckusick 		maxbpg = MAXBLKPG(bsize);
44830386Smckusick 	headswitch = lp->d_headswitch;
44930386Smckusick 	trackseek = lp->d_trkseek;
45047870Skarels #ifdef notdef /* label may be 0 if faked up by kernel */
45130691Skarels 	bbsize = lp->d_bbsize;
45230691Skarels 	sbsize = lp->d_sbsize;
45345836Skarels #endif
45430380Smckusick 	oldpartition = *pp;
45530691Skarels #ifdef tahoe
45630691Skarels 	realsectorsize = sectorsize;
45730862Skarels 	if (sectorsize != DEV_BSIZE) {		/* XXX */
45830691Skarels 		int secperblk = DEV_BSIZE / sectorsize;
45930691Skarels 
46030691Skarels 		sectorsize = DEV_BSIZE;
46130691Skarels 		nsectors /= secperblk;
46230691Skarels 		nphyssectors /= secperblk;
46330691Skarels 		secpercyl /= secperblk;
46430691Skarels 		fssize /= secperblk;
46530691Skarels 		pp->p_size /= secperblk;
46630691Skarels 	}
46730691Skarels #endif
46830380Smckusick 	mkfs(pp, special, fsi, fso);
46930691Skarels #ifdef tahoe
47030691Skarels 	if (realsectorsize != DEV_BSIZE)
47130691Skarels 		pp->p_size *= DEV_BSIZE / realsectorsize;
47230691Skarels #endif
47330380Smckusick 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
47430380Smckusick 		rewritelabel(special, fso, lp);
47539049Smckusick 	if (!Nflag)
47639049Smckusick 		close(fso);
47739049Smckusick 	close(fsi);
47849063Sbostic #ifdef MFS
47939325Smckusick 	if (mfs) {
48049063Sbostic 		struct mfs_args args;
48149063Sbostic 
48239325Smckusick 		sprintf(buf, "mfs:%d", getpid());
48339049Smckusick 		args.name = buf;
48439049Smckusick 		args.base = membase;
48539049Smckusick 		args.size = fssize * sectorsize;
48647870Skarels 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
48747870Skarels 			fatal("%s: %s", argv[1], strerror(errno));
48839049Smckusick 	}
48949063Sbostic #endif
49030380Smckusick 	exit(0);
49130380Smckusick }
49230380Smckusick 
49331680Skarels #ifdef COMPAT
49443314Smckusick char lmsg[] = "%s: can't read disk label; disk type must be specified";
49543314Smckusick #else
49643314Smckusick char lmsg[] = "%s: can't read disk label";
49743314Smckusick #endif
49831680Skarels 
49931680Skarels struct disklabel *
50030380Smckusick getdisklabel(s, fd)
50130380Smckusick 	char *s;
50231680Skarels 	int fd;
50330380Smckusick {
50430380Smckusick 	static struct disklabel lab;
50530380Smckusick 
50630380Smckusick 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
50743314Smckusick #ifdef COMPAT
50843314Smckusick 		if (disktype) {
50947870Skarels 			struct disklabel *lp, *getdiskbyname();
51043314Smckusick 
51147870Skarels 			unlabeled++;
51247870Skarels 			lp = getdiskbyname(disktype);
51347870Skarels 			if (lp == NULL)
51447870Skarels 				fatal("%s: unknown disk type", disktype);
51547870Skarels 			return (lp);
51643314Smckusick 		}
51743314Smckusick #endif
51847870Skarels 		(void)fprintf(stderr,
51947870Skarels 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
52043314Smckusick 		fatal(lmsg, s);
52110762Ssam 	}
52230380Smckusick 	return (&lab);
52330380Smckusick }
52410762Ssam 
52530380Smckusick rewritelabel(s, fd, lp)
52630380Smckusick 	char *s;
52730380Smckusick 	int fd;
52830380Smckusick 	register struct disklabel *lp;
52930380Smckusick {
53031680Skarels #ifdef COMPAT
53147870Skarels 	if (unlabeled)
53231680Skarels 		return;
53331680Skarels #endif
53430380Smckusick 	lp->d_checksum = 0;
53530380Smckusick 	lp->d_checksum = dkcksum(lp);
53630380Smckusick 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
53747870Skarels 		(void)fprintf(stderr,
53847870Skarels 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
53930380Smckusick 		fatal("%s: can't rewrite disk label", s);
54010762Ssam 	}
54130446Skarels #if vax
54230446Skarels 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
54330446Skarels 		register i;
54430691Skarels 		int cfd;
54530446Skarels 		daddr_t alt;
54630691Skarels 		char specname[64];
54730691Skarels 		char blk[1024];
54831045Ssam 		char *cp;
54930446Skarels 
55030691Skarels 		/*
55130691Skarels 		 * Make name for 'c' partition.
55230691Skarels 		 */
55330691Skarels 		strcpy(specname, s);
55430691Skarels 		cp = specname + strlen(specname) - 1;
55530691Skarels 		if (!isdigit(*cp))
55630691Skarels 			*cp = 'c';
55730691Skarels 		cfd = open(specname, O_WRONLY);
55847870Skarels 		if (cfd < 0)
55947870Skarels 			fatal("%s: %s", specname, strerror(errno));
56030691Skarels 		bzero(blk, sizeof(blk));
56130691Skarels 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
56230446Skarels 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
56330446Skarels 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
56447870Skarels 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
56547870Skarels 			    L_SET) == -1)
56647870Skarels 				fatal("lseek to badsector area: %s",
56747870Skarels 				    strerror(errno));
56847870Skarels 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
56947870Skarels 				fprintf(stderr,
57047870Skarels 				    "%s: alternate label %d write: %s\n",
57147870Skarels 				    progname, i/2, strerror(errno));
57230380Smckusick 		}
57339049Smckusick 		close(cfd);
57430380Smckusick 	}
57530446Skarels #endif
57610762Ssam }
57710762Ssam 
57810762Ssam /*VARARGS*/
57950787Storek void
58050787Storek #if __STDC__
58150787Storek fatal(const char *fmt, ...)
58250787Storek #else
58350787Storek fatal(fmt, va_alist)
58410762Ssam 	char *fmt;
58550787Storek 	va_dcl
58650787Storek #endif
58710762Ssam {
58847870Skarels 	va_list ap;
58910762Ssam 
59050787Storek #if __STDC__
59150787Storek 	va_start(ap, fmt);
59250787Storek #else
59350787Storek 	va_start(ap);
59450787Storek #endif
59539049Smckusick 	fprintf(stderr, "%s: ", progname);
59647870Skarels 	(void)vfprintf(stderr, fmt, ap);
59747870Skarels 	va_end(ap);
59810762Ssam 	putc('\n', stderr);
59947870Skarels 	exit(1);
60010762Ssam }
60147870Skarels 
60247870Skarels usage()
60347870Skarels {
60447870Skarels 	if (mfs) {
60547870Skarels 		fprintf(stderr,
60653709Smckusick 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
60753709Smckusick 			progname);
60847870Skarels 	} else
60947870Skarels 		fprintf(stderr,
61053709Smckusick 		    "usage: %s [ -fsoptions ] special-device%s\n",
61153709Smckusick 		    progname,
61247870Skarels #ifdef COMPAT
61347870Skarels 		    " [device-type]");
61447870Skarels #else
61547870Skarels 		    "");
61647870Skarels #endif
61747870Skarels 	fprintf(stderr, "where fsoptions are:\n");
61847870Skarels 	fprintf(stderr,
61947870Skarels 	    "\t-N do not create file system, just print out parameters\n");
62047870Skarels 	fprintf(stderr, "\t-S sector size\n");
62147870Skarels #ifdef COMPAT
62247870Skarels 	fprintf(stderr, "\t-T disktype\n");
62347870Skarels #endif
62447870Skarels 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
62547870Skarels 	fprintf(stderr, "\t-b block size\n");
62647870Skarels 	fprintf(stderr, "\t-c cylinders/group\n");
62747870Skarels 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
62847870Skarels 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
62947870Skarels 	fprintf(stderr, "\t-f frag size\n");
63047870Skarels 	fprintf(stderr, "\t-i number of bytes per inode\n");
63147870Skarels 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
63247870Skarels 	fprintf(stderr, "\t-l hardware sector interleave\n");
63347870Skarels 	fprintf(stderr, "\t-m minimum free space %%\n");
63447870Skarels 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
63547870Skarels 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
63647870Skarels 	fprintf(stderr, "\t-p spare sectors per track\n");
63747870Skarels 	fprintf(stderr, "\t-s file system size (sectors)\n");
63847870Skarels 	fprintf(stderr, "\t-r revolutions/minute\n");
63947870Skarels 	fprintf(stderr, "\t-t tracks/cylinder\n");
64047870Skarels 	fprintf(stderr, "\t-u sectors/track\n");
64147870Skarels 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
64247870Skarels 	exit(1);
64347870Skarels }
644