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