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
51366Spetede * Common Development and Distribution License (the "License").
61366Spetede * 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 */
217627SChris.Horne@Sun.COM
220Sstevel@tonic-gate /*
23*12215SJames.Hall@Sun.COM * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/systm.h>
280Sstevel@tonic-gate #include <sys/conf.h>
290Sstevel@tonic-gate #include <sys/file.h>
300Sstevel@tonic-gate #include <sys/user.h>
310Sstevel@tonic-gate #include <sys/uio.h>
320Sstevel@tonic-gate #include <sys/t_lock.h>
330Sstevel@tonic-gate #include <sys/buf.h>
340Sstevel@tonic-gate #include <sys/dkio.h>
350Sstevel@tonic-gate #include <sys/vtoc.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <vm/page.h>
380Sstevel@tonic-gate #include <sys/cmn_err.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/mkdev.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <sys/open.h>
440Sstevel@tonic-gate #include <sys/lvm/mdio.h>
450Sstevel@tonic-gate #include <sys/lvm/mdvar.h>
460Sstevel@tonic-gate #include <sys/lvm/md_stripe.h>
470Sstevel@tonic-gate #include <sys/lvm/md_convert.h>
480Sstevel@tonic-gate #include <sys/lvm/md_notify.h>
490Sstevel@tonic-gate #include <sys/modctl.h>
500Sstevel@tonic-gate #include <sys/ddi.h>
510Sstevel@tonic-gate #include <sys/sunddi.h>
520Sstevel@tonic-gate #include <sys/debug.h>
530Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
540Sstevel@tonic-gate #include <sys/sysevent/svm.h>
550Sstevel@tonic-gate
560Sstevel@tonic-gate md_ops_t stripe_md_ops;
570Sstevel@tonic-gate #ifndef lint
581366Spetede char _depends_on[] = "drv/md";
591651Spetede md_ops_t *md_interface_ops = &stripe_md_ops;
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate
620Sstevel@tonic-gate extern unit_t md_nunits;
630Sstevel@tonic-gate extern set_t md_nsets;
640Sstevel@tonic-gate extern md_set_t md_set[];
650Sstevel@tonic-gate
660Sstevel@tonic-gate extern kmutex_t md_mx;
670Sstevel@tonic-gate extern kcondvar_t md_cv;
680Sstevel@tonic-gate
690Sstevel@tonic-gate extern int md_status;
700Sstevel@tonic-gate extern major_t md_major;
710Sstevel@tonic-gate extern mdq_anchor_t md_done_daemon;
720Sstevel@tonic-gate
730Sstevel@tonic-gate static int md_stripe_mcs_buf_off;
740Sstevel@tonic-gate static kmem_cache_t *stripe_parent_cache = NULL;
750Sstevel@tonic-gate static kmem_cache_t *stripe_child_cache = NULL;
760Sstevel@tonic-gate
770Sstevel@tonic-gate /*ARGSUSED1*/
780Sstevel@tonic-gate static int
stripe_parent_constructor(void * p,void * d1,int d2)790Sstevel@tonic-gate stripe_parent_constructor(void *p, void *d1, int d2)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate mutex_init(&((md_sps_t *)p)->ps_mx,
820Sstevel@tonic-gate NULL, MUTEX_DEFAULT, NULL);
830Sstevel@tonic-gate return (0);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate static void
stripe_parent_init(void * ps)870Sstevel@tonic-gate stripe_parent_init(void *ps)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate bzero(ps, offsetof(md_sps_t, ps_mx));
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
920Sstevel@tonic-gate /*ARGSUSED1*/
930Sstevel@tonic-gate static void
stripe_parent_destructor(void * p,void * d)940Sstevel@tonic-gate stripe_parent_destructor(void *p, void *d)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate mutex_destroy(&((md_sps_t *)p)->ps_mx);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate
990Sstevel@tonic-gate /*ARGSUSED1*/
1000Sstevel@tonic-gate static int
stripe_child_constructor(void * p,void * d1,int d2)1010Sstevel@tonic-gate stripe_child_constructor(void *p, void *d1, int d2)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate bioinit(&((md_scs_t *)p)->cs_buf);
1040Sstevel@tonic-gate return (0);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate static void
stripe_child_init(md_scs_t * cs)1080Sstevel@tonic-gate stripe_child_init(md_scs_t *cs)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate cs->cs_mdunit = 0;
1110Sstevel@tonic-gate cs->cs_ps = NULL;
1120Sstevel@tonic-gate cs->cs_comp = NULL;
1130Sstevel@tonic-gate md_bioreset(&cs->cs_buf);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /*ARGSUSED1*/
1170Sstevel@tonic-gate static void
stripe_child_destructor(void * p,void * d)1180Sstevel@tonic-gate stripe_child_destructor(void *p, void *d)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate biofini(&((md_scs_t *)p)->cs_buf);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate /*ARGSUSED*/
1240Sstevel@tonic-gate static void
stripe_run_queue(void * d)1250Sstevel@tonic-gate stripe_run_queue(void *d)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate if (!(md_status & MD_GBL_DAEMONS_LIVE))
1280Sstevel@tonic-gate md_daemon(1, &md_done_daemon);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate static void
stripe_close_all_devs(ms_unit_t * un,int md_cflags)1320Sstevel@tonic-gate stripe_close_all_devs(ms_unit_t *un, int md_cflags)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate int row;
1350Sstevel@tonic-gate int i;
1360Sstevel@tonic-gate int c;
1370Sstevel@tonic-gate struct ms_comp *mdcomp;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate mdcomp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
1400Sstevel@tonic-gate for (row = 0; row < un->un_nrows; row++) {
1410Sstevel@tonic-gate struct ms_row *mdr = &un->un_row[row];
1420Sstevel@tonic-gate for (i = 0, c = mdr->un_icomp; i < mdr->un_ncomp; i++) {
1430Sstevel@tonic-gate struct ms_comp *mdc;
1440Sstevel@tonic-gate mdc = &mdcomp[c++];
1450Sstevel@tonic-gate if (md_cflags & MD_OFLG_PROBEDEV) {
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate * It is possible that the md_layered_open
1490Sstevel@tonic-gate * failed because the stripe unit structure
1500Sstevel@tonic-gate * contained a NODEV. In such a case since
1510Sstevel@tonic-gate * there is nothing to open, there is nothing
1520Sstevel@tonic-gate * to close.
1530Sstevel@tonic-gate */
1540Sstevel@tonic-gate if (mdc->un_dev == NODEV64)
1550Sstevel@tonic-gate continue;
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate if ((md_cflags & MD_OFLG_PROBEDEV) &&
1580Sstevel@tonic-gate (mdc->un_mirror.ms_flags & MDM_S_PROBEOPEN)) {
1590Sstevel@tonic-gate md_layered_close(mdc->un_dev,
1600Sstevel@tonic-gate md_cflags);
1617627SChris.Horne@Sun.COM mdc->un_mirror.ms_flags &= ~MDM_S_PROBEOPEN;
1620Sstevel@tonic-gate } else if (mdc->un_mirror.ms_flags & MDM_S_ISOPEN) {
1630Sstevel@tonic-gate md_layered_close(mdc->un_dev, md_cflags);
1640Sstevel@tonic-gate mdc->un_mirror.ms_flags &= ~MDM_S_ISOPEN;
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate static int
stripe_open_all_devs(ms_unit_t * un,int md_oflags)1710Sstevel@tonic-gate stripe_open_all_devs(ms_unit_t *un, int md_oflags)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate minor_t mnum = MD_SID(un);
1740Sstevel@tonic-gate int row;
1750Sstevel@tonic-gate int i;
1760Sstevel@tonic-gate int c;
1770Sstevel@tonic-gate struct ms_comp *mdcomp;
1780Sstevel@tonic-gate int err;
1790Sstevel@tonic-gate int cont_on_errors = (md_oflags & MD_OFLG_CONT_ERRS);
1800Sstevel@tonic-gate int probe_err_cnt = 0;
1810Sstevel@tonic-gate int total_comp_cnt = 0;
1820Sstevel@tonic-gate set_t setno = MD_MIN2SET(MD_SID(un));
1830Sstevel@tonic-gate side_t side = mddb_getsidenum(setno);
1840Sstevel@tonic-gate mdkey_t key;
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate mdcomp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate * For a probe call, if any component of a stripe or a concat
1900Sstevel@tonic-gate * can be opened, it is considered to be a success. The total number
1910Sstevel@tonic-gate * of components in a stripe are computed prior to starting a probe.
1920Sstevel@tonic-gate * This number is then compared against the number of components
1930Sstevel@tonic-gate * that could be be successfully opened. If none of the components
1940Sstevel@tonic-gate * in a stripe can be opened, only then an ENXIO is returned for a
1950Sstevel@tonic-gate * probe type open.
1960Sstevel@tonic-gate */
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate for (row = 0; row < un->un_nrows; row++) {
1990Sstevel@tonic-gate struct ms_row *mdr = &un->un_row[row];
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if (md_oflags & MD_OFLG_PROBEDEV)
2020Sstevel@tonic-gate total_comp_cnt += mdr->un_ncomp;
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate for (i = 0, c = mdr->un_icomp; i < mdr->un_ncomp; i++) {
2050Sstevel@tonic-gate struct ms_comp *mdc;
2060Sstevel@tonic-gate md_dev64_t tmpdev;
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate mdc = &mdcomp[c++];
2090Sstevel@tonic-gate tmpdev = mdc->un_dev;
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate * Do the open by device id
2120Sstevel@tonic-gate * Check if this comp is hotspared and
2130Sstevel@tonic-gate * if it is then use the key for hotspare.
2140Sstevel@tonic-gate * MN disksets don't use devids, so we better don't use
2150Sstevel@tonic-gate * md_devid_found/md_resolve_bydevid there. Rather do,
2160Sstevel@tonic-gate * what's done in stripe_build_incore()
2170Sstevel@tonic-gate */
2180Sstevel@tonic-gate if (MD_MNSET_SETNO(setno)) {
2190Sstevel@tonic-gate if (mdc->un_mirror.ms_hs_id != 0) {
2200Sstevel@tonic-gate (void) md_hot_spare_ifc(HS_MKDEV, 0, 0,
2210Sstevel@tonic-gate 0, &mdc->un_mirror.ms_hs_id, NULL,
2220Sstevel@tonic-gate &tmpdev, NULL);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate } else {
2250Sstevel@tonic-gate key = mdc->un_mirror.ms_hs_id ?
2260Sstevel@tonic-gate mdc->un_mirror.ms_hs_key : mdc->un_key;
2270Sstevel@tonic-gate if ((md_getmajor(tmpdev) != md_major) &&
2280Sstevel@tonic-gate md_devid_found(setno, side, key) == 1) {
2290Sstevel@tonic-gate tmpdev = md_resolve_bydevid(mnum,
2300Sstevel@tonic-gate tmpdev, key);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate * For a submirror, we only want to open those devices
2360Sstevel@tonic-gate * that are not errored. If the device is errored then
2370Sstevel@tonic-gate * then there is no reason to open it and leaving it
2380Sstevel@tonic-gate * closed allows the RCM/DR code to work so that the
2390Sstevel@tonic-gate * errored device can be replaced.
2400Sstevel@tonic-gate */
2410Sstevel@tonic-gate if ((md_oflags & MD_OFLG_PROBEDEV) ||
2420Sstevel@tonic-gate ! (mdc->un_mirror.ms_state & CS_ERRED)) {
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate err = md_layered_open(mnum, &tmpdev, md_oflags);
2450Sstevel@tonic-gate } else {
2460Sstevel@tonic-gate err = ENXIO;
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate * Only set the un_dev if the tmpdev != NODEV64. If
2510Sstevel@tonic-gate * it is NODEV64 then the md_layered_open() will have
2520Sstevel@tonic-gate * failed in some manner.
2530Sstevel@tonic-gate */
2540Sstevel@tonic-gate if (tmpdev != NODEV64)
2550Sstevel@tonic-gate mdc->un_dev = tmpdev;
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate if (err) {
2580Sstevel@tonic-gate if (!cont_on_errors) {
2590Sstevel@tonic-gate stripe_close_all_devs(un, md_oflags);
2600Sstevel@tonic-gate return (ENXIO);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate if (md_oflags & MD_OFLG_PROBEDEV)
2640Sstevel@tonic-gate probe_err_cnt++;
2650Sstevel@tonic-gate } else {
2660Sstevel@tonic-gate if (md_oflags & MD_OFLG_PROBEDEV) {
2670Sstevel@tonic-gate mdc->un_mirror.ms_flags |=
2687627SChris.Horne@Sun.COM MDM_S_PROBEOPEN;
2690Sstevel@tonic-gate } else
2700Sstevel@tonic-gate mdc->un_mirror.ms_flags |= MDM_S_ISOPEN;
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate /* If every component in a stripe could not be opened fail */
2760Sstevel@tonic-gate if ((md_oflags & MD_OFLG_PROBEDEV) &&
2770Sstevel@tonic-gate (probe_err_cnt == total_comp_cnt))
2780Sstevel@tonic-gate return (ENXIO);
2790Sstevel@tonic-gate else
2800Sstevel@tonic-gate return (0);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate int
stripe_build_incore(void * p,int snarfing)2840Sstevel@tonic-gate stripe_build_incore(void *p, int snarfing)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate ms_unit_t *un = (ms_unit_t *)p;
2870Sstevel@tonic-gate struct ms_comp *mdcomp;
2880Sstevel@tonic-gate minor_t mnum;
2890Sstevel@tonic-gate int row;
2900Sstevel@tonic-gate int i;
2910Sstevel@tonic-gate int c;
2920Sstevel@tonic-gate int ncomps;
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate mnum = MD_SID(un);
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate if (MD_UNIT(mnum) != NULL)
2970Sstevel@tonic-gate return (0);
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate MD_STATUS(un) = 0;
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate * Reset all the is_open flags, these are probably set
3030Sstevel@tonic-gate * cause they just came out of the database.
3040Sstevel@tonic-gate */
3050Sstevel@tonic-gate mdcomp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate ncomps = 0;
3080Sstevel@tonic-gate for (row = 0; row < un->un_nrows; row++) {
3090Sstevel@tonic-gate struct ms_row *mdr = &un->un_row[row];
3100Sstevel@tonic-gate ncomps += mdr->un_ncomp;
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate for (row = 0; row < un->un_nrows; row++) {
3140Sstevel@tonic-gate struct ms_row *mdr = &un->un_row[row];
3150Sstevel@tonic-gate for (i = 0, c = mdr->un_icomp; i < mdr->un_ncomp; i++) {
3160Sstevel@tonic-gate struct ms_comp *mdc;
3170Sstevel@tonic-gate set_t setno;
3180Sstevel@tonic-gate md_dev64_t tmpdev;
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate mdc = &mdcomp[c++];
3210Sstevel@tonic-gate mdc->un_mirror.ms_flags &=
3220Sstevel@tonic-gate ~(MDM_S_ISOPEN | MDM_S_IOERR | MDM_S_RS_TRIED);
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate if (!snarfing)
3250Sstevel@tonic-gate continue;
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate setno = MD_MIN2SET(mnum);
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate tmpdev = md_getdevnum(setno, mddb_getsidenum(setno),
3300Sstevel@tonic-gate mdc->un_key, MD_NOTRUST_DEVT);
3310Sstevel@tonic-gate mdc->un_dev = tmpdev;
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate * Check for hotspares. If the hotspares haven't been
3340Sstevel@tonic-gate * snarfed yet, stripe_open_all_devs() will do the
3350Sstevel@tonic-gate * remapping of the dev's later.
3360Sstevel@tonic-gate */
3370Sstevel@tonic-gate if (mdc->un_mirror.ms_hs_id != 0) {
3380Sstevel@tonic-gate mdc->un_mirror.ms_orig_dev = mdc->un_dev;
3390Sstevel@tonic-gate (void) md_hot_spare_ifc(HS_MKDEV, 0, 0,
3400Sstevel@tonic-gate 0, &mdc->un_mirror.ms_hs_id, NULL,
3410Sstevel@tonic-gate &tmpdev, NULL);
3420Sstevel@tonic-gate mdc->un_dev = tmpdev;
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate
3477627SChris.Horne@Sun.COM /* place various information in the in-core data structures */
3487627SChris.Horne@Sun.COM md_nblocks_set(mnum, un->c.un_total_blocks);
3490Sstevel@tonic-gate MD_UNIT(mnum) = un;
3507627SChris.Horne@Sun.COM
3510Sstevel@tonic-gate return (0);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate void
reset_stripe(ms_unit_t * un,minor_t mnum,int removing)3550Sstevel@tonic-gate reset_stripe(ms_unit_t *un, minor_t mnum, int removing)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate ms_comp_t *mdcomp;
3580Sstevel@tonic-gate struct ms_row *mdr;
3590Sstevel@tonic-gate int i, c;
3600Sstevel@tonic-gate int row;
3610Sstevel@tonic-gate int nsv;
3620Sstevel@tonic-gate int isv;
3630Sstevel@tonic-gate sv_dev_t *sv;
3640Sstevel@tonic-gate mddb_recid_t *recids;
3650Sstevel@tonic-gate mddb_recid_t vtoc_id;
3660Sstevel@tonic-gate int rid = 0;
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate md_destroy_unit_incore(mnum, &stripe_md_ops);
3690Sstevel@tonic-gate
3707627SChris.Horne@Sun.COM md_nblocks_set(mnum, -1ULL);
3710Sstevel@tonic-gate MD_UNIT(mnum) = NULL;
3720Sstevel@tonic-gate
3731623Stw21770 /*
3741623Stw21770 * Attempt release of its minor node
3751623Stw21770 */
3762077Stw21770 md_remove_minor_node(mnum);
3771623Stw21770
3780Sstevel@tonic-gate if (!removing)
3790Sstevel@tonic-gate return;
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate nsv = 0;
3820Sstevel@tonic-gate /* Count the number of devices */
3830Sstevel@tonic-gate for (row = 0; row < un->un_nrows; row++) {
3840Sstevel@tonic-gate mdr = &un->un_row[row];
3850Sstevel@tonic-gate nsv += mdr->un_ncomp;
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate sv = (sv_dev_t *)kmem_alloc(sizeof (sv_dev_t) * nsv, KM_SLEEP);
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate * allocate recids array. since we may have to commit
3910Sstevel@tonic-gate * underlying soft partition records, we need an array
3920Sstevel@tonic-gate * of size: total number of components in stripe + 3
3930Sstevel@tonic-gate * (one for the stripe itself, one for the hotspare, one
3940Sstevel@tonic-gate * for the end marker).
3950Sstevel@tonic-gate */
3960Sstevel@tonic-gate recids = kmem_alloc(sizeof (mddb_recid_t) * (nsv + 3), KM_SLEEP);
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate /*
3990Sstevel@tonic-gate * Save the md_dev64_t's and driver nm indexes.
4000Sstevel@tonic-gate * Because after the mddb_deleterec() we will
4010Sstevel@tonic-gate * not be able to access the unit structure.
4020Sstevel@tonic-gate *
4030Sstevel@tonic-gate * NOTE: Deleting the names before deleting the
4040Sstevel@tonic-gate * unit structure would cause problems if
4050Sstevel@tonic-gate * the machine crashed in between the two.
4060Sstevel@tonic-gate */
4070Sstevel@tonic-gate isv = 0;
4080Sstevel@tonic-gate mdcomp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate for (row = 0; row < un->un_nrows; row++) {
4110Sstevel@tonic-gate mdr = &un->un_row[row];
4120Sstevel@tonic-gate for (i = 0, c = mdr->un_icomp; i < mdr->un_ncomp; i++) {
4130Sstevel@tonic-gate struct ms_comp *mdc;
4140Sstevel@tonic-gate md_dev64_t child_dev;
4150Sstevel@tonic-gate md_unit_t *child_un;
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate mdc = &mdcomp[c++];
4180Sstevel@tonic-gate if (mdc->un_mirror.ms_hs_id != 0) {
4190Sstevel@tonic-gate mdkey_t hs_key;
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate hs_key = mdc->un_mirror.ms_hs_key;
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate mdc->un_dev = mdc->un_mirror.ms_orig_dev;
4240Sstevel@tonic-gate mdc->un_start_block =
4250Sstevel@tonic-gate mdc->un_mirror.ms_orig_blk;
4260Sstevel@tonic-gate mdc->un_mirror.ms_hs_id = 0;
4270Sstevel@tonic-gate mdc->un_mirror.ms_hs_key = 0;
4280Sstevel@tonic-gate mdc->un_mirror.ms_orig_dev = 0;
4290Sstevel@tonic-gate recids[0] = 0;
4300Sstevel@tonic-gate recids[1] = 0; /* recids[1] filled in below */
4310Sstevel@tonic-gate recids[2] = 0;
4320Sstevel@tonic-gate (void) md_hot_spare_ifc(HS_FREE, un->un_hsp_id,
4330Sstevel@tonic-gate 0, 0, &recids[0], &hs_key, NULL, NULL);
4340Sstevel@tonic-gate mddb_commitrecs_wrapper(recids);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate * check if we've got metadevice below us and
4390Sstevel@tonic-gate * deparent it if we do.
4400Sstevel@tonic-gate * NOTE: currently soft partitions are the
4410Sstevel@tonic-gate * the only metadevices stripes can be
4420Sstevel@tonic-gate * built on top of.
4430Sstevel@tonic-gate */
4440Sstevel@tonic-gate child_dev = mdc->un_dev;
4450Sstevel@tonic-gate if (md_getmajor(child_dev) == md_major) {
4460Sstevel@tonic-gate child_un = MD_UNIT(md_getminor(child_dev));
4470Sstevel@tonic-gate md_reset_parent(child_dev);
4480Sstevel@tonic-gate recids[rid++] = MD_RECID(child_un);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate sv[isv].setno = MD_MIN2SET(mnum);
4520Sstevel@tonic-gate sv[isv++].key = mdc->un_key;
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate recids[rid++] = un->c.un_record_id;
4570Sstevel@tonic-gate recids[rid] = 0; /* filled in below */
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate /*
4600Sstevel@tonic-gate * Decrement the HSP reference count and
4610Sstevel@tonic-gate * remove the knowledge of the HSP from the unit struct.
4620Sstevel@tonic-gate * This is done atomically to remove a window.
4630Sstevel@tonic-gate */
4640Sstevel@tonic-gate if (un->un_hsp_id != -1) {
4650Sstevel@tonic-gate (void) md_hot_spare_ifc(HSP_DECREF, un->un_hsp_id, 0, 0,
4660Sstevel@tonic-gate &recids[rid++], NULL, NULL, NULL);
4670Sstevel@tonic-gate un->un_hsp_id = -1;
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate /* set end marker and commit records */
4710Sstevel@tonic-gate recids[rid] = 0;
4720Sstevel@tonic-gate mddb_commitrecs_wrapper(recids);
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate vtoc_id = un->c.un_vtoc_id;
4750Sstevel@tonic-gate
4761623Stw21770 /*
4771623Stw21770 * Remove self from the namespace
4781623Stw21770 */
4791623Stw21770 if (un->c.un_revision & MD_FN_META_DEV) {
4801623Stw21770 (void) md_rem_selfname(un->c.un_self_id);
4811623Stw21770 }
4821623Stw21770
4830Sstevel@tonic-gate /* Remove the unit structure */
4840Sstevel@tonic-gate mddb_deleterec_wrapper(un->c.un_record_id);
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate /* Remove the vtoc, if present */
4870Sstevel@tonic-gate if (vtoc_id)
4880Sstevel@tonic-gate mddb_deleterec_wrapper(vtoc_id);
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_DELETE, SVM_TAG_METADEVICE,
4910Sstevel@tonic-gate MD_MIN2SET(mnum), MD_MIN2UNIT(mnum));
4920Sstevel@tonic-gate md_rem_names(sv, nsv);
4930Sstevel@tonic-gate kmem_free(sv, sizeof (sv_dev_t) * nsv);
4940Sstevel@tonic-gate kmem_free(recids, sizeof (mddb_recid_t) * (nsv + 3));
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate static void
stripe_error(md_sps_t * ps)4980Sstevel@tonic-gate stripe_error(md_sps_t *ps)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate struct buf *pb = ps->ps_bp;
5010Sstevel@tonic-gate mdi_unit_t *ui = ps->ps_ui;
5020Sstevel@tonic-gate md_dev64_t dev = ps->ps_errcomp->un_dev;
5030Sstevel@tonic-gate md_dev64_t md_dev = md_expldev(pb->b_edev);
5040Sstevel@tonic-gate char *str;
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate if (pb->b_flags & B_READ) {
5070Sstevel@tonic-gate ps->ps_errcomp->un_mirror.ms_flags |= MDM_S_READERR;
5080Sstevel@tonic-gate str = "read";
5090Sstevel@tonic-gate } else {
5100Sstevel@tonic-gate ps->ps_errcomp->un_mirror.ms_flags |= MDM_S_WRTERR;
5110Sstevel@tonic-gate str = "write";
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate if (!(ps->ps_flags & MD_SPS_DONTFREE)) {
5140Sstevel@tonic-gate if (MUTEX_HELD(&ps->ps_mx)) {
5150Sstevel@tonic-gate mutex_exit(&ps->ps_mx);
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate } else {
5180Sstevel@tonic-gate ASSERT(panicstr);
5190Sstevel@tonic-gate }
5200Sstevel@tonic-gate SPS_FREE(stripe_parent_cache, ps);
5210Sstevel@tonic-gate pb->b_flags |= B_ERROR;
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate md_kstat_done(ui, pb, 0);
5240Sstevel@tonic-gate md_unit_readerexit(ui);
5250Sstevel@tonic-gate md_biodone(pb);
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate cmn_err(CE_WARN, "md: %s: %s error on %s",
5280Sstevel@tonic-gate md_shortname(md_getminor(md_dev)), str,
5290Sstevel@tonic-gate md_devname(MD_DEV2SET(md_dev), dev, NULL, 0));
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate static int
stripe_done(struct buf * cb)5330Sstevel@tonic-gate stripe_done(struct buf *cb)
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate struct buf *pb;
5360Sstevel@tonic-gate mdi_unit_t *ui;
5370Sstevel@tonic-gate md_sps_t *ps;
5380Sstevel@tonic-gate md_scs_t *cs;
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate /*LINTED*/
5410Sstevel@tonic-gate cs = (md_scs_t *)((caddr_t)cb - md_stripe_mcs_buf_off);
5420Sstevel@tonic-gate ps = cs->cs_ps;
5430Sstevel@tonic-gate pb = ps->ps_bp;
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate mutex_enter(&ps->ps_mx);
5460Sstevel@tonic-gate if (cb->b_flags & B_ERROR) {
5470Sstevel@tonic-gate ps->ps_flags |= MD_SPS_ERROR;
5480Sstevel@tonic-gate pb->b_error = cb->b_error;
5490Sstevel@tonic-gate ps->ps_errcomp = cs->cs_comp;
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate if (cb->b_flags & B_REMAPPED)
5530Sstevel@tonic-gate bp_mapout(cb);
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate ps->ps_frags--;
5560Sstevel@tonic-gate if (ps->ps_frags != 0) {
5570Sstevel@tonic-gate mutex_exit(&ps->ps_mx);
5580Sstevel@tonic-gate kmem_cache_free(stripe_child_cache, cs);
5590Sstevel@tonic-gate return (1);
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate kmem_cache_free(stripe_child_cache, cs);
5620Sstevel@tonic-gate if (ps->ps_flags & MD_SPS_ERROR) {
5630Sstevel@tonic-gate stripe_error(ps);
5640Sstevel@tonic-gate return (1);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate ui = ps->ps_ui;
5670Sstevel@tonic-gate if (!(ps->ps_flags & MD_SPS_DONTFREE)) {
5680Sstevel@tonic-gate mutex_exit(&ps->ps_mx);
5690Sstevel@tonic-gate } else {
5700Sstevel@tonic-gate ASSERT(panicstr);
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate SPS_FREE(stripe_parent_cache, ps);
5730Sstevel@tonic-gate md_kstat_done(ui, pb, 0);
5740Sstevel@tonic-gate md_unit_readerexit(ui);
5750Sstevel@tonic-gate md_biodone(pb);
5760Sstevel@tonic-gate return (0);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate
5790Sstevel@tonic-gate
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate * This routine does the mapping from virtual (dev, blkno) of a metapartition
5820Sstevel@tonic-gate * to the real (dev, blkno) of a real disk partition.
5830Sstevel@tonic-gate * It goes to the md_conf[] table to find out the correct real partition
5840Sstevel@tonic-gate * dev and block number for this buffer.
5850Sstevel@tonic-gate *
5860Sstevel@tonic-gate * A single buf request can not go across real disk partition boundary.
5870Sstevel@tonic-gate * When the virtual request specified by (dev, blkno) spans more than one
5880Sstevel@tonic-gate * real partition, md_mapbuf will return 1. Then the caller should prepare
5890Sstevel@tonic-gate * another real buf and continue calling md_mapbuf to do the mapping until
5900Sstevel@tonic-gate * it returns 0.
5910Sstevel@tonic-gate *
5920Sstevel@tonic-gate */
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate static int
md_mapbuf(ms_unit_t * un,diskaddr_t blkno,u_longlong_t bcount,buf_t * bp,ms_comp_t ** mdc)5950Sstevel@tonic-gate md_mapbuf(
5960Sstevel@tonic-gate ms_unit_t *un,
5970Sstevel@tonic-gate diskaddr_t blkno,
5980Sstevel@tonic-gate u_longlong_t bcount,
5990Sstevel@tonic-gate buf_t *bp, /* if bp==NULL, skip bp updates */
6000Sstevel@tonic-gate ms_comp_t **mdc) /* if bp==NULL, skip mdc update */
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate struct ms_row *mdr;
6030Sstevel@tonic-gate struct ms_comp *mdcomp;
6040Sstevel@tonic-gate diskaddr_t stripe_blk;
6050Sstevel@tonic-gate diskaddr_t fragment, blk_in_row, endblk;
6060Sstevel@tonic-gate offset_t interlace;
6070Sstevel@tonic-gate size_t dev_index;
6080Sstevel@tonic-gate int row_index, more;
6090Sstevel@tonic-gate extern unsigned md_maxphys;
6100Sstevel@tonic-gate /* Work var's when bp==NULL */
6110Sstevel@tonic-gate u_longlong_t wb_bcount;
6120Sstevel@tonic-gate diskaddr_t wb_blkno;
6130Sstevel@tonic-gate md_dev64_t wb_edev;
6140Sstevel@tonic-gate ms_comp_t *wmdc;
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate /*
6170Sstevel@tonic-gate * Do a real calculation to derive the minor device of the
6180Sstevel@tonic-gate * Virtual Disk, which in turn will let us derive the
6190Sstevel@tonic-gate * device/minor of the underlying real device.
6200Sstevel@tonic-gate */
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate for (row_index = 0; row_index < un->un_nrows; row_index++) {
6240Sstevel@tonic-gate mdr = &un->un_row[row_index];
6250Sstevel@tonic-gate if (blkno < mdr->un_cum_blocks)
6260Sstevel@tonic-gate break;
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate ASSERT(row_index != un->un_nrows);
6290Sstevel@tonic-gate
6300Sstevel@tonic-gate mdcomp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate blk_in_row = blkno - mdr->un_cum_blocks + mdr->un_blocks;
6330Sstevel@tonic-gate endblk = (diskaddr_t)(blkno + howmany(bcount, DEV_BSIZE));
6340Sstevel@tonic-gate if (mdr->un_ncomp == 1) { /* No striping */
6350Sstevel@tonic-gate if (endblk > mdr->un_cum_blocks) {
6360Sstevel@tonic-gate wb_bcount = ldbtob(mdr->un_cum_blocks - blkno);
6370Sstevel@tonic-gate if ((row_index + 1) == un->un_nrows)
6380Sstevel@tonic-gate more = 0;
6390Sstevel@tonic-gate else
6400Sstevel@tonic-gate more = 1;
6410Sstevel@tonic-gate } else {
6420Sstevel@tonic-gate wb_bcount = bcount;
6430Sstevel@tonic-gate more = 0;
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate wmdc = &mdcomp[mdr->un_icomp];
6460Sstevel@tonic-gate wb_blkno = blk_in_row;
6470Sstevel@tonic-gate } else { /* Have striping */
6480Sstevel@tonic-gate interlace = mdr->un_interlace;
6490Sstevel@tonic-gate fragment = blk_in_row % interlace;
6500Sstevel@tonic-gate if (bcount > ldbtob(interlace - fragment)) {
6510Sstevel@tonic-gate more = 1;
6520Sstevel@tonic-gate wb_bcount = ldbtob(interlace - fragment);
6530Sstevel@tonic-gate } else {
6540Sstevel@tonic-gate more = 0;
6550Sstevel@tonic-gate wb_bcount = bcount;
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate stripe_blk = blk_in_row / interlace;
6590Sstevel@tonic-gate dev_index = (size_t)(stripe_blk % mdr->un_ncomp);
6600Sstevel@tonic-gate wmdc = &mdcomp[mdr->un_icomp + dev_index];
6617627SChris.Horne@Sun.COM wb_blkno = (diskaddr_t)(((stripe_blk / mdr->un_ncomp) *
6627627SChris.Horne@Sun.COM interlace) + fragment);
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate
6650Sstevel@tonic-gate wb_blkno += wmdc->un_start_block;
6660Sstevel@tonic-gate wb_edev = wmdc->un_dev;
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate /* only break up the I/O if we're not built on another metadevice */
6690Sstevel@tonic-gate if ((md_getmajor(wb_edev) != md_major) && (wb_bcount > md_maxphys)) {
6700Sstevel@tonic-gate wb_bcount = md_maxphys;
6710Sstevel@tonic-gate more = 1;
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate if (bp != (buf_t *)NULL) {
6740Sstevel@tonic-gate /*
6750Sstevel@tonic-gate * wb_bcount is limited by md_maxphys which is 'int'
6760Sstevel@tonic-gate */
6770Sstevel@tonic-gate bp->b_bcount = (size_t)wb_bcount;
6780Sstevel@tonic-gate bp->b_lblkno = wb_blkno;
6790Sstevel@tonic-gate bp->b_edev = md_dev64_to_dev(wb_edev);
6800Sstevel@tonic-gate *mdc = wmdc;
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate return (more);
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate static void
md_stripe_strategy(buf_t * pb,int flag,void * private)6860Sstevel@tonic-gate md_stripe_strategy(buf_t *pb, int flag, void *private)
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate md_sps_t *ps;
6890Sstevel@tonic-gate md_scs_t *cs;
6900Sstevel@tonic-gate int doing_writes;
6910Sstevel@tonic-gate int more;
6920Sstevel@tonic-gate ms_unit_t *un;
6930Sstevel@tonic-gate mdi_unit_t *ui;
6940Sstevel@tonic-gate size_t current_count;
6950Sstevel@tonic-gate diskaddr_t current_blkno;
6960Sstevel@tonic-gate off_t current_offset;
6970Sstevel@tonic-gate buf_t *cb; /* child buf pointer */
6980Sstevel@tonic-gate set_t setno;
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate setno = MD_MIN2SET(getminor(pb->b_edev));
7010Sstevel@tonic-gate
7020Sstevel@tonic-gate /*
7030Sstevel@tonic-gate * When doing IO to a multi owner meta device, check if set is halted.
7040Sstevel@tonic-gate * We do this check without the needed lock held, for performance
7050Sstevel@tonic-gate * reasons.
7060Sstevel@tonic-gate * If an IO just slips through while the set is locked via an
7070Sstevel@tonic-gate * MD_MN_SUSPEND_SET, we don't care about it.
7080Sstevel@tonic-gate * Only check for a suspended set if we are a top-level i/o request
7090Sstevel@tonic-gate * (MD_STR_NOTTOP is cleared in 'flag').
7100Sstevel@tonic-gate */
7110Sstevel@tonic-gate if ((md_set[setno].s_status & (MD_SET_HALTED | MD_SET_MNSET)) ==
7120Sstevel@tonic-gate (MD_SET_HALTED | MD_SET_MNSET)) {
7130Sstevel@tonic-gate if ((flag & MD_STR_NOTTOP) == 0) {
7140Sstevel@tonic-gate mutex_enter(&md_mx);
7150Sstevel@tonic-gate /* Here we loop until the set is no longer halted */
7160Sstevel@tonic-gate while (md_set[setno].s_status & MD_SET_HALTED) {
7170Sstevel@tonic-gate cv_wait(&md_cv, &md_mx);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate mutex_exit(&md_mx);
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate
7230Sstevel@tonic-gate ui = MDI_UNIT(getminor(pb->b_edev));
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate md_kstat_waitq_enter(ui);
7260Sstevel@tonic-gate
7270Sstevel@tonic-gate un = (ms_unit_t *)md_unit_readerlock(ui);
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate if ((flag & MD_NOBLOCK) == 0) {
7300Sstevel@tonic-gate if (md_inc_iocount(setno) != 0) {
7310Sstevel@tonic-gate pb->b_flags |= B_ERROR;
7320Sstevel@tonic-gate pb->b_error = ENXIO;
7330Sstevel@tonic-gate pb->b_resid = pb->b_bcount;
7342150Sjeanm md_kstat_waitq_exit(ui);
7350Sstevel@tonic-gate md_unit_readerexit(ui);
7360Sstevel@tonic-gate biodone(pb);
7370Sstevel@tonic-gate return;
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate } else {
7400Sstevel@tonic-gate md_inc_iocount_noblock(setno);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate if (!(flag & MD_STR_NOTTOP)) {
7440Sstevel@tonic-gate if (md_checkbuf(ui, (md_unit_t *)un, pb) != 0) {
7450Sstevel@tonic-gate md_kstat_waitq_exit(ui);
7460Sstevel@tonic-gate return;
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate ps = kmem_cache_alloc(stripe_parent_cache, MD_ALLOCFLAGS);
7510Sstevel@tonic-gate stripe_parent_init(ps);
7520Sstevel@tonic-gate
7530Sstevel@tonic-gate /*
7540Sstevel@tonic-gate * Save essential information from the original buffhdr
7550Sstevel@tonic-gate * in the md_save structure.
7560Sstevel@tonic-gate */
7570Sstevel@tonic-gate ps->ps_un = un;
7580Sstevel@tonic-gate ps->ps_ui = ui;
7590Sstevel@tonic-gate ps->ps_bp = pb;
7600Sstevel@tonic-gate ps->ps_addr = pb->b_un.b_addr;
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate if ((pb->b_flags & B_READ) == 0)
7630Sstevel@tonic-gate doing_writes = 1;
7640Sstevel@tonic-gate else
7650Sstevel@tonic-gate doing_writes = 0;
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate
7680Sstevel@tonic-gate current_count = pb->b_bcount;
7690Sstevel@tonic-gate current_blkno = pb->b_lblkno;
7700Sstevel@tonic-gate current_offset = 0;
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate if (!(flag & MD_STR_NOTTOP) && panicstr)
7730Sstevel@tonic-gate ps->ps_flags |= MD_SPS_DONTFREE;
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate md_kstat_waitq_to_runq(ui);
7760Sstevel@tonic-gate
7770Sstevel@tonic-gate ps->ps_frags++;
7780Sstevel@tonic-gate do {
7790Sstevel@tonic-gate cs = kmem_cache_alloc(stripe_child_cache, MD_ALLOCFLAGS);
7800Sstevel@tonic-gate stripe_child_init(cs);
7810Sstevel@tonic-gate cb = &cs->cs_buf;
7820Sstevel@tonic-gate cs->cs_ps = ps;
7830Sstevel@tonic-gate more = md_mapbuf(un, current_blkno, current_count, cb,
7847627SChris.Horne@Sun.COM &cs->cs_comp);
7850Sstevel@tonic-gate
7860Sstevel@tonic-gate cb = md_bioclone(pb, current_offset, cb->b_bcount, cb->b_edev,
7877627SChris.Horne@Sun.COM cb->b_lblkno, stripe_done, cb, KM_NOSLEEP);
7880Sstevel@tonic-gate /*
7890Sstevel@tonic-gate * Do these calculations now,
7900Sstevel@tonic-gate * so that we pickup a valid b_bcount from the chld_bp.
7910Sstevel@tonic-gate */
7920Sstevel@tonic-gate current_offset += cb->b_bcount;
7930Sstevel@tonic-gate current_count -= cb->b_bcount;
7940Sstevel@tonic-gate current_blkno += (diskaddr_t)(lbtodb(cb->b_bcount));
7950Sstevel@tonic-gate
7960Sstevel@tonic-gate if (more) {
7970Sstevel@tonic-gate mutex_enter(&ps->ps_mx);
7980Sstevel@tonic-gate ps->ps_frags++;
7990Sstevel@tonic-gate mutex_exit(&ps->ps_mx);
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate if (doing_writes &&
8030Sstevel@tonic-gate cs->cs_comp->un_mirror.ms_flags & MDM_S_NOWRITE) {
8040Sstevel@tonic-gate (void) stripe_done(cb);
8050Sstevel@tonic-gate continue;
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate md_call_strategy(cb, flag, private);
8080Sstevel@tonic-gate } while (more);
8090Sstevel@tonic-gate
8100Sstevel@tonic-gate if (!(flag & MD_STR_NOTTOP) && panicstr) {
8110Sstevel@tonic-gate while (!(ps->ps_flags & MD_SPS_DONE)) {
8120Sstevel@tonic-gate md_daemon(1, &md_done_daemon);
8130Sstevel@tonic-gate drv_usecwait(10);
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate kmem_cache_free(stripe_parent_cache, ps);
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate static int
stripe_snarf(md_snarfcmd_t cmd,set_t setno)8200Sstevel@tonic-gate stripe_snarf(md_snarfcmd_t cmd, set_t setno)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate ms_unit_t *un;
8230Sstevel@tonic-gate mddb_recid_t recid;
8240Sstevel@tonic-gate int gotsomething;
8250Sstevel@tonic-gate int all_stripes_gotten;
8260Sstevel@tonic-gate mddb_type_t typ1;
8270Sstevel@tonic-gate mddb_de_ic_t *dep;
8280Sstevel@tonic-gate mddb_rb32_t *rbp;
8290Sstevel@tonic-gate size_t newreqsize;
8300Sstevel@tonic-gate ms_unit_t *big_un;
8310Sstevel@tonic-gate ms_unit32_od_t *small_un;
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate
8340Sstevel@tonic-gate if (cmd == MD_SNARF_CLEANUP)
8350Sstevel@tonic-gate return (0);
8360Sstevel@tonic-gate
8370Sstevel@tonic-gate all_stripes_gotten = 1;
8380Sstevel@tonic-gate gotsomething = 0;
8390Sstevel@tonic-gate
8400Sstevel@tonic-gate typ1 = (mddb_type_t)md_getshared_key(setno,
8410Sstevel@tonic-gate stripe_md_ops.md_driver.md_drivername);
8420Sstevel@tonic-gate recid = mddb_makerecid(setno, 0);
8430Sstevel@tonic-gate
8440Sstevel@tonic-gate while ((recid = mddb_getnextrec(recid, typ1, 0)) > 0) {
8450Sstevel@tonic-gate if (mddb_getrecprivate(recid) & MD_PRV_GOTIT)
8460Sstevel@tonic-gate continue;
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate dep = mddb_getrecdep(recid);
8490Sstevel@tonic-gate dep->de_flags = MDDB_F_STRIPE;
8500Sstevel@tonic-gate rbp = dep->de_rb;
8510Sstevel@tonic-gate
8521623Stw21770 switch (rbp->rb_revision) {
8531623Stw21770 case MDDB_REV_RB:
8541623Stw21770 case MDDB_REV_RBFN:
8551623Stw21770 if ((rbp->rb_private & MD_PRV_CONVD) == 0) {
8561623Stw21770 /*
8571623Stw21770 * This means, we have an old and small record
8581623Stw21770 * and this record hasn't already been
8591623Stw21770 * converted. Before we create an incore
8601623Stw21770 * metadevice from this we have to convert it to
8611623Stw21770 * a big record.
8621623Stw21770 */
8631623Stw21770 small_un =
8641623Stw21770 (ms_unit32_od_t *)mddb_getrecaddr(recid);
8651623Stw21770 newreqsize = get_big_stripe_req_size(small_un,
8667627SChris.Horne@Sun.COM COMPLETE_STRUCTURE);
8671623Stw21770 big_un = (ms_unit_t *)kmem_zalloc(newreqsize,
8687627SChris.Horne@Sun.COM KM_SLEEP);
8691623Stw21770 stripe_convert((caddr_t)small_un,
8707627SChris.Horne@Sun.COM (caddr_t)big_un, SMALL_2_BIG);
8711623Stw21770 kmem_free(small_un, dep->de_reqsize);
8721623Stw21770 dep->de_rb_userdata = big_un;
8731623Stw21770 dep->de_reqsize = newreqsize;
8741623Stw21770 un = big_un;
8751623Stw21770 rbp->rb_private |= MD_PRV_CONVD;
8761623Stw21770 } else {
8771623Stw21770 /* Small device had already been converted */
8781623Stw21770 un = (ms_unit_t *)mddb_getrecaddr(recid);
8791623Stw21770 }
8801623Stw21770 un->c.un_revision &= ~MD_64BIT_META_DEV;
8811623Stw21770 break;
8821623Stw21770 case MDDB_REV_RB64:
8831623Stw21770 case MDDB_REV_RB64FN:
8840Sstevel@tonic-gate /* Big device */
8850Sstevel@tonic-gate un = (ms_unit_t *)mddb_getrecaddr(recid);
8861623Stw21770 un->c.un_revision |= MD_64BIT_META_DEV;
8871623Stw21770 un->c.un_flag |= MD_EFILABEL;
8881623Stw21770 break;
8890Sstevel@tonic-gate }
8902077Stw21770 MDDB_NOTE_FN(rbp->rb_revision, un->c.un_revision);
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate /* Create minor node for snarfed unit. */
8930Sstevel@tonic-gate (void) md_create_minor_node(MD_MIN2SET(MD_SID(un)), MD_SID(un));
8940Sstevel@tonic-gate
8950Sstevel@tonic-gate if (MD_UNIT(MD_SID(un)) != NULL) {
8960Sstevel@tonic-gate mddb_setrecprivate(recid, MD_PRV_PENDDEL);
8970Sstevel@tonic-gate continue;
8980Sstevel@tonic-gate }
8990Sstevel@tonic-gate all_stripes_gotten = 0;
9000Sstevel@tonic-gate if (stripe_build_incore((void *)un, 1) == 0) {
9010Sstevel@tonic-gate mddb_setrecprivate(recid, MD_PRV_GOTIT);
9020Sstevel@tonic-gate md_create_unit_incore(MD_SID(un), &stripe_md_ops, 0);
9030Sstevel@tonic-gate gotsomething = 1;
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate
9070Sstevel@tonic-gate if (!all_stripes_gotten)
9080Sstevel@tonic-gate return (gotsomething);
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate recid = mddb_makerecid(setno, 0);
9110Sstevel@tonic-gate while ((recid = mddb_getnextrec(recid, typ1, 0)) > 0)
9120Sstevel@tonic-gate if (!(mddb_getrecprivate(recid) & MD_PRV_GOTIT))
9130Sstevel@tonic-gate mddb_setrecprivate(recid, MD_PRV_PENDDEL);
9140Sstevel@tonic-gate
9150Sstevel@tonic-gate return (0);
9160Sstevel@tonic-gate }
9170Sstevel@tonic-gate
9180Sstevel@tonic-gate static int
stripe_halt(md_haltcmd_t cmd,set_t setno)9190Sstevel@tonic-gate stripe_halt(md_haltcmd_t cmd, set_t setno)
9200Sstevel@tonic-gate {
9210Sstevel@tonic-gate int i;
9220Sstevel@tonic-gate mdi_unit_t *ui;
9230Sstevel@tonic-gate minor_t mnum;
9240Sstevel@tonic-gate
9250Sstevel@tonic-gate if (cmd == MD_HALT_CLOSE)
9260Sstevel@tonic-gate return (0);
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate if (cmd == MD_HALT_OPEN)
9290Sstevel@tonic-gate return (0);
9300Sstevel@tonic-gate
9310Sstevel@tonic-gate if (cmd == MD_HALT_UNLOAD)
9320Sstevel@tonic-gate return (0);
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate if (cmd == MD_HALT_CHECK) {
9350Sstevel@tonic-gate for (i = 0; i < md_nunits; i++) {
9360Sstevel@tonic-gate mnum = MD_MKMIN(setno, i);
9370Sstevel@tonic-gate if ((ui = MDI_UNIT(mnum)) == NULL)
9380Sstevel@tonic-gate continue;
9390Sstevel@tonic-gate if (ui->ui_opsindex != stripe_md_ops.md_selfindex)
9400Sstevel@tonic-gate continue;
9410Sstevel@tonic-gate if (md_unit_isopen(ui))
9420Sstevel@tonic-gate return (1);
9430Sstevel@tonic-gate }
9440Sstevel@tonic-gate return (0);
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate
9470Sstevel@tonic-gate if (cmd != MD_HALT_DOIT)
9480Sstevel@tonic-gate return (1);
9490Sstevel@tonic-gate
9500Sstevel@tonic-gate for (i = 0; i < md_nunits; i++) {
9510Sstevel@tonic-gate mnum = MD_MKMIN(setno, i);
9520Sstevel@tonic-gate if ((ui = MDI_UNIT(mnum)) == NULL)
9530Sstevel@tonic-gate continue;
9540Sstevel@tonic-gate if (ui->ui_opsindex != stripe_md_ops.md_selfindex)
9550Sstevel@tonic-gate continue;
9560Sstevel@tonic-gate reset_stripe((ms_unit_t *)MD_UNIT(mnum), mnum, 0);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate
9590Sstevel@tonic-gate return (0);
9600Sstevel@tonic-gate }
9610Sstevel@tonic-gate
9620Sstevel@tonic-gate /*ARGSUSED3*/
9630Sstevel@tonic-gate static int
stripe_open(dev_t * dev,int flag,int otyp,cred_t * cred_p,int md_oflags)9640Sstevel@tonic-gate stripe_open(dev_t *dev, int flag, int otyp, cred_t *cred_p, int md_oflags)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate minor_t mnum = getminor(*dev);
9670Sstevel@tonic-gate mdi_unit_t *ui = MDI_UNIT(mnum);
9680Sstevel@tonic-gate ms_unit_t *un;
9690Sstevel@tonic-gate int err = 0;
97046Sskamm set_t setno;
97146Sskamm
97246Sskamm /*
97346Sskamm * When doing an open of a multi owner metadevice, check to see if this
97446Sskamm * node is a starting node and if a reconfig cycle is underway.
97546Sskamm * If so, the system isn't sufficiently set up enough to handle the
97646Sskamm * open (which involves I/O during sp_validate), so fail with ENXIO.
97746Sskamm */
97846Sskamm setno = MD_MIN2SET(mnum);
97946Sskamm if ((md_set[setno].s_status & (MD_SET_MNSET | MD_SET_MN_START_RC)) ==
98046Sskamm (MD_SET_MNSET | MD_SET_MN_START_RC)) {
98146Sskamm return (ENXIO);
98246Sskamm }
9830Sstevel@tonic-gate
9840Sstevel@tonic-gate /* single thread */
9850Sstevel@tonic-gate un = (ms_unit_t *)md_unit_openclose_enter(ui);
9860Sstevel@tonic-gate
9870Sstevel@tonic-gate /* open devices, if necessary */
9880Sstevel@tonic-gate if (! md_unit_isopen(ui) || (md_oflags & MD_OFLG_PROBEDEV)) {
9890Sstevel@tonic-gate if ((err = stripe_open_all_devs(un, md_oflags)) != 0) {
9900Sstevel@tonic-gate goto out;
9910Sstevel@tonic-gate }
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate
9940Sstevel@tonic-gate /* count open */
9950Sstevel@tonic-gate if ((err = md_unit_incopen(mnum, flag, otyp)) != 0)
9960Sstevel@tonic-gate goto out;
9970Sstevel@tonic-gate
9980Sstevel@tonic-gate /* unlock, return success */
9990Sstevel@tonic-gate out:
10000Sstevel@tonic-gate md_unit_openclose_exit(ui);
10010Sstevel@tonic-gate return (err);
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate
10040Sstevel@tonic-gate /*ARGSUSED1*/
10050Sstevel@tonic-gate static int
stripe_close(dev_t dev,int flag,int otyp,cred_t * cred_p,int md_cflags)10060Sstevel@tonic-gate stripe_close(
10070Sstevel@tonic-gate dev_t dev,
10080Sstevel@tonic-gate int flag,
10090Sstevel@tonic-gate int otyp,
10100Sstevel@tonic-gate cred_t *cred_p,
10110Sstevel@tonic-gate int md_cflags
10120Sstevel@tonic-gate )
10130Sstevel@tonic-gate {
10140Sstevel@tonic-gate minor_t mnum = getminor(dev);
10150Sstevel@tonic-gate mdi_unit_t *ui = MDI_UNIT(mnum);
10160Sstevel@tonic-gate ms_unit_t *un;
10170Sstevel@tonic-gate int err = 0;
10180Sstevel@tonic-gate
10190Sstevel@tonic-gate /* single thread */
10200Sstevel@tonic-gate un = (ms_unit_t *)md_unit_openclose_enter(ui);
10210Sstevel@tonic-gate
10220Sstevel@tonic-gate /* count closed */
10230Sstevel@tonic-gate if ((err = md_unit_decopen(mnum, otyp)) != 0)
10240Sstevel@tonic-gate goto out;
10250Sstevel@tonic-gate
10260Sstevel@tonic-gate /* close devices, if necessary */
10270Sstevel@tonic-gate if (! md_unit_isopen(ui) || (md_cflags & MD_OFLG_PROBEDEV)) {
10280Sstevel@tonic-gate stripe_close_all_devs(un, md_cflags);
10290Sstevel@tonic-gate }
10300Sstevel@tonic-gate
10310Sstevel@tonic-gate /* unlock, return success */
10320Sstevel@tonic-gate out:
10330Sstevel@tonic-gate md_unit_openclose_exit(ui);
10340Sstevel@tonic-gate return (err);
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate
10370Sstevel@tonic-gate
10380Sstevel@tonic-gate static struct buf dumpbuf;
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate /*
10410Sstevel@tonic-gate * This routine dumps memory to the disk. It assumes that the memory has
10420Sstevel@tonic-gate * already been mapped into mainbus space. It is called at disk interrupt
10430Sstevel@tonic-gate * priority when the system is in trouble.
10440Sstevel@tonic-gate *
10450Sstevel@tonic-gate */
10460Sstevel@tonic-gate static int
stripe_dump(dev_t dev,caddr_t addr,daddr_t blkno,int nblk)10470Sstevel@tonic-gate stripe_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
10480Sstevel@tonic-gate {
10490Sstevel@tonic-gate ms_unit_t *un;
10500Sstevel@tonic-gate buf_t *bp;
10510Sstevel@tonic-gate ms_comp_t *mdc;
10520Sstevel@tonic-gate u_longlong_t nb;
10530Sstevel@tonic-gate diskaddr_t mapblk;
10540Sstevel@tonic-gate int result;
10550Sstevel@tonic-gate int more;
10560Sstevel@tonic-gate int saveresult = 0;
10570Sstevel@tonic-gate
10580Sstevel@tonic-gate /*
10590Sstevel@tonic-gate * Don't need to grab the unit lock.
10600Sstevel@tonic-gate * Cause nothing else is suppose to be happenning.
10610Sstevel@tonic-gate * Also dump is not suppose to sleep.
10620Sstevel@tonic-gate */
10630Sstevel@tonic-gate un = (ms_unit_t *)MD_UNIT(getminor(dev));
10640Sstevel@tonic-gate
10650Sstevel@tonic-gate if ((diskaddr_t)blkno >= un->c.un_total_blocks)
10660Sstevel@tonic-gate return (EINVAL);
10670Sstevel@tonic-gate
10680Sstevel@tonic-gate if ((diskaddr_t)blkno + nblk > un->c.un_total_blocks)
10690Sstevel@tonic-gate return (EINVAL);
10700Sstevel@tonic-gate
10710Sstevel@tonic-gate bp = &dumpbuf;
10720Sstevel@tonic-gate nb = ldbtob(nblk);
10730Sstevel@tonic-gate do {
10740Sstevel@tonic-gate bzero((caddr_t)bp, sizeof (*bp));
10750Sstevel@tonic-gate more = md_mapbuf(un, (diskaddr_t)blkno, nb, bp, &mdc);
10760Sstevel@tonic-gate nblk = btodb(bp->b_bcount);
10770Sstevel@tonic-gate mapblk = bp->b_lblkno;
10780Sstevel@tonic-gate if (!(mdc->un_mirror.ms_flags & MDM_S_NOWRITE)) {
10790Sstevel@tonic-gate /*
10800Sstevel@tonic-gate * bdev_dump() is currently only able to take
10810Sstevel@tonic-gate * 32 bit wide blkno's.
10820Sstevel@tonic-gate */
10830Sstevel@tonic-gate result = bdev_dump(bp->b_edev, addr, (daddr_t)mapblk,
10847627SChris.Horne@Sun.COM nblk);
10850Sstevel@tonic-gate if (result)
10860Sstevel@tonic-gate saveresult = result;
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate
10890Sstevel@tonic-gate nb -= bp->b_bcount;
10900Sstevel@tonic-gate addr += bp->b_bcount;
10910Sstevel@tonic-gate blkno += nblk;
10920Sstevel@tonic-gate } while (more);
10930Sstevel@tonic-gate
10940Sstevel@tonic-gate return (saveresult);
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate
10970Sstevel@tonic-gate /*ARGSUSED*/
10980Sstevel@tonic-gate static intptr_t
stripe_shared_by_blk(md_dev64_t dev,void * junk,diskaddr_t blkno,u_longlong_t * cnt)10990Sstevel@tonic-gate stripe_shared_by_blk(
11000Sstevel@tonic-gate md_dev64_t dev,
11010Sstevel@tonic-gate void *junk,
11020Sstevel@tonic-gate diskaddr_t blkno,
11030Sstevel@tonic-gate u_longlong_t *cnt)
11040Sstevel@tonic-gate {
11050Sstevel@tonic-gate ms_unit_t *un;
11060Sstevel@tonic-gate buf_t bp;
11070Sstevel@tonic-gate ms_comp_t *comp;
11080Sstevel@tonic-gate
11090Sstevel@tonic-gate un = MD_UNIT(md_getminor(dev));
11100Sstevel@tonic-gate (void) md_mapbuf(un, blkno, ldbtob(*cnt), &bp, &comp);
11110Sstevel@tonic-gate *cnt = (u_longlong_t)lbtodb(bp.b_bcount);
11120Sstevel@tonic-gate return ((intptr_t)&comp->un_mirror);
11130Sstevel@tonic-gate }
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate /*
11160Sstevel@tonic-gate * stripe_block_count_skip_size() returns the following values
11170Sstevel@tonic-gate * so that the logical to physical block mappings can
11180Sstevel@tonic-gate * be calculated without intimate knowledge of the underpinnings.
11190Sstevel@tonic-gate *
11200Sstevel@tonic-gate * block - first logical block number of the device.
11210Sstevel@tonic-gate * block = [ # of blocks before THE row ] +
11220Sstevel@tonic-gate * [ # of blocks in THE row before the component ]
11230Sstevel@tonic-gate * count - # of segments (interlaced size).
11240Sstevel@tonic-gate * skip - # of logical blocks between segments, or delta to
11250Sstevel@tonic-gate * get to next segment
11260Sstevel@tonic-gate * size - interlace size used for the block, count, skip.
11270Sstevel@tonic-gate */
11280Sstevel@tonic-gate /*ARGSUSED*/
11290Sstevel@tonic-gate static intptr_t
stripe_block_count_skip_size(md_dev64_t dev,void * junk,int ci,diskaddr_t * block,size_t * count,u_longlong_t * skip,u_longlong_t * size)11300Sstevel@tonic-gate stripe_block_count_skip_size(
11310Sstevel@tonic-gate md_dev64_t dev,
11320Sstevel@tonic-gate void *junk,
11330Sstevel@tonic-gate int ci,
11340Sstevel@tonic-gate diskaddr_t *block,
11350Sstevel@tonic-gate size_t *count,
11360Sstevel@tonic-gate u_longlong_t *skip,
11370Sstevel@tonic-gate u_longlong_t *size)
11380Sstevel@tonic-gate {
11390Sstevel@tonic-gate ms_unit_t *un;
11400Sstevel@tonic-gate int row;
11410Sstevel@tonic-gate struct ms_row *mdr;
11420Sstevel@tonic-gate int cmpcount = 0;
11430Sstevel@tonic-gate
11440Sstevel@tonic-gate un = MD_UNIT(md_getminor(dev));
11450Sstevel@tonic-gate
11460Sstevel@tonic-gate for (row = 0; row < un->un_nrows; row++) {
11470Sstevel@tonic-gate mdr = &un->un_row[row];
11480Sstevel@tonic-gate if ((mdr->un_ncomp + cmpcount) > ci)
11490Sstevel@tonic-gate break;
11500Sstevel@tonic-gate cmpcount += mdr->un_ncomp;
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate ASSERT(row != un->un_nrows);
11530Sstevel@tonic-gate
11540Sstevel@tonic-gate /*
11550Sstevel@tonic-gate * Concatenations are always contiguous blocks,
11560Sstevel@tonic-gate * you cannot depend on the interlace being a usable
11570Sstevel@tonic-gate * value (except for stripes).
11580Sstevel@tonic-gate */
11590Sstevel@tonic-gate if (mdr->un_ncomp == 1) { /* Concats */
11600Sstevel@tonic-gate *block = mdr->un_cum_blocks - mdr->un_blocks;
11610Sstevel@tonic-gate *count = 1;
11620Sstevel@tonic-gate *skip = 0;
11630Sstevel@tonic-gate *size = mdr->un_blocks;
11640Sstevel@tonic-gate } else { /* Stripes */
11650Sstevel@tonic-gate *block = (mdr->un_cum_blocks - mdr->un_blocks) +
11660Sstevel@tonic-gate ((ci - cmpcount) * mdr->un_interlace);
11677627SChris.Horne@Sun.COM *count = (size_t)(mdr->un_blocks / (mdr->un_interlace *
11687627SChris.Horne@Sun.COM mdr->un_ncomp));
11690Sstevel@tonic-gate *skip = (mdr->un_interlace * mdr->un_ncomp) - mdr->un_interlace;
11700Sstevel@tonic-gate *size = mdr->un_interlace;
11710Sstevel@tonic-gate }
11720Sstevel@tonic-gate
11730Sstevel@tonic-gate return (0);
11740Sstevel@tonic-gate }
11750Sstevel@tonic-gate
11760Sstevel@tonic-gate /*ARGSUSED*/
11770Sstevel@tonic-gate static intptr_t
stripe_shared_by_indx(md_dev64_t dev,void * junk,int indx)11780Sstevel@tonic-gate stripe_shared_by_indx(md_dev64_t dev, void *junk, int indx)
11790Sstevel@tonic-gate {
11800Sstevel@tonic-gate ms_unit_t *un;
11810Sstevel@tonic-gate ms_comp_t *comp;
11820Sstevel@tonic-gate
11830Sstevel@tonic-gate un = MD_UNIT(md_getminor(dev));
11840Sstevel@tonic-gate comp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
11850Sstevel@tonic-gate comp += indx;
11860Sstevel@tonic-gate return ((intptr_t)&comp->un_mirror);
11870Sstevel@tonic-gate }
11880Sstevel@tonic-gate
11890Sstevel@tonic-gate /*ARGSUSED*/
11900Sstevel@tonic-gate intptr_t
stripe_component_count(md_dev64_t dev,void * junk)11910Sstevel@tonic-gate stripe_component_count(md_dev64_t dev, void *junk)
11920Sstevel@tonic-gate {
11930Sstevel@tonic-gate /*
11940Sstevel@tonic-gate * See comments for stripe_get_dev
11950Sstevel@tonic-gate */
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate ms_unit_t *un;
11980Sstevel@tonic-gate int count = 0;
11990Sstevel@tonic-gate int row;
12000Sstevel@tonic-gate
12010Sstevel@tonic-gate un = MD_UNIT(md_getminor(dev));
12020Sstevel@tonic-gate for (row = 0; row < un->un_nrows; row++)
12030Sstevel@tonic-gate count += un->un_row[row].un_ncomp;
12040Sstevel@tonic-gate return (count);
12050Sstevel@tonic-gate }
12060Sstevel@tonic-gate
12070Sstevel@tonic-gate /*ARGSUSED*/
12080Sstevel@tonic-gate intptr_t
stripe_get_dev(md_dev64_t dev,void * junk,int indx,ms_cd_info_t * cd)12090Sstevel@tonic-gate stripe_get_dev(md_dev64_t dev, void *junk, int indx, ms_cd_info_t *cd)
12100Sstevel@tonic-gate {
12110Sstevel@tonic-gate /*
12120Sstevel@tonic-gate * It should be noted that stripe_replace in stripe_ioctl.c calls this
12130Sstevel@tonic-gate * routine using makedevice(0, minor) for the first argument.
12140Sstevel@tonic-gate *
12150Sstevel@tonic-gate * If this routine at some point in the future needs to use the major
12160Sstevel@tonic-gate * number stripe_replace must be changed.
12170Sstevel@tonic-gate */
12180Sstevel@tonic-gate
12190Sstevel@tonic-gate ms_unit_t *un;
12200Sstevel@tonic-gate ms_comp_t *comp;
12210Sstevel@tonic-gate md_dev64_t tmpdev;
12220Sstevel@tonic-gate
12230Sstevel@tonic-gate un = MD_UNIT(md_getminor(dev));
12240Sstevel@tonic-gate comp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
12250Sstevel@tonic-gate comp += indx;
12260Sstevel@tonic-gate tmpdev = comp->un_dev;
12270Sstevel@tonic-gate /*
12280Sstevel@tonic-gate * Try to resolve devt again if NODEV64
12290Sstevel@tonic-gate * Check if this comp is hotspared and if it is
12300Sstevel@tonic-gate * then use key for hotspare
12310Sstevel@tonic-gate */
12320Sstevel@tonic-gate if (tmpdev == NODEV64) {
12330Sstevel@tonic-gate tmpdev = md_resolve_bydevid(md_getminor(dev), tmpdev,
12347627SChris.Horne@Sun.COM comp->un_mirror.ms_hs_id ?
12357627SChris.Horne@Sun.COM comp->un_mirror.ms_hs_key :
12367627SChris.Horne@Sun.COM comp->un_key);
12370Sstevel@tonic-gate comp->un_dev = tmpdev;
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate
12400Sstevel@tonic-gate cd->cd_dev = comp->un_dev;
12410Sstevel@tonic-gate cd->cd_orig_dev = comp->un_mirror.ms_orig_dev;
12420Sstevel@tonic-gate return (0);
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate
12450Sstevel@tonic-gate /*ARGSUSED*/
12460Sstevel@tonic-gate void
stripe_replace_done(md_dev64_t dev,sv_dev_t * sv)12470Sstevel@tonic-gate stripe_replace_done(md_dev64_t dev, sv_dev_t *sv)
12480Sstevel@tonic-gate {
12490Sstevel@tonic-gate /*
12500Sstevel@tonic-gate * See comments for stripe_get_dev
12510Sstevel@tonic-gate */
12520Sstevel@tonic-gate
12530Sstevel@tonic-gate minor_t mnum = md_getminor(dev);
12540Sstevel@tonic-gate
12550Sstevel@tonic-gate if (sv != NULL) {
12560Sstevel@tonic-gate md_rem_names(sv, 1);
12570Sstevel@tonic-gate kmem_free(sv, sizeof (sv_dev_t));
12580Sstevel@tonic-gate }
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate md_unit_writerexit(MDI_UNIT(mnum));
12610Sstevel@tonic-gate }
12620Sstevel@tonic-gate
12630Sstevel@tonic-gate /*ARGSUSED*/
12640Sstevel@tonic-gate intptr_t
stripe_replace_dev(md_dev64_t dev,void * junk,int ci,ms_new_dev_t * nd,mddb_recid_t * recids,int nrecids,void (** replace_done)(),void ** replace_data)12650Sstevel@tonic-gate stripe_replace_dev(md_dev64_t dev, void *junk, int ci, ms_new_dev_t *nd,
12660Sstevel@tonic-gate mddb_recid_t *recids, int nrecids, void (**replace_done)(),
12670Sstevel@tonic-gate void **replace_data)
12680Sstevel@tonic-gate {
12690Sstevel@tonic-gate minor_t mnum;
12700Sstevel@tonic-gate ms_unit_t *un;
12710Sstevel@tonic-gate mdi_unit_t *ui;
12720Sstevel@tonic-gate ms_comp_t *comp;
12730Sstevel@tonic-gate diskaddr_t dev_size;
12740Sstevel@tonic-gate int row;
12750Sstevel@tonic-gate int ncomps = 0;
12760Sstevel@tonic-gate int cmpcount = 0;
12770Sstevel@tonic-gate int rid = 0;
12780Sstevel@tonic-gate struct ms_row *mdr;
12790Sstevel@tonic-gate sv_dev_t *sv = NULL;
12800Sstevel@tonic-gate mddb_recid_t hs_id = 0;
12810Sstevel@tonic-gate set_t setno;
12820Sstevel@tonic-gate side_t side;
12830Sstevel@tonic-gate md_dev64_t this_dev;
1284*12215SJames.Hall@Sun.COM md_dev64_t old_dev;
12850Sstevel@tonic-gate
12860Sstevel@tonic-gate mnum = md_getminor(dev);
12870Sstevel@tonic-gate ui = MDI_UNIT(mnum);
12880Sstevel@tonic-gate setno = MD_MIN2SET(mnum);
12890Sstevel@tonic-gate side = mddb_getsidenum(setno);
12900Sstevel@tonic-gate
12910Sstevel@tonic-gate un = md_unit_writerlock(ui);
12920Sstevel@tonic-gate
12930Sstevel@tonic-gate *replace_data = NULL;
12940Sstevel@tonic-gate comp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
12950Sstevel@tonic-gate
12960Sstevel@tonic-gate comp += ci;
1297*12215SJames.Hall@Sun.COM old_dev = comp->un_dev;
12980Sstevel@tonic-gate
12990Sstevel@tonic-gate /*
13000Sstevel@tonic-gate * Count the number of components
13010Sstevel@tonic-gate */
13020Sstevel@tonic-gate for (row = 0; row < un->un_nrows; row++) {
13030Sstevel@tonic-gate struct ms_row *mdr = &un->un_row[row];
13040Sstevel@tonic-gate ncomps += mdr->un_ncomp;
13050Sstevel@tonic-gate }
13060Sstevel@tonic-gate
13070Sstevel@tonic-gate recids[0] = 0;
13080Sstevel@tonic-gate /*
13090Sstevel@tonic-gate * No need of checking size of new device,
13100Sstevel@tonic-gate * when hotsparing (it has already been done), or
13110Sstevel@tonic-gate * when enabling the device.
13120Sstevel@tonic-gate */
13130Sstevel@tonic-gate if ((nd != NULL) && (nd->nd_hs_id == 0)) {
13140Sstevel@tonic-gate for (row = 0; row < un->un_nrows; row++) {
13150Sstevel@tonic-gate mdr = &un->un_row[row];
13160Sstevel@tonic-gate if ((mdr->un_ncomp + cmpcount) > ci)
13170Sstevel@tonic-gate break;
13180Sstevel@tonic-gate cmpcount += mdr->un_ncomp;
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate ASSERT(row != un->un_nrows);
13210Sstevel@tonic-gate
13220Sstevel@tonic-gate /* Concatenations have a ncomp = 1 */
13230Sstevel@tonic-gate dev_size = mdr->un_blocks / mdr->un_ncomp;
13240Sstevel@tonic-gate
13250Sstevel@tonic-gate /*
13260Sstevel@tonic-gate * now check to see if new comp can be used in
13270Sstevel@tonic-gate * place of old comp
13280Sstevel@tonic-gate */
13290Sstevel@tonic-gate if ((un->c.un_flag & MD_LABELED) && (ci == 0) &&
13300Sstevel@tonic-gate nd->nd_labeled)
13310Sstevel@tonic-gate nd->nd_start_blk = 0;
13320Sstevel@tonic-gate else
13330Sstevel@tonic-gate nd->nd_nblks -= nd->nd_start_blk;
13340Sstevel@tonic-gate
13350Sstevel@tonic-gate if (dev_size > nd->nd_nblks) {
13360Sstevel@tonic-gate md_unit_writerexit(ui);
13370Sstevel@tonic-gate return (MDE_COMP_TOO_SMALL);
13380Sstevel@tonic-gate }
13390Sstevel@tonic-gate
13400Sstevel@tonic-gate sv = (sv_dev_t *)kmem_alloc(sizeof (sv_dev_t), KM_SLEEP);
13410Sstevel@tonic-gate sv->setno = MD_MIN2SET(mnum);
13420Sstevel@tonic-gate sv->key = comp->un_key;
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate
13450Sstevel@tonic-gate /*
13460Sstevel@tonic-gate * Close this component.
13470Sstevel@tonic-gate */
13480Sstevel@tonic-gate if (comp->un_mirror.ms_flags & MDM_S_ISOPEN) {
13490Sstevel@tonic-gate md_layered_close(comp->un_dev, MD_OFLG_NULL);
13500Sstevel@tonic-gate comp->un_mirror.ms_flags &= ~MDM_S_ISOPEN;
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate
13530Sstevel@tonic-gate /*
13540Sstevel@tonic-gate * If the component is hotspared, return to the pool.
13550Sstevel@tonic-gate */
13560Sstevel@tonic-gate if (comp->un_mirror.ms_hs_id != 0) {
13570Sstevel@tonic-gate hs_cmds_t cmd;
13580Sstevel@tonic-gate mdkey_t hs_key;
13590Sstevel@tonic-gate
13600Sstevel@tonic-gate hs_key = comp->un_mirror.ms_hs_key;
13610Sstevel@tonic-gate comp->un_dev = comp->un_mirror.ms_orig_dev;
13620Sstevel@tonic-gate comp->un_start_block = comp->un_mirror.ms_orig_blk;
13630Sstevel@tonic-gate comp->un_mirror.ms_hs_key = 0;
13640Sstevel@tonic-gate comp->un_mirror.ms_hs_id = 0;
13650Sstevel@tonic-gate comp->un_mirror.ms_orig_dev = 0;
13660Sstevel@tonic-gate
13670Sstevel@tonic-gate cmd = HS_FREE;
13680Sstevel@tonic-gate if ((comp->un_mirror.ms_state != CS_OKAY) &&
13690Sstevel@tonic-gate (comp->un_mirror.ms_state != CS_RESYNC))
13700Sstevel@tonic-gate cmd = HS_BAD;
13710Sstevel@tonic-gate (void) md_hot_spare_ifc(cmd, un->un_hsp_id, 0, 0, &hs_id,
13720Sstevel@tonic-gate &hs_key, NULL, NULL);
13730Sstevel@tonic-gate }
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate /*
13760Sstevel@tonic-gate * Open by device id; for enable (indicated by a NULL
13770Sstevel@tonic-gate * nd pointer), use the existing component info. For
13780Sstevel@tonic-gate * replace, use the new device.
13790Sstevel@tonic-gate */
13800Sstevel@tonic-gate if (nd == NULL) {
13810Sstevel@tonic-gate this_dev = md_resolve_bydevid(mnum, comp->un_dev, comp->un_key);
13820Sstevel@tonic-gate /*
13830Sstevel@tonic-gate * If someone replaced a new disk in the same slot
13840Sstevel@tonic-gate * we get NODEV64 since old device id cannot be
13850Sstevel@tonic-gate * resolved. The new devt is obtained from the
13860Sstevel@tonic-gate * mddb since devt is going to be unchanged for the
13870Sstevel@tonic-gate * enable case. No need to check for multiple
13880Sstevel@tonic-gate * keys here because the caller (comp_replace)
13890Sstevel@tonic-gate * has already sanity checked it for us.
13900Sstevel@tonic-gate */
13910Sstevel@tonic-gate if (this_dev == NODEV64) {
13920Sstevel@tonic-gate this_dev = md_getdevnum(setno, side, comp->un_key,
13930Sstevel@tonic-gate MD_TRUST_DEVT);
13940Sstevel@tonic-gate }
13950Sstevel@tonic-gate } else {
13960Sstevel@tonic-gate /*
13970Sstevel@tonic-gate * If this is a hotspare, save the original dev_t for later
13980Sstevel@tonic-gate * use. If this has occured during boot then the value of
13990Sstevel@tonic-gate * comp->un_dev will be NODEV64 because of the failure to look
14000Sstevel@tonic-gate * up the devid of the device.
14010Sstevel@tonic-gate */
14020Sstevel@tonic-gate if (nd->nd_hs_id != 0)
14030Sstevel@tonic-gate comp->un_mirror.ms_orig_dev = comp->un_dev;
14040Sstevel@tonic-gate this_dev = md_resolve_bydevid(mnum, nd->nd_dev, nd->nd_key);
14050Sstevel@tonic-gate }
14060Sstevel@tonic-gate
14070Sstevel@tonic-gate comp->un_dev = this_dev;
14080Sstevel@tonic-gate
14090Sstevel@tonic-gate /*
14100Sstevel@tonic-gate * Now open the new device if required. Note for a single component
14110Sstevel@tonic-gate * stripe it will not be open - leave this for the mirror driver to
14120Sstevel@tonic-gate * deal with.
14130Sstevel@tonic-gate */
14140Sstevel@tonic-gate if (md_unit_isopen(ui)) {
14150Sstevel@tonic-gate if (md_layered_open(mnum, &this_dev, MD_OFLG_NULL)) {
14160Sstevel@tonic-gate mddb_recid_t ids[3];
14170Sstevel@tonic-gate
14180Sstevel@tonic-gate ids[0] = un->c.un_record_id;
14190Sstevel@tonic-gate ids[1] = hs_id;
14200Sstevel@tonic-gate ids[2] = 0;
14210Sstevel@tonic-gate mddb_commitrecs_wrapper(ids);
14220Sstevel@tonic-gate if ((nd != NULL) && (nd->nd_hs_id != 0)) {
14230Sstevel@tonic-gate /*
14240Sstevel@tonic-gate * Revert back to the original device.
14250Sstevel@tonic-gate */
14260Sstevel@tonic-gate comp->un_dev = comp->un_mirror.ms_orig_dev;
14270Sstevel@tonic-gate
14280Sstevel@tonic-gate cmn_err(CE_WARN,
14290Sstevel@tonic-gate "md: %s: open error of hotspare %s",
14300Sstevel@tonic-gate md_shortname(mnum),
14310Sstevel@tonic-gate md_devname(MD_MIN2SET(mnum), nd->nd_dev,
14320Sstevel@tonic-gate NULL, 0));
14330Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_OPEN_FAIL,
14340Sstevel@tonic-gate SVM_TAG_HS, MD_MIN2SET(mnum), nd->nd_dev);
14350Sstevel@tonic-gate }
14360Sstevel@tonic-gate md_unit_writerexit(ui);
14370Sstevel@tonic-gate return (MDE_COMP_OPEN_ERR);
14380Sstevel@tonic-gate }
14390Sstevel@tonic-gate if (nd != NULL)
14400Sstevel@tonic-gate nd->nd_dev = this_dev;
14410Sstevel@tonic-gate
14420Sstevel@tonic-gate comp->un_mirror.ms_flags |= MDM_S_ISOPEN;
14430Sstevel@tonic-gate }
14440Sstevel@tonic-gate
14450Sstevel@tonic-gate if (nd == NULL) {
14460Sstevel@tonic-gate recids[0] = un->c.un_record_id;
14470Sstevel@tonic-gate recids[1] = hs_id;
14480Sstevel@tonic-gate recids[2] = 0;
14490Sstevel@tonic-gate *replace_done = stripe_replace_done;
14500Sstevel@tonic-gate return (0);
14510Sstevel@tonic-gate }
14520Sstevel@tonic-gate
14530Sstevel@tonic-gate /* if hot sparing this device */
14540Sstevel@tonic-gate if (nd->nd_hs_id != 0) {
14550Sstevel@tonic-gate char devname[MD_MAX_CTDLEN];
14560Sstevel@tonic-gate char hs_devname[MD_MAX_CTDLEN];
14570Sstevel@tonic-gate set_t setno;
14580Sstevel@tonic-gate
14590Sstevel@tonic-gate comp->un_mirror.ms_hs_id = nd->nd_hs_id;
14600Sstevel@tonic-gate comp->un_mirror.ms_hs_key = nd->nd_key;
14610Sstevel@tonic-gate
14620Sstevel@tonic-gate comp->un_mirror.ms_orig_blk = comp->un_start_block;
14630Sstevel@tonic-gate
14640Sstevel@tonic-gate setno = MD_MIN2SET(mnum);
14650Sstevel@tonic-gate
14660Sstevel@tonic-gate (void) md_devname(setno, comp->un_mirror.ms_orig_dev, devname,
14677627SChris.Horne@Sun.COM sizeof (devname));
14680Sstevel@tonic-gate (void) md_devname(setno, nd->nd_dev, hs_devname,
14690Sstevel@tonic-gate sizeof (hs_devname));
14700Sstevel@tonic-gate
14710Sstevel@tonic-gate cmn_err(CE_NOTE, "md: %s: hotspared device %s with %s",
14720Sstevel@tonic-gate md_shortname(mnum), devname, hs_devname);
14730Sstevel@tonic-gate
14740Sstevel@tonic-gate } else { /* replacing the device */
14750Sstevel@tonic-gate comp->un_key = nd->nd_key;
14760Sstevel@tonic-gate *replace_data = (void *)sv;
14770Sstevel@tonic-gate
14780Sstevel@tonic-gate /*
14790Sstevel@tonic-gate * For the old device, make sure to reset the parent
14800Sstevel@tonic-gate * if it's a metadevice.
14810Sstevel@tonic-gate */
14820Sstevel@tonic-gate if (md_getmajor(comp->un_dev) == md_major) {
1483*12215SJames.Hall@Sun.COM minor_t comp_mnum = md_getminor(old_dev);
14840Sstevel@tonic-gate md_unit_t *comp_un = MD_UNIT(comp_mnum);
14850Sstevel@tonic-gate
1486*12215SJames.Hall@Sun.COM md_reset_parent(old_dev);
14870Sstevel@tonic-gate recids[rid++] = MD_RECID(comp_un);
14880Sstevel@tonic-gate }
14890Sstevel@tonic-gate }
14900Sstevel@tonic-gate
14910Sstevel@tonic-gate comp->un_dev = nd->nd_dev;
14920Sstevel@tonic-gate comp->un_start_block = nd->nd_start_blk;
14930Sstevel@tonic-gate
14940Sstevel@tonic-gate /*
14950Sstevel@tonic-gate * For the new device, make sure to set the parent if it's a
14960Sstevel@tonic-gate * metadevice.
14970Sstevel@tonic-gate *
14980Sstevel@tonic-gate * If we ever support using metadevices as hot spares, this
14990Sstevel@tonic-gate * will need to be tested, and possibly moved into the
15000Sstevel@tonic-gate * preceding "else" clause, immediately following the parent
15010Sstevel@tonic-gate * reset block. For now, it's convenient to leave it here and
15020Sstevel@tonic-gate * only compress nd->nd_dev once.
15030Sstevel@tonic-gate */
15040Sstevel@tonic-gate if (md_getmajor(comp->un_dev) == md_major) {
15050Sstevel@tonic-gate minor_t comp_mnum = md_getminor(comp->un_dev);
15060Sstevel@tonic-gate md_unit_t *comp_un = MD_UNIT(comp_mnum);
15070Sstevel@tonic-gate
15080Sstevel@tonic-gate md_set_parent(comp->un_dev, MD_SID(un));
15090Sstevel@tonic-gate recids[rid++] = MD_RECID(comp_un);
15100Sstevel@tonic-gate }
15110Sstevel@tonic-gate
15120Sstevel@tonic-gate recids[rid++] = un->c.un_record_id;
15130Sstevel@tonic-gate recids[rid++] = hs_id;
15140Sstevel@tonic-gate recids[rid] = 0;
15150Sstevel@tonic-gate *replace_done = stripe_replace_done;
15160Sstevel@tonic-gate return (0);
15170Sstevel@tonic-gate }
15180Sstevel@tonic-gate
15190Sstevel@tonic-gate /*ARGSUSED*/
15200Sstevel@tonic-gate static intptr_t
stripe_hotspare_dev(md_dev64_t dev,void * junk,int ci,mddb_recid_t * recids,int nrecids,void (** replace_done)(),void ** replace_data)15210Sstevel@tonic-gate stripe_hotspare_dev(
15220Sstevel@tonic-gate md_dev64_t dev,
15230Sstevel@tonic-gate void *junk,
15240Sstevel@tonic-gate int ci,
15250Sstevel@tonic-gate mddb_recid_t *recids,
15260Sstevel@tonic-gate int nrecids,
15270Sstevel@tonic-gate void (**replace_done)(),
15280Sstevel@tonic-gate void **replace_data)
15290Sstevel@tonic-gate {
15300Sstevel@tonic-gate ms_unit_t *un;
15310Sstevel@tonic-gate mdi_unit_t *ui;
15320Sstevel@tonic-gate ms_comp_t *comp;
15330Sstevel@tonic-gate int row;
15340Sstevel@tonic-gate struct ms_row *mdr;
15350Sstevel@tonic-gate ms_new_dev_t nd;
15360Sstevel@tonic-gate int err;
15370Sstevel@tonic-gate int i;
15380Sstevel@tonic-gate minor_t mnum;
15390Sstevel@tonic-gate set_t setno;
15400Sstevel@tonic-gate int cmpcount = 0;
15410Sstevel@tonic-gate
15420Sstevel@tonic-gate mnum = md_getminor(dev);
15430Sstevel@tonic-gate ui = MDI_UNIT(mnum);
15440Sstevel@tonic-gate un = MD_UNIT(mnum);
15450Sstevel@tonic-gate setno = MD_MIN2SET(mnum);
15460Sstevel@tonic-gate
15470Sstevel@tonic-gate if (md_get_setstatus(setno) & MD_SET_STALE)
15480Sstevel@tonic-gate return (1);
15490Sstevel@tonic-gate
15500Sstevel@tonic-gate if (un->un_hsp_id == -1)
15510Sstevel@tonic-gate return (1);
15520Sstevel@tonic-gate
15530Sstevel@tonic-gate for (row = 0; row < un->un_nrows; row++) {
15540Sstevel@tonic-gate mdr = &un->un_row[row];
15550Sstevel@tonic-gate if ((mdr->un_ncomp + cmpcount) > ci)
15560Sstevel@tonic-gate break;
15570Sstevel@tonic-gate cmpcount += mdr->un_ncomp;
15580Sstevel@tonic-gate }
15590Sstevel@tonic-gate ASSERT(row != un->un_nrows);
15600Sstevel@tonic-gate
15610Sstevel@tonic-gate comp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
15620Sstevel@tonic-gate comp += ci;
15630Sstevel@tonic-gate /* Concatenations have a ncomp = 1 */
15640Sstevel@tonic-gate nd.nd_nblks = mdr->un_blocks / mdr->un_ncomp;
15650Sstevel@tonic-gate
15660Sstevel@tonic-gate if ((un->c.un_flag & MD_LABELED) && (ci == 0))
15670Sstevel@tonic-gate nd.nd_labeled = 1;
15680Sstevel@tonic-gate else
15690Sstevel@tonic-gate nd.nd_labeled = 0;
15700Sstevel@tonic-gate
15710Sstevel@tonic-gate again:
15720Sstevel@tonic-gate err = md_hot_spare_ifc(HS_GET, un->un_hsp_id, nd.nd_nblks,
15730Sstevel@tonic-gate nd.nd_labeled, &nd.nd_hs_id, &nd.nd_key, &nd.nd_dev,
15740Sstevel@tonic-gate &nd.nd_start_blk);
15750Sstevel@tonic-gate
15760Sstevel@tonic-gate if (err) {
15770Sstevel@tonic-gate if (!stripe_replace_dev(dev, junk, ci, NULL, recids, nrecids,
15780Sstevel@tonic-gate replace_done, replace_data)) {
15790Sstevel@tonic-gate mddb_commitrecs_wrapper(recids);
15800Sstevel@tonic-gate md_unit_writerexit(ui);
15810Sstevel@tonic-gate }
15820Sstevel@tonic-gate recids[0] = 0;
15830Sstevel@tonic-gate return (1);
15840Sstevel@tonic-gate }
15850Sstevel@tonic-gate
15860Sstevel@tonic-gate if (stripe_replace_dev(dev, junk, ci, &nd, recids, nrecids,
15877627SChris.Horne@Sun.COM replace_done, replace_data)) {
15880Sstevel@tonic-gate
15890Sstevel@tonic-gate (void) md_hot_spare_ifc(HS_BAD, un->un_hsp_id, 0, 0,
15900Sstevel@tonic-gate &nd.nd_hs_id, &nd.nd_key, NULL, NULL);
15910Sstevel@tonic-gate mddb_commitrec_wrapper(nd.nd_hs_id);
15920Sstevel@tonic-gate goto again;
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate
15950Sstevel@tonic-gate /* Leave a slot for the null recid */
15960Sstevel@tonic-gate for (i = 0; i < (nrecids - 1); i++) {
15970Sstevel@tonic-gate if (recids[i] == 0) {
15980Sstevel@tonic-gate recids[i++] = nd.nd_hs_id;
15990Sstevel@tonic-gate recids[i] = 0;
16000Sstevel@tonic-gate }
16010Sstevel@tonic-gate }
16020Sstevel@tonic-gate return (0);
16030Sstevel@tonic-gate }
16040Sstevel@tonic-gate
16050Sstevel@tonic-gate static int
stripe_imp_set(set_t setno)16060Sstevel@tonic-gate stripe_imp_set(
16070Sstevel@tonic-gate set_t setno
16080Sstevel@tonic-gate )
16090Sstevel@tonic-gate {
16100Sstevel@tonic-gate
16110Sstevel@tonic-gate mddb_recid_t recid;
16120Sstevel@tonic-gate int i, row, c, gotsomething;
16130Sstevel@tonic-gate mddb_type_t typ1;
16140Sstevel@tonic-gate mddb_de_ic_t *dep;
16150Sstevel@tonic-gate mddb_rb32_t *rbp;
16160Sstevel@tonic-gate ms_unit32_od_t *un32;
16170Sstevel@tonic-gate ms_unit_t *un64;
16181623Stw21770 md_dev64_t self_devt;
16190Sstevel@tonic-gate minor_t *self_id; /* minor needs to be updated */
16200Sstevel@tonic-gate md_parent_t *parent_id; /* parent needs to be updated */
16210Sstevel@tonic-gate mddb_recid_t *record_id; /* record id needs to be updated */
16220Sstevel@tonic-gate mddb_recid_t *hsp_id;
16230Sstevel@tonic-gate ms_comp32_od_t *comp32;
16240Sstevel@tonic-gate ms_comp_t *comp64;
16250Sstevel@tonic-gate
16260Sstevel@tonic-gate
16270Sstevel@tonic-gate gotsomething = 0;
16280Sstevel@tonic-gate
16290Sstevel@tonic-gate typ1 = (mddb_type_t)md_getshared_key(setno,
16300Sstevel@tonic-gate stripe_md_ops.md_driver.md_drivername);
16310Sstevel@tonic-gate recid = mddb_makerecid(setno, 0);
16320Sstevel@tonic-gate
16330Sstevel@tonic-gate while ((recid = mddb_getnextrec(recid, typ1, 0)) > 0) {
16340Sstevel@tonic-gate if (mddb_getrecprivate(recid) & MD_PRV_GOTIT)
16350Sstevel@tonic-gate continue;
16360Sstevel@tonic-gate
16370Sstevel@tonic-gate dep = mddb_getrecdep(recid);
16380Sstevel@tonic-gate rbp = dep->de_rb;
16390Sstevel@tonic-gate
16401623Stw21770 switch (rbp->rb_revision) {
16411623Stw21770 case MDDB_REV_RB:
16421623Stw21770 case MDDB_REV_RBFN:
16430Sstevel@tonic-gate /*
16440Sstevel@tonic-gate * Small device
16450Sstevel@tonic-gate */
16460Sstevel@tonic-gate un32 = (ms_unit32_od_t *)mddb_getrecaddr(recid);
16470Sstevel@tonic-gate self_id = &(un32->c.un_self_id);
16480Sstevel@tonic-gate parent_id = &(un32->c.un_parent);
16490Sstevel@tonic-gate record_id = &(un32->c.un_record_id);
16500Sstevel@tonic-gate hsp_id = &(un32->un_hsp_id);
16510Sstevel@tonic-gate
16527627SChris.Horne@Sun.COM comp32 = (ms_comp32_od_t *)
16537627SChris.Horne@Sun.COM ((void *)&((char *)un32)[un32->un_ocomp]);
16540Sstevel@tonic-gate for (row = 0; row < un32->un_nrows; row++) {
16557627SChris.Horne@Sun.COM struct ms_row32_od *mdr = &un32->un_row[row];
16567627SChris.Horne@Sun.COM for (i = 0, c = mdr->un_icomp;
16577627SChris.Horne@Sun.COM i < mdr->un_ncomp; i++) {
16587627SChris.Horne@Sun.COM ms_comp32_od_t *mdc;
16597627SChris.Horne@Sun.COM
16607627SChris.Horne@Sun.COM mdc = &comp32[c++];
16610Sstevel@tonic-gate
16627627SChris.Horne@Sun.COM if (!md_update_minor(setno,
16637627SChris.Horne@Sun.COM mddb_getsidenum(setno),
16647627SChris.Horne@Sun.COM mdc->un_key))
16657627SChris.Horne@Sun.COM goto out;
16660Sstevel@tonic-gate
16677627SChris.Horne@Sun.COM if (mdc->un_mirror.ms_hs_id != 0)
16687627SChris.Horne@Sun.COM mdc->un_mirror.ms_hs_id =
16697627SChris.Horne@Sun.COM MAKERECID(setno,
16707627SChris.Horne@Sun.COM mdc->un_mirror.ms_hs_id);
16717627SChris.Horne@Sun.COM }
16720Sstevel@tonic-gate }
16731623Stw21770 break;
16741623Stw21770 case MDDB_REV_RB64:
16751623Stw21770 case MDDB_REV_RB64FN:
16760Sstevel@tonic-gate un64 = (ms_unit_t *)mddb_getrecaddr(recid);
16770Sstevel@tonic-gate self_id = &(un64->c.un_self_id);
16780Sstevel@tonic-gate parent_id = &(un64->c.un_parent);
16790Sstevel@tonic-gate record_id = &(un64->c.un_record_id);
16800Sstevel@tonic-gate hsp_id = &(un64->un_hsp_id);
16810Sstevel@tonic-gate
16827627SChris.Horne@Sun.COM comp64 = (ms_comp_t *)
16837627SChris.Horne@Sun.COM ((void *)&((char *)un64)[un64->un_ocomp]);
16840Sstevel@tonic-gate for (row = 0; row < un64->un_nrows; row++) {
16857627SChris.Horne@Sun.COM struct ms_row *mdr = &un64->un_row[row];
16867627SChris.Horne@Sun.COM
16877627SChris.Horne@Sun.COM for (i = 0, c = mdr->un_icomp;
16887627SChris.Horne@Sun.COM i < mdr->un_ncomp; i++) {
16897627SChris.Horne@Sun.COM ms_comp_t *mdc;
16907627SChris.Horne@Sun.COM
16917627SChris.Horne@Sun.COM mdc = &comp64[c++];
16920Sstevel@tonic-gate
16937627SChris.Horne@Sun.COM if (!md_update_minor(setno,
16947627SChris.Horne@Sun.COM mddb_getsidenum(setno),
16957627SChris.Horne@Sun.COM mdc->un_key))
16967627SChris.Horne@Sun.COM goto out;
16970Sstevel@tonic-gate
16987627SChris.Horne@Sun.COM if (mdc->un_mirror.ms_hs_id != 0)
16997627SChris.Horne@Sun.COM mdc->un_mirror.ms_hs_id =
17007627SChris.Horne@Sun.COM MAKERECID(setno,
17017627SChris.Horne@Sun.COM mdc->un_mirror.ms_hs_id);
17027627SChris.Horne@Sun.COM }
17030Sstevel@tonic-gate }
17041623Stw21770 break;
17051623Stw21770 }
17061623Stw21770
17071623Stw21770 /*
17081623Stw21770 * If this is a top level and a friendly name metadevice,
17091623Stw21770 * update its minor in the namespace.
17101623Stw21770 */
17111623Stw21770 if ((*parent_id == MD_NO_PARENT) &&
17121623Stw21770 ((rbp->rb_revision == MDDB_REV_RBFN) ||
17131623Stw21770 (rbp->rb_revision == MDDB_REV_RB64FN))) {
17141623Stw21770
17151623Stw21770 self_devt = md_makedevice(md_major, *self_id);
17161623Stw21770 if (!md_update_top_device_minor(setno,
17171623Stw21770 mddb_getsidenum(setno), self_devt))
17181623Stw21770 goto out;
17190Sstevel@tonic-gate }
17200Sstevel@tonic-gate
17210Sstevel@tonic-gate /*
17220Sstevel@tonic-gate * Update unit with the imported setno
17230Sstevel@tonic-gate *
17240Sstevel@tonic-gate */
17250Sstevel@tonic-gate mddb_setrecprivate(recid, MD_PRV_GOTIT);
17260Sstevel@tonic-gate
17270Sstevel@tonic-gate *self_id = MD_MKMIN(setno, MD_MIN2UNIT(*self_id));
17280Sstevel@tonic-gate
17290Sstevel@tonic-gate if (*hsp_id != -1)
17300Sstevel@tonic-gate *hsp_id = MAKERECID(setno, DBID(*hsp_id));
17310Sstevel@tonic-gate
17320Sstevel@tonic-gate if (*parent_id != MD_NO_PARENT)
17330Sstevel@tonic-gate *parent_id = MD_MKMIN(setno, MD_MIN2UNIT(*parent_id));
17340Sstevel@tonic-gate *record_id = MAKERECID(setno, DBID(*record_id));
17350Sstevel@tonic-gate
17360Sstevel@tonic-gate gotsomething = 1;
17370Sstevel@tonic-gate }
17380Sstevel@tonic-gate
17390Sstevel@tonic-gate out:
17400Sstevel@tonic-gate return (gotsomething);
17410Sstevel@tonic-gate }
17420Sstevel@tonic-gate
17430Sstevel@tonic-gate static md_named_services_t stripe_named_services[] = {
17440Sstevel@tonic-gate {stripe_shared_by_blk, "shared by blk" },
17450Sstevel@tonic-gate {stripe_shared_by_indx, "shared by indx" },
17460Sstevel@tonic-gate {stripe_component_count, "get component count" },
17470Sstevel@tonic-gate {stripe_block_count_skip_size, "get block count skip size" },
17480Sstevel@tonic-gate {stripe_get_dev, "get device" },
17490Sstevel@tonic-gate {stripe_replace_dev, "replace device" },
17500Sstevel@tonic-gate {stripe_hotspare_dev, "hotspare device" },
17510Sstevel@tonic-gate {stripe_rename_check, MDRNM_CHECK },
17520Sstevel@tonic-gate {NULL, 0}
17530Sstevel@tonic-gate };
17540Sstevel@tonic-gate
17550Sstevel@tonic-gate md_ops_t stripe_md_ops = {
17560Sstevel@tonic-gate stripe_open, /* open */
17570Sstevel@tonic-gate stripe_close, /* close */
17580Sstevel@tonic-gate md_stripe_strategy, /* strategy */
17590Sstevel@tonic-gate NULL, /* print */
17600Sstevel@tonic-gate stripe_dump, /* dump */
17610Sstevel@tonic-gate NULL, /* read */
17620Sstevel@tonic-gate NULL, /* write */
17630Sstevel@tonic-gate md_stripe_ioctl, /* stripe_ioctl, */
17640Sstevel@tonic-gate stripe_snarf, /* stripe_snarf */
17650Sstevel@tonic-gate stripe_halt, /* stripe_halt */
17660Sstevel@tonic-gate NULL, /* aread */
17670Sstevel@tonic-gate NULL, /* awrite */
17680Sstevel@tonic-gate stripe_imp_set, /* import set */
17690Sstevel@tonic-gate stripe_named_services
17700Sstevel@tonic-gate };
17710Sstevel@tonic-gate
17720Sstevel@tonic-gate static void
init_init()17730Sstevel@tonic-gate init_init()
17740Sstevel@tonic-gate {
17750Sstevel@tonic-gate md_stripe_mcs_buf_off = sizeof (md_scs_t) - sizeof (buf_t);
17760Sstevel@tonic-gate
17770Sstevel@tonic-gate stripe_parent_cache = kmem_cache_create("md_stripe_parent",
17780Sstevel@tonic-gate sizeof (md_sps_t), 0, stripe_parent_constructor,
17790Sstevel@tonic-gate stripe_parent_destructor, stripe_run_queue, NULL, NULL,
17800Sstevel@tonic-gate 0);
17810Sstevel@tonic-gate stripe_child_cache = kmem_cache_create("md_stripe_child",
17820Sstevel@tonic-gate sizeof (md_scs_t) - sizeof (buf_t) + biosize(), 0,
17830Sstevel@tonic-gate stripe_child_constructor, stripe_child_destructor,
17840Sstevel@tonic-gate stripe_run_queue, NULL, NULL, 0);
17850Sstevel@tonic-gate }
17860Sstevel@tonic-gate
17870Sstevel@tonic-gate static void
fini_uninit()17880Sstevel@tonic-gate fini_uninit()
17890Sstevel@tonic-gate {
17900Sstevel@tonic-gate kmem_cache_destroy(stripe_parent_cache);
17910Sstevel@tonic-gate kmem_cache_destroy(stripe_child_cache);
17920Sstevel@tonic-gate stripe_parent_cache = stripe_child_cache = NULL;
17930Sstevel@tonic-gate }
17940Sstevel@tonic-gate
17950Sstevel@tonic-gate /* define the module linkage */
17964932Spetede MD_PLUGIN_MISC_MODULE("stripes module", init_init(), fini_uninit())
1797