xref: /csrg-svn/sbin/newfs/newfs.c (revision 65711)
121159Sdist /*
261523Sbostic  * Copyright (c) 1983, 1989, 1993
361523Sbostic  *	The Regents of the University of California.  All rights reserved.
439049Smckusick  *
542704Sbostic  * %sccs.include.redist.c%
621159Sdist  */
721159Sdist 
810762Ssam #ifndef lint
9*65711Shibler static char sccsid[] = "@(#)newfs.c	8.2 (Berkeley) 01/12/94";
1039049Smckusick #endif /* not lint */
1139049Smckusick 
1239049Smckusick #ifndef lint
1361523Sbostic static char copyright[] =
1461523Sbostic "@(#) Copyright (c) 1983, 1989, 1993\n\
1561523Sbostic 	The Regents of the University of California.  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
7559750Smckusick  * is run at between 95% and 100% full; thus the default value of
7659750Smckusick  * fs_minfree is 5%. With 5% free space, fragmentation is not a
7730380Smckusick  * problem, so we choose to optimize for time.
7830380Smckusick  */
7959750Smckusick #define MINFREE		5
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 /*
9132309Smckusick  * MAXBLKPG determines the maximum number of data blocks which are
9232309Smckusick  * placed in a single cylinder group. The default is one indirect
9332309Smckusick  * block worth of data blocks.
9432309Smckusick  */
9532309Smckusick #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
9632309Smckusick 
9732309Smckusick /*
9830380Smckusick  * Each file system has a number of inodes statically allocated.
9939049Smckusick  * We allocate one inode slot per NFPI fragments, expecting this
10030380Smckusick  * to be far more than we will ever need.
10130380Smckusick  */
10239049Smckusick #define	NFPI		4
10330380Smckusick 
10434137Smckusick /*
10534137Smckusick  * For each cylinder we keep track of the availability of blocks at different
10634137Smckusick  * rotational positions, so that we can lay out the data to be picked
10734137Smckusick  * up with minimum rotational latency.  NRPOS is the default number of
10834137Smckusick  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
10934137Smckusick  * of our summary information is 2ms for a typical 3600 rpm drive.
11034137Smckusick  */
11134137Smckusick #define	NRPOS		8	/* number distinct rotational positions */
11234137Smckusick 
11334137Smckusick 
11439325Smckusick int	mfs;			/* run as the memory based filesystem */
11530380Smckusick int	Nflag;			/* run without writing file system */
11659750Smckusick int	Oflag;			/* format as an 4.3BSD file system */
11710762Ssam int	fssize;			/* file system size */
11810762Ssam int	ntracks;		/* # tracks/cylinder */
11910762Ssam int	nsectors;		/* # sectors/track */
12030386Smckusick int	nphyssectors;		/* # sectors/track including spares */
12130380Smckusick int	secpercyl;		/* sectors per cylinder */
12230386Smckusick int	trackspares = -1;	/* spare sectors per track */
12330386Smckusick int	cylspares = -1;		/* spare sectors per cylinder */
12410762Ssam int	sectorsize;		/* bytes/sector */
12530691Skarels #ifdef tahoe
12630691Skarels int	realsectorsize;		/* bytes/sector in hardware */
12730691Skarels #endif
12811069Ssam int	rpm;			/* revolutions/minute of drive */
12930386Smckusick int	interleave;		/* hardware sector interleave */
13030386Smckusick int	trackskew = -1;		/* sector 0 skew, per track */
13130386Smckusick int	headswitch;		/* head switch time, usec */
13230386Smckusick int	trackseek;		/* track-to-track seek, usec */
13330862Skarels int	fsize = 0;		/* fragment size */
13430862Skarels int	bsize = 0;		/* block size */
13530380Smckusick int	cpg = DESCPG;		/* cylinders/cylinder group */
13632119Smckusick int	cpgflg;			/* cylinders/cylinder group flag was given */
13730380Smckusick int	minfree = MINFREE;	/* free space threshold */
13830380Smckusick int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
13939049Smckusick int	density;		/* number of bytes per inode */
14059750Smckusick int	maxcontig = 0;		/* max contiguous blocks to allocate */
14130380Smckusick int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
14232309Smckusick int	maxbpg;			/* maximum blocks per file in a cyl group */
14334137Smckusick int	nrpos = NRPOS;		/* # of distinguished rotational positions */
14430691Skarels int	bbsize = BBSIZE;	/* boot block size */
14530691Skarels int	sbsize = SBSIZE;	/* superblock size */
14639325Smckusick int	mntflags;		/* flags to be passed to mount */
14739049Smckusick u_long	memleft;		/* virtual memory available */
14839049Smckusick caddr_t	membase;		/* start address of memory based filesystem */
14931680Skarels #ifdef COMPAT
15043314Smckusick char	*disktype;
15147870Skarels int	unlabeled;
15231680Skarels #endif
15310762Ssam 
15410762Ssam char	device[MAXPATHLEN];
15539049Smckusick char	*progname;
15610762Ssam 
15710762Ssam main(argc, argv)
15814064Smckusick 	int argc;
15910762Ssam 	char *argv[];
16010762Ssam {
16147870Skarels 	extern char *optarg;
16247870Skarels 	extern int optind;
16347870Skarels 	register int ch;
16410762Ssam 	register struct partition *pp;
16530380Smckusick 	register struct disklabel *lp;
16630380Smckusick 	struct disklabel *getdisklabel();
16730380Smckusick 	struct partition oldpartition;
16810762Ssam 	struct stat st;
16930380Smckusick 	int fsi, fso;
17047870Skarels 	char *cp, *special, *opstring, buf[BUFSIZ];
17153709Smckusick 	struct statfs *mp;
17253709Smckusick 	char *s1, *s2;
17353709Smckusick 	int len, n;
17410762Ssam 
17547870Skarels 	if (progname = rindex(*argv, '/'))
17647870Skarels 		++progname;
17747870Skarels 	else
17839049Smckusick 		progname = *argv;
17947870Skarels 
18050402Smckusick 	if (strstr(progname, "mfs")) {
18147870Skarels 		mfs = 1;
18239049Smckusick 		Nflag++;
18339049Smckusick 	}
18410762Ssam 
18559750Smckusick 	opstring = "F:NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
18647870Skarels 	if (!mfs)
18747870Skarels 		opstring += 2;		/* -F is mfs only */
18839325Smckusick 
18947870Skarels 	while ((ch = getopt(argc, argv, opstring)) != EOF)
19047870Skarels 		switch(ch) {
19147870Skarels 		case 'F':
19247870Skarels 			if ((mntflags = atoi(optarg)) == 0)
19347870Skarels 				fatal("%s: bad mount flags", optarg);
19447870Skarels 			break;
19547870Skarels 		case 'N':
19647870Skarels 			Nflag++;
19747870Skarels 			break;
19859750Smckusick 		case 'O':
19959750Smckusick 			Oflag++;
20059750Smckusick 			break;
20147870Skarels 		case 'S':
20247870Skarels 			if ((sectorsize = atoi(optarg)) <= 0)
20347870Skarels 				fatal("%s: bad sector size", optarg);
20447870Skarels 			break;
20543314Smckusick #ifdef COMPAT
20647870Skarels 		case 'T':
20747870Skarels 			disktype = optarg;
20847870Skarels 			break;
20943314Smckusick #endif
21047870Skarels 		case 'a':
21147870Skarels 			if ((maxcontig = atoi(optarg)) <= 0)
21247870Skarels 				fatal("%s: bad max contiguous blocks\n",
21347870Skarels 				    optarg);
21447870Skarels 			break;
21547870Skarels 		case 'b':
21647870Skarels 			if ((bsize = atoi(optarg)) < MINBSIZE)
21747870Skarels 				fatal("%s: bad block size", optarg);
21847870Skarels 			break;
21947870Skarels 		case 'c':
22047870Skarels 			if ((cpg = atoi(optarg)) <= 0)
22147870Skarels 				fatal("%s: bad cylinders/group", optarg);
22247870Skarels 			cpgflg++;
22347870Skarels 			break;
22447870Skarels 		case 'd':
22547870Skarels 			if ((rotdelay = atoi(optarg)) < 0)
22647870Skarels 				fatal("%s: bad rotational delay\n", optarg);
22747870Skarels 			break;
22847870Skarels 		case 'e':
22947870Skarels 			if ((maxbpg = atoi(optarg)) <= 0)
23047870Skarels 				fatal("%s: bad blocks per file in a cyl group\n",
23147870Skarels 				    optarg);
23247870Skarels 			break;
23347870Skarels 		case 'f':
23447870Skarels 			if ((fsize = atoi(optarg)) <= 0)
23547870Skarels 				fatal("%s: bad frag size", optarg);
23647870Skarels 			break;
23747870Skarels 		case 'i':
23847870Skarels 			if ((density = atoi(optarg)) <= 0)
23947870Skarels 				fatal("%s: bad bytes per inode\n", optarg);
24047870Skarels 			break;
24147870Skarels 		case 'k':
24247870Skarels 			if ((trackskew = atoi(optarg)) < 0)
24347870Skarels 				fatal("%s: bad track skew", optarg);
24447870Skarels 			break;
24547870Skarels 		case 'l':
24647870Skarels 			if ((interleave = atoi(optarg)) <= 0)
24747870Skarels 				fatal("%s: bad interleave", optarg);
24847870Skarels 			break;
24947870Skarels 		case 'm':
25047870Skarels 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
25147870Skarels 				fatal("%s: bad free space %%\n", optarg);
25247870Skarels 			break;
25347870Skarels 		case 'n':
25447870Skarels 			if ((nrpos = atoi(optarg)) <= 0)
25547870Skarels 				fatal("%s: bad rotational layout count\n",
25647870Skarels 				    optarg);
25747870Skarels 			break;
25847870Skarels 		case 'o':
25947870Skarels 			if (strcmp(optarg, "space") == 0)
26047870Skarels 				opt = FS_OPTSPACE;
26147870Skarels 			else if (strcmp(optarg, "time") == 0)
26247870Skarels 				opt = FS_OPTTIME;
26347870Skarels 			else
26447870Skarels 				fatal("%s: bad optimization preference %s",
26547870Skarels 				    optarg, "(options are `space' or `time')");
26647870Skarels 			break;
26747870Skarels 		case 'p':
26847870Skarels 			if ((trackspares = atoi(optarg)) < 0)
26947870Skarels 				fatal("%s: bad spare sectors per track",
27047870Skarels 				    optarg);
27147870Skarels 			break;
27247870Skarels 		case 'r':
27347870Skarels 			if ((rpm = atoi(optarg)) <= 0)
27447870Skarels 				fatal("%s: bad revs/minute\n", optarg);
27547870Skarels 			break;
27647870Skarels 		case 's':
27747870Skarels 			if ((fssize = atoi(optarg)) <= 0)
27847870Skarels 				fatal("%s: bad file system size", optarg);
27947870Skarels 			break;
28047870Skarels 		case 't':
28147870Skarels 			if ((ntracks = atoi(optarg)) <= 0)
28247870Skarels 				fatal("%s: bad total tracks", optarg);
28347870Skarels 			break;
28447870Skarels 		case 'u':
28547870Skarels 			if ((nsectors = atoi(optarg)) <= 0)
28647870Skarels 				fatal("%s: bad sectors/track", optarg);
28747870Skarels 			break;
28847870Skarels 		case 'x':
28947870Skarels 			if ((cylspares = atoi(optarg)) < 0)
29047870Skarels 				fatal("%s: bad spare sectors per cylinder",
29147870Skarels 				    optarg);
29247870Skarels 			break;
29347870Skarels 		case '?':
29447870Skarels 		default:
29547870Skarels 			usage();
29647870Skarels 		}
29747870Skarels 	argc -= optind;
29847870Skarels 	argv += optind;
29943314Smckusick 
300*65711Shibler 	if (argc != 2 && (!mfs || argc != 1))
30147870Skarels 		usage();
30210762Ssam 
30310762Ssam 	special = argv[0];
30414064Smckusick 	cp = rindex(special, '/');
30547870Skarels 	if (cp == 0) {
30647870Skarels 		/*
30747870Skarels 		 * No path prefix; try /dev/r%s then /dev/%s.
30847870Skarels 		 */
30947870Skarels 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
31047870Skarels 		if (stat(device, &st) == -1)
31147870Skarels 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
31247870Skarels 		special = device;
31347870Skarels 	}
31453709Smckusick 	if (Nflag) {
31553709Smckusick 		fso = -1;
31653709Smckusick 	} else {
31730380Smckusick 		fso = open(special, O_WRONLY);
31847870Skarels 		if (fso < 0)
31947870Skarels 			fatal("%s: %s", special, strerror(errno));
32053709Smckusick 
32153709Smckusick 		/* Bail if target special is mounted */
32253709Smckusick 		n = getmntinfo(&mp, MNT_NOWAIT);
32353709Smckusick 		if (n == 0)
32453709Smckusick 			fatal("%s: getmntinfo: %s", special, strerror(errno));
32553709Smckusick 
32653709Smckusick 		len = sizeof(_PATH_DEV) - 1;
32753709Smckusick 		s1 = special;
32853709Smckusick 		if (strncmp(_PATH_DEV, s1, len) == 0)
32953709Smckusick 			s1 += len;
33053709Smckusick 
33153709Smckusick 		while (--n >= 0) {
33253709Smckusick 			s2 = mp->f_mntfromname;
33353709Smckusick 			if (strncmp(_PATH_DEV, s2, len) == 0) {
33453709Smckusick 				s2 += len - 1;
33553709Smckusick 				*s2 = 'r';
33653709Smckusick 			}
33753709Smckusick 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
33853709Smckusick 				fatal("%s is mounted on %s",
33953709Smckusick 				    special, mp->f_mntonname);
34053709Smckusick 			++mp;
34153709Smckusick 		}
34253709Smckusick 	}
34355561Smckusick 	if (mfs && disktype != NULL) {
34455561Smckusick 		lp = (struct disklabel *)getdiskbyname(disktype);
34555561Smckusick 		if (lp == NULL)
34655561Smckusick 			fatal("%s: unknown disk type", disktype);
34755561Smckusick 		pp = &lp->d_partitions[1];
34855561Smckusick 	} else {
34955561Smckusick 		fsi = open(special, O_RDONLY);
35055561Smckusick 		if (fsi < 0)
35155561Smckusick 			fatal("%s: %s", special, strerror(errno));
35255561Smckusick 		if (fstat(fsi, &st) < 0)
35355561Smckusick 			fatal("%s: %s", special, strerror(errno));
35455561Smckusick 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
35555561Smckusick 			printf("%s: %s: not a character-special device\n",
35655561Smckusick 			    progname, special);
35755561Smckusick 		cp = index(argv[0], '\0') - 1;
35855561Smckusick 		if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
35955561Smckusick 			fatal("%s: can't figure out file system partition",
36055561Smckusick 			    argv[0]);
36131680Skarels #ifdef COMPAT
36255561Smckusick 		if (!mfs && disktype == NULL)
36355561Smckusick 			disktype = argv[1];
36443314Smckusick #endif
36555561Smckusick 		lp = getdisklabel(special, fsi);
36655561Smckusick 		if (isdigit(*cp))
36755561Smckusick 			pp = &lp->d_partitions[0];
36855561Smckusick 		else
36955561Smckusick 			pp = &lp->d_partitions[*cp - 'a'];
37055561Smckusick 		if (pp->p_size == 0)
37155561Smckusick 			fatal("%s: `%c' partition is unavailable",
37255561Smckusick 			    argv[0], *cp);
37359679Shibler 		if (pp->p_fstype == FS_BOOT)
37459679Shibler 			fatal("%s: `%c' partition overlaps boot program",
37559679Shibler 			      argv[0], *cp);
37655561Smckusick 	}
37730380Smckusick 	if (fssize == 0)
37810762Ssam 		fssize = pp->p_size;
37939325Smckusick 	if (fssize > pp->p_size && !mfs)
38030380Smckusick 	       fatal("%s: maximum file system size on the `%c' partition is %d",
38130380Smckusick 			argv[0], *cp, pp->p_size);
38230380Smckusick 	if (rpm == 0) {
38330380Smckusick 		rpm = lp->d_rpm;
38430380Smckusick 		if (rpm <= 0)
38530743Skarels 			rpm = 3600;
38610762Ssam 	}
38730380Smckusick 	if (ntracks == 0) {
38830380Smckusick 		ntracks = lp->d_ntracks;
38930380Smckusick 		if (ntracks <= 0)
39030743Skarels 			fatal("%s: no default #tracks", argv[0]);
39130380Smckusick 	}
39210762Ssam 	if (nsectors == 0) {
39330380Smckusick 		nsectors = lp->d_nsectors;
39430380Smckusick 		if (nsectors <= 0)
39530743Skarels 			fatal("%s: no default #sectors/track", argv[0]);
39610762Ssam 	}
39710762Ssam 	if (sectorsize == 0) {
39830380Smckusick 		sectorsize = lp->d_secsize;
39930380Smckusick 		if (sectorsize <= 0)
40030743Skarels 			fatal("%s: no default sector size", argv[0]);
40110762Ssam 	}
40230386Smckusick 	if (trackskew == -1) {
40330386Smckusick 		trackskew = lp->d_trackskew;
40430386Smckusick 		if (trackskew < 0)
40530743Skarels 			trackskew = 0;
40630386Smckusick 	}
40730386Smckusick 	if (interleave == 0) {
40830386Smckusick 		interleave = lp->d_interleave;
40930386Smckusick 		if (interleave <= 0)
41030743Skarels 			interleave = 1;
41130386Smckusick 	}
41210762Ssam 	if (fsize == 0) {
41310762Ssam 		fsize = pp->p_fsize;
41430380Smckusick 		if (fsize <= 0)
41530380Smckusick 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
41610762Ssam 	}
41730380Smckusick 	if (bsize == 0) {
41830380Smckusick 		bsize = pp->p_frag * pp->p_fsize;
41930380Smckusick 		if (bsize <= 0)
42030380Smckusick 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
42111069Ssam 	}
42259750Smckusick 	/*
42359750Smckusick 	 * Maxcontig sets the default for the maximum number of blocks
42459750Smckusick 	 * that may be allocated sequentially. With filesystem clustering
42559750Smckusick 	 * it is possible to allocate contiguous blocks up to the maximum
42659750Smckusick 	 * transfer size permitted by the controller or buffering.
42759750Smckusick 	 */
42859750Smckusick 	if (maxcontig == 0)
42959750Smckusick 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
43039049Smckusick 	if (density == 0)
43139049Smckusick 		density = NFPI * fsize;
43259752Smckusick 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
43330380Smckusick 		fprintf(stderr, "Warning: changing optimization to space ");
43459752Smckusick 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
43524702Smckusick 		opt = FS_OPTSPACE;
43624702Smckusick 	}
43730386Smckusick 	if (trackspares == -1) {
43830386Smckusick 		trackspares = lp->d_sparespertrack;
43930386Smckusick 		if (trackspares < 0)
44030743Skarels 			trackspares = 0;
44130386Smckusick 	}
44230386Smckusick 	nphyssectors = nsectors + trackspares;
44330386Smckusick 	if (cylspares == -1) {
44430386Smckusick 		cylspares = lp->d_sparespercyl;
44530386Smckusick 		if (cylspares < 0)
44630743Skarels 			cylspares = 0;
44730386Smckusick 	}
44830386Smckusick 	secpercyl = nsectors * ntracks - cylspares;
44930380Smckusick 	if (secpercyl != lp->d_secpercyl)
45047870Skarels 		fprintf(stderr, "%s (%d) %s (%lu)\n",
45130380Smckusick 			"Warning: calculated sectors per cylinder", secpercyl,
45230380Smckusick 			"disagrees with disk label", lp->d_secpercyl);
45332321Smckusick 	if (maxbpg == 0)
45432321Smckusick 		maxbpg = MAXBLKPG(bsize);
45530386Smckusick 	headswitch = lp->d_headswitch;
45630386Smckusick 	trackseek = lp->d_trkseek;
45747870Skarels #ifdef notdef /* label may be 0 if faked up by kernel */
45830691Skarels 	bbsize = lp->d_bbsize;
45930691Skarels 	sbsize = lp->d_sbsize;
46045836Skarels #endif
46130380Smckusick 	oldpartition = *pp;
46230691Skarels #ifdef tahoe
46330691Skarels 	realsectorsize = sectorsize;
46430862Skarels 	if (sectorsize != DEV_BSIZE) {		/* XXX */
46530691Skarels 		int secperblk = DEV_BSIZE / sectorsize;
46630691Skarels 
46730691Skarels 		sectorsize = DEV_BSIZE;
46830691Skarels 		nsectors /= secperblk;
46930691Skarels 		nphyssectors /= secperblk;
47030691Skarels 		secpercyl /= secperblk;
47130691Skarels 		fssize /= secperblk;
47230691Skarels 		pp->p_size /= secperblk;
47330691Skarels 	}
47430691Skarels #endif
47530380Smckusick 	mkfs(pp, special, fsi, fso);
47630691Skarels #ifdef tahoe
47730691Skarels 	if (realsectorsize != DEV_BSIZE)
47830691Skarels 		pp->p_size *= DEV_BSIZE / realsectorsize;
47930691Skarels #endif
48030380Smckusick 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
48130380Smckusick 		rewritelabel(special, fso, lp);
48239049Smckusick 	if (!Nflag)
48339049Smckusick 		close(fso);
48439049Smckusick 	close(fsi);
48549063Sbostic #ifdef MFS
48639325Smckusick 	if (mfs) {
48749063Sbostic 		struct mfs_args args;
48849063Sbostic 
48939325Smckusick 		sprintf(buf, "mfs:%d", getpid());
490*65711Shibler 		args.fspec = buf;
491*65711Shibler 		args.export.ex_root = -2;
492*65711Shibler 		if (mntflags & MNT_RDONLY)
493*65711Shibler 			args.export.ex_flags = MNT_EXRDONLY;
494*65711Shibler 		else
495*65711Shibler 			args.export.ex_flags = 0;
49639049Smckusick 		args.base = membase;
49739049Smckusick 		args.size = fssize * sectorsize;
49847870Skarels 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
49947870Skarels 			fatal("%s: %s", argv[1], strerror(errno));
50039049Smckusick 	}
50149063Sbostic #endif
50230380Smckusick 	exit(0);
50330380Smckusick }
50430380Smckusick 
50531680Skarels #ifdef COMPAT
50643314Smckusick char lmsg[] = "%s: can't read disk label; disk type must be specified";
50743314Smckusick #else
50843314Smckusick char lmsg[] = "%s: can't read disk label";
50943314Smckusick #endif
51031680Skarels 
51131680Skarels struct disklabel *
51230380Smckusick getdisklabel(s, fd)
51330380Smckusick 	char *s;
51431680Skarels 	int fd;
51530380Smckusick {
51630380Smckusick 	static struct disklabel lab;
51730380Smckusick 
51830380Smckusick 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
51943314Smckusick #ifdef COMPAT
52043314Smckusick 		if (disktype) {
52147870Skarels 			struct disklabel *lp, *getdiskbyname();
52243314Smckusick 
52347870Skarels 			unlabeled++;
52447870Skarels 			lp = getdiskbyname(disktype);
52547870Skarels 			if (lp == NULL)
52647870Skarels 				fatal("%s: unknown disk type", disktype);
52747870Skarels 			return (lp);
52843314Smckusick 		}
52943314Smckusick #endif
53047870Skarels 		(void)fprintf(stderr,
53147870Skarels 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
53243314Smckusick 		fatal(lmsg, s);
53310762Ssam 	}
53430380Smckusick 	return (&lab);
53530380Smckusick }
53610762Ssam 
53730380Smckusick rewritelabel(s, fd, lp)
53830380Smckusick 	char *s;
53930380Smckusick 	int fd;
54030380Smckusick 	register struct disklabel *lp;
54130380Smckusick {
54231680Skarels #ifdef COMPAT
54347870Skarels 	if (unlabeled)
54431680Skarels 		return;
54531680Skarels #endif
54630380Smckusick 	lp->d_checksum = 0;
54730380Smckusick 	lp->d_checksum = dkcksum(lp);
54830380Smckusick 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
54947870Skarels 		(void)fprintf(stderr,
55047870Skarels 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
55130380Smckusick 		fatal("%s: can't rewrite disk label", s);
55210762Ssam 	}
55330446Skarels #if vax
55430446Skarels 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
55530446Skarels 		register i;
55630691Skarels 		int cfd;
55730446Skarels 		daddr_t alt;
55830691Skarels 		char specname[64];
55930691Skarels 		char blk[1024];
56031045Ssam 		char *cp;
56130446Skarels 
56230691Skarels 		/*
56330691Skarels 		 * Make name for 'c' partition.
56430691Skarels 		 */
56530691Skarels 		strcpy(specname, s);
56630691Skarels 		cp = specname + strlen(specname) - 1;
56730691Skarels 		if (!isdigit(*cp))
56830691Skarels 			*cp = 'c';
56930691Skarels 		cfd = open(specname, O_WRONLY);
57047870Skarels 		if (cfd < 0)
57147870Skarels 			fatal("%s: %s", specname, strerror(errno));
57230691Skarels 		bzero(blk, sizeof(blk));
57330691Skarels 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
57430446Skarels 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
57530446Skarels 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
57647870Skarels 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
57747870Skarels 			    L_SET) == -1)
57847870Skarels 				fatal("lseek to badsector area: %s",
57947870Skarels 				    strerror(errno));
58047870Skarels 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
58147870Skarels 				fprintf(stderr,
58247870Skarels 				    "%s: alternate label %d write: %s\n",
58347870Skarels 				    progname, i/2, strerror(errno));
58430380Smckusick 		}
58539049Smckusick 		close(cfd);
58630380Smckusick 	}
58730446Skarels #endif
58810762Ssam }
58910762Ssam 
59010762Ssam /*VARARGS*/
59150787Storek void
59250787Storek #if __STDC__
59350787Storek fatal(const char *fmt, ...)
59450787Storek #else
59550787Storek fatal(fmt, va_alist)
59610762Ssam 	char *fmt;
59750787Storek 	va_dcl
59850787Storek #endif
59910762Ssam {
60047870Skarels 	va_list ap;
60110762Ssam 
60250787Storek #if __STDC__
60350787Storek 	va_start(ap, fmt);
60450787Storek #else
60550787Storek 	va_start(ap);
60650787Storek #endif
60739049Smckusick 	fprintf(stderr, "%s: ", progname);
60847870Skarels 	(void)vfprintf(stderr, fmt, ap);
60947870Skarels 	va_end(ap);
61010762Ssam 	putc('\n', stderr);
61147870Skarels 	exit(1);
61210762Ssam }
61347870Skarels 
61447870Skarels usage()
61547870Skarels {
61647870Skarels 	if (mfs) {
61747870Skarels 		fprintf(stderr,
61853709Smckusick 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
61953709Smckusick 			progname);
62047870Skarels 	} else
62147870Skarels 		fprintf(stderr,
62253709Smckusick 		    "usage: %s [ -fsoptions ] special-device%s\n",
62353709Smckusick 		    progname,
62447870Skarels #ifdef COMPAT
62547870Skarels 		    " [device-type]");
62647870Skarels #else
62747870Skarels 		    "");
62847870Skarels #endif
62947870Skarels 	fprintf(stderr, "where fsoptions are:\n");
63047870Skarels 	fprintf(stderr,
63147870Skarels 	    "\t-N do not create file system, just print out parameters\n");
63259752Smckusick 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
63347870Skarels 	fprintf(stderr, "\t-S sector size\n");
63447870Skarels #ifdef COMPAT
63547870Skarels 	fprintf(stderr, "\t-T disktype\n");
63647870Skarels #endif
63747870Skarels 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
63847870Skarels 	fprintf(stderr, "\t-b block size\n");
63947870Skarels 	fprintf(stderr, "\t-c cylinders/group\n");
64047870Skarels 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
64147870Skarels 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
64247870Skarels 	fprintf(stderr, "\t-f frag size\n");
64347870Skarels 	fprintf(stderr, "\t-i number of bytes per inode\n");
64447870Skarels 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
64547870Skarels 	fprintf(stderr, "\t-l hardware sector interleave\n");
64647870Skarels 	fprintf(stderr, "\t-m minimum free space %%\n");
64747870Skarels 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
64847870Skarels 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
64947870Skarels 	fprintf(stderr, "\t-p spare sectors per track\n");
65047870Skarels 	fprintf(stderr, "\t-s file system size (sectors)\n");
65147870Skarels 	fprintf(stderr, "\t-r revolutions/minute\n");
65247870Skarels 	fprintf(stderr, "\t-t tracks/cylinder\n");
65347870Skarels 	fprintf(stderr, "\t-u sectors/track\n");
65447870Skarels 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
65547870Skarels 	exit(1);
65647870Skarels }
657