xref: /csrg-svn/sbin/newfs/newfs.c (revision 66597)
121159Sdist /*
266488Sbostic  * Copyright (c) 1983, 1989, 1993, 1994
361523Sbostic  *	The Regents of the University of California.  All rights reserved.
439049Smckusick  *
542704Sbostic  * %sccs.include.redist.c%
621159Sdist  */
721159Sdist 
810762Ssam #ifndef lint
9*66597Shibler static char sccsid[] = "@(#)newfs.c	8.7 (Berkeley) 04/01/94";
1039049Smckusick #endif /* not lint */
1139049Smckusick 
1239049Smckusick #ifndef lint
1361523Sbostic static char copyright[] =
1466488Sbostic "@(#) Copyright (c) 1983, 1989, 1993, 1994\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>
2766488Sbostic 
2851533Sbostic #include <ufs/ufs/dir.h>
2951533Sbostic #include <ufs/ffs/fs.h>
3010762Ssam 
3166488Sbostic #include <ctype.h>
3247870Skarels #include <errno.h>
3366488Sbostic #include <paths.h>
3466488Sbostic #include <stdio.h>
3566488Sbostic #include <stdlib.h>
3666488Sbostic #include <string.h>
3766488Sbostic #include <syslog.h>
3866488Sbostic #include <unistd.h>
3966488Sbostic 
4050787Storek #if __STDC__
4147870Skarels #include <stdarg.h>
4250787Storek #else
4350787Storek #include <varargs.h>
4450787Storek #endif
4510762Ssam 
4666488Sbostic #include "mntopts.h"
4766488Sbostic 
4866488Sbostic struct mntopt mopts[] = {
4966488Sbostic 	MOPT_STDOPTS,
50*66597Shibler 	MOPT_ASYNC,
5166488Sbostic 	{ NULL },
5266488Sbostic };
5366488Sbostic 
5450787Storek #if __STDC__
5550787Storek void	fatal(const char *fmt, ...);
5650787Storek #else
5750787Storek void	fatal();
5850787Storek #endif
5950787Storek 
6047870Skarels #define	COMPAT			/* allow non-labeled disks */
6131680Skarels 
6230380Smckusick /*
6330380Smckusick  * The following two constants set the default block and fragment sizes.
6430380Smckusick  * Both constants must be a power of 2 and meet the following constraints:
6530380Smckusick  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
6630380Smckusick  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
6730380Smckusick  *	DESBLKSIZE / DESFRAGSIZE <= 8
6830380Smckusick  */
6930380Smckusick #define	DFL_FRAGSIZE	1024
7030380Smckusick #define	DFL_BLKSIZE	8192
7111183Ssam 
7230380Smckusick /*
7334137Smckusick  * Cylinder groups may have up to many cylinders. The actual
7430380Smckusick  * number used depends upon how much information can be stored
7534137Smckusick  * on a single cylinder. The default is to use 16 cylinders
7630380Smckusick  * per group.
7730380Smckusick  */
7830380Smckusick #define	DESCPG		16	/* desired fs_cpg */
7930380Smckusick 
8030380Smckusick /*
8130380Smckusick  * MINFREE gives the minimum acceptable percentage of file system
8230380Smckusick  * blocks which may be free. If the freelist drops below this level
8330380Smckusick  * only the superuser may continue to allocate blocks. This may
8430380Smckusick  * be set to 0 if no reserve of free blocks is deemed necessary,
8530380Smckusick  * however throughput drops by fifty percent if the file system
8659750Smckusick  * is run at between 95% and 100% full; thus the default value of
8759750Smckusick  * fs_minfree is 5%. With 5% free space, fragmentation is not a
8830380Smckusick  * problem, so we choose to optimize for time.
8930380Smckusick  */
9059750Smckusick #define MINFREE		5
9130380Smckusick #define DEFAULTOPT	FS_OPTTIME
9230380Smckusick 
9330380Smckusick /*
9430380Smckusick  * ROTDELAY gives the minimum number of milliseconds to initiate
9530380Smckusick  * another disk transfer on the same cylinder. It is used in
9630380Smckusick  * determining the rotationally optimal layout for disk blocks
9730380Smckusick  * within a file; the default of fs_rotdelay is 4ms.
9830380Smckusick  */
9930380Smckusick #define ROTDELAY	4
10030380Smckusick 
10130380Smckusick /*
10232309Smckusick  * MAXBLKPG determines the maximum number of data blocks which are
10332309Smckusick  * placed in a single cylinder group. The default is one indirect
10432309Smckusick  * block worth of data blocks.
10532309Smckusick  */
10632309Smckusick #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
10732309Smckusick 
10832309Smckusick /*
10930380Smckusick  * Each file system has a number of inodes statically allocated.
11039049Smckusick  * We allocate one inode slot per NFPI fragments, expecting this
11130380Smckusick  * to be far more than we will ever need.
11230380Smckusick  */
11339049Smckusick #define	NFPI		4
11430380Smckusick 
11534137Smckusick /*
11634137Smckusick  * For each cylinder we keep track of the availability of blocks at different
11734137Smckusick  * rotational positions, so that we can lay out the data to be picked
11834137Smckusick  * up with minimum rotational latency.  NRPOS is the default number of
11934137Smckusick  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
12034137Smckusick  * of our summary information is 2ms for a typical 3600 rpm drive.
12134137Smckusick  */
12234137Smckusick #define	NRPOS		8	/* number distinct rotational positions */
12334137Smckusick 
12434137Smckusick 
12539325Smckusick int	mfs;			/* run as the memory based filesystem */
12630380Smckusick int	Nflag;			/* run without writing file system */
12759750Smckusick int	Oflag;			/* format as an 4.3BSD file system */
12810762Ssam int	fssize;			/* file system size */
12910762Ssam int	ntracks;		/* # tracks/cylinder */
13010762Ssam int	nsectors;		/* # sectors/track */
13130386Smckusick int	nphyssectors;		/* # sectors/track including spares */
13230380Smckusick int	secpercyl;		/* sectors per cylinder */
13330386Smckusick int	trackspares = -1;	/* spare sectors per track */
13430386Smckusick int	cylspares = -1;		/* spare sectors per cylinder */
13510762Ssam int	sectorsize;		/* bytes/sector */
13630691Skarels #ifdef tahoe
13730691Skarels int	realsectorsize;		/* bytes/sector in hardware */
13830691Skarels #endif
13911069Ssam int	rpm;			/* revolutions/minute of drive */
14030386Smckusick int	interleave;		/* hardware sector interleave */
14130386Smckusick int	trackskew = -1;		/* sector 0 skew, per track */
14230386Smckusick int	headswitch;		/* head switch time, usec */
14330386Smckusick int	trackseek;		/* track-to-track seek, usec */
14430862Skarels int	fsize = 0;		/* fragment size */
14530862Skarels int	bsize = 0;		/* block size */
14630380Smckusick int	cpg = DESCPG;		/* cylinders/cylinder group */
14732119Smckusick int	cpgflg;			/* cylinders/cylinder group flag was given */
14830380Smckusick int	minfree = MINFREE;	/* free space threshold */
14930380Smckusick int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
15039049Smckusick int	density;		/* number of bytes per inode */
15159750Smckusick int	maxcontig = 0;		/* max contiguous blocks to allocate */
15230380Smckusick int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
15332309Smckusick int	maxbpg;			/* maximum blocks per file in a cyl group */
15434137Smckusick int	nrpos = NRPOS;		/* # of distinguished rotational positions */
15530691Skarels int	bbsize = BBSIZE;	/* boot block size */
15630691Skarels int	sbsize = SBSIZE;	/* superblock size */
157*66597Shibler int	mntflags = MNT_ASYNC;	/* flags to be passed to mount */
15839049Smckusick u_long	memleft;		/* virtual memory available */
15939049Smckusick caddr_t	membase;		/* start address of memory based filesystem */
16031680Skarels #ifdef COMPAT
16143314Smckusick char	*disktype;
16247870Skarels int	unlabeled;
16331680Skarels #endif
16410762Ssam 
16510762Ssam char	device[MAXPATHLEN];
16639049Smckusick char	*progname;
16710762Ssam 
16866488Sbostic int
16910762Ssam main(argc, argv)
17014064Smckusick 	int argc;
17110762Ssam 	char *argv[];
17210762Ssam {
17347870Skarels 	extern char *optarg;
17447870Skarels 	extern int optind;
17547870Skarels 	register int ch;
17610762Ssam 	register struct partition *pp;
17730380Smckusick 	register struct disklabel *lp;
17830380Smckusick 	struct disklabel *getdisklabel();
17930380Smckusick 	struct partition oldpartition;
18010762Ssam 	struct stat st;
18153709Smckusick 	struct statfs *mp;
18266488Sbostic 	int fsi, fso, len, n;
18366488Sbostic 	char *cp, *s1, *s2, *special, *opstring, buf[BUFSIZ];
18410762Ssam 
18547870Skarels 	if (progname = rindex(*argv, '/'))
18647870Skarels 		++progname;
18747870Skarels 	else
18839049Smckusick 		progname = *argv;
18947870Skarels 
19050402Smckusick 	if (strstr(progname, "mfs")) {
19147870Skarels 		mfs = 1;
19239049Smckusick 		Nflag++;
19339049Smckusick 	}
19410762Ssam 
19566488Sbostic 	opstring = mfs ?
196*66597Shibler 	    "NT:a:b:c:d:e:f:i:m:o:s:" :
19766488Sbostic 	    "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
19847870Skarels 	while ((ch = getopt(argc, argv, opstring)) != EOF)
19966488Sbostic 		switch (ch) {
20047870Skarels 		case 'N':
20166488Sbostic 			Nflag = 1;
20247870Skarels 			break;
20359750Smckusick 		case 'O':
20466488Sbostic 			Oflag = 1;
20559750Smckusick 			break;
20647870Skarels 		case 'S':
20747870Skarels 			if ((sectorsize = atoi(optarg)) <= 0)
20847870Skarels 				fatal("%s: bad sector size", optarg);
20947870Skarels 			break;
21043314Smckusick #ifdef COMPAT
21147870Skarels 		case 'T':
21247870Skarels 			disktype = optarg;
21347870Skarels 			break;
21443314Smckusick #endif
21547870Skarels 		case 'a':
21647870Skarels 			if ((maxcontig = atoi(optarg)) <= 0)
21766488Sbostic 				fatal("%s: bad maximum contiguous blocks\n",
21847870Skarels 				    optarg);
21947870Skarels 			break;
22047870Skarels 		case 'b':
22147870Skarels 			if ((bsize = atoi(optarg)) < MINBSIZE)
22247870Skarels 				fatal("%s: bad block size", optarg);
22347870Skarels 			break;
22447870Skarels 		case 'c':
22547870Skarels 			if ((cpg = atoi(optarg)) <= 0)
22647870Skarels 				fatal("%s: bad cylinders/group", optarg);
22747870Skarels 			cpgflg++;
22847870Skarels 			break;
22947870Skarels 		case 'd':
23047870Skarels 			if ((rotdelay = atoi(optarg)) < 0)
23147870Skarels 				fatal("%s: bad rotational delay\n", optarg);
23247870Skarels 			break;
23347870Skarels 		case 'e':
23447870Skarels 			if ((maxbpg = atoi(optarg)) <= 0)
23566488Sbostic 		fatal("%s: bad blocks per file in a cylinder group\n",
23647870Skarels 				    optarg);
23747870Skarels 			break;
23847870Skarels 		case 'f':
23947870Skarels 			if ((fsize = atoi(optarg)) <= 0)
24066488Sbostic 				fatal("%s: bad fragment size", optarg);
24147870Skarels 			break;
24247870Skarels 		case 'i':
24347870Skarels 			if ((density = atoi(optarg)) <= 0)
24447870Skarels 				fatal("%s: bad bytes per inode\n", optarg);
24547870Skarels 			break;
24647870Skarels 		case 'k':
24747870Skarels 			if ((trackskew = atoi(optarg)) < 0)
24847870Skarels 				fatal("%s: bad track skew", optarg);
24947870Skarels 			break;
25047870Skarels 		case 'l':
25147870Skarels 			if ((interleave = atoi(optarg)) <= 0)
25247870Skarels 				fatal("%s: bad interleave", optarg);
25347870Skarels 			break;
25447870Skarels 		case 'm':
25547870Skarels 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
25647870Skarels 				fatal("%s: bad free space %%\n", optarg);
25747870Skarels 			break;
25847870Skarels 		case 'n':
25947870Skarels 			if ((nrpos = atoi(optarg)) <= 0)
26047870Skarels 				fatal("%s: bad rotational layout count\n",
26147870Skarels 				    optarg);
26247870Skarels 			break;
26347870Skarels 		case 'o':
26466488Sbostic 			if (mfs)
26566488Sbostic 				getmntopts(optarg, mopts, &mntflags);
26666488Sbostic 			else {
26766488Sbostic 				if (strcmp(optarg, "space") == 0)
26866488Sbostic 					opt = FS_OPTSPACE;
26966488Sbostic 				else if (strcmp(optarg, "time") == 0)
27066488Sbostic 					opt = FS_OPTTIME;
27166488Sbostic 				else
27266488Sbostic 	fatal("%s: unknown optimization preference: use `space' or `time'.");
27366488Sbostic 			}
27447870Skarels 			break;
27547870Skarels 		case 'p':
27647870Skarels 			if ((trackspares = atoi(optarg)) < 0)
27747870Skarels 				fatal("%s: bad spare sectors per track",
27847870Skarels 				    optarg);
27947870Skarels 			break;
28047870Skarels 		case 'r':
28147870Skarels 			if ((rpm = atoi(optarg)) <= 0)
28266488Sbostic 				fatal("%s: bad revolutions/minute\n", optarg);
28347870Skarels 			break;
28447870Skarels 		case 's':
28547870Skarels 			if ((fssize = atoi(optarg)) <= 0)
28647870Skarels 				fatal("%s: bad file system size", optarg);
28747870Skarels 			break;
28847870Skarels 		case 't':
28947870Skarels 			if ((ntracks = atoi(optarg)) <= 0)
29047870Skarels 				fatal("%s: bad total tracks", optarg);
29147870Skarels 			break;
29247870Skarels 		case 'u':
29347870Skarels 			if ((nsectors = atoi(optarg)) <= 0)
29447870Skarels 				fatal("%s: bad sectors/track", optarg);
29547870Skarels 			break;
29647870Skarels 		case 'x':
29747870Skarels 			if ((cylspares = atoi(optarg)) < 0)
29847870Skarels 				fatal("%s: bad spare sectors per cylinder",
29947870Skarels 				    optarg);
30047870Skarels 			break;
30147870Skarels 		case '?':
30247870Skarels 		default:
30347870Skarels 			usage();
30447870Skarels 		}
30547870Skarels 	argc -= optind;
30647870Skarels 	argv += optind;
30743314Smckusick 
30865901Shibler 	if (argc != 2 && (mfs || argc != 1))
30947870Skarels 		usage();
31010762Ssam 
31110762Ssam 	special = argv[0];
31214064Smckusick 	cp = rindex(special, '/');
31347870Skarels 	if (cp == 0) {
31447870Skarels 		/*
31547870Skarels 		 * No path prefix; try /dev/r%s then /dev/%s.
31647870Skarels 		 */
31747870Skarels 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
31847870Skarels 		if (stat(device, &st) == -1)
31947870Skarels 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
32047870Skarels 		special = device;
32147870Skarels 	}
32253709Smckusick 	if (Nflag) {
32353709Smckusick 		fso = -1;
32453709Smckusick 	} else {
32530380Smckusick 		fso = open(special, O_WRONLY);
32647870Skarels 		if (fso < 0)
32747870Skarels 			fatal("%s: %s", special, strerror(errno));
32853709Smckusick 
32953709Smckusick 		/* Bail if target special is mounted */
33053709Smckusick 		n = getmntinfo(&mp, MNT_NOWAIT);
33153709Smckusick 		if (n == 0)
33253709Smckusick 			fatal("%s: getmntinfo: %s", special, strerror(errno));
33353709Smckusick 
33453709Smckusick 		len = sizeof(_PATH_DEV) - 1;
33553709Smckusick 		s1 = special;
33653709Smckusick 		if (strncmp(_PATH_DEV, s1, len) == 0)
33753709Smckusick 			s1 += len;
33853709Smckusick 
33953709Smckusick 		while (--n >= 0) {
34053709Smckusick 			s2 = mp->f_mntfromname;
34153709Smckusick 			if (strncmp(_PATH_DEV, s2, len) == 0) {
34253709Smckusick 				s2 += len - 1;
34353709Smckusick 				*s2 = 'r';
34453709Smckusick 			}
34553709Smckusick 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
34653709Smckusick 				fatal("%s is mounted on %s",
34753709Smckusick 				    special, mp->f_mntonname);
34853709Smckusick 			++mp;
34953709Smckusick 		}
35053709Smckusick 	}
35155561Smckusick 	if (mfs && disktype != NULL) {
35255561Smckusick 		lp = (struct disklabel *)getdiskbyname(disktype);
35355561Smckusick 		if (lp == NULL)
35455561Smckusick 			fatal("%s: unknown disk type", disktype);
35555561Smckusick 		pp = &lp->d_partitions[1];
35655561Smckusick 	} else {
35755561Smckusick 		fsi = open(special, O_RDONLY);
35855561Smckusick 		if (fsi < 0)
35955561Smckusick 			fatal("%s: %s", special, strerror(errno));
36055561Smckusick 		if (fstat(fsi, &st) < 0)
36155561Smckusick 			fatal("%s: %s", special, strerror(errno));
36255561Smckusick 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
36355561Smckusick 			printf("%s: %s: not a character-special device\n",
36455561Smckusick 			    progname, special);
36555561Smckusick 		cp = index(argv[0], '\0') - 1;
36655561Smckusick 		if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
36755561Smckusick 			fatal("%s: can't figure out file system partition",
36855561Smckusick 			    argv[0]);
36931680Skarels #ifdef COMPAT
37055561Smckusick 		if (!mfs && disktype == NULL)
37155561Smckusick 			disktype = argv[1];
37243314Smckusick #endif
37355561Smckusick 		lp = getdisklabel(special, fsi);
37455561Smckusick 		if (isdigit(*cp))
37555561Smckusick 			pp = &lp->d_partitions[0];
37655561Smckusick 		else
37755561Smckusick 			pp = &lp->d_partitions[*cp - 'a'];
37855561Smckusick 		if (pp->p_size == 0)
37955561Smckusick 			fatal("%s: `%c' partition is unavailable",
38055561Smckusick 			    argv[0], *cp);
38159679Shibler 		if (pp->p_fstype == FS_BOOT)
38259679Shibler 			fatal("%s: `%c' partition overlaps boot program",
38359679Shibler 			      argv[0], *cp);
38455561Smckusick 	}
38530380Smckusick 	if (fssize == 0)
38610762Ssam 		fssize = pp->p_size;
38739325Smckusick 	if (fssize > pp->p_size && !mfs)
38830380Smckusick 	       fatal("%s: maximum file system size on the `%c' partition is %d",
38930380Smckusick 			argv[0], *cp, pp->p_size);
39030380Smckusick 	if (rpm == 0) {
39130380Smckusick 		rpm = lp->d_rpm;
39230380Smckusick 		if (rpm <= 0)
39330743Skarels 			rpm = 3600;
39410762Ssam 	}
39530380Smckusick 	if (ntracks == 0) {
39630380Smckusick 		ntracks = lp->d_ntracks;
39730380Smckusick 		if (ntracks <= 0)
39830743Skarels 			fatal("%s: no default #tracks", argv[0]);
39930380Smckusick 	}
40010762Ssam 	if (nsectors == 0) {
40130380Smckusick 		nsectors = lp->d_nsectors;
40230380Smckusick 		if (nsectors <= 0)
40330743Skarels 			fatal("%s: no default #sectors/track", argv[0]);
40410762Ssam 	}
40510762Ssam 	if (sectorsize == 0) {
40630380Smckusick 		sectorsize = lp->d_secsize;
40730380Smckusick 		if (sectorsize <= 0)
40830743Skarels 			fatal("%s: no default sector size", argv[0]);
40910762Ssam 	}
41030386Smckusick 	if (trackskew == -1) {
41130386Smckusick 		trackskew = lp->d_trackskew;
41230386Smckusick 		if (trackskew < 0)
41330743Skarels 			trackskew = 0;
41430386Smckusick 	}
41530386Smckusick 	if (interleave == 0) {
41630386Smckusick 		interleave = lp->d_interleave;
41730386Smckusick 		if (interleave <= 0)
41830743Skarels 			interleave = 1;
41930386Smckusick 	}
42010762Ssam 	if (fsize == 0) {
42110762Ssam 		fsize = pp->p_fsize;
42230380Smckusick 		if (fsize <= 0)
42330380Smckusick 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
42410762Ssam 	}
42530380Smckusick 	if (bsize == 0) {
42630380Smckusick 		bsize = pp->p_frag * pp->p_fsize;
42730380Smckusick 		if (bsize <= 0)
42830380Smckusick 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
42911069Ssam 	}
43059750Smckusick 	/*
43159750Smckusick 	 * Maxcontig sets the default for the maximum number of blocks
43259750Smckusick 	 * that may be allocated sequentially. With filesystem clustering
43359750Smckusick 	 * it is possible to allocate contiguous blocks up to the maximum
43459750Smckusick 	 * transfer size permitted by the controller or buffering.
43559750Smckusick 	 */
43659750Smckusick 	if (maxcontig == 0)
43766000Smckusick 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize - 1);
43839049Smckusick 	if (density == 0)
43939049Smckusick 		density = NFPI * fsize;
44059752Smckusick 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
44130380Smckusick 		fprintf(stderr, "Warning: changing optimization to space ");
44259752Smckusick 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
44324702Smckusick 		opt = FS_OPTSPACE;
44424702Smckusick 	}
44530386Smckusick 	if (trackspares == -1) {
44630386Smckusick 		trackspares = lp->d_sparespertrack;
44730386Smckusick 		if (trackspares < 0)
44830743Skarels 			trackspares = 0;
44930386Smckusick 	}
45030386Smckusick 	nphyssectors = nsectors + trackspares;
45130386Smckusick 	if (cylspares == -1) {
45230386Smckusick 		cylspares = lp->d_sparespercyl;
45330386Smckusick 		if (cylspares < 0)
45430743Skarels 			cylspares = 0;
45530386Smckusick 	}
45630386Smckusick 	secpercyl = nsectors * ntracks - cylspares;
45730380Smckusick 	if (secpercyl != lp->d_secpercyl)
45847870Skarels 		fprintf(stderr, "%s (%d) %s (%lu)\n",
45930380Smckusick 			"Warning: calculated sectors per cylinder", secpercyl,
46030380Smckusick 			"disagrees with disk label", lp->d_secpercyl);
46132321Smckusick 	if (maxbpg == 0)
46232321Smckusick 		maxbpg = MAXBLKPG(bsize);
46330386Smckusick 	headswitch = lp->d_headswitch;
46430386Smckusick 	trackseek = lp->d_trkseek;
46547870Skarels #ifdef notdef /* label may be 0 if faked up by kernel */
46630691Skarels 	bbsize = lp->d_bbsize;
46730691Skarels 	sbsize = lp->d_sbsize;
46845836Skarels #endif
46930380Smckusick 	oldpartition = *pp;
47030691Skarels #ifdef tahoe
47130691Skarels 	realsectorsize = sectorsize;
47230862Skarels 	if (sectorsize != DEV_BSIZE) {		/* XXX */
47330691Skarels 		int secperblk = DEV_BSIZE / sectorsize;
47430691Skarels 
47530691Skarels 		sectorsize = DEV_BSIZE;
47630691Skarels 		nsectors /= secperblk;
47730691Skarels 		nphyssectors /= secperblk;
47830691Skarels 		secpercyl /= secperblk;
47930691Skarels 		fssize /= secperblk;
48030691Skarels 		pp->p_size /= secperblk;
48130691Skarels 	}
48230691Skarels #endif
48330380Smckusick 	mkfs(pp, special, fsi, fso);
48430691Skarels #ifdef tahoe
48530691Skarels 	if (realsectorsize != DEV_BSIZE)
48630691Skarels 		pp->p_size *= DEV_BSIZE / realsectorsize;
48730691Skarels #endif
48830380Smckusick 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
48930380Smckusick 		rewritelabel(special, fso, lp);
49039049Smckusick 	if (!Nflag)
49139049Smckusick 		close(fso);
49239049Smckusick 	close(fsi);
49349063Sbostic #ifdef MFS
49439325Smckusick 	if (mfs) {
49549063Sbostic 		struct mfs_args args;
49649063Sbostic 
49739325Smckusick 		sprintf(buf, "mfs:%d", getpid());
49865711Shibler 		args.fspec = buf;
49965711Shibler 		args.export.ex_root = -2;
50065711Shibler 		if (mntflags & MNT_RDONLY)
50165711Shibler 			args.export.ex_flags = MNT_EXRDONLY;
50265711Shibler 		else
50365711Shibler 			args.export.ex_flags = 0;
50439049Smckusick 		args.base = membase;
50539049Smckusick 		args.size = fssize * sectorsize;
50647870Skarels 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
50747870Skarels 			fatal("%s: %s", argv[1], strerror(errno));
50839049Smckusick 	}
50949063Sbostic #endif
51030380Smckusick 	exit(0);
51130380Smckusick }
51230380Smckusick 
51331680Skarels #ifdef COMPAT
51443314Smckusick char lmsg[] = "%s: can't read disk label; disk type must be specified";
51543314Smckusick #else
51643314Smckusick char lmsg[] = "%s: can't read disk label";
51743314Smckusick #endif
51831680Skarels 
51931680Skarels struct disklabel *
52030380Smckusick getdisklabel(s, fd)
52130380Smckusick 	char *s;
52231680Skarels 	int fd;
52330380Smckusick {
52430380Smckusick 	static struct disklabel lab;
52530380Smckusick 
52630380Smckusick 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
52743314Smckusick #ifdef COMPAT
52843314Smckusick 		if (disktype) {
52947870Skarels 			struct disklabel *lp, *getdiskbyname();
53043314Smckusick 
53147870Skarels 			unlabeled++;
53247870Skarels 			lp = getdiskbyname(disktype);
53347870Skarels 			if (lp == NULL)
53447870Skarels 				fatal("%s: unknown disk type", disktype);
53547870Skarels 			return (lp);
53643314Smckusick 		}
53743314Smckusick #endif
53865928Spendry 		warn("ioctl (GDINFO)");
53943314Smckusick 		fatal(lmsg, s);
54010762Ssam 	}
54130380Smckusick 	return (&lab);
54230380Smckusick }
54310762Ssam 
54430380Smckusick rewritelabel(s, fd, lp)
54530380Smckusick 	char *s;
54630380Smckusick 	int fd;
54730380Smckusick 	register struct disklabel *lp;
54830380Smckusick {
54931680Skarels #ifdef COMPAT
55047870Skarels 	if (unlabeled)
55131680Skarels 		return;
55231680Skarels #endif
55330380Smckusick 	lp->d_checksum = 0;
55430380Smckusick 	lp->d_checksum = dkcksum(lp);
55530380Smckusick 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
55665928Spendry 		warn("ioctl (WDINFO)");
55730380Smckusick 		fatal("%s: can't rewrite disk label", s);
55810762Ssam 	}
55930446Skarels #if vax
56030446Skarels 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
56130446Skarels 		register i;
56230691Skarels 		int cfd;
56330446Skarels 		daddr_t alt;
56430691Skarels 		char specname[64];
56530691Skarels 		char blk[1024];
56631045Ssam 		char *cp;
56730446Skarels 
56830691Skarels 		/*
56930691Skarels 		 * Make name for 'c' partition.
57030691Skarels 		 */
57130691Skarels 		strcpy(specname, s);
57230691Skarels 		cp = specname + strlen(specname) - 1;
57330691Skarels 		if (!isdigit(*cp))
57430691Skarels 			*cp = 'c';
57530691Skarels 		cfd = open(specname, O_WRONLY);
57647870Skarels 		if (cfd < 0)
57747870Skarels 			fatal("%s: %s", specname, strerror(errno));
57830691Skarels 		bzero(blk, sizeof(blk));
57930691Skarels 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
58030446Skarels 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
58130446Skarels 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
58247870Skarels 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
58347870Skarels 			    L_SET) == -1)
58447870Skarels 				fatal("lseek to badsector area: %s",
58547870Skarels 				    strerror(errno));
58647870Skarels 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
58765928Spendry 				warn("alternate label %d write", i/2);
58830380Smckusick 		}
58939049Smckusick 		close(cfd);
59030380Smckusick 	}
59130446Skarels #endif
59210762Ssam }
59310762Ssam 
59410762Ssam /*VARARGS*/
59550787Storek void
59650787Storek #if __STDC__
59750787Storek fatal(const char *fmt, ...)
59850787Storek #else
59950787Storek fatal(fmt, va_alist)
60010762Ssam 	char *fmt;
60150787Storek 	va_dcl
60250787Storek #endif
60310762Ssam {
60447870Skarels 	va_list ap;
60510762Ssam 
60650787Storek #if __STDC__
60750787Storek 	va_start(ap, fmt);
60850787Storek #else
60950787Storek 	va_start(ap);
61050787Storek #endif
61165928Spendry 	if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
61265928Spendry 		openlog(progname, LOG_CONS, LOG_DAEMON);
61365928Spendry 		vsyslog(LOG_ERR, fmt, ap);
61465928Spendry 		closelog();
61565928Spendry 	} else {
61665928Spendry 		vwarnx(fmt, ap);
61765928Spendry 	}
61847870Skarels 	va_end(ap);
61947870Skarels 	exit(1);
62065928Spendry 	/*NOTREACHED*/
62110762Ssam }
62247870Skarels 
62347870Skarels usage()
62447870Skarels {
62547870Skarels 	if (mfs) {
62647870Skarels 		fprintf(stderr,
62753709Smckusick 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
62853709Smckusick 			progname);
62947870Skarels 	} else
63047870Skarels 		fprintf(stderr,
63153709Smckusick 		    "usage: %s [ -fsoptions ] special-device%s\n",
63253709Smckusick 		    progname,
63347870Skarels #ifdef COMPAT
63447870Skarels 		    " [device-type]");
63547870Skarels #else
63647870Skarels 		    "");
63747870Skarels #endif
63847870Skarels 	fprintf(stderr, "where fsoptions are:\n");
63947870Skarels 	fprintf(stderr,
64047870Skarels 	    "\t-N do not create file system, just print out parameters\n");
64159752Smckusick 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
64247870Skarels 	fprintf(stderr, "\t-S sector size\n");
64347870Skarels #ifdef COMPAT
64447870Skarels 	fprintf(stderr, "\t-T disktype\n");
64547870Skarels #endif
64647870Skarels 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
64747870Skarels 	fprintf(stderr, "\t-b block size\n");
64847870Skarels 	fprintf(stderr, "\t-c cylinders/group\n");
64947870Skarels 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
65047870Skarels 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
65147870Skarels 	fprintf(stderr, "\t-f frag size\n");
65247870Skarels 	fprintf(stderr, "\t-i number of bytes per inode\n");
65347870Skarels 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
65447870Skarels 	fprintf(stderr, "\t-l hardware sector interleave\n");
65547870Skarels 	fprintf(stderr, "\t-m minimum free space %%\n");
65647870Skarels 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
65747870Skarels 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
65847870Skarels 	fprintf(stderr, "\t-p spare sectors per track\n");
65947870Skarels 	fprintf(stderr, "\t-s file system size (sectors)\n");
66047870Skarels 	fprintf(stderr, "\t-r revolutions/minute\n");
66147870Skarels 	fprintf(stderr, "\t-t tracks/cylinder\n");
66247870Skarels 	fprintf(stderr, "\t-u sectors/track\n");
66347870Skarels 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
66447870Skarels 	exit(1);
66547870Skarels }
666