Home | History | Annotate | Download | only in newfs
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * newfs: friendly front end to mkfs
     24  *
     25  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     26  * Use is subject to license terms.
     27  */
     28 
     29 #include <sys/param.h>
     30 #include <sys/types.h>
     31 #include <locale.h>
     32 #include <sys/stat.h>
     33 #include <sys/buf.h>
     34 #include <sys/fs/ufs_fs.h>
     35 #include <sys/vnode.h>
     36 #include <sys/fs/ufs_inode.h>
     37 #include <sys/sysmacros.h>
     38 
     39 #include <errno.h>
     40 #include <stdio.h>
     41 #include <string.h>
     42 #include <stdlib.h>
     43 #include <stdarg.h>
     44 #include <stdio.h>
     45 #include <fcntl.h>
     46 #include <unistd.h>
     47 #include <limits.h>
     48 #include <libintl.h>
     49 #include <sys/dkio.h>
     50 #include <sys/vtoc.h>
     51 #include <sys/mkdev.h>
     52 #include <sys/efi_partition.h>
     53 
     54 #include <fslib.h>
     55 
     56 static unsigned int number(char *, char *, int, int);
     57 static int64_t number64(char *, char *, int, int64_t);
     58 static diskaddr_t getdiskbydev(char *);
     59 static int  yes(void);
     60 static int  notrand(char *);
     61 static void usage();
     62 static diskaddr_t get_device_size(int, char *);
     63 static diskaddr_t brute_force_get_device_size(int);
     64 static int validate_size(char *disk, diskaddr_t size);
     65 static void exenv(void);
     66 static struct fs *read_sb(char *);
     67 /*PRINTFLIKE1*/
     68 static void fatal(char *fmt, ...);
     69 
     70 #define	EPATH "PATH=/usr/sbin:/sbin:"
     71 #define	CPATH "/sbin"					/* an EPATH element */
     72 #define	MB (1024 * 1024)
     73 #define	GBSEC ((1024 * 1024 * 1024) / DEV_BSIZE)	/* sectors in a GB */
     74 #define	MINFREESEC ((64 * 1024 * 1024) / DEV_BSIZE)	/* sectors in 64 MB */
     75 #define	MINCPG (16)	/* traditional */
     76 #define	MAXDEFDENSITY (8 * 1024)	/* arbitrary */
     77 #define	MINDENSITY (2 * 1024)	/* traditional */
     78 #define	MIN_MTB_DENSITY (1024 * 1024)
     79 #define	POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     80 #define	SECTORS_PER_TERABYTE	(1LL << 31)
     81 /*
     82  * The following constant specifies an upper limit for file system size
     83  * that is actually a lot bigger than we expect to support with UFS. (Since
     84  * it's specified in sectors, the file system size would be 2**44 * 512,
     85  * which is 2**53, which is 8192 Terabytes.)  However, it's useful
     86  * for checking the basic sanity of a size value that is input on the
     87  * command line.
     88  */
     89 #define	FS_SIZE_UPPER_LIMIT	0x100000000000LL
     90 
     91 /* For use with number() */
     92 #define	NR_NONE		0
     93 #define	NR_PERCENT	0x01
     94 
     95 /*
     96  * The following two constants set the default block and fragment sizes.
     97  * Both constants must be a power of 2 and meet the following constraints:
     98  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
     99  *	DEV_BSIZE <= DESFRAGSIZE <= DESBLKSIZE
    100  *	DESBLKSIZE / DESFRAGSIZE <= 8
    101  */
    102 #define	DESBLKSIZE	8192
    103 #define	DESFRAGSIZE	1024
    104 
    105 #ifdef DEBUG
    106 #define	dprintf(x)	printf x
    107 #else
    108 #define	dprintf(x)
    109 #endif
    110 
    111 static int	Nflag;		/* run mkfs without writing file system */
    112 static int	Tflag;		/* set up file system for growth to over 1 TB */
    113 static int	verbose;	/* show mkfs line before exec */
    114 static int	fsize = 0;		/* fragment size */
    115 static int	fsize_flag = 0;	/* fragment size was specified on cmd line */
    116 static int	bsize;		/* block size */
    117 static int	ntracks;	/* # tracks/cylinder */
    118 static int	ntracks_set = 0; /* true if the user specified ntracks */
    119 static int	optim = FS_OPTTIME;	/* optimization, t(ime) or s(pace) */
    120 static int	nsectors;	/* # sectors/track */
    121 static int	cpg;		/* cylinders/cylinder group */
    122 static int	cpg_set = 0;	/* true if the user specified cpg */
    123 static int	minfree = -1;	/* free space threshold */
    124 static int	rpm;		/* revolutions/minute of drive */
    125 static int	rpm_set = 0;	/* true if the user specified rpm */
    126 static int	nrpos = 8;	/* # of distinguished rotational positions */
    127 				/* 8 is the historical default */
    128 static int	nrpos_set = 0;	/* true if the user specified nrpos */
    129 static int	density = 0;	/* number of bytes per inode */
    130 static int	apc;		/* alternates per cylinder */
    131 static int	apc_set = 0;	/* true if the user specified apc */
    132 static int 	rot = -1;	/* rotational delay (msecs) */
    133 static int	rot_set = 0;	/* true if the user specified rot */
    134 static int 	maxcontig = -1;	/* maximum number of contig blocks */
    135 static int	text_sb = 0;	/* no disk changes; just final sb text dump */
    136 static int	binary_sb = 0;	/* no disk changes; just final sb binary dump */
    137 static int	label_type;	/* see types below */
    138 
    139 /*
    140  * The variable use_efi_dflts is an indicator of whether to use EFI logic
    141  * or the geometry logic in laying out the filesystem. This is decided
    142  * based on the size/type of the disk and is used only for non-EFI labeled
    143  * disks and removable media.
    144  */
    145 static int	use_efi_dflts = 0;
    146 static int	isremovable = 0;
    147 static int	ishotpluggable = 0;
    148 
    149 static char	device[MAXPATHLEN];
    150 static char	cmd[BUFSIZ];
    151 
    152 extern	char	*getfullrawname(); /* from libadm */
    153 
    154 int
    155 main(int argc, char *argv[])
    156 {
    157 	char *special, *name;
    158 	struct stat64 st;
    159 	int status;
    160 	int option;
    161 	struct fs *sbp;	/* Pointer to superblock (if present) */
    162 	diskaddr_t actual_fssize;
    163 	diskaddr_t max_possible_fssize;
    164 	diskaddr_t req_fssize = 0;
    165 	diskaddr_t fssize = 0;
    166 	char	*req_fssize_str = NULL; /* requested size argument */
    167 
    168 	(void) setlocale(LC_ALL, "");
    169 
    170 #if !defined(TEXT_DOMAIN)
    171 #define	TEXT_DOMAIN	"SYS_TEST"
    172 #endif
    173 	(void) textdomain(TEXT_DOMAIN);
    174 
    175 	opterr = 0;	/* We print our own errors, disable getopt's message */
    176 	while ((option = getopt(argc, argv,
    177 	    "vNBSs:C:d:t:o:a:b:f:c:m:n:r:i:T")) != EOF) {
    178 		switch (option) {
    179 		case 'S':
    180 			text_sb++;
    181 			break;
    182 		case 'B':
    183 			binary_sb++;
    184 			break;
    185 		case 'v':
    186 			verbose++;
    187 			break;
    188 
    189 		case 'N':
    190 			Nflag++;
    191 			break;
    192 
    193 		case 's':
    194 			/*
    195 			 * The maximum file system size is a lot smaller
    196 			 * than FS_SIZE_UPPER_LIMIT, but until we find out
    197 			 * the device size and block size, we don't know
    198 			 * what it is.  So save the requested size in a
    199 			 * string so that we can print it out later if we
    200 			 * determine it's too big.
    201 			 */
    202 			req_fssize = number64("fssize", optarg, NR_NONE,
    203 			    FS_SIZE_UPPER_LIMIT);
    204 			if (req_fssize < 1024)
    205 				fatal(gettext(
    206 				    "%s: fssize must be at least 1024"),
    207 				    optarg);
    208 			req_fssize_str = strdup(optarg);
    209 			if (req_fssize_str == NULL)
    210 				fatal(gettext(
    211 				    "Insufficient memory for string copy."));
    212 			break;
    213 
    214 		case 'C':
    215 			maxcontig = number("maxcontig", optarg, NR_NONE, -1);
    216 			if (maxcontig < 0)
    217 				fatal(gettext("%s: bad maxcontig"), optarg);
    218 			break;
    219 
    220 		case 'd':
    221 			rot = number("rotdelay", optarg, NR_NONE, 0);
    222 			rot_set = 1;
    223 			if (rot < 0 || rot > 1000)
    224 				fatal(gettext(
    225 				    "%s: bad rotational delay"), optarg);
    226 			break;
    227 
    228 		case 't':
    229 			ntracks = number("ntrack", optarg, NR_NONE, 16);
    230 			ntracks_set = 1;
    231 			if ((ntracks < 0) ||
    232 			    (ntracks > INT_MAX))
    233 				fatal(gettext("%s: bad total tracks"), optarg);
    234 			break;
    235 
    236 		case 'o':
    237 			if (strcmp(optarg, "space") == 0)
    238 				optim = FS_OPTSPACE;
    239 			else if (strcmp(optarg, "time") == 0)
    240 				optim = FS_OPTTIME;
    241 			else
    242 				fatal(gettext(
    243 "%s: bad optimization preference (options are `space' or `time')"), optarg);
    244 			break;
    245 
    246 		case 'a':
    247 			apc = number("apc", optarg, NR_NONE, 0);
    248 			apc_set = 1;
    249 			if (apc < 0 || apc > 32768) /* see mkfs.c */
    250 				fatal(gettext(
    251 				    "%s: bad alternates per cyl"), optarg);
    252 			break;
    253 
    254 		case 'b':
    255 			bsize = number("bsize", optarg, NR_NONE, DESBLKSIZE);
    256 			if (bsize < MINBSIZE || bsize > MAXBSIZE)
    257 				fatal(gettext(
    258 				    "%s: bad block size"), optarg);
    259 			break;
    260 
    261 		case 'f':
    262 			fsize = number("fragsize", optarg, NR_NONE,
    263 			    DESFRAGSIZE);
    264 			fsize_flag++;
    265 			/* xxx ought to test against bsize for upper limit */
    266 			if (fsize < DEV_BSIZE)
    267 				fatal(gettext("%s: bad frag size"), optarg);
    268 			break;
    269 
    270 		case 'c':
    271 			cpg = number("cpg", optarg, NR_NONE, 16);
    272 			cpg_set = 1;
    273 			if (cpg < 1)
    274 				fatal(gettext("%s: bad cylinders/group"),
    275 				    optarg);
    276 			break;
    277 
    278 		case 'm':
    279 			minfree = number("minfree", optarg, NR_PERCENT, 10);
    280 			if (minfree < 0 || minfree > 99)
    281 				fatal(gettext("%s: bad free space %%"), optarg);
    282 			break;
    283 
    284 		case 'n':
    285 			nrpos = number("nrpos", optarg, NR_NONE, 8);
    286 			nrpos_set = 1;
    287 			if (nrpos <= 0)
    288 				fatal(gettext(
    289 				    "%s: bad number of rotational positions"),
    290 				    optarg);
    291 			break;
    292 
    293 		case 'r':
    294 			rpm = number("rpm", optarg, NR_NONE, 3600);
    295 			rpm_set = 1;
    296 			if (rpm < 0)
    297 				fatal(gettext("%s: bad revs/minute"), optarg);
    298 			break;
    299 
    300 		case 'i':
    301 			/* xxx ought to test against fsize */
    302 			density = number("nbpi", optarg, NR_NONE, 2048);
    303 			if (density < DEV_BSIZE)
    304 				fatal(gettext("%s: bad bytes per inode"),
    305 				    optarg);
    306 			break;
    307 
    308 		case 'T':
    309 			Tflag++;
    310 			break;
    311 
    312 		default:
    313 			usage();
    314 			fatal(gettext("-%c: unknown flag"), optopt);
    315 		}
    316 	}
    317 
    318 	/* At this point, there should only be one argument left:	*/
    319 	/* The raw-special-device itself. If not, print usage message.	*/
    320 	if ((argc - optind) != 1) {
    321 		usage();
    322 		exit(1);
    323 	}
    324 
    325 	name = argv[optind];
    326 
    327 	special = getfullrawname(name);
    328 	if (special == NULL) {
    329 		(void) fprintf(stderr, gettext("newfs: malloc failed\n"));
    330 		exit(1);
    331 	}
    332 
    333 	if (*special == '\0') {
    334 		if (strchr(name, '/') != NULL) {
    335 			if (stat64(name, &st) < 0) {
    336 				(void) fprintf(stderr,
    337 				    gettext("newfs: %s: %s\n"),
    338 				    name, strerror(errno));
    339 				exit(2);
    340 			}
    341 			fatal(gettext("%s: not a raw disk device"), name);
    342 		}
    343 		(void) snprintf(device, sizeof (device), "/dev/rdsk/%s", name);
    344 		if ((special = getfullrawname(device)) == NULL) {
    345 			(void) fprintf(stderr,
    346 			    gettext("newfs: malloc failed\n"));
    347 			exit(1);
    348 		}
    349 
    350 		if (*special == '\0') {
    351 			(void) snprintf(device, sizeof (device), "/dev/%s",
    352 			    name);
    353 			if ((special = getfullrawname(device)) == NULL) {
    354 				(void) fprintf(stderr,
    355 				    gettext("newfs: malloc failed\n"));
    356 				exit(1);
    357 			}
    358 			if (*special == '\0')
    359 				fatal(gettext(
    360 				    "%s: not a raw disk device"), name);
    361 		}
    362 	}
    363 
    364 	/*
    365 	 * getdiskbydev() determines the characteristics of the special
    366 	 * device on which the file system will be built.  In the case
    367 	 * of devices with SMI labels (that is, non-EFI labels), the
    368 	 * following characteristics are set (if they were not already
    369 	 * set on the command line, since the command line settings
    370 	 * take precedence):
    371 	 *
    372 	 *	nsectors - sectors per track
    373 	 *	ntracks - tracks per cylinder
    374 	 *	rpm - disk revolutions per minute
    375 	 *
    376 	 *	apc is NOT set
    377 	 *
    378 	 * getdiskbydev() also sets the following quantities for all
    379 	 * devices, if not already set:
    380 	 *
    381 	 *	bsize - file system block size
    382 	 *	maxcontig
    383 	 *	label_type (efi, vtoc, or other)
    384 	 *
    385 	 * getdiskbydev() returns the actual size of the device, in
    386 	 * sectors.
    387 	 */
    388 
    389 	actual_fssize = getdiskbydev(special);
    390 
    391 	if (req_fssize == 0) {
    392 		fssize = actual_fssize;
    393 	} else {
    394 		/*
    395 		 * If the user specified a size larger than what we've
    396 		 * determined as the actual size of the device, see if the
    397 		 * size specified by the user can be read.  If so, use it,
    398 		 * since some devices and volume managers may not support
    399 		 * the vtoc and EFI interfaces we use to determine device
    400 		 * size.
    401 		 */
    402 		if (req_fssize > actual_fssize &&
    403 		    validate_size(special, req_fssize)) {
    404 			(void) fprintf(stderr, gettext(
    405 "Warning: the requested size of this file system\n"
    406 "(%lld sectors) is greater than the size of the\n"
    407 "device reported by the driver (%lld sectors).\n"
    408 "However, a read of the device at the requested size\n"
    409 "does succeed, so the requested size will be used.\n"),
    410 			    req_fssize, actual_fssize);
    411 			fssize = req_fssize;
    412 		} else {
    413 			fssize = MIN(req_fssize, actual_fssize);
    414 		}
    415 	}
    416 
    417 	if (label_type == LABEL_TYPE_VTOC) {
    418 		if (nsectors < 0)
    419 			fatal(gettext("%s: no default #sectors/track"),
    420 			    special);
    421 		if (!use_efi_dflts) {
    422 			if (ntracks < 0)
    423 				fatal(gettext("%s: no default #tracks"),
    424 				    special);
    425 		}
    426 		if (rpm < 0)
    427 			fatal(gettext(
    428 			    "%s: no default revolutions/minute value"),
    429 			    special);
    430 		if (rpm < 60) {
    431 			(void) fprintf(stderr,
    432 			    gettext("Warning: setting rpm to 60\n"));
    433 			rpm = 60;
    434 		}
    435 	}
    436 	if (label_type == LABEL_TYPE_EFI || label_type == LABEL_TYPE_OTHER) {
    437 		if (ntracks_set)
    438 			(void) fprintf(stderr, gettext(
    439 "Warning: ntracks is obsolete for this device and will be ignored.\n"));
    440 		if (cpg_set)
    441 			(void) fprintf(stderr, gettext(
    442 "Warning: cylinders/group is obsolete for this device and will be ignored.\n"));
    443 		if (rpm_set)
    444 			(void) fprintf(stderr, gettext(
    445 "Warning: rpm is obsolete for this device and will be ignored.\n"));
    446 		if (rot_set)
    447 			(void) fprintf(stderr, gettext(
    448 "Warning: rotational delay is obsolete for this device and"
    449 " will be ignored.\n"));
    450 		if (nrpos_set)
    451 			(void) fprintf(stderr, gettext(
    452 "Warning: number of rotational positions is obsolete for this device and\n"
    453 "will be ignored.\n"));
    454 		if (apc_set)
    455 			(void) fprintf(stderr, gettext(
    456 "Warning: number of alternate sectors per cylinder is obsolete for this\n"
    457 "device and will be ignored.\n"));
    458 
    459 		/*
    460 		 * We need these for the call to mkfs, even though they are
    461 		 * meaningless.
    462 		 */
    463 		rpm = 60;
    464 		nrpos = 1;
    465 		apc = 0;
    466 		rot = -1;
    467 
    468 		/*
    469 		 * These values are set to produce a file system with
    470 		 * a cylinder group size of 48MB.   For disks with
    471 		 * non-EFI labels, most geometries result in cylinder
    472 		 * groups of around 40 - 50 MB, so we arbitrarily choose
    473 		 * 48MB for disks with EFI labels.  mkfs will reduce
    474 		 * cylinders per group even further if necessary.
    475 		 */
    476 
    477 		cpg = 16;
    478 		nsectors = 128;
    479 		ntracks = 48;
    480 
    481 		/*
    482 		 * mkfs produces peculiar results for file systems
    483 		 * that are smaller than one cylinder so don't allow
    484 		 * them to be created (this check is only made for
    485 		 * disks with EFI labels.  Eventually, it should probably
    486 		 * be enforced for all disks.)
    487 		 */
    488 
    489 		if (fssize < nsectors * ntracks) {
    490 			fatal(gettext(
    491 			    "file system size must be at least %d sectors"),
    492 			    nsectors * ntracks);
    493 		}
    494 	}
    495 
    496 	if (fssize > INT_MAX)
    497 		Tflag = 1;
    498 
    499 	/*
    500 	 * If the user requested that the file system be set up for
    501 	 * eventual growth to over a terabyte, or if it's already greater
    502 	 * than a terabyte, set the inode density (nbpi) to MIN_MTB_DENSITY
    503 	 * (unless the user has specified a larger nbpi), set the frag size
    504 	 * equal to the block size, and set the cylinders-per-group value
    505 	 * passed to mkfs to -1, which tells mkfs to make cylinder groups
    506 	 * as large as possible.
    507 	 */
    508 	if (Tflag) {
    509 		if (density < MIN_MTB_DENSITY)
    510 			density = MIN_MTB_DENSITY;
    511 		fsize = bsize;
    512 		cpg = -1; 	/* says make cyl groups as big as possible */
    513 	} else {
    514 		if (fsize == 0)
    515 			fsize = DESFRAGSIZE;
    516 	}
    517 
    518 	if (!POWEROF2(fsize)) {
    519 		(void) fprintf(stderr, gettext(
    520 		    "newfs: fragment size must a power of 2, not %d\n"), fsize);
    521 		fsize = bsize/8;
    522 		(void) fprintf(stderr, gettext(
    523 		    "newfs: fragsize reset to %ld\n"), fsize);
    524 	}
    525 
    526 	/*
    527 	 * The file system is limited in size by the fragment size.
    528 	 * The number of fragments in the file system must fit into
    529 	 * a signed 32-bit quantity, so the number of sectors in the
    530 	 * file system is INT_MAX * the number of sectors in a frag.
    531 	 */
    532 
    533 	max_possible_fssize = ((uint64_t)fsize)/DEV_BSIZE * INT_MAX;
    534 	if (fssize > max_possible_fssize)
    535 		fssize = max_possible_fssize;
    536 
    537 	/*
    538 	 * Now fssize is the final size of the file system (in sectors).
    539 	 * If it's less than what the user requested, print a message.
    540 	 */
    541 	if (fssize < req_fssize) {
    542 		(void) fprintf(stderr, gettext(
    543 		    "newfs: requested size of %s disk blocks is too large.\n"),
    544 		    req_fssize_str);
    545 		(void) fprintf(stderr, gettext(
    546 		    "newfs: Resetting size to %lld\n"), fssize);
    547 	}
    548 
    549 	/*
    550 	 * fssize now equals the size (in sectors) of the file system
    551 	 * that will be created.
    552 	 */
    553 
    554 	/* XXX - following defaults are both here and in mkfs */
    555 	if (density <= 0) {
    556 		if (fssize < GBSEC)
    557 			density = MINDENSITY;
    558 		else
    559 			density = (int)((((longlong_t)fssize + (GBSEC - 1)) /
    560 			    GBSEC) * MINDENSITY);
    561 		if (density <= 0)
    562 			density = MINDENSITY;
    563 		if (density > MAXDEFDENSITY)
    564 			density = MAXDEFDENSITY;
    565 	}
    566 	if (cpg == 0) {
    567 		/*
    568 		 * maxcpg calculation adapted from mkfs
    569 		 * In the case of disks with EFI labels, cpg has
    570 		 * already been set, so we won't enter this code.
    571 		 */
    572 		long maxcpg, maxipg;
    573 
    574 		maxipg = roundup(bsize * NBBY / 3,
    575 		    bsize / sizeof (struct inode));
    576 		maxcpg = (bsize - sizeof (struct cg) - howmany(maxipg, NBBY)) /
    577 		    (sizeof (long) + nrpos * sizeof (short) +
    578 		    nsectors / (MAXFRAG * NBBY));
    579 		cpg = (fssize / GBSEC) * 32;
    580 		if (cpg > maxcpg)
    581 			cpg = maxcpg;
    582 		if (cpg <= 0)
    583 			cpg = MINCPG;
    584 	}
    585 	if (minfree < 0) {
    586 		minfree = (int)(((float)MINFREESEC / fssize) * 100);
    587 		if (minfree > 10)
    588 			minfree = 10;
    589 		if (minfree <= 0)
    590 			minfree = 1;
    591 	}
    592 #ifdef i386	/* Bug 1170182 */
    593 	if (ntracks > 32 && (ntracks % 16) != 0) {
    594 		ntracks -= (ntracks % 16);
    595 	}
    596 #endif
    597 	/*
    598 	 * Confirmation
    599 	 */
    600 	if (isatty(fileno(stdin)) && !Nflag) {
    601 		/*
    602 		 * If we can read a valid superblock, report the mount
    603 		 * point on which this filesystem was last mounted.
    604 		 */
    605 		if (((sbp = read_sb(special)) != 0) &&
    606 		    (*sbp->fs_fsmnt != '\0')) {
    607 			(void) printf(gettext(
    608 			    "newfs: %s last mounted as %s\n"),
    609 			    special, sbp->fs_fsmnt);
    610 		}
    611 		(void) printf(gettext(
    612 		    "newfs: construct a new file system %s: (y/n)? "),
    613 		    special);
    614 		(void) fflush(stdout);
    615 		if (!yes())
    616 			exit(0);
    617 	}
    618 
    619 	dprintf(("DeBuG newfs : nsect=%d ntrak=%d cpg=%d\n",
    620 	    nsectors, ntracks, cpg));
    621 	/*
    622 	 * If alternates-per-cylinder is ever implemented:
    623 	 * need to get apc from dp->d_apc if no -a switch???
    624 	 */
    625 	(void) snprintf(cmd, sizeof (cmd), "pfexec mkfs -F ufs "
    626 	    "%s%s%s%s %lld %d %d %d %d %d %d %d %d %s %d %d %d %d %s",
    627 	    Nflag ? "-o N " : "", binary_sb ? "-o calcbinsb " : "",
    628 	    text_sb ? "-o calcsb " : "", special,
    629 	    fssize, nsectors, ntracks, bsize, fsize, cpg, minfree, rpm/60,
    630 	    density, optim == FS_OPTSPACE ? "s" : "t", apc, rot, nrpos,
    631 	    maxcontig, Tflag ? "y" : "n");
    632 	if (verbose) {
    633 		(void) printf("%s\n", cmd);
    634 		(void) fflush(stdout);
    635 	}
    636 	exenv();
    637 	if (status = system(cmd))
    638 		exit(status >> 8);
    639 	if (Nflag)
    640 		exit(0);
    641 	(void) snprintf(cmd, sizeof (cmd), "/usr/sbin/fsirand %s", special);
    642 	if (notrand(special) && (status = system(cmd)) != 0)
    643 		(void) fprintf(stderr,
    644 		    gettext("%s: failed, status = %d\n"),
    645 		    cmd, status);
    646 	return (0);
    647 }
    648 
    649 static void
    650 exenv(void)
    651 {
    652 	char *epath;				/* executable file path */
    653 	char *cpath;				/* current path */
    654 
    655 	if ((cpath = getenv("PATH")) == NULL) {
    656 		(void) fprintf(stderr, gettext("newfs: no PATH in env\n"));
    657 		/*
    658 		 * Background: the Bourne shell interpolates "." into
    659 		 * the path where said path starts with a colon, ends
    660 		 * with a colon, or has two adjacent colons.  Thus,
    661 		 * the path ":/sbin::/usr/sbin:" is equivalent to
    662 		 * ".:/sbin:.:/usr/sbin:.".  Now, we have no cpath,
    663 		 * and epath ends in a colon (to make for easy
    664 		 * catenation in the normal case).  By the above, if
    665 		 * we use "", then "." becomes part of path.  That's
    666 		 * bad, so use CPATH (which is just a duplicate of some
    667 		 * element in EPATH).  No point in opening ourselves
    668 		 * up to a Trojan horse attack when we don't have to....
    669 		 */
    670 		cpath = CPATH;
    671 	}
    672 	if ((epath = malloc(strlen(EPATH) + strlen(cpath) + 1)) == NULL) {
    673 		(void) fprintf(stderr, gettext("newfs: malloc failed\n"));
    674 		exit(1);
    675 	}
    676 	(void) strcpy(epath, EPATH);
    677 	(void) strcat(epath, cpath);
    678 	if (putenv(epath) < 0) {
    679 		(void) fprintf(stderr, gettext("newfs: putenv failed\n"));
    680 		exit(1);
    681 	}
    682 }
    683 
    684 static int
    685 yes(void)
    686 {
    687 	int	i, b;
    688 
    689 	i = b = getchar();
    690 	while (b != '\n' && b != '\0' && b != EOF)
    691 		b = getchar();
    692 	return (i == 'y');
    693 }
    694 
    695 /*
    696  * xxx Caller must run fmt through gettext(3) for us, if we ever
    697  * xxx go the i18n route....
    698  */
    699 static void
    700 fatal(char *fmt, ...)
    701 {
    702 	va_list pvar;
    703 
    704 	(void) fprintf(stderr, "newfs: ");
    705 	va_start(pvar, fmt);
    706 	(void) vfprintf(stderr, fmt, pvar);
    707 	va_end(pvar);
    708 	(void) putc('\n', stderr);
    709 	exit(10);
    710 }
    711 
    712 static diskaddr_t
    713 getdiskbydev(char *disk)
    714 {
    715 	struct dk_geom g;
    716 	struct dk_cinfo ci;
    717 	diskaddr_t actual_size;
    718 	int fd;
    719 
    720 	if ((fd = open64(disk, 0)) < 0) {
    721 		perror(disk);
    722 		exit(1);
    723 	}
    724 
    725 	/*
    726 	 * get_device_size() determines the actual size of the
    727 	 * device, and also the disk's attributes, such as geometry.
    728 	 */
    729 	actual_size = get_device_size(fd, disk);
    730 
    731 	if (label_type == LABEL_TYPE_VTOC) {
    732 
    733 		/*
    734 		 * Geometry information does not make sense for removable or
    735 		 * hotpluggable media anyway, so indicate mkfs to use EFI
    736 		 * default parameters.
    737 		 */
    738 		if (ioctl(fd, DKIOCREMOVABLE, &isremovable)) {
    739 			dprintf(("DeBuG newfs : Unable to determine if %s is"
    740 			    " Removable Media. Proceeding with system"
    741 			    " determined parameters.\n", disk));
    742 			isremovable = 0;
    743 		}
    744 
    745 		if (ioctl(fd, DKIOCHOTPLUGGABLE, &ishotpluggable)) {
    746 			dprintf(("DeBuG newfs : Unable to determine if %s is"
    747 			    " Hotpluggable Media. Proceeding with system"
    748 			    " determined parameters.\n", disk));
    749 			ishotpluggable = 0;
    750 		}
    751 
    752 		if ((isremovable || ishotpluggable) && !Tflag)
    753 			use_efi_dflts = 1;
    754 
    755 		if (ioctl(fd, DKIOCGGEOM, &g))
    756 			fatal(gettext(
    757 			    "%s: Unable to read Disk geometry"), disk);
    758 		if ((((diskaddr_t)g.dkg_ncyl * g.dkg_nhead *
    759 		    g.dkg_nsect) > CHSLIMIT) && !Tflag) {
    760 			use_efi_dflts = 1;
    761 		}
    762 		dprintf(("DeBuG newfs : geom=%llu, CHSLIMIT=%d "
    763 		    "isremovable = %d ishotpluggable = %d use_efi_dflts = %d\n",
    764 		    (diskaddr_t)g.dkg_ncyl * g.dkg_nhead * g.dkg_nsect,
    765 		    CHSLIMIT, isremovable, ishotpluggable, use_efi_dflts));
    766 		/*
    767 		 * The ntracks that is passed to mkfs is decided here based
    768 		 * on 'use_efi_dflts' and whether ntracks was specified as a
    769 		 * command line parameter to newfs.
    770 		 * If ntracks of -1 is passed to mkfs, mkfs uses DEF_TRACKS_EFI
    771 		 * and DEF_SECTORS_EFI for ntracks and nsectors respectively.
    772 		 */
    773 		if (nsectors == 0)
    774 			nsectors = g.dkg_nsect;
    775 		if (ntracks == 0)
    776 			ntracks = use_efi_dflts ? -1 : g.dkg_nhead;
    777 		if (rpm == 0)
    778 			rpm = ((int)g.dkg_rpm <= 0) ? 3600: g.dkg_rpm;
    779 	}
    780 
    781 	if (bsize == 0)
    782 		bsize = DESBLKSIZE;
    783 	/*
    784 	 * Adjust maxcontig by the device's maxtransfer. If maxtransfer
    785 	 * information is not available, default to the min of a MB and
    786 	 * maxphys.
    787 	 */
    788 	if (maxcontig == -1 && ioctl(fd, DKIOCINFO, &ci) == 0) {
    789 		maxcontig = ci.dki_maxtransfer * DEV_BSIZE;
    790 		if (maxcontig < 0) {
    791 			int	error, gotit, maxphys;
    792 			gotit = fsgetmaxphys(&maxphys, &error);
    793 
    794 			/*
    795 			 * If we cannot get the maxphys value, default
    796 			 * to ufs_maxmaxphys (MB).
    797 			 */
    798 			if (gotit) {
    799 				maxcontig = MIN(maxphys, MB);
    800 			} else {
    801 				(void) fprintf(stderr, gettext(
    802 "Warning: Could not get system value for maxphys. The value for maxcontig\n"
    803 "will default to 1MB.\n"));
    804 			maxcontig = MB;
    805 			}
    806 		}
    807 		maxcontig /= bsize;
    808 	}
    809 	(void) close(fd);
    810 	return (actual_size);
    811 }
    812 
    813 /*
    814  * Figure out how big the partition we're dealing with is.
    815  */
    816 static diskaddr_t
    817 get_device_size(int fd, char *name)
    818 {
    819 	struct extvtoc vtoc;
    820 	dk_gpt_t *efi_vtoc;
    821 	diskaddr_t	slicesize;
    822 
    823 	int index = read_extvtoc(fd, &vtoc);
    824 
    825 	if (index >= 0) {
    826 		label_type = LABEL_TYPE_VTOC;
    827 	} else {
    828 		if (index == VT_ENOTSUP || index == VT_ERROR) {
    829 			/* it might be an EFI label */
    830 			index = efi_alloc_and_read(fd, &efi_vtoc);
    831 			if (index >= 0)
    832 				label_type = LABEL_TYPE_EFI;
    833 		}
    834 	}
    835 
    836 	if (index < 0) {
    837 		/*
    838 		 * Since both attempts to read the label failed, we're
    839 		 * going to fall back to a brute force approach to
    840 		 * determining the device's size:  see how far out we can
    841 		 * perform reads on the device.
    842 		 */
    843 
    844 		slicesize = brute_force_get_device_size(fd);
    845 		if (slicesize == 0) {
    846 			switch (index) {
    847 			case VT_ERROR:
    848 				(void) fprintf(stderr, gettext(
    849 				    "newfs: %s: %s\n"), name, strerror(errno));
    850 				exit(10);
    851 				/*NOTREACHED*/
    852 			case VT_EIO:
    853 				fatal(gettext(
    854 				    "%s: I/O error accessing VTOC"), name);
    855 				/*NOTREACHED*/
    856 			case VT_EINVAL:
    857 				fatal(gettext(
    858 				    "%s: Invalid field in VTOC"), name);
    859 				/*NOTREACHED*/
    860 			default:
    861 				fatal(gettext(
    862 				    "%s: unknown error accessing VTOC"),
    863 				    name);
    864 				/*NOTREACHED*/
    865 			}
    866 		} else {
    867 			label_type = LABEL_TYPE_OTHER;
    868 		}
    869 	}
    870 
    871 	if (label_type == LABEL_TYPE_EFI) {
    872 		slicesize = efi_vtoc->efi_parts[index].p_size;
    873 		efi_free(efi_vtoc);
    874 	} else if (label_type == LABEL_TYPE_VTOC) {
    875 		slicesize = vtoc.v_part[index].p_size;
    876 	}
    877 
    878 	return (slicesize);
    879 }
    880 
    881 /*
    882  * brute_force_get_device_size
    883  *
    884  * Determine the size of the device by seeing how far we can
    885  * read.  Doing an llseek( , , SEEK_END) would probably work
    886  * in most cases, but we've seen at least one third-party driver
    887  * which doesn't correctly support the SEEK_END option when the
    888  * the device is greater than a terabyte.
    889  */
    890 
    891 static diskaddr_t
    892 brute_force_get_device_size(int fd)
    893 {
    894 	diskaddr_t	min_fail = 0;
    895 	diskaddr_t	max_succeed = 0;
    896 	diskaddr_t	cur_db_off;
    897 	char 		buf[DEV_BSIZE];
    898 
    899 	/*
    900 	 * First, see if we can read the device at all, just to
    901 	 * eliminate errors that have nothing to do with the
    902 	 * device's size.
    903 	 */
    904 
    905 	if (((llseek(fd, (offset_t)0, SEEK_SET)) == -1) ||
    906 	    ((read(fd, buf, DEV_BSIZE)) == -1))
    907 		return (0);  /* can't determine size */
    908 
    909 	/*
    910 	 * Now, go sequentially through the multiples of 4TB
    911 	 * to find the first read that fails (this isn't strictly
    912 	 * the most efficient way to find the actual size if the
    913 	 * size really could be anything between 0 and 2**64 bytes.
    914 	 * We expect the sizes to be less than 16 TB for some time,
    915 	 * so why do a bunch of reads that are larger than that?
    916 	 * However, this algorithm *will* work for sizes of greater
    917 	 * than 16 TB.  We're just not optimizing for those sizes.)
    918 	 */
    919 
    920 	for (cur_db_off = SECTORS_PER_TERABYTE * 4;
    921 	    min_fail == 0 && cur_db_off < FS_SIZE_UPPER_LIMIT;
    922 	    cur_db_off += 4 * SECTORS_PER_TERABYTE) {
    923 		if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE),
    924 		    SEEK_SET)) == -1) ||
    925 		    ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE))
    926 			min_fail = cur_db_off;
    927 		else
    928 			max_succeed = cur_db_off;
    929 	}
    930 
    931 	if (min_fail == 0)
    932 		return (0);
    933 
    934 	/*
    935 	 * We now know that the size of the device is less than
    936 	 * min_fail and greater than or equal to max_succeed.  Now
    937 	 * keep splitting the difference until the actual size in
    938 	 * sectors in known.  We also know that the difference
    939 	 * between max_succeed and min_fail at this time is
    940 	 * 4 * SECTORS_PER_TERABYTE, which is a power of two, which
    941 	 * simplifies the math below.
    942 	 */
    943 
    944 	while (min_fail - max_succeed > 1) {
    945 		cur_db_off = max_succeed + (min_fail - max_succeed)/2;
    946 		if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE),
    947 		    SEEK_SET)) == -1) ||
    948 		    ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE))
    949 			min_fail = cur_db_off;
    950 		else
    951 			max_succeed = cur_db_off;
    952 	}
    953 
    954 	/* the size is the last successfully read sector offset plus one */
    955 	return (max_succeed + 1);
    956 }
    957 
    958 /*
    959  * validate_size
    960  *
    961  * Return 1 if the device appears to be at least "size" sectors long.
    962  * Return 0 if it's shorter or we can't read it.
    963  */
    964 
    965 static int
    966 validate_size(char *disk, diskaddr_t size)
    967 {
    968 	char 		buf[DEV_BSIZE];
    969 	int fd, rc;
    970 
    971 	if ((fd = open64(disk, O_RDONLY)) < 0) {
    972 		perror(disk);
    973 		exit(1);
    974 	}
    975 
    976 	if ((llseek(fd, (offset_t)((size - 1) * DEV_BSIZE), SEEK_SET) == -1) ||
    977 	    (read(fd, buf, DEV_BSIZE)) != DEV_BSIZE)
    978 		rc = 0;
    979 	else
    980 		rc = 1;
    981 	(void) close(fd);
    982 	return (rc);
    983 }
    984 
    985 /*
    986  * read_sb(char * rawdev) - Attempt to read the superblock from a raw device
    987  *
    988  * Returns:
    989  *	0 :
    990  *		Could not read a valid superblock for a variety of reasons.
    991  *		Since 'newfs' handles any fatal conditions, we're not going
    992  *		to make any guesses as to why this is failing or what should
    993  *		be done about it.
    994  *
    995  *	struct fs *:
    996  *		A pointer to (what we think is) a valid superblock. The
    997  *		space for the superblock is static (inside the function)
    998  *		since we will only be reading the values from it.
    999  */
   1000 
   1001 struct fs *
   1002 read_sb(char *fsdev)
   1003 {
   1004 	static struct fs	sblock;
   1005 	struct stat64		statb;
   1006 	int			dskfd;
   1007 	char			*bufp = NULL;
   1008 	int			bufsz = 0;
   1009 
   1010 	if (stat64(fsdev, &statb) < 0)
   1011 		return (0);
   1012 
   1013 	if ((dskfd = open64(fsdev, O_RDONLY)) < 0)
   1014 		return (0);
   1015 
   1016 	/*
   1017 	 * We need a buffer whose size is a multiple of DEV_BSIZE in order
   1018 	 * to read from a raw device (which we were probably passed).
   1019 	 */
   1020 	bufsz = ((sizeof (sblock) / DEV_BSIZE) + 1) * DEV_BSIZE;
   1021 	if ((bufp = malloc(bufsz)) == NULL) {
   1022 		(void) close(dskfd);
   1023 		return (0);
   1024 	}
   1025 
   1026 	if (llseek(dskfd, (offset_t)SBOFF, SEEK_SET) < 0 ||
   1027 	    read(dskfd, bufp, bufsz) < 0) {
   1028 		(void) close(dskfd);
   1029 		free(bufp);
   1030 		return (0);
   1031 	}
   1032 	(void) close(dskfd);	/* Done with the file */
   1033 
   1034 	(void) memcpy(&sblock, bufp, sizeof (sblock));
   1035 	free(bufp);	/* Don't need this anymore */
   1036 
   1037 	if (((sblock.fs_magic != FS_MAGIC) &&
   1038 	    (sblock.fs_magic != MTB_UFS_MAGIC)) ||
   1039 	    sblock.fs_ncg < 1 || sblock.fs_cpg < 1)
   1040 		return (0);
   1041 
   1042 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
   1043 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
   1044 		return (0);
   1045 
   1046 	if (sblock.fs_sbsize < 0 || sblock.fs_sbsize > SBSIZE)
   1047 		return (0);
   1048 
   1049 	return (&sblock);
   1050 }
   1051 
   1052 /*
   1053  * Read the UFS file system on the raw device SPECIAL.  If it does not
   1054  * appear to be a UFS file system, return non-zero, indicating that
   1055  * fsirand should be called (and it will spit out an error message).
   1056  * If it is a UFS file system, take a look at the inodes in the first
   1057  * cylinder group.  If they appear to be randomized (non-zero), return
   1058  * zero, which will cause fsirand to not be called.  If the inode generation
   1059  * counts are all zero, then we must call fsirand, so return non-zero.
   1060  */
   1061 
   1062 #define	RANDOMIZED	0
   1063 #define	NOT_RANDOMIZED	1
   1064 
   1065 static int
   1066 notrand(char *special)
   1067 {
   1068 	long fsbuf[SBSIZE / sizeof (long)];
   1069 	struct dinode dibuf[MAXBSIZE/sizeof (struct dinode)];
   1070 	struct fs *fs;
   1071 	struct dinode *dip;
   1072 	offset_t seekaddr;
   1073 	int bno, inum;
   1074 	int fd;
   1075 
   1076 	fs = (struct fs *)fsbuf;
   1077 	if ((fd = open64(special, 0)) == -1)
   1078 		return (NOT_RANDOMIZED);
   1079 	if (llseek(fd, (offset_t)SBLOCK * DEV_BSIZE, 0) == -1 ||
   1080 	    read(fd, (char *)fs, SBSIZE) != SBSIZE ||
   1081 	    ((fs->fs_magic != FS_MAGIC) && (fs->fs_magic != MTB_UFS_MAGIC))) {
   1082 		(void) close(fd);
   1083 		return (NOT_RANDOMIZED);
   1084 	}
   1085 
   1086 	/* looks like a UFS file system; read the first cylinder group */
   1087 	bsize = INOPB(fs) * sizeof (struct dinode);
   1088 	inum = 0;
   1089 	while (inum < fs->fs_ipg) {
   1090 		bno = itod(fs, inum);
   1091 		seekaddr = (offset_t)fsbtodb(fs, bno) * DEV_BSIZE;
   1092 		if (llseek(fd, seekaddr, 0) == -1 ||
   1093 		    read(fd, (char *)dibuf, bsize) != bsize) {
   1094 			(void) close(fd);
   1095 			return (NOT_RANDOMIZED);
   1096 		}
   1097 		for (dip = dibuf; dip < &dibuf[INOPB(fs)]; dip++) {
   1098 			if (dip->di_gen != 0) {
   1099 				(void) close(fd);
   1100 				return (RANDOMIZED);
   1101 			}
   1102 			inum++;
   1103 		}
   1104 	}
   1105 	(void) close(fd);
   1106 	return (NOT_RANDOMIZED);
   1107 }
   1108 
   1109 static void
   1110 usage(void)
   1111 {
   1112 	(void) fprintf(stderr, gettext(
   1113 	    "usage: newfs [ -v ] [ mkfs-options ] raw-special-device\n"));
   1114 	(void) fprintf(stderr, gettext("where mkfs-options are:\n"));
   1115 	(void) fprintf(stderr, gettext(
   1116 	    "\t-N do not create file system, just print out parameters\n"));
   1117 	(void) fprintf(stderr, gettext(
   1118 "\t-T configure file system for eventual growth to over a terabyte\n"));
   1119 	(void) fprintf(stderr, gettext("\t-s file system size (sectors)\n"));
   1120 	(void) fprintf(stderr, gettext("\t-b block size\n"));
   1121 	(void) fprintf(stderr, gettext("\t-f frag size\n"));
   1122 	(void) fprintf(stderr, gettext("\t-t tracks/cylinder\n"));
   1123 	(void) fprintf(stderr, gettext("\t-c cylinders/group\n"));
   1124 	(void) fprintf(stderr, gettext("\t-m minimum free space %%\n"));
   1125 	(void) fprintf(stderr, gettext(
   1126 	    "\t-o optimization preference (`space' or `time')\n"));
   1127 	(void) fprintf(stderr, gettext("\t-r revolutions/minute\n"));
   1128 	(void) fprintf(stderr, gettext("\t-i number of bytes per inode\n"));
   1129 	(void) fprintf(stderr, gettext(
   1130 	    "\t-a number of alternates per cylinder\n"));
   1131 	(void) fprintf(stderr, gettext("\t-C maxcontig\n"));
   1132 	(void) fprintf(stderr, gettext("\t-d rotational delay\n"));
   1133 	(void) fprintf(stderr, gettext(
   1134 	    "\t-n number of rotational positions\n"));
   1135 	(void) fprintf(stderr, gettext(
   1136 "\t-S print a textual version of the calculated superblock to stdout\n"));
   1137 	(void) fprintf(stderr, gettext(
   1138 "\t-B dump a binary version of the calculated superblock to stdout\n"));
   1139 }
   1140 
   1141 /*
   1142  * Error-detecting version of atoi(3).  Adapted from mkfs' number().
   1143  */
   1144 static unsigned int
   1145 number(char *param, char *value, int flags, int def_value)
   1146 {
   1147 	char *cs;
   1148 	int n;
   1149 	int cut = INT_MAX / 10;    /* limit to avoid overflow */
   1150 	int minus = 0;
   1151 
   1152 	cs = value;
   1153 	if (*cs == '-') {
   1154 		minus = 1;
   1155 		cs += 1;
   1156 	}
   1157 	if ((*cs < '0') || (*cs > '9')) {
   1158 		goto bail_out;
   1159 	}
   1160 	n = 0;
   1161 	while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) {
   1162 		n = n*10 + *cs++ - '0';
   1163 	}
   1164 	if (minus)
   1165 		n = -n;
   1166 	for (;;) {
   1167 		switch (*cs++) {
   1168 		case '\0':
   1169 			return (n);
   1170 
   1171 		case '0': case '1': case '2': case '3': case '4':
   1172 		case '5': case '6': case '7': case '8': case '9':
   1173 			(void) fprintf(stderr, gettext(
   1174 			    "newfs: value for %s overflowed, using %d\n"),
   1175 			    param, def_value);
   1176 			return (def_value);
   1177 
   1178 		case '%':
   1179 			if (flags & NR_PERCENT)
   1180 				break;
   1181 			/* FALLTHROUGH */
   1182 
   1183 		default:
   1184 bail_out:
   1185 			fatal(gettext("bad numeric arg for %s: \"%s\""),
   1186 			    param, value);
   1187 
   1188 		}
   1189 	}
   1190 	/* NOTREACHED */
   1191 }
   1192 
   1193 /*
   1194  * Error-detecting version of atoi(3).  Adapted from mkfs' number().
   1195  */
   1196 static int64_t
   1197 number64(char *param, char *value, int flags, int64_t def_value)
   1198 {
   1199 	char *cs;
   1200 	int64_t n;
   1201 	int64_t cut = FS_SIZE_UPPER_LIMIT/ 10;    /* limit to avoid overflow */
   1202 	int minus = 0;
   1203 
   1204 	cs = value;
   1205 	if (*cs == '-') {
   1206 		minus = 1;
   1207 		cs += 1;
   1208 	}
   1209 	if ((*cs < '0') || (*cs > '9')) {
   1210 		goto bail_out;
   1211 	}
   1212 	n = 0;
   1213 	while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) {
   1214 		n = n*10 + *cs++ - '0';
   1215 	}
   1216 	if (minus)
   1217 		n = -n;
   1218 	for (;;) {
   1219 		switch (*cs++) {
   1220 		case '\0':
   1221 			return (n);
   1222 
   1223 		case '0': case '1': case '2': case '3': case '4':
   1224 		case '5': case '6': case '7': case '8': case '9':
   1225 			(void) fprintf(stderr, gettext(
   1226 			    "newfs: value for %s overflowed, using %d\n"),
   1227 			    param, def_value);
   1228 			return (def_value);
   1229 
   1230 		case '%':
   1231 			if (flags & NR_PERCENT)
   1232 				break;
   1233 			/* FALLTHROUGH */
   1234 
   1235 		default:
   1236 bail_out:
   1237 			fatal(gettext("bad numeric arg for %s: \"%s\""),
   1238 			    param, value);
   1239 
   1240 		}
   1241 	}
   1242 	/* NOTREACHED */
   1243 }
   1244