xref: /onnv-gate/usr/src/cmd/fs.d/ufs/newfs/newfs.c (revision 12312:2f3e4b56b6fe)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51565Svsakar  * Common Development and Distribution License (the "License").
61565Svsakar  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * newfs: friendly front end to mkfs
240Sstevel@tonic-gate  *
2512273SCasper.Dik@Sun.COM  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <locale.h>
310Sstevel@tonic-gate #include <sys/stat.h>
320Sstevel@tonic-gate #include <sys/buf.h>
330Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
340Sstevel@tonic-gate #include <sys/vnode.h>
350Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <string.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <stdarg.h>
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <fcntl.h>
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include <limits.h>
470Sstevel@tonic-gate #include <libintl.h>
480Sstevel@tonic-gate #include <sys/dkio.h>
490Sstevel@tonic-gate #include <sys/vtoc.h>
500Sstevel@tonic-gate #include <sys/mkdev.h>
510Sstevel@tonic-gate #include <sys/efi_partition.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include <fslib.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static unsigned int number(char *, char *, int, int);
560Sstevel@tonic-gate static int64_t number64(char *, char *, int, int64_t);
570Sstevel@tonic-gate static diskaddr_t getdiskbydev(char *);
580Sstevel@tonic-gate static int  yes(void);
590Sstevel@tonic-gate static int  notrand(char *);
600Sstevel@tonic-gate static void usage();
610Sstevel@tonic-gate static diskaddr_t get_device_size(int, char *);
620Sstevel@tonic-gate static diskaddr_t brute_force_get_device_size(int);
630Sstevel@tonic-gate static int validate_size(char *disk, diskaddr_t size);
640Sstevel@tonic-gate static void exenv(void);
650Sstevel@tonic-gate static struct fs *read_sb(char *);
660Sstevel@tonic-gate /*PRINTFLIKE1*/
670Sstevel@tonic-gate static void fatal(char *fmt, ...);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #define	EPATH "PATH=/usr/sbin:/sbin:"
700Sstevel@tonic-gate #define	CPATH "/sbin"					/* an EPATH element */
710Sstevel@tonic-gate #define	MB (1024 * 1024)
720Sstevel@tonic-gate #define	GBSEC ((1024 * 1024 * 1024) / DEV_BSIZE)	/* sectors in a GB */
730Sstevel@tonic-gate #define	MINFREESEC ((64 * 1024 * 1024) / DEV_BSIZE)	/* sectors in 64 MB */
740Sstevel@tonic-gate #define	MINCPG (16)	/* traditional */
750Sstevel@tonic-gate #define	MAXDEFDENSITY (8 * 1024)	/* arbitrary */
760Sstevel@tonic-gate #define	MINDENSITY (2 * 1024)	/* traditional */
770Sstevel@tonic-gate #define	MIN_MTB_DENSITY (1024 * 1024)
780Sstevel@tonic-gate #define	POWEROF2(num)	(((num) & ((num) - 1)) == 0)
790Sstevel@tonic-gate #define	SECTORS_PER_TERABYTE	(1LL << 31)
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * The following constant specifies an upper limit for file system size
820Sstevel@tonic-gate  * that is actually a lot bigger than we expect to support with UFS. (Since
830Sstevel@tonic-gate  * it's specified in sectors, the file system size would be 2**44 * 512,
840Sstevel@tonic-gate  * which is 2**53, which is 8192 Terabytes.)  However, it's useful
850Sstevel@tonic-gate  * for checking the basic sanity of a size value that is input on the
860Sstevel@tonic-gate  * command line.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate #define	FS_SIZE_UPPER_LIMIT	0x100000000000LL
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /* For use with number() */
910Sstevel@tonic-gate #define	NR_NONE		0
920Sstevel@tonic-gate #define	NR_PERCENT	0x01
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * The following two constants set the default block and fragment sizes.
960Sstevel@tonic-gate  * Both constants must be a power of 2 and meet the following constraints:
970Sstevel@tonic-gate  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
980Sstevel@tonic-gate  *	DEV_BSIZE <= DESFRAGSIZE <= DESBLKSIZE
990Sstevel@tonic-gate  *	DESBLKSIZE / DESFRAGSIZE <= 8
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate #define	DESBLKSIZE	8192
1020Sstevel@tonic-gate #define	DESFRAGSIZE	1024
1030Sstevel@tonic-gate 
1041565Svsakar #ifdef DEBUG
1051565Svsakar #define	dprintf(x)	printf x
1061565Svsakar #else
1071565Svsakar #define	dprintf(x)
1081565Svsakar #endif
109757Svsakar 
1100Sstevel@tonic-gate static int	Nflag;		/* run mkfs without writing file system */
1110Sstevel@tonic-gate static int	Tflag;		/* set up file system for growth to over 1 TB */
1120Sstevel@tonic-gate static int	verbose;	/* show mkfs line before exec */
1130Sstevel@tonic-gate static int	fsize = 0;		/* fragment size */
1140Sstevel@tonic-gate static int	fsize_flag = 0;	/* fragment size was specified on cmd line */
1150Sstevel@tonic-gate static int	bsize;		/* block size */
1160Sstevel@tonic-gate static int	ntracks;	/* # tracks/cylinder */
1170Sstevel@tonic-gate static int	ntracks_set = 0; /* true if the user specified ntracks */
1180Sstevel@tonic-gate static int	optim = FS_OPTTIME;	/* optimization, t(ime) or s(pace) */
1190Sstevel@tonic-gate static int	nsectors;	/* # sectors/track */
1200Sstevel@tonic-gate static int	cpg;		/* cylinders/cylinder group */
1210Sstevel@tonic-gate static int	cpg_set = 0;	/* true if the user specified cpg */
1220Sstevel@tonic-gate static int	minfree = -1;	/* free space threshold */
1230Sstevel@tonic-gate static int	rpm;		/* revolutions/minute of drive */
1240Sstevel@tonic-gate static int	rpm_set = 0;	/* true if the user specified rpm */
1250Sstevel@tonic-gate static int	nrpos = 8;	/* # of distinguished rotational positions */
1260Sstevel@tonic-gate 				/* 8 is the historical default */
1270Sstevel@tonic-gate static int	nrpos_set = 0;	/* true if the user specified nrpos */
1280Sstevel@tonic-gate static int	density = 0;	/* number of bytes per inode */
1290Sstevel@tonic-gate static int	apc;		/* alternates per cylinder */
1300Sstevel@tonic-gate static int	apc_set = 0;	/* true if the user specified apc */
1310Sstevel@tonic-gate static int 	rot = -1;	/* rotational delay (msecs) */
1320Sstevel@tonic-gate static int	rot_set = 0;	/* true if the user specified rot */
1330Sstevel@tonic-gate static int 	maxcontig = -1;	/* maximum number of contig blocks */
134392Sswilcox static int	text_sb = 0;	/* no disk changes; just final sb text dump */
135392Sswilcox static int	binary_sb = 0;	/* no disk changes; just final sb binary dump */
1360Sstevel@tonic-gate static int	label_type;	/* see types below */
1370Sstevel@tonic-gate 
138757Svsakar /*
139757Svsakar  * The variable use_efi_dflts is an indicator of whether to use EFI logic
140757Svsakar  * or the geometry logic in laying out the filesystem. This is decided
1415150Svsakar  * based on the size/type of the disk and is used only for non-EFI labeled
1425150Svsakar  * disks and removable media.
143757Svsakar  */
144757Svsakar static int	use_efi_dflts = 0;
1455150Svsakar static int	isremovable = 0;
1466969Sws195443 static int	ishotpluggable = 0;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate static char	device[MAXPATHLEN];
1490Sstevel@tonic-gate static char	cmd[BUFSIZ];
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate extern	char	*getfullrawname(); /* from libadm */
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate int
main(int argc,char * argv[])1540Sstevel@tonic-gate main(int argc, char *argv[])
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	char *special, *name;
1570Sstevel@tonic-gate 	struct stat64 st;
1580Sstevel@tonic-gate 	int status;
1590Sstevel@tonic-gate 	int option;
1600Sstevel@tonic-gate 	struct fs *sbp;	/* Pointer to superblock (if present) */
1610Sstevel@tonic-gate 	diskaddr_t actual_fssize;
1620Sstevel@tonic-gate 	diskaddr_t max_possible_fssize;
1630Sstevel@tonic-gate 	diskaddr_t req_fssize = 0;
1640Sstevel@tonic-gate 	diskaddr_t fssize = 0;
1650Sstevel@tonic-gate 	char	*req_fssize_str = NULL; /* requested size argument */
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1700Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
1710Sstevel@tonic-gate #endif
1720Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	opterr = 0;	/* We print our own errors, disable getopt's message */
175392Sswilcox 	while ((option = getopt(argc, argv,
176392Sswilcox 	    "vNBSs:C:d:t:o:a:b:f:c:m:n:r:i:T")) != EOF) {
1770Sstevel@tonic-gate 		switch (option) {
178392Sswilcox 		case 'S':
179392Sswilcox 			text_sb++;
180392Sswilcox 			break;
181392Sswilcox 		case 'B':
182392Sswilcox 			binary_sb++;
183392Sswilcox 			break;
1840Sstevel@tonic-gate 		case 'v':
1850Sstevel@tonic-gate 			verbose++;
1860Sstevel@tonic-gate 			break;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		case 'N':
1890Sstevel@tonic-gate 			Nflag++;
1900Sstevel@tonic-gate 			break;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		case 's':
1930Sstevel@tonic-gate 			/*
1940Sstevel@tonic-gate 			 * The maximum file system size is a lot smaller
1950Sstevel@tonic-gate 			 * than FS_SIZE_UPPER_LIMIT, but until we find out
1960Sstevel@tonic-gate 			 * the device size and block size, we don't know
1970Sstevel@tonic-gate 			 * what it is.  So save the requested size in a
1980Sstevel@tonic-gate 			 * string so that we can print it out later if we
1990Sstevel@tonic-gate 			 * determine it's too big.
2000Sstevel@tonic-gate 			 */
2010Sstevel@tonic-gate 			req_fssize = number64("fssize", optarg, NR_NONE,
2020Sstevel@tonic-gate 			    FS_SIZE_UPPER_LIMIT);
2030Sstevel@tonic-gate 			if (req_fssize < 1024)
2040Sstevel@tonic-gate 				fatal(gettext(
2050Sstevel@tonic-gate 				    "%s: fssize must be at least 1024"),
2060Sstevel@tonic-gate 				    optarg);
2070Sstevel@tonic-gate 			req_fssize_str = strdup(optarg);
2080Sstevel@tonic-gate 			if (req_fssize_str == NULL)
2090Sstevel@tonic-gate 				fatal(gettext(
2100Sstevel@tonic-gate 				    "Insufficient memory for string copy."));
2110Sstevel@tonic-gate 			break;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 		case 'C':
2140Sstevel@tonic-gate 			maxcontig = number("maxcontig", optarg, NR_NONE, -1);
2150Sstevel@tonic-gate 			if (maxcontig < 0)
2160Sstevel@tonic-gate 				fatal(gettext("%s: bad maxcontig"), optarg);
2170Sstevel@tonic-gate 			break;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		case 'd':
2200Sstevel@tonic-gate 			rot = number("rotdelay", optarg, NR_NONE, 0);
2210Sstevel@tonic-gate 			rot_set = 1;
2220Sstevel@tonic-gate 			if (rot < 0 || rot > 1000)
2230Sstevel@tonic-gate 				fatal(gettext(
2240Sstevel@tonic-gate 				    "%s: bad rotational delay"), optarg);
2250Sstevel@tonic-gate 			break;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 		case 't':
2280Sstevel@tonic-gate 			ntracks = number("ntrack", optarg, NR_NONE, 16);
2290Sstevel@tonic-gate 			ntracks_set = 1;
2300Sstevel@tonic-gate 			if ((ntracks < 0) ||
2310Sstevel@tonic-gate 			    (ntracks > INT_MAX))
2320Sstevel@tonic-gate 				fatal(gettext("%s: bad total tracks"), optarg);
2330Sstevel@tonic-gate 			break;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		case 'o':
2360Sstevel@tonic-gate 			if (strcmp(optarg, "space") == 0)
2375150Svsakar 				optim = FS_OPTSPACE;
2380Sstevel@tonic-gate 			else if (strcmp(optarg, "time") == 0)
2395150Svsakar 				optim = FS_OPTTIME;
2400Sstevel@tonic-gate 			else
2415150Svsakar 				fatal(gettext(
2425150Svsakar "%s: bad optimization preference (options are `space' or `time')"), optarg);
2430Sstevel@tonic-gate 			break;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		case 'a':
2460Sstevel@tonic-gate 			apc = number("apc", optarg, NR_NONE, 0);
2470Sstevel@tonic-gate 			apc_set = 1;
2480Sstevel@tonic-gate 			if (apc < 0 || apc > 32768) /* see mkfs.c */
2490Sstevel@tonic-gate 				fatal(gettext(
2500Sstevel@tonic-gate 				    "%s: bad alternates per cyl"), optarg);
2510Sstevel@tonic-gate 			break;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 		case 'b':
2540Sstevel@tonic-gate 			bsize = number("bsize", optarg, NR_NONE, DESBLKSIZE);
2550Sstevel@tonic-gate 			if (bsize < MINBSIZE || bsize > MAXBSIZE)
2560Sstevel@tonic-gate 				fatal(gettext(
2570Sstevel@tonic-gate 				    "%s: bad block size"), optarg);
2580Sstevel@tonic-gate 			break;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 		case 'f':
2610Sstevel@tonic-gate 			fsize = number("fragsize", optarg, NR_NONE,
2625150Svsakar 			    DESFRAGSIZE);
2630Sstevel@tonic-gate 			fsize_flag++;
2640Sstevel@tonic-gate 			/* xxx ought to test against bsize for upper limit */
2650Sstevel@tonic-gate 			if (fsize < DEV_BSIZE)
2660Sstevel@tonic-gate 				fatal(gettext("%s: bad frag size"), optarg);
2670Sstevel@tonic-gate 			break;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 		case 'c':
2700Sstevel@tonic-gate 			cpg = number("cpg", optarg, NR_NONE, 16);
2710Sstevel@tonic-gate 			cpg_set = 1;
2720Sstevel@tonic-gate 			if (cpg < 1)
2730Sstevel@tonic-gate 				fatal(gettext("%s: bad cylinders/group"),
2740Sstevel@tonic-gate 				    optarg);
2750Sstevel@tonic-gate 			break;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		case 'm':
2780Sstevel@tonic-gate 			minfree = number("minfree", optarg, NR_PERCENT, 10);
2790Sstevel@tonic-gate 			if (minfree < 0 || minfree > 99)
2800Sstevel@tonic-gate 				fatal(gettext("%s: bad free space %%"), optarg);
2810Sstevel@tonic-gate 			break;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		case 'n':
2840Sstevel@tonic-gate 			nrpos = number("nrpos", optarg, NR_NONE, 8);
2850Sstevel@tonic-gate 			nrpos_set = 1;
2860Sstevel@tonic-gate 			if (nrpos <= 0)
2870Sstevel@tonic-gate 				fatal(gettext(
2880Sstevel@tonic-gate 				    "%s: bad number of rotational positions"),
2890Sstevel@tonic-gate 				    optarg);
2900Sstevel@tonic-gate 			break;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		case 'r':
2930Sstevel@tonic-gate 			rpm = number("rpm", optarg, NR_NONE, 3600);
2940Sstevel@tonic-gate 			rpm_set = 1;
2950Sstevel@tonic-gate 			if (rpm < 0)
2960Sstevel@tonic-gate 				fatal(gettext("%s: bad revs/minute"), optarg);
2970Sstevel@tonic-gate 			break;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		case 'i':
3000Sstevel@tonic-gate 			/* xxx ought to test against fsize */
3010Sstevel@tonic-gate 			density = number("nbpi", optarg, NR_NONE, 2048);
3020Sstevel@tonic-gate 			if (density < DEV_BSIZE)
3030Sstevel@tonic-gate 				fatal(gettext("%s: bad bytes per inode"),
3040Sstevel@tonic-gate 				    optarg);
3050Sstevel@tonic-gate 			break;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		case 'T':
3080Sstevel@tonic-gate 			Tflag++;
3090Sstevel@tonic-gate 			break;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 		default:
3120Sstevel@tonic-gate 			usage();
3130Sstevel@tonic-gate 			fatal(gettext("-%c: unknown flag"), optopt);
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	/* At this point, there should only be one argument left:	*/
3180Sstevel@tonic-gate 	/* The raw-special-device itself. If not, print usage message.	*/
3190Sstevel@tonic-gate 	if ((argc - optind) != 1) {
3200Sstevel@tonic-gate 		usage();
3210Sstevel@tonic-gate 		exit(1);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	name = argv[optind];
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	special = getfullrawname(name);
3270Sstevel@tonic-gate 	if (special == NULL) {
3280Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("newfs: malloc failed\n"));
3290Sstevel@tonic-gate 		exit(1);
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (*special == '\0') {
3330Sstevel@tonic-gate 		if (strchr(name, '/') != NULL) {
3340Sstevel@tonic-gate 			if (stat64(name, &st) < 0) {
3350Sstevel@tonic-gate 				(void) fprintf(stderr,
3360Sstevel@tonic-gate 				    gettext("newfs: %s: %s\n"),
3370Sstevel@tonic-gate 				    name, strerror(errno));
3380Sstevel@tonic-gate 				exit(2);
3390Sstevel@tonic-gate 			}
3400Sstevel@tonic-gate 			fatal(gettext("%s: not a raw disk device"), name);
3410Sstevel@tonic-gate 		}
3426413Svk154806 		(void) snprintf(device, sizeof (device), "/dev/rdsk/%s", name);
3430Sstevel@tonic-gate 		if ((special = getfullrawname(device)) == NULL) {
3440Sstevel@tonic-gate 			(void) fprintf(stderr,
3450Sstevel@tonic-gate 			    gettext("newfs: malloc failed\n"));
3460Sstevel@tonic-gate 			exit(1);
3470Sstevel@tonic-gate 		}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 		if (*special == '\0') {
3506413Svk154806 			(void) snprintf(device, sizeof (device), "/dev/%s",
3516413Svk154806 			    name);
3520Sstevel@tonic-gate 			if ((special = getfullrawname(device)) == NULL) {
3530Sstevel@tonic-gate 				(void) fprintf(stderr,
3540Sstevel@tonic-gate 				    gettext("newfs: malloc failed\n"));
3550Sstevel@tonic-gate 				exit(1);
3560Sstevel@tonic-gate 			}
3570Sstevel@tonic-gate 			if (*special == '\0')
3580Sstevel@tonic-gate 				fatal(gettext(
3590Sstevel@tonic-gate 				    "%s: not a raw disk device"), name);
3600Sstevel@tonic-gate 		}
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	/*
3640Sstevel@tonic-gate 	 * getdiskbydev() determines the characteristics of the special
3650Sstevel@tonic-gate 	 * device on which the file system will be built.  In the case
3660Sstevel@tonic-gate 	 * of devices with SMI labels (that is, non-EFI labels), the
3670Sstevel@tonic-gate 	 * following characteristics are set (if they were not already
3680Sstevel@tonic-gate 	 * set on the command line, since the command line settings
3690Sstevel@tonic-gate 	 * take precedence):
3700Sstevel@tonic-gate 	 *
3710Sstevel@tonic-gate 	 *	nsectors - sectors per track
3720Sstevel@tonic-gate 	 *	ntracks - tracks per cylinder
3730Sstevel@tonic-gate 	 *	rpm - disk revolutions per minute
3740Sstevel@tonic-gate 	 *
3750Sstevel@tonic-gate 	 *	apc is NOT set
3760Sstevel@tonic-gate 	 *
3770Sstevel@tonic-gate 	 * getdiskbydev() also sets the following quantities for all
3780Sstevel@tonic-gate 	 * devices, if not already set:
3790Sstevel@tonic-gate 	 *
3800Sstevel@tonic-gate 	 *	bsize - file system block size
3810Sstevel@tonic-gate 	 *	maxcontig
3820Sstevel@tonic-gate 	 *	label_type (efi, vtoc, or other)
3830Sstevel@tonic-gate 	 *
3840Sstevel@tonic-gate 	 * getdiskbydev() returns the actual size of the device, in
3850Sstevel@tonic-gate 	 * sectors.
3860Sstevel@tonic-gate 	 */
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	actual_fssize = getdiskbydev(special);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (req_fssize == 0) {
3910Sstevel@tonic-gate 		fssize = actual_fssize;
3920Sstevel@tonic-gate 	} else {
3930Sstevel@tonic-gate 		/*
3940Sstevel@tonic-gate 		 * If the user specified a size larger than what we've
3950Sstevel@tonic-gate 		 * determined as the actual size of the device, see if the
3960Sstevel@tonic-gate 		 * size specified by the user can be read.  If so, use it,
3970Sstevel@tonic-gate 		 * since some devices and volume managers may not support
3980Sstevel@tonic-gate 		 * the vtoc and EFI interfaces we use to determine device
3990Sstevel@tonic-gate 		 * size.
4000Sstevel@tonic-gate 		 */
4010Sstevel@tonic-gate 		if (req_fssize > actual_fssize &&
4020Sstevel@tonic-gate 		    validate_size(special, req_fssize)) {
4030Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4040Sstevel@tonic-gate "Warning: the requested size of this file system\n"
4050Sstevel@tonic-gate "(%lld sectors) is greater than the size of the\n"
4060Sstevel@tonic-gate "device reported by the driver (%lld sectors).\n"
4070Sstevel@tonic-gate "However, a read of the device at the requested size\n"
4080Sstevel@tonic-gate "does succeed, so the requested size will be used.\n"),
4090Sstevel@tonic-gate 			    req_fssize, actual_fssize);
4100Sstevel@tonic-gate 			fssize = req_fssize;
4110Sstevel@tonic-gate 		} else {
4120Sstevel@tonic-gate 			fssize = MIN(req_fssize, actual_fssize);
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	if (label_type == LABEL_TYPE_VTOC) {
4171565Svsakar 		if (nsectors < 0)
4181565Svsakar 			fatal(gettext("%s: no default #sectors/track"),
4191565Svsakar 			    special);
420757Svsakar 		if (!use_efi_dflts) {
421757Svsakar 			if (ntracks < 0)
422757Svsakar 				fatal(gettext("%s: no default #tracks"),
423757Svsakar 				    special);
424757Svsakar 		}
4250Sstevel@tonic-gate 		if (rpm < 0)
4260Sstevel@tonic-gate 			fatal(gettext(
4270Sstevel@tonic-gate 			    "%s: no default revolutions/minute value"),
4280Sstevel@tonic-gate 			    special);
4290Sstevel@tonic-gate 		if (rpm < 60) {
4300Sstevel@tonic-gate 			(void) fprintf(stderr,
4310Sstevel@tonic-gate 			    gettext("Warning: setting rpm to 60\n"));
4320Sstevel@tonic-gate 			rpm = 60;
4330Sstevel@tonic-gate 		}
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 	if (label_type == LABEL_TYPE_EFI || label_type == LABEL_TYPE_OTHER) {
4360Sstevel@tonic-gate 		if (ntracks_set)
4370Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4380Sstevel@tonic-gate "Warning: ntracks is obsolete for this device and will be ignored.\n"));
4390Sstevel@tonic-gate 		if (cpg_set)
4400Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4410Sstevel@tonic-gate "Warning: cylinders/group is obsolete for this device and will be ignored.\n"));
4420Sstevel@tonic-gate 		if (rpm_set)
4430Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4440Sstevel@tonic-gate "Warning: rpm is obsolete for this device and will be ignored.\n"));
4450Sstevel@tonic-gate 		if (rot_set)
4460Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4470Sstevel@tonic-gate "Warning: rotational delay is obsolete for this device and"
4480Sstevel@tonic-gate " will be ignored.\n"));
4490Sstevel@tonic-gate 		if (nrpos_set)
4500Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4510Sstevel@tonic-gate "Warning: number of rotational positions is obsolete for this device and\n"
4520Sstevel@tonic-gate "will be ignored.\n"));
4530Sstevel@tonic-gate 		if (apc_set)
4540Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4550Sstevel@tonic-gate "Warning: number of alternate sectors per cylinder is obsolete for this\n"
4560Sstevel@tonic-gate "device and will be ignored.\n"));
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 		/*
4590Sstevel@tonic-gate 		 * We need these for the call to mkfs, even though they are
4600Sstevel@tonic-gate 		 * meaningless.
4610Sstevel@tonic-gate 		 */
4620Sstevel@tonic-gate 		rpm = 60;
4630Sstevel@tonic-gate 		nrpos = 1;
4640Sstevel@tonic-gate 		apc = 0;
4650Sstevel@tonic-gate 		rot = -1;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 		/*
4680Sstevel@tonic-gate 		 * These values are set to produce a file system with
4690Sstevel@tonic-gate 		 * a cylinder group size of 48MB.   For disks with
4700Sstevel@tonic-gate 		 * non-EFI labels, most geometries result in cylinder
4710Sstevel@tonic-gate 		 * groups of around 40 - 50 MB, so we arbitrarily choose
4720Sstevel@tonic-gate 		 * 48MB for disks with EFI labels.  mkfs will reduce
4730Sstevel@tonic-gate 		 * cylinders per group even further if necessary.
4740Sstevel@tonic-gate 		 */
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 		cpg = 16;
4770Sstevel@tonic-gate 		nsectors = 128;
4780Sstevel@tonic-gate 		ntracks = 48;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		/*
4810Sstevel@tonic-gate 		 * mkfs produces peculiar results for file systems
4820Sstevel@tonic-gate 		 * that are smaller than one cylinder so don't allow
4830Sstevel@tonic-gate 		 * them to be created (this check is only made for
4840Sstevel@tonic-gate 		 * disks with EFI labels.  Eventually, it should probably
4850Sstevel@tonic-gate 		 * be enforced for all disks.)
4860Sstevel@tonic-gate 		 */
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 		if (fssize < nsectors * ntracks) {
4890Sstevel@tonic-gate 			fatal(gettext(
4900Sstevel@tonic-gate 			    "file system size must be at least %d sectors"),
4910Sstevel@tonic-gate 			    nsectors * ntracks);
4920Sstevel@tonic-gate 		}
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	if (fssize > INT_MAX)
4960Sstevel@tonic-gate 		Tflag = 1;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	/*
4990Sstevel@tonic-gate 	 * If the user requested that the file system be set up for
5000Sstevel@tonic-gate 	 * eventual growth to over a terabyte, or if it's already greater
5010Sstevel@tonic-gate 	 * than a terabyte, set the inode density (nbpi) to MIN_MTB_DENSITY
5020Sstevel@tonic-gate 	 * (unless the user has specified a larger nbpi), set the frag size
5030Sstevel@tonic-gate 	 * equal to the block size, and set the cylinders-per-group value
5040Sstevel@tonic-gate 	 * passed to mkfs to -1, which tells mkfs to make cylinder groups
5050Sstevel@tonic-gate 	 * as large as possible.
5060Sstevel@tonic-gate 	 */
5070Sstevel@tonic-gate 	if (Tflag) {
5080Sstevel@tonic-gate 		if (density < MIN_MTB_DENSITY)
5090Sstevel@tonic-gate 			density = MIN_MTB_DENSITY;
5100Sstevel@tonic-gate 		fsize = bsize;
5110Sstevel@tonic-gate 		cpg = -1; 	/* says make cyl groups as big as possible */
5120Sstevel@tonic-gate 	} else {
5130Sstevel@tonic-gate 		if (fsize == 0)
5140Sstevel@tonic-gate 			fsize = DESFRAGSIZE;
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	if (!POWEROF2(fsize)) {
5180Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5190Sstevel@tonic-gate 		    "newfs: fragment size must a power of 2, not %d\n"), fsize);
5200Sstevel@tonic-gate 		fsize = bsize/8;
5210Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5220Sstevel@tonic-gate 		    "newfs: fragsize reset to %ld\n"), fsize);
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/*
5260Sstevel@tonic-gate 	 * The file system is limited in size by the fragment size.
5270Sstevel@tonic-gate 	 * The number of fragments in the file system must fit into
5280Sstevel@tonic-gate 	 * a signed 32-bit quantity, so the number of sectors in the
5290Sstevel@tonic-gate 	 * file system is INT_MAX * the number of sectors in a frag.
5300Sstevel@tonic-gate 	 */
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	max_possible_fssize = ((uint64_t)fsize)/DEV_BSIZE * INT_MAX;
5330Sstevel@tonic-gate 	if (fssize > max_possible_fssize)
5340Sstevel@tonic-gate 		fssize = max_possible_fssize;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	/*
5370Sstevel@tonic-gate 	 * Now fssize is the final size of the file system (in sectors).
5380Sstevel@tonic-gate 	 * If it's less than what the user requested, print a message.
5390Sstevel@tonic-gate 	 */
5400Sstevel@tonic-gate 	if (fssize < req_fssize) {
5410Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5420Sstevel@tonic-gate 		    "newfs: requested size of %s disk blocks is too large.\n"),
5430Sstevel@tonic-gate 		    req_fssize_str);
5440Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5450Sstevel@tonic-gate 		    "newfs: Resetting size to %lld\n"), fssize);
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	/*
5490Sstevel@tonic-gate 	 * fssize now equals the size (in sectors) of the file system
5500Sstevel@tonic-gate 	 * that will be created.
5510Sstevel@tonic-gate 	 */
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	/* XXX - following defaults are both here and in mkfs */
5540Sstevel@tonic-gate 	if (density <= 0) {
5550Sstevel@tonic-gate 		if (fssize < GBSEC)
5560Sstevel@tonic-gate 			density = MINDENSITY;
5570Sstevel@tonic-gate 		else
5580Sstevel@tonic-gate 			density = (int)((((longlong_t)fssize + (GBSEC - 1)) /
5595150Svsakar 			    GBSEC) * MINDENSITY);
5600Sstevel@tonic-gate 		if (density <= 0)
5610Sstevel@tonic-gate 			density = MINDENSITY;
5620Sstevel@tonic-gate 		if (density > MAXDEFDENSITY)
5630Sstevel@tonic-gate 			density = MAXDEFDENSITY;
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 	if (cpg == 0) {
5660Sstevel@tonic-gate 		/*
5670Sstevel@tonic-gate 		 * maxcpg calculation adapted from mkfs
5680Sstevel@tonic-gate 		 * In the case of disks with EFI labels, cpg has
5690Sstevel@tonic-gate 		 * already been set, so we won't enter this code.
5700Sstevel@tonic-gate 		 */
5710Sstevel@tonic-gate 		long maxcpg, maxipg;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 		maxipg = roundup(bsize * NBBY / 3,
5740Sstevel@tonic-gate 		    bsize / sizeof (struct inode));
5750Sstevel@tonic-gate 		maxcpg = (bsize - sizeof (struct cg) - howmany(maxipg, NBBY)) /
5760Sstevel@tonic-gate 		    (sizeof (long) + nrpos * sizeof (short) +
5775150Svsakar 		    nsectors / (MAXFRAG * NBBY));
5780Sstevel@tonic-gate 		cpg = (fssize / GBSEC) * 32;
5790Sstevel@tonic-gate 		if (cpg > maxcpg)
5800Sstevel@tonic-gate 			cpg = maxcpg;
5810Sstevel@tonic-gate 		if (cpg <= 0)
5820Sstevel@tonic-gate 			cpg = MINCPG;
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 	if (minfree < 0) {
5856413Svk154806 		minfree = (int)(((float)MINFREESEC / fssize) * 100);
5860Sstevel@tonic-gate 		if (minfree > 10)
5870Sstevel@tonic-gate 			minfree = 10;
5880Sstevel@tonic-gate 		if (minfree <= 0)
5890Sstevel@tonic-gate 			minfree = 1;
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate #ifdef i386	/* Bug 1170182 */
5920Sstevel@tonic-gate 	if (ntracks > 32 && (ntracks % 16) != 0) {
5930Sstevel@tonic-gate 		ntracks -= (ntracks % 16);
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate #endif
5960Sstevel@tonic-gate 	/*
5970Sstevel@tonic-gate 	 * Confirmation
5980Sstevel@tonic-gate 	 */
5990Sstevel@tonic-gate 	if (isatty(fileno(stdin)) && !Nflag) {
6000Sstevel@tonic-gate 		/*
6010Sstevel@tonic-gate 		 * If we can read a valid superblock, report the mount
6020Sstevel@tonic-gate 		 * point on which this filesystem was last mounted.
6030Sstevel@tonic-gate 		 */
6040Sstevel@tonic-gate 		if (((sbp = read_sb(special)) != 0) &&
6050Sstevel@tonic-gate 		    (*sbp->fs_fsmnt != '\0')) {
6060Sstevel@tonic-gate 			(void) printf(gettext(
6070Sstevel@tonic-gate 			    "newfs: %s last mounted as %s\n"),
6080Sstevel@tonic-gate 			    special, sbp->fs_fsmnt);
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 		(void) printf(gettext(
6110Sstevel@tonic-gate 		    "newfs: construct a new file system %s: (y/n)? "),
6120Sstevel@tonic-gate 		    special);
6130Sstevel@tonic-gate 		(void) fflush(stdout);
6140Sstevel@tonic-gate 		if (!yes())
6150Sstevel@tonic-gate 			exit(0);
6160Sstevel@tonic-gate 	}
6175760Svsakar 
618757Svsakar 	dprintf(("DeBuG newfs : nsect=%d ntrak=%d cpg=%d\n",
6195150Svsakar 	    nsectors, ntracks, cpg));
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * If alternates-per-cylinder is ever implemented:
6220Sstevel@tonic-gate 	 * need to get apc from dp->d_apc if no -a switch???
6230Sstevel@tonic-gate 	 */
62412273SCasper.Dik@Sun.COM 	(void) snprintf(cmd, sizeof (cmd), "mkfs -F ufs "
6256413Svk154806 	    "%s%s%s%s %lld %d %d %d %d %d %d %d %d %s %d %d %d %d %s",
626392Sswilcox 	    Nflag ? "-o N " : "", binary_sb ? "-o calcbinsb " : "",
627392Sswilcox 	    text_sb ? "-o calcsb " : "", special,
6280Sstevel@tonic-gate 	    fssize, nsectors, ntracks, bsize, fsize, cpg, minfree, rpm/60,
6290Sstevel@tonic-gate 	    density, optim == FS_OPTSPACE ? "s" : "t", apc, rot, nrpos,
6300Sstevel@tonic-gate 	    maxcontig, Tflag ? "y" : "n");
6310Sstevel@tonic-gate 	if (verbose) {
6320Sstevel@tonic-gate 		(void) printf("%s\n", cmd);
6330Sstevel@tonic-gate 		(void) fflush(stdout);
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 	exenv();
6360Sstevel@tonic-gate 	if (status = system(cmd))
6370Sstevel@tonic-gate 		exit(status >> 8);
6380Sstevel@tonic-gate 	if (Nflag)
6390Sstevel@tonic-gate 		exit(0);
6406413Svk154806 	(void) snprintf(cmd, sizeof (cmd), "/usr/sbin/fsirand %s", special);
6410Sstevel@tonic-gate 	if (notrand(special) && (status = system(cmd)) != 0)
6420Sstevel@tonic-gate 		(void) fprintf(stderr,
6430Sstevel@tonic-gate 		    gettext("%s: failed, status = %d\n"),
6440Sstevel@tonic-gate 		    cmd, status);
6450Sstevel@tonic-gate 	return (0);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate static void
exenv(void)6490Sstevel@tonic-gate exenv(void)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate 	char *epath;				/* executable file path */
6520Sstevel@tonic-gate 	char *cpath;				/* current path */
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	if ((cpath = getenv("PATH")) == NULL) {
6550Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("newfs: no PATH in env\n"));
6560Sstevel@tonic-gate 		/*
6570Sstevel@tonic-gate 		 * Background: the Bourne shell interpolates "." into
6580Sstevel@tonic-gate 		 * the path where said path starts with a colon, ends
6590Sstevel@tonic-gate 		 * with a colon, or has two adjacent colons.  Thus,
6600Sstevel@tonic-gate 		 * the path ":/sbin::/usr/sbin:" is equivalent to
6610Sstevel@tonic-gate 		 * ".:/sbin:.:/usr/sbin:.".  Now, we have no cpath,
6620Sstevel@tonic-gate 		 * and epath ends in a colon (to make for easy
6630Sstevel@tonic-gate 		 * catenation in the normal case).  By the above, if
6640Sstevel@tonic-gate 		 * we use "", then "." becomes part of path.  That's
6650Sstevel@tonic-gate 		 * bad, so use CPATH (which is just a duplicate of some
6660Sstevel@tonic-gate 		 * element in EPATH).  No point in opening ourselves
6670Sstevel@tonic-gate 		 * up to a Trojan horse attack when we don't have to....
6680Sstevel@tonic-gate 		 */
6690Sstevel@tonic-gate 		cpath = CPATH;
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 	if ((epath = malloc(strlen(EPATH) + strlen(cpath) + 1)) == NULL) {
6720Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("newfs: malloc failed\n"));
6730Sstevel@tonic-gate 		exit(1);
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 	(void) strcpy(epath, EPATH);
6760Sstevel@tonic-gate 	(void) strcat(epath, cpath);
6770Sstevel@tonic-gate 	if (putenv(epath) < 0) {
6780Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("newfs: putenv failed\n"));
6790Sstevel@tonic-gate 		exit(1);
6800Sstevel@tonic-gate 	}
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate static int
yes(void)6840Sstevel@tonic-gate yes(void)
6850Sstevel@tonic-gate {
6860Sstevel@tonic-gate 	int	i, b;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	i = b = getchar();
6890Sstevel@tonic-gate 	while (b != '\n' && b != '\0' && b != EOF)
6900Sstevel@tonic-gate 		b = getchar();
6910Sstevel@tonic-gate 	return (i == 'y');
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate /*
6950Sstevel@tonic-gate  * xxx Caller must run fmt through gettext(3) for us, if we ever
6960Sstevel@tonic-gate  * xxx go the i18n route....
6970Sstevel@tonic-gate  */
6980Sstevel@tonic-gate static void
fatal(char * fmt,...)6990Sstevel@tonic-gate fatal(char *fmt, ...)
7000Sstevel@tonic-gate {
7010Sstevel@tonic-gate 	va_list pvar;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	(void) fprintf(stderr, "newfs: ");
7040Sstevel@tonic-gate 	va_start(pvar, fmt);
7050Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, pvar);
7060Sstevel@tonic-gate 	va_end(pvar);
7070Sstevel@tonic-gate 	(void) putc('\n', stderr);
7080Sstevel@tonic-gate 	exit(10);
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate static diskaddr_t
getdiskbydev(char * disk)7120Sstevel@tonic-gate getdiskbydev(char *disk)
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate 	struct dk_geom g;
7150Sstevel@tonic-gate 	struct dk_cinfo ci;
716*12312SJim.Rice@Sun.COM 	struct dk_minfo info;
7170Sstevel@tonic-gate 	diskaddr_t actual_size;
7180Sstevel@tonic-gate 	int fd;
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	if ((fd = open64(disk, 0)) < 0) {
7210Sstevel@tonic-gate 		perror(disk);
7220Sstevel@tonic-gate 		exit(1);
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	/*
7260Sstevel@tonic-gate 	 * get_device_size() determines the actual size of the
7270Sstevel@tonic-gate 	 * device, and also the disk's attributes, such as geometry.
7280Sstevel@tonic-gate 	 */
7290Sstevel@tonic-gate 	actual_size = get_device_size(fd, disk);
7300Sstevel@tonic-gate 
7315760Svsakar 	if (label_type == LABEL_TYPE_VTOC) {
7326969Sws195443 
7335760Svsakar 		/*
7346969Sws195443 		 * Geometry information does not make sense for removable or
7356969Sws195443 		 * hotpluggable media anyway, so indicate mkfs to use EFI
7366969Sws195443 		 * default parameters.
7375760Svsakar 		 */
7385760Svsakar 		if (ioctl(fd, DKIOCREMOVABLE, &isremovable)) {
7395760Svsakar 			dprintf(("DeBuG newfs : Unable to determine if %s is"
7405760Svsakar 			    " Removable Media. Proceeding with system"
7415760Svsakar 			    " determined parameters.\n", disk));
7425760Svsakar 			isremovable = 0;
7436969Sws195443 		}
7446969Sws195443 
745*12312SJim.Rice@Sun.COM 		/* If removable check if a floppy disk */
746*12312SJim.Rice@Sun.COM 		if (isremovable) {
747*12312SJim.Rice@Sun.COM 			if (ioctl(fd, DKIOCGMEDIAINFO, &info)) {
748*12312SJim.Rice@Sun.COM 				dprintf(("DeBuG newfs : Unable to get media"
749*12312SJim.Rice@Sun.COM 				    " info from %s.\n", disk));
750*12312SJim.Rice@Sun.COM 			} else {
751*12312SJim.Rice@Sun.COM 				if (info.dki_media_type == DK_FLOPPY) {
752*12312SJim.Rice@Sun.COM 					isremovable = 0;
753*12312SJim.Rice@Sun.COM 				}
754*12312SJim.Rice@Sun.COM 			}
755*12312SJim.Rice@Sun.COM 		}
756*12312SJim.Rice@Sun.COM 
7576969Sws195443 		if (ioctl(fd, DKIOCHOTPLUGGABLE, &ishotpluggable)) {
7586969Sws195443 			dprintf(("DeBuG newfs : Unable to determine if %s is"
7596969Sws195443 			    " Hotpluggable Media. Proceeding with system"
7606969Sws195443 			    " determined parameters.\n", disk));
7616969Sws195443 			ishotpluggable = 0;
7626969Sws195443 		}
7636969Sws195443 
7646969Sws195443 		if ((isremovable || ishotpluggable) && !Tflag)
7655760Svsakar 			use_efi_dflts = 1;
7665150Svsakar 
7670Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &g))
7680Sstevel@tonic-gate 			fatal(gettext(
7690Sstevel@tonic-gate 			    "%s: Unable to read Disk geometry"), disk);
7707563SPrasad.Singamsetty@Sun.COM 		if ((((diskaddr_t)g.dkg_ncyl * g.dkg_nhead *
7717563SPrasad.Singamsetty@Sun.COM 		    g.dkg_nsect) > CHSLIMIT) && !Tflag) {
772757Svsakar 			use_efi_dflts = 1;
773757Svsakar 		}
7747563SPrasad.Singamsetty@Sun.COM 		dprintf(("DeBuG newfs : geom=%llu, CHSLIMIT=%d "
7756969Sws195443 		    "isremovable = %d ishotpluggable = %d use_efi_dflts = %d\n",
7767563SPrasad.Singamsetty@Sun.COM 		    (diskaddr_t)g.dkg_ncyl * g.dkg_nhead * g.dkg_nsect,
7777563SPrasad.Singamsetty@Sun.COM 		    CHSLIMIT, isremovable, ishotpluggable, use_efi_dflts));
778757Svsakar 		/*
7791565Svsakar 		 * The ntracks that is passed to mkfs is decided here based
7801565Svsakar 		 * on 'use_efi_dflts' and whether ntracks was specified as a
7811565Svsakar 		 * command line parameter to newfs.
7821565Svsakar 		 * If ntracks of -1 is passed to mkfs, mkfs uses DEF_TRACKS_EFI
7831565Svsakar 		 * and DEF_SECTORS_EFI for ntracks and nsectors respectively.
784757Svsakar 		 */
7850Sstevel@tonic-gate 		if (nsectors == 0)
7861565Svsakar 			nsectors = g.dkg_nsect;
7870Sstevel@tonic-gate 		if (ntracks == 0)
788757Svsakar 			ntracks = use_efi_dflts ? -1 : g.dkg_nhead;
7890Sstevel@tonic-gate 		if (rpm == 0)
7900Sstevel@tonic-gate 			rpm = ((int)g.dkg_rpm <= 0) ? 3600: g.dkg_rpm;
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	if (bsize == 0)
7940Sstevel@tonic-gate 		bsize = DESBLKSIZE;
7950Sstevel@tonic-gate 	/*
7960Sstevel@tonic-gate 	 * Adjust maxcontig by the device's maxtransfer. If maxtransfer
7970Sstevel@tonic-gate 	 * information is not available, default to the min of a MB and
7980Sstevel@tonic-gate 	 * maxphys.
7990Sstevel@tonic-gate 	 */
8000Sstevel@tonic-gate 	if (maxcontig == -1 && ioctl(fd, DKIOCINFO, &ci) == 0) {
8010Sstevel@tonic-gate 		maxcontig = ci.dki_maxtransfer * DEV_BSIZE;
8020Sstevel@tonic-gate 		if (maxcontig < 0) {
8030Sstevel@tonic-gate 			int	error, gotit, maxphys;
8040Sstevel@tonic-gate 			gotit = fsgetmaxphys(&maxphys, &error);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 			/*
8070Sstevel@tonic-gate 			 * If we cannot get the maxphys value, default
8080Sstevel@tonic-gate 			 * to ufs_maxmaxphys (MB).
8090Sstevel@tonic-gate 			 */
8100Sstevel@tonic-gate 			if (gotit) {
8110Sstevel@tonic-gate 				maxcontig = MIN(maxphys, MB);
8120Sstevel@tonic-gate 			} else {
8130Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
8140Sstevel@tonic-gate "Warning: Could not get system value for maxphys. The value for maxcontig\n"
8150Sstevel@tonic-gate "will default to 1MB.\n"));
8160Sstevel@tonic-gate 			maxcontig = MB;
8170Sstevel@tonic-gate 			}
8180Sstevel@tonic-gate 		}
8190Sstevel@tonic-gate 		maxcontig /= bsize;
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 	(void) close(fd);
8220Sstevel@tonic-gate 	return (actual_size);
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate /*
8260Sstevel@tonic-gate  * Figure out how big the partition we're dealing with is.
8270Sstevel@tonic-gate  */
8280Sstevel@tonic-gate static diskaddr_t
get_device_size(int fd,char * name)8290Sstevel@tonic-gate get_device_size(int fd, char *name)
8300Sstevel@tonic-gate {
8317563SPrasad.Singamsetty@Sun.COM 	struct extvtoc vtoc;
8320Sstevel@tonic-gate 	dk_gpt_t *efi_vtoc;
8330Sstevel@tonic-gate 	diskaddr_t	slicesize;
8340Sstevel@tonic-gate 
8357563SPrasad.Singamsetty@Sun.COM 	int index = read_extvtoc(fd, &vtoc);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	if (index >= 0) {
8380Sstevel@tonic-gate 		label_type = LABEL_TYPE_VTOC;
8390Sstevel@tonic-gate 	} else {
8400Sstevel@tonic-gate 		if (index == VT_ENOTSUP || index == VT_ERROR) {
8410Sstevel@tonic-gate 			/* it might be an EFI label */
8420Sstevel@tonic-gate 			index = efi_alloc_and_read(fd, &efi_vtoc);
8430Sstevel@tonic-gate 			if (index >= 0)
8440Sstevel@tonic-gate 				label_type = LABEL_TYPE_EFI;
8450Sstevel@tonic-gate 		}
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	if (index < 0) {
8490Sstevel@tonic-gate 		/*
8500Sstevel@tonic-gate 		 * Since both attempts to read the label failed, we're
8510Sstevel@tonic-gate 		 * going to fall back to a brute force approach to
8520Sstevel@tonic-gate 		 * determining the device's size:  see how far out we can
8530Sstevel@tonic-gate 		 * perform reads on the device.
8540Sstevel@tonic-gate 		 */
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		slicesize = brute_force_get_device_size(fd);
8570Sstevel@tonic-gate 		if (slicesize == 0) {
8580Sstevel@tonic-gate 			switch (index) {
8590Sstevel@tonic-gate 			case VT_ERROR:
8600Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
8610Sstevel@tonic-gate 				    "newfs: %s: %s\n"), name, strerror(errno));
8620Sstevel@tonic-gate 				exit(10);
8630Sstevel@tonic-gate 				/*NOTREACHED*/
8640Sstevel@tonic-gate 			case VT_EIO:
8650Sstevel@tonic-gate 				fatal(gettext(
8660Sstevel@tonic-gate 				    "%s: I/O error accessing VTOC"), name);
8670Sstevel@tonic-gate 				/*NOTREACHED*/
8680Sstevel@tonic-gate 			case VT_EINVAL:
8690Sstevel@tonic-gate 				fatal(gettext(
8700Sstevel@tonic-gate 				    "%s: Invalid field in VTOC"), name);
8710Sstevel@tonic-gate 				/*NOTREACHED*/
8720Sstevel@tonic-gate 			default:
8730Sstevel@tonic-gate 				fatal(gettext(
8740Sstevel@tonic-gate 				    "%s: unknown error accessing VTOC"),
8750Sstevel@tonic-gate 				    name);
8760Sstevel@tonic-gate 				/*NOTREACHED*/
8770Sstevel@tonic-gate 			}
8780Sstevel@tonic-gate 		} else {
8790Sstevel@tonic-gate 			label_type = LABEL_TYPE_OTHER;
8800Sstevel@tonic-gate 		}
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if (label_type == LABEL_TYPE_EFI) {
8840Sstevel@tonic-gate 		slicesize = efi_vtoc->efi_parts[index].p_size;
8850Sstevel@tonic-gate 		efi_free(efi_vtoc);
8860Sstevel@tonic-gate 	} else if (label_type == LABEL_TYPE_VTOC) {
8877563SPrasad.Singamsetty@Sun.COM 		slicesize = vtoc.v_part[index].p_size;
8880Sstevel@tonic-gate 	}
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	return (slicesize);
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate /*
8940Sstevel@tonic-gate  * brute_force_get_device_size
8950Sstevel@tonic-gate  *
8960Sstevel@tonic-gate  * Determine the size of the device by seeing how far we can
8970Sstevel@tonic-gate  * read.  Doing an llseek( , , SEEK_END) would probably work
8980Sstevel@tonic-gate  * in most cases, but we've seen at least one third-party driver
8990Sstevel@tonic-gate  * which doesn't correctly support the SEEK_END option when the
9000Sstevel@tonic-gate  * the device is greater than a terabyte.
9010Sstevel@tonic-gate  */
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate static diskaddr_t
brute_force_get_device_size(int fd)9040Sstevel@tonic-gate brute_force_get_device_size(int fd)
9050Sstevel@tonic-gate {
9060Sstevel@tonic-gate 	diskaddr_t	min_fail = 0;
9070Sstevel@tonic-gate 	diskaddr_t	max_succeed = 0;
9080Sstevel@tonic-gate 	diskaddr_t	cur_db_off;
9090Sstevel@tonic-gate 	char 		buf[DEV_BSIZE];
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	/*
9120Sstevel@tonic-gate 	 * First, see if we can read the device at all, just to
9130Sstevel@tonic-gate 	 * eliminate errors that have nothing to do with the
9140Sstevel@tonic-gate 	 * device's size.
9150Sstevel@tonic-gate 	 */
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	if (((llseek(fd, (offset_t)0, SEEK_SET)) == -1) ||
9180Sstevel@tonic-gate 	    ((read(fd, buf, DEV_BSIZE)) == -1))
9190Sstevel@tonic-gate 		return (0);  /* can't determine size */
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	/*
9220Sstevel@tonic-gate 	 * Now, go sequentially through the multiples of 4TB
9230Sstevel@tonic-gate 	 * to find the first read that fails (this isn't strictly
9240Sstevel@tonic-gate 	 * the most efficient way to find the actual size if the
9250Sstevel@tonic-gate 	 * size really could be anything between 0 and 2**64 bytes.
9260Sstevel@tonic-gate 	 * We expect the sizes to be less than 16 TB for some time,
9270Sstevel@tonic-gate 	 * so why do a bunch of reads that are larger than that?
9280Sstevel@tonic-gate 	 * However, this algorithm *will* work for sizes of greater
9290Sstevel@tonic-gate 	 * than 16 TB.  We're just not optimizing for those sizes.)
9300Sstevel@tonic-gate 	 */
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	for (cur_db_off = SECTORS_PER_TERABYTE * 4;
9330Sstevel@tonic-gate 	    min_fail == 0 && cur_db_off < FS_SIZE_UPPER_LIMIT;
9340Sstevel@tonic-gate 	    cur_db_off += 4 * SECTORS_PER_TERABYTE) {
9350Sstevel@tonic-gate 		if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE),
9360Sstevel@tonic-gate 		    SEEK_SET)) == -1) ||
9370Sstevel@tonic-gate 		    ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE))
9380Sstevel@tonic-gate 			min_fail = cur_db_off;
9390Sstevel@tonic-gate 		else
9400Sstevel@tonic-gate 			max_succeed = cur_db_off;
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	if (min_fail == 0)
9440Sstevel@tonic-gate 		return (0);
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	/*
9470Sstevel@tonic-gate 	 * We now know that the size of the device is less than
9480Sstevel@tonic-gate 	 * min_fail and greater than or equal to max_succeed.  Now
9490Sstevel@tonic-gate 	 * keep splitting the difference until the actual size in
9500Sstevel@tonic-gate 	 * sectors in known.  We also know that the difference
9510Sstevel@tonic-gate 	 * between max_succeed and min_fail at this time is
9520Sstevel@tonic-gate 	 * 4 * SECTORS_PER_TERABYTE, which is a power of two, which
9530Sstevel@tonic-gate 	 * simplifies the math below.
9540Sstevel@tonic-gate 	 */
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	while (min_fail - max_succeed > 1) {
9570Sstevel@tonic-gate 		cur_db_off = max_succeed + (min_fail - max_succeed)/2;
9580Sstevel@tonic-gate 		if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE),
9590Sstevel@tonic-gate 		    SEEK_SET)) == -1) ||
9600Sstevel@tonic-gate 		    ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE))
9610Sstevel@tonic-gate 			min_fail = cur_db_off;
9620Sstevel@tonic-gate 		else
9630Sstevel@tonic-gate 			max_succeed = cur_db_off;
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	/* the size is the last successfully read sector offset plus one */
9670Sstevel@tonic-gate 	return (max_succeed + 1);
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate /*
9710Sstevel@tonic-gate  * validate_size
9720Sstevel@tonic-gate  *
9730Sstevel@tonic-gate  * Return 1 if the device appears to be at least "size" sectors long.
9740Sstevel@tonic-gate  * Return 0 if it's shorter or we can't read it.
9750Sstevel@tonic-gate  */
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate static int
validate_size(char * disk,diskaddr_t size)9780Sstevel@tonic-gate validate_size(char *disk, diskaddr_t size)
9790Sstevel@tonic-gate {
9800Sstevel@tonic-gate 	char 		buf[DEV_BSIZE];
9810Sstevel@tonic-gate 	int fd, rc;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	if ((fd = open64(disk, O_RDONLY)) < 0) {
9840Sstevel@tonic-gate 		perror(disk);
9850Sstevel@tonic-gate 		exit(1);
9860Sstevel@tonic-gate 	}
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	if ((llseek(fd, (offset_t)((size - 1) * DEV_BSIZE), SEEK_SET) == -1) ||
9890Sstevel@tonic-gate 	    (read(fd, buf, DEV_BSIZE)) != DEV_BSIZE)
9900Sstevel@tonic-gate 		rc = 0;
9910Sstevel@tonic-gate 	else
9920Sstevel@tonic-gate 		rc = 1;
9930Sstevel@tonic-gate 	(void) close(fd);
9940Sstevel@tonic-gate 	return (rc);
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate /*
9980Sstevel@tonic-gate  * read_sb(char * rawdev) - Attempt to read the superblock from a raw device
9990Sstevel@tonic-gate  *
10000Sstevel@tonic-gate  * Returns:
10010Sstevel@tonic-gate  *	0 :
10020Sstevel@tonic-gate  *		Could not read a valid superblock for a variety of reasons.
10030Sstevel@tonic-gate  *		Since 'newfs' handles any fatal conditions, we're not going
10040Sstevel@tonic-gate  *		to make any guesses as to why this is failing or what should
10050Sstevel@tonic-gate  *		be done about it.
10060Sstevel@tonic-gate  *
10070Sstevel@tonic-gate  *	struct fs *:
10080Sstevel@tonic-gate  *		A pointer to (what we think is) a valid superblock. The
10090Sstevel@tonic-gate  *		space for the superblock is static (inside the function)
10100Sstevel@tonic-gate  *		since we will only be reading the values from it.
10110Sstevel@tonic-gate  */
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate struct fs *
read_sb(char * fsdev)10140Sstevel@tonic-gate read_sb(char *fsdev)
10150Sstevel@tonic-gate {
10160Sstevel@tonic-gate 	static struct fs	sblock;
10170Sstevel@tonic-gate 	struct stat64		statb;
10180Sstevel@tonic-gate 	int			dskfd;
10190Sstevel@tonic-gate 	char			*bufp = NULL;
10200Sstevel@tonic-gate 	int			bufsz = 0;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	if (stat64(fsdev, &statb) < 0)
10230Sstevel@tonic-gate 		return (0);
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	if ((dskfd = open64(fsdev, O_RDONLY)) < 0)
10260Sstevel@tonic-gate 		return (0);
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	/*
10290Sstevel@tonic-gate 	 * We need a buffer whose size is a multiple of DEV_BSIZE in order
10300Sstevel@tonic-gate 	 * to read from a raw device (which we were probably passed).
10310Sstevel@tonic-gate 	 */
10320Sstevel@tonic-gate 	bufsz = ((sizeof (sblock) / DEV_BSIZE) + 1) * DEV_BSIZE;
10330Sstevel@tonic-gate 	if ((bufp = malloc(bufsz)) == NULL) {
10340Sstevel@tonic-gate 		(void) close(dskfd);
10350Sstevel@tonic-gate 		return (0);
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	if (llseek(dskfd, (offset_t)SBOFF, SEEK_SET) < 0 ||
10390Sstevel@tonic-gate 	    read(dskfd, bufp, bufsz) < 0) {
10400Sstevel@tonic-gate 		(void) close(dskfd);
10410Sstevel@tonic-gate 		free(bufp);
10420Sstevel@tonic-gate 		return (0);
10430Sstevel@tonic-gate 	}
10440Sstevel@tonic-gate 	(void) close(dskfd);	/* Done with the file */
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	(void) memcpy(&sblock, bufp, sizeof (sblock));
10470Sstevel@tonic-gate 	free(bufp);	/* Don't need this anymore */
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	if (((sblock.fs_magic != FS_MAGIC) &&
10500Sstevel@tonic-gate 	    (sblock.fs_magic != MTB_UFS_MAGIC)) ||
10510Sstevel@tonic-gate 	    sblock.fs_ncg < 1 || sblock.fs_cpg < 1)
10520Sstevel@tonic-gate 		return (0);
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
10550Sstevel@tonic-gate 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
10560Sstevel@tonic-gate 		return (0);
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	if (sblock.fs_sbsize < 0 || sblock.fs_sbsize > SBSIZE)
10590Sstevel@tonic-gate 		return (0);
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	return (&sblock);
10620Sstevel@tonic-gate }
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate /*
10650Sstevel@tonic-gate  * Read the UFS file system on the raw device SPECIAL.  If it does not
10660Sstevel@tonic-gate  * appear to be a UFS file system, return non-zero, indicating that
10670Sstevel@tonic-gate  * fsirand should be called (and it will spit out an error message).
10680Sstevel@tonic-gate  * If it is a UFS file system, take a look at the inodes in the first
10690Sstevel@tonic-gate  * cylinder group.  If they appear to be randomized (non-zero), return
10700Sstevel@tonic-gate  * zero, which will cause fsirand to not be called.  If the inode generation
10710Sstevel@tonic-gate  * counts are all zero, then we must call fsirand, so return non-zero.
10720Sstevel@tonic-gate  */
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate #define	RANDOMIZED	0
10750Sstevel@tonic-gate #define	NOT_RANDOMIZED	1
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate static int
notrand(char * special)10780Sstevel@tonic-gate notrand(char *special)
10790Sstevel@tonic-gate {
10800Sstevel@tonic-gate 	long fsbuf[SBSIZE / sizeof (long)];
10810Sstevel@tonic-gate 	struct dinode dibuf[MAXBSIZE/sizeof (struct dinode)];
10820Sstevel@tonic-gate 	struct fs *fs;
10830Sstevel@tonic-gate 	struct dinode *dip;
10840Sstevel@tonic-gate 	offset_t seekaddr;
10850Sstevel@tonic-gate 	int bno, inum;
10860Sstevel@tonic-gate 	int fd;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	fs = (struct fs *)fsbuf;
10890Sstevel@tonic-gate 	if ((fd = open64(special, 0)) == -1)
10900Sstevel@tonic-gate 		return (NOT_RANDOMIZED);
10910Sstevel@tonic-gate 	if (llseek(fd, (offset_t)SBLOCK * DEV_BSIZE, 0) == -1 ||
10920Sstevel@tonic-gate 	    read(fd, (char *)fs, SBSIZE) != SBSIZE ||
10930Sstevel@tonic-gate 	    ((fs->fs_magic != FS_MAGIC) && (fs->fs_magic != MTB_UFS_MAGIC))) {
10940Sstevel@tonic-gate 		(void) close(fd);
10950Sstevel@tonic-gate 		return (NOT_RANDOMIZED);
10960Sstevel@tonic-gate 	}
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	/* looks like a UFS file system; read the first cylinder group */
10990Sstevel@tonic-gate 	bsize = INOPB(fs) * sizeof (struct dinode);
11000Sstevel@tonic-gate 	inum = 0;
11010Sstevel@tonic-gate 	while (inum < fs->fs_ipg) {
11020Sstevel@tonic-gate 		bno = itod(fs, inum);
11030Sstevel@tonic-gate 		seekaddr = (offset_t)fsbtodb(fs, bno) * DEV_BSIZE;
11040Sstevel@tonic-gate 		if (llseek(fd, seekaddr, 0) == -1 ||
11050Sstevel@tonic-gate 		    read(fd, (char *)dibuf, bsize) != bsize) {
11060Sstevel@tonic-gate 			(void) close(fd);
11070Sstevel@tonic-gate 			return (NOT_RANDOMIZED);
11080Sstevel@tonic-gate 		}
11090Sstevel@tonic-gate 		for (dip = dibuf; dip < &dibuf[INOPB(fs)]; dip++) {
11100Sstevel@tonic-gate 			if (dip->di_gen != 0) {
11110Sstevel@tonic-gate 				(void) close(fd);
11120Sstevel@tonic-gate 				return (RANDOMIZED);
11130Sstevel@tonic-gate 			}
11140Sstevel@tonic-gate 			inum++;
11150Sstevel@tonic-gate 		}
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate 	(void) close(fd);
11180Sstevel@tonic-gate 	return (NOT_RANDOMIZED);
11190Sstevel@tonic-gate }
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate static void
usage(void)11220Sstevel@tonic-gate usage(void)
11230Sstevel@tonic-gate {
11240Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
11250Sstevel@tonic-gate 	    "usage: newfs [ -v ] [ mkfs-options ] raw-special-device\n"));
11260Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("where mkfs-options are:\n"));
11270Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
11280Sstevel@tonic-gate 	    "\t-N do not create file system, just print out parameters\n"));
11290Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
11300Sstevel@tonic-gate "\t-T configure file system for eventual growth to over a terabyte\n"));
11310Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-s file system size (sectors)\n"));
11320Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-b block size\n"));
11330Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-f frag size\n"));
11340Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-t tracks/cylinder\n"));
11350Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-c cylinders/group\n"));
11360Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-m minimum free space %%\n"));
11370Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
11380Sstevel@tonic-gate 	    "\t-o optimization preference (`space' or `time')\n"));
11390Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-r revolutions/minute\n"));
11400Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-i number of bytes per inode\n"));
11410Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
11420Sstevel@tonic-gate 	    "\t-a number of alternates per cylinder\n"));
11430Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-C maxcontig\n"));
11440Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-d rotational delay\n"));
11450Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
11460Sstevel@tonic-gate 	    "\t-n number of rotational positions\n"));
1147392Sswilcox 	(void) fprintf(stderr, gettext(
1148392Sswilcox "\t-S print a textual version of the calculated superblock to stdout\n"));
1149392Sswilcox 	(void) fprintf(stderr, gettext(
1150392Sswilcox "\t-B dump a binary version of the calculated superblock to stdout\n"));
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate /*
11540Sstevel@tonic-gate  * Error-detecting version of atoi(3).  Adapted from mkfs' number().
11550Sstevel@tonic-gate  */
11560Sstevel@tonic-gate static unsigned int
number(char * param,char * value,int flags,int def_value)11570Sstevel@tonic-gate number(char *param, char *value, int flags, int def_value)
11580Sstevel@tonic-gate {
11590Sstevel@tonic-gate 	char *cs;
11600Sstevel@tonic-gate 	int n;
11610Sstevel@tonic-gate 	int cut = INT_MAX / 10;    /* limit to avoid overflow */
11620Sstevel@tonic-gate 	int minus = 0;
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	cs = value;
11650Sstevel@tonic-gate 	if (*cs == '-') {
11660Sstevel@tonic-gate 		minus = 1;
11670Sstevel@tonic-gate 		cs += 1;
11680Sstevel@tonic-gate 	}
11690Sstevel@tonic-gate 	if ((*cs < '0') || (*cs > '9')) {
11700Sstevel@tonic-gate 		goto bail_out;
11710Sstevel@tonic-gate 	}
11720Sstevel@tonic-gate 	n = 0;
11730Sstevel@tonic-gate 	while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) {
11740Sstevel@tonic-gate 		n = n*10 + *cs++ - '0';
11750Sstevel@tonic-gate 	}
11760Sstevel@tonic-gate 	if (minus)
11775150Svsakar 		n = -n;
11780Sstevel@tonic-gate 	for (;;) {
11790Sstevel@tonic-gate 		switch (*cs++) {
11800Sstevel@tonic-gate 		case '\0':
11810Sstevel@tonic-gate 			return (n);
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 		case '0': case '1': case '2': case '3': case '4':
11840Sstevel@tonic-gate 		case '5': case '6': case '7': case '8': case '9':
11850Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
11860Sstevel@tonic-gate 			    "newfs: value for %s overflowed, using %d\n"),
11870Sstevel@tonic-gate 			    param, def_value);
11880Sstevel@tonic-gate 			return (def_value);
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 		case '%':
11910Sstevel@tonic-gate 			if (flags & NR_PERCENT)
11920Sstevel@tonic-gate 				break;
11930Sstevel@tonic-gate 			/* FALLTHROUGH */
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 		default:
11960Sstevel@tonic-gate bail_out:
11970Sstevel@tonic-gate 			fatal(gettext("bad numeric arg for %s: \"%s\""),
11980Sstevel@tonic-gate 			    param, value);
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 		}
12010Sstevel@tonic-gate 	}
12020Sstevel@tonic-gate 	/* NOTREACHED */
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate /*
12060Sstevel@tonic-gate  * Error-detecting version of atoi(3).  Adapted from mkfs' number().
12070Sstevel@tonic-gate  */
12080Sstevel@tonic-gate static int64_t
number64(char * param,char * value,int flags,int64_t def_value)12090Sstevel@tonic-gate number64(char *param, char *value, int flags, int64_t def_value)
12100Sstevel@tonic-gate {
12110Sstevel@tonic-gate 	char *cs;
12120Sstevel@tonic-gate 	int64_t n;
12130Sstevel@tonic-gate 	int64_t cut = FS_SIZE_UPPER_LIMIT/ 10;    /* limit to avoid overflow */
12140Sstevel@tonic-gate 	int minus = 0;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	cs = value;
12170Sstevel@tonic-gate 	if (*cs == '-') {
12180Sstevel@tonic-gate 		minus = 1;
12190Sstevel@tonic-gate 		cs += 1;
12200Sstevel@tonic-gate 	}
12210Sstevel@tonic-gate 	if ((*cs < '0') || (*cs > '9')) {
12220Sstevel@tonic-gate 		goto bail_out;
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 	n = 0;
12250Sstevel@tonic-gate 	while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) {
12260Sstevel@tonic-gate 		n = n*10 + *cs++ - '0';
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 	if (minus)
12295150Svsakar 		n = -n;
12300Sstevel@tonic-gate 	for (;;) {
12310Sstevel@tonic-gate 		switch (*cs++) {
12320Sstevel@tonic-gate 		case '\0':
12330Sstevel@tonic-gate 			return (n);
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 		case '0': case '1': case '2': case '3': case '4':
12360Sstevel@tonic-gate 		case '5': case '6': case '7': case '8': case '9':
12370Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
12380Sstevel@tonic-gate 			    "newfs: value for %s overflowed, using %d\n"),
12390Sstevel@tonic-gate 			    param, def_value);
12400Sstevel@tonic-gate 			return (def_value);
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 		case '%':
12430Sstevel@tonic-gate 			if (flags & NR_PERCENT)
12440Sstevel@tonic-gate 				break;
12450Sstevel@tonic-gate 			/* FALLTHROUGH */
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 		default:
12480Sstevel@tonic-gate bail_out:
12490Sstevel@tonic-gate 			fatal(gettext("bad numeric arg for %s: \"%s\""),
12500Sstevel@tonic-gate 			    param, value);
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 		}
12530Sstevel@tonic-gate 	}
12540Sstevel@tonic-gate 	/* NOTREACHED */
12550Sstevel@tonic-gate }
1256