Home | History | Annotate | Line # | Download | only in newfs
newfs.c revision 1.41
      1  1.41    simonb /*	$NetBSD: newfs.c,v 1.41 2000/12/01 12:48:09 simonb Exp $	*/
      2  1.18       cgd 
      3   1.1       cgd /*
      4  1.10   mycroft  * Copyright (c) 1983, 1989, 1993, 1994
      5  1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.25  christos #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.25  christos __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
     39  1.25  christos 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43  1.18       cgd #if 0
     44  1.27     lukem static char sccsid[] = "@(#)newfs.c	8.13 (Berkeley) 5/1/95";
     45  1.18       cgd #else
     46  1.41    simonb __RCSID("$NetBSD: newfs.c,v 1.41 2000/12/01 12:48:09 simonb Exp $");
     47  1.18       cgd #endif
     48   1.1       cgd #endif /* not lint */
     49   1.1       cgd 
     50   1.1       cgd /*
     51   1.1       cgd  * newfs: friendly front end to mkfs
     52   1.1       cgd  */
     53   1.1       cgd #include <sys/param.h>
     54   1.1       cgd #include <sys/stat.h>
     55   1.1       cgd #include <sys/ioctl.h>
     56   1.1       cgd #include <sys/disklabel.h>
     57   1.1       cgd #include <sys/file.h>
     58   1.1       cgd #include <sys/mount.h>
     59  1.19   thorpej #include <sys/sysctl.h>
     60  1.30  drochner #include <sys/wait.h>
     61   1.1       cgd 
     62  1.10   mycroft #include <ufs/ufs/dir.h>
     63  1.27     lukem #include <ufs/ufs/dinode.h>
     64  1.32      fvdl #include <ufs/ufs/ufsmount.h>
     65  1.10   mycroft #include <ufs/ffs/fs.h>
     66  1.10   mycroft 
     67  1.10   mycroft #include <ctype.h>
     68  1.37      tron #include <disktab.h>
     69   1.1       cgd #include <errno.h>
     70  1.10   mycroft #include <paths.h>
     71   1.1       cgd #include <stdio.h>
     72  1.10   mycroft #include <stdlib.h>
     73   1.1       cgd #include <string.h>
     74  1.10   mycroft #include <syslog.h>
     75  1.10   mycroft #include <unistd.h>
     76  1.30  drochner #include <signal.h>
     77  1.25  christos #include <err.h>
     78  1.20   thorpej #include <util.h>
     79  1.10   mycroft 
     80  1.10   mycroft #include "mntopts.h"
     81  1.25  christos #include "dkcksum.h"
     82  1.25  christos #include "extern.h"
     83  1.10   mycroft 
     84  1.10   mycroft struct mntopt mopts[] = {
     85  1.10   mycroft 	MOPT_STDOPTS,
     86  1.10   mycroft 	MOPT_ASYNC,
     87  1.22       cgd 	MOPT_UPDATE,
     88  1.23       tls 	MOPT_NOATIME,
     89  1.10   mycroft 	{ NULL },
     90  1.10   mycroft };
     91  1.10   mycroft 
     92  1.40    simonb static struct disklabel *getdisklabel(char *, int);
     93  1.40    simonb static void rewritelabel(char *, int, struct disklabel *);
     94  1.40    simonb static void usage(void);
     95  1.40    simonb int main(int, char *[]);
     96   1.1       cgd 
     97   1.1       cgd #define	COMPAT			/* allow non-labeled disks */
     98   1.1       cgd 
     99   1.1       cgd /*
    100   1.1       cgd  * The following two constants set the default block and fragment sizes.
    101   1.1       cgd  * Both constants must be a power of 2 and meet the following constraints:
    102   1.1       cgd  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
    103   1.1       cgd  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
    104   1.1       cgd  *	DESBLKSIZE / DESFRAGSIZE <= 8
    105   1.1       cgd  */
    106   1.1       cgd #define	DFL_FRAGSIZE	1024
    107   1.1       cgd #define	DFL_BLKSIZE	8192
    108   1.1       cgd 
    109   1.1       cgd /*
    110   1.1       cgd  * Cylinder groups may have up to many cylinders. The actual
    111   1.1       cgd  * number used depends upon how much information can be stored
    112   1.1       cgd  * on a single cylinder. The default is to use 16 cylinders
    113   1.1       cgd  * per group.
    114   1.1       cgd  */
    115   1.1       cgd #define	DESCPG		16	/* desired fs_cpg */
    116   1.1       cgd 
    117   1.1       cgd /*
    118   1.1       cgd  * ROTDELAY gives the minimum number of milliseconds to initiate
    119   1.1       cgd  * another disk transfer on the same cylinder. It is used in
    120   1.1       cgd  * determining the rotationally optimal layout for disk blocks
    121  1.13   mycroft  * within a file; the default of fs_rotdelay is 0ms.
    122   1.1       cgd  */
    123  1.13   mycroft #define ROTDELAY	0
    124   1.1       cgd 
    125   1.1       cgd /*
    126   1.1       cgd  * MAXBLKPG determines the maximum number of data blocks which are
    127   1.1       cgd  * placed in a single cylinder group. The default is one indirect
    128   1.1       cgd  * block worth of data blocks.
    129   1.1       cgd  */
    130   1.1       cgd #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
    131   1.1       cgd 
    132   1.1       cgd /*
    133   1.1       cgd  * Each file system has a number of inodes statically allocated.
    134   1.1       cgd  * We allocate one inode slot per NFPI fragments, expecting this
    135   1.1       cgd  * to be far more than we will ever need.
    136   1.1       cgd  */
    137   1.1       cgd #define	NFPI		4
    138   1.1       cgd 
    139   1.1       cgd /*
    140   1.1       cgd  * For each cylinder we keep track of the availability of blocks at different
    141   1.1       cgd  * rotational positions, so that we can lay out the data to be picked
    142   1.1       cgd  * up with minimum rotational latency.  NRPOS is the default number of
    143   1.1       cgd  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
    144  1.13   mycroft  * of our summary information is 2ms for a typical 3600 rpm drive.  Caching
    145  1.13   mycroft  * and zoning pretty much defeats rotational optimization, so we now use a
    146  1.13   mycroft  * default of 1.
    147   1.1       cgd  */
    148  1.13   mycroft #define	NRPOS		1	/* number distinct rotational positions */
    149   1.1       cgd 
    150   1.1       cgd 
    151   1.1       cgd int	mfs;			/* run as the memory based filesystem */
    152   1.1       cgd int	Nflag;			/* run without writing file system */
    153  1.10   mycroft int	Oflag;			/* format as an 4.3BSD file system */
    154   1.1       cgd int	fssize;			/* file system size */
    155   1.1       cgd int	ntracks;		/* # tracks/cylinder */
    156   1.1       cgd int	nsectors;		/* # sectors/track */
    157   1.1       cgd int	nphyssectors;		/* # sectors/track including spares */
    158   1.1       cgd int	secpercyl;		/* sectors per cylinder */
    159   1.1       cgd int	trackspares = -1;	/* spare sectors per track */
    160   1.1       cgd int	cylspares = -1;		/* spare sectors per cylinder */
    161   1.1       cgd int	sectorsize;		/* bytes/sector */
    162   1.1       cgd int	rpm;			/* revolutions/minute of drive */
    163   1.1       cgd int	interleave;		/* hardware sector interleave */
    164   1.1       cgd int	trackskew = -1;		/* sector 0 skew, per track */
    165   1.1       cgd int	headswitch;		/* head switch time, usec */
    166   1.1       cgd int	trackseek;		/* track-to-track seek, usec */
    167   1.1       cgd int	fsize = 0;		/* fragment size */
    168   1.1       cgd int	bsize = 0;		/* block size */
    169   1.1       cgd int	cpg = DESCPG;		/* cylinders/cylinder group */
    170   1.1       cgd int	cpgflg;			/* cylinders/cylinder group flag was given */
    171   1.1       cgd int	minfree = MINFREE;	/* free space threshold */
    172   1.1       cgd int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
    173   1.1       cgd int	density;		/* number of bytes per inode */
    174  1.36   mycroft int	maxcontig = 0;		/* max contiguous blocks to allocate */
    175   1.1       cgd int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
    176   1.1       cgd int	maxbpg;			/* maximum blocks per file in a cyl group */
    177   1.1       cgd int	nrpos = NRPOS;		/* # of distinguished rotational positions */
    178   1.1       cgd int	bbsize = BBSIZE;	/* boot block size */
    179   1.1       cgd int	sbsize = SBSIZE;	/* superblock size */
    180  1.10   mycroft int	mntflags = MNT_ASYNC;	/* flags to be passed to mount */
    181   1.1       cgd u_long	memleft;		/* virtual memory available */
    182   1.1       cgd caddr_t	membase;		/* start address of memory based filesystem */
    183  1.33    bouyer int needswap =0;		/* Filesystem not in native byte order */
    184   1.1       cgd #ifdef COMPAT
    185   1.1       cgd char	*disktype;
    186   1.1       cgd int	unlabeled;
    187   1.1       cgd #endif
    188   1.1       cgd 
    189   1.1       cgd char	device[MAXPATHLEN];
    190  1.25  christos extern char *__progname;
    191   1.1       cgd 
    192  1.10   mycroft int
    193  1.40    simonb main(int argc, char *argv[])
    194   1.1       cgd {
    195  1.25  christos 	struct partition *pp;
    196  1.25  christos 	struct disklabel *lp;
    197  1.14       cgd 	struct disklabel mfsfakelabel;
    198   1.1       cgd 	struct partition oldpartition;
    199   1.1       cgd 	struct stat st;
    200  1.10   mycroft 	struct statfs *mp;
    201  1.41    simonb 	int ch, fsi = 0, fso, len, maxpartitions, n;
    202  1.41    simonb 	char *cp = NULL, *endp, *opstring, *s1, *s2, *special;
    203  1.30  drochner #ifdef MFS
    204  1.30  drochner 	char mountfromname[100];
    205  1.30  drochner 	pid_t pid, res;
    206  1.30  drochner 	struct statfs sf;
    207  1.30  drochner 	int status;
    208  1.30  drochner #endif
    209   1.1       cgd 
    210  1.25  christos 	if (strstr(__progname, "mfs")) {
    211   1.1       cgd 		mfs = 1;
    212   1.1       cgd 		Nflag++;
    213   1.1       cgd 	}
    214   1.1       cgd 
    215  1.19   thorpej 	maxpartitions = getmaxpartitions();
    216  1.19   thorpej 	if (maxpartitions > 26)
    217  1.25  christos 		errx(1, "insane maxpartitions value %d", maxpartitions);
    218  1.19   thorpej 
    219  1.10   mycroft 	opstring = mfs ?
    220  1.10   mycroft 	    "NT:a:b:c:d:e:f:i:m:o:s:" :
    221  1.33    bouyer 	    "B:NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
    222  1.26     lukem 	while ((ch = getopt(argc, argv, opstring)) != -1)
    223  1.10   mycroft 		switch (ch) {
    224  1.33    bouyer 		case 'B':
    225  1.33    bouyer 			if (strcmp(optarg, "be") == 0) {
    226  1.33    bouyer #if BYTE_ORDER == LITTLE_ENDIAN
    227  1.33    bouyer 				needswap = 1;
    228  1.33    bouyer #endif
    229  1.33    bouyer 			} else if (strcmp(optarg, "le") == 0) {
    230  1.33    bouyer #if BYTE_ORDER == BIG_ENDIAN
    231  1.33    bouyer 				needswap = 1;
    232  1.33    bouyer #endif
    233  1.33    bouyer 			} else
    234  1.33    bouyer 				usage();
    235  1.33    bouyer 			break;
    236  1.10   mycroft 		case 'N':
    237  1.10   mycroft 			Nflag = 1;
    238   1.1       cgd 			break;
    239  1.10   mycroft 		case 'O':
    240  1.10   mycroft 			Oflag = 1;
    241   1.1       cgd 			break;
    242   1.1       cgd 		case 'S':
    243   1.1       cgd 			if ((sectorsize = atoi(optarg)) <= 0)
    244  1.25  christos 				errx(1, "%s: bad sector size", optarg);
    245   1.1       cgd 			break;
    246   1.1       cgd #ifdef COMPAT
    247   1.1       cgd 		case 'T':
    248   1.1       cgd 			disktype = optarg;
    249   1.1       cgd 			break;
    250   1.1       cgd #endif
    251   1.1       cgd 		case 'a':
    252   1.1       cgd 			if ((maxcontig = atoi(optarg)) <= 0)
    253  1.25  christos 				errx(1, "%s: bad maximum contiguous blocks",
    254   1.1       cgd 				    optarg);
    255   1.1       cgd 			break;
    256   1.1       cgd 		case 'b':
    257   1.1       cgd 			if ((bsize = atoi(optarg)) < MINBSIZE)
    258  1.25  christos 				errx(1, "%s: bad block size", optarg);
    259   1.1       cgd 			break;
    260   1.1       cgd 		case 'c':
    261   1.1       cgd 			if ((cpg = atoi(optarg)) <= 0)
    262  1.25  christos 				errx(1, "%s: bad cylinders/group", optarg);
    263   1.1       cgd 			cpgflg++;
    264   1.1       cgd 			break;
    265   1.1       cgd 		case 'd':
    266   1.1       cgd 			if ((rotdelay = atoi(optarg)) < 0)
    267  1.25  christos 				errx(1, "%s: bad rotational delay", optarg);
    268   1.1       cgd 			break;
    269   1.1       cgd 		case 'e':
    270   1.1       cgd 			if ((maxbpg = atoi(optarg)) <= 0)
    271  1.25  christos 		errx(1, "%s: bad blocks per file in a cylinder group",
    272   1.1       cgd 				    optarg);
    273   1.1       cgd 			break;
    274   1.1       cgd 		case 'f':
    275   1.1       cgd 			if ((fsize = atoi(optarg)) <= 0)
    276  1.25  christos 				errx(1, "%s: bad fragment size", optarg);
    277   1.1       cgd 			break;
    278   1.1       cgd 		case 'i':
    279   1.1       cgd 			if ((density = atoi(optarg)) <= 0)
    280  1.25  christos 				errx(1, "%s: bad bytes per inode", optarg);
    281   1.1       cgd 			break;
    282   1.1       cgd 		case 'k':
    283   1.1       cgd 			if ((trackskew = atoi(optarg)) < 0)
    284  1.25  christos 				errx(1, "%s: bad track skew", optarg);
    285   1.1       cgd 			break;
    286   1.1       cgd 		case 'l':
    287   1.1       cgd 			if ((interleave = atoi(optarg)) <= 0)
    288  1.25  christos 				errx(1, "%s: bad interleave", optarg);
    289   1.1       cgd 			break;
    290   1.1       cgd 		case 'm':
    291   1.1       cgd 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
    292  1.25  christos 				errx(1, "%s: bad free space %%", optarg);
    293   1.1       cgd 			break;
    294   1.1       cgd 		case 'n':
    295   1.1       cgd 			if ((nrpos = atoi(optarg)) <= 0)
    296  1.25  christos 				errx(1, "%s: bad rotational layout count",
    297   1.1       cgd 				    optarg);
    298   1.1       cgd 			break;
    299   1.1       cgd 		case 'o':
    300  1.10   mycroft 			if (mfs)
    301  1.27     lukem 				getmntopts(optarg, mopts, &mntflags, 0);
    302  1.10   mycroft 			else {
    303  1.10   mycroft 				if (strcmp(optarg, "space") == 0)
    304  1.10   mycroft 					opt = FS_OPTSPACE;
    305  1.10   mycroft 				else if (strcmp(optarg, "time") == 0)
    306  1.10   mycroft 					opt = FS_OPTTIME;
    307  1.10   mycroft 				else
    308  1.25  christos 				    errx(1, "%s %s",
    309  1.25  christos 					"unknown optimization preference: ",
    310  1.25  christos 					"use `space' or `time'.");
    311  1.10   mycroft 			}
    312   1.1       cgd 			break;
    313   1.1       cgd 		case 'p':
    314   1.1       cgd 			if ((trackspares = atoi(optarg)) < 0)
    315  1.25  christos 				errx(1, "%s: bad spare sectors per track",
    316   1.1       cgd 				    optarg);
    317   1.1       cgd 			break;
    318   1.1       cgd 		case 'r':
    319   1.1       cgd 			if ((rpm = atoi(optarg)) <= 0)
    320  1.25  christos 				errx(1, "%s: bad revolutions/minute", optarg);
    321   1.1       cgd 			break;
    322   1.1       cgd 		case 's':
    323  1.41    simonb 			fssize = (int)strtol(optarg, &endp, 10);
    324  1.41    simonb 			if (*endp) {
    325  1.41    simonb 				if (mfs) {	/* Only do 'm' for mfs */
    326  1.41    simonb 					if (*endp == 'm' || *endp == 'M')
    327  1.41    simonb 						fssize *= 1024 * 1024 / 512;
    328  1.41    simonb 					else
    329  1.41    simonb 						fssize = -1;	/* error */
    330  1.41    simonb 				}
    331  1.41    simonb 				else
    332  1.41    simonb 					fssize = -1;	/* error */
    333  1.41    simonb 			}
    334  1.41    simonb 			if (fssize <= 0)
    335  1.25  christos 				errx(1, "%s: bad file system size", optarg);
    336   1.1       cgd 			break;
    337   1.1       cgd 		case 't':
    338   1.1       cgd 			if ((ntracks = atoi(optarg)) <= 0)
    339  1.25  christos 				errx(1, "%s: bad total tracks", optarg);
    340   1.1       cgd 			break;
    341   1.1       cgd 		case 'u':
    342   1.1       cgd 			if ((nsectors = atoi(optarg)) <= 0)
    343  1.25  christos 				errx(1, "%s: bad sectors/track", optarg);
    344   1.1       cgd 			break;
    345   1.1       cgd 		case 'x':
    346   1.1       cgd 			if ((cylspares = atoi(optarg)) < 0)
    347  1.25  christos 				errx(1, "%s: bad spare sectors per cylinder",
    348   1.1       cgd 				    optarg);
    349   1.1       cgd 			break;
    350   1.1       cgd 		case '?':
    351   1.1       cgd 		default:
    352   1.1       cgd 			usage();
    353   1.1       cgd 		}
    354   1.1       cgd 	argc -= optind;
    355   1.1       cgd 	argv += optind;
    356   1.1       cgd 
    357   1.1       cgd 	if (argc != 2 && (mfs || argc != 1))
    358   1.1       cgd 		usage();
    359   1.1       cgd 
    360   1.1       cgd 	special = argv[0];
    361  1.14       cgd 	if (mfs && !strcmp(special, "swap")) {
    362  1.14       cgd 		/*
    363  1.14       cgd 		 * it's an MFS, mounted on "swap."  fake up a label.
    364  1.14       cgd 		 * XXX XXX XXX
    365  1.14       cgd 		 */
    366  1.14       cgd 		fso = -1;	/* XXX; normally done below. */
    367  1.14       cgd 
    368  1.14       cgd 		memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
    369  1.14       cgd 		mfsfakelabel.d_secsize = 512;
    370  1.14       cgd 		mfsfakelabel.d_nsectors = 64;
    371  1.14       cgd 		mfsfakelabel.d_ntracks = 16;
    372  1.14       cgd 		mfsfakelabel.d_ncylinders = 16;
    373  1.14       cgd 		mfsfakelabel.d_secpercyl = 1024;
    374  1.14       cgd 		mfsfakelabel.d_secperunit = 16384;
    375  1.14       cgd 		mfsfakelabel.d_rpm = 3600;
    376  1.14       cgd 		mfsfakelabel.d_interleave = 1;
    377  1.14       cgd 		mfsfakelabel.d_npartitions = 1;
    378  1.14       cgd 		mfsfakelabel.d_partitions[0].p_size = 16384;
    379  1.14       cgd 		mfsfakelabel.d_partitions[0].p_fsize = 1024;
    380  1.14       cgd 		mfsfakelabel.d_partitions[0].p_frag = 8;
    381  1.14       cgd 		mfsfakelabel.d_partitions[0].p_cpg = 16;
    382  1.14       cgd 
    383  1.14       cgd 		lp = &mfsfakelabel;
    384  1.14       cgd 		pp = &mfsfakelabel.d_partitions[0];
    385  1.14       cgd 
    386  1.14       cgd 		goto havelabel;
    387  1.14       cgd 	}
    388  1.11   mycroft 	cp = strrchr(special, '/');
    389   1.1       cgd 	if (cp == 0) {
    390   1.1       cgd 		/*
    391   1.1       cgd 		 * No path prefix; try /dev/r%s then /dev/%s.
    392   1.1       cgd 		 */
    393  1.34   mycroft 		(void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
    394  1.34   mycroft 		    special);
    395   1.1       cgd 		if (stat(device, &st) == -1)
    396  1.34   mycroft 			(void)snprintf(device, sizeof(device), "%s%s",
    397  1.34   mycroft 			    _PATH_DEV, special);
    398   1.1       cgd 		special = device;
    399   1.1       cgd 	}
    400  1.10   mycroft 	if (Nflag) {
    401  1.10   mycroft 		fso = -1;
    402  1.10   mycroft 	} else {
    403   1.1       cgd 		fso = open(special, O_WRONLY);
    404   1.1       cgd 		if (fso < 0)
    405  1.28     enami 			err(1, "%s: open", special);
    406  1.10   mycroft 
    407  1.10   mycroft 		/* Bail if target special is mounted */
    408  1.10   mycroft 		n = getmntinfo(&mp, MNT_NOWAIT);
    409  1.10   mycroft 		if (n == 0)
    410  1.28     enami 			err(1, "%s: getmntinfo", special);
    411  1.10   mycroft 
    412  1.10   mycroft 		len = sizeof(_PATH_DEV) - 1;
    413  1.10   mycroft 		s1 = special;
    414  1.10   mycroft 		if (strncmp(_PATH_DEV, s1, len) == 0)
    415  1.10   mycroft 			s1 += len;
    416  1.10   mycroft 
    417  1.10   mycroft 		while (--n >= 0) {
    418  1.10   mycroft 			s2 = mp->f_mntfromname;
    419  1.10   mycroft 			if (strncmp(_PATH_DEV, s2, len) == 0) {
    420  1.10   mycroft 				s2 += len - 1;
    421  1.10   mycroft 				*s2 = 'r';
    422  1.10   mycroft 			}
    423  1.10   mycroft 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
    424  1.25  christos 				errx(1, "%s is mounted on %s",
    425  1.10   mycroft 				    special, mp->f_mntonname);
    426  1.10   mycroft 			++mp;
    427  1.10   mycroft 		}
    428  1.10   mycroft 	}
    429  1.10   mycroft 	if (mfs && disktype != NULL) {
    430  1.10   mycroft 		lp = (struct disklabel *)getdiskbyname(disktype);
    431  1.10   mycroft 		if (lp == NULL)
    432  1.25  christos 			errx(1, "%s: unknown disk type", disktype);
    433  1.10   mycroft 		pp = &lp->d_partitions[1];
    434  1.10   mycroft 	} else {
    435  1.10   mycroft 		fsi = open(special, O_RDONLY);
    436  1.10   mycroft 		if (fsi < 0)
    437  1.28     enami 			err(1, "%s: open", special);
    438  1.10   mycroft 		if (fstat(fsi, &st) < 0)
    439  1.28     enami 			err(1, "%s: fstat", special);
    440  1.15   mycroft 		if (!S_ISCHR(st.st_mode) && !mfs)
    441  1.25  christos 			warnx("%s: not a character-special device", special);
    442  1.11   mycroft 		cp = strchr(argv[0], '\0') - 1;
    443  1.25  christos 		if (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
    444  1.25  christos 		    && !isdigit(*cp)))
    445  1.25  christos 			errx(1, "can't figure out file system partition");
    446   1.1       cgd #ifdef COMPAT
    447  1.10   mycroft 		if (!mfs && disktype == NULL)
    448  1.10   mycroft 			disktype = argv[1];
    449   1.1       cgd #endif
    450  1.10   mycroft 		lp = getdisklabel(special, fsi);
    451  1.10   mycroft 		if (isdigit(*cp))
    452  1.10   mycroft 			pp = &lp->d_partitions[0];
    453  1.10   mycroft 		else
    454  1.10   mycroft 			pp = &lp->d_partitions[*cp - 'a'];
    455  1.10   mycroft 		if (pp->p_size == 0)
    456  1.25  christos 			errx(1, "`%c' partition is unavailable", *cp);
    457  1.10   mycroft 		if (pp->p_fstype == FS_BOOT)
    458  1.25  christos 			errx(1, "`%c' partition overlaps boot program", *cp);
    459  1.10   mycroft 	}
    460  1.14       cgd havelabel:
    461   1.1       cgd 	if (fssize == 0)
    462   1.1       cgd 		fssize = pp->p_size;
    463   1.1       cgd 	if (fssize > pp->p_size && !mfs)
    464  1.25  christos 		errx(1, "maximum file system size on the `%c' partition is %d",
    465  1.25  christos 		    *cp, pp->p_size);
    466   1.1       cgd 	if (rpm == 0) {
    467   1.1       cgd 		rpm = lp->d_rpm;
    468   1.1       cgd 		if (rpm <= 0)
    469   1.1       cgd 			rpm = 3600;
    470   1.1       cgd 	}
    471   1.1       cgd 	if (ntracks == 0) {
    472   1.1       cgd 		ntracks = lp->d_ntracks;
    473   1.1       cgd 		if (ntracks <= 0)
    474  1.25  christos 			errx(1, "no default #tracks");
    475   1.1       cgd 	}
    476   1.1       cgd 	if (nsectors == 0) {
    477   1.1       cgd 		nsectors = lp->d_nsectors;
    478   1.1       cgd 		if (nsectors <= 0)
    479  1.25  christos 			errx(1, "no default #sectors/track");
    480   1.1       cgd 	}
    481   1.1       cgd 	if (sectorsize == 0) {
    482   1.1       cgd 		sectorsize = lp->d_secsize;
    483   1.1       cgd 		if (sectorsize <= 0)
    484  1.25  christos 			errx(1, "no default sector size");
    485   1.1       cgd 	}
    486   1.1       cgd 	if (trackskew == -1) {
    487   1.1       cgd 		trackskew = lp->d_trackskew;
    488   1.1       cgd 		if (trackskew < 0)
    489   1.1       cgd 			trackskew = 0;
    490   1.1       cgd 	}
    491   1.1       cgd 	if (interleave == 0) {
    492   1.1       cgd 		interleave = lp->d_interleave;
    493   1.1       cgd 		if (interleave <= 0)
    494   1.1       cgd 			interleave = 1;
    495   1.1       cgd 	}
    496   1.1       cgd 	if (fsize == 0) {
    497   1.1       cgd 		fsize = pp->p_fsize;
    498   1.1       cgd 		if (fsize <= 0)
    499   1.1       cgd 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
    500   1.1       cgd 	}
    501   1.1       cgd 	if (bsize == 0) {
    502   1.1       cgd 		bsize = pp->p_frag * pp->p_fsize;
    503   1.1       cgd 		if (bsize <= 0)
    504   1.1       cgd 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
    505  1.38  wrstuden 	}
    506  1.38  wrstuden 	if (cpgflg == 0) {
    507  1.38  wrstuden 		if (pp->p_cpg != 0)
    508  1.38  wrstuden 			cpg = pp->p_cpg;
    509   1.1       cgd 	}
    510  1.10   mycroft 	/*
    511  1.10   mycroft 	 * Maxcontig sets the default for the maximum number of blocks
    512  1.10   mycroft 	 * that may be allocated sequentially. With filesystem clustering
    513  1.10   mycroft 	 * it is possible to allocate contiguous blocks up to the maximum
    514  1.10   mycroft 	 * transfer size permitted by the controller or buffering.
    515  1.10   mycroft 	 */
    516  1.10   mycroft 	if (maxcontig == 0)
    517  1.27     lukem 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
    518   1.1       cgd 	if (density == 0)
    519   1.1       cgd 		density = NFPI * fsize;
    520   1.8       cgd 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
    521  1.25  christos 		warnx("%s %s %d%%", "Warning: changing optimization to space",
    522  1.25  christos 		    "because minfree is less than", MINFREE);
    523   1.1       cgd 		opt = FS_OPTSPACE;
    524   1.1       cgd 	}
    525   1.1       cgd 	if (trackspares == -1) {
    526   1.1       cgd 		trackspares = lp->d_sparespertrack;
    527   1.1       cgd 		if (trackspares < 0)
    528   1.1       cgd 			trackspares = 0;
    529   1.1       cgd 	}
    530   1.1       cgd 	nphyssectors = nsectors + trackspares;
    531   1.1       cgd 	if (cylspares == -1) {
    532   1.1       cgd 		cylspares = lp->d_sparespercyl;
    533   1.1       cgd 		if (cylspares < 0)
    534   1.1       cgd 			cylspares = 0;
    535   1.1       cgd 	}
    536   1.1       cgd 	secpercyl = nsectors * ntracks - cylspares;
    537   1.1       cgd 	if (secpercyl != lp->d_secpercyl)
    538  1.25  christos 		warnx("%s (%d) %s (%u)\n",
    539   1.1       cgd 			"Warning: calculated sectors per cylinder", secpercyl,
    540   1.1       cgd 			"disagrees with disk label", lp->d_secpercyl);
    541   1.1       cgd 	if (maxbpg == 0)
    542   1.1       cgd 		maxbpg = MAXBLKPG(bsize);
    543   1.1       cgd 	headswitch = lp->d_headswitch;
    544   1.1       cgd 	trackseek = lp->d_trkseek;
    545   1.1       cgd #ifdef notdef /* label may be 0 if faked up by kernel */
    546   1.1       cgd 	bbsize = lp->d_bbsize;
    547   1.1       cgd 	sbsize = lp->d_sbsize;
    548   1.1       cgd #endif
    549   1.1       cgd 	oldpartition = *pp;
    550   1.1       cgd 	mkfs(pp, special, fsi, fso);
    551  1.12   mycroft 	if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)))
    552   1.1       cgd 		rewritelabel(special, fso, lp);
    553   1.1       cgd 	if (!Nflag)
    554   1.1       cgd 		close(fso);
    555   1.1       cgd 	close(fsi);
    556   1.1       cgd #ifdef MFS
    557   1.1       cgd 	if (mfs) {
    558   1.1       cgd 		struct mfs_args args;
    559   1.1       cgd 
    560  1.30  drochner 		switch (pid = fork()) {
    561  1.30  drochner 		case -1:
    562  1.30  drochner 			perror("mfs");
    563  1.30  drochner 			exit(10);
    564  1.30  drochner 		case 0:
    565  1.34   mycroft 			(void)snprintf(mountfromname, sizeof(mountfromname),
    566  1.34   mycroft 			    "mfs:%d", getpid());
    567  1.30  drochner 			break;
    568  1.30  drochner 		default:
    569  1.34   mycroft 			(void)snprintf(mountfromname, sizeof(mountfromname),
    570  1.34   mycroft 			    "mfs:%d", pid);
    571  1.30  drochner 			for (;;) {
    572  1.30  drochner 				/*
    573  1.30  drochner 				 * spin until the mount succeeds
    574  1.30  drochner 				 * or the child exits
    575  1.30  drochner 				 */
    576  1.30  drochner 				usleep(1);
    577  1.30  drochner 
    578  1.30  drochner 				/*
    579  1.30  drochner 				 * XXX Here is a race condition: another process
    580  1.30  drochner 				 * can mount a filesystem which hides our
    581  1.30  drochner 				 * ramdisk before we see the success.
    582  1.30  drochner 				 */
    583  1.30  drochner 				if (statfs(argv[1], &sf) < 0)
    584  1.30  drochner 					err(88, "statfs %s", argv[1]);
    585  1.30  drochner 				if (!strcmp(sf.f_mntfromname, mountfromname) &&
    586  1.30  drochner 				    !strncmp(sf.f_mntonname, argv[1],
    587  1.30  drochner 					     MNAMELEN) &&
    588  1.30  drochner 				    !strcmp(sf.f_fstypename, "mfs"))
    589  1.30  drochner 					exit(0);
    590  1.30  drochner 
    591  1.30  drochner 				res = waitpid(pid, &status, WNOHANG);
    592  1.30  drochner 				if (res == -1)
    593  1.30  drochner 					err(11, "waitpid");
    594  1.30  drochner 				if (res != pid)
    595  1.30  drochner 					continue;
    596  1.31  drochner 				if (WIFEXITED(status)) {
    597  1.31  drochner 					if (WEXITSTATUS(status) == 0)
    598  1.31  drochner 						exit(0);
    599  1.30  drochner 					errx(1, "%s: mount: %s", argv[1],
    600  1.30  drochner 					     strerror(WEXITSTATUS(status)));
    601  1.31  drochner 				} else
    602  1.30  drochner 					errx(11, "abnormal termination");
    603  1.30  drochner 			}
    604  1.30  drochner 			/* NOTREACHED */
    605  1.30  drochner 		}
    606  1.30  drochner 
    607  1.30  drochner 		(void) setsid();
    608  1.30  drochner 		(void) close(0);
    609  1.30  drochner 		(void) close(1);
    610  1.30  drochner 		(void) close(2);
    611  1.30  drochner 		(void) chdir("/");
    612  1.30  drochner 
    613  1.30  drochner 		args.fspec = mountfromname;
    614  1.10   mycroft 		args.export.ex_root = -2;
    615  1.10   mycroft 		if (mntflags & MNT_RDONLY)
    616  1.10   mycroft 			args.export.ex_flags = MNT_EXRDONLY;
    617  1.10   mycroft 		else
    618  1.10   mycroft 			args.export.ex_flags = 0;
    619   1.1       cgd 		args.base = membase;
    620   1.1       cgd 		args.size = fssize * sectorsize;
    621   1.1       cgd 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
    622  1.30  drochner 			exit(errno); /* parent prints message */
    623   1.1       cgd 	}
    624   1.1       cgd #endif
    625   1.1       cgd 	exit(0);
    626   1.1       cgd }
    627   1.1       cgd 
    628   1.1       cgd #ifdef COMPAT
    629  1.39        is const char lmsg[] = "%s: can't read disk label; disk type must be specified";
    630   1.1       cgd #else
    631  1.39        is const char lmsg[] = "%s: can't read disk label";
    632   1.1       cgd #endif
    633   1.1       cgd 
    634  1.25  christos static struct disklabel *
    635  1.40    simonb getdisklabel(char *s, volatile int fd)
    636  1.40    simonb /* XXX why is fs volatile?! */
    637   1.1       cgd {
    638   1.1       cgd 	static struct disklabel lab;
    639   1.1       cgd 
    640  1.24       tls 	if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
    641   1.1       cgd #ifdef COMPAT
    642   1.1       cgd 		if (disktype) {
    643  1.25  christos 			struct disklabel *lp;
    644   1.1       cgd 
    645   1.1       cgd 			unlabeled++;
    646   1.1       cgd 			lp = getdiskbyname(disktype);
    647   1.1       cgd 			if (lp == NULL)
    648  1.25  christos 				errx(1, "%s: unknown disk type", disktype);
    649   1.1       cgd 			return (lp);
    650   1.1       cgd 		}
    651   1.1       cgd #endif
    652  1.10   mycroft 		warn("ioctl (GDINFO)");
    653  1.25  christos 		errx(1, lmsg, s);
    654   1.1       cgd 	}
    655   1.1       cgd 	return (&lab);
    656   1.1       cgd }
    657   1.1       cgd 
    658  1.25  christos static void
    659  1.40    simonb rewritelabel(char *s, volatile int fd, struct disklabel *lp)
    660  1.40    simonb /* XXX why is fd volatile?! */
    661   1.1       cgd {
    662   1.1       cgd #ifdef COMPAT
    663   1.1       cgd 	if (unlabeled)
    664   1.1       cgd 		return;
    665   1.1       cgd #endif
    666   1.1       cgd 	lp->d_checksum = 0;
    667   1.1       cgd 	lp->d_checksum = dkcksum(lp);
    668   1.1       cgd 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    669  1.10   mycroft 		warn("ioctl (WDINFO)");
    670  1.25  christos 		errx(1, "%s: can't rewrite disk label", s);
    671   1.1       cgd 	}
    672  1.35      matt #if __vax__
    673   1.1       cgd 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    674  1.25  christos 		int i;
    675   1.1       cgd 		int cfd;
    676   1.1       cgd 		daddr_t alt;
    677   1.1       cgd 		char specname[64];
    678   1.1       cgd 		char blk[1024];
    679   1.1       cgd 		char *cp;
    680   1.1       cgd 
    681   1.1       cgd 		/*
    682   1.1       cgd 		 * Make name for 'c' partition.
    683   1.1       cgd 		 */
    684   1.1       cgd 		strcpy(specname, s);
    685   1.1       cgd 		cp = specname + strlen(specname) - 1;
    686   1.1       cgd 		if (!isdigit(*cp))
    687   1.1       cgd 			*cp = 'c';
    688   1.1       cgd 		cfd = open(specname, O_WRONLY);
    689   1.1       cgd 		if (cfd < 0)
    690  1.28     enami 			err(1, "%s: open", specname);
    691  1.11   mycroft 		memset(blk, 0, sizeof(blk));
    692   1.1       cgd 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
    693   1.1       cgd 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    694   1.1       cgd 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    695  1.17       cgd 			off_t offset;
    696  1.17       cgd 
    697  1.17       cgd 			offset = alt + i;
    698  1.17       cgd 			offset *= lp->d_secsize;
    699  1.17       cgd 			if (lseek(cfd, offset, SEEK_SET) == -1)
    700  1.25  christos 				err(1, "lseek to badsector area: ");
    701   1.1       cgd 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    702  1.10   mycroft 				warn("alternate label %d write", i/2);
    703   1.1       cgd 		}
    704   1.1       cgd 		close(cfd);
    705   1.1       cgd 	}
    706   1.1       cgd #endif
    707   1.1       cgd }
    708   1.1       cgd 
    709  1.25  christos static void
    710  1.40    simonb usage(void)
    711   1.1       cgd {
    712   1.1       cgd 	if (mfs) {
    713   1.1       cgd 		fprintf(stderr,
    714  1.10   mycroft 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
    715  1.25  christos 			__progname);
    716   1.1       cgd 	} else
    717   1.1       cgd 		fprintf(stderr,
    718  1.10   mycroft 		    "usage: %s [ -fsoptions ] special-device%s\n",
    719  1.25  christos 		    __progname,
    720   1.1       cgd #ifdef COMPAT
    721   1.1       cgd 		    " [device-type]");
    722   1.1       cgd #else
    723   1.1       cgd 		    "");
    724   1.1       cgd #endif
    725   1.1       cgd 	fprintf(stderr, "where fsoptions are:\n");
    726  1.33    bouyer 	fprintf(stderr, "\t-B byte order (`be' or `le')\n");
    727   1.1       cgd 	fprintf(stderr,
    728   1.1       cgd 	    "\t-N do not create file system, just print out parameters\n");
    729  1.10   mycroft 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
    730   1.1       cgd 	fprintf(stderr, "\t-S sector size\n");
    731   1.1       cgd #ifdef COMPAT
    732   1.1       cgd 	fprintf(stderr, "\t-T disktype\n");
    733   1.1       cgd #endif
    734   1.1       cgd 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    735   1.1       cgd 	fprintf(stderr, "\t-b block size\n");
    736   1.1       cgd 	fprintf(stderr, "\t-c cylinders/group\n");
    737   1.1       cgd 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    738   1.1       cgd 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    739   1.1       cgd 	fprintf(stderr, "\t-f frag size\n");
    740   1.1       cgd 	fprintf(stderr, "\t-i number of bytes per inode\n");
    741   1.1       cgd 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
    742   1.1       cgd 	fprintf(stderr, "\t-l hardware sector interleave\n");
    743   1.1       cgd 	fprintf(stderr, "\t-m minimum free space %%\n");
    744   1.1       cgd 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
    745   1.1       cgd 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    746   1.1       cgd 	fprintf(stderr, "\t-p spare sectors per track\n");
    747   1.1       cgd 	fprintf(stderr, "\t-s file system size (sectors)\n");
    748   1.1       cgd 	fprintf(stderr, "\t-r revolutions/minute\n");
    749   1.1       cgd 	fprintf(stderr, "\t-t tracks/cylinder\n");
    750   1.1       cgd 	fprintf(stderr, "\t-u sectors/track\n");
    751   1.1       cgd 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
    752   1.1       cgd 	exit(1);
    753   1.1       cgd }
    754