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, ("Unlocking key %s\n",
89 hex_encode(data, (unsigned char *)data->key.dptr,
92 if (tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key) != 0) {
93 DEBUG(0, ("tdb_chainunlock failed\n"));
100 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
104 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
106 struct db_record *result;
107 struct db_ctdb_rec *crec;
111 if (!(result = talloc(mem_ctx, struct db_record))) {
112 DEBUG(0, ("talloc failed\n"));
116 if (!(crec = TALLOC_ZERO_P(result, struct db_ctdb_rec))) {
117 DEBUG(0, ("talloc failed\n"));
122 result->private_data = (void *)crec;
123 crec->ctdb_ctx = ctx;
125 result->key.dsize = key.dsize;
126 result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
127 if (result->key.dptr == NULL) {
128 DEBUG(0, ("talloc failed\n"));
134 * Do a blocking lock on the record
138 DEBUG(10, ("Locking key %s\n",
139 hex_encode(result, (unsigned char *)key.dptr,
142 if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
143 DEBUG(3, ("tdb_chainlock failed\n"));
148 result->store = db_ctdb_store;
149 result->delete_rec = db_ctdb_delete;
150 talloc_set_destructor(result, db_ctdb_record_destr);
152 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
155 * See if we have a valid record and we are the dmaster. If so, we can
156 * take the shortcut and just return it.
159 if ((ctdb_data.dptr == NULL) ||
160 (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) ||
161 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster != get_my_vnn()
163 || (random() % 2 != 0)
166 SAFE_FREE(ctdb_data.dptr);
167 tdb_chainunlock(ctx->wtdb->tdb, key);
168 talloc_set_destructor(result, NULL);
170 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u)\n",
171 ctdb_data.dptr, ctdb_data.dptr ?
172 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
175 status = ctdbd_migrate(db_ctdbd_conn(ctx), ctx->db_id, key);
176 if (!NT_STATUS_IS_OK(status)) {
177 DEBUG(5, ("ctdb_migrate failed: %s\n",
182 /* now its migrated, try again */
186 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
188 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
189 result->value.dptr = NULL;
191 if ((result->value.dsize != 0)
192 && !(result->value.dptr = (uint8 *)talloc_memdup(
193 result, ctdb_data.dptr + sizeof(crec->header),
194 result->value.dsize))) {
195 DEBUG(0, ("talloc failed\n"));
199 SAFE_FREE(ctdb_data.dptr);
205 fetch (unlocked, no migration) operation on ctdb
207 static int db_ctdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
208 TDB_DATA key, TDB_DATA *data)
210 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
215 /* try a direct fetch */
216 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
219 * See if we have a valid record and we are the dmaster. If so, we can
220 * take the shortcut and just return it.
222 if ((ctdb_data.dptr != NULL) &&
223 (ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header)) &&
224 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster == get_my_vnn()) {
225 /* we are the dmaster - avoid the ctdb protocol op */
227 data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
228 if (data->dsize == 0) {
229 SAFE_FREE(ctdb_data.dptr);
234 data->dptr = (uint8 *)talloc_memdup(
235 mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header),
238 SAFE_FREE(ctdb_data.dptr);
240 if (data->dptr == NULL) {
246 SAFE_FREE(ctdb_data.dptr);
248 /* we weren't able to get it locally - ask ctdb to fetch it for us */
249 status = ctdbd_fetch(db_ctdbd_conn(ctx), ctx->db_id, key, mem_ctx,
251 if (!NT_STATUS_IS_OK(status)) {
252 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status)));
259 struct traverse_state {
260 struct db_context *db;
261 int (*fn)(struct db_record *rec, void *private_data);
265 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
267 struct traverse_state *state = (struct traverse_state *)private_data;
268 struct db_record *rec;
269 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
270 /* we have to give them a locked record to prevent races */
271 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
272 if (rec && rec->value.dsize > 0) {
273 state->fn(rec, state->private_data);
275 talloc_free(tmp_ctx);
278 static int db_ctdb_traverse(struct db_context *db,
279 int (*fn)(struct db_record *rec,
283 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
285 struct traverse_state state;
289 state.private_data = private_data;
291 ctdbd_traverse(ctx->db_id, traverse_callback, &state);
295 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
297 return NT_STATUS_MEDIA_WRITE_PROTECTED;
300 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
302 return NT_STATUS_MEDIA_WRITE_PROTECTED;
305 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
307 struct traverse_state *state = (struct traverse_state *)private_data;
308 struct db_record rec;
311 rec.store = db_ctdb_store_deny;
312 rec.delete_rec = db_ctdb_delete_deny;
313 rec.private_data = state->db;
314 state->fn(&rec, state->private_data);
317 static int db_ctdb_traverse_read(struct db_context *db,
318 int (*fn)(struct db_record *rec,
322 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
324 struct traverse_state state;
328 state.private_data = private_data;
330 ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
334 static int db_ctdb_get_seqnum(struct db_context *db)
336 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
338 return tdb_get_seqnum(ctx->wtdb->tdb);
342 * Get the ctdbd connection for a database. If possible, re-use the messaging
345 static struct ctdbd_connection *db_ctdbd_conn(struct db_ctdb_ctx *ctx)
347 struct ctdbd_connection *result;
349 result = messaging_ctdbd_connection();
351 if (result != NULL) {
353 if (ctx->conn == NULL) {
355 * Someone has initialized messaging since we
356 * initialized our own connection, we don't need it
359 TALLOC_FREE(ctx->conn);
365 if (ctx->conn == NULL) {
366 ctdbd_init_connection(ctx, &ctx->conn);
367 set_my_vnn(ctdbd_vnn(ctx->conn));
373 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
375 int hash_size, int tdb_flags,
376 int open_flags, mode_t mode)
378 struct db_context *result;
379 struct db_ctdb_ctx *db_ctdb;
383 if (!lp_clustering()) {
384 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
388 if (!(result = TALLOC_ZERO_P(mem_ctx, struct db_context))) {
389 DEBUG(0, ("talloc failed\n"));
394 if (!(db_ctdb = TALLOC_P(result, struct db_ctdb_ctx))) {
395 DEBUG(0, ("talloc failed\n"));
400 db_ctdb->conn = NULL;
402 status = ctdbd_db_attach(db_ctdbd_conn(db_ctdb), name,
403 &db_ctdb->db_id, tdb_flags);
405 if (!NT_STATUS_IS_OK(status)) {
406 DEBUG(0, ("ctdbd_db_attach failed for %s: %s\n", name,
412 db_path = ctdbd_dbpath(db_ctdbd_conn(db_ctdb), db_ctdb,
415 /* only pass through specific flags */
416 tdb_flags &= TDB_SEQNUM;
418 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags, O_RDWR, 0);
419 if (db_ctdb->wtdb == NULL) {
420 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
424 talloc_free(db_path);
426 result->private_data = (void *)db_ctdb;
427 result->fetch_locked = db_ctdb_fetch_locked;
428 result->fetch = db_ctdb_fetch;
429 result->traverse = db_ctdb_traverse;
430 result->traverse_read = db_ctdb_traverse_read;
431 result->get_seqnum = db_ctdb_get_seqnum;
433 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
434 name, db_ctdb->db_id));
441 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
443 int hash_size, int tdb_flags,
444 int open_flags, mode_t mode)
446 DEBUG(0, ("no clustering compiled in\n"));