xref: /csrg-svn/sbin/newfs/newfs.c (revision 53915)
121159Sdist /*
239049Smckusick  * Copyright (c) 1983, 1989 The Regents of the University of California.
339049Smckusick  * All rights reserved.
439049Smckusick  *
542704Sbostic  * %sccs.include.redist.c%
621159Sdist  */
721159Sdist 
810762Ssam #ifndef lint
9*53915Smckusick static char sccsid[] = "@(#)newfs.c	6.31 (Berkeley) 06/04/92";
1039049Smckusick #endif /* not lint */
1139049Smckusick 
1239049Smckusick #ifndef lint
1321159Sdist char copyright[] =
1439049Smckusick "@(#) Copyright (c) 1983, 1989 Regents of the University of California.\n\
1521159Sdist  All rights reserved.\n";
1639049Smckusick #endif /* not lint */
1710762Ssam 
1810762Ssam /*
1911069Ssam  * newfs: friendly front end to mkfs
2010762Ssam  */
2110762Ssam #include <sys/param.h>
2210762Ssam #include <sys/stat.h>
2330380Smckusick #include <sys/ioctl.h>
2430380Smckusick #include <sys/disklabel.h>
2530380Smckusick #include <sys/file.h>
2639049Smckusick #include <sys/mount.h>
2751533Sbostic #include <ufs/ufs/dir.h>
2851533Sbostic #include <ufs/ffs/fs.h>
2910762Ssam 
3047870Skarels #include <errno.h>
3150787Storek #if __STDC__
3247870Skarels #include <stdarg.h>
3350787Storek #else
3450787Storek #include <varargs.h>
3550787Storek #endif
36*53915Smckusick #include <unistd.h>
3710762Ssam #include <stdio.h>
3830380Smckusick #include <ctype.h>
3947870Skarels #include <string.h>
4047870Skarels #include <stdlib.h>
4137951Sbostic #include <paths.h>
4210762Ssam 
4350787Storek #if __STDC__
4450787Storek void	fatal(const char *fmt, ...);
4550787Storek #else
4650787Storek void	fatal();
4750787Storek #endif
4850787Storek 
4947870Skarels #define	COMPAT			/* allow non-labeled disks */
5031680Skarels 
5130380Smckusick /*
5230380Smckusick  * The following two constants set the default block and fragment sizes.
5330380Smckusick  * Both constants must be a power of 2 and meet the following constraints:
5430380Smckusick  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
5530380Smckusick  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
5630380Smckusick  *	DESBLKSIZE / DESFRAGSIZE <= 8
5730380Smckusick  */
5830380Smckusick #define	DFL_FRAGSIZE	1024
5930380Smckusick #define	DFL_BLKSIZE	8192
6011183Ssam 
6130380Smckusick /*
6234137Smckusick  * Cylinder groups may have up to many cylinders. The actual
6330380Smckusick  * number used depends upon how much information can be stored
6434137Smckusick  * on a single cylinder. The default is to use 16 cylinders
6530380Smckusick  * per group.
6630380Smckusick  */
6730380Smckusick #define	DESCPG		16	/* desired fs_cpg */
6830380Smckusick 
6930380Smckusick /*
7030380Smckusick  * MINFREE gives the minimum acceptable percentage of file system
7130380Smckusick  * blocks which may be free. If the freelist drops below this level
7230380Smckusick  * only the superuser may continue to allocate blocks. This may
7330380Smckusick  * be set to 0 if no reserve of free blocks is deemed necessary,
7430380Smckusick  * however throughput drops by fifty percent if the file system
7530380Smckusick  * is run at between 90% and 100% full; thus the default value of
7630380Smckusick  * fs_minfree is 10%. With 10% free space, fragmentation is not a
7730380Smckusick  * problem, so we choose to optimize for time.
7830380Smckusick  */
7930380Smckusick #define MINFREE		10
8030380Smckusick #define DEFAULTOPT	FS_OPTTIME
8130380Smckusick 
8230380Smckusick /*
8330380Smckusick  * ROTDELAY gives the minimum number of milliseconds to initiate
8430380Smckusick  * another disk transfer on the same cylinder. It is used in
8530380Smckusick  * determining the rotationally optimal layout for disk blocks
8630380Smckusick  * within a file; the default of fs_rotdelay is 4ms.
8730380Smckusick  */
8830380Smckusick #define ROTDELAY	4
8930380Smckusick 
9030380Smckusick /*
9130380Smckusick  * MAXCONTIG sets the default for the maximum number of blocks
9230380Smckusick  * that may be allocated sequentially. Since UNIX drivers are
9330380Smckusick  * not capable of scheduling multi-block transfers, this defaults
9430380Smckusick  * to 1 (ie no contiguous blocks are allocated).
9530380Smckusick  */
9630380Smckusick #define MAXCONTIG	1
9730380Smckusick 
9830380Smckusick /*
9932309Smckusick  * MAXBLKPG determines the maximum number of data blocks which are
10032309Smckusick  * placed in a single cylinder group. The default is one indirect
10132309Smckusick  * block worth of data blocks.
10232309Smckusick  */
10332309Smckusick #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
10432309Smckusick 
10532309Smckusick /*
10630380Smckusick  * Each file system has a number of inodes statically allocated.
10739049Smckusick  * We allocate one inode slot per NFPI fragments, expecting this
10830380Smckusick  * to be far more than we will ever need.
10930380Smckusick  */
11039049Smckusick #define	NFPI		4
11130380Smckusick 
11234137Smckusick /*
11334137Smckusick  * For each cylinder we keep track of the availability of blocks at different
11434137Smckusick  * rotational positions, so that we can lay out the data to be picked
11534137Smckusick  * up with minimum rotational latency.  NRPOS is the default number of
11634137Smckusick  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
11734137Smckusick  * of our summary information is 2ms for a typical 3600 rpm drive.
11834137Smckusick  */
11934137Smckusick #define	NRPOS		8	/* number distinct rotational positions */
12034137Smckusick 
12134137Smckusick 
12239325Smckusick int	mfs;			/* run as the memory based filesystem */
12330380Smckusick int	Nflag;			/* run without writing file system */
12410762Ssam int	fssize;			/* file system size */
12510762Ssam int	ntracks;		/* # tracks/cylinder */
12610762Ssam int	nsectors;		/* # sectors/track */
12730386Smckusick int	nphyssectors;		/* # sectors/track including spares */
12830380Smckusick int	secpercyl;		/* sectors per cylinder */
12930386Smckusick int	trackspares = -1;	/* spare sectors per track */
13030386Smckusick int	cylspares = -1;		/* spare sectors per cylinder */
13110762Ssam int	sectorsize;		/* bytes/sector */
13230691Skarels #ifdef tahoe
13330691Skarels int	realsectorsize;		/* bytes/sector in hardware */
13430691Skarels #endif
13511069Ssam int	rpm;			/* revolutions/minute of drive */
13630386Smckusick int	interleave;		/* hardware sector interleave */
13730386Smckusick int	trackskew = -1;		/* sector 0 skew, per track */
13830386Smckusick int	headswitch;		/* head switch time, usec */
13930386Smckusick int	trackseek;		/* track-to-track seek, usec */
14030862Skarels int	fsize = 0;		/* fragment size */
14130862Skarels int	bsize = 0;		/* block size */
14230380Smckusick int	cpg = DESCPG;		/* cylinders/cylinder group */
14332119Smckusick int	cpgflg;			/* cylinders/cylinder group flag was given */
14430380Smckusick int	minfree = MINFREE;	/* free space threshold */
14530380Smckusick int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
14639049Smckusick int	density;		/* number of bytes per inode */
14730380Smckusick int	maxcontig = MAXCONTIG;	/* max contiguous blocks to allocate */
14830380Smckusick int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
14932309Smckusick int	maxbpg;			/* maximum blocks per file in a cyl group */
15034137Smckusick int	nrpos = NRPOS;		/* # of distinguished rotational positions */
15130691Skarels int	bbsize = BBSIZE;	/* boot block size */
15230691Skarels int	sbsize = SBSIZE;	/* superblock size */
15339325Smckusick int	mntflags;		/* flags to be passed to mount */
15439049Smckusick u_long	memleft;		/* virtual memory available */
15539049Smckusick caddr_t	membase;		/* start address of memory based filesystem */
15631680Skarels #ifdef COMPAT
15743314Smckusick char	*disktype;
15847870Skarels int	unlabeled;
15931680Skarels #endif
16010762Ssam 
16110762Ssam char	device[MAXPATHLEN];
16239049Smckusick char	*progname;
16310762Ssam 
16410762Ssam main(argc, argv)
16514064Smckusick 	int argc;
16610762Ssam 	char *argv[];
16710762Ssam {
16847870Skarels 	extern char *optarg;
16947870Skarels 	extern int optind;
17047870Skarels 	register int ch;
17110762Ssam 	register struct partition *pp;
17230380Smckusick 	register struct disklabel *lp;
17330380Smckusick 	struct disklabel *getdisklabel();
17430380Smckusick 	struct partition oldpartition;
17510762Ssam 	struct stat st;
17630380Smckusick 	int fsi, fso;
17747870Skarels 	char *cp, *special, *opstring, buf[BUFSIZ];
17853709Smckusick 	struct statfs *mp;
17953709Smckusick 	char *s1, *s2;
18053709Smckusick 	int len, n;
18110762Ssam 
18247870Skarels 	if (progname = rindex(*argv, '/'))
18347870Skarels 		++progname;
18447870Skarels 	else
18539049Smckusick 		progname = *argv;
18647870Skarels 
18750402Smckusick 	if (strstr(progname, "mfs")) {
18847870Skarels 		mfs = 1;
18939049Smckusick 		Nflag++;
19039049Smckusick 	}
19110762Ssam 
19247870Skarels 	opstring = "F:NS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
19347870Skarels 	if (!mfs)
19447870Skarels 		opstring += 2;		/* -F is mfs only */
19539325Smckusick 
19647870Skarels 	while ((ch = getopt(argc, argv, opstring)) != EOF)
19747870Skarels 		switch(ch) {
19847870Skarels 		case 'F':
19947870Skarels 			if ((mntflags = atoi(optarg)) == 0)
20047870Skarels 				fatal("%s: bad mount flags", optarg);
20147870Skarels 			break;
20247870Skarels 		case 'N':
20347870Skarels 			Nflag++;
20447870Skarels 			break;
20547870Skarels 		case 'S':
20647870Skarels 			if ((sectorsize = atoi(optarg)) <= 0)
20747870Skarels 				fatal("%s: bad sector size", optarg);
20847870Skarels 			break;
20943314Smckusick #ifdef COMPAT
21047870Skarels 		case 'T':
21147870Skarels 			disktype = optarg;
21247870Skarels 			break;
21343314Smckusick #endif
21447870Skarels 		case 'a':
21547870Skarels 			if ((maxcontig = atoi(optarg)) <= 0)
21647870Skarels 				fatal("%s: bad max contiguous blocks\n",
21747870Skarels 				    optarg);
21847870Skarels 			break;
21947870Skarels 		case 'b':
22047870Skarels 			if ((bsize = atoi(optarg)) < MINBSIZE)
22147870Skarels 				fatal("%s: bad block size", optarg);
22247870Skarels 			break;
22347870Skarels 		case 'c':
22447870Skarels 			if ((cpg = atoi(optarg)) <= 0)
22547870Skarels 				fatal("%s: bad cylinders/group", optarg);
22647870Skarels 			cpgflg++;
22747870Skarels 			break;
22847870Skarels 		case 'd':
22947870Skarels 			if ((rotdelay = atoi(optarg)) < 0)
23047870Skarels 				fatal("%s: bad rotational delay\n", optarg);
23147870Skarels 			break;
23247870Skarels 		case 'e':
23347870Skarels 			if ((maxbpg = atoi(optarg)) <= 0)
23447870Skarels 				fatal("%s: bad blocks per file in a cyl group\n",
23547870Skarels 				    optarg);
23647870Skarels 			break;
23747870Skarels 		case 'f':
23847870Skarels 			if ((fsize = atoi(optarg)) <= 0)
23947870Skarels 				fatal("%s: bad frag size", optarg);
24047870Skarels 			break;
24147870Skarels 		case 'i':
24247870Skarels 			if ((density = atoi(optarg)) <= 0)
24347870Skarels 				fatal("%s: bad bytes per inode\n", optarg);
24447870Skarels 			break;
24547870Skarels 		case 'k':
24647870Skarels 			if ((trackskew = atoi(optarg)) < 0)
24747870Skarels 				fatal("%s: bad track skew", optarg);
24847870Skarels 			break;
24947870Skarels 		case 'l':
25047870Skarels 			if ((interleave = atoi(optarg)) <= 0)
25147870Skarels 				fatal("%s: bad interleave", optarg);
25247870Skarels 			break;
25347870Skarels 		case 'm':
25447870Skarels 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
25547870Skarels 				fatal("%s: bad free space %%\n", optarg);
25647870Skarels 			break;
25747870Skarels 		case 'n':
25847870Skarels 			if ((nrpos = atoi(optarg)) <= 0)
25947870Skarels 				fatal("%s: bad rotational layout count\n",
26047870Skarels 				    optarg);
26147870Skarels 			break;
26247870Skarels 		case 'o':
26347870Skarels 			if (strcmp(optarg, "space") == 0)
26447870Skarels 				opt = FS_OPTSPACE;
26547870Skarels 			else if (strcmp(optarg, "time") == 0)
26647870Skarels 				opt = FS_OPTTIME;
26747870Skarels 			else
26847870Skarels 				fatal("%s: bad optimization preference %s",
26947870Skarels 				    optarg, "(options are `space' or `time')");
27047870Skarels 			break;
27147870Skarels 		case 'p':
27247870Skarels 			if ((trackspares = atoi(optarg)) < 0)
27347870Skarels 				fatal("%s: bad spare sectors per track",
27447870Skarels 				    optarg);
27547870Skarels 			break;
27647870Skarels 		case 'r':
27747870Skarels 			if ((rpm = atoi(optarg)) <= 0)
27847870Skarels 				fatal("%s: bad revs/minute\n", optarg);
27947870Skarels 			break;
28047870Skarels 		case 's':
28147870Skarels 			if ((fssize = atoi(optarg)) <= 0)
28247870Skarels 				fatal("%s: bad file system size", optarg);
28347870Skarels 			break;
28447870Skarels 		case 't':
28547870Skarels 			if ((ntracks = atoi(optarg)) <= 0)
28647870Skarels 				fatal("%s: bad total tracks", optarg);
28747870Skarels 			break;
28847870Skarels 		case 'u':
28947870Skarels 			if ((nsectors = atoi(optarg)) <= 0)
29047870Skarels 				fatal("%s: bad sectors/track", optarg);
29147870Skarels 			break;
29247870Skarels 		case 'x':
29347870Skarels 			if ((cylspares = atoi(optarg)) < 0)
29447870Skarels 				fatal("%s: bad spare sectors per cylinder",
29547870Skarels 				    optarg);
29647870Skarels 			break;
29747870Skarels 		case '?':
29847870Skarels 		default:
29947870Skarels 			usage();
30047870Skarels 		}
30147870Skarels 	argc -= optind;
30247870Skarels 	argv += optind;
30343314Smckusick 
30447870Skarels 	if (argc != 2 && (mfs || argc != 1))
30547870Skarels 		usage();
30610762Ssam 
30710762Ssam 	special = argv[0];
30814064Smckusick 	cp = rindex(special, '/');
30947870Skarels 	if (cp == 0) {
31047870Skarels 		/*
31147870Skarels 		 * No path prefix; try /dev/r%s then /dev/%s.
31247870Skarels 		 */
31347870Skarels 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
31447870Skarels 		if (stat(device, &st) == -1)
31547870Skarels 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
31647870Skarels 		special = device;
31747870Skarels 	}
31853709Smckusick 	if (Nflag) {
31953709Smckusick 		fso = -1;
32053709Smckusick 	} else {
32130380Smckusick 		fso = open(special, O_WRONLY);
32247870Skarels 		if (fso < 0)
32347870Skarels 			fatal("%s: %s", special, strerror(errno));
32453709Smckusick 
32553709Smckusick 		/* Bail if target special is mounted */
32653709Smckusick 		n = getmntinfo(&mp, MNT_NOWAIT);
32753709Smckusick 		if (n == 0)
32853709Smckusick 			fatal("%s: getmntinfo: %s", special, strerror(errno));
32953709Smckusick 
33053709Smckusick 		len = sizeof(_PATH_DEV) - 1;
33153709Smckusick 		s1 = special;
33253709Smckusick 		if (strncmp(_PATH_DEV, s1, len) == 0)
33353709Smckusick 			s1 += len;
33453709Smckusick 
33553709Smckusick 		while (--n >= 0) {
33653709Smckusick 			s2 = mp->f_mntfromname;
33753709Smckusick 			if (strncmp(_PATH_DEV, s2, len) == 0) {
33853709Smckusick 				s2 += len - 1;
33953709Smckusick 				*s2 = 'r';
34053709Smckusick 			}
34153709Smckusick 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
34253709Smckusick 				fatal("%s is mounted on %s",
34353709Smckusick 				    special, mp->f_mntonname);
34453709Smckusick 			++mp;
34553709Smckusick 		}
34653709Smckusick 	}
34730380Smckusick 	fsi = open(special, O_RDONLY);
34847870Skarels 	if (fsi < 0)
34947870Skarels 		fatal("%s: %s", special, strerror(errno));
35047870Skarels 	if (fstat(fsi, &st) < 0)
35147870Skarels 		fatal("%s: %s", special, strerror(errno));
35247870Skarels 	if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
35347870Skarels 		printf("%s: %s: not a character-special device\n",
35447870Skarels 		    progname, special);
35510762Ssam 	cp = index(argv[0], '\0') - 1;
35630380Smckusick 	if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
35710762Ssam 		fatal("%s: can't figure out file system partition", argv[0]);
35831680Skarels #ifdef COMPAT
35943314Smckusick 	if (!mfs && disktype == NULL)
36043314Smckusick 		disktype = argv[1];
36143314Smckusick #endif
36230380Smckusick 	lp = getdisklabel(special, fsi);
36330380Smckusick 	if (isdigit(*cp))
36430380Smckusick 		pp = &lp->d_partitions[0];
36530380Smckusick 	else
36630380Smckusick 		pp = &lp->d_partitions[*cp - 'a'];
36730380Smckusick 	if (pp->p_size == 0)
36830380Smckusick 		fatal("%s: `%c' partition is unavailable", argv[0], *cp);
36930380Smckusick 	if (fssize == 0)
37010762Ssam 		fssize = pp->p_size;
37139325Smckusick 	if (fssize > pp->p_size && !mfs)
37230380Smckusick 	       fatal("%s: maximum file system size on the `%c' partition is %d",
37330380Smckusick 			argv[0], *cp, pp->p_size);
37430380Smckusick 	if (rpm == 0) {
37530380Smckusick 		rpm = lp->d_rpm;
37630380Smckusick 		if (rpm <= 0)
37730743Skarels 			rpm = 3600;
37810762Ssam 	}
37930380Smckusick 	if (ntracks == 0) {
38030380Smckusick 		ntracks = lp->d_ntracks;
38130380Smckusick 		if (ntracks <= 0)
38230743Skarels 			fatal("%s: no default #tracks", argv[0]);
38330380Smckusick 	}
38410762Ssam 	if (nsectors == 0) {
38530380Smckusick 		nsectors = lp->d_nsectors;
38630380Smckusick 		if (nsectors <= 0)
38730743Skarels 			fatal("%s: no default #sectors/track", argv[0]);
38810762Ssam 	}
38910762Ssam 	if (sectorsize == 0) {
39030380Smckusick 		sectorsize = lp->d_secsize;
39130380Smckusick 		if (sectorsize <= 0)
39230743Skarels 			fatal("%s: no default sector size", argv[0]);
39310762Ssam 	}
39430386Smckusick 	if (trackskew == -1) {
39530386Smckusick 		trackskew = lp->d_trackskew;
39630386Smckusick 		if (trackskew < 0)
39730743Skarels 			trackskew = 0;
39830386Smckusick 	}
39930386Smckusick 	if (interleave == 0) {
40030386Smckusick 		interleave = lp->d_interleave;
40130386Smckusick 		if (interleave <= 0)
40230743Skarels 			interleave = 1;
40330386Smckusick 	}
40410762Ssam 	if (fsize == 0) {
40510762Ssam 		fsize = pp->p_fsize;
40630380Smckusick 		if (fsize <= 0)
40730380Smckusick 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
40810762Ssam 	}
40930380Smckusick 	if (bsize == 0) {
41030380Smckusick 		bsize = pp->p_frag * pp->p_fsize;
41130380Smckusick 		if (bsize <= 0)
41230380Smckusick 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
41311069Ssam 	}
41439049Smckusick 	if (density == 0)
41539049Smckusick 		density = NFPI * fsize;
41624702Smckusick 	if (minfree < 10 && opt != FS_OPTSPACE) {
41730380Smckusick 		fprintf(stderr, "Warning: changing optimization to space ");
41830380Smckusick 		fprintf(stderr, "because minfree is less than 10%%\n");
41924702Smckusick 		opt = FS_OPTSPACE;
42024702Smckusick 	}
42130386Smckusick 	if (trackspares == -1) {
42230386Smckusick 		trackspares = lp->d_sparespertrack;
42330386Smckusick 		if (trackspares < 0)
42430743Skarels 			trackspares = 0;
42530386Smckusick 	}
42630386Smckusick 	nphyssectors = nsectors + trackspares;
42730386Smckusick 	if (cylspares == -1) {
42830386Smckusick 		cylspares = lp->d_sparespercyl;
42930386Smckusick 		if (cylspares < 0)
43030743Skarels 			cylspares = 0;
43130386Smckusick 	}
43230386Smckusick 	secpercyl = nsectors * ntracks - cylspares;
43330380Smckusick 	if (secpercyl != lp->d_secpercyl)
43447870Skarels 		fprintf(stderr, "%s (%d) %s (%lu)\n",
43530380Smckusick 			"Warning: calculated sectors per cylinder", secpercyl,
43630380Smckusick 			"disagrees with disk label", lp->d_secpercyl);
43732321Smckusick 	if (maxbpg == 0)
43832321Smckusick 		maxbpg = MAXBLKPG(bsize);
43930386Smckusick 	headswitch = lp->d_headswitch;
44030386Smckusick 	trackseek = lp->d_trkseek;
44147870Skarels #ifdef notdef /* label may be 0 if faked up by kernel */
44230691Skarels 	bbsize = lp->d_bbsize;
44330691Skarels 	sbsize = lp->d_sbsize;
44445836Skarels #endif
44530380Smckusick 	oldpartition = *pp;
44630691Skarels #ifdef tahoe
44730691Skarels 	realsectorsize = sectorsize;
44830862Skarels 	if (sectorsize != DEV_BSIZE) {		/* XXX */
44930691Skarels 		int secperblk = DEV_BSIZE / sectorsize;
45030691Skarels 
45130691Skarels 		sectorsize = DEV_BSIZE;
45230691Skarels 		nsectors /= secperblk;
45330691Skarels 		nphyssectors /= secperblk;
45430691Skarels 		secpercyl /= secperblk;
45530691Skarels 		fssize /= secperblk;
45630691Skarels 		pp->p_size /= secperblk;
45730691Skarels 	}
45830691Skarels #endif
45930380Smckusick 	mkfs(pp, special, fsi, fso);
46030691Skarels #ifdef tahoe
46130691Skarels 	if (realsectorsize != DEV_BSIZE)
46230691Skarels 		pp->p_size *= DEV_BSIZE / realsectorsize;
46330691Skarels #endif
46430380Smckusick 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
46530380Smckusick 		rewritelabel(special, fso, lp);
46639049Smckusick 	if (!Nflag)
46739049Smckusick 		close(fso);
46839049Smckusick 	close(fsi);
46949063Sbostic #ifdef MFS
47039325Smckusick 	if (mfs) {
47149063Sbostic 		struct mfs_args args;
47249063Sbostic 
47339325Smckusick 		sprintf(buf, "mfs:%d", getpid());
47439049Smckusick 		args.name = buf;
47539049Smckusick 		args.base = membase;
47639049Smckusick 		args.size = fssize * sectorsize;
47747870Skarels 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
47847870Skarels 			fatal("%s: %s", argv[1], strerror(errno));
47939049Smckusick 	}
48049063Sbostic #endif
48130380Smckusick 	exit(0);
48230380Smckusick }
48330380Smckusick 
48431680Skarels #ifdef COMPAT
48543314Smckusick char lmsg[] = "%s: can't read disk label; disk type must be specified";
48643314Smckusick #else
48743314Smckusick char lmsg[] = "%s: can't read disk label";
48843314Smckusick #endif
48931680Skarels 
49031680Skarels struct disklabel *
49130380Smckusick getdisklabel(s, fd)
49230380Smckusick 	char *s;
49331680Skarels 	int fd;
49430380Smckusick {
49530380Smckusick 	static struct disklabel lab;
49630380Smckusick 
49730380Smckusick 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
49843314Smckusick #ifdef COMPAT
49943314Smckusick 		if (disktype) {
50047870Skarels 			struct disklabel *lp, *getdiskbyname();
50143314Smckusick 
50247870Skarels 			unlabeled++;
50347870Skarels 			lp = getdiskbyname(disktype);
50447870Skarels 			if (lp == NULL)
50547870Skarels 				fatal("%s: unknown disk type", disktype);
50647870Skarels 			return (lp);
50743314Smckusick 		}
50843314Smckusick #endif
50947870Skarels 		(void)fprintf(stderr,
51047870Skarels 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
51143314Smckusick 		fatal(lmsg, s);
51210762Ssam 	}
51330380Smckusick 	return (&lab);
51430380Smckusick }
51510762Ssam 
51630380Smckusick rewritelabel(s, fd, lp)
51730380Smckusick 	char *s;
51830380Smckusick 	int fd;
51930380Smckusick 	register struct disklabel *lp;
52030380Smckusick {
52131680Skarels #ifdef COMPAT
52247870Skarels 	if (unlabeled)
52331680Skarels 		return;
52431680Skarels #endif
52530380Smckusick 	lp->d_checksum = 0;
52630380Smckusick 	lp->d_checksum = dkcksum(lp);
52730380Smckusick 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
52847870Skarels 		(void)fprintf(stderr,
52947870Skarels 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
53030380Smckusick 		fatal("%s: can't rewrite disk label", s);
53110762Ssam 	}
53230446Skarels #if vax
53330446Skarels 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
53430446Skarels 		register i;
53530691Skarels 		int cfd;
53630446Skarels 		daddr_t alt;
53730691Skarels 		char specname[64];
53830691Skarels 		char blk[1024];
53931045Ssam 		char *cp;
54030446Skarels 
54130691Skarels 		/*
54230691Skarels 		 * Make name for 'c' partition.
54330691Skarels 		 */
54430691Skarels 		strcpy(specname, s);
54530691Skarels 		cp = specname + strlen(specname) - 1;
54630691Skarels 		if (!isdigit(*cp))
54730691Skarels 			*cp = 'c';
54830691Skarels 		cfd = open(specname, O_WRONLY);
54947870Skarels 		if (cfd < 0)
55047870Skarels 			fatal("%s: %s", specname, strerror(errno));
55130691Skarels 		bzero(blk, sizeof(blk));
55230691Skarels 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
55330446Skarels 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
55430446Skarels 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
55547870Skarels 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
55647870Skarels 			    L_SET) == -1)
55747870Skarels 				fatal("lseek to badsector area: %s",
55847870Skarels 				    strerror(errno));
55947870Skarels 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
56047870Skarels 				fprintf(stderr,
56147870Skarels 				    "%s: alternate label %d write: %s\n",
56247870Skarels 				    progname, i/2, strerror(errno));
56330380Smckusick 		}
56439049Smckusick 		close(cfd);
56530380Smckusick 	}
56630446Skarels #endif
56710762Ssam }
56810762Ssam 
56910762Ssam /*VARARGS*/
57050787Storek void
57150787Storek #if __STDC__
57250787Storek fatal(const char *fmt, ...)
57350787Storek #else
57450787Storek fatal(fmt, va_alist)
57510762Ssam 	char *fmt;
57650787Storek 	va_dcl
57750787Storek #endif
57810762Ssam {
57947870Skarels 	va_list ap;
58010762Ssam 
58150787Storek #if __STDC__
58250787Storek 	va_start(ap, fmt);
58350787Storek #else
58450787Storek 	va_start(ap);
58550787Storek #endif
58639049Smckusick 	fprintf(stderr, "%s: ", progname);
58747870Skarels 	(void)vfprintf(stderr, fmt, ap);
58847870Skarels 	va_end(ap);
58910762Ssam 	putc('\n', stderr);
59047870Skarels 	exit(1);
59110762Ssam }
59247870Skarels 
59347870Skarels usage()
59447870Skarels {
59547870Skarels 	if (mfs) {
59647870Skarels 		fprintf(stderr,
59753709Smckusick 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
59853709Smckusick 			progname);
59947870Skarels 	} else
60047870Skarels 		fprintf(stderr,
60153709Smckusick 		    "usage: %s [ -fsoptions ] special-device%s\n",
60253709Smckusick 		    progname,
60347870Skarels #ifdef COMPAT
60447870Skarels 		    " [device-type]");
60547870Skarels #else
60647870Skarels 		    "");
60747870Skarels #endif
60847870Skarels 	fprintf(stderr, "where fsoptions are:\n");
60947870Skarels 	fprintf(stderr,
61047870Skarels 	    "\t-N do not create file system, just print out parameters\n");
61147870Skarels 	fprintf(stderr, "\t-S sector size\n");
61247870Skarels #ifdef COMPAT
61347870Skarels 	fprintf(stderr, "\t-T disktype\n");
61447870Skarels #endif
61547870Skarels 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
61647870Skarels 	fprintf(stderr, "\t-b block size\n");
61747870Skarels 	fprintf(stderr, "\t-c cylinders/group\n");
61847870Skarels 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
61947870Skarels 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
62047870Skarels 	fprintf(stderr, "\t-f frag size\n");
62147870Skarels 	fprintf(stderr, "\t-i number of bytes per inode\n");
62247870Skarels 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
62347870Skarels 	fprintf(stderr, "\t-l hardware sector interleave\n");
62447870Skarels 	fprintf(stderr, "\t-m minimum free space %%\n");
62547870Skarels 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
62647870Skarels 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
62747870Skarels 	fprintf(stderr, "\t-p spare sectors per track\n");
62847870Skarels 	fprintf(stderr, "\t-s file system size (sectors)\n");
62947870Skarels 	fprintf(stderr, "\t-r revolutions/minute\n");
63047870Skarels 	fprintf(stderr, "\t-t tracks/cylinder\n");
63147870Skarels 	fprintf(stderr, "\t-u sectors/track\n");
63247870Skarels 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
63347870Skarels 	exit(1);
63447870Skarels }
635