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/>.
21 #ifdef CLUSTER_SUPPORT
23 #include "ctdb_private.h"
24 #include "ctdbd_conn.h"
27 struct db_ctdb_transaction_handle {
28 struct db_ctdb_ctx *ctx;
30 * we store the reads and writes done under a transaction:
31 * - one list stores both reads and writes (m_all),
32 * - the other just writes (m_write)
34 struct ctdb_marshall_buffer *m_all;
35 struct ctdb_marshall_buffer *m_write;
42 struct db_context *db;
43 struct tdb_wrap *wtdb;
45 struct db_ctdb_transaction_handle *transaction;
46 struct g_lock_ctx *lock_ctx;
50 struct db_ctdb_ctx *ctdb_ctx;
51 struct ctdb_ltdb_header header;
54 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
59 static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
62 enum TDB_ERROR tret = tdb_error(tdb);
66 status = NT_STATUS_OBJECT_NAME_COLLISION;
69 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
72 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
81 * fetch a record from the tdb, separating out the header
82 * information and returning the body of the record.
84 static NTSTATUS db_ctdb_ltdb_fetch(struct db_ctdb_ctx *db,
86 struct ctdb_ltdb_header *header,
93 rec = tdb_fetch(db->wtdb->tdb, key);
94 if (rec.dsize < sizeof(struct ctdb_ltdb_header)) {
95 status = NT_STATUS_NOT_FOUND;
100 header->dmaster = (uint32_t)-1;
107 *header = *(struct ctdb_ltdb_header *)rec.dptr;
111 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
112 if (data->dsize == 0) {
115 data->dptr = (unsigned char *)talloc_memdup(mem_ctx,
117 + sizeof(struct ctdb_ltdb_header),
119 if (data->dptr == NULL) {
120 status = NT_STATUS_NO_MEMORY;
126 status = NT_STATUS_OK;
134 * Store a record together with the ctdb record header
135 * in the local copy of the database.
137 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
139 struct ctdb_ltdb_header *header,
142 TALLOC_CTX *tmp_ctx = talloc_stackframe();
146 rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
147 rec.dptr = (uint8_t *)talloc_size(tmp_ctx, rec.dsize);
149 if (rec.dptr == NULL) {
150 talloc_free(tmp_ctx);
151 return NT_STATUS_NO_MEMORY;
154 memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
155 memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
157 ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
159 talloc_free(tmp_ctx);
161 return (ret == 0) ? NT_STATUS_OK
162 : tdb_error_to_ntstatus(db->wtdb->tdb);
167 form a ctdb_rec_data record from a key/data pair
169 note that header may be NULL. If not NULL then it is included in the data portion
172 static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
174 struct ctdb_ltdb_header *header,
178 struct ctdb_rec_data *d;
180 length = offsetof(struct ctdb_rec_data, data) + key.dsize +
181 data.dsize + (header?sizeof(*header):0);
182 d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
188 d->keylen = key.dsize;
189 memcpy(&d->data[0], key.dptr, key.dsize);
191 d->datalen = data.dsize + sizeof(*header);
192 memcpy(&d->data[key.dsize], header, sizeof(*header));
193 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
195 d->datalen = data.dsize;
196 memcpy(&d->data[key.dsize], data.dptr, data.dsize);
202 /* helper function for marshalling multiple records */
203 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
204 struct ctdb_marshall_buffer *m,
208 struct ctdb_ltdb_header *header,
211 struct ctdb_rec_data *r;
212 size_t m_size, r_size;
213 struct ctdb_marshall_buffer *m2 = NULL;
215 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
222 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
223 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
230 m_size = talloc_get_size(m);
231 r_size = talloc_get_size(r);
233 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
234 mem_ctx, m, m_size + r_size);
240 memcpy(m_size + (uint8_t *)m2, r, r_size);
249 /* we've finished marshalling, return a data blob with the marshalled records */
250 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
253 data.dptr = (uint8_t *)m;
254 data.dsize = talloc_get_size(m);
259 loop over a marshalling buffer
261 - pass r==NULL to start
262 - loop the number of times indicated by m->count
264 static struct ctdb_rec_data *db_ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
266 struct ctdb_ltdb_header *header,
267 TDB_DATA *key, TDB_DATA *data)
270 r = (struct ctdb_rec_data *)&m->data[0];
272 r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
280 key->dptr = &r->data[0];
281 key->dsize = r->keylen;
284 data->dptr = &r->data[r->keylen];
285 data->dsize = r->datalen;
286 if (header != NULL) {
287 data->dptr += sizeof(*header);
288 data->dsize -= sizeof(*header);
292 if (header != NULL) {
293 if (r->datalen < sizeof(*header)) {
296 *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
303 * CTDB transaction destructor
305 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
309 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
310 if (!NT_STATUS_IS_OK(status)) {
311 DEBUG(0, ("g_lock_unlock failed: %s\n", nt_errstr(status)));
318 * CTDB dbwrap API: transaction_start function
319 * starts a transaction on a persistent database
321 static int db_ctdb_transaction_start(struct db_context *db)
323 struct db_ctdb_transaction_handle *h;
325 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
328 if (!db->persistent) {
329 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
334 if (ctx->transaction) {
335 ctx->transaction->nesting++;
339 h = talloc_zero(db, struct db_ctdb_transaction_handle);
341 DEBUG(0,(__location__ " oom for transaction handle\n"));
347 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
348 (unsigned int)ctx->db_id);
349 if (h->lock_name == NULL) {
350 DEBUG(0, ("talloc_asprintf failed\n"));
356 * Wait a day, i.e. forever...
358 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
359 timeval_set(86400, 0));
360 if (!NT_STATUS_IS_OK(status)) {
361 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
366 talloc_set_destructor(h, db_ctdb_transaction_destructor);
368 ctx->transaction = h;
370 DEBUG(5,(__location__ " Started transaction on db 0x%08x\n", ctx->db_id));
375 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
377 struct ctdb_ltdb_header *pheader,
381 struct ctdb_rec_data *rec = NULL;
382 struct ctdb_ltdb_header h;
392 * Walk the list of records written during this
393 * transaction. If we want to read one we have already
394 * written, return the last written sample. Thus we do not do
395 * a "break;" for the first hit, this record might have been
399 for (i=0; i<buf->count; i++) {
400 TDB_DATA tkey, tdata;
403 rec = db_ctdb_marshall_loop_next(buf, rec, &reqid, &h, &tkey,
409 if (tdb_data_equal(key, tkey)) {
420 data.dptr = (uint8_t *)talloc_memdup(mem_ctx, data.dptr,
422 if ((data.dsize != 0) && (data.dptr == NULL)) {
428 if (pheader != NULL) {
436 fetch a record inside a transaction
438 static int db_ctdb_transaction_fetch(struct db_ctdb_ctx *db,
440 TDB_DATA key, TDB_DATA *data)
442 struct db_ctdb_transaction_handle *h = db->transaction;
446 found = pull_newest_from_marshall_buffer(h->m_write, key, NULL,
452 status = db_ctdb_ltdb_fetch(h->ctx, key, NULL, mem_ctx, data);
454 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
456 } else if (!NT_STATUS_IS_OK(status)) {
460 h->m_all = db_ctdb_marshall_add(h, h->m_all, h->ctx->db_id, 1, key,
462 if (h->m_all == NULL) {
463 DEBUG(0,(__location__ " Failed to add to marshalling "
466 talloc_free(data->dptr);
474 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
475 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
477 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
481 struct db_record *result;
484 if (!(result = talloc(mem_ctx, struct db_record))) {
485 DEBUG(0, ("talloc failed\n"));
489 result->private_data = ctx->transaction;
491 result->key.dsize = key.dsize;
492 result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
493 if (result->key.dptr == NULL) {
494 DEBUG(0, ("talloc failed\n"));
499 result->store = db_ctdb_store_transaction;
500 result->delete_rec = db_ctdb_delete_transaction;
502 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
503 NULL, result, &result->value)) {
507 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
508 if (ctdb_data.dptr == NULL) {
509 /* create the record */
510 result->value = tdb_null;
514 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
515 result->value.dptr = NULL;
517 if ((result->value.dsize != 0)
518 && !(result->value.dptr = (uint8 *)talloc_memdup(
519 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
520 result->value.dsize))) {
521 DEBUG(0, ("talloc failed\n"));
525 SAFE_FREE(ctdb_data.dptr);
530 static int db_ctdb_record_destructor(struct db_record **recp)
532 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
533 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
534 rec->private_data, struct db_ctdb_transaction_handle);
535 int ret = h->ctx->db->transaction_commit(h->ctx->db);
537 DEBUG(0,(__location__ " transaction_commit failed\n"));
543 auto-create a transaction for persistent databases
545 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
550 struct db_record *rec, **recp;
552 res = db_ctdb_transaction_start(ctx->db);
557 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
559 ctx->db->transaction_cancel(ctx->db);
563 /* destroy this transaction when we release the lock */
564 recp = talloc(rec, struct db_record *);
566 ctx->db->transaction_cancel(ctx->db);
571 talloc_set_destructor(recp, db_ctdb_record_destructor);
577 stores a record inside a transaction
579 static int db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
580 TDB_DATA key, TDB_DATA data)
582 TALLOC_CTX *tmp_ctx = talloc_new(h);
584 struct ctdb_ltdb_header header;
588 /* we need the header so we can update the RSN */
590 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
593 rec = tdb_fetch(h->ctx->wtdb->tdb, key);
595 if (rec.dptr != NULL) {
596 memcpy(&header, rec.dptr,
597 sizeof(struct ctdb_ltdb_header));
598 rec.dsize -= sizeof(struct ctdb_ltdb_header);
601 * a special case, we are writing the same
602 * data that is there now
604 if (data.dsize == rec.dsize &&
606 rec.dptr + sizeof(struct ctdb_ltdb_header),
609 talloc_free(tmp_ctx);
616 header.dmaster = get_my_vnn();
619 h->m_all = db_ctdb_marshall_add(h, h->m_all, h->ctx->db_id, 0, key,
621 if (h->m_all == NULL) {
622 DEBUG(0,(__location__ " Failed to add to marshalling "
624 talloc_free(tmp_ctx);
628 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
629 if (h->m_write == NULL) {
630 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
631 talloc_free(tmp_ctx);
635 talloc_free(tmp_ctx);
641 a record store inside a transaction
643 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
645 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
646 rec->private_data, struct db_ctdb_transaction_handle);
649 ret = db_ctdb_transaction_store(h, rec->key, data);
651 return tdb_error_to_ntstatus(h->ctx->wtdb->tdb);
657 a record delete inside a transaction
659 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
661 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
662 rec->private_data, struct db_ctdb_transaction_handle);
665 ret = db_ctdb_transaction_store(h, rec->key, tdb_null);
667 return tdb_error_to_ntstatus(h->ctx->wtdb->tdb);
675 static int db_ctdb_transaction_commit(struct db_context *db)
677 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
681 struct db_ctdb_transaction_handle *h = ctx->transaction;
684 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
688 if (h->nested_cancel) {
689 db->transaction_cancel(db);
690 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
694 if (h->nesting != 0) {
699 DEBUG(5,(__location__ " Commit transaction on db 0x%08x\n", ctx->db_id));
702 if (h->m_write == NULL) {
703 /* no changes were made, potentially after a retry */
707 /* tell ctdbd to commit to the other nodes */
708 rets = ctdbd_control_local(messaging_ctdbd_connection(),
709 CTDB_CONTROL_TRANS3_COMMIT,
711 db_ctdb_marshall_finish(h->m_write),
712 NULL, NULL, &status);
713 if (!NT_STATUS_IS_OK(rets) || status != 0) {
716 * check the database sequence number and
717 * compare it to the seqnum after applying the
718 * marshall buffer. If it is the same: return success.
724 h->ctx->transaction = NULL;
733 static int db_ctdb_transaction_cancel(struct db_context *db)
735 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
737 struct db_ctdb_transaction_handle *h = ctx->transaction;
740 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
744 if (h->nesting != 0) {
746 h->nested_cancel = true;
750 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
752 ctx->transaction = NULL;
758 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
760 struct db_ctdb_rec *crec = talloc_get_type_abort(
761 rec->private_data, struct db_ctdb_rec);
763 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
768 static NTSTATUS db_ctdb_delete(struct db_record *rec)
773 * We have to store the header with empty data. TODO: Fix the
779 return db_ctdb_store(rec, data, 0);
783 static int db_ctdb_record_destr(struct db_record* data)
785 struct db_ctdb_rec *crec = talloc_get_type_abort(
786 data->private_data, struct db_ctdb_rec);
788 DEBUG(10, (DEBUGLEVEL > 10
789 ? "Unlocking db %u key %s\n"
790 : "Unlocking db %u key %.20s\n",
791 (int)crec->ctdb_ctx->db_id,
792 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
795 if (tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key) != 0) {
796 DEBUG(0, ("tdb_chainunlock failed\n"));
803 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
808 struct db_record *result;
809 struct db_ctdb_rec *crec;
812 int migrate_attempts = 0;
814 if (!(result = talloc(mem_ctx, struct db_record))) {
815 DEBUG(0, ("talloc failed\n"));
819 if (!(crec = TALLOC_ZERO_P(result, struct db_ctdb_rec))) {
820 DEBUG(0, ("talloc failed\n"));
825 result->private_data = (void *)crec;
826 crec->ctdb_ctx = ctx;
828 result->key.dsize = key.dsize;
829 result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
830 if (result->key.dptr == NULL) {
831 DEBUG(0, ("talloc failed\n"));
837 * Do a blocking lock on the record
841 if (DEBUGLEVEL >= 10) {
842 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
843 DEBUG(10, (DEBUGLEVEL > 10
844 ? "Locking db %u key %s\n"
845 : "Locking db %u key %.20s\n",
846 (int)crec->ctdb_ctx->db_id, keystr));
850 if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
851 DEBUG(3, ("tdb_chainlock failed\n"));
856 result->store = db_ctdb_store;
857 result->delete_rec = db_ctdb_delete;
858 talloc_set_destructor(result, db_ctdb_record_destr);
860 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
863 * See if we have a valid record and we are the dmaster. If so, we can
864 * take the shortcut and just return it.
867 if ((ctdb_data.dptr == NULL) ||
868 (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) ||
869 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster != get_my_vnn()
871 || (random() % 2 != 0)
874 SAFE_FREE(ctdb_data.dptr);
875 tdb_chainunlock(ctx->wtdb->tdb, key);
876 talloc_set_destructor(result, NULL);
878 migrate_attempts += 1;
880 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u)\n",
881 ctdb_data.dptr, ctdb_data.dptr ?
882 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
885 status = ctdbd_migrate(messaging_ctdbd_connection(),ctx->db_id, key);
886 if (!NT_STATUS_IS_OK(status)) {
887 DEBUG(5, ("ctdb_migrate failed: %s\n",
892 /* now its migrated, try again */
896 if (migrate_attempts > 10) {
897 DEBUG(0, ("db_ctdb_fetch_locked needed %d attempts\n",
901 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
903 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
904 result->value.dptr = NULL;
906 if ((result->value.dsize != 0)
907 && !(result->value.dptr = (uint8 *)talloc_memdup(
908 result, ctdb_data.dptr + sizeof(crec->header),
909 result->value.dsize))) {
910 DEBUG(0, ("talloc failed\n"));
914 SAFE_FREE(ctdb_data.dptr);
919 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
923 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
926 if (ctx->transaction != NULL) {
927 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
930 if (db->persistent) {
931 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
934 return fetch_locked_internal(ctx, mem_ctx, key, db->persistent);
938 fetch (unlocked, no migration) operation on ctdb
940 static int db_ctdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
941 TDB_DATA key, TDB_DATA *data)
943 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
948 if (ctx->transaction) {
949 return db_ctdb_transaction_fetch(ctx, mem_ctx, key, data);
952 /* try a direct fetch */
953 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
956 * See if we have a valid record and we are the dmaster. If so, we can
957 * take the shortcut and just return it.
958 * we bypass the dmaster check for persistent databases
960 if ((ctdb_data.dptr != NULL) &&
961 (ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header)) &&
963 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster == get_my_vnn())) {
964 /* we are the dmaster - avoid the ctdb protocol op */
966 data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
967 if (data->dsize == 0) {
968 SAFE_FREE(ctdb_data.dptr);
973 data->dptr = (uint8 *)talloc_memdup(
974 mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header),
977 SAFE_FREE(ctdb_data.dptr);
979 if (data->dptr == NULL) {
985 SAFE_FREE(ctdb_data.dptr);
987 /* we weren't able to get it locally - ask ctdb to fetch it for us */
988 status = ctdbd_fetch(messaging_ctdbd_connection(),ctx->db_id, key, mem_ctx, data);
989 if (!NT_STATUS_IS_OK(status)) {
990 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status)));
997 struct traverse_state {
998 struct db_context *db;
999 int (*fn)(struct db_record *rec, void *private_data);
1003 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1005 struct traverse_state *state = (struct traverse_state *)private_data;
1006 struct db_record *rec;
1007 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1008 /* we have to give them a locked record to prevent races */
1009 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1010 if (rec && rec->value.dsize > 0) {
1011 state->fn(rec, state->private_data);
1013 talloc_free(tmp_ctx);
1016 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1019 struct traverse_state *state = (struct traverse_state *)private_data;
1020 struct db_record *rec;
1021 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1023 /* we have to give them a locked record to prevent races */
1024 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1025 if (rec && rec->value.dsize > 0) {
1026 ret = state->fn(rec, state->private_data);
1028 talloc_free(tmp_ctx);
1032 static int db_ctdb_traverse(struct db_context *db,
1033 int (*fn)(struct db_record *rec,
1034 void *private_data),
1037 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1038 struct db_ctdb_ctx);
1039 struct traverse_state state;
1043 state.private_data = private_data;
1045 if (db->persistent) {
1046 /* for persistent databases we don't need to do a ctdb traverse,
1047 we can do a faster local traverse */
1048 return tdb_traverse(ctx->wtdb->tdb, traverse_persistent_callback, &state);
1052 ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1056 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1058 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1061 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1063 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1066 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1068 struct traverse_state *state = (struct traverse_state *)private_data;
1069 struct db_record rec;
1072 rec.store = db_ctdb_store_deny;
1073 rec.delete_rec = db_ctdb_delete_deny;
1074 rec.private_data = state->db;
1075 state->fn(&rec, state->private_data);
1078 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1081 struct traverse_state *state = (struct traverse_state *)private_data;
1082 struct db_record rec;
1085 rec.store = db_ctdb_store_deny;
1086 rec.delete_rec = db_ctdb_delete_deny;
1087 rec.private_data = state->db;
1089 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1090 /* a deleted record */
1093 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1094 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1096 return state->fn(&rec, state->private_data);
1099 static int db_ctdb_traverse_read(struct db_context *db,
1100 int (*fn)(struct db_record *rec,
1101 void *private_data),
1104 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1105 struct db_ctdb_ctx);
1106 struct traverse_state state;
1110 state.private_data = private_data;
1112 if (db->persistent) {
1113 /* for persistent databases we don't need to do a ctdb traverse,
1114 we can do a faster local traverse */
1115 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1118 ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1122 static int db_ctdb_get_seqnum(struct db_context *db)
1124 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1125 struct db_ctdb_ctx);
1126 return tdb_get_seqnum(ctx->wtdb->tdb);
1129 static int db_ctdb_get_flags(struct db_context *db)
1131 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1132 struct db_ctdb_ctx);
1133 return tdb_get_flags(ctx->wtdb->tdb);
1136 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1138 int hash_size, int tdb_flags,
1139 int open_flags, mode_t mode)
1141 struct db_context *result;
1142 struct db_ctdb_ctx *db_ctdb;
1144 struct ctdbd_connection *conn;
1146 if (!lp_clustering()) {
1147 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1151 if (!(result = TALLOC_ZERO_P(mem_ctx, struct db_context))) {
1152 DEBUG(0, ("talloc failed\n"));
1153 TALLOC_FREE(result);
1157 if (!(db_ctdb = TALLOC_P(result, struct db_ctdb_ctx))) {
1158 DEBUG(0, ("talloc failed\n"));
1159 TALLOC_FREE(result);
1163 db_ctdb->transaction = NULL;
1164 db_ctdb->db = result;
1166 conn = messaging_ctdbd_connection();
1168 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1169 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1170 TALLOC_FREE(result);
1174 db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1176 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1178 /* only pass through specific flags */
1179 tdb_flags &= TDB_SEQNUM;
1181 /* honor permissions if user has specified O_CREAT */
1182 if (open_flags & O_CREAT) {
1183 chmod(db_path, mode);
1186 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags, O_RDWR, 0);
1187 if (db_ctdb->wtdb == NULL) {
1188 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1189 TALLOC_FREE(result);
1192 talloc_free(db_path);
1194 if (result->persistent) {
1195 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1196 ctdb_conn_msg_ctx(conn));
1197 if (db_ctdb->lock_ctx == NULL) {
1198 DEBUG(0, ("g_lock_ctx_init failed\n"));
1199 TALLOC_FREE(result);
1204 result->private_data = (void *)db_ctdb;
1205 result->fetch_locked = db_ctdb_fetch_locked;
1206 result->fetch = db_ctdb_fetch;
1207 result->traverse = db_ctdb_traverse;
1208 result->traverse_read = db_ctdb_traverse_read;
1209 result->get_seqnum = db_ctdb_get_seqnum;
1210 result->get_flags = db_ctdb_get_flags;
1211 result->transaction_start = db_ctdb_transaction_start;
1212 result->transaction_commit = db_ctdb_transaction_commit;
1213 result->transaction_cancel = db_ctdb_transaction_cancel;
1215 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1216 name, db_ctdb->db_id));