xref: /csrg-svn/sbin/newfs/newfs.c (revision 66000)
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*66000Smckusick static char sccsid[] = "@(#)newfs.c	8.5 (Berkeley) 02/05/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>
4165928Spendry #include <syslog.h>
4237951Sbostic #include <paths.h>
4310762Ssam 
4450787Storek #if __STDC__
4550787Storek void	fatal(const char *fmt, ...);
4650787Storek #else
4750787Storek void	fatal();
4850787Storek #endif
4950787Storek 
5047870Skarels #define	COMPAT			/* allow non-labeled disks */
5131680Skarels 
5230380Smckusick /*
5330380Smckusick  * The following two constants set the default block and fragment sizes.
5430380Smckusick  * Both constants must be a power of 2 and meet the following constraints:
5530380Smckusick  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
5630380Smckusick  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
5730380Smckusick  *	DESBLKSIZE / DESFRAGSIZE <= 8
5830380Smckusick  */
5930380Smckusick #define	DFL_FRAGSIZE	1024
6030380Smckusick #define	DFL_BLKSIZE	8192
6111183Ssam 
6230380Smckusick /*
6334137Smckusick  * Cylinder groups may have up to many cylinders. The actual
6430380Smckusick  * number used depends upon how much information can be stored
6534137Smckusick  * on a single cylinder. The default is to use 16 cylinders
6630380Smckusick  * per group.
6730380Smckusick  */
6830380Smckusick #define	DESCPG		16	/* desired fs_cpg */
6930380Smckusick 
7030380Smckusick /*
7130380Smckusick  * MINFREE gives the minimum acceptable percentage of file system
7230380Smckusick  * blocks which may be free. If the freelist drops below this level
7330380Smckusick  * only the superuser may continue to allocate blocks. This may
7430380Smckusick  * be set to 0 if no reserve of free blocks is deemed necessary,
7530380Smckusick  * however throughput drops by fifty percent if the file system
7659750Smckusick  * is run at between 95% and 100% full; thus the default value of
7759750Smckusick  * fs_minfree is 5%. With 5% free space, fragmentation is not a
7830380Smckusick  * problem, so we choose to optimize for time.
7930380Smckusick  */
8059750Smckusick #define MINFREE		5
8130380Smckusick #define DEFAULTOPT	FS_OPTTIME
8230380Smckusick 
8330380Smckusick /*
8430380Smckusick  * ROTDELAY gives the minimum number of milliseconds to initiate
8530380Smckusick  * another disk transfer on the same cylinder. It is used in
8630380Smckusick  * determining the rotationally optimal layout for disk blocks
8730380Smckusick  * within a file; the default of fs_rotdelay is 4ms.
8830380Smckusick  */
8930380Smckusick #define ROTDELAY	4
9030380Smckusick 
9130380Smckusick /*
9232309Smckusick  * MAXBLKPG determines the maximum number of data blocks which are
9332309Smckusick  * placed in a single cylinder group. The default is one indirect
9432309Smckusick  * block worth of data blocks.
9532309Smckusick  */
9632309Smckusick #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
9732309Smckusick 
9832309Smckusick /*
9930380Smckusick  * Each file system has a number of inodes statically allocated.
10039049Smckusick  * We allocate one inode slot per NFPI fragments, expecting this
10130380Smckusick  * to be far more than we will ever need.
10230380Smckusick  */
10339049Smckusick #define	NFPI		4
10430380Smckusick 
10534137Smckusick /*
10634137Smckusick  * For each cylinder we keep track of the availability of blocks at different
10734137Smckusick  * rotational positions, so that we can lay out the data to be picked
10834137Smckusick  * up with minimum rotational latency.  NRPOS is the default number of
10934137Smckusick  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
11034137Smckusick  * of our summary information is 2ms for a typical 3600 rpm drive.
11134137Smckusick  */
11234137Smckusick #define	NRPOS		8	/* number distinct rotational positions */
11334137Smckusick 
11434137Smckusick 
11539325Smckusick int	mfs;			/* run as the memory based filesystem */
11630380Smckusick int	Nflag;			/* run without writing file system */
11759750Smckusick int	Oflag;			/* format as an 4.3BSD file system */
11810762Ssam int	fssize;			/* file system size */
11910762Ssam int	ntracks;		/* # tracks/cylinder */
12010762Ssam int	nsectors;		/* # sectors/track */
12130386Smckusick int	nphyssectors;		/* # sectors/track including spares */
12230380Smckusick int	secpercyl;		/* sectors per cylinder */
12330386Smckusick int	trackspares = -1;	/* spare sectors per track */
12430386Smckusick int	cylspares = -1;		/* spare sectors per cylinder */
12510762Ssam int	sectorsize;		/* bytes/sector */
12630691Skarels #ifdef tahoe
12730691Skarels int	realsectorsize;		/* bytes/sector in hardware */
12830691Skarels #endif
12911069Ssam int	rpm;			/* revolutions/minute of drive */
13030386Smckusick int	interleave;		/* hardware sector interleave */
13130386Smckusick int	trackskew = -1;		/* sector 0 skew, per track */
13230386Smckusick int	headswitch;		/* head switch time, usec */
13330386Smckusick int	trackseek;		/* track-to-track seek, usec */
13430862Skarels int	fsize = 0;		/* fragment size */
13530862Skarels int	bsize = 0;		/* block size */
13630380Smckusick int	cpg = DESCPG;		/* cylinders/cylinder group */
13732119Smckusick int	cpgflg;			/* cylinders/cylinder group flag was given */
13830380Smckusick int	minfree = MINFREE;	/* free space threshold */
13930380Smckusick int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
14039049Smckusick int	density;		/* number of bytes per inode */
14159750Smckusick int	maxcontig = 0;		/* max contiguous blocks to allocate */
14230380Smckusick int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
14332309Smckusick int	maxbpg;			/* maximum blocks per file in a cyl group */
14434137Smckusick int	nrpos = NRPOS;		/* # of distinguished rotational positions */
14530691Skarels int	bbsize = BBSIZE;	/* boot block size */
14630691Skarels int	sbsize = SBSIZE;	/* superblock size */
14739325Smckusick int	mntflags;		/* flags to be passed to mount */
14839049Smckusick u_long	memleft;		/* virtual memory available */
14939049Smckusick caddr_t	membase;		/* start address of memory based filesystem */
15031680Skarels #ifdef COMPAT
15143314Smckusick char	*disktype;
15247870Skarels int	unlabeled;
15331680Skarels #endif
15410762Ssam 
15510762Ssam char	device[MAXPATHLEN];
15639049Smckusick char	*progname;
15710762Ssam 
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;
17030380Smckusick 	int fsi, fso;
17147870Skarels 	char *cp, *special, *opstring, buf[BUFSIZ];
17253709Smckusick 	struct statfs *mp;
17353709Smckusick 	char *s1, *s2;
17453709Smckusick 	int len, n;
17510762Ssam 
17647870Skarels 	if (progname = rindex(*argv, '/'))
17747870Skarels 		++progname;
17847870Skarels 	else
17939049Smckusick 		progname = *argv;
18047870Skarels 
18150402Smckusick 	if (strstr(progname, "mfs")) {
18247870Skarels 		mfs = 1;
18339049Smckusick 		Nflag++;
18439049Smckusick 	}
18510762Ssam 
18659750Smckusick 	opstring = "F:NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
18747870Skarels 	if (!mfs)
18847870Skarels 		opstring += 2;		/* -F is mfs only */
18939325Smckusick 
19047870Skarels 	while ((ch = getopt(argc, argv, opstring)) != EOF)
19147870Skarels 		switch(ch) {
19247870Skarels 		case 'F':
19347870Skarels 			if ((mntflags = atoi(optarg)) == 0)
19447870Skarels 				fatal("%s: bad mount flags", optarg);
19547870Skarels 			break;
19647870Skarels 		case 'N':
19747870Skarels 			Nflag++;
19847870Skarels 			break;
19959750Smckusick 		case 'O':
20059750Smckusick 			Oflag++;
20159750Smckusick 			break;
20247870Skarels 		case 'S':
20347870Skarels 			if ((sectorsize = atoi(optarg)) <= 0)
20447870Skarels 				fatal("%s: bad sector size", optarg);
20547870Skarels 			break;
20643314Smckusick #ifdef COMPAT
20747870Skarels 		case 'T':
20847870Skarels 			disktype = optarg;
20947870Skarels 			break;
21043314Smckusick #endif
21147870Skarels 		case 'a':
21247870Skarels 			if ((maxcontig = atoi(optarg)) <= 0)
21347870Skarels 				fatal("%s: bad max contiguous blocks\n",
21447870Skarels 				    optarg);
21547870Skarels 			break;
21647870Skarels 		case 'b':
21747870Skarels 			if ((bsize = atoi(optarg)) < MINBSIZE)
21847870Skarels 				fatal("%s: bad block size", optarg);
21947870Skarels 			break;
22047870Skarels 		case 'c':
22147870Skarels 			if ((cpg = atoi(optarg)) <= 0)
22247870Skarels 				fatal("%s: bad cylinders/group", optarg);
22347870Skarels 			cpgflg++;
22447870Skarels 			break;
22547870Skarels 		case 'd':
22647870Skarels 			if ((rotdelay = atoi(optarg)) < 0)
22747870Skarels 				fatal("%s: bad rotational delay\n", optarg);
22847870Skarels 			break;
22947870Skarels 		case 'e':
23047870Skarels 			if ((maxbpg = atoi(optarg)) <= 0)
23147870Skarels 				fatal("%s: bad blocks per file in a cyl group\n",
23247870Skarels 				    optarg);
23347870Skarels 			break;
23447870Skarels 		case 'f':
23547870Skarels 			if ((fsize = atoi(optarg)) <= 0)
23647870Skarels 				fatal("%s: bad frag size", optarg);
23747870Skarels 			break;
23847870Skarels 		case 'i':
23947870Skarels 			if ((density = atoi(optarg)) <= 0)
24047870Skarels 				fatal("%s: bad bytes per inode\n", optarg);
24147870Skarels 			break;
24247870Skarels 		case 'k':
24347870Skarels 			if ((trackskew = atoi(optarg)) < 0)
24447870Skarels 				fatal("%s: bad track skew", optarg);
24547870Skarels 			break;
24647870Skarels 		case 'l':
24747870Skarels 			if ((interleave = atoi(optarg)) <= 0)
24847870Skarels 				fatal("%s: bad interleave", optarg);
24947870Skarels 			break;
25047870Skarels 		case 'm':
25147870Skarels 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
25247870Skarels 				fatal("%s: bad free space %%\n", optarg);
25347870Skarels 			break;
25447870Skarels 		case 'n':
25547870Skarels 			if ((nrpos = atoi(optarg)) <= 0)
25647870Skarels 				fatal("%s: bad rotational layout count\n",
25747870Skarels 				    optarg);
25847870Skarels 			break;
25947870Skarels 		case 'o':
26047870Skarels 			if (strcmp(optarg, "space") == 0)
26147870Skarels 				opt = FS_OPTSPACE;
26247870Skarels 			else if (strcmp(optarg, "time") == 0)
26347870Skarels 				opt = FS_OPTTIME;
26447870Skarels 			else
26547870Skarels 				fatal("%s: bad optimization preference %s",
26647870Skarels 				    optarg, "(options are `space' or `time')");
26747870Skarels 			break;
26847870Skarels 		case 'p':
26947870Skarels 			if ((trackspares = atoi(optarg)) < 0)
27047870Skarels 				fatal("%s: bad spare sectors per track",
27147870Skarels 				    optarg);
27247870Skarels 			break;
27347870Skarels 		case 'r':
27447870Skarels 			if ((rpm = atoi(optarg)) <= 0)
27547870Skarels 				fatal("%s: bad revs/minute\n", optarg);
27647870Skarels 			break;
27747870Skarels 		case 's':
27847870Skarels 			if ((fssize = atoi(optarg)) <= 0)
27947870Skarels 				fatal("%s: bad file system size", optarg);
28047870Skarels 			break;
28147870Skarels 		case 't':
28247870Skarels 			if ((ntracks = atoi(optarg)) <= 0)
28347870Skarels 				fatal("%s: bad total tracks", optarg);
28447870Skarels 			break;
28547870Skarels 		case 'u':
28647870Skarels 			if ((nsectors = atoi(optarg)) <= 0)
28747870Skarels 				fatal("%s: bad sectors/track", optarg);
28847870Skarels 			break;
28947870Skarels 		case 'x':
29047870Skarels 			if ((cylspares = atoi(optarg)) < 0)
29147870Skarels 				fatal("%s: bad spare sectors per cylinder",
29247870Skarels 				    optarg);
29347870Skarels 			break;
29447870Skarels 		case '?':
29547870Skarels 		default:
29647870Skarels 			usage();
29747870Skarels 		}
29847870Skarels 	argc -= optind;
29947870Skarels 	argv += optind;
30043314Smckusick 
30165901Shibler 	if (argc != 2 && (mfs || argc != 1))
30247870Skarels 		usage();
30310762Ssam 
30410762Ssam 	special = argv[0];
30514064Smckusick 	cp = rindex(special, '/');
30647870Skarels 	if (cp == 0) {
30747870Skarels 		/*
30847870Skarels 		 * No path prefix; try /dev/r%s then /dev/%s.
30947870Skarels 		 */
31047870Skarels 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
31147870Skarels 		if (stat(device, &st) == -1)
31247870Skarels 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
31347870Skarels 		special = device;
31447870Skarels 	}
31553709Smckusick 	if (Nflag) {
31653709Smckusick 		fso = -1;
31753709Smckusick 	} else {
31830380Smckusick 		fso = open(special, O_WRONLY);
31947870Skarels 		if (fso < 0)
32047870Skarels 			fatal("%s: %s", special, strerror(errno));
32153709Smckusick 
32253709Smckusick 		/* Bail if target special is mounted */
32353709Smckusick 		n = getmntinfo(&mp, MNT_NOWAIT);
32453709Smckusick 		if (n == 0)
32553709Smckusick 			fatal("%s: getmntinfo: %s", special, strerror(errno));
32653709Smckusick 
32753709Smckusick 		len = sizeof(_PATH_DEV) - 1;
32853709Smckusick 		s1 = special;
32953709Smckusick 		if (strncmp(_PATH_DEV, s1, len) == 0)
33053709Smckusick 			s1 += len;
33153709Smckusick 
33253709Smckusick 		while (--n >= 0) {
33353709Smckusick 			s2 = mp->f_mntfromname;
33453709Smckusick 			if (strncmp(_PATH_DEV, s2, len) == 0) {
33553709Smckusick 				s2 += len - 1;
33653709Smckusick 				*s2 = 'r';
33753709Smckusick 			}
33853709Smckusick 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
33953709Smckusick 				fatal("%s is mounted on %s",
34053709Smckusick 				    special, mp->f_mntonname);
34153709Smckusick 			++mp;
34253709Smckusick 		}
34353709Smckusick 	}
34455561Smckusick 	if (mfs && disktype != NULL) {
34555561Smckusick 		lp = (struct disklabel *)getdiskbyname(disktype);
34655561Smckusick 		if (lp == NULL)
34755561Smckusick 			fatal("%s: unknown disk type", disktype);
34855561Smckusick 		pp = &lp->d_partitions[1];
34955561Smckusick 	} else {
35055561Smckusick 		fsi = open(special, O_RDONLY);
35155561Smckusick 		if (fsi < 0)
35255561Smckusick 			fatal("%s: %s", special, strerror(errno));
35355561Smckusick 		if (fstat(fsi, &st) < 0)
35455561Smckusick 			fatal("%s: %s", special, strerror(errno));
35555561Smckusick 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
35655561Smckusick 			printf("%s: %s: not a character-special device\n",
35755561Smckusick 			    progname, special);
35855561Smckusick 		cp = index(argv[0], '\0') - 1;
35955561Smckusick 		if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
36055561Smckusick 			fatal("%s: can't figure out file system partition",
36155561Smckusick 			    argv[0]);
36231680Skarels #ifdef COMPAT
36355561Smckusick 		if (!mfs && disktype == NULL)
36455561Smckusick 			disktype = argv[1];
36543314Smckusick #endif
36655561Smckusick 		lp = getdisklabel(special, fsi);
36755561Smckusick 		if (isdigit(*cp))
36855561Smckusick 			pp = &lp->d_partitions[0];
36955561Smckusick 		else
37055561Smckusick 			pp = &lp->d_partitions[*cp - 'a'];
37155561Smckusick 		if (pp->p_size == 0)
37255561Smckusick 			fatal("%s: `%c' partition is unavailable",
37355561Smckusick 			    argv[0], *cp);
37459679Shibler 		if (pp->p_fstype == FS_BOOT)
37559679Shibler 			fatal("%s: `%c' partition overlaps boot program",
37659679Shibler 			      argv[0], *cp);
37755561Smckusick 	}
37830380Smckusick 	if (fssize == 0)
37910762Ssam 		fssize = pp->p_size;
38039325Smckusick 	if (fssize > pp->p_size && !mfs)
38130380Smckusick 	       fatal("%s: maximum file system size on the `%c' partition is %d",
38230380Smckusick 			argv[0], *cp, pp->p_size);
38330380Smckusick 	if (rpm == 0) {
38430380Smckusick 		rpm = lp->d_rpm;
38530380Smckusick 		if (rpm <= 0)
38630743Skarels 			rpm = 3600;
38710762Ssam 	}
38830380Smckusick 	if (ntracks == 0) {
38930380Smckusick 		ntracks = lp->d_ntracks;
39030380Smckusick 		if (ntracks <= 0)
39130743Skarels 			fatal("%s: no default #tracks", argv[0]);
39230380Smckusick 	}
39310762Ssam 	if (nsectors == 0) {
39430380Smckusick 		nsectors = lp->d_nsectors;
39530380Smckusick 		if (nsectors <= 0)
39630743Skarels 			fatal("%s: no default #sectors/track", argv[0]);
39710762Ssam 	}
39810762Ssam 	if (sectorsize == 0) {
39930380Smckusick 		sectorsize = lp->d_secsize;
40030380Smckusick 		if (sectorsize <= 0)
40130743Skarels 			fatal("%s: no default sector size", argv[0]);
40210762Ssam 	}
40330386Smckusick 	if (trackskew == -1) {
40430386Smckusick 		trackskew = lp->d_trackskew;
40530386Smckusick 		if (trackskew < 0)
40630743Skarels 			trackskew = 0;
40730386Smckusick 	}
40830386Smckusick 	if (interleave == 0) {
40930386Smckusick 		interleave = lp->d_interleave;
41030386Smckusick 		if (interleave <= 0)
41130743Skarels 			interleave = 1;
41230386Smckusick 	}
41310762Ssam 	if (fsize == 0) {
41410762Ssam 		fsize = pp->p_fsize;
41530380Smckusick 		if (fsize <= 0)
41630380Smckusick 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
41710762Ssam 	}
41830380Smckusick 	if (bsize == 0) {
41930380Smckusick 		bsize = pp->p_frag * pp->p_fsize;
42030380Smckusick 		if (bsize <= 0)
42130380Smckusick 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
42211069Ssam 	}
42359750Smckusick 	/*
42459750Smckusick 	 * Maxcontig sets the default for the maximum number of blocks
42559750Smckusick 	 * that may be allocated sequentially. With filesystem clustering
42659750Smckusick 	 * it is possible to allocate contiguous blocks up to the maximum
42759750Smckusick 	 * transfer size permitted by the controller or buffering.
42859750Smckusick 	 */
42959750Smckusick 	if (maxcontig == 0)
430*66000Smckusick 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize - 1);
43139049Smckusick 	if (density == 0)
43239049Smckusick 		density = NFPI * fsize;
43359752Smckusick 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
43430380Smckusick 		fprintf(stderr, "Warning: changing optimization to space ");
43559752Smckusick 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
43624702Smckusick 		opt = FS_OPTSPACE;
43724702Smckusick 	}
43830386Smckusick 	if (trackspares == -1) {
43930386Smckusick 		trackspares = lp->d_sparespertrack;
44030386Smckusick 		if (trackspares < 0)
44130743Skarels 			trackspares = 0;
44230386Smckusick 	}
44330386Smckusick 	nphyssectors = nsectors + trackspares;
44430386Smckusick 	if (cylspares == -1) {
44530386Smckusick 		cylspares = lp->d_sparespercyl;
44630386Smckusick 		if (cylspares < 0)
44730743Skarels 			cylspares = 0;
44830386Smckusick 	}
44930386Smckusick 	secpercyl = nsectors * ntracks - cylspares;
45030380Smckusick 	if (secpercyl != lp->d_secpercyl)
45147870Skarels 		fprintf(stderr, "%s (%d) %s (%lu)\n",
45230380Smckusick 			"Warning: calculated sectors per cylinder", secpercyl,
45330380Smckusick 			"disagrees with disk label", lp->d_secpercyl);
45432321Smckusick 	if (maxbpg == 0)
45532321Smckusick 		maxbpg = MAXBLKPG(bsize);
45630386Smckusick 	headswitch = lp->d_headswitch;
45730386Smckusick 	trackseek = lp->d_trkseek;
45847870Skarels #ifdef notdef /* label may be 0 if faked up by kernel */
45930691Skarels 	bbsize = lp->d_bbsize;
46030691Skarels 	sbsize = lp->d_sbsize;
46145836Skarels #endif
46230380Smckusick 	oldpartition = *pp;
46330691Skarels #ifdef tahoe
46430691Skarels 	realsectorsize = sectorsize;
46530862Skarels 	if (sectorsize != DEV_BSIZE) {		/* XXX */
46630691Skarels 		int secperblk = DEV_BSIZE / sectorsize;
46730691Skarels 
46830691Skarels 		sectorsize = DEV_BSIZE;
46930691Skarels 		nsectors /= secperblk;
47030691Skarels 		nphyssectors /= secperblk;
47130691Skarels 		secpercyl /= secperblk;
47230691Skarels 		fssize /= secperblk;
47330691Skarels 		pp->p_size /= secperblk;
47430691Skarels 	}
47530691Skarels #endif
47630380Smckusick 	mkfs(pp, special, fsi, fso);
47730691Skarels #ifdef tahoe
47830691Skarels 	if (realsectorsize != DEV_BSIZE)
47930691Skarels 		pp->p_size *= DEV_BSIZE / realsectorsize;
48030691Skarels #endif
48130380Smckusick 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
48230380Smckusick 		rewritelabel(special, fso, lp);
48339049Smckusick 	if (!Nflag)
48439049Smckusick 		close(fso);
48539049Smckusick 	close(fsi);
48649063Sbostic #ifdef MFS
48739325Smckusick 	if (mfs) {
48849063Sbostic 		struct mfs_args args;
48949063Sbostic 
49039325Smckusick 		sprintf(buf, "mfs:%d", getpid());
49165711Shibler 		args.fspec = buf;
49265711Shibler 		args.export.ex_root = -2;
49365711Shibler 		if (mntflags & MNT_RDONLY)
49465711Shibler 			args.export.ex_flags = MNT_EXRDONLY;
49565711Shibler 		else
49665711Shibler 			args.export.ex_flags = 0;
49739049Smckusick 		args.base = membase;
49839049Smckusick 		args.size = fssize * sectorsize;
49947870Skarels 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
50047870Skarels 			fatal("%s: %s", argv[1], strerror(errno));
50139049Smckusick 	}
50249063Sbostic #endif
50330380Smckusick 	exit(0);
50430380Smckusick }
50530380Smckusick 
50631680Skarels #ifdef COMPAT
50743314Smckusick char lmsg[] = "%s: can't read disk label; disk type must be specified";
50843314Smckusick #else
50943314Smckusick char lmsg[] = "%s: can't read disk label";
51043314Smckusick #endif
51131680Skarels 
51231680Skarels struct disklabel *
51330380Smckusick getdisklabel(s, fd)
51430380Smckusick 	char *s;
51531680Skarels 	int fd;
51630380Smckusick {
51730380Smckusick 	static struct disklabel lab;
51830380Smckusick 
51930380Smckusick 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
52043314Smckusick #ifdef COMPAT
52143314Smckusick 		if (disktype) {
52247870Skarels 			struct disklabel *lp, *getdiskbyname();
52343314Smckusick 
52447870Skarels 			unlabeled++;
52547870Skarels 			lp = getdiskbyname(disktype);
52647870Skarels 			if (lp == NULL)
52747870Skarels 				fatal("%s: unknown disk type", disktype);
52847870Skarels 			return (lp);
52943314Smckusick 		}
53043314Smckusick #endif
53165928Spendry 		warn("ioctl (GDINFO)");
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) {
54965928Spendry 		warn("ioctl (WDINFO)");
55030380Smckusick 		fatal("%s: can't rewrite disk label", s);
55110762Ssam 	}
55230446Skarels #if vax
55330446Skarels 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
55430446Skarels 		register i;
55530691Skarels 		int cfd;
55630446Skarels 		daddr_t alt;
55730691Skarels 		char specname[64];
55830691Skarels 		char blk[1024];
55931045Ssam 		char *cp;
56030446Skarels 
56130691Skarels 		/*
56230691Skarels 		 * Make name for 'c' partition.
56330691Skarels 		 */
56430691Skarels 		strcpy(specname, s);
56530691Skarels 		cp = specname + strlen(specname) - 1;
56630691Skarels 		if (!isdigit(*cp))
56730691Skarels 			*cp = 'c';
56830691Skarels 		cfd = open(specname, O_WRONLY);
56947870Skarels 		if (cfd < 0)
57047870Skarels 			fatal("%s: %s", specname, strerror(errno));
57130691Skarels 		bzero(blk, sizeof(blk));
57230691Skarels 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
57330446Skarels 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
57430446Skarels 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
57547870Skarels 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
57647870Skarels 			    L_SET) == -1)
57747870Skarels 				fatal("lseek to badsector area: %s",
57847870Skarels 				    strerror(errno));
57947870Skarels 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
58065928Spendry 				warn("alternate label %d write", i/2);
58130380Smckusick 		}
58239049Smckusick 		close(cfd);
58330380Smckusick 	}
58430446Skarels #endif
58510762Ssam }
58610762Ssam 
58710762Ssam /*VARARGS*/
58850787Storek void
58950787Storek #if __STDC__
59050787Storek fatal(const char *fmt, ...)
59150787Storek #else
59250787Storek fatal(fmt, va_alist)
59310762Ssam 	char *fmt;
59450787Storek 	va_dcl
59550787Storek #endif
59610762Ssam {
59747870Skarels 	va_list ap;
59810762Ssam 
59950787Storek #if __STDC__
60050787Storek 	va_start(ap, fmt);
60150787Storek #else
60250787Storek 	va_start(ap);
60350787Storek #endif
60465928Spendry 	if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
60565928Spendry 		openlog(progname, LOG_CONS, LOG_DAEMON);
60665928Spendry 		vsyslog(LOG_ERR, fmt, ap);
60765928Spendry 		closelog();
60865928Spendry 	} else {
60965928Spendry 		vwarnx(fmt, ap);
61065928Spendry 	}
61147870Skarels 	va_end(ap);
61247870Skarels 	exit(1);
61365928Spendry 	/*NOTREACHED*/
61410762Ssam }
61547870Skarels 
61647870Skarels usage()
61747870Skarels {
61847870Skarels 	if (mfs) {
61947870Skarels 		fprintf(stderr,
62053709Smckusick 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
62153709Smckusick 			progname);
62247870Skarels 	} else
62347870Skarels 		fprintf(stderr,
62453709Smckusick 		    "usage: %s [ -fsoptions ] special-device%s\n",
62553709Smckusick 		    progname,
62647870Skarels #ifdef COMPAT
62747870Skarels 		    " [device-type]");
62847870Skarels #else
62947870Skarels 		    "");
63047870Skarels #endif
63147870Skarels 	fprintf(stderr, "where fsoptions are:\n");
63247870Skarels 	fprintf(stderr,
63347870Skarels 	    "\t-N do not create file system, just print out parameters\n");
63459752Smckusick 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
63547870Skarels 	fprintf(stderr, "\t-S sector size\n");
63647870Skarels #ifdef COMPAT
63747870Skarels 	fprintf(stderr, "\t-T disktype\n");
63847870Skarels #endif
63947870Skarels 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
64047870Skarels 	fprintf(stderr, "\t-b block size\n");
64147870Skarels 	fprintf(stderr, "\t-c cylinders/group\n");
64247870Skarels 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
64347870Skarels 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
64447870Skarels 	fprintf(stderr, "\t-f frag size\n");
64547870Skarels 	fprintf(stderr, "\t-i number of bytes per inode\n");
64647870Skarels 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
64747870Skarels 	fprintf(stderr, "\t-l hardware sector interleave\n");
64847870Skarels 	fprintf(stderr, "\t-m minimum free space %%\n");
64947870Skarels 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
65047870Skarels 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
65147870Skarels 	fprintf(stderr, "\t-p spare sectors per track\n");
65247870Skarels 	fprintf(stderr, "\t-s file system size (sectors)\n");
65347870Skarels 	fprintf(stderr, "\t-r revolutions/minute\n");
65447870Skarels 	fprintf(stderr, "\t-t tracks/cylinder\n");
65547870Skarels 	fprintf(stderr, "\t-u sectors/track\n");
65647870Skarels 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
65747870Skarels 	exit(1);
65847870Skarels }
659