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 #include "ctdb_private.h"
31 #include "ctdbd_conn.h"
32 #include "dbwrap/dbwrap.h"
33 #include "dbwrap/dbwrap_private.h"
34 #include "dbwrap/dbwrap_ctdb.h"
37 #include "lib/cluster_support.h"
38 #include "lib/util/tevent_ntstatus.h"
40 struct db_ctdb_transaction_handle {
41 struct db_ctdb_ctx *ctx;
43 * we store the writes done under a transaction:
45 struct ctdb_marshall_buffer *m_write;
52 struct db_context *db;
53 struct ctdbd_connection *conn;
54 struct tdb_wrap *wtdb;
56 struct db_ctdb_transaction_handle *transaction;
57 struct g_lock_ctx *lock_ctx;
59 /* thresholds for warning messages */
60 int warn_unlock_msecs;
61 int warn_migrate_msecs;
62 int warn_migrate_attempts;
63 int warn_locktime_msecs;
67 struct db_ctdb_ctx *ctdb_ctx;
68 struct ctdb_ltdb_header header;
69 struct timeval lock_time;
72 struct ctdb_async_ctx {
74 struct ctdbd_connection *async_conn;
77 static struct ctdb_async_ctx ctdb_async_ctx;
79 static int ctdb_async_ctx_init_internal(TALLOC_CTX *mem_ctx,
80 struct tevent_context *ev,
86 TALLOC_FREE(ctdb_async_ctx.async_conn);
87 ctdb_async_ctx.initialized = false;
90 if (ctdb_async_ctx.initialized) {
95 ret = ctdbd_init_connection(mem_ctx,
98 &ctdb_async_ctx.async_conn);
101 if (ctdb_async_ctx.async_conn == NULL) {
102 DBG_ERR("ctdbd_init_connection failed\n");
106 ret = ctdbd_setup_fde(ctdb_async_ctx.async_conn, ev);
108 DBG_ERR("ctdbd_setup_ev failed\n");
115 static int ctdb_async_ctx_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev)
117 return ctdb_async_ctx_init_internal(mem_ctx, ev, false);
120 int ctdb_async_ctx_reinit(TALLOC_CTX *mem_ctx, struct tevent_context *ev)
122 return ctdb_async_ctx_init_internal(mem_ctx, ev, true);
125 static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
127 enum TDB_ERROR tret = tdb_error(tdb);
129 return map_nt_error_from_tdb(tret);
132 struct db_ctdb_ltdb_parse_state {
133 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
134 TDB_DATA data, void *private_data);
138 static int db_ctdb_ltdb_parser(TDB_DATA key, TDB_DATA data,
141 struct db_ctdb_ltdb_parse_state *state =
142 (struct db_ctdb_ltdb_parse_state *)private_data;
144 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
149 key, (struct ctdb_ltdb_header *)data.dptr,
150 make_tdb_data(data.dptr + sizeof(struct ctdb_ltdb_header),
151 data.dsize - sizeof(struct ctdb_ltdb_header)),
152 state->private_data);
156 static NTSTATUS db_ctdb_ltdb_parse(
157 struct db_ctdb_ctx *db, TDB_DATA key,
158 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
159 TDB_DATA data, void *private_data),
162 struct db_ctdb_ltdb_parse_state state;
165 state.parser = parser;
166 state.private_data = private_data;
168 ret = tdb_parse_record(db->wtdb->tdb, key, db_ctdb_ltdb_parser,
171 return NT_STATUS_NOT_FOUND;
177 * Store a record together with the ctdb record header
178 * in the local copy of the database.
180 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
182 struct ctdb_ltdb_header *header,
188 recs[0] = (TDB_DATA) { .dptr = (uint8_t *)header,
189 .dsize = sizeof(struct ctdb_ltdb_header) };
192 ret = tdb_storev(db->wtdb->tdb, key, recs, 2, TDB_REPLACE);
194 return (ret == 0) ? NT_STATUS_OK
195 : tdb_error_to_ntstatus(db->wtdb->tdb);
200 form a ctdb_rec_data record from a key/data pair
202 static struct ctdb_rec_data_old *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
204 struct ctdb_ltdb_header *header,
208 struct ctdb_rec_data_old *d;
210 length = offsetof(struct ctdb_rec_data_old, data) + key.dsize +
211 data.dsize + sizeof(*header);
212 d = (struct ctdb_rec_data_old *)talloc_size(mem_ctx, length);
218 d->keylen = key.dsize;
219 memcpy(&d->data[0], key.dptr, key.dsize);
221 d->datalen = data.dsize + sizeof(*header);
222 memcpy(&d->data[key.dsize], header, sizeof(*header));
223 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
228 /* helper function for marshalling multiple records */
229 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
230 struct ctdb_marshall_buffer *m,
234 struct ctdb_ltdb_header *header,
237 struct ctdb_rec_data_old *r;
238 size_t m_size, r_size;
239 struct ctdb_marshall_buffer *m2 = NULL;
241 r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
248 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
249 mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
256 m_size = talloc_get_size(m);
257 r_size = talloc_get_size(r);
259 m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
260 mem_ctx, m, m_size + r_size);
266 memcpy(m_size + (uint8_t *)m2, r, r_size);
275 /* we've finished marshalling, return a data blob with the marshalled records */
276 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
279 data.dptr = (uint8_t *)m;
280 data.dsize = talloc_get_size(m);
285 loop over a marshalling buffer
287 - pass r==NULL to start
288 - loop the number of times indicated by m->count
290 static struct ctdb_rec_data_old *db_ctdb_marshall_loop_next_key(
291 struct ctdb_marshall_buffer *m, struct ctdb_rec_data_old *r, TDB_DATA *key)
294 r = (struct ctdb_rec_data_old *)&m->data[0];
296 r = (struct ctdb_rec_data_old *)(r->length + (uint8_t *)r);
299 key->dptr = &r->data[0];
300 key->dsize = r->keylen;
304 static bool db_ctdb_marshall_buf_parse(
305 struct ctdb_rec_data_old *r, uint32_t *reqid,
306 struct ctdb_ltdb_header **header, TDB_DATA *data)
308 if (r->datalen < sizeof(struct ctdb_ltdb_header)) {
314 data->dptr = &r->data[r->keylen] + sizeof(struct ctdb_ltdb_header);
315 data->dsize = r->datalen - sizeof(struct ctdb_ltdb_header);
317 *header = (struct ctdb_ltdb_header *)&r->data[r->keylen];
323 * CTDB transaction destructor
325 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
329 status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
330 if (!NT_STATUS_IS_OK(status)) {
331 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h->lock_name,
339 * CTDB dbwrap API: transaction_start function
340 * starts a transaction on a persistent database
342 static int db_ctdb_transaction_start(struct db_context *db)
344 struct db_ctdb_transaction_handle *h;
346 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
349 if (!db->persistent) {
350 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
355 if (ctx->transaction) {
356 ctx->transaction->nesting++;
357 DEBUG(5, (__location__ " transaction start on db 0x%08x: nesting %d -> %d\n",
358 ctx->db_id, ctx->transaction->nesting - 1, ctx->transaction->nesting));
362 h = talloc_zero(db, struct db_ctdb_transaction_handle);
364 DEBUG(0,(__location__ " oom for transaction handle\n"));
370 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
371 (unsigned int)ctx->db_id);
372 if (h->lock_name == NULL) {
373 DEBUG(0, ("talloc_asprintf failed\n"));
379 * Wait a day, i.e. forever...
381 status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
382 timeval_set(86400, 0));
383 if (!NT_STATUS_IS_OK(status)) {
384 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
389 talloc_set_destructor(h, db_ctdb_transaction_destructor);
391 ctx->transaction = h;
393 DEBUG(5,(__location__ " transaction started on db 0x%08x\n", ctx->db_id));
398 static bool parse_newest_in_marshall_buffer(
399 struct ctdb_marshall_buffer *buf, TDB_DATA key,
400 void (*parser)(TDB_DATA key, struct ctdb_ltdb_header *header,
401 TDB_DATA data, void *private_data),
404 struct ctdb_rec_data_old *rec = NULL;
405 struct ctdb_ltdb_header *h = NULL;
414 * Walk the list of records written during this
415 * transaction. If we want to read one we have already
416 * written, return the last written sample. Thus we do not do
417 * a "break;" for the first hit, this record might have been
421 for (i=0; i<buf->count; i++) {
425 rec = db_ctdb_marshall_loop_next_key(buf, rec, &tkey);
430 if (!tdb_data_equal(key, tkey)) {
434 if (!db_ctdb_marshall_buf_parse(rec, &reqid, &h, &data)) {
443 parser(key, h, data, private_data);
448 struct pull_newest_from_marshall_buffer_state {
449 struct ctdb_ltdb_header *pheader;
454 static void pull_newest_from_marshall_buffer_parser(
455 TDB_DATA key, struct ctdb_ltdb_header *header,
456 TDB_DATA data, void *private_data)
458 struct pull_newest_from_marshall_buffer_state *state =
459 (struct pull_newest_from_marshall_buffer_state *)private_data;
461 if (state->pheader != NULL) {
462 memcpy(state->pheader, header, sizeof(*state->pheader));
464 if (state->pdata != NULL) {
465 state->pdata->dsize = data.dsize;
466 state->pdata->dptr = (uint8_t *)talloc_memdup(
467 state->mem_ctx, data.dptr, data.dsize);
471 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
473 struct ctdb_ltdb_header *pheader,
477 struct pull_newest_from_marshall_buffer_state state;
479 state.pheader = pheader;
480 state.mem_ctx = mem_ctx;
483 if (!parse_newest_in_marshall_buffer(
484 buf, key, pull_newest_from_marshall_buffer_parser,
488 if ((pdata != NULL) && (pdata->dsize != 0) && (pdata->dptr == NULL)) {
495 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
496 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
498 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
502 struct db_record *result;
505 if (!(result = talloc(mem_ctx, struct db_record))) {
506 DEBUG(0, ("talloc failed\n"));
510 result->db = ctx->db;
511 result->private_data = ctx->transaction;
513 result->key.dsize = key.dsize;
514 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
516 if (result->key.dptr == NULL) {
517 DEBUG(0, ("talloc failed\n"));
522 result->store = db_ctdb_store_transaction;
523 result->delete_rec = db_ctdb_delete_transaction;
525 if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
526 NULL, result, &result->value)) {
530 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
531 if (ctdb_data.dptr == NULL) {
532 /* create the record */
533 result->value = tdb_null;
537 result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
538 result->value.dptr = NULL;
540 if ((result->value.dsize != 0)
541 && !(result->value.dptr = (uint8_t *)talloc_memdup(
542 result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
543 result->value.dsize))) {
544 DEBUG(0, ("talloc failed\n"));
548 SAFE_FREE(ctdb_data.dptr);
553 static int db_ctdb_record_destructor(struct db_record **recp)
555 struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
556 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
557 rec->private_data, struct db_ctdb_transaction_handle);
558 int ret = h->ctx->db->transaction_commit(h->ctx->db);
560 DEBUG(0,(__location__ " transaction_commit failed\n"));
566 auto-create a transaction for persistent databases
568 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
573 struct db_record *rec, **recp;
575 res = db_ctdb_transaction_start(ctx->db);
580 rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
582 ctx->db->transaction_cancel(ctx->db);
586 /* destroy this transaction when we release the lock */
587 recp = talloc(rec, struct db_record *);
589 ctx->db->transaction_cancel(ctx->db);
594 talloc_set_destructor(recp, db_ctdb_record_destructor);
600 stores a record inside a transaction
602 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
603 TDB_DATA key, TDB_DATA data)
605 TALLOC_CTX *tmp_ctx = talloc_new(h);
607 struct ctdb_ltdb_header header;
611 /* we need the header so we can update the RSN */
613 if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
616 rec = tdb_fetch(h->ctx->wtdb->tdb, key);
618 if (rec.dptr != NULL) {
619 memcpy(&header, rec.dptr,
620 sizeof(struct ctdb_ltdb_header));
621 rec.dsize -= sizeof(struct ctdb_ltdb_header);
624 * a special case, we are writing the same
625 * data that is there now
627 if (data.dsize == rec.dsize &&
629 rec.dptr + sizeof(struct ctdb_ltdb_header),
632 talloc_free(tmp_ctx);
639 header.dmaster = ctdbd_vnn(h->ctx->conn);
642 h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
643 if (h->m_write == NULL) {
644 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
645 talloc_free(tmp_ctx);
646 return NT_STATUS_NO_MEMORY;
649 talloc_free(tmp_ctx);
655 a record store inside a transaction
657 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
659 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
660 rec->private_data, struct db_ctdb_transaction_handle);
663 status = db_ctdb_transaction_store(h, rec->key, data);
668 a record delete inside a transaction
670 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
672 struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
673 rec->private_data, struct db_ctdb_transaction_handle);
676 status = db_ctdb_transaction_store(h, rec->key, tdb_null);
680 static void db_ctdb_fetch_db_seqnum_parser(
681 TDB_DATA key, struct ctdb_ltdb_header *header,
682 TDB_DATA data, void *private_data)
684 uint64_t *seqnum = (uint64_t *)private_data;
686 if (data.dsize != sizeof(uint64_t)) {
690 memcpy(seqnum, data.dptr, sizeof(*seqnum));
694 * Fetch the db sequence number of a persistent db directly from the db.
696 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
702 if (seqnum == NULL) {
703 return NT_STATUS_INVALID_PARAMETER;
706 key = string_term_tdb_data(CTDB_DB_SEQNUM_KEY);
708 status = db_ctdb_ltdb_parse(
709 db, key, db_ctdb_fetch_db_seqnum_parser, seqnum);
711 if (NT_STATUS_IS_OK(status)) {
714 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
722 * Store the database sequence number inside a transaction.
724 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
728 const char *keyname = CTDB_DB_SEQNUM_KEY;
732 key = string_term_tdb_data(keyname);
734 data.dptr = (uint8_t *)&seqnum;
735 data.dsize = sizeof(uint64_t);
737 status = db_ctdb_transaction_store(h, key, data);
745 static int db_ctdb_transaction_commit(struct db_context *db)
747 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
751 struct db_ctdb_transaction_handle *h = ctx->transaction;
752 uint64_t old_seqnum, new_seqnum;
756 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
760 if (h->nested_cancel) {
761 db->transaction_cancel(db);
762 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
766 if (h->nesting != 0) {
768 DEBUG(5, (__location__ " transaction commit on db 0x%08x: nesting %d -> %d\n",
769 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
773 if (h->m_write == NULL) {
775 * No changes were made, so don't change the seqnum,
776 * don't push to other node, just exit with success.
782 DEBUG(5,(__location__ " transaction commit on db 0x%08x\n", ctx->db_id));
785 * As the last db action before committing, bump the database sequence
786 * number. Note that this undoes all changes to the seqnum records
787 * performed under the transaction. This record is not meant to be
788 * modified by user interaction. It is for internal use only...
790 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
791 if (!NT_STATUS_IS_OK(rets)) {
792 DEBUG(1, (__location__ " failed to fetch the db sequence number "
793 "in transaction commit on db 0x%08x\n", ctx->db_id));
798 new_seqnum = old_seqnum + 1;
800 rets = db_ctdb_store_db_seqnum(h, new_seqnum);
801 if (!NT_STATUS_IS_OK(rets)) {
802 DEBUG(1, (__location__ "failed to store the db sequence number "
803 " in transaction commit on db 0x%08x\n", ctx->db_id));
809 /* tell ctdbd to commit to the other nodes */
810 ret = ctdbd_control_local(ctx->conn, CTDB_CONTROL_TRANS3_COMMIT,
812 db_ctdb_marshall_finish(h->m_write),
813 NULL, NULL, &status);
814 if ((ret != 0) || status != 0) {
816 * The TRANS3_COMMIT control should only possibly fail when a
817 * recovery has been running concurrently. In any case, the db
818 * will be the same on all nodes, either the new copy or the
819 * old copy. This can be detected by comparing the old and new
820 * local sequence numbers.
822 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
823 if (!NT_STATUS_IS_OK(rets)) {
824 DEBUG(1, (__location__ " failed to refetch db sequence "
825 "number after failed TRANS3_COMMIT\n"));
830 if (new_seqnum == old_seqnum) {
831 /* Recovery prevented all our changes: retry. */
834 if (new_seqnum != (old_seqnum + 1)) {
835 DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
836 "old_seqnum[%lu] + (0 or 1) after failed "
837 "TRANS3_COMMIT - this should not happen!\n",
838 (unsigned long)new_seqnum,
839 (unsigned long)old_seqnum));
844 * Recovery propagated our changes to all nodes, completing
845 * our commit for us - succeed.
852 h->ctx->transaction = NULL;
861 static int db_ctdb_transaction_cancel(struct db_context *db)
863 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
865 struct db_ctdb_transaction_handle *h = ctx->transaction;
868 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
872 if (h->nesting != 0) {
874 h->nested_cancel = true;
875 DEBUG(5, (__location__ " transaction cancel on db 0x%08x: nesting %d -> %d\n",
876 ctx->db_id, ctx->transaction->nesting + 1, ctx->transaction->nesting));
880 DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
882 ctx->transaction = NULL;
888 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
890 struct db_ctdb_rec *crec = talloc_get_type_abort(
891 rec->private_data, struct db_ctdb_rec);
893 return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
898 static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
900 NTSTATUS status = NT_STATUS_OK;
902 struct ctdb_control_schedule_for_deletion *dd;
905 struct db_ctdb_rec *crec = talloc_get_type_abort(
906 rec->private_data, struct db_ctdb_rec);
907 struct db_ctdb_ctx *ctx = crec->ctdb_ctx;
909 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
910 indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
911 if (indata.dptr == NULL) {
912 DEBUG(0, (__location__ " talloc failed!\n"));
913 return NT_STATUS_NO_MEMORY;
916 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
917 dd->db_id = ctx->db_id;
918 dd->hdr = crec->header;
919 dd->keylen = rec->key.dsize;
920 memcpy(dd->key, rec->key.dptr, rec->key.dsize);
922 ret = ctdbd_control_local(ctx->conn,
923 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
924 crec->ctdb_ctx->db_id,
925 CTDB_CTRL_FLAG_NOREPLY, /* flags */
930 talloc_free(indata.dptr);
932 if ((ret != 0) || cstatus != 0) {
933 DEBUG(1, (__location__ " Error sending local control "
934 "SCHEDULE_FOR_DELETION: %s, cstatus = %"PRIi32"\n",
935 strerror(ret), cstatus));
937 status = map_nt_error_from_unix(ret);
939 status = NT_STATUS_UNSUCCESSFUL;
946 static NTSTATUS db_ctdb_delete(struct db_record *rec)
951 * We have to store the header with empty data. TODO: Fix the
955 status = db_ctdb_store(rec, tdb_null, 0);
956 if (!NT_STATUS_IS_OK(status)) {
960 status = db_ctdb_send_schedule_for_deletion(rec);
964 static int db_ctdb_record_destr(struct db_record* data)
966 struct db_ctdb_rec *crec = talloc_get_type_abort(
967 data->private_data, struct db_ctdb_rec);
970 struct timeval before;
973 DEBUG(10, (DEBUGLEVEL > 10
974 ? "Unlocking db %u key %s\n"
975 : "Unlocking db %u key %.20s\n",
976 (int)crec->ctdb_ctx->db_id,
977 hex_encode_talloc(data, (unsigned char *)data->key.dptr,
980 before = timeval_current();
982 ret = tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key);
984 timediff = timeval_elapsed(&before);
985 timediff *= 1000; /* get us milliseconds */
987 if (timediff > crec->ctdb_ctx->warn_unlock_msecs) {
989 key = hex_encode_talloc(talloc_tos(),
990 (unsigned char *)data->key.dptr,
992 DEBUG(0, ("tdb_chainunlock on db %s, key %s took %f milliseconds\n",
993 tdb_name(crec->ctdb_ctx->wtdb->tdb), key,
999 DEBUG(0, ("tdb_chainunlock failed\n"));
1003 threshold = crec->ctdb_ctx->warn_locktime_msecs;
1004 if (threshold != 0) {
1005 timediff = timeval_elapsed(&crec->lock_time) * 1000;
1006 if (timediff > threshold) {
1009 key = hex_encode_talloc(data,
1010 (unsigned char *)data->key.dptr,
1012 DEBUG(0, ("Held tdb lock on db %s, key %s "
1013 "%f milliseconds\n",
1014 tdb_name(crec->ctdb_ctx->wtdb->tdb),
1023 * Check whether we have a valid local copy of the given record,
1024 * either for reading or for writing.
1026 static bool db_ctdb_can_use_local_hdr(const struct ctdb_ltdb_header *hdr,
1027 uint32_t my_vnn, bool read_only)
1029 if (hdr->dmaster != my_vnn) {
1030 /* If we're not dmaster, it must be r/o copy. */
1031 return read_only && (hdr->flags & CTDB_REC_RO_HAVE_READONLY);
1035 * If we want write access, no one may have r/o copies.
1037 return read_only || !(hdr->flags & CTDB_REC_RO_HAVE_DELEGATIONS);
1040 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data, uint32_t my_vnn,
1043 if (ctdb_data.dptr == NULL) {
1047 if (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
1051 return db_ctdb_can_use_local_hdr(
1052 (struct ctdb_ltdb_header *)ctdb_data.dptr, my_vnn, read_only);
1055 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
1056 TALLOC_CTX *mem_ctx,
1060 struct db_record *result;
1061 struct db_ctdb_rec *crec;
1063 int migrate_attempts;
1064 struct timeval migrate_start;
1065 struct timeval chainlock_start;
1066 struct timeval ctdb_start_time;
1067 double chainlock_time = 0;
1068 double ctdb_time = 0;
1073 if (!(result = talloc(mem_ctx, struct db_record))) {
1074 DEBUG(0, ("talloc failed\n"));
1078 if (!(crec = talloc_zero(result, struct db_ctdb_rec))) {
1079 DEBUG(0, ("talloc failed\n"));
1080 TALLOC_FREE(result);
1084 result->db = ctx->db;
1085 result->private_data = (void *)crec;
1086 crec->ctdb_ctx = ctx;
1088 result->key.dsize = key.dsize;
1089 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
1091 if (result->key.dptr == NULL) {
1092 DEBUG(0, ("talloc failed\n"));
1093 TALLOC_FREE(result);
1097 migrate_attempts = 0;
1098 GetTimeOfDay(&migrate_start);
1101 * Do a blocking lock on the record
1105 if (DEBUGLEVEL >= 10) {
1106 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1107 DEBUG(10, (DEBUGLEVEL > 10
1108 ? "Locking db %u key %s\n"
1109 : "Locking db %u key %.20s\n",
1110 (int)crec->ctdb_ctx->db_id, keystr));
1111 TALLOC_FREE(keystr);
1114 GetTimeOfDay(&chainlock_start);
1116 ? tdb_chainlock_nonblock(ctx->wtdb->tdb, key)
1117 : tdb_chainlock(ctx->wtdb->tdb, key);
1118 chainlock_time += timeval_elapsed(&chainlock_start);
1121 DEBUG(3, ("tdb_chainlock failed\n"));
1122 TALLOC_FREE(result);
1126 result->store = db_ctdb_store;
1127 result->delete_rec = db_ctdb_delete;
1128 talloc_set_destructor(result, db_ctdb_record_destr);
1130 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
1133 * See if we have a valid record and we are the dmaster. If so, we can
1134 * take the shortcut and just return it.
1137 if (!db_ctdb_can_use_local_copy(ctdb_data, ctdbd_vnn(ctx->conn),
1139 SAFE_FREE(ctdb_data.dptr);
1140 tdb_chainunlock(ctx->wtdb->tdb, key);
1141 talloc_set_destructor(result, NULL);
1143 if (tryonly && (migrate_attempts != 0)) {
1144 DEBUG(5, ("record migrated away again\n"));
1145 TALLOC_FREE(result);
1149 migrate_attempts += 1;
1151 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %"PRIu32" "
1152 "(%"PRIu32") %"PRIu32"\n",
1153 ctdb_data.dptr, ctdb_data.dptr ?
1154 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster :
1156 ctdbd_vnn(ctx->conn),
1158 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->flags : 0));
1160 GetTimeOfDay(&ctdb_start_time);
1161 ret = ctdbd_migrate(ctx->conn, ctx->db_id, key);
1162 ctdb_time += timeval_elapsed(&ctdb_start_time);
1165 DEBUG(5, ("ctdb_migrate failed: %s\n",
1167 TALLOC_FREE(result);
1170 /* now its migrated, try again */
1176 duration = timeval_elapsed(&migrate_start);
1179 * Convert the duration to milliseconds to avoid a
1180 * floating-point division of
1181 * lp_parm_int("migrate_duration") by 1000.
1183 duration_msecs = duration * 1000;
1186 if ((migrate_attempts > ctx->warn_migrate_attempts) ||
1187 (duration_msecs > ctx->warn_migrate_msecs)) {
1190 if (tdb_get_flags(ctx->wtdb->tdb) & TDB_INCOMPATIBLE_HASH) {
1191 chain = tdb_jenkins_hash(&key) %
1192 tdb_hash_size(ctx->wtdb->tdb);
1195 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s, chain %d "
1196 "needed %d attempts, %d milliseconds, "
1197 "chainlock: %f ms, CTDB %f ms\n",
1198 tdb_name(ctx->wtdb->tdb),
1199 hex_encode_talloc(talloc_tos(),
1200 (unsigned char *)key.dptr,
1203 migrate_attempts, duration_msecs,
1204 chainlock_time * 1000.0,
1205 ctdb_time * 1000.0));
1208 GetTimeOfDay(&crec->lock_time);
1210 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1212 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1213 result->value.dptr = NULL;
1215 if ((result->value.dsize != 0)
1216 && !(result->value.dptr = (uint8_t *)talloc_memdup(
1217 result, ctdb_data.dptr + sizeof(crec->header),
1218 result->value.dsize))) {
1219 DEBUG(0, ("talloc failed\n"));
1220 TALLOC_FREE(result);
1223 SAFE_FREE(ctdb_data.dptr);
1228 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1229 TALLOC_CTX *mem_ctx,
1232 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1233 struct db_ctdb_ctx);
1235 if (ctx->transaction != NULL) {
1236 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1239 if (db->persistent) {
1240 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1243 return fetch_locked_internal(ctx, mem_ctx, key, false);
1246 static struct db_record *db_ctdb_try_fetch_locked(struct db_context *db,
1247 TALLOC_CTX *mem_ctx,
1250 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1251 struct db_ctdb_ctx);
1253 if (ctx->transaction != NULL) {
1254 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1257 if (db->persistent) {
1258 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1261 return fetch_locked_internal(ctx, mem_ctx, key, true);
1264 struct db_ctdb_parse_record_state {
1265 void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data);
1268 bool ask_for_readonly_copy;
1273 static void db_ctdb_parse_record_parser(
1274 TDB_DATA key, struct ctdb_ltdb_header *header,
1275 TDB_DATA data, void *private_data)
1277 struct db_ctdb_parse_record_state *state =
1278 (struct db_ctdb_parse_record_state *)private_data;
1279 state->parser(key, data, state->private_data);
1282 static void db_ctdb_parse_record_parser_nonpersistent(
1283 TDB_DATA key, struct ctdb_ltdb_header *header,
1284 TDB_DATA data, void *private_data)
1286 struct db_ctdb_parse_record_state *state =
1287 (struct db_ctdb_parse_record_state *)private_data;
1289 if (db_ctdb_can_use_local_hdr(header, state->my_vnn, true)) {
1291 * A record consisting only of the ctdb header can be
1292 * a validly created empty record or a tombstone
1293 * record of a deleted record (not vacuumed yet). Mark
1296 state->empty_record = (data.dsize == 0);
1297 if (!state->empty_record) {
1298 state->parser(key, data, state->private_data);
1303 * We found something in the db, so it seems that this record,
1304 * while not usable locally right now, is popular. Ask for a
1307 state->ask_for_readonly_copy = true;
1311 static NTSTATUS db_ctdb_try_parse_local_record(struct db_ctdb_ctx *ctx,
1313 struct db_ctdb_parse_record_state *state)
1317 if (ctx->transaction != NULL) {
1318 struct db_ctdb_transaction_handle *h = ctx->transaction;
1322 * Transactions only happen for persistent db's.
1325 found = parse_newest_in_marshall_buffer(
1326 h->m_write, key, db_ctdb_parse_record_parser, state);
1329 return NT_STATUS_OK;
1333 if (ctx->db->persistent) {
1335 * Persistent db, but not found in the transaction buffer
1337 return db_ctdb_ltdb_parse(
1338 ctx, key, db_ctdb_parse_record_parser, state);
1341 state->done = false;
1342 state->ask_for_readonly_copy = false;
1344 status = db_ctdb_ltdb_parse(
1345 ctx, key, db_ctdb_parse_record_parser_nonpersistent, state);
1346 if (NT_STATUS_IS_OK(status) && state->done) {
1347 if (state->empty_record) {
1349 * We know authoritatively, that this is an empty
1350 * record. Since ctdb does not distinguish between empty
1351 * and deleted records, this can be a record stored as
1352 * empty or a not-yet-vacuumed tombstone record of a
1353 * deleted record. Now Samba right now can live without
1354 * empty records, so we can safely report this record
1357 * See bugs 10008 and 12005.
1359 return NT_STATUS_NOT_FOUND;
1361 return NT_STATUS_OK;
1364 return NT_STATUS_MORE_PROCESSING_REQUIRED;
1367 static NTSTATUS db_ctdb_parse_record(struct db_context *db, TDB_DATA key,
1368 void (*parser)(TDB_DATA key,
1370 void *private_data),
1373 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1374 db->private_data, struct db_ctdb_ctx);
1375 struct db_ctdb_parse_record_state state;
1379 state.parser = parser;
1380 state.private_data = private_data;
1381 state.my_vnn = ctdbd_vnn(ctx->conn);
1382 state.empty_record = false;
1384 status = db_ctdb_try_parse_local_record(ctx, key, &state);
1385 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1389 ret = ctdbd_parse(ctx->conn, ctx->db_id, key,
1390 state.ask_for_readonly_copy, parser, private_data);
1392 if (ret == ENOENT) {
1395 * NT_STATUS_OBJECT_NAME_NOT_FOUND. Our upper
1396 * layers expect NT_STATUS_NOT_FOUND for "no
1397 * record around". We need to convert dbwrap
1398 * to 0/errno away from NTSTATUS ... :-)
1400 return NT_STATUS_NOT_FOUND;
1402 return map_nt_error_from_unix(ret);
1404 return NT_STATUS_OK;
1407 static void db_ctdb_parse_record_done(struct tevent_req *subreq);
1409 static struct tevent_req *db_ctdb_parse_record_send(
1410 TALLOC_CTX *mem_ctx,
1411 struct tevent_context *ev,
1412 struct db_context *db,
1414 void (*parser)(TDB_DATA key,
1416 void *private_data),
1418 enum dbwrap_req_state *req_state)
1420 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1421 db->private_data, struct db_ctdb_ctx);
1422 struct tevent_req *req = NULL;
1423 struct tevent_req *subreq = NULL;
1424 struct db_ctdb_parse_record_state *state = NULL;
1427 req = tevent_req_create(mem_ctx, &state,
1428 struct db_ctdb_parse_record_state);
1430 *req_state = DBWRAP_REQ_ERROR;
1435 *state = (struct db_ctdb_parse_record_state) {
1437 .private_data = private_data,
1438 .my_vnn = ctdbd_vnn(ctx->conn),
1439 .empty_record = false,
1442 status = db_ctdb_try_parse_local_record(ctx, key, state);
1443 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1444 if (tevent_req_nterror(req, status)) {
1445 *req_state = DBWRAP_REQ_ERROR;
1446 return tevent_req_post(req, ev);
1448 *req_state = DBWRAP_REQ_DONE;
1449 tevent_req_done(req);
1450 return tevent_req_post(req, ev);
1453 subreq = ctdbd_parse_send(state,
1455 ctdb_async_ctx.async_conn,
1458 state->ask_for_readonly_copy,
1462 if (tevent_req_nomem(subreq, req)) {
1463 *req_state = DBWRAP_REQ_ERROR;
1464 return tevent_req_post(req, ev);
1466 tevent_req_set_callback(subreq, db_ctdb_parse_record_done, req);
1471 static void db_ctdb_parse_record_done(struct tevent_req *subreq)
1473 struct tevent_req *req = tevent_req_callback_data(
1474 subreq, struct tevent_req);
1477 ret = ctdbd_parse_recv(subreq);
1478 TALLOC_FREE(subreq);
1480 if (ret == ENOENT) {
1482 * This maps to NT_STATUS_OBJECT_NAME_NOT_FOUND. Our
1483 * upper layers expect NT_STATUS_NOT_FOUND for "no
1484 * record around". We need to convert dbwrap to 0/errno
1485 * away from NTSTATUS ... :-)
1487 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
1490 tevent_req_nterror(req, map_nt_error_from_unix(ret));
1494 tevent_req_done(req);
1498 static NTSTATUS db_ctdb_parse_record_recv(struct tevent_req *req)
1500 return tevent_req_simple_recv_ntstatus(req);
1503 struct traverse_state {
1504 struct db_context *db;
1505 int (*fn)(struct db_record *rec, void *private_data);
1510 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1512 struct traverse_state *state = (struct traverse_state *)private_data;
1513 struct db_record *rec;
1514 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1515 /* we have to give them a locked record to prevent races */
1516 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1517 if (rec && rec->value.dsize > 0) {
1518 state->fn(rec, state->private_data);
1520 talloc_free(tmp_ctx);
1523 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1526 struct traverse_state *state = (struct traverse_state *)private_data;
1527 struct db_record *rec;
1528 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1532 * Skip the __db_sequence_number__ key:
1533 * This is used for persistent transactions internally.
1535 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1536 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1541 /* we have to give them a locked record to prevent races */
1542 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1543 if (rec && rec->value.dsize > 0) {
1544 ret = state->fn(rec, state->private_data);
1548 talloc_free(tmp_ctx);
1552 /* wrapper to use traverse_persistent_callback with dbwrap */
1553 static int traverse_persistent_callback_dbwrap(struct db_record *rec, void* data)
1555 return traverse_persistent_callback(NULL, rec->key, rec->value, data);
1558 static int db_ctdbd_traverse(uint32_t db_id,
1559 void (*fn)(TDB_DATA key, TDB_DATA data,
1560 void *private_data),
1563 struct ctdbd_connection *conn;
1567 ret = ctdbd_init_connection(talloc_tos(), lp_ctdbd_socket(),
1568 lp_ctdb_timeout(), &conn);
1571 DBG_WARNING("ctdbd_init_connection failed: %s\n",
1576 ret = ctdbd_traverse(conn, db_id, fn, private_data);
1580 DBG_WARNING("ctdbd_traverse failed: %s\n",
1589 static int db_ctdb_traverse(struct db_context *db,
1590 int (*fn)(struct db_record *rec,
1591 void *private_data),
1595 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1596 struct db_ctdb_ctx);
1597 struct traverse_state state;
1601 state.private_data = private_data;
1604 if (db->persistent) {
1605 struct tdb_context *ltdb = ctx->wtdb->tdb;
1607 /* for persistent databases we don't need to do a ctdb traverse,
1608 we can do a faster local traverse */
1609 ret = tdb_traverse(ltdb, traverse_persistent_callback, &state);
1613 if (ctx->transaction && ctx->transaction->m_write) {
1615 * we now have to handle keys not yet
1616 * present at transaction start
1618 struct db_context *newkeys = db_open_rbt(talloc_tos());
1619 struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
1620 struct ctdb_rec_data_old *rec=NULL;
1625 if (newkeys == NULL) {
1629 for (i=0; i<mbuf->count; i++) {
1631 rec = db_ctdb_marshall_loop_next_key(
1633 SMB_ASSERT(rec != NULL);
1635 if (!tdb_exists(ltdb, key)) {
1636 dbwrap_store(newkeys, key, tdb_null, 0);
1639 status = dbwrap_traverse(newkeys,
1640 traverse_persistent_callback_dbwrap,
1643 talloc_free(newkeys);
1644 if (!NT_STATUS_IS_OK(status)) {
1652 ret = db_ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1659 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1661 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1664 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1666 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1669 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1671 struct traverse_state *state = (struct traverse_state *)private_data;
1672 struct db_record rec;
1678 rec.store = db_ctdb_store_deny;
1679 rec.delete_rec = db_ctdb_delete_deny;
1680 rec.private_data = NULL;
1681 state->fn(&rec, state->private_data);
1685 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1688 struct traverse_state *state = (struct traverse_state *)private_data;
1689 struct db_record rec;
1692 * Skip the __db_sequence_number__ key:
1693 * This is used for persistent transactions internally.
1695 if (kbuf.dsize == strlen(CTDB_DB_SEQNUM_KEY) + 1 &&
1696 strcmp((const char*)kbuf.dptr, CTDB_DB_SEQNUM_KEY) == 0)
1705 rec.store = db_ctdb_store_deny;
1706 rec.delete_rec = db_ctdb_delete_deny;
1707 rec.private_data = NULL;
1709 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1710 /* a deleted record */
1713 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1714 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1717 return state->fn(&rec, state->private_data);
1720 static int db_ctdb_traverse_read(struct db_context *db,
1721 int (*fn)(struct db_record *rec,
1722 void *private_data),
1726 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1727 struct db_ctdb_ctx);
1728 struct traverse_state state;
1732 state.private_data = private_data;
1735 if (db->persistent) {
1736 /* for persistent databases we don't need to do a ctdb traverse,
1737 we can do a faster local traverse */
1738 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1741 ret = db_ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1748 static int db_ctdb_get_seqnum(struct db_context *db)
1750 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1751 struct db_ctdb_ctx);
1752 return tdb_get_seqnum(ctx->wtdb->tdb);
1755 static size_t db_ctdb_id(struct db_context *db, uint8_t *id, size_t idlen)
1757 struct db_ctdb_ctx *ctx = talloc_get_type_abort(
1758 db->private_data, struct db_ctdb_ctx);
1760 if (idlen >= sizeof(ctx->db_id)) {
1761 memcpy(id, &ctx->db_id, sizeof(ctx->db_id));
1764 return sizeof(ctx->db_id);
1767 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1768 struct messaging_context *msg_ctx,
1769 struct ctdbd_connection *conn,
1771 int hash_size, int tdb_flags,
1772 int open_flags, mode_t mode,
1773 enum dbwrap_lock_order lock_order,
1774 uint64_t dbwrap_flags)
1776 struct db_context *result;
1777 struct db_ctdb_ctx *db_ctdb;
1779 struct loadparm_context *lp_ctx;
1783 if (!lp_clustering()) {
1784 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1788 if (!(result = talloc_zero(mem_ctx, struct db_context))) {
1789 DEBUG(0, ("talloc failed\n"));
1790 TALLOC_FREE(result);
1794 if (!(db_ctdb = talloc(result, struct db_ctdb_ctx))) {
1795 DEBUG(0, ("talloc failed\n"));
1796 TALLOC_FREE(result);
1800 result->name = talloc_strdup(result, name);
1801 if (result->name == NULL) {
1802 DEBUG(0, ("talloc failed\n"));
1803 TALLOC_FREE(result);
1807 db_ctdb->transaction = NULL;
1808 db_ctdb->db = result;
1809 db_ctdb->conn = conn;
1811 ret = ctdbd_db_attach(db_ctdb->conn, name, &db_ctdb->db_id, tdb_flags);
1813 DEBUG(0, ("ctdbd_db_attach failed for %s: %s\n", name,
1815 TALLOC_FREE(result);
1819 db_path = ctdbd_dbpath(db_ctdb->conn, db_ctdb, db_ctdb->db_id);
1821 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1822 result->lock_order = lock_order;
1824 /* only pass through specific flags */
1825 tdb_flags &= TDB_SEQNUM|TDB_VOLATILE|
1826 TDB_MUTEX_LOCKING|TDB_CLEAR_IF_FIRST;
1828 if (!result->persistent) {
1829 ret = ctdb_async_ctx_init(NULL, messaging_tevent_context(msg_ctx));
1831 DBG_ERR("ctdb_async_ctx_init failed: %s\n", strerror(ret));
1832 TALLOC_FREE(result);
1837 if (!result->persistent &&
1838 (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS))
1842 indata = make_tdb_data((uint8_t *)&db_ctdb->db_id,
1843 sizeof(db_ctdb->db_id));
1845 ret = ctdbd_control_local(
1846 db_ctdb->conn, CTDB_CONTROL_SET_DB_READONLY, 0, 0,
1847 indata, NULL, NULL, &cstatus);
1848 if ((ret != 0) || (cstatus != 0)) {
1849 DEBUG(1, ("CTDB_CONTROL_SET_DB_READONLY failed: "
1850 "%s, %"PRIi32"\n", strerror(ret), cstatus));
1851 TALLOC_FREE(result);
1856 lp_ctx = loadparm_init_s3(db_path, loadparm_s3_helpers());
1858 if (hash_size == 0) {
1859 hash_size = lpcfg_tdb_hash_size(lp_ctx, db_path);
1862 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size,
1863 lpcfg_tdb_flags(lp_ctx, tdb_flags),
1865 talloc_unlink(db_path, lp_ctx);
1866 if (db_ctdb->wtdb == NULL) {
1867 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1868 TALLOC_FREE(result);
1871 talloc_free(db_path);
1873 /* honor permissions if user has specified O_CREAT */
1874 if (open_flags & O_CREAT) {
1876 fd = tdb_fd(db_ctdb->wtdb->tdb);
1877 ret = fchmod(fd, mode);
1879 DBG_WARNING("fchmod failed: %s\n",
1881 TALLOC_FREE(result);
1886 if (result->persistent) {
1887 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb, msg_ctx);
1888 if (db_ctdb->lock_ctx == NULL) {
1889 DEBUG(0, ("g_lock_ctx_init failed\n"));
1890 TALLOC_FREE(result);
1895 db_ctdb->warn_unlock_msecs = lp_parm_int(-1, "ctdb",
1896 "unlock_warn_threshold", 5);
1897 db_ctdb->warn_migrate_attempts = lp_parm_int(-1, "ctdb",
1898 "migrate_attempts", 10);
1899 db_ctdb->warn_migrate_msecs = lp_parm_int(-1, "ctdb",
1900 "migrate_duration", 5000);
1901 db_ctdb->warn_locktime_msecs = lp_ctdb_locktime_warn_threshold();
1903 result->private_data = (void *)db_ctdb;
1904 result->fetch_locked = db_ctdb_fetch_locked;
1905 result->try_fetch_locked = db_ctdb_try_fetch_locked;
1906 result->parse_record = db_ctdb_parse_record;
1907 result->parse_record_send = db_ctdb_parse_record_send;
1908 result->parse_record_recv = db_ctdb_parse_record_recv;
1909 result->traverse = db_ctdb_traverse;
1910 result->traverse_read = db_ctdb_traverse_read;
1911 result->get_seqnum = db_ctdb_get_seqnum;
1912 result->transaction_start = db_ctdb_transaction_start;
1913 result->transaction_commit = db_ctdb_transaction_commit;
1914 result->transaction_cancel = db_ctdb_transaction_cancel;
1915 result->id = db_ctdb_id;
1917 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1918 name, db_ctdb->db_id));