dbwrap: Print wait times with full precision
[Samba.git] / source3 / lib / dbwrap / dbwrap_ctdb.c
blob0b30d9fea3f1d34183af2fdb1e1e51fd48638709
1 /*
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/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "lib/tdb_wrap/tdb_wrap.h"
24 #include "util_tdb.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 * It is not possible to include ctdb.h and tdb_compat.h (included via
32 * some other include above) without warnings. This fixes those
33 * warnings.
36 #ifdef typesafe_cb
37 #undef typesafe_cb
38 #endif
40 #ifdef typesafe_cb_preargs
41 #undef typesafe_cb_preargs
42 #endif
44 #ifdef typesafe_cb_postargs
45 #undef typesafe_cb_postargs
46 #endif
48 #include "ctdb.h"
49 #include "ctdb_private.h"
50 #include "ctdbd_conn.h"
51 #include "dbwrap/dbwrap.h"
52 #include "dbwrap/dbwrap_private.h"
53 #include "dbwrap/dbwrap_ctdb.h"
54 #include "g_lock.h"
55 #include "messages.h"
57 struct db_ctdb_transaction_handle {
58 struct db_ctdb_ctx *ctx;
60 * we store the writes done under a transaction:
62 struct ctdb_marshall_buffer *m_write;
63 uint32_t nesting;
64 bool nested_cancel;
65 char *lock_name;
68 struct db_ctdb_ctx {
69 struct db_context *db;
70 struct tdb_wrap *wtdb;
71 uint32_t db_id;
72 struct db_ctdb_transaction_handle *transaction;
73 struct g_lock_ctx *lock_ctx;
75 /* thresholds for warning messages */
76 int warn_unlock_msecs;
77 int warn_migrate_msecs;
78 int warn_migrate_attempts;
79 int warn_locktime_msecs;
82 struct db_ctdb_rec {
83 struct db_ctdb_ctx *ctdb_ctx;
84 struct ctdb_ltdb_header header;
85 struct timeval lock_time;
88 static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
90 enum TDB_ERROR tret = tdb_error(tdb);
92 return map_nt_error_from_tdb(tret);
95 struct db_ctdb_ltdb_parse_state {
96 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
97 TDB_DATA data, void *private_data);
98 void *private_data;
101 static int db_ctdb_ltdb_parser(TDB_DATA key, TDB_DATA data,
102 void *private_data)
104 struct db_ctdb_ltdb_parse_state *state =
105 (struct db_ctdb_ltdb_parse_state *)private_data;
107 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
108 return -1;
111 state->parser(
112 key, (struct ctdb_ltdb_header *)data.dptr,
113 make_tdb_data(data.dptr + sizeof(struct ctdb_ltdb_header),
114 data.dsize - sizeof(struct ctdb_ltdb_header)),
115 state->private_data);
116 return 0;
119 static NTSTATUS db_ctdb_ltdb_parse(
120 struct db_ctdb_ctx *db, TDB_DATA key,
121 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
122 TDB_DATA data, void *private_data),
123 void *private_data)
125 struct db_ctdb_ltdb_parse_state state;
126 int ret;
128 state.parser = parser;
129 state.private_data = private_data;
131 ret = tdb_parse_record(db->wtdb->tdb, key, db_ctdb_ltdb_parser,
132 &state);
133 if (ret == -1) {
134 return NT_STATUS_NOT_FOUND;
136 return NT_STATUS_OK;
140 * Store a record together with the ctdb record header
141 * in the local copy of the database.
143 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
144 TDB_DATA key,
145 struct ctdb_ltdb_header *header,
146 TDB_DATA data)
148 TDB_DATA rec;
149 int ret;
151 rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
152 rec.dptr = (uint8_t *)talloc_size(talloc_tos(), rec.dsize);
154 if (rec.dptr == NULL) {
155 return NT_STATUS_NO_MEMORY;
158 memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
159 memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
161 ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
163 talloc_free(rec.dptr);
165 return (ret == 0) ? NT_STATUS_OK
166 : tdb_error_to_ntstatus(db->wtdb->tdb);
171 form a ctdb_rec_data record from a key/data pair
173 static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
174 TDB_DATA key,
175 struct ctdb_ltdb_header *header,
176 TDB_DATA data)
178 size_t length;
179 struct ctdb_rec_data *d;
181 length = offsetof(struct ctdb_rec_data, data) + key.dsize +
182 data.dsize + sizeof(*header);
183 d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
184 if (d == NULL) {
185 return NULL;
187 d->length = length;
188 d->reqid = reqid;
189 d->keylen = key.dsize;
190 memcpy(&d->data[0], key.dptr, key.dsize);
192 d->datalen = data.dsize + sizeof(*header);
193 memcpy(&d->data[key.dsize], header, sizeof(*header));
194 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
195 return d;
199 /* helper function for marshalling multiple records */
200 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
201 struct ctdb_marshall_buffer *m,
202 uint64_t db_id,
203 uint32_t reqid,
204 TDB_DATA key,
205 struct ctdb_ltdb_header *header,
206 TDB_DATA data)
208 struct ctdb_rec_data *r;
209 size_t m_size, r_size;
210 struct ctdb_marshall_buffer *m2 = NULL;
212 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
213 if (r == NULL) {
214 talloc_free(m);
215 return NULL;
218 if (m == NULL) {
219 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
220 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
221 if (m == NULL) {
222 goto done;
224 m->db_id = db_id;
227 m_size = talloc_get_size(m);
228 r_size = talloc_get_size(r);
230 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
231 mem_ctx, m, m_size + r_size);
232 if (m2 == NULL) {
233 talloc_free(m);
234 goto done;
237 memcpy(m_size + (uint8_t *)m2, r, r_size);
239 m2->count++;
241 done:
242 talloc_free(r);
243 return m2;
246 /* we've finished marshalling, return a data blob with the marshalled records */
247 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
249 TDB_DATA data;
250 data.dptr = (uint8_t *)m;
251 data.dsize = talloc_get_size(m);
252 return data;
256 loop over a marshalling buffer
258 - pass r==NULL to start
259 - loop the number of times indicated by m->count
261 static struct ctdb_rec_data *db_ctdb_marshall_loop_next_key(
262 struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r, TDB_DATA *key)
264 if (r == NULL) {
265 r = (struct ctdb_rec_data *)&m->data[0];
266 } else {
267 r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
270 key->dptr = &r->data[0];
271 key->dsize = r->keylen;
272 return r;
275 static bool db_ctdb_marshall_buf_parse(
276 struct ctdb_rec_data *r, uint32_t *reqid,
277 struct ctdb_ltdb_header **header, TDB_DATA *data)
279 if (r->datalen < sizeof(struct ctdb_ltdb_header)) {
280 return false;
283 *reqid = r->reqid;
285 data->dptr = &r->data[r->keylen] + sizeof(struct ctdb_ltdb_header);
286 data->dsize = r->datalen - sizeof(struct ctdb_ltdb_header);
288 *header = (struct ctdb_ltdb_header *)&r->data[r->keylen];
290 return true;
294 * CTDB transaction destructor
296 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
298 NTSTATUS status;
300 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
301 if (!NT_STATUS_IS_OK(status)) {
302 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h->lock_name,
303 nt_errstr(status)));
304 return -1;
306 return 0;
310 * CTDB dbwrap API: transaction_start function
311 * starts a transaction on a persistent database
313 static int db_ctdb_transaction_start(struct db_context *db)
315 struct db_ctdb_transaction_handle *h;
316 NTSTATUS status;
317 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
318 struct db_ctdb_ctx);
320 if (!db->persistent) {
321 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
322 ctx->db_id));
323 return -1;
326 if (ctx->transaction) {
327 ctx->transaction->nesting++;
328 DEBUG(5, (__location__ " transaction start on db 0x%08x: nesting %d -> %d\n",
329 ctx->db_id, ctx->transaction->nesting - 1, ctx->transaction->nesting));
330 return 0;
333 h = talloc_zero(db, struct db_ctdb_transaction_handle);
334 if (h == NULL) {
335 DEBUG(0,(__location__ " oom for transaction handle\n"));
336 return -1;
339 h->ctx = ctx;
341 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
342 (unsigned int)ctx->db_id);
343 if (h->lock_name == NULL) {
344 DEBUG(0, ("talloc_asprintf failed\n"));
345 TALLOC_FREE(h);
346 return -1;
350 * Wait a day, i.e. forever...
352 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
353 timeval_set(86400, 0));
354 if (!NT_STATUS_IS_OK(status)) {
355 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
356 TALLOC_FREE(h);
357 return -1;
360 talloc_set_destructor(h, db_ctdb_transaction_destructor);
362 ctx->transaction = h;
364 DEBUG(5,(__location__ " transaction started on db 0x%08x\n", ctx->db_id));
366 return 0;
369 static bool parse_newest_in_marshall_buffer(
370 struct ctdb_marshall_buffer *buf, TDB_DATA key,
371 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
372 TDB_DATA data, void *private_data),
373 void *private_data)
375 struct ctdb_rec_data *rec = NULL;
376 struct ctdb_ltdb_header *h = NULL;
377 TDB_DATA data;
378 int i;
380 if (buf == NULL) {
381 return false;
385 * Walk the list of records written during this
386 * transaction. If we want to read one we have already
387 * written, return the last written sample. Thus we do not do
388 * a "break;" for the first hit, this record might have been
389 * overwritten later.
392 for (i=0; i<buf->count; i++) {
393 TDB_DATA tkey;
394 uint32_t reqid;
396 rec = db_ctdb_marshall_loop_next_key(buf, rec, &tkey);
397 if (rec == NULL) {
398 return false;
401 if (!tdb_data_equal(key, tkey)) {
402 continue;
405 if (!db_ctdb_marshall_buf_parse(rec, &reqid, &h, &data)) {
406 return false;
410 if (h == NULL) {
411 return false;
414 parser(key, h, data, private_data);
416 return true;
419 struct pull_newest_from_marshall_buffer_state {
420 struct ctdb_ltdb_header *pheader;
421 TALLOC_CTX *mem_ctx;
422 TDB_DATA *pdata;
425 static void pull_newest_from_marshall_buffer_parser(
426 TDB_DATA key, struct ctdb_ltdb_header *header,
427 TDB_DATA data, void *private_data)
429 struct pull_newest_from_marshall_buffer_state *state =
430 (struct pull_newest_from_marshall_buffer_state *)private_data;
432 if (state->pheader != NULL) {
433 memcpy(state->pheader, header, sizeof(*state->pheader));
435 if (state->pdata != NULL) {
436 state->pdata->dsize = data.dsize;
437 state->pdata->dptr = (uint8_t *)talloc_memdup(
438 state->mem_ctx, data.dptr, data.dsize);
442 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
443 TDB_DATA key,
444 struct ctdb_ltdb_header *pheader,
445 TALLOC_CTX *mem_ctx,
446 TDB_DATA *pdata)
448 struct pull_newest_from_marshall_buffer_state state;
450 state.pheader = pheader;
451 state.mem_ctx = mem_ctx;
452 state.pdata = pdata;
454 if (!parse_newest_in_marshall_buffer(
455 buf, key, pull_newest_from_marshall_buffer_parser,
456 &state)) {
457 return false;
459 if ((pdata != NULL) && (pdata->dsize != 0) && (pdata->dptr == NULL)) {
460 /* ENOMEM */
461 return false;
463 return true;
466 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
467 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
469 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
470 TALLOC_CTX *mem_ctx,
471 TDB_DATA key)
473 struct db_record *result;
474 TDB_DATA ctdb_data;
476 if (!(result = talloc(mem_ctx, struct db_record))) {
477 DEBUG(0, ("talloc failed\n"));
478 return NULL;
481 result->db = ctx->db;
482 result->private_data = ctx->transaction;
484 result->key.dsize = key.dsize;
485 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
486 key.dsize);
487 if (result->key.dptr == NULL) {
488 DEBUG(0, ("talloc failed\n"));
489 TALLOC_FREE(result);
490 return NULL;
493 result->store = db_ctdb_store_transaction;
494 result->delete_rec = db_ctdb_delete_transaction;
496 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
497 NULL, result, &result->value)) {
498 return result;
501 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
502 if (ctdb_data.dptr == NULL) {
503 /* create the record */
504 result->value = tdb_null;
505 return result;
508 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
509 result->value.dptr = NULL;
511 if ((result->value.dsize != 0)
512 && !(result->value.dptr = (uint8_t *)talloc_memdup(
513 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
514 result->value.dsize))) {
515 DEBUG(0, ("talloc failed\n"));
516 TALLOC_FREE(result);
519 SAFE_FREE(ctdb_data.dptr);
521 return result;
524 static int db_ctdb_record_destructor(struct db_record **recp)
526 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
527 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
528 rec->private_data, struct db_ctdb_transaction_handle);
529 int ret = h->ctx->db->transaction_commit(h->ctx->db);
530 if (ret != 0) {
531 DEBUG(0,(__location__ " transaction_commit failed\n"));
533 return 0;
537 auto-create a transaction for persistent databases
539 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
540 TALLOC_CTX *mem_ctx,
541 TDB_DATA key)
543 int res;
544 struct db_record *rec, **recp;
546 res = db_ctdb_transaction_start(ctx->db);
547 if (res == -1) {
548 return NULL;
551 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
552 if (rec == NULL) {
553 ctx->db->transaction_cancel(ctx->db);
554 return NULL;
557 /* destroy this transaction when we release the lock */
558 recp = talloc(rec, struct db_record *);
559 if (recp == NULL) {
560 ctx->db->transaction_cancel(ctx->db);
561 talloc_free(rec);
562 return NULL;
564 *recp = rec;
565 talloc_set_destructor(recp, db_ctdb_record_destructor);
566 return rec;
571 stores a record inside a transaction
573 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
574 TDB_DATA key, TDB_DATA data)
576 TALLOC_CTX *tmp_ctx = talloc_new(h);
577 TDB_DATA rec;
578 struct ctdb_ltdb_header header;
580 ZERO_STRUCT(header);
582 /* we need the header so we can update the RSN */
584 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
585 NULL, NULL)) {
587 rec = tdb_fetch_compat(h->ctx->wtdb->tdb, key);
589 if (rec.dptr != NULL) {
590 memcpy(&header, rec.dptr,
591 sizeof(struct ctdb_ltdb_header));
592 rec.dsize -= sizeof(struct ctdb_ltdb_header);
595 * a special case, we are writing the same
596 * data that is there now
598 if (data.dsize == rec.dsize &&
599 memcmp(data.dptr,
600 rec.dptr + sizeof(struct ctdb_ltdb_header),
601 data.dsize) == 0) {
602 SAFE_FREE(rec.dptr);
603 talloc_free(tmp_ctx);
604 return NT_STATUS_OK;
607 SAFE_FREE(rec.dptr);
610 header.dmaster = get_my_vnn();
611 header.rsn++;
613 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
614 if (h->m_write == NULL) {
615 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
616 talloc_free(tmp_ctx);
617 return NT_STATUS_NO_MEMORY;
620 talloc_free(tmp_ctx);
621 return NT_STATUS_OK;
626 a record store inside a transaction
628 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
630 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
631 rec->private_data, struct db_ctdb_transaction_handle);
632 NTSTATUS status;
634 status = db_ctdb_transaction_store(h, rec->key, data);
635 return status;
639 a record delete inside a transaction
641 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
643 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
644 rec->private_data, struct db_ctdb_transaction_handle);
645 NTSTATUS status;
647 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
648 return status;
651 static void db_ctdb_fetch_db_seqnum_parser(
652 TDB_DATA key, struct ctdb_ltdb_header *header,
653 TDB_DATA data, void *private_data)
655 uint64_t *seqnum = (uint64_t *)private_data;
657 if (data.dsize != sizeof(uint64_t)) {
658 *seqnum = 0;
659 return;
661 memcpy(seqnum, data.dptr, sizeof(*seqnum));
665 * Fetch the db sequence number of a persistent db directly from the db.
667 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
668 uint64_t *seqnum)
670 NTSTATUS status;
671 TDB_DATA key;
673 if (seqnum == NULL) {
674 return NT_STATUS_INVALID_PARAMETER;
677 key = string_term_tdb_data(CTDB_DB_SEQNUM_KEY);
679 status = db_ctdb_ltdb_parse(
680 db, key, db_ctdb_fetch_db_seqnum_parser, seqnum);
682 if (NT_STATUS_IS_OK(status)) {
683 return NT_STATUS_OK;
685 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
686 *seqnum = 0;
687 return NT_STATUS_OK;
689 return status;
693 * Store the database sequence number inside a transaction.
695 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
696 uint64_t seqnum)
698 NTSTATUS status;
699 const char *keyname = CTDB_DB_SEQNUM_KEY;
700 TDB_DATA key;
701 TDB_DATA data;
703 key = string_term_tdb_data(keyname);
705 data.dptr = (uint8_t *)&seqnum;
706 data.dsize = sizeof(uint64_t);
708 status = db_ctdb_transaction_store(h, key, data);
710 return status;
714 commit a transaction
716 static int db_ctdb_transaction_commit(struct db_context *db)
718 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
719 struct db_ctdb_ctx);
720 NTSTATUS rets;
721 int status;
722 struct db_ctdb_transaction_handle *h = ctx->transaction;
723 uint64_t old_seqnum, new_seqnum;
724 int ret;
726 if (h == NULL) {
727 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
728 return -1;
731 if (h->nested_cancel) {
732 db->transaction_cancel(db);
733 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
734 return -1;
737 if (h->nesting != 0) {
738 h->nesting--;
739 DEBUG(5, (__location__ " transaction commit on db 0x%08x: nesting %d -> %d\n",
740 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
741 return 0;
744 if (h->m_write == NULL) {
746 * No changes were made, so don't change the seqnum,
747 * don't push to other node, just exit with success.
749 ret = 0;
750 goto done;
753 DEBUG(5,(__location__ " transaction commit on db 0x%08x\n", ctx->db_id));
756 * As the last db action before committing, bump the database sequence
757 * number. Note that this undoes all changes to the seqnum records
758 * performed under the transaction. This record is not meant to be
759 * modified by user interaction. It is for internal use only...
761 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
762 if (!NT_STATUS_IS_OK(rets)) {
763 DEBUG(1, (__location__ " failed to fetch the db sequence number "
764 "in transaction commit on db 0x%08x\n", ctx->db_id));
765 ret = -1;
766 goto done;
769 new_seqnum = old_seqnum + 1;
771 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
772 if (!NT_STATUS_IS_OK(rets)) {
773 DEBUG(1, (__location__ "failed to store the db sequence number "
774 " in transaction commit on db 0x%08x\n", ctx->db_id));
775 ret = -1;
776 goto done;
779 again:
780 /* tell ctdbd to commit to the other nodes */
781 rets = ctdbd_control_local(messaging_ctdbd_connection(),
782 CTDB_CONTROL_TRANS3_COMMIT,
783 h->ctx->db_id, 0,
784 db_ctdb_marshall_finish(h->m_write),
785 NULL, NULL, &status);
786 if (!NT_STATUS_IS_OK(rets) || status != 0) {
788 * The TRANS3_COMMIT control should only possibly fail when a
789 * recovery has been running concurrently. In any case, the db
790 * will be the same on all nodes, either the new copy or the
791 * old copy. This can be detected by comparing the old and new
792 * local sequence numbers.
794 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
795 if (!NT_STATUS_IS_OK(rets)) {
796 DEBUG(1, (__location__ " failed to refetch db sequence "
797 "number after failed TRANS3_COMMIT\n"));
798 ret = -1;
799 goto done;
802 if (new_seqnum == old_seqnum) {
803 /* Recovery prevented all our changes: retry. */
804 goto again;
806 if (new_seqnum != (old_seqnum + 1)) {
807 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
808 "old_seqnum[%lu] + (0 or 1) after failed "
809 "TRANS3_COMMIT - this should not happen!\n",
810 (unsigned long)new_seqnum,
811 (unsigned long)old_seqnum));
812 ret = -1;
813 goto done;
816 * Recovery propagated our changes to all nodes, completing
817 * our commit for us - succeed.
821 ret = 0;
823 done:
824 h->ctx->transaction = NULL;
825 talloc_free(h);
826 return ret;
831 cancel a transaction
833 static int db_ctdb_transaction_cancel(struct db_context *db)
835 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
836 struct db_ctdb_ctx);
837 struct db_ctdb_transaction_handle *h = ctx->transaction;
839 if (h == NULL) {
840 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
841 return -1;
844 if (h->nesting != 0) {
845 h->nesting--;
846 h->nested_cancel = true;
847 DEBUG(5, (__location__ " transaction cancel on db 0x%08x: nesting %d -> %d\n",
848 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
849 return 0;
852 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
854 ctx->transaction = NULL;
855 talloc_free(h);
856 return 0;
860 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
862 struct db_ctdb_rec *crec = talloc_get_type_abort(
863 rec->private_data, struct db_ctdb_rec);
865 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
870 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
871 static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
873 NTSTATUS status;
874 struct ctdb_control_schedule_for_deletion *dd;
875 TDB_DATA indata;
876 int cstatus;
877 struct db_ctdb_rec *crec = talloc_get_type_abort(
878 rec->private_data, struct db_ctdb_rec);
880 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
881 indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
882 if (indata.dptr == NULL) {
883 DEBUG(0, (__location__ " talloc failed!\n"));
884 return NT_STATUS_NO_MEMORY;
887 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
888 dd->db_id = crec->ctdb_ctx->db_id;
889 dd->hdr = crec->header;
890 dd->keylen = rec->key.dsize;
891 memcpy(dd->key, rec->key.dptr, rec->key.dsize);
893 status = ctdbd_control_local(messaging_ctdbd_connection(),
894 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
895 crec->ctdb_ctx->db_id,
896 CTDB_CTRL_FLAG_NOREPLY, /* flags */
897 indata,
898 NULL, /* outdata */
899 NULL, /* errmsg */
900 &cstatus);
901 talloc_free(indata.dptr);
903 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
904 DEBUG(1, (__location__ " Error sending local control "
905 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
906 nt_errstr(status), cstatus));
907 if (NT_STATUS_IS_OK(status)) {
908 status = NT_STATUS_UNSUCCESSFUL;
912 return status;
914 #endif
916 static NTSTATUS db_ctdb_delete(struct db_record *rec)
918 NTSTATUS status;
921 * We have to store the header with empty data. TODO: Fix the
922 * tdb-level cleanup
925 status = db_ctdb_store(rec, tdb_null, 0);
926 if (!NT_STATUS_IS_OK(status)) {
927 return status;
930 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
931 status = db_ctdb_send_schedule_for_deletion(rec);
932 #endif
934 return status;
937 static int db_ctdb_record_destr(struct db_record* data)
939 struct db_ctdb_rec *crec = talloc_get_type_abort(
940 data->private_data, struct db_ctdb_rec);
941 int threshold;
942 int ret;
943 struct timeval before;
944 double timediff;
946 DEBUG(10, (DEBUGLEVEL > 10
947 ? "Unlocking db %u key %s\n"
948 : "Unlocking db %u key %.20s\n",
949 (int)crec->ctdb_ctx->db_id,
950 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
951 data->key.dsize)));
953 before = timeval_current();
955 ret = tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key);
957 timediff = timeval_elapsed(&before);
958 timediff *= 1000; /* get us milliseconds */
960 if (timediff > crec->ctdb_ctx->warn_unlock_msecs) {
961 char *key;
962 key = hex_encode_talloc(talloc_tos(),
963 (unsigned char *)data->key.dptr,
964 data->key.dsize);
965 DEBUG(0, ("tdb_chainunlock on db %s, key %s took %f milliseconds\n",
966 tdb_name(crec->ctdb_ctx->wtdb->tdb), key,
967 timediff));
968 TALLOC_FREE(key);
971 if (ret != 0) {
972 DEBUG(0, ("tdb_chainunlock failed\n"));
973 return -1;
976 threshold = crec->ctdb_ctx->warn_locktime_msecs;
977 if (threshold != 0) {
978 timediff = timeval_elapsed(&crec->lock_time) * 1000;
979 if (timediff > threshold) {
980 const char *key;
982 key = hex_encode_talloc(data,
983 (unsigned char *)data->key.dptr,
984 data->key.dsize);
985 DEBUG(0, ("Held tdb lock on db %s, key %s "
986 "%f milliseconds\n",
987 tdb_name(crec->ctdb_ctx->wtdb->tdb),
988 key, timediff));
992 return 0;
996 * Check whether we have a valid local copy of the given record,
997 * either for reading or for writing.
999 static bool db_ctdb_can_use_local_hdr(const struct ctdb_ltdb_header *hdr,
1000 bool read_only)
1002 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1003 if (hdr->dmaster != get_my_vnn()) {
1004 /* If we're not dmaster, it must be r/o copy. */
1005 return read_only && (hdr->flags & CTDB_REC_RO_HAVE_READONLY);
1009 * If we want write access, no one may have r/o copies.
1011 return read_only || !(hdr->flags & CTDB_REC_RO_HAVE_DELEGATIONS);
1012 #else
1013 return (hdr->dmaster == get_my_vnn());
1014 #endif
1017 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data, bool read_only)
1019 if (ctdb_data.dptr == NULL) {
1020 return false;
1023 if (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
1024 return false;
1027 return db_ctdb_can_use_local_hdr(
1028 (struct ctdb_ltdb_header *)ctdb_data.dptr, read_only);
1031 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
1032 TALLOC_CTX *mem_ctx,
1033 TDB_DATA key,
1034 bool tryonly)
1036 struct db_record *result;
1037 struct db_ctdb_rec *crec;
1038 NTSTATUS status;
1039 TDB_DATA ctdb_data;
1040 int migrate_attempts;
1041 struct timeval migrate_start;
1042 struct timeval chainlock_start;
1043 struct timeval ctdb_start_time;
1044 double chainlock_time = 0;
1045 double ctdb_time = 0;
1046 int duration_msecs;
1047 int lockret;
1049 if (!(result = talloc(mem_ctx, struct db_record))) {
1050 DEBUG(0, ("talloc failed\n"));
1051 return NULL;
1054 if (!(crec = talloc_zero(result, struct db_ctdb_rec))) {
1055 DEBUG(0, ("talloc failed\n"));
1056 TALLOC_FREE(result);
1057 return NULL;
1060 result->db = ctx->db;
1061 result->private_data = (void *)crec;
1062 crec->ctdb_ctx = ctx;
1064 result->key.dsize = key.dsize;
1065 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
1066 key.dsize);
1067 if (result->key.dptr == NULL) {
1068 DEBUG(0, ("talloc failed\n"));
1069 TALLOC_FREE(result);
1070 return NULL;
1073 migrate_attempts = 0;
1074 GetTimeOfDay(&migrate_start);
1077 * Do a blocking lock on the record
1079 again:
1081 if (DEBUGLEVEL >= 10) {
1082 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1083 DEBUG(10, (DEBUGLEVEL > 10
1084 ? "Locking db %u key %s\n"
1085 : "Locking db %u key %.20s\n",
1086 (int)crec->ctdb_ctx->db_id, keystr));
1087 TALLOC_FREE(keystr);
1090 GetTimeOfDay(&chainlock_start);
1091 lockret = tryonly
1092 ? tdb_chainlock_nonblock(ctx->wtdb->tdb, key)
1093 : tdb_chainlock(ctx->wtdb->tdb, key);
1094 chainlock_time += timeval_elapsed(&chainlock_start);
1096 if (lockret != 0) {
1097 DEBUG(3, ("tdb_chainlock failed\n"));
1098 TALLOC_FREE(result);
1099 return NULL;
1102 result->store = db_ctdb_store;
1103 result->delete_rec = db_ctdb_delete;
1104 talloc_set_destructor(result, db_ctdb_record_destr);
1106 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
1109 * See if we have a valid record and we are the dmaster. If so, we can
1110 * take the shortcut and just return it.
1113 if (!db_ctdb_can_use_local_copy(ctdb_data, false)) {
1114 SAFE_FREE(ctdb_data.dptr);
1115 tdb_chainunlock(ctx->wtdb->tdb, key);
1116 talloc_set_destructor(result, NULL);
1118 if (tryonly && (migrate_attempts != 0)) {
1119 DEBUG(5, ("record migrated away again\n"));
1120 TALLOC_FREE(result);
1121 return NULL;
1124 migrate_attempts += 1;
1126 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1127 ctdb_data.dptr, ctdb_data.dptr ?
1128 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
1129 get_my_vnn(),
1130 ctdb_data.dptr ?
1131 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->flags : 0));
1133 GetTimeOfDay(&ctdb_start_time);
1134 status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id,
1135 key);
1136 ctdb_time += timeval_elapsed(&ctdb_start_time);
1138 if (!NT_STATUS_IS_OK(status)) {
1139 DEBUG(5, ("ctdb_migrate failed: %s\n",
1140 nt_errstr(status)));
1141 TALLOC_FREE(result);
1142 return NULL;
1144 /* now its migrated, try again */
1145 goto again;
1149 double duration;
1150 duration = timeval_elapsed(&migrate_start);
1153 * Convert the duration to milliseconds to avoid a
1154 * floating-point division of
1155 * lp_parm_int("migrate_duration") by 1000.
1157 duration_msecs = duration * 1000;
1160 if ((migrate_attempts > ctx->warn_migrate_attempts) ||
1161 (duration_msecs > ctx->warn_migrate_msecs)) {
1162 int chain = 0;
1164 if (tdb_get_flags(ctx->wtdb->tdb) & TDB_INCOMPATIBLE_HASH) {
1165 chain = tdb_jenkins_hash(&key) %
1166 tdb_hash_size(ctx->wtdb->tdb);
1169 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s, chain %d "
1170 "needed %d attempts, %d milliseconds, "
1171 "chainlock: %f ms, CTDB %f ms\n",
1172 tdb_name(ctx->wtdb->tdb),
1173 hex_encode_talloc(talloc_tos(),
1174 (unsigned char *)key.dptr,
1175 key.dsize),
1176 chain,
1177 migrate_attempts, duration_msecs,
1178 chainlock_time * 1000.0,
1179 ctdb_time * 1000.0));
1182 GetTimeOfDay(&crec->lock_time);
1184 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1186 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1187 result->value.dptr = NULL;
1189 if ((result->value.dsize != 0)
1190 && !(result->value.dptr = (uint8_t *)talloc_memdup(
1191 result, ctdb_data.dptr + sizeof(crec->header),
1192 result->value.dsize))) {
1193 DEBUG(0, ("talloc failed\n"));
1194 TALLOC_FREE(result);
1197 SAFE_FREE(ctdb_data.dptr);
1199 return result;
1202 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1203 TALLOC_CTX *mem_ctx,
1204 TDB_DATA key)
1206 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1207 struct db_ctdb_ctx);
1209 if (ctx->transaction != NULL) {
1210 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1213 if (db->persistent) {
1214 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1217 return fetch_locked_internal(ctx, mem_ctx, key, false);
1220 static struct db_record *db_ctdb_try_fetch_locked(struct db_context *db,
1221 TALLOC_CTX *mem_ctx,
1222 TDB_DATA key)
1224 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1225 struct db_ctdb_ctx);
1227 if (ctx->transaction != NULL) {
1228 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1231 if (db->persistent) {
1232 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1235 return fetch_locked_internal(ctx, mem_ctx, key, true);
1238 struct db_ctdb_parse_record_state {
1239 void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data);
1240 void *private_data;
1241 bool ask_for_readonly_copy;
1242 bool done;
1245 static void db_ctdb_parse_record_parser(
1246 TDB_DATA key, struct ctdb_ltdb_header *header,
1247 TDB_DATA data, void *private_data)
1249 struct db_ctdb_parse_record_state *state =
1250 (struct db_ctdb_parse_record_state *)private_data;
1251 state->parser(key, data, state->private_data);
1254 static void db_ctdb_parse_record_parser_nonpersistent(
1255 TDB_DATA key, struct ctdb_ltdb_header *header,
1256 TDB_DATA data, void *private_data)
1258 struct db_ctdb_parse_record_state *state =
1259 (struct db_ctdb_parse_record_state *)private_data;
1261 if (db_ctdb_can_use_local_hdr(header, true)) {
1262 state->parser(key, data, state->private_data);
1263 state->done = true;
1264 } else {
1266 * We found something in the db, so it seems that this record,
1267 * while not usable locally right now, is popular. Ask for a
1268 * R/O copy.
1270 state->ask_for_readonly_copy = true;
1274 static NTSTATUS db_ctdb_parse_record(struct db_context *db, TDB_DATA key,
1275 void (*parser)(TDB_DATA key,
1276 TDB_DATA data,
1277 void *private_data),
1278 void *private_data)
1280 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1281 db->private_data, struct db_ctdb_ctx);
1282 struct db_ctdb_parse_record_state state;
1283 NTSTATUS status;
1285 state.parser = parser;
1286 state.private_data = private_data;
1288 if (ctx->transaction != NULL) {
1289 struct db_ctdb_transaction_handle *h = ctx->transaction;
1290 bool found;
1293 * Transactions only happen for persistent db's.
1296 found = parse_newest_in_marshall_buffer(
1297 h->m_write, key, db_ctdb_parse_record_parser, &state);
1299 if (found) {
1300 return NT_STATUS_OK;
1304 if (db->persistent) {
1306 * Persistent db, but not found in the transaction buffer
1308 return db_ctdb_ltdb_parse(
1309 ctx, key, db_ctdb_parse_record_parser, &state);
1312 state.done = false;
1313 state.ask_for_readonly_copy = false;
1315 status = db_ctdb_ltdb_parse(
1316 ctx, key, db_ctdb_parse_record_parser_nonpersistent, &state);
1317 if (NT_STATUS_IS_OK(status) && state.done) {
1318 return NT_STATUS_OK;
1321 return ctdbd_parse(messaging_ctdbd_connection(), ctx->db_id, key,
1322 state.ask_for_readonly_copy, parser, private_data);
1325 struct traverse_state {
1326 struct db_context *db;
1327 int (*fn)(struct db_record *rec, void *private_data);
1328 void *private_data;
1329 int count;
1332 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1334 struct traverse_state *state = (struct traverse_state *)private_data;
1335 struct db_record *rec;
1336 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1337 /* we have to give them a locked record to prevent races */
1338 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1339 if (rec && rec->value.dsize > 0) {
1340 state->fn(rec, state->private_data);
1342 talloc_free(tmp_ctx);
1345 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1346 void *private_data)
1348 struct traverse_state *state = (struct traverse_state *)private_data;
1349 struct db_record *rec;
1350 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1351 int ret = 0;
1354 * Skip the __db_sequence_number__ key:
1355 * This is used for persistent transactions internally.
1357 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1358 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1360 goto done;
1363 /* we have to give them a locked record to prevent races */
1364 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1365 if (rec && rec->value.dsize > 0) {
1366 ret = state->fn(rec, state->private_data);
1369 done:
1370 talloc_free(tmp_ctx);
1371 return ret;
1374 /* wrapper to use traverse_persistent_callback with dbwrap */
1375 static int traverse_persistent_callback_dbwrap(struct db_record *rec, void* data)
1377 return traverse_persistent_callback(NULL, rec->key, rec->value, data);
1381 static int db_ctdb_traverse(struct db_context *db,
1382 int (*fn)(struct db_record *rec,
1383 void *private_data),
1384 void *private_data)
1386 NTSTATUS status;
1387 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1388 struct db_ctdb_ctx);
1389 struct traverse_state state;
1391 state.db = db;
1392 state.fn = fn;
1393 state.private_data = private_data;
1394 state.count = 0;
1396 if (db->persistent) {
1397 struct tdb_context *ltdb = ctx->wtdb->tdb;
1398 int ret;
1400 /* for persistent databases we don't need to do a ctdb traverse,
1401 we can do a faster local traverse */
1402 ret = tdb_traverse(ltdb, traverse_persistent_callback, &state);
1403 if (ret < 0) {
1404 return ret;
1406 if (ctx->transaction && ctx->transaction->m_write) {
1408 * we now have to handle keys not yet
1409 * present at transaction start
1411 struct db_context *newkeys = db_open_rbt(talloc_tos());
1412 struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
1413 struct ctdb_rec_data *rec=NULL;
1414 int i;
1415 int count = 0;
1417 if (newkeys == NULL) {
1418 return -1;
1421 for (i=0; i<mbuf->count; i++) {
1422 TDB_DATA key;
1423 rec = db_ctdb_marshall_loop_next_key(
1424 mbuf, rec, &key);
1425 SMB_ASSERT(rec != NULL);
1427 if (!tdb_exists(ltdb, key)) {
1428 dbwrap_store(newkeys, key, tdb_null, 0);
1431 status = dbwrap_traverse(newkeys,
1432 traverse_persistent_callback_dbwrap,
1433 &state,
1434 &count);
1435 talloc_free(newkeys);
1436 if (!NT_STATUS_IS_OK(status)) {
1437 return -1;
1439 ret += count;
1441 return ret;
1444 status = ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1445 if (!NT_STATUS_IS_OK(status)) {
1446 return -1;
1448 return state.count;
1451 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1453 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1456 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1458 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1461 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1463 struct traverse_state *state = (struct traverse_state *)private_data;
1464 struct db_record rec;
1466 ZERO_STRUCT(rec);
1467 rec.db = state->db;
1468 rec.key = key;
1469 rec.value = data;
1470 rec.store = db_ctdb_store_deny;
1471 rec.delete_rec = db_ctdb_delete_deny;
1472 rec.private_data = NULL;
1473 state->fn(&rec, state->private_data);
1474 state->count++;
1477 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1478 void *private_data)
1480 struct traverse_state *state = (struct traverse_state *)private_data;
1481 struct db_record rec;
1484 * Skip the __db_sequence_number__ key:
1485 * This is used for persistent transactions internally.
1487 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1488 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1490 return 0;
1493 ZERO_STRUCT(rec);
1494 rec.db = state->db;
1495 rec.key = kbuf;
1496 rec.value = dbuf;
1497 rec.store = db_ctdb_store_deny;
1498 rec.delete_rec = db_ctdb_delete_deny;
1499 rec.private_data = NULL;
1501 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1502 /* a deleted record */
1503 return 0;
1505 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1506 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1508 state->count++;
1509 return state->fn(&rec, state->private_data);
1512 static int db_ctdb_traverse_read(struct db_context *db,
1513 int (*fn)(struct db_record *rec,
1514 void *private_data),
1515 void *private_data)
1517 NTSTATUS status;
1518 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1519 struct db_ctdb_ctx);
1520 struct traverse_state state;
1522 state.db = db;
1523 state.fn = fn;
1524 state.private_data = private_data;
1525 state.count = 0;
1527 if (db->persistent) {
1528 /* for persistent databases we don't need to do a ctdb traverse,
1529 we can do a faster local traverse */
1530 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1533 status = ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1534 if (!NT_STATUS_IS_OK(status)) {
1535 return -1;
1537 return state.count;
1540 static int db_ctdb_get_seqnum(struct db_context *db)
1542 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1543 struct db_ctdb_ctx);
1544 return tdb_get_seqnum(ctx->wtdb->tdb);
1547 static void db_ctdb_id(struct db_context *db, const uint8_t **id,
1548 size_t *idlen)
1550 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1551 db->private_data, struct db_ctdb_ctx);
1553 *id = (uint8_t *)&ctx->db_id;
1554 *idlen = sizeof(ctx->db_id);
1557 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1558 const char *name,
1559 int hash_size, int tdb_flags,
1560 int open_flags, mode_t mode,
1561 enum dbwrap_lock_order lock_order,
1562 uint64_t dbwrap_flags)
1564 struct db_context *result;
1565 struct db_ctdb_ctx *db_ctdb;
1566 char *db_path;
1567 struct ctdbd_connection *conn;
1568 struct loadparm_context *lp_ctx;
1569 struct ctdb_db_priority prio;
1570 NTSTATUS status;
1571 int cstatus;
1573 if (!lp_clustering()) {
1574 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1575 return NULL;
1578 if (!(result = talloc_zero(mem_ctx, struct db_context))) {
1579 DEBUG(0, ("talloc failed\n"));
1580 TALLOC_FREE(result);
1581 return NULL;
1584 if (!(db_ctdb = talloc(result, struct db_ctdb_ctx))) {
1585 DEBUG(0, ("talloc failed\n"));
1586 TALLOC_FREE(result);
1587 return NULL;
1590 result->name = talloc_strdup(result, name);
1591 if (result->name == NULL) {
1592 DEBUG(0, ("talloc failed\n"));
1593 TALLOC_FREE(result);
1594 return NULL;
1597 db_ctdb->transaction = NULL;
1598 db_ctdb->db = result;
1600 conn = messaging_ctdbd_connection();
1601 if (conn == NULL) {
1602 DEBUG(1, ("Could not connect to ctdb\n"));
1603 TALLOC_FREE(result);
1604 return NULL;
1607 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1608 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1609 TALLOC_FREE(result);
1610 return NULL;
1613 db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1615 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1616 result->lock_order = lock_order;
1618 /* only pass through specific flags */
1619 tdb_flags &= TDB_SEQNUM|TDB_VOLATILE;
1621 /* honor permissions if user has specified O_CREAT */
1622 if (open_flags & O_CREAT) {
1623 chmod(db_path, mode);
1626 prio.db_id = db_ctdb->db_id;
1627 prio.priority = lock_order;
1629 status = ctdbd_control_local(
1630 conn, CTDB_CONTROL_SET_DB_PRIORITY, 0, 0,
1631 make_tdb_data((uint8_t *)&prio, sizeof(prio)),
1632 NULL, NULL, &cstatus);
1634 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1635 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1636 nt_errstr(status), cstatus));
1637 TALLOC_FREE(result);
1638 return NULL;
1641 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1642 if (!result->persistent &&
1643 (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS))
1645 TDB_DATA indata;
1647 indata = make_tdb_data((uint8_t *)&db_ctdb->db_id,
1648 sizeof(db_ctdb->db_id));
1650 status = ctdbd_control_local(
1651 conn, CTDB_CONTROL_SET_DB_READONLY, 0, 0, indata,
1652 NULL, NULL, &cstatus);
1653 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1654 DEBUG(1, ("CTDB_CONTROL_SET_DB_READONLY failed: "
1655 "%s, %d\n", nt_errstr(status), cstatus));
1656 TALLOC_FREE(result);
1657 return NULL;
1660 #endif
1662 lp_ctx = loadparm_init_s3(db_path, loadparm_s3_helpers());
1664 if (hash_size == 0) {
1665 hash_size = lpcfg_tdb_hash_size(lp_ctx, db_path);
1668 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size,
1669 lpcfg_tdb_flags(lp_ctx, tdb_flags),
1670 O_RDWR, 0);
1671 talloc_unlink(db_path, lp_ctx);
1672 if (db_ctdb->wtdb == NULL) {
1673 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1674 TALLOC_FREE(result);
1675 return NULL;
1677 talloc_free(db_path);
1679 if (result->persistent) {
1680 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1681 ctdb_conn_msg_ctx(conn));
1682 if (db_ctdb->lock_ctx == NULL) {
1683 DEBUG(0, ("g_lock_ctx_init failed\n"));
1684 TALLOC_FREE(result);
1685 return NULL;
1689 db_ctdb->warn_unlock_msecs = lp_parm_int(-1, "ctdb",
1690 "unlock_warn_threshold", 5);
1691 db_ctdb->warn_migrate_attempts = lp_parm_int(-1, "ctdb",
1692 "migrate_attempts", 10);
1693 db_ctdb->warn_migrate_msecs = lp_parm_int(-1, "ctdb",
1694 "migrate_duration", 5000);
1695 db_ctdb->warn_locktime_msecs = lp_ctdb_locktime_warn_threshold();
1697 result->private_data = (void *)db_ctdb;
1698 result->fetch_locked = db_ctdb_fetch_locked;
1699 result->try_fetch_locked = db_ctdb_try_fetch_locked;
1700 result->parse_record = db_ctdb_parse_record;
1701 result->traverse = db_ctdb_traverse;
1702 result->traverse_read = db_ctdb_traverse_read;
1703 result->get_seqnum = db_ctdb_get_seqnum;
1704 result->transaction_start = db_ctdb_transaction_start;
1705 result->transaction_commit = db_ctdb_transaction_commit;
1706 result->transaction_cancel = db_ctdb_transaction_cancel;
1707 result->id = db_ctdb_id;
1708 result->stored_callback = NULL;
1710 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1711 name, db_ctdb->db_id));
1713 return result;