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;
342 loop over a marshalling buffer
344 - pass r==NULL to start
345 - loop the number of times indicated by m->count
347 static struct ctdb_rec_data *db_ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
349 struct ctdb_ltdb_header *header,
350 TDB_DATA *key, TDB_DATA *data)
352 r = db_ctdb_marshall_loop_next_key(m, r, key);
362 data->dptr = &r->data[r->keylen];
363 data->dsize = r->datalen;
364 if (header != NULL) {
365 data->dptr += sizeof(*header);
366 data->dsize -= sizeof(*header);
370 if (header != NULL) {
371 if (r->datalen < sizeof(*header)) {
374 *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
381 * CTDB transaction destructor
383 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
387 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
388 if (!NT_STATUS_IS_OK(status)) {
389 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h->lock_name,
397 * CTDB dbwrap API: transaction_start function
398 * starts a transaction on a persistent database
400 static int db_ctdb_transaction_start(struct db_context *db)
402 struct db_ctdb_transaction_handle *h;
404 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
407 if (!db->persistent) {
408 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
413 if (ctx->transaction) {
414 ctx->transaction->nesting++;
415 DEBUG(5, (__location__ " transaction start on db 0x%08x: nesting %d -> %d\n",
416 ctx->db_id, ctx->transaction->nesting - 1, ctx->transaction->nesting));
420 h = talloc_zero(db, struct db_ctdb_transaction_handle);
422 DEBUG(0,(__location__ " oom for transaction handle\n"));
428 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
429 (unsigned int)ctx->db_id);
430 if (h->lock_name == NULL) {
431 DEBUG(0, ("talloc_asprintf failed\n"));
437 * Wait a day, i.e. forever...
439 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
440 timeval_set(86400, 0));
441 if (!NT_STATUS_IS_OK(status)) {
442 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
447 talloc_set_destructor(h, db_ctdb_transaction_destructor);
449 ctx->transaction = h;
451 DEBUG(5,(__location__ " transaction started on db 0x%08x\n", ctx->db_id));
456 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
458 struct ctdb_ltdb_header *pheader,
462 struct ctdb_rec_data *rec = NULL;
463 struct ctdb_ltdb_header h;
476 * Walk the list of records written during this
477 * transaction. If we want to read one we have already
478 * written, return the last written sample. Thus we do not do
479 * a "break;" for the first hit, this record might have been
483 for (i=0; i<buf->count; i++) {
484 TDB_DATA tkey, tdata;
486 struct ctdb_ltdb_header hdr;
490 rec = db_ctdb_marshall_loop_next(buf, rec, &reqid, &hdr, &tkey,
496 if (tdb_data_equal(key, tkey)) {
508 data.dptr = (uint8_t *)talloc_memdup(mem_ctx, data.dptr,
510 if ((data.dsize != 0) && (data.dptr == NULL)) {
516 if (pheader != NULL) {
524 fetch a record inside a transaction
526 static NTSTATUS db_ctdb_transaction_fetch(struct db_ctdb_ctx *db,
528 TDB_DATA key, TDB_DATA *data)
530 struct db_ctdb_transaction_handle *h = db->transaction;
534 found = pull_newest_from_marshall_buffer(h->m_write, key, NULL,
540 status = db_ctdb_ltdb_fetch(h->ctx, key, NULL, mem_ctx, data);
542 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
550 * Fetch a record from a persistent database
551 * without record locking and without an active transaction.
553 * This just fetches from the local database copy.
554 * Since the databases are kept in syc cluster-wide,
555 * there is no point in doing a ctdb call to fetch the
556 * record from the lmaster. It does even harm since migration
557 * of records bump their RSN and hence render the persistent
558 * database inconsistent.
560 static NTSTATUS db_ctdb_fetch_persistent(struct db_ctdb_ctx *db,
562 TDB_DATA key, TDB_DATA *data)
566 status = db_ctdb_ltdb_fetch(db, key, NULL, mem_ctx, data);
568 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
575 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
576 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
578 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
582 struct db_record *result;
585 if (!(result = talloc(mem_ctx, struct db_record))) {
586 DEBUG(0, ("talloc failed\n"));
590 result->private_data = ctx->transaction;
592 result->key.dsize = key.dsize;
593 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
595 if (result->key.dptr == NULL) {
596 DEBUG(0, ("talloc failed\n"));
601 result->store = db_ctdb_store_transaction;
602 result->delete_rec = db_ctdb_delete_transaction;
604 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
605 NULL, result, &result->value)) {
609 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
610 if (ctdb_data.dptr == NULL) {
611 /* create the record */
612 result->value = tdb_null;
616 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
617 result->value.dptr = NULL;
619 if ((result->value.dsize != 0)
620 && !(result->value.dptr = (uint8_t *)talloc_memdup(
621 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
622 result->value.dsize))) {
623 DEBUG(0, ("talloc failed\n"));
627 SAFE_FREE(ctdb_data.dptr);
632 static int db_ctdb_record_destructor(struct db_record **recp)
634 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
635 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
636 rec->private_data, struct db_ctdb_transaction_handle);
637 int ret = h->ctx->db->transaction_commit(h->ctx->db);
639 DEBUG(0,(__location__ " transaction_commit failed\n"));
645 auto-create a transaction for persistent databases
647 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
652 struct db_record *rec, **recp;
654 res = db_ctdb_transaction_start(ctx->db);
659 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
661 ctx->db->transaction_cancel(ctx->db);
665 /* destroy this transaction when we release the lock */
666 recp = talloc(rec, struct db_record *);
668 ctx->db->transaction_cancel(ctx->db);
673 talloc_set_destructor(recp, db_ctdb_record_destructor);
679 stores a record inside a transaction
681 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
682 TDB_DATA key, TDB_DATA data)
684 TALLOC_CTX *tmp_ctx = talloc_new(h);
686 struct ctdb_ltdb_header header;
690 /* we need the header so we can update the RSN */
692 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
695 rec = tdb_fetch_compat(h->ctx->wtdb->tdb, key);
697 if (rec.dptr != NULL) {
698 memcpy(&header, rec.dptr,
699 sizeof(struct ctdb_ltdb_header));
700 rec.dsize -= sizeof(struct ctdb_ltdb_header);
703 * a special case, we are writing the same
704 * data that is there now
706 if (data.dsize == rec.dsize &&
708 rec.dptr + sizeof(struct ctdb_ltdb_header),
711 talloc_free(tmp_ctx);
718 header.dmaster = get_my_vnn();
721 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
722 if (h->m_write == NULL) {
723 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
724 talloc_free(tmp_ctx);
725 return NT_STATUS_NO_MEMORY;
728 talloc_free(tmp_ctx);
734 a record store inside a transaction
736 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
738 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
739 rec->private_data, struct db_ctdb_transaction_handle);
742 status = db_ctdb_transaction_store(h, rec->key, data);
747 a record delete inside a transaction
749 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
751 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
752 rec->private_data, struct db_ctdb_transaction_handle);
755 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
759 static void db_ctdb_fetch_db_seqnum_parser(
760 TDB_DATA key, struct ctdb_ltdb_header *header,
761 TDB_DATA data, void *private_data)
763 uint64_t *seqnum = (uint64_t *)private_data;
765 if (data.dsize != sizeof(uint64_t)) {
769 memcpy(seqnum, data.dptr, sizeof(*seqnum));
773 * Fetch the db sequence number of a persistent db directly from the db.
775 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
781 if (seqnum == NULL) {
782 return NT_STATUS_INVALID_PARAMETER;
785 key = string_term_tdb_data(CTDB_DB_SEQNUM_KEY);
787 status = db_ctdb_ltdb_parse(
788 db, key, db_ctdb_fetch_db_seqnum_parser, seqnum);
790 if (NT_STATUS_IS_OK(status)) {
793 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
801 * Store the database sequence number inside a transaction.
803 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
807 const char *keyname = CTDB_DB_SEQNUM_KEY;
811 key = string_term_tdb_data(keyname);
813 data.dptr = (uint8_t *)&seqnum;
814 data.dsize = sizeof(uint64_t);
816 status = db_ctdb_transaction_store(h, key, data);
824 static int db_ctdb_transaction_commit(struct db_context *db)
826 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
830 struct db_ctdb_transaction_handle *h = ctx->transaction;
831 uint64_t old_seqnum, new_seqnum;
835 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
839 if (h->nested_cancel) {
840 db->transaction_cancel(db);
841 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
845 if (h->nesting != 0) {
847 DEBUG(5, (__location__ " transaction commit on db 0x%08x: nesting %d -> %d\n",
848 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
852 if (h->m_write == NULL) {
854 * No changes were made, so don't change the seqnum,
855 * don't push to other node, just exit with success.
861 DEBUG(5,(__location__ " transaction commit on db 0x%08x\n", ctx->db_id));
864 * As the last db action before committing, bump the database sequence
865 * number. Note that this undoes all changes to the seqnum records
866 * performed under the transaction. This record is not meant to be
867 * modified by user interaction. It is for internal use only...
869 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
870 if (!NT_STATUS_IS_OK(rets)) {
871 DEBUG(1, (__location__ " failed to fetch the db sequence number "
872 "in transaction commit on db 0x%08x\n", ctx->db_id));
877 new_seqnum = old_seqnum + 1;
879 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
880 if (!NT_STATUS_IS_OK(rets)) {
881 DEBUG(1, (__location__ "failed to store the db sequence number "
882 " in transaction commit on db 0x%08x\n", ctx->db_id));
888 /* tell ctdbd to commit to the other nodes */
889 rets = ctdbd_control_local(messaging_ctdbd_connection(),
890 CTDB_CONTROL_TRANS3_COMMIT,
892 db_ctdb_marshall_finish(h->m_write),
893 NULL, NULL, &status);
894 if (!NT_STATUS_IS_OK(rets) || status != 0) {
896 * The TRANS3_COMMIT control should only possibly fail when a
897 * recovery has been running concurrently. In any case, the db
898 * will be the same on all nodes, either the new copy or the
899 * old copy. This can be detected by comparing the old and new
900 * local sequence numbers.
902 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
903 if (!NT_STATUS_IS_OK(rets)) {
904 DEBUG(1, (__location__ " failed to refetch db sequence "
905 "number after failed TRANS3_COMMIT\n"));
910 if (new_seqnum == old_seqnum) {
911 /* Recovery prevented all our changes: retry. */
914 if (new_seqnum != (old_seqnum + 1)) {
915 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
916 "old_seqnum[%lu] + (0 or 1) after failed "
917 "TRANS3_COMMIT - this should not happen!\n",
918 (unsigned long)new_seqnum,
919 (unsigned long)old_seqnum));
924 * Recovery propagated our changes to all nodes, completing
925 * our commit for us - succeed.
932 h->ctx->transaction = NULL;
941 static int db_ctdb_transaction_cancel(struct db_context *db)
943 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
945 struct db_ctdb_transaction_handle *h = ctx->transaction;
948 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
952 if (h->nesting != 0) {
954 h->nested_cancel = true;
955 DEBUG(5, (__location__ " transaction cancel on db 0x%08x: nesting %d -> %d\n",
956 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
960 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
962 ctx->transaction = NULL;
968 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
970 struct db_ctdb_rec *crec = talloc_get_type_abort(
971 rec->private_data, struct db_ctdb_rec);
973 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
978 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
979 static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
982 struct ctdb_control_schedule_for_deletion *dd;
985 struct db_ctdb_rec *crec = talloc_get_type_abort(
986 rec->private_data, struct db_ctdb_rec);
988 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
989 indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
990 if (indata.dptr == NULL) {
991 DEBUG(0, (__location__ " talloc failed!\n"));
992 return NT_STATUS_NO_MEMORY;
995 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
996 dd->db_id = crec->ctdb_ctx->db_id;
997 dd->hdr = crec->header;
998 dd->keylen = rec->key.dsize;
999 memcpy(dd->key, rec->key.dptr, rec->key.dsize);
1001 status = ctdbd_control_local(messaging_ctdbd_connection(),
1002 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1003 crec->ctdb_ctx->db_id,
1004 CTDB_CTRL_FLAG_NOREPLY, /* flags */
1009 talloc_free(indata.dptr);
1011 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1012 DEBUG(1, (__location__ " Error sending local control "
1013 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
1014 nt_errstr(status), cstatus));
1015 if (NT_STATUS_IS_OK(status)) {
1016 status = NT_STATUS_UNSUCCESSFUL;
1024 static NTSTATUS db_ctdb_delete(struct db_record *rec)
1030 * We have to store the header with empty data. TODO: Fix the
1036 status = db_ctdb_store(rec, data, 0);
1037 if (!NT_STATUS_IS_OK(status)) {
1041 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
1042 status = db_ctdb_send_schedule_for_deletion(rec);
1048 static int db_ctdb_record_destr(struct db_record* data)
1050 struct db_ctdb_rec *crec = talloc_get_type_abort(
1051 data->private_data, struct db_ctdb_rec);
1054 DEBUG(10, (DEBUGLEVEL > 10
1055 ? "Unlocking db %u key %s\n"
1056 : "Unlocking db %u key %.20s\n",
1057 (int)crec->ctdb_ctx->db_id,
1058 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
1061 tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key);
1063 threshold = lp_ctdb_locktime_warn_threshold();
1064 if (threshold != 0) {
1065 double timediff = timeval_elapsed(&crec->lock_time);
1066 if ((timediff * 1000) > threshold) {
1069 key = hex_encode_talloc(data,
1070 (unsigned char *)data->key.dptr,
1072 DEBUG(0, ("Held tdb lock on db %s, key %s %f seconds\n",
1073 tdb_name(crec->ctdb_ctx->wtdb->tdb), key,
1082 * Check whether we have a valid local copy of the given record,
1083 * either for reading or for writing.
1085 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data, bool read_only)
1087 struct ctdb_ltdb_header *hdr;
1089 if (ctdb_data.dptr == NULL)
1092 if (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header))
1095 hdr = (struct ctdb_ltdb_header *)ctdb_data.dptr;
1097 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1098 if (hdr->dmaster != get_my_vnn()) {
1099 /* If we're not dmaster, it must be r/o copy. */
1100 return read_only && (hdr->flags & CTDB_REC_RO_HAVE_READONLY);
1104 * If we want write access, no one may have r/o copies.
1106 return read_only || !(hdr->flags & CTDB_REC_RO_HAVE_DELEGATIONS);
1108 return (hdr->dmaster == get_my_vnn());
1112 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
1113 TALLOC_CTX *mem_ctx,
1117 struct db_record *result;
1118 struct db_ctdb_rec *crec;
1121 int migrate_attempts = 0;
1124 if (!(result = talloc(mem_ctx, struct db_record))) {
1125 DEBUG(0, ("talloc failed\n"));
1129 if (!(crec = talloc_zero(result, struct db_ctdb_rec))) {
1130 DEBUG(0, ("talloc failed\n"));
1131 TALLOC_FREE(result);
1135 result->db = ctx->db;
1136 result->private_data = (void *)crec;
1137 crec->ctdb_ctx = ctx;
1139 result->key.dsize = key.dsize;
1140 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
1142 if (result->key.dptr == NULL) {
1143 DEBUG(0, ("talloc failed\n"));
1144 TALLOC_FREE(result);
1149 * Do a blocking lock on the record
1153 if (DEBUGLEVEL >= 10) {
1154 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1155 DEBUG(10, (DEBUGLEVEL > 10
1156 ? "Locking db %u key %s\n"
1157 : "Locking db %u key %.20s\n",
1158 (int)crec->ctdb_ctx->db_id, keystr));
1159 TALLOC_FREE(keystr);
1163 ? tdb_chainlock_nonblock(ctx->wtdb->tdb, key)
1164 : tdb_chainlock(ctx->wtdb->tdb, key);
1166 DEBUG(3, ("tdb_chainlock failed\n"));
1167 TALLOC_FREE(result);
1171 result->store = db_ctdb_store;
1172 result->delete_rec = db_ctdb_delete;
1173 talloc_set_destructor(result, db_ctdb_record_destr);
1175 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
1178 * See if we have a valid record and we are the dmaster. If so, we can
1179 * take the shortcut and just return it.
1182 if (!db_ctdb_can_use_local_copy(ctdb_data, false)) {
1183 SAFE_FREE(ctdb_data.dptr);
1184 tdb_chainunlock(ctx->wtdb->tdb, key);
1185 talloc_set_destructor(result, NULL);
1187 if (tryonly && (migrate_attempts != 0)) {
1188 DEBUG(5, ("record migrated away again\n"));
1189 TALLOC_FREE(result);
1193 migrate_attempts += 1;
1195 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1196 ctdb_data.dptr, ctdb_data.dptr ?
1197 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
1200 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->flags : 0));
1202 status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id,
1204 if (!NT_STATUS_IS_OK(status)) {
1205 DEBUG(5, ("ctdb_migrate failed: %s\n",
1206 nt_errstr(status)));
1207 TALLOC_FREE(result);
1210 /* now its migrated, try again */
1214 if (migrate_attempts > 10) {
1215 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s needed %d "
1216 "attempts\n", tdb_name(ctx->wtdb->tdb),
1217 hex_encode_talloc(talloc_tos(),
1218 (unsigned char *)key.dptr,
1223 GetTimeOfDay(&crec->lock_time);
1225 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1227 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1228 result->value.dptr = NULL;
1230 if ((result->value.dsize != 0)
1231 && !(result->value.dptr = (uint8_t *)talloc_memdup(
1232 result, ctdb_data.dptr + sizeof(crec->header),
1233 result->value.dsize))) {
1234 DEBUG(0, ("talloc failed\n"));
1235 TALLOC_FREE(result);
1238 SAFE_FREE(ctdb_data.dptr);
1243 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1244 TALLOC_CTX *mem_ctx,
1247 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1248 struct db_ctdb_ctx);
1250 if (ctx->transaction != NULL) {
1251 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1254 if (db->persistent) {
1255 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1258 return fetch_locked_internal(ctx, mem_ctx, key, false);
1261 static struct db_record *db_ctdb_try_fetch_locked(struct db_context *db,
1262 TALLOC_CTX *mem_ctx,
1265 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1266 struct db_ctdb_ctx);
1268 if (ctx->transaction != NULL) {
1269 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1272 if (db->persistent) {
1273 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1276 return fetch_locked_internal(ctx, mem_ctx, key, true);
1280 fetch (unlocked, no migration) operation on ctdb
1282 static NTSTATUS db_ctdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
1283 TDB_DATA key, TDB_DATA *data)
1285 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1286 struct db_ctdb_ctx);
1290 if (ctx->transaction) {
1291 return db_ctdb_transaction_fetch(ctx, mem_ctx, key, data);
1294 if (db->persistent) {
1295 return db_ctdb_fetch_persistent(ctx, mem_ctx, key, data);
1298 /* try a direct fetch */
1299 ctdb_data = tdb_fetch_compat(ctx->wtdb->tdb, key);
1302 * See if we have a valid record and we are the dmaster. If so, we can
1303 * take the shortcut and just return it.
1304 * we bypass the dmaster check for persistent databases
1306 if (db_ctdb_can_use_local_copy(ctdb_data, true)) {
1308 * We have a valid local copy - avoid the ctdb protocol op
1310 data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
1312 data->dptr = (uint8_t *)talloc_memdup(
1313 mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header),
1316 SAFE_FREE(ctdb_data.dptr);
1318 if (data->dptr == NULL) {
1319 return NT_STATUS_NO_MEMORY;
1321 return NT_STATUS_OK;
1324 SAFE_FREE(ctdb_data.dptr);
1327 * We weren't able to get it locally - ask ctdb to fetch it for us.
1328 * If we already had *something*, it's probably worth making a local
1331 status = ctdbd_fetch(messaging_ctdbd_connection(), ctx->db_id, key,
1333 ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header));
1334 if (!NT_STATUS_IS_OK(status)) {
1335 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status)));
1341 static NTSTATUS db_ctdb_parse_record(struct db_context *db, TDB_DATA key,
1342 void (*parser)(TDB_DATA key,
1344 void *private_data),
1350 status = db_ctdb_fetch(db, talloc_tos(), key, &data);
1351 if (!NT_STATUS_IS_OK(status)) {
1354 parser(key, data, private_data);
1355 TALLOC_FREE(data.dptr);
1356 return NT_STATUS_OK;
1359 struct traverse_state {
1360 struct db_context *db;
1361 int (*fn)(struct db_record *rec, void *private_data);
1366 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1368 struct traverse_state *state = (struct traverse_state *)private_data;
1369 struct db_record *rec;
1370 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1371 /* we have to give them a locked record to prevent races */
1372 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1373 if (rec && rec->value.dsize > 0) {
1374 state->fn(rec, state->private_data);
1376 talloc_free(tmp_ctx);
1379 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1382 struct traverse_state *state = (struct traverse_state *)private_data;
1383 struct db_record *rec;
1384 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1388 * Skip the __db_sequence_number__ key:
1389 * This is used for persistent transactions internally.
1391 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1392 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1397 /* we have to give them a locked record to prevent races */
1398 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1399 if (rec && rec->value.dsize > 0) {
1400 ret = state->fn(rec, state->private_data);
1404 talloc_free(tmp_ctx);
1408 /* wrapper to use traverse_persistent_callback with dbwrap */
1409 static int traverse_persistent_callback_dbwrap(struct db_record *rec, void* data)
1411 return traverse_persistent_callback(NULL, rec->key, rec->value, data);
1415 static int db_ctdb_traverse(struct db_context *db,
1416 int (*fn)(struct db_record *rec,
1417 void *private_data),
1421 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1422 struct db_ctdb_ctx);
1423 struct traverse_state state;
1427 state.private_data = private_data;
1430 if (db->persistent) {
1431 struct tdb_context *ltdb = ctx->wtdb->tdb;
1434 /* for persistent databases we don't need to do a ctdb traverse,
1435 we can do a faster local traverse */
1436 ret = tdb_traverse(ltdb, traverse_persistent_callback, &state);
1440 if (ctx->transaction && ctx->transaction->m_write) {
1442 * we now have to handle keys not yet
1443 * present at transaction start
1445 struct db_context *newkeys = db_open_rbt(talloc_tos());
1446 struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
1447 struct ctdb_rec_data *rec=NULL;
1451 if (newkeys == NULL) {
1455 for (i=0; i<mbuf->count; i++) {
1457 rec = db_ctdb_marshall_loop_next_key(
1459 SMB_ASSERT(rec != NULL);
1461 if (!tdb_exists(ltdb, key)) {
1462 dbwrap_store(newkeys, key, tdb_null, 0);
1465 status = dbwrap_traverse(newkeys,
1466 traverse_persistent_callback_dbwrap,
1469 talloc_free(newkeys);
1470 if (!NT_STATUS_IS_OK(status)) {
1478 status = ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1479 if (!NT_STATUS_IS_OK(status)) {
1485 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1487 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1490 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1492 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1495 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1497 struct traverse_state *state = (struct traverse_state *)private_data;
1498 struct db_record rec;
1501 rec.store = db_ctdb_store_deny;
1502 rec.delete_rec = db_ctdb_delete_deny;
1503 rec.private_data = state->db;
1504 state->fn(&rec, state->private_data);
1508 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1511 struct traverse_state *state = (struct traverse_state *)private_data;
1512 struct db_record rec;
1515 * Skip the __db_sequence_number__ key:
1516 * This is used for persistent transactions internally.
1518 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1519 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1526 rec.store = db_ctdb_store_deny;
1527 rec.delete_rec = db_ctdb_delete_deny;
1528 rec.private_data = state->db;
1530 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1531 /* a deleted record */
1534 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1535 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1538 return state->fn(&rec, state->private_data);
1541 static int db_ctdb_traverse_read(struct db_context *db,
1542 int (*fn)(struct db_record *rec,
1543 void *private_data),
1547 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1548 struct db_ctdb_ctx);
1549 struct traverse_state state;
1553 state.private_data = private_data;
1556 if (db->persistent) {
1557 /* for persistent databases we don't need to do a ctdb traverse,
1558 we can do a faster local traverse */
1559 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1562 status = ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1563 if (!NT_STATUS_IS_OK(status)) {
1569 static int db_ctdb_get_seqnum(struct db_context *db)
1571 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1572 struct db_ctdb_ctx);
1573 return tdb_get_seqnum(ctx->wtdb->tdb);
1576 static void db_ctdb_id(struct db_context *db, const uint8_t **id,
1579 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1580 db->private_data, struct db_ctdb_ctx);
1582 *id = (uint8_t *)&ctx->db_id;
1583 *idlen = sizeof(ctx->db_id);
1586 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1588 int hash_size, int tdb_flags,
1589 int open_flags, mode_t mode,
1590 enum dbwrap_lock_order lock_order)
1592 struct db_context *result;
1593 struct db_ctdb_ctx *db_ctdb;
1595 struct ctdbd_connection *conn;
1596 struct loadparm_context *lp_ctx;
1597 struct ctdb_db_priority prio;
1601 if (!lp_clustering()) {
1602 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1606 if (!(result = talloc_zero(mem_ctx, struct db_context))) {
1607 DEBUG(0, ("talloc failed\n"));
1608 TALLOC_FREE(result);
1612 if (!(db_ctdb = talloc(result, struct db_ctdb_ctx))) {
1613 DEBUG(0, ("talloc failed\n"));
1614 TALLOC_FREE(result);
1618 db_ctdb->transaction = NULL;
1619 db_ctdb->db = result;
1621 conn = messaging_ctdbd_connection();
1623 DEBUG(1, ("Could not connect to ctdb\n"));
1624 TALLOC_FREE(result);
1628 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1629 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1630 TALLOC_FREE(result);
1634 db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1636 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1637 result->lock_order = lock_order;
1639 /* only pass through specific flags */
1640 tdb_flags &= TDB_SEQNUM;
1642 /* honor permissions if user has specified O_CREAT */
1643 if (open_flags & O_CREAT) {
1644 chmod(db_path, mode);
1647 prio.db_id = db_ctdb->db_id;
1648 prio.priority = lock_order;
1650 status = ctdbd_control_local(
1651 conn, CTDB_CONTROL_SET_DB_PRIORITY, 0, 0,
1652 make_tdb_data((uint8_t *)&prio, sizeof(prio)),
1653 NULL, NULL, &cstatus);
1655 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1656 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1657 nt_errstr(status), cstatus));
1658 TALLOC_FREE(result);
1662 lp_ctx = loadparm_init_s3(db_path, loadparm_s3_helpers());
1664 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags,
1666 talloc_unlink(db_path, lp_ctx);
1667 if (db_ctdb->wtdb == NULL) {
1668 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1669 TALLOC_FREE(result);
1672 talloc_free(db_path);
1674 if (result->persistent) {
1675 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1676 ctdb_conn_msg_ctx(conn));
1677 if (db_ctdb->lock_ctx == NULL) {
1678 DEBUG(0, ("g_lock_ctx_init failed\n"));
1679 TALLOC_FREE(result);
1684 result->private_data = (void *)db_ctdb;
1685 result->fetch_locked = db_ctdb_fetch_locked;
1686 result->try_fetch_locked = db_ctdb_try_fetch_locked;
1687 result->parse_record = db_ctdb_parse_record;
1688 result->traverse = db_ctdb_traverse;
1689 result->traverse_read = db_ctdb_traverse_read;
1690 result->get_seqnum = db_ctdb_get_seqnum;
1691 result->transaction_start = db_ctdb_transaction_start;
1692 result->transaction_commit = db_ctdb_transaction_commit;
1693 result->transaction_cancel = db_ctdb_transaction_cancel;
1694 result->id = db_ctdb_id;
1695 result->stored_callback = NULL;
1697 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1698 name, db_ctdb->db_id));
1703 #else /* CLUSTER_SUPPORT */
1705 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1707 int hash_size, int tdb_flags,
1708 int open_flags, mode_t mode,
1709 enum dbwrap_lock_order lock_order)
1711 DEBUG(3, ("db_open_ctdb: no cluster support!\n"));