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