xref: /titanic_52/usr/src/cmd/fs.d/ufs/newfs/newfs.c (revision c2580b931007758eab8cb5ae8726ebe1588e259b)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
23 	/* from UCB 5.2 9/11/85 */
24 
25 /*
26  * newfs: friendly front end to mkfs
27  *
28  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
29  * Use is subject to license terms.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <locale.h>
35 #include <sys/stat.h>
36 #include <sys/buf.h>
37 #include <sys/fs/ufs_fs.h>
38 #include <sys/vnode.h>
39 #include <sys/fs/ufs_inode.h>
40 #include <sys/sysmacros.h>
41 
42 #include <errno.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #include <limits.h>
51 #include <libintl.h>
52 #include <sys/dkio.h>
53 #include <sys/vtoc.h>
54 #include <sys/mkdev.h>
55 #include <sys/efi_partition.h>
56 
57 #include <fslib.h>
58 
59 static unsigned int number(char *, char *, int, int);
60 static int64_t number64(char *, char *, int, int64_t);
61 static diskaddr_t getdiskbydev(char *);
62 static int  yes(void);
63 static int  notrand(char *);
64 static void usage();
65 static diskaddr_t get_device_size(int, char *);
66 static diskaddr_t brute_force_get_device_size(int);
67 static int validate_size(char *disk, diskaddr_t size);
68 static void exenv(void);
69 static struct fs *read_sb(char *);
70 /*PRINTFLIKE1*/
71 static void fatal(char *fmt, ...);
72 
73 #define	EPATH "PATH=/usr/sbin:/sbin:"
74 #define	CPATH "/sbin"					/* an EPATH element */
75 #define	MB (1024 * 1024)
76 #define	GBSEC ((1024 * 1024 * 1024) / DEV_BSIZE)	/* sectors in a GB */
77 #define	MINFREESEC ((64 * 1024 * 1024) / DEV_BSIZE)	/* sectors in 64 MB */
78 #define	MINCPG (16)	/* traditional */
79 #define	MAXDEFDENSITY (8 * 1024)	/* arbitrary */
80 #define	MINDENSITY (2 * 1024)	/* traditional */
81 #define	MIN_MTB_DENSITY (1024 * 1024)
82 #define	POWEROF2(num)	(((num) & ((num) - 1)) == 0)
83 #define	SECTORS_PER_TERABYTE	(1LL << 31)
84 /*
85  * The following constant specifies an upper limit for file system size
86  * that is actually a lot bigger than we expect to support with UFS. (Since
87  * it's specified in sectors, the file system size would be 2**44 * 512,
88  * which is 2**53, which is 8192 Terabytes.)  However, it's useful
89  * for checking the basic sanity of a size value that is input on the
90  * command line.
91  */
92 #define	FS_SIZE_UPPER_LIMIT	0x100000000000LL
93 
94 /* For use with number() */
95 #define	NR_NONE		0
96 #define	NR_PERCENT	0x01
97 
98 /*
99  * The following two constants set the default block and fragment sizes.
100  * Both constants must be a power of 2 and meet the following constraints:
101  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
102  *	DEV_BSIZE <= DESFRAGSIZE <= DESBLKSIZE
103  *	DESBLKSIZE / DESFRAGSIZE <= 8
104  */
105 #define	DESBLKSIZE	8192
106 #define	DESFRAGSIZE	1024
107 
108 #ifdef DEBUG
109 #define	dprintf(x)	printf x
110 #else
111 #define	dprintf(x)
112 #endif
113 
114 static int	Nflag;		/* run mkfs without writing file system */
115 static int	Tflag;		/* set up file system for growth to over 1 TB */
116 static int	verbose;	/* show mkfs line before exec */
117 static int	fsize = 0;		/* fragment size */
118 static int	fsize_flag = 0;	/* fragment size was specified on cmd line */
119 static int	bsize;		/* block size */
120 static int	ntracks;	/* # tracks/cylinder */
121 static int	ntracks_set = 0; /* true if the user specified ntracks */
122 static int	optim = FS_OPTTIME;	/* optimization, t(ime) or s(pace) */
123 static int	nsectors;	/* # sectors/track */
124 static int	cpg;		/* cylinders/cylinder group */
125 static int	cpg_set = 0;	/* true if the user specified cpg */
126 static int	minfree = -1;	/* free space threshold */
127 static int	rpm;		/* revolutions/minute of drive */
128 static int	rpm_set = 0;	/* true if the user specified rpm */
129 static int	nrpos = 8;	/* # of distinguished rotational positions */
130 				/* 8 is the historical default */
131 static int	nrpos_set = 0;	/* true if the user specified nrpos */
132 static int	density = 0;	/* number of bytes per inode */
133 static int	apc;		/* alternates per cylinder */
134 static int	apc_set = 0;	/* true if the user specified apc */
135 static int 	rot = -1;	/* rotational delay (msecs) */
136 static int	rot_set = 0;	/* true if the user specified rot */
137 static int 	maxcontig = -1;	/* maximum number of contig blocks */
138 static int	text_sb = 0;	/* no disk changes; just final sb text dump */
139 static int	binary_sb = 0;	/* no disk changes; just final sb binary dump */
140 static int	label_type;	/* see types below */
141 
142 /*
143  * The variable use_efi_dflts is an indicator of whether to use EFI logic
144  * or the geometry logic in laying out the filesystem. This is decided
145  * based on the size of the disk and is used only for non-EFI labeled disks.
146  */
147 static int	use_efi_dflts = 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')"),
244 				optarg);
245 			break;
246 
247 		case 'a':
248 			apc = number("apc", optarg, NR_NONE, 0);
249 			apc_set = 1;
250 			if (apc < 0 || apc > 32768) /* see mkfs.c */
251 				fatal(gettext(
252 				    "%s: bad alternates per cyl"), optarg);
253 			break;
254 
255 		case 'b':
256 			bsize = number("bsize", optarg, NR_NONE, DESBLKSIZE);
257 			if (bsize < MINBSIZE || bsize > MAXBSIZE)
258 				fatal(gettext(
259 				    "%s: bad block size"), optarg);
260 			break;
261 
262 		case 'f':
263 			fsize = number("fragsize", optarg, NR_NONE,
264 				DESFRAGSIZE);
265 			fsize_flag++;
266 			/* xxx ought to test against bsize for upper limit */
267 			if (fsize < DEV_BSIZE)
268 				fatal(gettext("%s: bad frag size"), optarg);
269 			break;
270 
271 		case 'c':
272 			cpg = number("cpg", optarg, NR_NONE, 16);
273 			cpg_set = 1;
274 			if (cpg < 1)
275 				fatal(gettext("%s: bad cylinders/group"),
276 				    optarg);
277 			break;
278 
279 		case 'm':
280 			minfree = number("minfree", optarg, NR_PERCENT, 10);
281 			if (minfree < 0 || minfree > 99)
282 				fatal(gettext("%s: bad free space %%"), optarg);
283 			break;
284 
285 		case 'n':
286 			nrpos = number("nrpos", optarg, NR_NONE, 8);
287 			nrpos_set = 1;
288 			if (nrpos <= 0)
289 				fatal(gettext(
290 				    "%s: bad number of rotational positions"),
291 				    optarg);
292 			break;
293 
294 		case 'r':
295 			rpm = number("rpm", optarg, NR_NONE, 3600);
296 			rpm_set = 1;
297 			if (rpm < 0)
298 				fatal(gettext("%s: bad revs/minute"), optarg);
299 			break;
300 
301 		case 'i':
302 			/* xxx ought to test against fsize */
303 			density = number("nbpi", optarg, NR_NONE, 2048);
304 			if (density < DEV_BSIZE)
305 				fatal(gettext("%s: bad bytes per inode"),
306 				    optarg);
307 			break;
308 
309 		case 'T':
310 			Tflag++;
311 			break;
312 
313 		default:
314 			usage();
315 			fatal(gettext("-%c: unknown flag"), optopt);
316 		}
317 	}
318 
319 	/* At this point, there should only be one argument left:	*/
320 	/* The raw-special-device itself. If not, print usage message.	*/
321 	if ((argc - optind) != 1) {
322 		usage();
323 		exit(1);
324 	}
325 
326 	name = argv[optind];
327 
328 	special = getfullrawname(name);
329 	if (special == NULL) {
330 		(void) fprintf(stderr, gettext("newfs: malloc failed\n"));
331 		exit(1);
332 	}
333 
334 	if (*special == '\0') {
335 		if (strchr(name, '/') != NULL) {
336 			if (stat64(name, &st) < 0) {
337 				(void) fprintf(stderr,
338 				    gettext("newfs: %s: %s\n"),
339 				    name, strerror(errno));
340 				exit(2);
341 			}
342 			fatal(gettext("%s: not a raw disk device"), name);
343 		}
344 		(void) sprintf(device, "/dev/rdsk/%s", name);
345 		if ((special = getfullrawname(device)) == NULL) {
346 			(void) fprintf(stderr,
347 			    gettext("newfs: malloc failed\n"));
348 			exit(1);
349 		}
350 
351 		if (*special == '\0') {
352 			(void) sprintf(device, "/dev/%s", 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 = ((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 	dprintf(("DeBuG newfs : nsect=%d ntrak=%d cpg=%d\n",
619 		nsectors, ntracks, cpg));
620 	/*
621 	 * If alternates-per-cylinder is ever implemented:
622 	 * need to get apc from dp->d_apc if no -a switch???
623 	 */
624 	(void) sprintf(cmd,
625 	"mkfs -F ufs %s%s%s%s %lld %d %d %d %d %d %d %d %d %s %d %d %d %d %s",
626 	    Nflag ? "-o N " : "", binary_sb ? "-o calcbinsb " : "",
627 	    text_sb ? "-o calcsb " : "", special,
628 	    fssize, nsectors, ntracks, bsize, fsize, cpg, minfree, rpm/60,
629 	    density, optim == FS_OPTSPACE ? "s" : "t", apc, rot, nrpos,
630 	    maxcontig, Tflag ? "y" : "n");
631 	if (verbose) {
632 		(void) printf("%s\n", cmd);
633 		(void) fflush(stdout);
634 	}
635 	exenv();
636 	if (status = system(cmd))
637 		exit(status >> 8);
638 	if (Nflag)
639 		exit(0);
640 	(void) sprintf(cmd, "/usr/sbin/fsirand %s", special);
641 	if (notrand(special) && (status = system(cmd)) != 0)
642 		(void) fprintf(stderr,
643 		    gettext("%s: failed, status = %d\n"),
644 		    cmd, status);
645 	return (0);
646 }
647 
648 static void
649 exenv(void)
650 {
651 	char *epath;				/* executable file path */
652 	char *cpath;				/* current path */
653 
654 	if ((cpath = getenv("PATH")) == NULL) {
655 		(void) fprintf(stderr, gettext("newfs: no PATH in env\n"));
656 		/*
657 		 * Background: the Bourne shell interpolates "." into
658 		 * the path where said path starts with a colon, ends
659 		 * with a colon, or has two adjacent colons.  Thus,
660 		 * the path ":/sbin::/usr/sbin:" is equivalent to
661 		 * ".:/sbin:.:/usr/sbin:.".  Now, we have no cpath,
662 		 * and epath ends in a colon (to make for easy
663 		 * catenation in the normal case).  By the above, if
664 		 * we use "", then "." becomes part of path.  That's
665 		 * bad, so use CPATH (which is just a duplicate of some
666 		 * element in EPATH).  No point in opening ourselves
667 		 * up to a Trojan horse attack when we don't have to....
668 		 */
669 		cpath = CPATH;
670 	}
671 	if ((epath = malloc(strlen(EPATH) + strlen(cpath) + 1)) == NULL) {
672 		(void) fprintf(stderr, gettext("newfs: malloc failed\n"));
673 		exit(1);
674 	}
675 	(void) strcpy(epath, EPATH);
676 	(void) strcat(epath, cpath);
677 	if (putenv(epath) < 0) {
678 		(void) fprintf(stderr, gettext("newfs: putenv failed\n"));
679 		exit(1);
680 	}
681 }
682 
683 static int
684 yes(void)
685 {
686 	int	i, b;
687 
688 	i = b = getchar();
689 	while (b != '\n' && b != '\0' && b != EOF)
690 		b = getchar();
691 	return (i == 'y');
692 }
693 
694 /*
695  * xxx Caller must run fmt through gettext(3) for us, if we ever
696  * xxx go the i18n route....
697  */
698 static void
699 fatal(char *fmt, ...)
700 {
701 	va_list pvar;
702 
703 	(void) fprintf(stderr, "newfs: ");
704 	va_start(pvar, fmt);
705 	(void) vfprintf(stderr, fmt, pvar);
706 	va_end(pvar);
707 	(void) putc('\n', stderr);
708 	exit(10);
709 }
710 
711 static diskaddr_t
712 getdiskbydev(char *disk)
713 {
714 	struct dk_geom g;
715 	struct dk_cinfo ci;
716 	diskaddr_t actual_size;
717 	int fd;
718 
719 	if ((fd = open64(disk, 0)) < 0) {
720 		perror(disk);
721 		exit(1);
722 	}
723 
724 	/*
725 	 * get_device_size() determines the actual size of the
726 	 * device, and also the disk's attributes, such as geometry.
727 	 */
728 	actual_size = get_device_size(fd, disk);
729 
730 	if (label_type == LABEL_TYPE_VTOC) {
731 		if (ioctl(fd, DKIOCGGEOM, &g))
732 			fatal(gettext(
733 			    "%s: Unable to read Disk geometry"), disk);
734 		dprintf(("DeBuG newfs : geom=%ld, CHSLIMIT=%d\n",
735 			g.dkg_ncyl * g.dkg_nhead * g.dkg_nsect, CHSLIMIT));
736 		if (((g.dkg_ncyl * g.dkg_nhead * g.dkg_nsect) > CHSLIMIT) &&
737 		    !Tflag) {
738 			dprintf(("DeBuG newfs : geom > CHSLIMIT\n"));
739 			use_efi_dflts = 1;
740 		}
741 		/*
742 		 * The ntracks that is passed to mkfs is decided here based
743 		 * on 'use_efi_dflts' and whether ntracks was specified as a
744 		 * command line parameter to newfs.
745 		 * If ntracks of -1 is passed to mkfs, mkfs uses DEF_TRACKS_EFI
746 		 * and DEF_SECTORS_EFI for ntracks and nsectors respectively.
747 		 */
748 		if (nsectors == 0)
749 			nsectors = g.dkg_nsect;
750 		if (ntracks == 0)
751 			ntracks = use_efi_dflts ? -1 : g.dkg_nhead;
752 		if (rpm == 0)
753 			rpm = ((int)g.dkg_rpm <= 0) ? 3600: g.dkg_rpm;
754 	}
755 
756 	if (bsize == 0)
757 		bsize = DESBLKSIZE;
758 	/*
759 	 * Adjust maxcontig by the device's maxtransfer. If maxtransfer
760 	 * information is not available, default to the min of a MB and
761 	 * maxphys.
762 	 */
763 	if (maxcontig == -1 && ioctl(fd, DKIOCINFO, &ci) == 0) {
764 		maxcontig = ci.dki_maxtransfer * DEV_BSIZE;
765 		if (maxcontig < 0) {
766 			int	error, gotit, maxphys;
767 			gotit = fsgetmaxphys(&maxphys, &error);
768 
769 			/*
770 			 * If we cannot get the maxphys value, default
771 			 * to ufs_maxmaxphys (MB).
772 			 */
773 			if (gotit) {
774 				maxcontig = MIN(maxphys, MB);
775 			} else {
776 				(void) fprintf(stderr, gettext(
777 "Warning: Could not get system value for maxphys. The value for maxcontig\n"
778 "will default to 1MB.\n"));
779 			maxcontig = MB;
780 			}
781 		}
782 		maxcontig /= bsize;
783 	}
784 	(void) close(fd);
785 	return (actual_size);
786 }
787 
788 /*
789  * Figure out how big the partition we're dealing with is.
790  */
791 static diskaddr_t
792 get_device_size(int fd, char *name)
793 {
794 	struct vtoc vtoc;
795 	dk_gpt_t *efi_vtoc;
796 	diskaddr_t	slicesize;
797 
798 	int index = read_vtoc(fd, &vtoc);
799 
800 	if (index >= 0) {
801 		label_type = LABEL_TYPE_VTOC;
802 	} else {
803 		if (index == VT_ENOTSUP || index == VT_ERROR) {
804 			/* it might be an EFI label */
805 			index = efi_alloc_and_read(fd, &efi_vtoc);
806 			if (index >= 0)
807 				label_type = LABEL_TYPE_EFI;
808 		}
809 	}
810 
811 	if (index < 0) {
812 		/*
813 		 * Since both attempts to read the label failed, we're
814 		 * going to fall back to a brute force approach to
815 		 * determining the device's size:  see how far out we can
816 		 * perform reads on the device.
817 		 */
818 
819 		slicesize = brute_force_get_device_size(fd);
820 		if (slicesize == 0) {
821 			switch (index) {
822 			case VT_ERROR:
823 				(void) fprintf(stderr, gettext(
824 				    "newfs: %s: %s\n"), name, strerror(errno));
825 				exit(10);
826 				/*NOTREACHED*/
827 			case VT_EIO:
828 				fatal(gettext(
829 				    "%s: I/O error accessing VTOC"), name);
830 				/*NOTREACHED*/
831 			case VT_EINVAL:
832 				fatal(gettext(
833 				    "%s: Invalid field in VTOC"), name);
834 				/*NOTREACHED*/
835 			default:
836 				fatal(gettext(
837 				    "%s: unknown error accessing VTOC"),
838 				    name);
839 				/*NOTREACHED*/
840 			}
841 		} else {
842 			label_type = LABEL_TYPE_OTHER;
843 		}
844 	}
845 
846 	if (label_type == LABEL_TYPE_EFI) {
847 		slicesize = efi_vtoc->efi_parts[index].p_size;
848 		efi_free(efi_vtoc);
849 	} else if (label_type == LABEL_TYPE_VTOC) {
850 		/*
851 		 * In the vtoc struct, p_size is a 32-bit signed quantity.
852 		 * In the dk_gpt struct (efi's version of the vtoc), p_size
853 		 * is an unsigned 64-bit quantity.  By casting the vtoc's
854 		 * psize to an unsigned 32-bit quantity, it will be copied
855 		 * to 'slicesize' (an unsigned 64-bit diskaddr_t) without
856 		 * sign extension.
857 		 */
858 
859 		slicesize = (uint32_t)vtoc.v_part[index].p_size;
860 	}
861 
862 	return (slicesize);
863 }
864 
865 /*
866  * brute_force_get_device_size
867  *
868  * Determine the size of the device by seeing how far we can
869  * read.  Doing an llseek( , , SEEK_END) would probably work
870  * in most cases, but we've seen at least one third-party driver
871  * which doesn't correctly support the SEEK_END option when the
872  * the device is greater than a terabyte.
873  */
874 
875 static diskaddr_t
876 brute_force_get_device_size(int fd)
877 {
878 	diskaddr_t	min_fail = 0;
879 	diskaddr_t	max_succeed = 0;
880 	diskaddr_t	cur_db_off;
881 	char 		buf[DEV_BSIZE];
882 
883 	/*
884 	 * First, see if we can read the device at all, just to
885 	 * eliminate errors that have nothing to do with the
886 	 * device's size.
887 	 */
888 
889 	if (((llseek(fd, (offset_t)0, SEEK_SET)) == -1) ||
890 	    ((read(fd, buf, DEV_BSIZE)) == -1))
891 		return (0);  /* can't determine size */
892 
893 	/*
894 	 * Now, go sequentially through the multiples of 4TB
895 	 * to find the first read that fails (this isn't strictly
896 	 * the most efficient way to find the actual size if the
897 	 * size really could be anything between 0 and 2**64 bytes.
898 	 * We expect the sizes to be less than 16 TB for some time,
899 	 * so why do a bunch of reads that are larger than that?
900 	 * However, this algorithm *will* work for sizes of greater
901 	 * than 16 TB.  We're just not optimizing for those sizes.)
902 	 */
903 
904 	for (cur_db_off = SECTORS_PER_TERABYTE * 4;
905 	    min_fail == 0 && cur_db_off < FS_SIZE_UPPER_LIMIT;
906 	    cur_db_off += 4 * SECTORS_PER_TERABYTE) {
907 		if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE),
908 		    SEEK_SET)) == -1) ||
909 		    ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE))
910 			min_fail = cur_db_off;
911 		else
912 			max_succeed = cur_db_off;
913 	}
914 
915 	if (min_fail == 0)
916 		return (0);
917 
918 	/*
919 	 * We now know that the size of the device is less than
920 	 * min_fail and greater than or equal to max_succeed.  Now
921 	 * keep splitting the difference until the actual size in
922 	 * sectors in known.  We also know that the difference
923 	 * between max_succeed and min_fail at this time is
924 	 * 4 * SECTORS_PER_TERABYTE, which is a power of two, which
925 	 * simplifies the math below.
926 	 */
927 
928 	while (min_fail - max_succeed > 1) {
929 		cur_db_off = max_succeed + (min_fail - max_succeed)/2;
930 		if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE),
931 		    SEEK_SET)) == -1) ||
932 		    ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE))
933 			min_fail = cur_db_off;
934 		else
935 			max_succeed = cur_db_off;
936 	}
937 
938 	/* the size is the last successfully read sector offset plus one */
939 	return (max_succeed + 1);
940 }
941 
942 /*
943  * validate_size
944  *
945  * Return 1 if the device appears to be at least "size" sectors long.
946  * Return 0 if it's shorter or we can't read it.
947  */
948 
949 static int
950 validate_size(char *disk, diskaddr_t size)
951 {
952 	char 		buf[DEV_BSIZE];
953 	int fd, rc;
954 
955 	if ((fd = open64(disk, O_RDONLY)) < 0) {
956 		perror(disk);
957 		exit(1);
958 	}
959 
960 	if ((llseek(fd, (offset_t)((size - 1) * DEV_BSIZE), SEEK_SET) == -1) ||
961 	    (read(fd, buf, DEV_BSIZE)) != DEV_BSIZE)
962 		rc = 0;
963 	else
964 		rc = 1;
965 	(void) close(fd);
966 	return (rc);
967 }
968 
969 /*
970  * read_sb(char * rawdev) - Attempt to read the superblock from a raw device
971  *
972  * Returns:
973  *	0 :
974  *		Could not read a valid superblock for a variety of reasons.
975  *		Since 'newfs' handles any fatal conditions, we're not going
976  *		to make any guesses as to why this is failing or what should
977  *		be done about it.
978  *
979  *	struct fs *:
980  *		A pointer to (what we think is) a valid superblock. The
981  *		space for the superblock is static (inside the function)
982  *		since we will only be reading the values from it.
983  */
984 
985 struct fs *
986 read_sb(char *fsdev)
987 {
988 	static struct fs	sblock;
989 	struct stat64		statb;
990 	int			dskfd;
991 	char			*bufp = NULL;
992 	int			bufsz = 0;
993 
994 	if (stat64(fsdev, &statb) < 0)
995 		return (0);
996 
997 	if ((dskfd = open64(fsdev, O_RDONLY)) < 0)
998 		return (0);
999 
1000 	/*
1001 	 * We need a buffer whose size is a multiple of DEV_BSIZE in order
1002 	 * to read from a raw device (which we were probably passed).
1003 	 */
1004 	bufsz = ((sizeof (sblock) / DEV_BSIZE) + 1) * DEV_BSIZE;
1005 	if ((bufp = malloc(bufsz)) == NULL) {
1006 		(void) close(dskfd);
1007 		return (0);
1008 	}
1009 
1010 	if (llseek(dskfd, (offset_t)SBOFF, SEEK_SET) < 0 ||
1011 	    read(dskfd, bufp, bufsz) < 0) {
1012 		(void) close(dskfd);
1013 		free(bufp);
1014 		return (0);
1015 	}
1016 	(void) close(dskfd);	/* Done with the file */
1017 
1018 	(void) memcpy(&sblock, bufp, sizeof (sblock));
1019 	free(bufp);	/* Don't need this anymore */
1020 
1021 	if (((sblock.fs_magic != FS_MAGIC) &&
1022 	    (sblock.fs_magic != MTB_UFS_MAGIC)) ||
1023 	    sblock.fs_ncg < 1 || sblock.fs_cpg < 1)
1024 		return (0);
1025 
1026 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
1027 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
1028 		return (0);
1029 
1030 	if (sblock.fs_sbsize < 0 || sblock.fs_sbsize > SBSIZE)
1031 		return (0);
1032 
1033 	return (&sblock);
1034 }
1035 
1036 /*
1037  * Read the UFS file system on the raw device SPECIAL.  If it does not
1038  * appear to be a UFS file system, return non-zero, indicating that
1039  * fsirand should be called (and it will spit out an error message).
1040  * If it is a UFS file system, take a look at the inodes in the first
1041  * cylinder group.  If they appear to be randomized (non-zero), return
1042  * zero, which will cause fsirand to not be called.  If the inode generation
1043  * counts are all zero, then we must call fsirand, so return non-zero.
1044  */
1045 
1046 #define	RANDOMIZED	0
1047 #define	NOT_RANDOMIZED	1
1048 
1049 static int
1050 notrand(char *special)
1051 {
1052 	long fsbuf[SBSIZE / sizeof (long)];
1053 	struct dinode dibuf[MAXBSIZE/sizeof (struct dinode)];
1054 	struct fs *fs;
1055 	struct dinode *dip;
1056 	offset_t seekaddr;
1057 	int bno, inum;
1058 	int fd;
1059 
1060 	fs = (struct fs *)fsbuf;
1061 	if ((fd = open64(special, 0)) == -1)
1062 		return (NOT_RANDOMIZED);
1063 	if (llseek(fd, (offset_t)SBLOCK * DEV_BSIZE, 0) == -1 ||
1064 	    read(fd, (char *)fs, SBSIZE) != SBSIZE ||
1065 	    ((fs->fs_magic != FS_MAGIC) && (fs->fs_magic != MTB_UFS_MAGIC))) {
1066 		(void) close(fd);
1067 		return (NOT_RANDOMIZED);
1068 	}
1069 
1070 	/* looks like a UFS file system; read the first cylinder group */
1071 	bsize = INOPB(fs) * sizeof (struct dinode);
1072 	inum = 0;
1073 	while (inum < fs->fs_ipg) {
1074 		bno = itod(fs, inum);
1075 		seekaddr = (offset_t)fsbtodb(fs, bno) * DEV_BSIZE;
1076 		if (llseek(fd, seekaddr, 0) == -1 ||
1077 		    read(fd, (char *)dibuf, bsize) != bsize) {
1078 			(void) close(fd);
1079 			return (NOT_RANDOMIZED);
1080 		}
1081 		for (dip = dibuf; dip < &dibuf[INOPB(fs)]; dip++) {
1082 			if (dip->di_gen != 0) {
1083 				(void) close(fd);
1084 				return (RANDOMIZED);
1085 			}
1086 			inum++;
1087 		}
1088 	}
1089 	(void) close(fd);
1090 	return (NOT_RANDOMIZED);
1091 }
1092 
1093 static void
1094 usage(void)
1095 {
1096 	(void) fprintf(stderr, gettext(
1097 	    "usage: newfs [ -v ] [ mkfs-options ] raw-special-device\n"));
1098 	(void) fprintf(stderr, gettext("where mkfs-options are:\n"));
1099 	(void) fprintf(stderr, gettext(
1100 	    "\t-N do not create file system, just print out parameters\n"));
1101 	(void) fprintf(stderr, gettext(
1102 "\t-T configure file system for eventual growth to over a terabyte\n"));
1103 	(void) fprintf(stderr, gettext("\t-s file system size (sectors)\n"));
1104 	(void) fprintf(stderr, gettext("\t-b block size\n"));
1105 	(void) fprintf(stderr, gettext("\t-f frag size\n"));
1106 	(void) fprintf(stderr, gettext("\t-t tracks/cylinder\n"));
1107 	(void) fprintf(stderr, gettext("\t-c cylinders/group\n"));
1108 	(void) fprintf(stderr, gettext("\t-m minimum free space %%\n"));
1109 	(void) fprintf(stderr, gettext(
1110 	    "\t-o optimization preference (`space' or `time')\n"));
1111 	(void) fprintf(stderr, gettext("\t-r revolutions/minute\n"));
1112 	(void) fprintf(stderr, gettext("\t-i number of bytes per inode\n"));
1113 	(void) fprintf(stderr, gettext(
1114 	    "\t-a number of alternates per cylinder\n"));
1115 	(void) fprintf(stderr, gettext("\t-C maxcontig\n"));
1116 	(void) fprintf(stderr, gettext("\t-d rotational delay\n"));
1117 	(void) fprintf(stderr, gettext(
1118 	    "\t-n number of rotational positions\n"));
1119 	(void) fprintf(stderr, gettext(
1120 "\t-S print a textual version of the calculated superblock to stdout\n"));
1121 	(void) fprintf(stderr, gettext(
1122 "\t-B dump a binary version of the calculated superblock to stdout\n"));
1123 }
1124 
1125 /*
1126  * Error-detecting version of atoi(3).  Adapted from mkfs' number().
1127  */
1128 static unsigned int
1129 number(char *param, char *value, int flags, int def_value)
1130 {
1131 	char *cs;
1132 	int n;
1133 	int cut = INT_MAX / 10;    /* limit to avoid overflow */
1134 	int minus = 0;
1135 
1136 	cs = value;
1137 	if (*cs == '-') {
1138 		minus = 1;
1139 		cs += 1;
1140 	}
1141 	if ((*cs < '0') || (*cs > '9')) {
1142 		goto bail_out;
1143 	}
1144 	n = 0;
1145 	while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) {
1146 		n = n*10 + *cs++ - '0';
1147 	}
1148 	if (minus)
1149 	    n = -n;
1150 	for (;;) {
1151 		switch (*cs++) {
1152 		case '\0':
1153 			return (n);
1154 
1155 		case '0': case '1': case '2': case '3': case '4':
1156 		case '5': case '6': case '7': case '8': case '9':
1157 			(void) fprintf(stderr, gettext(
1158 			    "newfs: value for %s overflowed, using %d\n"),
1159 			    param, def_value);
1160 			return (def_value);
1161 
1162 		case '%':
1163 			if (flags & NR_PERCENT)
1164 				break;
1165 			/* FALLTHROUGH */
1166 
1167 		default:
1168 bail_out:
1169 			fatal(gettext("bad numeric arg for %s: \"%s\""),
1170 			    param, value);
1171 
1172 		}
1173 	}
1174 	/* NOTREACHED */
1175 }
1176 
1177 /*
1178  * Error-detecting version of atoi(3).  Adapted from mkfs' number().
1179  */
1180 static int64_t
1181 number64(char *param, char *value, int flags, int64_t def_value)
1182 {
1183 	char *cs;
1184 	int64_t n;
1185 	int64_t cut = FS_SIZE_UPPER_LIMIT/ 10;    /* limit to avoid overflow */
1186 	int minus = 0;
1187 
1188 	cs = value;
1189 	if (*cs == '-') {
1190 		minus = 1;
1191 		cs += 1;
1192 	}
1193 	if ((*cs < '0') || (*cs > '9')) {
1194 		goto bail_out;
1195 	}
1196 	n = 0;
1197 	while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) {
1198 		n = n*10 + *cs++ - '0';
1199 	}
1200 	if (minus)
1201 	    n = -n;
1202 	for (;;) {
1203 		switch (*cs++) {
1204 		case '\0':
1205 			return (n);
1206 
1207 		case '0': case '1': case '2': case '3': case '4':
1208 		case '5': case '6': case '7': case '8': case '9':
1209 			(void) fprintf(stderr, gettext(
1210 			    "newfs: value for %s overflowed, using %d\n"),
1211 			    param, def_value);
1212 			return (def_value);
1213 
1214 		case '%':
1215 			if (flags & NR_PERCENT)
1216 				break;
1217 			/* FALLTHROUGH */
1218 
1219 		default:
1220 bail_out:
1221 			fatal(gettext("bad numeric arg for %s: \"%s\""),
1222 			    param, value);
1223 
1224 		}
1225 	}
1226 	/* NOTREACHED */
1227 }
1228