2
* Copyright (c) 2008, 2009 Red Hat, Inc.
6
* Author: Christine Caulfield (ccaulfie@redhat.com)
8
* This software licensed under BSD license, the text of which follows:
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions are met:
13
* - Redistributions of source code must retain the above copyright notice,
14
* this list of conditions and the following disclaimer.
15
* - Redistributions in binary form must reproduce the above copyright notice,
16
* this list of conditions and the following disclaimer in the documentation
17
* and/or other materials provided with the distribution.
18
* - Neither the name of Red Hat Inc. nor the names of its
19
* contributors may be used to endorse or promote products derived from this
20
* software without specific prior written permission.
22
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32
* THE POSSIBILITY OF SUCH DAMAGE.
39
#include <sys/types.h>
43
#include <sys/socket.h>
46
#include <sys/resource.h>
47
#include <netinet/in.h>
48
#include <arpa/inet.h>
57
#include <corosync/corotypes.h>
58
#include <corosync/coroipc_types.h>
59
#include <corosync/corodefs.h>
60
#include <corosync/swab.h>
61
#include <corosync/list.h>
62
#include <corosync/mar_gen.h>
63
#include <corosync/ipc_quorum.h>
64
#include <corosync/lcr/lcr_comp.h>
65
#include <corosync/lcr/lcr_ifact.h>
66
#include <corosync/mar_gen.h>
67
#include <corosync/engine/coroapi.h>
68
#include <corosync/engine/logsys.h>
69
#include <corosync/engine/quorum.h>
71
LOGSYS_DECLARE_SUBSYS ("QUORUM");
74
unsigned char track_flags;
76
struct list_head list;
80
struct internal_callback_pd {
81
struct list_head list;
82
quorum_callback_fn_t callback;
86
static void message_handler_req_lib_quorum_getquorate (void *conn,
88
static void message_handler_req_lib_quorum_trackstart (void *conn,
90
static void message_handler_req_lib_quorum_trackstop (void *conn,
92
static void send_library_notification(void *conn);
93
static void send_internal_notification(void);
94
static int quorum_exec_init_fn (struct corosync_api_v1 *api);
95
static int quorum_lib_init_fn (void *conn);
96
static int quorum_lib_exit_fn (void *conn);
98
static int primary_designated = 0;
99
static struct corosync_api_v1 *corosync_api;
100
static struct list_head lib_trackers_list;
101
static struct list_head internal_trackers_list;
102
static struct memb_ring_id quorum_ring_id;
103
static size_t quorum_view_list_entries = 0;
104
static int quorum_view_list[PROCESSOR_COUNT_MAX];
105
struct quorum_services_api_ver1 *quorum_iface = NULL;
107
/* Internal quorum API function */
108
static void quorum_api_set_quorum(const unsigned int *view_list,
109
size_t view_list_entries,
110
int quorum, struct memb_ring_id *ring_id)
114
primary_designated = quorum;
116
if (primary_designated) {
117
log_printf (LOGSYS_LEVEL_NOTICE, "This node is within the primary component and will provide service.\n");
119
log_printf (LOGSYS_LEVEL_NOTICE, "This node is within the non-primary component and will NOT provide any services.\n");
122
quorum_view_list_entries = view_list_entries;
123
memcpy(&quorum_ring_id, ring_id, sizeof (quorum_ring_id));
124
memcpy(quorum_view_list, view_list, sizeof(unsigned int)*view_list_entries);
126
log_printf (LOGSYS_LEVEL_NOTICE, "Members[%d]: ", (int)view_list_entries);
127
for (i=0; i<view_list_entries; i++)
128
log_printf (LOGSYS_LEVEL_NOTICE, " %d ", view_list[i]);
130
/* Tell internal listeners */
131
send_internal_notification();
133
/* Tell IPC listeners */
134
send_library_notification(NULL);
137
static struct corosync_lib_handler quorum_lib_service[] =
140
.lib_handler_fn = message_handler_req_lib_quorum_getquorate,
141
.flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
144
.lib_handler_fn = message_handler_req_lib_quorum_trackstart,
145
.flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
148
.lib_handler_fn = message_handler_req_lib_quorum_trackstop,
149
.flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
153
static struct corosync_service_engine quorum_service_handler = {
154
.name = "corosync cluster quorum service v0.1",
155
.id = QUORUM_SERVICE,
156
.private_data_size = sizeof (struct quorum_pd),
157
.flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
158
.allow_inquorate = CS_LIB_ALLOW_INQUORATE,
159
.lib_init_fn = quorum_lib_init_fn,
160
.lib_exit_fn = quorum_lib_exit_fn,
161
.lib_engine = quorum_lib_service,
162
.exec_init_fn = quorum_exec_init_fn,
163
.lib_engine_count = sizeof (quorum_lib_service) / sizeof (struct corosync_lib_handler),
164
.sync_mode = CS_SYNC_V1
167
static struct lcr_iface corosync_quorum_ver0[1] = {
169
.name = "corosync_quorum",
171
.versions_replace = 0,
172
.versions_replace_count = 0,
174
.dependency_count = 0,
181
static struct corosync_service_engine *quorum_get_service_handler_ver0 (void)
183
return (&quorum_service_handler);
186
static struct lcr_comp quorum_comp_ver0 = {
188
.ifaces = corosync_quorum_ver0
191
static struct corosync_service_engine_iface_ver0 quorum_service_handler_iface = {
192
.corosync_get_service_engine_ver0 = quorum_get_service_handler_ver0
195
#ifdef COROSYNC_SOLARIS
196
void corosync_lcr_component_register (void);
198
void corosync_lcr_component_register (void) {
200
__attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
202
lcr_component_register (&quorum_comp_ver0);
203
lcr_interfaces_set (&corosync_quorum_ver0[0], &quorum_service_handler_iface);
206
/* -------------------------------------------------- */
210
* Internal API functions for corosync
213
static int quorum_quorate(void)
215
return primary_designated;
219
static int quorum_register_callback(quorum_callback_fn_t function, void *context)
221
struct internal_callback_pd *pd = malloc(sizeof(struct internal_callback_pd));
225
pd->context = context;
226
pd->callback = function;
227
list_add (&pd->list, &internal_trackers_list);
232
static int quorum_unregister_callback(quorum_callback_fn_t function, void *context)
234
struct internal_callback_pd *pd;
235
struct list_head *tmp;
237
for (tmp = internal_trackers_list.next; tmp != &internal_trackers_list; tmp = tmp->next) {
239
pd = list_entry(tmp, struct internal_callback_pd, list);
240
if (pd->callback == function && pd->context == context) {
248
static struct quorum_callin_functions callins = {
249
.quorate = quorum_quorate,
250
.register_callback = quorum_register_callback,
251
.unregister_callback = quorum_unregister_callback
254
/* --------------------------------------------------------------------- */
256
static int quorum_exec_init_fn (struct corosync_api_v1 *api)
258
hdb_handle_t find_handle;
259
hdb_handle_t quorum_handle = 0;
260
hdb_handle_t q_handle;
263
void *quorum_iface_p;
265
#ifdef COROSYNC_SOLARIS
266
logsys_subsys_init();
269
list_init (&lib_trackers_list);
270
list_init (&internal_trackers_list);
273
* Tell corosync we have a quorum engine.
275
api->quorum_initialize(&callins);
278
* Look for a quorum provider
280
api->object_find_create(OBJECT_PARENT_HANDLE, "quorum", strlen("quorum"), &find_handle);
281
api->object_find_next(find_handle, &quorum_handle);
282
api->object_find_destroy(find_handle);
285
if ( !(res = api->object_key_get(quorum_handle,
288
(void *)&quorum_module,
291
res = lcr_ifact_reference (
299
log_printf (LOGSYS_LEVEL_NOTICE,
300
"Couldn't load quorum provider %s\n",
305
log_printf (LOGSYS_LEVEL_NOTICE,
306
"Using quorum provider %s\n", quorum_module);
308
quorum_iface = (struct quorum_services_api_ver1 *)quorum_iface_p;
309
quorum_iface->init (api, quorum_api_set_quorum);
314
* With no quorum provider, we are always quorate
316
primary_designated = 1;
322
static int quorum_lib_init_fn (void *conn)
324
struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
326
log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p\n", conn);
328
list_init (&pd->list);
334
static int quorum_lib_exit_fn (void *conn)
336
struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
338
log_printf(LOGSYS_LEVEL_DEBUG, "lib_exit_fn: conn=%p\n", conn);
340
if (quorum_pd->tracking_enabled) {
341
list_del (&quorum_pd->list);
342
list_init (&quorum_pd->list);
348
static void send_internal_notification(void)
350
struct list_head *tmp;
351
struct internal_callback_pd *pd;
353
for (tmp = internal_trackers_list.next; tmp != &internal_trackers_list; tmp = tmp->next) {
355
pd = list_entry(tmp, struct internal_callback_pd, list);
357
pd->callback(primary_designated, pd->context);
361
static void send_library_notification(void *conn)
363
int size = sizeof(struct res_lib_quorum_notification) + sizeof(unsigned int)*quorum_view_list_entries;
365
struct res_lib_quorum_notification *res_lib_quorum_notification = (struct res_lib_quorum_notification *)buf;
366
struct list_head *tmp;
369
log_printf(LOGSYS_LEVEL_DEBUG, "sending quorum notification to %p, length = %d\n", conn, size);
371
res_lib_quorum_notification->quorate = primary_designated;
372
res_lib_quorum_notification->ring_seq = quorum_ring_id.seq;
373
res_lib_quorum_notification->view_list_entries = quorum_view_list_entries;
374
for (i=0; i<quorum_view_list_entries; i++) {
375
res_lib_quorum_notification->view_list[i] = quorum_view_list[i];
378
res_lib_quorum_notification->header.id = MESSAGE_RES_QUORUM_NOTIFICATION;
379
res_lib_quorum_notification->header.size = size;
380
res_lib_quorum_notification->header.error = CS_OK;
382
/* Send it to all interested parties */
384
corosync_api->ipc_response_send(conn, res_lib_quorum_notification, size);
387
struct quorum_pd *qpd;
389
for (tmp = lib_trackers_list.next; tmp != &lib_trackers_list; tmp = tmp->next) {
391
qpd = list_entry(tmp, struct quorum_pd, list);
393
corosync_api->ipc_dispatch_send(qpd->conn,
394
res_lib_quorum_notification, size);
400
static void message_handler_req_lib_quorum_getquorate (void *conn,
403
struct res_lib_quorum_getquorate res_lib_quorum_getquorate;
405
log_printf(LOGSYS_LEVEL_DEBUG, "got quorate request on %p\n", conn);
408
res_lib_quorum_getquorate.quorate = primary_designated;
409
res_lib_quorum_getquorate.header.size = sizeof(res_lib_quorum_getquorate);
410
res_lib_quorum_getquorate.header.id = MESSAGE_RES_QUORUM_GETQUORATE;
411
res_lib_quorum_getquorate.header.error = CS_OK;
412
corosync_api->ipc_response_send(conn, &res_lib_quorum_getquorate, sizeof(res_lib_quorum_getquorate));
416
static void message_handler_req_lib_quorum_trackstart (void *conn,
419
const struct req_lib_quorum_trackstart *req_lib_quorum_trackstart = msg;
420
coroipc_response_header_t res;
421
struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
423
log_printf(LOGSYS_LEVEL_DEBUG, "got trackstart request on %p\n", conn);
426
* If an immediate listing of the current cluster membership
427
* is requested, generate membership list
429
if (req_lib_quorum_trackstart->track_flags & CS_TRACK_CURRENT ||
430
req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES) {
431
log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p\n", conn);
432
send_library_notification(conn);
436
* Record requests for tracking
438
if (req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES ||
439
req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
441
quorum_pd->track_flags = req_lib_quorum_trackstart->track_flags;
442
quorum_pd->tracking_enabled = 1;
444
list_add (&quorum_pd->list, &lib_trackers_list);
448
res.size = sizeof(res);
449
res.id = MESSAGE_RES_QUORUM_TRACKSTART;
451
corosync_api->ipc_response_send(conn, &res, sizeof(coroipc_response_header_t));
454
static void message_handler_req_lib_quorum_trackstop (void *conn, const void *msg)
456
coroipc_response_header_t res;
457
struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
459
log_printf(LOGSYS_LEVEL_DEBUG, "got trackstop request on %p\n", conn);
461
if (quorum_pd->tracking_enabled) {
463
quorum_pd->tracking_enabled = 0;
464
list_del (&quorum_pd->list);
465
list_init (&quorum_pd->list);
467
res.error = CS_ERR_NOT_EXIST;
471
res.size = sizeof(res);
472
res.id = MESSAGE_RES_QUORUM_TRACKSTOP;
474
corosync_api->ipc_response_send(conn, &res, sizeof(coroipc_response_header_t));