xref: /onnv-gate/usr/src/lib/libdscfg/common/cfg_cluster.c (revision 7836:4e95154b5b7a)
1*7836SJohn.Forte@Sun.COM /*
2*7836SJohn.Forte@Sun.COM  * CDDL HEADER START
3*7836SJohn.Forte@Sun.COM  *
4*7836SJohn.Forte@Sun.COM  * The contents of this file are subject to the terms of the
5*7836SJohn.Forte@Sun.COM  * Common Development and Distribution License (the "License").
6*7836SJohn.Forte@Sun.COM  * You may not use this file except in compliance with the License.
7*7836SJohn.Forte@Sun.COM  *
8*7836SJohn.Forte@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7836SJohn.Forte@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*7836SJohn.Forte@Sun.COM  * See the License for the specific language governing permissions
11*7836SJohn.Forte@Sun.COM  * and limitations under the License.
12*7836SJohn.Forte@Sun.COM  *
13*7836SJohn.Forte@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*7836SJohn.Forte@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7836SJohn.Forte@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*7836SJohn.Forte@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*7836SJohn.Forte@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*7836SJohn.Forte@Sun.COM  *
19*7836SJohn.Forte@Sun.COM  * CDDL HEADER END
20*7836SJohn.Forte@Sun.COM  */
21*7836SJohn.Forte@Sun.COM /*
22*7836SJohn.Forte@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*7836SJohn.Forte@Sun.COM  * Use is subject to license terms.
24*7836SJohn.Forte@Sun.COM  */
25*7836SJohn.Forte@Sun.COM 
26*7836SJohn.Forte@Sun.COM /*
27*7836SJohn.Forte@Sun.COM  * This file contains the glue code that allows the NWS software to
28*7836SJohn.Forte@Sun.COM  * determine whether a cluster disk service is local to this node or
29*7836SJohn.Forte@Sun.COM  * not.
30*7836SJohn.Forte@Sun.COM  *
31*7836SJohn.Forte@Sun.COM  * See PSARC/1999/462 for more information on the interfaces from
32*7836SJohn.Forte@Sun.COM  * suncluster that are used here.
33*7836SJohn.Forte@Sun.COM  */
34*7836SJohn.Forte@Sun.COM 
35*7836SJohn.Forte@Sun.COM #include <sys/types.h>
36*7836SJohn.Forte@Sun.COM #include <sys/wait.h>
37*7836SJohn.Forte@Sun.COM #include <sys/mkdev.h>
38*7836SJohn.Forte@Sun.COM #include <sys/stat.h>
39*7836SJohn.Forte@Sun.COM #include <stdlib.h>
40*7836SJohn.Forte@Sun.COM #include <unistd.h>
41*7836SJohn.Forte@Sun.COM #include <string.h>
42*7836SJohn.Forte@Sun.COM #include <strings.h>
43*7836SJohn.Forte@Sun.COM #include <errno.h>
44*7836SJohn.Forte@Sun.COM #include <fcntl.h>
45*7836SJohn.Forte@Sun.COM #include <stdio.h>
46*7836SJohn.Forte@Sun.COM #include <dlfcn.h>
47*7836SJohn.Forte@Sun.COM 
48*7836SJohn.Forte@Sun.COM #include <sys/ncall/ncall.h>
49*7836SJohn.Forte@Sun.COM #include <sys/nsctl/nsc_hash.h>
50*7836SJohn.Forte@Sun.COM 
51*7836SJohn.Forte@Sun.COM #include "cfg_cluster.h"
52*7836SJohn.Forte@Sun.COM #include "cfg_impl.h"
53*7836SJohn.Forte@Sun.COM #include "cfg.h"
54*7836SJohn.Forte@Sun.COM 
55*7836SJohn.Forte@Sun.COM /*
56*7836SJohn.Forte@Sun.COM  * Static variables
57*7836SJohn.Forte@Sun.COM  */
58*7836SJohn.Forte@Sun.COM 
59*7836SJohn.Forte@Sun.COM static scconf_nodeid_t cl_nodeid = (uint_t)0xffff;
60*7836SJohn.Forte@Sun.COM static char *cl_nodename = NULL;
61*7836SJohn.Forte@Sun.COM 
62*7836SJohn.Forte@Sun.COM static void *libscstat;
63*7836SJohn.Forte@Sun.COM static void *libscconf;
64*7836SJohn.Forte@Sun.COM 
65*7836SJohn.Forte@Sun.COM static hash_node_t **schash;
66*7836SJohn.Forte@Sun.COM static int init_sc_entry();
67*7836SJohn.Forte@Sun.COM 
68*7836SJohn.Forte@Sun.COM typedef struct hash_data_s {
69*7836SJohn.Forte@Sun.COM 	scstat_node_name_t	scstat_node_name;
70*7836SJohn.Forte@Sun.COM } hash_data_t;
71*7836SJohn.Forte@Sun.COM 
72*7836SJohn.Forte@Sun.COM /*
73*7836SJohn.Forte@Sun.COM  * Global variables
74*7836SJohn.Forte@Sun.COM  */
75*7836SJohn.Forte@Sun.COM int cl_initialized = 0;
76*7836SJohn.Forte@Sun.COM 
77*7836SJohn.Forte@Sun.COM 
78*7836SJohn.Forte@Sun.COM /*
79*7836SJohn.Forte@Sun.COM  * Tell the linker to keep quiet.
80*7836SJohn.Forte@Sun.COM  */
81*7836SJohn.Forte@Sun.COM 
82*7836SJohn.Forte@Sun.COM #pragma weak scconf_get_nodename
83*7836SJohn.Forte@Sun.COM #pragma weak scconf_strerr
84*7836SJohn.Forte@Sun.COM #pragma weak scconf_get_ds_by_devt
85*7836SJohn.Forte@Sun.COM 
86*7836SJohn.Forte@Sun.COM #pragma weak scstat_get_ds_status
87*7836SJohn.Forte@Sun.COM #pragma weak scstat_free_ds_status
88*7836SJohn.Forte@Sun.COM #pragma weak scstat_strerr
89*7836SJohn.Forte@Sun.COM 
90*7836SJohn.Forte@Sun.COM 
91*7836SJohn.Forte@Sun.COM /*
92*7836SJohn.Forte@Sun.COM  * Initialise the library if we have not done so before.
93*7836SJohn.Forte@Sun.COM  *
94*7836SJohn.Forte@Sun.COM  * - IMPORTANT -
95*7836SJohn.Forte@Sun.COM  *
96*7836SJohn.Forte@Sun.COM  * This must -never- be called from any command that can be started
97*7836SJohn.Forte@Sun.COM  * from /usr/cluster/lib/sc/run_reserve (and hence
98*7836SJohn.Forte@Sun.COM  * /usr/cluster/sbin/reconfig) or the system will deadlock
99*7836SJohn.Forte@Sun.COM  * during switchover.  This includes:
100*7836SJohn.Forte@Sun.COM  *
101*7836SJohn.Forte@Sun.COM  *       - svadm (no options, "print") -- called during sv switchover
102*7836SJohn.Forte@Sun.COM  *       - all boot commands
103*7836SJohn.Forte@Sun.COM  *
104*7836SJohn.Forte@Sun.COM  * - grab this node's cluster nodeid
105*7836SJohn.Forte@Sun.COM  * - attempt to dlopen() the suncluster shared libraries we need
106*7836SJohn.Forte@Sun.COM  * - grab this node's cluster nodename
107*7836SJohn.Forte@Sun.COM  *
108*7836SJohn.Forte@Sun.COM  * Returns:
109*7836SJohn.Forte@Sun.COM  *   0   - success
110*7836SJohn.Forte@Sun.COM  *  -1   - error, errno is set
111*7836SJohn.Forte@Sun.COM  */
112*7836SJohn.Forte@Sun.COM 
113*7836SJohn.Forte@Sun.COM int
cfg_cluster_init(void)114*7836SJohn.Forte@Sun.COM cfg_cluster_init(void)
115*7836SJohn.Forte@Sun.COM {
116*7836SJohn.Forte@Sun.COM 	const char *scconf = "/usr/cluster/lib/libscconf.so.1";
117*7836SJohn.Forte@Sun.COM 	const char *scstat = "/usr/cluster/lib/libscstat.so.1";
118*7836SJohn.Forte@Sun.COM #ifdef DEBUG
119*7836SJohn.Forte@Sun.COM 	char errbuf[SCCONF_MAXSTRINGLEN];
120*7836SJohn.Forte@Sun.COM #endif
121*7836SJohn.Forte@Sun.COM 	scconf_nodeid_t id;
122*7836SJohn.Forte@Sun.COM 	scconf_errno_t err;
123*7836SJohn.Forte@Sun.COM 	char *name;
124*7836SJohn.Forte@Sun.COM 	FILE *pipe;
125*7836SJohn.Forte@Sun.COM 	int rc;
126*7836SJohn.Forte@Sun.COM 
127*7836SJohn.Forte@Sun.COM 	/*
128*7836SJohn.Forte@Sun.COM 	 * First check to see if we really are a cluster as clinfo -n can lie
129*7836SJohn.Forte@Sun.COM 	 */
130*7836SJohn.Forte@Sun.COM 	if (cl_nodeid == 0xffff) {
131*7836SJohn.Forte@Sun.COM 		rc = system("/usr/sbin/clinfo");
132*7836SJohn.Forte@Sun.COM 		if (rc != -1 && WEXITSTATUS(rc) == 1) {
133*7836SJohn.Forte@Sun.COM 			/* not a cluster */
134*7836SJohn.Forte@Sun.COM 			cl_initialized = 1;
135*7836SJohn.Forte@Sun.COM 			cl_nodeid = 0;
136*7836SJohn.Forte@Sun.COM 			return (0);
137*7836SJohn.Forte@Sun.COM 		}
138*7836SJohn.Forte@Sun.COM 
139*7836SJohn.Forte@Sun.COM 		pipe = popen("/usr/sbin/clinfo -n 2>/dev/null || echo 0", "r");
140*7836SJohn.Forte@Sun.COM 		if (pipe == NULL) {
141*7836SJohn.Forte@Sun.COM #ifdef DEBUG
142*7836SJohn.Forte@Sun.COM 			fprintf(stderr, "unable to get nodeid: %s\n",
143*7836SJohn.Forte@Sun.COM 				strerror(errno));
144*7836SJohn.Forte@Sun.COM #endif
145*7836SJohn.Forte@Sun.COM 			return (-1);
146*7836SJohn.Forte@Sun.COM 		}
147*7836SJohn.Forte@Sun.COM 
148*7836SJohn.Forte@Sun.COM 		if ((rc = fscanf(pipe, "%d", &id)) != 1) {
149*7836SJohn.Forte@Sun.COM #ifdef DEBUG
150*7836SJohn.Forte@Sun.COM 			fprintf(stderr, "unable to get nodeid: %s\n",
151*7836SJohn.Forte@Sun.COM 				strerror(errno));
152*7836SJohn.Forte@Sun.COM #endif
153*7836SJohn.Forte@Sun.COM 			return (-1);
154*7836SJohn.Forte@Sun.COM 		}
155*7836SJohn.Forte@Sun.COM 
156*7836SJohn.Forte@Sun.COM 		pclose(pipe);
157*7836SJohn.Forte@Sun.COM 
158*7836SJohn.Forte@Sun.COM 		cl_nodeid = id;
159*7836SJohn.Forte@Sun.COM 	}
160*7836SJohn.Forte@Sun.COM 
161*7836SJohn.Forte@Sun.COM 	/* Already loaded the Sun Cluster device tree */
162*7836SJohn.Forte@Sun.COM 	if (cl_initialized)
163*7836SJohn.Forte@Sun.COM 		return (0);
164*7836SJohn.Forte@Sun.COM 
165*7836SJohn.Forte@Sun.COM 	/*
166*7836SJohn.Forte@Sun.COM 	 * Try and dlopen the various libraries that we need
167*7836SJohn.Forte@Sun.COM 	 */
168*7836SJohn.Forte@Sun.COM 
169*7836SJohn.Forte@Sun.COM 	libscconf = dlopen(scconf, RTLD_LAZY | RTLD_GLOBAL);
170*7836SJohn.Forte@Sun.COM 	if (libscconf == NULL)
171*7836SJohn.Forte@Sun.COM 		goto error;
172*7836SJohn.Forte@Sun.COM 
173*7836SJohn.Forte@Sun.COM 	libscstat = dlopen(scstat, RTLD_LAZY | RTLD_GLOBAL);
174*7836SJohn.Forte@Sun.COM 	if (libscstat == NULL)
175*7836SJohn.Forte@Sun.COM 		goto error;
176*7836SJohn.Forte@Sun.COM 
177*7836SJohn.Forte@Sun.COM 	err = scconf_get_nodename(id, &name);
178*7836SJohn.Forte@Sun.COM 	if (err == SCCONF_EPERM) {
179*7836SJohn.Forte@Sun.COM 		cl_nodename = NULL;
180*7836SJohn.Forte@Sun.COM 	} else if (err != SCCONF_NOERR) {
181*7836SJohn.Forte@Sun.COM #ifdef DEBUG
182*7836SJohn.Forte@Sun.COM 		scconf_strerr(errbuf, err);
183*7836SJohn.Forte@Sun.COM 		fprintf(stderr, "scconf_get_nodename: %d: %s\n", err, errbuf);
184*7836SJohn.Forte@Sun.COM #endif
185*7836SJohn.Forte@Sun.COM 		goto error;
186*7836SJohn.Forte@Sun.COM 	} else
187*7836SJohn.Forte@Sun.COM 		cl_nodename = name;
188*7836SJohn.Forte@Sun.COM 
189*7836SJohn.Forte@Sun.COM 	/* Load the Sun Cluster device tree */
190*7836SJohn.Forte@Sun.COM 	init_sc_entry();
191*7836SJohn.Forte@Sun.COM 	cl_initialized = 1;
192*7836SJohn.Forte@Sun.COM 	return (0);
193*7836SJohn.Forte@Sun.COM 
194*7836SJohn.Forte@Sun.COM error:	/* error cleanup */
195*7836SJohn.Forte@Sun.COM 	if (libscconf)
196*7836SJohn.Forte@Sun.COM 		dlclose(libscconf);
197*7836SJohn.Forte@Sun.COM 
198*7836SJohn.Forte@Sun.COM 	if (libscstat)
199*7836SJohn.Forte@Sun.COM 		dlclose(libscstat);
200*7836SJohn.Forte@Sun.COM 
201*7836SJohn.Forte@Sun.COM 	libscconf = NULL;
202*7836SJohn.Forte@Sun.COM 	libscstat = NULL;
203*7836SJohn.Forte@Sun.COM 
204*7836SJohn.Forte@Sun.COM 	errno = ENOSYS;
205*7836SJohn.Forte@Sun.COM 	return (-1);
206*7836SJohn.Forte@Sun.COM }
207*7836SJohn.Forte@Sun.COM 
208*7836SJohn.Forte@Sun.COM 
209*7836SJohn.Forte@Sun.COM /*
210*7836SJohn.Forte@Sun.COM  * cfg_issuncluster()
211*7836SJohn.Forte@Sun.COM  *
212*7836SJohn.Forte@Sun.COM  * Description:
213*7836SJohn.Forte@Sun.COM  *  Return the SunCluster nodeid of this node.
214*7836SJohn.Forte@Sun.COM  *
215*7836SJohn.Forte@Sun.COM  * Returns:
216*7836SJohn.Forte@Sun.COM  *  >0   - running in a SunCluster (value is nodeid of this node)
217*7836SJohn.Forte@Sun.COM  *   0   - not running in a cluster
218*7836SJohn.Forte@Sun.COM  *  -1   - failure; errno is set
219*7836SJohn.Forte@Sun.COM  */
220*7836SJohn.Forte@Sun.COM 
221*7836SJohn.Forte@Sun.COM int
cfg_issuncluster()222*7836SJohn.Forte@Sun.COM cfg_issuncluster()
223*7836SJohn.Forte@Sun.COM {
224*7836SJohn.Forte@Sun.COM 	if (cfg_cluster_init() >= 0)
225*7836SJohn.Forte@Sun.COM 		return ((int)cl_nodeid);
226*7836SJohn.Forte@Sun.COM 	else
227*7836SJohn.Forte@Sun.COM 		return (-1);
228*7836SJohn.Forte@Sun.COM }
229*7836SJohn.Forte@Sun.COM int
cfg_iscluster()230*7836SJohn.Forte@Sun.COM cfg_iscluster()
231*7836SJohn.Forte@Sun.COM {
232*7836SJohn.Forte@Sun.COM 	return (cfg_issuncluster());
233*7836SJohn.Forte@Sun.COM }
234*7836SJohn.Forte@Sun.COM 
235*7836SJohn.Forte@Sun.COM /*
236*7836SJohn.Forte@Sun.COM  * cfg_l_dgname_islocal()
237*7836SJohn.Forte@Sun.COM  * Check if disk group is local on a non-SunCluster.
238*7836SJohn.Forte@Sun.COM  *
239*7836SJohn.Forte@Sun.COM  * Returns as cfg_dgname_islocal().
240*7836SJohn.Forte@Sun.COM  */
241*7836SJohn.Forte@Sun.COM #ifndef lint
242*7836SJohn.Forte@Sun.COM static int
cfg_l_dgname_islocal(char * dgname,char ** othernode)243*7836SJohn.Forte@Sun.COM cfg_l_dgname_islocal(char *dgname, char **othernode)
244*7836SJohn.Forte@Sun.COM {
245*7836SJohn.Forte@Sun.COM 	const char *metaset = "/usr/sbin/metaset -s %s -o > /dev/null 2>&1";
246*7836SJohn.Forte@Sun.COM 	char command[1024];
247*7836SJohn.Forte@Sun.COM 	int rc;
248*7836SJohn.Forte@Sun.COM 
249*7836SJohn.Forte@Sun.COM 	if (snprintf(command, sizeof (command), metaset, dgname) >=
250*7836SJohn.Forte@Sun.COM 	    sizeof (command)) {
251*7836SJohn.Forte@Sun.COM 		errno = ENOMEM;
252*7836SJohn.Forte@Sun.COM 		return (-1);
253*7836SJohn.Forte@Sun.COM 	}
254*7836SJohn.Forte@Sun.COM 
255*7836SJohn.Forte@Sun.COM 	rc = system(command);
256*7836SJohn.Forte@Sun.COM 	if (rc < 0) {
257*7836SJohn.Forte@Sun.COM 		return (-1);
258*7836SJohn.Forte@Sun.COM 	}
259*7836SJohn.Forte@Sun.COM 
260*7836SJohn.Forte@Sun.COM 	if (WEXITSTATUS(rc) != 0) {
261*7836SJohn.Forte@Sun.COM 		if (othernode) {
262*7836SJohn.Forte@Sun.COM 			/* metaset doesn't tell us */
263*7836SJohn.Forte@Sun.COM 			*othernode = "unknown";
264*7836SJohn.Forte@Sun.COM 		}
265*7836SJohn.Forte@Sun.COM 
266*7836SJohn.Forte@Sun.COM 		return (0);
267*7836SJohn.Forte@Sun.COM 	}
268*7836SJohn.Forte@Sun.COM 
269*7836SJohn.Forte@Sun.COM 	return (1);
270*7836SJohn.Forte@Sun.COM }
271*7836SJohn.Forte@Sun.COM #endif
272*7836SJohn.Forte@Sun.COM 
273*7836SJohn.Forte@Sun.COM /*
274*7836SJohn.Forte@Sun.COM  * cfg_dgname_islocal(char *dgname, char **othernode)
275*7836SJohn.Forte@Sun.COM  * -- determine if the named disk service is mastered on this node
276*7836SJohn.Forte@Sun.COM  *
277*7836SJohn.Forte@Sun.COM  * If the disk service is mastered on another node, that nodename
278*7836SJohn.Forte@Sun.COM  * will be returned in othernode (if not NULL).  It is up to the
279*7836SJohn.Forte@Sun.COM  * calling program to call free() on this value at a later time to
280*7836SJohn.Forte@Sun.COM  * free the memory allocated.
281*7836SJohn.Forte@Sun.COM  *
282*7836SJohn.Forte@Sun.COM  * Returns:
283*7836SJohn.Forte@Sun.COM  *   1   - disk service is mastered on this node
284*7836SJohn.Forte@Sun.COM  *   0   - disk service is not mastered on this node (*othernode set)
285*7836SJohn.Forte@Sun.COM  *   -1  - error (errno will be set)
286*7836SJohn.Forte@Sun.COM  */
287*7836SJohn.Forte@Sun.COM 
288*7836SJohn.Forte@Sun.COM int
cfg_dgname_islocal(char * dgname,char ** othernode)289*7836SJohn.Forte@Sun.COM cfg_dgname_islocal(char *dgname, char **othernode)
290*7836SJohn.Forte@Sun.COM {
291*7836SJohn.Forte@Sun.COM 	hash_data_t *data;
292*7836SJohn.Forte@Sun.COM 
293*7836SJohn.Forte@Sun.COM 	if (dgname == NULL || *dgname == '\0' || othernode == NULL) {
294*7836SJohn.Forte@Sun.COM 		errno = EINVAL;
295*7836SJohn.Forte@Sun.COM 		return (-1);
296*7836SJohn.Forte@Sun.COM 	}
297*7836SJohn.Forte@Sun.COM 
298*7836SJohn.Forte@Sun.COM 	/* Handle non-cluster configurations */
299*7836SJohn.Forte@Sun.COM 	if (cfg_cluster_init() < 0) {
300*7836SJohn.Forte@Sun.COM 		return (-1);
301*7836SJohn.Forte@Sun.COM 	} else 	if (cl_nodeid == 0) {
302*7836SJohn.Forte@Sun.COM 		/* it has to be local */
303*7836SJohn.Forte@Sun.COM 		return (1);
304*7836SJohn.Forte@Sun.COM 	}
305*7836SJohn.Forte@Sun.COM 
306*7836SJohn.Forte@Sun.COM 	/*
307*7836SJohn.Forte@Sun.COM 	 * lookup the current diskgroup name
308*7836SJohn.Forte@Sun.COM 	 */
309*7836SJohn.Forte@Sun.COM 	if (data = (hash_data_t *)nsc_lookup(schash, dgname)) {
310*7836SJohn.Forte@Sun.COM 		if (strcmp(data->scstat_node_name, cl_nodename)) {
311*7836SJohn.Forte@Sun.COM 			if (othernode)
312*7836SJohn.Forte@Sun.COM 			    *othernode = strdup(data->scstat_node_name);
313*7836SJohn.Forte@Sun.COM 			return (0);
314*7836SJohn.Forte@Sun.COM 		} else {
315*7836SJohn.Forte@Sun.COM 			return (1);
316*7836SJohn.Forte@Sun.COM 		}
317*7836SJohn.Forte@Sun.COM 	} else {
318*7836SJohn.Forte@Sun.COM 		errno = ENODEV;
319*7836SJohn.Forte@Sun.COM 		return (-1);
320*7836SJohn.Forte@Sun.COM 	}
321*7836SJohn.Forte@Sun.COM }
322*7836SJohn.Forte@Sun.COM 
323*7836SJohn.Forte@Sun.COM /*
324*7836SJohn.Forte@Sun.COM  * cfg_l_dgname()
325*7836SJohn.Forte@Sun.COM  * parse the disk group name from the a device pathname on a non-SunCluster.
326*7836SJohn.Forte@Sun.COM  *
327*7836SJohn.Forte@Sun.COM  * Returns as cfg_dgname().
328*7836SJohn.Forte@Sun.COM  */
329*7836SJohn.Forte@Sun.COM 
330*7836SJohn.Forte@Sun.COM char *
cfg_l_dgname(const char * pathname,char * buffer,size_t buflen)331*7836SJohn.Forte@Sun.COM cfg_l_dgname(const char *pathname, char *buffer, size_t buflen)
332*7836SJohn.Forte@Sun.COM {
333*7836SJohn.Forte@Sun.COM 	const char *dev = "/dev/";
334*7836SJohn.Forte@Sun.COM 	const char *vx = "vx/";
335*7836SJohn.Forte@Sun.COM 	const char *md = "md/";
336*7836SJohn.Forte@Sun.COM 	const char *dsk = "dsk/";
337*7836SJohn.Forte@Sun.COM 	const char *start, *cp;
338*7836SJohn.Forte@Sun.COM 	int ll, len, chkdsk;
339*7836SJohn.Forte@Sun.COM 
340*7836SJohn.Forte@Sun.COM 	bzero(buffer, buflen);
341*7836SJohn.Forte@Sun.COM 	chkdsk = 0;
342*7836SJohn.Forte@Sun.COM 
343*7836SJohn.Forte@Sun.COM 	ll = strlen(dev);
344*7836SJohn.Forte@Sun.COM 	if (strncmp(pathname, dev, ll) != 0) {
345*7836SJohn.Forte@Sun.COM 		/* not a device pathname */
346*7836SJohn.Forte@Sun.COM 		errno = EINVAL;
347*7836SJohn.Forte@Sun.COM 		return ((char *)NULL);
348*7836SJohn.Forte@Sun.COM 	}
349*7836SJohn.Forte@Sun.COM 
350*7836SJohn.Forte@Sun.COM 	start = pathname + ll;
351*7836SJohn.Forte@Sun.COM 
352*7836SJohn.Forte@Sun.COM 	if (strncmp(start, md, (ll = strlen(md))) == 0) {
353*7836SJohn.Forte@Sun.COM 		/*
354*7836SJohn.Forte@Sun.COM 		 * SVM --
355*7836SJohn.Forte@Sun.COM 		 * /dev/md/dgname/{r}dsk/partition
356*7836SJohn.Forte@Sun.COM 		 */
357*7836SJohn.Forte@Sun.COM 
358*7836SJohn.Forte@Sun.COM 		start += ll;
359*7836SJohn.Forte@Sun.COM 
360*7836SJohn.Forte@Sun.COM 		if (strncmp(start, dsk, strlen(dsk)) == 0 ||
361*7836SJohn.Forte@Sun.COM 		    (*start == 'r' &&
362*7836SJohn.Forte@Sun.COM 		    strncmp((start + 1), dsk, strlen(dsk)) == 0)) {
363*7836SJohn.Forte@Sun.COM 			/* no dgname */
364*7836SJohn.Forte@Sun.COM 			return (buffer);
365*7836SJohn.Forte@Sun.COM 		}
366*7836SJohn.Forte@Sun.COM 
367*7836SJohn.Forte@Sun.COM 		chkdsk = 1;	/* check for trailing {r}dsk */
368*7836SJohn.Forte@Sun.COM 	} else if (strncmp(start, vx, (ll = strlen(vx))) == 0) {
369*7836SJohn.Forte@Sun.COM 		/*
370*7836SJohn.Forte@Sun.COM 		 * Veritas --
371*7836SJohn.Forte@Sun.COM 		 * /dev/vx/{r}dsk/dgname/partition
372*7836SJohn.Forte@Sun.COM 		 */
373*7836SJohn.Forte@Sun.COM 
374*7836SJohn.Forte@Sun.COM 		start += ll;
375*7836SJohn.Forte@Sun.COM 
376*7836SJohn.Forte@Sun.COM 		ll = strlen(dsk);
377*7836SJohn.Forte@Sun.COM 
378*7836SJohn.Forte@Sun.COM 		if (*start == 'r' && strncmp((start + 1), dsk, ll) == 0)
379*7836SJohn.Forte@Sun.COM 			start += ll + 1;
380*7836SJohn.Forte@Sun.COM 		else if (strncmp(start, dsk, ll) == 0)
381*7836SJohn.Forte@Sun.COM 			start += ll;
382*7836SJohn.Forte@Sun.COM 		else {
383*7836SJohn.Forte@Sun.COM 			/* no dgname */
384*7836SJohn.Forte@Sun.COM 			return (buffer);
385*7836SJohn.Forte@Sun.COM 		}
386*7836SJohn.Forte@Sun.COM 	} else {
387*7836SJohn.Forte@Sun.COM 		/* no dgname */
388*7836SJohn.Forte@Sun.COM 		return (buffer);
389*7836SJohn.Forte@Sun.COM 	}
390*7836SJohn.Forte@Sun.COM 
391*7836SJohn.Forte@Sun.COM 	for (cp = start, len = 0; *cp != '\0' && *cp != '/'; cp++)
392*7836SJohn.Forte@Sun.COM 		len++;	/* count length of dgname */
393*7836SJohn.Forte@Sun.COM 
394*7836SJohn.Forte@Sun.COM 	if (*cp == '\0') {
395*7836SJohn.Forte@Sun.COM 		/* no dgname */
396*7836SJohn.Forte@Sun.COM 		return (buffer);
397*7836SJohn.Forte@Sun.COM 	}
398*7836SJohn.Forte@Sun.COM 
399*7836SJohn.Forte@Sun.COM #ifdef DEBUG
400*7836SJohn.Forte@Sun.COM 	if (*cp != '/') {
401*7836SJohn.Forte@Sun.COM 		fprintf(stderr,
402*7836SJohn.Forte@Sun.COM 		    "cfg_dgname: parse error: *cp = '%c', expected '/'\n", *cp);
403*7836SJohn.Forte@Sun.COM 		errno = EPROTO;
404*7836SJohn.Forte@Sun.COM 		return ((char *)NULL);
405*7836SJohn.Forte@Sun.COM 	}
406*7836SJohn.Forte@Sun.COM #endif
407*7836SJohn.Forte@Sun.COM 
408*7836SJohn.Forte@Sun.COM 	if (chkdsk) {
409*7836SJohn.Forte@Sun.COM 		cp++;	/* skip the NULL */
410*7836SJohn.Forte@Sun.COM 
411*7836SJohn.Forte@Sun.COM 		ll = strlen(dsk);
412*7836SJohn.Forte@Sun.COM 
413*7836SJohn.Forte@Sun.COM 		if ((*cp != 'r' || strncmp((cp + 1), dsk, ll) != 0) &&
414*7836SJohn.Forte@Sun.COM 		    strncmp(cp, dsk, ll) != 0) {
415*7836SJohn.Forte@Sun.COM 			/* no dgname */
416*7836SJohn.Forte@Sun.COM 			return (buffer);
417*7836SJohn.Forte@Sun.COM 		}
418*7836SJohn.Forte@Sun.COM 	}
419*7836SJohn.Forte@Sun.COM 
420*7836SJohn.Forte@Sun.COM 	if (len >= buflen) {
421*7836SJohn.Forte@Sun.COM 		errno = E2BIG;
422*7836SJohn.Forte@Sun.COM 		return ((char *)NULL);
423*7836SJohn.Forte@Sun.COM 	}
424*7836SJohn.Forte@Sun.COM 
425*7836SJohn.Forte@Sun.COM 	(void) strncpy(buffer, start, len);
426*7836SJohn.Forte@Sun.COM 	return (buffer);
427*7836SJohn.Forte@Sun.COM }
428*7836SJohn.Forte@Sun.COM 
429*7836SJohn.Forte@Sun.COM 
430*7836SJohn.Forte@Sun.COM /*
431*7836SJohn.Forte@Sun.COM  * cfg_dgname()
432*7836SJohn.Forte@Sun.COM  * determine which cluster resource group the pathname belongs to, if any
433*7836SJohn.Forte@Sun.COM  *
434*7836SJohn.Forte@Sun.COM  * Returns:
435*7836SJohn.Forte@Sun.COM  *	NULL			- error (errno is set)
436*7836SJohn.Forte@Sun.COM  *	ptr to NULL-string	- no dgname
437*7836SJohn.Forte@Sun.COM  *	pointer to string	- dgname
438*7836SJohn.Forte@Sun.COM  */
439*7836SJohn.Forte@Sun.COM 
440*7836SJohn.Forte@Sun.COM char *
cfg_dgname(const char * pathname,char * buffer,size_t buflen)441*7836SJohn.Forte@Sun.COM cfg_dgname(const char *pathname, char *buffer, size_t buflen)
442*7836SJohn.Forte@Sun.COM {
443*7836SJohn.Forte@Sun.COM 	scconf_errno_t conferr;
444*7836SJohn.Forte@Sun.COM 	char *dsname = NULL;
445*7836SJohn.Forte@Sun.COM 	struct stat stb;
446*7836SJohn.Forte@Sun.COM #ifdef DEBUG
447*7836SJohn.Forte@Sun.COM 	char errbuf[SCCONF_MAXSTRINGLEN];
448*7836SJohn.Forte@Sun.COM #endif
449*7836SJohn.Forte@Sun.COM 
450*7836SJohn.Forte@Sun.COM 	bzero(buffer, buflen);
451*7836SJohn.Forte@Sun.COM 
452*7836SJohn.Forte@Sun.COM 	if (pathname == NULL || *pathname == '\0') {
453*7836SJohn.Forte@Sun.COM 		errno = EINVAL;
454*7836SJohn.Forte@Sun.COM 		return ((char *)NULL);
455*7836SJohn.Forte@Sun.COM 	}
456*7836SJohn.Forte@Sun.COM 
457*7836SJohn.Forte@Sun.COM 	/* Handle non-cluster configurations */
458*7836SJohn.Forte@Sun.COM 	if (cfg_cluster_init() < 0) {
459*7836SJohn.Forte@Sun.COM 		errno = EINVAL;
460*7836SJohn.Forte@Sun.COM 		return ((char *)NULL);
461*7836SJohn.Forte@Sun.COM 	} else 	if (cl_nodeid == 0) {
462*7836SJohn.Forte@Sun.COM 		/* must be local - return NULL-string dgname */
463*7836SJohn.Forte@Sun.COM 		return (buffer);
464*7836SJohn.Forte@Sun.COM 	}
465*7836SJohn.Forte@Sun.COM 
466*7836SJohn.Forte@Sun.COM 	if (stat(pathname, &stb) < 0) {
467*7836SJohn.Forte@Sun.COM 		errno = EINVAL;
468*7836SJohn.Forte@Sun.COM 		return ((char *)NULL);
469*7836SJohn.Forte@Sun.COM 	}
470*7836SJohn.Forte@Sun.COM 
471*7836SJohn.Forte@Sun.COM 	conferr = scconf_get_ds_by_devt(major(stb.st_rdev),
472*7836SJohn.Forte@Sun.COM 	    minor(stb.st_rdev), &dsname);
473*7836SJohn.Forte@Sun.COM 
474*7836SJohn.Forte@Sun.COM 	if (conferr == SCCONF_ENOEXIST) {
475*7836SJohn.Forte@Sun.COM 		return (buffer);
476*7836SJohn.Forte@Sun.COM 	} else if (conferr != SCCONF_NOERR) {
477*7836SJohn.Forte@Sun.COM #ifdef DEBUG
478*7836SJohn.Forte@Sun.COM 		scconf_strerr(errbuf, conferr);
479*7836SJohn.Forte@Sun.COM 		fprintf(stderr,
480*7836SJohn.Forte@Sun.COM 		    "scconf_get_ds_by_devt: %d: %s\n", conferr, errbuf);
481*7836SJohn.Forte@Sun.COM #endif
482*7836SJohn.Forte@Sun.COM 		errno = EINVAL;
483*7836SJohn.Forte@Sun.COM 		return ((char *)NULL);
484*7836SJohn.Forte@Sun.COM 	}
485*7836SJohn.Forte@Sun.COM 
486*7836SJohn.Forte@Sun.COM 	strncpy(buffer, dsname, buflen);
487*7836SJohn.Forte@Sun.COM 	free(dsname);
488*7836SJohn.Forte@Sun.COM 
489*7836SJohn.Forte@Sun.COM 	return (buffer);
490*7836SJohn.Forte@Sun.COM }
491*7836SJohn.Forte@Sun.COM 
492*7836SJohn.Forte@Sun.COM 
493*7836SJohn.Forte@Sun.COM /*
494*7836SJohn.Forte@Sun.COM  * init_sc_entry
495*7836SJohn.Forte@Sun.COM  *
496*7836SJohn.Forte@Sun.COM  * Add an entry into the sclist and the schash for future lookups.
497*7836SJohn.Forte@Sun.COM  *
498*7836SJohn.Forte@Sun.COM  * - IMPORTANT -
499*7836SJohn.Forte@Sun.COM  *
500*7836SJohn.Forte@Sun.COM  * This must -never- be called from any command that can be started
501*7836SJohn.Forte@Sun.COM  * from /usr/cluster/lib/sc/run_reserve (and hence
502*7836SJohn.Forte@Sun.COM  * /usr/cluster/sbin/reconfig) or the system will deadlock
503*7836SJohn.Forte@Sun.COM  * during switchover.  This includes:
504*7836SJohn.Forte@Sun.COM  *
505*7836SJohn.Forte@Sun.COM  *       - svadm (no options, "print") -- called during sv switchover
506*7836SJohn.Forte@Sun.COM  *       - all boot commands
507*7836SJohn.Forte@Sun.COM  *
508*7836SJohn.Forte@Sun.COM  * Return values:
509*7836SJohn.Forte@Sun.COM  *  -1  An error occurred.
510*7836SJohn.Forte@Sun.COM  *   0  Entry added
511*7836SJohn.Forte@Sun.COM  *   1  Entry already exists.
512*7836SJohn.Forte@Sun.COM  */
513*7836SJohn.Forte@Sun.COM static int
init_sc_entry()514*7836SJohn.Forte@Sun.COM init_sc_entry()
515*7836SJohn.Forte@Sun.COM {
516*7836SJohn.Forte@Sun.COM 	scstat_ds_node_state_t *dsn;
517*7836SJohn.Forte@Sun.COM 	scstat_ds_name_t dsname;
518*7836SJohn.Forte@Sun.COM 	scstat_ds_t *dsstatus, *dsp;
519*7836SJohn.Forte@Sun.COM 	scstat_errno_t err;
520*7836SJohn.Forte@Sun.COM #ifdef DEBUG
521*7836SJohn.Forte@Sun.COM 	char errbuf[SCCONF_MAXSTRINGLEN];
522*7836SJohn.Forte@Sun.COM #endif
523*7836SJohn.Forte@Sun.COM 
524*7836SJohn.Forte@Sun.COM 	hash_data_t *hdp;
525*7836SJohn.Forte@Sun.COM 
526*7836SJohn.Forte@Sun.COM 	/*
527*7836SJohn.Forte@Sun.COM 	 * Allocate a hash table
528*7836SJohn.Forte@Sun.COM 	 */
529*7836SJohn.Forte@Sun.COM 	if ((schash = nsc_create_hash()) == NULL)
530*7836SJohn.Forte@Sun.COM 		return (-1);
531*7836SJohn.Forte@Sun.COM 
532*7836SJohn.Forte@Sun.COM 	/*
533*7836SJohn.Forte@Sun.COM 	 * the API is broken here - the function is written to expect
534*7836SJohn.Forte@Sun.COM 	 * the first argument to be (scstat_ds_name_t), but the function
535*7836SJohn.Forte@Sun.COM 	 * declaration in scstat.h requires (scstat_ds_name_t *).
536*7836SJohn.Forte@Sun.COM 	 *
537*7836SJohn.Forte@Sun.COM 	 * We just cast it to get rid of the compiler warnings.
538*7836SJohn.Forte@Sun.COM 	 * If "dsname" is NULL, information for all device services is returned
539*7836SJohn.Forte@Sun.COM 	 */
540*7836SJohn.Forte@Sun.COM 	dsstatus = NULL;
541*7836SJohn.Forte@Sun.COM 	dsname = NULL;
542*7836SJohn.Forte@Sun.COM 	/* LINTED pointer alignment */
543*7836SJohn.Forte@Sun.COM 	err = scstat_get_ds_status((scstat_ds_name_t *)dsname, &dsstatus);
544*7836SJohn.Forte@Sun.COM 	if (err != SCSTAT_ENOERR) {
545*7836SJohn.Forte@Sun.COM #ifdef DEBUG
546*7836SJohn.Forte@Sun.COM 		scstat_strerr(err, errbuf);
547*7836SJohn.Forte@Sun.COM 		fprintf(stderr, "scstat_get_ds_status(): %d: %s\n",
548*7836SJohn.Forte@Sun.COM 		    err, errbuf);
549*7836SJohn.Forte@Sun.COM #endif
550*7836SJohn.Forte@Sun.COM 		errno = ENOSYS;
551*7836SJohn.Forte@Sun.COM 		return (-1);
552*7836SJohn.Forte@Sun.COM 	}
553*7836SJohn.Forte@Sun.COM 
554*7836SJohn.Forte@Sun.COM 	if (dsstatus == NULL) {
555*7836SJohn.Forte@Sun.COM 		errno = ENODEV;
556*7836SJohn.Forte@Sun.COM 		return (-1);
557*7836SJohn.Forte@Sun.COM 	}
558*7836SJohn.Forte@Sun.COM 
559*7836SJohn.Forte@Sun.COM 	/*
560*7836SJohn.Forte@Sun.COM 	 * Traverse scstat_ds list, saving away resource in out hash table
561*7836SJohn.Forte@Sun.COM 	 */
562*7836SJohn.Forte@Sun.COM 	for (dsp = dsstatus; dsp; dsp = dsp->scstat_ds_next) {
563*7836SJohn.Forte@Sun.COM 
564*7836SJohn.Forte@Sun.COM 		/* Skip over NULL scstat_ds_name's */
565*7836SJohn.Forte@Sun.COM 		if ((dsp->scstat_ds_name == NULL) ||
566*7836SJohn.Forte@Sun.COM 		    (dsp->scstat_ds_name[0] == '\0'))
567*7836SJohn.Forte@Sun.COM 			continue;
568*7836SJohn.Forte@Sun.COM 
569*7836SJohn.Forte@Sun.COM 		/* See element exits already, error if so */
570*7836SJohn.Forte@Sun.COM 		if (nsc_lookup(schash, dsp->scstat_ds_name)) {
571*7836SJohn.Forte@Sun.COM 			fprintf(stderr, "scstat_get_ds_status: duplicate %s",
572*7836SJohn.Forte@Sun.COM 				dsp->scstat_ds_name);
573*7836SJohn.Forte@Sun.COM 			errno = EEXIST;
574*7836SJohn.Forte@Sun.COM 			return (-1);
575*7836SJohn.Forte@Sun.COM 		}
576*7836SJohn.Forte@Sun.COM 
577*7836SJohn.Forte@Sun.COM 		/* Traverse the node status list */
578*7836SJohn.Forte@Sun.COM 		for (dsn = dsp->scstat_node_state_list; dsn;
579*7836SJohn.Forte@Sun.COM 					dsn = dsn->scstat_node_next) {
580*7836SJohn.Forte@Sun.COM 			/*
581*7836SJohn.Forte@Sun.COM 			 * Only keep trace of primary nodes
582*7836SJohn.Forte@Sun.COM 			 */
583*7836SJohn.Forte@Sun.COM 			if (dsn->scstat_node_state != SCSTAT_PRIMARY)
584*7836SJohn.Forte@Sun.COM 				continue;
585*7836SJohn.Forte@Sun.COM 
586*7836SJohn.Forte@Sun.COM 			/* Create an element to insert */
587*7836SJohn.Forte@Sun.COM 			hdp = (hash_data_t *)malloc(sizeof (hash_data_t));
588*7836SJohn.Forte@Sun.COM 			hdp->scstat_node_name = strdup(dsn->scstat_node_name);
589*7836SJohn.Forte@Sun.COM 			nsc_insert_node(schash, hdp, dsp->scstat_ds_name);
590*7836SJohn.Forte@Sun.COM 		}
591*7836SJohn.Forte@Sun.COM 	}
592*7836SJohn.Forte@Sun.COM 
593*7836SJohn.Forte@Sun.COM 	/*
594*7836SJohn.Forte@Sun.COM 	 * Free up scstat resources
595*7836SJohn.Forte@Sun.COM 	 */
596*7836SJohn.Forte@Sun.COM 	scstat_free_ds_status(dsstatus);
597*7836SJohn.Forte@Sun.COM 	return (0);
598*7836SJohn.Forte@Sun.COM }
599