2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/events/events.h"
24 #include "cluster/cluster.h"
25 #include "system/filesys.h"
26 #include "cluster/cluster_private.h"
27 #include "lib/tdb/include/tdb.h"
28 #include "include/ctdb.h"
30 #include "lib/util/dlinklist.h"
32 /* a linked list of messaging handlers, allowing incoming messages
33 to be directed to the right messaging context */
34 struct cluster_messaging_list {
35 struct cluster_messaging_list *next, *prev;
36 struct cluster_state *state;
37 struct messaging_context *msg;
38 struct server_id server;
39 cluster_message_fn_t handler;
42 struct cluster_state {
43 struct ctdb_context *ctdb;
44 struct cluster_messaging_list *list;
51 return a server_id for a ctdb node
53 static struct server_id ctdb_id(struct cluster_ops *ops, uint32_t id)
55 struct cluster_state *state = (struct cluster_state *)ops->private;
56 struct ctdb_context *ctdb = state->ctdb;
57 struct server_id server_id;
58 server_id.node = ctdb_get_vnn(ctdb);
65 return a server_id as a string
67 static const char *ctdb_id_string(struct cluster_ops *ops,
68 TALLOC_CTX *mem_ctx, struct server_id id)
70 return talloc_asprintf(mem_ctx, "%u.%u", id.node, id.id);
74 this is an interim method for subsystems that have not yet been
75 converted to use the ctdb api. It opens a shared database in the
76 cluster temporary area, using TDB_CLEAR_IF_FIRST which relies on
77 correct operation of fcntl locks on the shared fileystem.
79 static struct tdb_wrap *ctdb_tdb_tmp_open(struct cluster_ops *ops,
80 TALLOC_CTX *mem_ctx, const char *dbname,
83 const char *dir = lp_parm_string(-1, "ctdb", "shared data");
87 DEBUG(0,("ERROR: You must set 'ctdb:shared data' to a cluster shared path\n"));
90 path = talloc_asprintf(mem_ctx, "%s/%s", dir, dbname);
91 w = tdb_wrap_open(mem_ctx, path, 0,
92 flags | TDB_CLEAR_IF_FIRST,
93 O_RDWR|O_CREAT, 0600);
99 get at the ctdb handle
101 static void *ctdb_backend_handle(struct cluster_ops *ops)
103 struct cluster_state *state = (struct cluster_state *)ops->private;
104 return (void *)state->ctdb;
107 struct ctdb_handler_state {
108 struct cluster_state *state;
109 cluster_message_fn_t handler;
110 struct messaging_context *msg;
114 dispatch incoming ctdb messages
116 static void ctdb_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
117 TDB_DATA data, void *private)
119 struct ctdb_handler_state *s = talloc_get_type(private,
120 struct ctdb_handler_state);
122 blob.data = data.dptr;
123 blob.length = data.dsize;
124 s->handler(s->msg, blob);
127 static int ctdb_handler_destructor(struct ctdb_handler_state *s)
129 /* XXX - tell ctdb to de-register the message handler */
134 setup a handler for ctdb messages
136 static NTSTATUS ctdb_message_init(struct cluster_ops *ops,
137 struct messaging_context *msg,
138 struct server_id server,
139 cluster_message_fn_t handler)
141 struct cluster_state *state = (struct cluster_state *)ops->private;
142 struct ctdb_handler_state *h;
145 h = talloc(msg, struct ctdb_handler_state);
146 NT_STATUS_HAVE_NO_MEMORY(h);
149 h->handler = handler;
152 talloc_set_destructor(h, ctdb_handler_destructor);
154 /* setup a message handler */
155 ret = ctdb_set_message_handler(state->ctdb, server.id,
156 ctdb_message_handler, h);
158 DEBUG(0,("ctdb_set_message_handler failed - %s\n",
159 ctdb_errstr(state->ctdb)));
167 send a ctdb message to another node
169 static NTSTATUS ctdb_message_send(struct cluster_ops *ops,
170 struct server_id server, DATA_BLOB *data)
172 struct cluster_state *state = (struct cluster_state *)ops->private;
173 struct ctdb_context *ctdb = state->ctdb;
177 tdata.dptr = data->data;
178 tdata.dsize = data->length;
180 ret = ctdb_send_message(ctdb, server.node, server.id, tdata);
182 return NT_STATUS_INTERNAL_DB_CORRUPTION;
187 static struct cluster_ops cluster_ctdb_ops = {
188 .cluster_id = ctdb_id,
189 .cluster_id_string = ctdb_id_string,
190 .cluster_tdb_tmp_open = ctdb_tdb_tmp_open,
191 .backend_handle = ctdb_backend_handle,
192 .message_init = ctdb_message_init,
193 .message_send = ctdb_message_send,
197 /* initialise ctdb */
198 void cluster_ctdb_init(struct event_context *ev, const char *model)
200 struct cluster_state *state;
203 if (!lp_parm_bool(-1, "ctdb", "enable", False)) {
207 state = talloc(ev, struct cluster_state);
208 if (state == NULL) goto failed;
210 state->ctdb = ctdb_init(ev);
211 if (state->ctdb == NULL) goto failed;
213 ret = ctdb_socket_connect(state->ctdb);
215 DEBUG(0,(__location__ " Failed to connect to ctdb socket\n"));
220 state->vnn = ctdb_ctrl_getvnn(state->ctdb, timeval_zero(), CTDB_CURRENT_NODE);
221 if (state->vnn == (uint32_t)-1) {
222 DEBUG(0,(__location__ " Failed to get ctdb vnn\n"));
228 cluster_ctdb_ops.private = state;
230 cluster_set_ops(&cluster_ctdb_ops);
233 /* nasty hack for now ... */
235 void brl_ctdb_init_ops(void);
243 DEBUG(0,("cluster_ctdb_init failed\n"));