s3_dbrwap_ctdb: improve a comment in db_ctdb_can_use_local_record()
[Samba/gebeck_regimport.git] / source3 / lib / dbwrap / dbwrap_ctdb.c
blob5605b47cafdf565b7c628ea2e804098bdcdea397
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);
92 /**
93 * fetch a record from the tdb, separating out the header
94 * information and returning the body of the record.
96 static NTSTATUS db_ctdb_ltdb_fetch(struct db_ctdb_ctx *db,
97 TDB_DATA key,
98 struct ctdb_ltdb_header *header,
99 TALLOC_CTX *mem_ctx,
100 TDB_DATA *data)
102 TDB_DATA rec;
103 NTSTATUS status;
105 rec = tdb_fetch_compat(db->wtdb->tdb, key);
106 if (rec.dsize < sizeof(struct ctdb_ltdb_header)) {
107 status = NT_STATUS_NOT_FOUND;
108 if (data) {
109 ZERO_STRUCTP(data);
111 if (header) {
112 header->dmaster = (uint32_t)-1;
113 header->rsn = 0;
115 goto done;
118 if (header) {
119 *header = *(struct ctdb_ltdb_header *)rec.dptr;
122 if (data) {
123 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
124 if (data->dsize == 0) {
125 data->dptr = NULL;
126 } else {
127 data->dptr = (unsigned char *)talloc_memdup(mem_ctx,
128 rec.dptr
129 + sizeof(struct ctdb_ltdb_header),
130 data->dsize);
131 if (data->dptr == NULL) {
132 status = NT_STATUS_NO_MEMORY;
133 goto done;
138 status = NT_STATUS_OK;
140 done:
141 SAFE_FREE(rec.dptr);
142 return status;
146 * Store a record together with the ctdb record header
147 * in the local copy of the database.
149 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
150 TDB_DATA key,
151 struct ctdb_ltdb_header *header,
152 TDB_DATA data)
154 TALLOC_CTX *tmp_ctx = talloc_stackframe();
155 TDB_DATA rec;
156 int ret;
158 rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
159 rec.dptr = (uint8_t *)talloc_size(tmp_ctx, rec.dsize);
161 if (rec.dptr == NULL) {
162 talloc_free(tmp_ctx);
163 return NT_STATUS_NO_MEMORY;
166 memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
167 memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
169 ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
171 talloc_free(tmp_ctx);
173 return (ret == 0) ? NT_STATUS_OK
174 : tdb_error_to_ntstatus(db->wtdb->tdb);
179 form a ctdb_rec_data record from a key/data pair
181 note that header may be NULL. If not NULL then it is included in the data portion
182 of the record
184 static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
185 TDB_DATA key,
186 struct ctdb_ltdb_header *header,
187 TDB_DATA data)
189 size_t length;
190 struct ctdb_rec_data *d;
192 length = offsetof(struct ctdb_rec_data, data) + key.dsize +
193 data.dsize + (header?sizeof(*header):0);
194 d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
195 if (d == NULL) {
196 return NULL;
198 d->length = length;
199 d->reqid = reqid;
200 d->keylen = key.dsize;
201 memcpy(&d->data[0], key.dptr, key.dsize);
202 if (header) {
203 d->datalen = data.dsize + sizeof(*header);
204 memcpy(&d->data[key.dsize], header, sizeof(*header));
205 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
206 } else {
207 d->datalen = data.dsize;
208 memcpy(&d->data[key.dsize], data.dptr, data.dsize);
210 return d;
214 /* helper function for marshalling multiple records */
215 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
216 struct ctdb_marshall_buffer *m,
217 uint64_t db_id,
218 uint32_t reqid,
219 TDB_DATA key,
220 struct ctdb_ltdb_header *header,
221 TDB_DATA data)
223 struct ctdb_rec_data *r;
224 size_t m_size, r_size;
225 struct ctdb_marshall_buffer *m2 = NULL;
227 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
228 if (r == NULL) {
229 talloc_free(m);
230 return NULL;
233 if (m == NULL) {
234 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
235 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
236 if (m == NULL) {
237 goto done;
239 m->db_id = db_id;
242 m_size = talloc_get_size(m);
243 r_size = talloc_get_size(r);
245 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
246 mem_ctx, m, m_size + r_size);
247 if (m2 == NULL) {
248 talloc_free(m);
249 goto done;
252 memcpy(m_size + (uint8_t *)m2, r, r_size);
254 m2->count++;
256 done:
257 talloc_free(r);
258 return m2;
261 /* we've finished marshalling, return a data blob with the marshalled records */
262 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
264 TDB_DATA data;
265 data.dptr = (uint8_t *)m;
266 data.dsize = talloc_get_size(m);
267 return data;
271 loop over a marshalling buffer
273 - pass r==NULL to start
274 - loop the number of times indicated by m->count
276 static struct ctdb_rec_data *db_ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
277 uint32_t *reqid,
278 struct ctdb_ltdb_header *header,
279 TDB_DATA *key, TDB_DATA *data)
281 if (r == NULL) {
282 r = (struct ctdb_rec_data *)&m->data[0];
283 } else {
284 r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
287 if (reqid != NULL) {
288 *reqid = r->reqid;
291 if (key != NULL) {
292 key->dptr = &r->data[0];
293 key->dsize = r->keylen;
295 if (data != NULL) {
296 data->dptr = &r->data[r->keylen];
297 data->dsize = r->datalen;
298 if (header != NULL) {
299 data->dptr += sizeof(*header);
300 data->dsize -= sizeof(*header);
304 if (header != NULL) {
305 if (r->datalen < sizeof(*header)) {
306 return NULL;
308 *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
311 return r;
315 * CTDB transaction destructor
317 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
319 NTSTATUS status;
321 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
322 if (!NT_STATUS_IS_OK(status)) {
323 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h->lock_name,
324 nt_errstr(status)));
325 return -1;
327 return 0;
331 * CTDB dbwrap API: transaction_start function
332 * starts a transaction on a persistent database
334 static int db_ctdb_transaction_start(struct db_context *db)
336 struct db_ctdb_transaction_handle *h;
337 NTSTATUS status;
338 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
339 struct db_ctdb_ctx);
341 if (!db->persistent) {
342 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
343 ctx->db_id));
344 return -1;
347 if (ctx->transaction) {
348 ctx->transaction->nesting++;
349 DEBUG(5, (__location__ " transaction start on db 0x%08x: nesting %d -> %d\n",
350 ctx->db_id, ctx->transaction->nesting - 1, ctx->transaction->nesting));
351 return 0;
354 h = talloc_zero(db, struct db_ctdb_transaction_handle);
355 if (h == NULL) {
356 DEBUG(0,(__location__ " oom for transaction handle\n"));
357 return -1;
360 h->ctx = ctx;
362 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
363 (unsigned int)ctx->db_id);
364 if (h->lock_name == NULL) {
365 DEBUG(0, ("talloc_asprintf failed\n"));
366 TALLOC_FREE(h);
367 return -1;
371 * Wait a day, i.e. forever...
373 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
374 timeval_set(86400, 0));
375 if (!NT_STATUS_IS_OK(status)) {
376 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
377 TALLOC_FREE(h);
378 return -1;
381 talloc_set_destructor(h, db_ctdb_transaction_destructor);
383 ctx->transaction = h;
385 DEBUG(5,(__location__ " transaction started on db 0x%08x\n", ctx->db_id));
387 return 0;
390 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
391 TDB_DATA key,
392 struct ctdb_ltdb_header *pheader,
393 TALLOC_CTX *mem_ctx,
394 TDB_DATA *pdata)
396 struct ctdb_rec_data *rec = NULL;
397 struct ctdb_ltdb_header h;
398 bool found = false;
399 TDB_DATA data;
400 int i;
402 if (buf == NULL) {
403 return false;
406 ZERO_STRUCT(h);
407 ZERO_STRUCT(data);
410 * Walk the list of records written during this
411 * transaction. If we want to read one we have already
412 * written, return the last written sample. Thus we do not do
413 * a "break;" for the first hit, this record might have been
414 * overwritten later.
417 for (i=0; i<buf->count; i++) {
418 TDB_DATA tkey, tdata;
419 uint32_t reqid;
420 struct ctdb_ltdb_header hdr;
422 ZERO_STRUCT(hdr);
424 rec = db_ctdb_marshall_loop_next(buf, rec, &reqid, &hdr, &tkey,
425 &tdata);
426 if (rec == NULL) {
427 return false;
430 if (tdb_data_equal(key, tkey)) {
431 found = true;
432 data = tdata;
433 h = hdr;
437 if (!found) {
438 return false;
441 if (pdata != NULL) {
442 data.dptr = (uint8_t *)talloc_memdup(mem_ctx, data.dptr,
443 data.dsize);
444 if ((data.dsize != 0) && (data.dptr == NULL)) {
445 return false;
447 *pdata = data;
450 if (pheader != NULL) {
451 *pheader = h;
454 return true;
458 fetch a record inside a transaction
460 static NTSTATUS db_ctdb_transaction_fetch(struct db_ctdb_ctx *db,
461 TALLOC_CTX *mem_ctx,
462 TDB_DATA key, TDB_DATA *data)
464 struct db_ctdb_transaction_handle *h = db->transaction;
465 NTSTATUS status;
466 bool found;
468 found = pull_newest_from_marshall_buffer(h->m_write, key, NULL,
469 mem_ctx, data);
470 if (found) {
471 return NT_STATUS_OK;
474 status = db_ctdb_ltdb_fetch(h->ctx, key, NULL, mem_ctx, data);
476 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
477 *data = tdb_null;
480 return status;
484 * Fetch a record from a persistent database
485 * without record locking and without an active transaction.
487 * This just fetches from the local database copy.
488 * Since the databases are kept in syc cluster-wide,
489 * there is no point in doing a ctdb call to fetch the
490 * record from the lmaster. It does even harm since migration
491 * of records bump their RSN and hence render the persistent
492 * database inconsistent.
494 static NTSTATUS db_ctdb_fetch_persistent(struct db_ctdb_ctx *db,
495 TALLOC_CTX *mem_ctx,
496 TDB_DATA key, TDB_DATA *data)
498 NTSTATUS status;
500 status = db_ctdb_ltdb_fetch(db, key, NULL, mem_ctx, data);
502 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
503 *data = tdb_null;
506 return status;
509 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
510 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
512 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
513 TALLOC_CTX *mem_ctx,
514 TDB_DATA key)
516 struct db_record *result;
517 TDB_DATA ctdb_data;
519 if (!(result = talloc(mem_ctx, struct db_record))) {
520 DEBUG(0, ("talloc failed\n"));
521 return NULL;
524 result->private_data = ctx->transaction;
526 result->key.dsize = key.dsize;
527 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
528 key.dsize);
529 if (result->key.dptr == NULL) {
530 DEBUG(0, ("talloc failed\n"));
531 TALLOC_FREE(result);
532 return NULL;
535 result->store = db_ctdb_store_transaction;
536 result->delete_rec = db_ctdb_delete_transaction;
538 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
539 NULL, result, &result->value)) {
540 return result;
543 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
544 if (ctdb_data.dptr == NULL) {
545 /* create the record */
546 result->value = tdb_null;
547 return result;
550 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
551 result->value.dptr = NULL;
553 if ((result->value.dsize != 0)
554 && !(result->value.dptr = (uint8_t *)talloc_memdup(
555 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
556 result->value.dsize))) {
557 DEBUG(0, ("talloc failed\n"));
558 TALLOC_FREE(result);
561 SAFE_FREE(ctdb_data.dptr);
563 return result;
566 static int db_ctdb_record_destructor(struct db_record **recp)
568 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
569 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
570 rec->private_data, struct db_ctdb_transaction_handle);
571 int ret = h->ctx->db->transaction_commit(h->ctx->db);
572 if (ret != 0) {
573 DEBUG(0,(__location__ " transaction_commit failed\n"));
575 return 0;
579 auto-create a transaction for persistent databases
581 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
582 TALLOC_CTX *mem_ctx,
583 TDB_DATA key)
585 int res;
586 struct db_record *rec, **recp;
588 res = db_ctdb_transaction_start(ctx->db);
589 if (res == -1) {
590 return NULL;
593 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
594 if (rec == NULL) {
595 ctx->db->transaction_cancel(ctx->db);
596 return NULL;
599 /* destroy this transaction when we release the lock */
600 recp = talloc(rec, struct db_record *);
601 if (recp == NULL) {
602 ctx->db->transaction_cancel(ctx->db);
603 talloc_free(rec);
604 return NULL;
606 *recp = rec;
607 talloc_set_destructor(recp, db_ctdb_record_destructor);
608 return rec;
613 stores a record inside a transaction
615 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
616 TDB_DATA key, TDB_DATA data)
618 TALLOC_CTX *tmp_ctx = talloc_new(h);
619 TDB_DATA rec;
620 struct ctdb_ltdb_header header;
622 ZERO_STRUCT(header);
624 /* we need the header so we can update the RSN */
626 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
627 NULL, NULL)) {
629 rec = tdb_fetch_compat(h->ctx->wtdb->tdb, key);
631 if (rec.dptr != NULL) {
632 memcpy(&header, rec.dptr,
633 sizeof(struct ctdb_ltdb_header));
634 rec.dsize -= sizeof(struct ctdb_ltdb_header);
637 * a special case, we are writing the same
638 * data that is there now
640 if (data.dsize == rec.dsize &&
641 memcmp(data.dptr,
642 rec.dptr + sizeof(struct ctdb_ltdb_header),
643 data.dsize) == 0) {
644 SAFE_FREE(rec.dptr);
645 talloc_free(tmp_ctx);
646 return NT_STATUS_OK;
649 SAFE_FREE(rec.dptr);
652 header.dmaster = get_my_vnn();
653 header.rsn++;
655 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
656 if (h->m_write == NULL) {
657 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
658 talloc_free(tmp_ctx);
659 return NT_STATUS_NO_MEMORY;
662 talloc_free(tmp_ctx);
663 return NT_STATUS_OK;
668 a record store inside a transaction
670 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
672 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
673 rec->private_data, struct db_ctdb_transaction_handle);
674 NTSTATUS status;
676 status = db_ctdb_transaction_store(h, rec->key, data);
677 return status;
681 a record delete inside a transaction
683 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
685 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
686 rec->private_data, struct db_ctdb_transaction_handle);
687 NTSTATUS status;
689 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
690 return status;
694 * Fetch the db sequence number of a persistent db directly from the db.
696 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
697 uint64_t *seqnum)
699 NTSTATUS status;
700 const char *keyname = CTDB_DB_SEQNUM_KEY;
701 TDB_DATA key;
702 TDB_DATA data;
703 struct ctdb_ltdb_header header;
704 TALLOC_CTX *mem_ctx = talloc_stackframe();
706 if (seqnum == NULL) {
707 return NT_STATUS_INVALID_PARAMETER;
710 key = string_term_tdb_data(keyname);
712 status = db_ctdb_ltdb_fetch(db, key, &header, mem_ctx, &data);
713 if (!NT_STATUS_IS_OK(status) &&
714 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND))
716 goto done;
719 status = NT_STATUS_OK;
721 if (data.dsize != sizeof(uint64_t)) {
722 *seqnum = 0;
723 goto done;
726 *seqnum = *(uint64_t *)data.dptr;
728 done:
729 TALLOC_FREE(mem_ctx);
730 return status;
734 * Store the database sequence number inside a transaction.
736 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
737 uint64_t seqnum)
739 NTSTATUS status;
740 const char *keyname = CTDB_DB_SEQNUM_KEY;
741 TDB_DATA key;
742 TDB_DATA data;
744 key = string_term_tdb_data(keyname);
746 data.dptr = (uint8_t *)&seqnum;
747 data.dsize = sizeof(uint64_t);
749 status = db_ctdb_transaction_store(h, key, data);
751 return status;
755 commit a transaction
757 static int db_ctdb_transaction_commit(struct db_context *db)
759 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
760 struct db_ctdb_ctx);
761 NTSTATUS rets;
762 int status;
763 struct db_ctdb_transaction_handle *h = ctx->transaction;
764 uint64_t old_seqnum, new_seqnum;
765 int ret;
767 if (h == NULL) {
768 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
769 return -1;
772 if (h->nested_cancel) {
773 db->transaction_cancel(db);
774 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
775 return -1;
778 if (h->nesting != 0) {
779 h->nesting--;
780 DEBUG(5, (__location__ " transaction commit on db 0x%08x: nesting %d -> %d\n",
781 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
782 return 0;
785 if (h->m_write == NULL) {
787 * No changes were made, so don't change the seqnum,
788 * don't push to other node, just exit with success.
790 ret = 0;
791 goto done;
794 DEBUG(5,(__location__ " transaction commit on db 0x%08x\n", ctx->db_id));
797 * As the last db action before committing, bump the database sequence
798 * number. Note that this undoes all changes to the seqnum records
799 * performed under the transaction. This record is not meant to be
800 * modified by user interaction. It is for internal use only...
802 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
803 if (!NT_STATUS_IS_OK(rets)) {
804 DEBUG(1, (__location__ " failed to fetch the db sequence number "
805 "in transaction commit on db 0x%08x\n", ctx->db_id));
806 ret = -1;
807 goto done;
810 new_seqnum = old_seqnum + 1;
812 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
813 if (!NT_STATUS_IS_OK(rets)) {
814 DEBUG(1, (__location__ "failed to store the db sequence number "
815 " in transaction commit on db 0x%08x\n", ctx->db_id));
816 ret = -1;
817 goto done;
820 again:
821 /* tell ctdbd to commit to the other nodes */
822 rets = ctdbd_control_local(messaging_ctdbd_connection(),
823 CTDB_CONTROL_TRANS3_COMMIT,
824 h->ctx->db_id, 0,
825 db_ctdb_marshall_finish(h->m_write),
826 NULL, NULL, &status);
827 if (!NT_STATUS_IS_OK(rets) || status != 0) {
829 * The TRANS3_COMMIT control should only possibly fail when a
830 * recovery has been running concurrently. In any case, the db
831 * will be the same on all nodes, either the new copy or the
832 * old copy. This can be detected by comparing the old and new
833 * local sequence numbers.
835 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
836 if (!NT_STATUS_IS_OK(rets)) {
837 DEBUG(1, (__location__ " failed to refetch db sequence "
838 "number after failed TRANS3_COMMIT\n"));
839 ret = -1;
840 goto done;
843 if (new_seqnum == old_seqnum) {
844 /* Recovery prevented all our changes: retry. */
845 goto again;
846 } else if (new_seqnum != (old_seqnum + 1)) {
847 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
848 "old_seqnum[%lu] + (0 or 1) after failed "
849 "TRANS3_COMMIT - this should not happen!\n",
850 (unsigned long)new_seqnum,
851 (unsigned long)old_seqnum));
852 ret = -1;
853 goto done;
856 * Recovery propagated our changes to all nodes, completing
857 * our commit for us - succeed.
861 ret = 0;
863 done:
864 h->ctx->transaction = NULL;
865 talloc_free(h);
866 return ret;
871 cancel a transaction
873 static int db_ctdb_transaction_cancel(struct db_context *db)
875 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
876 struct db_ctdb_ctx);
877 struct db_ctdb_transaction_handle *h = ctx->transaction;
879 if (h == NULL) {
880 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
881 return -1;
884 if (h->nesting != 0) {
885 h->nesting--;
886 h->nested_cancel = true;
887 DEBUG(5, (__location__ " transaction cancel on db 0x%08x: nesting %d -> %d\n",
888 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
889 return 0;
892 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
894 ctx->transaction = NULL;
895 talloc_free(h);
896 return 0;
900 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
902 struct db_ctdb_rec *crec = talloc_get_type_abort(
903 rec->private_data, struct db_ctdb_rec);
905 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
910 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
911 static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
913 NTSTATUS status;
914 struct ctdb_control_schedule_for_deletion *dd;
915 TDB_DATA indata;
916 int cstatus;
917 struct db_ctdb_rec *crec = talloc_get_type_abort(
918 rec->private_data, struct db_ctdb_rec);
920 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
921 indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
922 if (indata.dptr == NULL) {
923 DEBUG(0, (__location__ " talloc failed!\n"));
924 return NT_STATUS_NO_MEMORY;
927 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
928 dd->db_id = crec->ctdb_ctx->db_id;
929 dd->hdr = crec->header;
930 dd->keylen = rec->key.dsize;
931 memcpy(dd->key, rec->key.dptr, rec->key.dsize);
933 status = ctdbd_control_local(messaging_ctdbd_connection(),
934 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
935 crec->ctdb_ctx->db_id,
936 CTDB_CTRL_FLAG_NOREPLY, /* flags */
937 indata,
938 NULL, /* outdata */
939 NULL, /* errmsg */
940 &cstatus);
941 talloc_free(indata.dptr);
943 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
944 DEBUG(1, (__location__ " Error sending local control "
945 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
946 nt_errstr(status), cstatus));
947 if (NT_STATUS_IS_OK(status)) {
948 status = NT_STATUS_UNSUCCESSFUL;
952 return status;
954 #endif
956 static NTSTATUS db_ctdb_delete(struct db_record *rec)
958 TDB_DATA data;
959 NTSTATUS status;
962 * We have to store the header with empty data. TODO: Fix the
963 * tdb-level cleanup
966 ZERO_STRUCT(data);
968 status = db_ctdb_store(rec, data, 0);
969 if (!NT_STATUS_IS_OK(status)) {
970 return status;
973 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
974 status = db_ctdb_send_schedule_for_deletion(rec);
975 #endif
977 return status;
980 static int db_ctdb_record_destr(struct db_record* data)
982 struct db_ctdb_rec *crec = talloc_get_type_abort(
983 data->private_data, struct db_ctdb_rec);
984 int threshold;
986 DEBUG(10, (DEBUGLEVEL > 10
987 ? "Unlocking db %u key %s\n"
988 : "Unlocking db %u key %.20s\n",
989 (int)crec->ctdb_ctx->db_id,
990 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
991 data->key.dsize)));
993 tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key);
995 threshold = lp_ctdb_locktime_warn_threshold();
996 if (threshold != 0) {
997 double timediff = timeval_elapsed(&crec->lock_time);
998 if ((timediff * 1000) > threshold) {
999 DEBUG(0, ("Held tdb lock %f seconds\n", timediff));
1003 return 0;
1007 * Check whether we have a valid local copy of the given record,
1008 * either for reading or for writing.
1010 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data, bool read_only)
1012 struct ctdb_ltdb_header *hdr;
1014 if (ctdb_data.dptr == NULL)
1015 return false;
1017 if (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header))
1018 return false;
1020 hdr = (struct ctdb_ltdb_header *)ctdb_data.dptr;
1022 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1023 if (hdr->dmaster != get_my_vnn()) {
1024 /* If we're not dmaster, it must be r/o copy. */
1025 return read_only && (hdr->flags & CTDB_REC_RO_HAVE_READONLY);
1029 * If we want write access, no one may have r/o copies.
1031 return read_only || !(hdr->flags & CTDB_REC_RO_HAVE_DELEGATIONS);
1032 #else
1033 return (hdr->dmaster == get_my_vnn());
1034 #endif
1037 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
1038 TALLOC_CTX *mem_ctx,
1039 TDB_DATA key,
1040 bool tryonly)
1042 struct db_record *result;
1043 struct db_ctdb_rec *crec;
1044 NTSTATUS status;
1045 TDB_DATA ctdb_data;
1046 int migrate_attempts = 0;
1047 int lockret;
1049 if (!(result = talloc(mem_ctx, struct db_record))) {
1050 DEBUG(0, ("talloc failed\n"));
1051 return NULL;
1054 if (!(crec = talloc_zero(result, struct db_ctdb_rec))) {
1055 DEBUG(0, ("talloc failed\n"));
1056 TALLOC_FREE(result);
1057 return NULL;
1060 result->private_data = (void *)crec;
1061 crec->ctdb_ctx = ctx;
1063 result->key.dsize = key.dsize;
1064 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
1065 key.dsize);
1066 if (result->key.dptr == NULL) {
1067 DEBUG(0, ("talloc failed\n"));
1068 TALLOC_FREE(result);
1069 return NULL;
1073 * Do a blocking lock on the record
1075 again:
1077 if (DEBUGLEVEL >= 10) {
1078 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1079 DEBUG(10, (DEBUGLEVEL > 10
1080 ? "Locking db %u key %s\n"
1081 : "Locking db %u key %.20s\n",
1082 (int)crec->ctdb_ctx->db_id, keystr));
1083 TALLOC_FREE(keystr);
1086 lockret = tryonly
1087 ? tdb_chainlock_nonblock(ctx->wtdb->tdb, key)
1088 : tdb_chainlock(ctx->wtdb->tdb, key);
1089 if (lockret != 0) {
1090 DEBUG(3, ("tdb_chainlock failed\n"));
1091 TALLOC_FREE(result);
1092 return NULL;
1095 result->store = db_ctdb_store;
1096 result->delete_rec = db_ctdb_delete;
1097 talloc_set_destructor(result, db_ctdb_record_destr);
1099 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
1102 * See if we have a valid record and we are the dmaster. If so, we can
1103 * take the shortcut and just return it.
1106 if (!db_ctdb_can_use_local_copy(ctdb_data, false)) {
1107 SAFE_FREE(ctdb_data.dptr);
1108 tdb_chainunlock(ctx->wtdb->tdb, key);
1109 talloc_set_destructor(result, NULL);
1111 if (tryonly && (migrate_attempts != 0)) {
1112 DEBUG(5, ("record migrated away again\n"));
1113 TALLOC_FREE(result);
1114 return NULL;
1117 migrate_attempts += 1;
1119 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1120 ctdb_data.dptr, ctdb_data.dptr ?
1121 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
1122 get_my_vnn(),
1123 ctdb_data.dptr ?
1124 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->flags : 0));
1126 status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id,
1127 key);
1128 if (!NT_STATUS_IS_OK(status)) {
1129 DEBUG(5, ("ctdb_migrate failed: %s\n",
1130 nt_errstr(status)));
1131 TALLOC_FREE(result);
1132 return NULL;
1134 /* now its migrated, try again */
1135 goto again;
1138 if (migrate_attempts > 10) {
1139 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s needed %d "
1140 "attempts\n", tdb_name(ctx->wtdb->tdb),
1141 hex_encode_talloc(talloc_tos(),
1142 (unsigned char *)key.dptr,
1143 key.dsize),
1144 migrate_attempts));
1147 GetTimeOfDay(&crec->lock_time);
1149 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1151 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1152 result->value.dptr = NULL;
1154 if ((result->value.dsize != 0)
1155 && !(result->value.dptr = (uint8_t *)talloc_memdup(
1156 result, ctdb_data.dptr + sizeof(crec->header),
1157 result->value.dsize))) {
1158 DEBUG(0, ("talloc failed\n"));
1159 TALLOC_FREE(result);
1162 SAFE_FREE(ctdb_data.dptr);
1164 return result;
1167 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1168 TALLOC_CTX *mem_ctx,
1169 TDB_DATA key)
1171 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1172 struct db_ctdb_ctx);
1174 if (ctx->transaction != NULL) {
1175 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1178 if (db->persistent) {
1179 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1182 return fetch_locked_internal(ctx, mem_ctx, key, false);
1185 static struct db_record *db_ctdb_try_fetch_locked(struct db_context *db,
1186 TALLOC_CTX *mem_ctx,
1187 TDB_DATA key)
1189 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1190 struct db_ctdb_ctx);
1192 if (ctx->transaction != NULL) {
1193 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1196 if (db->persistent) {
1197 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1200 return fetch_locked_internal(ctx, mem_ctx, key, true);
1204 fetch (unlocked, no migration) operation on ctdb
1206 static NTSTATUS db_ctdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
1207 TDB_DATA key, TDB_DATA *data)
1209 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1210 struct db_ctdb_ctx);
1211 NTSTATUS status;
1212 TDB_DATA ctdb_data;
1214 if (ctx->transaction) {
1215 return db_ctdb_transaction_fetch(ctx, mem_ctx, key, data);
1218 if (db->persistent) {
1219 return db_ctdb_fetch_persistent(ctx, mem_ctx, key, data);
1222 /* try a direct fetch */
1223 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
1226 * See if we have a valid record and we are the dmaster. If so, we can
1227 * take the shortcut and just return it.
1228 * we bypass the dmaster check for persistent databases
1230 if (db_ctdb_can_use_local_copy(ctdb_data, true)) {
1232 * We have a valid local copy - avoid the ctdb protocol op
1234 data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
1236 data->dptr = (uint8_t *)talloc_memdup(
1237 mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header),
1238 data->dsize);
1240 SAFE_FREE(ctdb_data.dptr);
1242 if (data->dptr == NULL) {
1243 return NT_STATUS_NO_MEMORY;
1245 return NT_STATUS_OK;
1248 SAFE_FREE(ctdb_data.dptr);
1251 * We weren't able to get it locally - ask ctdb to fetch it for us.
1252 * If we already had *something*, it's probably worth making a local
1253 * read-only copy.
1255 status = ctdbd_fetch(messaging_ctdbd_connection(), ctx->db_id, key,
1256 mem_ctx, data,
1257 ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header));
1258 if (!NT_STATUS_IS_OK(status)) {
1259 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status)));
1262 return status;
1265 static NTSTATUS db_ctdb_parse_record(struct db_context *db, TDB_DATA key,
1266 void (*parser)(TDB_DATA key,
1267 TDB_DATA data,
1268 void *private_data),
1269 void *private_data)
1271 NTSTATUS status;
1272 TDB_DATA data;
1274 status = db_ctdb_fetch(db, talloc_tos(), key, &data);
1275 if (!NT_STATUS_IS_OK(status)) {
1276 return status;
1278 parser(key, data, private_data);
1279 TALLOC_FREE(data.dptr);
1280 return NT_STATUS_OK;
1283 struct traverse_state {
1284 struct db_context *db;
1285 int (*fn)(struct db_record *rec, void *private_data);
1286 void *private_data;
1287 int count;
1290 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1292 struct traverse_state *state = (struct traverse_state *)private_data;
1293 struct db_record *rec;
1294 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1295 /* we have to give them a locked record to prevent races */
1296 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1297 if (rec && rec->value.dsize > 0) {
1298 state->fn(rec, state->private_data);
1300 talloc_free(tmp_ctx);
1303 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1304 void *private_data)
1306 struct traverse_state *state = (struct traverse_state *)private_data;
1307 struct db_record *rec;
1308 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1309 int ret = 0;
1312 * Skip the __db_sequence_number__ key:
1313 * This is used for persistent transactions internally.
1315 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1316 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1318 goto done;
1321 /* we have to give them a locked record to prevent races */
1322 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1323 if (rec && rec->value.dsize > 0) {
1324 ret = state->fn(rec, state->private_data);
1327 done:
1328 talloc_free(tmp_ctx);
1329 return ret;
1332 /* wrapper to use traverse_persistent_callback with dbwrap */
1333 static int traverse_persistent_callback_dbwrap(struct db_record *rec, void* data)
1335 return traverse_persistent_callback(NULL, rec->key, rec->value, data);
1339 static int db_ctdb_traverse(struct db_context *db,
1340 int (*fn)(struct db_record *rec,
1341 void *private_data),
1342 void *private_data)
1344 NTSTATUS status;
1345 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1346 struct db_ctdb_ctx);
1347 struct traverse_state state;
1349 state.db = db;
1350 state.fn = fn;
1351 state.private_data = private_data;
1352 state.count = 0;
1354 if (db->persistent) {
1355 struct tdb_context *ltdb = ctx->wtdb->tdb;
1356 int ret;
1358 /* for persistent databases we don't need to do a ctdb traverse,
1359 we can do a faster local traverse */
1360 ret = tdb_traverse(ltdb, traverse_persistent_callback, &state);
1361 if (ret < 0) {
1362 return ret;
1364 if (ctx->transaction && ctx->transaction->m_write) {
1366 * we now have to handle keys not yet
1367 * present at transaction start
1369 struct db_context *newkeys = db_open_rbt(talloc_tos());
1370 struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
1371 struct ctdb_rec_data *rec=NULL;
1372 int i;
1373 int count = 0;
1375 if (newkeys == NULL) {
1376 return -1;
1379 for (i=0; i<mbuf->count; i++) {
1380 TDB_DATA key;
1381 rec =db_ctdb_marshall_loop_next(mbuf, rec,
1382 NULL, NULL,
1383 &key, NULL);
1384 SMB_ASSERT(rec != NULL);
1386 if (!tdb_exists(ltdb, key)) {
1387 dbwrap_store(newkeys, key, tdb_null, 0);
1390 status = dbwrap_traverse(newkeys,
1391 traverse_persistent_callback_dbwrap,
1392 &state,
1393 &count);
1394 talloc_free(newkeys);
1395 if (!NT_STATUS_IS_OK(status)) {
1396 return -1;
1398 ret += count;
1400 return ret;
1403 status = ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1404 if (!NT_STATUS_IS_OK(status)) {
1405 return -1;
1407 return state.count;
1410 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1412 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1415 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1417 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1420 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1422 struct traverse_state *state = (struct traverse_state *)private_data;
1423 struct db_record rec;
1424 rec.key = key;
1425 rec.value = data;
1426 rec.store = db_ctdb_store_deny;
1427 rec.delete_rec = db_ctdb_delete_deny;
1428 rec.private_data = state->db;
1429 state->fn(&rec, state->private_data);
1430 state->count++;
1433 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1434 void *private_data)
1436 struct traverse_state *state = (struct traverse_state *)private_data;
1437 struct db_record rec;
1440 * Skip the __db_sequence_number__ key:
1441 * This is used for persistent transactions internally.
1443 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1444 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1446 return 0;
1449 rec.key = kbuf;
1450 rec.value = dbuf;
1451 rec.store = db_ctdb_store_deny;
1452 rec.delete_rec = db_ctdb_delete_deny;
1453 rec.private_data = state->db;
1455 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1456 /* a deleted record */
1457 return 0;
1459 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1460 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1462 state->count++;
1463 return state->fn(&rec, state->private_data);
1466 static int db_ctdb_traverse_read(struct db_context *db,
1467 int (*fn)(struct db_record *rec,
1468 void *private_data),
1469 void *private_data)
1471 NTSTATUS status;
1472 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1473 struct db_ctdb_ctx);
1474 struct traverse_state state;
1476 state.db = db;
1477 state.fn = fn;
1478 state.private_data = private_data;
1479 state.count = 0;
1481 if (db->persistent) {
1482 /* for persistent databases we don't need to do a ctdb traverse,
1483 we can do a faster local traverse */
1484 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1487 status = ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1488 if (!NT_STATUS_IS_OK(status)) {
1489 return -1;
1491 return state.count;
1494 static int db_ctdb_get_seqnum(struct db_context *db)
1496 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1497 struct db_ctdb_ctx);
1498 return tdb_get_seqnum(ctx->wtdb->tdb);
1501 static void db_ctdb_id(struct db_context *db, const uint8_t **id,
1502 size_t *idlen)
1504 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1505 db->private_data, struct db_ctdb_ctx);
1507 *id = (uint8_t *)&ctx->db_id;
1508 *idlen = sizeof(ctx->db_id);
1511 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1512 const char *name,
1513 int hash_size, int tdb_flags,
1514 int open_flags, mode_t mode,
1515 enum dbwrap_lock_order lock_order)
1517 struct db_context *result;
1518 struct db_ctdb_ctx *db_ctdb;
1519 char *db_path;
1520 struct ctdbd_connection *conn;
1521 struct loadparm_context *lp_ctx;
1522 struct ctdb_db_priority prio;
1523 NTSTATUS status;
1524 int cstatus;
1526 if (!lp_clustering()) {
1527 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1528 return NULL;
1531 if (!(result = talloc_zero(mem_ctx, struct db_context))) {
1532 DEBUG(0, ("talloc failed\n"));
1533 TALLOC_FREE(result);
1534 return NULL;
1537 if (!(db_ctdb = talloc(result, struct db_ctdb_ctx))) {
1538 DEBUG(0, ("talloc failed\n"));
1539 TALLOC_FREE(result);
1540 return NULL;
1543 db_ctdb->transaction = NULL;
1544 db_ctdb->db = result;
1546 conn = messaging_ctdbd_connection();
1547 if (conn == NULL) {
1548 DEBUG(1, ("Could not connect to ctdb\n"));
1549 TALLOC_FREE(result);
1550 return NULL;
1553 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1554 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1555 TALLOC_FREE(result);
1556 return NULL;
1559 db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1561 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1562 result->lock_order = lock_order;
1564 /* only pass through specific flags */
1565 tdb_flags &= TDB_SEQNUM;
1567 /* honor permissions if user has specified O_CREAT */
1568 if (open_flags & O_CREAT) {
1569 chmod(db_path, mode);
1572 prio.db_id = db_ctdb->db_id;
1573 prio.priority = lock_order;
1575 status = ctdbd_control_local(
1576 conn, CTDB_CONTROL_SET_DB_PRIORITY, 0, 0,
1577 make_tdb_data((uint8_t *)&prio, sizeof(prio)),
1578 NULL, NULL, &cstatus);
1580 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1581 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1582 nt_errstr(status), cstatus));
1583 TALLOC_FREE(result);
1584 return NULL;
1587 lp_ctx = loadparm_init_s3(db_path, loadparm_s3_helpers());
1589 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags,
1590 O_RDWR, 0, lp_ctx);
1591 talloc_unlink(db_path, lp_ctx);
1592 if (db_ctdb->wtdb == NULL) {
1593 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1594 TALLOC_FREE(result);
1595 return NULL;
1597 talloc_free(db_path);
1599 if (result->persistent) {
1600 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1601 ctdb_conn_msg_ctx(conn));
1602 if (db_ctdb->lock_ctx == NULL) {
1603 DEBUG(0, ("g_lock_ctx_init failed\n"));
1604 TALLOC_FREE(result);
1605 return NULL;
1609 result->private_data = (void *)db_ctdb;
1610 result->fetch_locked = db_ctdb_fetch_locked;
1611 result->try_fetch_locked = db_ctdb_try_fetch_locked;
1612 result->parse_record = db_ctdb_parse_record;
1613 result->traverse = db_ctdb_traverse;
1614 result->traverse_read = db_ctdb_traverse_read;
1615 result->get_seqnum = db_ctdb_get_seqnum;
1616 result->transaction_start = db_ctdb_transaction_start;
1617 result->transaction_commit = db_ctdb_transaction_commit;
1618 result->transaction_cancel = db_ctdb_transaction_cancel;
1619 result->id = db_ctdb_id;
1620 result->stored_callback = NULL;
1622 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1623 name, db_ctdb->db_id));
1625 return result;
1628 #else /* CLUSTER_SUPPORT */
1630 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1631 const char *name,
1632 int hash_size, int tdb_flags,
1633 int open_flags, mode_t mode,
1634 enum dbwrap_lock_order lock_order)
1636 DEBUG(3, ("db_open_ctdb: no cluster support!\n"));
1637 return NULL;
1640 #endif