xref: /csrg-svn/sbin/newfs/newfs.c (revision 69159)
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*69159Smckusick static char sccsid[] = "@(#)newfs.c	8.13 (Berkeley) 05/01/95";
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>
29*69159Smckusick #include <ufs/ufs/dinode.h>
3051533Sbostic #include <ufs/ffs/fs.h>
31*69159Smckusick #include <ufs/ufs/ufsmount.h>
3210762Ssam 
3366488Sbostic #include <ctype.h>
3447870Skarels #include <errno.h>
3566488Sbostic #include <paths.h>
3666488Sbostic #include <stdio.h>
3766488Sbostic #include <stdlib.h>
3866488Sbostic #include <string.h>
3966488Sbostic #include <syslog.h>
4066488Sbostic #include <unistd.h>
4166488Sbostic 
4250787Storek #if __STDC__
4347870Skarels #include <stdarg.h>
4450787Storek #else
4550787Storek #include <varargs.h>
4650787Storek #endif
4710762Ssam 
4866488Sbostic #include "mntopts.h"
4966488Sbostic 
5066488Sbostic struct mntopt mopts[] = {
5166488Sbostic 	MOPT_STDOPTS,
5266597Shibler 	MOPT_ASYNC,
5366488Sbostic 	{ NULL },
5466488Sbostic };
5566488Sbostic 
5650787Storek #if __STDC__
5750787Storek void	fatal(const char *fmt, ...);
5850787Storek #else
5950787Storek void	fatal();
6050787Storek #endif
6150787Storek 
6247870Skarels #define	COMPAT			/* allow non-labeled disks */
6331680Skarels 
6430380Smckusick /*
6530380Smckusick  * The following two constants set the default block and fragment sizes.
6630380Smckusick  * Both constants must be a power of 2 and meet the following constraints:
6730380Smckusick  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
6830380Smckusick  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
6930380Smckusick  *	DESBLKSIZE / DESFRAGSIZE <= 8
7030380Smckusick  */
7130380Smckusick #define	DFL_FRAGSIZE	1024
7230380Smckusick #define	DFL_BLKSIZE	8192
7311183Ssam 
7430380Smckusick /*
7534137Smckusick  * Cylinder groups may have up to many cylinders. The actual
7630380Smckusick  * number used depends upon how much information can be stored
7734137Smckusick  * on a single cylinder. The default is to use 16 cylinders
7830380Smckusick  * per group.
7930380Smckusick  */
8030380Smckusick #define	DESCPG		16	/* desired fs_cpg */
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 */
14666597Shibler int	mntflags = MNT_ASYNC;	/* 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 
15766488Sbostic int
main(argc,argv)15810762Ssam main(argc, argv)
15914064Smckusick 	int argc;
16010762Ssam 	char *argv[];
16110762Ssam {
16247870Skarels 	extern char *optarg;
16347870Skarels 	extern int optind;
16447870Skarels 	register int ch;
16510762Ssam 	register struct partition *pp;
16630380Smckusick 	register struct disklabel *lp;
16730380Smckusick 	struct disklabel *getdisklabel();
16830380Smckusick 	struct partition oldpartition;
16910762Ssam 	struct stat st;
17053709Smckusick 	struct statfs *mp;
17166488Sbostic 	int fsi, fso, len, n;
17266488Sbostic 	char *cp, *s1, *s2, *special, *opstring, buf[BUFSIZ];
17310762Ssam 
17468997Sbostic 	if (progname = strrchr(*argv, '/'))
17547870Skarels 		++progname;
17647870Skarels 	else
17739049Smckusick 		progname = *argv;
17847870Skarels 
17950402Smckusick 	if (strstr(progname, "mfs")) {
18047870Skarels 		mfs = 1;
18139049Smckusick 		Nflag++;
18239049Smckusick 	}
18310762Ssam 
18466488Sbostic 	opstring = mfs ?
18566597Shibler 	    "NT:a:b:c:d:e:f:i:m:o:s:" :
18666488Sbostic 	    "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
18747870Skarels 	while ((ch = getopt(argc, argv, opstring)) != EOF)
18866488Sbostic 		switch (ch) {
18947870Skarels 		case 'N':
19066488Sbostic 			Nflag = 1;
19147870Skarels 			break;
19259750Smckusick 		case 'O':
19366488Sbostic 			Oflag = 1;
19459750Smckusick 			break;
19547870Skarels 		case 'S':
19647870Skarels 			if ((sectorsize = atoi(optarg)) <= 0)
19747870Skarels 				fatal("%s: bad sector size", optarg);
19847870Skarels 			break;
19943314Smckusick #ifdef COMPAT
20047870Skarels 		case 'T':
20147870Skarels 			disktype = optarg;
20247870Skarels 			break;
20343314Smckusick #endif
20447870Skarels 		case 'a':
20547870Skarels 			if ((maxcontig = atoi(optarg)) <= 0)
20666488Sbostic 				fatal("%s: bad maximum contiguous blocks\n",
20747870Skarels 				    optarg);
20847870Skarels 			break;
20947870Skarels 		case 'b':
21047870Skarels 			if ((bsize = atoi(optarg)) < MINBSIZE)
21147870Skarels 				fatal("%s: bad block size", optarg);
21247870Skarels 			break;
21347870Skarels 		case 'c':
21447870Skarels 			if ((cpg = atoi(optarg)) <= 0)
21547870Skarels 				fatal("%s: bad cylinders/group", optarg);
21647870Skarels 			cpgflg++;
21747870Skarels 			break;
21847870Skarels 		case 'd':
21947870Skarels 			if ((rotdelay = atoi(optarg)) < 0)
22047870Skarels 				fatal("%s: bad rotational delay\n", optarg);
22147870Skarels 			break;
22247870Skarels 		case 'e':
22347870Skarels 			if ((maxbpg = atoi(optarg)) <= 0)
22466488Sbostic 		fatal("%s: bad blocks per file in a cylinder group\n",
22547870Skarels 				    optarg);
22647870Skarels 			break;
22747870Skarels 		case 'f':
22847870Skarels 			if ((fsize = atoi(optarg)) <= 0)
22966488Sbostic 				fatal("%s: bad fragment size", optarg);
23047870Skarels 			break;
23147870Skarels 		case 'i':
23247870Skarels 			if ((density = atoi(optarg)) <= 0)
23347870Skarels 				fatal("%s: bad bytes per inode\n", optarg);
23447870Skarels 			break;
23547870Skarels 		case 'k':
23647870Skarels 			if ((trackskew = atoi(optarg)) < 0)
23747870Skarels 				fatal("%s: bad track skew", optarg);
23847870Skarels 			break;
23947870Skarels 		case 'l':
24047870Skarels 			if ((interleave = atoi(optarg)) <= 0)
24147870Skarels 				fatal("%s: bad interleave", optarg);
24247870Skarels 			break;
24347870Skarels 		case 'm':
24447870Skarels 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
24547870Skarels 				fatal("%s: bad free space %%\n", optarg);
24647870Skarels 			break;
24747870Skarels 		case 'n':
24847870Skarels 			if ((nrpos = atoi(optarg)) <= 0)
24947870Skarels 				fatal("%s: bad rotational layout count\n",
25047870Skarels 				    optarg);
25147870Skarels 			break;
25247870Skarels 		case 'o':
25366488Sbostic 			if (mfs)
254*69159Smckusick 				getmntopts(optarg, mopts, &mntflags, 0);
25566488Sbostic 			else {
25666488Sbostic 				if (strcmp(optarg, "space") == 0)
25766488Sbostic 					opt = FS_OPTSPACE;
25866488Sbostic 				else if (strcmp(optarg, "time") == 0)
25966488Sbostic 					opt = FS_OPTTIME;
26066488Sbostic 				else
26166488Sbostic 	fatal("%s: unknown optimization preference: use `space' or `time'.");
26266488Sbostic 			}
26347870Skarels 			break;
26447870Skarels 		case 'p':
26547870Skarels 			if ((trackspares = atoi(optarg)) < 0)
26647870Skarels 				fatal("%s: bad spare sectors per track",
26747870Skarels 				    optarg);
26847870Skarels 			break;
26947870Skarels 		case 'r':
27047870Skarels 			if ((rpm = atoi(optarg)) <= 0)
27166488Sbostic 				fatal("%s: bad revolutions/minute\n", optarg);
27247870Skarels 			break;
27347870Skarels 		case 's':
27447870Skarels 			if ((fssize = atoi(optarg)) <= 0)
27547870Skarels 				fatal("%s: bad file system size", optarg);
27647870Skarels 			break;
27747870Skarels 		case 't':
27847870Skarels 			if ((ntracks = atoi(optarg)) <= 0)
27947870Skarels 				fatal("%s: bad total tracks", optarg);
28047870Skarels 			break;
28147870Skarels 		case 'u':
28247870Skarels 			if ((nsectors = atoi(optarg)) <= 0)
28347870Skarels 				fatal("%s: bad sectors/track", optarg);
28447870Skarels 			break;
28547870Skarels 		case 'x':
28647870Skarels 			if ((cylspares = atoi(optarg)) < 0)
28747870Skarels 				fatal("%s: bad spare sectors per cylinder",
28847870Skarels 				    optarg);
28947870Skarels 			break;
29047870Skarels 		case '?':
29147870Skarels 		default:
29247870Skarels 			usage();
29347870Skarels 		}
29447870Skarels 	argc -= optind;
29547870Skarels 	argv += optind;
29643314Smckusick 
29765901Shibler 	if (argc != 2 && (mfs || argc != 1))
29847870Skarels 		usage();
29910762Ssam 
30010762Ssam 	special = argv[0];
30168997Sbostic 	cp = strrchr(special, '/');
30247870Skarels 	if (cp == 0) {
30347870Skarels 		/*
30447870Skarels 		 * No path prefix; try /dev/r%s then /dev/%s.
30547870Skarels 		 */
30647870Skarels 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
30747870Skarels 		if (stat(device, &st) == -1)
30847870Skarels 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
30947870Skarels 		special = device;
31047870Skarels 	}
31153709Smckusick 	if (Nflag) {
31253709Smckusick 		fso = -1;
31353709Smckusick 	} else {
31430380Smckusick 		fso = open(special, O_WRONLY);
31547870Skarels 		if (fso < 0)
31647870Skarels 			fatal("%s: %s", special, strerror(errno));
31753709Smckusick 
31853709Smckusick 		/* Bail if target special is mounted */
31953709Smckusick 		n = getmntinfo(&mp, MNT_NOWAIT);
32053709Smckusick 		if (n == 0)
32153709Smckusick 			fatal("%s: getmntinfo: %s", special, strerror(errno));
32253709Smckusick 
32353709Smckusick 		len = sizeof(_PATH_DEV) - 1;
32453709Smckusick 		s1 = special;
32553709Smckusick 		if (strncmp(_PATH_DEV, s1, len) == 0)
32653709Smckusick 			s1 += len;
32753709Smckusick 
32853709Smckusick 		while (--n >= 0) {
32953709Smckusick 			s2 = mp->f_mntfromname;
33053709Smckusick 			if (strncmp(_PATH_DEV, s2, len) == 0) {
33153709Smckusick 				s2 += len - 1;
33253709Smckusick 				*s2 = 'r';
33353709Smckusick 			}
33453709Smckusick 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
33553709Smckusick 				fatal("%s is mounted on %s",
33653709Smckusick 				    special, mp->f_mntonname);
33753709Smckusick 			++mp;
33853709Smckusick 		}
33953709Smckusick 	}
34055561Smckusick 	if (mfs && disktype != NULL) {
34155561Smckusick 		lp = (struct disklabel *)getdiskbyname(disktype);
34255561Smckusick 		if (lp == NULL)
34355561Smckusick 			fatal("%s: unknown disk type", disktype);
34455561Smckusick 		pp = &lp->d_partitions[1];
34555561Smckusick 	} else {
34655561Smckusick 		fsi = open(special, O_RDONLY);
34755561Smckusick 		if (fsi < 0)
34855561Smckusick 			fatal("%s: %s", special, strerror(errno));
34955561Smckusick 		if (fstat(fsi, &st) < 0)
35055561Smckusick 			fatal("%s: %s", special, strerror(errno));
35155561Smckusick 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
35255561Smckusick 			printf("%s: %s: not a character-special device\n",
35355561Smckusick 			    progname, special);
35468997Sbostic 		cp = strchr(argv[0], '\0') - 1;
355*69159Smckusick 		if (cp == (char *)-1 ||
356*69159Smckusick 		    (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
35755561Smckusick 			fatal("%s: can't figure out file system partition",
35855561Smckusick 			    argv[0]);
35931680Skarels #ifdef COMPAT
36055561Smckusick 		if (!mfs && disktype == NULL)
36155561Smckusick 			disktype = argv[1];
36243314Smckusick #endif
36355561Smckusick 		lp = getdisklabel(special, fsi);
36455561Smckusick 		if (isdigit(*cp))
36555561Smckusick 			pp = &lp->d_partitions[0];
36655561Smckusick 		else
36755561Smckusick 			pp = &lp->d_partitions[*cp - 'a'];
36855561Smckusick 		if (pp->p_size == 0)
36955561Smckusick 			fatal("%s: `%c' partition is unavailable",
37055561Smckusick 			    argv[0], *cp);
37159679Shibler 		if (pp->p_fstype == FS_BOOT)
37259679Shibler 			fatal("%s: `%c' partition overlaps boot program",
37359679Shibler 			      argv[0], *cp);
37455561Smckusick 	}
37530380Smckusick 	if (fssize == 0)
37610762Ssam 		fssize = pp->p_size;
37739325Smckusick 	if (fssize > pp->p_size && !mfs)
37830380Smckusick 	       fatal("%s: maximum file system size on the `%c' partition is %d",
37930380Smckusick 			argv[0], *cp, pp->p_size);
38030380Smckusick 	if (rpm == 0) {
38130380Smckusick 		rpm = lp->d_rpm;
38230380Smckusick 		if (rpm <= 0)
38330743Skarels 			rpm = 3600;
38410762Ssam 	}
38530380Smckusick 	if (ntracks == 0) {
38630380Smckusick 		ntracks = lp->d_ntracks;
38730380Smckusick 		if (ntracks <= 0)
38830743Skarels 			fatal("%s: no default #tracks", argv[0]);
38930380Smckusick 	}
39010762Ssam 	if (nsectors == 0) {
39130380Smckusick 		nsectors = lp->d_nsectors;
39230380Smckusick 		if (nsectors <= 0)
39330743Skarels 			fatal("%s: no default #sectors/track", argv[0]);
39410762Ssam 	}
39510762Ssam 	if (sectorsize == 0) {
39630380Smckusick 		sectorsize = lp->d_secsize;
39730380Smckusick 		if (sectorsize <= 0)
39830743Skarels 			fatal("%s: no default sector size", argv[0]);
39910762Ssam 	}
40030386Smckusick 	if (trackskew == -1) {
40130386Smckusick 		trackskew = lp->d_trackskew;
40230386Smckusick 		if (trackskew < 0)
40330743Skarels 			trackskew = 0;
40430386Smckusick 	}
40530386Smckusick 	if (interleave == 0) {
40630386Smckusick 		interleave = lp->d_interleave;
40730386Smckusick 		if (interleave <= 0)
40830743Skarels 			interleave = 1;
40930386Smckusick 	}
41010762Ssam 	if (fsize == 0) {
41110762Ssam 		fsize = pp->p_fsize;
41230380Smckusick 		if (fsize <= 0)
41330380Smckusick 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
41410762Ssam 	}
41530380Smckusick 	if (bsize == 0) {
41630380Smckusick 		bsize = pp->p_frag * pp->p_fsize;
41730380Smckusick 		if (bsize <= 0)
41830380Smckusick 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
41911069Ssam 	}
42059750Smckusick 	/*
42159750Smckusick 	 * Maxcontig sets the default for the maximum number of blocks
42259750Smckusick 	 * that may be allocated sequentially. With filesystem clustering
42359750Smckusick 	 * it is possible to allocate contiguous blocks up to the maximum
42459750Smckusick 	 * transfer size permitted by the controller or buffering.
42559750Smckusick 	 */
42659750Smckusick 	if (maxcontig == 0)
42767566Smckusick 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
42839049Smckusick 	if (density == 0)
42939049Smckusick 		density = NFPI * fsize;
43059752Smckusick 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
43130380Smckusick 		fprintf(stderr, "Warning: changing optimization to space ");
43259752Smckusick 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
43324702Smckusick 		opt = FS_OPTSPACE;
43424702Smckusick 	}
43530386Smckusick 	if (trackspares == -1) {
43630386Smckusick 		trackspares = lp->d_sparespertrack;
43730386Smckusick 		if (trackspares < 0)
43830743Skarels 			trackspares = 0;
43930386Smckusick 	}
44030386Smckusick 	nphyssectors = nsectors + trackspares;
44130386Smckusick 	if (cylspares == -1) {
44230386Smckusick 		cylspares = lp->d_sparespercyl;
44330386Smckusick 		if (cylspares < 0)
44430743Skarels 			cylspares = 0;
44530386Smckusick 	}
44630386Smckusick 	secpercyl = nsectors * ntracks - cylspares;
44730380Smckusick 	if (secpercyl != lp->d_secpercyl)
44847870Skarels 		fprintf(stderr, "%s (%d) %s (%lu)\n",
44930380Smckusick 			"Warning: calculated sectors per cylinder", secpercyl,
45030380Smckusick 			"disagrees with disk label", lp->d_secpercyl);
45132321Smckusick 	if (maxbpg == 0)
45232321Smckusick 		maxbpg = MAXBLKPG(bsize);
45330386Smckusick 	headswitch = lp->d_headswitch;
45430386Smckusick 	trackseek = lp->d_trkseek;
45547870Skarels #ifdef notdef /* label may be 0 if faked up by kernel */
45630691Skarels 	bbsize = lp->d_bbsize;
45730691Skarels 	sbsize = lp->d_sbsize;
45845836Skarels #endif
45930380Smckusick 	oldpartition = *pp;
46030691Skarels #ifdef tahoe
46130691Skarels 	realsectorsize = sectorsize;
46230862Skarels 	if (sectorsize != DEV_BSIZE) {		/* XXX */
46330691Skarels 		int secperblk = DEV_BSIZE / sectorsize;
46430691Skarels 
46530691Skarels 		sectorsize = DEV_BSIZE;
46630691Skarels 		nsectors /= secperblk;
46730691Skarels 		nphyssectors /= secperblk;
46830691Skarels 		secpercyl /= secperblk;
46930691Skarels 		fssize /= secperblk;
47030691Skarels 		pp->p_size /= secperblk;
47130691Skarels 	}
47230691Skarels #endif
47330380Smckusick 	mkfs(pp, special, fsi, fso);
47430691Skarels #ifdef tahoe
47530691Skarels 	if (realsectorsize != DEV_BSIZE)
47630691Skarels 		pp->p_size *= DEV_BSIZE / realsectorsize;
47730691Skarels #endif
47869039Sbostic 	if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)))
47930380Smckusick 		rewritelabel(special, fso, lp);
48039049Smckusick 	if (!Nflag)
48139049Smckusick 		close(fso);
48239049Smckusick 	close(fsi);
48349063Sbostic #ifdef MFS
48439325Smckusick 	if (mfs) {
48549063Sbostic 		struct mfs_args args;
48649063Sbostic 
48739325Smckusick 		sprintf(buf, "mfs:%d", getpid());
48865711Shibler 		args.fspec = buf;
48965711Shibler 		args.export.ex_root = -2;
49065711Shibler 		if (mntflags & MNT_RDONLY)
49165711Shibler 			args.export.ex_flags = MNT_EXRDONLY;
49265711Shibler 		else
49365711Shibler 			args.export.ex_flags = 0;
49439049Smckusick 		args.base = membase;
49539049Smckusick 		args.size = fssize * sectorsize;
496*69159Smckusick 		if (mount("mfs", argv[1], mntflags, &args) < 0)
49747870Skarels 			fatal("%s: %s", argv[1], strerror(errno));
49839049Smckusick 	}
49949063Sbostic #endif
50030380Smckusick 	exit(0);
50130380Smckusick }
50230380Smckusick 
50331680Skarels #ifdef COMPAT
50443314Smckusick char lmsg[] = "%s: can't read disk label; disk type must be specified";
50543314Smckusick #else
50643314Smckusick char lmsg[] = "%s: can't read disk label";
50743314Smckusick #endif
50831680Skarels 
50931680Skarels struct disklabel *
getdisklabel(s,fd)51030380Smckusick getdisklabel(s, fd)
51130380Smckusick 	char *s;
51231680Skarels 	int fd;
51330380Smckusick {
51430380Smckusick 	static struct disklabel lab;
51530380Smckusick 
51630380Smckusick 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
51743314Smckusick #ifdef COMPAT
51843314Smckusick 		if (disktype) {
51947870Skarels 			struct disklabel *lp, *getdiskbyname();
52043314Smckusick 
52147870Skarels 			unlabeled++;
52247870Skarels 			lp = getdiskbyname(disktype);
52347870Skarels 			if (lp == NULL)
52447870Skarels 				fatal("%s: unknown disk type", disktype);
52547870Skarels 			return (lp);
52643314Smckusick 		}
52743314Smckusick #endif
52865928Spendry 		warn("ioctl (GDINFO)");
52943314Smckusick 		fatal(lmsg, s);
53010762Ssam 	}
53130380Smckusick 	return (&lab);
53230380Smckusick }
53310762Ssam 
rewritelabel(s,fd,lp)53430380Smckusick rewritelabel(s, fd, lp)
53530380Smckusick 	char *s;
53630380Smckusick 	int fd;
53730380Smckusick 	register struct disklabel *lp;
53830380Smckusick {
53931680Skarels #ifdef COMPAT
54047870Skarels 	if (unlabeled)
54131680Skarels 		return;
54231680Skarels #endif
54330380Smckusick 	lp->d_checksum = 0;
54430380Smckusick 	lp->d_checksum = dkcksum(lp);
54530380Smckusick 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
54665928Spendry 		warn("ioctl (WDINFO)");
54730380Smckusick 		fatal("%s: can't rewrite disk label", s);
54810762Ssam 	}
54930446Skarels #if vax
55030446Skarels 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
55130446Skarels 		register i;
55230691Skarels 		int cfd;
55330446Skarels 		daddr_t alt;
55430691Skarels 		char specname[64];
55530691Skarels 		char blk[1024];
55631045Ssam 		char *cp;
55730446Skarels 
55830691Skarels 		/*
55930691Skarels 		 * Make name for 'c' partition.
56030691Skarels 		 */
56130691Skarels 		strcpy(specname, s);
56230691Skarels 		cp = specname + strlen(specname) - 1;
56330691Skarels 		if (!isdigit(*cp))
56430691Skarels 			*cp = 'c';
56530691Skarels 		cfd = open(specname, O_WRONLY);
56647870Skarels 		if (cfd < 0)
56747870Skarels 			fatal("%s: %s", specname, strerror(errno));
56868997Sbostic 		memset(blk, 0, sizeof(blk));
56930691Skarels 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
57030446Skarels 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
57130446Skarels 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
57247870Skarels 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
57347870Skarels 			    L_SET) == -1)
57447870Skarels 				fatal("lseek to badsector area: %s",
57547870Skarels 				    strerror(errno));
57647870Skarels 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
57765928Spendry 				warn("alternate label %d write", i/2);
57830380Smckusick 		}
57939049Smckusick 		close(cfd);
58030380Smckusick 	}
58130446Skarels #endif
58210762Ssam }
58310762Ssam 
58410762Ssam /*VARARGS*/
58550787Storek void
58650787Storek #if __STDC__
fatal(const char * fmt,...)58750787Storek fatal(const char *fmt, ...)
58850787Storek #else
58950787Storek fatal(fmt, va_alist)
59010762Ssam 	char *fmt;
59150787Storek 	va_dcl
59250787Storek #endif
59310762Ssam {
59447870Skarels 	va_list ap;
59510762Ssam 
59650787Storek #if __STDC__
59750787Storek 	va_start(ap, fmt);
59850787Storek #else
59950787Storek 	va_start(ap);
60050787Storek #endif
60165928Spendry 	if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
60265928Spendry 		openlog(progname, LOG_CONS, LOG_DAEMON);
60365928Spendry 		vsyslog(LOG_ERR, fmt, ap);
60465928Spendry 		closelog();
60565928Spendry 	} else {
60665928Spendry 		vwarnx(fmt, ap);
60765928Spendry 	}
60847870Skarels 	va_end(ap);
60947870Skarels 	exit(1);
61065928Spendry 	/*NOTREACHED*/
61110762Ssam }
61247870Skarels 
usage()61347870Skarels usage()
61447870Skarels {
61547870Skarels 	if (mfs) {
61647870Skarels 		fprintf(stderr,
61753709Smckusick 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
61853709Smckusick 			progname);
61947870Skarels 	} else
62047870Skarels 		fprintf(stderr,
62153709Smckusick 		    "usage: %s [ -fsoptions ] special-device%s\n",
62253709Smckusick 		    progname,
62347870Skarels #ifdef COMPAT
62447870Skarels 		    " [device-type]");
62547870Skarels #else
62647870Skarels 		    "");
62747870Skarels #endif
62847870Skarels 	fprintf(stderr, "where fsoptions are:\n");
62947870Skarels 	fprintf(stderr,
63047870Skarels 	    "\t-N do not create file system, just print out parameters\n");
63159752Smckusick 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
63247870Skarels 	fprintf(stderr, "\t-S sector size\n");
63347870Skarels #ifdef COMPAT
63447870Skarels 	fprintf(stderr, "\t-T disktype\n");
63547870Skarels #endif
63647870Skarels 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
63747870Skarels 	fprintf(stderr, "\t-b block size\n");
63847870Skarels 	fprintf(stderr, "\t-c cylinders/group\n");
63947870Skarels 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
64047870Skarels 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
64147870Skarels 	fprintf(stderr, "\t-f frag size\n");
64247870Skarels 	fprintf(stderr, "\t-i number of bytes per inode\n");
64347870Skarels 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
64447870Skarels 	fprintf(stderr, "\t-l hardware sector interleave\n");
64547870Skarels 	fprintf(stderr, "\t-m minimum free space %%\n");
64647870Skarels 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
64747870Skarels 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
64847870Skarels 	fprintf(stderr, "\t-p spare sectors per track\n");
64947870Skarels 	fprintf(stderr, "\t-s file system size (sectors)\n");
65047870Skarels 	fprintf(stderr, "\t-r revolutions/minute\n");
65147870Skarels 	fprintf(stderr, "\t-t tracks/cylinder\n");
65247870Skarels 	fprintf(stderr, "\t-u sectors/track\n");
65347870Skarels 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
65447870Skarels 	exit(1);
65547870Skarels }
656