xref: /csrg-svn/sbin/newlfs/newfs.c (revision 51866)
151151Sbostic /*
251151Sbostic  * Copyright (c) 1983, 1989 The Regents of the University of California.
351151Sbostic  * All rights reserved.
451151Sbostic  *
551151Sbostic  * Redistribution and use in source and binary forms, with or without
651151Sbostic  * modification, are permitted provided that the following conditions
751151Sbostic  * are met:
851151Sbostic  * 1. Redistributions of source code must retain the above copyright
951151Sbostic  *    notice, this list of conditions and the following disclaimer.
1051151Sbostic  * 2. Redistributions in binary form must reproduce the above copyright
1151151Sbostic  *    notice, this list of conditions and the following disclaimer in the
1251151Sbostic  *    documentation and/or other materials provided with the distribution.
1351151Sbostic  * 3. All advertising materials mentioning features or use of this software
1451151Sbostic  *    must display the following acknowledgement:
1551151Sbostic  *	This product includes software developed by the University of
1651151Sbostic  *	California, Berkeley and its contributors.
1751151Sbostic  * 4. Neither the name of the University nor the names of its contributors
1851151Sbostic  *    may be used to endorse or promote products derived from this software
1951151Sbostic  *    without specific prior written permission.
2051151Sbostic  *
2151151Sbostic  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2251151Sbostic  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2351151Sbostic  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2451151Sbostic  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2551151Sbostic  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2651151Sbostic  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2751151Sbostic  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2851151Sbostic  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2951151Sbostic  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3051151Sbostic  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3151151Sbostic  * SUCH DAMAGE.
3251151Sbostic  */
3351151Sbostic 
3451151Sbostic #ifndef lint
3551151Sbostic static char sccsid[] = "@(#)newfs.c	6.28 (Berkeley) 8/6/91";
3651151Sbostic #endif /* not lint */
3751151Sbostic 
3851151Sbostic #ifndef lint
3951151Sbostic char copyright[] =
4051151Sbostic "@(#) Copyright (c) 1983, 1989 Regents of the University of California.\n\
4151151Sbostic  All rights reserved.\n";
4251151Sbostic #endif /* not lint */
4351151Sbostic 
4451151Sbostic /*
4551151Sbostic  * newfs: friendly front end to mkfs
4651151Sbostic  */
4751151Sbostic #include <sys/param.h>
4851151Sbostic #include <sys/stat.h>
4951151Sbostic #include <sys/ioctl.h>
5051151Sbostic #include <sys/disklabel.h>
5151151Sbostic #include <sys/file.h>
5251151Sbostic #include <sys/mount.h>
5351151Sbostic 
54*51866Sbostic #include <ufs/ufs/dir.h>
55*51866Sbostic #include <ufs/ufs/dinode.h>
56*51866Sbostic #include <ufs/ffs/fs.h>
57*51866Sbostic 
5851151Sbostic #include <errno.h>
5951151Sbostic #include <unistd.h>
6051151Sbostic #include <stdio.h>
6151151Sbostic #include <stdlib.h>
6251151Sbostic #include <ctype.h>
6351151Sbostic #include <string.h>
6451151Sbostic #include <paths.h>
6551151Sbostic #include "config.h"
6651151Sbostic #include "extern.h"
6751151Sbostic 
6851151Sbostic #define	COMPAT			/* allow non-labeled disks */
6951151Sbostic 
7051151Sbostic int	mfs;			/* run as the memory based filesystem */
7151151Sbostic int	Nflag;			/* run without writing file system */
7251151Sbostic int	fssize;			/* file system size */
7351151Sbostic int	ntracks;		/* # tracks/cylinder */
7451151Sbostic int	nsectors;		/* # sectors/track */
7551151Sbostic int	nphyssectors;		/* # sectors/track including spares */
7651151Sbostic int	secpercyl;		/* sectors per cylinder */
7751151Sbostic int	trackspares = -1;	/* spare sectors per track */
7851151Sbostic int	cylspares = -1;		/* spare sectors per cylinder */
7951151Sbostic int	sectorsize;		/* bytes/sector */
8051151Sbostic #ifdef tahoe
8151151Sbostic int	realsectorsize;		/* bytes/sector in hardware */
8251151Sbostic #endif
8351151Sbostic int	rpm;			/* revolutions/minute of drive */
8451151Sbostic int	interleave;		/* hardware sector interleave */
8551151Sbostic int	trackskew = -1;		/* sector 0 skew, per track */
8651151Sbostic int	headswitch;		/* head switch time, usec */
8751151Sbostic int	trackseek;		/* track-to-track seek, usec */
8851151Sbostic int	fsize = 0;		/* fragment size */
8951151Sbostic int	bsize = 0;		/* block size */
9051151Sbostic int	cpg = DESCPG;		/* cylinders/cylinder group */
9151151Sbostic int	cpgflg;			/* cylinders/cylinder group flag was given */
9251151Sbostic int	minfree = MINFREE;	/* free space threshold */
9351151Sbostic int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
9451151Sbostic int	density;		/* number of bytes per inode */
9551151Sbostic int	maxcontig = MAXCONTIG;	/* max contiguous blocks to allocate */
9651151Sbostic int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
9751151Sbostic int	maxbpg;			/* maximum blocks per file in a cyl group */
9851151Sbostic int	nrpos = NRPOS;		/* # of distinguished rotational positions */
9951151Sbostic int	bbsize = BBSIZE;	/* boot block size */
10051151Sbostic int	sbsize = SBSIZE;	/* superblock size */
10151151Sbostic int	mntflags;		/* flags to be passed to mount */
10251151Sbostic u_long	memleft;		/* virtual memory available */
10351151Sbostic caddr_t	membase;		/* start address of memory based filesystem */
10451151Sbostic #ifdef COMPAT
10551151Sbostic char	*disktype;
10651151Sbostic int	unlabeled;
10751151Sbostic #endif
10851151Sbostic 
10951151Sbostic char	device[MAXPATHLEN];
11051151Sbostic char	*progname, *special;
11151151Sbostic 
11251151Sbostic static struct disklabel *getdisklabel __P((char *, int));
11351151Sbostic static struct disklabel *debug_readlabel __P((int));
11451151Sbostic static void rewritelabel __P((char *, int, struct disklabel *));
11551151Sbostic static void usage __P((void));
11651151Sbostic 
11751151Sbostic int
11851151Sbostic main(argc, argv)
11951151Sbostic 	int argc;
12051151Sbostic 	char *argv[];
12151151Sbostic {
12251151Sbostic 	register int ch;
12351151Sbostic 	register struct partition *pp;
12451151Sbostic 	register struct disklabel *lp;
12551151Sbostic 	struct partition oldpartition;
12651151Sbostic 	struct stat st;
12751151Sbostic 	int debug, lfs, fsi, fso, segsize;
12851151Sbostic 	char *cp, *opstring;
12951151Sbostic 
13051151Sbostic 	if (progname = rindex(*argv, '/'))
13151151Sbostic 		++progname;
13251151Sbostic 	else
13351151Sbostic 		progname = *argv;
13451151Sbostic 
13551151Sbostic 	if (strstr(progname, "mfs")) {
13651151Sbostic 		mfs = 1;
13751151Sbostic 		Nflag++;
13851151Sbostic 	}
13951151Sbostic 
14051151Sbostic 	/* -F is mfs only and MUST come first! */
14151151Sbostic 	opstring = "F:B:DLNS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
14251151Sbostic 	if (!mfs)
14351151Sbostic 		opstring += 2;
14451151Sbostic 
14551151Sbostic 	debug = lfs = segsize = 0;
14651151Sbostic 	while ((ch = getopt(argc, argv, opstring)) != EOF)
14751151Sbostic 		switch(ch) {
14851151Sbostic 		case 'B':	/* LFS segment size */
14951151Sbostic 			if ((segsize = atoi(optarg)) < LFS_MINSEGSIZE)
15051151Sbostic 				fatal("%s: bad segment size", optarg);
15151151Sbostic 			break;
15251151Sbostic 		case 'D':
15351151Sbostic 			debug = 1;
15451151Sbostic 			break;
15551151Sbostic 		case 'F':
15651151Sbostic 			if ((mntflags = atoi(optarg)) == 0)
15751151Sbostic 				fatal("%s: bad mount flags", optarg);
15851151Sbostic 			break;
15951151Sbostic 		case 'L':	/* Create lfs */
16051151Sbostic 			lfs = 1;
16151151Sbostic 			break;
16251151Sbostic 		case 'N':
16351151Sbostic 			Nflag++;
16451151Sbostic 			break;
16551151Sbostic 		case 'S':
16651151Sbostic 			if ((sectorsize = atoi(optarg)) <= 0)
16751151Sbostic 				fatal("%s: bad sector size", optarg);
16851151Sbostic 			break;
16951151Sbostic #ifdef COMPAT
17051151Sbostic 		case 'T':
17151151Sbostic 			disktype = optarg;
17251151Sbostic 			break;
17351151Sbostic #endif
17451151Sbostic 		case 'a':
17551151Sbostic 			if ((maxcontig = atoi(optarg)) <= 0)
17651151Sbostic 				fatal("%s: bad max contiguous blocks\n",
17751151Sbostic 				    optarg);
17851151Sbostic 			break;
17951151Sbostic 		case 'b':	/* used for LFS */
18051151Sbostic 			if ((bsize = atoi(optarg)) < MINBSIZE)
18151151Sbostic 				fatal("%s: bad block size", optarg);
18251151Sbostic 			break;
18351151Sbostic 		case 'c':
18451151Sbostic 			if ((cpg = atoi(optarg)) <= 0)
18551151Sbostic 				fatal("%s: bad cylinders/group", optarg);
18651151Sbostic 			cpgflg++;
18751151Sbostic 			break;
18851151Sbostic 		case 'd':
18951151Sbostic 			if ((rotdelay = atoi(optarg)) < 0)
19051151Sbostic 				fatal("%s: bad rotational delay\n", optarg);
19151151Sbostic 			break;
19251151Sbostic 		case 'e':
19351151Sbostic 			if ((maxbpg = atoi(optarg)) <= 0)
19451151Sbostic 				fatal("%s: bad blocks per file in a cyl group\n",
19551151Sbostic 				    optarg);
19651151Sbostic 			break;
19751151Sbostic 		case 'f':
19851151Sbostic 			if ((fsize = atoi(optarg)) <= 0)
19951151Sbostic 				fatal("%s: bad frag size", optarg);
20051151Sbostic 			break;
20151151Sbostic 		case 'i':
20251151Sbostic 			if ((density = atoi(optarg)) <= 0)
20351151Sbostic 				fatal("%s: bad bytes per inode\n", optarg);
20451151Sbostic 			break;
20551151Sbostic 		case 'k':
20651151Sbostic 			if ((trackskew = atoi(optarg)) < 0)
20751151Sbostic 				fatal("%s: bad track skew", optarg);
20851151Sbostic 			break;
20951151Sbostic 		case 'l':
21051151Sbostic 			if ((interleave = atoi(optarg)) <= 0)
21151151Sbostic 				fatal("%s: bad interleave", optarg);
21251151Sbostic 			break;
21351151Sbostic 		case 'm':		/* used for LFS */
21451151Sbostic 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
21551151Sbostic 				fatal("%s: bad free space %%\n", optarg);
21651151Sbostic 			break;
21751151Sbostic 		case 'n':
21851151Sbostic 			if ((nrpos = atoi(optarg)) <= 0)
21951151Sbostic 				fatal("%s: bad rotational layout count\n",
22051151Sbostic 				    optarg);
22151151Sbostic 			break;
22251151Sbostic 		case 'o':
22351151Sbostic 			if (strcmp(optarg, "space") == 0)
22451151Sbostic 				opt = FS_OPTSPACE;
22551151Sbostic 			else if (strcmp(optarg, "time") == 0)
22651151Sbostic 				opt = FS_OPTTIME;
22751151Sbostic 			else
22851151Sbostic 				fatal("%s: bad optimization preference %s",
22951151Sbostic 				    optarg, "(options are `space' or `time')");
23051151Sbostic 			break;
23151151Sbostic 		case 'p':
23251151Sbostic 			if ((trackspares = atoi(optarg)) < 0)
23351151Sbostic 				fatal("%s: bad spare sectors per track",
23451151Sbostic 				    optarg);
23551151Sbostic 			break;
23651151Sbostic 		case 'r':
23751151Sbostic 			if ((rpm = atoi(optarg)) <= 0)
23851151Sbostic 				fatal("%s: bad revs/minute\n", optarg);
23951151Sbostic 			break;
24051151Sbostic 		case 's':	/* used for LFS */
24151151Sbostic 			if ((fssize = atoi(optarg)) <= 0)
24251151Sbostic 				fatal("%s: bad file system size", optarg);
24351151Sbostic 			break;
24451151Sbostic 		case 't':
24551151Sbostic 			if ((ntracks = atoi(optarg)) <= 0)
24651151Sbostic 				fatal("%s: bad total tracks", optarg);
24751151Sbostic 			break;
24851151Sbostic 		case 'u':
24951151Sbostic 			if ((nsectors = atoi(optarg)) <= 0)
25051151Sbostic 				fatal("%s: bad sectors/track", optarg);
25151151Sbostic 			break;
25251151Sbostic 		case 'x':
25351151Sbostic 			if ((cylspares = atoi(optarg)) < 0)
25451151Sbostic 				fatal("%s: bad spare sectors per cylinder",
25551151Sbostic 				    optarg);
25651151Sbostic 			break;
25751151Sbostic 		case '?':
25851151Sbostic 		default:
25951151Sbostic 			usage();
26051151Sbostic 		}
26151151Sbostic 	argc -= optind;
26251151Sbostic 	argv += optind;
26351151Sbostic 
26451151Sbostic 	if (argc != 2 && (mfs || argc != 1))
26551151Sbostic 		usage();
26651151Sbostic 
26751151Sbostic 	/*
26851151Sbostic 	 * If the -N flag isn't specified, open the output file.  If no path
26951151Sbostic 	 * prefix, try /dev/r%s and then /dev/%s.
27051151Sbostic 	 */
27151151Sbostic 	special = argv[0];
27251151Sbostic 	if (index(special, '/') == NULL) {
27351151Sbostic 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
27451151Sbostic 		if (stat(device, &st) == -1)
27551151Sbostic 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
27651151Sbostic 		special = device;
27751151Sbostic 	}
27851151Sbostic 	if (!Nflag) {
27951151Sbostic 		fso = open(special,
28051151Sbostic 		    (debug ? O_CREAT : 0) | O_WRONLY, DEFFILEMODE);
28151151Sbostic 		if (fso < 0)
28251151Sbostic 			fatal("%s: %s", special, strerror(errno));
28351151Sbostic 	} else
28451151Sbostic 		fso = -1;
28551151Sbostic 
28651151Sbostic 	/* Open the input file. */
28751151Sbostic 	fsi = open(special, O_RDONLY);
28851151Sbostic 	if (fsi < 0)
28951151Sbostic 		fatal("%s: %s", special, strerror(errno));
29051151Sbostic 	if (fstat(fsi, &st) < 0)
29151151Sbostic 		fatal("%s: %s", special, strerror(errno));
29251151Sbostic 
29351865Sbostic 	if (!debug && !mfs && !S_ISCHR(st.st_mode))
29451151Sbostic 		(void)printf("%s: %s: not a character-special device\n",
29551151Sbostic 		    progname, special);
29651151Sbostic 	cp = index(argv[0], '\0') - 1;
29751151Sbostic 	if (!debug && (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)))
29851151Sbostic 		fatal("%s: can't figure out file system partition", argv[0]);
29951151Sbostic 
30051151Sbostic #ifdef COMPAT
30151151Sbostic 	if (!mfs && disktype == NULL)
30251151Sbostic 		disktype = argv[1];
30351151Sbostic #endif
30451151Sbostic 	if (debug)
30551151Sbostic 		lp = debug_readlabel(fsi);
30651151Sbostic 	else
30751151Sbostic 		lp = getdisklabel(special, fsi);
30851151Sbostic 
30951151Sbostic 	if (isdigit(*cp))
31051151Sbostic 		pp = &lp->d_partitions[0];
31151151Sbostic 	else
31251151Sbostic 		pp = &lp->d_partitions[*cp - 'a'];
31351151Sbostic 	if (pp->p_size == 0)
31451151Sbostic 		fatal("%s: `%c' partition is unavailable", argv[0], *cp);
31551151Sbostic 
31651151Sbostic 	/* If we're making a LFS, we break out here */
31751151Sbostic 	if (lfs)
31851151Sbostic 		exit(make_lfs(fso, lp, pp, minfree, bsize, segsize));
31951151Sbostic 
32051151Sbostic 	if (fssize == 0)
32151151Sbostic 		fssize = pp->p_size;
32251151Sbostic 	if (fssize > pp->p_size && !mfs)
32351151Sbostic 	       fatal("%s: maximum file system size on the `%c' partition is %d",
32451151Sbostic 			argv[0], *cp, pp->p_size);
32551151Sbostic 	if (rpm == 0) {
32651151Sbostic 		rpm = lp->d_rpm;
32751151Sbostic 		if (rpm <= 0)
32851151Sbostic 			rpm = 3600;
32951151Sbostic 	}
33051151Sbostic 	if (ntracks == 0) {
33151151Sbostic 		ntracks = lp->d_ntracks;
33251151Sbostic 		if (ntracks <= 0)
33351151Sbostic 			fatal("%s: no default #tracks", argv[0]);
33451151Sbostic 	}
33551151Sbostic 	if (nsectors == 0) {
33651151Sbostic 		nsectors = lp->d_nsectors;
33751151Sbostic 		if (nsectors <= 0)
33851151Sbostic 			fatal("%s: no default #sectors/track", argv[0]);
33951151Sbostic 	}
34051151Sbostic 	if (sectorsize == 0) {
34151151Sbostic 		sectorsize = lp->d_secsize;
34251151Sbostic 		if (sectorsize <= 0)
34351151Sbostic 			fatal("%s: no default sector size", argv[0]);
34451151Sbostic 	}
34551151Sbostic 	if (trackskew == -1) {
34651151Sbostic 		trackskew = lp->d_trackskew;
34751151Sbostic 		if (trackskew < 0)
34851151Sbostic 			trackskew = 0;
34951151Sbostic 	}
35051151Sbostic 	if (interleave == 0) {
35151151Sbostic 		interleave = lp->d_interleave;
35251151Sbostic 		if (interleave <= 0)
35351151Sbostic 			interleave = 1;
35451151Sbostic 	}
35551151Sbostic 	if (fsize == 0) {
35651151Sbostic 		fsize = pp->p_fsize;
35751151Sbostic 		if (fsize <= 0)
35851151Sbostic 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
35951151Sbostic 	}
36051151Sbostic 	if (bsize == 0) {
36151151Sbostic 		bsize = pp->p_frag * pp->p_fsize;
36251151Sbostic 		if (bsize <= 0)
36351151Sbostic 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
36451151Sbostic 	}
36551151Sbostic 	if (density == 0)
36651151Sbostic 		density = NFPI * fsize;
36751151Sbostic 	if (minfree < 10 && opt != FS_OPTSPACE) {
36851151Sbostic 		fprintf(stderr, "Warning: changing optimization to space ");
36951151Sbostic 		fprintf(stderr, "because minfree is less than 10%%\n");
37051151Sbostic 		opt = FS_OPTSPACE;
37151151Sbostic 	}
37251151Sbostic 	if (trackspares == -1) {
37351151Sbostic 		trackspares = lp->d_sparespertrack;
37451151Sbostic 		if (trackspares < 0)
37551151Sbostic 			trackspares = 0;
37651151Sbostic 	}
37751151Sbostic 	nphyssectors = nsectors + trackspares;
37851151Sbostic 	if (cylspares == -1) {
37951151Sbostic 		cylspares = lp->d_sparespercyl;
38051151Sbostic 		if (cylspares < 0)
38151151Sbostic 			cylspares = 0;
38251151Sbostic 	}
38351151Sbostic 	secpercyl = nsectors * ntracks - cylspares;
38451151Sbostic 	if (secpercyl != lp->d_secpercyl)
38551151Sbostic 		fprintf(stderr, "%s (%d) %s (%lu)\n",
38651151Sbostic 			"Warning: calculated sectors per cylinder", secpercyl,
38751151Sbostic 			"disagrees with disk label", lp->d_secpercyl);
38851151Sbostic 	if (maxbpg == 0)
38951151Sbostic 		maxbpg = MAXBLKPG(bsize);
39051151Sbostic 	headswitch = lp->d_headswitch;
39151151Sbostic 	trackseek = lp->d_trkseek;
39251151Sbostic #ifdef notdef /* label may be 0 if faked up by kernel */
39351151Sbostic 	bbsize = lp->d_bbsize;
39451151Sbostic 	sbsize = lp->d_sbsize;
39551151Sbostic #endif
39651151Sbostic 	oldpartition = *pp;
39751151Sbostic #ifdef tahoe
39851151Sbostic 	realsectorsize = sectorsize;
39951151Sbostic 	if (sectorsize != DEV_BSIZE) {		/* XXX */
40051151Sbostic 		int secperblk = DEV_BSIZE / sectorsize;
40151151Sbostic 
40251151Sbostic 		sectorsize = DEV_BSIZE;
40351151Sbostic 		nsectors /= secperblk;
40451151Sbostic 		nphyssectors /= secperblk;
40551151Sbostic 		secpercyl /= secperblk;
40651151Sbostic 		fssize /= secperblk;
40751151Sbostic 		pp->p_size /= secperblk;
40851151Sbostic 	}
40951151Sbostic #endif
41051151Sbostic 	mkfs(pp, special, fsi, fso);
41151151Sbostic #ifdef tahoe
41251151Sbostic 	if (realsectorsize != DEV_BSIZE)
41351151Sbostic 		pp->p_size *= DEV_BSIZE / realsectorsize;
41451151Sbostic #endif
41551151Sbostic 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
41651151Sbostic 		rewritelabel(special, fso, lp);
41751151Sbostic 	if (!Nflag)
41851151Sbostic 		close(fso);
41951151Sbostic 	close(fsi);
42051151Sbostic #ifdef MFS
42151151Sbostic 	if (mfs) {
42251151Sbostic 		struct mfs_args args;
42351151Sbostic 		char buf[50];
42451151Sbostic 
42551151Sbostic 		(void)sprintf(buf, "mfs:%d", getpid());
42651151Sbostic 		args.name = buf;
42751151Sbostic 		args.base = membase;
42851151Sbostic 		args.size = fssize * sectorsize;
42951151Sbostic 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
43051151Sbostic 			fatal("%s: %s", argv[1], strerror(errno));
43151151Sbostic 	}
43251151Sbostic #endif
43351151Sbostic 	exit(0);
43451151Sbostic }
43551151Sbostic 
43651151Sbostic #ifdef COMPAT
43751151Sbostic char lmsg[] = "%s: can't read disk label; disk type must be specified";
43851151Sbostic #else
43951151Sbostic char lmsg[] = "%s: can't read disk label";
44051151Sbostic #endif
44151151Sbostic 
44251151Sbostic static struct disklabel *
44351151Sbostic getdisklabel(s, fd)
44451151Sbostic 	char *s;
44551151Sbostic 	int fd;
44651151Sbostic {
44751151Sbostic 	static struct disklabel lab;
44851151Sbostic 
44951151Sbostic 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
45051151Sbostic #ifdef COMPAT
45151151Sbostic 		if (disktype) {
45251151Sbostic 			struct disklabel *lp, *getdiskbyname();
45351151Sbostic 
45451151Sbostic 			unlabeled++;
45551151Sbostic 			lp = getdiskbyname(disktype);
45651151Sbostic 			if (lp == NULL)
45751151Sbostic 				fatal("%s: unknown disk type", disktype);
45851151Sbostic 			return (lp);
45951151Sbostic 		}
46051151Sbostic #endif
46151151Sbostic 		(void)fprintf(stderr,
46251151Sbostic 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
46351151Sbostic 		fatal(lmsg, s);
46451151Sbostic 	}
46551151Sbostic 	return (&lab);
46651151Sbostic }
46751151Sbostic 
46851151Sbostic 
46951151Sbostic static struct disklabel *
47051151Sbostic debug_readlabel(fd)
47151151Sbostic 	int fd;
47251151Sbostic {
47351151Sbostic 	static struct disklabel lab;
47451151Sbostic 	int n;
47551151Sbostic 
47651151Sbostic 	if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0)
47751151Sbostic 		fatal("unable to read disk label: %s", strerror(errno));
47851151Sbostic 	else if (n < sizeof(struct disklabel))
47951151Sbostic 		fatal("short read of disklabel: %d of %d bytes", n,
48051151Sbostic 			sizeof(struct disklabel));
48151151Sbostic 	return(&lab);
48251151Sbostic }
48351151Sbostic 
48451151Sbostic static void
48551151Sbostic rewritelabel(s, fd, lp)
48651151Sbostic 	char *s;
48751151Sbostic 	int fd;
48851151Sbostic 	register struct disklabel *lp;
48951151Sbostic {
49051151Sbostic #ifdef COMPAT
49151151Sbostic 	if (unlabeled)
49251151Sbostic 		return;
49351151Sbostic #endif
49451151Sbostic 	lp->d_checksum = 0;
49551151Sbostic 	lp->d_checksum = dkcksum(lp);
49651151Sbostic 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
49751151Sbostic 		(void)fprintf(stderr,
49851151Sbostic 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
49951151Sbostic 		fatal("%s: can't rewrite disk label", s);
50051151Sbostic 	}
50151151Sbostic #if vax
50251151Sbostic 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
50351151Sbostic 		register i;
50451151Sbostic 		int cfd;
50551151Sbostic 		daddr_t alt;
50651151Sbostic 		char specname[64];
50751151Sbostic 		char blk[1024];
50851151Sbostic 		char *cp;
50951151Sbostic 
51051151Sbostic 		/*
51151151Sbostic 		 * Make name for 'c' partition.
51251151Sbostic 		 */
51351151Sbostic 		strcpy(specname, s);
51451151Sbostic 		cp = specname + strlen(specname) - 1;
51551151Sbostic 		if (!isdigit(*cp))
51651151Sbostic 			*cp = 'c';
51751151Sbostic 		cfd = open(specname, O_WRONLY);
51851151Sbostic 		if (cfd < 0)
51951151Sbostic 			fatal("%s: %s", specname, strerror(errno));
52051151Sbostic 		bzero(blk, sizeof(blk));
52151151Sbostic 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
52251151Sbostic 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
52351151Sbostic 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
52451151Sbostic 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
52551151Sbostic 			    L_SET) == -1)
52651151Sbostic 				fatal("lseek to badsector area: %s",
52751151Sbostic 				    strerror(errno));
52851151Sbostic 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
52951151Sbostic 				fprintf(stderr,
53051151Sbostic 				    "%s: alternate label %d write: %s\n",
53151151Sbostic 				    progname, i/2, strerror(errno));
53251151Sbostic 		}
53351151Sbostic 		close(cfd);
53451151Sbostic 	}
53551151Sbostic #endif
53651151Sbostic }
53751151Sbostic 
53851151Sbostic void
53951151Sbostic usage()
54051151Sbostic {
54151151Sbostic 	if (mfs) {
54251151Sbostic 		fprintf(stderr,
54351151Sbostic 		    "usage: mfs [ -fsoptions ] special-device mount-point\n");
54451151Sbostic 	} else
54551151Sbostic 		fprintf(stderr,
54651151Sbostic 		    "usage: newfs [ -fsoptions ] special-device%s\n",
54751151Sbostic #ifdef COMPAT
54851151Sbostic 		    " [device-type]");
54951151Sbostic #else
55051151Sbostic 		    "");
55151151Sbostic #endif
55251151Sbostic 	fprintf(stderr, "where fsoptions are:\n");
55351151Sbostic 	fprintf(stderr, "\t-B LFS segment size\n");
55451151Sbostic 	fprintf(stderr, "\t-D debug\n");
55551151Sbostic 	fprintf(stderr, "\t-F mount flags\n");
55651151Sbostic 	fprintf(stderr, "\t-L create LFS file system\n");
55751151Sbostic 	fprintf(stderr,
55851151Sbostic 	    "\t-N do not create file system, just print out parameters\n");
55951151Sbostic 	fprintf(stderr, "\t-S sector size\n");
56051151Sbostic #ifdef COMPAT
56151151Sbostic 	fprintf(stderr, "\t-T disktype\n");
56251151Sbostic #endif
56351151Sbostic 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
56451151Sbostic 	fprintf(stderr, "\t-b block size\n");
56551151Sbostic 	fprintf(stderr, "\t-c cylinders/group\n");
56651151Sbostic 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
56751151Sbostic 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
56851151Sbostic 	fprintf(stderr, "\t-f frag size\n");
56951151Sbostic 	fprintf(stderr, "\t-i number of bytes per inode\n");
57051151Sbostic 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
57151151Sbostic 	fprintf(stderr, "\t-l hardware sector interleave\n");
57251151Sbostic 	fprintf(stderr, "\t-m minimum free space %%\n");
57351151Sbostic 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
57451151Sbostic 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
57551151Sbostic 	fprintf(stderr, "\t-p spare sectors per track\n");
57651151Sbostic 	fprintf(stderr, "\t-r revolutions/minute\n");
57751151Sbostic 	fprintf(stderr, "\t-s file system size (sectors)\n");
57851151Sbostic 	fprintf(stderr, "\t-t tracks/cylinder\n");
57951151Sbostic 	fprintf(stderr, "\t-u sectors/track\n");
58051151Sbostic 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
58151151Sbostic 	exit(1);
58251151Sbostic }
583