xref: /illumos-gate/usr/src/cmd/fs.d/ufs/mkfs/mkfs.c (revision bbf21555)
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
5d1a180b0Smaheshvs  * Common Development and Distribution License (the "License").
6d1a180b0Smaheshvs  * 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  */
2123a1cceaSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
2323a1cceaSRoger A. Faulkner  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27fe12dc75SToomas Soome /*	All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
396a1a5bb9SJohn Levon /*
406a1a5bb9SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
416a1a5bb9SJohn Levon  */
426a1a5bb9SJohn Levon 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * The maximum supported file system size (in sectors) is the
457c478bd9Sstevel@tonic-gate  * number of frags that can be represented in an int32_t field
467c478bd9Sstevel@tonic-gate  * (INT_MAX) times the maximum number of sectors per frag.  Since
477c478bd9Sstevel@tonic-gate  * the maximum frag size is MAXBSIZE, the maximum number of sectors
487c478bd9Sstevel@tonic-gate  * per frag is MAXBSIZE/DEV_BSIZE.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate #define	FS_MAX	(((diskaddr_t)INT_MAX) * (MAXBSIZE/DEV_BSIZE))
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * make file system for cylinder-group style file systems
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * usage:
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  *    mkfs [-F FSType] [-V] [-G [-P]] [-M dirname] [-m] [options]
587c478bd9Sstevel@tonic-gate  *	[-o specific_options]  special size
597c478bd9Sstevel@tonic-gate  *	[nsect ntrack bsize fsize cpg	minfree	rps nbpi opt apc rotdelay
607c478bd9Sstevel@tonic-gate  *	  2     3      4     5     6	7	8   9	 10  11  12
617c478bd9Sstevel@tonic-gate  *	nrpos maxcontig mtb]
627c478bd9Sstevel@tonic-gate  *	13    14	15
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  *  where specific_options are:
657c478bd9Sstevel@tonic-gate  *	N - no create
667c478bd9Sstevel@tonic-gate  *	nsect - The number of sectors per track
677c478bd9Sstevel@tonic-gate  *	ntrack - The number of tracks per cylinder
687c478bd9Sstevel@tonic-gate  *	bsize - block size
697c478bd9Sstevel@tonic-gate  *	fragsize - fragment size
707c478bd9Sstevel@tonic-gate  *	cgsize - The number of disk cylinders per cylinder group.
71fe12dc75SToomas Soome  *	free - minimum free space
727c478bd9Sstevel@tonic-gate  *	rps - rotational speed (rev/sec).
737c478bd9Sstevel@tonic-gate  *	nbpi - number of data bytes per allocated inode
747c478bd9Sstevel@tonic-gate  *	opt - optimization (space, time)
757c478bd9Sstevel@tonic-gate  *	apc - number of alternates
767c478bd9Sstevel@tonic-gate  *	gap - gap size
777c478bd9Sstevel@tonic-gate  *	nrpos - number of rotational positions
787c478bd9Sstevel@tonic-gate  *	maxcontig - maximum number of logical blocks that will be
797c478bd9Sstevel@tonic-gate  *		allocated contiguously before inserting rotational delay
807c478bd9Sstevel@tonic-gate  *	mtb - if "y", set up file system for eventual growth to over a
817c478bd9Sstevel@tonic-gate  *		a terabyte
827c478bd9Sstevel@tonic-gate  * -P Do not grow the file system, but print on stdout the maximal
837c478bd9Sstevel@tonic-gate  *    size in sectors to which the file system can be increased. The calculated
847c478bd9Sstevel@tonic-gate  *    size is limited by the value provided by the operand size.
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  * Note that -P is a project-private interface and together with -G intended
877c478bd9Sstevel@tonic-gate  * to be used only by the growfs script. It is therefore purposely not
887c478bd9Sstevel@tonic-gate  * documented in the man page.
897c478bd9Sstevel@tonic-gate  * The -P option is covered by PSARC case 2003/422.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * The following constants set the defaults used for the number
947c478bd9Sstevel@tonic-gate  * of sectors/track (fs_nsect), and number of tracks/cyl (fs_ntrak).
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  *			NSECT		NTRAK
977c478bd9Sstevel@tonic-gate  *	72MB CDC	18		9
987c478bd9Sstevel@tonic-gate  *	30MB CDC	18		5
997c478bd9Sstevel@tonic-gate  *	720KB Diskette	9		2
1006451fdbcSvsakar  *
1016451fdbcSvsakar  * However the defaults will be different for disks larger than CHSLIMIT.
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #define	DFLNSECT	32
1057c478bd9Sstevel@tonic-gate #define	DFLNTRAK	16
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1086451fdbcSvsakar  * The following default sectors and tracks values are used for
1096451fdbcSvsakar  * non-efi disks that are larger than the CHS addressing limit. The
1106451fdbcSvsakar  * existing default cpg of 16 (DESCPG) holds good for larger disks too.
1117c478bd9Sstevel@tonic-gate  */
1126451fdbcSvsakar #define	DEF_SECTORS_EFI	128
1136451fdbcSvsakar #define	DEF_TRACKS_EFI	48
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * The maximum number of cylinders in a group depends upon how much
1177c478bd9Sstevel@tonic-gate  * information can be stored on a single cylinder. The default is to
1187c478bd9Sstevel@tonic-gate  * use 16 cylinders per group.  This is effectively tradition - it was
1197c478bd9Sstevel@tonic-gate  * the largest value acceptable under SunOs 4.1
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate #define	DESCPG		16	/* desired fs_cpg */
1227c478bd9Sstevel@tonic-gate 
1236451fdbcSvsakar /*
1246451fdbcSvsakar  * The following two constants set the default block and fragment sizes.
1256451fdbcSvsakar  * Both constants must be a power of 2 and meet the following constraints:
1266451fdbcSvsakar  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
1276451fdbcSvsakar  *	DEV_BSIZE <= DESFRAGSIZE <= DESBLKSIZE
1286451fdbcSvsakar  *	DESBLKSIZE / DESFRAGSIZE <= 8
1296451fdbcSvsakar  */
1306451fdbcSvsakar #define	DESBLKSIZE	8192
1316451fdbcSvsakar #define	DESFRAGSIZE	1024
1326451fdbcSvsakar 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * MINFREE gives the minimum acceptable percentage of file system
1357c478bd9Sstevel@tonic-gate  * blocks which may be free. If the freelist drops below this level
1367c478bd9Sstevel@tonic-gate  * only the superuser may continue to allocate blocks. This may
1377c478bd9Sstevel@tonic-gate  * be set to 0 if no reserve of free blocks is deemed necessary,
1387c478bd9Sstevel@tonic-gate  * however throughput drops by fifty percent if the file system
1397c478bd9Sstevel@tonic-gate  * is run at between 90% and 100% full; thus the default value of
1407c478bd9Sstevel@tonic-gate  * fs_minfree is 10%. With 10% free space, fragmentation is not a
1417c478bd9Sstevel@tonic-gate  * problem, so we choose to optimize for time.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate #define	MINFREE		10
1447c478bd9Sstevel@tonic-gate #define	DEFAULTOPT	FS_OPTTIME
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * ROTDELAY gives the minimum number of milliseconds to initiate
1487c478bd9Sstevel@tonic-gate  * another disk transfer on the same cylinder. It is no longer used
1497c478bd9Sstevel@tonic-gate  * and will always default to 0.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate #define	ROTDELAY	0
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * MAXBLKPG determines the maximum number of data blocks which are
1557c478bd9Sstevel@tonic-gate  * placed in a single cylinder group. The default is one indirect
1567c478bd9Sstevel@tonic-gate  * block worth of data blocks.
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate #define	MAXBLKPG(bsize)	((bsize) / sizeof (daddr32_t))
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Each file system has a number of inodes statically allocated.
1627c478bd9Sstevel@tonic-gate  * We allocate one inode slot per NBPI bytes, expecting this
1637c478bd9Sstevel@tonic-gate  * to be far more than we will ever need.
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate #define	NBPI		2048	/* Number Bytes Per Inode */
1667c478bd9Sstevel@tonic-gate #define	MTB_NBPI	(MB)	/* Number Bytes Per Inode for multi-terabyte */
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Disks are assumed to rotate at 60HZ, unless otherwise specified.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate #define	DEFHZ		60
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  * Cylinder group related limits.
1757c478bd9Sstevel@tonic-gate  *
1767c478bd9Sstevel@tonic-gate  * For each cylinder we keep track of the availability of blocks at different
1777c478bd9Sstevel@tonic-gate  * rotational positions, so that we can lay out the data to be picked
1787c478bd9Sstevel@tonic-gate  * up with minimum rotational latency.  NRPOS is the number of rotational
1797c478bd9Sstevel@tonic-gate  * positions which we distinguish.  With NRPOS 8 the resolution of our
1807c478bd9Sstevel@tonic-gate  * summary information is 2ms for a typical 3600 rpm drive.
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate #define	NRPOS		8	/* number distinct rotational positions */
1837c478bd9Sstevel@tonic-gate 
1846451fdbcSvsakar #ifdef DEBUG
1856451fdbcSvsakar #define	dprintf(x)	printf x
1866451fdbcSvsakar #else
1876451fdbcSvsakar #define	dprintf(x)
1886451fdbcSvsakar #endif
1896451fdbcSvsakar 
1906451fdbcSvsakar /*
1916451fdbcSvsakar  * For the -N option, when calculating the backup superblocks, do not print
1926451fdbcSvsakar  * them if we are not really sure. We may have to try an alternate method of
1936451fdbcSvsakar  * arriving at the superblocks. So defer printing till a handful of superblocks
1946451fdbcSvsakar  * look good.
1956451fdbcSvsakar  */
1966451fdbcSvsakar #define	tprintf(x)	if (Nflag && retry) \
1976d24e334Svsakar 				(void) strncat(tmpbuf, x, strlen(x)); \
1986451fdbcSvsakar 			else \
1996451fdbcSvsakar 				(void) fprintf(stderr, x);
2006451fdbcSvsakar 
2016451fdbcSvsakar #define	ALTSB		32	/* Location of first backup superblock */
2026451fdbcSvsakar 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  * range_check "user_supplied" flag values.
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate #define	RC_DEFAULT	0
2077c478bd9Sstevel@tonic-gate #define	RC_KEYWORD	1
2087c478bd9Sstevel@tonic-gate #define	RC_POSITIONAL	2
2097c478bd9Sstevel@tonic-gate 
210303bf60bSsdebnath /*
211303bf60bSsdebnath  * ufs hole
212303bf60bSsdebnath  */
213303bf60bSsdebnath #define	UFS_HOLE	-1
214303bf60bSsdebnath 
2157c478bd9Sstevel@tonic-gate #ifndef	STANDALONE
2167c478bd9Sstevel@tonic-gate #include	<stdio.h>
2177c478bd9Sstevel@tonic-gate #include	<sys/mnttab.h>
2187c478bd9Sstevel@tonic-gate #endif
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate #include	<stdlib.h>
2217c478bd9Sstevel@tonic-gate #include	<unistd.h>
2227c478bd9Sstevel@tonic-gate #include	<malloc.h>
2237c478bd9Sstevel@tonic-gate #include	<string.h>
2247c478bd9Sstevel@tonic-gate #include	<strings.h>
2257c478bd9Sstevel@tonic-gate #include	<ctype.h>
2267c478bd9Sstevel@tonic-gate #include	<errno.h>
2277c478bd9Sstevel@tonic-gate #include	<sys/param.h>
2287c478bd9Sstevel@tonic-gate #include	<time.h>
2297c478bd9Sstevel@tonic-gate #include	<sys/types.h>
2307c478bd9Sstevel@tonic-gate #include	<sys/sysmacros.h>
2317c478bd9Sstevel@tonic-gate #include	<sys/vnode.h>
2327c478bd9Sstevel@tonic-gate #include	<sys/fs/ufs_fsdir.h>
2337c478bd9Sstevel@tonic-gate #include	<sys/fs/ufs_inode.h>
2347c478bd9Sstevel@tonic-gate #include	<sys/fs/ufs_fs.h>
2357c478bd9Sstevel@tonic-gate #include	<sys/fs/ufs_log.h>
2367c478bd9Sstevel@tonic-gate #include	<sys/mntent.h>
2377c478bd9Sstevel@tonic-gate #include	<sys/filio.h>
2387c478bd9Sstevel@tonic-gate #include	<limits.h>
2397c478bd9Sstevel@tonic-gate #include	<sys/int_const.h>
2407c478bd9Sstevel@tonic-gate #include	<signal.h>
2417c478bd9Sstevel@tonic-gate #include	<sys/efi_partition.h>
2426a1a5bb9SJohn Levon #include	<fslib.h>
2437c478bd9Sstevel@tonic-gate #include	"roll_log.h"
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate #define	bcopy(f, t, n)    (void) memcpy(t, f, n)
2467c478bd9Sstevel@tonic-gate #define	bzero(s, n)	(void) memset(s, 0, n)
2477c478bd9Sstevel@tonic-gate #define	bcmp(s, d, n)	memcmp(s, d, n)
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate #define	index(s, r)	strchr(s, r)
2507c478bd9Sstevel@tonic-gate #define	rindex(s, r)	strrchr(s, r)
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
2537c478bd9Sstevel@tonic-gate #include	<sys/statvfs.h>
2547c478bd9Sstevel@tonic-gate #include	<locale.h>
2557c478bd9Sstevel@tonic-gate #include	<fcntl.h>
256fe12dc75SToomas Soome #include	<sys/isa_defs.h>	/* for ENDIAN defines */
2577c478bd9Sstevel@tonic-gate #include	<sys/vtoc.h>
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate #include	<sys/dkio.h>
2607c478bd9Sstevel@tonic-gate #include	<sys/asynch.h>
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate extern offset_t	llseek();
2637c478bd9Sstevel@tonic-gate extern char	*getfullblkname();
2647c478bd9Sstevel@tonic-gate extern long	lrand48();
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate extern int	optind;
2677c478bd9Sstevel@tonic-gate extern char	*optarg;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate  * The size of a cylinder group is calculated by CGSIZE. The maximum size
2727c478bd9Sstevel@tonic-gate  * is limited by the fact that cylinder groups are at most one block.
2737c478bd9Sstevel@tonic-gate  * Its size is derived from the size of the maps maintained in the
2747c478bd9Sstevel@tonic-gate  * cylinder group and the (struct cg) size.
2757c478bd9Sstevel@tonic-gate  */
2767c478bd9Sstevel@tonic-gate #define	CGSIZE(fs) \
2777c478bd9Sstevel@tonic-gate 	/* base cg		*/ (sizeof (struct cg) + \
2787c478bd9Sstevel@tonic-gate 	/* blktot size	*/ (fs)->fs_cpg * sizeof (long) + \
2797c478bd9Sstevel@tonic-gate 	/* blks size	*/ (fs)->fs_cpg * (fs)->fs_nrpos * sizeof (short) + \
2807c478bd9Sstevel@tonic-gate 	/* inode map	*/ howmany((fs)->fs_ipg, NBBY) + \
2817c478bd9Sstevel@tonic-gate 	/* block map */ howmany((fs)->fs_cpg * (fs)->fs_spc / NSPF(fs), NBBY))
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * We limit the size of the inode map to be no more than a
2857c478bd9Sstevel@tonic-gate  * third of the cylinder group space, since we must leave at
2867c478bd9Sstevel@tonic-gate  * least an equal amount of space for the block map.
2877c478bd9Sstevel@tonic-gate  *
2887c478bd9Sstevel@tonic-gate  * N.B.: MAXIpG must be a multiple of INOPB(fs).
2897c478bd9Sstevel@tonic-gate  */
2907c478bd9Sstevel@tonic-gate #define	MAXIpG(fs)	roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * Same as MAXIpG, but parameterized by the block size (b) and the
2947c478bd9Sstevel@tonic-gate  * cylinder group divisor (d), which is the reciprocal of the fraction of the
2957c478bd9Sstevel@tonic-gate  * cylinder group overhead block that is used for the inode map.  So for
2967c478bd9Sstevel@tonic-gate  * example, if d = 5, the macro's computation assumes that 1/5 of the
2977c478bd9Sstevel@tonic-gate  * cylinder group overhead block can be dedicated to the inode map.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate #define	MAXIpG_B(b, d)	roundup((b) * NBBY / (d), (b) / sizeof (struct dinode))
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate #define	UMASK		0755
3027c478bd9Sstevel@tonic-gate #define	MAXINOPB	(MAXBSIZE / sizeof (struct dinode))
3037c478bd9Sstevel@tonic-gate #define	POWEROF2(num)	(((num) & ((num) - 1)) == 0)
3047c478bd9Sstevel@tonic-gate #define	MB		(1024*1024)
3057c478bd9Sstevel@tonic-gate #define	BETWEEN(x, l, h)	((x) >= (l) && (x) <= (h))
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /*
3087c478bd9Sstevel@tonic-gate  * Used to set the inode generation number. Since both inodes and dinodes
3097c478bd9Sstevel@tonic-gate  * are dealt with, we really need a pointer to an icommon here.
3107c478bd9Sstevel@tonic-gate  */
3117c478bd9Sstevel@tonic-gate #define	IRANDOMIZE(icp)	(icp)->ic_gen = lrand48();
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate  * Flags for number()
3157c478bd9Sstevel@tonic-gate  */
3167c478bd9Sstevel@tonic-gate #define	ALLOW_PERCENT	0x01	/* allow trailing `%' on number */
3177c478bd9Sstevel@tonic-gate #define	ALLOW_MS1	0x02	/* allow trailing `ms', state 1 */
3187c478bd9Sstevel@tonic-gate #define	ALLOW_MS2	0x04	/* allow trailing `ms', state 2 */
3197c478bd9Sstevel@tonic-gate #define	ALLOW_END_ONLY	0x08	/* must be at end of number & suffixes */
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate #define	MAXAIO	1000	/* maximum number of outstanding I/O's we'll manage */
3227c478bd9Sstevel@tonic-gate #define	BLOCK	1	/* block in aiowait */
3237c478bd9Sstevel@tonic-gate #define	NOBLOCK	0	/* don't block in aiowait */
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate #define	RELEASE 1	/* free an aio buffer after use */
3267c478bd9Sstevel@tonic-gate #define	SAVE	0	/* don't free the buffer */
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate typedef struct aio_trans {
3297c478bd9Sstevel@tonic-gate 	aio_result_t resultbuf;
3307c478bd9Sstevel@tonic-gate 	diskaddr_t bno;
3317c478bd9Sstevel@tonic-gate 	char *buffer;
3327c478bd9Sstevel@tonic-gate 	int size;
3337c478bd9Sstevel@tonic-gate 	int release;
3347c478bd9Sstevel@tonic-gate 	struct aio_trans *next;
3357c478bd9Sstevel@tonic-gate } aio_trans;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate typedef struct aio_results {
3387c478bd9Sstevel@tonic-gate 	int max;
3397c478bd9Sstevel@tonic-gate 	int outstanding;
3407c478bd9Sstevel@tonic-gate 	int maxpend;
3417c478bd9Sstevel@tonic-gate 	aio_trans *trans;
3427c478bd9Sstevel@tonic-gate } aio_results;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate int aio_inited = 0;
3457c478bd9Sstevel@tonic-gate aio_results results;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * Allow up to MAXBUF aio requests that each have a unique buffer.
3497c478bd9Sstevel@tonic-gate  * More aio's might be done, but not using memory through the getbuf()
3507c478bd9Sstevel@tonic-gate  * interface.  This can be raised, but you run into the potential of
3517c478bd9Sstevel@tonic-gate  * using more memory than is physically available on the machine,
3527c478bd9Sstevel@tonic-gate  * and if you start swapping, you can forget about performance.
3537c478bd9Sstevel@tonic-gate  * To prevent this, we also limit the total memory used for a given
3547c478bd9Sstevel@tonic-gate  * type of buffer to MAXBUFMEM.
3557c478bd9Sstevel@tonic-gate  *
3567c478bd9Sstevel@tonic-gate  * Tests indicate a cylinder group's worth of inodes takes:
3577c478bd9Sstevel@tonic-gate  *
3587c478bd9Sstevel@tonic-gate  *	NBPI	Size of Inode Buffer
3597c478bd9Sstevel@tonic-gate  *	 2k	1688k
3607c478bd9Sstevel@tonic-gate  *	 8k	 424k
3617c478bd9Sstevel@tonic-gate  *
3627c478bd9Sstevel@tonic-gate  * initcg() stores all the inodes for a cylinder group in one buffer,
3637c478bd9Sstevel@tonic-gate  * so allowing 20 buffers could take 32 MB if not limited by MAXBUFMEM.
3647c478bd9Sstevel@tonic-gate  */
3657c478bd9Sstevel@tonic-gate #define	MAXBUF		20
3667c478bd9Sstevel@tonic-gate #define	MAXBUFMEM	(8 * 1024 * 1024)
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate  * header information for buffers managed by getbuf() and freebuf()
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate typedef struct bufhdr {
3727c478bd9Sstevel@tonic-gate 	struct bufhdr *head;
3737c478bd9Sstevel@tonic-gate 	struct bufhdr *next;
3747c478bd9Sstevel@tonic-gate } bufhdr;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate int bufhdrsize;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate bufhdr inodebuf = { NULL, NULL };
3797c478bd9Sstevel@tonic-gate bufhdr cgsumbuf = { NULL, NULL };
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate #define	SECTORS_PER_TERABYTE	(1LL << 31)
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  * The following constant specifies an upper limit for file system size
3847c478bd9Sstevel@tonic-gate  * that is actually a lot bigger than we expect to support with UFS. (Since
3857c478bd9Sstevel@tonic-gate  * it's specified in sectors, the file system size would be 2**44 * 512,
3867c478bd9Sstevel@tonic-gate  * which is 2**53, which is 8192 Terabytes.)  However, it's useful
3877c478bd9Sstevel@tonic-gate  * for checking the basic sanity of a size value that is input on the
3887c478bd9Sstevel@tonic-gate  * command line.
3897c478bd9Sstevel@tonic-gate  */
3907c478bd9Sstevel@tonic-gate #define	FS_SIZE_UPPER_LIMIT	0x100000000000LL
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate /*
3937c478bd9Sstevel@tonic-gate  * Forward declarations
3947c478bd9Sstevel@tonic-gate  */
3957c478bd9Sstevel@tonic-gate static char *getbuf(bufhdr *bufhead, int size);
3967c478bd9Sstevel@tonic-gate static void freebuf(char *buf);
3977c478bd9Sstevel@tonic-gate static void freetrans(aio_trans *transp);
3987c478bd9Sstevel@tonic-gate static aio_trans *get_aiop();
3997c478bd9Sstevel@tonic-gate static aio_trans *wait_for_write(int block);
4007c478bd9Sstevel@tonic-gate static void initcg(int cylno);
4017c478bd9Sstevel@tonic-gate static void fsinit();
4027c478bd9Sstevel@tonic-gate static int makedir(struct direct *protodir, int entries);
4037c478bd9Sstevel@tonic-gate static void iput(struct inode *ip);
4047c478bd9Sstevel@tonic-gate static void rdfs(diskaddr_t bno, int size, char *bf);
4057c478bd9Sstevel@tonic-gate static void wtfs(diskaddr_t bno, int size, char *bf);
4067c478bd9Sstevel@tonic-gate static void awtfs(diskaddr_t bno, int size, char *bf, int release);
4077c478bd9Sstevel@tonic-gate static void wtfs_breakup(diskaddr_t bno, int size, char *bf);
4087c478bd9Sstevel@tonic-gate static int isblock(struct fs *fs, unsigned char *cp, int h);
4097c478bd9Sstevel@tonic-gate static void clrblock(struct fs *fs, unsigned char *cp, int h);
4107c478bd9Sstevel@tonic-gate static void setblock(struct fs *fs, unsigned char *cp, int h);
411f45bbb21SToomas Soome static void usage(void) __NORETURN;
4127c478bd9Sstevel@tonic-gate static void dump_fscmd(char *fsys, int fsi);
4137c478bd9Sstevel@tonic-gate static uint64_t number(uint64_t d_value, char *param, int flags);
4147c478bd9Sstevel@tonic-gate static int match(char *s);
4157c478bd9Sstevel@tonic-gate static char checkopt(char *optim);
4167c478bd9Sstevel@tonic-gate static char checkmtb(char *mtbarg);
4177c478bd9Sstevel@tonic-gate static void range_check(long *varp, char *name, long minimum,
4187c478bd9Sstevel@tonic-gate     long maximum, long def_val, int user_supplied);
4197c478bd9Sstevel@tonic-gate static void range_check_64(uint64_t *varp, char *name, uint64_t minimum,
4207c478bd9Sstevel@tonic-gate     uint64_t maximum, uint64_t def_val, int user_supplied);
4217c478bd9Sstevel@tonic-gate static daddr32_t alloc(int size, int mode);
4227c478bd9Sstevel@tonic-gate static diskaddr_t get_max_size(int fd);
4237c478bd9Sstevel@tonic-gate static long get_max_track_size(int fd);
4247c478bd9Sstevel@tonic-gate static void block_sigint(sigset_t *old_mask);
4257c478bd9Sstevel@tonic-gate static void unblock_sigint(sigset_t *old_mask);
4267c478bd9Sstevel@tonic-gate static void recover_from_sigint(int signum);
4277c478bd9Sstevel@tonic-gate static int confirm_abort(void);
42823a1cceaSRoger A. Faulkner static int getaline(FILE *fp, char *loc, int maxlen);
4297c478bd9Sstevel@tonic-gate static void flush_writes(void);
4307c478bd9Sstevel@tonic-gate static long compute_maxcpg(long, long, long, long, long);
4317c478bd9Sstevel@tonic-gate static int in_64bit_mode(void);
4327c478bd9Sstevel@tonic-gate static int validate_size(int fd, diskaddr_t size);
433355d6bb5Sswilcox static void dump_sblock(void);
4347c478bd9Sstevel@tonic-gate 
435140e9cb2Sprabahar /*
436140e9cb2Sprabahar  * Workaround for mkfs to function properly on disks attached to XMIT 2.X
437140e9cb2Sprabahar  * controller. If the address is not aligned at 8 byte boundary, mkfs on
438140e9cb2Sprabahar  * disks attached to XMIT 2.X controller exhibts un-predictable behaviour.
439140e9cb2Sprabahar  */
440140e9cb2Sprabahar #define	XMIT_2_X_ALIGN	8
441140e9cb2Sprabahar #pragma	align XMIT_2_X_ALIGN(fsun, altfsun, cgun)
442140e9cb2Sprabahar 
4437c478bd9Sstevel@tonic-gate union {
4447c478bd9Sstevel@tonic-gate 	struct fs fs;
4457c478bd9Sstevel@tonic-gate 	char pad[SBSIZE];
4466451fdbcSvsakar } fsun, altfsun;
4477c478bd9Sstevel@tonic-gate #define	sblock	fsun.fs
4486451fdbcSvsakar #define	altsblock	altfsun.fs
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate struct	csum *fscs;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate union cgun {
4537c478bd9Sstevel@tonic-gate 	struct cg cg;
4547c478bd9Sstevel@tonic-gate 	char pad[MAXBSIZE];
4557c478bd9Sstevel@tonic-gate } cgun;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate #define	acg	cgun.cg
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate  * Size of screen in cols in which to fit output
4607c478bd9Sstevel@tonic-gate  */
4617c478bd9Sstevel@tonic-gate #define	WIDTH	80
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate struct dinode zino[MAXBSIZE / sizeof (struct dinode)];
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate  * file descriptors used for rdfs(fsi) and wtfs(fso).
4677c478bd9Sstevel@tonic-gate  * Initialized to an illegal file descriptor number.
4687c478bd9Sstevel@tonic-gate  */
4697c478bd9Sstevel@tonic-gate int	fsi = -1;
4707c478bd9Sstevel@tonic-gate int	fso = -1;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate  * The BIG parameter is machine dependent.  It should be a longlong integer
4747c478bd9Sstevel@tonic-gate  * constant that can be used by the number parser to check the validity
4757c478bd9Sstevel@tonic-gate  * of numeric parameters.
4767c478bd9Sstevel@tonic-gate  */
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate #define	BIG		0x7fffffffffffffffLL
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /* Used to indicate to number() that a bogus value should cause us to exit */
4817c478bd9Sstevel@tonic-gate #define	NO_DEFAULT	LONG_MIN
4827c478bd9Sstevel@tonic-gate 
4836451fdbcSvsakar /*
4846451fdbcSvsakar  * INVALIDSBLIMIT is the number of bad backup superblocks that will be
4856451fdbcSvsakar  * tolerated before we decide to try arriving at a different set of them
4866451fdbcSvsakar  * using a different logic. This is applicable for non-EFI disks only.
4876451fdbcSvsakar  */
4886451fdbcSvsakar #define	INVALIDSBLIMIT	10
4896451fdbcSvsakar 
4907c478bd9Sstevel@tonic-gate /*
4917c478bd9Sstevel@tonic-gate  * The *_flag variables are used to indicate that the user specified
4927c478bd9Sstevel@tonic-gate  * the values, rather than that we made them up ourselves.  We can
4937c478bd9Sstevel@tonic-gate  * complain about the user giving us bogus values.
4947c478bd9Sstevel@tonic-gate  */
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /* semi-constants */
4977c478bd9Sstevel@tonic-gate long	sectorsize = DEV_BSIZE;		/* bytes/sector from param.h */
4987c478bd9Sstevel@tonic-gate long	bbsize = BBSIZE;		/* boot block size */
4997c478bd9Sstevel@tonic-gate long	sbsize = SBSIZE;		/* superblock size */
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate /* parameters */
5027c478bd9Sstevel@tonic-gate diskaddr_t	fssize_db;		/* file system size in disk blocks */
5037c478bd9Sstevel@tonic-gate diskaddr_t	fssize_frag;		/* file system size in frags */
5047c478bd9Sstevel@tonic-gate long	cpg;				/* cylinders/cylinder group */
5057c478bd9Sstevel@tonic-gate int	cpg_flag = RC_DEFAULT;
5067c478bd9Sstevel@tonic-gate long	rotdelay = -1;			/* rotational delay between blocks */
5077c478bd9Sstevel@tonic-gate int	rotdelay_flag = RC_DEFAULT;
5087c478bd9Sstevel@tonic-gate long	maxcontig;			/* max contiguous blocks to allocate */
5097c478bd9Sstevel@tonic-gate int	maxcontig_flag = RC_DEFAULT;
5107c478bd9Sstevel@tonic-gate long	nsect = DFLNSECT;		/* sectors per track */
5117c478bd9Sstevel@tonic-gate int	nsect_flag = RC_DEFAULT;
5127c478bd9Sstevel@tonic-gate long	ntrack = DFLNTRAK;		/* tracks per cylinder group */
5137c478bd9Sstevel@tonic-gate int	ntrack_flag = RC_DEFAULT;
5147c478bd9Sstevel@tonic-gate long	bsize = DESBLKSIZE;		/* filesystem block size */
5157c478bd9Sstevel@tonic-gate int	bsize_flag = RC_DEFAULT;
516fe12dc75SToomas Soome long	fragsize = DESFRAGSIZE;		/* filesystem fragment size */
5177c478bd9Sstevel@tonic-gate int	fragsize_flag = RC_DEFAULT;
518fe12dc75SToomas Soome long	minfree = MINFREE;		/* fs_minfree */
5197c478bd9Sstevel@tonic-gate int	minfree_flag = RC_DEFAULT;
5207c478bd9Sstevel@tonic-gate long	rps = DEFHZ;			/* revolutions/second of drive */
5217c478bd9Sstevel@tonic-gate int	rps_flag = RC_DEFAULT;
5227c478bd9Sstevel@tonic-gate long	nbpi = NBPI;			/* number of bytes per inode */
5237c478bd9Sstevel@tonic-gate int	nbpi_flag = RC_DEFAULT;
5247c478bd9Sstevel@tonic-gate long	nrpos = NRPOS;			/* number of rotational positions */
5257c478bd9Sstevel@tonic-gate int	nrpos_flag = RC_DEFAULT;
5267c478bd9Sstevel@tonic-gate long	apc = 0;			/* alternate sectors per cylinder */
5277c478bd9Sstevel@tonic-gate int	apc_flag = RC_DEFAULT;
5287c478bd9Sstevel@tonic-gate char	opt = 't';			/* optimization style, `t' or `s' */
5297c478bd9Sstevel@tonic-gate char	mtb = 'n';			/* multi-terabyte format, 'y' or 'n' */
530d50c8f90Svsakar #define	DEFAULT_SECT_TRAK_CPG	(nsect_flag == RC_DEFAULT && \
531d50c8f90Svsakar 				ntrack_flag == RC_DEFAULT && \
532d50c8f90Svsakar 				cpg_flag == RC_DEFAULT)
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate long	debug = 0;			/* enable debugging output */
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate int	spc_flag = 0;			/* alternate sectors specified or */
5377c478bd9Sstevel@tonic-gate 					/* found */
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /* global state */
5407c478bd9Sstevel@tonic-gate int	Nflag;		/* do not write to disk */
5417c478bd9Sstevel@tonic-gate int	mflag;		/* return the command line used to create this FS */
542355d6bb5Sswilcox int	rflag;		/* report the superblock in an easily-parsed form */
543355d6bb5Sswilcox int	Rflag;		/* dump the superblock in binary */
5447c478bd9Sstevel@tonic-gate char	*fsys;
5457c478bd9Sstevel@tonic-gate time_t	mkfstime;
5467c478bd9Sstevel@tonic-gate char	*string;
5476451fdbcSvsakar int	label_type;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate /*
5507c478bd9Sstevel@tonic-gate  * logging support
5517c478bd9Sstevel@tonic-gate  */
5525f10ef69SYuri Pankov int	islog;			/* true if ufs logging is enabled */
5535f10ef69SYuri Pankov int	islogok;		/* true if ufs log state is good */
5545f10ef69SYuri Pankov int	waslog;			/* true when ufs logging disabled during grow */
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  * growfs defines, globals, and forward references
5587c478bd9Sstevel@tonic-gate  */
5597c478bd9Sstevel@tonic-gate #define	NOTENOUGHSPACE 33
5607c478bd9Sstevel@tonic-gate int		grow;
561d50c8f90Svsakar #define	GROW_WITH_DEFAULT_TRAK	(grow && ntrack_flag == RC_DEFAULT)
562d50c8f90Svsakar 
5637c478bd9Sstevel@tonic-gate static int	Pflag;		/* probe to which size the fs can be grown */
5647c478bd9Sstevel@tonic-gate int		ismounted;
5657c478bd9Sstevel@tonic-gate char		*directory;
5667c478bd9Sstevel@tonic-gate diskaddr_t	grow_fssize;
5677c478bd9Sstevel@tonic-gate long		grow_fs_size;
5687c478bd9Sstevel@tonic-gate long		grow_fs_ncg;
5697c478bd9Sstevel@tonic-gate diskaddr_t		grow_fs_csaddr;
5707c478bd9Sstevel@tonic-gate long		grow_fs_cssize;
5717c478bd9Sstevel@tonic-gate int		grow_fs_clean;
5727c478bd9Sstevel@tonic-gate struct csum	*grow_fscs;
5737c478bd9Sstevel@tonic-gate diskaddr_t		grow_sifrag;
5747c478bd9Sstevel@tonic-gate int		test;
5757c478bd9Sstevel@tonic-gate int		testforce;
5767c478bd9Sstevel@tonic-gate diskaddr_t		testfrags;
5777c478bd9Sstevel@tonic-gate int		inlockexit;
5787c478bd9Sstevel@tonic-gate int		isbad;
5797c478bd9Sstevel@tonic-gate 
580f45bbb21SToomas Soome void		lockexit(int) __NORETURN;
5817c478bd9Sstevel@tonic-gate void		randomgeneration(void);
5827c478bd9Sstevel@tonic-gate void		checksummarysize(void);
5836451fdbcSvsakar int		checksblock(struct fs, int);
5847c478bd9Sstevel@tonic-gate void		growinit(char *);
5857c478bd9Sstevel@tonic-gate void		checkdev(char *, char  *);
5867c478bd9Sstevel@tonic-gate void		checkmount(struct mnttab *, char *);
5877c478bd9Sstevel@tonic-gate struct dinode	*gdinode(ino_t);
5887c478bd9Sstevel@tonic-gate int		csfraginrange(daddr32_t);
5897c478bd9Sstevel@tonic-gate struct csfrag	*findcsfrag(daddr32_t, struct csfrag **);
5907c478bd9Sstevel@tonic-gate void		checkindirect(ino_t, daddr32_t *, daddr32_t, int);
5917c478bd9Sstevel@tonic-gate void		addcsfrag(ino_t, daddr32_t, struct csfrag **);
5927c478bd9Sstevel@tonic-gate void		delcsfrag(daddr32_t, struct csfrag **);
5937c478bd9Sstevel@tonic-gate void		checkdirect(ino_t, daddr32_t *, daddr32_t *, int);
5947c478bd9Sstevel@tonic-gate void		findcsfragino(void);
5957c478bd9Sstevel@tonic-gate void		fixindirect(daddr32_t, int);
5967c478bd9Sstevel@tonic-gate void		fixdirect(caddr_t, daddr32_t, daddr32_t *, int);
5977c478bd9Sstevel@tonic-gate void		fixcsfragino(void);
5987c478bd9Sstevel@tonic-gate void		extendsummaryinfo(void);
5997c478bd9Sstevel@tonic-gate int		notenoughspace(void);
6007c478bd9Sstevel@tonic-gate void		unalloccsfragino(void);
6017c478bd9Sstevel@tonic-gate void		unalloccsfragfree(void);
6027c478bd9Sstevel@tonic-gate void		findcsfragfree(void);
6037c478bd9Sstevel@tonic-gate void		copycsfragino(void);
6047c478bd9Sstevel@tonic-gate void		rdcg(long);
6057c478bd9Sstevel@tonic-gate void		wtcg(void);
6067c478bd9Sstevel@tonic-gate void		flcg(void);
6077c478bd9Sstevel@tonic-gate void		allocfrags(long, daddr32_t *, long *);
6087c478bd9Sstevel@tonic-gate void		alloccsfragino(void);
6097c478bd9Sstevel@tonic-gate void		alloccsfragfree(void);
6107c478bd9Sstevel@tonic-gate void		freefrags(daddr32_t, long, long);
6117c478bd9Sstevel@tonic-gate int		findfreerange(long *, long *);
6127c478bd9Sstevel@tonic-gate void		resetallocinfo(void);
6137c478bd9Sstevel@tonic-gate void		extendcg(long);
6147c478bd9Sstevel@tonic-gate void		ulockfs(void);
6157c478bd9Sstevel@tonic-gate void		wlockfs(void);
6167c478bd9Sstevel@tonic-gate void		clockfs(void);
6177c478bd9Sstevel@tonic-gate void		wtsb(void);
6187c478bd9Sstevel@tonic-gate static int64_t	checkfragallocated(daddr32_t);
619fe12dc75SToomas Soome static struct csum	*read_summaryinfo(struct fs *);
620fe12dc75SToomas Soome static diskaddr_t	probe_summaryinfo();
6217c478bd9Sstevel@tonic-gate 
622d1a180b0Smaheshvs int
main(int argc,char * argv[])6237c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	long i, mincpc, mincpg, ibpcl;
6267c478bd9Sstevel@tonic-gate 	long cylno, rpos, blk, j, warn = 0;
6277c478bd9Sstevel@tonic-gate 	long mincpgcnt, maxcpg;
6287c478bd9Sstevel@tonic-gate 	uint64_t used, bpcg, inospercg;
6297c478bd9Sstevel@tonic-gate 	long mapcramped, inodecramped;
6307c478bd9Sstevel@tonic-gate 	long postblsize, rotblsize, totalsbsize;
6317c478bd9Sstevel@tonic-gate 	FILE *mnttab;
6327c478bd9Sstevel@tonic-gate 	struct mnttab mntp;
6337c478bd9Sstevel@tonic-gate 	char *special;
6347c478bd9Sstevel@tonic-gate 	struct statvfs64 fs;
6356451fdbcSvsakar 	struct dk_geom dkg;
63665908c77Syu, larry liu - Sun Microsystems - Beijing China 	struct dk_minfo dkminfo;
6377c478bd9Sstevel@tonic-gate 	char pbuf[sizeof (uint64_t) * 3 + 1];
6386451fdbcSvsakar 	char *tmpbuf;
6397c478bd9Sstevel@tonic-gate 	int width, plen;
6407c478bd9Sstevel@tonic-gate 	uint64_t num;
6417c478bd9Sstevel@tonic-gate 	int c, saverr;
6427c478bd9Sstevel@tonic-gate 	diskaddr_t max_fssize;
6437c478bd9Sstevel@tonic-gate 	long tmpmaxcontig = -1;
6447c478bd9Sstevel@tonic-gate 	struct sigaction sigact;
6457c478bd9Sstevel@tonic-gate 	uint64_t nbytes64;
6467c478bd9Sstevel@tonic-gate 	int remaining_cg;
6477c478bd9Sstevel@tonic-gate 	int do_dot = 0;
6488b7c2cdfSws 	int use_efi_dflts = 0, retry = 0, isremovable = 0, ishotpluggable = 0;
6496d24e334Svsakar 	int invalid_sb_cnt, ret, skip_this_sb, cg_too_small;
6506d24e334Svsakar 	int geom_nsect, geom_ntrack, geom_cpg;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
6557c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
6567c478bd9Sstevel@tonic-gate #endif
6577c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "F:bmo:VPGM:T:t:")) != EOF) {
6607c478bd9Sstevel@tonic-gate 		switch (c) {
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		case 'F':
6637c478bd9Sstevel@tonic-gate 			string = optarg;
6647c478bd9Sstevel@tonic-gate 			if (strcmp(string, "ufs") != 0)
6657c478bd9Sstevel@tonic-gate 				usage();
6667c478bd9Sstevel@tonic-gate 			break;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 		case 'm':	/* return command line used to create this FS */
6697c478bd9Sstevel@tonic-gate 			mflag++;
6707c478bd9Sstevel@tonic-gate 			break;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 		case 'o':
6737c478bd9Sstevel@tonic-gate 			/*
6747c478bd9Sstevel@tonic-gate 			 * ufs specific options.
6757c478bd9Sstevel@tonic-gate 			 */
6767c478bd9Sstevel@tonic-gate 			string = optarg;
6777c478bd9Sstevel@tonic-gate 			while (*string != '\0') {
6787c478bd9Sstevel@tonic-gate 				if (match("nsect=")) {
6797c478bd9Sstevel@tonic-gate 					nsect = number(DFLNSECT, "nsect", 0);
6807c478bd9Sstevel@tonic-gate 					nsect_flag = RC_KEYWORD;
6817c478bd9Sstevel@tonic-gate 				} else if (match("ntrack=")) {
6827c478bd9Sstevel@tonic-gate 					ntrack = number(DFLNTRAK, "ntrack", 0);
6837c478bd9Sstevel@tonic-gate 					ntrack_flag = RC_KEYWORD;
6847c478bd9Sstevel@tonic-gate 				} else if (match("bsize=")) {
6857c478bd9Sstevel@tonic-gate 					bsize = number(DESBLKSIZE, "bsize", 0);
6867c478bd9Sstevel@tonic-gate 					bsize_flag = RC_KEYWORD;
6877c478bd9Sstevel@tonic-gate 				} else if (match("fragsize=")) {
6887c478bd9Sstevel@tonic-gate 					fragsize = number(DESFRAGSIZE,
6897c478bd9Sstevel@tonic-gate 					    "fragsize", 0);
6907c478bd9Sstevel@tonic-gate 					fragsize_flag = RC_KEYWORD;
6917c478bd9Sstevel@tonic-gate 				} else if (match("cgsize=")) {
6927c478bd9Sstevel@tonic-gate 					cpg = number(DESCPG, "cgsize", 0);
6937c478bd9Sstevel@tonic-gate 					cpg_flag = RC_KEYWORD;
6947c478bd9Sstevel@tonic-gate 				} else if (match("free=")) {
6957c478bd9Sstevel@tonic-gate 					minfree = number(MINFREE, "free",
6967c478bd9Sstevel@tonic-gate 					    ALLOW_PERCENT);
6977c478bd9Sstevel@tonic-gate 					minfree_flag = RC_KEYWORD;
6987c478bd9Sstevel@tonic-gate 				} else if (match("maxcontig=")) {
6997c478bd9Sstevel@tonic-gate 					tmpmaxcontig =
7007c478bd9Sstevel@tonic-gate 					    number(-1, "maxcontig", 0);
7017c478bd9Sstevel@tonic-gate 					maxcontig_flag = RC_KEYWORD;
7027c478bd9Sstevel@tonic-gate 				} else if (match("nrpos=")) {
7037c478bd9Sstevel@tonic-gate 					nrpos = number(NRPOS, "nrpos", 0);
7047c478bd9Sstevel@tonic-gate 					nrpos_flag = RC_KEYWORD;
7057c478bd9Sstevel@tonic-gate 				} else if (match("rps=")) {
7067c478bd9Sstevel@tonic-gate 					rps = number(DEFHZ, "rps", 0);
7077c478bd9Sstevel@tonic-gate 					rps_flag = RC_KEYWORD;
7087c478bd9Sstevel@tonic-gate 				} else if (match("nbpi=")) {
7097c478bd9Sstevel@tonic-gate 					nbpi = number(NBPI, "nbpi", 0);
7107c478bd9Sstevel@tonic-gate 					nbpi_flag = RC_KEYWORD;
7117c478bd9Sstevel@tonic-gate 				} else if (match("opt=")) {
7127c478bd9Sstevel@tonic-gate 					opt = checkopt(string);
7137c478bd9Sstevel@tonic-gate 				} else if (match("mtb=")) {
7147c478bd9Sstevel@tonic-gate 					mtb = checkmtb(string);
7157c478bd9Sstevel@tonic-gate 				} else if (match("apc=")) {
7167c478bd9Sstevel@tonic-gate 					apc = number(0, "apc", 0);
7177c478bd9Sstevel@tonic-gate 					apc_flag = RC_KEYWORD;
7187c478bd9Sstevel@tonic-gate 				} else if (match("gap=")) {
7197c478bd9Sstevel@tonic-gate 					(void) number(0, "gap", ALLOW_MS1);
7207c478bd9Sstevel@tonic-gate 					rotdelay = ROTDELAY;
7217c478bd9Sstevel@tonic-gate 					rotdelay_flag = RC_DEFAULT;
7227c478bd9Sstevel@tonic-gate 				} else if (match("debug=")) {
7237c478bd9Sstevel@tonic-gate 					debug = number(0, "debug", 0);
7247c478bd9Sstevel@tonic-gate 				} else if (match("N")) {
7257c478bd9Sstevel@tonic-gate 					Nflag++;
726355d6bb5Sswilcox 				} else if (match("calcsb")) {
727355d6bb5Sswilcox 					rflag++;
728355d6bb5Sswilcox 					Nflag++;
729355d6bb5Sswilcox 				} else if (match("calcbinsb")) {
730355d6bb5Sswilcox 					rflag++;
731355d6bb5Sswilcox 					Rflag++;
732355d6bb5Sswilcox 					Nflag++;
7337c478bd9Sstevel@tonic-gate 				} else if (*string == '\0') {
7347c478bd9Sstevel@tonic-gate 					break;
7357c478bd9Sstevel@tonic-gate 				} else {
7367c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
737d50c8f90Svsakar 					    "illegal option: %s\n"), string);
7387c478bd9Sstevel@tonic-gate 					usage();
7397c478bd9Sstevel@tonic-gate 				}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 				if (*string == ',') string++;
7427c478bd9Sstevel@tonic-gate 				if (*string == ' ') string++;
7437c478bd9Sstevel@tonic-gate 			}
7447c478bd9Sstevel@tonic-gate 			break;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 		case 'V':
7477c478bd9Sstevel@tonic-gate 			{
7487c478bd9Sstevel@tonic-gate 				char	*opt_text;
7497c478bd9Sstevel@tonic-gate 				int	opt_count;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, gettext("mkfs -F ufs "));
7527c478bd9Sstevel@tonic-gate 				for (opt_count = 1; opt_count < argc;
753d50c8f90Svsakar 				    opt_count++) {
7547c478bd9Sstevel@tonic-gate 					opt_text = argv[opt_count];
7557c478bd9Sstevel@tonic-gate 					if (opt_text)
756d50c8f90Svsakar 						(void) fprintf(stdout, " %s ",
757d50c8f90Svsakar 						    opt_text);
7587c478bd9Sstevel@tonic-gate 				}
7597c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "\n");
7607c478bd9Sstevel@tonic-gate 			}
7617c478bd9Sstevel@tonic-gate 			break;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		case 'b':	/* do nothing for this */
7647c478bd9Sstevel@tonic-gate 			break;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		case 'M':	/* grow the mounted file system */
7677c478bd9Sstevel@tonic-gate 			directory = optarg;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
7707c478bd9Sstevel@tonic-gate 		case 'G':	/* grow the file system */
7717c478bd9Sstevel@tonic-gate 			grow = 1;
7727c478bd9Sstevel@tonic-gate 			break;
773fe12dc75SToomas Soome 		case 'P':	/* probe the file system growing size	*/
7747c478bd9Sstevel@tonic-gate 			Pflag = 1;
7757c478bd9Sstevel@tonic-gate 			grow = 1; /* probe mode implies fs growing	*/
7767c478bd9Sstevel@tonic-gate 			break;
7777c478bd9Sstevel@tonic-gate 		case 'T':	/* For testing */
7787c478bd9Sstevel@tonic-gate 			testforce = 1;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
7817c478bd9Sstevel@tonic-gate 		case 't':
7827c478bd9Sstevel@tonic-gate 			test = 1;
7837c478bd9Sstevel@tonic-gate 			string = optarg;
7847c478bd9Sstevel@tonic-gate 			testfrags = number(NO_DEFAULT, "testfrags", 0);
7857c478bd9Sstevel@tonic-gate 			break;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 		case '?':
7887c478bd9Sstevel@tonic-gate 			usage();
7897c478bd9Sstevel@tonic-gate 			break;
7907c478bd9Sstevel@tonic-gate 		}
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate #ifdef MKFS_DEBUG
7937c478bd9Sstevel@tonic-gate 	/*
7947c478bd9Sstevel@tonic-gate 	 * Turning on MKFS_DEBUG causes mkfs to produce a filesystem
7957c478bd9Sstevel@tonic-gate 	 * that can be reproduced by setting the time to 0 and seeding
7967c478bd9Sstevel@tonic-gate 	 * the random number generator to a constant.
7977c478bd9Sstevel@tonic-gate 	 */
7987c478bd9Sstevel@tonic-gate 	mkfstime = 0;	/* reproducible results */
7997c478bd9Sstevel@tonic-gate #else
8007c478bd9Sstevel@tonic-gate 	(void) time(&mkfstime);
8017c478bd9Sstevel@tonic-gate #endif
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	if (optind >= (argc - 1)) {
8047c478bd9Sstevel@tonic-gate 		if (optind > (argc - 1)) {
8057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8067c478bd9Sstevel@tonic-gate 			    gettext("special not specified\n"));
8077c478bd9Sstevel@tonic-gate 			usage();
8087c478bd9Sstevel@tonic-gate 		} else if (mflag == 0) {
8097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8107c478bd9Sstevel@tonic-gate 			    gettext("size not specified\n"));
8117c478bd9Sstevel@tonic-gate 			usage();
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate 	argc -= optind;
8157c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	fsys = argv[0];
8187c478bd9Sstevel@tonic-gate 	fsi = open64(fsys, O_RDONLY);
8197c478bd9Sstevel@tonic-gate 	if (fsi < 0) {
8207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: cannot open\n"), fsys);
8217c478bd9Sstevel@tonic-gate 		lockexit(32);
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	if (mflag) {
8257c478bd9Sstevel@tonic-gate 		dump_fscmd(fsys, fsi);
8267c478bd9Sstevel@tonic-gate 		lockexit(0);
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	/*
8307c478bd9Sstevel@tonic-gate 	 * The task of setting all of the configuration parameters for a
8317c478bd9Sstevel@tonic-gate 	 * UFS file system is basically a matter of solving n equations
8327c478bd9Sstevel@tonic-gate 	 * in m variables.  Typically, m is greater than n, so there is
8337c478bd9Sstevel@tonic-gate 	 * usually more than one valid solution.  Since this is usually
8347c478bd9Sstevel@tonic-gate 	 * an under-constrained problem, it's not always obvious what the
8357c478bd9Sstevel@tonic-gate 	 * "best" configuration is.
8367c478bd9Sstevel@tonic-gate 	 *
8377c478bd9Sstevel@tonic-gate 	 * In general, the approach is to
8387c478bd9Sstevel@tonic-gate 	 * 1. Determine the values for the file system parameters
8397c478bd9Sstevel@tonic-gate 	 *    that are externally contrained and therefore not adjustable
8407c478bd9Sstevel@tonic-gate 	 *    by mkfs (such as the device's size and maxtransfer size).
8417c478bd9Sstevel@tonic-gate 	 * 2. Acquire the user's requested setting for all configuration
8427c478bd9Sstevel@tonic-gate 	 *    values that can be set on the command line.
8437c478bd9Sstevel@tonic-gate 	 * 3. Determine the final value of all configuration values, by
8447c478bd9Sstevel@tonic-gate 	 *    the following approach:
8457c478bd9Sstevel@tonic-gate 	 *	- set the file system block size (fs_bsize).  Although
8467c478bd9Sstevel@tonic-gate 	 *	  this could be regarded as an adjustable parameter, in
8477c478bd9Sstevel@tonic-gate 	 *	  fact, it's pretty much a constant.  At this time, it's
8487c478bd9Sstevel@tonic-gate 	 *	  generally set to 8k (with older hardware, it can
8497c478bd9Sstevel@tonic-gate 	 *	  sometimes make sense to set it to 4k, but those
8507c478bd9Sstevel@tonic-gate 	 *	  situations are pretty rare now).
8517c478bd9Sstevel@tonic-gate 	 *	- re-adjust the maximum file system size based on the
8527c478bd9Sstevel@tonic-gate 	 *	  value of the file system block size.  Since the
8537c478bd9Sstevel@tonic-gate 	 *	  frag size can't be any larger than a file system
8547c478bd9Sstevel@tonic-gate 	 *	  block, and the number of frags in the file system
8557c478bd9Sstevel@tonic-gate 	 *	  has to fit into 31 bits, the file system block size
8567c478bd9Sstevel@tonic-gate 	 *	  affects the maximum file system size.
8577c478bd9Sstevel@tonic-gate 	 *	- now that the real maximum file system is known, set the
8587c478bd9Sstevel@tonic-gate 	 *	  actual size of the file system to be created to
8597c478bd9Sstevel@tonic-gate 	 *	  MIN(requested size, maximum file system size).
8607c478bd9Sstevel@tonic-gate 	 *	- now validate, and if necessary, adjust the following
8617c478bd9Sstevel@tonic-gate 	 *	  values:
8627c478bd9Sstevel@tonic-gate 	 *		rotdelay
8637c478bd9Sstevel@tonic-gate 	 *		nsect
8647c478bd9Sstevel@tonic-gate 	 *		maxcontig
8657c478bd9Sstevel@tonic-gate 	 *		apc
8667c478bd9Sstevel@tonic-gate 	 *		frag_size
8677c478bd9Sstevel@tonic-gate 	 *		rps
8687c478bd9Sstevel@tonic-gate 	 *		minfree
8697c478bd9Sstevel@tonic-gate 	 *		nrpos
8707c478bd9Sstevel@tonic-gate 	 *		nrack
8717c478bd9Sstevel@tonic-gate 	 *		nbpi
8727c478bd9Sstevel@tonic-gate 	 *	- calculate maxcpg (the maximum value of the cylinders-per-
8737c478bd9Sstevel@tonic-gate 	 *	  cylinder-group configuration parameters).  There are two
8747c478bd9Sstevel@tonic-gate 	 *	  algorithms for calculating maxcpg:  an old one, which is
8757c478bd9Sstevel@tonic-gate 	 *	  used for file systems of less than 1 terabyte, and a
8767c478bd9Sstevel@tonic-gate 	 *	  new one, implemented in the function compute_maxcpg(),
8777c478bd9Sstevel@tonic-gate 	 *	  which is used for file systems of greater than 1 TB.
8787c478bd9Sstevel@tonic-gate 	 *	  The difference between them is that compute_maxcpg()
8797c478bd9Sstevel@tonic-gate 	 *	  really tries to maximize the cpg value.  The old
8807c478bd9Sstevel@tonic-gate 	 *	  algorithm fails to take advantage of smaller frags and
8817c478bd9Sstevel@tonic-gate 	 *	  lower inode density when determining the maximum cpg,
8827c478bd9Sstevel@tonic-gate 	 *	  and thus comes up with much lower numbers in some
8837c478bd9Sstevel@tonic-gate 	 *	  configurations.  At some point, we might use the
8847c478bd9Sstevel@tonic-gate 	 *	  new algorithm for determining maxcpg for all file
8857c478bd9Sstevel@tonic-gate 	 *	  systems, but at this time, the changes implemented for
8867c478bd9Sstevel@tonic-gate 	 *	  multi-terabyte UFS are NOT being automatically applied
8877c478bd9Sstevel@tonic-gate 	 *	  to UFS file systems of less than a terabyte (in the
8887c478bd9Sstevel@tonic-gate 	 *	  interest of not changing existing UFS policy too much
8897c478bd9Sstevel@tonic-gate 	 *	  until the ramifications of the changes are well-understood
8907c478bd9Sstevel@tonic-gate 	 *	  and have been evaluated for their effects on performance.)
8917c478bd9Sstevel@tonic-gate 	 *	- check the current values of the configuration parameters
8927c478bd9Sstevel@tonic-gate 	 *	  against the various constraints imposed by UFS.  These
8937c478bd9Sstevel@tonic-gate 	 *	  include:
8947c478bd9Sstevel@tonic-gate 	 *		* There must be at least one inode in each
8957c478bd9Sstevel@tonic-gate 	 *		  cylinder group.
8967c478bd9Sstevel@tonic-gate 	 *		* The cylinder group overhead block, which
8977c478bd9Sstevel@tonic-gate 	 *		  contains the inode and frag bigmaps, must fit
8987c478bd9Sstevel@tonic-gate 	 *		  within one file system block.
8997c478bd9Sstevel@tonic-gate 	 *		* The space required for inode maps should
9007c478bd9Sstevel@tonic-gate 	 *		  occupy no more than a third of the cylinder
9017c478bd9Sstevel@tonic-gate 	 *		  group overhead block.
9027c478bd9Sstevel@tonic-gate 	 *		* The rotational position tables have to fit
9037c478bd9Sstevel@tonic-gate 	 *		  within the available space in the super block.
9047c478bd9Sstevel@tonic-gate 	 *	  Adjust the configuration values that can be adjusted
9057c478bd9Sstevel@tonic-gate 	 *	  so that these constraints are satisfied.  The
9067c478bd9Sstevel@tonic-gate 	 *	  configuration values that are adjustable are:
9077c478bd9Sstevel@tonic-gate 	 *		* frag size
9087c478bd9Sstevel@tonic-gate 	 *		* cylinders per group
9097c478bd9Sstevel@tonic-gate 	 *		* inode density (can be increased)
9107c478bd9Sstevel@tonic-gate 	 *		* number of rotational positions (the rotational
9117c478bd9Sstevel@tonic-gate 	 *		  position tables are eliminated altogether if
9127c478bd9Sstevel@tonic-gate 	 *		  there isn't enough room for them.)
9137c478bd9Sstevel@tonic-gate 	 * 4. Set the values for all the dependent configuration
9147c478bd9Sstevel@tonic-gate 	 *    values (those that aren't settable on the command
9157c478bd9Sstevel@tonic-gate 	 *    line and which are completely dependent on the
9167c478bd9Sstevel@tonic-gate 	 *    adjustable parameters).  This include cpc (cycles
9177c478bd9Sstevel@tonic-gate 	 *    per cylinder, spc (sectors-per-cylinder), and many others.
9187c478bd9Sstevel@tonic-gate 	 */
9197c478bd9Sstevel@tonic-gate 
920d50c8f90Svsakar 	/*
921d50c8f90Svsakar 	 * Figure out the partition size and initialize the label_type.
922d50c8f90Svsakar 	 */
9237c478bd9Sstevel@tonic-gate 	max_fssize = get_max_size(fsi);
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	/*
9267c478bd9Sstevel@tonic-gate 	 * Get and check positional arguments, if any.
9277c478bd9Sstevel@tonic-gate 	 */
9287c478bd9Sstevel@tonic-gate 	switch (argc - 1) {
9297c478bd9Sstevel@tonic-gate 	default:
9307c478bd9Sstevel@tonic-gate 		usage();
9317c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
9327c478bd9Sstevel@tonic-gate 	case 15:
9337c478bd9Sstevel@tonic-gate 		mtb = checkmtb(argv[15]);
9347c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9357c478bd9Sstevel@tonic-gate 	case 14:
9367c478bd9Sstevel@tonic-gate 		string = argv[14];
9377c478bd9Sstevel@tonic-gate 		tmpmaxcontig = number(-1, "maxcontig", 0);
9387c478bd9Sstevel@tonic-gate 		maxcontig_flag = RC_POSITIONAL;
9397c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9407c478bd9Sstevel@tonic-gate 	case 13:
9417c478bd9Sstevel@tonic-gate 		string = argv[13];
9427c478bd9Sstevel@tonic-gate 		nrpos = number(NRPOS, "nrpos", 0);
9437c478bd9Sstevel@tonic-gate 		nrpos_flag = RC_POSITIONAL;
9447c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9457c478bd9Sstevel@tonic-gate 	case 12:
9467c478bd9Sstevel@tonic-gate 		string = argv[12];
9477c478bd9Sstevel@tonic-gate 		rotdelay = ROTDELAY;
9487c478bd9Sstevel@tonic-gate 		rotdelay_flag = RC_DEFAULT;
9497c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9507c478bd9Sstevel@tonic-gate 	case 11:
9517c478bd9Sstevel@tonic-gate 		string = argv[11];
9527c478bd9Sstevel@tonic-gate 		apc = number(0, "apc", 0);
9537c478bd9Sstevel@tonic-gate 		apc_flag = RC_POSITIONAL;
9547c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9557c478bd9Sstevel@tonic-gate 	case 10:
9567c478bd9Sstevel@tonic-gate 		opt = checkopt(argv[10]);
9577c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9587c478bd9Sstevel@tonic-gate 	case 9:
9597c478bd9Sstevel@tonic-gate 		string = argv[9];
9607c478bd9Sstevel@tonic-gate 		nbpi = number(NBPI, "nbpi", 0);
9617c478bd9Sstevel@tonic-gate 		nbpi_flag = RC_POSITIONAL;
9627c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9637c478bd9Sstevel@tonic-gate 	case 8:
9647c478bd9Sstevel@tonic-gate 		string = argv[8];
9657c478bd9Sstevel@tonic-gate 		rps = number(DEFHZ, "rps", 0);
9667c478bd9Sstevel@tonic-gate 		rps_flag = RC_POSITIONAL;
9677c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9687c478bd9Sstevel@tonic-gate 	case 7:
9697c478bd9Sstevel@tonic-gate 		string = argv[7];
9707c478bd9Sstevel@tonic-gate 		minfree = number(MINFREE, "free", ALLOW_PERCENT);
9717c478bd9Sstevel@tonic-gate 		minfree_flag = RC_POSITIONAL;
9727c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9737c478bd9Sstevel@tonic-gate 	case 6:
9747c478bd9Sstevel@tonic-gate 		string = argv[6];
9757c478bd9Sstevel@tonic-gate 		cpg = number(DESCPG, "cgsize", 0);
9767c478bd9Sstevel@tonic-gate 		cpg_flag = RC_POSITIONAL;
9777c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9787c478bd9Sstevel@tonic-gate 	case 5:
9797c478bd9Sstevel@tonic-gate 		string = argv[5];
9807c478bd9Sstevel@tonic-gate 		fragsize = number(DESFRAGSIZE, "fragsize", 0);
9817c478bd9Sstevel@tonic-gate 		fragsize_flag = RC_POSITIONAL;
9827c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9837c478bd9Sstevel@tonic-gate 	case 4:
9847c478bd9Sstevel@tonic-gate 		string = argv[4];
9857c478bd9Sstevel@tonic-gate 		bsize = number(DESBLKSIZE, "bsize", 0);
9867c478bd9Sstevel@tonic-gate 		bsize_flag = RC_POSITIONAL;
9877c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9887c478bd9Sstevel@tonic-gate 	case 3:
9897c478bd9Sstevel@tonic-gate 		string = argv[3];
9907c478bd9Sstevel@tonic-gate 		ntrack = number(DFLNTRAK, "ntrack", 0);
9917c478bd9Sstevel@tonic-gate 		ntrack_flag = RC_POSITIONAL;
9927c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9937c478bd9Sstevel@tonic-gate 	case 2:
9947c478bd9Sstevel@tonic-gate 		string = argv[2];
9957c478bd9Sstevel@tonic-gate 		nsect = number(DFLNSECT, "nsect", 0);
9967c478bd9Sstevel@tonic-gate 		nsect_flag = RC_POSITIONAL;
9977c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9987c478bd9Sstevel@tonic-gate 	case 1:
9997c478bd9Sstevel@tonic-gate 		string = argv[1];
10007c478bd9Sstevel@tonic-gate 		fssize_db = number(max_fssize, "size", 0);
10017c478bd9Sstevel@tonic-gate 	}
10027c478bd9Sstevel@tonic-gate 
1003d50c8f90Svsakar 	/*
1004d50c8f90Svsakar 	 * Initialize the parameters in the same way as newfs so that
1005d50c8f90Svsakar 	 * newfs and mkfs would result in the same file system layout
1006d50c8f90Svsakar 	 * for EFI labelled disks. Do this only in the absence of user
1007d50c8f90Svsakar 	 * specified values for these parameters.
1008d50c8f90Svsakar 	 */
1009d50c8f90Svsakar 	if (label_type == LABEL_TYPE_EFI) {
1010d50c8f90Svsakar 		if (apc_flag == RC_DEFAULT) apc = 0;
1011d50c8f90Svsakar 		if (nrpos_flag == RC_DEFAULT) nrpos = 1;
1012d50c8f90Svsakar 		if (ntrack_flag == RC_DEFAULT) ntrack = DEF_TRACKS_EFI;
1013d50c8f90Svsakar 		if (rps_flag == RC_DEFAULT) rps = DEFHZ;
1014d50c8f90Svsakar 		if (nsect_flag == RC_DEFAULT) nsect = DEF_SECTORS_EFI;
1015d50c8f90Svsakar 	}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	if ((maxcontig_flag == RC_DEFAULT) || (tmpmaxcontig == -1) ||
1018d50c8f90Svsakar 	    (maxcontig == -1)) {
10197c478bd9Sstevel@tonic-gate 		long maxtrax = get_max_track_size(fsi);
10207c478bd9Sstevel@tonic-gate 		maxcontig = maxtrax / bsize;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	} else {
10237c478bd9Sstevel@tonic-gate 		maxcontig = tmpmaxcontig;
10247c478bd9Sstevel@tonic-gate 	}
10256451fdbcSvsakar 	dprintf(("DeBuG maxcontig : %ld\n", maxcontig));
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	if (rotdelay == -1) {	/* default by newfs and mkfs */
10287c478bd9Sstevel@tonic-gate 		rotdelay = ROTDELAY;
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	if (cpg_flag == RC_DEFAULT) { /* If not explicity set, use default */
10327c478bd9Sstevel@tonic-gate 		cpg = DESCPG;
10337c478bd9Sstevel@tonic-gate 	}
10346451fdbcSvsakar 	dprintf(("DeBuG cpg : %ld\n", cpg));
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	/*
10377c478bd9Sstevel@tonic-gate 	 * Now that we have the semi-sane args, either positional, via -o,
10387c478bd9Sstevel@tonic-gate 	 * or by defaulting, handle inter-dependencies and range checks.
10397c478bd9Sstevel@tonic-gate 	 */
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	/*
10427c478bd9Sstevel@tonic-gate 	 * Settle the file system block size first, since it's a fixed
10437c478bd9Sstevel@tonic-gate 	 * parameter once set and so many other parameters, including
10447c478bd9Sstevel@tonic-gate 	 * max_fssize, depend on it.
10457c478bd9Sstevel@tonic-gate 	 */
10467c478bd9Sstevel@tonic-gate 	range_check(&bsize, "bsize", MINBSIZE, MAXBSIZE, DESBLKSIZE,
10477c478bd9Sstevel@tonic-gate 	    bsize_flag);
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	if (!POWEROF2(bsize)) {
10507c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
10517c478bd9Sstevel@tonic-gate 		    gettext("block size must be a power of 2, not %ld\n"),
10527c478bd9Sstevel@tonic-gate 		    bsize);
10537c478bd9Sstevel@tonic-gate 		bsize = DESBLKSIZE;
10547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
10557c478bd9Sstevel@tonic-gate 		    gettext("mkfs: bsize reset to default %ld\n"),
10567c478bd9Sstevel@tonic-gate 		    bsize);
10577c478bd9Sstevel@tonic-gate 	}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	if (fssize_db > max_fssize && validate_size(fsi, fssize_db)) {
10607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
10617c478bd9Sstevel@tonic-gate 		    "Warning: the requested size of this file system\n"
10627c478bd9Sstevel@tonic-gate 		    "(%lld sectors) is greater than the size of the\n"
10637c478bd9Sstevel@tonic-gate 		    "device reported by the driver (%lld sectors).\n"
10647c478bd9Sstevel@tonic-gate 		    "However, a read of the device at the requested size\n"
10657c478bd9Sstevel@tonic-gate 		    "does succeed, so the requested size will be used.\n"),
10667c478bd9Sstevel@tonic-gate 		    fssize_db, max_fssize);
10677c478bd9Sstevel@tonic-gate 		max_fssize = fssize_db;
10687c478bd9Sstevel@tonic-gate 	}
10697c478bd9Sstevel@tonic-gate 	/*
10707c478bd9Sstevel@tonic-gate 	 * Since the maximum allocatable unit (the frag) must be less than
10717c478bd9Sstevel@tonic-gate 	 * or equal to bsize, and the number of frags must be less than or
10727c478bd9Sstevel@tonic-gate 	 * equal to INT_MAX, the total size of the file system (in
10737c478bd9Sstevel@tonic-gate 	 * bytes) must be less than or equal to bsize * INT_MAX.
10747c478bd9Sstevel@tonic-gate 	 */
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	if (max_fssize > ((diskaddr_t)bsize/DEV_BSIZE) * INT_MAX)
10777c478bd9Sstevel@tonic-gate 		max_fssize = ((diskaddr_t)bsize/DEV_BSIZE) * INT_MAX;
1078d50c8f90Svsakar 
10797c478bd9Sstevel@tonic-gate 	range_check_64(&fssize_db, "size", 1024LL, max_fssize, max_fssize, 1);
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	if (fssize_db >= SECTORS_PER_TERABYTE) {
10827c478bd9Sstevel@tonic-gate 		mtb = 'y';
10837c478bd9Sstevel@tonic-gate 		if (!in_64bit_mode()) {
10847c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
10857c478bd9Sstevel@tonic-gate "mkfs:  Warning: Creating a file system greater than 1 terabyte on a\n"
10867c478bd9Sstevel@tonic-gate "       system running a 32-bit kernel.  This file system will not be\n"
10877c478bd9Sstevel@tonic-gate "       accessible until the system is rebooted with a 64-bit kernel.\n"));
10887c478bd9Sstevel@tonic-gate 		}
10897c478bd9Sstevel@tonic-gate 	}
1090d50c8f90Svsakar 	dprintf(("DeBuG mtb : %c\n", mtb));
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/*
1093*bbf21555SRichard Lowe 	 * With newer and much larger disks, the newfs(8) and mkfs_ufs(8)
10946451fdbcSvsakar 	 * commands had problems in correctly handling the "native" geometries
10956451fdbcSvsakar 	 * for various storage devices.
10966451fdbcSvsakar 	 *
1097*bbf21555SRichard Lowe 	 * To handle the new age disks, mkfs_ufs(8) will use the EFI style
10986451fdbcSvsakar 	 * for non-EFI disks that are larger than the CHS addressing limit
10996451fdbcSvsakar 	 * ( > 8GB approx ) and ignore the disk geometry information for
11006451fdbcSvsakar 	 * these drives. This is what is currently done for multi-terrabyte
11016451fdbcSvsakar 	 * filesystems on EFI disks.
11026451fdbcSvsakar 	 *
11036451fdbcSvsakar 	 * However if the user asked for a specific layout by supplying values
11046d24e334Svsakar 	 * for even one of the three parameters (nsect, ntrack, cpg), honour
11056d24e334Svsakar 	 * the user supplied parameters.
11066d24e334Svsakar 	 *
11076d24e334Svsakar 	 * Choosing EFI style or native geometry style can make a lot of
11086d24e334Svsakar 	 * difference, because the size of a cylinder group is dependent on
11096d24e334Svsakar 	 * this choice. This in turn means that the position of alternate
11106d24e334Svsakar 	 * superblocks varies depending on the style chosen. It is not
11116d24e334Svsakar 	 * necessary that all disks of size > CHSLIMIT have EFI style layout.
11126d24e334Svsakar 	 * There can be disks which are > CHSLIMIT size, but have native
11136d24e334Svsakar 	 * geometry style layout, thereby warranting the need for alternate
11146d24e334Svsakar 	 * logic in superblock detection.
11157c478bd9Sstevel@tonic-gate 	 */
1116d50c8f90Svsakar 	if (mtb != 'y' && (ntrack == -1 || GROW_WITH_DEFAULT_TRAK ||
1117d50c8f90Svsakar 	    DEFAULT_SECT_TRAK_CPG)) {
11186451fdbcSvsakar 		/*
11196d24e334Svsakar 		 * "-1" indicates that we were called from newfs and ntracks
11206d24e334Svsakar 		 * was not specified in newfs command line. Calculate nsect
11216451fdbcSvsakar 		 * and ntrack in the same manner as newfs.
11226451fdbcSvsakar 		 *
11236451fdbcSvsakar 		 * This is required because, the defaults for nsect and ntrack
11246451fdbcSvsakar 		 * is hardcoded in mkfs, whereas to generate the alternate
11256451fdbcSvsakar 		 * superblock locations for the -N option, there is a need for
11266451fdbcSvsakar 		 * the geometry based values that newfs would have arrived at.
11276451fdbcSvsakar 		 * Newfs would have arrived at these values as below.
11286451fdbcSvsakar 		 */
1129d50c8f90Svsakar 		if (label_type == LABEL_TYPE_EFI ||
1130d50c8f90Svsakar 		    label_type == LABEL_TYPE_OTHER) {
1131d50c8f90Svsakar 			use_efi_dflts = 1;
1132d50c8f90Svsakar 			retry = 1;
1133d50c8f90Svsakar 		} else if (ioctl(fsi, DKIOCGGEOM, &dkg)) {
1134d50c8f90Svsakar 			dprintf(("%s: Unable to read Disk geometry", fsys));
1135d50c8f90Svsakar 			perror(gettext("Unable to read Disk geometry"));
1136d50c8f90Svsakar 			lockexit(32);
11376451fdbcSvsakar 		} else {
1138d50c8f90Svsakar 			nsect = dkg.dkg_nsect;
1139d50c8f90Svsakar 			ntrack = dkg.dkg_nhead;
11406451fdbcSvsakar #ifdef i386	/* Bug 1170182 */
1141d50c8f90Svsakar 			if (ntrack > 32 && (ntrack % 16) != 0) {
1142d50c8f90Svsakar 				ntrack -= (ntrack % 16);
1143d50c8f90Svsakar 			}
11446451fdbcSvsakar #endif
1145d50c8f90Svsakar 			if (ioctl(fsi, DKIOCREMOVABLE, &isremovable)) {
11464d594c33Svsakar 				dprintf(("DeBuG Unable to determine if %s is"
11474d594c33Svsakar 				    " Removable Media. Proceeding with system"
11484d594c33Svsakar 				    " determined parameters.\n", fsys));
1149d50c8f90Svsakar 				isremovable = 0;
1150d50c8f90Svsakar 			}
11518b7c2cdfSws 			if (ioctl(fsi, DKIOCHOTPLUGGABLE, &ishotpluggable)) {
11528b7c2cdfSws 				dprintf(("DeBuG Unable to determine if %s is"
11538b7c2cdfSws 				    " Hotpluggable Media. Proceeding with "
11548b7c2cdfSws 				    "system determined parameters.\n", fsys));
11558b7c2cdfSws 				ishotpluggable = 0;
11568b7c2cdfSws 			}
1157342440ecSPrasad Singamsetty 			if ((((diskaddr_t)dkg.dkg_ncyl * dkg.dkg_nhead *
1158342440ecSPrasad Singamsetty 			    dkg.dkg_nsect) > CHSLIMIT) || isremovable ||
1159342440ecSPrasad Singamsetty 			    ishotpluggable) {
1160d50c8f90Svsakar 				use_efi_dflts = 1;
1161d50c8f90Svsakar 				retry = 1;
1162d50c8f90Svsakar 			}
11636451fdbcSvsakar 		}
11646451fdbcSvsakar 	}
1165342440ecSPrasad Singamsetty 	dprintf(("DeBuG CHSLIMIT = %d geom = %llu\n", CHSLIMIT,
1166342440ecSPrasad Singamsetty 	    (diskaddr_t)dkg.dkg_ncyl * dkg.dkg_nhead * dkg.dkg_nsect));
11678b7c2cdfSws 	dprintf(("DeBuG label_type = %d isremovable = %d ishotpluggable = %d "
11688b7c2cdfSws 	    "use_efi_dflts = %d\n", label_type, isremovable, ishotpluggable,
11698b7c2cdfSws 	    use_efi_dflts));
11706451fdbcSvsakar 
11716451fdbcSvsakar 	/*
11726451fdbcSvsakar 	 * For the newfs -N case, even if the disksize is > CHSLIMIT, do not
11736451fdbcSvsakar 	 * blindly follow EFI style. If the fs_version indicates a geometry
11746451fdbcSvsakar 	 * based layout, try that one first. If it fails we can always try the
11756451fdbcSvsakar 	 * other logic.
11766451fdbcSvsakar 	 *
11776451fdbcSvsakar 	 * If we were called from growfs, we will have a problem if we mix
11786451fdbcSvsakar 	 * and match the filesystem creation and growth styles. For example,
11796d24e334Svsakar 	 * if we create using EFI style, we have to also grow using EFI
11806451fdbcSvsakar 	 * style. So follow the style indicated by the fs_version.
11816451fdbcSvsakar 	 *
11826451fdbcSvsakar 	 * Read and verify the primary superblock. If it looks sane, use the
11836451fdbcSvsakar 	 * fs_version from the superblock. If the primary superblock does
11846451fdbcSvsakar 	 * not look good, read and verify the first alternate superblock at
11856451fdbcSvsakar 	 * ALTSB. Use the fs_version to decide whether to use the
11866451fdbcSvsakar 	 * EFI style logic or the old geometry based logic to calculate
11876451fdbcSvsakar 	 * the alternate superblock locations.
11886451fdbcSvsakar 	 */
11896451fdbcSvsakar 	if ((Nflag && use_efi_dflts) || (grow)) {
11906451fdbcSvsakar 		if (grow && ntrack_flag != RC_DEFAULT)
11916d24e334Svsakar 			goto start_fs_creation;
11926451fdbcSvsakar 		rdfs((diskaddr_t)(SBOFF / sectorsize), (int)sbsize,
1193d50c8f90Svsakar 		    (char *)&altsblock);
11946451fdbcSvsakar 		ret = checksblock(altsblock, 1);
11956451fdbcSvsakar 
11966451fdbcSvsakar 		if (!ret) {
11976451fdbcSvsakar 			if (altsblock.fs_magic == MTB_UFS_MAGIC) {
11986451fdbcSvsakar 				mtb = 'y';
11996d24e334Svsakar 				goto start_fs_creation;
12006451fdbcSvsakar 			}
12016451fdbcSvsakar 			use_efi_dflts = (altsblock.fs_version ==
1202d50c8f90Svsakar 			    UFS_EFISTYLE4NONEFI_VERSION_2) ? 1 : 0;
12036451fdbcSvsakar 		} else {
12046451fdbcSvsakar 			/*
12056451fdbcSvsakar 			 * The primary superblock didn't help in determining
12066451fdbcSvsakar 			 * the fs_version. Try the first alternate superblock.
12076451fdbcSvsakar 			 */
12086451fdbcSvsakar 			dprintf(("DeBuG checksblock() failed - error : %d"
1209d50c8f90Svsakar 			    " for sb : %d\n", ret, SBOFF/sectorsize));
12106451fdbcSvsakar 			rdfs((diskaddr_t)ALTSB, (int)sbsize,
1211d50c8f90Svsakar 			    (char *)&altsblock);
12126451fdbcSvsakar 			ret = checksblock(altsblock, 1);
12136451fdbcSvsakar 
12146451fdbcSvsakar 			if (!ret) {
1215d50c8f90Svsakar 				if (altsblock.fs_magic == MTB_UFS_MAGIC) {
1216d50c8f90Svsakar 					mtb = 'y';
1217d50c8f90Svsakar 					goto start_fs_creation;
1218d50c8f90Svsakar 				}
1219d50c8f90Svsakar 				use_efi_dflts = (altsblock.fs_version ==
1220d50c8f90Svsakar 				    UFS_EFISTYLE4NONEFI_VERSION_2) ? 1 : 0;
12216d24e334Svsakar 			}
12226d24e334Svsakar 			dprintf(("DeBuG checksblock() returned : %d"
1223d50c8f90Svsakar 			    " for sb : %d\n", ret, ALTSB));
12246451fdbcSvsakar 		}
12256451fdbcSvsakar 	}
12266451fdbcSvsakar 
12276d24e334Svsakar 	geom_nsect = nsect;
12286d24e334Svsakar 	geom_ntrack = ntrack;
12296d24e334Svsakar 	geom_cpg = cpg;
12306d24e334Svsakar 	dprintf(("DeBuG geom_nsect=%d, geom_ntrack=%d, geom_cpg=%d\n",
1231d50c8f90Svsakar 	    geom_nsect, geom_ntrack, geom_cpg));
12326d24e334Svsakar 
12336d24e334Svsakar start_fs_creation:
12346451fdbcSvsakar retry_alternate_logic:
12356451fdbcSvsakar 	invalid_sb_cnt = 0;
12366d24e334Svsakar 	cg_too_small = 0;
12376451fdbcSvsakar 	if (use_efi_dflts) {
12386451fdbcSvsakar 		nsect = DEF_SECTORS_EFI;
12396451fdbcSvsakar 		ntrack = DEF_TRACKS_EFI;
12406451fdbcSvsakar 		cpg = DESCPG;
12416451fdbcSvsakar 		dprintf(("\nDeBuG Using EFI defaults\n"));
12426451fdbcSvsakar 	} else {
12436d24e334Svsakar 		nsect = geom_nsect;
12446d24e334Svsakar 		ntrack = geom_ntrack;
12456d24e334Svsakar 		cpg = geom_cpg;
12466d24e334Svsakar 		dprintf(("\nDeBuG Using Geometry\n"));
12476451fdbcSvsakar 		/*
12486451fdbcSvsakar 		 * 32K based on max block size of 64K, and rotational layout
12496451fdbcSvsakar 		 * test of nsect <= (256 * sectors/block).  Current block size
12506451fdbcSvsakar 		 * limit is not 64K, but it's growing soon.
12516451fdbcSvsakar 		 */
12526451fdbcSvsakar 		range_check(&nsect, "nsect", 1, 32768, DFLNSECT, nsect_flag);
12536451fdbcSvsakar 		/*
12546451fdbcSvsakar 		 * ntrack is the number of tracks per cylinder.
12556451fdbcSvsakar 		 * The ntrack value must be between 1 and the total number of
12566451fdbcSvsakar 		 * sectors in the file system.
12576451fdbcSvsakar 		 */
12586451fdbcSvsakar 		range_check(&ntrack, "ntrack", 1,
12596451fdbcSvsakar 		    fssize_db > INT_MAX ? INT_MAX : (uint32_t)fssize_db,
12606451fdbcSvsakar 		    DFLNTRAK, ntrack_flag);
12616451fdbcSvsakar 	}
12626451fdbcSvsakar 
12637c478bd9Sstevel@tonic-gate 	range_check(&apc, "apc", 0, nsect - 1, 0, apc_flag);
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	if (mtb == 'y')
12667c478bd9Sstevel@tonic-gate 		fragsize = bsize;
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	range_check(&fragsize, "fragsize", sectorsize, bsize,
12697c478bd9Sstevel@tonic-gate 	    MAX(bsize / MAXFRAG, MIN(DESFRAGSIZE, bsize)), fragsize_flag);
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	if ((bsize / MAXFRAG) > fragsize) {
12727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
12737c478bd9Sstevel@tonic-gate "fragment size %ld is too small, minimum with block size %ld is %ld\n"),
12747c478bd9Sstevel@tonic-gate 		    fragsize, bsize, bsize / MAXFRAG);
12757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12767c478bd9Sstevel@tonic-gate 		    gettext("mkfs: fragsize reset to minimum %ld\n"),
12777c478bd9Sstevel@tonic-gate 		    bsize / MAXFRAG);
12787c478bd9Sstevel@tonic-gate 		fragsize = bsize / MAXFRAG;
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	if (!POWEROF2(fragsize)) {
12827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12837c478bd9Sstevel@tonic-gate 		    gettext("fragment size must be a power of 2, not %ld\n"),
12847c478bd9Sstevel@tonic-gate 		    fragsize);
12857c478bd9Sstevel@tonic-gate 		fragsize = MAX(bsize / MAXFRAG, MIN(DESFRAGSIZE, bsize));
12867c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12877c478bd9Sstevel@tonic-gate 		    gettext("mkfs: fragsize reset to %ld\n"),
12887c478bd9Sstevel@tonic-gate 		    fragsize);
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	/* At this point, bsize must be >= fragsize, so no need to check it */
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	if (bsize < PAGESIZE) {
12947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
12957c478bd9Sstevel@tonic-gate 		    "WARNING: filesystem block size (%ld) is smaller than "
12967c478bd9Sstevel@tonic-gate 		    "memory page size (%ld).\nResulting filesystem can not be "
12977c478bd9Sstevel@tonic-gate 		    "mounted on this system.\n\n"),
12987c478bd9Sstevel@tonic-gate 		    bsize, (long)PAGESIZE);
12997c478bd9Sstevel@tonic-gate 	}
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	range_check(&rps, "rps", 1, 1000, DEFHZ, rps_flag);
13027c478bd9Sstevel@tonic-gate 	range_check(&minfree, "free", 0, 99, MINFREE, minfree_flag);
13037c478bd9Sstevel@tonic-gate 	range_check(&nrpos, "nrpos", 1, nsect, MIN(nsect, NRPOS), nrpos_flag);
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	/*
13067c478bd9Sstevel@tonic-gate 	 * nbpi is variable, but 2MB seems a reasonable upper limit,
13077c478bd9Sstevel@tonic-gate 	 * as 4MB tends to cause problems (using otherwise-default
13087c478bd9Sstevel@tonic-gate 	 * parameters).  The true limit is where we end up with one
13097c478bd9Sstevel@tonic-gate 	 * inode per cylinder group.  If this file system is being
13107c478bd9Sstevel@tonic-gate 	 * configured for multi-terabyte access, nbpi must be at least 1MB.
13117c478bd9Sstevel@tonic-gate 	 */
13127c478bd9Sstevel@tonic-gate 	if (mtb == 'y' && nbpi < MTB_NBPI) {
1313d50c8f90Svsakar 		if (nbpi_flag != RC_DEFAULT)
1314d50c8f90Svsakar 			(void) fprintf(stderr, gettext("mkfs: bad value for "
1315d50c8f90Svsakar 			    "nbpi: must be at least 1048576 for multi-terabyte,"
1316d50c8f90Svsakar 			    " nbpi reset to default 1048576\n"));
13177c478bd9Sstevel@tonic-gate 		nbpi = MTB_NBPI;
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	if (mtb == 'y')
13217c478bd9Sstevel@tonic-gate 		range_check(&nbpi, "nbpi", MTB_NBPI, 2 * MB, MTB_NBPI,
1322d50c8f90Svsakar 		    nbpi_flag);
13237c478bd9Sstevel@tonic-gate 	else
13247c478bd9Sstevel@tonic-gate 		range_check(&nbpi, "nbpi", DEV_BSIZE, 2 * MB, NBPI, nbpi_flag);
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	/*
13277c478bd9Sstevel@tonic-gate 	 * maxcpg is another variably-limited parameter.  Calculate
13287c478bd9Sstevel@tonic-gate 	 * the limit based on what we've got for its dependent
13297c478bd9Sstevel@tonic-gate 	 * variables.  Effectively, it's how much space is left in the
13307c478bd9Sstevel@tonic-gate 	 * superblock after all the other bits are accounted for.  We
13317c478bd9Sstevel@tonic-gate 	 * only fill in sblock fields so we can use MAXIpG.
13327c478bd9Sstevel@tonic-gate 	 *
13337c478bd9Sstevel@tonic-gate 	 * If the calculation of maxcpg below (for the mtb == 'n'
13347c478bd9Sstevel@tonic-gate 	 * case) is changed, update newfs as well.
13357c478bd9Sstevel@tonic-gate 	 *
13367c478bd9Sstevel@tonic-gate 	 * For old-style, non-MTB format file systems, use the old
13377c478bd9Sstevel@tonic-gate 	 * algorithm for calculating the maximum cylinder group size,
13387c478bd9Sstevel@tonic-gate 	 * even though it limits the cylinder group more than necessary.
13397c478bd9Sstevel@tonic-gate 	 * Since layout can affect performance, we don't want to change
13407c478bd9Sstevel@tonic-gate 	 * the default layout for non-MTB file systems at this time.
13417c478bd9Sstevel@tonic-gate 	 * However, for MTB file systems, use the new maxcpg calculation,
13427c478bd9Sstevel@tonic-gate 	 * which really maxes out the cylinder group size.
13437c478bd9Sstevel@tonic-gate 	 */
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	sblock.fs_bsize = bsize;
13467c478bd9Sstevel@tonic-gate 	sblock.fs_inopb = sblock.fs_bsize / sizeof (struct dinode);
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	if (mtb == 'n') {
13497c478bd9Sstevel@tonic-gate 		maxcpg = (bsize - sizeof (struct cg) -
13507c478bd9Sstevel@tonic-gate 		    howmany(MAXIpG(&sblock), NBBY)) /
13517c478bd9Sstevel@tonic-gate 		    (sizeof (long) + nrpos * sizeof (short) +
13527c478bd9Sstevel@tonic-gate 		    nsect / (MAXFRAG * NBBY));
13537c478bd9Sstevel@tonic-gate 	} else {
13547c478bd9Sstevel@tonic-gate 		maxcpg = compute_maxcpg(bsize, fragsize, nbpi, nrpos,
13557c478bd9Sstevel@tonic-gate 		    nsect * ntrack);
13567c478bd9Sstevel@tonic-gate 	}
13577c478bd9Sstevel@tonic-gate 
13586451fdbcSvsakar 	dprintf(("DeBuG cpg : %ld\n", cpg));
1359d50c8f90Svsakar 	/*
1360d50c8f90Svsakar 	 * Increase the cpg to maxcpg if either newfs was invoked
1361d50c8f90Svsakar 	 * with -T option or if mkfs wants to create a mtb file system
1362d50c8f90Svsakar 	 * and if the user has not specified the cpg.
1363d50c8f90Svsakar 	 */
1364d50c8f90Svsakar 	if (cpg == -1 || (mtb == 'y' && cpg_flag == RC_DEFAULT))
13657c478bd9Sstevel@tonic-gate 		cpg = maxcpg;
13666451fdbcSvsakar 	dprintf(("DeBuG cpg : %ld\n", cpg));
13676451fdbcSvsakar 
13687c478bd9Sstevel@tonic-gate 	/*
13697c478bd9Sstevel@tonic-gate 	 * mincpg is variable in complex ways, so we really can't
13707c478bd9Sstevel@tonic-gate 	 * do a sane lower-end limit check at this point.
13717c478bd9Sstevel@tonic-gate 	 */
13727c478bd9Sstevel@tonic-gate 	range_check(&cpg, "cgsize", 1, maxcpg, MIN(maxcpg, DESCPG), cpg_flag);
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	/*
13757c478bd9Sstevel@tonic-gate 	 * get the controller info
13767c478bd9Sstevel@tonic-gate 	 */
13777c478bd9Sstevel@tonic-gate 	islog = 0;
13787c478bd9Sstevel@tonic-gate 	islogok = 0;
13797c478bd9Sstevel@tonic-gate 	waslog = 0;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	/*
13827c478bd9Sstevel@tonic-gate 	 * Do not grow the file system, but print on stdout the maximum
13837c478bd9Sstevel@tonic-gate 	 * size in sectors to which the file system can be increased.
13847c478bd9Sstevel@tonic-gate 	 * The calculated size is limited by fssize_db.
13857c478bd9Sstevel@tonic-gate 	 * Note that we don't lock the filesystem and therefore under rare
13867c478bd9Sstevel@tonic-gate 	 * conditions (the filesystem is mounted, the free block count is
13877c478bd9Sstevel@tonic-gate 	 * almost zero, and the superuser is still changing it) the calculated
13887c478bd9Sstevel@tonic-gate 	 * size can be imprecise.
13897c478bd9Sstevel@tonic-gate 	 */
13907c478bd9Sstevel@tonic-gate 	if (Pflag) {
13917c478bd9Sstevel@tonic-gate 		(void) printf("%llu\n", probe_summaryinfo());
13927c478bd9Sstevel@tonic-gate 		exit(0);
13937c478bd9Sstevel@tonic-gate 	}
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	/*
13967c478bd9Sstevel@tonic-gate 	 * If we're growing an existing filesystem, then we're about
13977c478bd9Sstevel@tonic-gate 	 * to start doing things that can require recovery efforts if
13987c478bd9Sstevel@tonic-gate 	 * we get interrupted, so make sure we get a chance to do so.
13997c478bd9Sstevel@tonic-gate 	 */
14007c478bd9Sstevel@tonic-gate 	if (grow) {
14017c478bd9Sstevel@tonic-gate 		sigact.sa_handler = recover_from_sigint;
14027c478bd9Sstevel@tonic-gate 		sigemptyset(&sigact.sa_mask);
14037c478bd9Sstevel@tonic-gate 		sigact.sa_flags = SA_RESTART;
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 		if (sigaction(SIGINT, &sigact, (struct sigaction *)NULL) < 0) {
14067c478bd9Sstevel@tonic-gate 			perror(gettext("Could not register SIGINT handler"));
14077c478bd9Sstevel@tonic-gate 			lockexit(3);
14087c478bd9Sstevel@tonic-gate 		}
14097c478bd9Sstevel@tonic-gate 	}
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	if (!Nflag) {
14127c478bd9Sstevel@tonic-gate 		/*
14137c478bd9Sstevel@tonic-gate 		 * Check if MNTTAB is trustable
14147c478bd9Sstevel@tonic-gate 		 */
14157c478bd9Sstevel@tonic-gate 		if (statvfs64(MNTTAB, &fs) < 0) {
14167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("can't statvfs %s\n"),
1417d50c8f90Svsakar 			    MNTTAB);
14187c478bd9Sstevel@tonic-gate 			exit(32);
14197c478bd9Sstevel@tonic-gate 		}
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 		if (strcmp(MNTTYPE_MNTFS, fs.f_basetype) != 0) {
14227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
1423d50c8f90Svsakar 			    "%s file system type is not %s, can't mkfs\n"),
1424d50c8f90Svsakar 			    MNTTAB, MNTTYPE_MNTFS);
14257c478bd9Sstevel@tonic-gate 			exit(32);
14267c478bd9Sstevel@tonic-gate 		}
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 		special = getfullblkname(fsys);
14297c478bd9Sstevel@tonic-gate 		checkdev(fsys, special);
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 		/*
14327c478bd9Sstevel@tonic-gate 		 * If we found the block device name,
14337c478bd9Sstevel@tonic-gate 		 * then check the mount table.
14347c478bd9Sstevel@tonic-gate 		 * if mounted, and growing write lock the file system
14357c478bd9Sstevel@tonic-gate 		 *
14367c478bd9Sstevel@tonic-gate 		 */
14377c478bd9Sstevel@tonic-gate 		if ((special != NULL) && (*special != '\0')) {
14387c478bd9Sstevel@tonic-gate 			if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
14397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
1440d50c8f90Svsakar 				    "can't open %s\n"), MNTTAB);
14417c478bd9Sstevel@tonic-gate 				exit(32);
14427c478bd9Sstevel@tonic-gate 			}
14438509e9caSToomas Soome 			while ((getmntent(mnttab, &mntp)) == 0) {
14447c478bd9Sstevel@tonic-gate 				if (grow) {
14457c478bd9Sstevel@tonic-gate 					checkmount(&mntp, special);
14467c478bd9Sstevel@tonic-gate 					continue;
14477c478bd9Sstevel@tonic-gate 				}
14487c478bd9Sstevel@tonic-gate 				if (strcmp(special, mntp.mnt_special) == 0) {
14497c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
14507c478bd9Sstevel@tonic-gate 					    "%s is mounted, can't mkfs\n"),
14517c478bd9Sstevel@tonic-gate 					    special);
14527c478bd9Sstevel@tonic-gate 					exit(32);
14537c478bd9Sstevel@tonic-gate 				}
14547c478bd9Sstevel@tonic-gate 			}
14557c478bd9Sstevel@tonic-gate 			(void) fclose(mnttab);
14567c478bd9Sstevel@tonic-gate 		}
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 		if (directory && (ismounted == 0)) {
14597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s is not mounted\n"),
14607c478bd9Sstevel@tonic-gate 			    special);
14617c478bd9Sstevel@tonic-gate 			lockexit(32);
14627c478bd9Sstevel@tonic-gate 		}
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 		fso = (grow) ? open64(fsys, O_WRONLY) : creat64(fsys, 0666);
14657c478bd9Sstevel@tonic-gate 		if (fso < 0) {
14667c478bd9Sstevel@tonic-gate 			saverr = errno;
14677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
14687c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot create: %s\n"),
14697c478bd9Sstevel@tonic-gate 			    fsys, strerror(saverr));
14707c478bd9Sstevel@tonic-gate 			lockexit(32);
14717c478bd9Sstevel@tonic-gate 		}
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	} else {
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 		/*
14767c478bd9Sstevel@tonic-gate 		 * For the -N case, a file descriptor is needed for the llseek()
14777c478bd9Sstevel@tonic-gate 		 * in wtfs(). See the comment in wtfs() for more information.
14787c478bd9Sstevel@tonic-gate 		 *
14797c478bd9Sstevel@tonic-gate 		 * Get a file descriptor that's read-only so that this code
14807c478bd9Sstevel@tonic-gate 		 * doesn't accidentally write to the file.
14817c478bd9Sstevel@tonic-gate 		 */
14827c478bd9Sstevel@tonic-gate 		fso = open64(fsys, O_RDONLY);
14837c478bd9Sstevel@tonic-gate 		if (fso < 0) {
14847c478bd9Sstevel@tonic-gate 			saverr = errno;
14857c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: cannot open: %s\n"),
14867c478bd9Sstevel@tonic-gate 			    fsys, strerror(saverr));
14877c478bd9Sstevel@tonic-gate 			lockexit(32);
14887c478bd9Sstevel@tonic-gate 		}
14897c478bd9Sstevel@tonic-gate 	}
14907c478bd9Sstevel@tonic-gate 
149165908c77Syu, larry liu - Sun Microsystems - Beijing China 	/*
149265908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * Check the media sector size
149365908c77Syu, larry liu - Sun Microsystems - Beijing China 	 */
149465908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (ioctl(fso, DKIOCGMEDIAINFO, &dkminfo) != -1) {
149565908c77Syu, larry liu - Sun Microsystems - Beijing China 		if (dkminfo.dki_lbsize != 0 &&
149665908c77Syu, larry liu - Sun Microsystems - Beijing China 		    POWEROF2(dkminfo.dki_lbsize / DEV_BSIZE) &&
149765908c77Syu, larry liu - Sun Microsystems - Beijing China 		    dkminfo.dki_lbsize != DEV_BSIZE) {
149865908c77Syu, larry liu - Sun Microsystems - Beijing China 			fprintf(stderr,
149965908c77Syu, larry liu - Sun Microsystems - Beijing China 			    gettext("The device sector size %u is not "
150065908c77Syu, larry liu - Sun Microsystems - Beijing China 			    "supported by ufs!\n"), dkminfo.dki_lbsize);
150165908c77Syu, larry liu - Sun Microsystems - Beijing China 			(void) close(fso);
150265908c77Syu, larry liu - Sun Microsystems - Beijing China 			exit(1);
150365908c77Syu, larry liu - Sun Microsystems - Beijing China 		}
150465908c77Syu, larry liu - Sun Microsystems - Beijing China 	}
150565908c77Syu, larry liu - Sun Microsystems - Beijing China 
15067c478bd9Sstevel@tonic-gate 	/*
15077c478bd9Sstevel@tonic-gate 	 * seed random # generator (for ic_generation)
15087c478bd9Sstevel@tonic-gate 	 */
15097c478bd9Sstevel@tonic-gate #ifdef MKFS_DEBUG
15107c478bd9Sstevel@tonic-gate 	srand48(12962);	/* reproducible results */
15117c478bd9Sstevel@tonic-gate #else
15127c478bd9Sstevel@tonic-gate 	srand48((long)(time((time_t *)NULL) + getpid()));
15137c478bd9Sstevel@tonic-gate #endif
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	if (grow) {
15167c478bd9Sstevel@tonic-gate 		growinit(fsys);
15177c478bd9Sstevel@tonic-gate 		goto grow00;
15187c478bd9Sstevel@tonic-gate 	}
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	/*
15217c478bd9Sstevel@tonic-gate 	 * Validate the given file system size.
15227c478bd9Sstevel@tonic-gate 	 * Verify that its last block can actually be accessed.
15237c478bd9Sstevel@tonic-gate 	 *
15247c478bd9Sstevel@tonic-gate 	 * Note: it's ok to use sblock as a buffer because it is immediately
15257c478bd9Sstevel@tonic-gate 	 * overwritten by the rdfs() of the superblock in the next line.
15267c478bd9Sstevel@tonic-gate 	 *
15277c478bd9Sstevel@tonic-gate 	 * ToDo: Because the size checking is done in rdfs()/wtfs(), the
15287c478bd9Sstevel@tonic-gate 	 * error message for specifying an illegal size is very unfriendly.
15297c478bd9Sstevel@tonic-gate 	 * In the future, one could replace the rdfs()/wtfs() calls
15307c478bd9Sstevel@tonic-gate 	 * below with in-line calls to read() or write(). This allows better
15317c478bd9Sstevel@tonic-gate 	 * error messages to be put in place.
15327c478bd9Sstevel@tonic-gate 	 */
15337c478bd9Sstevel@tonic-gate 	rdfs(fssize_db - 1, (int)sectorsize, (char *)&sblock);
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	/*
15367c478bd9Sstevel@tonic-gate 	 * make the fs unmountable
15377c478bd9Sstevel@tonic-gate 	 */
15387c478bd9Sstevel@tonic-gate 	rdfs((diskaddr_t)(SBOFF / sectorsize), (int)sbsize, (char *)&sblock);
15397c478bd9Sstevel@tonic-gate 	sblock.fs_magic = -1;
15407c478bd9Sstevel@tonic-gate 	sblock.fs_clean = FSBAD;
15417c478bd9Sstevel@tonic-gate 	sblock.fs_state = FSOKAY - sblock.fs_time;
15427c478bd9Sstevel@tonic-gate 	wtfs((diskaddr_t)(SBOFF / sectorsize), (int)sbsize, (char *)&sblock);
15437c478bd9Sstevel@tonic-gate 	bzero(&sblock, (size_t)sbsize);
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	sblock.fs_nsect = nsect;
15467c478bd9Sstevel@tonic-gate 	sblock.fs_ntrak = ntrack;
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	/*
15497c478bd9Sstevel@tonic-gate 	 * Validate specified/determined spc
15507c478bd9Sstevel@tonic-gate 	 * and calculate minimum cylinders per group.
15517c478bd9Sstevel@tonic-gate 	 */
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	/*
15547c478bd9Sstevel@tonic-gate 	 * sectors/cyl = tracks/cyl * sectors/track
15557c478bd9Sstevel@tonic-gate 	 */
15567c478bd9Sstevel@tonic-gate 	sblock.fs_spc = sblock.fs_ntrak * sblock.fs_nsect;
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate grow00:
15597c478bd9Sstevel@tonic-gate 	if (apc_flag) {
15607c478bd9Sstevel@tonic-gate 		sblock.fs_spc -= apc;
15617c478bd9Sstevel@tonic-gate 	}
15627c478bd9Sstevel@tonic-gate 	/*
15637c478bd9Sstevel@tonic-gate 	 * Have to test for this separately from apc_flag, due to
15647c478bd9Sstevel@tonic-gate 	 * the growfs case....
15657c478bd9Sstevel@tonic-gate 	 */
15667c478bd9Sstevel@tonic-gate 	if (sblock.fs_spc != sblock.fs_ntrak * sblock.fs_nsect) {
15677c478bd9Sstevel@tonic-gate 		spc_flag = 1;
15687c478bd9Sstevel@tonic-gate 	}
15697c478bd9Sstevel@tonic-gate 	if (grow)
15707c478bd9Sstevel@tonic-gate 		goto grow10;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	sblock.fs_nrpos = nrpos;
15737c478bd9Sstevel@tonic-gate 	sblock.fs_bsize = bsize;
15747c478bd9Sstevel@tonic-gate 	sblock.fs_fsize = fragsize;
15757c478bd9Sstevel@tonic-gate 	sblock.fs_minfree = minfree;
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate grow10:
15787c478bd9Sstevel@tonic-gate 	if (nbpi < sblock.fs_fsize) {
15797c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
15807c478bd9Sstevel@tonic-gate 		"warning: wasteful data byte allocation / inode (nbpi):\n"));
15817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
15827c478bd9Sstevel@tonic-gate 		    "%ld smaller than allocatable fragment size of %d\n"),
15837c478bd9Sstevel@tonic-gate 		    nbpi, sblock.fs_fsize);
15847c478bd9Sstevel@tonic-gate 	}
15857c478bd9Sstevel@tonic-gate 	if (grow)
15867c478bd9Sstevel@tonic-gate 		goto grow20;
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	if (opt == 's')
15897c478bd9Sstevel@tonic-gate 		sblock.fs_optim = FS_OPTSPACE;
15907c478bd9Sstevel@tonic-gate 	else
15917c478bd9Sstevel@tonic-gate 		sblock.fs_optim = FS_OPTTIME;
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
15947c478bd9Sstevel@tonic-gate 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
15957c478bd9Sstevel@tonic-gate 	/*
15967c478bd9Sstevel@tonic-gate 	 * Planning now for future expansion.
15977c478bd9Sstevel@tonic-gate 	 */
15987c478bd9Sstevel@tonic-gate #if defined(_BIG_ENDIAN)
15997c478bd9Sstevel@tonic-gate 		sblock.fs_qbmask.val[0] = 0;
16007c478bd9Sstevel@tonic-gate 		sblock.fs_qbmask.val[1] = ~sblock.fs_bmask;
16017c478bd9Sstevel@tonic-gate 		sblock.fs_qfmask.val[0] = 0;
16027c478bd9Sstevel@tonic-gate 		sblock.fs_qfmask.val[1] = ~sblock.fs_fmask;
16037c478bd9Sstevel@tonic-gate #endif
16047c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
16057c478bd9Sstevel@tonic-gate 		sblock.fs_qbmask.val[0] = ~sblock.fs_bmask;
16067c478bd9Sstevel@tonic-gate 		sblock.fs_qbmask.val[1] = 0;
16077c478bd9Sstevel@tonic-gate 		sblock.fs_qfmask.val[0] = ~sblock.fs_fmask;
16087c478bd9Sstevel@tonic-gate 		sblock.fs_qfmask.val[1] = 0;
16097c478bd9Sstevel@tonic-gate #endif
16107c478bd9Sstevel@tonic-gate 	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
16117c478bd9Sstevel@tonic-gate 		sblock.fs_bshift++;
16127c478bd9Sstevel@tonic-gate 	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
16137c478bd9Sstevel@tonic-gate 		sblock.fs_fshift++;
16147c478bd9Sstevel@tonic-gate 	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
16157c478bd9Sstevel@tonic-gate 	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
16167c478bd9Sstevel@tonic-gate 		sblock.fs_fragshift++;
16177c478bd9Sstevel@tonic-gate 	if (sblock.fs_frag > MAXFRAG) {
16187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
16197c478bd9Sstevel@tonic-gate 	"fragment size %d is too small, minimum with block size %d is %d\n"),
16207c478bd9Sstevel@tonic-gate 		    sblock.fs_fsize, sblock.fs_bsize,
16217c478bd9Sstevel@tonic-gate 		    sblock.fs_bsize / MAXFRAG);
16227c478bd9Sstevel@tonic-gate 		lockexit(32);
16237c478bd9Sstevel@tonic-gate 	}
16247c478bd9Sstevel@tonic-gate 	sblock.fs_nindir = sblock.fs_bsize / sizeof (daddr32_t);
16257c478bd9Sstevel@tonic-gate 	sblock.fs_inopb = sblock.fs_bsize / sizeof (struct dinode);
16267c478bd9Sstevel@tonic-gate 	sblock.fs_nspf = sblock.fs_fsize / sectorsize;
16277c478bd9Sstevel@tonic-gate 	for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
16287c478bd9Sstevel@tonic-gate 		sblock.fs_fsbtodb++;
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	/*
16317c478bd9Sstevel@tonic-gate 	 * Compute the super-block, cylinder group, and inode blocks.
16327c478bd9Sstevel@tonic-gate 	 * Note that these "blkno" are really fragment addresses.
16337c478bd9Sstevel@tonic-gate 	 * For example, on an 8K/1K (block/fragment) system, fs_sblkno is 16,
16347c478bd9Sstevel@tonic-gate 	 * fs_cblkno is 24, and fs_iblkno is 32. This is why CGSIZE is so
16357c478bd9Sstevel@tonic-gate 	 * important: only 1 FS block is allocated for the cg struct (fragment
16367c478bd9Sstevel@tonic-gate 	 * numbers 24 through 31).
16377c478bd9Sstevel@tonic-gate 	 */
16387c478bd9Sstevel@tonic-gate 	sblock.fs_sblkno =
16397c478bd9Sstevel@tonic-gate 	    roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
16407c478bd9Sstevel@tonic-gate 	sblock.fs_cblkno = (daddr32_t)(sblock.fs_sblkno +
16417c478bd9Sstevel@tonic-gate 	    roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
16427c478bd9Sstevel@tonic-gate 	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	sblock.fs_cgoffset = roundup(
16457c478bd9Sstevel@tonic-gate 	    howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
16467c478bd9Sstevel@tonic-gate 	for (sblock.fs_cgmask = -1, i = sblock.fs_ntrak; i > 1; i >>= 1)
16477c478bd9Sstevel@tonic-gate 		sblock.fs_cgmask <<= 1;
16487c478bd9Sstevel@tonic-gate 	if (!POWEROF2(sblock.fs_ntrak))
16497c478bd9Sstevel@tonic-gate 		sblock.fs_cgmask <<= 1;
16507c478bd9Sstevel@tonic-gate 	/*
16517c478bd9Sstevel@tonic-gate 	 * Validate specified/determined spc
16527c478bd9Sstevel@tonic-gate 	 * and calculate minimum cylinders per group.
16537c478bd9Sstevel@tonic-gate 	 */
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
16567c478bd9Sstevel@tonic-gate 	    sblock.fs_cpc > 1 && (i & 1) == 0;
16577c478bd9Sstevel@tonic-gate 	    sblock.fs_cpc >>= 1, i >>= 1)
16587c478bd9Sstevel@tonic-gate 		/* void */;
16597c478bd9Sstevel@tonic-gate 	mincpc = sblock.fs_cpc;
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	/* if these calculations are changed, check dump_fscmd also */
16627c478bd9Sstevel@tonic-gate 	bpcg = (uint64_t)sblock.fs_spc * sectorsize;
16637c478bd9Sstevel@tonic-gate 	inospercg = (uint64_t)roundup(bpcg / sizeof (struct dinode),
16647c478bd9Sstevel@tonic-gate 	    INOPB(&sblock));
16657c478bd9Sstevel@tonic-gate 	if (inospercg > MAXIpG(&sblock))
16667c478bd9Sstevel@tonic-gate 		inospercg = MAXIpG(&sblock);
16677c478bd9Sstevel@tonic-gate 	used = (uint64_t)(sblock.fs_iblkno + inospercg /
16687c478bd9Sstevel@tonic-gate 	    INOPF(&sblock)) * NSPF(&sblock);
16697c478bd9Sstevel@tonic-gate 	mincpgcnt = (long)howmany((uint64_t)sblock.fs_cgoffset *
16707c478bd9Sstevel@tonic-gate 	    (~sblock.fs_cgmask) + used, sblock.fs_spc);
16717c478bd9Sstevel@tonic-gate 	mincpg = roundup(mincpgcnt, mincpc);
16727c478bd9Sstevel@tonic-gate 	/*
16737c478bd9Sstevel@tonic-gate 	 * Insure that cylinder group with mincpg has enough space
16747c478bd9Sstevel@tonic-gate 	 * for block maps
16757c478bd9Sstevel@tonic-gate 	 */
16767c478bd9Sstevel@tonic-gate 	sblock.fs_cpg = mincpg;
16777c478bd9Sstevel@tonic-gate 	sblock.fs_ipg = (int32_t)inospercg;
16787c478bd9Sstevel@tonic-gate 	mapcramped = 0;
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	/*
16817c478bd9Sstevel@tonic-gate 	 * Make sure the cg struct fits within the file system block.
16827c478bd9Sstevel@tonic-gate 	 * Use larger block sizes until it fits
16837c478bd9Sstevel@tonic-gate 	 */
16847c478bd9Sstevel@tonic-gate 	while (CGSIZE(&sblock) > sblock.fs_bsize) {
16857c478bd9Sstevel@tonic-gate 		mapcramped = 1;
16867c478bd9Sstevel@tonic-gate 		if (sblock.fs_bsize < MAXBSIZE) {
16877c478bd9Sstevel@tonic-gate 			sblock.fs_bsize <<= 1;
16887c478bd9Sstevel@tonic-gate 			if ((i & 1) == 0) {
16897c478bd9Sstevel@tonic-gate 				i >>= 1;
16907c478bd9Sstevel@tonic-gate 			} else {
16917c478bd9Sstevel@tonic-gate 				sblock.fs_cpc <<= 1;
16927c478bd9Sstevel@tonic-gate 				mincpc <<= 1;
16937c478bd9Sstevel@tonic-gate 				mincpg = roundup(mincpgcnt, mincpc);
16947c478bd9Sstevel@tonic-gate 				sblock.fs_cpg = mincpg;
16957c478bd9Sstevel@tonic-gate 			}
16967c478bd9Sstevel@tonic-gate 			sblock.fs_frag <<= 1;
16977c478bd9Sstevel@tonic-gate 			sblock.fs_fragshift += 1;
16987c478bd9Sstevel@tonic-gate 			if (sblock.fs_frag <= MAXFRAG)
16997c478bd9Sstevel@tonic-gate 				continue;
17007c478bd9Sstevel@tonic-gate 		}
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 		/*
17037c478bd9Sstevel@tonic-gate 		 * Looped far enough. The fragment is now as large as the
17047c478bd9Sstevel@tonic-gate 		 * filesystem block!
17057c478bd9Sstevel@tonic-gate 		 */
17067c478bd9Sstevel@tonic-gate 		if (sblock.fs_fsize == sblock.fs_bsize) {
17077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
17087c478bd9Sstevel@tonic-gate 		    "There is no block size that can support this disk\n"));
17097c478bd9Sstevel@tonic-gate 			lockexit(32);
17107c478bd9Sstevel@tonic-gate 		}
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 		/*
17137c478bd9Sstevel@tonic-gate 		 * Try a larger fragment. Double the fragment size.
17147c478bd9Sstevel@tonic-gate 		 */
17157c478bd9Sstevel@tonic-gate 		sblock.fs_frag >>= 1;
17167c478bd9Sstevel@tonic-gate 		sblock.fs_fragshift -= 1;
17177c478bd9Sstevel@tonic-gate 		sblock.fs_fsize <<= 1;
17187c478bd9Sstevel@tonic-gate 		sblock.fs_nspf <<= 1;
17197c478bd9Sstevel@tonic-gate 	}
17207c478bd9Sstevel@tonic-gate 	/*
17217c478bd9Sstevel@tonic-gate 	 * Insure that cylinder group with mincpg has enough space for inodes
17227c478bd9Sstevel@tonic-gate 	 */
17237c478bd9Sstevel@tonic-gate 	inodecramped = 0;
17247c478bd9Sstevel@tonic-gate 	used *= sectorsize;
17257c478bd9Sstevel@tonic-gate 	nbytes64 = (uint64_t)mincpg * bpcg - used;
17267c478bd9Sstevel@tonic-gate 	inospercg = (uint64_t)roundup((nbytes64 / nbpi), INOPB(&sblock));
17277c478bd9Sstevel@tonic-gate 	sblock.fs_ipg = (int32_t)inospercg;
17287c478bd9Sstevel@tonic-gate 	while (inospercg > MAXIpG(&sblock)) {
17297c478bd9Sstevel@tonic-gate 		inodecramped = 1;
17307c478bd9Sstevel@tonic-gate 		if (mincpc == 1 || sblock.fs_frag == 1 ||
17317c478bd9Sstevel@tonic-gate 		    sblock.fs_bsize == MINBSIZE)
17327c478bd9Sstevel@tonic-gate 			break;
17337c478bd9Sstevel@tonic-gate 		nbytes64 = (uint64_t)mincpg * bpcg - used;
17347c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
17357c478bd9Sstevel@tonic-gate 		    gettext("With a block size of %d %s %lu\n"),
17367c478bd9Sstevel@tonic-gate 		    sblock.fs_bsize, gettext("minimum bytes per inode is"),
17377c478bd9Sstevel@tonic-gate 		    (uint32_t)(nbytes64 / MAXIpG(&sblock) + 1));
17387c478bd9Sstevel@tonic-gate 		sblock.fs_bsize >>= 1;
17397c478bd9Sstevel@tonic-gate 		sblock.fs_frag >>= 1;
17407c478bd9Sstevel@tonic-gate 		sblock.fs_fragshift -= 1;
17417c478bd9Sstevel@tonic-gate 		mincpc >>= 1;
17427c478bd9Sstevel@tonic-gate 		sblock.fs_cpg = roundup(mincpgcnt, mincpc);
17437c478bd9Sstevel@tonic-gate 		if (CGSIZE(&sblock) > sblock.fs_bsize) {
17447c478bd9Sstevel@tonic-gate 			sblock.fs_bsize <<= 1;
17457c478bd9Sstevel@tonic-gate 			break;
17467c478bd9Sstevel@tonic-gate 		}
17477c478bd9Sstevel@tonic-gate 		mincpg = sblock.fs_cpg;
17487c478bd9Sstevel@tonic-gate 		nbytes64 = (uint64_t)mincpg * bpcg - used;
17497c478bd9Sstevel@tonic-gate 		inospercg = (uint64_t)roundup((nbytes64 / nbpi),
1750d50c8f90Svsakar 		    INOPB(&sblock));
17517c478bd9Sstevel@tonic-gate 		sblock.fs_ipg = (int32_t)inospercg;
17527c478bd9Sstevel@tonic-gate 	}
17537c478bd9Sstevel@tonic-gate 	if (inodecramped) {
17547c478bd9Sstevel@tonic-gate 		if (inospercg > MAXIpG(&sblock)) {
17557c478bd9Sstevel@tonic-gate 			nbytes64 = (uint64_t)mincpg * bpcg - used;
17567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
17577c478bd9Sstevel@tonic-gate 			    "Minimum bytes per inode is %d\n"),
17587c478bd9Sstevel@tonic-gate 			    (uint32_t)(nbytes64 / MAXIpG(&sblock) + 1));
17597c478bd9Sstevel@tonic-gate 		} else if (!mapcramped) {
17607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
17617c478bd9Sstevel@tonic-gate 	    "With %ld bytes per inode, minimum cylinders per group is %ld\n"),
17627c478bd9Sstevel@tonic-gate 			    nbpi, mincpg);
17637c478bd9Sstevel@tonic-gate 		}
17647c478bd9Sstevel@tonic-gate 	}
17657c478bd9Sstevel@tonic-gate 	if (mapcramped) {
17667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
17677c478bd9Sstevel@tonic-gate 		    "With %d sectors per cylinder, minimum cylinders "
17687c478bd9Sstevel@tonic-gate 		    "per group is %ld\n"),
17697c478bd9Sstevel@tonic-gate 		    sblock.fs_spc, mincpg);
17707c478bd9Sstevel@tonic-gate 	}
17717c478bd9Sstevel@tonic-gate 	if (inodecramped || mapcramped) {
17727c478bd9Sstevel@tonic-gate 		/*
17737c478bd9Sstevel@tonic-gate 		 * To make this at least somewhat comprehensible in
17747c478bd9Sstevel@tonic-gate 		 * the world of i18n, figure out what we're going to
17757c478bd9Sstevel@tonic-gate 		 * say and then say it all at one time.  The days of
17767c478bd9Sstevel@tonic-gate 		 * needing to scrimp on string space are behind us....
17777c478bd9Sstevel@tonic-gate 		 */
17787c478bd9Sstevel@tonic-gate 		if ((sblock.fs_bsize != bsize) &&
17797c478bd9Sstevel@tonic-gate 		    (sblock.fs_fsize != fragsize)) {
17807c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
17817c478bd9Sstevel@tonic-gate 	    "This requires the block size to be changed from %ld to %d\n"
17827c478bd9Sstevel@tonic-gate 	    "and the fragment size to be changed from %ld to %d\n"),
17837c478bd9Sstevel@tonic-gate 			    bsize, sblock.fs_bsize,
17847c478bd9Sstevel@tonic-gate 			    fragsize, sblock.fs_fsize);
17857c478bd9Sstevel@tonic-gate 		} else if (sblock.fs_bsize != bsize) {
17867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
17877c478bd9Sstevel@tonic-gate 	    "This requires the block size to be changed from %ld to %d\n"),
17887c478bd9Sstevel@tonic-gate 			    bsize, sblock.fs_bsize);
17897c478bd9Sstevel@tonic-gate 		} else if (sblock.fs_fsize != fragsize) {
17907c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
17917c478bd9Sstevel@tonic-gate 	    "This requires the fragment size to be changed from %ld to %d\n"),
17927c478bd9Sstevel@tonic-gate 			    fragsize, sblock.fs_fsize);
17937c478bd9Sstevel@tonic-gate 		} else {
17947c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
17957c478bd9Sstevel@tonic-gate 	    "Unable to make filesystem fit with the given constraints\n"));
17967c478bd9Sstevel@tonic-gate 		}
17977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
17987c478bd9Sstevel@tonic-gate 		    "Please re-run mkfs with corrected parameters\n"));
17997c478bd9Sstevel@tonic-gate 		lockexit(32);
18007c478bd9Sstevel@tonic-gate 	}
18017c478bd9Sstevel@tonic-gate 	/*
18027c478bd9Sstevel@tonic-gate 	 * Calculate the number of cylinders per group
18037c478bd9Sstevel@tonic-gate 	 */
18047c478bd9Sstevel@tonic-gate 	sblock.fs_cpg = cpg;
18057c478bd9Sstevel@tonic-gate 	if (sblock.fs_cpg % mincpc != 0) {
18067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
18077c478bd9Sstevel@tonic-gate 		    "Warning: cylinder groups must have a multiple "
18087c478bd9Sstevel@tonic-gate 		    "of %ld cylinders with the given\n         parameters\n"),
18097c478bd9Sstevel@tonic-gate 		    mincpc);
18107c478bd9Sstevel@tonic-gate 		sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
18117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Rounded cgsize up to %d\n"),
18127c478bd9Sstevel@tonic-gate 		    sblock.fs_cpg);
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 	/*
18157c478bd9Sstevel@tonic-gate 	 * Must insure there is enough space for inodes
18167c478bd9Sstevel@tonic-gate 	 */
18177c478bd9Sstevel@tonic-gate 	/* if these calculations are changed, check dump_fscmd also */
18187c478bd9Sstevel@tonic-gate 	nbytes64 = (uint64_t)sblock.fs_cpg * bpcg - used;
18197c478bd9Sstevel@tonic-gate 	sblock.fs_ipg = roundup((uint32_t)(nbytes64 / nbpi), INOPB(&sblock));
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	/*
18227c478bd9Sstevel@tonic-gate 	 * Slim down cylinders per group, until the inodes can fit.
18237c478bd9Sstevel@tonic-gate 	 */
18247c478bd9Sstevel@tonic-gate 	while (sblock.fs_ipg > MAXIpG(&sblock)) {
18257c478bd9Sstevel@tonic-gate 		inodecramped = 1;
18267c478bd9Sstevel@tonic-gate 		sblock.fs_cpg -= mincpc;
18277c478bd9Sstevel@tonic-gate 		nbytes64 = (uint64_t)sblock.fs_cpg * bpcg - used;
18287c478bd9Sstevel@tonic-gate 		sblock.fs_ipg = roundup((uint32_t)(nbytes64 / nbpi),
1829d50c8f90Svsakar 		    INOPB(&sblock));
18307c478bd9Sstevel@tonic-gate 	}
18317c478bd9Sstevel@tonic-gate 	/*
18327c478bd9Sstevel@tonic-gate 	 * Must insure there is enough space to hold block map.
18337c478bd9Sstevel@tonic-gate 	 * Cut down on cylinders per group, until the cg struct fits in a
18347c478bd9Sstevel@tonic-gate 	 * filesystem block.
18357c478bd9Sstevel@tonic-gate 	 */
18367c478bd9Sstevel@tonic-gate 	while (CGSIZE(&sblock) > sblock.fs_bsize) {
18377c478bd9Sstevel@tonic-gate 		mapcramped = 1;
18387c478bd9Sstevel@tonic-gate 		sblock.fs_cpg -= mincpc;
18397c478bd9Sstevel@tonic-gate 		nbytes64 = (uint64_t)sblock.fs_cpg * bpcg - used;
18407c478bd9Sstevel@tonic-gate 		sblock.fs_ipg = roundup((uint32_t)(nbytes64 / nbpi),
1841d50c8f90Svsakar 		    INOPB(&sblock));
18427c478bd9Sstevel@tonic-gate 	}
18437c478bd9Sstevel@tonic-gate 	sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
18447c478bd9Sstevel@tonic-gate 	if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
18457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18467c478bd9Sstevel@tonic-gate 		gettext("newfs: panic (fs_cpg * fs_spc) %% NSPF != 0\n"));
18477c478bd9Sstevel@tonic-gate 		lockexit(32);
18487c478bd9Sstevel@tonic-gate 	}
18497c478bd9Sstevel@tonic-gate 	if (sblock.fs_cpg < mincpg) {
18507c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
18517c478bd9Sstevel@tonic-gate "With the given parameters, cgsize must be at least %ld; please re-run mkfs\n"),
1852d50c8f90Svsakar 		    mincpg);
18537c478bd9Sstevel@tonic-gate 		lockexit(32);
18547c478bd9Sstevel@tonic-gate 	}
18557c478bd9Sstevel@tonic-gate 	sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
18567c478bd9Sstevel@tonic-gate grow20:
18577c478bd9Sstevel@tonic-gate 	/*
18587c478bd9Sstevel@tonic-gate 	 * Now have size for file system and nsect and ntrak.
18597c478bd9Sstevel@tonic-gate 	 * Determine number of cylinders and blocks in the file system.
18607c478bd9Sstevel@tonic-gate 	 */
18617c478bd9Sstevel@tonic-gate 	fssize_frag = (int64_t)dbtofsb(&sblock, fssize_db);
18627c478bd9Sstevel@tonic-gate 	if (fssize_frag > INT_MAX) {
18637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
18647c478bd9Sstevel@tonic-gate "There are too many fragments in the system, increase fragment size\n"),
18657c478bd9Sstevel@tonic-gate 		    mincpg);
18667c478bd9Sstevel@tonic-gate 		lockexit(32);
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 	sblock.fs_size = (int32_t)fssize_frag;
18697c478bd9Sstevel@tonic-gate 	sblock.fs_ncyl = (int32_t)(fssize_frag * NSPF(&sblock) / sblock.fs_spc);
18707c478bd9Sstevel@tonic-gate 	if (fssize_frag * NSPF(&sblock) >
18717c478bd9Sstevel@tonic-gate 	    (uint64_t)sblock.fs_ncyl * sblock.fs_spc) {
18727c478bd9Sstevel@tonic-gate 		sblock.fs_ncyl++;
18737c478bd9Sstevel@tonic-gate 		warn = 1;
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 	if (sblock.fs_ncyl < 1) {
18767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1877d50c8f90Svsakar 		    "file systems must have at least one cylinder\n"));
18787c478bd9Sstevel@tonic-gate 		lockexit(32);
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 	if (grow)
18817c478bd9Sstevel@tonic-gate 		goto grow30;
18827c478bd9Sstevel@tonic-gate 	/*
18837c478bd9Sstevel@tonic-gate 	 * Determine feasability/values of rotational layout tables.
18847c478bd9Sstevel@tonic-gate 	 *
18857c478bd9Sstevel@tonic-gate 	 * The size of the rotational layout tables is limited by the size
18867c478bd9Sstevel@tonic-gate 	 * of the file system block, fs_bsize.  The amount of space
18877c478bd9Sstevel@tonic-gate 	 * available for tables is calculated as (fs_bsize - sizeof (struct
18887c478bd9Sstevel@tonic-gate 	 * fs)).  The size of these tables is inversely proportional to the
18897c478bd9Sstevel@tonic-gate 	 * block size of the file system. The size increases if sectors per
18907c478bd9Sstevel@tonic-gate 	 * track are not powers of two, because more cylinders must be
18917c478bd9Sstevel@tonic-gate 	 * described by the tables before the rotational pattern repeats
18927c478bd9Sstevel@tonic-gate 	 * (fs_cpc).
18937c478bd9Sstevel@tonic-gate 	 */
18947c478bd9Sstevel@tonic-gate 	sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
18957c478bd9Sstevel@tonic-gate 	sblock.fs_sbsize = fragroundup(&sblock, sizeof (struct fs));
18967c478bd9Sstevel@tonic-gate 	sblock.fs_npsect = sblock.fs_nsect;
18977c478bd9Sstevel@tonic-gate 	if (sblock.fs_ntrak == 1) {
18987c478bd9Sstevel@tonic-gate 		sblock.fs_cpc = 0;
18997c478bd9Sstevel@tonic-gate 		goto next;
19007c478bd9Sstevel@tonic-gate 	}
19017c478bd9Sstevel@tonic-gate 	postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof (short);
19027c478bd9Sstevel@tonic-gate 	rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
19037c478bd9Sstevel@tonic-gate 	totalsbsize = sizeof (struct fs) + rotblsize;
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	/* do static allocation if nrpos == 8 and fs_cpc == 16  */
19067c478bd9Sstevel@tonic-gate 	if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
19077c478bd9Sstevel@tonic-gate 		/* use old static table space */
19087c478bd9Sstevel@tonic-gate 		sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
19097c478bd9Sstevel@tonic-gate 		    (char *)(&sblock.fs_link);
19107c478bd9Sstevel@tonic-gate 		sblock.fs_rotbloff = &sblock.fs_space[0] -
19117c478bd9Sstevel@tonic-gate 		    (uchar_t *)(&sblock.fs_link);
19127c478bd9Sstevel@tonic-gate 	} else {
19137c478bd9Sstevel@tonic-gate 		/* use 4.3 dynamic table space */
19147c478bd9Sstevel@tonic-gate 		sblock.fs_postbloff = &sblock.fs_space[0] -
19157c478bd9Sstevel@tonic-gate 		    (uchar_t *)(&sblock.fs_link);
19167c478bd9Sstevel@tonic-gate 		sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
19177c478bd9Sstevel@tonic-gate 		totalsbsize += postblsize;
19187c478bd9Sstevel@tonic-gate 	}
19197c478bd9Sstevel@tonic-gate 	if (totalsbsize > sblock.fs_bsize ||
19207c478bd9Sstevel@tonic-gate 	    sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
19217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
19227c478bd9Sstevel@tonic-gate 		    "Warning: insufficient space in super block for\n"
19237c478bd9Sstevel@tonic-gate 		    "rotational layout tables with nsect %d, ntrack %d, "
19247c478bd9Sstevel@tonic-gate 		    "and nrpos %d.\nOmitting tables - file system "
19257c478bd9Sstevel@tonic-gate 		    "performance may be impaired.\n"),
19267c478bd9Sstevel@tonic-gate 		    sblock.fs_nsect, sblock.fs_ntrak, sblock.fs_nrpos);
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 		/*
19297c478bd9Sstevel@tonic-gate 		 * Setting fs_cpc to 0 tells alloccgblk() in ufs_alloc.c to
19307c478bd9Sstevel@tonic-gate 		 * ignore the positional layout table and rotational
19317c478bd9Sstevel@tonic-gate 		 * position table.
19327c478bd9Sstevel@tonic-gate 		 */
19337c478bd9Sstevel@tonic-gate 		sblock.fs_cpc = 0;
19347c478bd9Sstevel@tonic-gate 		goto next;
19357c478bd9Sstevel@tonic-gate 	}
19367c478bd9Sstevel@tonic-gate 	sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	/*
19407c478bd9Sstevel@tonic-gate 	 * calculate the available blocks for each rotational position
19417c478bd9Sstevel@tonic-gate 	 */
19427c478bd9Sstevel@tonic-gate 	for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
19437c478bd9Sstevel@tonic-gate 		for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
19447c478bd9Sstevel@tonic-gate 			fs_postbl(&sblock, cylno)[rpos] = -1;
19457c478bd9Sstevel@tonic-gate 	for (i = (rotblsize - 1) * sblock.fs_frag;
19467c478bd9Sstevel@tonic-gate 	    i >= 0; i -= sblock.fs_frag) {
19477c478bd9Sstevel@tonic-gate 		cylno = cbtocylno(&sblock, i);
19487c478bd9Sstevel@tonic-gate 		rpos = cbtorpos(&sblock, i);
19497c478bd9Sstevel@tonic-gate 		blk = fragstoblks(&sblock, i);
19507c478bd9Sstevel@tonic-gate 		if (fs_postbl(&sblock, cylno)[rpos] == -1)
19517c478bd9Sstevel@tonic-gate 			fs_rotbl(&sblock)[blk] = 0;
19527c478bd9Sstevel@tonic-gate 		else
19537c478bd9Sstevel@tonic-gate 			fs_rotbl(&sblock)[blk] =
19547c478bd9Sstevel@tonic-gate 			    fs_postbl(&sblock, cylno)[rpos] - blk;
19557c478bd9Sstevel@tonic-gate 		fs_postbl(&sblock, cylno)[rpos] = blk;
19567c478bd9Sstevel@tonic-gate 	}
19577c478bd9Sstevel@tonic-gate next:
19587c478bd9Sstevel@tonic-gate grow30:
19597c478bd9Sstevel@tonic-gate 	/*
19607c478bd9Sstevel@tonic-gate 	 * Compute/validate number of cylinder groups.
19617c478bd9Sstevel@tonic-gate 	 * Note that if an excessively large filesystem is specified
19627c478bd9Sstevel@tonic-gate 	 * (e.g., more than 16384 cylinders for an 8K filesystem block), it
19637c478bd9Sstevel@tonic-gate 	 * does not get detected until checksummarysize()
19647c478bd9Sstevel@tonic-gate 	 */
19657c478bd9Sstevel@tonic-gate 	sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
19667c478bd9Sstevel@tonic-gate 	if (sblock.fs_ncyl % sblock.fs_cpg)
19677c478bd9Sstevel@tonic-gate 		sblock.fs_ncg++;
19687c478bd9Sstevel@tonic-gate 	sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
19697c478bd9Sstevel@tonic-gate 	i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
19707c478bd9Sstevel@tonic-gate 	ibpcl = cgdmin(&sblock, i) - cgbase(&sblock, i);
19717c478bd9Sstevel@tonic-gate 	if (ibpcl >= sblock.fs_fpg) {
19727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
19737c478bd9Sstevel@tonic-gate 		    "inode blocks/cyl group (%d) >= data blocks (%d)\n"),
19747c478bd9Sstevel@tonic-gate 		    cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
19757c478bd9Sstevel@tonic-gate 		    sblock.fs_fpg / sblock.fs_frag);
19767c478bd9Sstevel@tonic-gate 		if ((ibpcl < 0) || (sblock.fs_fpg < 0)) {
19777c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
19787c478bd9Sstevel@tonic-gate 	    "number of cylinders per cylinder group (%d) must be decreased.\n"),
19797c478bd9Sstevel@tonic-gate 			    sblock.fs_cpg);
19807c478bd9Sstevel@tonic-gate 		} else {
19817c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
19827c478bd9Sstevel@tonic-gate 	    "number of cylinders per cylinder group (%d) must be increased.\n"),
19837c478bd9Sstevel@tonic-gate 			    sblock.fs_cpg);
19847c478bd9Sstevel@tonic-gate 		}
19857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
19867c478bd9Sstevel@tonic-gate "Note that cgsize may have been adjusted to allow struct cg to fit.\n"));
19877c478bd9Sstevel@tonic-gate 		lockexit(32);
19887c478bd9Sstevel@tonic-gate 	}
19897c478bd9Sstevel@tonic-gate 	j = sblock.fs_ncg - 1;
19907c478bd9Sstevel@tonic-gate 	if ((i = fssize_frag - j * sblock.fs_fpg) < sblock.fs_fpg &&
19917c478bd9Sstevel@tonic-gate 	    cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
19927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
19937c478bd9Sstevel@tonic-gate 		    "Warning: inode blocks/cyl group (%d) >= data "
19947c478bd9Sstevel@tonic-gate 		    "blocks (%ld) in last\n    cylinder group. This "
19957c478bd9Sstevel@tonic-gate 		    "implies %ld sector(s) cannot be allocated.\n"),
19967c478bd9Sstevel@tonic-gate 		    (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
19977c478bd9Sstevel@tonic-gate 		    i / sblock.fs_frag, i * NSPF(&sblock));
19986d24e334Svsakar 		/*
19996d24e334Svsakar 		 * If there is only one cylinder group and that is not even
20006d24e334Svsakar 		 * big enough to hold the inodes, exit.
20016d24e334Svsakar 		 */
20026d24e334Svsakar 		if (sblock.fs_ncg == 1)
20036d24e334Svsakar 			cg_too_small = 1;
20047c478bd9Sstevel@tonic-gate 		sblock.fs_ncg--;
200538b58fe3SHarold N Shaw- Sun Microsystem 		sblock.fs_ncyl = sblock.fs_ncg * sblock.fs_cpg;
20067c478bd9Sstevel@tonic-gate 		sblock.fs_size = fssize_frag =
20077c478bd9Sstevel@tonic-gate 		    (int64_t)sblock.fs_ncyl * (int64_t)sblock.fs_spc /
20087c478bd9Sstevel@tonic-gate 		    (int64_t)NSPF(&sblock);
20097c478bd9Sstevel@tonic-gate 		warn = 0;
20107c478bd9Sstevel@tonic-gate 	}
20117c478bd9Sstevel@tonic-gate 	if (warn && !spc_flag) {
20127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
20137c478bd9Sstevel@tonic-gate 		    "Warning: %d sector(s) in last cylinder unallocated\n"),
20147c478bd9Sstevel@tonic-gate 		    sblock.fs_spc - (uint32_t)(fssize_frag * NSPF(&sblock) -
20157c478bd9Sstevel@tonic-gate 		    (uint64_t)(sblock.fs_ncyl - 1) * sblock.fs_spc));
20167c478bd9Sstevel@tonic-gate 	}
20177c478bd9Sstevel@tonic-gate 	/*
20187c478bd9Sstevel@tonic-gate 	 * fill in remaining fields of the super block
20197c478bd9Sstevel@tonic-gate 	 */
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	/*
20227c478bd9Sstevel@tonic-gate 	 * The csum records are stored in cylinder group 0, starting at
20237c478bd9Sstevel@tonic-gate 	 * cgdmin, the first data block.
20247c478bd9Sstevel@tonic-gate 	 */
20257c478bd9Sstevel@tonic-gate 	sblock.fs_csaddr = cgdmin(&sblock, 0);
20267c478bd9Sstevel@tonic-gate 	sblock.fs_cssize =
20277c478bd9Sstevel@tonic-gate 	    fragroundup(&sblock, sblock.fs_ncg * sizeof (struct csum));
20287c478bd9Sstevel@tonic-gate 	i = sblock.fs_bsize / sizeof (struct csum);
20297c478bd9Sstevel@tonic-gate 	sblock.fs_csmask = ~(i - 1);
20307c478bd9Sstevel@tonic-gate 	for (sblock.fs_csshift = 0; i > 1; i >>= 1)
20317c478bd9Sstevel@tonic-gate 		sblock.fs_csshift++;
20327c478bd9Sstevel@tonic-gate 	fscs = (struct csum *)calloc(1, sblock.fs_cssize);
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	checksummarysize();
20357c478bd9Sstevel@tonic-gate 	if (mtb == 'y') {
20367c478bd9Sstevel@tonic-gate 		sblock.fs_magic = MTB_UFS_MAGIC;
20377c478bd9Sstevel@tonic-gate 		sblock.fs_version = MTB_UFS_VERSION_1;
20387c478bd9Sstevel@tonic-gate 	} else {
20397c478bd9Sstevel@tonic-gate 		sblock.fs_magic = FS_MAGIC;
20406451fdbcSvsakar 		if (use_efi_dflts)
20416451fdbcSvsakar 			sblock.fs_version = UFS_EFISTYLE4NONEFI_VERSION_2;
20426451fdbcSvsakar 		else
20436451fdbcSvsakar 			sblock.fs_version = UFS_VERSION_MIN;
20447c478bd9Sstevel@tonic-gate 	}
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	if (grow) {
20477c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)grow_fscs, (caddr_t)fscs, (int)grow_fs_cssize);
20487c478bd9Sstevel@tonic-gate 		extendsummaryinfo();
20497c478bd9Sstevel@tonic-gate 		goto grow40;
20507c478bd9Sstevel@tonic-gate 	}
20517c478bd9Sstevel@tonic-gate 	sblock.fs_rotdelay = rotdelay;
20527c478bd9Sstevel@tonic-gate 	sblock.fs_maxcontig = maxcontig;
20537c478bd9Sstevel@tonic-gate 	sblock.fs_maxbpg = MAXBLKPG(sblock.fs_bsize);
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	sblock.fs_rps = rps;
20567c478bd9Sstevel@tonic-gate 	sblock.fs_cgrotor = 0;
20577c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_ndir = 0;
20587c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_nbfree = 0;
20597c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_nifree = 0;
20607c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_nffree = 0;
20617c478bd9Sstevel@tonic-gate 	sblock.fs_fmod = 0;
20627c478bd9Sstevel@tonic-gate 	sblock.fs_ronly = 0;
20637c478bd9Sstevel@tonic-gate 	sblock.fs_time = mkfstime;
20647c478bd9Sstevel@tonic-gate 	sblock.fs_state = FSOKAY - sblock.fs_time;
20657c478bd9Sstevel@tonic-gate 	sblock.fs_clean = FSCLEAN;
20667c478bd9Sstevel@tonic-gate grow40:
20677c478bd9Sstevel@tonic-gate 
2068355d6bb5Sswilcox 	/*
2069355d6bb5Sswilcox 	 * If all that's needed is a dump of the superblock we
2070355d6bb5Sswilcox 	 * would use by default, we've got it now.  So, splat it
2071355d6bb5Sswilcox 	 * out and leave.
2072355d6bb5Sswilcox 	 */
2073355d6bb5Sswilcox 	if (rflag) {
2074355d6bb5Sswilcox 		dump_sblock();
2075355d6bb5Sswilcox 		lockexit(0);
2076355d6bb5Sswilcox 	}
20777c478bd9Sstevel@tonic-gate 	/*
20787c478bd9Sstevel@tonic-gate 	 * Dump out summary information about file system.
20797c478bd9Sstevel@tonic-gate 	 */
20807c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
20817c478bd9Sstevel@tonic-gate 	    "%s:\t%lld sectors in %d cylinders of %d tracks, %d sectors\n"),
20827c478bd9Sstevel@tonic-gate 	    fsys, (uint64_t)sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
20837c478bd9Sstevel@tonic-gate 	    sblock.fs_ntrak, sblock.fs_nsect);
20847c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
20857c478bd9Sstevel@tonic-gate 	    "\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n"),
20867c478bd9Sstevel@tonic-gate 	    (float)sblock.fs_size * sblock.fs_fsize / MB, sblock.fs_ncg,
20877c478bd9Sstevel@tonic-gate 	    sblock.fs_cpg, (float)sblock.fs_fpg * sblock.fs_fsize / MB,
20887c478bd9Sstevel@tonic-gate 	    sblock.fs_ipg);
20896451fdbcSvsakar 
20906451fdbcSvsakar 	tmpbuf = calloc(sblock.fs_ncg / 50 + 500, 1);
20916451fdbcSvsakar 	if (tmpbuf == NULL) {
20926451fdbcSvsakar 		perror("calloc");
20936451fdbcSvsakar 		lockexit(32);
20946451fdbcSvsakar 	}
20956d24e334Svsakar 	if (cg_too_small) {
20966d24e334Svsakar 		(void) fprintf(stderr, gettext("File system creation failed. "
2097d50c8f90Svsakar 		    "There is only one cylinder group and\nthat is "
2098d50c8f90Svsakar 		    "not even big enough to hold the inodes.\n"));
20996d24e334Svsakar 		lockexit(32);
21006d24e334Svsakar 	}
21017c478bd9Sstevel@tonic-gate 	/*
21027c478bd9Sstevel@tonic-gate 	 * Now build the cylinders group blocks and
21037c478bd9Sstevel@tonic-gate 	 * then print out indices of cylinder groups.
21047c478bd9Sstevel@tonic-gate 	 */
21056451fdbcSvsakar 	tprintf(gettext(
21067c478bd9Sstevel@tonic-gate 	    "super-block backups (for fsck -F ufs -o b=#) at:\n"));
21077c478bd9Sstevel@tonic-gate 	for (width = cylno = 0; cylno < sblock.fs_ncg && cylno < 10; cylno++) {
21087c478bd9Sstevel@tonic-gate 		if ((grow == 0) || (cylno >= grow_fs_ncg))
21097c478bd9Sstevel@tonic-gate 			initcg(cylno);
21107c478bd9Sstevel@tonic-gate 		num = fsbtodb(&sblock, (uint64_t)cgsblock(&sblock, cylno));
21116451fdbcSvsakar 		/*
21126451fdbcSvsakar 		 * If Nflag and if the disk is larger than the CHSLIMIT,
21136451fdbcSvsakar 		 * then sanity test the superblocks before reporting. If there
21146d24e334Svsakar 		 * are too many superblocks which look insane, we have
21156d24e334Svsakar 		 * to retry with alternate logic. If both methods have
21166d24e334Svsakar 		 * failed, then our efforts to arrive at alternate
21176451fdbcSvsakar 		 * superblocks failed, so complain and exit.
21186451fdbcSvsakar 		 */
21196451fdbcSvsakar 		if (Nflag && retry) {
2120d50c8f90Svsakar 			skip_this_sb = 0;
2121d50c8f90Svsakar 			rdfs((diskaddr_t)num, sbsize, (char *)&altsblock);
2122d50c8f90Svsakar 			ret = checksblock(altsblock, 1);
2123d50c8f90Svsakar 			if (ret) {
21246451fdbcSvsakar 				skip_this_sb = 1;
21256451fdbcSvsakar 				invalid_sb_cnt++;
2126d50c8f90Svsakar 				dprintf(("DeBuG checksblock() failed - error :"
2127d50c8f90Svsakar 				    " %d for sb : %llu invalid_sb_cnt : %d\n",
2128d50c8f90Svsakar 				    ret, num, invalid_sb_cnt));
2129d50c8f90Svsakar 			} else {
2130d50c8f90Svsakar 				/*
2131d50c8f90Svsakar 				 * Though the superblock looks sane, verify if
2132d50c8f90Svsakar 				 * the fs_version in the superblock and the
2133d50c8f90Svsakar 				 * logic that we are using to arrive at the
2134d50c8f90Svsakar 				 * superblocks match.
2135d50c8f90Svsakar 				 */
2136d50c8f90Svsakar 				if (use_efi_dflts && altsblock.fs_version
2137d50c8f90Svsakar 				    != UFS_EFISTYLE4NONEFI_VERSION_2) {
2138d50c8f90Svsakar 					skip_this_sb = 1;
2139d50c8f90Svsakar 					invalid_sb_cnt++;
2140d50c8f90Svsakar 				}
21416451fdbcSvsakar 			}
2142d50c8f90Svsakar 			if (invalid_sb_cnt >= INVALIDSBLIMIT) {
2143d50c8f90Svsakar 				if (retry > 1) {
2144d50c8f90Svsakar 					(void) fprintf(stderr, gettext(
2145d50c8f90Svsakar 					    "Error determining alternate "
2146d50c8f90Svsakar 					    "superblock locations\n"));
2147d50c8f90Svsakar 					free(tmpbuf);
2148d50c8f90Svsakar 					lockexit(32);
2149d50c8f90Svsakar 				}
2150d50c8f90Svsakar 				retry++;
2151d50c8f90Svsakar 				use_efi_dflts = !use_efi_dflts;
2152d50c8f90Svsakar 				free(tmpbuf);
2153d50c8f90Svsakar 				goto retry_alternate_logic;
21546451fdbcSvsakar 			}
2155d50c8f90Svsakar 			if (skip_this_sb)
2156d50c8f90Svsakar 				continue;
21576451fdbcSvsakar 		}
21587c478bd9Sstevel@tonic-gate 		(void) sprintf(pbuf, " %llu,", num);
21597c478bd9Sstevel@tonic-gate 		plen = strlen(pbuf);
21607c478bd9Sstevel@tonic-gate 		if ((width + plen) > (WIDTH - 1)) {
21617c478bd9Sstevel@tonic-gate 			width = plen;
21626451fdbcSvsakar 			tprintf("\n");
21637c478bd9Sstevel@tonic-gate 		} else {
21647c478bd9Sstevel@tonic-gate 			width += plen;
21657c478bd9Sstevel@tonic-gate 		}
21666451fdbcSvsakar 		if (Nflag && retry)
21676d24e334Svsakar 			(void) strncat(tmpbuf, pbuf, strlen(pbuf));
21686451fdbcSvsakar 		else
21696451fdbcSvsakar 			(void) fprintf(stderr, "%s", pbuf);
21707c478bd9Sstevel@tonic-gate 	}
21716451fdbcSvsakar 	tprintf("\n");
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate 	remaining_cg = sblock.fs_ncg - cylno;
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	/*
21767c478bd9Sstevel@tonic-gate 	 * If there are more than 300 cylinder groups still to be
21777c478bd9Sstevel@tonic-gate 	 * initialized, print a "." for every 50 cylinder groups.
21787c478bd9Sstevel@tonic-gate 	 */
21797c478bd9Sstevel@tonic-gate 	if (remaining_cg > 300) {
21806451fdbcSvsakar 		tprintf(gettext("Initializing cylinder groups:\n"));
21817c478bd9Sstevel@tonic-gate 		do_dot = 1;
21827c478bd9Sstevel@tonic-gate 	}
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	/*
21857c478bd9Sstevel@tonic-gate 	 * Now initialize all cylinder groups between the first ten
21867c478bd9Sstevel@tonic-gate 	 * and the last ten.
21877c478bd9Sstevel@tonic-gate 	 *
21887c478bd9Sstevel@tonic-gate 	 * If the number of cylinder groups was less than 10, all of the
21897c478bd9Sstevel@tonic-gate 	 * cylinder group offsets would have printed in the last loop
21907c478bd9Sstevel@tonic-gate 	 * and cylno will already be equal to sblock.fs_ncg and so this
21917c478bd9Sstevel@tonic-gate 	 * loop will not be entered.  If there are less than 20 cylinder
21927c478bd9Sstevel@tonic-gate 	 * groups, cylno is already less than fs_ncg - 10, so this loop
21937c478bd9Sstevel@tonic-gate 	 * won't be entered in that case either.
21947c478bd9Sstevel@tonic-gate 	 */
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	i = 0;
21977c478bd9Sstevel@tonic-gate 	for (; cylno < sblock.fs_ncg - 10; cylno++) {
21987c478bd9Sstevel@tonic-gate 		if ((grow == 0) || (cylno >= grow_fs_ncg))
21997c478bd9Sstevel@tonic-gate 			initcg(cylno);
22007c478bd9Sstevel@tonic-gate 		if (do_dot && cylno % 50 == 0) {
22016451fdbcSvsakar 			tprintf(".");
22027c478bd9Sstevel@tonic-gate 			i++;
22037c478bd9Sstevel@tonic-gate 			if (i == WIDTH - 1) {
22046451fdbcSvsakar 				tprintf("\n");
22057c478bd9Sstevel@tonic-gate 				i = 0;
22067c478bd9Sstevel@tonic-gate 			}
22077c478bd9Sstevel@tonic-gate 		}
22087c478bd9Sstevel@tonic-gate 	}
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 	/*
22117c478bd9Sstevel@tonic-gate 	 * Now print the cylinder group offsets for the last 10
22127c478bd9Sstevel@tonic-gate 	 * cylinder groups, if any are left.
22137c478bd9Sstevel@tonic-gate 	 */
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	if (do_dot) {
22166451fdbcSvsakar 		tprintf(gettext(
22177c478bd9Sstevel@tonic-gate 	    "\nsuper-block backups for last 10 cylinder groups at:\n"));
22187c478bd9Sstevel@tonic-gate 	}
22197c478bd9Sstevel@tonic-gate 	for (width = 0; cylno < sblock.fs_ncg; cylno++) {
22207c478bd9Sstevel@tonic-gate 		if ((grow == 0) || (cylno >= grow_fs_ncg))
22217c478bd9Sstevel@tonic-gate 			initcg(cylno);
22227c478bd9Sstevel@tonic-gate 		num = fsbtodb(&sblock, (uint64_t)cgsblock(&sblock, cylno));
22236451fdbcSvsakar 		if (Nflag && retry) {
2224d50c8f90Svsakar 			skip_this_sb = 0;
2225d50c8f90Svsakar 			rdfs((diskaddr_t)num, sbsize, (char *)&altsblock);
2226d50c8f90Svsakar 			ret = checksblock(altsblock, 1);
2227d50c8f90Svsakar 			if (ret) {
22286451fdbcSvsakar 				skip_this_sb = 1;
22296451fdbcSvsakar 				invalid_sb_cnt++;
2230d50c8f90Svsakar 				dprintf(("DeBuG checksblock() failed - error :"
2231d50c8f90Svsakar 				    " %d for sb : %llu invalid_sb_cnt : %d\n",
2232d50c8f90Svsakar 				    ret, num, invalid_sb_cnt));
2233d50c8f90Svsakar 			} else {
2234d50c8f90Svsakar 				/*
2235d50c8f90Svsakar 				 * Though the superblock looks sane, verify if
2236d50c8f90Svsakar 				 * the fs_version in the superblock and the
2237d50c8f90Svsakar 				 * logic that we are using to arrive at the
2238d50c8f90Svsakar 				 * superblocks match.
2239d50c8f90Svsakar 				 */
2240d50c8f90Svsakar 				if (use_efi_dflts && altsblock.fs_version
2241d50c8f90Svsakar 				    != UFS_EFISTYLE4NONEFI_VERSION_2) {
2242d50c8f90Svsakar 					skip_this_sb = 1;
2243d50c8f90Svsakar 					invalid_sb_cnt++;
2244d50c8f90Svsakar 				}
22456451fdbcSvsakar 			}
2246d50c8f90Svsakar 			if (invalid_sb_cnt >= INVALIDSBLIMIT) {
2247d50c8f90Svsakar 				if (retry > 1) {
2248d50c8f90Svsakar 					(void) fprintf(stderr, gettext(
2249d50c8f90Svsakar 					    "Error determining alternate "
2250d50c8f90Svsakar 					    "superblock locations\n"));
2251d50c8f90Svsakar 					free(tmpbuf);
2252d50c8f90Svsakar 					lockexit(32);
2253d50c8f90Svsakar 				}
2254d50c8f90Svsakar 				retry++;
2255d50c8f90Svsakar 				use_efi_dflts = !use_efi_dflts;
2256d50c8f90Svsakar 				free(tmpbuf);
2257d50c8f90Svsakar 				goto retry_alternate_logic;
22586451fdbcSvsakar 			}
2259d50c8f90Svsakar 			if (skip_this_sb)
2260d50c8f90Svsakar 				continue;
22616451fdbcSvsakar 		}
22626451fdbcSvsakar 		/* Don't print ',' for the last superblock */
22636451fdbcSvsakar 		if (cylno == sblock.fs_ncg-1)
22646451fdbcSvsakar 			(void) sprintf(pbuf, " %llu", num);
22656451fdbcSvsakar 		else
22666451fdbcSvsakar 			(void) sprintf(pbuf, " %llu,", num);
22677c478bd9Sstevel@tonic-gate 		plen = strlen(pbuf);
22687c478bd9Sstevel@tonic-gate 		if ((width + plen) > (WIDTH - 1)) {
22697c478bd9Sstevel@tonic-gate 			width = plen;
22706451fdbcSvsakar 			tprintf("\n");
22717c478bd9Sstevel@tonic-gate 		} else {
22727c478bd9Sstevel@tonic-gate 			width += plen;
22737c478bd9Sstevel@tonic-gate 		}
22746451fdbcSvsakar 		if (Nflag && retry)
22756d24e334Svsakar 			(void) strncat(tmpbuf, pbuf, strlen(pbuf));
22766451fdbcSvsakar 		else
22776451fdbcSvsakar 			(void) fprintf(stderr, "%s", pbuf);
22787c478bd9Sstevel@tonic-gate 	}
22796451fdbcSvsakar 	tprintf("\n");
22806451fdbcSvsakar 	if (Nflag) {
22816451fdbcSvsakar 		if (retry)
22826d24e334Svsakar 			(void) fprintf(stderr, "%s", tmpbuf);
22836451fdbcSvsakar 		free(tmpbuf);
22847c478bd9Sstevel@tonic-gate 		lockexit(0);
22856451fdbcSvsakar 	}
22866451fdbcSvsakar 
22876451fdbcSvsakar 	free(tmpbuf);
22887c478bd9Sstevel@tonic-gate 	if (grow)
22897c478bd9Sstevel@tonic-gate 		goto grow50;
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate 	/*
22927c478bd9Sstevel@tonic-gate 	 * Now construct the initial file system,
22937c478bd9Sstevel@tonic-gate 	 * then write out the super-block.
22947c478bd9Sstevel@tonic-gate 	 */
22957c478bd9Sstevel@tonic-gate 	fsinit();
22967c478bd9Sstevel@tonic-gate grow50:
22977c478bd9Sstevel@tonic-gate 	/*
22987c478bd9Sstevel@tonic-gate 	 * write the superblock and csum information
22997c478bd9Sstevel@tonic-gate 	 */
23007c478bd9Sstevel@tonic-gate 	wtsb();
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 	/*
23037c478bd9Sstevel@tonic-gate 	 * extend the last cylinder group in the original file system
23047c478bd9Sstevel@tonic-gate 	 */
23057c478bd9Sstevel@tonic-gate 	if (grow) {
23067c478bd9Sstevel@tonic-gate 		extendcg(grow_fs_ncg-1);
23077c478bd9Sstevel@tonic-gate 		wtsb();
23087c478bd9Sstevel@tonic-gate 	}
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 	/*
23117c478bd9Sstevel@tonic-gate 	 * Write out the duplicate super blocks to the first 10
23127c478bd9Sstevel@tonic-gate 	 * cylinder groups (or fewer, if there are fewer than 10
23137c478bd9Sstevel@tonic-gate 	 * cylinder groups).
23147c478bd9Sstevel@tonic-gate 	 */
23157c478bd9Sstevel@tonic-gate 	for (cylno = 0; cylno < sblock.fs_ncg && cylno < 10; cylno++)
23167c478bd9Sstevel@tonic-gate 		awtfs(fsbtodb(&sblock, (uint64_t)cgsblock(&sblock, cylno)),
23177c478bd9Sstevel@tonic-gate 		    (int)sbsize, (char *)&sblock, SAVE);
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	/*
23207c478bd9Sstevel@tonic-gate 	 * Now write out duplicate super blocks to the remaining
23217c478bd9Sstevel@tonic-gate 	 * cylinder groups.  In the case of multi-terabyte file
23227c478bd9Sstevel@tonic-gate 	 * systems, just write out the super block to the last ten
23237c478bd9Sstevel@tonic-gate 	 * cylinder groups (or however many are left).
23247c478bd9Sstevel@tonic-gate 	 */
23257c478bd9Sstevel@tonic-gate 	if (mtb == 'y') {
23267c478bd9Sstevel@tonic-gate 		if (sblock.fs_ncg <= 10)
23277c478bd9Sstevel@tonic-gate 			cylno = sblock.fs_ncg;
23287c478bd9Sstevel@tonic-gate 		else if (sblock.fs_ncg <= 20)
23297c478bd9Sstevel@tonic-gate 			cylno = 10;
23307c478bd9Sstevel@tonic-gate 		else
23317c478bd9Sstevel@tonic-gate 			cylno = sblock.fs_ncg - 10;
23327c478bd9Sstevel@tonic-gate 	}
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	for (; cylno < sblock.fs_ncg; cylno++)
23357c478bd9Sstevel@tonic-gate 		awtfs(fsbtodb(&sblock, (uint64_t)cgsblock(&sblock, cylno)),
23367c478bd9Sstevel@tonic-gate 		    (int)sbsize, (char *)&sblock, SAVE);
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 	/*
23397c478bd9Sstevel@tonic-gate 	 * Flush out all the AIO writes we've done.  It's not
23407c478bd9Sstevel@tonic-gate 	 * necessary to do this explicitly, but it's the only
23417c478bd9Sstevel@tonic-gate 	 * way to report any errors from those writes.
23427c478bd9Sstevel@tonic-gate 	 */
23437c478bd9Sstevel@tonic-gate 	flush_writes();
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 	/*
23467c478bd9Sstevel@tonic-gate 	 * set clean flag
23477c478bd9Sstevel@tonic-gate 	 */
23487c478bd9Sstevel@tonic-gate 	if (grow)
23497c478bd9Sstevel@tonic-gate 		sblock.fs_clean = grow_fs_clean;
23507c478bd9Sstevel@tonic-gate 	else
23517c478bd9Sstevel@tonic-gate 		sblock.fs_clean = FSCLEAN;
23527c478bd9Sstevel@tonic-gate 	sblock.fs_time = mkfstime;
23537c478bd9Sstevel@tonic-gate 	sblock.fs_state = FSOKAY - sblock.fs_time;
23547c478bd9Sstevel@tonic-gate 	wtfs((diskaddr_t)(SBOFF / sectorsize), sbsize, (char *)&sblock);
23557c478bd9Sstevel@tonic-gate 	isbad = 0;
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	if (fsync(fso) == -1) {
23587c478bd9Sstevel@tonic-gate 		saverr = errno;
23597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
23607c478bd9Sstevel@tonic-gate 		    gettext("mkfs: fsync failed on write disk: %s\n"),
23617c478bd9Sstevel@tonic-gate 		    strerror(saverr));
23627c478bd9Sstevel@tonic-gate 		/* we're just cleaning up, so keep going */
23637c478bd9Sstevel@tonic-gate 	}
23647c478bd9Sstevel@tonic-gate 	if (close(fsi) == -1) {
23657c478bd9Sstevel@tonic-gate 		saverr = errno;
23667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
23677c478bd9Sstevel@tonic-gate 		    gettext("mkfs: close failed on read disk: %s\n"),
23687c478bd9Sstevel@tonic-gate 		    strerror(saverr));
23697c478bd9Sstevel@tonic-gate 		/* we're just cleaning up, so keep going */
23707c478bd9Sstevel@tonic-gate 	}
23717c478bd9Sstevel@tonic-gate 	if (close(fso) == -1) {
23727c478bd9Sstevel@tonic-gate 		saverr = errno;
23737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
23747c478bd9Sstevel@tonic-gate 		    gettext("mkfs: close failed on write disk: %s\n"),
23757c478bd9Sstevel@tonic-gate 		    strerror(saverr));
23767c478bd9Sstevel@tonic-gate 		/* we're just cleaning up, so keep going */
23777c478bd9Sstevel@tonic-gate 	}
23787c478bd9Sstevel@tonic-gate 	fsi = fso = -1;
2379029b4de8Sswilcox 
23807c478bd9Sstevel@tonic-gate #ifndef STANDALONE
23817c478bd9Sstevel@tonic-gate 	lockexit(0);
23827c478bd9Sstevel@tonic-gate #endif
2383029b4de8Sswilcox 
2384029b4de8Sswilcox 	return (0);
23857c478bd9Sstevel@tonic-gate }
23867c478bd9Sstevel@tonic-gate 
2387fe12dc75SToomas Soome static diskaddr_t
get_device_size(int fd)2388fe12dc75SToomas Soome get_device_size(int fd)
2389fe12dc75SToomas Soome {
2390fe12dc75SToomas Soome 	struct dk_minfo	disk_info;
2391fe12dc75SToomas Soome 
2392fe12dc75SToomas Soome 	if ((ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info)) == -1)
2393fe12dc75SToomas Soome 		return (0);
2394fe12dc75SToomas Soome 
2395fe12dc75SToomas Soome 	return (disk_info.dki_capacity);
2396fe12dc75SToomas Soome }
2397fe12dc75SToomas Soome 
23987c478bd9Sstevel@tonic-gate /*
23997c478bd9Sstevel@tonic-gate  * Figure out how big the partition we're dealing with is.
24007c478bd9Sstevel@tonic-gate  * The value returned is in disk blocks (sectors);
24017c478bd9Sstevel@tonic-gate  */
24027c478bd9Sstevel@tonic-gate static diskaddr_t
get_max_size(int fd)24037c478bd9Sstevel@tonic-gate get_max_size(int fd)
24047c478bd9Sstevel@tonic-gate {
2405342440ecSPrasad Singamsetty 	struct extvtoc vtoc;
24067c478bd9Sstevel@tonic-gate 	dk_gpt_t *efi_vtoc;
24077c478bd9Sstevel@tonic-gate 	diskaddr_t	slicesize;
24087c478bd9Sstevel@tonic-gate 
2409342440ecSPrasad Singamsetty 	int index = read_extvtoc(fd, &vtoc);
24107c478bd9Sstevel@tonic-gate 
24116451fdbcSvsakar 	if (index >= 0) {
24126451fdbcSvsakar 		label_type = LABEL_TYPE_VTOC;
24136451fdbcSvsakar 	} else {
24147c478bd9Sstevel@tonic-gate 		if (index == VT_ENOTSUP || index == VT_ERROR) {
24157c478bd9Sstevel@tonic-gate 			/* it might be an EFI label */
24167c478bd9Sstevel@tonic-gate 			index = efi_alloc_and_read(fd, &efi_vtoc);
24176451fdbcSvsakar 			label_type = LABEL_TYPE_EFI;
24187c478bd9Sstevel@tonic-gate 		}
24197c478bd9Sstevel@tonic-gate 	}
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 	if (index < 0) {
2422fe12dc75SToomas Soome 		/*
2423fe12dc75SToomas Soome 		 * Since both attempts to read the label failed, we're
2424fe12dc75SToomas Soome 		 * going to use DKIOCGMEDIAINFO to get device size.
2425fe12dc75SToomas Soome 		 */
2426fe12dc75SToomas Soome 
2427fe12dc75SToomas Soome 		label_type = LABEL_TYPE_OTHER;
2428fe12dc75SToomas Soome 		slicesize = get_device_size(fd);
2429fe12dc75SToomas Soome 		if (slicesize == 0) {
2430fe12dc75SToomas Soome 			switch (index) {
2431fe12dc75SToomas Soome 			case VT_ERROR:
2432fe12dc75SToomas Soome 				break;
2433fe12dc75SToomas Soome 			case VT_EIO:
2434fe12dc75SToomas Soome 				errno = EIO;
2435fe12dc75SToomas Soome 				break;
2436fe12dc75SToomas Soome 			case VT_EINVAL:
2437fe12dc75SToomas Soome 				errno = EINVAL;
2438fe12dc75SToomas Soome 			}
2439fe12dc75SToomas Soome 			perror(gettext("Can not determine partition size"));
2440fe12dc75SToomas Soome 			lockexit(32);
24417c478bd9Sstevel@tonic-gate 		}
24427c478bd9Sstevel@tonic-gate 	}
24437c478bd9Sstevel@tonic-gate 
24446451fdbcSvsakar 	if (label_type == LABEL_TYPE_EFI) {
24457c478bd9Sstevel@tonic-gate 		slicesize = efi_vtoc->efi_parts[index].p_size;
24467c478bd9Sstevel@tonic-gate 		efi_free(efi_vtoc);
2447fe12dc75SToomas Soome 	} else if (label_type == LABEL_TYPE_VTOC) {
24487c478bd9Sstevel@tonic-gate 		/*
24497c478bd9Sstevel@tonic-gate 		 * In the vtoc struct, p_size is a 32-bit signed quantity.
24507c478bd9Sstevel@tonic-gate 		 * In the dk_gpt struct (efi's version of the vtoc), p_size
24517c478bd9Sstevel@tonic-gate 		 * is an unsigned 64-bit quantity.  By casting the vtoc's
24527c478bd9Sstevel@tonic-gate 		 * psize to an unsigned 32-bit quantity, it will be copied
24537c478bd9Sstevel@tonic-gate 		 * to 'slicesize' (an unsigned 64-bit diskaddr_t) without
24547c478bd9Sstevel@tonic-gate 		 * sign extension.
24557c478bd9Sstevel@tonic-gate 		 */
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 		slicesize = (uint32_t)vtoc.v_part[index].p_size;
24587c478bd9Sstevel@tonic-gate 	}
24597c478bd9Sstevel@tonic-gate 
24606451fdbcSvsakar 	dprintf(("DeBuG get_max_size index = %d, p_size = %lld, dolimit = %d\n",
24616451fdbcSvsakar 	    index, slicesize, (slicesize > FS_MAX)));
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 	/*
24647c478bd9Sstevel@tonic-gate 	 * The next line limits a UFS file system to the maximum
24657c478bd9Sstevel@tonic-gate 	 * supported size.
24667c478bd9Sstevel@tonic-gate 	 */
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	if (slicesize > FS_MAX)
24697c478bd9Sstevel@tonic-gate 		return (FS_MAX);
24707c478bd9Sstevel@tonic-gate 	return (slicesize);
24717c478bd9Sstevel@tonic-gate }
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate static long
get_max_track_size(int fd)24747c478bd9Sstevel@tonic-gate get_max_track_size(int fd)
24757c478bd9Sstevel@tonic-gate {
24767c478bd9Sstevel@tonic-gate 	struct dk_cinfo ci;
24777c478bd9Sstevel@tonic-gate 	long track_size = -1;
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, &ci) == 0) {
24807c478bd9Sstevel@tonic-gate 		track_size = ci.dki_maxtransfer * DEV_BSIZE;
24817c478bd9Sstevel@tonic-gate 	}
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate 	if ((track_size < 0)) {
24847c478bd9Sstevel@tonic-gate 		int	error = 0;
24857c478bd9Sstevel@tonic-gate 		int	maxphys;
24867c478bd9Sstevel@tonic-gate 		int	gotit = 0;
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 		gotit = fsgetmaxphys(&maxphys, &error);
24897c478bd9Sstevel@tonic-gate 		if (gotit) {
24907c478bd9Sstevel@tonic-gate 			track_size = MIN(MB, maxphys);
24917c478bd9Sstevel@tonic-gate 		} else {
24927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
24937c478bd9Sstevel@tonic-gate "Warning: Could not get system value for maxphys. The value for\n"
24947c478bd9Sstevel@tonic-gate "maxcontig will default to 1MB.\n"));
24957c478bd9Sstevel@tonic-gate 			track_size = MB;
24967c478bd9Sstevel@tonic-gate 		}
24977c478bd9Sstevel@tonic-gate 	}
24987c478bd9Sstevel@tonic-gate 	return (track_size);
24997c478bd9Sstevel@tonic-gate }
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate /*
25027c478bd9Sstevel@tonic-gate  * Initialize a cylinder group.
25037c478bd9Sstevel@tonic-gate  */
25047c478bd9Sstevel@tonic-gate static void
initcg(int cylno)25057c478bd9Sstevel@tonic-gate initcg(int cylno)
25067c478bd9Sstevel@tonic-gate {
25077c478bd9Sstevel@tonic-gate 	diskaddr_t cbase, d;
25087c478bd9Sstevel@tonic-gate 	diskaddr_t dlower;	/* last data block before cg metadata */
25097c478bd9Sstevel@tonic-gate 	diskaddr_t dupper;	/* first data block after cg metadata */
25107c478bd9Sstevel@tonic-gate 	diskaddr_t dmax;
25117c478bd9Sstevel@tonic-gate 	int64_t i;
25127c478bd9Sstevel@tonic-gate 	struct csum *cs;
25137c478bd9Sstevel@tonic-gate 	struct dinode *inode_buffer;
25147c478bd9Sstevel@tonic-gate 	int size;
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate 	/*
25177c478bd9Sstevel@tonic-gate 	 * Variables used to store intermediate results as a part of
25187c478bd9Sstevel@tonic-gate 	 * the internal implementation of the cbtocylno() macros.
25197c478bd9Sstevel@tonic-gate 	 */
25207c478bd9Sstevel@tonic-gate 	diskaddr_t bno;		/* UFS block number (not sector number) */
25217c478bd9Sstevel@tonic-gate 	int	cbcylno;	/* current cylinder number */
25227c478bd9Sstevel@tonic-gate 	int	cbcylno_sect;	/* sector offset within cylinder */
25237c478bd9Sstevel@tonic-gate 	int	cbsect_incr;	/* amount to increment sector offset */
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 	/*
25267c478bd9Sstevel@tonic-gate 	 * Variables used to store intermediate results as a part of
25277c478bd9Sstevel@tonic-gate 	 * the internal implementation of the cbtorpos() macros.
25287c478bd9Sstevel@tonic-gate 	 */
25297c478bd9Sstevel@tonic-gate 	short	*cgblks;	/* pointer to array of free blocks in cg */
25307c478bd9Sstevel@tonic-gate 	int	trackrpos;	/* tmp variable for rotation position */
25317c478bd9Sstevel@tonic-gate 	int	trackoff;	/* offset within a track */
25327c478bd9Sstevel@tonic-gate 	int	trackoff_incr;	/* amount to increment trackoff */
25337c478bd9Sstevel@tonic-gate 	int	rpos;		/* rotation position of current block */
25347c478bd9Sstevel@tonic-gate 	int	rpos_incr;	/* amount to increment rpos per block */
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate 	union cgun *icgun;	/* local pointer to a cg summary block */
25377c478bd9Sstevel@tonic-gate #define	icg	(icgun->cg)
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	icgun = (union cgun *)getbuf(&cgsumbuf, sizeof (union cgun));
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 	/*
25427c478bd9Sstevel@tonic-gate 	 * Determine block bounds for cylinder group.
25437c478bd9Sstevel@tonic-gate 	 * Allow space for super block summary information in first
25447c478bd9Sstevel@tonic-gate 	 * cylinder group.
25457c478bd9Sstevel@tonic-gate 	 */
25467c478bd9Sstevel@tonic-gate 	cbase = cgbase(&sblock, cylno);
25477c478bd9Sstevel@tonic-gate 	dmax = cbase + sblock.fs_fpg;
25487c478bd9Sstevel@tonic-gate 	if (dmax > sblock.fs_size)	/* last cg may be smaller than normal */
25497c478bd9Sstevel@tonic-gate 		dmax = sblock.fs_size;
25507c478bd9Sstevel@tonic-gate 	dlower = cgsblock(&sblock, cylno) - cbase;
25517c478bd9Sstevel@tonic-gate 	dupper = cgdmin(&sblock, cylno) - cbase;
25527c478bd9Sstevel@tonic-gate 	if (cylno == 0)
25537c478bd9Sstevel@tonic-gate 		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
25547c478bd9Sstevel@tonic-gate 	cs = fscs + cylno;
25557c478bd9Sstevel@tonic-gate 	icg.cg_time = mkfstime;
25567c478bd9Sstevel@tonic-gate 	icg.cg_magic = CG_MAGIC;
25577c478bd9Sstevel@tonic-gate 	icg.cg_cgx = cylno;
2558355d6bb5Sswilcox 	/* last one gets whatever's left */
25597c478bd9Sstevel@tonic-gate 	if (cylno == sblock.fs_ncg - 1)
2560355d6bb5Sswilcox 		icg.cg_ncyl = sblock.fs_ncyl - (sblock.fs_cpg * cylno);
25617c478bd9Sstevel@tonic-gate 	else
25627c478bd9Sstevel@tonic-gate 		icg.cg_ncyl = sblock.fs_cpg;
25637c478bd9Sstevel@tonic-gate 	icg.cg_niblk = sblock.fs_ipg;
25647c478bd9Sstevel@tonic-gate 	icg.cg_ndblk = dmax - cbase;
25657c478bd9Sstevel@tonic-gate 	icg.cg_cs.cs_ndir = 0;
25667c478bd9Sstevel@tonic-gate 	icg.cg_cs.cs_nffree = 0;
25677c478bd9Sstevel@tonic-gate 	icg.cg_cs.cs_nbfree = 0;
25687c478bd9Sstevel@tonic-gate 	icg.cg_cs.cs_nifree = 0;
25697c478bd9Sstevel@tonic-gate 	icg.cg_rotor = 0;
25707c478bd9Sstevel@tonic-gate 	icg.cg_frotor = 0;
25717c478bd9Sstevel@tonic-gate 	icg.cg_irotor = 0;
25727c478bd9Sstevel@tonic-gate 	icg.cg_btotoff = &icg.cg_space[0] - (uchar_t *)(&icg.cg_link);
25737c478bd9Sstevel@tonic-gate 	icg.cg_boff = icg.cg_btotoff + sblock.fs_cpg * sizeof (long);
25747c478bd9Sstevel@tonic-gate 	icg.cg_iusedoff = icg.cg_boff +
2575d50c8f90Svsakar 	    sblock.fs_cpg * sblock.fs_nrpos * sizeof (short);
25767c478bd9Sstevel@tonic-gate 	icg.cg_freeoff = icg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
25777c478bd9Sstevel@tonic-gate 	icg.cg_nextfreeoff = icg.cg_freeoff +
2578d50c8f90Svsakar 	    howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
25797c478bd9Sstevel@tonic-gate 	for (i = 0; i < sblock.fs_frag; i++) {
25807c478bd9Sstevel@tonic-gate 		icg.cg_frsum[i] = 0;
25817c478bd9Sstevel@tonic-gate 	}
25827c478bd9Sstevel@tonic-gate 	bzero((caddr_t)cg_inosused(&icg), icg.cg_freeoff - icg.cg_iusedoff);
25837c478bd9Sstevel@tonic-gate 	icg.cg_cs.cs_nifree += sblock.fs_ipg;
25847c478bd9Sstevel@tonic-gate 	if (cylno == 0)
25857c478bd9Sstevel@tonic-gate 		for (i = 0; i < UFSROOTINO; i++) {
25867c478bd9Sstevel@tonic-gate 			setbit(cg_inosused(&icg), i);
25877c478bd9Sstevel@tonic-gate 			icg.cg_cs.cs_nifree--;
25887c478bd9Sstevel@tonic-gate 		}
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 	/*
25917c478bd9Sstevel@tonic-gate 	 * Initialize all the inodes in the cylinder group using
25927c478bd9Sstevel@tonic-gate 	 * random numbers.
25937c478bd9Sstevel@tonic-gate 	 */
25947c478bd9Sstevel@tonic-gate 	size = sblock.fs_ipg * sizeof (struct dinode);
25957c478bd9Sstevel@tonic-gate 	inode_buffer = (struct dinode *)getbuf(&inodebuf, size);
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate 	for (i = 0; i < sblock.fs_ipg; i++) {
25987c478bd9Sstevel@tonic-gate 		IRANDOMIZE(&(inode_buffer[i].di_ic));
25997c478bd9Sstevel@tonic-gate 	}
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate 	/*
26027c478bd9Sstevel@tonic-gate 	 * Write all inodes in a single write for performance.
26037c478bd9Sstevel@tonic-gate 	 */
26047c478bd9Sstevel@tonic-gate 	awtfs(fsbtodb(&sblock, (uint64_t)cgimin(&sblock, cylno)), (int)size,
26057c478bd9Sstevel@tonic-gate 	    (char *)inode_buffer, RELEASE);
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate 	bzero((caddr_t)cg_blktot(&icg), icg.cg_boff - icg.cg_btotoff);
26087c478bd9Sstevel@tonic-gate 	bzero((caddr_t)cg_blks(&sblock, &icg, 0),
26097c478bd9Sstevel@tonic-gate 	    icg.cg_iusedoff - icg.cg_boff);
26107c478bd9Sstevel@tonic-gate 	bzero((caddr_t)cg_blksfree(&icg), icg.cg_nextfreeoff - icg.cg_freeoff);
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	if (cylno > 0) {
26137c478bd9Sstevel@tonic-gate 		for (d = 0; d < dlower; d += sblock.fs_frag) {
26147c478bd9Sstevel@tonic-gate 			setblock(&sblock, cg_blksfree(&icg), d/sblock.fs_frag);
26157c478bd9Sstevel@tonic-gate 			icg.cg_cs.cs_nbfree++;
26167c478bd9Sstevel@tonic-gate 			cg_blktot(&icg)[cbtocylno(&sblock, d)]++;
26177c478bd9Sstevel@tonic-gate 			cg_blks(&sblock, &icg, cbtocylno(&sblock, d))
26187c478bd9Sstevel@tonic-gate 			    [cbtorpos(&sblock, d)]++;
26197c478bd9Sstevel@tonic-gate 		}
26207c478bd9Sstevel@tonic-gate 		sblock.fs_dsize += dlower;
26217c478bd9Sstevel@tonic-gate 	}
26227c478bd9Sstevel@tonic-gate 	sblock.fs_dsize += icg.cg_ndblk - dupper;
26237c478bd9Sstevel@tonic-gate 	if ((i = dupper % sblock.fs_frag) != 0) {
26247c478bd9Sstevel@tonic-gate 		icg.cg_frsum[sblock.fs_frag - i]++;
26257c478bd9Sstevel@tonic-gate 		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
26267c478bd9Sstevel@tonic-gate 			setbit(cg_blksfree(&icg), dupper);
26277c478bd9Sstevel@tonic-gate 			icg.cg_cs.cs_nffree++;
26287c478bd9Sstevel@tonic-gate 		}
26297c478bd9Sstevel@tonic-gate 	}
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	/*
26327c478bd9Sstevel@tonic-gate 	 * WARNING: The following code is somewhat confusing, but
26337c478bd9Sstevel@tonic-gate 	 * results in a substantial performance improvement in mkfs.
26347c478bd9Sstevel@tonic-gate 	 *
26357c478bd9Sstevel@tonic-gate 	 * Instead of using cbtocylno() and cbtorpos() macros, we
26367c478bd9Sstevel@tonic-gate 	 * keep track of all the intermediate state of those macros
26377c478bd9Sstevel@tonic-gate 	 * in some variables.  This allows simple addition to be
26387c478bd9Sstevel@tonic-gate 	 * done to calculate the results as we step through the
26397c478bd9Sstevel@tonic-gate 	 * blocks in an orderly fashion instead of the slower
26407c478bd9Sstevel@tonic-gate 	 * multiplication and division the macros are forced to
26417c478bd9Sstevel@tonic-gate 	 * used so they can support random input.  (Multiplication,
26427c478bd9Sstevel@tonic-gate 	 * division, and remainder operations typically take about
26437c478bd9Sstevel@tonic-gate 	 * 10x as many processor cycles as other operations.)
26447c478bd9Sstevel@tonic-gate 	 *
26457c478bd9Sstevel@tonic-gate 	 * The basic idea is to take code:
26467c478bd9Sstevel@tonic-gate 	 *
26477c478bd9Sstevel@tonic-gate 	 *	for (x = starting_x; x < max; x++)
26487c478bd9Sstevel@tonic-gate 	 *		y = (x * c) / z
26497c478bd9Sstevel@tonic-gate 	 *
26507c478bd9Sstevel@tonic-gate 	 * and rewrite it to take advantage of the fact that
26517c478bd9Sstevel@tonic-gate 	 * the variable x is incrementing in an orderly way:
26527c478bd9Sstevel@tonic-gate 	 *
26537c478bd9Sstevel@tonic-gate 	 *	intermediate = starting_x * c
26547c478bd9Sstevel@tonic-gate 	 *	yval = intermediate / z
26557c478bd9Sstevel@tonic-gate 	 *	for (x = starting_x; x < max; x++) {
26567c478bd9Sstevel@tonic-gate 	 *		y = yval;
26577c478bd9Sstevel@tonic-gate 	 *		intermediate += c
26587c478bd9Sstevel@tonic-gate 	 *		if (intermediate > z) {
26597c478bd9Sstevel@tonic-gate 	 *			yval++;
26607c478bd9Sstevel@tonic-gate 	 *			intermediate -= z
26617c478bd9Sstevel@tonic-gate 	 *		}
26627c478bd9Sstevel@tonic-gate 	 *	}
26637c478bd9Sstevel@tonic-gate 	 *
26647c478bd9Sstevel@tonic-gate 	 * Performance has improved as much as 4X using this code.
26657c478bd9Sstevel@tonic-gate 	 */
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 	/*
26687c478bd9Sstevel@tonic-gate 	 * Initialize the starting points for all the cbtocylno()
26697c478bd9Sstevel@tonic-gate 	 * macro variables and figure out the increments needed each
26707c478bd9Sstevel@tonic-gate 	 * time through the loop.
26717c478bd9Sstevel@tonic-gate 	 */
26727c478bd9Sstevel@tonic-gate 	cbcylno_sect = dupper * NSPF(&sblock);
26737c478bd9Sstevel@tonic-gate 	cbsect_incr = sblock.fs_frag * NSPF(&sblock);
26747c478bd9Sstevel@tonic-gate 	cbcylno = cbcylno_sect / sblock.fs_spc;
26757c478bd9Sstevel@tonic-gate 	cbcylno_sect %= sblock.fs_spc;
26767c478bd9Sstevel@tonic-gate 	cgblks = cg_blks(&sblock, &icg, cbcylno);
26777c478bd9Sstevel@tonic-gate 	bno = dupper / sblock.fs_frag;
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate 	/*
26807c478bd9Sstevel@tonic-gate 	 * Initialize the starting points for all the cbtorpos()
26817c478bd9Sstevel@tonic-gate 	 * macro variables and figure out the increments needed each
26827c478bd9Sstevel@tonic-gate 	 * time through the loop.
26837c478bd9Sstevel@tonic-gate 	 *
26847c478bd9Sstevel@tonic-gate 	 * It's harder to simplify the cbtorpos() macro if there were
26857c478bd9Sstevel@tonic-gate 	 * alternate sectors specified (or if they previously existed
26867c478bd9Sstevel@tonic-gate 	 * in the growfs case).  Since this is rare, we just revert to
26877c478bd9Sstevel@tonic-gate 	 * using the macros in this case and skip the variable setup.
26887c478bd9Sstevel@tonic-gate 	 */
26897c478bd9Sstevel@tonic-gate 	if (!spc_flag) {
26907c478bd9Sstevel@tonic-gate 		trackrpos = (cbcylno_sect % sblock.fs_nsect) * sblock.fs_nrpos;
26917c478bd9Sstevel@tonic-gate 		rpos = trackrpos / sblock.fs_nsect;
26927c478bd9Sstevel@tonic-gate 		trackoff = trackrpos % sblock.fs_nsect;
26937c478bd9Sstevel@tonic-gate 		trackoff_incr = cbsect_incr * sblock.fs_nrpos;
26947c478bd9Sstevel@tonic-gate 		rpos_incr = (trackoff_incr / sblock.fs_nsect) % sblock.fs_nrpos;
26957c478bd9Sstevel@tonic-gate 		trackoff_incr = trackoff_incr % sblock.fs_nsect;
26967c478bd9Sstevel@tonic-gate 	}
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 	/*
26997c478bd9Sstevel@tonic-gate 	 * Loop through all the blocks, marking them free and
27007c478bd9Sstevel@tonic-gate 	 * updating totals kept in the superblock and cg summary.
27017c478bd9Sstevel@tonic-gate 	 */
27027c478bd9Sstevel@tonic-gate 	for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
27037c478bd9Sstevel@tonic-gate 		setblock(&sblock, cg_blksfree(&icg),  bno);
27047c478bd9Sstevel@tonic-gate 		icg.cg_cs.cs_nbfree++;
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate 		cg_blktot(&icg)[cbcylno]++;
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 		if (!spc_flag)
27097c478bd9Sstevel@tonic-gate 			cgblks[rpos]++;
27107c478bd9Sstevel@tonic-gate 		else
27117c478bd9Sstevel@tonic-gate 			cg_blks(&sblock, &icg, cbtocylno(&sblock, d))
27127c478bd9Sstevel@tonic-gate 			    [cbtorpos(&sblock, d)]++;
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 		d += sblock.fs_frag;
27157c478bd9Sstevel@tonic-gate 		bno++;
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate 		/*
27187c478bd9Sstevel@tonic-gate 		 * Increment the sector offset within the cylinder
27197c478bd9Sstevel@tonic-gate 		 * for the cbtocylno() macro reimplementation.  If
27207c478bd9Sstevel@tonic-gate 		 * we're beyond the end of the cylinder, update the
27217c478bd9Sstevel@tonic-gate 		 * cylinder number, calculate the offset in the
27227c478bd9Sstevel@tonic-gate 		 * new cylinder, and update the cgblks pointer
27237c478bd9Sstevel@tonic-gate 		 * to the next rotational position.
27247c478bd9Sstevel@tonic-gate 		 */
27257c478bd9Sstevel@tonic-gate 		cbcylno_sect += cbsect_incr;
27267c478bd9Sstevel@tonic-gate 		if (cbcylno_sect >= sblock.fs_spc) {
27277c478bd9Sstevel@tonic-gate 			cbcylno++;
27287c478bd9Sstevel@tonic-gate 			cbcylno_sect -= sblock.fs_spc;
27297c478bd9Sstevel@tonic-gate 			cgblks += sblock.fs_nrpos;
27307c478bd9Sstevel@tonic-gate 		}
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 		/*
27337c478bd9Sstevel@tonic-gate 		 * If there aren't alternate sectors, increment the
27347c478bd9Sstevel@tonic-gate 		 * rotational position variables for the cbtorpos()
27357c478bd9Sstevel@tonic-gate 		 * reimplementation.  Note that we potentially
27367c478bd9Sstevel@tonic-gate 		 * increment rpos twice.  Once by rpos_incr, and one
27377c478bd9Sstevel@tonic-gate 		 * more time when we wrap to a new track because
27387c478bd9Sstevel@tonic-gate 		 * trackoff >= fs_nsect.
27397c478bd9Sstevel@tonic-gate 		 */
27407c478bd9Sstevel@tonic-gate 		if (!spc_flag) {
27417c478bd9Sstevel@tonic-gate 			trackoff += trackoff_incr;
27427c478bd9Sstevel@tonic-gate 			rpos += rpos_incr;
27437c478bd9Sstevel@tonic-gate 			if (trackoff >= sblock.fs_nsect) {
27447c478bd9Sstevel@tonic-gate 				trackoff -= sblock.fs_nsect;
27457c478bd9Sstevel@tonic-gate 				rpos++;
27467c478bd9Sstevel@tonic-gate 			}
27477c478bd9Sstevel@tonic-gate 			if (rpos >= sblock.fs_nrpos)
27487c478bd9Sstevel@tonic-gate 				rpos -= sblock.fs_nrpos;
27497c478bd9Sstevel@tonic-gate 		}
27507c478bd9Sstevel@tonic-gate 	}
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 	if (d < dmax - cbase) {
27537c478bd9Sstevel@tonic-gate 		icg.cg_frsum[dmax - cbase - d]++;
27547c478bd9Sstevel@tonic-gate 		for (; d < dmax - cbase; d++) {
27557c478bd9Sstevel@tonic-gate 			setbit(cg_blksfree(&icg), d);
27567c478bd9Sstevel@tonic-gate 			icg.cg_cs.cs_nffree++;
27577c478bd9Sstevel@tonic-gate 		}
27587c478bd9Sstevel@tonic-gate 	}
27597c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_ndir += icg.cg_cs.cs_ndir;
27607c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_nffree += icg.cg_cs.cs_nffree;
27617c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_nbfree += icg.cg_cs.cs_nbfree;
27627c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_nifree += icg.cg_cs.cs_nifree;
27637c478bd9Sstevel@tonic-gate 	*cs = icg.cg_cs;
27647c478bd9Sstevel@tonic-gate 	awtfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, cylno)),
2765d50c8f90Svsakar 	    sblock.fs_bsize, (char *)&icg, RELEASE);
27667c478bd9Sstevel@tonic-gate }
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate /*
27697c478bd9Sstevel@tonic-gate  * initialize the file system
27707c478bd9Sstevel@tonic-gate  */
27717c478bd9Sstevel@tonic-gate struct inode node;
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate #define	LOSTDIR
27747c478bd9Sstevel@tonic-gate #ifdef LOSTDIR
27757c478bd9Sstevel@tonic-gate #define	PREDEFDIR 3
27767c478bd9Sstevel@tonic-gate #else
27777c478bd9Sstevel@tonic-gate #define	PREDEFDIR 2
27787c478bd9Sstevel@tonic-gate #endif
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate struct direct root_dir[] = {
27817c478bd9Sstevel@tonic-gate 	{ UFSROOTINO, sizeof (struct direct), 1, "." },
27827c478bd9Sstevel@tonic-gate 	{ UFSROOTINO, sizeof (struct direct), 2, ".." },
27837c478bd9Sstevel@tonic-gate #ifdef LOSTDIR
27847c478bd9Sstevel@tonic-gate 	{ LOSTFOUNDINO, sizeof (struct direct), 10, "lost+found" },
27857c478bd9Sstevel@tonic-gate #endif
27867c478bd9Sstevel@tonic-gate };
27877c478bd9Sstevel@tonic-gate #ifdef LOSTDIR
27887c478bd9Sstevel@tonic-gate struct direct lost_found_dir[] = {
27897c478bd9Sstevel@tonic-gate 	{ LOSTFOUNDINO, sizeof (struct direct), 1, "." },
27907c478bd9Sstevel@tonic-gate 	{ UFSROOTINO, sizeof (struct direct), 2, ".." },
27917c478bd9Sstevel@tonic-gate 	{ 0, DIRBLKSIZ, 0, 0 },
27927c478bd9Sstevel@tonic-gate };
27937c478bd9Sstevel@tonic-gate #endif
27947c478bd9Sstevel@tonic-gate char buf[MAXBSIZE];
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate static void
fsinit()27977c478bd9Sstevel@tonic-gate fsinit()
27987c478bd9Sstevel@tonic-gate {
27997c478bd9Sstevel@tonic-gate 	int i;
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	/*
28037c478bd9Sstevel@tonic-gate 	 * initialize the node
28047c478bd9Sstevel@tonic-gate 	 */
28057c478bd9Sstevel@tonic-gate 	node.i_atime = mkfstime;
28067c478bd9Sstevel@tonic-gate 	node.i_mtime = mkfstime;
28077c478bd9Sstevel@tonic-gate 	node.i_ctime = mkfstime;
28087c478bd9Sstevel@tonic-gate #ifdef LOSTDIR
28097c478bd9Sstevel@tonic-gate 	/*
28107c478bd9Sstevel@tonic-gate 	 * create the lost+found directory
28117c478bd9Sstevel@tonic-gate 	 */
28127c478bd9Sstevel@tonic-gate 	(void) makedir(lost_found_dir, 2);
28137c478bd9Sstevel@tonic-gate 	for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ) {
28147c478bd9Sstevel@tonic-gate 		bcopy(&lost_found_dir[2], &buf[i], DIRSIZ(&lost_found_dir[2]));
28157c478bd9Sstevel@tonic-gate 	}
28167c478bd9Sstevel@tonic-gate 	node.i_number = LOSTFOUNDINO;
28172bc98732SRichard Lowe 	node.i_smode = IFDIR | 0700;
28187c478bd9Sstevel@tonic-gate 	node.i_nlink = 2;
28197c478bd9Sstevel@tonic-gate 	node.i_size = sblock.fs_bsize;
28207c478bd9Sstevel@tonic-gate 	node.i_db[0] = alloc((int)node.i_size, node.i_mode);
28217c478bd9Sstevel@tonic-gate 	node.i_blocks = btodb(fragroundup(&sblock, (int)node.i_size));
28227c478bd9Sstevel@tonic-gate 	IRANDOMIZE(&node.i_ic);
28237c478bd9Sstevel@tonic-gate 	wtfs(fsbtodb(&sblock, (uint64_t)node.i_db[0]), (int)node.i_size, buf);
28247c478bd9Sstevel@tonic-gate 	iput(&node);
28257c478bd9Sstevel@tonic-gate #endif
28267c478bd9Sstevel@tonic-gate 	/*
28277c478bd9Sstevel@tonic-gate 	 * create the root directory
28287c478bd9Sstevel@tonic-gate 	 */
28297c478bd9Sstevel@tonic-gate 	node.i_number = UFSROOTINO;
28302bc98732SRichard Lowe 	node.i_mode = IFDIR | UMASK;
28317c478bd9Sstevel@tonic-gate 	node.i_nlink = PREDEFDIR;
28327c478bd9Sstevel@tonic-gate 	node.i_size = makedir(root_dir, PREDEFDIR);
28337c478bd9Sstevel@tonic-gate 	node.i_db[0] = alloc(sblock.fs_fsize, node.i_mode);
28347c478bd9Sstevel@tonic-gate 	/* i_size < 2GB because we are initializing the file system */
28357c478bd9Sstevel@tonic-gate 	node.i_blocks = btodb(fragroundup(&sblock, (int)node.i_size));
28367c478bd9Sstevel@tonic-gate 	IRANDOMIZE(&node.i_ic);
28377c478bd9Sstevel@tonic-gate 	wtfs(fsbtodb(&sblock, (uint64_t)node.i_db[0]), sblock.fs_fsize, buf);
28387c478bd9Sstevel@tonic-gate 	iput(&node);
28397c478bd9Sstevel@tonic-gate }
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate /*
28427c478bd9Sstevel@tonic-gate  * construct a set of directory entries in "buf".
28437c478bd9Sstevel@tonic-gate  * return size of directory.
28447c478bd9Sstevel@tonic-gate  */
28457c478bd9Sstevel@tonic-gate static int
makedir(struct direct * protodir,int entries)28467c478bd9Sstevel@tonic-gate makedir(struct direct *protodir, int entries)
28477c478bd9Sstevel@tonic-gate {
28487c478bd9Sstevel@tonic-gate 	char *cp;
28497c478bd9Sstevel@tonic-gate 	int i;
28507c478bd9Sstevel@tonic-gate 	ushort_t spcleft;
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 	spcleft = DIRBLKSIZ;
28537c478bd9Sstevel@tonic-gate 	for (cp = buf, i = 0; i < entries - 1; i++) {
28547c478bd9Sstevel@tonic-gate 		protodir[i].d_reclen = DIRSIZ(&protodir[i]);
28557c478bd9Sstevel@tonic-gate 		bcopy(&protodir[i], cp, protodir[i].d_reclen);
28567c478bd9Sstevel@tonic-gate 		cp += protodir[i].d_reclen;
28577c478bd9Sstevel@tonic-gate 		spcleft -= protodir[i].d_reclen;
28587c478bd9Sstevel@tonic-gate 	}
28597c478bd9Sstevel@tonic-gate 	protodir[i].d_reclen = spcleft;
28607c478bd9Sstevel@tonic-gate 	bcopy(&protodir[i], cp, DIRSIZ(&protodir[i]));
28617c478bd9Sstevel@tonic-gate 	return (DIRBLKSIZ);
28627c478bd9Sstevel@tonic-gate }
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate /*
28657c478bd9Sstevel@tonic-gate  * allocate a block or frag
28667c478bd9Sstevel@tonic-gate  */
28677c478bd9Sstevel@tonic-gate static daddr32_t
alloc(int size,int mode)28687c478bd9Sstevel@tonic-gate alloc(int size, int mode)
28697c478bd9Sstevel@tonic-gate {
28707c478bd9Sstevel@tonic-gate 	int i, frag;
28717c478bd9Sstevel@tonic-gate 	daddr32_t d;
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 	rdfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, 0)), sblock.fs_cgsize,
28747c478bd9Sstevel@tonic-gate 	    (char *)&acg);
28757c478bd9Sstevel@tonic-gate 	if (acg.cg_magic != CG_MAGIC) {
28767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("cg 0: bad magic number\n"));
28777c478bd9Sstevel@tonic-gate 		lockexit(32);
28787c478bd9Sstevel@tonic-gate 	}
28797c478bd9Sstevel@tonic-gate 	if (acg.cg_cs.cs_nbfree == 0) {
28807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2881d50c8f90Svsakar 		    gettext("first cylinder group ran out of space\n"));
28827c478bd9Sstevel@tonic-gate 		lockexit(32);
28837c478bd9Sstevel@tonic-gate 	}
28847c478bd9Sstevel@tonic-gate 	for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
28857c478bd9Sstevel@tonic-gate 		if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag))
28867c478bd9Sstevel@tonic-gate 			goto goth;
28877c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
28887c478bd9Sstevel@tonic-gate 	    gettext("internal error: can't find block in cyl 0\n"));
28897c478bd9Sstevel@tonic-gate 	lockexit(32);
28907c478bd9Sstevel@tonic-gate goth:
28917c478bd9Sstevel@tonic-gate 	clrblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag);
28927c478bd9Sstevel@tonic-gate 	acg.cg_cs.cs_nbfree--;
28937c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_nbfree--;
28947c478bd9Sstevel@tonic-gate 	fscs[0].cs_nbfree--;
28957c478bd9Sstevel@tonic-gate 	if (mode & IFDIR) {
28967c478bd9Sstevel@tonic-gate 		acg.cg_cs.cs_ndir++;
28977c478bd9Sstevel@tonic-gate 		sblock.fs_cstotal.cs_ndir++;
28987c478bd9Sstevel@tonic-gate 		fscs[0].cs_ndir++;
28997c478bd9Sstevel@tonic-gate 	}
29007c478bd9Sstevel@tonic-gate 	cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
29017c478bd9Sstevel@tonic-gate 	cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--;
29027c478bd9Sstevel@tonic-gate 	if (size != sblock.fs_bsize) {
29037c478bd9Sstevel@tonic-gate 		frag = howmany(size, sblock.fs_fsize);
29047c478bd9Sstevel@tonic-gate 		fscs[0].cs_nffree += sblock.fs_frag - frag;
29057c478bd9Sstevel@tonic-gate 		sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
29067c478bd9Sstevel@tonic-gate 		acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
29077c478bd9Sstevel@tonic-gate 		acg.cg_frsum[sblock.fs_frag - frag]++;
29087c478bd9Sstevel@tonic-gate 		for (i = frag; i < sblock.fs_frag; i++)
29097c478bd9Sstevel@tonic-gate 			setbit(cg_blksfree(&acg), d + i);
29107c478bd9Sstevel@tonic-gate 	}
29117c478bd9Sstevel@tonic-gate 	wtfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, 0)), sblock.fs_cgsize,
29127c478bd9Sstevel@tonic-gate 	    (char *)&acg);
29137c478bd9Sstevel@tonic-gate 	return (d);
29147c478bd9Sstevel@tonic-gate }
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate /*
29177c478bd9Sstevel@tonic-gate  * Allocate an inode on the disk
29187c478bd9Sstevel@tonic-gate  */
29197c478bd9Sstevel@tonic-gate static void
iput(struct inode * ip)29207c478bd9Sstevel@tonic-gate iput(struct inode *ip)
29217c478bd9Sstevel@tonic-gate {
29227c478bd9Sstevel@tonic-gate 	struct dinode buf[MAXINOPB];
29237c478bd9Sstevel@tonic-gate 	diskaddr_t d;
29247c478bd9Sstevel@tonic-gate 
29257c478bd9Sstevel@tonic-gate 	rdfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, 0)), sblock.fs_cgsize,
29267c478bd9Sstevel@tonic-gate 	    (char *)&acg);
29277c478bd9Sstevel@tonic-gate 	if (acg.cg_magic != CG_MAGIC) {
29287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("cg 0: bad magic number\n"));
29297c478bd9Sstevel@tonic-gate 		lockexit(32);
29307c478bd9Sstevel@tonic-gate 	}
29317c478bd9Sstevel@tonic-gate 	acg.cg_cs.cs_nifree--;
29327c478bd9Sstevel@tonic-gate 	setbit(cg_inosused(&acg), ip->i_number);
29337c478bd9Sstevel@tonic-gate 	wtfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, 0)), sblock.fs_cgsize,
29347c478bd9Sstevel@tonic-gate 	    (char *)&acg);
29357c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_nifree--;
29367c478bd9Sstevel@tonic-gate 	fscs[0].cs_nifree--;
29377c478bd9Sstevel@tonic-gate 	if ((int)ip->i_number >= sblock.fs_ipg * sblock.fs_ncg) {
29387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2939d50c8f90Svsakar 		    gettext("fsinit: inode value out of range (%d).\n"),
2940d50c8f90Svsakar 		    ip->i_number);
29417c478bd9Sstevel@tonic-gate 		lockexit(32);
29427c478bd9Sstevel@tonic-gate 	}
29437c478bd9Sstevel@tonic-gate 	d = fsbtodb(&sblock, (uint64_t)itod(&sblock, (int)ip->i_number));
29447c478bd9Sstevel@tonic-gate 	rdfs(d, sblock.fs_bsize, (char *)buf);
29457c478bd9Sstevel@tonic-gate 	buf[itoo(&sblock, (int)ip->i_number)].di_ic = ip->i_ic;
29467c478bd9Sstevel@tonic-gate 	wtfs(d, sblock.fs_bsize, (char *)buf);
29477c478bd9Sstevel@tonic-gate }
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate /*
29507c478bd9Sstevel@tonic-gate  * getbuf()	-- Get a buffer for use in an AIO operation.  Buffer
29517c478bd9Sstevel@tonic-gate  *		is zero'd the first time returned, left with whatever
29527c478bd9Sstevel@tonic-gate  *		was in memory after that.  This function actually gets
29537c478bd9Sstevel@tonic-gate  *		enough memory the first time it's called to support
29547c478bd9Sstevel@tonic-gate  *		MAXBUF buffers like a slab allocator.  When all the
29557c478bd9Sstevel@tonic-gate  *		buffers are in use, it waits for an aio to complete
29567c478bd9Sstevel@tonic-gate  *		and make a buffer available.
29577c478bd9Sstevel@tonic-gate  *
29587c478bd9Sstevel@tonic-gate  *		Never returns an error.  Either succeeds or exits.
29597c478bd9Sstevel@tonic-gate  */
29607c478bd9Sstevel@tonic-gate static char *
getbuf(bufhdr * bufhead,int size)29617c478bd9Sstevel@tonic-gate getbuf(bufhdr *bufhead, int size)
29627c478bd9Sstevel@tonic-gate {
29637c478bd9Sstevel@tonic-gate 	bufhdr *pbuf;
29647c478bd9Sstevel@tonic-gate 	bufhdr *prev;
29657c478bd9Sstevel@tonic-gate 	int i;
29667c478bd9Sstevel@tonic-gate 	int buf_size, max_bufs;
29677c478bd9Sstevel@tonic-gate 
29687c478bd9Sstevel@tonic-gate 	/*
29697c478bd9Sstevel@tonic-gate 	 * Initialize all the buffers
29707c478bd9Sstevel@tonic-gate 	 */
29717c478bd9Sstevel@tonic-gate 	if (bufhead->head == NULL) {
29727c478bd9Sstevel@tonic-gate 		/*
29737c478bd9Sstevel@tonic-gate 		 * round up the size of our buffer header to a
29747c478bd9Sstevel@tonic-gate 		 * 16 byte boundary so the address we return to
29757c478bd9Sstevel@tonic-gate 		 * the caller is "suitably aligned".
29767c478bd9Sstevel@tonic-gate 		 */
29777c478bd9Sstevel@tonic-gate 		bufhdrsize = (sizeof (bufhdr) + 15) & ~15;
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate 		/*
29807c478bd9Sstevel@tonic-gate 		 * Add in our header to the buffer and round it all up to
29817c478bd9Sstevel@tonic-gate 		 * a 16 byte boundry so each member of the slab is aligned.
29827c478bd9Sstevel@tonic-gate 		 */
29837c478bd9Sstevel@tonic-gate 		buf_size = (size + bufhdrsize + 15) & ~15;
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 		/*
29867c478bd9Sstevel@tonic-gate 		 * Limit number of buffers to lesser of MAXBUFMEM's worth
29877c478bd9Sstevel@tonic-gate 		 * or MAXBUF, whichever is less.
29887c478bd9Sstevel@tonic-gate 		 */
29897c478bd9Sstevel@tonic-gate 		max_bufs = MAXBUFMEM / buf_size;
29907c478bd9Sstevel@tonic-gate 		if (max_bufs > MAXBUF)
29917c478bd9Sstevel@tonic-gate 			max_bufs = MAXBUF;
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 		pbuf = (bufhdr *)calloc(max_bufs, buf_size);
29947c478bd9Sstevel@tonic-gate 		if (pbuf == NULL) {
29957c478bd9Sstevel@tonic-gate 			perror("calloc");
29967c478bd9Sstevel@tonic-gate 			lockexit(32);
29977c478bd9Sstevel@tonic-gate 		}
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate 		bufhead->head = bufhead;
30007c478bd9Sstevel@tonic-gate 		prev = bufhead;
30017c478bd9Sstevel@tonic-gate 		for (i = 0; i < max_bufs; i++) {
30027c478bd9Sstevel@tonic-gate 			pbuf->head = bufhead;
30037c478bd9Sstevel@tonic-gate 			prev->next = pbuf;
30047c478bd9Sstevel@tonic-gate 			prev = pbuf;
30057c478bd9Sstevel@tonic-gate 			pbuf = (bufhdr *)((char *)pbuf + buf_size);
30067c478bd9Sstevel@tonic-gate 		}
30077c478bd9Sstevel@tonic-gate 	}
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate 	/*
30107c478bd9Sstevel@tonic-gate 	 * Get an available buffer, waiting for I/O if necessary
30117c478bd9Sstevel@tonic-gate 	 */
30127c478bd9Sstevel@tonic-gate 	wait_for_write(NOBLOCK);
30137c478bd9Sstevel@tonic-gate 	while (bufhead->next == NULL)
30147c478bd9Sstevel@tonic-gate 		wait_for_write(BLOCK);
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 	/*
30177c478bd9Sstevel@tonic-gate 	 * Take the buffer off the list
30187c478bd9Sstevel@tonic-gate 	 */
30197c478bd9Sstevel@tonic-gate 	pbuf = bufhead->next;
30207c478bd9Sstevel@tonic-gate 	bufhead->next = pbuf->next;
30217c478bd9Sstevel@tonic-gate 	pbuf->next = NULL;
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 	/*
30247c478bd9Sstevel@tonic-gate 	 * return the empty buffer space just past the header
30257c478bd9Sstevel@tonic-gate 	 */
30267c478bd9Sstevel@tonic-gate 	return ((char *)pbuf + bufhdrsize);
30277c478bd9Sstevel@tonic-gate }
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate /*
30307c478bd9Sstevel@tonic-gate  * freebuf()	-- Free a buffer gotten previously through getbuf.
30317c478bd9Sstevel@tonic-gate  *		Puts the buffer back on the appropriate list for
30327c478bd9Sstevel@tonic-gate  *		later use.  Never calls free().
30337c478bd9Sstevel@tonic-gate  *
30347c478bd9Sstevel@tonic-gate  * Assumes that SIGINT is blocked.
30357c478bd9Sstevel@tonic-gate  */
30367c478bd9Sstevel@tonic-gate static void
freebuf(char * buf)30377c478bd9Sstevel@tonic-gate freebuf(char *buf)
30387c478bd9Sstevel@tonic-gate {
30397c478bd9Sstevel@tonic-gate 	bufhdr *pbuf;
30407c478bd9Sstevel@tonic-gate 	bufhdr *bufhead;
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 	/*
30437c478bd9Sstevel@tonic-gate 	 * get the header for this buffer
30447c478bd9Sstevel@tonic-gate 	 */
30457c478bd9Sstevel@tonic-gate 	pbuf = (bufhdr *)(buf - bufhdrsize);
30467c478bd9Sstevel@tonic-gate 
30477c478bd9Sstevel@tonic-gate 	/*
30487c478bd9Sstevel@tonic-gate 	 * Put it back on the list of available buffers
30497c478bd9Sstevel@tonic-gate 	 */
30507c478bd9Sstevel@tonic-gate 	bufhead = pbuf->head;
30517c478bd9Sstevel@tonic-gate 	pbuf->next = bufhead->next;
30527c478bd9Sstevel@tonic-gate 	bufhead->next = pbuf;
30537c478bd9Sstevel@tonic-gate }
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate /*
30567c478bd9Sstevel@tonic-gate  * freetrans()	-- Free a transaction gotten previously through getaiop.
30577c478bd9Sstevel@tonic-gate  *		Puts the transaction struct back on the appropriate list for
30587c478bd9Sstevel@tonic-gate  *		later use.  Never calls free().
30597c478bd9Sstevel@tonic-gate  *
30607c478bd9Sstevel@tonic-gate  * Assumes that SIGINT is blocked.
30617c478bd9Sstevel@tonic-gate  */
30627c478bd9Sstevel@tonic-gate static void
freetrans(aio_trans * transp)30637c478bd9Sstevel@tonic-gate freetrans(aio_trans *transp)
30647c478bd9Sstevel@tonic-gate {
30657c478bd9Sstevel@tonic-gate 	/*
30667c478bd9Sstevel@tonic-gate 	 * free the buffer associated with this AIO if needed
30677c478bd9Sstevel@tonic-gate 	 */
30687c478bd9Sstevel@tonic-gate 	if (transp->release == RELEASE)
30697c478bd9Sstevel@tonic-gate 		freebuf(transp->buffer);
30707c478bd9Sstevel@tonic-gate 
30717c478bd9Sstevel@tonic-gate 	/*
30727c478bd9Sstevel@tonic-gate 	 * Put transaction on the free list
30737c478bd9Sstevel@tonic-gate 	 */
30747c478bd9Sstevel@tonic-gate 	transp->next = results.trans;
30757c478bd9Sstevel@tonic-gate 	results.trans = transp;
30767c478bd9Sstevel@tonic-gate }
30777c478bd9Sstevel@tonic-gate 
30787c478bd9Sstevel@tonic-gate /*
30797c478bd9Sstevel@tonic-gate  * wait_for_write()	-- Wait for an aio write to complete.  Return
30807c478bd9Sstevel@tonic-gate  *			the transaction structure for that write.
30817c478bd9Sstevel@tonic-gate  *
30827c478bd9Sstevel@tonic-gate  * Blocks SIGINT if necessary.
30837c478bd9Sstevel@tonic-gate  */
30847c478bd9Sstevel@tonic-gate aio_trans *
wait_for_write(int block)30857c478bd9Sstevel@tonic-gate wait_for_write(int block)
30867c478bd9Sstevel@tonic-gate {
30877c478bd9Sstevel@tonic-gate 	aio_trans	*transp;
30887c478bd9Sstevel@tonic-gate 	aio_result_t	*resultp;
30897c478bd9Sstevel@tonic-gate 	static struct timeval  zero_wait = { 0, 0 };
30907c478bd9Sstevel@tonic-gate 	sigset_t	old_mask;
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate 	/*
30937c478bd9Sstevel@tonic-gate 	 * If we know there aren't any outstanding transactions, just return
30947c478bd9Sstevel@tonic-gate 	 */
30957c478bd9Sstevel@tonic-gate 	if (results.outstanding == 0)
30967c478bd9Sstevel@tonic-gate 		return ((aio_trans *) 0);
30977c478bd9Sstevel@tonic-gate 
30987c478bd9Sstevel@tonic-gate 	block_sigint(&old_mask);
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	resultp = aiowait(block ? NULL : &zero_wait);
31017c478bd9Sstevel@tonic-gate 	if (resultp == NULL ||
31027c478bd9Sstevel@tonic-gate 	    (resultp == (aio_result_t *)-1 && errno == EINVAL)) {
31037c478bd9Sstevel@tonic-gate 		unblock_sigint(&old_mask);
31047c478bd9Sstevel@tonic-gate 		return ((aio_trans *) 0);
31057c478bd9Sstevel@tonic-gate 	}
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 	results.outstanding--;
31087c478bd9Sstevel@tonic-gate 	transp = (aio_trans *)resultp;
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate 	if (resultp->aio_return != transp->size) {
31117c478bd9Sstevel@tonic-gate 		if (resultp->aio_return == -1) {
31127c478bd9Sstevel@tonic-gate 			/*
31137c478bd9Sstevel@tonic-gate 			 * The aiowrite() may have failed because the
31147c478bd9Sstevel@tonic-gate 			 * kernel didn't have enough memory to do the job.
31157c478bd9Sstevel@tonic-gate 			 * Flush all pending writes and try a normal
31167c478bd9Sstevel@tonic-gate 			 * write().  wtfs_breakup() will call exit if it
31177c478bd9Sstevel@tonic-gate 			 * fails, so we don't worry about errors here.
31187c478bd9Sstevel@tonic-gate 			 */
31197c478bd9Sstevel@tonic-gate 			flush_writes();
31207c478bd9Sstevel@tonic-gate 			wtfs_breakup(transp->bno, transp->size, transp->buffer);
31217c478bd9Sstevel@tonic-gate 		} else {
31227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
31237c478bd9Sstevel@tonic-gate 			    "short write (%d of %d bytes) on sector %lld\n"),
31247c478bd9Sstevel@tonic-gate 			    resultp->aio_return, transp->size,
31257c478bd9Sstevel@tonic-gate 			    transp->bno);
31267c478bd9Sstevel@tonic-gate 			/*
31277c478bd9Sstevel@tonic-gate 			 * Don't unblock SIGINT, to avoid potential
31287c478bd9Sstevel@tonic-gate 			 * looping due to queued interrupts and
31297c478bd9Sstevel@tonic-gate 			 * error handling.
31307c478bd9Sstevel@tonic-gate 			 */
31317c478bd9Sstevel@tonic-gate 			lockexit(32);
31327c478bd9Sstevel@tonic-gate 		}
31337c478bd9Sstevel@tonic-gate 	}
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate 	resultp->aio_return = 0;
31367c478bd9Sstevel@tonic-gate 	freetrans(transp);
31377c478bd9Sstevel@tonic-gate 	unblock_sigint(&old_mask);
31387c478bd9Sstevel@tonic-gate 	return (transp);
31397c478bd9Sstevel@tonic-gate }
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate /*
31427c478bd9Sstevel@tonic-gate  * flush_writes()	-- flush all the outstanding aio writes.
31437c478bd9Sstevel@tonic-gate  */
31447c478bd9Sstevel@tonic-gate static void
flush_writes(void)31457c478bd9Sstevel@tonic-gate flush_writes(void)
31467c478bd9Sstevel@tonic-gate {
31477c478bd9Sstevel@tonic-gate 	while (wait_for_write(BLOCK))
31487c478bd9Sstevel@tonic-gate 		;
31497c478bd9Sstevel@tonic-gate }
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate /*
31527c478bd9Sstevel@tonic-gate  * get_aiop()	-- find and return an aio_trans structure on which a new
31537c478bd9Sstevel@tonic-gate  *		aio can be done.  Blocks on aiowait() if needed.  Reaps
31547c478bd9Sstevel@tonic-gate  *		all outstanding completed aio's.
31557c478bd9Sstevel@tonic-gate  *
31567c478bd9Sstevel@tonic-gate  * Assumes that SIGINT is blocked.
31577c478bd9Sstevel@tonic-gate  */
31587c478bd9Sstevel@tonic-gate aio_trans *
get_aiop()31597c478bd9Sstevel@tonic-gate get_aiop()
31607c478bd9Sstevel@tonic-gate {
31617c478bd9Sstevel@tonic-gate 	int i;
31627c478bd9Sstevel@tonic-gate 	aio_trans *transp;
31637c478bd9Sstevel@tonic-gate 	aio_trans *prev;
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 	/*
31667c478bd9Sstevel@tonic-gate 	 * initialize aio stuff
31677c478bd9Sstevel@tonic-gate 	 */
31687c478bd9Sstevel@tonic-gate 	if (!aio_inited) {
31697c478bd9Sstevel@tonic-gate 		aio_inited = 1;
31707c478bd9Sstevel@tonic-gate 
31717c478bd9Sstevel@tonic-gate 		results.maxpend = 0;
31727c478bd9Sstevel@tonic-gate 		results.outstanding = 0;
31737c478bd9Sstevel@tonic-gate 		results.max = MAXAIO;
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 		results.trans = (aio_trans *)calloc(results.max,
3176d50c8f90Svsakar 		    sizeof (aio_trans));
31777c478bd9Sstevel@tonic-gate 		if (results.trans == NULL) {
31787c478bd9Sstevel@tonic-gate 			perror("calloc");
31797c478bd9Sstevel@tonic-gate 			lockexit(32);
31807c478bd9Sstevel@tonic-gate 		}
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate 		/*
31837c478bd9Sstevel@tonic-gate 		 * Initialize the linked list of aio transaction
31847c478bd9Sstevel@tonic-gate 		 * structures.  Note that the final "next" pointer
31857c478bd9Sstevel@tonic-gate 		 * will be NULL since we got the buffer from calloc().
31867c478bd9Sstevel@tonic-gate 		 */
31877c478bd9Sstevel@tonic-gate 		prev = results.trans;
31887c478bd9Sstevel@tonic-gate 		for (i = 1; i < results.max; i++) {
31897c478bd9Sstevel@tonic-gate 			prev->next = &(results.trans[i]);
31907c478bd9Sstevel@tonic-gate 			prev = prev->next;
31917c478bd9Sstevel@tonic-gate 		}
31927c478bd9Sstevel@tonic-gate 	}
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 	wait_for_write(NOBLOCK);
31957c478bd9Sstevel@tonic-gate 	while (results.trans == NULL)
31967c478bd9Sstevel@tonic-gate 		wait_for_write(BLOCK);
31977c478bd9Sstevel@tonic-gate 	transp = results.trans;
31987c478bd9Sstevel@tonic-gate 	results.trans = results.trans->next;
31997c478bd9Sstevel@tonic-gate 
32007c478bd9Sstevel@tonic-gate 	transp->next = 0;
32017c478bd9Sstevel@tonic-gate 	transp->resultbuf.aio_return = AIO_INPROGRESS;
32027c478bd9Sstevel@tonic-gate 	return (transp);
32037c478bd9Sstevel@tonic-gate }
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate /*
32067c478bd9Sstevel@tonic-gate  * read a block from the file system
32077c478bd9Sstevel@tonic-gate  */
32087c478bd9Sstevel@tonic-gate static void
rdfs(diskaddr_t bno,int size,char * bf)32097c478bd9Sstevel@tonic-gate rdfs(diskaddr_t bno, int size, char *bf)
32107c478bd9Sstevel@tonic-gate {
32117c478bd9Sstevel@tonic-gate 	int n, saverr;
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 	/*
32147c478bd9Sstevel@tonic-gate 	 * In case we need any data that's pending in an aiowrite(),
32157c478bd9Sstevel@tonic-gate 	 * we wait for them all to complete before doing a read.
32167c478bd9Sstevel@tonic-gate 	 */
32177c478bd9Sstevel@tonic-gate 	flush_writes();
32187c478bd9Sstevel@tonic-gate 
32197c478bd9Sstevel@tonic-gate 	/*
32207c478bd9Sstevel@tonic-gate 	 * Note: the llseek() can succeed, even if the offset is out of range.
32217c478bd9Sstevel@tonic-gate 	 * It's not until the file i/o operation (the read()) that one knows
32227c478bd9Sstevel@tonic-gate 	 * for sure if the raw device can handle the offset.
32237c478bd9Sstevel@tonic-gate 	 */
32247c478bd9Sstevel@tonic-gate 	if (llseek(fsi, (offset_t)bno * sectorsize, 0) < 0) {
32257c478bd9Sstevel@tonic-gate 		saverr = errno;
32267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
32277c478bd9Sstevel@tonic-gate 		    gettext("seek error on sector %lld: %s\n"),
32287c478bd9Sstevel@tonic-gate 		    bno, strerror(saverr));
32297c478bd9Sstevel@tonic-gate 		lockexit(32);
32307c478bd9Sstevel@tonic-gate 	}
32317c478bd9Sstevel@tonic-gate 	n = read(fsi, bf, size);
32327c478bd9Sstevel@tonic-gate 	if (n != size) {
32337c478bd9Sstevel@tonic-gate 		saverr = errno;
32347c478bd9Sstevel@tonic-gate 		if (n == -1)
32357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
32367c478bd9Sstevel@tonic-gate 			    gettext("read error on sector %lld: %s\n"),
32377c478bd9Sstevel@tonic-gate 			    bno, strerror(saverr));
32387c478bd9Sstevel@tonic-gate 		else
32397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
32407c478bd9Sstevel@tonic-gate 			    "short read (%d of %d bytes) on sector %lld\n"),
32417c478bd9Sstevel@tonic-gate 			    n, size, bno);
32427c478bd9Sstevel@tonic-gate 		lockexit(32);
32437c478bd9Sstevel@tonic-gate 	}
32447c478bd9Sstevel@tonic-gate }
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate /*
32477c478bd9Sstevel@tonic-gate  * write a block to the file system
32487c478bd9Sstevel@tonic-gate  */
32497c478bd9Sstevel@tonic-gate static void
wtfs(diskaddr_t bno,int size,char * bf)32507c478bd9Sstevel@tonic-gate wtfs(diskaddr_t bno, int size, char *bf)
32517c478bd9Sstevel@tonic-gate {
32527c478bd9Sstevel@tonic-gate 	int n, saverr;
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate 	if (fso == -1)
32557c478bd9Sstevel@tonic-gate 		return;
32567c478bd9Sstevel@tonic-gate 
32577c478bd9Sstevel@tonic-gate 	/*
32587c478bd9Sstevel@tonic-gate 	 * Note: the llseek() can succeed, even if the offset is out of range.
32597c478bd9Sstevel@tonic-gate 	 * It's not until the file i/o operation (the write()) that one knows
32607c478bd9Sstevel@tonic-gate 	 * for sure if the raw device can handle the offset.
32617c478bd9Sstevel@tonic-gate 	 */
32627c478bd9Sstevel@tonic-gate 	if (llseek(fso, (offset_t)bno * sectorsize, 0) < 0) {
32637c478bd9Sstevel@tonic-gate 		saverr = errno;
32647c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
32657c478bd9Sstevel@tonic-gate 		    gettext("seek error on sector %lld: %s\n"),
32667c478bd9Sstevel@tonic-gate 		    bno, strerror(saverr));
32677c478bd9Sstevel@tonic-gate 		lockexit(32);
32687c478bd9Sstevel@tonic-gate 	}
32697c478bd9Sstevel@tonic-gate 	if (Nflag)
32707c478bd9Sstevel@tonic-gate 		return;
32717c478bd9Sstevel@tonic-gate 	n = write(fso, bf, size);
32727c478bd9Sstevel@tonic-gate 	if (n != size) {
32737c478bd9Sstevel@tonic-gate 		saverr = errno;
32747c478bd9Sstevel@tonic-gate 		if (n == -1)
32757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
32767c478bd9Sstevel@tonic-gate 			    gettext("write error on sector %lld: %s\n"),
32777c478bd9Sstevel@tonic-gate 			    bno, strerror(saverr));
32787c478bd9Sstevel@tonic-gate 		else
32797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
32807c478bd9Sstevel@tonic-gate 			    "short write (%d of %d bytes) on sector %lld\n"),
32817c478bd9Sstevel@tonic-gate 			    n, size, bno);
32827c478bd9Sstevel@tonic-gate 		lockexit(32);
32837c478bd9Sstevel@tonic-gate 	}
32847c478bd9Sstevel@tonic-gate }
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate /*
32877c478bd9Sstevel@tonic-gate  * write a block to the file system -- buffered with aio
32887c478bd9Sstevel@tonic-gate  */
32897c478bd9Sstevel@tonic-gate static void
awtfs(diskaddr_t bno,int size,char * bf,int release)32907c478bd9Sstevel@tonic-gate awtfs(diskaddr_t bno, int size, char *bf, int release)
32917c478bd9Sstevel@tonic-gate {
32927c478bd9Sstevel@tonic-gate 	int n;
3293fe12dc75SToomas Soome 	aio_trans	*transp;
3294fe12dc75SToomas Soome 	sigset_t	old_mask;
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 	if (fso == -1)
32977c478bd9Sstevel@tonic-gate 		return;
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 	/*
33007c478bd9Sstevel@tonic-gate 	 * We need to keep things consistent if we get interrupted,
33017c478bd9Sstevel@tonic-gate 	 * so defer any expected interrupts for the time being.
33027c478bd9Sstevel@tonic-gate 	 */
33037c478bd9Sstevel@tonic-gate 	block_sigint(&old_mask);
33047c478bd9Sstevel@tonic-gate 
33057c478bd9Sstevel@tonic-gate 	if (Nflag) {
33067c478bd9Sstevel@tonic-gate 		if (release == RELEASE)
33077c478bd9Sstevel@tonic-gate 			freebuf(bf);
33087c478bd9Sstevel@tonic-gate 	} else {
33097c478bd9Sstevel@tonic-gate 		transp = get_aiop();
33107c478bd9Sstevel@tonic-gate 		transp->bno = bno;
33117c478bd9Sstevel@tonic-gate 		transp->buffer = bf;
33127c478bd9Sstevel@tonic-gate 		transp->size = size;
33137c478bd9Sstevel@tonic-gate 		transp->release = release;
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate 		n = aiowrite(fso, bf, size, (off_t)bno * sectorsize,
3316d50c8f90Svsakar 		    SEEK_SET, &transp->resultbuf);
33177c478bd9Sstevel@tonic-gate 
33187c478bd9Sstevel@tonic-gate 		if (n < 0) {
33197c478bd9Sstevel@tonic-gate 			/*
33207c478bd9Sstevel@tonic-gate 			 * The aiowrite() may have failed because the
33217c478bd9Sstevel@tonic-gate 			 * kernel didn't have enough memory to do the job.
33227c478bd9Sstevel@tonic-gate 			 * Flush all pending writes and try a normal
33237c478bd9Sstevel@tonic-gate 			 * write().  wtfs_breakup() will call exit if it
33247c478bd9Sstevel@tonic-gate 			 * fails, so we don't worry about errors here.
33257c478bd9Sstevel@tonic-gate 			 */
33267c478bd9Sstevel@tonic-gate 			flush_writes();
33277c478bd9Sstevel@tonic-gate 			wtfs_breakup(transp->bno, transp->size, transp->buffer);
33287c478bd9Sstevel@tonic-gate 			freetrans(transp);
33297c478bd9Sstevel@tonic-gate 		} else {
33307c478bd9Sstevel@tonic-gate 			/*
33317c478bd9Sstevel@tonic-gate 			 * Keep track of our pending writes.
33327c478bd9Sstevel@tonic-gate 			 */
33337c478bd9Sstevel@tonic-gate 			results.outstanding++;
33347c478bd9Sstevel@tonic-gate 			if (results.outstanding > results.maxpend)
3335d50c8f90Svsakar 				results.maxpend = results.outstanding;
33367c478bd9Sstevel@tonic-gate 		}
33377c478bd9Sstevel@tonic-gate 	}
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 	unblock_sigint(&old_mask);
33407c478bd9Sstevel@tonic-gate }
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate /*
33447c478bd9Sstevel@tonic-gate  * write a block to the file system, but break it up into sbsize
33457c478bd9Sstevel@tonic-gate  * chunks to avoid forcing a large amount of memory to be locked down.
33467c478bd9Sstevel@tonic-gate  * Only used as a fallback when an aio write has failed.
33477c478bd9Sstevel@tonic-gate  */
33487c478bd9Sstevel@tonic-gate static void
wtfs_breakup(diskaddr_t bno,int size,char * bf)33497c478bd9Sstevel@tonic-gate wtfs_breakup(diskaddr_t bno, int size, char *bf)
33507c478bd9Sstevel@tonic-gate {
33517c478bd9Sstevel@tonic-gate 	int n, saverr;
33527c478bd9Sstevel@tonic-gate 	int wsize;
33537c478bd9Sstevel@tonic-gate 	int block_incr = sbsize / sectorsize;
33547c478bd9Sstevel@tonic-gate 
33557c478bd9Sstevel@tonic-gate 	if (size < sbsize)
33567c478bd9Sstevel@tonic-gate 		wsize = size;
33577c478bd9Sstevel@tonic-gate 	else
33587c478bd9Sstevel@tonic-gate 		wsize = sbsize;
33597c478bd9Sstevel@tonic-gate 
33607c478bd9Sstevel@tonic-gate 	n = 0;
33617c478bd9Sstevel@tonic-gate 	while (size) {
33627c478bd9Sstevel@tonic-gate 		/*
33637c478bd9Sstevel@tonic-gate 		 * Note: the llseek() can succeed, even if the offset is
33647c478bd9Sstevel@tonic-gate 		 * out of range.  It's not until the file i/o operation
33657c478bd9Sstevel@tonic-gate 		 * (the write()) that one knows for sure if the raw device
33667c478bd9Sstevel@tonic-gate 		 * can handle the offset.
33677c478bd9Sstevel@tonic-gate 		 */
33687c478bd9Sstevel@tonic-gate 		if (llseek(fso, (offset_t)bno * sectorsize, 0) < 0) {
33697c478bd9Sstevel@tonic-gate 			saverr = errno;
33707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
33717c478bd9Sstevel@tonic-gate 			    gettext("seek error on sector %lld: %s\n"),
33727c478bd9Sstevel@tonic-gate 			    bno, strerror(saverr));
33737c478bd9Sstevel@tonic-gate 			lockexit(32);
33747c478bd9Sstevel@tonic-gate 		}
33757c478bd9Sstevel@tonic-gate 
33767c478bd9Sstevel@tonic-gate 		n = write(fso, bf, wsize);
33777c478bd9Sstevel@tonic-gate 		if (n == -1) {
33787c478bd9Sstevel@tonic-gate 			saverr = errno;
33797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
33807c478bd9Sstevel@tonic-gate 			    gettext("write error on sector %lld: %s\n"),
33817c478bd9Sstevel@tonic-gate 			    bno, strerror(saverr));
33827c478bd9Sstevel@tonic-gate 			lockexit(32);
33837c478bd9Sstevel@tonic-gate 		}
33847c478bd9Sstevel@tonic-gate 		if (n != wsize) {
33857c478bd9Sstevel@tonic-gate 			saverr = errno;
33867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
33877c478bd9Sstevel@tonic-gate 			    "short write (%d of %d bytes) on sector %lld\n"),
33887c478bd9Sstevel@tonic-gate 			    n, size, bno);
33897c478bd9Sstevel@tonic-gate 			lockexit(32);
33907c478bd9Sstevel@tonic-gate 		}
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 		bno += block_incr;
33937c478bd9Sstevel@tonic-gate 		bf += wsize;
33947c478bd9Sstevel@tonic-gate 		size -= wsize;
33957c478bd9Sstevel@tonic-gate 		if (size < wsize)
33967c478bd9Sstevel@tonic-gate 			wsize = size;
33977c478bd9Sstevel@tonic-gate 	}
33987c478bd9Sstevel@tonic-gate }
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate /*
34027c478bd9Sstevel@tonic-gate  * check if a block is available
34037c478bd9Sstevel@tonic-gate  */
34047c478bd9Sstevel@tonic-gate static int
isblock(struct fs * fs,unsigned char * cp,int h)34057c478bd9Sstevel@tonic-gate isblock(struct fs *fs, unsigned char *cp, int h)
34067c478bd9Sstevel@tonic-gate {
34077c478bd9Sstevel@tonic-gate 	unsigned char mask;
34087c478bd9Sstevel@tonic-gate 
34097c478bd9Sstevel@tonic-gate 	switch (fs->fs_frag) {
34107c478bd9Sstevel@tonic-gate 	case 8:
34117c478bd9Sstevel@tonic-gate 		return (cp[h] == 0xff);
34127c478bd9Sstevel@tonic-gate 	case 4:
34137c478bd9Sstevel@tonic-gate 		mask = 0x0f << ((h & 0x1) << 2);
34147c478bd9Sstevel@tonic-gate 		return ((cp[h >> 1] & mask) == mask);
34157c478bd9Sstevel@tonic-gate 	case 2:
34167c478bd9Sstevel@tonic-gate 		mask = 0x03 << ((h & 0x3) << 1);
34177c478bd9Sstevel@tonic-gate 		return ((cp[h >> 2] & mask) == mask);
34187c478bd9Sstevel@tonic-gate 	case 1:
34197c478bd9Sstevel@tonic-gate 		mask = 0x01 << (h & 0x7);
34207c478bd9Sstevel@tonic-gate 		return ((cp[h >> 3] & mask) == mask);
34217c478bd9Sstevel@tonic-gate 	default:
34227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
34237c478bd9Sstevel@tonic-gate 		return (0);
34247c478bd9Sstevel@tonic-gate 	}
34257c478bd9Sstevel@tonic-gate }
34267c478bd9Sstevel@tonic-gate 
34277c478bd9Sstevel@tonic-gate /*
34287c478bd9Sstevel@tonic-gate  * take a block out of the map
34297c478bd9Sstevel@tonic-gate  */
34307c478bd9Sstevel@tonic-gate static void
clrblock(struct fs * fs,unsigned char * cp,int h)34317c478bd9Sstevel@tonic-gate clrblock(struct fs *fs, unsigned char *cp, int h)
34327c478bd9Sstevel@tonic-gate {
34337c478bd9Sstevel@tonic-gate 	switch ((fs)->fs_frag) {
34347c478bd9Sstevel@tonic-gate 	case 8:
34357c478bd9Sstevel@tonic-gate 		cp[h] = 0;
34367c478bd9Sstevel@tonic-gate 		return;
34377c478bd9Sstevel@tonic-gate 	case 4:
34387c478bd9Sstevel@tonic-gate 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
34397c478bd9Sstevel@tonic-gate 		return;
34407c478bd9Sstevel@tonic-gate 	case 2:
34417c478bd9Sstevel@tonic-gate 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
34427c478bd9Sstevel@tonic-gate 		return;
34437c478bd9Sstevel@tonic-gate 	case 1:
34447c478bd9Sstevel@tonic-gate 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
34457c478bd9Sstevel@tonic-gate 		return;
34467c478bd9Sstevel@tonic-gate 	default:
34477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
34487c478bd9Sstevel@tonic-gate 		    gettext("clrblock: bad fs_frag value %d\n"), fs->fs_frag);
34497c478bd9Sstevel@tonic-gate 		return;
34507c478bd9Sstevel@tonic-gate 	}
34517c478bd9Sstevel@tonic-gate }
34527c478bd9Sstevel@tonic-gate 
34537c478bd9Sstevel@tonic-gate /*
34547c478bd9Sstevel@tonic-gate  * put a block into the map
34557c478bd9Sstevel@tonic-gate  */
34567c478bd9Sstevel@tonic-gate static void
setblock(struct fs * fs,unsigned char * cp,int h)34577c478bd9Sstevel@tonic-gate setblock(struct fs *fs, unsigned char *cp, int h)
34587c478bd9Sstevel@tonic-gate {
34597c478bd9Sstevel@tonic-gate 	switch (fs->fs_frag) {
34607c478bd9Sstevel@tonic-gate 	case 8:
34617c478bd9Sstevel@tonic-gate 		cp[h] = 0xff;
34627c478bd9Sstevel@tonic-gate 		return;
34637c478bd9Sstevel@tonic-gate 	case 4:
34647c478bd9Sstevel@tonic-gate 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
34657c478bd9Sstevel@tonic-gate 		return;
34667c478bd9Sstevel@tonic-gate 	case 2:
34677c478bd9Sstevel@tonic-gate 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
34687c478bd9Sstevel@tonic-gate 		return;
34697c478bd9Sstevel@tonic-gate 	case 1:
34707c478bd9Sstevel@tonic-gate 		cp[h >> 3] |= (0x01 << (h & 0x7));
34717c478bd9Sstevel@tonic-gate 		return;
34727c478bd9Sstevel@tonic-gate 	default:
34737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
34747c478bd9Sstevel@tonic-gate 		    gettext("setblock: bad fs_frag value %d\n"), fs->fs_frag);
34757c478bd9Sstevel@tonic-gate 		return;
34767c478bd9Sstevel@tonic-gate 	}
34777c478bd9Sstevel@tonic-gate }
34787c478bd9Sstevel@tonic-gate 
34797c478bd9Sstevel@tonic-gate static void
usage(void)3480f45bbb21SToomas Soome usage(void)
34817c478bd9Sstevel@tonic-gate {
34827c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
34837c478bd9Sstevel@tonic-gate 	    gettext("ufs usage: mkfs [-F FSType] [-V] [-m] [-o options] "
3484d50c8f90Svsakar 	    "special "				/* param 0 */
3485d50c8f90Svsakar 	    "size(sectors) \\ \n"));		/* param 1 */
34867c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3487d50c8f90Svsakar 	    "[nsect "				/* param 2 */
3488d50c8f90Svsakar 	    "ntrack "				/* param 3 */
3489d50c8f90Svsakar 	    "bsize "				/* param 4 */
3490d50c8f90Svsakar 	    "fragsize "				/* param 5 */
3491d50c8f90Svsakar 	    "cpg "				/* param 6 */
3492d50c8f90Svsakar 	    "free "				/* param 7 */
3493d50c8f90Svsakar 	    "rps "				/* param 8 */
3494d50c8f90Svsakar 	    "nbpi "				/* param 9 */
3495d50c8f90Svsakar 	    "opt "				/* param 10 */
3496d50c8f90Svsakar 	    "apc "				/* param 11 */
3497d50c8f90Svsakar 	    "gap "				/* param 12 */
3498d50c8f90Svsakar 	    "nrpos "				/* param 13 */
3499d50c8f90Svsakar 	    "maxcontig "			/* param 14 */
3500d50c8f90Svsakar 	    "mtb]\n");				/* param 15 */
35017c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3502d50c8f90Svsakar 	    gettext(" -m : dump fs cmd line used to make this partition\n"
3503d50c8f90Svsakar 	    " -V :print this command line and return\n"
3504d50c8f90Svsakar 	    " -o :ufs options: :nsect=%d,ntrack=%d,bsize=%d,fragsize=%d\n"
3505d50c8f90Svsakar 	    " -o :ufs options: :cgsize=%d,free=%d,rps=%d,nbpi=%d,opt=%c\n"
3506d50c8f90Svsakar 	    " -o :ufs options: :apc=%d,gap=%d,nrpos=%d,maxcontig=%d\n"
3507d50c8f90Svsakar 	    " -o :ufs options: :mtb=%c,calcsb,calcbinsb\n"
35087c478bd9Sstevel@tonic-gate "NOTE that all -o suboptions: must be separated only by commas so as to\n"
35097c478bd9Sstevel@tonic-gate "be parsed as a single argument\n"),
3510d50c8f90Svsakar 	    nsect, ntrack, bsize, fragsize, cpg, sblock.fs_minfree, rps,
3511d50c8f90Svsakar 	    nbpi, opt, apc, (rotdelay == -1) ? 0 : rotdelay,
3512d50c8f90Svsakar 	    sblock.fs_nrpos, maxcontig, mtb);
35137c478bd9Sstevel@tonic-gate 	lockexit(32);
35147c478bd9Sstevel@tonic-gate }
35157c478bd9Sstevel@tonic-gate 
35167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
35177c478bd9Sstevel@tonic-gate static void
dump_fscmd(char * fsys,int fsi)35187c478bd9Sstevel@tonic-gate dump_fscmd(char *fsys, int fsi)
35197c478bd9Sstevel@tonic-gate {
35207c478bd9Sstevel@tonic-gate 	int64_t used, bpcg, inospercg;
35217c478bd9Sstevel@tonic-gate 	int64_t nbpi;
35227c478bd9Sstevel@tonic-gate 	uint64_t nbytes64;
35237c478bd9Sstevel@tonic-gate 
35247c478bd9Sstevel@tonic-gate 	bzero((char *)&sblock, sizeof (sblock));
35257c478bd9Sstevel@tonic-gate 	rdfs((diskaddr_t)SBLOCK, SBSIZE, (char *)&sblock);
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 	/*
35287c478bd9Sstevel@tonic-gate 	 * ensure a valid file system and if not, exit with error or else
35297c478bd9Sstevel@tonic-gate 	 * we will end up computing block numbers etc and dividing by zero
35307c478bd9Sstevel@tonic-gate 	 * which will cause floating point errors in this routine.
35317c478bd9Sstevel@tonic-gate 	 */
35327c478bd9Sstevel@tonic-gate 
35337c478bd9Sstevel@tonic-gate 	if ((sblock.fs_magic != FS_MAGIC) &&
35347c478bd9Sstevel@tonic-gate 	    (sblock.fs_magic != MTB_UFS_MAGIC)) {
3535d50c8f90Svsakar 		(void) fprintf(stderr, gettext(
3536d50c8f90Svsakar 		    "[not currently a valid file system - bad superblock]\n"));
35377c478bd9Sstevel@tonic-gate 		lockexit(32);
35387c478bd9Sstevel@tonic-gate 	}
35397c478bd9Sstevel@tonic-gate 
35406451fdbcSvsakar 	if (sblock.fs_magic == FS_MAGIC &&
35416451fdbcSvsakar 	    (sblock.fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
35426451fdbcSvsakar 	    sblock.fs_version != UFS_VERSION_MIN)) {
3543d50c8f90Svsakar 		(void) fprintf(stderr, gettext(
3544d50c8f90Svsakar 		    "Unknown version of UFS format: %d\n"), sblock.fs_version);
35456451fdbcSvsakar 		lockexit(32);
35466451fdbcSvsakar 	}
35476451fdbcSvsakar 
35487c478bd9Sstevel@tonic-gate 	if (sblock.fs_magic == MTB_UFS_MAGIC &&
35497c478bd9Sstevel@tonic-gate 	    (sblock.fs_version > MTB_UFS_VERSION_1 ||
35507c478bd9Sstevel@tonic-gate 	    sblock.fs_version < MTB_UFS_VERSION_MIN)) {
3551d50c8f90Svsakar 		(void) fprintf(stderr, gettext(
3552d50c8f90Svsakar 		    "Unknown version of UFS format: %d\n"), sblock.fs_version);
35537c478bd9Sstevel@tonic-gate 		lockexit(32);
35547c478bd9Sstevel@tonic-gate 	}
35557c478bd9Sstevel@tonic-gate 
35567c478bd9Sstevel@tonic-gate 	/*
35577c478bd9Sstevel@tonic-gate 	 * Compute a reasonable nbpi value.
35587c478bd9Sstevel@tonic-gate 	 * The algorithm for "used" is copied from code
35597c478bd9Sstevel@tonic-gate 	 * in main() verbatim.
35607c478bd9Sstevel@tonic-gate 	 * The nbpi equation is taken from main where the
35617c478bd9Sstevel@tonic-gate 	 * fs_ipg value is set for the last time.  The INOPB(...) - 1
35627c478bd9Sstevel@tonic-gate 	 * is used to account for the roundup.
35637c478bd9Sstevel@tonic-gate 	 * The problem is that a range of nbpi values map to
35647c478bd9Sstevel@tonic-gate 	 * the same file system layout.  So it is not possible
35657c478bd9Sstevel@tonic-gate 	 * to calculate the exact value specified when the file
35667c478bd9Sstevel@tonic-gate 	 * system was created.  So instead we determine the top
35677c478bd9Sstevel@tonic-gate 	 * end of the range of values.
35687c478bd9Sstevel@tonic-gate 	 */
35697c478bd9Sstevel@tonic-gate 	bpcg = sblock.fs_spc * sectorsize;
35707c478bd9Sstevel@tonic-gate 	inospercg = (int64_t)roundup(bpcg / sizeof (struct dinode),
35717c478bd9Sstevel@tonic-gate 	    INOPB(&sblock));
35727c478bd9Sstevel@tonic-gate 	if (inospercg > MAXIpG(&sblock))
35737c478bd9Sstevel@tonic-gate 		inospercg = MAXIpG(&sblock);
35747c478bd9Sstevel@tonic-gate 	used = (int64_t)
35757c478bd9Sstevel@tonic-gate 	    (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
35767c478bd9Sstevel@tonic-gate 	used *= sectorsize;
35777c478bd9Sstevel@tonic-gate 	nbytes64 = (uint64_t)sblock.fs_cpg * bpcg - used;
35787c478bd9Sstevel@tonic-gate 
35797c478bd9Sstevel@tonic-gate 	/*
35807c478bd9Sstevel@tonic-gate 	 * The top end of the range of values for nbpi may not be
35817c478bd9Sstevel@tonic-gate 	 * a valid command line value for mkfs. Report the bottom
35827c478bd9Sstevel@tonic-gate 	 * end instead.
35837c478bd9Sstevel@tonic-gate 	 */
35847c478bd9Sstevel@tonic-gate 	nbpi = (int64_t)(nbytes64 / (sblock.fs_ipg));
35857c478bd9Sstevel@tonic-gate 
35867c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, gettext("mkfs -F ufs -o "), fsys);
35877c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "nsect=%d,ntrack=%d,",
35887c478bd9Sstevel@tonic-gate 	    sblock.fs_nsect, sblock.fs_ntrak);
35897c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "bsize=%d,fragsize=%d,cgsize=%d,free=%d,",
35907c478bd9Sstevel@tonic-gate 	    sblock.fs_bsize, sblock.fs_fsize, sblock.fs_cpg, sblock.fs_minfree);
35917c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "rps=%d,nbpi=%lld,opt=%c,apc=%d,gap=%d,",
35927c478bd9Sstevel@tonic-gate 	    sblock.fs_rps, nbpi, (sblock.fs_optim == FS_OPTSPACE) ? 's' : 't',
35937c478bd9Sstevel@tonic-gate 	    (sblock.fs_ntrak * sblock.fs_nsect) - sblock.fs_spc,
35947c478bd9Sstevel@tonic-gate 	    sblock.fs_rotdelay);
35957c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "nrpos=%d,maxcontig=%d,mtb=%c ",
35967c478bd9Sstevel@tonic-gate 	    sblock.fs_nrpos, sblock.fs_maxcontig,
35977c478bd9Sstevel@tonic-gate 	    ((sblock.fs_magic == MTB_UFS_MAGIC) ? 'y' : 'n'));
35987c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "%s %lld\n", fsys,
35997c478bd9Sstevel@tonic-gate 	    fsbtodb(&sblock, sblock.fs_size));
36007c478bd9Sstevel@tonic-gate 
36017c478bd9Sstevel@tonic-gate 	bzero((char *)&sblock, sizeof (sblock));
36027c478bd9Sstevel@tonic-gate }
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate /* number ************************************************************* */
36057c478bd9Sstevel@tonic-gate /*									*/
36067c478bd9Sstevel@tonic-gate /* Convert a numeric string arg to binary				*/
36077c478bd9Sstevel@tonic-gate /*									*/
36087c478bd9Sstevel@tonic-gate /* Args:	d_value - default value, if have parse error		*/
36097c478bd9Sstevel@tonic-gate /*		param - the name of the argument, for error messages	*/
36107c478bd9Sstevel@tonic-gate /*		flags - parser state and what's allowed in the arg	*/
36117c478bd9Sstevel@tonic-gate /* Global arg:  string - pointer to command arg				*/
36127c478bd9Sstevel@tonic-gate /*									*/
36137c478bd9Sstevel@tonic-gate /* Valid forms: 123 | 123k | 123*123 | 123x123				*/
36147c478bd9Sstevel@tonic-gate /*									*/
36157c478bd9Sstevel@tonic-gate /* Return:	converted number					*/
36167c478bd9Sstevel@tonic-gate /*									*/
36177c478bd9Sstevel@tonic-gate /* ******************************************************************** */
36187c478bd9Sstevel@tonic-gate 
36197c478bd9Sstevel@tonic-gate static uint64_t
number(uint64_t d_value,char * param,int flags)36207c478bd9Sstevel@tonic-gate number(uint64_t d_value, char *param, int flags)
36217c478bd9Sstevel@tonic-gate {
36227c478bd9Sstevel@tonic-gate 	char *cs;
36237c478bd9Sstevel@tonic-gate 	uint64_t n, t;
36247c478bd9Sstevel@tonic-gate 	uint64_t cut = BIG / 10;    /* limit to avoid overflow */
36257c478bd9Sstevel@tonic-gate 	int minus = 0;
36267c478bd9Sstevel@tonic-gate 
36277c478bd9Sstevel@tonic-gate 	cs = string;
36287c478bd9Sstevel@tonic-gate 	if (*cs == '-') {
36297c478bd9Sstevel@tonic-gate 		minus = 1;
36307c478bd9Sstevel@tonic-gate 		cs += 1;
36317c478bd9Sstevel@tonic-gate 	}
36327c478bd9Sstevel@tonic-gate 	if ((*cs < '0') || (*cs > '9')) {
36337c478bd9Sstevel@tonic-gate 		goto bail_out;
36347c478bd9Sstevel@tonic-gate 	}
36357c478bd9Sstevel@tonic-gate 	n = 0;
36367c478bd9Sstevel@tonic-gate 	while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) {
36377c478bd9Sstevel@tonic-gate 		n = n*10 + *cs++ - '0';
36387c478bd9Sstevel@tonic-gate 	}
36397c478bd9Sstevel@tonic-gate 	if (minus)
3640d50c8f90Svsakar 		n = -n;
36417c478bd9Sstevel@tonic-gate 	for (;;) {
36427c478bd9Sstevel@tonic-gate 		switch (*cs++) {
36437c478bd9Sstevel@tonic-gate 		case 'k':
36447c478bd9Sstevel@tonic-gate 			if (flags & ALLOW_END_ONLY)
36457c478bd9Sstevel@tonic-gate 				goto bail_out;
36467c478bd9Sstevel@tonic-gate 			if (n > (BIG / 1024))
36477c478bd9Sstevel@tonic-gate 				goto overflow;
36487c478bd9Sstevel@tonic-gate 			n *= 1024;
36497c478bd9Sstevel@tonic-gate 			continue;
36507c478bd9Sstevel@tonic-gate 
36517c478bd9Sstevel@tonic-gate 		case '*':
36527c478bd9Sstevel@tonic-gate 		case 'x':
36537c478bd9Sstevel@tonic-gate 			if (flags & ALLOW_END_ONLY)
36547c478bd9Sstevel@tonic-gate 				goto bail_out;
36557c478bd9Sstevel@tonic-gate 			string = cs;
36567c478bd9Sstevel@tonic-gate 			t = number(d_value, param, flags);
36577c478bd9Sstevel@tonic-gate 			if (n > (BIG / t))
36587c478bd9Sstevel@tonic-gate 				goto overflow;
36597c478bd9Sstevel@tonic-gate 			n *= t;
36607c478bd9Sstevel@tonic-gate 			cs = string + 1; /* adjust for -- below */
36617c478bd9Sstevel@tonic-gate 
36627c478bd9Sstevel@tonic-gate 			/* recursion has read rest of expression */
36637c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
36647c478bd9Sstevel@tonic-gate 
36657c478bd9Sstevel@tonic-gate 		case ',':
36667c478bd9Sstevel@tonic-gate 		case '\0':
36677c478bd9Sstevel@tonic-gate 			cs--;
36687c478bd9Sstevel@tonic-gate 			string = cs;
36697c478bd9Sstevel@tonic-gate 			return (n);
36707c478bd9Sstevel@tonic-gate 
36717c478bd9Sstevel@tonic-gate 		case '%':
36727c478bd9Sstevel@tonic-gate 			if (flags & ALLOW_END_ONLY)
36737c478bd9Sstevel@tonic-gate 				goto bail_out;
36747c478bd9Sstevel@tonic-gate 			if (flags & ALLOW_PERCENT) {
36757c478bd9Sstevel@tonic-gate 				flags &= ~ALLOW_PERCENT;
36767c478bd9Sstevel@tonic-gate 				flags |= ALLOW_END_ONLY;
36777c478bd9Sstevel@tonic-gate 				continue;
36787c478bd9Sstevel@tonic-gate 			}
36797c478bd9Sstevel@tonic-gate 			goto bail_out;
36807c478bd9Sstevel@tonic-gate 
36817c478bd9Sstevel@tonic-gate 		case 'm':
36827c478bd9Sstevel@tonic-gate 			if (flags & ALLOW_END_ONLY)
36837c478bd9Sstevel@tonic-gate 				goto bail_out;
36847c478bd9Sstevel@tonic-gate 			if (flags & ALLOW_MS1) {
36857c478bd9Sstevel@tonic-gate 				flags &= ~ALLOW_MS1;
36867c478bd9Sstevel@tonic-gate 				flags |= ALLOW_MS2;
36877c478bd9Sstevel@tonic-gate 				continue;
36887c478bd9Sstevel@tonic-gate 			}
36897c478bd9Sstevel@tonic-gate 			goto bail_out;
36907c478bd9Sstevel@tonic-gate 
36917c478bd9Sstevel@tonic-gate 		case 's':
36927c478bd9Sstevel@tonic-gate 			if (flags & ALLOW_END_ONLY)
36937c478bd9Sstevel@tonic-gate 				goto bail_out;
36947c478bd9Sstevel@tonic-gate 			if (flags & ALLOW_MS2) {
36957c478bd9Sstevel@tonic-gate 				flags &= ~ALLOW_MS2;
36967c478bd9Sstevel@tonic-gate 				flags |= ALLOW_END_ONLY;
36977c478bd9Sstevel@tonic-gate 				continue;
36987c478bd9Sstevel@tonic-gate 			}
36997c478bd9Sstevel@tonic-gate 			goto bail_out;
37007c478bd9Sstevel@tonic-gate 
37017c478bd9Sstevel@tonic-gate 		case '0': case '1': case '2': case '3': case '4':
37027c478bd9Sstevel@tonic-gate 		case '5': case '6': case '7': case '8': case '9':
37037c478bd9Sstevel@tonic-gate overflow:
37047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
37057c478bd9Sstevel@tonic-gate 			    gettext("mkfs: value for %s overflowed\n"),
37067c478bd9Sstevel@tonic-gate 			    param);
37077c478bd9Sstevel@tonic-gate 			while ((*cs != '\0') && (*cs != ','))
37087c478bd9Sstevel@tonic-gate 				cs++;
37097c478bd9Sstevel@tonic-gate 			string = cs;
37107c478bd9Sstevel@tonic-gate 			return (BIG);
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate 		default:
37137c478bd9Sstevel@tonic-gate bail_out:
37147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
37157c478bd9Sstevel@tonic-gate 			    "mkfs: bad numeric arg for %s: \"%s\"\n"),
37167c478bd9Sstevel@tonic-gate 			    param, string);
37177c478bd9Sstevel@tonic-gate 			while ((*cs != '\0') && (*cs != ','))
37187c478bd9Sstevel@tonic-gate 				cs++;
37197c478bd9Sstevel@tonic-gate 			string = cs;
37207c478bd9Sstevel@tonic-gate 			if (d_value != NO_DEFAULT) {
37217c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
37227c478bd9Sstevel@tonic-gate 				    gettext("mkfs: %s reset to default %lld\n"),
37237c478bd9Sstevel@tonic-gate 				    param, d_value);
37247c478bd9Sstevel@tonic-gate 				return (d_value);
37257c478bd9Sstevel@tonic-gate 			}
37267c478bd9Sstevel@tonic-gate 			lockexit(2);
37277c478bd9Sstevel@tonic-gate 
37287c478bd9Sstevel@tonic-gate 		}
37297c478bd9Sstevel@tonic-gate 	} /* never gets here */
37307c478bd9Sstevel@tonic-gate }
37317c478bd9Sstevel@tonic-gate 
37327c478bd9Sstevel@tonic-gate /* match ************************************************************** */
37337c478bd9Sstevel@tonic-gate /*									*/
37347c478bd9Sstevel@tonic-gate /* Compare two text strings for equality				*/
37357c478bd9Sstevel@tonic-gate /*									*/
37367c478bd9Sstevel@tonic-gate /* Arg:	 s - pointer to string to match with a command arg		*/
37377c478bd9Sstevel@tonic-gate /* Global arg:  string - pointer to command arg				*/
37387c478bd9Sstevel@tonic-gate /*									*/
37397c478bd9Sstevel@tonic-gate /* Return:	1 if match, 0 if no match				*/
37407c478bd9Sstevel@tonic-gate /*		If match, also reset `string' to point to the text	*/
37417c478bd9Sstevel@tonic-gate /*		that follows the matching text.				*/
37427c478bd9Sstevel@tonic-gate /*									*/
37437c478bd9Sstevel@tonic-gate /* ******************************************************************** */
37447c478bd9Sstevel@tonic-gate 
37457c478bd9Sstevel@tonic-gate static int
match(char * s)37467c478bd9Sstevel@tonic-gate match(char *s)
37477c478bd9Sstevel@tonic-gate {
37487c478bd9Sstevel@tonic-gate 	char *cs;
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate 	cs = string;
37517c478bd9Sstevel@tonic-gate 	while (*cs++ == *s) {
37527c478bd9Sstevel@tonic-gate 		if (*s++ == '\0') {
37537c478bd9Sstevel@tonic-gate 			goto true;
37547c478bd9Sstevel@tonic-gate 		}
37557c478bd9Sstevel@tonic-gate 	}
37567c478bd9Sstevel@tonic-gate 	if (*s != '\0') {
37577c478bd9Sstevel@tonic-gate 		return (0);
37587c478bd9Sstevel@tonic-gate 	}
37597c478bd9Sstevel@tonic-gate 
37607c478bd9Sstevel@tonic-gate true:
37617c478bd9Sstevel@tonic-gate 	cs--;
37627c478bd9Sstevel@tonic-gate 	string = cs;
37637c478bd9Sstevel@tonic-gate 	return (1);
37647c478bd9Sstevel@tonic-gate }
37657c478bd9Sstevel@tonic-gate 
37667c478bd9Sstevel@tonic-gate /*
37677c478bd9Sstevel@tonic-gate  * GROWFS ROUTINES
37687c478bd9Sstevel@tonic-gate  */
37697c478bd9Sstevel@tonic-gate 
37707c478bd9Sstevel@tonic-gate /* ARGSUSED */
37717c478bd9Sstevel@tonic-gate void
lockexit(int exitstatus)37727c478bd9Sstevel@tonic-gate lockexit(int exitstatus)
37737c478bd9Sstevel@tonic-gate {
37747c478bd9Sstevel@tonic-gate 	if (Pflag) {
37757c478bd9Sstevel@tonic-gate 		/* the probe mode neither changes nor locks the filesystem */
37767c478bd9Sstevel@tonic-gate 		exit(exitstatus);
37777c478bd9Sstevel@tonic-gate 	}
37787c478bd9Sstevel@tonic-gate 
37797c478bd9Sstevel@tonic-gate 	/*
37807c478bd9Sstevel@tonic-gate 	 * flush the dirty cylinder group
37817c478bd9Sstevel@tonic-gate 	 */
37827c478bd9Sstevel@tonic-gate 	if (inlockexit == 0) {
37837c478bd9Sstevel@tonic-gate 		inlockexit = 1;
37847c478bd9Sstevel@tonic-gate 		flcg();
37857c478bd9Sstevel@tonic-gate 	}
37867c478bd9Sstevel@tonic-gate 
37877c478bd9Sstevel@tonic-gate 	if (aio_inited) {
37887c478bd9Sstevel@tonic-gate 		flush_writes();
37897c478bd9Sstevel@tonic-gate 	}
37907c478bd9Sstevel@tonic-gate 
37917c478bd9Sstevel@tonic-gate 	/*
37927c478bd9Sstevel@tonic-gate 	 * make sure the file system is unlocked before exiting
37937c478bd9Sstevel@tonic-gate 	 */
37947c478bd9Sstevel@tonic-gate 	if ((inlockexit == 1) && (!isbad)) {
37957c478bd9Sstevel@tonic-gate 		inlockexit = 2;
37967c478bd9Sstevel@tonic-gate 		ulockfs();
37977c478bd9Sstevel@tonic-gate 		/*
37987c478bd9Sstevel@tonic-gate 		 * if logging was enabled, then re-enable it
37997c478bd9Sstevel@tonic-gate 		 */
38007c478bd9Sstevel@tonic-gate 		if (waslog) {
38017c478bd9Sstevel@tonic-gate 			if (rl_log_control(fsys, _FIOLOGENABLE) != RL_SUCCESS) {
38027c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3803d50c8f90Svsakar 				    "failed to re-enable logging\n"));
38047c478bd9Sstevel@tonic-gate 			}
38057c478bd9Sstevel@tonic-gate 		}
38067c478bd9Sstevel@tonic-gate 	} else if (grow) {
38077c478bd9Sstevel@tonic-gate 		if (isbad) {
38087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3809d50c8f90Svsakar 			    "Filesystem is currently inconsistent.  It "
3810*bbf21555SRichard Lowe 			    "must be repaired with fsck(8)\nbefore being "
3811d50c8f90Svsakar 			    "used.  Use the following command to "
3812d50c8f90Svsakar 			    "do this:\n\n\tfsck %s\n\n"), fsys);
38137c478bd9Sstevel@tonic-gate 
38147c478bd9Sstevel@tonic-gate 			if (ismounted) {
38157c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3816d50c8f90Svsakar 				    "You will be told that the filesystem "
3817d50c8f90Svsakar 				    "is already mounted, and asked if you\n"
3818d50c8f90Svsakar 				    "wish to continue.  Answer `yes' to "
3819d50c8f90Svsakar 				    "this question.\n\n"));
38207c478bd9Sstevel@tonic-gate 			}
38217c478bd9Sstevel@tonic-gate 
38227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3823d50c8f90Svsakar 			    "One problem should be reported, that the summary "
3824d50c8f90Svsakar 			    "information is bad.\nYou will then be asked if it "
3825d50c8f90Svsakar 			    "should be salvaged.  Answer `yes' to\nthis "
3826d50c8f90Svsakar 			    "question.\n\n"));
38277c478bd9Sstevel@tonic-gate 		}
38287c478bd9Sstevel@tonic-gate 
38297c478bd9Sstevel@tonic-gate 		if (ismounted) {
38307c478bd9Sstevel@tonic-gate 			/*
38317c478bd9Sstevel@tonic-gate 			 * In theory, there's no way to get here without
38327c478bd9Sstevel@tonic-gate 			 * isbad also being set, but be robust in the
38337c478bd9Sstevel@tonic-gate 			 * face of future code changes.
38347c478bd9Sstevel@tonic-gate 			 */
38357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3836d50c8f90Svsakar 			    "The filesystem is currently mounted "
3837d50c8f90Svsakar 			    "read-only and write-locked.  "));
38387c478bd9Sstevel@tonic-gate 			if (isbad) {
38397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3840d50c8f90Svsakar 				    "After\nrunning fsck, unlock the "
3841d50c8f90Svsakar 				    "filesystem and "));
38427c478bd9Sstevel@tonic-gate 			} else {
38437c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3844d50c8f90Svsakar 				    "Unlock the filesystem\nand "));
38457c478bd9Sstevel@tonic-gate 			}
38467c478bd9Sstevel@tonic-gate 
38477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3848d50c8f90Svsakar 			    "re-enable writing with\nthe following "
3849d50c8f90Svsakar 			    "command:\n\n\tlockfs -u %s\n\n"), directory);
38507c478bd9Sstevel@tonic-gate 		}
38517c478bd9Sstevel@tonic-gate 	}
38527c478bd9Sstevel@tonic-gate 
38537c478bd9Sstevel@tonic-gate 	exit(exitstatus);
38547c478bd9Sstevel@tonic-gate }
38557c478bd9Sstevel@tonic-gate 
38567c478bd9Sstevel@tonic-gate void
randomgeneration()38577c478bd9Sstevel@tonic-gate randomgeneration()
38587c478bd9Sstevel@tonic-gate {
38597c478bd9Sstevel@tonic-gate 	int		 i;
38607c478bd9Sstevel@tonic-gate 	struct dinode	*dp;
38617c478bd9Sstevel@tonic-gate 
38627c478bd9Sstevel@tonic-gate 	/*
38637c478bd9Sstevel@tonic-gate 	 * always perform fsirand(1) function... newfs will notice that
38647c478bd9Sstevel@tonic-gate 	 * the inodes have been randomized and will not call fsirand itself
38657c478bd9Sstevel@tonic-gate 	 */
38667c478bd9Sstevel@tonic-gate 	for (i = 0, dp = zino; i < sblock.fs_inopb; ++i, ++dp)
38677c478bd9Sstevel@tonic-gate 		IRANDOMIZE(&dp->di_ic);
38687c478bd9Sstevel@tonic-gate }
38697c478bd9Sstevel@tonic-gate 
38707c478bd9Sstevel@tonic-gate /*
38717c478bd9Sstevel@tonic-gate  * Check the size of the summary information.
38727c478bd9Sstevel@tonic-gate  * Fields in sblock are not changed in this function.
38737c478bd9Sstevel@tonic-gate  *
38747c478bd9Sstevel@tonic-gate  * For an 8K filesystem block, the maximum number of cylinder groups is 16384.
38757c478bd9Sstevel@tonic-gate  *     MAXCSBUFS {32}  *   8K  {FS block size}
38767c478bd9Sstevel@tonic-gate  *                         divided by (sizeof csum) {16}
38777c478bd9Sstevel@tonic-gate  *
38787c478bd9Sstevel@tonic-gate  * Note that MAXCSBUFS is not used in the kernel; as of Solaris 2.6 build 32,
38797c478bd9Sstevel@tonic-gate  * this is the only place where it's referenced.
38807c478bd9Sstevel@tonic-gate  */
38817c478bd9Sstevel@tonic-gate void
checksummarysize()38827c478bd9Sstevel@tonic-gate checksummarysize()
38837c478bd9Sstevel@tonic-gate {
38847c478bd9Sstevel@tonic-gate 	diskaddr_t	dmax;
38857c478bd9Sstevel@tonic-gate 	diskaddr_t	dmin;
38867c478bd9Sstevel@tonic-gate 	int64_t	cg0frags;
38877c478bd9Sstevel@tonic-gate 	int64_t	cg0blocks;
38887c478bd9Sstevel@tonic-gate 	int64_t	maxncg;
38897c478bd9Sstevel@tonic-gate 	int64_t	maxfrags;
38907c478bd9Sstevel@tonic-gate 	uint64_t	fs_size;
38917c478bd9Sstevel@tonic-gate 	uint64_t maxfs_blocks; /* filesystem blocks for max filesystem size */
38927c478bd9Sstevel@tonic-gate 
38937c478bd9Sstevel@tonic-gate 	/*
38947c478bd9Sstevel@tonic-gate 	 * compute the maximum summary info size
38957c478bd9Sstevel@tonic-gate 	 */
38967c478bd9Sstevel@tonic-gate 	dmin = cgdmin(&sblock, 0);
38977c478bd9Sstevel@tonic-gate 	dmax = cgbase(&sblock, 0) + sblock.fs_fpg;
38987c478bd9Sstevel@tonic-gate 	fs_size = (grow) ? grow_fs_size : sblock.fs_size;
38997c478bd9Sstevel@tonic-gate 	if (dmax > fs_size)
39007c478bd9Sstevel@tonic-gate 		dmax = fs_size;
39017c478bd9Sstevel@tonic-gate 	cg0frags  = dmax - dmin;
39027c478bd9Sstevel@tonic-gate 	cg0blocks = cg0frags / sblock.fs_frag;
39037c478bd9Sstevel@tonic-gate 	cg0frags = cg0blocks * sblock.fs_frag;
39047c478bd9Sstevel@tonic-gate 	maxncg   = (longlong_t)cg0blocks *
39057c478bd9Sstevel@tonic-gate 	    (longlong_t)(sblock.fs_bsize / sizeof (struct csum));
39067c478bd9Sstevel@tonic-gate 
39077c478bd9Sstevel@tonic-gate 	maxfs_blocks = FS_MAX;
39087c478bd9Sstevel@tonic-gate 
39097c478bd9Sstevel@tonic-gate 	if (maxncg > ((longlong_t)maxfs_blocks / (longlong_t)sblock.fs_fpg) + 1)
39107c478bd9Sstevel@tonic-gate 		maxncg = ((longlong_t)maxfs_blocks /
39117c478bd9Sstevel@tonic-gate 		    (longlong_t)sblock.fs_fpg) + 1;
39127c478bd9Sstevel@tonic-gate 
39137c478bd9Sstevel@tonic-gate 	maxfrags = maxncg * (longlong_t)sblock.fs_fpg;
39147c478bd9Sstevel@tonic-gate 
39157c478bd9Sstevel@tonic-gate 	if (maxfrags > maxfs_blocks)
39167c478bd9Sstevel@tonic-gate 		maxfrags = maxfs_blocks;
39177c478bd9Sstevel@tonic-gate 
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate 	/*
39207c478bd9Sstevel@tonic-gate 	 * remember for later processing in extendsummaryinfo()
39217c478bd9Sstevel@tonic-gate 	 */
39227c478bd9Sstevel@tonic-gate 	if (test)
39237c478bd9Sstevel@tonic-gate 		grow_sifrag = dmin + (cg0blocks * sblock.fs_frag);
39247c478bd9Sstevel@tonic-gate 	if (testfrags == 0)
39257c478bd9Sstevel@tonic-gate 		testfrags = cg0frags;
39267c478bd9Sstevel@tonic-gate 	if (testforce)
39277c478bd9Sstevel@tonic-gate 		if (testfrags > cg0frags) {
39287c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3929d50c8f90Svsakar 			    gettext("Too many test frags (%lld); "
3930d50c8f90Svsakar 			    "try %lld\n"), testfrags, cg0frags);
39317c478bd9Sstevel@tonic-gate 			lockexit(32);
39327c478bd9Sstevel@tonic-gate 		}
39337c478bd9Sstevel@tonic-gate 
39347c478bd9Sstevel@tonic-gate 	/*
39357c478bd9Sstevel@tonic-gate 	 * if summary info is too large (too many cg's) tell the user and exit
39367c478bd9Sstevel@tonic-gate 	 */
39377c478bd9Sstevel@tonic-gate 	if ((longlong_t)sblock.fs_size > maxfrags) {
39387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
39397c478bd9Sstevel@tonic-gate 		    "Too many cylinder groups with %llu sectors;\n    try "
39407c478bd9Sstevel@tonic-gate 		    "increasing cgsize, or decreasing fssize to %llu\n"),
39417c478bd9Sstevel@tonic-gate 		    fsbtodb(&sblock, (uint64_t)sblock.fs_size),
39427c478bd9Sstevel@tonic-gate 		    fsbtodb(&sblock, (uint64_t)maxfrags));
39437c478bd9Sstevel@tonic-gate 		lockexit(32);
39447c478bd9Sstevel@tonic-gate 	}
39457c478bd9Sstevel@tonic-gate }
39467c478bd9Sstevel@tonic-gate 
39476451fdbcSvsakar /*
39486451fdbcSvsakar  * checksblock() has two uses:
3949*bbf21555SRichard Lowe  *	- One is to sanity test the superblock and is used when newfs(8)
39506451fdbcSvsakar  *	  is invoked with the "-N" option. If any discrepancy was found,
39516451fdbcSvsakar  *	  just return whatever error was found and do not exit.
39526451fdbcSvsakar  *	- the other use of it is in places where you expect the superblock
39536451fdbcSvsakar  *	  to be sane, and if it isn't, then we exit.
39546451fdbcSvsakar  * Which of the above two actions to take is indicated with the second argument.
39556451fdbcSvsakar  */
39567c478bd9Sstevel@tonic-gate 
39576451fdbcSvsakar int
checksblock(struct fs sb,int proceed)39586451fdbcSvsakar checksblock(struct fs sb, int proceed)
39596451fdbcSvsakar {
39606451fdbcSvsakar 	int err = 0;
39616451fdbcSvsakar 	char *errmsg;
39626451fdbcSvsakar 
39636451fdbcSvsakar 	if ((sb.fs_magic != FS_MAGIC) && (sb.fs_magic != MTB_UFS_MAGIC)) {
3964d50c8f90Svsakar 		err = 1;
3965d50c8f90Svsakar 		errmsg = gettext("Bad superblock; magic number wrong\n");
39666451fdbcSvsakar 	} else if ((sb.fs_magic == FS_MAGIC &&
3967d50c8f90Svsakar 	    (sb.fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
3968d50c8f90Svsakar 	    sb.fs_version != UFS_VERSION_MIN)) ||
3969d50c8f90Svsakar 	    (sb.fs_magic == MTB_UFS_MAGIC &&
3970d50c8f90Svsakar 	    (sb.fs_version > MTB_UFS_VERSION_1 ||
3971d50c8f90Svsakar 	    sb.fs_version < MTB_UFS_VERSION_MIN))) {
3972d50c8f90Svsakar 		err = 2;
3973d50c8f90Svsakar 		errmsg = gettext("Unrecognized version of UFS\n");
39746451fdbcSvsakar 	} else if (sb.fs_ncg < 1) {
3975d50c8f90Svsakar 		err = 3;
3976d50c8f90Svsakar 		errmsg = gettext("Bad superblock; ncg out of range\n");
39776451fdbcSvsakar 	} else if (sb.fs_cpg < 1) {
3978d50c8f90Svsakar 		err = 4;
3979d50c8f90Svsakar 		errmsg = gettext("Bad superblock; cpg out of range\n");
39806451fdbcSvsakar 	} else if (sb.fs_ncg * sb.fs_cpg < sb.fs_ncyl ||
3981d50c8f90Svsakar 	    (sb.fs_ncg - 1) * sb.fs_cpg >= sb.fs_ncyl) {
3982d50c8f90Svsakar 		err = 5;
3983d50c8f90Svsakar 		errmsg = gettext("Bad superblock; ncyl out of range\n");
39846451fdbcSvsakar 	} else if (sb.fs_sbsize <= 0 || sb.fs_sbsize > sb.fs_bsize) {
3985d50c8f90Svsakar 		err = 6;
3986d50c8f90Svsakar 		errmsg = gettext("Bad superblock; superblock size out of "
3987d50c8f90Svsakar 		    "range\n");
39886451fdbcSvsakar 	}
39896451fdbcSvsakar 
39906451fdbcSvsakar 	if (proceed) {
39916451fdbcSvsakar 		if (err) dprintf(("%s", errmsg));
39926451fdbcSvsakar 		return (err);
39936451fdbcSvsakar 	}
39946451fdbcSvsakar 
39956451fdbcSvsakar 	if (err) {
39966451fdbcSvsakar 		fprintf(stderr, "%s", errmsg);
39977c478bd9Sstevel@tonic-gate 		lockexit(32);
39987c478bd9Sstevel@tonic-gate 	}
3999d1a180b0Smaheshvs 	return (32);
40007c478bd9Sstevel@tonic-gate }
40017c478bd9Sstevel@tonic-gate 
40027c478bd9Sstevel@tonic-gate /*
40037c478bd9Sstevel@tonic-gate  * Roll the embedded log, if any, and set up the global variables
40045f10ef69SYuri Pankov  * islog and islogok.
40057c478bd9Sstevel@tonic-gate  */
40067c478bd9Sstevel@tonic-gate static void
logsetup(char * devstr)40077c478bd9Sstevel@tonic-gate logsetup(char *devstr)
40087c478bd9Sstevel@tonic-gate {
40097c478bd9Sstevel@tonic-gate 	void		*buf, *ud_buf;
40107c478bd9Sstevel@tonic-gate 	extent_block_t	*ebp;
40117c478bd9Sstevel@tonic-gate 	ml_unit_t	*ul;
40127c478bd9Sstevel@tonic-gate 	ml_odunit_t	*ud;
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate 	/*
40157c478bd9Sstevel@tonic-gate 	 * Does the superblock indicate that we are supposed to have a log ?
40167c478bd9Sstevel@tonic-gate 	 */
40177c478bd9Sstevel@tonic-gate 	if (sblock.fs_logbno == 0) {
40187c478bd9Sstevel@tonic-gate 		/*
40197c478bd9Sstevel@tonic-gate 		 * No log present, nothing to do.
40207c478bd9Sstevel@tonic-gate 		 */
40217c478bd9Sstevel@tonic-gate 		islog = 0;
40225f10ef69SYuri Pankov 		islogok = 0;
40237c478bd9Sstevel@tonic-gate 		return;
40247c478bd9Sstevel@tonic-gate 	} else {
40257c478bd9Sstevel@tonic-gate 		/*
40267c478bd9Sstevel@tonic-gate 		 * There's a log in a yet unknown state, attempt to roll it.
40277c478bd9Sstevel@tonic-gate 		 */
40287c478bd9Sstevel@tonic-gate 		islogok = 0;
40297c478bd9Sstevel@tonic-gate 
40307c478bd9Sstevel@tonic-gate 		/*
40317c478bd9Sstevel@tonic-gate 		 * We failed to roll the log, bail out.
40327c478bd9Sstevel@tonic-gate 		 */
40337c478bd9Sstevel@tonic-gate 		if (rl_roll_log(devstr) != RL_SUCCESS)
40347c478bd9Sstevel@tonic-gate 			return;
40357c478bd9Sstevel@tonic-gate 
40365f10ef69SYuri Pankov 		islog = 1;
40377c478bd9Sstevel@tonic-gate 
40387c478bd9Sstevel@tonic-gate 		/* log is not okay; check the fs */
40397c478bd9Sstevel@tonic-gate 		if ((FSOKAY != (sblock.fs_state + sblock.fs_time)) ||
40407c478bd9Sstevel@tonic-gate 		    (sblock.fs_clean != FSLOG))
40417c478bd9Sstevel@tonic-gate 			return;
40427c478bd9Sstevel@tonic-gate 
40437c478bd9Sstevel@tonic-gate 		/* get the log allocation block */
40447c478bd9Sstevel@tonic-gate 		buf = (void *)malloc(DEV_BSIZE);
40457c478bd9Sstevel@tonic-gate 		if (buf == (void *) NULL)
40467c478bd9Sstevel@tonic-gate 			return;
40477c478bd9Sstevel@tonic-gate 
40487c478bd9Sstevel@tonic-gate 		ud_buf = (void *)malloc(DEV_BSIZE);
40497c478bd9Sstevel@tonic-gate 		if (ud_buf == (void *) NULL) {
40507c478bd9Sstevel@tonic-gate 			free(buf);
40517c478bd9Sstevel@tonic-gate 			return;
40527c478bd9Sstevel@tonic-gate 		}
40537c478bd9Sstevel@tonic-gate 
40547c478bd9Sstevel@tonic-gate 		rdfs((diskaddr_t)logbtodb(&sblock, sblock.fs_logbno),
40557c478bd9Sstevel@tonic-gate 		    DEV_BSIZE, buf);
40567c478bd9Sstevel@tonic-gate 		ebp = (extent_block_t *)buf;
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate 		/* log allocation block is not okay; check the fs */
40597c478bd9Sstevel@tonic-gate 		if (ebp->type != LUFS_EXTENTS) {
40607c478bd9Sstevel@tonic-gate 			free(buf);
40617c478bd9Sstevel@tonic-gate 			free(ud_buf);
40627c478bd9Sstevel@tonic-gate 			return;
40637c478bd9Sstevel@tonic-gate 		}
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate 		/* get the log state block(s) */
40667c478bd9Sstevel@tonic-gate 		rdfs((diskaddr_t)logbtodb(&sblock, ebp->extents[0].pbno),
40677c478bd9Sstevel@tonic-gate 		    DEV_BSIZE, ud_buf);
40687c478bd9Sstevel@tonic-gate 		ud = (ml_odunit_t *)ud_buf;
40697c478bd9Sstevel@tonic-gate 		ul = (ml_unit_t *)malloc(sizeof (*ul));
40707c478bd9Sstevel@tonic-gate 		ul->un_ondisk = *ud;
40717c478bd9Sstevel@tonic-gate 
40727c478bd9Sstevel@tonic-gate 		/* log state is okay */
40737c478bd9Sstevel@tonic-gate 		if ((ul->un_chksum == ul->un_head_ident + ul->un_tail_ident) &&
40747c478bd9Sstevel@tonic-gate 		    (ul->un_version == LUFS_VERSION_LATEST) &&
40757c478bd9Sstevel@tonic-gate 		    (ul->un_badlog == 0))
40767c478bd9Sstevel@tonic-gate 			islogok = 1;
40777c478bd9Sstevel@tonic-gate 		free(ud_buf);
40787c478bd9Sstevel@tonic-gate 		free(buf);
40797c478bd9Sstevel@tonic-gate 		free(ul);
40807c478bd9Sstevel@tonic-gate 	}
40817c478bd9Sstevel@tonic-gate }
40827c478bd9Sstevel@tonic-gate 
40837c478bd9Sstevel@tonic-gate void
growinit(char * devstr)40847c478bd9Sstevel@tonic-gate growinit(char *devstr)
40857c478bd9Sstevel@tonic-gate {
40867c478bd9Sstevel@tonic-gate 	int	i;
40877c478bd9Sstevel@tonic-gate 	char	buf[DEV_BSIZE];
40887c478bd9Sstevel@tonic-gate 
40897c478bd9Sstevel@tonic-gate 	/*
40907c478bd9Sstevel@tonic-gate 	 * Read and verify the superblock
40917c478bd9Sstevel@tonic-gate 	 */
40927c478bd9Sstevel@tonic-gate 	rdfs((diskaddr_t)(SBOFF / sectorsize), (int)sbsize, (char *)&sblock);
40936451fdbcSvsakar 	(void) checksblock(sblock, 0);
40947c478bd9Sstevel@tonic-gate 	if (sblock.fs_postblformat != FS_DYNAMICPOSTBLFMT) {
40957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4096d50c8f90Svsakar 		    gettext("old file system format; can't growfs\n"));
40977c478bd9Sstevel@tonic-gate 		lockexit(32);
40987c478bd9Sstevel@tonic-gate 	}
40997c478bd9Sstevel@tonic-gate 
41007c478bd9Sstevel@tonic-gate 	/*
41017c478bd9Sstevel@tonic-gate 	 * can't shrink a file system
41027c478bd9Sstevel@tonic-gate 	 */
41037c478bd9Sstevel@tonic-gate 	grow_fssize = fsbtodb(&sblock, (uint64_t)sblock.fs_size);
41047c478bd9Sstevel@tonic-gate 	if (fssize_db < grow_fssize) {
41057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
41067c478bd9Sstevel@tonic-gate 		    gettext("%lld sectors < current size of %lld sectors\n"),
41077c478bd9Sstevel@tonic-gate 		    fssize_db, grow_fssize);
41087c478bd9Sstevel@tonic-gate 		lockexit(32);
41097c478bd9Sstevel@tonic-gate 	}
41107c478bd9Sstevel@tonic-gate 
41117c478bd9Sstevel@tonic-gate 	/*
41127c478bd9Sstevel@tonic-gate 	 * can't grow a system to over a terabyte unless it was set up
41137c478bd9Sstevel@tonic-gate 	 * as an MTB UFS file system.
41147c478bd9Sstevel@tonic-gate 	 */
41157c478bd9Sstevel@tonic-gate 	if (mtb == 'y' && sblock.fs_magic != MTB_UFS_MAGIC) {
41167c478bd9Sstevel@tonic-gate 		if (fssize_db >= SECTORS_PER_TERABYTE) {
41177c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
41187c478bd9Sstevel@tonic-gate "File system was not set up with the multi-terabyte format.\n"));
41197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
41207c478bd9Sstevel@tonic-gate "Its size cannot be increased to a terabyte or more.\n"));
41217c478bd9Sstevel@tonic-gate 		} else {
41227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
41237c478bd9Sstevel@tonic-gate "Cannot convert file system to multi-terabyte format.\n"));
41247c478bd9Sstevel@tonic-gate 		}
41257c478bd9Sstevel@tonic-gate 		lockexit(32);
41267c478bd9Sstevel@tonic-gate 	}
41277c478bd9Sstevel@tonic-gate 
41287c478bd9Sstevel@tonic-gate 	logsetup(devstr);
41297c478bd9Sstevel@tonic-gate 
41307c478bd9Sstevel@tonic-gate 	/*
41317c478bd9Sstevel@tonic-gate 	 * can't growfs when logging device has errors
41327c478bd9Sstevel@tonic-gate 	 */
41337c478bd9Sstevel@tonic-gate 	if ((islog && !islogok) ||
41347c478bd9Sstevel@tonic-gate 	    ((FSOKAY == (sblock.fs_state + sblock.fs_time)) &&
41357c478bd9Sstevel@tonic-gate 	    (sblock.fs_clean == FSLOG && !islog))) {
41367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4137d50c8f90Svsakar 		    gettext("logging device has errors; can't growfs\n"));
41387c478bd9Sstevel@tonic-gate 		lockexit(32);
41397c478bd9Sstevel@tonic-gate 	}
41407c478bd9Sstevel@tonic-gate 
41417c478bd9Sstevel@tonic-gate 	/*
41427c478bd9Sstevel@tonic-gate 	 * disable ufs logging for growing
41437c478bd9Sstevel@tonic-gate 	 */
41445f10ef69SYuri Pankov 	if (islog) {
41457c478bd9Sstevel@tonic-gate 		if (rl_log_control(devstr, _FIOLOGDISABLE) != RL_SUCCESS) {
41467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4147d50c8f90Svsakar 			    "failed to disable logging\n"));
41487c478bd9Sstevel@tonic-gate 			lockexit(32);
41497c478bd9Sstevel@tonic-gate 		}
41507c478bd9Sstevel@tonic-gate 		islog = 0;
41517c478bd9Sstevel@tonic-gate 		waslog = 1;
41527c478bd9Sstevel@tonic-gate 	}
41537c478bd9Sstevel@tonic-gate 
41547c478bd9Sstevel@tonic-gate 	/*
41557c478bd9Sstevel@tonic-gate 	 * if mounted write lock the file system to be grown
41567c478bd9Sstevel@tonic-gate 	 */
41577c478bd9Sstevel@tonic-gate 	if (ismounted)
41587c478bd9Sstevel@tonic-gate 		wlockfs();
41597c478bd9Sstevel@tonic-gate 
41607c478bd9Sstevel@tonic-gate 	/*
41617c478bd9Sstevel@tonic-gate 	 * refresh dynamic superblock state - disabling logging will have
41627c478bd9Sstevel@tonic-gate 	 * changed the amount of free space available in the file system
41637c478bd9Sstevel@tonic-gate 	 */
41647c478bd9Sstevel@tonic-gate 	rdfs((diskaddr_t)(SBOFF / sectorsize), sbsize, (char *)&sblock);
41657c478bd9Sstevel@tonic-gate 
41667c478bd9Sstevel@tonic-gate 	/*
41677c478bd9Sstevel@tonic-gate 	 * make sure device is big enough
41687c478bd9Sstevel@tonic-gate 	 */
41697c478bd9Sstevel@tonic-gate 	rdfs((diskaddr_t)fssize_db - 1, DEV_BSIZE, buf);
41707c478bd9Sstevel@tonic-gate 	wtfs((diskaddr_t)fssize_db - 1, DEV_BSIZE, buf);
41717c478bd9Sstevel@tonic-gate 
41727c478bd9Sstevel@tonic-gate 	/*
41737c478bd9Sstevel@tonic-gate 	 * read current summary information
41747c478bd9Sstevel@tonic-gate 	 */
41757c478bd9Sstevel@tonic-gate 	grow_fscs = read_summaryinfo(&sblock);
41767c478bd9Sstevel@tonic-gate 
41777c478bd9Sstevel@tonic-gate 	/*
41787c478bd9Sstevel@tonic-gate 	 * save some current size related fields from the superblock
41797c478bd9Sstevel@tonic-gate 	 * These are used in extendsummaryinfo()
41807c478bd9Sstevel@tonic-gate 	 */
41817c478bd9Sstevel@tonic-gate 	grow_fs_size	= sblock.fs_size;
41827c478bd9Sstevel@tonic-gate 	grow_fs_ncg	= sblock.fs_ncg;
41837c478bd9Sstevel@tonic-gate 	grow_fs_csaddr	= (diskaddr_t)sblock.fs_csaddr;
41847c478bd9Sstevel@tonic-gate 	grow_fs_cssize	= sblock.fs_cssize;
41857c478bd9Sstevel@tonic-gate 
41867c478bd9Sstevel@tonic-gate 	/*
41877c478bd9Sstevel@tonic-gate 	 * save and reset the clean flag
41887c478bd9Sstevel@tonic-gate 	 */
41897c478bd9Sstevel@tonic-gate 	if (FSOKAY == (sblock.fs_state + sblock.fs_time))
41907c478bd9Sstevel@tonic-gate 		grow_fs_clean = sblock.fs_clean;
41917c478bd9Sstevel@tonic-gate 	else
41927c478bd9Sstevel@tonic-gate 		grow_fs_clean = FSBAD;
41937c478bd9Sstevel@tonic-gate 	sblock.fs_clean = FSBAD;
41947c478bd9Sstevel@tonic-gate 	sblock.fs_state = FSOKAY - sblock.fs_time;
41957c478bd9Sstevel@tonic-gate 	isbad = 1;
41967c478bd9Sstevel@tonic-gate 	wtfs((diskaddr_t)(SBOFF / sectorsize), sbsize, (char *)&sblock);
41977c478bd9Sstevel@tonic-gate }
41987c478bd9Sstevel@tonic-gate 
41997c478bd9Sstevel@tonic-gate void
checkdev(char * rdev,char * bdev)42007c478bd9Sstevel@tonic-gate checkdev(char *rdev, char *bdev)
42017c478bd9Sstevel@tonic-gate {
42027c478bd9Sstevel@tonic-gate 	struct stat64	statarea;
42037c478bd9Sstevel@tonic-gate 
42047c478bd9Sstevel@tonic-gate 	if (stat64(bdev, &statarea) < 0) {
42057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("can't check mount point; "));
42067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("can't stat %s\n"), bdev);
42077c478bd9Sstevel@tonic-gate 		lockexit(32);
42087c478bd9Sstevel@tonic-gate 	}
42097c478bd9Sstevel@tonic-gate 	if ((statarea.st_mode & S_IFMT) != S_IFBLK) {
42107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
42117c478bd9Sstevel@tonic-gate 		    "can't check mount point; %s is not a block device\n"),
42127c478bd9Sstevel@tonic-gate 		    bdev);
42137c478bd9Sstevel@tonic-gate 		lockexit(32);
42147c478bd9Sstevel@tonic-gate 	}
42157c478bd9Sstevel@tonic-gate 	if (stat64(rdev, &statarea) < 0) {
42167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("can't stat %s\n"), rdev);
42177c478bd9Sstevel@tonic-gate 		lockexit(32);
42187c478bd9Sstevel@tonic-gate 	}
42197c478bd9Sstevel@tonic-gate 	if ((statarea.st_mode & S_IFMT) != S_IFCHR) {
42207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4221d50c8f90Svsakar 		    gettext("%s is not a character device\n"), rdev);
42227c478bd9Sstevel@tonic-gate 		lockexit(32);
42237c478bd9Sstevel@tonic-gate 	}
42247c478bd9Sstevel@tonic-gate }
42257c478bd9Sstevel@tonic-gate 
42267c478bd9Sstevel@tonic-gate void
checkmount(struct mnttab * mntp,char * bdevname)42277c478bd9Sstevel@tonic-gate checkmount(struct mnttab *mntp, char *bdevname)
42287c478bd9Sstevel@tonic-gate {
42297c478bd9Sstevel@tonic-gate 	struct stat64	statdir;
42307c478bd9Sstevel@tonic-gate 	struct stat64	statdev;
42317c478bd9Sstevel@tonic-gate 
42327c478bd9Sstevel@tonic-gate 	if (strcmp(bdevname, mntp->mnt_special) == 0) {
42337c478bd9Sstevel@tonic-gate 		if (stat64(mntp->mnt_mountp, &statdir) == -1) {
42347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("can't stat %s\n"),
4235d50c8f90Svsakar 			    mntp->mnt_mountp);
42367c478bd9Sstevel@tonic-gate 			lockexit(32);
42377c478bd9Sstevel@tonic-gate 		}
42387c478bd9Sstevel@tonic-gate 		if (stat64(mntp->mnt_special, &statdev) == -1) {
42397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("can't stat %s\n"),
4240d50c8f90Svsakar 			    mntp->mnt_special);
42417c478bd9Sstevel@tonic-gate 			lockexit(32);
42427c478bd9Sstevel@tonic-gate 		}
42437c478bd9Sstevel@tonic-gate 		if (statdir.st_dev != statdev.st_rdev) {
42447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4245*bbf21555SRichard Lowe 			    "%s is not mounted on %s; mnttab(5) wrong\n"),
4246d50c8f90Svsakar 			    mntp->mnt_special, mntp->mnt_mountp);
42477c478bd9Sstevel@tonic-gate 			lockexit(32);
42487c478bd9Sstevel@tonic-gate 		}
42497c478bd9Sstevel@tonic-gate 		ismounted = 1;
42507c478bd9Sstevel@tonic-gate 		if (directory) {
42517c478bd9Sstevel@tonic-gate 			if (strcmp(mntp->mnt_mountp, directory) != 0) {
42527c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4253d50c8f90Svsakar 				    gettext("%s is mounted on %s, not %s\n"),
4254355d6bb5Sswilcox 				    bdevname, mntp->mnt_mountp, directory);
42557c478bd9Sstevel@tonic-gate 				lockexit(32);
42567c478bd9Sstevel@tonic-gate 			}
42577c478bd9Sstevel@tonic-gate 		} else {
42587c478bd9Sstevel@tonic-gate 			if (grow)
42597c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4260355d6bb5Sswilcox 				    "%s is mounted on %s; can't growfs\n"),
4261355d6bb5Sswilcox 				    bdevname, mntp->mnt_mountp);
42627c478bd9Sstevel@tonic-gate 			else
42637c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4264355d6bb5Sswilcox 				    gettext("%s is mounted, can't mkfs\n"),
4265355d6bb5Sswilcox 				    bdevname);
42667c478bd9Sstevel@tonic-gate 			lockexit(32);
42677c478bd9Sstevel@tonic-gate 		}
42687c478bd9Sstevel@tonic-gate 	}
42697c478bd9Sstevel@tonic-gate }
42707c478bd9Sstevel@tonic-gate 
42717c478bd9Sstevel@tonic-gate struct dinode	*dibuf	= 0;
42727c478bd9Sstevel@tonic-gate diskaddr_t	difrag	= 0;
42737c478bd9Sstevel@tonic-gate 
42747c478bd9Sstevel@tonic-gate struct dinode *
gdinode(ino_t ino)42757c478bd9Sstevel@tonic-gate gdinode(ino_t ino)
42767c478bd9Sstevel@tonic-gate {
42777c478bd9Sstevel@tonic-gate 	/*
42787c478bd9Sstevel@tonic-gate 	 * read the block of inodes containing inode number ino
42797c478bd9Sstevel@tonic-gate 	 */
42807c478bd9Sstevel@tonic-gate 	if (dibuf == 0)
42817c478bd9Sstevel@tonic-gate 		dibuf = (struct dinode *)malloc((unsigned)sblock.fs_bsize);
42827c478bd9Sstevel@tonic-gate 	if (itod(&sblock, ino) != difrag) {
42837c478bd9Sstevel@tonic-gate 		difrag = itod(&sblock, ino);
42847c478bd9Sstevel@tonic-gate 		rdfs(fsbtodb(&sblock, (uint64_t)difrag), (int)sblock.fs_bsize,
4285d50c8f90Svsakar 		    (char *)dibuf);
42867c478bd9Sstevel@tonic-gate 	}
42877c478bd9Sstevel@tonic-gate 	return (dibuf + (ino % INOPB(&sblock)));
42887c478bd9Sstevel@tonic-gate }
42897c478bd9Sstevel@tonic-gate 
42907c478bd9Sstevel@tonic-gate /*
42917c478bd9Sstevel@tonic-gate  * structure that manages the frags we need for extended summary info
42927c478bd9Sstevel@tonic-gate  *	These frags can be:
42937c478bd9Sstevel@tonic-gate  *		free
42947c478bd9Sstevel@tonic-gate  *		data  block
42957c478bd9Sstevel@tonic-gate  *		alloc block
42967c478bd9Sstevel@tonic-gate  */
42977c478bd9Sstevel@tonic-gate struct csfrag {
42987c478bd9Sstevel@tonic-gate 	struct csfrag	*next;		/* next entry */
42997c478bd9Sstevel@tonic-gate 	daddr32_t	 ofrag;		/* old frag */
43007c478bd9Sstevel@tonic-gate 	daddr32_t	 nfrag;		/* new frag */
43017c478bd9Sstevel@tonic-gate 	long		 cylno;		/* cylno of nfrag */
43027c478bd9Sstevel@tonic-gate 	long		 frags;		/* number of frags */
43037c478bd9Sstevel@tonic-gate 	long		 size;		/* size in bytes */
43047c478bd9Sstevel@tonic-gate 	ino_t		 ino;		/* inode number */
43057c478bd9Sstevel@tonic-gate 	long		 fixed;		/* Boolean - Already fixed? */
43067c478bd9Sstevel@tonic-gate };
43077c478bd9Sstevel@tonic-gate struct csfrag	*csfrag;		/* state unknown */
43087c478bd9Sstevel@tonic-gate struct csfrag	*csfragino;		/* frags belonging to an inode */
43097c478bd9Sstevel@tonic-gate struct csfrag	*csfragfree;		/* frags that are free */
43107c478bd9Sstevel@tonic-gate 
43117c478bd9Sstevel@tonic-gate daddr32_t maxcsfrag	= 0;		/* maximum in range */
43127c478bd9Sstevel@tonic-gate daddr32_t mincsfrag	= 0x7fffffff;	/* minimum in range */
43137c478bd9Sstevel@tonic-gate 
43147c478bd9Sstevel@tonic-gate int
csfraginrange(daddr32_t frag)43157c478bd9Sstevel@tonic-gate csfraginrange(daddr32_t frag)
43167c478bd9Sstevel@tonic-gate {
43177c478bd9Sstevel@tonic-gate 	return ((frag >= mincsfrag) && (frag <= maxcsfrag));
43187c478bd9Sstevel@tonic-gate }
43197c478bd9Sstevel@tonic-gate 
43207c478bd9Sstevel@tonic-gate struct csfrag *
findcsfrag(daddr32_t frag,struct csfrag ** cfap)43217c478bd9Sstevel@tonic-gate findcsfrag(daddr32_t frag, struct csfrag **cfap)
43227c478bd9Sstevel@tonic-gate {
43237c478bd9Sstevel@tonic-gate 	struct csfrag	*cfp;
43247c478bd9Sstevel@tonic-gate 
43257c478bd9Sstevel@tonic-gate 	if (!csfraginrange(frag))
43267c478bd9Sstevel@tonic-gate 		return (NULL);
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate 	for (cfp = *cfap; cfp; cfp = cfp->next)
43297c478bd9Sstevel@tonic-gate 		if (cfp->ofrag == frag)
43307c478bd9Sstevel@tonic-gate 			return (cfp);
43317c478bd9Sstevel@tonic-gate 	return (NULL);
43327c478bd9Sstevel@tonic-gate }
43337c478bd9Sstevel@tonic-gate 
43347c478bd9Sstevel@tonic-gate void
checkindirect(ino_t ino,daddr32_t * fragsp,daddr32_t frag,int level)43357c478bd9Sstevel@tonic-gate checkindirect(ino_t ino, daddr32_t *fragsp, daddr32_t frag, int level)
43367c478bd9Sstevel@tonic-gate {
43377c478bd9Sstevel@tonic-gate 	int			i;
43387c478bd9Sstevel@tonic-gate 	int			ne	= sblock.fs_bsize / sizeof (daddr32_t);
43397c478bd9Sstevel@tonic-gate 	daddr32_t			fsb[MAXBSIZE / sizeof (daddr32_t)];
43407c478bd9Sstevel@tonic-gate 
43417c478bd9Sstevel@tonic-gate 	if (frag == 0)
43427c478bd9Sstevel@tonic-gate 		return;
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate 	rdfs(fsbtodb(&sblock, frag), (int)sblock.fs_bsize,
43457c478bd9Sstevel@tonic-gate 	    (char *)fsb);
43467c478bd9Sstevel@tonic-gate 
43477c478bd9Sstevel@tonic-gate 	checkdirect(ino, fragsp, fsb, sblock.fs_bsize / sizeof (daddr32_t));
43487c478bd9Sstevel@tonic-gate 
43497c478bd9Sstevel@tonic-gate 	if (level)
43507c478bd9Sstevel@tonic-gate 		for (i = 0; i < ne && *fragsp; ++i)
43517c478bd9Sstevel@tonic-gate 			checkindirect(ino, fragsp, fsb[i], level-1);
43527c478bd9Sstevel@tonic-gate }
43537c478bd9Sstevel@tonic-gate 
43547c478bd9Sstevel@tonic-gate void
addcsfrag(ino_t ino,daddr32_t frag,struct csfrag ** cfap)43557c478bd9Sstevel@tonic-gate addcsfrag(ino_t ino, daddr32_t frag, struct csfrag **cfap)
43567c478bd9Sstevel@tonic-gate {
43577c478bd9Sstevel@tonic-gate 	struct csfrag	*cfp, *curr, *prev;
43587c478bd9Sstevel@tonic-gate 
43597c478bd9Sstevel@tonic-gate 	/*
43607c478bd9Sstevel@tonic-gate 	 * establish a range for faster checking in csfraginrange()
43617c478bd9Sstevel@tonic-gate 	 */
43627c478bd9Sstevel@tonic-gate 	if (frag > maxcsfrag)
43637c478bd9Sstevel@tonic-gate 		maxcsfrag = frag;
43647c478bd9Sstevel@tonic-gate 	if (frag < mincsfrag)
43657c478bd9Sstevel@tonic-gate 		mincsfrag = frag;
43667c478bd9Sstevel@tonic-gate 
43677c478bd9Sstevel@tonic-gate 	/*
43687c478bd9Sstevel@tonic-gate 	 * if this frag belongs to an inode and is not the start of a block
43697c478bd9Sstevel@tonic-gate 	 *	then see if it is part of a frag range for this inode
43707c478bd9Sstevel@tonic-gate 	 */
43717c478bd9Sstevel@tonic-gate 	if (ino && (frag % sblock.fs_frag))
43727c478bd9Sstevel@tonic-gate 		for (cfp = *cfap; cfp; cfp = cfp->next) {
43737c478bd9Sstevel@tonic-gate 			if (ino != cfp->ino)
43747c478bd9Sstevel@tonic-gate 				continue;
43757c478bd9Sstevel@tonic-gate 			if (frag != cfp->ofrag + cfp->frags)
43767c478bd9Sstevel@tonic-gate 				continue;
43777c478bd9Sstevel@tonic-gate 			cfp->frags++;
43787c478bd9Sstevel@tonic-gate 			cfp->size += sblock.fs_fsize;
43797c478bd9Sstevel@tonic-gate 			return;
43807c478bd9Sstevel@tonic-gate 		}
43817c478bd9Sstevel@tonic-gate 	/*
43827c478bd9Sstevel@tonic-gate 	 * allocate a csfrag entry and insert it in an increasing order into the
43837c478bd9Sstevel@tonic-gate 	 * specified list
43847c478bd9Sstevel@tonic-gate 	 */
43857c478bd9Sstevel@tonic-gate 	cfp = (struct csfrag *)calloc(1, sizeof (struct csfrag));
43867c478bd9Sstevel@tonic-gate 	cfp->ino	= ino;
43877c478bd9Sstevel@tonic-gate 	cfp->ofrag	= frag;
43887c478bd9Sstevel@tonic-gate 	cfp->frags	= 1;
43897c478bd9Sstevel@tonic-gate 	cfp->size	= sblock.fs_fsize;
43907c478bd9Sstevel@tonic-gate 	for (prev = NULL, curr = *cfap; curr != NULL;
4391d50c8f90Svsakar 	    prev = curr, curr = curr->next) {
43927c478bd9Sstevel@tonic-gate 		if (frag < curr->ofrag) {
43937c478bd9Sstevel@tonic-gate 			cfp->next = curr;
43947c478bd9Sstevel@tonic-gate 			if (prev)
43957c478bd9Sstevel@tonic-gate 				prev->next = cfp;	/* middle element */
43967c478bd9Sstevel@tonic-gate 			else
43977c478bd9Sstevel@tonic-gate 				*cfap = cfp;		/* first element */
43987c478bd9Sstevel@tonic-gate 			break;
43997c478bd9Sstevel@tonic-gate 		}
44007c478bd9Sstevel@tonic-gate 		if (curr->next == NULL) {
44017c478bd9Sstevel@tonic-gate 			curr->next = cfp;		/* last element	*/
44027c478bd9Sstevel@tonic-gate 			break;
44037c478bd9Sstevel@tonic-gate 		}
44047c478bd9Sstevel@tonic-gate 	}
44057c478bd9Sstevel@tonic-gate 	if (*cfap == NULL)	/* will happen only once */
44067c478bd9Sstevel@tonic-gate 		*cfap = cfp;
44077c478bd9Sstevel@tonic-gate }
44087c478bd9Sstevel@tonic-gate 
44097c478bd9Sstevel@tonic-gate void
delcsfrag(daddr32_t frag,struct csfrag ** cfap)44107c478bd9Sstevel@tonic-gate delcsfrag(daddr32_t frag, struct csfrag **cfap)
44117c478bd9Sstevel@tonic-gate {
44127c478bd9Sstevel@tonic-gate 	struct csfrag	*cfp;
44137c478bd9Sstevel@tonic-gate 	struct csfrag	**cfpp;
44147c478bd9Sstevel@tonic-gate 
44157c478bd9Sstevel@tonic-gate 	/*
44167c478bd9Sstevel@tonic-gate 	 * free up entry whose beginning frag matches
44177c478bd9Sstevel@tonic-gate 	 */
44187c478bd9Sstevel@tonic-gate 	for (cfpp = cfap; *cfpp; cfpp = &(*cfpp)->next) {
44197c478bd9Sstevel@tonic-gate 		if (frag == (*cfpp)->ofrag) {
44207c478bd9Sstevel@tonic-gate 			cfp = *cfpp;
44217c478bd9Sstevel@tonic-gate 			*cfpp = (*cfpp)->next;
44227c478bd9Sstevel@tonic-gate 			free((char *)cfp);
44237c478bd9Sstevel@tonic-gate 			return;
44247c478bd9Sstevel@tonic-gate 		}
44257c478bd9Sstevel@tonic-gate 	}
44267c478bd9Sstevel@tonic-gate }
44277c478bd9Sstevel@tonic-gate 
44287c478bd9Sstevel@tonic-gate /*
44297c478bd9Sstevel@tonic-gate  * See whether any of the direct blocks in the array pointed by "db" and of
44307c478bd9Sstevel@tonic-gate  * length "ne" are within the range of frags needed to extend the cylinder
44317c478bd9Sstevel@tonic-gate  * summary. If so, remove those frags from the "as-yet-unclassified" list
44327c478bd9Sstevel@tonic-gate  * (csfrag) and add them to the "owned-by-inode" list (csfragino).
44337c478bd9Sstevel@tonic-gate  * For each such frag found, decrement the frag count pointed to by fragsp.
44347c478bd9Sstevel@tonic-gate  * "ino" is the inode that contains (either directly or indirectly) the frags
44357c478bd9Sstevel@tonic-gate  * being checked.
44367c478bd9Sstevel@tonic-gate  */
44377c478bd9Sstevel@tonic-gate void
checkdirect(ino_t ino,daddr32_t * fragsp,daddr32_t * db,int ne)44387c478bd9Sstevel@tonic-gate checkdirect(ino_t ino, daddr32_t *fragsp, daddr32_t *db, int ne)
44397c478bd9Sstevel@tonic-gate {
44407c478bd9Sstevel@tonic-gate 	int	 i;
44417c478bd9Sstevel@tonic-gate 	int	 j;
44427c478bd9Sstevel@tonic-gate 	int	 found;
44437c478bd9Sstevel@tonic-gate 	diskaddr_t	 frag;
44447c478bd9Sstevel@tonic-gate 
44457c478bd9Sstevel@tonic-gate 	/*
44467c478bd9Sstevel@tonic-gate 	 * scan for allocation within the new summary info range
44477c478bd9Sstevel@tonic-gate 	 */
44487c478bd9Sstevel@tonic-gate 	for (i = 0; i < ne && *fragsp; ++i) {
44497c478bd9Sstevel@tonic-gate 		if ((frag = *db++) != 0) {
44507c478bd9Sstevel@tonic-gate 			found = 0;
44517c478bd9Sstevel@tonic-gate 			for (j = 0; j < sblock.fs_frag && *fragsp; ++j) {
44527c478bd9Sstevel@tonic-gate 				if (found || (found = csfraginrange(frag))) {
44537c478bd9Sstevel@tonic-gate 					addcsfrag(ino, frag, &csfragino);
44547c478bd9Sstevel@tonic-gate 					delcsfrag(frag, &csfrag);
44557c478bd9Sstevel@tonic-gate 				}
44567c478bd9Sstevel@tonic-gate 				++frag;
44577c478bd9Sstevel@tonic-gate 				--(*fragsp);
44587c478bd9Sstevel@tonic-gate 			}
44597c478bd9Sstevel@tonic-gate 		}
44607c478bd9Sstevel@tonic-gate 	}
44617c478bd9Sstevel@tonic-gate }
44627c478bd9Sstevel@tonic-gate 
44637c478bd9Sstevel@tonic-gate void
findcsfragino()44647c478bd9Sstevel@tonic-gate findcsfragino()
44657c478bd9Sstevel@tonic-gate {
44667c478bd9Sstevel@tonic-gate 	int		 i;
44677c478bd9Sstevel@tonic-gate 	int		 j;
44687c478bd9Sstevel@tonic-gate 	daddr32_t		 frags;
44697c478bd9Sstevel@tonic-gate 	struct dinode	*dp;
44707c478bd9Sstevel@tonic-gate 
44717c478bd9Sstevel@tonic-gate 	/*
44727c478bd9Sstevel@tonic-gate 	 * scan all old inodes looking for allocations in the new
44737c478bd9Sstevel@tonic-gate 	 * summary info range.  Move the affected frag from the
44747c478bd9Sstevel@tonic-gate 	 * generic csfrag list onto the `owned-by-inode' list csfragino.
44757c478bd9Sstevel@tonic-gate 	 */
44767c478bd9Sstevel@tonic-gate 	for (i = UFSROOTINO; i < grow_fs_ncg*sblock.fs_ipg && csfrag; ++i) {
44777c478bd9Sstevel@tonic-gate 		dp = gdinode((ino_t)i);
44787c478bd9Sstevel@tonic-gate 		switch (dp->di_mode & IFMT) {
44797c478bd9Sstevel@tonic-gate 			case IFSHAD	:
4480fe12dc75SToomas Soome 			case IFLNK	:
4481fe12dc75SToomas Soome 			case IFDIR	:
4482fe12dc75SToomas Soome 			case IFREG	: break;
44837c478bd9Sstevel@tonic-gate 			default		: continue;
44847c478bd9Sstevel@tonic-gate 		}
44857c478bd9Sstevel@tonic-gate 
44867c478bd9Sstevel@tonic-gate 		frags   = dbtofsb(&sblock, dp->di_blocks);
44877c478bd9Sstevel@tonic-gate 
44887c478bd9Sstevel@tonic-gate 		checkdirect((ino_t)i, &frags, &dp->di_db[0], NDADDR+NIADDR);
4489303bf60bSsdebnath 		for (j = 0; j < NIADDR && frags; ++j) {
4490303bf60bSsdebnath 			/* Negate the block if its an fallocate'd block */
4491303bf60bSsdebnath 			if (dp->di_ib[j] < 0 && dp->di_ib[j] != UFS_HOLE)
4492303bf60bSsdebnath 				checkindirect((ino_t)i, &frags,
4493303bf60bSsdebnath 				    -(dp->di_ib[j]), j);
4494303bf60bSsdebnath 			else
4495303bf60bSsdebnath 				checkindirect((ino_t)i, &frags,
4496303bf60bSsdebnath 				    dp->di_ib[j], j);
4497303bf60bSsdebnath 		}
44987c478bd9Sstevel@tonic-gate 	}
44997c478bd9Sstevel@tonic-gate }
45007c478bd9Sstevel@tonic-gate 
45017c478bd9Sstevel@tonic-gate void
fixindirect(daddr32_t frag,int level)45027c478bd9Sstevel@tonic-gate fixindirect(daddr32_t frag, int level)
45037c478bd9Sstevel@tonic-gate {
45047c478bd9Sstevel@tonic-gate 	int			 i;
45057c478bd9Sstevel@tonic-gate 	int			 ne	= sblock.fs_bsize / sizeof (daddr32_t);
45067c478bd9Sstevel@tonic-gate 	daddr32_t			fsb[MAXBSIZE / sizeof (daddr32_t)];
45077c478bd9Sstevel@tonic-gate 
45087c478bd9Sstevel@tonic-gate 	if (frag == 0)
45097c478bd9Sstevel@tonic-gate 		return;
45107c478bd9Sstevel@tonic-gate 
45117c478bd9Sstevel@tonic-gate 	rdfs(fsbtodb(&sblock, (uint64_t)frag), (int)sblock.fs_bsize,
45127c478bd9Sstevel@tonic-gate 	    (char *)fsb);
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate 	fixdirect((caddr_t)fsb, frag, fsb, ne);
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate 	if (level)
45177c478bd9Sstevel@tonic-gate 		for (i = 0; i < ne; ++i)
45187c478bd9Sstevel@tonic-gate 			fixindirect(fsb[i], level-1);
45197c478bd9Sstevel@tonic-gate }
45207c478bd9Sstevel@tonic-gate 
45217c478bd9Sstevel@tonic-gate void
fixdirect(caddr_t bp,daddr32_t frag,daddr32_t * db,int ne)45227c478bd9Sstevel@tonic-gate fixdirect(caddr_t bp, daddr32_t frag, daddr32_t *db, int ne)
45237c478bd9Sstevel@tonic-gate {
45247c478bd9Sstevel@tonic-gate 	int	 i;
45257c478bd9Sstevel@tonic-gate 	struct csfrag	*cfp;
45267c478bd9Sstevel@tonic-gate 
45277c478bd9Sstevel@tonic-gate 	for (i = 0; i < ne; ++i, ++db) {
45287c478bd9Sstevel@tonic-gate 		if (*db == 0)
45297c478bd9Sstevel@tonic-gate 			continue;
45307c478bd9Sstevel@tonic-gate 		if ((cfp = findcsfrag(*db, &csfragino)) == NULL)
45317c478bd9Sstevel@tonic-gate 			continue;
45327c478bd9Sstevel@tonic-gate 		*db = cfp->nfrag;
45337c478bd9Sstevel@tonic-gate 		cfp->fixed = 1;
45347c478bd9Sstevel@tonic-gate 		wtfs(fsbtodb(&sblock, (uint64_t)frag), (int)sblock.fs_bsize,
45357c478bd9Sstevel@tonic-gate 		    bp);
45367c478bd9Sstevel@tonic-gate 	}
45377c478bd9Sstevel@tonic-gate }
45387c478bd9Sstevel@tonic-gate 
45397c478bd9Sstevel@tonic-gate void
fixcsfragino()45407c478bd9Sstevel@tonic-gate fixcsfragino()
45417c478bd9Sstevel@tonic-gate {
45427c478bd9Sstevel@tonic-gate 	int		 i;
45437c478bd9Sstevel@tonic-gate 	struct dinode	*dp;
45447c478bd9Sstevel@tonic-gate 	struct csfrag	*cfp;
45457c478bd9Sstevel@tonic-gate 
45467c478bd9Sstevel@tonic-gate 	for (cfp = csfragino; cfp; cfp = cfp->next) {
45477c478bd9Sstevel@tonic-gate 		if (cfp->fixed)
45487c478bd9Sstevel@tonic-gate 			continue;
45497c478bd9Sstevel@tonic-gate 		dp = gdinode((ino_t)cfp->ino);
45507c478bd9Sstevel@tonic-gate 		fixdirect((caddr_t)dibuf, difrag, dp->di_db, NDADDR+NIADDR);
45517c478bd9Sstevel@tonic-gate 		for (i = 0; i < NIADDR; ++i)
45527c478bd9Sstevel@tonic-gate 			fixindirect(dp->di_ib[i], i);
45537c478bd9Sstevel@tonic-gate 	}
45547c478bd9Sstevel@tonic-gate }
45557c478bd9Sstevel@tonic-gate 
45567c478bd9Sstevel@tonic-gate /*
45577c478bd9Sstevel@tonic-gate  * Read the cylinders summary information specified by settings in the
45587c478bd9Sstevel@tonic-gate  * passed 'fs' structure into a new allocated array of csum structures.
45597c478bd9Sstevel@tonic-gate  * The caller is responsible for freeing the returned array.
45607c478bd9Sstevel@tonic-gate  * Return a pointer to an array of csum structures.
45617c478bd9Sstevel@tonic-gate  */
45627c478bd9Sstevel@tonic-gate static struct csum *
read_summaryinfo(struct fs * fsp)45637c478bd9Sstevel@tonic-gate read_summaryinfo(struct	fs *fsp)
45647c478bd9Sstevel@tonic-gate {
4565fe12dc75SToomas Soome 	struct csum	*csp;
45667c478bd9Sstevel@tonic-gate 	int		i;
45677c478bd9Sstevel@tonic-gate 
45687c478bd9Sstevel@tonic-gate 	if ((csp = malloc((size_t)fsp->fs_cssize)) == NULL) {
45697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("cannot create csum list,"
4570d50c8f90Svsakar 		    " not enough memory\n"));
45717c478bd9Sstevel@tonic-gate 		exit(32);
45727c478bd9Sstevel@tonic-gate 	}
45737c478bd9Sstevel@tonic-gate 
45747c478bd9Sstevel@tonic-gate 	for (i = 0; i < fsp->fs_cssize; i += fsp->fs_bsize) {
45757c478bd9Sstevel@tonic-gate 		rdfs(fsbtodb(fsp,
4576d50c8f90Svsakar 		    (uint64_t)(fsp->fs_csaddr + numfrags(fsp, i))),
4577d50c8f90Svsakar 		    (int)(fsp->fs_cssize - i < fsp->fs_bsize ?
4578d50c8f90Svsakar 		    fsp->fs_cssize - i : fsp->fs_bsize), ((caddr_t)csp) + i);
45797c478bd9Sstevel@tonic-gate 	}
45807c478bd9Sstevel@tonic-gate 
45817c478bd9Sstevel@tonic-gate 	return (csp);
45827c478bd9Sstevel@tonic-gate }
45837c478bd9Sstevel@tonic-gate 
45847c478bd9Sstevel@tonic-gate /*
45857c478bd9Sstevel@tonic-gate  * Check the allocation of fragments that are to be made part of a csum block.
45867c478bd9Sstevel@tonic-gate  * A fragment is allocated if it is either in the csfragfree list or, it is
45877c478bd9Sstevel@tonic-gate  * in the csfragino list and has new frags associated with it.
45887c478bd9Sstevel@tonic-gate  * Return the number of allocated fragments.
45897c478bd9Sstevel@tonic-gate  */
45907c478bd9Sstevel@tonic-gate int64_t
checkfragallocated(daddr32_t frag)45917c478bd9Sstevel@tonic-gate checkfragallocated(daddr32_t frag)
45927c478bd9Sstevel@tonic-gate {
4593fe12dc75SToomas Soome 	struct	csfrag	*cfp;
45947c478bd9Sstevel@tonic-gate 	/*
45957c478bd9Sstevel@tonic-gate 	 * Since the lists are sorted we can break the search if the asked
45967c478bd9Sstevel@tonic-gate 	 * frag is smaller then the one in the list.
45977c478bd9Sstevel@tonic-gate 	 */
45987c478bd9Sstevel@tonic-gate 	for (cfp = csfragfree; cfp != NULL && frag >= cfp->ofrag;
4599d50c8f90Svsakar 	    cfp = cfp->next) {
46007c478bd9Sstevel@tonic-gate 		if (frag == cfp->ofrag)
46017c478bd9Sstevel@tonic-gate 			return (1);
46027c478bd9Sstevel@tonic-gate 	}
46037c478bd9Sstevel@tonic-gate 	for (cfp = csfragino; cfp != NULL && frag >= cfp->ofrag;
4604d50c8f90Svsakar 	    cfp = cfp->next) {
46057c478bd9Sstevel@tonic-gate 		if (frag == cfp->ofrag && cfp->nfrag != 0)
46067c478bd9Sstevel@tonic-gate 			return (cfp->frags);
46077c478bd9Sstevel@tonic-gate 	}
46087c478bd9Sstevel@tonic-gate 
46097c478bd9Sstevel@tonic-gate 	return (0);
46107c478bd9Sstevel@tonic-gate }
46117c478bd9Sstevel@tonic-gate 
46127c478bd9Sstevel@tonic-gate /*
46137c478bd9Sstevel@tonic-gate  * Figure out how much the filesystem can be grown. The limiting factor is
46147c478bd9Sstevel@tonic-gate  * the available free space needed to extend the cg summary info block.
46157c478bd9Sstevel@tonic-gate  * The free space is determined in three steps:
46167c478bd9Sstevel@tonic-gate  * - Try to extend the cg summary block to the required size.
46177c478bd9Sstevel@tonic-gate  * - Find free blocks in last cg.
46187c478bd9Sstevel@tonic-gate  * - Find free space in the last already allocated fragment of the summary info
46197c478bd9Sstevel@tonic-gate  *   block, and use it for additional csum structures.
46207c478bd9Sstevel@tonic-gate  * Return the maximum size of the new filesystem or 0 if it can't be grown.
46217c478bd9Sstevel@tonic-gate  * Please note that this function leaves the global list pointers csfrag,
46227c478bd9Sstevel@tonic-gate  * csfragfree, and csfragino initialized, and the caller is responsible for
46237c478bd9Sstevel@tonic-gate  * freeing the lists.
46247c478bd9Sstevel@tonic-gate  */
46257c478bd9Sstevel@tonic-gate diskaddr_t
probe_summaryinfo()46267c478bd9Sstevel@tonic-gate probe_summaryinfo()
46277c478bd9Sstevel@tonic-gate {
46287c478bd9Sstevel@tonic-gate 	/* fragments by which the csum block can be extended. */
4629fe12dc75SToomas Soome 	int64_t		growth_csum_frags = 0;
46307c478bd9Sstevel@tonic-gate 	/* fragments by which the filesystem can be extended. */
46317c478bd9Sstevel@tonic-gate 	int64_t		growth_fs_frags = 0;
46327c478bd9Sstevel@tonic-gate 	int64_t		new_fs_cssize;	/* size of csum blk in the new FS */
46337c478bd9Sstevel@tonic-gate 	int64_t		new_fs_ncg;	/* number of cg in the new FS */
4634fe12dc75SToomas Soome 	int64_t		spare_csum;
46357c478bd9Sstevel@tonic-gate 	daddr32_t	oldfrag_daddr;
46367c478bd9Sstevel@tonic-gate 	daddr32_t	newfrag_daddr;
46377c478bd9Sstevel@tonic-gate 	daddr32_t	daddr;
46387c478bd9Sstevel@tonic-gate 	int		i;
46397c478bd9Sstevel@tonic-gate 
46407c478bd9Sstevel@tonic-gate 	/*
46417c478bd9Sstevel@tonic-gate 	 * read and verify the superblock
46427c478bd9Sstevel@tonic-gate 	 */
46437c478bd9Sstevel@tonic-gate 	rdfs((diskaddr_t)(SBOFF / sectorsize), (int)sbsize, (char *)&sblock);
46446451fdbcSvsakar 	(void) checksblock(sblock, 0);
46457c478bd9Sstevel@tonic-gate 
46467c478bd9Sstevel@tonic-gate 	/*
46477c478bd9Sstevel@tonic-gate 	 * check how much we can extend the cg summary info block
46487c478bd9Sstevel@tonic-gate 	 */
46497c478bd9Sstevel@tonic-gate 
46507c478bd9Sstevel@tonic-gate 	/*
46517c478bd9Sstevel@tonic-gate 	 * read current summary information
46527c478bd9Sstevel@tonic-gate 	 */
46537c478bd9Sstevel@tonic-gate 	fscs = read_summaryinfo(&sblock);
46547c478bd9Sstevel@tonic-gate 
46557c478bd9Sstevel@tonic-gate 	/*
46567c478bd9Sstevel@tonic-gate 	 * build list of frags needed for cg summary info block extension
46577c478bd9Sstevel@tonic-gate 	 */
46587c478bd9Sstevel@tonic-gate 	oldfrag_daddr = howmany(sblock.fs_cssize, sblock.fs_fsize) +
4659d50c8f90Svsakar 	    sblock.fs_csaddr;
46607c478bd9Sstevel@tonic-gate 	new_fs_ncg = howmany(dbtofsb(&sblock, fssize_db), sblock.fs_fpg);
46617c478bd9Sstevel@tonic-gate 	new_fs_cssize = fragroundup(&sblock, new_fs_ncg * sizeof (struct csum));
46627c478bd9Sstevel@tonic-gate 	newfrag_daddr = howmany(new_fs_cssize, sblock.fs_fsize) +
4663d50c8f90Svsakar 	    sblock.fs_csaddr;
46647c478bd9Sstevel@tonic-gate 	/*
46657c478bd9Sstevel@tonic-gate 	 * add all of the frags that are required to grow the cyl summary to the
46667c478bd9Sstevel@tonic-gate 	 * csfrag list, which is the generic/unknown list, since at this point
46677c478bd9Sstevel@tonic-gate 	 * we don't yet know the state of those frags.
46687c478bd9Sstevel@tonic-gate 	 */
46697c478bd9Sstevel@tonic-gate 	for (daddr = oldfrag_daddr; daddr < newfrag_daddr; daddr++)
46707c478bd9Sstevel@tonic-gate 		addcsfrag((ino_t)0, daddr, &csfrag);
46717c478bd9Sstevel@tonic-gate 
46727c478bd9Sstevel@tonic-gate 	/*
46737c478bd9Sstevel@tonic-gate 	 * filter free fragments and allocate them. Note that the free frags
46747c478bd9Sstevel@tonic-gate 	 * must be allocated first otherwise they could be grabbed by
46757c478bd9Sstevel@tonic-gate 	 * alloccsfragino() for data frags.
46767c478bd9Sstevel@tonic-gate 	 */
46777c478bd9Sstevel@tonic-gate 	findcsfragfree();
46787c478bd9Sstevel@tonic-gate 	alloccsfragfree();
46797c478bd9Sstevel@tonic-gate 
46807c478bd9Sstevel@tonic-gate 	/*
46817c478bd9Sstevel@tonic-gate 	 * filter fragments owned by inodes and allocate them
46827c478bd9Sstevel@tonic-gate 	 */
46837c478bd9Sstevel@tonic-gate 	grow_fs_ncg = sblock.fs_ncg; /* findcsfragino() needs this glob. var. */
46847c478bd9Sstevel@tonic-gate 	findcsfragino();
46857c478bd9Sstevel@tonic-gate 	alloccsfragino();
46867c478bd9Sstevel@tonic-gate 
46877c478bd9Sstevel@tonic-gate 	if (notenoughspace()) {
46887c478bd9Sstevel@tonic-gate 		/*
46897c478bd9Sstevel@tonic-gate 		 * check how many consecutive fragments could be allocated
46907c478bd9Sstevel@tonic-gate 		 * in both lists.
46917c478bd9Sstevel@tonic-gate 		 */
46927c478bd9Sstevel@tonic-gate 		int64_t tmp_frags;
46937c478bd9Sstevel@tonic-gate 		for (daddr = oldfrag_daddr; daddr < newfrag_daddr;
4694d50c8f90Svsakar 		    daddr += tmp_frags) {
46957c478bd9Sstevel@tonic-gate 			if ((tmp_frags = checkfragallocated(daddr)) > 0)
46967c478bd9Sstevel@tonic-gate 				growth_csum_frags += tmp_frags;
46977c478bd9Sstevel@tonic-gate 			else
46987c478bd9Sstevel@tonic-gate 				break;
46997c478bd9Sstevel@tonic-gate 		}
47007c478bd9Sstevel@tonic-gate 	} else {
47017c478bd9Sstevel@tonic-gate 		/*
47027c478bd9Sstevel@tonic-gate 		 * We have all we need for the new desired size,
47037c478bd9Sstevel@tonic-gate 		 * so clean up and report back.
47047c478bd9Sstevel@tonic-gate 		 */
47057c478bd9Sstevel@tonic-gate 		return (fssize_db);
47067c478bd9Sstevel@tonic-gate 	}
47077c478bd9Sstevel@tonic-gate 
47087c478bd9Sstevel@tonic-gate 	/*
47097c478bd9Sstevel@tonic-gate 	 * given the number of fragments by which the csum block can be grown
47107c478bd9Sstevel@tonic-gate 	 * compute by how many new fragments the FS can be increased.
47117c478bd9Sstevel@tonic-gate 	 * It is the number of csum instances per fragment multiplied by
47127c478bd9Sstevel@tonic-gate 	 * `growth_csum_frags' and the number of fragments per cylinder group.
47137c478bd9Sstevel@tonic-gate 	 */
47147c478bd9Sstevel@tonic-gate 	growth_fs_frags = howmany(sblock.fs_fsize, sizeof (struct csum)) *
4715d50c8f90Svsakar 	    growth_csum_frags * sblock.fs_fpg;
47167c478bd9Sstevel@tonic-gate 
47177c478bd9Sstevel@tonic-gate 	/*
47187c478bd9Sstevel@tonic-gate 	 * compute free fragments in the last cylinder group
47197c478bd9Sstevel@tonic-gate 	 */
47207c478bd9Sstevel@tonic-gate 	rdcg(sblock.fs_ncg - 1);
47217c478bd9Sstevel@tonic-gate 	growth_fs_frags += sblock.fs_fpg - acg.cg_ndblk;
47227c478bd9Sstevel@tonic-gate 
47237c478bd9Sstevel@tonic-gate 	/*
47247c478bd9Sstevel@tonic-gate 	 * compute how many csum instances are unused in the old csum block.
47257c478bd9Sstevel@tonic-gate 	 * For each unused csum instance the FS can be grown by one cylinder
47267c478bd9Sstevel@tonic-gate 	 * group without extending the csum block.
47277c478bd9Sstevel@tonic-gate 	 */
47287c478bd9Sstevel@tonic-gate 	spare_csum = howmany(sblock.fs_cssize, sizeof (struct csum)) -
4729d50c8f90Svsakar 	    sblock.fs_ncg;
47307c478bd9Sstevel@tonic-gate 	if (spare_csum > 0)
47317c478bd9Sstevel@tonic-gate 		growth_fs_frags += spare_csum * sblock.fs_fpg;
47327c478bd9Sstevel@tonic-gate 
47337c478bd9Sstevel@tonic-gate 	/*
47347c478bd9Sstevel@tonic-gate 	 * recalculate the new filesystem size in sectors, shorten it by
47357c478bd9Sstevel@tonic-gate 	 * the requested size `fssize_db' if necessary.
47367c478bd9Sstevel@tonic-gate 	 */
47377c478bd9Sstevel@tonic-gate 	if (growth_fs_frags > 0) {
47387c478bd9Sstevel@tonic-gate 		diskaddr_t sect;
47397c478bd9Sstevel@tonic-gate 		sect = (sblock.fs_size + growth_fs_frags) * sblock.fs_nspf;
47407c478bd9Sstevel@tonic-gate 		return ((sect > fssize_db) ? fssize_db : sect);
47417c478bd9Sstevel@tonic-gate 	}
47427c478bd9Sstevel@tonic-gate 
47437c478bd9Sstevel@tonic-gate 	return (0);
47447c478bd9Sstevel@tonic-gate }
47457c478bd9Sstevel@tonic-gate 
47467c478bd9Sstevel@tonic-gate void
extendsummaryinfo()47477c478bd9Sstevel@tonic-gate extendsummaryinfo()
47487c478bd9Sstevel@tonic-gate {
47497c478bd9Sstevel@tonic-gate 	int64_t		i;
47507c478bd9Sstevel@tonic-gate 	int		localtest	= test;
47517c478bd9Sstevel@tonic-gate 	int64_t		frags;
47527c478bd9Sstevel@tonic-gate 	daddr32_t		oldfrag;
47537c478bd9Sstevel@tonic-gate 	daddr32_t		newfrag;
47547c478bd9Sstevel@tonic-gate 
47557c478bd9Sstevel@tonic-gate 	/*
47567c478bd9Sstevel@tonic-gate 	 * if no-write (-N), don't bother
47577c478bd9Sstevel@tonic-gate 	 */
47587c478bd9Sstevel@tonic-gate 	if (Nflag)
47597c478bd9Sstevel@tonic-gate 		return;
47607c478bd9Sstevel@tonic-gate 
47617c478bd9Sstevel@tonic-gate again:
47627c478bd9Sstevel@tonic-gate 	flcg();
47637c478bd9Sstevel@tonic-gate 	/*
47647c478bd9Sstevel@tonic-gate 	 * summary info did not change size -- do nothing unless in test mode
47657c478bd9Sstevel@tonic-gate 	 */
47667c478bd9Sstevel@tonic-gate 	if (grow_fs_cssize == sblock.fs_cssize)
47677c478bd9Sstevel@tonic-gate 		if (!localtest)
47687c478bd9Sstevel@tonic-gate 			return;
47697c478bd9Sstevel@tonic-gate 
47707c478bd9Sstevel@tonic-gate 	/*
47717c478bd9Sstevel@tonic-gate 	 * build list of frags needed for additional summary information
47727c478bd9Sstevel@tonic-gate 	 */
47737c478bd9Sstevel@tonic-gate 	oldfrag = howmany(grow_fs_cssize, sblock.fs_fsize) + grow_fs_csaddr;
47747c478bd9Sstevel@tonic-gate 	newfrag = howmany(sblock.fs_cssize, sblock.fs_fsize) + grow_fs_csaddr;
47757c478bd9Sstevel@tonic-gate 	/*
47767c478bd9Sstevel@tonic-gate 	 * add all of the frags that are required to grow the cyl summary to the
47777c478bd9Sstevel@tonic-gate 	 * csfrag list, which is the generic/unknown list, since at this point
47787c478bd9Sstevel@tonic-gate 	 * we don't yet know the state of those frags.
47797c478bd9Sstevel@tonic-gate 	 */
47807c478bd9Sstevel@tonic-gate 	for (i = oldfrag, frags = 0; i < newfrag; ++i, ++frags)
47817c478bd9Sstevel@tonic-gate 		addcsfrag((ino_t)0, (diskaddr_t)i, &csfrag);
47827c478bd9Sstevel@tonic-gate 	/*
47837c478bd9Sstevel@tonic-gate 	 * reduce the number of data blocks in the file system (fs_dsize) by
47847c478bd9Sstevel@tonic-gate 	 * the number of frags that need to be added to the cyl summary
47857c478bd9Sstevel@tonic-gate 	 */
47867c478bd9Sstevel@tonic-gate 	sblock.fs_dsize -= (newfrag - oldfrag);
47877c478bd9Sstevel@tonic-gate 
47887c478bd9Sstevel@tonic-gate 	/*
47897c478bd9Sstevel@tonic-gate 	 * In test mode, we move more data than necessary from
47907c478bd9Sstevel@tonic-gate 	 * cylinder group 0.  The lookup/allocate/move code can be
47917c478bd9Sstevel@tonic-gate 	 * better stressed without having to create HUGE file systems.
47927c478bd9Sstevel@tonic-gate 	 */
47937c478bd9Sstevel@tonic-gate 	if (localtest)
47947c478bd9Sstevel@tonic-gate 		for (i = newfrag; i < grow_sifrag; ++i) {
47957c478bd9Sstevel@tonic-gate 			if (frags >= testfrags)
47967c478bd9Sstevel@tonic-gate 				break;
47977c478bd9Sstevel@tonic-gate 			frags++;
47987c478bd9Sstevel@tonic-gate 			addcsfrag((ino_t)0, (diskaddr_t)i, &csfrag);
47997c478bd9Sstevel@tonic-gate 		}
48007c478bd9Sstevel@tonic-gate 
48017c478bd9Sstevel@tonic-gate 	/*
48027c478bd9Sstevel@tonic-gate 	 * move frags to free or inode lists, depending on owner
48037c478bd9Sstevel@tonic-gate 	 */
48047c478bd9Sstevel@tonic-gate 	findcsfragfree();
48057c478bd9Sstevel@tonic-gate 	findcsfragino();
48067c478bd9Sstevel@tonic-gate 
48077c478bd9Sstevel@tonic-gate 	/*
48087c478bd9Sstevel@tonic-gate 	 * if not all frags can be located, file system must be inconsistent
48097c478bd9Sstevel@tonic-gate 	 */
48107c478bd9Sstevel@tonic-gate 	if (csfrag) {
48117c478bd9Sstevel@tonic-gate 		isbad = 1;	/* should already be set, but make sure */
48127c478bd9Sstevel@tonic-gate 		lockexit(32);
48137c478bd9Sstevel@tonic-gate 	}
48147c478bd9Sstevel@tonic-gate 
48157c478bd9Sstevel@tonic-gate 	/*
48167c478bd9Sstevel@tonic-gate 	 * allocate the free frags. Note that the free frags must be allocated
48177c478bd9Sstevel@tonic-gate 	 * first otherwise they could be grabbed by alloccsfragino() for data
48187c478bd9Sstevel@tonic-gate 	 * frags.
48197c478bd9Sstevel@tonic-gate 	 */
48207c478bd9Sstevel@tonic-gate 	alloccsfragfree();
48217c478bd9Sstevel@tonic-gate 	/*
48227c478bd9Sstevel@tonic-gate 	 * allocate extra space for inode frags
48237c478bd9Sstevel@tonic-gate 	 */
48247c478bd9Sstevel@tonic-gate 	alloccsfragino();
48257c478bd9Sstevel@tonic-gate 
48267c478bd9Sstevel@tonic-gate 	/*
48277c478bd9Sstevel@tonic-gate 	 * not enough space
48287c478bd9Sstevel@tonic-gate 	 */
48297c478bd9Sstevel@tonic-gate 	if (notenoughspace()) {
48307c478bd9Sstevel@tonic-gate 		unalloccsfragfree();
48317c478bd9Sstevel@tonic-gate 		unalloccsfragino();
48327c478bd9Sstevel@tonic-gate 		if (localtest && !testforce) {
48337c478bd9Sstevel@tonic-gate 			localtest = 0;
48347c478bd9Sstevel@tonic-gate 			goto again;
48357c478bd9Sstevel@tonic-gate 		}
48367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Not enough free space\n"));
48377c478bd9Sstevel@tonic-gate 		lockexit(NOTENOUGHSPACE);
48387c478bd9Sstevel@tonic-gate 	}
48397c478bd9Sstevel@tonic-gate 
48407c478bd9Sstevel@tonic-gate 	/*
48417c478bd9Sstevel@tonic-gate 	 * copy the data from old frags to new frags
48427c478bd9Sstevel@tonic-gate 	 */
48437c478bd9Sstevel@tonic-gate 	copycsfragino();
48447c478bd9Sstevel@tonic-gate 
48457c478bd9Sstevel@tonic-gate 	/*
48467c478bd9Sstevel@tonic-gate 	 * fix the inodes to point to the new frags
48477c478bd9Sstevel@tonic-gate 	 */
48487c478bd9Sstevel@tonic-gate 	fixcsfragino();
48497c478bd9Sstevel@tonic-gate 
48507c478bd9Sstevel@tonic-gate 	/*
48517c478bd9Sstevel@tonic-gate 	 * We may have moved more frags than we needed.  Free them.
48527c478bd9Sstevel@tonic-gate 	 */
48537c478bd9Sstevel@tonic-gate 	rdcg((long)0);
48547c478bd9Sstevel@tonic-gate 	for (i = newfrag; i <= maxcsfrag; ++i)
48557c478bd9Sstevel@tonic-gate 		setbit(cg_blksfree(&acg), i-cgbase(&sblock, 0));
48567c478bd9Sstevel@tonic-gate 	wtcg();
48577c478bd9Sstevel@tonic-gate 
48587c478bd9Sstevel@tonic-gate 	flcg();
48597c478bd9Sstevel@tonic-gate }
48607c478bd9Sstevel@tonic-gate 
48617c478bd9Sstevel@tonic-gate /*
48627c478bd9Sstevel@tonic-gate  * Check if all fragments in the `csfragino' list were reallocated.
48637c478bd9Sstevel@tonic-gate  */
48647c478bd9Sstevel@tonic-gate int
notenoughspace()48657c478bd9Sstevel@tonic-gate notenoughspace()
48667c478bd9Sstevel@tonic-gate {
48677c478bd9Sstevel@tonic-gate 	struct csfrag	*cfp;
48687c478bd9Sstevel@tonic-gate 
48697c478bd9Sstevel@tonic-gate 	/*
48707c478bd9Sstevel@tonic-gate 	 * If any element in the csfragino array has a "new frag location"
48717c478bd9Sstevel@tonic-gate 	 * of 0, the allocfrags() function was unsuccessful in allocating
48727c478bd9Sstevel@tonic-gate 	 * space for moving the frag represented by this array element.
48737c478bd9Sstevel@tonic-gate 	 */
48747c478bd9Sstevel@tonic-gate 	for (cfp = csfragino; cfp; cfp = cfp->next)
48757c478bd9Sstevel@tonic-gate 		if (cfp->nfrag == 0)
48767c478bd9Sstevel@tonic-gate 			return (1);
48777c478bd9Sstevel@tonic-gate 	return (0);
48787c478bd9Sstevel@tonic-gate }
48797c478bd9Sstevel@tonic-gate 
48807c478bd9Sstevel@tonic-gate void
unalloccsfragino()48817c478bd9Sstevel@tonic-gate unalloccsfragino()
48827c478bd9Sstevel@tonic-gate {
48837c478bd9Sstevel@tonic-gate 	struct csfrag	*cfp;
48847c478bd9Sstevel@tonic-gate 
48857c478bd9Sstevel@tonic-gate 	while ((cfp = csfragino) != NULL) {
48867c478bd9Sstevel@tonic-gate 		if (cfp->nfrag)
48877c478bd9Sstevel@tonic-gate 			freefrags(cfp->nfrag, cfp->frags, cfp->cylno);
48887c478bd9Sstevel@tonic-gate 		delcsfrag(cfp->ofrag, &csfragino);
48897c478bd9Sstevel@tonic-gate 	}
48907c478bd9Sstevel@tonic-gate }
48917c478bd9Sstevel@tonic-gate 
48927c478bd9Sstevel@tonic-gate void
unalloccsfragfree()48937c478bd9Sstevel@tonic-gate unalloccsfragfree()
48947c478bd9Sstevel@tonic-gate {
48957c478bd9Sstevel@tonic-gate 	struct csfrag	*cfp;
48967c478bd9Sstevel@tonic-gate 
48977c478bd9Sstevel@tonic-gate 	while ((cfp = csfragfree) != NULL) {
48987c478bd9Sstevel@tonic-gate 		freefrags(cfp->ofrag, cfp->frags, cfp->cylno);
48997c478bd9Sstevel@tonic-gate 		delcsfrag(cfp->ofrag, &csfragfree);
49007c478bd9Sstevel@tonic-gate 	}
49017c478bd9Sstevel@tonic-gate }
49027c478bd9Sstevel@tonic-gate 
49037c478bd9Sstevel@tonic-gate /*
49047c478bd9Sstevel@tonic-gate  * For each frag in the "as-yet-unclassified" list (csfrag), see if
49057c478bd9Sstevel@tonic-gate  * it's free (i.e., its bit is set in the free frag bit map).  If so,
49067c478bd9Sstevel@tonic-gate  * move it from the "as-yet-unclassified" list to the csfragfree list.
49077c478bd9Sstevel@tonic-gate  */
49087c478bd9Sstevel@tonic-gate void
findcsfragfree()49097c478bd9Sstevel@tonic-gate findcsfragfree()
49107c478bd9Sstevel@tonic-gate {
49117c478bd9Sstevel@tonic-gate 	struct csfrag	*cfp;
49127c478bd9Sstevel@tonic-gate 	struct csfrag	*cfpnext;
49137c478bd9Sstevel@tonic-gate 
49147c478bd9Sstevel@tonic-gate 	/*
49157c478bd9Sstevel@tonic-gate 	 * move free frags onto the free-frag list
49167c478bd9Sstevel@tonic-gate 	 */
49177c478bd9Sstevel@tonic-gate 	rdcg((long)0);
49187c478bd9Sstevel@tonic-gate 	for (cfp = csfrag; cfp; cfp = cfpnext) {
49197c478bd9Sstevel@tonic-gate 		cfpnext = cfp->next;
49207c478bd9Sstevel@tonic-gate 		if (isset(cg_blksfree(&acg), cfp->ofrag - cgbase(&sblock, 0))) {
49217c478bd9Sstevel@tonic-gate 			addcsfrag(cfp->ino, cfp->ofrag, &csfragfree);
49227c478bd9Sstevel@tonic-gate 			delcsfrag(cfp->ofrag, &csfrag);
49237c478bd9Sstevel@tonic-gate 		}
49247c478bd9Sstevel@tonic-gate 	}
49257c478bd9Sstevel@tonic-gate }
49267c478bd9Sstevel@tonic-gate 
49277c478bd9Sstevel@tonic-gate void
copycsfragino()49287c478bd9Sstevel@tonic-gate copycsfragino()
49297c478bd9Sstevel@tonic-gate {
49307c478bd9Sstevel@tonic-gate 	struct csfrag	*cfp;
49317c478bd9Sstevel@tonic-gate 	char		buf[MAXBSIZE];
49327c478bd9Sstevel@tonic-gate 
49337c478bd9Sstevel@tonic-gate 	/*
49347c478bd9Sstevel@tonic-gate 	 * copy data from old frags to newly allocated frags
49357c478bd9Sstevel@tonic-gate 	 */
49367c478bd9Sstevel@tonic-gate 	for (cfp = csfragino; cfp; cfp = cfp->next) {
49377c478bd9Sstevel@tonic-gate 		rdfs(fsbtodb(&sblock, (uint64_t)cfp->ofrag), (int)cfp->size,
49387c478bd9Sstevel@tonic-gate 		    buf);
49397c478bd9Sstevel@tonic-gate 		wtfs(fsbtodb(&sblock, (uint64_t)cfp->nfrag), (int)cfp->size,
49407c478bd9Sstevel@tonic-gate 		    buf);
49417c478bd9Sstevel@tonic-gate 	}
49427c478bd9Sstevel@tonic-gate }
49437c478bd9Sstevel@tonic-gate 
49447c478bd9Sstevel@tonic-gate long	curcylno	= -1;
49457c478bd9Sstevel@tonic-gate int	cylnodirty	= 0;
49467c478bd9Sstevel@tonic-gate 
49477c478bd9Sstevel@tonic-gate void
rdcg(long cylno)49487c478bd9Sstevel@tonic-gate rdcg(long cylno)
49497c478bd9Sstevel@tonic-gate {
49507c478bd9Sstevel@tonic-gate 	if (cylno != curcylno) {
49517c478bd9Sstevel@tonic-gate 		flcg();
49527c478bd9Sstevel@tonic-gate 		curcylno = cylno;
49537c478bd9Sstevel@tonic-gate 		rdfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, curcylno)),
4954d50c8f90Svsakar 		    (int)sblock.fs_cgsize, (char *)&acg);
49557c478bd9Sstevel@tonic-gate 	}
49567c478bd9Sstevel@tonic-gate }
49577c478bd9Sstevel@tonic-gate 
49587c478bd9Sstevel@tonic-gate void
flcg()49597c478bd9Sstevel@tonic-gate flcg()
49607c478bd9Sstevel@tonic-gate {
49617c478bd9Sstevel@tonic-gate 	if (cylnodirty) {
49627c478bd9Sstevel@tonic-gate 		if (debug && Pflag) {
49637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4964d50c8f90Svsakar 			    "Assert: cylnodirty set in probe mode\n");
49657c478bd9Sstevel@tonic-gate 			return;
49667c478bd9Sstevel@tonic-gate 		}
49677c478bd9Sstevel@tonic-gate 		resetallocinfo();
49687c478bd9Sstevel@tonic-gate 		wtfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, curcylno)),
4969d50c8f90Svsakar 		    (int)sblock.fs_cgsize, (char *)&acg);
49707c478bd9Sstevel@tonic-gate 		cylnodirty = 0;
49717c478bd9Sstevel@tonic-gate 	}
49727c478bd9Sstevel@tonic-gate 	curcylno = -1;
49737c478bd9Sstevel@tonic-gate }
49747c478bd9Sstevel@tonic-gate 
49757c478bd9Sstevel@tonic-gate void
wtcg()49767c478bd9Sstevel@tonic-gate wtcg()
49777c478bd9Sstevel@tonic-gate {
49787c478bd9Sstevel@tonic-gate 	if (!Pflag) {
49797c478bd9Sstevel@tonic-gate 		/* probe mode should never write to disk */
49807c478bd9Sstevel@tonic-gate 		cylnodirty = 1;
49817c478bd9Sstevel@tonic-gate 	}
49827c478bd9Sstevel@tonic-gate }
49837c478bd9Sstevel@tonic-gate 
49847c478bd9Sstevel@tonic-gate void
allocfrags(long frags,daddr32_t * fragp,long * cylnop)49857c478bd9Sstevel@tonic-gate allocfrags(long frags, daddr32_t *fragp, long *cylnop)
49867c478bd9Sstevel@tonic-gate {
49877c478bd9Sstevel@tonic-gate 	int	 i;
49887c478bd9Sstevel@tonic-gate 	int	 j;
49897c478bd9Sstevel@tonic-gate 	long	 bits;
49907c478bd9Sstevel@tonic-gate 	long	 bit;
49917c478bd9Sstevel@tonic-gate 
49927c478bd9Sstevel@tonic-gate 	/*
49937c478bd9Sstevel@tonic-gate 	 * Allocate a free-frag range in an old cylinder group
49947c478bd9Sstevel@tonic-gate 	 */
49957c478bd9Sstevel@tonic-gate 	for (i = 0, *fragp = 0; i < grow_fs_ncg; ++i) {
49967c478bd9Sstevel@tonic-gate 		if (((fscs+i)->cs_nffree < frags) && ((fscs+i)->cs_nbfree == 0))
49977c478bd9Sstevel@tonic-gate 			continue;
49987c478bd9Sstevel@tonic-gate 		rdcg((long)i);
49997c478bd9Sstevel@tonic-gate 		bit = bits = 0;
50007c478bd9Sstevel@tonic-gate 		while (findfreerange(&bit, &bits)) {
50017c478bd9Sstevel@tonic-gate 			if (frags <= bits)  {
50027c478bd9Sstevel@tonic-gate 				for (j = 0; j < frags; ++j)
50037c478bd9Sstevel@tonic-gate 					clrbit(cg_blksfree(&acg), bit+j);
50047c478bd9Sstevel@tonic-gate 				wtcg();
50057c478bd9Sstevel@tonic-gate 				*cylnop = i;
50067c478bd9Sstevel@tonic-gate 				*fragp  = bit + cgbase(&sblock, i);
50077c478bd9Sstevel@tonic-gate 				return;
50087c478bd9Sstevel@tonic-gate 			}
50097c478bd9Sstevel@tonic-gate 			bit += bits;
50107c478bd9Sstevel@tonic-gate 		}
50117c478bd9Sstevel@tonic-gate 	}
50127c478bd9Sstevel@tonic-gate }
50137c478bd9Sstevel@tonic-gate 
50147c478bd9Sstevel@tonic-gate /*
50157c478bd9Sstevel@tonic-gate  * Allocate space for frags that need to be moved in order to free up space for
50167c478bd9Sstevel@tonic-gate  * expanding the cylinder summary info.
50177c478bd9Sstevel@tonic-gate  * For each frag that needs to be moved (each frag or range of frags in
50187c478bd9Sstevel@tonic-gate  * the csfragino list), allocate a new location and store the frag number
50197c478bd9Sstevel@tonic-gate  * of that new location in the nfrag field of the csfrag struct.
50207c478bd9Sstevel@tonic-gate  * If a new frag can't be allocated for any element in the csfragino list,
50217c478bd9Sstevel@tonic-gate  * set the new frag number for that element to 0 and return immediately.
50227c478bd9Sstevel@tonic-gate  * The notenoughspace() function will detect this condition.
50237c478bd9Sstevel@tonic-gate  */
50247c478bd9Sstevel@tonic-gate void
alloccsfragino()50257c478bd9Sstevel@tonic-gate alloccsfragino()
50267c478bd9Sstevel@tonic-gate {
50277c478bd9Sstevel@tonic-gate 	struct csfrag	*cfp;
50287c478bd9Sstevel@tonic-gate 
50297c478bd9Sstevel@tonic-gate 	/*
50307c478bd9Sstevel@tonic-gate 	 * allocate space for inode frag ranges
50317c478bd9Sstevel@tonic-gate 	 */
50327c478bd9Sstevel@tonic-gate 	for (cfp = csfragino; cfp; cfp = cfp->next) {
50337c478bd9Sstevel@tonic-gate 		allocfrags(cfp->frags, &cfp->nfrag, &cfp->cylno);
50347c478bd9Sstevel@tonic-gate 		if (cfp->nfrag == 0)
50357c478bd9Sstevel@tonic-gate 			break;
50367c478bd9Sstevel@tonic-gate 	}
50377c478bd9Sstevel@tonic-gate }
50387c478bd9Sstevel@tonic-gate 
50397c478bd9Sstevel@tonic-gate void
alloccsfragfree()50407c478bd9Sstevel@tonic-gate alloccsfragfree()
50417c478bd9Sstevel@tonic-gate {
50427c478bd9Sstevel@tonic-gate 	struct csfrag	*cfp;
50437c478bd9Sstevel@tonic-gate 
50447c478bd9Sstevel@tonic-gate 	/*
50457c478bd9Sstevel@tonic-gate 	 * allocate the free frags needed for extended summary info
50467c478bd9Sstevel@tonic-gate 	 */
50477c478bd9Sstevel@tonic-gate 	rdcg((long)0);
50487c478bd9Sstevel@tonic-gate 
50497c478bd9Sstevel@tonic-gate 	for (cfp = csfragfree; cfp; cfp = cfp->next)
50507c478bd9Sstevel@tonic-gate 		clrbit(cg_blksfree(&acg), cfp->ofrag - cgbase(&sblock, 0));
50517c478bd9Sstevel@tonic-gate 
50527c478bd9Sstevel@tonic-gate 	wtcg();
50537c478bd9Sstevel@tonic-gate }
50547c478bd9Sstevel@tonic-gate 
50557c478bd9Sstevel@tonic-gate void
freefrags(daddr32_t frag,long frags,long cylno)50567c478bd9Sstevel@tonic-gate freefrags(daddr32_t frag, long frags, long cylno)
50577c478bd9Sstevel@tonic-gate {
50587c478bd9Sstevel@tonic-gate 	int	i;
50597c478bd9Sstevel@tonic-gate 
50607c478bd9Sstevel@tonic-gate 	/*
50617c478bd9Sstevel@tonic-gate 	 * free frags
50627c478bd9Sstevel@tonic-gate 	 */
50637c478bd9Sstevel@tonic-gate 	rdcg(cylno);
50647c478bd9Sstevel@tonic-gate 	for (i = 0; i < frags; ++i) {
50657c478bd9Sstevel@tonic-gate 		setbit(cg_blksfree(&acg), (frag+i) - cgbase(&sblock, cylno));
50667c478bd9Sstevel@tonic-gate 	}
50677c478bd9Sstevel@tonic-gate 	wtcg();
50687c478bd9Sstevel@tonic-gate }
50697c478bd9Sstevel@tonic-gate 
50707c478bd9Sstevel@tonic-gate int
findfreerange(long * bitp,long * bitsp)50717c478bd9Sstevel@tonic-gate findfreerange(long *bitp, long *bitsp)
50727c478bd9Sstevel@tonic-gate {
50737c478bd9Sstevel@tonic-gate 	long	 bit;
50747c478bd9Sstevel@tonic-gate 
50757c478bd9Sstevel@tonic-gate 	/*
50767c478bd9Sstevel@tonic-gate 	 * find a range of free bits in a cylinder group bit map
50777c478bd9Sstevel@tonic-gate 	 */
50787c478bd9Sstevel@tonic-gate 	for (bit = *bitp, *bitsp = 0; bit < acg.cg_ndblk; ++bit)
50797c478bd9Sstevel@tonic-gate 		if (isset(cg_blksfree(&acg), bit))
50807c478bd9Sstevel@tonic-gate 			break;
50817c478bd9Sstevel@tonic-gate 
50827c478bd9Sstevel@tonic-gate 	if (bit >= acg.cg_ndblk)
50837c478bd9Sstevel@tonic-gate 		return (0);
50847c478bd9Sstevel@tonic-gate 
50857c478bd9Sstevel@tonic-gate 	*bitp  = bit;
50867c478bd9Sstevel@tonic-gate 	*bitsp = 1;
50877c478bd9Sstevel@tonic-gate 	for (++bit; bit < acg.cg_ndblk; ++bit, ++(*bitsp)) {
50887c478bd9Sstevel@tonic-gate 		if ((bit % sblock.fs_frag) == 0)
50897c478bd9Sstevel@tonic-gate 			break;
50907c478bd9Sstevel@tonic-gate 		if (isclr(cg_blksfree(&acg), bit))
50917c478bd9Sstevel@tonic-gate 			break;
50927c478bd9Sstevel@tonic-gate 	}
50937c478bd9Sstevel@tonic-gate 	return (1);
50947c478bd9Sstevel@tonic-gate }
50957c478bd9Sstevel@tonic-gate 
50967c478bd9Sstevel@tonic-gate void
resetallocinfo()50977c478bd9Sstevel@tonic-gate resetallocinfo()
50987c478bd9Sstevel@tonic-gate {
50997c478bd9Sstevel@tonic-gate 	long	cno;
51007c478bd9Sstevel@tonic-gate 	long	bit;
51017c478bd9Sstevel@tonic-gate 	long	bits;
51027c478bd9Sstevel@tonic-gate 
51037c478bd9Sstevel@tonic-gate 	/*
51047c478bd9Sstevel@tonic-gate 	 * Compute the free blocks/frags info and update the appropriate
51057c478bd9Sstevel@tonic-gate 	 * inmemory superblock, summary info, and cylinder group fields
51067c478bd9Sstevel@tonic-gate 	 */
51077c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_nffree -= acg.cg_cs.cs_nffree;
51087c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_nbfree -= acg.cg_cs.cs_nbfree;
51097c478bd9Sstevel@tonic-gate 
51107c478bd9Sstevel@tonic-gate 	acg.cg_cs.cs_nffree = 0;
51117c478bd9Sstevel@tonic-gate 	acg.cg_cs.cs_nbfree = 0;
51127c478bd9Sstevel@tonic-gate 
51137c478bd9Sstevel@tonic-gate 	bzero((caddr_t)acg.cg_frsum, sizeof (acg.cg_frsum));
51147c478bd9Sstevel@tonic-gate 	bzero((caddr_t)cg_blktot(&acg), (int)(acg.cg_iusedoff-acg.cg_btotoff));
51157c478bd9Sstevel@tonic-gate 
51167c478bd9Sstevel@tonic-gate 	bit = bits = 0;
51177c478bd9Sstevel@tonic-gate 	while (findfreerange(&bit, &bits)) {
51187c478bd9Sstevel@tonic-gate 		if (bits == sblock.fs_frag) {
51197c478bd9Sstevel@tonic-gate 			acg.cg_cs.cs_nbfree++;
51207c478bd9Sstevel@tonic-gate 			cno = cbtocylno(&sblock, bit);
51217c478bd9Sstevel@tonic-gate 			cg_blktot(&acg)[cno]++;
51227c478bd9Sstevel@tonic-gate 			cg_blks(&sblock, &acg, cno)[cbtorpos(&sblock, bit)]++;
51237c478bd9Sstevel@tonic-gate 		} else {
51247c478bd9Sstevel@tonic-gate 			acg.cg_cs.cs_nffree += bits;
51257c478bd9Sstevel@tonic-gate 			acg.cg_frsum[bits]++;
51267c478bd9Sstevel@tonic-gate 		}
51277c478bd9Sstevel@tonic-gate 		bit += bits;
51287c478bd9Sstevel@tonic-gate 	}
51297c478bd9Sstevel@tonic-gate 
51307c478bd9Sstevel@tonic-gate 	*(fscs + acg.cg_cgx) = acg.cg_cs;
51317c478bd9Sstevel@tonic-gate 
51327c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
51337c478bd9Sstevel@tonic-gate 	sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
51347c478bd9Sstevel@tonic-gate }
51357c478bd9Sstevel@tonic-gate 
51367c478bd9Sstevel@tonic-gate void
extendcg(long cylno)51377c478bd9Sstevel@tonic-gate extendcg(long cylno)
51387c478bd9Sstevel@tonic-gate {
51397c478bd9Sstevel@tonic-gate 	int	i;
51407c478bd9Sstevel@tonic-gate 	diskaddr_t	dupper;
51417c478bd9Sstevel@tonic-gate 	diskaddr_t	cbase;
51427c478bd9Sstevel@tonic-gate 	diskaddr_t	dmax;
51437c478bd9Sstevel@tonic-gate 
51447c478bd9Sstevel@tonic-gate 	/*
51457c478bd9Sstevel@tonic-gate 	 * extend the cylinder group at the end of the old file system
51467c478bd9Sstevel@tonic-gate 	 * if it was partially allocated becase of lack of space
51477c478bd9Sstevel@tonic-gate 	 */
51487c478bd9Sstevel@tonic-gate 	flcg();
51497c478bd9Sstevel@tonic-gate 	rdcg(cylno);
51507c478bd9Sstevel@tonic-gate 
51517c478bd9Sstevel@tonic-gate 	dupper = acg.cg_ndblk;
51527c478bd9Sstevel@tonic-gate 	if (cylno == sblock.fs_ncg - 1)
5153355d6bb5Sswilcox 		acg.cg_ncyl = sblock.fs_ncyl - (sblock.fs_cpg * cylno);
51547c478bd9Sstevel@tonic-gate 	else
51557c478bd9Sstevel@tonic-gate 		acg.cg_ncyl = sblock.fs_cpg;
51567c478bd9Sstevel@tonic-gate 	cbase = cgbase(&sblock, cylno);
51577c478bd9Sstevel@tonic-gate 	dmax = cbase + sblock.fs_fpg;
51587c478bd9Sstevel@tonic-gate 	if (dmax > sblock.fs_size)
51597c478bd9Sstevel@tonic-gate 		dmax = sblock.fs_size;
51607c478bd9Sstevel@tonic-gate 	acg.cg_ndblk = dmax - cbase;
51617c478bd9Sstevel@tonic-gate 
51627c478bd9Sstevel@tonic-gate 	for (i = dupper; i < acg.cg_ndblk; ++i)
51637c478bd9Sstevel@tonic-gate 		setbit(cg_blksfree(&acg), i);
51647c478bd9Sstevel@tonic-gate 
51657c478bd9Sstevel@tonic-gate 	sblock.fs_dsize += (acg.cg_ndblk - dupper);
51667c478bd9Sstevel@tonic-gate 
51677c478bd9Sstevel@tonic-gate 	wtcg();
51687c478bd9Sstevel@tonic-gate 	flcg();
51697c478bd9Sstevel@tonic-gate }
51707c478bd9Sstevel@tonic-gate 
51717c478bd9Sstevel@tonic-gate struct lockfs	lockfs;
51727c478bd9Sstevel@tonic-gate int		lockfd;
51737c478bd9Sstevel@tonic-gate int		islocked;
51747c478bd9Sstevel@tonic-gate int		lockfskey;
51757c478bd9Sstevel@tonic-gate char		lockfscomment[128];
51767c478bd9Sstevel@tonic-gate 
51777c478bd9Sstevel@tonic-gate void
ulockfs()51787c478bd9Sstevel@tonic-gate ulockfs()
51797c478bd9Sstevel@tonic-gate {
51807c478bd9Sstevel@tonic-gate 	/*
51817c478bd9Sstevel@tonic-gate 	 * if the file system was locked, unlock it before exiting
51827c478bd9Sstevel@tonic-gate 	 */
51837c478bd9Sstevel@tonic-gate 	if (islocked == 0)
51847c478bd9Sstevel@tonic-gate 		return;
51857c478bd9Sstevel@tonic-gate 
51867c478bd9Sstevel@tonic-gate 	/*
51877c478bd9Sstevel@tonic-gate 	 * first, check if the lock held
51887c478bd9Sstevel@tonic-gate 	 */
51897c478bd9Sstevel@tonic-gate 	lockfs.lf_flags = LOCKFS_MOD;
51907c478bd9Sstevel@tonic-gate 	if (ioctl(lockfd, _FIOLFSS, &lockfs) == -1) {
51917c478bd9Sstevel@tonic-gate 		perror(directory);
51927c478bd9Sstevel@tonic-gate 		lockexit(32);
51937c478bd9Sstevel@tonic-gate 	}
51947c478bd9Sstevel@tonic-gate 
51957c478bd9Sstevel@tonic-gate 	if (LOCKFS_IS_MOD(&lockfs)) {
51967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5197d50c8f90Svsakar 		    gettext("FILE SYSTEM CHANGED DURING GROWFS!\n"));
51987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5199d50c8f90Svsakar 		    gettext("   See lockfs(1), umount(1), and fsck(1)\n"));
52007c478bd9Sstevel@tonic-gate 		lockexit(32);
52017c478bd9Sstevel@tonic-gate 	}
52027c478bd9Sstevel@tonic-gate 	/*
52037c478bd9Sstevel@tonic-gate 	 * unlock the file system
52047c478bd9Sstevel@tonic-gate 	 */
52057c478bd9Sstevel@tonic-gate 	lockfs.lf_lock  = LOCKFS_ULOCK;
52067c478bd9Sstevel@tonic-gate 	lockfs.lf_flags = 0;
52077c478bd9Sstevel@tonic-gate 	lockfs.lf_key   = lockfskey;
52087c478bd9Sstevel@tonic-gate 	clockfs();
52097c478bd9Sstevel@tonic-gate 	if (ioctl(lockfd, _FIOLFS, &lockfs) == -1) {
52107c478bd9Sstevel@tonic-gate 		perror(directory);
52117c478bd9Sstevel@tonic-gate 		lockexit(32);
52127c478bd9Sstevel@tonic-gate 	}
52137c478bd9Sstevel@tonic-gate }
52147c478bd9Sstevel@tonic-gate 
52157c478bd9Sstevel@tonic-gate void
wlockfs()52167c478bd9Sstevel@tonic-gate wlockfs()
52177c478bd9Sstevel@tonic-gate {
52187c478bd9Sstevel@tonic-gate 
52197c478bd9Sstevel@tonic-gate 	/*
52207c478bd9Sstevel@tonic-gate 	 * if no-write (-N), don't bother
52217c478bd9Sstevel@tonic-gate 	 */
52227c478bd9Sstevel@tonic-gate 	if (Nflag)
52237c478bd9Sstevel@tonic-gate 		return;
52247c478bd9Sstevel@tonic-gate 	/*
52257c478bd9Sstevel@tonic-gate 	 * open the mountpoint, and write lock the file system
52267c478bd9Sstevel@tonic-gate 	 */
52277c478bd9Sstevel@tonic-gate 	if ((lockfd = open64(directory, O_RDONLY)) == -1) {
52287c478bd9Sstevel@tonic-gate 		perror(directory);
52297c478bd9Sstevel@tonic-gate 		lockexit(32);
52307c478bd9Sstevel@tonic-gate 	}
52317c478bd9Sstevel@tonic-gate 
52327c478bd9Sstevel@tonic-gate 	/*
52337c478bd9Sstevel@tonic-gate 	 * check if it is already locked
52347c478bd9Sstevel@tonic-gate 	 */
52357c478bd9Sstevel@tonic-gate 	if (ioctl(lockfd, _FIOLFSS, &lockfs) == -1) {
52367c478bd9Sstevel@tonic-gate 		perror(directory);
52377c478bd9Sstevel@tonic-gate 		lockexit(32);
52387c478bd9Sstevel@tonic-gate 	}
52397c478bd9Sstevel@tonic-gate 
52407c478bd9Sstevel@tonic-gate 	if (lockfs.lf_lock != LOCKFS_WLOCK) {
52417c478bd9Sstevel@tonic-gate 		lockfs.lf_lock  = LOCKFS_WLOCK;
52427c478bd9Sstevel@tonic-gate 		lockfs.lf_flags = 0;
52437c478bd9Sstevel@tonic-gate 		lockfs.lf_key   = 0;
52447c478bd9Sstevel@tonic-gate 		clockfs();
52457c478bd9Sstevel@tonic-gate 		if (ioctl(lockfd, _FIOLFS, &lockfs) == -1) {
52467c478bd9Sstevel@tonic-gate 			perror(directory);
52477c478bd9Sstevel@tonic-gate 			lockexit(32);
52487c478bd9Sstevel@tonic-gate 		}
52497c478bd9Sstevel@tonic-gate 	}
52507c478bd9Sstevel@tonic-gate 	islocked = 1;
52517c478bd9Sstevel@tonic-gate 	lockfskey = lockfs.lf_key;
52527c478bd9Sstevel@tonic-gate }
52537c478bd9Sstevel@tonic-gate 
52547c478bd9Sstevel@tonic-gate void
clockfs()52557c478bd9Sstevel@tonic-gate clockfs()
52567c478bd9Sstevel@tonic-gate {
52577c478bd9Sstevel@tonic-gate 	time_t	t;
52587c478bd9Sstevel@tonic-gate 	char	*ct;
52597c478bd9Sstevel@tonic-gate 
52607c478bd9Sstevel@tonic-gate 	(void) time(&t);
52617c478bd9Sstevel@tonic-gate 	ct = ctime(&t);
52627c478bd9Sstevel@tonic-gate 	ct[strlen(ct)-1] = '\0';
52637c478bd9Sstevel@tonic-gate 
52647c478bd9Sstevel@tonic-gate 	(void) sprintf(lockfscomment, "%s -- mkfs pid %d", ct, getpid());
52657c478bd9Sstevel@tonic-gate 	lockfs.lf_comlen  = strlen(lockfscomment)+1;
52667c478bd9Sstevel@tonic-gate 	lockfs.lf_comment = lockfscomment;
52677c478bd9Sstevel@tonic-gate }
52687c478bd9Sstevel@tonic-gate 
52697c478bd9Sstevel@tonic-gate /*
52707c478bd9Sstevel@tonic-gate  * Write the csum records and the superblock
52717c478bd9Sstevel@tonic-gate  */
52727c478bd9Sstevel@tonic-gate void
wtsb()52737c478bd9Sstevel@tonic-gate wtsb()
52747c478bd9Sstevel@tonic-gate {
52757c478bd9Sstevel@tonic-gate 	long	i;
52767c478bd9Sstevel@tonic-gate 
52777c478bd9Sstevel@tonic-gate 	/*
52787c478bd9Sstevel@tonic-gate 	 * write summary information
52797c478bd9Sstevel@tonic-gate 	 */
52807c478bd9Sstevel@tonic-gate 	for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
52817c478bd9Sstevel@tonic-gate 		wtfs(fsbtodb(&sblock, (uint64_t)(sblock.fs_csaddr +
5282d50c8f90Svsakar 		    numfrags(&sblock, i))),
5283d50c8f90Svsakar 		    (int)(sblock.fs_cssize - i < sblock.fs_bsize ?
5284d50c8f90Svsakar 		    sblock.fs_cssize - i : sblock.fs_bsize),
5285d50c8f90Svsakar 		    ((char *)fscs) + i);
52867c478bd9Sstevel@tonic-gate 
52877c478bd9Sstevel@tonic-gate 	/*
52887c478bd9Sstevel@tonic-gate 	 * write superblock
52897c478bd9Sstevel@tonic-gate 	 */
52907c478bd9Sstevel@tonic-gate 	sblock.fs_time = mkfstime;
52917c478bd9Sstevel@tonic-gate 	wtfs((diskaddr_t)(SBOFF / sectorsize), sbsize, (char *)&sblock);
52927c478bd9Sstevel@tonic-gate }
52937c478bd9Sstevel@tonic-gate 
52947c478bd9Sstevel@tonic-gate /*
52957c478bd9Sstevel@tonic-gate  * Verify that the optimization selection is reasonable, and advance
52967c478bd9Sstevel@tonic-gate  * the global "string" appropriately.
52977c478bd9Sstevel@tonic-gate  */
52987c478bd9Sstevel@tonic-gate static char
checkopt(char * optim)52997c478bd9Sstevel@tonic-gate checkopt(char *optim)
53007c478bd9Sstevel@tonic-gate {
53017c478bd9Sstevel@tonic-gate 	char	opt;
53027c478bd9Sstevel@tonic-gate 	int	limit = strcspn(optim, ",");
53037c478bd9Sstevel@tonic-gate 
53047c478bd9Sstevel@tonic-gate 	switch (limit) {
53057c478bd9Sstevel@tonic-gate 	case 0:	/* missing indicator (have comma or nul) */
53067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53077c478bd9Sstevel@tonic-gate 		    "mkfs: missing optimization flag reset to `t' (time)\n"));
53087c478bd9Sstevel@tonic-gate 		opt = 't';
53097c478bd9Sstevel@tonic-gate 		break;
53107c478bd9Sstevel@tonic-gate 
53117c478bd9Sstevel@tonic-gate 	case 1: /* single-character indicator */
53127c478bd9Sstevel@tonic-gate 		opt = *optim;
53137c478bd9Sstevel@tonic-gate 		if ((opt != 's') && (opt != 't')) {
53147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
53157c478bd9Sstevel@tonic-gate 		    "mkfs: bad optimization value `%c' reset to `t' (time)\n"),
53167c478bd9Sstevel@tonic-gate 			    opt);
53177c478bd9Sstevel@tonic-gate 			opt = 't';
53187c478bd9Sstevel@tonic-gate 		}
53197c478bd9Sstevel@tonic-gate 		break;
53207c478bd9Sstevel@tonic-gate 
53217c478bd9Sstevel@tonic-gate 	default: /* multi-character indicator */
53227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53237c478bd9Sstevel@tonic-gate 	    "mkfs: bad optimization value `%*.*s' reset to `t' (time)\n"),
53247c478bd9Sstevel@tonic-gate 		    limit, limit, optim);
53257c478bd9Sstevel@tonic-gate 		opt = 't';
53267c478bd9Sstevel@tonic-gate 		break;
53277c478bd9Sstevel@tonic-gate 	}
53287c478bd9Sstevel@tonic-gate 
53297c478bd9Sstevel@tonic-gate 	string += limit;
53307c478bd9Sstevel@tonic-gate 
53317c478bd9Sstevel@tonic-gate 	return (opt);
53327c478bd9Sstevel@tonic-gate }
53337c478bd9Sstevel@tonic-gate 
53347c478bd9Sstevel@tonic-gate /*
53357c478bd9Sstevel@tonic-gate  * Verify that the mtb selection is reasonable, and advance
53367c478bd9Sstevel@tonic-gate  * the global "string" appropriately.
53377c478bd9Sstevel@tonic-gate  */
53387c478bd9Sstevel@tonic-gate static char
checkmtb(char * mtbarg)53397c478bd9Sstevel@tonic-gate checkmtb(char *mtbarg)
53407c478bd9Sstevel@tonic-gate {
53417c478bd9Sstevel@tonic-gate 	char	mtbc;
53427c478bd9Sstevel@tonic-gate 	int	limit = strcspn(mtbarg, ",");
53437c478bd9Sstevel@tonic-gate 
53447c478bd9Sstevel@tonic-gate 	switch (limit) {
53457c478bd9Sstevel@tonic-gate 	case 0:	/* missing indicator (have comma or nul) */
53467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53477c478bd9Sstevel@tonic-gate 		    "mkfs: missing mtb flag reset to `n' (no mtb support)\n"));
53487c478bd9Sstevel@tonic-gate 		mtbc = 'n';
53497c478bd9Sstevel@tonic-gate 		break;
53507c478bd9Sstevel@tonic-gate 
53517c478bd9Sstevel@tonic-gate 	case 1: /* single-character indicator */
53527c478bd9Sstevel@tonic-gate 		mtbc = tolower(*mtbarg);
53537c478bd9Sstevel@tonic-gate 		if ((mtbc != 'y') && (mtbc != 'n')) {
53547c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
53557c478bd9Sstevel@tonic-gate 		    "mkfs: bad mtb value `%c' reset to `n' (no mtb support)\n"),
53567c478bd9Sstevel@tonic-gate 			    mtbc);
53577c478bd9Sstevel@tonic-gate 			mtbc = 'n';
53587c478bd9Sstevel@tonic-gate 		}
53597c478bd9Sstevel@tonic-gate 		break;
53607c478bd9Sstevel@tonic-gate 
53617c478bd9Sstevel@tonic-gate 	default: /* multi-character indicator */
53627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
53637c478bd9Sstevel@tonic-gate 	    "mkfs: bad mtb value `%*.*s' reset to `n' (no mtb support)\n"),
53647c478bd9Sstevel@tonic-gate 		    limit, limit, mtbarg);
53657c478bd9Sstevel@tonic-gate 		opt = 'n';
53667c478bd9Sstevel@tonic-gate 		break;
53677c478bd9Sstevel@tonic-gate 	}
53687c478bd9Sstevel@tonic-gate 
53697c478bd9Sstevel@tonic-gate 	string += limit;
53707c478bd9Sstevel@tonic-gate 
53717c478bd9Sstevel@tonic-gate 	return (mtbc);
53727c478bd9Sstevel@tonic-gate }
53737c478bd9Sstevel@tonic-gate 
53747c478bd9Sstevel@tonic-gate /*
53757c478bd9Sstevel@tonic-gate  * Verify that a value is in a range.  If it is not, resets it to
53767c478bd9Sstevel@tonic-gate  * its default value if one is supplied, exits otherwise.
53777c478bd9Sstevel@tonic-gate  *
53787c478bd9Sstevel@tonic-gate  * When testing, can compare user_supplied to RC_KEYWORD or RC_POSITIONAL.
53797c478bd9Sstevel@tonic-gate  */
53807c478bd9Sstevel@tonic-gate static void
range_check(long * varp,char * name,long minimum,long maximum,long def_val,int user_supplied)53817c478bd9Sstevel@tonic-gate range_check(long *varp, char *name, long minimum, long maximum,
53827c478bd9Sstevel@tonic-gate     long def_val, int user_supplied)
53837c478bd9Sstevel@tonic-gate {
53846451fdbcSvsakar 	dprintf(("DeBuG %s : %ld (%ld %ld %ld)\n",
5385d50c8f90Svsakar 	    name, *varp, minimum, maximum, def_val));
53866451fdbcSvsakar 
53877c478bd9Sstevel@tonic-gate 	if ((*varp < minimum) || (*varp > maximum)) {
53887c478bd9Sstevel@tonic-gate 		if (user_supplied != RC_DEFAULT) {
53897c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
53907c478bd9Sstevel@tonic-gate 	    "mkfs: bad value for %s: %ld must be between %ld and %ld\n"),
53917c478bd9Sstevel@tonic-gate 			    name, *varp, minimum, maximum);
53927c478bd9Sstevel@tonic-gate 		}
53937c478bd9Sstevel@tonic-gate 		if (def_val != NO_DEFAULT) {
53947c478bd9Sstevel@tonic-gate 			if (user_supplied) {
53957c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
53967c478bd9Sstevel@tonic-gate 				    gettext("mkfs: %s reset to default %ld\n"),
53977c478bd9Sstevel@tonic-gate 				    name, def_val);
53987c478bd9Sstevel@tonic-gate 			}
53997c478bd9Sstevel@tonic-gate 			*varp = def_val;
54006451fdbcSvsakar 			dprintf(("DeBuG %s : %ld\n", name, *varp));
54017c478bd9Sstevel@tonic-gate 			return;
54027c478bd9Sstevel@tonic-gate 		}
54037c478bd9Sstevel@tonic-gate 		lockexit(2);
54047c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
54057c478bd9Sstevel@tonic-gate 	}
54067c478bd9Sstevel@tonic-gate }
54077c478bd9Sstevel@tonic-gate 
54087c478bd9Sstevel@tonic-gate /*
54097c478bd9Sstevel@tonic-gate  * Verify that a value is in a range.  If it is not, resets it to
54107c478bd9Sstevel@tonic-gate  * its default value if one is supplied, exits otherwise.
54117c478bd9Sstevel@tonic-gate  *
54127c478bd9Sstevel@tonic-gate  * When testing, can compare user_supplied to RC_KEYWORD or RC_POSITIONAL.
54137c478bd9Sstevel@tonic-gate  */
54147c478bd9Sstevel@tonic-gate static void
range_check_64(uint64_t * varp,char * name,uint64_t minimum,uint64_t maximum,uint64_t def_val,int user_supplied)54157c478bd9Sstevel@tonic-gate range_check_64(uint64_t *varp, char *name, uint64_t minimum, uint64_t maximum,
54167c478bd9Sstevel@tonic-gate     uint64_t def_val, int user_supplied)
54177c478bd9Sstevel@tonic-gate {
54187c478bd9Sstevel@tonic-gate 	if ((*varp < minimum) || (*varp > maximum)) {
54197c478bd9Sstevel@tonic-gate 		if (user_supplied != RC_DEFAULT) {
54207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
54217c478bd9Sstevel@tonic-gate 	    "mkfs: bad value for %s: %lld must be between %lld and %lld\n"),
54227c478bd9Sstevel@tonic-gate 			    name, *varp, minimum, maximum);
54237c478bd9Sstevel@tonic-gate 		}
54247c478bd9Sstevel@tonic-gate 		if (def_val != NO_DEFAULT) {
54257c478bd9Sstevel@tonic-gate 			if (user_supplied) {
54267c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
54277c478bd9Sstevel@tonic-gate 				    gettext("mkfs: %s reset to default %lld\n"),
54287c478bd9Sstevel@tonic-gate 				    name, def_val);
54297c478bd9Sstevel@tonic-gate 			}
54307c478bd9Sstevel@tonic-gate 			*varp = def_val;
54317c478bd9Sstevel@tonic-gate 			return;
54327c478bd9Sstevel@tonic-gate 		}
54337c478bd9Sstevel@tonic-gate 		lockexit(2);
54347c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
54357c478bd9Sstevel@tonic-gate 	}
54367c478bd9Sstevel@tonic-gate }
54377c478bd9Sstevel@tonic-gate 
54387c478bd9Sstevel@tonic-gate /*
54397c478bd9Sstevel@tonic-gate  * Blocks SIGINT from delivery.  Returns the previous mask in the
54407c478bd9Sstevel@tonic-gate  * buffer provided, so that mask may be later restored.
54417c478bd9Sstevel@tonic-gate  */
54427c478bd9Sstevel@tonic-gate static void
block_sigint(sigset_t * old_mask)54437c478bd9Sstevel@tonic-gate block_sigint(sigset_t *old_mask)
54447c478bd9Sstevel@tonic-gate {
54457c478bd9Sstevel@tonic-gate 	sigset_t block_mask;
54467c478bd9Sstevel@tonic-gate 
54477c478bd9Sstevel@tonic-gate 	if (sigemptyset(&block_mask) < 0) {
54487c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Could not clear signal mask\n"));
54497c478bd9Sstevel@tonic-gate 		lockexit(3);
54507c478bd9Sstevel@tonic-gate 	}
54517c478bd9Sstevel@tonic-gate 	if (sigaddset(&block_mask, SIGINT) < 0) {
54527c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Could not set signal mask\n"));
54537c478bd9Sstevel@tonic-gate 		lockexit(3);
54547c478bd9Sstevel@tonic-gate 	}
54557c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_BLOCK, &block_mask, old_mask) < 0) {
54567c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Could not block SIGINT\n"));
54577c478bd9Sstevel@tonic-gate 		lockexit(3);
54587c478bd9Sstevel@tonic-gate 	}
54597c478bd9Sstevel@tonic-gate }
54607c478bd9Sstevel@tonic-gate 
54617c478bd9Sstevel@tonic-gate /*
54627c478bd9Sstevel@tonic-gate  * Restores the signal mask that was in force before a call
54637c478bd9Sstevel@tonic-gate  * to block_sigint().  This may actually still have SIGINT blocked,
54647c478bd9Sstevel@tonic-gate  * if we've been recursively invoked.
54657c478bd9Sstevel@tonic-gate  */
54667c478bd9Sstevel@tonic-gate static void
unblock_sigint(sigset_t * old_mask)54677c478bd9Sstevel@tonic-gate unblock_sigint(sigset_t *old_mask)
54687c478bd9Sstevel@tonic-gate {
54697c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, old_mask, (sigset_t *)NULL) < 0) {
54707c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Could not restore signal mask\n"));
54717c478bd9Sstevel@tonic-gate 		lockexit(3);
54727c478bd9Sstevel@tonic-gate 	}
54737c478bd9Sstevel@tonic-gate }
54747c478bd9Sstevel@tonic-gate 
54757c478bd9Sstevel@tonic-gate /*
54767c478bd9Sstevel@tonic-gate  * Attempt to be somewhat graceful about being interrupted, rather than
54777c478bd9Sstevel@tonic-gate  * just silently leaving the filesystem in an unusable state.
54787c478bd9Sstevel@tonic-gate  *
54797c478bd9Sstevel@tonic-gate  * The kernel has blocked SIGINT upon entry, so we don't have to worry
54807c478bd9Sstevel@tonic-gate  * about recursion if the user starts pounding on the keyboard.
54817c478bd9Sstevel@tonic-gate  */
54827c478bd9Sstevel@tonic-gate static void
recover_from_sigint(int signum)54837c478bd9Sstevel@tonic-gate recover_from_sigint(int signum)
54847c478bd9Sstevel@tonic-gate {
54857c478bd9Sstevel@tonic-gate 	if (fso > -1) {
54867c478bd9Sstevel@tonic-gate 		if ((Nflag != 0) || confirm_abort()) {
54877c478bd9Sstevel@tonic-gate 			lockexit(4);
54887c478bd9Sstevel@tonic-gate 		}
54897c478bd9Sstevel@tonic-gate 	}
54907c478bd9Sstevel@tonic-gate }
54917c478bd9Sstevel@tonic-gate 
54927c478bd9Sstevel@tonic-gate static int
confirm_abort(void)54937c478bd9Sstevel@tonic-gate confirm_abort(void)
54947c478bd9Sstevel@tonic-gate {
54957c478bd9Sstevel@tonic-gate 	char line[80];
54967c478bd9Sstevel@tonic-gate 
54977c478bd9Sstevel@tonic-gate 	printf(gettext("\n\nAborting at this point will leave the filesystem "
5498d50c8f90Svsakar 	    "in an inconsistent\nstate.  If you do choose to stop, "
5499d50c8f90Svsakar 	    "you will be given instructions on how to\nrecover "
5500d50c8f90Svsakar 	    "the filesystem.  Do you wish to cancel the filesystem "
5501d50c8f90Svsakar 	    "grow\noperation (y/n)?"));
550223a1cceaSRoger A. Faulkner 	if (getaline(stdin, line, sizeof (line)) == EOF)
55037c478bd9Sstevel@tonic-gate 		line[0] = 'y';
55047c478bd9Sstevel@tonic-gate 
55057c478bd9Sstevel@tonic-gate 	printf("\n");
55067c478bd9Sstevel@tonic-gate 	if (line[0] == 'y' || line[0] == 'Y')
55077c478bd9Sstevel@tonic-gate 		return (1);
55087c478bd9Sstevel@tonic-gate 	else {
55097c478bd9Sstevel@tonic-gate 		return (0);
55107c478bd9Sstevel@tonic-gate 	}
55117c478bd9Sstevel@tonic-gate }
55127c478bd9Sstevel@tonic-gate 
55137c478bd9Sstevel@tonic-gate static int
getaline(FILE * fp,char * loc,int maxlen)551423a1cceaSRoger A. Faulkner getaline(FILE *fp, char *loc, int maxlen)
55157c478bd9Sstevel@tonic-gate {
55167c478bd9Sstevel@tonic-gate 	int n;
55177c478bd9Sstevel@tonic-gate 	char *p, *lastloc;
55187c478bd9Sstevel@tonic-gate 
55197c478bd9Sstevel@tonic-gate 	p = loc;
55207c478bd9Sstevel@tonic-gate 	lastloc = &p[maxlen-1];
55217c478bd9Sstevel@tonic-gate 	while ((n = getc(fp)) != '\n') {
55227c478bd9Sstevel@tonic-gate 		if (n == EOF)
55237c478bd9Sstevel@tonic-gate 			return (EOF);
55247c478bd9Sstevel@tonic-gate 		if (!isspace(n) && p < lastloc)
55257c478bd9Sstevel@tonic-gate 			*p++ = n;
55267c478bd9Sstevel@tonic-gate 	}
55277c478bd9Sstevel@tonic-gate 	*p = 0;
55287c478bd9Sstevel@tonic-gate 	return (p - loc);
55297c478bd9Sstevel@tonic-gate }
55307c478bd9Sstevel@tonic-gate 
55317c478bd9Sstevel@tonic-gate /*
55327c478bd9Sstevel@tonic-gate  * Calculate the maximum value of cylinders-per-group for a file
55337c478bd9Sstevel@tonic-gate  * system with the characteristics:
55347c478bd9Sstevel@tonic-gate  *
55357c478bd9Sstevel@tonic-gate  *	bsize - file system block size
55367c478bd9Sstevel@tonic-gate  *	fragsize - frag size
55377c478bd9Sstevel@tonic-gate  *	nbpi - number of bytes of disk space per inode
55387c478bd9Sstevel@tonic-gate  *	nrpos - number of rotational positions
55397c478bd9Sstevel@tonic-gate  *	spc - sectors per cylinder
55407c478bd9Sstevel@tonic-gate  *
55417c478bd9Sstevel@tonic-gate  * These five characteristic are not adjustable (by this function).
55427c478bd9Sstevel@tonic-gate  * The only attribute of the file system which IS adjusted by this
55437c478bd9Sstevel@tonic-gate  * function in order to maximize cylinders-per-group is the proportion
55447c478bd9Sstevel@tonic-gate  * of the cylinder group overhead block used for the inode map.  The
55457c478bd9Sstevel@tonic-gate  * inode map cannot occupy more than one-third of the cylinder group
55467c478bd9Sstevel@tonic-gate  * overhead block, but it's OK for it to occupy less than one-third
55477c478bd9Sstevel@tonic-gate  * of the overhead block.
55487c478bd9Sstevel@tonic-gate  *
55497c478bd9Sstevel@tonic-gate  * The setting of nbpi determines one possible value for the maximum
55507c478bd9Sstevel@tonic-gate  * size of a cylinder group.  It does so because it determines the total
55517c478bd9Sstevel@tonic-gate  * number of inodes in the file system (file system size is fixed, and
55527c478bd9Sstevel@tonic-gate  * nbpi is fixed, so the total number of inodes is fixed too).  The
55537c478bd9Sstevel@tonic-gate  * cylinder group has to be small enough so that the number of inodes
55547c478bd9Sstevel@tonic-gate  * in the cylinder group is less than or equal to the number of bits
55557c478bd9Sstevel@tonic-gate  * in one-third (or whatever proportion is assumed) of a file system
55567c478bd9Sstevel@tonic-gate  * block.  The details of the calculation are:
55577c478bd9Sstevel@tonic-gate  *
55587c478bd9Sstevel@tonic-gate  *     The macro MAXIpG_B(bsize, inode_divisor) determines the maximum
55597c478bd9Sstevel@tonic-gate  *     number of inodes that can be in a cylinder group, given the
55607c478bd9Sstevel@tonic-gate  *     proportion of the cylinder group overhead block used for the
55617c478bd9Sstevel@tonic-gate  *     inode bitmaps (an inode_divisor of 3 means that 1/3 of the
55627c478bd9Sstevel@tonic-gate  *     block is used for inode bitmaps; an inode_divisor of 12 means
55637c478bd9Sstevel@tonic-gate  *     that 1/12 of the block is used for inode bitmaps.)
55647c478bd9Sstevel@tonic-gate  *
55657c478bd9Sstevel@tonic-gate  *     Once the number of inodes per cylinder group is known, the
55667c478bd9Sstevel@tonic-gate  *     maximum value of cylinders-per-group (determined by nbpi)
55677c478bd9Sstevel@tonic-gate  *     is calculated by the formula
55687c478bd9Sstevel@tonic-gate  *
55697c478bd9Sstevel@tonic-gate  *     maxcpg_given_nbpi = (size of a cylinder group)/(size of a cylinder)
55707c478bd9Sstevel@tonic-gate  *
55717c478bd9Sstevel@tonic-gate  *			 = (inodes-per-cg * nbpi)/(spc * DEV_BSIZE)
55727c478bd9Sstevel@tonic-gate  *
55737c478bd9Sstevel@tonic-gate  *     (Interestingly, the size of the file system never enters
55747c478bd9Sstevel@tonic-gate  *     into this calculation.)
55757c478bd9Sstevel@tonic-gate  *
55767c478bd9Sstevel@tonic-gate  * Another possible value for the maximum cylinder group size is determined
55777c478bd9Sstevel@tonic-gate  * by frag_size and nrpos.  The frags in the cylinder group must be
55787c478bd9Sstevel@tonic-gate  * representable in the frag bitmaps in the cylinder overhead block and the
55797c478bd9Sstevel@tonic-gate  * rotational positions for each cylinder must be represented in the
55807c478bd9Sstevel@tonic-gate  * rotational position tables.  The calculation of the maximum cpg
55817c478bd9Sstevel@tonic-gate  * value, given the frag and nrpos vales, is:
55827c478bd9Sstevel@tonic-gate  *
55837c478bd9Sstevel@tonic-gate  *     maxcpg_given_fragsize =
55847c478bd9Sstevel@tonic-gate  *	  (available space in the overhead block) / (size of per-cylinder data)
55857c478bd9Sstevel@tonic-gate  *
55867c478bd9Sstevel@tonic-gate  *     The available space in the overhead block =
55877c478bd9Sstevel@tonic-gate  *	  bsize - sizeof (struct cg) - space_used_for_inode_bitmaps
55887c478bd9Sstevel@tonic-gate  *
55897c478bd9Sstevel@tonic-gate  *     The size of the per-cylinder data is:
55907c478bd9Sstevel@tonic-gate  *	    sizeof(long)            # for the "blocks avail per cylinder" field
55917c478bd9Sstevel@tonic-gate  *	    + nrpos * sizeof(short)   # for the rotational position table entry
55927c478bd9Sstevel@tonic-gate  *	    + frags-per-cylinder/NBBY # number of bytes to represent this
55937c478bd9Sstevel@tonic-gate  *				      # cylinder in the frag bitmap
55947c478bd9Sstevel@tonic-gate  *
55957c478bd9Sstevel@tonic-gate  * The two calculated maximum values of cylinder-per-group will typically
55967c478bd9Sstevel@tonic-gate  * turn out to be different, since they are derived from two different
55977c478bd9Sstevel@tonic-gate  * constraints.  Usually, maxcpg_given_nbpi is much bigger than
55987c478bd9Sstevel@tonic-gate  * maxcpg_given_fragsize.  But they can be brought together by
55997c478bd9Sstevel@tonic-gate  * adjusting the proportion of the overhead block dedicated to
56007c478bd9Sstevel@tonic-gate  * the inode bitmaps.  Decreasing the proportion of the cylinder
56017c478bd9Sstevel@tonic-gate  * group overhead block used for inode maps will decrease
56027c478bd9Sstevel@tonic-gate  * maxcpg_given_nbpi and increase maxcpg_given_fragsize.
56037c478bd9Sstevel@tonic-gate  *
56047c478bd9Sstevel@tonic-gate  * This function calculates the initial values of maxcpg_given_nbpi
56057c478bd9Sstevel@tonic-gate  * and maxcpg_given_fragsize assuming that 1/3 of the cg overhead
56067c478bd9Sstevel@tonic-gate  * block is used for inode bitmaps.  Then it decreases the proportion
56077c478bd9Sstevel@tonic-gate  * of the cg overhead block used for inode bitmaps (by increasing
56087c478bd9Sstevel@tonic-gate  * the value of inode_divisor) until maxcpg_given_nbpi and
56097c478bd9Sstevel@tonic-gate  * maxcpg_given_fragsize are the same, or stop changing, or
56107c478bd9Sstevel@tonic-gate  * maxcpg_given_nbpi is less than maxcpg_given_fragsize.
56117c478bd9Sstevel@tonic-gate  *
56127c478bd9Sstevel@tonic-gate  * The loop terminates when any of the following occur:
56137c478bd9Sstevel@tonic-gate  *	* maxcpg_given_fragsize is greater than or equal to
56147c478bd9Sstevel@tonic-gate  *	  maxcpg_given_nbpi
56157c478bd9Sstevel@tonic-gate  *	* neither maxcpg_given_fragsize nor maxcpg_given_nbpi
56167c478bd9Sstevel@tonic-gate  *	  change in the expected direction
56177c478bd9Sstevel@tonic-gate  *
56187c478bd9Sstevel@tonic-gate  * The loop is guaranteed to terminate because it only continues
56197c478bd9Sstevel@tonic-gate  * while maxcpg_given_fragsize and maxcpg_given_nbpi are approaching
56207c478bd9Sstevel@tonic-gate  * each other.  As soon they cross each other, or neither one changes
56217c478bd9Sstevel@tonic-gate  * in the direction of the other, or one of them moves in the wrong
56227c478bd9Sstevel@tonic-gate  * direction, the loop completes.
56237c478bd9Sstevel@tonic-gate  */
56247c478bd9Sstevel@tonic-gate 
56257c478bd9Sstevel@tonic-gate static long
compute_maxcpg(long bsize,long fragsize,long nbpi,long nrpos,long spc)56267c478bd9Sstevel@tonic-gate compute_maxcpg(long bsize, long fragsize, long nbpi, long nrpos, long spc)
56277c478bd9Sstevel@tonic-gate {
56287c478bd9Sstevel@tonic-gate 	int	maxcpg_given_nbpi;	/* in cylinders */
56297c478bd9Sstevel@tonic-gate 	int	maxcpg_given_fragsize;	/* in cylinders */
56307c478bd9Sstevel@tonic-gate 	int	spf;			/* sectors per frag */
56317c478bd9Sstevel@tonic-gate 	int	inode_divisor;
56327c478bd9Sstevel@tonic-gate 	int	old_max_given_frag = 0;
56337c478bd9Sstevel@tonic-gate 	int	old_max_given_nbpi = INT_MAX;
56347c478bd9Sstevel@tonic-gate 
56357c478bd9Sstevel@tonic-gate 	spf = fragsize / DEV_BSIZE;
56367c478bd9Sstevel@tonic-gate 	inode_divisor = 3;
56377c478bd9Sstevel@tonic-gate 
56387c478bd9Sstevel@tonic-gate 	while (1) {
56397c478bd9Sstevel@tonic-gate 		maxcpg_given_nbpi =
56407c478bd9Sstevel@tonic-gate 		    (((int64_t)(MAXIpG_B(bsize, inode_divisor))) * nbpi) /
56417c478bd9Sstevel@tonic-gate 		    (DEV_BSIZE * ((int64_t)spc));
56427c478bd9Sstevel@tonic-gate 		maxcpg_given_fragsize =
56437c478bd9Sstevel@tonic-gate 		    (bsize - (sizeof (struct cg)) - (bsize / inode_divisor)) /
56447c478bd9Sstevel@tonic-gate 		    (sizeof (long) + nrpos * sizeof (short) +
5645d50c8f90Svsakar 		    (spc / spf) / NBBY);
56467c478bd9Sstevel@tonic-gate 
56477c478bd9Sstevel@tonic-gate 		if (maxcpg_given_fragsize >= maxcpg_given_nbpi)
56487c478bd9Sstevel@tonic-gate 			return (maxcpg_given_nbpi);
56497c478bd9Sstevel@tonic-gate 
56507c478bd9Sstevel@tonic-gate 		/*
56517c478bd9Sstevel@tonic-gate 		 * If neither value moves toward the other, return the
56527c478bd9Sstevel@tonic-gate 		 * least of the old values (we use the old instead of the
56537c478bd9Sstevel@tonic-gate 		 * new because: if the old is the same as the new, it
56547c478bd9Sstevel@tonic-gate 		 * doesn't matter which ones we use.  If one of the
56557c478bd9Sstevel@tonic-gate 		 * values changed, but in the wrong direction, the
56567c478bd9Sstevel@tonic-gate 		 * new values are suspect.  Better use the old.  This
56577c478bd9Sstevel@tonic-gate 		 * shouldn't happen, but it's best to check.
56587c478bd9Sstevel@tonic-gate 		 */
56597c478bd9Sstevel@tonic-gate 
56607c478bd9Sstevel@tonic-gate 		if (!(maxcpg_given_nbpi < old_max_given_nbpi) &&
56617c478bd9Sstevel@tonic-gate 		    !(maxcpg_given_fragsize > old_max_given_frag))
56627c478bd9Sstevel@tonic-gate 			return (MIN(old_max_given_nbpi, old_max_given_frag));
56637c478bd9Sstevel@tonic-gate 
56647c478bd9Sstevel@tonic-gate 		/*
56657c478bd9Sstevel@tonic-gate 		 * This is probably impossible, but if one of the maxcpg
56667c478bd9Sstevel@tonic-gate 		 * values moved in the "right" direction and one moved
56677c478bd9Sstevel@tonic-gate 		 * in the "wrong" direction (that is, the two values moved
56687c478bd9Sstevel@tonic-gate 		 * in the same direction), the previous conditional won't
56697c478bd9Sstevel@tonic-gate 		 * recognize that the values aren't converging (since at
56707c478bd9Sstevel@tonic-gate 		 * least one value moved in the "right" direction, the
56717c478bd9Sstevel@tonic-gate 		 * last conditional says "keep going").
56727c478bd9Sstevel@tonic-gate 		 *
56737c478bd9Sstevel@tonic-gate 		 * Just to make absolutely certain that the loop terminates,
56747c478bd9Sstevel@tonic-gate 		 * check for one of the values moving in the "wrong" direction
56757c478bd9Sstevel@tonic-gate 		 * and terminate the loop if it happens.
56767c478bd9Sstevel@tonic-gate 		 */
56777c478bd9Sstevel@tonic-gate 
56787c478bd9Sstevel@tonic-gate 		if (maxcpg_given_nbpi > old_max_given_nbpi ||
56797c478bd9Sstevel@tonic-gate 		    maxcpg_given_fragsize < old_max_given_frag)
56807c478bd9Sstevel@tonic-gate 			return (MIN(old_max_given_nbpi, old_max_given_frag));
56817c478bd9Sstevel@tonic-gate 
56827c478bd9Sstevel@tonic-gate 		old_max_given_nbpi = maxcpg_given_nbpi;
56837c478bd9Sstevel@tonic-gate 		old_max_given_frag = maxcpg_given_fragsize;
56847c478bd9Sstevel@tonic-gate 
56857c478bd9Sstevel@tonic-gate 		inode_divisor++;
56867c478bd9Sstevel@tonic-gate 	}
56877c478bd9Sstevel@tonic-gate }
56887c478bd9Sstevel@tonic-gate 
56897c478bd9Sstevel@tonic-gate static int
in_64bit_mode(void)56907c478bd9Sstevel@tonic-gate in_64bit_mode(void)
56917c478bd9Sstevel@tonic-gate {
56927c478bd9Sstevel@tonic-gate 	/*  cmd must be an absolute path, for security */
56937c478bd9Sstevel@tonic-gate 	char *cmd = "/usr/bin/isainfo -b";
56947c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
56957c478bd9Sstevel@tonic-gate 	FILE *ptr;
56967c478bd9Sstevel@tonic-gate 	int retval = 0;
56977c478bd9Sstevel@tonic-gate 
56987c478bd9Sstevel@tonic-gate 	putenv("IFS= \t");
56997c478bd9Sstevel@tonic-gate 	if ((ptr = popen(cmd, "r")) != NULL) {
57007c478bd9Sstevel@tonic-gate 		if (fgets(buf, BUFSIZ, ptr) != NULL &&
57017c478bd9Sstevel@tonic-gate 		    strncmp(buf, "64", 2) == 0)
57027c478bd9Sstevel@tonic-gate 			retval = 1;
57037c478bd9Sstevel@tonic-gate 		(void) pclose(ptr);
57047c478bd9Sstevel@tonic-gate 	}
57057c478bd9Sstevel@tonic-gate 	return (retval);
57067c478bd9Sstevel@tonic-gate }
57077c478bd9Sstevel@tonic-gate 
57087c478bd9Sstevel@tonic-gate /*
57097c478bd9Sstevel@tonic-gate  * validate_size
57107c478bd9Sstevel@tonic-gate  *
57117c478bd9Sstevel@tonic-gate  * Return 1 if the device appears to be at least "size" sectors long.
57127c478bd9Sstevel@tonic-gate  * Return 0 if it's shorter or we can't read it.
57137c478bd9Sstevel@tonic-gate  */
57147c478bd9Sstevel@tonic-gate 
57157c478bd9Sstevel@tonic-gate static int
validate_size(int fd,diskaddr_t size)57167c478bd9Sstevel@tonic-gate validate_size(int fd, diskaddr_t size)
57177c478bd9Sstevel@tonic-gate {
5718fe12dc75SToomas Soome 	char	buf[DEV_BSIZE];
57197c478bd9Sstevel@tonic-gate 	int rc;
57207c478bd9Sstevel@tonic-gate 
57217c478bd9Sstevel@tonic-gate 	if ((llseek(fd, (offset_t)((size - 1) * DEV_BSIZE), SEEK_SET) == -1) ||
57227c478bd9Sstevel@tonic-gate 	    (read(fd, buf, DEV_BSIZE)) != DEV_BSIZE)
57237c478bd9Sstevel@tonic-gate 		rc = 0;
57247c478bd9Sstevel@tonic-gate 	else
57257c478bd9Sstevel@tonic-gate 		rc = 1;
57267c478bd9Sstevel@tonic-gate 	return (rc);
57277c478bd9Sstevel@tonic-gate }
5728355d6bb5Sswilcox 
5729355d6bb5Sswilcox /*
5730355d6bb5Sswilcox  * Print every field of the calculated superblock, along with
5731355d6bb5Sswilcox  * its value.  To make parsing easier on the caller, the value
5732355d6bb5Sswilcox  * is printed first, then the name.  Additionally, there's only
5733355d6bb5Sswilcox  * one name/value pair per line.  All values are reported in
5734355d6bb5Sswilcox  * hexadecimal (with the traditional 0x prefix), as that's slightly
5735355d6bb5Sswilcox  * easier for humans to read.  Not that they're expected to, but
5736355d6bb5Sswilcox  * debugging happens.
5737355d6bb5Sswilcox  */
5738355d6bb5Sswilcox static void
dump_sblock(void)5739355d6bb5Sswilcox dump_sblock(void)
5740355d6bb5Sswilcox {
5741355d6bb5Sswilcox 	int row, column, pending, written;
5742355d6bb5Sswilcox 	caddr_t source;
5743355d6bb5Sswilcox 
5744355d6bb5Sswilcox 	if (Rflag) {
5745355d6bb5Sswilcox 		pending = sizeof (sblock);
5746355d6bb5Sswilcox 		source = (caddr_t)&sblock;
5747355d6bb5Sswilcox 		do {
5748355d6bb5Sswilcox 			written = write(fileno(stdout), source, pending);
5749355d6bb5Sswilcox 			pending -= written;
5750355d6bb5Sswilcox 			source += written;
5751355d6bb5Sswilcox 		} while ((pending > 0) && (written > 0));
5752355d6bb5Sswilcox 
5753355d6bb5Sswilcox 		if (written < 0) {
5754355d6bb5Sswilcox 			perror(gettext("Binary dump of superblock failed"));
5755355d6bb5Sswilcox 			lockexit(1);
5756355d6bb5Sswilcox 		}
5757355d6bb5Sswilcox 		return;
5758355d6bb5Sswilcox 	} else {
5759355d6bb5Sswilcox 		printf("0x%x sblock.fs_link\n", sblock.fs_link);
5760355d6bb5Sswilcox 		printf("0x%x sblock.fs_rolled\n", sblock.fs_rolled);
5761355d6bb5Sswilcox 		printf("0x%x sblock.fs_sblkno\n", sblock.fs_sblkno);
5762355d6bb5Sswilcox 		printf("0x%x sblock.fs_cblkno\n", sblock.fs_cblkno);
5763355d6bb5Sswilcox 		printf("0x%x sblock.fs_iblkno\n", sblock.fs_iblkno);
5764355d6bb5Sswilcox 		printf("0x%x sblock.fs_dblkno\n", sblock.fs_dblkno);
5765355d6bb5Sswilcox 		printf("0x%x sblock.fs_cgoffset\n", sblock.fs_cgoffset);
5766355d6bb5Sswilcox 		printf("0x%x sblock.fs_cgmask\n", sblock.fs_cgmask);
5767355d6bb5Sswilcox 		printf("0x%x sblock.fs_time\n", sblock.fs_time);
5768355d6bb5Sswilcox 		printf("0x%x sblock.fs_size\n", sblock.fs_size);
5769355d6bb5Sswilcox 		printf("0x%x sblock.fs_dsize\n", sblock.fs_dsize);
5770355d6bb5Sswilcox 		printf("0x%x sblock.fs_ncg\n", sblock.fs_ncg);
5771355d6bb5Sswilcox 		printf("0x%x sblock.fs_bsize\n", sblock.fs_bsize);
5772355d6bb5Sswilcox 		printf("0x%x sblock.fs_fsize\n", sblock.fs_fsize);
5773355d6bb5Sswilcox 		printf("0x%x sblock.fs_frag\n", sblock.fs_frag);
5774355d6bb5Sswilcox 		printf("0x%x sblock.fs_minfree\n", sblock.fs_minfree);
5775355d6bb5Sswilcox 		printf("0x%x sblock.fs_rotdelay\n", sblock.fs_rotdelay);
5776355d6bb5Sswilcox 		printf("0x%x sblock.fs_rps\n", sblock.fs_rps);
5777355d6bb5Sswilcox 		printf("0x%x sblock.fs_bmask\n", sblock.fs_bmask);
5778355d6bb5Sswilcox 		printf("0x%x sblock.fs_fmask\n", sblock.fs_fmask);
5779355d6bb5Sswilcox 		printf("0x%x sblock.fs_bshift\n", sblock.fs_bshift);
5780355d6bb5Sswilcox 		printf("0x%x sblock.fs_fshift\n", sblock.fs_fshift);
5781355d6bb5Sswilcox 		printf("0x%x sblock.fs_maxcontig\n", sblock.fs_maxcontig);
5782355d6bb5Sswilcox 		printf("0x%x sblock.fs_maxbpg\n", sblock.fs_maxbpg);
5783355d6bb5Sswilcox 		printf("0x%x sblock.fs_fragshift\n", sblock.fs_fragshift);
5784355d6bb5Sswilcox 		printf("0x%x sblock.fs_fsbtodb\n", sblock.fs_fsbtodb);
5785355d6bb5Sswilcox 		printf("0x%x sblock.fs_sbsize\n", sblock.fs_sbsize);
5786355d6bb5Sswilcox 		printf("0x%x sblock.fs_csmask\n", sblock.fs_csmask);
5787355d6bb5Sswilcox 		printf("0x%x sblock.fs_csshift\n", sblock.fs_csshift);
5788355d6bb5Sswilcox 		printf("0x%x sblock.fs_nindir\n", sblock.fs_nindir);
5789355d6bb5Sswilcox 		printf("0x%x sblock.fs_inopb\n", sblock.fs_inopb);
5790355d6bb5Sswilcox 		printf("0x%x sblock.fs_nspf\n", sblock.fs_nspf);
5791355d6bb5Sswilcox 		printf("0x%x sblock.fs_optim\n", sblock.fs_optim);
5792355d6bb5Sswilcox #ifdef _LITTLE_ENDIAN
5793355d6bb5Sswilcox 		printf("0x%x sblock.fs_state\n", sblock.fs_state);
5794355d6bb5Sswilcox #else
5795355d6bb5Sswilcox 		printf("0x%x sblock.fs_npsect\n", sblock.fs_npsect);
5796355d6bb5Sswilcox #endif
5797355d6bb5Sswilcox 		printf("0x%x sblock.fs_si\n", sblock.fs_si);
5798355d6bb5Sswilcox 		printf("0x%x sblock.fs_trackskew\n", sblock.fs_trackskew);
5799355d6bb5Sswilcox 		printf("0x%x sblock.fs_id[0]\n", sblock.fs_id[0]);
5800355d6bb5Sswilcox 		printf("0x%x sblock.fs_id[1]\n", sblock.fs_id[1]);
5801355d6bb5Sswilcox 		printf("0x%x sblock.fs_csaddr\n", sblock.fs_csaddr);
5802355d6bb5Sswilcox 		printf("0x%x sblock.fs_cssize\n", sblock.fs_cssize);
5803355d6bb5Sswilcox 		printf("0x%x sblock.fs_cgsize\n", sblock.fs_cgsize);
5804355d6bb5Sswilcox 		printf("0x%x sblock.fs_ntrak\n", sblock.fs_ntrak);
5805355d6bb5Sswilcox 		printf("0x%x sblock.fs_nsect\n", sblock.fs_nsect);
5806355d6bb5Sswilcox 		printf("0x%x sblock.fs_spc\n", sblock.fs_spc);
5807355d6bb5Sswilcox 		printf("0x%x sblock.fs_ncyl\n", sblock.fs_ncyl);
5808355d6bb5Sswilcox 		printf("0x%x sblock.fs_cpg\n", sblock.fs_cpg);
5809355d6bb5Sswilcox 		printf("0x%x sblock.fs_ipg\n", sblock.fs_ipg);
5810355d6bb5Sswilcox 		printf("0x%x sblock.fs_fpg\n", sblock.fs_fpg);
5811355d6bb5Sswilcox 		printf("0x%x sblock.fs_cstotal\n", sblock.fs_cstotal);
5812355d6bb5Sswilcox 		printf("0x%x sblock.fs_fmod\n", sblock.fs_fmod);
5813355d6bb5Sswilcox 		printf("0x%x sblock.fs_clean\n", sblock.fs_clean);
5814355d6bb5Sswilcox 		printf("0x%x sblock.fs_ronly\n", sblock.fs_ronly);
5815355d6bb5Sswilcox 		printf("0x%x sblock.fs_flags\n", sblock.fs_flags);
5816355d6bb5Sswilcox 		printf("0x%x sblock.fs_fsmnt\n", sblock.fs_fsmnt);
5817355d6bb5Sswilcox 		printf("0x%x sblock.fs_cgrotor\n", sblock.fs_cgrotor);
5818355d6bb5Sswilcox 		printf("0x%x sblock.fs_u.fs_csp\n", sblock.fs_u.fs_csp);
5819355d6bb5Sswilcox 		printf("0x%x sblock.fs_cpc\n", sblock.fs_cpc);
5820355d6bb5Sswilcox 
5821355d6bb5Sswilcox 		/*
5822355d6bb5Sswilcox 		 * No macros are defined for the dimensions of the
5823355d6bb5Sswilcox 		 * opostbl array.
5824355d6bb5Sswilcox 		 */
5825355d6bb5Sswilcox 		for (row = 0; row < 16; row++) {
5826355d6bb5Sswilcox 			for (column = 0; column < 8; column++) {
5827355d6bb5Sswilcox 				printf("0x%x sblock.fs_opostbl[%d][%d]\n",
5828355d6bb5Sswilcox 				    sblock.fs_opostbl[row][column],
5829355d6bb5Sswilcox 				    row, column);
5830355d6bb5Sswilcox 			}
5831355d6bb5Sswilcox 		}
5832355d6bb5Sswilcox 
5833355d6bb5Sswilcox 		/*
5834355d6bb5Sswilcox 		 * Ditto the size of sparecon.
5835355d6bb5Sswilcox 		 */
5836355d6bb5Sswilcox 		for (row = 0; row < 51; row++) {
5837355d6bb5Sswilcox 			printf("0x%x sblock.fs_sparecon[%d]\n",
5838355d6bb5Sswilcox 			    sblock.fs_sparecon[row], row);
5839355d6bb5Sswilcox 		}
5840355d6bb5Sswilcox 
5841355d6bb5Sswilcox 		printf("0x%x sblock.fs_version\n", sblock.fs_version);
5842355d6bb5Sswilcox 		printf("0x%x sblock.fs_logbno\n", sblock.fs_logbno);
5843355d6bb5Sswilcox 		printf("0x%x sblock.fs_reclaim\n", sblock.fs_reclaim);
5844355d6bb5Sswilcox 		printf("0x%x sblock.fs_sparecon2\n", sblock.fs_sparecon2);
5845355d6bb5Sswilcox #ifdef _LITTLE_ENDIAN
5846355d6bb5Sswilcox 		printf("0x%x sblock.fs_npsect\n", sblock.fs_npsect);
5847355d6bb5Sswilcox #else
5848355d6bb5Sswilcox 		printf("0x%x sblock.fs_state\n", sblock.fs_state);
5849355d6bb5Sswilcox #endif
5850355d6bb5Sswilcox 		printf("0x%llx sblock.fs_qbmask\n", sblock.fs_qbmask);
5851355d6bb5Sswilcox 		printf("0x%llx sblock.fs_qfmask\n", sblock.fs_qfmask);
5852355d6bb5Sswilcox 		printf("0x%x sblock.fs_postblformat\n", sblock.fs_postblformat);
5853355d6bb5Sswilcox 		printf("0x%x sblock.fs_nrpos\n", sblock.fs_nrpos);
5854355d6bb5Sswilcox 		printf("0x%x sblock.fs_postbloff\n", sblock.fs_postbloff);
5855355d6bb5Sswilcox 		printf("0x%x sblock.fs_rotbloff\n", sblock.fs_rotbloff);
5856355d6bb5Sswilcox 		printf("0x%x sblock.fs_magic\n", sblock.fs_magic);
5857355d6bb5Sswilcox 
5858355d6bb5Sswilcox 		/*
5859355d6bb5Sswilcox 		 * fs_space isn't of much use in this context, so we'll
5860355d6bb5Sswilcox 		 * just ignore it for now.
5861355d6bb5Sswilcox 		 */
5862355d6bb5Sswilcox 	}
5863355d6bb5Sswilcox }
5864