summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdscfg/common/cfg_cluster.c
blob: 2524986e56574d2e37c7832995a9fe51f3c8bb00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * This file contains the glue code that allows the NWS software to
 * determine whether a cluster disk service is local to this node or
 * not.
 *
 * See PSARC/1999/462 for more information on the interfaces from
 * suncluster that are used here.
 */

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mkdev.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <dlfcn.h>

#include <sys/ncall/ncall.h>
#include <sys/nsctl/nsc_hash.h>

#include "cfg_cluster.h"
#include "cfg_impl.h"
#include "cfg.h"

/*
 * Static variables
 */

static scconf_nodeid_t cl_nodeid = (uint_t)0xffff;
static char *cl_nodename = NULL;

static void *libscstat;
static void *libscconf;

static hash_node_t **schash;
static int init_sc_entry();

typedef struct hash_data_s {
	scstat_node_name_t	scstat_node_name;
} hash_data_t;

/*
 * Global variables
 */
int cl_initialized = 0;


/*
 * Tell the linker to keep quiet.
 */

#pragma weak scconf_get_nodename
#pragma weak scconf_strerr
#pragma weak scconf_get_ds_by_devt

#pragma weak scstat_get_ds_status
#pragma weak scstat_free_ds_status
#pragma weak scstat_strerr


/*
 * Initialise the library if we have not done so before.
 *
 * - IMPORTANT -
 *
 * This must -never- be called from any command that can be started
 * from /usr/cluster/lib/sc/run_reserve (and hence
 * /usr/cluster/sbin/reconfig) or the system will deadlock
 * during switchover.  This includes:
 *
 *       - svadm (no options, "print") -- called during sv switchover
 *       - all boot commands
 *
 * - grab this node's cluster nodeid
 * - attempt to dlopen() the suncluster shared libraries we need
 * - grab this node's cluster nodename
 *
 * Returns:
 *   0   - success
 *  -1   - error, errno is set
 */

int
cfg_cluster_init(void)
{
	const char *scconf = "/usr/cluster/lib/libscconf.so.1";
	const char *scstat = "/usr/cluster/lib/libscstat.so.1";
#ifdef DEBUG
	char errbuf[SCCONF_MAXSTRINGLEN];
#endif
	scconf_nodeid_t id;
	scconf_errno_t err;
	char *name;
	FILE *pipe;
	int rc;

	/*
	 * First check to see if we really are a cluster as clinfo -n can lie
	 */
	if (cl_nodeid == 0xffff) {
		rc = system("/usr/sbin/clinfo");
		if (rc != -1 && WEXITSTATUS(rc) == 1) {
			/* not a cluster */
			cl_initialized = 1;
			cl_nodeid = 0;
			return (0);
		}

		pipe = popen("/usr/sbin/clinfo -n 2>/dev/null || echo 0", "r");
		if (pipe == NULL) {
#ifdef DEBUG
			fprintf(stderr, "unable to get nodeid: %s\n",
				strerror(errno));
#endif
			return (-1);
		}

		if ((rc = fscanf(pipe, "%d", &id)) != 1) {
#ifdef DEBUG
			fprintf(stderr, "unable to get nodeid: %s\n",
				strerror(errno));
#endif
			return (-1);
		}

		pclose(pipe);

		cl_nodeid = id;
	}

	/* Already loaded the Sun Cluster device tree */
	if (cl_initialized)
		return (0);

	/*
	 * Try and dlopen the various libraries that we need
	 */

	libscconf = dlopen(scconf, RTLD_LAZY | RTLD_GLOBAL);
	if (libscconf == NULL)
		goto error;

	libscstat = dlopen(scstat, RTLD_LAZY | RTLD_GLOBAL);
	if (libscstat == NULL)
		goto error;

	err = scconf_get_nodename(id, &name);
	if (err == SCCONF_EPERM) {
		cl_nodename = NULL;
	} else if (err != SCCONF_NOERR) {
#ifdef DEBUG
		scconf_strerr(errbuf, err);
		fprintf(stderr, "scconf_get_nodename: %d: %s\n", err, errbuf);
#endif
		goto error;
	} else
		cl_nodename = name;

	/* Load the Sun Cluster device tree */
	init_sc_entry();
	cl_initialized = 1;
	return (0);

error:	/* error cleanup */
	if (libscconf)
		dlclose(libscconf);

	if (libscstat)
		dlclose(libscstat);

	libscconf = NULL;
	libscstat = NULL;

	errno = ENOSYS;
	return (-1);
}


/*
 * cfg_issuncluster()
 *
 * Description:
 *  Return the SunCluster nodeid of this node.
 *
 * Returns:
 *  >0   - running in a SunCluster (value is nodeid of this node)
 *   0   - not running in a cluster
 *  -1   - failure; errno is set
 */

int
cfg_issuncluster()
{
	if (cfg_cluster_init() >= 0)
		return ((int)cl_nodeid);
	else
		return (-1);
}
int
cfg_iscluster()
{
	return (cfg_issuncluster());
}

/*
 * cfg_l_dgname_islocal()
 * Check if disk group is local on a non-SunCluster.
 *
 * Returns as cfg_dgname_islocal().
 */
#ifndef lint
static int
cfg_l_dgname_islocal(char *dgname, char **othernode)
{
	const char *metaset = "/usr/sbin/metaset -s %s -o > /dev/null 2>&1";
	char command[1024];
	int rc;

	if (snprintf(command, sizeof (command), metaset, dgname) >=
	    sizeof (command)) {
		errno = ENOMEM;
		return (-1);
	}

	rc = system(command);
	if (rc < 0) {
		return (-1);
	}

	if (WEXITSTATUS(rc) != 0) {
		if (othernode) {
			/* metaset doesn't tell us */
			*othernode = "unknown";
		}

		return (0);
	}

	return (1);
}
#endif

/*
 * cfg_dgname_islocal(char *dgname, char **othernode)
 * -- determine if the named disk service is mastered on this node
 *
 * If the disk service is mastered on another node, that nodename
 * will be returned in othernode (if not NULL).  It is up to the
 * calling program to call free() on this value at a later time to
 * free the memory allocated.
 *
 * Returns:
 *   1   - disk service is mastered on this node
 *   0   - disk service is not mastered on this node (*othernode set)
 *   -1  - error (errno will be set)
 */

int
cfg_dgname_islocal(char *dgname, char **othernode)
{
	hash_data_t *data;

	if (dgname == NULL || *dgname == '\0' || othernode == NULL) {
		errno = EINVAL;
		return (-1);
	}

	/* Handle non-cluster configurations */
	if (cfg_cluster_init() < 0) {
		return (-1);
	} else 	if (cl_nodeid == 0) {
		/* it has to be local */
		return (1);
	}

	/*
	 * lookup the current diskgroup name
	 */
	if (data = (hash_data_t *)nsc_lookup(schash, dgname)) {
		if (strcmp(data->scstat_node_name, cl_nodename)) {
			if (othernode)
			    *othernode = strdup(data->scstat_node_name);
			return (0);
		} else {
			return (1);
		}
	} else {
		errno = ENODEV;
		return (-1);
	}
}

/*
 * cfg_l_dgname()
 * parse the disk group name from the a device pathname on a non-SunCluster.
 *
 * Returns as cfg_dgname().
 */

char *
cfg_l_dgname(const char *pathname, char *buffer, size_t buflen)
{
	const char *dev = "/dev/";
	const char *vx = "vx/";
	const char *md = "md/";
	const char *dsk = "dsk/";
	const char *start, *cp;
	int ll, len, chkdsk;

	bzero(buffer, buflen);
	chkdsk = 0;

	ll = strlen(dev);
	if (strncmp(pathname, dev, ll) != 0) {
		/* not a device pathname */
		errno = EINVAL;
		return ((char *)NULL);
	}

	start = pathname + ll;

	if (strncmp(start, md, (ll = strlen(md))) == 0) {
		/*
		 * SVM --
		 * /dev/md/dgname/{r}dsk/partition
		 */

		start += ll;

		if (strncmp(start, dsk, strlen(dsk)) == 0 ||
		    (*start == 'r' &&
		    strncmp((start + 1), dsk, strlen(dsk)) == 0)) {
			/* no dgname */
			return (buffer);
		}

		chkdsk = 1;	/* check for trailing {r}dsk */
	} else if (strncmp(start, vx, (ll = strlen(vx))) == 0) {
		/*
		 * Veritas --
		 * /dev/vx/{r}dsk/dgname/partition
		 */

		start += ll;

		ll = strlen(dsk);

		if (*start == 'r' && strncmp((start + 1), dsk, ll) == 0)
			start += ll + 1;
		else if (strncmp(start, dsk, ll) == 0)
			start += ll;
		else {
			/* no dgname */
			return (buffer);
		}
	} else {
		/* no dgname */
		return (buffer);
	}

	for (cp = start, len = 0; *cp != '\0' && *cp != '/'; cp++)
		len++;	/* count length of dgname */

	if (*cp == '\0') {
		/* no dgname */
		return (buffer);
	}

#ifdef DEBUG
	if (*cp != '/') {
		fprintf(stderr,
		    "cfg_dgname: parse error: *cp = '%c', expected '/'\n", *cp);
		errno = EPROTO;
		return ((char *)NULL);
	}
#endif

	if (chkdsk) {
		cp++;	/* skip the NULL */

		ll = strlen(dsk);

		if ((*cp != 'r' || strncmp((cp + 1), dsk, ll) != 0) &&
		    strncmp(cp, dsk, ll) != 0) {
			/* no dgname */
			return (buffer);
		}
	}

	if (len >= buflen) {
		errno = E2BIG;
		return ((char *)NULL);
	}

	(void) strncpy(buffer, start, len);
	return (buffer);
}


/*
 * cfg_dgname()
 * determine which cluster resource group the pathname belongs to, if any
 *
 * Returns:
 *	NULL			- error (errno is set)
 *	ptr to NULL-string	- no dgname
 *	pointer to string	- dgname
 */

char *
cfg_dgname(const char *pathname, char *buffer, size_t buflen)
{
	scconf_errno_t conferr;
	char *dsname = NULL;
	struct stat stb;
#ifdef DEBUG
	char errbuf[SCCONF_MAXSTRINGLEN];
#endif

	bzero(buffer, buflen);

	if (pathname == NULL || *pathname == '\0') {
		errno = EINVAL;
		return ((char *)NULL);
	}

	/* Handle non-cluster configurations */
	if (cfg_cluster_init() < 0) {
		errno = EINVAL;
		return ((char *)NULL);
	} else 	if (cl_nodeid == 0) {
		/* must be local - return NULL-string dgname */
		return (buffer);
	}

	if (stat(pathname, &stb) < 0) {
		errno = EINVAL;
		return ((char *)NULL);
	}

	conferr = scconf_get_ds_by_devt(major(stb.st_rdev),
	    minor(stb.st_rdev), &dsname);

	if (conferr == SCCONF_ENOEXIST) {
		return (buffer);
	} else if (conferr != SCCONF_NOERR) {
#ifdef DEBUG
		scconf_strerr(errbuf, conferr);
		fprintf(stderr,
		    "scconf_get_ds_by_devt: %d: %s\n", conferr, errbuf);
#endif
		errno = EINVAL;
		return ((char *)NULL);
	}

	strncpy(buffer, dsname, buflen);
	free(dsname);

	return (buffer);
}


/*
 * init_sc_entry
 *
 * Add an entry into the sclist and the schash for future lookups.
 *
 * - IMPORTANT -
 *
 * This must -never- be called from any command that can be started
 * from /usr/cluster/lib/sc/run_reserve (and hence
 * /usr/cluster/sbin/reconfig) or the system will deadlock
 * during switchover.  This includes:
 *
 *       - svadm (no options, "print") -- called during sv switchover
 *       - all boot commands
 *
 * Return values:
 *  -1  An error occurred.
 *   0  Entry added
 *   1  Entry already exists.
 */
static int
init_sc_entry()
{
	scstat_ds_node_state_t *dsn;
	scstat_ds_name_t dsname;
	scstat_ds_t *dsstatus, *dsp;
	scstat_errno_t err;
#ifdef DEBUG
	char errbuf[SCCONF_MAXSTRINGLEN];
#endif

	hash_data_t *hdp;

	/*
	 * Allocate a hash table
	 */
	if ((schash = nsc_create_hash()) == NULL)
		return (-1);

	/*
	 * the API is broken here - the function is written to expect
	 * the first argument to be (scstat_ds_name_t), but the function
	 * declaration in scstat.h requires (scstat_ds_name_t *).
	 *
	 * We just cast it to get rid of the compiler warnings.
	 * If "dsname" is NULL, information for all device services is returned
	 */
	dsstatus = NULL;
	dsname = NULL;
	/* LINTED pointer alignment */
	err = scstat_get_ds_status((scstat_ds_name_t *)dsname, &dsstatus);
	if (err != SCSTAT_ENOERR) {
#ifdef DEBUG
		scstat_strerr(err, errbuf);
		fprintf(stderr, "scstat_get_ds_status(): %d: %s\n",
		    err, errbuf);
#endif
		errno = ENOSYS;
		return (-1);
	}

	if (dsstatus == NULL) {
		errno = ENODEV;
		return (-1);
	}

	/*
	 * Traverse scstat_ds list, saving away resource in out hash table
	 */
	for (dsp = dsstatus; dsp; dsp = dsp->scstat_ds_next) {

		/* Skip over NULL scstat_ds_name's */
		if ((dsp->scstat_ds_name == NULL) ||
		    (dsp->scstat_ds_name[0] == '\0'))
			continue;

		/* See element exits already, error if so */
		if (nsc_lookup(schash, dsp->scstat_ds_name)) {
			fprintf(stderr, "scstat_get_ds_status: duplicate %s",
				dsp->scstat_ds_name);
			errno = EEXIST;
			return (-1);
		}

		/* Traverse the node status list */
		for (dsn = dsp->scstat_node_state_list; dsn;
					dsn = dsn->scstat_node_next) {
			/*
			 * Only keep trace of primary nodes
			 */
			if (dsn->scstat_node_state != SCSTAT_PRIMARY)
				continue;

			/* Create an element to insert */
			hdp = (hash_data_t *)malloc(sizeof (hash_data_t));
			hdp->scstat_node_name = strdup(dsn->scstat_node_name);
			nsc_insert_node(schash, hdp, dsp->scstat_ds_name);
		}
	}

	/*
	 * Free up scstat resources
	 */
	scstat_free_ds_status(dsstatus);
	return (0);
}