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
51945Sjeanm  * Common Development and Distribution License (the "License").
61945Sjeanm  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221945Sjeanm  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include	<stdio.h>
290Sstevel@tonic-gate #include	<stdarg.h>
300Sstevel@tonic-gate #include	<ctype.h>
310Sstevel@tonic-gate #include	<sys/fcntl.h>
320Sstevel@tonic-gate #include	<sys/types.h>
330Sstevel@tonic-gate #include	<devid.h>
340Sstevel@tonic-gate #include	<ftw.h>
350Sstevel@tonic-gate #include	<string.h>
360Sstevel@tonic-gate #include	<mdiox.h>
370Sstevel@tonic-gate #include	<sys/lvm/mdio.h>
380Sstevel@tonic-gate #include 	<meta.h>
390Sstevel@tonic-gate #include 	<syslog.h>
400Sstevel@tonic-gate #include	<sdssc.h>
41*2291Stn143363 #include	<libdevinfo.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 /*
180*2291Stn143363  * meta_update_devtree -- Update the /dev/md namespace for metadevices.
181*2291Stn143363  *
182*2291Stn143363  * Only update the specific link if a valid minor(not NODEV) is given.
183*2291Stn143363  * Otherwise, update the entire /dev/md .
184*2291Stn143363  */
185*2291Stn143363 
186*2291Stn143363 int
187*2291Stn143363 meta_update_devtree(minor_t mnum)
188*2291Stn143363 {
189*2291Stn143363 	char	nodename[40];
190*2291Stn143363 	di_devlink_handle_t	hdl;
191*2291Stn143363 
192*2291Stn143363 	/*
193*2291Stn143363 	 * di_devlink_init() returns once the /dev links have been
194*2291Stn143363 	 * updated(created or removed). If di_devlink_init returns
195*2291Stn143363 	 * a NULL, the link operation failed.
196*2291Stn143363 	 *
197*2291Stn143363 	 * Use the enhanced di_devlink_init interface if the mnum
198*2291Stn143363 	 * is available.
199*2291Stn143363 	 */
200*2291Stn143363 	if (mnum == NODEV) {
201*2291Stn143363 		/*
202*2291Stn143363 		 * NOTE: This will take a _long_ time for large numbers
203*2291Stn143363 		 * of metadevices.
204*2291Stn143363 		 */
205*2291Stn143363 		hdl = di_devlink_init("md", DI_MAKE_LINK);
206*2291Stn143363 	} else {
207*2291Stn143363 		/* Call di_devlink_init twice, for block and raw devices */
208*2291Stn143363 		(void) sprintf(nodename, "/pseudo/md@0:%lu,%lu,raw",
209*2291Stn143363 		    MD_MIN2SET(mnum), MD_MIN2UNIT(mnum));
210*2291Stn143363 		hdl = di_devlink_init(nodename, DI_MAKE_LINK);
211*2291Stn143363 
212*2291Stn143363 		if (hdl == NULL)
213*2291Stn143363 			return (-1);
214*2291Stn143363 		else
215*2291Stn143363 			(void) di_devlink_fini(&hdl);
216*2291Stn143363 
217*2291Stn143363 		(void) sprintf(nodename, "/pseudo/md@0:%lu,%lu,blk",
218*2291Stn143363 		    MD_MIN2SET(mnum), MD_MIN2UNIT(mnum));
219*2291Stn143363 		hdl = di_devlink_init(nodename, DI_MAKE_LINK);
220*2291Stn143363 	}
221*2291Stn143363 
222*2291Stn143363 	if (hdl != NULL) {
223*2291Stn143363 		(void) di_devlink_fini(&hdl);
224*2291Stn143363 		return (0);
225*2291Stn143363 	}
226*2291Stn143363 
227*2291Stn143363 	return (-1);
228*2291Stn143363 }
229*2291Stn143363 
230*2291Stn143363 /*
2310Sstevel@tonic-gate  * update_locator_namespace -- Contains the ioctl call that will update
2320Sstevel@tonic-gate  *		the ctds and pathname (ie. /dev/dsk etc) within the
2330Sstevel@tonic-gate  *		locator block namespace.
2340Sstevel@tonic-gate  *
2350Sstevel@tonic-gate  * RETURN
2360Sstevel@tonic-gate  *	METADEVADM_ERR		ioctl failed and ep is updated with the error
2370Sstevel@tonic-gate  *	METADEVADM_SUCCESS	success
2380Sstevel@tonic-gate  */
2390Sstevel@tonic-gate static int
2400Sstevel@tonic-gate update_locator_namespace(
2410Sstevel@tonic-gate 	set_t		setno,
2420Sstevel@tonic-gate 	side_t		sideno,
2430Sstevel@tonic-gate 	char		*devname,
2440Sstevel@tonic-gate 	md_dev64_t	dev,
2450Sstevel@tonic-gate 	char		*pname,
2460Sstevel@tonic-gate 	md_error_t	*ep
2470Sstevel@tonic-gate )
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	mdnm_params_t	nm;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	(void) memset(&nm, '\0', sizeof (nm));
2520Sstevel@tonic-gate 	nm.mde = mdnullerror;
2530Sstevel@tonic-gate 	nm.setno = setno;
2540Sstevel@tonic-gate 	nm.side = sideno;
2550Sstevel@tonic-gate 	nm.devname = (uintptr_t)devname;
2560Sstevel@tonic-gate 	nm.devname_len = strlen(devname);
2570Sstevel@tonic-gate 	nm.devt = dev;
2580Sstevel@tonic-gate 	nm.pathname = (uintptr_t)pname;
2590Sstevel@tonic-gate 	nm.pathname_len = strlen(pname);
2600Sstevel@tonic-gate 	if (metaioctl(MD_IOCUPD_LOCNM, &nm, &nm.mde, NULL) != 0) {
2610Sstevel@tonic-gate 		(void) mdstealerror(ep, &nm.mde);
2620Sstevel@tonic-gate 		return (METADEVADM_ERR);
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 	return (METADEVADM_SUCCESS);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate  * update_namespace -- Contains the ioctl call that will update the
2690Sstevel@tonic-gate  * 	device name and pathname in the namespace area.
2700Sstevel@tonic-gate  *
2710Sstevel@tonic-gate  * RETURN
2720Sstevel@tonic-gate  *	METADEVADM_ERR		ioctl failed and ep is updated with the error
2730Sstevel@tonic-gate  *	METADEVADM_SUCCESS	success
2740Sstevel@tonic-gate  */
2750Sstevel@tonic-gate static int
2760Sstevel@tonic-gate update_namespace(
2770Sstevel@tonic-gate 	set_t		setno,
2780Sstevel@tonic-gate 	side_t		sideno,
2790Sstevel@tonic-gate 	char		*devname,
2800Sstevel@tonic-gate 	md_dev64_t	dev,
2810Sstevel@tonic-gate 	mdkey_t		key,
2820Sstevel@tonic-gate 	char		*pname,
2830Sstevel@tonic-gate 	md_error_t	*ep
2840Sstevel@tonic-gate )
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	mdnm_params_t	nm;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	(void) memset(&nm, '\0', sizeof (nm));
2890Sstevel@tonic-gate 	nm.mde = mdnullerror;
2900Sstevel@tonic-gate 	nm.setno = setno;
2910Sstevel@tonic-gate 	nm.side = sideno;
2920Sstevel@tonic-gate 	nm.devname = (uintptr_t)devname;
2930Sstevel@tonic-gate 	nm.devname_len = strlen(devname);
2940Sstevel@tonic-gate 	nm.mnum = meta_getminor(dev);
2950Sstevel@tonic-gate 	nm.key = key;
2960Sstevel@tonic-gate 	nm.pathname = (uintptr_t)pname;
2970Sstevel@tonic-gate 	nm.pathname_len = strlen(pname);
2980Sstevel@tonic-gate 	if (metaioctl(MD_IOCUPD_NM, &nm, &nm.mde, NULL) != 0) {
2990Sstevel@tonic-gate 		(void) mdstealerror(ep, &nm.mde);
3000Sstevel@tonic-gate 		return (METADEVADM_ERR);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 	return (METADEVADM_SUCCESS);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate  * stripS - Strip s<digits> off the end of the ctds name if it exists
3070Sstevel@tonic-gate  */
3080Sstevel@tonic-gate static void
3090Sstevel@tonic-gate stripS(char *name)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	char	*p;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/* gobble number and 's' */
3140Sstevel@tonic-gate 	p = name + strlen(name) - 1;
3150Sstevel@tonic-gate 	for (; (p > name); --p) {
3160Sstevel@tonic-gate 		if (!isdigit(*p))
3170Sstevel@tonic-gate 			break;
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	if (*p == 's') {
3210Sstevel@tonic-gate 		*p = '\0';
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate  * getdiskname -- to be used when scanning the input from the -u arg.
3270Sstevel@tonic-gate  * 	This routine will strip off input that is anything but cxtxdx.
3280Sstevel@tonic-gate  *	ie. it will call stripS to get rid of slice info. Will also
3290Sstevel@tonic-gate  *	strip off /dev/dsk, /dev/rdsk, /dev/ap/dsk, /dev/ap/rdsk,
3300Sstevel@tonic-gate  *	/dev/did/dsk, or /dev/did/rdsk. The caller will need to free
3310Sstevel@tonic-gate  *	the return value.
3320Sstevel@tonic-gate  *
3330Sstevel@tonic-gate  * RETURN
3340Sstevel@tonic-gate  *	 string that has the disk name in it ie. c0t0d0
3350Sstevel@tonic-gate  */
3360Sstevel@tonic-gate static char *
3370Sstevel@tonic-gate getdiskname(
3380Sstevel@tonic-gate 	char	*name
3390Sstevel@tonic-gate )
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	char	*p;
3420Sstevel@tonic-gate 	char	*diskname;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/* regular device */
3450Sstevel@tonic-gate 	if ((strncmp(name, "/dev/dsk/", strlen("/dev/dsk/")) == 0) &&
3460Sstevel@tonic-gate 	    (strchr((p = name + strlen("/dev/dsk/")), '/') == NULL)) {
3470Sstevel@tonic-gate 		diskname = Strdup(p);
3480Sstevel@tonic-gate 		stripS(diskname);
3490Sstevel@tonic-gate 		return (diskname);
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	if ((strncmp(name, "/dev/rdsk/", strlen("/dev/rdsk/")) == 0) &&
3530Sstevel@tonic-gate 	    (strchr((p = name + strlen("/dev/rdsk/")), '/') == NULL)) {
3540Sstevel@tonic-gate 		diskname = Strdup(p);
3550Sstevel@tonic-gate 		stripS(diskname);
3560Sstevel@tonic-gate 		return (diskname);
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if ((strncmp(name, "/dev/ap/dsk/", strlen("/dev/ap/dsk/")) == 0) &&
3600Sstevel@tonic-gate 	    (strchr((p = name + strlen("/dev/ap/dsk/")), '/') == NULL)) {
3610Sstevel@tonic-gate 		diskname = Strdup(p);
3620Sstevel@tonic-gate 		stripS(diskname);
3630Sstevel@tonic-gate 		return (diskname);
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	if ((strncmp(name, "/dev/ap/rdsk/", strlen("/dev/ap/rdsk/")) == 0) &&
3670Sstevel@tonic-gate 	    (strchr((p = name + strlen("/dev/ap/rdsk/")), '/') == NULL)) {
3680Sstevel@tonic-gate 		diskname = Strdup(p);
3690Sstevel@tonic-gate 		stripS(diskname);
3700Sstevel@tonic-gate 		return (diskname);
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	if ((strncmp(name, "/dev/did/dsk/", strlen("/dev/did/dsk/")) == 0) &&
3740Sstevel@tonic-gate 	    (strchr((p = name + strlen("/dev/did/dsk/")), '/') == NULL)) {
3750Sstevel@tonic-gate 		diskname = Strdup(p);
3760Sstevel@tonic-gate 		stripS(diskname);
3770Sstevel@tonic-gate 		return (diskname);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	if ((strncmp(name, "/dev/did/rdsk/", strlen("/dev/did/rdsk/")) == 0) &&
3810Sstevel@tonic-gate 	    (strchr((p = name + strlen("/dev/did/rdsk/")), '/') == NULL)) {
3820Sstevel@tonic-gate 		diskname = Strdup(p);
3830Sstevel@tonic-gate 		stripS(diskname);
3840Sstevel@tonic-gate 		return (diskname);
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	diskname = Strdup(name);
3880Sstevel@tonic-gate 	stripS(diskname);
3890Sstevel@tonic-gate 	return (diskname);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate  * has_devid -- return the device ID for a given key
3940Sstevel@tonic-gate  *
3950Sstevel@tonic-gate  * RETURN
3960Sstevel@tonic-gate  *	NULL	error
3970Sstevel@tonic-gate  *	devid	devid found that corresponds to the given key.
3980Sstevel@tonic-gate  */
3990Sstevel@tonic-gate static ddi_devid_t
4000Sstevel@tonic-gate has_devid(set_t setno, side_t sideno,  mdkey_t key, md_error_t *ep)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	return (meta_getdidbykey(setno, sideno, key, ep));
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate /*
4060Sstevel@tonic-gate  * Go through the existing list of replicas and check to see
4070Sstevel@tonic-gate  * if their disk has moved, if so update the replica list
4080Sstevel@tonic-gate  *
4090Sstevel@tonic-gate  * RETURN
4100Sstevel@tonic-gate  *	-1	error
4110Sstevel@tonic-gate  *	 0	success
4120Sstevel@tonic-gate  */
4130Sstevel@tonic-gate static int
4140Sstevel@tonic-gate fix_replicanames(
4150Sstevel@tonic-gate 	mdsetname_t	*sp,
4160Sstevel@tonic-gate 	md_error_t	*ep
4170Sstevel@tonic-gate )
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	md_replicalist_t	*rlp = NULL;
4200Sstevel@tonic-gate 	md_replicalist_t	*rl;
4210Sstevel@tonic-gate 	int			ret = -1;
4220Sstevel@tonic-gate 	int			match_type = 0;
4230Sstevel@tonic-gate 	devid_nmlist_t		*disklist = NULL;
4240Sstevel@tonic-gate 	dev_t			small_dev = (dev_t)NODEV;
4250Sstevel@tonic-gate 	side_t			sideno;
4260Sstevel@tonic-gate 	set_t			setno = sp->setno;
4270Sstevel@tonic-gate 	char			*search_path;
4280Sstevel@tonic-gate 	int			search_number;
4290Sstevel@tonic-gate 	char			*ctds_name;
4300Sstevel@tonic-gate 	char			*path_name;
4310Sstevel@tonic-gate 	int			i;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	sideno = getmyside(sp, ep);
4340Sstevel@tonic-gate 	if (sideno == MD_SIDEWILD) {
4350Sstevel@tonic-gate 		mda_debug("Failed to find the side number\n");
4360Sstevel@tonic-gate 		return (-1);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	if (metareplicalist(sp, MD_BASICNAME_OK | PRINT_FAST, &rlp, ep) < 0) {
4400Sstevel@tonic-gate 		mda_debug("Unable to get a list of replicas\n");
4410Sstevel@tonic-gate 		return (METADEVADM_ERR);
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	for (rl = rlp; (rl != NULL); rl = rl->rl_next) {
4450Sstevel@tonic-gate 		md_replica_t	*r = rl->rl_repp;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		small_dev = meta_cmpldev(r->r_namep->dev);
4480Sstevel@tonic-gate 		search_number = mda_findpath(r->r_namep->bname);
4490Sstevel@tonic-gate 		if (search_number == -1) {
4500Sstevel@tonic-gate 			mda_debug("replica update: invalid path: %s",
4510Sstevel@tonic-gate 			    r->r_namep->bname);
4520Sstevel@tonic-gate 			continue;
4530Sstevel@tonic-gate 		} else {
4540Sstevel@tonic-gate 			search_path = plist[search_number].search_path;
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		if (r->r_devid == NULL)
4580Sstevel@tonic-gate 			continue;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 		ret = meta_deviceid_to_nmlist(search_path, r->r_devid,
4610Sstevel@tonic-gate 		    r->r_minor_name, &disklist);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 		mda_debug("replica update: search_path %s\n", search_path);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		if (ret != 0) {
4660Sstevel@tonic-gate 			/*
4670Sstevel@tonic-gate 			 * Failed to find the disk, nothing can be done.
4680Sstevel@tonic-gate 			 * The replica will be marked as bad later.
4690Sstevel@tonic-gate 			 */
4700Sstevel@tonic-gate 			mda_debug("replica update: failed to find disk %s\n",
4710Sstevel@tonic-gate 			    r->r_namep->cname);
4720Sstevel@tonic-gate 			continue;
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 		mda_debug("replica update: current %s (%p)\n",
4750Sstevel@tonic-gate 			r->r_namep->bname, (void *) small_dev);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 		/*
4780Sstevel@tonic-gate 		 * Check to see if the returned disk matches the stored one
4790Sstevel@tonic-gate 		 */
4800Sstevel@tonic-gate 		for (i = 0; disklist[i].dev != NODEV; i++) {
4810Sstevel@tonic-gate 			match_type = 0;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 			mda_debug("replica update: devid list: %s (%p)\n",
4840Sstevel@tonic-gate 			    disklist[i].devname, (void *) disklist[i].dev);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 			if (disklist[i].dev == small_dev) {
4870Sstevel@tonic-gate 				match_type |= DEV_MATCH;
4880Sstevel@tonic-gate 			}
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 			if (strncmp(r->r_namep->bname, disklist[i].devname,
4910Sstevel@tonic-gate 			    strlen(r->r_namep->bname)) == 0) {
4920Sstevel@tonic-gate 				match_type |= NAME_MATCH;
4930Sstevel@tonic-gate 			}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 			/*
4960Sstevel@tonic-gate 			 * break out if some sort of match is found because
4970Sstevel@tonic-gate 			 * we already match on the devid.
4980Sstevel@tonic-gate 			 */
4990Sstevel@tonic-gate 			if (match_type != 0)
5000Sstevel@tonic-gate 				break;
5010Sstevel@tonic-gate 		}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 		mda_debug("fix_replicanames: match: %x i: %d\n", match_type, i);
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 		if (match_type == (DEV_MATCH|NAME_MATCH)) {
5060Sstevel@tonic-gate 			/* no change */
5070Sstevel@tonic-gate 			mda_debug("replica update: no change %s\n",
5080Sstevel@tonic-gate 			    disklist[i].devname);
5090Sstevel@tonic-gate 			devid_free_nmlist(disklist);
5100Sstevel@tonic-gate 			continue;
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 		/* No match found - use the first entry in disklist */
5140Sstevel@tonic-gate 		if (disklist[i].dev == NODEV)
5150Sstevel@tonic-gate 			i = 0;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		mda_debug("replica update: reloading %s %p\n",
5180Sstevel@tonic-gate 		    disklist[i].devname,
51962Sjeanm 		    (void *)(uintptr_t)meta_expldev(disklist[i].dev));
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 		if (firsttime) {
5220Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
5230Sstevel@tonic-gate 			    "Disk movement detected\n"));
5240Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
5250Sstevel@tonic-gate 			    "Updating device names in Solaris Volume "
5260Sstevel@tonic-gate 			    "Manager\n"));
5270Sstevel@tonic-gate 			firsttime = 0;
5280Sstevel@tonic-gate 		}
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 		if (dev_options & DEV_VERBOSE) {
5310Sstevel@tonic-gate 			char	*devidstr;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 			devidstr =
5340Sstevel@tonic-gate 			    devid_str_encode(r->r_devid, r->r_minor_name);
5350Sstevel@tonic-gate 			if (devidstr == NULL) {
5360Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
5370Sstevel@tonic-gate 				    "Failed to encode the devid\n"));
5380Sstevel@tonic-gate 				continue;
5390Sstevel@tonic-gate 			}
5400Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
5410Sstevel@tonic-gate 			    "%s changed to %s from device relocation "
5420Sstevel@tonic-gate 			    "information %s\n"),
5430Sstevel@tonic-gate 			    (char *)r->r_namep->cname, disklist[i].devname,
5440Sstevel@tonic-gate 			    devidstr);
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 		if (!(dev_options & DEV_NOACTION)) {
5480Sstevel@tonic-gate 			mda_debug("Updating locator name\n");
5490Sstevel@tonic-gate 			ctds_name = strrchr(disklist[i].devname, '/');
5500Sstevel@tonic-gate 			ctds_name++;
5510Sstevel@tonic-gate 			if ((path_name = mda_getpath(disklist[i].devname))
5520Sstevel@tonic-gate 			    == NULL) {
5530Sstevel@tonic-gate 				continue;
5540Sstevel@tonic-gate 			}
5550Sstevel@tonic-gate 			if (update_locator_namespace(setno, sideno,
5560Sstevel@tonic-gate 			    ctds_name, meta_expldev(disklist[i].dev),
5570Sstevel@tonic-gate 			    path_name, ep) != 0) {
5580Sstevel@tonic-gate 				mda_debug("replica update: ioctl failed\n");
5590Sstevel@tonic-gate 				if (dev_options & DEV_VERBOSE) {
5600Sstevel@tonic-gate 					mda_print(dgettext(TEXT_DOMAIN,
5610Sstevel@tonic-gate 					    "Failed to update locator "
5620Sstevel@tonic-gate 					    "namespace on change from %s "
5630Sstevel@tonic-gate 					    "to %s\n"), ctds_name,
5640Sstevel@tonic-gate 					    disklist[i].devname);
5650Sstevel@tonic-gate 				}
5660Sstevel@tonic-gate 			}
5670Sstevel@tonic-gate 		}
5680Sstevel@tonic-gate 		Free(path_name);
5690Sstevel@tonic-gate 		devid_free_nmlist(disklist);
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 	metafreereplicalist(rlp);
5720Sstevel@tonic-gate 	return (0);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate /*
5760Sstevel@tonic-gate  * pathname_reload - main function for the -r option. Will reload the
5770Sstevel@tonic-gate  *	pathname in both the main namespace and the locator namespace.
5780Sstevel@tonic-gate  *	Also, checks both areas for invalid device ID's and prints them
5790Sstevel@tonic-gate  *	out.
5800Sstevel@tonic-gate  *
5810Sstevel@tonic-gate  *    If the set is a multi-node diskset that means there are no devid's
5820Sstevel@tonic-gate  *    so just return.
5830Sstevel@tonic-gate  *
5840Sstevel@tonic-gate  * RETURN
5850Sstevel@tonic-gate  *	METADEVADM_ERR		error
5860Sstevel@tonic-gate  *	METADEVADM_SUCCESS 	success
5870Sstevel@tonic-gate  *	METADEVADM_DEVIDINVALID	success, but invalid devids detected
5880Sstevel@tonic-gate  */
5890Sstevel@tonic-gate int
5900Sstevel@tonic-gate pathname_reload(
5910Sstevel@tonic-gate 	mdsetname_t		**spp,
5920Sstevel@tonic-gate 	set_t			setno,
5930Sstevel@tonic-gate 	md_error_t		*ep)
5940Sstevel@tonic-gate {
5950Sstevel@tonic-gate 	char			*drvnmp;
5960Sstevel@tonic-gate 	minor_t			mnum = 0;
5970Sstevel@tonic-gate 	md_dev64_t		dev = 0;
5980Sstevel@tonic-gate 	mdnm_params_t		nm;
5990Sstevel@tonic-gate 	char			*ctds_name;
6000Sstevel@tonic-gate 	ddi_devid_t		devidp;
6010Sstevel@tonic-gate 	md_i_didstat_t		ds;
6020Sstevel@tonic-gate 	side_t			sideno;
6030Sstevel@tonic-gate 	char			*search_path = NULL;
6040Sstevel@tonic-gate 	int			search_number;
6050Sstevel@tonic-gate 	devid_nmlist_t		*disklist = NULL;
6060Sstevel@tonic-gate 	char			*minor_name = NULL;
6070Sstevel@tonic-gate 	char			*devidstr = NULL;
6080Sstevel@tonic-gate 	char			*path = NULL;
6090Sstevel@tonic-gate 	int			ret;
6100Sstevel@tonic-gate 	dev_t			small_dev = (dev_t)NODEV;
6110Sstevel@tonic-gate 	int			match_type;
6120Sstevel@tonic-gate 	char			*tmp = NULL;
6130Sstevel@tonic-gate 	mdsetname_t		*sp = *spp;
6140Sstevel@tonic-gate 	md_set_desc		*sd;
6150Sstevel@tonic-gate 	int			i;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	/*
6180Sstevel@tonic-gate 	 * Check for multi-node diskset and return if it is one.
6190Sstevel@tonic-gate 	 */
6200Sstevel@tonic-gate 	if (!metaislocalset(sp)) {
6210Sstevel@tonic-gate 		if ((sd = metaget_setdesc(sp, ep)) == NULL)
6220Sstevel@tonic-gate 			return (METADEVADM_ERR);
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		if (MD_MNSET_DESC(sd))
6250Sstevel@tonic-gate 			return (METADEVADM_SUCCESS);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	/*
6290Sstevel@tonic-gate 	 * Get the entry of the namespace via the key. To do this
6300Sstevel@tonic-gate 	 * call MD_IOCNXTKEY until no more.
6310Sstevel@tonic-gate 	 * For each entry in the namespace we want to check
6320Sstevel@tonic-gate 	 * for devid and update
6330Sstevel@tonic-gate 	 */
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	(void) memset(&nm, '\0', sizeof (nm));
6360Sstevel@tonic-gate 	nm.key = MD_KEYWILD;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	sideno = getmyside(*spp, ep);
6390Sstevel@tonic-gate 	if (sideno == MD_SIDEWILD) {
6400Sstevel@tonic-gate 		/* failed to find this node in the set */
6410Sstevel@tonic-gate 		mda_debug("Failed to find the side number\n");
6420Sstevel@tonic-gate 		return (METADEVADM_ERR);
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	/* LINTED */
6460Sstevel@tonic-gate 	while (1) {
6470Sstevel@tonic-gate 		nm.mde	= mdnullerror;
6480Sstevel@tonic-gate 		nm.setno = setno;
6490Sstevel@tonic-gate 		nm.side = sideno;
6500Sstevel@tonic-gate 		/* look at each key in the namespace */
6510Sstevel@tonic-gate 		if (metaioctl(MD_IOCNXTKEY_NM, &nm, &nm.mde, NULL) != 0) {
6520Sstevel@tonic-gate 			(void) mdstealerror(ep, &nm.mde);
6530Sstevel@tonic-gate 			return (METADEVADM_ERR);
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 		if (nm.key == MD_KEYWILD) {
6570Sstevel@tonic-gate 			/* no more entries */
6580Sstevel@tonic-gate 			break;
6590Sstevel@tonic-gate 		}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 		/*
6620Sstevel@tonic-gate 		 * get the nm entry using the key. Then check to see if
6630Sstevel@tonic-gate 		 * there's a devid associated with this entry
6640Sstevel@tonic-gate 		 * If not, go onto next key.
6650Sstevel@tonic-gate 		 */
6660Sstevel@tonic-gate 		if ((nm.devname = (uintptr_t)meta_getnmentbykey(setno, sideno,
6670Sstevel@tonic-gate 		    nm.key, &drvnmp, &mnum, &dev, ep)) == NULL) {
6680Sstevel@tonic-gate 			mda_debug("pathname_reload: no name for key: %d\n",
6690Sstevel@tonic-gate 			    nm.key);
6700Sstevel@tonic-gate 			continue;
6710Sstevel@tonic-gate 		}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		mda_debug("pathname_reload: examining %s\n",
67462Sjeanm 		    (char *)(uintptr_t)nm.devname);
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		if ((devidp = has_devid(setno, sideno, nm.key, ep)) == NULL) {
6770Sstevel@tonic-gate 			/* metadevices do not have devid's in them */
6780Sstevel@tonic-gate 			mda_debug("pathname_reload: no devid for %s\n",
67962Sjeanm 			    (char *)(uintptr_t)nm.devname);
6801945Sjeanm 			/* Clear error if no devid and go to next nm entry */
6811945Sjeanm 			mdclrerror(ep);
6820Sstevel@tonic-gate 			continue;
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		if ((minor_name = meta_getdidminorbykey(setno, sideno,
6860Sstevel@tonic-gate 		    nm.key, ep)) == NULL) {
6870Sstevel@tonic-gate 			/*
6880Sstevel@tonic-gate 			 * In theory this is impossible because if the
6890Sstevel@tonic-gate 			 * devidp is non-null then the minor_name has
6900Sstevel@tonic-gate 			 * already been looked up.
6910Sstevel@tonic-gate 			 */
69262Sjeanm 			mda_debug("No minor name for %s\n",
69362Sjeanm 			    (char *)(uintptr_t)nm.devname);
6940Sstevel@tonic-gate 			free(devidp);
6950Sstevel@tonic-gate 			continue;
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 		/*
6980Sstevel@tonic-gate 		 * If there is a devid then we have a real device that
6990Sstevel@tonic-gate 		 * could have moved.
7000Sstevel@tonic-gate 		 */
7010Sstevel@tonic-gate 		devidstr = devid_str_encode(devidp, minor_name);
7020Sstevel@tonic-gate 		if (devidstr == NULL) {
7030Sstevel@tonic-gate 			mda_debug("Failed to encode the devid\n");
7040Sstevel@tonic-gate 			free(devidp);
7050Sstevel@tonic-gate 			continue;
7060Sstevel@tonic-gate 		}
7070Sstevel@tonic-gate 		mda_debug("devid: %s\n", devidstr);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 		/*
7100Sstevel@tonic-gate 		 * Find the search path that should be used. This is an
7110Sstevel@tonic-gate 		 * optimization to try and prevent a search for the complete
7120Sstevel@tonic-gate 		 * /dev namespace.
7130Sstevel@tonic-gate 		 */
71462Sjeanm 		search_number = mda_findpath((char *)(uintptr_t)nm.devname);
7150Sstevel@tonic-gate 		if (search_number == -1) {
7160Sstevel@tonic-gate 			search_path = "/dev";
7170Sstevel@tonic-gate 		} else {
7180Sstevel@tonic-gate 			search_path = plist[search_number].search_path;
7190Sstevel@tonic-gate 		}
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		/* now look for the disk name using the devid */
7220Sstevel@tonic-gate 		ret = meta_deviceid_to_nmlist(search_path, devidp,
7230Sstevel@tonic-gate 		    minor_name, &disklist);
7240Sstevel@tonic-gate 		free(devidp);
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 		if (ret != 0) {
7270Sstevel@tonic-gate 			/*
7280Sstevel@tonic-gate 			 * Failed to find the disk
7290Sstevel@tonic-gate 			 */
7300Sstevel@tonic-gate 			devid_str_free(devidstr);
7310Sstevel@tonic-gate 			continue;
7320Sstevel@tonic-gate 		}
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 		small_dev = meta_cmpldev(dev);
7350Sstevel@tonic-gate 		mda_debug("Old device lookup: %s (%p)\n",
73662Sjeanm 			(char *)(uintptr_t)nm.devname, (void *)small_dev);
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 		/*
7390Sstevel@tonic-gate 		 * Check to see if the returned disk matches the stored one
7400Sstevel@tonic-gate 		 */
7410Sstevel@tonic-gate 		for (i = 0; disklist[i].dev != NODEV; i++) {
7420Sstevel@tonic-gate 			match_type = 0;
7430Sstevel@tonic-gate 			mda_debug("From devid lookup: %s (%p)\n",
7440Sstevel@tonic-gate 				(char *)disklist[i].devname,
7450Sstevel@tonic-gate 				(void *)disklist[i].dev);
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 			if (disklist[i].dev == small_dev) {
7480Sstevel@tonic-gate 				match_type |= DEV_MATCH;
7490Sstevel@tonic-gate 			}
7500Sstevel@tonic-gate 
75162Sjeanm 			if (strncmp((char *)(uintptr_t)nm.devname,
75262Sjeanm 			    disklist[i].devname,
75362Sjeanm 			    strlen((char *)(uintptr_t)nm.devname)) == 0) {
7540Sstevel@tonic-gate 				mda_debug("Name match: %s and %s (%d)\n",
75562Sjeanm 				    disklist[i].devname,
75662Sjeanm 				    (char *)(uintptr_t)nm.devname,
75762Sjeanm 				    strlen((char *)(uintptr_t)nm.devname));
7580Sstevel@tonic-gate 				match_type |= NAME_MATCH;
7590Sstevel@tonic-gate 			}
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 			if (match_type == (DEV_MATCH|NAME_MATCH))
7620Sstevel@tonic-gate 				break;
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 		if (match_type == (DEV_MATCH|NAME_MATCH)) {
7660Sstevel@tonic-gate 			/* no change */
7670Sstevel@tonic-gate 			devid_str_free(devidstr);
7680Sstevel@tonic-gate 			mda_debug("All matched %s\n", disklist[i].devname);
7690Sstevel@tonic-gate 			devid_free_nmlist(disklist);
7700Sstevel@tonic-gate 			continue;
7710Sstevel@tonic-gate 		}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 		/* No match found - use the first entry in disklist */
7740Sstevel@tonic-gate 		i = 0;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 		if (firsttime) {
7770Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
7780Sstevel@tonic-gate 			    "Disk movement detected\n"));
7790Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
7800Sstevel@tonic-gate 			    "Updating device names in "
7810Sstevel@tonic-gate 			    "Solaris Volume Manager\n"));
7820Sstevel@tonic-gate 			firsttime = 0;
7830Sstevel@tonic-gate 		}
7840Sstevel@tonic-gate 		if (dev_options & DEV_VERBOSE) {
7850Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
7860Sstevel@tonic-gate 			    "%s changed to %s from device relocation "
7870Sstevel@tonic-gate 			    "information %s\n"),
78862Sjeanm 			    (char *)(uintptr_t)nm.devname, disklist[i].devname,
7890Sstevel@tonic-gate 			    devidstr);
7900Sstevel@tonic-gate 		}
7910Sstevel@tonic-gate 		devid_str_free(devidstr);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 		/* need to build up the path of the disk */
7940Sstevel@tonic-gate 		if ((path = Strdup(disklist[i].devname)) == NULL) {
7950Sstevel@tonic-gate 			mda_debug("Failed to duplicate path: %s\n",
7960Sstevel@tonic-gate 			    disklist[i].devname);
7970Sstevel@tonic-gate 			devid_free_nmlist(disklist);
7980Sstevel@tonic-gate 			continue;
7990Sstevel@tonic-gate 		}
8000Sstevel@tonic-gate 		if ((tmp = strrchr(path, '/')) == NULL) {
8010Sstevel@tonic-gate 			mda_debug("Failed to parse %s\n", path);
8020Sstevel@tonic-gate 			devid_free_nmlist(disklist);
8030Sstevel@tonic-gate 			Free(path);
8040Sstevel@tonic-gate 			continue;
8050Sstevel@tonic-gate 		}
8060Sstevel@tonic-gate 		tmp += sizeof (char);
8070Sstevel@tonic-gate 		*tmp = '\0';
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 		if ((ctds_name = strrchr(disklist[i].devname, '/')) == NULL) {
8100Sstevel@tonic-gate 			mda_debug("Failed to parse ctds name: %s\n",
8110Sstevel@tonic-gate 			    disklist[i].devname);
8120Sstevel@tonic-gate 			devid_free_nmlist(disklist);
8130Sstevel@tonic-gate 			Free(path);
8140Sstevel@tonic-gate 			continue;
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 		ctds_name += sizeof (char);
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 		mda_debug("Reloading disk %s %s %p\n",
81962Sjeanm 		    ctds_name, path,
82062Sjeanm 		    (void *)(uintptr_t)meta_expldev(disklist[i].dev));
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 		if (!(dev_options & DEV_NOACTION)) {
8230Sstevel@tonic-gate 			/* Something has changed so update the namespace */
8240Sstevel@tonic-gate 			if (update_namespace(setno, sideno, ctds_name,
8250Sstevel@tonic-gate 			    meta_expldev(disklist[i].dev), nm.key, path,
8260Sstevel@tonic-gate 			    ep) != 0) {
8270Sstevel@tonic-gate 				mda_debug("Failed to update namespace\n");
8280Sstevel@tonic-gate 				if (dev_options & DEV_VERBOSE) {
8290Sstevel@tonic-gate 					mda_print(dgettext(TEXT_DOMAIN,
8300Sstevel@tonic-gate 					    "Failed to update namespace on "
8310Sstevel@tonic-gate 					    "change from %s to %s\n"),
8320Sstevel@tonic-gate 					    ctds_name, disklist[i].devname);
8330Sstevel@tonic-gate 				}
8340Sstevel@tonic-gate 			}
8350Sstevel@tonic-gate 		}
8360Sstevel@tonic-gate 		devid_free_nmlist(disklist);
8370Sstevel@tonic-gate 		Free(path);
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	if (fix_replicanames(*spp, ep) == -1)
8410Sstevel@tonic-gate 		mda_debug("Failed to update replicas\n");
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	/*
8440Sstevel@tonic-gate 	 * check for invalid device id's
8450Sstevel@tonic-gate 	 */
8460Sstevel@tonic-gate 	(void) memset(&ds, '\0', sizeof (ds));
8470Sstevel@tonic-gate 	ds.setno = setno;
8480Sstevel@tonic-gate 	ds.side = sideno;
8490Sstevel@tonic-gate 	ds.mode = MD_FIND_INVDID;
8500Sstevel@tonic-gate 	/* get count of number of invalid device id's */
8510Sstevel@tonic-gate 	if (metaioctl(MD_IOCDID_STAT, &ds, &ds.mde, NULL) != 0) {
8520Sstevel@tonic-gate 		(void) mdstealerror(ep, &ds.mde);
8530Sstevel@tonic-gate 		return (METADEVADM_ERR);
8540Sstevel@tonic-gate 	}
8550Sstevel@tonic-gate 	if (ds.cnt != 0) {
8560Sstevel@tonic-gate 		char	*ctdptr, *ctdp;
8570Sstevel@tonic-gate 		/*
8580Sstevel@tonic-gate 		 * we have some invalid device id's so we need to
8590Sstevel@tonic-gate 		 * print them out
8600Sstevel@tonic-gate 		 */
8610Sstevel@tonic-gate 		ds.mode = MD_GET_INVDID;
8620Sstevel@tonic-gate 		/* malloc buffer for kernel to place devid list into */
8630Sstevel@tonic-gate 		if ((ctdptr = (char *)Malloc((ds.cnt * ds.maxsz) + 1)) == 0) {
8640Sstevel@tonic-gate 			return (METADEVADM_ERR);
8650Sstevel@tonic-gate 		}
8660Sstevel@tonic-gate 		ds.ctdp = (uintptr_t)ctdptr;
8670Sstevel@tonic-gate 		/* get actual list of invalid device id's */
8680Sstevel@tonic-gate 		if (metaioctl(MD_IOCDID_STAT, &ds, &ds.mde, NULL) != 0) {
8690Sstevel@tonic-gate 			Free(ctdptr);
8700Sstevel@tonic-gate 			(void) mdstealerror(ep, &ds.mde);
8710Sstevel@tonic-gate 			return (METADEVADM_ERR);
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 		/* print out the invalid devid's */
8750Sstevel@tonic-gate 		mda_print(dgettext(TEXT_DOMAIN,
8760Sstevel@tonic-gate 		    "Invalid device relocation information "
8770Sstevel@tonic-gate 		    "detected in Solaris Volume Manager\n"));
8780Sstevel@tonic-gate 		mda_print(dgettext(TEXT_DOMAIN,
8790Sstevel@tonic-gate 		    "Please check the status of the following disk(s):\n"));
88062Sjeanm 		ctdp = (char *)(uintptr_t)ds.ctdp;
8810Sstevel@tonic-gate 		while (*ctdp != NULL) {
8820Sstevel@tonic-gate 			mda_print("\t%s\n", ctdp);
8830Sstevel@tonic-gate 			ctdp += ds.maxsz;
8840Sstevel@tonic-gate 		}
8850Sstevel@tonic-gate 		Free(ctdptr);
8860Sstevel@tonic-gate 		return (METADEVADM_DEVIDINVALID);
8870Sstevel@tonic-gate 	}
8880Sstevel@tonic-gate 	return (METADEVADM_SUCCESS);
8890Sstevel@tonic-gate }
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate /*
8920Sstevel@tonic-gate  * replica_update_devid - cycle through the replica list, rlp, and
8930Sstevel@tonic-gate  *  update the device ids on all of the replicas that are on the
8940Sstevel@tonic-gate  *  device specified by lp. A side effect is to update the value of
8950Sstevel@tonic-gate  *  cdevidpp to contain the character representation of the device
8960Sstevel@tonic-gate  *  id before updating if it is not already set.
8970Sstevel@tonic-gate  *
8980Sstevel@tonic-gate  * RETURN
8990Sstevel@tonic-gate  *	METADEVADM_ERR		error
9000Sstevel@tonic-gate  *	METADEVADM_SUCCESS	success
9010Sstevel@tonic-gate  */
9020Sstevel@tonic-gate static int
9030Sstevel@tonic-gate replica_update_devid(
9040Sstevel@tonic-gate 	md_replicalist_t *rlp,
9050Sstevel@tonic-gate 	mddrivename_t	*dnp,
9060Sstevel@tonic-gate 	set_t		setno,
9070Sstevel@tonic-gate 	char		**cdevidpp,
9080Sstevel@tonic-gate 	md_error_t	*ep
9090Sstevel@tonic-gate )
9100Sstevel@tonic-gate {
9110Sstevel@tonic-gate 	mddb_config_t		db_c;
9120Sstevel@tonic-gate 	md_replicalist_t	*rl;
9130Sstevel@tonic-gate 	ddi_devid_t		devidp;
9140Sstevel@tonic-gate 	int			ret;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	if (cdevidpp == NULL)
9170Sstevel@tonic-gate 		return (METADEVADM_ERR);
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	ret = devid_str_decode(dnp->devid, &devidp, NULL);
9200Sstevel@tonic-gate 	if (ret != 0) {
9210Sstevel@tonic-gate 		/* failed to encode the devid */
9220Sstevel@tonic-gate 		mda_debug("Failed to decode %s into a valid devid\n",
9230Sstevel@tonic-gate 		    dnp->devid);
9240Sstevel@tonic-gate 		return (METADEVADM_ERR);
9250Sstevel@tonic-gate 	}
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	/* search replica list for give ctd name */
9280Sstevel@tonic-gate 	for (rl = rlp; (rl != NULL); rl = rl->rl_next) {
9290Sstevel@tonic-gate 		md_replica_t    *r = rl->rl_repp;
9300Sstevel@tonic-gate 		mdname_t	*rnp = r->r_namep;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		if (strncmp(rnp->cname, dnp->cname, strlen(dnp->cname)) == 0) {
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 			/* found the replica, now grab the devid */
9350Sstevel@tonic-gate 			if (*cdevidpp == NULL) {
9360Sstevel@tonic-gate 				*cdevidpp = devid_str_encode(r->r_devid, NULL);
9370Sstevel@tonic-gate 			}
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 			if (*cdevidpp == NULL) {
9400Sstevel@tonic-gate 				devid_free(devidp);
9410Sstevel@tonic-gate 				return (METADEVADM_ERR);
9420Sstevel@tonic-gate 			}
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 			mda_debug("Updating replica %s, set %d, old devid %s\n",
9450Sstevel@tonic-gate 			    rnp->cname, setno, *cdevidpp);
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 			if (dev_options & DEV_VERBOSE) {
9480Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
9490Sstevel@tonic-gate 				    "Updating replica %s of set number %d from "
9500Sstevel@tonic-gate 				    "device id %s to device id %s\n"),
9510Sstevel@tonic-gate 				    rnp->cname, setno, *cdevidpp, dnp->devid);
9520Sstevel@tonic-gate 			}
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 			(void) memset(&db_c, '\0', sizeof (db_c));
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 			db_c.c_setno = setno;
9570Sstevel@tonic-gate 			db_c.c_devt = rnp->dev;
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 			if (!(dev_options & DEV_NOACTION)) {
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 				mda_debug("Updating replica\n");
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 				/*
9640Sstevel@tonic-gate 				 * call into kernel to update lb
9650Sstevel@tonic-gate 				 * namespace device id
9660Sstevel@tonic-gate 				 * of given devt
9670Sstevel@tonic-gate 				 */
9680Sstevel@tonic-gate 				if (metaioctl(MD_DB_SETDID, &db_c,
9690Sstevel@tonic-gate 				    &db_c.c_mde, NULL) != 0) {
9700Sstevel@tonic-gate 					devid_free(devidp);
9710Sstevel@tonic-gate 					(void) mdstealerror(ep, &db_c.c_mde);
9720Sstevel@tonic-gate 					return (METADEVADM_ERR);
9730Sstevel@tonic-gate 				}
9740Sstevel@tonic-gate 			}
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 		}
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 	devid_free(devidp);
9790Sstevel@tonic-gate 	return (METADEVADM_SUCCESS);
9800Sstevel@tonic-gate }
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate /*
9830Sstevel@tonic-gate  * devid_update -- main routine for the -u option. Will update both the
9840Sstevel@tonic-gate  * 	namespace and the locator block with the correct devid for the
9850Sstevel@tonic-gate  * 	disk specified.
9860Sstevel@tonic-gate  *
9870Sstevel@tonic-gate  * RETURN
9880Sstevel@tonic-gate  *	METADEVADM_ERR		error
9890Sstevel@tonic-gate  *	METADEVADM_SUCCESS	success
9900Sstevel@tonic-gate  */
9910Sstevel@tonic-gate static int
9920Sstevel@tonic-gate devid_update(
9930Sstevel@tonic-gate 	mdsetname_t	**spp,
9940Sstevel@tonic-gate 	set_t		setno,
9950Sstevel@tonic-gate 	char		*ctd,
9960Sstevel@tonic-gate 	md_error_t	*ep
9970Sstevel@tonic-gate )
9980Sstevel@tonic-gate {
9990Sstevel@tonic-gate 	md_drive_desc		*dd, *ddp;
10000Sstevel@tonic-gate 	mddrivename_t		*dnp;
10010Sstevel@tonic-gate 	mdnm_params_t		nm;
10020Sstevel@tonic-gate 	ddi_devid_t		devidp;
10030Sstevel@tonic-gate 	side_t			side;
10040Sstevel@tonic-gate 	char			*old_cdevidp = NULL;
10050Sstevel@tonic-gate 	md_replicalist_t	*rlp = NULL;
10060Sstevel@tonic-gate 	int			rval = METADEVADM_ERR;
10070Sstevel@tonic-gate 	mdname_t		*np = NULL;
10080Sstevel@tonic-gate 	uint_t			rep_slice;
10090Sstevel@tonic-gate 	char			*pathname = NULL;
10100Sstevel@tonic-gate 	char			*diskname = NULL;
10110Sstevel@tonic-gate 	int			fd = -1;
10120Sstevel@tonic-gate 	int			len;
10130Sstevel@tonic-gate 	char			*fp;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	side = getmyside(*spp, ep);
10160Sstevel@tonic-gate 	if (side == MD_SIDEWILD) {
10170Sstevel@tonic-gate 		/* failed to find this node in the set */
10180Sstevel@tonic-gate 		mda_debug("Failed to find the side number\n");
10190Sstevel@tonic-gate 		return (METADEVADM_ERR);
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	if ((dnp = metadrivename(spp, ctd, ep)) == NULL) {
10230Sstevel@tonic-gate 		mda_debug("Failed to create a dnp for %s\n", ctd);
10240Sstevel@tonic-gate 		return (METADEVADM_ERR);
10250Sstevel@tonic-gate 	}
10260Sstevel@tonic-gate 	if (dnp->devid == NULL) {
10270Sstevel@tonic-gate 		/*
10280Sstevel@tonic-gate 		 * Disk does not have a devid! So cannot update the
10290Sstevel@tonic-gate 		 * devid within the replica.
10300Sstevel@tonic-gate 		 */
10310Sstevel@tonic-gate 		mda_debug("%s does not have a devid\n", dnp->cname);
10320Sstevel@tonic-gate 		if (dev_options & DEV_VERBOSE) {
10330Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
10340Sstevel@tonic-gate 			    "%s does not have a device id. Cannot update "
10350Sstevel@tonic-gate 			    "device id if none exists\n"), ctd);
10360Sstevel@tonic-gate 		}
10370Sstevel@tonic-gate 		return (METADEVADM_ERR);
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	mda_debug("Devid update to: %s\n", dnp->devid);
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	/*
10430Sstevel@tonic-gate 	 * Check if we own the set, if we do then do some processing
10440Sstevel@tonic-gate 	 * on the replicas.
10450Sstevel@tonic-gate 	 */
10460Sstevel@tonic-gate 	if (meta_check_ownership(*spp, ep) == 0) {
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 		/* get the replicas */
10490Sstevel@tonic-gate 		if (metareplicalist(*spp, MD_BASICNAME_OK | PRINT_FAST, &rlp,
10500Sstevel@tonic-gate 		    ep) < 0)
10510Sstevel@tonic-gate 			return (METADEVADM_ERR);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 		/* update the devids in the replicas if necessary */
10540Sstevel@tonic-gate 		if (replica_update_devid(rlp, dnp, setno, &old_cdevidp,
10550Sstevel@tonic-gate 		    ep) != METADEVADM_SUCCESS) {
10560Sstevel@tonic-gate 			metafreereplicalist(rlp);
10570Sstevel@tonic-gate 			return (METADEVADM_ERR);
10580Sstevel@tonic-gate 		}
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 		metafreereplicalist(rlp);
10610Sstevel@tonic-gate 	}
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	/*
10640Sstevel@tonic-gate 	 * If this is not the LOCAL set then need to update the LOCAL
10650Sstevel@tonic-gate 	 * replica with the new disk record.
10660Sstevel@tonic-gate 	 */
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	if (setno != MD_LOCAL_SET) {
10690Sstevel@tonic-gate 		mda_debug("Non-local set: %d side %d\n", setno, side);
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 		/*
10720Sstevel@tonic-gate 		 * Need to find the disk record within the set and then
10730Sstevel@tonic-gate 		 * update it.
10740Sstevel@tonic-gate 		 */
10750Sstevel@tonic-gate 		if ((dd =
10760Sstevel@tonic-gate 		    metaget_drivedesc(*spp, MD_FULLNAME_ONLY, ep)) == NULL) {
10770Sstevel@tonic-gate 			if (! mdisok(ep))
10780Sstevel@tonic-gate 				goto out;
10790Sstevel@tonic-gate 			/* no disks in the set - no point continuing */
10800Sstevel@tonic-gate 			mda_debug("No disks in diskset\n");
10810Sstevel@tonic-gate 			rval = METADEVADM_SUCCESS;
10820Sstevel@tonic-gate 			goto out;
10830Sstevel@tonic-gate 		}
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 		for (ddp = dd; ddp != NULL; ddp = ddp->dd_next) {
10860Sstevel@tonic-gate 			if (strncmp(ddp->dd_dnp->cname, dnp->cname,
10870Sstevel@tonic-gate 			    strlen(dnp->cname)) == 0)
10880Sstevel@tonic-gate 				break;
10890Sstevel@tonic-gate 		}
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 		if (ddp == NULL) {
10920Sstevel@tonic-gate 			/* failed to finddisk in the set */
10930Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
10940Sstevel@tonic-gate 			    "%s not found in set %s. Check your syntax\n"),
10950Sstevel@tonic-gate 			    ctd, (*spp)->setname);
10960Sstevel@tonic-gate 			(void) mddserror(ep, MDE_DS_DRIVENOTINSET, setno, NULL,
10970Sstevel@tonic-gate 			    ctd, (*spp)->setname);
10980Sstevel@tonic-gate 			goto out;
10990Sstevel@tonic-gate 		}
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 		/*
11020Sstevel@tonic-gate 		 * Now figure out the correct slice, for a diskset the slice
11030Sstevel@tonic-gate 		 * we care about is always the 'replica' slice.
11040Sstevel@tonic-gate 		 */
11050Sstevel@tonic-gate 		if (meta_replicaslice(dnp, &rep_slice, ep) != 0) {
11060Sstevel@tonic-gate 			mda_debug("Unable to find replica slice for %s\n",
11070Sstevel@tonic-gate 			    dnp->cname);
11080Sstevel@tonic-gate 			goto out;
11090Sstevel@tonic-gate 		}
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 		mda_debug("slice no: %d disk %s\n", rep_slice, dnp->cname);
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 		if ((np = metaslicename(dnp, rep_slice, ep)) == NULL) {
11140Sstevel@tonic-gate 			mda_debug("Unable to build namespace\n");
11150Sstevel@tonic-gate 			goto out;
11160Sstevel@tonic-gate 		}
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 		mda_debug("check: ctdname: %s\n", np->cname);
11190Sstevel@tonic-gate 		mda_debug("check: ctdname: %s\n", np->rname);
11200Sstevel@tonic-gate 		mda_debug("check: ctdname: %s\n", np->bname);
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 		if (!(dev_options & DEV_NOACTION)) {
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 			mda_debug("Updating record: key %d name %s\n",
11250Sstevel@tonic-gate 			    ddp->dd_dnp->side_names_key, np->cname);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 			pathname = mda_getpath(np->bname);
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 			if (update_namespace(MD_LOCAL_SET, side + SKEW,
11300Sstevel@tonic-gate 			    np->cname, np->dev, ddp->dd_dnp->side_names_key,
11310Sstevel@tonic-gate 			    pathname, ep) != 0) {
11320Sstevel@tonic-gate 				goto out;
11330Sstevel@tonic-gate 			}
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 			/*
11360Sstevel@tonic-gate 			 * Now update the devid entry as well, this works
11370Sstevel@tonic-gate 			 * correctly because the prior call to
11380Sstevel@tonic-gate 			 * update_namespace() above puts the correct dev_t
11390Sstevel@tonic-gate 			 * in the namespace which will then be resolved
11400Sstevel@tonic-gate 			 * to the new devid by the ioctl now called.
11410Sstevel@tonic-gate 			 */
11420Sstevel@tonic-gate 			nm.mde = mdnullerror;
11430Sstevel@tonic-gate 			nm.setno = MD_LOCAL_SET;
11440Sstevel@tonic-gate 			nm.side = side + SKEW;
11450Sstevel@tonic-gate 			nm.key = ddp->dd_dnp->side_names_key;
11460Sstevel@tonic-gate 			if (metaioctl(MD_SETNMDID, &nm, &nm.mde, NULL) != 0) {
11470Sstevel@tonic-gate 				(void) mdstealerror(ep, &nm.mde);
11480Sstevel@tonic-gate 				goto out;
11490Sstevel@tonic-gate 			}
11500Sstevel@tonic-gate 		}
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	if ((dev_options & DEV_LOCAL_SET) && (setno != MD_LOCAL_SET)) {
11540Sstevel@tonic-gate 		/*
11550Sstevel@tonic-gate 		 * Only want to update the local set so do not continue.
11560Sstevel@tonic-gate 		 */
11570Sstevel@tonic-gate 		rval = METADEVADM_SUCCESS;
11580Sstevel@tonic-gate 		goto out;
11590Sstevel@tonic-gate 	}
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	/*
11620Sstevel@tonic-gate 	 * Iterate through all of the metadevices looking for the
11630Sstevel@tonic-gate 	 * passed in ctd.  If found then update the devid
11640Sstevel@tonic-gate 	 */
11650Sstevel@tonic-gate 	(void) memset(&nm, '\0', sizeof (nm));
11660Sstevel@tonic-gate 	nm.key = MD_KEYWILD;
11670Sstevel@tonic-gate 	/* LINTED */
11680Sstevel@tonic-gate 	while (1) {
11690Sstevel@tonic-gate 		nm.mde = mdnullerror;
11700Sstevel@tonic-gate 		nm.setno = setno;
11710Sstevel@tonic-gate 		nm.side = side;
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 		/* search each namespace entry */
11740Sstevel@tonic-gate 		if (metaioctl(MD_IOCNXTKEY_NM, &nm, &nm.mde, NULL) != 0) {
11750Sstevel@tonic-gate 			(void) mdstealerror(ep, &nm.mde);
11760Sstevel@tonic-gate 			rval = METADEVADM_ERR;
11770Sstevel@tonic-gate 			goto out;
11780Sstevel@tonic-gate 		}
11790Sstevel@tonic-gate 		if (nm.key == MD_KEYWILD) {
11800Sstevel@tonic-gate 			if (setno != MD_LOCAL_SET) {
11810Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
11820Sstevel@tonic-gate 				    "%s not found in set %s. Check your "
11830Sstevel@tonic-gate 				    "syntax\n"), ctd, (*spp)->setname);
11840Sstevel@tonic-gate 				goto out;
11850Sstevel@tonic-gate 			} else {
11860Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
11870Sstevel@tonic-gate 				    "%s not found in local set. "
11880Sstevel@tonic-gate 				    "Check your syntax\n"), ctd);
11890Sstevel@tonic-gate 				goto out;
11900Sstevel@tonic-gate 			}
11910Sstevel@tonic-gate 		}
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 		nm.devname = (uintptr_t)meta_getnmentbykey(setno, side, nm.key,
11940Sstevel@tonic-gate 		    NULL, NULL, NULL, ep);
11950Sstevel@tonic-gate 		if (nm.devname == NULL) {
11960Sstevel@tonic-gate 			rval = METADEVADM_ERR;
11970Sstevel@tonic-gate 			goto out;
11980Sstevel@tonic-gate 		}
11990Sstevel@tonic-gate 
120062Sjeanm 		diskname = getdiskname((char *)(uintptr_t)nm.devname);
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 		mda_debug("Checking %s with %s\n", diskname, dnp->cname);
12030Sstevel@tonic-gate 		if (strcmp(diskname, dnp->cname) != 0)
12040Sstevel@tonic-gate 			continue;
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 		mda_debug("Updating device %s in namespace\n",
120762Sjeanm 		    (char *)(uintptr_t)nm.devname);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 		/*
12100Sstevel@tonic-gate 		 * found disk, does it have a devid within the namespace ?
12110Sstevel@tonic-gate 		 * It might not because it does not support devid's or was
12120Sstevel@tonic-gate 		 * put into the namespace when there was no devid support
12130Sstevel@tonic-gate 		 */
12140Sstevel@tonic-gate 		if ((devidp = has_devid(setno, side, nm.key, ep)) == NULL) {
12150Sstevel@tonic-gate 			mda_debug("%s has no devid in the namespace",
121662Sjeanm 			    (char *)(uintptr_t)nm.devname);
12170Sstevel@tonic-gate 			if (dev_options & DEV_VERBOSE) {
12180Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
12190Sstevel@tonic-gate 				"SVM has no device id for "
122062Sjeanm 				"%s, cannot update.\n"),
122162Sjeanm 				(char *)(uintptr_t)nm.devname);
12220Sstevel@tonic-gate 			}
12230Sstevel@tonic-gate 			continue; /* no devid. go on to next */
12240Sstevel@tonic-gate 		}
12250Sstevel@tonic-gate 		if (old_cdevidp == NULL) {
12260Sstevel@tonic-gate 			old_cdevidp = devid_str_encode(devidp, NULL);
12270Sstevel@tonic-gate 		}
12280Sstevel@tonic-gate 		free(devidp);
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 		/*
12310Sstevel@tonic-gate 		 * has devid so update namespace, note the key has been set
12320Sstevel@tonic-gate 		 * by the prior MD_IOCNXTKEY_NM ioctl.
12330Sstevel@tonic-gate 		 */
12340Sstevel@tonic-gate 		nm.mde = mdnullerror;
12350Sstevel@tonic-gate 		nm.setno = setno;
12360Sstevel@tonic-gate 		nm.side = side;
12370Sstevel@tonic-gate 		if (!(dev_options & DEV_NOACTION)) {
12380Sstevel@tonic-gate 			/*
12390Sstevel@tonic-gate 			 * The call below may fail if the -u option is being
12400Sstevel@tonic-gate 			 * used to update a disk that has been replaced.
12410Sstevel@tonic-gate 			 * The -u option to metadevadm should not be used
12420Sstevel@tonic-gate 			 * for this purpose because we trust the dev_t of
12430Sstevel@tonic-gate 			 * the device in the replica and if we have replaced
12440Sstevel@tonic-gate 			 * the device and it is a fibre one then the dev_t
12450Sstevel@tonic-gate 			 * will have changed. This means we end up looking for
12460Sstevel@tonic-gate 			 * the devid of a non-existant disk and we subsequently
12470Sstevel@tonic-gate 			 * fail with NODEVID.
12480Sstevel@tonic-gate 			 */
12490Sstevel@tonic-gate 			if (metaioctl(MD_SETNMDID, &nm,
12500Sstevel@tonic-gate 					&nm.mde, NULL) != 0) {
12510Sstevel@tonic-gate 				if (dev_options & DEV_VERBOSE) {
12520Sstevel@tonic-gate 					mda_print(dgettext(TEXT_DOMAIN,
12530Sstevel@tonic-gate 					    "SVM failed to update the device "
12540Sstevel@tonic-gate 					    "id for %s probably due to both "
12550Sstevel@tonic-gate 					    "devt and device id changing.\n"),
125662Sjeanm 					    (char *)(uintptr_t)nm.devname);
12570Sstevel@tonic-gate 				}
12580Sstevel@tonic-gate 				(void) mdstealerror(ep, &nm.mde);
12590Sstevel@tonic-gate 				mde_perror(ep, "");
12600Sstevel@tonic-gate 				rval = METADEVADM_ERR;
12610Sstevel@tonic-gate 				goto out;
12620Sstevel@tonic-gate 			}
12630Sstevel@tonic-gate 		}
12640Sstevel@tonic-gate 		if (old_cdevidp == NULL) {
12650Sstevel@tonic-gate 			rval = METADEVADM_ERR;
12660Sstevel@tonic-gate 			goto out;
12670Sstevel@tonic-gate 		}
12680Sstevel@tonic-gate 		break;
12690Sstevel@tonic-gate 	} /* end while */
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	mda_print(dgettext(TEXT_DOMAIN,
12720Sstevel@tonic-gate 		    "Updating Solaris Volume Manager device relocation "
12730Sstevel@tonic-gate 		    "information for %s\n"), ctd);
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	mda_print(dgettext(TEXT_DOMAIN,
12760Sstevel@tonic-gate 	    "Old device reloc information:\n\t%s\n"), old_cdevidp);
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	len = strlen(dnp->rname) + strlen("s0");
12790Sstevel@tonic-gate 	if ((fp = (char *)Malloc(len + 1)) == NULL) {
12800Sstevel@tonic-gate 		mda_print(dgettext(TEXT_DOMAIN,
12810Sstevel@tonic-gate 		    "insufficient memory, device Reloc info not "
12820Sstevel@tonic-gate 		    "available\n"));
12830Sstevel@tonic-gate 	} else {
12840Sstevel@tonic-gate 		(void) snprintf(fp, len + 1, "%ss0", dnp->rname);
12850Sstevel@tonic-gate 		if ((fd = open(fp, O_RDONLY|O_NDELAY)) < 0) {
12860Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
12870Sstevel@tonic-gate 			    "Open of %s failed\n"), fp);
12880Sstevel@tonic-gate 		} else {
12890Sstevel@tonic-gate 			int		rc = -1;
12900Sstevel@tonic-gate 			ddi_devid_t	devid1 = NULL;
12910Sstevel@tonic-gate 			char		*cdevidp;
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 			rc = devid_get(fd, &devid1);
12940Sstevel@tonic-gate 			if (close(fd) < 0) {
12950Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
12960Sstevel@tonic-gate 				    "Close of %s failed\n"), fp);
12970Sstevel@tonic-gate 			}
12980Sstevel@tonic-gate 			if (rc != 0) {
12990Sstevel@tonic-gate 				mda_print(dgettext(TEXT_DOMAIN,
13000Sstevel@tonic-gate 				    "Unable to obtain device "
13010Sstevel@tonic-gate 				    "Reloc info for %s\n"), fp);
13020Sstevel@tonic-gate 			} else {
13030Sstevel@tonic-gate 				cdevidp = devid_str_encode(devid1, NULL);
13040Sstevel@tonic-gate 				if (cdevidp == NULL) {
13050Sstevel@tonic-gate 					mda_print(dgettext(TEXT_DOMAIN,
13060Sstevel@tonic-gate 					    "Unable to print "
13070Sstevel@tonic-gate 					    "device Reloc info for %s\n"), fp);
13080Sstevel@tonic-gate 				} else {
13090Sstevel@tonic-gate 					mda_print(dgettext(TEXT_DOMAIN,
13100Sstevel@tonic-gate 					    "New device reloc "
13110Sstevel@tonic-gate 					    "information:\n\t%s\n"), cdevidp);
13120Sstevel@tonic-gate 					devid_str_free(cdevidp);
13130Sstevel@tonic-gate 				}
13140Sstevel@tonic-gate 				devid_free(devid1);
13150Sstevel@tonic-gate 			}
13160Sstevel@tonic-gate 		}
13170Sstevel@tonic-gate 		Free(fp);
13180Sstevel@tonic-gate 	}
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	rval = METADEVADM_SUCCESS;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate out:
13230Sstevel@tonic-gate 	if (diskname)
13240Sstevel@tonic-gate 		Free(diskname);
13250Sstevel@tonic-gate 	if (pathname)
13260Sstevel@tonic-gate 		Free(pathname);
13270Sstevel@tonic-gate 	if (old_cdevidp) {
13280Sstevel@tonic-gate 		devid_str_free(old_cdevidp);
13290Sstevel@tonic-gate 	}
13300Sstevel@tonic-gate 	return (rval);
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate }
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate /*
13350Sstevel@tonic-gate  * Check the ctd name of the disk to see if the disk has moved. If it
13360Sstevel@tonic-gate  * has moved then the newname is returned in 'newname', it is up to
13370Sstevel@tonic-gate  * the caller to free the memory associated with it.
13380Sstevel@tonic-gate  *
13390Sstevel@tonic-gate  * RETURN
13400Sstevel@tonic-gate  *	METADEVADM_ERR		error
13410Sstevel@tonic-gate  *	METADEVADM_SUCCESS	success
13420Sstevel@tonic-gate  *	METADEVADM_DISKMOVE	success, and the disk has moved
13430Sstevel@tonic-gate  *	METADEVADM_DSKNAME_ERR	error creating the disk name structures.
13440Sstevel@tonic-gate  */
13450Sstevel@tonic-gate int
13460Sstevel@tonic-gate meta_upd_ctdnames(
13470Sstevel@tonic-gate 	mdsetname_t	**spp,
13480Sstevel@tonic-gate 	set_t		setno,
13490Sstevel@tonic-gate 	side_t		sideno,
13500Sstevel@tonic-gate 	mddrivename_t	*dnp,
13510Sstevel@tonic-gate 	char		**newname,
13520Sstevel@tonic-gate 	md_error_t	*ep
13530Sstevel@tonic-gate )
13540Sstevel@tonic-gate {
13550Sstevel@tonic-gate 	char		*drvnmp;
13560Sstevel@tonic-gate 	int		i;
13570Sstevel@tonic-gate 	minor_t		mnum = 0;
13580Sstevel@tonic-gate 	md_dev64_t	dev = 0;
13590Sstevel@tonic-gate 	dev_t		small_dev = (dev_t)NODEV;
13600Sstevel@tonic-gate 	mdnm_params_t	nm;
13610Sstevel@tonic-gate 	char		*pathname;
13620Sstevel@tonic-gate 	char		*minor_name = NULL;
13630Sstevel@tonic-gate 	ddi_devid_t	devidp;
13640Sstevel@tonic-gate 	devid_nmlist_t	*disklist = NULL;
13650Sstevel@tonic-gate 	int		ret = 0;
13660Sstevel@tonic-gate 	mdsidenames_t	*snp;
13670Sstevel@tonic-gate 	int		match_type;
13680Sstevel@tonic-gate 	int		search_number = -1;
13690Sstevel@tonic-gate 	char		*search_type = NULL;
13700Sstevel@tonic-gate 	char		*search_path = NULL;
13710Sstevel@tonic-gate 	uint_t		rep_slice;
13720Sstevel@tonic-gate 	mddrivename_t	*newdnp;
13730Sstevel@tonic-gate 	mdname_t	*np;
13740Sstevel@tonic-gate 	mdsetname_t	*sp = *spp;
13750Sstevel@tonic-gate 	md_set_desc	*sd;
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	/*
13780Sstevel@tonic-gate 	 * setno should always be 0 but we're going to
13790Sstevel@tonic-gate 	 * check for multi-node diskset and return if it is one.
13800Sstevel@tonic-gate 	 */
13810Sstevel@tonic-gate 	if (!metaislocalset(sp)) {
13820Sstevel@tonic-gate 		if ((sd = metaget_setdesc(sp, ep)) == NULL)
13830Sstevel@tonic-gate 			return (METADEVADM_ERR);
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 		if (MD_MNSET_DESC(sd))
13860Sstevel@tonic-gate 			return (METADEVADM_SUCCESS);
13870Sstevel@tonic-gate 	}
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	if (dnp->devid == NULL) {
13900Sstevel@tonic-gate 		/* no devid, nothing can be done */
13910Sstevel@tonic-gate 		mda_debug("meta_upd_ctdnames: %s has no devid\n", dnp->cname);
13920Sstevel@tonic-gate 		if (dev_options & DEV_VERBOSE) {
13930Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
13940Sstevel@tonic-gate 			    "%s has no devid, cannot detect "
13950Sstevel@tonic-gate 			    "disk movement for this disk.\n"), dnp->cname);
13960Sstevel@tonic-gate 		}
13970Sstevel@tonic-gate 		return (ret);
13980Sstevel@tonic-gate 	}
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 	/*
14010Sstevel@tonic-gate 	 * Find the correct side name for the disk. There is a sidename
14020Sstevel@tonic-gate 	 * for each host associated with the diskset.
14030Sstevel@tonic-gate 	 */
14040Sstevel@tonic-gate 	for (snp = dnp->side_names; snp != NULL; snp = snp->next) {
14050Sstevel@tonic-gate 		mda_debug("meta_upd_ctdnames: %s %d args: setno %d sideno %d\n",
14060Sstevel@tonic-gate 		    snp->cname, snp->sideno, setno, sideno);
14070Sstevel@tonic-gate 		/* only use SKEW for the local replica */
14080Sstevel@tonic-gate 		if (setno == 0) {
14090Sstevel@tonic-gate 			if (snp->sideno + SKEW == sideno)
14100Sstevel@tonic-gate 				break;
14110Sstevel@tonic-gate 		} else {
14120Sstevel@tonic-gate 			if (snp->sideno == sideno)
14130Sstevel@tonic-gate 				break;
14140Sstevel@tonic-gate 		}
14150Sstevel@tonic-gate 	}
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	if (snp == NULL) {
14180Sstevel@tonic-gate 		/*
14190Sstevel@tonic-gate 		 * Failed to find the side name, this should not
14200Sstevel@tonic-gate 		 * be possible. However if it does happen this is an
14210Sstevel@tonic-gate 		 * indication of an inconsistant replica - something
14220Sstevel@tonic-gate 		 * might have gone wrong during an add or a delete of
14230Sstevel@tonic-gate 		 * a host.
14240Sstevel@tonic-gate 		 */
14250Sstevel@tonic-gate 		mda_debug("Unable to find the side information for disk %s",
14260Sstevel@tonic-gate 		    dnp->cname);
14270Sstevel@tonic-gate 		(void) mddserror(ep, MDE_DS_HOSTNOSIDE, (*spp)->setno, mynode(),
14280Sstevel@tonic-gate 		    NULL, dnp->cname);
14290Sstevel@tonic-gate 		return (METADEVADM_ERR);
14300Sstevel@tonic-gate 	}
14310Sstevel@tonic-gate 	/*
14320Sstevel@tonic-gate 	 * Find the type of device we are to be searching on
14330Sstevel@tonic-gate 	 */
14340Sstevel@tonic-gate 	search_number = mda_findpath(snp->cname);
14350Sstevel@tonic-gate 	if (search_number == -1) {
14360Sstevel@tonic-gate 		search_path = "/dev";
14370Sstevel@tonic-gate 		search_type = DEVID_MINOR_NAME_ALL;
14380Sstevel@tonic-gate 	} else {
14390Sstevel@tonic-gate 		search_path = plist[search_number].search_path;
14400Sstevel@tonic-gate 		search_type = plist[search_number].search_type;
14410Sstevel@tonic-gate 	}
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	mda_debug("Search path :%s searth_type: %x\n",
14440Sstevel@tonic-gate 	    search_path, (int)search_type);
14450Sstevel@tonic-gate 	(void) memset(&nm, '\0', sizeof (nm));
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	nm.mde = mdnullerror;
14480Sstevel@tonic-gate 	nm.setno = setno;
14490Sstevel@tonic-gate 	nm.side = sideno;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	/*
14520Sstevel@tonic-gate 	 * Get the devname from the name space.
14530Sstevel@tonic-gate 	 */
14540Sstevel@tonic-gate 	if ((nm.devname = (uintptr_t)meta_getnmentbykey(setno, sideno,
14550Sstevel@tonic-gate 	    dnp->side_names_key, &drvnmp, &mnum, &dev, ep)) == NULL) {
14560Sstevel@tonic-gate 		return (METADEVADM_ERR);
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	ret = devid_str_decode(dnp->devid, &devidp, &minor_name);
14600Sstevel@tonic-gate 	devid_str_free(minor_name);
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	if (ret != 0) {
14630Sstevel@tonic-gate 		/*
14640Sstevel@tonic-gate 		 * Failed to encode the devid.
14650Sstevel@tonic-gate 		 */
14660Sstevel@tonic-gate 		devid_free(devidp);
14670Sstevel@tonic-gate 		return (METADEVADM_ERR);
14680Sstevel@tonic-gate 	}
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	/*
14710Sstevel@tonic-gate 	 * Use the stored devid to find the existing device node and check
14720Sstevel@tonic-gate 	 * to see if the disk has moved. Use the raw devices as the name
14730Sstevel@tonic-gate 	 * of the disk is stored as the raw device, if this is not done
14740Sstevel@tonic-gate 	 * then the disk will not be found.
14750Sstevel@tonic-gate 	 */
14760Sstevel@tonic-gate 	ret = meta_deviceid_to_nmlist(search_path, devidp,
14770Sstevel@tonic-gate 	    search_type, &disklist);
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	if (ret != 0) {
14800Sstevel@tonic-gate 		if (dev_options & DEV_VERBOSE) {
14810Sstevel@tonic-gate 			mda_print(dgettext(TEXT_DOMAIN,
14820Sstevel@tonic-gate 			    "Device ID %s last associated with "
14830Sstevel@tonic-gate 			    "disk %s no longer found in system\n"),
14840Sstevel@tonic-gate 			    dnp->devid, dnp->cname);
14850Sstevel@tonic-gate 		}
14860Sstevel@tonic-gate 		devid_free(devidp);
14870Sstevel@tonic-gate 		devid_free_nmlist(disklist);
14880Sstevel@tonic-gate 		return (METADEVADM_SUCCESS);
14890Sstevel@tonic-gate 	}
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	small_dev = meta_cmpldev(dev);
14920Sstevel@tonic-gate 	mda_debug("Old device lookup: %s (%p)\n",
149362Sjeanm 			(char *)(uintptr_t)nm.devname, (void *)small_dev);
14940Sstevel@tonic-gate 	/*
14950Sstevel@tonic-gate 	 * Check to see if the returned disk matches the stored one
14960Sstevel@tonic-gate 	 */
14970Sstevel@tonic-gate 	for (i = 0; disklist[i].dev != NODEV; i++) {
14980Sstevel@tonic-gate 		match_type = 0;
14990Sstevel@tonic-gate 		mda_debug("From devid lookup: %s (%p)\n",
15000Sstevel@tonic-gate 				disklist[i].devname, (void *)disklist[i].dev);
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 		if (disklist[i].dev == small_dev) {
15030Sstevel@tonic-gate 			match_type |= DEV_MATCH;
15040Sstevel@tonic-gate 		}
15050Sstevel@tonic-gate 
150662Sjeanm 		if (strncmp((char *)(uintptr_t)nm.devname, disklist[i].devname,
150762Sjeanm 		    strlen((char *)(uintptr_t)nm.devname)) == 0) {
15080Sstevel@tonic-gate 			match_type |= NAME_MATCH;
15090Sstevel@tonic-gate 		}
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 		if (match_type != 0)
15120Sstevel@tonic-gate 			break;
15130Sstevel@tonic-gate 	}
15140Sstevel@tonic-gate 	devid_free(devidp);
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	mda_debug("meta_upd_ctdnames: match: %x i: %d\n", match_type, i);
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	if (match_type == (DEV_MATCH|NAME_MATCH)) {
15190Sstevel@tonic-gate 		/* no change */
15200Sstevel@tonic-gate 		devid_free_nmlist(disklist);
15210Sstevel@tonic-gate 		return (METADEVADM_SUCCESS);
15220Sstevel@tonic-gate 	}
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	/* No match found - use the first entry in disklist */
15250Sstevel@tonic-gate 	if (disklist[i].dev == NODEV)
15260Sstevel@tonic-gate 		i = 0;
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 	if (!(match_type & DEV_MATCH)) {
15290Sstevel@tonic-gate 		/* did not match on the dev, so dev_t has changed */
15300Sstevel@tonic-gate 		mda_debug("Did not match on dev: %p %p\n",
15310Sstevel@tonic-gate 		    (void *) small_dev, (void *) disklist[i].dev);
15320Sstevel@tonic-gate 		dev = meta_expldev(disklist[i].dev);
15330Sstevel@tonic-gate 	}
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	if (!(match_type & NAME_MATCH)) {
15360Sstevel@tonic-gate 		mda_debug("Did not match on name: %s (%p)\n",
153762Sjeanm 		    (char *)(uintptr_t)nm.devname, (void *) disklist[i].dev);
15380Sstevel@tonic-gate 	}
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	/*
15410Sstevel@tonic-gate 	 * If here, then the name in the disklist is the one we
15420Sstevel@tonic-gate 	 * want in any case so use it.
15430Sstevel@tonic-gate 	 */
15440Sstevel@tonic-gate 	mda_debug("devname: %s\n", disklist[i].devname);
15450Sstevel@tonic-gate 	/*
15460Sstevel@tonic-gate 	 * Need to remove the slice as metadrivename() expects a diskname
15470Sstevel@tonic-gate 	 */
15480Sstevel@tonic-gate 	stripS(disklist[i].devname);
15490Sstevel@tonic-gate 	/*
15500Sstevel@tonic-gate 	 * Build an mddrivename_t to use
15510Sstevel@tonic-gate 	 */
15520Sstevel@tonic-gate 	if ((newdnp = metadrivename(spp, disklist[i].devname, ep)) == NULL) {
15530Sstevel@tonic-gate 		mda_debug("Unable to make a dnp out of %s\n",
15540Sstevel@tonic-gate 		    disklist[i].devname);
15550Sstevel@tonic-gate 		return (METADEVADM_DSKNAME_ERR);
15560Sstevel@tonic-gate 	}
15570Sstevel@tonic-gate 	/*
15580Sstevel@tonic-gate 	 * Need to find the correct slice used for the replica
15590Sstevel@tonic-gate 	 */
15600Sstevel@tonic-gate 	if (meta_replicaslice(newdnp, &rep_slice, ep) != 0) {
15610Sstevel@tonic-gate 		return (METADEVADM_DSKNAME_ERR);
15620Sstevel@tonic-gate 	}
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	if ((np = metaslicename(newdnp, rep_slice, ep)) == NULL) {
15650Sstevel@tonic-gate 		mda_debug("Failed to build an np for %s\n", dnp->rname);
15660Sstevel@tonic-gate 		return (METADEVADM_DSKNAME_ERR);
15670Sstevel@tonic-gate 	}
15680Sstevel@tonic-gate 	mda_debug("check: cname: %s\n", np->cname);
15690Sstevel@tonic-gate 	mda_debug("check: rname: %s\n", np->rname);
15700Sstevel@tonic-gate 	mda_debug("check: bname: %s\n", np->bname);
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	if (newname != NULL)
15730Sstevel@tonic-gate 		*newname = Strdup(np->bname);
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	if (!(dev_options & DEV_NOACTION)) {
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 		mda_debug("update namespace\n");
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 		/* get the block path */
15800Sstevel@tonic-gate 		pathname = mda_getpath(np->bname);
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 		if (update_namespace(setno, sideno, np->cname,
15830Sstevel@tonic-gate 		    dev, dnp->side_names_key, pathname, ep) != 0) {
15840Sstevel@tonic-gate 			/* finished with the list so return the memory */
15850Sstevel@tonic-gate 			Free(pathname);
15860Sstevel@tonic-gate 			devid_free_nmlist(disklist);
15870Sstevel@tonic-gate 			return (METADEVADM_ERR);
15880Sstevel@tonic-gate 		}
15890Sstevel@tonic-gate 	}
15900Sstevel@tonic-gate 	/* finished with the list so return the memory */
15910Sstevel@tonic-gate 	Free(pathname);
15920Sstevel@tonic-gate 	devid_free_nmlist(disklist);
15930Sstevel@tonic-gate 	ret = METADEVADM_DISKMOVE;
15940Sstevel@tonic-gate 	return (ret);
15950Sstevel@tonic-gate }
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate int
15980Sstevel@tonic-gate meta_fixdevid(
15990Sstevel@tonic-gate 	mdsetname_t	*sp,
16000Sstevel@tonic-gate 	mddevopts_t	options,
16010Sstevel@tonic-gate 	char		*diskname,
16020Sstevel@tonic-gate 	md_error_t	*ep
16030Sstevel@tonic-gate )
16040Sstevel@tonic-gate {
16050Sstevel@tonic-gate 	set_t		setno = sp->setno;
16060Sstevel@tonic-gate 	int		ret = 0;
16070Sstevel@tonic-gate 	char		*pathname = NULL;
16080Sstevel@tonic-gate 	mdsetname_t	*local_sp = NULL;
16090Sstevel@tonic-gate 	md_drive_desc	*d = NULL;
16100Sstevel@tonic-gate 	char		*newname = NULL;
16110Sstevel@tonic-gate 	md_drive_desc	*dd;
16120Sstevel@tonic-gate 	side_t		sideno;
16130Sstevel@tonic-gate 	md_set_desc	*sd;
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	/* if MN diskset just return */
16160Sstevel@tonic-gate 	if (!metaislocalset(sp)) {
16170Sstevel@tonic-gate 		if ((sd = metaget_setdesc(sp, ep)) == NULL) {
16180Sstevel@tonic-gate 			return (METADEVADM_ERR);
16190Sstevel@tonic-gate 		}
16200Sstevel@tonic-gate 		if (MD_MNSET_DESC(sd))
16210Sstevel@tonic-gate 			return (METADEVADM_SUCCESS);
16220Sstevel@tonic-gate 	}
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	dev_options |= options;
16250Sstevel@tonic-gate 	mda_debug("dev_options: %x\n", dev_options);
16260Sstevel@tonic-gate 	if (dev_options & DEV_RELOAD) {
16270Sstevel@tonic-gate 		/*
16280Sstevel@tonic-gate 		 * If it's not the local set we need to check the local
16290Sstevel@tonic-gate 		 * namespace to see if disks have moved as it contains
16300Sstevel@tonic-gate 		 * entries for the disks in the set.
16310Sstevel@tonic-gate 		 */
16320Sstevel@tonic-gate 		if (setno != MD_LOCAL_SET) {
16330Sstevel@tonic-gate 			if ((dd = metaget_drivedesc(sp, MD_BASICNAME_OK |
16340Sstevel@tonic-gate 			    PRINT_FAST, ep)) == NULL) {
16350Sstevel@tonic-gate 				mde_perror(ep, "");
16360Sstevel@tonic-gate 				mdclrerror(ep);
16370Sstevel@tonic-gate 				return (METADEVADM_ERR);
16380Sstevel@tonic-gate 			}
16390Sstevel@tonic-gate 			local_sp = metasetname(MD_LOCAL_NAME, ep);
16400Sstevel@tonic-gate 			sideno = getmyside(sp, ep) + SKEW;
16410Sstevel@tonic-gate 			for (d = dd; d != NULL; d = d->dd_next) {
16420Sstevel@tonic-gate 				/*
16430Sstevel@tonic-gate 				 * Actually do the check of the disks.
16440Sstevel@tonic-gate 				 */
16450Sstevel@tonic-gate 				ret = meta_upd_ctdnames(&local_sp, 0, sideno,
16460Sstevel@tonic-gate 				    d->dd_dnp, &newname, ep);
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 				if ((ret == METADEVADM_ERR) ||
16490Sstevel@tonic-gate 				    (ret == METADEVADM_DSKNAME_ERR)) {
16500Sstevel@tonic-gate 					/* check failed in unknown manner */
16510Sstevel@tonic-gate 					mda_debug("meta_upd_ctdnames failed\n");
16520Sstevel@tonic-gate 					return (METADEVADM_ERR);
16530Sstevel@tonic-gate 				}
16540Sstevel@tonic-gate 			}
16550Sstevel@tonic-gate 		}
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 		/* do a reload of the devid namespace */
16580Sstevel@tonic-gate 		ret = pathname_reload(&sp, setno, ep);
16590Sstevel@tonic-gate 	} else if (dev_options & DEV_UPDATE) {
16600Sstevel@tonic-gate 		pathname = getdiskname(diskname);
16610Sstevel@tonic-gate 		ret = devid_update(&sp, setno, pathname, ep);
16620Sstevel@tonic-gate 		free(pathname);
16630Sstevel@tonic-gate 	}
16640Sstevel@tonic-gate 	return (ret);
16650Sstevel@tonic-gate }
1666