xref: /csrg-svn/sbin/newfs/newfs.c (revision 59679)
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*59679Shibler static char sccsid[] = "@(#)newfs.c	6.33 (Berkeley) 05/03/93";
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 	}
34755561Smckusick 	if (mfs && disktype != NULL) {
34855561Smckusick 		lp = (struct disklabel *)getdiskbyname(disktype);
34955561Smckusick 		if (lp == NULL)
35055561Smckusick 			fatal("%s: unknown disk type", disktype);
35155561Smckusick 		pp = &lp->d_partitions[1];
35255561Smckusick 	} else {
35355561Smckusick 		fsi = open(special, O_RDONLY);
35455561Smckusick 		if (fsi < 0)
35555561Smckusick 			fatal("%s: %s", special, strerror(errno));
35655561Smckusick 		if (fstat(fsi, &st) < 0)
35755561Smckusick 			fatal("%s: %s", special, strerror(errno));
35855561Smckusick 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
35955561Smckusick 			printf("%s: %s: not a character-special device\n",
36055561Smckusick 			    progname, special);
36155561Smckusick 		cp = index(argv[0], '\0') - 1;
36255561Smckusick 		if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
36355561Smckusick 			fatal("%s: can't figure out file system partition",
36455561Smckusick 			    argv[0]);
36531680Skarels #ifdef COMPAT
36655561Smckusick 		if (!mfs && disktype == NULL)
36755561Smckusick 			disktype = argv[1];
36843314Smckusick #endif
36955561Smckusick 		lp = getdisklabel(special, fsi);
37055561Smckusick 		if (isdigit(*cp))
37155561Smckusick 			pp = &lp->d_partitions[0];
37255561Smckusick 		else
37355561Smckusick 			pp = &lp->d_partitions[*cp - 'a'];
37455561Smckusick 		if (pp->p_size == 0)
37555561Smckusick 			fatal("%s: `%c' partition is unavailable",
37655561Smckusick 			    argv[0], *cp);
377*59679Shibler 		if (pp->p_fstype == FS_BOOT)
378*59679Shibler 			fatal("%s: `%c' partition overlaps boot program",
379*59679Shibler 			      argv[0], *cp);
38055561Smckusick 	}
38130380Smckusick 	if (fssize == 0)
38210762Ssam 		fssize = pp->p_size;
38339325Smckusick 	if (fssize > pp->p_size && !mfs)
38430380Smckusick 	       fatal("%s: maximum file system size on the `%c' partition is %d",
38530380Smckusick 			argv[0], *cp, pp->p_size);
38630380Smckusick 	if (rpm == 0) {
38730380Smckusick 		rpm = lp->d_rpm;
38830380Smckusick 		if (rpm <= 0)
38930743Skarels 			rpm = 3600;
39010762Ssam 	}
39130380Smckusick 	if (ntracks == 0) {
39230380Smckusick 		ntracks = lp->d_ntracks;
39330380Smckusick 		if (ntracks <= 0)
39430743Skarels 			fatal("%s: no default #tracks", argv[0]);
39530380Smckusick 	}
39610762Ssam 	if (nsectors == 0) {
39730380Smckusick 		nsectors = lp->d_nsectors;
39830380Smckusick 		if (nsectors <= 0)
39930743Skarels 			fatal("%s: no default #sectors/track", argv[0]);
40010762Ssam 	}
40110762Ssam 	if (sectorsize == 0) {
40230380Smckusick 		sectorsize = lp->d_secsize;
40330380Smckusick 		if (sectorsize <= 0)
40430743Skarels 			fatal("%s: no default sector size", argv[0]);
40510762Ssam 	}
40630386Smckusick 	if (trackskew == -1) {
40730386Smckusick 		trackskew = lp->d_trackskew;
40830386Smckusick 		if (trackskew < 0)
40930743Skarels 			trackskew = 0;
41030386Smckusick 	}
41130386Smckusick 	if (interleave == 0) {
41230386Smckusick 		interleave = lp->d_interleave;
41330386Smckusick 		if (interleave <= 0)
41430743Skarels 			interleave = 1;
41530386Smckusick 	}
41610762Ssam 	if (fsize == 0) {
41710762Ssam 		fsize = pp->p_fsize;
41830380Smckusick 		if (fsize <= 0)
41930380Smckusick 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
42010762Ssam 	}
42130380Smckusick 	if (bsize == 0) {
42230380Smckusick 		bsize = pp->p_frag * pp->p_fsize;
42330380Smckusick 		if (bsize <= 0)
42430380Smckusick 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
42511069Ssam 	}
42639049Smckusick 	if (density == 0)
42739049Smckusick 		density = NFPI * fsize;
42824702Smckusick 	if (minfree < 10 && opt != FS_OPTSPACE) {
42930380Smckusick 		fprintf(stderr, "Warning: changing optimization to space ");
43030380Smckusick 		fprintf(stderr, "because minfree is less than 10%%\n");
43124702Smckusick 		opt = FS_OPTSPACE;
43224702Smckusick 	}
43330386Smckusick 	if (trackspares == -1) {
43430386Smckusick 		trackspares = lp->d_sparespertrack;
43530386Smckusick 		if (trackspares < 0)
43630743Skarels 			trackspares = 0;
43730386Smckusick 	}
43830386Smckusick 	nphyssectors = nsectors + trackspares;
43930386Smckusick 	if (cylspares == -1) {
44030386Smckusick 		cylspares = lp->d_sparespercyl;
44130386Smckusick 		if (cylspares < 0)
44230743Skarels 			cylspares = 0;
44330386Smckusick 	}
44430386Smckusick 	secpercyl = nsectors * ntracks - cylspares;
44530380Smckusick 	if (secpercyl != lp->d_secpercyl)
44647870Skarels 		fprintf(stderr, "%s (%d) %s (%lu)\n",
44730380Smckusick 			"Warning: calculated sectors per cylinder", secpercyl,
44830380Smckusick 			"disagrees with disk label", lp->d_secpercyl);
44932321Smckusick 	if (maxbpg == 0)
45032321Smckusick 		maxbpg = MAXBLKPG(bsize);
45130386Smckusick 	headswitch = lp->d_headswitch;
45230386Smckusick 	trackseek = lp->d_trkseek;
45347870Skarels #ifdef notdef /* label may be 0 if faked up by kernel */
45430691Skarels 	bbsize = lp->d_bbsize;
45530691Skarels 	sbsize = lp->d_sbsize;
45645836Skarels #endif
45730380Smckusick 	oldpartition = *pp;
45830691Skarels #ifdef tahoe
45930691Skarels 	realsectorsize = sectorsize;
46030862Skarels 	if (sectorsize != DEV_BSIZE) {		/* XXX */
46130691Skarels 		int secperblk = DEV_BSIZE / sectorsize;
46230691Skarels 
46330691Skarels 		sectorsize = DEV_BSIZE;
46430691Skarels 		nsectors /= secperblk;
46530691Skarels 		nphyssectors /= secperblk;
46630691Skarels 		secpercyl /= secperblk;
46730691Skarels 		fssize /= secperblk;
46830691Skarels 		pp->p_size /= secperblk;
46930691Skarels 	}
47030691Skarels #endif
47130380Smckusick 	mkfs(pp, special, fsi, fso);
47230691Skarels #ifdef tahoe
47330691Skarels 	if (realsectorsize != DEV_BSIZE)
47430691Skarels 		pp->p_size *= DEV_BSIZE / realsectorsize;
47530691Skarels #endif
47630380Smckusick 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
47730380Smckusick 		rewritelabel(special, fso, lp);
47839049Smckusick 	if (!Nflag)
47939049Smckusick 		close(fso);
48039049Smckusick 	close(fsi);
48149063Sbostic #ifdef MFS
48239325Smckusick 	if (mfs) {
48349063Sbostic 		struct mfs_args args;
48449063Sbostic 
48539325Smckusick 		sprintf(buf, "mfs:%d", getpid());
48639049Smckusick 		args.name = buf;
48739049Smckusick 		args.base = membase;
48839049Smckusick 		args.size = fssize * sectorsize;
48947870Skarels 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
49047870Skarels 			fatal("%s: %s", argv[1], strerror(errno));
49139049Smckusick 	}
49249063Sbostic #endif
49330380Smckusick 	exit(0);
49430380Smckusick }
49530380Smckusick 
49631680Skarels #ifdef COMPAT
49743314Smckusick char lmsg[] = "%s: can't read disk label; disk type must be specified";
49843314Smckusick #else
49943314Smckusick char lmsg[] = "%s: can't read disk label";
50043314Smckusick #endif
50131680Skarels 
50231680Skarels struct disklabel *
50330380Smckusick getdisklabel(s, fd)
50430380Smckusick 	char *s;
50531680Skarels 	int fd;
50630380Smckusick {
50730380Smckusick 	static struct disklabel lab;
50830380Smckusick 
50930380Smckusick 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
51043314Smckusick #ifdef COMPAT
51143314Smckusick 		if (disktype) {
51247870Skarels 			struct disklabel *lp, *getdiskbyname();
51343314Smckusick 
51447870Skarels 			unlabeled++;
51547870Skarels 			lp = getdiskbyname(disktype);
51647870Skarels 			if (lp == NULL)
51747870Skarels 				fatal("%s: unknown disk type", disktype);
51847870Skarels 			return (lp);
51943314Smckusick 		}
52043314Smckusick #endif
52147870Skarels 		(void)fprintf(stderr,
52247870Skarels 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
52343314Smckusick 		fatal(lmsg, s);
52410762Ssam 	}
52530380Smckusick 	return (&lab);
52630380Smckusick }
52710762Ssam 
52830380Smckusick rewritelabel(s, fd, lp)
52930380Smckusick 	char *s;
53030380Smckusick 	int fd;
53130380Smckusick 	register struct disklabel *lp;
53230380Smckusick {
53331680Skarels #ifdef COMPAT
53447870Skarels 	if (unlabeled)
53531680Skarels 		return;
53631680Skarels #endif
53730380Smckusick 	lp->d_checksum = 0;
53830380Smckusick 	lp->d_checksum = dkcksum(lp);
53930380Smckusick 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
54047870Skarels 		(void)fprintf(stderr,
54147870Skarels 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
54230380Smckusick 		fatal("%s: can't rewrite disk label", s);
54310762Ssam 	}
54430446Skarels #if vax
54530446Skarels 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
54630446Skarels 		register i;
54730691Skarels 		int cfd;
54830446Skarels 		daddr_t alt;
54930691Skarels 		char specname[64];
55030691Skarels 		char blk[1024];
55131045Ssam 		char *cp;
55230446Skarels 
55330691Skarels 		/*
55430691Skarels 		 * Make name for 'c' partition.
55530691Skarels 		 */
55630691Skarels 		strcpy(specname, s);
55730691Skarels 		cp = specname + strlen(specname) - 1;
55830691Skarels 		if (!isdigit(*cp))
55930691Skarels 			*cp = 'c';
56030691Skarels 		cfd = open(specname, O_WRONLY);
56147870Skarels 		if (cfd < 0)
56247870Skarels 			fatal("%s: %s", specname, strerror(errno));
56330691Skarels 		bzero(blk, sizeof(blk));
56430691Skarels 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
56530446Skarels 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
56630446Skarels 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
56747870Skarels 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
56847870Skarels 			    L_SET) == -1)
56947870Skarels 				fatal("lseek to badsector area: %s",
57047870Skarels 				    strerror(errno));
57147870Skarels 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
57247870Skarels 				fprintf(stderr,
57347870Skarels 				    "%s: alternate label %d write: %s\n",
57447870Skarels 				    progname, i/2, strerror(errno));
57530380Smckusick 		}
57639049Smckusick 		close(cfd);
57730380Smckusick 	}
57830446Skarels #endif
57910762Ssam }
58010762Ssam 
58110762Ssam /*VARARGS*/
58250787Storek void
58350787Storek #if __STDC__
58450787Storek fatal(const char *fmt, ...)
58550787Storek #else
58650787Storek fatal(fmt, va_alist)
58710762Ssam 	char *fmt;
58850787Storek 	va_dcl
58950787Storek #endif
59010762Ssam {
59147870Skarels 	va_list ap;
59210762Ssam 
59350787Storek #if __STDC__
59450787Storek 	va_start(ap, fmt);
59550787Storek #else
59650787Storek 	va_start(ap);
59750787Storek #endif
59839049Smckusick 	fprintf(stderr, "%s: ", progname);
59947870Skarels 	(void)vfprintf(stderr, fmt, ap);
60047870Skarels 	va_end(ap);
60110762Ssam 	putc('\n', stderr);
60247870Skarels 	exit(1);
60310762Ssam }
60447870Skarels 
60547870Skarels usage()
60647870Skarels {
60747870Skarels 	if (mfs) {
60847870Skarels 		fprintf(stderr,
60953709Smckusick 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
61053709Smckusick 			progname);
61147870Skarels 	} else
61247870Skarels 		fprintf(stderr,
61353709Smckusick 		    "usage: %s [ -fsoptions ] special-device%s\n",
61453709Smckusick 		    progname,
61547870Skarels #ifdef COMPAT
61647870Skarels 		    " [device-type]");
61747870Skarels #else
61847870Skarels 		    "");
61947870Skarels #endif
62047870Skarels 	fprintf(stderr, "where fsoptions are:\n");
62147870Skarels 	fprintf(stderr,
62247870Skarels 	    "\t-N do not create file system, just print out parameters\n");
62347870Skarels 	fprintf(stderr, "\t-S sector size\n");
62447870Skarels #ifdef COMPAT
62547870Skarels 	fprintf(stderr, "\t-T disktype\n");
62647870Skarels #endif
62747870Skarels 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
62847870Skarels 	fprintf(stderr, "\t-b block size\n");
62947870Skarels 	fprintf(stderr, "\t-c cylinders/group\n");
63047870Skarels 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
63147870Skarels 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
63247870Skarels 	fprintf(stderr, "\t-f frag size\n");
63347870Skarels 	fprintf(stderr, "\t-i number of bytes per inode\n");
63447870Skarels 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
63547870Skarels 	fprintf(stderr, "\t-l hardware sector interleave\n");
63647870Skarels 	fprintf(stderr, "\t-m minimum free space %%\n");
63747870Skarels 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
63847870Skarels 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
63947870Skarels 	fprintf(stderr, "\t-p spare sectors per track\n");
64047870Skarels 	fprintf(stderr, "\t-s file system size (sectors)\n");
64147870Skarels 	fprintf(stderr, "\t-r revolutions/minute\n");
64247870Skarels 	fprintf(stderr, "\t-t tracks/cylinder\n");
64347870Skarels 	fprintf(stderr, "\t-u sectors/track\n");
64447870Skarels 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
64547870Skarels 	exit(1);
64647870Skarels }
647