s3:dbwrap_ctdb: setup result->name in db_open_ctdb()
[Samba/gebeck_regimport.git] / source3 / lib / dbwrap / dbwrap_ctdb.c
blobdd0f4eac324d9b7a63b4bccf30ae46b41907b199
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;
78 struct db_ctdb_rec {
79 struct db_ctdb_ctx *ctdb_ctx;
80 struct ctdb_ltdb_header header;
81 struct timeval lock_time;
84 static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
86 enum TDB_ERROR tret = tdb_error(tdb);
88 return map_nt_error_from_tdb(tret);
91 struct db_ctdb_ltdb_parse_state {
92 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
93 TDB_DATA data, void *private_data);
94 void *private_data;
97 static int db_ctdb_ltdb_parser(TDB_DATA key, TDB_DATA data,
98 void *private_data)
100 struct db_ctdb_ltdb_parse_state *state =
101 (struct db_ctdb_ltdb_parse_state *)private_data;
103 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
104 return -1;
106 state->parser(
107 key, (struct ctdb_ltdb_header *)data.dptr,
108 make_tdb_data(data.dptr + sizeof(struct ctdb_ltdb_header),
109 data.dsize - sizeof(struct ctdb_ltdb_header)),
110 state->private_data);
111 return 0;
114 static NTSTATUS db_ctdb_ltdb_parse(
115 struct db_ctdb_ctx *db, TDB_DATA key,
116 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
117 TDB_DATA data, void *private_data),
118 void *private_data)
120 struct db_ctdb_ltdb_parse_state state;
121 int ret;
123 state.parser = parser;
124 state.private_data = private_data;
126 ret = tdb_parse_record(db->wtdb->tdb, key, db_ctdb_ltdb_parser,
127 &state);
128 if (ret == -1) {
129 return NT_STATUS_NOT_FOUND;
131 return NT_STATUS_OK;
135 * Store a record together with the ctdb record header
136 * in the local copy of the database.
138 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
139 TDB_DATA key,
140 struct ctdb_ltdb_header *header,
141 TDB_DATA data)
143 TALLOC_CTX *tmp_ctx = talloc_stackframe();
144 TDB_DATA rec;
145 int ret;
147 rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
148 rec.dptr = (uint8_t *)talloc_size(tmp_ctx, rec.dsize);
150 if (rec.dptr == NULL) {
151 talloc_free(tmp_ctx);
152 return NT_STATUS_NO_MEMORY;
155 memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
156 memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
158 ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
160 talloc_free(tmp_ctx);
162 return (ret == 0) ? NT_STATUS_OK
163 : tdb_error_to_ntstatus(db->wtdb->tdb);
168 form a ctdb_rec_data record from a key/data pair
170 static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
171 TDB_DATA key,
172 struct ctdb_ltdb_header *header,
173 TDB_DATA data)
175 size_t length;
176 struct ctdb_rec_data *d;
178 length = offsetof(struct ctdb_rec_data, data) + key.dsize +
179 data.dsize + sizeof(*header);
180 d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
181 if (d == NULL) {
182 return NULL;
184 d->length = length;
185 d->reqid = reqid;
186 d->keylen = key.dsize;
187 memcpy(&d->data[0], key.dptr, key.dsize);
189 d->datalen = data.dsize + sizeof(*header);
190 memcpy(&d->data[key.dsize], header, sizeof(*header));
191 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
192 return d;
196 /* helper function for marshalling multiple records */
197 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
198 struct ctdb_marshall_buffer *m,
199 uint64_t db_id,
200 uint32_t reqid,
201 TDB_DATA key,
202 struct ctdb_ltdb_header *header,
203 TDB_DATA data)
205 struct ctdb_rec_data *r;
206 size_t m_size, r_size;
207 struct ctdb_marshall_buffer *m2 = NULL;
209 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
210 if (r == NULL) {
211 talloc_free(m);
212 return NULL;
215 if (m == NULL) {
216 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
217 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
218 if (m == NULL) {
219 goto done;
221 m->db_id = db_id;
224 m_size = talloc_get_size(m);
225 r_size = talloc_get_size(r);
227 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
228 mem_ctx, m, m_size + r_size);
229 if (m2 == NULL) {
230 talloc_free(m);
231 goto done;
234 memcpy(m_size + (uint8_t *)m2, r, r_size);
236 m2->count++;
238 done:
239 talloc_free(r);
240 return m2;
243 /* we've finished marshalling, return a data blob with the marshalled records */
244 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
246 TDB_DATA data;
247 data.dptr = (uint8_t *)m;
248 data.dsize = talloc_get_size(m);
249 return data;
253 loop over a marshalling buffer
255 - pass r==NULL to start
256 - loop the number of times indicated by m->count
258 static struct ctdb_rec_data *db_ctdb_marshall_loop_next_key(
259 struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r, TDB_DATA *key)
261 if (r == NULL) {
262 r = (struct ctdb_rec_data *)&m->data[0];
263 } else {
264 r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
267 key->dptr = &r->data[0];
268 key->dsize = r->keylen;
269 return r;
272 static bool db_ctdb_marshall_buf_parse(
273 struct ctdb_rec_data *r, uint32_t *reqid,
274 struct ctdb_ltdb_header **header, TDB_DATA *data)
276 if (r->datalen < sizeof(struct ctdb_ltdb_header)) {
277 return false;
280 *reqid = r->reqid;
282 data->dptr = &r->data[r->keylen] + sizeof(struct ctdb_ltdb_header);
283 data->dsize = r->datalen - sizeof(struct ctdb_ltdb_header);
285 *header = (struct ctdb_ltdb_header *)&r->data[r->keylen];
287 return true;
291 * CTDB transaction destructor
293 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
295 NTSTATUS status;
297 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
298 if (!NT_STATUS_IS_OK(status)) {
299 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h->lock_name,
300 nt_errstr(status)));
301 return -1;
303 return 0;
307 * CTDB dbwrap API: transaction_start function
308 * starts a transaction on a persistent database
310 static int db_ctdb_transaction_start(struct db_context *db)
312 struct db_ctdb_transaction_handle *h;
313 NTSTATUS status;
314 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
315 struct db_ctdb_ctx);
317 if (!db->persistent) {
318 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
319 ctx->db_id));
320 return -1;
323 if (ctx->transaction) {
324 ctx->transaction->nesting++;
325 DEBUG(5, (__location__ " transaction start on db 0x%08x: nesting %d -> %d\n",
326 ctx->db_id, ctx->transaction->nesting - 1, ctx->transaction->nesting));
327 return 0;
330 h = talloc_zero(db, struct db_ctdb_transaction_handle);
331 if (h == NULL) {
332 DEBUG(0,(__location__ " oom for transaction handle\n"));
333 return -1;
336 h->ctx = ctx;
338 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
339 (unsigned int)ctx->db_id);
340 if (h->lock_name == NULL) {
341 DEBUG(0, ("talloc_asprintf failed\n"));
342 TALLOC_FREE(h);
343 return -1;
347 * Wait a day, i.e. forever...
349 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
350 timeval_set(86400, 0));
351 if (!NT_STATUS_IS_OK(status)) {
352 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
353 TALLOC_FREE(h);
354 return -1;
357 talloc_set_destructor(h, db_ctdb_transaction_destructor);
359 ctx->transaction = h;
361 DEBUG(5,(__location__ " transaction started on db 0x%08x\n", ctx->db_id));
363 return 0;
366 static bool parse_newest_in_marshall_buffer(
367 struct ctdb_marshall_buffer *buf, TDB_DATA key,
368 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
369 TDB_DATA data, void *private_data),
370 void *private_data)
372 struct ctdb_rec_data *rec = NULL;
373 struct ctdb_ltdb_header *h = NULL;
374 TDB_DATA data;
375 int i;
377 if (buf == NULL) {
378 return false;
382 * Walk the list of records written during this
383 * transaction. If we want to read one we have already
384 * written, return the last written sample. Thus we do not do
385 * a "break;" for the first hit, this record might have been
386 * overwritten later.
389 for (i=0; i<buf->count; i++) {
390 TDB_DATA tkey;
391 uint32_t reqid;
393 rec = db_ctdb_marshall_loop_next_key(buf, rec, &tkey);
394 if (rec == NULL) {
395 return false;
398 if (!tdb_data_equal(key, tkey)) {
399 continue;
402 if (!db_ctdb_marshall_buf_parse(rec, &reqid, &h, &data)) {
403 return false;
407 if (h == NULL) {
408 return false;
411 parser(key, h, data, private_data);
413 return true;
416 struct pull_newest_from_marshall_buffer_state {
417 struct ctdb_ltdb_header *pheader;
418 TALLOC_CTX *mem_ctx;
419 TDB_DATA *pdata;
422 static void pull_newest_from_marshall_buffer_parser(
423 TDB_DATA key, struct ctdb_ltdb_header *header,
424 TDB_DATA data, void *private_data)
426 struct pull_newest_from_marshall_buffer_state *state =
427 (struct pull_newest_from_marshall_buffer_state *)private_data;
429 if (state->pheader != NULL) {
430 memcpy(state->pheader, header, sizeof(*state->pheader));
432 if (state->pdata != NULL) {
433 state->pdata->dsize = data.dsize;
434 state->pdata->dptr = (uint8_t *)talloc_memdup(
435 state->mem_ctx, data.dptr, data.dsize);
439 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
440 TDB_DATA key,
441 struct ctdb_ltdb_header *pheader,
442 TALLOC_CTX *mem_ctx,
443 TDB_DATA *pdata)
445 struct pull_newest_from_marshall_buffer_state state;
447 state.pheader = pheader;
448 state.mem_ctx = mem_ctx;
449 state.pdata = pdata;
451 if (!parse_newest_in_marshall_buffer(
452 buf, key, pull_newest_from_marshall_buffer_parser,
453 &state)) {
454 return false;
456 if ((pdata != NULL) && (pdata->dsize != 0) && (pdata->dptr == NULL)) {
457 /* ENOMEM */
458 return false;
460 return true;
463 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
464 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
466 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
467 TALLOC_CTX *mem_ctx,
468 TDB_DATA key)
470 struct db_record *result;
471 TDB_DATA ctdb_data;
473 if (!(result = talloc(mem_ctx, struct db_record))) {
474 DEBUG(0, ("talloc failed\n"));
475 return NULL;
478 result->private_data = ctx->transaction;
480 result->key.dsize = key.dsize;
481 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
482 key.dsize);
483 if (result->key.dptr == NULL) {
484 DEBUG(0, ("talloc failed\n"));
485 TALLOC_FREE(result);
486 return NULL;
489 result->store = db_ctdb_store_transaction;
490 result->delete_rec = db_ctdb_delete_transaction;
492 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
493 NULL, result, &result->value)) {
494 return result;
497 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
498 if (ctdb_data.dptr == NULL) {
499 /* create the record */
500 result->value = tdb_null;
501 return result;
504 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
505 result->value.dptr = NULL;
507 if ((result->value.dsize != 0)
508 && !(result->value.dptr = (uint8_t *)talloc_memdup(
509 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
510 result->value.dsize))) {
511 DEBUG(0, ("talloc failed\n"));
512 TALLOC_FREE(result);
515 SAFE_FREE(ctdb_data.dptr);
517 return result;
520 static int db_ctdb_record_destructor(struct db_record **recp)
522 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
523 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
524 rec->private_data, struct db_ctdb_transaction_handle);
525 int ret = h->ctx->db->transaction_commit(h->ctx->db);
526 if (ret != 0) {
527 DEBUG(0,(__location__ " transaction_commit failed\n"));
529 return 0;
533 auto-create a transaction for persistent databases
535 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
536 TALLOC_CTX *mem_ctx,
537 TDB_DATA key)
539 int res;
540 struct db_record *rec, **recp;
542 res = db_ctdb_transaction_start(ctx->db);
543 if (res == -1) {
544 return NULL;
547 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
548 if (rec == NULL) {
549 ctx->db->transaction_cancel(ctx->db);
550 return NULL;
553 /* destroy this transaction when we release the lock */
554 recp = talloc(rec, struct db_record *);
555 if (recp == NULL) {
556 ctx->db->transaction_cancel(ctx->db);
557 talloc_free(rec);
558 return NULL;
560 *recp = rec;
561 talloc_set_destructor(recp, db_ctdb_record_destructor);
562 return rec;
567 stores a record inside a transaction
569 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
570 TDB_DATA key, TDB_DATA data)
572 TALLOC_CTX *tmp_ctx = talloc_new(h);
573 TDB_DATA rec;
574 struct ctdb_ltdb_header header;
576 ZERO_STRUCT(header);
578 /* we need the header so we can update the RSN */
580 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
581 NULL, NULL)) {
583 rec = tdb_fetch_compat(h->ctx->wtdb->tdb, key);
585 if (rec.dptr != NULL) {
586 memcpy(&header, rec.dptr,
587 sizeof(struct ctdb_ltdb_header));
588 rec.dsize -= sizeof(struct ctdb_ltdb_header);
591 * a special case, we are writing the same
592 * data that is there now
594 if (data.dsize == rec.dsize &&
595 memcmp(data.dptr,
596 rec.dptr + sizeof(struct ctdb_ltdb_header),
597 data.dsize) == 0) {
598 SAFE_FREE(rec.dptr);
599 talloc_free(tmp_ctx);
600 return NT_STATUS_OK;
603 SAFE_FREE(rec.dptr);
606 header.dmaster = get_my_vnn();
607 header.rsn++;
609 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
610 if (h->m_write == NULL) {
611 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
612 talloc_free(tmp_ctx);
613 return NT_STATUS_NO_MEMORY;
616 talloc_free(tmp_ctx);
617 return NT_STATUS_OK;
622 a record store inside a transaction
624 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
626 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
627 rec->private_data, struct db_ctdb_transaction_handle);
628 NTSTATUS status;
630 status = db_ctdb_transaction_store(h, rec->key, data);
631 return status;
635 a record delete inside a transaction
637 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
639 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
640 rec->private_data, struct db_ctdb_transaction_handle);
641 NTSTATUS status;
643 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
644 return status;
647 static void db_ctdb_fetch_db_seqnum_parser(
648 TDB_DATA key, struct ctdb_ltdb_header *header,
649 TDB_DATA data, void *private_data)
651 uint64_t *seqnum = (uint64_t *)private_data;
653 if (data.dsize != sizeof(uint64_t)) {
654 *seqnum = 0;
655 return;
657 memcpy(seqnum, data.dptr, sizeof(*seqnum));
661 * Fetch the db sequence number of a persistent db directly from the db.
663 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
664 uint64_t *seqnum)
666 NTSTATUS status;
667 TDB_DATA key;
669 if (seqnum == NULL) {
670 return NT_STATUS_INVALID_PARAMETER;
673 key = string_term_tdb_data(CTDB_DB_SEQNUM_KEY);
675 status = db_ctdb_ltdb_parse(
676 db, key, db_ctdb_fetch_db_seqnum_parser, seqnum);
678 if (NT_STATUS_IS_OK(status)) {
679 return NT_STATUS_OK;
681 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
682 *seqnum = 0;
683 return NT_STATUS_OK;
685 return status;
689 * Store the database sequence number inside a transaction.
691 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
692 uint64_t seqnum)
694 NTSTATUS status;
695 const char *keyname = CTDB_DB_SEQNUM_KEY;
696 TDB_DATA key;
697 TDB_DATA data;
699 key = string_term_tdb_data(keyname);
701 data.dptr = (uint8_t *)&seqnum;
702 data.dsize = sizeof(uint64_t);
704 status = db_ctdb_transaction_store(h, key, data);
706 return status;
710 commit a transaction
712 static int db_ctdb_transaction_commit(struct db_context *db)
714 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
715 struct db_ctdb_ctx);
716 NTSTATUS rets;
717 int status;
718 struct db_ctdb_transaction_handle *h = ctx->transaction;
719 uint64_t old_seqnum, new_seqnum;
720 int ret;
722 if (h == NULL) {
723 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
724 return -1;
727 if (h->nested_cancel) {
728 db->transaction_cancel(db);
729 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
730 return -1;
733 if (h->nesting != 0) {
734 h->nesting--;
735 DEBUG(5, (__location__ " transaction commit on db 0x%08x: nesting %d -> %d\n",
736 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
737 return 0;
740 if (h->m_write == NULL) {
742 * No changes were made, so don't change the seqnum,
743 * don't push to other node, just exit with success.
745 ret = 0;
746 goto done;
749 DEBUG(5,(__location__ " transaction commit on db 0x%08x\n", ctx->db_id));
752 * As the last db action before committing, bump the database sequence
753 * number. Note that this undoes all changes to the seqnum records
754 * performed under the transaction. This record is not meant to be
755 * modified by user interaction. It is for internal use only...
757 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
758 if (!NT_STATUS_IS_OK(rets)) {
759 DEBUG(1, (__location__ " failed to fetch the db sequence number "
760 "in transaction commit on db 0x%08x\n", ctx->db_id));
761 ret = -1;
762 goto done;
765 new_seqnum = old_seqnum + 1;
767 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
768 if (!NT_STATUS_IS_OK(rets)) {
769 DEBUG(1, (__location__ "failed to store the db sequence number "
770 " in transaction commit on db 0x%08x\n", ctx->db_id));
771 ret = -1;
772 goto done;
775 again:
776 /* tell ctdbd to commit to the other nodes */
777 rets = ctdbd_control_local(messaging_ctdbd_connection(),
778 CTDB_CONTROL_TRANS3_COMMIT,
779 h->ctx->db_id, 0,
780 db_ctdb_marshall_finish(h->m_write),
781 NULL, NULL, &status);
782 if (!NT_STATUS_IS_OK(rets) || status != 0) {
784 * The TRANS3_COMMIT control should only possibly fail when a
785 * recovery has been running concurrently. In any case, the db
786 * will be the same on all nodes, either the new copy or the
787 * old copy. This can be detected by comparing the old and new
788 * local sequence numbers.
790 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
791 if (!NT_STATUS_IS_OK(rets)) {
792 DEBUG(1, (__location__ " failed to refetch db sequence "
793 "number after failed TRANS3_COMMIT\n"));
794 ret = -1;
795 goto done;
798 if (new_seqnum == old_seqnum) {
799 /* Recovery prevented all our changes: retry. */
800 goto again;
802 if (new_seqnum != (old_seqnum + 1)) {
803 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
804 "old_seqnum[%lu] + (0 or 1) after failed "
805 "TRANS3_COMMIT - this should not happen!\n",
806 (unsigned long)new_seqnum,
807 (unsigned long)old_seqnum));
808 ret = -1;
809 goto done;
812 * Recovery propagated our changes to all nodes, completing
813 * our commit for us - succeed.
817 ret = 0;
819 done:
820 h->ctx->transaction = NULL;
821 talloc_free(h);
822 return ret;
827 cancel a transaction
829 static int db_ctdb_transaction_cancel(struct db_context *db)
831 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
832 struct db_ctdb_ctx);
833 struct db_ctdb_transaction_handle *h = ctx->transaction;
835 if (h == NULL) {
836 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
837 return -1;
840 if (h->nesting != 0) {
841 h->nesting--;
842 h->nested_cancel = true;
843 DEBUG(5, (__location__ " transaction cancel on db 0x%08x: nesting %d -> %d\n",
844 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
845 return 0;
848 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
850 ctx->transaction = NULL;
851 talloc_free(h);
852 return 0;
856 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
858 struct db_ctdb_rec *crec = talloc_get_type_abort(
859 rec->private_data, struct db_ctdb_rec);
861 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
866 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
867 static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
869 NTSTATUS status;
870 struct ctdb_control_schedule_for_deletion *dd;
871 TDB_DATA indata;
872 int cstatus;
873 struct db_ctdb_rec *crec = talloc_get_type_abort(
874 rec->private_data, struct db_ctdb_rec);
876 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
877 indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
878 if (indata.dptr == NULL) {
879 DEBUG(0, (__location__ " talloc failed!\n"));
880 return NT_STATUS_NO_MEMORY;
883 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
884 dd->db_id = crec->ctdb_ctx->db_id;
885 dd->hdr = crec->header;
886 dd->keylen = rec->key.dsize;
887 memcpy(dd->key, rec->key.dptr, rec->key.dsize);
889 status = ctdbd_control_local(messaging_ctdbd_connection(),
890 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
891 crec->ctdb_ctx->db_id,
892 CTDB_CTRL_FLAG_NOREPLY, /* flags */
893 indata,
894 NULL, /* outdata */
895 NULL, /* errmsg */
896 &cstatus);
897 talloc_free(indata.dptr);
899 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
900 DEBUG(1, (__location__ " Error sending local control "
901 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
902 nt_errstr(status), cstatus));
903 if (NT_STATUS_IS_OK(status)) {
904 status = NT_STATUS_UNSUCCESSFUL;
908 return status;
910 #endif
912 static NTSTATUS db_ctdb_delete(struct db_record *rec)
914 TDB_DATA data;
915 NTSTATUS status;
918 * We have to store the header with empty data. TODO: Fix the
919 * tdb-level cleanup
922 ZERO_STRUCT(data);
924 status = db_ctdb_store(rec, data, 0);
925 if (!NT_STATUS_IS_OK(status)) {
926 return status;
929 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
930 status = db_ctdb_send_schedule_for_deletion(rec);
931 #endif
933 return status;
936 static int db_ctdb_record_destr(struct db_record* data)
938 struct db_ctdb_rec *crec = talloc_get_type_abort(
939 data->private_data, struct db_ctdb_rec);
940 int threshold;
942 DEBUG(10, (DEBUGLEVEL > 10
943 ? "Unlocking db %u key %s\n"
944 : "Unlocking db %u key %.20s\n",
945 (int)crec->ctdb_ctx->db_id,
946 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
947 data->key.dsize)));
949 tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key);
951 threshold = lp_ctdb_locktime_warn_threshold();
952 if (threshold != 0) {
953 double timediff = timeval_elapsed(&crec->lock_time);
954 if ((timediff * 1000) > threshold) {
955 const char *key;
957 key = hex_encode_talloc(data,
958 (unsigned char *)data->key.dptr,
959 data->key.dsize);
960 DEBUG(0, ("Held tdb lock on db %s, key %s %f seconds\n",
961 tdb_name(crec->ctdb_ctx->wtdb->tdb), key,
962 timediff));
966 return 0;
970 * Check whether we have a valid local copy of the given record,
971 * either for reading or for writing.
973 static bool db_ctdb_can_use_local_hdr(const struct ctdb_ltdb_header *hdr,
974 bool read_only)
976 #ifdef HAVE_CTDB_WANT_READONLY_DECL
977 if (hdr->dmaster != get_my_vnn()) {
978 /* If we're not dmaster, it must be r/o copy. */
979 return read_only && (hdr->flags & CTDB_REC_RO_HAVE_READONLY);
983 * If we want write access, no one may have r/o copies.
985 return read_only || !(hdr->flags & CTDB_REC_RO_HAVE_DELEGATIONS);
986 #else
987 return (hdr->dmaster == get_my_vnn());
988 #endif
991 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data, bool read_only)
993 if (ctdb_data.dptr == NULL)
994 return false;
996 if (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header))
997 return false;
999 return db_ctdb_can_use_local_hdr(
1000 (struct ctdb_ltdb_header *)ctdb_data.dptr, read_only);
1003 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
1004 TALLOC_CTX *mem_ctx,
1005 TDB_DATA key,
1006 bool tryonly)
1008 struct db_record *result;
1009 struct db_ctdb_rec *crec;
1010 NTSTATUS status;
1011 TDB_DATA ctdb_data;
1012 int migrate_attempts = 0;
1013 int lockret;
1015 if (!(result = talloc(mem_ctx, struct db_record))) {
1016 DEBUG(0, ("talloc failed\n"));
1017 return NULL;
1020 if (!(crec = talloc_zero(result, struct db_ctdb_rec))) {
1021 DEBUG(0, ("talloc failed\n"));
1022 TALLOC_FREE(result);
1023 return NULL;
1026 result->db = ctx->db;
1027 result->private_data = (void *)crec;
1028 crec->ctdb_ctx = ctx;
1030 result->key.dsize = key.dsize;
1031 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
1032 key.dsize);
1033 if (result->key.dptr == NULL) {
1034 DEBUG(0, ("talloc failed\n"));
1035 TALLOC_FREE(result);
1036 return NULL;
1040 * Do a blocking lock on the record
1042 again:
1044 if (DEBUGLEVEL >= 10) {
1045 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1046 DEBUG(10, (DEBUGLEVEL > 10
1047 ? "Locking db %u key %s\n"
1048 : "Locking db %u key %.20s\n",
1049 (int)crec->ctdb_ctx->db_id, keystr));
1050 TALLOC_FREE(keystr);
1053 lockret = tryonly
1054 ? tdb_chainlock_nonblock(ctx->wtdb->tdb, key)
1055 : tdb_chainlock(ctx->wtdb->tdb, key);
1056 if (lockret != 0) {
1057 DEBUG(3, ("tdb_chainlock failed\n"));
1058 TALLOC_FREE(result);
1059 return NULL;
1062 result->store = db_ctdb_store;
1063 result->delete_rec = db_ctdb_delete;
1064 talloc_set_destructor(result, db_ctdb_record_destr);
1066 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
1069 * See if we have a valid record and we are the dmaster. If so, we can
1070 * take the shortcut and just return it.
1073 if (!db_ctdb_can_use_local_copy(ctdb_data, false)) {
1074 SAFE_FREE(ctdb_data.dptr);
1075 tdb_chainunlock(ctx->wtdb->tdb, key);
1076 talloc_set_destructor(result, NULL);
1078 if (tryonly && (migrate_attempts != 0)) {
1079 DEBUG(5, ("record migrated away again\n"));
1080 TALLOC_FREE(result);
1081 return NULL;
1084 migrate_attempts += 1;
1086 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1087 ctdb_data.dptr, ctdb_data.dptr ?
1088 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
1089 get_my_vnn(),
1090 ctdb_data.dptr ?
1091 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->flags : 0));
1093 status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id,
1094 key);
1095 if (!NT_STATUS_IS_OK(status)) {
1096 DEBUG(5, ("ctdb_migrate failed: %s\n",
1097 nt_errstr(status)));
1098 TALLOC_FREE(result);
1099 return NULL;
1101 /* now its migrated, try again */
1102 goto again;
1105 if (migrate_attempts > 10) {
1106 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s needed %d "
1107 "attempts\n", tdb_name(ctx->wtdb->tdb),
1108 hex_encode_talloc(talloc_tos(),
1109 (unsigned char *)key.dptr,
1110 key.dsize),
1111 migrate_attempts));
1114 GetTimeOfDay(&crec->lock_time);
1116 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1118 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1119 result->value.dptr = NULL;
1121 if ((result->value.dsize != 0)
1122 && !(result->value.dptr = (uint8_t *)talloc_memdup(
1123 result, ctdb_data.dptr + sizeof(crec->header),
1124 result->value.dsize))) {
1125 DEBUG(0, ("talloc failed\n"));
1126 TALLOC_FREE(result);
1129 SAFE_FREE(ctdb_data.dptr);
1131 return result;
1134 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1135 TALLOC_CTX *mem_ctx,
1136 TDB_DATA key)
1138 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1139 struct db_ctdb_ctx);
1141 if (ctx->transaction != NULL) {
1142 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1145 if (db->persistent) {
1146 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1149 return fetch_locked_internal(ctx, mem_ctx, key, false);
1152 static struct db_record *db_ctdb_try_fetch_locked(struct db_context *db,
1153 TALLOC_CTX *mem_ctx,
1154 TDB_DATA key)
1156 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1157 struct db_ctdb_ctx);
1159 if (ctx->transaction != NULL) {
1160 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1163 if (db->persistent) {
1164 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1167 return fetch_locked_internal(ctx, mem_ctx, key, true);
1170 struct db_ctdb_parse_record_state {
1171 void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data);
1172 void *private_data;
1173 bool ask_for_readonly_copy;
1174 bool done;
1177 static void db_ctdb_parse_record_parser(
1178 TDB_DATA key, struct ctdb_ltdb_header *header,
1179 TDB_DATA data, void *private_data)
1181 struct db_ctdb_parse_record_state *state =
1182 (struct db_ctdb_parse_record_state *)private_data;
1183 state->parser(key, data, state->private_data);
1186 static void db_ctdb_parse_record_parser_nonpersistent(
1187 TDB_DATA key, struct ctdb_ltdb_header *header,
1188 TDB_DATA data, void *private_data)
1190 struct db_ctdb_parse_record_state *state =
1191 (struct db_ctdb_parse_record_state *)private_data;
1193 if (db_ctdb_can_use_local_hdr(header, true)) {
1194 state->parser(key, data, state->private_data);
1195 state->done = true;
1196 } else {
1198 * We found something in the db, so it seems that this record,
1199 * while not usable locally right now, is popular. Ask for a
1200 * R/O copy.
1202 state->ask_for_readonly_copy = true;
1206 static NTSTATUS db_ctdb_parse_record(struct db_context *db, TDB_DATA key,
1207 void (*parser)(TDB_DATA key,
1208 TDB_DATA data,
1209 void *private_data),
1210 void *private_data)
1212 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1213 db->private_data, struct db_ctdb_ctx);
1214 struct db_ctdb_parse_record_state state;
1215 NTSTATUS status;
1216 TDB_DATA data;
1218 state.parser = parser;
1219 state.private_data = private_data;
1221 if (ctx->transaction != NULL) {
1222 struct db_ctdb_transaction_handle *h = ctx->transaction;
1223 bool found;
1226 * Transactions only happen for persistent db's.
1229 found = parse_newest_in_marshall_buffer(
1230 h->m_write, key, db_ctdb_parse_record_parser, &state);
1232 if (found) {
1233 return NT_STATUS_OK;
1237 if (db->persistent) {
1239 * Persistent db, but not found in the transaction buffer
1241 return db_ctdb_ltdb_parse(
1242 ctx, key, db_ctdb_parse_record_parser, &state);
1245 state.done = false;
1246 state.ask_for_readonly_copy = false;
1248 status = db_ctdb_ltdb_parse(
1249 ctx, key, db_ctdb_parse_record_parser_nonpersistent, &state);
1250 if (NT_STATUS_IS_OK(status) && state.done) {
1251 return NT_STATUS_OK;
1254 status = ctdbd_fetch(messaging_ctdbd_connection(), ctx->db_id, key,
1255 talloc_tos(), &data, state.ask_for_readonly_copy);
1256 if (!NT_STATUS_IS_OK(status)) {
1257 return status;
1259 parser(key, data, private_data);
1260 TALLOC_FREE(data.dptr);
1261 return NT_STATUS_OK;
1264 struct traverse_state {
1265 struct db_context *db;
1266 int (*fn)(struct db_record *rec, void *private_data);
1267 void *private_data;
1268 int count;
1271 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1273 struct traverse_state *state = (struct traverse_state *)private_data;
1274 struct db_record *rec;
1275 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1276 /* we have to give them a locked record to prevent races */
1277 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1278 if (rec && rec->value.dsize > 0) {
1279 state->fn(rec, state->private_data);
1281 talloc_free(tmp_ctx);
1284 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1285 void *private_data)
1287 struct traverse_state *state = (struct traverse_state *)private_data;
1288 struct db_record *rec;
1289 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1290 int ret = 0;
1293 * Skip the __db_sequence_number__ key:
1294 * This is used for persistent transactions internally.
1296 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1297 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1299 goto done;
1302 /* we have to give them a locked record to prevent races */
1303 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1304 if (rec && rec->value.dsize > 0) {
1305 ret = state->fn(rec, state->private_data);
1308 done:
1309 talloc_free(tmp_ctx);
1310 return ret;
1313 /* wrapper to use traverse_persistent_callback with dbwrap */
1314 static int traverse_persistent_callback_dbwrap(struct db_record *rec, void* data)
1316 return traverse_persistent_callback(NULL, rec->key, rec->value, data);
1320 static int db_ctdb_traverse(struct db_context *db,
1321 int (*fn)(struct db_record *rec,
1322 void *private_data),
1323 void *private_data)
1325 NTSTATUS status;
1326 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1327 struct db_ctdb_ctx);
1328 struct traverse_state state;
1330 state.db = db;
1331 state.fn = fn;
1332 state.private_data = private_data;
1333 state.count = 0;
1335 if (db->persistent) {
1336 struct tdb_context *ltdb = ctx->wtdb->tdb;
1337 int ret;
1339 /* for persistent databases we don't need to do a ctdb traverse,
1340 we can do a faster local traverse */
1341 ret = tdb_traverse(ltdb, traverse_persistent_callback, &state);
1342 if (ret < 0) {
1343 return ret;
1345 if (ctx->transaction && ctx->transaction->m_write) {
1347 * we now have to handle keys not yet
1348 * present at transaction start
1350 struct db_context *newkeys = db_open_rbt(talloc_tos());
1351 struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
1352 struct ctdb_rec_data *rec=NULL;
1353 int i;
1354 int count = 0;
1356 if (newkeys == NULL) {
1357 return -1;
1360 for (i=0; i<mbuf->count; i++) {
1361 TDB_DATA key;
1362 rec = db_ctdb_marshall_loop_next_key(
1363 mbuf, rec, &key);
1364 SMB_ASSERT(rec != NULL);
1366 if (!tdb_exists(ltdb, key)) {
1367 dbwrap_store(newkeys, key, tdb_null, 0);
1370 status = dbwrap_traverse(newkeys,
1371 traverse_persistent_callback_dbwrap,
1372 &state,
1373 &count);
1374 talloc_free(newkeys);
1375 if (!NT_STATUS_IS_OK(status)) {
1376 return -1;
1378 ret += count;
1380 return ret;
1383 status = ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1384 if (!NT_STATUS_IS_OK(status)) {
1385 return -1;
1387 return state.count;
1390 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1392 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1395 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1397 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1400 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1402 struct traverse_state *state = (struct traverse_state *)private_data;
1403 struct db_record rec;
1404 rec.key = key;
1405 rec.value = data;
1406 rec.store = db_ctdb_store_deny;
1407 rec.delete_rec = db_ctdb_delete_deny;
1408 rec.private_data = state->db;
1409 state->fn(&rec, state->private_data);
1410 state->count++;
1413 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1414 void *private_data)
1416 struct traverse_state *state = (struct traverse_state *)private_data;
1417 struct db_record rec;
1420 * Skip the __db_sequence_number__ key:
1421 * This is used for persistent transactions internally.
1423 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1424 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1426 return 0;
1429 rec.key = kbuf;
1430 rec.value = dbuf;
1431 rec.store = db_ctdb_store_deny;
1432 rec.delete_rec = db_ctdb_delete_deny;
1433 rec.private_data = state->db;
1435 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1436 /* a deleted record */
1437 return 0;
1439 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1440 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1442 state->count++;
1443 return state->fn(&rec, state->private_data);
1446 static int db_ctdb_traverse_read(struct db_context *db,
1447 int (*fn)(struct db_record *rec,
1448 void *private_data),
1449 void *private_data)
1451 NTSTATUS status;
1452 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1453 struct db_ctdb_ctx);
1454 struct traverse_state state;
1456 state.db = db;
1457 state.fn = fn;
1458 state.private_data = private_data;
1459 state.count = 0;
1461 if (db->persistent) {
1462 /* for persistent databases we don't need to do a ctdb traverse,
1463 we can do a faster local traverse */
1464 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1467 status = ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1468 if (!NT_STATUS_IS_OK(status)) {
1469 return -1;
1471 return state.count;
1474 static int db_ctdb_get_seqnum(struct db_context *db)
1476 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1477 struct db_ctdb_ctx);
1478 return tdb_get_seqnum(ctx->wtdb->tdb);
1481 static void db_ctdb_id(struct db_context *db, const uint8_t **id,
1482 size_t *idlen)
1484 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1485 db->private_data, struct db_ctdb_ctx);
1487 *id = (uint8_t *)&ctx->db_id;
1488 *idlen = sizeof(ctx->db_id);
1491 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1492 const char *name,
1493 int hash_size, int tdb_flags,
1494 int open_flags, mode_t mode,
1495 enum dbwrap_lock_order lock_order)
1497 struct db_context *result;
1498 struct db_ctdb_ctx *db_ctdb;
1499 char *db_path;
1500 struct ctdbd_connection *conn;
1501 struct loadparm_context *lp_ctx;
1502 struct ctdb_db_priority prio;
1503 NTSTATUS status;
1504 int cstatus;
1506 if (!lp_clustering()) {
1507 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1508 return NULL;
1511 if (!(result = talloc_zero(mem_ctx, struct db_context))) {
1512 DEBUG(0, ("talloc failed\n"));
1513 TALLOC_FREE(result);
1514 return NULL;
1517 if (!(db_ctdb = talloc(result, struct db_ctdb_ctx))) {
1518 DEBUG(0, ("talloc failed\n"));
1519 TALLOC_FREE(result);
1520 return NULL;
1523 result->name = talloc_strdup(result, name);
1524 if (result->name == NULL) {
1525 DEBUG(0, ("talloc failed\n"));
1526 TALLOC_FREE(result);
1527 return NULL;
1530 db_ctdb->transaction = NULL;
1531 db_ctdb->db = result;
1533 conn = messaging_ctdbd_connection();
1534 if (conn == NULL) {
1535 DEBUG(1, ("Could not connect to ctdb\n"));
1536 TALLOC_FREE(result);
1537 return NULL;
1540 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1541 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1542 TALLOC_FREE(result);
1543 return NULL;
1546 db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1548 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1549 result->lock_order = lock_order;
1551 /* only pass through specific flags */
1552 tdb_flags &= TDB_SEQNUM;
1554 /* honor permissions if user has specified O_CREAT */
1555 if (open_flags & O_CREAT) {
1556 chmod(db_path, mode);
1559 prio.db_id = db_ctdb->db_id;
1560 prio.priority = lock_order;
1562 status = ctdbd_control_local(
1563 conn, CTDB_CONTROL_SET_DB_PRIORITY, 0, 0,
1564 make_tdb_data((uint8_t *)&prio, sizeof(prio)),
1565 NULL, NULL, &cstatus);
1567 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1568 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1569 nt_errstr(status), cstatus));
1570 TALLOC_FREE(result);
1571 return NULL;
1574 lp_ctx = loadparm_init_s3(db_path, loadparm_s3_helpers());
1576 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags,
1577 O_RDWR, 0, lp_ctx);
1578 talloc_unlink(db_path, lp_ctx);
1579 if (db_ctdb->wtdb == NULL) {
1580 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1581 TALLOC_FREE(result);
1582 return NULL;
1584 talloc_free(db_path);
1586 if (result->persistent) {
1587 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1588 ctdb_conn_msg_ctx(conn));
1589 if (db_ctdb->lock_ctx == NULL) {
1590 DEBUG(0, ("g_lock_ctx_init failed\n"));
1591 TALLOC_FREE(result);
1592 return NULL;
1596 result->private_data = (void *)db_ctdb;
1597 result->fetch_locked = db_ctdb_fetch_locked;
1598 result->try_fetch_locked = db_ctdb_try_fetch_locked;
1599 result->parse_record = db_ctdb_parse_record;
1600 result->traverse = db_ctdb_traverse;
1601 result->traverse_read = db_ctdb_traverse_read;
1602 result->get_seqnum = db_ctdb_get_seqnum;
1603 result->transaction_start = db_ctdb_transaction_start;
1604 result->transaction_commit = db_ctdb_transaction_commit;
1605 result->transaction_cancel = db_ctdb_transaction_cancel;
1606 result->id = db_ctdb_id;
1607 result->stored_callback = NULL;
1609 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1610 name, db_ctdb->db_id));
1612 return result;
1615 #else /* CLUSTER_SUPPORT */
1617 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1618 const char *name,
1619 int hash_size, int tdb_flags,
1620 int open_flags, mode_t mode,
1621 enum dbwrap_lock_order lock_order)
1623 DEBUG(3, ("db_open_ctdb: no cluster support!\n"));
1624 errno = ENOSYS;
1625 return NULL;
1628 #endif