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"
31 #include "ctdb_private.h"
32 #include "ctdbd_conn.h"
33 #include "dbwrap/dbwrap.h"
34 #include "dbwrap/dbwrap_private.h"
35 #include "dbwrap/dbwrap_ctdb.h"
38 #include "lib/cluster_support.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 tdb_wrap *wtdb;
55 struct db_ctdb_transaction_handle *transaction;
56 struct g_lock_ctx *lock_ctx;
58 /* thresholds for warning messages */
59 int warn_unlock_msecs;
60 int warn_migrate_msecs;
61 int warn_migrate_attempts;
62 int warn_locktime_msecs;
66 struct db_ctdb_ctx *ctdb_ctx;
67 struct ctdb_ltdb_header header;
68 struct timeval lock_time;
71 static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
73 enum TDB_ERROR tret = tdb_error(tdb);
75 return map_nt_error_from_tdb(tret);
78 struct db_ctdb_ltdb_parse_state {
79 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
80 TDB_DATA data, void *private_data);
84 static int db_ctdb_ltdb_parser(TDB_DATA key, TDB_DATA data,
87 struct db_ctdb_ltdb_parse_state *state =
88 (struct db_ctdb_ltdb_parse_state *)private_data;
90 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
95 key, (struct ctdb_ltdb_header *)data.dptr,
96 make_tdb_data(data.dptr + sizeof(struct ctdb_ltdb_header),
97 data.dsize - sizeof(struct ctdb_ltdb_header)),
102 static NTSTATUS db_ctdb_ltdb_parse(
103 struct db_ctdb_ctx *db, TDB_DATA key,
104 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
105 TDB_DATA data, void *private_data),
108 struct db_ctdb_ltdb_parse_state state;
111 state.parser = parser;
112 state.private_data = private_data;
114 ret = tdb_parse_record(db->wtdb->tdb, key, db_ctdb_ltdb_parser,
117 return NT_STATUS_NOT_FOUND;
123 * Store a record together with the ctdb record header
124 * in the local copy of the database.
126 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
128 struct ctdb_ltdb_header *header,
134 rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
135 rec.dptr = (uint8_t *)talloc_size(talloc_tos(), rec.dsize);
137 if (rec.dptr == NULL) {
138 return NT_STATUS_NO_MEMORY;
141 memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
142 memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
144 ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
146 talloc_free(rec.dptr);
148 return (ret == 0) ? NT_STATUS_OK
149 : tdb_error_to_ntstatus(db->wtdb->tdb);
154 form a ctdb_rec_data record from a key/data pair
156 static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
158 struct ctdb_ltdb_header *header,
162 struct ctdb_rec_data *d;
164 length = offsetof(struct ctdb_rec_data, data) + key.dsize +
165 data.dsize + sizeof(*header);
166 d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
172 d->keylen = key.dsize;
173 memcpy(&d->data[0], key.dptr, key.dsize);
175 d->datalen = data.dsize + sizeof(*header);
176 memcpy(&d->data[key.dsize], header, sizeof(*header));
177 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
182 /* helper function for marshalling multiple records */
183 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
184 struct ctdb_marshall_buffer *m,
188 struct ctdb_ltdb_header *header,
191 struct ctdb_rec_data *r;
192 size_t m_size, r_size;
193 struct ctdb_marshall_buffer *m2 = NULL;
195 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
202 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
203 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
210 m_size = talloc_get_size(m);
211 r_size = talloc_get_size(r);
213 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
214 mem_ctx, m, m_size + r_size);
220 memcpy(m_size + (uint8_t *)m2, r, r_size);
229 /* we've finished marshalling, return a data blob with the marshalled records */
230 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
233 data.dptr = (uint8_t *)m;
234 data.dsize = talloc_get_size(m);
239 loop over a marshalling buffer
241 - pass r==NULL to start
242 - loop the number of times indicated by m->count
244 static struct ctdb_rec_data *db_ctdb_marshall_loop_next_key(
245 struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r, TDB_DATA *key)
248 r = (struct ctdb_rec_data *)&m->data[0];
250 r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
253 key->dptr = &r->data[0];
254 key->dsize = r->keylen;
258 static bool db_ctdb_marshall_buf_parse(
259 struct ctdb_rec_data *r, uint32_t *reqid,
260 struct ctdb_ltdb_header **header, TDB_DATA *data)
262 if (r->datalen < sizeof(struct ctdb_ltdb_header)) {
268 data->dptr = &r->data[r->keylen] + sizeof(struct ctdb_ltdb_header);
269 data->dsize = r->datalen - sizeof(struct ctdb_ltdb_header);
271 *header = (struct ctdb_ltdb_header *)&r->data[r->keylen];
277 * CTDB transaction destructor
279 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
283 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
284 if (!NT_STATUS_IS_OK(status)) {
285 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h->lock_name,
293 * CTDB dbwrap API: transaction_start function
294 * starts a transaction on a persistent database
296 static int db_ctdb_transaction_start(struct db_context *db)
298 struct db_ctdb_transaction_handle *h;
300 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
303 if (!db->persistent) {
304 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
309 if (ctx->transaction) {
310 ctx->transaction->nesting++;
311 DEBUG(5, (__location__ " transaction start on db 0x%08x: nesting %d -> %d\n",
312 ctx->db_id, ctx->transaction->nesting - 1, ctx->transaction->nesting));
316 h = talloc_zero(db, struct db_ctdb_transaction_handle);
318 DEBUG(0,(__location__ " oom for transaction handle\n"));
324 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
325 (unsigned int)ctx->db_id);
326 if (h->lock_name == NULL) {
327 DEBUG(0, ("talloc_asprintf failed\n"));
333 * Wait a day, i.e. forever...
335 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
336 timeval_set(86400, 0));
337 if (!NT_STATUS_IS_OK(status)) {
338 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
343 talloc_set_destructor(h, db_ctdb_transaction_destructor);
345 ctx->transaction = h;
347 DEBUG(5,(__location__ " transaction started on db 0x%08x\n", ctx->db_id));
352 static bool parse_newest_in_marshall_buffer(
353 struct ctdb_marshall_buffer *buf, TDB_DATA key,
354 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
355 TDB_DATA data, void *private_data),
358 struct ctdb_rec_data *rec = NULL;
359 struct ctdb_ltdb_header *h = NULL;
368 * Walk the list of records written during this
369 * transaction. If we want to read one we have already
370 * written, return the last written sample. Thus we do not do
371 * a "break;" for the first hit, this record might have been
375 for (i=0; i<buf->count; i++) {
379 rec = db_ctdb_marshall_loop_next_key(buf, rec, &tkey);
384 if (!tdb_data_equal(key, tkey)) {
388 if (!db_ctdb_marshall_buf_parse(rec, &reqid, &h, &data)) {
397 parser(key, h, data, private_data);
402 struct pull_newest_from_marshall_buffer_state {
403 struct ctdb_ltdb_header *pheader;
408 static void pull_newest_from_marshall_buffer_parser(
409 TDB_DATA key, struct ctdb_ltdb_header *header,
410 TDB_DATA data, void *private_data)
412 struct pull_newest_from_marshall_buffer_state *state =
413 (struct pull_newest_from_marshall_buffer_state *)private_data;
415 if (state->pheader != NULL) {
416 memcpy(state->pheader, header, sizeof(*state->pheader));
418 if (state->pdata != NULL) {
419 state->pdata->dsize = data.dsize;
420 state->pdata->dptr = (uint8_t *)talloc_memdup(
421 state->mem_ctx, data.dptr, data.dsize);
425 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
427 struct ctdb_ltdb_header *pheader,
431 struct pull_newest_from_marshall_buffer_state state;
433 state.pheader = pheader;
434 state.mem_ctx = mem_ctx;
437 if (!parse_newest_in_marshall_buffer(
438 buf, key, pull_newest_from_marshall_buffer_parser,
442 if ((pdata != NULL) && (pdata->dsize != 0) && (pdata->dptr == NULL)) {
449 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
450 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
452 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
456 struct db_record *result;
459 if (!(result = talloc(mem_ctx, struct db_record))) {
460 DEBUG(0, ("talloc failed\n"));
464 result->db = ctx->db;
465 result->private_data = ctx->transaction;
467 result->key.dsize = key.dsize;
468 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
470 if (result->key.dptr == NULL) {
471 DEBUG(0, ("talloc failed\n"));
476 result->store = db_ctdb_store_transaction;
477 result->delete_rec = db_ctdb_delete_transaction;
479 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
480 NULL, result, &result->value)) {
484 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
485 if (ctdb_data.dptr == NULL) {
486 /* create the record */
487 result->value = tdb_null;
491 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
492 result->value.dptr = NULL;
494 if ((result->value.dsize != 0)
495 && !(result->value.dptr = (uint8_t *)talloc_memdup(
496 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
497 result->value.dsize))) {
498 DEBUG(0, ("talloc failed\n"));
502 SAFE_FREE(ctdb_data.dptr);
507 static int db_ctdb_record_destructor(struct db_record **recp)
509 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
510 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
511 rec->private_data, struct db_ctdb_transaction_handle);
512 int ret = h->ctx->db->transaction_commit(h->ctx->db);
514 DEBUG(0,(__location__ " transaction_commit failed\n"));
520 auto-create a transaction for persistent databases
522 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
527 struct db_record *rec, **recp;
529 res = db_ctdb_transaction_start(ctx->db);
534 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
536 ctx->db->transaction_cancel(ctx->db);
540 /* destroy this transaction when we release the lock */
541 recp = talloc(rec, struct db_record *);
543 ctx->db->transaction_cancel(ctx->db);
548 talloc_set_destructor(recp, db_ctdb_record_destructor);
554 stores a record inside a transaction
556 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
557 TDB_DATA key, TDB_DATA data)
559 TALLOC_CTX *tmp_ctx = talloc_new(h);
561 struct ctdb_ltdb_header header;
565 /* we need the header so we can update the RSN */
567 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
570 rec = tdb_fetch(h->ctx->wtdb->tdb, key);
572 if (rec.dptr != NULL) {
573 memcpy(&header, rec.dptr,
574 sizeof(struct ctdb_ltdb_header));
575 rec.dsize -= sizeof(struct ctdb_ltdb_header);
578 * a special case, we are writing the same
579 * data that is there now
581 if (data.dsize == rec.dsize &&
583 rec.dptr + sizeof(struct ctdb_ltdb_header),
586 talloc_free(tmp_ctx);
593 header.dmaster = get_my_vnn();
596 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
597 if (h->m_write == NULL) {
598 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
599 talloc_free(tmp_ctx);
600 return NT_STATUS_NO_MEMORY;
603 talloc_free(tmp_ctx);
609 a record store inside a transaction
611 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
613 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
614 rec->private_data, struct db_ctdb_transaction_handle);
617 status = db_ctdb_transaction_store(h, rec->key, data);
622 a record delete inside a transaction
624 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
626 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
627 rec->private_data, struct db_ctdb_transaction_handle);
630 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
634 static void db_ctdb_fetch_db_seqnum_parser(
635 TDB_DATA key, struct ctdb_ltdb_header *header,
636 TDB_DATA data, void *private_data)
638 uint64_t *seqnum = (uint64_t *)private_data;
640 if (data.dsize != sizeof(uint64_t)) {
644 memcpy(seqnum, data.dptr, sizeof(*seqnum));
648 * Fetch the db sequence number of a persistent db directly from the db.
650 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
656 if (seqnum == NULL) {
657 return NT_STATUS_INVALID_PARAMETER;
660 key = string_term_tdb_data(CTDB_DB_SEQNUM_KEY);
662 status = db_ctdb_ltdb_parse(
663 db, key, db_ctdb_fetch_db_seqnum_parser, seqnum);
665 if (NT_STATUS_IS_OK(status)) {
668 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
676 * Store the database sequence number inside a transaction.
678 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
682 const char *keyname = CTDB_DB_SEQNUM_KEY;
686 key = string_term_tdb_data(keyname);
688 data.dptr = (uint8_t *)&seqnum;
689 data.dsize = sizeof(uint64_t);
691 status = db_ctdb_transaction_store(h, key, data);
699 static int db_ctdb_transaction_commit(struct db_context *db)
701 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
705 struct db_ctdb_transaction_handle *h = ctx->transaction;
706 uint64_t old_seqnum, new_seqnum;
710 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
714 if (h->nested_cancel) {
715 db->transaction_cancel(db);
716 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
720 if (h->nesting != 0) {
722 DEBUG(5, (__location__ " transaction commit on db 0x%08x: nesting %d -> %d\n",
723 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
727 if (h->m_write == NULL) {
729 * No changes were made, so don't change the seqnum,
730 * don't push to other node, just exit with success.
736 DEBUG(5,(__location__ " transaction commit on db 0x%08x\n", ctx->db_id));
739 * As the last db action before committing, bump the database sequence
740 * number. Note that this undoes all changes to the seqnum records
741 * performed under the transaction. This record is not meant to be
742 * modified by user interaction. It is for internal use only...
744 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
745 if (!NT_STATUS_IS_OK(rets)) {
746 DEBUG(1, (__location__ " failed to fetch the db sequence number "
747 "in transaction commit on db 0x%08x\n", ctx->db_id));
752 new_seqnum = old_seqnum + 1;
754 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
755 if (!NT_STATUS_IS_OK(rets)) {
756 DEBUG(1, (__location__ "failed to store the db sequence number "
757 " in transaction commit on db 0x%08x\n", ctx->db_id));
763 /* tell ctdbd to commit to the other nodes */
764 rets = ctdbd_control_local(messaging_ctdbd_connection(),
765 CTDB_CONTROL_TRANS3_COMMIT,
767 db_ctdb_marshall_finish(h->m_write),
768 NULL, NULL, &status);
769 if (!NT_STATUS_IS_OK(rets) || status != 0) {
771 * The TRANS3_COMMIT control should only possibly fail when a
772 * recovery has been running concurrently. In any case, the db
773 * will be the same on all nodes, either the new copy or the
774 * old copy. This can be detected by comparing the old and new
775 * local sequence numbers.
777 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
778 if (!NT_STATUS_IS_OK(rets)) {
779 DEBUG(1, (__location__ " failed to refetch db sequence "
780 "number after failed TRANS3_COMMIT\n"));
785 if (new_seqnum == old_seqnum) {
786 /* Recovery prevented all our changes: retry. */
789 if (new_seqnum != (old_seqnum + 1)) {
790 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
791 "old_seqnum[%lu] + (0 or 1) after failed "
792 "TRANS3_COMMIT - this should not happen!\n",
793 (unsigned long)new_seqnum,
794 (unsigned long)old_seqnum));
799 * Recovery propagated our changes to all nodes, completing
800 * our commit for us - succeed.
807 h->ctx->transaction = NULL;
816 static int db_ctdb_transaction_cancel(struct db_context *db)
818 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
820 struct db_ctdb_transaction_handle *h = ctx->transaction;
823 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
827 if (h->nesting != 0) {
829 h->nested_cancel = true;
830 DEBUG(5, (__location__ " transaction cancel on db 0x%08x: nesting %d -> %d\n",
831 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
835 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
837 ctx->transaction = NULL;
843 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
845 struct db_ctdb_rec *crec = talloc_get_type_abort(
846 rec->private_data, struct db_ctdb_rec);
848 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
853 static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
856 struct ctdb_control_schedule_for_deletion *dd;
859 struct db_ctdb_rec *crec = talloc_get_type_abort(
860 rec->private_data, struct db_ctdb_rec);
862 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
863 indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
864 if (indata.dptr == NULL) {
865 DEBUG(0, (__location__ " talloc failed!\n"));
866 return NT_STATUS_NO_MEMORY;
869 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
870 dd->db_id = crec->ctdb_ctx->db_id;
871 dd->hdr = crec->header;
872 dd->keylen = rec->key.dsize;
873 memcpy(dd->key, rec->key.dptr, rec->key.dsize);
875 status = ctdbd_control_local(messaging_ctdbd_connection(),
876 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
877 crec->ctdb_ctx->db_id,
878 CTDB_CTRL_FLAG_NOREPLY, /* flags */
883 talloc_free(indata.dptr);
885 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
886 DEBUG(1, (__location__ " Error sending local control "
887 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
888 nt_errstr(status), cstatus));
889 if (NT_STATUS_IS_OK(status)) {
890 status = NT_STATUS_UNSUCCESSFUL;
897 static NTSTATUS db_ctdb_delete(struct db_record *rec)
902 * We have to store the header with empty data. TODO: Fix the
906 status = db_ctdb_store(rec, tdb_null, 0);
907 if (!NT_STATUS_IS_OK(status)) {
911 status = db_ctdb_send_schedule_for_deletion(rec);
915 static int db_ctdb_record_destr(struct db_record* data)
917 struct db_ctdb_rec *crec = talloc_get_type_abort(
918 data->private_data, struct db_ctdb_rec);
921 struct timeval before;
924 DEBUG(10, (DEBUGLEVEL > 10
925 ? "Unlocking db %u key %s\n"
926 : "Unlocking db %u key %.20s\n",
927 (int)crec->ctdb_ctx->db_id,
928 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
931 before = timeval_current();
933 ret = tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key);
935 timediff = timeval_elapsed(&before);
936 timediff *= 1000; /* get us milliseconds */
938 if (timediff > crec->ctdb_ctx->warn_unlock_msecs) {
940 key = hex_encode_talloc(talloc_tos(),
941 (unsigned char *)data->key.dptr,
943 DEBUG(0, ("tdb_chainunlock on db %s, key %s took %f milliseconds\n",
944 tdb_name(crec->ctdb_ctx->wtdb->tdb), key,
950 DEBUG(0, ("tdb_chainunlock failed\n"));
954 threshold = crec->ctdb_ctx->warn_locktime_msecs;
955 if (threshold != 0) {
956 timediff = timeval_elapsed(&crec->lock_time) * 1000;
957 if (timediff > threshold) {
960 key = hex_encode_talloc(data,
961 (unsigned char *)data->key.dptr,
963 DEBUG(0, ("Held tdb lock on db %s, key %s "
965 tdb_name(crec->ctdb_ctx->wtdb->tdb),
974 * Check whether we have a valid local copy of the given record,
975 * either for reading or for writing.
977 static bool db_ctdb_can_use_local_hdr(const struct ctdb_ltdb_header *hdr,
980 if (hdr->dmaster != get_my_vnn()) {
981 /* If we're not dmaster, it must be r/o copy. */
982 return read_only && (hdr->flags & CTDB_REC_RO_HAVE_READONLY);
986 * If we want write access, no one may have r/o copies.
988 return read_only || !(hdr->flags & CTDB_REC_RO_HAVE_DELEGATIONS);
991 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data, bool read_only)
993 if (ctdb_data.dptr == NULL) {
997 if (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
1001 return db_ctdb_can_use_local_hdr(
1002 (struct ctdb_ltdb_header *)ctdb_data.dptr, read_only);
1005 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
1006 TALLOC_CTX *mem_ctx,
1010 struct db_record *result;
1011 struct db_ctdb_rec *crec;
1014 int migrate_attempts;
1015 struct timeval migrate_start;
1016 struct timeval chainlock_start;
1017 struct timeval ctdb_start_time;
1018 double chainlock_time = 0;
1019 double ctdb_time = 0;
1023 if (!(result = talloc(mem_ctx, struct db_record))) {
1024 DEBUG(0, ("talloc failed\n"));
1028 if (!(crec = talloc_zero(result, struct db_ctdb_rec))) {
1029 DEBUG(0, ("talloc failed\n"));
1030 TALLOC_FREE(result);
1034 result->db = ctx->db;
1035 result->private_data = (void *)crec;
1036 crec->ctdb_ctx = ctx;
1038 result->key.dsize = key.dsize;
1039 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
1041 if (result->key.dptr == NULL) {
1042 DEBUG(0, ("talloc failed\n"));
1043 TALLOC_FREE(result);
1047 migrate_attempts = 0;
1048 GetTimeOfDay(&migrate_start);
1051 * Do a blocking lock on the record
1055 if (DEBUGLEVEL >= 10) {
1056 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1057 DEBUG(10, (DEBUGLEVEL > 10
1058 ? "Locking db %u key %s\n"
1059 : "Locking db %u key %.20s\n",
1060 (int)crec->ctdb_ctx->db_id, keystr));
1061 TALLOC_FREE(keystr);
1064 GetTimeOfDay(&chainlock_start);
1066 ? tdb_chainlock_nonblock(ctx->wtdb->tdb, key)
1067 : tdb_chainlock(ctx->wtdb->tdb, key);
1068 chainlock_time += timeval_elapsed(&chainlock_start);
1071 DEBUG(3, ("tdb_chainlock failed\n"));
1072 TALLOC_FREE(result);
1076 result->store = db_ctdb_store;
1077 result->delete_rec = db_ctdb_delete;
1078 talloc_set_destructor(result, db_ctdb_record_destr);
1080 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
1083 * See if we have a valid record and we are the dmaster. If so, we can
1084 * take the shortcut and just return it.
1087 if (!db_ctdb_can_use_local_copy(ctdb_data, false)) {
1088 SAFE_FREE(ctdb_data.dptr);
1089 tdb_chainunlock(ctx->wtdb->tdb, key);
1090 talloc_set_destructor(result, NULL);
1092 if (tryonly && (migrate_attempts != 0)) {
1093 DEBUG(5, ("record migrated away again\n"));
1094 TALLOC_FREE(result);
1098 migrate_attempts += 1;
1100 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1101 ctdb_data.dptr, ctdb_data.dptr ?
1102 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
1105 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->flags : 0));
1107 GetTimeOfDay(&ctdb_start_time);
1108 status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id,
1110 ctdb_time += timeval_elapsed(&ctdb_start_time);
1112 if (!NT_STATUS_IS_OK(status)) {
1113 DEBUG(5, ("ctdb_migrate failed: %s\n",
1114 nt_errstr(status)));
1115 TALLOC_FREE(result);
1118 /* now its migrated, try again */
1124 duration = timeval_elapsed(&migrate_start);
1127 * Convert the duration to milliseconds to avoid a
1128 * floating-point division of
1129 * lp_parm_int("migrate_duration") by 1000.
1131 duration_msecs = duration * 1000;
1134 if ((migrate_attempts > ctx->warn_migrate_attempts) ||
1135 (duration_msecs > ctx->warn_migrate_msecs)) {
1138 if (tdb_get_flags(ctx->wtdb->tdb) & TDB_INCOMPATIBLE_HASH) {
1139 chain = tdb_jenkins_hash(&key) %
1140 tdb_hash_size(ctx->wtdb->tdb);
1143 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s, chain %d "
1144 "needed %d attempts, %d milliseconds, "
1145 "chainlock: %f ms, CTDB %f ms\n",
1146 tdb_name(ctx->wtdb->tdb),
1147 hex_encode_talloc(talloc_tos(),
1148 (unsigned char *)key.dptr,
1151 migrate_attempts, duration_msecs,
1152 chainlock_time * 1000.0,
1153 ctdb_time * 1000.0));
1156 GetTimeOfDay(&crec->lock_time);
1158 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1160 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1161 result->value.dptr = NULL;
1163 if ((result->value.dsize != 0)
1164 && !(result->value.dptr = (uint8_t *)talloc_memdup(
1165 result, ctdb_data.dptr + sizeof(crec->header),
1166 result->value.dsize))) {
1167 DEBUG(0, ("talloc failed\n"));
1168 TALLOC_FREE(result);
1171 SAFE_FREE(ctdb_data.dptr);
1176 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1177 TALLOC_CTX *mem_ctx,
1180 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1181 struct db_ctdb_ctx);
1183 if (ctx->transaction != NULL) {
1184 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1187 if (db->persistent) {
1188 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1191 return fetch_locked_internal(ctx, mem_ctx, key, false);
1194 static struct db_record *db_ctdb_try_fetch_locked(struct db_context *db,
1195 TALLOC_CTX *mem_ctx,
1198 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1199 struct db_ctdb_ctx);
1201 if (ctx->transaction != NULL) {
1202 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1205 if (db->persistent) {
1206 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1209 return fetch_locked_internal(ctx, mem_ctx, key, true);
1212 struct db_ctdb_parse_record_state {
1213 void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data);
1215 bool ask_for_readonly_copy;
1219 static void db_ctdb_parse_record_parser(
1220 TDB_DATA key, struct ctdb_ltdb_header *header,
1221 TDB_DATA data, void *private_data)
1223 struct db_ctdb_parse_record_state *state =
1224 (struct db_ctdb_parse_record_state *)private_data;
1225 state->parser(key, data, state->private_data);
1228 static void db_ctdb_parse_record_parser_nonpersistent(
1229 TDB_DATA key, struct ctdb_ltdb_header *header,
1230 TDB_DATA data, void *private_data)
1232 struct db_ctdb_parse_record_state *state =
1233 (struct db_ctdb_parse_record_state *)private_data;
1235 if (db_ctdb_can_use_local_hdr(header, true)) {
1236 state->parser(key, data, state->private_data);
1240 * We found something in the db, so it seems that this record,
1241 * while not usable locally right now, is popular. Ask for a
1244 state->ask_for_readonly_copy = true;
1248 static NTSTATUS db_ctdb_parse_record(struct db_context *db, TDB_DATA key,
1249 void (*parser)(TDB_DATA key,
1251 void *private_data),
1254 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1255 db->private_data, struct db_ctdb_ctx);
1256 struct db_ctdb_parse_record_state state;
1259 state.parser = parser;
1260 state.private_data = private_data;
1262 if (ctx->transaction != NULL) {
1263 struct db_ctdb_transaction_handle *h = ctx->transaction;
1267 * Transactions only happen for persistent db's.
1270 found = parse_newest_in_marshall_buffer(
1271 h->m_write, key, db_ctdb_parse_record_parser, &state);
1274 return NT_STATUS_OK;
1278 if (db->persistent) {
1280 * Persistent db, but not found in the transaction buffer
1282 return db_ctdb_ltdb_parse(
1283 ctx, key, db_ctdb_parse_record_parser, &state);
1287 state.ask_for_readonly_copy = false;
1289 status = db_ctdb_ltdb_parse(
1290 ctx, key, db_ctdb_parse_record_parser_nonpersistent, &state);
1291 if (NT_STATUS_IS_OK(status) && state.done) {
1292 return NT_STATUS_OK;
1295 return ctdbd_parse(messaging_ctdbd_connection(), ctx->db_id, key,
1296 state.ask_for_readonly_copy, parser, private_data);
1299 struct traverse_state {
1300 struct db_context *db;
1301 int (*fn)(struct db_record *rec, void *private_data);
1306 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1308 struct traverse_state *state = (struct traverse_state *)private_data;
1309 struct db_record *rec;
1310 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1311 /* we have to give them a locked record to prevent races */
1312 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1313 if (rec && rec->value.dsize > 0) {
1314 state->fn(rec, state->private_data);
1316 talloc_free(tmp_ctx);
1319 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1322 struct traverse_state *state = (struct traverse_state *)private_data;
1323 struct db_record *rec;
1324 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1328 * Skip the __db_sequence_number__ key:
1329 * This is used for persistent transactions internally.
1331 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1332 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1337 /* we have to give them a locked record to prevent races */
1338 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1339 if (rec && rec->value.dsize > 0) {
1340 ret = state->fn(rec, state->private_data);
1344 talloc_free(tmp_ctx);
1348 /* wrapper to use traverse_persistent_callback with dbwrap */
1349 static int traverse_persistent_callback_dbwrap(struct db_record *rec, void* data)
1351 return traverse_persistent_callback(NULL, rec->key, rec->value, data);
1355 static int db_ctdb_traverse(struct db_context *db,
1356 int (*fn)(struct db_record *rec,
1357 void *private_data),
1361 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1362 struct db_ctdb_ctx);
1363 struct traverse_state state;
1367 state.private_data = private_data;
1370 if (db->persistent) {
1371 struct tdb_context *ltdb = ctx->wtdb->tdb;
1374 /* for persistent databases we don't need to do a ctdb traverse,
1375 we can do a faster local traverse */
1376 ret = tdb_traverse(ltdb, traverse_persistent_callback, &state);
1380 if (ctx->transaction && ctx->transaction->m_write) {
1382 * we now have to handle keys not yet
1383 * present at transaction start
1385 struct db_context *newkeys = db_open_rbt(talloc_tos());
1386 struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
1387 struct ctdb_rec_data *rec=NULL;
1391 if (newkeys == NULL) {
1395 for (i=0; i<mbuf->count; i++) {
1397 rec = db_ctdb_marshall_loop_next_key(
1399 SMB_ASSERT(rec != NULL);
1401 if (!tdb_exists(ltdb, key)) {
1402 dbwrap_store(newkeys, key, tdb_null, 0);
1405 status = dbwrap_traverse(newkeys,
1406 traverse_persistent_callback_dbwrap,
1409 talloc_free(newkeys);
1410 if (!NT_STATUS_IS_OK(status)) {
1418 status = ctdbd_traverse(messaging_ctdbd_connection(), ctx->db_id,
1419 traverse_callback, &state);
1420 if (!NT_STATUS_IS_OK(status)) {
1426 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1428 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1431 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1433 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1436 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1438 struct traverse_state *state = (struct traverse_state *)private_data;
1439 struct db_record rec;
1445 rec.store = db_ctdb_store_deny;
1446 rec.delete_rec = db_ctdb_delete_deny;
1447 rec.private_data = NULL;
1448 state->fn(&rec, state->private_data);
1452 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1455 struct traverse_state *state = (struct traverse_state *)private_data;
1456 struct db_record rec;
1459 * Skip the __db_sequence_number__ key:
1460 * This is used for persistent transactions internally.
1462 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1463 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1472 rec.store = db_ctdb_store_deny;
1473 rec.delete_rec = db_ctdb_delete_deny;
1474 rec.private_data = NULL;
1476 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1477 /* a deleted record */
1480 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1481 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1484 return state->fn(&rec, state->private_data);
1487 static int db_ctdb_traverse_read(struct db_context *db,
1488 int (*fn)(struct db_record *rec,
1489 void *private_data),
1493 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1494 struct db_ctdb_ctx);
1495 struct traverse_state state;
1499 state.private_data = private_data;
1502 if (db->persistent) {
1503 /* for persistent databases we don't need to do a ctdb traverse,
1504 we can do a faster local traverse */
1505 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1508 status = ctdbd_traverse(messaging_ctdbd_connection(), ctx->db_id,
1509 traverse_read_callback, &state);
1510 if (!NT_STATUS_IS_OK(status)) {
1516 static int db_ctdb_get_seqnum(struct db_context *db)
1518 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1519 struct db_ctdb_ctx);
1520 return tdb_get_seqnum(ctx->wtdb->tdb);
1523 static size_t db_ctdb_id(struct db_context *db, uint8_t *id, size_t idlen)
1525 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1526 db->private_data, struct db_ctdb_ctx);
1528 if (idlen >= sizeof(ctx->db_id)) {
1529 memcpy(id, &ctx->db_id, sizeof(ctx->db_id));
1532 return sizeof(ctx->db_id);
1535 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1537 int hash_size, int tdb_flags,
1538 int open_flags, mode_t mode,
1539 enum dbwrap_lock_order lock_order,
1540 uint64_t dbwrap_flags)
1542 struct db_context *result;
1543 struct db_ctdb_ctx *db_ctdb;
1545 struct ctdbd_connection *conn;
1546 struct loadparm_context *lp_ctx;
1547 struct ctdb_db_priority prio;
1551 if (!lp_clustering()) {
1552 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1556 if (!(result = talloc_zero(mem_ctx, struct db_context))) {
1557 DEBUG(0, ("talloc failed\n"));
1558 TALLOC_FREE(result);
1562 if (!(db_ctdb = talloc(result, struct db_ctdb_ctx))) {
1563 DEBUG(0, ("talloc failed\n"));
1564 TALLOC_FREE(result);
1568 result->name = talloc_strdup(result, name);
1569 if (result->name == NULL) {
1570 DEBUG(0, ("talloc failed\n"));
1571 TALLOC_FREE(result);
1575 db_ctdb->transaction = NULL;
1576 db_ctdb->db = result;
1578 conn = messaging_ctdbd_connection();
1580 DEBUG(1, ("Could not connect to ctdb\n"));
1581 TALLOC_FREE(result);
1585 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1586 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1587 TALLOC_FREE(result);
1591 db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1593 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1594 result->lock_order = lock_order;
1596 /* only pass through specific flags */
1597 tdb_flags &= TDB_SEQNUM|TDB_VOLATILE|
1598 TDB_MUTEX_LOCKING|TDB_CLEAR_IF_FIRST;
1600 /* honor permissions if user has specified O_CREAT */
1601 if (open_flags & O_CREAT) {
1602 chmod(db_path, mode);
1605 prio.db_id = db_ctdb->db_id;
1606 prio.priority = lock_order;
1608 status = ctdbd_control_local(
1609 conn, CTDB_CONTROL_SET_DB_PRIORITY, 0, 0,
1610 make_tdb_data((uint8_t *)&prio, sizeof(prio)),
1611 NULL, NULL, &cstatus);
1613 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1614 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1615 nt_errstr(status), cstatus));
1616 TALLOC_FREE(result);
1620 if (!result->persistent &&
1621 (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS))
1625 indata = make_tdb_data((uint8_t *)&db_ctdb->db_id,
1626 sizeof(db_ctdb->db_id));
1628 status = ctdbd_control_local(
1629 conn, CTDB_CONTROL_SET_DB_READONLY, 0, 0, indata,
1630 NULL, NULL, &cstatus);
1631 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1632 DEBUG(1, ("CTDB_CONTROL_SET_DB_READONLY failed: "
1633 "%s, %d\n", nt_errstr(status), cstatus));
1634 TALLOC_FREE(result);
1639 lp_ctx = loadparm_init_s3(db_path, loadparm_s3_helpers());
1641 if (hash_size == 0) {
1642 hash_size = lpcfg_tdb_hash_size(lp_ctx, db_path);
1645 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size,
1646 lpcfg_tdb_flags(lp_ctx, tdb_flags),
1648 talloc_unlink(db_path, lp_ctx);
1649 if (db_ctdb->wtdb == NULL) {
1650 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1651 TALLOC_FREE(result);
1654 talloc_free(db_path);
1656 if (result->persistent) {
1657 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1658 ctdb_conn_msg_ctx(conn));
1659 if (db_ctdb->lock_ctx == NULL) {
1660 DEBUG(0, ("g_lock_ctx_init failed\n"));
1661 TALLOC_FREE(result);
1666 db_ctdb->warn_unlock_msecs = lp_parm_int(-1, "ctdb",
1667 "unlock_warn_threshold", 5);
1668 db_ctdb->warn_migrate_attempts = lp_parm_int(-1, "ctdb",
1669 "migrate_attempts", 10);
1670 db_ctdb->warn_migrate_msecs = lp_parm_int(-1, "ctdb",
1671 "migrate_duration", 5000);
1672 db_ctdb->warn_locktime_msecs = lp_ctdb_locktime_warn_threshold();
1674 result->private_data = (void *)db_ctdb;
1675 result->fetch_locked = db_ctdb_fetch_locked;
1676 result->try_fetch_locked = db_ctdb_try_fetch_locked;
1677 result->parse_record = db_ctdb_parse_record;
1678 result->traverse = db_ctdb_traverse;
1679 result->traverse_read = db_ctdb_traverse_read;
1680 result->get_seqnum = db_ctdb_get_seqnum;
1681 result->transaction_start = db_ctdb_transaction_start;
1682 result->transaction_commit = db_ctdb_transaction_commit;
1683 result->transaction_cancel = db_ctdb_transaction_cancel;
1684 result->id = db_ctdb_id;
1685 result->stored_callback = NULL;
1687 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1688 name, db_ctdb->db_id));