dbwrap-ctdb: Avoid a talloc_stackframe()
[Samba/vl.git] / source3 / lib / dbwrap / dbwrap_ctdb.c
blobe55689cad56c26120e389aea6f340a3552850f2b
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 TDB_DATA rec;
144 int ret;
146 rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
147 rec.dptr = (uint8_t *)talloc_size(talloc_tos(), rec.dsize);
149 if (rec.dptr == NULL) {
150 return NT_STATUS_NO_MEMORY;
153 memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
154 memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
156 ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
158 talloc_free(rec.dptr);
160 return (ret == 0) ? NT_STATUS_OK
161 : tdb_error_to_ntstatus(db->wtdb->tdb);
166 form a ctdb_rec_data record from a key/data pair
168 static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
169 TDB_DATA key,
170 struct ctdb_ltdb_header *header,
171 TDB_DATA data)
173 size_t length;
174 struct ctdb_rec_data *d;
176 length = offsetof(struct ctdb_rec_data, data) + key.dsize +
177 data.dsize + sizeof(*header);
178 d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
179 if (d == NULL) {
180 return NULL;
182 d->length = length;
183 d->reqid = reqid;
184 d->keylen = key.dsize;
185 memcpy(&d->data[0], key.dptr, key.dsize);
187 d->datalen = data.dsize + sizeof(*header);
188 memcpy(&d->data[key.dsize], header, sizeof(*header));
189 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
190 return d;
194 /* helper function for marshalling multiple records */
195 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
196 struct ctdb_marshall_buffer *m,
197 uint64_t db_id,
198 uint32_t reqid,
199 TDB_DATA key,
200 struct ctdb_ltdb_header *header,
201 TDB_DATA data)
203 struct ctdb_rec_data *r;
204 size_t m_size, r_size;
205 struct ctdb_marshall_buffer *m2 = NULL;
207 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
208 if (r == NULL) {
209 talloc_free(m);
210 return NULL;
213 if (m == NULL) {
214 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
215 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
216 if (m == NULL) {
217 goto done;
219 m->db_id = db_id;
222 m_size = talloc_get_size(m);
223 r_size = talloc_get_size(r);
225 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
226 mem_ctx, m, m_size + r_size);
227 if (m2 == NULL) {
228 talloc_free(m);
229 goto done;
232 memcpy(m_size + (uint8_t *)m2, r, r_size);
234 m2->count++;
236 done:
237 talloc_free(r);
238 return m2;
241 /* we've finished marshalling, return a data blob with the marshalled records */
242 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
244 TDB_DATA data;
245 data.dptr = (uint8_t *)m;
246 data.dsize = talloc_get_size(m);
247 return data;
251 loop over a marshalling buffer
253 - pass r==NULL to start
254 - loop the number of times indicated by m->count
256 static struct ctdb_rec_data *db_ctdb_marshall_loop_next_key(
257 struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r, TDB_DATA *key)
259 if (r == NULL) {
260 r = (struct ctdb_rec_data *)&m->data[0];
261 } else {
262 r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
265 key->dptr = &r->data[0];
266 key->dsize = r->keylen;
267 return r;
270 static bool db_ctdb_marshall_buf_parse(
271 struct ctdb_rec_data *r, uint32_t *reqid,
272 struct ctdb_ltdb_header **header, TDB_DATA *data)
274 if (r->datalen < sizeof(struct ctdb_ltdb_header)) {
275 return false;
278 *reqid = r->reqid;
280 data->dptr = &r->data[r->keylen] + sizeof(struct ctdb_ltdb_header);
281 data->dsize = r->datalen - sizeof(struct ctdb_ltdb_header);
283 *header = (struct ctdb_ltdb_header *)&r->data[r->keylen];
285 return true;
289 * CTDB transaction destructor
291 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
293 NTSTATUS status;
295 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
296 if (!NT_STATUS_IS_OK(status)) {
297 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h->lock_name,
298 nt_errstr(status)));
299 return -1;
301 return 0;
305 * CTDB dbwrap API: transaction_start function
306 * starts a transaction on a persistent database
308 static int db_ctdb_transaction_start(struct db_context *db)
310 struct db_ctdb_transaction_handle *h;
311 NTSTATUS status;
312 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
313 struct db_ctdb_ctx);
315 if (!db->persistent) {
316 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
317 ctx->db_id));
318 return -1;
321 if (ctx->transaction) {
322 ctx->transaction->nesting++;
323 DEBUG(5, (__location__ " transaction start on db 0x%08x: nesting %d -> %d\n",
324 ctx->db_id, ctx->transaction->nesting - 1, ctx->transaction->nesting));
325 return 0;
328 h = talloc_zero(db, struct db_ctdb_transaction_handle);
329 if (h == NULL) {
330 DEBUG(0,(__location__ " oom for transaction handle\n"));
331 return -1;
334 h->ctx = ctx;
336 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
337 (unsigned int)ctx->db_id);
338 if (h->lock_name == NULL) {
339 DEBUG(0, ("talloc_asprintf failed\n"));
340 TALLOC_FREE(h);
341 return -1;
345 * Wait a day, i.e. forever...
347 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
348 timeval_set(86400, 0));
349 if (!NT_STATUS_IS_OK(status)) {
350 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
351 TALLOC_FREE(h);
352 return -1;
355 talloc_set_destructor(h, db_ctdb_transaction_destructor);
357 ctx->transaction = h;
359 DEBUG(5,(__location__ " transaction started on db 0x%08x\n", ctx->db_id));
361 return 0;
364 static bool parse_newest_in_marshall_buffer(
365 struct ctdb_marshall_buffer *buf, TDB_DATA key,
366 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
367 TDB_DATA data, void *private_data),
368 void *private_data)
370 struct ctdb_rec_data *rec = NULL;
371 struct ctdb_ltdb_header *h = NULL;
372 TDB_DATA data;
373 int i;
375 if (buf == NULL) {
376 return false;
380 * Walk the list of records written during this
381 * transaction. If we want to read one we have already
382 * written, return the last written sample. Thus we do not do
383 * a "break;" for the first hit, this record might have been
384 * overwritten later.
387 for (i=0; i<buf->count; i++) {
388 TDB_DATA tkey;
389 uint32_t reqid;
391 rec = db_ctdb_marshall_loop_next_key(buf, rec, &tkey);
392 if (rec == NULL) {
393 return false;
396 if (!tdb_data_equal(key, tkey)) {
397 continue;
400 if (!db_ctdb_marshall_buf_parse(rec, &reqid, &h, &data)) {
401 return false;
405 if (h == NULL) {
406 return false;
409 parser(key, h, data, private_data);
411 return true;
414 struct pull_newest_from_marshall_buffer_state {
415 struct ctdb_ltdb_header *pheader;
416 TALLOC_CTX *mem_ctx;
417 TDB_DATA *pdata;
420 static void pull_newest_from_marshall_buffer_parser(
421 TDB_DATA key, struct ctdb_ltdb_header *header,
422 TDB_DATA data, void *private_data)
424 struct pull_newest_from_marshall_buffer_state *state =
425 (struct pull_newest_from_marshall_buffer_state *)private_data;
427 if (state->pheader != NULL) {
428 memcpy(state->pheader, header, sizeof(*state->pheader));
430 if (state->pdata != NULL) {
431 state->pdata->dsize = data.dsize;
432 state->pdata->dptr = (uint8_t *)talloc_memdup(
433 state->mem_ctx, data.dptr, data.dsize);
437 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
438 TDB_DATA key,
439 struct ctdb_ltdb_header *pheader,
440 TALLOC_CTX *mem_ctx,
441 TDB_DATA *pdata)
443 struct pull_newest_from_marshall_buffer_state state;
445 state.pheader = pheader;
446 state.mem_ctx = mem_ctx;
447 state.pdata = pdata;
449 if (!parse_newest_in_marshall_buffer(
450 buf, key, pull_newest_from_marshall_buffer_parser,
451 &state)) {
452 return false;
454 if ((pdata != NULL) && (pdata->dsize != 0) && (pdata->dptr == NULL)) {
455 /* ENOMEM */
456 return false;
458 return true;
461 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
462 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
464 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
465 TALLOC_CTX *mem_ctx,
466 TDB_DATA key)
468 struct db_record *result;
469 TDB_DATA ctdb_data;
471 if (!(result = talloc(mem_ctx, struct db_record))) {
472 DEBUG(0, ("talloc failed\n"));
473 return NULL;
476 result->db = ctx->db;
477 result->private_data = ctx->transaction;
479 result->key.dsize = key.dsize;
480 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
481 key.dsize);
482 if (result->key.dptr == NULL) {
483 DEBUG(0, ("talloc failed\n"));
484 TALLOC_FREE(result);
485 return NULL;
488 result->store = db_ctdb_store_transaction;
489 result->delete_rec = db_ctdb_delete_transaction;
491 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
492 NULL, result, &result->value)) {
493 return result;
496 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
497 if (ctdb_data.dptr == NULL) {
498 /* create the record */
499 result->value = tdb_null;
500 return result;
503 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
504 result->value.dptr = NULL;
506 if ((result->value.dsize != 0)
507 && !(result->value.dptr = (uint8_t *)talloc_memdup(
508 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
509 result->value.dsize))) {
510 DEBUG(0, ("talloc failed\n"));
511 TALLOC_FREE(result);
514 SAFE_FREE(ctdb_data.dptr);
516 return result;
519 static int db_ctdb_record_destructor(struct db_record **recp)
521 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
522 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
523 rec->private_data, struct db_ctdb_transaction_handle);
524 int ret = h->ctx->db->transaction_commit(h->ctx->db);
525 if (ret != 0) {
526 DEBUG(0,(__location__ " transaction_commit failed\n"));
528 return 0;
532 auto-create a transaction for persistent databases
534 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
535 TALLOC_CTX *mem_ctx,
536 TDB_DATA key)
538 int res;
539 struct db_record *rec, **recp;
541 res = db_ctdb_transaction_start(ctx->db);
542 if (res == -1) {
543 return NULL;
546 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
547 if (rec == NULL) {
548 ctx->db->transaction_cancel(ctx->db);
549 return NULL;
552 /* destroy this transaction when we release the lock */
553 recp = talloc(rec, struct db_record *);
554 if (recp == NULL) {
555 ctx->db->transaction_cancel(ctx->db);
556 talloc_free(rec);
557 return NULL;
559 *recp = rec;
560 talloc_set_destructor(recp, db_ctdb_record_destructor);
561 return rec;
566 stores a record inside a transaction
568 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
569 TDB_DATA key, TDB_DATA data)
571 TALLOC_CTX *tmp_ctx = talloc_new(h);
572 TDB_DATA rec;
573 struct ctdb_ltdb_header header;
575 ZERO_STRUCT(header);
577 /* we need the header so we can update the RSN */
579 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
580 NULL, NULL)) {
582 rec = tdb_fetch_compat(h->ctx->wtdb->tdb, key);
584 if (rec.dptr != NULL) {
585 memcpy(&header, rec.dptr,
586 sizeof(struct ctdb_ltdb_header));
587 rec.dsize -= sizeof(struct ctdb_ltdb_header);
590 * a special case, we are writing the same
591 * data that is there now
593 if (data.dsize == rec.dsize &&
594 memcmp(data.dptr,
595 rec.dptr + sizeof(struct ctdb_ltdb_header),
596 data.dsize) == 0) {
597 SAFE_FREE(rec.dptr);
598 talloc_free(tmp_ctx);
599 return NT_STATUS_OK;
602 SAFE_FREE(rec.dptr);
605 header.dmaster = get_my_vnn();
606 header.rsn++;
608 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
609 if (h->m_write == NULL) {
610 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
611 talloc_free(tmp_ctx);
612 return NT_STATUS_NO_MEMORY;
615 talloc_free(tmp_ctx);
616 return NT_STATUS_OK;
621 a record store inside a transaction
623 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
625 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
626 rec->private_data, struct db_ctdb_transaction_handle);
627 NTSTATUS status;
629 status = db_ctdb_transaction_store(h, rec->key, data);
630 return status;
634 a record delete inside a transaction
636 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
638 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
639 rec->private_data, struct db_ctdb_transaction_handle);
640 NTSTATUS status;
642 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
643 return status;
646 static void db_ctdb_fetch_db_seqnum_parser(
647 TDB_DATA key, struct ctdb_ltdb_header *header,
648 TDB_DATA data, void *private_data)
650 uint64_t *seqnum = (uint64_t *)private_data;
652 if (data.dsize != sizeof(uint64_t)) {
653 *seqnum = 0;
654 return;
656 memcpy(seqnum, data.dptr, sizeof(*seqnum));
660 * Fetch the db sequence number of a persistent db directly from the db.
662 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
663 uint64_t *seqnum)
665 NTSTATUS status;
666 TDB_DATA key;
668 if (seqnum == NULL) {
669 return NT_STATUS_INVALID_PARAMETER;
672 key = string_term_tdb_data(CTDB_DB_SEQNUM_KEY);
674 status = db_ctdb_ltdb_parse(
675 db, key, db_ctdb_fetch_db_seqnum_parser, seqnum);
677 if (NT_STATUS_IS_OK(status)) {
678 return NT_STATUS_OK;
680 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
681 *seqnum = 0;
682 return NT_STATUS_OK;
684 return status;
688 * Store the database sequence number inside a transaction.
690 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
691 uint64_t seqnum)
693 NTSTATUS status;
694 const char *keyname = CTDB_DB_SEQNUM_KEY;
695 TDB_DATA key;
696 TDB_DATA data;
698 key = string_term_tdb_data(keyname);
700 data.dptr = (uint8_t *)&seqnum;
701 data.dsize = sizeof(uint64_t);
703 status = db_ctdb_transaction_store(h, key, data);
705 return status;
709 commit a transaction
711 static int db_ctdb_transaction_commit(struct db_context *db)
713 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
714 struct db_ctdb_ctx);
715 NTSTATUS rets;
716 int status;
717 struct db_ctdb_transaction_handle *h = ctx->transaction;
718 uint64_t old_seqnum, new_seqnum;
719 int ret;
721 if (h == NULL) {
722 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
723 return -1;
726 if (h->nested_cancel) {
727 db->transaction_cancel(db);
728 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
729 return -1;
732 if (h->nesting != 0) {
733 h->nesting--;
734 DEBUG(5, (__location__ " transaction commit on db 0x%08x: nesting %d -> %d\n",
735 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
736 return 0;
739 if (h->m_write == NULL) {
741 * No changes were made, so don't change the seqnum,
742 * don't push to other node, just exit with success.
744 ret = 0;
745 goto done;
748 DEBUG(5,(__location__ " transaction commit on db 0x%08x\n", ctx->db_id));
751 * As the last db action before committing, bump the database sequence
752 * number. Note that this undoes all changes to the seqnum records
753 * performed under the transaction. This record is not meant to be
754 * modified by user interaction. It is for internal use only...
756 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
757 if (!NT_STATUS_IS_OK(rets)) {
758 DEBUG(1, (__location__ " failed to fetch the db sequence number "
759 "in transaction commit on db 0x%08x\n", ctx->db_id));
760 ret = -1;
761 goto done;
764 new_seqnum = old_seqnum + 1;
766 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
767 if (!NT_STATUS_IS_OK(rets)) {
768 DEBUG(1, (__location__ "failed to store the db sequence number "
769 " in transaction commit on db 0x%08x\n", ctx->db_id));
770 ret = -1;
771 goto done;
774 again:
775 /* tell ctdbd to commit to the other nodes */
776 rets = ctdbd_control_local(messaging_ctdbd_connection(),
777 CTDB_CONTROL_TRANS3_COMMIT,
778 h->ctx->db_id, 0,
779 db_ctdb_marshall_finish(h->m_write),
780 NULL, NULL, &status);
781 if (!NT_STATUS_IS_OK(rets) || status != 0) {
783 * The TRANS3_COMMIT control should only possibly fail when a
784 * recovery has been running concurrently. In any case, the db
785 * will be the same on all nodes, either the new copy or the
786 * old copy. This can be detected by comparing the old and new
787 * local sequence numbers.
789 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
790 if (!NT_STATUS_IS_OK(rets)) {
791 DEBUG(1, (__location__ " failed to refetch db sequence "
792 "number after failed TRANS3_COMMIT\n"));
793 ret = -1;
794 goto done;
797 if (new_seqnum == old_seqnum) {
798 /* Recovery prevented all our changes: retry. */
799 goto again;
801 if (new_seqnum != (old_seqnum + 1)) {
802 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
803 "old_seqnum[%lu] + (0 or 1) after failed "
804 "TRANS3_COMMIT - this should not happen!\n",
805 (unsigned long)new_seqnum,
806 (unsigned long)old_seqnum));
807 ret = -1;
808 goto done;
811 * Recovery propagated our changes to all nodes, completing
812 * our commit for us - succeed.
816 ret = 0;
818 done:
819 h->ctx->transaction = NULL;
820 talloc_free(h);
821 return ret;
826 cancel a transaction
828 static int db_ctdb_transaction_cancel(struct db_context *db)
830 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
831 struct db_ctdb_ctx);
832 struct db_ctdb_transaction_handle *h = ctx->transaction;
834 if (h == NULL) {
835 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
836 return -1;
839 if (h->nesting != 0) {
840 h->nesting--;
841 h->nested_cancel = true;
842 DEBUG(5, (__location__ " transaction cancel on db 0x%08x: nesting %d -> %d\n",
843 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
844 return 0;
847 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
849 ctx->transaction = NULL;
850 talloc_free(h);
851 return 0;
855 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
857 struct db_ctdb_rec *crec = talloc_get_type_abort(
858 rec->private_data, struct db_ctdb_rec);
860 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
865 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
866 static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
868 NTSTATUS status;
869 struct ctdb_control_schedule_for_deletion *dd;
870 TDB_DATA indata;
871 int cstatus;
872 struct db_ctdb_rec *crec = talloc_get_type_abort(
873 rec->private_data, struct db_ctdb_rec);
875 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
876 indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
877 if (indata.dptr == NULL) {
878 DEBUG(0, (__location__ " talloc failed!\n"));
879 return NT_STATUS_NO_MEMORY;
882 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
883 dd->db_id = crec->ctdb_ctx->db_id;
884 dd->hdr = crec->header;
885 dd->keylen = rec->key.dsize;
886 memcpy(dd->key, rec->key.dptr, rec->key.dsize);
888 status = ctdbd_control_local(messaging_ctdbd_connection(),
889 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
890 crec->ctdb_ctx->db_id,
891 CTDB_CTRL_FLAG_NOREPLY, /* flags */
892 indata,
893 NULL, /* outdata */
894 NULL, /* errmsg */
895 &cstatus);
896 talloc_free(indata.dptr);
898 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
899 DEBUG(1, (__location__ " Error sending local control "
900 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
901 nt_errstr(status), cstatus));
902 if (NT_STATUS_IS_OK(status)) {
903 status = NT_STATUS_UNSUCCESSFUL;
907 return status;
909 #endif
911 static NTSTATUS db_ctdb_delete(struct db_record *rec)
913 NTSTATUS status;
916 * We have to store the header with empty data. TODO: Fix the
917 * tdb-level cleanup
920 status = db_ctdb_store(rec, tdb_null, 0);
921 if (!NT_STATUS_IS_OK(status)) {
922 return status;
925 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
926 status = db_ctdb_send_schedule_for_deletion(rec);
927 #endif
929 return status;
932 static int db_ctdb_record_destr(struct db_record* data)
934 struct db_ctdb_rec *crec = talloc_get_type_abort(
935 data->private_data, struct db_ctdb_rec);
936 int threshold;
938 DEBUG(10, (DEBUGLEVEL > 10
939 ? "Unlocking db %u key %s\n"
940 : "Unlocking db %u key %.20s\n",
941 (int)crec->ctdb_ctx->db_id,
942 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
943 data->key.dsize)));
945 tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key);
947 threshold = lp_ctdb_locktime_warn_threshold();
948 if (threshold != 0) {
949 double timediff = timeval_elapsed(&crec->lock_time);
950 if ((timediff * 1000) > threshold) {
951 const char *key;
953 key = hex_encode_talloc(data,
954 (unsigned char *)data->key.dptr,
955 data->key.dsize);
956 DEBUG(0, ("Held tdb lock on db %s, key %s %f seconds\n",
957 tdb_name(crec->ctdb_ctx->wtdb->tdb), key,
958 timediff));
962 return 0;
966 * Check whether we have a valid local copy of the given record,
967 * either for reading or for writing.
969 static bool db_ctdb_can_use_local_hdr(const struct ctdb_ltdb_header *hdr,
970 bool read_only)
972 #ifdef HAVE_CTDB_WANT_READONLY_DECL
973 if (hdr->dmaster != get_my_vnn()) {
974 /* If we're not dmaster, it must be r/o copy. */
975 return read_only && (hdr->flags & CTDB_REC_RO_HAVE_READONLY);
979 * If we want write access, no one may have r/o copies.
981 return read_only || !(hdr->flags & CTDB_REC_RO_HAVE_DELEGATIONS);
982 #else
983 return (hdr->dmaster == get_my_vnn());
984 #endif
987 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data, bool read_only)
989 if (ctdb_data.dptr == NULL)
990 return false;
992 if (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header))
993 return false;
995 return db_ctdb_can_use_local_hdr(
996 (struct ctdb_ltdb_header *)ctdb_data.dptr, read_only);
999 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
1000 TALLOC_CTX *mem_ctx,
1001 TDB_DATA key,
1002 bool tryonly)
1004 struct db_record *result;
1005 struct db_ctdb_rec *crec;
1006 NTSTATUS status;
1007 TDB_DATA ctdb_data;
1008 int migrate_attempts = 0;
1009 int lockret;
1011 if (!(result = talloc(mem_ctx, struct db_record))) {
1012 DEBUG(0, ("talloc failed\n"));
1013 return NULL;
1016 if (!(crec = talloc_zero(result, struct db_ctdb_rec))) {
1017 DEBUG(0, ("talloc failed\n"));
1018 TALLOC_FREE(result);
1019 return NULL;
1022 result->db = ctx->db;
1023 result->private_data = (void *)crec;
1024 crec->ctdb_ctx = ctx;
1026 result->key.dsize = key.dsize;
1027 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
1028 key.dsize);
1029 if (result->key.dptr == NULL) {
1030 DEBUG(0, ("talloc failed\n"));
1031 TALLOC_FREE(result);
1032 return NULL;
1036 * Do a blocking lock on the record
1038 again:
1040 if (DEBUGLEVEL >= 10) {
1041 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1042 DEBUG(10, (DEBUGLEVEL > 10
1043 ? "Locking db %u key %s\n"
1044 : "Locking db %u key %.20s\n",
1045 (int)crec->ctdb_ctx->db_id, keystr));
1046 TALLOC_FREE(keystr);
1049 lockret = tryonly
1050 ? tdb_chainlock_nonblock(ctx->wtdb->tdb, key)
1051 : tdb_chainlock(ctx->wtdb->tdb, key);
1052 if (lockret != 0) {
1053 DEBUG(3, ("tdb_chainlock failed\n"));
1054 TALLOC_FREE(result);
1055 return NULL;
1058 result->store = db_ctdb_store;
1059 result->delete_rec = db_ctdb_delete;
1060 talloc_set_destructor(result, db_ctdb_record_destr);
1062 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
1065 * See if we have a valid record and we are the dmaster. If so, we can
1066 * take the shortcut and just return it.
1069 if (!db_ctdb_can_use_local_copy(ctdb_data, false)) {
1070 SAFE_FREE(ctdb_data.dptr);
1071 tdb_chainunlock(ctx->wtdb->tdb, key);
1072 talloc_set_destructor(result, NULL);
1074 if (tryonly && (migrate_attempts != 0)) {
1075 DEBUG(5, ("record migrated away again\n"));
1076 TALLOC_FREE(result);
1077 return NULL;
1080 migrate_attempts += 1;
1082 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1083 ctdb_data.dptr, ctdb_data.dptr ?
1084 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
1085 get_my_vnn(),
1086 ctdb_data.dptr ?
1087 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->flags : 0));
1089 status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id,
1090 key);
1091 if (!NT_STATUS_IS_OK(status)) {
1092 DEBUG(5, ("ctdb_migrate failed: %s\n",
1093 nt_errstr(status)));
1094 TALLOC_FREE(result);
1095 return NULL;
1097 /* now its migrated, try again */
1098 goto again;
1101 if (migrate_attempts > 10) {
1102 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s needed %d "
1103 "attempts\n", tdb_name(ctx->wtdb->tdb),
1104 hex_encode_talloc(talloc_tos(),
1105 (unsigned char *)key.dptr,
1106 key.dsize),
1107 migrate_attempts));
1110 GetTimeOfDay(&crec->lock_time);
1112 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1114 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1115 result->value.dptr = NULL;
1117 if ((result->value.dsize != 0)
1118 && !(result->value.dptr = (uint8_t *)talloc_memdup(
1119 result, ctdb_data.dptr + sizeof(crec->header),
1120 result->value.dsize))) {
1121 DEBUG(0, ("talloc failed\n"));
1122 TALLOC_FREE(result);
1125 SAFE_FREE(ctdb_data.dptr);
1127 return result;
1130 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1131 TALLOC_CTX *mem_ctx,
1132 TDB_DATA key)
1134 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1135 struct db_ctdb_ctx);
1137 if (ctx->transaction != NULL) {
1138 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1141 if (db->persistent) {
1142 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1145 return fetch_locked_internal(ctx, mem_ctx, key, false);
1148 static struct db_record *db_ctdb_try_fetch_locked(struct db_context *db,
1149 TALLOC_CTX *mem_ctx,
1150 TDB_DATA key)
1152 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1153 struct db_ctdb_ctx);
1155 if (ctx->transaction != NULL) {
1156 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1159 if (db->persistent) {
1160 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1163 return fetch_locked_internal(ctx, mem_ctx, key, true);
1166 struct db_ctdb_parse_record_state {
1167 void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data);
1168 void *private_data;
1169 bool ask_for_readonly_copy;
1170 bool done;
1173 static void db_ctdb_parse_record_parser(
1174 TDB_DATA key, struct ctdb_ltdb_header *header,
1175 TDB_DATA data, void *private_data)
1177 struct db_ctdb_parse_record_state *state =
1178 (struct db_ctdb_parse_record_state *)private_data;
1179 state->parser(key, data, state->private_data);
1182 static void db_ctdb_parse_record_parser_nonpersistent(
1183 TDB_DATA key, struct ctdb_ltdb_header *header,
1184 TDB_DATA data, void *private_data)
1186 struct db_ctdb_parse_record_state *state =
1187 (struct db_ctdb_parse_record_state *)private_data;
1189 if (db_ctdb_can_use_local_hdr(header, true)) {
1190 state->parser(key, data, state->private_data);
1191 state->done = true;
1192 } else {
1194 * We found something in the db, so it seems that this record,
1195 * while not usable locally right now, is popular. Ask for a
1196 * R/O copy.
1198 state->ask_for_readonly_copy = true;
1202 static NTSTATUS db_ctdb_parse_record(struct db_context *db, TDB_DATA key,
1203 void (*parser)(TDB_DATA key,
1204 TDB_DATA data,
1205 void *private_data),
1206 void *private_data)
1208 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1209 db->private_data, struct db_ctdb_ctx);
1210 struct db_ctdb_parse_record_state state;
1211 NTSTATUS status;
1213 state.parser = parser;
1214 state.private_data = private_data;
1216 if (ctx->transaction != NULL) {
1217 struct db_ctdb_transaction_handle *h = ctx->transaction;
1218 bool found;
1221 * Transactions only happen for persistent db's.
1224 found = parse_newest_in_marshall_buffer(
1225 h->m_write, key, db_ctdb_parse_record_parser, &state);
1227 if (found) {
1228 return NT_STATUS_OK;
1232 if (db->persistent) {
1234 * Persistent db, but not found in the transaction buffer
1236 return db_ctdb_ltdb_parse(
1237 ctx, key, db_ctdb_parse_record_parser, &state);
1240 state.done = false;
1241 state.ask_for_readonly_copy = false;
1243 status = db_ctdb_ltdb_parse(
1244 ctx, key, db_ctdb_parse_record_parser_nonpersistent, &state);
1245 if (NT_STATUS_IS_OK(status) && state.done) {
1246 return NT_STATUS_OK;
1249 return ctdbd_parse(messaging_ctdbd_connection(), ctx->db_id, key,
1250 state.ask_for_readonly_copy, parser, private_data);
1253 struct traverse_state {
1254 struct db_context *db;
1255 int (*fn)(struct db_record *rec, void *private_data);
1256 void *private_data;
1257 int count;
1260 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1262 struct traverse_state *state = (struct traverse_state *)private_data;
1263 struct db_record *rec;
1264 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1265 /* we have to give them a locked record to prevent races */
1266 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1267 if (rec && rec->value.dsize > 0) {
1268 state->fn(rec, state->private_data);
1270 talloc_free(tmp_ctx);
1273 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1274 void *private_data)
1276 struct traverse_state *state = (struct traverse_state *)private_data;
1277 struct db_record *rec;
1278 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1279 int ret = 0;
1282 * Skip the __db_sequence_number__ key:
1283 * This is used for persistent transactions internally.
1285 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1286 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1288 goto done;
1291 /* we have to give them a locked record to prevent races */
1292 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1293 if (rec && rec->value.dsize > 0) {
1294 ret = state->fn(rec, state->private_data);
1297 done:
1298 talloc_free(tmp_ctx);
1299 return ret;
1302 /* wrapper to use traverse_persistent_callback with dbwrap */
1303 static int traverse_persistent_callback_dbwrap(struct db_record *rec, void* data)
1305 return traverse_persistent_callback(NULL, rec->key, rec->value, data);
1309 static int db_ctdb_traverse(struct db_context *db,
1310 int (*fn)(struct db_record *rec,
1311 void *private_data),
1312 void *private_data)
1314 NTSTATUS status;
1315 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1316 struct db_ctdb_ctx);
1317 struct traverse_state state;
1319 state.db = db;
1320 state.fn = fn;
1321 state.private_data = private_data;
1322 state.count = 0;
1324 if (db->persistent) {
1325 struct tdb_context *ltdb = ctx->wtdb->tdb;
1326 int ret;
1328 /* for persistent databases we don't need to do a ctdb traverse,
1329 we can do a faster local traverse */
1330 ret = tdb_traverse(ltdb, traverse_persistent_callback, &state);
1331 if (ret < 0) {
1332 return ret;
1334 if (ctx->transaction && ctx->transaction->m_write) {
1336 * we now have to handle keys not yet
1337 * present at transaction start
1339 struct db_context *newkeys = db_open_rbt(talloc_tos());
1340 struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
1341 struct ctdb_rec_data *rec=NULL;
1342 int i;
1343 int count = 0;
1345 if (newkeys == NULL) {
1346 return -1;
1349 for (i=0; i<mbuf->count; i++) {
1350 TDB_DATA key;
1351 rec = db_ctdb_marshall_loop_next_key(
1352 mbuf, rec, &key);
1353 SMB_ASSERT(rec != NULL);
1355 if (!tdb_exists(ltdb, key)) {
1356 dbwrap_store(newkeys, key, tdb_null, 0);
1359 status = dbwrap_traverse(newkeys,
1360 traverse_persistent_callback_dbwrap,
1361 &state,
1362 &count);
1363 talloc_free(newkeys);
1364 if (!NT_STATUS_IS_OK(status)) {
1365 return -1;
1367 ret += count;
1369 return ret;
1372 status = ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1373 if (!NT_STATUS_IS_OK(status)) {
1374 return -1;
1376 return state.count;
1379 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1381 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1384 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1386 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1389 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1391 struct traverse_state *state = (struct traverse_state *)private_data;
1392 struct db_record rec;
1394 ZERO_STRUCT(rec);
1395 rec.db = state->db;
1396 rec.key = key;
1397 rec.value = data;
1398 rec.store = db_ctdb_store_deny;
1399 rec.delete_rec = db_ctdb_delete_deny;
1400 rec.private_data = NULL;
1401 state->fn(&rec, state->private_data);
1402 state->count++;
1405 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1406 void *private_data)
1408 struct traverse_state *state = (struct traverse_state *)private_data;
1409 struct db_record rec;
1412 * Skip the __db_sequence_number__ key:
1413 * This is used for persistent transactions internally.
1415 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1416 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1418 return 0;
1421 ZERO_STRUCT(rec);
1422 rec.db = state->db;
1423 rec.key = kbuf;
1424 rec.value = dbuf;
1425 rec.store = db_ctdb_store_deny;
1426 rec.delete_rec = db_ctdb_delete_deny;
1427 rec.private_data = NULL;
1429 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1430 /* a deleted record */
1431 return 0;
1433 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1434 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1436 state->count++;
1437 return state->fn(&rec, state->private_data);
1440 static int db_ctdb_traverse_read(struct db_context *db,
1441 int (*fn)(struct db_record *rec,
1442 void *private_data),
1443 void *private_data)
1445 NTSTATUS status;
1446 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1447 struct db_ctdb_ctx);
1448 struct traverse_state state;
1450 state.db = db;
1451 state.fn = fn;
1452 state.private_data = private_data;
1453 state.count = 0;
1455 if (db->persistent) {
1456 /* for persistent databases we don't need to do a ctdb traverse,
1457 we can do a faster local traverse */
1458 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1461 status = ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1462 if (!NT_STATUS_IS_OK(status)) {
1463 return -1;
1465 return state.count;
1468 static int db_ctdb_get_seqnum(struct db_context *db)
1470 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1471 struct db_ctdb_ctx);
1472 return tdb_get_seqnum(ctx->wtdb->tdb);
1475 static void db_ctdb_id(struct db_context *db, const uint8_t **id,
1476 size_t *idlen)
1478 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1479 db->private_data, struct db_ctdb_ctx);
1481 *id = (uint8_t *)&ctx->db_id;
1482 *idlen = sizeof(ctx->db_id);
1485 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1486 const char *name,
1487 int hash_size, int tdb_flags,
1488 int open_flags, mode_t mode,
1489 enum dbwrap_lock_order lock_order)
1491 struct db_context *result;
1492 struct db_ctdb_ctx *db_ctdb;
1493 char *db_path;
1494 struct ctdbd_connection *conn;
1495 struct loadparm_context *lp_ctx;
1496 struct ctdb_db_priority prio;
1497 NTSTATUS status;
1498 int cstatus;
1500 if (!lp_clustering()) {
1501 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1502 return NULL;
1505 if (!(result = talloc_zero(mem_ctx, struct db_context))) {
1506 DEBUG(0, ("talloc failed\n"));
1507 TALLOC_FREE(result);
1508 return NULL;
1511 if (!(db_ctdb = talloc(result, struct db_ctdb_ctx))) {
1512 DEBUG(0, ("talloc failed\n"));
1513 TALLOC_FREE(result);
1514 return NULL;
1517 result->name = talloc_strdup(result, name);
1518 if (result->name == NULL) {
1519 DEBUG(0, ("talloc failed\n"));
1520 TALLOC_FREE(result);
1521 return NULL;
1524 db_ctdb->transaction = NULL;
1525 db_ctdb->db = result;
1527 conn = messaging_ctdbd_connection();
1528 if (conn == NULL) {
1529 DEBUG(1, ("Could not connect to ctdb\n"));
1530 TALLOC_FREE(result);
1531 return NULL;
1534 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1535 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1536 TALLOC_FREE(result);
1537 return NULL;
1540 db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1542 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1543 result->lock_order = lock_order;
1545 /* only pass through specific flags */
1546 tdb_flags &= TDB_SEQNUM;
1548 /* honor permissions if user has specified O_CREAT */
1549 if (open_flags & O_CREAT) {
1550 chmod(db_path, mode);
1553 prio.db_id = db_ctdb->db_id;
1554 prio.priority = lock_order;
1556 status = ctdbd_control_local(
1557 conn, CTDB_CONTROL_SET_DB_PRIORITY, 0, 0,
1558 make_tdb_data((uint8_t *)&prio, sizeof(prio)),
1559 NULL, NULL, &cstatus);
1561 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1562 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1563 nt_errstr(status), cstatus));
1564 TALLOC_FREE(result);
1565 return NULL;
1568 lp_ctx = loadparm_init_s3(db_path, loadparm_s3_helpers());
1570 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags,
1571 O_RDWR, 0, lp_ctx);
1572 talloc_unlink(db_path, lp_ctx);
1573 if (db_ctdb->wtdb == NULL) {
1574 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1575 TALLOC_FREE(result);
1576 return NULL;
1578 talloc_free(db_path);
1580 if (result->persistent) {
1581 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1582 ctdb_conn_msg_ctx(conn));
1583 if (db_ctdb->lock_ctx == NULL) {
1584 DEBUG(0, ("g_lock_ctx_init failed\n"));
1585 TALLOC_FREE(result);
1586 return NULL;
1590 result->private_data = (void *)db_ctdb;
1591 result->fetch_locked = db_ctdb_fetch_locked;
1592 result->try_fetch_locked = db_ctdb_try_fetch_locked;
1593 result->parse_record = db_ctdb_parse_record;
1594 result->traverse = db_ctdb_traverse;
1595 result->traverse_read = db_ctdb_traverse_read;
1596 result->get_seqnum = db_ctdb_get_seqnum;
1597 result->transaction_start = db_ctdb_transaction_start;
1598 result->transaction_commit = db_ctdb_transaction_commit;
1599 result->transaction_cancel = db_ctdb_transaction_cancel;
1600 result->id = db_ctdb_id;
1601 result->stored_callback = NULL;
1603 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1604 name, db_ctdb->db_id));
1606 return result;
1609 #else /* CLUSTER_SUPPORT */
1611 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1612 const char *name,
1613 int hash_size, int tdb_flags,
1614 int open_flags, mode_t mode,
1615 enum dbwrap_lock_order lock_order)
1617 DEBUG(3, ("db_open_ctdb: no cluster support!\n"));
1618 errno = ENOSYS;
1619 return NULL;
1622 #endif