s3:param: move lp_ctdbd_socket() to ctdbd_conn.c
[Samba.git] / source3 / lib / dbwrap / dbwrap_ctdb.c
blob42c9a684261a896428a898416ea1131358d6b344
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"
30 #ifdef CLUSTER_SUPPORT
33 * It is not possible to include ctdb.h and tdb_compat.h (included via
34 * some other include above) without warnings. This fixes those
35 * warnings.
38 #ifdef typesafe_cb
39 #undef typesafe_cb
40 #endif
42 #ifdef typesafe_cb_preargs
43 #undef typesafe_cb_preargs
44 #endif
46 #ifdef typesafe_cb_postargs
47 #undef typesafe_cb_postargs
48 #endif
50 #include "ctdb.h"
51 #include "ctdb_private.h"
52 #include "ctdbd_conn.h"
53 #include "dbwrap/dbwrap.h"
54 #include "dbwrap/dbwrap_private.h"
55 #include "dbwrap/dbwrap_ctdb.h"
56 #include "g_lock.h"
57 #include "messages.h"
59 struct db_ctdb_transaction_handle {
60 struct db_ctdb_ctx *ctx;
62 * we store the writes done under a transaction:
64 struct ctdb_marshall_buffer *m_write;
65 uint32_t nesting;
66 bool nested_cancel;
67 char *lock_name;
70 struct db_ctdb_ctx {
71 struct db_context *db;
72 struct tdb_wrap *wtdb;
73 uint32_t db_id;
74 struct db_ctdb_transaction_handle *transaction;
75 struct g_lock_ctx *lock_ctx;
77 /* thresholds for warning messages */
78 int warn_unlock_msecs;
79 int warn_migrate_msecs;
80 int warn_migrate_attempts;
81 int warn_locktime_msecs;
84 struct db_ctdb_rec {
85 struct db_ctdb_ctx *ctdb_ctx;
86 struct ctdb_ltdb_header header;
87 struct timeval lock_time;
90 static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
92 enum TDB_ERROR tret = tdb_error(tdb);
94 return map_nt_error_from_tdb(tret);
97 struct db_ctdb_ltdb_parse_state {
98 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
99 TDB_DATA data, void *private_data);
100 void *private_data;
103 static int db_ctdb_ltdb_parser(TDB_DATA key, TDB_DATA data,
104 void *private_data)
106 struct db_ctdb_ltdb_parse_state *state =
107 (struct db_ctdb_ltdb_parse_state *)private_data;
109 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
110 return -1;
113 state->parser(
114 key, (struct ctdb_ltdb_header *)data.dptr,
115 make_tdb_data(data.dptr + sizeof(struct ctdb_ltdb_header),
116 data.dsize - sizeof(struct ctdb_ltdb_header)),
117 state->private_data);
118 return 0;
121 static NTSTATUS db_ctdb_ltdb_parse(
122 struct db_ctdb_ctx *db, TDB_DATA key,
123 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
124 TDB_DATA data, void *private_data),
125 void *private_data)
127 struct db_ctdb_ltdb_parse_state state;
128 int ret;
130 state.parser = parser;
131 state.private_data = private_data;
133 ret = tdb_parse_record(db->wtdb->tdb, key, db_ctdb_ltdb_parser,
134 &state);
135 if (ret == -1) {
136 return NT_STATUS_NOT_FOUND;
138 return NT_STATUS_OK;
142 * Store a record together with the ctdb record header
143 * in the local copy of the database.
145 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
146 TDB_DATA key,
147 struct ctdb_ltdb_header *header,
148 TDB_DATA data)
150 TDB_DATA rec;
151 int ret;
153 rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
154 rec.dptr = (uint8_t *)talloc_size(talloc_tos(), rec.dsize);
156 if (rec.dptr == NULL) {
157 return NT_STATUS_NO_MEMORY;
160 memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
161 memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
163 ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
165 talloc_free(rec.dptr);
167 return (ret == 0) ? NT_STATUS_OK
168 : tdb_error_to_ntstatus(db->wtdb->tdb);
173 form a ctdb_rec_data record from a key/data pair
175 static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
176 TDB_DATA key,
177 struct ctdb_ltdb_header *header,
178 TDB_DATA data)
180 size_t length;
181 struct ctdb_rec_data *d;
183 length = offsetof(struct ctdb_rec_data, data) + key.dsize +
184 data.dsize + sizeof(*header);
185 d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
186 if (d == NULL) {
187 return NULL;
189 d->length = length;
190 d->reqid = reqid;
191 d->keylen = key.dsize;
192 memcpy(&d->data[0], key.dptr, key.dsize);
194 d->datalen = data.dsize + sizeof(*header);
195 memcpy(&d->data[key.dsize], header, sizeof(*header));
196 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
197 return d;
201 /* helper function for marshalling multiple records */
202 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
203 struct ctdb_marshall_buffer *m,
204 uint64_t db_id,
205 uint32_t reqid,
206 TDB_DATA key,
207 struct ctdb_ltdb_header *header,
208 TDB_DATA data)
210 struct ctdb_rec_data *r;
211 size_t m_size, r_size;
212 struct ctdb_marshall_buffer *m2 = NULL;
214 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
215 if (r == NULL) {
216 talloc_free(m);
217 return NULL;
220 if (m == NULL) {
221 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
222 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
223 if (m == NULL) {
224 goto done;
226 m->db_id = db_id;
229 m_size = talloc_get_size(m);
230 r_size = talloc_get_size(r);
232 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
233 mem_ctx, m, m_size + r_size);
234 if (m2 == NULL) {
235 talloc_free(m);
236 goto done;
239 memcpy(m_size + (uint8_t *)m2, r, r_size);
241 m2->count++;
243 done:
244 talloc_free(r);
245 return m2;
248 /* we've finished marshalling, return a data blob with the marshalled records */
249 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
251 TDB_DATA data;
252 data.dptr = (uint8_t *)m;
253 data.dsize = talloc_get_size(m);
254 return data;
258 loop over a marshalling buffer
260 - pass r==NULL to start
261 - loop the number of times indicated by m->count
263 static struct ctdb_rec_data *db_ctdb_marshall_loop_next_key(
264 struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r, TDB_DATA *key)
266 if (r == NULL) {
267 r = (struct ctdb_rec_data *)&m->data[0];
268 } else {
269 r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
272 key->dptr = &r->data[0];
273 key->dsize = r->keylen;
274 return r;
277 static bool db_ctdb_marshall_buf_parse(
278 struct ctdb_rec_data *r, uint32_t *reqid,
279 struct ctdb_ltdb_header **header, TDB_DATA *data)
281 if (r->datalen < sizeof(struct ctdb_ltdb_header)) {
282 return false;
285 *reqid = r->reqid;
287 data->dptr = &r->data[r->keylen] + sizeof(struct ctdb_ltdb_header);
288 data->dsize = r->datalen - sizeof(struct ctdb_ltdb_header);
290 *header = (struct ctdb_ltdb_header *)&r->data[r->keylen];
292 return true;
296 * CTDB transaction destructor
298 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
300 NTSTATUS status;
302 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
303 if (!NT_STATUS_IS_OK(status)) {
304 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h->lock_name,
305 nt_errstr(status)));
306 return -1;
308 return 0;
312 * CTDB dbwrap API: transaction_start function
313 * starts a transaction on a persistent database
315 static int db_ctdb_transaction_start(struct db_context *db)
317 struct db_ctdb_transaction_handle *h;
318 NTSTATUS status;
319 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
320 struct db_ctdb_ctx);
322 if (!db->persistent) {
323 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
324 ctx->db_id));
325 return -1;
328 if (ctx->transaction) {
329 ctx->transaction->nesting++;
330 DEBUG(5, (__location__ " transaction start on db 0x%08x: nesting %d -> %d\n",
331 ctx->db_id, ctx->transaction->nesting - 1, ctx->transaction->nesting));
332 return 0;
335 h = talloc_zero(db, struct db_ctdb_transaction_handle);
336 if (h == NULL) {
337 DEBUG(0,(__location__ " oom for transaction handle\n"));
338 return -1;
341 h->ctx = ctx;
343 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
344 (unsigned int)ctx->db_id);
345 if (h->lock_name == NULL) {
346 DEBUG(0, ("talloc_asprintf failed\n"));
347 TALLOC_FREE(h);
348 return -1;
352 * Wait a day, i.e. forever...
354 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
355 timeval_set(86400, 0));
356 if (!NT_STATUS_IS_OK(status)) {
357 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
358 TALLOC_FREE(h);
359 return -1;
362 talloc_set_destructor(h, db_ctdb_transaction_destructor);
364 ctx->transaction = h;
366 DEBUG(5,(__location__ " transaction started on db 0x%08x\n", ctx->db_id));
368 return 0;
371 static bool parse_newest_in_marshall_buffer(
372 struct ctdb_marshall_buffer *buf, TDB_DATA key,
373 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
374 TDB_DATA data, void *private_data),
375 void *private_data)
377 struct ctdb_rec_data *rec = NULL;
378 struct ctdb_ltdb_header *h = NULL;
379 TDB_DATA data;
380 int i;
382 if (buf == NULL) {
383 return false;
387 * Walk the list of records written during this
388 * transaction. If we want to read one we have already
389 * written, return the last written sample. Thus we do not do
390 * a "break;" for the first hit, this record might have been
391 * overwritten later.
394 for (i=0; i<buf->count; i++) {
395 TDB_DATA tkey;
396 uint32_t reqid;
398 rec = db_ctdb_marshall_loop_next_key(buf, rec, &tkey);
399 if (rec == NULL) {
400 return false;
403 if (!tdb_data_equal(key, tkey)) {
404 continue;
407 if (!db_ctdb_marshall_buf_parse(rec, &reqid, &h, &data)) {
408 return false;
412 if (h == NULL) {
413 return false;
416 parser(key, h, data, private_data);
418 return true;
421 struct pull_newest_from_marshall_buffer_state {
422 struct ctdb_ltdb_header *pheader;
423 TALLOC_CTX *mem_ctx;
424 TDB_DATA *pdata;
427 static void pull_newest_from_marshall_buffer_parser(
428 TDB_DATA key, struct ctdb_ltdb_header *header,
429 TDB_DATA data, void *private_data)
431 struct pull_newest_from_marshall_buffer_state *state =
432 (struct pull_newest_from_marshall_buffer_state *)private_data;
434 if (state->pheader != NULL) {
435 memcpy(state->pheader, header, sizeof(*state->pheader));
437 if (state->pdata != NULL) {
438 state->pdata->dsize = data.dsize;
439 state->pdata->dptr = (uint8_t *)talloc_memdup(
440 state->mem_ctx, data.dptr, data.dsize);
444 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
445 TDB_DATA key,
446 struct ctdb_ltdb_header *pheader,
447 TALLOC_CTX *mem_ctx,
448 TDB_DATA *pdata)
450 struct pull_newest_from_marshall_buffer_state state;
452 state.pheader = pheader;
453 state.mem_ctx = mem_ctx;
454 state.pdata = pdata;
456 if (!parse_newest_in_marshall_buffer(
457 buf, key, pull_newest_from_marshall_buffer_parser,
458 &state)) {
459 return false;
461 if ((pdata != NULL) && (pdata->dsize != 0) && (pdata->dptr == NULL)) {
462 /* ENOMEM */
463 return false;
465 return true;
468 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
469 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
471 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
472 TALLOC_CTX *mem_ctx,
473 TDB_DATA key)
475 struct db_record *result;
476 TDB_DATA ctdb_data;
478 if (!(result = talloc(mem_ctx, struct db_record))) {
479 DEBUG(0, ("talloc failed\n"));
480 return NULL;
483 result->db = ctx->db;
484 result->private_data = ctx->transaction;
486 result->key.dsize = key.dsize;
487 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
488 key.dsize);
489 if (result->key.dptr == NULL) {
490 DEBUG(0, ("talloc failed\n"));
491 TALLOC_FREE(result);
492 return NULL;
495 result->store = db_ctdb_store_transaction;
496 result->delete_rec = db_ctdb_delete_transaction;
498 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
499 NULL, result, &result->value)) {
500 return result;
503 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
504 if (ctdb_data.dptr == NULL) {
505 /* create the record */
506 result->value = tdb_null;
507 return result;
510 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
511 result->value.dptr = NULL;
513 if ((result->value.dsize != 0)
514 && !(result->value.dptr = (uint8_t *)talloc_memdup(
515 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
516 result->value.dsize))) {
517 DEBUG(0, ("talloc failed\n"));
518 TALLOC_FREE(result);
521 SAFE_FREE(ctdb_data.dptr);
523 return result;
526 static int db_ctdb_record_destructor(struct db_record **recp)
528 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
529 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
530 rec->private_data, struct db_ctdb_transaction_handle);
531 int ret = h->ctx->db->transaction_commit(h->ctx->db);
532 if (ret != 0) {
533 DEBUG(0,(__location__ " transaction_commit failed\n"));
535 return 0;
539 auto-create a transaction for persistent databases
541 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
542 TALLOC_CTX *mem_ctx,
543 TDB_DATA key)
545 int res;
546 struct db_record *rec, **recp;
548 res = db_ctdb_transaction_start(ctx->db);
549 if (res == -1) {
550 return NULL;
553 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
554 if (rec == NULL) {
555 ctx->db->transaction_cancel(ctx->db);
556 return NULL;
559 /* destroy this transaction when we release the lock */
560 recp = talloc(rec, struct db_record *);
561 if (recp == NULL) {
562 ctx->db->transaction_cancel(ctx->db);
563 talloc_free(rec);
564 return NULL;
566 *recp = rec;
567 talloc_set_destructor(recp, db_ctdb_record_destructor);
568 return rec;
573 stores a record inside a transaction
575 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
576 TDB_DATA key, TDB_DATA data)
578 TALLOC_CTX *tmp_ctx = talloc_new(h);
579 TDB_DATA rec;
580 struct ctdb_ltdb_header header;
582 ZERO_STRUCT(header);
584 /* we need the header so we can update the RSN */
586 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
587 NULL, NULL)) {
589 rec = tdb_fetch_compat(h->ctx->wtdb->tdb, key);
591 if (rec.dptr != NULL) {
592 memcpy(&header, rec.dptr,
593 sizeof(struct ctdb_ltdb_header));
594 rec.dsize -= sizeof(struct ctdb_ltdb_header);
597 * a special case, we are writing the same
598 * data that is there now
600 if (data.dsize == rec.dsize &&
601 memcmp(data.dptr,
602 rec.dptr + sizeof(struct ctdb_ltdb_header),
603 data.dsize) == 0) {
604 SAFE_FREE(rec.dptr);
605 talloc_free(tmp_ctx);
606 return NT_STATUS_OK;
609 SAFE_FREE(rec.dptr);
612 header.dmaster = get_my_vnn();
613 header.rsn++;
615 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
616 if (h->m_write == NULL) {
617 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
618 talloc_free(tmp_ctx);
619 return NT_STATUS_NO_MEMORY;
622 talloc_free(tmp_ctx);
623 return NT_STATUS_OK;
628 a record store inside a transaction
630 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
632 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
633 rec->private_data, struct db_ctdb_transaction_handle);
634 NTSTATUS status;
636 status = db_ctdb_transaction_store(h, rec->key, data);
637 return status;
641 a record delete inside a transaction
643 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
645 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
646 rec->private_data, struct db_ctdb_transaction_handle);
647 NTSTATUS status;
649 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
650 return status;
653 static void db_ctdb_fetch_db_seqnum_parser(
654 TDB_DATA key, struct ctdb_ltdb_header *header,
655 TDB_DATA data, void *private_data)
657 uint64_t *seqnum = (uint64_t *)private_data;
659 if (data.dsize != sizeof(uint64_t)) {
660 *seqnum = 0;
661 return;
663 memcpy(seqnum, data.dptr, sizeof(*seqnum));
667 * Fetch the db sequence number of a persistent db directly from the db.
669 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
670 uint64_t *seqnum)
672 NTSTATUS status;
673 TDB_DATA key;
675 if (seqnum == NULL) {
676 return NT_STATUS_INVALID_PARAMETER;
679 key = string_term_tdb_data(CTDB_DB_SEQNUM_KEY);
681 status = db_ctdb_ltdb_parse(
682 db, key, db_ctdb_fetch_db_seqnum_parser, seqnum);
684 if (NT_STATUS_IS_OK(status)) {
685 return NT_STATUS_OK;
687 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
688 *seqnum = 0;
689 return NT_STATUS_OK;
691 return status;
695 * Store the database sequence number inside a transaction.
697 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
698 uint64_t seqnum)
700 NTSTATUS status;
701 const char *keyname = CTDB_DB_SEQNUM_KEY;
702 TDB_DATA key;
703 TDB_DATA data;
705 key = string_term_tdb_data(keyname);
707 data.dptr = (uint8_t *)&seqnum;
708 data.dsize = sizeof(uint64_t);
710 status = db_ctdb_transaction_store(h, key, data);
712 return status;
716 commit a transaction
718 static int db_ctdb_transaction_commit(struct db_context *db)
720 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
721 struct db_ctdb_ctx);
722 NTSTATUS rets;
723 int status;
724 struct db_ctdb_transaction_handle *h = ctx->transaction;
725 uint64_t old_seqnum, new_seqnum;
726 int ret;
728 if (h == NULL) {
729 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
730 return -1;
733 if (h->nested_cancel) {
734 db->transaction_cancel(db);
735 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
736 return -1;
739 if (h->nesting != 0) {
740 h->nesting--;
741 DEBUG(5, (__location__ " transaction commit on db 0x%08x: nesting %d -> %d\n",
742 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
743 return 0;
746 if (h->m_write == NULL) {
748 * No changes were made, so don't change the seqnum,
749 * don't push to other node, just exit with success.
751 ret = 0;
752 goto done;
755 DEBUG(5,(__location__ " transaction commit on db 0x%08x\n", ctx->db_id));
758 * As the last db action before committing, bump the database sequence
759 * number. Note that this undoes all changes to the seqnum records
760 * performed under the transaction. This record is not meant to be
761 * modified by user interaction. It is for internal use only...
763 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
764 if (!NT_STATUS_IS_OK(rets)) {
765 DEBUG(1, (__location__ " failed to fetch the db sequence number "
766 "in transaction commit on db 0x%08x\n", ctx->db_id));
767 ret = -1;
768 goto done;
771 new_seqnum = old_seqnum + 1;
773 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
774 if (!NT_STATUS_IS_OK(rets)) {
775 DEBUG(1, (__location__ "failed to store the db sequence number "
776 " in transaction commit on db 0x%08x\n", ctx->db_id));
777 ret = -1;
778 goto done;
781 again:
782 /* tell ctdbd to commit to the other nodes */
783 rets = ctdbd_control_local(messaging_ctdbd_connection(),
784 CTDB_CONTROL_TRANS3_COMMIT,
785 h->ctx->db_id, 0,
786 db_ctdb_marshall_finish(h->m_write),
787 NULL, NULL, &status);
788 if (!NT_STATUS_IS_OK(rets) || status != 0) {
790 * The TRANS3_COMMIT control should only possibly fail when a
791 * recovery has been running concurrently. In any case, the db
792 * will be the same on all nodes, either the new copy or the
793 * old copy. This can be detected by comparing the old and new
794 * local sequence numbers.
796 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
797 if (!NT_STATUS_IS_OK(rets)) {
798 DEBUG(1, (__location__ " failed to refetch db sequence "
799 "number after failed TRANS3_COMMIT\n"));
800 ret = -1;
801 goto done;
804 if (new_seqnum == old_seqnum) {
805 /* Recovery prevented all our changes: retry. */
806 goto again;
808 if (new_seqnum != (old_seqnum + 1)) {
809 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
810 "old_seqnum[%lu] + (0 or 1) after failed "
811 "TRANS3_COMMIT - this should not happen!\n",
812 (unsigned long)new_seqnum,
813 (unsigned long)old_seqnum));
814 ret = -1;
815 goto done;
818 * Recovery propagated our changes to all nodes, completing
819 * our commit for us - succeed.
823 ret = 0;
825 done:
826 h->ctx->transaction = NULL;
827 talloc_free(h);
828 return ret;
833 cancel a transaction
835 static int db_ctdb_transaction_cancel(struct db_context *db)
837 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
838 struct db_ctdb_ctx);
839 struct db_ctdb_transaction_handle *h = ctx->transaction;
841 if (h == NULL) {
842 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
843 return -1;
846 if (h->nesting != 0) {
847 h->nesting--;
848 h->nested_cancel = true;
849 DEBUG(5, (__location__ " transaction cancel on db 0x%08x: nesting %d -> %d\n",
850 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
851 return 0;
854 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
856 ctx->transaction = NULL;
857 talloc_free(h);
858 return 0;
862 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
864 struct db_ctdb_rec *crec = talloc_get_type_abort(
865 rec->private_data, struct db_ctdb_rec);
867 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
872 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
873 static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
875 NTSTATUS status;
876 struct ctdb_control_schedule_for_deletion *dd;
877 TDB_DATA indata;
878 int cstatus;
879 struct db_ctdb_rec *crec = talloc_get_type_abort(
880 rec->private_data, struct db_ctdb_rec);
882 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
883 indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
884 if (indata.dptr == NULL) {
885 DEBUG(0, (__location__ " talloc failed!\n"));
886 return NT_STATUS_NO_MEMORY;
889 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
890 dd->db_id = crec->ctdb_ctx->db_id;
891 dd->hdr = crec->header;
892 dd->keylen = rec->key.dsize;
893 memcpy(dd->key, rec->key.dptr, rec->key.dsize);
895 status = ctdbd_control_local(messaging_ctdbd_connection(),
896 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
897 crec->ctdb_ctx->db_id,
898 CTDB_CTRL_FLAG_NOREPLY, /* flags */
899 indata,
900 NULL, /* outdata */
901 NULL, /* errmsg */
902 &cstatus);
903 talloc_free(indata.dptr);
905 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
906 DEBUG(1, (__location__ " Error sending local control "
907 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
908 nt_errstr(status), cstatus));
909 if (NT_STATUS_IS_OK(status)) {
910 status = NT_STATUS_UNSUCCESSFUL;
914 return status;
916 #endif
918 static NTSTATUS db_ctdb_delete(struct db_record *rec)
920 NTSTATUS status;
923 * We have to store the header with empty data. TODO: Fix the
924 * tdb-level cleanup
927 status = db_ctdb_store(rec, tdb_null, 0);
928 if (!NT_STATUS_IS_OK(status)) {
929 return status;
932 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
933 status = db_ctdb_send_schedule_for_deletion(rec);
934 #endif
936 return status;
939 static int db_ctdb_record_destr(struct db_record* data)
941 struct db_ctdb_rec *crec = talloc_get_type_abort(
942 data->private_data, struct db_ctdb_rec);
943 int threshold;
944 int ret;
945 struct timeval before;
946 double timediff;
948 DEBUG(10, (DEBUGLEVEL > 10
949 ? "Unlocking db %u key %s\n"
950 : "Unlocking db %u key %.20s\n",
951 (int)crec->ctdb_ctx->db_id,
952 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
953 data->key.dsize)));
955 before = timeval_current();
957 ret = tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key);
959 timediff = timeval_elapsed(&before);
960 timediff *= 1000; /* get us milliseconds */
962 if (timediff > crec->ctdb_ctx->warn_unlock_msecs) {
963 char *key;
964 key = hex_encode_talloc(talloc_tos(),
965 (unsigned char *)data->key.dptr,
966 data->key.dsize);
967 DEBUG(0, ("tdb_chainunlock on db %s, key %s took %f milliseconds\n",
968 tdb_name(crec->ctdb_ctx->wtdb->tdb), key,
969 timediff));
970 TALLOC_FREE(key);
973 if (ret != 0) {
974 DEBUG(0, ("tdb_chainunlock failed\n"));
975 return -1;
978 threshold = crec->ctdb_ctx->warn_locktime_msecs;
979 if (threshold != 0) {
980 timediff = timeval_elapsed(&crec->lock_time) * 1000;
981 if (timediff > threshold) {
982 const char *key;
984 key = hex_encode_talloc(data,
985 (unsigned char *)data->key.dptr,
986 data->key.dsize);
987 DEBUG(0, ("Held tdb lock on db %s, key %s "
988 "%f milliseconds\n",
989 tdb_name(crec->ctdb_ctx->wtdb->tdb),
990 key, timediff));
994 return 0;
998 * Check whether we have a valid local copy of the given record,
999 * either for reading or for writing.
1001 static bool db_ctdb_can_use_local_hdr(const struct ctdb_ltdb_header *hdr,
1002 bool read_only)
1004 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1005 if (hdr->dmaster != get_my_vnn()) {
1006 /* If we're not dmaster, it must be r/o copy. */
1007 return read_only && (hdr->flags & CTDB_REC_RO_HAVE_READONLY);
1011 * If we want write access, no one may have r/o copies.
1013 return read_only || !(hdr->flags & CTDB_REC_RO_HAVE_DELEGATIONS);
1014 #else
1015 return (hdr->dmaster == get_my_vnn());
1016 #endif
1019 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data, bool read_only)
1021 if (ctdb_data.dptr == NULL) {
1022 return false;
1025 if (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
1026 return false;
1029 return db_ctdb_can_use_local_hdr(
1030 (struct ctdb_ltdb_header *)ctdb_data.dptr, read_only);
1033 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
1034 TALLOC_CTX *mem_ctx,
1035 TDB_DATA key,
1036 bool tryonly)
1038 struct db_record *result;
1039 struct db_ctdb_rec *crec;
1040 NTSTATUS status;
1041 TDB_DATA ctdb_data;
1042 int migrate_attempts;
1043 struct timeval migrate_start;
1044 struct timeval chainlock_start;
1045 struct timeval ctdb_start_time;
1046 double chainlock_time = 0;
1047 double ctdb_time = 0;
1048 int duration_msecs;
1049 int lockret;
1051 if (!(result = talloc(mem_ctx, struct db_record))) {
1052 DEBUG(0, ("talloc failed\n"));
1053 return NULL;
1056 if (!(crec = talloc_zero(result, struct db_ctdb_rec))) {
1057 DEBUG(0, ("talloc failed\n"));
1058 TALLOC_FREE(result);
1059 return NULL;
1062 result->db = ctx->db;
1063 result->private_data = (void *)crec;
1064 crec->ctdb_ctx = ctx;
1066 result->key.dsize = key.dsize;
1067 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
1068 key.dsize);
1069 if (result->key.dptr == NULL) {
1070 DEBUG(0, ("talloc failed\n"));
1071 TALLOC_FREE(result);
1072 return NULL;
1075 migrate_attempts = 0;
1076 GetTimeOfDay(&migrate_start);
1079 * Do a blocking lock on the record
1081 again:
1083 if (DEBUGLEVEL >= 10) {
1084 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1085 DEBUG(10, (DEBUGLEVEL > 10
1086 ? "Locking db %u key %s\n"
1087 : "Locking db %u key %.20s\n",
1088 (int)crec->ctdb_ctx->db_id, keystr));
1089 TALLOC_FREE(keystr);
1092 GetTimeOfDay(&chainlock_start);
1093 lockret = tryonly
1094 ? tdb_chainlock_nonblock(ctx->wtdb->tdb, key)
1095 : tdb_chainlock(ctx->wtdb->tdb, key);
1096 chainlock_time += timeval_elapsed(&chainlock_start);
1098 if (lockret != 0) {
1099 DEBUG(3, ("tdb_chainlock failed\n"));
1100 TALLOC_FREE(result);
1101 return NULL;
1104 result->store = db_ctdb_store;
1105 result->delete_rec = db_ctdb_delete;
1106 talloc_set_destructor(result, db_ctdb_record_destr);
1108 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
1111 * See if we have a valid record and we are the dmaster. If so, we can
1112 * take the shortcut and just return it.
1115 if (!db_ctdb_can_use_local_copy(ctdb_data, false)) {
1116 SAFE_FREE(ctdb_data.dptr);
1117 tdb_chainunlock(ctx->wtdb->tdb, key);
1118 talloc_set_destructor(result, NULL);
1120 if (tryonly && (migrate_attempts != 0)) {
1121 DEBUG(5, ("record migrated away again\n"));
1122 TALLOC_FREE(result);
1123 return NULL;
1126 migrate_attempts += 1;
1128 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1129 ctdb_data.dptr, ctdb_data.dptr ?
1130 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
1131 get_my_vnn(),
1132 ctdb_data.dptr ?
1133 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->flags : 0));
1135 GetTimeOfDay(&ctdb_start_time);
1136 status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id,
1137 key);
1138 ctdb_time += timeval_elapsed(&ctdb_start_time);
1140 if (!NT_STATUS_IS_OK(status)) {
1141 DEBUG(5, ("ctdb_migrate failed: %s\n",
1142 nt_errstr(status)));
1143 TALLOC_FREE(result);
1144 return NULL;
1146 /* now its migrated, try again */
1147 goto again;
1151 double duration;
1152 duration = timeval_elapsed(&migrate_start);
1155 * Convert the duration to milliseconds to avoid a
1156 * floating-point division of
1157 * lp_parm_int("migrate_duration") by 1000.
1159 duration_msecs = duration * 1000;
1162 if ((migrate_attempts > ctx->warn_migrate_attempts) ||
1163 (duration_msecs > ctx->warn_migrate_msecs)) {
1164 int chain = 0;
1166 if (tdb_get_flags(ctx->wtdb->tdb) & TDB_INCOMPATIBLE_HASH) {
1167 chain = tdb_jenkins_hash(&key) %
1168 tdb_hash_size(ctx->wtdb->tdb);
1171 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s, chain %d "
1172 "needed %d attempts, %d milliseconds, "
1173 "chainlock: %d ms, CTDB %d ms\n",
1174 tdb_name(ctx->wtdb->tdb),
1175 hex_encode_talloc(talloc_tos(),
1176 (unsigned char *)key.dptr,
1177 key.dsize),
1178 chain,
1179 migrate_attempts, duration_msecs,
1180 (int) chainlock_time * 1000,
1181 (int) ctdb_time * 1000));
1184 GetTimeOfDay(&crec->lock_time);
1186 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1188 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1189 result->value.dptr = NULL;
1191 if ((result->value.dsize != 0)
1192 && !(result->value.dptr = (uint8_t *)talloc_memdup(
1193 result, ctdb_data.dptr + sizeof(crec->header),
1194 result->value.dsize))) {
1195 DEBUG(0, ("talloc failed\n"));
1196 TALLOC_FREE(result);
1199 SAFE_FREE(ctdb_data.dptr);
1201 return result;
1204 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1205 TALLOC_CTX *mem_ctx,
1206 TDB_DATA key)
1208 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1209 struct db_ctdb_ctx);
1211 if (ctx->transaction != NULL) {
1212 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1215 if (db->persistent) {
1216 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1219 return fetch_locked_internal(ctx, mem_ctx, key, false);
1222 static struct db_record *db_ctdb_try_fetch_locked(struct db_context *db,
1223 TALLOC_CTX *mem_ctx,
1224 TDB_DATA key)
1226 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1227 struct db_ctdb_ctx);
1229 if (ctx->transaction != NULL) {
1230 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1233 if (db->persistent) {
1234 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1237 return fetch_locked_internal(ctx, mem_ctx, key, true);
1240 struct db_ctdb_parse_record_state {
1241 void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data);
1242 void *private_data;
1243 bool ask_for_readonly_copy;
1244 bool done;
1247 static void db_ctdb_parse_record_parser(
1248 TDB_DATA key, struct ctdb_ltdb_header *header,
1249 TDB_DATA data, void *private_data)
1251 struct db_ctdb_parse_record_state *state =
1252 (struct db_ctdb_parse_record_state *)private_data;
1253 state->parser(key, data, state->private_data);
1256 static void db_ctdb_parse_record_parser_nonpersistent(
1257 TDB_DATA key, struct ctdb_ltdb_header *header,
1258 TDB_DATA data, void *private_data)
1260 struct db_ctdb_parse_record_state *state =
1261 (struct db_ctdb_parse_record_state *)private_data;
1263 if (db_ctdb_can_use_local_hdr(header, true)) {
1264 state->parser(key, data, state->private_data);
1265 state->done = true;
1266 } else {
1268 * We found something in the db, so it seems that this record,
1269 * while not usable locally right now, is popular. Ask for a
1270 * R/O copy.
1272 state->ask_for_readonly_copy = true;
1276 static NTSTATUS db_ctdb_parse_record(struct db_context *db, TDB_DATA key,
1277 void (*parser)(TDB_DATA key,
1278 TDB_DATA data,
1279 void *private_data),
1280 void *private_data)
1282 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1283 db->private_data, struct db_ctdb_ctx);
1284 struct db_ctdb_parse_record_state state;
1285 NTSTATUS status;
1287 state.parser = parser;
1288 state.private_data = private_data;
1290 if (ctx->transaction != NULL) {
1291 struct db_ctdb_transaction_handle *h = ctx->transaction;
1292 bool found;
1295 * Transactions only happen for persistent db's.
1298 found = parse_newest_in_marshall_buffer(
1299 h->m_write, key, db_ctdb_parse_record_parser, &state);
1301 if (found) {
1302 return NT_STATUS_OK;
1306 if (db->persistent) {
1308 * Persistent db, but not found in the transaction buffer
1310 return db_ctdb_ltdb_parse(
1311 ctx, key, db_ctdb_parse_record_parser, &state);
1314 state.done = false;
1315 state.ask_for_readonly_copy = false;
1317 status = db_ctdb_ltdb_parse(
1318 ctx, key, db_ctdb_parse_record_parser_nonpersistent, &state);
1319 if (NT_STATUS_IS_OK(status) && state.done) {
1320 return NT_STATUS_OK;
1323 return ctdbd_parse(messaging_ctdbd_connection(), ctx->db_id, key,
1324 state.ask_for_readonly_copy, parser, private_data);
1327 struct traverse_state {
1328 struct db_context *db;
1329 int (*fn)(struct db_record *rec, void *private_data);
1330 void *private_data;
1331 int count;
1334 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1336 struct traverse_state *state = (struct traverse_state *)private_data;
1337 struct db_record *rec;
1338 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1339 /* we have to give them a locked record to prevent races */
1340 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1341 if (rec && rec->value.dsize > 0) {
1342 state->fn(rec, state->private_data);
1344 talloc_free(tmp_ctx);
1347 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1348 void *private_data)
1350 struct traverse_state *state = (struct traverse_state *)private_data;
1351 struct db_record *rec;
1352 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1353 int ret = 0;
1356 * Skip the __db_sequence_number__ key:
1357 * This is used for persistent transactions internally.
1359 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1360 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1362 goto done;
1365 /* we have to give them a locked record to prevent races */
1366 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1367 if (rec && rec->value.dsize > 0) {
1368 ret = state->fn(rec, state->private_data);
1371 done:
1372 talloc_free(tmp_ctx);
1373 return ret;
1376 /* wrapper to use traverse_persistent_callback with dbwrap */
1377 static int traverse_persistent_callback_dbwrap(struct db_record *rec, void* data)
1379 return traverse_persistent_callback(NULL, rec->key, rec->value, data);
1383 static int db_ctdb_traverse(struct db_context *db,
1384 int (*fn)(struct db_record *rec,
1385 void *private_data),
1386 void *private_data)
1388 NTSTATUS status;
1389 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1390 struct db_ctdb_ctx);
1391 struct traverse_state state;
1393 state.db = db;
1394 state.fn = fn;
1395 state.private_data = private_data;
1396 state.count = 0;
1398 if (db->persistent) {
1399 struct tdb_context *ltdb = ctx->wtdb->tdb;
1400 int ret;
1402 /* for persistent databases we don't need to do a ctdb traverse,
1403 we can do a faster local traverse */
1404 ret = tdb_traverse(ltdb, traverse_persistent_callback, &state);
1405 if (ret < 0) {
1406 return ret;
1408 if (ctx->transaction && ctx->transaction->m_write) {
1410 * we now have to handle keys not yet
1411 * present at transaction start
1413 struct db_context *newkeys = db_open_rbt(talloc_tos());
1414 struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
1415 struct ctdb_rec_data *rec=NULL;
1416 int i;
1417 int count = 0;
1419 if (newkeys == NULL) {
1420 return -1;
1423 for (i=0; i<mbuf->count; i++) {
1424 TDB_DATA key;
1425 rec = db_ctdb_marshall_loop_next_key(
1426 mbuf, rec, &key);
1427 SMB_ASSERT(rec != NULL);
1429 if (!tdb_exists(ltdb, key)) {
1430 dbwrap_store(newkeys, key, tdb_null, 0);
1433 status = dbwrap_traverse(newkeys,
1434 traverse_persistent_callback_dbwrap,
1435 &state,
1436 &count);
1437 talloc_free(newkeys);
1438 if (!NT_STATUS_IS_OK(status)) {
1439 return -1;
1441 ret += count;
1443 return ret;
1446 status = ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1447 if (!NT_STATUS_IS_OK(status)) {
1448 return -1;
1450 return state.count;
1453 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1455 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1458 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1460 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1463 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1465 struct traverse_state *state = (struct traverse_state *)private_data;
1466 struct db_record rec;
1468 ZERO_STRUCT(rec);
1469 rec.db = state->db;
1470 rec.key = key;
1471 rec.value = data;
1472 rec.store = db_ctdb_store_deny;
1473 rec.delete_rec = db_ctdb_delete_deny;
1474 rec.private_data = NULL;
1475 state->fn(&rec, state->private_data);
1476 state->count++;
1479 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1480 void *private_data)
1482 struct traverse_state *state = (struct traverse_state *)private_data;
1483 struct db_record rec;
1486 * Skip the __db_sequence_number__ key:
1487 * This is used for persistent transactions internally.
1489 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1490 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1492 return 0;
1495 ZERO_STRUCT(rec);
1496 rec.db = state->db;
1497 rec.key = kbuf;
1498 rec.value = dbuf;
1499 rec.store = db_ctdb_store_deny;
1500 rec.delete_rec = db_ctdb_delete_deny;
1501 rec.private_data = NULL;
1503 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1504 /* a deleted record */
1505 return 0;
1507 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1508 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1510 state->count++;
1511 return state->fn(&rec, state->private_data);
1514 static int db_ctdb_traverse_read(struct db_context *db,
1515 int (*fn)(struct db_record *rec,
1516 void *private_data),
1517 void *private_data)
1519 NTSTATUS status;
1520 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1521 struct db_ctdb_ctx);
1522 struct traverse_state state;
1524 state.db = db;
1525 state.fn = fn;
1526 state.private_data = private_data;
1527 state.count = 0;
1529 if (db->persistent) {
1530 /* for persistent databases we don't need to do a ctdb traverse,
1531 we can do a faster local traverse */
1532 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1535 status = ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1536 if (!NT_STATUS_IS_OK(status)) {
1537 return -1;
1539 return state.count;
1542 static int db_ctdb_get_seqnum(struct db_context *db)
1544 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1545 struct db_ctdb_ctx);
1546 return tdb_get_seqnum(ctx->wtdb->tdb);
1549 static void db_ctdb_id(struct db_context *db, const uint8_t **id,
1550 size_t *idlen)
1552 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1553 db->private_data, struct db_ctdb_ctx);
1555 *id = (uint8_t *)&ctx->db_id;
1556 *idlen = sizeof(ctx->db_id);
1559 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1560 const char *name,
1561 int hash_size, int tdb_flags,
1562 int open_flags, mode_t mode,
1563 enum dbwrap_lock_order lock_order,
1564 uint64_t dbwrap_flags)
1566 struct db_context *result;
1567 struct db_ctdb_ctx *db_ctdb;
1568 char *db_path;
1569 struct ctdbd_connection *conn;
1570 struct loadparm_context *lp_ctx;
1571 struct ctdb_db_priority prio;
1572 NTSTATUS status;
1573 int cstatus;
1575 if (!lp_clustering()) {
1576 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1577 return NULL;
1580 if (!(result = talloc_zero(mem_ctx, struct db_context))) {
1581 DEBUG(0, ("talloc failed\n"));
1582 TALLOC_FREE(result);
1583 return NULL;
1586 if (!(db_ctdb = talloc(result, struct db_ctdb_ctx))) {
1587 DEBUG(0, ("talloc failed\n"));
1588 TALLOC_FREE(result);
1589 return NULL;
1592 result->name = talloc_strdup(result, name);
1593 if (result->name == NULL) {
1594 DEBUG(0, ("talloc failed\n"));
1595 TALLOC_FREE(result);
1596 return NULL;
1599 db_ctdb->transaction = NULL;
1600 db_ctdb->db = result;
1602 conn = messaging_ctdbd_connection();
1603 if (conn == NULL) {
1604 DEBUG(1, ("Could not connect to ctdb\n"));
1605 TALLOC_FREE(result);
1606 return NULL;
1609 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1610 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1611 TALLOC_FREE(result);
1612 return NULL;
1615 db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1617 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1618 result->lock_order = lock_order;
1620 /* only pass through specific flags */
1621 tdb_flags &= TDB_SEQNUM;
1623 /* honor permissions if user has specified O_CREAT */
1624 if (open_flags & O_CREAT) {
1625 chmod(db_path, mode);
1628 prio.db_id = db_ctdb->db_id;
1629 prio.priority = lock_order;
1631 status = ctdbd_control_local(
1632 conn, CTDB_CONTROL_SET_DB_PRIORITY, 0, 0,
1633 make_tdb_data((uint8_t *)&prio, sizeof(prio)),
1634 NULL, NULL, &cstatus);
1636 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1637 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1638 nt_errstr(status), cstatus));
1639 TALLOC_FREE(result);
1640 return NULL;
1643 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1644 if (!result->persistent &&
1645 (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS))
1647 TDB_DATA indata;
1649 indata = make_tdb_data((uint8_t *)&db_ctdb->db_id,
1650 sizeof(db_ctdb->db_id));
1652 status = ctdbd_control_local(
1653 conn, CTDB_CONTROL_SET_DB_READONLY, 0, 0, indata,
1654 NULL, NULL, &cstatus);
1655 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1656 DEBUG(1, ("CTDB_CONTROL_SET_DB_READONLY failed: "
1657 "%s, %d\n", nt_errstr(status), cstatus));
1658 TALLOC_FREE(result);
1659 return NULL;
1662 #endif
1664 lp_ctx = loadparm_init_s3(db_path, loadparm_s3_helpers());
1666 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags,
1667 O_RDWR, 0, lp_ctx);
1668 talloc_unlink(db_path, lp_ctx);
1669 if (db_ctdb->wtdb == NULL) {
1670 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1671 TALLOC_FREE(result);
1672 return NULL;
1674 talloc_free(db_path);
1676 if (result->persistent) {
1677 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1678 ctdb_conn_msg_ctx(conn));
1679 if (db_ctdb->lock_ctx == NULL) {
1680 DEBUG(0, ("g_lock_ctx_init failed\n"));
1681 TALLOC_FREE(result);
1682 return NULL;
1686 db_ctdb->warn_unlock_msecs = lp_parm_int(-1, "ctdb",
1687 "unlock_warn_threshold", 5);
1688 db_ctdb->warn_migrate_attempts = lp_parm_int(-1, "ctdb",
1689 "migrate_attempts", 10);
1690 db_ctdb->warn_migrate_msecs = lp_parm_int(-1, "ctdb",
1691 "migrate_duration", 5000);
1692 db_ctdb->warn_locktime_msecs = lp_ctdb_locktime_warn_threshold();
1694 result->private_data = (void *)db_ctdb;
1695 result->fetch_locked = db_ctdb_fetch_locked;
1696 result->try_fetch_locked = db_ctdb_try_fetch_locked;
1697 result->parse_record = db_ctdb_parse_record;
1698 result->traverse = db_ctdb_traverse;
1699 result->traverse_read = db_ctdb_traverse_read;
1700 result->get_seqnum = db_ctdb_get_seqnum;
1701 result->transaction_start = db_ctdb_transaction_start;
1702 result->transaction_commit = db_ctdb_transaction_commit;
1703 result->transaction_cancel = db_ctdb_transaction_cancel;
1704 result->id = db_ctdb_id;
1705 result->stored_callback = NULL;
1707 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1708 name, db_ctdb->db_id));
1710 return result;
1713 #endif