xref: /dragonfly/sbin/newfs/newfs.c (revision 59a92d18)
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  * @(#) Copyright (c) 1983, 1989, 1993, 1994 The Regents of the University of California.  All rights reserved.
34  * @(#)newfs.c	8.13 (Berkeley) 5/1/95
35  * $FreeBSD: src/sbin/newfs/newfs.c,v 1.30.2.9 2003/05/13 12:03:55 joerg Exp $
36  * $DragonFly: src/sbin/newfs/newfs.c,v 1.17 2007/06/19 19:18:20 dillon Exp $
37  */
38 
39 /*
40  * newfs: friendly front end to mkfs
41  */
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/diskslice.h>
45 #include <sys/file.h>
46 #include <sys/mount.h>
47 
48 #include <vfs/ufs/dir.h>
49 #include <vfs/ufs/dinode.h>
50 #include <vfs/ufs/fs.h>
51 #include <vfs/ufs/ufsmount.h>
52 
53 #include <ctype.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <paths.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <syslog.h>
61 #include <unistd.h>
62 #include <disktab.h>
63 
64 #ifdef MFS
65 #include <sys/types.h>
66 #include <sys/mman.h>
67 #endif
68 
69 #if __STDC__
70 #include <stdarg.h>
71 #else
72 #include <varargs.h>
73 #endif
74 
75 #include "mntopts.h"
76 #include "defs.h"
77 
78 struct mntopt mopts[] = {
79 	MOPT_STDOPTS,
80 	MOPT_ASYNC,
81 	MOPT_NULL,
82 };
83 
84 void	fatal(const char *fmt, ...);
85 
86 #define	COMPAT			/* allow non-labeled disks */
87 
88 /*
89  * The following two constants set the default block and fragment sizes.
90  * Both constants must be a power of 2 and meet the following constraints:
91  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
92  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
93  *	DESBLKSIZE / DESFRAGSIZE <= 8
94  */
95 #define	DFL_FRAGSIZE	2048
96 #define	DFL_BLKSIZE	16384
97 
98 /*
99  * Cylinder groups may have up to many cylinders. The actual
100  * number used depends upon how much information can be stored
101  * on a single cylinder. The default is to use as many as possible
102  * cylinders per group.
103  */
104 #define	DESCPG		65536	/* desired fs_cpg ("infinity") */
105 
106 /*
107  * Once upon a time...
108  *    ROTDELAY gives the minimum number of milliseconds to initiate
109  *    another disk transfer on the same cylinder. It is used in
110  *    determining the rotationally optimal layout for disk blocks
111  *    within a file; the default of fs_rotdelay is 4ms.
112  *
113  * ...but now we make this 0 to disable the rotdelay delay because
114  * modern drives with read/write-behind achieve higher performance
115  * without the delay.
116  */
117 #define ROTDELAY	0
118 
119 /*
120  * MAXBLKPG determines the maximum number of data blocks which are
121  * placed in a single cylinder group. The default is one indirect
122  * block worth of data blocks.
123  */
124 #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
125 
126 /*
127  * Each file system has a number of inodes statically allocated.
128  * We allocate one inode slot per NFPI fragments, expecting this
129  * to be far more than we will ever need.
130  */
131 #define	NFPI		4
132 
133 /*
134  * Once upon a time...
135  *    For each cylinder we keep track of the availability of blocks at different
136  *    rotational positions, so that we can lay out the data to be picked
137  *    up with minimum rotational latency.  NRPOS is the default number of
138  *    rotational positions that we distinguish.  With NRPOS of 8 the resolution
139  *    of our summary information is 2ms for a typical 3600 rpm drive.
140  *
141  * ...but now we make this 1 (which essentially disables the rotational
142  * position table because modern drives with read-ahead and write-behind do
143  * better without the rotational position table.
144  */
145 #define	NRPOS		1	/* number distinct rotational positions */
146 
147 /*
148  * About the same time as the above, we knew what went where on the disks.
149  * no longer so, so kill the code which finds the different platters too...
150  * We do this by saying one head, with a lot of sectors on it.
151  * The number of sectors are used to determine the size of a cyl-group.
152  * Kirk suggested one or two meg per "cylinder" so we say two.
153  */
154 #define NTRACKS		1	/* number of heads */
155 #define NSECTORS	4096	/* number of sectors */
156 
157 int	mfs;			/* run as the memory based filesystem */
158 char	*mfs_mtpt;		/* mount point for mfs		*/
159 struct stat mfs_mtstat;		/* stat prior to mount		*/
160 int	Lflag;			/* add a volume label */
161 int	Nflag;			/* run without writing file system */
162 int	Oflag;			/* format as an 4.3BSD file system */
163 int	Cflag;			/* copy underlying filesystem (mfs only) */
164 int	Uflag;			/* enable soft updates for file system */
165 u_long	fssize;			/* file system size */
166 int	ntracks = NTRACKS;	/* # tracks/cylinder */
167 int	nsectors = NSECTORS;	/* # sectors/track */
168 int	nphyssectors;		/* # sectors/track including spares */
169 int	secpercyl;		/* sectors per cylinder */
170 int	trackspares = -1;	/* spare sectors per track */
171 int	cylspares = -1;		/* spare sectors per cylinder */
172 int	sectorsize;		/* bytes/sector */
173 int	realsectorsize;		/* bytes/sector in hardware */
174 int	rpm;			/* revolutions/minute of drive */
175 int	interleave;		/* hardware sector interleave */
176 int	trackskew = -1;		/* sector 0 skew, per track */
177 int	headswitch;		/* head switch time, usec */
178 int	trackseek;		/* track-to-track seek, usec */
179 int	fsize = 0;		/* fragment size */
180 int	bsize = 0;		/* block size */
181 int	cpg = DESCPG;		/* cylinders/cylinder group */
182 int	cpgflg;			/* cylinders/cylinder group flag was given */
183 int	minfree = MINFREE;	/* free space threshold */
184 int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
185 int	density;		/* number of bytes per inode */
186 int	maxcontig = 0;		/* max contiguous blocks to allocate */
187 int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
188 int	maxbpg;			/* maximum blocks per file in a cyl group */
189 int	nrpos = NRPOS;		/* # of distinguished rotational positions */
190 int	avgfilesize = AVFILESIZ;/* expected average file size */
191 int	avgfilesperdir = AFPDIR;/* expected number of files per directory */
192 int	bbsize = BBSIZE;	/* boot block size */
193 int	sbsize = SBSIZE;	/* superblock size */
194 int	mntflags = MNT_ASYNC;	/* flags to be passed to mount */
195 int	t_or_u_flag = 0;	/* user has specified -t or -u */
196 caddr_t	membase;		/* start address of memory based filesystem */
197 char	*filename;
198 u_char	*volumelabel = NULL;	/* volume label for filesystem */
199 #ifdef COMPAT
200 char	*disktype;
201 int	unlabeled;
202 #endif
203 
204 char	mfsdevname[256];
205 char	*progname;
206 
207 static void usage(void);
208 static void mfsintr(int signo);
209 
210 int
211 main(int argc, char **argv)
212 {
213 	int ch, i;
214 	struct disktab geom;		/* disk geometry data */
215 	struct stat st;
216 	struct statfs *mp;
217 	int fsi = -1, fso = -1, len, n, vflag;
218 	char *s1, *s2, *special;
219 	const char *opstring;
220 #ifdef MFS
221 	struct vfsconf vfc;
222 	int error;
223 #endif
224 
225 	bzero(&geom, sizeof(geom));
226 	vflag = 0;
227 	if ((progname = strrchr(*argv, '/')))
228 		++progname;
229 	else
230 		progname = *argv;
231 
232 	if (strstr(progname, "mfs")) {
233 		mfs = 1;
234 		Nflag++;
235 	}
236 
237 	opstring = mfs ?
238 	    "L:NCF:T:Ua:b:c:d:e:f:g:h:i:m:o:s:v" :
239 	    "L:NOS:T:Ua:b:c:d:e:f:g:h:i:k:l:m:n:o:p:r:s:t:u:vx:";
240 	while ((ch = getopt(argc, argv, opstring)) != -1) {
241 		switch (ch) {
242 		case 'L':
243 			volumelabel = optarg;
244 			i = -1;
245 			while (isalnum(volumelabel[++i]))
246 				;
247 			if (volumelabel[i] != '\0')
248 				errx(1, "bad volume label. Valid characters are alphanumerics.");
249 			if (strlen(volumelabel) >= MAXVOLLEN)
250 				errx(1, "bad volume label. Length is longer than %d.",
251 				    MAXVOLLEN);
252 			Lflag = 1;
253 			break;
254 		case 'N':
255 			Nflag = 1;
256 			break;
257 		case 'O':
258 			Oflag = 1;
259 			break;
260 		case 'C':
261 			Cflag = 1;	/* MFS only */
262 			break;
263 		case 'S':
264 			if ((sectorsize = atoi(optarg)) <= 0)
265 				fatal("%s: bad sector size", optarg);
266 			break;
267 #ifdef COMPAT
268 		case 'T':
269 			disktype = optarg;
270 			break;
271 #endif
272 		case 'F':
273 			filename = optarg;
274 			break;
275 		case 'U':
276 			Uflag = 1;
277 			break;
278 		case 'a':
279 			if ((maxcontig = atoi(optarg)) <= 0)
280 				fatal("%s: bad maximum contiguous blocks",
281 				    optarg);
282 			break;
283 		case 'b':
284 			if ((bsize = atoi(optarg)) < MINBSIZE)
285 				fatal("%s: bad block size", optarg);
286 			break;
287 		case 'c':
288 			if ((cpg = atoi(optarg)) <= 0)
289 				fatal("%s: bad cylinders/group", optarg);
290 			cpgflg++;
291 			break;
292 		case 'd':
293 			if ((rotdelay = atoi(optarg)) < 0)
294 				fatal("%s: bad rotational delay", optarg);
295 			break;
296 		case 'e':
297 			if ((maxbpg = atoi(optarg)) <= 0)
298 		fatal("%s: bad blocks per file in a cylinder group",
299 				    optarg);
300 			break;
301 		case 'f':
302 			if ((fsize = atoi(optarg)) <= 0)
303 				fatal("%s: bad fragment size", optarg);
304 			break;
305 		case 'g':
306 			if ((avgfilesize = atoi(optarg)) <= 0)
307 				fatal("%s: bad average file size", optarg);
308 			break;
309 		case 'h':
310 			if ((avgfilesperdir = atoi(optarg)) <= 0)
311 				fatal("%s: bad average files per dir", optarg);
312 			break;
313 		case 'i':
314 			if ((density = atoi(optarg)) <= 0)
315 				fatal("%s: bad bytes per inode", optarg);
316 			break;
317 		case 'k':
318 			if ((trackskew = atoi(optarg)) < 0)
319 				fatal("%s: bad track skew", optarg);
320 			break;
321 		case 'l':
322 			if ((interleave = atoi(optarg)) <= 0)
323 				fatal("%s: bad interleave", optarg);
324 			break;
325 		case 'm':
326 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
327 				fatal("%s: bad free space %%", optarg);
328 			break;
329 		case 'n':
330 			if ((nrpos = atoi(optarg)) < 0)
331 				fatal("%s: bad rotational layout count",
332 				    optarg);
333 			if (nrpos == 0)
334 				nrpos = 1;
335 			break;
336 		case 'o':
337 			if (mfs)
338 				getmntopts(optarg, mopts, &mntflags, 0);
339 			else {
340 				if (strcmp(optarg, "space") == 0)
341 					opt = FS_OPTSPACE;
342 				else if (strcmp(optarg, "time") == 0)
343 					opt = FS_OPTTIME;
344 				else
345 	fatal("%s: unknown optimization preference: use `space' or `time'", optarg);
346 			}
347 			break;
348 		case 'p':
349 			if ((trackspares = atoi(optarg)) < 0)
350 				fatal("%s: bad spare sectors per track",
351 				    optarg);
352 			break;
353 		case 'r':
354 			if ((rpm = atoi(optarg)) <= 0)
355 				fatal("%s: bad revolutions/minute", optarg);
356 			break;
357 		case 's':
358 			/*
359 			 * Unsigned long but limit to long.  On 32 bit a
360 			 * tad under 2G, on 64 bit the upper bound is more
361 			 * swap space then operand size.
362 			 *
363 			 * NOTE: fssize is converted from 512 byte sectors
364 			 * to filesystem block-sized sectors by mkfs XXX.
365 			 */
366 			fssize = strtoul(optarg, NULL, 10);
367 			if (fssize == 0 || fssize > LONG_MAX)
368 				fatal("%s: bad file system size", optarg);
369 			break;
370 		case 't':
371 			t_or_u_flag++;
372 			if ((ntracks = atoi(optarg)) < 0)
373 				fatal("%s: bad total tracks", optarg);
374 			break;
375 		case 'u':
376 			t_or_u_flag++;
377 			if ((nsectors = atoi(optarg)) < 0)
378 				fatal("%s: bad sectors/track", optarg);
379 			break;
380 		case 'v':
381 			vflag = 1;
382 			break;
383 		case 'x':
384 			if ((cylspares = atoi(optarg)) < 0)
385 				fatal("%s: bad spare sectors per cylinder",
386 				    optarg);
387 			break;
388 		case '?':
389 		default:
390 			usage();
391 		}
392 	}
393 	argc -= optind;
394 	argv += optind;
395 
396 	if (argc != 2 && (mfs || argc != 1))
397 		usage();
398 
399 	special = argv[0];
400 	/* Copy the NetBSD way of faking up a disk label */
401         if (mfs && !strcmp(special, "swap")) {
402                 /*
403                  * it's an MFS, mounted on "swap."  fake up a label.
404                  * XXX XXX XXX
405                  */
406                 fso = -1;       /* XXX; normally done below. */
407 
408 		geom.d_media_blksize = 512;
409 		geom.d_nheads = 16;
410 		geom.d_secpertrack = 64;
411 		/* geom.d_ncylinders not used */
412 		geom.d_secpercyl = 1024;
413 		geom.d_media_blocks = 16384;
414                 geom.d_rpm = 3600;
415                 geom.d_interleave = 1;
416 
417                 goto havelabel;
418         }
419 
420 	/*
421 	 * If we can't stat the device and the path is relative, try
422 	 * prepending /dev.
423 	 */
424 	if (stat(special, &st) < 0 && special[0] && special[0] != '/')
425 		asprintf(&special, "/dev/%s", special);
426 
427 	if (Nflag) {
428 		fso = -1;
429 	} else {
430 		fso = open(special, O_WRONLY);
431 		if (fso < 0)
432 			fatal("%s: %s", special, strerror(errno));
433 
434 		/* Bail if target special is mounted */
435 		n = getmntinfo(&mp, MNT_NOWAIT);
436 		if (n == 0)
437 			fatal("%s: getmntinfo: %s", special, strerror(errno));
438 
439 		len = sizeof(_PATH_DEV) - 1;
440 		s1 = special;
441 		if (strncmp(_PATH_DEV, s1, len) == 0)
442 			s1 += len;
443 
444 		while (--n >= 0) {
445 			s2 = mp->f_mntfromname;
446 			if (strncmp(_PATH_DEV, s2, len) == 0) {
447 				s2 += len - 1;
448 				*s2 = 'r';
449 			}
450 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
451 				fatal("%s is mounted on %s",
452 				    special, mp->f_mntonname);
453 			++mp;
454 		}
455 	}
456 	if (mfs && disktype != NULL) {
457 		struct disktab *dt;
458 
459 		if ((dt = getdisktabbyname(disktype)) == NULL)
460 			fatal("%s: unknown disk type", disktype);
461 		geom = *dt;
462 	} else {
463 		struct partinfo pinfo;
464 
465 		if (special[0] == 0)
466  			fatal("null special file name");
467 		fsi = open(special, O_RDONLY);
468 		if (fsi < 0)
469 			fatal("%s: %s", special, strerror(errno));
470 		if (fstat(fsi, &st) < 0)
471 			fatal("%s: %s", special, strerror(errno));
472 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs && !vflag)
473 			printf("%s: %s: not a character-special device\n",
474 			    progname, special);
475 #ifdef COMPAT
476 		if (!mfs && disktype == NULL)
477 			disktype = argv[1];
478 #endif
479 		if (ioctl(fsi, DIOCGPART, &pinfo) < 0) {
480 			if (!vflag) {
481 				fatal("%s: unable to retrieve geometry "
482 				      "information", argv[0]);
483 			}
484 			/*
485 			 * fake up geometry data
486 			 */
487 			geom.d_media_blksize = 512;
488 			geom.d_nheads = 16;
489 			geom.d_secpertrack = 64;
490 			geom.d_secpercyl = 1024;
491 			geom.d_media_blocks = st.st_size /
492 					      geom.d_media_blksize;
493 			geom.d_media_size = geom.d_media_blocks *
494 					    geom.d_media_blksize;
495 			/* geom.d_ncylinders not used */
496 		} else {
497 			/*
498 			 * extract geometry from pinfo
499 			 */
500 			geom.d_media_blksize = pinfo.media_blksize;
501 			geom.d_nheads = pinfo.d_nheads;
502 			geom.d_secpertrack = pinfo.d_secpertrack;
503 			geom.d_secpercyl = pinfo.d_secpercyl;
504 			/* geom.d_ncylinders not used */
505 			geom.d_media_blocks = pinfo.media_blocks;
506 			geom.d_media_size = pinfo.media_size;
507 		}
508 		if (geom.d_media_blocks == 0 || geom.d_media_size == 0) {
509 			fatal("%s: is unavailable", argv[0]);
510 		}
511 		printf("%s: media size %6.2fMB\n",
512 		        argv[0], geom.d_media_size / 1024.0 / 1024.0);
513 		if (geom.d_media_size / 512 >= 0x80000000ULL)
514 			fatal("%s: media size is too large for newfs to handle",
515 			      argv[0]);
516 	}
517 havelabel:
518 	if (fssize == 0)
519 		fssize = geom.d_media_blocks;
520 	if ((u_long)fssize > geom.d_media_blocks && !mfs) {
521 	       fatal("%s: maximum file system size is %lld blocks",
522 		     argv[0], geom.d_media_blocks);
523 	}
524 	if (rpm == 0) {
525 		rpm = geom.d_rpm;
526 		if (rpm <= 0)
527 			rpm = 3600;
528 	}
529 	if (ntracks == 0) {
530 		ntracks = geom.d_nheads;
531 		if (ntracks <= 0)
532 			fatal("%s: no default #tracks", argv[0]);
533 	}
534 	if (nsectors == 0) {
535 		nsectors = geom.d_secpertrack;
536 		if (nsectors <= 0)
537 			fatal("%s: no default #sectors/track", argv[0]);
538 	}
539 	if (sectorsize == 0) {
540 		sectorsize = geom.d_media_blksize;
541 		if (sectorsize <= 0)
542 			fatal("%s: no default sector size", argv[0]);
543 	}
544 	if (trackskew == -1) {
545 		trackskew = geom.d_trackskew;
546 		if (trackskew < 0)
547 			trackskew = 0;
548 	}
549 	if (interleave == 0) {
550 		interleave = geom.d_interleave;
551 		if (interleave <= 0)
552 			interleave = 1;
553 	}
554 	if (fsize == 0)
555 		fsize = MAX(DFL_FRAGSIZE, geom.d_media_blksize);
556 	if (bsize == 0)
557 		bsize = MIN(DFL_BLKSIZE, 8 * fsize);
558 	/*
559 	 * Maxcontig sets the default for the maximum number of blocks
560 	 * that may be allocated sequentially. With filesystem clustering
561 	 * it is possible to allocate contiguous blocks up to the maximum
562 	 * transfer size permitted by the controller or buffering.
563 	 */
564 	if (maxcontig == 0)
565 		maxcontig = MAX(1, MAXPHYS / bsize - 1);
566 	if (density == 0)
567 		density = NFPI * fsize;
568 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
569 		fprintf(stderr, "Warning: changing optimization to space ");
570 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
571 		opt = FS_OPTSPACE;
572 	}
573 	if (trackspares == -1)
574 		trackspares = 0;
575 	nphyssectors = nsectors + trackspares;
576 	if (cylspares == -1)
577 		cylspares = 0;
578 	secpercyl = nsectors * ntracks - cylspares;
579 	/*
580 	 * Only complain if -t or -u have been specified; the default
581 	 * case (4096 sectors per cylinder) is intended to disagree
582 	 * with the disklabel.
583 	 */
584 	if (t_or_u_flag && (uint32_t)secpercyl != geom.d_secpercyl)
585 		fprintf(stderr, "%s (%d) %s (%u)\n",
586 			"Warning: calculated sectors per cylinder", secpercyl,
587 			"disagrees with disk label", geom.d_secpercyl);
588 	if (maxbpg == 0)
589 		maxbpg = MAXBLKPG(bsize);
590 	headswitch = geom.d_headswitch;
591 	trackseek = geom.d_trkseek;
592 	realsectorsize = sectorsize;
593 	if (sectorsize != DEV_BSIZE) {		/* XXX */
594 		int secperblk = sectorsize / DEV_BSIZE;
595 
596 		sectorsize = DEV_BSIZE;
597 		nsectors *= secperblk;
598 		nphyssectors *= secperblk;
599 		secpercyl *= secperblk;
600 		fssize *= secperblk;
601 	}
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(special, fsi, fso, (Cflag && mfs) ? argv[1] : NULL);
612 
613 	/*
614 	 * NOTE: Newfs no longer accesses or attempts to update the
615 	 * filesystem disklabel.
616 	 *
617 	 * NOTE: fssize is converted from 512 byte sectors
618 	 * to filesystem block-sized sectors by mkfs XXX.
619 	 */
620 	if (!Nflag)
621 		close(fso);
622 	close(fsi);
623 #ifdef MFS
624 	if (mfs) {
625 		struct mfs_args args;
626 
627 		bzero(&args, sizeof(args));
628 
629 		snprintf(mfsdevname, sizeof(mfsdevname), "/dev/mfs%d",
630 			getpid());
631 		args.fspec = mfsdevname;
632 		args.export.ex_root = -2;
633 		if (mntflags & MNT_RDONLY)
634 			args.export.ex_flags = MNT_EXRDONLY;
635 		else
636 			args.export.ex_flags = 0;
637 		args.base = membase;
638 		args.size = fssize * fsize;
639 
640 		error = getvfsbyname("mfs", &vfc);
641 		if (error && vfsisloadable("mfs")) {
642 			if (vfsload("mfs"))
643 				fatal("vfsload(mfs)");
644 			endvfsent();	/* flush cache */
645 			error = getvfsbyname("mfs", &vfc);
646 		}
647 		if (error)
648 			fatal("mfs filesystem not available");
649 
650 #if 0
651 		int udev;
652 		udev = (253 << 8) | (getpid() & 255) |
653 			((getpid() & ~0xFF) << 8);
654 		if (mknod(mfsdevname, S_IFCHR | 0700, udev) < 0)
655 			printf("Warning: unable to create %s\n", mfsdevname);
656 #endif
657 		signal(SIGINT, mfsintr);
658 		if (mount(vfc.vfc_name, argv[1], mntflags, &args) < 0)
659 			fatal("%s: %s", argv[1], strerror(errno));
660 		signal(SIGINT, SIG_DFL);
661 		mfsintr(SIGINT);
662 	}
663 #endif
664 	exit(0);
665 }
666 
667 #ifdef MFS
668 
669 static void
670 mfsintr(__unused int signo)
671 {
672 	if (filename)
673 		munmap(membase, fssize * fsize);
674 #if 0
675 	remove(mfsdevname);
676 #endif
677 }
678 
679 #endif
680 
681 /*VARARGS*/
682 void
683 fatal(const char *fmt, ...)
684 {
685 	va_list ap;
686 
687 	va_start(ap, fmt);
688 	if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
689 		openlog(progname, LOG_CONS, LOG_DAEMON);
690 		vsyslog(LOG_ERR, fmt, ap);
691 		closelog();
692 	} else {
693 		vwarnx(fmt, ap);
694 	}
695 	va_end(ap);
696 	exit(1);
697 	/*NOTREACHED*/
698 }
699 
700 static void
701 usage(void)
702 {
703 	if (mfs) {
704 		fprintf(stderr,
705 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
706 			progname);
707 	} else
708 		fprintf(stderr,
709 		    "usage: %s [ -fsoptions ] special-device%s\n",
710 		    progname,
711 #ifdef COMPAT
712 		    " [device-type]");
713 #else
714 		    "");
715 #endif
716 	fprintf(stderr, "where fsoptions are:\n");
717 	fprintf(stderr, "\t-C (mfs) Copy the underlying filesystem to the MFS mount\n");
718 	fprintf(stderr, "\t-L volume name\n");
719 	fprintf(stderr,
720 	    "\t-N do not create file system, just print out parameters\n");
721 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
722 	fprintf(stderr, "\t-S sector size\n");
723 #ifdef COMPAT
724 	fprintf(stderr, "\t-T disktype\n");
725 #endif
726 	fprintf(stderr, "\t-U enable soft updates\n");
727 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
728 	fprintf(stderr, "\t-b block size\n");
729 	fprintf(stderr, "\t-c cylinders/group\n");
730 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
731 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
732 	fprintf(stderr, "\t-f frag size\n");
733 	fprintf(stderr, "\t-g average file size\n");
734 	fprintf(stderr, "\t-h average files per directory\n");
735 	fprintf(stderr, "\t-i number of bytes per inode\n");
736 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
737 	fprintf(stderr, "\t-l hardware sector interleave\n");
738 	fprintf(stderr, "\t-m minimum free space %%\n");
739 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
740 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
741 	fprintf(stderr, "\t-p spare sectors per track\n");
742 	fprintf(stderr, "\t-s file system size (sectors)\n");
743 	fprintf(stderr, "\t-r revolutions/minute\n");
744 	fprintf(stderr, "\t-t tracks/cylinder\n");
745 	fprintf(stderr, "\t-u sectors/track\n");
746 	fprintf(stderr,
747         "\t-v do not attempt to determine partition name from device name\n");
748 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
749 	exit(1);
750 }
751