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