~ubuntu-branches/ubuntu/precise/corosync/precise-proposed

« back to all changes in this revision

Viewing changes to exec/vsf_quorum.c

  • Committer: Bazaar Package Importer
  • Author(s): Ante Karamatic
  • Date: 2009-08-21 09:29:56 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090821092956-w9qxxxx3zeoh8dem
Tags: 1.0.0-4ubuntu2
* debian/control:
  - 'Ubuntu Developers' instead of 'Ubuntu Core Developers'
    as maintainer
  - Bump debhelper dependecy to 7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2008, 2009 Red Hat, Inc.
 
3
 *
 
4
 * All rights reserved.
 
5
 *
 
6
 * Author: Christine Caulfield (ccaulfie@redhat.com)
 
7
 *
 
8
 * This software licensed under BSD license, the text of which follows:
 
9
 *
 
10
 * Redistribution and use in source and binary forms, with or without
 
11
 * modification, are permitted provided that the following conditions are met:
 
12
 *
 
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.
 
21
 *
 
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.
 
33
 */
 
34
 
 
35
#include <config.h>
 
36
 
 
37
#include <pwd.h>
 
38
#include <grp.h>
 
39
#include <sys/types.h>
 
40
#include <sys/poll.h>
 
41
#include <sys/uio.h>
 
42
#include <sys/mman.h>
 
43
#include <sys/socket.h>
 
44
#include <sys/un.h>
 
45
#include <sys/time.h>
 
46
#include <sys/resource.h>
 
47
#include <netinet/in.h>
 
48
#include <arpa/inet.h>
 
49
#include <unistd.h>
 
50
#include <fcntl.h>
 
51
#include <stdlib.h>
 
52
#include <stdio.h>
 
53
#include <errno.h>
 
54
#include <sched.h>
 
55
#include <time.h>
 
56
 
 
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>
 
70
 
 
71
LOGSYS_DECLARE_SUBSYS ("QUORUM");
 
72
 
 
73
struct quorum_pd {
 
74
        unsigned char track_flags;
 
75
        int tracking_enabled;
 
76
        struct list_head list;
 
77
        void *conn;
 
78
};
 
79
 
 
80
struct internal_callback_pd {
 
81
        struct list_head list;
 
82
        quorum_callback_fn_t callback;
 
83
        void *context;
 
84
};
 
85
 
 
86
static void message_handler_req_lib_quorum_getquorate (void *conn,
 
87
                                                       const void *msg);
 
88
static void message_handler_req_lib_quorum_trackstart (void *conn,
 
89
                                                       const void *msg);
 
90
static void message_handler_req_lib_quorum_trackstop (void *conn,
 
91
                                                      const void *msg);
 
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);
 
97
 
 
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;
 
106
 
 
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)
 
111
{
 
112
        int i;
 
113
 
 
114
        primary_designated = quorum;
 
115
 
 
116
        if (primary_designated) {
 
117
                log_printf (LOGSYS_LEVEL_NOTICE, "This node is within the primary component and will provide service.\n");
 
118
        } else {
 
119
                log_printf (LOGSYS_LEVEL_NOTICE, "This node is within the non-primary component and will NOT provide any services.\n");
 
120
        }
 
121
 
 
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);
 
125
 
 
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]);
 
129
 
 
130
        /* Tell internal listeners */
 
131
        send_internal_notification();
 
132
 
 
133
        /* Tell IPC listeners */
 
134
        send_library_notification(NULL);
 
135
}
 
136
 
 
137
static struct corosync_lib_handler quorum_lib_service[] =
 
138
{
 
139
        { /* 0 */
 
140
                .lib_handler_fn                         = message_handler_req_lib_quorum_getquorate,
 
141
                .flow_control                           = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
 
142
        },
 
143
        { /* 1 */
 
144
                .lib_handler_fn                         = message_handler_req_lib_quorum_trackstart,
 
145
                .flow_control                           = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
 
146
        },
 
147
        { /* 2 */
 
148
                .lib_handler_fn                         = message_handler_req_lib_quorum_trackstop,
 
149
                .flow_control                           = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
 
150
        }
 
151
};
 
152
 
 
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
 
165
};
 
166
 
 
167
static struct lcr_iface corosync_quorum_ver0[1] = {
 
168
        {
 
169
                .name                   = "corosync_quorum",
 
170
                .version                = 0,
 
171
                .versions_replace       = 0,
 
172
                .versions_replace_count = 0,
 
173
                .dependencies           = 0,
 
174
                .dependency_count       = 0,
 
175
                .constructor            = NULL,
 
176
                .destructor             = NULL,
 
177
                .interfaces             = NULL,
 
178
        },
 
179
};
 
180
 
 
181
static struct corosync_service_engine *quorum_get_service_handler_ver0 (void)
 
182
{
 
183
        return (&quorum_service_handler);
 
184
}
 
185
 
 
186
static struct lcr_comp quorum_comp_ver0 = {
 
187
        .iface_count                    = 1,
 
188
        .ifaces                         = corosync_quorum_ver0
 
189
};
 
190
 
 
191
static struct corosync_service_engine_iface_ver0 quorum_service_handler_iface = {
 
192
        .corosync_get_service_engine_ver0 = quorum_get_service_handler_ver0
 
193
};
 
194
 
 
195
#ifdef COROSYNC_SOLARIS
 
196
void corosync_lcr_component_register (void);
 
197
 
 
198
void corosync_lcr_component_register (void) {
 
199
#else
 
200
__attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
 
201
#endif
 
202
        lcr_component_register (&quorum_comp_ver0);
 
203
        lcr_interfaces_set (&corosync_quorum_ver0[0], &quorum_service_handler_iface);
 
204
}
 
205
 
 
206
/* -------------------------------------------------- */
 
207
 
 
208
 
 
209
/*
 
210
 * Internal API functions for corosync
 
211
 */
 
212
 
 
213
static int quorum_quorate(void)
 
214
{
 
215
        return primary_designated;
 
216
}
 
217
 
 
218
 
 
219
static int quorum_register_callback(quorum_callback_fn_t function, void *context)
 
220
{
 
221
        struct internal_callback_pd *pd = malloc(sizeof(struct internal_callback_pd));
 
222
        if (!pd)
 
223
                return -1;
 
224
 
 
225
        pd->context  = context;
 
226
        pd->callback = function;
 
227
        list_add (&pd->list, &internal_trackers_list);
 
228
 
 
229
        return 0;
 
230
}
 
231
 
 
232
static int quorum_unregister_callback(quorum_callback_fn_t function, void *context)
 
233
{
 
234
        struct internal_callback_pd *pd;
 
235
        struct list_head *tmp;
 
236
 
 
237
        for (tmp = internal_trackers_list.next; tmp != &internal_trackers_list; tmp = tmp->next) {
 
238
 
 
239
                pd = list_entry(tmp, struct internal_callback_pd, list);
 
240
                if (pd->callback == function && pd->context == context) {
 
241
                        list_del(&pd->list);
 
242
                        return 0;
 
243
                }
 
244
        }
 
245
        return -1;
 
246
}
 
247
 
 
248
static struct quorum_callin_functions callins = {
 
249
        .quorate = quorum_quorate,
 
250
        .register_callback = quorum_register_callback,
 
251
        .unregister_callback = quorum_unregister_callback
 
252
};
 
253
 
 
254
/* --------------------------------------------------------------------- */
 
255
 
 
256
static int quorum_exec_init_fn (struct corosync_api_v1 *api)
 
257
{
 
258
        hdb_handle_t find_handle;
 
259
        hdb_handle_t quorum_handle = 0;
 
260
        hdb_handle_t q_handle;
 
261
        char *quorum_module;
 
262
        int res;
 
263
        void *quorum_iface_p;
 
264
 
 
265
#ifdef COROSYNC_SOLARIS
 
266
        logsys_subsys_init();
 
267
#endif
 
268
        corosync_api = api;
 
269
        list_init (&lib_trackers_list);
 
270
        list_init (&internal_trackers_list);
 
271
 
 
272
        /*
 
273
         * Tell corosync we have a quorum engine.
 
274
         */
 
275
        api->quorum_initialize(&callins);
 
276
 
 
277
        /*
 
278
         * Look for a quorum provider
 
279
         */
 
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);
 
283
 
 
284
        if (quorum_handle) {
 
285
                if ( !(res = api->object_key_get(quorum_handle,
 
286
                                                 "provider",
 
287
                                                 strlen("provider"),
 
288
                                                 (void *)&quorum_module,
 
289
                                                 NULL))) {
 
290
 
 
291
                        res = lcr_ifact_reference (
 
292
                                &q_handle,
 
293
                                quorum_module,
 
294
                                0,
 
295
                                &quorum_iface_p,
 
296
                                0);
 
297
 
 
298
                        if (res == -1) {
 
299
                                log_printf (LOGSYS_LEVEL_NOTICE,
 
300
                                            "Couldn't load quorum provider %s\n",
 
301
                                            quorum_module);
 
302
                                return (-1);
 
303
                        }
 
304
 
 
305
                        log_printf (LOGSYS_LEVEL_NOTICE,
 
306
                                    "Using quorum provider %s\n", quorum_module);
 
307
 
 
308
                        quorum_iface = (struct quorum_services_api_ver1 *)quorum_iface_p;
 
309
                        quorum_iface->init (api, quorum_api_set_quorum);
 
310
                }
 
311
        }
 
312
        if (!quorum_iface) {
 
313
                /*
 
314
                 * With no quorum provider, we are always quorate
 
315
                 */
 
316
                primary_designated = 1;
 
317
        }
 
318
 
 
319
        return (0);
 
320
}
 
321
 
 
322
static int quorum_lib_init_fn (void *conn)
 
323
{
 
324
        struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
 
325
 
 
326
        log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p\n", conn);
 
327
 
 
328
        list_init (&pd->list);
 
329
        pd->conn = conn;
 
330
 
 
331
        return (0);
 
332
}
 
333
 
 
334
static int quorum_lib_exit_fn (void *conn)
 
335
{
 
336
        struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
 
337
 
 
338
        log_printf(LOGSYS_LEVEL_DEBUG, "lib_exit_fn: conn=%p\n", conn);
 
339
 
 
340
        if (quorum_pd->tracking_enabled) {
 
341
                list_del (&quorum_pd->list);
 
342
                list_init (&quorum_pd->list);
 
343
        }
 
344
        return (0);
 
345
}
 
346
 
 
347
 
 
348
static void send_internal_notification(void)
 
349
{
 
350
        struct list_head *tmp;
 
351
        struct internal_callback_pd *pd;
 
352
 
 
353
        for (tmp = internal_trackers_list.next; tmp != &internal_trackers_list; tmp = tmp->next) {
 
354
 
 
355
                pd = list_entry(tmp, struct internal_callback_pd, list);
 
356
 
 
357
                pd->callback(primary_designated, pd->context);
 
358
        }
 
359
}
 
360
 
 
361
static void send_library_notification(void *conn)
 
362
{
 
363
        int size = sizeof(struct res_lib_quorum_notification) + sizeof(unsigned int)*quorum_view_list_entries;
 
364
        char buf[size];
 
365
        struct res_lib_quorum_notification *res_lib_quorum_notification = (struct res_lib_quorum_notification *)buf;
 
366
        struct list_head *tmp;
 
367
        int i;
 
368
 
 
369
        log_printf(LOGSYS_LEVEL_DEBUG, "sending quorum notification to %p, length = %d\n", conn, size);
 
370
 
 
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];
 
376
        }
 
377
 
 
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;
 
381
 
 
382
        /* Send it to all interested parties */
 
383
        if (conn) {
 
384
                corosync_api->ipc_response_send(conn, res_lib_quorum_notification, size);
 
385
        }
 
386
        else {
 
387
                struct quorum_pd *qpd;
 
388
 
 
389
                for (tmp = lib_trackers_list.next; tmp != &lib_trackers_list; tmp = tmp->next) {
 
390
 
 
391
                        qpd = list_entry(tmp, struct quorum_pd, list);
 
392
 
 
393
                        corosync_api->ipc_dispatch_send(qpd->conn,
 
394
                             res_lib_quorum_notification, size);
 
395
                }
 
396
        }
 
397
        return;
 
398
}
 
399
 
 
400
static void message_handler_req_lib_quorum_getquorate (void *conn,
 
401
                                                       const void *msg)
 
402
{
 
403
        struct res_lib_quorum_getquorate res_lib_quorum_getquorate;
 
404
 
 
405
        log_printf(LOGSYS_LEVEL_DEBUG, "got quorate request on %p\n", conn);
 
406
 
 
407
        /* send status */
 
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));
 
413
}
 
414
 
 
415
 
 
416
static void message_handler_req_lib_quorum_trackstart (void *conn,
 
417
                                                       const void *msg)
 
418
{
 
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);
 
422
 
 
423
        log_printf(LOGSYS_LEVEL_DEBUG, "got trackstart request on %p\n", conn);
 
424
 
 
425
        /*
 
426
         * If an immediate listing of the current cluster membership
 
427
         * is requested, generate membership list
 
428
         */
 
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);
 
433
        }
 
434
 
 
435
        /*
 
436
         * Record requests for tracking
 
437
         */
 
438
        if (req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES ||
 
439
            req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
 
440
 
 
441
                quorum_pd->track_flags = req_lib_quorum_trackstart->track_flags;
 
442
                quorum_pd->tracking_enabled = 1;
 
443
 
 
444
                list_add (&quorum_pd->list, &lib_trackers_list);
 
445
        }
 
446
 
 
447
        /* send status */
 
448
        res.size = sizeof(res);
 
449
        res.id = MESSAGE_RES_QUORUM_TRACKSTART;
 
450
        res.error = CS_OK;
 
451
        corosync_api->ipc_response_send(conn, &res, sizeof(coroipc_response_header_t));
 
452
}
 
453
 
 
454
static void message_handler_req_lib_quorum_trackstop (void *conn, const void *msg)
 
455
{
 
456
        coroipc_response_header_t res;
 
457
        struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
 
458
 
 
459
        log_printf(LOGSYS_LEVEL_DEBUG, "got trackstop request on %p\n", conn);
 
460
 
 
461
        if (quorum_pd->tracking_enabled) {
 
462
                res.error = CS_OK;
 
463
                quorum_pd->tracking_enabled = 0;
 
464
                list_del (&quorum_pd->list);
 
465
                list_init (&quorum_pd->list);
 
466
        } else {
 
467
                res.error = CS_ERR_NOT_EXIST;
 
468
        }
 
469
 
 
470
        /* send status */
 
471
        res.size = sizeof(res);
 
472
        res.id = MESSAGE_RES_QUORUM_TRACKSTOP;
 
473
        res.error = CS_OK;
 
474
        corosync_api->ipc_response_send(conn, &res, sizeof(coroipc_response_header_t));
 
475
}