xref: /freebsd/sbin/newfs/newfs.c (revision 99e8005137088aafb1350e23b113d69b01b0820f)
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
35 static 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
42 static char sccsid[] = "@(#)newfs.c	8.13 (Berkeley) 5/1/95";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
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 
85 struct mntopt mopts[] = {
86 	MOPT_STDOPTS,
87 	MOPT_ASYNC,
88 	{ NULL },
89 };
90 
91 #if __STDC__
92 void	fatal(const char *fmt, ...);
93 #else
94 void	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 22 cylinders
113  * per group, which seems to be the largest value allowed given
114  * all the other default values.
115  */
116 #define	DESCPG		22	/* desired fs_cpg */
117 
118 /*
119  * Once upon a time...
120  *    ROTDELAY gives the minimum number of milliseconds to initiate
121  *    another disk transfer on the same cylinder. It is used in
122  *    determining the rotationally optimal layout for disk blocks
123  *    within a file; the default of fs_rotdelay is 4ms.
124  *
125  * ...but now we make this 0 to disable the rotdelay delay because
126  * modern drives with read/write-behind achieve higher performance
127  * without the delay.
128  */
129 #define ROTDELAY	0
130 
131 /*
132  * MAXBLKPG determines the maximum number of data blocks which are
133  * placed in a single cylinder group. The default is one indirect
134  * block worth of data blocks.
135  */
136 #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
137 
138 /*
139  * Each file system has a number of inodes statically allocated.
140  * We allocate one inode slot per NFPI fragments, expecting this
141  * to be far more than we will ever need.
142  */
143 #define	NFPI		4
144 
145 /*
146  * Once upon a time...
147  *    For each cylinder we keep track of the availability of blocks at different
148  *    rotational positions, so that we can lay out the data to be picked
149  *    up with minimum rotational latency.  NRPOS is the default number of
150  *    rotational positions that we distinguish.  With NRPOS of 8 the resolution
151  *    of our summary information is 2ms for a typical 3600 rpm drive.
152  *
153  * ...but now we make this 1 (which essentially disables the rotational
154  * position table because modern drives with read-ahead and write-behind do
155  * better without the rotational position table.
156  */
157 #define	NRPOS		1	/* number distinct rotational positions */
158 
159 /*
160  * About the same time as the above, we knew what went where on the disks.
161  * no longer so, so kill the code which finds the different platters too...
162  * We do this by saying one head, with a lot of sectors on it.
163  * The number of sectors are used to determine the size of a cyl-group.
164  * Kirk suggested one or two meg per "cylinder" so we say two.
165  */
166 #define NTRACKS		1	/* number of heads */
167 #define NSECTORS	4096	/* number of sectors */
168 
169 int	mfs;			/* run as the memory based filesystem */
170 char	*mfs_mtpt;		/* mount point for mfs		*/
171 struct stat mfs_mtstat;		/* stat prior to mount		*/
172 int	Nflag;			/* run without writing file system */
173 int	Oflag;			/* format as an 4.3BSD file system */
174 int	Uflag;			/* enable soft updates for file system */
175 int	fssize;			/* file system size */
176 int	ntracks = NTRACKS;	/* # tracks/cylinder */
177 int	nsectors = NSECTORS;	/* # sectors/track */
178 int	nphyssectors;		/* # sectors/track including spares */
179 int	secpercyl;		/* sectors per cylinder */
180 int	trackspares = -1;	/* spare sectors per track */
181 int	cylspares = -1;		/* spare sectors per cylinder */
182 int	sectorsize;		/* bytes/sector */
183 int	realsectorsize;		/* bytes/sector in hardware */
184 int	rpm;			/* revolutions/minute of drive */
185 int	interleave;		/* hardware sector interleave */
186 int	trackskew = -1;		/* sector 0 skew, per track */
187 int	headswitch;		/* head switch time, usec */
188 int	trackseek;		/* track-to-track seek, usec */
189 int	fsize = 0;		/* fragment size */
190 int	bsize = 0;		/* block size */
191 int	cpg = DESCPG;		/* cylinders/cylinder group */
192 int	cpgflg;			/* cylinders/cylinder group flag was given */
193 int	minfree = MINFREE;	/* free space threshold */
194 int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
195 int	density;		/* number of bytes per inode */
196 int	maxcontig = 0;		/* max contiguous blocks to allocate */
197 int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
198 int	maxbpg;			/* maximum blocks per file in a cyl group */
199 int	nrpos = NRPOS;		/* # of distinguished rotational positions */
200 int	avgfilesize = AVFILESIZ;/* expected average file size */
201 int	avgfilesperdir = AFPDIR;/* expected number of files per directory */
202 int	bbsize = BBSIZE;	/* boot block size */
203 int	sbsize = SBSIZE;	/* superblock size */
204 int	mntflags = MNT_ASYNC;	/* flags to be passed to mount */
205 int	t_or_u_flag = 0;	/* user has specified -t or -u */
206 u_long	memleft;		/* virtual memory available */
207 caddr_t	membase;		/* start address of memory based filesystem */
208 char	*filename;
209 #ifdef COMPAT
210 char	*disktype;
211 int	unlabeled;
212 #endif
213 
214 char	device[MAXPATHLEN];
215 char	*progname;
216 
217 extern void mkfs __P((struct partition *, char *, int, int));
218 static void usage __P((void));
219 
220 int
221 main(argc, argv)
222 	int argc;
223 	char *argv[];
224 {
225 	register int ch;
226 	register struct partition *pp;
227 	register struct disklabel *lp;
228 	struct disklabel mfsfakelabel;
229 	struct disklabel *getdisklabel();
230 	struct partition oldpartition;
231 	struct stat st;
232 	struct statfs *mp;
233 	int fsi, fso, len, n, vflag;
234 	char *cp, *s1, *s2, *special, *opstring;
235 #ifdef MFS
236 	struct vfsconf vfc;
237 	int error;
238 	char buf[BUFSIZ];
239 #endif
240 
241 	vflag = 0;
242 	if ((progname = strrchr(*argv, '/')))
243 		++progname;
244 	else
245 		progname = *argv;
246 
247 	if (strstr(progname, "mfs")) {
248 		mfs = 1;
249 		Nflag++;
250 		fprintf(stderr,
251 "WARNING: MFS is being phased out in preference for md devices\n"
252 "WARNING: Please see mdconfig(8) for details\n"
253 "WARNING: Continuing in 15 seconds\n");
254 		sleep(15);
255 
256 	}
257 
258 	opstring = mfs ?
259 	    "NF:T:Ua:b:c:d:e:f:g:h:i:m:o:s:" :
260 	    "NOS:T:Ua:b:c:d:e:f:g:h:i:k:l:m:n:o:p:r:s:t:u:vx:";
261 	while ((ch = getopt(argc, argv, opstring)) != -1)
262 		switch (ch) {
263 		case 'N':
264 			Nflag = 1;
265 			break;
266 		case 'O':
267 			Oflag = 1;
268 			break;
269 		case 'S':
270 			if ((sectorsize = atoi(optarg)) <= 0)
271 				fatal("%s: bad sector size", optarg);
272 			break;
273 #ifdef COMPAT
274 		case 'T':
275 			disktype = optarg;
276 			break;
277 #endif
278 		case 'F':
279 			filename = optarg;
280 			break;
281 		case 'U':
282 			Uflag = 1;
283 			break;
284 		case 'a':
285 			if ((maxcontig = atoi(optarg)) <= 0)
286 				fatal("%s: bad maximum contiguous blocks",
287 				    optarg);
288 			break;
289 		case 'b':
290 			if ((bsize = atoi(optarg)) < MINBSIZE)
291 				fatal("%s: bad block size", optarg);
292 			break;
293 		case 'c':
294 			if ((cpg = atoi(optarg)) <= 0)
295 				fatal("%s: bad cylinders/group", optarg);
296 			cpgflg++;
297 			break;
298 		case 'd':
299 			if ((rotdelay = atoi(optarg)) < 0)
300 				fatal("%s: bad rotational delay", optarg);
301 			break;
302 		case 'e':
303 			if ((maxbpg = atoi(optarg)) <= 0)
304 		fatal("%s: bad blocks per file in a cylinder group",
305 				    optarg);
306 			break;
307 		case 'f':
308 			if ((fsize = atoi(optarg)) <= 0)
309 				fatal("%s: bad fragment size", optarg);
310 			break;
311 		case 'g':
312 			if ((avgfilesize = atoi(optarg)) <= 0)
313 				fatal("%s: bad average file size", optarg);
314 			break;
315 		case 'h':
316 			if ((avgfilesperdir = atoi(optarg)) <= 0)
317 				fatal("%s: bad average files per dir", optarg);
318 			break;
319 		case 'i':
320 			if ((density = atoi(optarg)) <= 0)
321 				fatal("%s: bad bytes per inode", optarg);
322 			break;
323 		case 'k':
324 			if ((trackskew = atoi(optarg)) < 0)
325 				fatal("%s: bad track skew", optarg);
326 			break;
327 		case 'l':
328 			if ((interleave = atoi(optarg)) <= 0)
329 				fatal("%s: bad interleave", optarg);
330 			break;
331 		case 'm':
332 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
333 				fatal("%s: bad free space %%", optarg);
334 			break;
335 		case 'n':
336 			if ((nrpos = atoi(optarg)) < 0)
337 				fatal("%s: bad rotational layout count",
338 				    optarg);
339 			if (nrpos == 0)
340 				nrpos = 1;
341 			break;
342 		case 'o':
343 			if (mfs)
344 				getmntopts(optarg, mopts, &mntflags, 0);
345 			else {
346 				if (strcmp(optarg, "space") == 0)
347 					opt = FS_OPTSPACE;
348 				else if (strcmp(optarg, "time") == 0)
349 					opt = FS_OPTTIME;
350 				else
351 	fatal("%s: unknown optimization preference: use `space' or `time'", optarg);
352 			}
353 			break;
354 		case 'p':
355 			if ((trackspares = atoi(optarg)) < 0)
356 				fatal("%s: bad spare sectors per track",
357 				    optarg);
358 			break;
359 		case 'r':
360 			if ((rpm = atoi(optarg)) <= 0)
361 				fatal("%s: bad revolutions/minute", optarg);
362 			break;
363 		case 's':
364 			if ((fssize = atoi(optarg)) <= 0)
365 				fatal("%s: bad file system size", optarg);
366 			break;
367 		case 't':
368 			t_or_u_flag++;
369 			if ((ntracks = atoi(optarg)) < 0)
370 				fatal("%s: bad total tracks", optarg);
371 			break;
372 		case 'u':
373 			t_or_u_flag++;
374 			if ((nsectors = atoi(optarg)) < 0)
375 				fatal("%s: bad sectors/track", optarg);
376 			break;
377 		case 'v':
378 			vflag = 1;
379 			break;
380 		case 'x':
381 			if ((cylspares = atoi(optarg)) < 0)
382 				fatal("%s: bad spare sectors per cylinder",
383 				    optarg);
384 			break;
385 		case '?':
386 		default:
387 			usage();
388 		}
389 	argc -= optind;
390 	argv += optind;
391 
392 	if (argc != 2 && (mfs || argc != 1))
393 		usage();
394 
395 	special = argv[0];
396 	/* Copy the NetBSD way of faking up a disk label */
397         if (mfs && !strcmp(special, "swap")) {
398                 /*
399                  * it's an MFS, mounted on "swap."  fake up a label.
400                  * XXX XXX XXX
401                  */
402                 fso = -1;       /* XXX; normally done below. */
403 
404                 memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
405                 mfsfakelabel.d_secsize = 512;
406                 mfsfakelabel.d_nsectors = 64;
407                 mfsfakelabel.d_ntracks = 16;
408                 mfsfakelabel.d_ncylinders = 16;
409                 mfsfakelabel.d_secpercyl = 1024;
410                 mfsfakelabel.d_secperunit = 16384;
411                 mfsfakelabel.d_rpm = 3600;
412                 mfsfakelabel.d_interleave = 1;
413                 mfsfakelabel.d_npartitions = 1;
414                 mfsfakelabel.d_partitions[0].p_size = 16384;
415                 mfsfakelabel.d_partitions[0].p_fsize = 1024;
416                 mfsfakelabel.d_partitions[0].p_frag = 8;
417                 mfsfakelabel.d_partitions[0].p_cpg = 16;
418 
419                 lp = &mfsfakelabel;
420                 pp = &mfsfakelabel.d_partitions[0];
421 
422                 goto havelabel;
423         }
424 
425 	cp = strrchr(special, '/');
426 	if (cp == 0) {
427 		/*
428 		 * No path prefix; try /dev/%s.
429 		 */
430 		(void)snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special);
431 		special = device;
432 	}
433 	if (Nflag) {
434 		fso = -1;
435 	} else {
436 		fso = open(special, O_WRONLY);
437 		if (fso < 0)
438 			fatal("%s: %s", special, strerror(errno));
439 
440 		/* Bail if target special is mounted */
441 		n = getmntinfo(&mp, MNT_NOWAIT);
442 		if (n == 0)
443 			fatal("%s: getmntinfo: %s", special, strerror(errno));
444 
445 		len = sizeof(_PATH_DEV) - 1;
446 		s1 = special;
447 		if (strncmp(_PATH_DEV, s1, len) == 0)
448 			s1 += len;
449 
450 		while (--n >= 0) {
451 			s2 = mp->f_mntfromname;
452 			if (strncmp(_PATH_DEV, s2, len) == 0) {
453 				s2 += len - 1;
454 				*s2 = 'r';
455 			}
456 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
457 				fatal("%s is mounted on %s",
458 				    special, mp->f_mntonname);
459 			++mp;
460 		}
461 	}
462 	if (mfs && disktype != NULL) {
463 		lp = (struct disklabel *)getdiskbyname(disktype);
464 		if (lp == NULL)
465 			fatal("%s: unknown disk type", disktype);
466 		pp = &lp->d_partitions[1];
467 	} else {
468 		fsi = open(special, O_RDONLY);
469 		if (fsi < 0)
470 			fatal("%s: %s", special, strerror(errno));
471 		if (fstat(fsi, &st) < 0)
472 			fatal("%s: %s", special, strerror(errno));
473 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
474 			printf("%s: %s: not a character-special device\n",
475 			    progname, special);
476  		cp = strchr(argv[0], '\0');
477  		if (cp == argv[0])
478  			fatal("null special file name");
479  		cp--;
480  		if (!vflag && (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
481 			fatal("%s: can't figure out file system partition",
482 			    argv[0]);
483 #ifdef COMPAT
484 		if (!mfs && disktype == NULL)
485 			disktype = argv[1];
486 #endif
487 		lp = getdisklabel(special, fsi);
488 		if (vflag || isdigit(*cp))
489 			pp = &lp->d_partitions[0];
490 		else
491 			pp = &lp->d_partitions[*cp - 'a'];
492 		if (pp->p_size == 0)
493 			fatal("%s: `%c' partition is unavailable",
494 			    argv[0], *cp);
495 		if (pp->p_fstype == FS_BOOT)
496 			fatal("%s: `%c' partition overlaps boot program",
497 			      argv[0], *cp);
498 	}
499 havelabel:
500 	if (fssize == 0)
501 		fssize = pp->p_size;
502 	if (fssize > pp->p_size && !mfs)
503 	       fatal("%s: maximum file system size on the `%c' partition is %d",
504 			argv[0], *cp, pp->p_size);
505 	if (rpm == 0) {
506 		rpm = lp->d_rpm;
507 		if (rpm <= 0)
508 			rpm = 3600;
509 	}
510 	if (ntracks == 0) {
511 		ntracks = lp->d_ntracks;
512 		if (ntracks <= 0)
513 			fatal("%s: no default #tracks", argv[0]);
514 	}
515 	if (nsectors == 0) {
516 		nsectors = lp->d_nsectors;
517 		if (nsectors <= 0)
518 			fatal("%s: no default #sectors/track", argv[0]);
519 	}
520 	if (sectorsize == 0) {
521 		sectorsize = lp->d_secsize;
522 		if (sectorsize <= 0)
523 			fatal("%s: no default sector size", argv[0]);
524 	}
525 	if (trackskew == -1) {
526 		trackskew = lp->d_trackskew;
527 		if (trackskew < 0)
528 			trackskew = 0;
529 	}
530 	if (interleave == 0) {
531 		interleave = lp->d_interleave;
532 		if (interleave <= 0)
533 			interleave = 1;
534 	}
535 	if (fsize == 0) {
536 		fsize = pp->p_fsize;
537 		if (fsize <= 0)
538 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
539 	}
540 	if (bsize == 0) {
541 		bsize = pp->p_frag * pp->p_fsize;
542 		if (bsize <= 0)
543 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
544 	}
545 	/*
546 	 * Maxcontig sets the default for the maximum number of blocks
547 	 * that may be allocated sequentially. With filesystem clustering
548 	 * it is possible to allocate contiguous blocks up to the maximum
549 	 * transfer size permitted by the controller or buffering.
550 	 */
551 	if (maxcontig == 0)
552 		maxcontig = MAX(1, MAXPHYS / bsize - 1);
553 	if (density == 0)
554 		density = NFPI * fsize;
555 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
556 		fprintf(stderr, "Warning: changing optimization to space ");
557 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
558 		opt = FS_OPTSPACE;
559 	}
560 	if (trackspares == -1) {
561 		trackspares = lp->d_sparespertrack;
562 		if (trackspares < 0)
563 			trackspares = 0;
564 	}
565 	nphyssectors = nsectors + trackspares;
566 	if (cylspares == -1) {
567 		cylspares = lp->d_sparespercyl;
568 		if (cylspares < 0)
569 			cylspares = 0;
570 	}
571 	secpercyl = nsectors * ntracks - cylspares;
572 	/*
573 	 * Only complain if -t or -u have been specified; the default
574 	 * case (4096 sectors per cylinder) is intended to disagree
575 	 * with the disklabel.
576 	 */
577 	if (t_or_u_flag && secpercyl != lp->d_secpercyl)
578 		fprintf(stderr, "%s (%d) %s (%lu)\n",
579 			"Warning: calculated sectors per cylinder", secpercyl,
580 			"disagrees with disk label", (u_long)lp->d_secpercyl);
581 	if (maxbpg == 0)
582 		maxbpg = MAXBLKPG(bsize);
583 	headswitch = lp->d_headswitch;
584 	trackseek = lp->d_trkseek;
585 #ifdef notdef /* label may be 0 if faked up by kernel */
586 	bbsize = lp->d_bbsize;
587 	sbsize = lp->d_sbsize;
588 #endif
589 	oldpartition = *pp;
590 #ifdef tahoe
591 	realsectorsize = sectorsize;
592 	if (sectorsize != DEV_BSIZE) {		/* XXX */
593 		int secperblk = DEV_BSIZE / sectorsize;
594 
595 		sectorsize = DEV_BSIZE;
596 		nsectors /= secperblk;
597 		nphyssectors /= secperblk;
598 		secpercyl /= secperblk;
599 		fssize /= secperblk;
600 		pp->p_size /= secperblk;
601 	}
602 #else
603 	realsectorsize = sectorsize;
604 	if (sectorsize != DEV_BSIZE) {		/* XXX */
605 		int secperblk = sectorsize / DEV_BSIZE;
606 
607 		sectorsize = DEV_BSIZE;
608 		nsectors *= secperblk;
609 		nphyssectors *= secperblk;
610 		secpercyl *= secperblk;
611 		fssize *= secperblk;
612 		pp->p_size *= secperblk;
613 	}
614 #endif
615 	if (mfs) {
616 		mfs_mtpt = argv[1];
617 		if (
618 		    stat(mfs_mtpt, &mfs_mtstat) < 0 ||
619 		    !S_ISDIR(mfs_mtstat.st_mode)
620 		) {
621 			fatal("mount point not dir: %s", mfs_mtpt);
622 		}
623 	}
624 	mkfs(pp, special, fsi, fso);
625 #ifdef tahoe
626 	if (realsectorsize != DEV_BSIZE)
627 		pp->p_size *= DEV_BSIZE / realsectorsize;
628 #else
629 	if (realsectorsize != DEV_BSIZE)
630 		pp->p_size /= realsectorsize /DEV_BSIZE;
631 #endif
632 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
633 		rewritelabel(special, fso, lp);
634 	if (!Nflag)
635 		close(fso);
636 	close(fsi);
637 #ifdef MFS
638 	if (mfs) {
639 		struct mfs_args args;
640 
641 		snprintf(buf, sizeof(buf), "mfs:%d", getpid());
642 		args.fspec = buf;
643 		args.export.ex_root = -2;
644 		if (mntflags & MNT_RDONLY)
645 			args.export.ex_flags = MNT_EXRDONLY;
646 		else
647 			args.export.ex_flags = 0;
648 		args.base = membase;
649 		args.size = fssize * sectorsize;
650 
651 		error = getvfsbyname("mfs", &vfc);
652 		if (error && vfsisloadable("mfs")) {
653 			if (vfsload("mfs"))
654 				fatal("vfsload(mfs)");
655 			endvfsent();	/* flush cache */
656 			error = getvfsbyname("mfs", &vfc);
657 		}
658 		if (error)
659 			fatal("mfs filesystem not available");
660 
661 		if (mount(vfc.vfc_name, argv[1], mntflags, &args) < 0)
662 			fatal("%s: %s", argv[1], strerror(errno));
663 		if(filename) {
664 			munmap(membase,fssize * sectorsize);
665 		}
666 	}
667 #endif
668 	exit(0);
669 }
670 
671 #ifdef COMPAT
672 char lmsg[] = "%s: can't read disk label; disk type must be specified";
673 #else
674 char lmsg[] = "%s: can't read disk label";
675 #endif
676 
677 struct disklabel *
678 getdisklabel(s, fd)
679 	char *s;
680 	int fd;
681 {
682 	static struct disklabel lab;
683 
684 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
685 #ifdef COMPAT
686 		if (disktype) {
687 			struct disklabel *lp, *getdiskbyname();
688 
689 			unlabeled++;
690 			lp = getdiskbyname(disktype);
691 			if (lp == NULL)
692 				fatal("%s: unknown disk type", disktype);
693 			return (lp);
694 		}
695 #endif
696 		warn("ioctl (GDINFO)");
697 		fatal(lmsg, s);
698 	}
699 	return (&lab);
700 }
701 
702 rewritelabel(s, fd, lp)
703 	char *s;
704 	int fd;
705 	register struct disklabel *lp;
706 {
707 #ifdef COMPAT
708 	if (unlabeled)
709 		return;
710 #endif
711 	lp->d_checksum = 0;
712 	lp->d_checksum = dkcksum(lp);
713 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
714 		warn("ioctl (WDINFO)");
715 		fatal("%s: can't rewrite disk label", s);
716 	}
717 }
718 
719 /*VARARGS*/
720 void
721 #if __STDC__
722 fatal(const char *fmt, ...)
723 #else
724 fatal(fmt, va_alist)
725 	char *fmt;
726 	va_dcl
727 #endif
728 {
729 	va_list ap;
730 
731 #if __STDC__
732 	va_start(ap, fmt);
733 #else
734 	va_start(ap);
735 #endif
736 	if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
737 		openlog(progname, LOG_CONS, LOG_DAEMON);
738 		vsyslog(LOG_ERR, fmt, ap);
739 		closelog();
740 	} else {
741 		vwarnx(fmt, ap);
742 	}
743 	va_end(ap);
744 	exit(1);
745 	/*NOTREACHED*/
746 }
747 
748 static void
749 usage()
750 {
751 	if (mfs) {
752 		fprintf(stderr,
753 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
754 			progname);
755 	} else
756 		fprintf(stderr,
757 		    "usage: %s [ -fsoptions ] special-device%s\n",
758 		    progname,
759 #ifdef COMPAT
760 		    " [device-type]");
761 #else
762 		    "");
763 #endif
764 	fprintf(stderr, "where fsoptions are:\n");
765 	fprintf(stderr,
766 	    "\t-N do not create file system, just print out parameters\n");
767 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
768 	fprintf(stderr, "\t-S sector size\n");
769 #ifdef COMPAT
770 	fprintf(stderr, "\t-T disktype\n");
771 #endif
772 	fprintf(stderr, "\t-U enable soft updates\n");
773 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
774 	fprintf(stderr, "\t-b block size\n");
775 	fprintf(stderr, "\t-c cylinders/group\n");
776 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
777 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
778 	fprintf(stderr, "\t-f frag size\n");
779 	fprintf(stderr, "\t-g average file size\n");
780 	fprintf(stderr, "\t-h average files per directory\n");
781 	fprintf(stderr, "\t-i number of bytes per inode\n");
782 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
783 	fprintf(stderr, "\t-l hardware sector interleave\n");
784 	fprintf(stderr, "\t-m minimum free space %%\n");
785 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
786 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
787 	fprintf(stderr, "\t-p spare sectors per track\n");
788 	fprintf(stderr, "\t-s file system size (sectors)\n");
789 	fprintf(stderr, "\t-r revolutions/minute\n");
790 	fprintf(stderr, "\t-t tracks/cylinder\n");
791 	fprintf(stderr, "\t-u sectors/track\n");
792 	fprintf(stderr,
793         "\t-v do not attempt to determine partition name from device name\n");
794 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
795 	exit(1);
796 }
797