xref: /freebsd/sbin/newfs/newfs.c (revision ee41f1b1cf5e3d4f586cb85b46123b416275862c)
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 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 
170 int	mfs;			/* run as the memory based filesystem */
171 char	*mfs_mtpt;		/* mount point for mfs		*/
172 struct stat mfs_mtstat;		/* stat prior to mount		*/
173 int	Nflag;			/* run without writing file system */
174 int	Oflag;			/* format as an 4.3BSD 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	bbsize = BBSIZE;	/* boot block size */
201 int	sbsize = SBSIZE;	/* superblock size */
202 int	mntflags = MNT_ASYNC;	/* flags to be passed to mount */
203 int	t_or_u_flag = 0;	/* user has specified -t or -u */
204 u_long	memleft;		/* virtual memory available */
205 caddr_t	membase;		/* start address of memory based filesystem */
206 char	*filename;
207 #ifdef COMPAT
208 char	*disktype;
209 int	unlabeled;
210 #endif
211 
212 char	device[MAXPATHLEN];
213 char	*progname;
214 
215 extern void mkfs __P((struct partition *, char *, int, int));
216 static void usage __P((void));
217 
218 int
219 main(argc, argv)
220 	int argc;
221 	char *argv[];
222 {
223 	register int ch;
224 	register struct partition *pp;
225 	register struct disklabel *lp;
226 	struct disklabel mfsfakelabel;
227 	struct disklabel *getdisklabel();
228 	struct partition oldpartition;
229 	struct stat st;
230 	struct statfs *mp;
231 	int fsi, fso, len, n, vflag;
232 	char *cp, *s1, *s2, *special, *opstring;
233 #ifdef MFS
234 	struct vfsconf vfc;
235 	int error;
236 	char buf[BUFSIZ];
237 #endif
238 
239 	vflag = 0;
240 	if ((progname = strrchr(*argv, '/')))
241 		++progname;
242 	else
243 		progname = *argv;
244 
245 	if (strstr(progname, "mfs")) {
246 		mfs = 1;
247 		Nflag++;
248 		fprintf(stderr,
249 "WARNING: MFS is being phased out in preference for md devices\n"
250 "WARNING: Please see mdconfig(8) for details\n"
251 "WARNING: Continuing in 15 seconds\n");
252 		sleep(15);
253 
254 	}
255 
256 	opstring = mfs ?
257 	    "NF:T:a:b:c:d:e:f:i:m:o:s:" :
258 	    "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:vx:";
259 	while ((ch = getopt(argc, argv, opstring)) != -1)
260 		switch (ch) {
261 		case 'N':
262 			Nflag = 1;
263 			break;
264 		case 'O':
265 			Oflag = 1;
266 			break;
267 		case 'S':
268 			if ((sectorsize = atoi(optarg)) <= 0)
269 				fatal("%s: bad sector size", optarg);
270 			break;
271 #ifdef COMPAT
272 		case 'T':
273 			disktype = optarg;
274 			break;
275 #endif
276 		case 'F':
277 			filename = optarg;
278 			break;
279 		case 'a':
280 			if ((maxcontig = atoi(optarg)) <= 0)
281 				fatal("%s: bad maximum contiguous blocks",
282 				    optarg);
283 			break;
284 		case 'b':
285 			if ((bsize = atoi(optarg)) < MINBSIZE)
286 				fatal("%s: bad block size", optarg);
287 			break;
288 		case 'c':
289 			if ((cpg = atoi(optarg)) <= 0)
290 				fatal("%s: bad cylinders/group", optarg);
291 			cpgflg++;
292 			break;
293 		case 'd':
294 			if ((rotdelay = atoi(optarg)) < 0)
295 				fatal("%s: bad rotational delay", optarg);
296 			break;
297 		case 'e':
298 			if ((maxbpg = atoi(optarg)) <= 0)
299 		fatal("%s: bad blocks per file in a cylinder group",
300 				    optarg);
301 			break;
302 		case 'f':
303 			if ((fsize = atoi(optarg)) <= 0)
304 				fatal("%s: bad fragment size", optarg);
305 			break;
306 		case 'i':
307 			if ((density = atoi(optarg)) <= 0)
308 				fatal("%s: bad bytes per inode", optarg);
309 			break;
310 		case 'k':
311 			if ((trackskew = atoi(optarg)) < 0)
312 				fatal("%s: bad track skew", optarg);
313 			break;
314 		case 'l':
315 			if ((interleave = atoi(optarg)) <= 0)
316 				fatal("%s: bad interleave", optarg);
317 			break;
318 		case 'm':
319 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
320 				fatal("%s: bad free space %%", optarg);
321 			break;
322 		case 'n':
323 			if ((nrpos = atoi(optarg)) < 0)
324 				fatal("%s: bad rotational layout count",
325 				    optarg);
326 			if (nrpos == 0)
327 				nrpos = 1;
328 			break;
329 		case 'o':
330 			if (mfs)
331 				getmntopts(optarg, mopts, &mntflags, 0);
332 			else {
333 				if (strcmp(optarg, "space") == 0)
334 					opt = FS_OPTSPACE;
335 				else if (strcmp(optarg, "time") == 0)
336 					opt = FS_OPTTIME;
337 				else
338 	fatal("%s: unknown optimization preference: use `space' or `time'");
339 			}
340 			break;
341 		case 'p':
342 			if ((trackspares = atoi(optarg)) < 0)
343 				fatal("%s: bad spare sectors per track",
344 				    optarg);
345 			break;
346 		case 'r':
347 			if ((rpm = atoi(optarg)) <= 0)
348 				fatal("%s: bad revolutions/minute", optarg);
349 			break;
350 		case 's':
351 			if ((fssize = atoi(optarg)) <= 0)
352 				fatal("%s: bad file system size", optarg);
353 			break;
354 		case 't':
355 			t_or_u_flag++;
356 			if ((ntracks = atoi(optarg)) < 0)
357 				fatal("%s: bad total tracks", optarg);
358 			break;
359 		case 'u':
360 			t_or_u_flag++;
361 			if ((nsectors = atoi(optarg)) < 0)
362 				fatal("%s: bad sectors/track", optarg);
363 			break;
364 		case 'v':
365 			vflag = 1;
366 			break;
367 		case 'x':
368 			if ((cylspares = atoi(optarg)) < 0)
369 				fatal("%s: bad spare sectors per cylinder",
370 				    optarg);
371 			break;
372 		case '?':
373 		default:
374 			usage();
375 		}
376 	argc -= optind;
377 	argv += optind;
378 
379 	if (argc != 2 && (mfs || argc != 1))
380 		usage();
381 
382 	special = argv[0];
383 	/* Copy the NetBSD way of faking up a disk label */
384         if (mfs && !strcmp(special, "swap")) {
385                 /*
386                  * it's an MFS, mounted on "swap."  fake up a label.
387                  * XXX XXX XXX
388                  */
389                 fso = -1;       /* XXX; normally done below. */
390 
391                 memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
392                 mfsfakelabel.d_secsize = 512;
393                 mfsfakelabel.d_nsectors = 64;
394                 mfsfakelabel.d_ntracks = 16;
395                 mfsfakelabel.d_ncylinders = 16;
396                 mfsfakelabel.d_secpercyl = 1024;
397                 mfsfakelabel.d_secperunit = 16384;
398                 mfsfakelabel.d_rpm = 3600;
399                 mfsfakelabel.d_interleave = 1;
400                 mfsfakelabel.d_npartitions = 1;
401                 mfsfakelabel.d_partitions[0].p_size = 16384;
402                 mfsfakelabel.d_partitions[0].p_fsize = 1024;
403                 mfsfakelabel.d_partitions[0].p_frag = 8;
404                 mfsfakelabel.d_partitions[0].p_cpg = 16;
405 
406                 lp = &mfsfakelabel;
407                 pp = &mfsfakelabel.d_partitions[0];
408 
409                 goto havelabel;
410         }
411 
412 	cp = strrchr(special, '/');
413 	if (cp == 0) {
414 		/*
415 		 * No path prefix; try /dev/%s.
416 		 */
417 		(void)sprintf(device, "%s%s", _PATH_DEV, special);
418 		special = device;
419 	}
420 	if (Nflag) {
421 		fso = -1;
422 	} else {
423 		fso = open(special, O_WRONLY);
424 		if (fso < 0)
425 			fatal("%s: %s", special, strerror(errno));
426 
427 		/* Bail if target special is mounted */
428 		n = getmntinfo(&mp, MNT_NOWAIT);
429 		if (n == 0)
430 			fatal("%s: getmntinfo: %s", special, strerror(errno));
431 
432 		len = sizeof(_PATH_DEV) - 1;
433 		s1 = special;
434 		if (strncmp(_PATH_DEV, s1, len) == 0)
435 			s1 += len;
436 
437 		while (--n >= 0) {
438 			s2 = mp->f_mntfromname;
439 			if (strncmp(_PATH_DEV, s2, len) == 0) {
440 				s2 += len - 1;
441 				*s2 = 'r';
442 			}
443 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
444 				fatal("%s is mounted on %s",
445 				    special, mp->f_mntonname);
446 			++mp;
447 		}
448 	}
449 	if (mfs && disktype != NULL) {
450 		lp = (struct disklabel *)getdiskbyname(disktype);
451 		if (lp == NULL)
452 			fatal("%s: unknown disk type", disktype);
453 		pp = &lp->d_partitions[1];
454 	} else {
455 		fsi = open(special, O_RDONLY);
456 		if (fsi < 0)
457 			fatal("%s: %s", special, strerror(errno));
458 		if (fstat(fsi, &st) < 0)
459 			fatal("%s: %s", special, strerror(errno));
460 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
461 			printf("%s: %s: not a character-special device\n",
462 			    progname, special);
463  		cp = strchr(argv[0], '\0');
464  		if (cp == argv[0])
465  			fatal("null special file name");
466  		cp--;
467  		if (!vflag && (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
468 			fatal("%s: can't figure out file system partition",
469 			    argv[0]);
470 #ifdef COMPAT
471 		if (!mfs && disktype == NULL)
472 			disktype = argv[1];
473 #endif
474 		lp = getdisklabel(special, fsi);
475 		if (vflag || isdigit(*cp))
476 			pp = &lp->d_partitions[0];
477 		else
478 			pp = &lp->d_partitions[*cp - 'a'];
479 		if (pp->p_size == 0)
480 			fatal("%s: `%c' partition is unavailable",
481 			    argv[0], *cp);
482 		if (pp->p_fstype == FS_BOOT)
483 			fatal("%s: `%c' partition overlaps boot program",
484 			      argv[0], *cp);
485 	}
486 havelabel:
487 	if (fssize == 0)
488 		fssize = pp->p_size;
489 	if (fssize > pp->p_size && !mfs)
490 	       fatal("%s: maximum file system size on the `%c' partition is %d",
491 			argv[0], *cp, pp->p_size);
492 	if (rpm == 0) {
493 		rpm = lp->d_rpm;
494 		if (rpm <= 0)
495 			rpm = 3600;
496 	}
497 	if (ntracks == 0) {
498 		ntracks = lp->d_ntracks;
499 		if (ntracks <= 0)
500 			fatal("%s: no default #tracks", argv[0]);
501 	}
502 	if (nsectors == 0) {
503 		nsectors = lp->d_nsectors;
504 		if (nsectors <= 0)
505 			fatal("%s: no default #sectors/track", argv[0]);
506 	}
507 	if (sectorsize == 0) {
508 		sectorsize = lp->d_secsize;
509 		if (sectorsize <= 0)
510 			fatal("%s: no default sector size", argv[0]);
511 	}
512 	if (trackskew == -1) {
513 		trackskew = lp->d_trackskew;
514 		if (trackskew < 0)
515 			trackskew = 0;
516 	}
517 	if (interleave == 0) {
518 		interleave = lp->d_interleave;
519 		if (interleave <= 0)
520 			interleave = 1;
521 	}
522 	if (fsize == 0) {
523 		fsize = pp->p_fsize;
524 		if (fsize <= 0)
525 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
526 	}
527 	if (bsize == 0) {
528 		bsize = pp->p_frag * pp->p_fsize;
529 		if (bsize <= 0)
530 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
531 	}
532 	/*
533 	 * Maxcontig sets the default for the maximum number of blocks
534 	 * that may be allocated sequentially. With filesystem clustering
535 	 * it is possible to allocate contiguous blocks up to the maximum
536 	 * transfer size permitted by the controller or buffering.
537 	 */
538 	if (maxcontig == 0)
539 		maxcontig = MAX(1, MAXPHYS / bsize - 1);
540 	if (density == 0)
541 		density = NFPI * fsize;
542 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
543 		fprintf(stderr, "Warning: changing optimization to space ");
544 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
545 		opt = FS_OPTSPACE;
546 	}
547 	if (trackspares == -1) {
548 		trackspares = lp->d_sparespertrack;
549 		if (trackspares < 0)
550 			trackspares = 0;
551 	}
552 	nphyssectors = nsectors + trackspares;
553 	if (cylspares == -1) {
554 		cylspares = lp->d_sparespercyl;
555 		if (cylspares < 0)
556 			cylspares = 0;
557 	}
558 	secpercyl = nsectors * ntracks - cylspares;
559 	/*
560 	 * Only complain if -t or -u have been specified; the default
561 	 * case (4096 sectors per cylinder) is intended to disagree
562 	 * with the disklabel.
563 	 */
564 	if (t_or_u_flag && secpercyl != lp->d_secpercyl)
565 		fprintf(stderr, "%s (%d) %s (%lu)\n",
566 			"Warning: calculated sectors per cylinder", secpercyl,
567 			"disagrees with disk label", (u_long)lp->d_secpercyl);
568 	if (maxbpg == 0)
569 		maxbpg = MAXBLKPG(bsize);
570 	headswitch = lp->d_headswitch;
571 	trackseek = lp->d_trkseek;
572 #ifdef notdef /* label may be 0 if faked up by kernel */
573 	bbsize = lp->d_bbsize;
574 	sbsize = lp->d_sbsize;
575 #endif
576 	oldpartition = *pp;
577 #ifdef tahoe
578 	realsectorsize = sectorsize;
579 	if (sectorsize != DEV_BSIZE) {		/* XXX */
580 		int secperblk = DEV_BSIZE / sectorsize;
581 
582 		sectorsize = DEV_BSIZE;
583 		nsectors /= secperblk;
584 		nphyssectors /= secperblk;
585 		secpercyl /= secperblk;
586 		fssize /= secperblk;
587 		pp->p_size /= secperblk;
588 	}
589 #else
590 	realsectorsize = sectorsize;
591 	if (sectorsize != DEV_BSIZE) {		/* XXX */
592 		int secperblk = sectorsize / DEV_BSIZE;
593 
594 		sectorsize = DEV_BSIZE;
595 		nsectors *= secperblk;
596 		nphyssectors *= secperblk;
597 		secpercyl *= secperblk;
598 		fssize *= secperblk;
599 		pp->p_size *= secperblk;
600 	}
601 #endif
602 	if (mfs) {
603 		mfs_mtpt = argv[1];
604 		if (
605 		    stat(mfs_mtpt, &mfs_mtstat) < 0 ||
606 		    !S_ISDIR(mfs_mtstat.st_mode)
607 		) {
608 			fatal("mount point not dir: %s", mfs_mtpt);
609 		}
610 	}
611 	mkfs(pp, special, fsi, fso);
612 #ifdef tahoe
613 	if (realsectorsize != DEV_BSIZE)
614 		pp->p_size *= DEV_BSIZE / realsectorsize;
615 #else
616 	if (realsectorsize != DEV_BSIZE)
617 		pp->p_size /= realsectorsize /DEV_BSIZE;
618 #endif
619 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
620 		rewritelabel(special, fso, lp);
621 	if (!Nflag)
622 		close(fso);
623 	close(fsi);
624 #ifdef MFS
625 	if (mfs) {
626 		struct mfs_args args;
627 
628 		sprintf(buf, "mfs:%d", getpid());
629 		args.fspec = buf;
630 		args.export.ex_root = -2;
631 		if (mntflags & MNT_RDONLY)
632 			args.export.ex_flags = MNT_EXRDONLY;
633 		else
634 			args.export.ex_flags = 0;
635 		args.base = membase;
636 		args.size = fssize * sectorsize;
637 
638 		error = getvfsbyname("mfs", &vfc);
639 		if (error && vfsisloadable("mfs")) {
640 			if (vfsload("mfs"))
641 				fatal("vfsload(mfs)");
642 			endvfsent();	/* flush cache */
643 			error = getvfsbyname("mfs", &vfc);
644 		}
645 		if (error)
646 			fatal("mfs filesystem not available");
647 
648 		if (mount(vfc.vfc_name, argv[1], mntflags, &args) < 0)
649 			fatal("%s: %s", argv[1], strerror(errno));
650 		if(filename) {
651 			munmap(membase,fssize * sectorsize);
652 		}
653 	}
654 #endif
655 	exit(0);
656 }
657 
658 #ifdef COMPAT
659 char lmsg[] = "%s: can't read disk label; disk type must be specified";
660 #else
661 char lmsg[] = "%s: can't read disk label";
662 #endif
663 
664 struct disklabel *
665 getdisklabel(s, fd)
666 	char *s;
667 	int fd;
668 {
669 	static struct disklabel lab;
670 
671 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
672 #ifdef COMPAT
673 		if (disktype) {
674 			struct disklabel *lp, *getdiskbyname();
675 
676 			unlabeled++;
677 			lp = getdiskbyname(disktype);
678 			if (lp == NULL)
679 				fatal("%s: unknown disk type", disktype);
680 			return (lp);
681 		}
682 #endif
683 		warn("ioctl (GDINFO)");
684 		fatal(lmsg, s);
685 	}
686 	return (&lab);
687 }
688 
689 rewritelabel(s, fd, lp)
690 	char *s;
691 	int fd;
692 	register struct disklabel *lp;
693 {
694 #ifdef COMPAT
695 	if (unlabeled)
696 		return;
697 #endif
698 	lp->d_checksum = 0;
699 	lp->d_checksum = dkcksum(lp);
700 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
701 		warn("ioctl (WDINFO)");
702 		fatal("%s: can't rewrite disk label", s);
703 	}
704 }
705 
706 /*VARARGS*/
707 void
708 #if __STDC__
709 fatal(const char *fmt, ...)
710 #else
711 fatal(fmt, va_alist)
712 	char *fmt;
713 	va_dcl
714 #endif
715 {
716 	va_list ap;
717 
718 #if __STDC__
719 	va_start(ap, fmt);
720 #else
721 	va_start(ap);
722 #endif
723 	if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
724 		openlog(progname, LOG_CONS, LOG_DAEMON);
725 		vsyslog(LOG_ERR, fmt, ap);
726 		closelog();
727 	} else {
728 		vwarnx(fmt, ap);
729 	}
730 	va_end(ap);
731 	exit(1);
732 	/*NOTREACHED*/
733 }
734 
735 static void
736 usage()
737 {
738 	if (mfs) {
739 		fprintf(stderr,
740 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
741 			progname);
742 	} else
743 		fprintf(stderr,
744 		    "usage: %s [ -fsoptions ] special-device%s\n",
745 		    progname,
746 #ifdef COMPAT
747 		    " [device-type]");
748 #else
749 		    "");
750 #endif
751 	fprintf(stderr, "where fsoptions are:\n");
752 	fprintf(stderr,
753 	    "\t-N do not create file system, just print out parameters\n");
754 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
755 	fprintf(stderr, "\t-S sector size\n");
756 #ifdef COMPAT
757 	fprintf(stderr, "\t-T disktype\n");
758 #endif
759 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
760 	fprintf(stderr, "\t-b block size\n");
761 	fprintf(stderr, "\t-c cylinders/group\n");
762 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
763 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
764 	fprintf(stderr, "\t-f frag size\n");
765 	fprintf(stderr, "\t-i number of bytes per inode\n");
766 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
767 	fprintf(stderr, "\t-l hardware sector interleave\n");
768 	fprintf(stderr, "\t-m minimum free space %%\n");
769 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
770 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
771 	fprintf(stderr, "\t-p spare sectors per track\n");
772 	fprintf(stderr, "\t-s file system size (sectors)\n");
773 	fprintf(stderr, "\t-r revolutions/minute\n");
774 	fprintf(stderr, "\t-t tracks/cylinder\n");
775 	fprintf(stderr, "\t-u sectors/track\n");
776 	fprintf(stderr,
777         "\t-v do not attempt to determine partition name from device name\n");
778 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
779 	exit(1);
780 }
781