4 Copyright (C) Andrew Tridgell 2006
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "lib/events/events.h"
23 #include "lib/tdb/include/tdb.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "../include/ctdb_private.h"
28 #include "lib/util/dlinklist.h"
31 find an attached ctdb_db handle given a name
33 struct ctdb_db_context *ctdb_db_handle(struct ctdb_context *ctdb, const char *name)
35 struct ctdb_db_context *tmp_db;
36 for (tmp_db=ctdb->db_list;tmp_db;tmp_db=tmp_db->next) {
37 if (strcmp(name, tmp_db->db_name) == 0) {
46 this is the dummy null procedure that all databases support
48 static int ctdb_null_func(struct ctdb_call_info *call)
55 attach to a specific database
57 struct ctdb_db_context *ctdb_attach(struct ctdb_context *ctdb, const char *name, int tdb_flags,
58 int open_flags, mode_t mode)
60 struct ctdb_db_context *ctdb_db, *tmp_db;
64 ctdb_db = talloc_zero(ctdb, struct ctdb_db_context);
65 CTDB_NO_MEMORY_NULL(ctdb, ctdb_db);
68 ctdb_db->db_name = talloc_strdup(ctdb_db, name);
69 CTDB_NO_MEMORY_NULL(ctdb, ctdb_db->db_name);
71 data.dptr = discard_const(name);
72 data.dsize = strlen(name);
73 ctdb_db->db_id = ctdb_hash(&data);
75 for (tmp_db=ctdb->db_list;tmp_db;tmp_db=tmp_db->next) {
76 if (tmp_db->db_id == ctdb_db->db_id) {
77 ctdb_set_error(ctdb, "CTDB database hash collission '%s' : '%s'",
78 name, tmp_db->db_name);
84 if (mkdir(ctdb->db_directory, 0700) == -1 && errno != EEXIST) {
85 DEBUG(0,(__location__ " Unable to create ctdb directory '%s'\n",
91 /* add the node id to the database name, so when we run on loopback
92 we don't conflict in the local filesystem */
93 name = talloc_asprintf(ctdb_db, "%s/%s", ctdb->db_directory, name);
95 /* when we have a separate daemon this will need to be a real
96 file, not a TDB_INTERNAL, so the parent can access it to
98 ctdb_db->ltdb = tdb_wrap_open(ctdb, name, 0, TDB_CLEAR_IF_FIRST, open_flags, mode);
99 if (ctdb_db->ltdb == NULL) {
100 ctdb_set_error(ctdb, "Failed to open tdb %s\n", name);
101 talloc_free(ctdb_db);
107 all databases support the "null" function. we need this in
108 order to do forced migration of records
110 ret = ctdb_set_call(ctdb_db, ctdb_null_func, CTDB_NULL_FUNC);
112 talloc_free(ctdb_db);
116 DLIST_ADD(ctdb->db_list, ctdb_db);
122 return the lmaster given a key
124 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key)
126 return ctdb_hash(key) % ctdb->num_nodes;
131 construct an initial header for a record with no ltdb header yet
133 static void ltdb_initial_header(struct ctdb_db_context *ctdb_db,
135 struct ctdb_ltdb_header *header)
138 /* initial dmaster is the lmaster */
139 header->dmaster = ctdb_lmaster(ctdb_db->ctdb, &key);
140 header->laccessor = header->dmaster;
146 fetch a record from the ltdb, separating out the header information
147 and returning the body of the record. A valid (initial) header is
148 returned if the record is not present
150 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db,
151 TDB_DATA key, struct ctdb_ltdb_header *header,
152 TALLOC_CTX *mem_ctx, TDB_DATA *data)
155 struct ctdb_context *ctdb = ctdb_db->ctdb;
157 rec = tdb_fetch(ctdb_db->ltdb->tdb, key);
158 if (rec.dsize < sizeof(*header)) {
160 /* return an initial header */
161 if (rec.dptr) free(rec.dptr);
162 ltdb_initial_header(ctdb_db, key, header);
167 ctdb_ltdb_store(ctdb_db, key, header, d2);
171 *header = *(struct ctdb_ltdb_header *)rec.dptr;
174 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
175 data->dptr = talloc_memdup(mem_ctx,
176 sizeof(struct ctdb_ltdb_header)+rec.dptr,
182 CTDB_NO_MEMORY(ctdb, data->dptr);
190 fetch a record from the ltdb, separating out the header information
191 and returning the body of the record. A valid (initial) header is
192 returned if the record is not present
194 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key,
195 struct ctdb_ltdb_header *header, TDB_DATA data)
197 struct ctdb_context *ctdb = ctdb_db->ctdb;
201 rec.dsize = sizeof(*header) + data.dsize;
202 rec.dptr = talloc_size(ctdb, rec.dsize);
203 CTDB_NO_MEMORY(ctdb, rec.dptr);
205 memcpy(rec.dptr, header, sizeof(*header));
206 memcpy(rec.dptr + sizeof(*header), data.dptr, data.dsize);
208 ret = tdb_store(ctdb_db->ltdb->tdb, key, rec, TDB_REPLACE);
209 talloc_free(rec.dptr);
216 lock a record in the ltdb, given a key
218 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
220 return tdb_chainlock(ctdb_db->ltdb->tdb, key);
224 unlock a record in the ltdb, given a key
226 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
228 return tdb_chainunlock(ctdb_db->ltdb->tdb, key);
231 struct lock_fetch_state {
232 struct ctdb_context *ctdb;
233 void (*recv_pkt)(void *, uint8_t *, uint32_t);
235 struct ctdb_req_header *hdr;
239 called when we should retry the operation
241 static void lock_fetch_callback(void *p)
243 struct lock_fetch_state *state = talloc_get_type(p, struct lock_fetch_state);
244 state->recv_pkt(state->recv_context, (uint8_t *)state->hdr, state->hdr->length);
246 DEBUG(2,(__location__ " PACKET REQUEUED\n"));
251 do a non-blocking ltdb_lock, deferring this ctdb request until we
254 It does the following:
256 1) tries to get the chainlock. If it succeeds, then it returns 0
258 2) if it fails to get a chainlock immediately then it sets up a
259 non-blocking chainlock via ctdb_lockwait, and when it gets the
260 chainlock it re-submits this ctdb request to the main packet
263 This effectively queues all ctdb requests that cannot be
264 immediately satisfied until it can get the lock. This means that
265 the main ctdb daemon will not block waiting for a chainlock held by
268 There are 3 possible return values:
270 0: means that it got the lock immediately.
271 -1: means that it failed to get the lock, and won't retry
272 -2: means that it failed to get the lock immediately, but will retry
274 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db,
275 TDB_DATA key, struct ctdb_req_header *hdr,
276 void (*recv_pkt)(void *, uint8_t *, uint32_t ),
280 struct tdb_context *tdb = ctdb_db->ltdb->tdb;
281 struct lockwait_handle *h;
282 struct lock_fetch_state *state;
284 ret = tdb_chainlock_nonblock(tdb, key);
287 !(errno == EACCES || errno == EAGAIN || errno == EDEADLK)) {
288 /* a hard failure - don't try again */
292 /* when torturing, ensure we test the contended path */
293 if ((ctdb_db->ctdb->flags & CTDB_FLAG_TORTURE) &&
296 tdb_chainunlock(tdb, key);
299 /* first the non-contended path */
304 state = talloc(ctdb_db, struct lock_fetch_state);
305 state->ctdb = ctdb_db->ctdb;
307 state->recv_pkt = recv_pkt;
308 state->recv_context = recv_context;
310 /* now the contended path */
311 h = ctdb_lockwait(ctdb_db, key, lock_fetch_callback, state);
313 tdb_chainunlock(tdb, key);
317 /* we need to move the packet off the temporary context in ctdb_recv_pkt(),
318 so it won't be freed yet */
319 talloc_steal(state, hdr);
320 talloc_steal(state, h);
322 /* now tell the caller than we will retry asynchronously */
327 a varient of ctdb_ltdb_lock_requeue that also fetches the record
329 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db,
330 TDB_DATA key, struct ctdb_ltdb_header *header,
331 struct ctdb_req_header *hdr, TDB_DATA *data,
332 void (*recv_pkt)(void *, uint8_t *, uint32_t ),
337 ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr, recv_pkt, recv_context);
339 ret = ctdb_ltdb_fetch(ctdb_db, key, header, hdr, data);
341 ctdb_ltdb_unlock(ctdb_db, key);