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