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  */
210Sstevel@tonic-gate /*
221366Spetede  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Soft partitioning metadevice driver (md_sp).
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * This file contains the primary operations of the soft partitioning
320Sstevel@tonic-gate  * metadevice driver.  This includes all routines for normal operation
330Sstevel@tonic-gate  * (open/close/read/write).  Please see mdvar.h for a definition of
340Sstevel@tonic-gate  * metadevice operations vector (md_ops_t).  This driver is loosely
350Sstevel@tonic-gate  * based on the stripe driver (md_stripe).
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * All metadevice administration is done through the use of ioctl's.
380Sstevel@tonic-gate  * As such, all administrative routines appear in sp_ioctl.c.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * Soft partitions are represented both in-core and in the metadb with a
410Sstevel@tonic-gate  * unit structure.  The soft partition-specific information in the unit
420Sstevel@tonic-gate  * structure includes the following information:
430Sstevel@tonic-gate  *	- Device information (md_dev64_t & md key) about the device on which
440Sstevel@tonic-gate  *	  the soft partition is built.
450Sstevel@tonic-gate  *	- Soft partition status information.
460Sstevel@tonic-gate  *	- The size of the soft partition and number of extents used to
470Sstevel@tonic-gate  *	  make up that size.
480Sstevel@tonic-gate  *	- An array of exents which define virtual/physical offset
490Sstevel@tonic-gate  *	  mappings and lengths for each extent.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * Typical soft partition operation proceeds as follows:
520Sstevel@tonic-gate  *	- The unit structure is fetched from the metadb and placed into
530Sstevel@tonic-gate  *	  an in-core array (as with other metadevices).  This operation
540Sstevel@tonic-gate  *	  is performed via sp_build_incore( ) and takes place during
550Sstevel@tonic-gate  *	  "snarfing" (when all metadevices are brought in-core at
560Sstevel@tonic-gate  *	  once) and when a new soft partition is created.
570Sstevel@tonic-gate  *	- A soft partition is opened via sp_open( ).  At open time the
580Sstevel@tonic-gate  *	  the soft partition unit structure is verified with the soft
590Sstevel@tonic-gate  *	  partition on-disk structures.  Additionally, the soft partition
600Sstevel@tonic-gate  *	  status is checked (only soft partitions in the OK state may be
610Sstevel@tonic-gate  *	  opened).
620Sstevel@tonic-gate  *	- Soft partition I/O is performed via sp_strategy( ) which relies on
630Sstevel@tonic-gate  *	  a support routine, sp_mapbuf( ), to do most of the work.
640Sstevel@tonic-gate  *	  sp_mapbuf( ) maps a buffer to a particular extent via a binary
650Sstevel@tonic-gate  *	  search of the extent array in the soft partition unit structure.
660Sstevel@tonic-gate  *	  Once a translation has been performed, the I/O is passed down
670Sstevel@tonic-gate  *	  to the next layer, which may be another metadevice or a physical
680Sstevel@tonic-gate  *	  disk.  Since a soft partition may contain multiple, non-contiguous
690Sstevel@tonic-gate  *	  extents, a single I/O may have to be fragmented.
700Sstevel@tonic-gate  *	- Soft partitions are closed using sp_close.
710Sstevel@tonic-gate  *
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #include <sys/param.h>
750Sstevel@tonic-gate #include <sys/systm.h>
760Sstevel@tonic-gate #include <sys/conf.h>
770Sstevel@tonic-gate #include <sys/file.h>
780Sstevel@tonic-gate #include <sys/user.h>
790Sstevel@tonic-gate #include <sys/uio.h>
800Sstevel@tonic-gate #include <sys/t_lock.h>
810Sstevel@tonic-gate #include <sys/buf.h>
820Sstevel@tonic-gate #include <sys/dkio.h>
830Sstevel@tonic-gate #include <sys/vtoc.h>
840Sstevel@tonic-gate #include <sys/kmem.h>
850Sstevel@tonic-gate #include <vm/page.h>
860Sstevel@tonic-gate #include <sys/cmn_err.h>
870Sstevel@tonic-gate #include <sys/sysmacros.h>
880Sstevel@tonic-gate #include <sys/types.h>
890Sstevel@tonic-gate #include <sys/mkdev.h>
900Sstevel@tonic-gate #include <sys/stat.h>
910Sstevel@tonic-gate #include <sys/open.h>
920Sstevel@tonic-gate #include <sys/lvm/mdvar.h>
930Sstevel@tonic-gate #include <sys/lvm/md_sp.h>
940Sstevel@tonic-gate #include <sys/lvm/md_convert.h>
950Sstevel@tonic-gate #include <sys/lvm/md_notify.h>
960Sstevel@tonic-gate #include <sys/lvm/md_crc.h>
970Sstevel@tonic-gate #include <sys/modctl.h>
980Sstevel@tonic-gate #include <sys/ddi.h>
990Sstevel@tonic-gate #include <sys/sunddi.h>
1000Sstevel@tonic-gate #include <sys/debug.h>
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
1030Sstevel@tonic-gate #include <sys/sysevent/svm.h>
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate md_ops_t		sp_md_ops;
1060Sstevel@tonic-gate #ifndef	lint
1071366Spetede char			_depends_on[] = "drv/md";
1080Sstevel@tonic-gate md_ops_t		*md_interface_ops = &sp_md_ops;
1090Sstevel@tonic-gate #endif
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate extern unit_t		md_nunits;
1120Sstevel@tonic-gate extern set_t		md_nsets;
1130Sstevel@tonic-gate extern md_set_t		md_set[];
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate extern int		md_status;
1160Sstevel@tonic-gate extern major_t		md_major;
1170Sstevel@tonic-gate extern mdq_anchor_t	md_done_daemon;
1180Sstevel@tonic-gate extern mdq_anchor_t	md_sp_daemon;
1190Sstevel@tonic-gate extern kmutex_t		md_mx;
1200Sstevel@tonic-gate extern kcondvar_t	md_cv;
1210Sstevel@tonic-gate extern md_krwlock_t	md_unit_array_rw;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate static kmem_cache_t	*sp_parent_cache = NULL;
1240Sstevel@tonic-gate static kmem_cache_t	*sp_child_cache = NULL;
1250Sstevel@tonic-gate static void		sp_send_stat_ok(mp_unit_t *);
1260Sstevel@tonic-gate static void		sp_send_stat_err(mp_unit_t *);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * FUNCTION:	sp_parent_constructor()
1300Sstevel@tonic-gate  * INPUT:	none.
1310Sstevel@tonic-gate  * OUTPUT:	ps	- parent save structure initialized.
1320Sstevel@tonic-gate  * RETURNS:	void *	- ptr to initialized parent save structure.
1330Sstevel@tonic-gate  * PURPOSE:	initialize parent save structure.
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate /*ARGSUSED1*/
1360Sstevel@tonic-gate static int
1370Sstevel@tonic-gate sp_parent_constructor(void *p, void *d1, int d2)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	mutex_init(&((md_spps_t *)p)->ps_mx,
1400Sstevel@tonic-gate 	    NULL, MUTEX_DEFAULT, NULL);
1410Sstevel@tonic-gate 	return (0);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static void
1450Sstevel@tonic-gate sp_parent_init(md_spps_t *ps)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	bzero(ps, offsetof(md_spps_t, ps_mx));
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*ARGSUSED1*/
1510Sstevel@tonic-gate static void
1520Sstevel@tonic-gate sp_parent_destructor(void *p, void *d)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	mutex_destroy(&((md_spps_t *)p)->ps_mx);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * FUNCTION:	sp_child_constructor()
1590Sstevel@tonic-gate  * INPUT:	none.
1600Sstevel@tonic-gate  * OUTPUT:	cs	- child save structure initialized.
1610Sstevel@tonic-gate  * RETURNS:	void *	- ptr to initialized child save structure.
1620Sstevel@tonic-gate  * PURPOSE:	initialize child save structure.
1630Sstevel@tonic-gate  */
1640Sstevel@tonic-gate /*ARGSUSED1*/
1650Sstevel@tonic-gate static int
1660Sstevel@tonic-gate sp_child_constructor(void *p, void *d1, int d2)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	bioinit(&((md_spcs_t *)p)->cs_buf);
1690Sstevel@tonic-gate 	return (0);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate static void
1730Sstevel@tonic-gate sp_child_init(md_spcs_t *cs)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	cs->cs_mdunit = 0;
1760Sstevel@tonic-gate 	cs->cs_ps = NULL;
1770Sstevel@tonic-gate 	md_bioreset(&cs->cs_buf);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*ARGSUSED1*/
1810Sstevel@tonic-gate static void
1820Sstevel@tonic-gate sp_child_destructor(void *p, void *d)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	biofini(&((md_spcs_t *)p)->cs_buf);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate  * FUNCTION:	sp_run_queue()
1890Sstevel@tonic-gate  * INPUT:	none.
1900Sstevel@tonic-gate  * OUTPUT:	none.
1910Sstevel@tonic-gate  * RETURNS:	void.
1920Sstevel@tonic-gate  * PURPOSE:	run the md_daemon to clean up memory pool.
1930Sstevel@tonic-gate  */
1940Sstevel@tonic-gate /*ARGSUSED*/
1950Sstevel@tonic-gate static void
1960Sstevel@tonic-gate sp_run_queue(void *d)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	if (!(md_status & MD_GBL_DAEMONS_LIVE))
1990Sstevel@tonic-gate 		md_daemon(1, &md_done_daemon);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate  * FUNCTION:	sp_build_incore()
2050Sstevel@tonic-gate  * INPUT:	p		- ptr to unit structure.
2060Sstevel@tonic-gate  *		snarfing	- flag to tell us we are snarfing.
2070Sstevel@tonic-gate  * OUTPUT:	non.
2080Sstevel@tonic-gate  * RETURNS:	int	- 0 (always).
2090Sstevel@tonic-gate  * PURPOSE:	place unit structure into in-core unit array (keyed from
2100Sstevel@tonic-gate  *		minor number).
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate int
2130Sstevel@tonic-gate sp_build_incore(void *p, int snarfing)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	mp_unit_t	*un = (mp_unit_t *)p;
2160Sstevel@tonic-gate 	minor_t		mnum;
2170Sstevel@tonic-gate 	set_t		setno;
2180Sstevel@tonic-gate 	md_dev64_t	tmpdev;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	mnum = MD_SID(un);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	if (MD_UNIT(mnum) != NULL)
2230Sstevel@tonic-gate 		return (0);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	MD_STATUS(un) = 0;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (snarfing) {
2280Sstevel@tonic-gate 		/*
2290Sstevel@tonic-gate 		 * if we are snarfing, we get the device information
2300Sstevel@tonic-gate 		 * from the metadb record (using the metadb key for
2310Sstevel@tonic-gate 		 * that device).
2320Sstevel@tonic-gate 		 */
2330Sstevel@tonic-gate 		setno = MD_MIN2SET(mnum);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		tmpdev = md_getdevnum(setno, mddb_getsidenum(setno),
2360Sstevel@tonic-gate 		    un->un_key, MD_NOTRUST_DEVT);
2370Sstevel@tonic-gate 		un->un_dev = tmpdev;
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/* place unit in in-core array */
2410Sstevel@tonic-gate 	MD_UNIT(mnum) = un;
2420Sstevel@tonic-gate 	return (0);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * FUNCTION:	reset_sp()
2470Sstevel@tonic-gate  * INPUT:	un		- unit structure to be reset/removed.
2480Sstevel@tonic-gate  *		mnum		- minor number to be reset/removed.
2490Sstevel@tonic-gate  *		removing	- flag to tell us if we are removing
2500Sstevel@tonic-gate  *				  permanently or just reseting in-core
2510Sstevel@tonic-gate  *				  structures.
2520Sstevel@tonic-gate  * OUTPUT:	none.
2530Sstevel@tonic-gate  * RETURNS:	void.
2540Sstevel@tonic-gate  * PURPOSE:	used to either simply reset in-core structures or to
2550Sstevel@tonic-gate  *		permanently remove metadevices from the metadb.
2560Sstevel@tonic-gate  */
2570Sstevel@tonic-gate void
2580Sstevel@tonic-gate reset_sp(mp_unit_t *un, minor_t mnum, int removing)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	sv_dev_t	*sv;
2610Sstevel@tonic-gate 	mddb_recid_t	vtoc_id;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	/* clean up in-core structures */
2640Sstevel@tonic-gate 	md_destroy_unit_incore(mnum, &sp_md_ops);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	MD_UNIT(mnum) = NULL;
2670Sstevel@tonic-gate 
2681623Stw21770 	/*
2691623Stw21770 	 * Attempt release of minor node
2701623Stw21770 	 */
271*2077Stw21770 	md_remove_minor_node(mnum);
2721623Stw21770 
2730Sstevel@tonic-gate 	if (!removing)
2740Sstevel@tonic-gate 		return;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/* we are removing the soft partition from the metadb */
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/*
2790Sstevel@tonic-gate 	 * Save off device information so we can get to
2800Sstevel@tonic-gate 	 * it after we do the mddb_deleterec().
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 	sv = (sv_dev_t *)kmem_alloc(sizeof (sv_dev_t), KM_SLEEP);
2830Sstevel@tonic-gate 	sv->setno = MD_MIN2SET(mnum);
2840Sstevel@tonic-gate 	sv->key = un->un_key;
2850Sstevel@tonic-gate 	vtoc_id = un->c.un_vtoc_id;
2860Sstevel@tonic-gate 
2871623Stw21770 	/*
2881623Stw21770 	 * Remove self from the namespace
2891623Stw21770 	 */
2901623Stw21770 	if (un->c.un_revision & MD_FN_META_DEV) {
2911623Stw21770 		(void) md_rem_selfname(un->c.un_self_id);
2921623Stw21770 	}
2931623Stw21770 
2940Sstevel@tonic-gate 	/* Remove the unit structure */
2950Sstevel@tonic-gate 	mddb_deleterec_wrapper(un->c.un_record_id);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	if (vtoc_id)
2980Sstevel@tonic-gate 		mddb_deleterec_wrapper(vtoc_id);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_DELETE, TAG_METADEVICE,
3010Sstevel@tonic-gate 	    MD_MIN2SET(mnum), MD_MIN2UNIT(mnum));
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/*
3040Sstevel@tonic-gate 	 * remove the underlying device name from the metadb.  if other
3050Sstevel@tonic-gate 	 * soft partitions are built on this device, this will simply
3060Sstevel@tonic-gate 	 * decrease the reference count for this device.  otherwise the
3070Sstevel@tonic-gate 	 * name record for this device will be removed from the metadb.
3080Sstevel@tonic-gate 	 */
3090Sstevel@tonic-gate 	md_rem_names(sv, 1);
3100Sstevel@tonic-gate 	kmem_free(sv, sizeof (sv_dev_t));
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate  * FUNCTION:	sp_send_stat_msg
3150Sstevel@tonic-gate  * INPUT:	un	- unit reference
3160Sstevel@tonic-gate  *		status	- status to be sent to master node
3170Sstevel@tonic-gate  *			MD_SP_OK - soft-partition is now OK
3180Sstevel@tonic-gate  *			MD_SP_ERR	"	"	 errored
3190Sstevel@tonic-gate  * OUTPUT:	none.
3200Sstevel@tonic-gate  * RETURNS:	void.
3210Sstevel@tonic-gate  * PURPOSE:	send a soft-partition status change to the master node. If the
3220Sstevel@tonic-gate  *		message succeeds we simply return. If it fails we panic as the
3230Sstevel@tonic-gate  *		cluster-wide view of the metadevices is now inconsistent.
3240Sstevel@tonic-gate  * CALLING CONTEXT:
3250Sstevel@tonic-gate  *	Blockable. No locks can be held.
3260Sstevel@tonic-gate  */
3270Sstevel@tonic-gate static void
3280Sstevel@tonic-gate sp_send_stat_msg(mp_unit_t *un, sp_status_t status)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	md_mn_msg_sp_setstat_t	sp_msg;
3310Sstevel@tonic-gate 	md_mn_kresult_t	*kres;
3320Sstevel@tonic-gate 	set_t		setno = MD_UN2SET(un);
3330Sstevel@tonic-gate 	int		rval;
3340Sstevel@tonic-gate 	const char	*str = (status == MD_SP_ERR) ? "MD_SP_ERR" : "MD_SP_OK";
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	sp_msg.sp_setstat_mnum = MD_SID(un);
3370Sstevel@tonic-gate 	sp_msg.sp_setstat_status = status;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	kres = kmem_alloc(sizeof (md_mn_kresult_t), KM_SLEEP);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	rval = mdmn_ksend_message(setno, MD_MN_MSG_SP_SETSTAT2, MD_MSGF_NO_LOG,
3420Sstevel@tonic-gate 	    (char *)&sp_msg, sizeof (sp_msg), kres);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	if (!MDMN_KSEND_MSG_OK(rval, kres)) {
3450Sstevel@tonic-gate 		mdmn_ksend_show_error(rval, kres, "MD_MN_MSG_SP_SETSTAT2");
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		/*
3480Sstevel@tonic-gate 		 * Panic as we are now in an inconsistent state.
3490Sstevel@tonic-gate 		 */
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		cmn_err(CE_PANIC, "md: %s: %s could not be set on all nodes\n",
3520Sstevel@tonic-gate 		    md_shortname(MD_SID(un)), str);
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	kmem_free(kres, sizeof (md_mn_kresult_t));
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate  * FUNCTION:	sp_finish_error
3600Sstevel@tonic-gate  * INPUT:	ps	- parent save structure for error-ed I/O.
3610Sstevel@tonic-gate  *		lock_held	- set if the unit readerlock is held
3620Sstevel@tonic-gate  * OUTPUT:	none.
3630Sstevel@tonic-gate  * RETURNS:	void.
3640Sstevel@tonic-gate  * PURPOSE:	report a driver error
3650Sstevel@tonic-gate  */
3660Sstevel@tonic-gate static void
3670Sstevel@tonic-gate sp_finish_error(md_spps_t *ps, int lock_held)
3680Sstevel@tonic-gate {
3690Sstevel@tonic-gate 	struct buf	*pb = ps->ps_bp;
3700Sstevel@tonic-gate 	mdi_unit_t	*ui = ps->ps_ui;
3710Sstevel@tonic-gate 	md_dev64_t	un_dev;			/* underlying device */
3720Sstevel@tonic-gate 	md_dev64_t	md_dev = md_expldev(pb->b_edev); /* metadev in error */
3730Sstevel@tonic-gate 	char		*str;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	un_dev = md_expldev(ps->ps_un->un_dev);
3760Sstevel@tonic-gate 	/* set error type */
3770Sstevel@tonic-gate 	if (pb->b_flags & B_READ) {
3780Sstevel@tonic-gate 		str = "read";
3790Sstevel@tonic-gate 	} else {
3800Sstevel@tonic-gate 		str = "write";
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	SPPS_FREE(sp_parent_cache, ps);
3850Sstevel@tonic-gate 	pb->b_flags |= B_ERROR;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	md_kstat_done(ui, pb, 0);
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (lock_held) {
3900Sstevel@tonic-gate 		md_unit_readerexit(ui);
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 	md_biodone(pb);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	cmn_err(CE_WARN, "md: %s: %s error on %s",
3950Sstevel@tonic-gate 	    md_shortname(md_getminor(md_dev)), str,
3960Sstevel@tonic-gate 	    md_devname(MD_DEV2SET(md_dev), un_dev, NULL, 0));
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate /*
4010Sstevel@tonic-gate  * FUNCTION:	sp_xmit_ok
4020Sstevel@tonic-gate  * INPUT:	dq	- daemon queue referencing failing ps structure
4030Sstevel@tonic-gate  * OUTPUT:	none.
4040Sstevel@tonic-gate  * RETURNS:	void.
4050Sstevel@tonic-gate  * PURPOSE:	send a message to the master node in a multi-owner diskset to
4060Sstevel@tonic-gate  *		update all attached nodes view of the soft-part to be MD_SP_OK.
4070Sstevel@tonic-gate  * CALLING CONTEXT:
4080Sstevel@tonic-gate  *	Blockable. No unit lock held.
4090Sstevel@tonic-gate  */
4100Sstevel@tonic-gate static void
4110Sstevel@tonic-gate sp_xmit_ok(daemon_queue_t *dq)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	md_spps_t	*ps = (md_spps_t *)dq;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	/* Send a MD_MN_MSG_SP_SETSTAT to the master */
4160Sstevel@tonic-gate 	sp_send_stat_msg(ps->ps_un, MD_SP_OK);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	/*
4190Sstevel@tonic-gate 	 * Successfully transmitted error state to all nodes, now release this
4200Sstevel@tonic-gate 	 * parent structure.
4210Sstevel@tonic-gate 	 */
4220Sstevel@tonic-gate 	SPPS_FREE(sp_parent_cache, ps);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate /*
4260Sstevel@tonic-gate  * FUNCTION:	sp_xmit_error
4270Sstevel@tonic-gate  * INPUT:	dq	- daemon queue referencing failing ps structure
4280Sstevel@tonic-gate  * OUTPUT:	none.
4290Sstevel@tonic-gate  * RETURNS:	void.
4300Sstevel@tonic-gate  * PURPOSE:	send a message to the master node in a multi-owner diskset to
4310Sstevel@tonic-gate  *		update all attached nodes view of the soft-part to be MD_SP_ERR.
4320Sstevel@tonic-gate  * CALLING CONTEXT:
4330Sstevel@tonic-gate  *	Blockable. No unit lock held.
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate static void
4360Sstevel@tonic-gate sp_xmit_error(daemon_queue_t *dq)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate 	md_spps_t	*ps = (md_spps_t *)dq;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	/* Send a MD_MN_MSG_SP_SETSTAT to the master */
4410Sstevel@tonic-gate 	sp_send_stat_msg(ps->ps_un, MD_SP_ERR);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/*
4440Sstevel@tonic-gate 	 * Successfully transmitted error state to all nodes, now release this
4450Sstevel@tonic-gate 	 * parent structure.
4460Sstevel@tonic-gate 	 */
4470Sstevel@tonic-gate 	SPPS_FREE(sp_parent_cache, ps);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate static void
4500Sstevel@tonic-gate sp_send_stat_ok(mp_unit_t *un)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	minor_t		mnum = MD_SID(un);
4530Sstevel@tonic-gate 	md_spps_t	*ps;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	ps = kmem_cache_alloc(sp_parent_cache, MD_ALLOCFLAGS);
4560Sstevel@tonic-gate 	sp_parent_init(ps);
4570Sstevel@tonic-gate 	ps->ps_un = un;
4580Sstevel@tonic-gate 	ps->ps_ui = MDI_UNIT(mnum);
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	daemon_request(&md_sp_daemon, sp_xmit_ok, (daemon_queue_t *)ps,
4610Sstevel@tonic-gate 	REQ_OLD);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate static void
4650Sstevel@tonic-gate sp_send_stat_err(mp_unit_t *un)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	minor_t		mnum = MD_SID(un);
4680Sstevel@tonic-gate 	md_spps_t	*ps;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	ps = kmem_cache_alloc(sp_parent_cache, MD_ALLOCFLAGS);
4710Sstevel@tonic-gate 	sp_parent_init(ps);
4720Sstevel@tonic-gate 	ps->ps_un = un;
4730Sstevel@tonic-gate 	ps->ps_ui = MDI_UNIT(mnum);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	daemon_request(&md_sp_daemon, sp_xmit_error, (daemon_queue_t *)ps,
4760Sstevel@tonic-gate 	REQ_OLD);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate /*
4810Sstevel@tonic-gate  * FUNCTION:	sp_error()
4820Sstevel@tonic-gate  * INPUT:	ps	- parent save structure for error-ed I/O.
4830Sstevel@tonic-gate  * OUTPUT:	none.
4840Sstevel@tonic-gate  * RETURNS:	void.
4850Sstevel@tonic-gate  * PURPOSE:	report a driver error.
4860Sstevel@tonic-gate  * CALLING CONTEXT:
4870Sstevel@tonic-gate  *	Interrupt - non-blockable
4880Sstevel@tonic-gate  */
4890Sstevel@tonic-gate static void
4900Sstevel@tonic-gate sp_error(md_spps_t *ps)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	set_t		setno = MD_UN2SET(ps->ps_un);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	/*
4950Sstevel@tonic-gate 	 * Drop the mutex associated with this request before (potentially)
4960Sstevel@tonic-gate 	 * enqueuing the free onto a separate thread. We have to release the
4970Sstevel@tonic-gate 	 * mutex before destroying the parent structure.
4980Sstevel@tonic-gate 	 */
4990Sstevel@tonic-gate 	if (!(ps->ps_flags & MD_SPPS_DONTFREE)) {
5000Sstevel@tonic-gate 		if (MUTEX_HELD(&ps->ps_mx)) {
5010Sstevel@tonic-gate 			mutex_exit(&ps->ps_mx);
5020Sstevel@tonic-gate 		}
5030Sstevel@tonic-gate 	} else {
5040Sstevel@tonic-gate 		/*
5050Sstevel@tonic-gate 		 * this should only ever happen if we are panicking,
5060Sstevel@tonic-gate 		 * since DONTFREE is only set on the parent if panicstr
5070Sstevel@tonic-gate 		 * is non-NULL.
5080Sstevel@tonic-gate 		 */
5090Sstevel@tonic-gate 		ASSERT(panicstr);
5100Sstevel@tonic-gate 	}
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * For a multi-owner set we need to send a message to the master so that
5140Sstevel@tonic-gate 	 * all nodes get the errored status when we first encounter it. To avoid
5150Sstevel@tonic-gate 	 * deadlocking when multiple soft-partitions encounter an error on one
5160Sstevel@tonic-gate 	 * physical unit we drop the unit readerlock before enqueueing the
5170Sstevel@tonic-gate 	 * request. That way we can service any messages that require a
5180Sstevel@tonic-gate 	 * writerlock to be held. Additionally, to avoid deadlocking when at
5190Sstevel@tonic-gate 	 * the bottom of a metadevice stack and a higher level mirror has
5200Sstevel@tonic-gate 	 * multiple requests outstanding on this soft-part, we clone the ps
5210Sstevel@tonic-gate 	 * that failed and pass the error back up the stack to release the
5220Sstevel@tonic-gate 	 * reference that this i/o may have in the higher-level metadevice.
5230Sstevel@tonic-gate 	 * The other nodes in the cluster just have to modify the soft-part
5240Sstevel@tonic-gate 	 * status and we do not need to block the i/o completion for this.
5250Sstevel@tonic-gate 	 */
5260Sstevel@tonic-gate 	if (MD_MNSET_SETNO(setno)) {
5270Sstevel@tonic-gate 		md_spps_t	*err_ps;
5280Sstevel@tonic-gate 		err_ps = kmem_cache_alloc(sp_parent_cache, MD_ALLOCFLAGS);
5290Sstevel@tonic-gate 		sp_parent_init(err_ps);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 		err_ps->ps_un = ps->ps_un;
5320Sstevel@tonic-gate 		err_ps->ps_ui = ps->ps_ui;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 		md_unit_readerexit(ps->ps_ui);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 		daemon_request(&md_sp_daemon, sp_xmit_error,
5370Sstevel@tonic-gate 		    (daemon_queue_t *)err_ps, REQ_OLD);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		sp_finish_error(ps, 0);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 		return;
5420Sstevel@tonic-gate 	} else {
5430Sstevel@tonic-gate 		ps->ps_un->un_status = MD_SP_ERR;
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	/* Flag the error */
5470Sstevel@tonic-gate 	sp_finish_error(ps, 1);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate /*
5520Sstevel@tonic-gate  * FUNCTION:	sp_mapbuf()
5530Sstevel@tonic-gate  * INPUT:	un	- unit structure for soft partition we are doing
5540Sstevel@tonic-gate  *			  I/O on.
5550Sstevel@tonic-gate  *		voff	- virtual offset in soft partition to map.
5560Sstevel@tonic-gate  *		bcount	- # of blocks in the I/O.
5570Sstevel@tonic-gate  * OUTPUT:	bp	- translated buffer to be passed down to next layer.
5580Sstevel@tonic-gate  * RETURNS:	1	- request must be fragmented, more work to do,
5590Sstevel@tonic-gate  *		0	- request satisified, no more work to do
5600Sstevel@tonic-gate  *		-1	- error
5610Sstevel@tonic-gate  * PURPOSE:	Map the the virtual offset in the soft partition (passed
5620Sstevel@tonic-gate  *		in via voff) to the "physical" offset on whatever the soft
5630Sstevel@tonic-gate  *		partition is built on top of.  We do this by doing a binary
5640Sstevel@tonic-gate  *		search of the extent array in the soft partition unit
5650Sstevel@tonic-gate  *		structure.  Once the current extent is found, we do the
5660Sstevel@tonic-gate  *		translation, determine if the I/O will cross extent
5670Sstevel@tonic-gate  *		boundaries (if so, we have to fragment the I/O), then
5680Sstevel@tonic-gate  *		fill in the buf structure to be passed down to the next layer.
5690Sstevel@tonic-gate  */
5700Sstevel@tonic-gate static int
5710Sstevel@tonic-gate sp_mapbuf(
5720Sstevel@tonic-gate 	mp_unit_t	*un,
5730Sstevel@tonic-gate 	sp_ext_offset_t	voff,
5740Sstevel@tonic-gate 	sp_ext_length_t	bcount,
5750Sstevel@tonic-gate 	buf_t		*bp
5760Sstevel@tonic-gate )
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	int		lo, mid, hi, found, more;
5790Sstevel@tonic-gate 	size_t		new_bcount;
5800Sstevel@tonic-gate 	sp_ext_offset_t new_blkno;
5810Sstevel@tonic-gate 	sp_ext_offset_t	new_offset;
5820Sstevel@tonic-gate 	sp_ext_offset_t	ext_endblk;
5830Sstevel@tonic-gate 	md_dev64_t	new_edev;
5840Sstevel@tonic-gate 	extern unsigned	md_maxphys;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	found = 0;
5870Sstevel@tonic-gate 	lo = 0;
5880Sstevel@tonic-gate 	hi = un->un_numexts - 1;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	/*
5910Sstevel@tonic-gate 	 * do a binary search to find the extent that contains the
5920Sstevel@tonic-gate 	 * starting offset.  after this loop, mid contains the index
5930Sstevel@tonic-gate 	 * of the correct extent.
5940Sstevel@tonic-gate 	 */
5950Sstevel@tonic-gate 	while (lo <= hi && !found) {
5960Sstevel@tonic-gate 		mid = (lo + hi) / 2;
5970Sstevel@tonic-gate 		/* is the starting offset contained within the mid-ext? */
5980Sstevel@tonic-gate 		if (voff >= un->un_ext[mid].un_voff &&
5990Sstevel@tonic-gate 		    voff < un->un_ext[mid].un_voff + un->un_ext[mid].un_len)
6000Sstevel@tonic-gate 			found = 1;
6010Sstevel@tonic-gate 		else if (voff < un->un_ext[mid].un_voff)
6020Sstevel@tonic-gate 			hi = mid - 1;
6030Sstevel@tonic-gate 		else /* voff > un->un_ext[mid].un_voff + un->un_ext[mid].len */
6040Sstevel@tonic-gate 			lo = mid + 1;
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	if (!found) {
6080Sstevel@tonic-gate 		cmn_err(CE_WARN, "sp_mapbuf: invalid offset %llu.\n", voff);
6090Sstevel@tonic-gate 		return (-1);
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	/* translate to underlying physical offset/device */
6130Sstevel@tonic-gate 	new_offset = voff - un->un_ext[mid].un_voff;
6140Sstevel@tonic-gate 	new_blkno = un->un_ext[mid].un_poff + new_offset;
6150Sstevel@tonic-gate 	new_edev = un->un_dev;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	/* determine if we need to break the I/O into fragments */
6180Sstevel@tonic-gate 	ext_endblk = un->un_ext[mid].un_voff + un->un_ext[mid].un_len;
6190Sstevel@tonic-gate 	if (voff + btodb(bcount) > ext_endblk) {
6200Sstevel@tonic-gate 		new_bcount = dbtob(ext_endblk - voff);
6210Sstevel@tonic-gate 		more = 1;
6220Sstevel@tonic-gate 	} else {
6230Sstevel@tonic-gate 		new_bcount = bcount;
6240Sstevel@tonic-gate 		more = 0;
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/* only break up the I/O if we're not built on another metadevice */
6280Sstevel@tonic-gate 	if ((md_getmajor(new_edev) != md_major) && (new_bcount > md_maxphys)) {
6290Sstevel@tonic-gate 		new_bcount = md_maxphys;
6300Sstevel@tonic-gate 		more = 1;
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 	if (bp != (buf_t *)NULL) {
6330Sstevel@tonic-gate 		/* do bp updates */
6340Sstevel@tonic-gate 		bp->b_bcount = new_bcount;
6350Sstevel@tonic-gate 		bp->b_lblkno = new_blkno;
6360Sstevel@tonic-gate 		bp->b_edev = md_dev64_to_dev(new_edev);
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 	return (more);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate  * FUNCTION:	sp_validate()
6430Sstevel@tonic-gate  * INPUT:	un	- unit structure to be validated.
6440Sstevel@tonic-gate  * OUTPUT:	none.
6450Sstevel@tonic-gate  * RETURNS:	0	- soft partition ok.
6460Sstevel@tonic-gate  *		-1	- error.
6470Sstevel@tonic-gate  * PURPOSE:	called on open to sanity check the soft partition.  In
6480Sstevel@tonic-gate  *		order to open a soft partition:
6490Sstevel@tonic-gate  *		- it must have at least one extent
6500Sstevel@tonic-gate  *		- the extent info in core and on disk must match
6510Sstevel@tonic-gate  *		- it may not be in an intermediate state (which would
6520Sstevel@tonic-gate  *		  imply that a two-phase commit was interrupted)
6530Sstevel@tonic-gate  *
6540Sstevel@tonic-gate  *		If the extent checking fails (B_ERROR returned from the read
6550Sstevel@tonic-gate  *		strategy call) _and_ we're a multi-owner diskset, we send a
6560Sstevel@tonic-gate  *		message to the master so that all nodes inherit the same view
6570Sstevel@tonic-gate  *		of the soft partition.
6580Sstevel@tonic-gate  *		If we are checking a soft-part that is marked as in error, and
6590Sstevel@tonic-gate  *		we can actually read and validate the watermarks we send a
6600Sstevel@tonic-gate  *		message to clear the error to the master node.
6610Sstevel@tonic-gate  */
6620Sstevel@tonic-gate static int
6630Sstevel@tonic-gate sp_validate(mp_unit_t *un)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	uint_t		ext;
6660Sstevel@tonic-gate 	struct buf	*buf;
6670Sstevel@tonic-gate 	sp_ext_length_t	len;
6680Sstevel@tonic-gate 	mp_watermark_t	*wm;
6690Sstevel@tonic-gate 	set_t		setno;
6700Sstevel@tonic-gate 	int		reset_error = 0;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	setno = MD_UN2SET(un);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	/* sanity check unit structure components ?? */
6750Sstevel@tonic-gate 	if (un->un_status != MD_SP_OK) {
6760Sstevel@tonic-gate 		if (un->un_status != MD_SP_ERR) {
6770Sstevel@tonic-gate 			cmn_err(CE_WARN, "md: %s: open failed, soft partition "
6780Sstevel@tonic-gate 			    "status is %u.",
6790Sstevel@tonic-gate 			    md_shortname(MD_SID(un)),
6800Sstevel@tonic-gate 			    un->un_status);
6810Sstevel@tonic-gate 			return (-1);
6820Sstevel@tonic-gate 		} else {
6830Sstevel@tonic-gate 			cmn_err(CE_WARN, "md: %s: open of soft partition "
6840Sstevel@tonic-gate 			    "in Errored state.",
6850Sstevel@tonic-gate 			    md_shortname(MD_SID(un)));
6860Sstevel@tonic-gate 			reset_error = 1;
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	if (un->un_numexts == 0) {
6910Sstevel@tonic-gate 		cmn_err(CE_WARN, "md: %s: open failed, soft partition does "
6920Sstevel@tonic-gate 		    "not have any extents.", md_shortname(MD_SID(un)));
6930Sstevel@tonic-gate 		return (-1);
6940Sstevel@tonic-gate 	}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	len = 0LL;
6970Sstevel@tonic-gate 	for (ext = 0; ext < un->un_numexts; ext++) {
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		/* tally extent lengths to check total size */
7000Sstevel@tonic-gate 		len += un->un_ext[ext].un_len;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 		/* allocate buffer for watermark */
7030Sstevel@tonic-gate 		buf = getrbuf(KM_SLEEP);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		/* read watermark */
7060Sstevel@tonic-gate 		buf->b_flags = B_READ;
7070Sstevel@tonic-gate 		buf->b_edev = md_dev64_to_dev(un->un_dev);
7080Sstevel@tonic-gate 		buf->b_iodone = NULL;
7090Sstevel@tonic-gate 		buf->b_proc = NULL;
7100Sstevel@tonic-gate 		buf->b_bcount = sizeof (mp_watermark_t);
7110Sstevel@tonic-gate 		buf->b_lblkno = un->un_ext[ext].un_poff - 1;
7120Sstevel@tonic-gate 		buf->b_bufsize = sizeof (mp_watermark_t);
7130Sstevel@tonic-gate 		buf->b_un.b_addr = kmem_alloc(sizeof (mp_watermark_t),
7140Sstevel@tonic-gate 		    KM_SLEEP);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 		/*
7170Sstevel@tonic-gate 		 * make the call non-blocking so that it is not affected
7180Sstevel@tonic-gate 		 * by a set take.
7190Sstevel@tonic-gate 		 */
7200Sstevel@tonic-gate 		md_call_strategy(buf, MD_STR_MAPPED|MD_NOBLOCK, NULL);
7210Sstevel@tonic-gate 		(void) biowait(buf);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		if (buf->b_flags & B_ERROR) {
7240Sstevel@tonic-gate 			cmn_err(CE_WARN, "md: %s: open failed, could not "
7250Sstevel@tonic-gate 			    "read watermark at block %llu for extent %u, "
7260Sstevel@tonic-gate 			    "error %d.", md_shortname(MD_SID(un)),
7270Sstevel@tonic-gate 			    buf->b_lblkno, ext, buf->b_error);
7280Sstevel@tonic-gate 			kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t));
7290Sstevel@tonic-gate 			freerbuf(buf);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 			/*
7320Sstevel@tonic-gate 			 * If we're a multi-owner diskset we send a message
7330Sstevel@tonic-gate 			 * indicating that this soft-part has an invalid
7340Sstevel@tonic-gate 			 * extent to the master node. This ensures a consistent
7350Sstevel@tonic-gate 			 * view of the soft-part across the cluster.
7360Sstevel@tonic-gate 			 */
7370Sstevel@tonic-gate 			if (MD_MNSET_SETNO(setno)) {
7380Sstevel@tonic-gate 				sp_send_stat_err(un);
7390Sstevel@tonic-gate 			}
7400Sstevel@tonic-gate 			return (-1);
7410Sstevel@tonic-gate 		}
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 		wm = (mp_watermark_t *)buf->b_un.b_addr;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 		/* make sure the checksum is correct first */
7460Sstevel@tonic-gate 		if (crcchk((uchar_t *)wm, (uint_t *)&wm->wm_checksum,
7470Sstevel@tonic-gate 		    (uint_t)sizeof (mp_watermark_t), (uchar_t *)NULL)) {
7480Sstevel@tonic-gate 			cmn_err(CE_WARN, "md: %s: open failed, watermark "
7490Sstevel@tonic-gate 			    "at block %llu for extent %u does not have a "
7500Sstevel@tonic-gate 			    "valid checksum 0x%08x.", md_shortname(MD_SID(un)),
7510Sstevel@tonic-gate 			    buf->b_lblkno, ext, wm->wm_checksum);
7520Sstevel@tonic-gate 			kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t));
7530Sstevel@tonic-gate 			freerbuf(buf);
7540Sstevel@tonic-gate 			return (-1);
7550Sstevel@tonic-gate 		}
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 		if (wm->wm_magic != MD_SP_MAGIC) {
7580Sstevel@tonic-gate 			cmn_err(CE_WARN, "md: %s: open failed, watermark "
7590Sstevel@tonic-gate 			    "at block %llu for extent %u does not have a "
7600Sstevel@tonic-gate 			    "valid watermark magic number, expected 0x%x, "
7610Sstevel@tonic-gate 			    "found 0x%x.", md_shortname(MD_SID(un)),
7620Sstevel@tonic-gate 			    buf->b_lblkno, ext, MD_SP_MAGIC, wm->wm_magic);
7630Sstevel@tonic-gate 			kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t));
7640Sstevel@tonic-gate 			freerbuf(buf);
7650Sstevel@tonic-gate 			return (-1);
7660Sstevel@tonic-gate 		}
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 		/* make sure sequence number matches the current extent */
7690Sstevel@tonic-gate 		if (wm->wm_seq != ext) {
7700Sstevel@tonic-gate 			cmn_err(CE_WARN, "md: %s: open failed, watermark "
7710Sstevel@tonic-gate 			    "at block %llu for extent %u has invalid "
7720Sstevel@tonic-gate 			    "sequence number %u.", md_shortname(MD_SID(un)),
7730Sstevel@tonic-gate 			    buf->b_lblkno, ext, wm->wm_seq);
7740Sstevel@tonic-gate 			kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t));
7750Sstevel@tonic-gate 			freerbuf(buf);
7760Sstevel@tonic-gate 			return (-1);
7770Sstevel@tonic-gate 		}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 		/* make sure watermark length matches unit structure */
7800Sstevel@tonic-gate 		if (wm->wm_length != un->un_ext[ext].un_len) {
7810Sstevel@tonic-gate 			cmn_err(CE_WARN, "md: %s: open failed, watermark "
7820Sstevel@tonic-gate 			    "at block %llu for extent %u has inconsistent "
7830Sstevel@tonic-gate 			    "length, expected %llu, found %llu.",
7840Sstevel@tonic-gate 			    md_shortname(MD_SID(un)), buf->b_lblkno,
7850Sstevel@tonic-gate 			    ext, un->un_ext[ext].un_len,
7860Sstevel@tonic-gate 			    (u_longlong_t)wm->wm_length);
7870Sstevel@tonic-gate 			kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t));
7880Sstevel@tonic-gate 			freerbuf(buf);
7890Sstevel@tonic-gate 			return (-1);
7900Sstevel@tonic-gate 		}
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 		/*
7930Sstevel@tonic-gate 		 * make sure the type is a valid soft partition and not
7940Sstevel@tonic-gate 		 * a free extent or the end.
7950Sstevel@tonic-gate 		 */
7960Sstevel@tonic-gate 		if (wm->wm_type != EXTTYP_ALLOC) {
7970Sstevel@tonic-gate 			cmn_err(CE_WARN, "md: %s: open failed, watermark "
7980Sstevel@tonic-gate 			    "at block %llu for extent %u is not marked "
7990Sstevel@tonic-gate 			    "as in-use, type = %u.", md_shortname(MD_SID(un)),
8000Sstevel@tonic-gate 			    buf->b_lblkno, ext, wm->wm_type);
8010Sstevel@tonic-gate 			kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t));
8020Sstevel@tonic-gate 			freerbuf(buf);
8030Sstevel@tonic-gate 			return (-1);
8040Sstevel@tonic-gate 		}
8050Sstevel@tonic-gate 		/* free up buffer */
8060Sstevel@tonic-gate 		kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t));
8070Sstevel@tonic-gate 		freerbuf(buf);
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	if (len != un->un_length) {
8110Sstevel@tonic-gate 		cmn_err(CE_WARN, "md: %s: open failed, computed length "
8120Sstevel@tonic-gate 		    "%llu != expected length %llu.", md_shortname(MD_SID(un)),
8130Sstevel@tonic-gate 		    len, un->un_length);
8140Sstevel@tonic-gate 		return (-1);
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/*
8180Sstevel@tonic-gate 	 * If we're a multi-owner set _and_ reset_error is set, we should clear
8190Sstevel@tonic-gate 	 * the error condition on all nodes in the set. Use SP_SETSTAT2 with
8200Sstevel@tonic-gate 	 * MD_SP_OK.
8210Sstevel@tonic-gate 	 */
8220Sstevel@tonic-gate 	if (MD_MNSET_SETNO(setno) && reset_error) {
8230Sstevel@tonic-gate 		sp_send_stat_ok(un);
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 	return (0);
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate /*
8290Sstevel@tonic-gate  * FUNCTION:	sp_done()
8300Sstevel@tonic-gate  * INPUT:	child_buf	- buffer attached to child save structure.
8310Sstevel@tonic-gate  *				  this is the buffer on which I/O has just
8320Sstevel@tonic-gate  *				  completed.
8330Sstevel@tonic-gate  * OUTPUT:	none.
8340Sstevel@tonic-gate  * RETURNS:	0	- success.
8350Sstevel@tonic-gate  *		1	- error.
8360Sstevel@tonic-gate  * PURPOSE:	called on I/O completion.
8370Sstevel@tonic-gate  */
8380Sstevel@tonic-gate static int
8390Sstevel@tonic-gate sp_done(struct buf *child_buf)
8400Sstevel@tonic-gate {
8410Sstevel@tonic-gate 	struct buf	*parent_buf;
8420Sstevel@tonic-gate 	mdi_unit_t	*ui;
8430Sstevel@tonic-gate 	md_spps_t	*ps;
8440Sstevel@tonic-gate 	md_spcs_t	*cs;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	/* find the child save structure to which this buffer belongs */
8470Sstevel@tonic-gate 	cs = (md_spcs_t *)((caddr_t)child_buf -
8480Sstevel@tonic-gate 	    (sizeof (md_spcs_t) - sizeof (buf_t)));
8490Sstevel@tonic-gate 	/* now get the parent save structure */
8500Sstevel@tonic-gate 	ps = cs->cs_ps;
8510Sstevel@tonic-gate 	parent_buf = ps->ps_bp;
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	mutex_enter(&ps->ps_mx);
8540Sstevel@tonic-gate 	/* pass any errors back up to the parent */
8550Sstevel@tonic-gate 	if (child_buf->b_flags & B_ERROR) {
8560Sstevel@tonic-gate 		ps->ps_flags |= MD_SPPS_ERROR;
8570Sstevel@tonic-gate 		parent_buf->b_error = child_buf->b_error;
8580Sstevel@tonic-gate 	}
8590Sstevel@tonic-gate 	/* mapout, if needed */
8600Sstevel@tonic-gate 	if (child_buf->b_flags & B_REMAPPED)
8610Sstevel@tonic-gate 		bp_mapout(child_buf);
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	ps->ps_frags--;
8640Sstevel@tonic-gate 	if (ps->ps_frags != 0) {
8650Sstevel@tonic-gate 		/*
8660Sstevel@tonic-gate 		 * if this parent has more children, we just free the
8670Sstevel@tonic-gate 		 * child and return.
8680Sstevel@tonic-gate 		 */
8690Sstevel@tonic-gate 		kmem_cache_free(sp_child_cache, cs);
8700Sstevel@tonic-gate 		mutex_exit(&ps->ps_mx);
8710Sstevel@tonic-gate 		return (1);
8720Sstevel@tonic-gate 	}
8730Sstevel@tonic-gate 	/* there are no more children */
8740Sstevel@tonic-gate 	kmem_cache_free(sp_child_cache, cs);
8750Sstevel@tonic-gate 	if (ps->ps_flags & MD_SPPS_ERROR) {
8760Sstevel@tonic-gate 		sp_error(ps);
8770Sstevel@tonic-gate 		return (1);
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 	ui = ps->ps_ui;
8800Sstevel@tonic-gate 	if (!(ps->ps_flags & MD_SPPS_DONTFREE)) {
8810Sstevel@tonic-gate 		mutex_exit(&ps->ps_mx);
8820Sstevel@tonic-gate 	} else {
8830Sstevel@tonic-gate 		/*
8840Sstevel@tonic-gate 		 * this should only ever happen if we are panicking,
8850Sstevel@tonic-gate 		 * since DONTFREE is only set on the parent if panicstr
8860Sstevel@tonic-gate 		 * is non-NULL.
8870Sstevel@tonic-gate 		 */
8880Sstevel@tonic-gate 		ASSERT(panicstr);
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 	SPPS_FREE(sp_parent_cache, ps);
8910Sstevel@tonic-gate 	md_kstat_done(ui, parent_buf, 0);
8920Sstevel@tonic-gate 	md_unit_readerexit(ui);
8930Sstevel@tonic-gate 	md_biodone(parent_buf);
8940Sstevel@tonic-gate 	return (0);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate  * FUNCTION:	md_sp_strategy()
8990Sstevel@tonic-gate  * INPUT:	parent_buf	- parent buffer
9000Sstevel@tonic-gate  *		flag		- flags
9010Sstevel@tonic-gate  *		private		- private data
9020Sstevel@tonic-gate  * OUTPUT:	none.
9030Sstevel@tonic-gate  * RETURNS:	void.
9040Sstevel@tonic-gate  * PURPOSE:	Soft partitioning I/O strategy.  Performs the main work
9050Sstevel@tonic-gate  *		needed to do I/O to a soft partition.  The basic
9060Sstevel@tonic-gate  *		algorithm is as follows:
9070Sstevel@tonic-gate  *			- Allocate a child save structure to keep track
9080Sstevel@tonic-gate  *			  of the I/O we are going to pass down.
9090Sstevel@tonic-gate  *			- Map the I/O to the correct extent in the soft
9100Sstevel@tonic-gate  *			  partition (see sp_mapbuf()).
9110Sstevel@tonic-gate  *			- bioclone() the buffer and pass it down the
9120Sstevel@tonic-gate  *			  stack using md_call_strategy.
9130Sstevel@tonic-gate  *			- If the I/O needs to split across extents,
9140Sstevel@tonic-gate  *			  repeat the above steps until all fragments
9150Sstevel@tonic-gate  *			  are finished.
9160Sstevel@tonic-gate  */
9170Sstevel@tonic-gate static void
9180Sstevel@tonic-gate md_sp_strategy(buf_t *parent_buf, int flag, void *private)
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate 	md_spps_t	*ps;
9210Sstevel@tonic-gate 	md_spcs_t	*cs;
9220Sstevel@tonic-gate 	int		more;
9230Sstevel@tonic-gate 	mp_unit_t	*un;
9240Sstevel@tonic-gate 	mdi_unit_t	*ui;
9250Sstevel@tonic-gate 	size_t		current_count;
9260Sstevel@tonic-gate 	off_t		current_offset;
9270Sstevel@tonic-gate 	sp_ext_offset_t	current_blkno;
9280Sstevel@tonic-gate 	buf_t		*child_buf;
9290Sstevel@tonic-gate 	set_t		setno = MD_MIN2SET(getminor(parent_buf->b_edev));
9300Sstevel@tonic-gate 	int		strat_flag = flag;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	/*
9330Sstevel@tonic-gate 	 * When doing IO to a multi owner meta device, check if set is halted.
9340Sstevel@tonic-gate 	 * We do this check without the needed lock held, for performance
9350Sstevel@tonic-gate 	 * reasons.
9360Sstevel@tonic-gate 	 * If an IO just slips through while the set is locked via an
9370Sstevel@tonic-gate 	 * MD_MN_SUSPEND_SET, we don't care about it.
9380Sstevel@tonic-gate 	 * Only check for suspension if we are a top-level i/o request
9390Sstevel@tonic-gate 	 * (MD_STR_NOTTOP is cleared in 'flag');
9400Sstevel@tonic-gate 	 */
9410Sstevel@tonic-gate 	if ((md_set[setno].s_status & (MD_SET_HALTED | MD_SET_MNSET)) ==
9420Sstevel@tonic-gate 	    (MD_SET_HALTED | MD_SET_MNSET)) {
9430Sstevel@tonic-gate 		if ((flag & MD_STR_NOTTOP) == 0) {
9440Sstevel@tonic-gate 			mutex_enter(&md_mx);
9450Sstevel@tonic-gate 			/* Here we loop until the set is no longer halted */
9460Sstevel@tonic-gate 			while (md_set[setno].s_status & MD_SET_HALTED) {
9470Sstevel@tonic-gate 				cv_wait(&md_cv, &md_mx);
9480Sstevel@tonic-gate 			}
9490Sstevel@tonic-gate 			mutex_exit(&md_mx);
9500Sstevel@tonic-gate 		}
9510Sstevel@tonic-gate 	}
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	ui = MDI_UNIT(getminor(parent_buf->b_edev));
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	md_kstat_waitq_enter(ui);
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	un = (mp_unit_t *)md_unit_readerlock(ui);
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	if ((flag & MD_NOBLOCK) == 0) {
9600Sstevel@tonic-gate 		if (md_inc_iocount(setno) != 0) {
9610Sstevel@tonic-gate 			parent_buf->b_flags |= B_ERROR;
9620Sstevel@tonic-gate 			parent_buf->b_error = ENXIO;
9630Sstevel@tonic-gate 			parent_buf->b_resid = parent_buf->b_bcount;
9640Sstevel@tonic-gate 			md_unit_readerexit(ui);
9650Sstevel@tonic-gate 			biodone(parent_buf);
9660Sstevel@tonic-gate 			return;
9670Sstevel@tonic-gate 		}
9680Sstevel@tonic-gate 	} else {
9690Sstevel@tonic-gate 		md_inc_iocount_noblock(setno);
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	if (!(flag & MD_STR_NOTTOP)) {
9730Sstevel@tonic-gate 		if (md_checkbuf(ui, (md_unit_t *)un, parent_buf) != 0) {
9740Sstevel@tonic-gate 			md_kstat_waitq_exit(ui);
9750Sstevel@tonic-gate 			return;
9760Sstevel@tonic-gate 		}
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	ps = kmem_cache_alloc(sp_parent_cache, MD_ALLOCFLAGS);
9800Sstevel@tonic-gate 	sp_parent_init(ps);
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	/*
9830Sstevel@tonic-gate 	 * Save essential information from the original buffhdr
9840Sstevel@tonic-gate 	 * in the parent.
9850Sstevel@tonic-gate 	 */
9860Sstevel@tonic-gate 	ps->ps_un = un;
9870Sstevel@tonic-gate 	ps->ps_ui = ui;
9880Sstevel@tonic-gate 	ps->ps_bp = parent_buf;
9890Sstevel@tonic-gate 	ps->ps_addr = parent_buf->b_un.b_addr;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	current_count = parent_buf->b_bcount;
9920Sstevel@tonic-gate 	current_blkno = (sp_ext_offset_t)parent_buf->b_blkno;
9930Sstevel@tonic-gate 	current_offset  = 0;
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	/*
9960Sstevel@tonic-gate 	 * if we are at the top and we are panicking,
9970Sstevel@tonic-gate 	 * we don't free in order to save state.
9980Sstevel@tonic-gate 	 */
9990Sstevel@tonic-gate 	if (!(flag & MD_STR_NOTTOP) && (panicstr != NULL))
10000Sstevel@tonic-gate 		ps->ps_flags |= MD_SPPS_DONTFREE;
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	md_kstat_waitq_to_runq(ui);
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	ps->ps_frags++;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	/*
10070Sstevel@tonic-gate 	 * Mark this i/o as MD_STR_ABR if we've had ABR enabled on this
10080Sstevel@tonic-gate 	 * metadevice.
10090Sstevel@tonic-gate 	 */
10100Sstevel@tonic-gate 	if (ui->ui_tstate & MD_ABR_CAP)
10110Sstevel@tonic-gate 		strat_flag |= MD_STR_ABR;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	/*
10140Sstevel@tonic-gate 	 * this loop does the main work of an I/O.  we allocate a
10150Sstevel@tonic-gate 	 * a child save for each buf, do the logical to physical
10160Sstevel@tonic-gate 	 * mapping, decide if we need to frag the I/O, clone the
10170Sstevel@tonic-gate 	 * new I/O to pass down the stack.  repeat until we've
10180Sstevel@tonic-gate 	 * taken care of the entire buf that was passed to us.
10190Sstevel@tonic-gate 	 */
10200Sstevel@tonic-gate 	do {
10210Sstevel@tonic-gate 		cs = kmem_cache_alloc(sp_child_cache, MD_ALLOCFLAGS);
10220Sstevel@tonic-gate 		sp_child_init(cs);
10230Sstevel@tonic-gate 		child_buf = &cs->cs_buf;
10240Sstevel@tonic-gate 		cs->cs_ps = ps;
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 		more = sp_mapbuf(un, current_blkno, current_count, child_buf);
10270Sstevel@tonic-gate 		if (more == -1) {
10280Sstevel@tonic-gate 			parent_buf->b_flags |= B_ERROR;
10290Sstevel@tonic-gate 			parent_buf->b_error = EIO;
10300Sstevel@tonic-gate 			md_kstat_done(ui, parent_buf, 0);
10310Sstevel@tonic-gate 			md_unit_readerexit(ui);
10320Sstevel@tonic-gate 			md_biodone(parent_buf);
10330Sstevel@tonic-gate 			kmem_cache_free(sp_parent_cache, ps);
10340Sstevel@tonic-gate 			return;
10350Sstevel@tonic-gate 		}
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		child_buf = md_bioclone(parent_buf, current_offset,
10380Sstevel@tonic-gate 					child_buf->b_bcount, child_buf->b_edev,
10390Sstevel@tonic-gate 					child_buf->b_blkno, sp_done, child_buf,
10400Sstevel@tonic-gate 					KM_NOSLEEP);
10410Sstevel@tonic-gate 		/* calculate new offset, counts, etc... */
10420Sstevel@tonic-gate 		current_offset += child_buf->b_bcount;
10430Sstevel@tonic-gate 		current_count -=  child_buf->b_bcount;
10440Sstevel@tonic-gate 		current_blkno +=  (sp_ext_offset_t)(btodb(child_buf->b_bcount));
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 		if (more) {
10470Sstevel@tonic-gate 			mutex_enter(&ps->ps_mx);
10480Sstevel@tonic-gate 			ps->ps_frags++;
10490Sstevel@tonic-gate 			mutex_exit(&ps->ps_mx);
10500Sstevel@tonic-gate 		}
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 		md_call_strategy(child_buf, strat_flag, private);
10530Sstevel@tonic-gate 	} while (more);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	if (!(flag & MD_STR_NOTTOP) && (panicstr != NULL)) {
10560Sstevel@tonic-gate 		while (!(ps->ps_flags & MD_SPPS_DONE)) {
10570Sstevel@tonic-gate 			md_daemon(1, &md_done_daemon);
10580Sstevel@tonic-gate 		}
10590Sstevel@tonic-gate 		kmem_cache_free(sp_parent_cache, ps);
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate /*
10640Sstevel@tonic-gate  * FUNCTION:	sp_directed_read()
10650Sstevel@tonic-gate  * INPUT:	mnum	- minor number
10660Sstevel@tonic-gate  *		vdr	- vol_directed_rd_t from user
10670Sstevel@tonic-gate  *		mode	- access mode for copying data out.
10680Sstevel@tonic-gate  * OUTPUT:	none.
10690Sstevel@tonic-gate  * RETURNS:	0	- success
10700Sstevel@tonic-gate  *		Exxxxx	- failure error-code
10710Sstevel@tonic-gate  * PURPOSE:	Construct the necessary sub-device i/o requests to perform the
10720Sstevel@tonic-gate  *		directed read as requested by the user. This is essentially the
10730Sstevel@tonic-gate  *		same as md_sp_strategy() with the exception being that the
10740Sstevel@tonic-gate  *		underlying 'md_call_strategy' is replaced with an ioctl call.
10750Sstevel@tonic-gate  */
10760Sstevel@tonic-gate int
10770Sstevel@tonic-gate sp_directed_read(minor_t mnum, vol_directed_rd_t *vdr, int mode)
10780Sstevel@tonic-gate {
10790Sstevel@tonic-gate 	md_spps_t	*ps;
10800Sstevel@tonic-gate 	md_spcs_t	*cs;
10810Sstevel@tonic-gate 	int		more;
10820Sstevel@tonic-gate 	mp_unit_t	*un;
10830Sstevel@tonic-gate 	mdi_unit_t	*ui;
10840Sstevel@tonic-gate 	size_t		current_count;
10850Sstevel@tonic-gate 	off_t		current_offset;
10860Sstevel@tonic-gate 	sp_ext_offset_t	current_blkno;
10870Sstevel@tonic-gate 	buf_t		*child_buf, *parent_buf;
10880Sstevel@tonic-gate 	void		*kbuffer;
10890Sstevel@tonic-gate 	vol_directed_rd_t	cvdr;
10900Sstevel@tonic-gate 	caddr_t		userbuf;
10910Sstevel@tonic-gate 	offset_t	useroff;
10920Sstevel@tonic-gate 	int		ret = 0;
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	ui = MDI_UNIT(mnum);
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	md_kstat_waitq_enter(ui);
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	bzero(&cvdr, sizeof (cvdr));
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	un = (mp_unit_t *)md_unit_readerlock(ui);
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	/*
11030Sstevel@tonic-gate 	 * Construct a parent_buf header which reflects the user-supplied
11040Sstevel@tonic-gate 	 * request.
11050Sstevel@tonic-gate 	 */
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	kbuffer = kmem_alloc(vdr->vdr_nbytes, KM_NOSLEEP);
11080Sstevel@tonic-gate 	if (kbuffer == NULL) {
11090Sstevel@tonic-gate 		vdr->vdr_flags |= DKV_DMR_ERROR;
11100Sstevel@tonic-gate 		md_unit_readerexit(ui);
11110Sstevel@tonic-gate 		return (ENOMEM);
11120Sstevel@tonic-gate 	}
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	parent_buf = getrbuf(KM_NOSLEEP);
11150Sstevel@tonic-gate 	if (parent_buf == NULL) {
11160Sstevel@tonic-gate 		vdr->vdr_flags |= DKV_DMR_ERROR;
11170Sstevel@tonic-gate 		md_unit_readerexit(ui);
11180Sstevel@tonic-gate 		kmem_free(kbuffer, vdr->vdr_nbytes);
11190Sstevel@tonic-gate 		return (ENOMEM);
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate 	parent_buf->b_un.b_addr = kbuffer;
11220Sstevel@tonic-gate 	parent_buf->b_flags = B_READ;
11230Sstevel@tonic-gate 	parent_buf->b_bcount = vdr->vdr_nbytes;
11240Sstevel@tonic-gate 	parent_buf->b_lblkno = lbtodb(vdr->vdr_offset);
11250Sstevel@tonic-gate 	parent_buf->b_edev = un->un_dev;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	ps = kmem_cache_alloc(sp_parent_cache, MD_ALLOCFLAGS);
11290Sstevel@tonic-gate 	sp_parent_init(ps);
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	/*
11320Sstevel@tonic-gate 	 * Save essential information from the original buffhdr
11330Sstevel@tonic-gate 	 * in the parent.
11340Sstevel@tonic-gate 	 */
11350Sstevel@tonic-gate 	ps->ps_un = un;
11360Sstevel@tonic-gate 	ps->ps_ui = ui;
11370Sstevel@tonic-gate 	ps->ps_bp = parent_buf;
11380Sstevel@tonic-gate 	ps->ps_addr = parent_buf->b_un.b_addr;
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	current_count = parent_buf->b_bcount;
11410Sstevel@tonic-gate 	current_blkno = (sp_ext_offset_t)parent_buf->b_lblkno;
11420Sstevel@tonic-gate 	current_offset  = 0;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	ps->ps_frags++;
11450Sstevel@tonic-gate 	vdr->vdr_bytesread = 0;
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	/*
11480Sstevel@tonic-gate 	 * this loop does the main work of an I/O.  we allocate a
11490Sstevel@tonic-gate 	 * a child save for each buf, do the logical to physical
11500Sstevel@tonic-gate 	 * mapping, decide if we need to frag the I/O, clone the
11510Sstevel@tonic-gate 	 * new I/O to pass down the stack.  repeat until we've
11520Sstevel@tonic-gate 	 * taken care of the entire buf that was passed to us.
11530Sstevel@tonic-gate 	 */
11540Sstevel@tonic-gate 	do {
11550Sstevel@tonic-gate 		cs = kmem_cache_alloc(sp_child_cache, MD_ALLOCFLAGS);
11560Sstevel@tonic-gate 		sp_child_init(cs);
11570Sstevel@tonic-gate 		child_buf = &cs->cs_buf;
11580Sstevel@tonic-gate 		cs->cs_ps = ps;
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 		more = sp_mapbuf(un, current_blkno, current_count, child_buf);
11610Sstevel@tonic-gate 		if (more == -1) {
11620Sstevel@tonic-gate 			ret = EIO;
11630Sstevel@tonic-gate 			vdr->vdr_flags |= DKV_DMR_SHORT;
11640Sstevel@tonic-gate 			kmem_cache_free(sp_child_cache, cs);
11650Sstevel@tonic-gate 			goto err_out;
11660Sstevel@tonic-gate 		}
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 		cvdr.vdr_flags = vdr->vdr_flags;
11690Sstevel@tonic-gate 		cvdr.vdr_side = vdr->vdr_side;
11700Sstevel@tonic-gate 		cvdr.vdr_nbytes = child_buf->b_bcount;
11710Sstevel@tonic-gate 		cvdr.vdr_offset = ldbtob(child_buf->b_lblkno);
11720Sstevel@tonic-gate 		/* Work out where we are in the allocated buffer */
117362Sjeanm 		useroff = (offset_t)(uintptr_t)kbuffer;
11740Sstevel@tonic-gate 		useroff = useroff + (offset_t)current_offset;
117562Sjeanm 		cvdr.vdr_data = (void *)(uintptr_t)useroff;
11760Sstevel@tonic-gate 		child_buf = md_bioclone(parent_buf, current_offset,
11770Sstevel@tonic-gate 					child_buf->b_bcount, child_buf->b_edev,
11780Sstevel@tonic-gate 					child_buf->b_blkno, NULL,
11790Sstevel@tonic-gate 					child_buf, KM_NOSLEEP);
11800Sstevel@tonic-gate 		/* calculate new offset, counts, etc... */
11810Sstevel@tonic-gate 		current_offset += child_buf->b_bcount;
11820Sstevel@tonic-gate 		current_count -=  child_buf->b_bcount;
11830Sstevel@tonic-gate 		current_blkno +=  (sp_ext_offset_t)(btodb(child_buf->b_bcount));
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 		if (more) {
11860Sstevel@tonic-gate 			mutex_enter(&ps->ps_mx);
11870Sstevel@tonic-gate 			ps->ps_frags++;
11880Sstevel@tonic-gate 			mutex_exit(&ps->ps_mx);
11890Sstevel@tonic-gate 		}
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 		ret = md_call_ioctl(child_buf->b_edev, DKIOCDMR, &cvdr,
11920Sstevel@tonic-gate 		    (mode | FKIOCTL), NULL);
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 		/*
11950Sstevel@tonic-gate 		 * Free the child structure as we've finished with it.
11960Sstevel@tonic-gate 		 * Normally this would be done by sp_done() but we're just
11970Sstevel@tonic-gate 		 * using md_bioclone() to segment the transfer and we never
11980Sstevel@tonic-gate 		 * issue a strategy request so the iodone will not be called.
11990Sstevel@tonic-gate 		 */
12000Sstevel@tonic-gate 		kmem_cache_free(sp_child_cache, cs);
12010Sstevel@tonic-gate 		if (ret == 0) {
12020Sstevel@tonic-gate 			/* copyout the returned data to vdr_data + offset */
12030Sstevel@tonic-gate 			userbuf = (caddr_t)kbuffer;
12040Sstevel@tonic-gate 			userbuf += (caddr_t)(cvdr.vdr_data) - (caddr_t)kbuffer;
12050Sstevel@tonic-gate 			if (ddi_copyout(userbuf, vdr->vdr_data,
12060Sstevel@tonic-gate 			    cvdr.vdr_bytesread, mode)) {
12070Sstevel@tonic-gate 				ret = EFAULT;
12080Sstevel@tonic-gate 				goto err_out;
12090Sstevel@tonic-gate 			}
12100Sstevel@tonic-gate 			vdr->vdr_bytesread += cvdr.vdr_bytesread;
12110Sstevel@tonic-gate 		} else {
12120Sstevel@tonic-gate 			goto err_out;
12130Sstevel@tonic-gate 		}
12140Sstevel@tonic-gate 	} while (more);
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	/*
12170Sstevel@tonic-gate 	 * Update the user-supplied vol_directed_rd_t structure with the
12180Sstevel@tonic-gate 	 * contents of the last issued child request.
12190Sstevel@tonic-gate 	 */
12200Sstevel@tonic-gate 	vdr->vdr_flags = cvdr.vdr_flags;
12210Sstevel@tonic-gate 	vdr->vdr_side = cvdr.vdr_side;
12220Sstevel@tonic-gate 	bcopy(cvdr.vdr_side_name, vdr->vdr_side_name, VOL_SIDENAME);
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate err_out:
12250Sstevel@tonic-gate 	if (ret != 0) {
12260Sstevel@tonic-gate 		vdr->vdr_flags |= DKV_DMR_ERROR;
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 	if (vdr->vdr_bytesread != vdr->vdr_nbytes) {
12290Sstevel@tonic-gate 		vdr->vdr_flags |= DKV_DMR_SHORT;
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate 	kmem_cache_free(sp_parent_cache, ps);
12320Sstevel@tonic-gate 	kmem_free(kbuffer, vdr->vdr_nbytes);
12330Sstevel@tonic-gate 	freerbuf(parent_buf);
12340Sstevel@tonic-gate 	md_unit_readerexit(ui);
12350Sstevel@tonic-gate 	return (ret);
12360Sstevel@tonic-gate }
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate /*
12390Sstevel@tonic-gate  * FUNCTION:	sp_snarf()
12400Sstevel@tonic-gate  * INPUT:	cmd	- snarf cmd.
12410Sstevel@tonic-gate  *		setno	- set number.
12420Sstevel@tonic-gate  * OUTPUT:	none.
12430Sstevel@tonic-gate  * RETURNS:	1	- soft partitions were snarfed.
12440Sstevel@tonic-gate  *		0	- no soft partitions were snarfed.
12450Sstevel@tonic-gate  * PURPOSE:	Snarf soft partition metadb records into their in-core
12460Sstevel@tonic-gate  *		structures.  This routine is called at "snarf time" when
12470Sstevel@tonic-gate  *		md loads and gets all metadevices records into memory.
12480Sstevel@tonic-gate  *		The basic algorithm is simply to walk the soft partition
12490Sstevel@tonic-gate  *		records in the metadb and call the soft partitioning
12500Sstevel@tonic-gate  *		build_incore routine to set up the in-core structures.
12510Sstevel@tonic-gate  */
12520Sstevel@tonic-gate static int
12530Sstevel@tonic-gate sp_snarf(md_snarfcmd_t cmd, set_t setno)
12540Sstevel@tonic-gate {
12550Sstevel@tonic-gate 	mp_unit_t	*un;
12560Sstevel@tonic-gate 	mddb_recid_t	recid;
12570Sstevel@tonic-gate 	int		gotsomething;
12580Sstevel@tonic-gate 	int		all_sp_gotten;
12590Sstevel@tonic-gate 	mddb_type_t	rec_type;
12600Sstevel@tonic-gate 	mddb_de_ic_t	*dep;
12610Sstevel@tonic-gate 	mddb_rb32_t	*rbp;
12620Sstevel@tonic-gate 	mp_unit_t	*big_un;
12630Sstevel@tonic-gate 	mp_unit32_od_t	*small_un;
12640Sstevel@tonic-gate 	size_t		newreqsize;
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	if (cmd == MD_SNARF_CLEANUP)
12680Sstevel@tonic-gate 		return (0);
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	all_sp_gotten = 1;
12710Sstevel@tonic-gate 	gotsomething = 0;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	/* get the record type */
12740Sstevel@tonic-gate 	rec_type = (mddb_type_t)md_getshared_key(setno,
12750Sstevel@tonic-gate 	    sp_md_ops.md_driver.md_drivername);
12760Sstevel@tonic-gate 	recid = mddb_makerecid(setno, 0);
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	/*
12790Sstevel@tonic-gate 	 * walk soft partition records in the metadb and call
12800Sstevel@tonic-gate 	 * sp_build_incore to build in-core structures.
12810Sstevel@tonic-gate 	 */
12820Sstevel@tonic-gate 	while ((recid = mddb_getnextrec(recid, rec_type, 0)) > 0) {
12830Sstevel@tonic-gate 		/* if we've already gotten this record, go to the next one */
12840Sstevel@tonic-gate 		if (mddb_getrecprivate(recid) & MD_PRV_GOTIT)
12850Sstevel@tonic-gate 			continue;
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 		dep = mddb_getrecdep(recid);
12890Sstevel@tonic-gate 		dep->de_flags = MDDB_F_SOFTPART;
12900Sstevel@tonic-gate 		rbp = dep->de_rb;
12910Sstevel@tonic-gate 
12921623Stw21770 		switch (rbp->rb_revision) {
12931623Stw21770 		case MDDB_REV_RB:
12941623Stw21770 		case MDDB_REV_RBFN:
12951623Stw21770 			if ((rbp->rb_private & MD_PRV_CONVD) == 0) {
12961623Stw21770 				/*
12971623Stw21770 				 * This means, we have an old and small record.
12981623Stw21770 				 * And this record hasn't already been converted
12991623Stw21770 				 * :-o before we create an incore metadevice
13001623Stw21770 				 * from this we have to convert it to a big
13011623Stw21770 				 * record.
13021623Stw21770 				 */
13031623Stw21770 				small_un =
13041623Stw21770 				    (mp_unit32_od_t *)mddb_getrecaddr(recid);
13051623Stw21770 				newreqsize = sizeof (mp_unit_t) +
13061623Stw21770 						((small_un->un_numexts - 1) *
13071623Stw21770 						sizeof (struct mp_ext));
13081623Stw21770 				big_un = (mp_unit_t *)kmem_zalloc(newreqsize,
13091623Stw21770 					KM_SLEEP);
13101623Stw21770 				softpart_convert((caddr_t)small_un,
13111623Stw21770 					(caddr_t)big_un, SMALL_2_BIG);
13121623Stw21770 				kmem_free(small_un, dep->de_reqsize);
13131623Stw21770 				dep->de_rb_userdata = big_un;
13141623Stw21770 				dep->de_reqsize = newreqsize;
13151623Stw21770 				rbp->rb_private |= MD_PRV_CONVD;
13161623Stw21770 				un = big_un;
13171623Stw21770 			} else {
13181623Stw21770 				/* Record has already been converted */
13191623Stw21770 				un = (mp_unit_t *)mddb_getrecaddr(recid);
13201623Stw21770 			}
13211623Stw21770 			un->c.un_revision &= ~MD_64BIT_META_DEV;
13221623Stw21770 			break;
13231623Stw21770 		case MDDB_REV_RB64:
13241623Stw21770 		case MDDB_REV_RB64FN:
13250Sstevel@tonic-gate 			/* Large device */
13260Sstevel@tonic-gate 			un = (mp_unit_t *)mddb_getrecaddr(recid);
13271623Stw21770 			un->c.un_revision |= MD_64BIT_META_DEV;
13281623Stw21770 			un->c.un_flag |= MD_EFILABEL;
13291623Stw21770 			break;
13300Sstevel@tonic-gate 		}
1331*2077Stw21770 		MDDB_NOTE_FN(rbp->rb_revision, un->c.un_revision);
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 		/*
13340Sstevel@tonic-gate 		 * Create minor node for snarfed entry.
13350Sstevel@tonic-gate 		 */
13360Sstevel@tonic-gate 		(void) md_create_minor_node(MD_MIN2SET(MD_SID(un)), MD_SID(un));
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 		if (MD_UNIT(MD_SID(un)) != NULL) {
13390Sstevel@tonic-gate 			/* unit is already in-core */
13400Sstevel@tonic-gate 			mddb_setrecprivate(recid, MD_PRV_PENDDEL);
13410Sstevel@tonic-gate 			continue;
13420Sstevel@tonic-gate 		}
13430Sstevel@tonic-gate 		all_sp_gotten = 0;
13440Sstevel@tonic-gate 		if (sp_build_incore((void *)un, 1) == 0) {
13450Sstevel@tonic-gate 			mddb_setrecprivate(recid, MD_PRV_GOTIT);
13460Sstevel@tonic-gate 			md_create_unit_incore(MD_SID(un), &sp_md_ops, 0);
13470Sstevel@tonic-gate 			gotsomething = 1;
13480Sstevel@tonic-gate 		}
13490Sstevel@tonic-gate 	}
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	if (!all_sp_gotten)
13520Sstevel@tonic-gate 		return (gotsomething);
13530Sstevel@tonic-gate 	/* double-check records */
13540Sstevel@tonic-gate 	recid = mddb_makerecid(setno, 0);
13550Sstevel@tonic-gate 	while ((recid = mddb_getnextrec(recid, rec_type, 0)) > 0)
13560Sstevel@tonic-gate 		if (!(mddb_getrecprivate(recid) & MD_PRV_GOTIT))
13570Sstevel@tonic-gate 			mddb_setrecprivate(recid, MD_PRV_PENDDEL);
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	return (0);
13600Sstevel@tonic-gate }
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate /*
13630Sstevel@tonic-gate  * FUNCTION:	sp_halt()
13640Sstevel@tonic-gate  * INPUT:	cmd	- halt cmd.
13650Sstevel@tonic-gate  *		setno	- set number.
13660Sstevel@tonic-gate  * RETURNS:	0	- success.
13670Sstevel@tonic-gate  *		1	- err.
13680Sstevel@tonic-gate  * PURPOSE:	Perform driver halt operations.  As with stripe, we
13690Sstevel@tonic-gate  *		support MD_HALT_CHECK and MD_HALT_DOIT.  The first
13700Sstevel@tonic-gate  *		does a check to see if halting can be done safely
13710Sstevel@tonic-gate  *		(no open soft partitions), the second cleans up and
13720Sstevel@tonic-gate  *		shuts down the driver.
13730Sstevel@tonic-gate  */
13740Sstevel@tonic-gate static int
13750Sstevel@tonic-gate sp_halt(md_haltcmd_t cmd, set_t setno)
13760Sstevel@tonic-gate {
13770Sstevel@tonic-gate 	int		i;
13780Sstevel@tonic-gate 	mdi_unit_t	*ui;
13790Sstevel@tonic-gate 	minor_t		mnum;
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	if (cmd == MD_HALT_CLOSE)
13820Sstevel@tonic-gate 		return (0);
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	if (cmd == MD_HALT_OPEN)
13850Sstevel@tonic-gate 		return (0);
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	if (cmd == MD_HALT_UNLOAD)
13880Sstevel@tonic-gate 		return (0);
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	if (cmd == MD_HALT_CHECK) {
13910Sstevel@tonic-gate 		for (i = 0; i < md_nunits; i++) {
13920Sstevel@tonic-gate 			mnum = MD_MKMIN(setno, i);
13930Sstevel@tonic-gate 			if ((ui = MDI_UNIT(mnum)) == NULL)
13940Sstevel@tonic-gate 				continue;
13950Sstevel@tonic-gate 			if (ui->ui_opsindex != sp_md_ops.md_selfindex)
13960Sstevel@tonic-gate 				continue;
13970Sstevel@tonic-gate 			if (md_unit_isopen(ui))
13980Sstevel@tonic-gate 				return (1);
13990Sstevel@tonic-gate 		}
14000Sstevel@tonic-gate 		return (0);
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	if (cmd != MD_HALT_DOIT)
14040Sstevel@tonic-gate 		return (1);
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	for (i = 0; i < md_nunits; i++) {
14070Sstevel@tonic-gate 		mnum = MD_MKMIN(setno, i);
14080Sstevel@tonic-gate 		if ((ui = MDI_UNIT(mnum)) == NULL)
14090Sstevel@tonic-gate 			continue;
14100Sstevel@tonic-gate 		if (ui->ui_opsindex != sp_md_ops.md_selfindex)
14110Sstevel@tonic-gate 			continue;
14120Sstevel@tonic-gate 		reset_sp((mp_unit_t *)MD_UNIT(mnum), mnum, 0);
14130Sstevel@tonic-gate 	}
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	return (0);
14160Sstevel@tonic-gate }
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate /*
14190Sstevel@tonic-gate  * FUNCTION:	sp_open_dev()
14200Sstevel@tonic-gate  * INPUT:	un	- unit structure.
14210Sstevel@tonic-gate  *		oflags	- open flags.
14220Sstevel@tonic-gate  * OUTPUT:	none.
14230Sstevel@tonic-gate  * RETURNS:	0		- success.
14240Sstevel@tonic-gate  *		non-zero	- err.
14250Sstevel@tonic-gate  * PURPOSE:	open underlying device via md_layered_open.
14260Sstevel@tonic-gate  */
14270Sstevel@tonic-gate static int
14280Sstevel@tonic-gate sp_open_dev(mp_unit_t *un, int oflags)
14290Sstevel@tonic-gate {
14300Sstevel@tonic-gate 	minor_t		mnum = MD_SID(un);
14310Sstevel@tonic-gate 	int		err;
14320Sstevel@tonic-gate 	md_dev64_t	tmpdev;
14330Sstevel@tonic-gate 	set_t		setno = MD_MIN2SET(MD_SID(un));
14340Sstevel@tonic-gate 	side_t		side = mddb_getsidenum(setno);
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	tmpdev = un->un_dev;
14370Sstevel@tonic-gate 	/*
14380Sstevel@tonic-gate 	 * Do the open by device id if underlying is regular
14390Sstevel@tonic-gate 	 */
14400Sstevel@tonic-gate 	if ((md_getmajor(tmpdev) != md_major) &&
14410Sstevel@tonic-gate 		md_devid_found(setno, side, un->un_key) == 1) {
14420Sstevel@tonic-gate 		tmpdev = md_resolve_bydevid(mnum, tmpdev, un->un_key);
14430Sstevel@tonic-gate 	}
14440Sstevel@tonic-gate 	err = md_layered_open(mnum, &tmpdev, oflags);
14450Sstevel@tonic-gate 	un->un_dev = tmpdev;
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	if (err)
14480Sstevel@tonic-gate 		return (ENXIO);
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 	return (0);
14510Sstevel@tonic-gate }
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate /*
14540Sstevel@tonic-gate  * FUNCTION:	sp_open()
14550Sstevel@tonic-gate  * INPUT:	dev		- device to open.
14560Sstevel@tonic-gate  *		flag		- pass-through flag.
14570Sstevel@tonic-gate  *		otyp		- pass-through open type.
14580Sstevel@tonic-gate  *		cred_p		- credentials.
14590Sstevel@tonic-gate  *		md_oflags	- open flags.
14600Sstevel@tonic-gate  * OUTPUT:	none.
14610Sstevel@tonic-gate  * RETURNS:	0		- success.
14620Sstevel@tonic-gate  *		non-zero	- err.
14630Sstevel@tonic-gate  * PURPOSE:	open a soft partition.
14640Sstevel@tonic-gate  */
14650Sstevel@tonic-gate /* ARGSUSED */
14660Sstevel@tonic-gate static int
14670Sstevel@tonic-gate sp_open(
14680Sstevel@tonic-gate 	dev_t		*dev,
14690Sstevel@tonic-gate 	int		flag,
14700Sstevel@tonic-gate 	int		otyp,
14710Sstevel@tonic-gate 	cred_t		*cred_p,
14720Sstevel@tonic-gate 	int		md_oflags
14730Sstevel@tonic-gate )
14740Sstevel@tonic-gate {
14750Sstevel@tonic-gate 	minor_t		mnum = getminor(*dev);
14760Sstevel@tonic-gate 	mdi_unit_t	*ui = MDI_UNIT(mnum);
14770Sstevel@tonic-gate 	mp_unit_t	*un;
14780Sstevel@tonic-gate 	int		err = 0;
14790Sstevel@tonic-gate 	set_t		setno;
14800Sstevel@tonic-gate 
148146Sskamm 	/*
148246Sskamm 	 * When doing an open of a multi owner metadevice, check to see if this
148346Sskamm 	 * node is a starting node and if a reconfig cycle is underway.
148446Sskamm 	 * If so, the system isn't sufficiently set up enough to handle the
148546Sskamm 	 * open (which involves I/O during sp_validate), so fail with ENXIO.
148646Sskamm 	 */
148746Sskamm 	setno = MD_MIN2SET(mnum);
148846Sskamm 	if ((md_set[setno].s_status & (MD_SET_MNSET | MD_SET_MN_START_RC)) ==
148946Sskamm 	    (MD_SET_MNSET | MD_SET_MN_START_RC)) {
149046Sskamm 			return (ENXIO);
149146Sskamm 	}
149246Sskamm 
14930Sstevel@tonic-gate 	/* grab necessary locks */
14940Sstevel@tonic-gate 	un = (mp_unit_t *)md_unit_openclose_enter(ui);
14950Sstevel@tonic-gate 	setno = MD_UN2SET(un);
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 	/* open underlying device, if necessary */
14980Sstevel@tonic-gate 	if (! md_unit_isopen(ui) || (md_oflags & MD_OFLG_PROBEDEV)) {
14990Sstevel@tonic-gate 		if ((err = sp_open_dev(un, md_oflags)) != 0)
15000Sstevel@tonic-gate 			goto out;
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 		if (MD_MNSET_SETNO(setno)) {
15030Sstevel@tonic-gate 			/* For probe, don't incur the overhead of validate */
15040Sstevel@tonic-gate 			if (!(md_oflags & MD_OFLG_PROBEDEV)) {
15050Sstevel@tonic-gate 				/*
15060Sstevel@tonic-gate 				 * Don't call sp_validate while
15070Sstevel@tonic-gate 				 * unit_openclose lock is held.  So, actually
15080Sstevel@tonic-gate 				 * open the device, drop openclose lock,
15090Sstevel@tonic-gate 				 * call sp_validate, reacquire openclose lock,
15100Sstevel@tonic-gate 				 * and close the device.  If sp_validate
15110Sstevel@tonic-gate 				 * succeeds, then device will be re-opened.
15120Sstevel@tonic-gate 				 */
15130Sstevel@tonic-gate 				if ((err = md_unit_incopen(mnum, flag,
15140Sstevel@tonic-gate 				    otyp)) != 0)
15150Sstevel@tonic-gate 					goto out;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 				mutex_enter(&ui->ui_mx);
15180Sstevel@tonic-gate 				ui->ui_lock |= MD_UL_OPENINPROGRESS;
15190Sstevel@tonic-gate 				mutex_exit(&ui->ui_mx);
15200Sstevel@tonic-gate 				md_unit_openclose_exit(ui);
15210Sstevel@tonic-gate 				if (otyp != OTYP_LYR)
15220Sstevel@tonic-gate 					rw_exit(&md_unit_array_rw.lock);
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 				err = sp_validate(un);
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 				if (otyp != OTYP_LYR)
15270Sstevel@tonic-gate 					rw_enter(&md_unit_array_rw.lock,
15280Sstevel@tonic-gate 					    RW_READER);
15290Sstevel@tonic-gate 				(void) md_unit_openclose_enter(ui);
15300Sstevel@tonic-gate 				(void) md_unit_decopen(mnum, otyp);
15310Sstevel@tonic-gate 				mutex_enter(&ui->ui_mx);
15320Sstevel@tonic-gate 				ui->ui_lock &= ~MD_UL_OPENINPROGRESS;
15330Sstevel@tonic-gate 				cv_broadcast(&ui->ui_cv);
15340Sstevel@tonic-gate 				mutex_exit(&ui->ui_mx);
15350Sstevel@tonic-gate 				/*
15360Sstevel@tonic-gate 				 * Should be in the same state as before
15370Sstevel@tonic-gate 				 * the sp_validate.
15380Sstevel@tonic-gate 				 */
15390Sstevel@tonic-gate 				if (err != 0) {
15400Sstevel@tonic-gate 					/* close the device opened above */
15410Sstevel@tonic-gate 					md_layered_close(un->un_dev, md_oflags);
15420Sstevel@tonic-gate 					err = EIO;
15430Sstevel@tonic-gate 					goto out;
15440Sstevel@tonic-gate 				}
15450Sstevel@tonic-gate 			}
15460Sstevel@tonic-gate 			/*
15470Sstevel@tonic-gate 			 * As we're a multi-owner metadevice we need to ensure
15480Sstevel@tonic-gate 			 * that all nodes have the same idea of the status.
15490Sstevel@tonic-gate 			 * sp_validate() will mark the device as errored (if
15500Sstevel@tonic-gate 			 * it cannot read the watermark) or ok (if it was
15510Sstevel@tonic-gate 			 * previously errored but the watermark is now valid).
15520Sstevel@tonic-gate 			 * This code-path is only entered on the non-probe open
15530Sstevel@tonic-gate 			 * so we will maintain the errored state during a probe
15540Sstevel@tonic-gate 			 * call. This means the sys-admin must metarecover -m
15550Sstevel@tonic-gate 			 * to reset the soft-partition error.
15560Sstevel@tonic-gate 			 */
15570Sstevel@tonic-gate 		} else {
15580Sstevel@tonic-gate 			/* For probe, don't incur the overhead of validate */
15590Sstevel@tonic-gate 			if (!(md_oflags & MD_OFLG_PROBEDEV) &&
15600Sstevel@tonic-gate 			    (err = sp_validate(un)) != 0) {
15610Sstevel@tonic-gate 				/* close the device opened above */
15620Sstevel@tonic-gate 				md_layered_close(un->un_dev, md_oflags);
15630Sstevel@tonic-gate 				err = EIO;
15640Sstevel@tonic-gate 				goto out;
15650Sstevel@tonic-gate 			} else {
15660Sstevel@tonic-gate 				/*
15670Sstevel@tonic-gate 				 * we succeeded in validating the on disk
15680Sstevel@tonic-gate 				 * format versus the in core, so reset the
15690Sstevel@tonic-gate 				 * status if it's in error
15700Sstevel@tonic-gate 				 */
15710Sstevel@tonic-gate 				if (un->un_status == MD_SP_ERR) {
15720Sstevel@tonic-gate 					un->un_status = MD_SP_OK;
15730Sstevel@tonic-gate 				}
15740Sstevel@tonic-gate 			}
15750Sstevel@tonic-gate 		}
15760Sstevel@tonic-gate 	}
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 	/* count open */
15790Sstevel@tonic-gate 	if ((err = md_unit_incopen(mnum, flag, otyp)) != 0)
15800Sstevel@tonic-gate 		goto out;
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate out:
15830Sstevel@tonic-gate 	md_unit_openclose_exit(ui);
15840Sstevel@tonic-gate 	return (err);
15850Sstevel@tonic-gate }
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate /*
15880Sstevel@tonic-gate  * FUNCTION:	sp_close()
15890Sstevel@tonic-gate  * INPUT:	dev		- device to close.
15900Sstevel@tonic-gate  *		flag		- pass-through flag.
15910Sstevel@tonic-gate  *		otyp		- pass-through type.
15920Sstevel@tonic-gate  *		cred_p		- credentials.
15930Sstevel@tonic-gate  *		md_cflags	- close flags.
15940Sstevel@tonic-gate  * OUTPUT:	none.
15950Sstevel@tonic-gate  * RETURNS:	0		- success.
15960Sstevel@tonic-gate  *		non-zero	- err.
15970Sstevel@tonic-gate  * PURPOSE:	close a soft paritition.
15980Sstevel@tonic-gate  */
15990Sstevel@tonic-gate /* ARGSUSED */
16000Sstevel@tonic-gate static int
16010Sstevel@tonic-gate sp_close(
16020Sstevel@tonic-gate 	dev_t		dev,
16030Sstevel@tonic-gate 	int		flag,
16040Sstevel@tonic-gate 	int		otyp,
16050Sstevel@tonic-gate 	cred_t		*cred_p,
16060Sstevel@tonic-gate 	int		md_cflags
16070Sstevel@tonic-gate )
16080Sstevel@tonic-gate {
16090Sstevel@tonic-gate 	minor_t		mnum = getminor(dev);
16100Sstevel@tonic-gate 	mdi_unit_t	*ui = MDI_UNIT(mnum);
16110Sstevel@tonic-gate 	mp_unit_t	*un;
16120Sstevel@tonic-gate 	int		err = 0;
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	/* grab necessary locks */
16150Sstevel@tonic-gate 	un = (mp_unit_t *)md_unit_openclose_enter(ui);
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	/* count closed */
16180Sstevel@tonic-gate 	if ((err = md_unit_decopen(mnum, otyp)) != 0)
16190Sstevel@tonic-gate 		goto out;
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	/* close devices, if necessary */
16220Sstevel@tonic-gate 	if (! md_unit_isopen(ui) || (md_cflags & MD_OFLG_PROBEDEV)) {
16230Sstevel@tonic-gate 		md_layered_close(un->un_dev, md_cflags);
16240Sstevel@tonic-gate 	}
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	/*
16270Sstevel@tonic-gate 	 * If a MN set and transient capabilities (eg ABR/DMR) are set,
16280Sstevel@tonic-gate 	 * clear these capabilities if this is the last close in
16290Sstevel@tonic-gate 	 * the cluster
16300Sstevel@tonic-gate 	 */
16310Sstevel@tonic-gate 	if (MD_MNSET_SETNO(MD_UN2SET(un)) &&
16320Sstevel@tonic-gate 	    (ui->ui_tstate & MD_ABR_CAP)) {
16330Sstevel@tonic-gate 		md_unit_openclose_exit(ui);
16340Sstevel@tonic-gate 		mdmn_clear_all_capabilities(mnum);
16350Sstevel@tonic-gate 		return (0);
16360Sstevel@tonic-gate 	}
16370Sstevel@tonic-gate 	/* unlock, return success */
16380Sstevel@tonic-gate out:
16390Sstevel@tonic-gate 	md_unit_openclose_exit(ui);
16400Sstevel@tonic-gate 	return (err);
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate /* used in sp_dump routine */
16450Sstevel@tonic-gate static struct buf dumpbuf;
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate /*
16480Sstevel@tonic-gate  * FUNCTION:	sp_dump()
16490Sstevel@tonic-gate  * INPUT:	dev	- device to dump to.
16500Sstevel@tonic-gate  *		addr	- address to dump.
16510Sstevel@tonic-gate  *		blkno	- blkno on device.
16520Sstevel@tonic-gate  *		nblk	- number of blocks to dump.
16530Sstevel@tonic-gate  * OUTPUT:	none.
16540Sstevel@tonic-gate  * RETURNS:	result from bdev_dump.
16550Sstevel@tonic-gate  * PURPOSE:  This routine dumps memory to the disk.  It assumes that
16560Sstevel@tonic-gate  *           the memory has already been mapped into mainbus space.
16570Sstevel@tonic-gate  *           It is called at disk interrupt priority when the system
16580Sstevel@tonic-gate  *           is in trouble.
16590Sstevel@tonic-gate  *           NOTE: this function is defined using 32-bit arguments,
16600Sstevel@tonic-gate  *           but soft partitioning is internally 64-bit.  Arguments
16610Sstevel@tonic-gate  *           are casted where appropriate.
16620Sstevel@tonic-gate  */
16630Sstevel@tonic-gate static int
16640Sstevel@tonic-gate sp_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
16650Sstevel@tonic-gate {
16660Sstevel@tonic-gate 	mp_unit_t	*un;
16670Sstevel@tonic-gate 	buf_t		*bp;
16680Sstevel@tonic-gate 	sp_ext_length_t	nb;
16690Sstevel@tonic-gate 	daddr_t		mapblk;
16700Sstevel@tonic-gate 	int		result;
16710Sstevel@tonic-gate 	int		more;
16720Sstevel@tonic-gate 	int		saveresult = 0;
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	/*
16750Sstevel@tonic-gate 	 * Don't need to grab the unit lock.
16760Sstevel@tonic-gate 	 * Cause nothing else is supposed to be happenning.
16770Sstevel@tonic-gate 	 * Also dump is not supposed to sleep.
16780Sstevel@tonic-gate 	 */
16790Sstevel@tonic-gate 	un = (mp_unit_t *)MD_UNIT(getminor(dev));
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	if ((diskaddr_t)blkno >= un->c.un_total_blocks)
16820Sstevel@tonic-gate 		return (EINVAL);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	if (((diskaddr_t)blkno + nblk) > un->c.un_total_blocks)
16850Sstevel@tonic-gate 		return (EINVAL);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	bp = &dumpbuf;
16880Sstevel@tonic-gate 	nb = (sp_ext_length_t)dbtob(nblk);
16890Sstevel@tonic-gate 	do {
16900Sstevel@tonic-gate 		bzero((caddr_t)bp, sizeof (*bp));
16910Sstevel@tonic-gate 		more = sp_mapbuf(un, (sp_ext_offset_t)blkno, nb, bp);
16920Sstevel@tonic-gate 		nblk = (int)(btodb(bp->b_bcount));
16930Sstevel@tonic-gate 		mapblk = bp->b_blkno;
16940Sstevel@tonic-gate 		result = bdev_dump(bp->b_edev, addr, mapblk, nblk);
16950Sstevel@tonic-gate 		if (result)
16960Sstevel@tonic-gate 			saveresult = result;
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 		nb -= bp->b_bcount;
16990Sstevel@tonic-gate 		addr += bp->b_bcount;
17000Sstevel@tonic-gate 		blkno += nblk;
17010Sstevel@tonic-gate 	} while (more);
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 	return (saveresult);
17040Sstevel@tonic-gate }
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate static int
17070Sstevel@tonic-gate sp_imp_set(
17080Sstevel@tonic-gate 	set_t	setno
17090Sstevel@tonic-gate )
17100Sstevel@tonic-gate {
17110Sstevel@tonic-gate 	mddb_recid_t	recid;
17120Sstevel@tonic-gate 	int		gotsomething;
17130Sstevel@tonic-gate 	mddb_type_t	rec_type;
17140Sstevel@tonic-gate 	mddb_de_ic_t	*dep;
17150Sstevel@tonic-gate 	mddb_rb32_t	*rbp;
17160Sstevel@tonic-gate 	mp_unit_t	*un64;
17170Sstevel@tonic-gate 	mp_unit32_od_t	*un32;
17181623Stw21770 	md_dev64_t	self_devt;
17190Sstevel@tonic-gate 	minor_t		*self_id;	/* minor needs to be updated */
17200Sstevel@tonic-gate 	md_parent_t	*parent_id;	/* parent needs to be updated */
17210Sstevel@tonic-gate 	mddb_recid_t	*record_id;	/* record id needs to be updated */
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	gotsomething = 0;
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	rec_type = (mddb_type_t)md_getshared_key(setno,
17260Sstevel@tonic-gate 		sp_md_ops.md_driver.md_drivername);
17270Sstevel@tonic-gate 	recid = mddb_makerecid(setno, 0);
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	while ((recid = mddb_getnextrec(recid, rec_type, 0)) > 0) {
17300Sstevel@tonic-gate 		if (mddb_getrecprivate(recid) & MD_PRV_GOTIT)
17310Sstevel@tonic-gate 			continue;
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 		dep = mddb_getrecdep(recid);
17340Sstevel@tonic-gate 		rbp = dep->de_rb;
17350Sstevel@tonic-gate 
17361623Stw21770 		switch (rbp->rb_revision) {
17371623Stw21770 		case MDDB_REV_RB:
17381623Stw21770 		case MDDB_REV_RBFN:
17390Sstevel@tonic-gate 			/*
17400Sstevel@tonic-gate 			 * Small device
17410Sstevel@tonic-gate 			 */
17420Sstevel@tonic-gate 			un32 = (mp_unit32_od_t *)mddb_getrecaddr(recid);
17430Sstevel@tonic-gate 			self_id = &(un32->c.un_self_id);
17440Sstevel@tonic-gate 			parent_id = &(un32->c.un_parent);
17450Sstevel@tonic-gate 			record_id = &(un32->c.un_record_id);
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 			if (!md_update_minor(setno, mddb_getsidenum
17480Sstevel@tonic-gate 				(setno), un32->un_key))
17490Sstevel@tonic-gate 				goto out;
17501623Stw21770 			break;
17511623Stw21770 
17521623Stw21770 		case MDDB_REV_RB64:
17531623Stw21770 		case MDDB_REV_RB64FN:
17540Sstevel@tonic-gate 			un64 = (mp_unit_t *)mddb_getrecaddr(recid);
17550Sstevel@tonic-gate 			self_id = &(un64->c.un_self_id);
17560Sstevel@tonic-gate 			parent_id = &(un64->c.un_parent);
17570Sstevel@tonic-gate 			record_id = &(un64->c.un_record_id);
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 			if (!md_update_minor(setno, mddb_getsidenum
17600Sstevel@tonic-gate 				(setno), un64->un_key))
17610Sstevel@tonic-gate 				goto out;
17621623Stw21770 			break;
17631623Stw21770 		}
17641623Stw21770 
17651623Stw21770 		/*
17661623Stw21770 		 * If this is a top level and a friendly name metadevice,
17671623Stw21770 		 * update its minor in the namespace.
17681623Stw21770 		 */
17691623Stw21770 		if ((*parent_id == MD_NO_PARENT) &&
17701623Stw21770 		    ((rbp->rb_revision == MDDB_REV_RBFN) ||
17711623Stw21770 		    (rbp->rb_revision == MDDB_REV_RB64FN))) {
17721623Stw21770 
17731623Stw21770 			self_devt = md_makedevice(md_major, *self_id);
17741623Stw21770 			if (!md_update_top_device_minor(setno,
17751623Stw21770 			    mddb_getsidenum(setno), self_devt))
17761623Stw21770 				goto out;
17770Sstevel@tonic-gate 		}
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 		/*
17800Sstevel@tonic-gate 		 * Update unit with the imported setno
17810Sstevel@tonic-gate 		 *
17820Sstevel@tonic-gate 		 */
17830Sstevel@tonic-gate 		mddb_setrecprivate(recid, MD_PRV_GOTIT);
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 		*self_id = MD_MKMIN(setno, MD_MIN2UNIT(*self_id));
17860Sstevel@tonic-gate 		if (*parent_id != MD_NO_PARENT)
17870Sstevel@tonic-gate 			*parent_id = MD_MKMIN(setno, MD_MIN2UNIT(*parent_id));
17880Sstevel@tonic-gate 		*record_id = MAKERECID(setno, DBID(*record_id));
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 		gotsomething = 1;
17910Sstevel@tonic-gate 	}
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate out:
17940Sstevel@tonic-gate 	return (gotsomething);
17950Sstevel@tonic-gate }
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate static md_named_services_t sp_named_services[] = {
17980Sstevel@tonic-gate 	{NULL,					0}
17990Sstevel@tonic-gate };
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate md_ops_t sp_md_ops = {
18020Sstevel@tonic-gate 	sp_open,		/* open */
18030Sstevel@tonic-gate 	sp_close,		/* close */
18040Sstevel@tonic-gate 	md_sp_strategy,		/* strategy */
18050Sstevel@tonic-gate 	NULL,			/* print */
18060Sstevel@tonic-gate 	sp_dump,		/* dump */
18070Sstevel@tonic-gate 	NULL,			/* read */
18080Sstevel@tonic-gate 	NULL,			/* write */
18090Sstevel@tonic-gate 	md_sp_ioctl,		/* ioctl, */
18100Sstevel@tonic-gate 	sp_snarf,		/* snarf */
18110Sstevel@tonic-gate 	sp_halt,		/* halt */
18120Sstevel@tonic-gate 	NULL,			/* aread */
18130Sstevel@tonic-gate 	NULL,			/* awrite */
18140Sstevel@tonic-gate 	sp_imp_set,		/* import set */
18150Sstevel@tonic-gate 	sp_named_services
18160Sstevel@tonic-gate };
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate static void
18190Sstevel@tonic-gate init_init()
18200Sstevel@tonic-gate {
18210Sstevel@tonic-gate 	sp_parent_cache = kmem_cache_create("md_softpart_parent",
18220Sstevel@tonic-gate 	    sizeof (md_spps_t), 0, sp_parent_constructor,
18230Sstevel@tonic-gate 	    sp_parent_destructor, sp_run_queue, NULL, NULL, 0);
18240Sstevel@tonic-gate 	sp_child_cache = kmem_cache_create("md_softpart_child",
18250Sstevel@tonic-gate 	    sizeof (md_spcs_t) - sizeof (buf_t) + biosize(), 0,
18260Sstevel@tonic-gate 	    sp_child_constructor, sp_child_destructor, sp_run_queue,
18270Sstevel@tonic-gate 	    NULL, NULL, 0);
18280Sstevel@tonic-gate }
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate static void
18310Sstevel@tonic-gate fini_uninit()
18320Sstevel@tonic-gate {
18330Sstevel@tonic-gate 	kmem_cache_destroy(sp_parent_cache);
18340Sstevel@tonic-gate 	kmem_cache_destroy(sp_child_cache);
18350Sstevel@tonic-gate 	sp_parent_cache = sp_child_cache = NULL;
18360Sstevel@tonic-gate }
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate /* define the module linkage */
18390Sstevel@tonic-gate MD_PLUGIN_MISC_MODULE("soft partition module %I%", init_init(), fini_uninit())
1840