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
5*8516SAchim.Maurer@Sun.COM  * Common Development and Distribution License (the "License").
6*8516SAchim.Maurer@Sun.COM  * 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*8516SAchim.Maurer@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Block comment which describes the contents of this file.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <dlfcn.h>
310Sstevel@tonic-gate #include <meta.h>
320Sstevel@tonic-gate #include <metadyn.h>
330Sstevel@tonic-gate #include <sdssc.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #define	SDSSC_PATH SDSSC_CL_LIBDIR "/sc/libsds_sc.so"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate static func_table_t dl_table[] = {
380Sstevel@tonic-gate 	{ "_sdssc_version",		(void **)&sdssc_version },
390Sstevel@tonic-gate 	{ "_sdssc_create_begin",	(void **)&sdssc_create_begin },
400Sstevel@tonic-gate 	{ "_sdssc_mo_create_begin",	(void **)&sdssc_mo_create_begin },
410Sstevel@tonic-gate 	{ "_sdssc_create_end",		(void **)&sdssc_create_end },
420Sstevel@tonic-gate 	{ "_sdssc_delete_begin",	(void **)&sdssc_delete_begin },
430Sstevel@tonic-gate 	{ "_sdssc_delete_end",		(void **)&sdssc_delete_end },
440Sstevel@tonic-gate 	{ "_sdssc_get_index",		(void **)&sdssc_get_index },
450Sstevel@tonic-gate 	{ "_sdssc_add_hosts",		(void **)&sdssc_add_hosts },
460Sstevel@tonic-gate 	{ "_sdssc_delete_hosts",	(void **)&sdssc_delete_hosts },
470Sstevel@tonic-gate 	{ "_sdssc_get_primary_host",	(void **)&sdssc_get_primary_host },
480Sstevel@tonic-gate 	{ "_sdssc_cmd_proxy",		(void **)&sdssc_cmd_proxy },
490Sstevel@tonic-gate 	{ "_sdssc_getnodelist",		(void **)&sdssc_getnodelist },
500Sstevel@tonic-gate 	{ "_sdssc_freenodelist",	(void **)&sdssc_freenodelist },
510Sstevel@tonic-gate 	{ "_sdssc_binddevs",		(void **)&sdssc_binddevs },
520Sstevel@tonic-gate 	{ "_sdssc_bindclusterdevs",	(void **)&sdssc_bindclusterdevs },
530Sstevel@tonic-gate 	{ "_sdssc_gettransportbynode",	(void **)&sdssc_gettransportbynode },
540Sstevel@tonic-gate 	{ "_sdssc_free_mdcerr_list",	(void **)&sdssc_free_mdcerr_list },
550Sstevel@tonic-gate 	{ "_sdssc_property_get",	(void **)&sdssc_property_get },
560Sstevel@tonic-gate 	{ "_sdssc_property_set",	(void **)&sdssc_property_set },
570Sstevel@tonic-gate 	{ "_sdssc_get_services",	(void **)&sdssc_get_services },
580Sstevel@tonic-gate 	{ "_sdssc_get_services_free",	(void **)&sdssc_get_services_free },
590Sstevel@tonic-gate 	{ "_sdssc_suspend",		(void **)&sdssc_suspend },
600Sstevel@tonic-gate 	{ "_sdssc_convert_cluster_path",
610Sstevel@tonic-gate 	    (void **)&sdssc_convert_cluster_path },
620Sstevel@tonic-gate 	{ "_sdssc_convert_ctd_path",
630Sstevel@tonic-gate 	    (void **)&sdssc_convert_ctd_path },
640Sstevel@tonic-gate 	{ "_sdssc_convert_path_free",
650Sstevel@tonic-gate 	    (void **)&sdssc_convert_path_free },
660Sstevel@tonic-gate 	{ "_sdssc_notify_service",	(void **)&sdssc_notify_service },
670Sstevel@tonic-gate 	{ "_sdssc_cm_nm2nid",	(void **)&sdssc_cm_nm2nid },
680Sstevel@tonic-gate 	{ "_sdssc_cm_sr_nm2nid",	(void **)&sdssc_cm_sr_nm2nid },
690Sstevel@tonic-gate 	{ "_sdssc_cm_nid2nm",	(void **)&sdssc_cm_nid2nm },
700Sstevel@tonic-gate 	{ "_sdssc_cm_sr_nid2nm",	(void **)&sdssc_cm_sr_nid2nm },
710Sstevel@tonic-gate 	{ "_sdssc_get_priv_ipaddr",	(void **)&sdssc_get_priv_ipaddr },
720Sstevel@tonic-gate 	{ (char *)0,			(void **)0 }
730Sstevel@tonic-gate };
740Sstevel@tonic-gate 
750Sstevel@tonic-gate static rval_e
760Sstevel@tonic-gate just_dup_string(const char *source, char **dest)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	*dest = strdup(source);
790Sstevel@tonic-gate 	return (SDSSC_OKAY);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static void
830Sstevel@tonic-gate free_dup_string(char *source)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	free(source);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * not_bound -- routine to always return NOT_BOUND
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate static rval_e
920Sstevel@tonic-gate not_bound(void)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	return (SDSSC_NOT_BOUND);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * not_bound_error -- routine to always return SDSSC_NOT_BOUND_ERROR since
990Sstevel@tonic-gate  * routine is not bound.  This is used when using an older version
1000Sstevel@tonic-gate  * of libsdssc that doesn't support MN disksets.  When an MN specific
1010Sstevel@tonic-gate  * routine is called (such as sdssc_mo_create_set) an SDSSC_NOT_BOUND_ERROR
1020Sstevel@tonic-gate  * will be returned.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate static rval_e
1050Sstevel@tonic-gate not_bound_error(void)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	return (SDSSC_NOT_BOUND_ERROR);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * set_common_routine -- set cluster interface routines to return NOT_BOUND
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate static void
1150Sstevel@tonic-gate set_common_routine()
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	func_table_p	f;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	for (f = dl_table; f->fptr != (void *)0; f++) {
1200Sstevel@tonic-gate 		if (strcmp(f->fname, "_sdssc_convert_cluster_path") == 0) {
1210Sstevel@tonic-gate 			*f->fptr = (void *)&just_dup_string;
1220Sstevel@tonic-gate 		} else if (strcmp(f->fname, "_sdssc_free_convert_cluster_path")
1230Sstevel@tonic-gate 		    == 0) {
1240Sstevel@tonic-gate 			*f->fptr = (void *)&free_dup_string;
1250Sstevel@tonic-gate 		} else {
1260Sstevel@tonic-gate 			*f->fptr = (void *)&not_bound;
1270Sstevel@tonic-gate 		}
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * sdssc_bind_library -- entry point which resolves all cluster interface pts.
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate rval_e
1350Sstevel@tonic-gate sdssc_bind_library(void)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	void		*dp;
1380Sstevel@tonic-gate 	int		(*lb)();
1390Sstevel@tonic-gate 	func_table_p	ftp;
140*8516SAchim.Maurer@Sun.COM 	static int	initialised = 0;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/*
1430Sstevel@tonic-gate 	 * If already bound then just return okay so this routine
1440Sstevel@tonic-gate 	 * becomes idempotent. If this check isn't made then we'll
1450Sstevel@tonic-gate 	 * fail when calling the "_bind_library" function because
1460Sstevel@tonic-gate 	 * dcs_initialize() can only be called once.
147*8516SAchim.Maurer@Sun.COM 	 * If not bound first time we try it again.
1480Sstevel@tonic-gate 	 */
149*8516SAchim.Maurer@Sun.COM 	if (initialised && (void *)sdssc_version != (void *)not_bound) {
150*8516SAchim.Maurer@Sun.COM 		return (SDSSC_OKAY);
1510Sstevel@tonic-gate 	}
152*8516SAchim.Maurer@Sun.COM 	initialised = 1;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if ((dp = dlopen(SDSSC_PATH, RTLD_LAZY)) == NULL) {
1550Sstevel@tonic-gate 		set_common_routine();
1560Sstevel@tonic-gate 		return (SDSSC_NOT_BOUND);
1570Sstevel@tonic-gate 	} else {
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 		/*
1600Sstevel@tonic-gate 		 * Allow the binding library to initialize state if
1610Sstevel@tonic-gate 		 * necessary. Currently this calls the DCS initialize()
1620Sstevel@tonic-gate 		 * routine which checks to see if we're part of a cluster.
1630Sstevel@tonic-gate 		 */
1640Sstevel@tonic-gate 		if ((lb = (int (*)())dlsym(dp, "_bind_library")) != NULL) {
1650Sstevel@tonic-gate 			if (lb() != 0) {
1660Sstevel@tonic-gate 				set_common_routine();
1670Sstevel@tonic-gate 				return (SDSSC_NOT_BOUND);
1680Sstevel@tonic-gate 			}
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 		/*
1720Sstevel@tonic-gate 		 * Load 'em up. Pick up the function address and store
1730Sstevel@tonic-gate 		 * the values in the global pointers for other routines
1740Sstevel@tonic-gate 		 * to use.
1750Sstevel@tonic-gate 		 */
1760Sstevel@tonic-gate 		for (ftp = dl_table; ftp->fptr != (void *)0; ftp++) {
1770Sstevel@tonic-gate 			if ((*ftp->fptr = dlsym(dp, ftp->fname)) == NULL) {
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 				/*
1800Sstevel@tonic-gate 				 * If old libsdssc library is there, then
1810Sstevel@tonic-gate 				 * sdssc_mo_create_begin is not yet supported.
1820Sstevel@tonic-gate 				 */
1830Sstevel@tonic-gate 				if (strcmp(ftp->fname,
1840Sstevel@tonic-gate 				    "sdssc_mo_create_begin")) {
1850Sstevel@tonic-gate 					*ftp->fptr = (void *)&not_bound_error;
1860Sstevel@tonic-gate 					continue;
1870Sstevel@tonic-gate 				}
1880Sstevel@tonic-gate 				/*
1890Sstevel@tonic-gate 				 * If this routine fails to find a single
1900Sstevel@tonic-gate 				 * entry point that it's expecting
1910Sstevel@tonic-gate 				 * (except sdssc_mo_create_begin) then
1920Sstevel@tonic-gate 				 * setup non-sdssc stubs routines
1930Sstevel@tonic-gate 				 * as function pointers.
1940Sstevel@tonic-gate 				 */
1950Sstevel@tonic-gate 				set_common_routine();
1960Sstevel@tonic-gate 				return (SDSSC_ERROR);
1970Sstevel@tonic-gate 			}
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		return (SDSSC_OKAY);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate }
203