2 Unix SMB/CIFS implementation.
3 Database interface wrapper around ctdbd
4 Copyright (C) Volker Lendecke 2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifdef CLUSTER_SUPPORT
25 #include "ctdb_private.h"
28 struct tdb_wrap *wtdb;
30 struct ctdbd_connection *conn;
34 struct db_ctdb_ctx *ctdb_ctx;
35 struct ctdb_ltdb_header header;
38 static struct ctdbd_connection *db_ctdbd_conn(struct db_ctdb_ctx *ctx);
40 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
42 struct db_ctdb_rec *crec = talloc_get_type_abort(
43 rec->private_data, struct db_ctdb_rec);
47 cdata.dsize = sizeof(crec->header) + data.dsize;
49 if (!(cdata.dptr = SMB_MALLOC_ARRAY(uint8, cdata.dsize))) {
50 return NT_STATUS_NO_MEMORY;
53 memcpy(cdata.dptr, &crec->header, sizeof(crec->header));
54 memcpy(cdata.dptr + sizeof(crec->header), data.dptr, data.dsize);
56 ret = tdb_store(crec->ctdb_ctx->wtdb->tdb, rec->key, cdata, TDB_REPLACE);
58 SAFE_FREE(cdata.dptr);
60 return (ret == 0) ? NT_STATUS_OK : NT_STATUS_INTERNAL_DB_CORRUPTION;
63 static NTSTATUS db_ctdb_delete(struct db_record *rec)
65 struct db_ctdb_rec *crec = talloc_get_type_abort(
66 rec->private_data, struct db_ctdb_rec);
71 * We have to store the header with empty data. TODO: Fix the
75 data.dptr = (uint8 *)&crec->header;
76 data.dsize = sizeof(crec->header);
78 ret = tdb_store(crec->ctdb_ctx->wtdb->tdb, rec->key, data, TDB_REPLACE);
80 return (ret == 0) ? NT_STATUS_OK : NT_STATUS_INTERNAL_DB_CORRUPTION;
83 static int db_ctdb_record_destr(struct db_record* data)
85 struct db_ctdb_rec *crec = talloc_get_type_abort(
86 data->private_data, struct db_ctdb_rec);
88 DEBUG(10, (DEBUGLEVEL > 10
89 ? "Unlocking db %u key %s\n"
90 : "Unlocking db %u key %.20s\n",
91 (int)crec->ctdb_ctx->db_id,
92 hex_encode(data, (unsigned char *)data->key.dptr,
95 if (tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key) != 0) {
96 DEBUG(0, ("tdb_chainunlock failed\n"));
103 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
107 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
109 struct db_record *result;
110 struct db_ctdb_rec *crec;
114 if (!(result = talloc(mem_ctx, struct db_record))) {
115 DEBUG(0, ("talloc failed\n"));
119 if (!(crec = TALLOC_ZERO_P(result, struct db_ctdb_rec))) {
120 DEBUG(0, ("talloc failed\n"));
125 result->private_data = (void *)crec;
126 crec->ctdb_ctx = ctx;
128 result->key.dsize = key.dsize;
129 result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
130 if (result->key.dptr == NULL) {
131 DEBUG(0, ("talloc failed\n"));
137 * Do a blocking lock on the record
141 if (DEBUGLEVEL >= 10) {
142 char *keystr = hex_encode(result, key.dptr, key.dsize);
143 DEBUG(10, (DEBUGLEVEL > 10
144 ? "Locking db %u key %s\n"
145 : "Locking db %u key %.20s\n",
146 (int)crec->ctdb_ctx->db_id, keystr));
150 if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
151 DEBUG(3, ("tdb_chainlock failed\n"));
156 result->store = db_ctdb_store;
157 result->delete_rec = db_ctdb_delete;
158 talloc_set_destructor(result, db_ctdb_record_destr);
160 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
163 * See if we have a valid record and we are the dmaster. If so, we can
164 * take the shortcut and just return it.
167 if ((ctdb_data.dptr == NULL) ||
168 (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) ||
169 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster != get_my_vnn()
171 || (random() % 2 != 0)
174 SAFE_FREE(ctdb_data.dptr);
175 tdb_chainunlock(ctx->wtdb->tdb, key);
176 talloc_set_destructor(result, NULL);
178 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u)\n",
179 ctdb_data.dptr, ctdb_data.dptr ?
180 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
183 status = ctdbd_migrate(db_ctdbd_conn(ctx), ctx->db_id, key);
184 if (!NT_STATUS_IS_OK(status)) {
185 DEBUG(5, ("ctdb_migrate failed: %s\n",
190 /* now its migrated, try again */
194 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
196 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
197 result->value.dptr = NULL;
199 if ((result->value.dsize != 0)
200 && !(result->value.dptr = (uint8 *)talloc_memdup(
201 result, ctdb_data.dptr + sizeof(crec->header),
202 result->value.dsize))) {
203 DEBUG(0, ("talloc failed\n"));
207 SAFE_FREE(ctdb_data.dptr);
213 fetch (unlocked, no migration) operation on ctdb
215 static int db_ctdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
216 TDB_DATA key, TDB_DATA *data)
218 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
223 /* try a direct fetch */
224 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
227 * See if we have a valid record and we are the dmaster. If so, we can
228 * take the shortcut and just return it.
230 if ((ctdb_data.dptr != NULL) &&
231 (ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header)) &&
232 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster == get_my_vnn()) {
233 /* we are the dmaster - avoid the ctdb protocol op */
235 data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
236 if (data->dsize == 0) {
237 SAFE_FREE(ctdb_data.dptr);
242 data->dptr = (uint8 *)talloc_memdup(
243 mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header),
246 SAFE_FREE(ctdb_data.dptr);
248 if (data->dptr == NULL) {
254 SAFE_FREE(ctdb_data.dptr);
256 /* we weren't able to get it locally - ask ctdb to fetch it for us */
257 status = ctdbd_fetch(db_ctdbd_conn(ctx), ctx->db_id, key, mem_ctx,
259 if (!NT_STATUS_IS_OK(status)) {
260 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status)));
267 struct traverse_state {
268 struct db_context *db;
269 int (*fn)(struct db_record *rec, void *private_data);
273 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
275 struct traverse_state *state = (struct traverse_state *)private_data;
276 struct db_record *rec;
277 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
278 /* we have to give them a locked record to prevent races */
279 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
280 if (rec && rec->value.dsize > 0) {
281 state->fn(rec, state->private_data);
283 talloc_free(tmp_ctx);
286 static int db_ctdb_traverse(struct db_context *db,
287 int (*fn)(struct db_record *rec,
291 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
293 struct traverse_state state;
297 state.private_data = private_data;
299 ctdbd_traverse(ctx->db_id, traverse_callback, &state);
303 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
305 return NT_STATUS_MEDIA_WRITE_PROTECTED;
308 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
310 return NT_STATUS_MEDIA_WRITE_PROTECTED;
313 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
315 struct traverse_state *state = (struct traverse_state *)private_data;
316 struct db_record rec;
319 rec.store = db_ctdb_store_deny;
320 rec.delete_rec = db_ctdb_delete_deny;
321 rec.private_data = state->db;
322 state->fn(&rec, state->private_data);
325 static int db_ctdb_traverse_read(struct db_context *db,
326 int (*fn)(struct db_record *rec,
330 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
332 struct traverse_state state;
336 state.private_data = private_data;
338 ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
342 static int db_ctdb_get_seqnum(struct db_context *db)
344 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
346 return tdb_get_seqnum(ctx->wtdb->tdb);
350 * Get the ctdbd connection for a database. If possible, re-use the messaging
353 static struct ctdbd_connection *db_ctdbd_conn(struct db_ctdb_ctx *ctx)
355 struct ctdbd_connection *result;
357 result = messaging_ctdbd_connection();
359 if (result != NULL) {
361 if (ctx->conn == NULL) {
363 * Someone has initialized messaging since we
364 * initialized our own connection, we don't need it
367 TALLOC_FREE(ctx->conn);
373 if (ctx->conn == NULL) {
375 status = ctdbd_init_connection(ctx, &ctx->conn);
376 if (!NT_STATUS_IS_OK(status)) {
379 set_my_vnn(ctdbd_vnn(ctx->conn));
385 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
387 int hash_size, int tdb_flags,
388 int open_flags, mode_t mode)
390 struct db_context *result;
391 struct db_ctdb_ctx *db_ctdb;
395 if (!lp_clustering()) {
396 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
400 if (!(result = TALLOC_ZERO_P(mem_ctx, struct db_context))) {
401 DEBUG(0, ("talloc failed\n"));
406 if (!(db_ctdb = TALLOC_P(result, struct db_ctdb_ctx))) {
407 DEBUG(0, ("talloc failed\n"));
412 db_ctdb->conn = NULL;
414 status = ctdbd_db_attach(db_ctdbd_conn(db_ctdb), name,
415 &db_ctdb->db_id, tdb_flags);
417 if (!NT_STATUS_IS_OK(status)) {
418 DEBUG(0, ("ctdbd_db_attach failed for %s: %s\n", name,
424 db_path = ctdbd_dbpath(db_ctdbd_conn(db_ctdb), db_ctdb,
427 /* only pass through specific flags */
428 tdb_flags &= TDB_SEQNUM;
430 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags, O_RDWR, 0);
431 if (db_ctdb->wtdb == NULL) {
432 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
436 talloc_free(db_path);
438 result->private_data = (void *)db_ctdb;
439 result->fetch_locked = db_ctdb_fetch_locked;
440 result->fetch = db_ctdb_fetch;
441 result->traverse = db_ctdb_traverse;
442 result->traverse_read = db_ctdb_traverse_read;
443 result->get_seqnum = db_ctdb_get_seqnum;
445 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
446 name, db_ctdb->db_id));
453 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
455 int hash_size, int tdb_flags,
456 int open_flags, mode_t mode)
458 DEBUG(0, ("no clustering compiled in\n"));