10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*456Stn143363  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Just in case we're not in a build environment, make sure that
310Sstevel@tonic-gate  * TEXT_DOMAIN gets set to something.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
340Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
350Sstevel@tonic-gate #endif
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * stripe operations
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <limits.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <meta.h>
440Sstevel@tonic-gate #include <sys/lvm/md_stripe.h>
450Sstevel@tonic-gate #include <sys/lvm/md_convert.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define	QUOTE(x)	#x
480Sstevel@tonic-gate #define	VAL2STR(x)	QUOTE(x)
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * replace stripe/concat
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate int
540Sstevel@tonic-gate meta_stripe_replace(
550Sstevel@tonic-gate 	mdsetname_t	*sp,
560Sstevel@tonic-gate 	mdname_t	*stripenp,
570Sstevel@tonic-gate 	mdname_t	*oldnp,
580Sstevel@tonic-gate 	mdname_t	*newnp,
590Sstevel@tonic-gate 	mdcmdopts_t	options,
600Sstevel@tonic-gate 	md_error_t	*ep
610Sstevel@tonic-gate )
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	replace_params_t	params;
640Sstevel@tonic-gate 	md_dev64_t		old_dev,
650Sstevel@tonic-gate 				new_dev;
660Sstevel@tonic-gate 	diskaddr_t		new_start_blk,
670Sstevel@tonic-gate 				new_end_blk,
680Sstevel@tonic-gate 				label,
690Sstevel@tonic-gate 				size,
700Sstevel@tonic-gate 				start_blk;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	/* should have same set */
730Sstevel@tonic-gate 	assert(sp != NULL);
740Sstevel@tonic-gate 	assert(sp->setno == MD_MIN2SET(meta_getminor(stripenp->dev)));
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	new_dev = newnp->dev;
770Sstevel@tonic-gate 	new_start_blk = newnp->start_blk;
780Sstevel@tonic-gate 	new_end_blk = newnp->end_blk;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	meta_invalidate_name(stripenp);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	/* the old device binding is now established */
830Sstevel@tonic-gate 	if ((old_dev = oldnp->dev) == NODEV64)
840Sstevel@tonic-gate 		return (mdsyserror(ep, ENODEV, oldnp->cname));
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (((strcmp(oldnp->rname, newnp->rname) == 0) &&
870Sstevel@tonic-gate 	    (old_dev != new_dev))) {
880Sstevel@tonic-gate 		newnp->dev = new_dev;
890Sstevel@tonic-gate 		newnp->start_blk = new_start_blk;
900Sstevel@tonic-gate 		newnp->end_blk = new_end_blk;
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	if ((size = metagetsize(newnp, ep)) == MD_DISKADDR_ERROR)
940Sstevel@tonic-gate 		return (-1);
950Sstevel@tonic-gate 	if ((label = metagetlabel(newnp, ep)) == MD_DISKADDR_ERROR)
960Sstevel@tonic-gate 		return (-1);
970Sstevel@tonic-gate 	if ((start_blk = metagetstart(sp, newnp, ep)) == MD_DISKADDR_ERROR)
980Sstevel@tonic-gate 		return (-1);
990Sstevel@tonic-gate 	if (start_blk >= size) {
1000Sstevel@tonic-gate 		(void) mdsyserror(ep, ENOSPC, newnp->cname);
1010Sstevel@tonic-gate 		return (-1);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	/* In dryrun mode (DOIT not set) we must not alter the mddb */
1050Sstevel@tonic-gate 	if (options & MDCMD_DOIT) {
1060Sstevel@tonic-gate 		if (add_key_name(sp, newnp, NULL, ep) != 0)
1070Sstevel@tonic-gate 			return (-1);
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	/*
1110Sstevel@tonic-gate 	 * There is no need to call meta_fixdevid() here as this function is
1120Sstevel@tonic-gate 	 * only called by the metareplace -c command which actually does
1130Sstevel@tonic-gate 	 * nothing (in terms of a resync) and thus does nothing with the devid.
1140Sstevel@tonic-gate 	 */
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	(void) memset(&params, 0, sizeof (params));
1170Sstevel@tonic-gate 	params.mnum = meta_getminor(stripenp->dev);
1180Sstevel@tonic-gate 	MD_SETDRIVERNAME(&params, MD_STRIPE, sp->setno);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	params.cmd = REPLACE_COMP;
1210Sstevel@tonic-gate 	params.old_dev = old_dev;
1220Sstevel@tonic-gate 	params.new_dev = new_dev;
1230Sstevel@tonic-gate 	params.new_key = newnp->key;
1240Sstevel@tonic-gate 	params.start_blk = newnp->start_blk;
1250Sstevel@tonic-gate 	params.number_blks = size;
1260Sstevel@tonic-gate 	/* Is this just a dryrun ? */
1270Sstevel@tonic-gate 	if ((options & MDCMD_DOIT) == 0) {
1280Sstevel@tonic-gate 		params.options |= MDIOCTL_DRYRUN;
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 	if (label == 0)
1310Sstevel@tonic-gate 		params.has_label = 0;
1320Sstevel@tonic-gate 	else
1330Sstevel@tonic-gate 		params.has_label = 1;
1340Sstevel@tonic-gate 	if (metaioctl(MD_IOCREPLACE, &params, &params.mde, NULL) != 0) {
1350Sstevel@tonic-gate 		if (options & MDCMD_DOIT)
1360Sstevel@tonic-gate 			(void) del_key_name(sp, newnp, ep);
1370Sstevel@tonic-gate 		return (mdstealerror(ep, &params.mde));
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	meta_invalidate_name(oldnp);
1400Sstevel@tonic-gate 	meta_invalidate_name(newnp);
1410Sstevel@tonic-gate 	meta_invalidate_name(stripenp);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	if (options & MDCMD_PRINT) {
1440Sstevel@tonic-gate 		(void) printf(dgettext(TEXT_DOMAIN,
1450Sstevel@tonic-gate 		    "%s: device %s is replaced with %s\n"),
1460Sstevel@tonic-gate 		    stripenp->cname, oldnp->cname, newnp->cname);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 	return (0);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate  * FUNCTION:	meta_get_stripe_names()
1550Sstevel@tonic-gate  * INPUT:	sp	- the set name to get stripes from
1560Sstevel@tonic-gate  *		options	- options from the command line
1570Sstevel@tonic-gate  * OUTPUT:	nlpp	- list of all stripe names
1580Sstevel@tonic-gate  *		ep	- return error pointer
1590Sstevel@tonic-gate  * RETURNS:	int	- -1 if error, 0 success
1600Sstevel@tonic-gate  * PURPOSE:	returns a list of all stripes in the metadb
1610Sstevel@tonic-gate  *		for all devices in the specified set
1620Sstevel@tonic-gate  */
1630Sstevel@tonic-gate int
1640Sstevel@tonic-gate meta_get_stripe_names(
1650Sstevel@tonic-gate 	mdsetname_t	*sp,
1660Sstevel@tonic-gate 	mdnamelist_t	**nlpp,
1670Sstevel@tonic-gate 	int		options,
1680Sstevel@tonic-gate 	md_error_t	*ep
1690Sstevel@tonic-gate )
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	return (meta_get_names(MD_STRIPE, sp, nlpp, options, ep));
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * free stripe
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate void
1780Sstevel@tonic-gate meta_free_stripe(
1790Sstevel@tonic-gate 	md_stripe_t	*stripep
1800Sstevel@tonic-gate )
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	uint_t		row;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	for (row = 0; (row < stripep->rows.rows_len); ++row) {
1850Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		if (rp->comps.comps_val != NULL) {
1880Sstevel@tonic-gate 			assert(rp->comps.comps_len > 0);
1890Sstevel@tonic-gate 			Free(rp->comps.comps_val);
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 	if (stripep->rows.rows_val != NULL) {
1930Sstevel@tonic-gate 		assert(stripep->rows.rows_len > 0);
1940Sstevel@tonic-gate 		Free(stripep->rows.rows_val);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 	Free(stripep);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate  * get stripe (common)
2020Sstevel@tonic-gate  */
2030Sstevel@tonic-gate md_stripe_t *
2040Sstevel@tonic-gate meta_get_stripe_common(
2050Sstevel@tonic-gate 	mdsetname_t	*sp,
2060Sstevel@tonic-gate 	mdname_t	*stripenp,
2070Sstevel@tonic-gate 	int		fast,
2080Sstevel@tonic-gate 	md_error_t	*ep
2090Sstevel@tonic-gate )
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	mddrivename_t	*dnp = stripenp->drivenamep;
2120Sstevel@tonic-gate 	char		*miscname;
2130Sstevel@tonic-gate 	ms_unit_t	*ms;
2140Sstevel@tonic-gate 	md_stripe_t	*stripep;
2150Sstevel@tonic-gate 	uint_t		row;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/* must have set */
2180Sstevel@tonic-gate 	assert(sp != NULL);
2190Sstevel@tonic-gate 	assert(sp->setno == MD_MIN2SET(meta_getminor(stripenp->dev)));
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/* short circuit */
2220Sstevel@tonic-gate 	if (dnp->unitp != NULL) {
2230Sstevel@tonic-gate 		assert(dnp->unitp->type == MD_DEVICE);
2240Sstevel@tonic-gate 		return ((md_stripe_t *)dnp->unitp);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	/* get miscname and unit */
2280Sstevel@tonic-gate 	if ((miscname = metagetmiscname(stripenp, ep)) == NULL)
2290Sstevel@tonic-gate 		return (NULL);
2300Sstevel@tonic-gate 	if (strcmp(miscname, MD_STRIPE) != 0) {
2310Sstevel@tonic-gate 		(void) mdmderror(ep, MDE_NOT_STRIPE,
2320Sstevel@tonic-gate 			    meta_getminor(stripenp->dev), stripenp->cname);
2330Sstevel@tonic-gate 		return (NULL);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 	if ((ms = (ms_unit_t *)meta_get_mdunit(sp, stripenp, ep)) == NULL)
2360Sstevel@tonic-gate 		return (NULL);
2370Sstevel@tonic-gate 	assert(ms->c.un_type == MD_DEVICE);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/* allocate stripe */
2400Sstevel@tonic-gate 	stripep = Zalloc(sizeof (*stripep));
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/* allocate rows */
2430Sstevel@tonic-gate 	assert(ms->un_nrows > 0);
2440Sstevel@tonic-gate 	stripep->rows.rows_len = ms->un_nrows;
2450Sstevel@tonic-gate 	stripep->rows.rows_val = Zalloc(stripep->rows.rows_len *
2460Sstevel@tonic-gate 	    sizeof (*stripep->rows.rows_val));
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/* get common info */
2490Sstevel@tonic-gate 	stripep->common.namep = stripenp;
2500Sstevel@tonic-gate 	stripep->common.type = ms->c.un_type;
2510Sstevel@tonic-gate 	stripep->common.state = ms->c.un_status;
2520Sstevel@tonic-gate 	stripep->common.capabilities = ms->c.un_capabilities;
2530Sstevel@tonic-gate 	stripep->common.parent = ms->c.un_parent;
2540Sstevel@tonic-gate 	stripep->common.size = ms->c.un_total_blocks;
2550Sstevel@tonic-gate 	stripep->common.user_flags = ms->c.un_user_flags;
2560Sstevel@tonic-gate 	stripep->common.revision = ms->c.un_revision;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	/* get options */
2590Sstevel@tonic-gate 	if ((ms->un_hsp_id != MD_HSP_NONE) &&
2600Sstevel@tonic-gate 	    ((stripep->hspnamep = metahsphspname(&sp, ms->un_hsp_id,
2610Sstevel@tonic-gate 	    ep)) == NULL)) {
2620Sstevel@tonic-gate 		goto out;
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	/* get rows */
2660Sstevel@tonic-gate 	for (row = 0; (row < ms->un_nrows); ++row) {
2670Sstevel@tonic-gate 		struct ms_row	*mdr = &ms->un_row[row];
2680Sstevel@tonic-gate 		struct ms_comp	*mdcomp = (void *)&((char *)ms)[ms->un_ocomp];
2690Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
2700Sstevel@tonic-gate 		uint_t		comp, c;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		/* get interlace */
2730Sstevel@tonic-gate 		rp->interlace = mdr->un_interlace;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 		/* allocate comps */
2760Sstevel@tonic-gate 		assert(mdr->un_ncomp > 0);
2770Sstevel@tonic-gate 		rp->comps.comps_len = mdr->un_ncomp;
2780Sstevel@tonic-gate 		rp->comps.comps_val = Zalloc(rp->comps.comps_len *
2790Sstevel@tonic-gate 		    sizeof (*rp->comps.comps_val));
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 		/* get components */
2820Sstevel@tonic-gate 		for (comp = 0, c = mdr->un_icomp; (comp < mdr->un_ncomp);
2830Sstevel@tonic-gate 		    ++comp, ++c) {
2840Sstevel@tonic-gate 			struct ms_comp	*mdc = &mdcomp[c];
2850Sstevel@tonic-gate 			diskaddr_t	comp_start_blk = mdc->un_start_block;
2860Sstevel@tonic-gate 			md_comp_t	*cp = &rp->comps.comps_val[comp];
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 			/* get the component name */
2890Sstevel@tonic-gate 			cp->compnamep = metakeyname(&sp, mdc->un_key, fast, ep);
2900Sstevel@tonic-gate 			if (cp->compnamep == NULL)
2910Sstevel@tonic-gate 				goto out;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 			/* if hotspared */
2940Sstevel@tonic-gate 			if (mdc->un_mirror.ms_hs_id != 0) {
2950Sstevel@tonic-gate 				diskaddr_t hs_start_blk = mdc->un_start_block;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 				/* get the hotspare name */
2980Sstevel@tonic-gate 				cp->hsnamep = metakeyname(&sp,
2990Sstevel@tonic-gate 				    mdc->un_mirror.ms_hs_key, fast, ep);
3000Sstevel@tonic-gate 				if (cp->hsnamep == NULL)
3010Sstevel@tonic-gate 					goto out;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 				if (getenv("META_DEBUG_START_BLK") != NULL) {
3040Sstevel@tonic-gate 					if (metagetstart(sp, cp->hsnamep,
3050Sstevel@tonic-gate 					    ep) == MD_DISKADDR_ERROR)
3060Sstevel@tonic-gate 						mdclrerror(ep);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 					if ((cp->hsnamep->start_blk == 0) &&
3090Sstevel@tonic-gate 					    (hs_start_blk != 0))
3100Sstevel@tonic-gate 						md_eprintf(dgettext(TEXT_DOMAIN,
3110Sstevel@tonic-gate 					    "%s: suspected bad start block,"
3120Sstevel@tonic-gate 					    " seems labelled [stripe/hs]\n"),
3130Sstevel@tonic-gate 					    cp->hsnamep->cname);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 					if ((cp->hsnamep->start_blk > 0) &&
3160Sstevel@tonic-gate 					    (hs_start_blk == 0) &&
3170Sstevel@tonic-gate 					    ! ((row == 0) && (comp == 0)))
3180Sstevel@tonic-gate 						md_eprintf(dgettext(TEXT_DOMAIN,
3190Sstevel@tonic-gate 					    "%s: suspected bad start block, "
3200Sstevel@tonic-gate 					    "seems unlabelled [stripe/hs]\n"),
3210Sstevel@tonic-gate 					    cp->hsnamep->cname);
3220Sstevel@tonic-gate 				}
3230Sstevel@tonic-gate 				/* override any start_blk */
3240Sstevel@tonic-gate 				cp->hsnamep->start_blk = hs_start_blk;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 				/* get the right component start_blk */
3270Sstevel@tonic-gate 				comp_start_blk = mdc->un_mirror.ms_orig_blk;
3280Sstevel@tonic-gate 			} else {
3290Sstevel@tonic-gate 				if (getenv("META_DEBUG_START_BLK") != NULL) {
3300Sstevel@tonic-gate 					if (metagetstart(sp, cp->compnamep,
3310Sstevel@tonic-gate 					    ep) == MD_DISKADDR_ERROR)
3320Sstevel@tonic-gate 						mdclrerror(ep);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 					if ((cp->compnamep->start_blk == 0) &&
3350Sstevel@tonic-gate 					    (comp_start_blk != 0))
3360Sstevel@tonic-gate 						md_eprintf(dgettext(TEXT_DOMAIN,
3370Sstevel@tonic-gate 					    "%s: suspected bad start block,"
3380Sstevel@tonic-gate 					    " seems labelled [stripe]"),
3390Sstevel@tonic-gate 					    cp->compnamep->cname);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 					if ((cp->compnamep->start_blk > 0) &&
3420Sstevel@tonic-gate 					    (comp_start_blk == 0) &&
3430Sstevel@tonic-gate 					    ! ((row == 0) && (comp == 0)))
3440Sstevel@tonic-gate 						md_eprintf(dgettext(TEXT_DOMAIN,
3450Sstevel@tonic-gate 					    "%s: suspected bad start block, "
3460Sstevel@tonic-gate 					    "seems unlabelled [stripe]"),
3470Sstevel@tonic-gate 					    cp->compnamep->cname);
3480Sstevel@tonic-gate 				}
3490Sstevel@tonic-gate 			}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 			/* override any start_blk */
3520Sstevel@tonic-gate 			cp->compnamep->start_blk = comp_start_blk;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 			/* get state */
3550Sstevel@tonic-gate 			cp->state = mdc->un_mirror.ms_state;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 			/* get time of last state change */
3580Sstevel@tonic-gate 			cp->timestamp = mdc->un_mirror.ms_timestamp;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 			/* get lasterr count */
3610Sstevel@tonic-gate 			cp->lasterrcnt = mdc->un_mirror.ms_lasterrcnt;
3620Sstevel@tonic-gate 		}
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/* cleanup, return success */
3660Sstevel@tonic-gate 	Free(ms);
3670Sstevel@tonic-gate 	dnp->unitp = (md_common_t *)stripep;
3680Sstevel@tonic-gate 	return (stripep);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	/* cleanup, return error */
3710Sstevel@tonic-gate out:
3720Sstevel@tonic-gate 	Free(ms);
3730Sstevel@tonic-gate 	meta_free_stripe(stripep);
3740Sstevel@tonic-gate 	return (NULL);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate  * get stripe
3790Sstevel@tonic-gate  */
3800Sstevel@tonic-gate md_stripe_t *
3810Sstevel@tonic-gate meta_get_stripe(
3820Sstevel@tonic-gate 	mdsetname_t	*sp,
3830Sstevel@tonic-gate 	mdname_t	*stripenp,
3840Sstevel@tonic-gate 	md_error_t	*ep
3850Sstevel@tonic-gate )
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	return (meta_get_stripe_common(sp, stripenp, 0, ep));
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate /*
3910Sstevel@tonic-gate  * check stripe for dev
3920Sstevel@tonic-gate  */
3930Sstevel@tonic-gate static int
3940Sstevel@tonic-gate in_stripe(
3950Sstevel@tonic-gate 	mdsetname_t	*sp,
3960Sstevel@tonic-gate 	mdname_t	*stripenp,
3970Sstevel@tonic-gate 	mdname_t	*np,
3980Sstevel@tonic-gate 	diskaddr_t	slblk,
3990Sstevel@tonic-gate 	diskaddr_t	nblks,
4000Sstevel@tonic-gate 	md_error_t	*ep
4010Sstevel@tonic-gate )
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	md_stripe_t	*stripep;
4040Sstevel@tonic-gate 	uint_t		row;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	/* should be in the same set */
4070Sstevel@tonic-gate 	assert(sp != NULL);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	/* get unit */
4100Sstevel@tonic-gate 	if ((stripep = meta_get_stripe(sp, stripenp, ep)) == NULL)
4110Sstevel@tonic-gate 		return (-1);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	/* look in rows */
4140Sstevel@tonic-gate 	for (row = 0; (row < stripep->rows.rows_len); ++row) {
4150Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
4160Sstevel@tonic-gate 		uint_t		comp;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 		/* look in columns */
4190Sstevel@tonic-gate 		for (comp = 0; (comp < rp->comps.comps_len); ++comp) {
4200Sstevel@tonic-gate 			md_comp_t	*cp = &rp->comps.comps_val[comp];
4210Sstevel@tonic-gate 			mdname_t	*compnp = cp->compnamep;
4220Sstevel@tonic-gate 			diskaddr_t	comp_sblk;
4230Sstevel@tonic-gate 			int		err;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 			/* check same drive since metagetstart() can fail */
4260Sstevel@tonic-gate 			if ((err = meta_check_samedrive(np, compnp, ep)) < 0)
4270Sstevel@tonic-gate 				return (-1);
4280Sstevel@tonic-gate 			else if (err == 0)
4290Sstevel@tonic-gate 				continue;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 			/* check overlap */
4320Sstevel@tonic-gate 			if ((comp_sblk = metagetstart(sp, compnp, ep)) ==
4330Sstevel@tonic-gate 			    MD_DISKADDR_ERROR)
4340Sstevel@tonic-gate 				return (-1);
4350Sstevel@tonic-gate 			if (meta_check_overlap(stripenp->cname, np,
4360Sstevel@tonic-gate 			    slblk, nblks, compnp, comp_sblk, -1,
4370Sstevel@tonic-gate 			    ep) != 0) {
4380Sstevel@tonic-gate 				return (-1);
4390Sstevel@tonic-gate 			}
4400Sstevel@tonic-gate 		}
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/* return success */
4440Sstevel@tonic-gate 	return (0);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate /*
4480Sstevel@tonic-gate  * check to see if we're in a stripe
4490Sstevel@tonic-gate  */
4500Sstevel@tonic-gate int
4510Sstevel@tonic-gate meta_check_instripe(
4520Sstevel@tonic-gate 	mdsetname_t	*sp,
4530Sstevel@tonic-gate 	mdname_t	*np,
4540Sstevel@tonic-gate 	diskaddr_t	slblk,
4550Sstevel@tonic-gate 	diskaddr_t	nblks,
4560Sstevel@tonic-gate 	md_error_t	*ep
4570Sstevel@tonic-gate )
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate 	mdnamelist_t	*stripenlp = NULL;
4600Sstevel@tonic-gate 	mdnamelist_t	*p;
4610Sstevel@tonic-gate 	int		rval = 0;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	/* should have a set */
4640Sstevel@tonic-gate 	assert(sp != NULL);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/* for each stripe */
4670Sstevel@tonic-gate 	if (meta_get_stripe_names(sp, &stripenlp, 0, ep) < 0)
4680Sstevel@tonic-gate 		return (-1);
4690Sstevel@tonic-gate 	for (p = stripenlp; (p != NULL); p = p->next) {
4700Sstevel@tonic-gate 		mdname_t	*stripenp = p->namep;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 		/* check stripe */
4730Sstevel@tonic-gate 		if (in_stripe(sp, stripenp, np, slblk, nblks, ep) != 0) {
4740Sstevel@tonic-gate 			rval = -1;
4750Sstevel@tonic-gate 			break;
4760Sstevel@tonic-gate 		}
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/* cleanup, return success */
4800Sstevel@tonic-gate 	metafreenamelist(stripenlp);
4810Sstevel@tonic-gate 	return (rval);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate  * check component
4860Sstevel@tonic-gate  */
4870Sstevel@tonic-gate int
4880Sstevel@tonic-gate meta_check_component(
4890Sstevel@tonic-gate 	mdsetname_t	*sp,
4900Sstevel@tonic-gate 	mdname_t	*np,
4910Sstevel@tonic-gate 	int		force,
4920Sstevel@tonic-gate 	md_error_t	*ep
4930Sstevel@tonic-gate )
4940Sstevel@tonic-gate {
4950Sstevel@tonic-gate 	mdchkopts_t	options = (MDCHK_ALLOW_MDDB);
4960Sstevel@tonic-gate 	md_common_t	*mdp;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	/*
4990Sstevel@tonic-gate 	 * See if we are a soft partition: meta_sp_issp() returns 0 if
5000Sstevel@tonic-gate 	 * np points to a soft partition, so the if and else clauses
5010Sstevel@tonic-gate 	 * here represent "not a soft partition" and "soft partition,"
5020Sstevel@tonic-gate 	 * respectively.
5030Sstevel@tonic-gate 	 */
5040Sstevel@tonic-gate 	if (meta_sp_issp(sp, np, ep) != 0) {
5050Sstevel@tonic-gate 		/* make sure we have a disk */
5060Sstevel@tonic-gate 		if (metachkcomp(np, ep) != 0)
5070Sstevel@tonic-gate 			return (-1);
5080Sstevel@tonic-gate 	} else {
5090Sstevel@tonic-gate 		/* make sure soft partition can parent & doesn't have parent */
5100Sstevel@tonic-gate 		if ((mdp = meta_get_unit(sp, np, ep)) == NULL)
5110Sstevel@tonic-gate 			return (mdmderror(ep, MDE_INVAL_UNIT, NULL,
5120Sstevel@tonic-gate 			    np->cname));
5130Sstevel@tonic-gate 		if (mdp->capabilities == MD_CANT_PARENT)
5140Sstevel@tonic-gate 			return (mdmderror(ep, MDE_INVAL_UNIT, NULL,
5150Sstevel@tonic-gate 			    np->cname));
5160Sstevel@tonic-gate 		if (MD_HAS_PARENT(mdp->parent)) {
5170Sstevel@tonic-gate 			mdname_t *pnp;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 			pnp = metamnumname(&sp, mdp->parent, 0, ep);
5200Sstevel@tonic-gate 			if (pnp == NULL) {
5210Sstevel@tonic-gate 				return (-1);
5220Sstevel@tonic-gate 			}
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 			return (mduseerror(ep, MDE_ALREADY, np->dev,
5250Sstevel@tonic-gate 			    pnp->cname, np->cname));
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	/* check to ensure that it is not already in use */
5300Sstevel@tonic-gate 	if ((! force) &&
5310Sstevel@tonic-gate 	    (meta_check_inuse(sp, np, MDCHK_INUSE, ep) != 0)) {
5320Sstevel@tonic-gate 		return (-1);
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	/* make sure it is in the set */
5360Sstevel@tonic-gate 	if (meta_check_inset(sp, np, ep) != 0)
5370Sstevel@tonic-gate 		return (-1);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/* make sure its not in a metadevice */
5400Sstevel@tonic-gate 	if (meta_check_inmeta(sp, np, options, 0, -1, ep) != 0)
5410Sstevel@tonic-gate 		return (-1);
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	/* return success */
5440Sstevel@tonic-gate 	return (0);
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate  * print stripe
5490Sstevel@tonic-gate  */
5500Sstevel@tonic-gate static int
5510Sstevel@tonic-gate stripe_print(
5520Sstevel@tonic-gate 	md_stripe_t	*stripep,
5530Sstevel@tonic-gate 	char		*fname,
5540Sstevel@tonic-gate 	FILE		*fp,
5550Sstevel@tonic-gate 	mdprtopts_t	options,
5560Sstevel@tonic-gate 	md_error_t	*ep
5570Sstevel@tonic-gate )
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate 	uint_t		row;
5600Sstevel@tonic-gate 	int		rval = -1;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	if (options & PRINT_LARGEDEVICES) {
5630Sstevel@tonic-gate 		if (stripep->common.revision != MD_64BIT_META_DEV) {
5640Sstevel@tonic-gate 			rval = 0;
5650Sstevel@tonic-gate 			goto out;
5660Sstevel@tonic-gate 		}
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/* print name and num rows */
5700Sstevel@tonic-gate 	if (fprintf(fp, "%s %u",
5710Sstevel@tonic-gate 	    stripep->common.namep->cname, stripep->rows.rows_len) == EOF)
5720Sstevel@tonic-gate 		goto out;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	/* print rows */
5750Sstevel@tonic-gate 	for (row = 0; (row < stripep->rows.rows_len); ++row) {
5760Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
5770Sstevel@tonic-gate 		uint_t		comp;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 		/* print num components */
5800Sstevel@tonic-gate 		if (fprintf(fp, " %u", rp->comps.comps_len) == EOF)
5810Sstevel@tonic-gate 			goto out;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 		/* print components */
5840Sstevel@tonic-gate 		for (comp = 0; (comp < rp->comps.comps_len); ++comp) {
5850Sstevel@tonic-gate 			md_comp_t	*cp = &rp->comps.comps_val[comp];
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 			/* print component */
5880Sstevel@tonic-gate 			/*
5890Sstevel@tonic-gate 			 * If the path is our standard /dev/rdsk or /dev/md/rdsk
5900Sstevel@tonic-gate 			 * then just print out the cxtxdxsx or the dx, metainit
5910Sstevel@tonic-gate 			 * will assume the default, otherwise we need the full
5920Sstevel@tonic-gate 			 * pathname to make sure this works as we intend.
5930Sstevel@tonic-gate 			 */
5940Sstevel@tonic-gate 			if ((strstr(cp->compnamep->rname, "/dev/rdsk") ==
5950Sstevel@tonic-gate 			    NULL) && (strstr(cp->compnamep->rname,
5960Sstevel@tonic-gate 			    "/dev/md/rdsk") == NULL) &&
5970Sstevel@tonic-gate 			    (strstr(cp->compnamep->rname, "/dev/td/") ==
5980Sstevel@tonic-gate 			    NULL)) {
5990Sstevel@tonic-gate 				/* not standard path, print full pathname */
6000Sstevel@tonic-gate 				if (fprintf(fp, " %s", cp->compnamep->rname)
6010Sstevel@tonic-gate 				    == EOF)
6020Sstevel@tonic-gate 					goto out;
6030Sstevel@tonic-gate 			} else {
6040Sstevel@tonic-gate 				/* standard path */
6050Sstevel@tonic-gate 				if (fprintf(fp, " %s", cp->compnamep->cname)
6060Sstevel@tonic-gate 				    == EOF)
6070Sstevel@tonic-gate 					goto out;
6080Sstevel@tonic-gate 			}
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		/* print interlace */
6120Sstevel@tonic-gate 		if (rp->comps.comps_len > 1)
6130Sstevel@tonic-gate 			if (fprintf(fp, " -i %lldb", rp->interlace) == EOF)
6140Sstevel@tonic-gate 				goto out;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 		/* print continuation */
6170Sstevel@tonic-gate 		if (row != (stripep->rows.rows_len - 1))
6180Sstevel@tonic-gate 			if (fprintf(fp, " \\\n\t") == EOF)
6190Sstevel@tonic-gate 				goto out;
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	/* print hotspare name */
6230Sstevel@tonic-gate 	if (stripep->hspnamep != NULL)
6240Sstevel@tonic-gate 		if (fprintf(fp, " -h %s", stripep->hspnamep->hspname) == EOF)
6250Sstevel@tonic-gate 			goto out;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/* terminate last line */
6280Sstevel@tonic-gate 	if (fprintf(fp, "\n") == EOF)
6290Sstevel@tonic-gate 		goto out;
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	/* success */
6320Sstevel@tonic-gate 	rval = 0;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	/* cleanup, return error */
6350Sstevel@tonic-gate out:
6360Sstevel@tonic-gate 	if (rval != 0)
6370Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, fname);
6380Sstevel@tonic-gate 	return (rval);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate  * convert component state to name
6430Sstevel@tonic-gate  */
6440Sstevel@tonic-gate char *
6450Sstevel@tonic-gate comp_state_to_name(
6460Sstevel@tonic-gate 	md_comp_t	*mdcp,
6470Sstevel@tonic-gate 	md_timeval32_t	*tvp,
6480Sstevel@tonic-gate 	uint_t		tstate	/* Errored tstate flags */
6490Sstevel@tonic-gate )
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate 	comp_state_t	state = mdcp->state;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/* grab time */
6540Sstevel@tonic-gate 	if (tvp != NULL)
6550Sstevel@tonic-gate 		*tvp = mdcp->timestamp;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	if (tstate != 0) {
6580Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unavailable"));
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	/* return state */
6620Sstevel@tonic-gate 	switch (state) {
6630Sstevel@tonic-gate 	case CS_OKAY:
6640Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Okay"));
6650Sstevel@tonic-gate 	case CS_ERRED:
6660Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Maintenance"));
6670Sstevel@tonic-gate 	case CS_LAST_ERRED:
6680Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Last Erred"));
6690Sstevel@tonic-gate 	case CS_RESYNC:
6700Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resyncing"));
6710Sstevel@tonic-gate 	default:
6720Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "invalid"));
6730Sstevel@tonic-gate 	}
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate /*
6770Sstevel@tonic-gate  * print subdevice stripe row
6780Sstevel@tonic-gate  */
6790Sstevel@tonic-gate static int
6800Sstevel@tonic-gate subdev_row_report(
6810Sstevel@tonic-gate 	mdsetname_t	*sp,
6820Sstevel@tonic-gate 	md_row_t	*rp,
6830Sstevel@tonic-gate 	char		*fname,
6840Sstevel@tonic-gate 	FILE		*fp,
6850Sstevel@tonic-gate 	mdprtopts_t	options,
6860Sstevel@tonic-gate 	uint_t		top_tstate,	/* Errored tstate flags */
6870Sstevel@tonic-gate 	md_error_t	*ep
6880Sstevel@tonic-gate )
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate 	uint_t		comp;
6910Sstevel@tonic-gate 	int		rval = -1;
6920Sstevel@tonic-gate 	ddi_devid_t	dtp;
6930Sstevel@tonic-gate 	int		len = 0;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	/*
6970Sstevel@tonic-gate 	 * building a format string on the fly that will be used
6980Sstevel@tonic-gate 	 * in fprintf. This is to allow really really long ctd names
6990Sstevel@tonic-gate 	 */
7000Sstevel@tonic-gate 	for (comp = 0; (comp < rp->comps.comps_len); ++comp) {
7010Sstevel@tonic-gate 		md_comp_t	*cp = &rp->comps.comps_val[comp];
7020Sstevel@tonic-gate 		char		*cname = cp->compnamep->cname;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 		len = max(len, strlen(cname));
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	len = max(len, strlen(dgettext(TEXT_DOMAIN, "Device")));
7080Sstevel@tonic-gate 	len += 2;
7090Sstevel@tonic-gate 	/* print header */
7100Sstevel@tonic-gate 	if (! (options & PRINT_TIMES)) {
7110Sstevel@tonic-gate 		if (fprintf(fp,
7120Sstevel@tonic-gate 		    "\t%-*.*s %-12.12s %5.5s %12.12s %5.5s %s\n",
7130Sstevel@tonic-gate 		    len, len,
7140Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "Device"),
7150Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "Start Block"),
7160Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "Dbase"),
7170Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "State"),
7180Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "Reloc"),
7190Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN, "Hot Spare")) == EOF) {
7200Sstevel@tonic-gate 			goto out;
7210Sstevel@tonic-gate 		}
7220Sstevel@tonic-gate 	} else {
7230Sstevel@tonic-gate 		if (fprintf(fp,
7240Sstevel@tonic-gate 		    "\t%-*s %5s %5s %-11s %-5s %-9s %s\n",
7250Sstevel@tonic-gate 		    len,
7260Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "Device"),
7270Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "Start"),
7280Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "Dbase"),
7290Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "State"),
7300Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "Reloc"),
7310Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "Hot Spare"),
7320Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN, "Time")) == EOF) {
7330Sstevel@tonic-gate 			goto out;
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	/* print components */
7390Sstevel@tonic-gate 	for (comp = 0; (comp < rp->comps.comps_len); ++comp) {
7400Sstevel@tonic-gate 		md_comp_t	*cp = &rp->comps.comps_val[comp];
7410Sstevel@tonic-gate 		mdname_t	*namep = cp->compnamep;
7420Sstevel@tonic-gate 		char		*cname = namep->cname;
7430Sstevel@tonic-gate 		diskaddr_t	start_blk;
7440Sstevel@tonic-gate 		int		has_mddb;
7450Sstevel@tonic-gate 		char		*has_mddb_str;
7460Sstevel@tonic-gate 		char		*comp_state;
7470Sstevel@tonic-gate 		md_timeval32_t	tv;
7480Sstevel@tonic-gate 		char		*hsname = ((cp->hsnamep != NULL) ?
7490Sstevel@tonic-gate 					    cp->hsnamep->cname : "");
7500Sstevel@tonic-gate 		char		*devid = " ";
7510Sstevel@tonic-gate 		mdname_t	*didnp = NULL;
7520Sstevel@tonic-gate 		uint_t		tstate = 0;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 		/* get info */
7550Sstevel@tonic-gate 		if ((start_blk = metagetstart(sp, namep, ep)) ==
7560Sstevel@tonic-gate 		    MD_DISKADDR_ERROR) {
7570Sstevel@tonic-gate 			return (-1);
7580Sstevel@tonic-gate 		}
7590Sstevel@tonic-gate 		if ((has_mddb = metahasmddb(sp, namep, ep)) < 0) {
7600Sstevel@tonic-gate 			return (-1);
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 		if (has_mddb)
7630Sstevel@tonic-gate 			has_mddb_str = dgettext(TEXT_DOMAIN, "Yes");
7640Sstevel@tonic-gate 		else
7650Sstevel@tonic-gate 			has_mddb_str = dgettext(TEXT_DOMAIN, "No");
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 		/*
7680Sstevel@tonic-gate 		 * If the component is a metadevice, print out either
7690Sstevel@tonic-gate 		 * unavailable or the state of the metadevice, if not
7700Sstevel@tonic-gate 		 * a metadevice, print nothing if the state of the
7710Sstevel@tonic-gate 		 * stripe is unavailable
7720Sstevel@tonic-gate 		 */
7730Sstevel@tonic-gate 		if (metaismeta(namep)) {
7740Sstevel@tonic-gate 			if (meta_get_tstate(namep->dev, &tstate, ep) != 0)
7750Sstevel@tonic-gate 				return (-1);
7760Sstevel@tonic-gate 			comp_state = comp_state_to_name(cp, &tv, tstate &
7770Sstevel@tonic-gate 			    MD_DEV_ERRORED);
7780Sstevel@tonic-gate 		} else {
7790Sstevel@tonic-gate 			/*
7800Sstevel@tonic-gate 			 * if top_tstate is set, that implies that you have
7810Sstevel@tonic-gate 			 * a ctd type device with an unavailable metadevice
7820Sstevel@tonic-gate 			 * on top of it. If so, print a - for it's state
7830Sstevel@tonic-gate 			 */
7840Sstevel@tonic-gate 			if (top_tstate != 0)
7850Sstevel@tonic-gate 				comp_state = "-";
7860Sstevel@tonic-gate 			else
7870Sstevel@tonic-gate 				comp_state = comp_state_to_name(cp, &tv,
7880Sstevel@tonic-gate 				    tstate & MD_DEV_ERRORED);
7890Sstevel@tonic-gate 		}
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 		/* populate the key in the name_p structure */
7920Sstevel@tonic-gate 		if ((didnp = metadevname(&sp, namep->dev, ep))
7930Sstevel@tonic-gate 				== NULL) {
7940Sstevel@tonic-gate 			return (-1);
7950Sstevel@tonic-gate 		}
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	    /* determine if devid does NOT exist */
7980Sstevel@tonic-gate 		if (options & PRINT_DEVID) {
7990Sstevel@tonic-gate 		    if ((dtp = meta_getdidbykey(sp->setno, getmyside(sp, ep),
8000Sstevel@tonic-gate 				didnp->key, ep)) == NULL)
8010Sstevel@tonic-gate 				devid = dgettext(TEXT_DOMAIN, "No ");
8020Sstevel@tonic-gate 			else {
8030Sstevel@tonic-gate 				devid = dgettext(TEXT_DOMAIN, "Yes");
8040Sstevel@tonic-gate 				free(dtp);
8050Sstevel@tonic-gate 			}
8060Sstevel@tonic-gate 		}
8070Sstevel@tonic-gate 		/* print info */
8080Sstevel@tonic-gate 		/*
8090Sstevel@tonic-gate 		 * building a format string on the fly that will be used
8100Sstevel@tonic-gate 		 * in fprintf. This is to allow really really long ctd names
8110Sstevel@tonic-gate 		 */
8120Sstevel@tonic-gate 		if (! (options & PRINT_TIMES)) {
8130Sstevel@tonic-gate 			if (fprintf(fp,
8140Sstevel@tonic-gate 			    "\t%-*s %8lld     %-5.5s %12.12s %5.5s %s\n",
8150Sstevel@tonic-gate 			    len, cname, start_blk,
8160Sstevel@tonic-gate 			    has_mddb_str, comp_state, devid, hsname) == EOF) {
8170Sstevel@tonic-gate 				goto out;
8180Sstevel@tonic-gate 			}
8190Sstevel@tonic-gate 		} else {
8200Sstevel@tonic-gate 			char	*timep = meta_print_time(&tv);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 			if (fprintf(fp,
8230Sstevel@tonic-gate 			    "\t%-*s %5lld %-5s %-11s %-5s %-9s %s\n",
8240Sstevel@tonic-gate 			    len, cname, start_blk,
8250Sstevel@tonic-gate 			    has_mddb_str, comp_state, devid, hsname,
8260Sstevel@tonic-gate 			    timep) == EOF) {
8270Sstevel@tonic-gate 				goto out;
8280Sstevel@tonic-gate 			}
8290Sstevel@tonic-gate 		}
8300Sstevel@tonic-gate 	}
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	/* success */
8330Sstevel@tonic-gate 	rval = 0;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	/* cleanup, return error */
8360Sstevel@tonic-gate out:
8370Sstevel@tonic-gate 	if (rval != 0)
8380Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, fname);
8390Sstevel@tonic-gate 	return (rval);
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate /*
8430Sstevel@tonic-gate  * print toplevel stripe row
8440Sstevel@tonic-gate  */
8450Sstevel@tonic-gate /*ARGSUSED4*/
8460Sstevel@tonic-gate static int
8470Sstevel@tonic-gate toplev_row_report(
8480Sstevel@tonic-gate 	mdsetname_t	*sp,
8490Sstevel@tonic-gate 	md_row_t	*rp,
8500Sstevel@tonic-gate 	char		*fname,
8510Sstevel@tonic-gate 	FILE		*fp,
8520Sstevel@tonic-gate 	mdprtopts_t	options,
8530Sstevel@tonic-gate 	md_error_t	*ep
8540Sstevel@tonic-gate )
8550Sstevel@tonic-gate {
8560Sstevel@tonic-gate 	uint_t		comp;
8570Sstevel@tonic-gate 	int		rval = -1;
8580Sstevel@tonic-gate 	char		*devid = " ";
8590Sstevel@tonic-gate 	mdname_t	*didnp = NULL;
8600Sstevel@tonic-gate 	int		len = 0;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	/*
8630Sstevel@tonic-gate 	 * building a format string on the fly that will be used
8640Sstevel@tonic-gate 	 * in fprintf. This is to allow really really long ctd names
8650Sstevel@tonic-gate 	 */
8660Sstevel@tonic-gate 	for (comp = 0; (comp < rp->comps.comps_len); ++comp) {
8670Sstevel@tonic-gate 		len = max(len,
8680Sstevel@tonic-gate 		    strlen(rp->comps.comps_val[comp].compnamep->cname));
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	len = max(len, strlen(dgettext(TEXT_DOMAIN, "Device")));
8720Sstevel@tonic-gate 	len += 2;
8730Sstevel@tonic-gate 	/* print header */
8740Sstevel@tonic-gate 	if (fprintf(fp,
8750Sstevel@tonic-gate 	    "\t%-*.*s %-12.12s %-5.5s\t%s\n",
8760Sstevel@tonic-gate 	    len, len,
8770Sstevel@tonic-gate 	    dgettext(TEXT_DOMAIN, "Device"),
8780Sstevel@tonic-gate 	    dgettext(TEXT_DOMAIN, "Start Block"),
8790Sstevel@tonic-gate 	    dgettext(TEXT_DOMAIN, "Dbase"),
8800Sstevel@tonic-gate 		dgettext(TEXT_DOMAIN, "Reloc")) == EOF) {
8810Sstevel@tonic-gate 		goto out;
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	/* print components */
8850Sstevel@tonic-gate 	for (comp = 0; (comp < rp->comps.comps_len); ++comp) {
8860Sstevel@tonic-gate 		md_comp_t	*cp = &rp->comps.comps_val[comp];
8870Sstevel@tonic-gate 		mdname_t	*namep = cp->compnamep;
8880Sstevel@tonic-gate 		char		*cname = namep->cname;
8890Sstevel@tonic-gate 		diskaddr_t	start_blk;
8900Sstevel@tonic-gate 		int		has_mddb;
8910Sstevel@tonic-gate 		char		*has_mddb_str;
8920Sstevel@tonic-gate 		ddi_devid_t	dtp;
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 		/* get info */
8950Sstevel@tonic-gate 		if ((start_blk = metagetstart(sp, namep, ep)) ==
8960Sstevel@tonic-gate 		    MD_DISKADDR_ERROR) {
8970Sstevel@tonic-gate 			return (-1);
8980Sstevel@tonic-gate 		}
8990Sstevel@tonic-gate 		if ((has_mddb = metahasmddb(sp, namep, ep)) < 0) {
9000Sstevel@tonic-gate 			return (-1);
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 		if (has_mddb)
9030Sstevel@tonic-gate 			has_mddb_str = dgettext(TEXT_DOMAIN, "Yes");
9040Sstevel@tonic-gate 		else
9050Sstevel@tonic-gate 			has_mddb_str = dgettext(TEXT_DOMAIN, "No");
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 		/* populate the key in the name_p structure */
9080Sstevel@tonic-gate 		if ((didnp = metadevname(&sp, namep->dev, ep))
9090Sstevel@tonic-gate 				== NULL) {
9100Sstevel@tonic-gate 			return (-1);
9110Sstevel@tonic-gate 		}
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	    /* determine if devid does NOT exist */
9140Sstevel@tonic-gate 	    if (options & PRINT_DEVID) {
9150Sstevel@tonic-gate 		if ((dtp = meta_getdidbykey(sp->setno, getmyside(sp, ep),
9160Sstevel@tonic-gate 				didnp->key, ep)) == NULL) {
9170Sstevel@tonic-gate 				devid = dgettext(TEXT_DOMAIN, "No ");
9180Sstevel@tonic-gate 			} else {
9190Sstevel@tonic-gate 				devid = dgettext(TEXT_DOMAIN, "Yes");
9200Sstevel@tonic-gate 				free(dtp);
9210Sstevel@tonic-gate 			}
9220Sstevel@tonic-gate 		}
9230Sstevel@tonic-gate 		/* print info */
9240Sstevel@tonic-gate 		/*
9250Sstevel@tonic-gate 		 * building a format string on the fly that will be used
9260Sstevel@tonic-gate 		 * in fprintf. This is to allow really really long ctd names
9270Sstevel@tonic-gate 		 */
9280Sstevel@tonic-gate 		if (fprintf(fp,
9290Sstevel@tonic-gate 		    "\t%-*s %8lld     %-5.5s\t%s\n", len,
9300Sstevel@tonic-gate 		    cname, start_blk, has_mddb_str, devid) == EOF) {
9310Sstevel@tonic-gate 			goto out;
9320Sstevel@tonic-gate 		}
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	/* success */
9360Sstevel@tonic-gate 	rval = 0;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	/* cleanup, return error */
9390Sstevel@tonic-gate out:
9400Sstevel@tonic-gate 	if (rval != 0)
9410Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, fname);
9420Sstevel@tonic-gate 	return (rval);
9430Sstevel@tonic-gate }
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate /*
9460Sstevel@tonic-gate  * print stripe options
9470Sstevel@tonic-gate  */
9480Sstevel@tonic-gate int
9490Sstevel@tonic-gate meta_print_stripe_options(
9500Sstevel@tonic-gate 	mdhspname_t	*hspnamep,
9510Sstevel@tonic-gate 	char		*fname,
9520Sstevel@tonic-gate 	FILE		*fp,
9530Sstevel@tonic-gate 	md_error_t	*ep
9540Sstevel@tonic-gate )
9550Sstevel@tonic-gate {
9560Sstevel@tonic-gate 	char		*hspname = ((hspnamep != NULL) ? hspnamep->hspname :
9570Sstevel@tonic-gate 					dgettext(TEXT_DOMAIN, "none"));
9580Sstevel@tonic-gate 	int		rval = -1;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	/* print options */
9610Sstevel@tonic-gate 	if (fprintf(fp, dgettext(TEXT_DOMAIN,
9620Sstevel@tonic-gate 	    "    Hot spare pool: %s\n"), hspname) == EOF) {
9630Sstevel@tonic-gate 		goto out;
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	/* success */
9670Sstevel@tonic-gate 	rval = 0;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	/* cleanup, return error */
9700Sstevel@tonic-gate out:
9710Sstevel@tonic-gate 	if (rval != 0)
9720Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, fname);
9730Sstevel@tonic-gate 	return (rval);
9740Sstevel@tonic-gate }
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate /*
9770Sstevel@tonic-gate  * report stripe
9780Sstevel@tonic-gate  */
9790Sstevel@tonic-gate static int
9800Sstevel@tonic-gate stripe_report(
9810Sstevel@tonic-gate 	mdsetname_t	*sp,
9820Sstevel@tonic-gate 	md_stripe_t	*stripep,
9830Sstevel@tonic-gate 	mdnamelist_t	**nlpp,
9840Sstevel@tonic-gate 	char		*fname,
9850Sstevel@tonic-gate 	FILE		*fp,
9860Sstevel@tonic-gate 	mdprtopts_t	options,
9870Sstevel@tonic-gate 	md_error_t	*ep
9880Sstevel@tonic-gate )
9890Sstevel@tonic-gate {
9900Sstevel@tonic-gate 	uint_t		row;
9910Sstevel@tonic-gate 	int		rval = -1;
9920Sstevel@tonic-gate 	uint_t		tstate = 0;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	/*
9950Sstevel@tonic-gate 	 * if the -B option has been specified check to see if the
9960Sstevel@tonic-gate 	 * metadevice is s "big" one and print if so, also if a
9970Sstevel@tonic-gate 	 * big device we need to store the ctd involved for use in
9980Sstevel@tonic-gate 	 * printing out the relocation information.
9990Sstevel@tonic-gate 	 */
10000Sstevel@tonic-gate 	if (options & PRINT_LARGEDEVICES) {
10010Sstevel@tonic-gate 		if (stripep->common.revision != MD_64BIT_META_DEV) {
10020Sstevel@tonic-gate 			rval = 0;
10030Sstevel@tonic-gate 			goto out;
10040Sstevel@tonic-gate 		} else {
10050Sstevel@tonic-gate 			if (meta_getdevs(sp, stripep->common.namep,
10060Sstevel@tonic-gate 			    nlpp, ep) != 0)
10070Sstevel@tonic-gate 				goto out;
10080Sstevel@tonic-gate 		}
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	/* print header */
10120Sstevel@tonic-gate 	if (options & PRINT_HEADER) {
10130Sstevel@tonic-gate 		if (fprintf(fp, "%s: Concat/Stripe\n",
10140Sstevel@tonic-gate 		    stripep->common.namep->cname) == EOF) {
10150Sstevel@tonic-gate 			goto out;
10160Sstevel@tonic-gate 		}
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/* print hotspare pool */
10210Sstevel@tonic-gate 	if (stripep->hspnamep != NULL) {
10220Sstevel@tonic-gate 		if (meta_print_stripe_options(stripep->hspnamep,
10230Sstevel@tonic-gate 		    fname, fp, ep) != 0) {
10240Sstevel@tonic-gate 			return (-1);
10250Sstevel@tonic-gate 		}
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	if (metaismeta(stripep->common.namep)) {
10290Sstevel@tonic-gate 		if (meta_get_tstate(stripep->common.namep->dev, &tstate, ep)
10300Sstevel@tonic-gate 		    != 0)
10310Sstevel@tonic-gate 			return (-1);
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 	if ((tstate & MD_DEV_ERRORED) != 0) {
10340Sstevel@tonic-gate 		if (fprintf(fp, dgettext(TEXT_DOMAIN,
10350Sstevel@tonic-gate 		    "    State: Unavailable\n"
10360Sstevel@tonic-gate 		    "    Reconnect disk and invoke: metastat -i\n")) == EOF) {
10370Sstevel@tonic-gate 			goto out;
10380Sstevel@tonic-gate 		}
10390Sstevel@tonic-gate 	}
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	/* print size */
10420Sstevel@tonic-gate 	if (fprintf(fp, dgettext(TEXT_DOMAIN, "    Size: %lld blocks (%s)\n"),
10430Sstevel@tonic-gate 	    stripep->common.size,
10440Sstevel@tonic-gate 	    meta_number_to_string(stripep->common.size, DEV_BSIZE))
10450Sstevel@tonic-gate 	    == EOF) {
10460Sstevel@tonic-gate 		goto out;
10470Sstevel@tonic-gate 	}
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	/* print rows */
10500Sstevel@tonic-gate 	for (row = 0; (row < stripep->rows.rows_len); ++row) {
10510Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 		/* print stripe and interlace */
10540Sstevel@tonic-gate 		if (rp->comps.comps_len > 1) {
10550Sstevel@tonic-gate 			if (fprintf(fp, dgettext(TEXT_DOMAIN,
10560Sstevel@tonic-gate 			    "    Stripe %u: (interlace: %lld blocks)\n"),
10570Sstevel@tonic-gate 			    row, rp->interlace) == EOF) {
10580Sstevel@tonic-gate 				goto out;
10590Sstevel@tonic-gate 			}
10600Sstevel@tonic-gate 		} else {
10610Sstevel@tonic-gate 			if (fprintf(fp, dgettext(TEXT_DOMAIN,
10620Sstevel@tonic-gate 			    "    Stripe %u:\n"),
10630Sstevel@tonic-gate 			    row) == EOF) {
10640Sstevel@tonic-gate 				goto out;
10650Sstevel@tonic-gate 			}
10660Sstevel@tonic-gate 		}
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 		/* print components appropriately */
10690Sstevel@tonic-gate 		if (MD_HAS_PARENT(stripep->common.parent)) {
10700Sstevel@tonic-gate 			if (subdev_row_report(sp, rp, fname, fp, options,
10710Sstevel@tonic-gate 			    tstate & MD_DEV_ERRORED, ep) != 0) {
10720Sstevel@tonic-gate 				return (-1);
10730Sstevel@tonic-gate 			}
10740Sstevel@tonic-gate 		} else {
10750Sstevel@tonic-gate 			if (toplev_row_report(sp, rp, fname, fp, options,
10760Sstevel@tonic-gate 			    ep) != 0) {
10770Sstevel@tonic-gate 				return (-1);
10780Sstevel@tonic-gate 			}
10790Sstevel@tonic-gate 		}
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	/* add extra line */
10830Sstevel@tonic-gate 	if (fprintf(fp, "\n") == EOF)
10840Sstevel@tonic-gate 		goto out;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	/* success */
10870Sstevel@tonic-gate 	rval = 0;
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	/* cleanup, return error */
10900Sstevel@tonic-gate out:
10910Sstevel@tonic-gate 	if (rval != 0)
10920Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, fname);
10930Sstevel@tonic-gate 	return (rval);
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate /*
10970Sstevel@tonic-gate  * print/report stripe
10980Sstevel@tonic-gate  */
10990Sstevel@tonic-gate int
11000Sstevel@tonic-gate meta_stripe_print(
11010Sstevel@tonic-gate 	mdsetname_t	*sp,
11020Sstevel@tonic-gate 	mdname_t	*stripenp,
11030Sstevel@tonic-gate 	mdnamelist_t	**nlpp,
11040Sstevel@tonic-gate 	char		*fname,
11050Sstevel@tonic-gate 	FILE		*fp,
11060Sstevel@tonic-gate 	mdprtopts_t	options,
11070Sstevel@tonic-gate 	md_error_t	*ep
11080Sstevel@tonic-gate )
11090Sstevel@tonic-gate {
11100Sstevel@tonic-gate 	md_stripe_t	*stripep;
11110Sstevel@tonic-gate 	int		row, comp;
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	/* should have same set */
11140Sstevel@tonic-gate 	assert(sp != NULL);
11150Sstevel@tonic-gate 	assert((stripenp == NULL) ||
11160Sstevel@tonic-gate 	    (sp->setno == MD_MIN2SET(meta_getminor(stripenp->dev))));
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	/* print all stripes */
11190Sstevel@tonic-gate 	if (stripenp == NULL) {
11200Sstevel@tonic-gate 		mdnamelist_t	*nlp = NULL;
11210Sstevel@tonic-gate 		mdnamelist_t	*p;
11220Sstevel@tonic-gate 		int		cnt;
11230Sstevel@tonic-gate 		int		rval = 0;
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 		/* get list */
11260Sstevel@tonic-gate 		if ((cnt = meta_get_stripe_names(sp, &nlp, options, ep)) < 0)
11270Sstevel@tonic-gate 			return (-1);
11280Sstevel@tonic-gate 		else if (cnt == 0)
11290Sstevel@tonic-gate 			return (0);
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 		/* recurse */
11320Sstevel@tonic-gate 		for (p = nlp; (p != NULL); p = p->next) {
11330Sstevel@tonic-gate 			mdname_t	*np = p->namep;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 			if (meta_stripe_print(sp, np, nlpp, fname, fp,
11360Sstevel@tonic-gate 			    options, ep) != 0)
11370Sstevel@tonic-gate 				rval = -1;
11380Sstevel@tonic-gate 		}
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 		/* cleanup, return success */
11410Sstevel@tonic-gate 		metafreenamelist(nlp);
11420Sstevel@tonic-gate 		return (rval);
11430Sstevel@tonic-gate 	}
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	/* get unit structure */
11460Sstevel@tonic-gate 	if ((stripep = meta_get_stripe_common(sp, stripenp,
11470Sstevel@tonic-gate 	    ((options & PRINT_FAST) ? 1 : 0), ep)) == NULL)
11480Sstevel@tonic-gate 		return (-1);
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	/* check for parented */
11510Sstevel@tonic-gate 	if ((! (options & PRINT_SUBDEVS)) &&
11520Sstevel@tonic-gate 	    (MD_HAS_PARENT(stripep->common.parent))) {
11530Sstevel@tonic-gate 		return (0);
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	/* print appropriate detail */
11570Sstevel@tonic-gate 	if (options & PRINT_SHORT) {
11580Sstevel@tonic-gate 		if (stripe_print(stripep, fname, fp, options, ep) != 0)
11590Sstevel@tonic-gate 			return (-1);
11600Sstevel@tonic-gate 	} else {
11610Sstevel@tonic-gate 		if (stripe_report(sp, stripep, nlpp, fname, fp, options,
11620Sstevel@tonic-gate 		    ep) != 0)
11630Sstevel@tonic-gate 			return (-1);
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	/* Recurse on components that are metadevices */
11670Sstevel@tonic-gate 	for (row = 0; (row < stripep->rows.rows_len); ++row) {
11680Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 		/* look for components that are metadevices */
11710Sstevel@tonic-gate 		for (comp = 0; (comp < rp->comps.comps_len); ++comp) {
11720Sstevel@tonic-gate 			md_comp_t	*cp = &rp->comps.comps_val[comp];
11730Sstevel@tonic-gate 			mdname_t	*namep = cp->compnamep;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 			if ((metaismeta(namep)) &&
11760Sstevel@tonic-gate 			    (meta_print_name(sp, namep, nlpp, fname, fp,
11770Sstevel@tonic-gate 			    (options | PRINT_HEADER | PRINT_SUBDEVS),
11780Sstevel@tonic-gate 			    NULL, ep) != 0)) {
11790Sstevel@tonic-gate 				return (-1);
11800Sstevel@tonic-gate 			}
11810Sstevel@tonic-gate 		}
11820Sstevel@tonic-gate 	}
11830Sstevel@tonic-gate 	return (0);
11840Sstevel@tonic-gate }
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate /*
11870Sstevel@tonic-gate  * find stripe component to replace
11880Sstevel@tonic-gate  */
11890Sstevel@tonic-gate int
11900Sstevel@tonic-gate meta_find_erred_comp(
11910Sstevel@tonic-gate 	mdsetname_t	*sp,
11920Sstevel@tonic-gate 	mdname_t	*stripenp,
11930Sstevel@tonic-gate 	mdname_t	**compnpp,
11940Sstevel@tonic-gate 	comp_state_t	*compstate,
11950Sstevel@tonic-gate 	md_error_t	*ep
11960Sstevel@tonic-gate )
11970Sstevel@tonic-gate {
11980Sstevel@tonic-gate 	md_stripe_t	*stripep;
11990Sstevel@tonic-gate 	md_comp_t	*compp = NULL;
12000Sstevel@tonic-gate 	uint_t		lasterrcnt = 0;
12010Sstevel@tonic-gate 	uint_t		row;
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	/* get stripe */
12040Sstevel@tonic-gate 	*compnpp = NULL;
12050Sstevel@tonic-gate 	if ((stripep = meta_get_stripe_common(sp, stripenp, 1, ep)) == NULL)
12060Sstevel@tonic-gate 		return (-1);
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	/*
12090Sstevel@tonic-gate 	 * Try to find the first erred component.
12100Sstevel@tonic-gate 	 * If there is not one, then look for the
12110Sstevel@tonic-gate 	 *	first last_erred component.
12120Sstevel@tonic-gate 	 */
12130Sstevel@tonic-gate 	for (row = 0; (row < stripep->rows.rows_len); ++row) {
12140Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
12150Sstevel@tonic-gate 		uint_t		comp;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 		for (comp = 0; (comp < rp->comps.comps_len); ++comp) {
12180Sstevel@tonic-gate 			md_comp_t	*cp = &rp->comps.comps_val[comp];
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 			if ((cp->state == CS_ERRED) && ((compp == NULL) ||
12210Sstevel@tonic-gate 			    (cp->lasterrcnt < lasterrcnt))) {
12220Sstevel@tonic-gate 				compp = cp;
12230Sstevel@tonic-gate 				lasterrcnt = cp->lasterrcnt;
12240Sstevel@tonic-gate 			}
12250Sstevel@tonic-gate 		}
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate 	for (row = 0; (row < stripep->rows.rows_len); ++row) {
12280Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
12290Sstevel@tonic-gate 		uint_t		comp;
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 		for (comp = 0; (comp < rp->comps.comps_len); ++comp) {
12320Sstevel@tonic-gate 			md_comp_t	*cp = &rp->comps.comps_val[comp];
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 			if ((cp->state == CS_LAST_ERRED) && ((compp == NULL) ||
12350Sstevel@tonic-gate 			    (cp->lasterrcnt < lasterrcnt))) {
12360Sstevel@tonic-gate 				compp = cp;
12370Sstevel@tonic-gate 				lasterrcnt = cp->lasterrcnt;
12380Sstevel@tonic-gate 			}
12390Sstevel@tonic-gate 		}
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	/* return component */
12430Sstevel@tonic-gate 	if (compp != NULL) {
12440Sstevel@tonic-gate 		*compnpp = compp->compnamep;
12450Sstevel@tonic-gate 		*compstate = compp->state;
12460Sstevel@tonic-gate 	}
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	/* return success */
12490Sstevel@tonic-gate 	return (0);
12500Sstevel@tonic-gate }
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate /*
12530Sstevel@tonic-gate  * invalidate component names
12540Sstevel@tonic-gate  */
12550Sstevel@tonic-gate static int
12560Sstevel@tonic-gate invalidate_components(
12570Sstevel@tonic-gate 	mdsetname_t	*sp,
12580Sstevel@tonic-gate 	mdname_t	*stripenp,
12590Sstevel@tonic-gate 	md_error_t	*ep
12600Sstevel@tonic-gate )
12610Sstevel@tonic-gate {
12620Sstevel@tonic-gate 	md_stripe_t	*stripep;
12630Sstevel@tonic-gate 	uint_t		row;
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	if ((stripep = meta_get_stripe(sp, stripenp, ep)) == NULL)
12660Sstevel@tonic-gate 		return (-1);
12670Sstevel@tonic-gate 	for (row = 0; (row < stripep->rows.rows_len); ++row) {
12680Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
12690Sstevel@tonic-gate 		uint_t		comp;
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 		for (comp = 0; (comp < rp->comps.comps_len); ++comp) {
12720Sstevel@tonic-gate 			md_comp_t	*cp = &rp->comps.comps_val[comp];
12730Sstevel@tonic-gate 			mdname_t	*compnp = cp->compnamep;
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 			meta_invalidate_name(compnp);
12760Sstevel@tonic-gate 		}
12770Sstevel@tonic-gate 	}
12780Sstevel@tonic-gate 	return (0);
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate /*
12820Sstevel@tonic-gate  * attach components to stripe
12830Sstevel@tonic-gate  */
12840Sstevel@tonic-gate int
12850Sstevel@tonic-gate meta_stripe_attach(
12860Sstevel@tonic-gate 	mdsetname_t		*sp,
12870Sstevel@tonic-gate 	mdname_t		*stripenp,
12880Sstevel@tonic-gate 	mdnamelist_t		*nlp,
12890Sstevel@tonic-gate 	diskaddr_t		interlace,
12900Sstevel@tonic-gate 	mdcmdopts_t		options,
12910Sstevel@tonic-gate 	md_error_t		*ep
12920Sstevel@tonic-gate )
12930Sstevel@tonic-gate {
12940Sstevel@tonic-gate 	mdnamelist_t		*lp;
12950Sstevel@tonic-gate 	ms_unit_t		*old_un, *new_un;
12960Sstevel@tonic-gate 	struct ms_row		*mdr, *new_mdr;
12970Sstevel@tonic-gate 	uint_t			newcomps, ncomps, icomp;
12980Sstevel@tonic-gate 	uint_t			row;
12990Sstevel@tonic-gate 	size_t			mdsize, first_comp;
13000Sstevel@tonic-gate 	diskaddr_t		new_blks;
13010Sstevel@tonic-gate 	diskaddr_t		limit;
13020Sstevel@tonic-gate 	diskaddr_t		disk_size = 0;
13030Sstevel@tonic-gate 	ms_comp_t		*mdcomp, *new_comp;
13040Sstevel@tonic-gate 	uint_t			write_reinstruct = 0;
13050Sstevel@tonic-gate 	uint_t			read_reinstruct = 0;
13060Sstevel@tonic-gate 	mdnamelist_t		*keynlp = NULL;
13070Sstevel@tonic-gate 	uint_t			round_cyl = 1;
13080Sstevel@tonic-gate 	minor_t			parent;
13090Sstevel@tonic-gate 	md_grow_params_t	mgp;
13100Sstevel@tonic-gate 	int			rval = -1;
13110Sstevel@tonic-gate 	md_timeval32_t		creation_time;
13120Sstevel@tonic-gate 	int			create_flag = MD_CRO_32BIT;
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	/* should have a set */
13150Sstevel@tonic-gate 	assert(sp != NULL);
13160Sstevel@tonic-gate 	assert(sp->setno == MD_MIN2SET(meta_getminor(stripenp->dev)));
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	/* check type */
13190Sstevel@tonic-gate 	if (metachkmeta(stripenp, ep) != 0)
13200Sstevel@tonic-gate 		return (-1);
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	/* check and count components */
13230Sstevel@tonic-gate 	assert(nlp != NULL);
13240Sstevel@tonic-gate 	newcomps = 0;
13250Sstevel@tonic-gate 	for (lp = nlp; (lp != NULL); lp = lp->next) {
13260Sstevel@tonic-gate 		mdname_t	*np = lp->namep;
13270Sstevel@tonic-gate 		mdnamelist_t	*p;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 		/* check against existing devices */
13300Sstevel@tonic-gate 		if (meta_check_component(sp, np, 0, ep) != 0)
13310Sstevel@tonic-gate 			return (-1);
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 		/* check against ourselves */
13340Sstevel@tonic-gate 		for (p = lp->next; (p != NULL); p = p->next) {
13350Sstevel@tonic-gate 			if (meta_check_overlap(np->cname, np, 0, -1,
13360Sstevel@tonic-gate 			    p->namep, 0, -1, ep) != 0) {
13370Sstevel@tonic-gate 				return (-1);
13380Sstevel@tonic-gate 			}
13390Sstevel@tonic-gate 		}
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 		/* count */
13420Sstevel@tonic-gate 		++newcomps;
13430Sstevel@tonic-gate 	}
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	/* get old unit */
13460Sstevel@tonic-gate 	if ((old_un = (ms_unit_t *)meta_get_mdunit(sp, stripenp, ep)) == NULL)
13470Sstevel@tonic-gate 		return (-1);
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	/* if zero, inherit the last rows interlace value */
13500Sstevel@tonic-gate 	if (interlace == 0) {
13510Sstevel@tonic-gate 		mdr = &old_un->un_row[old_un->un_nrows - 1];
13520Sstevel@tonic-gate 		interlace = mdr->un_interlace;
13530Sstevel@tonic-gate 	}
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	/*
13560Sstevel@tonic-gate 	 * calculate size of new unit structure
13570Sstevel@tonic-gate 	 */
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	/* unit + rows */
13600Sstevel@tonic-gate 	mdsize = sizeof (ms_unit_t) - sizeof (struct ms_row);
13610Sstevel@tonic-gate 	mdsize += sizeof (struct ms_row) * (old_un->un_nrows + 1);
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	/* number of new components being added */
13640Sstevel@tonic-gate 	ncomps = newcomps;
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	/* count the # of components in the old unit */
13670Sstevel@tonic-gate 	mdr = &old_un->un_row[0];
13680Sstevel@tonic-gate 	for (row = 0; (row < old_un->un_nrows); row++)
13690Sstevel@tonic-gate 		ncomps += mdr[row].un_ncomp;
13700Sstevel@tonic-gate 	first_comp = roundup(mdsize, sizeof (long long));
13710Sstevel@tonic-gate 	mdsize += sizeof (ms_comp_t) * ncomps + (first_comp - mdsize);
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	/* allocate new unit */
13740Sstevel@tonic-gate 	new_un = Zalloc(mdsize);
13750Sstevel@tonic-gate 	new_un->un_ocomp = first_comp;
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	/* compute new data */
13780Sstevel@tonic-gate 	new_mdr = &new_un->un_row[old_un->un_nrows];
13790Sstevel@tonic-gate 	new_mdr->un_icomp = ncomps - newcomps;
13800Sstevel@tonic-gate 	new_mdr->un_ncomp = newcomps;
13810Sstevel@tonic-gate 	new_mdr->un_blocks = 0;
13820Sstevel@tonic-gate 	new_mdr->un_cum_blocks =
13830Sstevel@tonic-gate 	    old_un->un_row[old_un->un_nrows - 1].un_cum_blocks;
13840Sstevel@tonic-gate 	new_mdr->un_interlace = interlace;
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	/* for each new device */
13870Sstevel@tonic-gate 	mdcomp = (struct ms_comp *)(void *)&((char *)new_un)[new_un->un_ocomp];
13880Sstevel@tonic-gate 	icomp = new_mdr->un_icomp;
13890Sstevel@tonic-gate 	if (meta_gettimeofday(&creation_time) == -1)
13900Sstevel@tonic-gate 		return (mdsyserror(ep, errno, NULL));
13910Sstevel@tonic-gate 	for (lp = nlp; (lp != NULL); lp = lp->next) {
13920Sstevel@tonic-gate 		mdname_t	*np = lp->namep;
13930Sstevel@tonic-gate 		diskaddr_t	size, start_blk;
13940Sstevel@tonic-gate 		mdgeom_t		*geomp;
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 		/* figure out how big */
13970Sstevel@tonic-gate 		if ((size = metagetsize(np, ep)) == MD_DISKADDR_ERROR)
13980Sstevel@tonic-gate 			goto out;
13990Sstevel@tonic-gate 		if ((start_blk = metagetstart(sp, np, ep)) ==
14000Sstevel@tonic-gate 		    MD_DISKADDR_ERROR)
14010Sstevel@tonic-gate 			goto out;
14020Sstevel@tonic-gate 		if (start_blk >= size) {
14030Sstevel@tonic-gate 			(void) mdsyserror(ep, ENOSPC, np->cname);
14040Sstevel@tonic-gate 			goto out;
14050Sstevel@tonic-gate 		}
14060Sstevel@tonic-gate 		size -= start_blk;
14070Sstevel@tonic-gate 		if (newcomps > 1)
14080Sstevel@tonic-gate 			size = rounddown(size, interlace);
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 		/* adjust for smallest disk */
14110Sstevel@tonic-gate 		if (disk_size == 0) {
14120Sstevel@tonic-gate 			disk_size = size;
14130Sstevel@tonic-gate 		} else if (size < disk_size) {
14140Sstevel@tonic-gate 			disk_size = size;
14150Sstevel@tonic-gate 		}
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 		/* get worst reinstructs */
14180Sstevel@tonic-gate 		if ((geomp = metagetgeom(np, ep)) == NULL)
14190Sstevel@tonic-gate 			goto out;
14200Sstevel@tonic-gate 		if (geomp->write_reinstruct > write_reinstruct)
14210Sstevel@tonic-gate 			write_reinstruct = geomp->write_reinstruct;
14220Sstevel@tonic-gate 		if (geomp->read_reinstruct > read_reinstruct)
14230Sstevel@tonic-gate 			read_reinstruct = geomp->read_reinstruct;
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 		/* In dryrun mode (DOIT not set) we must not alter the mddb */
14260Sstevel@tonic-gate 		if (options & MDCMD_DOIT) {
14270Sstevel@tonic-gate 			/* store name in namespace */
14280Sstevel@tonic-gate 			if (add_key_name(sp, np, &keynlp, ep) != 0)
14290Sstevel@tonic-gate 				goto out;
14300Sstevel@tonic-gate 		}
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 		/* build new component */
14330Sstevel@tonic-gate 		new_comp = &mdcomp[icomp++];
14340Sstevel@tonic-gate 		new_comp->un_key = np->key;
14350Sstevel@tonic-gate 		new_comp->un_dev = np->dev;
14360Sstevel@tonic-gate 		new_comp->un_start_block = start_blk;
14370Sstevel@tonic-gate 		new_comp->un_mirror.ms_state = CS_OKAY;
14380Sstevel@tonic-gate 		new_comp->un_mirror.ms_timestamp = creation_time;
14390Sstevel@tonic-gate 	}
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 	limit = LLONG_MAX;
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	/* compute new size */
14440Sstevel@tonic-gate 	new_mdr->un_blocks = new_mdr->un_ncomp * disk_size;
14450Sstevel@tonic-gate 	new_blks = new_mdr->un_cum_blocks + new_mdr->un_blocks;
14460Sstevel@tonic-gate 	if (new_blks > limit) {
14470Sstevel@tonic-gate 		new_mdr->un_cum_blocks = limit;
14480Sstevel@tonic-gate 		new_blks = limit;
14490Sstevel@tonic-gate 		md_eprintf(dgettext(TEXT_DOMAIN,
14500Sstevel@tonic-gate 		    "unit size overflow, limit is %lld blocks\n"),
14510Sstevel@tonic-gate 		    limit);
14520Sstevel@tonic-gate 	} else {
14530Sstevel@tonic-gate 		new_mdr->un_cum_blocks += new_mdr->un_blocks;
14540Sstevel@tonic-gate 	}
14550Sstevel@tonic-gate 	new_un->c.un_actual_tb = new_mdr->un_cum_blocks;
14560Sstevel@tonic-gate 	new_un->un_nrows = old_un->un_nrows + 1;
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 	/* adjust geometry */
14590Sstevel@tonic-gate 	new_un->c.un_nhead = old_un->c.un_nhead;
14600Sstevel@tonic-gate 	new_un->c.un_nsect = old_un->c.un_nsect;
14610Sstevel@tonic-gate 	new_un->c.un_rpm = old_un->c.un_rpm;
14620Sstevel@tonic-gate 	new_un->c.un_wr_reinstruct = old_un->c.un_wr_reinstruct;
14630Sstevel@tonic-gate 	new_un->c.un_rd_reinstruct = old_un->c.un_rd_reinstruct;
14640Sstevel@tonic-gate 	if (meta_adjust_geom((md_unit_t *)new_un, stripenp,
14650Sstevel@tonic-gate 	    write_reinstruct, read_reinstruct, round_cyl, ep) != 0)
14660Sstevel@tonic-gate 		goto out;
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	/* if in dryrun mode, we are done here. */
14690Sstevel@tonic-gate 	if ((options & MDCMD_DOIT) == 0)  {
14700Sstevel@tonic-gate 		if (options & MDCMD_PRINT) {
14710Sstevel@tonic-gate 			if (newcomps == 1) {
14720Sstevel@tonic-gate 				(void) printf(dgettext(TEXT_DOMAIN,
14730Sstevel@tonic-gate 				"%s: attaching component would suceed\n"),
14740Sstevel@tonic-gate 				stripenp->cname);
14750Sstevel@tonic-gate 			} else {
14760Sstevel@tonic-gate 				(void) printf(dgettext(TEXT_DOMAIN,
14770Sstevel@tonic-gate 				"%s: attaching components would suceed\n"),
14780Sstevel@tonic-gate 				stripenp->cname);
14790Sstevel@tonic-gate 			}
14800Sstevel@tonic-gate 		}
14810Sstevel@tonic-gate 		rval = 0; /* success */
14820Sstevel@tonic-gate 		goto out;
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	create_flag = meta_check_devicesize(new_un->c.un_total_blocks);
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	/* grow stripe */
14880Sstevel@tonic-gate 	(void) memset(&mgp, 0, sizeof (mgp));
14890Sstevel@tonic-gate 	mgp.mnum = MD_SID(old_un);
14900Sstevel@tonic-gate 	MD_SETDRIVERNAME(&mgp, MD_STRIPE, sp->setno);
14910Sstevel@tonic-gate 	mgp.size = mdsize;
14920Sstevel@tonic-gate 	mgp.mdp = (uintptr_t)new_un;
14930Sstevel@tonic-gate 	mgp.nrows = old_un->un_nrows;
14940Sstevel@tonic-gate 	if (create_flag == MD_CRO_32BIT) {
14950Sstevel@tonic-gate 		mgp.options = MD_CRO_32BIT;
14960Sstevel@tonic-gate 		new_un->c.un_revision = MD_32BIT_META_DEV;
14970Sstevel@tonic-gate 	} else {
14980Sstevel@tonic-gate 		mgp.options = MD_CRO_64BIT;
14990Sstevel@tonic-gate 		new_un->c.un_revision = MD_64BIT_META_DEV;
15000Sstevel@tonic-gate 	}
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	if ((MD_HAS_PARENT(old_un->c.un_parent)) &&
15030Sstevel@tonic-gate 	    (old_un->c.un_parent != MD_MULTI_PARENT)) {
15040Sstevel@tonic-gate 		mgp.npar = 1;
15050Sstevel@tonic-gate 		parent = old_un->c.un_parent;
15060Sstevel@tonic-gate 		mgp.par = (uintptr_t)(&parent);
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	if (metaioctl(MD_IOCGROW, &mgp, &mgp.mde, NULL) != 0) {
15100Sstevel@tonic-gate 		(void) mdstealerror(ep, &mgp.mde);
15110Sstevel@tonic-gate 		goto out;
15120Sstevel@tonic-gate 	}
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	/* clear cache */
15150Sstevel@tonic-gate 	if (invalidate_components(sp, stripenp, ep) != 0)
15160Sstevel@tonic-gate 		goto out;
15170Sstevel@tonic-gate 	meta_invalidate_name(stripenp);
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	/* let em know */
15200Sstevel@tonic-gate 	if (options & MDCMD_PRINT) {
15210Sstevel@tonic-gate 		if (newcomps == 1) {
15220Sstevel@tonic-gate 			(void) printf(dgettext(TEXT_DOMAIN,
15230Sstevel@tonic-gate 			    "%s: component is attached\n"), stripenp->cname);
15240Sstevel@tonic-gate 		} else {
15250Sstevel@tonic-gate 			(void) printf(dgettext(TEXT_DOMAIN,
15260Sstevel@tonic-gate 			    "%s: components are attached\n"), stripenp->cname);
15270Sstevel@tonic-gate 		}
15280Sstevel@tonic-gate 		(void) fflush(stdout);
15290Sstevel@tonic-gate 	}
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 	/* grow any parents */
15320Sstevel@tonic-gate 	if (meta_concat_parent(sp, stripenp, ep) != 0)
15330Sstevel@tonic-gate 		return (-1);
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	rval = 0;	/* success */
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	/* cleanup, return error */
15380Sstevel@tonic-gate out:
15390Sstevel@tonic-gate 	Free(old_un);
15400Sstevel@tonic-gate 	Free(new_un);
15410Sstevel@tonic-gate 	if (options & MDCMD_DOIT) {
15420Sstevel@tonic-gate 		if (rval != 0)
15430Sstevel@tonic-gate 			(void) del_key_names(sp, keynlp, NULL);
15440Sstevel@tonic-gate 		metafreenamelist(keynlp);
15450Sstevel@tonic-gate 	}
15460Sstevel@tonic-gate 	return (rval);
15470Sstevel@tonic-gate }
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate /*
15500Sstevel@tonic-gate  * get stripe parameters
15510Sstevel@tonic-gate  */
15520Sstevel@tonic-gate int
15530Sstevel@tonic-gate meta_stripe_get_params(
15540Sstevel@tonic-gate 	mdsetname_t	*sp,
15550Sstevel@tonic-gate 	mdname_t	*stripenp,
15560Sstevel@tonic-gate 	ms_params_t	*paramsp,
15570Sstevel@tonic-gate 	md_error_t	*ep
15580Sstevel@tonic-gate )
15590Sstevel@tonic-gate {
15600Sstevel@tonic-gate 	md_stripe_t	*stripep;
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	/* should have a set */
15630Sstevel@tonic-gate 	assert(sp != NULL);
15640Sstevel@tonic-gate 	assert(sp->setno == MD_MIN2SET(meta_getminor(stripenp->dev)));
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	/* check name */
15670Sstevel@tonic-gate 	if (metachkmeta(stripenp, ep) != 0)
15680Sstevel@tonic-gate 		return (-1);
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	/* get unit */
15710Sstevel@tonic-gate 	if ((stripep = meta_get_stripe(sp, stripenp, ep)) == NULL)
15720Sstevel@tonic-gate 		return (-1);
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	/* return parameters */
15750Sstevel@tonic-gate 	(void) memset(paramsp, 0, sizeof (*paramsp));
15760Sstevel@tonic-gate 	if (stripep->hspnamep == NULL)
15770Sstevel@tonic-gate 		paramsp->hsp_id = MD_HSP_NONE;
15780Sstevel@tonic-gate 	else
15790Sstevel@tonic-gate 		paramsp->hsp_id = stripep->hspnamep->hsp;
15800Sstevel@tonic-gate 	return (0);
15810Sstevel@tonic-gate }
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate /*
15840Sstevel@tonic-gate  * set stripe parameters
15850Sstevel@tonic-gate  */
15860Sstevel@tonic-gate int
15870Sstevel@tonic-gate meta_stripe_set_params(
15880Sstevel@tonic-gate 	mdsetname_t		*sp,
15890Sstevel@tonic-gate 	mdname_t		*stripenp,
15900Sstevel@tonic-gate 	ms_params_t		*paramsp,
15910Sstevel@tonic-gate 	md_error_t		*ep
15920Sstevel@tonic-gate )
15930Sstevel@tonic-gate {
15940Sstevel@tonic-gate 	md_stripe_params_t	msp;
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	/* should have a set */
15970Sstevel@tonic-gate 	assert(sp != NULL);
15980Sstevel@tonic-gate 	assert(sp->setno == MD_MIN2SET(meta_getminor(stripenp->dev)));
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	/* check name */
16010Sstevel@tonic-gate 	if (metachkmeta(stripenp, ep) != 0)
16020Sstevel@tonic-gate 		return (-1);
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 	/* set parameters */
16050Sstevel@tonic-gate 	(void) memset(&msp, 0, sizeof (msp));
16060Sstevel@tonic-gate 	MD_SETDRIVERNAME(&msp, MD_STRIPE, sp->setno);
16070Sstevel@tonic-gate 	msp.mnum = meta_getminor(stripenp->dev);
16080Sstevel@tonic-gate 	msp.params = *paramsp;
16090Sstevel@tonic-gate 	if (metaioctl(MD_IOCCHANGE, &msp, &msp.mde, stripenp->cname) != 0)
16100Sstevel@tonic-gate 		return (mdstealerror(ep, &msp.mde));
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	/* clear cache */
16130Sstevel@tonic-gate 	meta_invalidate_name(stripenp);
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	/* return success */
16160Sstevel@tonic-gate 	return (0);
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate /*
16200Sstevel@tonic-gate  * check for dups in the stripe itself
16210Sstevel@tonic-gate  */
16220Sstevel@tonic-gate static int
16230Sstevel@tonic-gate check_twice(
16240Sstevel@tonic-gate 	md_stripe_t	*stripep,
16250Sstevel@tonic-gate 	uint_t		row,
16260Sstevel@tonic-gate 	uint_t		comp,
16270Sstevel@tonic-gate 	md_error_t	*ep
16280Sstevel@tonic-gate )
16290Sstevel@tonic-gate {
16300Sstevel@tonic-gate 	mdname_t	*stripenp = stripep->common.namep;
16310Sstevel@tonic-gate 	mdname_t	*thisnp;
16320Sstevel@tonic-gate 	uint_t		r;
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	thisnp = stripep->rows.rows_val[row].comps.comps_val[comp].compnamep;
16350Sstevel@tonic-gate 	for (r = 0; (r <= row); ++r) {
16360Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[r];
16370Sstevel@tonic-gate 		uint_t		e = ((r == row) ? comp : rp->comps.comps_len);
16380Sstevel@tonic-gate 		uint_t		c;
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 		for (c = 0; (c < e); ++c) {
16410Sstevel@tonic-gate 			md_comp_t	*cp = &rp->comps.comps_val[c];
16420Sstevel@tonic-gate 			mdname_t	*compnp = cp->compnamep;
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 			if (meta_check_overlap(stripenp->cname, thisnp, 0, -1,
16450Sstevel@tonic-gate 			    compnp, 0, -1, ep) != 0) {
16460Sstevel@tonic-gate 				return (-1);
16470Sstevel@tonic-gate 			}
16480Sstevel@tonic-gate 		}
16490Sstevel@tonic-gate 	}
16500Sstevel@tonic-gate 	return (0);
16510Sstevel@tonic-gate }
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate /*
16540Sstevel@tonic-gate  * default stripe interlace
16550Sstevel@tonic-gate  */
16560Sstevel@tonic-gate diskaddr_t
16570Sstevel@tonic-gate meta_default_stripe_interlace(void)
16580Sstevel@tonic-gate {
16590Sstevel@tonic-gate 	diskaddr_t		interlace;
16600Sstevel@tonic-gate 
1661*456Stn143363 	/* default to 512k, round up if necessary */
1662*456Stn143363 	interlace = btodb(512 * 1024);
16630Sstevel@tonic-gate 	if (interlace < btodb(MININTERLACE))
16640Sstevel@tonic-gate 		interlace = roundup(MININTERLACE, interlace);
16650Sstevel@tonic-gate 	return (interlace);
16660Sstevel@tonic-gate }
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate /*
16690Sstevel@tonic-gate  * convert interlaces
16700Sstevel@tonic-gate  */
16710Sstevel@tonic-gate int
16720Sstevel@tonic-gate meta_stripe_check_interlace(
16730Sstevel@tonic-gate 	diskaddr_t	interlace,
16740Sstevel@tonic-gate 	char		*uname,
16750Sstevel@tonic-gate 	md_error_t	*ep
16760Sstevel@tonic-gate )
16770Sstevel@tonic-gate {
16780Sstevel@tonic-gate 	if ((interlace < btodb(MININTERLACE)) ||
16790Sstevel@tonic-gate 		(interlace > btodb(MAXINTERLACE))) {
16800Sstevel@tonic-gate 		return (mderror(ep, MDE_BAD_INTERLACE, uname));
16810Sstevel@tonic-gate 	}
16820Sstevel@tonic-gate 	return (0);
16830Sstevel@tonic-gate }
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate /*
16870Sstevel@tonic-gate  * check stripe
16880Sstevel@tonic-gate  */
16890Sstevel@tonic-gate int
16900Sstevel@tonic-gate meta_check_stripe(
16910Sstevel@tonic-gate 	mdsetname_t	*sp,
16920Sstevel@tonic-gate 	md_stripe_t	*stripep,
16930Sstevel@tonic-gate 	mdcmdopts_t	options,
16940Sstevel@tonic-gate 	md_error_t	*ep
16950Sstevel@tonic-gate )
16960Sstevel@tonic-gate {
16970Sstevel@tonic-gate 	mdname_t	*stripenp = stripep->common.namep;
16980Sstevel@tonic-gate 	int		force = ((options & MDCMD_FORCE) ? 1 : 0);
16990Sstevel@tonic-gate 	int		doit = ((options & MDCMD_DOIT) ? 1 : 0);
17000Sstevel@tonic-gate 	int		updateit = ((options & MDCMD_UPDATE) ? 1 : 0);
17010Sstevel@tonic-gate 	uint_t		row;
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 	/* check rows */
17040Sstevel@tonic-gate 	if (stripep->rows.rows_len < 1) {
17050Sstevel@tonic-gate 		return (mdmderror(ep, MDE_BAD_STRIPE,
17060Sstevel@tonic-gate 		    meta_getminor(stripenp->dev), stripenp->cname));
17070Sstevel@tonic-gate 	}
17080Sstevel@tonic-gate 	for (row = 0; (row < stripep->rows.rows_len); ++row) {
17090Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
17100Sstevel@tonic-gate 		uint_t		comp;
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 		/* check number */
17130Sstevel@tonic-gate 		if (rp->comps.comps_len < 1) {
17140Sstevel@tonic-gate 			return (mdmderror(ep, MDE_BAD_STRIPE,
17150Sstevel@tonic-gate 			    meta_getminor(stripenp->dev), stripenp->cname));
17160Sstevel@tonic-gate 		}
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 		/* compute default interlace */
17190Sstevel@tonic-gate 		if (rp->interlace == 0) {
17200Sstevel@tonic-gate 			rp->interlace = meta_default_stripe_interlace();
17210Sstevel@tonic-gate 		}
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 		/* check interlace */
17240Sstevel@tonic-gate 		if (meta_stripe_check_interlace(rp->interlace, stripenp->cname,
17250Sstevel@tonic-gate 		    ep) != 0) {
17260Sstevel@tonic-gate 			return (-1);
17270Sstevel@tonic-gate 		}
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 		/* check components */
17300Sstevel@tonic-gate 		for (comp = 0; (comp < rp->comps.comps_len); ++comp) {
17310Sstevel@tonic-gate 			md_comp_t	*cp = &rp->comps.comps_val[comp];
17320Sstevel@tonic-gate 			mdname_t	*compnp = cp->compnamep;
17330Sstevel@tonic-gate 			diskaddr_t	start_blk, size;
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 			/* check component */
17360Sstevel@tonic-gate 			if (!updateit) {
17370Sstevel@tonic-gate 				if (meta_check_component(sp, compnp,
17380Sstevel@tonic-gate 					force, ep) != 0)
17390Sstevel@tonic-gate 					return (-1);
17400Sstevel@tonic-gate 				if (((start_blk = metagetstart(sp, compnp,
17410Sstevel@tonic-gate 				    ep)) == MD_DISKADDR_ERROR) ||
17420Sstevel@tonic-gate 				    ((size = metagetsize(compnp, ep)) ==
17430Sstevel@tonic-gate 				    MD_DISKADDR_ERROR)) {
17440Sstevel@tonic-gate 					return (-1);
17450Sstevel@tonic-gate 				}
17460Sstevel@tonic-gate 				if (start_blk >= size)
17470Sstevel@tonic-gate 					return (mdsyserror(ep, ENOSPC,
17480Sstevel@tonic-gate 						compnp->cname));
17490Sstevel@tonic-gate 				size -= start_blk;
17500Sstevel@tonic-gate 				size = rounddown(size, rp->interlace);
17510Sstevel@tonic-gate 				if (size == 0)
17520Sstevel@tonic-gate 					return (mdsyserror(ep, ENOSPC,
17530Sstevel@tonic-gate 						compnp->cname));
17540Sstevel@tonic-gate 			}
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 			/* check this stripe too */
17570Sstevel@tonic-gate 			if (check_twice(stripep, row, comp, ep) != 0)
17580Sstevel@tonic-gate 				return (-1);
17590Sstevel@tonic-gate 		}
17600Sstevel@tonic-gate 	}
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	/* check hotspare pool name */
17630Sstevel@tonic-gate 	if (doit) {
17640Sstevel@tonic-gate 		if ((stripep->hspnamep != NULL) &&
17650Sstevel@tonic-gate 		    (metachkhsp(sp, stripep->hspnamep, ep) != 0)) {
17660Sstevel@tonic-gate 			return (-1);
17670Sstevel@tonic-gate 		}
17680Sstevel@tonic-gate 	}
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 	/* return success */
17710Sstevel@tonic-gate 	return (0);
17720Sstevel@tonic-gate }
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate /*
17750Sstevel@tonic-gate  * setup stripe geometry
17760Sstevel@tonic-gate  */
17770Sstevel@tonic-gate static int
17780Sstevel@tonic-gate stripe_geom(
17790Sstevel@tonic-gate 	md_stripe_t	*stripep,
17800Sstevel@tonic-gate 	ms_unit_t	*ms,
17810Sstevel@tonic-gate 	md_error_t	*ep
17820Sstevel@tonic-gate )
17830Sstevel@tonic-gate {
17840Sstevel@tonic-gate 	uint_t		nrow = stripep->rows.rows_len;
17850Sstevel@tonic-gate 	uint_t		write_reinstruct = 0;
17860Sstevel@tonic-gate 	uint_t		read_reinstruct = 0;
17870Sstevel@tonic-gate 	uint_t		round_cyl = 1;
17880Sstevel@tonic-gate 	uint_t		row;
17890Sstevel@tonic-gate 	mdgeom_t	*geomp;
17900Sstevel@tonic-gate 	diskaddr_t	first_row_size = 0;
17910Sstevel@tonic-gate 	char		*miscname;
17920Sstevel@tonic-gate 	int		is_sp = 0;
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	/* get worst reinstructs */
17950Sstevel@tonic-gate 	for (row = 0; (row < nrow); ++row) {
17960Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
17970Sstevel@tonic-gate 		uint_t		ncomp = rp->comps.comps_len;
17980Sstevel@tonic-gate 		uint_t		comp;
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 		for (comp = 0; (comp < ncomp); ++comp) {
18010Sstevel@tonic-gate 			md_comp_t	*cp = &rp->comps.comps_val[comp];
18020Sstevel@tonic-gate 			mdname_t	*compnp = cp->compnamep;
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 			if ((geomp = metagetgeom(compnp, ep)) == NULL)
18050Sstevel@tonic-gate 				return (-1);
18060Sstevel@tonic-gate 			if (geomp->write_reinstruct > write_reinstruct)
18070Sstevel@tonic-gate 				write_reinstruct = geomp->write_reinstruct;
18080Sstevel@tonic-gate 			if (geomp->read_reinstruct > read_reinstruct)
18090Sstevel@tonic-gate 				read_reinstruct = geomp->read_reinstruct;
18100Sstevel@tonic-gate 		}
18110Sstevel@tonic-gate 	}
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	if ((geomp = metagetgeom(
18140Sstevel@tonic-gate 	    stripep->rows.rows_val[0].comps.comps_val[0].compnamep,
18150Sstevel@tonic-gate 	    ep)) == NULL) {
18160Sstevel@tonic-gate 		return (-1);
18170Sstevel@tonic-gate 	}
18180Sstevel@tonic-gate 	/*
18190Sstevel@tonic-gate 	 * Figure out if the first component is a softpartition as the
18200Sstevel@tonic-gate 	 * truncation check only occurs on them.
18210Sstevel@tonic-gate 	 */
18220Sstevel@tonic-gate 	if ((miscname = metagetmiscname(
18230Sstevel@tonic-gate 	    stripep->rows.rows_val[0].comps.comps_val[0].compnamep,
18240Sstevel@tonic-gate 	    ep)) == NULL) {
18250Sstevel@tonic-gate 		if (!mdisdeverror(ep, MDE_NOT_META))
18260Sstevel@tonic-gate 			return (-1);
18270Sstevel@tonic-gate 	} else if (strcmp(miscname, MD_SP) == 0) {
18280Sstevel@tonic-gate 		is_sp = 1;
18290Sstevel@tonic-gate 	}
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate 	/* setup geometry from first device */
18330Sstevel@tonic-gate 	if (meta_setup_geom((md_unit_t *)ms, stripep->common.namep, geomp,
18340Sstevel@tonic-gate 	    write_reinstruct, read_reinstruct, round_cyl, ep) != 0)
18350Sstevel@tonic-gate 		return (-1);
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 	/*
18380Sstevel@tonic-gate 	 * Here we want to make sure that any truncation did not
18390Sstevel@tonic-gate 	 * result in lost data (or, more appropriately, inaccessible
18400Sstevel@tonic-gate 	 * data).
18410Sstevel@tonic-gate 	 *
18420Sstevel@tonic-gate 	 * This is mainly a danger for (1, 1) concats, but it is
18430Sstevel@tonic-gate 	 * mathematically possible for other somewhat contrived
18440Sstevel@tonic-gate 	 * arrangements where in the sum of the lengths of each row
18450Sstevel@tonic-gate 	 * beyond the first is smaller than the cylinder size of the
18460Sstevel@tonic-gate 	 * only component in the first row.
18470Sstevel@tonic-gate 	 *
18480Sstevel@tonic-gate 	 * It is tempting to simply test for truncation here, by
18490Sstevel@tonic-gate 	 * (md->c.un_total_blocks < md->c.un_actual_tb). That does
18500Sstevel@tonic-gate 	 * not tell us, however, if rounding resulted in data loss,
18510Sstevel@tonic-gate 	 * rather only that it occurred. The somewhat less obvious
18520Sstevel@tonic-gate 	 * test below covers both the obvious (1, 1) case and the
18530Sstevel@tonic-gate 	 * aforementioned corner case.
18540Sstevel@tonic-gate 	 */
18550Sstevel@tonic-gate 	first_row_size = ms->un_row[0].un_blocks;
18560Sstevel@tonic-gate 	if (is_sp == 1) {
18570Sstevel@tonic-gate 		md_unit_t	*md = (md_unit_t *)ms;
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 		if (md->c.un_total_blocks < first_row_size) {
18600Sstevel@tonic-gate 			char buf[] = VAL2STR(ULLONG_MAX);
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 			/*
18630Sstevel@tonic-gate 			 * The only difference here is the text of the error
18640Sstevel@tonic-gate 			 * message, since the remediation is slightly
18650Sstevel@tonic-gate 			 * different in the one-component versus
18660Sstevel@tonic-gate 			 * multiple-component cases.
18670Sstevel@tonic-gate 			 */
18680Sstevel@tonic-gate 			if (nrow == 1) {
18690Sstevel@tonic-gate 				(void) mderror(ep, MDE_STRIPE_TRUNC_SINGLE,
18700Sstevel@tonic-gate 				    stripep->common.namep->cname);
18710Sstevel@tonic-gate 			} else {
18720Sstevel@tonic-gate 				(void) mderror(ep, MDE_STRIPE_TRUNC_MULTIPLE,
18730Sstevel@tonic-gate 				    stripep->common.namep->cname);
18740Sstevel@tonic-gate 			}
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 			/*
18770Sstevel@tonic-gate 			 * By the size comparison above and the initialization
18780Sstevel@tonic-gate 			 * of buf[] in terms of ULLONG_MAX, we guarantee that
18790Sstevel@tonic-gate 			 * the value arg is non-negative and that we won't
18800Sstevel@tonic-gate 			 * overflow the container.
18810Sstevel@tonic-gate 			 */
18820Sstevel@tonic-gate 			mderrorextra(ep, ulltostr((md->c.un_total_blocks +
18830Sstevel@tonic-gate 			    (geomp->nhead * geomp->nsect))
18840Sstevel@tonic-gate 			    - first_row_size, &buf[sizeof (buf) - 1]));
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 			return (-1);
18870Sstevel@tonic-gate 		}
18880Sstevel@tonic-gate 	}
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 	/* return success */
18910Sstevel@tonic-gate 	return (0);
18920Sstevel@tonic-gate }
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate /*
18950Sstevel@tonic-gate  * create stripe
18960Sstevel@tonic-gate  */
18970Sstevel@tonic-gate int
18980Sstevel@tonic-gate meta_create_stripe(
18990Sstevel@tonic-gate 	mdsetname_t	*sp,
19000Sstevel@tonic-gate 	md_stripe_t	*stripep,
19010Sstevel@tonic-gate 	mdcmdopts_t	options,
19020Sstevel@tonic-gate 	md_error_t	*ep
19030Sstevel@tonic-gate )
19040Sstevel@tonic-gate {
19050Sstevel@tonic-gate 	mdname_t	*stripenp = stripep->common.namep;
19060Sstevel@tonic-gate 	int		force = ((options & MDCMD_FORCE) ? 1 : 0);
19070Sstevel@tonic-gate 	int		doall = ((options & MDCMD_ALLOPTION) ? 1 : 0);
19080Sstevel@tonic-gate 	uint_t		nrow = stripep->rows.rows_len;
19090Sstevel@tonic-gate 	uint_t		ncomp = 0;
19100Sstevel@tonic-gate 	uint_t		icomp = 0;
19110Sstevel@tonic-gate 	diskaddr_t	cum_blocks = 0;
19120Sstevel@tonic-gate 	diskaddr_t	limit;
19130Sstevel@tonic-gate 	size_t		mdsize, first_comp;
19140Sstevel@tonic-gate 	uint_t		row;
19150Sstevel@tonic-gate 	ms_unit_t	*ms;
19160Sstevel@tonic-gate 	ms_comp_t	*mdcomp;
19170Sstevel@tonic-gate 	mdnamelist_t	*keynlp = NULL;
19180Sstevel@tonic-gate 	md_set_params_t	set_params;
19190Sstevel@tonic-gate 	int		rval = -1;
19200Sstevel@tonic-gate 	md_timeval32_t	creation_time;
19210Sstevel@tonic-gate 	int		create_flag = MD_CRO_32BIT;
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	/* validate stripe */
19240Sstevel@tonic-gate 	if (meta_check_stripe(sp, stripep, options, ep) != 0)
19250Sstevel@tonic-gate 		return (-1);
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 	/* allocate stripe unit */
19280Sstevel@tonic-gate 	mdsize = sizeof (*ms) - sizeof (ms->un_row[0]);
19290Sstevel@tonic-gate 	mdsize += sizeof (ms->un_row) * nrow;
19300Sstevel@tonic-gate 	for (row = 0; (row < nrow); ++row) {
19310Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 		ncomp += rp->comps.comps_len;
19340Sstevel@tonic-gate 	}
19350Sstevel@tonic-gate 	first_comp = roundup(mdsize, sizeof (long long));
19360Sstevel@tonic-gate 	mdsize += (first_comp - mdsize) + (ncomp * sizeof (ms_comp_t));
19370Sstevel@tonic-gate 	ms = Zalloc(mdsize);
19380Sstevel@tonic-gate 	ms->un_ocomp = first_comp;
19390Sstevel@tonic-gate 	if (meta_gettimeofday(&creation_time) == -1)
19400Sstevel@tonic-gate 		return (mdsyserror(ep, errno, NULL));
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 	/* do rows */
19430Sstevel@tonic-gate 	mdcomp = (ms_comp_t *)(void *)&((char *)ms)[ms->un_ocomp];
19440Sstevel@tonic-gate 	for (row = 0; (row < nrow); ++row) {
19450Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
19460Sstevel@tonic-gate 		uint_t		ncomp = rp->comps.comps_len;
19470Sstevel@tonic-gate 		struct ms_row	*mdr = &ms->un_row[row];
19480Sstevel@tonic-gate 		diskaddr_t	disk_size = 0;
19490Sstevel@tonic-gate 		uint_t		comp;
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate 		/* setup component count and offfset */
19520Sstevel@tonic-gate 		mdr->un_icomp = icomp;
19530Sstevel@tonic-gate 		mdr->un_ncomp = ncomp;
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 		/* do components */
19560Sstevel@tonic-gate 		for (comp = 0; (comp < ncomp); ++comp) {
19570Sstevel@tonic-gate 			md_comp_t	*cp = &rp->comps.comps_val[comp];
19580Sstevel@tonic-gate 			mdname_t	*compnp = cp->compnamep;
19590Sstevel@tonic-gate 			ms_comp_t	*mdc = &mdcomp[icomp++];
19600Sstevel@tonic-gate 			diskaddr_t	size, start_blk;
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 			/*
19630Sstevel@tonic-gate 			 * get start and size
19640Sstevel@tonic-gate 			 * if first component is labelled, include label
19650Sstevel@tonic-gate 			 */
19660Sstevel@tonic-gate 			if ((size = metagetsize(compnp, ep)) ==
19670Sstevel@tonic-gate 			    MD_DISKADDR_ERROR)
19680Sstevel@tonic-gate 				goto out;
19690Sstevel@tonic-gate 			if ((start_blk = metagetstart(sp, compnp, ep)) ==
19700Sstevel@tonic-gate 			    MD_DISKADDR_ERROR)
19710Sstevel@tonic-gate 				goto out;
19720Sstevel@tonic-gate 			if ((row == 0) && (comp == 0)) {
19730Sstevel@tonic-gate 				diskaddr_t	label;
19740Sstevel@tonic-gate 				int		has_db;
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 				if ((has_db = metahasmddb(sp, compnp, ep)) < 0)
19770Sstevel@tonic-gate 					goto out;
19780Sstevel@tonic-gate 				if ((label = metagetlabel(compnp, ep)) ==
19790Sstevel@tonic-gate 				    MD_DISKADDR_ERROR)
19800Sstevel@tonic-gate 					goto out;
19810Sstevel@tonic-gate 				if ((has_db == 0) && (label != 0)) {
19820Sstevel@tonic-gate 					ms->c.un_flag |= MD_LABELED;
19830Sstevel@tonic-gate 					start_blk = compnp->start_blk = 0;
19840Sstevel@tonic-gate 				}
19850Sstevel@tonic-gate 			}
19860Sstevel@tonic-gate 			/* make sure we still have something left */
19870Sstevel@tonic-gate 			if (start_blk >= size) {
19880Sstevel@tonic-gate 				(void) mdsyserror(ep, ENOSPC, compnp->cname);
19890Sstevel@tonic-gate 				goto out;
19900Sstevel@tonic-gate 			}
19910Sstevel@tonic-gate 			size -= start_blk;
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 			/*
19940Sstevel@tonic-gate 			 * round down by interlace: this only applies
19950Sstevel@tonic-gate 			 * if this row is a stripe, as indicated by
19960Sstevel@tonic-gate 			 * (ncomp > 1)
19970Sstevel@tonic-gate 			 */
19980Sstevel@tonic-gate 			if (ncomp > 1)
19990Sstevel@tonic-gate 				size = rounddown(size, rp->interlace);
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 			if (size == 0) {
20020Sstevel@tonic-gate 				(void) mdsyserror(ep, ENOSPC, compnp->cname);
20030Sstevel@tonic-gate 				goto out;
20040Sstevel@tonic-gate 			}
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 			/*
20070Sstevel@tonic-gate 			 * adjust for smallest disk: for a concat (any
20080Sstevel@tonic-gate 			 * row with only one component), this will
20090Sstevel@tonic-gate 			 * never hit the second conditional.
20100Sstevel@tonic-gate 			 */
20110Sstevel@tonic-gate 			if (disk_size == 0) {
20120Sstevel@tonic-gate 				disk_size = size;
20130Sstevel@tonic-gate 			} else if (size < disk_size) {
20140Sstevel@tonic-gate 				disk_size = size;
20150Sstevel@tonic-gate 			}
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 			if (options & MDCMD_DOIT) {
20180Sstevel@tonic-gate 				/* store name in namespace */
20190Sstevel@tonic-gate 				if (add_key_name(sp, compnp, &keynlp, ep) != 0)
20200Sstevel@tonic-gate 					goto out;
20210Sstevel@tonic-gate 			}
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate 			/* setup component */
20240Sstevel@tonic-gate 			mdc->un_key = compnp->key;
20250Sstevel@tonic-gate 			mdc->un_dev = compnp->dev;
20260Sstevel@tonic-gate 			mdc->un_start_block = start_blk;
20270Sstevel@tonic-gate 			mdc->un_mirror.ms_state = CS_OKAY;
20280Sstevel@tonic-gate 			mdc->un_mirror.ms_timestamp = creation_time;
20290Sstevel@tonic-gate 		}
20300Sstevel@tonic-gate 		limit = LLONG_MAX;
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 		/* setup row */
20330Sstevel@tonic-gate 		mdr->un_blocks = mdr->un_ncomp * disk_size;
20340Sstevel@tonic-gate 		cum_blocks += mdr->un_blocks;
20350Sstevel@tonic-gate 		if (cum_blocks > limit) {
20360Sstevel@tonic-gate 			cum_blocks = limit;
20370Sstevel@tonic-gate 			md_eprintf(dgettext(TEXT_DOMAIN,
20380Sstevel@tonic-gate 			    "unit size overflow, limit is %lld blocks\n"),
20390Sstevel@tonic-gate 			    limit);
20400Sstevel@tonic-gate 		}
20410Sstevel@tonic-gate 		mdr->un_cum_blocks = cum_blocks;
20420Sstevel@tonic-gate 		mdr->un_interlace = rp->interlace;
20430Sstevel@tonic-gate 	}
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	/* setup unit */
20460Sstevel@tonic-gate 	ms->c.un_type = MD_DEVICE;
20470Sstevel@tonic-gate 	MD_SID(ms) = meta_getminor(stripenp->dev);
20480Sstevel@tonic-gate 	ms->c.un_actual_tb = cum_blocks;
20490Sstevel@tonic-gate 	ms->c.un_size = mdsize;
20500Sstevel@tonic-gate 	if (stripep->hspnamep != NULL)
20510Sstevel@tonic-gate 		ms->un_hsp_id = stripep->hspnamep->hsp;
20520Sstevel@tonic-gate 	else
20530Sstevel@tonic-gate 		ms->un_hsp_id = MD_HSP_NONE;
20540Sstevel@tonic-gate 	ms->un_nrows = nrow;
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate 	/* fill in the size of the stripe */
20570Sstevel@tonic-gate 	if (options & MDCMD_UPDATE) {
20580Sstevel@tonic-gate 		stripep->common.size = ms->c.un_total_blocks;
20590Sstevel@tonic-gate 		for (row = 0; (row < nrow); ++row) {
20600Sstevel@tonic-gate 			stripep->rows.rows_val[row].row_size =
20610Sstevel@tonic-gate 			    ms->un_row[row].un_blocks;
20620Sstevel@tonic-gate 		}
20630Sstevel@tonic-gate 	}
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 	if (stripe_geom(stripep, ms, ep) != 0) {
20660Sstevel@tonic-gate 		/*
20670Sstevel@tonic-gate 		 * If the device is being truncated then only allow this
20680Sstevel@tonic-gate 		 * if the user is aware (using the -f option) or they
20690Sstevel@tonic-gate 		 * are in a recovery/complete build situation (using the -a
20700Sstevel@tonic-gate 		 * option).
20710Sstevel@tonic-gate 		 */
20720Sstevel@tonic-gate 		if ((mdiserror(ep, MDE_STRIPE_TRUNC_SINGLE) ||
20730Sstevel@tonic-gate 		    mdiserror(ep, MDE_STRIPE_TRUNC_MULTIPLE)) &&
20740Sstevel@tonic-gate 		    (force || doall)) {
20750Sstevel@tonic-gate 			md_eprintf(dgettext(TEXT_DOMAIN,
20760Sstevel@tonic-gate "%s: WARNING: This form of metainit is not recommended.\n"
20770Sstevel@tonic-gate "The stripe is truncating the size of the underlying device.\n"
20780Sstevel@tonic-gate "Please see ERRORS in metainit(1M) for additional information.\n"),
20790Sstevel@tonic-gate 			    stripenp->cname);
20800Sstevel@tonic-gate 			mdclrerror(ep);
20810Sstevel@tonic-gate 		} else {
20820Sstevel@tonic-gate 			goto out;
20830Sstevel@tonic-gate 		}
20840Sstevel@tonic-gate 	}
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	create_flag = meta_check_devicesize(ms->c.un_total_blocks);
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	/* if we're not doing anything, return success */
20890Sstevel@tonic-gate 	if (! (options & MDCMD_DOIT)) {
20900Sstevel@tonic-gate 		rval = 0;	/* success */
20910Sstevel@tonic-gate 		goto out;
20920Sstevel@tonic-gate 	}
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	/* create stripe */
20950Sstevel@tonic-gate 	(void) memset(&set_params, 0, sizeof (set_params));
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 	/* did the user tell us to generate a large device? */
20980Sstevel@tonic-gate 	if (create_flag == MD_CRO_64BIT) {
20990Sstevel@tonic-gate 		ms->c.un_revision = MD_64BIT_META_DEV;
21000Sstevel@tonic-gate 		set_params.options = MD_CRO_64BIT;
21010Sstevel@tonic-gate 	} else {
21020Sstevel@tonic-gate 		ms->c.un_revision = MD_32BIT_META_DEV;
21030Sstevel@tonic-gate 		set_params.options = MD_CRO_32BIT;
21040Sstevel@tonic-gate 	}
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	set_params.mnum = MD_SID(ms);
21070Sstevel@tonic-gate 	set_params.size = ms->c.un_size;
21080Sstevel@tonic-gate 	set_params.mdp = (uintptr_t)ms;
21090Sstevel@tonic-gate 	MD_SETDRIVERNAME(&set_params, MD_STRIPE, MD_MIN2SET(set_params.mnum));
21100Sstevel@tonic-gate 	if (metaioctl(MD_IOCSET, &set_params, &set_params.mde,
21110Sstevel@tonic-gate 	    stripenp->cname) != 0) {
21120Sstevel@tonic-gate 		(void) mdstealerror(ep, &set_params.mde);
21130Sstevel@tonic-gate 		goto out;
21140Sstevel@tonic-gate 	}
21150Sstevel@tonic-gate 	rval = 0;	/* success */
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	/* cleanup, return success */
21180Sstevel@tonic-gate out:
21190Sstevel@tonic-gate 	Free(ms);
21200Sstevel@tonic-gate 	if (rval != 0) {
21210Sstevel@tonic-gate 		(void) del_key_names(sp, keynlp, NULL);
21220Sstevel@tonic-gate 	}
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 	metafreenamelist(keynlp);
21250Sstevel@tonic-gate 	if ((rval == 0) && (options & MDCMD_DOIT)) {
21260Sstevel@tonic-gate 		if (invalidate_components(sp, stripenp, ep) != 0)
21270Sstevel@tonic-gate 			rval = -1;
21280Sstevel@tonic-gate 		meta_invalidate_name(stripenp);
21290Sstevel@tonic-gate 	}
21300Sstevel@tonic-gate 	return (rval);
21310Sstevel@tonic-gate }
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate /*
21340Sstevel@tonic-gate  * initialize stripe
21350Sstevel@tonic-gate  * NOTE: this functions is metainit(1m)'s command line parser!
21360Sstevel@tonic-gate  */
21370Sstevel@tonic-gate int
21380Sstevel@tonic-gate meta_init_stripe(
21390Sstevel@tonic-gate 	mdsetname_t	**spp,
21400Sstevel@tonic-gate 	int		argc,
21410Sstevel@tonic-gate 	char		*argv[],
21420Sstevel@tonic-gate 	mdcmdopts_t	options,
21430Sstevel@tonic-gate 	md_error_t	*ep
21440Sstevel@tonic-gate )
21450Sstevel@tonic-gate {
21460Sstevel@tonic-gate 	char		*uname = argv[0];
21470Sstevel@tonic-gate 	mdname_t	*stripenp = NULL;
21480Sstevel@tonic-gate 	int		old_optind;
21490Sstevel@tonic-gate 	int		c;
21500Sstevel@tonic-gate 	md_stripe_t	*stripep = NULL;
21510Sstevel@tonic-gate 	uint_t		nrow, row;
21520Sstevel@tonic-gate 	int		rval = -1;
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 	/* get stripe name */
21550Sstevel@tonic-gate 	assert(argc > 0);
21560Sstevel@tonic-gate 	if (argc < 1)
21570Sstevel@tonic-gate 		goto syntax;
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 	if ((stripenp = metaname(spp, uname, ep)) == NULL)
21600Sstevel@tonic-gate 		goto out;
21610Sstevel@tonic-gate 	assert(*spp != NULL);
21620Sstevel@tonic-gate 	uname = stripenp->cname;
21630Sstevel@tonic-gate 	if (metachkmeta(stripenp, ep) != 0)
21640Sstevel@tonic-gate 		goto out;
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate 	if (!(options & MDCMD_NOLOCK)) {
21670Sstevel@tonic-gate 		/* grab set lock */
21680Sstevel@tonic-gate 		if (meta_lock(*spp, TRUE, ep))
21690Sstevel@tonic-gate 			goto out;
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 		if (meta_check_ownership(*spp, ep) != 0)
21720Sstevel@tonic-gate 			goto out;
21730Sstevel@tonic-gate 	}
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate 	/* see if it exists already */
21760Sstevel@tonic-gate 	if (metagetmiscname(stripenp, ep) != NULL) {
21770Sstevel@tonic-gate 		(void) mdmderror(ep, MDE_UNIT_ALREADY_SETUP,
21780Sstevel@tonic-gate 		    meta_getminor(stripenp->dev), uname);
21790Sstevel@tonic-gate 		goto out;
21800Sstevel@tonic-gate 	} else if (! mdismderror(ep, MDE_UNIT_NOT_SETUP)) {
21810Sstevel@tonic-gate 		goto out;
21820Sstevel@tonic-gate 	} else {
21830Sstevel@tonic-gate 		mdclrerror(ep);
21840Sstevel@tonic-gate 	}
21850Sstevel@tonic-gate 	--argc, ++argv;
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 	/* parse general options */
21880Sstevel@tonic-gate 	optind = 0;
21890Sstevel@tonic-gate 	opterr = 0;
21900Sstevel@tonic-gate 	if (getopt(argc, argv, "") != -1)
21910Sstevel@tonic-gate 		goto options;
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 	/* allocate stripe */
21940Sstevel@tonic-gate 	stripep = Zalloc(sizeof (*stripep));
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	/* setup common */
21970Sstevel@tonic-gate 	stripep->common.namep = stripenp;
21980Sstevel@tonic-gate 	stripep->common.type = MD_DEVICE;
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 	/* allocate and parse rows */
22010Sstevel@tonic-gate 	if (argc < 1) {
22020Sstevel@tonic-gate 		(void) mdmderror(ep, MDE_NROWS, meta_getminor(stripenp->dev),
22030Sstevel@tonic-gate 				uname);
22040Sstevel@tonic-gate 		goto out;
22050Sstevel@tonic-gate 	} else if ((sscanf(argv[0], "%u", &nrow) != 1) || ((int)nrow < 0)) {
22060Sstevel@tonic-gate 		goto syntax;
22070Sstevel@tonic-gate 	} else if (nrow < 1) {
22080Sstevel@tonic-gate 		(void) mdmderror(ep, MDE_NROWS, meta_getminor(stripenp->dev),
22090Sstevel@tonic-gate 				uname);
22100Sstevel@tonic-gate 		goto out;
22110Sstevel@tonic-gate 	}
22120Sstevel@tonic-gate 	--argc, ++argv;
22130Sstevel@tonic-gate 	stripep->rows.rows_len = nrow;
22140Sstevel@tonic-gate 	stripep->rows.rows_val =
22150Sstevel@tonic-gate 	    Zalloc(nrow * sizeof (*stripep->rows.rows_val));
22160Sstevel@tonic-gate 	for (row = 0; (row < nrow); ++row) {
22170Sstevel@tonic-gate 		md_row_t	*mdr = &stripep->rows.rows_val[row];
22180Sstevel@tonic-gate 		uint_t		ncomp, comp;
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 		/* allocate and parse components */
22210Sstevel@tonic-gate 		if (argc < 1) {
22220Sstevel@tonic-gate 			(void) mdmderror(ep, MDE_NROWS,
22230Sstevel@tonic-gate 					meta_getminor(stripenp->dev), uname);
22240Sstevel@tonic-gate 			goto out;
22250Sstevel@tonic-gate 		} else if ((sscanf(argv[0], "%u", &ncomp) != 1) ||
22260Sstevel@tonic-gate 		    ((int)ncomp < 0)) {
22270Sstevel@tonic-gate 			goto syntax;
22280Sstevel@tonic-gate 		} else if (ncomp < 1) {
22290Sstevel@tonic-gate 			(void) mdmderror(ep, MDE_NCOMPS,
22300Sstevel@tonic-gate 					meta_getminor(stripenp->dev), uname);
22310Sstevel@tonic-gate 			goto out;
22320Sstevel@tonic-gate 		}
22330Sstevel@tonic-gate 		--argc, ++argv;
22340Sstevel@tonic-gate 		mdr->comps.comps_len = ncomp;
22350Sstevel@tonic-gate 		mdr->comps.comps_val =
22360Sstevel@tonic-gate 		    Zalloc(ncomp * sizeof (*mdr->comps.comps_val));
22370Sstevel@tonic-gate 		for (comp = 0; (comp < ncomp); ++comp) {
22380Sstevel@tonic-gate 			md_comp_t	*mdc = &mdr->comps.comps_val[comp];
22390Sstevel@tonic-gate 			mdname_t	*compnp;
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate 			/* parse component name */
22420Sstevel@tonic-gate 			if (argc < 1) {
22430Sstevel@tonic-gate 				(void) mdmderror(ep, MDE_NCOMPS,
22440Sstevel@tonic-gate 				    meta_getminor(stripenp->dev), uname);
22450Sstevel@tonic-gate 				goto out;
22460Sstevel@tonic-gate 			}
22470Sstevel@tonic-gate 			if ((compnp = metaname(spp, argv[0], ep)) == NULL) {
22480Sstevel@tonic-gate 				goto out;
22490Sstevel@tonic-gate 			}
22500Sstevel@tonic-gate 			/* check for soft partition */
22510Sstevel@tonic-gate 			if (meta_sp_issp(*spp, compnp, ep) != 0) {
22520Sstevel@tonic-gate 				/* check disk */
22530Sstevel@tonic-gate 				if (metachkcomp(compnp, ep) != 0) {
22540Sstevel@tonic-gate 					goto out;
22550Sstevel@tonic-gate 				}
22560Sstevel@tonic-gate 			}
22570Sstevel@tonic-gate 			mdc->compnamep = compnp;
22580Sstevel@tonic-gate 			--argc, ++argv;
22590Sstevel@tonic-gate 		}
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 		/* parse row options */
22620Sstevel@tonic-gate 		old_optind = optind = 0;
22630Sstevel@tonic-gate 		opterr = 0;
22640Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "i:")) != -1) {
22650Sstevel@tonic-gate 			switch (c) {
22660Sstevel@tonic-gate 			case 'i':
22670Sstevel@tonic-gate 				if (parse_interlace(uname, optarg,
22680Sstevel@tonic-gate 				    &mdr->interlace, ep) != 0) {
22690Sstevel@tonic-gate 					goto out;
22700Sstevel@tonic-gate 				}
22710Sstevel@tonic-gate 				if (meta_stripe_check_interlace(mdr->interlace,
22720Sstevel@tonic-gate 					uname, ep))
22730Sstevel@tonic-gate 					goto out;
22740Sstevel@tonic-gate 				break;
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 			default:
22770Sstevel@tonic-gate 				optind = old_optind;	/* bomb out later */
22780Sstevel@tonic-gate 				goto done_row_opts;
22790Sstevel@tonic-gate 			}
22800Sstevel@tonic-gate 			old_optind = optind;
22810Sstevel@tonic-gate 		}
22820Sstevel@tonic-gate done_row_opts:
22830Sstevel@tonic-gate 		argc -= optind;
22840Sstevel@tonic-gate 		argv += optind;
22850Sstevel@tonic-gate 	}
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 	/* parse stripe options */
22880Sstevel@tonic-gate 	old_optind = optind = 0;
22890Sstevel@tonic-gate 	opterr = 0;
22900Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "h:")) != -1) {
22910Sstevel@tonic-gate 		switch (c) {
22920Sstevel@tonic-gate 		case 'h':
22930Sstevel@tonic-gate 			if ((stripep->hspnamep = metahspname(spp, optarg,
22940Sstevel@tonic-gate 			    ep)) == NULL) {
22950Sstevel@tonic-gate 				goto out;
22960Sstevel@tonic-gate 			}
22970Sstevel@tonic-gate 			break;
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate 		default:
23000Sstevel@tonic-gate 			argc += old_optind;
23010Sstevel@tonic-gate 			argv += old_optind;
23020Sstevel@tonic-gate 			goto options;
23030Sstevel@tonic-gate 		}
23040Sstevel@tonic-gate 		old_optind = optind;
23050Sstevel@tonic-gate 	}
23060Sstevel@tonic-gate 	argc -= optind;
23070Sstevel@tonic-gate 	argv += optind;
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate 	/* we should be at the end */
23100Sstevel@tonic-gate 	if (argc != 0)
23110Sstevel@tonic-gate 		goto syntax;
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 	/* create stripe */
23140Sstevel@tonic-gate 	if (meta_create_stripe(*spp, stripep, options, ep) != 0)
23150Sstevel@tonic-gate 		goto out;
23160Sstevel@tonic-gate 	rval = 0;	/* success */
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 	/* let em know */
23190Sstevel@tonic-gate 	if (options & MDCMD_PRINT) {
23200Sstevel@tonic-gate 		(void) printf(dgettext(TEXT_DOMAIN,
23210Sstevel@tonic-gate 		    "%s: Concat/Stripe is setup\n"),
23220Sstevel@tonic-gate 		    uname);
23230Sstevel@tonic-gate 		(void) fflush(stdout);
23240Sstevel@tonic-gate 	}
23250Sstevel@tonic-gate 	goto out;
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 	/* syntax error */
23280Sstevel@tonic-gate syntax:
23290Sstevel@tonic-gate 	rval = meta_cook_syntax(ep, MDE_SYNTAX, uname, argc, argv);
23300Sstevel@tonic-gate 	goto out;
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate 	/* options error */
23330Sstevel@tonic-gate options:
23340Sstevel@tonic-gate 	rval = meta_cook_syntax(ep, MDE_OPTION, uname, argc, argv);
23350Sstevel@tonic-gate 	goto out;
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate 	/* cleanup, return error */
23380Sstevel@tonic-gate out:
23390Sstevel@tonic-gate 	if (stripep != NULL)
23400Sstevel@tonic-gate 		meta_free_stripe(stripep);
23410Sstevel@tonic-gate 	return (rval);
23420Sstevel@tonic-gate }
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate /*
23450Sstevel@tonic-gate  * reset stripes
23460Sstevel@tonic-gate  */
23470Sstevel@tonic-gate int
23480Sstevel@tonic-gate meta_stripe_reset(
23490Sstevel@tonic-gate 	mdsetname_t	*sp,
23500Sstevel@tonic-gate 	mdname_t	*stripenp,
23510Sstevel@tonic-gate 	mdcmdopts_t	options,
23520Sstevel@tonic-gate 	md_error_t	*ep
23530Sstevel@tonic-gate )
23540Sstevel@tonic-gate {
23550Sstevel@tonic-gate 	md_stripe_t	*stripep;
23560Sstevel@tonic-gate 	int		rval = -1;
23570Sstevel@tonic-gate 	int		row, comp;
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	/* should have same set */
23600Sstevel@tonic-gate 	assert(sp != NULL);
23610Sstevel@tonic-gate 	assert((stripenp == NULL) ||
23620Sstevel@tonic-gate 	    (sp->setno == MD_MIN2SET(meta_getminor(stripenp->dev))));
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 	/* reset all stripes */
23650Sstevel@tonic-gate 	if (stripenp == NULL) {
23660Sstevel@tonic-gate 		mdnamelist_t	*stripenlp = NULL;
23670Sstevel@tonic-gate 		mdnamelist_t	*p;
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 		/* for each stripe */
23700Sstevel@tonic-gate 		rval = 0;
23710Sstevel@tonic-gate 		if (meta_get_stripe_names(sp, &stripenlp, 0, ep) < 0)
23720Sstevel@tonic-gate 			return (-1);
23730Sstevel@tonic-gate 		for (p = stripenlp; (p != NULL); p = p->next) {
23740Sstevel@tonic-gate 			/* reset stripe */
23750Sstevel@tonic-gate 			stripenp = p->namep;
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate 			/*
23780Sstevel@tonic-gate 			 * If this is a multi-node set, we send a series
23790Sstevel@tonic-gate 			 * of individual metaclear commands.
23800Sstevel@tonic-gate 			 */
23810Sstevel@tonic-gate 			if (meta_is_mn_set(sp, ep)) {
23820Sstevel@tonic-gate 				if (meta_mn_send_metaclear_command(sp,
23830Sstevel@tonic-gate 				    stripenp->cname, options, 0, ep) != 0) {
23840Sstevel@tonic-gate 					rval = -1;
23850Sstevel@tonic-gate 					break;
23860Sstevel@tonic-gate 				}
23870Sstevel@tonic-gate 			} else {
23880Sstevel@tonic-gate 				if (meta_stripe_reset(sp, stripenp,
23890Sstevel@tonic-gate 				    options, ep) != 0) {
23900Sstevel@tonic-gate 					rval = -1;
23910Sstevel@tonic-gate 					break;
23920Sstevel@tonic-gate 				}
23930Sstevel@tonic-gate 			}
23940Sstevel@tonic-gate 		}
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate 		/* cleanup, return success */
23970Sstevel@tonic-gate 		metafreenamelist(stripenlp);
23980Sstevel@tonic-gate 		return (rval);
23990Sstevel@tonic-gate 	}
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate 	/* check name */
24020Sstevel@tonic-gate 	if (metachkmeta(stripenp, ep) != 0)
24030Sstevel@tonic-gate 		return (-1);
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	/* get unit structure */
24060Sstevel@tonic-gate 	if ((stripep = meta_get_stripe(sp, stripenp, ep)) == NULL)
24070Sstevel@tonic-gate 		return (-1);
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 	/* make sure nobody owns us */
24100Sstevel@tonic-gate 	if (MD_HAS_PARENT(stripep->common.parent)) {
24110Sstevel@tonic-gate 		return (mdmderror(ep, MDE_IN_USE, meta_getminor(stripenp->dev),
24120Sstevel@tonic-gate 		    stripenp->cname));
24130Sstevel@tonic-gate 	}
24140Sstevel@tonic-gate 
24150Sstevel@tonic-gate 	/* clear subdevices cache */
24160Sstevel@tonic-gate 	if (invalidate_components(sp, stripenp, ep) != 0)
24170Sstevel@tonic-gate 		return (-1);
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 	/* clear metadevice */
24200Sstevel@tonic-gate 	if (meta_reset(sp, stripenp, options, ep) != 0)
24210Sstevel@tonic-gate 		goto out;
24220Sstevel@tonic-gate 	rval = 0;	/* success */
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	/* let em know */
24250Sstevel@tonic-gate 	if (options & MDCMD_PRINT) {
24260Sstevel@tonic-gate 		(void) printf(dgettext(TEXT_DOMAIN,
24270Sstevel@tonic-gate 		    "%s: Concat/Stripe is cleared\n"),
24280Sstevel@tonic-gate 		    stripenp->cname);
24290Sstevel@tonic-gate 		(void) fflush(stdout);
24300Sstevel@tonic-gate 	}
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	/* clear subdevices */
24330Sstevel@tonic-gate 	if (! (options & MDCMD_RECURSE))
24340Sstevel@tonic-gate 		goto out;
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 	for (row = 0; (row < stripep->rows.rows_len); ++row) {
24370Sstevel@tonic-gate 		md_row_t	*rp = &stripep->rows.rows_val[row];
24380Sstevel@tonic-gate 		for (comp = 0; (comp < rp->comps.comps_len); ++comp) {
24390Sstevel@tonic-gate 			md_comp_t	*cp = &rp->comps.comps_val[comp];
24400Sstevel@tonic-gate 			mdname_t	*compnp = cp->compnamep;
24410Sstevel@tonic-gate 
24420Sstevel@tonic-gate 			/* only recurse on metadevices */
24430Sstevel@tonic-gate 			if (! metaismeta(compnp))
24440Sstevel@tonic-gate 				continue;
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate 			if (meta_reset_by_name(sp, compnp, options, ep) != 0)
24470Sstevel@tonic-gate 				rval = -1;
24480Sstevel@tonic-gate 		}
24490Sstevel@tonic-gate 	}
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	/* cleanup, return success */
24520Sstevel@tonic-gate out:
24530Sstevel@tonic-gate 	meta_invalidate_name(stripenp);
24540Sstevel@tonic-gate 	return (rval);
24550Sstevel@tonic-gate }
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate /*
24580Sstevel@tonic-gate  * reports TRUE if any stripe component is in error
24590Sstevel@tonic-gate  */
24600Sstevel@tonic-gate int
24610Sstevel@tonic-gate meta_stripe_anycomp_is_err(mdsetname_t *sp, mdnamelist_t *stripe_names)
24620Sstevel@tonic-gate {
24630Sstevel@tonic-gate 	mdnamelist_t	*nlp;
24640Sstevel@tonic-gate 	md_error_t	  status	= mdnullerror;
24650Sstevel@tonic-gate 	md_error_t	 *ep		= &status;
24660Sstevel@tonic-gate 	int		  any_errs	= FALSE;
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 	for (nlp = stripe_names; nlp; nlp = nlp->next) {
24690Sstevel@tonic-gate 		md_stripe_t	*stripep;
24700Sstevel@tonic-gate 		int		 row;
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 		if ((stripep = meta_get_stripe(sp, nlp->namep, ep)) == NULL) {
24730Sstevel@tonic-gate 			any_errs |= TRUE;
24740Sstevel@tonic-gate 			goto out;
24750Sstevel@tonic-gate 		}
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 		for (row = 0; row < stripep->rows.rows_len; ++row) {
24780Sstevel@tonic-gate 			md_row_t	*rp	= &stripep->rows.rows_val[row];
24790Sstevel@tonic-gate 			uint_t		 comp;
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 			for (comp = 0; comp < rp->comps.comps_len; ++comp) {
24820Sstevel@tonic-gate 				md_comp_t *cp	= &rp->comps.comps_val[comp];
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate 				if (cp->state != CS_OKAY) {
24850Sstevel@tonic-gate 					any_errs |= TRUE;
24860Sstevel@tonic-gate 					goto out;
24870Sstevel@tonic-gate 				}
24880Sstevel@tonic-gate 			}
24890Sstevel@tonic-gate 		}
24900Sstevel@tonic-gate 	}
24910Sstevel@tonic-gate out:
24920Sstevel@tonic-gate 	if (!mdisok(ep))
24930Sstevel@tonic-gate 		mdclrerror(ep);
24940Sstevel@tonic-gate 
24950Sstevel@tonic-gate 	return (any_errs);
24960Sstevel@tonic-gate }
2497