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