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