xref: /csrg-svn/sbin/newfs/newfs.c (revision 44970)
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*44970Sbostic static char sccsid[] = "@(#)newfs.c	6.23 (Berkeley) 07/25/90";
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>
2338380Smckusick #include <ufs/fs.h>
2438380Smckusick #include <ufs/dir.h>
2530380Smckusick #include <sys/ioctl.h>
2630380Smckusick #include <sys/disklabel.h>
2730380Smckusick #include <sys/file.h>
2839049Smckusick #include <sys/mount.h>
2910762Ssam 
30*44970Sbostic #include <errno.h>
31*44970Sbostic #include <stdarg.h>
3210762Ssam #include <stdio.h>
3330380Smckusick #include <ctype.h>
34*44970Sbostic #include <string.h>
35*44970Sbostic #include <stdlib.h>
3637951Sbostic #include <paths.h>
3710762Ssam 
38*44970Sbostic #define	COMPAT			/* allow non-labeled disks */
3931680Skarels 
4030380Smckusick /*
4130380Smckusick  * The following two constants set the default block and fragment sizes.
4230380Smckusick  * Both constants must be a power of 2 and meet the following constraints:
4330380Smckusick  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
4430380Smckusick  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
4530380Smckusick  *	DESBLKSIZE / DESFRAGSIZE <= 8
4630380Smckusick  */
4730380Smckusick #define	DFL_FRAGSIZE	1024
4830380Smckusick #define	DFL_BLKSIZE	8192
4911183Ssam 
5030380Smckusick /*
5134137Smckusick  * Cylinder groups may have up to many cylinders. The actual
5230380Smckusick  * number used depends upon how much information can be stored
5334137Smckusick  * on a single cylinder. The default is to use 16 cylinders
5430380Smckusick  * per group.
5530380Smckusick  */
5630380Smckusick #define	DESCPG		16	/* desired fs_cpg */
5730380Smckusick 
5830380Smckusick /*
5930380Smckusick  * MINFREE gives the minimum acceptable percentage of file system
6030380Smckusick  * blocks which may be free. If the freelist drops below this level
6130380Smckusick  * only the superuser may continue to allocate blocks. This may
6230380Smckusick  * be set to 0 if no reserve of free blocks is deemed necessary,
6330380Smckusick  * however throughput drops by fifty percent if the file system
6430380Smckusick  * is run at between 90% and 100% full; thus the default value of
6530380Smckusick  * fs_minfree is 10%. With 10% free space, fragmentation is not a
6630380Smckusick  * problem, so we choose to optimize for time.
6730380Smckusick  */
6830380Smckusick #define MINFREE		10
6930380Smckusick #define DEFAULTOPT	FS_OPTTIME
7030380Smckusick 
7130380Smckusick /*
7230380Smckusick  * ROTDELAY gives the minimum number of milliseconds to initiate
7330380Smckusick  * another disk transfer on the same cylinder. It is used in
7430380Smckusick  * determining the rotationally optimal layout for disk blocks
7530380Smckusick  * within a file; the default of fs_rotdelay is 4ms.
7630380Smckusick  */
7730380Smckusick #define ROTDELAY	4
7830380Smckusick 
7930380Smckusick /*
8030380Smckusick  * MAXCONTIG sets the default for the maximum number of blocks
8130380Smckusick  * that may be allocated sequentially. Since UNIX drivers are
8230380Smckusick  * not capable of scheduling multi-block transfers, this defaults
8330380Smckusick  * to 1 (ie no contiguous blocks are allocated).
8430380Smckusick  */
8530380Smckusick #define MAXCONTIG	1
8630380Smckusick 
8730380Smckusick /*
8832309Smckusick  * MAXBLKPG determines the maximum number of data blocks which are
8932309Smckusick  * placed in a single cylinder group. The default is one indirect
9032309Smckusick  * block worth of data blocks.
9132309Smckusick  */
9232309Smckusick #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
9332309Smckusick 
9432309Smckusick /*
9530380Smckusick  * Each file system has a number of inodes statically allocated.
9639049Smckusick  * We allocate one inode slot per NFPI fragments, expecting this
9730380Smckusick  * to be far more than we will ever need.
9830380Smckusick  */
9939049Smckusick #define	NFPI		4
10030380Smckusick 
10134137Smckusick /*
10234137Smckusick  * For each cylinder we keep track of the availability of blocks at different
10334137Smckusick  * rotational positions, so that we can lay out the data to be picked
10434137Smckusick  * up with minimum rotational latency.  NRPOS is the default number of
10534137Smckusick  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
10634137Smckusick  * of our summary information is 2ms for a typical 3600 rpm drive.
10734137Smckusick  */
10834137Smckusick #define	NRPOS		8	/* number distinct rotational positions */
10934137Smckusick 
11034137Smckusick 
11139325Smckusick int	mfs;			/* run as the memory based filesystem */
11230380Smckusick int	Nflag;			/* run without writing file system */
11310762Ssam int	fssize;			/* file system size */
11410762Ssam int	ntracks;		/* # tracks/cylinder */
11510762Ssam int	nsectors;		/* # sectors/track */
11630386Smckusick int	nphyssectors;		/* # sectors/track including spares */
11730380Smckusick int	secpercyl;		/* sectors per cylinder */
11830386Smckusick int	trackspares = -1;	/* spare sectors per track */
11930386Smckusick int	cylspares = -1;		/* spare sectors per cylinder */
12010762Ssam int	sectorsize;		/* bytes/sector */
12130691Skarels #ifdef tahoe
12230691Skarels int	realsectorsize;		/* bytes/sector in hardware */
12330691Skarels #endif
12411069Ssam int	rpm;			/* revolutions/minute of drive */
12530386Smckusick int	interleave;		/* hardware sector interleave */
12630386Smckusick int	trackskew = -1;		/* sector 0 skew, per track */
12730386Smckusick int	headswitch;		/* head switch time, usec */
12830386Smckusick int	trackseek;		/* track-to-track seek, usec */
12930862Skarels int	fsize = 0;		/* fragment size */
13030862Skarels int	bsize = 0;		/* block size */
13130380Smckusick int	cpg = DESCPG;		/* cylinders/cylinder group */
13232119Smckusick int	cpgflg;			/* cylinders/cylinder group flag was given */
13330380Smckusick int	minfree = MINFREE;	/* free space threshold */
13430380Smckusick int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
13539049Smckusick int	density;		/* number of bytes per inode */
13630380Smckusick int	maxcontig = MAXCONTIG;	/* max contiguous blocks to allocate */
13730380Smckusick int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
13832309Smckusick int	maxbpg;			/* maximum blocks per file in a cyl group */
13934137Smckusick int	nrpos = NRPOS;		/* # of distinguished rotational positions */
14030691Skarels int	bbsize = BBSIZE;	/* boot block size */
14130691Skarels int	sbsize = SBSIZE;	/* superblock size */
14239325Smckusick int	mntflags;		/* flags to be passed to mount */
14339049Smckusick u_long	memleft;		/* virtual memory available */
14439049Smckusick caddr_t	membase;		/* start address of memory based filesystem */
14531680Skarels #ifdef COMPAT
14643314Smckusick char	*disktype;
147*44970Sbostic int	unlabeled;
14831680Skarels #endif
14910762Ssam 
15010762Ssam char	device[MAXPATHLEN];
15139049Smckusick char	*progname;
15210762Ssam 
15310762Ssam main(argc, argv)
15414064Smckusick 	int argc;
15510762Ssam 	char *argv[];
15610762Ssam {
157*44970Sbostic 	extern char *optarg;
158*44970Sbostic 	extern int optind;
159*44970Sbostic 	register int ch;
16010762Ssam 	register struct partition *pp;
16130380Smckusick 	register struct disklabel *lp;
16230380Smckusick 	struct disklabel *getdisklabel();
16330380Smckusick 	struct partition oldpartition;
16439049Smckusick 	struct mfs_args args;
16510762Ssam 	struct stat st;
16630380Smckusick 	int fsi, fso;
167*44970Sbostic 	char *cp, *special, *opstring, buf[BUFSIZ];
16810762Ssam 
169*44970Sbostic 	if (progname = rindex(*argv, '/'))
170*44970Sbostic 		++progname;
171*44970Sbostic 	else
17239049Smckusick 		progname = *argv;
173*44970Sbostic 
17439325Smckusick 	if (!strcmp(progname, "mfs")) {
175*44970Sbostic 		mfs = 1;
17639049Smckusick 		Nflag++;
17739049Smckusick 	}
17810762Ssam 
179*44970Sbostic 	opstring = "F:NS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
180*44970Sbostic 	if (!mfs)
181*44970Sbostic 		opstring += 2;		/* -F is mfs only */
18239325Smckusick 
183*44970Sbostic 	while ((ch = getopt(argc, argv, opstring)) != EOF)
184*44970Sbostic 		switch(ch) {
185*44970Sbostic 		case 'F':
186*44970Sbostic 			if ((mntflags = atoi(optarg)) == 0)
187*44970Sbostic 				fatal("%s: bad mount flags", optarg);
188*44970Sbostic 			break;
189*44970Sbostic 		case 'N':
190*44970Sbostic 			Nflag++;
191*44970Sbostic 			break;
192*44970Sbostic 		case 'S':
193*44970Sbostic 			if ((sectorsize = atoi(optarg)) <= 0)
194*44970Sbostic 				fatal("%s: bad sector size", optarg);
195*44970Sbostic 			break;
19643314Smckusick #ifdef COMPAT
197*44970Sbostic 		case 'T':
198*44970Sbostic 			disktype = optarg;
199*44970Sbostic 			break;
20043314Smckusick #endif
201*44970Sbostic 		case 'a':
202*44970Sbostic 			if ((maxcontig = atoi(optarg)) <= 0)
203*44970Sbostic 				fatal("%s: bad max contiguous blocks\n",
204*44970Sbostic 				    optarg);
205*44970Sbostic 			break;
206*44970Sbostic 		case 'b':
207*44970Sbostic 			if ((bsize = atoi(optarg)) < MINBSIZE)
208*44970Sbostic 				fatal("%s: bad block size", optarg);
209*44970Sbostic 			break;
210*44970Sbostic 		case 'c':
211*44970Sbostic 			if ((cpg = atoi(optarg)) <= 0)
212*44970Sbostic 				fatal("%s: bad cylinders/group", optarg);
213*44970Sbostic 			cpgflg++;
214*44970Sbostic 			break;
215*44970Sbostic 		case 'd':
216*44970Sbostic 			if ((rotdelay = atoi(optarg)) < 0)
217*44970Sbostic 				fatal("%s: bad rotational delay\n", optarg);
218*44970Sbostic 			break;
219*44970Sbostic 		case 'e':
220*44970Sbostic 			if ((maxbpg = atoi(optarg)) <= 0)
221*44970Sbostic 				fatal("%s: bad blocks per file in a cyl group\n",
222*44970Sbostic 				    optarg);
223*44970Sbostic 			break;
224*44970Sbostic 		case 'f':
225*44970Sbostic 			if ((fsize = atoi(optarg)) <= 0)
226*44970Sbostic 				fatal("%s: bad frag size", optarg);
227*44970Sbostic 			break;
228*44970Sbostic 		case 'i':
229*44970Sbostic 			if ((density = atoi(optarg)) <= 0)
230*44970Sbostic 				fatal("%s: bad bytes per inode\n", optarg);
231*44970Sbostic 			break;
232*44970Sbostic 		case 'k':
233*44970Sbostic 			if ((trackskew = atoi(optarg)) < 0)
234*44970Sbostic 				fatal("%s: bad track skew", optarg);
235*44970Sbostic 			break;
236*44970Sbostic 		case 'l':
237*44970Sbostic 			if ((interleave = atoi(optarg)) <= 0)
238*44970Sbostic 				fatal("%s: bad interleave", optarg);
239*44970Sbostic 			break;
240*44970Sbostic 		case 'm':
241*44970Sbostic 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
242*44970Sbostic 				fatal("%s: bad free space %%\n", optarg);
243*44970Sbostic 			break;
244*44970Sbostic 		case 'n':
245*44970Sbostic 			if ((nrpos = atoi(optarg)) <= 0)
246*44970Sbostic 				fatal("%s: bad rotational layout count\n",
247*44970Sbostic 				    optarg);
248*44970Sbostic 			break;
249*44970Sbostic 		case 'o':
250*44970Sbostic 			if (strcmp(optarg, "space") == 0)
251*44970Sbostic 				opt = FS_OPTSPACE;
252*44970Sbostic 			else if (strcmp(optarg, "time") == 0)
253*44970Sbostic 				opt = FS_OPTTIME;
254*44970Sbostic 			else
255*44970Sbostic 				fatal("%s: bad optimization preference %s",
256*44970Sbostic 				    optarg, "(options are `space' or `time')");
257*44970Sbostic 			break;
258*44970Sbostic 		case 'p':
259*44970Sbostic 			if ((trackspares = atoi(optarg)) < 0)
260*44970Sbostic 				fatal("%s: bad spare sectors per track",
261*44970Sbostic 				    optarg);
262*44970Sbostic 			break;
263*44970Sbostic 		case 'r':
264*44970Sbostic 			if ((rpm = atoi(optarg)) <= 0)
265*44970Sbostic 				fatal("%s: bad revs/minute\n", optarg);
266*44970Sbostic 			break;
267*44970Sbostic 		case 's':
268*44970Sbostic 			if ((fssize = atoi(optarg)) <= 0)
269*44970Sbostic 				fatal("%s: bad file system size", optarg);
270*44970Sbostic 			break;
271*44970Sbostic 		case 't':
272*44970Sbostic 			if ((ntracks = atoi(optarg)) <= 0)
273*44970Sbostic 				fatal("%s: bad total tracks", optarg);
274*44970Sbostic 			break;
275*44970Sbostic 		case 'u':
276*44970Sbostic 			if ((nsectors = atoi(optarg)) <= 0)
277*44970Sbostic 				fatal("%s: bad sectors/track", optarg);
278*44970Sbostic 			break;
279*44970Sbostic 		case 'x':
280*44970Sbostic 			if ((cylspares = atoi(optarg)) < 0)
281*44970Sbostic 				fatal("%s: bad spare sectors per cylinder",
282*44970Sbostic 				    optarg);
283*44970Sbostic 			break;
284*44970Sbostic 		case '?':
285*44970Sbostic 		default:
286*44970Sbostic 			usage();
287*44970Sbostic 		}
288*44970Sbostic 	argc -= optind;
289*44970Sbostic 	argv += optind;
29043314Smckusick 
291*44970Sbostic 	if (argc != 2 && (mfs || argc != 1))
292*44970Sbostic 		usage();
29310762Ssam 
29410762Ssam 	special = argv[0];
29514064Smckusick 	cp = rindex(special, '/');
29614064Smckusick 	if (cp != 0)
29714064Smckusick 		special = cp + 1;
29843314Smckusick 	if (*special == 'r'
29943314Smckusick #if defined(vax) || defined(tahoe)
300*44970Sbostic 	    && special[1] != 'a' && special[1] != 'b')
30143314Smckusick #endif
30243314Smckusick #if defined(hp300)
303*44970Sbostic 	    && special[1] != 'd')
30443314Smckusick #endif
30514064Smckusick 		special++;
30637951Sbostic 	(void)sprintf(device, "%s/r%s", _PATH_DEV, special);
30732463Sbostic 	special = device;
30830380Smckusick 	if (!Nflag) {
30930380Smckusick 		fso = open(special, O_WRONLY);
310*44970Sbostic 		if (fso < 0)
311*44970Sbostic 			fatal("%s: %s", special, strerror(errno));
31230380Smckusick 	} else
31330380Smckusick 		fso = -1;
31430380Smckusick 	fsi = open(special, O_RDONLY);
315*44970Sbostic 	if (fsi < 0)
316*44970Sbostic 		fatal("%s: %s", special, strerror(errno));
317*44970Sbostic 	if (fstat(fsi, &st) < 0)
318*44970Sbostic 		fatal("%s: %s", special, strerror(errno));
31914064Smckusick 	if ((st.st_mode & S_IFMT) != S_IFCHR)
32014064Smckusick 		fatal("%s: not a character device", special);
32110762Ssam 	cp = index(argv[0], '\0') - 1;
32230380Smckusick 	if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
32310762Ssam 		fatal("%s: can't figure out file system partition", argv[0]);
32431680Skarels #ifdef COMPAT
32543314Smckusick 	if (!mfs && disktype == NULL)
32643314Smckusick 		disktype = argv[1];
32743314Smckusick #endif
32830380Smckusick 	lp = getdisklabel(special, fsi);
32930380Smckusick 	if (isdigit(*cp))
33030380Smckusick 		pp = &lp->d_partitions[0];
33130380Smckusick 	else
33230380Smckusick 		pp = &lp->d_partitions[*cp - 'a'];
33330380Smckusick 	if (pp->p_size == 0)
33430380Smckusick 		fatal("%s: `%c' partition is unavailable", argv[0], *cp);
33530380Smckusick 	if (fssize == 0)
33610762Ssam 		fssize = pp->p_size;
33739325Smckusick 	if (fssize > pp->p_size && !mfs)
33830380Smckusick 	       fatal("%s: maximum file system size on the `%c' partition is %d",
33930380Smckusick 			argv[0], *cp, pp->p_size);
34030380Smckusick 	if (rpm == 0) {
34130380Smckusick 		rpm = lp->d_rpm;
34230380Smckusick 		if (rpm <= 0)
34330743Skarels 			rpm = 3600;
34410762Ssam 	}
34530380Smckusick 	if (ntracks == 0) {
34630380Smckusick 		ntracks = lp->d_ntracks;
34730380Smckusick 		if (ntracks <= 0)
34830743Skarels 			fatal("%s: no default #tracks", argv[0]);
34930380Smckusick 	}
35010762Ssam 	if (nsectors == 0) {
35130380Smckusick 		nsectors = lp->d_nsectors;
35230380Smckusick 		if (nsectors <= 0)
35330743Skarels 			fatal("%s: no default #sectors/track", argv[0]);
35410762Ssam 	}
35510762Ssam 	if (sectorsize == 0) {
35630380Smckusick 		sectorsize = lp->d_secsize;
35730380Smckusick 		if (sectorsize <= 0)
35830743Skarels 			fatal("%s: no default sector size", argv[0]);
35910762Ssam 	}
36030386Smckusick 	if (trackskew == -1) {
36130386Smckusick 		trackskew = lp->d_trackskew;
36230386Smckusick 		if (trackskew < 0)
36330743Skarels 			trackskew = 0;
36430386Smckusick 	}
36530386Smckusick 	if (interleave == 0) {
36630386Smckusick 		interleave = lp->d_interleave;
36730386Smckusick 		if (interleave <= 0)
36830743Skarels 			interleave = 1;
36930386Smckusick 	}
37010762Ssam 	if (fsize == 0) {
37110762Ssam 		fsize = pp->p_fsize;
37230380Smckusick 		if (fsize <= 0)
37330380Smckusick 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
37410762Ssam 	}
37530380Smckusick 	if (bsize == 0) {
37630380Smckusick 		bsize = pp->p_frag * pp->p_fsize;
37730380Smckusick 		if (bsize <= 0)
37830380Smckusick 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
37911069Ssam 	}
38039049Smckusick 	if (density == 0)
38139049Smckusick 		density = NFPI * fsize;
38224702Smckusick 	if (minfree < 10 && opt != FS_OPTSPACE) {
38330380Smckusick 		fprintf(stderr, "Warning: changing optimization to space ");
38430380Smckusick 		fprintf(stderr, "because minfree is less than 10%%\n");
38524702Smckusick 		opt = FS_OPTSPACE;
38624702Smckusick 	}
38730386Smckusick 	if (trackspares == -1) {
38830386Smckusick 		trackspares = lp->d_sparespertrack;
38930386Smckusick 		if (trackspares < 0)
39030743Skarels 			trackspares = 0;
39130386Smckusick 	}
39230386Smckusick 	nphyssectors = nsectors + trackspares;
39330386Smckusick 	if (cylspares == -1) {
39430386Smckusick 		cylspares = lp->d_sparespercyl;
39530386Smckusick 		if (cylspares < 0)
39630743Skarels 			cylspares = 0;
39730386Smckusick 	}
39830386Smckusick 	secpercyl = nsectors * ntracks - cylspares;
39930380Smckusick 	if (secpercyl != lp->d_secpercyl)
400*44970Sbostic 		fprintf(stderr, "%s (%d) %s (%lu)\n",
40130380Smckusick 			"Warning: calculated sectors per cylinder", secpercyl,
40230380Smckusick 			"disagrees with disk label", lp->d_secpercyl);
40332321Smckusick 	if (maxbpg == 0)
40432321Smckusick 		maxbpg = MAXBLKPG(bsize);
40530386Smckusick 	headswitch = lp->d_headswitch;
40630386Smckusick 	trackseek = lp->d_trkseek;
40730691Skarels 	bbsize = lp->d_bbsize;
40830691Skarels 	sbsize = lp->d_sbsize;
40930380Smckusick 	oldpartition = *pp;
41030691Skarels #ifdef tahoe
41130691Skarels 	realsectorsize = sectorsize;
41230862Skarels 	if (sectorsize != DEV_BSIZE) {		/* XXX */
41330691Skarels 		int secperblk = DEV_BSIZE / sectorsize;
41430691Skarels 
41530691Skarels 		sectorsize = DEV_BSIZE;
41630691Skarels 		nsectors /= secperblk;
41730691Skarels 		nphyssectors /= secperblk;
41830691Skarels 		secpercyl /= secperblk;
41930691Skarels 		fssize /= secperblk;
42030691Skarels 		pp->p_size /= secperblk;
42130691Skarels 	}
42230691Skarels #endif
42330380Smckusick 	mkfs(pp, special, fsi, fso);
42430691Skarels #ifdef tahoe
42530691Skarels 	if (realsectorsize != DEV_BSIZE)
42630691Skarels 		pp->p_size *= DEV_BSIZE / realsectorsize;
42730691Skarels #endif
42830380Smckusick 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
42930380Smckusick 		rewritelabel(special, fso, lp);
43039049Smckusick 	if (!Nflag)
43139049Smckusick 		close(fso);
43239049Smckusick 	close(fsi);
43339325Smckusick 	if (mfs) {
43439325Smckusick 		sprintf(buf, "mfs:%d", getpid());
43539049Smckusick 		args.name = buf;
43639049Smckusick 		args.base = membase;
43739049Smckusick 		args.size = fssize * sectorsize;
438*44970Sbostic 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
439*44970Sbostic 			fatal("%s: %s", argv[1], strerror(errno));
44039049Smckusick 	}
44130380Smckusick 	exit(0);
44230380Smckusick }
44330380Smckusick 
44431680Skarels #ifdef COMPAT
44543314Smckusick char lmsg[] = "%s: can't read disk label; disk type must be specified";
44643314Smckusick #else
44743314Smckusick char lmsg[] = "%s: can't read disk label";
44843314Smckusick #endif
44931680Skarels 
45031680Skarels struct disklabel *
45130380Smckusick getdisklabel(s, fd)
45230380Smckusick 	char *s;
45331680Skarels 	int fd;
45430380Smckusick {
45530380Smckusick 	static struct disklabel lab;
45630380Smckusick 
45730380Smckusick 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
45843314Smckusick #ifdef COMPAT
45943314Smckusick 		if (disktype) {
46043314Smckusick 			struct disklabel *getdiskbyname();
46143314Smckusick 
462*44970Sbostic 			unlabeled++;
46343314Smckusick 			return (getdiskbyname(disktype));
46443314Smckusick 		}
46543314Smckusick #endif
466*44970Sbostic 		(void)fprintf(stderr,
467*44970Sbostic 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
46843314Smckusick 		fatal(lmsg, s);
46910762Ssam 	}
47030380Smckusick 	return (&lab);
47130380Smckusick }
47210762Ssam 
47330380Smckusick rewritelabel(s, fd, lp)
47430380Smckusick 	char *s;
47530380Smckusick 	int fd;
47630380Smckusick 	register struct disklabel *lp;
47730380Smckusick {
47831680Skarels #ifdef COMPAT
479*44970Sbostic 	if (unlabeled)
48031680Skarels 		return;
48131680Skarels #endif
48230380Smckusick 	lp->d_checksum = 0;
48330380Smckusick 	lp->d_checksum = dkcksum(lp);
48430380Smckusick 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
485*44970Sbostic 		(void)fprintf(stderr,
486*44970Sbostic 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
48730380Smckusick 		fatal("%s: can't rewrite disk label", s);
48810762Ssam 	}
48930446Skarels #if vax
49030446Skarels 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
49130446Skarels 		register i;
49230691Skarels 		int cfd;
49330446Skarels 		daddr_t alt;
49430691Skarels 		char specname[64];
49530691Skarels 		char blk[1024];
49631045Ssam 		char *cp;
49730446Skarels 
49830691Skarels 		/*
49930691Skarels 		 * Make name for 'c' partition.
50030691Skarels 		 */
50130691Skarels 		strcpy(specname, s);
50230691Skarels 		cp = specname + strlen(specname) - 1;
50330691Skarels 		if (!isdigit(*cp))
50430691Skarels 			*cp = 'c';
50530691Skarels 		cfd = open(specname, O_WRONLY);
506*44970Sbostic 		if (cfd < 0)
507*44970Sbostic 			fatal("%s: %s", specname, strerror(errno));
50830691Skarels 		bzero(blk, sizeof(blk));
50930691Skarels 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
51030446Skarels 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
51130446Skarels 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
51231680Skarels 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize, L_SET) == -1) {
513*44970Sbostic 				fatal("lseek to badsector area: %s",
514*44970Sbostic 				    strerror(errno));
515*44970Sbostic 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
516*44970Sbostic 				fprintf(stderr,
517*44970Sbostic 				    "%s: alternate label %d write: %s\n",
518*44970Sbostic 				    progname, i/2, strerror(errno));
51930380Smckusick 		}
52039049Smckusick 		close(cfd);
52130380Smckusick 	}
52230446Skarels #endif
52310762Ssam }
52410762Ssam 
52510762Ssam /*VARARGS*/
526*44970Sbostic fatal(fmt)
52710762Ssam 	char *fmt;
52810762Ssam {
529*44970Sbostic 	va_list ap;
53010762Ssam 
53139049Smckusick 	fprintf(stderr, "%s: ", progname);
532*44970Sbostic 	va_start(ap, fmt);
533*44970Sbostic 	(void)vfprintf(stderr, fmt, ap);
534*44970Sbostic 	va_end(ap);
53510762Ssam 	putc('\n', stderr);
536*44970Sbostic 	exit(1);
53710762Ssam }
538*44970Sbostic 
539*44970Sbostic usage()
540*44970Sbostic {
541*44970Sbostic 	if (mfs) {
542*44970Sbostic 		fprintf(stderr,
543*44970Sbostic 		    "usage: mfs [ -fsoptions ] special-device mount-point\n");
544*44970Sbostic 	} else
545*44970Sbostic 		fprintf(stderr,
546*44970Sbostic 		    "usage: newfs [ -fsoptions ] special-device%s\n",
547*44970Sbostic #ifdef COMPAT
548*44970Sbostic 		    " [device-type]");
549*44970Sbostic #else
550*44970Sbostic 		    "");
551*44970Sbostic #endif
552*44970Sbostic 	fprintf(stderr, "where fsoptions are:\n");
553*44970Sbostic 	fprintf(stderr,
554*44970Sbostic 	    "\t-N do not create file system, just print out parameters\n");
555*44970Sbostic 	fprintf(stderr, "\t-S sector size\n");
556*44970Sbostic #ifdef COMPAT
557*44970Sbostic 	fprintf(stderr, "\t-T disktype\n");
558*44970Sbostic #endif
559*44970Sbostic 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
560*44970Sbostic 	fprintf(stderr, "\t-b block size\n");
561*44970Sbostic 	fprintf(stderr, "\t-c cylinders/group\n");
562*44970Sbostic 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
563*44970Sbostic 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
564*44970Sbostic 	fprintf(stderr, "\t-f frag size\n");
565*44970Sbostic 	fprintf(stderr, "\t-i number of bytes per inode\n");
566*44970Sbostic 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
567*44970Sbostic 	fprintf(stderr, "\t-l hardware sector interleave\n");
568*44970Sbostic 	fprintf(stderr, "\t-m minimum free space %%\n");
569*44970Sbostic 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
570*44970Sbostic 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
571*44970Sbostic 	fprintf(stderr, "\t-p spare sectors per track\n");
572*44970Sbostic 	fprintf(stderr, "\t-s file system size (sectors)\n");
573*44970Sbostic 	fprintf(stderr, "\t-r revolutions/minute\n");
574*44970Sbostic 	fprintf(stderr, "\t-t tracks/cylinder\n");
575*44970Sbostic 	fprintf(stderr, "\t-u sectors/track\n");
576*44970Sbostic 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
577*44970Sbostic 	exit(1);
578*44970Sbostic }
579