xref: /onnv-gate/usr/src/uts/common/io/lvm/trans/mdtrans.c (revision 7627:8599a7568728)
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  */
21*7627SChris.Horne@Sun.COM 
220Sstevel@tonic-gate /*
23*7627SChris.Horne@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/conf.h>
300Sstevel@tonic-gate #include <sys/debug.h>
310Sstevel@tonic-gate #include <sys/file.h>
320Sstevel@tonic-gate #include <sys/user.h>
330Sstevel@tonic-gate #include <sys/uio.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/modctl.h>
450Sstevel@tonic-gate #include <sys/ddi.h>
460Sstevel@tonic-gate #include <sys/sunddi.h>
470Sstevel@tonic-gate #include <sys/disp.h>
480Sstevel@tonic-gate #include <sys/buf.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <sys/lvm/mdvar.h>
510Sstevel@tonic-gate #include <sys/lvm/md_trans.h>
520Sstevel@tonic-gate #include <sys/lvm/md_notify.h>
530Sstevel@tonic-gate #include <sys/lvm/md_convert.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
560Sstevel@tonic-gate #include <sys/sysevent/svm.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate md_ops_t		trans_md_ops;
590Sstevel@tonic-gate #ifndef	lint
601366Spetede char			_depends_on[] = "drv/md fs/ufs";
610Sstevel@tonic-gate md_ops_t		*md_interface_ops = &trans_md_ops;
620Sstevel@tonic-gate #endif	/* lint */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate extern unit_t		md_nunits;
650Sstevel@tonic-gate extern set_t		md_nsets;
660Sstevel@tonic-gate extern md_set_t		md_set[];
670Sstevel@tonic-gate extern int		md_status;
680Sstevel@tonic-gate extern major_t		md_major;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate extern int		md_trans_ioctl();
710Sstevel@tonic-gate extern md_krwlock_t	md_unit_array_rw;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate extern mdq_anchor_t	md_done_daemon;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate extern	int		md_in_upgrade;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static kmem_cache_t	*trans_parent_cache = NULL;
780Sstevel@tonic-gate kmem_cache_t		*trans_child_cache = NULL;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #ifdef	DEBUG
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * ROUTINES FOR TESTING:
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate static int
_init_debug()850Sstevel@tonic-gate _init_debug()
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	extern int	_init_ioctl();
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	return (_init_ioctl());
900Sstevel@tonic-gate }
910Sstevel@tonic-gate static int
_fini_debug()920Sstevel@tonic-gate _fini_debug()
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	extern int	_fini_ioctl();
950Sstevel@tonic-gate 	int	err = 0;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	err = _fini_ioctl();
980Sstevel@tonic-gate 	return (err);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate #endif	/* DEBUG */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * BEGIN RELEASE DEBUG
1050Sstevel@tonic-gate  *	The following routines remain in the released product for testability
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate int
trans_done_shadow(buf_t * bp)1080Sstevel@tonic-gate trans_done_shadow(buf_t *bp)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	buf_t		*pb;
1110Sstevel@tonic-gate 	md_tps_t	*ps = (md_tps_t *)bp->b_chain;
1120Sstevel@tonic-gate 	int		rv = 0;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	pb = ps->ps_bp;
1150Sstevel@tonic-gate 	mutex_enter(&ps->ps_mx);
1160Sstevel@tonic-gate 	ps->ps_count--;
1170Sstevel@tonic-gate 	if (ps->ps_count > 0) {
1180Sstevel@tonic-gate 		if ((bp->b_flags & B_ERROR) != 0) {
1190Sstevel@tonic-gate 			pb->b_flags |= B_ERROR;
1200Sstevel@tonic-gate 			pb->b_error = bp->b_error;
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 		mutex_exit(&ps->ps_mx);
1230Sstevel@tonic-gate 		kmem_cache_free(trans_child_cache, bp);
1240Sstevel@tonic-gate 	} else {
1250Sstevel@tonic-gate 		mutex_exit(&ps->ps_mx);
1260Sstevel@tonic-gate 		mutex_destroy(&ps->ps_mx);
1270Sstevel@tonic-gate 		rv = trans_done(bp);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	return (rv);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate static void
shadow_debug(mt_unit_t * un,buf_t * pb,md_tps_t * ps,buf_t * cb,int flag,void * private)1330Sstevel@tonic-gate shadow_debug(mt_unit_t	*un,		/* trans unit info */
1340Sstevel@tonic-gate 		buf_t	*pb,		/* primary buffer */
1350Sstevel@tonic-gate 		md_tps_t	*ps,		/* trans parent save */
1360Sstevel@tonic-gate 		buf_t	*cb,		/* buffer for writing to master */
1370Sstevel@tonic-gate 		int	flag,
1380Sstevel@tonic-gate 		void	*private)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	buf_t		*sb;		/* Shadow buffer */
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	mutex_init(&ps->ps_mx, NULL, MUTEX_DEFAULT, NULL);
1430Sstevel@tonic-gate 	ps->ps_count = 2;		/* Write child buffer & shadow */
1440Sstevel@tonic-gate 	cb->b_iodone = trans_done_shadow;
1450Sstevel@tonic-gate 	sb = kmem_cache_alloc(trans_child_cache, MD_ALLOCFLAGS);
1460Sstevel@tonic-gate 	trans_child_init(sb);
1470Sstevel@tonic-gate 	sb = bioclone(pb, 0, pb->b_bcount, md_dev64_to_dev(un->un_s_dev),
148*7627SChris.Horne@Sun.COM 	    pb->b_blkno, trans_done_shadow, sb, KM_NOSLEEP);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	sb->b_flags |= B_ASYNC;
1510Sstevel@tonic-gate 	sb->b_chain = (void *)ps;
1520Sstevel@tonic-gate 	md_call_strategy(sb, flag | MD_STR_MAPPED, private);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate  * END RELEASE DEBUG
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * COMMON MEMORY ALLOCATION ROUTINES (so that we can discover leaks)
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate void *
md_trans_zalloc(size_t nb)1620Sstevel@tonic-gate md_trans_zalloc(size_t nb)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	TRANSSTATS(ts_trans_zalloc);
1650Sstevel@tonic-gate 	TRANSSTATSADD(ts_trans_alloced, nb);
1660Sstevel@tonic-gate 	return (kmem_zalloc(nb, KM_SLEEP));
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate void *
md_trans_alloc(size_t nb)1690Sstevel@tonic-gate md_trans_alloc(size_t nb)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	TRANSSTATS(ts_trans_alloc);
1720Sstevel@tonic-gate 	TRANSSTATSADD(ts_trans_alloced, nb);
1730Sstevel@tonic-gate 	return (kmem_alloc(nb, KM_SLEEP));
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate void
md_trans_free(void * va,size_t nb)1760Sstevel@tonic-gate md_trans_free(void *va, size_t nb)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	TRANSSTATS(ts_trans_free);
1790Sstevel@tonic-gate 	TRANSSTATSADD(ts_trans_freed, nb);
1800Sstevel@tonic-gate 	if (nb)
1810Sstevel@tonic-gate 		kmem_free(va, nb);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate static void
trans_parent_init(md_tps_t * ps)1850Sstevel@tonic-gate trans_parent_init(md_tps_t *ps)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	bzero(ps, sizeof (md_tps_t));
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*ARGSUSED1*/
1910Sstevel@tonic-gate int
trans_child_constructor(void * p,void * d1,int d2)1920Sstevel@tonic-gate trans_child_constructor(void *p, void *d1, int d2)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	bioinit(p);
1950Sstevel@tonic-gate 	return (0);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate void
trans_child_init(struct buf * bp)1990Sstevel@tonic-gate trans_child_init(struct buf *bp)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate 	md_bioreset(bp);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate /*ARGSUSED1*/
2050Sstevel@tonic-gate void
trans_child_destructor(void * p,void * d)2060Sstevel@tonic-gate trans_child_destructor(void *p, void *d)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	biofini(p);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate void
trans_commit(mt_unit_t * un,int domstr)2120Sstevel@tonic-gate trans_commit(mt_unit_t *un, int domstr)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	mddb_recid_t	recids[4];
2150Sstevel@tonic-gate 	md_unit_t	*su;
2160Sstevel@tonic-gate 	int		ri = 0;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (md_get_setstatus(MD_UN2SET(un)) & MD_SET_STALE)
2190Sstevel@tonic-gate 		return;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	recids[ri++] = un->c.un_record_id;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (domstr)
2240Sstevel@tonic-gate 		if (md_getmajor(un->un_m_dev) == md_major) {
2250Sstevel@tonic-gate 			su = MD_UNIT(md_getminor(un->un_m_dev));
2260Sstevel@tonic-gate 			recids[ri++] = su->c.un_record_id;
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	if (ri == 0)
2300Sstevel@tonic-gate 		return;
2310Sstevel@tonic-gate 	recids[ri] = 0;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	uniqtime32(&un->un_timestamp);
2340Sstevel@tonic-gate 	mddb_commitrecs_wrapper(recids);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate void
trans_close_all_devs(mt_unit_t * un)2380Sstevel@tonic-gate trans_close_all_devs(mt_unit_t *un)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	if ((un->un_flags & TRANS_NEED_OPEN) == 0) {
2410Sstevel@tonic-gate 		md_layered_close(un->un_m_dev, MD_OFLG_NULL);
2420Sstevel@tonic-gate 		if (un->un_l_unit)
2430Sstevel@tonic-gate 			ldl_close_dev(un->un_l_unit);
2440Sstevel@tonic-gate 		un->un_flags |= TRANS_NEED_OPEN;
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate int
trans_open_all_devs(mt_unit_t * un)2490Sstevel@tonic-gate trans_open_all_devs(mt_unit_t *un)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	int		err;
2520Sstevel@tonic-gate 	minor_t		mnum = MD_SID(un);
2530Sstevel@tonic-gate 	md_dev64_t	tmpdev = un->un_m_dev;
2540Sstevel@tonic-gate 	set_t		setno = MD_MIN2SET(MD_SID(un));
2550Sstevel@tonic-gate 	side_t		side = mddb_getsidenum(setno);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * Do the open by device id if it is regular device
2590Sstevel@tonic-gate 	 */
2600Sstevel@tonic-gate 	if ((md_getmajor(tmpdev) != md_major) &&
261*7627SChris.Horne@Sun.COM 	    md_devid_found(setno, side, un->un_m_key) == 1) {
2620Sstevel@tonic-gate 		tmpdev = md_resolve_bydevid(mnum, tmpdev, un->un_m_key);
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 	err = md_layered_open(mnum, &tmpdev, MD_OFLG_NULL);
2650Sstevel@tonic-gate 	un->un_m_dev = tmpdev;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	if (err)
2680Sstevel@tonic-gate 		return (ENXIO);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (un->un_l_unit) {
2710Sstevel@tonic-gate 		err = ldl_open_dev(un, un->un_l_unit);
2720Sstevel@tonic-gate 		if (err) {
2730Sstevel@tonic-gate 			md_layered_close(tmpdev, MD_OFLG_NULL);
2740Sstevel@tonic-gate 			return (ENXIO);
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 	return (0);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate uint_t	mt_debug	= 0;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate int
trans_build_incore(void * p,int snarfing)2830Sstevel@tonic-gate trans_build_incore(void *p, int snarfing)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	mt_unit_t	*un = (mt_unit_t *)p;
2860Sstevel@tonic-gate 	minor_t		mnum;
2870Sstevel@tonic-gate 	set_t		setno;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	/*
2900Sstevel@tonic-gate 	 * initialize debug mode and always start with no shadowing.
2910Sstevel@tonic-gate 	 */
2920Sstevel@tonic-gate 	if (!snarfing)
2930Sstevel@tonic-gate 		un->un_debug = mt_debug;
2940Sstevel@tonic-gate 	un->un_s_dev = NODEV64;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	mnum = MD_SID(un);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	if (MD_UNIT(mnum) != NULL)
2990Sstevel@tonic-gate 		return (0);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	setno = MD_MIN2SET(mnum);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/*
3040Sstevel@tonic-gate 	 * If snarfing the metatrans device,
3050Sstevel@tonic-gate 	 *	then remake the device number
3060Sstevel@tonic-gate 	 */
3070Sstevel@tonic-gate 	if (snarfing) {
3080Sstevel@tonic-gate 		un->un_m_dev =  md_getdevnum(setno, mddb_getsidenum(setno),
309*7627SChris.Horne@Sun.COM 		    un->un_m_key, MD_NOTRUST_DEVT);
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	/*
3130Sstevel@tonic-gate 	 * db rec is partially deleted; finish the db delete later
3140Sstevel@tonic-gate 	 */
3150Sstevel@tonic-gate 	if (MD_STATUS(un) & MD_UN_BEING_RESET) {
3160Sstevel@tonic-gate 		mddb_setrecprivate(un->c.un_record_id, MD_PRV_PENDCLEAN);
3170Sstevel@tonic-gate 		return (1);
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * With the current device id implementation there is possibility
3220Sstevel@tonic-gate 	 * that we may have NODEV if the underlying can't be resolved at
3230Sstevel@tonic-gate 	 * snarf time.  If this is the case we want to be consistent with
3240Sstevel@tonic-gate 	 * the normal behavior and continue to allow the snarf of unit
3250Sstevel@tonic-gate 	 * and resolve the devt at the open time
3260Sstevel@tonic-gate 	 */
3270Sstevel@tonic-gate 	if ((md_getmajor(un->un_m_dev) == md_major) &&
328*7627SChris.Horne@Sun.COM 	    (md_dev_exists(un->un_m_dev) == 0)) {
3290Sstevel@tonic-gate 		return (1);
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/*
3330Sstevel@tonic-gate 	 * retain the detach status; reset open status
3340Sstevel@tonic-gate 	 */
3350Sstevel@tonic-gate 	un->un_flags &= (TRANS_DETACHING | TRANS_DETACHED);
3360Sstevel@tonic-gate 	un->un_flags |= TRANS_NEED_OPEN;
3370Sstevel@tonic-gate 	if ((un->un_flags & TRANS_DETACHED) == 0)
3380Sstevel@tonic-gate 		un->un_flags |= TRANS_ATTACHING;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	/*
3410Sstevel@tonic-gate 	 * log device not set up yet; try again later
3420Sstevel@tonic-gate 	 */
3430Sstevel@tonic-gate 	if ((un->un_flags & TRANS_DETACHED) == 0)
3440Sstevel@tonic-gate 		if (ldl_findlog(un->un_l_recid) == NULL)
3450Sstevel@tonic-gate 			return (1);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/*
3480Sstevel@tonic-gate 	 * initialize incore fields
3490Sstevel@tonic-gate 	 */
3500Sstevel@tonic-gate 	un->un_next = NULL;
3510Sstevel@tonic-gate 	un->un_l_unit = NULL;
3520Sstevel@tonic-gate 	un->un_deltamap = NULL;
3530Sstevel@tonic-gate 	un->un_udmap = NULL;
3540Sstevel@tonic-gate 	un->un_logmap = NULL;
3550Sstevel@tonic-gate 	un->un_matamap = NULL;
3560Sstevel@tonic-gate 	un->un_shadowmap = NULL;
3570Sstevel@tonic-gate 	un->un_ut = NULL;
3580Sstevel@tonic-gate 	un->un_logreset = 0;
3590Sstevel@tonic-gate 	un->un_dev = md_makedevice(md_major, mnum);
3600Sstevel@tonic-gate 	MD_STATUS(un) = 0;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	/* necessary because capability didn't exist pre-4.1 */
3630Sstevel@tonic-gate 	MD_CAPAB(un) = (MD_CAN_META_CHILD & ~MD_CAN_PARENT);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/*
3660Sstevel@tonic-gate 	 * attach the log
3670Sstevel@tonic-gate 	 */
3680Sstevel@tonic-gate 	trans_attach(un, 0);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	/*
3710Sstevel@tonic-gate 	 * check for master dev dynconcat
3720Sstevel@tonic-gate 	 */
3730Sstevel@tonic-gate 	if (md_getmajor(un->un_m_dev) == md_major) {
3740Sstevel@tonic-gate 		struct mdc_unit	*c;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 		c = MD_UNIT(md_getminor(un->un_m_dev));
3770Sstevel@tonic-gate 		un->c.un_total_blocks = c->un_total_blocks;
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
380*7627SChris.Horne@Sun.COM 	/* place various information in the in-core data structures */
381*7627SChris.Horne@Sun.COM 	md_nblocks_set(mnum, un->c.un_total_blocks);
3820Sstevel@tonic-gate 	MD_UNIT(mnum) = un;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	return (0);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate int
trans_detach(mt_unit_t * un,int force)3880Sstevel@tonic-gate trans_detach(mt_unit_t *un, int force)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	mdi_unit_t	*ui = MDI_UNIT(MD_SID(un));
3910Sstevel@tonic-gate 	int		error	= 0;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	/*
3940Sstevel@tonic-gate 	 * The caller is responsible for single-threading this routine.
3950Sstevel@tonic-gate 	 */
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (ui == NULL)
3980Sstevel@tonic-gate 		return (0);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	/*
4010Sstevel@tonic-gate 	 * already detached or the log isn't attached yet; do nothing
4020Sstevel@tonic-gate 	 */
4030Sstevel@tonic-gate 	if (un->un_flags & (TRANS_DETACHED | TRANS_ATTACHING))
4040Sstevel@tonic-gate 		return (0);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	/*
4070Sstevel@tonic-gate 	 * set state to detaching
4080Sstevel@tonic-gate 	 */
4090Sstevel@tonic-gate 	if (force || !md_unit_isopen(ui)) {
4100Sstevel@tonic-gate 		un->un_flags |= TRANS_DETACHING;
4110Sstevel@tonic-gate 		if (!MD_UPGRADE) {
4120Sstevel@tonic-gate 			trans_commit(un, 0);
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 		SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_DETACHING, TAG_METADEVICE,
4150Sstevel@tonic-gate 		    MD_UN2SET(un), MD_SID(un));
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	/*
4190Sstevel@tonic-gate 	 * device is busy
4200Sstevel@tonic-gate 	 */
4210Sstevel@tonic-gate 	if (md_unit_isopen(ui))
4220Sstevel@tonic-gate 		return (EBUSY);
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	/*
4250Sstevel@tonic-gate 	 * detach the log
4260Sstevel@tonic-gate 	 *	if successful
4270Sstevel@tonic-gate 	 *		flags committed to TRANS_DETACHED in database
4280Sstevel@tonic-gate 	 *		un->un_l_unit set to NULL
4290Sstevel@tonic-gate 	 *		no error returned
4300Sstevel@tonic-gate 	 */
4310Sstevel@tonic-gate 	error = ldl_reset(un, 1, force);
4320Sstevel@tonic-gate 	if (error)
4330Sstevel@tonic-gate 		return (error);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	/*
4360Sstevel@tonic-gate 	 * commit to database
4370Sstevel@tonic-gate 	 */
4380Sstevel@tonic-gate 	if (!MD_UPGRADE) {
4390Sstevel@tonic-gate 		trans_commit(un, 0);
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 	SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_DETACH, TAG_METADEVICE, MD_UN2SET(un),
4420Sstevel@tonic-gate 	    MD_SID(un));
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	return (0);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate void
trans_attach(mt_unit_t * un,int attaching)4480Sstevel@tonic-gate trans_attach(mt_unit_t *un, int attaching)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	mdi_unit_t	*ui = MDI_UNIT(MD_SID(un));
4510Sstevel@tonic-gate 	ml_unit_t	*ul;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	/*
4540Sstevel@tonic-gate 	 * called from snarf, set, and attach.  Hence, the attaching param
4550Sstevel@tonic-gate 	 * The caller is responsible for single-threading this routine.
4560Sstevel@tonic-gate 	 */
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	/*
4590Sstevel@tonic-gate 	 * not attaching; do nothing
4600Sstevel@tonic-gate 	 */
4610Sstevel@tonic-gate 	if ((un->un_flags & TRANS_ATTACHING) == 0)
4620Sstevel@tonic-gate 		return;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	/*
4650Sstevel@tonic-gate 	 * find log unit struct
4660Sstevel@tonic-gate 	 */
4670Sstevel@tonic-gate 	ul = ldl_findlog(un->un_l_recid);
4680Sstevel@tonic-gate 	if (ul == NULL)
4690Sstevel@tonic-gate 		return;
4700Sstevel@tonic-gate 	un->un_l_dev = ul->un_dev;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	/*
4730Sstevel@tonic-gate 	 * device is busy; do nothing
4740Sstevel@tonic-gate 	 */
4750Sstevel@tonic-gate 	if (attaching && md_unit_isopen(ui))
4760Sstevel@tonic-gate 		return;
4770Sstevel@tonic-gate 	/*
4780Sstevel@tonic-gate 	 * other functions use non-NULL un_l_unit as detach/attach flag
4790Sstevel@tonic-gate 	 */
4800Sstevel@tonic-gate 	un->un_l_unit = ul;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	/*
4830Sstevel@tonic-gate 	 *   add metatrans device to the log's list of mt devices
4840Sstevel@tonic-gate 	 */
4850Sstevel@tonic-gate 	ldl_utadd(un);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/*
4880Sstevel@tonic-gate 	 * attached
4890Sstevel@tonic-gate 	 */
4900Sstevel@tonic-gate 	un->un_flags &= ~TRANS_ATTACHING;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate int
trans_reset(mt_unit_t * un,minor_t mnum,int removing,int force)4950Sstevel@tonic-gate trans_reset(mt_unit_t *un, minor_t mnum, int removing, int force)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate 	sv_dev_t	sv;
4980Sstevel@tonic-gate 	mddb_recid_t	vtoc_id;
4990Sstevel@tonic-gate 	int		error	= 0;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	/*
5020Sstevel@tonic-gate 	 * reset log, maps, and ufs interface
5030Sstevel@tonic-gate 	 */
5040Sstevel@tonic-gate 	error = ldl_reset(un, removing, force);
5050Sstevel@tonic-gate 	if (error)
5060Sstevel@tonic-gate 		return (error);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	/*
5090Sstevel@tonic-gate 	 * done with underyling devices
5100Sstevel@tonic-gate 	 */
5110Sstevel@tonic-gate 	trans_close_all_devs(un);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	md_destroy_unit_incore(mnum, &trans_md_ops);
5140Sstevel@tonic-gate 
515*7627SChris.Horne@Sun.COM 	md_nblocks_set(mnum, -1ULL);
5160Sstevel@tonic-gate 	MD_UNIT(mnum) = NULL;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	if (!removing)
5190Sstevel@tonic-gate 		return (0);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	md_reset_parent(un->un_m_dev);
5220Sstevel@tonic-gate 	MD_STATUS(un) |= MD_UN_BEING_RESET;
5230Sstevel@tonic-gate 	trans_commit(un, 1);
5240Sstevel@tonic-gate 	SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_DELETE, TAG_METADEVICE, MD_UN2SET(un),
5250Sstevel@tonic-gate 	    MD_SID(un));
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/* Save the mstr key */
5280Sstevel@tonic-gate 	sv.setno = MD_MIN2SET(mnum);
5290Sstevel@tonic-gate 	sv.key = un->un_m_key;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	vtoc_id = un->c.un_vtoc_id;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	mddb_deleterec_wrapper(un->c.un_record_id);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	/* Remove the vtoc, if present */
5360Sstevel@tonic-gate 	if (vtoc_id)
5370Sstevel@tonic-gate 		mddb_deleterec_wrapper(vtoc_id);
5380Sstevel@tonic-gate 	md_rem_names(&sv, 1);
5390Sstevel@tonic-gate 	return (0);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate static void
trans_wait_panic(struct buf * cb)5430Sstevel@tonic-gate trans_wait_panic(struct buf *cb)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	while ((cb->b_flags & B_DONE) == 0) {
5460Sstevel@tonic-gate 		md_daemon(1, &md_done_daemon);
5470Sstevel@tonic-gate 		drv_usecwait(10);
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate static void
trans_error(md_tps_t * ps)5520Sstevel@tonic-gate trans_error(md_tps_t *ps)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate 	md_dev64_t	md_dev;
5550Sstevel@tonic-gate 	md_dev64_t	m_dev;
5560Sstevel@tonic-gate 	char		*str;
5570Sstevel@tonic-gate 	struct buf	*pb;
5580Sstevel@tonic-gate 	mdi_unit_t	*ui;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	pb = ps->ps_bp;
5610Sstevel@tonic-gate 	ui = ps->ps_ui;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	/*
5640Sstevel@tonic-gate 	 * gather up params for cmn_err
5650Sstevel@tonic-gate 	 */
5660Sstevel@tonic-gate 	if (pb->b_flags & B_READ)
5670Sstevel@tonic-gate 		str = "read";
5680Sstevel@tonic-gate 	else
5690Sstevel@tonic-gate 		str = "write";
5700Sstevel@tonic-gate 	md_dev = md_expldev(pb->b_edev);
5710Sstevel@tonic-gate 	m_dev = ps->ps_un->un_m_dev;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	/*
5740Sstevel@tonic-gate 	 * free up the resources for this request and done the errored buf
5750Sstevel@tonic-gate 	 */
5760Sstevel@tonic-gate 	md_kstat_done(ui, pb, 0);
5770Sstevel@tonic-gate 	kmem_cache_free(trans_parent_cache, ps);
5780Sstevel@tonic-gate 	md_unit_readerexit(ui);
5790Sstevel@tonic-gate 	md_biodone(pb);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	/*
5820Sstevel@tonic-gate 	 * print pretty error message
5830Sstevel@tonic-gate 	 */
5840Sstevel@tonic-gate 	cmn_err(CE_WARN, "md: %s: %s error on %s",
5850Sstevel@tonic-gate 	    md_shortname(md_getminor(md_dev)), str,
5860Sstevel@tonic-gate 	    md_devname(MD_DEV2SET(md_dev), m_dev, NULL, 0));
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate int
trans_done(struct buf * cb)5900Sstevel@tonic-gate trans_done(struct buf *cb)
5910Sstevel@tonic-gate {
5920Sstevel@tonic-gate 	struct buf	*pb;
5930Sstevel@tonic-gate 	mdi_unit_t	*ui;
5940Sstevel@tonic-gate 	md_tps_t	*ps;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	ps = (md_tps_t *)cb->b_chain;
5970Sstevel@tonic-gate 	pb = ps->ps_bp;
5980Sstevel@tonic-gate 	ui = ps->ps_ui;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	if (cb->b_flags & B_ERROR) {
6010Sstevel@tonic-gate 		pb->b_flags |= B_ERROR;
6020Sstevel@tonic-gate 		pb->b_error = cb->b_error;
6030Sstevel@tonic-gate 		/*
6040Sstevel@tonic-gate 		 * device not in hard error state; report error
6050Sstevel@tonic-gate 		 */
6060Sstevel@tonic-gate 		if (!ldl_isherror(ps->ps_un->un_l_unit)) {
6070Sstevel@tonic-gate 			daemon_request(&md_done_daemon, trans_error,
608*7627SChris.Horne@Sun.COM 			    (daemon_queue_t *)ps, REQ_OLD);
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 			if (cb->b_flags & B_REMAPPED)
6110Sstevel@tonic-gate 				bp_mapout(cb);
6120Sstevel@tonic-gate 			if (panicstr)
6130Sstevel@tonic-gate 				cb->b_flags |= B_DONE;
6140Sstevel@tonic-gate 			else
6150Sstevel@tonic-gate 				kmem_cache_free(trans_child_cache, cb);
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 			return (1);
6180Sstevel@tonic-gate 		}
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	if (cb->b_flags & B_REMAPPED)
6220Sstevel@tonic-gate 		bp_mapout(cb);
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	if (panicstr)
6250Sstevel@tonic-gate 		cb->b_flags |= B_DONE;
6260Sstevel@tonic-gate 	else
6270Sstevel@tonic-gate 		kmem_cache_free(trans_child_cache, cb);
6280Sstevel@tonic-gate 	kmem_cache_free(trans_parent_cache, ps);
6290Sstevel@tonic-gate 	md_kstat_done(ui, pb, 0);
6300Sstevel@tonic-gate 	md_unit_readerexit(ui);
6310Sstevel@tonic-gate 	md_biodone(pb);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	return (0);
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate static void
md_trans_strategy(buf_t * pb,int flag,void * private)6370Sstevel@tonic-gate md_trans_strategy(buf_t *pb, int flag, void *private)
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate 	md_tps_t	*ps;
6400Sstevel@tonic-gate 	buf_t		*cb;		/* child buf pointer */
6410Sstevel@tonic-gate 	mt_unit_t	*un;
6420Sstevel@tonic-gate 	mdi_unit_t	*ui;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	ui = MDI_UNIT(getminor(pb->b_edev));
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	md_kstat_waitq_enter(ui);
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	un = (mt_unit_t *)md_unit_readerlock(ui);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	if (md_inc_iocount(MD_MIN2SET(getminor(pb->b_edev))) != 0) {
6510Sstevel@tonic-gate 		pb->b_flags |= B_ERROR;
6520Sstevel@tonic-gate 		pb->b_error = ENXIO;
6530Sstevel@tonic-gate 		pb->b_resid = pb->b_bcount;
6542150Sjeanm 		md_kstat_waitq_exit(ui);
6550Sstevel@tonic-gate 		md_unit_readerexit(ui);
6560Sstevel@tonic-gate 		biodone(pb);
6570Sstevel@tonic-gate 		return;
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	ASSERT(!(flag & MD_STR_NOTTOP));
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	/* check and map */
6630Sstevel@tonic-gate 	if (md_checkbuf(ui, (md_unit_t *)un, pb) != 0) {
6640Sstevel@tonic-gate 		md_kstat_waitq_exit(ui);
6650Sstevel@tonic-gate 		return;
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	bp_mapin(pb);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	ps = kmem_cache_alloc(trans_parent_cache, MD_ALLOCFLAGS);
6710Sstevel@tonic-gate 	trans_parent_init(ps);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * Save essential information from the original buffhdr
6750Sstevel@tonic-gate 	 * in the md_save structure.
6760Sstevel@tonic-gate 	 */
6770Sstevel@tonic-gate 	ps->ps_un = un;
6780Sstevel@tonic-gate 	ps->ps_ui = ui;
6790Sstevel@tonic-gate 	ps->ps_bp = pb;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	cb = kmem_cache_alloc(trans_child_cache, MD_ALLOCFLAGS);
6820Sstevel@tonic-gate 	trans_child_init(cb);
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	cb = bioclone(pb, 0, pb->b_bcount, md_dev64_to_dev(un->un_m_dev),
685*7627SChris.Horne@Sun.COM 	    pb->b_blkno, trans_done, cb, KM_NOSLEEP);
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	cb->b_chain = (void *)ps;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	/*
6900Sstevel@tonic-gate 	 * RELEASE DEBUG
6910Sstevel@tonic-gate 	 * The following calls shadow debug for testing purposes if we are
6920Sstevel@tonic-gate 	 * writing and if shadowing is turned on.
6930Sstevel@tonic-gate 	 */
6940Sstevel@tonic-gate 	if ((un->un_s_dev != NODEV64) &&
6950Sstevel@tonic-gate 	    ((pb->b_flags & B_READ) == 0))
6960Sstevel@tonic-gate 		shadow_debug(un, pb, ps, cb, flag, private);
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	md_kstat_waitq_to_runq(ui);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	(void) md_call_strategy(cb, flag | MD_STR_MAPPED | MD_NOBLOCK, private);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	/*
7030Sstevel@tonic-gate 	 * panic in progress; process daemon queues
7040Sstevel@tonic-gate 	 */
7050Sstevel@tonic-gate 	if (panicstr) {
7060Sstevel@tonic-gate 		trans_wait_panic(cb);
7070Sstevel@tonic-gate 		kmem_cache_free(trans_child_cache, cb);
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate /* ARGSUSED */
7120Sstevel@tonic-gate static int
md_trans_read(dev_t dev,struct uio * uio,cred_t * credp)7130Sstevel@tonic-gate md_trans_read(dev_t dev, struct uio *uio, cred_t *credp)
7140Sstevel@tonic-gate {
7150Sstevel@tonic-gate 	int			error;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	if ((error = md_chk_uio(uio)) != 0)
7180Sstevel@tonic-gate 		return (error);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate /* ARGSUSED */
7240Sstevel@tonic-gate static int
md_trans_aread(dev_t dev,struct aio_req * aio,cred_t * credp)7250Sstevel@tonic-gate md_trans_aread(dev_t dev, struct aio_req *aio, cred_t *credp)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate 	int			error;
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	if ((error = md_chk_uio(aio->aio_uio)) != 0)
7300Sstevel@tonic-gate 		return (error);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	return (aphysio(mdstrategy, anocancel, dev, B_READ, minphys, aio));
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate /* ARGSUSED */
7360Sstevel@tonic-gate static int
md_trans_write(dev_t dev,struct uio * uio,cred_t * credp)7370Sstevel@tonic-gate md_trans_write(dev_t dev, struct uio *uio, cred_t *credp)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	int	error;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	if ((error = md_chk_uio(uio)) != 0)
7420Sstevel@tonic-gate 		return (error);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate /* ARGSUSED */
7480Sstevel@tonic-gate static int
md_trans_awrite(dev_t dev,struct aio_req * aio,cred_t * credp)7490Sstevel@tonic-gate md_trans_awrite(dev_t dev, struct aio_req *aio, cred_t *credp)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate 	int	error;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if ((error = md_chk_uio(aio->aio_uio)) != 0)
7540Sstevel@tonic-gate 		return (error);
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	return (aphysio(mdstrategy, anocancel, dev, B_WRITE, minphys, aio));
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate static void
trans_cleanup(mt_unit_t * un)7600Sstevel@tonic-gate trans_cleanup(mt_unit_t *un)
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate 	sv_dev_t	sv;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	MD_STATUS(un) |= MD_UN_LOG_DELETED;
7650Sstevel@tonic-gate 	trans_commit(un, 0);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	/* Save the mstr key */
7680Sstevel@tonic-gate 	sv.setno = MD_UN2SET(un);
7690Sstevel@tonic-gate 	sv.key = un->un_m_key;
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	mddb_deleterec_wrapper(un->c.un_record_id);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	md_rem_names(&sv, 1);
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate static int
trans_snarf(md_snarfcmd_t cmd,set_t setno)7770Sstevel@tonic-gate trans_snarf(md_snarfcmd_t cmd, set_t setno)
7780Sstevel@tonic-gate {
7790Sstevel@tonic-gate 	mt_unit_t	*un;
7800Sstevel@tonic-gate 	ml_unit_t	*ul;
7810Sstevel@tonic-gate 	mddb_recid_t	recid;
7820Sstevel@tonic-gate 	int		gotsomething;
7830Sstevel@tonic-gate 	mddb_type_t	typ1;
7840Sstevel@tonic-gate 	int		all_trans_gotten;
7850Sstevel@tonic-gate 	mddb_de_ic_t    *dep;
7860Sstevel@tonic-gate 	mddb_rb32_t	*rbp;
7870Sstevel@tonic-gate 	size_t		newreqsize;
7880Sstevel@tonic-gate 	static int	trans_found = 0;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	if (cmd == MD_SNARF_CLEANUP) {
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 		if (md_get_setstatus(setno) & MD_SET_STALE)
7950Sstevel@tonic-gate 			return (0);
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 		/*
7980Sstevel@tonic-gate 		 * clean up partially cleared trans devices
7990Sstevel@tonic-gate 		 */
8000Sstevel@tonic-gate 		typ1 = (mddb_type_t)md_getshared_key(setno,
8010Sstevel@tonic-gate 		    trans_md_ops.md_driver.md_drivername);
8020Sstevel@tonic-gate 		recid = mddb_makerecid(setno, 0);
8030Sstevel@tonic-gate 		while ((recid = mddb_getnextrec(recid, typ1, TRANS_REC)) > 0) {
8040Sstevel@tonic-gate 			un = (mt_unit_t *)mddb_getrecaddr(recid);
8050Sstevel@tonic-gate 			(void) trans_detach(un, 1);
8060Sstevel@tonic-gate 			if (mddb_getrecprivate(recid) & MD_PRV_CLEANUP) {
8070Sstevel@tonic-gate 				trans_cleanup(un);
8080Sstevel@tonic-gate 				recid = mddb_makerecid(setno, 0);
8090Sstevel@tonic-gate 			}
8100Sstevel@tonic-gate 		}
8110Sstevel@tonic-gate 		/*
8120Sstevel@tonic-gate 		 * clean up partially cleared log devices
8130Sstevel@tonic-gate 		 */
8140Sstevel@tonic-gate 		recid = mddb_makerecid(setno, 0);
8150Sstevel@tonic-gate 		while ((recid = mddb_getnextrec(recid, typ1, LOG_REC)) > 0) {
8160Sstevel@tonic-gate 			if (mddb_getrecprivate(recid) & MD_PRV_CLEANUP) {
8170Sstevel@tonic-gate 				ul = (ml_unit_t *)mddb_getrecaddr(recid);
8180Sstevel@tonic-gate 				ldl_cleanup(ul);
8190Sstevel@tonic-gate 				recid = mddb_makerecid(setno, 0);
8200Sstevel@tonic-gate 			}
8210Sstevel@tonic-gate 		}
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 		return (0);
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	/*
8270Sstevel@tonic-gate 	 * must snarf up the log devices first
8280Sstevel@tonic-gate 	 */
8290Sstevel@tonic-gate 	gotsomething = 0;
8300Sstevel@tonic-gate 	all_trans_gotten = 1;
8310Sstevel@tonic-gate 	typ1 = (mddb_type_t)md_getshared_key(setno,
8320Sstevel@tonic-gate 	    trans_md_ops.md_driver.md_drivername);
8330Sstevel@tonic-gate 	recid = mddb_makerecid(setno, 0);
8340Sstevel@tonic-gate 	while ((recid = mddb_getnextrec(recid, typ1, LOG_REC)) > 0) {
8350Sstevel@tonic-gate 		ml_unit_t	*big_ul;
8360Sstevel@tonic-gate 		ml_unit32_od_t	*small_ul;
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		if (mddb_getrecprivate(recid) & MD_PRV_GOTIT)
8390Sstevel@tonic-gate 			continue;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 		small_ul = (ml_unit32_od_t *)mddb_getrecaddr(recid);
8420Sstevel@tonic-gate 		dep = mddb_getrecdep(recid);
8430Sstevel@tonic-gate 		dep->de_flags = MDDB_F_TRANS_LOG;
8440Sstevel@tonic-gate 		rbp = dep->de_rb;
8450Sstevel@tonic-gate 		/*
8460Sstevel@tonic-gate 		 * As trans records are always old records,
8470Sstevel@tonic-gate 		 * we have to check if this record already has been converted.
8480Sstevel@tonic-gate 		 * We don't want to do that work twice.
8490Sstevel@tonic-gate 		 */
8500Sstevel@tonic-gate 		if ((rbp->rb_private & MD_PRV_CONVD) == 0) {
8510Sstevel@tonic-gate 			newreqsize = sizeof (ml_unit_t);
8520Sstevel@tonic-gate 			big_ul = (ml_unit_t *)kmem_zalloc(newreqsize, KM_SLEEP);
8530Sstevel@tonic-gate 			trans_log_convert((caddr_t)small_ul, (caddr_t)big_ul,
854*7627SChris.Horne@Sun.COM 			    SMALL_2_BIG);
8550Sstevel@tonic-gate 			kmem_free(small_ul, dep->de_reqsize);
8560Sstevel@tonic-gate 			/*
8570Sstevel@tonic-gate 			 * Update userdata and incore userdata
8580Sstevel@tonic-gate 			 * incores are at the end of ul
8590Sstevel@tonic-gate 			 */
8600Sstevel@tonic-gate 			dep->de_rb_userdata_ic = big_ul;
8610Sstevel@tonic-gate 			dep->de_rb_userdata = big_ul;
8620Sstevel@tonic-gate 			dep->de_icreqsize = newreqsize;
8630Sstevel@tonic-gate 			rbp->rb_private |= MD_PRV_CONVD;
8640Sstevel@tonic-gate 			ul = big_ul;
8650Sstevel@tonic-gate 		} else {
8660Sstevel@tonic-gate 			/* already converted, just set the pointer */
8670Sstevel@tonic-gate 			ul = dep->de_rb_userdata;
8680Sstevel@tonic-gate 		}
8690Sstevel@tonic-gate 		all_trans_gotten = 0;
8700Sstevel@tonic-gate 		if (ldl_build_incore(ul, 1) == 0) {
8710Sstevel@tonic-gate 			mddb_setrecprivate(recid, MD_PRV_GOTIT);
8720Sstevel@tonic-gate 			gotsomething = 1;
8730Sstevel@tonic-gate 		}
8740Sstevel@tonic-gate 	}
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	/*
8770Sstevel@tonic-gate 	 * now snarf up metatrans devices
8780Sstevel@tonic-gate 	 */
8790Sstevel@tonic-gate 	gotsomething = 0;
8800Sstevel@tonic-gate 	recid = mddb_makerecid(setno, 0);
8810Sstevel@tonic-gate 	while ((recid = mddb_getnextrec(recid, typ1, TRANS_REC)) > 0) {
8820Sstevel@tonic-gate 		mt_unit_t	*big_un;
8830Sstevel@tonic-gate 		mt_unit32_od_t	*small_un;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 		if (mddb_getrecprivate(recid) & MD_PRV_GOTIT)
8860Sstevel@tonic-gate 			continue;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 		if ((trans_found == 0) && (!MD_UPGRADE)) {
8890Sstevel@tonic-gate 			cmn_err(CE_WARN, MD_EOF_TRANS_MSG MD_EOF_TRANS_WARNING);
8900Sstevel@tonic-gate 			trans_found = 1;
8910Sstevel@tonic-gate 		}
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 		small_un = (mt_unit32_od_t *)mddb_getrecaddr(recid);
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 		dep = mddb_getrecdep(recid);
8960Sstevel@tonic-gate 		dep->de_flags = MDDB_F_TRANS_MASTER;
8970Sstevel@tonic-gate 		rbp = dep->de_rb;
8980Sstevel@tonic-gate 		/*
8990Sstevel@tonic-gate 		 * As trans records are always old records,
9000Sstevel@tonic-gate 		 * we have to check if this record already has been converted.
9010Sstevel@tonic-gate 		 * We don't want to do that work twice.
9020Sstevel@tonic-gate 		 */
9030Sstevel@tonic-gate 		if ((rbp->rb_private & MD_PRV_CONVD) == 0) {
9040Sstevel@tonic-gate 			newreqsize = sizeof (mt_unit_t);
9050Sstevel@tonic-gate 			big_un = (mt_unit_t *)kmem_zalloc(newreqsize, KM_SLEEP);
9060Sstevel@tonic-gate 			trans_master_convert((caddr_t)small_un, (caddr_t)big_un,
907*7627SChris.Horne@Sun.COM 			    SMALL_2_BIG);
9080Sstevel@tonic-gate 			kmem_free(small_un, dep->de_reqsize);
9090Sstevel@tonic-gate 			/*
9100Sstevel@tonic-gate 			 * Update userdata and incore userdata
9110Sstevel@tonic-gate 			 * incores are at the end of ul
9120Sstevel@tonic-gate 			 */
9130Sstevel@tonic-gate 			dep->de_rb_userdata_ic = big_un;
9140Sstevel@tonic-gate 			dep->de_rb_userdata = big_un;
9150Sstevel@tonic-gate 			dep->de_icreqsize = newreqsize;
9160Sstevel@tonic-gate 			rbp->rb_private |= MD_PRV_CONVD;
9170Sstevel@tonic-gate 			un = big_un;
9181623Stw21770 			un->c.un_revision &= ~MD_64BIT_META_DEV;
9190Sstevel@tonic-gate 		} else {
9200Sstevel@tonic-gate 			/* already converted, just set the pointer */
9210Sstevel@tonic-gate 			un = dep->de_rb_userdata;
9220Sstevel@tonic-gate 		}
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 		/*
9250Sstevel@tonic-gate 		 * Create minor node for snarfed entry.
9260Sstevel@tonic-gate 		 */
9270Sstevel@tonic-gate 		(void) md_create_minor_node(MD_MIN2SET(MD_SID(un)), MD_SID(un));
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 		if (MD_UNIT(MD_SID(un)) != NULL) {
9300Sstevel@tonic-gate 			mddb_setrecprivate(recid, MD_PRV_PENDDEL);
9310Sstevel@tonic-gate 			continue;
9320Sstevel@tonic-gate 		}
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 		all_trans_gotten = 0;
9350Sstevel@tonic-gate 		if (trans_build_incore(un, 1) == 0) {
9360Sstevel@tonic-gate 			mddb_setrecprivate(recid, MD_PRV_GOTIT);
9370Sstevel@tonic-gate 			md_create_unit_incore(MD_SID(un), &trans_md_ops, 0);
9380Sstevel@tonic-gate 			gotsomething = 1;
9390Sstevel@tonic-gate 		}
9400Sstevel@tonic-gate 	}
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	if (!all_trans_gotten)
9430Sstevel@tonic-gate 		return (gotsomething);
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	recid = mddb_makerecid(setno, 0);
9460Sstevel@tonic-gate 	while ((recid = mddb_getnextrec(recid, typ1, 0)) > 0)
9470Sstevel@tonic-gate 		if (!(mddb_getrecprivate(recid) & MD_PRV_GOTIT))
9480Sstevel@tonic-gate 			mddb_setrecprivate(recid, MD_PRV_PENDDEL);
9490Sstevel@tonic-gate 	return (0);
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate static int
trans_halt(md_haltcmd_t cmd,set_t setno)9530Sstevel@tonic-gate trans_halt(md_haltcmd_t cmd, set_t setno)
9540Sstevel@tonic-gate {
9550Sstevel@tonic-gate 	unit_t		i;
9560Sstevel@tonic-gate 	mdi_unit_t	*ui;
9570Sstevel@tonic-gate 	minor_t		mnum;
9580Sstevel@tonic-gate 	mt_unit_t	*un;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	if (cmd == MD_HALT_CLOSE) {
9610Sstevel@tonic-gate 		for (i = 0; i < md_nunits; i++) {
9620Sstevel@tonic-gate 			mnum = MD_MKMIN(setno, i);
9630Sstevel@tonic-gate 			if ((ui = MDI_UNIT(mnum)) == NULL)
9640Sstevel@tonic-gate 				continue;
9650Sstevel@tonic-gate 			if (ui->ui_opsindex != trans_md_ops.md_selfindex)
9660Sstevel@tonic-gate 				continue;
9670Sstevel@tonic-gate 			if (md_unit_isopen(ui)) {
9680Sstevel@tonic-gate 				return (1);
9690Sstevel@tonic-gate 			}
9700Sstevel@tonic-gate 		}
9710Sstevel@tonic-gate 		for (i = 0; i < md_nunits; i++) {
9720Sstevel@tonic-gate 			mnum = MD_MKMIN(setno, i);
9730Sstevel@tonic-gate 			if ((ui = MDI_UNIT(mnum)) == NULL)
9740Sstevel@tonic-gate 				continue;
9750Sstevel@tonic-gate 			if (ui->ui_opsindex != trans_md_ops.md_selfindex)
9760Sstevel@tonic-gate 				continue;
9770Sstevel@tonic-gate 			un = (mt_unit_t *)MD_UNIT(mnum);
9780Sstevel@tonic-gate 			if ((un->un_flags & TRANS_NEED_OPEN) == 0) {
9790Sstevel@tonic-gate 				trans_close_all_devs(un);
9800Sstevel@tonic-gate 			}
9810Sstevel@tonic-gate 		}
9820Sstevel@tonic-gate 		return (0);
9830Sstevel@tonic-gate 	}
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	if (cmd == MD_HALT_OPEN) {
9860Sstevel@tonic-gate 		for (i = 0; i < md_nunits; i++) {
9870Sstevel@tonic-gate 			mnum = MD_MKMIN(setno, i);
9880Sstevel@tonic-gate 			if ((ui = MDI_UNIT(mnum)) == NULL)
9890Sstevel@tonic-gate 				continue;
9900Sstevel@tonic-gate 			if (ui->ui_opsindex != trans_md_ops.md_selfindex)
9910Sstevel@tonic-gate 				continue;
9920Sstevel@tonic-gate 			ldl_open_underlying((mt_unit_t *)MD_UNIT(mnum));
9930Sstevel@tonic-gate 		}
9940Sstevel@tonic-gate 		return (0);
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	if (cmd == MD_HALT_CHECK) {
9980Sstevel@tonic-gate 		for (i = 0; i < md_nunits; i++) {
9990Sstevel@tonic-gate 			mnum = MD_MKMIN(setno, i);
10000Sstevel@tonic-gate 			if ((ui = MDI_UNIT(mnum)) == NULL)
10010Sstevel@tonic-gate 				continue;
10020Sstevel@tonic-gate 			if (ui->ui_opsindex != trans_md_ops.md_selfindex)
10030Sstevel@tonic-gate 				continue;
10040Sstevel@tonic-gate 			if (md_unit_isopen(ui)) {
10050Sstevel@tonic-gate 				return (1);
10060Sstevel@tonic-gate 			}
10070Sstevel@tonic-gate 		}
10080Sstevel@tonic-gate 		return (0);
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 	if (cmd == MD_HALT_DOIT) {
10110Sstevel@tonic-gate 		for (i = 0; i < md_nunits; i++) {
10120Sstevel@tonic-gate 			mnum = MD_MKMIN(setno, i);
10130Sstevel@tonic-gate 			if ((ui = MDI_UNIT(mnum)) == NULL)
10140Sstevel@tonic-gate 				continue;
10150Sstevel@tonic-gate 			if (ui->ui_opsindex != trans_md_ops.md_selfindex)
10160Sstevel@tonic-gate 				continue;
10170Sstevel@tonic-gate 			(void) trans_reset((mt_unit_t *)MD_UNIT(mnum), mnum,
10180Sstevel@tonic-gate 			    0, 1);
10190Sstevel@tonic-gate 		}
10200Sstevel@tonic-gate 		return (0);
10210Sstevel@tonic-gate 	}
10220Sstevel@tonic-gate 	if (cmd == MD_HALT_UNLOAD)
10230Sstevel@tonic-gate 		return (0);
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	return (1);
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate /*ARGSUSED3*/
10290Sstevel@tonic-gate static int
trans_open(dev_t * dev,int flag,int otyp,cred_t * cred_p,int md_oflags)10300Sstevel@tonic-gate trans_open(
10310Sstevel@tonic-gate 	dev_t		*dev,
10320Sstevel@tonic-gate 	int		flag,
10330Sstevel@tonic-gate 	int		otyp,
10340Sstevel@tonic-gate 	cred_t		*cred_p,
10350Sstevel@tonic-gate 	int		md_oflags
10360Sstevel@tonic-gate )
10370Sstevel@tonic-gate {
10380Sstevel@tonic-gate 	minor_t		mnum = getminor(*dev);
10390Sstevel@tonic-gate 	mdi_unit_t	*ui = MDI_UNIT(mnum);
10400Sstevel@tonic-gate 	mt_unit_t	*un;
10410Sstevel@tonic-gate 	int		err;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	/* disallow layered opens (e.g., PrestoServe) */
10440Sstevel@tonic-gate 	if (otyp == OTYP_LYR)
10450Sstevel@tonic-gate 		return (EINVAL);
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	/* single thread */
10480Sstevel@tonic-gate 	un = (mt_unit_t *)md_unit_openclose_enter(ui);
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	/* if already open, count open, return success */
10510Sstevel@tonic-gate 	if (md_unit_isopen(ui)) {
10520Sstevel@tonic-gate 		err = md_unit_incopen(mnum, flag, otyp);
10530Sstevel@tonic-gate 		md_unit_openclose_exit(ui);
10540Sstevel@tonic-gate 		if (err != 0)
10550Sstevel@tonic-gate 			return (err);
10560Sstevel@tonic-gate 		return (0);
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	/*
10600Sstevel@tonic-gate 	 * For some reason, not all of the metatrans devices attached to
10610Sstevel@tonic-gate 	 * this log were openable at snarf;  try again now.  All of the
10620Sstevel@tonic-gate 	 * underlying devices have to be openable for the roll thread to work.
10630Sstevel@tonic-gate 	 */
10640Sstevel@tonic-gate 	if (un->un_flags & TRANS_NEED_OPEN) {
10650Sstevel@tonic-gate 		md_unit_openclose_exit(ui);
10660Sstevel@tonic-gate 		ldl_open_underlying(un);
10670Sstevel@tonic-gate 		if (un->un_flags & TRANS_NEED_OPEN)
10680Sstevel@tonic-gate 			return (EINVAL);
10690Sstevel@tonic-gate 		un = (mt_unit_t *)md_unit_openclose_enter(ui);
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	/* count open */
10730Sstevel@tonic-gate 	err = md_unit_incopen(mnum, flag, otyp);
10740Sstevel@tonic-gate 	md_unit_openclose_exit(ui);
10750Sstevel@tonic-gate 	if (err != 0)
10760Sstevel@tonic-gate 		return (err);
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	/* return success */
10790Sstevel@tonic-gate 	return (0);
10800Sstevel@tonic-gate }
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate /*ARGSUSED1*/
10830Sstevel@tonic-gate static int
trans_close(dev_t dev,int flag,int otyp,cred_t * cred_p,int md_oflags)10840Sstevel@tonic-gate trans_close(
10850Sstevel@tonic-gate 	dev_t		dev,
10860Sstevel@tonic-gate 	int		flag,
10870Sstevel@tonic-gate 	int		otyp,
10880Sstevel@tonic-gate 	cred_t		*cred_p,
10890Sstevel@tonic-gate 	int		md_oflags
10900Sstevel@tonic-gate )
10910Sstevel@tonic-gate {
10920Sstevel@tonic-gate 	minor_t		mnum = getminor(dev);
10930Sstevel@tonic-gate 	mdi_unit_t	*ui = MDI_UNIT(mnum);
10940Sstevel@tonic-gate 	mt_unit_t	*un;
10950Sstevel@tonic-gate 	int		err = 0;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	/* single thread */
10980Sstevel@tonic-gate 	un = (mt_unit_t *)md_unit_openclose_enter(ui);
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	/* count closed */
11010Sstevel@tonic-gate 	if ((err = md_unit_decopen(mnum, otyp)) != 0) {
11020Sstevel@tonic-gate 		md_unit_openclose_exit(ui);
11030Sstevel@tonic-gate 		return (err);
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	/* if still open */
11070Sstevel@tonic-gate 	if (md_unit_isopen(ui)) {
11080Sstevel@tonic-gate 		md_unit_openclose_exit(ui);
11090Sstevel@tonic-gate 		return (0);
11100Sstevel@tonic-gate 	}
11110Sstevel@tonic-gate 	md_unit_openclose_exit(ui);
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	if (un->un_flags & TRANS_DETACHING) {
11140Sstevel@tonic-gate 		/*
11150Sstevel@tonic-gate 		 * prevent new opens and try to detach the log
11160Sstevel@tonic-gate 		 */
11170Sstevel@tonic-gate 		rw_enter(&md_unit_array_rw.lock, RW_WRITER);
11180Sstevel@tonic-gate 		(void) trans_detach(un, 0);
11190Sstevel@tonic-gate 		rw_exit(&md_unit_array_rw.lock);
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate 	if (un->un_flags & TRANS_ATTACHING) {
11220Sstevel@tonic-gate 		/*
11230Sstevel@tonic-gate 		 * prevent new opens and try to attach the log
11240Sstevel@tonic-gate 		 */
11250Sstevel@tonic-gate 		rw_enter(&md_unit_array_rw.lock, RW_WRITER);
11260Sstevel@tonic-gate 		trans_attach(un, 1);
11270Sstevel@tonic-gate 		rw_exit(&md_unit_array_rw.lock);
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	return (0);
11310Sstevel@tonic-gate }
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate static int
trans_imp_set(set_t setno)11340Sstevel@tonic-gate trans_imp_set(
11350Sstevel@tonic-gate 	set_t	setno
11360Sstevel@tonic-gate )
11370Sstevel@tonic-gate {
11380Sstevel@tonic-gate 	mt_unit32_od_t	*un32;
11390Sstevel@tonic-gate 	ml_unit32_od_t	*ul32;
11400Sstevel@tonic-gate 	mddb_recid_t	recid;
11410Sstevel@tonic-gate 	int		gotsomething = 0;
11420Sstevel@tonic-gate 	mddb_type_t	typ1;
11430Sstevel@tonic-gate 	minor_t		*self_id;	/* minor needs to be updated */
11440Sstevel@tonic-gate 	mddb_recid_t	*record_id;	/* record id needs to be updated */
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	/*
11470Sstevel@tonic-gate 	 * Do log first if there is any
11480Sstevel@tonic-gate 	 * Note that trans record is always 32 bit
11490Sstevel@tonic-gate 	 */
11500Sstevel@tonic-gate 	typ1 = (mddb_type_t)md_getshared_key(setno,
11510Sstevel@tonic-gate 	    trans_md_ops.md_driver.md_drivername);
11520Sstevel@tonic-gate 	recid = mddb_makerecid(setno, 0);
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	while ((recid = mddb_getnextrec(recid, typ1, LOG_REC)) > 0) {
11550Sstevel@tonic-gate 		if (mddb_getrecprivate(recid) & MD_PRV_GOTIT)
11560Sstevel@tonic-gate 			continue;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 		ul32 = (ml_unit32_od_t *)mddb_getrecaddr(recid);
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 		/*
11610Sstevel@tonic-gate 		 * Trans log record always is old format
11620Sstevel@tonic-gate 		 * Go ahead update the record with the new set info
11630Sstevel@tonic-gate 		 */
11640Sstevel@tonic-gate 		record_id = &(ul32->un_recid);
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 		/*
11670Sstevel@tonic-gate 		 * Mark the record and update it
11680Sstevel@tonic-gate 		 */
11690Sstevel@tonic-gate 		*record_id = MAKERECID(setno, DBID(*record_id));
11700Sstevel@tonic-gate 		if (!md_update_minor(setno, mddb_getsidenum
1171*7627SChris.Horne@Sun.COM 		    (setno), ul32->un_key))
11720Sstevel@tonic-gate 			goto out;
11730Sstevel@tonic-gate 		mddb_setrecprivate(recid, MD_PRV_GOTIT);
11740Sstevel@tonic-gate 	}
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	/*
11780Sstevel@tonic-gate 	 * Now do the master
11790Sstevel@tonic-gate 	 */
11800Sstevel@tonic-gate 	recid = mddb_makerecid(setno, 0);
11810Sstevel@tonic-gate 	while ((recid = mddb_getnextrec(recid, typ1, TRANS_REC)) > 0) {
11820Sstevel@tonic-gate 		if (mddb_getrecprivate(recid) & MD_PRV_GOTIT)
11830Sstevel@tonic-gate 			continue;
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 		un32 = (mt_unit32_od_t *)mddb_getrecaddr(recid);
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 		/*
11880Sstevel@tonic-gate 		 * Trans master record always is old format
11890Sstevel@tonic-gate 		 */
11900Sstevel@tonic-gate 		self_id = &(un32->c.un_self_id);
11910Sstevel@tonic-gate 		record_id = &(un32->c.un_record_id);
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 		/*
11940Sstevel@tonic-gate 		 * Mark the record and update it
11950Sstevel@tonic-gate 		 */
11960Sstevel@tonic-gate 		*record_id = MAKERECID(setno, DBID(*record_id));
11970Sstevel@tonic-gate 		*self_id = MD_MKMIN(setno, MD_MIN2UNIT(*self_id));
11980Sstevel@tonic-gate 		if (!md_update_minor(setno, mddb_getsidenum
1199*7627SChris.Horne@Sun.COM 		    (setno), un32->un_m_key))
12000Sstevel@tonic-gate 			goto out;
12010Sstevel@tonic-gate 		mddb_setrecprivate(recid, MD_PRV_GOTIT);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 		gotsomething = 1;
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate out:
12070Sstevel@tonic-gate 	return (gotsomething);
12080Sstevel@tonic-gate }
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate static md_named_services_t	trans_named_services[] = {
12110Sstevel@tonic-gate 	{(intptr_t (*)()) trans_rename_listkids,	MDRNM_LIST_URKIDS   },
12120Sstevel@tonic-gate 	{(intptr_t (*)()) trans_rename_check,		MDRNM_CHECK	    },
12130Sstevel@tonic-gate 	{(intptr_t (*)()) trans_renexch_update_kids,	MDRNM_UPDATE_KIDS   },
12140Sstevel@tonic-gate 	{(intptr_t (*)()) trans_rename_update_self,	MDRNM_UPDATE_SELF   },
12150Sstevel@tonic-gate 	{(intptr_t (*)()) trans_exchange_self_update_from_down,
12160Sstevel@tonic-gate 						MDRNM_SELF_UPDATE_FROM_DOWN },
12170Sstevel@tonic-gate 	{(intptr_t (*)()) trans_exchange_parent_update_to,
12180Sstevel@tonic-gate 						MDRNM_PARENT_UPDATE_TO	    },
12190Sstevel@tonic-gate 	{NULL,						0		    }
12200Sstevel@tonic-gate };
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate md_ops_t trans_md_ops = {
12230Sstevel@tonic-gate 	trans_open,		/* open */
12240Sstevel@tonic-gate 	trans_close,		/* close */
12250Sstevel@tonic-gate 	md_trans_strategy,	/* strategy */
12260Sstevel@tonic-gate 	NULL,			/* print */
12270Sstevel@tonic-gate 	NULL,			/* dump */
12280Sstevel@tonic-gate 	md_trans_read,		/* read */
12290Sstevel@tonic-gate 	md_trans_write,		/* write */
12300Sstevel@tonic-gate 	md_trans_ioctl,		/* trans ioctl */
12310Sstevel@tonic-gate 	trans_snarf,		/* trans_snarf */
12320Sstevel@tonic-gate 	trans_halt,		/* halt */
12330Sstevel@tonic-gate 	md_trans_aread,		/* aread */
12340Sstevel@tonic-gate 	md_trans_awrite,	/* awrite */
12350Sstevel@tonic-gate 	trans_imp_set,		/* import set */
12360Sstevel@tonic-gate 	trans_named_services
12370Sstevel@tonic-gate };
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate static void
init_init(void)12400Sstevel@tonic-gate init_init(void)
12410Sstevel@tonic-gate {
12420Sstevel@tonic-gate 	_init_ldl();
12430Sstevel@tonic-gate 	ASSERT(_init_debug());
12440Sstevel@tonic-gate 	trans_parent_cache = kmem_cache_create("md_trans_parent",
12450Sstevel@tonic-gate 	    sizeof (md_tps_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
12460Sstevel@tonic-gate 	trans_child_cache = kmem_cache_create("md_trans_child", biosize(), 0,
12470Sstevel@tonic-gate 	    trans_child_constructor, trans_child_destructor,
12480Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
12490Sstevel@tonic-gate }
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate static void
fini_uninit(void)12520Sstevel@tonic-gate fini_uninit(void)
12530Sstevel@tonic-gate {
12540Sstevel@tonic-gate 	ASSERT(_fini_debug());
12550Sstevel@tonic-gate 	_fini_ldl();
12560Sstevel@tonic-gate 	kmem_cache_destroy(trans_parent_cache);
12570Sstevel@tonic-gate 	kmem_cache_destroy(trans_child_cache);
12580Sstevel@tonic-gate 	trans_parent_cache = trans_child_cache = NULL;
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate /* define the module linkage */
12624932Spetede MD_PLUGIN_MISC_MODULE("trans module", init_init(), fini_uninit())
1263