Home | History | Annotate | Line # | Download | only in newfs
newfs.c revision 1.67
      1  1.67       agc /*	$NetBSD: newfs.c,v 1.67 2003/08/07 10:04:34 agc Exp $	*/
      2  1.67       agc 
      3  1.67       agc /*
      4  1.67       agc  * Copyright (c) 1983, 1989, 1993, 1994
      5  1.67       agc  *	The Regents of the University of California.  All rights reserved.
      6  1.67       agc  *
      7  1.67       agc  * Redistribution and use in source and binary forms, with or without
      8  1.67       agc  * modification, are permitted provided that the following conditions
      9  1.67       agc  * are met:
     10  1.67       agc  * 1. Redistributions of source code must retain the above copyright
     11  1.67       agc  *    notice, this list of conditions and the following disclaimer.
     12  1.67       agc  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.67       agc  *    notice, this list of conditions and the following disclaimer in the
     14  1.67       agc  *    documentation and/or other materials provided with the distribution.
     15  1.67       agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.67       agc  *    may be used to endorse or promote products derived from this software
     17  1.67       agc  *    without specific prior written permission.
     18  1.67       agc  *
     19  1.67       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.67       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.67       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.67       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.67       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.67       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.67       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.67       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.67       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.67       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.67       agc  * SUCH DAMAGE.
     30  1.67       agc  */
     31  1.18       cgd 
     32   1.1       cgd /*
     33  1.64      fvdl  * Copyright (c) 2002 Networks Associates Technology, Inc.
     34  1.64      fvdl  * All rights reserved.
     35  1.64      fvdl  *
     36  1.64      fvdl  * This software was developed for the FreeBSD Project by Marshall
     37  1.64      fvdl  * Kirk McKusick and Network Associates Laboratories, the Security
     38  1.64      fvdl  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
     39  1.64      fvdl  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
     40  1.64      fvdl  * research program
     41  1.64      fvdl  *
     42   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     43   1.1       cgd  * modification, are permitted provided that the following conditions
     44   1.1       cgd  * are met:
     45   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     46   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     47   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     49   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     50   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     51   1.1       cgd  *    must display the following acknowledgement:
     52   1.1       cgd  *	This product includes software developed by the University of
     53   1.1       cgd  *	California, Berkeley and its contributors.
     54   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     55   1.1       cgd  *    may be used to endorse or promote products derived from this software
     56   1.1       cgd  *    without specific prior written permission.
     57   1.1       cgd  *
     58   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68   1.1       cgd  * SUCH DAMAGE.
     69   1.1       cgd  */
     70   1.1       cgd 
     71  1.25  christos #include <sys/cdefs.h>
     72   1.1       cgd #ifndef lint
     73  1.25  christos __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
     74  1.25  christos 	The Regents of the University of California.  All rights reserved.\n");
     75   1.1       cgd #endif /* not lint */
     76   1.1       cgd 
     77   1.1       cgd #ifndef lint
     78  1.18       cgd #if 0
     79  1.27     lukem static char sccsid[] = "@(#)newfs.c	8.13 (Berkeley) 5/1/95";
     80  1.18       cgd #else
     81  1.67       agc __RCSID("$NetBSD: newfs.c,v 1.67 2003/08/07 10:04:34 agc Exp $");
     82  1.18       cgd #endif
     83   1.1       cgd #endif /* not lint */
     84   1.1       cgd 
     85   1.1       cgd /*
     86   1.1       cgd  * newfs: friendly front end to mkfs
     87   1.1       cgd  */
     88   1.1       cgd #include <sys/param.h>
     89   1.1       cgd #include <sys/ioctl.h>
     90   1.1       cgd #include <sys/disklabel.h>
     91   1.1       cgd #include <sys/file.h>
     92   1.1       cgd #include <sys/mount.h>
     93  1.19   thorpej #include <sys/sysctl.h>
     94  1.30  drochner #include <sys/wait.h>
     95   1.1       cgd 
     96  1.10   mycroft #include <ufs/ufs/dir.h>
     97  1.27     lukem #include <ufs/ufs/dinode.h>
     98  1.32      fvdl #include <ufs/ufs/ufsmount.h>
     99  1.10   mycroft #include <ufs/ffs/fs.h>
    100  1.10   mycroft 
    101  1.10   mycroft #include <ctype.h>
    102  1.37      tron #include <disktab.h>
    103  1.54    simonb #include <err.h>
    104   1.1       cgd #include <errno.h>
    105  1.54    simonb #include <grp.h>
    106  1.10   mycroft #include <paths.h>
    107  1.54    simonb #include <pwd.h>
    108  1.54    simonb #include <signal.h>
    109   1.1       cgd #include <stdio.h>
    110  1.10   mycroft #include <stdlib.h>
    111   1.1       cgd #include <string.h>
    112  1.10   mycroft #include <syslog.h>
    113  1.10   mycroft #include <unistd.h>
    114  1.20   thorpej #include <util.h>
    115  1.10   mycroft 
    116  1.10   mycroft #include "mntopts.h"
    117  1.25  christos #include "dkcksum.h"
    118  1.25  christos #include "extern.h"
    119  1.10   mycroft 
    120  1.10   mycroft struct mntopt mopts[] = {
    121  1.10   mycroft 	MOPT_STDOPTS,
    122  1.10   mycroft 	MOPT_ASYNC,
    123  1.22       cgd 	MOPT_UPDATE,
    124  1.60  christos 	MOPT_GETARGS,
    125  1.23       tls 	MOPT_NOATIME,
    126  1.10   mycroft 	{ NULL },
    127  1.10   mycroft };
    128  1.10   mycroft 
    129  1.40    simonb static struct disklabel *getdisklabel(char *, int);
    130  1.40    simonb static void rewritelabel(char *, int, struct disklabel *);
    131  1.54    simonb static gid_t mfs_group(const char *);
    132  1.54    simonb static uid_t mfs_user(const char *);
    133  1.43     lukem static int strsuftoi(const char *, const char *, int, int);
    134  1.40    simonb static void usage(void);
    135  1.40    simonb int main(int, char *[]);
    136   1.1       cgd 
    137   1.1       cgd #define	COMPAT			/* allow non-labeled disks */
    138   1.1       cgd 
    139   1.1       cgd /*
    140   1.1       cgd  * The following two constants set the default block and fragment sizes.
    141   1.1       cgd  * Both constants must be a power of 2 and meet the following constraints:
    142   1.1       cgd  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
    143   1.1       cgd  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
    144   1.1       cgd  *	DESBLKSIZE / DESFRAGSIZE <= 8
    145   1.1       cgd  */
    146  1.53  augustss /*
    147  1.53  augustss  * For file systems smaller than SMALL_FSSIZE we use the S_DFL_* defaults,
    148  1.53  augustss  * otherwise if less than MEDIUM_FSSIZE use M_DFL_*, otherwise use
    149  1.53  augustss  * L_DFL_*.
    150  1.53  augustss  */
    151  1.54    simonb #define	SMALL_FSSIZE	(20*1024*2)
    152  1.53  augustss #define	S_DFL_FRAGSIZE	512
    153  1.53  augustss #define	S_DFL_BLKSIZE	4096
    154  1.54    simonb #define	MEDIUM_FSSIZE	(1000*1024*2)
    155  1.53  augustss #define	M_DFL_FRAGSIZE	1024
    156  1.53  augustss #define	M_DFL_BLKSIZE	8192
    157  1.53  augustss #define	L_DFL_FRAGSIZE	2048
    158  1.53  augustss #define	L_DFL_BLKSIZE	16384
    159   1.1       cgd 
    160  1.65       dbj /* Apple requires the fragment size to be at least APPLEUFS_DIRBLKSIZ
    161  1.65       dbj  * but the block size cannot be larger than Darwin's PAGE_SIZE.  See
    162  1.65       dbj  * the mount check in Darwin's ffs_mountfs for an explanation.
    163  1.65       dbj  */
    164  1.65       dbj #define APPLEUFS_DFL_FRAGSIZE APPLEUFS_DIRBLKSIZ /* 1024 */
    165  1.65       dbj #define APPLEUFS_DFL_BLKSIZE 4096 /* default Darwin PAGE_SIZE */
    166  1.65       dbj 
    167   1.1       cgd /*
    168  1.43     lukem  * Default sector size.
    169  1.43     lukem  */
    170  1.54    simonb #define	DFL_SECSIZE	512
    171  1.43     lukem 
    172  1.43     lukem /*
    173   1.1       cgd  * Cylinder groups may have up to many cylinders. The actual
    174   1.1       cgd  * number used depends upon how much information can be stored
    175   1.1       cgd  * on a single cylinder. The default is to use 16 cylinders
    176   1.1       cgd  * per group.
    177   1.1       cgd  */
    178  1.64      fvdl #define	MAXBLKSPERCG	0x7fffffff	/* desired fs_fpg ("infinity") */
    179   1.1       cgd 
    180   1.1       cgd /*
    181   1.1       cgd  * MAXBLKPG determines the maximum number of data blocks which are
    182   1.1       cgd  * placed in a single cylinder group. The default is one indirect
    183   1.1       cgd  * block worth of data blocks.
    184   1.1       cgd  */
    185  1.64      fvdl #define	MAXBLKPG_UFS1(bsize)	((bsize) / sizeof(int32_t))
    186  1.64      fvdl #define	MAXBLKPG_UFS2(bsize)	((bsize) / sizeof(int64_t))
    187   1.1       cgd 
    188   1.1       cgd /*
    189   1.1       cgd  * Each file system has a number of inodes statically allocated.
    190   1.1       cgd  * We allocate one inode slot per NFPI fragments, expecting this
    191   1.1       cgd  * to be far more than we will ever need.
    192   1.1       cgd  */
    193   1.1       cgd #define	NFPI		4
    194   1.1       cgd 
    195   1.1       cgd 
    196   1.1       cgd int	mfs;			/* run as the memory based filesystem */
    197   1.1       cgd int	Nflag;			/* run without writing file system */
    198  1.64      fvdl int	Oflag = 1;		/* format as an 4.3BSD file system */
    199  1.64      fvdl int64_t	fssize;			/* file system size */
    200   1.1       cgd int	sectorsize;		/* bytes/sector */
    201   1.1       cgd int	fsize = 0;		/* fragment size */
    202   1.1       cgd int	bsize = 0;		/* block size */
    203  1.64      fvdl int	maxbsize = 0;		/* maximum clustering */
    204  1.64      fvdl int	maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */
    205   1.1       cgd int	minfree = MINFREE;	/* free space threshold */
    206   1.1       cgd int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
    207   1.1       cgd int	density;		/* number of bytes per inode */
    208  1.36   mycroft int	maxcontig = 0;		/* max contiguous blocks to allocate */
    209   1.1       cgd int	maxbpg;			/* maximum blocks per file in a cyl group */
    210  1.47     lukem int	avgfilesize = AVFILESIZ;/* expected average file size */
    211  1.47     lukem int	avgfpdir = AFPDIR;	/* expected number of files per directory */
    212   1.1       cgd int	bbsize = BBSIZE;	/* boot block size */
    213  1.64      fvdl int	sbsize = SBLOCKSIZE;	/* superblock size */
    214  1.10   mycroft int	mntflags = MNT_ASYNC;	/* flags to be passed to mount */
    215   1.1       cgd u_long	memleft;		/* virtual memory available */
    216   1.1       cgd caddr_t	membase;		/* start address of memory based filesystem */
    217  1.43     lukem int	needswap;		/* Filesystem not in native byte order */
    218   1.1       cgd #ifdef COMPAT
    219   1.1       cgd char	*disktype;
    220   1.1       cgd int	unlabeled;
    221   1.1       cgd #endif
    222  1.61       dbj char *appleufs_volname = 0; /* Apple UFS volume name */
    223  1.61       dbj int isappleufs = 0;
    224   1.1       cgd 
    225   1.1       cgd char	device[MAXPATHLEN];
    226   1.1       cgd 
    227  1.10   mycroft int
    228  1.40    simonb main(int argc, char *argv[])
    229   1.1       cgd {
    230  1.25  christos 	struct partition *pp;
    231  1.25  christos 	struct disklabel *lp;
    232  1.14       cgd 	struct disklabel mfsfakelabel;
    233   1.1       cgd 	struct partition oldpartition;
    234  1.10   mycroft 	struct statfs *mp;
    235  1.58     lukem 	int ch, fsi, fso, len, maxpartitions, n, Fflag, Iflag, Zflag;
    236  1.43     lukem 	char *cp, *endp, *s1, *s2, *special;
    237  1.43     lukem 	const char *opstring;
    238  1.43     lukem 	long long llsize;
    239  1.53  augustss 	int dfl_fragsize, dfl_blksize;
    240  1.30  drochner #ifdef MFS
    241  1.30  drochner 	char mountfromname[100];
    242  1.30  drochner 	pid_t pid, res;
    243  1.30  drochner 	struct statfs sf;
    244  1.56     lukem 	int status;
    245  1.56     lukem #endif
    246  1.54    simonb 	mode_t mfsmode;
    247  1.54    simonb 	uid_t mfsuid;
    248  1.54    simonb 	gid_t mfsgid;
    249   1.1       cgd 
    250  1.43     lukem 	cp = NULL;
    251  1.43     lukem 	fsi = fso = -1;
    252  1.58     lukem 	Fflag = Iflag = Zflag = 0;
    253  1.42       cgd 	if (strstr(getprogname(), "mfs")) {
    254   1.1       cgd 		mfs = 1;
    255  1.54    simonb 		mfsmode = 01777; /* default mode for a /tmp-type directory */
    256  1.54    simonb 		mfsuid = 0;	/* user root */
    257  1.54    simonb 		mfsgid = 0;	/* group wheel */
    258   1.1       cgd 		Nflag++;
    259   1.1       cgd 	}
    260   1.1       cgd 
    261  1.19   thorpej 	maxpartitions = getmaxpartitions();
    262  1.19   thorpej 	if (maxpartitions > 26)
    263  1.25  christos 		errx(1, "insane maxpartitions value %d", maxpartitions);
    264  1.19   thorpej 
    265  1.10   mycroft 	opstring = mfs ?
    266  1.54    simonb 	    "NT:a:b:c:d:e:f:g:h:i:m:o:p:s:u:" :
    267  1.64      fvdl 	    "B:FINO:S:T:Za:b:c:d:e:f:g:h:i:l:m:o:p:r:s:u:v:";
    268  1.26     lukem 	while ((ch = getopt(argc, argv, opstring)) != -1)
    269  1.10   mycroft 		switch (ch) {
    270  1.33    bouyer 		case 'B':
    271  1.33    bouyer 			if (strcmp(optarg, "be") == 0) {
    272  1.33    bouyer #if BYTE_ORDER == LITTLE_ENDIAN
    273  1.33    bouyer 				needswap = 1;
    274  1.33    bouyer #endif
    275  1.33    bouyer 			} else if (strcmp(optarg, "le") == 0) {
    276  1.33    bouyer #if BYTE_ORDER == BIG_ENDIAN
    277  1.33    bouyer 				needswap = 1;
    278  1.33    bouyer #endif
    279  1.33    bouyer 			} else
    280  1.33    bouyer 				usage();
    281  1.33    bouyer 			break;
    282  1.43     lukem 		case 'F':
    283  1.43     lukem 			Fflag = 1;
    284  1.43     lukem 			break;
    285  1.58     lukem 		case 'I':
    286  1.58     lukem 			Iflag = 1;
    287  1.58     lukem 			break;
    288  1.10   mycroft 		case 'N':
    289  1.10   mycroft 			Nflag = 1;
    290   1.1       cgd 			break;
    291  1.10   mycroft 		case 'O':
    292  1.64      fvdl 			Oflag = strsuftoi("format", optarg, 0, 2);
    293   1.1       cgd 			break;
    294   1.1       cgd 		case 'S':
    295  1.43     lukem 			sectorsize = strsuftoi("sector size",
    296  1.43     lukem 			    optarg, 1, INT_MAX);
    297   1.1       cgd 			break;
    298   1.1       cgd #ifdef COMPAT
    299   1.1       cgd 		case 'T':
    300   1.1       cgd 			disktype = optarg;
    301   1.1       cgd 			break;
    302   1.1       cgd #endif
    303  1.43     lukem 		case 'Z':
    304  1.43     lukem 			Zflag = 1;
    305  1.43     lukem 			break;
    306   1.1       cgd 		case 'a':
    307  1.43     lukem 			maxcontig = strsuftoi("maximum contiguous blocks",
    308  1.43     lukem 			    optarg, 1, INT_MAX);
    309   1.1       cgd 			break;
    310   1.1       cgd 		case 'b':
    311  1.43     lukem 			bsize = strsuftoi("block size",
    312  1.50     lukem 			    optarg, MINBSIZE, MAXBSIZE);
    313   1.1       cgd 			break;
    314   1.1       cgd 		case 'c':
    315  1.64      fvdl 			maxblkspercg = strsuftoi("max. blocks per group",
    316  1.43     lukem 			    optarg, 1, INT_MAX);
    317   1.1       cgd 			break;
    318   1.1       cgd 		case 'd':
    319  1.64      fvdl 			maxbsize = strsuftoi("maximum extent size",
    320  1.43     lukem 			    optarg, 0, INT_MAX);
    321   1.1       cgd 			break;
    322   1.1       cgd 		case 'e':
    323  1.43     lukem 			maxbpg = strsuftoi(
    324  1.43     lukem 			    "blocks per file in a cylinder group",
    325  1.43     lukem 			    optarg, 1, INT_MAX);
    326   1.1       cgd 			break;
    327   1.1       cgd 		case 'f':
    328  1.43     lukem 			fsize = strsuftoi("fragment size",
    329  1.50     lukem 			    optarg, 1, MAXBSIZE);
    330   1.1       cgd 			break;
    331  1.47     lukem 		case 'g':
    332  1.54    simonb 			if (mfs)
    333  1.54    simonb 				mfsgid = mfs_group(optarg);
    334  1.54    simonb 			else {
    335  1.54    simonb 				avgfilesize = strsuftoi("average file size",
    336  1.54    simonb 				    optarg, 1, INT_MAX);
    337  1.54    simonb 			}
    338  1.47     lukem 			break;
    339  1.47     lukem 		case 'h':
    340  1.47     lukem 			avgfpdir = strsuftoi("expected files per directory",
    341  1.47     lukem 			    optarg, 1, INT_MAX);
    342  1.47     lukem 			break;
    343   1.1       cgd 		case 'i':
    344  1.43     lukem 			density = strsuftoi("bytes per inode",
    345  1.43     lukem 			    optarg, 1, INT_MAX);
    346   1.1       cgd 			break;
    347   1.1       cgd 		case 'm':
    348  1.43     lukem 			minfree = strsuftoi("free space %",
    349  1.43     lukem 			    optarg, 0, 99);
    350   1.1       cgd 			break;
    351   1.1       cgd 		case 'o':
    352  1.10   mycroft 			if (mfs)
    353  1.27     lukem 				getmntopts(optarg, mopts, &mntflags, 0);
    354  1.10   mycroft 			else {
    355  1.10   mycroft 				if (strcmp(optarg, "space") == 0)
    356  1.10   mycroft 					opt = FS_OPTSPACE;
    357  1.10   mycroft 				else if (strcmp(optarg, "time") == 0)
    358  1.10   mycroft 					opt = FS_OPTTIME;
    359  1.10   mycroft 				else
    360  1.25  christos 				    errx(1, "%s %s",
    361  1.25  christos 					"unknown optimization preference: ",
    362  1.25  christos 					"use `space' or `time'.");
    363  1.10   mycroft 			}
    364   1.1       cgd 			break;
    365   1.1       cgd 		case 'p':
    366  1.54    simonb 			if (mfs) {
    367  1.54    simonb 				if ((mfsmode = strtol(optarg, NULL, 8)) <= 0)
    368  1.54    simonb 					errx(1, "bad mode `%s'", optarg);
    369  1.64      fvdl 			} else
    370  1.64      fvdl 				errx(1, "unknown option 'p'");
    371   1.1       cgd 			break;
    372   1.1       cgd 		case 's':
    373  1.43     lukem 			llsize = strtoll(optarg, &endp, 10);
    374  1.43     lukem 			if (endp[0] != '\0' && endp[1] != '\0')
    375  1.43     lukem 				llsize = -1;
    376  1.43     lukem 			else {
    377  1.43     lukem 				int	ssiz;
    378  1.43     lukem 
    379  1.43     lukem 				ssiz = (sectorsize ? sectorsize : DFL_SECSIZE);
    380  1.43     lukem 				switch (tolower((unsigned char)endp[0])) {
    381  1.43     lukem 				case 'b':
    382  1.43     lukem 					llsize /= ssiz;
    383  1.43     lukem 					break;
    384  1.43     lukem 				case 'k':
    385  1.43     lukem 					llsize *= 1024 / ssiz;
    386  1.43     lukem 					break;
    387  1.43     lukem 				case 'm':
    388  1.43     lukem 					llsize *= 1024 * 1024 / ssiz;
    389  1.43     lukem 					break;
    390  1.43     lukem 				case 'g':
    391  1.43     lukem 					llsize *= 1024 * 1024 * 1024 / ssiz;
    392  1.43     lukem 					break;
    393  1.43     lukem 				case '\0':
    394  1.43     lukem 				case 's':
    395  1.43     lukem 					break;
    396  1.43     lukem 				default:
    397  1.43     lukem 					llsize = -1;
    398  1.41    simonb 				}
    399  1.41    simonb 			}
    400  1.64      fvdl 			if (llsize > LLONG_MAX)
    401  1.43     lukem 				errx(1, "file system size `%s' is too large.",
    402  1.43     lukem 				    optarg);
    403  1.43     lukem 			if (llsize <= 0)
    404  1.43     lukem 				errx(1,
    405  1.43     lukem 			    "`%s' is not a valid number for file system size.",
    406  1.43     lukem 				    optarg);
    407  1.64      fvdl 			fssize = llsize;
    408   1.1       cgd 			break;
    409   1.1       cgd 		case 'u':
    410  1.54    simonb 			if (mfs)
    411  1.54    simonb 				mfsuid = mfs_user(optarg);
    412  1.64      fvdl 			else
    413  1.64      fvdl 				errx(1, "deprecated option 'u'");
    414   1.1       cgd 			break;
    415  1.61       dbj 		case 'v':
    416  1.61       dbj 			appleufs_volname = optarg;
    417  1.61       dbj 			if (strchr(appleufs_volname, ':') || strchr(appleufs_volname, '/'))
    418  1.61       dbj 				errx(1,"Apple UFS volume name cannot contain ':' or '/'");
    419  1.61       dbj 			if (appleufs_volname[0] == '\0')
    420  1.61       dbj 				errx(1,"Apple UFS volume name cannot be zero length");
    421  1.61       dbj 			isappleufs = 1;
    422  1.61       dbj 			break;
    423   1.1       cgd 		case '?':
    424   1.1       cgd 		default:
    425   1.1       cgd 			usage();
    426   1.1       cgd 		}
    427   1.1       cgd 	argc -= optind;
    428   1.1       cgd 	argv += optind;
    429  1.60  christos 	if (mntflags & MNT_GETARGS)
    430  1.60  christos 		goto doit;
    431   1.1       cgd 
    432   1.1       cgd 	if (argc != 2 && (mfs || argc != 1))
    433   1.1       cgd 		usage();
    434   1.1       cgd 
    435   1.1       cgd 	special = argv[0];
    436  1.49     lukem 	if (Fflag || mfs) {
    437  1.14       cgd 		/*
    438  1.49     lukem 		 * it's a file system image or an MFS, so fake up a label.
    439  1.49     lukem 		 * XXX
    440  1.14       cgd 		 */
    441  1.43     lukem 		if (!sectorsize)
    442  1.43     lukem 			sectorsize = DFL_SECSIZE;
    443  1.43     lukem 
    444  1.43     lukem 		if (Fflag && !Nflag) {	/* creating image in a regular file */
    445  1.44     lukem 			if (fssize == 0)
    446  1.44     lukem 				errx(1, "need to specify size when using -F");
    447  1.43     lukem 			fso = open(special, O_RDWR | O_CREAT | O_TRUNC, 0777);
    448  1.43     lukem 			if (fso == -1)
    449  1.43     lukem 				err(1, "can't open file %s", special);
    450  1.44     lukem 			if ((fsi = dup(fso)) == -1)
    451  1.44     lukem 				err(1, "can't dup(2) image fd");
    452  1.48     lukem 		/* XXXLUKEM: only ftruncate() regular files ? */
    453  1.43     lukem 			if (ftruncate(fso, (off_t)fssize * sectorsize) == -1)
    454  1.64      fvdl 				err(1, "can't resize %s to %lld",
    455  1.64      fvdl 				    special, (long long)fssize);
    456  1.43     lukem 
    457  1.43     lukem 			if (Zflag) {	/* pre-zero the file */
    458  1.43     lukem 				char	*buf;
    459  1.45     lukem 				int	bufsize, i;
    460  1.45     lukem 				off_t	bufrem;
    461  1.45     lukem 				struct statfs sfs;
    462  1.45     lukem 
    463  1.45     lukem 				if (fstatfs(fso, &sfs) == -1) {
    464  1.45     lukem 					warn("can't fstatfs `%s'", special);
    465  1.45     lukem 					bufsize = 8192;
    466  1.45     lukem 				} else
    467  1.45     lukem 					bufsize = sfs.f_iosize;
    468  1.43     lukem 
    469  1.45     lukem 				if ((buf = calloc(1, bufsize)) == NULL)
    470  1.43     lukem 					err(1, "can't malloc buffer of %d",
    471  1.45     lukem 					bufsize);
    472  1.45     lukem 				bufrem = fssize * sectorsize;
    473  1.43     lukem 				printf(
    474  1.45     lukem     "Creating file system image in `%s', size %lld bytes, in %d byte chunks.\n",
    475  1.45     lukem 				    special, (long long)bufrem, bufsize);
    476  1.45     lukem 				while (bufrem > 0) {
    477  1.45     lukem 					i = write(fso, buf,
    478  1.45     lukem 					    MIN(bufsize, bufrem));
    479  1.45     lukem 					if (i == -1)
    480  1.45     lukem 						err(1, "writing image");
    481  1.45     lukem 					bufrem -= i;
    482  1.43     lukem 				}
    483  1.43     lukem 			}
    484  1.43     lukem 
    485  1.43     lukem 		}
    486  1.14       cgd 
    487  1.14       cgd 		memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
    488  1.43     lukem 		mfsfakelabel.d_secsize = sectorsize;
    489  1.43     lukem 		mfsfakelabel.d_nsectors = 64;	/* these 3 add up to 16MB */
    490  1.14       cgd 		mfsfakelabel.d_ntracks = 16;
    491  1.14       cgd 		mfsfakelabel.d_ncylinders = 16;
    492  1.43     lukem 		mfsfakelabel.d_secpercyl =
    493  1.43     lukem 		    mfsfakelabel.d_nsectors * mfsfakelabel.d_ntracks;
    494  1.43     lukem 		mfsfakelabel.d_secperunit =
    495  1.43     lukem 		    mfsfakelabel.d_ncylinders * mfsfakelabel.d_secpercyl;
    496  1.49     lukem 		mfsfakelabel.d_rpm = 10000;
    497  1.14       cgd 		mfsfakelabel.d_interleave = 1;
    498  1.14       cgd 		mfsfakelabel.d_npartitions = 1;
    499  1.43     lukem 		mfsfakelabel.d_partitions[0].p_size = mfsfakelabel.d_secperunit;
    500  1.14       cgd 		mfsfakelabel.d_partitions[0].p_fsize = 1024;
    501  1.14       cgd 		mfsfakelabel.d_partitions[0].p_frag = 8;
    502  1.14       cgd 		mfsfakelabel.d_partitions[0].p_cpg = 16;
    503  1.14       cgd 
    504  1.14       cgd 		lp = &mfsfakelabel;
    505  1.14       cgd 		pp = &mfsfakelabel.d_partitions[0];
    506  1.49     lukem 	} else {	/* !Fflag && !mfs */
    507  1.49     lukem 		fsi = opendisk(special, O_RDONLY, device, sizeof(device), 0);
    508  1.49     lukem 		special = device;
    509  1.49     lukem 		if (fsi < 0)
    510  1.49     lukem 			err(1, "%s: open for read", special);
    511  1.49     lukem 
    512  1.49     lukem 		if (Nflag) {
    513  1.49     lukem 			fso = -1;
    514  1.49     lukem 		} else {
    515  1.49     lukem 			fso = open(special, O_WRONLY);
    516  1.49     lukem 			if (fso < 0)
    517  1.49     lukem 				err(1, "%s: open for write", special);
    518  1.49     lukem 
    519  1.49     lukem 			/* Bail if target special is mounted */
    520  1.49     lukem 			n = getmntinfo(&mp, MNT_NOWAIT);
    521  1.49     lukem 			if (n == 0)
    522  1.49     lukem 				err(1, "%s: getmntinfo", special);
    523  1.49     lukem 
    524  1.49     lukem 			len = sizeof(_PATH_DEV) - 1;
    525  1.49     lukem 			s1 = special;
    526  1.49     lukem 			if (strncmp(_PATH_DEV, s1, len) == 0)
    527  1.49     lukem 				s1 += len;
    528  1.49     lukem 
    529  1.49     lukem 			while (--n >= 0) {
    530  1.49     lukem 				s2 = mp->f_mntfromname;
    531  1.49     lukem 				if (strncmp(_PATH_DEV, s2, len) == 0) {
    532  1.49     lukem 					s2 += len - 1;
    533  1.49     lukem 					*s2 = 'r';
    534  1.49     lukem 				}
    535  1.49     lukem 				if (strcmp(s1, s2) == 0 ||
    536  1.49     lukem 				    strcmp(s1, &s2[1]) == 0)
    537  1.49     lukem 					errx(1, "%s is mounted on %s",
    538  1.49     lukem 					    special, mp->f_mntonname);
    539  1.49     lukem 				++mp;
    540  1.10   mycroft 			}
    541  1.10   mycroft 		}
    542  1.11   mycroft 		cp = strchr(argv[0], '\0') - 1;
    543  1.25  christos 		if (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
    544  1.25  christos 		    && !isdigit(*cp)))
    545  1.25  christos 			errx(1, "can't figure out file system partition");
    546   1.1       cgd #ifdef COMPAT
    547  1.49     lukem 		if (disktype == NULL)
    548  1.10   mycroft 			disktype = argv[1];
    549   1.1       cgd #endif
    550  1.10   mycroft 		lp = getdisklabel(special, fsi);
    551  1.10   mycroft 		if (isdigit(*cp))
    552  1.10   mycroft 			pp = &lp->d_partitions[0];
    553  1.10   mycroft 		else
    554  1.10   mycroft 			pp = &lp->d_partitions[*cp - 'a'];
    555  1.10   mycroft 		if (pp->p_size == 0)
    556  1.25  christos 			errx(1, "`%c' partition is unavailable", *cp);
    557  1.61       dbj 		if (pp->p_fstype == FS_APPLEUFS)
    558  1.61       dbj 			isappleufs = 1;
    559  1.61       dbj 		if (isappleufs) {
    560  1.61       dbj 			if (!Iflag && (pp->p_fstype != FS_APPLEUFS))
    561  1.61       dbj 				errx(1, "`%c' partition type is not `Apple UFS'", *cp);
    562  1.61       dbj 		} else {
    563  1.61       dbj 			if (!Iflag && (pp->p_fstype != FS_BSDFFS))
    564  1.61       dbj 				errx(1, "`%c' partition type is not `4.2BSD'", *cp);
    565  1.61       dbj 		}
    566  1.49     lukem 	}	/* !Fflag && !mfs */
    567  1.43     lukem 
    568   1.1       cgd 	if (fssize == 0)
    569   1.1       cgd 		fssize = pp->p_size;
    570  1.43     lukem 	if (fssize > pp->p_size && !mfs && !Fflag)
    571  1.25  christos 		errx(1, "maximum file system size on the `%c' partition is %d",
    572  1.25  christos 		    *cp, pp->p_size);
    573   1.1       cgd 	if (sectorsize == 0) {
    574   1.1       cgd 		sectorsize = lp->d_secsize;
    575   1.1       cgd 		if (sectorsize <= 0)
    576  1.25  christos 			errx(1, "no default sector size");
    577   1.1       cgd 	}
    578  1.53  augustss 
    579  1.65       dbj 	if (isappleufs) {
    580  1.65       dbj 		dfl_fragsize = APPLEUFS_DFL_FRAGSIZE;
    581  1.65       dbj 		dfl_blksize = APPLEUFS_DFL_BLKSIZE;
    582  1.65       dbj 	} else if (fssize < SMALL_FSSIZE) {
    583  1.53  augustss 		dfl_fragsize = S_DFL_FRAGSIZE;
    584  1.53  augustss 		dfl_blksize = S_DFL_BLKSIZE;
    585  1.53  augustss 	} else if (fssize < MEDIUM_FSSIZE) {
    586  1.53  augustss 		dfl_fragsize = M_DFL_FRAGSIZE;
    587  1.53  augustss 		dfl_blksize = M_DFL_BLKSIZE;
    588  1.53  augustss 	} else {
    589  1.53  augustss 		dfl_fragsize = L_DFL_FRAGSIZE;
    590  1.53  augustss 		dfl_blksize = L_DFL_BLKSIZE;
    591  1.53  augustss 	}
    592  1.53  augustss 
    593   1.1       cgd 	if (fsize == 0) {
    594   1.1       cgd 		fsize = pp->p_fsize;
    595   1.1       cgd 		if (fsize <= 0)
    596  1.53  augustss 			fsize = MAX(dfl_fragsize, lp->d_secsize);
    597   1.1       cgd 	}
    598   1.1       cgd 	if (bsize == 0) {
    599   1.1       cgd 		bsize = pp->p_frag * pp->p_fsize;
    600   1.1       cgd 		if (bsize <= 0)
    601  1.53  augustss 			bsize = MIN(dfl_blksize, 8 * fsize);
    602   1.1       cgd 	}
    603  1.65       dbj 
    604  1.65       dbj 	if (isappleufs && (fsize < APPLEUFS_DFL_FRAGSIZE)) {
    605  1.65       dbj 		warnx("Warning: chosen fsize of %d is less than Apple UFS minimum of %d",
    606  1.65       dbj 			fsize,APPLEUFS_DFL_FRAGSIZE);
    607  1.65       dbj 	}
    608  1.65       dbj 	if (isappleufs && (bsize > APPLEUFS_DFL_BLKSIZE)) {
    609  1.65       dbj 		warnx("Warning: chosen bsize of %d is greater than Apple UFS maximum of %d",
    610  1.65       dbj 			bsize,APPLEUFS_DFL_BLKSIZE);
    611  1.65       dbj 	}
    612  1.65       dbj 
    613  1.10   mycroft 	/*
    614  1.10   mycroft 	 * Maxcontig sets the default for the maximum number of blocks
    615  1.10   mycroft 	 * that may be allocated sequentially. With filesystem clustering
    616  1.10   mycroft 	 * it is possible to allocate contiguous blocks up to the maximum
    617  1.10   mycroft 	 * transfer size permitted by the controller or buffering.
    618  1.10   mycroft 	 */
    619  1.10   mycroft 	if (maxcontig == 0)
    620  1.27     lukem 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
    621   1.1       cgd 	if (density == 0)
    622   1.1       cgd 		density = NFPI * fsize;
    623   1.8       cgd 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
    624  1.25  christos 		warnx("%s %s %d%%", "Warning: changing optimization to space",
    625  1.25  christos 		    "because minfree is less than", MINFREE);
    626   1.1       cgd 		opt = FS_OPTSPACE;
    627   1.1       cgd 	}
    628  1.64      fvdl 	if (maxbpg == 0) {
    629  1.64      fvdl 		if (Oflag <= 1)
    630  1.64      fvdl 			maxbpg = MAXBLKPG_UFS1(bsize);
    631  1.64      fvdl 		else
    632  1.64      fvdl 			maxbpg = MAXBLKPG_UFS2(bsize);
    633  1.64      fvdl 	}
    634   1.1       cgd #ifdef notdef /* label may be 0 if faked up by kernel */
    635   1.1       cgd 	bbsize = lp->d_bbsize;
    636   1.1       cgd 	sbsize = lp->d_sbsize;
    637   1.1       cgd #endif
    638   1.1       cgd 	oldpartition = *pp;
    639  1.54    simonb 	mkfs(pp, special, fsi, fso, mfsmode, mfsuid, mfsgid);
    640  1.43     lukem 	if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)) && !Fflag)
    641   1.1       cgd 		rewritelabel(special, fso, lp);
    642   1.1       cgd 	if (!Nflag)
    643   1.1       cgd 		close(fso);
    644   1.1       cgd 	close(fsi);
    645   1.1       cgd #ifdef MFS
    646   1.1       cgd 	if (mfs) {
    647   1.1       cgd 		struct mfs_args args;
    648   1.1       cgd 
    649  1.30  drochner 		switch (pid = fork()) {
    650  1.30  drochner 		case -1:
    651  1.30  drochner 			perror("mfs");
    652  1.30  drochner 			exit(10);
    653  1.30  drochner 		case 0:
    654  1.34   mycroft 			(void)snprintf(mountfromname, sizeof(mountfromname),
    655  1.34   mycroft 			    "mfs:%d", getpid());
    656  1.30  drochner 			break;
    657  1.30  drochner 		default:
    658  1.34   mycroft 			(void)snprintf(mountfromname, sizeof(mountfromname),
    659  1.34   mycroft 			    "mfs:%d", pid);
    660  1.30  drochner 			for (;;) {
    661  1.30  drochner 				/*
    662  1.30  drochner 				 * spin until the mount succeeds
    663  1.30  drochner 				 * or the child exits
    664  1.30  drochner 				 */
    665  1.30  drochner 				usleep(1);
    666  1.30  drochner 
    667  1.30  drochner 				/*
    668  1.30  drochner 				 * XXX Here is a race condition: another process
    669  1.30  drochner 				 * can mount a filesystem which hides our
    670  1.30  drochner 				 * ramdisk before we see the success.
    671  1.30  drochner 				 */
    672  1.30  drochner 				if (statfs(argv[1], &sf) < 0)
    673  1.30  drochner 					err(88, "statfs %s", argv[1]);
    674  1.30  drochner 				if (!strcmp(sf.f_mntfromname, mountfromname) &&
    675  1.30  drochner 				    !strncmp(sf.f_mntonname, argv[1],
    676  1.30  drochner 					     MNAMELEN) &&
    677  1.30  drochner 				    !strcmp(sf.f_fstypename, "mfs"))
    678  1.30  drochner 					exit(0);
    679  1.30  drochner 
    680  1.30  drochner 				res = waitpid(pid, &status, WNOHANG);
    681  1.30  drochner 				if (res == -1)
    682  1.30  drochner 					err(11, "waitpid");
    683  1.30  drochner 				if (res != pid)
    684  1.30  drochner 					continue;
    685  1.31  drochner 				if (WIFEXITED(status)) {
    686  1.31  drochner 					if (WEXITSTATUS(status) == 0)
    687  1.31  drochner 						exit(0);
    688  1.30  drochner 					errx(1, "%s: mount: %s", argv[1],
    689  1.30  drochner 					     strerror(WEXITSTATUS(status)));
    690  1.31  drochner 				} else
    691  1.30  drochner 					errx(11, "abnormal termination");
    692  1.30  drochner 			}
    693  1.30  drochner 			/* NOTREACHED */
    694  1.30  drochner 		}
    695  1.30  drochner 
    696  1.30  drochner 		(void) setsid();
    697  1.30  drochner 		(void) close(0);
    698  1.30  drochner 		(void) close(1);
    699  1.30  drochner 		(void) close(2);
    700  1.30  drochner 		(void) chdir("/");
    701  1.30  drochner 
    702  1.30  drochner 		args.fspec = mountfromname;
    703  1.10   mycroft 		args.export.ex_root = -2;
    704  1.10   mycroft 		if (mntflags & MNT_RDONLY)
    705  1.10   mycroft 			args.export.ex_flags = MNT_EXRDONLY;
    706  1.10   mycroft 		else
    707  1.10   mycroft 			args.export.ex_flags = 0;
    708   1.1       cgd 		args.base = membase;
    709   1.1       cgd 		args.size = fssize * sectorsize;
    710  1.60  christos doit:
    711  1.60  christos 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0) {
    712  1.60  christos 			if (mntflags & MNT_GETARGS)
    713  1.60  christos 				err(1, "mount `%s' failed", argv[1]);
    714  1.30  drochner 			exit(errno); /* parent prints message */
    715  1.60  christos 		}
    716  1.60  christos 		if (mntflags & MNT_GETARGS)
    717  1.60  christos 			printf("base=%p, size=%ld\n", args.base, args.size);
    718   1.1       cgd 	}
    719   1.1       cgd #endif
    720   1.1       cgd 	exit(0);
    721   1.1       cgd }
    722   1.1       cgd 
    723   1.1       cgd #ifdef COMPAT
    724  1.39        is const char lmsg[] = "%s: can't read disk label; disk type must be specified";
    725   1.1       cgd #else
    726  1.39        is const char lmsg[] = "%s: can't read disk label";
    727   1.1       cgd #endif
    728   1.1       cgd 
    729  1.25  christos static struct disklabel *
    730  1.40    simonb getdisklabel(char *s, volatile int fd)
    731  1.40    simonb /* XXX why is fs volatile?! */
    732   1.1       cgd {
    733   1.1       cgd 	static struct disklabel lab;
    734   1.1       cgd 
    735  1.24       tls 	if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
    736   1.1       cgd #ifdef COMPAT
    737   1.1       cgd 		if (disktype) {
    738  1.25  christos 			struct disklabel *lp;
    739   1.1       cgd 
    740   1.1       cgd 			unlabeled++;
    741   1.1       cgd 			lp = getdiskbyname(disktype);
    742   1.1       cgd 			if (lp == NULL)
    743  1.25  christos 				errx(1, "%s: unknown disk type", disktype);
    744   1.1       cgd 			return (lp);
    745   1.1       cgd 		}
    746   1.1       cgd #endif
    747  1.10   mycroft 		warn("ioctl (GDINFO)");
    748  1.25  christos 		errx(1, lmsg, s);
    749   1.1       cgd 	}
    750   1.1       cgd 	return (&lab);
    751   1.1       cgd }
    752   1.1       cgd 
    753  1.25  christos static void
    754  1.40    simonb rewritelabel(char *s, volatile int fd, struct disklabel *lp)
    755  1.40    simonb /* XXX why is fd volatile?! */
    756   1.1       cgd {
    757   1.1       cgd #ifdef COMPAT
    758   1.1       cgd 	if (unlabeled)
    759   1.1       cgd 		return;
    760   1.1       cgd #endif
    761   1.1       cgd 	lp->d_checksum = 0;
    762   1.1       cgd 	lp->d_checksum = dkcksum(lp);
    763   1.1       cgd 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    764  1.57   thorpej 		if (errno == ESRCH)
    765  1.57   thorpej 			return;
    766  1.10   mycroft 		warn("ioctl (WDINFO)");
    767  1.25  christos 		errx(1, "%s: can't rewrite disk label", s);
    768   1.1       cgd 	}
    769  1.35      matt #if __vax__
    770   1.1       cgd 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    771  1.25  christos 		int i;
    772   1.1       cgd 		int cfd;
    773   1.1       cgd 		daddr_t alt;
    774  1.62       scw 		off_t loff;
    775   1.1       cgd 		char specname[64];
    776   1.1       cgd 		char blk[1024];
    777   1.1       cgd 		char *cp;
    778   1.1       cgd 
    779   1.1       cgd 		/*
    780   1.1       cgd 		 * Make name for 'c' partition.
    781   1.1       cgd 		 */
    782  1.66    itojun 		strlcpy(specname, s, sizeof(specname));
    783   1.1       cgd 		cp = specname + strlen(specname) - 1;
    784   1.1       cgd 		if (!isdigit(*cp))
    785   1.1       cgd 			*cp = 'c';
    786   1.1       cgd 		cfd = open(specname, O_WRONLY);
    787   1.1       cgd 		if (cfd < 0)
    788  1.28     enami 			err(1, "%s: open", specname);
    789  1.62       scw 		if ((loff = getlabeloffset()) < 0)
    790  1.62       scw 			err(1, "getlabeloffset()");
    791  1.11   mycroft 		memset(blk, 0, sizeof(blk));
    792  1.62       scw 		*(struct disklabel *)(blk + loff) = *lp;
    793   1.1       cgd 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    794   1.1       cgd 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    795  1.17       cgd 			off_t offset;
    796  1.17       cgd 
    797  1.17       cgd 			offset = alt + i;
    798  1.17       cgd 			offset *= lp->d_secsize;
    799  1.17       cgd 			if (lseek(cfd, offset, SEEK_SET) == -1)
    800  1.25  christos 				err(1, "lseek to badsector area: ");
    801   1.1       cgd 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    802  1.10   mycroft 				warn("alternate label %d write", i/2);
    803   1.1       cgd 		}
    804   1.1       cgd 		close(cfd);
    805   1.1       cgd 	}
    806   1.1       cgd #endif
    807   1.1       cgd }
    808   1.1       cgd 
    809  1.54    simonb static gid_t
    810  1.54    simonb mfs_group(const char *gname)
    811  1.54    simonb {
    812  1.54    simonb 	struct group *gp;
    813  1.54    simonb 
    814  1.54    simonb 	if (!(gp = getgrnam(gname)) && !isdigit((unsigned char)*gname))
    815  1.54    simonb 		errx(1, "unknown gname %s", gname);
    816  1.54    simonb 	return gp ? gp->gr_gid : atoi(gname);
    817  1.54    simonb }
    818  1.54    simonb 
    819  1.54    simonb static uid_t
    820  1.54    simonb mfs_user(const char *uname)
    821  1.54    simonb {
    822  1.54    simonb 	struct passwd *pp;
    823  1.54    simonb 
    824  1.54    simonb 	if (!(pp = getpwnam(uname)) && !isdigit((unsigned char)*uname))
    825  1.54    simonb 		errx(1, "unknown user %s", uname);
    826  1.54    simonb 	return pp ? pp->pw_uid : atoi(uname);
    827  1.54    simonb }
    828  1.54    simonb 
    829  1.43     lukem static int
    830  1.43     lukem strsuftoi(const char *desc, const char *arg, int min, int max)
    831  1.43     lukem {
    832  1.43     lukem 	long long result;
    833  1.43     lukem 	char	*ep;
    834  1.43     lukem 
    835  1.43     lukem 	errno = 0;
    836  1.43     lukem 	result = strtoll(arg, &ep, 10);
    837  1.43     lukem 	if (ep[0] != '\0' && ep[1] != '\0')
    838  1.43     lukem 		errx(1, "%s `%s' is not a valid number.", desc, arg);
    839  1.43     lukem 	switch (tolower((unsigned char)ep[0])) {
    840  1.43     lukem 	case '\0':
    841  1.43     lukem 	case 'b':
    842  1.43     lukem 		break;
    843  1.43     lukem 	case 'k':
    844  1.43     lukem 		result <<= 10;
    845  1.43     lukem 		break;
    846  1.43     lukem 	case 'm':
    847  1.43     lukem 		result <<= 20;
    848  1.43     lukem 		break;
    849  1.43     lukem 	case 'g':
    850  1.43     lukem 		result <<= 30;
    851  1.43     lukem 		break;
    852  1.43     lukem 	default:
    853  1.43     lukem 		errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
    854  1.43     lukem 	}
    855  1.43     lukem 	if (result < min)
    856  1.43     lukem 		errx(1, "%s `%s' (%lld) is less than minimum (%d).",
    857  1.43     lukem 		    desc, arg, result, min);
    858  1.43     lukem 	if (result > max)
    859  1.43     lukem 		errx(1, "%s `%s' (%lld) is greater than maximum (%d).",
    860  1.43     lukem 		    desc, arg, result, max);
    861  1.43     lukem 	return ((int)result);
    862  1.43     lukem }
    863  1.43     lukem 
    864  1.54    simonb #define	NEWFS		1
    865  1.54    simonb #define	MFS_MOUNT	2
    866  1.54    simonb #define	BOTH		NEWFS | MFS_MOUNT
    867  1.54    simonb 
    868  1.54    simonb struct help_strings {
    869  1.54    simonb 	int flags;
    870  1.54    simonb 	const char *str;
    871  1.54    simonb } const help_strings[] = {
    872  1.54    simonb 	{ NEWFS,	"-B byteorder\tbyte order (`be' or `le')" },
    873  1.54    simonb 	{ NEWFS,	"-F \t\tcreate file system image in regular file" },
    874  1.58     lukem 	{ NEWFS,	"-I \t\tdo not check that the file system type is '4.2BSD'" },
    875  1.54    simonb 	{ BOTH,		"-N \t\tdo not create file system, just print out "
    876  1.54    simonb 			    "parameters" },
    877  1.64      fvdl 	{ NEWFS,	"-O N\t\tfilesystem format: 0 ==> 4.3BSD, 1 ==> FFS, 2==> UFS2" },
    878  1.54    simonb 	{ NEWFS,	"-S secsize\tsector size" },
    879  1.54    simonb #ifdef COMPAT
    880  1.54    simonb 	{ NEWFS,	"-T disktype\tdisk type" },
    881  1.54    simonb #endif
    882  1.54    simonb 	{ NEWFS,	"-Z \t\tpre-zero the image file (with -F)" },
    883  1.54    simonb 	{ BOTH,		"-a maxcontig\tmaximum contiguous blocks" },
    884  1.54    simonb 	{ BOTH,		"-b bsize\tblock size" },
    885  1.64      fvdl 	{ BOTH,		"-c blocks\tblocks per cylinder group" },
    886  1.64      fvdl 	{ BOTH,		"-d maxbsize\t maximum extent size" },
    887  1.54    simonb 	{ BOTH,		"-e maxbpg\tmaximum blocks per file in a cylinder group"
    888  1.54    simonb 			    },
    889  1.54    simonb 	{ BOTH,		"-f fsize\tfrag size" },
    890  1.54    simonb 	{ NEWFS,	"-g avgfilesize\taverage file size" },
    891  1.54    simonb 	{ MFS_MOUNT,	"-g groupname\tgroup name of mount point" },
    892  1.54    simonb 	{ BOTH,		"-h avgfpdir\taverage files per directory" },
    893  1.54    simonb 	{ BOTH,		"-i density\tnumber of bytes per inode" },
    894  1.54    simonb 	{ BOTH,		"-m minfree\tminimum free space %%" },
    895  1.54    simonb 	{ BOTH,		"-o optim\toptimization preference (`space' or `time')"
    896  1.54    simonb 			    },
    897  1.54    simonb 	{ MFS_MOUNT,	"-p perm\t\tpermissions (in octal)" },
    898  1.54    simonb 	{ BOTH,		"-s fssize\tfile system size (sectors)" },
    899  1.54    simonb 	{ MFS_MOUNT,	"-u username\tuser name of mount point" },
    900  1.61       dbj 	{ NEWFS,	"-v volname\tApple UFS volume name" },
    901  1.54    simonb 	{ 0, NULL }
    902  1.54    simonb };
    903  1.54    simonb 
    904  1.25  christos static void
    905  1.40    simonb usage(void)
    906   1.1       cgd {
    907  1.54    simonb 	int match;
    908  1.54    simonb 	const struct help_strings *hs;
    909  1.43     lukem 
    910   1.1       cgd 	if (mfs) {
    911   1.1       cgd 		fprintf(stderr,
    912  1.43     lukem 		    "usage: %s [ fsoptions ] special-device mount-point\n",
    913  1.42       cgd 			getprogname());
    914   1.1       cgd 	} else
    915   1.1       cgd 		fprintf(stderr,
    916  1.43     lukem 		    "usage: %s [ fsoptions ] special-device%s\n",
    917  1.42       cgd 		    getprogname(),
    918   1.1       cgd #ifdef COMPAT
    919   1.1       cgd 		    " [device-type]");
    920   1.1       cgd #else
    921   1.1       cgd 		    "");
    922   1.1       cgd #endif
    923   1.1       cgd 	fprintf(stderr, "where fsoptions are:\n");
    924  1.54    simonb 
    925  1.54    simonb 	match = mfs ? MFS_MOUNT : NEWFS;
    926  1.54    simonb 	for (hs = help_strings; hs->flags != 0; hs++)
    927  1.54    simonb 		if (hs->flags & match)
    928  1.54    simonb 			fprintf(stderr, "\t%s\n", hs->str);
    929   1.1       cgd 	exit(1);
    930   1.1       cgd }
    931