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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*62Sjeanm  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include	<stdio.h>
300Sstevel@tonic-gate #include	<stdarg.h>
310Sstevel@tonic-gate #include	<ctype.h>
320Sstevel@tonic-gate #include	<sys/fcntl.h>
330Sstevel@tonic-gate #include	<sys/types.h>
340Sstevel@tonic-gate #include	<devid.h>
350Sstevel@tonic-gate #include	<ftw.h>
360Sstevel@tonic-gate #include	<string.h>
370Sstevel@tonic-gate #include	<mdiox.h>
380Sstevel@tonic-gate #include	<sys/lvm/mdio.h>
390Sstevel@tonic-gate #include 	<meta.h>
400Sstevel@tonic-gate #include 	<syslog.h>
410Sstevel@tonic-gate #include	<sdssc.h>
420Sstevel@tonic-gate #include	"meta_set_prv.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * Just in case we're not in a build environment, make sure that
460Sstevel@tonic-gate  * TEXT_DOMAIN gets set to something.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
490Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
500Sstevel@tonic-gate #endif
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	RAW_PATH		0x001	/* rdsk */
530Sstevel@tonic-gate #define	BLOCK_PATH		0x002	/* dsk */
540Sstevel@tonic-gate #define	DSK_TYPE		0x004	/* normal /dev/[r]dsk */
550Sstevel@tonic-gate #define	TEST_TYPE		0x008	/* test driver path */
560Sstevel@tonic-gate #define	DID_TYPE		0x010	/* cluster did path */
570Sstevel@tonic-gate #define	AP_TYPE			0x020	/* should be obsolete */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate typedef struct path_list {
600Sstevel@tonic-gate 	char			*search_path;
610Sstevel@tonic-gate 	char			*search_type;
620Sstevel@tonic-gate 	int			path_type;
630Sstevel@tonic-gate } path_list_t;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * A table of the supported path types - this should ideally be generated
670Sstevel@tonic-gate  * by reading the /etc/lvm/devpath file
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate static path_list_t plist[] = {
700Sstevel@tonic-gate 	{"/dev/rdsk", DEVID_MINOR_NAME_ALL_CHR, RAW_PATH|DSK_TYPE},
710Sstevel@tonic-gate 	{"/dev/dsk", DEVID_MINOR_NAME_ALL_BLK, BLOCK_PATH|DSK_TYPE},
720Sstevel@tonic-gate 	{"/dev/did/rdsk", DEVID_MINOR_NAME_ALL_CHR, RAW_PATH|DID_TYPE},
730Sstevel@tonic-gate 	{"/dev/did/dsk", DEVID_MINOR_NAME_ALL_BLK, BLOCK_PATH|DID_TYPE},
740Sstevel@tonic-gate 	{"/dev/td/dsk", DEVID_MINOR_NAME_ALL_BLK, BLOCK_PATH|TEST_TYPE},
750Sstevel@tonic-gate 	{"/dev/td/rdsk", DEVID_MINOR_NAME_ALL_CHR, RAW_PATH|TEST_TYPE},
760Sstevel@tonic-gate };
770Sstevel@tonic-gate static int num = sizeof (plist)/sizeof (path_list_t);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static mddevopts_t	dev_options = 0;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /* indicate whether to print an error message or not */
820Sstevel@tonic-gate static int	firsttime = 1;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #define	DEV_MATCH	0x1
850Sstevel@tonic-gate #define	NAME_MATCH	0x2
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #define	DEBUGON		1
880Sstevel@tonic-gate #define	DEBUGOFF	2
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * Debug function: to turn on devadm function debugging include DEVADM
920Sstevel@tonic-gate  * in the MD_DEBUG enviroment variable: MD_DEBUG=...,DEVADM...
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate /*PRINTFLIKE1*/
950Sstevel@tonic-gate static void
960Sstevel@tonic-gate mda_debug(char *format, ...)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	char	*p;
990Sstevel@tonic-gate 	static int debug_set = 0;
1000Sstevel@tonic-gate 	va_list ap;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	if (debug_set == 0) {
1030Sstevel@tonic-gate 		if (((p = getenv("MD_DEBUG")) != NULL) &&
1040Sstevel@tonic-gate 		    (strstr(p, "DEVADM") != NULL))
1050Sstevel@tonic-gate 			debug_set = DEBUGON;
1060Sstevel@tonic-gate 		else
1070Sstevel@tonic-gate 			debug_set = DEBUGOFF;
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 	if (debug_set == DEBUGON) {
1100Sstevel@tonic-gate 		va_start(ap, format);
1110Sstevel@tonic-gate 		(void) vfprintf(stderr, format, ap);
1120Sstevel@tonic-gate 		va_end(ap);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /* print error messages to the terminal or syslog */
1170Sstevel@tonic-gate /*PRINTFLIKE1*/
1180Sstevel@tonic-gate static void
1190Sstevel@tonic-gate mda_print(char *message, ...)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	va_list	ap;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	va_start(ap, message);
1240Sstevel@tonic-gate 	if (dev_options & DEV_LOG) {
1250Sstevel@tonic-gate 		/*
1260Sstevel@tonic-gate 		 * The program is a daemon in the sense that it
1270Sstevel@tonic-gate 		 * is a system utility.
1280Sstevel@tonic-gate 		 */
1290Sstevel@tonic-gate 		(void) vsyslog((LOG_ERR | LOG_DAEMON), message, ap);
1300Sstevel@tonic-gate 	} else {
1310Sstevel@tonic-gate 		(void) vfprintf(stderr, message, ap);
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	va_end(ap);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * Utility to find the correct options to use for the devid search
1380Sstevel@tonic-gate  * based upon the path of the device.
1390Sstevel@tonic-gate  *
1400Sstevel@tonic-gate  * RETURN:
1410Sstevel@tonic-gate  *	-1 	Error, the path passed in is not in the table
1420Sstevel@tonic-gate  *      >= 0    The element number for the options within the table
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate static int
1450Sstevel@tonic-gate mda_findpath(char *path)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	int	i = 0;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
1500Sstevel@tonic-gate 		if (strncmp(plist[i].search_path, path,
1510Sstevel@tonic-gate 		    strlen(plist[i].search_path)) == 0)
1520Sstevel@tonic-gate 			return (i);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 	return (-1);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * Utility to get the path of a device
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate static char *
1610Sstevel@tonic-gate mda_getpath(char *devname)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	char	*ptr;
1640Sstevel@tonic-gate 	char	*pathname;
1650Sstevel@tonic-gate 	size_t	len;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if ((ptr = strrchr(devname, '/')) == NULL) {
1680Sstevel@tonic-gate 		mda_debug("Invalid format: %s\n", devname);
1690Sstevel@tonic-gate 		return (NULL);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 	ptr++;
1720Sstevel@tonic-gate 	len = strlen(devname) - strlen(ptr);
1730Sstevel@tonic-gate 	pathname = Malloc(len + 1);
1740Sstevel@tonic-gate 	(void) strncpy(pathname, devname, len);
1750Sstevel@tonic-gate 	pathname[len] = '\0';
1760Sstevel@tonic-gate 	return (pathname);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate  * update_locator_namespace -- Contains the ioctl call that will update
1810Sstevel@tonic-gate  *		the ctds and pathname (ie. /dev/dsk etc) within the
1820Sstevel@tonic-gate  *		locator block namespace.
1830Sstevel@tonic-gate  *
1840Sstevel@tonic-gate  * RETURN
1850Sstevel@tonic-gate  *	METADEVADM_ERR		ioctl failed and ep is updated with the error
1860Sstevel@tonic-gate  *	METADEVADM_SUCCESS	success
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate static int
1890Sstevel@tonic-gate update_locator_namespace(
1900Sstevel@tonic-gate 	set_t		setno,
1910Sstevel@tonic-gate 	side_t		sideno,
1920Sstevel@tonic-gate 	char		*devname,
1930Sstevel@tonic-gate 	md_dev64_t	dev,
1940Sstevel@tonic-gate 	char		*pname,
1950Sstevel@tonic-gate 	md_error_t	*ep
1960Sstevel@tonic-gate )
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	mdnm_params_t	nm;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	(void) memset(&nm, '\0', sizeof (nm));
2010Sstevel@tonic-gate 	nm.mde = mdnullerror;
2020Sstevel@tonic-gate 	nm.setno = setno;
2030Sstevel@tonic-gate 	nm.side = sideno;
2040Sstevel@tonic-gate 	nm.devname = (uintptr_t)devname;
2050Sstevel@tonic-gate 	nm.devname_len = strlen(devname);
2060Sstevel@tonic-gate 	nm.devt = dev;
2070Sstevel@tonic-gate 	nm.pathname = (uintptr_t)pname;
2080Sstevel@tonic-gate 	nm.pathname_len = strlen(pname);
2090Sstevel@tonic-gate 	if (metaioctl(MD_IOCUPD_LOCNM, &nm, &nm.mde, NULL) != 0) {
2100Sstevel@tonic-gate 		(void) mdstealerror(ep, &nm.mde);
2110Sstevel@tonic-gate 		return (METADEVADM_ERR);
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 	return (METADEVADM_SUCCESS);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate  * update_namespace -- Contains the ioctl call that will update the
2180Sstevel@tonic-gate  * 	device name and pathname in the namespace area.
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  * RETURN
2210Sstevel@tonic-gate  *	METADEVADM_ERR		ioctl failed and ep is updated with the error
2220Sstevel@tonic-gate  *	METADEVADM_SUCCESS	success
2230Sstevel@tonic-gate  */
2240Sstevel@tonic-gate static int
2250Sstevel@tonic-gate update_namespace(
2260Sstevel@tonic-gate 	set_t		setno,
2270Sstevel@tonic-gate 	side_t		sideno,
2280Sstevel@tonic-gate 	char		*devname,
2290Sstevel@tonic-gate 	md_dev64_t	dev,
2300Sstevel@tonic-gate 	mdkey_t		key,
2310Sstevel@tonic-gate 	char		*pname,
2320Sstevel@tonic-gate 	md_error_t	*ep
2330Sstevel@tonic-gate )
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	mdnm_params_t	nm;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	(void) memset(&nm, '\0', sizeof (nm));
2380Sstevel@tonic-gate 	nm.mde = mdnullerror;
2390Sstevel@tonic-gate 	nm.setno = setno;
2400Sstevel@tonic-gate 	nm.side = sideno;
2410Sstevel@tonic-gate 	nm.devname = (uintptr_t)devname;
2420Sstevel@tonic-gate 	nm.devname_len = strlen(devname);
2430Sstevel@tonic-gate 	nm.mnum = meta_getminor(dev);
2440Sstevel@tonic-gate 	nm.key = key;
2450Sstevel@tonic-gate 	nm.pathname = (uintptr_t)pname;
2460Sstevel@tonic-gate 	nm.pathname_len = strlen(pname);
2470Sstevel@tonic-gate 	if (metaioctl(MD_IOCUPD_NM, &nm, &nm.mde, NULL) != 0) {
2480Sstevel@tonic-gate 		(void) mdstealerror(ep, &nm.mde);
2490Sstevel@tonic-gate 		return (METADEVADM_ERR);
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 	return (METADEVADM_SUCCESS);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate  * stripS - Strip s<digits> off the end of the ctds name if it exists
2560Sstevel@tonic-gate  */
2570Sstevel@tonic-gate static void
2580Sstevel@tonic-gate stripS(char *name)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	char	*p;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/* gobble number and 's' */
2630Sstevel@tonic-gate 	p = name + strlen(name) - 1;
2640Sstevel@tonic-gate 	for (; (p > name); --p) {
2650Sstevel@tonic-gate 		if (!isdigit(*p))
2660Sstevel@tonic-gate 			break;
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (*p == 's') {
2700Sstevel@tonic-gate 		*p = '\0';
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate  * getdiskname -- to be used when scanning the input from the -u arg.
2760Sstevel@tonic-gate  * 	This routine will strip off input that is anything but cxtxdx.
2770Sstevel@tonic-gate  *	ie. it will call stripS to get rid of slice info. Will also
2780Sstevel@tonic-gate  *	strip off /dev/dsk, /dev/rdsk, /dev/ap/dsk, /dev/ap/rdsk,
2790Sstevel@tonic-gate  *	/dev/did/dsk, or /dev/did/rdsk. The caller will need to free
2800Sstevel@tonic-gate  *	the return value.
2810Sstevel@tonic-gate  *
2820Sstevel@tonic-gate  * RETURN
2830Sstevel@tonic-gate  *	 string that has the disk name in it ie. c0t0d0
2840Sstevel@tonic-gate  */
2850Sstevel@tonic-gate static char *
2860Sstevel@tonic-gate getdiskname(
2870Sstevel@tonic-gate 	char	*name
2880Sstevel@tonic-gate )
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	char	*p;
2910Sstevel@tonic-gate 	char	*diskname;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	/* regular device */
2940Sstevel@tonic-gate 	if ((strncmp(name, "/dev/dsk/", strlen("/dev/dsk/")) == 0) &&
2950Sstevel@tonic-gate 	    (strchr((p = name + strlen("/dev/dsk/")), '/') == NULL)) {
2960Sstevel@tonic-gate 		diskname = Strdup(p);
2970Sstevel@tonic-gate 		stripS(diskname);
2980Sstevel@tonic-gate 		return (diskname);
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	if ((strncmp(name, "/dev/rdsk/", strlen("/dev/rdsk/")) == 0) &&
3020Sstevel@tonic-gate 	    (strchr((p = name + strlen("/dev/rdsk/")), '/') == NULL)) {
3030Sstevel@tonic-gate 		diskname = Strdup(p);
3040Sstevel@tonic-gate 		stripS(diskname);
3050Sstevel@tonic-gate 		return (diskname);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if ((strncmp(name, "/dev/ap/dsk/", strlen("/dev/ap/dsk/")) == 0) &&
3090Sstevel@tonic-gate 	    (strchr((p = name + strlen("/dev/ap/dsk/")), '/') == NULL)) {
3100Sstevel@tonic-gate 		diskname = Strdup(p);
3110Sstevel@tonic-gate 		stripS(diskname);
3120Sstevel@tonic-gate 		return (diskname);
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	if ((strncmp(name, "/dev/ap/rdsk/", strlen("/dev/ap/rdsk/")) == 0) &&
3160Sstevel@tonic-gate 	    (strchr((p = name + strlen("/dev/ap/rdsk/")), '/') == NULL)) {
3170Sstevel@tonic-gate 		diskname = Strdup(p);
3180Sstevel@tonic-gate 		stripS(diskname);
3190Sstevel@tonic-gate 		return (diskname);
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if ((strncmp(name, "/dev/did/dsk/", strlen("/dev/did/dsk/")) == 0) &&
3230Sstevel@tonic-gate 	    (strchr((p = name + strlen("/dev/did/dsk/")), '/') == NULL)) {
3240Sstevel@tonic-gate 		diskname = Strdup(p);
3250Sstevel@tonic-gate 		stripS(diskname);
3260Sstevel@tonic-gate 		return (diskname);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	if ((strncmp(name, "/dev/did/rdsk/", strlen("/dev/did/rdsk/")) == 0) &&
3300Sstevel@tonic-gate 	    (strchr((p = name + strlen("/dev/did/rdsk/")), '/') == NULL)) {
3310Sstevel@tonic-gate 		diskname = Strdup(p);
3320Sstevel@tonic-gate 		stripS(diskname);
3330Sstevel@tonic-gate 		return (diskname);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	diskname = Strdup(name);
3370Sstevel@tonic-gate 	stripS(diskname);
3380Sstevel@tonic-gate 	return (diskname);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate  * has_devid -- return the device ID for a given key
3430Sstevel@tonic-gate  *
3440Sstevel@tonic-gate  * RETURN
3450Sstevel@tonic-gate  *	NULL	error
3460Sstevel@tonic-gate  *	devid	devid found that corresponds to the given key.
3470Sstevel@tonic-gate  */
3480Sstevel@tonic-gate static ddi_devid_t
3490Sstevel@tonic-gate has_devid(set_t setno, side_t sideno,  mdkey_t key, md_error_t *ep)
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate 	return (meta_getdidbykey(setno, sideno, key, ep));
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate  * Go through the existing list of replicas and check to see
3560Sstevel@tonic-gate  * if their disk has moved, if so update the replica list
3570Sstevel@tonic-gate  *
3580Sstevel@tonic-gate  * RETURN
3590Sstevel@tonic-gate  *	-1	error
3600Sstevel@tonic-gate  *	 0	success
3610Sstevel@tonic-gate  */
3620Sstevel@tonic-gate static int
3630Sstevel@tonic-gate fix_replicanames(
3640Sstevel@tonic-gate 	mdsetname_t	*sp,
3650Sstevel@tonic-gate 	md_error_t	*ep
3660Sstevel@tonic-gate )
3670Sstevel@tonic-gate {
3680Sstevel@tonic-gate 	md_replicalist_t	*rlp = NULL;
3690Sstevel@tonic-gate 	md_replicalist_t	*rl;
3700Sstevel@tonic-gate 	int			ret = -1;
3710Sstevel@tonic-gate 	int			match_type = 0;
3720Sstevel@tonic-gate 	devid_nmlist_t		*disklist = NULL;
3730Sstevel@tonic-gate 	dev_t			small_dev = (dev_t)NODEV;
3740Sstevel@tonic-gate 	side_t			sideno;
3750Sstevel@tonic-gate 	set_t			setno = sp->setno;
3760Sstevel@tonic-gate 	char			*search_path;
3770Sstevel@tonic-gate 	int			search_number;
3780Sstevel@tonic-gate 	char			*ctds_name;
3790Sstevel@tonic-gate 	char			*path_name;
3800Sstevel@tonic-gate 	int			i;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	sideno = getmyside(sp, ep);
3830Sstevel@tonic-gate 	if (sideno == MD_SIDEWILD) {
3840Sstevel@tonic-gate 		mda_debug("Failed to find the side number\n");
3850Sstevel@tonic-gate 		return (-1);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	if (metareplicalist(sp, MD_BASICNAME_OK | PRINT_FAST, &rlp, ep) < 0) {
3890Sstevel@tonic-gate 		mda_debug("Unable to get a list of replicas\n");
3900Sstevel@tonic-gate 		return (METADEVADM_ERR);
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	for (rl = rlp; (rl != NULL); rl = rl->rl_next) {
3940Sstevel@tonic-gate 		md_replica_t	*r = rl->rl_repp;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 		small_dev = meta_cmpldev(r->r_namep->dev);
3970Sstevel@tonic-gate 		search_number = mda_findpath(r->r_namep->bname);
3980Sstevel@tonic-gate 		if (search_number == -1) {
3990Sstevel@tonic-gate 			mda_debug("replica update: invalid path: %s",
4000Sstevel@tonic-gate 			    r->r_namep->bname);
4010Sstevel@tonic-gate 			continue;
4020Sstevel@tonic-gate 		} else {
4030Sstevel@tonic-gate 			search_path = plist[search_number].search_path;
4040Sstevel@tonic-gate 		}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 		if (r->r_devid == NULL)
4070Sstevel@tonic-gate 			continue;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 		ret = meta_deviceid_to_nmlist(search_path, r->r_devid,
4100Sstevel@tonic-gate 		    r->r_minor_name, &disklist);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 		mda_debug("replica update: search_path %s\n", search_path);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		if (ret != 0) {
4150Sstevel@tonic-gate 			/*
4160Sstevel@tonic-gate 			 * Failed to find the disk, nothing can be done.
4170Sstevel@tonic-gate 			 * The replica will be marked as bad later.
4180Sstevel@tonic-gate 			 */
4190Sstevel@tonic-gate 			mda_debug("replica update: failed to find disk %s\n",
4200Sstevel@tonic-gate 			    r->r_namep->cname);
4210Sstevel@tonic-gate 			continue;
4220Sstevel@tonic-gate 		}
4230Sstevel@tonic-gate 		mda_debug("replica update: current %s (%p)\n",
4240Sstevel@tonic-gate 			r->r_namep->bname, (void *) small_dev);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 		/*
4270Sstevel@tonic-gate 		 * Check to see if the returned disk matches the stored one
4280Sstevel@tonic-gate 		 */
4290Sstevel@tonic-gate 		for (i = 0; disklist[i].dev != NODEV; i++) {
4300Sstevel@tonic-gate 			match_type = 0;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 			mda_debug("replica update: devid list: %s (%p)\n",
4330Sstevel@tonic-gate 			    disklist[i].devname, (void *) disklist[i].dev);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 			if (disklist[i].dev == small_dev) {
4360Sstevel@tonic-gate 				match_type |= DEV_MATCH;
4370Sstevel@tonic-gate 			}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 			if (strncmp(r->r_namep->bname, disklist[i].devname,
4400Sstevel@tonic-gate 			    strlen(r->r_namep->bname)) == 0) {
4410Sstevel@tonic-gate 				match_type |= NAME_MATCH;
4420Sstevel@tonic-gate 			}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 			/*
4450Sstevel@tonic-gate 			 * break out if some sort of match is found because
4460Sstevel@tonic-gate 			 * we already match on the devid.
4470Sstevel@tonic-gate 			 */
4480Sstevel@tonic-gate 			if (match_type != 0)
4490Sstevel@tonic-gate 				break;
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 		mda_debug("fix_replicanames: match: %x i: %d\n", match_type, i);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		if (match_type == (DEV_MATCH|NAME_MATCH)) {
4550Sstevel@tonic-gate 			/* no change */
4560Sstevel@tonic-gate 			mda_debug("replica update: no change %s\n",
4570Sstevel@tonic-gate 			    disklist[i].devname);
4580Sstevel@tonic-gate 			devid_free_nmlist(disklist);
4590Sstevel@tonic-gate 			continue;
4600Sstevel@tonic-gate 		}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 		/* No match found - use the first entry in disklist */
4630Sstevel@tonic-gate 		if (disklist[i].dev == NODEV)
4640Sstevel@tonic-gate 			i = 0;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 		mda_debug("replica update: reloading %s %p\n",
4670Sstevel@tonic-gate 		    disklist[i].devname,
468*62Sjeanm 		    (void *)(uintptr_t)meta_expldev(disklist[i].dev));
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 		if (firsttime) {
4710Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
4720Sstevel@tonic-gate 			    "Disk movement detected\n"));
4730Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
4740Sstevel@tonic-gate 			    "Updating device names in Solaris Volume "
4750Sstevel@tonic-gate 			    "Manager\n"));
4760Sstevel@tonic-gate 			firsttime = 0;
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 		if (dev_options & DEV_VERBOSE) {
4800Sstevel@tonic-gate 			char	*devidstr;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 			devidstr =
4830Sstevel@tonic-gate 			    devid_str_encode(r->r_devid, r->r_minor_name);
4840Sstevel@tonic-gate 			if (devidstr == NULL) {
4850Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
4860Sstevel@tonic-gate 				    "Failed to encode the devid\n"));
4870Sstevel@tonic-gate 				continue;
4880Sstevel@tonic-gate 			}
4890Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
4900Sstevel@tonic-gate 			    "%s changed to %s from device relocation "
4910Sstevel@tonic-gate 			    "information %s\n"),
4920Sstevel@tonic-gate 			    (char *)r->r_namep->cname, disklist[i].devname,
4930Sstevel@tonic-gate 			    devidstr);
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		if (!(dev_options & DEV_NOACTION)) {
4970Sstevel@tonic-gate 			mda_debug("Updating locator name\n");
4980Sstevel@tonic-gate 			ctds_name = strrchr(disklist[i].devname, '/');
4990Sstevel@tonic-gate 			ctds_name++;
5000Sstevel@tonic-gate 			if ((path_name = mda_getpath(disklist[i].devname))
5010Sstevel@tonic-gate 			    == NULL) {
5020Sstevel@tonic-gate 				continue;
5030Sstevel@tonic-gate 			}
5040Sstevel@tonic-gate 			if (update_locator_namespace(setno, sideno,
5050Sstevel@tonic-gate 			    ctds_name, meta_expldev(disklist[i].dev),
5060Sstevel@tonic-gate 			    path_name, ep) != 0) {
5070Sstevel@tonic-gate 				mda_debug("replica update: ioctl failed\n");
5080Sstevel@tonic-gate 				if (dev_options & DEV_VERBOSE) {
5090Sstevel@tonic-gate 					mda_print(dgettext(TEXT_DOMAIN,
5100Sstevel@tonic-gate 					    "Failed to update locator "
5110Sstevel@tonic-gate 					    "namespace on change from %s "
5120Sstevel@tonic-gate 					    "to %s\n"), ctds_name,
5130Sstevel@tonic-gate 					    disklist[i].devname);
5140Sstevel@tonic-gate 				}
5150Sstevel@tonic-gate 			}
5160Sstevel@tonic-gate 		}
5170Sstevel@tonic-gate 		Free(path_name);
5180Sstevel@tonic-gate 		devid_free_nmlist(disklist);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	metafreereplicalist(rlp);
5210Sstevel@tonic-gate 	return (0);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate /*
5250Sstevel@tonic-gate  * pathname_reload - main function for the -r option. Will reload the
5260Sstevel@tonic-gate  *	pathname in both the main namespace and the locator namespace.
5270Sstevel@tonic-gate  *	Also, checks both areas for invalid device ID's and prints them
5280Sstevel@tonic-gate  *	out.
5290Sstevel@tonic-gate  *
5300Sstevel@tonic-gate  *    If the set is a multi-node diskset that means there are no devid's
5310Sstevel@tonic-gate  *    so just return.
5320Sstevel@tonic-gate  *
5330Sstevel@tonic-gate  * RETURN
5340Sstevel@tonic-gate  *	METADEVADM_ERR		error
5350Sstevel@tonic-gate  *	METADEVADM_SUCCESS 	success
5360Sstevel@tonic-gate  *	METADEVADM_DEVIDINVALID	success, but invalid devids detected
5370Sstevel@tonic-gate  */
5380Sstevel@tonic-gate int
5390Sstevel@tonic-gate pathname_reload(
5400Sstevel@tonic-gate 	mdsetname_t		**spp,
5410Sstevel@tonic-gate 	set_t			setno,
5420Sstevel@tonic-gate 	md_error_t		*ep)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	char			*drvnmp;
5450Sstevel@tonic-gate 	minor_t			mnum = 0;
5460Sstevel@tonic-gate 	md_dev64_t		dev = 0;
5470Sstevel@tonic-gate 	mdnm_params_t		nm;
5480Sstevel@tonic-gate 	char			*ctds_name;
5490Sstevel@tonic-gate 	ddi_devid_t		devidp;
5500Sstevel@tonic-gate 	md_i_didstat_t		ds;
5510Sstevel@tonic-gate 	side_t			sideno;
5520Sstevel@tonic-gate 	char			*search_path = NULL;
5530Sstevel@tonic-gate 	int			search_number;
5540Sstevel@tonic-gate 	devid_nmlist_t		*disklist = NULL;
5550Sstevel@tonic-gate 	char			*minor_name = NULL;
5560Sstevel@tonic-gate 	char			*devidstr = NULL;
5570Sstevel@tonic-gate 	char			*path = NULL;
5580Sstevel@tonic-gate 	int			ret;
5590Sstevel@tonic-gate 	dev_t			small_dev = (dev_t)NODEV;
5600Sstevel@tonic-gate 	int			match_type;
5610Sstevel@tonic-gate 	char			*tmp = NULL;
5620Sstevel@tonic-gate 	mdsetname_t		*sp = *spp;
5630Sstevel@tonic-gate 	md_set_desc		*sd;
5640Sstevel@tonic-gate 	int			i;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	/*
5670Sstevel@tonic-gate 	 * Check for multi-node diskset and return if it is one.
5680Sstevel@tonic-gate 	 */
5690Sstevel@tonic-gate 	if (!metaislocalset(sp)) {
5700Sstevel@tonic-gate 		if ((sd = metaget_setdesc(sp, ep)) == NULL)
5710Sstevel@tonic-gate 			return (METADEVADM_ERR);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 		if (MD_MNSET_DESC(sd))
5740Sstevel@tonic-gate 			return (METADEVADM_SUCCESS);
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	/*
5780Sstevel@tonic-gate 	 * Get the entry of the namespace via the key. To do this
5790Sstevel@tonic-gate 	 * call MD_IOCNXTKEY until no more.
5800Sstevel@tonic-gate 	 * For each entry in the namespace we want to check
5810Sstevel@tonic-gate 	 * for devid and update
5820Sstevel@tonic-gate 	 */
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	(void) memset(&nm, '\0', sizeof (nm));
5850Sstevel@tonic-gate 	nm.key = MD_KEYWILD;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	sideno = getmyside(*spp, ep);
5880Sstevel@tonic-gate 	if (sideno == MD_SIDEWILD) {
5890Sstevel@tonic-gate 		/* failed to find this node in the set */
5900Sstevel@tonic-gate 		mda_debug("Failed to find the side number\n");
5910Sstevel@tonic-gate 		return (METADEVADM_ERR);
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	/* LINTED */
5950Sstevel@tonic-gate 	while (1) {
5960Sstevel@tonic-gate 		nm.mde	= mdnullerror;
5970Sstevel@tonic-gate 		nm.setno = setno;
5980Sstevel@tonic-gate 		nm.side = sideno;
5990Sstevel@tonic-gate 		/* look at each key in the namespace */
6000Sstevel@tonic-gate 		if (metaioctl(MD_IOCNXTKEY_NM, &nm, &nm.mde, NULL) != 0) {
6010Sstevel@tonic-gate 			(void) mdstealerror(ep, &nm.mde);
6020Sstevel@tonic-gate 			return (METADEVADM_ERR);
6030Sstevel@tonic-gate 		}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 		if (nm.key == MD_KEYWILD) {
6060Sstevel@tonic-gate 			/* no more entries */
6070Sstevel@tonic-gate 			break;
6080Sstevel@tonic-gate 		}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 		/*
6110Sstevel@tonic-gate 		 * get the nm entry using the key. Then check to see if
6120Sstevel@tonic-gate 		 * there's a devid associated with this entry
6130Sstevel@tonic-gate 		 * If not, go onto next key.
6140Sstevel@tonic-gate 		 */
6150Sstevel@tonic-gate 		if ((nm.devname = (uintptr_t)meta_getnmentbykey(setno, sideno,
6160Sstevel@tonic-gate 		    nm.key, &drvnmp, &mnum, &dev, ep)) == NULL) {
6170Sstevel@tonic-gate 			mda_debug("pathname_reload: no name for key: %d\n",
6180Sstevel@tonic-gate 			    nm.key);
6190Sstevel@tonic-gate 			continue;
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		mda_debug("pathname_reload: examining %s\n",
623*62Sjeanm 		    (char *)(uintptr_t)nm.devname);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		if ((devidp = has_devid(setno, sideno, nm.key, ep)) == NULL) {
6260Sstevel@tonic-gate 			/* metadevices do not have devid's in them */
6270Sstevel@tonic-gate 			mda_debug("pathname_reload: no devid for %s\n",
628*62Sjeanm 			    (char *)(uintptr_t)nm.devname);
6290Sstevel@tonic-gate 			continue;
6300Sstevel@tonic-gate 		}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 		if ((minor_name = meta_getdidminorbykey(setno, sideno,
6330Sstevel@tonic-gate 		    nm.key, ep)) == NULL) {
6340Sstevel@tonic-gate 			/*
6350Sstevel@tonic-gate 			 * In theory this is impossible because if the
6360Sstevel@tonic-gate 			 * devidp is non-null then the minor_name has
6370Sstevel@tonic-gate 			 * already been looked up.
6380Sstevel@tonic-gate 			 */
639*62Sjeanm 			mda_debug("No minor name for %s\n",
640*62Sjeanm 			    (char *)(uintptr_t)nm.devname);
6410Sstevel@tonic-gate 			free(devidp);
6420Sstevel@tonic-gate 			continue;
6430Sstevel@tonic-gate 		}
6440Sstevel@tonic-gate 		/*
6450Sstevel@tonic-gate 		 * If there is a devid then we have a real device that
6460Sstevel@tonic-gate 		 * could have moved.
6470Sstevel@tonic-gate 		 */
6480Sstevel@tonic-gate 		devidstr = devid_str_encode(devidp, minor_name);
6490Sstevel@tonic-gate 		if (devidstr == NULL) {
6500Sstevel@tonic-gate 			mda_debug("Failed to encode the devid\n");
6510Sstevel@tonic-gate 			free(devidp);
6520Sstevel@tonic-gate 			continue;
6530Sstevel@tonic-gate 		}
6540Sstevel@tonic-gate 		mda_debug("devid: %s\n", devidstr);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 		/*
6570Sstevel@tonic-gate 		 * Find the search path that should be used. This is an
6580Sstevel@tonic-gate 		 * optimization to try and prevent a search for the complete
6590Sstevel@tonic-gate 		 * /dev namespace.
6600Sstevel@tonic-gate 		 */
661*62Sjeanm 		search_number = mda_findpath((char *)(uintptr_t)nm.devname);
6620Sstevel@tonic-gate 		if (search_number == -1) {
6630Sstevel@tonic-gate 			search_path = "/dev";
6640Sstevel@tonic-gate 		} else {
6650Sstevel@tonic-gate 			search_path = plist[search_number].search_path;
6660Sstevel@tonic-gate 		}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		/* now look for the disk name using the devid */
6690Sstevel@tonic-gate 		ret = meta_deviceid_to_nmlist(search_path, devidp,
6700Sstevel@tonic-gate 		    minor_name, &disklist);
6710Sstevel@tonic-gate 		free(devidp);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		if (ret != 0) {
6740Sstevel@tonic-gate 			/*
6750Sstevel@tonic-gate 			 * Failed to find the disk
6760Sstevel@tonic-gate 			 */
6770Sstevel@tonic-gate 			devid_str_free(devidstr);
6780Sstevel@tonic-gate 			continue;
6790Sstevel@tonic-gate 		}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 		small_dev = meta_cmpldev(dev);
6820Sstevel@tonic-gate 		mda_debug("Old device lookup: %s (%p)\n",
683*62Sjeanm 			(char *)(uintptr_t)nm.devname, (void *)small_dev);
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		/*
6860Sstevel@tonic-gate 		 * Check to see if the returned disk matches the stored one
6870Sstevel@tonic-gate 		 */
6880Sstevel@tonic-gate 		for (i = 0; disklist[i].dev != NODEV; i++) {
6890Sstevel@tonic-gate 			match_type = 0;
6900Sstevel@tonic-gate 			mda_debug("From devid lookup: %s (%p)\n",
6910Sstevel@tonic-gate 				(char *)disklist[i].devname,
6920Sstevel@tonic-gate 				(void *)disklist[i].dev);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 			if (disklist[i].dev == small_dev) {
6950Sstevel@tonic-gate 				match_type |= DEV_MATCH;
6960Sstevel@tonic-gate 			}
6970Sstevel@tonic-gate 
698*62Sjeanm 			if (strncmp((char *)(uintptr_t)nm.devname,
699*62Sjeanm 			    disklist[i].devname,
700*62Sjeanm 			    strlen((char *)(uintptr_t)nm.devname)) == 0) {
7010Sstevel@tonic-gate 				mda_debug("Name match: %s and %s (%d)\n",
702*62Sjeanm 				    disklist[i].devname,
703*62Sjeanm 				    (char *)(uintptr_t)nm.devname,
704*62Sjeanm 				    strlen((char *)(uintptr_t)nm.devname));
7050Sstevel@tonic-gate 				match_type |= NAME_MATCH;
7060Sstevel@tonic-gate 			}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 			if (match_type == (DEV_MATCH|NAME_MATCH))
7090Sstevel@tonic-gate 				break;
7100Sstevel@tonic-gate 		}
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		if (match_type == (DEV_MATCH|NAME_MATCH)) {
7130Sstevel@tonic-gate 			/* no change */
7140Sstevel@tonic-gate 			devid_str_free(devidstr);
7150Sstevel@tonic-gate 			mda_debug("All matched %s\n", disklist[i].devname);
7160Sstevel@tonic-gate 			devid_free_nmlist(disklist);
7170Sstevel@tonic-gate 			continue;
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 		/* No match found - use the first entry in disklist */
7210Sstevel@tonic-gate 		i = 0;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		if (firsttime) {
7240Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
7250Sstevel@tonic-gate 			    "Disk movement detected\n"));
7260Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
7270Sstevel@tonic-gate 			    "Updating device names in "
7280Sstevel@tonic-gate 			    "Solaris Volume Manager\n"));
7290Sstevel@tonic-gate 			firsttime = 0;
7300Sstevel@tonic-gate 		}
7310Sstevel@tonic-gate 		if (dev_options & DEV_VERBOSE) {
7320Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
7330Sstevel@tonic-gate 			    "%s changed to %s from device relocation "
7340Sstevel@tonic-gate 			    "information %s\n"),
735*62Sjeanm 			    (char *)(uintptr_t)nm.devname, disklist[i].devname,
7360Sstevel@tonic-gate 			    devidstr);
7370Sstevel@tonic-gate 		}
7380Sstevel@tonic-gate 		devid_str_free(devidstr);
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		/* need to build up the path of the disk */
7410Sstevel@tonic-gate 		if ((path = Strdup(disklist[i].devname)) == NULL) {
7420Sstevel@tonic-gate 			mda_debug("Failed to duplicate path: %s\n",
7430Sstevel@tonic-gate 			    disklist[i].devname);
7440Sstevel@tonic-gate 			devid_free_nmlist(disklist);
7450Sstevel@tonic-gate 			continue;
7460Sstevel@tonic-gate 		}
7470Sstevel@tonic-gate 		if ((tmp = strrchr(path, '/')) == NULL) {
7480Sstevel@tonic-gate 			mda_debug("Failed to parse %s\n", path);
7490Sstevel@tonic-gate 			devid_free_nmlist(disklist);
7500Sstevel@tonic-gate 			Free(path);
7510Sstevel@tonic-gate 			continue;
7520Sstevel@tonic-gate 		}
7530Sstevel@tonic-gate 		tmp += sizeof (char);
7540Sstevel@tonic-gate 		*tmp = '\0';
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 		if ((ctds_name = strrchr(disklist[i].devname, '/')) == NULL) {
7570Sstevel@tonic-gate 			mda_debug("Failed to parse ctds name: %s\n",
7580Sstevel@tonic-gate 			    disklist[i].devname);
7590Sstevel@tonic-gate 			devid_free_nmlist(disklist);
7600Sstevel@tonic-gate 			Free(path);
7610Sstevel@tonic-gate 			continue;
7620Sstevel@tonic-gate 		}
7630Sstevel@tonic-gate 		ctds_name += sizeof (char);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 		mda_debug("Reloading disk %s %s %p\n",
766*62Sjeanm 		    ctds_name, path,
767*62Sjeanm 		    (void *)(uintptr_t)meta_expldev(disklist[i].dev));
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 		if (!(dev_options & DEV_NOACTION)) {
7700Sstevel@tonic-gate 			/* Something has changed so update the namespace */
7710Sstevel@tonic-gate 			if (update_namespace(setno, sideno, ctds_name,
7720Sstevel@tonic-gate 			    meta_expldev(disklist[i].dev), nm.key, path,
7730Sstevel@tonic-gate 			    ep) != 0) {
7740Sstevel@tonic-gate 				mda_debug("Failed to update namespace\n");
7750Sstevel@tonic-gate 				if (dev_options & DEV_VERBOSE) {
7760Sstevel@tonic-gate 					mda_print(dgettext(TEXT_DOMAIN,
7770Sstevel@tonic-gate 					    "Failed to update namespace on "
7780Sstevel@tonic-gate 					    "change from %s to %s\n"),
7790Sstevel@tonic-gate 					    ctds_name, disklist[i].devname);
7800Sstevel@tonic-gate 				}
7810Sstevel@tonic-gate 			}
7820Sstevel@tonic-gate 		}
7830Sstevel@tonic-gate 		devid_free_nmlist(disklist);
7840Sstevel@tonic-gate 		Free(path);
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	if (fix_replicanames(*spp, ep) == -1)
7880Sstevel@tonic-gate 		mda_debug("Failed to update replicas\n");
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	/*
7910Sstevel@tonic-gate 	 * check for invalid device id's
7920Sstevel@tonic-gate 	 */
7930Sstevel@tonic-gate 	(void) memset(&ds, '\0', sizeof (ds));
7940Sstevel@tonic-gate 	ds.setno = setno;
7950Sstevel@tonic-gate 	ds.side = sideno;
7960Sstevel@tonic-gate 	ds.mode = MD_FIND_INVDID;
7970Sstevel@tonic-gate 	/* get count of number of invalid device id's */
7980Sstevel@tonic-gate 	if (metaioctl(MD_IOCDID_STAT, &ds, &ds.mde, NULL) != 0) {
7990Sstevel@tonic-gate 		(void) mdstealerror(ep, &ds.mde);
8000Sstevel@tonic-gate 		return (METADEVADM_ERR);
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 	if (ds.cnt != 0) {
8030Sstevel@tonic-gate 		char	*ctdptr, *ctdp;
8040Sstevel@tonic-gate 		/*
8050Sstevel@tonic-gate 		 * we have some invalid device id's so we need to
8060Sstevel@tonic-gate 		 * print them out
8070Sstevel@tonic-gate 		 */
8080Sstevel@tonic-gate 		ds.mode = MD_GET_INVDID;
8090Sstevel@tonic-gate 		/* malloc buffer for kernel to place devid list into */
8100Sstevel@tonic-gate 		if ((ctdptr = (char *)Malloc((ds.cnt * ds.maxsz) + 1)) == 0) {
8110Sstevel@tonic-gate 			return (METADEVADM_ERR);
8120Sstevel@tonic-gate 		}
8130Sstevel@tonic-gate 		ds.ctdp = (uintptr_t)ctdptr;
8140Sstevel@tonic-gate 		/* get actual list of invalid device id's */
8150Sstevel@tonic-gate 		if (metaioctl(MD_IOCDID_STAT, &ds, &ds.mde, NULL) != 0) {
8160Sstevel@tonic-gate 			Free(ctdptr);
8170Sstevel@tonic-gate 			(void) mdstealerror(ep, &ds.mde);
8180Sstevel@tonic-gate 			return (METADEVADM_ERR);
8190Sstevel@tonic-gate 		}
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 		/* print out the invalid devid's */
8220Sstevel@tonic-gate 		mda_print(dgettext(TEXT_DOMAIN,
8230Sstevel@tonic-gate 		    "Invalid device relocation information "
8240Sstevel@tonic-gate 		    "detected in Solaris Volume Manager\n"));
8250Sstevel@tonic-gate 		mda_print(dgettext(TEXT_DOMAIN,
8260Sstevel@tonic-gate 		    "Please check the status of the following disk(s):\n"));
827*62Sjeanm 		ctdp = (char *)(uintptr_t)ds.ctdp;
8280Sstevel@tonic-gate 		while (*ctdp != NULL) {
8290Sstevel@tonic-gate 			mda_print("\t%s\n", ctdp);
8300Sstevel@tonic-gate 			ctdp += ds.maxsz;
8310Sstevel@tonic-gate 		}
8320Sstevel@tonic-gate 		Free(ctdptr);
8330Sstevel@tonic-gate 		return (METADEVADM_DEVIDINVALID);
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 	return (METADEVADM_SUCCESS);
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate /*
8390Sstevel@tonic-gate  * replica_update_devid - cycle through the replica list, rlp, and
8400Sstevel@tonic-gate  *  update the device ids on all of the replicas that are on the
8410Sstevel@tonic-gate  *  device specified by lp. A side effect is to update the value of
8420Sstevel@tonic-gate  *  cdevidpp to contain the character representation of the device
8430Sstevel@tonic-gate  *  id before updating if it is not already set.
8440Sstevel@tonic-gate  *
8450Sstevel@tonic-gate  * RETURN
8460Sstevel@tonic-gate  *	METADEVADM_ERR		error
8470Sstevel@tonic-gate  *	METADEVADM_SUCCESS	success
8480Sstevel@tonic-gate  */
8490Sstevel@tonic-gate static int
8500Sstevel@tonic-gate replica_update_devid(
8510Sstevel@tonic-gate 	md_replicalist_t *rlp,
8520Sstevel@tonic-gate 	mddrivename_t	*dnp,
8530Sstevel@tonic-gate 	set_t		setno,
8540Sstevel@tonic-gate 	char		**cdevidpp,
8550Sstevel@tonic-gate 	md_error_t	*ep
8560Sstevel@tonic-gate )
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate 	mddb_config_t		db_c;
8590Sstevel@tonic-gate 	md_replicalist_t	*rl;
8600Sstevel@tonic-gate 	ddi_devid_t		devidp;
8610Sstevel@tonic-gate 	int			ret;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	if (cdevidpp == NULL)
8640Sstevel@tonic-gate 		return (METADEVADM_ERR);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	ret = devid_str_decode(dnp->devid, &devidp, NULL);
8670Sstevel@tonic-gate 	if (ret != 0) {
8680Sstevel@tonic-gate 		/* failed to encode the devid */
8690Sstevel@tonic-gate 		mda_debug("Failed to decode %s into a valid devid\n",
8700Sstevel@tonic-gate 		    dnp->devid);
8710Sstevel@tonic-gate 		return (METADEVADM_ERR);
8720Sstevel@tonic-gate 	}
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	/* search replica list for give ctd name */
8750Sstevel@tonic-gate 	for (rl = rlp; (rl != NULL); rl = rl->rl_next) {
8760Sstevel@tonic-gate 		md_replica_t    *r = rl->rl_repp;
8770Sstevel@tonic-gate 		mdname_t	*rnp = r->r_namep;
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 		if (strncmp(rnp->cname, dnp->cname, strlen(dnp->cname)) == 0) {
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 			/* found the replica, now grab the devid */
8820Sstevel@tonic-gate 			if (*cdevidpp == NULL) {
8830Sstevel@tonic-gate 				*cdevidpp = devid_str_encode(r->r_devid, NULL);
8840Sstevel@tonic-gate 			}
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 			if (*cdevidpp == NULL) {
8870Sstevel@tonic-gate 				devid_free(devidp);
8880Sstevel@tonic-gate 				return (METADEVADM_ERR);
8890Sstevel@tonic-gate 			}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 			mda_debug("Updating replica %s, set %d, old devid %s\n",
8920Sstevel@tonic-gate 			    rnp->cname, setno, *cdevidpp);
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 			if (dev_options & DEV_VERBOSE) {
8950Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
8960Sstevel@tonic-gate 				    "Updating replica %s of set number %d from "
8970Sstevel@tonic-gate 				    "device id %s to device id %s\n"),
8980Sstevel@tonic-gate 				    rnp->cname, setno, *cdevidpp, dnp->devid);
8990Sstevel@tonic-gate 			}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 			(void) memset(&db_c, '\0', sizeof (db_c));
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 			db_c.c_setno = setno;
9040Sstevel@tonic-gate 			db_c.c_devt = rnp->dev;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 			if (!(dev_options & DEV_NOACTION)) {
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 				mda_debug("Updating replica\n");
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 				/*
9110Sstevel@tonic-gate 				 * call into kernel to update lb
9120Sstevel@tonic-gate 				 * namespace device id
9130Sstevel@tonic-gate 				 * of given devt
9140Sstevel@tonic-gate 				 */
9150Sstevel@tonic-gate 				if (metaioctl(MD_DB_SETDID, &db_c,
9160Sstevel@tonic-gate 				    &db_c.c_mde, NULL) != 0) {
9170Sstevel@tonic-gate 					devid_free(devidp);
9180Sstevel@tonic-gate 					(void) mdstealerror(ep, &db_c.c_mde);
9190Sstevel@tonic-gate 					return (METADEVADM_ERR);
9200Sstevel@tonic-gate 				}
9210Sstevel@tonic-gate 			}
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 		}
9240Sstevel@tonic-gate 	}
9250Sstevel@tonic-gate 	devid_free(devidp);
9260Sstevel@tonic-gate 	return (METADEVADM_SUCCESS);
9270Sstevel@tonic-gate }
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate /*
9300Sstevel@tonic-gate  * devid_update -- main routine for the -u option. Will update both the
9310Sstevel@tonic-gate  * 	namespace and the locator block with the correct devid for the
9320Sstevel@tonic-gate  * 	disk specified.
9330Sstevel@tonic-gate  *
9340Sstevel@tonic-gate  * RETURN
9350Sstevel@tonic-gate  *	METADEVADM_ERR		error
9360Sstevel@tonic-gate  *	METADEVADM_SUCCESS	success
9370Sstevel@tonic-gate  */
9380Sstevel@tonic-gate static int
9390Sstevel@tonic-gate devid_update(
9400Sstevel@tonic-gate 	mdsetname_t	**spp,
9410Sstevel@tonic-gate 	set_t		setno,
9420Sstevel@tonic-gate 	char		*ctd,
9430Sstevel@tonic-gate 	md_error_t	*ep
9440Sstevel@tonic-gate )
9450Sstevel@tonic-gate {
9460Sstevel@tonic-gate 	md_drive_desc		*dd, *ddp;
9470Sstevel@tonic-gate 	mddrivename_t		*dnp;
9480Sstevel@tonic-gate 	mdnm_params_t		nm;
9490Sstevel@tonic-gate 	ddi_devid_t		devidp;
9500Sstevel@tonic-gate 	side_t			side;
9510Sstevel@tonic-gate 	char			*old_cdevidp = NULL;
9520Sstevel@tonic-gate 	md_replicalist_t	*rlp = NULL;
9530Sstevel@tonic-gate 	int			rval = METADEVADM_ERR;
9540Sstevel@tonic-gate 	mdname_t		*np = NULL;
9550Sstevel@tonic-gate 	uint_t			rep_slice;
9560Sstevel@tonic-gate 	char			*pathname = NULL;
9570Sstevel@tonic-gate 	char			*diskname = NULL;
9580Sstevel@tonic-gate 	int			fd = -1;
9590Sstevel@tonic-gate 	int			len;
9600Sstevel@tonic-gate 	char			*fp;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	side = getmyside(*spp, ep);
9630Sstevel@tonic-gate 	if (side == MD_SIDEWILD) {
9640Sstevel@tonic-gate 		/* failed to find this node in the set */
9650Sstevel@tonic-gate 		mda_debug("Failed to find the side number\n");
9660Sstevel@tonic-gate 		return (METADEVADM_ERR);
9670Sstevel@tonic-gate 	}
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	if ((dnp = metadrivename(spp, ctd, ep)) == NULL) {
9700Sstevel@tonic-gate 		mda_debug("Failed to create a dnp for %s\n", ctd);
9710Sstevel@tonic-gate 		return (METADEVADM_ERR);
9720Sstevel@tonic-gate 	}
9730Sstevel@tonic-gate 	if (dnp->devid == NULL) {
9740Sstevel@tonic-gate 		/*
9750Sstevel@tonic-gate 		 * Disk does not have a devid! So cannot update the
9760Sstevel@tonic-gate 		 * devid within the replica.
9770Sstevel@tonic-gate 		 */
9780Sstevel@tonic-gate 		mda_debug("%s does not have a devid\n", dnp->cname);
9790Sstevel@tonic-gate 		if (dev_options & DEV_VERBOSE) {
9800Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
9810Sstevel@tonic-gate 			    "%s does not have a device id. Cannot update "
9820Sstevel@tonic-gate 			    "device id if none exists\n"), ctd);
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 		return (METADEVADM_ERR);
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	mda_debug("Devid update to: %s\n", dnp->devid);
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	/*
9900Sstevel@tonic-gate 	 * Check if we own the set, if we do then do some processing
9910Sstevel@tonic-gate 	 * on the replicas.
9920Sstevel@tonic-gate 	 */
9930Sstevel@tonic-gate 	if (meta_check_ownership(*spp, ep) == 0) {
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 		/* get the replicas */
9960Sstevel@tonic-gate 		if (metareplicalist(*spp, MD_BASICNAME_OK | PRINT_FAST, &rlp,
9970Sstevel@tonic-gate 		    ep) < 0)
9980Sstevel@tonic-gate 			return (METADEVADM_ERR);
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 		/* update the devids in the replicas if necessary */
10010Sstevel@tonic-gate 		if (replica_update_devid(rlp, dnp, setno, &old_cdevidp,
10020Sstevel@tonic-gate 		    ep) != METADEVADM_SUCCESS) {
10030Sstevel@tonic-gate 			metafreereplicalist(rlp);
10040Sstevel@tonic-gate 			return (METADEVADM_ERR);
10050Sstevel@tonic-gate 		}
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 		metafreereplicalist(rlp);
10080Sstevel@tonic-gate 	}
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	/*
10110Sstevel@tonic-gate 	 * If this is not the LOCAL set then need to update the LOCAL
10120Sstevel@tonic-gate 	 * replica with the new disk record.
10130Sstevel@tonic-gate 	 */
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	if (setno != MD_LOCAL_SET) {
10160Sstevel@tonic-gate 		mda_debug("Non-local set: %d side %d\n", setno, side);
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 		/*
10190Sstevel@tonic-gate 		 * Need to find the disk record within the set and then
10200Sstevel@tonic-gate 		 * update it.
10210Sstevel@tonic-gate 		 */
10220Sstevel@tonic-gate 		if ((dd =
10230Sstevel@tonic-gate 		    metaget_drivedesc(*spp, MD_FULLNAME_ONLY, ep)) == NULL) {
10240Sstevel@tonic-gate 			if (! mdisok(ep))
10250Sstevel@tonic-gate 				goto out;
10260Sstevel@tonic-gate 			/* no disks in the set - no point continuing */
10270Sstevel@tonic-gate 			mda_debug("No disks in diskset\n");
10280Sstevel@tonic-gate 			rval = METADEVADM_SUCCESS;
10290Sstevel@tonic-gate 			goto out;
10300Sstevel@tonic-gate 		}
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 		for (ddp = dd; ddp != NULL; ddp = ddp->dd_next) {
10330Sstevel@tonic-gate 			if (strncmp(ddp->dd_dnp->cname, dnp->cname,
10340Sstevel@tonic-gate 			    strlen(dnp->cname)) == 0)
10350Sstevel@tonic-gate 				break;
10360Sstevel@tonic-gate 		}
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 		if (ddp == NULL) {
10390Sstevel@tonic-gate 			/* failed to finddisk in the set */
10400Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
10410Sstevel@tonic-gate 			    "%s not found in set %s. Check your syntax\n"),
10420Sstevel@tonic-gate 			    ctd, (*spp)->setname);
10430Sstevel@tonic-gate 			(void) mddserror(ep, MDE_DS_DRIVENOTINSET, setno, NULL,
10440Sstevel@tonic-gate 			    ctd, (*spp)->setname);
10450Sstevel@tonic-gate 			goto out;
10460Sstevel@tonic-gate 		}
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 		/*
10490Sstevel@tonic-gate 		 * Now figure out the correct slice, for a diskset the slice
10500Sstevel@tonic-gate 		 * we care about is always the 'replica' slice.
10510Sstevel@tonic-gate 		 */
10520Sstevel@tonic-gate 		if (meta_replicaslice(dnp, &rep_slice, ep) != 0) {
10530Sstevel@tonic-gate 			mda_debug("Unable to find replica slice for %s\n",
10540Sstevel@tonic-gate 			    dnp->cname);
10550Sstevel@tonic-gate 			goto out;
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 		mda_debug("slice no: %d disk %s\n", rep_slice, dnp->cname);
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 		if ((np = metaslicename(dnp, rep_slice, ep)) == NULL) {
10610Sstevel@tonic-gate 			mda_debug("Unable to build namespace\n");
10620Sstevel@tonic-gate 			goto out;
10630Sstevel@tonic-gate 		}
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 		mda_debug("check: ctdname: %s\n", np->cname);
10660Sstevel@tonic-gate 		mda_debug("check: ctdname: %s\n", np->rname);
10670Sstevel@tonic-gate 		mda_debug("check: ctdname: %s\n", np->bname);
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 		if (!(dev_options & DEV_NOACTION)) {
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 			mda_debug("Updating record: key %d name %s\n",
10720Sstevel@tonic-gate 			    ddp->dd_dnp->side_names_key, np->cname);
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 			pathname = mda_getpath(np->bname);
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 			if (update_namespace(MD_LOCAL_SET, side + SKEW,
10770Sstevel@tonic-gate 			    np->cname, np->dev, ddp->dd_dnp->side_names_key,
10780Sstevel@tonic-gate 			    pathname, ep) != 0) {
10790Sstevel@tonic-gate 				goto out;
10800Sstevel@tonic-gate 			}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 			/*
10830Sstevel@tonic-gate 			 * Now update the devid entry as well, this works
10840Sstevel@tonic-gate 			 * correctly because the prior call to
10850Sstevel@tonic-gate 			 * update_namespace() above puts the correct dev_t
10860Sstevel@tonic-gate 			 * in the namespace which will then be resolved
10870Sstevel@tonic-gate 			 * to the new devid by the ioctl now called.
10880Sstevel@tonic-gate 			 */
10890Sstevel@tonic-gate 			nm.mde = mdnullerror;
10900Sstevel@tonic-gate 			nm.setno = MD_LOCAL_SET;
10910Sstevel@tonic-gate 			nm.side = side + SKEW;
10920Sstevel@tonic-gate 			nm.key = ddp->dd_dnp->side_names_key;
10930Sstevel@tonic-gate 			if (metaioctl(MD_SETNMDID, &nm, &nm.mde, NULL) != 0) {
10940Sstevel@tonic-gate 				(void) mdstealerror(ep, &nm.mde);
10950Sstevel@tonic-gate 				goto out;
10960Sstevel@tonic-gate 			}
10970Sstevel@tonic-gate 		}
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	if ((dev_options & DEV_LOCAL_SET) && (setno != MD_LOCAL_SET)) {
11010Sstevel@tonic-gate 		/*
11020Sstevel@tonic-gate 		 * Only want to update the local set so do not continue.
11030Sstevel@tonic-gate 		 */
11040Sstevel@tonic-gate 		rval = METADEVADM_SUCCESS;
11050Sstevel@tonic-gate 		goto out;
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	/*
11090Sstevel@tonic-gate 	 * Iterate through all of the metadevices looking for the
11100Sstevel@tonic-gate 	 * passed in ctd.  If found then update the devid
11110Sstevel@tonic-gate 	 */
11120Sstevel@tonic-gate 	(void) memset(&nm, '\0', sizeof (nm));
11130Sstevel@tonic-gate 	nm.key = MD_KEYWILD;
11140Sstevel@tonic-gate 	/* LINTED */
11150Sstevel@tonic-gate 	while (1) {
11160Sstevel@tonic-gate 		nm.mde = mdnullerror;
11170Sstevel@tonic-gate 		nm.setno = setno;
11180Sstevel@tonic-gate 		nm.side = side;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 		/* search each namespace entry */
11210Sstevel@tonic-gate 		if (metaioctl(MD_IOCNXTKEY_NM, &nm, &nm.mde, NULL) != 0) {
11220Sstevel@tonic-gate 			(void) mdstealerror(ep, &nm.mde);
11230Sstevel@tonic-gate 			rval = METADEVADM_ERR;
11240Sstevel@tonic-gate 			goto out;
11250Sstevel@tonic-gate 		}
11260Sstevel@tonic-gate 		if (nm.key == MD_KEYWILD) {
11270Sstevel@tonic-gate 			if (setno != MD_LOCAL_SET) {
11280Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
11290Sstevel@tonic-gate 				    "%s not found in set %s. Check your "
11300Sstevel@tonic-gate 				    "syntax\n"), ctd, (*spp)->setname);
11310Sstevel@tonic-gate 				goto out;
11320Sstevel@tonic-gate 			} else {
11330Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
11340Sstevel@tonic-gate 				    "%s not found in local set. "
11350Sstevel@tonic-gate 				    "Check your syntax\n"), ctd);
11360Sstevel@tonic-gate 				goto out;
11370Sstevel@tonic-gate 			}
11380Sstevel@tonic-gate 		}
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 		nm.devname = (uintptr_t)meta_getnmentbykey(setno, side, nm.key,
11410Sstevel@tonic-gate 		    NULL, NULL, NULL, ep);
11420Sstevel@tonic-gate 		if (nm.devname == NULL) {
11430Sstevel@tonic-gate 			rval = METADEVADM_ERR;
11440Sstevel@tonic-gate 			goto out;
11450Sstevel@tonic-gate 		}
11460Sstevel@tonic-gate 
1147*62Sjeanm 		diskname = getdiskname((char *)(uintptr_t)nm.devname);
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 		mda_debug("Checking %s with %s\n", diskname, dnp->cname);
11500Sstevel@tonic-gate 		if (strcmp(diskname, dnp->cname) != 0)
11510Sstevel@tonic-gate 			continue;
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 		mda_debug("Updating device %s in namespace\n",
1154*62Sjeanm 		    (char *)(uintptr_t)nm.devname);
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 		/*
11570Sstevel@tonic-gate 		 * found disk, does it have a devid within the namespace ?
11580Sstevel@tonic-gate 		 * It might not because it does not support devid's or was
11590Sstevel@tonic-gate 		 * put into the namespace when there was no devid support
11600Sstevel@tonic-gate 		 */
11610Sstevel@tonic-gate 		if ((devidp = has_devid(setno, side, nm.key, ep)) == NULL) {
11620Sstevel@tonic-gate 			mda_debug("%s has no devid in the namespace",
1163*62Sjeanm 			    (char *)(uintptr_t)nm.devname);
11640Sstevel@tonic-gate 			if (dev_options & DEV_VERBOSE) {
11650Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
11660Sstevel@tonic-gate 				"SVM has no device id for "
1167*62Sjeanm 				"%s, cannot update.\n"),
1168*62Sjeanm 				(char *)(uintptr_t)nm.devname);
11690Sstevel@tonic-gate 			}
11700Sstevel@tonic-gate 			continue; /* no devid. go on to next */
11710Sstevel@tonic-gate 		}
11720Sstevel@tonic-gate 		if (old_cdevidp == NULL) {
11730Sstevel@tonic-gate 			old_cdevidp = devid_str_encode(devidp, NULL);
11740Sstevel@tonic-gate 		}
11750Sstevel@tonic-gate 		free(devidp);
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 		/*
11780Sstevel@tonic-gate 		 * has devid so update namespace, note the key has been set
11790Sstevel@tonic-gate 		 * by the prior MD_IOCNXTKEY_NM ioctl.
11800Sstevel@tonic-gate 		 */
11810Sstevel@tonic-gate 		nm.mde = mdnullerror;
11820Sstevel@tonic-gate 		nm.setno = setno;
11830Sstevel@tonic-gate 		nm.side = side;
11840Sstevel@tonic-gate 		if (!(dev_options & DEV_NOACTION)) {
11850Sstevel@tonic-gate 			/*
11860Sstevel@tonic-gate 			 * The call below may fail if the -u option is being
11870Sstevel@tonic-gate 			 * used to update a disk that has been replaced.
11880Sstevel@tonic-gate 			 * The -u option to metadevadm should not be used
11890Sstevel@tonic-gate 			 * for this purpose because we trust the dev_t of
11900Sstevel@tonic-gate 			 * the device in the replica and if we have replaced
11910Sstevel@tonic-gate 			 * the device and it is a fibre one then the dev_t
11920Sstevel@tonic-gate 			 * will have changed. This means we end up looking for
11930Sstevel@tonic-gate 			 * the devid of a non-existant disk and we subsequently
11940Sstevel@tonic-gate 			 * fail with NODEVID.
11950Sstevel@tonic-gate 			 */
11960Sstevel@tonic-gate 			if (metaioctl(MD_SETNMDID, &nm,
11970Sstevel@tonic-gate 					&nm.mde, NULL) != 0) {
11980Sstevel@tonic-gate 				if (dev_options & DEV_VERBOSE) {
11990Sstevel@tonic-gate 					mda_print(dgettext(TEXT_DOMAIN,
12000Sstevel@tonic-gate 					    "SVM failed to update the device "
12010Sstevel@tonic-gate 					    "id for %s probably due to both "
12020Sstevel@tonic-gate 					    "devt and device id changing.\n"),
1203*62Sjeanm 					    (char *)(uintptr_t)nm.devname);
12040Sstevel@tonic-gate 				}
12050Sstevel@tonic-gate 				(void) mdstealerror(ep, &nm.mde);
12060Sstevel@tonic-gate 				mde_perror(ep, "");
12070Sstevel@tonic-gate 				rval = METADEVADM_ERR;
12080Sstevel@tonic-gate 				goto out;
12090Sstevel@tonic-gate 			}
12100Sstevel@tonic-gate 		}
12110Sstevel@tonic-gate 		if (old_cdevidp == NULL) {
12120Sstevel@tonic-gate 			rval = METADEVADM_ERR;
12130Sstevel@tonic-gate 			goto out;
12140Sstevel@tonic-gate 		}
12150Sstevel@tonic-gate 		break;
12160Sstevel@tonic-gate 	} /* end while */
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	mda_print(dgettext(TEXT_DOMAIN,
12190Sstevel@tonic-gate 		    "Updating Solaris Volume Manager device relocation "
12200Sstevel@tonic-gate 		    "information for %s\n"), ctd);
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	mda_print(dgettext(TEXT_DOMAIN,
12230Sstevel@tonic-gate 	    "Old device reloc information:\n\t%s\n"), old_cdevidp);
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	len = strlen(dnp->rname) + strlen("s0");
12260Sstevel@tonic-gate 	if ((fp = (char *)Malloc(len + 1)) == NULL) {
12270Sstevel@tonic-gate 		mda_print(dgettext(TEXT_DOMAIN,
12280Sstevel@tonic-gate 		    "insufficient memory, device Reloc info not "
12290Sstevel@tonic-gate 		    "available\n"));
12300Sstevel@tonic-gate 	} else {
12310Sstevel@tonic-gate 		(void) snprintf(fp, len + 1, "%ss0", dnp->rname);
12320Sstevel@tonic-gate 		if ((fd = open(fp, O_RDONLY|O_NDELAY)) < 0) {
12330Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
12340Sstevel@tonic-gate 			    "Open of %s failed\n"), fp);
12350Sstevel@tonic-gate 		} else {
12360Sstevel@tonic-gate 			int		rc = -1;
12370Sstevel@tonic-gate 			ddi_devid_t	devid1 = NULL;
12380Sstevel@tonic-gate 			char		*cdevidp;
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 			rc = devid_get(fd, &devid1);
12410Sstevel@tonic-gate 			if (close(fd) < 0) {
12420Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
12430Sstevel@tonic-gate 				    "Close of %s failed\n"), fp);
12440Sstevel@tonic-gate 			}
12450Sstevel@tonic-gate 			if (rc != 0) {
12460Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
12470Sstevel@tonic-gate 				    "Unable to obtain device "
12480Sstevel@tonic-gate 				    "Reloc info for %s\n"), fp);
12490Sstevel@tonic-gate 			} else {
12500Sstevel@tonic-gate 				cdevidp = devid_str_encode(devid1, NULL);
12510Sstevel@tonic-gate 				if (cdevidp == NULL) {
12520Sstevel@tonic-gate 					mda_print(dgettext(TEXT_DOMAIN,
12530Sstevel@tonic-gate 					    "Unable to print "
12540Sstevel@tonic-gate 					    "device Reloc info for %s\n"), fp);
12550Sstevel@tonic-gate 				} else {
12560Sstevel@tonic-gate 					mda_print(dgettext(TEXT_DOMAIN,
12570Sstevel@tonic-gate 					    "New device reloc "
12580Sstevel@tonic-gate 					    "information:\n\t%s\n"), cdevidp);
12590Sstevel@tonic-gate 					devid_str_free(cdevidp);
12600Sstevel@tonic-gate 				}
12610Sstevel@tonic-gate 				devid_free(devid1);
12620Sstevel@tonic-gate 			}
12630Sstevel@tonic-gate 		}
12640Sstevel@tonic-gate 		Free(fp);
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	rval = METADEVADM_SUCCESS;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate out:
12700Sstevel@tonic-gate 	if (diskname)
12710Sstevel@tonic-gate 		Free(diskname);
12720Sstevel@tonic-gate 	if (pathname)
12730Sstevel@tonic-gate 		Free(pathname);
12740Sstevel@tonic-gate 	if (old_cdevidp) {
12750Sstevel@tonic-gate 		devid_str_free(old_cdevidp);
12760Sstevel@tonic-gate 	}
12770Sstevel@tonic-gate 	return (rval);
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate /*
12820Sstevel@tonic-gate  * Check the ctd name of the disk to see if the disk has moved. If it
12830Sstevel@tonic-gate  * has moved then the newname is returned in 'newname', it is up to
12840Sstevel@tonic-gate  * the caller to free the memory associated with it.
12850Sstevel@tonic-gate  *
12860Sstevel@tonic-gate  * RETURN
12870Sstevel@tonic-gate  *	METADEVADM_ERR		error
12880Sstevel@tonic-gate  *	METADEVADM_SUCCESS	success
12890Sstevel@tonic-gate  *	METADEVADM_DISKMOVE	success, and the disk has moved
12900Sstevel@tonic-gate  *	METADEVADM_DSKNAME_ERR	error creating the disk name structures.
12910Sstevel@tonic-gate  */
12920Sstevel@tonic-gate int
12930Sstevel@tonic-gate meta_upd_ctdnames(
12940Sstevel@tonic-gate 	mdsetname_t	**spp,
12950Sstevel@tonic-gate 	set_t		setno,
12960Sstevel@tonic-gate 	side_t		sideno,
12970Sstevel@tonic-gate 	mddrivename_t	*dnp,
12980Sstevel@tonic-gate 	char		**newname,
12990Sstevel@tonic-gate 	md_error_t	*ep
13000Sstevel@tonic-gate )
13010Sstevel@tonic-gate {
13020Sstevel@tonic-gate 	char		*drvnmp;
13030Sstevel@tonic-gate 	int		i;
13040Sstevel@tonic-gate 	minor_t		mnum = 0;
13050Sstevel@tonic-gate 	md_dev64_t	dev = 0;
13060Sstevel@tonic-gate 	dev_t		small_dev = (dev_t)NODEV;
13070Sstevel@tonic-gate 	mdnm_params_t	nm;
13080Sstevel@tonic-gate 	char		*pathname;
13090Sstevel@tonic-gate 	char		*minor_name = NULL;
13100Sstevel@tonic-gate 	ddi_devid_t	devidp;
13110Sstevel@tonic-gate 	devid_nmlist_t	*disklist = NULL;
13120Sstevel@tonic-gate 	int		ret = 0;
13130Sstevel@tonic-gate 	mdsidenames_t	*snp;
13140Sstevel@tonic-gate 	int		match_type;
13150Sstevel@tonic-gate 	int		search_number = -1;
13160Sstevel@tonic-gate 	char		*search_type = NULL;
13170Sstevel@tonic-gate 	char		*search_path = NULL;
13180Sstevel@tonic-gate 	uint_t		rep_slice;
13190Sstevel@tonic-gate 	mddrivename_t	*newdnp;
13200Sstevel@tonic-gate 	mdname_t	*np;
13210Sstevel@tonic-gate 	mdsetname_t	*sp = *spp;
13220Sstevel@tonic-gate 	md_set_desc	*sd;
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	/*
13250Sstevel@tonic-gate 	 * setno should always be 0 but we're going to
13260Sstevel@tonic-gate 	 * check for multi-node diskset and return if it is one.
13270Sstevel@tonic-gate 	 */
13280Sstevel@tonic-gate 	if (!metaislocalset(sp)) {
13290Sstevel@tonic-gate 		if ((sd = metaget_setdesc(sp, ep)) == NULL)
13300Sstevel@tonic-gate 			return (METADEVADM_ERR);
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 		if (MD_MNSET_DESC(sd))
13330Sstevel@tonic-gate 			return (METADEVADM_SUCCESS);
13340Sstevel@tonic-gate 	}
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	if (dnp->devid == NULL) {
13370Sstevel@tonic-gate 		/* no devid, nothing can be done */
13380Sstevel@tonic-gate 		mda_debug("meta_upd_ctdnames: %s has no devid\n", dnp->cname);
13390Sstevel@tonic-gate 		if (dev_options & DEV_VERBOSE) {
13400Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
13410Sstevel@tonic-gate 			    "%s has no devid, cannot detect "
13420Sstevel@tonic-gate 			    "disk movement for this disk.\n"), dnp->cname);
13430Sstevel@tonic-gate 		}
13440Sstevel@tonic-gate 		return (ret);
13450Sstevel@tonic-gate 	}
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	/*
13480Sstevel@tonic-gate 	 * Find the correct side name for the disk. There is a sidename
13490Sstevel@tonic-gate 	 * for each host associated with the diskset.
13500Sstevel@tonic-gate 	 */
13510Sstevel@tonic-gate 	for (snp = dnp->side_names; snp != NULL; snp = snp->next) {
13520Sstevel@tonic-gate 		mda_debug("meta_upd_ctdnames: %s %d args: setno %d sideno %d\n",
13530Sstevel@tonic-gate 		    snp->cname, snp->sideno, setno, sideno);
13540Sstevel@tonic-gate 		/* only use SKEW for the local replica */
13550Sstevel@tonic-gate 		if (setno == 0) {
13560Sstevel@tonic-gate 			if (snp->sideno + SKEW == sideno)
13570Sstevel@tonic-gate 				break;
13580Sstevel@tonic-gate 		} else {
13590Sstevel@tonic-gate 			if (snp->sideno == sideno)
13600Sstevel@tonic-gate 				break;
13610Sstevel@tonic-gate 		}
13620Sstevel@tonic-gate 	}
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	if (snp == NULL) {
13650Sstevel@tonic-gate 		/*
13660Sstevel@tonic-gate 		 * Failed to find the side name, this should not
13670Sstevel@tonic-gate 		 * be possible. However if it does happen this is an
13680Sstevel@tonic-gate 		 * indication of an inconsistant replica - something
13690Sstevel@tonic-gate 		 * might have gone wrong during an add or a delete of
13700Sstevel@tonic-gate 		 * a host.
13710Sstevel@tonic-gate 		 */
13720Sstevel@tonic-gate 		mda_debug("Unable to find the side information for disk %s",
13730Sstevel@tonic-gate 		    dnp->cname);
13740Sstevel@tonic-gate 		(void) mddserror(ep, MDE_DS_HOSTNOSIDE, (*spp)->setno, mynode(),
13750Sstevel@tonic-gate 		    NULL, dnp->cname);
13760Sstevel@tonic-gate 		return (METADEVADM_ERR);
13770Sstevel@tonic-gate 	}
13780Sstevel@tonic-gate 	/*
13790Sstevel@tonic-gate 	 * Find the type of device we are to be searching on
13800Sstevel@tonic-gate 	 */
13810Sstevel@tonic-gate 	search_number = mda_findpath(snp->cname);
13820Sstevel@tonic-gate 	if (search_number == -1) {
13830Sstevel@tonic-gate 		search_path = "/dev";
13840Sstevel@tonic-gate 		search_type = DEVID_MINOR_NAME_ALL;
13850Sstevel@tonic-gate 	} else {
13860Sstevel@tonic-gate 		search_path = plist[search_number].search_path;
13870Sstevel@tonic-gate 		search_type = plist[search_number].search_type;
13880Sstevel@tonic-gate 	}
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	mda_debug("Search path :%s searth_type: %x\n",
13910Sstevel@tonic-gate 	    search_path, (int)search_type);
13920Sstevel@tonic-gate 	(void) memset(&nm, '\0', sizeof (nm));
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	nm.mde = mdnullerror;
13950Sstevel@tonic-gate 	nm.setno = setno;
13960Sstevel@tonic-gate 	nm.side = sideno;
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	/*
13990Sstevel@tonic-gate 	 * Get the devname from the name space.
14000Sstevel@tonic-gate 	 */
14010Sstevel@tonic-gate 	if ((nm.devname = (uintptr_t)meta_getnmentbykey(setno, sideno,
14020Sstevel@tonic-gate 	    dnp->side_names_key, &drvnmp, &mnum, &dev, ep)) == NULL) {
14030Sstevel@tonic-gate 		return (METADEVADM_ERR);
14040Sstevel@tonic-gate 	}
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	ret = devid_str_decode(dnp->devid, &devidp, &minor_name);
14070Sstevel@tonic-gate 	devid_str_free(minor_name);
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	if (ret != 0) {
14100Sstevel@tonic-gate 		/*
14110Sstevel@tonic-gate 		 * Failed to encode the devid.
14120Sstevel@tonic-gate 		 */
14130Sstevel@tonic-gate 		devid_free(devidp);
14140Sstevel@tonic-gate 		return (METADEVADM_ERR);
14150Sstevel@tonic-gate 	}
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	/*
14180Sstevel@tonic-gate 	 * Use the stored devid to find the existing device node and check
14190Sstevel@tonic-gate 	 * to see if the disk has moved. Use the raw devices as the name
14200Sstevel@tonic-gate 	 * of the disk is stored as the raw device, if this is not done
14210Sstevel@tonic-gate 	 * then the disk will not be found.
14220Sstevel@tonic-gate 	 */
14230Sstevel@tonic-gate 	ret = meta_deviceid_to_nmlist(search_path, devidp,
14240Sstevel@tonic-gate 	    search_type, &disklist);
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	if (ret != 0) {
14270Sstevel@tonic-gate 		if (dev_options & DEV_VERBOSE) {
14280Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
14290Sstevel@tonic-gate 			    "Device ID %s last associated with "
14300Sstevel@tonic-gate 			    "disk %s no longer found in system\n"),
14310Sstevel@tonic-gate 			    dnp->devid, dnp->cname);
14320Sstevel@tonic-gate 		}
14330Sstevel@tonic-gate 		devid_free(devidp);
14340Sstevel@tonic-gate 		devid_free_nmlist(disklist);
14350Sstevel@tonic-gate 		return (METADEVADM_SUCCESS);
14360Sstevel@tonic-gate 	}
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	small_dev = meta_cmpldev(dev);
14390Sstevel@tonic-gate 	mda_debug("Old device lookup: %s (%p)\n",
1440*62Sjeanm 			(char *)(uintptr_t)nm.devname, (void *)small_dev);
14410Sstevel@tonic-gate 	/*
14420Sstevel@tonic-gate 	 * Check to see if the returned disk matches the stored one
14430Sstevel@tonic-gate 	 */
14440Sstevel@tonic-gate 	for (i = 0; disklist[i].dev != NODEV; i++) {
14450Sstevel@tonic-gate 		match_type = 0;
14460Sstevel@tonic-gate 		mda_debug("From devid lookup: %s (%p)\n",
14470Sstevel@tonic-gate 				disklist[i].devname, (void *)disklist[i].dev);
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 		if (disklist[i].dev == small_dev) {
14500Sstevel@tonic-gate 			match_type |= DEV_MATCH;
14510Sstevel@tonic-gate 		}
14520Sstevel@tonic-gate 
1453*62Sjeanm 		if (strncmp((char *)(uintptr_t)nm.devname, disklist[i].devname,
1454*62Sjeanm 		    strlen((char *)(uintptr_t)nm.devname)) == 0) {
14550Sstevel@tonic-gate 			match_type |= NAME_MATCH;
14560Sstevel@tonic-gate 		}
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 		if (match_type != 0)
14590Sstevel@tonic-gate 			break;
14600Sstevel@tonic-gate 	}
14610Sstevel@tonic-gate 	devid_free(devidp);
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 	mda_debug("meta_upd_ctdnames: match: %x i: %d\n", match_type, i);
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	if (match_type == (DEV_MATCH|NAME_MATCH)) {
14660Sstevel@tonic-gate 		/* no change */
14670Sstevel@tonic-gate 		devid_free_nmlist(disklist);
14680Sstevel@tonic-gate 		return (METADEVADM_SUCCESS);
14690Sstevel@tonic-gate 	}
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	/* No match found - use the first entry in disklist */
14720Sstevel@tonic-gate 	if (disklist[i].dev == NODEV)
14730Sstevel@tonic-gate 		i = 0;
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	if (!(match_type & DEV_MATCH)) {
14760Sstevel@tonic-gate 		/* did not match on the dev, so dev_t has changed */
14770Sstevel@tonic-gate 		mda_debug("Did not match on dev: %p %p\n",
14780Sstevel@tonic-gate 		    (void *) small_dev, (void *) disklist[i].dev);
14790Sstevel@tonic-gate 		dev = meta_expldev(disklist[i].dev);
14800Sstevel@tonic-gate 	}
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	if (!(match_type & NAME_MATCH)) {
14830Sstevel@tonic-gate 		mda_debug("Did not match on name: %s (%p)\n",
1484*62Sjeanm 		    (char *)(uintptr_t)nm.devname, (void *) disklist[i].dev);
14850Sstevel@tonic-gate 	}
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	/*
14880Sstevel@tonic-gate 	 * If here, then the name in the disklist is the one we
14890Sstevel@tonic-gate 	 * want in any case so use it.
14900Sstevel@tonic-gate 	 */
14910Sstevel@tonic-gate 	mda_debug("devname: %s\n", disklist[i].devname);
14920Sstevel@tonic-gate 	/*
14930Sstevel@tonic-gate 	 * Need to remove the slice as metadrivename() expects a diskname
14940Sstevel@tonic-gate 	 */
14950Sstevel@tonic-gate 	stripS(disklist[i].devname);
14960Sstevel@tonic-gate 	/*
14970Sstevel@tonic-gate 	 * Build an mddrivename_t to use
14980Sstevel@tonic-gate 	 */
14990Sstevel@tonic-gate 	if ((newdnp = metadrivename(spp, disklist[i].devname, ep)) == NULL) {
15000Sstevel@tonic-gate 		mda_debug("Unable to make a dnp out of %s\n",
15010Sstevel@tonic-gate 		    disklist[i].devname);
15020Sstevel@tonic-gate 		return (METADEVADM_DSKNAME_ERR);
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate 	/*
15050Sstevel@tonic-gate 	 * Need to find the correct slice used for the replica
15060Sstevel@tonic-gate 	 */
15070Sstevel@tonic-gate 	if (meta_replicaslice(newdnp, &rep_slice, ep) != 0) {
15080Sstevel@tonic-gate 		return (METADEVADM_DSKNAME_ERR);
15090Sstevel@tonic-gate 	}
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	if ((np = metaslicename(newdnp, rep_slice, ep)) == NULL) {
15120Sstevel@tonic-gate 		mda_debug("Failed to build an np for %s\n", dnp->rname);
15130Sstevel@tonic-gate 		return (METADEVADM_DSKNAME_ERR);
15140Sstevel@tonic-gate 	}
15150Sstevel@tonic-gate 	mda_debug("check: cname: %s\n", np->cname);
15160Sstevel@tonic-gate 	mda_debug("check: rname: %s\n", np->rname);
15170Sstevel@tonic-gate 	mda_debug("check: bname: %s\n", np->bname);
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	if (newname != NULL)
15200Sstevel@tonic-gate 		*newname = Strdup(np->bname);
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	if (!(dev_options & DEV_NOACTION)) {
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 		mda_debug("update namespace\n");
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 		/* get the block path */
15270Sstevel@tonic-gate 		pathname = mda_getpath(np->bname);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 		if (update_namespace(setno, sideno, np->cname,
15300Sstevel@tonic-gate 		    dev, dnp->side_names_key, pathname, ep) != 0) {
15310Sstevel@tonic-gate 			/* finished with the list so return the memory */
15320Sstevel@tonic-gate 			Free(pathname);
15330Sstevel@tonic-gate 			devid_free_nmlist(disklist);
15340Sstevel@tonic-gate 			return (METADEVADM_ERR);
15350Sstevel@tonic-gate 		}
15360Sstevel@tonic-gate 	}
15370Sstevel@tonic-gate 	/* finished with the list so return the memory */
15380Sstevel@tonic-gate 	Free(pathname);
15390Sstevel@tonic-gate 	devid_free_nmlist(disklist);
15400Sstevel@tonic-gate 	ret = METADEVADM_DISKMOVE;
15410Sstevel@tonic-gate 	return (ret);
15420Sstevel@tonic-gate }
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate int
15450Sstevel@tonic-gate meta_fixdevid(
15460Sstevel@tonic-gate 	mdsetname_t	*sp,
15470Sstevel@tonic-gate 	mddevopts_t	options,
15480Sstevel@tonic-gate 	char		*diskname,
15490Sstevel@tonic-gate 	md_error_t	*ep
15500Sstevel@tonic-gate )
15510Sstevel@tonic-gate {
15520Sstevel@tonic-gate 	set_t		setno = sp->setno;
15530Sstevel@tonic-gate 	int		ret = 0;
15540Sstevel@tonic-gate 	char		*pathname = NULL;
15550Sstevel@tonic-gate 	mdsetname_t	*local_sp = NULL;
15560Sstevel@tonic-gate 	md_drive_desc	*d = NULL;
15570Sstevel@tonic-gate 	char		*newname = NULL;
15580Sstevel@tonic-gate 	md_drive_desc	*dd;
15590Sstevel@tonic-gate 	side_t		sideno;
15600Sstevel@tonic-gate 	md_set_desc	*sd;
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	/* if MN diskset just return */
15630Sstevel@tonic-gate 	if (!metaislocalset(sp)) {
15640Sstevel@tonic-gate 		if ((sd = metaget_setdesc(sp, ep)) == NULL) {
15650Sstevel@tonic-gate 			return (METADEVADM_ERR);
15660Sstevel@tonic-gate 		}
15670Sstevel@tonic-gate 		if (MD_MNSET_DESC(sd))
15680Sstevel@tonic-gate 			return (METADEVADM_SUCCESS);
15690Sstevel@tonic-gate 	}
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	dev_options |= options;
15720Sstevel@tonic-gate 	mda_debug("dev_options: %x\n", dev_options);
15730Sstevel@tonic-gate 	if (dev_options & DEV_RELOAD) {
15740Sstevel@tonic-gate 		/*
15750Sstevel@tonic-gate 		 * If it's not the local set we need to check the local
15760Sstevel@tonic-gate 		 * namespace to see if disks have moved as it contains
15770Sstevel@tonic-gate 		 * entries for the disks in the set.
15780Sstevel@tonic-gate 		 */
15790Sstevel@tonic-gate 		if (setno != MD_LOCAL_SET) {
15800Sstevel@tonic-gate 			if ((dd = metaget_drivedesc(sp, MD_BASICNAME_OK |
15810Sstevel@tonic-gate 			    PRINT_FAST, ep)) == NULL) {
15820Sstevel@tonic-gate 				mde_perror(ep, "");
15830Sstevel@tonic-gate 				mdclrerror(ep);
15840Sstevel@tonic-gate 				return (METADEVADM_ERR);
15850Sstevel@tonic-gate 			}
15860Sstevel@tonic-gate 			local_sp = metasetname(MD_LOCAL_NAME, ep);
15870Sstevel@tonic-gate 			sideno = getmyside(sp, ep) + SKEW;
15880Sstevel@tonic-gate 			for (d = dd; d != NULL; d = d->dd_next) {
15890Sstevel@tonic-gate 				/*
15900Sstevel@tonic-gate 				 * Actually do the check of the disks.
15910Sstevel@tonic-gate 				 */
15920Sstevel@tonic-gate 				ret = meta_upd_ctdnames(&local_sp, 0, sideno,
15930Sstevel@tonic-gate 				    d->dd_dnp, &newname, ep);
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 				if ((ret == METADEVADM_ERR) ||
15960Sstevel@tonic-gate 				    (ret == METADEVADM_DSKNAME_ERR)) {
15970Sstevel@tonic-gate 					/* check failed in unknown manner */
15980Sstevel@tonic-gate 					mda_debug("meta_upd_ctdnames failed\n");
15990Sstevel@tonic-gate 					return (METADEVADM_ERR);
16000Sstevel@tonic-gate 				}
16010Sstevel@tonic-gate 			}
16020Sstevel@tonic-gate 		}
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 		/* do a reload of the devid namespace */
16050Sstevel@tonic-gate 		ret = pathname_reload(&sp, setno, ep);
16060Sstevel@tonic-gate 	} else if (dev_options & DEV_UPDATE) {
16070Sstevel@tonic-gate 		pathname = getdiskname(diskname);
16080Sstevel@tonic-gate 		ret = devid_update(&sp, setno, pathname, ep);
16090Sstevel@tonic-gate 		free(pathname);
16100Sstevel@tonic-gate 	}
16110Sstevel@tonic-gate 	return (ret);
16120Sstevel@tonic-gate }
1613