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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/events/events.h"
25 #include "cluster/cluster.h"
26 #include "system/filesys.h"
27 #include "cluster/cluster_private.h"
28 #include "lib/tdb/include/tdb.h"
29 #include "include/ctdb.h"
31 #include "lib/util/dlinklist.h"
33 /* a linked list of messaging handlers, allowing incoming messages
34 to be directed to the right messaging context */
35 struct cluster_messaging_list {
36 struct cluster_messaging_list *next, *prev;
37 struct cluster_state *state;
38 struct messaging_context *msg;
39 struct server_id server;
40 cluster_message_fn_t handler;
43 struct cluster_state {
44 struct ctdb_context *ctdb;
45 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 = 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 = ops->private;
104 return (void *)state->ctdb;
108 dispatch incoming ctdb messages
110 static void ctdb_message_handler(struct ctdb_context *ctdb, uint32_t srvid,
111 TDB_DATA data, void *private)
113 struct cluster_state *state = talloc_get_type(private, struct cluster_state);
114 struct cluster_messaging_list *m;
115 for (m=state->list;m;m=m->next) {
116 if (srvid == m->server.id) {
118 bdata.data = data.dptr;
119 bdata.length = data.dsize;
120 m->handler(m->msg, bdata);
126 destroy a element of messaging list (when messaging context goes away)
128 static int cluster_messaging_destructor(struct cluster_messaging_list *m)
130 DLIST_REMOVE(m->state->list, m);
135 setup a handler for ctdb messages
137 static NTSTATUS ctdb_message_init(struct cluster_ops *ops,
138 struct messaging_context *msg,
139 struct server_id server,
140 cluster_message_fn_t handler)
142 struct cluster_state *state = ops->private;
143 struct cluster_messaging_list *m;
146 /* setup messaging handler */
147 ret = ctdb_set_message_handler(state->ctdb, ctdb_message_handler,
150 DEBUG(0,("ctdb_set_message_handler failed - %s\n",
151 ctdb_errstr(state->ctdb)));
155 m = talloc(msg, struct cluster_messaging_list);
156 NT_STATUS_HAVE_NO_MEMORY(m);
161 m->handler = handler;
162 DLIST_ADD(state->list, m);
164 talloc_set_destructor(m, cluster_messaging_destructor);
170 send a ctdb message to another node
172 static NTSTATUS ctdb_message_send(struct cluster_ops *ops,
173 struct server_id server, DATA_BLOB *data)
175 struct cluster_state *state = ops->private;
176 struct ctdb_context *ctdb = state->ctdb;
180 tdata.dptr = data->data;
181 tdata.dsize = data->length;
183 ret = ctdb_send_message(ctdb, server.node, server.id, tdata);
185 return NT_STATUS_INTERNAL_DB_CORRUPTION;
190 static struct cluster_ops cluster_ctdb_ops = {
191 .cluster_id = ctdb_id,
192 .cluster_id_string = ctdb_id_string,
193 .cluster_tdb_tmp_open = ctdb_tdb_tmp_open,
194 .backend_handle = ctdb_backend_handle,
195 .message_init = ctdb_message_init,
196 .message_send = ctdb_message_send,
200 /* initialise ctdb */
201 void cluster_ctdb_init(struct event_context *ev)
205 const char *transport;
206 struct cluster_state *state;
208 const char *db_list[] = { "brlock", "opendb" };
210 nlist = lp_parm_string(-1, "ctdb", "nlist");
211 if (nlist == NULL) return;
213 address = lp_parm_string(-1, "ctdb", "address");
214 if (address == NULL) return;
216 transport = lp_parm_string(-1, "ctdb", "transport");
217 if (transport == NULL) {
221 state = talloc(ev, struct cluster_state);
222 if (state == NULL) goto failed;
224 state->ctdb = ctdb_init(ev);
225 if (state->ctdb == NULL) goto failed;
229 cluster_ctdb_ops.private = state;
231 ret = ctdb_set_transport(state->ctdb, transport);
233 DEBUG(0,("ctdb_set_transport failed - %s\n",
234 ctdb_errstr(state->ctdb)));
238 if (lp_parm_bool(-1, "ctdb", "selfconnect", False)) {
239 DEBUG(0,("Enabling ctdb selfconnect\n"));
240 ctdb_set_flags(state->ctdb, CTDB_FLAG_SELF_CONNECT);
243 lacount = lp_parm_int(-1, "ctdb", "maxlacount", -1);
245 ctdb_set_max_lacount(state->ctdb, lacount);
248 /* tell ctdb what address to listen on */
249 ret = ctdb_set_address(state->ctdb, address);
251 DEBUG(0,("ctdb_set_address failed - %s\n", ctdb_errstr(state->ctdb)));
255 /* tell ctdb what nodes are available */
256 ret = ctdb_set_nlist(state->ctdb, nlist);
258 DEBUG(0,("ctdb_set_nlist failed - %s\n", ctdb_errstr(state->ctdb)));
262 /* attach all the databases we will need */
263 for (i=0;i<ARRAY_SIZE(db_list);i++) {
264 struct ctdb_db_context *ctdb_db;
265 ctdb_db = ctdb_attach(state->ctdb, db_list[i], TDB_INTERNAL,
266 O_RDWR|O_CREAT|O_TRUNC, 0666);
267 if (ctdb_db == NULL) goto failed;
270 /* start the protocol running */
271 ret = ctdb_start(state->ctdb);
273 DEBUG(0,("ctdb_start failed - %s\n", ctdb_errstr(state->ctdb)));
277 /* wait until all nodes are connected (should not be needed
278 outide of test code) */
279 ctdb_connect_wait(state->ctdb);
281 cluster_set_ops(&cluster_ctdb_ops);
286 DEBUG(0,("cluster_ctdb_init failed\n"));