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