newfs.c revision 37775
1/*
2 * Copyright (c) 1983, 1989, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)newfs.c	8.13 (Berkeley) 5/1/95";
43#endif
44static const char rcsid[] =
45	"$Id: newfs.c,v 1.21 1998/07/16 12:04:52 charnier Exp $";
46#endif /* not lint */
47
48/*
49 * newfs: friendly front end to mkfs
50 */
51#include <sys/param.h>
52#include <sys/stat.h>
53#include <sys/disklabel.h>
54#include <sys/file.h>
55#include <sys/mount.h>
56
57#include <ufs/ufs/dir.h>
58#include <ufs/ufs/dinode.h>
59#include <ufs/ffs/fs.h>
60#include <ufs/ufs/ufsmount.h>
61
62#include <ctype.h>
63#include <err.h>
64#include <errno.h>
65#include <paths.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
69#include <syslog.h>
70#include <unistd.h>
71
72#ifdef MFS
73#include <sys/types.h>
74#include <sys/mman.h>
75#endif
76
77#if __STDC__
78#include <stdarg.h>
79#else
80#include <varargs.h>
81#endif
82
83#include "mntopts.h"
84
85struct mntopt mopts[] = {
86	MOPT_STDOPTS,
87	MOPT_ASYNC,
88	{ NULL },
89};
90
91#if __STDC__
92void	fatal(const char *fmt, ...);
93#else
94void	fatal();
95#endif
96
97#define	COMPAT			/* allow non-labeled disks */
98
99/*
100 * The following two constants set the default block and fragment sizes.
101 * Both constants must be a power of 2 and meet the following constraints:
102 *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
103 *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
104 *	DESBLKSIZE / DESFRAGSIZE <= 8
105 */
106#define	DFL_FRAGSIZE	1024
107#define	DFL_BLKSIZE	8192
108
109/*
110 * Cylinder groups may have up to many cylinders. The actual
111 * number used depends upon how much information can be stored
112 * on a single cylinder. The default is to use 16 cylinders
113 * per group.
114 */
115#define	DESCPG		16	/* desired fs_cpg */
116
117/*
118 * Once upon a time...
119 *    ROTDELAY gives the minimum number of milliseconds to initiate
120 *    another disk transfer on the same cylinder. It is used in
121 *    determining the rotationally optimal layout for disk blocks
122 *    within a file; the default of fs_rotdelay is 4ms.
123 *
124 * ...but now we make this 0 to disable the rotdelay delay because
125 * modern drives with read/write-behind achieve higher performance
126 * without the delay.
127 */
128#define ROTDELAY	0
129
130/*
131 * MAXBLKPG determines the maximum number of data blocks which are
132 * placed in a single cylinder group. The default is one indirect
133 * block worth of data blocks.
134 */
135#define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
136
137/*
138 * Each file system has a number of inodes statically allocated.
139 * We allocate one inode slot per NFPI fragments, expecting this
140 * to be far more than we will ever need.
141 */
142#define	NFPI		4
143
144/*
145 * Once upon a time...
146 *    For each cylinder we keep track of the availability of blocks at different
147 *    rotational positions, so that we can lay out the data to be picked
148 *    up with minimum rotational latency.  NRPOS is the default number of
149 *    rotational positions that we distinguish.  With NRPOS of 8 the resolution
150 *    of our summary information is 2ms for a typical 3600 rpm drive.
151 *
152 * ...but now we make this 1 (which essentially disables the rotational
153 * position table because modern drives with read-ahead and write-behind do
154 * better without the rotational position table.
155 */
156#define	NRPOS		1	/* number distinct rotational positions */
157
158/*
159 * About the same time as the above, we knew what went where on the disks.
160 * no longer so, so kill the code which finds the different platters too...
161 * We do this by saying one head, with a lot of sectors on it.
162 * The number of sectors are used to determine the size of a cyl-group.
163 * Kirk suggested one or two meg per "cylinder" so we say two.
164 */
165
166#define NTRACKS		1	/* number of heads */
167
168#define NSECTORS	4096	/* number of sectors */
169
170int	mfs;			/* run as the memory based filesystem */
171int	Nflag;			/* run without writing file system */
172int	Oflag;			/* format as an 4.3BSD file system */
173int	fssize;			/* file system size */
174int	ntracks = NTRACKS;	/* # tracks/cylinder */
175int	nsectors = NSECTORS;	/* # sectors/track */
176int	nphyssectors;		/* # sectors/track including spares */
177int	secpercyl;		/* sectors per cylinder */
178int	trackspares = -1;	/* spare sectors per track */
179int	cylspares = -1;		/* spare sectors per cylinder */
180int	sectorsize;		/* bytes/sector */
181int	realsectorsize;		/* bytes/sector in hardware */
182int	rpm;			/* revolutions/minute of drive */
183int	interleave;		/* hardware sector interleave */
184int	trackskew = -1;		/* sector 0 skew, per track */
185int	headswitch;		/* head switch time, usec */
186int	trackseek;		/* track-to-track seek, usec */
187int	fsize = 0;		/* fragment size */
188int	bsize = 0;		/* block size */
189int	cpg = DESCPG;		/* cylinders/cylinder group */
190int	cpgflg;			/* cylinders/cylinder group flag was given */
191int	minfree = MINFREE;	/* free space threshold */
192int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
193int	density;		/* number of bytes per inode */
194int	maxcontig = 0;		/* max contiguous blocks to allocate */
195int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
196int	maxbpg;			/* maximum blocks per file in a cyl group */
197int	nrpos = NRPOS;		/* # of distinguished rotational positions */
198int	bbsize = BBSIZE;	/* boot block size */
199int	sbsize = SBSIZE;	/* superblock size */
200int	mntflags = MNT_ASYNC;	/* flags to be passed to mount */
201int	t_or_u_flag = 0;	/* user has specified -t or -u */
202u_long	memleft;		/* virtual memory available */
203caddr_t	membase;		/* start address of memory based filesystem */
204char	*filename;
205#ifdef COMPAT
206char	*disktype;
207int	unlabeled;
208#endif
209
210char	device[MAXPATHLEN];
211char	*progname;
212
213extern void mkfs __P((struct partition *, char *, int, int));
214static void usage __P((void));
215
216int
217main(argc, argv)
218	int argc;
219	char *argv[];
220{
221	register int ch;
222	register struct partition *pp;
223	register struct disklabel *lp;
224	struct disklabel mfsfakelabel;
225	struct disklabel *getdisklabel();
226	struct partition oldpartition;
227	struct stat st;
228	struct statfs *mp;
229	int fsi, fso, len, n;
230	char *cp, *s1, *s2, *special, *opstring;
231#ifdef MFS
232	struct vfsconf vfc;
233	int error;
234	char buf[BUFSIZ];
235#endif
236
237	if ((progname = strrchr(*argv, '/')))
238		++progname;
239	else
240		progname = *argv;
241
242	if (strstr(progname, "mfs")) {
243		mfs = 1;
244		Nflag++;
245	}
246
247	opstring = mfs ?
248	    "NF:T:a:b:c:d:e:f:i:m:o:s:" :
249	    "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
250	while ((ch = getopt(argc, argv, opstring)) != -1)
251		switch (ch) {
252		case 'N':
253			Nflag = 1;
254			break;
255		case 'O':
256			Oflag = 1;
257			break;
258		case 'S':
259			if ((sectorsize = atoi(optarg)) <= 0)
260				fatal("%s: bad sector size", optarg);
261			break;
262#ifdef COMPAT
263		case 'T':
264			disktype = optarg;
265			break;
266#endif
267		case 'F':
268			filename = optarg;
269			break;
270		case 'a':
271			if ((maxcontig = atoi(optarg)) <= 0)
272				fatal("%s: bad maximum contiguous blocks",
273				    optarg);
274			break;
275		case 'b':
276			if ((bsize = atoi(optarg)) < MINBSIZE)
277				fatal("%s: bad block size", optarg);
278			break;
279		case 'c':
280			if ((cpg = atoi(optarg)) <= 0)
281				fatal("%s: bad cylinders/group", optarg);
282			cpgflg++;
283			break;
284		case 'd':
285			if ((rotdelay = atoi(optarg)) < 0)
286				fatal("%s: bad rotational delay", optarg);
287			break;
288		case 'e':
289			if ((maxbpg = atoi(optarg)) <= 0)
290		fatal("%s: bad blocks per file in a cylinder group",
291				    optarg);
292			break;
293		case 'f':
294			if ((fsize = atoi(optarg)) <= 0)
295				fatal("%s: bad fragment size", optarg);
296			break;
297		case 'i':
298			if ((density = atoi(optarg)) <= 0)
299				fatal("%s: bad bytes per inode", optarg);
300			break;
301		case 'k':
302			if ((trackskew = atoi(optarg)) < 0)
303				fatal("%s: bad track skew", optarg);
304			break;
305		case 'l':
306			if ((interleave = atoi(optarg)) <= 0)
307				fatal("%s: bad interleave", optarg);
308			break;
309		case 'm':
310			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
311				fatal("%s: bad free space %%", optarg);
312			break;
313		case 'n':
314			if ((nrpos = atoi(optarg)) < 0)
315				fatal("%s: bad rotational layout count",
316				    optarg);
317			if (nrpos == 0)
318				nrpos = 1;
319			break;
320		case 'o':
321			if (mfs)
322				getmntopts(optarg, mopts, &mntflags, 0);
323			else {
324				if (strcmp(optarg, "space") == 0)
325					opt = FS_OPTSPACE;
326				else if (strcmp(optarg, "time") == 0)
327					opt = FS_OPTTIME;
328				else
329	fatal("%s: unknown optimization preference: use `space' or `time'");
330			}
331			break;
332		case 'p':
333			if ((trackspares = atoi(optarg)) < 0)
334				fatal("%s: bad spare sectors per track",
335				    optarg);
336			break;
337		case 'r':
338			if ((rpm = atoi(optarg)) <= 0)
339				fatal("%s: bad revolutions/minute", optarg);
340			break;
341		case 's':
342			if ((fssize = atoi(optarg)) <= 0)
343				fatal("%s: bad file system size", optarg);
344			break;
345		case 't':
346			t_or_u_flag++;
347			if ((ntracks = atoi(optarg)) < 0)
348				fatal("%s: bad total tracks", optarg);
349			break;
350		case 'u':
351			t_or_u_flag++;
352			if ((nsectors = atoi(optarg)) < 0)
353				fatal("%s: bad sectors/track", optarg);
354			break;
355		case 'x':
356			if ((cylspares = atoi(optarg)) < 0)
357				fatal("%s: bad spare sectors per cylinder",
358				    optarg);
359			break;
360		case '?':
361		default:
362			usage();
363		}
364	argc -= optind;
365	argv += optind;
366
367	if (argc != 2 && (mfs || argc != 1))
368		usage();
369
370	special = argv[0];
371	/* Copy the NetBSD way of faking up a disk label */
372        if (mfs && !strcmp(special, "swap")) {
373                /*
374                 * it's an MFS, mounted on "swap."  fake up a label.
375                 * XXX XXX XXX
376                 */
377                fso = -1;       /* XXX; normally done below. */
378
379                memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
380                mfsfakelabel.d_secsize = 512;
381                mfsfakelabel.d_nsectors = 64;
382                mfsfakelabel.d_ntracks = 16;
383                mfsfakelabel.d_ncylinders = 16;
384                mfsfakelabel.d_secpercyl = 1024;
385                mfsfakelabel.d_secperunit = 16384;
386                mfsfakelabel.d_rpm = 3600;
387                mfsfakelabel.d_interleave = 1;
388                mfsfakelabel.d_npartitions = 1;
389                mfsfakelabel.d_partitions[0].p_size = 16384;
390                mfsfakelabel.d_partitions[0].p_fsize = 1024;
391                mfsfakelabel.d_partitions[0].p_frag = 8;
392                mfsfakelabel.d_partitions[0].p_cpg = 16;
393
394                lp = &mfsfakelabel;
395                pp = &mfsfakelabel.d_partitions[0];
396
397                goto havelabel;
398        }
399
400	cp = strrchr(special, '/');
401	if (cp == 0) {
402		/*
403		 * No path prefix; try /dev/r%s then /dev/%s.
404		 */
405		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
406		if (stat(device, &st) == -1)
407			(void)sprintf(device, "%s%s", _PATH_DEV, special);
408		special = device;
409	}
410	if (Nflag) {
411		fso = -1;
412	} else {
413		fso = open(special, O_WRONLY);
414		if (fso < 0)
415			fatal("%s: %s", special, strerror(errno));
416
417		/* Bail if target special is mounted */
418		n = getmntinfo(&mp, MNT_NOWAIT);
419		if (n == 0)
420			fatal("%s: getmntinfo: %s", special, strerror(errno));
421
422		len = sizeof(_PATH_DEV) - 1;
423		s1 = special;
424		if (strncmp(_PATH_DEV, s1, len) == 0)
425			s1 += len;
426
427		while (--n >= 0) {
428			s2 = mp->f_mntfromname;
429			if (strncmp(_PATH_DEV, s2, len) == 0) {
430				s2 += len - 1;
431				*s2 = 'r';
432			}
433			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
434				fatal("%s is mounted on %s",
435				    special, mp->f_mntonname);
436			++mp;
437		}
438	}
439	if (mfs && disktype != NULL) {
440		lp = (struct disklabel *)getdiskbyname(disktype);
441		if (lp == NULL)
442			fatal("%s: unknown disk type", disktype);
443		pp = &lp->d_partitions[1];
444	} else {
445		fsi = open(special, O_RDONLY);
446		if (fsi < 0)
447			fatal("%s: %s", special, strerror(errno));
448		if (fstat(fsi, &st) < 0)
449			fatal("%s: %s", special, strerror(errno));
450		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
451			printf("%s: %s: not a character-special device\n",
452			    progname, special);
453		cp = strchr(argv[0], '\0') - 1;
454		if (cp == (char *)-1 ||
455		    ((*cp < 'a' || *cp > 'h') && !isdigit(*cp)))
456			fatal("%s: can't figure out file system partition",
457			    argv[0]);
458#ifdef COMPAT
459		if (!mfs && disktype == NULL)
460			disktype = argv[1];
461#endif
462		lp = getdisklabel(special, fsi);
463		if (isdigit(*cp))
464			pp = &lp->d_partitions[0];
465		else
466			pp = &lp->d_partitions[*cp - 'a'];
467		if (pp->p_size == 0)
468			fatal("%s: `%c' partition is unavailable",
469			    argv[0], *cp);
470		if (pp->p_fstype == FS_BOOT)
471			fatal("%s: `%c' partition overlaps boot program",
472			      argv[0], *cp);
473	}
474havelabel:
475	if (fssize == 0)
476		fssize = pp->p_size;
477	if (fssize > pp->p_size && !mfs)
478	       fatal("%s: maximum file system size on the `%c' partition is %d",
479			argv[0], *cp, pp->p_size);
480	if (rpm == 0) {
481		rpm = lp->d_rpm;
482		if (rpm <= 0)
483			rpm = 3600;
484	}
485	if (ntracks == 0) {
486		ntracks = lp->d_ntracks;
487		if (ntracks <= 0)
488			fatal("%s: no default #tracks", argv[0]);
489	}
490	if (nsectors == 0) {
491		nsectors = lp->d_nsectors;
492		if (nsectors <= 0)
493			fatal("%s: no default #sectors/track", argv[0]);
494	}
495	if (sectorsize == 0) {
496		sectorsize = lp->d_secsize;
497		if (sectorsize <= 0)
498			fatal("%s: no default sector size", argv[0]);
499	}
500	if (trackskew == -1) {
501		trackskew = lp->d_trackskew;
502		if (trackskew < 0)
503			trackskew = 0;
504	}
505	if (interleave == 0) {
506		interleave = lp->d_interleave;
507		if (interleave <= 0)
508			interleave = 1;
509	}
510	if (fsize == 0) {
511		fsize = pp->p_fsize;
512		if (fsize <= 0)
513			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
514	}
515	if (bsize == 0) {
516		bsize = pp->p_frag * pp->p_fsize;
517		if (bsize <= 0)
518			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
519	}
520	/*
521	 * Maxcontig sets the default for the maximum number of blocks
522	 * that may be allocated sequentially. With filesystem clustering
523	 * it is possible to allocate contiguous blocks up to the maximum
524	 * transfer size permitted by the controller or buffering.
525	 */
526	if (maxcontig == 0)
527		maxcontig = MAX(1, MAXPHYS / bsize - 1);
528	if (density == 0)
529		density = NFPI * fsize;
530	if (minfree < MINFREE && opt != FS_OPTSPACE) {
531		fprintf(stderr, "Warning: changing optimization to space ");
532		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
533		opt = FS_OPTSPACE;
534	}
535	if (trackspares == -1) {
536		trackspares = lp->d_sparespertrack;
537		if (trackspares < 0)
538			trackspares = 0;
539	}
540	nphyssectors = nsectors + trackspares;
541	if (cylspares == -1) {
542		cylspares = lp->d_sparespercyl;
543		if (cylspares < 0)
544			cylspares = 0;
545	}
546	secpercyl = nsectors * ntracks - cylspares;
547	/*
548	 * Only complain if -t or -u have been specified; the default
549	 * case (4096 sectors per cylinder) is intended to disagree
550	 * with the disklabel.
551	 */
552	if (t_or_u_flag && secpercyl != lp->d_secpercyl)
553		fprintf(stderr, "%s (%d) %s (%lu)\n",
554			"Warning: calculated sectors per cylinder", secpercyl,
555			"disagrees with disk label", (u_long)lp->d_secpercyl);
556	if (maxbpg == 0)
557		maxbpg = MAXBLKPG(bsize);
558	headswitch = lp->d_headswitch;
559	trackseek = lp->d_trkseek;
560#ifdef notdef /* label may be 0 if faked up by kernel */
561	bbsize = lp->d_bbsize;
562	sbsize = lp->d_sbsize;
563#endif
564	oldpartition = *pp;
565#ifdef tahoe
566	realsectorsize = sectorsize;
567	if (sectorsize != DEV_BSIZE) {		/* XXX */
568		int secperblk = DEV_BSIZE / sectorsize;
569
570		sectorsize = DEV_BSIZE;
571		nsectors /= secperblk;
572		nphyssectors /= secperblk;
573		secpercyl /= secperblk;
574		fssize /= secperblk;
575		pp->p_size /= secperblk;
576	}
577#else
578	realsectorsize = sectorsize;
579	if (sectorsize != DEV_BSIZE) {		/* XXX */
580		int secperblk = sectorsize / DEV_BSIZE;
581
582		sectorsize = DEV_BSIZE;
583		nsectors *= secperblk;
584		nphyssectors *= secperblk;
585		secpercyl *= secperblk;
586		fssize *= secperblk;
587		pp->p_size *= secperblk;
588	}
589#endif
590	mkfs(pp, special, fsi, fso);
591#ifdef tahoe
592	if (realsectorsize != DEV_BSIZE)
593		pp->p_size *= DEV_BSIZE / realsectorsize;
594#else
595	if (realsectorsize != DEV_BSIZE)
596		pp->p_size /= realsectorsize /DEV_BSIZE;
597#endif
598	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
599		rewritelabel(special, fso, lp);
600	if (!Nflag)
601		close(fso);
602	close(fsi);
603#ifdef MFS
604	if (mfs) {
605		struct mfs_args args;
606
607		sprintf(buf, "mfs:%d", getpid());
608		args.fspec = buf;
609		args.export.ex_root = -2;
610		if (mntflags & MNT_RDONLY)
611			args.export.ex_flags = MNT_EXRDONLY;
612		else
613			args.export.ex_flags = 0;
614		args.base = membase;
615		args.size = fssize * sectorsize;
616
617		error = getvfsbyname("mfs", &vfc);
618		if (error && vfsisloadable("mfs")) {
619			if (vfsload("mfs"))
620				fatal("vfsload(mfs)");
621			endvfsent();	/* flush cache */
622			error = getvfsbyname("mfs", &vfc);
623		}
624		if (error)
625			fatal("mfs filesystem not available");
626
627		if (mount(vfc.vfc_name, argv[1], mntflags, &args) < 0)
628			fatal("%s: %s", argv[1], strerror(errno));
629		if(filename) {
630			munmap(membase,fssize * sectorsize);
631		}
632	}
633#endif
634	exit(0);
635}
636
637#ifdef COMPAT
638char lmsg[] = "%s: can't read disk label; disk type must be specified";
639#else
640char lmsg[] = "%s: can't read disk label";
641#endif
642
643struct disklabel *
644getdisklabel(s, fd)
645	char *s;
646	int fd;
647{
648	static struct disklabel lab;
649
650	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
651#ifdef COMPAT
652		if (disktype) {
653			struct disklabel *lp, *getdiskbyname();
654
655			unlabeled++;
656			lp = getdiskbyname(disktype);
657			if (lp == NULL)
658				fatal("%s: unknown disk type", disktype);
659			return (lp);
660		}
661#endif
662		warn("ioctl (GDINFO)");
663		fatal(lmsg, s);
664	}
665	return (&lab);
666}
667
668rewritelabel(s, fd, lp)
669	char *s;
670	int fd;
671	register struct disklabel *lp;
672{
673#ifdef COMPAT
674	if (unlabeled)
675		return;
676#endif
677	lp->d_checksum = 0;
678	lp->d_checksum = dkcksum(lp);
679	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
680		warn("ioctl (WDINFO)");
681		fatal("%s: can't rewrite disk label", s);
682	}
683}
684
685/*VARARGS*/
686void
687#if __STDC__
688fatal(const char *fmt, ...)
689#else
690fatal(fmt, va_alist)
691	char *fmt;
692	va_dcl
693#endif
694{
695	va_list ap;
696
697#if __STDC__
698	va_start(ap, fmt);
699#else
700	va_start(ap);
701#endif
702	if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
703		openlog(progname, LOG_CONS, LOG_DAEMON);
704		vsyslog(LOG_ERR, fmt, ap);
705		closelog();
706	} else {
707		vwarnx(fmt, ap);
708	}
709	va_end(ap);
710	exit(1);
711	/*NOTREACHED*/
712}
713
714static void
715usage()
716{
717	if (mfs) {
718		fprintf(stderr,
719		    "usage: %s [ -fsoptions ] special-device mount-point\n",
720			progname);
721	} else
722		fprintf(stderr,
723		    "usage: %s [ -fsoptions ] special-device%s\n",
724		    progname,
725#ifdef COMPAT
726		    " [device-type]");
727#else
728		    "");
729#endif
730	fprintf(stderr, "where fsoptions are:\n");
731	fprintf(stderr,
732	    "\t-N do not create file system, just print out parameters\n");
733	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
734	fprintf(stderr, "\t-S sector size\n");
735#ifdef COMPAT
736	fprintf(stderr, "\t-T disktype\n");
737#endif
738	fprintf(stderr, "\t-a maximum contiguous blocks\n");
739	fprintf(stderr, "\t-b block size\n");
740	fprintf(stderr, "\t-c cylinders/group\n");
741	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
742	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
743	fprintf(stderr, "\t-f frag size\n");
744	fprintf(stderr, "\t-i number of bytes per inode\n");
745	fprintf(stderr, "\t-k sector 0 skew, per track\n");
746	fprintf(stderr, "\t-l hardware sector interleave\n");
747	fprintf(stderr, "\t-m minimum free space %%\n");
748	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
749	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
750	fprintf(stderr, "\t-p spare sectors per track\n");
751	fprintf(stderr, "\t-s file system size (sectors)\n");
752	fprintf(stderr, "\t-r revolutions/minute\n");
753	fprintf(stderr, "\t-t tracks/cylinder\n");
754	fprintf(stderr, "\t-u sectors/track\n");
755	fprintf(stderr, "\t-x spare sectors per cylinder\n");
756	exit(1);
757}
758