Home | History | Annotate | Line # | Download | only in newfs
newfs.c revision 1.75
      1  1.75       dsl /*	$NetBSD: newfs.c,v 1.75 2003/11/01 17:43:03 dsl 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.75       dsl __RCSID("$NetBSD: newfs.c,v 1.75 2003/11/01 17:43:03 dsl 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.75       dsl #include <stdint.h>
    116  1.75       dsl #include <limits.h>
    117  1.10   mycroft 
    118  1.10   mycroft #include "mntopts.h"
    119  1.25  christos #include "dkcksum.h"
    120  1.25  christos #include "extern.h"
    121  1.10   mycroft 
    122  1.10   mycroft struct mntopt mopts[] = {
    123  1.10   mycroft 	MOPT_STDOPTS,
    124  1.10   mycroft 	MOPT_ASYNC,
    125  1.22       cgd 	MOPT_UPDATE,
    126  1.60  christos 	MOPT_GETARGS,
    127  1.23       tls 	MOPT_NOATIME,
    128  1.10   mycroft 	{ NULL },
    129  1.10   mycroft };
    130  1.10   mycroft 
    131  1.40    simonb static struct disklabel *getdisklabel(char *, int);
    132  1.40    simonb static void rewritelabel(char *, int, struct disklabel *);
    133  1.54    simonb static gid_t mfs_group(const char *);
    134  1.54    simonb static uid_t mfs_user(const char *);
    135  1.75       dsl static int64_t strsuftoi64(const char *, const char *, int64_t, int64_t, int *);
    136  1.40    simonb static void usage(void);
    137  1.40    simonb int main(int, char *[]);
    138   1.1       cgd 
    139   1.1       cgd #define	COMPAT			/* allow non-labeled disks */
    140   1.1       cgd 
    141   1.1       cgd /*
    142   1.1       cgd  * The following two constants set the default block and fragment sizes.
    143   1.1       cgd  * Both constants must be a power of 2 and meet the following constraints:
    144   1.1       cgd  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
    145   1.1       cgd  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
    146   1.1       cgd  *	DESBLKSIZE / DESFRAGSIZE <= 8
    147   1.1       cgd  */
    148  1.53  augustss /*
    149  1.53  augustss  * For file systems smaller than SMALL_FSSIZE we use the S_DFL_* defaults,
    150  1.53  augustss  * otherwise if less than MEDIUM_FSSIZE use M_DFL_*, otherwise use
    151  1.53  augustss  * L_DFL_*.
    152  1.53  augustss  */
    153  1.54    simonb #define	SMALL_FSSIZE	(20*1024*2)
    154  1.53  augustss #define	S_DFL_FRAGSIZE	512
    155  1.54    simonb #define	MEDIUM_FSSIZE	(1000*1024*2)
    156  1.53  augustss #define	M_DFL_FRAGSIZE	1024
    157  1.53  augustss #define	L_DFL_FRAGSIZE	2048
    158  1.75       dsl #define	DFL_FRAG_BLK	8
    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  * MAXBLKPG determines the maximum number of data blocks which are
    174   1.1       cgd  * placed in a single cylinder group. The default is one indirect
    175   1.1       cgd  * block worth of data blocks.
    176   1.1       cgd  */
    177  1.64      fvdl #define	MAXBLKPG_UFS1(bsize)	((bsize) / sizeof(int32_t))
    178  1.64      fvdl #define	MAXBLKPG_UFS2(bsize)	((bsize) / sizeof(int64_t))
    179   1.1       cgd 
    180   1.1       cgd /*
    181   1.1       cgd  * Each file system has a number of inodes statically allocated.
    182   1.1       cgd  * We allocate one inode slot per NFPI fragments, expecting this
    183   1.1       cgd  * to be far more than we will ever need.
    184   1.1       cgd  */
    185   1.1       cgd #define	NFPI		4
    186   1.1       cgd 
    187   1.1       cgd 
    188   1.1       cgd int	mfs;			/* run as the memory based filesystem */
    189   1.1       cgd int	Nflag;			/* run without writing file system */
    190  1.64      fvdl int	Oflag = 1;		/* format as an 4.3BSD file system */
    191  1.64      fvdl int64_t	fssize;			/* file system size */
    192   1.1       cgd int	sectorsize;		/* bytes/sector */
    193   1.1       cgd int	fsize = 0;		/* fragment size */
    194   1.1       cgd int	bsize = 0;		/* block size */
    195  1.64      fvdl int	maxbsize = 0;		/* maximum clustering */
    196   1.1       cgd int	minfree = MINFREE;	/* free space threshold */
    197   1.1       cgd int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
    198   1.1       cgd int	density;		/* number of bytes per inode */
    199  1.70       dsl int	num_inodes;		/* number of inoder (overrides density) */
    200  1.36   mycroft int	maxcontig = 0;		/* max contiguous blocks to allocate */
    201   1.1       cgd int	maxbpg;			/* maximum blocks per file in a cyl group */
    202  1.47     lukem int	avgfilesize = AVFILESIZ;/* expected average file size */
    203  1.47     lukem int	avgfpdir = AFPDIR;	/* expected number of files per directory */
    204  1.10   mycroft int	mntflags = MNT_ASYNC;	/* flags to be passed to mount */
    205   1.1       cgd u_long	memleft;		/* virtual memory available */
    206   1.1       cgd caddr_t	membase;		/* start address of memory based filesystem */
    207  1.43     lukem int	needswap;		/* Filesystem not in native byte order */
    208   1.1       cgd #ifdef COMPAT
    209   1.1       cgd char	*disktype;
    210   1.1       cgd int	unlabeled;
    211   1.1       cgd #endif
    212  1.61       dbj char *appleufs_volname = 0; /* Apple UFS volume name */
    213  1.61       dbj int isappleufs = 0;
    214   1.1       cgd 
    215   1.1       cgd char	device[MAXPATHLEN];
    216   1.1       cgd 
    217  1.10   mycroft int
    218  1.40    simonb main(int argc, char *argv[])
    219   1.1       cgd {
    220  1.75       dsl 	struct partition *pp = NULL;
    221  1.75       dsl 	struct disklabel *lp = NULL;
    222   1.1       cgd 	struct partition oldpartition;
    223  1.10   mycroft 	struct statfs *mp;
    224  1.75       dsl 	struct stat sb;
    225  1.75       dsl 	int ch, fsi, fso, len, n, Fflag, Iflag, Zflag;
    226  1.75       dsl 	uint ptn = 0;	/* gcc -Wuninitialised */
    227  1.75       dsl 	char *cp, *s1, *s2, *special;
    228  1.43     lukem 	const char *opstring;
    229  1.75       dsl 	int byte_sized = 0;
    230  1.30  drochner #ifdef MFS
    231  1.30  drochner 	char mountfromname[100];
    232  1.30  drochner 	pid_t pid, res;
    233  1.30  drochner 	struct statfs sf;
    234  1.56     lukem 	int status;
    235  1.56     lukem #endif
    236  1.75       dsl 	mode_t mfsmode = 01777;	/* default mode for a /tmp-type directory */
    237  1.75       dsl 	uid_t mfsuid = 0;	/* user root */
    238  1.75       dsl 	gid_t mfsgid = 0;	/* group wheel */
    239   1.1       cgd 
    240  1.43     lukem 	cp = NULL;
    241  1.43     lukem 	fsi = fso = -1;
    242  1.58     lukem 	Fflag = Iflag = Zflag = 0;
    243  1.42       cgd 	if (strstr(getprogname(), "mfs")) {
    244   1.1       cgd 		mfs = 1;
    245   1.1       cgd 		Nflag++;
    246   1.1       cgd 	}
    247   1.1       cgd 
    248  1.10   mycroft 	opstring = mfs ?
    249  1.75       dsl 	    "NT:a:b:c:d:e:f:g:h:i:m:n:o:p:s:u:" :
    250  1.75       dsl 	    "B:FINO:S:T:Za:b:c:d:e:f:g:h:i:l:m:n:o:p:r:s:u:v:";
    251  1.26     lukem 	while ((ch = getopt(argc, argv, opstring)) != -1)
    252  1.10   mycroft 		switch (ch) {
    253  1.33    bouyer 		case 'B':
    254  1.33    bouyer 			if (strcmp(optarg, "be") == 0) {
    255  1.33    bouyer #if BYTE_ORDER == LITTLE_ENDIAN
    256  1.33    bouyer 				needswap = 1;
    257  1.33    bouyer #endif
    258  1.33    bouyer 			} else if (strcmp(optarg, "le") == 0) {
    259  1.33    bouyer #if BYTE_ORDER == BIG_ENDIAN
    260  1.33    bouyer 				needswap = 1;
    261  1.33    bouyer #endif
    262  1.33    bouyer 			} else
    263  1.33    bouyer 				usage();
    264  1.33    bouyer 			break;
    265  1.43     lukem 		case 'F':
    266  1.43     lukem 			Fflag = 1;
    267  1.43     lukem 			break;
    268  1.58     lukem 		case 'I':
    269  1.58     lukem 			Iflag = 1;
    270  1.58     lukem 			break;
    271  1.10   mycroft 		case 'N':
    272  1.10   mycroft 			Nflag = 1;
    273   1.1       cgd 			break;
    274  1.10   mycroft 		case 'O':
    275  1.75       dsl 			Oflag = strsuftoi64("format", optarg, 0, 2, NULL);
    276   1.1       cgd 			break;
    277   1.1       cgd 		case 'S':
    278  1.75       dsl 			sectorsize = strsuftoi64("sector size",
    279  1.75       dsl 			    optarg, 1, INT_MAX, NULL);
    280   1.1       cgd 			break;
    281   1.1       cgd #ifdef COMPAT
    282   1.1       cgd 		case 'T':
    283   1.1       cgd 			disktype = optarg;
    284   1.1       cgd 			break;
    285   1.1       cgd #endif
    286  1.43     lukem 		case 'Z':
    287  1.43     lukem 			Zflag = 1;
    288  1.43     lukem 			break;
    289   1.1       cgd 		case 'a':
    290  1.75       dsl 			maxcontig = strsuftoi64("maximum contiguous blocks",
    291  1.75       dsl 			    optarg, 1, INT_MAX, NULL);
    292   1.1       cgd 			break;
    293   1.1       cgd 		case 'b':
    294  1.75       dsl 			bsize = strsuftoi64("block size",
    295  1.75       dsl 			    optarg, MINBSIZE, MAXBSIZE, NULL);
    296   1.1       cgd 			break;
    297  1.68       dsl 		case 'c':	/* was cylinders per group... */
    298   1.1       cgd 			break;
    299   1.1       cgd 		case 'd':
    300  1.75       dsl 			maxbsize = strsuftoi64("maximum extent size",
    301  1.75       dsl 			    optarg, 0, INT_MAX, NULL);
    302   1.1       cgd 			break;
    303   1.1       cgd 		case 'e':
    304  1.75       dsl 			maxbpg = strsuftoi64(
    305  1.43     lukem 			    "blocks per file in a cylinder group",
    306  1.75       dsl 			    optarg, 1, INT_MAX, NULL);
    307   1.1       cgd 			break;
    308   1.1       cgd 		case 'f':
    309  1.75       dsl 			fsize = strsuftoi64("fragment size",
    310  1.75       dsl 			    optarg, 1, MAXBSIZE, NULL);
    311   1.1       cgd 			break;
    312  1.47     lukem 		case 'g':
    313  1.54    simonb 			if (mfs)
    314  1.54    simonb 				mfsgid = mfs_group(optarg);
    315  1.54    simonb 			else {
    316  1.75       dsl 				avgfilesize = strsuftoi64("average file size",
    317  1.75       dsl 				    optarg, 1, INT_MAX, NULL);
    318  1.54    simonb 			}
    319  1.47     lukem 			break;
    320  1.47     lukem 		case 'h':
    321  1.75       dsl 			avgfpdir = strsuftoi64("expected files per directory",
    322  1.75       dsl 			    optarg, 1, INT_MAX, NULL);
    323  1.47     lukem 			break;
    324   1.1       cgd 		case 'i':
    325  1.75       dsl 			density = strsuftoi64("bytes per inode",
    326  1.75       dsl 			    optarg, 1, INT_MAX, NULL);
    327   1.1       cgd 			break;
    328   1.1       cgd 		case 'm':
    329  1.75       dsl 			minfree = strsuftoi64("free space %",
    330  1.75       dsl 			    optarg, 0, 99, NULL);
    331   1.1       cgd 			break;
    332  1.70       dsl 		case 'n':
    333  1.75       dsl 			num_inodes = strsuftoi64("number of inodes",
    334  1.75       dsl 			    optarg, 1, INT_MAX, NULL);
    335  1.70       dsl 			break;
    336   1.1       cgd 		case 'o':
    337  1.10   mycroft 			if (mfs)
    338  1.27     lukem 				getmntopts(optarg, mopts, &mntflags, 0);
    339  1.10   mycroft 			else {
    340  1.10   mycroft 				if (strcmp(optarg, "space") == 0)
    341  1.10   mycroft 					opt = FS_OPTSPACE;
    342  1.10   mycroft 				else if (strcmp(optarg, "time") == 0)
    343  1.10   mycroft 					opt = FS_OPTTIME;
    344  1.10   mycroft 				else
    345  1.25  christos 				    errx(1, "%s %s",
    346  1.25  christos 					"unknown optimization preference: ",
    347  1.25  christos 					"use `space' or `time'.");
    348  1.10   mycroft 			}
    349   1.1       cgd 			break;
    350   1.1       cgd 		case 'p':
    351  1.54    simonb 			if (mfs) {
    352  1.54    simonb 				if ((mfsmode = strtol(optarg, NULL, 8)) <= 0)
    353  1.54    simonb 					errx(1, "bad mode `%s'", optarg);
    354  1.64      fvdl 			} else
    355  1.64      fvdl 				errx(1, "unknown option 'p'");
    356   1.1       cgd 			break;
    357   1.1       cgd 		case 's':
    358  1.75       dsl 			fssize = strsuftoi64("file system size",
    359  1.75       dsl 			    optarg, INT64_MIN, INT64_MAX, &byte_sized);
    360   1.1       cgd 			break;
    361   1.1       cgd 		case 'u':
    362  1.54    simonb 			if (mfs)
    363  1.54    simonb 				mfsuid = mfs_user(optarg);
    364  1.64      fvdl 			else
    365  1.64      fvdl 				errx(1, "deprecated option 'u'");
    366   1.1       cgd 			break;
    367  1.61       dbj 		case 'v':
    368  1.61       dbj 			appleufs_volname = optarg;
    369  1.61       dbj 			if (strchr(appleufs_volname, ':') || strchr(appleufs_volname, '/'))
    370  1.61       dbj 				errx(1,"Apple UFS volume name cannot contain ':' or '/'");
    371  1.61       dbj 			if (appleufs_volname[0] == '\0')
    372  1.61       dbj 				errx(1,"Apple UFS volume name cannot be zero length");
    373  1.61       dbj 			isappleufs = 1;
    374  1.61       dbj 			break;
    375   1.1       cgd 		case '?':
    376   1.1       cgd 		default:
    377   1.1       cgd 			usage();
    378   1.1       cgd 		}
    379   1.1       cgd 	argc -= optind;
    380   1.1       cgd 	argv += optind;
    381  1.60  christos 	if (mntflags & MNT_GETARGS)
    382  1.60  christos 		goto doit;
    383   1.1       cgd 
    384   1.1       cgd 	if (argc != 2 && (mfs || argc != 1))
    385   1.1       cgd 		usage();
    386   1.1       cgd 
    387  1.75       dsl 	memset(&oldpartition, 0, sizeof oldpartition);
    388  1.75       dsl 	memset(&sb, 0, sizeof sb);
    389   1.1       cgd 	special = argv[0];
    390  1.49     lukem 	if (Fflag || mfs) {
    391  1.14       cgd 		/*
    392  1.75       dsl 		 * It's a file system image or an MFS,
    393  1.75       dsl 		 * no label, use fixed default for sectorsize.
    394  1.14       cgd 		 */
    395  1.75       dsl 		if (sectorsize == 0)
    396  1.43     lukem 			sectorsize = DFL_SECSIZE;
    397  1.43     lukem 
    398  1.75       dsl 		if (!mfs) {	/* creating image in a regular file */
    399  1.75       dsl 			int fl;
    400  1.75       dsl 			if (Nflag)
    401  1.75       dsl 				fl = O_RDONLY;
    402  1.75       dsl 			else {
    403  1.75       dsl 				if (fssize > 0)
    404  1.75       dsl 					fl = O_RDWR | O_CREAT;
    405  1.75       dsl 				else
    406  1.75       dsl 					fl = O_RDWR;
    407  1.75       dsl 			}
    408  1.75       dsl 			fsi = open(special, fl, 0777);
    409  1.75       dsl 			if (fsi == -1)
    410  1.43     lukem 				err(1, "can't open file %s", special);
    411  1.75       dsl 			if (fstat(fsi, &sb) == -1)
    412  1.75       dsl 				err(1, "can't fstat opened %s", special);
    413  1.75       dsl 			if (!Nflag)
    414  1.75       dsl 				fso = fsi;
    415  1.43     lukem 		}
    416  1.49     lukem 	} else {	/* !Fflag && !mfs */
    417  1.49     lukem 		fsi = opendisk(special, O_RDONLY, device, sizeof(device), 0);
    418  1.49     lukem 		special = device;
    419  1.75       dsl 		if (fsi < 0 || fstat(fsi, &sb) == -1)
    420  1.49     lukem 			err(1, "%s: open for read", special);
    421  1.49     lukem 
    422  1.75       dsl 		if (!Nflag) {
    423  1.75       dsl 			fso = open(special, O_WRONLY, 0);
    424  1.49     lukem 			if (fso < 0)
    425  1.49     lukem 				err(1, "%s: open for write", special);
    426  1.49     lukem 
    427  1.49     lukem 			/* Bail if target special is mounted */
    428  1.49     lukem 			n = getmntinfo(&mp, MNT_NOWAIT);
    429  1.49     lukem 			if (n == 0)
    430  1.49     lukem 				err(1, "%s: getmntinfo", special);
    431  1.49     lukem 
    432  1.49     lukem 			len = sizeof(_PATH_DEV) - 1;
    433  1.49     lukem 			s1 = special;
    434  1.49     lukem 			if (strncmp(_PATH_DEV, s1, len) == 0)
    435  1.49     lukem 				s1 += len;
    436  1.49     lukem 
    437  1.49     lukem 			while (--n >= 0) {
    438  1.49     lukem 				s2 = mp->f_mntfromname;
    439  1.49     lukem 				if (strncmp(_PATH_DEV, s2, len) == 0) {
    440  1.49     lukem 					s2 += len - 1;
    441  1.49     lukem 					*s2 = 'r';
    442  1.49     lukem 				}
    443  1.49     lukem 				if (strcmp(s1, s2) == 0 ||
    444  1.49     lukem 				    strcmp(s1, &s2[1]) == 0)
    445  1.49     lukem 					errx(1, "%s is mounted on %s",
    446  1.49     lukem 					    special, mp->f_mntonname);
    447  1.49     lukem 				++mp;
    448  1.10   mycroft 			}
    449  1.10   mycroft 		}
    450  1.75       dsl 
    451   1.1       cgd #ifdef COMPAT
    452  1.49     lukem 		if (disktype == NULL)
    453  1.10   mycroft 			disktype = argv[1];
    454   1.1       cgd #endif
    455  1.10   mycroft 		lp = getdisklabel(special, fsi);
    456  1.75       dsl 		if (sectorsize == 0) {
    457  1.75       dsl 			sectorsize = lp->d_secsize;
    458  1.75       dsl 			if (sectorsize <= 0)
    459  1.75       dsl 				errx(1, "no default sector size");
    460  1.75       dsl 		}
    461  1.75       dsl 
    462  1.75       dsl 		ptn = strchr(special, '\0')[-1] - 'a';
    463  1.75       dsl 		if (ptn < lp->d_npartitions && ptn == DISKPART(sb.st_rdev)) {
    464  1.75       dsl 			/* Assume partition really does match the label */
    465  1.75       dsl 			pp = &lp->d_partitions[ptn];
    466  1.75       dsl 			oldpartition = *pp;
    467  1.75       dsl 			if (pp->p_size == 0)
    468  1.75       dsl 				errx(1, "`%c' partition is unavailable",
    469  1.75       dsl 					'a' + ptn);
    470  1.75       dsl 			if (pp->p_fstype == FS_APPLEUFS)
    471  1.75       dsl 				isappleufs = 1;
    472  1.75       dsl 			if (!Iflag) {
    473  1.75       dsl 				if (isappleufs) {
    474  1.75       dsl 					if (pp->p_fstype != FS_APPLEUFS)
    475  1.75       dsl 						errx(1, "`%c' partition type is not `Apple UFS'", 'a' + ptn);
    476  1.75       dsl 				} else {
    477  1.75       dsl 					if (pp->p_fstype != FS_BSDFFS)
    478  1.75       dsl 						errx(1, "`%c' partition type is not `4.2BSD'", 'a' + ptn);
    479  1.75       dsl 				}
    480  1.75       dsl 			}
    481  1.61       dbj 		}
    482  1.49     lukem 	}	/* !Fflag && !mfs */
    483  1.43     lukem 
    484  1.75       dsl 	if (byte_sized)
    485  1.75       dsl 		fssize /= sectorsize;
    486  1.75       dsl 	if (fssize <= 0) {
    487  1.75       dsl 		if (sb.st_size != 0)
    488  1.75       dsl 			fssize += sb.st_size / sectorsize;
    489  1.75       dsl 		else
    490  1.75       dsl 			fssize += oldpartition.p_size;
    491  1.75       dsl 		if (fssize <= 0)
    492  1.75       dsl 			errx(1, "Unable to determine file system size");
    493  1.75       dsl 	}
    494  1.75       dsl 
    495  1.75       dsl 	if (pp != NULL && fssize > pp->p_size)
    496  1.75       dsl 		errx(1, "maximum file system size on `%s' is %d sectors",
    497  1.75       dsl 		    special, pp->p_size);
    498  1.75       dsl 
    499  1.75       dsl 	/* XXXLUKEM: only ftruncate() regular files ? (dsl: or at all?) */
    500  1.75       dsl 	if (Fflag && fso != -1
    501  1.75       dsl 	    && ftruncate(fso, (off_t)fssize * sectorsize) == -1)
    502  1.75       dsl 		err(1, "can't ftruncate %s to %" PRId64, special, fssize);
    503  1.75       dsl 
    504  1.75       dsl 	if (Zflag && fso != -1) {	/* pre-zero (and de-sparce) the file */
    505  1.75       dsl 		char	*buf;
    506  1.75       dsl 		int	bufsize, i;
    507  1.75       dsl 		off_t	bufrem;
    508  1.75       dsl 		struct statfs sfs;
    509  1.75       dsl 
    510  1.75       dsl 		if (fstatfs(fso, &sfs) == -1) {
    511  1.75       dsl 			warn("can't fstatfs `%s'", special);
    512  1.75       dsl 			bufsize = 8192;
    513  1.75       dsl 		} else
    514  1.75       dsl 			bufsize = sfs.f_iosize;
    515  1.75       dsl 
    516  1.75       dsl 		if ((buf = calloc(1, bufsize)) == NULL)
    517  1.75       dsl 			err(1, "can't malloc buffer of %d",
    518  1.75       dsl 			bufsize);
    519  1.75       dsl 		bufrem = fssize * sectorsize;
    520  1.75       dsl 		printf(
    521  1.75       dsl     "Creating file system image in `%s', size %lld bytes, in %d byte chunks.\n",
    522  1.75       dsl 		    special, (long long)bufrem, bufsize);
    523  1.75       dsl 		while (bufrem > 0) {
    524  1.75       dsl 			i = write(fso, buf, MIN(bufsize, bufrem));
    525  1.75       dsl 			if (i == -1)
    526  1.75       dsl 				err(1, "writing image");
    527  1.75       dsl 			bufrem -= i;
    528  1.75       dsl 		}
    529  1.75       dsl 		free(buf);
    530  1.53  augustss 	}
    531  1.53  augustss 
    532  1.75       dsl 	/* Sort out fragment and block sizes */
    533   1.1       cgd 	if (fsize == 0) {
    534  1.75       dsl 		fsize = bsize / DFL_FRAG_BLK;
    535  1.75       dsl 		if (fsize == 0)
    536  1.75       dsl 			fsize = oldpartition.p_fsize;
    537  1.75       dsl 		if (fsize <= 0) {
    538  1.75       dsl 			if (isappleufs) {
    539  1.75       dsl 				fsize = APPLEUFS_DFL_FRAGSIZE;
    540  1.75       dsl 			} else {
    541  1.75       dsl 				if (fssize < SMALL_FSSIZE)
    542  1.75       dsl 					fsize = S_DFL_FRAGSIZE;
    543  1.75       dsl 				else if (fssize < MEDIUM_FSSIZE)
    544  1.75       dsl 					fsize = M_DFL_FRAGSIZE;
    545  1.75       dsl 				else
    546  1.75       dsl 					fsize = L_DFL_FRAGSIZE;
    547  1.75       dsl 				if (fsize < sectorsize)
    548  1.75       dsl 					fsize = sectorsize;
    549  1.75       dsl 			}
    550  1.75       dsl 		}
    551   1.1       cgd 	}
    552   1.1       cgd 	if (bsize == 0) {
    553  1.75       dsl 		bsize = oldpartition.p_frag * oldpartition.p_fsize;
    554  1.75       dsl 		if (bsize <= 0) {
    555  1.75       dsl 			if (isappleufs)
    556  1.75       dsl 				bsize = APPLEUFS_DFL_BLKSIZE;
    557  1.75       dsl 			else
    558  1.75       dsl 				bsize = DFL_FRAG_BLK * fsize;
    559  1.75       dsl 		}
    560   1.1       cgd 	}
    561  1.65       dbj 
    562  1.65       dbj 	if (isappleufs && (fsize < APPLEUFS_DFL_FRAGSIZE)) {
    563  1.65       dbj 		warnx("Warning: chosen fsize of %d is less than Apple UFS minimum of %d",
    564  1.75       dsl 			fsize, APPLEUFS_DFL_FRAGSIZE);
    565  1.65       dbj 	}
    566  1.65       dbj 	if (isappleufs && (bsize > APPLEUFS_DFL_BLKSIZE)) {
    567  1.65       dbj 		warnx("Warning: chosen bsize of %d is greater than Apple UFS maximum of %d",
    568  1.75       dsl 			bsize, APPLEUFS_DFL_BLKSIZE);
    569  1.65       dbj 	}
    570  1.65       dbj 
    571  1.10   mycroft 	/*
    572  1.10   mycroft 	 * Maxcontig sets the default for the maximum number of blocks
    573  1.10   mycroft 	 * that may be allocated sequentially. With filesystem clustering
    574  1.10   mycroft 	 * it is possible to allocate contiguous blocks up to the maximum
    575  1.10   mycroft 	 * transfer size permitted by the controller or buffering.
    576  1.10   mycroft 	 */
    577  1.10   mycroft 	if (maxcontig == 0)
    578  1.27     lukem 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
    579   1.1       cgd 	if (density == 0)
    580   1.1       cgd 		density = NFPI * fsize;
    581   1.8       cgd 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
    582  1.25  christos 		warnx("%s %s %d%%", "Warning: changing optimization to space",
    583  1.25  christos 		    "because minfree is less than", MINFREE);
    584   1.1       cgd 		opt = FS_OPTSPACE;
    585   1.1       cgd 	}
    586  1.64      fvdl 	if (maxbpg == 0) {
    587  1.64      fvdl 		if (Oflag <= 1)
    588  1.64      fvdl 			maxbpg = MAXBLKPG_UFS1(bsize);
    589  1.64      fvdl 		else
    590  1.64      fvdl 			maxbpg = MAXBLKPG_UFS2(bsize);
    591  1.64      fvdl 	}
    592  1.54    simonb 	mkfs(pp, special, fsi, fso, mfsmode, mfsuid, mfsgid);
    593  1.75       dsl 	if (!Nflag && pp != NULL
    594  1.75       dsl 	    && memcmp(pp, &oldpartition, sizeof(oldpartition)))
    595   1.1       cgd 		rewritelabel(special, fso, lp);
    596  1.75       dsl 	if (fsi != -1 && fsi != fso)
    597  1.75       dsl 		close(fsi);
    598  1.75       dsl 	if (fso != -1)
    599   1.1       cgd 		close(fso);
    600   1.1       cgd #ifdef MFS
    601   1.1       cgd 	if (mfs) {
    602   1.1       cgd 		struct mfs_args args;
    603   1.1       cgd 
    604  1.30  drochner 		switch (pid = fork()) {
    605  1.30  drochner 		case -1:
    606  1.30  drochner 			perror("mfs");
    607  1.30  drochner 			exit(10);
    608  1.30  drochner 		case 0:
    609  1.34   mycroft 			(void)snprintf(mountfromname, sizeof(mountfromname),
    610  1.34   mycroft 			    "mfs:%d", getpid());
    611  1.30  drochner 			break;
    612  1.30  drochner 		default:
    613  1.34   mycroft 			(void)snprintf(mountfromname, sizeof(mountfromname),
    614  1.34   mycroft 			    "mfs:%d", pid);
    615  1.30  drochner 			for (;;) {
    616  1.30  drochner 				/*
    617  1.30  drochner 				 * spin until the mount succeeds
    618  1.30  drochner 				 * or the child exits
    619  1.30  drochner 				 */
    620  1.30  drochner 				usleep(1);
    621  1.30  drochner 
    622  1.30  drochner 				/*
    623  1.30  drochner 				 * XXX Here is a race condition: another process
    624  1.30  drochner 				 * can mount a filesystem which hides our
    625  1.30  drochner 				 * ramdisk before we see the success.
    626  1.30  drochner 				 */
    627  1.30  drochner 				if (statfs(argv[1], &sf) < 0)
    628  1.30  drochner 					err(88, "statfs %s", argv[1]);
    629  1.30  drochner 				if (!strcmp(sf.f_mntfromname, mountfromname) &&
    630  1.30  drochner 				    !strncmp(sf.f_mntonname, argv[1],
    631  1.30  drochner 					     MNAMELEN) &&
    632  1.30  drochner 				    !strcmp(sf.f_fstypename, "mfs"))
    633  1.30  drochner 					exit(0);
    634  1.30  drochner 
    635  1.30  drochner 				res = waitpid(pid, &status, WNOHANG);
    636  1.30  drochner 				if (res == -1)
    637  1.30  drochner 					err(11, "waitpid");
    638  1.30  drochner 				if (res != pid)
    639  1.30  drochner 					continue;
    640  1.31  drochner 				if (WIFEXITED(status)) {
    641  1.31  drochner 					if (WEXITSTATUS(status) == 0)
    642  1.31  drochner 						exit(0);
    643  1.30  drochner 					errx(1, "%s: mount: %s", argv[1],
    644  1.30  drochner 					     strerror(WEXITSTATUS(status)));
    645  1.31  drochner 				} else
    646  1.30  drochner 					errx(11, "abnormal termination");
    647  1.30  drochner 			}
    648  1.30  drochner 			/* NOTREACHED */
    649  1.30  drochner 		}
    650  1.30  drochner 
    651  1.30  drochner 		(void) setsid();
    652  1.30  drochner 		(void) close(0);
    653  1.30  drochner 		(void) close(1);
    654  1.30  drochner 		(void) close(2);
    655  1.30  drochner 		(void) chdir("/");
    656  1.30  drochner 
    657  1.30  drochner 		args.fspec = mountfromname;
    658  1.10   mycroft 		args.export.ex_root = -2;
    659  1.10   mycroft 		if (mntflags & MNT_RDONLY)
    660  1.10   mycroft 			args.export.ex_flags = MNT_EXRDONLY;
    661  1.10   mycroft 		else
    662  1.10   mycroft 			args.export.ex_flags = 0;
    663   1.1       cgd 		args.base = membase;
    664   1.1       cgd 		args.size = fssize * sectorsize;
    665  1.60  christos doit:
    666  1.60  christos 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0) {
    667  1.60  christos 			if (mntflags & MNT_GETARGS)
    668  1.60  christos 				err(1, "mount `%s' failed", argv[1]);
    669  1.30  drochner 			exit(errno); /* parent prints message */
    670  1.60  christos 		}
    671  1.60  christos 		if (mntflags & MNT_GETARGS)
    672  1.60  christos 			printf("base=%p, size=%ld\n", args.base, args.size);
    673   1.1       cgd 	}
    674   1.1       cgd #endif
    675   1.1       cgd 	exit(0);
    676   1.1       cgd }
    677   1.1       cgd 
    678   1.1       cgd #ifdef COMPAT
    679  1.39        is const char lmsg[] = "%s: can't read disk label; disk type must be specified";
    680   1.1       cgd #else
    681  1.39        is const char lmsg[] = "%s: can't read disk label";
    682   1.1       cgd #endif
    683   1.1       cgd 
    684  1.25  christos static struct disklabel *
    685  1.75       dsl getdisklabel(char *s, int fd)
    686   1.1       cgd {
    687   1.1       cgd 	static struct disklabel lab;
    688   1.1       cgd 
    689  1.24       tls 	if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
    690   1.1       cgd #ifdef COMPAT
    691   1.1       cgd 		if (disktype) {
    692  1.25  christos 			struct disklabel *lp;
    693   1.1       cgd 
    694   1.1       cgd 			unlabeled++;
    695   1.1       cgd 			lp = getdiskbyname(disktype);
    696   1.1       cgd 			if (lp == NULL)
    697  1.25  christos 				errx(1, "%s: unknown disk type", disktype);
    698   1.1       cgd 			return (lp);
    699   1.1       cgd 		}
    700   1.1       cgd #endif
    701  1.10   mycroft 		warn("ioctl (GDINFO)");
    702  1.25  christos 		errx(1, lmsg, s);
    703   1.1       cgd 	}
    704   1.1       cgd 	return (&lab);
    705   1.1       cgd }
    706   1.1       cgd 
    707  1.25  christos static void
    708  1.75       dsl rewritelabel(char *s, int fd, struct disklabel *lp)
    709   1.1       cgd {
    710   1.1       cgd #ifdef COMPAT
    711   1.1       cgd 	if (unlabeled)
    712   1.1       cgd 		return;
    713   1.1       cgd #endif
    714   1.1       cgd 	lp->d_checksum = 0;
    715   1.1       cgd 	lp->d_checksum = dkcksum(lp);
    716   1.1       cgd 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    717  1.57   thorpej 		if (errno == ESRCH)
    718  1.57   thorpej 			return;
    719  1.10   mycroft 		warn("ioctl (WDINFO)");
    720  1.25  christos 		errx(1, "%s: can't rewrite disk label", s);
    721   1.1       cgd 	}
    722  1.35      matt #if __vax__
    723   1.1       cgd 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    724  1.25  christos 		int i;
    725   1.1       cgd 		int cfd;
    726   1.1       cgd 		daddr_t alt;
    727  1.62       scw 		off_t loff;
    728   1.1       cgd 		char specname[64];
    729   1.1       cgd 		char blk[1024];
    730   1.1       cgd 		char *cp;
    731   1.1       cgd 
    732   1.1       cgd 		/*
    733   1.1       cgd 		 * Make name for 'c' partition.
    734   1.1       cgd 		 */
    735  1.66    itojun 		strlcpy(specname, s, sizeof(specname));
    736   1.1       cgd 		cp = specname + strlen(specname) - 1;
    737   1.1       cgd 		if (!isdigit(*cp))
    738   1.1       cgd 			*cp = 'c';
    739   1.1       cgd 		cfd = open(specname, O_WRONLY);
    740   1.1       cgd 		if (cfd < 0)
    741  1.28     enami 			err(1, "%s: open", specname);
    742  1.62       scw 		if ((loff = getlabeloffset()) < 0)
    743  1.62       scw 			err(1, "getlabeloffset()");
    744  1.11   mycroft 		memset(blk, 0, sizeof(blk));
    745  1.62       scw 		*(struct disklabel *)(blk + loff) = *lp;
    746   1.1       cgd 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    747   1.1       cgd 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    748  1.17       cgd 			off_t offset;
    749  1.17       cgd 
    750  1.17       cgd 			offset = alt + i;
    751  1.17       cgd 			offset *= lp->d_secsize;
    752  1.17       cgd 			if (lseek(cfd, offset, SEEK_SET) == -1)
    753  1.25  christos 				err(1, "lseek to badsector area: ");
    754   1.1       cgd 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    755  1.10   mycroft 				warn("alternate label %d write", i/2);
    756   1.1       cgd 		}
    757   1.1       cgd 		close(cfd);
    758   1.1       cgd 	}
    759   1.1       cgd #endif
    760   1.1       cgd }
    761   1.1       cgd 
    762  1.54    simonb static gid_t
    763  1.54    simonb mfs_group(const char *gname)
    764  1.54    simonb {
    765  1.54    simonb 	struct group *gp;
    766  1.54    simonb 
    767  1.54    simonb 	if (!(gp = getgrnam(gname)) && !isdigit((unsigned char)*gname))
    768  1.54    simonb 		errx(1, "unknown gname %s", gname);
    769  1.54    simonb 	return gp ? gp->gr_gid : atoi(gname);
    770  1.54    simonb }
    771  1.54    simonb 
    772  1.54    simonb static uid_t
    773  1.54    simonb mfs_user(const char *uname)
    774  1.54    simonb {
    775  1.54    simonb 	struct passwd *pp;
    776  1.54    simonb 
    777  1.54    simonb 	if (!(pp = getpwnam(uname)) && !isdigit((unsigned char)*uname))
    778  1.54    simonb 		errx(1, "unknown user %s", uname);
    779  1.54    simonb 	return pp ? pp->pw_uid : atoi(uname);
    780  1.54    simonb }
    781  1.54    simonb 
    782  1.75       dsl static int64_t
    783  1.75       dsl strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max, int *num_suffix)
    784  1.43     lukem {
    785  1.75       dsl 	int64_t result, r1;
    786  1.75       dsl 	int shift = 0;
    787  1.43     lukem 	char	*ep;
    788  1.43     lukem 
    789  1.43     lukem 	errno = 0;
    790  1.75       dsl 	r1 = strtoll(arg, &ep, 10);
    791  1.43     lukem 	if (ep[0] != '\0' && ep[1] != '\0')
    792  1.43     lukem 		errx(1, "%s `%s' is not a valid number.", desc, arg);
    793  1.75       dsl 	switch (ep[0]) {
    794  1.43     lukem 	case '\0':
    795  1.75       dsl 	case 's': case 'S':
    796  1.75       dsl 		if (num_suffix != NULL)
    797  1.75       dsl 			*num_suffix = 0;
    798  1.43     lukem 		break;
    799  1.75       dsl 	case 'g': case 'G':
    800  1.75       dsl 		shift += 10;
    801  1.75       dsl 		/* FALLTHROUGH */
    802  1.75       dsl 	case 'm': case 'M':
    803  1.75       dsl 		shift += 10;
    804  1.75       dsl 		/* FALLTHROUGH */
    805  1.75       dsl 	case 'k': case 'K':
    806  1.75       dsl 		shift += 10;
    807  1.75       dsl 		/* FALLTHROUGH */
    808  1.75       dsl 	case 'b': case 'B':
    809  1.75       dsl 		if (num_suffix != NULL)
    810  1.75       dsl 			*num_suffix = 1;
    811  1.43     lukem 		break;
    812  1.43     lukem 	default:
    813  1.43     lukem 		errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
    814  1.43     lukem 	}
    815  1.75       dsl 	result = r1 << shift;
    816  1.75       dsl 	if (errno == ERANGE || result >> shift != r1)
    817  1.75       dsl 		errx(1, "%s `%s' is too large to convert.", desc, arg);
    818  1.43     lukem 	if (result < min)
    819  1.75       dsl 		errx(1, "%s `%s' (%" PRId64 ") is less than the minimum (%" PRId64 ").",
    820  1.43     lukem 		    desc, arg, result, min);
    821  1.43     lukem 	if (result > max)
    822  1.75       dsl 		errx(1, "%s `%s' (%" PRId64 ") is greater than the maximum (%" PRId64 ").",
    823  1.43     lukem 		    desc, arg, result, max);
    824  1.75       dsl 	return result;
    825  1.43     lukem }
    826  1.43     lukem 
    827  1.54    simonb #define	NEWFS		1
    828  1.54    simonb #define	MFS_MOUNT	2
    829  1.54    simonb #define	BOTH		NEWFS | MFS_MOUNT
    830  1.54    simonb 
    831  1.54    simonb struct help_strings {
    832  1.54    simonb 	int flags;
    833  1.54    simonb 	const char *str;
    834  1.54    simonb } const help_strings[] = {
    835  1.54    simonb 	{ NEWFS,	"-B byteorder\tbyte order (`be' or `le')" },
    836  1.54    simonb 	{ NEWFS,	"-F \t\tcreate file system image in regular file" },
    837  1.58     lukem 	{ NEWFS,	"-I \t\tdo not check that the file system type is '4.2BSD'" },
    838  1.54    simonb 	{ BOTH,		"-N \t\tdo not create file system, just print out "
    839  1.54    simonb 			    "parameters" },
    840  1.75       dsl 	{ NEWFS,	"-O N\t\tfilesystem format: 0 ==> 4.3BSD, 1 ==> FFS, 2 ==> UFS2" },
    841  1.54    simonb 	{ NEWFS,	"-S secsize\tsector size" },
    842  1.54    simonb #ifdef COMPAT
    843  1.54    simonb 	{ NEWFS,	"-T disktype\tdisk type" },
    844  1.54    simonb #endif
    845  1.75       dsl 	{ NEWFS,	"-Z \t\tpre-zero the image file" },
    846  1.54    simonb 	{ BOTH,		"-a maxcontig\tmaximum contiguous blocks" },
    847  1.54    simonb 	{ BOTH,		"-b bsize\tblock size" },
    848  1.64      fvdl 	{ BOTH,		"-c blocks\tblocks per cylinder group" },
    849  1.75       dsl 	{ BOTH,		"-d maxbsize\tmaximum extent size" },
    850  1.54    simonb 	{ BOTH,		"-e maxbpg\tmaximum blocks per file in a cylinder group"
    851  1.54    simonb 			    },
    852  1.54    simonb 	{ BOTH,		"-f fsize\tfrag size" },
    853  1.54    simonb 	{ NEWFS,	"-g avgfilesize\taverage file size" },
    854  1.54    simonb 	{ MFS_MOUNT,	"-g groupname\tgroup name of mount point" },
    855  1.54    simonb 	{ BOTH,		"-h avgfpdir\taverage files per directory" },
    856  1.54    simonb 	{ BOTH,		"-i density\tnumber of bytes per inode" },
    857  1.54    simonb 	{ BOTH,		"-m minfree\tminimum free space %%" },
    858  1.75       dsl 	{ BOTH,		"-n inodes\tnumder of inodes (overrides -i density)" },
    859  1.54    simonb 	{ BOTH,		"-o optim\toptimization preference (`space' or `time')"
    860  1.54    simonb 			    },
    861  1.54    simonb 	{ MFS_MOUNT,	"-p perm\t\tpermissions (in octal)" },
    862  1.54    simonb 	{ BOTH,		"-s fssize\tfile system size (sectors)" },
    863  1.54    simonb 	{ MFS_MOUNT,	"-u username\tuser name of mount point" },
    864  1.61       dbj 	{ NEWFS,	"-v volname\tApple UFS volume name" },
    865  1.54    simonb 	{ 0, NULL }
    866  1.54    simonb };
    867  1.54    simonb 
    868  1.25  christos static void
    869  1.40    simonb usage(void)
    870   1.1       cgd {
    871  1.54    simonb 	int match;
    872  1.54    simonb 	const struct help_strings *hs;
    873  1.43     lukem 
    874   1.1       cgd 	if (mfs) {
    875   1.1       cgd 		fprintf(stderr,
    876  1.43     lukem 		    "usage: %s [ fsoptions ] special-device mount-point\n",
    877  1.42       cgd 			getprogname());
    878   1.1       cgd 	} else
    879   1.1       cgd 		fprintf(stderr,
    880  1.43     lukem 		    "usage: %s [ fsoptions ] special-device%s\n",
    881  1.42       cgd 		    getprogname(),
    882   1.1       cgd #ifdef COMPAT
    883  1.75       dsl 		    " [disk-type]");
    884   1.1       cgd #else
    885   1.1       cgd 		    "");
    886   1.1       cgd #endif
    887   1.1       cgd 	fprintf(stderr, "where fsoptions are:\n");
    888  1.54    simonb 
    889  1.54    simonb 	match = mfs ? MFS_MOUNT : NEWFS;
    890  1.54    simonb 	for (hs = help_strings; hs->flags != 0; hs++)
    891  1.54    simonb 		if (hs->flags & match)
    892  1.54    simonb 			fprintf(stderr, "\t%s\n", hs->str);
    893   1.1       cgd 	exit(1);
    894   1.1       cgd }
    895