dbwrap_ctdb: implement DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS
[Samba.git] / source3 / lib / dbwrap / dbwrap_ctdb.c
blob87e644dbe5fe938e6cf97a5e1a11278ca67ca7bb
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface wrapper around ctdbd
4 Copyright (C) Volker Lendecke 2007-2009
5 Copyright (C) Michael Adam 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "lib/tdb_wrap/tdb_wrap.h"
24 #include "util_tdb.h"
25 #include "dbwrap/dbwrap.h"
26 #include "dbwrap/dbwrap_ctdb.h"
27 #include "dbwrap/dbwrap_rbt.h"
28 #include "lib/param/param.h"
30 #ifdef CLUSTER_SUPPORT
33 * It is not possible to include ctdb.h and tdb_compat.h (included via
34 * some other include above) without warnings. This fixes those
35 * warnings.
38 #ifdef typesafe_cb
39 #undef typesafe_cb
40 #endif
42 #ifdef typesafe_cb_preargs
43 #undef typesafe_cb_preargs
44 #endif
46 #ifdef typesafe_cb_postargs
47 #undef typesafe_cb_postargs
48 #endif
50 #include "ctdb.h"
51 #include "ctdb_private.h"
52 #include "ctdbd_conn.h"
53 #include "dbwrap/dbwrap.h"
54 #include "dbwrap/dbwrap_private.h"
55 #include "dbwrap/dbwrap_ctdb.h"
56 #include "g_lock.h"
57 #include "messages.h"
59 struct db_ctdb_transaction_handle {
60 struct db_ctdb_ctx *ctx;
62 * we store the writes done under a transaction:
64 struct ctdb_marshall_buffer *m_write;
65 uint32_t nesting;
66 bool nested_cancel;
67 char *lock_name;
70 struct db_ctdb_ctx {
71 struct db_context *db;
72 struct tdb_wrap *wtdb;
73 uint32_t db_id;
74 struct db_ctdb_transaction_handle *transaction;
75 struct g_lock_ctx *lock_ctx;
77 /* thresholds for warning messages */
78 int warn_unlock_msecs;
79 int warn_migrate_msecs;
80 int warn_migrate_attempts;
81 int warn_locktime_msecs;
84 struct db_ctdb_rec {
85 struct db_ctdb_ctx *ctdb_ctx;
86 struct ctdb_ltdb_header header;
87 struct timeval lock_time;
90 static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
92 enum TDB_ERROR tret = tdb_error(tdb);
94 return map_nt_error_from_tdb(tret);
97 struct db_ctdb_ltdb_parse_state {
98 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
99 TDB_DATA data, void *private_data);
100 void *private_data;
103 static int db_ctdb_ltdb_parser(TDB_DATA key, TDB_DATA data,
104 void *private_data)
106 struct db_ctdb_ltdb_parse_state *state =
107 (struct db_ctdb_ltdb_parse_state *)private_data;
109 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
110 return -1;
112 if (data.dsize == sizeof(struct ctdb_ltdb_header)) {
114 * Making this a separate case that needs fixing
115 * separately. This is an empty record. ctdbd does not
116 * distinguish between empty and deleted records. Samba right
117 * now can live without empty records, so lets treat zero-size
118 * (i.e. deleted) records as non-existing.
120 return -1;
122 state->parser(
123 key, (struct ctdb_ltdb_header *)data.dptr,
124 make_tdb_data(data.dptr + sizeof(struct ctdb_ltdb_header),
125 data.dsize - sizeof(struct ctdb_ltdb_header)),
126 state->private_data);
127 return 0;
130 static NTSTATUS db_ctdb_ltdb_parse(
131 struct db_ctdb_ctx *db, TDB_DATA key,
132 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
133 TDB_DATA data, void *private_data),
134 void *private_data)
136 struct db_ctdb_ltdb_parse_state state;
137 int ret;
139 state.parser = parser;
140 state.private_data = private_data;
142 ret = tdb_parse_record(db->wtdb->tdb, key, db_ctdb_ltdb_parser,
143 &state);
144 if (ret == -1) {
145 return NT_STATUS_NOT_FOUND;
147 return NT_STATUS_OK;
151 * Store a record together with the ctdb record header
152 * in the local copy of the database.
154 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
155 TDB_DATA key,
156 struct ctdb_ltdb_header *header,
157 TDB_DATA data)
159 TDB_DATA rec;
160 int ret;
162 rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
163 rec.dptr = (uint8_t *)talloc_size(talloc_tos(), rec.dsize);
165 if (rec.dptr == NULL) {
166 return NT_STATUS_NO_MEMORY;
169 memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
170 memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
172 ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
174 talloc_free(rec.dptr);
176 return (ret == 0) ? NT_STATUS_OK
177 : tdb_error_to_ntstatus(db->wtdb->tdb);
182 form a ctdb_rec_data record from a key/data pair
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 + sizeof(*header);
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);
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 return d;
210 /* helper function for marshalling multiple records */
211 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
212 struct ctdb_marshall_buffer *m,
213 uint64_t db_id,
214 uint32_t reqid,
215 TDB_DATA key,
216 struct ctdb_ltdb_header *header,
217 TDB_DATA data)
219 struct ctdb_rec_data *r;
220 size_t m_size, r_size;
221 struct ctdb_marshall_buffer *m2 = NULL;
223 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
224 if (r == NULL) {
225 talloc_free(m);
226 return NULL;
229 if (m == NULL) {
230 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
231 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
232 if (m == NULL) {
233 goto done;
235 m->db_id = db_id;
238 m_size = talloc_get_size(m);
239 r_size = talloc_get_size(r);
241 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
242 mem_ctx, m, m_size + r_size);
243 if (m2 == NULL) {
244 talloc_free(m);
245 goto done;
248 memcpy(m_size + (uint8_t *)m2, r, r_size);
250 m2->count++;
252 done:
253 talloc_free(r);
254 return m2;
257 /* we've finished marshalling, return a data blob with the marshalled records */
258 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
260 TDB_DATA data;
261 data.dptr = (uint8_t *)m;
262 data.dsize = talloc_get_size(m);
263 return data;
267 loop over a marshalling buffer
269 - pass r==NULL to start
270 - loop the number of times indicated by m->count
272 static struct ctdb_rec_data *db_ctdb_marshall_loop_next_key(
273 struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r, TDB_DATA *key)
275 if (r == NULL) {
276 r = (struct ctdb_rec_data *)&m->data[0];
277 } else {
278 r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
281 key->dptr = &r->data[0];
282 key->dsize = r->keylen;
283 return r;
286 static bool db_ctdb_marshall_buf_parse(
287 struct ctdb_rec_data *r, uint32_t *reqid,
288 struct ctdb_ltdb_header **header, TDB_DATA *data)
290 if (r->datalen < sizeof(struct ctdb_ltdb_header)) {
291 return false;
294 *reqid = r->reqid;
296 data->dptr = &r->data[r->keylen] + sizeof(struct ctdb_ltdb_header);
297 data->dsize = r->datalen - sizeof(struct ctdb_ltdb_header);
299 *header = (struct ctdb_ltdb_header *)&r->data[r->keylen];
301 return true;
305 * CTDB transaction destructor
307 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
309 NTSTATUS status;
311 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
312 if (!NT_STATUS_IS_OK(status)) {
313 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h->lock_name,
314 nt_errstr(status)));
315 return -1;
317 return 0;
321 * CTDB dbwrap API: transaction_start function
322 * starts a transaction on a persistent database
324 static int db_ctdb_transaction_start(struct db_context *db)
326 struct db_ctdb_transaction_handle *h;
327 NTSTATUS status;
328 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
329 struct db_ctdb_ctx);
331 if (!db->persistent) {
332 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
333 ctx->db_id));
334 return -1;
337 if (ctx->transaction) {
338 ctx->transaction->nesting++;
339 DEBUG(5, (__location__ " transaction start on db 0x%08x: nesting %d -> %d\n",
340 ctx->db_id, ctx->transaction->nesting - 1, ctx->transaction->nesting));
341 return 0;
344 h = talloc_zero(db, struct db_ctdb_transaction_handle);
345 if (h == NULL) {
346 DEBUG(0,(__location__ " oom for transaction handle\n"));
347 return -1;
350 h->ctx = ctx;
352 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
353 (unsigned int)ctx->db_id);
354 if (h->lock_name == NULL) {
355 DEBUG(0, ("talloc_asprintf failed\n"));
356 TALLOC_FREE(h);
357 return -1;
361 * Wait a day, i.e. forever...
363 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
364 timeval_set(86400, 0));
365 if (!NT_STATUS_IS_OK(status)) {
366 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
367 TALLOC_FREE(h);
368 return -1;
371 talloc_set_destructor(h, db_ctdb_transaction_destructor);
373 ctx->transaction = h;
375 DEBUG(5,(__location__ " transaction started on db 0x%08x\n", ctx->db_id));
377 return 0;
380 static bool parse_newest_in_marshall_buffer(
381 struct ctdb_marshall_buffer *buf, TDB_DATA key,
382 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
383 TDB_DATA data, void *private_data),
384 void *private_data)
386 struct ctdb_rec_data *rec = NULL;
387 struct ctdb_ltdb_header *h = NULL;
388 TDB_DATA data;
389 int i;
391 if (buf == NULL) {
392 return false;
396 * Walk the list of records written during this
397 * transaction. If we want to read one we have already
398 * written, return the last written sample. Thus we do not do
399 * a "break;" for the first hit, this record might have been
400 * overwritten later.
403 for (i=0; i<buf->count; i++) {
404 TDB_DATA tkey;
405 uint32_t reqid;
407 rec = db_ctdb_marshall_loop_next_key(buf, rec, &tkey);
408 if (rec == NULL) {
409 return false;
412 if (!tdb_data_equal(key, tkey)) {
413 continue;
416 if (!db_ctdb_marshall_buf_parse(rec, &reqid, &h, &data)) {
417 return false;
421 if (h == NULL) {
422 return false;
425 parser(key, h, data, private_data);
427 return true;
430 struct pull_newest_from_marshall_buffer_state {
431 struct ctdb_ltdb_header *pheader;
432 TALLOC_CTX *mem_ctx;
433 TDB_DATA *pdata;
436 static void pull_newest_from_marshall_buffer_parser(
437 TDB_DATA key, struct ctdb_ltdb_header *header,
438 TDB_DATA data, void *private_data)
440 struct pull_newest_from_marshall_buffer_state *state =
441 (struct pull_newest_from_marshall_buffer_state *)private_data;
443 if (state->pheader != NULL) {
444 memcpy(state->pheader, header, sizeof(*state->pheader));
446 if (state->pdata != NULL) {
447 state->pdata->dsize = data.dsize;
448 state->pdata->dptr = (uint8_t *)talloc_memdup(
449 state->mem_ctx, data.dptr, data.dsize);
453 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
454 TDB_DATA key,
455 struct ctdb_ltdb_header *pheader,
456 TALLOC_CTX *mem_ctx,
457 TDB_DATA *pdata)
459 struct pull_newest_from_marshall_buffer_state state;
461 state.pheader = pheader;
462 state.mem_ctx = mem_ctx;
463 state.pdata = pdata;
465 if (!parse_newest_in_marshall_buffer(
466 buf, key, pull_newest_from_marshall_buffer_parser,
467 &state)) {
468 return false;
470 if ((pdata != NULL) && (pdata->dsize != 0) && (pdata->dptr == NULL)) {
471 /* ENOMEM */
472 return false;
474 return true;
477 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
478 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
480 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
481 TALLOC_CTX *mem_ctx,
482 TDB_DATA key)
484 struct db_record *result;
485 TDB_DATA ctdb_data;
487 if (!(result = talloc(mem_ctx, struct db_record))) {
488 DEBUG(0, ("talloc failed\n"));
489 return NULL;
492 result->db = ctx->db;
493 result->private_data = ctx->transaction;
495 result->key.dsize = key.dsize;
496 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
497 key.dsize);
498 if (result->key.dptr == NULL) {
499 DEBUG(0, ("talloc failed\n"));
500 TALLOC_FREE(result);
501 return NULL;
504 result->store = db_ctdb_store_transaction;
505 result->delete_rec = db_ctdb_delete_transaction;
507 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
508 NULL, result, &result->value)) {
509 return result;
512 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
513 if (ctdb_data.dptr == NULL) {
514 /* create the record */
515 result->value = tdb_null;
516 return result;
519 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
520 result->value.dptr = NULL;
522 if ((result->value.dsize != 0)
523 && !(result->value.dptr = (uint8_t *)talloc_memdup(
524 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
525 result->value.dsize))) {
526 DEBUG(0, ("talloc failed\n"));
527 TALLOC_FREE(result);
530 SAFE_FREE(ctdb_data.dptr);
532 return result;
535 static int db_ctdb_record_destructor(struct db_record **recp)
537 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
538 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
539 rec->private_data, struct db_ctdb_transaction_handle);
540 int ret = h->ctx->db->transaction_commit(h->ctx->db);
541 if (ret != 0) {
542 DEBUG(0,(__location__ " transaction_commit failed\n"));
544 return 0;
548 auto-create a transaction for persistent databases
550 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
551 TALLOC_CTX *mem_ctx,
552 TDB_DATA key)
554 int res;
555 struct db_record *rec, **recp;
557 res = db_ctdb_transaction_start(ctx->db);
558 if (res == -1) {
559 return NULL;
562 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
563 if (rec == NULL) {
564 ctx->db->transaction_cancel(ctx->db);
565 return NULL;
568 /* destroy this transaction when we release the lock */
569 recp = talloc(rec, struct db_record *);
570 if (recp == NULL) {
571 ctx->db->transaction_cancel(ctx->db);
572 talloc_free(rec);
573 return NULL;
575 *recp = rec;
576 talloc_set_destructor(recp, db_ctdb_record_destructor);
577 return rec;
582 stores a record inside a transaction
584 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
585 TDB_DATA key, TDB_DATA data)
587 TALLOC_CTX *tmp_ctx = talloc_new(h);
588 TDB_DATA rec;
589 struct ctdb_ltdb_header header;
591 ZERO_STRUCT(header);
593 /* we need the header so we can update the RSN */
595 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
596 NULL, NULL)) {
598 rec = tdb_fetch_compat(h->ctx->wtdb->tdb, key);
600 if (rec.dptr != NULL) {
601 memcpy(&header, rec.dptr,
602 sizeof(struct ctdb_ltdb_header));
603 rec.dsize -= sizeof(struct ctdb_ltdb_header);
606 * a special case, we are writing the same
607 * data that is there now
609 if (data.dsize == rec.dsize &&
610 memcmp(data.dptr,
611 rec.dptr + sizeof(struct ctdb_ltdb_header),
612 data.dsize) == 0) {
613 SAFE_FREE(rec.dptr);
614 talloc_free(tmp_ctx);
615 return NT_STATUS_OK;
618 SAFE_FREE(rec.dptr);
621 header.dmaster = get_my_vnn();
622 header.rsn++;
624 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
625 if (h->m_write == NULL) {
626 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
627 talloc_free(tmp_ctx);
628 return NT_STATUS_NO_MEMORY;
631 talloc_free(tmp_ctx);
632 return NT_STATUS_OK;
637 a record store inside a transaction
639 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
641 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
642 rec->private_data, struct db_ctdb_transaction_handle);
643 NTSTATUS status;
645 status = db_ctdb_transaction_store(h, rec->key, data);
646 return status;
650 a record delete inside a transaction
652 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
654 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
655 rec->private_data, struct db_ctdb_transaction_handle);
656 NTSTATUS status;
658 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
659 return status;
662 static void db_ctdb_fetch_db_seqnum_parser(
663 TDB_DATA key, struct ctdb_ltdb_header *header,
664 TDB_DATA data, void *private_data)
666 uint64_t *seqnum = (uint64_t *)private_data;
668 if (data.dsize != sizeof(uint64_t)) {
669 *seqnum = 0;
670 return;
672 memcpy(seqnum, data.dptr, sizeof(*seqnum));
676 * Fetch the db sequence number of a persistent db directly from the db.
678 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
679 uint64_t *seqnum)
681 NTSTATUS status;
682 TDB_DATA key;
684 if (seqnum == NULL) {
685 return NT_STATUS_INVALID_PARAMETER;
688 key = string_term_tdb_data(CTDB_DB_SEQNUM_KEY);
690 status = db_ctdb_ltdb_parse(
691 db, key, db_ctdb_fetch_db_seqnum_parser, seqnum);
693 if (NT_STATUS_IS_OK(status)) {
694 return NT_STATUS_OK;
696 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
697 *seqnum = 0;
698 return NT_STATUS_OK;
700 return status;
704 * Store the database sequence number inside a transaction.
706 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
707 uint64_t seqnum)
709 NTSTATUS status;
710 const char *keyname = CTDB_DB_SEQNUM_KEY;
711 TDB_DATA key;
712 TDB_DATA data;
714 key = string_term_tdb_data(keyname);
716 data.dptr = (uint8_t *)&seqnum;
717 data.dsize = sizeof(uint64_t);
719 status = db_ctdb_transaction_store(h, key, data);
721 return status;
725 commit a transaction
727 static int db_ctdb_transaction_commit(struct db_context *db)
729 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
730 struct db_ctdb_ctx);
731 NTSTATUS rets;
732 int status;
733 struct db_ctdb_transaction_handle *h = ctx->transaction;
734 uint64_t old_seqnum, new_seqnum;
735 int ret;
737 if (h == NULL) {
738 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
739 return -1;
742 if (h->nested_cancel) {
743 db->transaction_cancel(db);
744 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
745 return -1;
748 if (h->nesting != 0) {
749 h->nesting--;
750 DEBUG(5, (__location__ " transaction commit on db 0x%08x: nesting %d -> %d\n",
751 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
752 return 0;
755 if (h->m_write == NULL) {
757 * No changes were made, so don't change the seqnum,
758 * don't push to other node, just exit with success.
760 ret = 0;
761 goto done;
764 DEBUG(5,(__location__ " transaction commit on db 0x%08x\n", ctx->db_id));
767 * As the last db action before committing, bump the database sequence
768 * number. Note that this undoes all changes to the seqnum records
769 * performed under the transaction. This record is not meant to be
770 * modified by user interaction. It is for internal use only...
772 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
773 if (!NT_STATUS_IS_OK(rets)) {
774 DEBUG(1, (__location__ " failed to fetch the db sequence number "
775 "in transaction commit on db 0x%08x\n", ctx->db_id));
776 ret = -1;
777 goto done;
780 new_seqnum = old_seqnum + 1;
782 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
783 if (!NT_STATUS_IS_OK(rets)) {
784 DEBUG(1, (__location__ "failed to store the db sequence number "
785 " in transaction commit on db 0x%08x\n", ctx->db_id));
786 ret = -1;
787 goto done;
790 again:
791 /* tell ctdbd to commit to the other nodes */
792 rets = ctdbd_control_local(messaging_ctdbd_connection(),
793 CTDB_CONTROL_TRANS3_COMMIT,
794 h->ctx->db_id, 0,
795 db_ctdb_marshall_finish(h->m_write),
796 NULL, NULL, &status);
797 if (!NT_STATUS_IS_OK(rets) || status != 0) {
799 * The TRANS3_COMMIT control should only possibly fail when a
800 * recovery has been running concurrently. In any case, the db
801 * will be the same on all nodes, either the new copy or the
802 * old copy. This can be detected by comparing the old and new
803 * local sequence numbers.
805 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
806 if (!NT_STATUS_IS_OK(rets)) {
807 DEBUG(1, (__location__ " failed to refetch db sequence "
808 "number after failed TRANS3_COMMIT\n"));
809 ret = -1;
810 goto done;
813 if (new_seqnum == old_seqnum) {
814 /* Recovery prevented all our changes: retry. */
815 goto again;
817 if (new_seqnum != (old_seqnum + 1)) {
818 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
819 "old_seqnum[%lu] + (0 or 1) after failed "
820 "TRANS3_COMMIT - this should not happen!\n",
821 (unsigned long)new_seqnum,
822 (unsigned long)old_seqnum));
823 ret = -1;
824 goto done;
827 * Recovery propagated our changes to all nodes, completing
828 * our commit for us - succeed.
832 ret = 0;
834 done:
835 h->ctx->transaction = NULL;
836 talloc_free(h);
837 return ret;
842 cancel a transaction
844 static int db_ctdb_transaction_cancel(struct db_context *db)
846 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
847 struct db_ctdb_ctx);
848 struct db_ctdb_transaction_handle *h = ctx->transaction;
850 if (h == NULL) {
851 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
852 return -1;
855 if (h->nesting != 0) {
856 h->nesting--;
857 h->nested_cancel = true;
858 DEBUG(5, (__location__ " transaction cancel on db 0x%08x: nesting %d -> %d\n",
859 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
860 return 0;
863 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
865 ctx->transaction = NULL;
866 talloc_free(h);
867 return 0;
871 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
873 struct db_ctdb_rec *crec = talloc_get_type_abort(
874 rec->private_data, struct db_ctdb_rec);
876 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
881 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
882 static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
884 NTSTATUS status;
885 struct ctdb_control_schedule_for_deletion *dd;
886 TDB_DATA indata;
887 int cstatus;
888 struct db_ctdb_rec *crec = talloc_get_type_abort(
889 rec->private_data, struct db_ctdb_rec);
891 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
892 indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
893 if (indata.dptr == NULL) {
894 DEBUG(0, (__location__ " talloc failed!\n"));
895 return NT_STATUS_NO_MEMORY;
898 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
899 dd->db_id = crec->ctdb_ctx->db_id;
900 dd->hdr = crec->header;
901 dd->keylen = rec->key.dsize;
902 memcpy(dd->key, rec->key.dptr, rec->key.dsize);
904 status = ctdbd_control_local(messaging_ctdbd_connection(),
905 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
906 crec->ctdb_ctx->db_id,
907 CTDB_CTRL_FLAG_NOREPLY, /* flags */
908 indata,
909 NULL, /* outdata */
910 NULL, /* errmsg */
911 &cstatus);
912 talloc_free(indata.dptr);
914 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
915 DEBUG(1, (__location__ " Error sending local control "
916 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
917 nt_errstr(status), cstatus));
918 if (NT_STATUS_IS_OK(status)) {
919 status = NT_STATUS_UNSUCCESSFUL;
923 return status;
925 #endif
927 static NTSTATUS db_ctdb_delete(struct db_record *rec)
929 NTSTATUS status;
932 * We have to store the header with empty data. TODO: Fix the
933 * tdb-level cleanup
936 status = db_ctdb_store(rec, tdb_null, 0);
937 if (!NT_STATUS_IS_OK(status)) {
938 return status;
941 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
942 status = db_ctdb_send_schedule_for_deletion(rec);
943 #endif
945 return status;
948 static int db_ctdb_record_destr(struct db_record* data)
950 struct db_ctdb_rec *crec = talloc_get_type_abort(
951 data->private_data, struct db_ctdb_rec);
952 int threshold;
953 int ret;
954 struct timeval before;
955 double timediff;
957 DEBUG(10, (DEBUGLEVEL > 10
958 ? "Unlocking db %u key %s\n"
959 : "Unlocking db %u key %.20s\n",
960 (int)crec->ctdb_ctx->db_id,
961 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
962 data->key.dsize)));
964 before = timeval_current();
966 ret = tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key);
968 timediff = timeval_elapsed(&before);
969 timediff *= 1000; /* get us milliseconds */
971 if (timediff > crec->ctdb_ctx->warn_unlock_msecs) {
972 char *key;
973 key = hex_encode_talloc(talloc_tos(),
974 (unsigned char *)data->key.dptr,
975 data->key.dsize);
976 DEBUG(0, ("tdb_chainunlock on db %s, key %s took %f milliseconds\n",
977 tdb_name(crec->ctdb_ctx->wtdb->tdb), key,
978 timediff));
979 TALLOC_FREE(key);
982 if (ret != 0) {
983 DEBUG(0, ("tdb_chainunlock failed\n"));
984 return -1;
987 threshold = crec->ctdb_ctx->warn_locktime_msecs;
988 if (threshold != 0) {
989 timediff = timeval_elapsed(&crec->lock_time) * 1000;
990 if (timediff > threshold) {
991 const char *key;
993 key = hex_encode_talloc(data,
994 (unsigned char *)data->key.dptr,
995 data->key.dsize);
996 DEBUG(0, ("Held tdb lock on db %s, key %s "
997 "%f milliseconds\n",
998 tdb_name(crec->ctdb_ctx->wtdb->tdb),
999 key, 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_hdr(const struct ctdb_ltdb_header *hdr,
1011 bool read_only)
1013 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1014 if (hdr->dmaster != get_my_vnn()) {
1015 /* If we're not dmaster, it must be r/o copy. */
1016 return read_only && (hdr->flags & CTDB_REC_RO_HAVE_READONLY);
1020 * If we want write access, no one may have r/o copies.
1022 return read_only || !(hdr->flags & CTDB_REC_RO_HAVE_DELEGATIONS);
1023 #else
1024 return (hdr->dmaster == get_my_vnn());
1025 #endif
1028 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data, bool read_only)
1030 if (ctdb_data.dptr == NULL) {
1031 return false;
1034 if (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
1035 return false;
1038 return db_ctdb_can_use_local_hdr(
1039 (struct ctdb_ltdb_header *)ctdb_data.dptr, read_only);
1042 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
1043 TALLOC_CTX *mem_ctx,
1044 TDB_DATA key,
1045 bool tryonly)
1047 struct db_record *result;
1048 struct db_ctdb_rec *crec;
1049 NTSTATUS status;
1050 TDB_DATA ctdb_data;
1051 int migrate_attempts;
1052 struct timeval migrate_start;
1053 struct timeval chainlock_start;
1054 struct timeval ctdb_start_time;
1055 double chainlock_time = 0;
1056 double ctdb_time = 0;
1057 int duration_msecs;
1058 int lockret;
1060 if (!(result = talloc(mem_ctx, struct db_record))) {
1061 DEBUG(0, ("talloc failed\n"));
1062 return NULL;
1065 if (!(crec = talloc_zero(result, struct db_ctdb_rec))) {
1066 DEBUG(0, ("talloc failed\n"));
1067 TALLOC_FREE(result);
1068 return NULL;
1071 result->db = ctx->db;
1072 result->private_data = (void *)crec;
1073 crec->ctdb_ctx = ctx;
1075 result->key.dsize = key.dsize;
1076 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
1077 key.dsize);
1078 if (result->key.dptr == NULL) {
1079 DEBUG(0, ("talloc failed\n"));
1080 TALLOC_FREE(result);
1081 return NULL;
1084 migrate_attempts = 0;
1085 GetTimeOfDay(&migrate_start);
1088 * Do a blocking lock on the record
1090 again:
1092 if (DEBUGLEVEL >= 10) {
1093 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1094 DEBUG(10, (DEBUGLEVEL > 10
1095 ? "Locking db %u key %s\n"
1096 : "Locking db %u key %.20s\n",
1097 (int)crec->ctdb_ctx->db_id, keystr));
1098 TALLOC_FREE(keystr);
1101 GetTimeOfDay(&chainlock_start);
1102 lockret = tryonly
1103 ? tdb_chainlock_nonblock(ctx->wtdb->tdb, key)
1104 : tdb_chainlock(ctx->wtdb->tdb, key);
1105 chainlock_time += timeval_elapsed(&chainlock_start);
1107 if (lockret != 0) {
1108 DEBUG(3, ("tdb_chainlock failed\n"));
1109 TALLOC_FREE(result);
1110 return NULL;
1113 result->store = db_ctdb_store;
1114 result->delete_rec = db_ctdb_delete;
1115 talloc_set_destructor(result, db_ctdb_record_destr);
1117 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
1120 * See if we have a valid record and we are the dmaster. If so, we can
1121 * take the shortcut and just return it.
1124 if (!db_ctdb_can_use_local_copy(ctdb_data, false)) {
1125 SAFE_FREE(ctdb_data.dptr);
1126 tdb_chainunlock(ctx->wtdb->tdb, key);
1127 talloc_set_destructor(result, NULL);
1129 if (tryonly && (migrate_attempts != 0)) {
1130 DEBUG(5, ("record migrated away again\n"));
1131 TALLOC_FREE(result);
1132 return NULL;
1135 migrate_attempts += 1;
1137 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1138 ctdb_data.dptr, ctdb_data.dptr ?
1139 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
1140 get_my_vnn(),
1141 ctdb_data.dptr ?
1142 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->flags : 0));
1144 GetTimeOfDay(&ctdb_start_time);
1145 status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id,
1146 key);
1147 ctdb_time += timeval_elapsed(&ctdb_start_time);
1149 if (!NT_STATUS_IS_OK(status)) {
1150 DEBUG(5, ("ctdb_migrate failed: %s\n",
1151 nt_errstr(status)));
1152 TALLOC_FREE(result);
1153 return NULL;
1155 /* now its migrated, try again */
1156 goto again;
1160 double duration;
1161 duration = timeval_elapsed(&migrate_start);
1164 * Convert the duration to milliseconds to avoid a
1165 * floating-point division of
1166 * lp_parm_int("migrate_duration") by 1000.
1168 duration_msecs = duration * 1000;
1171 if ((migrate_attempts > ctx->warn_migrate_attempts) ||
1172 (duration_msecs > ctx->warn_migrate_msecs)) {
1173 int chain = 0;
1175 if (tdb_get_flags(ctx->wtdb->tdb) & TDB_INCOMPATIBLE_HASH) {
1176 chain = tdb_jenkins_hash(&key) %
1177 tdb_hash_size(ctx->wtdb->tdb);
1180 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s, chain %d "
1181 "needed %d attempts, %d milliseconds, "
1182 "chainlock: %d ms, CTDB %d ms\n",
1183 tdb_name(ctx->wtdb->tdb),
1184 hex_encode_talloc(talloc_tos(),
1185 (unsigned char *)key.dptr,
1186 key.dsize),
1187 chain,
1188 migrate_attempts, duration_msecs,
1189 (int) chainlock_time * 1000,
1190 (int) ctdb_time * 1000));
1193 GetTimeOfDay(&crec->lock_time);
1195 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1197 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1198 result->value.dptr = NULL;
1200 if ((result->value.dsize != 0)
1201 && !(result->value.dptr = (uint8_t *)talloc_memdup(
1202 result, ctdb_data.dptr + sizeof(crec->header),
1203 result->value.dsize))) {
1204 DEBUG(0, ("talloc failed\n"));
1205 TALLOC_FREE(result);
1208 SAFE_FREE(ctdb_data.dptr);
1210 return result;
1213 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1214 TALLOC_CTX *mem_ctx,
1215 TDB_DATA key)
1217 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1218 struct db_ctdb_ctx);
1220 if (ctx->transaction != NULL) {
1221 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1224 if (db->persistent) {
1225 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1228 return fetch_locked_internal(ctx, mem_ctx, key, false);
1231 static struct db_record *db_ctdb_try_fetch_locked(struct db_context *db,
1232 TALLOC_CTX *mem_ctx,
1233 TDB_DATA key)
1235 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1236 struct db_ctdb_ctx);
1238 if (ctx->transaction != NULL) {
1239 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1242 if (db->persistent) {
1243 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1246 return fetch_locked_internal(ctx, mem_ctx, key, true);
1249 struct db_ctdb_parse_record_state {
1250 void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data);
1251 void *private_data;
1252 bool ask_for_readonly_copy;
1253 bool done;
1256 static void db_ctdb_parse_record_parser(
1257 TDB_DATA key, struct ctdb_ltdb_header *header,
1258 TDB_DATA data, void *private_data)
1260 struct db_ctdb_parse_record_state *state =
1261 (struct db_ctdb_parse_record_state *)private_data;
1262 state->parser(key, data, state->private_data);
1265 static void db_ctdb_parse_record_parser_nonpersistent(
1266 TDB_DATA key, struct ctdb_ltdb_header *header,
1267 TDB_DATA data, void *private_data)
1269 struct db_ctdb_parse_record_state *state =
1270 (struct db_ctdb_parse_record_state *)private_data;
1272 if (db_ctdb_can_use_local_hdr(header, true)) {
1273 state->parser(key, data, state->private_data);
1274 state->done = true;
1275 } else {
1277 * We found something in the db, so it seems that this record,
1278 * while not usable locally right now, is popular. Ask for a
1279 * R/O copy.
1281 state->ask_for_readonly_copy = true;
1285 static NTSTATUS db_ctdb_parse_record(struct db_context *db, TDB_DATA key,
1286 void (*parser)(TDB_DATA key,
1287 TDB_DATA data,
1288 void *private_data),
1289 void *private_data)
1291 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1292 db->private_data, struct db_ctdb_ctx);
1293 struct db_ctdb_parse_record_state state;
1294 NTSTATUS status;
1296 state.parser = parser;
1297 state.private_data = private_data;
1299 if (ctx->transaction != NULL) {
1300 struct db_ctdb_transaction_handle *h = ctx->transaction;
1301 bool found;
1304 * Transactions only happen for persistent db's.
1307 found = parse_newest_in_marshall_buffer(
1308 h->m_write, key, db_ctdb_parse_record_parser, &state);
1310 if (found) {
1311 return NT_STATUS_OK;
1315 if (db->persistent) {
1317 * Persistent db, but not found in the transaction buffer
1319 return db_ctdb_ltdb_parse(
1320 ctx, key, db_ctdb_parse_record_parser, &state);
1323 state.done = false;
1324 state.ask_for_readonly_copy = false;
1326 status = db_ctdb_ltdb_parse(
1327 ctx, key, db_ctdb_parse_record_parser_nonpersistent, &state);
1328 if (NT_STATUS_IS_OK(status) && state.done) {
1329 return NT_STATUS_OK;
1332 return ctdbd_parse(messaging_ctdbd_connection(), ctx->db_id, key,
1333 state.ask_for_readonly_copy, parser, private_data);
1336 struct traverse_state {
1337 struct db_context *db;
1338 int (*fn)(struct db_record *rec, void *private_data);
1339 void *private_data;
1340 int count;
1343 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1345 struct traverse_state *state = (struct traverse_state *)private_data;
1346 struct db_record *rec;
1347 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1348 /* we have to give them a locked record to prevent races */
1349 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1350 if (rec && rec->value.dsize > 0) {
1351 state->fn(rec, state->private_data);
1353 talloc_free(tmp_ctx);
1356 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1357 void *private_data)
1359 struct traverse_state *state = (struct traverse_state *)private_data;
1360 struct db_record *rec;
1361 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1362 int ret = 0;
1365 * Skip the __db_sequence_number__ key:
1366 * This is used for persistent transactions internally.
1368 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1369 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1371 goto done;
1374 /* we have to give them a locked record to prevent races */
1375 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1376 if (rec && rec->value.dsize > 0) {
1377 ret = state->fn(rec, state->private_data);
1380 done:
1381 talloc_free(tmp_ctx);
1382 return ret;
1385 /* wrapper to use traverse_persistent_callback with dbwrap */
1386 static int traverse_persistent_callback_dbwrap(struct db_record *rec, void* data)
1388 return traverse_persistent_callback(NULL, rec->key, rec->value, data);
1392 static int db_ctdb_traverse(struct db_context *db,
1393 int (*fn)(struct db_record *rec,
1394 void *private_data),
1395 void *private_data)
1397 NTSTATUS status;
1398 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1399 struct db_ctdb_ctx);
1400 struct traverse_state state;
1402 state.db = db;
1403 state.fn = fn;
1404 state.private_data = private_data;
1405 state.count = 0;
1407 if (db->persistent) {
1408 struct tdb_context *ltdb = ctx->wtdb->tdb;
1409 int ret;
1411 /* for persistent databases we don't need to do a ctdb traverse,
1412 we can do a faster local traverse */
1413 ret = tdb_traverse(ltdb, traverse_persistent_callback, &state);
1414 if (ret < 0) {
1415 return ret;
1417 if (ctx->transaction && ctx->transaction->m_write) {
1419 * we now have to handle keys not yet
1420 * present at transaction start
1422 struct db_context *newkeys = db_open_rbt(talloc_tos());
1423 struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
1424 struct ctdb_rec_data *rec=NULL;
1425 int i;
1426 int count = 0;
1428 if (newkeys == NULL) {
1429 return -1;
1432 for (i=0; i<mbuf->count; i++) {
1433 TDB_DATA key;
1434 rec = db_ctdb_marshall_loop_next_key(
1435 mbuf, rec, &key);
1436 SMB_ASSERT(rec != NULL);
1438 if (!tdb_exists(ltdb, key)) {
1439 dbwrap_store(newkeys, key, tdb_null, 0);
1442 status = dbwrap_traverse(newkeys,
1443 traverse_persistent_callback_dbwrap,
1444 &state,
1445 &count);
1446 talloc_free(newkeys);
1447 if (!NT_STATUS_IS_OK(status)) {
1448 return -1;
1450 ret += count;
1452 return ret;
1455 status = ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1456 if (!NT_STATUS_IS_OK(status)) {
1457 return -1;
1459 return state.count;
1462 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1464 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1467 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1469 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1472 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1474 struct traverse_state *state = (struct traverse_state *)private_data;
1475 struct db_record rec;
1477 ZERO_STRUCT(rec);
1478 rec.db = state->db;
1479 rec.key = key;
1480 rec.value = data;
1481 rec.store = db_ctdb_store_deny;
1482 rec.delete_rec = db_ctdb_delete_deny;
1483 rec.private_data = NULL;
1484 state->fn(&rec, state->private_data);
1485 state->count++;
1488 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1489 void *private_data)
1491 struct traverse_state *state = (struct traverse_state *)private_data;
1492 struct db_record rec;
1495 * Skip the __db_sequence_number__ key:
1496 * This is used for persistent transactions internally.
1498 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1499 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1501 return 0;
1504 ZERO_STRUCT(rec);
1505 rec.db = state->db;
1506 rec.key = kbuf;
1507 rec.value = dbuf;
1508 rec.store = db_ctdb_store_deny;
1509 rec.delete_rec = db_ctdb_delete_deny;
1510 rec.private_data = NULL;
1512 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1513 /* a deleted record */
1514 return 0;
1516 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1517 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1519 state->count++;
1520 return state->fn(&rec, state->private_data);
1523 static int db_ctdb_traverse_read(struct db_context *db,
1524 int (*fn)(struct db_record *rec,
1525 void *private_data),
1526 void *private_data)
1528 NTSTATUS status;
1529 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1530 struct db_ctdb_ctx);
1531 struct traverse_state state;
1533 state.db = db;
1534 state.fn = fn;
1535 state.private_data = private_data;
1536 state.count = 0;
1538 if (db->persistent) {
1539 /* for persistent databases we don't need to do a ctdb traverse,
1540 we can do a faster local traverse */
1541 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1544 status = ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1545 if (!NT_STATUS_IS_OK(status)) {
1546 return -1;
1548 return state.count;
1551 static int db_ctdb_get_seqnum(struct db_context *db)
1553 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1554 struct db_ctdb_ctx);
1555 return tdb_get_seqnum(ctx->wtdb->tdb);
1558 static void db_ctdb_id(struct db_context *db, const uint8_t **id,
1559 size_t *idlen)
1561 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1562 db->private_data, struct db_ctdb_ctx);
1564 *id = (uint8_t *)&ctx->db_id;
1565 *idlen = sizeof(ctx->db_id);
1568 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1569 const char *name,
1570 int hash_size, int tdb_flags,
1571 int open_flags, mode_t mode,
1572 enum dbwrap_lock_order lock_order,
1573 uint64_t dbwrap_flags)
1575 struct db_context *result;
1576 struct db_ctdb_ctx *db_ctdb;
1577 char *db_path;
1578 struct ctdbd_connection *conn;
1579 struct loadparm_context *lp_ctx;
1580 struct ctdb_db_priority prio;
1581 NTSTATUS status;
1582 int cstatus;
1584 if (!lp_clustering()) {
1585 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1586 return NULL;
1589 if (!(result = talloc_zero(mem_ctx, struct db_context))) {
1590 DEBUG(0, ("talloc failed\n"));
1591 TALLOC_FREE(result);
1592 return NULL;
1595 if (!(db_ctdb = talloc(result, struct db_ctdb_ctx))) {
1596 DEBUG(0, ("talloc failed\n"));
1597 TALLOC_FREE(result);
1598 return NULL;
1601 result->name = talloc_strdup(result, name);
1602 if (result->name == NULL) {
1603 DEBUG(0, ("talloc failed\n"));
1604 TALLOC_FREE(result);
1605 return NULL;
1608 db_ctdb->transaction = NULL;
1609 db_ctdb->db = result;
1611 conn = messaging_ctdbd_connection();
1612 if (conn == NULL) {
1613 DEBUG(1, ("Could not connect to ctdb\n"));
1614 TALLOC_FREE(result);
1615 return NULL;
1618 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1619 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1620 TALLOC_FREE(result);
1621 return NULL;
1624 db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1626 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1627 result->lock_order = lock_order;
1629 /* only pass through specific flags */
1630 tdb_flags &= TDB_SEQNUM;
1632 /* honor permissions if user has specified O_CREAT */
1633 if (open_flags & O_CREAT) {
1634 chmod(db_path, mode);
1637 prio.db_id = db_ctdb->db_id;
1638 prio.priority = lock_order;
1640 status = ctdbd_control_local(
1641 conn, CTDB_CONTROL_SET_DB_PRIORITY, 0, 0,
1642 make_tdb_data((uint8_t *)&prio, sizeof(prio)),
1643 NULL, NULL, &cstatus);
1645 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1646 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1647 nt_errstr(status), cstatus));
1648 TALLOC_FREE(result);
1649 return NULL;
1652 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1653 if (!result->persistent &&
1654 (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS))
1656 TDB_DATA indata;
1658 indata = make_tdb_data((uint8_t *)&db_ctdb->db_id,
1659 sizeof(db_ctdb->db_id));
1661 status = ctdbd_control_local(
1662 conn, CTDB_CONTROL_SET_DB_READONLY, 0, 0, indata,
1663 NULL, NULL, &cstatus);
1664 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1665 DEBUG(1, ("CTDB_CONTROL_SET_DB_READONLY failed: "
1666 "%s, %d\n", nt_errstr(status), cstatus));
1667 TALLOC_FREE(result);
1668 return NULL;
1671 #endif
1673 lp_ctx = loadparm_init_s3(db_path, loadparm_s3_helpers());
1675 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags,
1676 O_RDWR, 0, lp_ctx);
1677 talloc_unlink(db_path, lp_ctx);
1678 if (db_ctdb->wtdb == NULL) {
1679 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1680 TALLOC_FREE(result);
1681 return NULL;
1683 talloc_free(db_path);
1685 if (result->persistent) {
1686 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1687 ctdb_conn_msg_ctx(conn));
1688 if (db_ctdb->lock_ctx == NULL) {
1689 DEBUG(0, ("g_lock_ctx_init failed\n"));
1690 TALLOC_FREE(result);
1691 return NULL;
1695 db_ctdb->warn_unlock_msecs = lp_parm_int(-1, "ctdb",
1696 "unlock_warn_threshold", 5);
1697 db_ctdb->warn_migrate_attempts = lp_parm_int(-1, "ctdb",
1698 "migrate_attempts", 10);
1699 db_ctdb->warn_migrate_msecs = lp_parm_int(-1, "ctdb",
1700 "migrate_duration", 5000);
1701 db_ctdb->warn_locktime_msecs = lp_ctdb_locktime_warn_threshold();
1703 result->private_data = (void *)db_ctdb;
1704 result->fetch_locked = db_ctdb_fetch_locked;
1705 result->try_fetch_locked = db_ctdb_try_fetch_locked;
1706 result->parse_record = db_ctdb_parse_record;
1707 result->traverse = db_ctdb_traverse;
1708 result->traverse_read = db_ctdb_traverse_read;
1709 result->get_seqnum = db_ctdb_get_seqnum;
1710 result->transaction_start = db_ctdb_transaction_start;
1711 result->transaction_commit = db_ctdb_transaction_commit;
1712 result->transaction_cancel = db_ctdb_transaction_cancel;
1713 result->id = db_ctdb_id;
1714 result->stored_callback = NULL;
1716 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1717 name, db_ctdb->db_id));
1719 return result;
1722 #else /* CLUSTER_SUPPORT */
1724 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1725 const char *name,
1726 int hash_size, int tdb_flags,
1727 int open_flags, mode_t mode,
1728 enum dbwrap_lock_order lock_order,
1729 uint64_t dbwrap_flags)
1731 DEBUG(3, ("db_open_ctdb: no cluster support!\n"));
1732 errno = ENOSYS;
1733 return NULL;
1736 #endif