2 Unix SMB/CIFS implementation.
3 Database interface wrapper around ctdbd
4 Copyright (C) Volker Lendecke 2007-2009
5 Copyright (C) Michael Adam 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "lib/tdb_wrap/tdb_wrap.h"
25 #include "dbwrap/dbwrap.h"
26 #include "dbwrap/dbwrap_ctdb.h"
27 #include "dbwrap/dbwrap_rbt.h"
28 #include "lib/param/param.h"
30 #include "ctdb/include/ctdb_protocol.h"
31 #include "ctdbd_conn.h"
32 #include "dbwrap/dbwrap.h"
33 #include "dbwrap/dbwrap_private.h"
34 #include "dbwrap/dbwrap_ctdb.h"
37 #include "lib/cluster_support.h"
38 #include "lib/util/tevent_ntstatus.h"
40 struct db_ctdb_transaction_handle {
41 struct db_ctdb_ctx *ctx;
43 * we store the writes done under a transaction:
45 struct ctdb_marshall_buffer *m_write;
52 struct db_context *db;
53 struct ctdbd_connection *conn;
54 struct tdb_wrap *wtdb;
56 struct db_ctdb_transaction_handle *transaction;
57 struct g_lock_ctx *lock_ctx;
59 /* thresholds for warning messages */
60 int warn_unlock_msecs;
61 int warn_migrate_msecs;
62 int warn_migrate_attempts;
63 int warn_locktime_msecs;
67 struct db_ctdb_ctx *ctdb_ctx;
68 struct ctdb_ltdb_header header;
69 struct timeval lock_time;
72 struct ctdb_async_ctx {
74 struct ctdbd_connection *async_conn;
77 static struct ctdb_async_ctx ctdb_async_ctx;
79 static int ctdb_async_ctx_init_internal(TALLOC_CTX *mem_ctx,
80 struct tevent_context *ev,
86 TALLOC_FREE(ctdb_async_ctx.async_conn);
87 ctdb_async_ctx.initialized = false;
90 if (ctdb_async_ctx.initialized) {
95 ret = ctdbd_init_connection(mem_ctx,
98 &ctdb_async_ctx.async_conn);
101 if (ctdb_async_ctx.async_conn == NULL) {
102 DBG_ERR("ctdbd_init_connection failed\n");
106 ret = ctdbd_setup_fde(ctdb_async_ctx.async_conn, ev);
108 DBG_ERR("ctdbd_setup_fde failed\n");
109 TALLOC_FREE(ctdb_async_ctx.async_conn);
113 ctdb_async_ctx.initialized = true;
117 static int ctdb_async_ctx_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev)
119 return ctdb_async_ctx_init_internal(mem_ctx, ev, false);
122 int ctdb_async_ctx_reinit(TALLOC_CTX *mem_ctx, struct tevent_context *ev)
124 return ctdb_async_ctx_init_internal(mem_ctx, ev, true);
127 static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
129 enum TDB_ERROR tret = tdb_error(tdb);
131 return map_nt_error_from_tdb(tret);
134 struct db_ctdb_ltdb_parse_state {
135 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
136 TDB_DATA data, void *private_data);
140 static int db_ctdb_ltdb_parser(TDB_DATA key, TDB_DATA data,
143 struct db_ctdb_ltdb_parse_state *state =
144 (struct db_ctdb_ltdb_parse_state *)private_data;
146 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
151 key, (struct ctdb_ltdb_header *)data.dptr,
152 make_tdb_data(data.dptr + sizeof(struct ctdb_ltdb_header),
153 data.dsize - sizeof(struct ctdb_ltdb_header)),
154 state->private_data);
158 static NTSTATUS db_ctdb_ltdb_parse(
159 struct db_ctdb_ctx *db, TDB_DATA key,
160 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
161 TDB_DATA data, void *private_data),
164 struct db_ctdb_ltdb_parse_state state;
167 state.parser = parser;
168 state.private_data = private_data;
170 ret = tdb_parse_record(db->wtdb->tdb, key, db_ctdb_ltdb_parser,
173 return NT_STATUS_NOT_FOUND;
179 * Store a record together with the ctdb record header
180 * in the local copy of the database.
182 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
184 struct ctdb_ltdb_header *header,
190 recs[0] = (TDB_DATA) { .dptr = (uint8_t *)header,
191 .dsize = sizeof(struct ctdb_ltdb_header) };
194 ret = tdb_storev(db->wtdb->tdb, key, recs, 2, TDB_REPLACE);
196 return (ret == 0) ? NT_STATUS_OK
197 : tdb_error_to_ntstatus(db->wtdb->tdb);
202 form a ctdb_rec_data record from a key/data pair
204 static struct ctdb_rec_data_old *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
206 struct ctdb_ltdb_header *header,
210 struct ctdb_rec_data_old *d;
212 length = offsetof(struct ctdb_rec_data_old, data) + key.dsize +
213 data.dsize + sizeof(*header);
214 d = (struct ctdb_rec_data_old *)talloc_size(mem_ctx, length);
220 d->keylen = key.dsize;
221 memcpy(&d->data[0], key.dptr, key.dsize);
223 d->datalen = data.dsize + sizeof(*header);
224 memcpy(&d->data[key.dsize], header, sizeof(*header));
225 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
230 /* helper function for marshalling multiple records */
231 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
232 struct ctdb_marshall_buffer *m,
236 struct ctdb_ltdb_header *header,
239 struct ctdb_rec_data_old *r;
240 size_t m_size, r_size;
241 struct ctdb_marshall_buffer *m2 = NULL;
243 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
250 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
251 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
258 m_size = talloc_get_size(m);
259 r_size = talloc_get_size(r);
261 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
262 mem_ctx, m, m_size + r_size);
268 memcpy(m_size + (uint8_t *)m2, r, r_size);
277 /* we've finished marshalling, return a data blob with the marshalled records */
278 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
281 data.dptr = (uint8_t *)m;
282 data.dsize = talloc_get_size(m);
287 loop over a marshalling buffer
289 - pass r==NULL to start
290 - loop the number of times indicated by m->count
292 static struct ctdb_rec_data_old *db_ctdb_marshall_loop_next_key(
293 struct ctdb_marshall_buffer *m, struct ctdb_rec_data_old *r, TDB_DATA *key)
296 r = (struct ctdb_rec_data_old *)&m->data[0];
298 r = (struct ctdb_rec_data_old *)(r->length + (uint8_t *)r);
301 key->dptr = &r->data[0];
302 key->dsize = r->keylen;
306 static bool db_ctdb_marshall_buf_parse(
307 struct ctdb_rec_data_old *r, uint32_t *reqid,
308 struct ctdb_ltdb_header **header, TDB_DATA *data)
310 if (r->datalen < sizeof(struct ctdb_ltdb_header)) {
316 data->dptr = &r->data[r->keylen] + sizeof(struct ctdb_ltdb_header);
317 data->dsize = r->datalen - sizeof(struct ctdb_ltdb_header);
319 *header = (struct ctdb_ltdb_header *)&r->data[r->keylen];
325 * CTDB transaction destructor
327 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
331 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
332 if (!NT_STATUS_IS_OK(status)) {
333 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h->lock_name,
341 * CTDB dbwrap API: transaction_start function
342 * starts a transaction on a persistent database
344 static int db_ctdb_transaction_start(struct db_context *db)
346 struct db_ctdb_transaction_handle *h;
348 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
351 if (!db->persistent) {
352 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
357 if (ctx->transaction) {
358 ctx->transaction->nesting++;
359 DEBUG(5, (__location__ " transaction start on db 0x%08x: nesting %d -> %d\n",
360 ctx->db_id, ctx->transaction->nesting - 1, ctx->transaction->nesting));
364 h = talloc_zero(db, struct db_ctdb_transaction_handle);
366 DEBUG(0,(__location__ " oom for transaction handle\n"));
372 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
373 (unsigned int)ctx->db_id);
374 if (h->lock_name == NULL) {
375 DEBUG(0, ("talloc_asprintf failed\n"));
381 * Wait a day, i.e. forever...
383 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
384 timeval_set(86400, 0));
385 if (!NT_STATUS_IS_OK(status)) {
386 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
391 talloc_set_destructor(h, db_ctdb_transaction_destructor);
393 ctx->transaction = h;
395 DEBUG(5,(__location__ " transaction started on db 0x%08x\n", ctx->db_id));
400 static bool parse_newest_in_marshall_buffer(
401 struct ctdb_marshall_buffer *buf, TDB_DATA key,
402 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
403 TDB_DATA data, void *private_data),
406 struct ctdb_rec_data_old *rec = NULL;
407 struct ctdb_ltdb_header *h = NULL;
416 * Walk the list of records written during this
417 * transaction. If we want to read one we have already
418 * written, return the last written sample. Thus we do not do
419 * a "break;" for the first hit, this record might have been
423 for (i=0; i<buf->count; i++) {
427 rec = db_ctdb_marshall_loop_next_key(buf, rec, &tkey);
432 if (!tdb_data_equal(key, tkey)) {
436 if (!db_ctdb_marshall_buf_parse(rec, &reqid, &h, &data)) {
445 parser(key, h, data, private_data);
450 struct pull_newest_from_marshall_buffer_state {
451 struct ctdb_ltdb_header *pheader;
456 static void pull_newest_from_marshall_buffer_parser(
457 TDB_DATA key, struct ctdb_ltdb_header *header,
458 TDB_DATA data, void *private_data)
460 struct pull_newest_from_marshall_buffer_state *state =
461 (struct pull_newest_from_marshall_buffer_state *)private_data;
463 if (state->pheader != NULL) {
464 memcpy(state->pheader, header, sizeof(*state->pheader));
466 if (state->pdata != NULL) {
467 state->pdata->dsize = data.dsize;
468 state->pdata->dptr = (uint8_t *)talloc_memdup(
469 state->mem_ctx, data.dptr, data.dsize);
473 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
475 struct ctdb_ltdb_header *pheader,
479 struct pull_newest_from_marshall_buffer_state state;
481 state.pheader = pheader;
482 state.mem_ctx = mem_ctx;
485 if (!parse_newest_in_marshall_buffer(
486 buf, key, pull_newest_from_marshall_buffer_parser,
490 if ((pdata != NULL) && (pdata->dsize != 0) && (pdata->dptr == NULL)) {
497 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
498 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
500 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
504 struct db_record *result;
507 if (!(result = talloc(mem_ctx, struct db_record))) {
508 DEBUG(0, ("talloc failed\n"));
512 result->db = ctx->db;
513 result->private_data = ctx->transaction;
515 result->key.dsize = key.dsize;
516 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
518 if (result->key.dptr == NULL) {
519 DEBUG(0, ("talloc failed\n"));
524 result->store = db_ctdb_store_transaction;
525 result->delete_rec = db_ctdb_delete_transaction;
527 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
528 NULL, result, &result->value)) {
532 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
533 if (ctdb_data.dptr == NULL) {
534 /* create the record */
535 result->value = tdb_null;
539 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
540 result->value.dptr = NULL;
542 if ((result->value.dsize != 0)
543 && !(result->value.dptr = (uint8_t *)talloc_memdup(
544 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
545 result->value.dsize))) {
546 DEBUG(0, ("talloc failed\n"));
550 SAFE_FREE(ctdb_data.dptr);
555 static int db_ctdb_record_destructor(struct db_record **recp)
557 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
558 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
559 rec->private_data, struct db_ctdb_transaction_handle);
560 int ret = h->ctx->db->transaction_commit(h->ctx->db);
562 DEBUG(0,(__location__ " transaction_commit failed\n"));
568 auto-create a transaction for persistent databases
570 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
575 struct db_record *rec, **recp;
577 res = db_ctdb_transaction_start(ctx->db);
582 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
584 ctx->db->transaction_cancel(ctx->db);
588 /* destroy this transaction when we release the lock */
589 recp = talloc(rec, struct db_record *);
591 ctx->db->transaction_cancel(ctx->db);
596 talloc_set_destructor(recp, db_ctdb_record_destructor);
602 stores a record inside a transaction
604 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
605 TDB_DATA key, TDB_DATA data)
607 TALLOC_CTX *tmp_ctx = talloc_new(h);
609 struct ctdb_ltdb_header header;
613 /* we need the header so we can update the RSN */
615 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
618 rec = tdb_fetch(h->ctx->wtdb->tdb, key);
620 if (rec.dptr != NULL) {
621 memcpy(&header, rec.dptr,
622 sizeof(struct ctdb_ltdb_header));
623 rec.dsize -= sizeof(struct ctdb_ltdb_header);
626 * a special case, we are writing the same
627 * data that is there now
629 if (data.dsize == rec.dsize &&
631 rec.dptr + sizeof(struct ctdb_ltdb_header),
634 talloc_free(tmp_ctx);
641 header.dmaster = ctdbd_vnn(h->ctx->conn);
644 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
645 if (h->m_write == NULL) {
646 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
647 talloc_free(tmp_ctx);
648 return NT_STATUS_NO_MEMORY;
651 talloc_free(tmp_ctx);
657 a record store inside a transaction
659 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
661 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
662 rec->private_data, struct db_ctdb_transaction_handle);
665 status = db_ctdb_transaction_store(h, rec->key, data);
670 a record delete inside a transaction
672 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
674 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
675 rec->private_data, struct db_ctdb_transaction_handle);
678 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
682 static void db_ctdb_fetch_db_seqnum_parser(
683 TDB_DATA key, struct ctdb_ltdb_header *header,
684 TDB_DATA data, void *private_data)
686 uint64_t *seqnum = (uint64_t *)private_data;
688 if (data.dsize != sizeof(uint64_t)) {
692 memcpy(seqnum, data.dptr, sizeof(*seqnum));
696 * Fetch the db sequence number of a persistent db directly from the db.
698 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
704 if (seqnum == NULL) {
705 return NT_STATUS_INVALID_PARAMETER;
708 key = string_term_tdb_data(CTDB_DB_SEQNUM_KEY);
710 status = db_ctdb_ltdb_parse(
711 db, key, db_ctdb_fetch_db_seqnum_parser, seqnum);
713 if (NT_STATUS_IS_OK(status)) {
716 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
724 * Store the database sequence number inside a transaction.
726 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
730 const char *keyname = CTDB_DB_SEQNUM_KEY;
734 key = string_term_tdb_data(keyname);
736 data.dptr = (uint8_t *)&seqnum;
737 data.dsize = sizeof(uint64_t);
739 status = db_ctdb_transaction_store(h, key, data);
747 static int db_ctdb_transaction_commit(struct db_context *db)
749 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
753 struct db_ctdb_transaction_handle *h = ctx->transaction;
754 uint64_t old_seqnum, new_seqnum;
758 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
762 if (h->nested_cancel) {
763 db->transaction_cancel(db);
764 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
768 if (h->nesting != 0) {
770 DEBUG(5, (__location__ " transaction commit on db 0x%08x: nesting %d -> %d\n",
771 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
775 if (h->m_write == NULL) {
777 * No changes were made, so don't change the seqnum,
778 * don't push to other node, just exit with success.
784 DEBUG(5,(__location__ " transaction commit on db 0x%08x\n", ctx->db_id));
787 * As the last db action before committing, bump the database sequence
788 * number. Note that this undoes all changes to the seqnum records
789 * performed under the transaction. This record is not meant to be
790 * modified by user interaction. It is for internal use only...
792 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
793 if (!NT_STATUS_IS_OK(rets)) {
794 DEBUG(1, (__location__ " failed to fetch the db sequence number "
795 "in transaction commit on db 0x%08x\n", ctx->db_id));
800 new_seqnum = old_seqnum + 1;
802 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
803 if (!NT_STATUS_IS_OK(rets)) {
804 DEBUG(1, (__location__ "failed to store the db sequence number "
805 " in transaction commit on db 0x%08x\n", ctx->db_id));
811 /* tell ctdbd to commit to the other nodes */
812 ret = ctdbd_control_local(ctx->conn, CTDB_CONTROL_TRANS3_COMMIT,
814 db_ctdb_marshall_finish(h->m_write),
815 NULL, NULL, &status);
816 if ((ret != 0) || status != 0) {
818 * The TRANS3_COMMIT control should only possibly fail when a
819 * recovery has been running concurrently. In any case, the db
820 * will be the same on all nodes, either the new copy or the
821 * old copy. This can be detected by comparing the old and new
822 * local sequence numbers.
824 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
825 if (!NT_STATUS_IS_OK(rets)) {
826 DEBUG(1, (__location__ " failed to refetch db sequence "
827 "number after failed TRANS3_COMMIT\n"));
832 if (new_seqnum == old_seqnum) {
833 /* Recovery prevented all our changes: retry. */
836 if (new_seqnum != (old_seqnum + 1)) {
837 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
838 "old_seqnum[%lu] + (0 or 1) after failed "
839 "TRANS3_COMMIT - this should not happen!\n",
840 (unsigned long)new_seqnum,
841 (unsigned long)old_seqnum));
846 * Recovery propagated our changes to all nodes, completing
847 * our commit for us - succeed.
854 h->ctx->transaction = NULL;
863 static int db_ctdb_transaction_cancel(struct db_context *db)
865 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
867 struct db_ctdb_transaction_handle *h = ctx->transaction;
870 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
874 if (h->nesting != 0) {
876 h->nested_cancel = true;
877 DEBUG(5, (__location__ " transaction cancel on db 0x%08x: nesting %d -> %d\n",
878 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
882 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
884 ctx->transaction = NULL;
890 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
892 struct db_ctdb_rec *crec = talloc_get_type_abort(
893 rec->private_data, struct db_ctdb_rec);
895 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
900 static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
902 NTSTATUS status = NT_STATUS_OK;
904 struct ctdb_control_schedule_for_deletion *dd;
907 struct db_ctdb_rec *crec = talloc_get_type_abort(
908 rec->private_data, struct db_ctdb_rec);
909 struct db_ctdb_ctx *ctx = crec->ctdb_ctx;
911 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
912 indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
913 if (indata.dptr == NULL) {
914 DEBUG(0, (__location__ " talloc failed!\n"));
915 return NT_STATUS_NO_MEMORY;
918 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
919 dd->db_id = ctx->db_id;
920 dd->hdr = crec->header;
921 dd->keylen = rec->key.dsize;
922 memcpy(dd->key, rec->key.dptr, rec->key.dsize);
924 ret = ctdbd_control_local(ctx->conn,
925 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
926 crec->ctdb_ctx->db_id,
927 CTDB_CTRL_FLAG_NOREPLY, /* flags */
932 talloc_free(indata.dptr);
934 if ((ret != 0) || cstatus != 0) {
935 DEBUG(1, (__location__ " Error sending local control "
936 "SCHEDULE_FOR_DELETION: %s, cstatus = %"PRIi32"\n",
937 strerror(ret), cstatus));
939 status = map_nt_error_from_unix(ret);
941 status = NT_STATUS_UNSUCCESSFUL;
948 static NTSTATUS db_ctdb_delete(struct db_record *rec)
953 * We have to store the header with empty data. TODO: Fix the
957 status = db_ctdb_store(rec, tdb_null, 0);
958 if (!NT_STATUS_IS_OK(status)) {
962 status = db_ctdb_send_schedule_for_deletion(rec);
966 static int db_ctdb_record_destr(struct db_record* data)
968 struct db_ctdb_rec *crec = talloc_get_type_abort(
969 data->private_data, struct db_ctdb_rec);
972 struct timeval before;
975 DEBUG(10, (DEBUGLEVEL > 10
976 ? "Unlocking db %u key %s\n"
977 : "Unlocking db %u key %.20s\n",
978 (int)crec->ctdb_ctx->db_id,
979 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
982 before = timeval_current();
984 ret = tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key);
986 timediff = timeval_elapsed(&before);
987 timediff *= 1000; /* get us milliseconds */
989 if (timediff > crec->ctdb_ctx->warn_unlock_msecs) {
991 key = hex_encode_talloc(talloc_tos(),
992 (unsigned char *)data->key.dptr,
994 DEBUG(0, ("tdb_chainunlock on db %s, key %s took %f milliseconds\n",
995 tdb_name(crec->ctdb_ctx->wtdb->tdb), key,
1001 DEBUG(0, ("tdb_chainunlock failed\n"));
1005 threshold = crec->ctdb_ctx->warn_locktime_msecs;
1006 if (threshold != 0) {
1007 timediff = timeval_elapsed(&crec->lock_time) * 1000;
1008 if (timediff > threshold) {
1011 key = hex_encode_talloc(data,
1012 (unsigned char *)data->key.dptr,
1014 DEBUG(0, ("Held tdb lock on db %s, key %s "
1015 "%f milliseconds\n",
1016 tdb_name(crec->ctdb_ctx->wtdb->tdb),
1025 * Check whether we have a valid local copy of the given record,
1026 * either for reading or for writing.
1028 static bool db_ctdb_can_use_local_hdr(const struct ctdb_ltdb_header *hdr,
1029 uint32_t my_vnn, bool read_only)
1031 if (hdr->dmaster != my_vnn) {
1032 /* If we're not dmaster, it must be r/o copy. */
1033 return read_only && (hdr->flags & CTDB_REC_RO_HAVE_READONLY);
1037 * If we want write access, no one may have r/o copies.
1039 return read_only || !(hdr->flags & CTDB_REC_RO_HAVE_DELEGATIONS);
1042 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data, uint32_t my_vnn,
1045 if (ctdb_data.dptr == NULL) {
1049 if (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
1053 return db_ctdb_can_use_local_hdr(
1054 (struct ctdb_ltdb_header *)ctdb_data.dptr, my_vnn, read_only);
1057 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
1058 TALLOC_CTX *mem_ctx,
1062 struct db_record *result;
1063 struct db_ctdb_rec *crec;
1065 int migrate_attempts;
1066 struct timeval migrate_start;
1067 struct timeval chainlock_start;
1068 struct timeval ctdb_start_time;
1069 double chainlock_time = 0;
1070 double ctdb_time = 0;
1075 if (!(result = talloc(mem_ctx, struct db_record))) {
1076 DEBUG(0, ("talloc failed\n"));
1080 if (!(crec = talloc_zero(result, struct db_ctdb_rec))) {
1081 DEBUG(0, ("talloc failed\n"));
1082 TALLOC_FREE(result);
1086 result->db = ctx->db;
1087 result->private_data = (void *)crec;
1088 crec->ctdb_ctx = ctx;
1090 result->key.dsize = key.dsize;
1091 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
1093 if (result->key.dptr == NULL) {
1094 DEBUG(0, ("talloc failed\n"));
1095 TALLOC_FREE(result);
1099 migrate_attempts = 0;
1100 GetTimeOfDay(&migrate_start);
1103 * Do a blocking lock on the record
1107 if (DEBUGLEVEL >= 10) {
1108 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1109 DEBUG(10, (DEBUGLEVEL > 10
1110 ? "Locking db %u key %s\n"
1111 : "Locking db %u key %.20s\n",
1112 (int)crec->ctdb_ctx->db_id, keystr));
1113 TALLOC_FREE(keystr);
1116 GetTimeOfDay(&chainlock_start);
1118 ? tdb_chainlock_nonblock(ctx->wtdb->tdb, key)
1119 : tdb_chainlock(ctx->wtdb->tdb, key);
1120 chainlock_time += timeval_elapsed(&chainlock_start);
1123 DEBUG(3, ("tdb_chainlock failed\n"));
1124 TALLOC_FREE(result);
1128 result->store = db_ctdb_store;
1129 result->delete_rec = db_ctdb_delete;
1130 talloc_set_destructor(result, db_ctdb_record_destr);
1132 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
1135 * See if we have a valid record and we are the dmaster. If so, we can
1136 * take the shortcut and just return it.
1139 if (!db_ctdb_can_use_local_copy(ctdb_data, ctdbd_vnn(ctx->conn),
1141 SAFE_FREE(ctdb_data.dptr);
1142 tdb_chainunlock(ctx->wtdb->tdb, key);
1143 talloc_set_destructor(result, NULL);
1145 if (tryonly && (migrate_attempts != 0)) {
1146 DEBUG(5, ("record migrated away again\n"));
1147 TALLOC_FREE(result);
1151 migrate_attempts += 1;
1153 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %"PRIu32" "
1154 "(%"PRIu32") %"PRIu32"\n",
1155 ctdb_data.dptr, ctdb_data.dptr ?
1156 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster :
1158 ctdbd_vnn(ctx->conn),
1160 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->flags : 0));
1162 GetTimeOfDay(&ctdb_start_time);
1163 ret = ctdbd_migrate(ctx->conn, ctx->db_id, key);
1164 ctdb_time += timeval_elapsed(&ctdb_start_time);
1167 DEBUG(5, ("ctdbd_migrate failed: %s\n",
1169 TALLOC_FREE(result);
1172 /* now its migrated, try again */
1178 duration = timeval_elapsed(&migrate_start);
1181 * Convert the duration to milliseconds to avoid a
1182 * floating-point division of
1183 * lp_parm_int("migrate_duration") by 1000.
1185 duration_msecs = duration * 1000;
1188 if ((migrate_attempts > ctx->warn_migrate_attempts) ||
1189 (duration_msecs > ctx->warn_migrate_msecs)) {
1192 if (tdb_get_flags(ctx->wtdb->tdb) & TDB_INCOMPATIBLE_HASH) {
1193 chain = tdb_jenkins_hash(&key) %
1194 tdb_hash_size(ctx->wtdb->tdb);
1197 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s, chain %d "
1198 "needed %d attempts, %d milliseconds, "
1199 "chainlock: %f ms, CTDB %f ms\n",
1200 tdb_name(ctx->wtdb->tdb),
1201 hex_encode_talloc(talloc_tos(),
1202 (unsigned char *)key.dptr,
1205 migrate_attempts, duration_msecs,
1206 chainlock_time * 1000.0,
1207 ctdb_time * 1000.0));
1210 GetTimeOfDay(&crec->lock_time);
1212 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1214 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1215 result->value.dptr = NULL;
1217 if ((result->value.dsize != 0)
1218 && !(result->value.dptr = (uint8_t *)talloc_memdup(
1219 result, ctdb_data.dptr + sizeof(crec->header),
1220 result->value.dsize))) {
1221 DEBUG(0, ("talloc failed\n"));
1222 TALLOC_FREE(result);
1225 SAFE_FREE(ctdb_data.dptr);
1230 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1231 TALLOC_CTX *mem_ctx,
1234 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1235 struct db_ctdb_ctx);
1237 if (ctx->transaction != NULL) {
1238 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1241 if (db->persistent) {
1242 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1245 return fetch_locked_internal(ctx, mem_ctx, key, false);
1248 static struct db_record *db_ctdb_try_fetch_locked(struct db_context *db,
1249 TALLOC_CTX *mem_ctx,
1252 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1253 struct db_ctdb_ctx);
1255 if (ctx->transaction != NULL) {
1256 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1259 if (db->persistent) {
1260 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1263 return fetch_locked_internal(ctx, mem_ctx, key, true);
1266 struct db_ctdb_parse_record_state {
1267 void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data);
1270 bool ask_for_readonly_copy;
1275 static void db_ctdb_parse_record_parser(
1276 TDB_DATA key, struct ctdb_ltdb_header *header,
1277 TDB_DATA data, void *private_data)
1279 struct db_ctdb_parse_record_state *state =
1280 (struct db_ctdb_parse_record_state *)private_data;
1281 state->parser(key, data, state->private_data);
1284 static void db_ctdb_parse_record_parser_nonpersistent(
1285 TDB_DATA key, struct ctdb_ltdb_header *header,
1286 TDB_DATA data, void *private_data)
1288 struct db_ctdb_parse_record_state *state =
1289 (struct db_ctdb_parse_record_state *)private_data;
1291 if (db_ctdb_can_use_local_hdr(header, state->my_vnn, true)) {
1293 * A record consisting only of the ctdb header can be
1294 * a validly created empty record or a tombstone
1295 * record of a deleted record (not vacuumed yet). Mark
1298 state->empty_record = (data.dsize == 0);
1299 if (!state->empty_record) {
1300 state->parser(key, data, state->private_data);
1305 * We found something in the db, so it seems that this record,
1306 * while not usable locally right now, is popular. Ask for a
1309 state->ask_for_readonly_copy = true;
1313 static NTSTATUS db_ctdb_try_parse_local_record(struct db_ctdb_ctx *ctx,
1315 struct db_ctdb_parse_record_state *state)
1319 if (ctx->transaction != NULL) {
1320 struct db_ctdb_transaction_handle *h = ctx->transaction;
1324 * Transactions only happen for persistent db's.
1327 found = parse_newest_in_marshall_buffer(
1328 h->m_write, key, db_ctdb_parse_record_parser, state);
1331 return NT_STATUS_OK;
1335 if (ctx->db->persistent) {
1337 * Persistent db, but not found in the transaction buffer
1339 return db_ctdb_ltdb_parse(
1340 ctx, key, db_ctdb_parse_record_parser, state);
1343 state->done = false;
1344 state->ask_for_readonly_copy = false;
1346 status = db_ctdb_ltdb_parse(
1347 ctx, key, db_ctdb_parse_record_parser_nonpersistent, state);
1348 if (NT_STATUS_IS_OK(status) && state->done) {
1349 if (state->empty_record) {
1351 * We know authoritatively, that this is an empty
1352 * record. Since ctdb does not distinguish between empty
1353 * and deleted records, this can be a record stored as
1354 * empty or a not-yet-vacuumed tombstone record of a
1355 * deleted record. Now Samba right now can live without
1356 * empty records, so we can safely report this record
1359 * See bugs 10008 and 12005.
1361 return NT_STATUS_NOT_FOUND;
1363 return NT_STATUS_OK;
1366 return NT_STATUS_MORE_PROCESSING_REQUIRED;
1369 static NTSTATUS db_ctdb_parse_record(struct db_context *db, TDB_DATA key,
1370 void (*parser)(TDB_DATA key,
1372 void *private_data),
1375 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1376 db->private_data, struct db_ctdb_ctx);
1377 struct db_ctdb_parse_record_state state;
1381 state.parser = parser;
1382 state.private_data = private_data;
1383 state.my_vnn = ctdbd_vnn(ctx->conn);
1384 state.empty_record = false;
1386 status = db_ctdb_try_parse_local_record(ctx, key, &state);
1387 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1391 ret = ctdbd_parse(ctx->conn, ctx->db_id, key,
1392 state.ask_for_readonly_copy, parser, private_data);
1394 if (ret == ENOENT) {
1397 * NT_STATUS_OBJECT_NAME_NOT_FOUND. Our upper
1398 * layers expect NT_STATUS_NOT_FOUND for "no
1399 * record around". We need to convert dbwrap
1400 * to 0/errno away from NTSTATUS ... :-)
1402 return NT_STATUS_NOT_FOUND;
1404 return map_nt_error_from_unix(ret);
1406 return NT_STATUS_OK;
1409 static void db_ctdb_parse_record_done(struct tevent_req *subreq);
1411 static struct tevent_req *db_ctdb_parse_record_send(
1412 TALLOC_CTX *mem_ctx,
1413 struct tevent_context *ev,
1414 struct db_context *db,
1416 void (*parser)(TDB_DATA key,
1418 void *private_data),
1420 enum dbwrap_req_state *req_state)
1422 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1423 db->private_data, struct db_ctdb_ctx);
1424 struct tevent_req *req = NULL;
1425 struct tevent_req *subreq = NULL;
1426 struct db_ctdb_parse_record_state *state = NULL;
1429 req = tevent_req_create(mem_ctx, &state,
1430 struct db_ctdb_parse_record_state);
1432 *req_state = DBWRAP_REQ_ERROR;
1437 *state = (struct db_ctdb_parse_record_state) {
1439 .private_data = private_data,
1440 .my_vnn = ctdbd_vnn(ctx->conn),
1441 .empty_record = false,
1444 status = db_ctdb_try_parse_local_record(ctx, key, state);
1445 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1446 if (tevent_req_nterror(req, status)) {
1447 *req_state = DBWRAP_REQ_ERROR;
1448 return tevent_req_post(req, ev);
1450 *req_state = DBWRAP_REQ_DONE;
1451 tevent_req_done(req);
1452 return tevent_req_post(req, ev);
1455 subreq = ctdbd_parse_send(state,
1457 ctdb_async_ctx.async_conn,
1460 state->ask_for_readonly_copy,
1464 if (tevent_req_nomem(subreq, req)) {
1465 *req_state = DBWRAP_REQ_ERROR;
1466 return tevent_req_post(req, ev);
1468 tevent_req_set_callback(subreq, db_ctdb_parse_record_done, req);
1473 static void db_ctdb_parse_record_done(struct tevent_req *subreq)
1475 struct tevent_req *req = tevent_req_callback_data(
1476 subreq, struct tevent_req);
1479 ret = ctdbd_parse_recv(subreq);
1480 TALLOC_FREE(subreq);
1482 if (ret == ENOENT) {
1484 * This maps to NT_STATUS_OBJECT_NAME_NOT_FOUND. Our
1485 * upper layers expect NT_STATUS_NOT_FOUND for "no
1486 * record around". We need to convert dbwrap to 0/errno
1487 * away from NTSTATUS ... :-)
1489 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
1492 tevent_req_nterror(req, map_nt_error_from_unix(ret));
1496 tevent_req_done(req);
1500 static NTSTATUS db_ctdb_parse_record_recv(struct tevent_req *req)
1502 return tevent_req_simple_recv_ntstatus(req);
1505 struct traverse_state {
1506 struct db_context *db;
1507 int (*fn)(struct db_record *rec, void *private_data);
1512 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1514 struct traverse_state *state = (struct traverse_state *)private_data;
1515 struct db_record *rec;
1516 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1517 /* we have to give them a locked record to prevent races */
1518 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1519 if (rec && rec->value.dsize > 0) {
1520 state->fn(rec, state->private_data);
1522 talloc_free(tmp_ctx);
1525 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1528 struct traverse_state *state = (struct traverse_state *)private_data;
1529 struct db_record *rec;
1530 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1534 * Skip the __db_sequence_number__ key:
1535 * This is used for persistent transactions internally.
1537 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1538 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1543 /* we have to give them a locked record to prevent races */
1544 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1545 if (rec && rec->value.dsize > 0) {
1546 ret = state->fn(rec, state->private_data);
1550 talloc_free(tmp_ctx);
1554 /* wrapper to use traverse_persistent_callback with dbwrap */
1555 static int traverse_persistent_callback_dbwrap(struct db_record *rec, void* data)
1557 return traverse_persistent_callback(NULL, rec->key, rec->value, data);
1560 static int db_ctdbd_traverse(uint32_t db_id,
1561 void (*fn)(TDB_DATA key, TDB_DATA data,
1562 void *private_data),
1565 struct ctdbd_connection *conn;
1569 ret = ctdbd_init_connection(talloc_tos(), lp_ctdbd_socket(),
1570 lp_ctdb_timeout(), &conn);
1573 DBG_WARNING("ctdbd_init_connection failed: %s\n",
1578 ret = ctdbd_traverse(conn, db_id, fn, private_data);
1582 DBG_WARNING("ctdbd_traverse failed: %s\n",
1591 static int db_ctdb_traverse(struct db_context *db,
1592 int (*fn)(struct db_record *rec,
1593 void *private_data),
1597 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1598 struct db_ctdb_ctx);
1599 struct traverse_state state;
1603 state.private_data = private_data;
1606 if (db->persistent) {
1607 struct tdb_context *ltdb = ctx->wtdb->tdb;
1609 /* for persistent databases we don't need to do a ctdb traverse,
1610 we can do a faster local traverse */
1611 ret = tdb_traverse(ltdb, traverse_persistent_callback, &state);
1615 if (ctx->transaction && ctx->transaction->m_write) {
1617 * we now have to handle keys not yet
1618 * present at transaction start
1620 struct db_context *newkeys = db_open_rbt(talloc_tos());
1621 struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
1622 struct ctdb_rec_data_old *rec=NULL;
1627 if (newkeys == NULL) {
1631 for (i=0; i<mbuf->count; i++) {
1633 rec = db_ctdb_marshall_loop_next_key(
1635 SMB_ASSERT(rec != NULL);
1637 if (!tdb_exists(ltdb, key)) {
1638 dbwrap_store(newkeys, key, tdb_null, 0);
1641 status = dbwrap_traverse(newkeys,
1642 traverse_persistent_callback_dbwrap,
1645 talloc_free(newkeys);
1646 if (!NT_STATUS_IS_OK(status)) {
1654 ret = db_ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1661 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1663 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1666 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1668 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1671 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1673 struct traverse_state *state = (struct traverse_state *)private_data;
1674 struct db_record rec;
1680 rec.store = db_ctdb_store_deny;
1681 rec.delete_rec = db_ctdb_delete_deny;
1682 rec.private_data = NULL;
1683 state->fn(&rec, state->private_data);
1687 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1690 struct traverse_state *state = (struct traverse_state *)private_data;
1691 struct db_record rec;
1694 * Skip the __db_sequence_number__ key:
1695 * This is used for persistent transactions internally.
1697 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1698 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1707 rec.store = db_ctdb_store_deny;
1708 rec.delete_rec = db_ctdb_delete_deny;
1709 rec.private_data = NULL;
1711 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1712 /* a deleted record */
1715 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1716 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1719 return state->fn(&rec, state->private_data);
1722 static int db_ctdb_traverse_read(struct db_context *db,
1723 int (*fn)(struct db_record *rec,
1724 void *private_data),
1728 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1729 struct db_ctdb_ctx);
1730 struct traverse_state state;
1734 state.private_data = private_data;
1737 if (db->persistent) {
1738 /* for persistent databases we don't need to do a ctdb traverse,
1739 we can do a faster local traverse */
1740 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1743 ret = db_ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1750 static int db_ctdb_get_seqnum(struct db_context *db)
1752 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1753 struct db_ctdb_ctx);
1754 return tdb_get_seqnum(ctx->wtdb->tdb);
1757 static size_t db_ctdb_id(struct db_context *db, uint8_t *id, size_t idlen)
1759 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1760 db->private_data, struct db_ctdb_ctx);
1762 if (idlen >= sizeof(ctx->db_id)) {
1763 memcpy(id, &ctx->db_id, sizeof(ctx->db_id));
1766 return sizeof(ctx->db_id);
1769 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1770 struct messaging_context *msg_ctx,
1771 struct ctdbd_connection *conn,
1773 int hash_size, int tdb_flags,
1774 int open_flags, mode_t mode,
1775 enum dbwrap_lock_order lock_order,
1776 uint64_t dbwrap_flags)
1778 struct db_context *result;
1779 struct db_ctdb_ctx *db_ctdb;
1781 struct loadparm_context *lp_ctx;
1786 if (!lp_clustering()) {
1787 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1791 if (!(result = talloc_zero(mem_ctx, struct db_context))) {
1792 DEBUG(0, ("talloc failed\n"));
1793 TALLOC_FREE(result);
1797 if (!(db_ctdb = talloc(result, struct db_ctdb_ctx))) {
1798 DEBUG(0, ("talloc failed\n"));
1799 TALLOC_FREE(result);
1803 result->name = talloc_strdup(result, name);
1804 if (result->name == NULL) {
1805 DEBUG(0, ("talloc failed\n"));
1806 TALLOC_FREE(result);
1810 db_ctdb->transaction = NULL;
1811 db_ctdb->db = result;
1812 db_ctdb->conn = conn;
1814 ret = ctdbd_db_attach(db_ctdb->conn, name, &db_ctdb->db_id, tdb_flags);
1816 DEBUG(0, ("ctdbd_db_attach failed for %s: %s\n", name,
1818 TALLOC_FREE(result);
1822 if (tdb_flags & TDB_SEQNUM) {
1823 data.dptr = (uint8_t *)&db_ctdb->db_id;
1824 data.dsize = sizeof(db_ctdb->db_id);
1826 ret = ctdbd_control_local(conn, CTDB_CONTROL_ENABLE_SEQNUM,
1828 NULL, NULL, &cstatus);
1829 if ((ret != 0) || cstatus != 0) {
1830 DBG_ERR("ctdb_control for enable seqnum "
1831 "failed: %s\n", strerror(ret));
1832 TALLOC_FREE(result);
1837 db_path = ctdbd_dbpath(db_ctdb->conn, db_ctdb, db_ctdb->db_id);
1839 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1840 result->lock_order = lock_order;
1842 /* only pass through specific flags */
1843 tdb_flags &= TDB_SEQNUM|TDB_VOLATILE|
1844 TDB_MUTEX_LOCKING|TDB_CLEAR_IF_FIRST;
1846 if (!result->persistent) {
1847 ret = ctdb_async_ctx_init(NULL, messaging_tevent_context(msg_ctx));
1849 DBG_ERR("ctdb_async_ctx_init failed: %s\n", strerror(ret));
1850 TALLOC_FREE(result);
1855 if (!result->persistent &&
1856 (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS))
1860 indata = make_tdb_data((uint8_t *)&db_ctdb->db_id,
1861 sizeof(db_ctdb->db_id));
1863 ret = ctdbd_control_local(
1864 db_ctdb->conn, CTDB_CONTROL_SET_DB_READONLY, 0, 0,
1865 indata, NULL, NULL, &cstatus);
1866 if ((ret != 0) || (cstatus != 0)) {
1867 DEBUG(1, ("CTDB_CONTROL_SET_DB_READONLY failed: "
1868 "%s, %"PRIi32"\n", strerror(ret), cstatus));
1869 TALLOC_FREE(result);
1874 lp_ctx = loadparm_init_s3(db_path, loadparm_s3_helpers());
1876 if (hash_size == 0) {
1877 hash_size = lpcfg_tdb_hash_size(lp_ctx, db_path);
1880 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size,
1881 lpcfg_tdb_flags(lp_ctx, tdb_flags),
1883 talloc_unlink(db_path, lp_ctx);
1884 if (db_ctdb->wtdb == NULL) {
1885 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1886 TALLOC_FREE(result);
1889 talloc_free(db_path);
1891 /* honor permissions if user has specified O_CREAT */
1892 if (open_flags & O_CREAT) {
1894 fd = tdb_fd(db_ctdb->wtdb->tdb);
1895 ret = fchmod(fd, mode);
1897 DBG_WARNING("fchmod failed: %s\n",
1899 TALLOC_FREE(result);
1904 if (result->persistent) {
1905 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb, msg_ctx);
1906 if (db_ctdb->lock_ctx == NULL) {
1907 DEBUG(0, ("g_lock_ctx_init failed\n"));
1908 TALLOC_FREE(result);
1913 db_ctdb->warn_unlock_msecs = lp_parm_int(-1, "ctdb",
1914 "unlock_warn_threshold", 5);
1915 db_ctdb->warn_migrate_attempts = lp_parm_int(-1, "ctdb",
1916 "migrate_attempts", 10);
1917 db_ctdb->warn_migrate_msecs = lp_parm_int(-1, "ctdb",
1918 "migrate_duration", 5000);
1919 db_ctdb->warn_locktime_msecs = lp_ctdb_locktime_warn_threshold();
1921 result->private_data = (void *)db_ctdb;
1922 result->fetch_locked = db_ctdb_fetch_locked;
1923 result->try_fetch_locked = db_ctdb_try_fetch_locked;
1924 result->parse_record = db_ctdb_parse_record;
1925 result->parse_record_send = db_ctdb_parse_record_send;
1926 result->parse_record_recv = db_ctdb_parse_record_recv;
1927 result->traverse = db_ctdb_traverse;
1928 result->traverse_read = db_ctdb_traverse_read;
1929 result->get_seqnum = db_ctdb_get_seqnum;
1930 result->transaction_start = db_ctdb_transaction_start;
1931 result->transaction_commit = db_ctdb_transaction_commit;
1932 result->transaction_cancel = db_ctdb_transaction_cancel;
1933 result->id = db_ctdb_id;
1935 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1936 name, db_ctdb->db_id));