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