Home | History | Annotate | Line # | Download | only in newfs
newfs.c revision 1.20
      1  1.20  thorpej /*	$NetBSD: newfs.c,v 1.20 1996/05/16 07:13:03 thorpej 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.1      cgd #ifndef lint
     37  1.10  mycroft static char copyright[] =
     38  1.10  mycroft "@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
     39  1.10  mycroft 	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.18      cgd static char sccsid[] = "@(#)newfs.c	8.8 (Berkeley) 4/18/94";
     45  1.18      cgd #else
     46  1.20  thorpej static char rcsid[] = "$NetBSD: newfs.c,v 1.20 1996/05/16 07:13:03 thorpej 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.1      cgd 
     61  1.10  mycroft #include <ufs/ufs/dir.h>
     62  1.10  mycroft #include <ufs/ffs/fs.h>
     63  1.10  mycroft 
     64  1.10  mycroft #include <ctype.h>
     65   1.1      cgd #include <errno.h>
     66  1.10  mycroft #include <paths.h>
     67   1.1      cgd #include <stdio.h>
     68  1.10  mycroft #include <stdlib.h>
     69   1.1      cgd #include <string.h>
     70  1.10  mycroft #include <syslog.h>
     71  1.10  mycroft #include <unistd.h>
     72  1.20  thorpej #include <util.h>
     73  1.10  mycroft 
     74  1.10  mycroft #if __STDC__
     75  1.10  mycroft #include <stdarg.h>
     76  1.10  mycroft #else
     77  1.10  mycroft #include <varargs.h>
     78  1.10  mycroft #endif
     79  1.10  mycroft 
     80  1.10  mycroft #include "mntopts.h"
     81  1.10  mycroft 
     82  1.10  mycroft struct mntopt mopts[] = {
     83  1.10  mycroft 	MOPT_STDOPTS,
     84  1.10  mycroft 	MOPT_ASYNC,
     85  1.10  mycroft 	{ NULL },
     86  1.10  mycroft };
     87  1.10  mycroft 
     88  1.10  mycroft #if __STDC__
     89  1.10  mycroft void	fatal(const char *fmt, ...);
     90  1.10  mycroft #else
     91  1.10  mycroft void	fatal();
     92  1.10  mycroft #endif
     93   1.1      cgd 
     94   1.1      cgd #define	COMPAT			/* allow non-labeled disks */
     95   1.1      cgd 
     96   1.1      cgd /*
     97   1.1      cgd  * The following two constants set the default block and fragment sizes.
     98   1.1      cgd  * Both constants must be a power of 2 and meet the following constraints:
     99   1.1      cgd  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
    100   1.1      cgd  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
    101   1.1      cgd  *	DESBLKSIZE / DESFRAGSIZE <= 8
    102   1.1      cgd  */
    103   1.1      cgd #define	DFL_FRAGSIZE	1024
    104   1.1      cgd #define	DFL_BLKSIZE	8192
    105   1.1      cgd 
    106   1.1      cgd /*
    107   1.1      cgd  * Cylinder groups may have up to many cylinders. The actual
    108   1.1      cgd  * number used depends upon how much information can be stored
    109   1.1      cgd  * on a single cylinder. The default is to use 16 cylinders
    110   1.1      cgd  * per group.
    111   1.1      cgd  */
    112   1.1      cgd #define	DESCPG		16	/* desired fs_cpg */
    113   1.1      cgd 
    114   1.1      cgd /*
    115   1.1      cgd  * ROTDELAY gives the minimum number of milliseconds to initiate
    116   1.1      cgd  * another disk transfer on the same cylinder. It is used in
    117   1.1      cgd  * determining the rotationally optimal layout for disk blocks
    118  1.13  mycroft  * within a file; the default of fs_rotdelay is 0ms.
    119   1.1      cgd  */
    120  1.13  mycroft #define ROTDELAY	0
    121   1.1      cgd 
    122   1.1      cgd /*
    123   1.1      cgd  * MAXBLKPG determines the maximum number of data blocks which are
    124   1.1      cgd  * placed in a single cylinder group. The default is one indirect
    125   1.1      cgd  * block worth of data blocks.
    126   1.1      cgd  */
    127   1.1      cgd #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
    128   1.1      cgd 
    129   1.1      cgd /*
    130   1.1      cgd  * Each file system has a number of inodes statically allocated.
    131   1.1      cgd  * We allocate one inode slot per NFPI fragments, expecting this
    132   1.1      cgd  * to be far more than we will ever need.
    133   1.1      cgd  */
    134   1.1      cgd #define	NFPI		4
    135   1.1      cgd 
    136   1.1      cgd /*
    137   1.1      cgd  * For each cylinder we keep track of the availability of blocks at different
    138   1.1      cgd  * rotational positions, so that we can lay out the data to be picked
    139   1.1      cgd  * up with minimum rotational latency.  NRPOS is the default number of
    140   1.1      cgd  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
    141  1.13  mycroft  * of our summary information is 2ms for a typical 3600 rpm drive.  Caching
    142  1.13  mycroft  * and zoning pretty much defeats rotational optimization, so we now use a
    143  1.13  mycroft  * default of 1.
    144   1.1      cgd  */
    145  1.13  mycroft #define	NRPOS		1	/* number distinct rotational positions */
    146   1.1      cgd 
    147   1.1      cgd 
    148   1.1      cgd int	mfs;			/* run as the memory based filesystem */
    149   1.1      cgd int	Nflag;			/* run without writing file system */
    150  1.10  mycroft int	Oflag;			/* format as an 4.3BSD file system */
    151   1.1      cgd int	fssize;			/* file system size */
    152   1.1      cgd int	ntracks;		/* # tracks/cylinder */
    153   1.1      cgd int	nsectors;		/* # sectors/track */
    154   1.1      cgd int	nphyssectors;		/* # sectors/track including spares */
    155   1.1      cgd int	secpercyl;		/* sectors per cylinder */
    156   1.1      cgd int	trackspares = -1;	/* spare sectors per track */
    157   1.1      cgd int	cylspares = -1;		/* spare sectors per cylinder */
    158   1.1      cgd int	sectorsize;		/* bytes/sector */
    159   1.1      cgd #ifdef tahoe
    160   1.1      cgd int	realsectorsize;		/* bytes/sector in hardware */
    161   1.1      cgd #endif
    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.13  mycroft int	maxcontig = 8;		/* 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.1      cgd #ifdef COMPAT
    184   1.1      cgd char	*disktype;
    185   1.1      cgd int	unlabeled;
    186   1.1      cgd #endif
    187   1.1      cgd 
    188   1.1      cgd char	device[MAXPATHLEN];
    189   1.1      cgd char	*progname;
    190   1.1      cgd 
    191  1.10  mycroft int
    192   1.1      cgd main(argc, argv)
    193   1.1      cgd 	int argc;
    194   1.1      cgd 	char *argv[];
    195   1.1      cgd {
    196   1.1      cgd 	extern char *optarg;
    197   1.1      cgd 	extern int optind;
    198   1.1      cgd 	register int ch;
    199   1.1      cgd 	register struct partition *pp;
    200   1.1      cgd 	register struct disklabel *lp;
    201  1.14      cgd 	struct disklabel mfsfakelabel;
    202   1.1      cgd 	struct disklabel *getdisklabel();
    203   1.1      cgd 	struct partition oldpartition;
    204   1.1      cgd 	struct stat st;
    205  1.10  mycroft 	struct statfs *mp;
    206  1.19  thorpej 	int fsi, fso, len, n, maxpartitions;
    207  1.10  mycroft 	char *cp, *s1, *s2, *special, *opstring, buf[BUFSIZ];
    208   1.1      cgd 
    209  1.11  mycroft 	if (progname = strrchr(*argv, '/'))
    210   1.1      cgd 		++progname;
    211   1.1      cgd 	else
    212   1.1      cgd 		progname = *argv;
    213   1.1      cgd 
    214   1.1      cgd 	if (strstr(progname, "mfs")) {
    215   1.1      cgd 		mfs = 1;
    216   1.1      cgd 		Nflag++;
    217   1.1      cgd 	}
    218   1.1      cgd 
    219  1.19  thorpej 	maxpartitions = getmaxpartitions();
    220  1.19  thorpej 	if (maxpartitions > 26)
    221  1.19  thorpej 		fatal("insane maxpartitions value %d", maxpartitions);
    222  1.19  thorpej 
    223  1.10  mycroft 	opstring = mfs ?
    224  1.10  mycroft 	    "NT:a:b:c:d:e:f:i:m:o:s:" :
    225  1.10  mycroft 	    "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
    226   1.1      cgd 	while ((ch = getopt(argc, argv, opstring)) != EOF)
    227  1.10  mycroft 		switch (ch) {
    228  1.10  mycroft 		case 'N':
    229  1.10  mycroft 			Nflag = 1;
    230   1.1      cgd 			break;
    231  1.10  mycroft 		case 'O':
    232  1.10  mycroft 			Oflag = 1;
    233   1.1      cgd 			break;
    234   1.1      cgd 		case 'S':
    235   1.1      cgd 			if ((sectorsize = atoi(optarg)) <= 0)
    236   1.1      cgd 				fatal("%s: bad sector size", optarg);
    237   1.1      cgd 			break;
    238   1.1      cgd #ifdef COMPAT
    239   1.1      cgd 		case 'T':
    240   1.1      cgd 			disktype = optarg;
    241   1.1      cgd 			break;
    242   1.1      cgd #endif
    243   1.1      cgd 		case 'a':
    244   1.1      cgd 			if ((maxcontig = atoi(optarg)) <= 0)
    245  1.10  mycroft 				fatal("%s: bad maximum contiguous blocks\n",
    246   1.1      cgd 				    optarg);
    247   1.1      cgd 			break;
    248   1.1      cgd 		case 'b':
    249   1.1      cgd 			if ((bsize = atoi(optarg)) < MINBSIZE)
    250   1.1      cgd 				fatal("%s: bad block size", optarg);
    251   1.1      cgd 			break;
    252   1.1      cgd 		case 'c':
    253   1.1      cgd 			if ((cpg = atoi(optarg)) <= 0)
    254   1.1      cgd 				fatal("%s: bad cylinders/group", optarg);
    255   1.1      cgd 			cpgflg++;
    256   1.1      cgd 			break;
    257   1.1      cgd 		case 'd':
    258   1.1      cgd 			if ((rotdelay = atoi(optarg)) < 0)
    259   1.1      cgd 				fatal("%s: bad rotational delay\n", optarg);
    260   1.1      cgd 			break;
    261   1.1      cgd 		case 'e':
    262   1.1      cgd 			if ((maxbpg = atoi(optarg)) <= 0)
    263  1.10  mycroft 		fatal("%s: bad blocks per file in a cylinder group\n",
    264   1.1      cgd 				    optarg);
    265   1.1      cgd 			break;
    266   1.1      cgd 		case 'f':
    267   1.1      cgd 			if ((fsize = atoi(optarg)) <= 0)
    268  1.10  mycroft 				fatal("%s: bad fragment size", optarg);
    269   1.1      cgd 			break;
    270   1.1      cgd 		case 'i':
    271   1.1      cgd 			if ((density = atoi(optarg)) <= 0)
    272   1.1      cgd 				fatal("%s: bad bytes per inode\n", optarg);
    273   1.1      cgd 			break;
    274   1.1      cgd 		case 'k':
    275   1.1      cgd 			if ((trackskew = atoi(optarg)) < 0)
    276   1.1      cgd 				fatal("%s: bad track skew", optarg);
    277   1.1      cgd 			break;
    278   1.1      cgd 		case 'l':
    279   1.1      cgd 			if ((interleave = atoi(optarg)) <= 0)
    280   1.1      cgd 				fatal("%s: bad interleave", optarg);
    281   1.1      cgd 			break;
    282   1.1      cgd 		case 'm':
    283   1.1      cgd 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
    284   1.1      cgd 				fatal("%s: bad free space %%\n", optarg);
    285   1.1      cgd 			break;
    286   1.1      cgd 		case 'n':
    287   1.1      cgd 			if ((nrpos = atoi(optarg)) <= 0)
    288   1.1      cgd 				fatal("%s: bad rotational layout count\n",
    289   1.1      cgd 				    optarg);
    290   1.1      cgd 			break;
    291   1.1      cgd 		case 'o':
    292  1.10  mycroft 			if (mfs)
    293  1.10  mycroft 				getmntopts(optarg, mopts, &mntflags);
    294  1.10  mycroft 			else {
    295  1.10  mycroft 				if (strcmp(optarg, "space") == 0)
    296  1.10  mycroft 					opt = FS_OPTSPACE;
    297  1.10  mycroft 				else if (strcmp(optarg, "time") == 0)
    298  1.10  mycroft 					opt = FS_OPTTIME;
    299  1.10  mycroft 				else
    300  1.10  mycroft 	fatal("%s: unknown optimization preference: use `space' or `time'.");
    301  1.10  mycroft 			}
    302   1.1      cgd 			break;
    303   1.1      cgd 		case 'p':
    304   1.1      cgd 			if ((trackspares = atoi(optarg)) < 0)
    305   1.1      cgd 				fatal("%s: bad spare sectors per track",
    306   1.1      cgd 				    optarg);
    307   1.1      cgd 			break;
    308   1.1      cgd 		case 'r':
    309   1.1      cgd 			if ((rpm = atoi(optarg)) <= 0)
    310  1.10  mycroft 				fatal("%s: bad revolutions/minute\n", optarg);
    311   1.1      cgd 			break;
    312   1.1      cgd 		case 's':
    313   1.1      cgd 			if ((fssize = atoi(optarg)) <= 0)
    314   1.1      cgd 				fatal("%s: bad file system size", optarg);
    315   1.1      cgd 			break;
    316   1.1      cgd 		case 't':
    317   1.1      cgd 			if ((ntracks = atoi(optarg)) <= 0)
    318   1.1      cgd 				fatal("%s: bad total tracks", optarg);
    319   1.1      cgd 			break;
    320   1.1      cgd 		case 'u':
    321   1.1      cgd 			if ((nsectors = atoi(optarg)) <= 0)
    322   1.1      cgd 				fatal("%s: bad sectors/track", optarg);
    323   1.1      cgd 			break;
    324   1.1      cgd 		case 'x':
    325   1.1      cgd 			if ((cylspares = atoi(optarg)) < 0)
    326   1.1      cgd 				fatal("%s: bad spare sectors per cylinder",
    327   1.1      cgd 				    optarg);
    328   1.1      cgd 			break;
    329   1.1      cgd 		case '?':
    330   1.1      cgd 		default:
    331   1.1      cgd 			usage();
    332   1.1      cgd 		}
    333   1.1      cgd 	argc -= optind;
    334   1.1      cgd 	argv += optind;
    335   1.1      cgd 
    336   1.1      cgd 	if (argc != 2 && (mfs || argc != 1))
    337   1.1      cgd 		usage();
    338   1.1      cgd 
    339   1.1      cgd 	special = argv[0];
    340  1.14      cgd 	if (mfs && !strcmp(special, "swap")) {
    341  1.14      cgd 		/*
    342  1.14      cgd 		 * it's an MFS, mounted on "swap."  fake up a label.
    343  1.14      cgd 		 * XXX XXX XXX
    344  1.14      cgd 		 */
    345  1.14      cgd 		fso = -1;	/* XXX; normally done below. */
    346  1.14      cgd 
    347  1.14      cgd 		memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
    348  1.14      cgd 		mfsfakelabel.d_secsize = 512;
    349  1.14      cgd 		mfsfakelabel.d_nsectors = 64;
    350  1.14      cgd 		mfsfakelabel.d_ntracks = 16;
    351  1.14      cgd 		mfsfakelabel.d_ncylinders = 16;
    352  1.14      cgd 		mfsfakelabel.d_secpercyl = 1024;
    353  1.14      cgd 		mfsfakelabel.d_secperunit = 16384;
    354  1.14      cgd 		mfsfakelabel.d_rpm = 3600;
    355  1.14      cgd 		mfsfakelabel.d_interleave = 1;
    356  1.14      cgd 		mfsfakelabel.d_npartitions = 1;
    357  1.14      cgd 		mfsfakelabel.d_partitions[0].p_size = 16384;
    358  1.14      cgd 		mfsfakelabel.d_partitions[0].p_fsize = 1024;
    359  1.14      cgd 		mfsfakelabel.d_partitions[0].p_frag = 8;
    360  1.14      cgd 		mfsfakelabel.d_partitions[0].p_cpg = 16;
    361  1.14      cgd 
    362  1.14      cgd 		lp = &mfsfakelabel;
    363  1.14      cgd 		pp = &mfsfakelabel.d_partitions[0];
    364  1.14      cgd 
    365  1.14      cgd 		goto havelabel;
    366  1.14      cgd 	}
    367  1.11  mycroft 	cp = strrchr(special, '/');
    368   1.1      cgd 	if (cp == 0) {
    369   1.1      cgd 		/*
    370   1.1      cgd 		 * No path prefix; try /dev/r%s then /dev/%s.
    371   1.1      cgd 		 */
    372   1.1      cgd 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
    373   1.1      cgd 		if (stat(device, &st) == -1)
    374   1.1      cgd 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
    375   1.1      cgd 		special = device;
    376   1.1      cgd 	}
    377  1.10  mycroft 	if (Nflag) {
    378  1.10  mycroft 		fso = -1;
    379  1.10  mycroft 	} else {
    380   1.1      cgd 		fso = open(special, O_WRONLY);
    381   1.1      cgd 		if (fso < 0)
    382   1.1      cgd 			fatal("%s: %s", special, strerror(errno));
    383  1.10  mycroft 
    384  1.10  mycroft 		/* Bail if target special is mounted */
    385  1.10  mycroft 		n = getmntinfo(&mp, MNT_NOWAIT);
    386  1.10  mycroft 		if (n == 0)
    387  1.10  mycroft 			fatal("%s: getmntinfo: %s", special, strerror(errno));
    388  1.10  mycroft 
    389  1.10  mycroft 		len = sizeof(_PATH_DEV) - 1;
    390  1.10  mycroft 		s1 = special;
    391  1.10  mycroft 		if (strncmp(_PATH_DEV, s1, len) == 0)
    392  1.10  mycroft 			s1 += len;
    393  1.10  mycroft 
    394  1.10  mycroft 		while (--n >= 0) {
    395  1.10  mycroft 			s2 = mp->f_mntfromname;
    396  1.10  mycroft 			if (strncmp(_PATH_DEV, s2, len) == 0) {
    397  1.10  mycroft 				s2 += len - 1;
    398  1.10  mycroft 				*s2 = 'r';
    399  1.10  mycroft 			}
    400  1.10  mycroft 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
    401  1.10  mycroft 				fatal("%s is mounted on %s",
    402  1.10  mycroft 				    special, mp->f_mntonname);
    403  1.10  mycroft 			++mp;
    404  1.10  mycroft 		}
    405  1.10  mycroft 	}
    406  1.10  mycroft 	if (mfs && disktype != NULL) {
    407  1.10  mycroft 		lp = (struct disklabel *)getdiskbyname(disktype);
    408  1.10  mycroft 		if (lp == NULL)
    409  1.10  mycroft 			fatal("%s: unknown disk type", disktype);
    410  1.10  mycroft 		pp = &lp->d_partitions[1];
    411  1.10  mycroft 	} else {
    412  1.10  mycroft 		fsi = open(special, O_RDONLY);
    413  1.10  mycroft 		if (fsi < 0)
    414  1.10  mycroft 			fatal("%s: %s", special, strerror(errno));
    415  1.10  mycroft 		if (fstat(fsi, &st) < 0)
    416  1.10  mycroft 			fatal("%s: %s", special, strerror(errno));
    417  1.15  mycroft 		if (!S_ISCHR(st.st_mode) && !mfs)
    418  1.10  mycroft 			printf("%s: %s: not a character-special device\n",
    419  1.10  mycroft 			    progname, special);
    420  1.11  mycroft 		cp = strchr(argv[0], '\0') - 1;
    421  1.19  thorpej 		if (cp == 0 || (*cp < 'a' || *cp > ('a' + maxpartitions - 1))
    422  1.19  thorpej 		    && !isdigit(*cp))
    423  1.10  mycroft 			fatal("%s: can't figure out file system partition",
    424  1.10  mycroft 			    argv[0]);
    425   1.1      cgd #ifdef COMPAT
    426  1.10  mycroft 		if (!mfs && disktype == NULL)
    427  1.10  mycroft 			disktype = argv[1];
    428   1.1      cgd #endif
    429  1.10  mycroft 		lp = getdisklabel(special, fsi);
    430  1.10  mycroft 		if (isdigit(*cp))
    431  1.10  mycroft 			pp = &lp->d_partitions[0];
    432  1.10  mycroft 		else
    433  1.10  mycroft 			pp = &lp->d_partitions[*cp - 'a'];
    434  1.10  mycroft 		if (pp->p_size == 0)
    435  1.10  mycroft 			fatal("%s: `%c' partition is unavailable",
    436  1.10  mycroft 			    argv[0], *cp);
    437  1.10  mycroft 		if (pp->p_fstype == FS_BOOT)
    438  1.10  mycroft 			fatal("%s: `%c' partition overlaps boot program",
    439  1.10  mycroft 			      argv[0], *cp);
    440  1.10  mycroft 	}
    441  1.14      cgd havelabel:
    442   1.1      cgd 	if (fssize == 0)
    443   1.1      cgd 		fssize = pp->p_size;
    444   1.1      cgd 	if (fssize > pp->p_size && !mfs)
    445   1.1      cgd 	       fatal("%s: maximum file system size on the `%c' partition is %d",
    446   1.1      cgd 			argv[0], *cp, pp->p_size);
    447   1.1      cgd 	if (rpm == 0) {
    448   1.1      cgd 		rpm = lp->d_rpm;
    449   1.1      cgd 		if (rpm <= 0)
    450   1.1      cgd 			rpm = 3600;
    451   1.1      cgd 	}
    452   1.1      cgd 	if (ntracks == 0) {
    453   1.1      cgd 		ntracks = lp->d_ntracks;
    454   1.1      cgd 		if (ntracks <= 0)
    455   1.1      cgd 			fatal("%s: no default #tracks", argv[0]);
    456   1.1      cgd 	}
    457   1.1      cgd 	if (nsectors == 0) {
    458   1.1      cgd 		nsectors = lp->d_nsectors;
    459   1.1      cgd 		if (nsectors <= 0)
    460   1.1      cgd 			fatal("%s: no default #sectors/track", argv[0]);
    461   1.1      cgd 	}
    462   1.1      cgd 	if (sectorsize == 0) {
    463   1.1      cgd 		sectorsize = lp->d_secsize;
    464   1.1      cgd 		if (sectorsize <= 0)
    465   1.1      cgd 			fatal("%s: no default sector size", argv[0]);
    466   1.1      cgd 	}
    467   1.1      cgd 	if (trackskew == -1) {
    468   1.1      cgd 		trackskew = lp->d_trackskew;
    469   1.1      cgd 		if (trackskew < 0)
    470   1.1      cgd 			trackskew = 0;
    471   1.1      cgd 	}
    472   1.1      cgd 	if (interleave == 0) {
    473   1.1      cgd 		interleave = lp->d_interleave;
    474   1.1      cgd 		if (interleave <= 0)
    475   1.1      cgd 			interleave = 1;
    476   1.1      cgd 	}
    477   1.1      cgd 	if (fsize == 0) {
    478   1.1      cgd 		fsize = pp->p_fsize;
    479   1.1      cgd 		if (fsize <= 0)
    480   1.1      cgd 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
    481   1.1      cgd 	}
    482   1.1      cgd 	if (bsize == 0) {
    483   1.1      cgd 		bsize = pp->p_frag * pp->p_fsize;
    484   1.1      cgd 		if (bsize <= 0)
    485   1.1      cgd 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
    486   1.1      cgd 	}
    487  1.10  mycroft 	/*
    488  1.10  mycroft 	 * Maxcontig sets the default for the maximum number of blocks
    489  1.10  mycroft 	 * that may be allocated sequentially. With filesystem clustering
    490  1.10  mycroft 	 * it is possible to allocate contiguous blocks up to the maximum
    491  1.10  mycroft 	 * transfer size permitted by the controller or buffering.
    492  1.10  mycroft 	 */
    493  1.10  mycroft 	if (maxcontig == 0)
    494  1.10  mycroft 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize - 1);
    495   1.1      cgd 	if (density == 0)
    496   1.1      cgd 		density = NFPI * fsize;
    497   1.8      cgd 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
    498   1.1      cgd 		fprintf(stderr, "Warning: changing optimization to space ");
    499   1.8      cgd 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
    500   1.1      cgd 		opt = FS_OPTSPACE;
    501   1.1      cgd 	}
    502   1.1      cgd 	if (trackspares == -1) {
    503   1.1      cgd 		trackspares = lp->d_sparespertrack;
    504   1.1      cgd 		if (trackspares < 0)
    505   1.1      cgd 			trackspares = 0;
    506   1.1      cgd 	}
    507   1.1      cgd 	nphyssectors = nsectors + trackspares;
    508   1.1      cgd 	if (cylspares == -1) {
    509   1.1      cgd 		cylspares = lp->d_sparespercyl;
    510   1.1      cgd 		if (cylspares < 0)
    511   1.1      cgd 			cylspares = 0;
    512   1.1      cgd 	}
    513   1.1      cgd 	secpercyl = nsectors * ntracks - cylspares;
    514   1.1      cgd 	if (secpercyl != lp->d_secpercyl)
    515   1.1      cgd 		fprintf(stderr, "%s (%d) %s (%lu)\n",
    516   1.1      cgd 			"Warning: calculated sectors per cylinder", secpercyl,
    517   1.1      cgd 			"disagrees with disk label", lp->d_secpercyl);
    518   1.1      cgd 	if (maxbpg == 0)
    519   1.1      cgd 		maxbpg = MAXBLKPG(bsize);
    520   1.1      cgd 	headswitch = lp->d_headswitch;
    521   1.1      cgd 	trackseek = lp->d_trkseek;
    522   1.1      cgd #ifdef notdef /* label may be 0 if faked up by kernel */
    523   1.1      cgd 	bbsize = lp->d_bbsize;
    524   1.1      cgd 	sbsize = lp->d_sbsize;
    525   1.1      cgd #endif
    526   1.1      cgd 	oldpartition = *pp;
    527   1.1      cgd #ifdef tahoe
    528   1.1      cgd 	realsectorsize = sectorsize;
    529   1.1      cgd 	if (sectorsize != DEV_BSIZE) {		/* XXX */
    530   1.1      cgd 		int secperblk = DEV_BSIZE / sectorsize;
    531   1.1      cgd 
    532   1.1      cgd 		sectorsize = DEV_BSIZE;
    533   1.1      cgd 		nsectors /= secperblk;
    534   1.1      cgd 		nphyssectors /= secperblk;
    535   1.1      cgd 		secpercyl /= secperblk;
    536   1.1      cgd 		fssize /= secperblk;
    537   1.1      cgd 		pp->p_size /= secperblk;
    538   1.1      cgd 	}
    539   1.1      cgd #endif
    540   1.1      cgd 	mkfs(pp, special, fsi, fso);
    541   1.1      cgd #ifdef tahoe
    542   1.1      cgd 	if (realsectorsize != DEV_BSIZE)
    543   1.1      cgd 		pp->p_size *= DEV_BSIZE / realsectorsize;
    544   1.1      cgd #endif
    545  1.12  mycroft 	if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)))
    546   1.1      cgd 		rewritelabel(special, fso, lp);
    547   1.1      cgd 	if (!Nflag)
    548   1.1      cgd 		close(fso);
    549   1.1      cgd 	close(fsi);
    550   1.1      cgd #ifdef MFS
    551   1.1      cgd 	if (mfs) {
    552   1.1      cgd 		struct mfs_args args;
    553   1.1      cgd 
    554   1.1      cgd 		sprintf(buf, "mfs:%d", getpid());
    555  1.10  mycroft 		args.fspec = buf;
    556  1.10  mycroft 		args.export.ex_root = -2;
    557  1.10  mycroft 		if (mntflags & MNT_RDONLY)
    558  1.10  mycroft 			args.export.ex_flags = MNT_EXRDONLY;
    559  1.10  mycroft 		else
    560  1.10  mycroft 			args.export.ex_flags = 0;
    561   1.1      cgd 		args.base = membase;
    562   1.1      cgd 		args.size = fssize * sectorsize;
    563   1.1      cgd 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
    564   1.1      cgd 			fatal("%s: %s", argv[1], strerror(errno));
    565   1.1      cgd 	}
    566   1.1      cgd #endif
    567   1.1      cgd 	exit(0);
    568   1.1      cgd }
    569   1.1      cgd 
    570   1.1      cgd #ifdef COMPAT
    571   1.1      cgd char lmsg[] = "%s: can't read disk label; disk type must be specified";
    572   1.1      cgd #else
    573   1.1      cgd char lmsg[] = "%s: can't read disk label";
    574   1.1      cgd #endif
    575   1.1      cgd 
    576   1.1      cgd struct disklabel *
    577   1.1      cgd getdisklabel(s, fd)
    578   1.1      cgd 	char *s;
    579   1.1      cgd 	int fd;
    580   1.1      cgd {
    581   1.1      cgd 	static struct disklabel lab;
    582   1.1      cgd 
    583   1.1      cgd 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    584   1.1      cgd #ifdef COMPAT
    585   1.1      cgd 		if (disktype) {
    586   1.1      cgd 			struct disklabel *lp, *getdiskbyname();
    587   1.1      cgd 
    588   1.1      cgd 			unlabeled++;
    589   1.1      cgd 			lp = getdiskbyname(disktype);
    590   1.1      cgd 			if (lp == NULL)
    591   1.1      cgd 				fatal("%s: unknown disk type", disktype);
    592   1.1      cgd 			return (lp);
    593   1.1      cgd 		}
    594   1.1      cgd #endif
    595  1.10  mycroft 		warn("ioctl (GDINFO)");
    596   1.1      cgd 		fatal(lmsg, s);
    597   1.1      cgd 	}
    598   1.1      cgd 	return (&lab);
    599   1.1      cgd }
    600   1.1      cgd 
    601   1.1      cgd rewritelabel(s, fd, lp)
    602   1.1      cgd 	char *s;
    603   1.1      cgd 	int fd;
    604   1.1      cgd 	register struct disklabel *lp;
    605   1.1      cgd {
    606   1.1      cgd #ifdef COMPAT
    607   1.1      cgd 	if (unlabeled)
    608   1.1      cgd 		return;
    609   1.1      cgd #endif
    610   1.1      cgd 	lp->d_checksum = 0;
    611   1.1      cgd 	lp->d_checksum = dkcksum(lp);
    612   1.1      cgd 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    613  1.10  mycroft 		warn("ioctl (WDINFO)");
    614   1.1      cgd 		fatal("%s: can't rewrite disk label", s);
    615   1.1      cgd 	}
    616   1.1      cgd #if vax
    617   1.1      cgd 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    618   1.1      cgd 		register i;
    619   1.1      cgd 		int cfd;
    620   1.1      cgd 		daddr_t alt;
    621   1.1      cgd 		char specname[64];
    622   1.1      cgd 		char blk[1024];
    623   1.1      cgd 		char *cp;
    624   1.1      cgd 
    625   1.1      cgd 		/*
    626   1.1      cgd 		 * Make name for 'c' partition.
    627   1.1      cgd 		 */
    628   1.1      cgd 		strcpy(specname, s);
    629   1.1      cgd 		cp = specname + strlen(specname) - 1;
    630   1.1      cgd 		if (!isdigit(*cp))
    631   1.1      cgd 			*cp = 'c';
    632   1.1      cgd 		cfd = open(specname, O_WRONLY);
    633   1.1      cgd 		if (cfd < 0)
    634   1.1      cgd 			fatal("%s: %s", specname, strerror(errno));
    635  1.11  mycroft 		memset(blk, 0, sizeof(blk));
    636   1.1      cgd 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
    637   1.1      cgd 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    638   1.1      cgd 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    639  1.17      cgd 			off_t offset;
    640  1.17      cgd 
    641  1.17      cgd 			offset = alt + i;
    642  1.17      cgd 			offset *= lp->d_secsize;
    643  1.17      cgd 			if (lseek(cfd, offset, SEEK_SET) == -1)
    644   1.1      cgd 				fatal("lseek to badsector area: %s",
    645   1.1      cgd 				    strerror(errno));
    646   1.1      cgd 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    647  1.10  mycroft 				warn("alternate label %d write", i/2);
    648   1.1      cgd 		}
    649   1.1      cgd 		close(cfd);
    650   1.1      cgd 	}
    651   1.1      cgd #endif
    652   1.1      cgd }
    653   1.1      cgd 
    654   1.1      cgd /*VARARGS*/
    655  1.10  mycroft void
    656  1.10  mycroft #if __STDC__
    657  1.10  mycroft fatal(const char *fmt, ...)
    658  1.10  mycroft #else
    659  1.10  mycroft fatal(fmt, va_alist)
    660  1.10  mycroft 	char *fmt;
    661  1.10  mycroft 	va_dcl
    662  1.10  mycroft #endif
    663   1.1      cgd {
    664  1.10  mycroft 	va_list ap;
    665  1.10  mycroft 
    666  1.10  mycroft #if __STDC__
    667  1.10  mycroft 	va_start(ap, fmt);
    668  1.10  mycroft #else
    669  1.10  mycroft 	va_start(ap);
    670  1.10  mycroft #endif
    671  1.10  mycroft 	if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
    672  1.10  mycroft 		openlog(progname, LOG_CONS, LOG_DAEMON);
    673  1.10  mycroft 		vsyslog(LOG_ERR, fmt, ap);
    674  1.10  mycroft 		closelog();
    675  1.10  mycroft 	} else {
    676  1.10  mycroft 		vwarnx(fmt, ap);
    677  1.10  mycroft 	}
    678  1.10  mycroft 	va_end(ap);
    679   1.1      cgd 	exit(1);
    680  1.10  mycroft 	/*NOTREACHED*/
    681   1.1      cgd }
    682   1.1      cgd 
    683   1.1      cgd usage()
    684   1.1      cgd {
    685   1.1      cgd 	if (mfs) {
    686   1.1      cgd 		fprintf(stderr,
    687  1.10  mycroft 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
    688  1.10  mycroft 			progname);
    689   1.1      cgd 	} else
    690   1.1      cgd 		fprintf(stderr,
    691  1.10  mycroft 		    "usage: %s [ -fsoptions ] special-device%s\n",
    692  1.10  mycroft 		    progname,
    693   1.1      cgd #ifdef COMPAT
    694   1.1      cgd 		    " [device-type]");
    695   1.1      cgd #else
    696   1.1      cgd 		    "");
    697   1.1      cgd #endif
    698   1.1      cgd 	fprintf(stderr, "where fsoptions are:\n");
    699   1.1      cgd 	fprintf(stderr,
    700   1.1      cgd 	    "\t-N do not create file system, just print out parameters\n");
    701  1.10  mycroft 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
    702   1.1      cgd 	fprintf(stderr, "\t-S sector size\n");
    703   1.1      cgd #ifdef COMPAT
    704   1.1      cgd 	fprintf(stderr, "\t-T disktype\n");
    705   1.1      cgd #endif
    706   1.1      cgd 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    707   1.1      cgd 	fprintf(stderr, "\t-b block size\n");
    708   1.1      cgd 	fprintf(stderr, "\t-c cylinders/group\n");
    709   1.1      cgd 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    710   1.1      cgd 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    711   1.1      cgd 	fprintf(stderr, "\t-f frag size\n");
    712   1.1      cgd 	fprintf(stderr, "\t-i number of bytes per inode\n");
    713   1.1      cgd 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
    714   1.1      cgd 	fprintf(stderr, "\t-l hardware sector interleave\n");
    715   1.1      cgd 	fprintf(stderr, "\t-m minimum free space %%\n");
    716   1.1      cgd 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
    717   1.1      cgd 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    718   1.1      cgd 	fprintf(stderr, "\t-p spare sectors per track\n");
    719   1.1      cgd 	fprintf(stderr, "\t-s file system size (sectors)\n");
    720   1.1      cgd 	fprintf(stderr, "\t-r revolutions/minute\n");
    721   1.1      cgd 	fprintf(stderr, "\t-t tracks/cylinder\n");
    722   1.1      cgd 	fprintf(stderr, "\t-u sectors/track\n");
    723   1.1      cgd 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
    724   1.1      cgd 	exit(1);
    725   1.1      cgd }
    726