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/>.
22 #include "system/filesys.h"
23 #include "lib/tdb_wrap/tdb_wrap.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
42 #ifdef typesafe_cb_preargs
43 #undef typesafe_cb_preargs
46 #ifdef typesafe_cb_postargs
47 #undef typesafe_cb_postargs
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"
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;
71 struct db_context *db;
72 struct tdb_wrap *wtdb;
74 struct db_ctdb_transaction_handle *transaction;
75 struct g_lock_ctx *lock_ctx;
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);
97 static int db_ctdb_ltdb_parser(TDB_DATA key, TDB_DATA 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)) {
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);
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),
120 struct db_ctdb_ltdb_parse_state state;
123 state.parser = parser;
124 state.private_data = private_data;
126 ret = tdb_parse_record(db->wtdb->tdb, key, db_ctdb_ltdb_parser,
129 return NT_STATUS_NOT_FOUND;
134 struct db_ctdb_ltdb_fetch_state {
135 struct ctdb_ltdb_header *header;
141 static void db_ctdb_ltdb_fetch_parser(
142 TDB_DATA key, struct ctdb_ltdb_header *header,
143 TDB_DATA data, void *private_data)
145 struct db_ctdb_ltdb_fetch_state *state =
146 (struct db_ctdb_ltdb_fetch_state *)private_data;
148 if (state->header != NULL) {
149 memcpy(state->header, header, sizeof(struct ctdb_ltdb_header));
151 if (state->data == NULL) {
154 state->data->dsize = data.dsize;
155 if (data.dsize == 0) {
156 state->data->dptr = NULL;
159 state->data->dptr = talloc_memdup(state->mem_ctx, data.dptr,
161 if (state->data->dptr == NULL) {
168 * fetch a record from the tdb, separating out the header
169 * information and returning the body of the record.
171 static NTSTATUS db_ctdb_ltdb_fetch(struct db_ctdb_ctx *db,
173 struct ctdb_ltdb_header *header,
177 struct db_ctdb_ltdb_fetch_state state;
180 state.header = header;
181 state.mem_ctx = mem_ctx;
185 status = db_ctdb_ltdb_parse(db, key, db_ctdb_ltdb_fetch_parser,
187 if (!NT_STATUS_IS_OK(status)) {
192 header->dmaster = (uint32_t)-1;
198 return NT_STATUS_NO_MEMORY;
204 * Store a record together with the ctdb record header
205 * in the local copy of the database.
207 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
209 struct ctdb_ltdb_header *header,
212 TALLOC_CTX *tmp_ctx = talloc_stackframe();
216 rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
217 rec.dptr = (uint8_t *)talloc_size(tmp_ctx, rec.dsize);
219 if (rec.dptr == NULL) {
220 talloc_free(tmp_ctx);
221 return NT_STATUS_NO_MEMORY;
224 memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
225 memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
227 ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
229 talloc_free(tmp_ctx);
231 return (ret == 0) ? NT_STATUS_OK
232 : tdb_error_to_ntstatus(db->wtdb->tdb);
237 form a ctdb_rec_data record from a key/data pair
239 static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
241 struct ctdb_ltdb_header *header,
245 struct ctdb_rec_data *d;
247 length = offsetof(struct ctdb_rec_data, data) + key.dsize +
248 data.dsize + sizeof(*header);
249 d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
255 d->keylen = key.dsize;
256 memcpy(&d->data[0], key.dptr, key.dsize);
258 d->datalen = data.dsize + sizeof(*header);
259 memcpy(&d->data[key.dsize], header, sizeof(*header));
260 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
265 /* helper function for marshalling multiple records */
266 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
267 struct ctdb_marshall_buffer *m,
271 struct ctdb_ltdb_header *header,
274 struct ctdb_rec_data *r;
275 size_t m_size, r_size;
276 struct ctdb_marshall_buffer *m2 = NULL;
278 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
285 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
286 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
293 m_size = talloc_get_size(m);
294 r_size = talloc_get_size(r);
296 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
297 mem_ctx, m, m_size + r_size);
303 memcpy(m_size + (uint8_t *)m2, r, r_size);
312 /* we've finished marshalling, return a data blob with the marshalled records */
313 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
316 data.dptr = (uint8_t *)m;
317 data.dsize = talloc_get_size(m);
322 loop over a marshalling buffer
324 - pass r==NULL to start
325 - loop the number of times indicated by m->count
327 static struct ctdb_rec_data *db_ctdb_marshall_loop_next_key(
328 struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r, TDB_DATA *key)
331 r = (struct ctdb_rec_data *)&m->data[0];
333 r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
336 key->dptr = &r->data[0];
337 key->dsize = r->keylen;
341 static bool db_ctdb_marshall_buf_parse(
342 struct ctdb_rec_data *r, uint32_t *reqid,
343 struct ctdb_ltdb_header **header, TDB_DATA *data)
345 if (r->datalen < sizeof(struct ctdb_ltdb_header)) {
351 data->dptr = &r->data[r->keylen] + sizeof(struct ctdb_ltdb_header);
352 data->dsize = r->datalen - sizeof(struct ctdb_ltdb_header);
354 *header = (struct ctdb_ltdb_header *)&r->data[r->keylen];
360 * CTDB transaction destructor
362 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
366 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
367 if (!NT_STATUS_IS_OK(status)) {
368 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h->lock_name,
376 * CTDB dbwrap API: transaction_start function
377 * starts a transaction on a persistent database
379 static int db_ctdb_transaction_start(struct db_context *db)
381 struct db_ctdb_transaction_handle *h;
383 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
386 if (!db->persistent) {
387 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
392 if (ctx->transaction) {
393 ctx->transaction->nesting++;
394 DEBUG(5, (__location__ " transaction start on db 0x%08x: nesting %d -> %d\n",
395 ctx->db_id, ctx->transaction->nesting - 1, ctx->transaction->nesting));
399 h = talloc_zero(db, struct db_ctdb_transaction_handle);
401 DEBUG(0,(__location__ " oom for transaction handle\n"));
407 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
408 (unsigned int)ctx->db_id);
409 if (h->lock_name == NULL) {
410 DEBUG(0, ("talloc_asprintf failed\n"));
416 * Wait a day, i.e. forever...
418 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
419 timeval_set(86400, 0));
420 if (!NT_STATUS_IS_OK(status)) {
421 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
426 talloc_set_destructor(h, db_ctdb_transaction_destructor);
428 ctx->transaction = h;
430 DEBUG(5,(__location__ " transaction started on db 0x%08x\n", ctx->db_id));
435 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
437 struct ctdb_ltdb_header *pheader,
441 struct ctdb_rec_data *rec = NULL;
442 struct ctdb_ltdb_header *h = NULL;
451 * Walk the list of records written during this
452 * transaction. If we want to read one we have already
453 * written, return the last written sample. Thus we do not do
454 * a "break;" for the first hit, this record might have been
458 for (i=0; i<buf->count; i++) {
462 rec = db_ctdb_marshall_loop_next_key(buf, rec, &tkey);
467 if (!tdb_data_equal(key, tkey)) {
471 if (!db_ctdb_marshall_buf_parse(rec, &reqid, &h, &data)) {
481 data.dptr = (uint8_t *)talloc_memdup(mem_ctx, data.dptr,
483 if ((data.dsize != 0) && (data.dptr == NULL)) {
489 if (pheader != NULL) {
497 fetch a record inside a transaction
499 static NTSTATUS db_ctdb_transaction_fetch(struct db_ctdb_ctx *db,
501 TDB_DATA key, TDB_DATA *data)
503 struct db_ctdb_transaction_handle *h = db->transaction;
507 found = pull_newest_from_marshall_buffer(h->m_write, key, NULL,
513 status = db_ctdb_ltdb_fetch(h->ctx, key, NULL, mem_ctx, data);
515 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
523 * Fetch a record from a persistent database
524 * without record locking and without an active transaction.
526 * This just fetches from the local database copy.
527 * Since the databases are kept in syc cluster-wide,
528 * there is no point in doing a ctdb call to fetch the
529 * record from the lmaster. It does even harm since migration
530 * of records bump their RSN and hence render the persistent
531 * database inconsistent.
533 static NTSTATUS db_ctdb_fetch_persistent(struct db_ctdb_ctx *db,
535 TDB_DATA key, TDB_DATA *data)
539 status = db_ctdb_ltdb_fetch(db, key, NULL, mem_ctx, data);
541 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
548 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
549 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
551 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
555 struct db_record *result;
558 if (!(result = talloc(mem_ctx, struct db_record))) {
559 DEBUG(0, ("talloc failed\n"));
563 result->private_data = ctx->transaction;
565 result->key.dsize = key.dsize;
566 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
568 if (result->key.dptr == NULL) {
569 DEBUG(0, ("talloc failed\n"));
574 result->store = db_ctdb_store_transaction;
575 result->delete_rec = db_ctdb_delete_transaction;
577 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
578 NULL, result, &result->value)) {
582 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
583 if (ctdb_data.dptr == NULL) {
584 /* create the record */
585 result->value = tdb_null;
589 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
590 result->value.dptr = NULL;
592 if ((result->value.dsize != 0)
593 && !(result->value.dptr = (uint8_t *)talloc_memdup(
594 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
595 result->value.dsize))) {
596 DEBUG(0, ("talloc failed\n"));
600 SAFE_FREE(ctdb_data.dptr);
605 static int db_ctdb_record_destructor(struct db_record **recp)
607 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
608 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
609 rec->private_data, struct db_ctdb_transaction_handle);
610 int ret = h->ctx->db->transaction_commit(h->ctx->db);
612 DEBUG(0,(__location__ " transaction_commit failed\n"));
618 auto-create a transaction for persistent databases
620 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
625 struct db_record *rec, **recp;
627 res = db_ctdb_transaction_start(ctx->db);
632 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
634 ctx->db->transaction_cancel(ctx->db);
638 /* destroy this transaction when we release the lock */
639 recp = talloc(rec, struct db_record *);
641 ctx->db->transaction_cancel(ctx->db);
646 talloc_set_destructor(recp, db_ctdb_record_destructor);
652 stores a record inside a transaction
654 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
655 TDB_DATA key, TDB_DATA data)
657 TALLOC_CTX *tmp_ctx = talloc_new(h);
659 struct ctdb_ltdb_header header;
663 /* we need the header so we can update the RSN */
665 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
668 rec = tdb_fetch_compat(h->ctx->wtdb->tdb, key);
670 if (rec.dptr != NULL) {
671 memcpy(&header, rec.dptr,
672 sizeof(struct ctdb_ltdb_header));
673 rec.dsize -= sizeof(struct ctdb_ltdb_header);
676 * a special case, we are writing the same
677 * data that is there now
679 if (data.dsize == rec.dsize &&
681 rec.dptr + sizeof(struct ctdb_ltdb_header),
684 talloc_free(tmp_ctx);
691 header.dmaster = get_my_vnn();
694 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
695 if (h->m_write == NULL) {
696 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
697 talloc_free(tmp_ctx);
698 return NT_STATUS_NO_MEMORY;
701 talloc_free(tmp_ctx);
707 a record store inside a transaction
709 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
711 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
712 rec->private_data, struct db_ctdb_transaction_handle);
715 status = db_ctdb_transaction_store(h, rec->key, data);
720 a record delete inside a transaction
722 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
724 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
725 rec->private_data, struct db_ctdb_transaction_handle);
728 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
732 static void db_ctdb_fetch_db_seqnum_parser(
733 TDB_DATA key, struct ctdb_ltdb_header *header,
734 TDB_DATA data, void *private_data)
736 uint64_t *seqnum = (uint64_t *)private_data;
738 if (data.dsize != sizeof(uint64_t)) {
742 memcpy(seqnum, data.dptr, sizeof(*seqnum));
746 * Fetch the db sequence number of a persistent db directly from the db.
748 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
754 if (seqnum == NULL) {
755 return NT_STATUS_INVALID_PARAMETER;
758 key = string_term_tdb_data(CTDB_DB_SEQNUM_KEY);
760 status = db_ctdb_ltdb_parse(
761 db, key, db_ctdb_fetch_db_seqnum_parser, seqnum);
763 if (NT_STATUS_IS_OK(status)) {
766 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
774 * Store the database sequence number inside a transaction.
776 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
780 const char *keyname = CTDB_DB_SEQNUM_KEY;
784 key = string_term_tdb_data(keyname);
786 data.dptr = (uint8_t *)&seqnum;
787 data.dsize = sizeof(uint64_t);
789 status = db_ctdb_transaction_store(h, key, data);
797 static int db_ctdb_transaction_commit(struct db_context *db)
799 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
803 struct db_ctdb_transaction_handle *h = ctx->transaction;
804 uint64_t old_seqnum, new_seqnum;
808 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
812 if (h->nested_cancel) {
813 db->transaction_cancel(db);
814 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
818 if (h->nesting != 0) {
820 DEBUG(5, (__location__ " transaction commit on db 0x%08x: nesting %d -> %d\n",
821 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
825 if (h->m_write == NULL) {
827 * No changes were made, so don't change the seqnum,
828 * don't push to other node, just exit with success.
834 DEBUG(5,(__location__ " transaction commit on db 0x%08x\n", ctx->db_id));
837 * As the last db action before committing, bump the database sequence
838 * number. Note that this undoes all changes to the seqnum records
839 * performed under the transaction. This record is not meant to be
840 * modified by user interaction. It is for internal use only...
842 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
843 if (!NT_STATUS_IS_OK(rets)) {
844 DEBUG(1, (__location__ " failed to fetch the db sequence number "
845 "in transaction commit on db 0x%08x\n", ctx->db_id));
850 new_seqnum = old_seqnum + 1;
852 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
853 if (!NT_STATUS_IS_OK(rets)) {
854 DEBUG(1, (__location__ "failed to store the db sequence number "
855 " in transaction commit on db 0x%08x\n", ctx->db_id));
861 /* tell ctdbd to commit to the other nodes */
862 rets = ctdbd_control_local(messaging_ctdbd_connection(),
863 CTDB_CONTROL_TRANS3_COMMIT,
865 db_ctdb_marshall_finish(h->m_write),
866 NULL, NULL, &status);
867 if (!NT_STATUS_IS_OK(rets) || status != 0) {
869 * The TRANS3_COMMIT control should only possibly fail when a
870 * recovery has been running concurrently. In any case, the db
871 * will be the same on all nodes, either the new copy or the
872 * old copy. This can be detected by comparing the old and new
873 * local sequence numbers.
875 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
876 if (!NT_STATUS_IS_OK(rets)) {
877 DEBUG(1, (__location__ " failed to refetch db sequence "
878 "number after failed TRANS3_COMMIT\n"));
883 if (new_seqnum == old_seqnum) {
884 /* Recovery prevented all our changes: retry. */
887 if (new_seqnum != (old_seqnum + 1)) {
888 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
889 "old_seqnum[%lu] + (0 or 1) after failed "
890 "TRANS3_COMMIT - this should not happen!\n",
891 (unsigned long)new_seqnum,
892 (unsigned long)old_seqnum));
897 * Recovery propagated our changes to all nodes, completing
898 * our commit for us - succeed.
905 h->ctx->transaction = NULL;
914 static int db_ctdb_transaction_cancel(struct db_context *db)
916 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
918 struct db_ctdb_transaction_handle *h = ctx->transaction;
921 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
925 if (h->nesting != 0) {
927 h->nested_cancel = true;
928 DEBUG(5, (__location__ " transaction cancel on db 0x%08x: nesting %d -> %d\n",
929 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
933 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
935 ctx->transaction = NULL;
941 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
943 struct db_ctdb_rec *crec = talloc_get_type_abort(
944 rec->private_data, struct db_ctdb_rec);
946 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
951 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
952 static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
955 struct ctdb_control_schedule_for_deletion *dd;
958 struct db_ctdb_rec *crec = talloc_get_type_abort(
959 rec->private_data, struct db_ctdb_rec);
961 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
962 indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
963 if (indata.dptr == NULL) {
964 DEBUG(0, (__location__ " talloc failed!\n"));
965 return NT_STATUS_NO_MEMORY;
968 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
969 dd->db_id = crec->ctdb_ctx->db_id;
970 dd->hdr = crec->header;
971 dd->keylen = rec->key.dsize;
972 memcpy(dd->key, rec->key.dptr, rec->key.dsize);
974 status = ctdbd_control_local(messaging_ctdbd_connection(),
975 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
976 crec->ctdb_ctx->db_id,
977 CTDB_CTRL_FLAG_NOREPLY, /* flags */
982 talloc_free(indata.dptr);
984 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
985 DEBUG(1, (__location__ " Error sending local control "
986 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
987 nt_errstr(status), cstatus));
988 if (NT_STATUS_IS_OK(status)) {
989 status = NT_STATUS_UNSUCCESSFUL;
997 static NTSTATUS db_ctdb_delete(struct db_record *rec)
1003 * We have to store the header with empty data. TODO: Fix the
1009 status = db_ctdb_store(rec, data, 0);
1010 if (!NT_STATUS_IS_OK(status)) {
1014 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
1015 status = db_ctdb_send_schedule_for_deletion(rec);
1021 static int db_ctdb_record_destr(struct db_record* data)
1023 struct db_ctdb_rec *crec = talloc_get_type_abort(
1024 data->private_data, struct db_ctdb_rec);
1027 DEBUG(10, (DEBUGLEVEL > 10
1028 ? "Unlocking db %u key %s\n"
1029 : "Unlocking db %u key %.20s\n",
1030 (int)crec->ctdb_ctx->db_id,
1031 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
1034 tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key);
1036 threshold = lp_ctdb_locktime_warn_threshold();
1037 if (threshold != 0) {
1038 double timediff = timeval_elapsed(&crec->lock_time);
1039 if ((timediff * 1000) > threshold) {
1042 key = hex_encode_talloc(data,
1043 (unsigned char *)data->key.dptr,
1045 DEBUG(0, ("Held tdb lock on db %s, key %s %f seconds\n",
1046 tdb_name(crec->ctdb_ctx->wtdb->tdb), key,
1055 * Check whether we have a valid local copy of the given record,
1056 * either for reading or for writing.
1058 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data, bool read_only)
1060 struct ctdb_ltdb_header *hdr;
1062 if (ctdb_data.dptr == NULL)
1065 if (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header))
1068 hdr = (struct ctdb_ltdb_header *)ctdb_data.dptr;
1070 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1071 if (hdr->dmaster != get_my_vnn()) {
1072 /* If we're not dmaster, it must be r/o copy. */
1073 return read_only && (hdr->flags & CTDB_REC_RO_HAVE_READONLY);
1077 * If we want write access, no one may have r/o copies.
1079 return read_only || !(hdr->flags & CTDB_REC_RO_HAVE_DELEGATIONS);
1081 return (hdr->dmaster == get_my_vnn());
1085 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
1086 TALLOC_CTX *mem_ctx,
1090 struct db_record *result;
1091 struct db_ctdb_rec *crec;
1094 int migrate_attempts = 0;
1097 if (!(result = talloc(mem_ctx, struct db_record))) {
1098 DEBUG(0, ("talloc failed\n"));
1102 if (!(crec = talloc_zero(result, struct db_ctdb_rec))) {
1103 DEBUG(0, ("talloc failed\n"));
1104 TALLOC_FREE(result);
1108 result->db = ctx->db;
1109 result->private_data = (void *)crec;
1110 crec->ctdb_ctx = ctx;
1112 result->key.dsize = key.dsize;
1113 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
1115 if (result->key.dptr == NULL) {
1116 DEBUG(0, ("talloc failed\n"));
1117 TALLOC_FREE(result);
1122 * Do a blocking lock on the record
1126 if (DEBUGLEVEL >= 10) {
1127 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1128 DEBUG(10, (DEBUGLEVEL > 10
1129 ? "Locking db %u key %s\n"
1130 : "Locking db %u key %.20s\n",
1131 (int)crec->ctdb_ctx->db_id, keystr));
1132 TALLOC_FREE(keystr);
1136 ? tdb_chainlock_nonblock(ctx->wtdb->tdb, key)
1137 : tdb_chainlock(ctx->wtdb->tdb, key);
1139 DEBUG(3, ("tdb_chainlock failed\n"));
1140 TALLOC_FREE(result);
1144 result->store = db_ctdb_store;
1145 result->delete_rec = db_ctdb_delete;
1146 talloc_set_destructor(result, db_ctdb_record_destr);
1148 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
1151 * See if we have a valid record and we are the dmaster. If so, we can
1152 * take the shortcut and just return it.
1155 if (!db_ctdb_can_use_local_copy(ctdb_data, false)) {
1156 SAFE_FREE(ctdb_data.dptr);
1157 tdb_chainunlock(ctx->wtdb->tdb, key);
1158 talloc_set_destructor(result, NULL);
1160 if (tryonly && (migrate_attempts != 0)) {
1161 DEBUG(5, ("record migrated away again\n"));
1162 TALLOC_FREE(result);
1166 migrate_attempts += 1;
1168 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1169 ctdb_data.dptr, ctdb_data.dptr ?
1170 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
1173 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->flags : 0));
1175 status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id,
1177 if (!NT_STATUS_IS_OK(status)) {
1178 DEBUG(5, ("ctdb_migrate failed: %s\n",
1179 nt_errstr(status)));
1180 TALLOC_FREE(result);
1183 /* now its migrated, try again */
1187 if (migrate_attempts > 10) {
1188 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s needed %d "
1189 "attempts\n", tdb_name(ctx->wtdb->tdb),
1190 hex_encode_talloc(talloc_tos(),
1191 (unsigned char *)key.dptr,
1196 GetTimeOfDay(&crec->lock_time);
1198 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1200 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1201 result->value.dptr = NULL;
1203 if ((result->value.dsize != 0)
1204 && !(result->value.dptr = (uint8_t *)talloc_memdup(
1205 result, ctdb_data.dptr + sizeof(crec->header),
1206 result->value.dsize))) {
1207 DEBUG(0, ("talloc failed\n"));
1208 TALLOC_FREE(result);
1211 SAFE_FREE(ctdb_data.dptr);
1216 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1217 TALLOC_CTX *mem_ctx,
1220 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1221 struct db_ctdb_ctx);
1223 if (ctx->transaction != NULL) {
1224 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1227 if (db->persistent) {
1228 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1231 return fetch_locked_internal(ctx, mem_ctx, key, false);
1234 static struct db_record *db_ctdb_try_fetch_locked(struct db_context *db,
1235 TALLOC_CTX *mem_ctx,
1238 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1239 struct db_ctdb_ctx);
1241 if (ctx->transaction != NULL) {
1242 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1245 if (db->persistent) {
1246 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1249 return fetch_locked_internal(ctx, mem_ctx, key, true);
1253 fetch (unlocked, no migration) operation on ctdb
1255 static NTSTATUS db_ctdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
1256 TDB_DATA key, TDB_DATA *data)
1258 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1259 struct db_ctdb_ctx);
1263 if (ctx->transaction) {
1264 return db_ctdb_transaction_fetch(ctx, mem_ctx, key, data);
1267 if (db->persistent) {
1268 return db_ctdb_fetch_persistent(ctx, mem_ctx, key, data);
1271 /* try a direct fetch */
1272 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
1275 * See if we have a valid record and we are the dmaster. If so, we can
1276 * take the shortcut and just return it.
1277 * we bypass the dmaster check for persistent databases
1279 if (db_ctdb_can_use_local_copy(ctdb_data, true)) {
1281 * We have a valid local copy - avoid the ctdb protocol op
1283 data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
1285 data->dptr = (uint8_t *)talloc_memdup(
1286 mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header),
1289 SAFE_FREE(ctdb_data.dptr);
1291 if (data->dptr == NULL) {
1292 return NT_STATUS_NO_MEMORY;
1294 return NT_STATUS_OK;
1297 SAFE_FREE(ctdb_data.dptr);
1300 * We weren't able to get it locally - ask ctdb to fetch it for us.
1301 * If we already had *something*, it's probably worth making a local
1304 status = ctdbd_fetch(messaging_ctdbd_connection(), ctx->db_id, key,
1306 ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header));
1307 if (!NT_STATUS_IS_OK(status)) {
1308 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status)));
1314 static NTSTATUS db_ctdb_parse_record(struct db_context *db, TDB_DATA key,
1315 void (*parser)(TDB_DATA key,
1317 void *private_data),
1323 status = db_ctdb_fetch(db, talloc_tos(), key, &data);
1324 if (!NT_STATUS_IS_OK(status)) {
1327 parser(key, data, private_data);
1328 TALLOC_FREE(data.dptr);
1329 return NT_STATUS_OK;
1332 struct traverse_state {
1333 struct db_context *db;
1334 int (*fn)(struct db_record *rec, void *private_data);
1339 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1341 struct traverse_state *state = (struct traverse_state *)private_data;
1342 struct db_record *rec;
1343 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1344 /* we have to give them a locked record to prevent races */
1345 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1346 if (rec && rec->value.dsize > 0) {
1347 state->fn(rec, state->private_data);
1349 talloc_free(tmp_ctx);
1352 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1355 struct traverse_state *state = (struct traverse_state *)private_data;
1356 struct db_record *rec;
1357 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1361 * Skip the __db_sequence_number__ key:
1362 * This is used for persistent transactions internally.
1364 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1365 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1370 /* we have to give them a locked record to prevent races */
1371 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1372 if (rec && rec->value.dsize > 0) {
1373 ret = state->fn(rec, state->private_data);
1377 talloc_free(tmp_ctx);
1381 /* wrapper to use traverse_persistent_callback with dbwrap */
1382 static int traverse_persistent_callback_dbwrap(struct db_record *rec, void* data)
1384 return traverse_persistent_callback(NULL, rec->key, rec->value, data);
1388 static int db_ctdb_traverse(struct db_context *db,
1389 int (*fn)(struct db_record *rec,
1390 void *private_data),
1394 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1395 struct db_ctdb_ctx);
1396 struct traverse_state state;
1400 state.private_data = private_data;
1403 if (db->persistent) {
1404 struct tdb_context *ltdb = ctx->wtdb->tdb;
1407 /* for persistent databases we don't need to do a ctdb traverse,
1408 we can do a faster local traverse */
1409 ret = tdb_traverse(ltdb, traverse_persistent_callback, &state);
1413 if (ctx->transaction && ctx->transaction->m_write) {
1415 * we now have to handle keys not yet
1416 * present at transaction start
1418 struct db_context *newkeys = db_open_rbt(talloc_tos());
1419 struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
1420 struct ctdb_rec_data *rec=NULL;
1424 if (newkeys == NULL) {
1428 for (i=0; i<mbuf->count; i++) {
1430 rec = db_ctdb_marshall_loop_next_key(
1432 SMB_ASSERT(rec != NULL);
1434 if (!tdb_exists(ltdb, key)) {
1435 dbwrap_store(newkeys, key, tdb_null, 0);
1438 status = dbwrap_traverse(newkeys,
1439 traverse_persistent_callback_dbwrap,
1442 talloc_free(newkeys);
1443 if (!NT_STATUS_IS_OK(status)) {
1451 status = ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1452 if (!NT_STATUS_IS_OK(status)) {
1458 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1460 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1463 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1465 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1468 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1470 struct traverse_state *state = (struct traverse_state *)private_data;
1471 struct db_record rec;
1474 rec.store = db_ctdb_store_deny;
1475 rec.delete_rec = db_ctdb_delete_deny;
1476 rec.private_data = state->db;
1477 state->fn(&rec, state->private_data);
1481 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1484 struct traverse_state *state = (struct traverse_state *)private_data;
1485 struct db_record rec;
1488 * Skip the __db_sequence_number__ key:
1489 * This is used for persistent transactions internally.
1491 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1492 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1499 rec.store = db_ctdb_store_deny;
1500 rec.delete_rec = db_ctdb_delete_deny;
1501 rec.private_data = state->db;
1503 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1504 /* a deleted record */
1507 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1508 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1511 return state->fn(&rec, state->private_data);
1514 static int db_ctdb_traverse_read(struct db_context *db,
1515 int (*fn)(struct db_record *rec,
1516 void *private_data),
1520 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1521 struct db_ctdb_ctx);
1522 struct traverse_state state;
1526 state.private_data = private_data;
1529 if (db->persistent) {
1530 /* for persistent databases we don't need to do a ctdb traverse,
1531 we can do a faster local traverse */
1532 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1535 status = ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1536 if (!NT_STATUS_IS_OK(status)) {
1542 static int db_ctdb_get_seqnum(struct db_context *db)
1544 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1545 struct db_ctdb_ctx);
1546 return tdb_get_seqnum(ctx->wtdb->tdb);
1549 static void db_ctdb_id(struct db_context *db, const uint8_t **id,
1552 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1553 db->private_data, struct db_ctdb_ctx);
1555 *id = (uint8_t *)&ctx->db_id;
1556 *idlen = sizeof(ctx->db_id);
1559 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1561 int hash_size, int tdb_flags,
1562 int open_flags, mode_t mode,
1563 enum dbwrap_lock_order lock_order)
1565 struct db_context *result;
1566 struct db_ctdb_ctx *db_ctdb;
1568 struct ctdbd_connection *conn;
1569 struct loadparm_context *lp_ctx;
1570 struct ctdb_db_priority prio;
1574 if (!lp_clustering()) {
1575 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1579 if (!(result = talloc_zero(mem_ctx, struct db_context))) {
1580 DEBUG(0, ("talloc failed\n"));
1581 TALLOC_FREE(result);
1585 if (!(db_ctdb = talloc(result, struct db_ctdb_ctx))) {
1586 DEBUG(0, ("talloc failed\n"));
1587 TALLOC_FREE(result);
1591 db_ctdb->transaction = NULL;
1592 db_ctdb->db = result;
1594 conn = messaging_ctdbd_connection();
1596 DEBUG(1, ("Could not connect to ctdb\n"));
1597 TALLOC_FREE(result);
1601 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1602 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1603 TALLOC_FREE(result);
1607 db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1609 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1610 result->lock_order = lock_order;
1612 /* only pass through specific flags */
1613 tdb_flags &= TDB_SEQNUM;
1615 /* honor permissions if user has specified O_CREAT */
1616 if (open_flags & O_CREAT) {
1617 chmod(db_path, mode);
1620 prio.db_id = db_ctdb->db_id;
1621 prio.priority = lock_order;
1623 status = ctdbd_control_local(
1624 conn, CTDB_CONTROL_SET_DB_PRIORITY, 0, 0,
1625 make_tdb_data((uint8_t *)&prio, sizeof(prio)),
1626 NULL, NULL, &cstatus);
1628 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1629 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1630 nt_errstr(status), cstatus));
1631 TALLOC_FREE(result);
1635 lp_ctx = loadparm_init_s3(db_path, loadparm_s3_helpers());
1637 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags,
1639 talloc_unlink(db_path, lp_ctx);
1640 if (db_ctdb->wtdb == NULL) {
1641 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1642 TALLOC_FREE(result);
1645 talloc_free(db_path);
1647 if (result->persistent) {
1648 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1649 ctdb_conn_msg_ctx(conn));
1650 if (db_ctdb->lock_ctx == NULL) {
1651 DEBUG(0, ("g_lock_ctx_init failed\n"));
1652 TALLOC_FREE(result);
1657 result->private_data = (void *)db_ctdb;
1658 result->fetch_locked = db_ctdb_fetch_locked;
1659 result->try_fetch_locked = db_ctdb_try_fetch_locked;
1660 result->parse_record = db_ctdb_parse_record;
1661 result->traverse = db_ctdb_traverse;
1662 result->traverse_read = db_ctdb_traverse_read;
1663 result->get_seqnum = db_ctdb_get_seqnum;
1664 result->transaction_start = db_ctdb_transaction_start;
1665 result->transaction_commit = db_ctdb_transaction_commit;
1666 result->transaction_cancel = db_ctdb_transaction_cancel;
1667 result->id = db_ctdb_id;
1668 result->stored_callback = NULL;
1670 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1671 name, db_ctdb->db_id));
1676 #else /* CLUSTER_SUPPORT */
1678 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1680 int hash_size, int tdb_flags,
1681 int open_flags, mode_t mode,
1682 enum dbwrap_lock_order lock_order)
1684 DEBUG(3, ("db_open_ctdb: no cluster support!\n"));