xref: /illumos-gate/usr/src/cmd/fs.d/ufs/newfs/newfs.c (revision f45bbb21)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56d24e334Svsakar  * Common Development and Distribution License (the "License").
66d24e334Svsakar  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * newfs: friendly front end to mkfs
247c478bd9Sstevel@tonic-gate  *
25134a1f4eSCasper H.S. Dik  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <locale.h>
317c478bd9Sstevel@tonic-gate #include <sys/stat.h>
327c478bd9Sstevel@tonic-gate #include <sys/buf.h>
337c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
347c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
357c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <stdarg.h>
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <fcntl.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <limits.h>
477c478bd9Sstevel@tonic-gate #include <libintl.h>
487c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
497c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
507c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
517c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
537c478bd9Sstevel@tonic-gate #include <fslib.h>
557c478bd9Sstevel@tonic-gate static unsigned int number(char *, char *, int, int);
567c478bd9Sstevel@tonic-gate static int64_t number64(char *, char *, int, int64_t);
577c478bd9Sstevel@tonic-gate static diskaddr_t getdiskbydev(char *);
587c478bd9Sstevel@tonic-gate static int  yes(void);
597c478bd9Sstevel@tonic-gate static int  notrand(char *);
607c478bd9Sstevel@tonic-gate static void usage();
617c478bd9Sstevel@tonic-gate static diskaddr_t get_device_size(int, char *);
627c478bd9Sstevel@tonic-gate static diskaddr_t brute_force_get_device_size(int);
637c478bd9Sstevel@tonic-gate static int validate_size(char *disk, diskaddr_t size);
647c478bd9Sstevel@tonic-gate static void exenv(void);
657c478bd9Sstevel@tonic-gate static struct fs *read_sb(char *);
667c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
67*f45bbb21SToomas Soome static void fatal(char *fmt, ...) __NORETURN;
697c478bd9Sstevel@tonic-gate #define	EPATH "PATH=/usr/sbin:/sbin:"
707c478bd9Sstevel@tonic-gate #define	CPATH "/sbin"					/* an EPATH element */
717c478bd9Sstevel@tonic-gate #define	MB (1024 * 1024)
727c478bd9Sstevel@tonic-gate #define	GBSEC ((1024 * 1024 * 1024) / DEV_BSIZE)	/* sectors in a GB */
737c478bd9Sstevel@tonic-gate #define	MINFREESEC ((64 * 1024 * 1024) / DEV_BSIZE)	/* sectors in 64 MB */
747c478bd9Sstevel@tonic-gate #define	MINCPG (16)	/* traditional */
757c478bd9Sstevel@tonic-gate #define	MAXDEFDENSITY (8 * 1024)	/* arbitrary */
767c478bd9Sstevel@tonic-gate #define	MINDENSITY (2 * 1024)	/* traditional */
777c478bd9Sstevel@tonic-gate #define	MIN_MTB_DENSITY (1024 * 1024)
787c478bd9Sstevel@tonic-gate #define	POWEROF2(num)	(((num) & ((num) - 1)) == 0)
797c478bd9Sstevel@tonic-gate #define	SECTORS_PER_TERABYTE	(1LL << 31)
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * The following constant specifies an upper limit for file system size
827c478bd9Sstevel@tonic-gate  * that is actually a lot bigger than we expect to support with UFS. (Since
837c478bd9Sstevel@tonic-gate  * it's specified in sectors, the file system size would be 2**44 * 512,
847c478bd9Sstevel@tonic-gate  * which is 2**53, which is 8192 Terabytes.)  However, it's useful
857c478bd9Sstevel@tonic-gate  * for checking the basic sanity of a size value that is input on the
867c478bd9Sstevel@tonic-gate  * command line.
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate #define	FS_SIZE_UPPER_LIMIT	0x100000000000LL
907c478bd9Sstevel@tonic-gate /* For use with number() */
917c478bd9Sstevel@tonic-gate #define	NR_NONE		0
927c478bd9Sstevel@tonic-gate #define	NR_PERCENT	0x01
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * The following two constants set the default block and fragment sizes.
967c478bd9Sstevel@tonic-gate  * Both constants must be a power of 2 and meet the following constraints:
977c478bd9Sstevel@tonic-gate  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
987c478bd9Sstevel@tonic-gate  *	DEV_BSIZE <= DESFRAGSIZE <= DESBLKSIZE
997c478bd9Sstevel@tonic-gate  *	DESBLKSIZE / DESFRAGSIZE <= 8
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate #define	DESBLKSIZE	8192
1027c478bd9Sstevel@tonic-gate #define	DESFRAGSIZE	1024
1046d24e334Svsakar #ifdef DEBUG
1056d24e334Svsakar #define	dprintf(x)	printf x
1066d24e334Svsakar #else
1076d24e334Svsakar #define	dprintf(x)
1086d24e334Svsakar #endif
1107c478bd9Sstevel@tonic-gate static int	Nflag;		/* run mkfs without writing file system */
1117c478bd9Sstevel@tonic-gate static int	Tflag;		/* set up file system for growth to over 1 TB */
1127c478bd9Sstevel@tonic-gate static int	verbose;	/* show mkfs line before exec */
1137c478bd9Sstevel@tonic-gate static int	fsize = 0;		/* fragment size */
1147c478bd9Sstevel@tonic-gate static int	fsize_flag = 0;	/* fragment size was specified on cmd line */
1157c478bd9Sstevel@tonic-gate static int	bsize;		/* block size */
1167c478bd9Sstevel@tonic-gate static int	ntracks;	/* # tracks/cylinder */
1177c478bd9Sstevel@tonic-gate static int	ntracks_set = 0; /* true if the user specified ntracks */
1187c478bd9Sstevel@tonic-gate static int	optim = FS_OPTTIME;	/* optimization, t(ime) or s(pace) */
1197c478bd9Sstevel@tonic-gate static int	nsectors;	/* # sectors/track */
1207c478bd9Sstevel@tonic-gate static int	cpg;		/* cylinders/cylinder group */
1217c478bd9Sstevel@tonic-gate static int	cpg_set = 0;	/* true if the user specified cpg */
1227c478bd9Sstevel@tonic-gate static int	minfree = -1;	/* free space threshold */
1237c478bd9Sstevel@tonic-gate static int	rpm;		/* revolutions/minute of drive */
1247c478bd9Sstevel@tonic-gate static int	rpm_set = 0;	/* true if the user specified rpm */
1257c478bd9Sstevel@tonic-gate static int	nrpos = 8;	/* # of distinguished rotational positions */
1267c478bd9Sstevel@tonic-gate 				/* 8 is the historical default */
1277c478bd9Sstevel@tonic-gate static int	nrpos_set = 0;	/* true if the user specified nrpos */
1287c478bd9Sstevel@tonic-gate static int	density = 0;	/* number of bytes per inode */
1297c478bd9Sstevel@tonic-gate static int	apc;		/* alternates per cylinder */
1307c478bd9Sstevel@tonic-gate static int	apc_set = 0;	/* true if the user specified apc */
1317c478bd9Sstevel@tonic-gate static int 	rot = -1;	/* rotational delay (msecs) */
1327c478bd9Sstevel@tonic-gate static int	rot_set = 0;	/* true if the user specified rot */
1337c478bd9Sstevel@tonic-gate static int 	maxcontig = -1;	/* maximum number of contig blocks */
134355d6bb5Sswilcox static int	text_sb = 0;	/* no disk changes; just final sb text dump */
135355d6bb5Sswilcox static int	binary_sb = 0;	/* no disk changes; just final sb binary dump */
1367c478bd9Sstevel@tonic-gate static int	label_type;	/* see types below */
1386451fdbcSvsakar /*
1396451fdbcSvsakar  * The variable use_efi_dflts is an indicator of whether to use EFI logic
1406451fdbcSvsakar  * or the geometry logic in laying out the filesystem. This is decided
141d50c8f90Svsakar  * based on the size/type of the disk and is used only for non-EFI labeled
142d50c8f90Svsakar  * disks and removable media.
1436451fdbcSvsakar  */
1446451fdbcSvsakar static int	use_efi_dflts = 0;
145d50c8f90Svsakar static int	isremovable = 0;
1468b7c2cdfSws static int	ishotpluggable = 0;
1487c478bd9Sstevel@tonic-gate static char	device[MAXPATHLEN];
1497c478bd9Sstevel@tonic-gate static char	cmd[BUFSIZ];
1517c478bd9Sstevel@tonic-gate extern	char	*getfullrawname(); /* from libadm */
1537c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1547c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	char *special, *name;
1577c478bd9Sstevel@tonic-gate 	struct stat64 st;
1587c478bd9Sstevel@tonic-gate 	int status;
1597c478bd9Sstevel@tonic-gate 	int option;
1607c478bd9Sstevel@tonic-gate 	struct fs *sbp;	/* Pointer to superblock (if present) */
1617c478bd9Sstevel@tonic-gate 	diskaddr_t actual_fssize;
1627c478bd9Sstevel@tonic-gate 	diskaddr_t max_possible_fssize;
1637c478bd9Sstevel@tonic-gate 	diskaddr_t req_fssize = 0;
1647c478bd9Sstevel@tonic-gate 	diskaddr_t fssize = 0;
1657c478bd9Sstevel@tonic-gate 	char	*req_fssize_str = NULL; /* requested size argument */
1677c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1697c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1707c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
1717c478bd9Sstevel@tonic-gate #endif
1727c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1747c478bd9Sstevel@tonic-gate 	opterr = 0;	/* We print our own errors, disable getopt's message */
175355d6bb5Sswilcox 	while ((option = getopt(argc, argv,
176355d6bb5Sswilcox 	    "vNBSs:C:d:t:o:a:b:f:c:m:n:r:i:T")) != EOF) {
1777c478bd9Sstevel@tonic-gate 		switch (option) {
178355d6bb5Sswilcox 		case 'S':
179355d6bb5Sswilcox 			text_sb++;
180355d6bb5Sswilcox 			break;
181355d6bb5Sswilcox 		case 'B':
182355d6bb5Sswilcox 			binary_sb++;
183355d6bb5Sswilcox 			break;
1847c478bd9Sstevel@tonic-gate 		case 'v':
1857c478bd9Sstevel@tonic-gate 			verbose++;
1867c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 		case 'N':
1897c478bd9Sstevel@tonic-gate 			Nflag++;
1907c478bd9Sstevel@tonic-gate 			break;
1927c478bd9Sstevel@tonic-gate 		case 's':
1937c478bd9Sstevel@tonic-gate 			/*
1947c478bd9Sstevel@tonic-gate 			 * The maximum file system size is a lot smaller
1957c478bd9Sstevel@tonic-gate 			 * than FS_SIZE_UPPER_LIMIT, but until we find out
1967c478bd9Sstevel@tonic-gate 			 * the device size and block size, we don't know
1977c478bd9Sstevel@tonic-gate 			 * what it is.  So save the requested size in a
1987c478bd9Sstevel@tonic-gate 			 * string so that we can print it out later if we
1997c478bd9Sstevel@tonic-gate 			 * determine it's too big.
2007c478bd9Sstevel@tonic-gate 			 */
2017c478bd9Sstevel@tonic-gate 			req_fssize = number64("fssize", optarg, NR_NONE,
2027c478bd9Sstevel@tonic-gate 			    FS_SIZE_UPPER_LIMIT);
2037c478bd9Sstevel@tonic-gate 			if (req_fssize < 1024)
2047c478bd9Sstevel@tonic-gate 				fatal(gettext(
2057c478bd9Sstevel@tonic-gate 				    "%s: fssize must be at least 1024"),
2067c478bd9Sstevel@tonic-gate 				    optarg);
2077c478bd9Sstevel@tonic-gate 			req_fssize_str = strdup(optarg);
2087c478bd9Sstevel@tonic-gate 			if (req_fssize_str == NULL)
2097c478bd9Sstevel@tonic-gate 				fatal(gettext(
2107c478bd9Sstevel@tonic-gate 				    "Insufficient memory for string copy."));
2117c478bd9Sstevel@tonic-gate 			break;
2137c478bd9Sstevel@tonic-gate 		case 'C':
2147c478bd9Sstevel@tonic-gate 			maxcontig = number("maxcontig", optarg, NR_NONE, -1);
2157c478bd9Sstevel@tonic-gate 			if (maxcontig < 0)
2167c478bd9Sstevel@tonic-gate 				fatal(gettext("%s: bad maxcontig"), optarg);
2177c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 		case 'd':
2207c478bd9Sstevel@tonic-gate 			rot = number("rotdelay", optarg, NR_NONE, 0);
2217c478bd9Sstevel@tonic-gate 			rot_set = 1;
2227c478bd9Sstevel@tonic-gate 			if (rot < 0 || rot > 1000)
2237c478bd9Sstevel@tonic-gate 				fatal(gettext(
2247c478bd9Sstevel@tonic-gate 				    "%s: bad rotational delay"), optarg);
2257c478bd9Sstevel@tonic-gate 			break;
2277c478bd9Sstevel@tonic-gate 		case 't':
2287c478bd9Sstevel@tonic-gate 			ntracks = number("ntrack", optarg, NR_NONE, 16);
2297c478bd9Sstevel@tonic-gate 			ntracks_set = 1;
2307c478bd9Sstevel@tonic-gate 			if ((ntracks < 0) ||
2317c478bd9Sstevel@tonic-gate 			    (ntracks > INT_MAX))
2327c478bd9Sstevel@tonic-gate 				fatal(gettext("%s: bad total tracks"), optarg);
2337c478bd9Sstevel@tonic-gate 			break;
2357c478bd9Sstevel@tonic-gate 		case 'o':
2367c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "space") == 0)
237d50c8f90Svsakar 				optim = FS_OPTSPACE;
2387c478bd9Sstevel@tonic-gate 			else if (strcmp(optarg, "time") == 0)
239d50c8f90Svsakar 				optim = FS_OPTTIME;
2407c478bd9Sstevel@tonic-gate 			else
241d50c8f90Svsakar 				fatal(gettext(
242d50c8f90Svsakar "%s: bad optimization preference (options are `space' or `time')"), optarg);
2437c478bd9Sstevel@tonic-gate 			break;
2457c478bd9Sstevel@tonic-gate 		case 'a':
2467c478bd9Sstevel@tonic-gate 			apc = number("apc", optarg, NR_NONE, 0);
2477c478bd9Sstevel@tonic-gate 			apc_set = 1;
2487c478bd9Sstevel@tonic-gate 			if (apc < 0 || apc > 32768) /* see mkfs.c */
2497c478bd9Sstevel@tonic-gate 				fatal(gettext(
2507c478bd9Sstevel@tonic-gate 				    "%s: bad alternates per cyl"), optarg);
2517c478bd9Sstevel@tonic-gate 			break;
2537c478bd9Sstevel@tonic-gate 		case 'b':
2547c478bd9Sstevel@tonic-gate 			bsize = number("bsize", optarg, NR_NONE, DESBLKSIZE);
2557c478bd9Sstevel@tonic-gate 			if (bsize < MINBSIZE || bsize > MAXBSIZE)
2567c478bd9Sstevel@tonic-gate 				fatal(gettext(
2577c478bd9Sstevel@tonic-gate 				    "%s: bad block size"), optarg);
2587c478bd9Sstevel@tonic-gate 			break;
2607c478bd9Sstevel@tonic-gate 		case 'f':
2617c478bd9Sstevel@tonic-gate 			fsize = number("fragsize", optarg, NR_NONE,
262d50c8f90Svsakar 			    DESFRAGSIZE);
2637c478bd9Sstevel@tonic-gate 			fsize_flag++;
2647c478bd9Sstevel@tonic-gate 			/* xxx ought to test against bsize for upper limit */
2657c478bd9Sstevel@tonic-gate 			if (fsize < DEV_BSIZE)
2667c478bd9Sstevel@tonic-gate 				fatal(gettext("%s: bad frag size"), optarg);
2677c478bd9Sstevel@tonic-gate 			break;
2697c478bd9Sstevel@tonic-gate 		case 'c':
2707c478bd9Sstevel@tonic-gate 			cpg = number("cpg", optarg, NR_NONE, 16);
2717c478bd9Sstevel@tonic-gate 			cpg_set = 1;
2727c478bd9Sstevel@tonic-gate 			if (cpg < 1)
2737c478bd9Sstevel@tonic-gate 				fatal(gettext("%s: bad cylinders/group"),
2747c478bd9Sstevel@tonic-gate 				    optarg);
2757c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 		case 'm':
2787c478bd9Sstevel@tonic-gate 			minfree = number("minfree", optarg, NR_PERCENT, 10);
2797c478bd9Sstevel@tonic-gate 			if (minfree < 0 || minfree > 99)
2807c478bd9Sstevel@tonic-gate 				fatal(gettext("%s: bad free space %%"), optarg);
2817c478bd9Sstevel@tonic-gate 			break;
2837c478bd9Sstevel@tonic-gate 		case 'n':
2847c478bd9Sstevel@tonic-gate 			nrpos = number("nrpos", optarg, NR_NONE, 8);
2857c478bd9Sstevel@tonic-gate 			nrpos_set = 1;
2867c478bd9Sstevel@tonic-gate 			if (nrpos <= 0)
2877c478bd9Sstevel@tonic-gate 				fatal(gettext(
2887c478bd9Sstevel@tonic-gate 				    "%s: bad number of rotational positions"),
2897c478bd9Sstevel@tonic-gate 				    optarg);
2907c478bd9Sstevel@tonic-gate 			break;
2927c478bd9Sstevel@tonic-gate 		case 'r':
2937c478bd9Sstevel@tonic-gate 			rpm = number("rpm", optarg, NR_NONE, 3600);
2947c478bd9Sstevel@tonic-gate 			rpm_set = 1;
2957c478bd9Sstevel@tonic-gate 			if (rpm < 0)
2967c478bd9Sstevel@tonic-gate 				fatal(gettext("%s: bad revs/minute"), optarg);
2977c478bd9Sstevel@tonic-gate 			break;
2997c478bd9Sstevel@tonic-gate 		case 'i':
3007c478bd9Sstevel@tonic-gate 			/* xxx ought to test against fsize */
3017c478bd9Sstevel@tonic-gate 			density = number("nbpi", optarg, NR_NONE, 2048);
3027c478bd9Sstevel@tonic-gate 			if (density < DEV_BSIZE)
3037c478bd9Sstevel@tonic-gate 				fatal(gettext("%s: bad bytes per inode"),
3047c478bd9Sstevel@tonic-gate 				    optarg);
3057c478bd9Sstevel@tonic-gate 			break;
3077c478bd9Sstevel@tonic-gate 		case 'T':
3087c478bd9Sstevel@tonic-gate 			Tflag++;
3097c478bd9Sstevel@tonic-gate 			break;
3117c478bd9Sstevel@tonic-gate 		default:
3127c478bd9Sstevel@tonic-gate 			usage();
3137c478bd9Sstevel@tonic-gate 			fatal(gettext("-%c: unknown flag"), optopt);
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 	/* At this point, there should only be one argument left:	*/
3187c478bd9Sstevel@tonic-gate 	/* The raw-special-device itself. If not, print usage message.	*/
3197c478bd9Sstevel@tonic-gate 	if ((argc - optind) != 1) {
3207c478bd9Sstevel@tonic-gate 		usage();
3217c478bd9Sstevel@tonic-gate 		exit(1);
3227c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 	name = argv[optind];
3267c478bd9Sstevel@tonic-gate 	special = getfullrawname(name);
3277c478bd9Sstevel@tonic-gate 	if (special == NULL) {
3287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("newfs: malloc failed\n"));
3297c478bd9Sstevel@tonic-gate 		exit(1);
3307c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 	if (*special == '\0') {
3337c478bd9Sstevel@tonic-gate 		if (strchr(name, '/') != NULL) {
3347c478bd9Sstevel@tonic-gate 			if (stat64(name, &st) < 0) {
3357c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3367c478bd9Sstevel@tonic-gate 				    gettext("newfs: %s: %s\n"),
3377c478bd9Sstevel@tonic-gate 				    name, strerror(errno));
3387c478bd9Sstevel@tonic-gate 				exit(2);
3397c478bd9Sstevel@tonic-gate 			}
3407c478bd9Sstevel@tonic-gate 			fatal(gettext("%s: not a raw disk device"), name);
3417c478bd9Sstevel@tonic-gate 		}
342db60a39dSvk 		(void) snprintf(device, sizeof (device), "/dev/rdsk/%s", name);
3437c478bd9Sstevel@tonic-gate 		if ((special = getfullrawname(device)) == NULL) {
3447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3457c478bd9Sstevel@tonic-gate 			    gettext("newfs: malloc failed\n"));
3467c478bd9Sstevel@tonic-gate 			exit(1);
3477c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 		if (*special == '\0') {
350db60a39dSvk 			(void) snprintf(device, sizeof (device), "/dev/%s",
351db60a39dSvk 			    name);
3527c478bd9Sstevel@tonic-gate 			if ((special = getfullrawname(device)) == NULL) {
3537c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3547c478bd9Sstevel@tonic-gate 				    gettext("newfs: malloc failed\n"));
3557c478bd9Sstevel@tonic-gate 				exit(1);
3567c478bd9Sstevel@tonic-gate 			}
3577c478bd9Sstevel@tonic-gate 			if (*special == '\0')
3587c478bd9Sstevel@tonic-gate 				fatal(gettext(
3597c478bd9Sstevel@tonic-gate 				    "%s: not a raw disk device"), name);
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 	/*
3647c478bd9Sstevel@tonic-gate 	 * getdiskbydev() determines the characteristics of the special
3657c478bd9Sstevel@tonic-gate 	 * device on which the file system will be built.  In the case
3667c478bd9Sstevel@tonic-gate 	 * of devices with SMI labels (that is, non-EFI labels), the
3677c478bd9Sstevel@tonic-gate 	 * following characteristics are set (if they were not already
3687c478bd9Sstevel@tonic-gate 	 * set on the command line, since the command line settings
3697c478bd9Sstevel@tonic-gate 	 * take precedence):
3707c478bd9Sstevel@tonic-gate 	 *
3717c478bd9Sstevel@tonic-gate 	 *	nsectors - sectors per track
3727c478bd9Sstevel@tonic-gate 	 *	ntracks - tracks per cylinder
3737c478bd9Sstevel@tonic-gate 	 *	rpm - disk revolutions per minute
3747c478bd9Sstevel@tonic-gate 	 *
3757c478bd9Sstevel@tonic-gate 	 *	apc is NOT set
3767c478bd9Sstevel@tonic-gate 	 *
3777c478bd9Sstevel@tonic-gate 	 * getdiskbydev() also sets the following quantities for all
3787c478bd9Sstevel@tonic-gate 	 * devices, if not already set:
3797c478bd9Sstevel@tonic-gate 	 *
3807c478bd9Sstevel@tonic-gate 	 *	bsize - file system block size
3817c478bd9Sstevel@tonic-gate 	 *	maxcontig
3827c478bd9Sstevel@tonic-gate 	 *	label_type (efi, vtoc, or other)
3837c478bd9Sstevel@tonic-gate 	 *
3847c478bd9Sstevel@tonic-gate 	 * getdiskbydev() returns the actual size of the device, in
3857c478bd9Sstevel@tonic-gate 	 * sectors.
3867c478bd9Sstevel@tonic-gate 	 */
3887c478bd9Sstevel@tonic-gate 	actual_fssize = getdiskbydev(special);
3907c478bd9Sstevel@tonic-gate 	if (req_fssize == 0) {
3917c478bd9Sstevel@tonic-gate 		fssize = actual_fssize;
3927c478bd9Sstevel@tonic-gate 	} else {
3937c478bd9Sstevel@tonic-gate 		/*
3947c478bd9Sstevel@tonic-gate 		 * If the user specified a size larger than what we've
3957c478bd9Sstevel@tonic-gate 		 * determined as the actual size of the device, see if the
3967c478bd9Sstevel@tonic-gate 		 * size specified by the user can be read.  If so, use it,
3977c478bd9Sstevel@tonic-gate 		 * since some devices and volume managers may not support
3987c478bd9Sstevel@tonic-gate 		 * the vtoc and EFI interfaces we use to determine device
3997c478bd9Sstevel@tonic-gate 		 * size.
4007c478bd9Sstevel@tonic-gate 		 */
4017c478bd9Sstevel@tonic-gate 		if (req_fssize > actual_fssize &&
4027c478bd9Sstevel@tonic-gate 		    validate_size(special, req_fssize)) {
4037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4047c478bd9Sstevel@tonic-gate "Warning: the requested size of this file system\n"
4057c478bd9Sstevel@tonic-gate "(%lld sectors) is greater than the size of the\n"
4067c478bd9Sstevel@tonic-gate "device reported by the driver (%lld sectors).\n"
4077c478bd9Sstevel@tonic-gate "However, a read of the device at the requested size\n"
4087c478bd9Sstevel@tonic-gate "does succeed, so the requested size will be used.\n"),
4097c478bd9Sstevel@tonic-gate 			    req_fssize, actual_fssize);
4107c478bd9Sstevel@tonic-gate 			fssize = req_fssize;
4117c478bd9Sstevel@tonic-gate 		} else {
4127c478bd9Sstevel@tonic-gate 			fssize = MIN(req_fssize, actual_fssize);
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 	if (label_type == LABEL_TYPE_VTOC) {
4176d24e334Svsakar 		if (nsectors < 0)
4186d24e334Svsakar 			fatal(gettext("%s: no default #sectors/track"),
4196d24e334Svsakar 			    special);
4206451fdbcSvsakar 		if (!use_efi_dflts) {
4216451fdbcSvsakar 			if (ntracks < 0)
4226451fdbcSvsakar 				fatal(gettext("%s: no default #tracks"),
4236451fdbcSvsakar 				    special);
4246451fdbcSvsakar 		}
4257c478bd9Sstevel@tonic-gate 		if (rpm < 0)
4267c478bd9Sstevel@tonic-gate 			fatal(gettext(
4277c478bd9Sstevel@tonic-gate 			    "%s: no default revolutions/minute value"),
4287c478bd9Sstevel@tonic-gate 			    special);
4297c478bd9Sstevel@tonic-gate 		if (rpm < 60) {
4307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4317c478bd9Sstevel@tonic-gate 			    gettext("Warning: setting rpm to 60\n"));
4327c478bd9Sstevel@tonic-gate 			rpm = 60;
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	if (label_type == LABEL_TYPE_EFI || label_type == LABEL_TYPE_OTHER) {
4367c478bd9Sstevel@tonic-gate 		if (ntracks_set)
4377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4387c478bd9Sstevel@tonic-gate "Warning: ntracks is obsolete for this device and will be ignored.\n"));
4397c478bd9Sstevel@tonic-gate 		if (cpg_set)
4407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4417c478bd9Sstevel@tonic-gate "Warning: cylinders/group is obsolete for this device and will be ignored.\n"));
4427c478bd9Sstevel@tonic-gate 		if (rpm_set)
4437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4447c478bd9Sstevel@tonic-gate "Warning: rpm is obsolete for this device and will be ignored.\n"));
4457c478bd9Sstevel@tonic-gate 		if (rot_set)
4467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4477c478bd9Sstevel@tonic-gate "Warning: rotational delay is obsolete for this device and"
4487c478bd9Sstevel@tonic-gate " will be ignored.\n"));
4497c478bd9Sstevel@tonic-gate 		if (nrpos_set)
4507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4517c478bd9Sstevel@tonic-gate "Warning: number of rotational positions is obsolete for this device and\n"
4527c478bd9Sstevel@tonic-gate "will be ignored.\n"));
4537c478bd9Sstevel@tonic-gate 		if (apc_set)
4547c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4557c478bd9Sstevel@tonic-gate "Warning: number of alternate sectors per cylinder is obsolete for this\n"
4567c478bd9Sstevel@tonic-gate "device and will be ignored.\n"));
4587c478bd9Sstevel@tonic-gate 		/*
4597c478bd9Sstevel@tonic-gate 		 * We need these for the call to mkfs, even though they are
4607c478bd9Sstevel@tonic-gate 		 * meaningless.
4617c478bd9Sstevel@tonic-gate 		 */
4627c478bd9Sstevel@tonic-gate 		rpm = 60;
4637c478bd9Sstevel@tonic-gate 		nrpos = 1;
4647c478bd9Sstevel@tonic-gate 		apc = 0;
4657c478bd9Sstevel@tonic-gate 		rot = -1;
4677c478bd9Sstevel@tonic-gate 		/*
4687c478bd9Sstevel@tonic-gate 		 * These values are set to produce a file system with
4697c478bd9Sstevel@tonic-gate 		 * a cylinder group size of 48MB.   For disks with
4707c478bd9Sstevel@tonic-gate 		 * non-EFI labels, most geometries result in cylinder
4717c478bd9Sstevel@tonic-gate 		 * groups of around 40 - 50 MB, so we arbitrarily choose
4727c478bd9Sstevel@tonic-gate 		 * 48MB for disks with EFI labels.  mkfs will reduce
4737c478bd9Sstevel@tonic-gate 		 * cylinders per group even further if necessary.
4747c478bd9Sstevel@tonic-gate 		 */
4767c478bd9Sstevel@tonic-gate 		cpg = 16;
4777c478bd9Sstevel@tonic-gate 		nsectors = 128;
4787c478bd9Sstevel@tonic-gate 		ntracks = 48;
4807c478bd9Sstevel@tonic-gate 		/*
4817c478bd9Sstevel@tonic-gate 		 * mkfs produces peculiar results for file systems
4827c478bd9Sstevel@tonic-gate 		 * that are smaller than one cylinder so don't allow
4837c478bd9Sstevel@tonic-gate 		 * them to be created (this check is only made for
4847c478bd9Sstevel@tonic-gate 		 * disks with EFI labels.  Eventually, it should probably
4857c478bd9Sstevel@tonic-gate 		 * be enforced for all disks.)
4867c478bd9Sstevel@tonic-gate 		 */
4887c478bd9Sstevel@tonic-gate 		if (fssize < nsectors * ntracks) {
4897c478bd9Sstevel@tonic-gate 			fatal(gettext(
4907c478bd9Sstevel@tonic-gate 			    "file system size must be at least %d sectors"),
4917c478bd9Sstevel@tonic-gate 			    nsectors * ntracks);
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	if (fssize > INT_MAX)
4967c478bd9Sstevel@tonic-gate 		Tflag = 1;
4987c478bd9Sstevel@tonic-gate 	/*
4997c478bd9Sstevel@tonic-gate 	 * If the user requested that the file system be set up for
5007c478bd9Sstevel@tonic-gate 	 * eventual growth to over a terabyte, or if it's already greater
5017c478bd9Sstevel@tonic-gate 	 * than a terabyte, set the inode density (nbpi) to MIN_MTB_DENSITY
5027c478bd9Sstevel@tonic-gate 	 * (unless the user has specified a larger nbpi), set the frag size
5037c478bd9Sstevel@tonic-gate 	 * equal to the block size, and set the cylinders-per-group value
5047c478bd9Sstevel@tonic-gate 	 * passed to mkfs to -1, which tells mkfs to make cylinder groups
5057c478bd9Sstevel@tonic-gate 	 * as large as possible.
5067c478bd9Sstevel@tonic-gate 	 */
5077c478bd9Sstevel@tonic-gate 	if (Tflag) {
5087c478bd9Sstevel@tonic-gate 		if (density < MIN_MTB_DENSITY)
5097c478bd9Sstevel@tonic-gate 			density = MIN_MTB_DENSITY;
5107c478bd9Sstevel@tonic-gate 		fsize = bsize;
5117c478bd9Sstevel@tonic-gate 		cpg = -1; 	/* says make cyl groups as big as possible */
5127c478bd9Sstevel@tonic-gate 	} else {
5137c478bd9Sstevel@tonic-gate 		if (fsize == 0)
5147c478bd9Sstevel@tonic-gate 			fsize = DESFRAGSIZE;
5157c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	if (!POWEROF2(fsize)) {
5187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5197c478bd9Sstevel@tonic-gate 		    "newfs: fragment size must a power of 2, not %d\n"), fsize);
5207c478bd9Sstevel@tonic-gate 		fsize = bsize/8;
5217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5227c478bd9Sstevel@tonic-gate 		    "newfs: fragsize reset to %ld\n"), fsize);
5237c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 	/*
5267c478bd9Sstevel@tonic-gate 	 * The file system is limited in size by the fragment size.
5277c478bd9Sstevel@tonic-gate 	 * The number of fragments in the file system must fit into
5287c478bd9Sstevel@tonic-gate 	 * a signed 32-bit quantity, so the number of sectors in the
5297c478bd9Sstevel@tonic-gate 	 * file system is INT_MAX * the number of sectors in a frag.
5307c478bd9Sstevel@tonic-gate 	 */
5327c478bd9Sstevel@tonic-gate 	max_possible_fssize = ((uint64_t)fsize)/DEV_BSIZE * INT_MAX;
5337c478bd9Sstevel@tonic-gate 	if (fssize > max_possible_fssize)
5347c478bd9Sstevel@tonic-gate 		fssize = max_possible_fssize;
5367c478bd9Sstevel@tonic-gate 	/*
5377c478bd9Sstevel@tonic-gate 	 * Now fssize is the final size of the file system (in sectors).
5387c478bd9Sstevel@tonic-gate 	 * If it's less than what the user requested, print a message.
5397c478bd9Sstevel@tonic-gate 	 */
5407c478bd9Sstevel@tonic-gate 	if (fssize < req_fssize) {
5417c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5427c478bd9Sstevel@tonic-gate 		    "newfs: requested size of %s disk blocks is too large.\n"),
5437c478bd9Sstevel@tonic-gate 		    req_fssize_str);
5447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5457c478bd9Sstevel@tonic-gate 		    "newfs: Resetting size to %lld\n"), fssize);
5467c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * fssize now equals the size (in sectors) of the file system
5507c478bd9Sstevel@tonic-gate 	 * that will be created.
5517c478bd9Sstevel@tonic-gate 	 */
5537c478bd9Sstevel@tonic-gate 	/* XXX - following defaults are both here and in mkfs */
5547c478bd9Sstevel@tonic-gate 	if (density <= 0) {
5557c478bd9Sstevel@tonic-gate 		if (fssize < GBSEC)
5567c478bd9Sstevel@tonic-gate 			density = MINDENSITY;
5577c478bd9Sstevel@tonic-gate 		else
5587c478bd9Sstevel@tonic-gate 			density = (int)((((longlong_t)fssize + (GBSEC - 1)) /
559d50c8f90Svsakar 			    GBSEC) * MINDENSITY);
5607c478bd9Sstevel@tonic-gate 		if (density <= 0)
5617c478bd9Sstevel@tonic-gate 			density = MINDENSITY;
5627c478bd9Sstevel@tonic-gate 		if (density > MAXDEFDENSITY)
5637c478bd9Sstevel@tonic-gate 			density = MAXDEFDENSITY;
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 	if (cpg == 0) {
5667c478bd9Sstevel@tonic-gate 		/*
5677c478bd9Sstevel@tonic-gate 		 * maxcpg calculation adapted from mkfs
5687c478bd9Sstevel@tonic-gate 		 * In the case of disks with EFI labels, cpg has
5697c478bd9Sstevel@tonic-gate 		 * already been set, so we won't enter this code.
5707c478bd9Sstevel@tonic-gate 		 */
5717c478bd9Sstevel@tonic-gate 		long maxcpg, maxipg;
5737c478bd9Sstevel@tonic-gate 		maxipg = roundup(bsize * NBBY / 3,
5747c478bd9Sstevel@tonic-gate 		    bsize / sizeof (struct inode));
5757c478bd9Sstevel@tonic-gate 		maxcpg = (bsize - sizeof (struct cg) - howmany(maxipg, NBBY)) /
5767c478bd9Sstevel@tonic-gate 		    (sizeof (long) + nrpos * sizeof (short) +
577d50c8f90Svsakar 		    nsectors / (MAXFRAG * NBBY));
5787c478bd9Sstevel@tonic-gate 		cpg = (fssize / GBSEC) * 32;
5797c478bd9Sstevel@tonic-gate 		if (cpg > maxcpg)
5807c478bd9Sstevel@tonic-gate 			cpg = maxcpg;
5817c478bd9Sstevel@tonic-gate 		if (cpg <= 0)
5827c478bd9Sstevel@tonic-gate 			cpg = MINCPG;
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 	if (minfree < 0) {
585db60a39dSvk 		minfree = (int)(((float)MINFREESEC / fssize) * 100);
5867c478bd9Sstevel@tonic-gate 		if (minfree > 10)
5877c478bd9Sstevel@tonic-gate 			minfree = 10;
5887c478bd9Sstevel@tonic-gate 		if (minfree <= 0)
5897c478bd9Sstevel@tonic-gate 			minfree = 1;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate #ifdef i386	/* Bug 1170182 */
5927c478bd9Sstevel@tonic-gate 	if (ntracks > 32 && (ntracks % 16) != 0) {
5937c478bd9Sstevel@tonic-gate 		ntracks -= (ntracks % 16);
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate #endif
5967c478bd9Sstevel@tonic-gate 	/*
5977c478bd9Sstevel@tonic-gate 	 * Confirmation
5987c478bd9Sstevel@tonic-gate 	 */
5997c478bd9Sstevel@tonic-gate 	if (isatty(fileno(stdin)) && !Nflag) {
6007c478bd9Sstevel@tonic-gate 		/*
6017c478bd9Sstevel@tonic-gate 		 * If we can read a valid superblock, report the mount
6027c478bd9Sstevel@tonic-gate 		 * point on which this filesystem was last mounted.
6037c478bd9Sstevel@tonic-gate 		 */
6047c478bd9Sstevel@tonic-gate 		if (((sbp = read_sb(special)) != 0) &&
6057c478bd9Sstevel@tonic-gate 		    (*sbp->fs_fsmnt != '\0')) {
6067c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
6077c478bd9Sstevel@tonic-gate 			    "newfs: %s last mounted as %s\n"),
6087c478bd9Sstevel@tonic-gate 			    special, sbp->fs_fsmnt);
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
6117c478bd9Sstevel@tonic-gate 		    "newfs: construct a new file system %s: (y/n)? "),
6127c478bd9Sstevel@tonic-gate 		    special);
6137c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
6147c478bd9Sstevel@tonic-gate 		if (!yes())
6157c478bd9Sstevel@tonic-gate 			exit(0);
6167c478bd9Sstevel@tonic-gate 	}
6186451fdbcSvsakar 	dprintf(("DeBuG newfs : nsect=%d ntrak=%d cpg=%d\n",
619d50c8f90Svsakar 	    nsectors, ntracks, cpg));
6207c478bd9Sstevel@tonic-gate 	/*
6217c478bd9Sstevel@tonic-gate 	 * If alternates-per-cylinder is ever implemented:
6227c478bd9Sstevel@tonic-gate 	 * need to get apc from dp->d_apc if no -a switch???
6237c478bd9Sstevel@tonic-gate 	 */
624134a1f4eSCasper H.S. Dik 	(void) snprintf(cmd, sizeof (cmd), "mkfs -F ufs "
625db60a39dSvk 	    "%s%s%s%s %lld %d %d %d %d %d %d %d %d %s %d %d %d %d %s",
626355d6bb5Sswilcox 	    Nflag ? "-o N " : "", binary_sb ? "-o calcbinsb " : "",
627355d6bb5Sswilcox 	    text_sb ? "-o calcsb " : "", special,
6287c478bd9Sstevel@tonic-gate 	    fssize, nsectors, ntracks, bsize, fsize, cpg, minfree, rpm/60,
6297c478bd9Sstevel@tonic-gate 	    density, optim == FS_OPTSPACE ? "s" : "t", apc, rot, nrpos,
6307c478bd9Sstevel@tonic-gate 	    maxcontig, Tflag ? "y" : "n");
6317c478bd9Sstevel@tonic-gate 	if (verbose) {
6327c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", cmd);
6337c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	exenv();
6367c478bd9Sstevel@tonic-gate 	if (status = system(cmd))
6377c478bd9Sstevel@tonic-gate 		exit(status >> 8);
6387c478bd9Sstevel@tonic-gate 	if (Nflag)
6397c478bd9Sstevel@tonic-gate 		exit(0);
640db60a39dSvk 	(void) snprintf(cmd, sizeof (cmd), "/usr/sbin/fsirand %s", special);
6417c478bd9Sstevel@tonic-gate 	if (notrand(special) && (status = system(cmd)) != 0)
6427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6437c478bd9Sstevel@tonic-gate 		    gettext("%s: failed, status = %d\n"),
6447c478bd9Sstevel@tonic-gate 		    cmd, status);
6457c478bd9Sstevel@tonic-gate 	return (0);
6467c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate static void
exenv(void)6497c478bd9Sstevel@tonic-gate exenv(void)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	char *epath;				/* executable file path */
6527c478bd9Sstevel@tonic-gate 	char *cpath;				/* current path */
6547c478bd9Sstevel@tonic-gate 	if ((cpath = getenv("PATH")) == NULL) {
6557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("newfs: no PATH in env\n"));
6567c478bd9Sstevel@tonic-gate 		/*
6577c478bd9Sstevel@tonic-gate 		 * Background: the Bourne shell interpolates "." into
6587c478bd9Sstevel@tonic-gate 		 * the path where said path starts with a colon, ends
6597c478bd9Sstevel@tonic-gate 		 * with a colon, or has two adjacent colons.  Thus,
6607c478bd9Sstevel@tonic-gate 		 * the path ":/sbin::/usr/sbin:" is equivalent to
6617c478bd9Sstevel@tonic-gate 		 * ".:/sbin:.:/usr/sbin:.".  Now, we have no cpath,
6627c478bd9Sstevel@tonic-gate 		 * and epath ends in a colon (to make for easy
6637c478bd9Sstevel@tonic-gate 		 * catenation in the normal case).  By the above, if
6647c478bd9Sstevel@tonic-gate 		 * we use "", then "." becomes part of path.  That's
6657c478bd9Sstevel@tonic-gate 		 * bad, so use CPATH (which is just a duplicate of some
6667c478bd9Sstevel@tonic-gate 		 * element in EPATH).  No point in opening ourselves
6677c478bd9Sstevel@tonic-gate 		 * up to a Trojan horse attack when we don't have to....
6687c478bd9Sstevel@tonic-gate 		 */
6697c478bd9Sstevel@tonic-gate 		cpath = CPATH;
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 	if ((epath = malloc(strlen(EPATH) + strlen(cpath) + 1)) == NULL) {
6727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("newfs: malloc failed\n"));
6737c478bd9Sstevel@tonic-gate 		exit(1);
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 	(void) strcpy(epath, EPATH);
6767c478bd9Sstevel@tonic-gate 	(void) strcat(epath, cpath);
6777c478bd9Sstevel@tonic-gate 	if (putenv(epath) < 0) {
6787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("newfs: putenv failed\n"));
6797c478bd9Sstevel@tonic-gate 		exit(1);
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate static int
yes(void)6847c478bd9Sstevel@tonic-gate yes(void)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	int	i, b;
6887c478bd9Sstevel@tonic-gate 	i = b = getchar();
6897c478bd9Sstevel@tonic-gate 	while (b != '\n' && b != '\0' && b != EOF)
6907c478bd9Sstevel@tonic-gate 		b = getchar();
6917c478bd9Sstevel@tonic-gate 	return (i == 'y');
6927c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate /*
6957c478bd9Sstevel@tonic-gate  * xxx Caller must run fmt through gettext(3) for us, if we ever
6967c478bd9Sstevel@tonic-gate  * xxx go the i18n route....
6977c478bd9Sstevel@tonic-gate  */
6987c478bd9Sstevel@tonic-gate static void
fatal(char * fmt,...)6997c478bd9Sstevel@tonic-gate fatal(char *fmt, ...)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	va_list pvar;
7037c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "newfs: ");
7047c478bd9Sstevel@tonic-gate 	va_start(pvar, fmt);
7057c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, pvar);
7067c478bd9Sstevel@tonic-gate 	va_end(pvar);
7077c478bd9Sstevel@tonic-gate 	(void) putc('\n', stderr);
7087c478bd9Sstevel@tonic-gate 	exit(10);
7097c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate static diskaddr_t
getdiskbydev(char * disk)7127c478bd9Sstevel@tonic-gate getdiskbydev(char *disk)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate 	struct dk_geom g;
7157c478bd9Sstevel@tonic-gate 	struct dk_cinfo ci;
716060ca14cSJim Rice 	struct dk_minfo info;
7177c478bd9Sstevel@tonic-gate 	diskaddr_t actual_size;
7187c478bd9Sstevel@tonic-gate 	int fd;
7207c478bd9Sstevel@tonic-gate 	if ((fd = open64(disk, 0)) < 0) {
7217c478bd9Sstevel@tonic-gate 		perror(disk);
7227c478bd9Sstevel@tonic-gate 		exit(1);
7237c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 	/*
7267c478bd9Sstevel@tonic-gate 	 * get_device_size() determines the actual size of the
7277c478bd9Sstevel@tonic-gate 	 * device, and also the disk's attributes, such as geometry.
7287c478bd9Sstevel@tonic-gate 	 */
7297c478bd9Sstevel@tonic-gate 	actual_size = get_device_size(fd, disk);
7317c478bd9Sstevel@tonic-gate 	if (label_type == LABEL_TYPE_VTOC) {
7334d594c33Svsakar 		/*
7348b7c2cdfSws 		 * Geometry information does not make sense for removable or
7358b7c2cdfSws 		 * hotpluggable media anyway, so indicate mkfs to use EFI
7368b7c2cdfSws 		 * default parameters.
7374d594c33Svsakar 		 */
7384d594c33Svsakar 		if (ioctl(fd, DKIOCREMOVABLE, &isremovable)) {
7394d594c33Svsakar 			dprintf(("DeBuG newfs : Unable to determine if %s is"
7404d594c33Svsakar 			    " Removable Media. Proceeding with system"
7414d594c33Svsakar 			    " determined parameters.\n", disk));
7424d594c33Svsakar 			isremovable = 0;
7438b7c2cdfSws 		}
745060ca14cSJim Rice 		/* If removable check if a floppy disk */
746060ca14cSJim Rice 		if (isremovable) {
747060ca14cSJim Rice 			if (ioctl(fd, DKIOCGMEDIAINFO, &info)) {
748060ca14cSJim Rice 				dprintf(("DeBuG newfs : Unable to get media"
749060ca14cSJim Rice 				    " info from %s.\n", disk));
750060ca14cSJim Rice 			} else {
751060ca14cSJim Rice 				if (info.dki_media_type == DK_FLOPPY) {
752060ca14cSJim Rice 					isremovable = 0;
753060ca14cSJim Rice 				}
754060ca14cSJim Rice 			}
755060ca14cSJim Rice 		}
756060ca14cSJim Rice 
7578b7c2cdfSws 		if (ioctl(fd, DKIOCHOTPLUGGABLE, &ishotpluggable)) {
7588b7c2cdfSws 			dprintf(("DeBuG newfs : Unable to determine if %s is"
7598b7c2cdfSws 			    " Hotpluggable Media. Proceeding with system"
7608b7c2cdfSws 			    " determined parameters.\n", disk));
7618b7c2cdfSws 			ishotpluggable = 0;
7628b7c2cdfSws 		}
7648b7c2cdfSws 		if ((isremovable || ishotpluggable) && !Tflag)
7654d594c33Svsakar 			use_efi_dflts = 1;
7677c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &g))
7687c478bd9Sstevel@tonic-gate 			fatal(gettext(
7697c478bd9Sstevel@tonic-gate 			    "%s: Unable to read Disk geometry"), disk);
770342440ecSPrasad Singamsetty 		if ((((diskaddr_t)g.dkg_ncyl * g.dkg_nhead *
771342440ecSPrasad Singamsetty 		    g.dkg_nsect) > CHSLIMIT) && !Tflag) {
7726451fdbcSvsakar 			use_efi_dflts = 1;
7736451fdbcSvsakar 		}
774342440ecSPrasad Singamsetty 		dprintf(("DeBuG newfs : geom=%llu, CHSLIMIT=%d "
7758b7c2cdfSws 		    "isremovable = %d ishotpluggable = %d use_efi_dflts = %d\n",
776342440ecSPrasad Singamsetty 		    (diskaddr_t)g.dkg_ncyl * g.dkg_nhead * g.dkg_nsect,
777342440ecSPrasad Singamsetty 		    CHSLIMIT, isremovable, ishotpluggable, use_efi_dflts));
7786451fdbcSvsakar 		/*
7796d24e334Svsakar 		 * The ntracks that is passed to mkfs is decided here based
7806d24e334Svsakar 		 * on 'use_efi_dflts' and whether ntracks was specified as a
7816d24e334Svsakar 		 * command line parameter to newfs.
7826d24e334Svsakar 		 * If ntracks of -1 is passed to mkfs, mkfs uses DEF_TRACKS_EFI
7836d24e334Svsakar 		 * and DEF_SECTORS_EFI for ntracks and nsectors respectively.
7846451fdbcSvsakar 		 */
7857c478bd9Sstevel@tonic-gate 		if (nsectors == 0)
7866d24e334Svsakar 			nsectors = g.dkg_nsect;
7877c478bd9Sstevel@tonic-gate 		if (ntracks == 0)
7886451fdbcSvsakar 			ntracks = use_efi_dflts ? -1 : g.dkg_nhead;
7897c478bd9Sstevel@tonic-gate 		if (rpm == 0)
7907c478bd9Sstevel@tonic-gate 			rpm = ((int)g.dkg_rpm <= 0) ? 3600: g.dkg_rpm;
7917c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 	if (bsize == 0)
7947c478bd9Sstevel@tonic-gate 		bsize = DESBLKSIZE;
7957c478bd9Sstevel@tonic-gate 	/*
7967c478bd9Sstevel@tonic-gate 	 * Adjust maxcontig by the device's maxtransfer. If maxtransfer
7977c478bd9Sstevel@tonic-gate 	 * information is not available, default to the min of a MB and
7987c478bd9Sstevel@tonic-gate 	 * maxphys.
7997c478bd9Sstevel@tonic-gate 	 */
8007c478bd9Sstevel@tonic-gate 	if (maxcontig == -1 && ioctl(fd, DKIOCINFO, &ci) == 0) {
8017c478bd9Sstevel@tonic-gate 		maxcontig = ci.dki_maxtransfer * DEV_BSIZE;
8027c478bd9Sstevel@tonic-gate 		if (maxcontig < 0) {
8037c478bd9Sstevel@tonic-gate 			int	error, gotit, maxphys;
8047c478bd9Sstevel@tonic-gate 			gotit = fsgetmaxphys(&maxphys, &error);
8067c478bd9Sstevel@tonic-gate 			/*
8077c478bd9Sstevel@tonic-gate 			 * If we cannot get the maxphys value, default
8087c478bd9Sstevel@tonic-gate 			 * to ufs_maxmaxphys (MB).
8097c478bd9Sstevel@tonic-gate 			 */
8107c478bd9Sstevel@tonic-gate 			if (gotit) {
8117c478bd9Sstevel@tonic-gate 				maxcontig = MIN(maxphys, MB);
8127c478bd9Sstevel@tonic-gate 			} else {
8137c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
8147c478bd9Sstevel@tonic-gate "Warning: Could not get system value for maxphys. The value for maxcontig\n"
8157c478bd9Sstevel@tonic-gate "will default to 1MB.\n"));
8167c478bd9Sstevel@tonic-gate 			maxcontig = MB;
8177c478bd9Sstevel@tonic-gate 			}
8187c478bd9Sstevel@tonic-gate 		}
8197c478bd9Sstevel@tonic-gate 		maxcontig /= bsize;
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 	(void) close(fd);
8227c478bd9Sstevel@tonic-gate 	return (actual_size);
8237c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate /*
8267c478bd9Sstevel@tonic-gate  * Figure out how big the partition we're dealing with is.
8277c478bd9Sstevel@tonic-gate  */
8287c478bd9Sstevel@tonic-gate static diskaddr_t
get_device_size(int fd,char * name)8297c478bd9Sstevel@tonic-gate get_device_size(int fd, char *name)
8307c478bd9Sstevel@tonic-gate {
831342440ecSPrasad Singamsetty 	struct extvtoc vtoc;
8327c478bd9Sstevel@tonic-gate 	dk_gpt_t *efi_vtoc;
8337c478bd9Sstevel@tonic-gate 	diskaddr_t	slicesize;
835342440ecSPrasad Singamsetty 	int index = read_extvtoc(fd, &vtoc);
8377c478bd9Sstevel@tonic-gate 	if (index >= 0) {
8387c478bd9Sstevel@tonic-gate 		label_type = LABEL_TYPE_VTOC;
8397c478bd9Sstevel@tonic-gate 	} else {
8407c478bd9Sstevel@tonic-gate 		if (index == VT_ENOTSUP || index == VT_ERROR) {
8417c478bd9Sstevel@tonic-gate 			/* it might be an EFI label */
8427c478bd9Sstevel@tonic-gate 			index = efi_alloc_and_read(fd, &efi_vtoc);
8437c478bd9Sstevel@tonic-gate 			if (index >= 0)
8447c478bd9Sstevel@tonic-gate 				label_type = LABEL_TYPE_EFI;
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 	if (index < 0) {
8497c478bd9Sstevel@tonic-gate 		/*
8507c478bd9Sstevel@tonic-gate 		 * Since both attempts to read the label failed, we're
8517c478bd9Sstevel@tonic-gate 		 * going to fall back to a brute force approach to
8527c478bd9Sstevel@tonic-gate 		 * determining the device's size:  see how far out we can
8537c478bd9Sstevel@tonic-gate 		 * perform reads on the device.
8547c478bd9Sstevel@tonic-gate 		 */
8567c478bd9Sstevel@tonic-gate 		slicesize = brute_force_get_device_size(fd);
8577c478bd9Sstevel@tonic-gate 		if (slicesize == 0) {
8587c478bd9Sstevel@tonic-gate 			switch (index) {
8597c478bd9Sstevel@tonic-gate 			case VT_ERROR:
8607c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
8617c478bd9Sstevel@tonic-gate 				    "newfs: %s: %s\n"), name, strerror(errno));
8627c478bd9Sstevel@tonic-gate 				exit(10);
8637c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
8647c478bd9Sstevel@tonic-gate 			case VT_EIO:
8657c478bd9Sstevel@tonic-gate 				fatal(gettext(
8667c478bd9Sstevel@tonic-gate 				    "%s: I/O error accessing VTOC"), name);
8677c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
8687c478bd9Sstevel@tonic-gate 			case VT_EINVAL:
8697c478bd9Sstevel@tonic-gate 				fatal(gettext(
8707c478bd9Sstevel@tonic-gate 				    "%s: Invalid field in VTOC"), name);
8717c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
8727c478bd9Sstevel@tonic-gate 			default:
8737c478bd9Sstevel@tonic-gate 				fatal(gettext(
8747c478bd9Sstevel@tonic-gate 				    "%s: unknown error accessing VTOC"),
8757c478bd9Sstevel@tonic-gate 				    name);
8767c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
8777c478bd9Sstevel@tonic-gate 			}
8787c478bd9Sstevel@tonic-gate 		} else {
8797c478bd9Sstevel@tonic-gate 			label_type = LABEL_TYPE_OTHER;
8807c478bd9Sstevel@tonic-gate 		}
8817c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate 	if (label_type == LABEL_TYPE_EFI) {
8847c478bd9Sstevel@tonic-gate 		slicesize = efi_vtoc->efi_parts[index].p_size;
8857c478bd9Sstevel@tonic-gate 		efi_free(efi_vtoc);
8867c478bd9Sstevel@tonic-gate 	} else if (label_type == LABEL_TYPE_VTOC) {
887342440ecSPrasad Singamsetty 		slicesize = vtoc.v_part[index].p_size;
8887c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 	return (slicesize);
8917c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate /*
8947c478bd9Sstevel@tonic-gate  * brute_force_get_device_size
8957c478bd9Sstevel@tonic-gate  *
8967c478bd9Sstevel@tonic-gate  * Determine the size of the device by seeing how far we can
8977c478bd9Sstevel@tonic-gate  * read.  Doing an llseek( , , SEEK_END) would probably work
8987c478bd9Sstevel@tonic-gate  * in most cases, but we've seen at least one third-party driver
8997c478bd9Sstevel@tonic-gate  * which doesn't correctly support the SEEK_END option when the
9007c478bd9Sstevel@tonic-gate  * the device is greater than a terabyte.
9017c478bd9Sstevel@tonic-gate  */
9037c478bd9Sstevel@tonic-gate static diskaddr_t
brute_force_get_device_size(int fd)9047c478bd9Sstevel@tonic-gate brute_force_get_device_size(int fd)
9057c478bd9Sstevel@tonic-gate {
9067c478bd9Sstevel@tonic-gate 	diskaddr_t	min_fail = 0;
9077c478bd9Sstevel@tonic-gate 	diskaddr_t	max_succeed = 0;
9087c478bd9Sstevel@tonic-gate 	diskaddr_t	cur_db_off;
9097c478bd9Sstevel@tonic-gate 	char 		buf[DEV_BSIZE];
9117c478bd9Sstevel@tonic-gate 	/*
9127c478bd9Sstevel@tonic-gate 	 * First, see if we can read the device at all, just to
9137c478bd9Sstevel@tonic-gate 	 * eliminate errors that have nothing to do with the
9147c478bd9Sstevel@tonic-gate 	 * device's size.
9157c478bd9Sstevel@tonic-gate 	 */
9177c478bd9Sstevel@tonic-gate 	if (((llseek(fd, (offset_t)0, SEEK_SET)) == -1) ||
9187c478bd9Sstevel@tonic-gate 	    ((read(fd, buf, DEV_BSIZE)) == -1))
9197c478bd9Sstevel@tonic-gate 		return (0);  /* can't determine size */
9217c478bd9Sstevel@tonic-gate 	/*
9227c478bd9Sstevel@tonic-gate 	 * Now, go sequentially through the multiples of 4TB
9237c478bd9Sstevel@tonic-gate 	 * to find the first read that fails (this isn't strictly
9247c478bd9Sstevel@tonic-gate 	 * the most efficient way to find the actual size if the
9257c478bd9Sstevel@tonic-gate 	 * size really could be anything between 0 and 2**64 bytes.
9267c478bd9Sstevel@tonic-gate 	 * We expect the sizes to be less than 16 TB for some time,
9277c478bd9Sstevel@tonic-gate 	 * so why do a bunch of reads that are larger than that?
9287c478bd9Sstevel@tonic-gate 	 * However, this algorithm *will* work for sizes of greater
9297c478bd9Sstevel@tonic-gate 	 * than 16 TB.  We're just not optimizing for those sizes.)
9307c478bd9Sstevel@tonic-gate 	 */
9327c478bd9Sstevel@tonic-gate 	for (cur_db_off = SECTORS_PER_TERABYTE * 4;
9337c478bd9Sstevel@tonic-gate 	    min_fail == 0 && cur_db_off < FS_SIZE_UPPER_LIMIT;
9347c478bd9Sstevel@tonic-gate 	    cur_db_off += 4 * SECTORS_PER_TERABYTE) {
9357c478bd9Sstevel@tonic-gate 		if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE),
9367c478bd9Sstevel@tonic-gate 		    SEEK_SET)) == -1) ||
9377c478bd9Sstevel@tonic-gate 		    ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE))
9387c478bd9Sstevel@tonic-gate 			min_fail = cur_db_off;
9397c478bd9Sstevel@tonic-gate 		else
9407c478bd9Sstevel@tonic-gate 			max_succeed = cur_db_off;
9417c478bd9Sstevel@tonic-gate 	}
9437c478bd9Sstevel@tonic-gate 	if (min_fail == 0)
9447c478bd9Sstevel@tonic-gate 		return (0);
9467c478bd9Sstevel@tonic-gate 	/*
9477c478bd9Sstevel@tonic-gate 	 * We now know that the size of the device is less than
9487c478bd9Sstevel@tonic-gate 	 * min_fail and greater than or equal to max_succeed.  Now
9497c478bd9Sstevel@tonic-gate 	 * keep splitting the difference until the actual size in
9507c478bd9Sstevel@tonic-gate 	 * sectors in known.  We also know that the difference
9517c478bd9Sstevel@tonic-gate 	 * between max_succeed and min_fail at this time is
9527c478bd9Sstevel@tonic-gate 	 * 4 * SECTORS_PER_TERABYTE, which is a power of two, which
9537c478bd9Sstevel@tonic-gate 	 * simplifies the math below.
9547c478bd9Sstevel@tonic-gate 	 */
9567c478bd9Sstevel@tonic-gate 	while (min_fail - max_succeed > 1) {
9577c478bd9Sstevel@tonic-gate 		cur_db_off = max_succeed + (min_fail - max_succeed)/2;
9587c478bd9Sstevel@tonic-gate 		if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE),
9597c478bd9Sstevel@tonic-gate 		    SEEK_SET)) == -1) ||
9607c478bd9Sstevel@tonic-gate 		    ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE))
9617c478bd9Sstevel@tonic-gate 			min_fail = cur_db_off;
9627c478bd9Sstevel@tonic-gate 		else
9637c478bd9Sstevel@tonic-gate 			max_succeed = cur_db_off;
9647c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 	/* the size is the last successfully read sector offset plus one */
9677c478bd9Sstevel@tonic-gate 	return (max_succeed + 1);
9687c478bd9Sstevel@tonic-gate }
9707c478bd9Sstevel@tonic-gate /*
9717c478bd9Sstevel@tonic-gate  * validate_size
9727c478bd9Sstevel@tonic-gate  *
9737c478bd9Sstevel@tonic-gate  * Return 1 if the device appears to be at least "size" sectors long.
9747c478bd9Sstevel@tonic-gate  * Return 0 if it's shorter or we can't read it.
9757c478bd9Sstevel@tonic-gate  */
9777c478bd9Sstevel@tonic-gate static int
validate_size(char * disk,diskaddr_t size)9787c478bd9Sstevel@tonic-gate validate_size(char *disk, diskaddr_t size)
9797c478bd9Sstevel@tonic-gate {
9807c478bd9Sstevel@tonic-gate 	char 		buf[DEV_BSIZE];
9817c478bd9Sstevel@tonic-gate 	int fd, rc;
9837c478bd9Sstevel@tonic-gate 	if ((fd = open64(disk, O_RDONLY)) < 0) {
9847c478bd9Sstevel@tonic-gate 		perror(disk);
9857c478bd9Sstevel@tonic-gate 		exit(1);
9867c478bd9Sstevel@tonic-gate 	}
9887c478bd9Sstevel@tonic-gate 	if ((llseek(fd, (offset_t)((size - 1) * DEV_BSIZE), SEEK_SET) == -1) ||
9897c478bd9Sstevel@tonic-gate 	    (read(fd, buf, DEV_BSIZE)) != DEV_BSIZE)
9907c478bd9Sstevel@tonic-gate 		rc = 0;
9917c478bd9Sstevel@tonic-gate 	else
9927c478bd9Sstevel@tonic-gate 		rc = 1;
9937c478bd9Sstevel@tonic-gate 	(void) close(fd);
9947c478bd9Sstevel@tonic-gate 	return (rc);
9957c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate /*
9987c478bd9Sstevel@tonic-gate  * read_sb(char * rawdev) - Attempt to read the superblock from a raw device
9997c478bd9Sstevel@tonic-gate  *
10007c478bd9Sstevel@tonic-gate  * Returns:
10017c478bd9Sstevel@tonic-gate  *	0 :
10027c478bd9Sstevel@tonic-gate  *		Could not read a valid superblock for a variety of reasons.
10037c478bd9Sstevel@tonic-gate  *		Since 'newfs' handles any fatal conditions, we're not going
10047c478bd9Sstevel@tonic-gate  *		to make any guesses as to why this is failing or what should
10057c478bd9Sstevel@tonic-gate  *		be done about it.
10067c478bd9Sstevel@tonic-gate  *
10077c478bd9Sstevel@tonic-gate  *	struct fs *:
10087c478bd9Sstevel@tonic-gate  *		A pointer to (what we think is) a valid superblock. The
10097c478bd9Sstevel@tonic-gate  *		space for the superblock is static (inside the function)
10107c478bd9Sstevel@tonic-gate  *		since we will only be reading the values from it.
10117c478bd9Sstevel@tonic-gate  */
10137c478bd9Sstevel@tonic-gate struct fs *
read_sb(char * fsdev)10147c478bd9Sstevel@tonic-gate read_sb(char *fsdev)
10157c478bd9Sstevel@tonic-gate {
10167c478bd9Sstevel@tonic-gate 	static struct fs	sblock;
10177c478bd9Sstevel@tonic-gate 	struct stat64		statb;
10187c478bd9Sstevel@tonic-gate 	int			dskfd;
10197c478bd9Sstevel@tonic-gate 	char			*bufp = NULL;
10207c478bd9Sstevel@tonic-gate 	int			bufsz = 0;
10227c478bd9Sstevel@tonic-gate 	if (stat64(fsdev, &statb) < 0)
10237c478bd9Sstevel@tonic-gate 		return (0);
10257c478bd9Sstevel@tonic-gate 	if ((dskfd = open64(fsdev, O_RDONLY)) < 0)
10267c478bd9Sstevel@tonic-gate 		return (0);
10287c478bd9Sstevel@tonic-gate 	/*
10297c478bd9Sstevel@tonic-gate 	 * We need a buffer whose size is a multiple of DEV_BSIZE in order
10307c478bd9Sstevel@tonic-gate 	 * to read from a raw device (which we were probably passed).
10317c478bd9Sstevel@tonic-gate 	 */
10327c478bd9Sstevel@tonic-gate 	bufsz = ((sizeof (sblock) / DEV_BSIZE) + 1) * DEV_BSIZE;
10337c478bd9Sstevel@tonic-gate 	if ((bufp = malloc(bufsz)) == NULL) {
10347c478bd9Sstevel@tonic-gate 		(void) close(dskfd);
10357c478bd9Sstevel@tonic-gate 		return (0);
10367c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 	if (llseek(dskfd, (offset_t)SBOFF, SEEK_SET) < 0 ||
10397c478bd9Sstevel@tonic-gate 	    read(dskfd, bufp, bufsz) < 0) {
10407c478bd9Sstevel@tonic-gate 		(void) close(dskfd);
10417c478bd9Sstevel@tonic-gate 		free(bufp);
10427c478bd9Sstevel@tonic-gate 		return (0);
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 	(void) close(dskfd);	/* Done with the file */
10467c478bd9Sstevel@tonic-gate 	(void) memcpy(&sblock, bufp, sizeof (sblock));
10477c478bd9Sstevel@tonic-gate 	free(bufp);	/* Don't need this anymore */
10497c478bd9Sstevel@tonic-gate 	if (((sblock.fs_magic != FS_MAGIC) &&
10507c478bd9Sstevel@tonic-gate 	    (sblock.fs_magic != MTB_UFS_MAGIC)) ||
10517c478bd9Sstevel@tonic-gate 	    sblock.fs_ncg < 1 || sblock.fs_cpg < 1)
10527c478bd9Sstevel@tonic-gate 		return (0);
10547c478bd9Sstevel@tonic-gate 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
10557c478bd9Sstevel@tonic-gate 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
10567c478bd9Sstevel@tonic-gate 		return (0);
10587c478bd9Sstevel@tonic-gate 	if (sblock.fs_sbsize < 0 || sblock.fs_sbsize > SBSIZE)
10597c478bd9Sstevel@tonic-gate 		return (0);
10617c478bd9Sstevel@tonic-gate 	return (&sblock);
10627c478bd9Sstevel@tonic-gate }
10647c478bd9Sstevel@tonic-gate /*
10657c478bd9Sstevel@tonic-gate  * Read the UFS file system on the raw device SPECIAL.  If it does not
10667c478bd9Sstevel@tonic-gate  * appear to be a UFS file system, return non-zero, indicating that
10677c478bd9Sstevel@tonic-gate  * fsirand should be called (and it will spit out an error message).
10687c478bd9Sstevel@tonic-gate  * If it is a UFS file system, take a look at the inodes in the first
10697c478bd9Sstevel@tonic-gate  * cylinder group.  If they appear to be randomized (non-zero), return
10707c478bd9Sstevel@tonic-gate  * zero, which will cause fsirand to not be called.  If the inode generation
10717c478bd9Sstevel@tonic-gate  * counts are all zero, then we must call fsirand, so return non-zero.
10727c478bd9Sstevel@tonic-gate  */
10747c478bd9Sstevel@tonic-gate #define	RANDOMIZED	0
10757c478bd9Sstevel@tonic-gate #define	NOT_RANDOMIZED	1
10777c478bd9Sstevel@tonic-gate static int
notrand(char * special)10787c478bd9Sstevel@tonic-gate notrand(char *special)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	long fsbuf[SBSIZE / sizeof (long)];
10817c478bd9Sstevel@tonic-gate 	struct dinode dibuf[MAXBSIZE/sizeof (struct dinode)];
10827c478bd9Sstevel@tonic-gate 	struct fs *fs;
10837c478bd9Sstevel@tonic-gate 	struct dinode *dip;
10847c478bd9Sstevel@tonic-gate 	offset_t seekaddr;
10857c478bd9Sstevel@tonic-gate 	int bno, inum;
10867c478bd9Sstevel@tonic-gate 	int fd;
10887c478bd9Sstevel@tonic-gate 	fs = (struct fs *)fsbuf;
10897c478bd9Sstevel@tonic-gate 	if ((fd = open64(special, 0)) == -1)
10907c478bd9Sstevel@tonic-gate 		return (NOT_RANDOMIZED);
10917c478bd9Sstevel@tonic-gate 	if (llseek(fd, (offset_t)SBLOCK * DEV_BSIZE, 0) == -1 ||
10927c478bd9Sstevel@tonic-gate 	    read(fd, (char *)fs, SBSIZE) != SBSIZE ||
10937c478bd9Sstevel@tonic-gate 	    ((fs->fs_magic != FS_MAGIC) && (fs->fs_magic != MTB_UFS_MAGIC))) {
10947c478bd9Sstevel@tonic-gate 		(void) close(fd);
10957c478bd9Sstevel@tonic-gate 		return (NOT_RANDOMIZED);
10967c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 	/* looks like a UFS file system; read the first cylinder group */
10997c478bd9Sstevel@tonic-gate 	bsize = INOPB(fs) * sizeof (struct dinode);
11007c478bd9Sstevel@tonic-gate 	inum = 0;
11017c478bd9Sstevel@tonic-gate 	while (inum < fs->fs_ipg) {
11027c478bd9Sstevel@tonic-gate 		bno = itod(fs, inum);
11037c478bd9Sstevel@tonic-gate 		seekaddr = (offset_t)fsbtodb(fs, bno) * DEV_BSIZE;
11047c478bd9Sstevel@tonic-gate 		if (llseek(fd, seekaddr, 0) == -1 ||
11057c478bd9Sstevel@tonic-gate 		    read(fd, (char *)dibuf, bsize) != bsize) {
11067c478bd9Sstevel@tonic-gate 			(void) close(fd);
11077c478bd9Sstevel@tonic-gate 			return (NOT_RANDOMIZED);
11087c478bd9Sstevel@tonic-gate 		}
11097c478bd9Sstevel@tonic-gate 		for (dip = dibuf; dip < &dibuf[INOPB(fs)]; dip++) {
11107c478bd9Sstevel@tonic-gate 			if (dip->di_gen != 0) {
11117c478bd9Sstevel@tonic-gate 				(void) close(fd);
11127c478bd9Sstevel@tonic-gate 				return (RANDOMIZED);
11137c478bd9Sstevel@tonic-gate 			}
11147c478bd9Sstevel@tonic-gate 			inum++;
11157c478bd9Sstevel@tonic-gate 		}
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 	(void) close(fd);
11187c478bd9Sstevel@tonic-gate 	return (NOT_RANDOMIZED);
11197c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate static void
usage(void)11227c478bd9Sstevel@tonic-gate usage(void)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
11257c478bd9Sstevel@tonic-gate 	    "usage: newfs [ -v ] [ mkfs-options ] raw-special-device\n"));
11267c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("where mkfs-options are:\n"));
11277c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
11287c478bd9Sstevel@tonic-gate 	    "\t-N do not create file system, just print out parameters\n"));
11297c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
11307c478bd9Sstevel@tonic-gate "\t-T configure file system for eventual growth to over a terabyte\n"));
11317c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-s file system size (sectors)\n"));
11327c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-b block size\n"));
11337c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-f frag size\n"));
11347c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-t tracks/cylinder\n"));
11357c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-c cylinders/group\n"));
11367c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-m minimum free space %%\n"));
11377c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
11387c478bd9Sstevel@tonic-gate 	    "\t-o optimization preference (`space' or `time')\n"));
11397c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-r revolutions/minute\n"));
11407c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-i number of bytes per inode\n"));
11417c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
11427c478bd9Sstevel@tonic-gate 	    "\t-a number of alternates per cylinder\n"));
11437c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-C maxcontig\n"));
11447c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t-d rotational delay\n"));
11457c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
11467c478bd9Sstevel@tonic-gate 	    "\t-n number of rotational positions\n"));
1147355d6bb5Sswilcox 	(void) fprintf(stderr, gettext(
1148355d6bb5Sswilcox "\t-S print a textual version of the calculated superblock to stdout\n"));
1149355d6bb5Sswilcox 	(void) fprintf(stderr, gettext(
1150355d6bb5Sswilcox "\t-B dump a binary version of the calculated superblock to stdout\n"));
11517c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate /*
11547c478bd9Sstevel@tonic-gate  * Error-detecting version of atoi(3).  Adapted from mkfs' number().
11557c478bd9Sstevel@tonic-gate  */
11567c478bd9Sstevel@tonic-gate static unsigned int
number(char * param,char * value,int flags,int def_value)11577c478bd9Sstevel@tonic-gate number(char *param, char *value, int flags, int def_value)
11587c478bd9Sstevel@tonic-gate {
11597c478bd9Sstevel@tonic-gate 	char *cs;
11607c478bd9Sstevel@tonic-gate 	int n;
11617c478bd9Sstevel@tonic-gate 	int cut = INT_MAX / 10;    /* limit to avoid overflow */
11627c478bd9Sstevel@tonic-gate 	int minus = 0;
11647c478bd9Sstevel@tonic-gate 	cs = value;
11657c478bd9Sstevel@tonic-gate 	if (*cs == '-') {
11667c478bd9Sstevel@tonic-gate 		minus = 1;
11677c478bd9Sstevel@tonic-gate 		cs += 1;
11687c478bd9Sstevel@tonic-gate 	}
11697c478bd9Sstevel@tonic-gate 	if ((*cs < '0') || (*cs > '9')) {
11707c478bd9Sstevel@tonic-gate 		goto bail_out;