xref: /onnv-gate/usr/src/uts/common/io/lvm/stripe/stripe_ioctl.c (revision 9017:47960a78ed2a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51623Stw21770  * Common Development and Distribution License (the "License").
61623Stw21770  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
217563SPrasad.Singamsetty@Sun.COM 
220Sstevel@tonic-gate /*
23*9017SJohn.Wren.Kennedy@Sun.COM  * Copyright 2009 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/file.h>
310Sstevel@tonic-gate #include <sys/user.h>
320Sstevel@tonic-gate #include <sys/uio.h>
330Sstevel@tonic-gate #include <sys/t_lock.h>
340Sstevel@tonic-gate #include <sys/buf.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/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>
441623Stw21770 #include <sys/lvm/mdvar.h>
450Sstevel@tonic-gate #include <sys/lvm/md_stripe.h>
460Sstevel@tonic-gate #include <sys/lvm/md_notify.h>
470Sstevel@tonic-gate #include <sys/modctl.h>
480Sstevel@tonic-gate #include <sys/ddi.h>
490Sstevel@tonic-gate #include <sys/sunddi.h>
500Sstevel@tonic-gate #include <sys/debug.h>
510Sstevel@tonic-gate #include <sys/model.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
540Sstevel@tonic-gate #include <sys/sysevent/svm.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate extern int		md_status;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate extern unit_t		md_nunits;
590Sstevel@tonic-gate extern set_t		md_nsets;
600Sstevel@tonic-gate extern md_set_t		md_set[];
610Sstevel@tonic-gate 
620Sstevel@tonic-gate extern md_ops_t		stripe_md_ops;
630Sstevel@tonic-gate extern md_krwlock_t	md_unit_array_rw;
640Sstevel@tonic-gate extern major_t		md_major;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static int
stripe_replace(replace_params_t * params)670Sstevel@tonic-gate stripe_replace(replace_params_t *params)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	minor_t		mnum = params->mnum;
700Sstevel@tonic-gate 	ms_unit_t	*un;
710Sstevel@tonic-gate 	mddb_recid_t	recids[6];
720Sstevel@tonic-gate 	ms_new_dev_t	nd;
730Sstevel@tonic-gate 	ms_cd_info_t	cd;
747563SPrasad.Singamsetty@Sun.COM 	int		ci;
757563SPrasad.Singamsetty@Sun.COM 	int		cmpcnt;
760Sstevel@tonic-gate 	void		*repl_data;
770Sstevel@tonic-gate 	md_dev64_t	fake_devt;
780Sstevel@tonic-gate 	void		(*repl_done)();
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	mdclrerror(&params->mde);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	un = (ms_unit_t *)MD_UNIT(mnum);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	if (MD_STATUS(un) & MD_UN_RESYNC_ACTIVE) {
850Sstevel@tonic-gate 		return (mdmderror(&params->mde, MDE_RESYNC_ACTIVE, mnum));
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	nd.nd_dev = params->new_dev;
890Sstevel@tonic-gate 	nd.nd_key = params->new_key;
900Sstevel@tonic-gate 	nd.nd_nblks = params->number_blks;
910Sstevel@tonic-gate 	nd.nd_start_blk = params->start_blk;
920Sstevel@tonic-gate 	nd.nd_labeled = params->has_label;
930Sstevel@tonic-gate 	nd.nd_hs_id = 0;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	/*
960Sstevel@tonic-gate 	 * stripe_component_count and stripe_get_dev only care about the
970Sstevel@tonic-gate 	 * minor number associated with the first argument which is a
980Sstevel@tonic-gate 	 * md_dev64_t
990Sstevel@tonic-gate 	 *
1000Sstevel@tonic-gate 	 * The comments section for these two routines have been updated
1010Sstevel@tonic-gate 	 * to indicate that this routine calls with fake major numbers.
1020Sstevel@tonic-gate 	 */
1030Sstevel@tonic-gate 	fake_devt = md_makedevice(0, mnum);
1040Sstevel@tonic-gate 	cmpcnt = stripe_component_count(fake_devt, NULL);
1050Sstevel@tonic-gate 	for (ci = 0; ci < cmpcnt; ci++) {
1060Sstevel@tonic-gate 		(void) stripe_get_dev(fake_devt, NULL, ci, &cd);
1070Sstevel@tonic-gate 		if ((cd.cd_dev == params->old_dev) ||
1080Sstevel@tonic-gate 		    (cd.cd_orig_dev == params->old_dev))
1090Sstevel@tonic-gate 			break;
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 	if (ci == cmpcnt) {
1120Sstevel@tonic-gate 		return (EINVAL);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	/*  In case of a dryrun we're done here */
1160Sstevel@tonic-gate 	if (params->options & MDIOCTL_DRYRUN) {
1170Sstevel@tonic-gate 		return (0);
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	(void) stripe_replace_dev(fake_devt, 0, ci, &nd, recids, 6,
1210Sstevel@tonic-gate 	    &repl_done, &repl_data);
1220Sstevel@tonic-gate 	mddb_commitrecs_wrapper(recids);
1230Sstevel@tonic-gate 	(*repl_done)(fake_devt, repl_data);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_REPLACE, SVM_TAG_METADEVICE,
1260Sstevel@tonic-gate 	    MD_UN2SET(un), MD_SID(un));
1270Sstevel@tonic-gate 	return (0);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static int
stripe_set(void * d,int mode)1310Sstevel@tonic-gate stripe_set(void *d, int mode)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	minor_t		mnum;
1340Sstevel@tonic-gate 	ms_unit_t	*un;
1350Sstevel@tonic-gate 	void		*p;
1360Sstevel@tonic-gate 	mddb_recid_t	ms_recid;
1370Sstevel@tonic-gate 	mddb_recid_t	*recids;
1380Sstevel@tonic-gate 	mddb_type_t	typ1;
1390Sstevel@tonic-gate 	int		err;
1400Sstevel@tonic-gate 	set_t		setno;
1410Sstevel@tonic-gate 	md_error_t	*mdep;
1420Sstevel@tonic-gate 	struct ms_comp	*mdcomp;
1430Sstevel@tonic-gate 	int		row;
1440Sstevel@tonic-gate 	int		rid;
1450Sstevel@tonic-gate 	int		num_recs;
1460Sstevel@tonic-gate 	int		i, c;
1470Sstevel@tonic-gate 	md_set_params_t	*msp = d;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	mnum = msp->mnum;
1500Sstevel@tonic-gate 	setno = MD_MIN2SET(mnum);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	mdep = &msp->mde;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	mdclrerror(mdep);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if ((setno >= md_nsets) || (MD_MIN2UNIT(mnum) >= md_nunits)) {
1570Sstevel@tonic-gate 		return (mdmderror(mdep, MDE_INVAL_UNIT, mnum));
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if (md_get_setstatus(setno) & MD_SET_STALE)
1610Sstevel@tonic-gate 		return (mdmddberror(mdep, MDE_DB_STALE, mnum, setno));
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	un = MD_UNIT(mnum);
1640Sstevel@tonic-gate 	if (un != NULL) {
1650Sstevel@tonic-gate 		return (mdmderror(mdep, MDE_UNIT_ALREADY_SETUP, mnum));
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	typ1 = (mddb_type_t)md_getshared_key(setno,
1700Sstevel@tonic-gate 	    stripe_md_ops.md_driver.md_drivername);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/* create the db record for this mdstruct */
1730Sstevel@tonic-gate 	if (msp->options & MD_CRO_64BIT) {
1740Sstevel@tonic-gate #if defined(_ILP32)
1750Sstevel@tonic-gate 		return (mdmderror(mdep, MDE_UNIT_TOO_LARGE, mnum));
1760Sstevel@tonic-gate #else
1770Sstevel@tonic-gate 		ms_recid = mddb_createrec((size_t)msp->size, typ1, 0,
1787563SPrasad.Singamsetty@Sun.COM 		    MD_CRO_64BIT | MD_CRO_STRIPE | MD_CRO_FN, setno);
1790Sstevel@tonic-gate #endif
1800Sstevel@tonic-gate 	} else {
1810Sstevel@tonic-gate 		ms_recid = mddb_createrec((size_t)msp->size, typ1, 0,
1827563SPrasad.Singamsetty@Sun.COM 		    MD_CRO_32BIT | MD_CRO_STRIPE | MD_CRO_FN, setno);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 	if (ms_recid < 0)
1850Sstevel@tonic-gate 		return (mddbstatus2error(mdep, ms_recid, mnum, setno));
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	/* get the address of the mdstruct */
1880Sstevel@tonic-gate 	p = (void *) mddb_getrecaddr(ms_recid);
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * It is okay that we muck with the mdstruct here,
1910Sstevel@tonic-gate 	 * since no one else will know about the mdstruct
1920Sstevel@tonic-gate 	 * until we commit it. If we crash, the record will
1930Sstevel@tonic-gate 	 * be automatically purged, since we haven't
1940Sstevel@tonic-gate 	 * committed it yet.
1950Sstevel@tonic-gate 	 */
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/* copy in the user's mdstruct */
1980Sstevel@tonic-gate 	if (err = ddi_copyin((caddr_t)(uintptr_t)msp->mdp, (caddr_t)p,
1990Sstevel@tonic-gate 	    (size_t)msp->size, mode)) {
2000Sstevel@tonic-gate 		mddb_deleterec_wrapper(ms_recid);
2010Sstevel@tonic-gate 		return (EFAULT);
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	un = (ms_unit_t *)p;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	/* All 64 bit metadevices only support EFI labels. */
2070Sstevel@tonic-gate 	if (msp->options & MD_CRO_64BIT) {
2080Sstevel@tonic-gate 		un->c.un_flag |= MD_EFILABEL;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/*
2120Sstevel@tonic-gate 	 * allocate the real recids array.  since we may have to commit
2130Sstevel@tonic-gate 	 * underlying metadevice records, we need an array
2140Sstevel@tonic-gate 	 * of size: total number of components in stripe + 3
2150Sstevel@tonic-gate 	 * (1 for the stripe itself, one for the hotspare, one
2160Sstevel@tonic-gate 	 * for the end marker).
2170Sstevel@tonic-gate 	 */
2180Sstevel@tonic-gate 	num_recs = 3;
2190Sstevel@tonic-gate 	rid = 0;
2200Sstevel@tonic-gate 	for (row = 0; row < un->un_nrows; row++) {
2210Sstevel@tonic-gate 		struct ms_row *mdr = &un->un_row[row];
2220Sstevel@tonic-gate 		num_recs += mdr->un_ncomp;
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 	recids = kmem_alloc(num_recs * sizeof (mddb_recid_t), KM_SLEEP);
2250Sstevel@tonic-gate 	recids[rid++] = ms_recid;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	MD_SID(un) = mnum;
2280Sstevel@tonic-gate 	MD_RECID(un) = recids[0];
2290Sstevel@tonic-gate 	MD_CAPAB(un) = MD_CAN_PARENT | MD_CAN_SUB_MIRROR | MD_CAN_SP;
2300Sstevel@tonic-gate 	MD_PARENT(un) = MD_NO_PARENT;
2311623Stw21770 	un->c.un_revision |= MD_FN_META_DEV;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if (err = stripe_build_incore(p, 0)) {
2347627SChris.Horne@Sun.COM 		md_nblocks_set(mnum, -1ULL);
2350Sstevel@tonic-gate 		MD_UNIT(mnum) = NULL;
2367627SChris.Horne@Sun.COM 
2370Sstevel@tonic-gate 		mddb_deleterec_wrapper(recids[0]);
2380Sstevel@tonic-gate 		kmem_free(recids, num_recs * sizeof (mddb_recid_t));
2390Sstevel@tonic-gate 		return (err);
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2421623Stw21770 	/*
2431623Stw21770 	 * Update unit availability
2441623Stw21770 	 */
2451623Stw21770 	md_set[setno].s_un_avail--;
2461623Stw21770 
2470Sstevel@tonic-gate 	recids[rid] = 0;
2480Sstevel@tonic-gate 	if (un->un_hsp_id != -1)
2490Sstevel@tonic-gate 		err = md_hot_spare_ifc(HSP_INCREF, un->un_hsp_id, 0, 0,
2500Sstevel@tonic-gate 		    &recids[rid++], NULL, NULL, NULL);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if (err) {
2547627SChris.Horne@Sun.COM 		md_nblocks_set(mnum, -1ULL);
2550Sstevel@tonic-gate 		MD_UNIT(mnum) = NULL;
2567627SChris.Horne@Sun.COM 
2570Sstevel@tonic-gate 		mddb_deleterec_wrapper(recids[0]);
2580Sstevel@tonic-gate 		kmem_free(recids, num_recs * sizeof (mddb_recid_t));
2590Sstevel@tonic-gate 		return (mdhsperror(mdep, MDE_INVAL_HSP, un->un_hsp_id));
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * set the parent on any metadevice components.
2640Sstevel@tonic-gate 	 * NOTE: currently soft partitions are the only metadevices
2650Sstevel@tonic-gate 	 * which can appear within a stripe.
2660Sstevel@tonic-gate 	 */
2670Sstevel@tonic-gate 	mdcomp = (ms_comp_t *)((void *)&((char *)un)[un->un_ocomp]);
2680Sstevel@tonic-gate 	for (row = 0; row < un->un_nrows; row++) {
2690Sstevel@tonic-gate 		struct ms_row *mdr = &un->un_row[row];
2700Sstevel@tonic-gate 		for (i = 0, c = mdr->un_icomp; i < mdr->un_ncomp; i++) {
2710Sstevel@tonic-gate 			ms_comp_t *mdc = &mdcomp[c++];
2720Sstevel@tonic-gate 			md_dev64_t comp_dev;
2730Sstevel@tonic-gate 			md_unit_t *comp_un;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 			comp_dev = mdc->un_dev;
2760Sstevel@tonic-gate 			if (md_getmajor(comp_dev) == md_major) {
2770Sstevel@tonic-gate 				/* set parent and disallow soft partitioning */
2780Sstevel@tonic-gate 				comp_un = MD_UNIT(md_getminor(comp_dev));
2790Sstevel@tonic-gate 				recids[rid++] = MD_RECID(comp_un);
2800Sstevel@tonic-gate 				md_set_parent(mdc->un_dev, MD_SID(un));
2810Sstevel@tonic-gate 			}
2820Sstevel@tonic-gate 		}
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	/* set end marker */
2860Sstevel@tonic-gate 	recids[rid] = 0;
2870Sstevel@tonic-gate 	mddb_commitrecs_wrapper(recids);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	md_create_unit_incore(mnum, &stripe_md_ops, 0);
2900Sstevel@tonic-gate 	kmem_free(recids, (num_recs * sizeof (mddb_recid_t)));
2910Sstevel@tonic-gate 	SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_CREATE, SVM_TAG_METADEVICE,
2920Sstevel@tonic-gate 	    MD_UN2SET(un), MD_SID(un));
2930Sstevel@tonic-gate 	return (0);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*ARGSUSED*/
2980Sstevel@tonic-gate static int
stripe_get(void * d,int mode,IOLOCK * lock)2990Sstevel@tonic-gate stripe_get(void *d, int mode, IOLOCK *lock)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	minor_t		mnum;
3020Sstevel@tonic-gate 	mdi_unit_t	*ui;
3030Sstevel@tonic-gate 	ms_unit_t	*un;
3040Sstevel@tonic-gate 	md_error_t	*mdep;
3050Sstevel@tonic-gate 	md_i_get_t	*migp = d;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	mnum = migp->id;
3090Sstevel@tonic-gate 	mdep = &migp->mde;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	mdclrerror(mdep);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	if ((MD_MIN2SET(mnum) >= md_nsets) || (MD_MIN2UNIT(mnum) >= md_nunits))
3140Sstevel@tonic-gate 		return (mdmderror(mdep, MDE_INVAL_UNIT, mnum));
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if ((ui = MDI_UNIT(mnum)) == NULL) {
3170Sstevel@tonic-gate 		return (mdmderror(mdep, MDE_UNIT_NOT_SETUP, mnum));
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	un = (ms_unit_t *)md_ioctl_readerlock(lock, ui);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if (migp->size == 0) {
3230Sstevel@tonic-gate 		migp->size = un->c.un_size;
3240Sstevel@tonic-gate 		return (0);
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (migp->size < un->c.un_size) {
3280Sstevel@tonic-gate 		return (EFAULT);
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	if (ddi_copyout(un, (void *)(uintptr_t)migp->mdp,
3320Sstevel@tonic-gate 	    un->c.un_size, mode))
3330Sstevel@tonic-gate 		return (EFAULT);
3340Sstevel@tonic-gate 	return (0);
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate static int
stripe_reset(md_i_reset_t * mirp)3380Sstevel@tonic-gate stripe_reset(md_i_reset_t *mirp)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	minor_t		mnum = mirp->mnum;
3410Sstevel@tonic-gate 	ms_unit_t	*un;
3420Sstevel@tonic-gate 	mdi_unit_t	*ui;
3430Sstevel@tonic-gate 	set_t		setno = MD_MIN2SET(mnum);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	mdclrerror(&mirp->mde);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if ((setno >= md_nsets) || (MD_MIN2UNIT(mnum) >= md_nunits))
3480Sstevel@tonic-gate 		return (mdmderror(&mirp->mde, MDE_INVAL_UNIT, mnum));
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (md_get_setstatus(setno) & MD_SET_STALE)
3510Sstevel@tonic-gate 		return (mdmddberror(&mirp->mde, MDE_DB_STALE, mnum, setno));
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	un = MD_UNIT(mnum);
3540Sstevel@tonic-gate 	if (un == NULL) {
3550Sstevel@tonic-gate 		return (mdmderror(&mirp->mde, MDE_UNIT_NOT_SETUP, mnum));
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	/* This prevents new opens */
3590Sstevel@tonic-gate 	rw_enter(&md_unit_array_rw.lock, RW_WRITER);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	if (MD_HAS_PARENT(un->c.un_parent)) {
3620Sstevel@tonic-gate 		rw_exit(&md_unit_array_rw.lock);
3630Sstevel@tonic-gate 		return (mdmderror(&mirp->mde, MDE_IN_USE, mnum));
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/* single thread */
3670Sstevel@tonic-gate 	ui = MDI_UNIT(mnum);
3680Sstevel@tonic-gate 	un = md_unit_openclose_enter(ui);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	if (md_unit_isopen(ui)) {
3710Sstevel@tonic-gate 		md_unit_openclose_exit(ui);
3720Sstevel@tonic-gate 		rw_exit(&md_unit_array_rw.lock);
3730Sstevel@tonic-gate 		return (mdmderror(&mirp->mde, MDE_IS_OPEN, mnum));
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	md_unit_openclose_exit(ui);
3770Sstevel@tonic-gate 	reset_stripe(un, mnum, 1);
3781623Stw21770 
3791623Stw21770 	/*
3801623Stw21770 	 * Update unit availability
3811623Stw21770 	 */
3821623Stw21770 	md_set[setno].s_un_avail++;
3831623Stw21770 
3841623Stw21770 	/*
3851623Stw21770 	 * If MN set, reset s_un_next so all nodes can have
3861623Stw21770 	 * the same view of the next available slot when
3871623Stw21770 	 * nodes are -w and -j
3881623Stw21770 	 */
3891623Stw21770 	if (MD_MNSET_SETNO(setno)) {
3901623Stw21770 		(void) md_upd_set_unnext(setno, MD_MIN2UNIT(mnum));
3911623Stw21770 	}
3921623Stw21770 
3930Sstevel@tonic-gate 	rw_exit(&md_unit_array_rw.lock);
3940Sstevel@tonic-gate 	return (0);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate static int
stripe_grow(void * d,int mode,IOLOCK * lockp)3980Sstevel@tonic-gate stripe_grow(void *d, int mode, IOLOCK *lockp)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	minor_t		mnum;
4010Sstevel@tonic-gate 	ms_unit_t	*un, *new_un;
4020Sstevel@tonic-gate 	mdi_unit_t	*ui;
4030Sstevel@tonic-gate 	minor_t		*par = NULL;
4040Sstevel@tonic-gate 	IOLOCK		*plock = NULL;
4050Sstevel@tonic-gate 	ms_comp_t	*mdcomp, *new_comp;
4060Sstevel@tonic-gate 	int		row, i, c;
4070Sstevel@tonic-gate 	mddb_recid_t	ms_recid;
4080Sstevel@tonic-gate 	mddb_recid_t	old_vtoc = 0;
4090Sstevel@tonic-gate 	mddb_recid_t	*recids;
4101623Stw21770 	md_create_rec_option_t options;
4110Sstevel@tonic-gate 	mddb_type_t	typ1;
4120Sstevel@tonic-gate 	int		err;
4130Sstevel@tonic-gate 	int64_t		tb, atb;
4140Sstevel@tonic-gate 	uint_t		nr, oc;
4150Sstevel@tonic-gate 	int		opened;
4160Sstevel@tonic-gate 	int		rval = 0;
4170Sstevel@tonic-gate 	set_t		setno;
4180Sstevel@tonic-gate 	md_error_t	*mdep;
4190Sstevel@tonic-gate 	int		npar;
4200Sstevel@tonic-gate 	int		rid;
4210Sstevel@tonic-gate 	int		num_recs;
4220Sstevel@tonic-gate 	u_longlong_t	rev;
4230Sstevel@tonic-gate 	md_grow_params_t	*mgp = d;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	mnum = mgp->mnum;
4270Sstevel@tonic-gate 	mdep = &mgp->mde;
4280Sstevel@tonic-gate 	setno = MD_MIN2SET(mnum);
4290Sstevel@tonic-gate 	npar = mgp->npar;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	mdclrerror(mdep);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	if ((setno >= md_nsets) || (MD_MIN2UNIT(mnum) >= md_nunits))
4340Sstevel@tonic-gate 		return (mdmderror(mdep, MDE_INVAL_UNIT, mnum));
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (md_get_setstatus(setno) & MD_SET_STALE)
4370Sstevel@tonic-gate 		return (mdmddberror(mdep, MDE_DB_STALE, mnum, setno));
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	ui = MDI_UNIT(mnum);
4400Sstevel@tonic-gate 	if (ui == NULL) {
4410Sstevel@tonic-gate 		return (mdmderror(mdep, MDE_UNIT_NOT_SETUP, mnum));
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	if (npar >= 1) {
4450Sstevel@tonic-gate 		ASSERT((minor_t *)(uintptr_t)mgp->par != NULL);
4460Sstevel@tonic-gate 		par = kmem_alloc(npar * sizeof (*par), KM_SLEEP);
4470Sstevel@tonic-gate 		plock = kmem_alloc(npar * sizeof (*plock), KM_SLEEP);
4480Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)(uintptr_t)mgp->par, (caddr_t)par,
4490Sstevel@tonic-gate 		    (npar * sizeof (*par)), mode) != 0) {
4500Sstevel@tonic-gate 			kmem_free(par, npar * sizeof (*par));
4510Sstevel@tonic-gate 			kmem_free(plock, npar * sizeof (*plock));
4520Sstevel@tonic-gate 			return (EFAULT);
4530Sstevel@tonic-gate 		}
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	/*
4570Sstevel@tonic-gate 	 * we grab unit reader/writer first, then parent locks,
4580Sstevel@tonic-gate 	 * then our own.
4590Sstevel@tonic-gate 	 * we expect parent units to be sorted to avoid deadlock
4600Sstevel@tonic-gate 	 */
4610Sstevel@tonic-gate 	rw_enter(&md_unit_array_rw.lock, RW_WRITER);
4620Sstevel@tonic-gate 	for (i = 0; i < npar; ++i) {
4630Sstevel@tonic-gate 		(void) md_ioctl_writerlock(&plock[i],
4640Sstevel@tonic-gate 		    MDI_UNIT(par[i]));
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 	un = (ms_unit_t *)md_ioctl_writerlock(lockp, ui);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	if (un->un_nrows != mgp->nrows) {
4690Sstevel@tonic-gate 		rval = EINVAL;
4700Sstevel@tonic-gate 		goto out;
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	typ1 = (mddb_type_t)md_getshared_key(setno,
4740Sstevel@tonic-gate 	    stripe_md_ops.md_driver.md_drivername);
4750Sstevel@tonic-gate 
4761623Stw21770 	/*
4771623Stw21770 	 * Preserve the friendly name nature of growing device.
4781623Stw21770 	 */
4791623Stw21770 	options = MD_CRO_STRIPE;
4801623Stw21770 	if (un->c.un_revision & MD_FN_META_DEV)
4811623Stw21770 		options |= MD_CRO_FN;
4820Sstevel@tonic-gate 	if (mgp->options & MD_CRO_64BIT) {
4830Sstevel@tonic-gate #if defined(_ILP32)
4840Sstevel@tonic-gate 		rval = mdmderror(mdep, MDE_UNIT_TOO_LARGE, mnum);
4850Sstevel@tonic-gate 		goto out;
4860Sstevel@tonic-gate #else
4870Sstevel@tonic-gate 		ms_recid = mddb_createrec((size_t)mgp->size, typ1, 0,
4887563SPrasad.Singamsetty@Sun.COM 		    MD_CRO_64BIT | options, setno);
4890Sstevel@tonic-gate #endif
4900Sstevel@tonic-gate 	} else {
4910Sstevel@tonic-gate 		ms_recid = mddb_createrec((size_t)mgp->size, typ1, 0,
4927563SPrasad.Singamsetty@Sun.COM 		    MD_CRO_32BIT | options, setno);
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	if (ms_recid < 0) {
4970Sstevel@tonic-gate 		rval = mddbstatus2error(mdep, (int)ms_recid, mnum, setno);
4980Sstevel@tonic-gate 		goto out;
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	/* get the address of the new unit */
5020Sstevel@tonic-gate 	new_un = (ms_unit_t *)mddb_getrecaddr(ms_recid);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	/*
5050Sstevel@tonic-gate 	 * It is okay that we muck with the new unit here,
5060Sstevel@tonic-gate 	 * since no one else will know about the unit struct
5070Sstevel@tonic-gate 	 * until we commit it. If we crash, the record will
5080Sstevel@tonic-gate 	 * be automatically purged, since we haven't
5090Sstevel@tonic-gate 	 * committed it yet and the old unit struct will be found.
5100Sstevel@tonic-gate 	 */
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/* copy in the user's unit struct */
5130Sstevel@tonic-gate 	err = ddi_copyin((caddr_t)(uintptr_t)mgp->mdp, (caddr_t)new_un,
5140Sstevel@tonic-gate 	    (size_t)mgp->size, mode);
5150Sstevel@tonic-gate 	if (err) {
5160Sstevel@tonic-gate 		mddb_deleterec_wrapper(ms_recid);
5170Sstevel@tonic-gate 		rval = EFAULT;
5180Sstevel@tonic-gate 		goto out;
5190Sstevel@tonic-gate 	}
5201623Stw21770 	if (options & MD_CRO_FN)
5211623Stw21770 		new_un->c.un_revision |= MD_FN_META_DEV;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	/*
5240Sstevel@tonic-gate 	 * allocate the real recids array.  since we may have to
5250Sstevel@tonic-gate 	 * commit underlying metadevice records, we need an
5260Sstevel@tonic-gate 	 * array of size: total number of new components being
5270Sstevel@tonic-gate 	 * attached + 2 (one for the stripe itself, one for the
5280Sstevel@tonic-gate 	 * end marker).
5290Sstevel@tonic-gate 	 */
5300Sstevel@tonic-gate 	num_recs = 2;
5310Sstevel@tonic-gate 	rid = 0;
5320Sstevel@tonic-gate 	for (row = 0; row < new_un->un_nrows; row++) {
5330Sstevel@tonic-gate 		struct ms_row *mdr = &new_un->un_row[row];
5340Sstevel@tonic-gate 		num_recs += mdr->un_ncomp;
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 	recids = kmem_alloc(num_recs * sizeof (mddb_recid_t), KM_SLEEP);
5370Sstevel@tonic-gate 	recids[rid++] = ms_recid;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/*
5400Sstevel@tonic-gate 	 * Save a few of the new unit structs fields.
5410Sstevel@tonic-gate 	 * Before they get clobbered.
5420Sstevel@tonic-gate 	 */
5430Sstevel@tonic-gate 	tb = new_un->c.un_total_blocks;
5440Sstevel@tonic-gate 	atb = new_un->c.un_actual_tb;
5450Sstevel@tonic-gate 	nr = new_un->un_nrows;
5460Sstevel@tonic-gate 	oc = new_un->un_ocomp;
5470Sstevel@tonic-gate 	rev = new_un->c.un_revision;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/*
5500Sstevel@tonic-gate 	 * Copy the old unit struct (static stuff)
5510Sstevel@tonic-gate 	 * into new unit struct
5520Sstevel@tonic-gate 	 */
5530Sstevel@tonic-gate 	bcopy((caddr_t)un, (caddr_t)new_un,
5540Sstevel@tonic-gate 	    sizeof (ms_unit_t) + ((nr - 2) * (sizeof (struct ms_row))));
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	/*
5570Sstevel@tonic-gate 	 * Restore the saved stuff.
5580Sstevel@tonic-gate 	 */
5590Sstevel@tonic-gate 	new_un->c.un_total_blocks = tb;
5607627SChris.Horne@Sun.COM 	md_nblocks_set(mnum, new_un->c.un_total_blocks);
5610Sstevel@tonic-gate 	new_un->c.un_actual_tb = atb;
5620Sstevel@tonic-gate 	new_un->un_nrows = nr;
5630Sstevel@tonic-gate 	new_un->un_ocomp = oc;
5640Sstevel@tonic-gate 	new_un->c.un_revision = rev;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	new_un->c.un_record_id = ms_recid;
5670Sstevel@tonic-gate 	new_un->c.un_size = mgp->size;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/* All 64 bit metadevices only support EFI labels. */
5700Sstevel@tonic-gate 	if (mgp->options & MD_CRO_64BIT) {
5710Sstevel@tonic-gate 		new_un->c.un_flag |= MD_EFILABEL;
5720Sstevel@tonic-gate 		/*
5730Sstevel@tonic-gate 		 * If the device was previously smaller than a terabyte,
5740Sstevel@tonic-gate 		 * and had a vtoc record attached to it, we remove the
5750Sstevel@tonic-gate 		 * vtoc record, because the layout has changed completely.
5760Sstevel@tonic-gate 		 */
5771623Stw21770 		if (((un->c.un_revision & MD_64BIT_META_DEV) == 0) &&
5780Sstevel@tonic-gate 		    (un->c.un_vtoc_id != 0)) {
5790Sstevel@tonic-gate 			old_vtoc = un->c.un_vtoc_id;
5800Sstevel@tonic-gate 			new_un->c.un_vtoc_id =
5817563SPrasad.Singamsetty@Sun.COM 			    md_vtoc_to_efi_record(old_vtoc, setno);
5820Sstevel@tonic-gate 		}
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	/*
5860Sstevel@tonic-gate 	 * Copy the old component structs into the new unit struct.
5870Sstevel@tonic-gate 	 */
5880Sstevel@tonic-gate 	mdcomp = (ms_comp_t *)((void *)&((char *)un)[un->un_ocomp]);
5890Sstevel@tonic-gate 	new_comp = (ms_comp_t *)((void *)&((char *)new_un)[new_un->un_ocomp]);
5900Sstevel@tonic-gate 	for (row = 0; row < un->un_nrows; row++) {
5910Sstevel@tonic-gate 		struct ms_row *mdr = &un->un_row[row];
5920Sstevel@tonic-gate 		for (i = 0, c = mdr->un_icomp; i < mdr->un_ncomp; i++, c++) {
5930Sstevel@tonic-gate 			bcopy((caddr_t)&mdcomp[c], (caddr_t)&new_comp[c],
5940Sstevel@tonic-gate 			    sizeof (ms_comp_t));
5950Sstevel@tonic-gate 		}
5960Sstevel@tonic-gate 	}
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	opened = md_unit_isopen(ui);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	/*
6010Sstevel@tonic-gate 	 * Set parent on metadevices being added.
6020Sstevel@tonic-gate 	 * Open the new devices being added.
6030Sstevel@tonic-gate 	 * NOTE: currently soft partitions are the only metadevices
6040Sstevel@tonic-gate 	 * which can appear within a stripe.
6050Sstevel@tonic-gate 	 */
6060Sstevel@tonic-gate 	for (row = un->un_nrows; row < new_un->un_nrows; row++) {
6070Sstevel@tonic-gate 		struct ms_row *mdr = &new_un->un_row[row];
6080Sstevel@tonic-gate 		for (i = 0, c = mdr->un_icomp; i < mdr->un_ncomp; i++) {
6090Sstevel@tonic-gate 			struct ms_comp	*mdc = &new_comp[c++];
6100Sstevel@tonic-gate 			md_dev64_t comp_dev;
6110Sstevel@tonic-gate 			md_unit_t *comp_un;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 			comp_dev = mdc->un_dev;
6140Sstevel@tonic-gate 			/* set parent on any metadevices */
6150Sstevel@tonic-gate 			if (md_getmajor(comp_dev) == md_major) {
6160Sstevel@tonic-gate 				comp_un = MD_UNIT(md_getminor(comp_dev));
6170Sstevel@tonic-gate 				recids[rid++] = MD_RECID(comp_un);
6180Sstevel@tonic-gate 				md_set_parent(comp_dev, MD_SID(new_un));
6190Sstevel@tonic-gate 			}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 			if (opened) {
6220Sstevel@tonic-gate 				md_dev64_t tmpdev = mdc->un_dev;
6230Sstevel@tonic-gate 				/*
6240Sstevel@tonic-gate 				 * Open by device id
6250Sstevel@tonic-gate 				 * Check if this comp is hotspared and
6260Sstevel@tonic-gate 				 * if it is then use the key for hotspare
6270Sstevel@tonic-gate 				 */
6280Sstevel@tonic-gate 				tmpdev = md_resolve_bydevid(mnum, tmpdev,
6297563SPrasad.Singamsetty@Sun.COM 				    mdc->un_mirror.ms_hs_id ?
6307563SPrasad.Singamsetty@Sun.COM 				    mdc->un_mirror.ms_hs_key : mdc->un_key);
6310Sstevel@tonic-gate 				(void) md_layered_open(mnum, &tmpdev,
6327563SPrasad.Singamsetty@Sun.COM 				    MD_OFLG_NULL);
6330Sstevel@tonic-gate 				mdc->un_dev = tmpdev;
6340Sstevel@tonic-gate 				mdc->un_mirror.ms_flags |= MDM_S_ISOPEN;
6350Sstevel@tonic-gate 			}
6360Sstevel@tonic-gate 		}
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	/* set end marker */
6400Sstevel@tonic-gate 	recids[rid] = 0;
6410Sstevel@tonic-gate 	/* commit new unit struct */
6420Sstevel@tonic-gate 	mddb_commitrecs_wrapper(recids);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	/* delete old unit struct */
6450Sstevel@tonic-gate 	mddb_deleterec_wrapper(un->c.un_record_id);
6467627SChris.Horne@Sun.COM 
6477627SChris.Horne@Sun.COM 	/* place new unit in in-core array */
6487627SChris.Horne@Sun.COM 	md_nblocks_set(mnum, new_un->c.un_total_blocks);
6490Sstevel@tonic-gate 	MD_UNIT(mnum) = new_un;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	/*
6520Sstevel@tonic-gate 	 * If old_vtoc has a non zero value, we know:
6530Sstevel@tonic-gate 	 * - This unit crossed the border from smaller to larger one TB
6540Sstevel@tonic-gate 	 * - There was a vtoc record for the unit,
6550Sstevel@tonic-gate 	 * - This vtoc record is no longer needed, because
6560Sstevel@tonic-gate 	 *   a new efi record has been created for this un.
6570Sstevel@tonic-gate 	 */
6580Sstevel@tonic-gate 	if (old_vtoc != 0) {
6590Sstevel@tonic-gate 		mddb_deleterec_wrapper(old_vtoc);
6600Sstevel@tonic-gate 	}
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	/* free recids array */
6630Sstevel@tonic-gate 	kmem_free(recids, num_recs * sizeof (mddb_recid_t));
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_GROW, SVM_TAG_METADEVICE,
6660Sstevel@tonic-gate 	    MD_UN2SET(new_un), MD_SID(new_un));
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	/* release locks, return success */
6690Sstevel@tonic-gate out:
6700Sstevel@tonic-gate 	for (i =  npar - 1; (i >= 0); --i)
6710Sstevel@tonic-gate 		md_ioctl_writerexit(&plock[i]);
6720Sstevel@tonic-gate 	rw_exit(&md_unit_array_rw.lock);
6730Sstevel@tonic-gate 	if (plock != NULL)
6740Sstevel@tonic-gate 		kmem_free(plock, npar * sizeof (*plock));
6750Sstevel@tonic-gate 	if (par != NULL)
6760Sstevel@tonic-gate 		kmem_free(par, npar * sizeof (*par));
6770Sstevel@tonic-gate 	return (rval);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate static int
stripe_get_geom(ms_unit_t * un,struct dk_geom * geomp)6810Sstevel@tonic-gate stripe_get_geom(
6820Sstevel@tonic-gate 	ms_unit_t	*un,
6830Sstevel@tonic-gate 	struct dk_geom	*geomp
6840Sstevel@tonic-gate )
6850Sstevel@tonic-gate {
6860Sstevel@tonic-gate 	md_get_geom((md_unit_t *)un, geomp);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	return (0);
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate static int
stripe_get_vtoc(ms_unit_t * un,struct vtoc * vtocp)6920Sstevel@tonic-gate stripe_get_vtoc(
6930Sstevel@tonic-gate 	ms_unit_t	*un,
6940Sstevel@tonic-gate 	struct vtoc	*vtocp
6950Sstevel@tonic-gate )
6960Sstevel@tonic-gate {
6970Sstevel@tonic-gate 	md_get_vtoc((md_unit_t *)un, vtocp);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	return (0);
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate static int
stripe_set_vtoc(ms_unit_t * un,struct vtoc * vtocp)7030Sstevel@tonic-gate stripe_set_vtoc(
7040Sstevel@tonic-gate 	ms_unit_t	*un,
7050Sstevel@tonic-gate 	struct vtoc	*vtocp
7060Sstevel@tonic-gate )
7070Sstevel@tonic-gate {
7080Sstevel@tonic-gate 	return (md_set_vtoc((md_unit_t *)un, vtocp));
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate static int
stripe_get_extvtoc(ms_unit_t * un,struct extvtoc * vtocp)7127563SPrasad.Singamsetty@Sun.COM stripe_get_extvtoc(
7137563SPrasad.Singamsetty@Sun.COM 	ms_unit_t	*un,
7147563SPrasad.Singamsetty@Sun.COM 	struct extvtoc	*vtocp
7157563SPrasad.Singamsetty@Sun.COM )
7167563SPrasad.Singamsetty@Sun.COM {
7177563SPrasad.Singamsetty@Sun.COM 	md_get_extvtoc((md_unit_t *)un, vtocp);
7187563SPrasad.Singamsetty@Sun.COM 
7197563SPrasad.Singamsetty@Sun.COM 	return (0);
7207563SPrasad.Singamsetty@Sun.COM }
7217563SPrasad.Singamsetty@Sun.COM 
7227563SPrasad.Singamsetty@Sun.COM static int
stripe_set_extvtoc(ms_unit_t * un,struct extvtoc * vtocp)7237563SPrasad.Singamsetty@Sun.COM stripe_set_extvtoc(
7247563SPrasad.Singamsetty@Sun.COM 	ms_unit_t	*un,
7257563SPrasad.Singamsetty@Sun.COM 	struct extvtoc	*vtocp
7267563SPrasad.Singamsetty@Sun.COM )
7277563SPrasad.Singamsetty@Sun.COM {
7287563SPrasad.Singamsetty@Sun.COM 	return (md_set_extvtoc((md_unit_t *)un, vtocp));
7297563SPrasad.Singamsetty@Sun.COM }
7307563SPrasad.Singamsetty@Sun.COM 
7317563SPrasad.Singamsetty@Sun.COM static int
stripe_get_cgapart(ms_unit_t * un,struct dk_map * dkmapp)7320Sstevel@tonic-gate stripe_get_cgapart(
7330Sstevel@tonic-gate 	ms_unit_t	*un,
7340Sstevel@tonic-gate 	struct dk_map	*dkmapp
7350Sstevel@tonic-gate )
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 	md_get_cgapart((md_unit_t *)un, dkmapp);
7380Sstevel@tonic-gate 	return (0);
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate static int
stripe_getdevs(void * d,int mode,IOLOCK * lock)7420Sstevel@tonic-gate stripe_getdevs(
7430Sstevel@tonic-gate 	void			*d,
7440Sstevel@tonic-gate 	int			mode,
7450Sstevel@tonic-gate 	IOLOCK			*lock
7460Sstevel@tonic-gate )
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate 	minor_t			mnum;
7490Sstevel@tonic-gate 	mdi_unit_t		*ui;
7500Sstevel@tonic-gate 	ms_unit_t		*un;
7510Sstevel@tonic-gate 	struct ms_row		*mdr;
7520Sstevel@tonic-gate 	ms_comp_t		*mdcomp, *mdc;
7530Sstevel@tonic-gate 	int			r, c, i;
7540Sstevel@tonic-gate 	int			cnt;
7550Sstevel@tonic-gate 	md_error_t		*mdep;
7560Sstevel@tonic-gate 	md_dev64_t		*devsp;
7570Sstevel@tonic-gate 	md_dev64_t		unit_dev;
7580Sstevel@tonic-gate 	md_getdevs_params_t	*mgdp = d;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	mnum = mgdp->mnum;
7620Sstevel@tonic-gate 	mdep = &mgdp->mde;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	/* check out unit */
7650Sstevel@tonic-gate 	mdclrerror(mdep);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if ((MD_MIN2SET(mnum) >= md_nsets) || (MD_MIN2UNIT(mnum) >= md_nunits))
7680Sstevel@tonic-gate 		return (mdmderror(mdep, MDE_INVAL_UNIT, mnum));
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	if ((ui = MDI_UNIT(mnum)) == NULL) {
7710Sstevel@tonic-gate 		return (mdmderror(mdep, MDE_UNIT_NOT_SETUP, mnum));
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	un = (ms_unit_t *)md_ioctl_readerlock(lock, ui);
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	mdcomp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
7770Sstevel@tonic-gate 	devsp = (md_dev64_t *)(uintptr_t)mgdp->devs;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	for (cnt = 0, r = 0; (r < un->un_nrows); ++r) {
7800Sstevel@tonic-gate 		mdr = &un->un_row[r];
7810Sstevel@tonic-gate 		for (c = 0, i = mdr->un_icomp; (c < mdr->un_ncomp); ++c) {
7820Sstevel@tonic-gate 			mdc = &mdcomp[i++];
7830Sstevel@tonic-gate 			if (cnt < mgdp->cnt) {
7840Sstevel@tonic-gate 				unit_dev = mdc->un_dev;
7850Sstevel@tonic-gate 				if (md_getmajor(unit_dev) != md_major) {
7860Sstevel@tonic-gate 					if ((unit_dev = md_xlate_mini_2_targ
7870Sstevel@tonic-gate 					    (unit_dev)) == NODEV64)
7880Sstevel@tonic-gate 						return (ENODEV);
7890Sstevel@tonic-gate 				}
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 				if (ddi_copyout((caddr_t)&unit_dev, devsp,
7927563SPrasad.Singamsetty@Sun.COM 				    sizeof (*devsp), mode) != 0)
7930Sstevel@tonic-gate 					return (EFAULT);
7940Sstevel@tonic-gate 				++devsp;
7950Sstevel@tonic-gate 			}
7960Sstevel@tonic-gate 			++cnt;
7970Sstevel@tonic-gate 		}
7980Sstevel@tonic-gate 	}
7990Sstevel@tonic-gate 	mgdp->cnt = cnt;
8000Sstevel@tonic-gate 	return (0);
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate static int
stripe_change(md_stripe_params_t * msp,IOLOCK * lock)8040Sstevel@tonic-gate stripe_change(
8050Sstevel@tonic-gate 	md_stripe_params_t	*msp,
8060Sstevel@tonic-gate 	IOLOCK			*lock
8070Sstevel@tonic-gate )
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate 	ms_params_t		*pp = &msp->params;
8100Sstevel@tonic-gate 	minor_t			mnum = msp->mnum;
8110Sstevel@tonic-gate 	ms_unit_t		*un;
8120Sstevel@tonic-gate 	mdi_unit_t		*ui;
8130Sstevel@tonic-gate 	int			r, c, i;
8140Sstevel@tonic-gate 	struct ms_row		*mdr;
8150Sstevel@tonic-gate 	ms_comp_t		*mdcomp, *mdc;
8160Sstevel@tonic-gate 	mddb_recid_t		recids[4];
8170Sstevel@tonic-gate 	int			irecid;
8180Sstevel@tonic-gate 	int			inc_new_hsp = 0;
8190Sstevel@tonic-gate 	int			err;
8200Sstevel@tonic-gate 	set_t			setno = MD_MIN2SET(mnum);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	mdclrerror(&msp->mde);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	if ((setno >= md_nsets) || (MD_MIN2UNIT(mnum) >= md_nunits))
8250Sstevel@tonic-gate 		return (mdmderror(&msp->mde, MDE_INVAL_UNIT, mnum));
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	if (md_get_setstatus(setno) & MD_SET_STALE)
8280Sstevel@tonic-gate 		return (mdmddberror(&msp->mde, MDE_DB_STALE, mnum, setno));
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	if ((ui = MDI_UNIT(mnum)) == NULL) {
8310Sstevel@tonic-gate 		return (mdmderror(&msp->mde, MDE_UNIT_NOT_SETUP, mnum));
8320Sstevel@tonic-gate 	}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	if (!pp->change_hsp_id)
8350Sstevel@tonic-gate 		return (0);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	un = (ms_unit_t *)md_ioctl_writerlock(lock, ui);
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	/* verify that no hot spares are in use */
8400Sstevel@tonic-gate 	mdcomp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
8410Sstevel@tonic-gate 	for (r = 0; r < un->un_nrows; r++) {
8420Sstevel@tonic-gate 		mdr = &un->un_row[r];
8430Sstevel@tonic-gate 		for (c = 0, i = mdr->un_icomp; c < mdr->un_ncomp; c++) {
8440Sstevel@tonic-gate 			mdc = &mdcomp[i++];
8450Sstevel@tonic-gate 			if (mdc->un_mirror.ms_hs_id != 0) {
8460Sstevel@tonic-gate 				return (mdmderror(&msp->mde, MDE_HS_IN_USE,
8470Sstevel@tonic-gate 				    mnum));
8480Sstevel@tonic-gate 			}
8490Sstevel@tonic-gate 		}
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	recids[1] = 0;
8530Sstevel@tonic-gate 	recids[2] = 0;
8540Sstevel@tonic-gate 	irecid = 1;
8550Sstevel@tonic-gate 	if (pp->hsp_id != -1) {
8560Sstevel@tonic-gate 		/* increment the reference count of the new hsp */
8570Sstevel@tonic-gate 		err = md_hot_spare_ifc(HSP_INCREF, pp->hsp_id, 0, 0,
8580Sstevel@tonic-gate 		    &recids[1], NULL, NULL, NULL);
8590Sstevel@tonic-gate 		if (err) {
8600Sstevel@tonic-gate 			return (mdhsperror(&msp->mde, MDE_INVAL_HSP,
8610Sstevel@tonic-gate 			    pp->hsp_id));
8620Sstevel@tonic-gate 		}
8630Sstevel@tonic-gate 		inc_new_hsp = 1;
8640Sstevel@tonic-gate 		irecid++;
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	if (un->un_hsp_id != -1) {
8680Sstevel@tonic-gate 		/* decrement the reference count of the old hsp */
8690Sstevel@tonic-gate 		err = md_hot_spare_ifc(HSP_DECREF, un->un_hsp_id, 0, 0,
8700Sstevel@tonic-gate 		    &recids[irecid], NULL, NULL, NULL);
8710Sstevel@tonic-gate 		if (err) {
8720Sstevel@tonic-gate 			err = mdhsperror(&msp->mde, MDE_INVAL_HSP,
8730Sstevel@tonic-gate 			    pp->hsp_id);
8740Sstevel@tonic-gate 			if (inc_new_hsp) {
8750Sstevel@tonic-gate 				(void) md_hot_spare_ifc(HSP_DECREF,
8760Sstevel@tonic-gate 				    pp->hsp_id, 0, 0,
8770Sstevel@tonic-gate 				    &recids[1], NULL, NULL, NULL);
8780Sstevel@tonic-gate 				/*
8790Sstevel@tonic-gate 				 * Don't need to commit the record,
8800Sstevel@tonic-gate 				 * cause it never got commit before
8810Sstevel@tonic-gate 				 */
8820Sstevel@tonic-gate 			}
8830Sstevel@tonic-gate 			return (err);
8840Sstevel@tonic-gate 		}
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	un->un_hsp_id = pp->hsp_id;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	recids[0] = un->c.un_record_id;
8900Sstevel@tonic-gate 	recids[3] = 0;
8910Sstevel@tonic-gate 	mddb_commitrecs_wrapper(recids);
8920Sstevel@tonic-gate 	SE_NOTIFY(EC_SVM_STATE, ESC_SVM_CHANGE, SVM_TAG_METADEVICE,
8930Sstevel@tonic-gate 	    MD_UN2SET(un), MD_SID(un));
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	return (0);
8960Sstevel@tonic-gate }
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate static int
stripe_admin_ioctl(int cmd,void * data,int mode,IOLOCK * lockp)8990Sstevel@tonic-gate stripe_admin_ioctl(int cmd, void *data, int mode, IOLOCK *lockp)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate 	size_t	sz = 0;
9020Sstevel@tonic-gate 	void	*d = NULL;
9030Sstevel@tonic-gate 	int	err = 0;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	/* We can only handle 32-bit clients for internal commands */
9060Sstevel@tonic-gate 	if ((mode & DATAMODEL_MASK) != DATAMODEL_ILP32) {
9070Sstevel@tonic-gate 		return (EINVAL);
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/* handle ioctl */
9110Sstevel@tonic-gate 	switch (cmd) {
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	case MD_IOCSET:
9140Sstevel@tonic-gate 	{
9150Sstevel@tonic-gate 		if (! (mode & FWRITE))
9160Sstevel@tonic-gate 			return (EACCES);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 		sz = sizeof (struct md_set_params);
9190Sstevel@tonic-gate 		d = kmem_alloc(sz, KM_SLEEP);
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 		if (ddi_copyin(data, d, sz, mode)) {
9220Sstevel@tonic-gate 			err = EFAULT;
9230Sstevel@tonic-gate 			break;
9240Sstevel@tonic-gate 		}
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 		err = stripe_set(d, mode);
9270Sstevel@tonic-gate 		break;
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	case MD_IOCGET:
9310Sstevel@tonic-gate 	{
9320Sstevel@tonic-gate 		if (! (mode & FREAD))
9330Sstevel@tonic-gate 			return (EACCES);
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 		sz = sizeof (struct md_i_get);
9360Sstevel@tonic-gate 		d = kmem_alloc(sz, KM_SLEEP);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 		if (ddi_copyin(data, d, sz, mode)) {
9390Sstevel@tonic-gate 			err = EFAULT;
9400Sstevel@tonic-gate 			break;
9410Sstevel@tonic-gate 		}
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 		err = stripe_get(d, mode, lockp);
9440Sstevel@tonic-gate 		break;
9450Sstevel@tonic-gate 	}
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	case MD_IOCRESET:
9480Sstevel@tonic-gate 	{
9490Sstevel@tonic-gate 		if (! (mode & FWRITE))
9500Sstevel@tonic-gate 			return (EACCES);
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 		sz = sizeof (md_i_reset_t);
9530Sstevel@tonic-gate 		d = kmem_alloc(sz, KM_SLEEP);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 		if (ddi_copyin(data, d, sz, mode)) {
9560Sstevel@tonic-gate 			err = EFAULT;
9570Sstevel@tonic-gate 			break;
9580Sstevel@tonic-gate 		}
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 		err = stripe_reset((md_i_reset_t *)d);
9610Sstevel@tonic-gate 		break;
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	case MD_IOCGROW:
9650Sstevel@tonic-gate 	{
9660Sstevel@tonic-gate 		if (! (mode & FWRITE))
9670Sstevel@tonic-gate 			return (EACCES);
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 		sz = sizeof (struct md_grow_params);
9700Sstevel@tonic-gate 		d  = kmem_alloc(sz, KM_SLEEP);
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 		if (ddi_copyin(data, d, sz, mode)) {
9730Sstevel@tonic-gate 			err = EFAULT;
9740Sstevel@tonic-gate 			break;
9750Sstevel@tonic-gate 		}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 		err = stripe_grow(d, mode, lockp);
9780Sstevel@tonic-gate 		break;
9790Sstevel@tonic-gate 	}
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	case MD_IOCGET_DEVS:
9820Sstevel@tonic-gate 	{
9830Sstevel@tonic-gate 		if (! (mode & FREAD))
9840Sstevel@tonic-gate 			return (EACCES);
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 		sz = sizeof (struct md_getdevs_params);
9870Sstevel@tonic-gate 		d  = kmem_alloc(sz, KM_SLEEP);
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 		if (ddi_copyin(data, d, sz, mode)) {
9900Sstevel@tonic-gate 			err = EFAULT;
9910Sstevel@tonic-gate 			break;
9920Sstevel@tonic-gate 		}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 		err = stripe_getdevs(d, mode, lockp);
9950Sstevel@tonic-gate 		break;
9960Sstevel@tonic-gate 	}
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	case MD_IOCCHANGE:
9990Sstevel@tonic-gate 	{
10000Sstevel@tonic-gate 		if (! (mode & FWRITE))
10010Sstevel@tonic-gate 			return (EACCES);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		sz = sizeof (md_stripe_params_t);
10040Sstevel@tonic-gate 		d = kmem_alloc(sz, KM_SLEEP);
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 		if (ddi_copyin(data, d, sz, mode)) {
10070Sstevel@tonic-gate 			err = EFAULT;
10080Sstevel@tonic-gate 			break;
10090Sstevel@tonic-gate 		}
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 		err = stripe_change((md_stripe_params_t *)d, lockp);
10120Sstevel@tonic-gate 		break;
10130Sstevel@tonic-gate 	}
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	case MD_IOCREPLACE:
10160Sstevel@tonic-gate 	{
10170Sstevel@tonic-gate 		if (! (mode & FWRITE))
10180Sstevel@tonic-gate 			return (EACCES);
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 		sz = sizeof (replace_params_t);
10210Sstevel@tonic-gate 		d = kmem_alloc(sz, KM_SLEEP);
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 		if (ddi_copyin(data, d, sz, mode)) {
10240Sstevel@tonic-gate 			err = EFAULT;
10250Sstevel@tonic-gate 			break;
10260Sstevel@tonic-gate 		}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 		err = stripe_replace((replace_params_t *)d);
10290Sstevel@tonic-gate 		break;
10300Sstevel@tonic-gate 	}
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	case MD_IOCPROBE_DEV:
10330Sstevel@tonic-gate 	{
10340Sstevel@tonic-gate 		/*
10350Sstevel@tonic-gate 		 * Ignore the request since stripe is not
10360Sstevel@tonic-gate 		 * a type of 'redundant' metadevice
10370Sstevel@tonic-gate 		 */
10380Sstevel@tonic-gate 		break;
10390Sstevel@tonic-gate 	}
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	default:
10420Sstevel@tonic-gate 		return (ENOTTY);
10430Sstevel@tonic-gate 	}
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	/*
10460Sstevel@tonic-gate 	 * copyout and free any args
10470Sstevel@tonic-gate 	 */
10480Sstevel@tonic-gate 	if (sz != 0) {
10490Sstevel@tonic-gate 		if (err == 0) {
10500Sstevel@tonic-gate 			if (ddi_copyout(d, data, sz, mode) != 0) {
10510Sstevel@tonic-gate 				err = EFAULT;
10520Sstevel@tonic-gate 			}
10530Sstevel@tonic-gate 		}
10540Sstevel@tonic-gate 		kmem_free(d, sz);
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 	return (err);
10570Sstevel@tonic-gate }
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate /*
10600Sstevel@tonic-gate  * The parameters of md_stripe_ioctl are defined by the ddi and so
10610Sstevel@tonic-gate  *  dev is of type dev_t and not md_dev64_t
10620Sstevel@tonic-gate  */
10630Sstevel@tonic-gate int
md_stripe_ioctl(dev_t dev,int cmd,void * data,int mode,IOLOCK * lockp)10640Sstevel@tonic-gate md_stripe_ioctl(dev_t dev, int cmd, void *data, int mode, IOLOCK *lockp)
10650Sstevel@tonic-gate {
10660Sstevel@tonic-gate 	minor_t		mnum = getminor(dev);
10670Sstevel@tonic-gate 	ms_unit_t	*un;
10680Sstevel@tonic-gate 	int		err = 0;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	/* handle admin ioctls */
10710Sstevel@tonic-gate 	if (mnum == MD_ADM_MINOR)
10720Sstevel@tonic-gate 		return (stripe_admin_ioctl(cmd, data, mode, lockp));
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	/* check unit */
10750Sstevel@tonic-gate 	if ((MD_MIN2SET(mnum) >= md_nsets) ||
10760Sstevel@tonic-gate 	    (MD_MIN2UNIT(mnum) >= md_nunits) ||
10770Sstevel@tonic-gate 	    ((un = MD_UNIT(mnum)) == NULL))
10780Sstevel@tonic-gate 		return (ENXIO);
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	/* is this a supported ioctl? */
10817563SPrasad.Singamsetty@Sun.COM 	err = md_check_ioctl_against_unit(cmd, un->c);
10820Sstevel@tonic-gate 	if (err != 0) {
10830Sstevel@tonic-gate 		return (err);
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	/* handle ioctl */
10870Sstevel@tonic-gate 	switch (cmd) {
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	case DKIOCINFO:
10900Sstevel@tonic-gate 	{
10910Sstevel@tonic-gate 		struct dk_cinfo		*p;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 		if (! (mode & FREAD))
10940Sstevel@tonic-gate 			return (EACCES);
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		p = kmem_alloc(sizeof (*p), KM_SLEEP);
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 		get_info(p, mnum);
10990Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)p, data, sizeof (*p), mode) != 0)
11000Sstevel@tonic-gate 			err = EFAULT;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 		kmem_free(p, sizeof (*p));
11030Sstevel@tonic-gate 		return (err);
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	case DKIOCGMEDIAINFO:
11070Sstevel@tonic-gate 	{
11080Sstevel@tonic-gate 		struct dk_minfo	p;
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 		if (! (mode & FREAD))
11110Sstevel@tonic-gate 			return (EACCES);
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 		get_minfo(&p, mnum);
11140Sstevel@tonic-gate 		if (ddi_copyout(&p, data, sizeof (struct dk_minfo), mode) != 0)
11150Sstevel@tonic-gate 			err = EFAULT;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 		return (err);
11180Sstevel@tonic-gate 	}
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	case DKIOCGGEOM:
11210Sstevel@tonic-gate 	{
11220Sstevel@tonic-gate 		struct dk_geom		*p;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 		if (! (mode & FREAD))
11250Sstevel@tonic-gate 			return (EACCES);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 		p = kmem_alloc(sizeof (*p), KM_SLEEP);
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 		if ((err = stripe_get_geom(un, p)) == 0) {
11300Sstevel@tonic-gate 			if (ddi_copyout((caddr_t)p, data, sizeof (*p),
11310Sstevel@tonic-gate 			    mode) != 0)
11320Sstevel@tonic-gate 				err = EFAULT;
11330Sstevel@tonic-gate 		}
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 		kmem_free(p, sizeof (*p));
11360Sstevel@tonic-gate 		return (err);
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	case DKIOCGVTOC:
11400Sstevel@tonic-gate 	{
1141*9017SJohn.Wren.Kennedy@Sun.COM 		struct vtoc	*vtoc;
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 		if (! (mode & FREAD))
11440Sstevel@tonic-gate 			return (EACCES);
11450Sstevel@tonic-gate 
1146*9017SJohn.Wren.Kennedy@Sun.COM 		vtoc = kmem_zalloc(sizeof (*vtoc), KM_SLEEP);
1147*9017SJohn.Wren.Kennedy@Sun.COM 		if ((err = stripe_get_vtoc(un, vtoc)) != 0) {
1148*9017SJohn.Wren.Kennedy@Sun.COM 			kmem_free(vtoc, sizeof (*vtoc));
11490Sstevel@tonic-gate 			return (err);
11500Sstevel@tonic-gate 		}
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 		if ((mode & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
1153*9017SJohn.Wren.Kennedy@Sun.COM 			if (ddi_copyout(vtoc, data, sizeof (*vtoc), mode))
11540Sstevel@tonic-gate 				err = EFAULT;
11550Sstevel@tonic-gate 		}
11560Sstevel@tonic-gate #ifdef _SYSCALL32
11570Sstevel@tonic-gate 		else {
1158*9017SJohn.Wren.Kennedy@Sun.COM 			struct vtoc32	*vtoc32;
1159*9017SJohn.Wren.Kennedy@Sun.COM 
1160*9017SJohn.Wren.Kennedy@Sun.COM 			vtoc32 = kmem_zalloc(sizeof (*vtoc32), KM_SLEEP);
1161*9017SJohn.Wren.Kennedy@Sun.COM 
1162*9017SJohn.Wren.Kennedy@Sun.COM 			vtoctovtoc32((*vtoc), (*vtoc32));
1163*9017SJohn.Wren.Kennedy@Sun.COM 			if (ddi_copyout(vtoc32, data, sizeof (*vtoc32), mode))
11640Sstevel@tonic-gate 				err = EFAULT;
1165*9017SJohn.Wren.Kennedy@Sun.COM 			kmem_free(vtoc32, sizeof (*vtoc32));
11660Sstevel@tonic-gate 		}
11670Sstevel@tonic-gate #endif /* _SYSCALL32 */
11680Sstevel@tonic-gate 
1169*9017SJohn.Wren.Kennedy@Sun.COM 		kmem_free(vtoc, sizeof (*vtoc));
11700Sstevel@tonic-gate 		return (err);
11710Sstevel@tonic-gate 	}
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	case DKIOCSVTOC:
11740Sstevel@tonic-gate 	{
1175*9017SJohn.Wren.Kennedy@Sun.COM 		struct vtoc	*vtoc;
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 		if (! (mode & FWRITE))
11780Sstevel@tonic-gate 			return (EACCES);
11790Sstevel@tonic-gate 
1180*9017SJohn.Wren.Kennedy@Sun.COM 		vtoc = kmem_zalloc(sizeof (*vtoc), KM_SLEEP);
11810Sstevel@tonic-gate 		if ((mode & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
1182*9017SJohn.Wren.Kennedy@Sun.COM 			if (ddi_copyin(data, vtoc, sizeof (*vtoc), mode)) {
11830Sstevel@tonic-gate 				err = EFAULT;
11840Sstevel@tonic-gate 			}
11850Sstevel@tonic-gate 		}
11860Sstevel@tonic-gate #ifdef _SYSCALL32
11870Sstevel@tonic-gate 		else {
1188*9017SJohn.Wren.Kennedy@Sun.COM 			struct vtoc32	*vtoc32;
1189*9017SJohn.Wren.Kennedy@Sun.COM 
1190*9017SJohn.Wren.Kennedy@Sun.COM 			vtoc32 = kmem_zalloc(sizeof (*vtoc32), KM_SLEEP);
1191*9017SJohn.Wren.Kennedy@Sun.COM 
1192*9017SJohn.Wren.Kennedy@Sun.COM 			if (ddi_copyin(data, vtoc32, sizeof (*vtoc32), mode)) {
11930Sstevel@tonic-gate 				err = EFAULT;
11940Sstevel@tonic-gate 			} else {
1195*9017SJohn.Wren.Kennedy@Sun.COM 				vtoc32tovtoc((*vtoc32), (*vtoc));
11960Sstevel@tonic-gate 			}
1197*9017SJohn.Wren.Kennedy@Sun.COM 			kmem_free(vtoc32, sizeof (*vtoc32));
11980Sstevel@tonic-gate 		}
11990Sstevel@tonic-gate #endif /* _SYSCALL32 */
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 		if (err == 0) {
1202*9017SJohn.Wren.Kennedy@Sun.COM 			err = stripe_set_vtoc(un, vtoc);
12030Sstevel@tonic-gate 		}
12040Sstevel@tonic-gate 
1205*9017SJohn.Wren.Kennedy@Sun.COM 		kmem_free(vtoc, sizeof (*vtoc));
12060Sstevel@tonic-gate 		return (err);
12070Sstevel@tonic-gate 	}
12080Sstevel@tonic-gate 
12097563SPrasad.Singamsetty@Sun.COM 
12107563SPrasad.Singamsetty@Sun.COM 	case DKIOCGEXTVTOC:
12117563SPrasad.Singamsetty@Sun.COM 	{
1212*9017SJohn.Wren.Kennedy@Sun.COM 		struct extvtoc	*extvtoc;
12137563SPrasad.Singamsetty@Sun.COM 
12147563SPrasad.Singamsetty@Sun.COM 		if (! (mode & FREAD))
12157563SPrasad.Singamsetty@Sun.COM 			return (EACCES);
12167563SPrasad.Singamsetty@Sun.COM 
1217*9017SJohn.Wren.Kennedy@Sun.COM 		extvtoc = kmem_zalloc(sizeof (*extvtoc), KM_SLEEP);
1218*9017SJohn.Wren.Kennedy@Sun.COM 		if ((err = stripe_get_extvtoc(un, extvtoc)) != 0) {
1219*9017SJohn.Wren.Kennedy@Sun.COM 			kmem_free(extvtoc, sizeof (*extvtoc));
12207563SPrasad.Singamsetty@Sun.COM 			return (err);
12217563SPrasad.Singamsetty@Sun.COM 		}
12227563SPrasad.Singamsetty@Sun.COM 
1223*9017SJohn.Wren.Kennedy@Sun.COM 		if (ddi_copyout(extvtoc, data, sizeof (*extvtoc), mode))
12247563SPrasad.Singamsetty@Sun.COM 			err = EFAULT;
12257563SPrasad.Singamsetty@Sun.COM 
1226*9017SJohn.Wren.Kennedy@Sun.COM 		kmem_free(extvtoc, sizeof (*extvtoc));
12277563SPrasad.Singamsetty@Sun.COM 		return (err);
12287563SPrasad.Singamsetty@Sun.COM 	}
12297563SPrasad.Singamsetty@Sun.COM 
12307563SPrasad.Singamsetty@Sun.COM 	case DKIOCSEXTVTOC:
12317563SPrasad.Singamsetty@Sun.COM 	{
1232*9017SJohn.Wren.Kennedy@Sun.COM 		struct extvtoc	*extvtoc;
12337563SPrasad.Singamsetty@Sun.COM 
12347563SPrasad.Singamsetty@Sun.COM 		if (! (mode & FWRITE))
12357563SPrasad.Singamsetty@Sun.COM 			return (EACCES);
12367563SPrasad.Singamsetty@Sun.COM 
1237*9017SJohn.Wren.Kennedy@Sun.COM 		extvtoc = kmem_zalloc(sizeof (*extvtoc), KM_SLEEP);
1238*9017SJohn.Wren.Kennedy@Sun.COM 		if (ddi_copyin(data, extvtoc, sizeof (*extvtoc), mode)) {
12397563SPrasad.Singamsetty@Sun.COM 			err = EFAULT;
12407563SPrasad.Singamsetty@Sun.COM 		}
12417563SPrasad.Singamsetty@Sun.COM 
12427563SPrasad.Singamsetty@Sun.COM 		if (err == 0) {
1243*9017SJohn.Wren.Kennedy@Sun.COM 			err = stripe_set_extvtoc(un, extvtoc);
12447563SPrasad.Singamsetty@Sun.COM 		}
12457563SPrasad.Singamsetty@Sun.COM 
1246*9017SJohn.Wren.Kennedy@Sun.COM 		kmem_free(extvtoc, sizeof (*extvtoc));
12477563SPrasad.Singamsetty@Sun.COM 		return (err);
12487563SPrasad.Singamsetty@Sun.COM 	}
12497563SPrasad.Singamsetty@Sun.COM 
12500Sstevel@tonic-gate 	case DKIOCGAPART:
12510Sstevel@tonic-gate 	{
12520Sstevel@tonic-gate 		struct dk_map	dmp;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 		if ((err = stripe_get_cgapart(un, &dmp)) != 0) {
12550Sstevel@tonic-gate 			return (err);
12560Sstevel@tonic-gate 		}
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 		if ((mode & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
12590Sstevel@tonic-gate 			if (ddi_copyout((caddr_t)&dmp, data, sizeof (dmp),
12607563SPrasad.Singamsetty@Sun.COM 			    mode) != 0)
12610Sstevel@tonic-gate 				err = EFAULT;
12620Sstevel@tonic-gate 		}
12630Sstevel@tonic-gate #ifdef _SYSCALL32
12640Sstevel@tonic-gate 		else {
12650Sstevel@tonic-gate 			struct dk_map32 dmp32;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 			dmp32.dkl_cylno = dmp.dkl_cylno;
12680Sstevel@tonic-gate 			dmp32.dkl_nblk = dmp.dkl_nblk;
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 			if (ddi_copyout((caddr_t)&dmp32, data, sizeof (dmp32),
12717563SPrasad.Singamsetty@Sun.COM 			    mode) != 0)
12720Sstevel@tonic-gate 				err = EFAULT;
12730Sstevel@tonic-gate 		}
12740Sstevel@tonic-gate #endif /* _SYSCALL32 */
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 		return (err);
12770Sstevel@tonic-gate 	}
12780Sstevel@tonic-gate 	case DKIOCGETEFI:
12790Sstevel@tonic-gate 	{
12800Sstevel@tonic-gate 		/*
12810Sstevel@tonic-gate 		 * This one can be done centralized,
12820Sstevel@tonic-gate 		 * no need to put in the same code for all types of metadevices
12830Sstevel@tonic-gate 		 */
12840Sstevel@tonic-gate 		return (md_dkiocgetefi(mnum, data, mode));
12850Sstevel@tonic-gate 	}
12860Sstevel@tonic-gate 	case DKIOCSETEFI:
12870Sstevel@tonic-gate 	{
12880Sstevel@tonic-gate 		/*
12890Sstevel@tonic-gate 		 * This one can be done centralized,
12900Sstevel@tonic-gate 		 * no need to put in the same code for all types of metadevices
12910Sstevel@tonic-gate 		 */
12920Sstevel@tonic-gate 		return (md_dkiocsetefi(mnum, data, mode));
12930Sstevel@tonic-gate 	}
12940Sstevel@tonic-gate 	case DKIOCPARTITION:
12950Sstevel@tonic-gate 	{
12960Sstevel@tonic-gate 		return (md_dkiocpartition(mnum, data, mode));
12970Sstevel@tonic-gate 	}
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	default:
13000Sstevel@tonic-gate 		return (ENOTTY);
13010Sstevel@tonic-gate 	}
13020Sstevel@tonic-gate }
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate /*
13050Sstevel@tonic-gate  * rename named service entry points and support functions
13060Sstevel@tonic-gate  */
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate /*
13090Sstevel@tonic-gate  * rename/exchange role swap functions are handled generically
13100Sstevel@tonic-gate  */
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate /*
13130Sstevel@tonic-gate  * support routine for MDRNM_CHECK
13140Sstevel@tonic-gate  */
13150Sstevel@tonic-gate static int
stripe_may_renexch_self(ms_unit_t * un,mdi_unit_t * ui,md_rentxn_t * rtxnp)13160Sstevel@tonic-gate stripe_may_renexch_self(
13170Sstevel@tonic-gate 		ms_unit_t	*un,
13180Sstevel@tonic-gate 		mdi_unit_t	*ui,
13190Sstevel@tonic-gate 		md_rentxn_t	*rtxnp)
13200Sstevel@tonic-gate {
13210Sstevel@tonic-gate 	minor_t		 from_min;
13220Sstevel@tonic-gate 	minor_t		 to_min;
13230Sstevel@tonic-gate 	bool_t		 toplevel;
13240Sstevel@tonic-gate 	bool_t		 related;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	ASSERT(rtxnp);
13270Sstevel@tonic-gate 	ASSERT((rtxnp->op == MDRNOP_RENAME) || (rtxnp->op == MDRNOP_EXCHANGE));
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	from_min = rtxnp->from.mnum;
13300Sstevel@tonic-gate 	to_min = rtxnp->to.mnum;
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	if (!un || !ui) {
13330Sstevel@tonic-gate 		(void) mdmderror(&rtxnp->mde, MDE_RENAME_CONFIG_ERROR,
13347563SPrasad.Singamsetty@Sun.COM 		    from_min);
13350Sstevel@tonic-gate 		return (EINVAL);
13360Sstevel@tonic-gate 	}
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 	ASSERT(!(MD_CAPAB(un) & MD_CAN_META_CHILD));
13390Sstevel@tonic-gate 	if (MD_CAPAB(un) & MD_CAN_META_CHILD) {
13400Sstevel@tonic-gate 		(void) mdmderror(&rtxnp->mde, MDE_RENAME_SOURCE_BAD, from_min);
13410Sstevel@tonic-gate 		return (EINVAL);
13420Sstevel@tonic-gate 	}
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	if (MD_PARENT(un) == MD_MULTI_PARENT) {
13450Sstevel@tonic-gate 		(void) mdmderror(&rtxnp->mde, MDE_RENAME_SOURCE_BAD, from_min);
13460Sstevel@tonic-gate 		return (EINVAL);
13470Sstevel@tonic-gate 	}
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	toplevel = !MD_HAS_PARENT(MD_PARENT(un));
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	/* we're related if trying to swap with our parent */
13520Sstevel@tonic-gate 	related  = (!toplevel) && (MD_PARENT(un) == to_min);
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	switch (rtxnp->op) {
13550Sstevel@tonic-gate 	case MDRNOP_EXCHANGE:
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 		if (!related) {
13580Sstevel@tonic-gate 			(void) mdmderror(&rtxnp->mde,
13597563SPrasad.Singamsetty@Sun.COM 			    MDE_RENAME_TARGET_UNRELATED, to_min);
13600Sstevel@tonic-gate 			return (EINVAL);
13610Sstevel@tonic-gate 		}
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 		break;
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	case MDRNOP_RENAME:
13660Sstevel@tonic-gate 		/*
13670Sstevel@tonic-gate 		 * if from is top-level and is open, then the kernel is using
13680Sstevel@tonic-gate 		 * the device and we return EBUSY.
13690Sstevel@tonic-gate 		 */
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 		if (toplevel && md_unit_isopen(ui)) {
13720Sstevel@tonic-gate 			(void) mdmderror(&rtxnp->mde, MDE_RENAME_BUSY,
13737563SPrasad.Singamsetty@Sun.COM 			    from_min);
13740Sstevel@tonic-gate 			return (EBUSY);
13750Sstevel@tonic-gate 		}
13760Sstevel@tonic-gate 		break;
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	default:
13790Sstevel@tonic-gate 		(void) mdmderror(&rtxnp->mde, MDE_RENAME_CONFIG_ERROR,
13807563SPrasad.Singamsetty@Sun.COM 		    from_min);
13810Sstevel@tonic-gate 		return (EINVAL);
13820Sstevel@tonic-gate 	}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	return (0);	/* ok */
13850Sstevel@tonic-gate }
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate /*
13880Sstevel@tonic-gate  * Named service entry point: MDRNM_CHECK
13890Sstevel@tonic-gate  */
13900Sstevel@tonic-gate intptr_t
stripe_rename_check(md_rendelta_t * delta,md_rentxn_t * rtxnp)13910Sstevel@tonic-gate stripe_rename_check(
13920Sstevel@tonic-gate 	md_rendelta_t	*delta,
13930Sstevel@tonic-gate 	md_rentxn_t	*rtxnp)
13940Sstevel@tonic-gate {
13950Sstevel@tonic-gate 	int	err	= 0;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	ASSERT(delta);
13980Sstevel@tonic-gate 	ASSERT(rtxnp);
13990Sstevel@tonic-gate 	ASSERT(delta->unp);
14000Sstevel@tonic-gate 	ASSERT(delta->uip);
14010Sstevel@tonic-gate 	ASSERT((rtxnp->op == MDRNOP_RENAME) || (MDRNOP_EXCHANGE == rtxnp->op));
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	if (!delta || !rtxnp || !delta->uip || !delta->unp) {
14040Sstevel@tonic-gate 		(void) mdsyserror(&rtxnp->mde, EINVAL);
14050Sstevel@tonic-gate 		return (EINVAL);
14060Sstevel@tonic-gate 	}
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	/* self does additional checks */
14090Sstevel@tonic-gate 	if (delta->old_role == MDRR_SELF) {
14100Sstevel@tonic-gate 		err = stripe_may_renexch_self((ms_unit_t *)delta->unp,
14117563SPrasad.Singamsetty@Sun.COM 		    delta->uip, rtxnp);
14120Sstevel@tonic-gate 	}
14130Sstevel@tonic-gate out:
14140Sstevel@tonic-gate 	return (err);
14150Sstevel@tonic-gate }
14160Sstevel@tonic-gate /* end of rename/exchange */
1417