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