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 /*
280Sstevel@tonic-gate * NAME: raid_ioctl.c
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * DESCRIPTION: RAID driver source file containing IOCTL operations.
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * ROUTINES PROVIDED FOR EXTERNAL USE:
330Sstevel@tonic-gate * raid_commit() - commits MD database updates for a RAID metadevice
340Sstevel@tonic-gate * md_raid_ioctl() - RAID metadevice IOCTL operations entry point.
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * ROUTINES PROVIDED FOR INTERNAL USE:
370Sstevel@tonic-gate * raid_getun() - Performs unit checking on a RAID metadevice
380Sstevel@tonic-gate * init_col_nextio() - normal backend when zeroing column of RAID metadevice.
390Sstevel@tonic-gate * init_col_int() - I/O interrupt while zeroing column of RAID metadevice.
400Sstevel@tonic-gate * raid_init_columns() - Zero one or more columns of a RAID metadevice.
410Sstevel@tonic-gate * raid_set() - used to create a RAID metadevice
420Sstevel@tonic-gate * raid_get() - used to get the unit structure of a RAID metadevice
430Sstevel@tonic-gate * raid_replace() - used to replace a component of a RAID metadevice
440Sstevel@tonic-gate * raid_grow() - Concatenate to a RAID metadevice
450Sstevel@tonic-gate * raid_change() - change dynamic values of a RAID metadevice
460Sstevel@tonic-gate * raid_reset() - used to reset (clear / remove) a RAID metadevice
470Sstevel@tonic-gate * raid_get_geom() - used to get the geometry of a RAID metadevice
480Sstevel@tonic-gate * raid_get_vtoc() - used to get the VTOC on a RAID metadevice
490Sstevel@tonic-gate * raid_set_vtoc() - used to set the VTOC on a RAID metadevice
507563SPrasad.Singamsetty@Sun.COM * raid_get_extvtoc() - used to get the extended VTOC on a RAID metadevice
517563SPrasad.Singamsetty@Sun.COM * raid_set_extvtoc() - used to set the extended VTOC on a RAID metadevice
520Sstevel@tonic-gate * raid_getdevs() - return all devices within a RAID metadevice
530Sstevel@tonic-gate * raid_admin_ioctl() - IOCTL operations unique to metadevices and RAID
540Sstevel@tonic-gate */
550Sstevel@tonic-gate
560Sstevel@tonic-gate
570Sstevel@tonic-gate #include <sys/param.h>
580Sstevel@tonic-gate #include <sys/systm.h>
590Sstevel@tonic-gate #include <sys/conf.h>
600Sstevel@tonic-gate #include <sys/file.h>
610Sstevel@tonic-gate #include <sys/user.h>
620Sstevel@tonic-gate #include <sys/uio.h>
630Sstevel@tonic-gate #include <sys/t_lock.h>
640Sstevel@tonic-gate #include <sys/buf.h>
650Sstevel@tonic-gate #include <sys/dkio.h>
660Sstevel@tonic-gate #include <sys/vtoc.h>
670Sstevel@tonic-gate #include <sys/kmem.h>
680Sstevel@tonic-gate #include <vm/page.h>
690Sstevel@tonic-gate #include <sys/sysmacros.h>
700Sstevel@tonic-gate #include <sys/types.h>
710Sstevel@tonic-gate #include <sys/mkdev.h>
720Sstevel@tonic-gate #include <sys/stat.h>
730Sstevel@tonic-gate #include <sys/open.h>
740Sstevel@tonic-gate #include <sys/disp.h>
750Sstevel@tonic-gate #include <sys/modctl.h>
760Sstevel@tonic-gate #include <sys/ddi.h>
770Sstevel@tonic-gate #include <sys/sunddi.h>
780Sstevel@tonic-gate #include <sys/cred.h>
790Sstevel@tonic-gate #include <sys/lvm/mdvar.h>
800Sstevel@tonic-gate #include <sys/lvm/md_names.h>
810Sstevel@tonic-gate #include <sys/lvm/md_mddb.h>
820Sstevel@tonic-gate #include <sys/lvm/md_raid.h>
830Sstevel@tonic-gate #include <sys/lvm/md_convert.h>
840Sstevel@tonic-gate
850Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
860Sstevel@tonic-gate #include <sys/sysevent/svm.h>
870Sstevel@tonic-gate
880Sstevel@tonic-gate extern int md_status;
890Sstevel@tonic-gate extern unit_t md_nunits;
900Sstevel@tonic-gate extern set_t md_nsets;
910Sstevel@tonic-gate extern md_set_t md_set[];
920Sstevel@tonic-gate extern md_ops_t raid_md_ops;
930Sstevel@tonic-gate extern major_t md_major;
940Sstevel@tonic-gate extern md_krwlock_t md_unit_array_rw;
950Sstevel@tonic-gate extern mdq_anchor_t md_done_daemon;
960Sstevel@tonic-gate extern mdq_anchor_t md_ff_daemonq;
970Sstevel@tonic-gate extern int mdopen();
980Sstevel@tonic-gate extern int mdclose();
990Sstevel@tonic-gate extern void md_probe_one();
1000Sstevel@tonic-gate extern int md_init_probereq(md_probedev_impl_t *,
1010Sstevel@tonic-gate daemon_queue_t **);
1020Sstevel@tonic-gate extern md_resync_t md_cpr_resync;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate extern void dump_mr_unit(mr_unit_t *);
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate typedef struct raid_ci {
1080Sstevel@tonic-gate DAEMON_QUEUE
1090Sstevel@tonic-gate struct raid_ci *ci_next;
1100Sstevel@tonic-gate mr_unit_t *ci_un;
1110Sstevel@tonic-gate int ci_col;
1120Sstevel@tonic-gate int ci_err;
1130Sstevel@tonic-gate int ci_flag;
1140Sstevel@tonic-gate size_t ci_zerosize;
1150Sstevel@tonic-gate diskaddr_t ci_blkno;
1160Sstevel@tonic-gate diskaddr_t ci_lastblk;
1170Sstevel@tonic-gate buf_t ci_buf;
1180Sstevel@tonic-gate } raid_ci_t;
1190Sstevel@tonic-gate /* values for the ci_flag */
1200Sstevel@tonic-gate #define COL_INITING (0x0001)
1210Sstevel@tonic-gate #define COL_INIT_DONE (0x0002)
1220Sstevel@tonic-gate #define COL_READY (0x0004)
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate * NAME: raid_getun
1260Sstevel@tonic-gate * DESCRIPTION: performs a lot of unit checking on a RAID metadevice
1270Sstevel@tonic-gate * PARAMETERS: minor_t mnum - minor device number for RAID unit
1280Sstevel@tonic-gate * md_error_t *mde - pointer to error reporting structure
1290Sstevel@tonic-gate * int flags - pointer to error reporting structure
1300Sstevel@tonic-gate * STALE_OK - allow stale MD memory
1310Sstevel@tonic-gate * NO_OLD - unit must not exist
1320Sstevel@tonic-gate * NO_LOCK - no IOCTL lock needed
1330Sstevel@tonic-gate * WR_LOCK - write IOCTL lock needed
1340Sstevel@tonic-gate * RD_LOCK - read IOCTL lock needed
1350Sstevel@tonic-gate * IOLOCK *lock - pointer to IOCTL lock
1360Sstevel@tonic-gate *
1370Sstevel@tonic-gate * LOCKS: obtains unit reader or writer lock via IOLOCK
1380Sstevel@tonic-gate *
1390Sstevel@tonic-gate */
1400Sstevel@tonic-gate static mr_unit_t *
raid_getun(minor_t mnum,md_error_t * mde,int flags,IOLOCK * lock)1410Sstevel@tonic-gate raid_getun(minor_t mnum, md_error_t *mde, int flags, IOLOCK *lock)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate mr_unit_t *un;
1440Sstevel@tonic-gate mdi_unit_t *ui;
1450Sstevel@tonic-gate set_t setno = MD_MIN2SET(mnum);
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate if ((setno >= md_nsets) || (MD_MIN2UNIT(mnum) >= md_nunits)) {
1480Sstevel@tonic-gate (void) mdmderror(mde, MDE_INVAL_UNIT, mnum);
1490Sstevel@tonic-gate return (NULL);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate if (!(flags & STALE_OK)) {
1530Sstevel@tonic-gate if (md_get_setstatus(setno) & MD_SET_STALE) {
1540Sstevel@tonic-gate (void) mdmddberror(mde, MDE_DB_STALE, mnum, setno);
1550Sstevel@tonic-gate return (NULL);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate ui = MDI_UNIT(mnum);
1600Sstevel@tonic-gate if (flags & NO_OLD) {
1610Sstevel@tonic-gate if (ui != NULL) {
1620Sstevel@tonic-gate (void) mdmderror(mde, MDE_UNIT_ALREADY_SETUP, mnum);
1630Sstevel@tonic-gate return (NULL);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate return ((mr_unit_t *)1);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if (ui == NULL) {
1690Sstevel@tonic-gate (void) mdmderror(mde, MDE_UNIT_NOT_SETUP, mnum);
1700Sstevel@tonic-gate return (NULL);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate if (flags & ARRAY_WRITER)
1730Sstevel@tonic-gate md_array_writer(lock);
1740Sstevel@tonic-gate else if (flags & ARRAY_READER)
1750Sstevel@tonic-gate md_array_reader(lock);
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate if (!(flags & NO_LOCK)) {
1780Sstevel@tonic-gate if (flags & WR_LOCK) {
1790Sstevel@tonic-gate (void) md_ioctl_io_lock(lock, ui);
1800Sstevel@tonic-gate (void) md_ioctl_writerlock(lock, ui);
1810Sstevel@tonic-gate } else /* RD_LOCK */
1820Sstevel@tonic-gate (void) md_ioctl_readerlock(lock, ui);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate un = (mr_unit_t *)MD_UNIT(mnum);
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate if (un->c.un_type != MD_METARAID) {
1870Sstevel@tonic-gate (void) mdmderror(mde, MDE_NOT_RAID, mnum);
1880Sstevel@tonic-gate return (NULL);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate return (un);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate * NAME: raid_commit
1970Sstevel@tonic-gate * DESCRIPTION: commits MD database updates for a RAID metadevice
1980Sstevel@tonic-gate * PARAMETERS: mr_unit_t *un - RAID unit to update in the MD database
1990Sstevel@tonic-gate * mddb_recid_t *extras - array of other record IDs to update
2000Sstevel@tonic-gate *
2010Sstevel@tonic-gate * LOCKS: assumes caller holds unit writer lock
2020Sstevel@tonic-gate *
2030Sstevel@tonic-gate */
2040Sstevel@tonic-gate void
raid_commit(mr_unit_t * un,mddb_recid_t * extras)2050Sstevel@tonic-gate raid_commit(mr_unit_t *un, mddb_recid_t *extras)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate mddb_recid_t *recids;
2080Sstevel@tonic-gate int ri = 0;
2090Sstevel@tonic-gate int nrecids = 0;
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate if (md_get_setstatus(MD_UN2SET(un)) & MD_SET_STALE)
2120Sstevel@tonic-gate return;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate /* Count the extra recids */
2150Sstevel@tonic-gate if (extras != NULL) {
2160Sstevel@tonic-gate while (extras[nrecids] != 0) {
2170Sstevel@tonic-gate nrecids++;
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate * Allocate space for two recids in addition to the extras:
2230Sstevel@tonic-gate * one for the unit structure, one for the null terminator.
2240Sstevel@tonic-gate */
2250Sstevel@tonic-gate nrecids += 2;
2260Sstevel@tonic-gate recids = (mddb_recid_t *)
2270Sstevel@tonic-gate kmem_zalloc(nrecids * sizeof (mddb_recid_t), KM_SLEEP);
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate if (un != NULL) {
2300Sstevel@tonic-gate ASSERT(MDI_UNIT(MD_SID(un)) ? UNIT_WRITER_HELD(un) : 1);
2310Sstevel@tonic-gate recids[ri++] = un->c.un_record_id;
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate if (extras != NULL) {
2350Sstevel@tonic-gate while (*extras != 0) {
2360Sstevel@tonic-gate recids[ri++] = *extras;
2370Sstevel@tonic-gate extras++;
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate if (ri > 0) {
2420Sstevel@tonic-gate mddb_commitrecs_wrapper(recids);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate kmem_free(recids, nrecids * sizeof (mddb_recid_t));
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate static int
raid_check_pw(mr_unit_t * un)2490Sstevel@tonic-gate raid_check_pw(mr_unit_t *un)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate buf_t bp;
2520Sstevel@tonic-gate char *buf;
2530Sstevel@tonic-gate mr_column_t *colptr;
2540Sstevel@tonic-gate minor_t mnum = MD_SID(un);
2550Sstevel@tonic-gate int i;
2560Sstevel@tonic-gate int err = 0;
2570Sstevel@tonic-gate minor_t unit;
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate buf = kmem_zalloc((uint_t)DEV_BSIZE, KM_SLEEP);
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate for (i = 0; i < un->un_totalcolumncnt; i++) {
2620Sstevel@tonic-gate md_dev64_t tmpdev;
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate colptr = &un->un_column[i];
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate tmpdev = colptr->un_dev;
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate * Open by device id
2690Sstevel@tonic-gate * If this device is hotspared
2700Sstevel@tonic-gate * use the hotspare key
2710Sstevel@tonic-gate */
2720Sstevel@tonic-gate tmpdev = md_resolve_bydevid(mnum, tmpdev, HOTSPARED(un, i) ?
2737563SPrasad.Singamsetty@Sun.COM colptr->un_hs_key : colptr->un_orig_key);
2740Sstevel@tonic-gate if (md_layered_open(mnum, &tmpdev, MD_OFLG_NULL)) {
2750Sstevel@tonic-gate colptr->un_dev = tmpdev;
2760Sstevel@tonic-gate return (1);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate colptr->un_dev = tmpdev;
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate bzero((caddr_t)&bp, sizeof (buf_t));
2810Sstevel@tonic-gate bp.b_back = &bp;
2820Sstevel@tonic-gate bp.b_forw = &bp;
2830Sstevel@tonic-gate bp.b_flags = B_READ | B_BUSY;
2840Sstevel@tonic-gate sema_init(&bp.b_io, 0, NULL,
2850Sstevel@tonic-gate SEMA_DEFAULT, NULL);
2860Sstevel@tonic-gate sema_init(&bp.b_sem, 0, NULL,
2870Sstevel@tonic-gate SEMA_DEFAULT, NULL);
2880Sstevel@tonic-gate bp.b_edev = md_dev64_to_dev(colptr->un_dev);
2890Sstevel@tonic-gate bp.b_lblkno = colptr->un_pwstart;
2900Sstevel@tonic-gate bp.b_bcount = DEV_BSIZE;
2910Sstevel@tonic-gate bp.b_bufsize = DEV_BSIZE;
2920Sstevel@tonic-gate bp.b_un.b_addr = (caddr_t)buf;
2930Sstevel@tonic-gate bp.b_offset = -1;
2940Sstevel@tonic-gate (void) md_call_strategy(&bp, 0, NULL);
2950Sstevel@tonic-gate if (biowait(&bp))
2960Sstevel@tonic-gate err = 1;
2970Sstevel@tonic-gate if (i == 0) {
2981623Stw21770 if (un->c.un_revision & MD_64BIT_META_DEV) {
2990Sstevel@tonic-gate unit = ((raid_pwhdr_t *)buf)->rpw_unit;
3000Sstevel@tonic-gate } else {
3010Sstevel@tonic-gate unit = ((raid_pwhdr32_od_t *)buf)->rpw_unit;
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate /*
3050Sstevel@tonic-gate * depending upon being an 64bit or 32 bit raid, the
3060Sstevel@tonic-gate * pre write headers have different layout
3070Sstevel@tonic-gate */
3081623Stw21770 if (un->c.un_revision & MD_64BIT_META_DEV) {
3090Sstevel@tonic-gate if ((((raid_pwhdr_t *)buf)->rpw_column != i) ||
3100Sstevel@tonic-gate (((raid_pwhdr_t *)buf)->rpw_unit != unit))
3110Sstevel@tonic-gate err = 1;
3120Sstevel@tonic-gate } else {
3130Sstevel@tonic-gate if ((((raid_pwhdr32_od_t *)buf)->rpw_column != i) ||
3140Sstevel@tonic-gate (((raid_pwhdr32_od_t *)buf)->rpw_unit != unit))
3150Sstevel@tonic-gate err = 1;
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate md_layered_close(colptr->un_dev, MD_OFLG_NULL);
3180Sstevel@tonic-gate if (err)
3190Sstevel@tonic-gate break;
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate kmem_free(buf, DEV_BSIZE);
3220Sstevel@tonic-gate return (err);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate * NAME: init_col_nextio
3270Sstevel@tonic-gate * DESCRIPTION: normal backend process when zeroing column of a RAID metadevice.
3280Sstevel@tonic-gate * PARAMETERS: raid_ci_t *cur - struct for column being zeroed
3290Sstevel@tonic-gate *
3300Sstevel@tonic-gate * LOCKS: assumes caller holds unit reader lock,
3310Sstevel@tonic-gate * preiodically releases and reacquires unit reader lock,
3320Sstevel@tonic-gate * broadcasts on unit conditional variable (un_cv)
3330Sstevel@tonic-gate *
3340Sstevel@tonic-gate */
3350Sstevel@tonic-gate #define INIT_RLS_CNT 10
3360Sstevel@tonic-gate static void
init_col_nextio(raid_ci_t * cur)3370Sstevel@tonic-gate init_col_nextio(raid_ci_t *cur)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate mr_unit_t *un;
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate un = cur->ci_un;
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate cur->ci_blkno += cur->ci_zerosize;
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate mutex_enter(&un->un_mx);
3460Sstevel@tonic-gate /* ===> update un_percent_done */
3470Sstevel@tonic-gate un->un_init_iocnt += btodb(cur->ci_buf.b_bcount);
3480Sstevel@tonic-gate mutex_exit(&un->un_mx);
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate /*
3510Sstevel@tonic-gate * When gorwing a device, normal I/O is still going on.
3520Sstevel@tonic-gate * The init thread still holds the unit reader lock which
3530Sstevel@tonic-gate * prevents I/O from doing state changes.
3540Sstevel@tonic-gate * So every INIT_RLS_CNT init I/Os, we will release the
3550Sstevel@tonic-gate * unit reader lock.
3560Sstevel@tonic-gate *
3570Sstevel@tonic-gate * CAVEAT:
3580Sstevel@tonic-gate * We know we are in the middle of a grow operation and the
3590Sstevel@tonic-gate * unit cannot be grown or removed (through reset or halt)
3600Sstevel@tonic-gate * so the mr_unit_t structure will not move or disappear.
3610Sstevel@tonic-gate * In addition, we know that only one of the init I/Os
3620Sstevel@tonic-gate * can be in col_init_nextio at a time because they are
3630Sstevel@tonic-gate * placed on the md_done_daemon queue and md only processes
3640Sstevel@tonic-gate * one element of this queue at a time. In addition, any
3650Sstevel@tonic-gate * code that needs to acquire the unit writer lock to change
3660Sstevel@tonic-gate * state is supposed to be on the md_mstr_daemon queue so
3670Sstevel@tonic-gate * it can be processing while we sit here waiting to get the
3680Sstevel@tonic-gate * unit reader lock back.
3690Sstevel@tonic-gate */
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate if (cur->ci_blkno < cur->ci_lastblk) {
3720Sstevel@tonic-gate /* truncate last chunk to end_addr if needed */
3730Sstevel@tonic-gate if (cur->ci_blkno + cur->ci_zerosize > cur->ci_lastblk) {
3740Sstevel@tonic-gate cur->ci_zerosize = (size_t)
3757563SPrasad.Singamsetty@Sun.COM (cur->ci_lastblk - cur->ci_blkno);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate /* set address and length for I/O bufs */
3790Sstevel@tonic-gate cur->ci_buf.b_bufsize = dbtob(cur->ci_zerosize);
3800Sstevel@tonic-gate cur->ci_buf.b_bcount = dbtob(cur->ci_zerosize);
3810Sstevel@tonic-gate cur->ci_buf.b_lblkno = cur->ci_blkno;
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate (void) md_call_strategy(&cur->ci_buf, MD_STR_NOTTOP, NULL);
3840Sstevel@tonic-gate return;
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate /* finished initializing this column */
3870Sstevel@tonic-gate mutex_enter(&un->un_mx);
3880Sstevel@tonic-gate cur->ci_flag = COL_INIT_DONE;
3890Sstevel@tonic-gate uniqtime32(&un->un_column[cur->ci_col].un_devtimestamp);
3900Sstevel@tonic-gate mutex_exit(&un->un_mx);
3910Sstevel@tonic-gate cv_broadcast(&un->un_cv);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate * NAME: init_col_int
3960Sstevel@tonic-gate * DESCRIPTION: I/O interrupt while zeroing column of a RAID metadevice.
3970Sstevel@tonic-gate * PARAMETERS: buf_t *cb - I/O buffer for which interrupt occurred
3980Sstevel@tonic-gate *
3990Sstevel@tonic-gate * LOCKS: assumes caller holds unit reader or writer lock
4000Sstevel@tonic-gate *
4010Sstevel@tonic-gate */
4020Sstevel@tonic-gate static int
init_col_int(buf_t * cb)4030Sstevel@tonic-gate init_col_int(buf_t *cb)
4040Sstevel@tonic-gate {
4050Sstevel@tonic-gate raid_ci_t *cur;
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate cur = (raid_ci_t *)cb->b_chain;
4080Sstevel@tonic-gate if (cb->b_flags & B_ERROR) {
4090Sstevel@tonic-gate mutex_enter(&cur->ci_un->un_mx);
4100Sstevel@tonic-gate cur->ci_err = EIO;
4110Sstevel@tonic-gate mutex_exit(&cur->ci_un->un_mx);
4120Sstevel@tonic-gate cv_broadcast(&cur->ci_un->un_cv);
4130Sstevel@tonic-gate return (1);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate daemon_request(&md_done_daemon, init_col_nextio,
4167563SPrasad.Singamsetty@Sun.COM (daemon_queue_t *)cur, REQ_OLD);
4170Sstevel@tonic-gate return (1);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate * NAME: raid_init_columns
4220Sstevel@tonic-gate * DESCRIPTION: Zero one or more columns of a RAID metadevice.
4230Sstevel@tonic-gate * PARAMETERS: minor_t mnum - RAID unit minor identifier
4240Sstevel@tonic-gate *
4250Sstevel@tonic-gate * LOCKS: obtains and releases unit reader lock,
4260Sstevel@tonic-gate * obtains and releases unit writer lock,
4270Sstevel@tonic-gate * obtains and releases md_unit_array_rw write lock,
4280Sstevel@tonic-gate * obtains and releases unit mutex (un_mx) lock,
4290Sstevel@tonic-gate * waits on unit conditional variable (un_cv)
4300Sstevel@tonic-gate *
4310Sstevel@tonic-gate */
4320Sstevel@tonic-gate static void
raid_init_columns(minor_t mnum)4330Sstevel@tonic-gate raid_init_columns(minor_t mnum)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate mr_unit_t *un;
4360Sstevel@tonic-gate mdi_unit_t *ui;
4370Sstevel@tonic-gate raid_ci_t *ci_chain = NULL, *cur;
4380Sstevel@tonic-gate rus_state_t state;
4390Sstevel@tonic-gate caddr_t zero_addr;
4400Sstevel@tonic-gate diskaddr_t end_off;
4410Sstevel@tonic-gate size_t zerosize;
4420Sstevel@tonic-gate int err = 0;
4430Sstevel@tonic-gate int ix;
4440Sstevel@tonic-gate int colcnt = 0;
4450Sstevel@tonic-gate int col;
4460Sstevel@tonic-gate set_t setno = MD_MIN2SET(mnum);
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate /*
4490Sstevel@tonic-gate * Increment the raid resync count for cpr
4500Sstevel@tonic-gate */
4510Sstevel@tonic-gate mutex_enter(&md_cpr_resync.md_resync_mutex);
4520Sstevel@tonic-gate md_cpr_resync.md_raid_resync++;
4530Sstevel@tonic-gate mutex_exit(&md_cpr_resync.md_resync_mutex);
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate * initialization is a multiple step process. The first step
4570Sstevel@tonic-gate * is to go through the unit structure and start each device
4580Sstevel@tonic-gate * in the init state writing zeros over the component.
4590Sstevel@tonic-gate * Next initialize the prewrite areas, so the device can be
4600Sstevel@tonic-gate * used if a metainit -k is done. Now close the componenets.
4610Sstevel@tonic-gate *
4620Sstevel@tonic-gate * Once this complete set the state of each component being
4630Sstevel@tonic-gate * zeroed and set the correct state for the unit.
4640Sstevel@tonic-gate *
4650Sstevel@tonic-gate * last commit the records.
4660Sstevel@tonic-gate */
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate ui = MDI_UNIT(mnum);
4690Sstevel@tonic-gate un = md_unit_readerlock(ui);
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate /* check for active init on this column */
4720Sstevel@tonic-gate /* exiting is cpr safe */
4730Sstevel@tonic-gate if ((un->un_init_colcnt > 0) && (un->un_resync_index != -1)) {
4740Sstevel@tonic-gate md_unit_readerexit(ui);
4750Sstevel@tonic-gate (void) raid_internal_close(mnum, OTYP_LYR, 0, 0);
4760Sstevel@tonic-gate /*
4770Sstevel@tonic-gate * Decrement the raid resync count for cpr
4780Sstevel@tonic-gate */
4790Sstevel@tonic-gate mutex_enter(&md_cpr_resync.md_resync_mutex);
4800Sstevel@tonic-gate md_cpr_resync.md_raid_resync--;
4810Sstevel@tonic-gate mutex_exit(&md_cpr_resync.md_resync_mutex);
4820Sstevel@tonic-gate thread_exit();
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_INIT_START, SVM_TAG_METADEVICE, setno,
4860Sstevel@tonic-gate MD_SID(un));
4870Sstevel@tonic-gate un->un_init_colcnt = 0;
4880Sstevel@tonic-gate un->un_init_iocnt = 0;
4890Sstevel@tonic-gate end_off = un->un_pwsize + (un->un_segsize * un->un_segsincolumn);
4900Sstevel@tonic-gate zerosize = (size_t)MIN((diskaddr_t)un->un_maxio, end_off);
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate /* allocate zero-filled buffer */
4930Sstevel@tonic-gate zero_addr = kmem_zalloc(dbtob(zerosize), KM_SLEEP);
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate for (ix = 0; ix < un->un_totalcolumncnt; ix++) {
4960Sstevel@tonic-gate if (un->un_column[ix].un_devstate != RCS_INIT)
4970Sstevel@tonic-gate continue;
4980Sstevel@tonic-gate /* allocate new column init structure */
4990Sstevel@tonic-gate cur = (raid_ci_t *)kmem_zalloc((sizeof (raid_ci_t)), KM_SLEEP);
5000Sstevel@tonic-gate ASSERT(cur != NULL);
5010Sstevel@tonic-gate un->un_init_colcnt++;
5020Sstevel@tonic-gate cur->ci_next = ci_chain;
5030Sstevel@tonic-gate ci_chain = cur;
5040Sstevel@tonic-gate cur->ci_un = un;
5050Sstevel@tonic-gate cur->ci_col = ix;
5060Sstevel@tonic-gate cur->ci_err = 0;
5070Sstevel@tonic-gate cur->ci_flag = COL_INITING;
5080Sstevel@tonic-gate cur->ci_zerosize = zerosize;
5090Sstevel@tonic-gate cur->ci_blkno = un->un_column[ix].un_pwstart;
5100Sstevel@tonic-gate cur->ci_lastblk = cur->ci_blkno + un->un_pwsize
5110Sstevel@tonic-gate + (un->un_segsize * un->un_segsincolumn);
5120Sstevel@tonic-gate /* initialize static buf fields */
5130Sstevel@tonic-gate cur->ci_buf.b_un.b_addr = zero_addr;
5140Sstevel@tonic-gate cur->ci_buf.b_chain = (buf_t *)cur;
5150Sstevel@tonic-gate cur->ci_buf.b_back = &cur->ci_buf;
5160Sstevel@tonic-gate cur->ci_buf.b_forw = &cur->ci_buf;
5170Sstevel@tonic-gate cur->ci_buf.b_iodone = init_col_int;
5180Sstevel@tonic-gate cur->ci_buf.b_flags = B_BUSY | B_WRITE;
5190Sstevel@tonic-gate cur->ci_buf.b_edev = md_dev64_to_dev(un->un_column[ix].un_dev);
5207563SPrasad.Singamsetty@Sun.COM sema_init(&cur->ci_buf.b_io, 0, NULL, SEMA_DEFAULT, NULL);
5217563SPrasad.Singamsetty@Sun.COM sema_init(&cur->ci_buf.b_sem, 0, NULL, SEMA_DEFAULT, NULL);
5220Sstevel@tonic-gate /* set address and length for I/O bufs */
5230Sstevel@tonic-gate cur->ci_buf.b_bufsize = dbtob(zerosize);
5240Sstevel@tonic-gate cur->ci_buf.b_bcount = dbtob(zerosize);
5250Sstevel@tonic-gate cur->ci_buf.b_lblkno = un->un_column[ix].un_pwstart;
5260Sstevel@tonic-gate cur->ci_buf.b_offset = -1;
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate if (! (un->un_column[ix].un_devflags & MD_RAID_DEV_ISOPEN)) {
5290Sstevel@tonic-gate md_dev64_t tmpdev = un->un_column[ix].un_dev;
5300Sstevel@tonic-gate /*
5310Sstevel@tonic-gate * Open by device id
5320Sstevel@tonic-gate * If this column is hotspared then
5330Sstevel@tonic-gate * use the hotspare key
5340Sstevel@tonic-gate */
5350Sstevel@tonic-gate tmpdev = md_resolve_bydevid(mnum, tmpdev,
5367563SPrasad.Singamsetty@Sun.COM HOTSPARED(un, ix) ?
5377563SPrasad.Singamsetty@Sun.COM un->un_column[ix].un_hs_key :
5387563SPrasad.Singamsetty@Sun.COM un->un_column[ix].un_orig_key);
5390Sstevel@tonic-gate if ((cur->ci_err = md_layered_open(mnum, &tmpdev,
5400Sstevel@tonic-gate MD_OFLG_NULL)) == 0)
5410Sstevel@tonic-gate un->un_column[ix].un_devflags |=
5420Sstevel@tonic-gate MD_RAID_DEV_ISOPEN;
5430Sstevel@tonic-gate un->un_column[ix].un_dev = tmpdev;
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate if (cur->ci_err == 0)
5460Sstevel@tonic-gate md_call_strategy(&cur->ci_buf, MD_STR_NOTTOP, NULL);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate md_unit_readerexit(ui);
5500Sstevel@tonic-gate state = un->un_state;
5510Sstevel@tonic-gate colcnt = un->un_init_colcnt;
5520Sstevel@tonic-gate mutex_enter(&un->un_mx);
5530Sstevel@tonic-gate while (colcnt) {
5540Sstevel@tonic-gate cv_wait(&un->un_cv, &un->un_mx);
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate colcnt = 0;
5570Sstevel@tonic-gate for (cur = ci_chain; cur != NULL; cur = cur->ci_next) {
5580Sstevel@tonic-gate col = cur->ci_col;
5590Sstevel@tonic-gate if ((cur->ci_flag != COL_INITING) || (cur->ci_err)) {
5600Sstevel@tonic-gate if (cur->ci_err)
5610Sstevel@tonic-gate err = cur->ci_err;
5620Sstevel@tonic-gate else if (cur->ci_flag == COL_INIT_DONE) {
5630Sstevel@tonic-gate (void) init_pw_area(un,
5647563SPrasad.Singamsetty@Sun.COM un->un_column[col].un_dev,
5657563SPrasad.Singamsetty@Sun.COM un->un_column[col].un_pwstart,
5667563SPrasad.Singamsetty@Sun.COM col);
5670Sstevel@tonic-gate cur->ci_flag = COL_READY;
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate } else {
5700Sstevel@tonic-gate colcnt++;
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate mutex_exit(&un->un_mx);
5750Sstevel@tonic-gate
5760Sstevel@tonic-gate /* This prevents new opens */
5770Sstevel@tonic-gate rw_enter(&md_unit_array_rw.lock, RW_WRITER);
5780Sstevel@tonic-gate (void) md_io_writerlock(ui);
5790Sstevel@tonic-gate un = (mr_unit_t *)md_unit_writerlock(ui);
5800Sstevel@tonic-gate while (ci_chain) {
5810Sstevel@tonic-gate cur = ci_chain;
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate /* take this element out of the chain */
5840Sstevel@tonic-gate ci_chain = cur->ci_next;
5850Sstevel@tonic-gate /* free this element */
5860Sstevel@tonic-gate sema_destroy(&cur->ci_buf.b_io);
5870Sstevel@tonic-gate sema_destroy(&cur->ci_buf.b_sem);
5880Sstevel@tonic-gate if (cur->ci_err)
5890Sstevel@tonic-gate raid_set_state(cur->ci_un, cur->ci_col,
5900Sstevel@tonic-gate RCS_INIT_ERRED, 0);
5910Sstevel@tonic-gate else
5920Sstevel@tonic-gate raid_set_state(cur->ci_un, cur->ci_col,
5930Sstevel@tonic-gate RCS_OKAY, 0);
5940Sstevel@tonic-gate kmem_free(cur, sizeof (raid_ci_t));
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate /* free the zeroed buffer */
5980Sstevel@tonic-gate kmem_free(zero_addr, dbtob(zerosize));
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate /* determine new unit state */
6010Sstevel@tonic-gate if (err == 0) {
6020Sstevel@tonic-gate if (state == RUS_INIT)
6030Sstevel@tonic-gate un->un_state = RUS_OKAY;
6040Sstevel@tonic-gate else {
6050Sstevel@tonic-gate un->c.un_total_blocks = un->un_grow_tb;
6067627SChris.Horne@Sun.COM md_nblocks_set(mnum, un->c.un_total_blocks);
6070Sstevel@tonic-gate un->un_grow_tb = 0;
6080Sstevel@tonic-gate if (raid_state_cnt(un, RCS_OKAY) ==
6090Sstevel@tonic-gate un->un_totalcolumncnt)
6100Sstevel@tonic-gate un->un_state = RUS_OKAY;
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate } else { /* error orcurred */
6130Sstevel@tonic-gate if (state & RUS_INIT)
6140Sstevel@tonic-gate un->un_state = RUS_DOI;
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate uniqtime32(&un->un_timestamp);
6170Sstevel@tonic-gate MD_STATUS(un) &= ~MD_UN_GROW_PENDING;
6180Sstevel@tonic-gate un->un_init_colcnt = 0;
6190Sstevel@tonic-gate un->un_init_iocnt = 0;
6200Sstevel@tonic-gate raid_commit(un, NULL);
6210Sstevel@tonic-gate md_unit_writerexit(ui);
6220Sstevel@tonic-gate (void) md_io_writerexit(ui);
6230Sstevel@tonic-gate rw_exit(&md_unit_array_rw.lock);
6240Sstevel@tonic-gate if (err) {
6250Sstevel@tonic-gate if (un->un_state & RUS_DOI) {
6260Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_INIT_FATAL,
6270Sstevel@tonic-gate SVM_TAG_METADEVICE, setno, MD_SID(un));
6280Sstevel@tonic-gate } else {
6290Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_INIT_FAILED,
6300Sstevel@tonic-gate SVM_TAG_METADEVICE, setno, MD_SID(un));
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate } else {
6330Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_INIT_SUCCESS,
6340Sstevel@tonic-gate SVM_TAG_METADEVICE, setno, MD_SID(un));
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate (void) raid_internal_close(mnum, OTYP_LYR, 0, 0);
6370Sstevel@tonic-gate /*
6380Sstevel@tonic-gate * Decrement the raid resync count for cpr
6390Sstevel@tonic-gate */
6400Sstevel@tonic-gate mutex_enter(&md_cpr_resync.md_resync_mutex);
6410Sstevel@tonic-gate md_cpr_resync.md_raid_resync--;
6420Sstevel@tonic-gate mutex_exit(&md_cpr_resync.md_resync_mutex);
6430Sstevel@tonic-gate thread_exit();
6440Sstevel@tonic-gate /*NOTREACHED*/
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate static int
raid_init_unit(minor_t mnum,md_error_t * ep)6480Sstevel@tonic-gate raid_init_unit(minor_t mnum, md_error_t *ep)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate mdi_unit_t *ui;
6510Sstevel@tonic-gate mr_unit_t *un;
6520Sstevel@tonic-gate int rval, i;
6530Sstevel@tonic-gate set_t setno = MD_MIN2SET(mnum);
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate ui = MDI_UNIT(mnum);
6560Sstevel@tonic-gate if (md_get_setstatus(setno) & MD_SET_STALE)
6570Sstevel@tonic-gate return (mdmddberror(ep, MDE_DB_STALE, mnum, setno));
6580Sstevel@tonic-gate
6590Sstevel@tonic-gate /* Don't start an init if the device is not available */
6600Sstevel@tonic-gate if ((ui == NULL) || (ui->ui_tstate & MD_DEV_ERRORED)) {
6610Sstevel@tonic-gate return (mdmderror(ep, MDE_RAID_OPEN_FAILURE, mnum));
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate
6640Sstevel@tonic-gate if (raid_internal_open(mnum, (FREAD | FWRITE),
6657563SPrasad.Singamsetty@Sun.COM OTYP_LYR, MD_OFLG_ISINIT)) {
6660Sstevel@tonic-gate rval = mdmderror(ep, MDE_RAID_OPEN_FAILURE, mnum);
6670Sstevel@tonic-gate goto out;
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate
6700Sstevel@tonic-gate un = md_unit_readerlock(ui);
6710Sstevel@tonic-gate un->un_percent_done = 0;
6720Sstevel@tonic-gate md_unit_readerexit(ui);
6730Sstevel@tonic-gate /* start resync_unit thread */
6740Sstevel@tonic-gate (void) thread_create(NULL, 0, raid_init_columns,
6750Sstevel@tonic-gate (void *)(uintptr_t)mnum, 0, &p0, TS_RUN, minclsyspri);
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate return (0);
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate out:
6800Sstevel@tonic-gate un = md_unit_writerlock(ui);
6810Sstevel@tonic-gate MD_STATUS(un) &= ~MD_UN_GROW_PENDING;
6820Sstevel@tonic-gate /* recover state */
6830Sstevel@tonic-gate for (i = 0; i < un->un_totalcolumncnt; i++)
6840Sstevel@tonic-gate if (COLUMN_STATE(un, i) == RCS_INIT)
6850Sstevel@tonic-gate raid_set_state(un, i, RCS_ERRED, 0);
6860Sstevel@tonic-gate if (un->un_state & RUS_INIT)
6870Sstevel@tonic-gate un->un_state = RUS_DOI;
6880Sstevel@tonic-gate raid_commit(un, NULL);
6890Sstevel@tonic-gate md_unit_writerexit(ui);
6900Sstevel@tonic-gate if (un->un_state & RUS_DOI) {
6910Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_INIT_FATAL,
6920Sstevel@tonic-gate SVM_TAG_METADEVICE, MD_UN2SET(un), MD_SID(un));
6930Sstevel@tonic-gate } else {
6940Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_INIT_FAILED,
6950Sstevel@tonic-gate SVM_TAG_METADEVICE, MD_UN2SET(un), MD_SID(un));
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate return (rval);
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate /*
7010Sstevel@tonic-gate * NAME: raid_regen
7020Sstevel@tonic-gate *
7030Sstevel@tonic-gate * DESCRIPTION: regenerate all the parity on the raid device. This
7040Sstevel@tonic-gate * routine starts a thread that will regenerate the
7050Sstevel@tonic-gate * parity on a raid device. If an I/O error occurs during
7060Sstevel@tonic-gate * this process the entire device is placed in error.
7070Sstevel@tonic-gate *
7080Sstevel@tonic-gate * PARAMETERS: md_set_params_t *msp - ioctl packet
7090Sstevel@tonic-gate */
7100Sstevel@tonic-gate static void
regen_unit(minor_t mnum)7110Sstevel@tonic-gate regen_unit(minor_t mnum)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate mdi_unit_t *ui = MDI_UNIT(mnum);
7140Sstevel@tonic-gate mr_unit_t *un = MD_UNIT(mnum);
7150Sstevel@tonic-gate buf_t buf, *bp;
7160Sstevel@tonic-gate caddr_t buffer;
7170Sstevel@tonic-gate int err = 0;
7180Sstevel@tonic-gate diskaddr_t total_segments;
7190Sstevel@tonic-gate diskaddr_t line;
7200Sstevel@tonic-gate size_t iosize;
7210Sstevel@tonic-gate
7220Sstevel@tonic-gate /*
7230Sstevel@tonic-gate * Increment raid resync count for cpr
7240Sstevel@tonic-gate */
7250Sstevel@tonic-gate mutex_enter(&md_cpr_resync.md_resync_mutex);
7260Sstevel@tonic-gate md_cpr_resync.md_raid_resync++;
7270Sstevel@tonic-gate mutex_exit(&md_cpr_resync.md_resync_mutex);
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate iosize = dbtob(un->un_segsize);
7300Sstevel@tonic-gate buffer = kmem_alloc(iosize, KM_SLEEP);
7310Sstevel@tonic-gate bp = &buf;
7320Sstevel@tonic-gate total_segments = un->un_segsincolumn;
7330Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_REGEN_START, SVM_TAG_METADEVICE,
7340Sstevel@tonic-gate MD_UN2SET(un), MD_SID(un));
7350Sstevel@tonic-gate un->un_percent_done = 0;
7360Sstevel@tonic-gate init_buf(bp, B_READ | B_BUSY, iosize);
7370Sstevel@tonic-gate
7380Sstevel@tonic-gate for (line = 0; line < total_segments; line++) {
7390Sstevel@tonic-gate bp->b_lblkno = line *
7407563SPrasad.Singamsetty@Sun.COM ((un->un_origcolumncnt - 1) * un->un_segsize);
7410Sstevel@tonic-gate bp->b_un.b_addr = buffer;
7420Sstevel@tonic-gate bp->b_bcount = iosize;
7430Sstevel@tonic-gate bp->b_iodone = NULL;
7440Sstevel@tonic-gate /*
7450Sstevel@tonic-gate * The following assignment is only correct because
7460Sstevel@tonic-gate * md_raid_strategy is fine when it's only a minor number
7470Sstevel@tonic-gate * and not a real dev_t. Yuck.
7480Sstevel@tonic-gate */
7490Sstevel@tonic-gate bp->b_edev = mnum;
7500Sstevel@tonic-gate md_raid_strategy(bp, MD_STR_NOTTOP, NULL);
7510Sstevel@tonic-gate if (biowait(bp)) {
7520Sstevel@tonic-gate err = 1;
7530Sstevel@tonic-gate break;
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate un->un_percent_done = (uint_t)((line * 1000) /
7567563SPrasad.Singamsetty@Sun.COM un->un_segsincolumn);
7570Sstevel@tonic-gate /* just to avoid rounding errors */
7580Sstevel@tonic-gate if (un->un_percent_done > 1000)
7590Sstevel@tonic-gate un->un_percent_done = 1000;
7600Sstevel@tonic-gate reset_buf(bp, B_READ | B_BUSY, iosize);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate destroy_buf(bp);
7630Sstevel@tonic-gate kmem_free(buffer, iosize);
7640Sstevel@tonic-gate
7650Sstevel@tonic-gate (void) md_io_writerlock(ui);
7660Sstevel@tonic-gate (void) raid_internal_close(mnum, OTYP_LYR, 0, 0);
7670Sstevel@tonic-gate (void) md_io_writerexit(ui);
7680Sstevel@tonic-gate un = md_unit_writerlock(ui);
7690Sstevel@tonic-gate if (!err &&
7707563SPrasad.Singamsetty@Sun.COM (raid_state_cnt(un, RCS_OKAY) == un->un_totalcolumncnt))
7710Sstevel@tonic-gate un->un_state = RUS_OKAY;
7720Sstevel@tonic-gate raid_commit(un, NULL);
7730Sstevel@tonic-gate md_unit_writerexit(ui);
7740Sstevel@tonic-gate if (err ||
7757563SPrasad.Singamsetty@Sun.COM raid_state_cnt(un, RCS_OKAY) != un->un_totalcolumncnt) {
7760Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_REGEN_FAILED,
7770Sstevel@tonic-gate SVM_TAG_METADEVICE, MD_UN2SET(un), MD_SID(un));
7780Sstevel@tonic-gate } else {
7790Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_REGEN_DONE, SVM_TAG_METADEVICE,
7800Sstevel@tonic-gate MD_UN2SET(un), MD_SID(un));
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate
7830Sstevel@tonic-gate /*
7840Sstevel@tonic-gate * Decrement the raid resync count for cpr
7850Sstevel@tonic-gate */
7860Sstevel@tonic-gate mutex_enter(&md_cpr_resync.md_resync_mutex);
7870Sstevel@tonic-gate md_cpr_resync.md_raid_resync--;
7880Sstevel@tonic-gate mutex_exit(&md_cpr_resync.md_resync_mutex);
7890Sstevel@tonic-gate thread_exit();
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate
7920Sstevel@tonic-gate static int
raid_regen_unit(minor_t mnum,md_error_t * ep)7930Sstevel@tonic-gate raid_regen_unit(minor_t mnum, md_error_t *ep)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate mdi_unit_t *ui;
7960Sstevel@tonic-gate mr_unit_t *un;
7970Sstevel@tonic-gate int i;
7980Sstevel@tonic-gate set_t setno = MD_MIN2SET(mnum);
7990Sstevel@tonic-gate
8000Sstevel@tonic-gate ui = MDI_UNIT(mnum);
8010Sstevel@tonic-gate un = (mr_unit_t *)MD_UNIT(mnum);
8020Sstevel@tonic-gate
8030Sstevel@tonic-gate if (md_get_setstatus(setno) & MD_SET_STALE)
8040Sstevel@tonic-gate return (mdmddberror(ep, MDE_DB_STALE, mnum, setno));
8050Sstevel@tonic-gate
8060Sstevel@tonic-gate /* Don't start a regen if the device is not available */
8070Sstevel@tonic-gate if ((ui == NULL) || (ui->ui_tstate & MD_DEV_ERRORED)) {
8080Sstevel@tonic-gate return (mdmderror(ep, MDE_RAID_OPEN_FAILURE, mnum));
8090Sstevel@tonic-gate }
8100Sstevel@tonic-gate
8110Sstevel@tonic-gate if (raid_internal_open(mnum, (FREAD | FWRITE), OTYP_LYR, 0)) {
8120Sstevel@tonic-gate (void) md_unit_writerlock(ui);
8130Sstevel@tonic-gate for (i = 0; i < un->un_totalcolumncnt; i++)
8140Sstevel@tonic-gate raid_set_state(un, i, RCS_ERRED, 0);
8150Sstevel@tonic-gate md_unit_writerexit(ui);
8160Sstevel@tonic-gate return (mdmderror(ep, MDE_RAID_OPEN_FAILURE, mnum));
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate /* start resync_unit thread */
8200Sstevel@tonic-gate (void) thread_create(NULL, 0, regen_unit,
8210Sstevel@tonic-gate (void *)(uintptr_t)mnum, 0, &p0, TS_RUN, minclsyspri);
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate return (0);
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate
8260Sstevel@tonic-gate static int
raid_regen(md_regen_param_t * mrp,IOLOCK * lock)8270Sstevel@tonic-gate raid_regen(md_regen_param_t *mrp, IOLOCK *lock)
8280Sstevel@tonic-gate {
8290Sstevel@tonic-gate minor_t mnum = mrp->mnum;
8300Sstevel@tonic-gate mr_unit_t *un;
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate mdclrerror(&mrp->mde);
8330Sstevel@tonic-gate
8340Sstevel@tonic-gate un = md_unit_readerlock(MDI_UNIT(mnum));
8350Sstevel@tonic-gate
8360Sstevel@tonic-gate if (MD_STATUS(un) & MD_UN_GROW_PENDING) {
8370Sstevel@tonic-gate md_unit_readerexit(MDI_UNIT(mnum));
8380Sstevel@tonic-gate return (mdmderror(&mrp->mde, MDE_IN_USE, mnum));
8390Sstevel@tonic-gate }
8400Sstevel@tonic-gate
8410Sstevel@tonic-gate if ((MD_STATUS(un) & MD_UN_RESYNC_ACTIVE) ||
8420Sstevel@tonic-gate (raid_state_cnt(un, RCS_RESYNC))) {
8430Sstevel@tonic-gate md_unit_readerexit(MDI_UNIT(mnum));
8440Sstevel@tonic-gate return (mdmderror(&mrp->mde, MDE_RESYNC_ACTIVE, mnum));
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate
8470Sstevel@tonic-gate if ((raid_state_cnt(un, RCS_INIT) != 0) || (un->un_state & RUS_INIT)) {
8480Sstevel@tonic-gate md_unit_readerexit(MDI_UNIT(mnum));
8490Sstevel@tonic-gate return (mdmderror(&mrp->mde, MDE_IN_USE, mnum));
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate
8520Sstevel@tonic-gate if ((raid_state_cnt(un, RCS_OKAY) != un->un_totalcolumncnt) ||
8530Sstevel@tonic-gate (! (un->un_state & RUS_OKAY))) {
8540Sstevel@tonic-gate md_unit_readerexit(MDI_UNIT(mnum));
8550Sstevel@tonic-gate return (mdmderror(&mrp->mde, MDE_RAID_NOT_OKAY, mnum));
8560Sstevel@tonic-gate }
8570Sstevel@tonic-gate
8580Sstevel@tonic-gate md_unit_readerexit(MDI_UNIT(mnum));
8590Sstevel@tonic-gate
8600Sstevel@tonic-gate /* get locks and recheck to be sure something did not change */
8610Sstevel@tonic-gate if ((un = raid_getun(mnum, &mrp->mde, WRITERS, lock)) == NULL)
8620Sstevel@tonic-gate return (0);
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate if ((raid_state_cnt(un, RCS_OKAY) != un->un_totalcolumncnt) ||
8650Sstevel@tonic-gate (! (un->un_state & RUS_OKAY))) {
8660Sstevel@tonic-gate return (mdmderror(&mrp->mde, MDE_RAID_NOT_OKAY, mnum));
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate raid_set_state(un, 0, RCS_REGEN, 0);
8700Sstevel@tonic-gate raid_commit(un, NULL);
8710Sstevel@tonic-gate md_ioctl_droplocks(lock);
8720Sstevel@tonic-gate return (raid_regen_unit(mnum, &mrp->mde));
8730Sstevel@tonic-gate }
8740Sstevel@tonic-gate
8750Sstevel@tonic-gate /*
8760Sstevel@tonic-gate * NAME: raid_set
8770Sstevel@tonic-gate * DESCRIPTION: used to create a RAID metadevice
8780Sstevel@tonic-gate * PARAMETERS: md_set_params_t *d - pointer to set data structure
8790Sstevel@tonic-gate * int mode - must be FWRITE
8800Sstevel@tonic-gate *
8810Sstevel@tonic-gate * LOCKS: none
8820Sstevel@tonic-gate *
8830Sstevel@tonic-gate */
8840Sstevel@tonic-gate static int
raid_set(void * d,int mode)8850Sstevel@tonic-gate raid_set(void *d, int mode)
8860Sstevel@tonic-gate {
8870Sstevel@tonic-gate minor_t mnum;
8880Sstevel@tonic-gate mr_unit_t *un;
8890Sstevel@tonic-gate mddb_recid_t mr_recid;
8900Sstevel@tonic-gate mddb_recid_t *recids;
8910Sstevel@tonic-gate mddb_type_t typ1;
8920Sstevel@tonic-gate int err;
8930Sstevel@tonic-gate set_t setno;
8940Sstevel@tonic-gate int num_recs;
8950Sstevel@tonic-gate int rid;
8960Sstevel@tonic-gate int col;
8970Sstevel@tonic-gate md_set_params_t *msp = d;
8980Sstevel@tonic-gate
8990Sstevel@tonic-gate
9000Sstevel@tonic-gate mnum = msp->mnum;
9010Sstevel@tonic-gate setno = MD_MIN2SET(mnum);
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate mdclrerror(&msp->mde);
9040Sstevel@tonic-gate
9050Sstevel@tonic-gate if (raid_getun(mnum, &msp->mde, NO_OLD, NULL) == NULL)
9060Sstevel@tonic-gate return (0);
9070Sstevel@tonic-gate
9080Sstevel@tonic-gate typ1 = (mddb_type_t)md_getshared_key(setno,
9090Sstevel@tonic-gate raid_md_ops.md_driver.md_drivername);
9100Sstevel@tonic-gate
9110Sstevel@tonic-gate /* create the db record for this mdstruct */
9120Sstevel@tonic-gate
9130Sstevel@tonic-gate if (msp->options & MD_CRO_64BIT) {
9140Sstevel@tonic-gate #if defined(_ILP32)
9150Sstevel@tonic-gate return (mdmderror(&msp->mde, MDE_UNIT_TOO_LARGE, mnum));
9160Sstevel@tonic-gate #else
9170Sstevel@tonic-gate mr_recid = mddb_createrec(msp->size, typ1, 0,
9187563SPrasad.Singamsetty@Sun.COM MD_CRO_64BIT | MD_CRO_RAID | MD_CRO_FN, setno);
9190Sstevel@tonic-gate #endif
9200Sstevel@tonic-gate } else {
9210Sstevel@tonic-gate mr_recid = mddb_createrec(msp->size, typ1, 0,
9227563SPrasad.Singamsetty@Sun.COM MD_CRO_32BIT | MD_CRO_RAID | MD_CRO_FN, setno);
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate
9250Sstevel@tonic-gate if (mr_recid < 0)
9260Sstevel@tonic-gate return (mddbstatus2error(&msp->mde,
9277563SPrasad.Singamsetty@Sun.COM (int)mr_recid, mnum, setno));
9280Sstevel@tonic-gate
9290Sstevel@tonic-gate /* get the address of the mdstruct */
9300Sstevel@tonic-gate un = (mr_unit_t *)mddb_getrecaddr(mr_recid);
9310Sstevel@tonic-gate /*
9320Sstevel@tonic-gate * It is okay that we muck with the mdstruct here,
9330Sstevel@tonic-gate * since no one else will know about the mdstruct
9340Sstevel@tonic-gate * until we commit it. If we crash, the record will
9350Sstevel@tonic-gate * be automatically purged, since we haven't
9360Sstevel@tonic-gate * committed it yet.
9370Sstevel@tonic-gate */
9380Sstevel@tonic-gate
9390Sstevel@tonic-gate /* copy in the user's mdstruct */
9400Sstevel@tonic-gate if (err = ddi_copyin((caddr_t)(uintptr_t)msp->mdp, un,
9410Sstevel@tonic-gate msp->size, mode)) {
9420Sstevel@tonic-gate mddb_deleterec_wrapper(mr_recid);
9430Sstevel@tonic-gate return (EFAULT);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate /* All 64 bit metadevices only support EFI labels. */
9460Sstevel@tonic-gate if (msp->options & MD_CRO_64BIT) {
9470Sstevel@tonic-gate un->c.un_flag |= MD_EFILABEL;
9480Sstevel@tonic-gate }
9490Sstevel@tonic-gate
9500Sstevel@tonic-gate /*
9510Sstevel@tonic-gate * allocate the real recids array. since we may have to commit
9520Sstevel@tonic-gate * underlying metadevice records, we need an array of size:
9530Sstevel@tonic-gate * total number of components in raid + 3 (1 for the raid itself,
9540Sstevel@tonic-gate * one for the hotspare, one for the end marker).
9550Sstevel@tonic-gate */
9560Sstevel@tonic-gate num_recs = un->un_totalcolumncnt + 3;
9570Sstevel@tonic-gate rid = 0;
9580Sstevel@tonic-gate recids = kmem_alloc(num_recs * sizeof (mddb_recid_t), KM_SLEEP);
9590Sstevel@tonic-gate recids[rid++] = mr_recid;
9600Sstevel@tonic-gate
9610Sstevel@tonic-gate MD_SID(un) = mnum;
9620Sstevel@tonic-gate MD_RECID(un) = recids[0];
9630Sstevel@tonic-gate MD_CAPAB(un) = MD_CAN_PARENT | MD_CAN_SP;
9640Sstevel@tonic-gate MD_PARENT(un) = MD_NO_PARENT;
9650Sstevel@tonic-gate un->un_resync_copysize = 0;
9661623Stw21770 un->c.un_revision |= MD_FN_META_DEV;
9670Sstevel@tonic-gate
9680Sstevel@tonic-gate if (UNIT_STATE(un) == RUS_INIT)
9690Sstevel@tonic-gate MD_STATUS(un) |= MD_UN_GROW_PENDING;
9700Sstevel@tonic-gate
9710Sstevel@tonic-gate if ((UNIT_STATE(un) != RUS_INIT) && raid_check_pw(un)) {
9720Sstevel@tonic-gate mddb_deleterec_wrapper(mr_recid);
9730Sstevel@tonic-gate err = mderror(&msp->mde, MDE_RAID_INVALID);
9740Sstevel@tonic-gate goto out;
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate
9770Sstevel@tonic-gate if (err = raid_build_incore(un, 0)) {
9780Sstevel@tonic-gate if (un->mr_ic) {
9790Sstevel@tonic-gate kmem_free(un->un_column_ic, sizeof (mr_column_ic_t) *
9807563SPrasad.Singamsetty@Sun.COM un->un_totalcolumncnt);
9810Sstevel@tonic-gate kmem_free(un->mr_ic, sizeof (*un->mr_ic));
9820Sstevel@tonic-gate }
9837627SChris.Horne@Sun.COM
9847627SChris.Horne@Sun.COM md_nblocks_set(mnum, -1ULL);
9850Sstevel@tonic-gate MD_UNIT(mnum) = NULL;
9867627SChris.Horne@Sun.COM
9870Sstevel@tonic-gate mddb_deleterec_wrapper(mr_recid);
9880Sstevel@tonic-gate goto out;
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate
9911623Stw21770 /*
9921623Stw21770 * Update unit availability
9931623Stw21770 */
9941623Stw21770 md_set[setno].s_un_avail--;
9951623Stw21770
9960Sstevel@tonic-gate recids[rid] = 0;
9970Sstevel@tonic-gate if (un->un_hsp_id != -1) {
9980Sstevel@tonic-gate /* increment the reference count of the hot spare pool */
9990Sstevel@tonic-gate err = md_hot_spare_ifc(HSP_INCREF, un->un_hsp_id, 0, 0,
10000Sstevel@tonic-gate &recids[rid], NULL, NULL, NULL);
10010Sstevel@tonic-gate if (err) {
10027627SChris.Horne@Sun.COM md_nblocks_set(mnum, -1ULL);
10030Sstevel@tonic-gate MD_UNIT(mnum) = NULL;
10047627SChris.Horne@Sun.COM
10050Sstevel@tonic-gate mddb_deleterec_wrapper(mr_recid);
10060Sstevel@tonic-gate goto out;
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate rid++;
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate
10110Sstevel@tonic-gate /*
10120Sstevel@tonic-gate * set the parent on any metadevice components.
10130Sstevel@tonic-gate * NOTE: currently soft partitions are the only metadevices
10140Sstevel@tonic-gate * which can appear within a RAID metadevice.
10150Sstevel@tonic-gate */
10160Sstevel@tonic-gate for (col = 0; col < un->un_totalcolumncnt; col++) {
10170Sstevel@tonic-gate mr_column_t *mr_col = &un->un_column[col];
10180Sstevel@tonic-gate md_unit_t *comp_un;
10190Sstevel@tonic-gate
10200Sstevel@tonic-gate if (md_getmajor(mr_col->un_dev) == md_major) {
10210Sstevel@tonic-gate comp_un = MD_UNIT(md_getminor(mr_col->un_dev));
10220Sstevel@tonic-gate recids[rid++] = MD_RECID(comp_un);
10230Sstevel@tonic-gate md_set_parent(mr_col->un_dev, MD_SID(un));
10240Sstevel@tonic-gate }
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate /* set the end marker */
10280Sstevel@tonic-gate recids[rid] = 0;
10290Sstevel@tonic-gate
10300Sstevel@tonic-gate mddb_commitrecs_wrapper(recids);
10310Sstevel@tonic-gate md_create_unit_incore(mnum, &raid_md_ops, 1);
10320Sstevel@tonic-gate
10330Sstevel@tonic-gate SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_CREATE, SVM_TAG_METADEVICE, setno,
10340Sstevel@tonic-gate MD_SID(un));
10350Sstevel@tonic-gate
10360Sstevel@tonic-gate out:
10370Sstevel@tonic-gate kmem_free(recids, (num_recs * sizeof (mddb_recid_t)));
10380Sstevel@tonic-gate if (err)
10390Sstevel@tonic-gate return (err);
10400Sstevel@tonic-gate
10410Sstevel@tonic-gate /* only attempt to init a device that is in the init state */
10420Sstevel@tonic-gate if (UNIT_STATE(un) != RUS_INIT)
10430Sstevel@tonic-gate return (0);
10440Sstevel@tonic-gate
10450Sstevel@tonic-gate return (raid_init_unit(mnum, &msp->mde));
10460Sstevel@tonic-gate }
10470Sstevel@tonic-gate
10480Sstevel@tonic-gate /*
10490Sstevel@tonic-gate * NAME: raid_get
10500Sstevel@tonic-gate * DESCRIPTION: used to get the unit structure of a RAID metadevice
10510Sstevel@tonic-gate * PARAMETERS: md_i_get_t *migp - pointer to get data structure
10520Sstevel@tonic-gate * int mode - must be FREAD
10530Sstevel@tonic-gate * IOLOCK *lock - pointer to IOCTL lock
10540Sstevel@tonic-gate *
10550Sstevel@tonic-gate * LOCKS: obtains unit reader lock via IOLOCK
10560Sstevel@tonic-gate *
10570Sstevel@tonic-gate */
10580Sstevel@tonic-gate static int
raid_get(void * migp,int mode,IOLOCK * lock)10590Sstevel@tonic-gate raid_get(
10600Sstevel@tonic-gate void *migp,
10610Sstevel@tonic-gate int mode,
10620Sstevel@tonic-gate IOLOCK *lock
10630Sstevel@tonic-gate )
10640Sstevel@tonic-gate {
10650Sstevel@tonic-gate minor_t mnum;
10660Sstevel@tonic-gate mr_unit_t *un;
10670Sstevel@tonic-gate md_i_get_t *migph = migp;
10680Sstevel@tonic-gate
10690Sstevel@tonic-gate
10700Sstevel@tonic-gate mnum = migph->id;
10710Sstevel@tonic-gate
10720Sstevel@tonic-gate mdclrerror(&migph->mde);
10730Sstevel@tonic-gate
10740Sstevel@tonic-gate if ((un = raid_getun(mnum, &migph->mde,
10757563SPrasad.Singamsetty@Sun.COM RD_LOCK, lock)) == NULL)
10760Sstevel@tonic-gate return (0);
10770Sstevel@tonic-gate
10780Sstevel@tonic-gate if (migph->size == 0) {
10790Sstevel@tonic-gate migph->size = un->c.un_size;
10800Sstevel@tonic-gate return (0);
10810Sstevel@tonic-gate }
10820Sstevel@tonic-gate
10830Sstevel@tonic-gate if (migph->size < un->c.un_size) {
10840Sstevel@tonic-gate return (EFAULT);
10850Sstevel@tonic-gate }
10860Sstevel@tonic-gate if (ddi_copyout(un, (void *)(uintptr_t)migph->mdp,
10870Sstevel@tonic-gate un->c.un_size, mode))
10880Sstevel@tonic-gate return (EFAULT);
10890Sstevel@tonic-gate
10900Sstevel@tonic-gate return (0);
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate
10930Sstevel@tonic-gate
10940Sstevel@tonic-gate /*
10950Sstevel@tonic-gate * NAME: raid_replace
10960Sstevel@tonic-gate * DESCRIPTION: used to replace a component of a RAID metadevice
10970Sstevel@tonic-gate * PARAMETERS: replace_params_t *mrp - pointer to replace data structure
10980Sstevel@tonic-gate * IOLOCK *lock - pointer to IOCTL lock
10990Sstevel@tonic-gate *
11000Sstevel@tonic-gate * LOCKS: obtains unit writer lock via IOLOCK (through raid_getun),
11010Sstevel@tonic-gate * obtains and releases md_unit_array_rw write lock
11020Sstevel@tonic-gate *
11030Sstevel@tonic-gate */
11040Sstevel@tonic-gate static int
raid_replace(replace_params_t * mrp,IOLOCK * lock)11050Sstevel@tonic-gate raid_replace(
11060Sstevel@tonic-gate replace_params_t *mrp,
11070Sstevel@tonic-gate IOLOCK *lock
11080Sstevel@tonic-gate )
11090Sstevel@tonic-gate {
11100Sstevel@tonic-gate minor_t mnum = mrp->mnum;
11110Sstevel@tonic-gate md_dev64_t odev = mrp->old_dev;
11120Sstevel@tonic-gate md_error_t *ep = &mrp->mde;
11130Sstevel@tonic-gate mr_unit_t *un;
11140Sstevel@tonic-gate rcs_state_t state;
11150Sstevel@tonic-gate int ix, col = -1;
11160Sstevel@tonic-gate int force = 0;
11170Sstevel@tonic-gate int err = 0;
11180Sstevel@tonic-gate replace_cmd_t cmd;
11190Sstevel@tonic-gate set_t setno;
11200Sstevel@tonic-gate side_t side;
11210Sstevel@tonic-gate mdkey_t devkey;
11220Sstevel@tonic-gate int nkeys;
11230Sstevel@tonic-gate mddb_recid_t extra_recids[3] = { 0, 0, 0 };
11240Sstevel@tonic-gate int extra_rids = 0;
11250Sstevel@tonic-gate md_error_t mde = mdnullerror;
11260Sstevel@tonic-gate sv_dev_t sv = {MD_SET_BAD, MD_SIDEWILD, MD_KEYWILD};
11270Sstevel@tonic-gate
11280Sstevel@tonic-gate mdclrerror(ep);
11290Sstevel@tonic-gate setno = MD_MIN2SET(mnum);
11300Sstevel@tonic-gate side = mddb_getsidenum(setno);
11310Sstevel@tonic-gate
11320Sstevel@tonic-gate un = md_unit_readerlock(MDI_UNIT(mnum));
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate if ((MD_STATUS(un) & MD_UN_RESYNC_ACTIVE) ||
11350Sstevel@tonic-gate (raid_state_cnt(un, RCS_RESYNC) != 0)) {
11360Sstevel@tonic-gate md_unit_readerexit(MDI_UNIT(mnum));
11370Sstevel@tonic-gate return (mdmderror(ep, MDE_RESYNC_ACTIVE, mnum));
11380Sstevel@tonic-gate }
11390Sstevel@tonic-gate
11400Sstevel@tonic-gate if (un->un_state & RUS_DOI) {
11410Sstevel@tonic-gate md_unit_readerexit(MDI_UNIT(mnum));
11420Sstevel@tonic-gate return (mdmderror(ep, MDE_RAID_DOI, mnum));
11430Sstevel@tonic-gate }
11440Sstevel@tonic-gate
11450Sstevel@tonic-gate if ((raid_state_cnt(un, RCS_INIT) != 0) || (un->un_state & RUS_INIT) ||
11460Sstevel@tonic-gate (MD_STATUS(un) & MD_UN_GROW_PENDING)) {
11470Sstevel@tonic-gate md_unit_readerexit(MDI_UNIT(mnum));
11480Sstevel@tonic-gate return (mdmderror(ep, MDE_IN_USE, mnum));
11490Sstevel@tonic-gate }
11500Sstevel@tonic-gate
11510Sstevel@tonic-gate md_unit_readerexit(MDI_UNIT(mnum));
11520Sstevel@tonic-gate
11530Sstevel@tonic-gate /* get locks and recheck to be sure something did not change */
11540Sstevel@tonic-gate if ((un = raid_getun(mnum, ep, WRITERS, lock)) == NULL)
11550Sstevel@tonic-gate return (0);
11560Sstevel@tonic-gate
11570Sstevel@tonic-gate if (md_getkeyfromdev(setno, side, odev, &devkey, &nkeys) != 0) {
11580Sstevel@tonic-gate return (mddeverror(ep, MDE_NAME_SPACE, odev));
11590Sstevel@tonic-gate }
11600Sstevel@tonic-gate
11610Sstevel@tonic-gate for (ix = 0; ix < un->un_totalcolumncnt; ix++) {
11620Sstevel@tonic-gate md_dev64_t tmpdevt = un->un_column[ix].un_orig_dev;
11630Sstevel@tonic-gate /*
11640Sstevel@tonic-gate * Try to resolve devt again if NODEV64
11650Sstevel@tonic-gate */
11660Sstevel@tonic-gate if (tmpdevt == NODEV64) {
11670Sstevel@tonic-gate tmpdevt = md_resolve_bydevid(mnum, tmpdevt,
11687563SPrasad.Singamsetty@Sun.COM un->un_column[ix].un_orig_key);
11690Sstevel@tonic-gate un->un_column[ix].un_orig_dev = tmpdevt;
11700Sstevel@tonic-gate }
11710Sstevel@tonic-gate
11720Sstevel@tonic-gate if (un->un_column[ix].un_orig_dev == odev) {
11730Sstevel@tonic-gate col = ix;
11740Sstevel@tonic-gate break;
11750Sstevel@tonic-gate } else {
11760Sstevel@tonic-gate if (un->un_column[ix].un_orig_dev == NODEV64) {
11770Sstevel@tonic-gate /*
11780Sstevel@tonic-gate * Now we use the keys to match.
11790Sstevel@tonic-gate * If no key found, continue.
11800Sstevel@tonic-gate */
11810Sstevel@tonic-gate if (nkeys == 0) {
11820Sstevel@tonic-gate continue;
11830Sstevel@tonic-gate }
11840Sstevel@tonic-gate if (un->un_column[ix].un_orig_key == devkey) {
11850Sstevel@tonic-gate if (nkeys > 1)
11860Sstevel@tonic-gate return (mddeverror(ep,
11870Sstevel@tonic-gate MDE_MULTNM, odev));
11880Sstevel@tonic-gate col = ix;
11890Sstevel@tonic-gate break;
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate }
11920Sstevel@tonic-gate }
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate
11950Sstevel@tonic-gate if (col == -1)
11960Sstevel@tonic-gate return (mdcomperror(ep, MDE_CANT_FIND_COMP,
11970Sstevel@tonic-gate mnum, odev));
11980Sstevel@tonic-gate
11990Sstevel@tonic-gate if ((MD_STATUS(un) & MD_UN_RESYNC_ACTIVE) ||
12000Sstevel@tonic-gate (raid_state_cnt(un, RCS_RESYNC) != 0))
12010Sstevel@tonic-gate return (mdmderror(ep, MDE_RESYNC_ACTIVE, mnum));
12020Sstevel@tonic-gate
12030Sstevel@tonic-gate if (un->un_state & RUS_DOI)
12040Sstevel@tonic-gate return (mdcomperror(ep, MDE_REPL_INVAL_STATE, mnum,
12057563SPrasad.Singamsetty@Sun.COM un->un_column[col].un_dev));
12060Sstevel@tonic-gate
12070Sstevel@tonic-gate if ((raid_state_cnt(un, RCS_INIT) != 0) || (un->un_state & RUS_INIT) ||
12080Sstevel@tonic-gate (MD_STATUS(un) & MD_UN_GROW_PENDING))
12090Sstevel@tonic-gate return (mdmderror(ep, MDE_IN_USE, mnum));
12100Sstevel@tonic-gate
12110Sstevel@tonic-gate if ((mrp->cmd == FORCE_ENABLE_COMP) || (mrp->cmd == FORCE_REPLACE_COMP))
12120Sstevel@tonic-gate force = 1;
12130Sstevel@tonic-gate if ((mrp->cmd == FORCE_ENABLE_COMP) || (mrp->cmd == ENABLE_COMP))
12140Sstevel@tonic-gate cmd = ENABLE_COMP;
12150Sstevel@tonic-gate if ((mrp->cmd == FORCE_REPLACE_COMP) || (mrp->cmd == REPLACE_COMP))
12160Sstevel@tonic-gate cmd = REPLACE_COMP;
12170Sstevel@tonic-gate
12180Sstevel@tonic-gate if (un->un_state == RUS_LAST_ERRED) {
12190Sstevel@tonic-gate /* Must use -f force flag for unit in LAST_ERRED state */
12200Sstevel@tonic-gate if (!force)
12217563SPrasad.Singamsetty@Sun.COM return (mdmderror(ep, MDE_RAID_NEED_FORCE, mnum));
12220Sstevel@tonic-gate
12230Sstevel@tonic-gate /* Must use -f force flag on ERRED column first */
12240Sstevel@tonic-gate if (un->un_column[col].un_devstate != RCS_ERRED) {
12250Sstevel@tonic-gate for (ix = 0; ix < un->un_totalcolumncnt; ix++) {
12260Sstevel@tonic-gate if (un->un_column[ix].un_devstate & RCS_ERRED)
12270Sstevel@tonic-gate return (mdcomperror(ep,
12287563SPrasad.Singamsetty@Sun.COM MDE_RAID_COMP_ERRED, mnum,
12297563SPrasad.Singamsetty@Sun.COM un->un_column[ix].un_dev));
12300Sstevel@tonic-gate }
12310Sstevel@tonic-gate }
12320Sstevel@tonic-gate
12330Sstevel@tonic-gate /* must use -f force flag on LAST_ERRED columns next */
12340Sstevel@tonic-gate if ((un->un_column[col].un_devstate != RCS_LAST_ERRED) &&
12350Sstevel@tonic-gate (un->un_column[col].un_devstate != RCS_ERRED))
12360Sstevel@tonic-gate return (mdcomperror(ep, MDE_RAID_COMP_ERRED,
12377563SPrasad.Singamsetty@Sun.COM mnum, un->un_column[col].un_dev));
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate
12400Sstevel@tonic-gate if (un->un_state == RUS_ERRED) {
12410Sstevel@tonic-gate if (! (un->un_column[col].un_devstate &
12420Sstevel@tonic-gate (RCS_ERRED | RCS_INIT_ERRED)))
12430Sstevel@tonic-gate return (mdcomperror(ep, MDE_RAID_COMP_ERRED,
12440Sstevel@tonic-gate mnum, un->un_column[ix].un_dev));
12450Sstevel@tonic-gate }
12460Sstevel@tonic-gate
12470Sstevel@tonic-gate ASSERT(!(un->un_column[col].un_devflags & MD_RAID_ALT_ISOPEN));
12480Sstevel@tonic-gate ASSERT(!(un->un_column[col].un_devflags & MD_RAID_WRITE_ALT));
12490Sstevel@tonic-gate
12500Sstevel@tonic-gate state = un->un_column[col].un_devstate;
12510Sstevel@tonic-gate if (state & RCS_INIT_ERRED) {
12520Sstevel@tonic-gate MD_STATUS(un) |= MD_UN_GROW_PENDING;
12530Sstevel@tonic-gate un->un_percent_done = 0;
12540Sstevel@tonic-gate raid_set_state(un, col, RCS_INIT, 0);
12550Sstevel@tonic-gate } else if (((mrp->options & MDIOCTL_NO_RESYNC_RAID) == 0) &&
12560Sstevel@tonic-gate resync_request(mnum, col, 0, ep))
12570Sstevel@tonic-gate return (mdmderror(ep, MDE_RESYNC_ACTIVE, mnum));
12580Sstevel@tonic-gate
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate if (cmd == REPLACE_COMP) {
12610Sstevel@tonic-gate md_dev64_t tmpdev = mrp->new_dev;
12620Sstevel@tonic-gate
12630Sstevel@tonic-gate /*
12640Sstevel@tonic-gate * open the device by device id
12650Sstevel@tonic-gate */
12660Sstevel@tonic-gate tmpdev = md_resolve_bydevid(mnum, tmpdev, mrp->new_key);
12670Sstevel@tonic-gate if (md_layered_open(mnum, &tmpdev, MD_OFLG_NULL)) {
12680Sstevel@tonic-gate return (mdcomperror(ep, MDE_COMP_OPEN_ERR, mnum,
12690Sstevel@tonic-gate tmpdev));
12700Sstevel@tonic-gate }
12710Sstevel@tonic-gate
12720Sstevel@tonic-gate /*
12730Sstevel@tonic-gate * If it's a metadevice, make sure it gets reparented
12740Sstevel@tonic-gate */
12750Sstevel@tonic-gate if (md_getmajor(tmpdev) == md_major) {
12760Sstevel@tonic-gate minor_t new_mnum = md_getminor(tmpdev);
12770Sstevel@tonic-gate md_unit_t *new_un = MD_UNIT(new_mnum);
12780Sstevel@tonic-gate
12790Sstevel@tonic-gate md_set_parent(tmpdev, MD_SID(un));
12800Sstevel@tonic-gate extra_recids[extra_rids++] = MD_RECID(new_un);
12810Sstevel@tonic-gate }
12820Sstevel@tonic-gate
12830Sstevel@tonic-gate mrp->new_dev = tmpdev;
12840Sstevel@tonic-gate un->un_column[col].un_orig_dev = tmpdev;
12850Sstevel@tonic-gate un->un_column[col].un_orig_key = mrp->new_key;
12860Sstevel@tonic-gate un->un_column[col].un_orig_pwstart = mrp->start_blk;
12870Sstevel@tonic-gate un->un_column[col].un_orig_devstart =
12880Sstevel@tonic-gate mrp->start_blk + un->un_pwsize;
12890Sstevel@tonic-gate
12900Sstevel@tonic-gate /*
12910Sstevel@tonic-gate * If the old device was a metadevice, make sure to
12920Sstevel@tonic-gate * reset its parent.
12930Sstevel@tonic-gate */
12940Sstevel@tonic-gate if (md_getmajor(odev) == md_major) {
12950Sstevel@tonic-gate minor_t old_mnum = md_getminor(odev);
12960Sstevel@tonic-gate md_unit_t *old_un = MD_UNIT(old_mnum);
12970Sstevel@tonic-gate
12980Sstevel@tonic-gate md_reset_parent(odev);
12990Sstevel@tonic-gate extra_recids[extra_rids++] =
13000Sstevel@tonic-gate MD_RECID(old_un);
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate
13030Sstevel@tonic-gate if (HOTSPARED(un, col)) {
13040Sstevel@tonic-gate md_layered_close(mrp->new_dev, MD_OFLG_NULL);
13050Sstevel@tonic-gate un->un_column[col].un_alt_dev = mrp->new_dev;
13060Sstevel@tonic-gate un->un_column[col].un_alt_pwstart = mrp->start_blk;
13070Sstevel@tonic-gate un->un_column[col].un_alt_devstart =
13080Sstevel@tonic-gate mrp->start_blk + un->un_pwsize;
13090Sstevel@tonic-gate un->un_column[col].un_devflags |= MD_RAID_COPY_RESYNC;
13100Sstevel@tonic-gate } else {
13110Sstevel@tonic-gate /*
13120Sstevel@tonic-gate * not hot spared. Close the old device and
13130Sstevel@tonic-gate * move the new device in.
13140Sstevel@tonic-gate */
13150Sstevel@tonic-gate if (un->un_column[col].un_devflags & MD_RAID_DEV_ISOPEN)
13160Sstevel@tonic-gate md_layered_close(odev, MD_OFLG_NULL);
13170Sstevel@tonic-gate un->un_column[col].un_devflags |= MD_RAID_DEV_ISOPEN;
13180Sstevel@tonic-gate un->un_column[col].un_dev = mrp->new_dev;
13190Sstevel@tonic-gate un->un_column[col].un_pwstart = mrp->start_blk;
13200Sstevel@tonic-gate un->un_column[col].un_devstart =
13210Sstevel@tonic-gate mrp->start_blk + un->un_pwsize;
13220Sstevel@tonic-gate if ((mrp->options & MDIOCTL_NO_RESYNC_RAID) == 0) {
13230Sstevel@tonic-gate un->un_column[col].un_devflags |=
13240Sstevel@tonic-gate MD_RAID_REGEN_RESYNC;
13250Sstevel@tonic-gate }
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate /*
13280Sstevel@tonic-gate * If the old device is not a metadevice then
13290Sstevel@tonic-gate * save off the set number and key so that it
13300Sstevel@tonic-gate * can be removed from the namespace later.
13310Sstevel@tonic-gate */
13320Sstevel@tonic-gate if (md_getmajor(odev) != md_major) {
13330Sstevel@tonic-gate sv.setno = setno;
13340Sstevel@tonic-gate sv.key = devkey;
13350Sstevel@tonic-gate }
13360Sstevel@tonic-gate }
13370Sstevel@tonic-gate
13380Sstevel@tonic-gate if (cmd == ENABLE_COMP) {
13390Sstevel@tonic-gate md_dev64_t tmpdev = un->un_column[col].un_orig_dev;
13400Sstevel@tonic-gate mdkey_t raidkey = un->un_column[col].un_orig_key;
13410Sstevel@tonic-gate
13420Sstevel@tonic-gate /*
13430Sstevel@tonic-gate * We trust the dev_t because we cannot determine the
13440Sstevel@tonic-gate * dev_t from the device id since a new disk is in the
13450Sstevel@tonic-gate * same location. Since this is a call from metareplace -e dx
13460Sstevel@tonic-gate * AND it is SCSI a new dev_t is not generated. So the
13470Sstevel@tonic-gate * dev_t from the mddb is used. Before enabling the device
13480Sstevel@tonic-gate * we check to make sure that multiple entries for the same
13490Sstevel@tonic-gate * device does not exist in the namespace. If they do we
13500Sstevel@tonic-gate * fail the ioctl.
13510Sstevel@tonic-gate * One of the many ways multiple entries in the name space
13520Sstevel@tonic-gate * can occur is if one removed the failed component in a
13530Sstevel@tonic-gate * RAID metadevice and put another disk that was part of
13540Sstevel@tonic-gate * another metadevice. After reboot metadevadm would correctly
13550Sstevel@tonic-gate * update the device name for the metadevice whose component
13560Sstevel@tonic-gate * has moved. However now in the metadb there are two entries
13570Sstevel@tonic-gate * for the same name (ctds) that belong to different
13580Sstevel@tonic-gate * metadevices. One is valid, the other is a ghost or "last
13590Sstevel@tonic-gate * know as" ctds.
13600Sstevel@tonic-gate */
13610Sstevel@tonic-gate tmpdev = md_resolve_bydevid(mnum, tmpdev, raidkey);
13620Sstevel@tonic-gate if (tmpdev == NODEV64)
13630Sstevel@tonic-gate tmpdev = md_getdevnum(setno, side, raidkey,
13640Sstevel@tonic-gate MD_TRUST_DEVT);
13650Sstevel@tonic-gate /*
13660Sstevel@tonic-gate * check for multiple entries in namespace for the
13670Sstevel@tonic-gate * same dev
13680Sstevel@tonic-gate */
13690Sstevel@tonic-gate
13700Sstevel@tonic-gate if (md_getkeyfromdev(setno, side, tmpdev, &devkey,
13710Sstevel@tonic-gate &nkeys) != 0)
13720Sstevel@tonic-gate return (mddeverror(ep, MDE_NAME_SPACE, tmpdev));
13730Sstevel@tonic-gate /*
13740Sstevel@tonic-gate * If number of keys are greater that
13750Sstevel@tonic-gate * 1, then we have an invalid
13760Sstevel@tonic-gate * namespace. STOP and return.
13770Sstevel@tonic-gate */
13780Sstevel@tonic-gate if (nkeys > 1)
13790Sstevel@tonic-gate return (mddeverror(ep, MDE_MULTNM, tmpdev));
13800Sstevel@tonic-gate if (devkey != raidkey)
13810Sstevel@tonic-gate return (mdcomperror(ep, MDE_CANT_FIND_COMP,
13820Sstevel@tonic-gate mnum, tmpdev));
13830Sstevel@tonic-gate
13840Sstevel@tonic-gate if (un->un_column[col].un_orig_dev == NODEV64)
13850Sstevel@tonic-gate un->un_column[col].un_orig_dev = tmpdev;
13860Sstevel@tonic-gate
13870Sstevel@tonic-gate if (HOTSPARED(un, col)) {
13880Sstevel@tonic-gate un->un_column[col].un_alt_dev =
13890Sstevel@tonic-gate un->un_column[col].un_orig_dev;
13900Sstevel@tonic-gate un->un_column[col].un_alt_pwstart =
13910Sstevel@tonic-gate un->un_column[col].un_orig_pwstart;
13920Sstevel@tonic-gate un->un_column[col].un_alt_devstart =
13930Sstevel@tonic-gate un->un_column[col].un_orig_devstart;
13940Sstevel@tonic-gate un->un_column[col].un_devflags |= MD_RAID_COPY_RESYNC;
13950Sstevel@tonic-gate } else {
13960Sstevel@tonic-gate if (!(un->un_column[col].un_devflags &
13977563SPrasad.Singamsetty@Sun.COM MD_RAID_DEV_ISOPEN)) {
13980Sstevel@tonic-gate if (md_layered_open(mnum, &tmpdev,
13990Sstevel@tonic-gate MD_OFLG_NULL)) {
14000Sstevel@tonic-gate un->un_column[col].un_dev = tmpdev;
14010Sstevel@tonic-gate return (mdcomperror(ep,
14020Sstevel@tonic-gate MDE_COMP_OPEN_ERR, mnum, tmpdev));
14030Sstevel@tonic-gate }
14040Sstevel@tonic-gate ASSERT(tmpdev != NODEV64 &&
14050Sstevel@tonic-gate tmpdev != 0);
14060Sstevel@tonic-gate
14070Sstevel@tonic-gate if ((md_getmajor(tmpdev) != md_major) &&
14087563SPrasad.Singamsetty@Sun.COM (md_devid_found(setno, side, raidkey)
14097563SPrasad.Singamsetty@Sun.COM == 1)) {
14100Sstevel@tonic-gate if (md_update_namespace_did(setno, side,
14110Sstevel@tonic-gate raidkey, &mde) != 0) {
14120Sstevel@tonic-gate cmn_err(CE_WARN,
14130Sstevel@tonic-gate "md: could not"
14147563SPrasad.Singamsetty@Sun.COM " update namespace\n");
14150Sstevel@tonic-gate }
14160Sstevel@tonic-gate }
14170Sstevel@tonic-gate un->un_column[col].un_dev =
14187563SPrasad.Singamsetty@Sun.COM un->un_column[col].un_orig_dev;
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate un->un_column[col].un_devflags |= MD_RAID_DEV_ISOPEN;
14210Sstevel@tonic-gate un->un_column[col].un_devflags |= MD_RAID_REGEN_RESYNC;
14220Sstevel@tonic-gate }
14230Sstevel@tonic-gate }
14240Sstevel@tonic-gate if (mrp->has_label) {
14250Sstevel@tonic-gate un->un_column[col].un_devflags |= MD_RAID_HAS_LABEL;
14260Sstevel@tonic-gate } else {
14270Sstevel@tonic-gate un->un_column[col].un_devflags &= ~MD_RAID_HAS_LABEL;
14280Sstevel@tonic-gate }
14290Sstevel@tonic-gate
14300Sstevel@tonic-gate raid_commit(un, extra_recids);
14310Sstevel@tonic-gate
14320Sstevel@tonic-gate /* If the component has been replaced - clean up the name space */
14330Sstevel@tonic-gate if (sv.setno != MD_SET_BAD) {
14340Sstevel@tonic-gate md_rem_names(&sv, 1);
14350Sstevel@tonic-gate }
14360Sstevel@tonic-gate
14370Sstevel@tonic-gate md_ioctl_droplocks(lock);
14380Sstevel@tonic-gate
14390Sstevel@tonic-gate if ((cmd == ENABLE_COMP) || (cmd == FORCE_ENABLE_COMP)) {
14400Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_ENABLE, SVM_TAG_METADEVICE,
14410Sstevel@tonic-gate setno, MD_SID(un));
14420Sstevel@tonic-gate } else {
14430Sstevel@tonic-gate SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_REPLACE, SVM_TAG_METADEVICE,
14440Sstevel@tonic-gate setno, MD_SID(un));
14450Sstevel@tonic-gate }
14460Sstevel@tonic-gate
14470Sstevel@tonic-gate if (un->un_column[col].un_devstate & RCS_INIT)
14480Sstevel@tonic-gate err = raid_init_unit(mnum, ep);
14490Sstevel@tonic-gate else if ((mrp->options & MDIOCTL_NO_RESYNC_RAID) == 0)
14500Sstevel@tonic-gate err = raid_resync_unit(mnum, ep);
14510Sstevel@tonic-gate
14520Sstevel@tonic-gate mdclrerror(ep);
14530Sstevel@tonic-gate if (!err)
14540Sstevel@tonic-gate return (0);
14550Sstevel@tonic-gate
14560Sstevel@tonic-gate /* be sure state */
14570Sstevel@tonic-gate /* is already set by this time */
14580Sstevel@tonic-gate /* fix state and commit record */
14590Sstevel@tonic-gate un = md_unit_writerlock(MDI_UNIT(mnum));
14600Sstevel@tonic-gate if (state & RCS_INIT_ERRED)
14610Sstevel@tonic-gate raid_set_state(un, col, state, 1);
14620Sstevel@tonic-gate else if (state & RCS_OKAY)
14630Sstevel@tonic-gate raid_set_state(un, col, RCS_ERRED, 0);
14640Sstevel@tonic-gate else
14650Sstevel@tonic-gate raid_set_state(un, col, state, 1);
14660Sstevel@tonic-gate raid_commit(un, NULL);
14670Sstevel@tonic-gate md_unit_writerexit(MDI_UNIT(mnum));
14680Sstevel@tonic-gate mdclrerror(ep);
14690Sstevel@tonic-gate return (0);
14700Sstevel@tonic-gate }
14710Sstevel@tonic-gate
14720Sstevel@tonic-gate
14730Sstevel@tonic-gate /*
14740Sstevel@tonic-gate * NAME: raid_set_sync
14750Sstevel@tonic-gate * DESCRIPTION: used to sync a component of a RAID metadevice
14760Sstevel@tonic-gate * PARAMETERS: md_resync_ioctl_t *mrp - pointer to resync data structure
14770Sstevel@tonic-gate * int mode - must be FWRITE
14780Sstevel@tonic-gate * IOLOCK *lock - pointer to IOCTL lock
14790Sstevel@tonic-gate *
14800Sstevel@tonic-gate * LOCKS: obtains unit writer lock via IOLOCK (through raid_getun),
14810Sstevel@tonic-gate * obtains and releases md_unit_array_rw write lock
14820Sstevel@tonic-gate *
14830Sstevel@tonic-gate */
14840Sstevel@tonic-gate static int
raid_set_sync(md_resync_ioctl_t * rip,IOLOCK * lock)14850Sstevel@tonic-gate raid_set_sync(
14860Sstevel@tonic-gate md_resync_ioctl_t *rip,
14870Sstevel@tonic-gate IOLOCK *lock
14880Sstevel@tonic-gate )
14890Sstevel@tonic-gate {
14900Sstevel@tonic-gate minor_t mnum = rip->ri_mnum;
14910Sstevel@tonic-gate mr_unit_t *un;
14920Sstevel@tonic-gate int init = 0;
14930Sstevel@tonic-gate int resync = 0;
14940Sstevel@tonic-gate int regen = 0;
14950Sstevel@tonic-gate int ix;
14960Sstevel@tonic-gate int err;
14970Sstevel@tonic-gate
14980Sstevel@tonic-gate mdclrerror(&rip->mde);
14990Sstevel@tonic-gate
15000Sstevel@tonic-gate if ((un = raid_getun(mnum, &rip->mde, WRITERS, lock)) == NULL)
15010Sstevel@tonic-gate return (0);
15020Sstevel@tonic-gate
15030Sstevel@tonic-gate if (un->un_state & RUS_DOI)
15040Sstevel@tonic-gate return (mdmderror(&rip->mde, MDE_RAID_DOI, mnum));
15050Sstevel@tonic-gate
15060Sstevel@tonic-gate if (un->c.un_status & MD_UN_RESYNC_ACTIVE)
15070Sstevel@tonic-gate return (mdmderror(&rip->mde, MDE_RESYNC_ACTIVE, mnum));
15080Sstevel@tonic-gate
15090Sstevel@tonic-gate /* This prevents new opens */
15100Sstevel@tonic-gate
15110Sstevel@tonic-gate rip->ri_flags = 0;
15120Sstevel@tonic-gate if (un->un_state & RUS_REGEN)
15130Sstevel@tonic-gate regen++;
15140Sstevel@tonic-gate
15150Sstevel@tonic-gate if (raid_state_cnt(un, RCS_RESYNC))
15160Sstevel@tonic-gate resync++;
15170Sstevel@tonic-gate
15180Sstevel@tonic-gate if (raid_state_cnt(un, RCS_INIT) || (un->un_state & RUS_INIT))
15190Sstevel@tonic-gate init++;
15200Sstevel@tonic-gate
15210Sstevel@tonic-gate ASSERT(!(resync && init && regen));
15220Sstevel@tonic-gate md_ioctl_droplocks(lock);
15230Sstevel@tonic-gate rip->ri_percent_done = 0;
15240Sstevel@tonic-gate
15250Sstevel@tonic-gate if (init) {
15260Sstevel@tonic-gate MD_STATUS(un) |= MD_UN_GROW_PENDING;
15270Sstevel@tonic-gate return (raid_init_unit(mnum, &rip->mde));
15280Sstevel@tonic-gate }
15290Sstevel@tonic-gate
15300Sstevel@tonic-gate /*
15310Sstevel@tonic-gate * If resync is needed, it will call raid_internal_open forcing
15320Sstevel@tonic-gate * replay before the open completes.
15330Sstevel@tonic-gate * Otherwise, call raid_internal_open directly to force
15340Sstevel@tonic-gate * replay to complete during boot (metasync -r).
15350Sstevel@tonic-gate * NOTE: the unit writer lock must remain held while setting
15360Sstevel@tonic-gate * MD_UN_RESYNC_ACTIVE but must be released before
15370Sstevel@tonic-gate * calling raid_resync_unit or raid_internal_open.
15380Sstevel@tonic-gate */
15390Sstevel@tonic-gate if (resync) {
15400Sstevel@tonic-gate ASSERT(resync < 2);
15410Sstevel@tonic-gate un = md_unit_writerlock(MDI_UNIT(mnum));
15420Sstevel@tonic-gate MD_STATUS(un) |= MD_UN_RESYNC_ACTIVE;
15430Sstevel@tonic-gate /* Must release unit writer lock for resync */
15440Sstevel@tonic-gate /*
15450Sstevel@tonic-gate * correctly setup the devices before trying to start the
15460Sstevel@tonic-gate * resync operation.
15470Sstevel@tonic-gate */
15480Sstevel@tonic-gate for (ix = 0; un->un_totalcolumncnt; ix++) {
15490Sstevel@tonic-gate if (un->un_column[ix].un_devstate & RCS_RESYNC) {
15500Sstevel@tonic-gate if ((un->un_column[ix].un_devflags &
15510Sstevel@tonic-gate MD_RAID_COPY_RESYNC) &&
15520Sstevel@tonic-gate HOTSPARED(un, ix)) {
15530Sstevel@tonic-gate un->un_column[ix].un_alt_dev =
15540Sstevel@tonic-gate un->un_column[ix].un_orig_dev;
15550Sstevel@tonic-gate un->un_column[ix].un_alt_devstart =
15560Sstevel@tonic-gate un->un_column[ix].un_orig_devstart;
15570Sstevel@tonic-gate un->un_column[ix].un_alt_pwstart =
15580Sstevel@tonic-gate un->un_column[ix].un_orig_pwstart;
15590Sstevel@tonic-gate }
15600Sstevel@tonic-gate break;
15610Sstevel@tonic-gate }
15620Sstevel@tonic-gate }
15630Sstevel@tonic-gate ASSERT(un->un_column[ix].un_devflags &
15640Sstevel@tonic-gate (MD_RAID_COPY_RESYNC | MD_RAID_REGEN_RESYNC));
15650Sstevel@tonic-gate rip->ri_percent_done = 0;
15660Sstevel@tonic-gate un->un_column[ix].un_devflags |= MD_RAID_RESYNC;
15670Sstevel@tonic-gate (void) resync_request(mnum, ix, 0, NULL);
15680Sstevel@tonic-gate md_unit_writerexit(MDI_UNIT(mnum));
15690Sstevel@tonic-gate err = raid_resync_unit(mnum, &rip->mde);
15700Sstevel@tonic-gate return (err);
15710Sstevel@tonic-gate }
15720Sstevel@tonic-gate
15730Sstevel@tonic-gate if (regen) {
15740Sstevel@tonic-gate err = raid_regen_unit(mnum, &rip->mde);
15750Sstevel@tonic-gate return (err);
15760Sstevel@tonic-gate }
15770Sstevel@tonic-gate
15780Sstevel@tonic-gate /* The unit requires not work so just force replay of the device */
15790Sstevel@tonic-gate if (raid_internal_open(mnum, (FREAD | FWRITE), OTYP_LYR, 0))
15800Sstevel@tonic-gate return (mdmderror(&rip->mde,
15817563SPrasad.Singamsetty@Sun.COM MDE_RAID_OPEN_FAILURE, mnum));
15820Sstevel@tonic-gate (void) raid_internal_close(mnum, OTYP_LYR, 0, 0);
15830Sstevel@tonic-gate
15840Sstevel@tonic-gate return (0);
15850Sstevel@tonic-gate }
15860Sstevel@tonic-gate
15870Sstevel@tonic-gate /*
15880Sstevel@tonic-gate * NAME: raid_get_resync
15890Sstevel@tonic-gate * DESCRIPTION: used to check resync status on a component of a RAID metadevice
15900Sstevel@tonic-gate * PARAMETERS: md_resync_ioctl_t *mrp - pointer to resync data structure
15910Sstevel@tonic-gate * int mode - must be FWRITE
15920Sstevel@tonic-gate * IOLOCK *lock - pointer to IOCTL lock
15930Sstevel@tonic-gate *
15940Sstevel@tonic-gate * LOCKS: none
15950Sstevel@tonic-gate *
15960Sstevel@tonic-gate */
15970Sstevel@tonic-gate static int
raid_get_resync(md_resync_ioctl_t * rip,IOLOCK * lock)15980Sstevel@tonic-gate raid_get_resync(
15990Sstevel@tonic-gate md_resync_ioctl_t *rip,
16000Sstevel@tonic-gate IOLOCK *lock
16010Sstevel@tonic-gate )
16020Sstevel@tonic-gate {
16030Sstevel@tonic-gate minor_t mnum = rip->ri_mnum;
16040Sstevel@tonic-gate mr_unit_t *un;
16050Sstevel@tonic-gate u_longlong_t percent;
16060Sstevel@tonic-gate int cnt;
16070Sstevel@tonic-gate int ix;
16080Sstevel@tonic-gate uint64_t d;
16090Sstevel@tonic-gate
16100Sstevel@tonic-gate mdclrerror(&rip->mde);
16110Sstevel@tonic-gate
16120Sstevel@tonic-gate if ((un = raid_getun(mnum, &rip->mde, RD_LOCK, lock)) == NULL)
16130Sstevel@tonic-gate return (0);
16140Sstevel@tonic-gate
16150Sstevel@tonic-gate rip->ri_flags = 0;
16160Sstevel@tonic-gate if (MD_STATUS(un) & MD_UN_RESYNC_ACTIVE) {
16170Sstevel@tonic-gate d = un->un_segsincolumn;
16180Sstevel@tonic-gate percent = d ? ((1000 * un->un_resync_line_index) / d) : 0;
16190Sstevel@tonic-gate if (percent > 1000)
16200Sstevel@tonic-gate percent = 1000; /* can't go over 100% */
16210Sstevel@tonic-gate rip->ri_percent_done = (int)percent;
16220Sstevel@tonic-gate rip->ri_flags |= MD_RI_INPROGRESS;
16230Sstevel@tonic-gate }
16240Sstevel@tonic-gate
16250Sstevel@tonic-gate if (UNIT_STATE(un) & RUS_INIT) {
16260Sstevel@tonic-gate d = un->un_segsize * un->un_segsincolumn *
16270Sstevel@tonic-gate un->un_totalcolumncnt;
16280Sstevel@tonic-gate percent =
16290Sstevel@tonic-gate d ? ((1000 * (u_longlong_t)un->un_init_iocnt) / d) : 0;
16300Sstevel@tonic-gate if (percent > 1000)
16310Sstevel@tonic-gate percent = 1000; /* can't go over 100% */
16320Sstevel@tonic-gate rip->ri_percent_done = (int)percent;
16330Sstevel@tonic-gate rip->ri_flags |= MD_GROW_INPROGRESS;
16340Sstevel@tonic-gate } else if (MD_STATUS(un) & MD_UN_GROW_PENDING) {
16350Sstevel@tonic-gate d = un->un_segsize * un->un_segsincolumn * un->un_init_colcnt;
16360Sstevel@tonic-gate percent =
16370Sstevel@tonic-gate d ? (((u_longlong_t)un->un_init_iocnt * 1000) / d) : 0;
16380Sstevel@tonic-gate if (percent > 1000)
16390Sstevel@tonic-gate percent = 1000;
16400Sstevel@tonic-gate rip->ri_percent_done = (int)percent;
16410Sstevel@tonic-gate rip->ri_flags |= MD_GROW_INPROGRESS;
16420Sstevel@tonic-gate }
16430Sstevel@tonic-gate
16440Sstevel@tonic-gate if (un->un_state & RUS_REGEN)
16450Sstevel@tonic-gate rip->ri_percent_done = un->un_percent_done;
16460Sstevel@tonic-gate
16470Sstevel@tonic-gate cnt = 0;
16480Sstevel@tonic-gate for (ix = 0; ix < un->un_totalcolumncnt; ix++) {
16490Sstevel@tonic-gate switch (un->un_column[ix].un_devstate) {
16500Sstevel@tonic-gate case RCS_INIT:
16510Sstevel@tonic-gate case RCS_ERRED:
16520Sstevel@tonic-gate case RCS_LAST_ERRED:
16530Sstevel@tonic-gate cnt++;
16540Sstevel@tonic-gate break;
16550Sstevel@tonic-gate default:
16560Sstevel@tonic-gate break;
16570Sstevel@tonic-gate }
16580Sstevel@tonic-gate }
16590Sstevel@tonic-gate d = un->un_totalcolumncnt;
16600Sstevel@tonic-gate rip->ri_percent_dirty = d ? (((u_longlong_t)cnt * 100) / d) : 0;
16610Sstevel@tonic-gate return (0);
16620Sstevel@tonic-gate }
16630Sstevel@tonic-gate
16640Sstevel@tonic-gate /*
16650Sstevel@tonic-gate * NAME: raid_grow
16660Sstevel@tonic-gate * DESCRIPTION: Concatenate to a RAID metadevice
16670Sstevel@tonic-gate * PARAMETERS: md_grow_params_t *mgp
16680Sstevel@tonic-gate * - pointer to IOCGROW data structure
16690Sstevel@tonic-gate * int mode - must be FWRITE
16700Sstevel@tonic-gate * IOLOCK *lockp - IOCTL read/write and unit_array_rw lock
16710Sstevel@tonic-gate *
16720Sstevel@tonic-gate * LOCKS: obtains unit writer lock via IOLOCK (through raid_getun),
16730Sstevel@tonic-gate * obtains and releases md_unit_array_rw write lock
16740Sstevel@tonic-gate *
16750Sstevel@tonic-gate */
16760Sstevel@tonic-gate static int
raid_grow(void * mgp,int mode,IOLOCK * lock)16770Sstevel@tonic-gate raid_grow(void *mgp, int mode, IOLOCK *lock)
16780Sstevel@tonic-gate {
16790Sstevel@tonic-gate minor_t mnum;
16800Sstevel@tonic-gate mr_unit_t *un, *new_un;
16810Sstevel@tonic-gate mdi_unit_t *ui;
16820Sstevel@tonic-gate mddb_type_t typ1;
16830Sstevel@tonic-gate mddb_recid_t mr_recid;
16840Sstevel@tonic-gate mddb_recid_t old_vtoc = 0;
16850Sstevel@tonic-gate mddb_recid_t *recids;
16861623Stw21770 md_create_rec_option_t options;
16870Sstevel@tonic-gate int err;
16880Sstevel@tonic-gate int col, i;
16890Sstevel@tonic-gate int64_t tb, atb;
16900Sstevel@tonic-gate u_longlong_t unrev;
16910Sstevel@tonic-gate int tc;
16920Sstevel@tonic-gate int rval = 0;
16930Sstevel@tonic-gate set_t setno;
16940Sstevel@tonic-gate mr_column_ic_t *mrc;
16950Sstevel@tonic-gate int num_recs, rid;
16960Sstevel@tonic-gate md_grow_params_t *mgph = mgp;
16970Sstevel@tonic-gate
16980Sstevel@tonic-gate
16990Sstevel@tonic-gate mnum = mgph->mnum;
17000Sstevel@tonic-gate
17010Sstevel@tonic-gate mdclrerror(&mgph->mde);
17020Sstevel@tonic-gate
17030Sstevel@tonic-gate ui = MDI_UNIT(mnum);
17040Sstevel@tonic-gate un = md_unit_readerlock(ui);
17050Sstevel@tonic-gate
17060Sstevel@tonic-gate if (MD_STATUS(un) & MD_UN_GROW_PENDING) {
17070Sstevel@tonic-gate md_unit_readerexit(ui);
17080Sstevel@tonic-gate return (mdmderror(&mgph->mde, MDE_IN_USE, mnum));
17090Sstevel@tonic-gate }
17100Sstevel@tonic-gate
17110Sstevel@tonic-gate if (MD_STATUS(un) & MD_UN_RESYNC_ACTIVE) {
17120Sstevel@tonic-gate md_unit_readerexit(ui);
17137563SPrasad.Singamsetty@Sun.COM return (mdmderror(&mgph->mde, MDE_RESYNC_ACTIVE, mnum));
17140Sstevel@tonic-gate }
17150Sstevel@tonic-gate
17160Sstevel@tonic-gate if (UNIT_STATE(un) & RUS_LAST_ERRED) {
17170Sstevel@tonic-gate md_unit_readerexit(ui);
17187563SPrasad.Singamsetty@Sun.COM return (mdmderror(&mgph->mde, MDE_RAID_LAST_ERRED, mnum));
17190Sstevel@tonic-gate }
17200Sstevel@tonic-gate
17210Sstevel@tonic-gate if (UNIT_STATE(un) & RUS_DOI) {
17220Sstevel@tonic-gate md_unit_readerexit(ui);
17230Sstevel@tonic-gate return (mdmderror(&mgph->mde, MDE_RAID_DOI, mnum));
17240Sstevel@tonic-gate }
17250Sstevel@tonic-gate
17260Sstevel@tonic-gate if ((raid_state_cnt(un, RCS_INIT) != 0) || (un->un_state & RUS_INIT)) {
17270Sstevel@tonic-gate md_unit_readerexit(ui);
17280Sstevel@tonic-gate return (mdmderror(&mgph->mde, MDE_IN_USE, mnum));
17290Sstevel@tonic-gate }
17300Sstevel@tonic-gate
17310Sstevel@tonic-gate md_unit_readerexit(ui);
17320Sstevel@tonic-gate
17330Sstevel@tonic-gate if ((un = raid_getun(mnum, &mgph->mde, WRITERS, lock)) ==
17347563SPrasad.Singamsetty@Sun.COM NULL)
17350Sstevel@tonic-gate return (0);
17360Sstevel@tonic-gate
17370Sstevel@tonic-gate if (MD_STATUS(un) & MD_UN_GROW_PENDING)
17380Sstevel@tonic-gate return (mdmderror(&mgph->mde, MDE_IN_USE, mnum));
17390Sstevel@tonic-gate
17400Sstevel@tonic-gate if (MD_STATUS(un) & MD_UN_RESYNC_ACTIVE)
17417563SPrasad.Singamsetty@Sun.COM return (mdmderror(&mgph->mde, MDE_RESYNC_ACTIVE, mnum));
17420Sstevel@tonic-gate
17430Sstevel@tonic-gate if (un->c.un_size >= mgph->size)
17440Sstevel@tonic-gate return (EINVAL);
17450Sstevel@tonic-gate
17460Sstevel@tonic-gate if (UNIT_STATE(un) & RUS_LAST_ERRED)
17477563SPrasad.Singamsetty@Sun.COM return (mdmderror(&mgph->mde, MDE_RAID_LAST_ERRED, mnum));
17480Sstevel@tonic-gate
17490Sstevel@tonic-gate if (UNIT_STATE(un) & RUS_DOI)
17500Sstevel@tonic-gate return (mdmderror(&mgph->mde, MDE_RAID_DOI, mnum));
17510Sstevel@tonic-gate
17520Sstevel@tonic-gate if ((raid_state_cnt(un, RCS_INIT) != 0) || (un->un_state & RUS_INIT))
17530Sstevel@tonic-gate return (mdmderror(&mgph->mde, MDE_IN_USE, mnum));
17540Sstevel@tonic-gate
17550Sstevel@tonic-gate setno = MD_MIN2SET(mnum);
17560Sstevel@tonic-gate
17570Sstevel@tonic-gate typ1 = (mddb_type_t)md_getshared_key(setno,
17580Sstevel@tonic-gate raid_md_ops.md_driver.md_drivername);
17590Sstevel@tonic-gate
17601623Stw21770 /*
17611623Stw21770 * Preserve the friendly name nature of the device that is
17621623Stw21770 * growing.
17631623Stw21770 */
17641623Stw21770 options = MD_CRO_RAID;
17651623Stw21770 if (un->c.un_revision & MD_FN_META_DEV)
17661623Stw21770 options |= MD_CRO_FN;
17670Sstevel@tonic-gate if (mgph->options & MD_CRO_64BIT) {
17680Sstevel@tonic-gate #if defined(_ILP32)
17690Sstevel@tonic-gate return (mdmderror(&mgph->mde, MDE_UNIT_TOO_LARGE, mnum));
17700Sstevel@tonic-gate #else
17710Sstevel@tonic-gate mr_recid = mddb_createrec(mgph->size, typ1, 0,
17727563SPrasad.Singamsetty@Sun.COM MD_CRO_64BIT | options, setno);
17730Sstevel@tonic-gate #endif
17740Sstevel@tonic-gate } else {
17750Sstevel@tonic-gate mr_recid = mddb_createrec(mgph->size, typ1, 0,
17767563SPrasad.Singamsetty@Sun.COM MD_CRO_32BIT | options, setno);
17770Sstevel@tonic-gate }
17780Sstevel@tonic-gate if (mr_recid < 0) {
17790Sstevel@tonic-gate rval = mddbstatus2error(&mgph->mde, (int)mr_recid,
17807563SPrasad.Singamsetty@Sun.COM mnum, setno);
17810Sstevel@tonic-gate return (rval);
17820Sstevel@tonic-gate }
17830Sstevel@tonic-gate
17840Sstevel@tonic-gate /* get the address of the new unit */
17850Sstevel@tonic-gate new_un = (mr_unit_t *)mddb_getrecaddr(mr_recid);
17860Sstevel@tonic-gate
17870Sstevel@tonic-gate /*
17880Sstevel@tonic-gate * It is okay that we muck with the new unit here,
17890Sstevel@tonic-gate * since no one else will know about the unit struct
17900Sstevel@tonic-gate * until we commit it. If we crash, the record will
17910Sstevel@tonic-gate * be automatically purged, since we haven't
17920Sstevel@tonic-gate * committed it yet and the old unit struct will be found.
17930Sstevel@tonic-gate */
17940Sstevel@tonic-gate
17950Sstevel@tonic-gate /* copy in the user's unit struct */
17960Sstevel@tonic-gate err = ddi_copyin((void *)(uintptr_t)mgph->mdp, new_un,
17970Sstevel@tonic-gate mgph->size, mode);
17980Sstevel@tonic-gate if (err) {
17990Sstevel@tonic-gate mddb_deleterec_wrapper(mr_recid);
18000Sstevel@tonic-gate return (EFAULT);
18010Sstevel@tonic-gate }
18020Sstevel@tonic-gate
18030Sstevel@tonic-gate /* make sure columns are being added */
18040Sstevel@tonic-gate if (un->un_totalcolumncnt >= new_un->un_totalcolumncnt) {
18050Sstevel@tonic-gate mddb_deleterec_wrapper(mr_recid);
18060Sstevel@tonic-gate return (EINVAL);
18070Sstevel@tonic-gate }
18080Sstevel@tonic-gate
18090Sstevel@tonic-gate /*
18100Sstevel@tonic-gate * Save a few of the new unit structs fields.
18110Sstevel@tonic-gate * Before they get clobbered.
18120Sstevel@tonic-gate */
18130Sstevel@tonic-gate tc = new_un->un_totalcolumncnt;
18140Sstevel@tonic-gate tb = new_un->c.un_total_blocks;
18150Sstevel@tonic-gate atb = new_un->c.un_actual_tb;
18160Sstevel@tonic-gate unrev = new_un->c.un_revision;
18170Sstevel@tonic-gate
18180Sstevel@tonic-gate /*
18190Sstevel@tonic-gate * Copy the old unit struct (static stuff)
18200Sstevel@tonic-gate * into new unit struct
18210Sstevel@tonic-gate */
18220Sstevel@tonic-gate bcopy((caddr_t)un, (caddr_t)new_un, un->c.un_size);
18230Sstevel@tonic-gate
18240Sstevel@tonic-gate /*
18250Sstevel@tonic-gate * Restore a few of the new unit struct values.
18260Sstevel@tonic-gate */
18270Sstevel@tonic-gate new_un->un_totalcolumncnt = tc;
18280Sstevel@tonic-gate new_un->c.un_actual_tb = atb;
18290Sstevel@tonic-gate new_un->un_grow_tb = tb;
18300Sstevel@tonic-gate new_un->c.un_revision = unrev;
18310Sstevel@tonic-gate new_un->c.un_record_id = mr_recid;
18320Sstevel@tonic-gate new_un->c.un_size = mgph->size;
18330Sstevel@tonic-gate
18340Sstevel@tonic-gate ASSERT(new_un->mr_ic == un->mr_ic);
18350Sstevel@tonic-gate
18360Sstevel@tonic-gate /*
18370Sstevel@tonic-gate * Save old column slots
18380Sstevel@tonic-gate */
18390Sstevel@tonic-gate mrc = un->un_column_ic;
18400Sstevel@tonic-gate
18410Sstevel@tonic-gate /*
18420Sstevel@tonic-gate * Allocate new column slot
18430Sstevel@tonic-gate */
18440Sstevel@tonic-gate new_un->un_column_ic = (mr_column_ic_t *)
18450Sstevel@tonic-gate kmem_zalloc(sizeof (mr_column_ic_t) * new_un->un_totalcolumncnt,
18467563SPrasad.Singamsetty@Sun.COM KM_SLEEP);
18470Sstevel@tonic-gate
18480Sstevel@tonic-gate /*
18490Sstevel@tonic-gate * Restore old column slots
18500Sstevel@tonic-gate * Free the old column slots
18510Sstevel@tonic-gate */
18520Sstevel@tonic-gate bcopy(mrc, new_un->un_column_ic,
18537563SPrasad.Singamsetty@Sun.COM sizeof (mr_column_ic_t) * un->un_totalcolumncnt);
18540Sstevel@tonic-gate kmem_free(mrc, sizeof (mr_column_ic_t) * un->un_totalcolumncnt);
18550Sstevel@tonic-gate
18560Sstevel@tonic-gate /* All 64 bit metadevices only support EFI labels. */
18570Sstevel@tonic-gate if (mgph->options & MD_CRO_64BIT) {
18580Sstevel@tonic-gate new_un->c.un_flag |= MD_EFILABEL;
18590Sstevel@tonic-gate /*
18600Sstevel@tonic-gate * If the device was previously smaller than a terabyte,
18610Sstevel@tonic-gate * and had a vtoc record attached to it, we remove the
18620Sstevel@tonic-gate * vtoc record, because the layout has changed completely.
18630Sstevel@tonic-gate */
18641623Stw21770 if (((un->c.un_revision & MD_64BIT_META_DEV) == 0) &&
18650Sstevel@tonic-gate (un->c.un_vtoc_id != 0)) {
18660Sstevel@tonic-gate old_vtoc = un->c.un_vtoc_id;
18670Sstevel@tonic-gate new_un->c.un_vtoc_id =
18687563SPrasad.Singamsetty@Sun.COM md_vtoc_to_efi_record(old_vtoc, setno);
18690Sstevel@tonic-gate }
18700Sstevel@tonic-gate }
18710Sstevel@tonic-gate
18720Sstevel@tonic-gate
18730Sstevel@tonic-gate /*
18740Sstevel@tonic-gate * allocate the real recids array. since we may have to commit
18750Sstevel@tonic-gate * underlying metadevice records, we need an array of size:
18760Sstevel@tonic-gate * total number of new components being attach + 2 (one for the
18770Sstevel@tonic-gate * raid itself, one for the end marker).
18780Sstevel@tonic-gate */
18790Sstevel@tonic-gate num_recs = new_un->un_totalcolumncnt + 2;
18800Sstevel@tonic-gate rid = 0;
18810Sstevel@tonic-gate recids = kmem_alloc(num_recs * sizeof (mddb_recid_t), KM_SLEEP);
18820Sstevel@tonic-gate recids[rid++] = mr_recid;
18830Sstevel@tonic-gate
18840Sstevel@tonic-gate for (col = un->un_totalcolumncnt;
18850Sstevel@tonic-gate (col < new_un->un_totalcolumncnt); col++) {
18860Sstevel@tonic-gate mr_column_t *mr_col = &new_un->un_column[col];
18870Sstevel@tonic-gate md_unit_t *comp_un;
18880Sstevel@tonic-gate
18890Sstevel@tonic-gate if (raid_build_pw_reservation(new_un, col) != 0) {
18900Sstevel@tonic-gate /* release pwslots already allocated by grow */
18910Sstevel@tonic-gate for (i = un->un_totalcolumncnt; i < col; i++) {
18920Sstevel@tonic-gate raid_free_pw_reservation(new_un, i);
18930Sstevel@tonic-gate }
18940Sstevel@tonic-gate kmem_free(new_un->un_column_ic,
18950Sstevel@tonic-gate sizeof (mr_column_ic_t) *
18960Sstevel@tonic-gate new_un->un_totalcolumncnt);
18970Sstevel@tonic-gate kmem_free(new_un->mr_ic, sizeof (*un->mr_ic));
18980Sstevel@tonic-gate kmem_free(recids, num_recs * sizeof (mddb_recid_t));
18990Sstevel@tonic-gate mddb_deleterec_wrapper(mr_recid);
19000Sstevel@tonic-gate return (EINVAL);
19010Sstevel@tonic-gate }
19020Sstevel@tonic-gate /*
19030Sstevel@tonic-gate * set parent on metadevices being added.
19040Sstevel@tonic-gate * NOTE: currently soft partitions are the only metadevices
19050Sstevel@tonic-gate * which can appear within a RAID metadevice.
19060Sstevel@tonic-gate */
19070Sstevel@tonic-gate if (md_getmajor(mr_col->un_dev) == md_major) {
19080Sstevel@tonic-gate comp_un = MD_UNIT(md_getminor(mr_col->un_dev));
19090Sstevel@tonic-gate recids[rid++] = MD_RECID(comp_un);
19100Sstevel@tonic-gate md_set_parent(mr_col->un_dev, MD_SID(new_un));
19110Sstevel@tonic-gate }
19120Sstevel@tonic-gate new_un->un_column[col].un_devflags = 0;
19130Sstevel@tonic-gate }
19140Sstevel@tonic-gate
19150Sstevel@tonic-gate /* set end marker */
19160Sstevel@tonic-gate recids[rid] = 0;
19170Sstevel@tonic-gate
19180Sstevel@tonic-gate /* commit new unit struct */
19190Sstevel@tonic-gate mddb_commitrecs_wrapper(recids);
19200Sstevel@tonic-gate
19210Sstevel@tonic-gate /* delete old unit struct */
19220Sstevel@tonic-gate mddb_deleterec_wrapper(un->c.un_record_id);
19237627SChris.Horne@Sun.COM
19247627SChris.Horne@Sun.COM /* place new unit in in-core array */
19257627SChris.Horne@Sun.COM md_nblocks_set(mnum, new_un->c.un_total_blocks);
19260Sstevel@tonic-gate MD_UNIT(mnum) = new_un;
19270Sstevel@tonic-gate
19280Sstevel@tonic-gate /*
19290Sstevel@tonic-gate * If old_vtoc has a non zero value, we know:
19300Sstevel@tonic-gate * - This unit crossed the border from smaller to larger one TB
19310Sstevel@tonic-gate * - There was a vtoc record for the unit,
19320Sstevel@tonic-gate * - This vtoc record is no longer needed, because
19330Sstevel@tonic-gate * a new efi record has been created for this un.
19340Sstevel@tonic-gate */
19350Sstevel@tonic-gate if (old_vtoc != 0) {
19360Sstevel@tonic-gate mddb_deleterec_wrapper(old_vtoc);
19370Sstevel@tonic-gate }
19380Sstevel@tonic-gate
19390Sstevel@tonic-gate /* free recids */
19400Sstevel@tonic-gate kmem_free(recids, num_recs * sizeof (mddb_recid_t));
19410Sstevel@tonic-gate
19420Sstevel@tonic-gate SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_GROW, SVM_TAG_METADEVICE,
19430Sstevel@tonic-gate MD_UN2SET(new_un), MD_SID(new_un));
19440Sstevel@tonic-gate MD_STATUS(new_un) |= MD_UN_GROW_PENDING;
19450Sstevel@tonic-gate
19460Sstevel@tonic-gate /*
19470Sstevel@tonic-gate * Since the md_ioctl_writelock aquires the unit write lock
19480Sstevel@tonic-gate * and open/close aquires the unit reader lock it is necessary
19490Sstevel@tonic-gate * to drop the unit write lock and then reaquire it as needed
19500Sstevel@tonic-gate * later.
19510Sstevel@tonic-gate */
19520Sstevel@tonic-gate md_unit_writerexit(ui);
19530Sstevel@tonic-gate
19540Sstevel@tonic-gate if (raid_internal_open(mnum, (FREAD | FWRITE), OTYP_LYR, 0)) {
19557563SPrasad.Singamsetty@Sun.COM rval = mdmderror(&mgph->mde, MDE_RAID_OPEN_FAILURE, mnum);
19560Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_OPEN_FAIL, SVM_TAG_METADEVICE,
19570Sstevel@tonic-gate MD_UN2SET(new_un), MD_SID(new_un));
19580Sstevel@tonic-gate return (rval);
19590Sstevel@tonic-gate }
19600Sstevel@tonic-gate (void) md_unit_writerlock(ui);
19610Sstevel@tonic-gate for (i = 0; i < new_un->un_totalcolumncnt; i++) {
19620Sstevel@tonic-gate if (new_un->un_column[i].un_devstate & RCS_OKAY)
19630Sstevel@tonic-gate (void) init_pw_area(new_un, new_un->un_column[i].un_dev,
19647563SPrasad.Singamsetty@Sun.COM new_un->un_column[i].un_pwstart, i);
19650Sstevel@tonic-gate }
19660Sstevel@tonic-gate md_unit_writerexit(ui);
19670Sstevel@tonic-gate (void) raid_internal_close(mnum, OTYP_LYR, 0, 0);
19680Sstevel@tonic-gate (void) md_unit_writerlock(ui);
19690Sstevel@tonic-gate /* create a background thread to initialize the columns */
19700Sstevel@tonic-gate md_ioctl_droplocks(lock);
19710Sstevel@tonic-gate
19720Sstevel@tonic-gate return (raid_init_unit(mnum, &mgph->mde));
19730Sstevel@tonic-gate }
19740Sstevel@tonic-gate
19750Sstevel@tonic-gate /*
19760Sstevel@tonic-gate * NAME: raid_reset
19770Sstevel@tonic-gate * DESCRIPTION: used to reset (clear / remove) a RAID metadevice
19780Sstevel@tonic-gate * PARAMETERS: md_i_reset_t *mirp - pointer to reset data structure
19790Sstevel@tonic-gate *
19800Sstevel@tonic-gate * LOCKS: obtains and releases md_unit_array_rw write lock
19810Sstevel@tonic-gate *
19820Sstevel@tonic-gate */
19830Sstevel@tonic-gate static int
raid_reset(md_i_reset_t * mirp)19840Sstevel@tonic-gate raid_reset(md_i_reset_t *mirp)
19850Sstevel@tonic-gate {
19860Sstevel@tonic-gate minor_t mnum = mirp->mnum;
19870Sstevel@tonic-gate mr_unit_t *un;
19880Sstevel@tonic-gate mdi_unit_t *ui;
19891623Stw21770 set_t setno = MD_MIN2SET(mnum);
19900Sstevel@tonic-gate
19910Sstevel@tonic-gate mdclrerror(&mirp->mde);
19920Sstevel@tonic-gate
19930Sstevel@tonic-gate rw_enter(&md_unit_array_rw.lock, RW_WRITER);
19940Sstevel@tonic-gate /*
19950Sstevel@tonic-gate * NOTE: need to get md_unit_writerlock to avoid conflict
19960Sstevel@tonic-gate * with raid_init thread.
19970Sstevel@tonic-gate */
19980Sstevel@tonic-gate if ((un = raid_getun(mnum, &mirp->mde, NO_LOCK, NULL)) ==
19990Sstevel@tonic-gate NULL) {
20000Sstevel@tonic-gate rw_exit(&md_unit_array_rw.lock);
20010Sstevel@tonic-gate return (0);
20020Sstevel@tonic-gate }
20030Sstevel@tonic-gate ui = MDI_UNIT(mnum);
20040Sstevel@tonic-gate
20050Sstevel@tonic-gate if (MD_HAS_PARENT(MD_PARENT(un))) {
20060Sstevel@tonic-gate rw_exit(&md_unit_array_rw.lock);
20070Sstevel@tonic-gate return (mdmderror(&mirp->mde, MDE_IN_USE, mnum));
20080Sstevel@tonic-gate }
20090Sstevel@tonic-gate
20100Sstevel@tonic-gate un = (mr_unit_t *)md_unit_openclose_enter(ui);
20110Sstevel@tonic-gate if (md_unit_isopen(MDI_UNIT(mnum))) {
20120Sstevel@tonic-gate md_unit_openclose_exit(ui);
20130Sstevel@tonic-gate rw_exit(&md_unit_array_rw.lock);
20140Sstevel@tonic-gate return (mdmderror(&mirp->mde, MDE_IS_OPEN, mnum));
20150Sstevel@tonic-gate }
20160Sstevel@tonic-gate md_unit_openclose_exit(ui);
20170Sstevel@tonic-gate if (UNIT_STATE(un) != RUS_OKAY && !mirp->force) {
20180Sstevel@tonic-gate rw_exit(&md_unit_array_rw.lock);
20190Sstevel@tonic-gate return (mdmderror(&mirp->mde, MDE_RAID_NEED_FORCE, mnum));
20200Sstevel@tonic-gate }
20210Sstevel@tonic-gate
20220Sstevel@tonic-gate reset_raid(un, mnum, 1);
20231623Stw21770
20241623Stw21770 /*
20251623Stw21770 * Update unit availability
20261623Stw21770 */
20271623Stw21770 md_set[setno].s_un_avail++;
20281623Stw21770
20291623Stw21770 /*
20301623Stw21770 * If MN set, reset s_un_next so all nodes can have
20311623Stw21770 * the same view of the next available slot when
20321623Stw21770 * nodes are -w and -j
20331623Stw21770 */
20341623Stw21770 if (MD_MNSET_SETNO(setno)) {
20351623Stw21770 (void) md_upd_set_unnext(setno, MD_MIN2UNIT(mnum));
20361623Stw21770 }
20371623Stw21770
20380Sstevel@tonic-gate rw_exit(&md_unit_array_rw.lock);
20390Sstevel@tonic-gate
20400Sstevel@tonic-gate return (0);
20410Sstevel@tonic-gate }
20420Sstevel@tonic-gate
20430Sstevel@tonic-gate /*
20440Sstevel@tonic-gate * NAME: raid_get_geom
20450Sstevel@tonic-gate * DESCRIPTION: used to get the geometry of a RAID metadevice
20460Sstevel@tonic-gate * PARAMETERS: mr_unit_t *un - RAID unit to get the geometry for
20470Sstevel@tonic-gate * struct dk_geom *gp - pointer to geometry data structure
20480Sstevel@tonic-gate *
20490Sstevel@tonic-gate * LOCKS: none
20500Sstevel@tonic-gate *
20510Sstevel@tonic-gate */
20520Sstevel@tonic-gate static int
raid_get_geom(mr_unit_t * un,struct dk_geom * geomp)20530Sstevel@tonic-gate raid_get_geom(
20540Sstevel@tonic-gate mr_unit_t *un,
20550Sstevel@tonic-gate struct dk_geom *geomp
20560Sstevel@tonic-gate )
20570Sstevel@tonic-gate {
20580Sstevel@tonic-gate md_get_geom((md_unit_t *)un, geomp);
20590Sstevel@tonic-gate
20600Sstevel@tonic-gate return (0);
20610Sstevel@tonic-gate }
20620Sstevel@tonic-gate
20630Sstevel@tonic-gate /*
20640Sstevel@tonic-gate * NAME: raid_get_vtoc
20650Sstevel@tonic-gate * DESCRIPTION: used to get the VTOC on a RAID metadevice
20660Sstevel@tonic-gate * PARAMETERS: mr_unit_t *un - RAID unit to get the VTOC from
20670Sstevel@tonic-gate * struct vtoc *vtocp - pointer to VTOC data structure
20680Sstevel@tonic-gate *
20690Sstevel@tonic-gate * LOCKS: none
20700Sstevel@tonic-gate *
20710Sstevel@tonic-gate */
20720Sstevel@tonic-gate static int
raid_get_vtoc(mr_unit_t * un,struct vtoc * vtocp)20730Sstevel@tonic-gate raid_get_vtoc(
20740Sstevel@tonic-gate mr_unit_t *un,
20750Sstevel@tonic-gate struct vtoc *vtocp
20760Sstevel@tonic-gate )
20770Sstevel@tonic-gate {
20780Sstevel@tonic-gate md_get_vtoc((md_unit_t *)un, vtocp);
20790Sstevel@tonic-gate
20800Sstevel@tonic-gate return (0);
20810Sstevel@tonic-gate }
20820Sstevel@tonic-gate
20830Sstevel@tonic-gate /*
20840Sstevel@tonic-gate * NAME: raid_set_vtoc
20850Sstevel@tonic-gate * DESCRIPTION: used to set the VTOC on a RAID metadevice
20860Sstevel@tonic-gate * PARAMETERS: mr_unit_t *un - RAID unit to set the VTOC on
20870Sstevel@tonic-gate * struct vtoc *vtocp - pointer to VTOC data structure
20880Sstevel@tonic-gate *
20890Sstevel@tonic-gate * LOCKS: none
20900Sstevel@tonic-gate *
20910Sstevel@tonic-gate */
20920Sstevel@tonic-gate static int
raid_set_vtoc(mr_unit_t * un,struct vtoc * vtocp)20930Sstevel@tonic-gate raid_set_vtoc(
20940Sstevel@tonic-gate mr_unit_t *un,
20950Sstevel@tonic-gate struct vtoc *vtocp
20960Sstevel@tonic-gate )
20970Sstevel@tonic-gate {
20980Sstevel@tonic-gate return (md_set_vtoc((md_unit_t *)un, vtocp));
20990Sstevel@tonic-gate }
21000Sstevel@tonic-gate
21010Sstevel@tonic-gate
21027563SPrasad.Singamsetty@Sun.COM /*
21037563SPrasad.Singamsetty@Sun.COM * NAME: raid_get_extvtoc
21047563SPrasad.Singamsetty@Sun.COM * DESCRIPTION: used to get the extended VTOC on a RAID metadevice
21057563SPrasad.Singamsetty@Sun.COM * PARAMETERS: mr_unit_t *un - RAID unit to get the VTOC from
21067563SPrasad.Singamsetty@Sun.COM * struct extvtoc *vtocp - pointer to extended VTOC data structure
21077563SPrasad.Singamsetty@Sun.COM *
21087563SPrasad.Singamsetty@Sun.COM * LOCKS: none
21097563SPrasad.Singamsetty@Sun.COM *
21107563SPrasad.Singamsetty@Sun.COM */
21117563SPrasad.Singamsetty@Sun.COM static int
raid_get_extvtoc(mr_unit_t * un,struct extvtoc * vtocp)21127563SPrasad.Singamsetty@Sun.COM raid_get_extvtoc(
21137563SPrasad.Singamsetty@Sun.COM mr_unit_t *un,
21147563SPrasad.Singamsetty@Sun.COM struct extvtoc *vtocp
21157563SPrasad.Singamsetty@Sun.COM )
21167563SPrasad.Singamsetty@Sun.COM {
21177563SPrasad.Singamsetty@Sun.COM md_get_extvtoc((md_unit_t *)un, vtocp);
21187563SPrasad.Singamsetty@Sun.COM
21197563SPrasad.Singamsetty@Sun.COM return (0);
21207563SPrasad.Singamsetty@Sun.COM }
21217563SPrasad.Singamsetty@Sun.COM
21227563SPrasad.Singamsetty@Sun.COM /*
21237563SPrasad.Singamsetty@Sun.COM * NAME: raid_set_extvtoc
21247563SPrasad.Singamsetty@Sun.COM * DESCRIPTION: used to set the extended VTOC on a RAID metadevice
21257563SPrasad.Singamsetty@Sun.COM * PARAMETERS: mr_unit_t *un - RAID unit to set the VTOC on
21267563SPrasad.Singamsetty@Sun.COM * struct extvtoc *vtocp - pointer to extended VTOC data structure
21277563SPrasad.Singamsetty@Sun.COM *
21287563SPrasad.Singamsetty@Sun.COM * LOCKS: none
21297563SPrasad.Singamsetty@Sun.COM *
21307563SPrasad.Singamsetty@Sun.COM */
21317563SPrasad.Singamsetty@Sun.COM static int
raid_set_extvtoc(mr_unit_t * un,struct extvtoc * vtocp)21327563SPrasad.Singamsetty@Sun.COM raid_set_extvtoc(
21337563SPrasad.Singamsetty@Sun.COM mr_unit_t *un,
21347563SPrasad.Singamsetty@Sun.COM struct extvtoc *vtocp
21357563SPrasad.Singamsetty@Sun.COM )
21367563SPrasad.Singamsetty@Sun.COM {
21377563SPrasad.Singamsetty@Sun.COM return (md_set_extvtoc((md_unit_t *)un, vtocp));
21387563SPrasad.Singamsetty@Sun.COM }
21397563SPrasad.Singamsetty@Sun.COM
21407563SPrasad.Singamsetty@Sun.COM
21410Sstevel@tonic-gate
21420Sstevel@tonic-gate /*
21430Sstevel@tonic-gate * NAME: raid_get_cgapart
21440Sstevel@tonic-gate * DESCRIPTION: used to get the dk_map on a RAID metadevice
21450Sstevel@tonic-gate * PARAMETERS: mr_unit_t *un - RAID unit to set the VTOC on
21460Sstevel@tonic-gate * struct vtoc *dkmapp - pointer to dk_map data structure
21470Sstevel@tonic-gate *
21480Sstevel@tonic-gate * LOCKS: none
21490Sstevel@tonic-gate *
21500Sstevel@tonic-gate */
21510Sstevel@tonic-gate
21520Sstevel@tonic-gate static int
raid_get_cgapart(mr_unit_t * un,struct dk_map * dkmapp)21530Sstevel@tonic-gate raid_get_cgapart(
21540Sstevel@tonic-gate mr_unit_t *un,
21550Sstevel@tonic-gate struct dk_map *dkmapp
21560Sstevel@tonic-gate )
21570Sstevel@tonic-gate {
21580Sstevel@tonic-gate md_get_cgapart((md_unit_t *)un, dkmapp);
21590Sstevel@tonic-gate return (0);
21600Sstevel@tonic-gate }
21610Sstevel@tonic-gate
21620Sstevel@tonic-gate /*
21630Sstevel@tonic-gate * NAME: raid_getdevs
21640Sstevel@tonic-gate * DESCRIPTION: return all devices within a RAID metadevice
21650Sstevel@tonic-gate * PARAMETERS: md_getdevs_params_t *mgdp
21660Sstevel@tonic-gate * - pointer to getdevs IOCTL data structure
21670Sstevel@tonic-gate * int mode - should be FREAD
21680Sstevel@tonic-gate * IOLOCK *lockp - IOCTL read/write lock
21690Sstevel@tonic-gate *
21700Sstevel@tonic-gate * LOCKS: obtains unit reader lock via IOLOCK
21710Sstevel@tonic-gate *
21720Sstevel@tonic-gate */
21730Sstevel@tonic-gate static int
raid_getdevs(void * mgdp,int mode,IOLOCK * lock)21740Sstevel@tonic-gate raid_getdevs(
21750Sstevel@tonic-gate void *mgdp,
21760Sstevel@tonic-gate int mode,
21770Sstevel@tonic-gate IOLOCK *lock
21780Sstevel@tonic-gate )
21790Sstevel@tonic-gate {
21800Sstevel@tonic-gate minor_t mnum;
21810Sstevel@tonic-gate mr_unit_t *un;
21820Sstevel@tonic-gate md_dev64_t *udevs;
21830Sstevel@tonic-gate int i, cnt;
21840Sstevel@tonic-gate md_dev64_t unit_dev;
21850Sstevel@tonic-gate md_getdevs_params_t *mgdph = mgdp;
21860Sstevel@tonic-gate
21870Sstevel@tonic-gate
21880Sstevel@tonic-gate mnum = mgdph->mnum;
21890Sstevel@tonic-gate
21900Sstevel@tonic-gate /* check out unit */
21910Sstevel@tonic-gate mdclrerror(&mgdph->mde);
21920Sstevel@tonic-gate
21937563SPrasad.Singamsetty@Sun.COM if ((un = raid_getun(mnum, &mgdph->mde, RD_LOCK, lock)) == NULL)
21940Sstevel@tonic-gate return (0);
21950Sstevel@tonic-gate
21960Sstevel@tonic-gate udevs = (md_dev64_t *)(uintptr_t)mgdph->devs;
21970Sstevel@tonic-gate
21980Sstevel@tonic-gate for (cnt = 0, i = 0; i < un->un_totalcolumncnt; i++, cnt++) {
21990Sstevel@tonic-gate if (cnt < mgdph->cnt) {
22000Sstevel@tonic-gate unit_dev = un->un_column[i].un_orig_dev;
22010Sstevel@tonic-gate if (md_getmajor(unit_dev) != md_major) {
22020Sstevel@tonic-gate if ((unit_dev = md_xlate_mini_2_targ
22030Sstevel@tonic-gate (unit_dev)) == NODEV64)
22040Sstevel@tonic-gate return (ENODEV);
22050Sstevel@tonic-gate }
22060Sstevel@tonic-gate
22070Sstevel@tonic-gate if (ddi_copyout((caddr_t)&unit_dev,
22087563SPrasad.Singamsetty@Sun.COM (caddr_t)&udevs[cnt], sizeof (*udevs), mode) != 0)
22090Sstevel@tonic-gate return (EFAULT);
22100Sstevel@tonic-gate }
22110Sstevel@tonic-gate if (HOTSPARED(un, i)) {
22120Sstevel@tonic-gate cnt++;
22130Sstevel@tonic-gate if (cnt >= mgdph->cnt)
22140Sstevel@tonic-gate continue;
22150Sstevel@tonic-gate
22160Sstevel@tonic-gate unit_dev = un->un_column[i].un_dev;
22170Sstevel@tonic-gate if (md_getmajor(unit_dev) != md_major) {
22180Sstevel@tonic-gate if ((unit_dev = md_xlate_mini_2_targ
22190Sstevel@tonic-gate (unit_dev)) == NODEV64)
22200Sstevel@tonic-gate return (ENODEV);
22210Sstevel@tonic-gate }
22220Sstevel@tonic-gate
22230Sstevel@tonic-gate if (ddi_copyout((caddr_t)&unit_dev,
22247563SPrasad.Singamsetty@Sun.COM (caddr_t)&udevs[cnt], sizeof (*udevs), mode) != 0)
22250Sstevel@tonic-gate return (EFAULT);
22260Sstevel@tonic-gate }
22270Sstevel@tonic-gate }
22280Sstevel@tonic-gate mgdph->cnt = cnt;
22290Sstevel@tonic-gate return (0);
22300Sstevel@tonic-gate }
22310Sstevel@tonic-gate
22320Sstevel@tonic-gate /*
22330Sstevel@tonic-gate * NAME: raid_change
22340Sstevel@tonic-gate * DESCRIPTION: used to change the following dynamic values:
22350Sstevel@tonic-gate * the hot spare pool
22360Sstevel@tonic-gate * in the unit structure of a RAID metadevice
22370Sstevel@tonic-gate * PARAMETERS: md_change_params_t *mcp - pointer to change data structure
22380Sstevel@tonic-gate * IOLOCK *lock - pointer to IOCTL lock
22390Sstevel@tonic-gate *
22400Sstevel@tonic-gate * LOCKS: obtains unit writer lock via IOLOCK (through raid_getun)
22410Sstevel@tonic-gate *
22420Sstevel@tonic-gate */
22430Sstevel@tonic-gate static int
raid_change(md_raid_params_t * mrp,IOLOCK * lock)22440Sstevel@tonic-gate raid_change(
22450Sstevel@tonic-gate md_raid_params_t *mrp,
22460Sstevel@tonic-gate IOLOCK *lock
22470Sstevel@tonic-gate )
22480Sstevel@tonic-gate {
22490Sstevel@tonic-gate minor_t mnum = mrp->mnum;
22500Sstevel@tonic-gate mr_unit_t *un;
22510Sstevel@tonic-gate int ix;
22520Sstevel@tonic-gate mddb_recid_t recids[3] = {0, 0, 0};
22530Sstevel@tonic-gate int err;
22540Sstevel@tonic-gate int irecid;
22550Sstevel@tonic-gate int inc_new_hsp = 0;
22560Sstevel@tonic-gate
22570Sstevel@tonic-gate mdclrerror(&mrp->mde);
22580Sstevel@tonic-gate
22590Sstevel@tonic-gate if ((un = raid_getun(mnum, &mrp->mde, WR_LOCK, lock)) == NULL)
22600Sstevel@tonic-gate return (0);
22610Sstevel@tonic-gate
22620Sstevel@tonic-gate if (!mrp->params.change_hsp_id)
22630Sstevel@tonic-gate return (0);
22640Sstevel@tonic-gate
22650Sstevel@tonic-gate /* verify that no hotspare is in use */
22660Sstevel@tonic-gate for (ix = 0; ix < un->un_totalcolumncnt; ix++) {
22670Sstevel@tonic-gate if (HOTSPARED(un, ix)) {
22680Sstevel@tonic-gate return (mdmderror(&mrp->mde, MDE_HS_IN_USE, mnum));
22690Sstevel@tonic-gate }
22700Sstevel@tonic-gate }
22710Sstevel@tonic-gate
22720Sstevel@tonic-gate /* replace the hot spare pool */
22730Sstevel@tonic-gate
22740Sstevel@tonic-gate irecid = 0;
22750Sstevel@tonic-gate if (mrp->params.hsp_id != -1) {
22760Sstevel@tonic-gate /* increment the reference count of the new hsp */
22770Sstevel@tonic-gate err = md_hot_spare_ifc(HSP_INCREF, mrp->params.hsp_id, 0, 0,
22780Sstevel@tonic-gate &recids[0], NULL, NULL, NULL);
22790Sstevel@tonic-gate if (err) {
22800Sstevel@tonic-gate return (mdhsperror(&mrp->mde, MDE_INVAL_HSP,
22810Sstevel@tonic-gate mrp->params.hsp_id));
22820Sstevel@tonic-gate }
22830Sstevel@tonic-gate inc_new_hsp = 1;
22840Sstevel@tonic-gate irecid++;
22850Sstevel@tonic-gate }
22860Sstevel@tonic-gate
22870Sstevel@tonic-gate if (un->un_hsp_id != -1) {
22880Sstevel@tonic-gate /* decrement the reference count of the old hsp */
22890Sstevel@tonic-gate err = md_hot_spare_ifc(HSP_DECREF, un->un_hsp_id, 0, 0,
22900Sstevel@tonic-gate &recids[irecid], NULL, NULL, NULL);
22910Sstevel@tonic-gate if (err) {
22920Sstevel@tonic-gate err = mdhsperror(&mrp->mde, MDE_INVAL_HSP,
22930Sstevel@tonic-gate mrp->params.hsp_id);
22940Sstevel@tonic-gate if (inc_new_hsp) {
22950Sstevel@tonic-gate (void) md_hot_spare_ifc(HSP_DECREF,
22960Sstevel@tonic-gate mrp->params.hsp_id, 0, 0,
22970Sstevel@tonic-gate &recids[0], NULL, NULL, NULL);
22980Sstevel@tonic-gate /*
22990Sstevel@tonic-gate * Don't need to commit the record,
23000Sstevel@tonic-gate * because it wasn't committed before
23010Sstevel@tonic-gate */
23020Sstevel@tonic-gate }
23030Sstevel@tonic-gate return (err);
23040Sstevel@tonic-gate }
23050Sstevel@tonic-gate }
23060Sstevel@tonic-gate
23070Sstevel@tonic-gate un->un_hsp_id = mrp->params.hsp_id;
23080Sstevel@tonic-gate
23090Sstevel@tonic-gate raid_commit(un, recids);
23100Sstevel@tonic-gate SE_NOTIFY(EC_SVM_STATE, ESC_SVM_CHANGE, SVM_TAG_METADEVICE,
23110Sstevel@tonic-gate MD_UN2SET(un), MD_SID(un));
23120Sstevel@tonic-gate
23130Sstevel@tonic-gate /* Now trigger hot spare processing in case one is needed. */
23140Sstevel@tonic-gate if ((un->un_hsp_id != -1) && (un->un_state == RUS_ERRED))
23150Sstevel@tonic-gate (void) raid_hotspares();
23160Sstevel@tonic-gate
23170Sstevel@tonic-gate return (0);
23180Sstevel@tonic-gate }
23190Sstevel@tonic-gate
23200Sstevel@tonic-gate /*
23210Sstevel@tonic-gate * NAME: raid_admin_ioctl
23220Sstevel@tonic-gate * DESCRIPTION: IOCTL operations unique to metadevices and RAID
23230Sstevel@tonic-gate * PARAMETERS: int cmd - IOCTL command to be executed
23240Sstevel@tonic-gate * void *data - pointer to IOCTL data structure
23250Sstevel@tonic-gate * int mode - either FREAD or FWRITE
23260Sstevel@tonic-gate * IOLOCK *lockp - IOCTL read/write lock
23270Sstevel@tonic-gate *
23280Sstevel@tonic-gate * LOCKS: none
23290Sstevel@tonic-gate *
23300Sstevel@tonic-gate */
23310Sstevel@tonic-gate static int
raid_admin_ioctl(int cmd,void * data,int mode,IOLOCK * lockp)23320Sstevel@tonic-gate raid_admin_ioctl(
23330Sstevel@tonic-gate int cmd,
23340Sstevel@tonic-gate void *data,
23350Sstevel@tonic-gate int mode,
23360Sstevel@tonic-gate IOLOCK *lockp
23370Sstevel@tonic-gate )
23380Sstevel@tonic-gate {
23390Sstevel@tonic-gate size_t sz = 0;
23400Sstevel@tonic-gate void *d = NULL;
23410Sstevel@tonic-gate int err = 0;
23420Sstevel@tonic-gate
23430Sstevel@tonic-gate /* We can only handle 32-bit clients for internal commands */
23440Sstevel@tonic-gate if ((mode & DATAMODEL_MASK) != DATAMODEL_ILP32) {
23450Sstevel@tonic-gate return (EINVAL);
23460Sstevel@tonic-gate }
23470Sstevel@tonic-gate
23480Sstevel@tonic-gate
23490Sstevel@tonic-gate /* dispatch ioctl */
23500Sstevel@tonic-gate switch (cmd) {
23510Sstevel@tonic-gate
23520Sstevel@tonic-gate case MD_IOCSET:
23530Sstevel@tonic-gate {
23540Sstevel@tonic-gate if (! (mode & FWRITE))
23550Sstevel@tonic-gate return (EACCES);
23560Sstevel@tonic-gate
23570Sstevel@tonic-gate sz = sizeof (md_set_params_t);
23580Sstevel@tonic-gate d = kmem_alloc(sz, KM_SLEEP);
23590Sstevel@tonic-gate
23600Sstevel@tonic-gate if (ddi_copyin(data, d, sz, mode)) {
23610Sstevel@tonic-gate err = EFAULT;
23620Sstevel@tonic-gate break;
23630Sstevel@tonic-gate }
23640Sstevel@tonic-gate
23650Sstevel@tonic-gate err = raid_set(d, mode);
23660Sstevel@tonic-gate break;
23670Sstevel@tonic-gate }
23680Sstevel@tonic-gate
23690Sstevel@tonic-gate case MD_IOCGET:
23700Sstevel@tonic-gate {
23710Sstevel@tonic-gate if (! (mode & FREAD))
23720Sstevel@tonic-gate return (EACCES);
23730Sstevel@tonic-gate
23740Sstevel@tonic-gate sz = sizeof (md_i_get_t);
23750Sstevel@tonic-gate d = kmem_alloc(sz, KM_SLEEP);
23760Sstevel@tonic-gate
23770Sstevel@tonic-gate if (ddi_copyin(data, d, sz, mode)) {
23780Sstevel@tonic-gate err = EFAULT;
23790Sstevel@tonic-gate break;
23800Sstevel@tonic-gate }
23810Sstevel@tonic-gate
23820Sstevel@tonic-gate err = raid_get(d, mode, lockp);
23830Sstevel@tonic-gate break;
23840Sstevel@tonic-gate }
23850Sstevel@tonic-gate
23860Sstevel@tonic-gate case MD_IOCREPLACE:
23870Sstevel@tonic-gate {
23880Sstevel@tonic-gate if (! (mode & FWRITE))
23890Sstevel@tonic-gate return (EACCES);
23900Sstevel@tonic-gate
23910Sstevel@tonic-gate sz = sizeof (replace_params_t);
23920Sstevel@tonic-gate d = kmem_alloc(sz, KM_SLEEP);
23930Sstevel@tonic-gate
23940Sstevel@tonic-gate if (ddi_copyin(data, d, sz, mode)) {
23950Sstevel@tonic-gate err = EFAULT;
23960Sstevel@tonic-gate break;
23970Sstevel@tonic-gate }
23980Sstevel@tonic-gate
23990Sstevel@tonic-gate err = raid_replace((replace_params_t *)d, lockp);
24000Sstevel@tonic-gate break;
24010Sstevel@tonic-gate }
24020Sstevel@tonic-gate
24030Sstevel@tonic-gate case MD_IOCSETSYNC:
24040Sstevel@tonic-gate {
24050Sstevel@tonic-gate if (! (mode & FWRITE))
24060Sstevel@tonic-gate return (EACCES);
24070Sstevel@tonic-gate
24080Sstevel@tonic-gate sz = sizeof (md_resync_ioctl_t);
24090Sstevel@tonic-gate d = kmem_alloc(sz, KM_SLEEP);
24100Sstevel@tonic-gate
24110Sstevel@tonic-gate if (ddi_copyin(data, d, sz, mode)) {
24120Sstevel@tonic-gate err = EFAULT;
24130Sstevel@tonic-gate break;
24140Sstevel@tonic-gate }
24150Sstevel@tonic-gate
24160Sstevel@tonic-gate err = raid_set_sync((md_resync_ioctl_t *)d, lockp);
24170Sstevel@tonic-gate break;
24180Sstevel@tonic-gate }
24190Sstevel@tonic-gate
24200Sstevel@tonic-gate case MD_IOCGETSYNC:
24210Sstevel@tonic-gate {
24220Sstevel@tonic-gate if (! (mode & FREAD))
24230Sstevel@tonic-gate return (EACCES);
24240Sstevel@tonic-gate
24250Sstevel@tonic-gate sz = sizeof (md_resync_ioctl_t);
24260Sstevel@tonic-gate d = kmem_alloc(sz, KM_SLEEP);
24270Sstevel@tonic-gate
24280Sstevel@tonic-gate if (ddi_copyin(data, d, sz, mode)) {
24290Sstevel@tonic-gate err = EFAULT;
24300Sstevel@tonic-gate break;
24310Sstevel@tonic-gate }
24320Sstevel@tonic-gate err = raid_get_resync((md_resync_ioctl_t *)d, lockp);
24330Sstevel@tonic-gate
24340Sstevel@tonic-gate break;
24350Sstevel@tonic-gate }
24360Sstevel@tonic-gate
24370Sstevel@tonic-gate case MD_IOCGROW:
24380Sstevel@tonic-gate {
24390Sstevel@tonic-gate if (! (mode & FWRITE))
24400Sstevel@tonic-gate return (EACCES);
24410Sstevel@tonic-gate
24420Sstevel@tonic-gate sz = sizeof (md_grow_params_t);
24430Sstevel@tonic-gate d = kmem_alloc(sz, KM_SLEEP);
24440Sstevel@tonic-gate
24450Sstevel@tonic-gate if (ddi_copyin(data, d, sz, mode)) {
24460Sstevel@tonic-gate err = EFAULT;
24470Sstevel@tonic-gate break;
24480Sstevel@tonic-gate }
24490Sstevel@tonic-gate
24500Sstevel@tonic-gate err = raid_grow(d, mode, lockp);
24510Sstevel@tonic-gate break;
24520Sstevel@tonic-gate }
24530Sstevel@tonic-gate
24540Sstevel@tonic-gate case MD_IOCCHANGE:
24550Sstevel@tonic-gate {
24560Sstevel@tonic-gate if (! (mode & FWRITE))
24570Sstevel@tonic-gate return (EACCES);
24580Sstevel@tonic-gate
24590Sstevel@tonic-gate sz = sizeof (md_raid_params_t);
24600Sstevel@tonic-gate d = kmem_alloc(sz, KM_SLEEP);
24610Sstevel@tonic-gate
24620Sstevel@tonic-gate if (ddi_copyin(data, d, sz, mode)) {
24630Sstevel@tonic-gate err = EFAULT;
24640Sstevel@tonic-gate break;
24650Sstevel@tonic-gate }
24660Sstevel@tonic-gate
24670Sstevel@tonic-gate err = raid_change((md_raid_params_t *)d, lockp);
24680Sstevel@tonic-gate break;
24690Sstevel@tonic-gate }
24700Sstevel@tonic-gate
24710Sstevel@tonic-gate case MD_IOCRESET:
24720Sstevel@tonic-gate {
24730Sstevel@tonic-gate if (! (mode & FWRITE))
24740Sstevel@tonic-gate return (EACCES);
24750Sstevel@tonic-gate
24760Sstevel@tonic-gate sz = sizeof (md_i_reset_t);
24770Sstevel@tonic-gate d = kmem_alloc(sz, KM_SLEEP);
24780Sstevel@tonic-gate
24790Sstevel@tonic-gate if (ddi_copyin(data, d, sz, mode)) {
24800Sstevel@tonic-gate err = EFAULT;
24810Sstevel@tonic-gate break;
24820Sstevel@tonic-gate }
24830Sstevel@tonic-gate
24840Sstevel@tonic-gate err = raid_reset((md_i_reset_t *)d);
24850Sstevel@tonic-gate break;
24860Sstevel@tonic-gate }
24870Sstevel@tonic-gate
24880Sstevel@tonic-gate case MD_IOCGET_DEVS:
24890Sstevel@tonic-gate {
24900Sstevel@tonic-gate if (! (mode & FREAD))
24910Sstevel@tonic-gate return (EACCES);
24920Sstevel@tonic-gate
24930Sstevel@tonic-gate sz = sizeof (md_getdevs_params_t);
24940Sstevel@tonic-gate d = kmem_alloc(sz, KM_SLEEP);
24950Sstevel@tonic-gate
24960Sstevel@tonic-gate if (ddi_copyin(data, d, sz, mode)) {
24970Sstevel@tonic-gate err = EFAULT;
24980Sstevel@tonic-gate break;
24990Sstevel@tonic-gate }
25000Sstevel@tonic-gate
25010Sstevel@tonic-gate err = raid_getdevs(d, mode, lockp);
25020Sstevel@tonic-gate break;
25030Sstevel@tonic-gate }
25040Sstevel@tonic-gate
25050Sstevel@tonic-gate case MD_IOCSETREGEN:
25060Sstevel@tonic-gate {
25070Sstevel@tonic-gate if (! (mode & FWRITE))
25080Sstevel@tonic-gate return (EACCES);
25090Sstevel@tonic-gate
25100Sstevel@tonic-gate sz = sizeof (md_regen_param_t);
25110Sstevel@tonic-gate d = kmem_alloc(sz, KM_SLEEP);
25120Sstevel@tonic-gate
25130Sstevel@tonic-gate if (ddi_copyin(data, d, sz, mode)) {
25140Sstevel@tonic-gate err = EFAULT;
25150Sstevel@tonic-gate break;
25160Sstevel@tonic-gate }
25170Sstevel@tonic-gate
25180Sstevel@tonic-gate err = raid_regen((md_regen_param_t *)d, lockp);
25190Sstevel@tonic-gate break;
25200Sstevel@tonic-gate }
25210Sstevel@tonic-gate
25220Sstevel@tonic-gate case MD_IOCPROBE_DEV:
25230Sstevel@tonic-gate {
25240Sstevel@tonic-gate md_probedev_impl_t *p = NULL;
25250Sstevel@tonic-gate md_probedev_t *ph = NULL;
25260Sstevel@tonic-gate daemon_queue_t *hdr = NULL;
25270Sstevel@tonic-gate int i;
25280Sstevel@tonic-gate size_t sz1 = 0;
25290Sstevel@tonic-gate
25300Sstevel@tonic-gate
25310Sstevel@tonic-gate if (! (mode & FREAD))
25320Sstevel@tonic-gate return (EACCES);
25330Sstevel@tonic-gate
25340Sstevel@tonic-gate sz = sizeof (md_probedev_t);
25350Sstevel@tonic-gate
25360Sstevel@tonic-gate d = kmem_alloc(sz, KM_SLEEP);
25370Sstevel@tonic-gate
25380Sstevel@tonic-gate /* now copy in the data */
25390Sstevel@tonic-gate if (ddi_copyin(data, d, sz, mode)) {
25400Sstevel@tonic-gate err = EFAULT;
25410Sstevel@tonic-gate goto free_mem;
25420Sstevel@tonic-gate }
25430Sstevel@tonic-gate
25440Sstevel@tonic-gate /*
25450Sstevel@tonic-gate * Sanity test the args. Test name should have the keyword
25460Sstevel@tonic-gate * probe.
25470Sstevel@tonic-gate */
25480Sstevel@tonic-gate p = kmem_alloc(sizeof (md_probedev_impl_t), KM_SLEEP);
25490Sstevel@tonic-gate p->probe_sema = NULL;
25500Sstevel@tonic-gate p->probe_mx = NULL;
25510Sstevel@tonic-gate p->probe.mnum_list = (uint64_t)NULL;
25520Sstevel@tonic-gate
25530Sstevel@tonic-gate ph = (md_probedev_t *)d;
25540Sstevel@tonic-gate p->probe.nmdevs = ph->nmdevs;
25550Sstevel@tonic-gate (void) strcpy(p->probe.test_name, ph->test_name);
25560Sstevel@tonic-gate bcopy(&ph->md_driver, &(p->probe.md_driver),
25577563SPrasad.Singamsetty@Sun.COM sizeof (md_driver_t));
25580Sstevel@tonic-gate
25590Sstevel@tonic-gate if ((p->probe.nmdevs < 1) ||
25607563SPrasad.Singamsetty@Sun.COM (strstr(p->probe.test_name, "probe") == NULL)) {
25610Sstevel@tonic-gate err = EINVAL;
25620Sstevel@tonic-gate goto free_mem;
25630Sstevel@tonic-gate }
25640Sstevel@tonic-gate
25650Sstevel@tonic-gate sz1 = sizeof (minor_t) * p->probe.nmdevs;
25660Sstevel@tonic-gate
25670Sstevel@tonic-gate p->probe.mnum_list = (uint64_t)(uintptr_t)kmem_alloc(sz1,
25680Sstevel@tonic-gate KM_SLEEP);
25690Sstevel@tonic-gate
25700Sstevel@tonic-gate if (ddi_copyin((caddr_t)(uintptr_t)ph->mnum_list,
25710Sstevel@tonic-gate (caddr_t)(uintptr_t)p->probe.mnum_list, sz1, mode)) {
25720Sstevel@tonic-gate err = EFAULT;
25730Sstevel@tonic-gate goto free_mem;
25740Sstevel@tonic-gate }
25750Sstevel@tonic-gate
25760Sstevel@tonic-gate if (err = md_init_probereq(p, &hdr))
25770Sstevel@tonic-gate goto free_mem;
25780Sstevel@tonic-gate
25790Sstevel@tonic-gate /*
25800Sstevel@tonic-gate * put the request on the queue and wait.
25810Sstevel@tonic-gate */
25820Sstevel@tonic-gate
25830Sstevel@tonic-gate daemon_request_new(&md_ff_daemonq, md_probe_one, hdr, REQ_NEW);
25840Sstevel@tonic-gate
25850Sstevel@tonic-gate (void) IOLOCK_RETURN(0, lockp);
25860Sstevel@tonic-gate /* wait for the events to occur */
25870Sstevel@tonic-gate for (i = 0; i < p->probe.nmdevs; i++) {
25880Sstevel@tonic-gate sema_p(PROBE_SEMA(p));
25890Sstevel@tonic-gate }
25907563SPrasad.Singamsetty@Sun.COM while (md_ioctl_lock_enter() == EINTR)
25917563SPrasad.Singamsetty@Sun.COM ;
25920Sstevel@tonic-gate
25930Sstevel@tonic-gate /*
25940Sstevel@tonic-gate * clean up. The hdr list is freed in the probe routines
25950Sstevel@tonic-gate * since the list is NULL by the time we get here.
25960Sstevel@tonic-gate */
25970Sstevel@tonic-gate free_mem:
25980Sstevel@tonic-gate if (p) {
25990Sstevel@tonic-gate if (p->probe_sema != NULL) {
26000Sstevel@tonic-gate sema_destroy(PROBE_SEMA(p));
26010Sstevel@tonic-gate kmem_free(p->probe_sema, sizeof (ksema_t));
26020Sstevel@tonic-gate }
26030Sstevel@tonic-gate if (p->probe_mx != NULL) {
26040Sstevel@tonic-gate mutex_destroy(PROBE_MX(p));
26050Sstevel@tonic-gate kmem_free(p->probe_mx, sizeof (kmutex_t));
26060Sstevel@tonic-gate }
26070Sstevel@tonic-gate if (p->probe.mnum_list)
26080Sstevel@tonic-gate kmem_free((caddr_t)(uintptr_t)
26090Sstevel@tonic-gate p->probe.mnum_list, sz1);
26100Sstevel@tonic-gate
26110Sstevel@tonic-gate kmem_free(p, sizeof (md_probedev_impl_t));
26120Sstevel@tonic-gate }
26130Sstevel@tonic-gate break;
26140Sstevel@tonic-gate }
26150Sstevel@tonic-gate
26160Sstevel@tonic-gate default:
26170Sstevel@tonic-gate return (ENOTTY);
26180Sstevel@tonic-gate }
26190Sstevel@tonic-gate
26200Sstevel@tonic-gate /*
26210Sstevel@tonic-gate * copyout and free any args
26220Sstevel@tonic-gate */
26230Sstevel@tonic-gate if (sz != 0) {
26240Sstevel@tonic-gate if (err == 0) {
26250Sstevel@tonic-gate if (ddi_copyout(d, data, sz, mode) != 0) {
26260Sstevel@tonic-gate err = EFAULT;
26270Sstevel@tonic-gate }
26280Sstevel@tonic-gate }
26290Sstevel@tonic-gate kmem_free(d, sz);
26300Sstevel@tonic-gate }
26310Sstevel@tonic-gate return (err);
26320Sstevel@tonic-gate }
26330Sstevel@tonic-gate
26340Sstevel@tonic-gate /*
26350Sstevel@tonic-gate * NAME: md_raid_ioctl
26360Sstevel@tonic-gate * DESCRIPTION: RAID metadevice IOCTL operations entry point.
26370Sstevel@tonic-gate * PARAMETERS: md_dev64_t dev - RAID device identifier
26380Sstevel@tonic-gate * int cmd - IOCTL command to be executed
26390Sstevel@tonic-gate * void *data - pointer to IOCTL data structure
26400Sstevel@tonic-gate * int mode - either FREAD or FWRITE
26410Sstevel@tonic-gate * IOLOCK *lockp - IOCTL read/write lock
26420Sstevel@tonic-gate *
26430Sstevel@tonic-gate * LOCKS: none
26440Sstevel@tonic-gate *
26450Sstevel@tonic-gate */
26460Sstevel@tonic-gate int
md_raid_ioctl(dev_t dev,int cmd,void * data,int mode,IOLOCK * lockp)26470Sstevel@tonic-gate md_raid_ioctl(
26480Sstevel@tonic-gate dev_t dev,
26490Sstevel@tonic-gate int cmd,
26500Sstevel@tonic-gate void *data,
26510Sstevel@tonic-gate int mode,
26520Sstevel@tonic-gate IOLOCK *lockp
26530Sstevel@tonic-gate )
26540Sstevel@tonic-gate {
26550Sstevel@tonic-gate minor_t mnum = getminor(dev);
26560Sstevel@tonic-gate mr_unit_t *un;
26570Sstevel@tonic-gate int err = 0;
26580Sstevel@tonic-gate
26590Sstevel@tonic-gate /* handle admin ioctls */
26600Sstevel@tonic-gate if (mnum == MD_ADM_MINOR)
26610Sstevel@tonic-gate return (raid_admin_ioctl(cmd, data, mode, lockp));
26620Sstevel@tonic-gate
26630Sstevel@tonic-gate /* check unit */
26640Sstevel@tonic-gate if ((MD_MIN2SET(mnum) >= md_nsets) ||
26650Sstevel@tonic-gate (MD_MIN2UNIT(mnum) >= md_nunits) ||
26660Sstevel@tonic-gate ((un = MD_UNIT(mnum)) == NULL))
26670Sstevel@tonic-gate return (ENXIO);
26680Sstevel@tonic-gate
26690Sstevel@tonic-gate /* is this a supported ioctl? */
26707563SPrasad.Singamsetty@Sun.COM err = md_check_ioctl_against_unit(cmd, un->c);
26710Sstevel@tonic-gate if (err != 0) {
26720Sstevel@tonic-gate return (err);
26730Sstevel@tonic-gate }
26740Sstevel@tonic-gate
26750Sstevel@tonic-gate /* dispatch ioctl */
26760Sstevel@tonic-gate switch (cmd) {
26770Sstevel@tonic-gate
26780Sstevel@tonic-gate case DKIOCINFO:
26790Sstevel@tonic-gate {
26800Sstevel@tonic-gate struct dk_cinfo *p;
26810Sstevel@tonic-gate
26820Sstevel@tonic-gate if (! (mode & FREAD))
26830Sstevel@tonic-gate return (EACCES);
26840Sstevel@tonic-gate
26850Sstevel@tonic-gate p = kmem_alloc(sizeof (*p), KM_SLEEP);
26860Sstevel@tonic-gate
26870Sstevel@tonic-gate get_info(p, mnum);
26880Sstevel@tonic-gate if (ddi_copyout((caddr_t)p, data, sizeof (*p), mode) != 0)
26890Sstevel@tonic-gate err = EFAULT;
26900Sstevel@tonic-gate
26910Sstevel@tonic-gate kmem_free(p, sizeof (*p));
26920Sstevel@tonic-gate return (err);
26930Sstevel@tonic-gate }
26940Sstevel@tonic-gate
26950Sstevel@tonic-gate case DKIOCGMEDIAINFO:
26960Sstevel@tonic-gate {
26970Sstevel@tonic-gate struct dk_minfo p;
26980Sstevel@tonic-gate
26990Sstevel@tonic-gate if (! (mode & FREAD))
27000Sstevel@tonic-gate return (EACCES);
27010Sstevel@tonic-gate
27020Sstevel@tonic-gate get_minfo(&p, mnum);
27030Sstevel@tonic-gate if (ddi_copyout(&p, data, sizeof (struct dk_minfo), mode) != 0)
27040Sstevel@tonic-gate err = EFAULT;
27050Sstevel@tonic-gate
27060Sstevel@tonic-gate return (err);
27070Sstevel@tonic-gate }
27080Sstevel@tonic-gate
27090Sstevel@tonic-gate case DKIOCGGEOM:
27100Sstevel@tonic-gate {
27110Sstevel@tonic-gate struct dk_geom *p;
27120Sstevel@tonic-gate
27130Sstevel@tonic-gate if (! (mode & FREAD))
27140Sstevel@tonic-gate return (EACCES);
27150Sstevel@tonic-gate
27160Sstevel@tonic-gate p = kmem_alloc(sizeof (*p), KM_SLEEP);
27170Sstevel@tonic-gate
27180Sstevel@tonic-gate if ((err = raid_get_geom(un, p)) == 0) {
27190Sstevel@tonic-gate if (ddi_copyout((caddr_t)p, data, sizeof (*p),
27200Sstevel@tonic-gate mode) != 0)
27210Sstevel@tonic-gate err = EFAULT;
27220Sstevel@tonic-gate }
27230Sstevel@tonic-gate
27240Sstevel@tonic-gate kmem_free(p, sizeof (*p));
27250Sstevel@tonic-gate return (err);
27260Sstevel@tonic-gate }
27270Sstevel@tonic-gate
27280Sstevel@tonic-gate case DKIOCGVTOC:
27290Sstevel@tonic-gate {
2730*9017SJohn.Wren.Kennedy@Sun.COM struct vtoc *vtoc;
27310Sstevel@tonic-gate
27320Sstevel@tonic-gate if (! (mode & FREAD))
27330Sstevel@tonic-gate return (EACCES);
27340Sstevel@tonic-gate
2735*9017SJohn.Wren.Kennedy@Sun.COM vtoc = kmem_zalloc(sizeof (*vtoc), KM_SLEEP);
2736*9017SJohn.Wren.Kennedy@Sun.COM if ((err = raid_get_vtoc(un, vtoc)) != 0) {
2737*9017SJohn.Wren.Kennedy@Sun.COM kmem_free(vtoc, sizeof (*vtoc));
27380Sstevel@tonic-gate return (err);
27390Sstevel@tonic-gate }
27400Sstevel@tonic-gate
27410Sstevel@tonic-gate if ((mode & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
2742*9017SJohn.Wren.Kennedy@Sun.COM if (ddi_copyout(vtoc, data, sizeof (*vtoc), mode))
27430Sstevel@tonic-gate err = EFAULT;
27440Sstevel@tonic-gate }
27450Sstevel@tonic-gate #ifdef _SYSCALL32
27460Sstevel@tonic-gate else {
2747*9017SJohn.Wren.Kennedy@Sun.COM struct vtoc32 *vtoc32;
2748*9017SJohn.Wren.Kennedy@Sun.COM
2749*9017SJohn.Wren.Kennedy@Sun.COM vtoc32 = kmem_zalloc(sizeof (*vtoc32), KM_SLEEP);
2750*9017SJohn.Wren.Kennedy@Sun.COM
2751*9017SJohn.Wren.Kennedy@Sun.COM vtoctovtoc32((*vtoc), (*vtoc32));
2752*9017SJohn.Wren.Kennedy@Sun.COM if (ddi_copyout(vtoc32, data, sizeof (*vtoc32), mode))
27530Sstevel@tonic-gate err = EFAULT;
2754*9017SJohn.Wren.Kennedy@Sun.COM kmem_free(vtoc32, sizeof (*vtoc32));
27550Sstevel@tonic-gate }
27560Sstevel@tonic-gate #endif /* _SYSCALL32 */
27570Sstevel@tonic-gate
2758*9017SJohn.Wren.Kennedy@Sun.COM kmem_free(vtoc, sizeof (*vtoc));
27590Sstevel@tonic-gate return (err);
27600Sstevel@tonic-gate }
27610Sstevel@tonic-gate
27620Sstevel@tonic-gate case DKIOCSVTOC:
27630Sstevel@tonic-gate {
2764*9017SJohn.Wren.Kennedy@Sun.COM struct vtoc *vtoc;
27650Sstevel@tonic-gate
27660Sstevel@tonic-gate if (! (mode & FWRITE))
27670Sstevel@tonic-gate return (EACCES);
27680Sstevel@tonic-gate
2769*9017SJohn.Wren.Kennedy@Sun.COM vtoc = kmem_zalloc(sizeof (*vtoc), KM_SLEEP);
27700Sstevel@tonic-gate if ((mode & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
2771*9017SJohn.Wren.Kennedy@Sun.COM if (ddi_copyin(data, vtoc, sizeof (*vtoc), mode)) {
27720Sstevel@tonic-gate err = EFAULT;
27730Sstevel@tonic-gate }
27740Sstevel@tonic-gate }
27750Sstevel@tonic-gate #ifdef _SYSCALL32
27760Sstevel@tonic-gate else {
2777*9017SJohn.Wren.Kennedy@Sun.COM struct vtoc32 *vtoc32;
2778*9017SJohn.Wren.Kennedy@Sun.COM
2779*9017SJohn.Wren.Kennedy@Sun.COM vtoc32 = kmem_zalloc(sizeof (*vtoc32), KM_SLEEP);
2780*9017SJohn.Wren.Kennedy@Sun.COM
2781*9017SJohn.Wren.Kennedy@Sun.COM if (ddi_copyin(data, vtoc32, sizeof (*vtoc32), mode)) {
27820Sstevel@tonic-gate err = EFAULT;
27830Sstevel@tonic-gate } else {
2784*9017SJohn.Wren.Kennedy@Sun.COM vtoc32tovtoc((*vtoc32), (*vtoc));
27850Sstevel@tonic-gate }
2786*9017SJohn.Wren.Kennedy@Sun.COM kmem_free(vtoc32, sizeof (*vtoc32));
27870Sstevel@tonic-gate }
27880Sstevel@tonic-gate #endif /* _SYSCALL32 */
27890Sstevel@tonic-gate
27900Sstevel@tonic-gate if (err == 0)
2791*9017SJohn.Wren.Kennedy@Sun.COM err = raid_set_vtoc(un, vtoc);
2792*9017SJohn.Wren.Kennedy@Sun.COM
2793*9017SJohn.Wren.Kennedy@Sun.COM kmem_free(vtoc, sizeof (*vtoc));
27940Sstevel@tonic-gate return (err);
27950Sstevel@tonic-gate }
27960Sstevel@tonic-gate
27977563SPrasad.Singamsetty@Sun.COM case DKIOCGEXTVTOC:
27987563SPrasad.Singamsetty@Sun.COM {
2799*9017SJohn.Wren.Kennedy@Sun.COM struct extvtoc *extvtoc;
28007563SPrasad.Singamsetty@Sun.COM
28017563SPrasad.Singamsetty@Sun.COM if (! (mode & FREAD))
28027563SPrasad.Singamsetty@Sun.COM return (EACCES);
28037563SPrasad.Singamsetty@Sun.COM
2804*9017SJohn.Wren.Kennedy@Sun.COM extvtoc = kmem_zalloc(sizeof (*extvtoc), KM_SLEEP);
2805*9017SJohn.Wren.Kennedy@Sun.COM if ((err = raid_get_extvtoc(un, extvtoc)) != 0) {
2806*9017SJohn.Wren.Kennedy@Sun.COM kmem_free(extvtoc, sizeof (*extvtoc));
28077563SPrasad.Singamsetty@Sun.COM return (err);
28087563SPrasad.Singamsetty@Sun.COM }
28097563SPrasad.Singamsetty@Sun.COM
2810*9017SJohn.Wren.Kennedy@Sun.COM if (ddi_copyout(extvtoc, data, sizeof (*extvtoc), mode))
28117563SPrasad.Singamsetty@Sun.COM err = EFAULT;
28127563SPrasad.Singamsetty@Sun.COM
2813*9017SJohn.Wren.Kennedy@Sun.COM kmem_free(extvtoc, sizeof (*extvtoc));
28147563SPrasad.Singamsetty@Sun.COM return (err);
28157563SPrasad.Singamsetty@Sun.COM }
28167563SPrasad.Singamsetty@Sun.COM
28177563SPrasad.Singamsetty@Sun.COM case DKIOCSEXTVTOC:
28187563SPrasad.Singamsetty@Sun.COM {
2819*9017SJohn.Wren.Kennedy@Sun.COM struct extvtoc *extvtoc;
28207563SPrasad.Singamsetty@Sun.COM
28217563SPrasad.Singamsetty@Sun.COM if (! (mode & FWRITE))
28227563SPrasad.Singamsetty@Sun.COM return (EACCES);
28237563SPrasad.Singamsetty@Sun.COM
2824*9017SJohn.Wren.Kennedy@Sun.COM extvtoc = kmem_zalloc(sizeof (*extvtoc), KM_SLEEP);
2825*9017SJohn.Wren.Kennedy@Sun.COM if (ddi_copyin(data, extvtoc, sizeof (*extvtoc), mode)) {
28267563SPrasad.Singamsetty@Sun.COM err = EFAULT;
28277563SPrasad.Singamsetty@Sun.COM }
28287563SPrasad.Singamsetty@Sun.COM
28297563SPrasad.Singamsetty@Sun.COM if (err == 0)
2830*9017SJohn.Wren.Kennedy@Sun.COM err = raid_set_extvtoc(un, extvtoc);
2831*9017SJohn.Wren.Kennedy@Sun.COM
2832*9017SJohn.Wren.Kennedy@Sun.COM kmem_free(extvtoc, sizeof (*extvtoc));
28337563SPrasad.Singamsetty@Sun.COM return (err);
28347563SPrasad.Singamsetty@Sun.COM }
28357563SPrasad.Singamsetty@Sun.COM
28360Sstevel@tonic-gate case DKIOCGAPART:
28370Sstevel@tonic-gate {
28380Sstevel@tonic-gate struct dk_map dmp;
28390Sstevel@tonic-gate
28400Sstevel@tonic-gate if ((err = raid_get_cgapart(un, &dmp)) != 0) {
28410Sstevel@tonic-gate return (err);
28420Sstevel@tonic-gate }
28430Sstevel@tonic-gate
28440Sstevel@tonic-gate if ((mode & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
28450Sstevel@tonic-gate if (ddi_copyout((caddr_t)&dmp, data, sizeof (dmp),
28467563SPrasad.Singamsetty@Sun.COM mode) != 0)
28470Sstevel@tonic-gate err = EFAULT;
28480Sstevel@tonic-gate }
28490Sstevel@tonic-gate #ifdef _SYSCALL32
28500Sstevel@tonic-gate else {
28510Sstevel@tonic-gate struct dk_map32 dmp32;
28520Sstevel@tonic-gate
28530Sstevel@tonic-gate dmp32.dkl_cylno = dmp.dkl_cylno;
28540Sstevel@tonic-gate dmp32.dkl_nblk = dmp.dkl_nblk;
28550Sstevel@tonic-gate
28560Sstevel@tonic-gate if (ddi_copyout((caddr_t)&dmp32, data, sizeof (dmp32),
28577563SPrasad.Singamsetty@Sun.COM mode) != 0)
28580Sstevel@tonic-gate err = EFAULT;
28590Sstevel@tonic-gate }
28600Sstevel@tonic-gate #endif /* _SYSCALL32 */
28610Sstevel@tonic-gate
28620Sstevel@tonic-gate return (err);
28630Sstevel@tonic-gate }
28640Sstevel@tonic-gate case DKIOCGETEFI:
28650Sstevel@tonic-gate {
28660Sstevel@tonic-gate /*
28670Sstevel@tonic-gate * This one can be done centralized,
28680Sstevel@tonic-gate * no need to put in the same code for all types of metadevices
28690Sstevel@tonic-gate */
28700Sstevel@tonic-gate return (md_dkiocgetefi(mnum, data, mode));
28710Sstevel@tonic-gate }
28720Sstevel@tonic-gate
28730Sstevel@tonic-gate case DKIOCSETEFI:
28740Sstevel@tonic-gate {
28750Sstevel@tonic-gate /*
28760Sstevel@tonic-gate * This one can be done centralized,
28770Sstevel@tonic-gate * no need to put in the same code for all types of metadevices
28780Sstevel@tonic-gate */
28790Sstevel@tonic-gate return (md_dkiocsetefi(mnum, data, mode));
28800Sstevel@tonic-gate }
28810Sstevel@tonic-gate
28820Sstevel@tonic-gate case DKIOCPARTITION:
28830Sstevel@tonic-gate {
28840Sstevel@tonic-gate return (md_dkiocpartition(mnum, data, mode));
28850Sstevel@tonic-gate }
28860Sstevel@tonic-gate
28870Sstevel@tonic-gate default:
28880Sstevel@tonic-gate return (ENOTTY);
28890Sstevel@tonic-gate }
28900Sstevel@tonic-gate }
28910Sstevel@tonic-gate
28920Sstevel@tonic-gate /*
28930Sstevel@tonic-gate * rename/exchange named service entry points and support functions follow.
28940Sstevel@tonic-gate * Most functions are handled generically, except for raid-specific locking
28950Sstevel@tonic-gate * and checking
28960Sstevel@tonic-gate */
28970Sstevel@tonic-gate
28980Sstevel@tonic-gate /*
28990Sstevel@tonic-gate * NAME: raid_may_renexch_self
29000Sstevel@tonic-gate * DESCRIPTION: support routine for rename check ("MDRNM_CHECK") named service
29010Sstevel@tonic-gate * PARAMETERS: mr_unit_t *un - unit struct of raid unit to be renamed
29020Sstevel@tonic-gate * mdi_unit_t *ui - in-core unit struct of same raid unit
29030Sstevel@tonic-gate * md_rentxn_t *rtxnp - rename transaction state
29040Sstevel@tonic-gate *
29050Sstevel@tonic-gate * LOCKS: none
29060Sstevel@tonic-gate *
29070Sstevel@tonic-gate */
29080Sstevel@tonic-gate static int
raid_may_renexch_self(mr_unit_t * un,mdi_unit_t * ui,md_rentxn_t * rtxnp)29090Sstevel@tonic-gate raid_may_renexch_self(
29100Sstevel@tonic-gate mr_unit_t *un,
29110Sstevel@tonic-gate mdi_unit_t *ui,
29120Sstevel@tonic-gate md_rentxn_t *rtxnp)
29130Sstevel@tonic-gate {
29140Sstevel@tonic-gate minor_t from_min;
29150Sstevel@tonic-gate minor_t to_min;
29160Sstevel@tonic-gate bool_t toplevel;
29170Sstevel@tonic-gate bool_t related;
29180Sstevel@tonic-gate
29190Sstevel@tonic-gate from_min = rtxnp->from.mnum;
29200Sstevel@tonic-gate to_min = rtxnp->to.mnum;
29210Sstevel@tonic-gate
29220Sstevel@tonic-gate if (!un || !ui) {
29230Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde, MDE_RENAME_CONFIG_ERROR,
29247563SPrasad.Singamsetty@Sun.COM from_min);
29250Sstevel@tonic-gate return (EINVAL);
29260Sstevel@tonic-gate }
29270Sstevel@tonic-gate
29280Sstevel@tonic-gate ASSERT(!(MD_CAPAB(un) & MD_CAN_META_CHILD));
29290Sstevel@tonic-gate if (MD_CAPAB(un) & MD_CAN_META_CHILD) {
29300Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde, MDE_RENAME_SOURCE_BAD, from_min);
29310Sstevel@tonic-gate return (EINVAL);
29320Sstevel@tonic-gate }
29330Sstevel@tonic-gate
29340Sstevel@tonic-gate if (MD_PARENT(un) == MD_MULTI_PARENT) {
29350Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde, MDE_RENAME_SOURCE_BAD, from_min);
29360Sstevel@tonic-gate return (EINVAL);
29370Sstevel@tonic-gate }
29380Sstevel@tonic-gate
29390Sstevel@tonic-gate toplevel = !MD_HAS_PARENT(MD_PARENT(un));
29400Sstevel@tonic-gate
29410Sstevel@tonic-gate /* we're related if trying to swap with our parent */
29420Sstevel@tonic-gate related = (!toplevel) && (MD_PARENT(un) == to_min);
29430Sstevel@tonic-gate
29440Sstevel@tonic-gate switch (rtxnp->op) {
29450Sstevel@tonic-gate case MDRNOP_EXCHANGE:
29460Sstevel@tonic-gate
29470Sstevel@tonic-gate if (!related) {
29480Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde,
29497563SPrasad.Singamsetty@Sun.COM MDE_RENAME_TARGET_UNRELATED, to_min);
29500Sstevel@tonic-gate return (EINVAL);
29510Sstevel@tonic-gate }
29520Sstevel@tonic-gate
29530Sstevel@tonic-gate break;
29540Sstevel@tonic-gate
29550Sstevel@tonic-gate case MDRNOP_RENAME:
29560Sstevel@tonic-gate /*
29570Sstevel@tonic-gate * if from is top-level and is open, then the kernel is using
29580Sstevel@tonic-gate * the md_dev64_t.
29590Sstevel@tonic-gate */
29600Sstevel@tonic-gate
29610Sstevel@tonic-gate if (toplevel && md_unit_isopen(ui)) {
29620Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde, MDE_RENAME_BUSY,
29637563SPrasad.Singamsetty@Sun.COM from_min);
29640Sstevel@tonic-gate return (EBUSY);
29650Sstevel@tonic-gate }
29660Sstevel@tonic-gate break;
29670Sstevel@tonic-gate
29680Sstevel@tonic-gate default:
29690Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde, MDE_RENAME_CONFIG_ERROR,
29707563SPrasad.Singamsetty@Sun.COM from_min);
29710Sstevel@tonic-gate return (EINVAL);
29720Sstevel@tonic-gate }
29730Sstevel@tonic-gate
29740Sstevel@tonic-gate return (0); /* ok */
29750Sstevel@tonic-gate }
29760Sstevel@tonic-gate
29770Sstevel@tonic-gate /*
29780Sstevel@tonic-gate * NAME: raid_rename_check
29790Sstevel@tonic-gate * DESCRIPTION: ("MDRNM_CHECK") rename/exchange named service entry point
29800Sstevel@tonic-gate * PARAMETERS: md_rendelta_t *delta - describes changes to be made to this
29810Sstevel@tonic-gate * raid device for rename transaction
29820Sstevel@tonic-gate * md_rentxn_t *rtxnp - rename transaction state
29830Sstevel@tonic-gate *
29840Sstevel@tonic-gate * LOCKS: none
29850Sstevel@tonic-gate *
29860Sstevel@tonic-gate */
29870Sstevel@tonic-gate intptr_t
raid_rename_check(md_rendelta_t * delta,md_rentxn_t * rtxnp)29880Sstevel@tonic-gate raid_rename_check(
29890Sstevel@tonic-gate md_rendelta_t *delta,
29900Sstevel@tonic-gate md_rentxn_t *rtxnp)
29910Sstevel@tonic-gate {
29920Sstevel@tonic-gate int err = 0;
29930Sstevel@tonic-gate int column;
29940Sstevel@tonic-gate mr_unit_t *un;
29950Sstevel@tonic-gate
29960Sstevel@tonic-gate ASSERT(delta);
29970Sstevel@tonic-gate ASSERT(rtxnp);
29980Sstevel@tonic-gate ASSERT(delta->unp);
29990Sstevel@tonic-gate ASSERT(delta->uip);
30000Sstevel@tonic-gate
30010Sstevel@tonic-gate if (!delta || !rtxnp || !delta->unp || !delta->uip) {
30020Sstevel@tonic-gate (void) mdsyserror(&rtxnp->mde, EINVAL);
30030Sstevel@tonic-gate return (EINVAL);
30040Sstevel@tonic-gate }
30050Sstevel@tonic-gate
30060Sstevel@tonic-gate un = (mr_unit_t *)delta->unp;
30070Sstevel@tonic-gate
30080Sstevel@tonic-gate for (column = 0; column < un->un_totalcolumncnt; column++) {
30090Sstevel@tonic-gate rcs_state_t colstate;
30100Sstevel@tonic-gate
30110Sstevel@tonic-gate colstate = un->un_column[column].un_devstate;
30120Sstevel@tonic-gate
30130Sstevel@tonic-gate if (colstate & RCS_LAST_ERRED) {
30140Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde, MDE_RAID_LAST_ERRED,
30157563SPrasad.Singamsetty@Sun.COM md_getminor(delta->dev));
30160Sstevel@tonic-gate return (EINVAL);
30170Sstevel@tonic-gate }
30180Sstevel@tonic-gate
30190Sstevel@tonic-gate if (colstate & RCS_INIT_ERRED) {
30200Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde, MDE_RAID_DOI,
30217563SPrasad.Singamsetty@Sun.COM md_getminor(delta->dev));
30220Sstevel@tonic-gate return (EINVAL);
30230Sstevel@tonic-gate }
30240Sstevel@tonic-gate
30250Sstevel@tonic-gate /* How did we get this far before detecting this? */
30260Sstevel@tonic-gate if (colstate & RCS_RESYNC) {
30270Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde, MDE_RENAME_BUSY,
30287563SPrasad.Singamsetty@Sun.COM md_getminor(delta->dev));
30290Sstevel@tonic-gate return (EBUSY);
30300Sstevel@tonic-gate }
30310Sstevel@tonic-gate
30320Sstevel@tonic-gate if (colstate & RCS_ERRED) {
30330Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde, MDE_RAID_NOT_OKAY,
30347563SPrasad.Singamsetty@Sun.COM md_getminor(delta->dev));
30350Sstevel@tonic-gate return (EINVAL);
30360Sstevel@tonic-gate }
30370Sstevel@tonic-gate
30380Sstevel@tonic-gate if (!(colstate & RCS_OKAY)) {
30390Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde, MDE_RAID_NOT_OKAY,
30407563SPrasad.Singamsetty@Sun.COM md_getminor(delta->dev));
30410Sstevel@tonic-gate return (EINVAL);
30420Sstevel@tonic-gate }
30430Sstevel@tonic-gate
30440Sstevel@tonic-gate if (HOTSPARED(un, column)) {
30450Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde, MDE_RAID_NOT_OKAY,
30467563SPrasad.Singamsetty@Sun.COM md_getminor(delta->dev));
30470Sstevel@tonic-gate return (EINVAL);
30480Sstevel@tonic-gate }
30490Sstevel@tonic-gate }
30500Sstevel@tonic-gate
30510Sstevel@tonic-gate /* self does additional checks */
30520Sstevel@tonic-gate if (delta->old_role == MDRR_SELF) {
30530Sstevel@tonic-gate err = raid_may_renexch_self((mr_unit_t *)delta->unp,
30547563SPrasad.Singamsetty@Sun.COM delta->uip, rtxnp);
30550Sstevel@tonic-gate }
30560Sstevel@tonic-gate return (err);
30570Sstevel@tonic-gate }
30580Sstevel@tonic-gate
30590Sstevel@tonic-gate /*
30600Sstevel@tonic-gate * NAME: raid_rename_lock
30610Sstevel@tonic-gate * DESCRIPTION: ("MDRNM_LOCK") rename/exchange named service entry point
30620Sstevel@tonic-gate * PARAMETERS: md_rendelta_t *delta - describes changes to be made to this
30630Sstevel@tonic-gate * raid device for rename transaction
30640Sstevel@tonic-gate * md_rentxn_t *rtxnp - rename transaction state
30650Sstevel@tonic-gate *
30660Sstevel@tonic-gate * LOCKS: io and unit locks (taken explicitly *not* via ioctl wrappers)
30670Sstevel@tonic-gate *
30680Sstevel@tonic-gate */
30690Sstevel@tonic-gate intptr_t
raid_rename_lock(md_rendelta_t * delta,md_rentxn_t * rtxnp)30700Sstevel@tonic-gate raid_rename_lock(
30710Sstevel@tonic-gate md_rendelta_t *delta,
30720Sstevel@tonic-gate md_rentxn_t *rtxnp)
30730Sstevel@tonic-gate {
30740Sstevel@tonic-gate minor_t mnum;
30750Sstevel@tonic-gate
30760Sstevel@tonic-gate ASSERT(delta);
30770Sstevel@tonic-gate ASSERT(rtxnp);
30780Sstevel@tonic-gate
30790Sstevel@tonic-gate mnum = md_getminor(delta->dev);
30800Sstevel@tonic-gate if (mnum == rtxnp->to.mnum && rtxnp->op == MDRNOP_RENAME) {
30810Sstevel@tonic-gate return (0);
30820Sstevel@tonic-gate }
30830Sstevel@tonic-gate
30840Sstevel@tonic-gate ASSERT(delta->uip);
30850Sstevel@tonic-gate if (!delta->uip) {
30860Sstevel@tonic-gate (void) mdmderror(&rtxnp->mde, MDE_UNIT_NOT_SETUP, mnum);
30870Sstevel@tonic-gate return (ENODEV);
30880Sstevel@tonic-gate }
30890Sstevel@tonic-gate
30900Sstevel@tonic-gate ASSERT(delta->unp);
30910Sstevel@tonic-gate if (!delta->unp) {
30920Sstevel@tonic-gate
30930Sstevel@tonic-gate return (ENODEV);
30940Sstevel@tonic-gate }
30950Sstevel@tonic-gate
30960Sstevel@tonic-gate ASSERT(!IO_WRITER_HELD(delta->unp));
30970Sstevel@tonic-gate (void) md_io_writerlock(delta->uip);
30980Sstevel@tonic-gate ASSERT(IO_WRITER_HELD(delta->unp));
30990Sstevel@tonic-gate
31000Sstevel@tonic-gate
31010Sstevel@tonic-gate ASSERT(!UNIT_WRITER_HELD(delta->unp));
31020Sstevel@tonic-gate (void) md_unit_writerlock(delta->uip);
31030Sstevel@tonic-gate ASSERT(UNIT_WRITER_HELD(delta->unp));
31040Sstevel@tonic-gate
31050Sstevel@tonic-gate return (0);
31060Sstevel@tonic-gate }
31070Sstevel@tonic-gate
31080Sstevel@tonic-gate /*
31090Sstevel@tonic-gate * NAME: raid_rename_unlock
31100Sstevel@tonic-gate * DESCRIPTION: ("MDRNM_UNLOCK") rename/exchange named service entry point
31110Sstevel@tonic-gate * PARAMETERS: md_rendelta_t *delta - describes changes to be made to this
31120Sstevel@tonic-gate * raid device for rename transaction
31130Sstevel@tonic-gate * md_rentxn_t *rtxnp - rename transaction state
31140Sstevel@tonic-gate *
31150Sstevel@tonic-gate * LOCKS: drops io and unit locks
31160Sstevel@tonic-gate *
31170Sstevel@tonic-gate */
31180Sstevel@tonic-gate /* ARGSUSED */
31190Sstevel@tonic-gate void
raid_rename_unlock(md_rendelta_t * delta,md_rentxn_t * rtxnp)31200Sstevel@tonic-gate raid_rename_unlock(
31210Sstevel@tonic-gate md_rendelta_t *delta,
31220Sstevel@tonic-gate md_rentxn_t *rtxnp)
31230Sstevel@tonic-gate {
31240Sstevel@tonic-gate mr_unit_t *un = (mr_unit_t *)delta->unp;
31250Sstevel@tonic-gate minor_t mnum = MD_SID(un);
31260Sstevel@tonic-gate int col;
31270Sstevel@tonic-gate
31280Sstevel@tonic-gate ASSERT(delta);
31290Sstevel@tonic-gate ASSERT(delta->unp);
31300Sstevel@tonic-gate ASSERT(delta->uip);
31310Sstevel@tonic-gate
31320Sstevel@tonic-gate ASSERT(UNIT_WRITER_HELD(delta->unp));
31330Sstevel@tonic-gate md_unit_writerexit(delta->uip);
31340Sstevel@tonic-gate ASSERT(!UNIT_WRITER_HELD(delta->unp));
31350Sstevel@tonic-gate
31360Sstevel@tonic-gate if (! (delta->txn_stat.role_swapped) || ! (delta->txn_stat.is_open)) {
31370Sstevel@tonic-gate goto out;
31380Sstevel@tonic-gate }
31390Sstevel@tonic-gate if (raid_internal_open(mnum, (FREAD | FWRITE),
31407563SPrasad.Singamsetty@Sun.COM OTYP_LYR, MD_OFLG_ISINIT) == 0) {
31410Sstevel@tonic-gate for (col = 0; col < un->un_totalcolumncnt; col++) {
31420Sstevel@tonic-gate if (un->un_column[col].un_devstate & RCS_OKAY)
31430Sstevel@tonic-gate (void) init_pw_area(un,
31447563SPrasad.Singamsetty@Sun.COM un->un_column[col].un_dev,
31457563SPrasad.Singamsetty@Sun.COM un->un_column[col].un_pwstart, col);
31460Sstevel@tonic-gate }
31470Sstevel@tonic-gate (void) raid_internal_close(mnum, OTYP_LYR, 0, 0);
31480Sstevel@tonic-gate }
31490Sstevel@tonic-gate
31500Sstevel@tonic-gate out:
31510Sstevel@tonic-gate ASSERT(IO_WRITER_HELD(delta->unp));
31520Sstevel@tonic-gate md_io_writerexit(delta->uip);
31530Sstevel@tonic-gate ASSERT(!IO_WRITER_HELD(delta->unp));
31540Sstevel@tonic-gate }
31550Sstevel@tonic-gate /* end of rename/exchange named service and support functions */
3156