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